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 unsigned long last_schedule_time;
47 #define NETXEN_MAX_CRB_XFORM 60
48 static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
49 #define NETXEN_ADDR_ERROR (0xffffffff)
51 #define crb_addr_transform(name) \
52 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
53 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
55 #define NETXEN_NIC_XDMA_RESET 0x8000ff
57 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
58 uint32_t ctx, uint32_t ringid);
61 static void netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
62 unsigned long off, int *data)
64 void __iomem *addr = pci_base_offset(adapter, off);
69 static void crb_addr_transform_setup(void)
71 crb_addr_transform(XDMA);
72 crb_addr_transform(TIMR);
73 crb_addr_transform(SRE);
74 crb_addr_transform(SQN3);
75 crb_addr_transform(SQN2);
76 crb_addr_transform(SQN1);
77 crb_addr_transform(SQN0);
78 crb_addr_transform(SQS3);
79 crb_addr_transform(SQS2);
80 crb_addr_transform(SQS1);
81 crb_addr_transform(SQS0);
82 crb_addr_transform(RPMX7);
83 crb_addr_transform(RPMX6);
84 crb_addr_transform(RPMX5);
85 crb_addr_transform(RPMX4);
86 crb_addr_transform(RPMX3);
87 crb_addr_transform(RPMX2);
88 crb_addr_transform(RPMX1);
89 crb_addr_transform(RPMX0);
90 crb_addr_transform(ROMUSB);
91 crb_addr_transform(SN);
92 crb_addr_transform(QMN);
93 crb_addr_transform(QMS);
94 crb_addr_transform(PGNI);
95 crb_addr_transform(PGND);
96 crb_addr_transform(PGN3);
97 crb_addr_transform(PGN2);
98 crb_addr_transform(PGN1);
99 crb_addr_transform(PGN0);
100 crb_addr_transform(PGSI);
101 crb_addr_transform(PGSD);
102 crb_addr_transform(PGS3);
103 crb_addr_transform(PGS2);
104 crb_addr_transform(PGS1);
105 crb_addr_transform(PGS0);
106 crb_addr_transform(PS);
107 crb_addr_transform(PH);
108 crb_addr_transform(NIU);
109 crb_addr_transform(I2Q);
110 crb_addr_transform(EG);
111 crb_addr_transform(MN);
112 crb_addr_transform(MS);
113 crb_addr_transform(CAS2);
114 crb_addr_transform(CAS1);
115 crb_addr_transform(CAS0);
116 crb_addr_transform(CAM);
117 crb_addr_transform(C2C1);
118 crb_addr_transform(C2C0);
119 crb_addr_transform(SMB);
122 int netxen_init_firmware(struct netxen_adapter *adapter)
124 u32 state = 0, loops = 0, err = 0;
127 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
129 if (state == PHAN_INITIALIZE_ACK)
132 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
135 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
140 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
146 writel(INTR_SCHEME_PERPORT,
147 NETXEN_CRB_NORMALIZE(adapter, CRB_NIC_CAPABILITIES_HOST));
148 writel(MSI_MODE_MULTIFUNC,
149 NETXEN_CRB_NORMALIZE(adapter, CRB_NIC_MSI_MODE_HOST));
150 writel(MPORT_MULTI_FUNCTION_MODE,
151 NETXEN_CRB_NORMALIZE(adapter, CRB_MPORT_MODE));
152 writel(PHAN_INITIALIZE_ACK,
153 NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
158 #define NETXEN_ADDR_LIMIT 0xffffffffULL
160 void *netxen_alloc(struct pci_dev *pdev, size_t sz, dma_addr_t * ptr,
161 struct pci_dev **used_dev)
165 addr = pci_alloc_consistent(pdev, sz, ptr);
166 if ((unsigned long long)(*ptr) < NETXEN_ADDR_LIMIT) {
170 pci_free_consistent(pdev, sz, addr, *ptr);
171 addr = pci_alloc_consistent(NULL, sz, ptr);
176 void netxen_initialize_adapter_sw(struct netxen_adapter *adapter)
181 struct netxen_rcv_desc_ctx *rcv_desc;
183 DPRINTK(INFO, "initializing some queues: %p\n", adapter);
184 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
185 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
186 struct netxen_rx_buffer *rx_buf;
187 rcv_desc = &adapter->recv_ctx[ctxid].rcv_desc[ring];
188 rcv_desc->begin_alloc = 0;
189 rx_buf = rcv_desc->rx_buf_arr;
190 num_rx_bufs = rcv_desc->max_rx_desc_count;
192 * Now go through all of them, set reference handles
193 * and put them in the queues.
195 for (i = 0; i < num_rx_bufs; i++) {
196 rx_buf->ref_handle = i;
197 rx_buf->state = NETXEN_BUFFER_FREE;
198 DPRINTK(INFO, "Rx buf:ctx%d i(%d) rx_buf:"
199 "%p\n", ctxid, i, rx_buf);
206 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
208 switch (adapter->ahw.board_type) {
210 adapter->enable_phy_interrupts =
211 netxen_niu_gbe_enable_phy_interrupts;
212 adapter->disable_phy_interrupts =
213 netxen_niu_gbe_disable_phy_interrupts;
214 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
215 adapter->macaddr_set = netxen_niu_macaddr_set;
216 adapter->set_mtu = netxen_nic_set_mtu_gb;
217 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
218 adapter->unset_promisc = netxen_niu_set_promiscuous_mode;
219 adapter->phy_read = netxen_niu_gbe_phy_read;
220 adapter->phy_write = netxen_niu_gbe_phy_write;
221 adapter->init_niu = netxen_nic_init_niu_gb;
222 adapter->stop_port = netxen_niu_disable_gbe_port;
225 case NETXEN_NIC_XGBE:
226 adapter->enable_phy_interrupts =
227 netxen_niu_xgbe_enable_phy_interrupts;
228 adapter->disable_phy_interrupts =
229 netxen_niu_xgbe_disable_phy_interrupts;
230 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
231 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
232 adapter->set_mtu = netxen_nic_set_mtu_xgb;
233 adapter->init_port = netxen_niu_xg_init_port;
234 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
235 adapter->unset_promisc = netxen_niu_xg_set_promiscuous_mode;
236 adapter->stop_port = netxen_niu_disable_xg_port;
245 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
246 * address to external PCI CRB address.
248 static u32 netxen_decode_crb_addr(u32 addr)
251 u32 base_addr, offset, pci_base;
253 crb_addr_transform_setup();
255 pci_base = NETXEN_ADDR_ERROR;
256 base_addr = addr & 0xfff00000;
257 offset = addr & 0x000fffff;
259 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
260 if (crb_addr_xform[i] == base_addr) {
265 if (pci_base == NETXEN_ADDR_ERROR)
268 return (pci_base + offset);
271 static long rom_max_timeout = 100;
272 static long rom_lock_timeout = 10000;
273 static long rom_write_timeout = 700;
275 static int rom_lock(struct netxen_adapter *adapter)
282 /* acquire semaphore2 from PCI HW block */
283 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
287 if (timeout >= rom_lock_timeout)
297 for (iter = 0; iter < 20; iter++)
298 cpu_relax(); /*This a nop instr on i386 */
301 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
305 static int netxen_wait_rom_done(struct netxen_adapter *adapter)
311 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
314 if (timeout >= rom_max_timeout) {
315 printk("Timeout reached waiting for rom done");
322 static int netxen_rom_wren(struct netxen_adapter *adapter)
324 /* Set write enable latch in ROM status register */
325 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
326 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
328 if (netxen_wait_rom_done(adapter)) {
334 static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
337 unsigned int data = 0xdeaddead;
338 data = netxen_nic_reg_read(adapter, addr);
342 static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
344 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
346 if (netxen_wait_rom_done(adapter)) {
349 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
352 static void netxen_rom_unlock(struct netxen_adapter *adapter)
356 /* release semaphore2 */
357 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
361 static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
366 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
368 val = netxen_do_rom_rdsr(adapter);
371 if (timeout > rom_max_timeout) {
378 static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
381 if (netxen_rom_wren(adapter)) {
384 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
385 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
386 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
387 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
389 if (netxen_wait_rom_done(adapter)) {
390 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
394 return netxen_rom_wip_poll(adapter);
397 static int do_rom_fast_read(struct netxen_adapter *adapter,
402 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
403 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
404 udelay(100); /* prevent bursting on CRB */
405 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
406 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
407 if (netxen_wait_rom_done(adapter)) {
408 printk("Error waiting for rom done\n");
411 /* reset abyte_cnt and dummy_byte_cnt */
412 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
413 udelay(100); /* prevent bursting on CRB */
414 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
416 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
420 static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
421 u8 *bytes, size_t size)
426 for (addridx = addr; addridx < (addr + size); addridx += 4) {
428 ret = do_rom_fast_read(adapter, addridx, &v);
431 *(__le32 *)bytes = cpu_to_le32(v);
439 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
440 u8 *bytes, size_t size)
444 ret = rom_lock(adapter);
448 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
450 netxen_rom_unlock(adapter);
454 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
458 if (rom_lock(adapter) != 0)
461 ret = do_rom_fast_read(adapter, addr, valp);
462 netxen_rom_unlock(adapter);
467 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
471 if (rom_lock(adapter) != 0) {
474 ret = do_rom_fast_write(adapter, addr, data);
475 netxen_rom_unlock(adapter);
480 static int do_rom_fast_write_words(struct netxen_adapter *adapter,
481 int addr, u8 *bytes, size_t size)
486 while (addridx < (addr + size)) {
487 int last_attempt = 0;
491 data = le32_to_cpu((*(__le32*)bytes));
492 ret = do_rom_fast_write(adapter, addridx, data);
499 ret = do_rom_fast_read(adapter, addridx, &data1);
506 if (timeout++ >= rom_write_timeout) {
507 if (last_attempt++ < 4) {
508 ret = do_rom_fast_write(adapter,
514 printk(KERN_INFO "Data write did not "
515 "succeed at address 0x%x\n", addridx);
528 int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
529 u8 *bytes, size_t size)
533 ret = rom_lock(adapter);
537 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
538 netxen_rom_unlock(adapter);
543 static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
547 ret = netxen_rom_wren(adapter);
551 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
552 netxen_crb_writelit_adapter(adapter,
553 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
555 ret = netxen_wait_rom_done(adapter);
559 return netxen_rom_wip_poll(adapter);
562 static int netxen_rom_rdsr(struct netxen_adapter *adapter)
566 ret = rom_lock(adapter);
570 ret = netxen_do_rom_rdsr(adapter);
571 netxen_rom_unlock(adapter);
575 int netxen_backup_crbinit(struct netxen_adapter *adapter)
577 int ret = FLASH_SUCCESS;
579 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
583 /* unlock sector 63 */
584 val = netxen_rom_rdsr(adapter);
586 ret = netxen_rom_wrsr(adapter, val);
587 if (ret != FLASH_SUCCESS)
590 ret = netxen_rom_wip_poll(adapter);
591 if (ret != FLASH_SUCCESS)
594 /* copy sector 0 to sector 63 */
595 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
596 buffer, NETXEN_FLASH_SECTOR_SIZE);
597 if (ret != FLASH_SUCCESS)
600 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
601 buffer, NETXEN_FLASH_SECTOR_SIZE);
602 if (ret != FLASH_SUCCESS)
606 val = netxen_rom_rdsr(adapter);
610 if (netxen_rom_wrsr(adapter, val) == 0) {
611 ret = netxen_rom_wip_poll(adapter);
612 if (ret != FLASH_SUCCESS)
616 ret = netxen_rom_wip_poll(adapter);
617 if (ret != FLASH_SUCCESS)
627 static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
629 netxen_rom_wren(adapter);
630 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
631 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
632 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
634 if (netxen_wait_rom_done(adapter)) {
635 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
638 return netxen_rom_wip_poll(adapter);
641 static void check_erased_flash(struct netxen_adapter *adapter, int addr)
645 int count = 0, erased_errors = 0;
648 range = (addr == NETXEN_USER_START) ?
649 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
651 for (i = addr; i < range; i += 4) {
652 netxen_rom_fast_read(adapter, i, &val);
653 if (val != 0xffffffff)
659 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
660 "for sector address: %x\n", erased_errors, count, addr);
663 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
666 if (rom_lock(adapter) != 0) {
669 ret = netxen_do_rom_se(adapter, addr);
670 netxen_rom_unlock(adapter);
672 check_erased_flash(adapter, addr);
677 static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
680 int ret = FLASH_SUCCESS;
683 for (i = start; i < end; i++) {
684 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
687 ret = netxen_rom_wip_poll(adapter);
696 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
698 int ret = FLASH_SUCCESS;
701 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
702 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
703 ret = netxen_flash_erase_sections(adapter, start, end);
709 netxen_flash_erase_primary(struct netxen_adapter *adapter)
711 int ret = FLASH_SUCCESS;
714 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
715 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
716 ret = netxen_flash_erase_sections(adapter, start, end);
721 void netxen_halt_pegs(struct netxen_adapter *adapter)
723 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
724 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
725 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
726 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
729 int netxen_flash_unlock(struct netxen_adapter *adapter)
733 ret = netxen_rom_wrsr(adapter, 0);
737 ret = netxen_rom_wren(adapter);
744 #define NETXEN_BOARDTYPE 0x4008
745 #define NETXEN_BOARDNUM 0x400c
746 #define NETXEN_CHIPNUM 0x4010
747 #define NETXEN_ROMBUS_RESET 0xFFFFFFFF
748 #define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
749 #define NETXEN_ROM_FOUND_INIT 0x400
751 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
756 struct crb_addr_pair *buf;
760 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
761 NETXEN_ROMBUS_RESET);
765 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
766 printk("P2 ROM board type: 0x%08x\n", val);
768 printk("Could not read board type\n");
769 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
770 printk("P2 ROM board num: 0x%08x\n", val);
772 printk("Could not read board number\n");
773 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
774 printk("P2 ROM chip num: 0x%08x\n", val);
776 printk("Could not read chip number\n");
779 if (netxen_rom_fast_read(adapter, 0, &n) == 0
780 && (n & NETXEN_ROM_FIRST_BARRIER)) {
781 n &= ~NETXEN_ROM_ROUNDUP;
782 if (n < NETXEN_ROM_FOUND_INIT) {
784 printk("%s: %d CRB init values found"
785 " in ROM.\n", netxen_nic_driver_name, n);
787 printk("%s:n=0x%x Error! NetXen card flash not"
788 " initialized.\n", __FUNCTION__, n);
791 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
793 printk("%s: netxen_pinit_from_rom: Unable to calloc "
794 "memory.\n", netxen_nic_driver_name);
797 for (i = 0; i < n; i++) {
798 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
799 || netxen_rom_fast_read(adapter, 8 * i + 8,
807 printk("%s: PCI: 0x%08x == 0x%08x\n",
808 netxen_nic_driver_name, (unsigned int)
809 netxen_decode_crb_addr(addr), val);
811 for (i = 0; i < n; i++) {
813 off = netxen_decode_crb_addr(buf[i].addr);
814 if (off == NETXEN_ADDR_ERROR) {
815 printk(KERN_ERR"CRB init value out of range %x\n",
819 off += NETXEN_PCI_CRBSPACE;
820 /* skipping cold reboot MAGIC */
821 if (off == NETXEN_CAM_RAM(0x1fc))
824 /* After writing this register, HW needs time for CRB */
825 /* to quiet down (else crb_window returns 0xffffffff) */
826 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
828 /* hold xdma in reset also */
829 buf[i].data = NETXEN_NIC_XDMA_RESET;
832 if (ADDR_IN_WINDOW1(off)) {
834 NETXEN_CRB_NORMALIZE(adapter, off));
836 netxen_nic_pci_change_crbwindow(adapter, 0);
838 pci_base_offset(adapter, off));
840 netxen_nic_pci_change_crbwindow(adapter, 1);
842 if (init_delay == 1) {
850 /* disable_peg_cache_all */
852 /* unreset_net_cache */
853 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
855 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
857 /* p2dn replyCount */
858 netxen_crb_writelit_adapter(adapter,
859 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
860 /* disable_peg_cache 0 */
861 netxen_crb_writelit_adapter(adapter,
862 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
863 /* disable_peg_cache 1 */
864 netxen_crb_writelit_adapter(adapter,
865 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
870 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
872 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
875 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
877 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
880 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
882 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
885 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
887 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
893 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
899 adapter->dummy_dma.addr =
900 pci_alloc_consistent(adapter->ahw.pdev,
901 NETXEN_HOST_DUMMY_DMA_SIZE,
902 &adapter->dummy_dma.phys_addr);
903 if (adapter->dummy_dma.addr == NULL) {
904 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
909 addr = (uint64_t) adapter->dummy_dma.phys_addr;
910 hi = (addr >> 32) & 0xffffffff;
911 lo = addr & 0xffffffff;
913 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
914 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
919 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
923 if (adapter->dummy_dma.addr) {
926 if (dma_watchdog_shutdown_request(adapter) == 1)
929 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
934 pci_free_consistent(adapter->ahw.pdev,
935 NETXEN_HOST_DUMMY_DMA_SIZE,
936 adapter->dummy_dma.addr,
937 adapter->dummy_dma.phys_addr);
938 adapter->dummy_dma.addr = NULL;
940 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
941 adapter->netdev->name);
946 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
953 val = readl(NETXEN_CRB_NORMALIZE
954 (adapter, CRB_CMDPEG_STATE));
955 pegtune_val = readl(NETXEN_CRB_NORMALIZE
956 (adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
958 if (val == PHAN_INITIALIZE_COMPLETE ||
959 val == PHAN_INITIALIZE_ACK)
965 printk(KERN_WARNING "netxen_phantom_init: init failed, "
966 "pegtune_val=%x\n", pegtune_val);
974 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
976 struct net_device *netdev = adapter->netdev;
977 uint32_t temp, temp_state, temp_val;
980 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
982 temp_state = nx_get_temp_state(temp);
983 temp_val = nx_get_temp_val(temp);
985 if (temp_state == NX_TEMP_PANIC) {
987 "%s: Device temperature %d degrees C exceeds"
988 " maximum allowed. Hardware has been shut down.\n",
989 netxen_nic_driver_name, temp_val);
991 netif_carrier_off(netdev);
992 netif_stop_queue(netdev);
994 } else if (temp_state == NX_TEMP_WARN) {
995 if (adapter->temp == NX_TEMP_NORMAL) {
997 "%s: Device temperature %d degrees C "
998 "exceeds operating range."
999 " Immediate action needed.\n",
1000 netxen_nic_driver_name, temp_val);
1003 if (adapter->temp == NX_TEMP_WARN) {
1005 "%s: Device temperature is now %d degrees C"
1006 " in normal range.\n", netxen_nic_driver_name,
1010 adapter->temp = temp_state;
1014 void netxen_watchdog_task(struct work_struct *work)
1016 struct netxen_adapter *adapter =
1017 container_of(work, struct netxen_adapter, watchdog_task);
1019 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1022 if (adapter->handle_phy_intr)
1023 adapter->handle_phy_intr(adapter);
1025 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1029 * netxen_process_rcv() send the received packet to the protocol stack.
1030 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1031 * invoke the routine to send more rx buffers to the Phantom...
1033 static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1034 struct status_desc *desc)
1036 struct pci_dev *pdev = adapter->pdev;
1037 struct net_device *netdev = adapter->netdev;
1038 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1039 int index = netxen_get_sts_refhandle(sts_data);
1040 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1041 struct netxen_rx_buffer *buffer;
1042 struct sk_buff *skb;
1043 u32 length = netxen_get_sts_totallength(sts_data);
1045 struct netxen_rcv_desc_ctx *rcv_desc;
1048 desc_ctx = netxen_get_sts_type(sts_data);
1049 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1050 printk("%s: %s Bad Rcv descriptor ring\n",
1051 netxen_nic_driver_name, netdev->name);
1055 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
1056 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1057 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1058 index, rcv_desc->max_rx_desc_count);
1061 buffer = &rcv_desc->rx_buf_arr[index];
1062 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1063 buffer->lro_current_frags++;
1064 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1065 buffer->lro_expected_frags =
1066 netxen_get_sts_desc_lro_cnt(desc);
1067 buffer->lro_length = length;
1069 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1070 if (buffer->lro_expected_frags != 0) {
1071 printk("LRO: (refhandle:%x) recv frag. "
1072 "wait for last. flags: %x expected:%d "
1074 netxen_get_sts_desc_lro_last_frag(desc),
1075 buffer->lro_expected_frags,
1076 buffer->lro_current_frags);
1082 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1083 PCI_DMA_FROMDEVICE);
1085 skb = (struct sk_buff *)buffer->skb;
1087 if (likely(adapter->rx_csum &&
1088 netxen_get_sts_status(sts_data) == STATUS_CKSUM_OK)) {
1089 adapter->stats.csummed++;
1090 skb->ip_summed = CHECKSUM_UNNECESSARY;
1092 skb->ip_summed = CHECKSUM_NONE;
1095 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1096 /* True length was only available on the last pkt */
1097 skb_put(skb, buffer->lro_length);
1099 skb_put(skb, length);
1102 skb->protocol = eth_type_trans(skb, netdev);
1104 ret = netif_receive_skb(skb);
1105 netdev->last_rx = jiffies;
1107 rcv_desc->rcv_pending--;
1110 * We just consumed one buffer so post a buffer.
1113 buffer->state = NETXEN_BUFFER_FREE;
1114 buffer->lro_current_frags = 0;
1115 buffer->lro_expected_frags = 0;
1117 adapter->stats.no_rcv++;
1118 adapter->stats.rxbytes += length;
1121 /* Process Receive status ring */
1122 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1124 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1125 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1126 struct status_desc *desc; /* used to read status desc here */
1127 u32 consumer = recv_ctx->status_rx_consumer;
1129 int count = 0, ring;
1131 while (count < max) {
1132 desc = &desc_head[consumer];
1133 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1134 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1135 netxen_get_sts_owner(desc));
1138 netxen_process_rcv(adapter, ctxid, desc);
1139 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1140 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1143 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
1144 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1146 /* update the consumer index in phantom */
1148 recv_ctx->status_rx_consumer = consumer;
1149 recv_ctx->status_rx_producer = producer;
1153 NETXEN_CRB_NORMALIZE(adapter,
1154 recv_crb_registers[adapter->portnum].
1155 crb_rcv_status_consumer));
1161 /* Process Command status ring */
1162 int netxen_process_cmd_ring(struct netxen_adapter *adapter)
1164 u32 last_consumer, consumer;
1166 struct netxen_cmd_buffer *buffer;
1167 struct pci_dev *pdev = adapter->pdev;
1168 struct net_device *netdev = adapter->netdev;
1169 struct netxen_skb_frag *frag;
1172 last_consumer = adapter->last_cmd_consumer;
1173 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1175 while (last_consumer != consumer) {
1176 buffer = &adapter->cmd_buf_arr[last_consumer];
1178 frag = &buffer->frag_array[0];
1179 pci_unmap_single(pdev, frag->dma, frag->length,
1182 for (i = 1; i < buffer->frag_count; i++) {
1183 frag++; /* Get the next frag */
1184 pci_unmap_page(pdev, frag->dma, frag->length,
1189 adapter->stats.xmitfinished++;
1190 dev_kfree_skb_any(buffer->skb);
1194 last_consumer = get_next_index(last_consumer,
1195 adapter->max_tx_desc_count);
1196 if (++count >= MAX_STATUS_HANDLE)
1201 adapter->last_cmd_consumer = last_consumer;
1203 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1204 netif_tx_lock(netdev);
1205 netif_wake_queue(netdev);
1207 netif_tx_unlock(netdev);
1211 * If everything is freed up to consumer then check if the ring is full
1212 * If the ring is full then check if more needs to be freed and
1213 * schedule the call back again.
1215 * This happens when there are 2 CPUs. One could be freeing and the
1216 * other filling it. If the ring is full when we get out of here and
1217 * the card has already interrupted the host then the host can miss the
1220 * There is still a possible race condition and the host could miss an
1221 * interrupt. The card has to take care of this.
1223 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1224 done = (last_consumer == consumer);
1230 * netxen_post_rx_buffers puts buffer in the Phantom memory
1232 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1234 struct pci_dev *pdev = adapter->ahw.pdev;
1235 struct sk_buff *skb;
1236 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1237 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1239 struct rcv_desc *pdesc;
1240 struct netxen_rx_buffer *buffer;
1243 netxen_ctx_msg msg = 0;
1246 rcv_desc = &recv_ctx->rcv_desc[ringid];
1248 producer = rcv_desc->producer;
1249 index = rcv_desc->begin_alloc;
1250 buffer = &rcv_desc->rx_buf_arr[index];
1251 /* We can start writing rx descriptors into the phantom memory. */
1252 while (buffer->state == NETXEN_BUFFER_FREE) {
1253 skb = dev_alloc_skb(rcv_desc->skb_size);
1254 if (unlikely(!skb)) {
1257 * We need to schedule the posting of buffers to the pegs.
1259 rcv_desc->begin_alloc = index;
1260 DPRINTK(ERR, "netxen_post_rx_buffers: "
1261 " allocated only %d buffers\n", count);
1265 count++; /* now there should be no failure */
1266 pdesc = &rcv_desc->desc_head[producer];
1268 #if defined(XGB_DEBUG)
1269 *(unsigned long *)(skb->head) = 0xc0debabe;
1270 if (skb_is_nonlinear(skb)) {
1271 printk("Allocated SKB @%p is nonlinear\n");
1274 skb_reserve(skb, 2);
1275 /* This will be setup when we receive the
1276 * buffer after it has been filled FSL TBD TBD
1277 * skb->dev = netdev;
1279 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1280 PCI_DMA_FROMDEVICE);
1281 pdesc->addr_buffer = cpu_to_le64(dma);
1283 buffer->state = NETXEN_BUFFER_BUSY;
1285 /* make a rcv descriptor */
1286 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1287 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1288 DPRINTK(INFO, "done writing descripter\n");
1290 get_next_index(producer, rcv_desc->max_rx_desc_count);
1291 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1292 buffer = &rcv_desc->rx_buf_arr[index];
1294 /* if we did allocate buffers, then write the count to Phantom */
1296 rcv_desc->begin_alloc = index;
1297 rcv_desc->rcv_pending += count;
1298 rcv_desc->producer = producer;
1300 writel((producer - 1) &
1301 (rcv_desc->max_rx_desc_count - 1),
1302 NETXEN_CRB_NORMALIZE(adapter,
1305 rcv_desc_crb[ringid].
1306 crb_rcv_producer_offset));
1308 * Write a doorbell msg to tell phanmon of change in
1309 * receive ring producer
1311 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1312 netxen_set_msg_privid(msg);
1313 netxen_set_msg_count(msg,
1316 max_rx_desc_count - 1)));
1317 netxen_set_msg_ctxid(msg, adapter->portnum);
1318 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1320 DB_NORMALIZE(adapter,
1321 NETXEN_RCV_PRODUCER_OFFSET));
1325 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1326 uint32_t ctx, uint32_t ringid)
1328 struct pci_dev *pdev = adapter->ahw.pdev;
1329 struct sk_buff *skb;
1330 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1331 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1333 struct rcv_desc *pdesc;
1334 struct netxen_rx_buffer *buffer;
1338 rcv_desc = &recv_ctx->rcv_desc[ringid];
1340 producer = rcv_desc->producer;
1341 index = rcv_desc->begin_alloc;
1342 buffer = &rcv_desc->rx_buf_arr[index];
1343 /* We can start writing rx descriptors into the phantom memory. */
1344 while (buffer->state == NETXEN_BUFFER_FREE) {
1345 skb = dev_alloc_skb(rcv_desc->skb_size);
1346 if (unlikely(!skb)) {
1348 * We need to schedule the posting of buffers to the pegs.
1350 rcv_desc->begin_alloc = index;
1351 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
1352 " allocated only %d buffers\n", count);
1355 count++; /* now there should be no failure */
1356 pdesc = &rcv_desc->desc_head[producer];
1357 skb_reserve(skb, 2);
1359 * This will be setup when we receive the
1360 * buffer after it has been filled
1361 * skb->dev = netdev;
1364 buffer->state = NETXEN_BUFFER_BUSY;
1365 buffer->dma = pci_map_single(pdev, skb->data,
1367 PCI_DMA_FROMDEVICE);
1369 /* make a rcv descriptor */
1370 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1371 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1372 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1373 DPRINTK(INFO, "done writing descripter\n");
1375 get_next_index(producer, rcv_desc->max_rx_desc_count);
1376 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1377 buffer = &rcv_desc->rx_buf_arr[index];
1380 /* if we did allocate buffers, then write the count to Phantom */
1382 rcv_desc->begin_alloc = index;
1383 rcv_desc->rcv_pending += count;
1384 rcv_desc->producer = producer;
1386 writel((producer - 1) &
1387 (rcv_desc->max_rx_desc_count - 1),
1388 NETXEN_CRB_NORMALIZE(adapter,
1391 rcv_desc_crb[ringid].
1392 crb_rcv_producer_offset));
1397 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1399 memset(&adapter->stats, 0, sizeof(adapter->stats));