2 * Copyright (C) 2003 - 2009 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:
27 * Cupertino, CA 95014-0701
31 #include <linux/netdevice.h>
32 #include <linux/delay.h>
33 #include "netxen_nic.h"
34 #include "netxen_nic_hw.h"
35 #include "netxen_nic_phan_reg.h"
37 struct crb_addr_pair {
42 #define NETXEN_MAX_CRB_XFORM 60
43 static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
44 #define NETXEN_ADDR_ERROR (0xffffffff)
46 #define crb_addr_transform(name) \
47 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
48 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
50 #define NETXEN_NIC_XDMA_RESET 0x8000ff
53 netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
54 struct nx_host_rds_ring *rds_ring);
56 static void crb_addr_transform_setup(void)
58 crb_addr_transform(XDMA);
59 crb_addr_transform(TIMR);
60 crb_addr_transform(SRE);
61 crb_addr_transform(SQN3);
62 crb_addr_transform(SQN2);
63 crb_addr_transform(SQN1);
64 crb_addr_transform(SQN0);
65 crb_addr_transform(SQS3);
66 crb_addr_transform(SQS2);
67 crb_addr_transform(SQS1);
68 crb_addr_transform(SQS0);
69 crb_addr_transform(RPMX7);
70 crb_addr_transform(RPMX6);
71 crb_addr_transform(RPMX5);
72 crb_addr_transform(RPMX4);
73 crb_addr_transform(RPMX3);
74 crb_addr_transform(RPMX2);
75 crb_addr_transform(RPMX1);
76 crb_addr_transform(RPMX0);
77 crb_addr_transform(ROMUSB);
78 crb_addr_transform(SN);
79 crb_addr_transform(QMN);
80 crb_addr_transform(QMS);
81 crb_addr_transform(PGNI);
82 crb_addr_transform(PGND);
83 crb_addr_transform(PGN3);
84 crb_addr_transform(PGN2);
85 crb_addr_transform(PGN1);
86 crb_addr_transform(PGN0);
87 crb_addr_transform(PGSI);
88 crb_addr_transform(PGSD);
89 crb_addr_transform(PGS3);
90 crb_addr_transform(PGS2);
91 crb_addr_transform(PGS1);
92 crb_addr_transform(PGS0);
93 crb_addr_transform(PS);
94 crb_addr_transform(PH);
95 crb_addr_transform(NIU);
96 crb_addr_transform(I2Q);
97 crb_addr_transform(EG);
98 crb_addr_transform(MN);
99 crb_addr_transform(MS);
100 crb_addr_transform(CAS2);
101 crb_addr_transform(CAS1);
102 crb_addr_transform(CAS0);
103 crb_addr_transform(CAM);
104 crb_addr_transform(C2C1);
105 crb_addr_transform(C2C0);
106 crb_addr_transform(SMB);
107 crb_addr_transform(OCM0);
108 crb_addr_transform(I2C0);
111 void netxen_release_rx_buffers(struct netxen_adapter *adapter)
113 struct netxen_recv_context *recv_ctx;
114 struct nx_host_rds_ring *rds_ring;
115 struct netxen_rx_buffer *rx_buf;
118 recv_ctx = &adapter->recv_ctx;
119 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
120 rds_ring = &recv_ctx->rds_rings[ring];
121 for (i = 0; i < rds_ring->num_desc; ++i) {
122 rx_buf = &(rds_ring->rx_buf_arr[i]);
123 if (rx_buf->state == NETXEN_BUFFER_FREE)
125 pci_unmap_single(adapter->pdev,
129 if (rx_buf->skb != NULL)
130 dev_kfree_skb_any(rx_buf->skb);
135 void netxen_release_tx_buffers(struct netxen_adapter *adapter)
137 struct netxen_cmd_buffer *cmd_buf;
138 struct netxen_skb_frag *buffrag;
140 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
142 cmd_buf = tx_ring->cmd_buf_arr;
143 for (i = 0; i < tx_ring->num_desc; i++) {
144 buffrag = cmd_buf->frag_array;
146 pci_unmap_single(adapter->pdev, buffrag->dma,
147 buffrag->length, PCI_DMA_TODEVICE);
150 for (j = 0; j < cmd_buf->frag_count; j++) {
153 pci_unmap_page(adapter->pdev, buffrag->dma,
160 dev_kfree_skb_any(cmd_buf->skb);
167 void netxen_free_sw_resources(struct netxen_adapter *adapter)
169 struct netxen_recv_context *recv_ctx;
170 struct nx_host_rds_ring *rds_ring;
171 struct nx_host_tx_ring *tx_ring;
174 recv_ctx = &adapter->recv_ctx;
176 if (recv_ctx->rds_rings == NULL)
179 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
180 rds_ring = &recv_ctx->rds_rings[ring];
181 vfree(rds_ring->rx_buf_arr);
182 rds_ring->rx_buf_arr = NULL;
184 kfree(recv_ctx->rds_rings);
187 if (adapter->tx_ring == NULL)
190 tx_ring = adapter->tx_ring;
191 vfree(tx_ring->cmd_buf_arr);
194 int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
196 struct netxen_recv_context *recv_ctx;
197 struct nx_host_rds_ring *rds_ring;
198 struct nx_host_sds_ring *sds_ring;
199 struct nx_host_tx_ring *tx_ring;
200 struct netxen_rx_buffer *rx_buf;
203 struct netxen_cmd_buffer *cmd_buf_arr;
204 struct net_device *netdev = adapter->netdev;
205 struct pci_dev *pdev = adapter->pdev;
207 size = sizeof(struct nx_host_tx_ring);
208 tx_ring = kzalloc(size, GFP_KERNEL);
209 if (tx_ring == NULL) {
210 dev_err(&pdev->dev, "%s: failed to allocate tx ring struct\n",
214 adapter->tx_ring = tx_ring;
216 tx_ring->num_desc = adapter->num_txd;
218 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
219 if (cmd_buf_arr == NULL) {
220 dev_err(&pdev->dev, "%s: failed to allocate cmd buffer ring\n",
224 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
225 tx_ring->cmd_buf_arr = cmd_buf_arr;
227 recv_ctx = &adapter->recv_ctx;
229 size = adapter->max_rds_rings * sizeof (struct nx_host_rds_ring);
230 rds_ring = kzalloc(size, GFP_KERNEL);
231 if (rds_ring == NULL) {
232 dev_err(&pdev->dev, "%s: failed to allocate rds ring struct\n",
236 recv_ctx->rds_rings = rds_ring;
238 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
239 rds_ring = &recv_ctx->rds_rings[ring];
241 case RCV_RING_NORMAL:
242 rds_ring->num_desc = adapter->num_rxd;
243 if (adapter->ahw.cut_through) {
245 NX_CT_DEFAULT_RX_BUF_LEN;
247 NX_CT_DEFAULT_RX_BUF_LEN;
249 rds_ring->dma_size = RX_DMA_MAP_LEN;
251 MAX_RX_BUFFER_LENGTH;
256 rds_ring->num_desc = adapter->num_jumbo_rxd;
257 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
259 NX_P3_RX_JUMBO_BUF_MAX_LEN;
262 NX_P2_RX_JUMBO_BUF_MAX_LEN;
264 rds_ring->dma_size + NET_IP_ALIGN;
268 rds_ring->num_desc = adapter->num_lro_rxd;
269 rds_ring->dma_size = RX_LRO_DMA_MAP_LEN;
270 rds_ring->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
274 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
275 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
276 if (rds_ring->rx_buf_arr == NULL) {
277 printk(KERN_ERR "%s: Failed to allocate "
278 "rx buffer ring %d\n",
280 /* free whatever was already allocated */
283 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
284 INIT_LIST_HEAD(&rds_ring->free_list);
286 * Now go through all of them, set reference handles
287 * and put them in the queues.
289 rx_buf = rds_ring->rx_buf_arr;
290 for (i = 0; i < rds_ring->num_desc; i++) {
291 list_add_tail(&rx_buf->list,
292 &rds_ring->free_list);
293 rx_buf->ref_handle = i;
294 rx_buf->state = NETXEN_BUFFER_FREE;
297 spin_lock_init(&rds_ring->lock);
300 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
301 sds_ring = &recv_ctx->sds_rings[ring];
302 sds_ring->irq = adapter->msix_entries[ring].vector;
303 sds_ring->adapter = adapter;
304 sds_ring->num_desc = adapter->num_rxd;
306 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
307 INIT_LIST_HEAD(&sds_ring->free_list[i]);
313 netxen_free_sw_resources(adapter);
317 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
319 adapter->macaddr_set = netxen_p2_nic_set_mac_addr;
320 adapter->set_multi = netxen_p2_nic_set_multi;
322 switch (adapter->ahw.port_type) {
324 adapter->enable_phy_interrupts =
325 netxen_niu_gbe_enable_phy_interrupts;
326 adapter->disable_phy_interrupts =
327 netxen_niu_gbe_disable_phy_interrupts;
328 adapter->set_mtu = netxen_nic_set_mtu_gb;
329 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
330 adapter->phy_read = netxen_niu_gbe_phy_read;
331 adapter->phy_write = netxen_niu_gbe_phy_write;
332 adapter->init_port = netxen_niu_gbe_init_port;
333 adapter->stop_port = netxen_niu_disable_gbe_port;
336 case NETXEN_NIC_XGBE:
337 adapter->enable_phy_interrupts =
338 netxen_niu_xgbe_enable_phy_interrupts;
339 adapter->disable_phy_interrupts =
340 netxen_niu_xgbe_disable_phy_interrupts;
341 adapter->set_mtu = netxen_nic_set_mtu_xgb;
342 adapter->init_port = netxen_niu_xg_init_port;
343 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
344 adapter->stop_port = netxen_niu_disable_xg_port;
351 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
352 adapter->set_mtu = nx_fw_cmd_set_mtu;
353 adapter->set_promisc = netxen_p3_nic_set_promisc;
354 adapter->macaddr_set = netxen_p3_nic_set_mac_addr;
355 adapter->set_multi = netxen_p3_nic_set_multi;
360 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
361 * address to external PCI CRB address.
363 static u32 netxen_decode_crb_addr(u32 addr)
366 u32 base_addr, offset, pci_base;
368 crb_addr_transform_setup();
370 pci_base = NETXEN_ADDR_ERROR;
371 base_addr = addr & 0xfff00000;
372 offset = addr & 0x000fffff;
374 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
375 if (crb_addr_xform[i] == base_addr) {
380 if (pci_base == NETXEN_ADDR_ERROR)
383 return (pci_base + offset);
386 static long rom_max_timeout = 100;
387 static long rom_lock_timeout = 10000;
389 static int rom_lock(struct netxen_adapter *adapter)
396 /* acquire semaphore2 from PCI HW block */
397 done = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK));
400 if (timeout >= rom_lock_timeout)
410 for (iter = 0; iter < 20; iter++)
411 cpu_relax(); /*This a nop instr on i386 */
414 NXWR32(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
418 static int netxen_wait_rom_done(struct netxen_adapter *adapter)
426 done = NXRD32(adapter, NETXEN_ROMUSB_GLB_STATUS);
429 if (timeout >= rom_max_timeout) {
430 printk("Timeout reached waiting for rom done");
437 static void netxen_rom_unlock(struct netxen_adapter *adapter)
439 /* release semaphore2 */
440 NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK));
444 static int do_rom_fast_read(struct netxen_adapter *adapter,
447 NXWR32(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
448 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
449 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
450 NXWR32(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
451 if (netxen_wait_rom_done(adapter)) {
452 printk("Error waiting for rom done\n");
455 /* reset abyte_cnt and dummy_byte_cnt */
456 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
458 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
460 *valp = NXRD32(adapter, NETXEN_ROMUSB_ROM_RDATA);
464 static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
465 u8 *bytes, size_t size)
470 for (addridx = addr; addridx < (addr + size); addridx += 4) {
472 ret = do_rom_fast_read(adapter, addridx, &v);
475 *(__le32 *)bytes = cpu_to_le32(v);
483 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
484 u8 *bytes, size_t size)
488 ret = rom_lock(adapter);
492 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
494 netxen_rom_unlock(adapter);
498 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
502 if (rom_lock(adapter) != 0)
505 ret = do_rom_fast_read(adapter, addr, valp);
506 netxen_rom_unlock(adapter);
510 #define NETXEN_BOARDTYPE 0x4008
511 #define NETXEN_BOARDNUM 0x400c
512 #define NETXEN_CHIPNUM 0x4010
514 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
517 int i, n, init_delay = 0;
518 struct crb_addr_pair *buf;
524 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0xffffffff);
525 netxen_rom_unlock(adapter);
528 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
529 printk("P2 ROM board type: 0x%08x\n", val);
531 printk("Could not read board type\n");
532 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
533 printk("P2 ROM board num: 0x%08x\n", val);
535 printk("Could not read board number\n");
536 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
537 printk("P2 ROM chip num: 0x%08x\n", val);
539 printk("Could not read chip number\n");
542 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
543 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
545 netxen_rom_fast_read(adapter, 4, &n) != 0) {
546 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
547 "n: %08x\n", netxen_nic_driver_name, n);
550 offset = n & 0xffffU;
551 n = (n >> 16) & 0xffffU;
553 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
555 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
556 "n: %08x\n", netxen_nic_driver_name, n);
565 printk(KERN_DEBUG "%s: %d CRB init values found"
566 " in ROM.\n", netxen_nic_driver_name, n);
568 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
569 " initialized.\n", __func__, n);
573 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
575 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
576 netxen_nic_driver_name);
579 for (i = 0; i < n; i++) {
580 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
581 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
590 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
591 netxen_nic_driver_name,
592 (u32)netxen_decode_crb_addr(addr), val);
594 for (i = 0; i < n; i++) {
596 off = netxen_decode_crb_addr(buf[i].addr);
597 if (off == NETXEN_ADDR_ERROR) {
598 printk(KERN_ERR"CRB init value out of range %x\n",
602 off += NETXEN_PCI_CRBSPACE;
603 /* skipping cold reboot MAGIC */
604 if (off == NETXEN_CAM_RAM(0x1fc))
607 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
608 /* do not reset PCI */
609 if (off == (ROMUSB_GLB + 0xbc))
611 if (off == (ROMUSB_GLB + 0xa8))
613 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
615 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
617 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
619 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
620 buf[i].data = 0x1020;
621 /* skip the function enable register */
622 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
624 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
626 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
630 if (off == NETXEN_ADDR_ERROR) {
631 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
632 netxen_nic_driver_name, buf[i].addr);
637 /* After writing this register, HW needs time for CRB */
638 /* to quiet down (else crb_window returns 0xffffffff) */
639 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
641 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
642 /* hold xdma in reset also */
643 buf[i].data = NETXEN_NIC_XDMA_RESET;
644 buf[i].data = 0x8000ff;
648 NXWR32(adapter, off, buf[i].data);
654 /* disable_peg_cache_all */
656 /* unreset_net_cache */
657 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
658 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
659 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
662 /* p2dn replyCount */
663 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
664 /* disable_peg_cache 0 */
665 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
666 /* disable_peg_cache 1 */
667 NXWR32(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
672 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
673 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
675 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
676 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
678 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
679 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
681 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
682 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
687 netxen_load_firmware(struct netxen_adapter *adapter)
690 u32 i, flashaddr, size;
691 const struct firmware *fw = adapter->fw;
693 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
694 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 1);
699 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
701 ptr64 = (u64 *)&fw->data[NETXEN_BOOTLD_START];
702 flashaddr = NETXEN_BOOTLD_START;
704 for (i = 0; i < size; i++) {
705 data = cpu_to_le64(ptr64[i]);
706 adapter->pci_mem_write(adapter, flashaddr, &data, 8);
710 size = *(u32 *)&fw->data[NX_FW_SIZE_OFFSET];
711 size = (__force u32)cpu_to_le32(size) / 8;
713 ptr64 = (u64 *)&fw->data[NETXEN_IMAGE_START];
714 flashaddr = NETXEN_IMAGE_START;
716 for (i = 0; i < size; i++) {
717 data = cpu_to_le64(ptr64[i]);
719 if (adapter->pci_mem_write(adapter,
720 flashaddr, &data, 8))
728 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 4;
729 flashaddr = NETXEN_BOOTLD_START;
731 for (i = 0; i < size; i++) {
732 if (netxen_rom_fast_read(adapter,
733 flashaddr, (int *)&data) != 0)
736 if (adapter->pci_mem_write(adapter,
737 flashaddr, &data, 4))
745 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
746 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001d);
748 NXWR32(adapter, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL, 0x3fff);
749 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 0);
756 netxen_validate_firmware(struct netxen_adapter *adapter, const char *fwname)
759 u32 major, minor, build, ver, min_ver, bios;
760 struct pci_dev *pdev = adapter->pdev;
761 const struct firmware *fw = adapter->fw;
763 if (fw->size < NX_FW_MIN_SIZE)
766 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_MAGIC_OFFSET]);
767 if ((__force u32)val != NETXEN_BDINFO_MAGIC)
770 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
771 major = (__force u32)val & 0xff;
772 minor = ((__force u32)val >> 8) & 0xff;
773 build = (__force u32)val >> 16;
775 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
776 min_ver = NETXEN_VERSION_CODE(4, 0, 216);
778 min_ver = NETXEN_VERSION_CODE(3, 4, 216);
780 ver = NETXEN_VERSION_CODE(major, minor, build);
782 if ((major > _NETXEN_NIC_LINUX_MAJOR) || (ver < min_ver)) {
784 "%s: firmware version %d.%d.%d unsupported\n",
785 fwname, major, minor, build);
789 val = cpu_to_le32(*(u32 *)&fw->data[NX_BIOS_VERSION_OFFSET]);
790 netxen_rom_fast_read(adapter, NX_BIOS_VERSION_OFFSET, (int *)&bios);
791 if ((__force u32)val != bios) {
792 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
797 /* check if flashed firmware is newer */
798 if (netxen_rom_fast_read(adapter,
799 NX_FW_VERSION_OFFSET, (int *)&val))
801 major = (__force u32)val & 0xff;
802 minor = ((__force u32)val >> 8) & 0xff;
803 build = (__force u32)val >> 16;
804 if (NETXEN_VERSION_CODE(major, minor, build) > ver)
807 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
811 static char *fw_name[] = { "nxromimg.bin", "nx3fwct.bin", "nx3fwmn.bin" };
813 void netxen_request_firmware(struct netxen_adapter *adapter)
815 u32 capability, flashed_ver;
817 struct pci_dev *pdev = adapter->pdev;
820 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
821 fw_type = NX_P2_MN_ROMIMAGE;
824 fw_type = NX_P3_CT_ROMIMAGE;
831 netxen_rom_fast_read(adapter,
832 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
833 if (flashed_ver >= NETXEN_VERSION_CODE(4, 0, 220)) {
834 capability = NXRD32(adapter, NX_PEG_TUNE_CAPABILITY);
835 if (capability & NX_PEG_TUNE_MN_PRESENT) {
836 fw_type = NX_P3_MN_ROMIMAGE;
842 rc = request_firmware(&adapter->fw, fw_name[fw_type], &pdev->dev);
844 if (fw_type == NX_P3_CT_ROMIMAGE) {
853 rc = netxen_validate_firmware(adapter, fw_name[fw_type]);
855 release_firmware(adapter->fw);
857 if (fw_type == NX_P3_CT_ROMIMAGE) {
868 dev_info(&pdev->dev, "loading firmware from file %s\n",
871 dev_info(&pdev->dev, "loading firmware from flash\n");
876 netxen_release_firmware(struct netxen_adapter *adapter)
879 release_firmware(adapter->fw);
882 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
888 adapter->dummy_dma.addr =
889 pci_alloc_consistent(adapter->pdev,
890 NETXEN_HOST_DUMMY_DMA_SIZE,
891 &adapter->dummy_dma.phys_addr);
892 if (adapter->dummy_dma.addr == NULL) {
893 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
898 addr = (uint64_t) adapter->dummy_dma.phys_addr;
899 hi = (addr >> 32) & 0xffffffff;
900 lo = addr & 0xffffffff;
902 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
903 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
905 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
907 NXWR32(adapter, CRB_HOST_DUMMY_BUF, temp);
913 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
917 if (!adapter->dummy_dma.addr)
920 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
922 if (dma_watchdog_shutdown_request(adapter) == 1)
925 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
931 pci_free_consistent(adapter->pdev,
932 NETXEN_HOST_DUMMY_DMA_SIZE,
933 adapter->dummy_dma.addr,
934 adapter->dummy_dma.phys_addr);
935 adapter->dummy_dma.addr = NULL;
937 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
938 adapter->netdev->name);
942 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
951 val = NXRD32(adapter, CRB_CMDPEG_STATE);
954 case PHAN_INITIALIZE_COMPLETE:
955 case PHAN_INITIALIZE_ACK:
957 case PHAN_INITIALIZE_FAILED:
967 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
970 dev_warn(&adapter->pdev->dev, "firmware init failed\n");
975 netxen_receive_peg_ready(struct netxen_adapter *adapter)
981 val = NXRD32(adapter, CRB_RCVPEG_STATE);
983 if (val == PHAN_PEG_RCV_INITIALIZED)
991 printk(KERN_ERR "Receive Peg initialization not "
992 "complete, state: 0x%x.\n", val);
999 int netxen_init_firmware(struct netxen_adapter *adapter)
1003 err = netxen_receive_peg_ready(adapter);
1007 NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1008 NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1009 NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1010 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
1012 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222)) {
1013 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
1020 netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
1025 u8 link_status, module, duplex, autoneg;
1026 struct net_device *netdev = adapter->netdev;
1028 adapter->has_link_events = 1;
1030 cable_OUI = msg->body[1] & 0xffffffff;
1031 cable_len = (msg->body[1] >> 32) & 0xffff;
1032 link_speed = (msg->body[1] >> 48) & 0xffff;
1034 link_status = msg->body[2] & 0xff;
1035 duplex = (msg->body[2] >> 16) & 0xff;
1036 autoneg = (msg->body[2] >> 24) & 0xff;
1038 module = (msg->body[2] >> 8) & 0xff;
1039 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
1040 printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
1041 netdev->name, cable_OUI, cable_len);
1042 } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
1043 printk(KERN_INFO "%s: unsupported cable length %d\n",
1044 netdev->name, cable_len);
1047 netxen_advert_link_change(adapter, link_status);
1049 /* update link parameters */
1050 if (duplex == LINKEVENT_FULL_DUPLEX)
1051 adapter->link_duplex = DUPLEX_FULL;
1053 adapter->link_duplex = DUPLEX_HALF;
1054 adapter->module_type = module;
1055 adapter->link_autoneg = autoneg;
1056 adapter->link_speed = link_speed;
1060 netxen_handle_fw_message(int desc_cnt, int index,
1061 struct nx_host_sds_ring *sds_ring)
1064 struct status_desc *desc;
1067 while (desc_cnt > 0 && i < 8) {
1068 desc = &sds_ring->desc_head[index];
1069 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1070 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1072 index = get_next_index(index, sds_ring->num_desc);
1076 opcode = netxen_get_nic_msg_opcode(msg.body[0]);
1078 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1079 netxen_handle_linkevent(sds_ring->adapter, &msg);
1087 netxen_alloc_rx_skb(struct netxen_adapter *adapter,
1088 struct nx_host_rds_ring *rds_ring,
1089 struct netxen_rx_buffer *buffer)
1091 struct sk_buff *skb;
1093 struct pci_dev *pdev = adapter->pdev;
1095 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1101 if (!adapter->ahw.cut_through)
1102 skb_reserve(skb, 2);
1104 dma = pci_map_single(pdev, skb->data,
1105 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1107 if (pci_dma_mapping_error(pdev, dma)) {
1108 dev_kfree_skb_any(skb);
1115 buffer->state = NETXEN_BUFFER_BUSY;
1120 static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1121 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1123 struct netxen_rx_buffer *buffer;
1124 struct sk_buff *skb;
1126 buffer = &rds_ring->rx_buf_arr[index];
1128 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1129 PCI_DMA_FROMDEVICE);
1135 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1136 adapter->stats.csummed++;
1137 skb->ip_summed = CHECKSUM_UNNECESSARY;
1139 skb->ip_summed = CHECKSUM_NONE;
1141 skb->dev = adapter->netdev;
1145 buffer->state = NETXEN_BUFFER_FREE;
1149 static struct netxen_rx_buffer *
1150 netxen_process_rcv(struct netxen_adapter *adapter,
1151 int ring, int index, int length, int cksum, int pkt_offset,
1152 struct nx_host_sds_ring *sds_ring)
1154 struct net_device *netdev = adapter->netdev;
1155 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1156 struct netxen_rx_buffer *buffer;
1157 struct sk_buff *skb;
1158 struct nx_host_rds_ring *rds_ring = &recv_ctx->rds_rings[ring];
1160 if (unlikely(index > rds_ring->num_desc))
1163 buffer = &rds_ring->rx_buf_arr[index];
1165 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1169 if (length > rds_ring->skb_size)
1170 skb_put(skb, rds_ring->skb_size);
1172 skb_put(skb, length);
1176 skb_pull(skb, pkt_offset);
1178 skb->protocol = eth_type_trans(skb, netdev);
1180 napi_gro_receive(&sds_ring->napi, skb);
1182 adapter->stats.no_rcv++;
1183 adapter->stats.rxbytes += length;
1188 #define netxen_merge_rx_buffers(list, head) \
1189 do { list_splice_tail_init(list, head); } while (0);
1192 netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
1194 struct netxen_adapter *adapter = sds_ring->adapter;
1196 struct list_head *cur;
1198 struct status_desc *desc;
1199 struct netxen_rx_buffer *rxbuf;
1201 u32 consumer = sds_ring->consumer;
1205 int opcode, ring, index, length, cksum, pkt_offset, desc_cnt;
1207 while (count < max) {
1208 desc = &sds_ring->desc_head[consumer];
1209 sts_data = le64_to_cpu(desc->status_desc_data[0]);
1211 if (!(sts_data & STATUS_OWNER_HOST))
1214 desc_cnt = netxen_get_sts_desc_cnt(sts_data);
1215 ring = netxen_get_sts_type(sts_data);
1217 if (ring > RCV_RING_JUMBO)
1220 opcode = netxen_get_sts_opcode(sts_data);
1223 case NETXEN_NIC_RXPKT_DESC:
1224 case NETXEN_OLD_RXPKT_DESC:
1226 case NETXEN_NIC_RESPONSE_DESC:
1227 netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
1232 WARN_ON(desc_cnt > 1);
1234 index = netxen_get_sts_refhandle(sts_data);
1235 length = netxen_get_sts_totallength(sts_data);
1236 cksum = netxen_get_sts_status(sts_data);
1237 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1239 rxbuf = netxen_process_rcv(adapter, ring, index,
1240 length, cksum, pkt_offset, sds_ring);
1243 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1246 for (; desc_cnt > 0; desc_cnt--) {
1247 desc = &sds_ring->desc_head[consumer];
1248 desc->status_desc_data[0] =
1249 cpu_to_le64(STATUS_OWNER_PHANTOM);
1250 consumer = get_next_index(consumer, sds_ring->num_desc);
1255 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1256 struct nx_host_rds_ring *rds_ring =
1257 &adapter->recv_ctx.rds_rings[ring];
1259 if (!list_empty(&sds_ring->free_list[ring])) {
1260 list_for_each(cur, &sds_ring->free_list[ring]) {
1261 rxbuf = list_entry(cur,
1262 struct netxen_rx_buffer, list);
1263 netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
1265 spin_lock(&rds_ring->lock);
1266 netxen_merge_rx_buffers(&sds_ring->free_list[ring],
1267 &rds_ring->free_list);
1268 spin_unlock(&rds_ring->lock);
1271 netxen_post_rx_buffers_nodb(adapter, rds_ring);
1275 sds_ring->consumer = consumer;
1276 NXWR32(adapter, sds_ring->crb_sts_consumer, consumer);
1282 /* Process Command status ring */
1283 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1285 u32 sw_consumer, hw_consumer;
1287 struct netxen_cmd_buffer *buffer;
1288 struct pci_dev *pdev = adapter->pdev;
1289 struct net_device *netdev = adapter->netdev;
1290 struct netxen_skb_frag *frag;
1292 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1294 if (!spin_trylock(&adapter->tx_clean_lock))
1297 sw_consumer = tx_ring->sw_consumer;
1298 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1300 while (sw_consumer != hw_consumer) {
1301 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1303 frag = &buffer->frag_array[0];
1304 pci_unmap_single(pdev, frag->dma, frag->length,
1307 for (i = 1; i < buffer->frag_count; i++) {
1308 frag++; /* Get the next frag */
1309 pci_unmap_page(pdev, frag->dma, frag->length,
1314 adapter->stats.xmitfinished++;
1315 dev_kfree_skb_any(buffer->skb);
1319 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
1320 if (++count >= MAX_STATUS_HANDLE)
1324 if (count && netif_running(netdev)) {
1325 tx_ring->sw_consumer = sw_consumer;
1329 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
1330 netif_tx_lock(netdev);
1331 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
1332 netif_wake_queue(netdev);
1333 netif_tx_unlock(netdev);
1337 * If everything is freed up to consumer then check if the ring is full
1338 * If the ring is full then check if more needs to be freed and
1339 * schedule the call back again.
1341 * This happens when there are 2 CPUs. One could be freeing and the
1342 * other filling it. If the ring is full when we get out of here and
1343 * the card has already interrupted the host then the host can miss the
1346 * There is still a possible race condition and the host could miss an
1347 * interrupt. The card has to take care of this.
1349 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1350 done = (sw_consumer == hw_consumer);
1351 spin_unlock(&adapter->tx_clean_lock);
1357 netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
1358 struct nx_host_rds_ring *rds_ring)
1360 struct rcv_desc *pdesc;
1361 struct netxen_rx_buffer *buffer;
1362 int producer, count = 0;
1363 netxen_ctx_msg msg = 0;
1364 struct list_head *head;
1366 producer = rds_ring->producer;
1368 spin_lock(&rds_ring->lock);
1369 head = &rds_ring->free_list;
1370 while (!list_empty(head)) {
1372 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1375 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1380 list_del(&buffer->list);
1382 /* make a rcv descriptor */
1383 pdesc = &rds_ring->desc_head[producer];
1384 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1385 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1386 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1388 producer = get_next_index(producer, rds_ring->num_desc);
1390 spin_unlock(&rds_ring->lock);
1393 rds_ring->producer = producer;
1394 NXWR32(adapter, rds_ring->crb_rcv_producer,
1395 (producer-1) & (rds_ring->num_desc-1));
1397 if (adapter->fw_major < 4) {
1399 * Write a doorbell msg to tell phanmon of change in
1400 * receive ring producer
1401 * Only for firmware version < 4.0.0
1403 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1404 netxen_set_msg_privid(msg);
1405 netxen_set_msg_count(msg,
1407 (rds_ring->num_desc - 1)));
1408 netxen_set_msg_ctxid(msg, adapter->portnum);
1409 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1411 DB_NORMALIZE(adapter,
1412 NETXEN_RCV_PRODUCER_OFFSET));
1418 netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1419 struct nx_host_rds_ring *rds_ring)
1421 struct rcv_desc *pdesc;
1422 struct netxen_rx_buffer *buffer;
1423 int producer, count = 0;
1424 struct list_head *head;
1426 producer = rds_ring->producer;
1427 if (!spin_trylock(&rds_ring->lock))
1430 head = &rds_ring->free_list;
1431 while (!list_empty(head)) {
1433 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1436 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1441 list_del(&buffer->list);
1443 /* make a rcv descriptor */
1444 pdesc = &rds_ring->desc_head[producer];
1445 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1446 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1447 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1449 producer = get_next_index(producer, rds_ring->num_desc);
1453 rds_ring->producer = producer;
1454 NXWR32(adapter, rds_ring->crb_rcv_producer,
1455 (producer - 1) & (rds_ring->num_desc - 1));
1457 spin_unlock(&rds_ring->lock);
1460 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1462 memset(&adapter->stats, 0, sizeof(adapter->stats));