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 ((unsigned long ) 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
56 netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
57 unsigned long off, int *data)
59 void __iomem *addr = pci_base_offset(adapter, off);
63 static void crb_addr_transform_setup(void)
65 crb_addr_transform(XDMA);
66 crb_addr_transform(TIMR);
67 crb_addr_transform(SRE);
68 crb_addr_transform(SQN3);
69 crb_addr_transform(SQN2);
70 crb_addr_transform(SQN1);
71 crb_addr_transform(SQN0);
72 crb_addr_transform(SQS3);
73 crb_addr_transform(SQS2);
74 crb_addr_transform(SQS1);
75 crb_addr_transform(SQS0);
76 crb_addr_transform(RPMX7);
77 crb_addr_transform(RPMX6);
78 crb_addr_transform(RPMX5);
79 crb_addr_transform(RPMX4);
80 crb_addr_transform(RPMX3);
81 crb_addr_transform(RPMX2);
82 crb_addr_transform(RPMX1);
83 crb_addr_transform(RPMX0);
84 crb_addr_transform(ROMUSB);
85 crb_addr_transform(SN);
86 crb_addr_transform(QMN);
87 crb_addr_transform(QMS);
88 crb_addr_transform(PGNI);
89 crb_addr_transform(PGND);
90 crb_addr_transform(PGN3);
91 crb_addr_transform(PGN2);
92 crb_addr_transform(PGN1);
93 crb_addr_transform(PGN0);
94 crb_addr_transform(PGSI);
95 crb_addr_transform(PGSD);
96 crb_addr_transform(PGS3);
97 crb_addr_transform(PGS2);
98 crb_addr_transform(PGS1);
99 crb_addr_transform(PGS0);
100 crb_addr_transform(PS);
101 crb_addr_transform(PH);
102 crb_addr_transform(NIU);
103 crb_addr_transform(I2Q);
104 crb_addr_transform(EG);
105 crb_addr_transform(MN);
106 crb_addr_transform(MS);
107 crb_addr_transform(CAS2);
108 crb_addr_transform(CAS1);
109 crb_addr_transform(CAS0);
110 crb_addr_transform(CAM);
111 crb_addr_transform(C2C1);
112 crb_addr_transform(C2C0);
113 crb_addr_transform(SMB);
116 int netxen_init_firmware(struct netxen_adapter *adapter)
118 u32 state = 0, loops = 0, err = 0;
121 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
123 if (state == PHAN_INITIALIZE_ACK)
126 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
129 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
134 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
140 writel(MPORT_SINGLE_FUNCTION_MODE,
141 NETXEN_CRB_NORMALIZE(adapter, CRB_MPORT_MODE));
142 writel(PHAN_INITIALIZE_ACK,
143 NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
148 #define NETXEN_ADDR_LIMIT 0xffffffffULL
150 void *netxen_alloc(struct pci_dev *pdev, size_t sz, dma_addr_t * ptr,
151 struct pci_dev **used_dev)
155 addr = pci_alloc_consistent(pdev, sz, ptr);
156 if ((unsigned long long)(*ptr) < NETXEN_ADDR_LIMIT) {
160 pci_free_consistent(pdev, sz, addr, *ptr);
161 addr = pci_alloc_consistent(NULL, sz, ptr);
166 void netxen_initialize_adapter_sw(struct netxen_adapter *adapter)
171 struct netxen_rcv_desc_ctx *rcv_desc;
173 DPRINTK(INFO, "initializing some queues: %p\n", adapter);
174 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
175 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
176 struct netxen_rx_buffer *rx_buf;
177 rcv_desc = &adapter->recv_ctx[ctxid].rcv_desc[ring];
178 rcv_desc->rcv_free = rcv_desc->max_rx_desc_count;
179 rcv_desc->begin_alloc = 0;
180 rx_buf = rcv_desc->rx_buf_arr;
181 num_rx_bufs = rcv_desc->max_rx_desc_count;
183 * Now go through all of them, set reference handles
184 * and put them in the queues.
186 for (i = 0; i < num_rx_bufs; i++) {
187 rx_buf->ref_handle = i;
188 rx_buf->state = NETXEN_BUFFER_FREE;
189 DPRINTK(INFO, "Rx buf:ctx%d i(%d) rx_buf:"
190 "%p\n", ctxid, i, rx_buf);
197 void netxen_initialize_adapter_hw(struct netxen_adapter *adapter)
200 struct netxen_board_info *board_info = &(adapter->ahw.boardcfg);
202 if (netxen_nic_get_board_info(adapter) != 0)
203 printk("%s: Error getting board config info.\n",
204 netxen_nic_driver_name);
205 get_brd_port_by_type(board_info->board_type, &ports);
207 printk(KERN_ERR "%s: Unknown board type\n",
208 netxen_nic_driver_name);
209 adapter->ahw.max_ports = ports;
212 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
214 switch (adapter->ahw.board_type) {
216 adapter->enable_phy_interrupts =
217 netxen_niu_gbe_enable_phy_interrupts;
218 adapter->disable_phy_interrupts =
219 netxen_niu_gbe_disable_phy_interrupts;
220 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
221 adapter->macaddr_set = netxen_niu_macaddr_set;
222 adapter->set_mtu = netxen_nic_set_mtu_gb;
223 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
224 adapter->unset_promisc = netxen_niu_set_promiscuous_mode;
225 adapter->phy_read = netxen_niu_gbe_phy_read;
226 adapter->phy_write = netxen_niu_gbe_phy_write;
227 adapter->init_port = netxen_niu_gbe_init_port;
228 adapter->init_niu = netxen_nic_init_niu_gb;
229 adapter->stop_port = netxen_niu_disable_gbe_port;
232 case NETXEN_NIC_XGBE:
233 adapter->enable_phy_interrupts =
234 netxen_niu_xgbe_enable_phy_interrupts;
235 adapter->disable_phy_interrupts =
236 netxen_niu_xgbe_disable_phy_interrupts;
237 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
238 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
239 adapter->set_mtu = netxen_nic_set_mtu_xgb;
240 adapter->init_port = netxen_niu_xg_init_port;
241 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
242 adapter->unset_promisc = netxen_niu_xg_set_promiscuous_mode;
243 adapter->stop_port = netxen_niu_disable_xg_port;
252 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
253 * address to external PCI CRB address.
255 unsigned long netxen_decode_crb_addr(unsigned long addr)
258 unsigned long base_addr, offset, pci_base;
260 crb_addr_transform_setup();
262 pci_base = NETXEN_ADDR_ERROR;
263 base_addr = addr & 0xfff00000;
264 offset = addr & 0x000fffff;
266 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
267 if (crb_addr_xform[i] == base_addr) {
272 if (pci_base == NETXEN_ADDR_ERROR)
275 return (pci_base + offset);
278 static long rom_max_timeout = 10000;
279 static long rom_lock_timeout = 1000000;
280 static long rom_write_timeout = 700;
282 static inline int rom_lock(struct netxen_adapter *adapter)
289 /* acquire semaphore2 from PCI HW block */
290 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
294 if (timeout >= rom_lock_timeout)
304 for (iter = 0; iter < 20; iter++)
305 cpu_relax(); /*This a nop instr on i386 */
308 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
312 int netxen_wait_rom_done(struct netxen_adapter *adapter)
318 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
321 if (timeout >= rom_max_timeout) {
322 printk("Timeout reached waiting for rom done");
329 static inline int netxen_rom_wren(struct netxen_adapter *adapter)
331 /* Set write enable latch in ROM status register */
332 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
333 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
335 if (netxen_wait_rom_done(adapter)) {
341 static inline unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
344 unsigned int data = 0xdeaddead;
345 data = netxen_nic_reg_read(adapter, addr);
349 static inline int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
351 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
353 if (netxen_wait_rom_done(adapter)) {
356 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
359 static inline void netxen_rom_unlock(struct netxen_adapter *adapter)
363 /* release semaphore2 */
364 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
368 int netxen_rom_wip_poll(struct netxen_adapter *adapter)
373 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
375 val = netxen_do_rom_rdsr(adapter);
378 if (timeout > rom_max_timeout) {
385 static inline int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
388 if (netxen_rom_wren(adapter)) {
391 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
392 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
393 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
394 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
396 if (netxen_wait_rom_done(adapter)) {
397 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
401 return netxen_rom_wip_poll(adapter);
405 do_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
407 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
408 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
409 udelay(70); /* prevent bursting on CRB */
410 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
411 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
412 if (netxen_wait_rom_done(adapter)) {
413 printk("Error waiting for rom done\n");
416 /* reset abyte_cnt and dummy_byte_cnt */
417 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
418 udelay(70); /* prevent bursting on CRB */
419 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
421 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
426 do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
427 u8 *bytes, size_t size)
432 for (addridx = addr; addridx < (addr + size); addridx += 4) {
433 ret = do_rom_fast_read(adapter, addridx, (int *)bytes);
443 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
444 u8 *bytes, size_t size)
448 ret = rom_lock(adapter);
452 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
454 netxen_rom_unlock(adapter);
458 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
462 if (rom_lock(adapter) != 0)
465 ret = do_rom_fast_read(adapter, addr, valp);
466 netxen_rom_unlock(adapter);
470 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
474 if (rom_lock(adapter) != 0) {
477 ret = do_rom_fast_write(adapter, addr, data);
478 netxen_rom_unlock(adapter);
482 static inline int do_rom_fast_write_words(struct netxen_adapter *adapter,
483 int addr, u8 *bytes, size_t size)
488 while (addridx < (addr + size)) {
489 int last_attempt = 0;
495 ret = do_rom_fast_write(adapter, addridx, data);
502 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 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 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(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, CRBINIT_START,
596 buffer, FLASH_SECTOR_SIZE);
597 if (ret != FLASH_SUCCESS)
600 ret = netxen_rom_fast_write_words(adapter, FIXED_START,
601 buffer, 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 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 void check_erased_flash(struct netxen_adapter *adapter, int addr)
645 int count = 0, erased_errors = 0;
648 range = (addr == USER_START) ? FIXED_START : addr + FLASH_SECTOR_SIZE;
650 for (i = addr; i < range; i += 4) {
651 netxen_rom_fast_read(adapter, i, &val);
652 if (val != 0xffffffff)
658 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
659 "for sector address: %x\n", erased_errors, count, addr);
662 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
665 if (rom_lock(adapter) != 0) {
668 ret = netxen_do_rom_se(adapter, addr);
669 netxen_rom_unlock(adapter);
671 check_erased_flash(adapter, addr);
677 netxen_flash_erase_sections(struct netxen_adapter *adapter, int start, int end)
679 int ret = FLASH_SUCCESS;
682 for (i = start; i < end; i++) {
683 ret = netxen_rom_se(adapter, i * FLASH_SECTOR_SIZE);
686 ret = netxen_rom_wip_poll(adapter);
695 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
697 int ret = FLASH_SUCCESS;
700 start = SECONDARY_START / FLASH_SECTOR_SIZE;
701 end = USER_START / FLASH_SECTOR_SIZE;
702 ret = netxen_flash_erase_sections(adapter, start, end);
708 netxen_flash_erase_primary(struct netxen_adapter *adapter)
710 int ret = FLASH_SUCCESS;
713 start = PRIMARY_START / FLASH_SECTOR_SIZE;
714 end = SECONDARY_START / FLASH_SECTOR_SIZE;
715 ret = netxen_flash_erase_sections(adapter, start, end);
720 void netxen_halt_pegs(struct netxen_adapter *adapter)
722 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
723 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
724 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
725 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
728 int netxen_flash_unlock(struct netxen_adapter *adapter)
732 ret = netxen_rom_wrsr(adapter, 0);
736 ret = netxen_rom_wren(adapter);
743 #define NETXEN_BOARDTYPE 0x4008
744 #define NETXEN_BOARDNUM 0x400c
745 #define NETXEN_CHIPNUM 0x4010
746 #define NETXEN_ROMBUS_RESET 0xFFFFFFFF
747 #define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
748 #define NETXEN_ROM_FOUND_INIT 0x400
750 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
752 int addr, val, status;
755 struct crb_addr_pair *buf;
759 status = netxen_nic_get_board_info(adapter);
761 printk("%s: netxen_pinit_from_rom: Error getting board info\n",
762 netxen_nic_driver_name);
764 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
765 NETXEN_ROMBUS_RESET);
769 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
770 printk("P2 ROM board type: 0x%08x\n", val);
772 printk("Could not read board type\n");
773 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
774 printk("P2 ROM board num: 0x%08x\n", val);
776 printk("Could not read board number\n");
777 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
778 printk("P2 ROM chip num: 0x%08x\n", val);
780 printk("Could not read chip number\n");
783 if (netxen_rom_fast_read(adapter, 0, &n) == 0
784 && (n & NETXEN_ROM_FIRST_BARRIER)) {
785 n &= ~NETXEN_ROM_ROUNDUP;
786 if (n < NETXEN_ROM_FOUND_INIT) {
788 printk("%s: %d CRB init values found"
789 " in ROM.\n", netxen_nic_driver_name, n);
791 printk("%s:n=0x%x Error! NetXen card flash not"
792 " initialized.\n", __FUNCTION__, n);
795 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
797 printk("%s: netxen_pinit_from_rom: Unable to calloc "
798 "memory.\n", netxen_nic_driver_name);
801 for (i = 0; i < n; i++) {
802 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
803 || netxen_rom_fast_read(adapter, 8 * i + 8,
811 printk("%s: PCI: 0x%08x == 0x%08x\n",
812 netxen_nic_driver_name, (unsigned int)
813 netxen_decode_crb_addr((unsigned long)
816 for (i = 0; i < n; i++) {
818 off = netxen_decode_crb_addr((unsigned long)buf[i].addr);
819 if (off == NETXEN_ADDR_ERROR) {
820 printk(KERN_ERR"CRB init value out of range %lx\n",
824 off += NETXEN_PCI_CRBSPACE;
825 /* skipping cold reboot MAGIC */
826 if (off == NETXEN_CAM_RAM(0x1fc))
829 /* After writing this register, HW needs time for CRB */
830 /* to quiet down (else crb_window returns 0xffffffff) */
831 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
833 /* hold xdma in reset also */
834 buf[i].data = NETXEN_NIC_XDMA_RESET;
837 if (ADDR_IN_WINDOW1(off)) {
839 NETXEN_CRB_NORMALIZE(adapter, off));
841 netxen_nic_pci_change_crbwindow(adapter, 0);
843 pci_base_offset(adapter, off));
845 netxen_nic_pci_change_crbwindow(adapter, 1);
847 if (init_delay == 1) {
855 /* disable_peg_cache_all */
857 /* unreset_net_cache */
858 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
860 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
862 /* p2dn replyCount */
863 netxen_crb_writelit_adapter(adapter,
864 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
865 /* disable_peg_cache 0 */
866 netxen_crb_writelit_adapter(adapter,
867 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
868 /* disable_peg_cache 1 */
869 netxen_crb_writelit_adapter(adapter,
870 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
875 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
877 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
880 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
882 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
885 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
887 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
890 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
892 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
898 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
904 adapter->dummy_dma.addr =
905 pci_alloc_consistent(adapter->ahw.pdev,
906 NETXEN_HOST_DUMMY_DMA_SIZE,
907 &adapter->dummy_dma.phys_addr);
908 if (adapter->dummy_dma.addr == NULL) {
909 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
914 addr = (uint64_t) adapter->dummy_dma.phys_addr;
915 hi = (addr >> 32) & 0xffffffff;
916 lo = addr & 0xffffffff;
918 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
919 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
924 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
926 if (adapter->dummy_dma.addr) {
927 pci_free_consistent(adapter->ahw.pdev,
928 NETXEN_HOST_DUMMY_DMA_SIZE,
929 adapter->dummy_dma.addr,
930 adapter->dummy_dma.phys_addr);
931 adapter->dummy_dma.addr = NULL;
935 void netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
941 val = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
942 while (val != PHAN_INITIALIZE_COMPLETE && loops < 200000) {
946 readl(NETXEN_CRB_NORMALIZE
947 (adapter, CRB_CMDPEG_STATE));
950 if (val != PHAN_INITIALIZE_COMPLETE)
951 printk("WARNING: Initial boot wait loop failed...\n");
955 int netxen_nic_rx_has_work(struct netxen_adapter *adapter)
959 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
960 struct netxen_recv_context *recv_ctx =
961 &(adapter->recv_ctx[ctx]);
963 struct status_desc *desc_head;
964 struct status_desc *desc;
966 consumer = recv_ctx->status_rx_consumer;
967 desc_head = recv_ctx->rcv_status_desc_head;
968 desc = &desc_head[consumer];
970 if (netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)
977 static inline int netxen_nic_check_temp(struct netxen_adapter *adapter)
980 struct netxen_port *port;
981 struct net_device *netdev;
982 uint32_t temp, temp_state, temp_val;
985 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
987 temp_state = nx_get_temp_state(temp);
988 temp_val = nx_get_temp_val(temp);
990 if (temp_state == NX_TEMP_PANIC) {
992 "%s: Device temperature %d degrees C exceeds"
993 " maximum allowed. Hardware has been shut down.\n",
994 netxen_nic_driver_name, temp_val);
995 for (port_num = 0; port_num < adapter->ahw.max_ports;
997 port = adapter->port[port_num];
998 netdev = port->netdev;
1000 netif_carrier_off(netdev);
1001 netif_stop_queue(netdev);
1004 } else if (temp_state == NX_TEMP_WARN) {
1005 if (adapter->temp == NX_TEMP_NORMAL) {
1007 "%s: Device temperature %d degrees C "
1008 "exceeds operating range."
1009 " Immediate action needed.\n",
1010 netxen_nic_driver_name, temp_val);
1013 if (adapter->temp == NX_TEMP_WARN) {
1015 "%s: Device temperature is now %d degrees C"
1016 " in normal range.\n", netxen_nic_driver_name,
1020 adapter->temp = temp_state;
1024 void netxen_watchdog_task(struct work_struct *work)
1027 struct netxen_port *port;
1028 struct net_device *netdev;
1029 struct netxen_adapter *adapter =
1030 container_of(work, struct netxen_adapter, watchdog_task);
1032 if (netxen_nic_check_temp(adapter))
1035 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
1036 port = adapter->port[port_num];
1037 netdev = port->netdev;
1039 if ((netif_running(netdev)) && !netif_carrier_ok(netdev)) {
1040 printk(KERN_INFO "%s port %d, %s carrier is now ok\n",
1041 netxen_nic_driver_name, port_num, netdev->name);
1042 netif_carrier_on(netdev);
1045 if (netif_queue_stopped(netdev))
1046 netif_wake_queue(netdev);
1049 if (adapter->handle_phy_intr)
1050 adapter->handle_phy_intr(adapter);
1051 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1055 * netxen_process_rcv() send the received packet to the protocol stack.
1056 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1057 * invoke the routine to send more rx buffers to the Phantom...
1060 netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1061 struct status_desc *desc)
1063 struct netxen_port *port = adapter->port[netxen_get_sts_port(desc)];
1064 struct pci_dev *pdev = port->pdev;
1065 struct net_device *netdev = port->netdev;
1066 int index = netxen_get_sts_refhandle(desc);
1067 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1068 struct netxen_rx_buffer *buffer;
1069 struct sk_buff *skb;
1070 u32 length = netxen_get_sts_totallength(desc);
1072 struct netxen_rcv_desc_ctx *rcv_desc;
1075 desc_ctx = netxen_get_sts_type(desc);
1076 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1077 printk("%s: %s Bad Rcv descriptor ring\n",
1078 netxen_nic_driver_name, netdev->name);
1082 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
1083 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1084 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1085 index, rcv_desc->max_rx_desc_count);
1088 buffer = &rcv_desc->rx_buf_arr[index];
1089 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1090 buffer->lro_current_frags++;
1091 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1092 buffer->lro_expected_frags =
1093 netxen_get_sts_desc_lro_cnt(desc);
1094 buffer->lro_length = length;
1096 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1097 if (buffer->lro_expected_frags != 0) {
1098 printk("LRO: (refhandle:%x) recv frag."
1099 "wait for last. flags: %x expected:%d"
1101 netxen_get_sts_desc_lro_last_frag(desc),
1102 buffer->lro_expected_frags,
1103 buffer->lro_current_frags);
1109 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1110 PCI_DMA_FROMDEVICE);
1112 skb = (struct sk_buff *)buffer->skb;
1114 if (likely(netxen_get_sts_status(desc) == STATUS_CKSUM_OK)) {
1115 port->stats.csummed++;
1116 skb->ip_summed = CHECKSUM_UNNECESSARY;
1119 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1120 /* True length was only available on the last pkt */
1121 skb_put(skb, buffer->lro_length);
1123 skb_put(skb, length);
1126 skb->protocol = eth_type_trans(skb, netdev);
1128 ret = netif_receive_skb(skb);
1131 * RH: Do we need these stats on a regular basis. Can we get it from
1135 case NET_RX_SUCCESS:
1136 port->stats.uphappy++;
1140 port->stats.uplcong++;
1144 port->stats.upmcong++;
1147 case NET_RX_CN_HIGH:
1148 port->stats.uphcong++;
1152 port->stats.updropped++;
1156 port->stats.updunno++;
1160 netdev->last_rx = jiffies;
1162 rcv_desc->rcv_free++;
1163 rcv_desc->rcv_pending--;
1166 * We just consumed one buffer so post a buffer.
1168 adapter->stats.post_called++;
1170 buffer->state = NETXEN_BUFFER_FREE;
1171 buffer->lro_current_frags = 0;
1172 buffer->lro_expected_frags = 0;
1174 port->stats.no_rcv++;
1175 port->stats.rxbytes += length;
1178 /* Process Receive status ring */
1179 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1181 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1182 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1183 struct status_desc *desc; /* used to read status desc here */
1184 u32 consumer = recv_ctx->status_rx_consumer;
1186 int count = 0, ring;
1188 DPRINTK(INFO, "procesing receive\n");
1190 * we assume in this case that there is only one port and that is
1191 * port #1...changes need to be done in firmware to indicate port
1192 * number as part of the descriptor. This way we will be able to get
1193 * the netdev which is associated with that device.
1195 while (count < max) {
1196 desc = &desc_head[consumer];
1197 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1198 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1199 netxen_get_sts_owner(desc));
1202 netxen_process_rcv(adapter, ctxid, desc);
1203 netxen_clear_sts_owner(desc);
1204 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1205 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1209 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
1210 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1214 /* update the consumer index in phantom */
1216 adapter->stats.process_rcv++;
1217 recv_ctx->status_rx_consumer = consumer;
1218 recv_ctx->status_rx_producer = producer;
1222 NETXEN_CRB_NORMALIZE(adapter,
1223 recv_crb_registers[ctxid].
1224 crb_rcv_status_consumer));
1230 /* Process Command status ring */
1231 int netxen_process_cmd_ring(unsigned long data)
1235 struct netxen_adapter *adapter = (struct netxen_adapter *)data;
1238 struct netxen_cmd_buffer *buffer;
1239 struct netxen_port *port; /* port #1 */
1240 struct netxen_port *nport;
1241 struct pci_dev *pdev;
1242 struct netxen_skb_frag *frag;
1244 struct sk_buff *skb = NULL;
1248 spin_lock(&adapter->tx_lock);
1249 last_consumer = adapter->last_cmd_consumer;
1250 DPRINTK(INFO, "procesing xmit complete\n");
1251 /* we assume in this case that there is only one port and that is
1252 * port #1...changes need to be done in firmware to indicate port
1253 * number as part of the descriptor. This way we will be able to get
1254 * the netdev which is associated with that device.
1257 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1258 if (last_consumer == consumer) { /* Ring is empty */
1259 DPRINTK(INFO, "last_consumer %d == consumer %d\n",
1260 last_consumer, consumer);
1261 spin_unlock(&adapter->tx_lock);
1265 adapter->proc_cmd_buf_counter++;
1266 adapter->stats.process_xmit++;
1268 * Not needed - does not seem to be used anywhere.
1269 * adapter->cmd_consumer = consumer;
1271 spin_unlock(&adapter->tx_lock);
1273 while ((last_consumer != consumer) && (count1 < MAX_STATUS_HANDLE)) {
1274 buffer = &adapter->cmd_buf_arr[last_consumer];
1275 port = adapter->port[buffer->port];
1277 frag = &buffer->frag_array[0];
1279 if (skb && (cmpxchg(&buffer->skb, skb, 0) == skb)) {
1280 pci_unmap_single(pdev, frag->dma, frag->length,
1282 for (i = 1; i < buffer->frag_count; i++) {
1283 DPRINTK(INFO, "getting fragment no %d\n", i);
1284 frag++; /* Get the next frag */
1285 pci_unmap_page(pdev, frag->dma, frag->length,
1289 port->stats.skbfreed++;
1290 dev_kfree_skb_any(skb);
1292 } else if (adapter->proc_cmd_buf_counter == 1) {
1293 port->stats.txnullskb++;
1295 if (unlikely(netif_queue_stopped(port->netdev)
1296 && netif_carrier_ok(port->netdev))
1297 && ((jiffies - port->netdev->trans_start) >
1298 port->netdev->watchdog_timeo)) {
1299 SCHEDULE_WORK(&port->tx_timeout_task);
1302 last_consumer = get_next_index(last_consumer,
1303 adapter->max_tx_desc_count);
1306 adapter->stats.noxmitdone += count1;
1309 spin_lock(&adapter->tx_lock);
1310 if ((--adapter->proc_cmd_buf_counter) == 0) {
1311 adapter->last_cmd_consumer = last_consumer;
1312 while ((adapter->last_cmd_consumer != consumer)
1313 && (count2 < MAX_STATUS_HANDLE)) {
1315 &adapter->cmd_buf_arr[adapter->last_cmd_consumer];
1320 adapter->last_cmd_consumer =
1321 get_next_index(adapter->last_cmd_consumer,
1322 adapter->max_tx_desc_count);
1325 if (count1 || count2) {
1326 for (p = 0; p < adapter->ahw.max_ports; p++) {
1327 nport = adapter->port[p];
1328 if (netif_queue_stopped(nport->netdev)
1329 && (nport->flags & NETXEN_NETDEV_STATUS)) {
1330 netif_wake_queue(nport->netdev);
1331 nport->flags &= ~NETXEN_NETDEV_STATUS;
1336 * If everything is freed up to consumer then check if the ring is full
1337 * If the ring is full then check if more needs to be freed and
1338 * schedule the call back again.
1340 * This happens when there are 2 CPUs. One could be freeing and the
1341 * other filling it. If the ring is full when we get out of here and
1342 * the card has already interrupted the host then the host can miss the
1345 * There is still a possible race condition and the host could miss an
1346 * interrupt. The card has to take care of this.
1348 if (adapter->last_cmd_consumer == consumer &&
1349 (((adapter->cmd_producer + 1) %
1350 adapter->max_tx_desc_count) == adapter->last_cmd_consumer)) {
1351 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1353 done = (adapter->last_cmd_consumer == consumer);
1355 spin_unlock(&adapter->tx_lock);
1356 DPRINTK(INFO, "last consumer is %d in %s\n", last_consumer,
1362 * netxen_post_rx_buffers puts buffer in the Phantom memory
1364 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1366 struct pci_dev *pdev = adapter->ahw.pdev;
1367 struct sk_buff *skb;
1368 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1369 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1371 struct rcv_desc *pdesc;
1372 struct netxen_rx_buffer *buffer;
1375 netxen_ctx_msg msg = 0;
1378 adapter->stats.post_called++;
1379 rcv_desc = &recv_ctx->rcv_desc[ringid];
1381 producer = rcv_desc->producer;
1382 index = rcv_desc->begin_alloc;
1383 buffer = &rcv_desc->rx_buf_arr[index];
1384 /* We can start writing rx descriptors into the phantom memory. */
1385 while (buffer->state == NETXEN_BUFFER_FREE) {
1386 skb = dev_alloc_skb(rcv_desc->skb_size);
1387 if (unlikely(!skb)) {
1390 * We need to schedule the posting of buffers to the pegs.
1392 rcv_desc->begin_alloc = index;
1393 DPRINTK(ERR, "netxen_post_rx_buffers: "
1394 " allocated only %d buffers\n", count);
1398 count++; /* now there should be no failure */
1399 pdesc = &rcv_desc->desc_head[producer];
1401 #if defined(XGB_DEBUG)
1402 *(unsigned long *)(skb->head) = 0xc0debabe;
1403 if (skb_is_nonlinear(skb)) {
1404 printk("Allocated SKB @%p is nonlinear\n");
1407 skb_reserve(skb, 2);
1408 /* This will be setup when we receive the
1409 * buffer after it has been filled FSL TBD TBD
1410 * skb->dev = netdev;
1412 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1413 PCI_DMA_FROMDEVICE);
1414 pdesc->addr_buffer = cpu_to_le64(dma);
1416 buffer->state = NETXEN_BUFFER_BUSY;
1418 /* make a rcv descriptor */
1419 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1420 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1421 DPRINTK(INFO, "done writing descripter\n");
1423 get_next_index(producer, rcv_desc->max_rx_desc_count);
1424 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1425 buffer = &rcv_desc->rx_buf_arr[index];
1427 /* if we did allocate buffers, then write the count to Phantom */
1429 rcv_desc->begin_alloc = index;
1430 rcv_desc->rcv_pending += count;
1431 adapter->stats.lastposted = count;
1432 adapter->stats.posted += count;
1433 rcv_desc->producer = producer;
1434 if (rcv_desc->rcv_free >= 32) {
1435 rcv_desc->rcv_free = 0;
1437 writel((producer - 1) &
1438 (rcv_desc->max_rx_desc_count - 1),
1439 NETXEN_CRB_NORMALIZE(adapter,
1440 recv_crb_registers[0].
1441 rcv_desc_crb[ringid].
1442 crb_rcv_producer_offset));
1444 * Write a doorbell msg to tell phanmon of change in
1445 * receive ring producer
1447 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1448 netxen_set_msg_privid(msg);
1449 netxen_set_msg_count(msg,
1452 max_rx_desc_count - 1)));
1453 netxen_set_msg_ctxid(msg, 0);
1454 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1456 DB_NORMALIZE(adapter,
1457 NETXEN_RCV_PRODUCER_OFFSET));
1462 void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter, uint32_t ctx,
1465 struct pci_dev *pdev = adapter->ahw.pdev;
1466 struct sk_buff *skb;
1467 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1468 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1470 struct rcv_desc *pdesc;
1471 struct netxen_rx_buffer *buffer;
1475 adapter->stats.post_called++;
1476 rcv_desc = &recv_ctx->rcv_desc[ringid];
1478 producer = rcv_desc->producer;
1479 index = rcv_desc->begin_alloc;
1480 buffer = &rcv_desc->rx_buf_arr[index];
1481 /* We can start writing rx descriptors into the phantom memory. */
1482 while (buffer->state == NETXEN_BUFFER_FREE) {
1483 skb = dev_alloc_skb(rcv_desc->skb_size);
1484 if (unlikely(!skb)) {
1486 * We need to schedule the posting of buffers to the pegs.
1488 rcv_desc->begin_alloc = index;
1489 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
1490 " allocated only %d buffers\n", count);
1493 count++; /* now there should be no failure */
1494 pdesc = &rcv_desc->desc_head[producer];
1495 skb_reserve(skb, 2);
1497 * This will be setup when we receive the
1498 * buffer after it has been filled
1499 * skb->dev = netdev;
1502 buffer->state = NETXEN_BUFFER_BUSY;
1503 buffer->dma = pci_map_single(pdev, skb->data,
1505 PCI_DMA_FROMDEVICE);
1507 /* make a rcv descriptor */
1508 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1509 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1510 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1511 DPRINTK(INFO, "done writing descripter\n");
1513 get_next_index(producer, rcv_desc->max_rx_desc_count);
1514 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1515 buffer = &rcv_desc->rx_buf_arr[index];
1518 /* if we did allocate buffers, then write the count to Phantom */
1520 rcv_desc->begin_alloc = index;
1521 rcv_desc->rcv_pending += count;
1522 adapter->stats.lastposted = count;
1523 adapter->stats.posted += count;
1524 rcv_desc->producer = producer;
1525 if (rcv_desc->rcv_free >= 32) {
1526 rcv_desc->rcv_free = 0;
1528 writel((producer - 1) &
1529 (rcv_desc->max_rx_desc_count - 1),
1530 NETXEN_CRB_NORMALIZE(adapter,
1531 recv_crb_registers[0].
1532 rcv_desc_crb[ringid].
1533 crb_rcv_producer_offset));
1539 int netxen_nic_tx_has_work(struct netxen_adapter *adapter)
1541 if (find_diff_among(adapter->last_cmd_consumer,
1542 adapter->cmd_producer,
1543 adapter->max_tx_desc_count) > 0)
1550 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1552 struct netxen_port *port;
1555 memset(&adapter->stats, 0, sizeof(adapter->stats));
1556 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
1557 port = adapter->port[port_num];
1558 memset(&port->stats, 0, sizeof(port->stats));