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