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 if (rds_ring->rx_buf_arr) {
182 vfree(rds_ring->rx_buf_arr);
183 rds_ring->rx_buf_arr = NULL;
186 kfree(recv_ctx->rds_rings);
189 if (adapter->tx_ring == NULL)
192 tx_ring = adapter->tx_ring;
193 if (tx_ring->cmd_buf_arr)
194 vfree(tx_ring->cmd_buf_arr);
197 int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
199 struct netxen_recv_context *recv_ctx;
200 struct nx_host_rds_ring *rds_ring;
201 struct nx_host_sds_ring *sds_ring;
202 struct nx_host_tx_ring *tx_ring;
203 struct netxen_rx_buffer *rx_buf;
206 struct netxen_cmd_buffer *cmd_buf_arr;
207 struct net_device *netdev = adapter->netdev;
208 struct pci_dev *pdev = adapter->pdev;
210 size = sizeof(struct nx_host_tx_ring);
211 tx_ring = kzalloc(size, GFP_KERNEL);
212 if (tx_ring == NULL) {
213 dev_err(&pdev->dev, "%s: failed to allocate tx ring struct\n",
217 adapter->tx_ring = tx_ring;
219 tx_ring->num_desc = adapter->num_txd;
221 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
222 if (cmd_buf_arr == NULL) {
223 dev_err(&pdev->dev, "%s: failed to allocate cmd buffer ring\n",
227 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
228 tx_ring->cmd_buf_arr = cmd_buf_arr;
230 recv_ctx = &adapter->recv_ctx;
232 size = adapter->max_rds_rings * sizeof (struct nx_host_rds_ring);
233 rds_ring = kzalloc(size, GFP_KERNEL);
234 if (rds_ring == NULL) {
235 dev_err(&pdev->dev, "%s: failed to allocate rds ring struct\n",
239 recv_ctx->rds_rings = rds_ring;
241 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
242 rds_ring = &recv_ctx->rds_rings[ring];
244 case RCV_RING_NORMAL:
245 rds_ring->num_desc = adapter->num_rxd;
246 if (adapter->ahw.cut_through) {
248 NX_CT_DEFAULT_RX_BUF_LEN;
250 NX_CT_DEFAULT_RX_BUF_LEN;
252 rds_ring->dma_size = RX_DMA_MAP_LEN;
254 MAX_RX_BUFFER_LENGTH;
259 rds_ring->num_desc = adapter->num_jumbo_rxd;
260 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
262 NX_P3_RX_JUMBO_BUF_MAX_LEN;
265 NX_P2_RX_JUMBO_BUF_MAX_LEN;
267 rds_ring->dma_size + NET_IP_ALIGN;
271 rds_ring->num_desc = adapter->num_lro_rxd;
272 rds_ring->dma_size = RX_LRO_DMA_MAP_LEN;
273 rds_ring->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
277 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
278 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
279 if (rds_ring->rx_buf_arr == NULL) {
280 printk(KERN_ERR "%s: Failed to allocate "
281 "rx buffer ring %d\n",
283 /* free whatever was already allocated */
286 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
287 INIT_LIST_HEAD(&rds_ring->free_list);
289 * Now go through all of them, set reference handles
290 * and put them in the queues.
292 rx_buf = rds_ring->rx_buf_arr;
293 for (i = 0; i < rds_ring->num_desc; i++) {
294 list_add_tail(&rx_buf->list,
295 &rds_ring->free_list);
296 rx_buf->ref_handle = i;
297 rx_buf->state = NETXEN_BUFFER_FREE;
300 spin_lock_init(&rds_ring->lock);
303 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
304 sds_ring = &recv_ctx->sds_rings[ring];
305 sds_ring->irq = adapter->msix_entries[ring].vector;
306 sds_ring->adapter = adapter;
307 sds_ring->num_desc = adapter->num_rxd;
309 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
310 INIT_LIST_HEAD(&sds_ring->free_list[i]);
316 netxen_free_sw_resources(adapter);
320 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
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->macaddr_set = netxen_niu_macaddr_set;
329 adapter->set_mtu = netxen_nic_set_mtu_gb;
330 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
331 adapter->phy_read = netxen_niu_gbe_phy_read;
332 adapter->phy_write = netxen_niu_gbe_phy_write;
333 adapter->init_port = netxen_niu_gbe_init_port;
334 adapter->stop_port = netxen_niu_disable_gbe_port;
337 case NETXEN_NIC_XGBE:
338 adapter->enable_phy_interrupts =
339 netxen_niu_xgbe_enable_phy_interrupts;
340 adapter->disable_phy_interrupts =
341 netxen_niu_xgbe_disable_phy_interrupts;
342 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
343 adapter->set_mtu = netxen_nic_set_mtu_xgb;
344 adapter->init_port = netxen_niu_xg_init_port;
345 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
346 adapter->stop_port = netxen_niu_disable_xg_port;
353 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
354 adapter->set_mtu = nx_fw_cmd_set_mtu;
355 adapter->set_promisc = netxen_p3_nic_set_promisc;
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)
949 val = NXRD32(adapter, CRB_CMDPEG_STATE);
951 if (val == PHAN_INITIALIZE_COMPLETE ||
952 val == PHAN_INITIALIZE_ACK)
960 pegtune_val = NXRD32(adapter,
961 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
962 printk(KERN_WARNING "netxen_phantom_init: init failed, "
963 "pegtune_val=%x\n", pegtune_val);
972 netxen_receive_peg_ready(struct netxen_adapter *adapter)
978 val = NXRD32(adapter, CRB_RCVPEG_STATE);
980 if (val == PHAN_PEG_RCV_INITIALIZED)
988 printk(KERN_ERR "Receive Peg initialization not "
989 "complete, state: 0x%x.\n", val);
996 int netxen_init_firmware(struct netxen_adapter *adapter)
1000 err = netxen_receive_peg_ready(adapter);
1004 NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1005 NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1006 NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1007 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
1009 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222)) {
1010 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
1017 netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
1022 u8 link_status, module, duplex, autoneg;
1023 struct net_device *netdev = adapter->netdev;
1025 adapter->has_link_events = 1;
1027 cable_OUI = msg->body[1] & 0xffffffff;
1028 cable_len = (msg->body[1] >> 32) & 0xffff;
1029 link_speed = (msg->body[1] >> 48) & 0xffff;
1031 link_status = msg->body[2] & 0xff;
1032 duplex = (msg->body[2] >> 16) & 0xff;
1033 autoneg = (msg->body[2] >> 24) & 0xff;
1035 module = (msg->body[2] >> 8) & 0xff;
1036 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
1037 printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
1038 netdev->name, cable_OUI, cable_len);
1039 } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
1040 printk(KERN_INFO "%s: unsupported cable length %d\n",
1041 netdev->name, cable_len);
1044 netxen_advert_link_change(adapter, link_status);
1046 /* update link parameters */
1047 if (duplex == LINKEVENT_FULL_DUPLEX)
1048 adapter->link_duplex = DUPLEX_FULL;
1050 adapter->link_duplex = DUPLEX_HALF;
1051 adapter->module_type = module;
1052 adapter->link_autoneg = autoneg;
1053 adapter->link_speed = link_speed;
1057 netxen_handle_fw_message(int desc_cnt, int index,
1058 struct nx_host_sds_ring *sds_ring)
1061 struct status_desc *desc;
1064 while (desc_cnt > 0 && i < 8) {
1065 desc = &sds_ring->desc_head[index];
1066 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1067 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1069 index = get_next_index(index, sds_ring->num_desc);
1073 opcode = netxen_get_nic_msg_opcode(msg.body[0]);
1075 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1076 netxen_handle_linkevent(sds_ring->adapter, &msg);
1084 netxen_alloc_rx_skb(struct netxen_adapter *adapter,
1085 struct nx_host_rds_ring *rds_ring,
1086 struct netxen_rx_buffer *buffer)
1088 struct sk_buff *skb;
1090 struct pci_dev *pdev = adapter->pdev;
1092 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1098 if (!adapter->ahw.cut_through)
1099 skb_reserve(skb, 2);
1101 dma = pci_map_single(pdev, skb->data,
1102 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1104 if (pci_dma_mapping_error(pdev, dma)) {
1105 dev_kfree_skb_any(skb);
1112 buffer->state = NETXEN_BUFFER_BUSY;
1117 static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1118 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1120 struct netxen_rx_buffer *buffer;
1121 struct sk_buff *skb;
1123 buffer = &rds_ring->rx_buf_arr[index];
1125 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1126 PCI_DMA_FROMDEVICE);
1132 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1133 adapter->stats.csummed++;
1134 skb->ip_summed = CHECKSUM_UNNECESSARY;
1136 skb->ip_summed = CHECKSUM_NONE;
1138 skb->dev = adapter->netdev;
1142 buffer->state = NETXEN_BUFFER_FREE;
1146 static struct netxen_rx_buffer *
1147 netxen_process_rcv(struct netxen_adapter *adapter,
1148 int ring, int index, int length, int cksum, int pkt_offset,
1149 struct nx_host_sds_ring *sds_ring)
1151 struct net_device *netdev = adapter->netdev;
1152 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1153 struct netxen_rx_buffer *buffer;
1154 struct sk_buff *skb;
1155 struct nx_host_rds_ring *rds_ring = &recv_ctx->rds_rings[ring];
1157 if (unlikely(index > rds_ring->num_desc))
1160 buffer = &rds_ring->rx_buf_arr[index];
1162 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1166 if (length > rds_ring->skb_size)
1167 skb_put(skb, rds_ring->skb_size);
1169 skb_put(skb, length);
1173 skb_pull(skb, pkt_offset);
1175 skb->protocol = eth_type_trans(skb, netdev);
1177 napi_gro_receive(&sds_ring->napi, skb);
1179 adapter->stats.no_rcv++;
1180 adapter->stats.rxbytes += length;
1185 #define netxen_merge_rx_buffers(list, head) \
1186 do { list_splice_tail_init(list, head); } while (0);
1189 netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
1191 struct netxen_adapter *adapter = sds_ring->adapter;
1193 struct list_head *cur;
1195 struct status_desc *desc;
1196 struct netxen_rx_buffer *rxbuf;
1198 u32 consumer = sds_ring->consumer;
1202 int opcode, ring, index, length, cksum, pkt_offset, desc_cnt;
1204 while (count < max) {
1205 desc = &sds_ring->desc_head[consumer];
1206 sts_data = le64_to_cpu(desc->status_desc_data[0]);
1208 if (!(sts_data & STATUS_OWNER_HOST))
1211 desc_cnt = netxen_get_sts_desc_cnt(sts_data);
1212 ring = netxen_get_sts_type(sts_data);
1214 if (ring > RCV_RING_JUMBO)
1217 opcode = netxen_get_sts_opcode(sts_data);
1220 case NETXEN_NIC_RXPKT_DESC:
1221 case NETXEN_OLD_RXPKT_DESC:
1223 case NETXEN_NIC_RESPONSE_DESC:
1224 netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
1229 WARN_ON(desc_cnt > 1);
1231 index = netxen_get_sts_refhandle(sts_data);
1232 length = netxen_get_sts_totallength(sts_data);
1233 cksum = netxen_get_sts_status(sts_data);
1234 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1236 rxbuf = netxen_process_rcv(adapter, ring, index,
1237 length, cksum, pkt_offset, sds_ring);
1240 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1243 for (; desc_cnt > 0; desc_cnt--) {
1244 desc = &sds_ring->desc_head[consumer];
1245 desc->status_desc_data[0] =
1246 cpu_to_le64(STATUS_OWNER_PHANTOM);
1247 consumer = get_next_index(consumer, sds_ring->num_desc);
1252 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1253 struct nx_host_rds_ring *rds_ring =
1254 &adapter->recv_ctx.rds_rings[ring];
1256 if (!list_empty(&sds_ring->free_list[ring])) {
1257 list_for_each(cur, &sds_ring->free_list[ring]) {
1258 rxbuf = list_entry(cur,
1259 struct netxen_rx_buffer, list);
1260 netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
1262 spin_lock(&rds_ring->lock);
1263 netxen_merge_rx_buffers(&sds_ring->free_list[ring],
1264 &rds_ring->free_list);
1265 spin_unlock(&rds_ring->lock);
1268 netxen_post_rx_buffers_nodb(adapter, rds_ring);
1272 sds_ring->consumer = consumer;
1273 NXWR32(adapter, sds_ring->crb_sts_consumer, consumer);
1279 /* Process Command status ring */
1280 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1282 u32 sw_consumer, hw_consumer;
1284 struct netxen_cmd_buffer *buffer;
1285 struct pci_dev *pdev = adapter->pdev;
1286 struct net_device *netdev = adapter->netdev;
1287 struct netxen_skb_frag *frag;
1289 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1291 if (!spin_trylock(&adapter->tx_clean_lock))
1294 sw_consumer = tx_ring->sw_consumer;
1295 barrier(); /* hw_consumer can change underneath */
1296 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1298 while (sw_consumer != hw_consumer) {
1299 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1301 frag = &buffer->frag_array[0];
1302 pci_unmap_single(pdev, frag->dma, frag->length,
1305 for (i = 1; i < buffer->frag_count; i++) {
1306 frag++; /* Get the next frag */
1307 pci_unmap_page(pdev, frag->dma, frag->length,
1312 adapter->stats.xmitfinished++;
1313 dev_kfree_skb_any(buffer->skb);
1317 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
1318 if (++count >= MAX_STATUS_HANDLE)
1323 tx_ring->sw_consumer = sw_consumer;
1325 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1326 netif_tx_lock(netdev);
1327 netif_wake_queue(netdev);
1329 netif_tx_unlock(netdev);
1333 * If everything is freed up to consumer then check if the ring is full
1334 * If the ring is full then check if more needs to be freed and
1335 * schedule the call back again.
1337 * This happens when there are 2 CPUs. One could be freeing and the
1338 * other filling it. If the ring is full when we get out of here and
1339 * the card has already interrupted the host then the host can miss the
1342 * There is still a possible race condition and the host could miss an
1343 * interrupt. The card has to take care of this.
1345 barrier(); /* hw_consumer can change underneath */
1346 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1347 done = (sw_consumer == hw_consumer);
1348 spin_unlock(&adapter->tx_clean_lock);
1354 netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
1355 struct nx_host_rds_ring *rds_ring)
1357 struct rcv_desc *pdesc;
1358 struct netxen_rx_buffer *buffer;
1359 int producer, count = 0;
1360 netxen_ctx_msg msg = 0;
1361 struct list_head *head;
1363 producer = rds_ring->producer;
1365 spin_lock(&rds_ring->lock);
1366 head = &rds_ring->free_list;
1367 while (!list_empty(head)) {
1369 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1372 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1377 list_del(&buffer->list);
1379 /* make a rcv descriptor */
1380 pdesc = &rds_ring->desc_head[producer];
1381 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1382 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1383 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1385 producer = get_next_index(producer, rds_ring->num_desc);
1387 spin_unlock(&rds_ring->lock);
1390 rds_ring->producer = producer;
1391 NXWR32(adapter, rds_ring->crb_rcv_producer,
1392 (producer-1) & (rds_ring->num_desc-1));
1394 if (adapter->fw_major < 4) {
1396 * Write a doorbell msg to tell phanmon of change in
1397 * receive ring producer
1398 * Only for firmware version < 4.0.0
1400 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1401 netxen_set_msg_privid(msg);
1402 netxen_set_msg_count(msg,
1404 (rds_ring->num_desc - 1)));
1405 netxen_set_msg_ctxid(msg, adapter->portnum);
1406 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1408 DB_NORMALIZE(adapter,
1409 NETXEN_RCV_PRODUCER_OFFSET));
1415 netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1416 struct nx_host_rds_ring *rds_ring)
1418 struct rcv_desc *pdesc;
1419 struct netxen_rx_buffer *buffer;
1420 int producer, count = 0;
1421 struct list_head *head;
1423 producer = rds_ring->producer;
1424 if (!spin_trylock(&rds_ring->lock))
1427 head = &rds_ring->free_list;
1428 while (!list_empty(head)) {
1430 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1433 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1438 list_del(&buffer->list);
1440 /* make a rcv descriptor */
1441 pdesc = &rds_ring->desc_head[producer];
1442 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1443 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1444 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1446 producer = get_next_index(producer, rds_ring->num_desc);
1450 rds_ring->producer = producer;
1451 NXWR32(adapter, rds_ring->crb_rcv_producer,
1452 (producer - 1) & (rds_ring->num_desc - 1));
1455 spin_unlock(&rds_ring->lock);
1458 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1460 memset(&adapter->stats, 0, sizeof(adapter->stats));