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