blob: a3d010a247473876c833c7615d205c73243d5bbc [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
2 * Copyright (C) 2009 - QLogic Corporation.
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called "COPYING".
22 *
23 */
24
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000026#include <linux/vmalloc.h>
27#include <linux/interrupt.h>
28
29#include "qlcnic.h"
30
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>
37
38MODULE_DESCRIPTION("QLogic 10 GbE Converged Ethernet Driver");
39MODULE_LICENSE("GPL");
40MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
41MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
42
43char qlcnic_driver_name[] = "qlcnic";
44static const char qlcnic_driver_string[] = "QLogic Converged Ethernet Driver v"
45 QLCNIC_LINUX_VERSIONID;
46
47static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
48
49/* Default to restricted 1G auto-neg mode */
50static int wol_port_mode = 5;
51
52static int use_msi = 1;
53module_param(use_msi, int, 0644);
54MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
55
56static int use_msi_x = 1;
57module_param(use_msi_x, int, 0644);
58MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
59
60static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
61module_param(auto_fw_reset, int, 0644);
62MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
63
64static int __devinit qlcnic_probe(struct pci_dev *pdev,
65 const struct pci_device_id *ent);
66static void __devexit qlcnic_remove(struct pci_dev *pdev);
67static int qlcnic_open(struct net_device *netdev);
68static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000069static void qlcnic_tx_timeout(struct net_device *netdev);
70static void qlcnic_tx_timeout_task(struct work_struct *work);
71static void qlcnic_attach_work(struct work_struct *work);
72static void qlcnic_fwinit_work(struct work_struct *work);
73static void qlcnic_fw_poll_work(struct work_struct *work);
74static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
75 work_func_t func, int delay);
76static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
77static int qlcnic_poll(struct napi_struct *napi, int budget);
78#ifdef CONFIG_NET_POLL_CONTROLLER
79static void qlcnic_poll_controller(struct net_device *netdev);
80#endif
81
82static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
83static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
84static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
85static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
86
87static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter);
88static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
89
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +000090static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000091static irqreturn_t qlcnic_intr(int irq, void *data);
92static irqreturn_t qlcnic_msi_intr(int irq, void *data);
93static irqreturn_t qlcnic_msix_intr(int irq, void *data);
94
95static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
96static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
97
98/* PCI Device ID Table */
99#define ENTRY(device) \
100 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
101 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
102
103#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
104
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000105static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000106 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
107 {0,}
108};
109
110MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
111
112
113void
114qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
115 struct qlcnic_host_tx_ring *tx_ring)
116{
117 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
118
119 if (qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH) {
120 netif_stop_queue(adapter->netdev);
121 smp_mb();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +0000122 adapter->stats.xmit_off++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000123 }
124}
125
126static const u32 msi_tgt_status[8] = {
127 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
128 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
129 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
130 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
131};
132
133static const
134struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
135
136static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
137{
138 writel(0, sds_ring->crb_intr_mask);
139}
140
141static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
142{
143 struct qlcnic_adapter *adapter = sds_ring->adapter;
144
145 writel(0x1, sds_ring->crb_intr_mask);
146
147 if (!QLCNIC_IS_MSI_FAMILY(adapter))
148 writel(0xfbff, adapter->tgt_mask_reg);
149}
150
151static int
152qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
153{
154 int size = sizeof(struct qlcnic_host_sds_ring) * count;
155
156 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
157
158 return (recv_ctx->sds_rings == NULL);
159}
160
161static void
162qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
163{
164 if (recv_ctx->sds_rings != NULL)
165 kfree(recv_ctx->sds_rings);
166
167 recv_ctx->sds_rings = NULL;
168}
169
170static int
171qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
172{
173 int ring;
174 struct qlcnic_host_sds_ring *sds_ring;
175 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
176
177 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
178 return -ENOMEM;
179
180 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
181 sds_ring = &recv_ctx->sds_rings[ring];
182 netif_napi_add(netdev, &sds_ring->napi,
183 qlcnic_poll, QLCNIC_NETDEV_WEIGHT);
184 }
185
186 return 0;
187}
188
189static void
190qlcnic_napi_del(struct qlcnic_adapter *adapter)
191{
192 int ring;
193 struct qlcnic_host_sds_ring *sds_ring;
194 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
195
196 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
197 sds_ring = &recv_ctx->sds_rings[ring];
198 netif_napi_del(&sds_ring->napi);
199 }
200
201 qlcnic_free_sds_rings(&adapter->recv_ctx);
202}
203
204static void
205qlcnic_napi_enable(struct qlcnic_adapter *adapter)
206{
207 int ring;
208 struct qlcnic_host_sds_ring *sds_ring;
209 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
210
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000211 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
212 return;
213
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000214 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
215 sds_ring = &recv_ctx->sds_rings[ring];
216 napi_enable(&sds_ring->napi);
217 qlcnic_enable_int(sds_ring);
218 }
219}
220
221static void
222qlcnic_napi_disable(struct qlcnic_adapter *adapter)
223{
224 int ring;
225 struct qlcnic_host_sds_ring *sds_ring;
226 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
227
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000228 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
229 return;
230
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000231 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
232 sds_ring = &recv_ctx->sds_rings[ring];
233 qlcnic_disable_int(sds_ring);
234 napi_synchronize(&sds_ring->napi);
235 napi_disable(&sds_ring->napi);
236 }
237}
238
239static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
240{
241 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000242}
243
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000244static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
245{
246 u32 val, data;
247
248 val = adapter->ahw.board_type;
249 if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
250 (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
251 if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
252 data = QLCNIC_PORT_MODE_802_3_AP;
253 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
254 } else if (port_mode == QLCNIC_PORT_MODE_XG) {
255 data = QLCNIC_PORT_MODE_XG;
256 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
257 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
258 data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
259 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
260 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
261 data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
262 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
263 } else {
264 data = QLCNIC_PORT_MODE_AUTO_NEG;
265 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
266 }
267
268 if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
269 (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
270 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
271 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
272 wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
273 }
274 QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
275 }
276}
277
278static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
279{
280 u32 control;
281 int pos;
282
283 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
284 if (pos) {
285 pci_read_config_dword(pdev, pos, &control);
286 if (enable)
287 control |= PCI_MSIX_FLAGS_ENABLE;
288 else
289 control = 0;
290 pci_write_config_dword(pdev, pos, control);
291 }
292}
293
294static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
295{
296 int i;
297
298 for (i = 0; i < count; i++)
299 adapter->msix_entries[i].entry = i;
300}
301
302static int
303qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
304{
305 int i;
306 unsigned char *p;
307 u64 mac_addr;
308 struct net_device *netdev = adapter->netdev;
309 struct pci_dev *pdev = adapter->pdev;
310
311 if (qlcnic_get_mac_addr(adapter, &mac_addr) != 0)
312 return -EIO;
313
314 p = (unsigned char *)&mac_addr;
315 for (i = 0; i < 6; i++)
316 netdev->dev_addr[i] = *(p + 5 - i);
317
318 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
319 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
320
321 /* set station address */
322
323 if (!is_valid_ether_addr(netdev->perm_addr))
324 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
325 netdev->dev_addr);
326
327 return 0;
328}
329
330static int qlcnic_set_mac(struct net_device *netdev, void *p)
331{
332 struct qlcnic_adapter *adapter = netdev_priv(netdev);
333 struct sockaddr *addr = p;
334
335 if (!is_valid_ether_addr(addr->sa_data))
336 return -EINVAL;
337
338 if (netif_running(netdev)) {
339 netif_device_detach(netdev);
340 qlcnic_napi_disable(adapter);
341 }
342
343 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
344 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
345 qlcnic_set_multi(adapter->netdev);
346
347 if (netif_running(netdev)) {
348 netif_device_attach(netdev);
349 qlcnic_napi_enable(adapter);
350 }
351 return 0;
352}
353
354static const struct net_device_ops qlcnic_netdev_ops = {
355 .ndo_open = qlcnic_open,
356 .ndo_stop = qlcnic_close,
357 .ndo_start_xmit = qlcnic_xmit_frame,
358 .ndo_get_stats = qlcnic_get_stats,
359 .ndo_validate_addr = eth_validate_addr,
360 .ndo_set_multicast_list = qlcnic_set_multi,
361 .ndo_set_mac_address = qlcnic_set_mac,
362 .ndo_change_mtu = qlcnic_change_mtu,
363 .ndo_tx_timeout = qlcnic_tx_timeout,
364#ifdef CONFIG_NET_POLL_CONTROLLER
365 .ndo_poll_controller = qlcnic_poll_controller,
366#endif
367};
368
369static void
370qlcnic_setup_intr(struct qlcnic_adapter *adapter)
371{
372 const struct qlcnic_legacy_intr_set *legacy_intrp;
373 struct pci_dev *pdev = adapter->pdev;
374 int err, num_msix;
375
376 if (adapter->rss_supported) {
377 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
378 MSIX_ENTRIES_PER_ADAPTER : 2;
379 } else
380 num_msix = 1;
381
382 adapter->max_sds_rings = 1;
383
384 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
385
386 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
387
388 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
389 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
390 legacy_intrp->tgt_status_reg);
391 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
392 legacy_intrp->tgt_mask_reg);
393 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
394
395 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
396 ISR_INT_STATE_REG);
397
398 qlcnic_set_msix_bit(pdev, 0);
399
400 if (adapter->msix_supported) {
401
402 qlcnic_init_msix_entries(adapter, num_msix);
403 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
404 if (err == 0) {
405 adapter->flags |= QLCNIC_MSIX_ENABLED;
406 qlcnic_set_msix_bit(pdev, 1);
407
408 if (adapter->rss_supported)
409 adapter->max_sds_rings = num_msix;
410
411 dev_info(&pdev->dev, "using msi-x interrupts\n");
412 return;
413 }
414
415 if (err > 0)
416 pci_disable_msix(pdev);
417
418 /* fall through for msi */
419 }
420
421 if (use_msi && !pci_enable_msi(pdev)) {
422 adapter->flags |= QLCNIC_MSI_ENABLED;
423 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
424 msi_tgt_status[adapter->ahw.pci_func]);
425 dev_info(&pdev->dev, "using msi interrupts\n");
426 adapter->msix_entries[0].vector = pdev->irq;
427 return;
428 }
429
430 dev_info(&pdev->dev, "using legacy interrupts\n");
431 adapter->msix_entries[0].vector = pdev->irq;
432}
433
434static void
435qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
436{
437 if (adapter->flags & QLCNIC_MSIX_ENABLED)
438 pci_disable_msix(adapter->pdev);
439 if (adapter->flags & QLCNIC_MSI_ENABLED)
440 pci_disable_msi(adapter->pdev);
441}
442
443static void
444qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
445{
446 if (adapter->ahw.pci_base0 != NULL)
447 iounmap(adapter->ahw.pci_base0);
448}
449
450static int
451qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
452{
453 void __iomem *mem_ptr0 = NULL;
454 resource_size_t mem_base;
455 unsigned long mem_len, pci_len0 = 0;
456
457 struct pci_dev *pdev = adapter->pdev;
458 int pci_func = adapter->ahw.pci_func;
459
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000460 /* remap phys address */
461 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
462 mem_len = pci_resource_len(pdev, 0);
463
464 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
465
466 mem_ptr0 = pci_ioremap_bar(pdev, 0);
467 if (mem_ptr0 == NULL) {
468 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
469 return -EIO;
470 }
471 pci_len0 = mem_len;
472 } else {
473 return -EIO;
474 }
475
476 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
477
478 adapter->ahw.pci_base0 = mem_ptr0;
479 adapter->ahw.pci_len0 = pci_len0;
480
481 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
482 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
483
484 return 0;
485}
486
487static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
488{
489 struct pci_dev *pdev = adapter->pdev;
490 int i, found = 0;
491
492 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
493 if (qlcnic_boards[i].vendor == pdev->vendor &&
494 qlcnic_boards[i].device == pdev->device &&
495 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
496 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
497 strcpy(name, qlcnic_boards[i].short_name);
498 found = 1;
499 break;
500 }
501
502 }
503
504 if (!found)
505 name = "Unknown";
506}
507
508static void
509qlcnic_check_options(struct qlcnic_adapter *adapter)
510{
511 u32 fw_major, fw_minor, fw_build;
512 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
513 char serial_num[32];
514 int i, offset, val;
515 int *ptr32;
516 struct pci_dev *pdev = adapter->pdev;
517
518 adapter->driver_mismatch = 0;
519
520 ptr32 = (int *)&serial_num;
521 offset = QLCNIC_FW_SERIAL_NUM_OFFSET;
522 for (i = 0; i < 8; i++) {
523 if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) {
524 dev_err(&pdev->dev, "error reading board info\n");
525 adapter->driver_mismatch = 1;
526 return;
527 }
528 ptr32[i] = cpu_to_le32(val);
529 offset += sizeof(u32);
530 }
531
532 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
533 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
534 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
535
536 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
537
538 if (adapter->portnum == 0) {
539 get_brd_name(adapter, brd_name);
540
541 pr_info("%s: %s Board Chip rev 0x%x\n",
542 module_name(THIS_MODULE),
543 brd_name, adapter->ahw.revision_id);
544 }
545
546 if (adapter->fw_version < QLCNIC_VERSION_CODE(3, 4, 216)) {
547 adapter->driver_mismatch = 1;
548 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
549 fw_major, fw_minor, fw_build);
550 return;
551 }
552
553 i = QLCRD32(adapter, QLCNIC_SRE_MISC);
554 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
555
556 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
557 fw_major, fw_minor, fw_build,
558 adapter->ahw.cut_through ? "cut-through" : "legacy");
559
560 if (adapter->fw_version >= QLCNIC_VERSION_CODE(4, 0, 222))
561 adapter->capabilities = QLCRD32(adapter, CRB_FW_CAPABILITIES_1);
562
563 adapter->flags &= ~QLCNIC_LRO_ENABLED;
564
565 if (adapter->ahw.port_type == QLCNIC_XGBE) {
566 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
567 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
568 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
569 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
570 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
571 }
572
573 adapter->msix_supported = !!use_msi_x;
574 adapter->rss_supported = !!use_msi_x;
575
576 adapter->num_txd = MAX_CMD_DESCRIPTORS;
577
578 adapter->num_lro_rxd = 0;
579 adapter->max_rds_rings = 2;
580}
581
582static int
583qlcnic_start_firmware(struct qlcnic_adapter *adapter)
584{
585 int val, err, first_boot;
586
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000587 err = qlcnic_can_start_firmware(adapter);
588 if (err < 0)
589 return err;
590 else if (!err)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000591 goto wait_init;
592
593 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
594 if (first_boot == 0x55555555)
595 /* This is the first boot after power up */
596 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
597
598 qlcnic_request_firmware(adapter);
599
600 err = qlcnic_need_fw_reset(adapter);
601 if (err < 0)
602 goto err_out;
603 if (err == 0)
604 goto wait_init;
605
606 if (first_boot != 0x55555555) {
607 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
608 qlcnic_pinit_from_rom(adapter);
609 msleep(1);
610 }
611
612 QLCWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
613 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
614 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
615
616 qlcnic_set_port_mode(adapter);
617
618 err = qlcnic_load_firmware(adapter);
619 if (err)
620 goto err_out;
621
622 qlcnic_release_firmware(adapter);
623
624 val = (_QLCNIC_LINUX_MAJOR << 16)
625 | ((_QLCNIC_LINUX_MINOR << 8))
626 | (_QLCNIC_LINUX_SUBVERSION);
627 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
628
629wait_init:
630 /* Handshake with the card before we register the devices. */
631 err = qlcnic_phantom_init(adapter);
632 if (err)
633 goto err_out;
634
635 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
636
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000637 qlcnic_check_options(adapter);
638
639 adapter->need_fw_reset = 0;
640
641 /* fall through and release firmware */
642
643err_out:
644 qlcnic_release_firmware(adapter);
645 return err;
646}
647
648static int
649qlcnic_request_irq(struct qlcnic_adapter *adapter)
650{
651 irq_handler_t handler;
652 struct qlcnic_host_sds_ring *sds_ring;
653 int err, ring;
654
655 unsigned long flags = 0;
656 struct net_device *netdev = adapter->netdev;
657 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
658
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000659 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
660 handler = qlcnic_tmp_intr;
661 if (!QLCNIC_IS_MSI_FAMILY(adapter))
662 flags |= IRQF_SHARED;
663
664 } else {
665 if (adapter->flags & QLCNIC_MSIX_ENABLED)
666 handler = qlcnic_msix_intr;
667 else if (adapter->flags & QLCNIC_MSI_ENABLED)
668 handler = qlcnic_msi_intr;
669 else {
670 flags |= IRQF_SHARED;
671 handler = qlcnic_intr;
672 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000673 }
674 adapter->irq = netdev->irq;
675
676 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
677 sds_ring = &recv_ctx->sds_rings[ring];
678 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
679 err = request_irq(sds_ring->irq, handler,
680 flags, sds_ring->name, sds_ring);
681 if (err)
682 return err;
683 }
684
685 return 0;
686}
687
688static void
689qlcnic_free_irq(struct qlcnic_adapter *adapter)
690{
691 int ring;
692 struct qlcnic_host_sds_ring *sds_ring;
693
694 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
695
696 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
697 sds_ring = &recv_ctx->sds_rings[ring];
698 free_irq(sds_ring->irq, sds_ring);
699 }
700}
701
702static void
703qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
704{
705 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
706 adapter->coal.normal.data.rx_time_us =
707 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
708 adapter->coal.normal.data.rx_packets =
709 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
710 adapter->coal.normal.data.tx_time_us =
711 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
712 adapter->coal.normal.data.tx_packets =
713 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
714}
715
716static int
717__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
718{
719 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
720 return -EIO;
721
722 qlcnic_set_multi(netdev);
723 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
724
725 adapter->ahw.linkup = 0;
726
727 if (adapter->max_sds_rings > 1)
728 qlcnic_config_rss(adapter, 1);
729
730 qlcnic_config_intr_coalesce(adapter);
731
732 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
733 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
734
735 qlcnic_napi_enable(adapter);
736
737 qlcnic_linkevent_request(adapter, 1);
738
739 set_bit(__QLCNIC_DEV_UP, &adapter->state);
740 return 0;
741}
742
743/* Usage: During resume and firmware recovery module.*/
744
745static int
746qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
747{
748 int err = 0;
749
750 rtnl_lock();
751 if (netif_running(netdev))
752 err = __qlcnic_up(adapter, netdev);
753 rtnl_unlock();
754
755 return err;
756}
757
758static void
759__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
760{
761 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
762 return;
763
764 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
765 return;
766
767 smp_mb();
768 spin_lock(&adapter->tx_clean_lock);
769 netif_carrier_off(netdev);
770 netif_tx_disable(netdev);
771
772 qlcnic_free_mac_list(adapter);
773
774 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
775
776 qlcnic_napi_disable(adapter);
777
778 qlcnic_release_tx_buffers(adapter);
779 spin_unlock(&adapter->tx_clean_lock);
780}
781
782/* Usage: During suspend and firmware recovery module */
783
784static void
785qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
786{
787 rtnl_lock();
788 if (netif_running(netdev))
789 __qlcnic_down(adapter, netdev);
790 rtnl_unlock();
791
792}
793
794static int
795qlcnic_attach(struct qlcnic_adapter *adapter)
796{
797 struct net_device *netdev = adapter->netdev;
798 struct pci_dev *pdev = adapter->pdev;
799 int err, ring;
800 struct qlcnic_host_rds_ring *rds_ring;
801
802 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
803 return 0;
804
805 err = qlcnic_init_firmware(adapter);
806 if (err)
807 return err;
808
809 err = qlcnic_napi_add(adapter, netdev);
810 if (err)
811 return err;
812
813 err = qlcnic_alloc_sw_resources(adapter);
814 if (err) {
815 dev_err(&pdev->dev, "Error in setting sw resources\n");
816 return err;
817 }
818
819 err = qlcnic_alloc_hw_resources(adapter);
820 if (err) {
821 dev_err(&pdev->dev, "Error in setting hw resources\n");
822 goto err_out_free_sw;
823 }
824
825
826 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
827 rds_ring = &adapter->recv_ctx.rds_rings[ring];
828 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
829 }
830
831 err = qlcnic_request_irq(adapter);
832 if (err) {
833 dev_err(&pdev->dev, "failed to setup interrupt\n");
834 goto err_out_free_rxbuf;
835 }
836
837 qlcnic_init_coalesce_defaults(adapter);
838
839 qlcnic_create_sysfs_entries(adapter);
840
841 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
842 return 0;
843
844err_out_free_rxbuf:
845 qlcnic_release_rx_buffers(adapter);
846 qlcnic_free_hw_resources(adapter);
847err_out_free_sw:
848 qlcnic_free_sw_resources(adapter);
849 return err;
850}
851
852static void
853qlcnic_detach(struct qlcnic_adapter *adapter)
854{
855 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
856 return;
857
858 qlcnic_remove_sysfs_entries(adapter);
859
860 qlcnic_free_hw_resources(adapter);
861 qlcnic_release_rx_buffers(adapter);
862 qlcnic_free_irq(adapter);
863 qlcnic_napi_del(adapter);
864 qlcnic_free_sw_resources(adapter);
865
866 adapter->is_up = 0;
867}
868
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000869void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
870{
871 struct qlcnic_adapter *adapter = netdev_priv(netdev);
872 struct qlcnic_host_sds_ring *sds_ring;
873 int ring;
874
Amit Kumar Salechacdaff182010-02-01 05:25:00 +0000875 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
876 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
877 sds_ring = &adapter->recv_ctx.sds_rings[ring];
878 qlcnic_disable_int(sds_ring);
879 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000880 }
881
882 qlcnic_detach(adapter);
883
884 adapter->diag_test = 0;
885 adapter->max_sds_rings = max_sds_rings;
886
887 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000888 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000889
890 if (netif_running(netdev))
891 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000892out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000893 netif_device_attach(netdev);
894}
895
896int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
897{
898 struct qlcnic_adapter *adapter = netdev_priv(netdev);
899 struct qlcnic_host_sds_ring *sds_ring;
900 int ring;
901 int ret;
902
903 netif_device_detach(netdev);
904
905 if (netif_running(netdev))
906 __qlcnic_down(adapter, netdev);
907
908 qlcnic_detach(adapter);
909
910 adapter->max_sds_rings = 1;
911 adapter->diag_test = test;
912
913 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000914 if (ret) {
915 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000916 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000917 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000918
Amit Kumar Salechacdaff182010-02-01 05:25:00 +0000919 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
920 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
921 sds_ring = &adapter->recv_ctx.sds_rings[ring];
922 qlcnic_enable_int(sds_ring);
923 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000924 }
925
926 return 0;
927}
928
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000929int
930qlcnic_reset_context(struct qlcnic_adapter *adapter)
931{
932 int err = 0;
933 struct net_device *netdev = adapter->netdev;
934
935 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
936 return -EBUSY;
937
938 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
939
940 netif_device_detach(netdev);
941
942 if (netif_running(netdev))
943 __qlcnic_down(adapter, netdev);
944
945 qlcnic_detach(adapter);
946
947 if (netif_running(netdev)) {
948 err = qlcnic_attach(adapter);
949 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000950 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000951 }
952
953 netif_device_attach(netdev);
954 }
955
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000956 clear_bit(__QLCNIC_RESETTING, &adapter->state);
957 return err;
958}
959
960static int
961qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -0700962 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000963{
964 int err;
965 struct pci_dev *pdev = adapter->pdev;
966
967 adapter->rx_csum = 1;
968 adapter->mc_enabled = 0;
969 adapter->max_mc_count = 38;
970
971 netdev->netdev_ops = &qlcnic_netdev_ops;
972 netdev->watchdog_timeo = 2*HZ;
973
974 qlcnic_change_mtu(netdev, netdev->mtu);
975
976 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
977
978 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
979 netdev->features |= (NETIF_F_GRO);
980 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
981
982 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
983 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
984
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -0700985 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000986 netdev->features |= NETIF_F_HIGHDMA;
987 netdev->vlan_features |= NETIF_F_HIGHDMA;
988 }
989
990 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
991 netdev->features |= (NETIF_F_HW_VLAN_TX);
992
993 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
994 netdev->features |= NETIF_F_LRO;
995
996 netdev->irq = adapter->msix_entries[0].vector;
997
998 INIT_WORK(&adapter->tx_timeout_task, qlcnic_tx_timeout_task);
999
1000 if (qlcnic_read_mac_addr(adapter))
1001 dev_warn(&pdev->dev, "failed to read mac addr\n");
1002
1003 netif_carrier_off(netdev);
1004 netif_stop_queue(netdev);
1005
1006 err = register_netdev(netdev);
1007 if (err) {
1008 dev_err(&pdev->dev, "failed to register net device\n");
1009 return err;
1010 }
1011
1012 return 0;
1013}
1014
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001015static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1016{
1017 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1018 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1019 *pci_using_dac = 1;
1020 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1021 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1022 *pci_using_dac = 0;
1023 else {
1024 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1025 return -EIO;
1026 }
1027
1028 return 0;
1029}
1030
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001031static int __devinit
1032qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1033{
1034 struct net_device *netdev = NULL;
1035 struct qlcnic_adapter *adapter = NULL;
1036 int err;
1037 int pci_func_id = PCI_FUNC(pdev->devfn);
1038 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001039 uint8_t pci_using_dac;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001040
1041 err = pci_enable_device(pdev);
1042 if (err)
1043 return err;
1044
1045 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1046 err = -ENODEV;
1047 goto err_out_disable_pdev;
1048 }
1049
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001050 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1051 if (err)
1052 goto err_out_disable_pdev;
1053
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001054 err = pci_request_regions(pdev, qlcnic_driver_name);
1055 if (err)
1056 goto err_out_disable_pdev;
1057
1058 pci_set_master(pdev);
1059
1060 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1061 if (!netdev) {
1062 dev_err(&pdev->dev, "failed to allocate net_device\n");
1063 err = -ENOMEM;
1064 goto err_out_free_res;
1065 }
1066
1067 SET_NETDEV_DEV(netdev, &pdev->dev);
1068
1069 adapter = netdev_priv(netdev);
1070 adapter->netdev = netdev;
1071 adapter->pdev = pdev;
1072 adapter->ahw.pci_func = pci_func_id;
1073
1074 revision_id = pdev->revision;
1075 adapter->ahw.revision_id = revision_id;
1076
1077 rwlock_init(&adapter->ahw.crb_lock);
1078 mutex_init(&adapter->ahw.mem_lock);
1079
1080 spin_lock_init(&adapter->tx_clean_lock);
1081 INIT_LIST_HEAD(&adapter->mac_list);
1082
1083 err = qlcnic_setup_pci_map(adapter);
1084 if (err)
1085 goto err_out_free_netdev;
1086
1087 /* This will be reset for mezz cards */
1088 adapter->portnum = pci_func_id;
1089
1090 err = qlcnic_get_board_info(adapter);
1091 if (err) {
1092 dev_err(&pdev->dev, "Error getting board config info.\n");
1093 goto err_out_iounmap;
1094 }
1095
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001096 qlcnic_setup_idc_param(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001097
1098 err = qlcnic_start_firmware(adapter);
1099 if (err)
1100 goto err_out_decr_ref;
1101
1102 /*
1103 * See if the firmware gave us a virtual-physical port mapping.
1104 */
1105 adapter->physical_port = adapter->portnum;
1106
1107 qlcnic_clear_stats(adapter);
1108
1109 qlcnic_setup_intr(adapter);
1110
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001111 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001112 if (err)
1113 goto err_out_disable_msi;
1114
1115 pci_set_drvdata(pdev, adapter);
1116
1117 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1118
1119 switch (adapter->ahw.port_type) {
1120 case QLCNIC_GBE:
1121 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1122 adapter->netdev->name);
1123 break;
1124 case QLCNIC_XGBE:
1125 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1126 adapter->netdev->name);
1127 break;
1128 }
1129
1130 qlcnic_create_diag_entries(adapter);
1131
1132 return 0;
1133
1134err_out_disable_msi:
1135 qlcnic_teardown_intr(adapter);
1136
1137err_out_decr_ref:
1138 qlcnic_clr_all_drv_state(adapter);
1139
1140err_out_iounmap:
1141 qlcnic_cleanup_pci_map(adapter);
1142
1143err_out_free_netdev:
1144 free_netdev(netdev);
1145
1146err_out_free_res:
1147 pci_release_regions(pdev);
1148
1149err_out_disable_pdev:
1150 pci_set_drvdata(pdev, NULL);
1151 pci_disable_device(pdev);
1152 return err;
1153}
1154
1155static void __devexit qlcnic_remove(struct pci_dev *pdev)
1156{
1157 struct qlcnic_adapter *adapter;
1158 struct net_device *netdev;
1159
1160 adapter = pci_get_drvdata(pdev);
1161 if (adapter == NULL)
1162 return;
1163
1164 netdev = adapter->netdev;
1165
1166 qlcnic_cancel_fw_work(adapter);
1167
1168 unregister_netdev(netdev);
1169
1170 cancel_work_sync(&adapter->tx_timeout_task);
1171
1172 qlcnic_detach(adapter);
1173
1174 qlcnic_clr_all_drv_state(adapter);
1175
1176 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1177
1178 qlcnic_teardown_intr(adapter);
1179
1180 qlcnic_remove_diag_entries(adapter);
1181
1182 qlcnic_cleanup_pci_map(adapter);
1183
1184 qlcnic_release_firmware(adapter);
1185
1186 pci_release_regions(pdev);
1187 pci_disable_device(pdev);
1188 pci_set_drvdata(pdev, NULL);
1189
1190 free_netdev(netdev);
1191}
1192static int __qlcnic_shutdown(struct pci_dev *pdev)
1193{
1194 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1195 struct net_device *netdev = adapter->netdev;
1196 int retval;
1197
1198 netif_device_detach(netdev);
1199
1200 qlcnic_cancel_fw_work(adapter);
1201
1202 if (netif_running(netdev))
1203 qlcnic_down(adapter, netdev);
1204
1205 cancel_work_sync(&adapter->tx_timeout_task);
1206
1207 qlcnic_detach(adapter);
1208
1209 qlcnic_clr_all_drv_state(adapter);
1210
1211 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1212
1213 retval = pci_save_state(pdev);
1214 if (retval)
1215 return retval;
1216
1217 if (qlcnic_wol_supported(adapter)) {
1218 pci_enable_wake(pdev, PCI_D3cold, 1);
1219 pci_enable_wake(pdev, PCI_D3hot, 1);
1220 }
1221
1222 return 0;
1223}
1224
1225static void qlcnic_shutdown(struct pci_dev *pdev)
1226{
1227 if (__qlcnic_shutdown(pdev))
1228 return;
1229
1230 pci_disable_device(pdev);
1231}
1232
1233#ifdef CONFIG_PM
1234static int
1235qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1236{
1237 int retval;
1238
1239 retval = __qlcnic_shutdown(pdev);
1240 if (retval)
1241 return retval;
1242
1243 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1244 return 0;
1245}
1246
1247static int
1248qlcnic_resume(struct pci_dev *pdev)
1249{
1250 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1251 struct net_device *netdev = adapter->netdev;
1252 int err;
1253
1254 err = pci_enable_device(pdev);
1255 if (err)
1256 return err;
1257
1258 pci_set_power_state(pdev, PCI_D0);
1259 pci_set_master(pdev);
1260 pci_restore_state(pdev);
1261
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001262 err = qlcnic_start_firmware(adapter);
1263 if (err) {
1264 dev_err(&pdev->dev, "failed to start firmware\n");
1265 return err;
1266 }
1267
1268 if (netif_running(netdev)) {
1269 err = qlcnic_attach(adapter);
1270 if (err)
1271 goto err_out;
1272
1273 err = qlcnic_up(adapter, netdev);
1274 if (err)
1275 goto err_out_detach;
1276
1277
1278 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1279 }
1280
1281 netif_device_attach(netdev);
1282 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1283 return 0;
1284
1285err_out_detach:
1286 qlcnic_detach(adapter);
1287err_out:
1288 qlcnic_clr_all_drv_state(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001289 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001290 return err;
1291}
1292#endif
1293
1294static int qlcnic_open(struct net_device *netdev)
1295{
1296 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1297 int err;
1298
1299 if (adapter->driver_mismatch)
1300 return -EIO;
1301
1302 err = qlcnic_attach(adapter);
1303 if (err)
1304 return err;
1305
1306 err = __qlcnic_up(adapter, netdev);
1307 if (err)
1308 goto err_out;
1309
1310 netif_start_queue(netdev);
1311
1312 return 0;
1313
1314err_out:
1315 qlcnic_detach(adapter);
1316 return err;
1317}
1318
1319/*
1320 * qlcnic_close - Disables a network interface entry point
1321 */
1322static int qlcnic_close(struct net_device *netdev)
1323{
1324 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1325
1326 __qlcnic_down(adapter, netdev);
1327 return 0;
1328}
1329
1330static void
1331qlcnic_tso_check(struct net_device *netdev,
1332 struct qlcnic_host_tx_ring *tx_ring,
1333 struct cmd_desc_type0 *first_desc,
1334 struct sk_buff *skb)
1335{
1336 u8 opcode = TX_ETHER_PKT;
1337 __be16 protocol = skb->protocol;
1338 u16 flags = 0, vid = 0;
1339 u32 producer;
1340 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1341 struct cmd_desc_type0 *hwdesc;
1342 struct vlan_ethhdr *vh;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001343 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001344
1345 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1346
1347 vh = (struct vlan_ethhdr *)skb->data;
1348 protocol = vh->h_vlan_encapsulated_proto;
1349 flags = FLAGS_VLAN_TAGGED;
1350
1351 } else if (vlan_tx_tag_present(skb)) {
1352
1353 flags = FLAGS_VLAN_OOB;
1354 vid = vlan_tx_tag_get(skb);
1355 qlcnic_set_tx_vlan_tci(first_desc, vid);
1356 vlan_oob = 1;
1357 }
1358
1359 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1360 skb_shinfo(skb)->gso_size > 0) {
1361
1362 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1363
1364 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1365 first_desc->total_hdr_length = hdr_len;
1366 if (vlan_oob) {
1367 first_desc->total_hdr_length += VLAN_HLEN;
1368 first_desc->tcp_hdr_offset = VLAN_HLEN;
1369 first_desc->ip_hdr_offset = VLAN_HLEN;
1370 /* Only in case of TSO on vlan device */
1371 flags |= FLAGS_VLAN_TAGGED;
1372 }
1373
1374 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1375 TX_TCP_LSO6 : TX_TCP_LSO;
1376 tso = 1;
1377
1378 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1379 u8 l4proto;
1380
1381 if (protocol == cpu_to_be16(ETH_P_IP)) {
1382 l4proto = ip_hdr(skb)->protocol;
1383
1384 if (l4proto == IPPROTO_TCP)
1385 opcode = TX_TCP_PKT;
1386 else if (l4proto == IPPROTO_UDP)
1387 opcode = TX_UDP_PKT;
1388 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1389 l4proto = ipv6_hdr(skb)->nexthdr;
1390
1391 if (l4proto == IPPROTO_TCP)
1392 opcode = TX_TCPV6_PKT;
1393 else if (l4proto == IPPROTO_UDP)
1394 opcode = TX_UDPV6_PKT;
1395 }
1396 }
1397
1398 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1399 first_desc->ip_hdr_offset += skb_network_offset(skb);
1400 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1401
1402 if (!tso)
1403 return;
1404
1405 /* For LSO, we need to copy the MAC/IP/TCP headers into
1406 * the descriptor ring
1407 */
1408 producer = tx_ring->producer;
1409 copied = 0;
1410 offset = 2;
1411
1412 if (vlan_oob) {
1413 /* Create a TSO vlan header template for firmware */
1414
1415 hwdesc = &tx_ring->desc_head[producer];
1416 tx_ring->cmd_buf_arr[producer].skb = NULL;
1417
1418 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1419 hdr_len + VLAN_HLEN);
1420
1421 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1422 skb_copy_from_linear_data(skb, vh, 12);
1423 vh->h_vlan_proto = htons(ETH_P_8021Q);
1424 vh->h_vlan_TCI = htons(vid);
1425 skb_copy_from_linear_data_offset(skb, 12,
1426 (char *)vh + 16, copy_len - 16);
1427
1428 copied = copy_len - VLAN_HLEN;
1429 offset = 0;
1430
1431 producer = get_next_index(producer, tx_ring->num_desc);
1432 }
1433
1434 while (copied < hdr_len) {
1435
1436 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1437 (hdr_len - copied));
1438
1439 hwdesc = &tx_ring->desc_head[producer];
1440 tx_ring->cmd_buf_arr[producer].skb = NULL;
1441
1442 skb_copy_from_linear_data_offset(skb, copied,
1443 (char *)hwdesc + offset, copy_len);
1444
1445 copied += copy_len;
1446 offset = 0;
1447
1448 producer = get_next_index(producer, tx_ring->num_desc);
1449 }
1450
1451 tx_ring->producer = producer;
1452 barrier();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001453 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001454}
1455
1456static int
1457qlcnic_map_tx_skb(struct pci_dev *pdev,
1458 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1459{
1460 struct qlcnic_skb_frag *nf;
1461 struct skb_frag_struct *frag;
1462 int i, nr_frags;
1463 dma_addr_t map;
1464
1465 nr_frags = skb_shinfo(skb)->nr_frags;
1466 nf = &pbuf->frag_array[0];
1467
1468 map = pci_map_single(pdev, skb->data,
1469 skb_headlen(skb), PCI_DMA_TODEVICE);
1470 if (pci_dma_mapping_error(pdev, map))
1471 goto out_err;
1472
1473 nf->dma = map;
1474 nf->length = skb_headlen(skb);
1475
1476 for (i = 0; i < nr_frags; i++) {
1477 frag = &skb_shinfo(skb)->frags[i];
1478 nf = &pbuf->frag_array[i+1];
1479
1480 map = pci_map_page(pdev, frag->page, frag->page_offset,
1481 frag->size, PCI_DMA_TODEVICE);
1482 if (pci_dma_mapping_error(pdev, map))
1483 goto unwind;
1484
1485 nf->dma = map;
1486 nf->length = frag->size;
1487 }
1488
1489 return 0;
1490
1491unwind:
1492 while (--i >= 0) {
1493 nf = &pbuf->frag_array[i+1];
1494 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1495 }
1496
1497 nf = &pbuf->frag_array[0];
1498 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1499
1500out_err:
1501 return -ENOMEM;
1502}
1503
1504static inline void
1505qlcnic_clear_cmddesc(u64 *desc)
1506{
1507 desc[0] = 0ULL;
1508 desc[2] = 0ULL;
1509}
1510
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001511netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001512qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1513{
1514 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1515 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1516 struct qlcnic_cmd_buffer *pbuf;
1517 struct qlcnic_skb_frag *buffrag;
1518 struct cmd_desc_type0 *hwdesc, *first_desc;
1519 struct pci_dev *pdev;
1520 int i, k;
1521
1522 u32 producer;
1523 int frag_count, no_of_desc;
1524 u32 num_txd = tx_ring->num_desc;
1525
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00001526 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1527 netif_stop_queue(netdev);
1528 return NETDEV_TX_BUSY;
1529 }
1530
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001531 frag_count = skb_shinfo(skb)->nr_frags + 1;
1532
1533 /* 4 fragments per cmd des */
1534 no_of_desc = (frag_count + 3) >> 2;
1535
1536 if (unlikely(no_of_desc + 2 > qlcnic_tx_avail(tx_ring))) {
1537 netif_stop_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001538 adapter->stats.xmit_off++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001539 return NETDEV_TX_BUSY;
1540 }
1541
1542 producer = tx_ring->producer;
1543 pbuf = &tx_ring->cmd_buf_arr[producer];
1544
1545 pdev = adapter->pdev;
1546
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001547 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1548 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001549 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001550 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001551
1552 pbuf->skb = skb;
1553 pbuf->frag_count = frag_count;
1554
1555 first_desc = hwdesc = &tx_ring->desc_head[producer];
1556 qlcnic_clear_cmddesc((u64 *)hwdesc);
1557
1558 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1559 qlcnic_set_tx_port(first_desc, adapter->portnum);
1560
1561 for (i = 0; i < frag_count; i++) {
1562
1563 k = i % 4;
1564
1565 if ((k == 0) && (i > 0)) {
1566 /* move to next desc.*/
1567 producer = get_next_index(producer, num_txd);
1568 hwdesc = &tx_ring->desc_head[producer];
1569 qlcnic_clear_cmddesc((u64 *)hwdesc);
1570 tx_ring->cmd_buf_arr[producer].skb = NULL;
1571 }
1572
1573 buffrag = &pbuf->frag_array[i];
1574
1575 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1576 switch (k) {
1577 case 0:
1578 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1579 break;
1580 case 1:
1581 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1582 break;
1583 case 2:
1584 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1585 break;
1586 case 3:
1587 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1588 break;
1589 }
1590 }
1591
1592 tx_ring->producer = get_next_index(producer, num_txd);
1593
1594 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1595
1596 qlcnic_update_cmd_producer(adapter, tx_ring);
1597
1598 adapter->stats.txbytes += skb->len;
1599 adapter->stats.xmitcalled++;
1600
1601 return NETDEV_TX_OK;
1602
1603drop_packet:
1604 adapter->stats.txdropped++;
1605 dev_kfree_skb_any(skb);
1606 return NETDEV_TX_OK;
1607}
1608
1609static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1610{
1611 struct net_device *netdev = adapter->netdev;
1612 u32 temp, temp_state, temp_val;
1613 int rv = 0;
1614
1615 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1616
1617 temp_state = qlcnic_get_temp_state(temp);
1618 temp_val = qlcnic_get_temp_val(temp);
1619
1620 if (temp_state == QLCNIC_TEMP_PANIC) {
1621 dev_err(&netdev->dev,
1622 "Device temperature %d degrees C exceeds"
1623 " maximum allowed. Hardware has been shut down.\n",
1624 temp_val);
1625 rv = 1;
1626 } else if (temp_state == QLCNIC_TEMP_WARN) {
1627 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1628 dev_err(&netdev->dev,
1629 "Device temperature %d degrees C "
1630 "exceeds operating range."
1631 " Immediate action needed.\n",
1632 temp_val);
1633 }
1634 } else {
1635 if (adapter->temp == QLCNIC_TEMP_WARN) {
1636 dev_info(&netdev->dev,
1637 "Device temperature is now %d degrees C"
1638 " in normal range.\n", temp_val);
1639 }
1640 }
1641 adapter->temp = temp_state;
1642 return rv;
1643}
1644
1645void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1646{
1647 struct net_device *netdev = adapter->netdev;
1648
1649 if (adapter->ahw.linkup && !linkup) {
1650 dev_info(&netdev->dev, "NIC Link is down\n");
1651 adapter->ahw.linkup = 0;
1652 if (netif_running(netdev)) {
1653 netif_carrier_off(netdev);
1654 netif_stop_queue(netdev);
1655 }
1656 } else if (!adapter->ahw.linkup && linkup) {
1657 dev_info(&netdev->dev, "NIC Link is up\n");
1658 adapter->ahw.linkup = 1;
1659 if (netif_running(netdev)) {
1660 netif_carrier_on(netdev);
1661 netif_wake_queue(netdev);
1662 }
1663 }
1664}
1665
1666static void qlcnic_tx_timeout(struct net_device *netdev)
1667{
1668 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1669
1670 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1671 return;
1672
1673 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1674 schedule_work(&adapter->tx_timeout_task);
1675}
1676
1677static void qlcnic_tx_timeout_task(struct work_struct *work)
1678{
1679 struct qlcnic_adapter *adapter =
1680 container_of(work, struct qlcnic_adapter, tx_timeout_task);
1681
1682 if (!netif_running(adapter->netdev))
1683 return;
1684
1685 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1686 return;
1687
1688 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
1689 goto request_reset;
1690
1691 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1692 if (!qlcnic_reset_context(adapter)) {
1693 adapter->netdev->trans_start = jiffies;
1694 return;
1695
1696 /* context reset failed, fall through for fw reset */
1697 }
1698
1699request_reset:
1700 adapter->need_fw_reset = 1;
1701 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00001702 QLCDB(adapter, DRV, "Resetting adapter\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001703}
1704
1705static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1706{
1707 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1708 struct net_device_stats *stats = &netdev->stats;
1709
1710 memset(stats, 0, sizeof(*stats));
1711
1712 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1713 stats->tx_packets = adapter->stats.xmitfinished;
1714 stats->rx_bytes = adapter->stats.rxbytes;
1715 stats->tx_bytes = adapter->stats.txbytes;
1716 stats->rx_dropped = adapter->stats.rxdropped;
1717 stats->tx_dropped = adapter->stats.txdropped;
1718
1719 return stats;
1720}
1721
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001722static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001723{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001724 u32 status;
1725
1726 status = readl(adapter->isr_int_vec);
1727
1728 if (!(status & adapter->int_vec_bit))
1729 return IRQ_NONE;
1730
1731 /* check interrupt state machine, to be sure */
1732 status = readl(adapter->crb_int_state_reg);
1733 if (!ISR_LEGACY_INT_TRIGGERED(status))
1734 return IRQ_NONE;
1735
1736 writel(0xffffffff, adapter->tgt_status_reg);
1737 /* read twice to ensure write is flushed */
1738 readl(adapter->isr_int_vec);
1739 readl(adapter->isr_int_vec);
1740
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001741 return IRQ_HANDLED;
1742}
1743
1744static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
1745{
1746 struct qlcnic_host_sds_ring *sds_ring = data;
1747 struct qlcnic_adapter *adapter = sds_ring->adapter;
1748
1749 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1750 goto done;
1751 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
1752 writel(0xffffffff, adapter->tgt_status_reg);
1753 goto done;
1754 }
1755
1756 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1757 return IRQ_NONE;
1758
1759done:
1760 adapter->diag_cnt++;
1761 qlcnic_enable_int(sds_ring);
1762 return IRQ_HANDLED;
1763}
1764
1765static irqreturn_t qlcnic_intr(int irq, void *data)
1766{
1767 struct qlcnic_host_sds_ring *sds_ring = data;
1768 struct qlcnic_adapter *adapter = sds_ring->adapter;
1769
1770 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1771 return IRQ_NONE;
1772
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001773 napi_schedule(&sds_ring->napi);
1774
1775 return IRQ_HANDLED;
1776}
1777
1778static irqreturn_t qlcnic_msi_intr(int irq, void *data)
1779{
1780 struct qlcnic_host_sds_ring *sds_ring = data;
1781 struct qlcnic_adapter *adapter = sds_ring->adapter;
1782
1783 /* clear interrupt */
1784 writel(0xffffffff, adapter->tgt_status_reg);
1785
1786 napi_schedule(&sds_ring->napi);
1787 return IRQ_HANDLED;
1788}
1789
1790static irqreturn_t qlcnic_msix_intr(int irq, void *data)
1791{
1792 struct qlcnic_host_sds_ring *sds_ring = data;
1793
1794 napi_schedule(&sds_ring->napi);
1795 return IRQ_HANDLED;
1796}
1797
1798static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
1799{
1800 u32 sw_consumer, hw_consumer;
1801 int count = 0, i;
1802 struct qlcnic_cmd_buffer *buffer;
1803 struct pci_dev *pdev = adapter->pdev;
1804 struct net_device *netdev = adapter->netdev;
1805 struct qlcnic_skb_frag *frag;
1806 int done;
1807 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1808
1809 if (!spin_trylock(&adapter->tx_clean_lock))
1810 return 1;
1811
1812 sw_consumer = tx_ring->sw_consumer;
1813 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1814
1815 while (sw_consumer != hw_consumer) {
1816 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1817 if (buffer->skb) {
1818 frag = &buffer->frag_array[0];
1819 pci_unmap_single(pdev, frag->dma, frag->length,
1820 PCI_DMA_TODEVICE);
1821 frag->dma = 0ULL;
1822 for (i = 1; i < buffer->frag_count; i++) {
1823 frag++;
1824 pci_unmap_page(pdev, frag->dma, frag->length,
1825 PCI_DMA_TODEVICE);
1826 frag->dma = 0ULL;
1827 }
1828
1829 adapter->stats.xmitfinished++;
1830 dev_kfree_skb_any(buffer->skb);
1831 buffer->skb = NULL;
1832 }
1833
1834 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
1835 if (++count >= MAX_STATUS_HANDLE)
1836 break;
1837 }
1838
1839 if (count && netif_running(netdev)) {
1840 tx_ring->sw_consumer = sw_consumer;
1841
1842 smp_mb();
1843
1844 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
1845 __netif_tx_lock(tx_ring->txq, smp_processor_id());
1846 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
1847 netif_wake_queue(netdev);
1848 adapter->tx_timeo_cnt = 0;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001849 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001850 }
1851 __netif_tx_unlock(tx_ring->txq);
1852 }
1853 }
1854 /*
1855 * If everything is freed up to consumer then check if the ring is full
1856 * If the ring is full then check if more needs to be freed and
1857 * schedule the call back again.
1858 *
1859 * This happens when there are 2 CPUs. One could be freeing and the
1860 * other filling it. If the ring is full when we get out of here and
1861 * the card has already interrupted the host then the host can miss the
1862 * interrupt.
1863 *
1864 * There is still a possible race condition and the host could miss an
1865 * interrupt. The card has to take care of this.
1866 */
1867 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1868 done = (sw_consumer == hw_consumer);
1869 spin_unlock(&adapter->tx_clean_lock);
1870
1871 return done;
1872}
1873
1874static int qlcnic_poll(struct napi_struct *napi, int budget)
1875{
1876 struct qlcnic_host_sds_ring *sds_ring =
1877 container_of(napi, struct qlcnic_host_sds_ring, napi);
1878
1879 struct qlcnic_adapter *adapter = sds_ring->adapter;
1880
1881 int tx_complete;
1882 int work_done;
1883
1884 tx_complete = qlcnic_process_cmd_ring(adapter);
1885
1886 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
1887
1888 if ((work_done < budget) && tx_complete) {
1889 napi_complete(&sds_ring->napi);
1890 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1891 qlcnic_enable_int(sds_ring);
1892 }
1893
1894 return work_done;
1895}
1896
1897#ifdef CONFIG_NET_POLL_CONTROLLER
1898static void qlcnic_poll_controller(struct net_device *netdev)
1899{
1900 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1901 disable_irq(adapter->irq);
1902 qlcnic_intr(adapter->irq, adapter);
1903 enable_irq(adapter->irq);
1904}
1905#endif
1906
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001907static int
1908qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001909{
1910 u32 val;
1911
1912 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
1913 state != QLCNIC_DEV_NEED_QUISCENT);
1914
1915 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001916 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001917
1918 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1919
1920 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001921 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001922 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001923 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001924
1925 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1926
1927 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001928
1929 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001930}
1931
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00001932static int
1933qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
1934{
1935 u32 val;
1936
1937 if (qlcnic_api_lock(adapter))
1938 return -EBUSY;
1939
1940 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001941 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00001942 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1943
1944 qlcnic_api_unlock(adapter);
1945
1946 return 0;
1947}
1948
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001949static void
1950qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
1951{
1952 u32 val;
1953
1954 if (qlcnic_api_lock(adapter))
1955 goto err;
1956
1957 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001958 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001959 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
1960
1961 if (!(val & 0x11111111))
1962 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
1963
1964 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001965 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001966 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1967
1968 qlcnic_api_unlock(adapter);
1969err:
1970 adapter->fw_fail_cnt = 0;
1971 clear_bit(__QLCNIC_START_FW, &adapter->state);
1972 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1973}
1974
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00001975/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001976static int
1977qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
1978{
1979 int act, state;
1980
1981 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1982 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
1983
1984 if (((state & 0x11111111) == (act & 0x11111111)) ||
1985 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
1986 return 0;
1987 else
1988 return 1;
1989}
1990
1991static int
1992qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
1993{
1994 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001995 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001996 u8 portnum = adapter->portnum;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001997
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00001998 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
1999 return 1;
2000
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002001 if (qlcnic_api_lock(adapter))
2002 return -1;
2003
2004 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002005 if (!(val & (1 << (portnum * 4)))) {
2006 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002007 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002008 }
2009
2010 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002011 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002012
2013 switch (prev_state) {
2014 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002015 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002016 qlcnic_api_unlock(adapter);
2017 return 1;
2018
2019 case QLCNIC_DEV_READY:
2020 qlcnic_api_unlock(adapter);
2021 return 0;
2022
2023 case QLCNIC_DEV_NEED_RESET:
2024 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002025 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002026 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2027 break;
2028
2029 case QLCNIC_DEV_NEED_QUISCENT:
2030 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002031 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002032 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2033 break;
2034
2035 case QLCNIC_DEV_FAILED:
2036 qlcnic_api_unlock(adapter);
2037 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002038
2039 case QLCNIC_DEV_INITIALIZING:
2040 case QLCNIC_DEV_QUISCENT:
2041 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002042 }
2043
2044 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002045
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002046 do {
2047 msleep(1000);
2048 } while ((QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) != QLCNIC_DEV_READY)
2049 && --dev_init_timeo);
2050
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002051 if (!dev_init_timeo) {
2052 dev_err(&adapter->pdev->dev,
2053 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002054 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002055 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002056
2057 if (qlcnic_api_lock(adapter))
2058 return -1;
2059
2060 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002061 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002062 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2063
2064 qlcnic_api_unlock(adapter);
2065
2066 return 0;
2067}
2068
2069static void
2070qlcnic_fwinit_work(struct work_struct *work)
2071{
2072 struct qlcnic_adapter *adapter = container_of(work,
2073 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002074 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002075
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002076 if (qlcnic_api_lock(adapter))
2077 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002078
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002079 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2080 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2081 adapter->reset_ack_timeo);
2082 goto skip_ack_check;
2083 }
2084
2085 if (!qlcnic_check_drv_state(adapter)) {
2086skip_ack_check:
2087 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2088 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2089 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2090 QLCNIC_DEV_INITIALIZING);
2091 set_bit(__QLCNIC_START_FW, &adapter->state);
2092 QLCDB(adapter, DRV, "Restarting fw\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002093 }
2094
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002095 qlcnic_api_unlock(adapter);
2096
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002097 if (!qlcnic_start_firmware(adapter)) {
2098 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2099 return;
2100 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002101 goto err_ret;
2102 }
2103
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002104 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002105
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002106 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002107 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002108
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002109 switch (dev_state) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002110 case QLCNIC_DEV_NEED_RESET:
2111 qlcnic_schedule_work(adapter,
2112 qlcnic_fwinit_work, FW_POLL_DELAY);
2113 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002114 case QLCNIC_DEV_FAILED:
2115 break;
2116
2117 default:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002118 if (!qlcnic_start_firmware(adapter)) {
2119 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2120 return;
2121 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002122 }
2123
2124err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002125 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2126 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002127 netif_device_attach(adapter->netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002128 qlcnic_clr_all_drv_state(adapter);
2129}
2130
2131static void
2132qlcnic_detach_work(struct work_struct *work)
2133{
2134 struct qlcnic_adapter *adapter = container_of(work,
2135 struct qlcnic_adapter, fw_work.work);
2136 struct net_device *netdev = adapter->netdev;
2137 u32 status;
2138
2139 netif_device_detach(netdev);
2140
2141 qlcnic_down(adapter, netdev);
2142
Amit Kumar Salechace668442010-02-01 05:24:57 +00002143 rtnl_lock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002144 qlcnic_detach(adapter);
Amit Kumar Salechace668442010-02-01 05:24:57 +00002145 rtnl_unlock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002146
2147 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2148
2149 if (status & QLCNIC_RCODE_FATAL_ERROR)
2150 goto err_ret;
2151
2152 if (adapter->temp == QLCNIC_TEMP_PANIC)
2153 goto err_ret;
2154
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002155 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2156 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002157
2158 adapter->fw_wait_cnt = 0;
2159
2160 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2161
2162 return;
2163
2164err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002165 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2166 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002167 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002168 qlcnic_clr_all_drv_state(adapter);
2169
2170}
2171
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002172/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002173static void
2174qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2175{
2176 u32 state;
2177
2178 if (qlcnic_api_lock(adapter))
2179 return;
2180
2181 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2182
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002183 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002184 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002185 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002186 }
2187
2188 qlcnic_api_unlock(adapter);
2189}
2190
2191static void
2192qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2193 work_func_t func, int delay)
2194{
2195 INIT_DELAYED_WORK(&adapter->fw_work, func);
2196 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2197}
2198
2199static void
2200qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2201{
2202 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2203 msleep(10);
2204
2205 cancel_delayed_work_sync(&adapter->fw_work);
2206}
2207
2208static void
2209qlcnic_attach_work(struct work_struct *work)
2210{
2211 struct qlcnic_adapter *adapter = container_of(work,
2212 struct qlcnic_adapter, fw_work.work);
2213 struct net_device *netdev = adapter->netdev;
2214 int err;
2215
2216 if (netif_running(netdev)) {
2217 err = qlcnic_attach(adapter);
2218 if (err)
2219 goto done;
2220
2221 err = qlcnic_up(adapter, netdev);
2222 if (err) {
2223 qlcnic_detach(adapter);
2224 goto done;
2225 }
2226
2227 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2228 }
2229
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002230done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002231 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002232 adapter->fw_fail_cnt = 0;
2233 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002234
2235 if (!qlcnic_clr_drv_state(adapter))
2236 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2237 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002238}
2239
2240static int
2241qlcnic_check_health(struct qlcnic_adapter *adapter)
2242{
2243 u32 state = 0, heartbit;
2244 struct net_device *netdev = adapter->netdev;
2245
2246 if (qlcnic_check_temp(adapter))
2247 goto detach;
2248
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002249 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002250 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002251
2252 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2253 if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT)
2254 adapter->need_fw_reset = 1;
2255
2256 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2257 if (heartbit != adapter->heartbit) {
2258 adapter->heartbit = heartbit;
2259 adapter->fw_fail_cnt = 0;
2260 if (adapter->need_fw_reset)
2261 goto detach;
2262 return 0;
2263 }
2264
2265 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2266 return 0;
2267
2268 qlcnic_dev_request_reset(adapter);
2269
2270 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
2271
2272 dev_info(&netdev->dev, "firmware hang detected\n");
2273
2274detach:
2275 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2276 QLCNIC_DEV_NEED_RESET;
2277
2278 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002279 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2280
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002281 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002282 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2283 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002284
2285 return 1;
2286}
2287
2288static void
2289qlcnic_fw_poll_work(struct work_struct *work)
2290{
2291 struct qlcnic_adapter *adapter = container_of(work,
2292 struct qlcnic_adapter, fw_work.work);
2293
2294 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2295 goto reschedule;
2296
2297
2298 if (qlcnic_check_health(adapter))
2299 return;
2300
2301reschedule:
2302 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2303}
2304
2305static ssize_t
2306qlcnic_store_bridged_mode(struct device *dev,
2307 struct device_attribute *attr, const char *buf, size_t len)
2308{
2309 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2310 unsigned long new;
2311 int ret = -EINVAL;
2312
2313 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2314 goto err_out;
2315
2316 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2317 goto err_out;
2318
2319 if (strict_strtoul(buf, 2, &new))
2320 goto err_out;
2321
2322 if (!qlcnic_config_bridged_mode(adapter, !!new))
2323 ret = len;
2324
2325err_out:
2326 return ret;
2327}
2328
2329static ssize_t
2330qlcnic_show_bridged_mode(struct device *dev,
2331 struct device_attribute *attr, char *buf)
2332{
2333 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2334 int bridged_mode = 0;
2335
2336 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2337 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2338
2339 return sprintf(buf, "%d\n", bridged_mode);
2340}
2341
2342static struct device_attribute dev_attr_bridged_mode = {
2343 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2344 .show = qlcnic_show_bridged_mode,
2345 .store = qlcnic_store_bridged_mode,
2346};
2347
2348static ssize_t
2349qlcnic_store_diag_mode(struct device *dev,
2350 struct device_attribute *attr, const char *buf, size_t len)
2351{
2352 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2353 unsigned long new;
2354
2355 if (strict_strtoul(buf, 2, &new))
2356 return -EINVAL;
2357
2358 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2359 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2360
2361 return len;
2362}
2363
2364static ssize_t
2365qlcnic_show_diag_mode(struct device *dev,
2366 struct device_attribute *attr, char *buf)
2367{
2368 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2369
2370 return sprintf(buf, "%d\n",
2371 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2372}
2373
2374static struct device_attribute dev_attr_diag_mode = {
2375 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2376 .show = qlcnic_show_diag_mode,
2377 .store = qlcnic_store_diag_mode,
2378};
2379
2380static int
2381qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2382 loff_t offset, size_t size)
2383{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002384 size_t crb_size = 4;
2385
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002386 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2387 return -EIO;
2388
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002389 if (offset < QLCNIC_PCI_CRBSPACE) {
2390 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2391 QLCNIC_PCI_CAMQM_END))
2392 crb_size = 8;
2393 else
2394 return -EINVAL;
2395 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002396
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002397 if ((size != crb_size) || (offset & (crb_size-1)))
2398 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002399
2400 return 0;
2401}
2402
2403static ssize_t
2404qlcnic_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2405 char *buf, loff_t offset, size_t size)
2406{
2407 struct device *dev = container_of(kobj, struct device, kobj);
2408 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2409 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002410 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002411 int ret;
2412
2413 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2414 if (ret != 0)
2415 return ret;
2416
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002417 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2418 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2419 memcpy(buf, &qmdata, size);
2420 } else {
2421 data = QLCRD32(adapter, offset);
2422 memcpy(buf, &data, size);
2423 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002424 return size;
2425}
2426
2427static ssize_t
2428qlcnic_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2429 char *buf, loff_t offset, size_t size)
2430{
2431 struct device *dev = container_of(kobj, struct device, kobj);
2432 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2433 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002434 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002435 int ret;
2436
2437 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2438 if (ret != 0)
2439 return ret;
2440
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002441 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2442 memcpy(&qmdata, buf, size);
2443 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
2444 } else {
2445 memcpy(&data, buf, size);
2446 QLCWR32(adapter, offset, data);
2447 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002448 return size;
2449}
2450
2451static int
2452qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
2453 loff_t offset, size_t size)
2454{
2455 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2456 return -EIO;
2457
2458 if ((size != 8) || (offset & 0x7))
2459 return -EIO;
2460
2461 return 0;
2462}
2463
2464static ssize_t
2465qlcnic_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2466 char *buf, loff_t offset, size_t size)
2467{
2468 struct device *dev = container_of(kobj, struct device, kobj);
2469 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2470 u64 data;
2471 int ret;
2472
2473 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2474 if (ret != 0)
2475 return ret;
2476
2477 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
2478 return -EIO;
2479
2480 memcpy(buf, &data, size);
2481
2482 return size;
2483}
2484
2485static ssize_t
2486qlcnic_sysfs_write_mem(struct kobject *kobj, struct bin_attribute *attr,
2487 char *buf, loff_t offset, size_t size)
2488{
2489 struct device *dev = container_of(kobj, struct device, kobj);
2490 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2491 u64 data;
2492 int ret;
2493
2494 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2495 if (ret != 0)
2496 return ret;
2497
2498 memcpy(&data, buf, size);
2499
2500 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
2501 return -EIO;
2502
2503 return size;
2504}
2505
2506
2507static struct bin_attribute bin_attr_crb = {
2508 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2509 .size = 0,
2510 .read = qlcnic_sysfs_read_crb,
2511 .write = qlcnic_sysfs_write_crb,
2512};
2513
2514static struct bin_attribute bin_attr_mem = {
2515 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2516 .size = 0,
2517 .read = qlcnic_sysfs_read_mem,
2518 .write = qlcnic_sysfs_write_mem,
2519};
2520
2521static void
2522qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
2523{
2524 struct device *dev = &adapter->pdev->dev;
2525
2526 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2527 if (device_create_file(dev, &dev_attr_bridged_mode))
2528 dev_warn(dev,
2529 "failed to create bridged_mode sysfs entry\n");
2530}
2531
2532static void
2533qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
2534{
2535 struct device *dev = &adapter->pdev->dev;
2536
2537 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2538 device_remove_file(dev, &dev_attr_bridged_mode);
2539}
2540
2541static void
2542qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
2543{
2544 struct device *dev = &adapter->pdev->dev;
2545
2546 if (device_create_file(dev, &dev_attr_diag_mode))
2547 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2548 if (device_create_bin_file(dev, &bin_attr_crb))
2549 dev_info(dev, "failed to create crb sysfs entry\n");
2550 if (device_create_bin_file(dev, &bin_attr_mem))
2551 dev_info(dev, "failed to create mem sysfs entry\n");
2552}
2553
2554
2555static void
2556qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
2557{
2558 struct device *dev = &adapter->pdev->dev;
2559
2560 device_remove_file(dev, &dev_attr_diag_mode);
2561 device_remove_bin_file(dev, &bin_attr_crb);
2562 device_remove_bin_file(dev, &bin_attr_mem);
2563}
2564
2565#ifdef CONFIG_INET
2566
2567#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
2568
2569static int
2570qlcnic_destip_supported(struct qlcnic_adapter *adapter)
2571{
2572 if (adapter->ahw.cut_through)
2573 return 0;
2574
2575 return 1;
2576}
2577
2578static void
2579qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2580{
2581 struct in_device *indev;
2582 struct qlcnic_adapter *adapter = netdev_priv(dev);
2583
2584 if (!qlcnic_destip_supported(adapter))
2585 return;
2586
2587 indev = in_dev_get(dev);
2588 if (!indev)
2589 return;
2590
2591 for_ifa(indev) {
2592 switch (event) {
2593 case NETDEV_UP:
2594 qlcnic_config_ipaddr(adapter,
2595 ifa->ifa_address, QLCNIC_IP_UP);
2596 break;
2597 case NETDEV_DOWN:
2598 qlcnic_config_ipaddr(adapter,
2599 ifa->ifa_address, QLCNIC_IP_DOWN);
2600 break;
2601 default:
2602 break;
2603 }
2604 } endfor_ifa(indev);
2605
2606 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002607}
2608
2609static int qlcnic_netdev_event(struct notifier_block *this,
2610 unsigned long event, void *ptr)
2611{
2612 struct qlcnic_adapter *adapter;
2613 struct net_device *dev = (struct net_device *)ptr;
2614
2615recheck:
2616 if (dev == NULL)
2617 goto done;
2618
2619 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2620 dev = vlan_dev_real_dev(dev);
2621 goto recheck;
2622 }
2623
2624 if (!is_qlcnic_netdev(dev))
2625 goto done;
2626
2627 adapter = netdev_priv(dev);
2628
2629 if (!adapter)
2630 goto done;
2631
2632 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2633 goto done;
2634
2635 qlcnic_config_indev_addr(dev, event);
2636done:
2637 return NOTIFY_DONE;
2638}
2639
2640static int
2641qlcnic_inetaddr_event(struct notifier_block *this,
2642 unsigned long event, void *ptr)
2643{
2644 struct qlcnic_adapter *adapter;
2645 struct net_device *dev;
2646
2647 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2648
2649 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2650
2651recheck:
2652 if (dev == NULL || !netif_running(dev))
2653 goto done;
2654
2655 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2656 dev = vlan_dev_real_dev(dev);
2657 goto recheck;
2658 }
2659
2660 if (!is_qlcnic_netdev(dev))
2661 goto done;
2662
2663 adapter = netdev_priv(dev);
2664
2665 if (!adapter || !qlcnic_destip_supported(adapter))
2666 goto done;
2667
2668 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2669 goto done;
2670
2671 switch (event) {
2672 case NETDEV_UP:
2673 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
2674 break;
2675 case NETDEV_DOWN:
2676 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
2677 break;
2678 default:
2679 break;
2680 }
2681
2682done:
2683 return NOTIFY_DONE;
2684}
2685
2686static struct notifier_block qlcnic_netdev_cb = {
2687 .notifier_call = qlcnic_netdev_event,
2688};
2689
2690static struct notifier_block qlcnic_inetaddr_cb = {
2691 .notifier_call = qlcnic_inetaddr_event,
2692};
2693#else
2694static void
2695qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2696{ }
2697#endif
2698
2699static struct pci_driver qlcnic_driver = {
2700 .name = qlcnic_driver_name,
2701 .id_table = qlcnic_pci_tbl,
2702 .probe = qlcnic_probe,
2703 .remove = __devexit_p(qlcnic_remove),
2704#ifdef CONFIG_PM
2705 .suspend = qlcnic_suspend,
2706 .resume = qlcnic_resume,
2707#endif
2708 .shutdown = qlcnic_shutdown
2709};
2710
2711static int __init qlcnic_init_module(void)
2712{
2713
2714 printk(KERN_INFO "%s\n", qlcnic_driver_string);
2715
2716#ifdef CONFIG_INET
2717 register_netdevice_notifier(&qlcnic_netdev_cb);
2718 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
2719#endif
2720
2721
2722 return pci_register_driver(&qlcnic_driver);
2723}
2724
2725module_init(qlcnic_init_module);
2726
2727static void __exit qlcnic_exit_module(void)
2728{
2729
2730 pci_unregister_driver(&qlcnic_driver);
2731
2732#ifdef CONFIG_INET
2733 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
2734 unregister_netdevice_notifier(&qlcnic_netdev_cb);
2735#endif
2736}
2737
2738module_exit(qlcnic_exit_module);