netxen: add new board types
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / netxen / netxen_nic_main.c
blob153b391917e4369ce0f7c3a5c0dbc7b7dc88ef75
1 /*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
30 * Main source file for NetXen NIC Driver on Linux
34 #include <linux/vmalloc.h>
35 #include <linux/highmem.h>
36 #include "netxen_nic_hw.h"
38 #include "netxen_nic.h"
39 #include "netxen_nic_phan_reg.h"
41 #include <linux/dma-mapping.h>
42 #include <net/ip.h>
44 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
50 NETXEN_NIC_LINUX_VERSIONID;
52 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
54 /* Default to restricted 1G auto-neg mode */
55 static int wol_port_mode = 5;
57 static int use_msi = 1;
59 static int use_msi_x = 1;
61 /* Local functions to NetXen NIC driver */
62 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
63 const struct pci_device_id *ent);
64 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
65 static int netxen_nic_open(struct net_device *netdev);
66 static int netxen_nic_close(struct net_device *netdev);
67 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
68 static void netxen_tx_timeout(struct net_device *netdev);
69 static void netxen_tx_timeout_task(struct work_struct *work);
70 static void netxen_watchdog(unsigned long);
71 static int netxen_nic_poll(struct napi_struct *napi, int budget);
72 #ifdef CONFIG_NET_POLL_CONTROLLER
73 static void netxen_nic_poll_controller(struct net_device *netdev);
74 #endif
75 static irqreturn_t netxen_intr(int irq, void *data);
76 static irqreturn_t netxen_msi_intr(int irq, void *data);
78 /* PCI Device ID Table */
79 #define ENTRY(device) \
80 {PCI_DEVICE(0x4040, (device)), \
81 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
83 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
84 ENTRY(0x0001),
85 ENTRY(0x0002),
86 ENTRY(0x0003),
87 ENTRY(0x0004),
88 ENTRY(0x0005),
89 ENTRY(0x0024),
90 ENTRY(0x0025),
91 ENTRY(0x0100),
92 {0,}
95 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
98 * In netxen_nic_down(), we must wait for any pending callback requests into
99 * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be
100 * reenabled right after it is deleted in netxen_nic_down().
101 * FLUSH_SCHEDULED_WORK() does this synchronization.
103 * Normally, schedule_work()/flush_scheduled_work() could have worked, but
104 * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off()
105 * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a
106 * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause
107 * linkwatch_event() to be executed which also attempts to acquire the rtnl
108 * lock thus causing a deadlock.
111 static struct workqueue_struct *netxen_workq;
112 #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
113 #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
115 static void netxen_watchdog(unsigned long);
117 static uint32_t crb_cmd_producer[4] = {
118 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
119 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
122 void
123 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
124 uint32_t crb_producer)
126 adapter->pci_write_normalize(adapter,
127 adapter->crb_addr_cmd_producer, crb_producer);
130 static uint32_t crb_cmd_consumer[4] = {
131 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
132 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
135 static inline void
136 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
137 u32 crb_consumer)
139 adapter->pci_write_normalize(adapter,
140 adapter->crb_addr_cmd_consumer, crb_consumer);
143 static uint32_t msi_tgt_status[8] = {
144 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
145 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
146 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
147 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
150 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
152 static void netxen_nic_disable_int(struct netxen_adapter *adapter)
154 u32 mask = 0x7ff;
155 int retries = 32;
156 int pci_fn = adapter->ahw.pci_func;
158 if (adapter->msi_mode != MSI_MODE_MULTIFUNC)
159 adapter->pci_write_normalize(adapter,
160 adapter->crb_intr_mask, 0);
162 if (adapter->intr_scheme != -1 &&
163 adapter->intr_scheme != INTR_SCHEME_PERPORT)
164 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
166 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
167 do {
168 adapter->pci_write_immediate(adapter,
169 ISR_INT_TARGET_STATUS, 0xffffffff);
170 mask = adapter->pci_read_immediate(adapter,
171 ISR_INT_VECTOR);
172 if (!(mask & 0x80))
173 break;
174 udelay(10);
175 } while (--retries);
177 if (!retries) {
178 printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n",
179 netxen_nic_driver_name);
181 } else {
182 if (adapter->msi_mode == MSI_MODE_MULTIFUNC) {
183 adapter->pci_write_immediate(adapter,
184 msi_tgt_status[pci_fn], 0xffffffff);
189 static void netxen_nic_enable_int(struct netxen_adapter *adapter)
191 u32 mask;
193 DPRINTK(1, INFO, "Entered ISR Enable \n");
195 if (adapter->intr_scheme != -1 &&
196 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
197 switch (adapter->ahw.board_type) {
198 case NETXEN_NIC_GBE:
199 mask = 0x77b;
200 break;
201 case NETXEN_NIC_XGBE:
202 mask = 0x77f;
203 break;
204 default:
205 mask = 0x7ff;
206 break;
209 adapter->pci_write_immediate(adapter, ISR_INT_MASK, mask);
212 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
214 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
215 mask = 0xbff;
216 if (adapter->intr_scheme != -1 &&
217 adapter->intr_scheme != INTR_SCHEME_PERPORT) {
218 adapter->pci_write_normalize(adapter,
219 CRB_INT_VECTOR, 0);
221 adapter->pci_write_immediate(adapter,
222 ISR_INT_TARGET_MASK, mask);
225 DPRINTK(1, INFO, "Done with enable Int\n");
228 static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
230 struct pci_dev *pdev = adapter->pdev;
231 int err;
232 uint64_t mask;
234 #ifdef CONFIG_IA64
235 adapter->dma_mask = DMA_32BIT_MASK;
236 #else
237 if (revision_id >= NX_P3_B0) {
238 /* should go to DMA_64BIT_MASK */
239 adapter->dma_mask = DMA_39BIT_MASK;
240 mask = DMA_39BIT_MASK;
241 } else if (revision_id == NX_P3_A2) {
242 adapter->dma_mask = DMA_39BIT_MASK;
243 mask = DMA_39BIT_MASK;
244 } else if (revision_id == NX_P2_C1) {
245 adapter->dma_mask = DMA_35BIT_MASK;
246 mask = DMA_35BIT_MASK;
247 } else {
248 adapter->dma_mask = DMA_32BIT_MASK;
249 mask = DMA_32BIT_MASK;
250 goto set_32_bit_mask;
254 * Consistent DMA mask is set to 32 bit because it cannot be set to
255 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
256 * come off this pool.
258 if (pci_set_dma_mask(pdev, mask) == 0 &&
259 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) == 0) {
260 adapter->pci_using_dac = 1;
261 return 0;
263 #endif /* CONFIG_IA64 */
265 set_32_bit_mask:
266 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
267 if (!err)
268 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
269 if (err) {
270 DPRINTK(ERR, "No usable DMA configuration, aborting:%d\n", err);
271 return err;
274 adapter->pci_using_dac = 0;
275 return 0;
278 static void netxen_check_options(struct netxen_adapter *adapter)
280 switch (adapter->ahw.boardcfg.board_type) {
281 case NETXEN_BRDTYPE_P3_HMEZ:
282 case NETXEN_BRDTYPE_P3_XG_LOM:
283 case NETXEN_BRDTYPE_P3_10G_CX4:
284 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
285 case NETXEN_BRDTYPE_P3_IMEZ:
286 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
287 case NETXEN_BRDTYPE_P3_10G_SFP_QT:
288 case NETXEN_BRDTYPE_P3_10G_SFP_CT:
289 case NETXEN_BRDTYPE_P3_10G_XFP:
290 case NETXEN_BRDTYPE_P3_10000_BASE_T:
291 adapter->msix_supported = !!use_msi_x;
292 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
293 break;
295 case NETXEN_BRDTYPE_P2_SB31_10G:
296 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
297 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
298 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
299 adapter->msix_supported = 0;
300 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
301 break;
303 case NETXEN_BRDTYPE_P3_REF_QG:
304 case NETXEN_BRDTYPE_P3_4_GB:
305 case NETXEN_BRDTYPE_P3_4_GB_MM:
306 adapter->msix_supported = 0;
307 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
308 break;
310 case NETXEN_BRDTYPE_P2_SB35_4G:
311 case NETXEN_BRDTYPE_P2_SB31_2G:
312 adapter->msix_supported = 0;
313 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
314 break;
316 default:
317 adapter->msix_supported = 0;
318 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
320 printk(KERN_WARNING "Unknown board type(0x%x)\n",
321 adapter->ahw.boardcfg.board_type);
322 break;
325 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
326 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
327 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
329 adapter->max_possible_rss_rings = 1;
330 return;
333 static int
334 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
336 int ret = 0;
338 if (first_boot == 0x55555555) {
339 /* This is the first boot after power up */
341 /* PCI bus master workaround */
342 adapter->hw_read_wx(adapter,
343 NETXEN_PCIE_REG(0x4), &first_boot, 4);
344 if (!(first_boot & 0x4)) {
345 first_boot |= 0x4;
346 adapter->hw_write_wx(adapter,
347 NETXEN_PCIE_REG(0x4), &first_boot, 4);
348 adapter->hw_read_wx(adapter,
349 NETXEN_PCIE_REG(0x4), &first_boot, 4);
352 /* This is the first boot after power up */
353 adapter->hw_read_wx(adapter,
354 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
355 if (first_boot != 0x80000f) {
356 /* clear the register for future unloads/loads */
357 adapter->pci_write_normalize(adapter,
358 NETXEN_CAM_RAM(0x1fc), 0);
359 ret = -1;
362 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
363 /* Start P2 boot loader */
364 adapter->pci_write_normalize(adapter,
365 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
366 adapter->pci_write_normalize(adapter,
367 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
370 return ret;
373 static void netxen_set_port_mode(struct netxen_adapter *adapter)
375 u32 val, data;
377 val = adapter->ahw.boardcfg.board_type;
378 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
379 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
380 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
381 data = NETXEN_PORT_MODE_802_3_AP;
382 adapter->hw_write_wx(adapter,
383 NETXEN_PORT_MODE_ADDR, &data, 4);
384 } else if (port_mode == NETXEN_PORT_MODE_XG) {
385 data = NETXEN_PORT_MODE_XG;
386 adapter->hw_write_wx(adapter,
387 NETXEN_PORT_MODE_ADDR, &data, 4);
388 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
389 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
390 adapter->hw_write_wx(adapter,
391 NETXEN_PORT_MODE_ADDR, &data, 4);
392 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
393 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
394 adapter->hw_write_wx(adapter,
395 NETXEN_PORT_MODE_ADDR, &data, 4);
396 } else {
397 data = NETXEN_PORT_MODE_AUTO_NEG;
398 adapter->hw_write_wx(adapter,
399 NETXEN_PORT_MODE_ADDR, &data, 4);
402 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
403 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
404 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
405 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
406 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
408 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
409 &wol_port_mode, 4);
413 #define PCI_CAP_ID_GEN 0x10
415 static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
417 u32 pdevfuncsave;
418 u32 c8c9value = 0;
419 u32 chicken = 0;
420 u32 control = 0;
421 int i, pos;
422 struct pci_dev *pdev;
424 pdev = pci_get_device(0x1166, 0x0140, NULL);
425 if (pdev) {
426 pci_dev_put(pdev);
427 adapter->hw_read_wx(adapter,
428 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
429 chicken |= 0x4000;
430 adapter->hw_write_wx(adapter,
431 NETXEN_PCIE_REG(PCIE_TGT_SPLIT_CHICKEN), &chicken, 4);
434 pdev = adapter->pdev;
436 adapter->hw_read_wx(adapter,
437 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
438 /* clear chicken3.25:24 */
439 chicken &= 0xFCFFFFFF;
441 * if gen1 and B0, set F1020 - if gen 2, do nothing
442 * if gen2 set to F1000
444 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
445 if (pos == 0xC0) {
446 pci_read_config_dword(pdev, pos + 0x10, &control);
447 if ((control & 0x000F0000) != 0x00020000) {
448 /* set chicken3.24 if gen1 */
449 chicken |= 0x01000000;
451 printk(KERN_INFO "%s Gen2 strapping detected\n",
452 netxen_nic_driver_name);
453 c8c9value = 0xF1000;
454 } else {
455 /* set chicken3.24 if gen1 */
456 chicken |= 0x01000000;
457 printk(KERN_INFO "%s Gen1 strapping detected\n",
458 netxen_nic_driver_name);
459 if (adapter->ahw.revision_id == NX_P3_B0)
460 c8c9value = 0xF1020;
461 else
462 c8c9value = 0;
465 adapter->hw_write_wx(adapter,
466 NETXEN_PCIE_REG(PCIE_CHICKEN3), &chicken, 4);
468 if (!c8c9value)
469 return;
471 pdevfuncsave = pdev->devfn;
472 if (pdevfuncsave & 0x07)
473 return;
475 for (i = 0; i < 8; i++) {
476 pci_read_config_dword(pdev, pos + 8, &control);
477 pci_read_config_dword(pdev, pos + 8, &control);
478 pci_write_config_dword(pdev, pos + 8, c8c9value);
479 pdev->devfn++;
481 pdev->devfn = pdevfuncsave;
484 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
486 u32 control;
487 int pos;
489 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
490 if (pos) {
491 pci_read_config_dword(pdev, pos, &control);
492 if (enable)
493 control |= PCI_MSIX_FLAGS_ENABLE;
494 else
495 control = 0;
496 pci_write_config_dword(pdev, pos, control);
500 static void netxen_init_msix_entries(struct netxen_adapter *adapter)
502 int i;
504 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
505 adapter->msix_entries[i].entry = i;
509 * netxen_nic_probe()
511 * The Linux system will invoke this after identifying the vendor ID and
512 * device Id in the pci_tbl supported by this module.
514 * A quad port card has one operational PCI config space, (function 0),
515 * which is used to access all four ports.
517 * This routine will initialize the adapter, and setup the global parameters
518 * along with the port's specific structure.
520 static int __devinit
521 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
523 struct net_device *netdev = NULL;
524 struct netxen_adapter *adapter = NULL;
525 void __iomem *mem_ptr0 = NULL;
526 void __iomem *mem_ptr1 = NULL;
527 void __iomem *mem_ptr2 = NULL;
528 unsigned long first_page_group_end;
529 unsigned long first_page_group_start;
532 u8 __iomem *db_ptr = NULL;
533 unsigned long mem_base, mem_len, db_base, db_len, pci_len0 = 0;
534 int i = 0, err;
535 int first_driver, first_boot;
536 __le64 mac_addr[FLASH_NUM_PORTS + 1];
537 u32 val;
538 int pci_func_id = PCI_FUNC(pdev->devfn);
539 DECLARE_MAC_BUF(mac);
540 struct netxen_legacy_intr_set *legacy_intrp;
541 uint8_t revision_id;
543 if (pci_func_id == 0)
544 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
546 if (pdev->class != 0x020000) {
547 printk(KERN_DEBUG "NetXen function %d, class %x will not "
548 "be enabled.\n",pci_func_id, pdev->class);
549 return -ENODEV;
552 if ((err = pci_enable_device(pdev)))
553 return err;
555 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
556 err = -ENODEV;
557 goto err_out_disable_pdev;
560 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
561 goto err_out_disable_pdev;
563 pci_set_master(pdev);
565 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
566 if(!netdev) {
567 printk(KERN_ERR"%s: Failed to allocate memory for the "
568 "device block.Check system memory resource"
569 " usage.\n", netxen_nic_driver_name);
570 goto err_out_free_res;
573 SET_NETDEV_DEV(netdev, &pdev->dev);
575 adapter = netdev->priv;
576 adapter->netdev = netdev;
577 adapter->pdev = pdev;
578 adapter->ahw.pci_func = pci_func_id;
580 revision_id = pdev->revision;
581 adapter->ahw.revision_id = revision_id;
583 err = nx_set_dma_mask(adapter, revision_id);
584 if (err)
585 goto err_out_free_netdev;
587 rwlock_init(&adapter->adapter_lock);
588 adapter->ahw.qdr_sn_window = -1;
589 adapter->ahw.ddr_mn_window = -1;
591 /* remap phys address */
592 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
593 mem_len = pci_resource_len(pdev, 0);
594 pci_len0 = 0;
596 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
597 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
598 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
599 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
600 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
601 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
602 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
603 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
604 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
606 /* 128 Meg of memory */
607 if (mem_len == NETXEN_PCI_128MB_SIZE) {
608 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
609 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
610 SECOND_PAGE_GROUP_SIZE);
611 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
612 THIRD_PAGE_GROUP_SIZE);
613 first_page_group_start = FIRST_PAGE_GROUP_START;
614 first_page_group_end = FIRST_PAGE_GROUP_END;
615 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
616 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
617 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
618 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
619 first_page_group_start = 0;
620 first_page_group_end = 0;
621 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
622 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
623 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
624 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
625 adapter->pci_write_immediate =
626 netxen_nic_pci_write_immediate_2M;
627 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
628 adapter->pci_write_normalize =
629 netxen_nic_pci_write_normalize_2M;
630 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
631 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
632 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
634 mem_ptr0 = ioremap(mem_base, mem_len);
635 pci_len0 = mem_len;
636 first_page_group_start = 0;
637 first_page_group_end = 0;
639 adapter->ahw.ddr_mn_window = 0;
640 adapter->ahw.qdr_sn_window = 0;
642 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
643 (pci_func_id * 0x20);
644 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
645 if (pci_func_id < 4)
646 adapter->ahw.ms_win_crb += (pci_func_id * 0x20);
647 else
648 adapter->ahw.ms_win_crb +=
649 0xA0 + ((pci_func_id - 4) * 0x10);
650 } else {
651 err = -EIO;
652 goto err_out_free_netdev;
655 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
657 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
658 db_len = pci_resource_len(pdev, 4);
660 if (db_len == 0) {
661 printk(KERN_ERR "%s: doorbell is disabled\n",
662 netxen_nic_driver_name);
663 err = -EIO;
664 goto err_out_iounmap;
666 DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base,
667 db_len);
669 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
670 if (!db_ptr) {
671 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
672 netxen_nic_driver_name);
673 err = -EIO;
674 goto err_out_iounmap;
676 DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr);
678 adapter->ahw.pci_base0 = mem_ptr0;
679 adapter->ahw.pci_len0 = pci_len0;
680 adapter->ahw.first_page_group_start = first_page_group_start;
681 adapter->ahw.first_page_group_end = first_page_group_end;
682 adapter->ahw.pci_base1 = mem_ptr1;
683 adapter->ahw.pci_base2 = mem_ptr2;
684 adapter->ahw.db_base = db_ptr;
685 adapter->ahw.db_len = db_len;
687 netif_napi_add(netdev, &adapter->napi,
688 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
690 if (revision_id >= NX_P3_B0)
691 legacy_intrp = &legacy_intr[pci_func_id];
692 else
693 legacy_intrp = &legacy_intr[0];
695 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
696 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
697 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
698 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
700 /* this will be read from FW later */
701 adapter->intr_scheme = -1;
702 adapter->msi_mode = -1;
704 /* This will be reset for mezz cards */
705 adapter->portnum = pci_func_id;
706 adapter->status &= ~NETXEN_NETDEV_STATUS;
707 adapter->rx_csum = 1;
708 adapter->mc_enabled = 0;
709 if (NX_IS_REVISION_P3(revision_id)) {
710 adapter->max_mc_count = 38;
711 adapter->max_rds_rings = 2;
712 } else {
713 adapter->max_mc_count = 16;
714 adapter->max_rds_rings = 3;
717 netdev->open = netxen_nic_open;
718 netdev->stop = netxen_nic_close;
719 netdev->hard_start_xmit = netxen_nic_xmit_frame;
720 netdev->get_stats = netxen_nic_get_stats;
721 if (NX_IS_REVISION_P3(revision_id))
722 netdev->set_multicast_list = netxen_p3_nic_set_multi;
723 else
724 netdev->set_multicast_list = netxen_p2_nic_set_multi;
725 netdev->set_mac_address = netxen_nic_set_mac;
726 netdev->change_mtu = netxen_nic_change_mtu;
727 netdev->tx_timeout = netxen_tx_timeout;
728 netdev->watchdog_timeo = 2*HZ;
730 netxen_nic_change_mtu(netdev, netdev->mtu);
732 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
733 #ifdef CONFIG_NET_POLL_CONTROLLER
734 netdev->poll_controller = netxen_nic_poll_controller;
735 #endif
736 /* ScatterGather support */
737 netdev->features = NETIF_F_SG;
738 netdev->features |= NETIF_F_IP_CSUM;
739 netdev->features |= NETIF_F_TSO;
740 if (NX_IS_REVISION_P3(revision_id)) {
741 netdev->features |= NETIF_F_IPV6_CSUM;
742 netdev->features |= NETIF_F_TSO6;
745 if (adapter->pci_using_dac)
746 netdev->features |= NETIF_F_HIGHDMA;
749 * Set the CRB window to invalid. If any register in window 0 is
750 * accessed it should set the window to 0 and then reset it to 1.
752 adapter->curr_window = 255;
754 if (netxen_nic_get_board_info(adapter) != 0) {
755 printk("%s: Error getting board config info.\n",
756 netxen_nic_driver_name);
757 err = -EIO;
758 goto err_out_iounmap;
761 netxen_initialize_adapter_ops(adapter);
763 /* Mezz cards have PCI function 0,2,3 enabled */
764 switch (adapter->ahw.boardcfg.board_type) {
765 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
766 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
767 if (pci_func_id >= 2)
768 adapter->portnum = pci_func_id - 2;
769 break;
770 default:
771 break;
775 * This call will setup various max rx/tx counts.
776 * It must be done before any buffer/ring allocations.
778 netxen_check_options(adapter);
780 first_driver = 0;
781 if (NX_IS_REVISION_P3(revision_id)) {
782 if (adapter->ahw.pci_func == 0)
783 first_driver = 1;
784 } else {
785 if (adapter->portnum == 0)
786 first_driver = 1;
788 adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum];
789 adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum];
790 netxen_nic_update_cmd_producer(adapter, 0);
791 netxen_nic_update_cmd_consumer(adapter, 0);
793 if (first_driver) {
794 first_boot = adapter->pci_read_normalize(adapter,
795 NETXEN_CAM_RAM(0x1fc));
797 err = netxen_check_hw_init(adapter, first_boot);
798 if (err) {
799 printk(KERN_ERR "%s: error in init HW init sequence\n",
800 netxen_nic_driver_name);
801 goto err_out_iounmap;
804 if (NX_IS_REVISION_P3(revision_id))
805 netxen_set_port_mode(adapter);
807 if (first_boot != 0x55555555) {
808 adapter->pci_write_normalize(adapter,
809 CRB_CMDPEG_STATE, 0);
810 netxen_pinit_from_rom(adapter, 0);
811 msleep(1);
812 netxen_load_firmware(adapter);
815 if (NX_IS_REVISION_P3(revision_id))
816 netxen_pcie_strap_init(adapter);
818 if (NX_IS_REVISION_P2(revision_id)) {
820 /* Initialize multicast addr pool owners */
821 val = 0x7654;
822 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
823 val |= 0x0f000000;
824 netxen_crb_writelit_adapter(adapter,
825 NETXEN_MAC_ADDR_CNTL_REG, val);
829 if ((first_boot == 0x55555555) &&
830 (NX_IS_REVISION_P2(revision_id))) {
831 /* Unlock the HW, prompting the boot sequence */
832 adapter->pci_write_normalize(adapter,
833 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, 1);
836 err = netxen_initialize_adapter_offload(adapter);
837 if (err)
838 goto err_out_iounmap;
841 * Tell the hardware our version number.
843 i = (_NETXEN_NIC_LINUX_MAJOR << 16)
844 | ((_NETXEN_NIC_LINUX_MINOR << 8))
845 | (_NETXEN_NIC_LINUX_SUBVERSION);
846 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, i);
848 /* Handshake with the card before we register the devices. */
849 netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
851 } /* first_driver */
853 netxen_nic_flash_print(adapter);
855 if (NX_IS_REVISION_P3(revision_id)) {
856 adapter->hw_read_wx(adapter,
857 NETXEN_MIU_MN_CONTROL, &val, 4);
858 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
859 dev_info(&pdev->dev, "firmware running in %s mode\n",
860 adapter->ahw.cut_through ? "cut through" : "legacy");
864 * See if the firmware gave us a virtual-physical port mapping.
866 adapter->physical_port = adapter->portnum;
867 i = adapter->pci_read_normalize(adapter, CRB_V2P(adapter->portnum));
868 if (i != 0x55555555)
869 adapter->physical_port = i;
871 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
873 netxen_set_msix_bit(pdev, 0);
875 if (NX_IS_REVISION_P3(revision_id)) {
876 if ((mem_len != NETXEN_PCI_128MB_SIZE) &&
877 mem_len != NETXEN_PCI_2MB_SIZE)
878 adapter->msix_supported = 0;
881 if (adapter->msix_supported) {
883 netxen_init_msix_entries(adapter);
885 if (pci_enable_msix(pdev, adapter->msix_entries,
886 MSIX_ENTRIES_PER_ADAPTER))
887 goto request_msi;
889 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
890 netxen_set_msix_bit(pdev, 1);
891 dev_info(&pdev->dev, "using msi-x interrupts\n");
893 } else {
894 request_msi:
895 if (use_msi && !pci_enable_msi(pdev)) {
896 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
897 dev_info(&pdev->dev, "using msi interrupts\n");
898 } else
899 dev_info(&pdev->dev, "using legacy interrupts\n");
902 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
903 netdev->irq = adapter->msix_entries[0].vector;
904 else
905 netdev->irq = pdev->irq;
907 err = netxen_receive_peg_ready(adapter);
908 if (err)
909 goto err_out_disable_msi;
911 init_timer(&adapter->watchdog_timer);
912 adapter->ahw.linkup = 0;
913 adapter->watchdog_timer.function = &netxen_watchdog;
914 adapter->watchdog_timer.data = (unsigned long)adapter;
915 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
916 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
918 if (netxen_is_flash_supported(adapter) == 0 &&
919 netxen_get_flash_mac_addr(adapter, mac_addr) == 0) {
920 unsigned char *p;
922 p = (unsigned char *)&mac_addr[adapter->portnum];
923 netdev->dev_addr[0] = *(p + 5);
924 netdev->dev_addr[1] = *(p + 4);
925 netdev->dev_addr[2] = *(p + 3);
926 netdev->dev_addr[3] = *(p + 2);
927 netdev->dev_addr[4] = *(p + 1);
928 netdev->dev_addr[5] = *(p + 0);
930 memcpy(netdev->perm_addr, netdev->dev_addr,
931 netdev->addr_len);
932 if (!is_valid_ether_addr(netdev->perm_addr)) {
933 printk(KERN_ERR "%s: Bad MAC address %s.\n",
934 netxen_nic_driver_name,
935 print_mac(mac, netdev->dev_addr));
936 } else {
937 adapter->macaddr_set(adapter, netdev->dev_addr);
941 netif_carrier_off(netdev);
942 netif_stop_queue(netdev);
944 if ((err = register_netdev(netdev))) {
945 printk(KERN_ERR "%s: register_netdev failed port #%d"
946 " aborting\n", netxen_nic_driver_name,
947 adapter->portnum);
948 err = -EIO;
949 goto err_out_disable_msi;
952 pci_set_drvdata(pdev, adapter);
954 switch (adapter->ahw.board_type) {
955 case NETXEN_NIC_GBE:
956 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
957 adapter->netdev->name);
958 break;
959 case NETXEN_NIC_XGBE:
960 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
961 adapter->netdev->name);
962 break;
965 return 0;
967 err_out_disable_msi:
968 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
969 pci_disable_msix(pdev);
970 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
971 pci_disable_msi(pdev);
973 if (first_driver)
974 netxen_free_adapter_offload(adapter);
976 err_out_iounmap:
977 if (db_ptr)
978 iounmap(db_ptr);
980 if (mem_ptr0)
981 iounmap(mem_ptr0);
982 if (mem_ptr1)
983 iounmap(mem_ptr1);
984 if (mem_ptr2)
985 iounmap(mem_ptr2);
987 err_out_free_netdev:
988 free_netdev(netdev);
990 err_out_free_res:
991 pci_release_regions(pdev);
993 err_out_disable_pdev:
994 pci_set_drvdata(pdev, NULL);
995 pci_disable_device(pdev);
996 return err;
999 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1001 struct netxen_adapter *adapter;
1002 struct net_device *netdev;
1004 adapter = pci_get_drvdata(pdev);
1005 if (adapter == NULL)
1006 return;
1008 netdev = adapter->netdev;
1010 unregister_netdev(netdev);
1012 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1013 netxen_free_hw_resources(adapter);
1014 netxen_free_sw_resources(adapter);
1017 if (adapter->portnum == 0)
1018 netxen_free_adapter_offload(adapter);
1020 if (adapter->irq)
1021 free_irq(adapter->irq, adapter);
1023 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1024 pci_disable_msix(pdev);
1025 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1026 pci_disable_msi(pdev);
1028 iounmap(adapter->ahw.db_base);
1029 iounmap(adapter->ahw.pci_base0);
1030 iounmap(adapter->ahw.pci_base1);
1031 iounmap(adapter->ahw.pci_base2);
1033 pci_release_regions(pdev);
1034 pci_disable_device(pdev);
1035 pci_set_drvdata(pdev, NULL);
1037 free_netdev(netdev);
1041 * Called when a network interface is made active
1042 * @returns 0 on success, negative value on failure
1044 static int netxen_nic_open(struct net_device *netdev)
1046 struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv;
1047 int err = 0;
1048 int ctx, ring;
1049 irq_handler_t handler;
1050 unsigned long flags = IRQF_SAMPLE_RANDOM;
1052 if (adapter->driver_mismatch)
1053 return -EIO;
1055 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
1056 err = netxen_init_firmware(adapter);
1057 if (err != 0) {
1058 printk(KERN_ERR "Failed to init firmware\n");
1059 return -EIO;
1062 err = netxen_alloc_sw_resources(adapter);
1063 if (err) {
1064 printk(KERN_ERR "%s: Error in setting sw resources\n",
1065 netdev->name);
1066 return err;
1069 netxen_nic_clear_stats(adapter);
1071 err = netxen_alloc_hw_resources(adapter);
1072 if (err) {
1073 printk(KERN_ERR "%s: Error in setting hw resources\n",
1074 netdev->name);
1075 goto err_out_free_sw;
1078 if (adapter->fw_major < 4) {
1079 adapter->crb_addr_cmd_producer =
1080 crb_cmd_producer[adapter->portnum];
1081 adapter->crb_addr_cmd_consumer =
1082 crb_cmd_consumer[adapter->portnum];
1085 netxen_nic_update_cmd_producer(adapter, 0);
1086 netxen_nic_update_cmd_consumer(adapter, 0);
1088 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1089 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1090 netxen_post_rx_buffers(adapter, ctx, ring);
1092 if (NETXEN_IS_MSI_FAMILY(adapter))
1093 handler = netxen_msi_intr;
1094 else {
1095 flags |= IRQF_SHARED;
1096 handler = netxen_intr;
1098 adapter->irq = netdev->irq;
1099 err = request_irq(adapter->irq, handler,
1100 flags, netdev->name, adapter);
1101 if (err) {
1102 printk(KERN_ERR "request_irq failed with: %d\n", err);
1103 goto err_out_free_hw;
1106 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1109 /* Done here again so that even if phantom sw overwrote it,
1110 * we set it */
1111 err = adapter->init_port(adapter, adapter->physical_port);
1112 if (err) {
1113 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1114 netxen_nic_driver_name, adapter->portnum);
1115 goto err_out_free_irq;
1117 adapter->macaddr_set(adapter, netdev->dev_addr);
1119 netxen_nic_set_link_parameters(adapter);
1121 netdev->set_multicast_list(netdev);
1122 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1123 nx_fw_cmd_set_mtu(adapter, netdev->mtu);
1124 else
1125 adapter->set_mtu(adapter, netdev->mtu);
1127 mod_timer(&adapter->watchdog_timer, jiffies);
1129 napi_enable(&adapter->napi);
1130 netxen_nic_enable_int(adapter);
1132 netif_start_queue(netdev);
1134 return 0;
1136 err_out_free_irq:
1137 free_irq(adapter->irq, adapter);
1138 err_out_free_hw:
1139 netxen_free_hw_resources(adapter);
1140 err_out_free_sw:
1141 netxen_free_sw_resources(adapter);
1142 return err;
1146 * netxen_nic_close - Disables a network interface entry point
1148 static int netxen_nic_close(struct net_device *netdev)
1150 struct netxen_adapter *adapter = netdev_priv(netdev);
1152 netif_carrier_off(netdev);
1153 netif_stop_queue(netdev);
1154 napi_disable(&adapter->napi);
1156 if (adapter->stop_port)
1157 adapter->stop_port(adapter);
1159 netxen_nic_disable_int(adapter);
1161 netxen_release_tx_buffers(adapter);
1163 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1164 FLUSH_SCHEDULED_WORK();
1165 del_timer_sync(&adapter->watchdog_timer);
1168 return 0;
1171 void netxen_tso_check(struct netxen_adapter *adapter,
1172 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1174 if (desc->mss) {
1175 desc->total_hdr_length = (sizeof(struct ethhdr) +
1176 ip_hdrlen(skb) + tcp_hdrlen(skb));
1178 if ((NX_IS_REVISION_P3(adapter->ahw.revision_id)) &&
1179 (skb->protocol == htons(ETH_P_IPV6)))
1180 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO6);
1181 else
1182 netxen_set_cmd_desc_opcode(desc, TX_TCP_LSO);
1184 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1185 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1186 netxen_set_cmd_desc_opcode(desc, TX_TCP_PKT);
1187 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1188 netxen_set_cmd_desc_opcode(desc, TX_UDP_PKT);
1189 else
1190 return;
1192 desc->tcp_hdr_offset = skb_transport_offset(skb);
1193 desc->ip_hdr_offset = skb_network_offset(skb);
1196 static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1198 struct netxen_adapter *adapter = netdev_priv(netdev);
1199 struct netxen_hardware_context *hw = &adapter->ahw;
1200 unsigned int first_seg_len = skb->len - skb->data_len;
1201 struct netxen_skb_frag *buffrag;
1202 unsigned int i;
1204 u32 producer, consumer;
1205 u32 saved_producer = 0;
1206 struct cmd_desc_type0 *hwdesc;
1207 int k;
1208 struct netxen_cmd_buffer *pbuf = NULL;
1209 int frag_count;
1210 int no_of_desc;
1211 u32 num_txd = adapter->max_tx_desc_count;
1213 frag_count = skb_shinfo(skb)->nr_frags + 1;
1215 /* There 4 fragments per descriptor */
1216 no_of_desc = (frag_count + 3) >> 2;
1217 if (netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) {
1218 if (skb_shinfo(skb)->gso_size > 0) {
1220 no_of_desc++;
1221 if ((ip_hdrlen(skb) + tcp_hdrlen(skb) +
1222 sizeof(struct ethhdr)) >
1223 (sizeof(struct cmd_desc_type0) - 2)) {
1224 no_of_desc++;
1229 producer = adapter->cmd_producer;
1230 smp_mb();
1231 consumer = adapter->last_cmd_consumer;
1232 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1233 netif_stop_queue(netdev);
1234 smp_mb();
1235 return NETDEV_TX_BUSY;
1238 /* Copy the descriptors into the hardware */
1239 saved_producer = producer;
1240 hwdesc = &hw->cmd_desc_head[producer];
1241 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1242 /* Take skb->data itself */
1243 pbuf = &adapter->cmd_buf_arr[producer];
1244 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1245 skb_shinfo(skb)->gso_size > 0) {
1246 pbuf->mss = skb_shinfo(skb)->gso_size;
1247 hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1248 } else {
1249 pbuf->mss = 0;
1250 hwdesc->mss = 0;
1252 pbuf->total_length = skb->len;
1253 pbuf->skb = skb;
1254 pbuf->cmd = TX_ETHER_PKT;
1255 pbuf->frag_count = frag_count;
1256 pbuf->port = adapter->portnum;
1257 buffrag = &pbuf->frag_array[0];
1258 buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len,
1259 PCI_DMA_TODEVICE);
1260 buffrag->length = first_seg_len;
1261 netxen_set_cmd_desc_totallength(hwdesc, skb->len);
1262 netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count);
1263 netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT);
1265 netxen_set_cmd_desc_port(hwdesc, adapter->portnum);
1266 netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum);
1267 hwdesc->buffer1_length = cpu_to_le16(first_seg_len);
1268 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1270 for (i = 1, k = 1; i < frag_count; i++, k++) {
1271 struct skb_frag_struct *frag;
1272 int len, temp_len;
1273 unsigned long offset;
1274 dma_addr_t temp_dma;
1276 /* move to next desc. if there is a need */
1277 if ((i & 0x3) == 0) {
1278 k = 0;
1279 producer = get_next_index(producer, num_txd);
1280 hwdesc = &hw->cmd_desc_head[producer];
1281 memset(hwdesc, 0, sizeof(struct cmd_desc_type0));
1282 pbuf = &adapter->cmd_buf_arr[producer];
1283 pbuf->skb = NULL;
1285 frag = &skb_shinfo(skb)->frags[i - 1];
1286 len = frag->size;
1287 offset = frag->page_offset;
1289 temp_len = len;
1290 temp_dma = pci_map_page(adapter->pdev, frag->page, offset,
1291 len, PCI_DMA_TODEVICE);
1293 buffrag++;
1294 buffrag->dma = temp_dma;
1295 buffrag->length = temp_len;
1297 switch (k) {
1298 case 0:
1299 hwdesc->buffer1_length = cpu_to_le16(temp_len);
1300 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1301 break;
1302 case 1:
1303 hwdesc->buffer2_length = cpu_to_le16(temp_len);
1304 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1305 break;
1306 case 2:
1307 hwdesc->buffer3_length = cpu_to_le16(temp_len);
1308 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1309 break;
1310 case 3:
1311 hwdesc->buffer4_length = cpu_to_le16(temp_len);
1312 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1313 break;
1315 frag++;
1317 producer = get_next_index(producer, num_txd);
1319 /* might change opcode to TX_TCP_LSO */
1320 netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb);
1322 /* For LSO, we need to copy the MAC/IP/TCP headers into
1323 * the descriptor ring
1325 if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer])
1326 == TX_TCP_LSO) {
1327 int hdr_len, first_hdr_len, more_hdr;
1328 hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length;
1329 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1330 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
1331 more_hdr = 1;
1332 } else {
1333 first_hdr_len = hdr_len;
1334 more_hdr = 0;
1336 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1337 hwdesc = &hw->cmd_desc_head[producer];
1338 pbuf = &adapter->cmd_buf_arr[producer];
1339 pbuf->skb = NULL;
1341 /* copy the first 64 bytes */
1342 memcpy(((void *)hwdesc) + 2,
1343 (void *)(skb->data), first_hdr_len);
1344 producer = get_next_index(producer, num_txd);
1346 if (more_hdr) {
1347 hwdesc = &hw->cmd_desc_head[producer];
1348 pbuf = &adapter->cmd_buf_arr[producer];
1349 pbuf->skb = NULL;
1350 /* copy the next 64 bytes - should be enough except
1351 * for pathological case
1353 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1354 hwdesc,
1355 (hdr_len -
1356 first_hdr_len));
1357 producer = get_next_index(producer, num_txd);
1361 adapter->cmd_producer = producer;
1362 adapter->stats.txbytes += skb->len;
1364 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
1366 adapter->stats.xmitcalled++;
1367 netdev->trans_start = jiffies;
1369 return NETDEV_TX_OK;
1372 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1374 struct net_device *netdev = adapter->netdev;
1375 uint32_t temp, temp_state, temp_val;
1376 int rv = 0;
1378 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1380 temp_state = nx_get_temp_state(temp);
1381 temp_val = nx_get_temp_val(temp);
1383 if (temp_state == NX_TEMP_PANIC) {
1384 printk(KERN_ALERT
1385 "%s: Device temperature %d degrees C exceeds"
1386 " maximum allowed. Hardware has been shut down.\n",
1387 netxen_nic_driver_name, temp_val);
1389 netif_carrier_off(netdev);
1390 netif_stop_queue(netdev);
1391 rv = 1;
1392 } else if (temp_state == NX_TEMP_WARN) {
1393 if (adapter->temp == NX_TEMP_NORMAL) {
1394 printk(KERN_ALERT
1395 "%s: Device temperature %d degrees C "
1396 "exceeds operating range."
1397 " Immediate action needed.\n",
1398 netxen_nic_driver_name, temp_val);
1400 } else {
1401 if (adapter->temp == NX_TEMP_WARN) {
1402 printk(KERN_INFO
1403 "%s: Device temperature is now %d degrees C"
1404 " in normal range.\n", netxen_nic_driver_name,
1405 temp_val);
1408 adapter->temp = temp_state;
1409 return rv;
1412 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1414 struct net_device *netdev = adapter->netdev;
1415 u32 val, port, linkup;
1417 port = adapter->physical_port;
1419 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1420 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1421 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1422 linkup = (val == XG_LINK_UP_P3);
1423 } else {
1424 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1425 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1426 linkup = (val >> port) & 1;
1427 else {
1428 val = (val >> port*8) & 0xff;
1429 linkup = (val == XG_LINK_UP);
1433 if (adapter->ahw.linkup && !linkup) {
1434 printk(KERN_INFO "%s: %s NIC Link is down\n",
1435 netxen_nic_driver_name, netdev->name);
1436 adapter->ahw.linkup = 0;
1437 if (netif_running(netdev)) {
1438 netif_carrier_off(netdev);
1439 netif_stop_queue(netdev);
1441 } else if (!adapter->ahw.linkup && linkup) {
1442 printk(KERN_INFO "%s: %s NIC Link is up\n",
1443 netxen_nic_driver_name, netdev->name);
1444 adapter->ahw.linkup = 1;
1445 if (netif_running(netdev)) {
1446 netif_carrier_on(netdev);
1447 netif_wake_queue(netdev);
1452 static void netxen_watchdog(unsigned long v)
1454 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1456 SCHEDULE_WORK(&adapter->watchdog_task);
1459 void netxen_watchdog_task(struct work_struct *work)
1461 struct netxen_adapter *adapter =
1462 container_of(work, struct netxen_adapter, watchdog_task);
1464 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1465 return;
1467 netxen_nic_handle_phy_intr(adapter);
1469 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1472 static void netxen_tx_timeout(struct net_device *netdev)
1474 struct netxen_adapter *adapter = (struct netxen_adapter *)
1475 netdev_priv(netdev);
1476 SCHEDULE_WORK(&adapter->tx_timeout_task);
1479 static void netxen_tx_timeout_task(struct work_struct *work)
1481 struct netxen_adapter *adapter =
1482 container_of(work, struct netxen_adapter, tx_timeout_task);
1484 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
1485 netxen_nic_driver_name, adapter->netdev->name);
1487 netxen_nic_disable_int(adapter);
1488 napi_disable(&adapter->napi);
1490 adapter->netdev->trans_start = jiffies;
1492 napi_enable(&adapter->napi);
1493 netxen_nic_enable_int(adapter);
1494 netif_wake_queue(adapter->netdev);
1498 * netxen_nic_get_stats - Get System Network Statistics
1499 * @netdev: network interface device structure
1501 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1503 struct netxen_adapter *adapter = netdev_priv(netdev);
1504 struct net_device_stats *stats = &adapter->net_stats;
1506 memset(stats, 0, sizeof(*stats));
1508 /* total packets received */
1509 stats->rx_packets = adapter->stats.no_rcv;
1510 /* total packets transmitted */
1511 stats->tx_packets = adapter->stats.xmitedframes +
1512 adapter->stats.xmitfinished;
1513 /* total bytes received */
1514 stats->rx_bytes = adapter->stats.rxbytes;
1515 /* total bytes transmitted */
1516 stats->tx_bytes = adapter->stats.txbytes;
1517 /* bad packets received */
1518 stats->rx_errors = adapter->stats.rcvdbadskb;
1519 /* packet transmit problems */
1520 stats->tx_errors = adapter->stats.nocmddescriptor;
1521 /* no space in linux buffers */
1522 stats->rx_dropped = adapter->stats.rxdropped;
1523 /* no space available in linux */
1524 stats->tx_dropped = adapter->stats.txdropped;
1526 return stats;
1529 static inline void
1530 netxen_handle_int(struct netxen_adapter *adapter)
1532 netxen_nic_disable_int(adapter);
1533 napi_schedule(&adapter->napi);
1536 static irqreturn_t netxen_intr(int irq, void *data)
1538 struct netxen_adapter *adapter = data;
1539 u32 our_int = 0;
1541 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
1542 /* not our interrupt */
1543 if ((our_int & (0x80 << adapter->portnum)) == 0)
1544 return IRQ_NONE;
1546 if (adapter->intr_scheme == INTR_SCHEME_PERPORT) {
1547 /* claim interrupt */
1548 adapter->pci_write_normalize(adapter, CRB_INT_VECTOR,
1549 our_int & ~((u32)(0x80 << adapter->portnum)));
1552 netxen_handle_int(adapter);
1554 return IRQ_HANDLED;
1557 static irqreturn_t netxen_msi_intr(int irq, void *data)
1559 struct netxen_adapter *adapter = data;
1561 netxen_handle_int(adapter);
1562 return IRQ_HANDLED;
1565 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1567 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
1568 int tx_complete;
1569 int ctx;
1570 int work_done;
1572 tx_complete = netxen_process_cmd_ring(adapter);
1574 work_done = 0;
1575 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1577 * Fairness issue. This will give undue weight to the
1578 * receive context 0.
1582 * To avoid starvation, we give each of our receivers,
1583 * a fraction of the quota. Sometimes, it might happen that we
1584 * have enough quota to process every packet, but since all the
1585 * packets are on one context, it gets only half of the quota,
1586 * and ends up not processing it.
1588 work_done += netxen_process_rcv_ring(adapter, ctx,
1589 budget / MAX_RCV_CTX);
1592 if ((work_done < budget) && tx_complete) {
1593 netif_rx_complete(adapter->netdev, &adapter->napi);
1594 netxen_nic_enable_int(adapter);
1597 return work_done;
1600 #ifdef CONFIG_NET_POLL_CONTROLLER
1601 static void netxen_nic_poll_controller(struct net_device *netdev)
1603 struct netxen_adapter *adapter = netdev_priv(netdev);
1604 disable_irq(adapter->irq);
1605 netxen_intr(adapter->irq, adapter);
1606 enable_irq(adapter->irq);
1608 #endif
1610 static struct pci_driver netxen_driver = {
1611 .name = netxen_nic_driver_name,
1612 .id_table = netxen_pci_tbl,
1613 .probe = netxen_nic_probe,
1614 .remove = __devexit_p(netxen_nic_remove)
1617 /* Driver Registration on NetXen card */
1619 static int __init netxen_init_module(void)
1621 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
1622 return -ENOMEM;
1624 return pci_register_driver(&netxen_driver);
1627 module_init(netxen_init_module);
1629 static void __exit netxen_exit_module(void)
1631 pci_unregister_driver(&netxen_driver);
1632 destroy_workqueue(netxen_workq);
1635 module_exit(netxen_exit_module);