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 * Source file for NIC routines to initialize the Phantom Hardware
34 #include <linux/netdevice.h>
35 #include <linux/delay.h>
36 #include "netxen_nic.h"
37 #include "netxen_nic_hw.h"
38 #include "netxen_nic_phan_reg.h"
40 struct crb_addr_pair {
45 #define NETXEN_MAX_CRB_XFORM 60
46 static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
47 #define NETXEN_ADDR_ERROR (0xffffffff)
49 #define crb_addr_transform(name) \
50 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
51 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
53 #define NETXEN_NIC_XDMA_RESET 0x8000ff
55 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
56 uint32_t ctx, uint32_t ringid);
59 static void netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
60 unsigned long off, int *data)
62 void __iomem *addr = pci_base_offset(adapter, off);
67 static void crb_addr_transform_setup(void)
69 crb_addr_transform(XDMA);
70 crb_addr_transform(TIMR);
71 crb_addr_transform(SRE);
72 crb_addr_transform(SQN3);
73 crb_addr_transform(SQN2);
74 crb_addr_transform(SQN1);
75 crb_addr_transform(SQN0);
76 crb_addr_transform(SQS3);
77 crb_addr_transform(SQS2);
78 crb_addr_transform(SQS1);
79 crb_addr_transform(SQS0);
80 crb_addr_transform(RPMX7);
81 crb_addr_transform(RPMX6);
82 crb_addr_transform(RPMX5);
83 crb_addr_transform(RPMX4);
84 crb_addr_transform(RPMX3);
85 crb_addr_transform(RPMX2);
86 crb_addr_transform(RPMX1);
87 crb_addr_transform(RPMX0);
88 crb_addr_transform(ROMUSB);
89 crb_addr_transform(SN);
90 crb_addr_transform(QMN);
91 crb_addr_transform(QMS);
92 crb_addr_transform(PGNI);
93 crb_addr_transform(PGND);
94 crb_addr_transform(PGN3);
95 crb_addr_transform(PGN2);
96 crb_addr_transform(PGN1);
97 crb_addr_transform(PGN0);
98 crb_addr_transform(PGSI);
99 crb_addr_transform(PGSD);
100 crb_addr_transform(PGS3);
101 crb_addr_transform(PGS2);
102 crb_addr_transform(PGS1);
103 crb_addr_transform(PGS0);
104 crb_addr_transform(PS);
105 crb_addr_transform(PH);
106 crb_addr_transform(NIU);
107 crb_addr_transform(I2Q);
108 crb_addr_transform(EG);
109 crb_addr_transform(MN);
110 crb_addr_transform(MS);
111 crb_addr_transform(CAS2);
112 crb_addr_transform(CAS1);
113 crb_addr_transform(CAS0);
114 crb_addr_transform(CAM);
115 crb_addr_transform(C2C1);
116 crb_addr_transform(C2C0);
117 crb_addr_transform(SMB);
118 crb_addr_transform(OCM0);
119 crb_addr_transform(I2C0);
122 int netxen_init_firmware(struct netxen_adapter *adapter)
124 u32 state = 0, loops = 0, err = 0;
127 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
129 if (state == PHAN_INITIALIZE_ACK)
132 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
135 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
140 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
146 adapter->pci_write_normalize(adapter,
147 CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
148 adapter->pci_write_normalize(adapter,
149 CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
150 adapter->pci_write_normalize(adapter,
151 CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
152 adapter->pci_write_normalize(adapter,
153 CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
158 void netxen_release_rx_buffers(struct netxen_adapter *adapter)
160 struct netxen_recv_context *recv_ctx;
161 struct nx_host_rds_ring *rds_ring;
162 struct netxen_rx_buffer *rx_buf;
165 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
166 recv_ctx = &adapter->recv_ctx[ctxid];
167 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
168 rds_ring = &recv_ctx->rds_rings[ring];
169 for (i = 0; i < rds_ring->max_rx_desc_count; ++i) {
170 rx_buf = &(rds_ring->rx_buf_arr[i]);
171 if (rx_buf->state == NETXEN_BUFFER_FREE)
173 pci_unmap_single(adapter->pdev,
177 if (rx_buf->skb != NULL)
178 dev_kfree_skb_any(rx_buf->skb);
184 void netxen_release_tx_buffers(struct netxen_adapter *adapter)
186 struct netxen_cmd_buffer *cmd_buf;
187 struct netxen_skb_frag *buffrag;
190 cmd_buf = adapter->cmd_buf_arr;
191 for (i = 0; i < adapter->max_tx_desc_count; i++) {
192 buffrag = cmd_buf->frag_array;
194 pci_unmap_single(adapter->pdev, buffrag->dma,
195 buffrag->length, PCI_DMA_TODEVICE);
198 for (j = 0; j < cmd_buf->frag_count; j++) {
201 pci_unmap_page(adapter->pdev, buffrag->dma,
207 /* Free the skb we received in netxen_nic_xmit_frame */
209 dev_kfree_skb_any(cmd_buf->skb);
216 void netxen_free_sw_resources(struct netxen_adapter *adapter)
218 struct netxen_recv_context *recv_ctx;
219 struct nx_host_rds_ring *rds_ring;
222 for (ctx = 0; ctx < MAX_RCV_CTX; ctx++) {
223 recv_ctx = &adapter->recv_ctx[ctx];
224 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
225 rds_ring = &recv_ctx->rds_rings[ring];
226 if (rds_ring->rx_buf_arr) {
227 vfree(rds_ring->rx_buf_arr);
228 rds_ring->rx_buf_arr = NULL;
232 if (adapter->cmd_buf_arr)
233 vfree(adapter->cmd_buf_arr);
237 int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
239 struct netxen_recv_context *recv_ctx;
240 struct nx_host_rds_ring *rds_ring;
241 struct netxen_rx_buffer *rx_buf;
242 int ctx, ring, i, num_rx_bufs;
244 struct netxen_cmd_buffer *cmd_buf_arr;
245 struct net_device *netdev = adapter->netdev;
247 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
248 if (cmd_buf_arr == NULL) {
249 printk(KERN_ERR "%s: Failed to allocate cmd buffer ring\n",
253 memset(cmd_buf_arr, 0, TX_RINGSIZE);
254 adapter->cmd_buf_arr = cmd_buf_arr;
256 for (ctx = 0; ctx < MAX_RCV_CTX; ctx++) {
257 recv_ctx = &adapter->recv_ctx[ctx];
258 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
259 rds_ring = &recv_ctx->rds_rings[ring];
260 switch (RCV_DESC_TYPE(ring)) {
261 case RCV_DESC_NORMAL:
262 rds_ring->max_rx_desc_count =
263 adapter->max_rx_desc_count;
264 rds_ring->flags = RCV_DESC_NORMAL;
265 if (adapter->ahw.cut_through) {
267 NX_CT_DEFAULT_RX_BUF_LEN;
269 NX_CT_DEFAULT_RX_BUF_LEN;
271 rds_ring->dma_size = RX_DMA_MAP_LEN;
273 MAX_RX_BUFFER_LENGTH;
278 rds_ring->max_rx_desc_count =
279 adapter->max_jumbo_rx_desc_count;
280 rds_ring->flags = RCV_DESC_JUMBO;
281 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
283 NX_P3_RX_JUMBO_BUF_MAX_LEN;
286 NX_P2_RX_JUMBO_BUF_MAX_LEN;
288 rds_ring->dma_size + NET_IP_ALIGN;
292 rds_ring->max_rx_desc_count =
293 adapter->max_lro_rx_desc_count;
294 rds_ring->flags = RCV_DESC_LRO;
295 rds_ring->dma_size = RX_LRO_DMA_MAP_LEN;
296 rds_ring->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
300 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
301 vmalloc(RCV_BUFFSIZE);
302 if (rds_ring->rx_buf_arr == NULL) {
303 printk(KERN_ERR "%s: Failed to allocate "
304 "rx buffer ring %d\n",
306 /* free whatever was already allocated */
309 memset(rds_ring->rx_buf_arr, 0, RCV_BUFFSIZE);
310 INIT_LIST_HEAD(&rds_ring->free_list);
312 * Now go through all of them, set reference handles
313 * and put them in the queues.
315 num_rx_bufs = rds_ring->max_rx_desc_count;
316 rx_buf = rds_ring->rx_buf_arr;
317 for (i = 0; i < num_rx_bufs; i++) {
318 list_add_tail(&rx_buf->list,
319 &rds_ring->free_list);
320 rx_buf->ref_handle = i;
321 rx_buf->state = NETXEN_BUFFER_FREE;
330 netxen_free_sw_resources(adapter);
334 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
336 switch (adapter->ahw.board_type) {
338 adapter->enable_phy_interrupts =
339 netxen_niu_gbe_enable_phy_interrupts;
340 adapter->disable_phy_interrupts =
341 netxen_niu_gbe_disable_phy_interrupts;
342 adapter->macaddr_set = netxen_niu_macaddr_set;
343 adapter->set_mtu = netxen_nic_set_mtu_gb;
344 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
345 adapter->phy_read = netxen_niu_gbe_phy_read;
346 adapter->phy_write = netxen_niu_gbe_phy_write;
347 adapter->init_port = netxen_niu_gbe_init_port;
348 adapter->stop_port = netxen_niu_disable_gbe_port;
351 case NETXEN_NIC_XGBE:
352 adapter->enable_phy_interrupts =
353 netxen_niu_xgbe_enable_phy_interrupts;
354 adapter->disable_phy_interrupts =
355 netxen_niu_xgbe_disable_phy_interrupts;
356 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
357 adapter->set_mtu = netxen_nic_set_mtu_xgb;
358 adapter->init_port = netxen_niu_xg_init_port;
359 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
360 adapter->stop_port = netxen_niu_disable_xg_port;
367 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
368 adapter->set_mtu = nx_fw_cmd_set_mtu;
369 adapter->set_promisc = netxen_p3_nic_set_promisc;
374 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
375 * address to external PCI CRB address.
377 static u32 netxen_decode_crb_addr(u32 addr)
380 u32 base_addr, offset, pci_base;
382 crb_addr_transform_setup();
384 pci_base = NETXEN_ADDR_ERROR;
385 base_addr = addr & 0xfff00000;
386 offset = addr & 0x000fffff;
388 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
389 if (crb_addr_xform[i] == base_addr) {
394 if (pci_base == NETXEN_ADDR_ERROR)
397 return (pci_base + offset);
400 static long rom_max_timeout = 100;
401 static long rom_lock_timeout = 10000;
403 static long rom_write_timeout = 700;
406 static int rom_lock(struct netxen_adapter *adapter)
413 /* acquire semaphore2 from PCI HW block */
414 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
418 if (timeout >= rom_lock_timeout)
428 for (iter = 0; iter < 20; iter++)
429 cpu_relax(); /*This a nop instr on i386 */
432 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
436 static int netxen_wait_rom_done(struct netxen_adapter *adapter)
444 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
447 if (timeout >= rom_max_timeout) {
448 printk("Timeout reached waiting for rom done");
456 static int netxen_rom_wren(struct netxen_adapter *adapter)
458 /* Set write enable latch in ROM status register */
459 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
460 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
462 if (netxen_wait_rom_done(adapter)) {
468 static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
471 unsigned int data = 0xdeaddead;
472 data = netxen_nic_reg_read(adapter, addr);
476 static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
478 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
480 if (netxen_wait_rom_done(adapter)) {
483 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
487 static void netxen_rom_unlock(struct netxen_adapter *adapter)
491 /* release semaphore2 */
492 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
497 static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
502 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
504 val = netxen_do_rom_rdsr(adapter);
507 if (timeout > rom_max_timeout) {
514 static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
517 if (netxen_rom_wren(adapter)) {
520 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
521 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
522 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
523 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
525 if (netxen_wait_rom_done(adapter)) {
526 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
530 return netxen_rom_wip_poll(adapter);
534 static int do_rom_fast_read(struct netxen_adapter *adapter,
537 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
538 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
539 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
540 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
541 if (netxen_wait_rom_done(adapter)) {
542 printk("Error waiting for rom done\n");
545 /* reset abyte_cnt and dummy_byte_cnt */
546 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
548 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
550 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
554 static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
555 u8 *bytes, size_t size)
560 for (addridx = addr; addridx < (addr + size); addridx += 4) {
562 ret = do_rom_fast_read(adapter, addridx, &v);
565 *(__le32 *)bytes = cpu_to_le32(v);
573 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
574 u8 *bytes, size_t size)
578 ret = rom_lock(adapter);
582 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
584 netxen_rom_unlock(adapter);
588 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
592 if (rom_lock(adapter) != 0)
595 ret = do_rom_fast_read(adapter, addr, valp);
596 netxen_rom_unlock(adapter);
601 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
605 if (rom_lock(adapter) != 0) {
608 ret = do_rom_fast_write(adapter, addr, data);
609 netxen_rom_unlock(adapter);
613 static int do_rom_fast_write_words(struct netxen_adapter *adapter,
614 int addr, u8 *bytes, size_t size)
619 while (addridx < (addr + size)) {
620 int last_attempt = 0;
624 data = le32_to_cpu((*(__le32*)bytes));
625 ret = do_rom_fast_write(adapter, addridx, data);
632 ret = do_rom_fast_read(adapter, addridx, &data1);
639 if (timeout++ >= rom_write_timeout) {
640 if (last_attempt++ < 4) {
641 ret = do_rom_fast_write(adapter,
647 printk(KERN_INFO "Data write did not "
648 "succeed at address 0x%x\n", addridx);
661 int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
662 u8 *bytes, size_t size)
666 ret = rom_lock(adapter);
670 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
671 netxen_rom_unlock(adapter);
676 static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
680 ret = netxen_rom_wren(adapter);
684 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
685 netxen_crb_writelit_adapter(adapter,
686 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
688 ret = netxen_wait_rom_done(adapter);
692 return netxen_rom_wip_poll(adapter);
695 static int netxen_rom_rdsr(struct netxen_adapter *adapter)
699 ret = rom_lock(adapter);
703 ret = netxen_do_rom_rdsr(adapter);
704 netxen_rom_unlock(adapter);
708 int netxen_backup_crbinit(struct netxen_adapter *adapter)
710 int ret = FLASH_SUCCESS;
712 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
716 /* unlock sector 63 */
717 val = netxen_rom_rdsr(adapter);
719 ret = netxen_rom_wrsr(adapter, val);
720 if (ret != FLASH_SUCCESS)
723 ret = netxen_rom_wip_poll(adapter);
724 if (ret != FLASH_SUCCESS)
727 /* copy sector 0 to sector 63 */
728 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
729 buffer, NETXEN_FLASH_SECTOR_SIZE);
730 if (ret != FLASH_SUCCESS)
733 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
734 buffer, NETXEN_FLASH_SECTOR_SIZE);
735 if (ret != FLASH_SUCCESS)
739 val = netxen_rom_rdsr(adapter);
743 if (netxen_rom_wrsr(adapter, val) == 0) {
744 ret = netxen_rom_wip_poll(adapter);
745 if (ret != FLASH_SUCCESS)
749 ret = netxen_rom_wip_poll(adapter);
750 if (ret != FLASH_SUCCESS)
760 static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
762 netxen_rom_wren(adapter);
763 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
764 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
765 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
767 if (netxen_wait_rom_done(adapter)) {
768 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
771 return netxen_rom_wip_poll(adapter);
774 static void check_erased_flash(struct netxen_adapter *adapter, int addr)
778 int count = 0, erased_errors = 0;
781 range = (addr == NETXEN_USER_START) ?
782 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
784 for (i = addr; i < range; i += 4) {
785 netxen_rom_fast_read(adapter, i, &val);
786 if (val != 0xffffffff)
792 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
793 "for sector address: %x\n", erased_errors, count, addr);
796 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
799 if (rom_lock(adapter) != 0) {
802 ret = netxen_do_rom_se(adapter, addr);
803 netxen_rom_unlock(adapter);
805 check_erased_flash(adapter, addr);
810 static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
813 int ret = FLASH_SUCCESS;
816 for (i = start; i < end; i++) {
817 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
820 ret = netxen_rom_wip_poll(adapter);
829 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
831 int ret = FLASH_SUCCESS;
834 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
835 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
836 ret = netxen_flash_erase_sections(adapter, start, end);
842 netxen_flash_erase_primary(struct netxen_adapter *adapter)
844 int ret = FLASH_SUCCESS;
847 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
848 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
849 ret = netxen_flash_erase_sections(adapter, start, end);
854 void netxen_halt_pegs(struct netxen_adapter *adapter)
856 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
857 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
858 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
859 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
862 int netxen_flash_unlock(struct netxen_adapter *adapter)
866 ret = netxen_rom_wrsr(adapter, 0);
870 ret = netxen_rom_wren(adapter);
878 #define NETXEN_BOARDTYPE 0x4008
879 #define NETXEN_BOARDNUM 0x400c
880 #define NETXEN_CHIPNUM 0x4010
882 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
885 int i, n, init_delay = 0;
886 struct crb_addr_pair *buf;
892 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
894 netxen_rom_unlock(adapter);
897 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
898 printk("P2 ROM board type: 0x%08x\n", val);
900 printk("Could not read board type\n");
901 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
902 printk("P2 ROM board num: 0x%08x\n", val);
904 printk("Could not read board number\n");
905 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
906 printk("P2 ROM chip num: 0x%08x\n", val);
908 printk("Could not read chip number\n");
911 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
912 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
914 netxen_rom_fast_read(adapter, 4, &n) != 0) {
915 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
916 "n: %08x\n", netxen_nic_driver_name, n);
919 offset = n & 0xffffU;
920 n = (n >> 16) & 0xffffU;
922 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
924 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
925 "n: %08x\n", netxen_nic_driver_name, n);
934 printk(KERN_DEBUG "%s: %d CRB init values found"
935 " in ROM.\n", netxen_nic_driver_name, n);
937 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
938 " initialized.\n", __func__, n);
942 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
944 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
945 netxen_nic_driver_name);
948 for (i = 0; i < n; i++) {
949 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
950 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
959 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
960 netxen_nic_driver_name,
961 (u32)netxen_decode_crb_addr(addr), val);
963 for (i = 0; i < n; i++) {
965 off = netxen_decode_crb_addr(buf[i].addr);
966 if (off == NETXEN_ADDR_ERROR) {
967 printk(KERN_ERR"CRB init value out of range %x\n",
971 off += NETXEN_PCI_CRBSPACE;
972 /* skipping cold reboot MAGIC */
973 if (off == NETXEN_CAM_RAM(0x1fc))
976 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
977 /* do not reset PCI */
978 if (off == (ROMUSB_GLB + 0xbc))
980 if (off == (ROMUSB_GLB + 0xa8))
982 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
984 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
986 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
988 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
989 buf[i].data = 0x1020;
990 /* skip the function enable register */
991 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
993 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
995 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
999 if (off == NETXEN_ADDR_ERROR) {
1000 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
1001 netxen_nic_driver_name, buf[i].addr);
1006 /* After writing this register, HW needs time for CRB */
1007 /* to quiet down (else crb_window returns 0xffffffff) */
1008 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
1010 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1011 /* hold xdma in reset also */
1012 buf[i].data = NETXEN_NIC_XDMA_RESET;
1013 buf[i].data = 0x8000ff;
1017 adapter->hw_write_wx(adapter, off, &buf[i].data, 4);
1023 /* disable_peg_cache_all */
1025 /* unreset_net_cache */
1026 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1027 adapter->hw_read_wx(adapter,
1028 NETXEN_ROMUSB_GLB_SW_RESET, &val, 4);
1029 netxen_crb_writelit_adapter(adapter,
1030 NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
1033 /* p2dn replyCount */
1034 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
1035 /* disable_peg_cache 0 */
1036 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
1037 /* disable_peg_cache 1 */
1038 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
1043 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
1044 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
1046 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
1047 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
1049 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
1050 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
1052 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
1053 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
1057 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
1063 adapter->dummy_dma.addr =
1064 pci_alloc_consistent(adapter->pdev,
1065 NETXEN_HOST_DUMMY_DMA_SIZE,
1066 &adapter->dummy_dma.phys_addr);
1067 if (adapter->dummy_dma.addr == NULL) {
1068 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
1073 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1074 hi = (addr >> 32) & 0xffffffff;
1075 lo = addr & 0xffffffff;
1077 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1078 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
1080 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1082 adapter->hw_write_wx(adapter, CRB_HOST_DUMMY_BUF, &temp, 4);
1088 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
1092 if (!adapter->dummy_dma.addr)
1095 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1097 if (dma_watchdog_shutdown_request(adapter) == 1)
1100 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
1106 pci_free_consistent(adapter->pdev,
1107 NETXEN_HOST_DUMMY_DMA_SIZE,
1108 adapter->dummy_dma.addr,
1109 adapter->dummy_dma.phys_addr);
1110 adapter->dummy_dma.addr = NULL;
1112 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
1113 adapter->netdev->name);
1117 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
1124 val = adapter->pci_read_normalize(adapter,
1127 if (val == PHAN_INITIALIZE_COMPLETE ||
1128 val == PHAN_INITIALIZE_ACK)
1133 } while (--retries);
1136 pegtune_val = adapter->pci_read_normalize(adapter,
1137 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
1138 printk(KERN_WARNING "netxen_phantom_init: init failed, "
1139 "pegtune_val=%x\n", pegtune_val);
1147 int netxen_receive_peg_ready(struct netxen_adapter *adapter)
1153 val = adapter->pci_read_normalize(adapter, CRB_RCVPEG_STATE);
1155 if (val == PHAN_PEG_RCV_INITIALIZED)
1160 } while (--retries);
1163 printk(KERN_ERR "Receive Peg initialization not "
1164 "complete, state: 0x%x.\n", val);
1171 static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1172 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1174 struct netxen_rx_buffer *buffer;
1175 struct sk_buff *skb;
1177 buffer = &rds_ring->rx_buf_arr[index];
1179 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1180 PCI_DMA_FROMDEVICE);
1186 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1187 adapter->stats.csummed++;
1188 skb->ip_summed = CHECKSUM_UNNECESSARY;
1190 skb->ip_summed = CHECKSUM_NONE;
1192 skb->dev = adapter->netdev;
1197 buffer->state = NETXEN_BUFFER_FREE;
1198 buffer->lro_current_frags = 0;
1199 buffer->lro_expected_frags = 0;
1200 list_add_tail(&buffer->list, &rds_ring->free_list);
1205 * netxen_process_rcv() send the received packet to the protocol stack.
1206 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1207 * invoke the routine to send more rx buffers to the Phantom...
1209 static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1210 struct status_desc *desc, struct status_desc *frag_desc)
1212 struct net_device *netdev = adapter->netdev;
1213 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1214 int index = netxen_get_sts_refhandle(sts_data);
1215 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1216 struct netxen_rx_buffer *buffer;
1217 struct sk_buff *skb;
1218 u32 length = netxen_get_sts_totallength(sts_data);
1220 u16 pkt_offset = 0, cksum;
1221 struct nx_host_rds_ring *rds_ring;
1223 desc_ctx = netxen_get_sts_type(sts_data);
1224 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1225 printk("%s: %s Bad Rcv descriptor ring\n",
1226 netxen_nic_driver_name, netdev->name);
1230 rds_ring = &recv_ctx->rds_rings[desc_ctx];
1231 if (unlikely(index > rds_ring->max_rx_desc_count)) {
1232 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1233 index, rds_ring->max_rx_desc_count);
1236 buffer = &rds_ring->rx_buf_arr[index];
1237 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1238 buffer->lro_current_frags++;
1239 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1240 buffer->lro_expected_frags =
1241 netxen_get_sts_desc_lro_cnt(desc);
1242 buffer->lro_length = length;
1244 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1245 if (buffer->lro_expected_frags != 0) {
1246 printk("LRO: (refhandle:%x) recv frag. "
1247 "wait for last. flags: %x expected:%d "
1249 netxen_get_sts_desc_lro_last_frag(desc),
1250 buffer->lro_expected_frags,
1251 buffer->lro_current_frags);
1257 cksum = netxen_get_sts_status(sts_data);
1259 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1263 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1264 /* True length was only available on the last pkt */
1265 skb_put(skb, buffer->lro_length);
1267 if (length > rds_ring->skb_size)
1268 skb_put(skb, rds_ring->skb_size);
1270 skb_put(skb, length);
1272 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1274 skb_pull(skb, pkt_offset);
1277 skb->protocol = eth_type_trans(skb, netdev);
1280 * rx buffer chaining is disabled, walk and free
1281 * any spurious rx buffer chain.
1284 u16 i, nr_frags = desc->nr_frags;
1286 dev_kfree_skb_any(skb);
1287 for (i = 0; i < nr_frags; i++) {
1288 index = le16_to_cpu(frag_desc->frag_handles[i]);
1289 skb = netxen_process_rxbuf(adapter,
1290 rds_ring, index, cksum);
1292 dev_kfree_skb_any(skb);
1294 adapter->stats.rxdropped++;
1296 netif_receive_skb(skb);
1298 adapter->stats.no_rcv++;
1299 adapter->stats.rxbytes += length;
1303 /* Process Receive status ring */
1304 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1306 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1307 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1308 struct status_desc *desc, *frag_desc;
1309 u32 consumer = recv_ctx->status_rx_consumer;
1310 int count = 0, ring;
1314 while (count < max) {
1315 desc = &desc_head[consumer];
1316 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1317 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1318 netxen_get_sts_owner(desc));
1322 sts_data = le64_to_cpu(desc->status_desc_data);
1323 opcode = netxen_get_sts_opcode(sts_data);
1325 if (opcode == NETXEN_NIC_RXPKT_DESC) {
1326 if (desc->nr_frags) {
1327 consumer = get_next_index(consumer,
1328 adapter->max_rx_desc_count);
1329 frag_desc = &desc_head[consumer];
1330 netxen_set_sts_owner(frag_desc,
1331 STATUS_OWNER_PHANTOM);
1335 netxen_process_rcv(adapter, ctxid, desc, frag_desc);
1337 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1339 consumer = get_next_index(consumer,
1340 adapter->max_rx_desc_count);
1343 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1344 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1346 /* update the consumer index in phantom */
1348 recv_ctx->status_rx_consumer = consumer;
1351 adapter->pci_write_normalize(adapter,
1352 recv_ctx->crb_sts_consumer, consumer);
1358 /* Process Command status ring */
1359 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1361 u32 last_consumer, consumer;
1363 struct netxen_cmd_buffer *buffer;
1364 struct pci_dev *pdev = adapter->pdev;
1365 struct net_device *netdev = adapter->netdev;
1366 struct netxen_skb_frag *frag;
1369 last_consumer = adapter->last_cmd_consumer;
1370 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1372 while (last_consumer != consumer) {
1373 buffer = &adapter->cmd_buf_arr[last_consumer];
1375 frag = &buffer->frag_array[0];
1376 pci_unmap_single(pdev, frag->dma, frag->length,
1379 for (i = 1; i < buffer->frag_count; i++) {
1380 frag++; /* Get the next frag */
1381 pci_unmap_page(pdev, frag->dma, frag->length,
1386 adapter->stats.xmitfinished++;
1387 dev_kfree_skb_any(buffer->skb);
1391 last_consumer = get_next_index(last_consumer,
1392 adapter->max_tx_desc_count);
1393 if (++count >= MAX_STATUS_HANDLE)
1398 adapter->last_cmd_consumer = last_consumer;
1400 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1401 netif_tx_lock(netdev);
1402 netif_wake_queue(netdev);
1404 netif_tx_unlock(netdev);
1408 * If everything is freed up to consumer then check if the ring is full
1409 * If the ring is full then check if more needs to be freed and
1410 * schedule the call back again.
1412 * This happens when there are 2 CPUs. One could be freeing and the
1413 * other filling it. If the ring is full when we get out of here and
1414 * the card has already interrupted the host then the host can miss the
1417 * There is still a possible race condition and the host could miss an
1418 * interrupt. The card has to take care of this.
1420 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1421 done = (last_consumer == consumer);
1427 * netxen_post_rx_buffers puts buffer in the Phantom memory
1429 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1431 struct pci_dev *pdev = adapter->pdev;
1432 struct sk_buff *skb;
1433 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1434 struct nx_host_rds_ring *rds_ring = NULL;
1436 struct rcv_desc *pdesc;
1437 struct netxen_rx_buffer *buffer;
1439 netxen_ctx_msg msg = 0;
1441 struct list_head *head;
1443 rds_ring = &recv_ctx->rds_rings[ringid];
1445 producer = rds_ring->producer;
1446 head = &rds_ring->free_list;
1448 /* We can start writing rx descriptors into the phantom memory. */
1449 while (!list_empty(head)) {
1451 skb = dev_alloc_skb(rds_ring->skb_size);
1452 if (unlikely(!skb)) {
1456 if (!adapter->ahw.cut_through)
1457 skb_reserve(skb, 2);
1459 dma = pci_map_single(pdev, skb->data,
1460 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1461 if (pci_dma_mapping_error(pdev, dma)) {
1462 dev_kfree_skb_any(skb);
1467 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1468 list_del(&buffer->list);
1471 buffer->state = NETXEN_BUFFER_BUSY;
1474 /* make a rcv descriptor */
1475 pdesc = &rds_ring->desc_head[producer];
1476 pdesc->addr_buffer = cpu_to_le64(dma);
1477 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1478 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1480 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
1482 /* if we did allocate buffers, then write the count to Phantom */
1484 rds_ring->producer = producer;
1486 adapter->pci_write_normalize(adapter,
1487 rds_ring->crb_rcv_producer,
1488 (producer-1) & (rds_ring->max_rx_desc_count-1));
1490 if (adapter->fw_major < 4) {
1492 * Write a doorbell msg to tell phanmon of change in
1493 * receive ring producer
1494 * Only for firmware version < 4.0.0
1496 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1497 netxen_set_msg_privid(msg);
1498 netxen_set_msg_count(msg,
1501 max_rx_desc_count - 1)));
1502 netxen_set_msg_ctxid(msg, adapter->portnum);
1503 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1505 DB_NORMALIZE(adapter,
1506 NETXEN_RCV_PRODUCER_OFFSET));
1511 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1512 uint32_t ctx, uint32_t ringid)
1514 struct pci_dev *pdev = adapter->pdev;
1515 struct sk_buff *skb;
1516 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1517 struct nx_host_rds_ring *rds_ring = NULL;
1519 struct rcv_desc *pdesc;
1520 struct netxen_rx_buffer *buffer;
1522 struct list_head *head;
1525 rds_ring = &recv_ctx->rds_rings[ringid];
1527 producer = rds_ring->producer;
1528 head = &rds_ring->free_list;
1529 /* We can start writing rx descriptors into the phantom memory. */
1530 while (!list_empty(head)) {
1532 skb = dev_alloc_skb(rds_ring->skb_size);
1533 if (unlikely(!skb)) {
1537 if (!adapter->ahw.cut_through)
1538 skb_reserve(skb, 2);
1540 dma = pci_map_single(pdev, skb->data,
1541 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1542 if (pci_dma_mapping_error(pdev, dma)) {
1543 dev_kfree_skb_any(skb);
1548 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1549 list_del(&buffer->list);
1552 buffer->state = NETXEN_BUFFER_BUSY;
1555 /* make a rcv descriptor */
1556 pdesc = &rds_ring->desc_head[producer];
1557 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1558 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1559 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1561 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
1564 /* if we did allocate buffers, then write the count to Phantom */
1566 rds_ring->producer = producer;
1568 adapter->pci_write_normalize(adapter,
1569 rds_ring->crb_rcv_producer,
1570 (producer-1) & (rds_ring->max_rx_desc_count-1));
1575 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1577 memset(&adapter->stats, 0, sizeof(adapter->stats));