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);
311 rds_ring->begin_alloc = 0;
313 * Now go through all of them, set reference handles
314 * and put them in the queues.
316 num_rx_bufs = rds_ring->max_rx_desc_count;
317 rx_buf = rds_ring->rx_buf_arr;
318 for (i = 0; i < num_rx_bufs; i++) {
319 list_add_tail(&rx_buf->list,
320 &rds_ring->free_list);
321 rx_buf->ref_handle = i;
322 rx_buf->state = NETXEN_BUFFER_FREE;
331 netxen_free_sw_resources(adapter);
335 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
337 switch (adapter->ahw.board_type) {
339 adapter->enable_phy_interrupts =
340 netxen_niu_gbe_enable_phy_interrupts;
341 adapter->disable_phy_interrupts =
342 netxen_niu_gbe_disable_phy_interrupts;
343 adapter->macaddr_set = netxen_niu_macaddr_set;
344 adapter->set_mtu = netxen_nic_set_mtu_gb;
345 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
346 adapter->phy_read = netxen_niu_gbe_phy_read;
347 adapter->phy_write = netxen_niu_gbe_phy_write;
348 adapter->init_port = netxen_niu_gbe_init_port;
349 adapter->stop_port = netxen_niu_disable_gbe_port;
352 case NETXEN_NIC_XGBE:
353 adapter->enable_phy_interrupts =
354 netxen_niu_xgbe_enable_phy_interrupts;
355 adapter->disable_phy_interrupts =
356 netxen_niu_xgbe_disable_phy_interrupts;
357 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
358 adapter->set_mtu = netxen_nic_set_mtu_xgb;
359 adapter->init_port = netxen_niu_xg_init_port;
360 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
361 adapter->stop_port = netxen_niu_disable_xg_port;
370 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
371 * address to external PCI CRB address.
373 static u32 netxen_decode_crb_addr(u32 addr)
376 u32 base_addr, offset, pci_base;
378 crb_addr_transform_setup();
380 pci_base = NETXEN_ADDR_ERROR;
381 base_addr = addr & 0xfff00000;
382 offset = addr & 0x000fffff;
384 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
385 if (crb_addr_xform[i] == base_addr) {
390 if (pci_base == NETXEN_ADDR_ERROR)
393 return (pci_base + offset);
396 static long rom_max_timeout = 100;
397 static long rom_lock_timeout = 10000;
399 static long rom_write_timeout = 700;
402 static int rom_lock(struct netxen_adapter *adapter)
409 /* acquire semaphore2 from PCI HW block */
410 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
414 if (timeout >= rom_lock_timeout)
424 for (iter = 0; iter < 20; iter++)
425 cpu_relax(); /*This a nop instr on i386 */
428 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
432 static int netxen_wait_rom_done(struct netxen_adapter *adapter)
438 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
441 if (timeout >= rom_max_timeout) {
442 printk("Timeout reached waiting for rom done");
450 static int netxen_rom_wren(struct netxen_adapter *adapter)
452 /* Set write enable latch in ROM status register */
453 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
454 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
456 if (netxen_wait_rom_done(adapter)) {
462 static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
465 unsigned int data = 0xdeaddead;
466 data = netxen_nic_reg_read(adapter, addr);
470 static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
472 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
474 if (netxen_wait_rom_done(adapter)) {
477 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
481 static void netxen_rom_unlock(struct netxen_adapter *adapter)
485 /* release semaphore2 */
486 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
491 static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
496 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
498 val = netxen_do_rom_rdsr(adapter);
501 if (timeout > rom_max_timeout) {
508 static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
511 if (netxen_rom_wren(adapter)) {
514 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
515 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
516 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
517 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
519 if (netxen_wait_rom_done(adapter)) {
520 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
524 return netxen_rom_wip_poll(adapter);
528 static int do_rom_fast_read(struct netxen_adapter *adapter,
533 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
534 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
535 udelay(100); /* prevent bursting on CRB */
536 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
537 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
538 if (netxen_wait_rom_done(adapter)) {
539 printk("Error waiting for rom done\n");
542 /* reset abyte_cnt and dummy_byte_cnt */
543 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
544 udelay(100); /* prevent bursting on CRB */
545 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
547 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
551 static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
552 u8 *bytes, size_t size)
557 for (addridx = addr; addridx < (addr + size); addridx += 4) {
559 ret = do_rom_fast_read(adapter, addridx, &v);
562 *(__le32 *)bytes = cpu_to_le32(v);
570 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
571 u8 *bytes, size_t size)
575 ret = rom_lock(adapter);
579 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
581 netxen_rom_unlock(adapter);
585 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
589 if (rom_lock(adapter) != 0)
592 ret = do_rom_fast_read(adapter, addr, valp);
593 netxen_rom_unlock(adapter);
598 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
602 if (rom_lock(adapter) != 0) {
605 ret = do_rom_fast_write(adapter, addr, data);
606 netxen_rom_unlock(adapter);
610 static int do_rom_fast_write_words(struct netxen_adapter *adapter,
611 int addr, u8 *bytes, size_t size)
616 while (addridx < (addr + size)) {
617 int last_attempt = 0;
621 data = le32_to_cpu((*(__le32*)bytes));
622 ret = do_rom_fast_write(adapter, addridx, data);
629 ret = do_rom_fast_read(adapter, addridx, &data1);
636 if (timeout++ >= rom_write_timeout) {
637 if (last_attempt++ < 4) {
638 ret = do_rom_fast_write(adapter,
644 printk(KERN_INFO "Data write did not "
645 "succeed at address 0x%x\n", addridx);
658 int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
659 u8 *bytes, size_t size)
663 ret = rom_lock(adapter);
667 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
668 netxen_rom_unlock(adapter);
673 static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
677 ret = netxen_rom_wren(adapter);
681 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
682 netxen_crb_writelit_adapter(adapter,
683 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
685 ret = netxen_wait_rom_done(adapter);
689 return netxen_rom_wip_poll(adapter);
692 static int netxen_rom_rdsr(struct netxen_adapter *adapter)
696 ret = rom_lock(adapter);
700 ret = netxen_do_rom_rdsr(adapter);
701 netxen_rom_unlock(adapter);
705 int netxen_backup_crbinit(struct netxen_adapter *adapter)
707 int ret = FLASH_SUCCESS;
709 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
713 /* unlock sector 63 */
714 val = netxen_rom_rdsr(adapter);
716 ret = netxen_rom_wrsr(adapter, val);
717 if (ret != FLASH_SUCCESS)
720 ret = netxen_rom_wip_poll(adapter);
721 if (ret != FLASH_SUCCESS)
724 /* copy sector 0 to sector 63 */
725 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
726 buffer, NETXEN_FLASH_SECTOR_SIZE);
727 if (ret != FLASH_SUCCESS)
730 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
731 buffer, NETXEN_FLASH_SECTOR_SIZE);
732 if (ret != FLASH_SUCCESS)
736 val = netxen_rom_rdsr(adapter);
740 if (netxen_rom_wrsr(adapter, val) == 0) {
741 ret = netxen_rom_wip_poll(adapter);
742 if (ret != FLASH_SUCCESS)
746 ret = netxen_rom_wip_poll(adapter);
747 if (ret != FLASH_SUCCESS)
757 static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
759 netxen_rom_wren(adapter);
760 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
761 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
762 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
764 if (netxen_wait_rom_done(adapter)) {
765 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
768 return netxen_rom_wip_poll(adapter);
771 static void check_erased_flash(struct netxen_adapter *adapter, int addr)
775 int count = 0, erased_errors = 0;
778 range = (addr == NETXEN_USER_START) ?
779 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
781 for (i = addr; i < range; i += 4) {
782 netxen_rom_fast_read(adapter, i, &val);
783 if (val != 0xffffffff)
789 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
790 "for sector address: %x\n", erased_errors, count, addr);
793 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
796 if (rom_lock(adapter) != 0) {
799 ret = netxen_do_rom_se(adapter, addr);
800 netxen_rom_unlock(adapter);
802 check_erased_flash(adapter, addr);
807 static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
810 int ret = FLASH_SUCCESS;
813 for (i = start; i < end; i++) {
814 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
817 ret = netxen_rom_wip_poll(adapter);
826 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
828 int ret = FLASH_SUCCESS;
831 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
832 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
833 ret = netxen_flash_erase_sections(adapter, start, end);
839 netxen_flash_erase_primary(struct netxen_adapter *adapter)
841 int ret = FLASH_SUCCESS;
844 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
845 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
846 ret = netxen_flash_erase_sections(adapter, start, end);
851 void netxen_halt_pegs(struct netxen_adapter *adapter)
853 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
854 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
855 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
856 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
859 int netxen_flash_unlock(struct netxen_adapter *adapter)
863 ret = netxen_rom_wrsr(adapter, 0);
867 ret = netxen_rom_wren(adapter);
875 #define NETXEN_BOARDTYPE 0x4008
876 #define NETXEN_BOARDNUM 0x400c
877 #define NETXEN_CHIPNUM 0x4010
879 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
882 int i, init_delay = 0;
883 struct crb_addr_pair *buf;
888 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
892 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
893 printk("P2 ROM board type: 0x%08x\n", val);
895 printk("Could not read board type\n");
896 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
897 printk("P2 ROM board num: 0x%08x\n", val);
899 printk("Could not read board number\n");
900 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
901 printk("P2 ROM chip num: 0x%08x\n", val);
903 printk("Could not read chip number\n");
906 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
907 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
908 (n != 0xcafecafeUL) ||
909 netxen_rom_fast_read(adapter, 4, &n) != 0) {
910 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
911 "n: %08x\n", netxen_nic_driver_name, n);
914 offset = n & 0xffffU;
915 n = (n >> 16) & 0xffffU;
917 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
919 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
920 "n: %08x\n", netxen_nic_driver_name, n);
929 printk(KERN_DEBUG "%s: %d CRB init values found"
930 " in ROM.\n", netxen_nic_driver_name, n);
932 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
933 " initialized.\n", __func__, n);
937 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
939 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
940 netxen_nic_driver_name);
943 for (i = 0; i < n; i++) {
944 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
945 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0)
952 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
953 netxen_nic_driver_name,
954 (u32)netxen_decode_crb_addr(addr), val);
956 for (i = 0; i < n; i++) {
958 off = netxen_decode_crb_addr(buf[i].addr);
959 if (off == NETXEN_ADDR_ERROR) {
960 printk(KERN_ERR"CRB init value out of range %x\n",
964 off += NETXEN_PCI_CRBSPACE;
965 /* skipping cold reboot MAGIC */
966 if (off == NETXEN_CAM_RAM(0x1fc))
969 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
970 /* do not reset PCI */
971 if (off == (ROMUSB_GLB + 0xbc))
973 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
974 buf[i].data = 0x1020;
975 /* skip the function enable register */
976 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
978 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
980 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
984 if (off == NETXEN_ADDR_ERROR) {
985 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
986 netxen_nic_driver_name, buf[i].addr);
990 /* After writing this register, HW needs time for CRB */
991 /* to quiet down (else crb_window returns 0xffffffff) */
992 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
994 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
995 /* hold xdma in reset also */
996 buf[i].data = NETXEN_NIC_XDMA_RESET;
1000 adapter->hw_write_wx(adapter, off, &buf[i].data, 4);
1002 if (init_delay == 1) {
1010 /* disable_peg_cache_all */
1012 /* unreset_net_cache */
1013 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1014 adapter->hw_read_wx(adapter,
1015 NETXEN_ROMUSB_GLB_SW_RESET, &val, 4);
1016 netxen_crb_writelit_adapter(adapter,
1017 NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
1020 /* p2dn replyCount */
1021 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
1022 /* disable_peg_cache 0 */
1023 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
1024 /* disable_peg_cache 1 */
1025 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
1030 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
1031 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
1033 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
1034 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
1036 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
1037 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
1039 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
1040 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
1044 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
1050 adapter->dummy_dma.addr =
1051 pci_alloc_consistent(adapter->pdev,
1052 NETXEN_HOST_DUMMY_DMA_SIZE,
1053 &adapter->dummy_dma.phys_addr);
1054 if (adapter->dummy_dma.addr == NULL) {
1055 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
1060 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1061 hi = (addr >> 32) & 0xffffffff;
1062 lo = addr & 0xffffffff;
1064 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1065 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
1067 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1069 adapter->hw_write_wx(adapter, CRB_HOST_DUMMY_BUF, &temp, 4);
1075 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
1079 if (adapter->dummy_dma.addr) {
1082 if (dma_watchdog_shutdown_request(adapter) == 1)
1085 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
1090 pci_free_consistent(adapter->pdev,
1091 NETXEN_HOST_DUMMY_DMA_SIZE,
1092 adapter->dummy_dma.addr,
1093 adapter->dummy_dma.phys_addr);
1094 adapter->dummy_dma.addr = NULL;
1096 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
1097 adapter->netdev->name);
1102 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
1109 val = adapter->pci_read_normalize(adapter,
1112 if (val == PHAN_INITIALIZE_COMPLETE ||
1113 val == PHAN_INITIALIZE_ACK)
1118 } while (--retries);
1121 pegtune_val = adapter->pci_read_normalize(adapter,
1122 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
1123 printk(KERN_WARNING "netxen_phantom_init: init failed, "
1124 "pegtune_val=%x\n", pegtune_val);
1132 int netxen_receive_peg_ready(struct netxen_adapter *adapter)
1138 val = adapter->pci_read_normalize(adapter, CRB_RCVPEG_STATE);
1140 if (val == PHAN_PEG_RCV_INITIALIZED)
1145 } while (--retries);
1148 printk(KERN_ERR "Receive Peg initialization not "
1149 "complete, state: 0x%x.\n", val);
1156 static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1157 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1159 struct netxen_rx_buffer *buffer;
1160 struct sk_buff *skb;
1162 buffer = &rds_ring->rx_buf_arr[index];
1164 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1165 PCI_DMA_FROMDEVICE);
1171 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1172 adapter->stats.csummed++;
1173 skb->ip_summed = CHECKSUM_UNNECESSARY;
1175 skb->ip_summed = CHECKSUM_NONE;
1177 skb->dev = adapter->netdev;
1182 buffer->state = NETXEN_BUFFER_FREE;
1183 buffer->lro_current_frags = 0;
1184 buffer->lro_expected_frags = 0;
1185 list_add_tail(&buffer->list, &rds_ring->free_list);
1190 * netxen_process_rcv() send the received packet to the protocol stack.
1191 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1192 * invoke the routine to send more rx buffers to the Phantom...
1194 static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1195 struct status_desc *desc, struct status_desc *frag_desc)
1197 struct net_device *netdev = adapter->netdev;
1198 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1199 int index = netxen_get_sts_refhandle(sts_data);
1200 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1201 struct netxen_rx_buffer *buffer;
1202 struct sk_buff *skb;
1203 u32 length = netxen_get_sts_totallength(sts_data);
1205 u16 pkt_offset = 0, cksum;
1206 struct nx_host_rds_ring *rds_ring;
1208 desc_ctx = netxen_get_sts_type(sts_data);
1209 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1210 printk("%s: %s Bad Rcv descriptor ring\n",
1211 netxen_nic_driver_name, netdev->name);
1215 rds_ring = &recv_ctx->rds_rings[desc_ctx];
1216 if (unlikely(index > rds_ring->max_rx_desc_count)) {
1217 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1218 index, rds_ring->max_rx_desc_count);
1221 buffer = &rds_ring->rx_buf_arr[index];
1222 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1223 buffer->lro_current_frags++;
1224 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1225 buffer->lro_expected_frags =
1226 netxen_get_sts_desc_lro_cnt(desc);
1227 buffer->lro_length = length;
1229 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1230 if (buffer->lro_expected_frags != 0) {
1231 printk("LRO: (refhandle:%x) recv frag. "
1232 "wait for last. flags: %x expected:%d "
1234 netxen_get_sts_desc_lro_last_frag(desc),
1235 buffer->lro_expected_frags,
1236 buffer->lro_current_frags);
1242 cksum = netxen_get_sts_status(sts_data);
1244 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1248 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1249 /* True length was only available on the last pkt */
1250 skb_put(skb, buffer->lro_length);
1252 if (length > rds_ring->skb_size)
1253 skb_put(skb, rds_ring->skb_size);
1255 skb_put(skb, length);
1257 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1259 skb_pull(skb, pkt_offset);
1262 skb->protocol = eth_type_trans(skb, netdev);
1265 * rx buffer chaining is disabled, walk and free
1266 * any spurious rx buffer chain.
1269 u16 i, nr_frags = desc->nr_frags;
1271 dev_kfree_skb_any(skb);
1272 for (i = 0; i < nr_frags; i++) {
1273 index = frag_desc->frag_handles[i];
1274 skb = netxen_process_rxbuf(adapter,
1275 rds_ring, index, cksum);
1277 dev_kfree_skb_any(skb);
1279 adapter->stats.rxdropped++;
1282 netif_receive_skb(skb);
1283 netdev->last_rx = jiffies;
1285 adapter->stats.no_rcv++;
1286 adapter->stats.rxbytes += length;
1290 /* Process Receive status ring */
1291 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1293 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1294 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1295 struct status_desc *desc, *frag_desc;
1296 u32 consumer = recv_ctx->status_rx_consumer;
1297 int count = 0, ring;
1301 while (count < max) {
1302 desc = &desc_head[consumer];
1303 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1304 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1305 netxen_get_sts_owner(desc));
1309 sts_data = le64_to_cpu(desc->status_desc_data);
1310 opcode = netxen_get_sts_opcode(sts_data);
1312 if (opcode == NETXEN_NIC_RXPKT_DESC) {
1313 if (desc->nr_frags) {
1314 consumer = get_next_index(consumer,
1315 adapter->max_rx_desc_count);
1316 frag_desc = &desc_head[consumer];
1317 netxen_set_sts_owner(frag_desc,
1318 STATUS_OWNER_PHANTOM);
1322 netxen_process_rcv(adapter, ctxid, desc, frag_desc);
1324 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1326 consumer = get_next_index(consumer,
1327 adapter->max_rx_desc_count);
1330 for (ring = 0; ring < adapter->max_rds_rings; ring++)
1331 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1333 /* update the consumer index in phantom */
1335 recv_ctx->status_rx_consumer = consumer;
1338 adapter->pci_write_normalize(adapter,
1339 recv_ctx->crb_sts_consumer, consumer);
1345 /* Process Command status ring */
1346 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1348 u32 last_consumer, consumer;
1350 struct netxen_cmd_buffer *buffer;
1351 struct pci_dev *pdev = adapter->pdev;
1352 struct net_device *netdev = adapter->netdev;
1353 struct netxen_skb_frag *frag;
1356 last_consumer = adapter->last_cmd_consumer;
1357 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1359 while (last_consumer != consumer) {
1360 buffer = &adapter->cmd_buf_arr[last_consumer];
1362 frag = &buffer->frag_array[0];
1363 pci_unmap_single(pdev, frag->dma, frag->length,
1366 for (i = 1; i < buffer->frag_count; i++) {
1367 frag++; /* Get the next frag */
1368 pci_unmap_page(pdev, frag->dma, frag->length,
1373 adapter->stats.xmitfinished++;
1374 dev_kfree_skb_any(buffer->skb);
1378 last_consumer = get_next_index(last_consumer,
1379 adapter->max_tx_desc_count);
1380 if (++count >= MAX_STATUS_HANDLE)
1385 adapter->last_cmd_consumer = last_consumer;
1387 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1388 netif_tx_lock(netdev);
1389 netif_wake_queue(netdev);
1391 netif_tx_unlock(netdev);
1395 * If everything is freed up to consumer then check if the ring is full
1396 * If the ring is full then check if more needs to be freed and
1397 * schedule the call back again.
1399 * This happens when there are 2 CPUs. One could be freeing and the
1400 * other filling it. If the ring is full when we get out of here and
1401 * the card has already interrupted the host then the host can miss the
1404 * There is still a possible race condition and the host could miss an
1405 * interrupt. The card has to take care of this.
1407 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1408 done = (last_consumer == consumer);
1414 * netxen_post_rx_buffers puts buffer in the Phantom memory
1416 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1418 struct pci_dev *pdev = adapter->pdev;
1419 struct sk_buff *skb;
1420 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1421 struct nx_host_rds_ring *rds_ring = NULL;
1423 struct rcv_desc *pdesc;
1424 struct netxen_rx_buffer *buffer;
1427 netxen_ctx_msg msg = 0;
1429 struct list_head *head;
1431 rds_ring = &recv_ctx->rds_rings[ringid];
1433 producer = rds_ring->producer;
1434 index = rds_ring->begin_alloc;
1435 head = &rds_ring->free_list;
1437 /* We can start writing rx descriptors into the phantom memory. */
1438 while (!list_empty(head)) {
1440 skb = dev_alloc_skb(rds_ring->skb_size);
1441 if (unlikely(!skb)) {
1442 rds_ring->begin_alloc = index;
1446 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1447 list_del(&buffer->list);
1449 count++; /* now there should be no failure */
1450 pdesc = &rds_ring->desc_head[producer];
1452 if (!adapter->ahw.cut_through)
1453 skb_reserve(skb, 2);
1454 /* This will be setup when we receive the
1455 * buffer after it has been filled FSL TBD TBD
1456 * skb->dev = netdev;
1458 dma = pci_map_single(pdev, skb->data, rds_ring->dma_size,
1459 PCI_DMA_FROMDEVICE);
1460 pdesc->addr_buffer = cpu_to_le64(dma);
1462 buffer->state = NETXEN_BUFFER_BUSY;
1464 /* make a rcv descriptor */
1465 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1466 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1467 DPRINTK(INFO, "done writing descripter\n");
1469 get_next_index(producer, rds_ring->max_rx_desc_count);
1470 index = get_next_index(index, rds_ring->max_rx_desc_count);
1472 /* if we did allocate buffers, then write the count to Phantom */
1474 rds_ring->begin_alloc = index;
1475 rds_ring->producer = producer;
1477 adapter->pci_write_normalize(adapter,
1478 rds_ring->crb_rcv_producer,
1479 (producer-1) & (rds_ring->max_rx_desc_count-1));
1481 if (adapter->fw_major < 4) {
1483 * Write a doorbell msg to tell phanmon of change in
1484 * receive ring producer
1485 * Only for firmware version < 4.0.0
1487 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1488 netxen_set_msg_privid(msg);
1489 netxen_set_msg_count(msg,
1492 max_rx_desc_count - 1)));
1493 netxen_set_msg_ctxid(msg, adapter->portnum);
1494 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1496 DB_NORMALIZE(adapter,
1497 NETXEN_RCV_PRODUCER_OFFSET));
1502 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1503 uint32_t ctx, uint32_t ringid)
1505 struct pci_dev *pdev = adapter->pdev;
1506 struct sk_buff *skb;
1507 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1508 struct nx_host_rds_ring *rds_ring = NULL;
1510 struct rcv_desc *pdesc;
1511 struct netxen_rx_buffer *buffer;
1514 struct list_head *head;
1516 rds_ring = &recv_ctx->rds_rings[ringid];
1518 producer = rds_ring->producer;
1519 index = rds_ring->begin_alloc;
1520 head = &rds_ring->free_list;
1521 /* We can start writing rx descriptors into the phantom memory. */
1522 while (!list_empty(head)) {
1524 skb = dev_alloc_skb(rds_ring->skb_size);
1525 if (unlikely(!skb)) {
1526 rds_ring->begin_alloc = index;
1530 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1531 list_del(&buffer->list);
1533 count++; /* now there should be no failure */
1534 pdesc = &rds_ring->desc_head[producer];
1535 if (!adapter->ahw.cut_through)
1536 skb_reserve(skb, 2);
1538 buffer->state = NETXEN_BUFFER_BUSY;
1539 buffer->dma = pci_map_single(pdev, skb->data,
1541 PCI_DMA_FROMDEVICE);
1543 /* make a rcv descriptor */
1544 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1545 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1546 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1548 get_next_index(producer, rds_ring->max_rx_desc_count);
1549 index = get_next_index(index, rds_ring->max_rx_desc_count);
1550 buffer = &rds_ring->rx_buf_arr[index];
1553 /* if we did allocate buffers, then write the count to Phantom */
1555 rds_ring->begin_alloc = index;
1556 rds_ring->producer = producer;
1558 adapter->pci_write_normalize(adapter,
1559 rds_ring->crb_rcv_producer,
1560 (producer-1) & (rds_ring->max_rx_desc_count-1));
1565 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1567 memset(&adapter->stats, 0, sizeof(adapter->stats));