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