blob: 50be833c3767d642b6d6746a2dc79071bd075a56 [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
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000546 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
547 fw_major, fw_minor, fw_build);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000548
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000549 adapter->capabilities = QLCRD32(adapter, CRB_FW_CAPABILITIES_1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000550
551 adapter->flags &= ~QLCNIC_LRO_ENABLED;
552
553 if (adapter->ahw.port_type == QLCNIC_XGBE) {
554 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
555 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
556 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
557 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
558 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
559 }
560
561 adapter->msix_supported = !!use_msi_x;
562 adapter->rss_supported = !!use_msi_x;
563
564 adapter->num_txd = MAX_CMD_DESCRIPTORS;
565
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000566 adapter->max_rds_rings = 2;
567}
568
569static int
570qlcnic_start_firmware(struct qlcnic_adapter *adapter)
571{
572 int val, err, first_boot;
573
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000574 err = qlcnic_can_start_firmware(adapter);
575 if (err < 0)
576 return err;
577 else if (!err)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000578 goto wait_init;
579
580 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
581 if (first_boot == 0x55555555)
582 /* This is the first boot after power up */
583 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
584
585 qlcnic_request_firmware(adapter);
586
587 err = qlcnic_need_fw_reset(adapter);
588 if (err < 0)
589 goto err_out;
590 if (err == 0)
591 goto wait_init;
592
593 if (first_boot != 0x55555555) {
594 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
595 qlcnic_pinit_from_rom(adapter);
596 msleep(1);
597 }
598
599 QLCWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
600 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
601 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
602
603 qlcnic_set_port_mode(adapter);
604
605 err = qlcnic_load_firmware(adapter);
606 if (err)
607 goto err_out;
608
609 qlcnic_release_firmware(adapter);
610
611 val = (_QLCNIC_LINUX_MAJOR << 16)
612 | ((_QLCNIC_LINUX_MINOR << 8))
613 | (_QLCNIC_LINUX_SUBVERSION);
614 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
615
616wait_init:
617 /* Handshake with the card before we register the devices. */
618 err = qlcnic_phantom_init(adapter);
619 if (err)
620 goto err_out;
621
622 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
623
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000624 qlcnic_check_options(adapter);
625
626 adapter->need_fw_reset = 0;
627
628 /* fall through and release firmware */
629
630err_out:
631 qlcnic_release_firmware(adapter);
632 return err;
633}
634
635static int
636qlcnic_request_irq(struct qlcnic_adapter *adapter)
637{
638 irq_handler_t handler;
639 struct qlcnic_host_sds_ring *sds_ring;
640 int err, ring;
641
642 unsigned long flags = 0;
643 struct net_device *netdev = adapter->netdev;
644 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
645
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000646 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
647 handler = qlcnic_tmp_intr;
648 if (!QLCNIC_IS_MSI_FAMILY(adapter))
649 flags |= IRQF_SHARED;
650
651 } else {
652 if (adapter->flags & QLCNIC_MSIX_ENABLED)
653 handler = qlcnic_msix_intr;
654 else if (adapter->flags & QLCNIC_MSI_ENABLED)
655 handler = qlcnic_msi_intr;
656 else {
657 flags |= IRQF_SHARED;
658 handler = qlcnic_intr;
659 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000660 }
661 adapter->irq = netdev->irq;
662
663 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
664 sds_ring = &recv_ctx->sds_rings[ring];
665 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
666 err = request_irq(sds_ring->irq, handler,
667 flags, sds_ring->name, sds_ring);
668 if (err)
669 return err;
670 }
671
672 return 0;
673}
674
675static void
676qlcnic_free_irq(struct qlcnic_adapter *adapter)
677{
678 int ring;
679 struct qlcnic_host_sds_ring *sds_ring;
680
681 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
682
683 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
684 sds_ring = &recv_ctx->sds_rings[ring];
685 free_irq(sds_ring->irq, sds_ring);
686 }
687}
688
689static void
690qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
691{
692 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
693 adapter->coal.normal.data.rx_time_us =
694 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
695 adapter->coal.normal.data.rx_packets =
696 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
697 adapter->coal.normal.data.tx_time_us =
698 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
699 adapter->coal.normal.data.tx_packets =
700 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
701}
702
703static int
704__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
705{
706 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
707 return -EIO;
708
709 qlcnic_set_multi(netdev);
710 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
711
712 adapter->ahw.linkup = 0;
713
714 if (adapter->max_sds_rings > 1)
715 qlcnic_config_rss(adapter, 1);
716
717 qlcnic_config_intr_coalesce(adapter);
718
719 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
720 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
721
722 qlcnic_napi_enable(adapter);
723
724 qlcnic_linkevent_request(adapter, 1);
725
726 set_bit(__QLCNIC_DEV_UP, &adapter->state);
727 return 0;
728}
729
730/* Usage: During resume and firmware recovery module.*/
731
732static int
733qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
734{
735 int err = 0;
736
737 rtnl_lock();
738 if (netif_running(netdev))
739 err = __qlcnic_up(adapter, netdev);
740 rtnl_unlock();
741
742 return err;
743}
744
745static void
746__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
747{
748 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
749 return;
750
751 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
752 return;
753
754 smp_mb();
755 spin_lock(&adapter->tx_clean_lock);
756 netif_carrier_off(netdev);
757 netif_tx_disable(netdev);
758
759 qlcnic_free_mac_list(adapter);
760
761 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
762
763 qlcnic_napi_disable(adapter);
764
765 qlcnic_release_tx_buffers(adapter);
766 spin_unlock(&adapter->tx_clean_lock);
767}
768
769/* Usage: During suspend and firmware recovery module */
770
771static void
772qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
773{
774 rtnl_lock();
775 if (netif_running(netdev))
776 __qlcnic_down(adapter, netdev);
777 rtnl_unlock();
778
779}
780
781static int
782qlcnic_attach(struct qlcnic_adapter *adapter)
783{
784 struct net_device *netdev = adapter->netdev;
785 struct pci_dev *pdev = adapter->pdev;
786 int err, ring;
787 struct qlcnic_host_rds_ring *rds_ring;
788
789 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
790 return 0;
791
792 err = qlcnic_init_firmware(adapter);
793 if (err)
794 return err;
795
796 err = qlcnic_napi_add(adapter, netdev);
797 if (err)
798 return err;
799
800 err = qlcnic_alloc_sw_resources(adapter);
801 if (err) {
802 dev_err(&pdev->dev, "Error in setting sw resources\n");
803 return err;
804 }
805
806 err = qlcnic_alloc_hw_resources(adapter);
807 if (err) {
808 dev_err(&pdev->dev, "Error in setting hw resources\n");
809 goto err_out_free_sw;
810 }
811
812
813 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
814 rds_ring = &adapter->recv_ctx.rds_rings[ring];
815 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
816 }
817
818 err = qlcnic_request_irq(adapter);
819 if (err) {
820 dev_err(&pdev->dev, "failed to setup interrupt\n");
821 goto err_out_free_rxbuf;
822 }
823
824 qlcnic_init_coalesce_defaults(adapter);
825
826 qlcnic_create_sysfs_entries(adapter);
827
828 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
829 return 0;
830
831err_out_free_rxbuf:
832 qlcnic_release_rx_buffers(adapter);
833 qlcnic_free_hw_resources(adapter);
834err_out_free_sw:
835 qlcnic_free_sw_resources(adapter);
836 return err;
837}
838
839static void
840qlcnic_detach(struct qlcnic_adapter *adapter)
841{
842 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
843 return;
844
845 qlcnic_remove_sysfs_entries(adapter);
846
847 qlcnic_free_hw_resources(adapter);
848 qlcnic_release_rx_buffers(adapter);
849 qlcnic_free_irq(adapter);
850 qlcnic_napi_del(adapter);
851 qlcnic_free_sw_resources(adapter);
852
853 adapter->is_up = 0;
854}
855
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000856void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
857{
858 struct qlcnic_adapter *adapter = netdev_priv(netdev);
859 struct qlcnic_host_sds_ring *sds_ring;
860 int ring;
861
Amit Kumar Salechacdaff182010-02-01 05:25:00 +0000862 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
863 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
864 sds_ring = &adapter->recv_ctx.sds_rings[ring];
865 qlcnic_disable_int(sds_ring);
866 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000867 }
868
869 qlcnic_detach(adapter);
870
871 adapter->diag_test = 0;
872 adapter->max_sds_rings = max_sds_rings;
873
874 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000875 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000876
877 if (netif_running(netdev))
878 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000879out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000880 netif_device_attach(netdev);
881}
882
883int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
884{
885 struct qlcnic_adapter *adapter = netdev_priv(netdev);
886 struct qlcnic_host_sds_ring *sds_ring;
887 int ring;
888 int ret;
889
890 netif_device_detach(netdev);
891
892 if (netif_running(netdev))
893 __qlcnic_down(adapter, netdev);
894
895 qlcnic_detach(adapter);
896
897 adapter->max_sds_rings = 1;
898 adapter->diag_test = test;
899
900 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000901 if (ret) {
902 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000903 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000904 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000905
Amit Kumar Salechacdaff182010-02-01 05:25:00 +0000906 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
907 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
908 sds_ring = &adapter->recv_ctx.sds_rings[ring];
909 qlcnic_enable_int(sds_ring);
910 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000911 }
912
913 return 0;
914}
915
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000916int
917qlcnic_reset_context(struct qlcnic_adapter *adapter)
918{
919 int err = 0;
920 struct net_device *netdev = adapter->netdev;
921
922 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
923 return -EBUSY;
924
925 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
926
927 netif_device_detach(netdev);
928
929 if (netif_running(netdev))
930 __qlcnic_down(adapter, netdev);
931
932 qlcnic_detach(adapter);
933
934 if (netif_running(netdev)) {
935 err = qlcnic_attach(adapter);
936 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +0000937 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000938 }
939
940 netif_device_attach(netdev);
941 }
942
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000943 clear_bit(__QLCNIC_RESETTING, &adapter->state);
944 return err;
945}
946
947static int
948qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -0700949 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000950{
951 int err;
952 struct pci_dev *pdev = adapter->pdev;
953
954 adapter->rx_csum = 1;
955 adapter->mc_enabled = 0;
956 adapter->max_mc_count = 38;
957
958 netdev->netdev_ops = &qlcnic_netdev_ops;
959 netdev->watchdog_timeo = 2*HZ;
960
961 qlcnic_change_mtu(netdev, netdev->mtu);
962
963 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
964
965 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
966 netdev->features |= (NETIF_F_GRO);
967 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
968
969 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
970 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
971
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -0700972 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000973 netdev->features |= NETIF_F_HIGHDMA;
974 netdev->vlan_features |= NETIF_F_HIGHDMA;
975 }
976
977 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
978 netdev->features |= (NETIF_F_HW_VLAN_TX);
979
980 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
981 netdev->features |= NETIF_F_LRO;
982
983 netdev->irq = adapter->msix_entries[0].vector;
984
985 INIT_WORK(&adapter->tx_timeout_task, qlcnic_tx_timeout_task);
986
987 if (qlcnic_read_mac_addr(adapter))
988 dev_warn(&pdev->dev, "failed to read mac addr\n");
989
990 netif_carrier_off(netdev);
991 netif_stop_queue(netdev);
992
993 err = register_netdev(netdev);
994 if (err) {
995 dev_err(&pdev->dev, "failed to register net device\n");
996 return err;
997 }
998
999 return 0;
1000}
1001
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001002static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1003{
1004 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1005 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1006 *pci_using_dac = 1;
1007 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1008 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1009 *pci_using_dac = 0;
1010 else {
1011 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1012 return -EIO;
1013 }
1014
1015 return 0;
1016}
1017
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001018static int __devinit
1019qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1020{
1021 struct net_device *netdev = NULL;
1022 struct qlcnic_adapter *adapter = NULL;
1023 int err;
1024 int pci_func_id = PCI_FUNC(pdev->devfn);
1025 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001026 uint8_t pci_using_dac;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001027
1028 err = pci_enable_device(pdev);
1029 if (err)
1030 return err;
1031
1032 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1033 err = -ENODEV;
1034 goto err_out_disable_pdev;
1035 }
1036
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001037 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1038 if (err)
1039 goto err_out_disable_pdev;
1040
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001041 err = pci_request_regions(pdev, qlcnic_driver_name);
1042 if (err)
1043 goto err_out_disable_pdev;
1044
1045 pci_set_master(pdev);
1046
1047 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1048 if (!netdev) {
1049 dev_err(&pdev->dev, "failed to allocate net_device\n");
1050 err = -ENOMEM;
1051 goto err_out_free_res;
1052 }
1053
1054 SET_NETDEV_DEV(netdev, &pdev->dev);
1055
1056 adapter = netdev_priv(netdev);
1057 adapter->netdev = netdev;
1058 adapter->pdev = pdev;
1059 adapter->ahw.pci_func = pci_func_id;
1060
1061 revision_id = pdev->revision;
1062 adapter->ahw.revision_id = revision_id;
1063
1064 rwlock_init(&adapter->ahw.crb_lock);
1065 mutex_init(&adapter->ahw.mem_lock);
1066
1067 spin_lock_init(&adapter->tx_clean_lock);
1068 INIT_LIST_HEAD(&adapter->mac_list);
1069
1070 err = qlcnic_setup_pci_map(adapter);
1071 if (err)
1072 goto err_out_free_netdev;
1073
1074 /* This will be reset for mezz cards */
1075 adapter->portnum = pci_func_id;
1076
1077 err = qlcnic_get_board_info(adapter);
1078 if (err) {
1079 dev_err(&pdev->dev, "Error getting board config info.\n");
1080 goto err_out_iounmap;
1081 }
1082
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001083 qlcnic_setup_idc_param(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001084
1085 err = qlcnic_start_firmware(adapter);
1086 if (err)
1087 goto err_out_decr_ref;
1088
1089 /*
1090 * See if the firmware gave us a virtual-physical port mapping.
1091 */
1092 adapter->physical_port = adapter->portnum;
1093
1094 qlcnic_clear_stats(adapter);
1095
1096 qlcnic_setup_intr(adapter);
1097
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001098 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001099 if (err)
1100 goto err_out_disable_msi;
1101
1102 pci_set_drvdata(pdev, adapter);
1103
1104 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1105
1106 switch (adapter->ahw.port_type) {
1107 case QLCNIC_GBE:
1108 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1109 adapter->netdev->name);
1110 break;
1111 case QLCNIC_XGBE:
1112 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1113 adapter->netdev->name);
1114 break;
1115 }
1116
1117 qlcnic_create_diag_entries(adapter);
1118
1119 return 0;
1120
1121err_out_disable_msi:
1122 qlcnic_teardown_intr(adapter);
1123
1124err_out_decr_ref:
1125 qlcnic_clr_all_drv_state(adapter);
1126
1127err_out_iounmap:
1128 qlcnic_cleanup_pci_map(adapter);
1129
1130err_out_free_netdev:
1131 free_netdev(netdev);
1132
1133err_out_free_res:
1134 pci_release_regions(pdev);
1135
1136err_out_disable_pdev:
1137 pci_set_drvdata(pdev, NULL);
1138 pci_disable_device(pdev);
1139 return err;
1140}
1141
1142static void __devexit qlcnic_remove(struct pci_dev *pdev)
1143{
1144 struct qlcnic_adapter *adapter;
1145 struct net_device *netdev;
1146
1147 adapter = pci_get_drvdata(pdev);
1148 if (adapter == NULL)
1149 return;
1150
1151 netdev = adapter->netdev;
1152
1153 qlcnic_cancel_fw_work(adapter);
1154
1155 unregister_netdev(netdev);
1156
1157 cancel_work_sync(&adapter->tx_timeout_task);
1158
1159 qlcnic_detach(adapter);
1160
1161 qlcnic_clr_all_drv_state(adapter);
1162
1163 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1164
1165 qlcnic_teardown_intr(adapter);
1166
1167 qlcnic_remove_diag_entries(adapter);
1168
1169 qlcnic_cleanup_pci_map(adapter);
1170
1171 qlcnic_release_firmware(adapter);
1172
1173 pci_release_regions(pdev);
1174 pci_disable_device(pdev);
1175 pci_set_drvdata(pdev, NULL);
1176
1177 free_netdev(netdev);
1178}
1179static int __qlcnic_shutdown(struct pci_dev *pdev)
1180{
1181 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1182 struct net_device *netdev = adapter->netdev;
1183 int retval;
1184
1185 netif_device_detach(netdev);
1186
1187 qlcnic_cancel_fw_work(adapter);
1188
1189 if (netif_running(netdev))
1190 qlcnic_down(adapter, netdev);
1191
1192 cancel_work_sync(&adapter->tx_timeout_task);
1193
1194 qlcnic_detach(adapter);
1195
1196 qlcnic_clr_all_drv_state(adapter);
1197
1198 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1199
1200 retval = pci_save_state(pdev);
1201 if (retval)
1202 return retval;
1203
1204 if (qlcnic_wol_supported(adapter)) {
1205 pci_enable_wake(pdev, PCI_D3cold, 1);
1206 pci_enable_wake(pdev, PCI_D3hot, 1);
1207 }
1208
1209 return 0;
1210}
1211
1212static void qlcnic_shutdown(struct pci_dev *pdev)
1213{
1214 if (__qlcnic_shutdown(pdev))
1215 return;
1216
1217 pci_disable_device(pdev);
1218}
1219
1220#ifdef CONFIG_PM
1221static int
1222qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1223{
1224 int retval;
1225
1226 retval = __qlcnic_shutdown(pdev);
1227 if (retval)
1228 return retval;
1229
1230 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1231 return 0;
1232}
1233
1234static int
1235qlcnic_resume(struct pci_dev *pdev)
1236{
1237 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1238 struct net_device *netdev = adapter->netdev;
1239 int err;
1240
1241 err = pci_enable_device(pdev);
1242 if (err)
1243 return err;
1244
1245 pci_set_power_state(pdev, PCI_D0);
1246 pci_set_master(pdev);
1247 pci_restore_state(pdev);
1248
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001249 err = qlcnic_start_firmware(adapter);
1250 if (err) {
1251 dev_err(&pdev->dev, "failed to start firmware\n");
1252 return err;
1253 }
1254
1255 if (netif_running(netdev)) {
1256 err = qlcnic_attach(adapter);
1257 if (err)
1258 goto err_out;
1259
1260 err = qlcnic_up(adapter, netdev);
1261 if (err)
1262 goto err_out_detach;
1263
1264
1265 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1266 }
1267
1268 netif_device_attach(netdev);
1269 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1270 return 0;
1271
1272err_out_detach:
1273 qlcnic_detach(adapter);
1274err_out:
1275 qlcnic_clr_all_drv_state(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001276 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001277 return err;
1278}
1279#endif
1280
1281static int qlcnic_open(struct net_device *netdev)
1282{
1283 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1284 int err;
1285
1286 if (adapter->driver_mismatch)
1287 return -EIO;
1288
1289 err = qlcnic_attach(adapter);
1290 if (err)
1291 return err;
1292
1293 err = __qlcnic_up(adapter, netdev);
1294 if (err)
1295 goto err_out;
1296
1297 netif_start_queue(netdev);
1298
1299 return 0;
1300
1301err_out:
1302 qlcnic_detach(adapter);
1303 return err;
1304}
1305
1306/*
1307 * qlcnic_close - Disables a network interface entry point
1308 */
1309static int qlcnic_close(struct net_device *netdev)
1310{
1311 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1312
1313 __qlcnic_down(adapter, netdev);
1314 return 0;
1315}
1316
1317static void
1318qlcnic_tso_check(struct net_device *netdev,
1319 struct qlcnic_host_tx_ring *tx_ring,
1320 struct cmd_desc_type0 *first_desc,
1321 struct sk_buff *skb)
1322{
1323 u8 opcode = TX_ETHER_PKT;
1324 __be16 protocol = skb->protocol;
1325 u16 flags = 0, vid = 0;
1326 u32 producer;
1327 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1328 struct cmd_desc_type0 *hwdesc;
1329 struct vlan_ethhdr *vh;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001330 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001331
1332 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1333
1334 vh = (struct vlan_ethhdr *)skb->data;
1335 protocol = vh->h_vlan_encapsulated_proto;
1336 flags = FLAGS_VLAN_TAGGED;
1337
1338 } else if (vlan_tx_tag_present(skb)) {
1339
1340 flags = FLAGS_VLAN_OOB;
1341 vid = vlan_tx_tag_get(skb);
1342 qlcnic_set_tx_vlan_tci(first_desc, vid);
1343 vlan_oob = 1;
1344 }
1345
1346 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1347 skb_shinfo(skb)->gso_size > 0) {
1348
1349 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1350
1351 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1352 first_desc->total_hdr_length = hdr_len;
1353 if (vlan_oob) {
1354 first_desc->total_hdr_length += VLAN_HLEN;
1355 first_desc->tcp_hdr_offset = VLAN_HLEN;
1356 first_desc->ip_hdr_offset = VLAN_HLEN;
1357 /* Only in case of TSO on vlan device */
1358 flags |= FLAGS_VLAN_TAGGED;
1359 }
1360
1361 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1362 TX_TCP_LSO6 : TX_TCP_LSO;
1363 tso = 1;
1364
1365 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1366 u8 l4proto;
1367
1368 if (protocol == cpu_to_be16(ETH_P_IP)) {
1369 l4proto = ip_hdr(skb)->protocol;
1370
1371 if (l4proto == IPPROTO_TCP)
1372 opcode = TX_TCP_PKT;
1373 else if (l4proto == IPPROTO_UDP)
1374 opcode = TX_UDP_PKT;
1375 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1376 l4proto = ipv6_hdr(skb)->nexthdr;
1377
1378 if (l4proto == IPPROTO_TCP)
1379 opcode = TX_TCPV6_PKT;
1380 else if (l4proto == IPPROTO_UDP)
1381 opcode = TX_UDPV6_PKT;
1382 }
1383 }
1384
1385 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1386 first_desc->ip_hdr_offset += skb_network_offset(skb);
1387 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1388
1389 if (!tso)
1390 return;
1391
1392 /* For LSO, we need to copy the MAC/IP/TCP headers into
1393 * the descriptor ring
1394 */
1395 producer = tx_ring->producer;
1396 copied = 0;
1397 offset = 2;
1398
1399 if (vlan_oob) {
1400 /* Create a TSO vlan header template for firmware */
1401
1402 hwdesc = &tx_ring->desc_head[producer];
1403 tx_ring->cmd_buf_arr[producer].skb = NULL;
1404
1405 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1406 hdr_len + VLAN_HLEN);
1407
1408 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1409 skb_copy_from_linear_data(skb, vh, 12);
1410 vh->h_vlan_proto = htons(ETH_P_8021Q);
1411 vh->h_vlan_TCI = htons(vid);
1412 skb_copy_from_linear_data_offset(skb, 12,
1413 (char *)vh + 16, copy_len - 16);
1414
1415 copied = copy_len - VLAN_HLEN;
1416 offset = 0;
1417
1418 producer = get_next_index(producer, tx_ring->num_desc);
1419 }
1420
1421 while (copied < hdr_len) {
1422
1423 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1424 (hdr_len - copied));
1425
1426 hwdesc = &tx_ring->desc_head[producer];
1427 tx_ring->cmd_buf_arr[producer].skb = NULL;
1428
1429 skb_copy_from_linear_data_offset(skb, copied,
1430 (char *)hwdesc + offset, copy_len);
1431
1432 copied += copy_len;
1433 offset = 0;
1434
1435 producer = get_next_index(producer, tx_ring->num_desc);
1436 }
1437
1438 tx_ring->producer = producer;
1439 barrier();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001440 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001441}
1442
1443static int
1444qlcnic_map_tx_skb(struct pci_dev *pdev,
1445 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1446{
1447 struct qlcnic_skb_frag *nf;
1448 struct skb_frag_struct *frag;
1449 int i, nr_frags;
1450 dma_addr_t map;
1451
1452 nr_frags = skb_shinfo(skb)->nr_frags;
1453 nf = &pbuf->frag_array[0];
1454
1455 map = pci_map_single(pdev, skb->data,
1456 skb_headlen(skb), PCI_DMA_TODEVICE);
1457 if (pci_dma_mapping_error(pdev, map))
1458 goto out_err;
1459
1460 nf->dma = map;
1461 nf->length = skb_headlen(skb);
1462
1463 for (i = 0; i < nr_frags; i++) {
1464 frag = &skb_shinfo(skb)->frags[i];
1465 nf = &pbuf->frag_array[i+1];
1466
1467 map = pci_map_page(pdev, frag->page, frag->page_offset,
1468 frag->size, PCI_DMA_TODEVICE);
1469 if (pci_dma_mapping_error(pdev, map))
1470 goto unwind;
1471
1472 nf->dma = map;
1473 nf->length = frag->size;
1474 }
1475
1476 return 0;
1477
1478unwind:
1479 while (--i >= 0) {
1480 nf = &pbuf->frag_array[i+1];
1481 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1482 }
1483
1484 nf = &pbuf->frag_array[0];
1485 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1486
1487out_err:
1488 return -ENOMEM;
1489}
1490
1491static inline void
1492qlcnic_clear_cmddesc(u64 *desc)
1493{
1494 desc[0] = 0ULL;
1495 desc[2] = 0ULL;
1496}
1497
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001498netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001499qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1500{
1501 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1502 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1503 struct qlcnic_cmd_buffer *pbuf;
1504 struct qlcnic_skb_frag *buffrag;
1505 struct cmd_desc_type0 *hwdesc, *first_desc;
1506 struct pci_dev *pdev;
1507 int i, k;
1508
1509 u32 producer;
1510 int frag_count, no_of_desc;
1511 u32 num_txd = tx_ring->num_desc;
1512
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00001513 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1514 netif_stop_queue(netdev);
1515 return NETDEV_TX_BUSY;
1516 }
1517
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001518 frag_count = skb_shinfo(skb)->nr_frags + 1;
1519
1520 /* 4 fragments per cmd des */
1521 no_of_desc = (frag_count + 3) >> 2;
1522
1523 if (unlikely(no_of_desc + 2 > qlcnic_tx_avail(tx_ring))) {
1524 netif_stop_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001525 adapter->stats.xmit_off++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001526 return NETDEV_TX_BUSY;
1527 }
1528
1529 producer = tx_ring->producer;
1530 pbuf = &tx_ring->cmd_buf_arr[producer];
1531
1532 pdev = adapter->pdev;
1533
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001534 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1535 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001536 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001537 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001538
1539 pbuf->skb = skb;
1540 pbuf->frag_count = frag_count;
1541
1542 first_desc = hwdesc = &tx_ring->desc_head[producer];
1543 qlcnic_clear_cmddesc((u64 *)hwdesc);
1544
1545 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1546 qlcnic_set_tx_port(first_desc, adapter->portnum);
1547
1548 for (i = 0; i < frag_count; i++) {
1549
1550 k = i % 4;
1551
1552 if ((k == 0) && (i > 0)) {
1553 /* move to next desc.*/
1554 producer = get_next_index(producer, num_txd);
1555 hwdesc = &tx_ring->desc_head[producer];
1556 qlcnic_clear_cmddesc((u64 *)hwdesc);
1557 tx_ring->cmd_buf_arr[producer].skb = NULL;
1558 }
1559
1560 buffrag = &pbuf->frag_array[i];
1561
1562 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1563 switch (k) {
1564 case 0:
1565 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1566 break;
1567 case 1:
1568 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1569 break;
1570 case 2:
1571 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1572 break;
1573 case 3:
1574 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1575 break;
1576 }
1577 }
1578
1579 tx_ring->producer = get_next_index(producer, num_txd);
1580
1581 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1582
1583 qlcnic_update_cmd_producer(adapter, tx_ring);
1584
1585 adapter->stats.txbytes += skb->len;
1586 adapter->stats.xmitcalled++;
1587
1588 return NETDEV_TX_OK;
1589
1590drop_packet:
1591 adapter->stats.txdropped++;
1592 dev_kfree_skb_any(skb);
1593 return NETDEV_TX_OK;
1594}
1595
1596static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1597{
1598 struct net_device *netdev = adapter->netdev;
1599 u32 temp, temp_state, temp_val;
1600 int rv = 0;
1601
1602 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1603
1604 temp_state = qlcnic_get_temp_state(temp);
1605 temp_val = qlcnic_get_temp_val(temp);
1606
1607 if (temp_state == QLCNIC_TEMP_PANIC) {
1608 dev_err(&netdev->dev,
1609 "Device temperature %d degrees C exceeds"
1610 " maximum allowed. Hardware has been shut down.\n",
1611 temp_val);
1612 rv = 1;
1613 } else if (temp_state == QLCNIC_TEMP_WARN) {
1614 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1615 dev_err(&netdev->dev,
1616 "Device temperature %d degrees C "
1617 "exceeds operating range."
1618 " Immediate action needed.\n",
1619 temp_val);
1620 }
1621 } else {
1622 if (adapter->temp == QLCNIC_TEMP_WARN) {
1623 dev_info(&netdev->dev,
1624 "Device temperature is now %d degrees C"
1625 " in normal range.\n", temp_val);
1626 }
1627 }
1628 adapter->temp = temp_state;
1629 return rv;
1630}
1631
1632void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1633{
1634 struct net_device *netdev = adapter->netdev;
1635
1636 if (adapter->ahw.linkup && !linkup) {
1637 dev_info(&netdev->dev, "NIC Link is down\n");
1638 adapter->ahw.linkup = 0;
1639 if (netif_running(netdev)) {
1640 netif_carrier_off(netdev);
1641 netif_stop_queue(netdev);
1642 }
1643 } else if (!adapter->ahw.linkup && linkup) {
1644 dev_info(&netdev->dev, "NIC Link is up\n");
1645 adapter->ahw.linkup = 1;
1646 if (netif_running(netdev)) {
1647 netif_carrier_on(netdev);
1648 netif_wake_queue(netdev);
1649 }
1650 }
1651}
1652
1653static void qlcnic_tx_timeout(struct net_device *netdev)
1654{
1655 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1656
1657 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1658 return;
1659
1660 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1661 schedule_work(&adapter->tx_timeout_task);
1662}
1663
1664static void qlcnic_tx_timeout_task(struct work_struct *work)
1665{
1666 struct qlcnic_adapter *adapter =
1667 container_of(work, struct qlcnic_adapter, tx_timeout_task);
1668
1669 if (!netif_running(adapter->netdev))
1670 return;
1671
1672 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1673 return;
1674
1675 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
1676 goto request_reset;
1677
1678 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1679 if (!qlcnic_reset_context(adapter)) {
1680 adapter->netdev->trans_start = jiffies;
1681 return;
1682
1683 /* context reset failed, fall through for fw reset */
1684 }
1685
1686request_reset:
1687 adapter->need_fw_reset = 1;
1688 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00001689 QLCDB(adapter, DRV, "Resetting adapter\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001690}
1691
1692static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1693{
1694 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1695 struct net_device_stats *stats = &netdev->stats;
1696
1697 memset(stats, 0, sizeof(*stats));
1698
1699 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1700 stats->tx_packets = adapter->stats.xmitfinished;
1701 stats->rx_bytes = adapter->stats.rxbytes;
1702 stats->tx_bytes = adapter->stats.txbytes;
1703 stats->rx_dropped = adapter->stats.rxdropped;
1704 stats->tx_dropped = adapter->stats.txdropped;
1705
1706 return stats;
1707}
1708
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001709static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001710{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001711 u32 status;
1712
1713 status = readl(adapter->isr_int_vec);
1714
1715 if (!(status & adapter->int_vec_bit))
1716 return IRQ_NONE;
1717
1718 /* check interrupt state machine, to be sure */
1719 status = readl(adapter->crb_int_state_reg);
1720 if (!ISR_LEGACY_INT_TRIGGERED(status))
1721 return IRQ_NONE;
1722
1723 writel(0xffffffff, adapter->tgt_status_reg);
1724 /* read twice to ensure write is flushed */
1725 readl(adapter->isr_int_vec);
1726 readl(adapter->isr_int_vec);
1727
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001728 return IRQ_HANDLED;
1729}
1730
1731static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
1732{
1733 struct qlcnic_host_sds_ring *sds_ring = data;
1734 struct qlcnic_adapter *adapter = sds_ring->adapter;
1735
1736 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1737 goto done;
1738 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
1739 writel(0xffffffff, adapter->tgt_status_reg);
1740 goto done;
1741 }
1742
1743 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1744 return IRQ_NONE;
1745
1746done:
1747 adapter->diag_cnt++;
1748 qlcnic_enable_int(sds_ring);
1749 return IRQ_HANDLED;
1750}
1751
1752static irqreturn_t qlcnic_intr(int irq, void *data)
1753{
1754 struct qlcnic_host_sds_ring *sds_ring = data;
1755 struct qlcnic_adapter *adapter = sds_ring->adapter;
1756
1757 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1758 return IRQ_NONE;
1759
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001760 napi_schedule(&sds_ring->napi);
1761
1762 return IRQ_HANDLED;
1763}
1764
1765static irqreturn_t qlcnic_msi_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 /* clear interrupt */
1771 writel(0xffffffff, adapter->tgt_status_reg);
1772
1773 napi_schedule(&sds_ring->napi);
1774 return IRQ_HANDLED;
1775}
1776
1777static irqreturn_t qlcnic_msix_intr(int irq, void *data)
1778{
1779 struct qlcnic_host_sds_ring *sds_ring = data;
1780
1781 napi_schedule(&sds_ring->napi);
1782 return IRQ_HANDLED;
1783}
1784
1785static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
1786{
1787 u32 sw_consumer, hw_consumer;
1788 int count = 0, i;
1789 struct qlcnic_cmd_buffer *buffer;
1790 struct pci_dev *pdev = adapter->pdev;
1791 struct net_device *netdev = adapter->netdev;
1792 struct qlcnic_skb_frag *frag;
1793 int done;
1794 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1795
1796 if (!spin_trylock(&adapter->tx_clean_lock))
1797 return 1;
1798
1799 sw_consumer = tx_ring->sw_consumer;
1800 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1801
1802 while (sw_consumer != hw_consumer) {
1803 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1804 if (buffer->skb) {
1805 frag = &buffer->frag_array[0];
1806 pci_unmap_single(pdev, frag->dma, frag->length,
1807 PCI_DMA_TODEVICE);
1808 frag->dma = 0ULL;
1809 for (i = 1; i < buffer->frag_count; i++) {
1810 frag++;
1811 pci_unmap_page(pdev, frag->dma, frag->length,
1812 PCI_DMA_TODEVICE);
1813 frag->dma = 0ULL;
1814 }
1815
1816 adapter->stats.xmitfinished++;
1817 dev_kfree_skb_any(buffer->skb);
1818 buffer->skb = NULL;
1819 }
1820
1821 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
1822 if (++count >= MAX_STATUS_HANDLE)
1823 break;
1824 }
1825
1826 if (count && netif_running(netdev)) {
1827 tx_ring->sw_consumer = sw_consumer;
1828
1829 smp_mb();
1830
1831 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
1832 __netif_tx_lock(tx_ring->txq, smp_processor_id());
1833 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
1834 netif_wake_queue(netdev);
1835 adapter->tx_timeo_cnt = 0;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001836 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001837 }
1838 __netif_tx_unlock(tx_ring->txq);
1839 }
1840 }
1841 /*
1842 * If everything is freed up to consumer then check if the ring is full
1843 * If the ring is full then check if more needs to be freed and
1844 * schedule the call back again.
1845 *
1846 * This happens when there are 2 CPUs. One could be freeing and the
1847 * other filling it. If the ring is full when we get out of here and
1848 * the card has already interrupted the host then the host can miss the
1849 * interrupt.
1850 *
1851 * There is still a possible race condition and the host could miss an
1852 * interrupt. The card has to take care of this.
1853 */
1854 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1855 done = (sw_consumer == hw_consumer);
1856 spin_unlock(&adapter->tx_clean_lock);
1857
1858 return done;
1859}
1860
1861static int qlcnic_poll(struct napi_struct *napi, int budget)
1862{
1863 struct qlcnic_host_sds_ring *sds_ring =
1864 container_of(napi, struct qlcnic_host_sds_ring, napi);
1865
1866 struct qlcnic_adapter *adapter = sds_ring->adapter;
1867
1868 int tx_complete;
1869 int work_done;
1870
1871 tx_complete = qlcnic_process_cmd_ring(adapter);
1872
1873 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
1874
1875 if ((work_done < budget) && tx_complete) {
1876 napi_complete(&sds_ring->napi);
1877 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1878 qlcnic_enable_int(sds_ring);
1879 }
1880
1881 return work_done;
1882}
1883
1884#ifdef CONFIG_NET_POLL_CONTROLLER
1885static void qlcnic_poll_controller(struct net_device *netdev)
1886{
1887 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1888 disable_irq(adapter->irq);
1889 qlcnic_intr(adapter->irq, adapter);
1890 enable_irq(adapter->irq);
1891}
1892#endif
1893
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001894static int
1895qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001896{
1897 u32 val;
1898
1899 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
1900 state != QLCNIC_DEV_NEED_QUISCENT);
1901
1902 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001903 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001904
1905 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1906
1907 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001908 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001909 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001910 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001911
1912 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1913
1914 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00001915
1916 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001917}
1918
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00001919static int
1920qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
1921{
1922 u32 val;
1923
1924 if (qlcnic_api_lock(adapter))
1925 return -EBUSY;
1926
1927 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001928 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00001929 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1930
1931 qlcnic_api_unlock(adapter);
1932
1933 return 0;
1934}
1935
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001936static void
1937qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
1938{
1939 u32 val;
1940
1941 if (qlcnic_api_lock(adapter))
1942 goto err;
1943
1944 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001945 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001946 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
1947
1948 if (!(val & 0x11111111))
1949 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
1950
1951 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001952 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001953 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1954
1955 qlcnic_api_unlock(adapter);
1956err:
1957 adapter->fw_fail_cnt = 0;
1958 clear_bit(__QLCNIC_START_FW, &adapter->state);
1959 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1960}
1961
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00001962/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001963static int
1964qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
1965{
1966 int act, state;
1967
1968 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1969 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
1970
1971 if (((state & 0x11111111) == (act & 0x11111111)) ||
1972 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
1973 return 0;
1974 else
1975 return 1;
1976}
1977
1978static int
1979qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
1980{
1981 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00001982 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001983 u8 portnum = adapter->portnum;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001984
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00001985 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
1986 return 1;
1987
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001988 if (qlcnic_api_lock(adapter))
1989 return -1;
1990
1991 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00001992 if (!(val & (1 << (portnum * 4)))) {
1993 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001994 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001995 }
1996
1997 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00001998 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001999
2000 switch (prev_state) {
2001 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002002 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002003 qlcnic_api_unlock(adapter);
2004 return 1;
2005
2006 case QLCNIC_DEV_READY:
2007 qlcnic_api_unlock(adapter);
2008 return 0;
2009
2010 case QLCNIC_DEV_NEED_RESET:
2011 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002012 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002013 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2014 break;
2015
2016 case QLCNIC_DEV_NEED_QUISCENT:
2017 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002018 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002019 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2020 break;
2021
2022 case QLCNIC_DEV_FAILED:
2023 qlcnic_api_unlock(adapter);
2024 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002025
2026 case QLCNIC_DEV_INITIALIZING:
2027 case QLCNIC_DEV_QUISCENT:
2028 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002029 }
2030
2031 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002032
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002033 do {
2034 msleep(1000);
2035 } while ((QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) != QLCNIC_DEV_READY)
2036 && --dev_init_timeo);
2037
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002038 if (!dev_init_timeo) {
2039 dev_err(&adapter->pdev->dev,
2040 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002041 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002042 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002043
2044 if (qlcnic_api_lock(adapter))
2045 return -1;
2046
2047 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002048 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002049 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2050
2051 qlcnic_api_unlock(adapter);
2052
2053 return 0;
2054}
2055
2056static void
2057qlcnic_fwinit_work(struct work_struct *work)
2058{
2059 struct qlcnic_adapter *adapter = container_of(work,
2060 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002061 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002062
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002063 if (qlcnic_api_lock(adapter))
2064 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002065
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002066 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2067 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2068 adapter->reset_ack_timeo);
2069 goto skip_ack_check;
2070 }
2071
2072 if (!qlcnic_check_drv_state(adapter)) {
2073skip_ack_check:
2074 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2075 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2076 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2077 QLCNIC_DEV_INITIALIZING);
2078 set_bit(__QLCNIC_START_FW, &adapter->state);
2079 QLCDB(adapter, DRV, "Restarting fw\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002080 }
2081
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002082 qlcnic_api_unlock(adapter);
2083
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002084 if (!qlcnic_start_firmware(adapter)) {
2085 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2086 return;
2087 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002088 goto err_ret;
2089 }
2090
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002091 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002092
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002093 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002094 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002095
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002096 switch (dev_state) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002097 case QLCNIC_DEV_NEED_RESET:
2098 qlcnic_schedule_work(adapter,
2099 qlcnic_fwinit_work, FW_POLL_DELAY);
2100 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002101 case QLCNIC_DEV_FAILED:
2102 break;
2103
2104 default:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002105 if (!qlcnic_start_firmware(adapter)) {
2106 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2107 return;
2108 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002109 }
2110
2111err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002112 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2113 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002114 netif_device_attach(adapter->netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002115 qlcnic_clr_all_drv_state(adapter);
2116}
2117
2118static void
2119qlcnic_detach_work(struct work_struct *work)
2120{
2121 struct qlcnic_adapter *adapter = container_of(work,
2122 struct qlcnic_adapter, fw_work.work);
2123 struct net_device *netdev = adapter->netdev;
2124 u32 status;
2125
2126 netif_device_detach(netdev);
2127
2128 qlcnic_down(adapter, netdev);
2129
Amit Kumar Salechace668442010-02-01 05:24:57 +00002130 rtnl_lock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002131 qlcnic_detach(adapter);
Amit Kumar Salechace668442010-02-01 05:24:57 +00002132 rtnl_unlock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002133
2134 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2135
2136 if (status & QLCNIC_RCODE_FATAL_ERROR)
2137 goto err_ret;
2138
2139 if (adapter->temp == QLCNIC_TEMP_PANIC)
2140 goto err_ret;
2141
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002142 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2143 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002144
2145 adapter->fw_wait_cnt = 0;
2146
2147 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2148
2149 return;
2150
2151err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002152 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2153 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002154 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002155 qlcnic_clr_all_drv_state(adapter);
2156
2157}
2158
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002159/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002160static void
2161qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2162{
2163 u32 state;
2164
2165 if (qlcnic_api_lock(adapter))
2166 return;
2167
2168 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2169
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002170 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002171 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002172 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002173 }
2174
2175 qlcnic_api_unlock(adapter);
2176}
2177
2178static void
2179qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2180 work_func_t func, int delay)
2181{
2182 INIT_DELAYED_WORK(&adapter->fw_work, func);
2183 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2184}
2185
2186static void
2187qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2188{
2189 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2190 msleep(10);
2191
2192 cancel_delayed_work_sync(&adapter->fw_work);
2193}
2194
2195static void
2196qlcnic_attach_work(struct work_struct *work)
2197{
2198 struct qlcnic_adapter *adapter = container_of(work,
2199 struct qlcnic_adapter, fw_work.work);
2200 struct net_device *netdev = adapter->netdev;
2201 int err;
2202
2203 if (netif_running(netdev)) {
2204 err = qlcnic_attach(adapter);
2205 if (err)
2206 goto done;
2207
2208 err = qlcnic_up(adapter, netdev);
2209 if (err) {
2210 qlcnic_detach(adapter);
2211 goto done;
2212 }
2213
2214 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2215 }
2216
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002217done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002218 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002219 adapter->fw_fail_cnt = 0;
2220 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002221
2222 if (!qlcnic_clr_drv_state(adapter))
2223 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2224 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002225}
2226
2227static int
2228qlcnic_check_health(struct qlcnic_adapter *adapter)
2229{
2230 u32 state = 0, heartbit;
2231 struct net_device *netdev = adapter->netdev;
2232
2233 if (qlcnic_check_temp(adapter))
2234 goto detach;
2235
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002236 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002237 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002238
2239 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2240 if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT)
2241 adapter->need_fw_reset = 1;
2242
2243 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2244 if (heartbit != adapter->heartbit) {
2245 adapter->heartbit = heartbit;
2246 adapter->fw_fail_cnt = 0;
2247 if (adapter->need_fw_reset)
2248 goto detach;
2249 return 0;
2250 }
2251
2252 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2253 return 0;
2254
2255 qlcnic_dev_request_reset(adapter);
2256
2257 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
2258
2259 dev_info(&netdev->dev, "firmware hang detected\n");
2260
2261detach:
2262 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2263 QLCNIC_DEV_NEED_RESET;
2264
2265 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002266 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2267
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002268 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002269 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2270 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002271
2272 return 1;
2273}
2274
2275static void
2276qlcnic_fw_poll_work(struct work_struct *work)
2277{
2278 struct qlcnic_adapter *adapter = container_of(work,
2279 struct qlcnic_adapter, fw_work.work);
2280
2281 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2282 goto reschedule;
2283
2284
2285 if (qlcnic_check_health(adapter))
2286 return;
2287
2288reschedule:
2289 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2290}
2291
2292static ssize_t
2293qlcnic_store_bridged_mode(struct device *dev,
2294 struct device_attribute *attr, const char *buf, size_t len)
2295{
2296 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2297 unsigned long new;
2298 int ret = -EINVAL;
2299
2300 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2301 goto err_out;
2302
2303 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2304 goto err_out;
2305
2306 if (strict_strtoul(buf, 2, &new))
2307 goto err_out;
2308
2309 if (!qlcnic_config_bridged_mode(adapter, !!new))
2310 ret = len;
2311
2312err_out:
2313 return ret;
2314}
2315
2316static ssize_t
2317qlcnic_show_bridged_mode(struct device *dev,
2318 struct device_attribute *attr, char *buf)
2319{
2320 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2321 int bridged_mode = 0;
2322
2323 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2324 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2325
2326 return sprintf(buf, "%d\n", bridged_mode);
2327}
2328
2329static struct device_attribute dev_attr_bridged_mode = {
2330 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2331 .show = qlcnic_show_bridged_mode,
2332 .store = qlcnic_store_bridged_mode,
2333};
2334
2335static ssize_t
2336qlcnic_store_diag_mode(struct device *dev,
2337 struct device_attribute *attr, const char *buf, size_t len)
2338{
2339 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2340 unsigned long new;
2341
2342 if (strict_strtoul(buf, 2, &new))
2343 return -EINVAL;
2344
2345 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2346 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2347
2348 return len;
2349}
2350
2351static ssize_t
2352qlcnic_show_diag_mode(struct device *dev,
2353 struct device_attribute *attr, char *buf)
2354{
2355 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2356
2357 return sprintf(buf, "%d\n",
2358 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2359}
2360
2361static struct device_attribute dev_attr_diag_mode = {
2362 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2363 .show = qlcnic_show_diag_mode,
2364 .store = qlcnic_store_diag_mode,
2365};
2366
2367static int
2368qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2369 loff_t offset, size_t size)
2370{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002371 size_t crb_size = 4;
2372
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002373 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2374 return -EIO;
2375
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002376 if (offset < QLCNIC_PCI_CRBSPACE) {
2377 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2378 QLCNIC_PCI_CAMQM_END))
2379 crb_size = 8;
2380 else
2381 return -EINVAL;
2382 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002383
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002384 if ((size != crb_size) || (offset & (crb_size-1)))
2385 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002386
2387 return 0;
2388}
2389
2390static ssize_t
2391qlcnic_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2392 char *buf, loff_t offset, size_t size)
2393{
2394 struct device *dev = container_of(kobj, struct device, kobj);
2395 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2396 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002397 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002398 int ret;
2399
2400 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2401 if (ret != 0)
2402 return ret;
2403
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002404 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2405 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2406 memcpy(buf, &qmdata, size);
2407 } else {
2408 data = QLCRD32(adapter, offset);
2409 memcpy(buf, &data, size);
2410 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002411 return size;
2412}
2413
2414static ssize_t
2415qlcnic_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2416 char *buf, loff_t offset, size_t size)
2417{
2418 struct device *dev = container_of(kobj, struct device, kobj);
2419 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2420 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002421 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002422 int ret;
2423
2424 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2425 if (ret != 0)
2426 return ret;
2427
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002428 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2429 memcpy(&qmdata, buf, size);
2430 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
2431 } else {
2432 memcpy(&data, buf, size);
2433 QLCWR32(adapter, offset, data);
2434 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002435 return size;
2436}
2437
2438static int
2439qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
2440 loff_t offset, size_t size)
2441{
2442 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2443 return -EIO;
2444
2445 if ((size != 8) || (offset & 0x7))
2446 return -EIO;
2447
2448 return 0;
2449}
2450
2451static ssize_t
2452qlcnic_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2453 char *buf, loff_t offset, size_t size)
2454{
2455 struct device *dev = container_of(kobj, struct device, kobj);
2456 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2457 u64 data;
2458 int ret;
2459
2460 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2461 if (ret != 0)
2462 return ret;
2463
2464 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
2465 return -EIO;
2466
2467 memcpy(buf, &data, size);
2468
2469 return size;
2470}
2471
2472static ssize_t
2473qlcnic_sysfs_write_mem(struct kobject *kobj, struct bin_attribute *attr,
2474 char *buf, loff_t offset, size_t size)
2475{
2476 struct device *dev = container_of(kobj, struct device, kobj);
2477 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2478 u64 data;
2479 int ret;
2480
2481 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2482 if (ret != 0)
2483 return ret;
2484
2485 memcpy(&data, buf, size);
2486
2487 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
2488 return -EIO;
2489
2490 return size;
2491}
2492
2493
2494static struct bin_attribute bin_attr_crb = {
2495 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2496 .size = 0,
2497 .read = qlcnic_sysfs_read_crb,
2498 .write = qlcnic_sysfs_write_crb,
2499};
2500
2501static struct bin_attribute bin_attr_mem = {
2502 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2503 .size = 0,
2504 .read = qlcnic_sysfs_read_mem,
2505 .write = qlcnic_sysfs_write_mem,
2506};
2507
2508static void
2509qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
2510{
2511 struct device *dev = &adapter->pdev->dev;
2512
2513 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2514 if (device_create_file(dev, &dev_attr_bridged_mode))
2515 dev_warn(dev,
2516 "failed to create bridged_mode sysfs entry\n");
2517}
2518
2519static void
2520qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
2521{
2522 struct device *dev = &adapter->pdev->dev;
2523
2524 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2525 device_remove_file(dev, &dev_attr_bridged_mode);
2526}
2527
2528static void
2529qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
2530{
2531 struct device *dev = &adapter->pdev->dev;
2532
2533 if (device_create_file(dev, &dev_attr_diag_mode))
2534 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2535 if (device_create_bin_file(dev, &bin_attr_crb))
2536 dev_info(dev, "failed to create crb sysfs entry\n");
2537 if (device_create_bin_file(dev, &bin_attr_mem))
2538 dev_info(dev, "failed to create mem sysfs entry\n");
2539}
2540
2541
2542static void
2543qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
2544{
2545 struct device *dev = &adapter->pdev->dev;
2546
2547 device_remove_file(dev, &dev_attr_diag_mode);
2548 device_remove_bin_file(dev, &bin_attr_crb);
2549 device_remove_bin_file(dev, &bin_attr_mem);
2550}
2551
2552#ifdef CONFIG_INET
2553
2554#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
2555
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002556static void
2557qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2558{
2559 struct in_device *indev;
2560 struct qlcnic_adapter *adapter = netdev_priv(dev);
2561
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002562 indev = in_dev_get(dev);
2563 if (!indev)
2564 return;
2565
2566 for_ifa(indev) {
2567 switch (event) {
2568 case NETDEV_UP:
2569 qlcnic_config_ipaddr(adapter,
2570 ifa->ifa_address, QLCNIC_IP_UP);
2571 break;
2572 case NETDEV_DOWN:
2573 qlcnic_config_ipaddr(adapter,
2574 ifa->ifa_address, QLCNIC_IP_DOWN);
2575 break;
2576 default:
2577 break;
2578 }
2579 } endfor_ifa(indev);
2580
2581 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002582}
2583
2584static int qlcnic_netdev_event(struct notifier_block *this,
2585 unsigned long event, void *ptr)
2586{
2587 struct qlcnic_adapter *adapter;
2588 struct net_device *dev = (struct net_device *)ptr;
2589
2590recheck:
2591 if (dev == NULL)
2592 goto done;
2593
2594 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2595 dev = vlan_dev_real_dev(dev);
2596 goto recheck;
2597 }
2598
2599 if (!is_qlcnic_netdev(dev))
2600 goto done;
2601
2602 adapter = netdev_priv(dev);
2603
2604 if (!adapter)
2605 goto done;
2606
2607 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2608 goto done;
2609
2610 qlcnic_config_indev_addr(dev, event);
2611done:
2612 return NOTIFY_DONE;
2613}
2614
2615static int
2616qlcnic_inetaddr_event(struct notifier_block *this,
2617 unsigned long event, void *ptr)
2618{
2619 struct qlcnic_adapter *adapter;
2620 struct net_device *dev;
2621
2622 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2623
2624 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2625
2626recheck:
2627 if (dev == NULL || !netif_running(dev))
2628 goto done;
2629
2630 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2631 dev = vlan_dev_real_dev(dev);
2632 goto recheck;
2633 }
2634
2635 if (!is_qlcnic_netdev(dev))
2636 goto done;
2637
2638 adapter = netdev_priv(dev);
2639
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00002640 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002641 goto done;
2642
2643 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2644 goto done;
2645
2646 switch (event) {
2647 case NETDEV_UP:
2648 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
2649 break;
2650 case NETDEV_DOWN:
2651 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
2652 break;
2653 default:
2654 break;
2655 }
2656
2657done:
2658 return NOTIFY_DONE;
2659}
2660
2661static struct notifier_block qlcnic_netdev_cb = {
2662 .notifier_call = qlcnic_netdev_event,
2663};
2664
2665static struct notifier_block qlcnic_inetaddr_cb = {
2666 .notifier_call = qlcnic_inetaddr_event,
2667};
2668#else
2669static void
2670qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2671{ }
2672#endif
2673
2674static struct pci_driver qlcnic_driver = {
2675 .name = qlcnic_driver_name,
2676 .id_table = qlcnic_pci_tbl,
2677 .probe = qlcnic_probe,
2678 .remove = __devexit_p(qlcnic_remove),
2679#ifdef CONFIG_PM
2680 .suspend = qlcnic_suspend,
2681 .resume = qlcnic_resume,
2682#endif
2683 .shutdown = qlcnic_shutdown
2684};
2685
2686static int __init qlcnic_init_module(void)
2687{
2688
2689 printk(KERN_INFO "%s\n", qlcnic_driver_string);
2690
2691#ifdef CONFIG_INET
2692 register_netdevice_notifier(&qlcnic_netdev_cb);
2693 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
2694#endif
2695
2696
2697 return pci_register_driver(&qlcnic_driver);
2698}
2699
2700module_init(qlcnic_init_module);
2701
2702static void __exit qlcnic_exit_module(void)
2703{
2704
2705 pci_unregister_driver(&qlcnic_driver);
2706
2707#ifdef CONFIG_INET
2708 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
2709 unregister_netdevice_notifier(&qlcnic_netdev_cb);
2710#endif
2711}
2712
2713module_exit(qlcnic_exit_module);