linux/drivers/net/ethernet/intel/e100.c

// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 1999 - 2006 Intel Corporation. */

/*
 *	e100.c: Intel(R) PRO/100 ethernet driver
 *
 *	(Re)written 2003 by [email protected].  Based loosely on
 *	original e100 driver, but better described as a munging of
 *	e100, e1000, eepro100, tg3, 8139cp, and other drivers.
 *
 *	References:
 *		Intel 8255x 10/100 Mbps Ethernet Controller Family,
 *		Open Source Software Developers Manual,
 *		http://sourceforge.net/projects/e1000
 *
 *
 *	                      Theory of Operation
 *
 *	I.   General
 *
 *	The driver supports Intel(R) 10/100 Mbps PCI Fast Ethernet
 *	controller family, which includes the 82557, 82558, 82559, 82550,
 *	82551, and 82562 devices.  82558 and greater controllers
 *	integrate the Intel 82555 PHY.  The controllers are used in
 *	server and client network interface cards, as well as in
 *	LAN-On-Motherboard (LOM), CardBus, MiniPCI, and ICHx
 *	configurations.  8255x supports a 32-bit linear addressing
 *	mode and operates at 33Mhz PCI clock rate.
 *
 *	II.  Driver Operation
 *
 *	Memory-mapped mode is used exclusively to access the device's
 *	shared-memory structure, the Control/Status Registers (CSR). All
 *	setup, configuration, and control of the device, including queuing
 *	of Tx, Rx, and configuration commands is through the CSR.
 *	cmd_lock serializes accesses to the CSR command register.  cb_lock
 *	protects the shared Command Block List (CBL).
 *
 *	8255x is highly MII-compliant and all access to the PHY go
 *	through the Management Data Interface (MDI).  Consequently, the
 *	driver leverages the mii.c library shared with other MII-compliant
 *	devices.
 *
 *	Big- and Little-Endian byte order as well as 32- and 64-bit
 *	archs are supported.  Weak-ordered memory and non-cache-coherent
 *	archs are supported.
 *
 *	III. Transmit
 *
 *	A Tx skb is mapped and hangs off of a TCB.  TCBs are linked
 *	together in a fixed-size ring (CBL) thus forming the flexible mode
 *	memory structure.  A TCB marked with the suspend-bit indicates
 *	the end of the ring.  The last TCB processed suspends the
 *	controller, and the controller can be restarted by issue a CU
 *	resume command to continue from the suspend point, or a CU start
 *	command to start at a given position in the ring.
 *
 *	Non-Tx commands (config, multicast setup, etc) are linked
 *	into the CBL ring along with Tx commands.  The common structure
 *	used for both Tx and non-Tx commands is the Command Block (CB).
 *
 *	cb_to_use is the next CB to use for queuing a command; cb_to_clean
 *	is the next CB to check for completion; cb_to_send is the first
 *	CB to start on in case of a previous failure to resume.  CB clean
 *	up happens in interrupt context in response to a CU interrupt.
 *	cbs_avail keeps track of number of free CB resources available.
 *
 * 	Hardware padding of short packets to minimum packet size is
 * 	enabled.  82557 pads with 7Eh, while the later controllers pad
 * 	with 00h.
 *
 *	IV.  Receive
 *
 *	The Receive Frame Area (RFA) comprises a ring of Receive Frame
 *	Descriptors (RFD) + data buffer, thus forming the simplified mode
 *	memory structure.  Rx skbs are allocated to contain both the RFD
 *	and the data buffer, but the RFD is pulled off before the skb is
 *	indicated.  The data buffer is aligned such that encapsulated
 *	protocol headers are u32-aligned.  Since the RFD is part of the
 *	mapped shared memory, and completion status is contained within
 *	the RFD, the RFD must be dma_sync'ed to maintain a consistent
 *	view from software and hardware.
 *
 *	In order to keep updates to the RFD link field from colliding with
 *	hardware writes to mark packets complete, we use the feature that
 *	hardware will not write to a size 0 descriptor and mark the previous
 *	packet as end-of-list (EL).   After updating the link, we remove EL
 *	and only then restore the size such that hardware may use the
 *	previous-to-end RFD.
 *
 *	Under typical operation, the  receive unit (RU) is start once,
 *	and the controller happily fills RFDs as frames arrive.  If
 *	replacement RFDs cannot be allocated, or the RU goes non-active,
 *	the RU must be restarted.  Frame arrival generates an interrupt,
 *	and Rx indication and re-allocation happen in the same context,
 *	therefore no locking is required.  A software-generated interrupt
 *	is generated from the watchdog to recover from a failed allocation
 *	scenario where all Rx resources have been indicated and none re-
 *	placed.
 *
 *	V.   Miscellaneous
 *
 * 	VLAN offloading of tagging, stripping and filtering is not
 * 	supported, but driver will accommodate the extra 4-byte VLAN tag
 * 	for processing by upper layers.  Tx/Rx Checksum offloading is not
 * 	supported.  Tx Scatter/Gather is not supported.  Jumbo Frames is
 * 	not supported (hardware limitation).
 *
 * 	MagicPacket(tm) WoL support is enabled/disabled via ethtool.
 *
 * 	Thanks to JC ([email protected]) for helping with
 * 	testing/troubleshooting the development driver.
 *
 * 	TODO:
 * 	o several entry points race with dev->close
 * 	o check for tx-no-resources/stop Q races with tx clean/wake Q
 *
 *	FIXES:
 * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
 *	- Stratus87247: protect MDI control register manipulations
 * 2009/06/01 - Andreas Mohr <andi at lisas dot de>
 *      - add clean lowlevel I/O emulation for cards with MII-lacking PHYs
 */

#define pr_fmt(fmt)

#include <linux/hardirq.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/mii.h>
#include <linux/if_vlan.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <linux/string.h>
#include <linux/firmware.h>
#include <linux/rtnetlink.h>
#include <linux/unaligned.h>


#define DRV_NAME
#define DRV_DESCRIPTION
#define DRV_COPYRIGHT

#define E100_WATCHDOG_PERIOD
#define E100_NAPI_WEIGHT

#define FIRMWARE_D101M
#define FIRMWARE_D101S
#define FIRMWARE_D102E

MODULE_DESCRIPTION();
MODULE_LICENSE();
MODULE_FIRMWARE();
MODULE_FIRMWARE();
MODULE_FIRMWARE();

static int debug =;
static int eeprom_bad_csum_allow =;
static int use_io =;
module_param(debug, int, 0);
module_param(eeprom_bad_csum_allow, int, 0444);
module_param(use_io, int, 0444);
MODULE_PARM_DESC();
MODULE_PARM_DESC();
MODULE_PARM_DESC();

#define INTEL_8255X_ETHERNET_DEVICE(device_id, ich)
static const struct pci_device_id e100_id_table[] =;
MODULE_DEVICE_TABLE(pci, e100_id_table);

enum mac {};

enum phy {};

/* CSR (Control/Status Registers) */
struct csr {};

enum scb_status {};

enum ru_state  {};

enum scb_stat_ack {};

enum scb_cmd_hi {};

enum scb_cmd_lo {};

enum cuc_dump {};

enum port {};

enum eeprom_ctrl_lo {};

enum mdi_ctrl {};

enum eeprom_op {};

enum eeprom_offsets {};

enum eeprom_cnfg_mdix {};

enum eeprom_phy_iface {};

enum eeprom_id {};

enum eeprom_config_asf {};

enum cb_status {};

/*
 * cb_command - Command Block flags
 * @cb_tx_nc:  0: controller does CRC (normal),  1: CRC from skb memory
 */
enum cb_command {};

struct rfd {};

struct rx {};

#if defined(__BIG_ENDIAN_BITFIELD)
#define X
#else
#define X(a,b)
#endif
struct config {};

#define E100_MAX_MULTICAST_ADDRS
struct multi {};

/* Important: keep total struct u32-aligned */
#define UCODE_SIZE
struct cb {};

enum loopback {};

struct stats {};

struct mem {};

struct param_range {};

struct params {};

struct nic {};

static inline void e100_write_flush(struct nic *nic)
{}

static void e100_enable_irq(struct nic *nic)
{}

static void e100_disable_irq(struct nic *nic)
{}

static void e100_hw_reset(struct nic *nic)
{}

static int e100_self_test(struct nic *nic)
{}

static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, __le16 data)
{
	u32 cmd_addr_data[3];
	u8 ctrl;
	int i, j;

	/* Three cmds: write/erase enable, write data, write/erase disable */
	cmd_addr_data[0] = op_ewen << (addr_len - 2);
	cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
		le16_to_cpu(data);
	cmd_addr_data[2] = op_ewds << (addr_len - 2);

	/* Bit-bang cmds to write word to eeprom */
	for (j = 0; j < 3; j++) {

		/* Chip select */
		iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
		e100_write_flush(nic); udelay(4);

		for (i = 31; i >= 0; i--) {
			ctrl = (cmd_addr_data[j] & (1 << i)) ?
				eecs | eedi : eecs;
			iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
			e100_write_flush(nic); udelay(4);

			iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
			e100_write_flush(nic); udelay(4);
		}
		/* Wait 10 msec for cmd to complete */
		msleep(10);

		/* Chip deselect */
		iowrite8(0, &nic->csr->eeprom_ctrl_lo);
		e100_write_flush(nic); udelay(4);
	}
};

/* General technique stolen from the eepro100 driver - very clever */
static __le16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
{
	u32 cmd_addr_data;
	u16 data = 0;
	u8 ctrl;
	int i;

	cmd_addr_data = ((op_read << *addr_len) | addr) << 16;

	/* Chip select */
	iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
	e100_write_flush(nic); udelay(4);

	/* Bit-bang to read word from eeprom */
	for (i = 31; i >= 0; i--) {
		ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
		iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
		e100_write_flush(nic); udelay(4);

		iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
		e100_write_flush(nic); udelay(4);

		/* Eeprom drives a dummy zero to EEDO after receiving
		 * complete address.  Use this to adjust addr_len. */
		ctrl = ioread8(&nic->csr->eeprom_ctrl_lo);
		if (!(ctrl & eedo) && i > 16) {
			*addr_len -= (i - 16);
			i = 17;
		}

		data = (data << 1) | (ctrl & eedo ? 1 : 0);
	}

	/* Chip deselect */
	iowrite8(0, &nic->csr->eeprom_ctrl_lo);
	e100_write_flush(nic); udelay(4);

	return cpu_to_le16(data);
};

/* Load entire EEPROM image into driver cache and validate checksum */
static int e100_eeprom_load(struct nic *nic)
{}

/* Save (portion of) driver EEPROM cache to device and update checksum */
static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
{}

#define E100_WAIT_SCB_TIMEOUT
#define E100_WAIT_SCB_FAST
static int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
{}

static int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
	int (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
{}

static int mdio_read(struct net_device *netdev, int addr, int reg)
{}

static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
{}

/* the standard mdio_ctrl() function for usual MII-compliant hardware */
static u16 mdio_ctrl_hw(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
{}

/* slightly tweaked mdio_ctrl() function for phy_82552_v specifics */
static u16 mdio_ctrl_phy_82552_v(struct nic *nic,
				 u32 addr,
				 u32 dir,
				 u32 reg,
				 u16 data)
{}

/* Fully software-emulated mdio_ctrl() function for cards without
 * MII-compliant PHYs.
 * For now, this is mainly geared towards 80c24 support; in case of further
 * requirements for other types (i82503, ...?) either extend this mechanism
 * or split it, whichever is cleaner.
 */
static u16 mdio_ctrl_phy_mii_emulated(struct nic *nic,
				      u32 addr,
				      u32 dir,
				      u32 reg,
				      u16 data)
{}
static inline int e100_phy_supports_mii(struct nic *nic)
{}

static void e100_get_defaults(struct nic *nic)
{}

static int e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
{}

/*************************************************************************
*  CPUSaver parameters
*
*  All CPUSaver parameters are 16-bit literals that are part of a
*  "move immediate value" instruction.  By changing the value of
*  the literal in the instruction before the code is loaded, the
*  driver can change the algorithm.
*
*  INTDELAY - This loads the dead-man timer with its initial value.
*    When this timer expires the interrupt is asserted, and the
*    timer is reset each time a new packet is received.  (see
*    BUNDLEMAX below to set the limit on number of chained packets)
*    The current default is 0x600 or 1536.  Experiments show that
*    the value should probably stay within the 0x200 - 0x1000.
*
*  BUNDLEMAX -
*    This sets the maximum number of frames that will be bundled.  In
*    some situations, such as the TCP windowing algorithm, it may be
*    better to limit the growth of the bundle size than let it go as
*    high as it can, because that could cause too much added latency.
*    The default is six, because this is the number of packets in the
*    default TCP window size.  A value of 1 would make CPUSaver indicate
*    an interrupt for every frame received.  If you do not want to put
*    a limit on the bundle size, set this value to xFFFF.
*
*  BUNDLESMALL -
*    This contains a bit-mask describing the minimum size frame that
*    will be bundled.  The default masks the lower 7 bits, which means
*    that any frame less than 128 bytes in length will not be bundled,
*    but will instead immediately generate an interrupt.  This does
*    not affect the current bundle in any way.  Any frame that is 128
*    bytes or large will be bundled normally.  This feature is meant
*    to provide immediate indication of ACK frames in a TCP environment.
*    Customers were seeing poor performance when a machine with CPUSaver
*    enabled was sending but not receiving.  The delay introduced when
*    the ACKs were received was enough to reduce total throughput, because
*    the sender would sit idle until the ACK was finally seen.
*
*    The current default is 0xFF80, which masks out the lower 7 bits.
*    This means that any frame which is x7F (127) bytes or smaller
*    will cause an immediate interrupt.  Because this value must be a
*    bit mask, there are only a few valid values that can be used.  To
*    turn this feature off, the driver can write the value xFFFF to the
*    lower word of this instruction (in the same way that the other
*    parameters are used).  Likewise, a value of 0xF800 (2047) would
*    cause an interrupt to be generated for every frame, because all
*    standard Ethernet frames are <= 2047 bytes in length.
*************************************************************************/

/* if you wish to disable the ucode functionality, while maintaining the
 * workarounds it provides, set the following defines to:
 * BUNDLESMALL 0
 * BUNDLEMAX 1
 * INTDELAY 1
 */
#define BUNDLESMALL
#define BUNDLEMAX
#define INTDELAY

/* Initialize firmware */
static const struct firmware *e100_request_firmware(struct nic *nic)
{}

static int e100_setup_ucode(struct nic *nic, struct cb *cb,
			     struct sk_buff *skb)
{}

static inline int e100_load_ucode_wait(struct nic *nic)
{}

static int e100_setup_iaaddr(struct nic *nic, struct cb *cb,
	struct sk_buff *skb)
{}

static int e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
{}

static int e100_phy_check_without_mii(struct nic *nic)
{}

#define NCONFIG_AUTO_SWITCH
#define MII_NSC_CONG
#define NSC_CONG_ENABLE
#define NSC_CONG_TXREADY
static int e100_phy_init(struct nic *nic)
{}

static int e100_hw_init(struct nic *nic)
{}

static int e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
{}

static void e100_set_multicast_list(struct net_device *netdev)
{}

static void e100_update_stats(struct nic *nic)
{}

static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
{}

static void e100_watchdog(struct timer_list *t)
{}

static int e100_xmit_prepare(struct nic *nic, struct cb *cb,
	struct sk_buff *skb)
{}

static netdev_tx_t e100_xmit_frame(struct sk_buff *skb,
				   struct net_device *netdev)
{}

static int e100_tx_clean(struct nic *nic)
{}

static void e100_clean_cbs(struct nic *nic)
{}

static int e100_alloc_cbs(struct nic *nic)
{}

static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
{}

#define RFD_BUF_LEN
static int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
{}

static int e100_rx_indicate(struct nic *nic, struct rx *rx,
	unsigned int *work_done, unsigned int work_to_do)
{}

static void e100_rx_clean(struct nic *nic, unsigned int *work_done,
	unsigned int work_to_do)
{}

static void e100_rx_clean_list(struct nic *nic)
{}

static int e100_rx_alloc_list(struct nic *nic)
{}

static irqreturn_t e100_intr(int irq, void *dev_id)
{}

static int e100_poll(struct napi_struct *napi, int budget)
{}

#ifdef CONFIG_NET_POLL_CONTROLLER
static void e100_netpoll(struct net_device *netdev)
{}
#endif

static int e100_set_mac_address(struct net_device *netdev, void *p)
{}

static int e100_asf(struct nic *nic)
{}

static int e100_up(struct nic *nic)
{}

static void e100_down(struct nic *nic)
{}

static void e100_tx_timeout(struct net_device *netdev, unsigned int txqueue)
{}

static void e100_tx_timeout_task(struct work_struct *work)
{}

static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
{}

#define MII_LED_CONTROL
#define E100_82552_LED_OVERRIDE
#define E100_82552_LED_ON
#define E100_82552_LED_OFF

static int e100_get_link_ksettings(struct net_device *netdev,
				   struct ethtool_link_ksettings *cmd)
{}

static int e100_set_link_ksettings(struct net_device *netdev,
				   const struct ethtool_link_ksettings *cmd)
{}

static void e100_get_drvinfo(struct net_device *netdev,
	struct ethtool_drvinfo *info)
{}

#define E100_PHY_REGS
static int e100_get_regs_len(struct net_device *netdev)
{}

static void e100_get_regs(struct net_device *netdev,
	struct ethtool_regs *regs, void *p)
{}

static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
{}

static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
{}

static u32 e100_get_msglevel(struct net_device *netdev)
{}

static void e100_set_msglevel(struct net_device *netdev, u32 value)
{}

static int e100_nway_reset(struct net_device *netdev)
{}

static u32 e100_get_link(struct net_device *netdev)
{}

static int e100_get_eeprom_len(struct net_device *netdev)
{}

#define E100_EEPROM_MAGIC
static int e100_get_eeprom(struct net_device *netdev,
	struct ethtool_eeprom *eeprom, u8 *bytes)
{}

static int e100_set_eeprom(struct net_device *netdev,
	struct ethtool_eeprom *eeprom, u8 *bytes)
{}

static void e100_get_ringparam(struct net_device *netdev,
			       struct ethtool_ringparam *ring,
			       struct kernel_ethtool_ringparam *kernel_ring,
			       struct netlink_ext_ack *extack)
{}

static int e100_set_ringparam(struct net_device *netdev,
			      struct ethtool_ringparam *ring,
			      struct kernel_ethtool_ringparam *kernel_ring,
			      struct netlink_ext_ack *extack)
{}

static const char e100_gstrings_test[][ETH_GSTRING_LEN] =;
#define E100_TEST_LEN

static void e100_diag_test(struct net_device *netdev,
	struct ethtool_test *test, u64 *data)
{}

static int e100_set_phys_id(struct net_device *netdev,
			    enum ethtool_phys_id_state state)
{}

static const char e100_gstrings_stats[][ETH_GSTRING_LEN] =;
#define E100_NET_STATS_LEN
#define E100_STATS_LEN

static int e100_get_sset_count(struct net_device *netdev, int sset)
{}

static void e100_get_ethtool_stats(struct net_device *netdev,
	struct ethtool_stats *stats, u64 *data)
{}

static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
{}

static const struct ethtool_ops e100_ethtool_ops =;

static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
{}

static int e100_alloc(struct nic *nic)
{}

static void e100_free(struct nic *nic)
{}

static int e100_open(struct net_device *netdev)
{}

static int e100_close(struct net_device *netdev)
{}

static int e100_set_features(struct net_device *netdev,
			     netdev_features_t features)
{}

static const struct net_device_ops e100_netdev_ops =;

static int e100_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{}

static void e100_remove(struct pci_dev *pdev)
{}

#define E100_82552_SMARTSPEED
#define E100_82552_REV_ANEG
#define E100_82552_ANEG_NOW
static void __e100_shutdown(struct pci_dev *pdev, bool *enable_wake)
{}

static int __e100_power_off(struct pci_dev *pdev, bool wake)
{}

static int e100_suspend(struct device *dev_d)
{}

static int e100_resume(struct device *dev_d)
{}

static void e100_shutdown(struct pci_dev *pdev)
{}

/* ------------------ PCI Error Recovery infrastructure  -------------- */
/**
 * e100_io_error_detected - called when PCI error is detected.
 * @pdev: Pointer to PCI device
 * @state: The current pci connection state
 */
static pci_ers_result_t e100_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
{}

/**
 * e100_io_slot_reset - called after the pci bus has been reset.
 * @pdev: Pointer to PCI device
 *
 * Restart the card from scratch.
 */
static pci_ers_result_t e100_io_slot_reset(struct pci_dev *pdev)
{}

/**
 * e100_io_resume - resume normal operations
 * @pdev: Pointer to PCI device
 *
 * Resume normal operations after an error recovery
 * sequence has been completed.
 */
static void e100_io_resume(struct pci_dev *pdev)
{}

static const struct pci_error_handlers e100_err_handler =;

static DEFINE_SIMPLE_DEV_PM_OPS(e100_pm_ops, e100_suspend, e100_resume);

static struct pci_driver e100_driver =;

static int __init e100_init_module(void)
{}

static void __exit e100_cleanup_module(void)
{}

module_init();
module_exit(e100_cleanup_module);