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 int netxen_flash_unlock(struct netxen_adapter *adapter)
724 ret = netxen_rom_wrsr(adapter, 0);
728 ret = netxen_rom_wren(adapter);
735 #define NETXEN_BOARDTYPE 0x4008
736 #define NETXEN_BOARDNUM 0x400c
737 #define NETXEN_CHIPNUM 0x4010
738 #define NETXEN_ROMBUS_RESET 0xFFFFFFFF
739 #define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
740 #define NETXEN_ROM_FOUND_INIT 0x400
742 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
744 int addr, val, status;
747 struct crb_addr_pair *buf;
751 status = netxen_nic_get_board_info(adapter);
753 printk("%s: netxen_pinit_from_rom: Error getting board info\n",
754 netxen_nic_driver_name);
756 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
757 NETXEN_ROMBUS_RESET);
761 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
762 printk("P2 ROM board type: 0x%08x\n", val);
764 printk("Could not read board type\n");
765 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
766 printk("P2 ROM board num: 0x%08x\n", val);
768 printk("Could not read board number\n");
769 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
770 printk("P2 ROM chip num: 0x%08x\n", val);
772 printk("Could not read chip number\n");
775 if (netxen_rom_fast_read(adapter, 0, &n) == 0
776 && (n & NETXEN_ROM_FIRST_BARRIER)) {
777 n &= ~NETXEN_ROM_ROUNDUP;
778 if (n < NETXEN_ROM_FOUND_INIT) {
780 printk("%s: %d CRB init values found"
781 " in ROM.\n", netxen_nic_driver_name, n);
783 printk("%s:n=0x%x Error! NetXen card flash not"
784 " initialized.\n", __FUNCTION__, n);
787 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
789 printk("%s: netxen_pinit_from_rom: Unable to calloc "
790 "memory.\n", netxen_nic_driver_name);
793 for (i = 0; i < n; i++) {
794 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
795 || netxen_rom_fast_read(adapter, 8 * i + 8,
803 printk("%s: PCI: 0x%08x == 0x%08x\n",
804 netxen_nic_driver_name, (unsigned int)
805 netxen_decode_crb_addr((unsigned long)
808 for (i = 0; i < n; i++) {
810 off = netxen_decode_crb_addr((unsigned long)buf[i].addr);
811 if (off == NETXEN_ADDR_ERROR) {
812 printk(KERN_ERR"CRB init value out of range %lx\n",
816 off += NETXEN_PCI_CRBSPACE;
817 /* skipping cold reboot MAGIC */
818 if (off == NETXEN_CAM_RAM(0x1fc))
821 /* After writing this register, HW needs time for CRB */
822 /* to quiet down (else crb_window returns 0xffffffff) */
823 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
825 /* hold xdma in reset also */
826 buf[i].data = NETXEN_NIC_XDMA_RESET;
829 if (ADDR_IN_WINDOW1(off)) {
831 NETXEN_CRB_NORMALIZE(adapter, off));
833 netxen_nic_pci_change_crbwindow(adapter, 0);
835 pci_base_offset(adapter, off));
837 netxen_nic_pci_change_crbwindow(adapter, 1);
839 if (init_delay == 1) {
847 /* disable_peg_cache_all */
849 /* unreset_net_cache */
850 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
852 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
854 /* p2dn replyCount */
855 netxen_crb_writelit_adapter(adapter,
856 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
857 /* disable_peg_cache 0 */
858 netxen_crb_writelit_adapter(adapter,
859 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
860 /* disable_peg_cache 1 */
861 netxen_crb_writelit_adapter(adapter,
862 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
867 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
869 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
872 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
874 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
877 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
879 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
882 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
884 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
890 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
896 adapter->dummy_dma.addr =
897 pci_alloc_consistent(adapter->ahw.pdev,
898 NETXEN_HOST_DUMMY_DMA_SIZE,
899 &adapter->dummy_dma.phys_addr);
900 if (adapter->dummy_dma.addr == NULL) {
901 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
906 addr = (uint64_t) adapter->dummy_dma.phys_addr;
907 hi = (addr >> 32) & 0xffffffff;
908 lo = addr & 0xffffffff;
910 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
911 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
916 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
918 if (adapter->dummy_dma.addr) {
919 pci_free_consistent(adapter->ahw.pdev,
920 NETXEN_HOST_DUMMY_DMA_SIZE,
921 adapter->dummy_dma.addr,
922 adapter->dummy_dma.phys_addr);
923 adapter->dummy_dma.addr = NULL;
927 void netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
933 val = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
934 while (val != PHAN_INITIALIZE_COMPLETE && loops < 200000) {
938 readl(NETXEN_CRB_NORMALIZE
939 (adapter, CRB_CMDPEG_STATE));
942 if (val != PHAN_INITIALIZE_COMPLETE)
943 printk("WARNING: Initial boot wait loop failed...\n");
947 int netxen_nic_rx_has_work(struct netxen_adapter *adapter)
951 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
952 struct netxen_recv_context *recv_ctx =
953 &(adapter->recv_ctx[ctx]);
955 struct status_desc *desc_head;
956 struct status_desc *desc;
958 consumer = recv_ctx->status_rx_consumer;
959 desc_head = recv_ctx->rcv_status_desc_head;
960 desc = &desc_head[consumer];
962 if (netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)
969 static inline int netxen_nic_check_temp(struct netxen_adapter *adapter)
972 struct netxen_port *port;
973 struct net_device *netdev;
974 uint32_t temp, temp_state, temp_val;
977 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
979 temp_state = nx_get_temp_state(temp);
980 temp_val = nx_get_temp_val(temp);
982 if (temp_state == NX_TEMP_PANIC) {
984 "%s: Device temperature %d degrees C exceeds"
985 " maximum allowed. Hardware has been shut down.\n",
986 netxen_nic_driver_name, temp_val);
987 for (port_num = 0; port_num < adapter->ahw.max_ports;
989 port = adapter->port[port_num];
990 netdev = port->netdev;
992 netif_carrier_off(netdev);
993 netif_stop_queue(netdev);
996 } else if (temp_state == NX_TEMP_WARN) {
997 if (adapter->temp == NX_TEMP_NORMAL) {
999 "%s: Device temperature %d degrees C "
1000 "exceeds operating range."
1001 " Immediate action needed.\n",
1002 netxen_nic_driver_name, temp_val);
1005 if (adapter->temp == NX_TEMP_WARN) {
1007 "%s: Device temperature is now %d degrees C"
1008 " in normal range.\n", netxen_nic_driver_name,
1012 adapter->temp = temp_state;
1016 void netxen_watchdog_task(struct work_struct *work)
1019 struct netxen_port *port;
1020 struct net_device *netdev;
1021 struct netxen_adapter *adapter =
1022 container_of(work, struct netxen_adapter, watchdog_task);
1024 if (netxen_nic_check_temp(adapter))
1027 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
1028 port = adapter->port[port_num];
1029 netdev = port->netdev;
1031 if ((netif_running(netdev)) && !netif_carrier_ok(netdev)) {
1032 printk(KERN_INFO "%s port %d, %s carrier is now ok\n",
1033 netxen_nic_driver_name, port_num, netdev->name);
1034 netif_carrier_on(netdev);
1037 if (netif_queue_stopped(netdev))
1038 netif_wake_queue(netdev);
1041 if (adapter->handle_phy_intr)
1042 adapter->handle_phy_intr(adapter);
1043 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1047 * netxen_process_rcv() send the received packet to the protocol stack.
1048 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1049 * invoke the routine to send more rx buffers to the Phantom...
1052 netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1053 struct status_desc *desc)
1055 struct netxen_port *port = adapter->port[netxen_get_sts_port(desc)];
1056 struct pci_dev *pdev = port->pdev;
1057 struct net_device *netdev = port->netdev;
1058 int index = netxen_get_sts_refhandle(desc);
1059 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1060 struct netxen_rx_buffer *buffer;
1061 struct sk_buff *skb;
1062 u32 length = netxen_get_sts_totallength(desc);
1064 struct netxen_rcv_desc_ctx *rcv_desc;
1067 desc_ctx = netxen_get_sts_type(desc);
1068 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1069 printk("%s: %s Bad Rcv descriptor ring\n",
1070 netxen_nic_driver_name, netdev->name);
1074 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
1075 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1076 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1077 index, rcv_desc->max_rx_desc_count);
1080 buffer = &rcv_desc->rx_buf_arr[index];
1081 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1082 buffer->lro_current_frags++;
1083 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1084 buffer->lro_expected_frags =
1085 netxen_get_sts_desc_lro_cnt(desc);
1086 buffer->lro_length = length;
1088 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1089 if (buffer->lro_expected_frags != 0) {
1090 printk("LRO: (refhandle:%x) recv frag."
1091 "wait for last. flags: %x expected:%d"
1093 netxen_get_sts_desc_lro_last_frag(desc),
1094 buffer->lro_expected_frags,
1095 buffer->lro_current_frags);
1101 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1102 PCI_DMA_FROMDEVICE);
1104 skb = (struct sk_buff *)buffer->skb;
1106 if (likely(netxen_get_sts_status(desc) == STATUS_CKSUM_OK)) {
1107 port->stats.csummed++;
1108 skb->ip_summed = CHECKSUM_UNNECESSARY;
1111 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1112 /* True length was only available on the last pkt */
1113 skb_put(skb, buffer->lro_length);
1115 skb_put(skb, length);
1118 skb->protocol = eth_type_trans(skb, netdev);
1120 ret = netif_receive_skb(skb);
1123 * RH: Do we need these stats on a regular basis. Can we get it from
1127 case NET_RX_SUCCESS:
1128 port->stats.uphappy++;
1132 port->stats.uplcong++;
1136 port->stats.upmcong++;
1139 case NET_RX_CN_HIGH:
1140 port->stats.uphcong++;
1144 port->stats.updropped++;
1148 port->stats.updunno++;
1152 netdev->last_rx = jiffies;
1154 rcv_desc->rcv_free++;
1155 rcv_desc->rcv_pending--;
1158 * We just consumed one buffer so post a buffer.
1160 adapter->stats.post_called++;
1162 buffer->state = NETXEN_BUFFER_FREE;
1163 buffer->lro_current_frags = 0;
1164 buffer->lro_expected_frags = 0;
1166 port->stats.no_rcv++;
1167 port->stats.rxbytes += length;
1170 /* Process Receive status ring */
1171 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1173 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1174 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1175 struct status_desc *desc; /* used to read status desc here */
1176 u32 consumer = recv_ctx->status_rx_consumer;
1178 int count = 0, ring;
1180 DPRINTK(INFO, "procesing receive\n");
1182 * we assume in this case that there is only one port and that is
1183 * port #1...changes need to be done in firmware to indicate port
1184 * number as part of the descriptor. This way we will be able to get
1185 * the netdev which is associated with that device.
1187 while (count < max) {
1188 desc = &desc_head[consumer];
1189 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1190 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1191 netxen_get_sts_owner(desc));
1194 netxen_process_rcv(adapter, ctxid, desc);
1195 netxen_clear_sts_owner(desc);
1196 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1197 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1201 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
1202 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1206 /* update the consumer index in phantom */
1208 adapter->stats.process_rcv++;
1209 recv_ctx->status_rx_consumer = consumer;
1210 recv_ctx->status_rx_producer = producer;
1214 NETXEN_CRB_NORMALIZE(adapter,
1215 recv_crb_registers[ctxid].
1216 crb_rcv_status_consumer));
1222 /* Process Command status ring */
1223 int netxen_process_cmd_ring(unsigned long data)
1227 struct netxen_adapter *adapter = (struct netxen_adapter *)data;
1230 struct netxen_cmd_buffer *buffer;
1231 struct netxen_port *port; /* port #1 */
1232 struct netxen_port *nport;
1233 struct pci_dev *pdev;
1234 struct netxen_skb_frag *frag;
1236 struct sk_buff *skb = NULL;
1240 spin_lock(&adapter->tx_lock);
1241 last_consumer = adapter->last_cmd_consumer;
1242 DPRINTK(INFO, "procesing xmit complete\n");
1243 /* we assume in this case that there is only one port and that is
1244 * port #1...changes need to be done in firmware to indicate port
1245 * number as part of the descriptor. This way we will be able to get
1246 * the netdev which is associated with that device.
1249 consumer = *(adapter->cmd_consumer);
1250 if (last_consumer == consumer) { /* Ring is empty */
1251 DPRINTK(INFO, "last_consumer %d == consumer %d\n",
1252 last_consumer, consumer);
1253 spin_unlock(&adapter->tx_lock);
1257 adapter->proc_cmd_buf_counter++;
1258 adapter->stats.process_xmit++;
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 port = adapter->port[buffer->port];
1269 frag = &buffer->frag_array[0];
1271 if (skb && (cmpxchg(&buffer->skb, skb, 0) == skb)) {
1272 pci_unmap_single(pdev, frag->dma, frag->length,
1274 for (i = 1; i < buffer->frag_count; i++) {
1275 DPRINTK(INFO, "getting fragment no %d\n", i);
1276 frag++; /* Get the next frag */
1277 pci_unmap_page(pdev, frag->dma, frag->length,
1281 port->stats.skbfreed++;
1282 dev_kfree_skb_any(skb);
1284 } else if (adapter->proc_cmd_buf_counter == 1) {
1285 port->stats.txnullskb++;
1287 if (unlikely(netif_queue_stopped(port->netdev)
1288 && netif_carrier_ok(port->netdev))
1289 && ((jiffies - port->netdev->trans_start) >
1290 port->netdev->watchdog_timeo)) {
1291 SCHEDULE_WORK(&port->tx_timeout_task);
1294 last_consumer = get_next_index(last_consumer,
1295 adapter->max_tx_desc_count);
1298 adapter->stats.noxmitdone += count1;
1301 spin_lock(&adapter->tx_lock);
1302 if ((--adapter->proc_cmd_buf_counter) == 0) {
1303 adapter->last_cmd_consumer = last_consumer;
1304 while ((adapter->last_cmd_consumer != consumer)
1305 && (count2 < MAX_STATUS_HANDLE)) {
1307 &adapter->cmd_buf_arr[adapter->last_cmd_consumer];
1312 adapter->last_cmd_consumer =
1313 get_next_index(adapter->last_cmd_consumer,
1314 adapter->max_tx_desc_count);
1317 if (count1 || count2) {
1318 for (p = 0; p < adapter->ahw.max_ports; p++) {
1319 nport = adapter->port[p];
1320 if (netif_queue_stopped(nport->netdev)
1321 && (nport->flags & NETXEN_NETDEV_STATUS)) {
1322 netif_wake_queue(nport->netdev);
1323 nport->flags &= ~NETXEN_NETDEV_STATUS;
1328 * If everything is freed up to consumer then check if the ring is full
1329 * If the ring is full then check if more needs to be freed and
1330 * schedule the call back again.
1332 * This happens when there are 2 CPUs. One could be freeing and the
1333 * other filling it. If the ring is full when we get out of here and
1334 * the card has already interrupted the host then the host can miss the
1337 * There is still a possible race condition and the host could miss an
1338 * interrupt. The card has to take care of this.
1340 if (adapter->last_cmd_consumer == consumer &&
1341 (((adapter->cmd_producer + 1) %
1342 adapter->max_tx_desc_count) == adapter->last_cmd_consumer)) {
1343 consumer = *(adapter->cmd_consumer);
1345 done = (adapter->last_cmd_consumer == consumer);
1347 spin_unlock(&adapter->tx_lock);
1348 DPRINTK(INFO, "last consumer is %d in %s\n", last_consumer,
1354 * netxen_post_rx_buffers puts buffer in the Phantom memory
1356 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1358 struct pci_dev *pdev = adapter->ahw.pdev;
1359 struct sk_buff *skb;
1360 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1361 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1363 struct rcv_desc *pdesc;
1364 struct netxen_rx_buffer *buffer;
1367 netxen_ctx_msg msg = 0;
1370 adapter->stats.post_called++;
1371 rcv_desc = &recv_ctx->rcv_desc[ringid];
1373 producer = rcv_desc->producer;
1374 index = rcv_desc->begin_alloc;
1375 buffer = &rcv_desc->rx_buf_arr[index];
1376 /* We can start writing rx descriptors into the phantom memory. */
1377 while (buffer->state == NETXEN_BUFFER_FREE) {
1378 skb = dev_alloc_skb(rcv_desc->skb_size);
1379 if (unlikely(!skb)) {
1382 * We need to schedule the posting of buffers to the pegs.
1384 rcv_desc->begin_alloc = index;
1385 DPRINTK(ERR, "netxen_post_rx_buffers: "
1386 " allocated only %d buffers\n", count);
1390 count++; /* now there should be no failure */
1391 pdesc = &rcv_desc->desc_head[producer];
1393 #if defined(XGB_DEBUG)
1394 *(unsigned long *)(skb->head) = 0xc0debabe;
1395 if (skb_is_nonlinear(skb)) {
1396 printk("Allocated SKB @%p is nonlinear\n");
1399 skb_reserve(skb, 2);
1400 /* This will be setup when we receive the
1401 * buffer after it has been filled FSL TBD TBD
1402 * skb->dev = netdev;
1404 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1405 PCI_DMA_FROMDEVICE);
1406 pdesc->addr_buffer = cpu_to_le64(dma);
1408 buffer->state = NETXEN_BUFFER_BUSY;
1410 /* make a rcv descriptor */
1411 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1412 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1413 DPRINTK(INFO, "done writing descripter\n");
1415 get_next_index(producer, rcv_desc->max_rx_desc_count);
1416 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1417 buffer = &rcv_desc->rx_buf_arr[index];
1419 /* if we did allocate buffers, then write the count to Phantom */
1421 rcv_desc->begin_alloc = index;
1422 rcv_desc->rcv_pending += count;
1423 adapter->stats.lastposted = count;
1424 adapter->stats.posted += count;
1425 rcv_desc->producer = producer;
1426 if (rcv_desc->rcv_free >= 32) {
1427 rcv_desc->rcv_free = 0;
1429 writel((producer - 1) &
1430 (rcv_desc->max_rx_desc_count - 1),
1431 NETXEN_CRB_NORMALIZE(adapter,
1432 recv_crb_registers[0].
1433 rcv_desc_crb[ringid].
1434 crb_rcv_producer_offset));
1436 * Write a doorbell msg to tell phanmon of change in
1437 * receive ring producer
1439 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1440 netxen_set_msg_privid(msg);
1441 netxen_set_msg_count(msg,
1444 max_rx_desc_count - 1)));
1445 netxen_set_msg_ctxid(msg, 0);
1446 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1448 DB_NORMALIZE(adapter,
1449 NETXEN_RCV_PRODUCER_OFFSET));
1454 void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter, uint32_t ctx,
1457 struct pci_dev *pdev = adapter->ahw.pdev;
1458 struct sk_buff *skb;
1459 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1460 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1462 struct rcv_desc *pdesc;
1463 struct netxen_rx_buffer *buffer;
1467 adapter->stats.post_called++;
1468 rcv_desc = &recv_ctx->rcv_desc[ringid];
1470 producer = rcv_desc->producer;
1471 index = rcv_desc->begin_alloc;
1472 buffer = &rcv_desc->rx_buf_arr[index];
1473 /* We can start writing rx descriptors into the phantom memory. */
1474 while (buffer->state == NETXEN_BUFFER_FREE) {
1475 skb = dev_alloc_skb(rcv_desc->skb_size);
1476 if (unlikely(!skb)) {
1478 * We need to schedule the posting of buffers to the pegs.
1480 rcv_desc->begin_alloc = index;
1481 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
1482 " allocated only %d buffers\n", count);
1485 count++; /* now there should be no failure */
1486 pdesc = &rcv_desc->desc_head[producer];
1487 skb_reserve(skb, 2);
1489 * This will be setup when we receive the
1490 * buffer after it has been filled
1491 * skb->dev = netdev;
1494 buffer->state = NETXEN_BUFFER_BUSY;
1495 buffer->dma = pci_map_single(pdev, skb->data,
1497 PCI_DMA_FROMDEVICE);
1499 /* make a rcv descriptor */
1500 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1501 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1502 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1503 DPRINTK(INFO, "done writing descripter\n");
1505 get_next_index(producer, rcv_desc->max_rx_desc_count);
1506 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1507 buffer = &rcv_desc->rx_buf_arr[index];
1510 /* if we did allocate buffers, then write the count to Phantom */
1512 rcv_desc->begin_alloc = index;
1513 rcv_desc->rcv_pending += count;
1514 adapter->stats.lastposted = count;
1515 adapter->stats.posted += count;
1516 rcv_desc->producer = producer;
1517 if (rcv_desc->rcv_free >= 32) {
1518 rcv_desc->rcv_free = 0;
1520 writel((producer - 1) &
1521 (rcv_desc->max_rx_desc_count - 1),
1522 NETXEN_CRB_NORMALIZE(adapter,
1523 recv_crb_registers[0].
1524 rcv_desc_crb[ringid].
1525 crb_rcv_producer_offset));
1531 int netxen_nic_tx_has_work(struct netxen_adapter *adapter)
1533 if (find_diff_among(adapter->last_cmd_consumer,
1534 adapter->cmd_producer,
1535 adapter->max_tx_desc_count) > 0)
1542 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1544 struct netxen_port *port;
1547 memset(&adapter->stats, 0, sizeof(adapter->stats));
1548 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
1549 port = adapter->port[port_num];
1550 memset(&port->stats, 0, sizeof(port->stats));