2 * Copyright (C) 2003 - 2006 NetXen, Inc.
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,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
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>
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 #define NETXEN_NETDEV_WEIGHT 120
53 #define NETXEN_ADAPTER_UP_MAGIC 777
54 #define NETXEN_NIC_PEG_TUNE 0
56 /* Local functions to NetXen NIC driver */
57 static int __devinit
netxen_nic_probe(struct pci_dev
*pdev
,
58 const struct pci_device_id
*ent
);
59 static void __devexit
netxen_nic_remove(struct pci_dev
*pdev
);
60 static int netxen_nic_open(struct net_device
*netdev
);
61 static int netxen_nic_close(struct net_device
*netdev
);
62 static int netxen_nic_xmit_frame(struct sk_buff
*, struct net_device
*);
63 static void netxen_tx_timeout(struct net_device
*netdev
);
64 static void netxen_tx_timeout_task(struct work_struct
*work
);
65 static void netxen_watchdog(unsigned long);
66 static int netxen_nic_poll(struct napi_struct
*napi
, int budget
);
67 #ifdef CONFIG_NET_POLL_CONTROLLER
68 static void netxen_nic_poll_controller(struct net_device
*netdev
);
70 static irqreturn_t
netxen_intr(int irq
, void *data
);
71 static irqreturn_t
netxen_msi_intr(int irq
, void *data
);
73 int physical_port
[] = {0, 1, 2, 3};
75 /* PCI Device ID Table */
76 static struct pci_device_id netxen_pci_tbl
[] __devinitdata
= {
77 {PCI_DEVICE(0x4040, 0x0001)},
78 {PCI_DEVICE(0x4040, 0x0002)},
79 {PCI_DEVICE(0x4040, 0x0003)},
80 {PCI_DEVICE(0x4040, 0x0004)},
81 {PCI_DEVICE(0x4040, 0x0005)},
82 {PCI_DEVICE(0x4040, 0x0024)},
83 {PCI_DEVICE(0x4040, 0x0025)},
87 MODULE_DEVICE_TABLE(pci
, netxen_pci_tbl
);
89 struct workqueue_struct
*netxen_workq
;
90 static void netxen_watchdog(unsigned long);
92 static void netxen_nic_update_cmd_producer(struct netxen_adapter
*adapter
,
93 uint32_t crb_producer
)
95 switch (adapter
->portnum
) {
97 writel(crb_producer
, NETXEN_CRB_NORMALIZE
98 (adapter
, CRB_CMD_PRODUCER_OFFSET
));
101 writel(crb_producer
, NETXEN_CRB_NORMALIZE
102 (adapter
, CRB_CMD_PRODUCER_OFFSET_1
));
105 writel(crb_producer
, NETXEN_CRB_NORMALIZE
106 (adapter
, CRB_CMD_PRODUCER_OFFSET_2
));
109 writel(crb_producer
, NETXEN_CRB_NORMALIZE
110 (adapter
, CRB_CMD_PRODUCER_OFFSET_3
));
113 printk(KERN_WARNING
"We tried to update "
114 "CRB_CMD_PRODUCER_OFFSET for invalid "
115 "PCI function id %d\n",
121 static void netxen_nic_update_cmd_consumer(struct netxen_adapter
*adapter
,
124 switch (adapter
->portnum
) {
126 writel(crb_consumer
, NETXEN_CRB_NORMALIZE
127 (adapter
, CRB_CMD_CONSUMER_OFFSET
));
130 writel(crb_consumer
, NETXEN_CRB_NORMALIZE
131 (adapter
, CRB_CMD_CONSUMER_OFFSET_1
));
134 writel(crb_consumer
, NETXEN_CRB_NORMALIZE
135 (adapter
, CRB_CMD_CONSUMER_OFFSET_2
));
138 writel(crb_consumer
, NETXEN_CRB_NORMALIZE
139 (adapter
, CRB_CMD_CONSUMER_OFFSET_3
));
142 printk(KERN_WARNING
"We tried to update "
143 "CRB_CMD_PRODUCER_OFFSET for invalid "
144 "PCI function id %d\n",
150 #define ADAPTER_LIST_SIZE 12
152 static uint32_t msi_tgt_status
[4] = {
153 ISR_INT_TARGET_STATUS
, ISR_INT_TARGET_STATUS_F1
,
154 ISR_INT_TARGET_STATUS_F2
, ISR_INT_TARGET_STATUS_F3
157 static uint32_t sw_int_mask
[4] = {
158 CRB_SW_INT_MASK_0
, CRB_SW_INT_MASK_1
,
159 CRB_SW_INT_MASK_2
, CRB_SW_INT_MASK_3
162 static void netxen_nic_disable_int(struct netxen_adapter
*adapter
)
166 int port
= adapter
->portnum
;
167 int pci_fn
= adapter
->ahw
.pci_func
;
169 if (adapter
->msi_mode
!= MSI_MODE_MULTIFUNC
)
170 writel(0x0, NETXEN_CRB_NORMALIZE(adapter
, sw_int_mask
[port
]));
172 if (adapter
->intr_scheme
!= -1 &&
173 adapter
->intr_scheme
!= INTR_SCHEME_PERPORT
)
174 writel(mask
,PCI_OFFSET_SECOND_RANGE(adapter
, ISR_INT_MASK
));
176 if (!(adapter
->flags
& NETXEN_NIC_MSI_ENABLED
)) {
179 PCI_OFFSET_SECOND_RANGE(adapter
, ISR_INT_TARGET_STATUS
));
180 mask
= readl(pci_base_offset(adapter
, ISR_INT_VECTOR
));
187 printk(KERN_NOTICE
"%s: Failed to disable interrupt completely\n",
188 netxen_nic_driver_name
);
191 if (adapter
->msi_mode
== MSI_MODE_MULTIFUNC
) {
192 writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter
,
193 msi_tgt_status
[pci_fn
]));
198 static void netxen_nic_enable_int(struct netxen_adapter
*adapter
)
201 int port
= adapter
->portnum
;
203 DPRINTK(1, INFO
, "Entered ISR Enable \n");
205 if (adapter
->intr_scheme
!= -1 &&
206 adapter
->intr_scheme
!= INTR_SCHEME_PERPORT
) {
207 switch (adapter
->ahw
.board_type
) {
211 case NETXEN_NIC_XGBE
:
219 writel(mask
, PCI_OFFSET_SECOND_RANGE(adapter
, ISR_INT_MASK
));
222 writel(0x1, NETXEN_CRB_NORMALIZE(adapter
, sw_int_mask
[port
]));
224 if (!(adapter
->flags
& NETXEN_NIC_MSI_ENABLED
)) {
226 if (adapter
->intr_scheme
!= -1 &&
227 adapter
->intr_scheme
!= INTR_SCHEME_PERPORT
) {
228 writel(0X0, NETXEN_CRB_NORMALIZE(adapter
, CRB_INT_VECTOR
));
231 PCI_OFFSET_SECOND_RANGE(adapter
, ISR_INT_TARGET_MASK
));
234 DPRINTK(1, INFO
, "Done with enable Int\n");
240 * The Linux system will invoke this after identifying the vendor ID and
241 * device Id in the pci_tbl supported by this module.
243 * A quad port card has one operational PCI config space, (function 0),
244 * which is used to access all four ports.
246 * This routine will initialize the adapter, and setup the global parameters
247 * along with the port's specific structure.
250 netxen_nic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
252 struct net_device
*netdev
= NULL
;
253 struct netxen_adapter
*adapter
= NULL
;
254 void __iomem
*mem_ptr0
= NULL
;
255 void __iomem
*mem_ptr1
= NULL
;
256 void __iomem
*mem_ptr2
= NULL
;
257 unsigned long first_page_group_end
;
258 unsigned long first_page_group_start
;
261 u8 __iomem
*db_ptr
= NULL
;
262 unsigned long mem_base
, mem_len
, db_base
, db_len
;
263 int pci_using_dac
, i
= 0, err
;
265 struct netxen_recv_context
*recv_ctx
= NULL
;
266 struct netxen_rcv_desc_ctx
*rcv_desc
= NULL
;
267 struct netxen_cmd_buffer
*cmd_buf_arr
= NULL
;
268 __le64 mac_addr
[FLASH_NUM_PORTS
+ 1];
271 int pci_func_id
= PCI_FUNC(pdev
->devfn
);
272 DECLARE_MAC_BUF(mac
);
274 printk(KERN_INFO
"%s \n", netxen_nic_driver_string
);
276 if (pdev
->class != 0x020000) {
277 printk(KERN_ERR
"NetXen function %d, class %x will not "
278 "be enabled.\n",pci_func_id
, pdev
->class);
281 if ((err
= pci_enable_device(pdev
)))
283 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
285 goto err_out_disable_pdev
;
288 if ((err
= pci_request_regions(pdev
, netxen_nic_driver_name
)))
289 goto err_out_disable_pdev
;
291 pci_set_master(pdev
);
292 if (pdev
->revision
== NX_P2_C1
&&
293 (pci_set_dma_mask(pdev
, DMA_35BIT_MASK
) == 0) &&
294 (pci_set_consistent_dma_mask(pdev
, DMA_35BIT_MASK
) == 0)) {
297 if ((err
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
)) ||
298 (err
= pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
)))
299 goto err_out_free_res
;
305 netdev
= alloc_etherdev(sizeof(struct netxen_adapter
));
307 printk(KERN_ERR
"%s: Failed to allocate memory for the "
308 "device block.Check system memory resource"
309 " usage.\n", netxen_nic_driver_name
);
310 goto err_out_free_res
;
313 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
315 adapter
= netdev
->priv
;
317 adapter
->ahw
.pdev
= pdev
;
318 adapter
->ahw
.pci_func
= pci_func_id
;
320 /* remap phys address */
321 mem_base
= pci_resource_start(pdev
, 0); /* 0 is for BAR 0 */
322 mem_len
= pci_resource_len(pdev
, 0);
324 /* 128 Meg of memory */
325 if (mem_len
== NETXEN_PCI_128MB_SIZE
) {
326 mem_ptr0
= ioremap(mem_base
, FIRST_PAGE_GROUP_SIZE
);
327 mem_ptr1
= ioremap(mem_base
+ SECOND_PAGE_GROUP_START
,
328 SECOND_PAGE_GROUP_SIZE
);
329 mem_ptr2
= ioremap(mem_base
+ THIRD_PAGE_GROUP_START
,
330 THIRD_PAGE_GROUP_SIZE
);
331 first_page_group_start
= FIRST_PAGE_GROUP_START
;
332 first_page_group_end
= FIRST_PAGE_GROUP_END
;
333 } else if (mem_len
== NETXEN_PCI_32MB_SIZE
) {
334 mem_ptr1
= ioremap(mem_base
, SECOND_PAGE_GROUP_SIZE
);
335 mem_ptr2
= ioremap(mem_base
+ THIRD_PAGE_GROUP_START
-
336 SECOND_PAGE_GROUP_START
, THIRD_PAGE_GROUP_SIZE
);
337 first_page_group_start
= 0;
338 first_page_group_end
= 0;
341 goto err_out_free_netdev
;
344 if ((!mem_ptr0
&& mem_len
== NETXEN_PCI_128MB_SIZE
) ||
345 !mem_ptr1
|| !mem_ptr2
) {
347 "Cannot remap adapter memory aborting.:"
348 "0 -> %p, 1 -> %p, 2 -> %p\n",
349 mem_ptr0
, mem_ptr1
, mem_ptr2
);
352 goto err_out_iounmap
;
354 db_base
= pci_resource_start(pdev
, 4); /* doorbell is on bar 4 */
355 db_len
= pci_resource_len(pdev
, 4);
358 printk(KERN_ERR
"%s: doorbell is disabled\n",
359 netxen_nic_driver_name
);
361 goto err_out_iounmap
;
363 DPRINTK(INFO
, "doorbell ioremap from %lx a size of %lx\n", db_base
,
366 db_ptr
= ioremap(db_base
, NETXEN_DB_MAPSIZE_BYTES
);
368 printk(KERN_ERR
"%s: Failed to allocate doorbell map.",
369 netxen_nic_driver_name
);
371 goto err_out_iounmap
;
373 DPRINTK(INFO
, "doorbell ioremaped at %p\n", db_ptr
);
375 adapter
->ahw
.pci_base0
= mem_ptr0
;
376 adapter
->ahw
.first_page_group_start
= first_page_group_start
;
377 adapter
->ahw
.first_page_group_end
= first_page_group_end
;
378 adapter
->ahw
.pci_base1
= mem_ptr1
;
379 adapter
->ahw
.pci_base2
= mem_ptr2
;
380 adapter
->ahw
.db_base
= db_ptr
;
381 adapter
->ahw
.db_len
= db_len
;
383 adapter
->netdev
= netdev
;
384 adapter
->pdev
= pdev
;
386 netif_napi_add(netdev
, &adapter
->napi
,
387 netxen_nic_poll
, NETXEN_NETDEV_WEIGHT
);
389 /* this will be read from FW later */
390 adapter
->intr_scheme
= -1;
391 adapter
->msi_mode
= -1;
393 /* This will be reset for mezz cards */
394 adapter
->portnum
= pci_func_id
;
395 adapter
->status
&= ~NETXEN_NETDEV_STATUS
;
396 adapter
->rx_csum
= 1;
398 netdev
->open
= netxen_nic_open
;
399 netdev
->stop
= netxen_nic_close
;
400 netdev
->hard_start_xmit
= netxen_nic_xmit_frame
;
401 netdev
->get_stats
= netxen_nic_get_stats
;
402 netdev
->set_multicast_list
= netxen_nic_set_multi
;
403 netdev
->set_mac_address
= netxen_nic_set_mac
;
404 netdev
->change_mtu
= netxen_nic_change_mtu
;
405 netdev
->tx_timeout
= netxen_tx_timeout
;
406 netdev
->watchdog_timeo
= 2*HZ
;
408 netxen_nic_change_mtu(netdev
, netdev
->mtu
);
410 SET_ETHTOOL_OPS(netdev
, &netxen_nic_ethtool_ops
);
411 #ifdef CONFIG_NET_POLL_CONTROLLER
412 netdev
->poll_controller
= netxen_nic_poll_controller
;
414 /* ScatterGather support */
415 netdev
->features
= NETIF_F_SG
;
416 netdev
->features
|= NETIF_F_IP_CSUM
;
417 netdev
->features
|= NETIF_F_TSO
;
420 netdev
->features
|= NETIF_F_HIGHDMA
;
422 if (pci_enable_msi(pdev
))
423 adapter
->flags
&= ~NETXEN_NIC_MSI_ENABLED
;
425 adapter
->flags
|= NETXEN_NIC_MSI_ENABLED
;
427 netdev
->irq
= pdev
->irq
;
428 INIT_WORK(&adapter
->tx_timeout_task
, netxen_tx_timeout_task
);
431 * Set the CRB window to invalid. If any register in window 0 is
432 * accessed it should set the window to 0 and then reset it to 1.
434 adapter
->curr_window
= 255;
436 /* initialize the adapter */
437 netxen_initialize_adapter_hw(adapter
);
440 * Adapter in our case is quad port so initialize it before
441 * initializing the ports
444 netxen_initialize_adapter_ops(adapter
);
446 adapter
->max_tx_desc_count
= MAX_CMD_DESCRIPTORS_HOST
;
447 if ((adapter
->ahw
.boardcfg
.board_type
== NETXEN_BRDTYPE_P2_SB35_4G
) ||
448 (adapter
->ahw
.boardcfg
.board_type
==
449 NETXEN_BRDTYPE_P2_SB31_2G
))
450 adapter
->max_rx_desc_count
= MAX_RCV_DESCRIPTORS_1G
;
452 adapter
->max_rx_desc_count
= MAX_RCV_DESCRIPTORS
;
453 adapter
->max_jumbo_rx_desc_count
= MAX_JUMBO_RCV_DESCRIPTORS
;
454 adapter
->max_lro_rx_desc_count
= MAX_LRO_RCV_DESCRIPTORS
;
456 cmd_buf_arr
= (struct netxen_cmd_buffer
*)vmalloc(TX_RINGSIZE
);
457 if (cmd_buf_arr
== NULL
) {
459 "%s: Could not allocate cmd_buf_arr memory:%d\n",
460 netxen_nic_driver_name
, (int)TX_RINGSIZE
);
462 goto err_out_free_adapter
;
464 memset(cmd_buf_arr
, 0, TX_RINGSIZE
);
465 adapter
->cmd_buf_arr
= cmd_buf_arr
;
467 for (i
= 0; i
< MAX_RCV_CTX
; ++i
) {
468 recv_ctx
= &adapter
->recv_ctx
[i
];
469 for (ring
= 0; ring
< NUM_RCV_DESC_RINGS
; ring
++) {
470 rcv_desc
= &recv_ctx
->rcv_desc
[ring
];
471 switch (RCV_DESC_TYPE(ring
)) {
472 case RCV_DESC_NORMAL
:
473 rcv_desc
->max_rx_desc_count
=
474 adapter
->max_rx_desc_count
;
475 rcv_desc
->flags
= RCV_DESC_NORMAL
;
476 rcv_desc
->dma_size
= RX_DMA_MAP_LEN
;
477 rcv_desc
->skb_size
= MAX_RX_BUFFER_LENGTH
;
481 rcv_desc
->max_rx_desc_count
=
482 adapter
->max_jumbo_rx_desc_count
;
483 rcv_desc
->flags
= RCV_DESC_JUMBO
;
484 rcv_desc
->dma_size
= RX_JUMBO_DMA_MAP_LEN
;
485 rcv_desc
->skb_size
= MAX_RX_JUMBO_BUFFER_LENGTH
;
489 rcv_desc
->max_rx_desc_count
=
490 adapter
->max_lro_rx_desc_count
;
491 rcv_desc
->flags
= RCV_DESC_LRO
;
492 rcv_desc
->dma_size
= RX_LRO_DMA_MAP_LEN
;
493 rcv_desc
->skb_size
= MAX_RX_LRO_BUFFER_LENGTH
;
497 rcv_desc
->rx_buf_arr
= (struct netxen_rx_buffer
*)
498 vmalloc(RCV_BUFFSIZE
);
500 if (rcv_desc
->rx_buf_arr
== NULL
) {
501 printk(KERN_ERR
"%s: Could not allocate "
502 "rcv_desc->rx_buf_arr memory:%d\n",
503 netxen_nic_driver_name
,
506 goto err_out_free_rx_buffer
;
508 memset(rcv_desc
->rx_buf_arr
, 0, RCV_BUFFSIZE
);
513 netxen_initialize_adapter_sw(adapter
); /* initialize the buffers in adapter */
515 /* Mezz cards have PCI function 0,2,3 enabled */
516 if ((adapter
->ahw
.boardcfg
.board_type
== NETXEN_BRDTYPE_P2_SB31_10G_IMEZ
)
517 && (pci_func_id
>= 2))
518 adapter
->portnum
= pci_func_id
- 2;
521 if(adapter
->portnum
== 0) {
522 netxen_pinit_from_rom(adapter
, 0);
524 netxen_load_firmware(adapter
);
528 init_timer(&adapter
->watchdog_timer
);
529 adapter
->ahw
.xg_linkup
= 0;
530 adapter
->watchdog_timer
.function
= &netxen_watchdog
;
531 adapter
->watchdog_timer
.data
= (unsigned long)adapter
;
532 INIT_WORK(&adapter
->watchdog_task
, netxen_watchdog_task
);
533 adapter
->ahw
.pdev
= pdev
;
534 adapter
->ahw
.revision_id
= pdev
->revision
;
536 /* make sure Window == 1 */
537 netxen_nic_pci_change_crbwindow(adapter
, 1);
539 netxen_nic_update_cmd_producer(adapter
, 0);
540 netxen_nic_update_cmd_consumer(adapter
, 0);
541 writel(0, NETXEN_CRB_NORMALIZE(adapter
, CRB_HOST_CMD_ADDR_LO
));
543 if (netxen_is_flash_supported(adapter
) == 0 &&
544 netxen_get_flash_mac_addr(adapter
, mac_addr
) == 0)
550 unsigned char *p
= (unsigned char *)&mac_addr
[adapter
->portnum
];
551 netdev
->dev_addr
[0] = *(p
+ 5);
552 netdev
->dev_addr
[1] = *(p
+ 4);
553 netdev
->dev_addr
[2] = *(p
+ 3);
554 netdev
->dev_addr
[3] = *(p
+ 2);
555 netdev
->dev_addr
[4] = *(p
+ 1);
556 netdev
->dev_addr
[5] = *(p
+ 0);
558 memcpy(netdev
->perm_addr
, netdev
->dev_addr
,
560 if (!is_valid_ether_addr(netdev
->perm_addr
)) {
561 printk(KERN_ERR
"%s: Bad MAC address %s.\n",
562 netxen_nic_driver_name
,
563 print_mac(mac
, netdev
->dev_addr
));
565 if (adapter
->macaddr_set
)
566 adapter
->macaddr_set(adapter
,
571 if (adapter
->portnum
== 0) {
572 err
= netxen_initialize_adapter_offload(adapter
);
574 goto err_out_free_rx_buffer
;
575 val
= readl(NETXEN_CRB_NORMALIZE(adapter
,
576 NETXEN_CAM_RAM(0x1fc)));
577 if (val
== 0x55555555) {
578 /* This is the first boot after power up */
579 netxen_nic_read_w0(adapter
, NETXEN_PCIE_REG(0x4), &val
);
582 netxen_nic_write_w0(adapter
, NETXEN_PCIE_REG(0x4), val
);
583 netxen_nic_read_w0(adapter
, NETXEN_PCIE_REG(0x4), &val
);
585 printk(KERN_ERR
"%s: failed to set MSI bit in PCI-e reg\n",
586 netxen_nic_driver_name
);
588 val
= readl(NETXEN_CRB_NORMALIZE(adapter
,
589 NETXEN_ROMUSB_GLB_SW_RESET
));
590 printk(KERN_INFO
"NetXen: read 0x%08x for reset reg.\n",val
);
591 if (val
!= 0x80000f) {
592 /* clear the register for future unloads/loads */
593 writel(0, NETXEN_CRB_NORMALIZE(adapter
,
594 NETXEN_CAM_RAM(0x1fc)));
595 printk(KERN_ERR
"ERROR in NetXen HW init sequence.\n");
597 goto err_out_free_dev
;
601 /* clear the register for future unloads/loads */
602 writel(0, NETXEN_CRB_NORMALIZE(adapter
, NETXEN_CAM_RAM(0x1fc)));
603 printk(KERN_INFO
"State: 0x%0x\n",
604 readl(NETXEN_CRB_NORMALIZE(adapter
, CRB_CMDPEG_STATE
)));
607 * Tell the hardware our version number.
609 i
= (_NETXEN_NIC_LINUX_MAJOR
<< 16)
610 | ((_NETXEN_NIC_LINUX_MINOR
<< 8))
611 | (_NETXEN_NIC_LINUX_SUBVERSION
);
612 writel(i
, NETXEN_CRB_NORMALIZE(adapter
, CRB_DRIVER_VERSION
));
614 /* Unlock the HW, prompting the boot sequence */
616 NETXEN_CRB_NORMALIZE(adapter
,
617 NETXEN_ROMUSB_GLB_PEGTUNE_DONE
));
618 /* Handshake with the card before we register the devices. */
619 netxen_phantom_init(adapter
, NETXEN_NIC_PEG_TUNE
);
623 * See if the firmware gave us a virtual-physical port mapping.
625 i
= readl(NETXEN_CRB_NORMALIZE(adapter
, CRB_V2P(adapter
->portnum
)));
627 physical_port
[adapter
->portnum
] = i
;
629 netif_carrier_off(netdev
);
630 netif_stop_queue(netdev
);
632 if ((err
= register_netdev(netdev
))) {
633 printk(KERN_ERR
"%s: register_netdev failed port #%d"
634 " aborting\n", netxen_nic_driver_name
,
637 goto err_out_free_dev
;
640 pci_set_drvdata(pdev
, adapter
);
642 switch (adapter
->ahw
.board_type
) {
644 printk(KERN_INFO
"%s: QUAD GbE board initialized\n",
645 netxen_nic_driver_name
);
648 case NETXEN_NIC_XGBE
:
649 printk(KERN_INFO
"%s: XGbE board initialized\n",
650 netxen_nic_driver_name
);
654 adapter
->driver_mismatch
= 0;
659 if (adapter
->portnum
== 0)
660 netxen_free_adapter_offload(adapter
);
662 err_out_free_rx_buffer
:
663 for (i
= 0; i
< MAX_RCV_CTX
; ++i
) {
664 recv_ctx
= &adapter
->recv_ctx
[i
];
665 for (ring
= 0; ring
< NUM_RCV_DESC_RINGS
; ring
++) {
666 rcv_desc
= &recv_ctx
->rcv_desc
[ring
];
667 if (rcv_desc
->rx_buf_arr
!= NULL
) {
668 vfree(rcv_desc
->rx_buf_arr
);
669 rcv_desc
->rx_buf_arr
= NULL
;
675 err_out_free_adapter
:
676 if (adapter
->flags
& NETXEN_NIC_MSI_ENABLED
)
677 pci_disable_msi(pdev
);
679 pci_set_drvdata(pdev
, NULL
);
696 pci_release_regions(pdev
);
698 err_out_disable_pdev
:
699 pci_disable_device(pdev
);
703 static void __devexit
netxen_nic_remove(struct pci_dev
*pdev
)
705 struct netxen_adapter
*adapter
;
706 struct net_device
*netdev
;
707 struct netxen_rx_buffer
*buffer
;
708 struct netxen_recv_context
*recv_ctx
;
709 struct netxen_rcv_desc_ctx
*rcv_desc
;
711 static int init_firmware_done
= 0;
713 adapter
= pci_get_drvdata(pdev
);
717 netdev
= adapter
->netdev
;
719 unregister_netdev(netdev
);
721 if (adapter
->is_up
== NETXEN_ADAPTER_UP_MAGIC
) {
722 init_firmware_done
++;
723 netxen_free_hw_resources(adapter
);
726 for (ctxid
= 0; ctxid
< MAX_RCV_CTX
; ++ctxid
) {
727 recv_ctx
= &adapter
->recv_ctx
[ctxid
];
728 for (ring
= 0; ring
< NUM_RCV_DESC_RINGS
; ring
++) {
729 rcv_desc
= &recv_ctx
->rcv_desc
[ring
];
730 for (i
= 0; i
< rcv_desc
->max_rx_desc_count
; ++i
) {
731 buffer
= &(rcv_desc
->rx_buf_arr
[i
]);
732 if (buffer
->state
== NETXEN_BUFFER_FREE
)
734 pci_unmap_single(pdev
, buffer
->dma
,
737 if (buffer
->skb
!= NULL
)
738 dev_kfree_skb_any(buffer
->skb
);
740 vfree(rcv_desc
->rx_buf_arr
);
744 vfree(adapter
->cmd_buf_arr
);
746 if (adapter
->portnum
== 0) {
747 if (init_firmware_done
) {
750 if (dma_watchdog_shutdown_request(adapter
) == 1)
753 if (dma_watchdog_shutdown_poll_result(adapter
) == 1)
758 printk(KERN_ERR
"%s: dma_watchdog_shutdown failed\n",
761 /* clear the register for future unloads/loads */
762 writel(0, NETXEN_CRB_NORMALIZE(adapter
, NETXEN_CAM_RAM(0x1fc)));
763 printk(KERN_INFO
"State: 0x%0x\n",
764 readl(NETXEN_CRB_NORMALIZE(adapter
, CRB_CMDPEG_STATE
)));
766 /* leave the hw in the same state as reboot */
767 writel(0, NETXEN_CRB_NORMALIZE(adapter
, CRB_CMDPEG_STATE
));
768 netxen_pinit_from_rom(adapter
, 0);
770 netxen_load_firmware(adapter
);
771 netxen_phantom_init(adapter
, NETXEN_NIC_PEG_TUNE
);
774 /* clear the register for future unloads/loads */
775 writel(0, NETXEN_CRB_NORMALIZE(adapter
, NETXEN_CAM_RAM(0x1fc)));
776 printk(KERN_INFO
"State: 0x%0x\n",
777 readl(NETXEN_CRB_NORMALIZE(adapter
, CRB_CMDPEG_STATE
)));
781 if (dma_watchdog_shutdown_request(adapter
) == 1)
784 if (dma_watchdog_shutdown_poll_result(adapter
) == 1)
789 netxen_free_adapter_offload(adapter
);
791 printk(KERN_ERR
"%s: dma_watchdog_shutdown failed\n",
797 free_irq(adapter
->irq
, adapter
);
799 if (adapter
->flags
& NETXEN_NIC_MSI_ENABLED
)
800 pci_disable_msi(pdev
);
802 iounmap(adapter
->ahw
.db_base
);
803 iounmap(adapter
->ahw
.pci_base0
);
804 iounmap(adapter
->ahw
.pci_base1
);
805 iounmap(adapter
->ahw
.pci_base2
);
807 pci_release_regions(pdev
);
808 pci_disable_device(pdev
);
809 pci_set_drvdata(pdev
, NULL
);
815 * Called when a network interface is made active
816 * @returns 0 on success, negative value on failure
818 static int netxen_nic_open(struct net_device
*netdev
)
820 struct netxen_adapter
*adapter
= (struct netxen_adapter
*)netdev
->priv
;
823 irq_handler_t handler
;
824 unsigned long flags
= IRQF_SAMPLE_RANDOM
;
826 if (adapter
->is_up
!= NETXEN_ADAPTER_UP_MAGIC
) {
827 err
= netxen_init_firmware(adapter
);
829 printk(KERN_ERR
"Failed to init firmware\n");
832 netxen_nic_flash_print(adapter
);
834 /* setup all the resources for the Phantom... */
835 /* this include the descriptors for rcv, tx, and status */
836 netxen_nic_clear_stats(adapter
);
837 err
= netxen_nic_hw_resources(adapter
);
839 printk(KERN_ERR
"Error in setting hw resources:%d\n",
843 for (ctx
= 0; ctx
< MAX_RCV_CTX
; ++ctx
) {
844 for (ring
= 0; ring
< NUM_RCV_DESC_RINGS
; ring
++)
845 netxen_post_rx_buffers(adapter
, ctx
, ring
);
847 adapter
->irq
= adapter
->ahw
.pdev
->irq
;
848 if (adapter
->flags
& NETXEN_NIC_MSI_ENABLED
)
849 handler
= netxen_msi_intr
;
851 flags
|= IRQF_SHARED
;
852 handler
= netxen_intr
;
854 err
= request_irq(adapter
->irq
, handler
,
855 flags
, netdev
->name
, adapter
);
857 printk(KERN_ERR
"request_irq failed with: %d\n", err
);
858 netxen_free_hw_resources(adapter
);
862 adapter
->is_up
= NETXEN_ADAPTER_UP_MAGIC
;
864 /* Done here again so that even if phantom sw overwrote it,
866 if (adapter
->init_port
867 && adapter
->init_port(adapter
, adapter
->portnum
) != 0) {
868 printk(KERN_ERR
"%s: Failed to initialize port %d\n",
869 netxen_nic_driver_name
, adapter
->portnum
);
872 if (adapter
->macaddr_set
)
873 adapter
->macaddr_set(adapter
, netdev
->dev_addr
);
875 netxen_nic_set_link_parameters(adapter
);
877 netxen_nic_set_multi(netdev
);
878 if (adapter
->set_mtu
)
879 adapter
->set_mtu(adapter
, netdev
->mtu
);
881 if (!adapter
->driver_mismatch
)
882 mod_timer(&adapter
->watchdog_timer
, jiffies
);
884 napi_enable(&adapter
->napi
);
885 netxen_nic_enable_int(adapter
);
887 if (!adapter
->driver_mismatch
)
888 netif_start_queue(netdev
);
894 * netxen_nic_close - Disables a network interface entry point
896 static int netxen_nic_close(struct net_device
*netdev
)
898 struct netxen_adapter
*adapter
= netdev_priv(netdev
);
900 struct netxen_cmd_buffer
*cmd_buff
;
901 struct netxen_skb_frag
*buffrag
;
903 netif_carrier_off(netdev
);
904 netif_stop_queue(netdev
);
905 napi_disable(&adapter
->napi
);
907 if (adapter
->stop_port
)
908 adapter
->stop_port(adapter
);
910 netxen_nic_disable_int(adapter
);
912 cmd_buff
= adapter
->cmd_buf_arr
;
913 for (i
= 0; i
< adapter
->max_tx_desc_count
; i
++) {
914 buffrag
= cmd_buff
->frag_array
;
916 pci_unmap_single(adapter
->pdev
, buffrag
->dma
,
917 buffrag
->length
, PCI_DMA_TODEVICE
);
920 for (j
= 0; j
< cmd_buff
->frag_count
; j
++) {
923 pci_unmap_page(adapter
->pdev
, buffrag
->dma
,
929 /* Free the skb we received in netxen_nic_xmit_frame */
931 dev_kfree_skb_any(cmd_buff
->skb
);
932 cmd_buff
->skb
= NULL
;
936 if (adapter
->is_up
== NETXEN_ADAPTER_UP_MAGIC
) {
937 FLUSH_SCHEDULED_WORK();
938 del_timer_sync(&adapter
->watchdog_timer
);
944 static int netxen_nic_xmit_frame(struct sk_buff
*skb
, struct net_device
*netdev
)
946 struct netxen_adapter
*adapter
= netdev_priv(netdev
);
947 struct netxen_hardware_context
*hw
= &adapter
->ahw
;
948 unsigned int first_seg_len
= skb
->len
- skb
->data_len
;
949 struct netxen_skb_frag
*buffrag
;
952 u32 producer
, consumer
;
953 u32 saved_producer
= 0;
954 struct cmd_desc_type0
*hwdesc
;
956 struct netxen_cmd_buffer
*pbuf
= NULL
;
959 u32 num_txd
= adapter
->max_tx_desc_count
;
961 frag_count
= skb_shinfo(skb
)->nr_frags
+ 1;
963 /* There 4 fragments per descriptor */
964 no_of_desc
= (frag_count
+ 3) >> 2;
965 if (netdev
->features
& NETIF_F_TSO
) {
966 if (skb_shinfo(skb
)->gso_size
> 0) {
969 if ((ip_hdrlen(skb
) + tcp_hdrlen(skb
) +
970 sizeof(struct ethhdr
)) >
971 (sizeof(struct cmd_desc_type0
) - 2)) {
977 producer
= adapter
->cmd_producer
;
979 consumer
= adapter
->last_cmd_consumer
;
980 if ((no_of_desc
+2) > find_diff_among(producer
, consumer
, num_txd
)) {
981 netif_stop_queue(netdev
);
983 return NETDEV_TX_BUSY
;
986 /* Copy the descriptors into the hardware */
987 saved_producer
= producer
;
988 hwdesc
= &hw
->cmd_desc_head
[producer
];
989 memset(hwdesc
, 0, sizeof(struct cmd_desc_type0
));
990 /* Take skb->data itself */
991 pbuf
= &adapter
->cmd_buf_arr
[producer
];
992 if ((netdev
->features
& NETIF_F_TSO
) && skb_shinfo(skb
)->gso_size
> 0) {
993 pbuf
->mss
= skb_shinfo(skb
)->gso_size
;
994 hwdesc
->mss
= cpu_to_le16(skb_shinfo(skb
)->gso_size
);
999 pbuf
->total_length
= skb
->len
;
1001 pbuf
->cmd
= TX_ETHER_PKT
;
1002 pbuf
->frag_count
= frag_count
;
1003 pbuf
->port
= adapter
->portnum
;
1004 buffrag
= &pbuf
->frag_array
[0];
1005 buffrag
->dma
= pci_map_single(adapter
->pdev
, skb
->data
, first_seg_len
,
1007 buffrag
->length
= first_seg_len
;
1008 netxen_set_cmd_desc_totallength(hwdesc
, skb
->len
);
1009 netxen_set_cmd_desc_num_of_buff(hwdesc
, frag_count
);
1010 netxen_set_cmd_desc_opcode(hwdesc
, TX_ETHER_PKT
);
1012 netxen_set_cmd_desc_port(hwdesc
, adapter
->portnum
);
1013 netxen_set_cmd_desc_ctxid(hwdesc
, adapter
->portnum
);
1014 hwdesc
->buffer1_length
= cpu_to_le16(first_seg_len
);
1015 hwdesc
->addr_buffer1
= cpu_to_le64(buffrag
->dma
);
1017 for (i
= 1, k
= 1; i
< frag_count
; i
++, k
++) {
1018 struct skb_frag_struct
*frag
;
1020 unsigned long offset
;
1021 dma_addr_t temp_dma
;
1023 /* move to next desc. if there is a need */
1024 if ((i
& 0x3) == 0) {
1026 producer
= get_next_index(producer
, num_txd
);
1027 hwdesc
= &hw
->cmd_desc_head
[producer
];
1028 memset(hwdesc
, 0, sizeof(struct cmd_desc_type0
));
1029 pbuf
= &adapter
->cmd_buf_arr
[producer
];
1032 frag
= &skb_shinfo(skb
)->frags
[i
- 1];
1034 offset
= frag
->page_offset
;
1037 temp_dma
= pci_map_page(adapter
->pdev
, frag
->page
, offset
,
1038 len
, PCI_DMA_TODEVICE
);
1041 buffrag
->dma
= temp_dma
;
1042 buffrag
->length
= temp_len
;
1046 hwdesc
->buffer1_length
= cpu_to_le16(temp_len
);
1047 hwdesc
->addr_buffer1
= cpu_to_le64(temp_dma
);
1050 hwdesc
->buffer2_length
= cpu_to_le16(temp_len
);
1051 hwdesc
->addr_buffer2
= cpu_to_le64(temp_dma
);
1054 hwdesc
->buffer3_length
= cpu_to_le16(temp_len
);
1055 hwdesc
->addr_buffer3
= cpu_to_le64(temp_dma
);
1058 hwdesc
->buffer4_length
= cpu_to_le16(temp_len
);
1059 hwdesc
->addr_buffer4
= cpu_to_le64(temp_dma
);
1064 producer
= get_next_index(producer
, num_txd
);
1066 /* might change opcode to TX_TCP_LSO */
1067 netxen_tso_check(adapter
, &hw
->cmd_desc_head
[saved_producer
], skb
);
1069 /* For LSO, we need to copy the MAC/IP/TCP headers into
1070 * the descriptor ring
1072 if (netxen_get_cmd_desc_opcode(&hw
->cmd_desc_head
[saved_producer
])
1074 int hdr_len
, first_hdr_len
, more_hdr
;
1075 hdr_len
= hw
->cmd_desc_head
[saved_producer
].total_hdr_length
;
1076 if (hdr_len
> (sizeof(struct cmd_desc_type0
) - 2)) {
1077 first_hdr_len
= sizeof(struct cmd_desc_type0
) - 2;
1080 first_hdr_len
= hdr_len
;
1083 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1084 hwdesc
= &hw
->cmd_desc_head
[producer
];
1085 pbuf
= &adapter
->cmd_buf_arr
[producer
];
1088 /* copy the first 64 bytes */
1089 memcpy(((void *)hwdesc
) + 2,
1090 (void *)(skb
->data
), first_hdr_len
);
1091 producer
= get_next_index(producer
, num_txd
);
1094 hwdesc
= &hw
->cmd_desc_head
[producer
];
1095 pbuf
= &adapter
->cmd_buf_arr
[producer
];
1097 /* copy the next 64 bytes - should be enough except
1098 * for pathological case
1100 skb_copy_from_linear_data_offset(skb
, first_hdr_len
,
1104 producer
= get_next_index(producer
, num_txd
);
1108 adapter
->cmd_producer
= producer
;
1109 adapter
->stats
.txbytes
+= skb
->len
;
1111 netxen_nic_update_cmd_producer(adapter
, adapter
->cmd_producer
);
1113 adapter
->stats
.xmitcalled
++;
1114 netdev
->trans_start
= jiffies
;
1116 return NETDEV_TX_OK
;
1119 static void netxen_watchdog(unsigned long v
)
1121 struct netxen_adapter
*adapter
= (struct netxen_adapter
*)v
;
1123 SCHEDULE_WORK(&adapter
->watchdog_task
);
1126 static void netxen_tx_timeout(struct net_device
*netdev
)
1128 struct netxen_adapter
*adapter
= (struct netxen_adapter
*)
1129 netdev_priv(netdev
);
1130 SCHEDULE_WORK(&adapter
->tx_timeout_task
);
1133 static void netxen_tx_timeout_task(struct work_struct
*work
)
1135 struct netxen_adapter
*adapter
=
1136 container_of(work
, struct netxen_adapter
, tx_timeout_task
);
1138 printk(KERN_ERR
"%s %s: transmit timeout, resetting.\n",
1139 netxen_nic_driver_name
, adapter
->netdev
->name
);
1141 netxen_nic_disable_int(adapter
);
1142 napi_disable(&adapter
->napi
);
1144 adapter
->netdev
->trans_start
= jiffies
;
1146 napi_enable(&adapter
->napi
);
1147 netxen_nic_enable_int(adapter
);
1148 netif_wake_queue(adapter
->netdev
);
1152 netxen_handle_int(struct netxen_adapter
*adapter
)
1154 netxen_nic_disable_int(adapter
);
1155 napi_schedule(&adapter
->napi
);
1158 irqreturn_t
netxen_intr(int irq
, void *data
)
1160 struct netxen_adapter
*adapter
= data
;
1163 our_int
= readl(NETXEN_CRB_NORMALIZE(adapter
, CRB_INT_VECTOR
));
1164 /* not our interrupt */
1165 if ((our_int
& (0x80 << adapter
->portnum
)) == 0)
1168 if (adapter
->intr_scheme
== INTR_SCHEME_PERPORT
) {
1169 /* claim interrupt */
1170 writel(our_int
& ~((u32
)(0x80 << adapter
->portnum
)),
1171 NETXEN_CRB_NORMALIZE(adapter
, CRB_INT_VECTOR
));
1174 netxen_handle_int(adapter
);
1179 irqreturn_t
netxen_msi_intr(int irq
, void *data
)
1181 struct netxen_adapter
*adapter
= data
;
1183 netxen_handle_int(adapter
);
1187 static int netxen_nic_poll(struct napi_struct
*napi
, int budget
)
1189 struct netxen_adapter
*adapter
= container_of(napi
, struct netxen_adapter
, napi
);
1194 tx_complete
= netxen_process_cmd_ring(adapter
);
1197 for (ctx
= 0; ctx
< MAX_RCV_CTX
; ++ctx
) {
1199 * Fairness issue. This will give undue weight to the
1200 * receive context 0.
1204 * To avoid starvation, we give each of our receivers,
1205 * a fraction of the quota. Sometimes, it might happen that we
1206 * have enough quota to process every packet, but since all the
1207 * packets are on one context, it gets only half of the quota,
1208 * and ends up not processing it.
1210 work_done
+= netxen_process_rcv_ring(adapter
, ctx
,
1211 budget
/ MAX_RCV_CTX
);
1214 if ((work_done
< budget
) && tx_complete
) {
1215 netif_rx_complete(adapter
->netdev
, &adapter
->napi
);
1216 netxen_nic_enable_int(adapter
);
1222 #ifdef CONFIG_NET_POLL_CONTROLLER
1223 static void netxen_nic_poll_controller(struct net_device
*netdev
)
1225 struct netxen_adapter
*adapter
= netdev_priv(netdev
);
1226 disable_irq(adapter
->irq
);
1227 netxen_intr(adapter
->irq
, adapter
);
1228 enable_irq(adapter
->irq
);
1232 static struct pci_driver netxen_driver
= {
1233 .name
= netxen_nic_driver_name
,
1234 .id_table
= netxen_pci_tbl
,
1235 .probe
= netxen_nic_probe
,
1236 .remove
= __devexit_p(netxen_nic_remove
)
1239 /* Driver Registration on NetXen card */
1241 static int __init
netxen_init_module(void)
1243 if ((netxen_workq
= create_singlethread_workqueue("netxen")) == NULL
)
1246 return pci_register_driver(&netxen_driver
);
1249 module_init(netxen_init_module
);
1251 static void __exit
netxen_exit_module(void)
1254 * Wait for some time to allow the dma to drain, if any.
1257 pci_unregister_driver(&netxen_driver
);
1258 destroy_workqueue(netxen_workq
);
1261 module_exit(netxen_exit_module
);