📄 subr.c
字号:
/***************************************************************************** * * * File: subr.c * * $Revision: 1.27 $ * * $Date: 2005/06/22 01:08:36 $ * * Description: * * Various subroutines (intr,pio,etc.) used by Chelsio 10G Ethernet driver. * * part of the Chelsio 10Gb Ethernet Driver. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License, version 2, as * * published by the Free Software Foundation. * * * * You should have received a copy of the GNU General Public License along * * with this program; if not, write to the Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * * * http://www.chelsio.com * * * * Copyright (c) 2003 - 2005 Chelsio Communications, Inc. * * All rights reserved. * * * * Maintainers: maintainers@chelsio.com * * * * Authors: Dimitrios Michailidis <dm@chelsio.com> * * Tina Yang <tainay@chelsio.com> * * Felix Marti <felix@chelsio.com> * * Scott Bardone <sbardone@chelsio.com> * * Kurt Ottaway <kottaway@chelsio.com> * * Frank DiMambro <frank@chelsio.com> * * * * History: * * * ****************************************************************************/#include "common.h"#include "elmer0.h"#include "regs.h"#include "gmac.h"#include "cphy.h"#include "sge.h"#include "espi.h"/** * t1_wait_op_done - wait until an operation is completed * @adapter: the adapter performing the operation * @reg: the register to check for completion * @mask: a single-bit field within @reg that indicates completion * @polarity: the value of the field when the operation is completed * @attempts: number of check iterations * @delay: delay in usecs between iterations * * Wait until an operation is completed by checking a bit in a register * up to @attempts times. Returns %0 if the operation completes and %1 * otherwise. */static int t1_wait_op_done(adapter_t *adapter, int reg, u32 mask, int polarity, int attempts, int delay){ while (1) { u32 val = readl(adapter->regs + reg) & mask; if (!!val == polarity) return 0; if (--attempts == 0) return 1; if (delay) udelay(delay); }}#define TPI_ATTEMPTS 50/* * Write a register over the TPI interface (unlocked and locked versions). */static int __t1_tpi_write(adapter_t *adapter, u32 addr, u32 value){ int tpi_busy; writel(addr, adapter->regs + A_TPI_ADDR); writel(value, adapter->regs + A_TPI_WR_DATA); writel(F_TPIWR, adapter->regs + A_TPI_CSR); tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1, TPI_ATTEMPTS, 3); if (tpi_busy) CH_ALERT("%s: TPI write to 0x%x failed\n", adapter->name, addr); return tpi_busy;}int t1_tpi_write(adapter_t *adapter, u32 addr, u32 value){ int ret; spin_lock(&(adapter)->tpi_lock); ret = __t1_tpi_write(adapter, addr, value); spin_unlock(&(adapter)->tpi_lock); return ret;}/* * Read a register over the TPI interface (unlocked and locked versions). */static int __t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp){ int tpi_busy; writel(addr, adapter->regs + A_TPI_ADDR); writel(0, adapter->regs + A_TPI_CSR); tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1, TPI_ATTEMPTS, 3); if (tpi_busy) CH_ALERT("%s: TPI read from 0x%x failed\n", adapter->name, addr); else *valp = readl(adapter->regs + A_TPI_RD_DATA); return tpi_busy;}int t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp){ int ret; spin_lock(&(adapter)->tpi_lock); ret = __t1_tpi_read(adapter, addr, valp); spin_unlock(&(adapter)->tpi_lock); return ret;}/* * Called when a port's link settings change to propagate the new values to the * associated PHY and MAC. After performing the common tasks it invokes an * OS-specific handler. *//* static */ void link_changed(adapter_t *adapter, int port_id){ int link_ok, speed, duplex, fc; struct cphy *phy = adapter->port[port_id].phy; struct link_config *lc = &adapter->port[port_id].link_config; phy->ops->get_link_status(phy, &link_ok, &speed, &duplex, &fc); lc->speed = speed < 0 ? SPEED_INVALID : speed; lc->duplex = duplex < 0 ? DUPLEX_INVALID : duplex; if (!(lc->requested_fc & PAUSE_AUTONEG)) fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX); if (link_ok && speed >= 0 && lc->autoneg == AUTONEG_ENABLE) { /* Set MAC speed, duplex, and flow control to match PHY. */ struct cmac *mac = adapter->port[port_id].mac; mac->ops->set_speed_duplex_fc(mac, speed, duplex, fc); lc->fc = (unsigned char)fc; } t1_link_changed(adapter, port_id, link_ok, speed, duplex, fc);}static int t1_pci_intr_handler(adapter_t *adapter){ u32 pcix_cause; pci_read_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, &pcix_cause); if (pcix_cause) { pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, pcix_cause); t1_fatal_err(adapter); /* PCI errors are fatal */ } return 0;}/* * Wait until Elmer's MI1 interface is ready for new operations. */static int mi1_wait_until_ready(adapter_t *adapter, int mi1_reg){ int attempts = 100, busy; do { u32 val; __t1_tpi_read(adapter, mi1_reg, &val); busy = val & F_MI1_OP_BUSY; if (busy) udelay(10); } while (busy && --attempts); if (busy) CH_ALERT("%s: MDIO operation timed out\n", adapter->name); return busy;}/* * MI1 MDIO initialization. */static void mi1_mdio_init(adapter_t *adapter, const struct board_info *bi){ u32 clkdiv = bi->clock_elmer0 / (2 * bi->mdio_mdc) - 1; u32 val = F_MI1_PREAMBLE_ENABLE | V_MI1_MDI_INVERT(bi->mdio_mdiinv) | V_MI1_MDI_ENABLE(bi->mdio_mdien) | V_MI1_CLK_DIV(clkdiv); if (!(bi->caps & SUPPORTED_10000baseT_Full)) val |= V_MI1_SOF(1); t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_CFG, val);}static int mi1_mdio_ext_read(adapter_t *adapter, int phy_addr, int mmd_addr, int reg_addr, unsigned int *valp){ u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr); spin_lock(&(adapter)->tpi_lock); /* Write the address we want. */ __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr); __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr); __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_ADDRESS); mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP); /* Write the operation we want. */ __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_READ); mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP); /* Read the data. */ __t1_tpi_read(adapter, A_ELMER0_PORT0_MI1_DATA, valp); spin_unlock(&(adapter)->tpi_lock); return 0;}static int mi1_mdio_ext_write(adapter_t *adapter, int phy_addr, int mmd_addr, int reg_addr, unsigned int val){ u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr); spin_lock(&(adapter)->tpi_lock); /* Write the address we want. */ __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr); __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr); __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_ADDRESS); mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP); /* Write the data. */ __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, val); __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_WRITE); mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP); spin_unlock(&(adapter)->tpi_lock); return 0;}static struct mdio_ops mi1_mdio_ext_ops = { mi1_mdio_init, mi1_mdio_ext_read, mi1_mdio_ext_write};enum { CH_BRD_N110_1F, CH_BRD_N210_1F,};static struct board_info t1_board[] = {{ CHBT_BOARD_N110, 1/*ports#*/, SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE /*caps*/, CHBT_TERM_T1, CHBT_MAC_PM3393, CHBT_PHY_88X2010, 125000000/*clk-core*/, 0/*clk-mc3*/, 0/*clk-mc4*/, 1/*espi-ports*/, 0/*clk-cspi*/, 44/*clk-elmer0*/, 0/*mdien*/, 0/*mdiinv*/, 1/*mdc*/, 0/*phybaseaddr*/, &t1_pm3393_ops, &t1_mv88x201x_ops, &mi1_mdio_ext_ops, "Chelsio N110 1x10GBaseX NIC" },{ CHBT_BOARD_N210, 1/*ports#*/, SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE /*caps*/, CHBT_TERM_T2, CHBT_MAC_PM3393, CHBT_PHY_88X2010, 125000000/*clk-core*/, 0/*clk-mc3*/, 0/*clk-mc4*/, 1/*espi-ports*/, 0/*clk-cspi*/, 44/*clk-elmer0*/, 0/*mdien*/, 0/*mdiinv*/, 1/*mdc*/, 0/*phybaseaddr*/, &t1_pm3393_ops, &t1_mv88x201x_ops, &mi1_mdio_ext_ops, "Chelsio N210 1x10GBaseX NIC" },};struct pci_device_id t1_pci_tbl[] = { CH_DEVICE(7, 0, CH_BRD_N110_1F), CH_DEVICE(10, 1, CH_BRD_N210_1F), { 0, }};MODULE_DEVICE_TABLE(pci, t1_pci_tbl);/* * Return the board_info structure with a given index. Out-of-range indices * return NULL. */const struct board_info *t1_get_board_info(unsigned int board_id){ return board_id < ARRAY_SIZE(t1_board) ? &t1_board[board_id] : NULL;}struct chelsio_vpd_t { u32 format_version; u8 serial_number[16]; u8 mac_base_address[6]; u8 pad[2]; /* make multiple-of-4 size requirement explicit */};#define EEPROMSIZE (8 * 1024)#define EEPROM_MAX_POLL 4/* * Read SEEPROM. A zero is written to the flag register when the addres is * written to the Control register. The hardware device will set the flag to a * one when 4B have been transferred to the Data register. */int t1_seeprom_read(adapter_t *adapter, u32 addr, u32 *data){ int i = EEPROM_MAX_POLL; u16 val; if (addr >= EEPROMSIZE || (addr & 3)) return -EINVAL; pci_write_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, (u16)addr); do { udelay(50); pci_read_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, &val); } while (!(val & F_VPD_OP_FLAG) && --i); if (!(val & F_VPD_OP_FLAG)) { CH_ERR("%s: reading EEPROM address 0x%x failed\n", adapter->name, addr); return -EIO; } pci_read_config_dword(adapter->pdev, A_PCICFG_VPD_DATA, data); *data = le32_to_cpu(*data); return 0;}static int t1_eeprom_vpd_get(adapter_t *adapter, struct chelsio_vpd_t *vpd){ int addr, ret = 0; for (addr = 0; !ret && addr < sizeof(*vpd); addr += sizeof(u32)) ret = t1_seeprom_read(adapter, addr, (u32 *)((u8 *)vpd + addr)); return ret;}/* * Read a port's MAC address from the VPD ROM. */static int vpd_macaddress_get(adapter_t *adapter, int index, u8 mac_addr[]){ struct chelsio_vpd_t vpd; if (t1_eeprom_vpd_get(adapter, &vpd)) return 1; memcpy(mac_addr, vpd.mac_base_address, 5); mac_addr[5] = vpd.mac_base_address[5] + index; return 0;}/* * Set up the MAC/PHY according to the requested link settings. * * If the PHY can auto-negotiate first decide what to advertise, then * enable/disable auto-negotiation as desired and reset. * * If the PHY does not auto-negotiate we just reset it. * * If auto-negotiation is off set the MAC to the proper speed/duplex/FC, * otherwise do it later based on the outcome of auto-negotiation. */int t1_link_start(struct cphy *phy, struct cmac *mac, struct link_config *lc){ unsigned int fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX); if (lc->supported & SUPPORTED_Autoneg) { lc->advertising &= ~(ADVERTISED_ASYM_PAUSE | ADVERTISED_PAUSE); if (fc) { lc->advertising |= ADVERTISED_ASYM_PAUSE; if (fc == (PAUSE_RX | PAUSE_TX)) lc->advertising |= ADVERTISED_PAUSE; } phy->ops->advertise(phy, lc->advertising); if (lc->autoneg == AUTONEG_DISABLE) { lc->speed = lc->requested_speed; lc->duplex = lc->requested_duplex; lc->fc = (unsigned char)fc; mac->ops->set_speed_duplex_fc(mac, lc->speed, lc->duplex, fc); /* Also disables autoneg */ phy->ops->set_speed_duplex(phy, lc->speed, lc->duplex); phy->ops->reset(phy, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -