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)
957 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
958 netxen_nic_driver_name,
959 (u32)netxen_decode_crb_addr(addr), val);
961 for (i = 0; i < n; i++) {
963 off = netxen_decode_crb_addr(buf[i].addr);
964 if (off == NETXEN_ADDR_ERROR) {
965 printk(KERN_ERR"CRB init value out of range %x\n",
969 off += NETXEN_PCI_CRBSPACE;
970 /* skipping cold reboot MAGIC */
971 if (off == NETXEN_CAM_RAM(0x1fc))
974 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
975 /* do not reset PCI */
976 if (off == (ROMUSB_GLB + 0xbc))
978 if (off == (ROMUSB_GLB + 0xa8))
980 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
982 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
984 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
986 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
987 buf[i].data = 0x1020;
988 /* skip the function enable register */
989 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
991 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
993 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
997 if (off == NETXEN_ADDR_ERROR) {
998 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
999 netxen_nic_driver_name, buf[i].addr);
1004 /* After writing this register, HW needs time for CRB */
1005 /* to quiet down (else crb_window returns 0xffffffff) */
1006 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
1008 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1009 /* hold xdma in reset also */
1010 buf[i].data = NETXEN_NIC_XDMA_RESET;
1011 buf[i].data = 0x8000ff;
1015 adapter->hw_write_wx(adapter, off, &buf[i].data, 4);
1021 /* disable_peg_cache_all */
1023 /* unreset_net_cache */
1024 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1025 adapter->hw_read_wx(adapter,
1026 NETXEN_ROMUSB_GLB_SW_RESET, &val, 4);
1027 netxen_crb_writelit_adapter(adapter,
1028 NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
1031 /* p2dn replyCount */
1032 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
1033 /* disable_peg_cache 0 */
1034 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
1035 /* disable_peg_cache 1 */
1036 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
1041 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
1042 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
1044 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
1045 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
1047 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
1048 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
1050 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
1051 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
1055 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
1061 adapter->dummy_dma.addr =
1062 pci_alloc_consistent(adapter->pdev,
1063 NETXEN_HOST_DUMMY_DMA_SIZE,
1064 &adapter->dummy_dma.phys_addr);
1065 if (adapter->dummy_dma.addr == NULL) {
1066 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
1071 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1072 hi = (addr >> 32) & 0xffffffff;
1073 lo = addr & 0xffffffff;
1075 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1076 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
1078 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1080 adapter->hw_write_wx(adapter, CRB_HOST_DUMMY_BUF, &temp, 4);
1086 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
1090 if (!adapter->dummy_dma.addr)
1093 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1095 if (dma_watchdog_shutdown_request(adapter) == 1)
1098 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
1104 pci_free_consistent(adapter->pdev,
1105 NETXEN_HOST_DUMMY_DMA_SIZE,
1106 adapter->dummy_dma.addr,
1107 adapter->dummy_dma.phys_addr);
1108 adapter->dummy_dma.addr = NULL;
1110 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
1111 adapter->netdev->name);
1115 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
1122 val = adapter->pci_read_normalize(adapter,
1125 if (val == PHAN_INITIALIZE_COMPLETE ||
1126 val == PHAN_INITIALIZE_ACK)
1131 } while (--retries);
1134 pegtune_val = adapter->pci_read_normalize(adapter,
1135 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
1136 printk(KERN_WARNING "netxen_phantom_init: init failed, "
1137 "pegtune_val=%x\n", pegtune_val);
1145 int netxen_receive_peg_ready(struct netxen_adapter *adapter)
1151 val = adapter->pci_read_normalize(adapter, CRB_RCVPEG_STATE);
1153 if (val == PHAN_PEG_RCV_INITIALIZED)
1158 } while (--retries);
1161 printk(KERN_ERR "Receive Peg initialization not "
1162 "complete, state: 0x%x.\n", val);
1169 static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1170 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1172 struct netxen_rx_buffer *buffer;
1173 struct sk_buff *skb;
1175 buffer = &rds_ring->rx_buf_arr[index];
1177 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1178 PCI_DMA_FROMDEVICE);
1184 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1185 adapter->stats.csummed++;
1186 skb->ip_summed = CHECKSUM_UNNECESSARY;
1188 skb->ip_summed = CHECKSUM_NONE;
1190 skb->dev = adapter->netdev;
1195 buffer->state = NETXEN_BUFFER_FREE;
1196 buffer->lro_current_frags = 0;
1197 buffer->lro_expected_frags = 0;
1198 list_add_tail(&buffer->list, &rds_ring->free_list);
1203 * netxen_process_rcv() send the received packet to the protocol stack.
1204 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1205 * invoke the routine to send more rx buffers to the Phantom...
1207 static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1208 struct status_desc *desc, struct status_desc *frag_desc)
1210 struct net_device *netdev = adapter->netdev;
1211 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1212 int index = netxen_get_sts_refhandle(sts_data);
1213 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1214 struct netxen_rx_buffer *buffer;
1215 struct sk_buff *skb;
1216 u32 length = netxen_get_sts_totallength(sts_data);
1218 u16 pkt_offset = 0, cksum;
1219 struct nx_host_rds_ring *rds_ring;
1221 desc_ctx = netxen_get_sts_type(sts_data);
1222 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1223 printk("%s: %s Bad Rcv descriptor ring\n",
1224 netxen_nic_driver_name, netdev->name);
1228 rds_ring = &recv_ctx->rds_rings[desc_ctx];
1229 if (unlikely(index > rds_ring->max_rx_desc_count)) {
1230 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1231 index, rds_ring->max_rx_desc_count);
1234 buffer = &rds_ring->rx_buf_arr[index];
1235 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1236 buffer->lro_current_frags++;
1237 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1238 buffer->lro_expected_frags =
1239 netxen_get_sts_desc_lro_cnt(desc);
1240 buffer->lro_length = length;
1242 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1243 if (buffer->lro_expected_frags != 0) {
1244 printk("LRO: (refhandle:%x) recv frag. "
1245 "wait for last. flags: %x expected:%d "
1247 netxen_get_sts_desc_lro_last_frag(desc),
1248 buffer->lro_expected_frags,
1249 buffer->lro_current_frags);
1255 cksum = netxen_get_sts_status(sts_data);
1257 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1261 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1262 /* True length was only available on the last pkt */
1263 skb_put(skb, buffer->lro_length);
1265 if (length > rds_ring->skb_size)
1266 skb_put(skb, rds_ring->skb_size);
1268 skb_put(skb, length);
1270 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1272 skb_pull(skb, pkt_offset);
1275 skb->protocol = eth_type_trans(skb, netdev);
1278 * rx buffer chaining is disabled, walk and free
1279 * any spurious rx buffer chain.
1282 u16 i, nr_frags = desc->nr_frags;
1284 dev_kfree_skb_any(skb);
1285 for (i = 0; i < nr_frags; i++) {
1286 index = le16_to_cpu(frag_desc->frag_handles[i]);
1287 skb = netxen_process_rxbuf(adapter,
1288 rds_ring, index, cksum);
1290 dev_kfree_skb_any(skb);
1292 adapter->stats.rxdropped++;
1294 netif_receive_skb(skb);
1296 adapter->stats.no_rcv++;
1297 adapter->stats.rxbytes += length;
1301 /* Process Receive status ring */
1302 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1304 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1305 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1306 struct status_desc *desc, *frag_desc;
1307 u32 consumer = recv_ctx->status_rx_consumer;
1308 int count = 0, ring;
1312 while (count < max) {
1313 desc = &desc_head[consumer];
1314 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1315 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1316 netxen_get_sts_owner(desc));
1320 sts_data = le64_to_cpu(desc->status_desc_data);
1321 opcode = netxen_get_sts_opcode(sts_data);
1323 if (opcode == NETXEN_NIC_RXPKT_DESC) {
1324 if (desc->nr_frags) {
1325 consumer = get_next_index(consumer,
1326 adapter->max_rx_desc_count);
1327 frag_desc = &desc_head[consumer];
1328 netxen_set_sts_owner(frag_desc,
1329 STATUS_OWNER_PHANTOM);
1333 netxen_process_rcv(adapter, ctxid, desc, frag_desc);
1335 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1337 consumer = get_next_index(consumer,
1338 adapter->max_rx_desc_count);
1341 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1342 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1344 /* update the consumer index in phantom */
1346 recv_ctx->status_rx_consumer = consumer;
1349 adapter->pci_write_normalize(adapter,
1350 recv_ctx->crb_sts_consumer, consumer);
1356 /* Process Command status ring */
1357 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1359 u32 last_consumer, consumer;
1361 struct netxen_cmd_buffer *buffer;
1362 struct pci_dev *pdev = adapter->pdev;
1363 struct net_device *netdev = adapter->netdev;
1364 struct netxen_skb_frag *frag;
1367 last_consumer = adapter->last_cmd_consumer;
1368 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1370 while (last_consumer != consumer) {
1371 buffer = &adapter->cmd_buf_arr[last_consumer];
1373 frag = &buffer->frag_array[0];
1374 pci_unmap_single(pdev, frag->dma, frag->length,
1377 for (i = 1; i < buffer->frag_count; i++) {
1378 frag++; /* Get the next frag */
1379 pci_unmap_page(pdev, frag->dma, frag->length,
1384 adapter->stats.xmitfinished++;
1385 dev_kfree_skb_any(buffer->skb);
1389 last_consumer = get_next_index(last_consumer,
1390 adapter->max_tx_desc_count);
1391 if (++count >= MAX_STATUS_HANDLE)
1396 adapter->last_cmd_consumer = last_consumer;
1398 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1399 netif_tx_lock(netdev);
1400 netif_wake_queue(netdev);
1402 netif_tx_unlock(netdev);
1406 * If everything is freed up to consumer then check if the ring is full
1407 * If the ring is full then check if more needs to be freed and
1408 * schedule the call back again.
1410 * This happens when there are 2 CPUs. One could be freeing and the
1411 * other filling it. If the ring is full when we get out of here and
1412 * the card has already interrupted the host then the host can miss the
1415 * There is still a possible race condition and the host could miss an
1416 * interrupt. The card has to take care of this.
1418 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1419 done = (last_consumer == consumer);
1425 * netxen_post_rx_buffers puts buffer in the Phantom memory
1427 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1429 struct pci_dev *pdev = adapter->pdev;
1430 struct sk_buff *skb;
1431 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1432 struct nx_host_rds_ring *rds_ring = NULL;
1434 struct rcv_desc *pdesc;
1435 struct netxen_rx_buffer *buffer;
1437 netxen_ctx_msg msg = 0;
1439 struct list_head *head;
1441 rds_ring = &recv_ctx->rds_rings[ringid];
1443 producer = rds_ring->producer;
1444 head = &rds_ring->free_list;
1446 /* We can start writing rx descriptors into the phantom memory. */
1447 while (!list_empty(head)) {
1449 skb = dev_alloc_skb(rds_ring->skb_size);
1450 if (unlikely(!skb)) {
1454 if (!adapter->ahw.cut_through)
1455 skb_reserve(skb, 2);
1457 dma = pci_map_single(pdev, skb->data,
1458 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1459 if (pci_dma_mapping_error(pdev, dma)) {
1460 dev_kfree_skb_any(skb);
1465 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1466 list_del(&buffer->list);
1469 buffer->state = NETXEN_BUFFER_BUSY;
1472 /* make a rcv descriptor */
1473 pdesc = &rds_ring->desc_head[producer];
1474 pdesc->addr_buffer = cpu_to_le64(dma);
1475 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1476 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1478 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
1480 /* if we did allocate buffers, then write the count to Phantom */
1482 rds_ring->producer = producer;
1484 adapter->pci_write_normalize(adapter,
1485 rds_ring->crb_rcv_producer,
1486 (producer-1) & (rds_ring->max_rx_desc_count-1));
1488 if (adapter->fw_major < 4) {
1490 * Write a doorbell msg to tell phanmon of change in
1491 * receive ring producer
1492 * Only for firmware version < 4.0.0
1494 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1495 netxen_set_msg_privid(msg);
1496 netxen_set_msg_count(msg,
1499 max_rx_desc_count - 1)));
1500 netxen_set_msg_ctxid(msg, adapter->portnum);
1501 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1503 DB_NORMALIZE(adapter,
1504 NETXEN_RCV_PRODUCER_OFFSET));
1509 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1510 uint32_t ctx, uint32_t ringid)
1512 struct pci_dev *pdev = adapter->pdev;
1513 struct sk_buff *skb;
1514 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1515 struct nx_host_rds_ring *rds_ring = NULL;
1517 struct rcv_desc *pdesc;
1518 struct netxen_rx_buffer *buffer;
1520 struct list_head *head;
1523 rds_ring = &recv_ctx->rds_rings[ringid];
1525 producer = rds_ring->producer;
1526 head = &rds_ring->free_list;
1527 /* We can start writing rx descriptors into the phantom memory. */
1528 while (!list_empty(head)) {
1530 skb = dev_alloc_skb(rds_ring->skb_size);
1531 if (unlikely(!skb)) {
1535 if (!adapter->ahw.cut_through)
1536 skb_reserve(skb, 2);
1538 dma = pci_map_single(pdev, skb->data,
1539 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1540 if (pci_dma_mapping_error(pdev, dma)) {
1541 dev_kfree_skb_any(skb);
1546 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1547 list_del(&buffer->list);
1550 buffer->state = NETXEN_BUFFER_BUSY;
1553 /* make a rcv descriptor */
1554 pdesc = &rds_ring->desc_head[producer];
1555 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1556 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1557 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1559 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
1562 /* if we did allocate buffers, then write the count to Phantom */
1564 rds_ring->producer = producer;
1566 adapter->pci_write_normalize(adapter,
1567 rds_ring->crb_rcv_producer,
1568 (producer-1) & (rds_ring->max_rx_desc_count-1));
1573 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1575 memset(&adapter->stats, 0, sizeof(adapter->stats));