drivers/net/: use DEFINE_PCI_DEVICE_TABLE()
[deliverable/linux.git] / drivers / net / netxen / netxen_nic_main.c
1 /*
2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.
23 *
24 */
25
26 #include <linux/vmalloc.h>
27 #include <linux/interrupt.h>
28 #include "netxen_nic_hw.h"
29
30 #include "netxen_nic.h"
31
32 #include <linux/dma-mapping.h>
33 #include <linux/if_vlan.h>
34 #include <net/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/inetdevice.h>
37 #include <linux/sysfs.h>
38 #include <linux/aer.h>
39
40 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Converged Ethernet Driver");
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
43 MODULE_FIRMWARE(NX_P2_MN_ROMIMAGE_NAME);
44 MODULE_FIRMWARE(NX_P3_CT_ROMIMAGE_NAME);
45 MODULE_FIRMWARE(NX_P3_MN_ROMIMAGE_NAME);
46 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
47
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
50 NETXEN_NIC_LINUX_VERSIONID;
51
52 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
53
54 /* Default to restricted 1G auto-neg mode */
55 static int wol_port_mode = 5;
56
57 static int use_msi = 1;
58
59 static int use_msi_x = 1;
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 __devinit netxen_nic_probe(struct pci_dev *pdev,
66 const struct pci_device_id *ent);
67 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
68 static int netxen_nic_open(struct net_device *netdev);
69 static int netxen_nic_close(struct net_device *netdev);
70 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
71 struct net_device *);
72 static void netxen_tx_timeout(struct net_device *netdev);
73 static void netxen_tx_timeout_task(struct work_struct *work);
74 static void netxen_fw_poll_work(struct work_struct *work);
75 static void netxen_schedule_work(struct netxen_adapter *adapter,
76 work_func_t func, int delay);
77 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
78 static int netxen_nic_poll(struct napi_struct *napi, int budget);
79 #ifdef CONFIG_NET_POLL_CONTROLLER
80 static void netxen_nic_poll_controller(struct net_device *netdev);
81 #endif
82
83 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
84 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
85 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
86 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
87
88 static int nx_dev_request_aer(struct netxen_adapter *adapter);
89 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
90 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
91
92 static irqreturn_t netxen_intr(int irq, void *data);
93 static irqreturn_t netxen_msi_intr(int irq, void *data);
94 static irqreturn_t netxen_msix_intr(int irq, void *data);
95
96 static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
97
98 /* PCI Device ID Table */
99 #define ENTRY(device) \
100 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
101 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
102
103 static DEFINE_PCI_DEVICE_TABLE(netxen_pci_tbl) = {
104 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
105 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
106 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
107 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
108 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
109 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
110 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
111 ENTRY(PCI_DEVICE_ID_NX3031),
112 {0,}
113 };
114
115 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
116
117 static uint32_t crb_cmd_producer[4] = {
118 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
119 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
120 };
121
122 void
123 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
124 struct nx_host_tx_ring *tx_ring)
125 {
126 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
127
128 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
129 netif_stop_queue(adapter->netdev);
130 smp_mb();
131 }
132 }
133
134 static uint32_t crb_cmd_consumer[4] = {
135 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
136 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
137 };
138
139 static inline void
140 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
141 struct nx_host_tx_ring *tx_ring)
142 {
143 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
144 }
145
146 static uint32_t msi_tgt_status[8] = {
147 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
148 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
149 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
150 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
151 };
152
153 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
154
155 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
156 {
157 struct netxen_adapter *adapter = sds_ring->adapter;
158
159 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
160 }
161
162 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
163 {
164 struct netxen_adapter *adapter = sds_ring->adapter;
165
166 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
167
168 if (!NETXEN_IS_MSI_FAMILY(adapter))
169 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
170 }
171
172 static int
173 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
174 {
175 int size = sizeof(struct nx_host_sds_ring) * count;
176
177 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
178
179 return (recv_ctx->sds_rings == NULL);
180 }
181
182 static void
183 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
184 {
185 if (recv_ctx->sds_rings != NULL)
186 kfree(recv_ctx->sds_rings);
187
188 recv_ctx->sds_rings = NULL;
189 }
190
191 static int
192 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
193 {
194 int ring;
195 struct nx_host_sds_ring *sds_ring;
196 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
197
198 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
199 return -ENOMEM;
200
201 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
202 sds_ring = &recv_ctx->sds_rings[ring];
203 netif_napi_add(netdev, &sds_ring->napi,
204 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
205 }
206
207 return 0;
208 }
209
210 static void
211 netxen_napi_del(struct netxen_adapter *adapter)
212 {
213 int ring;
214 struct nx_host_sds_ring *sds_ring;
215 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
216
217 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
218 sds_ring = &recv_ctx->sds_rings[ring];
219 netif_napi_del(&sds_ring->napi);
220 }
221
222 netxen_free_sds_rings(&adapter->recv_ctx);
223 }
224
225 static void
226 netxen_napi_enable(struct netxen_adapter *adapter)
227 {
228 int ring;
229 struct nx_host_sds_ring *sds_ring;
230 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
231
232 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
233 sds_ring = &recv_ctx->sds_rings[ring];
234 napi_enable(&sds_ring->napi);
235 netxen_nic_enable_int(sds_ring);
236 }
237 }
238
239 static void
240 netxen_napi_disable(struct netxen_adapter *adapter)
241 {
242 int ring;
243 struct nx_host_sds_ring *sds_ring;
244 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
245
246 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
247 sds_ring = &recv_ctx->sds_rings[ring];
248 netxen_nic_disable_int(sds_ring);
249 napi_synchronize(&sds_ring->napi);
250 napi_disable(&sds_ring->napi);
251 }
252 }
253
254 static int nx_set_dma_mask(struct netxen_adapter *adapter)
255 {
256 struct pci_dev *pdev = adapter->pdev;
257 uint64_t mask, cmask;
258
259 adapter->pci_using_dac = 0;
260
261 mask = DMA_BIT_MASK(32);
262 cmask = DMA_BIT_MASK(32);
263
264 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
265 #ifndef CONFIG_IA64
266 mask = DMA_BIT_MASK(35);
267 #endif
268 } else {
269 mask = DMA_BIT_MASK(39);
270 cmask = mask;
271 }
272
273 if (pci_set_dma_mask(pdev, mask) == 0 &&
274 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
275 adapter->pci_using_dac = 1;
276 return 0;
277 }
278
279 return -EIO;
280 }
281
282 /* Update addressable range if firmware supports it */
283 static int
284 nx_update_dma_mask(struct netxen_adapter *adapter)
285 {
286 int change, shift, err;
287 uint64_t mask, old_mask, old_cmask;
288 struct pci_dev *pdev = adapter->pdev;
289
290 change = 0;
291
292 shift = NXRD32(adapter, CRB_DMA_SHIFT);
293 if (shift > 32)
294 return 0;
295
296 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
297 change = 1;
298 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
299 change = 1;
300
301 if (change) {
302 old_mask = pdev->dma_mask;
303 old_cmask = pdev->dev.coherent_dma_mask;
304
305 mask = DMA_BIT_MASK(32+shift);
306
307 err = pci_set_dma_mask(pdev, mask);
308 if (err)
309 goto err_out;
310
311 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
312
313 err = pci_set_consistent_dma_mask(pdev, mask);
314 if (err)
315 goto err_out;
316 }
317 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
318 }
319
320 return 0;
321
322 err_out:
323 pci_set_dma_mask(pdev, old_mask);
324 pci_set_consistent_dma_mask(pdev, old_cmask);
325 return err;
326 }
327
328 static int
329 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
330 {
331 u32 val, timeout;
332
333 if (first_boot == 0x55555555) {
334 /* This is the first boot after power up */
335 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
336
337 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
338 return 0;
339
340 /* PCI bus master workaround */
341 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
342 if (!(first_boot & 0x4)) {
343 first_boot |= 0x4;
344 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
345 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
346 }
347
348 /* This is the first boot after power up */
349 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
350 if (first_boot != 0x80000f) {
351 /* clear the register for future unloads/loads */
352 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
353 return -EIO;
354 }
355
356 /* Start P2 boot loader */
357 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
358 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
359 timeout = 0;
360 do {
361 msleep(1);
362 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
363
364 if (++timeout > 5000)
365 return -EIO;
366
367 } while (val == NETXEN_BDINFO_MAGIC);
368 }
369 return 0;
370 }
371
372 static void netxen_set_port_mode(struct netxen_adapter *adapter)
373 {
374 u32 val, data;
375
376 val = adapter->ahw.board_type;
377 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
378 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
379 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
380 data = NETXEN_PORT_MODE_802_3_AP;
381 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
382 } else if (port_mode == NETXEN_PORT_MODE_XG) {
383 data = NETXEN_PORT_MODE_XG;
384 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
385 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
386 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
387 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
388 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
389 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
390 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
391 } else {
392 data = NETXEN_PORT_MODE_AUTO_NEG;
393 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
394 }
395
396 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
397 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
398 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
399 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
400 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
401 }
402 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
403 }
404 }
405
406 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
407 {
408 u32 control;
409 int pos;
410
411 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
412 if (pos) {
413 pci_read_config_dword(pdev, pos, &control);
414 if (enable)
415 control |= PCI_MSIX_FLAGS_ENABLE;
416 else
417 control = 0;
418 pci_write_config_dword(pdev, pos, control);
419 }
420 }
421
422 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
423 {
424 int i;
425
426 for (i = 0; i < count; i++)
427 adapter->msix_entries[i].entry = i;
428 }
429
430 static int
431 netxen_read_mac_addr(struct netxen_adapter *adapter)
432 {
433 int i;
434 unsigned char *p;
435 __le64 mac_addr;
436 struct net_device *netdev = adapter->netdev;
437 struct pci_dev *pdev = adapter->pdev;
438
439 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
440 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
441 return -EIO;
442 } else {
443 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
444 return -EIO;
445 }
446
447 p = (unsigned char *)&mac_addr;
448 for (i = 0; i < 6; i++)
449 netdev->dev_addr[i] = *(p + 5 - i);
450
451 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
452 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
453
454 /* set station address */
455
456 if (!is_valid_ether_addr(netdev->perm_addr))
457 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
458
459 return 0;
460 }
461
462 int netxen_nic_set_mac(struct net_device *netdev, void *p)
463 {
464 struct netxen_adapter *adapter = netdev_priv(netdev);
465 struct sockaddr *addr = p;
466
467 if (!is_valid_ether_addr(addr->sa_data))
468 return -EINVAL;
469
470 if (netif_running(netdev)) {
471 netif_device_detach(netdev);
472 netxen_napi_disable(adapter);
473 }
474
475 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
476 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
477 adapter->macaddr_set(adapter, addr->sa_data);
478
479 if (netif_running(netdev)) {
480 netif_device_attach(netdev);
481 netxen_napi_enable(adapter);
482 }
483 return 0;
484 }
485
486 static void netxen_set_multicast_list(struct net_device *dev)
487 {
488 struct netxen_adapter *adapter = netdev_priv(dev);
489
490 adapter->set_multi(dev);
491 }
492
493 static const struct net_device_ops netxen_netdev_ops = {
494 .ndo_open = netxen_nic_open,
495 .ndo_stop = netxen_nic_close,
496 .ndo_start_xmit = netxen_nic_xmit_frame,
497 .ndo_get_stats = netxen_nic_get_stats,
498 .ndo_validate_addr = eth_validate_addr,
499 .ndo_set_multicast_list = netxen_set_multicast_list,
500 .ndo_set_mac_address = netxen_nic_set_mac,
501 .ndo_change_mtu = netxen_nic_change_mtu,
502 .ndo_tx_timeout = netxen_tx_timeout,
503 #ifdef CONFIG_NET_POLL_CONTROLLER
504 .ndo_poll_controller = netxen_nic_poll_controller,
505 #endif
506 };
507
508 static void
509 netxen_setup_intr(struct netxen_adapter *adapter)
510 {
511 struct netxen_legacy_intr_set *legacy_intrp;
512 struct pci_dev *pdev = adapter->pdev;
513 int err, num_msix;
514
515 if (adapter->rss_supported) {
516 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
517 MSIX_ENTRIES_PER_ADAPTER : 2;
518 } else
519 num_msix = 1;
520
521 adapter->max_sds_rings = 1;
522
523 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
524
525 if (adapter->ahw.revision_id >= NX_P3_B0)
526 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
527 else
528 legacy_intrp = &legacy_intr[0];
529
530 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
531 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
532 legacy_intrp->tgt_status_reg);
533 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
534 legacy_intrp->tgt_mask_reg);
535 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
536 legacy_intrp->pci_int_reg);
537 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
538
539 if (adapter->ahw.revision_id >= NX_P3_B1)
540 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
541 ISR_INT_STATE_REG);
542 else
543 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
544 CRB_INT_VECTOR);
545
546 netxen_set_msix_bit(pdev, 0);
547
548 if (adapter->msix_supported) {
549
550 netxen_init_msix_entries(adapter, num_msix);
551 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
552 if (err == 0) {
553 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
554 netxen_set_msix_bit(pdev, 1);
555
556 if (adapter->rss_supported)
557 adapter->max_sds_rings = num_msix;
558
559 dev_info(&pdev->dev, "using msi-x interrupts\n");
560 return;
561 }
562
563 if (err > 0)
564 pci_disable_msix(pdev);
565
566 /* fall through for msi */
567 }
568
569 if (use_msi && !pci_enable_msi(pdev)) {
570 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
571 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
572 msi_tgt_status[adapter->ahw.pci_func]);
573 dev_info(&pdev->dev, "using msi interrupts\n");
574 adapter->msix_entries[0].vector = pdev->irq;
575 return;
576 }
577
578 dev_info(&pdev->dev, "using legacy interrupts\n");
579 adapter->msix_entries[0].vector = pdev->irq;
580 }
581
582 static void
583 netxen_teardown_intr(struct netxen_adapter *adapter)
584 {
585 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
586 pci_disable_msix(adapter->pdev);
587 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
588 pci_disable_msi(adapter->pdev);
589 }
590
591 static void
592 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
593 {
594 if (adapter->ahw.db_base != NULL)
595 iounmap(adapter->ahw.db_base);
596 if (adapter->ahw.pci_base0 != NULL)
597 iounmap(adapter->ahw.pci_base0);
598 if (adapter->ahw.pci_base1 != NULL)
599 iounmap(adapter->ahw.pci_base1);
600 if (adapter->ahw.pci_base2 != NULL)
601 iounmap(adapter->ahw.pci_base2);
602 }
603
604 static int
605 netxen_setup_pci_map(struct netxen_adapter *adapter)
606 {
607 void __iomem *mem_ptr0 = NULL;
608 void __iomem *mem_ptr1 = NULL;
609 void __iomem *mem_ptr2 = NULL;
610 void __iomem *db_ptr = NULL;
611
612 resource_size_t mem_base, db_base;
613 unsigned long mem_len, db_len = 0, pci_len0 = 0;
614
615 struct pci_dev *pdev = adapter->pdev;
616 int pci_func = adapter->ahw.pci_func;
617
618 int err = 0;
619
620 /*
621 * Set the CRB window to invalid. If any register in window 0 is
622 * accessed it should set the window to 0 and then reset it to 1.
623 */
624 adapter->ahw.crb_win = -1;
625 adapter->ahw.ocm_win = -1;
626
627 /* remap phys address */
628 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
629 mem_len = pci_resource_len(pdev, 0);
630
631 /* 128 Meg of memory */
632 if (mem_len == NETXEN_PCI_128MB_SIZE) {
633 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
634 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
635 SECOND_PAGE_GROUP_SIZE);
636 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
637 THIRD_PAGE_GROUP_SIZE);
638 pci_len0 = FIRST_PAGE_GROUP_SIZE;
639 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
640 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
641 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
642 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
643 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
644
645 mem_ptr0 = pci_ioremap_bar(pdev, 0);
646 if (mem_ptr0 == NULL) {
647 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
648 return -EIO;
649 }
650 pci_len0 = mem_len;
651 } else {
652 return -EIO;
653 }
654
655 netxen_setup_hwops(adapter);
656
657 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
658
659 adapter->ahw.pci_base0 = mem_ptr0;
660 adapter->ahw.pci_len0 = pci_len0;
661 adapter->ahw.pci_base1 = mem_ptr1;
662 adapter->ahw.pci_base2 = mem_ptr2;
663
664 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
665 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
666 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
667
668 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
669 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
670 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
671 }
672
673 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
674 goto skip_doorbell;
675
676 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
677 db_len = pci_resource_len(pdev, 4);
678
679 if (db_len == 0) {
680 printk(KERN_ERR "%s: doorbell is disabled\n",
681 netxen_nic_driver_name);
682 err = -EIO;
683 goto err_out;
684 }
685
686 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
687 if (!db_ptr) {
688 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
689 netxen_nic_driver_name);
690 err = -EIO;
691 goto err_out;
692 }
693
694 skip_doorbell:
695 adapter->ahw.db_base = db_ptr;
696 adapter->ahw.db_len = db_len;
697 return 0;
698
699 err_out:
700 netxen_cleanup_pci_map(adapter);
701 return err;
702 }
703
704 static void
705 netxen_check_options(struct netxen_adapter *adapter)
706 {
707 u32 fw_major, fw_minor, fw_build;
708 char brd_name[NETXEN_MAX_SHORT_NAME];
709 char serial_num[32];
710 int i, offset, val;
711 int *ptr32;
712 struct pci_dev *pdev = adapter->pdev;
713
714 adapter->driver_mismatch = 0;
715
716 ptr32 = (int *)&serial_num;
717 offset = NX_FW_SERIAL_NUM_OFFSET;
718 for (i = 0; i < 8; i++) {
719 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
720 dev_err(&pdev->dev, "error reading board info\n");
721 adapter->driver_mismatch = 1;
722 return;
723 }
724 ptr32[i] = cpu_to_le32(val);
725 offset += sizeof(u32);
726 }
727
728 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
729 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
730 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
731
732 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
733
734 if (adapter->portnum == 0) {
735 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
736
737 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
738 module_name(THIS_MODULE),
739 brd_name, serial_num, adapter->ahw.revision_id);
740 }
741
742 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
743 adapter->driver_mismatch = 1;
744 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
745 fw_major, fw_minor, fw_build);
746 return;
747 }
748
749 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
750 i = NXRD32(adapter, NETXEN_SRE_MISC);
751 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
752 }
753
754 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
755 fw_major, fw_minor, fw_build,
756 adapter->ahw.cut_through ? "cut-through" : "legacy");
757
758 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
759 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
760
761 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
762
763 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
764 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
765 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
766 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
767 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
768 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
769 }
770
771 adapter->msix_supported = 0;
772 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
773 adapter->msix_supported = !!use_msi_x;
774 adapter->rss_supported = !!use_msi_x;
775 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
776 switch (adapter->ahw.board_type) {
777 case NETXEN_BRDTYPE_P2_SB31_10G:
778 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
779 adapter->msix_supported = !!use_msi_x;
780 adapter->rss_supported = !!use_msi_x;
781 break;
782 default:
783 break;
784 }
785 }
786
787 adapter->num_txd = MAX_CMD_DESCRIPTORS;
788
789 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
790 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
791 adapter->max_rds_rings = 3;
792 } else {
793 adapter->num_lro_rxd = 0;
794 adapter->max_rds_rings = 2;
795 }
796 }
797
798 static int
799 netxen_start_firmware(struct netxen_adapter *adapter)
800 {
801 int val, err, first_boot;
802 struct pci_dev *pdev = adapter->pdev;
803
804 /* required for NX2031 dummy dma */
805 err = nx_set_dma_mask(adapter);
806 if (err)
807 return err;
808
809 if (!netxen_can_start_firmware(adapter))
810 goto wait_init;
811
812 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
813
814 err = netxen_check_hw_init(adapter, first_boot);
815 if (err) {
816 dev_err(&pdev->dev, "error in init HW init sequence\n");
817 return err;
818 }
819
820 netxen_request_firmware(adapter);
821
822 err = netxen_need_fw_reset(adapter);
823 if (err < 0)
824 goto err_out;
825 if (err == 0)
826 goto wait_init;
827
828 if (first_boot != 0x55555555) {
829 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
830 netxen_pinit_from_rom(adapter);
831 msleep(1);
832 }
833
834 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
835 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
836 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
837
838 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
839 netxen_set_port_mode(adapter);
840
841 err = netxen_load_firmware(adapter);
842 if (err)
843 goto err_out;
844
845 netxen_release_firmware(adapter);
846
847 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
848
849 /* Initialize multicast addr pool owners */
850 val = 0x7654;
851 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
852 val |= 0x0f000000;
853 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
854
855 }
856
857 err = netxen_init_dummy_dma(adapter);
858 if (err)
859 goto err_out;
860
861 /*
862 * Tell the hardware our version number.
863 */
864 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
865 | ((_NETXEN_NIC_LINUX_MINOR << 8))
866 | (_NETXEN_NIC_LINUX_SUBVERSION);
867 NXWR32(adapter, CRB_DRIVER_VERSION, val);
868
869 wait_init:
870 /* Handshake with the card before we register the devices. */
871 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
872 if (err) {
873 netxen_free_dummy_dma(adapter);
874 goto err_out;
875 }
876
877 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
878
879 nx_update_dma_mask(adapter);
880
881 netxen_check_options(adapter);
882
883 adapter->need_fw_reset = 0;
884
885 /* fall through and release firmware */
886
887 err_out:
888 netxen_release_firmware(adapter);
889 return err;
890 }
891
892 static int
893 netxen_nic_request_irq(struct netxen_adapter *adapter)
894 {
895 irq_handler_t handler;
896 struct nx_host_sds_ring *sds_ring;
897 int err, ring;
898
899 unsigned long flags = IRQF_SAMPLE_RANDOM;
900 struct net_device *netdev = adapter->netdev;
901 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
902
903 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
904 handler = netxen_msix_intr;
905 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
906 handler = netxen_msi_intr;
907 else {
908 flags |= IRQF_SHARED;
909 handler = netxen_intr;
910 }
911 adapter->irq = netdev->irq;
912
913 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
914 sds_ring = &recv_ctx->sds_rings[ring];
915 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
916 err = request_irq(sds_ring->irq, handler,
917 flags, sds_ring->name, sds_ring);
918 if (err)
919 return err;
920 }
921
922 return 0;
923 }
924
925 static void
926 netxen_nic_free_irq(struct netxen_adapter *adapter)
927 {
928 int ring;
929 struct nx_host_sds_ring *sds_ring;
930
931 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
932
933 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
934 sds_ring = &recv_ctx->sds_rings[ring];
935 free_irq(sds_ring->irq, sds_ring);
936 }
937 }
938
939 static void
940 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
941 {
942 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
943 adapter->coal.normal.data.rx_time_us =
944 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
945 adapter->coal.normal.data.rx_packets =
946 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
947 adapter->coal.normal.data.tx_time_us =
948 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
949 adapter->coal.normal.data.tx_packets =
950 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
951 }
952
953 /* with rtnl_lock */
954 static int
955 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
956 {
957 int err;
958
959 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
960 return -EIO;
961
962 err = adapter->init_port(adapter, adapter->physical_port);
963 if (err) {
964 printk(KERN_ERR "%s: Failed to initialize port %d\n",
965 netxen_nic_driver_name, adapter->portnum);
966 return err;
967 }
968 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
969 adapter->macaddr_set(adapter, adapter->mac_addr);
970
971 adapter->set_multi(netdev);
972 adapter->set_mtu(adapter, netdev->mtu);
973
974 adapter->ahw.linkup = 0;
975
976 if (adapter->max_sds_rings > 1)
977 netxen_config_rss(adapter, 1);
978
979 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
980 netxen_config_intr_coalesce(adapter);
981
982 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
983 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
984
985 netxen_napi_enable(adapter);
986
987 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
988 netxen_linkevent_request(adapter, 1);
989 else
990 netxen_nic_set_link_parameters(adapter);
991
992 set_bit(__NX_DEV_UP, &adapter->state);
993 return 0;
994 }
995
996 /* Usage: During resume and firmware recovery module.*/
997
998 static inline int
999 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1000 {
1001 int err = 0;
1002
1003 rtnl_lock();
1004 if (netif_running(netdev))
1005 err = __netxen_nic_up(adapter, netdev);
1006 rtnl_unlock();
1007
1008 return err;
1009 }
1010
1011 /* with rtnl_lock */
1012 static void
1013 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1014 {
1015 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1016 return;
1017
1018 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1019 return;
1020
1021 smp_mb();
1022 spin_lock(&adapter->tx_clean_lock);
1023 netif_carrier_off(netdev);
1024 netif_tx_disable(netdev);
1025
1026 if (adapter->stop_port)
1027 adapter->stop_port(adapter);
1028
1029 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1030 netxen_p3_free_mac_list(adapter);
1031
1032 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1033
1034 netxen_napi_disable(adapter);
1035
1036 netxen_release_tx_buffers(adapter);
1037 spin_unlock(&adapter->tx_clean_lock);
1038 }
1039
1040 /* Usage: During suspend and firmware recovery module */
1041
1042 static inline void
1043 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1044 {
1045 rtnl_lock();
1046 if (netif_running(netdev))
1047 __netxen_nic_down(adapter, netdev);
1048 rtnl_unlock();
1049
1050 }
1051
1052 static int
1053 netxen_nic_attach(struct netxen_adapter *adapter)
1054 {
1055 struct net_device *netdev = adapter->netdev;
1056 struct pci_dev *pdev = adapter->pdev;
1057 int err, ring;
1058 struct nx_host_rds_ring *rds_ring;
1059 struct nx_host_tx_ring *tx_ring;
1060
1061 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1062 return 0;
1063
1064 err = netxen_init_firmware(adapter);
1065 if (err)
1066 return err;
1067
1068 err = netxen_napi_add(adapter, netdev);
1069 if (err)
1070 return err;
1071
1072 err = netxen_alloc_sw_resources(adapter);
1073 if (err) {
1074 printk(KERN_ERR "%s: Error in setting sw resources\n",
1075 netdev->name);
1076 return err;
1077 }
1078
1079 err = netxen_alloc_hw_resources(adapter);
1080 if (err) {
1081 printk(KERN_ERR "%s: Error in setting hw resources\n",
1082 netdev->name);
1083 goto err_out_free_sw;
1084 }
1085
1086 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1087 tx_ring = adapter->tx_ring;
1088 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1089 crb_cmd_producer[adapter->portnum]);
1090 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1091 crb_cmd_consumer[adapter->portnum]);
1092
1093 tx_ring->producer = 0;
1094 tx_ring->sw_consumer = 0;
1095
1096 netxen_nic_update_cmd_producer(adapter, tx_ring);
1097 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1098 }
1099
1100 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1101 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1102 netxen_post_rx_buffers(adapter, ring, rds_ring);
1103 }
1104
1105 err = netxen_nic_request_irq(adapter);
1106 if (err) {
1107 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1108 netdev->name);
1109 goto err_out_free_rxbuf;
1110 }
1111
1112 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1113 netxen_nic_init_coalesce_defaults(adapter);
1114
1115 netxen_create_sysfs_entries(adapter);
1116
1117 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1118 return 0;
1119
1120 err_out_free_rxbuf:
1121 netxen_release_rx_buffers(adapter);
1122 netxen_free_hw_resources(adapter);
1123 err_out_free_sw:
1124 netxen_free_sw_resources(adapter);
1125 return err;
1126 }
1127
1128 static void
1129 netxen_nic_detach(struct netxen_adapter *adapter)
1130 {
1131 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1132 return;
1133
1134 netxen_remove_sysfs_entries(adapter);
1135
1136 netxen_free_hw_resources(adapter);
1137 netxen_release_rx_buffers(adapter);
1138 netxen_nic_free_irq(adapter);
1139 netxen_napi_del(adapter);
1140 netxen_free_sw_resources(adapter);
1141
1142 adapter->is_up = 0;
1143 }
1144
1145 int
1146 netxen_nic_reset_context(struct netxen_adapter *adapter)
1147 {
1148 int err = 0;
1149 struct net_device *netdev = adapter->netdev;
1150
1151 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1152 return -EBUSY;
1153
1154 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1155
1156 netif_device_detach(netdev);
1157
1158 if (netif_running(netdev))
1159 __netxen_nic_down(adapter, netdev);
1160
1161 netxen_nic_detach(adapter);
1162
1163 if (netif_running(netdev)) {
1164 err = netxen_nic_attach(adapter);
1165 if (!err)
1166 err = __netxen_nic_up(adapter, netdev);
1167
1168 if (err)
1169 goto done;
1170 }
1171
1172 netif_device_attach(netdev);
1173 }
1174
1175 done:
1176 clear_bit(__NX_RESETTING, &adapter->state);
1177 return err;
1178 }
1179
1180 static int
1181 netxen_setup_netdev(struct netxen_adapter *adapter,
1182 struct net_device *netdev)
1183 {
1184 int err = 0;
1185 struct pci_dev *pdev = adapter->pdev;
1186
1187 adapter->rx_csum = 1;
1188 adapter->mc_enabled = 0;
1189 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1190 adapter->max_mc_count = 38;
1191 else
1192 adapter->max_mc_count = 16;
1193
1194 netdev->netdev_ops = &netxen_netdev_ops;
1195 netdev->watchdog_timeo = 2*HZ;
1196
1197 netxen_nic_change_mtu(netdev, netdev->mtu);
1198
1199 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1200
1201 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1202 netdev->features |= (NETIF_F_GRO);
1203 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1204
1205 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1206 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1207 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1208 }
1209
1210 if (adapter->pci_using_dac) {
1211 netdev->features |= NETIF_F_HIGHDMA;
1212 netdev->vlan_features |= NETIF_F_HIGHDMA;
1213 }
1214
1215 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1216 netdev->features |= (NETIF_F_HW_VLAN_TX);
1217
1218 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1219 netdev->features |= NETIF_F_LRO;
1220
1221 netdev->irq = adapter->msix_entries[0].vector;
1222
1223 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1224
1225 if (netxen_read_mac_addr(adapter))
1226 dev_warn(&pdev->dev, "failed to read mac addr\n");
1227
1228 netif_carrier_off(netdev);
1229 netif_stop_queue(netdev);
1230
1231 err = register_netdev(netdev);
1232 if (err) {
1233 dev_err(&pdev->dev, "failed to register net device\n");
1234 return err;
1235 }
1236
1237 return 0;
1238 }
1239
1240 static int __devinit
1241 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1242 {
1243 struct net_device *netdev = NULL;
1244 struct netxen_adapter *adapter = NULL;
1245 int i = 0, err;
1246 int pci_func_id = PCI_FUNC(pdev->devfn);
1247 uint8_t revision_id;
1248
1249 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1250 pr_warning("%s: chip revisions between 0x%x-0x%x"
1251 "will not be enabled.\n",
1252 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1253 return -ENODEV;
1254 }
1255
1256 if ((err = pci_enable_device(pdev)))
1257 return err;
1258
1259 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1260 err = -ENODEV;
1261 goto err_out_disable_pdev;
1262 }
1263
1264 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1265 goto err_out_disable_pdev;
1266
1267 if (NX_IS_REVISION_P3(pdev->revision))
1268 pci_enable_pcie_error_reporting(pdev);
1269
1270 pci_set_master(pdev);
1271
1272 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1273 if(!netdev) {
1274 dev_err(&pdev->dev, "failed to allocate net_device\n");
1275 err = -ENOMEM;
1276 goto err_out_free_res;
1277 }
1278
1279 SET_NETDEV_DEV(netdev, &pdev->dev);
1280
1281 adapter = netdev_priv(netdev);
1282 adapter->netdev = netdev;
1283 adapter->pdev = pdev;
1284 adapter->ahw.pci_func = pci_func_id;
1285
1286 revision_id = pdev->revision;
1287 adapter->ahw.revision_id = revision_id;
1288
1289 rwlock_init(&adapter->ahw.crb_lock);
1290 spin_lock_init(&adapter->ahw.mem_lock);
1291
1292 spin_lock_init(&adapter->tx_clean_lock);
1293 INIT_LIST_HEAD(&adapter->mac_list);
1294
1295 err = netxen_setup_pci_map(adapter);
1296 if (err)
1297 goto err_out_free_netdev;
1298
1299 /* This will be reset for mezz cards */
1300 adapter->portnum = pci_func_id;
1301
1302 err = netxen_nic_get_board_info(adapter);
1303 if (err) {
1304 dev_err(&pdev->dev, "Error getting board config info.\n");
1305 goto err_out_iounmap;
1306 }
1307
1308 /* Mezz cards have PCI function 0,2,3 enabled */
1309 switch (adapter->ahw.board_type) {
1310 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1311 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1312 if (pci_func_id >= 2)
1313 adapter->portnum = pci_func_id - 2;
1314 break;
1315 default:
1316 break;
1317 }
1318
1319 err = netxen_start_firmware(adapter);
1320 if (err)
1321 goto err_out_decr_ref;
1322
1323 /*
1324 * See if the firmware gave us a virtual-physical port mapping.
1325 */
1326 adapter->physical_port = adapter->portnum;
1327 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1328 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1329 if (i != 0x55555555)
1330 adapter->physical_port = i;
1331 }
1332
1333 netxen_nic_clear_stats(adapter);
1334
1335 netxen_setup_intr(adapter);
1336
1337 err = netxen_setup_netdev(adapter, netdev);
1338 if (err)
1339 goto err_out_disable_msi;
1340
1341 pci_set_drvdata(pdev, adapter);
1342
1343 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1344
1345 switch (adapter->ahw.port_type) {
1346 case NETXEN_NIC_GBE:
1347 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1348 adapter->netdev->name);
1349 break;
1350 case NETXEN_NIC_XGBE:
1351 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1352 adapter->netdev->name);
1353 break;
1354 }
1355
1356 netxen_create_diag_entries(adapter);
1357
1358 return 0;
1359
1360 err_out_disable_msi:
1361 netxen_teardown_intr(adapter);
1362
1363 netxen_free_dummy_dma(adapter);
1364
1365 err_out_decr_ref:
1366 nx_decr_dev_ref_cnt(adapter);
1367
1368 err_out_iounmap:
1369 netxen_cleanup_pci_map(adapter);
1370
1371 err_out_free_netdev:
1372 free_netdev(netdev);
1373
1374 err_out_free_res:
1375 pci_release_regions(pdev);
1376
1377 err_out_disable_pdev:
1378 pci_set_drvdata(pdev, NULL);
1379 pci_disable_device(pdev);
1380 return err;
1381 }
1382
1383 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1384 {
1385 struct netxen_adapter *adapter;
1386 struct net_device *netdev;
1387
1388 adapter = pci_get_drvdata(pdev);
1389 if (adapter == NULL)
1390 return;
1391
1392 netdev = adapter->netdev;
1393
1394 netxen_cancel_fw_work(adapter);
1395
1396 unregister_netdev(netdev);
1397
1398 cancel_work_sync(&adapter->tx_timeout_task);
1399
1400 netxen_nic_detach(adapter);
1401
1402 nx_decr_dev_ref_cnt(adapter);
1403
1404 if (adapter->portnum == 0)
1405 netxen_free_dummy_dma(adapter);
1406
1407 clear_bit(__NX_RESETTING, &adapter->state);
1408
1409 netxen_teardown_intr(adapter);
1410
1411 netxen_remove_diag_entries(adapter);
1412
1413 netxen_cleanup_pci_map(adapter);
1414
1415 netxen_release_firmware(adapter);
1416
1417 if (NX_IS_REVISION_P3(pdev->revision))
1418 pci_disable_pcie_error_reporting(pdev);
1419
1420 pci_release_regions(pdev);
1421 pci_disable_device(pdev);
1422 pci_set_drvdata(pdev, NULL);
1423
1424 free_netdev(netdev);
1425 }
1426
1427 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1428 {
1429 struct net_device *netdev = adapter->netdev;
1430
1431 netif_device_detach(netdev);
1432
1433 netxen_cancel_fw_work(adapter);
1434
1435 if (netif_running(netdev))
1436 netxen_nic_down(adapter, netdev);
1437
1438 cancel_work_sync(&adapter->tx_timeout_task);
1439
1440 netxen_nic_detach(adapter);
1441
1442 if (adapter->portnum == 0)
1443 netxen_free_dummy_dma(adapter);
1444
1445 nx_decr_dev_ref_cnt(adapter);
1446
1447 clear_bit(__NX_RESETTING, &adapter->state);
1448 }
1449
1450 static int netxen_nic_attach_func(struct pci_dev *pdev)
1451 {
1452 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1453 struct net_device *netdev = adapter->netdev;
1454 int err;
1455
1456 err = pci_enable_device(pdev);
1457 if (err)
1458 return err;
1459
1460 pci_set_power_state(pdev, PCI_D0);
1461 pci_set_master(pdev);
1462 pci_restore_state(pdev);
1463
1464 adapter->ahw.crb_win = -1;
1465 adapter->ahw.ocm_win = -1;
1466
1467 err = netxen_start_firmware(adapter);
1468 if (err) {
1469 dev_err(&pdev->dev, "failed to start firmware\n");
1470 return err;
1471 }
1472
1473 if (netif_running(netdev)) {
1474 err = netxen_nic_attach(adapter);
1475 if (err)
1476 goto err_out;
1477
1478 err = netxen_nic_up(adapter, netdev);
1479 if (err)
1480 goto err_out_detach;
1481
1482 netxen_config_indev_addr(netdev, NETDEV_UP);
1483 }
1484
1485 netif_device_attach(netdev);
1486 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1487 return 0;
1488
1489 err_out_detach:
1490 netxen_nic_detach(adapter);
1491 err_out:
1492 nx_decr_dev_ref_cnt(adapter);
1493 return err;
1494 }
1495
1496 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1497 pci_channel_state_t state)
1498 {
1499 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1500
1501 if (state == pci_channel_io_perm_failure)
1502 return PCI_ERS_RESULT_DISCONNECT;
1503
1504 if (nx_dev_request_aer(adapter))
1505 return PCI_ERS_RESULT_RECOVERED;
1506
1507 netxen_nic_detach_func(adapter);
1508
1509 pci_disable_device(pdev);
1510
1511 return PCI_ERS_RESULT_NEED_RESET;
1512 }
1513
1514 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1515 {
1516 int err = 0;
1517
1518 err = netxen_nic_attach_func(pdev);
1519
1520 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1521 }
1522
1523 static void netxen_io_resume(struct pci_dev *pdev)
1524 {
1525 pci_cleanup_aer_uncorrect_error_status(pdev);
1526 }
1527
1528 static void netxen_nic_shutdown(struct pci_dev *pdev)
1529 {
1530 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1531
1532 netxen_nic_detach_func(adapter);
1533
1534 if (pci_save_state(pdev))
1535 return;
1536
1537 if (netxen_nic_wol_supported(adapter)) {
1538 pci_enable_wake(pdev, PCI_D3cold, 1);
1539 pci_enable_wake(pdev, PCI_D3hot, 1);
1540 }
1541
1542 pci_disable_device(pdev);
1543 }
1544
1545 #ifdef CONFIG_PM
1546 static int
1547 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1548 {
1549 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1550 int retval;
1551
1552 netxen_nic_detach_func(adapter);
1553
1554 retval = pci_save_state(pdev);
1555 if (retval)
1556 return retval;
1557
1558 if (netxen_nic_wol_supported(adapter)) {
1559 pci_enable_wake(pdev, PCI_D3cold, 1);
1560 pci_enable_wake(pdev, PCI_D3hot, 1);
1561 }
1562
1563 pci_disable_device(pdev);
1564 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1565
1566 return 0;
1567 }
1568
1569 static int
1570 netxen_nic_resume(struct pci_dev *pdev)
1571 {
1572 return netxen_nic_attach_func(pdev);
1573 }
1574 #endif
1575
1576 static int netxen_nic_open(struct net_device *netdev)
1577 {
1578 struct netxen_adapter *adapter = netdev_priv(netdev);
1579 int err = 0;
1580
1581 if (adapter->driver_mismatch)
1582 return -EIO;
1583
1584 err = netxen_nic_attach(adapter);
1585 if (err)
1586 return err;
1587
1588 err = __netxen_nic_up(adapter, netdev);
1589 if (err)
1590 goto err_out;
1591
1592 netif_start_queue(netdev);
1593
1594 return 0;
1595
1596 err_out:
1597 netxen_nic_detach(adapter);
1598 return err;
1599 }
1600
1601 /*
1602 * netxen_nic_close - Disables a network interface entry point
1603 */
1604 static int netxen_nic_close(struct net_device *netdev)
1605 {
1606 struct netxen_adapter *adapter = netdev_priv(netdev);
1607
1608 __netxen_nic_down(adapter, netdev);
1609 return 0;
1610 }
1611
1612 static void
1613 netxen_tso_check(struct net_device *netdev,
1614 struct nx_host_tx_ring *tx_ring,
1615 struct cmd_desc_type0 *first_desc,
1616 struct sk_buff *skb)
1617 {
1618 u8 opcode = TX_ETHER_PKT;
1619 __be16 protocol = skb->protocol;
1620 u16 flags = 0, vid = 0;
1621 u32 producer;
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
1626 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1627
1628 vh = (struct vlan_ethhdr *)skb->data;
1629 protocol = vh->h_vlan_encapsulated_proto;
1630 flags = FLAGS_VLAN_TAGGED;
1631
1632 } else if (vlan_tx_tag_present(skb)) {
1633
1634 flags = FLAGS_VLAN_OOB;
1635 vid = vlan_tx_tag_get(skb);
1636 netxen_set_tx_vlan_tci(first_desc, vid);
1637 vlan_oob = 1;
1638 }
1639
1640 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1641 skb_shinfo(skb)->gso_size > 0) {
1642
1643 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1644
1645 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1646 first_desc->total_hdr_length = hdr_len;
1647 if (vlan_oob) {
1648 first_desc->total_hdr_length += VLAN_HLEN;
1649 first_desc->tcp_hdr_offset = VLAN_HLEN;
1650 first_desc->ip_hdr_offset = VLAN_HLEN;
1651 /* Only in case of TSO on vlan device */
1652 flags |= FLAGS_VLAN_TAGGED;
1653 }
1654
1655 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1656 TX_TCP_LSO6 : TX_TCP_LSO;
1657 tso = 1;
1658
1659 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1660 u8 l4proto;
1661
1662 if (protocol == cpu_to_be16(ETH_P_IP)) {
1663 l4proto = ip_hdr(skb)->protocol;
1664
1665 if (l4proto == IPPROTO_TCP)
1666 opcode = TX_TCP_PKT;
1667 else if(l4proto == IPPROTO_UDP)
1668 opcode = TX_UDP_PKT;
1669 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1670 l4proto = ipv6_hdr(skb)->nexthdr;
1671
1672 if (l4proto == IPPROTO_TCP)
1673 opcode = TX_TCPV6_PKT;
1674 else if(l4proto == IPPROTO_UDP)
1675 opcode = TX_UDPV6_PKT;
1676 }
1677 }
1678
1679 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1680 first_desc->ip_hdr_offset += skb_network_offset(skb);
1681 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1682
1683 if (!tso)
1684 return;
1685
1686 /* For LSO, we need to copy the MAC/IP/TCP headers into
1687 * the descriptor ring
1688 */
1689 producer = tx_ring->producer;
1690 copied = 0;
1691 offset = 2;
1692
1693 if (vlan_oob) {
1694 /* Create a TSO vlan header template for firmware */
1695
1696 hwdesc = &tx_ring->desc_head[producer];
1697 tx_ring->cmd_buf_arr[producer].skb = NULL;
1698
1699 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1700 hdr_len + VLAN_HLEN);
1701
1702 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1703 skb_copy_from_linear_data(skb, vh, 12);
1704 vh->h_vlan_proto = htons(ETH_P_8021Q);
1705 vh->h_vlan_TCI = htons(vid);
1706 skb_copy_from_linear_data_offset(skb, 12,
1707 (char *)vh + 16, copy_len - 16);
1708
1709 copied = copy_len - VLAN_HLEN;
1710 offset = 0;
1711
1712 producer = get_next_index(producer, tx_ring->num_desc);
1713 }
1714
1715 while (copied < hdr_len) {
1716
1717 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1718 (hdr_len - copied));
1719
1720 hwdesc = &tx_ring->desc_head[producer];
1721 tx_ring->cmd_buf_arr[producer].skb = NULL;
1722
1723 skb_copy_from_linear_data_offset(skb, copied,
1724 (char *)hwdesc + offset, copy_len);
1725
1726 copied += copy_len;
1727 offset = 0;
1728
1729 producer = get_next_index(producer, tx_ring->num_desc);
1730 }
1731
1732 tx_ring->producer = producer;
1733 barrier();
1734 }
1735
1736 static int
1737 netxen_map_tx_skb(struct pci_dev *pdev,
1738 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1739 {
1740 struct netxen_skb_frag *nf;
1741 struct skb_frag_struct *frag;
1742 int i, nr_frags;
1743 dma_addr_t map;
1744
1745 nr_frags = skb_shinfo(skb)->nr_frags;
1746 nf = &pbuf->frag_array[0];
1747
1748 map = pci_map_single(pdev, skb->data,
1749 skb_headlen(skb), PCI_DMA_TODEVICE);
1750 if (pci_dma_mapping_error(pdev, map))
1751 goto out_err;
1752
1753 nf->dma = map;
1754 nf->length = skb_headlen(skb);
1755
1756 for (i = 0; i < nr_frags; i++) {
1757 frag = &skb_shinfo(skb)->frags[i];
1758 nf = &pbuf->frag_array[i+1];
1759
1760 map = pci_map_page(pdev, frag->page, frag->page_offset,
1761 frag->size, PCI_DMA_TODEVICE);
1762 if (pci_dma_mapping_error(pdev, map))
1763 goto unwind;
1764
1765 nf->dma = map;
1766 nf->length = frag->size;
1767 }
1768
1769 return 0;
1770
1771 unwind:
1772 while (--i >= 0) {
1773 nf = &pbuf->frag_array[i+1];
1774 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1775 }
1776
1777 nf = &pbuf->frag_array[0];
1778 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1779
1780 out_err:
1781 return -ENOMEM;
1782 }
1783
1784 static inline void
1785 netxen_clear_cmddesc(u64 *desc)
1786 {
1787 desc[0] = 0ULL;
1788 desc[2] = 0ULL;
1789 }
1790
1791 static netdev_tx_t
1792 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1793 {
1794 struct netxen_adapter *adapter = netdev_priv(netdev);
1795 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1796 struct netxen_cmd_buffer *pbuf;
1797 struct netxen_skb_frag *buffrag;
1798 struct cmd_desc_type0 *hwdesc, *first_desc;
1799 struct pci_dev *pdev;
1800 int i, k;
1801
1802 u32 producer;
1803 int frag_count, no_of_desc;
1804 u32 num_txd = tx_ring->num_desc;
1805
1806 frag_count = skb_shinfo(skb)->nr_frags + 1;
1807
1808 /* 4 fragments per cmd des */
1809 no_of_desc = (frag_count + 3) >> 2;
1810
1811 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
1812 netif_stop_queue(netdev);
1813 return NETDEV_TX_BUSY;
1814 }
1815
1816 producer = tx_ring->producer;
1817 pbuf = &tx_ring->cmd_buf_arr[producer];
1818
1819 pdev = adapter->pdev;
1820
1821 if (netxen_map_tx_skb(pdev, skb, pbuf))
1822 goto drop_packet;
1823
1824 pbuf->skb = skb;
1825 pbuf->frag_count = frag_count;
1826
1827 first_desc = hwdesc = &tx_ring->desc_head[producer];
1828 netxen_clear_cmddesc((u64 *)hwdesc);
1829
1830 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1831 netxen_set_tx_port(first_desc, adapter->portnum);
1832
1833 for (i = 0; i < frag_count; i++) {
1834
1835 k = i % 4;
1836
1837 if ((k == 0) && (i > 0)) {
1838 /* move to next desc.*/
1839 producer = get_next_index(producer, num_txd);
1840 hwdesc = &tx_ring->desc_head[producer];
1841 netxen_clear_cmddesc((u64 *)hwdesc);
1842 tx_ring->cmd_buf_arr[producer].skb = NULL;
1843 }
1844
1845 buffrag = &pbuf->frag_array[i];
1846
1847 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1848 switch (k) {
1849 case 0:
1850 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1851 break;
1852 case 1:
1853 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1854 break;
1855 case 2:
1856 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1857 break;
1858 case 3:
1859 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1860 break;
1861 }
1862 }
1863
1864 tx_ring->producer = get_next_index(producer, num_txd);
1865
1866 netxen_tso_check(netdev, tx_ring, first_desc, skb);
1867
1868 netxen_nic_update_cmd_producer(adapter, tx_ring);
1869
1870 adapter->stats.txbytes += skb->len;
1871 adapter->stats.xmitcalled++;
1872
1873 return NETDEV_TX_OK;
1874
1875 drop_packet:
1876 adapter->stats.txdropped++;
1877 dev_kfree_skb_any(skb);
1878 return NETDEV_TX_OK;
1879 }
1880
1881 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1882 {
1883 struct net_device *netdev = adapter->netdev;
1884 uint32_t temp, temp_state, temp_val;
1885 int rv = 0;
1886
1887 temp = NXRD32(adapter, CRB_TEMP_STATE);
1888
1889 temp_state = nx_get_temp_state(temp);
1890 temp_val = nx_get_temp_val(temp);
1891
1892 if (temp_state == NX_TEMP_PANIC) {
1893 printk(KERN_ALERT
1894 "%s: Device temperature %d degrees C exceeds"
1895 " maximum allowed. Hardware has been shut down.\n",
1896 netdev->name, temp_val);
1897 rv = 1;
1898 } else if (temp_state == NX_TEMP_WARN) {
1899 if (adapter->temp == NX_TEMP_NORMAL) {
1900 printk(KERN_ALERT
1901 "%s: Device temperature %d degrees C "
1902 "exceeds operating range."
1903 " Immediate action needed.\n",
1904 netdev->name, temp_val);
1905 }
1906 } else {
1907 if (adapter->temp == NX_TEMP_WARN) {
1908 printk(KERN_INFO
1909 "%s: Device temperature is now %d degrees C"
1910 " in normal range.\n", netdev->name,
1911 temp_val);
1912 }
1913 }
1914 adapter->temp = temp_state;
1915 return rv;
1916 }
1917
1918 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1919 {
1920 struct net_device *netdev = adapter->netdev;
1921
1922 if (adapter->ahw.linkup && !linkup) {
1923 printk(KERN_INFO "%s: %s NIC Link is down\n",
1924 netxen_nic_driver_name, netdev->name);
1925 adapter->ahw.linkup = 0;
1926 if (netif_running(netdev)) {
1927 netif_carrier_off(netdev);
1928 netif_stop_queue(netdev);
1929 }
1930 adapter->link_changed = !adapter->has_link_events;
1931 } else if (!adapter->ahw.linkup && linkup) {
1932 printk(KERN_INFO "%s: %s NIC Link is up\n",
1933 netxen_nic_driver_name, netdev->name);
1934 adapter->ahw.linkup = 1;
1935 if (netif_running(netdev)) {
1936 netif_carrier_on(netdev);
1937 netif_wake_queue(netdev);
1938 }
1939 adapter->link_changed = !adapter->has_link_events;
1940 }
1941 }
1942
1943 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1944 {
1945 u32 val, port, linkup;
1946
1947 port = adapter->physical_port;
1948
1949 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1950 val = NXRD32(adapter, CRB_XG_STATE_P3);
1951 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1952 linkup = (val == XG_LINK_UP_P3);
1953 } else {
1954 val = NXRD32(adapter, CRB_XG_STATE);
1955 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1956 linkup = (val >> port) & 1;
1957 else {
1958 val = (val >> port*8) & 0xff;
1959 linkup = (val == XG_LINK_UP);
1960 }
1961 }
1962
1963 netxen_advert_link_change(adapter, linkup);
1964 }
1965
1966 static void netxen_tx_timeout(struct net_device *netdev)
1967 {
1968 struct netxen_adapter *adapter = netdev_priv(netdev);
1969
1970 if (test_bit(__NX_RESETTING, &adapter->state))
1971 return;
1972
1973 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1974 schedule_work(&adapter->tx_timeout_task);
1975 }
1976
1977 static void netxen_tx_timeout_task(struct work_struct *work)
1978 {
1979 struct netxen_adapter *adapter =
1980 container_of(work, struct netxen_adapter, tx_timeout_task);
1981
1982 if (!netif_running(adapter->netdev))
1983 return;
1984
1985 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1986 return;
1987
1988 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
1989 goto request_reset;
1990
1991 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1992 /* try to scrub interrupt */
1993 netxen_napi_disable(adapter);
1994
1995 adapter->netdev->trans_start = jiffies;
1996
1997 netxen_napi_enable(adapter);
1998
1999 netif_wake_queue(adapter->netdev);
2000
2001 clear_bit(__NX_RESETTING, &adapter->state);
2002
2003 } else {
2004 clear_bit(__NX_RESETTING, &adapter->state);
2005 if (!netxen_nic_reset_context(adapter)) {
2006 adapter->netdev->trans_start = jiffies;
2007 return;
2008 }
2009
2010 /* context reset failed, fall through for fw reset */
2011 }
2012
2013 request_reset:
2014 adapter->need_fw_reset = 1;
2015 clear_bit(__NX_RESETTING, &adapter->state);
2016 }
2017
2018 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
2019 {
2020 struct netxen_adapter *adapter = netdev_priv(netdev);
2021 struct net_device_stats *stats = &netdev->stats;
2022
2023 memset(stats, 0, sizeof(*stats));
2024
2025 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2026 stats->tx_packets = adapter->stats.xmitfinished;
2027 stats->rx_bytes = adapter->stats.rxbytes;
2028 stats->tx_bytes = adapter->stats.txbytes;
2029 stats->rx_dropped = adapter->stats.rxdropped;
2030 stats->tx_dropped = adapter->stats.txdropped;
2031
2032 return stats;
2033 }
2034
2035 static irqreturn_t netxen_intr(int irq, void *data)
2036 {
2037 struct nx_host_sds_ring *sds_ring = data;
2038 struct netxen_adapter *adapter = sds_ring->adapter;
2039 u32 status = 0;
2040
2041 status = readl(adapter->isr_int_vec);
2042
2043 if (!(status & adapter->int_vec_bit))
2044 return IRQ_NONE;
2045
2046 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2047 /* check interrupt state machine, to be sure */
2048 status = readl(adapter->crb_int_state_reg);
2049 if (!ISR_LEGACY_INT_TRIGGERED(status))
2050 return IRQ_NONE;
2051
2052 } else {
2053 unsigned long our_int = 0;
2054
2055 our_int = readl(adapter->crb_int_state_reg);
2056
2057 /* not our interrupt */
2058 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2059 return IRQ_NONE;
2060
2061 /* claim interrupt */
2062 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2063
2064 /* clear interrupt */
2065 netxen_nic_disable_int(sds_ring);
2066 }
2067
2068 writel(0xffffffff, adapter->tgt_status_reg);
2069 /* read twice to ensure write is flushed */
2070 readl(adapter->isr_int_vec);
2071 readl(adapter->isr_int_vec);
2072
2073 napi_schedule(&sds_ring->napi);
2074
2075 return IRQ_HANDLED;
2076 }
2077
2078 static irqreturn_t netxen_msi_intr(int irq, void *data)
2079 {
2080 struct nx_host_sds_ring *sds_ring = data;
2081 struct netxen_adapter *adapter = sds_ring->adapter;
2082
2083 /* clear interrupt */
2084 writel(0xffffffff, adapter->tgt_status_reg);
2085
2086 napi_schedule(&sds_ring->napi);
2087 return IRQ_HANDLED;
2088 }
2089
2090 static irqreturn_t netxen_msix_intr(int irq, void *data)
2091 {
2092 struct nx_host_sds_ring *sds_ring = data;
2093
2094 napi_schedule(&sds_ring->napi);
2095 return IRQ_HANDLED;
2096 }
2097
2098 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2099 {
2100 struct nx_host_sds_ring *sds_ring =
2101 container_of(napi, struct nx_host_sds_ring, napi);
2102
2103 struct netxen_adapter *adapter = sds_ring->adapter;
2104
2105 int tx_complete;
2106 int work_done;
2107
2108 tx_complete = netxen_process_cmd_ring(adapter);
2109
2110 work_done = netxen_process_rcv_ring(sds_ring, budget);
2111
2112 if ((work_done < budget) && tx_complete) {
2113 napi_complete(&sds_ring->napi);
2114 if (test_bit(__NX_DEV_UP, &adapter->state))
2115 netxen_nic_enable_int(sds_ring);
2116 }
2117
2118 return work_done;
2119 }
2120
2121 #ifdef CONFIG_NET_POLL_CONTROLLER
2122 static void netxen_nic_poll_controller(struct net_device *netdev)
2123 {
2124 struct netxen_adapter *adapter = netdev_priv(netdev);
2125 disable_irq(adapter->irq);
2126 netxen_intr(adapter->irq, adapter);
2127 enable_irq(adapter->irq);
2128 }
2129 #endif
2130
2131 static int
2132 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2133 {
2134 int count;
2135 if (netxen_api_lock(adapter))
2136 return -EIO;
2137
2138 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2139
2140 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2141
2142 netxen_api_unlock(adapter);
2143 return count;
2144 }
2145
2146 static int
2147 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2148 {
2149 int count;
2150 if (netxen_api_lock(adapter))
2151 return -EIO;
2152
2153 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2154 WARN_ON(count == 0);
2155
2156 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2157
2158 if (count == 0)
2159 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2160
2161 netxen_api_unlock(adapter);
2162 return count;
2163 }
2164
2165 static int
2166 nx_dev_request_aer(struct netxen_adapter *adapter)
2167 {
2168 u32 state;
2169 int ret = -EINVAL;
2170
2171 if (netxen_api_lock(adapter))
2172 return ret;
2173
2174 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2175
2176 if (state == NX_DEV_NEED_AER)
2177 ret = 0;
2178 else if (state == NX_DEV_READY) {
2179 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2180 ret = 0;
2181 }
2182
2183 netxen_api_unlock(adapter);
2184 return ret;
2185 }
2186
2187 static int
2188 nx_dev_request_reset(struct netxen_adapter *adapter)
2189 {
2190 u32 state;
2191 int ret = -EINVAL;
2192
2193 if (netxen_api_lock(adapter))
2194 return ret;
2195
2196 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2197
2198 if (state == NX_DEV_NEED_RESET)
2199 ret = 0;
2200 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2201 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2202 ret = 0;
2203 }
2204
2205 netxen_api_unlock(adapter);
2206
2207 return ret;
2208 }
2209
2210 static int
2211 netxen_can_start_firmware(struct netxen_adapter *adapter)
2212 {
2213 int count;
2214 int can_start = 0;
2215
2216 if (netxen_api_lock(adapter))
2217 return 0;
2218
2219 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2220
2221 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2222 count = 0;
2223
2224 if (count == 0) {
2225 can_start = 1;
2226 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2227 }
2228
2229 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2230
2231 netxen_api_unlock(adapter);
2232
2233 return can_start;
2234 }
2235
2236 static void
2237 netxen_schedule_work(struct netxen_adapter *adapter,
2238 work_func_t func, int delay)
2239 {
2240 INIT_DELAYED_WORK(&adapter->fw_work, func);
2241 schedule_delayed_work(&adapter->fw_work, delay);
2242 }
2243
2244 static void
2245 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2246 {
2247 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2248 msleep(10);
2249
2250 cancel_delayed_work_sync(&adapter->fw_work);
2251 }
2252
2253 static void
2254 netxen_attach_work(struct work_struct *work)
2255 {
2256 struct netxen_adapter *adapter = container_of(work,
2257 struct netxen_adapter, fw_work.work);
2258 struct net_device *netdev = adapter->netdev;
2259 int err = 0;
2260
2261 if (netif_running(netdev)) {
2262 err = netxen_nic_attach(adapter);
2263 if (err)
2264 goto done;
2265
2266 err = netxen_nic_up(adapter, netdev);
2267 if (err) {
2268 netxen_nic_detach(adapter);
2269 goto done;
2270 }
2271
2272 netxen_config_indev_addr(netdev, NETDEV_UP);
2273 }
2274
2275 netif_device_attach(netdev);
2276
2277 done:
2278 adapter->fw_fail_cnt = 0;
2279 clear_bit(__NX_RESETTING, &adapter->state);
2280 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2281 }
2282
2283 static void
2284 netxen_fwinit_work(struct work_struct *work)
2285 {
2286 struct netxen_adapter *adapter = container_of(work,
2287 struct netxen_adapter, fw_work.work);
2288 int dev_state;
2289
2290 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2291
2292 switch (dev_state) {
2293 case NX_DEV_COLD:
2294 case NX_DEV_READY:
2295 if (!netxen_start_firmware(adapter)) {
2296 netxen_schedule_work(adapter, netxen_attach_work, 0);
2297 return;
2298 }
2299 break;
2300
2301 case NX_DEV_INITALIZING:
2302 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2303 netxen_schedule_work(adapter,
2304 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2305 return;
2306 }
2307
2308 case NX_DEV_FAILED:
2309 default:
2310 nx_incr_dev_ref_cnt(adapter);
2311 break;
2312 }
2313
2314 clear_bit(__NX_RESETTING, &adapter->state);
2315 }
2316
2317 static void
2318 netxen_detach_work(struct work_struct *work)
2319 {
2320 struct netxen_adapter *adapter = container_of(work,
2321 struct netxen_adapter, fw_work.work);
2322 struct net_device *netdev = adapter->netdev;
2323 int ref_cnt, delay;
2324 u32 status;
2325
2326 netif_device_detach(netdev);
2327
2328 netxen_nic_down(adapter, netdev);
2329
2330 netxen_nic_detach(adapter);
2331
2332 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2333
2334 if (status & NX_RCODE_FATAL_ERROR)
2335 goto err_ret;
2336
2337 if (adapter->temp == NX_TEMP_PANIC)
2338 goto err_ret;
2339
2340 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2341
2342 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2343
2344 adapter->fw_wait_cnt = 0;
2345 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2346
2347 return;
2348
2349 err_ret:
2350 clear_bit(__NX_RESETTING, &adapter->state);
2351 }
2352
2353 static int
2354 netxen_check_health(struct netxen_adapter *adapter)
2355 {
2356 u32 state, heartbit;
2357 struct net_device *netdev = adapter->netdev;
2358
2359 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2360 if (state == NX_DEV_NEED_AER)
2361 return 0;
2362
2363 if (netxen_nic_check_temp(adapter))
2364 goto detach;
2365
2366 if (adapter->need_fw_reset) {
2367 if (nx_dev_request_reset(adapter))
2368 return 0;
2369 goto detach;
2370 }
2371
2372 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2373 * 1. Tx timeout 2. Fw hang
2374 * Send request to destroy context in case of tx timeout only
2375 * and doesn't required in case of Fw hang
2376 */
2377 if (state == NX_DEV_NEED_RESET) {
2378 adapter->need_fw_reset = 1;
2379 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2380 goto detach;
2381 }
2382
2383 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2384 return 0;
2385
2386 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2387 if (heartbit != adapter->heartbit) {
2388 adapter->heartbit = heartbit;
2389 adapter->fw_fail_cnt = 0;
2390 if (adapter->need_fw_reset)
2391 goto detach;
2392 return 0;
2393 }
2394
2395 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2396 return 0;
2397
2398 if (nx_dev_request_reset(adapter))
2399 return 0;
2400
2401 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2402
2403 dev_info(&netdev->dev, "firmware hang detected\n");
2404
2405 detach:
2406 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2407 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2408 netxen_schedule_work(adapter, netxen_detach_work, 0);
2409 return 1;
2410 }
2411
2412 static void
2413 netxen_fw_poll_work(struct work_struct *work)
2414 {
2415 struct netxen_adapter *adapter = container_of(work,
2416 struct netxen_adapter, fw_work.work);
2417
2418 if (test_bit(__NX_RESETTING, &adapter->state))
2419 goto reschedule;
2420
2421 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2422 if (!adapter->has_link_events) {
2423
2424 netxen_nic_handle_phy_intr(adapter);
2425
2426 if (adapter->link_changed)
2427 netxen_nic_set_link_parameters(adapter);
2428 }
2429 }
2430
2431 if (netxen_check_health(adapter))
2432 return;
2433
2434 reschedule:
2435 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2436 }
2437
2438 static ssize_t
2439 netxen_store_bridged_mode(struct device *dev,
2440 struct device_attribute *attr, const char *buf, size_t len)
2441 {
2442 struct net_device *net = to_net_dev(dev);
2443 struct netxen_adapter *adapter = netdev_priv(net);
2444 unsigned long new;
2445 int ret = -EINVAL;
2446
2447 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2448 goto err_out;
2449
2450 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2451 goto err_out;
2452
2453 if (strict_strtoul(buf, 2, &new))
2454 goto err_out;
2455
2456 if (!netxen_config_bridged_mode(adapter, !!new))
2457 ret = len;
2458
2459 err_out:
2460 return ret;
2461 }
2462
2463 static ssize_t
2464 netxen_show_bridged_mode(struct device *dev,
2465 struct device_attribute *attr, char *buf)
2466 {
2467 struct net_device *net = to_net_dev(dev);
2468 struct netxen_adapter *adapter;
2469 int bridged_mode = 0;
2470
2471 adapter = netdev_priv(net);
2472
2473 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2474 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2475
2476 return sprintf(buf, "%d\n", bridged_mode);
2477 }
2478
2479 static struct device_attribute dev_attr_bridged_mode = {
2480 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2481 .show = netxen_show_bridged_mode,
2482 .store = netxen_store_bridged_mode,
2483 };
2484
2485 static ssize_t
2486 netxen_store_diag_mode(struct device *dev,
2487 struct device_attribute *attr, const char *buf, size_t len)
2488 {
2489 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2490 unsigned long new;
2491
2492 if (strict_strtoul(buf, 2, &new))
2493 return -EINVAL;
2494
2495 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2496 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2497
2498 return len;
2499 }
2500
2501 static ssize_t
2502 netxen_show_diag_mode(struct device *dev,
2503 struct device_attribute *attr, char *buf)
2504 {
2505 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2506
2507 return sprintf(buf, "%d\n",
2508 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2509 }
2510
2511 static struct device_attribute dev_attr_diag_mode = {
2512 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2513 .show = netxen_show_diag_mode,
2514 .store = netxen_store_diag_mode,
2515 };
2516
2517 static int
2518 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2519 loff_t offset, size_t size)
2520 {
2521 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2522 return -EIO;
2523
2524 if ((size != 4) || (offset & 0x3))
2525 return -EINVAL;
2526
2527 if (offset < NETXEN_PCI_CRBSPACE)
2528 return -EINVAL;
2529
2530 return 0;
2531 }
2532
2533 static ssize_t
2534 netxen_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2535 char *buf, loff_t offset, size_t size)
2536 {
2537 struct device *dev = container_of(kobj, struct device, kobj);
2538 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2539 u32 data;
2540 int ret;
2541
2542 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2543 if (ret != 0)
2544 return ret;
2545
2546 data = NXRD32(adapter, offset);
2547 memcpy(buf, &data, size);
2548 return size;
2549 }
2550
2551 static ssize_t
2552 netxen_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2553 char *buf, loff_t offset, size_t size)
2554 {
2555 struct device *dev = container_of(kobj, struct device, kobj);
2556 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2557 u32 data;
2558 int ret;
2559
2560 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2561 if (ret != 0)
2562 return ret;
2563
2564 memcpy(&data, buf, size);
2565 NXWR32(adapter, offset, data);
2566 return size;
2567 }
2568
2569 static int
2570 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2571 loff_t offset, size_t size)
2572 {
2573 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2574 return -EIO;
2575
2576 if ((size != 8) || (offset & 0x7))
2577 return -EIO;
2578
2579 return 0;
2580 }
2581
2582 static ssize_t
2583 netxen_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2584 char *buf, loff_t offset, size_t size)
2585 {
2586 struct device *dev = container_of(kobj, struct device, kobj);
2587 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2588 u64 data;
2589 int ret;
2590
2591 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2592 if (ret != 0)
2593 return ret;
2594
2595 if (adapter->pci_mem_read(adapter, offset, &data))
2596 return -EIO;
2597
2598 memcpy(buf, &data, size);
2599
2600 return size;
2601 }
2602
2603 ssize_t netxen_sysfs_write_mem(struct kobject *kobj,
2604 struct bin_attribute *attr, char *buf,
2605 loff_t offset, size_t size)
2606 {
2607 struct device *dev = container_of(kobj, struct device, kobj);
2608 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2609 u64 data;
2610 int ret;
2611
2612 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2613 if (ret != 0)
2614 return ret;
2615
2616 memcpy(&data, buf, size);
2617
2618 if (adapter->pci_mem_write(adapter, offset, data))
2619 return -EIO;
2620
2621 return size;
2622 }
2623
2624
2625 static struct bin_attribute bin_attr_crb = {
2626 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2627 .size = 0,
2628 .read = netxen_sysfs_read_crb,
2629 .write = netxen_sysfs_write_crb,
2630 };
2631
2632 static struct bin_attribute bin_attr_mem = {
2633 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2634 .size = 0,
2635 .read = netxen_sysfs_read_mem,
2636 .write = netxen_sysfs_write_mem,
2637 };
2638
2639
2640 static void
2641 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2642 {
2643 struct net_device *netdev = adapter->netdev;
2644 struct device *dev = &netdev->dev;
2645
2646 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2647 /* bridged_mode control */
2648 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2649 dev_warn(&netdev->dev,
2650 "failed to create bridged_mode sysfs entry\n");
2651 }
2652 }
2653 }
2654
2655 static void
2656 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2657 {
2658 struct net_device *netdev = adapter->netdev;
2659 struct device *dev = &netdev->dev;
2660
2661 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2662 device_remove_file(dev, &dev_attr_bridged_mode);
2663 }
2664
2665 static void
2666 netxen_create_diag_entries(struct netxen_adapter *adapter)
2667 {
2668 struct pci_dev *pdev = adapter->pdev;
2669 struct device *dev;
2670
2671 dev = &pdev->dev;
2672 if (device_create_file(dev, &dev_attr_diag_mode))
2673 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2674 if (device_create_bin_file(dev, &bin_attr_crb))
2675 dev_info(dev, "failed to create crb sysfs entry\n");
2676 if (device_create_bin_file(dev, &bin_attr_mem))
2677 dev_info(dev, "failed to create mem sysfs entry\n");
2678 }
2679
2680
2681 static void
2682 netxen_remove_diag_entries(struct netxen_adapter *adapter)
2683 {
2684 struct pci_dev *pdev = adapter->pdev;
2685 struct device *dev = &pdev->dev;
2686
2687 device_remove_file(dev, &dev_attr_diag_mode);
2688 device_remove_bin_file(dev, &bin_attr_crb);
2689 device_remove_bin_file(dev, &bin_attr_mem);
2690 }
2691
2692 #ifdef CONFIG_INET
2693
2694 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2695
2696 static int
2697 netxen_destip_supported(struct netxen_adapter *adapter)
2698 {
2699 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2700 return 0;
2701
2702 if (adapter->ahw.cut_through)
2703 return 0;
2704
2705 return 1;
2706 }
2707
2708 static void
2709 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2710 {
2711 struct in_device *indev;
2712 struct netxen_adapter *adapter = netdev_priv(dev);
2713
2714 if (!netxen_destip_supported(adapter))
2715 return;
2716
2717 indev = in_dev_get(dev);
2718 if (!indev)
2719 return;
2720
2721 for_ifa(indev) {
2722 switch (event) {
2723 case NETDEV_UP:
2724 netxen_config_ipaddr(adapter,
2725 ifa->ifa_address, NX_IP_UP);
2726 break;
2727 case NETDEV_DOWN:
2728 netxen_config_ipaddr(adapter,
2729 ifa->ifa_address, NX_IP_DOWN);
2730 break;
2731 default:
2732 break;
2733 }
2734 } endfor_ifa(indev);
2735
2736 in_dev_put(indev);
2737 return;
2738 }
2739
2740 static int netxen_netdev_event(struct notifier_block *this,
2741 unsigned long event, void *ptr)
2742 {
2743 struct netxen_adapter *adapter;
2744 struct net_device *dev = (struct net_device *)ptr;
2745
2746 recheck:
2747 if (dev == NULL)
2748 goto done;
2749
2750 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2751 dev = vlan_dev_real_dev(dev);
2752 goto recheck;
2753 }
2754
2755 if (!is_netxen_netdev(dev))
2756 goto done;
2757
2758 adapter = netdev_priv(dev);
2759
2760 if (!adapter)
2761 goto done;
2762
2763 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2764 goto done;
2765
2766 netxen_config_indev_addr(dev, event);
2767 done:
2768 return NOTIFY_DONE;
2769 }
2770
2771 static int
2772 netxen_inetaddr_event(struct notifier_block *this,
2773 unsigned long event, void *ptr)
2774 {
2775 struct netxen_adapter *adapter;
2776 struct net_device *dev;
2777
2778 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2779
2780 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2781
2782 recheck:
2783 if (dev == NULL || !netif_running(dev))
2784 goto done;
2785
2786 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2787 dev = vlan_dev_real_dev(dev);
2788 goto recheck;
2789 }
2790
2791 if (!is_netxen_netdev(dev))
2792 goto done;
2793
2794 adapter = netdev_priv(dev);
2795
2796 if (!adapter || !netxen_destip_supported(adapter))
2797 goto done;
2798
2799 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2800 goto done;
2801
2802 switch (event) {
2803 case NETDEV_UP:
2804 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2805 break;
2806 case NETDEV_DOWN:
2807 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2808 break;
2809 default:
2810 break;
2811 }
2812
2813 done:
2814 return NOTIFY_DONE;
2815 }
2816
2817 static struct notifier_block netxen_netdev_cb = {
2818 .notifier_call = netxen_netdev_event,
2819 };
2820
2821 static struct notifier_block netxen_inetaddr_cb = {
2822 .notifier_call = netxen_inetaddr_event,
2823 };
2824 #else
2825 static void
2826 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2827 { }
2828 #endif
2829
2830 static struct pci_error_handlers netxen_err_handler = {
2831 .error_detected = netxen_io_error_detected,
2832 .slot_reset = netxen_io_slot_reset,
2833 .resume = netxen_io_resume,
2834 };
2835
2836 static struct pci_driver netxen_driver = {
2837 .name = netxen_nic_driver_name,
2838 .id_table = netxen_pci_tbl,
2839 .probe = netxen_nic_probe,
2840 .remove = __devexit_p(netxen_nic_remove),
2841 #ifdef CONFIG_PM
2842 .suspend = netxen_nic_suspend,
2843 .resume = netxen_nic_resume,
2844 #endif
2845 .shutdown = netxen_nic_shutdown,
2846 .err_handler = &netxen_err_handler
2847 };
2848
2849 static int __init netxen_init_module(void)
2850 {
2851 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2852
2853 #ifdef CONFIG_INET
2854 register_netdevice_notifier(&netxen_netdev_cb);
2855 register_inetaddr_notifier(&netxen_inetaddr_cb);
2856 #endif
2857 return pci_register_driver(&netxen_driver);
2858 }
2859
2860 module_init(netxen_init_module);
2861
2862 static void __exit netxen_exit_module(void)
2863 {
2864 pci_unregister_driver(&netxen_driver);
2865
2866 #ifdef CONFIG_INET
2867 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2868 unregister_netdevice_notifier(&netxen_netdev_cb);
2869 #endif
2870 }
2871
2872 module_exit(netxen_exit_module);
This page took 0.126935 seconds and 5 git commands to generate.