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
58 netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
59 unsigned long off, int *data)
61 void __iomem *addr = pci_base_offset(adapter, off);
65 static void crb_addr_transform_setup(void)
67 crb_addr_transform(XDMA);
68 crb_addr_transform(TIMR);
69 crb_addr_transform(SRE);
70 crb_addr_transform(SQN3);
71 crb_addr_transform(SQN2);
72 crb_addr_transform(SQN1);
73 crb_addr_transform(SQN0);
74 crb_addr_transform(SQS3);
75 crb_addr_transform(SQS2);
76 crb_addr_transform(SQS1);
77 crb_addr_transform(SQS0);
78 crb_addr_transform(RPMX7);
79 crb_addr_transform(RPMX6);
80 crb_addr_transform(RPMX5);
81 crb_addr_transform(RPMX4);
82 crb_addr_transform(RPMX3);
83 crb_addr_transform(RPMX2);
84 crb_addr_transform(RPMX1);
85 crb_addr_transform(RPMX0);
86 crb_addr_transform(ROMUSB);
87 crb_addr_transform(SN);
88 crb_addr_transform(QMN);
89 crb_addr_transform(QMS);
90 crb_addr_transform(PGNI);
91 crb_addr_transform(PGND);
92 crb_addr_transform(PGN3);
93 crb_addr_transform(PGN2);
94 crb_addr_transform(PGN1);
95 crb_addr_transform(PGN0);
96 crb_addr_transform(PGSI);
97 crb_addr_transform(PGSD);
98 crb_addr_transform(PGS3);
99 crb_addr_transform(PGS2);
100 crb_addr_transform(PGS1);
101 crb_addr_transform(PGS0);
102 crb_addr_transform(PS);
103 crb_addr_transform(PH);
104 crb_addr_transform(NIU);
105 crb_addr_transform(I2Q);
106 crb_addr_transform(EG);
107 crb_addr_transform(MN);
108 crb_addr_transform(MS);
109 crb_addr_transform(CAS2);
110 crb_addr_transform(CAS1);
111 crb_addr_transform(CAS0);
112 crb_addr_transform(CAM);
113 crb_addr_transform(C2C1);
114 crb_addr_transform(C2C0);
115 crb_addr_transform(SMB);
118 int netxen_init_firmware(struct netxen_adapter *adapter)
120 u32 state = 0, loops = 0, err = 0;
123 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
125 if (state == PHAN_INITIALIZE_ACK)
128 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
131 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
136 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
142 writel(MPORT_MULTI_FUNCTION_MODE,
143 NETXEN_CRB_NORMALIZE(adapter, CRB_MPORT_MODE));
144 writel(PHAN_INITIALIZE_ACK,
145 NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
150 #define NETXEN_ADDR_LIMIT 0xffffffffULL
152 void *netxen_alloc(struct pci_dev *pdev, size_t sz, dma_addr_t * ptr,
153 struct pci_dev **used_dev)
157 addr = pci_alloc_consistent(pdev, sz, ptr);
158 if ((unsigned long long)(*ptr) < NETXEN_ADDR_LIMIT) {
162 pci_free_consistent(pdev, sz, addr, *ptr);
163 addr = pci_alloc_consistent(NULL, sz, ptr);
168 void netxen_initialize_adapter_sw(struct netxen_adapter *adapter)
173 struct netxen_rcv_desc_ctx *rcv_desc;
175 DPRINTK(INFO, "initializing some queues: %p\n", adapter);
176 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
177 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
178 struct netxen_rx_buffer *rx_buf;
179 rcv_desc = &adapter->recv_ctx[ctxid].rcv_desc[ring];
180 rcv_desc->rcv_free = rcv_desc->max_rx_desc_count;
181 rcv_desc->begin_alloc = 0;
182 rx_buf = rcv_desc->rx_buf_arr;
183 num_rx_bufs = rcv_desc->max_rx_desc_count;
185 * Now go through all of them, set reference handles
186 * and put them in the queues.
188 for (i = 0; i < num_rx_bufs; i++) {
189 rx_buf->ref_handle = i;
190 rx_buf->state = NETXEN_BUFFER_FREE;
191 DPRINTK(INFO, "Rx buf:ctx%d i(%d) rx_buf:"
192 "%p\n", ctxid, i, rx_buf);
199 void netxen_initialize_adapter_hw(struct netxen_adapter *adapter)
202 struct netxen_board_info *board_info = &(adapter->ahw.boardcfg);
204 if (netxen_nic_get_board_info(adapter) != 0)
205 printk("%s: Error getting board config info.\n",
206 netxen_nic_driver_name);
207 get_brd_port_by_type(board_info->board_type, &ports);
209 printk(KERN_ERR "%s: Unknown board type\n",
210 netxen_nic_driver_name);
211 adapter->ahw.max_ports = ports;
214 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
216 switch (adapter->ahw.board_type) {
218 adapter->enable_phy_interrupts =
219 netxen_niu_gbe_enable_phy_interrupts;
220 adapter->disable_phy_interrupts =
221 netxen_niu_gbe_disable_phy_interrupts;
222 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
223 adapter->macaddr_set = netxen_niu_macaddr_set;
224 adapter->set_mtu = netxen_nic_set_mtu_gb;
225 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
226 adapter->unset_promisc = netxen_niu_set_promiscuous_mode;
227 adapter->phy_read = netxen_niu_gbe_phy_read;
228 adapter->phy_write = netxen_niu_gbe_phy_write;
229 adapter->init_niu = netxen_nic_init_niu_gb;
230 adapter->stop_port = netxen_niu_disable_gbe_port;
233 case NETXEN_NIC_XGBE:
234 adapter->enable_phy_interrupts =
235 netxen_niu_xgbe_enable_phy_interrupts;
236 adapter->disable_phy_interrupts =
237 netxen_niu_xgbe_disable_phy_interrupts;
238 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
239 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
240 adapter->set_mtu = netxen_nic_set_mtu_xgb;
241 adapter->init_port = netxen_niu_xg_init_port;
242 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
243 adapter->unset_promisc = netxen_niu_xg_set_promiscuous_mode;
244 adapter->stop_port = netxen_niu_disable_xg_port;
253 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
254 * address to external PCI CRB address.
256 u32 netxen_decode_crb_addr(u32 addr)
259 u32 base_addr, offset, pci_base;
261 crb_addr_transform_setup();
263 pci_base = NETXEN_ADDR_ERROR;
264 base_addr = addr & 0xfff00000;
265 offset = addr & 0x000fffff;
267 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
268 if (crb_addr_xform[i] == base_addr) {
273 if (pci_base == NETXEN_ADDR_ERROR)
276 return (pci_base + offset);
279 static long rom_max_timeout = 100;
280 static long rom_lock_timeout = 10000;
281 static long rom_write_timeout = 700;
283 static inline int rom_lock(struct netxen_adapter *adapter)
290 /* acquire semaphore2 from PCI HW block */
291 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
295 if (timeout >= rom_lock_timeout)
305 for (iter = 0; iter < 20; iter++)
306 cpu_relax(); /*This a nop instr on i386 */
309 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
313 int netxen_wait_rom_done(struct netxen_adapter *adapter)
319 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
322 if (timeout >= rom_max_timeout) {
323 printk("Timeout reached waiting for rom done");
330 static inline int netxen_rom_wren(struct netxen_adapter *adapter)
332 /* Set write enable latch in ROM status register */
333 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
334 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
336 if (netxen_wait_rom_done(adapter)) {
342 static inline unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
345 unsigned int data = 0xdeaddead;
346 data = netxen_nic_reg_read(adapter, addr);
350 static inline int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
352 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
354 if (netxen_wait_rom_done(adapter)) {
357 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
360 static inline void netxen_rom_unlock(struct netxen_adapter *adapter)
364 /* release semaphore2 */
365 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
369 int netxen_rom_wip_poll(struct netxen_adapter *adapter)
374 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
376 val = netxen_do_rom_rdsr(adapter);
379 if (timeout > rom_max_timeout) {
386 static inline int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
389 if (netxen_rom_wren(adapter)) {
392 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
393 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
394 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
395 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
397 if (netxen_wait_rom_done(adapter)) {
398 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
402 return netxen_rom_wip_poll(adapter);
406 do_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
408 if (jiffies > (last_schedule_time + (8 * HZ))) {
409 last_schedule_time = jiffies;
413 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
414 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
415 udelay(100); /* prevent bursting on CRB */
416 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
417 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
418 if (netxen_wait_rom_done(adapter)) {
419 printk("Error waiting for rom done\n");
422 /* reset abyte_cnt and dummy_byte_cnt */
423 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
424 udelay(100); /* prevent bursting on CRB */
425 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
427 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
432 do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
433 u8 *bytes, size_t size)
438 for (addridx = addr; addridx < (addr + size); addridx += 4) {
439 ret = do_rom_fast_read(adapter, addridx, (int *)bytes);
442 *(int *)bytes = cpu_to_le32(*(int *)bytes);
450 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
451 u8 *bytes, size_t size)
455 ret = rom_lock(adapter);
459 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
461 netxen_rom_unlock(adapter);
465 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
469 if (rom_lock(adapter) != 0)
472 ret = do_rom_fast_read(adapter, addr, valp);
473 netxen_rom_unlock(adapter);
477 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
481 if (rom_lock(adapter) != 0) {
484 ret = do_rom_fast_write(adapter, addr, data);
485 netxen_rom_unlock(adapter);
489 static inline int do_rom_fast_write_words(struct netxen_adapter *adapter,
490 int addr, u8 *bytes, size_t size)
495 while (addridx < (addr + size)) {
496 int last_attempt = 0;
500 data = le32_to_cpu((*(u32*)bytes));
501 ret = do_rom_fast_write(adapter, addridx, data);
508 ret = do_rom_fast_read(adapter, addridx, &data1);
515 if (timeout++ >= rom_write_timeout) {
516 if (last_attempt++ < 4) {
517 ret = do_rom_fast_write(adapter,
523 printk(KERN_INFO "Data write did not "
524 "succeed at address 0x%x\n", addridx);
537 int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
538 u8 *bytes, size_t size)
542 ret = rom_lock(adapter);
546 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
547 netxen_rom_unlock(adapter);
552 int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
556 ret = netxen_rom_wren(adapter);
560 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
561 netxen_crb_writelit_adapter(adapter,
562 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
564 ret = netxen_wait_rom_done(adapter);
568 return netxen_rom_wip_poll(adapter);
571 int netxen_rom_rdsr(struct netxen_adapter *adapter)
575 ret = rom_lock(adapter);
579 ret = netxen_do_rom_rdsr(adapter);
580 netxen_rom_unlock(adapter);
584 int netxen_backup_crbinit(struct netxen_adapter *adapter)
586 int ret = FLASH_SUCCESS;
588 char *buffer = kmalloc(FLASH_SECTOR_SIZE, GFP_KERNEL);
592 /* unlock sector 63 */
593 val = netxen_rom_rdsr(adapter);
595 ret = netxen_rom_wrsr(adapter, val);
596 if (ret != FLASH_SUCCESS)
599 ret = netxen_rom_wip_poll(adapter);
600 if (ret != FLASH_SUCCESS)
603 /* copy sector 0 to sector 63 */
604 ret = netxen_rom_fast_read_words(adapter, CRBINIT_START,
605 buffer, FLASH_SECTOR_SIZE);
606 if (ret != FLASH_SUCCESS)
609 ret = netxen_rom_fast_write_words(adapter, FIXED_START,
610 buffer, FLASH_SECTOR_SIZE);
611 if (ret != FLASH_SUCCESS)
615 val = netxen_rom_rdsr(adapter);
619 if (netxen_rom_wrsr(adapter, val) == 0) {
620 ret = netxen_rom_wip_poll(adapter);
621 if (ret != FLASH_SUCCESS)
625 ret = netxen_rom_wip_poll(adapter);
626 if (ret != FLASH_SUCCESS)
636 int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
638 netxen_rom_wren(adapter);
639 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
640 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
641 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
643 if (netxen_wait_rom_done(adapter)) {
644 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
647 return netxen_rom_wip_poll(adapter);
650 void check_erased_flash(struct netxen_adapter *adapter, int addr)
654 int count = 0, erased_errors = 0;
657 range = (addr == USER_START) ? FIXED_START : addr + FLASH_SECTOR_SIZE;
659 for (i = addr; i < range; i += 4) {
660 netxen_rom_fast_read(adapter, i, &val);
661 if (val != 0xffffffff)
667 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
668 "for sector address: %x\n", erased_errors, count, addr);
671 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
674 if (rom_lock(adapter) != 0) {
677 ret = netxen_do_rom_se(adapter, addr);
678 netxen_rom_unlock(adapter);
680 check_erased_flash(adapter, addr);
686 netxen_flash_erase_sections(struct netxen_adapter *adapter, int start, int end)
688 int ret = FLASH_SUCCESS;
691 for (i = start; i < end; i++) {
692 ret = netxen_rom_se(adapter, i * FLASH_SECTOR_SIZE);
695 ret = netxen_rom_wip_poll(adapter);
704 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
706 int ret = FLASH_SUCCESS;
709 start = SECONDARY_START / FLASH_SECTOR_SIZE;
710 end = USER_START / FLASH_SECTOR_SIZE;
711 ret = netxen_flash_erase_sections(adapter, start, end);
717 netxen_flash_erase_primary(struct netxen_adapter *adapter)
719 int ret = FLASH_SUCCESS;
722 start = PRIMARY_START / FLASH_SECTOR_SIZE;
723 end = SECONDARY_START / FLASH_SECTOR_SIZE;
724 ret = netxen_flash_erase_sections(adapter, start, end);
729 void netxen_halt_pegs(struct netxen_adapter *adapter)
731 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
732 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
733 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
734 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
737 int netxen_flash_unlock(struct netxen_adapter *adapter)
741 ret = netxen_rom_wrsr(adapter, 0);
745 ret = netxen_rom_wren(adapter);
752 #define NETXEN_BOARDTYPE 0x4008
753 #define NETXEN_BOARDNUM 0x400c
754 #define NETXEN_CHIPNUM 0x4010
755 #define NETXEN_ROMBUS_RESET 0xFFFFFFFF
756 #define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
757 #define NETXEN_ROM_FOUND_INIT 0x400
759 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
761 int addr, val, status;
764 struct crb_addr_pair *buf;
768 status = netxen_nic_get_board_info(adapter);
770 printk("%s: netxen_pinit_from_rom: Error getting board info\n",
771 netxen_nic_driver_name);
773 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
774 NETXEN_ROMBUS_RESET);
778 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
779 printk("P2 ROM board type: 0x%08x\n", val);
781 printk("Could not read board type\n");
782 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
783 printk("P2 ROM board num: 0x%08x\n", val);
785 printk("Could not read board number\n");
786 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
787 printk("P2 ROM chip num: 0x%08x\n", val);
789 printk("Could not read chip number\n");
792 if (netxen_rom_fast_read(adapter, 0, &n) == 0
793 && (n & NETXEN_ROM_FIRST_BARRIER)) {
794 n &= ~NETXEN_ROM_ROUNDUP;
795 if (n < NETXEN_ROM_FOUND_INIT) {
797 printk("%s: %d CRB init values found"
798 " in ROM.\n", netxen_nic_driver_name, n);
800 printk("%s:n=0x%x Error! NetXen card flash not"
801 " initialized.\n", __FUNCTION__, n);
804 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
806 printk("%s: netxen_pinit_from_rom: Unable to calloc "
807 "memory.\n", netxen_nic_driver_name);
810 for (i = 0; i < n; i++) {
811 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
812 || netxen_rom_fast_read(adapter, 8 * i + 8,
820 printk("%s: PCI: 0x%08x == 0x%08x\n",
821 netxen_nic_driver_name, (unsigned int)
822 netxen_decode_crb_addr(addr), val);
824 for (i = 0; i < n; i++) {
826 off = netxen_decode_crb_addr(buf[i].addr);
827 if (off == NETXEN_ADDR_ERROR) {
828 printk(KERN_ERR"CRB init value out of range %x\n",
832 off += NETXEN_PCI_CRBSPACE;
833 /* skipping cold reboot MAGIC */
834 if (off == NETXEN_CAM_RAM(0x1fc))
837 /* After writing this register, HW needs time for CRB */
838 /* to quiet down (else crb_window returns 0xffffffff) */
839 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
841 /* hold xdma in reset also */
842 buf[i].data = NETXEN_NIC_XDMA_RESET;
845 if (ADDR_IN_WINDOW1(off)) {
847 NETXEN_CRB_NORMALIZE(adapter, off));
849 netxen_nic_pci_change_crbwindow(adapter, 0);
851 pci_base_offset(adapter, off));
853 netxen_nic_pci_change_crbwindow(adapter, 1);
855 if (init_delay == 1) {
863 /* disable_peg_cache_all */
865 /* unreset_net_cache */
866 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
868 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
870 /* p2dn replyCount */
871 netxen_crb_writelit_adapter(adapter,
872 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
873 /* disable_peg_cache 0 */
874 netxen_crb_writelit_adapter(adapter,
875 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
876 /* disable_peg_cache 1 */
877 netxen_crb_writelit_adapter(adapter,
878 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
883 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
885 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
888 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
890 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
893 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
895 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
898 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
900 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
906 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
912 adapter->dummy_dma.addr =
913 pci_alloc_consistent(adapter->ahw.pdev,
914 NETXEN_HOST_DUMMY_DMA_SIZE,
915 &adapter->dummy_dma.phys_addr);
916 if (adapter->dummy_dma.addr == NULL) {
917 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
922 addr = (uint64_t) adapter->dummy_dma.phys_addr;
923 hi = (addr >> 32) & 0xffffffff;
924 lo = addr & 0xffffffff;
926 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
927 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
932 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
934 if (adapter->dummy_dma.addr) {
935 writel(0, NETXEN_CRB_NORMALIZE(adapter,
936 CRB_HOST_DUMMY_BUF_ADDR_HI));
937 writel(0, NETXEN_CRB_NORMALIZE(adapter,
938 CRB_HOST_DUMMY_BUF_ADDR_LO));
939 pci_free_consistent(adapter->ahw.pdev,
940 NETXEN_HOST_DUMMY_DMA_SIZE,
941 adapter->dummy_dma.addr,
942 adapter->dummy_dma.phys_addr);
943 adapter->dummy_dma.addr = NULL;
947 void netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
953 val = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
954 while (val != PHAN_INITIALIZE_COMPLETE &&
955 val != PHAN_INITIALIZE_ACK && loops < 200000) {
959 readl(NETXEN_CRB_NORMALIZE
960 (adapter, CRB_CMDPEG_STATE));
963 if (val != PHAN_INITIALIZE_COMPLETE)
964 printk("WARNING: Initial boot wait loop failed...\n");
968 int netxen_nic_rx_has_work(struct netxen_adapter *adapter)
972 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
973 struct netxen_recv_context *recv_ctx =
974 &(adapter->recv_ctx[ctx]);
976 struct status_desc *desc_head;
977 struct status_desc *desc;
979 consumer = recv_ctx->status_rx_consumer;
980 desc_head = recv_ctx->rcv_status_desc_head;
981 desc = &desc_head[consumer];
983 if (netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)
990 static inline int netxen_nic_check_temp(struct netxen_adapter *adapter)
992 struct net_device *netdev = adapter->netdev;
993 uint32_t temp, temp_state, temp_val;
996 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
998 temp_state = nx_get_temp_state(temp);
999 temp_val = nx_get_temp_val(temp);
1001 if (temp_state == NX_TEMP_PANIC) {
1003 "%s: Device temperature %d degrees C exceeds"
1004 " maximum allowed. Hardware has been shut down.\n",
1005 netxen_nic_driver_name, temp_val);
1007 netif_carrier_off(netdev);
1008 netif_stop_queue(netdev);
1010 } else if (temp_state == NX_TEMP_WARN) {
1011 if (adapter->temp == NX_TEMP_NORMAL) {
1013 "%s: Device temperature %d degrees C "
1014 "exceeds operating range."
1015 " Immediate action needed.\n",
1016 netxen_nic_driver_name, temp_val);
1019 if (adapter->temp == NX_TEMP_WARN) {
1021 "%s: Device temperature is now %d degrees C"
1022 " in normal range.\n", netxen_nic_driver_name,
1026 adapter->temp = temp_state;
1030 void netxen_watchdog_task(struct work_struct *work)
1032 struct net_device *netdev;
1033 struct netxen_adapter *adapter =
1034 container_of(work, struct netxen_adapter, watchdog_task);
1036 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1039 netdev = adapter->netdev;
1040 if ((netif_running(netdev)) && !netif_carrier_ok(netdev)) {
1041 printk(KERN_INFO "%s port %d, %s carrier is now ok\n",
1042 netxen_nic_driver_name, adapter->portnum, netdev->name);
1043 netif_carrier_on(netdev);
1046 if (netif_queue_stopped(netdev))
1047 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 pci_dev *pdev = adapter->pdev;
1064 struct net_device *netdev = adapter->netdev;
1065 int index = netxen_get_sts_refhandle(desc);
1066 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1067 struct netxen_rx_buffer *buffer;
1068 struct sk_buff *skb;
1069 u32 length = netxen_get_sts_totallength(desc);
1071 struct netxen_rcv_desc_ctx *rcv_desc;
1074 desc_ctx = netxen_get_sts_type(desc);
1075 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1076 printk("%s: %s Bad Rcv descriptor ring\n",
1077 netxen_nic_driver_name, netdev->name);
1081 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
1082 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1083 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1084 index, rcv_desc->max_rx_desc_count);
1087 buffer = &rcv_desc->rx_buf_arr[index];
1088 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1089 buffer->lro_current_frags++;
1090 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1091 buffer->lro_expected_frags =
1092 netxen_get_sts_desc_lro_cnt(desc);
1093 buffer->lro_length = length;
1095 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1096 if (buffer->lro_expected_frags != 0) {
1097 printk("LRO: (refhandle:%x) recv frag."
1098 "wait for last. flags: %x expected:%d"
1100 netxen_get_sts_desc_lro_last_frag(desc),
1101 buffer->lro_expected_frags,
1102 buffer->lro_current_frags);
1108 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1109 PCI_DMA_FROMDEVICE);
1111 skb = (struct sk_buff *)buffer->skb;
1113 if (likely(netxen_get_sts_status(desc) == STATUS_CKSUM_OK)) {
1114 adapter->stats.csummed++;
1115 skb->ip_summed = CHECKSUM_UNNECESSARY;
1117 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1118 /* True length was only available on the last pkt */
1119 skb_put(skb, buffer->lro_length);
1121 skb_put(skb, length);
1124 skb->protocol = eth_type_trans(skb, netdev);
1126 ret = netif_receive_skb(skb);
1129 * RH: Do we need these stats on a regular basis. Can we get it from
1133 case NET_RX_SUCCESS:
1134 adapter->stats.uphappy++;
1138 adapter->stats.uplcong++;
1142 adapter->stats.upmcong++;
1145 case NET_RX_CN_HIGH:
1146 adapter->stats.uphcong++;
1150 adapter->stats.updropped++;
1154 adapter->stats.updunno++;
1158 netdev->last_rx = jiffies;
1160 rcv_desc->rcv_free++;
1161 rcv_desc->rcv_pending--;
1164 * We just consumed one buffer so post a buffer.
1167 buffer->state = NETXEN_BUFFER_FREE;
1168 buffer->lro_current_frags = 0;
1169 buffer->lro_expected_frags = 0;
1171 adapter->stats.no_rcv++;
1172 adapter->stats.rxbytes += length;
1175 /* Process Receive status ring */
1176 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1178 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1179 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1180 struct status_desc *desc; /* used to read status desc here */
1181 u32 consumer = recv_ctx->status_rx_consumer;
1183 int count = 0, ring;
1185 DPRINTK(INFO, "procesing receive\n");
1187 * we assume in this case that there is only one port and that is
1188 * port #1...changes need to be done in firmware to indicate port
1189 * number as part of the descriptor. This way we will be able to get
1190 * the netdev which is associated with that device.
1192 while (count < max) {
1193 desc = &desc_head[consumer];
1194 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1195 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1196 netxen_get_sts_owner(desc));
1199 netxen_process_rcv(adapter, ctxid, desc);
1200 netxen_clear_sts_owner(desc);
1201 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1202 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1206 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
1207 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1211 /* update the consumer index in phantom */
1213 recv_ctx->status_rx_consumer = consumer;
1214 recv_ctx->status_rx_producer = producer;
1218 NETXEN_CRB_NORMALIZE(adapter,
1219 recv_crb_registers[adapter->portnum].
1220 crb_rcv_status_consumer));
1226 /* Process Command status ring */
1227 int netxen_process_cmd_ring(unsigned long data)
1231 struct netxen_adapter *adapter = (struct netxen_adapter *)data;
1234 struct netxen_cmd_buffer *buffer;
1235 struct pci_dev *pdev;
1236 struct netxen_skb_frag *frag;
1238 struct sk_buff *skb = NULL;
1241 spin_lock(&adapter->tx_lock);
1242 last_consumer = adapter->last_cmd_consumer;
1243 DPRINTK(INFO, "procesing xmit complete\n");
1244 /* we assume in this case that there is only one port and that is
1245 * port #1...changes need to be done in firmware to indicate port
1246 * number as part of the descriptor. This way we will be able to get
1247 * the netdev which is associated with that device.
1250 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1251 if (last_consumer == consumer) { /* Ring is empty */
1252 DPRINTK(INFO, "last_consumer %d == consumer %d\n",
1253 last_consumer, consumer);
1254 spin_unlock(&adapter->tx_lock);
1258 adapter->proc_cmd_buf_counter++;
1260 * Not needed - does not seem to be used anywhere.
1261 * adapter->cmd_consumer = consumer;
1263 spin_unlock(&adapter->tx_lock);
1265 while ((last_consumer != consumer) && (count1 < MAX_STATUS_HANDLE)) {
1266 buffer = &adapter->cmd_buf_arr[last_consumer];
1267 pdev = adapter->pdev;
1268 frag = &buffer->frag_array[0];
1270 if (skb && (cmpxchg(&buffer->skb, skb, 0) == skb)) {
1271 pci_unmap_single(pdev, frag->dma, frag->length,
1273 for (i = 1; i < buffer->frag_count; i++) {
1274 DPRINTK(INFO, "getting fragment no %d\n", i);
1275 frag++; /* Get the next frag */
1276 pci_unmap_page(pdev, frag->dma, frag->length,
1280 adapter->stats.skbfreed++;
1281 dev_kfree_skb_any(skb);
1283 } else if (adapter->proc_cmd_buf_counter == 1) {
1284 adapter->stats.txnullskb++;
1286 if (unlikely(netif_queue_stopped(adapter->netdev)
1287 && netif_carrier_ok(adapter->netdev))
1288 && ((jiffies - adapter->netdev->trans_start) >
1289 adapter->netdev->watchdog_timeo)) {
1290 SCHEDULE_WORK(&adapter->tx_timeout_task);
1293 last_consumer = get_next_index(last_consumer,
1294 adapter->max_tx_desc_count);
1299 spin_lock(&adapter->tx_lock);
1300 if ((--adapter->proc_cmd_buf_counter) == 0) {
1301 adapter->last_cmd_consumer = last_consumer;
1302 while ((adapter->last_cmd_consumer != consumer)
1303 && (count2 < MAX_STATUS_HANDLE)) {
1305 &adapter->cmd_buf_arr[adapter->last_cmd_consumer];
1310 adapter->last_cmd_consumer =
1311 get_next_index(adapter->last_cmd_consumer,
1312 adapter->max_tx_desc_count);
1315 if (count1 || count2) {
1316 if (netif_queue_stopped(adapter->netdev)
1317 && (adapter->flags & NETXEN_NETDEV_STATUS)) {
1318 netif_wake_queue(adapter->netdev);
1319 adapter->flags &= ~NETXEN_NETDEV_STATUS;
1323 * If everything is freed up to consumer then check if the ring is full
1324 * If the ring is full then check if more needs to be freed and
1325 * schedule the call back again.
1327 * This happens when there are 2 CPUs. One could be freeing and the
1328 * other filling it. If the ring is full when we get out of here and
1329 * the card has already interrupted the host then the host can miss the
1332 * There is still a possible race condition and the host could miss an
1333 * interrupt. The card has to take care of this.
1335 if (adapter->last_cmd_consumer == consumer &&
1336 (((adapter->cmd_producer + 1) %
1337 adapter->max_tx_desc_count) == adapter->last_cmd_consumer)) {
1338 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1340 done = (adapter->last_cmd_consumer == consumer);
1342 spin_unlock(&adapter->tx_lock);
1343 DPRINTK(INFO, "last consumer is %d in %s\n", last_consumer,
1349 * netxen_post_rx_buffers puts buffer in the Phantom memory
1351 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1353 struct pci_dev *pdev = adapter->ahw.pdev;
1354 struct sk_buff *skb;
1355 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1356 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1358 struct rcv_desc *pdesc;
1359 struct netxen_rx_buffer *buffer;
1362 netxen_ctx_msg msg = 0;
1365 rcv_desc = &recv_ctx->rcv_desc[ringid];
1367 producer = rcv_desc->producer;
1368 index = rcv_desc->begin_alloc;
1369 buffer = &rcv_desc->rx_buf_arr[index];
1370 /* We can start writing rx descriptors into the phantom memory. */
1371 while (buffer->state == NETXEN_BUFFER_FREE) {
1372 skb = dev_alloc_skb(rcv_desc->skb_size);
1373 if (unlikely(!skb)) {
1376 * We need to schedule the posting of buffers to the pegs.
1378 rcv_desc->begin_alloc = index;
1379 DPRINTK(ERR, "netxen_post_rx_buffers: "
1380 " allocated only %d buffers\n", count);
1384 count++; /* now there should be no failure */
1385 pdesc = &rcv_desc->desc_head[producer];
1387 #if defined(XGB_DEBUG)
1388 *(unsigned long *)(skb->head) = 0xc0debabe;
1389 if (skb_is_nonlinear(skb)) {
1390 printk("Allocated SKB @%p is nonlinear\n");
1393 skb_reserve(skb, 2);
1394 /* This will be setup when we receive the
1395 * buffer after it has been filled FSL TBD TBD
1396 * skb->dev = netdev;
1398 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1399 PCI_DMA_FROMDEVICE);
1400 pdesc->addr_buffer = cpu_to_le64(dma);
1402 buffer->state = NETXEN_BUFFER_BUSY;
1404 /* make a rcv descriptor */
1405 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1406 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1407 DPRINTK(INFO, "done writing descripter\n");
1409 get_next_index(producer, rcv_desc->max_rx_desc_count);
1410 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1411 buffer = &rcv_desc->rx_buf_arr[index];
1413 /* if we did allocate buffers, then write the count to Phantom */
1415 rcv_desc->begin_alloc = index;
1416 rcv_desc->rcv_pending += count;
1417 rcv_desc->producer = producer;
1418 if (rcv_desc->rcv_free >= 32) {
1419 rcv_desc->rcv_free = 0;
1421 writel((producer - 1) &
1422 (rcv_desc->max_rx_desc_count - 1),
1423 NETXEN_CRB_NORMALIZE(adapter,
1426 rcv_desc_crb[ringid].
1427 crb_rcv_producer_offset));
1429 * Write a doorbell msg to tell phanmon of change in
1430 * receive ring producer
1432 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1433 netxen_set_msg_privid(msg);
1434 netxen_set_msg_count(msg,
1437 max_rx_desc_count - 1)));
1438 netxen_set_msg_ctxid(msg, adapter->portnum);
1439 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1441 DB_NORMALIZE(adapter,
1442 NETXEN_RCV_PRODUCER_OFFSET));
1447 void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter, uint32_t ctx,
1450 struct pci_dev *pdev = adapter->ahw.pdev;
1451 struct sk_buff *skb;
1452 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1453 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1455 struct rcv_desc *pdesc;
1456 struct netxen_rx_buffer *buffer;
1460 rcv_desc = &recv_ctx->rcv_desc[ringid];
1462 producer = rcv_desc->producer;
1463 index = rcv_desc->begin_alloc;
1464 buffer = &rcv_desc->rx_buf_arr[index];
1465 /* We can start writing rx descriptors into the phantom memory. */
1466 while (buffer->state == NETXEN_BUFFER_FREE) {
1467 skb = dev_alloc_skb(rcv_desc->skb_size);
1468 if (unlikely(!skb)) {
1470 * We need to schedule the posting of buffers to the pegs.
1472 rcv_desc->begin_alloc = index;
1473 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
1474 " allocated only %d buffers\n", count);
1477 count++; /* now there should be no failure */
1478 pdesc = &rcv_desc->desc_head[producer];
1479 skb_reserve(skb, 2);
1481 * This will be setup when we receive the
1482 * buffer after it has been filled
1483 * skb->dev = netdev;
1486 buffer->state = NETXEN_BUFFER_BUSY;
1487 buffer->dma = pci_map_single(pdev, skb->data,
1489 PCI_DMA_FROMDEVICE);
1491 /* make a rcv descriptor */
1492 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1493 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1494 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1495 DPRINTK(INFO, "done writing descripter\n");
1497 get_next_index(producer, rcv_desc->max_rx_desc_count);
1498 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1499 buffer = &rcv_desc->rx_buf_arr[index];
1502 /* if we did allocate buffers, then write the count to Phantom */
1504 rcv_desc->begin_alloc = index;
1505 rcv_desc->rcv_pending += count;
1506 rcv_desc->producer = producer;
1507 if (rcv_desc->rcv_free >= 32) {
1508 rcv_desc->rcv_free = 0;
1510 writel((producer - 1) &
1511 (rcv_desc->max_rx_desc_count - 1),
1512 NETXEN_CRB_NORMALIZE(adapter,
1515 rcv_desc_crb[ringid].
1516 crb_rcv_producer_offset));
1522 int netxen_nic_tx_has_work(struct netxen_adapter *adapter)
1524 if (find_diff_among(adapter->last_cmd_consumer,
1525 adapter->cmd_producer,
1526 adapter->max_tx_desc_count) > 0)
1533 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1535 memset(&adapter->stats, 0, sizeof(adapter->stats));