e100.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,332 行 · 第 1/5 页

C
2,332 行
字号
};struct stats {	u32 tx_good_frames, tx_max_collisions, tx_late_collisions,		tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,		tx_multiple_collisions, tx_total_collisions;	u32 rx_good_frames, rx_crc_errors, rx_alignment_errors,		rx_resource_errors, rx_overrun_errors, rx_cdt_errors,		rx_short_frame_errors;	u32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;	u16 xmt_tco_frames, rcv_tco_frames;	u32 complete;};struct mem {	struct {		u32 signature;		u32 result;	} selftest;	struct stats stats;	u8 dump_buf[596];};struct param_range {	u32 min;	u32 max;	u32 count;};struct params {	struct param_range rfds;	struct param_range cbs;};struct nic {	/* Begin: frequently used values: keep adjacent for cache effect */	u32 msg_enable				____cacheline_aligned;	struct net_device *netdev;	struct pci_dev *pdev;	struct rx *rxs				____cacheline_aligned;	struct rx *rx_to_use;	struct rx *rx_to_clean;	struct rfd blank_rfd;	int ru_running;	spinlock_t cb_lock			____cacheline_aligned;	spinlock_t cmd_lock;	struct csr *csr;	enum scb_cmd_lo cuc_cmd;	unsigned int cbs_avail;	struct cb *cbs;	struct cb *cb_to_use;	struct cb *cb_to_send;	struct cb *cb_to_clean;	u16 tx_command;	/* End: frequently used values: keep adjacent for cache effect */	enum {		ich                = (1 << 0),		promiscuous        = (1 << 1),		multicast_all      = (1 << 2),		wol_magic          = (1 << 3),		ich_10h_workaround = (1 << 4),	} flags					____cacheline_aligned;	enum mac mac;	enum phy phy;	struct params params;	struct net_device_stats net_stats;	struct timer_list watchdog;	struct timer_list blink_timer;	struct mii_if_info mii;	enum loopback loopback;	struct mem *mem;	dma_addr_t dma_addr;	dma_addr_t cbs_dma_addr;	u8 adaptive_ifs;	u8 tx_threshold;	u32 tx_frames;	u32 tx_collisions;	u32 tx_deferred;	u32 tx_single_collisions;	u32 tx_multiple_collisions;	u32 tx_fc_pause;	u32 tx_tco_frames;	u32 rx_fc_pause;	u32 rx_fc_unsupported;	u32 rx_tco_frames;	u32 rx_over_length_errors;	u8 rev_id;	u16 leds;	u16 eeprom_wc;	u16 eeprom[256];	u32 pm_state[16];};static inline void e100_write_flush(struct nic *nic){	/* Flush previous PCI writes through intermediate bridges	 * by doing a benign read */	(void)readb(&nic->csr->scb.status);}static inline void e100_enable_irq(struct nic *nic){	writeb(irq_mask_none, &nic->csr->scb.cmd_hi);	e100_write_flush(nic);}static inline void e100_disable_irq(struct nic *nic){	writeb(irq_mask_all, &nic->csr->scb.cmd_hi);	e100_write_flush(nic);}static void e100_hw_reset(struct nic *nic){	/* Put CU and RU into idle with a selective reset to get	 * device off of PCI bus */	writel(selective_reset, &nic->csr->port);	e100_write_flush(nic); udelay(20);	/* Now fully reset device */	writel(software_reset, &nic->csr->port);	e100_write_flush(nic); udelay(20);	/* TCO workaround - 82559 and greater */	if(nic->mac >= mac_82559_D101M) {		/* Issue a redundant CU load base without setting		 * general pointer, and without waiting for scb to		 * clear.  This gets us into post-driver.  Finally,		 * wait 20 msec for reset to take effect. */		writeb(cuc_load_base, &nic->csr->scb.cmd_lo);		mdelay(20);	}	/* Mask off our interrupt line - it's unmasked after reset */	e100_disable_irq(nic);}static int e100_self_test(struct nic *nic){	u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);	/* Passing the self-test is a pretty good indication	 * that the device can DMA to/from host memory */	nic->mem->selftest.signature = 0;	nic->mem->selftest.result = 0xFFFFFFFF;	writel(selftest | dma_addr, &nic->csr->port);	e100_write_flush(nic);	/* Wait 10 msec for self-test to complete */	set_current_state(TASK_UNINTERRUPTIBLE);	schedule_timeout(HZ / 100 + 1);	/* Interrupts are enabled after self-test */	e100_disable_irq(nic);	/* Check results of self-test */	if(nic->mem->selftest.result != 0) {		DPRINTK(HW, ERR, "Self-test failed: result=0x%08X\n",			nic->mem->selftest.result);		return -ETIMEDOUT;	}	if(nic->mem->selftest.signature == 0) {		DPRINTK(HW, ERR, "Self-test failed: timed out\n");		return -ETIMEDOUT;	}	return 0;}static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, u16 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) |		cpu_to_le16(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 */		writeb(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;			writeb(ctrl, &nic->csr->eeprom_ctrl_lo);			e100_write_flush(nic); udelay(4);			writeb(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);			e100_write_flush(nic); udelay(4);		}		/* Wait 10 msec for cmd to complete */		set_current_state(TASK_UNINTERRUPTIBLE);		schedule_timeout(HZ / 100 + 1);		/* Chip deselect */		writeb(0, &nic->csr->eeprom_ctrl_lo);		e100_write_flush(nic); udelay(4);	}};/* General technique stolen from the eepro100 driver - very clever */static u16 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 */	writeb(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;		writeb(ctrl, &nic->csr->eeprom_ctrl_lo);		e100_write_flush(nic); udelay(4);				writeb(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 = readb(&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 */	writeb(0, &nic->csr->eeprom_ctrl_lo);	e100_write_flush(nic); udelay(4);	return le16_to_cpu(data);};/* Load entire EEPROM image into driver cache and validate checksum */static int e100_eeprom_load(struct nic *nic){	u16 addr, addr_len = 8, checksum = 0;	/* Try reading with an 8-bit addr len to discover actual addr len */	e100_eeprom_read(nic, &addr_len, 0);	nic->eeprom_wc = 1 << addr_len;	for(addr = 0; addr < nic->eeprom_wc; addr++) {		nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);		if(addr < nic->eeprom_wc - 1)			checksum += cpu_to_le16(nic->eeprom[addr]);	}	/* The checksum, stored in the last word, is calculated such that	 * the sum of words should be 0xBABA */	checksum = le16_to_cpu(0xBABA - checksum);	if(checksum != nic->eeprom[nic->eeprom_wc - 1]) {		DPRINTK(PROBE, ERR, "EEPROM corrupted\n");		return -EAGAIN;	}	return 0;}/* Save (portion of) driver EEPROM cache to device and update checksum */static int e100_eeprom_save(struct nic *nic, u16 start, u16 count){	u16 addr, addr_len = 8, checksum = 0;	/* Try reading with an 8-bit addr len to discover actual addr len */	e100_eeprom_read(nic, &addr_len, 0);	nic->eeprom_wc = 1 << addr_len;	if(start + count >= nic->eeprom_wc)		return -EINVAL;	for(addr = start; addr < start + count; addr++)		e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);	/* The checksum, stored in the last word, is calculated such that	 * the sum of words should be 0xBABA */	for(addr = 0; addr < nic->eeprom_wc - 1; addr++)		checksum += cpu_to_le16(nic->eeprom[addr]);	nic->eeprom[nic->eeprom_wc - 1] = le16_to_cpu(0xBABA - checksum);	e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,		nic->eeprom[nic->eeprom_wc - 1]);	return 0;}#define E100_WAIT_SCB_TIMEOUT 40static inline int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr){	unsigned long flags;	unsigned int i;	int err = 0;	spin_lock_irqsave(&nic->cmd_lock, flags);	/* Previous command is accepted when SCB clears */	for(i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {		if(likely(!readb(&nic->csr->scb.cmd_lo)))			break;		cpu_relax();		if(unlikely(i > (E100_WAIT_SCB_TIMEOUT >> 1)))			udelay(5);	}	if(unlikely(i == E100_WAIT_SCB_TIMEOUT)) {		err = -EAGAIN;		goto err_unlock;	}	if(unlikely(cmd != cuc_resume))		writel(dma_addr, &nic->csr->scb.gen_ptr);	writeb(cmd, &nic->csr->scb.cmd_lo);err_unlock:	spin_unlock_irqrestore(&nic->cmd_lock, flags);	return err;}static inline int e100_exec_cb(struct nic *nic, struct sk_buff *skb,	void (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *)){	struct cb *cb;	unsigned long flags;	int err = 0;	spin_lock_irqsave(&nic->cb_lock, flags);	if(unlikely(!nic->cbs_avail)) {		err = -ENOMEM;		goto err_unlock;	}	cb = nic->cb_to_use;	nic->cb_to_use = cb->next;	nic->cbs_avail--;	cb->skb = skb;	if(unlikely(!nic->cbs_avail))		err = -ENOSPC;	cb_prepare(nic, cb, skb);	/* Order is important otherwise we'll be in a race with h/w:	 * set S-bit in current first, then clear S-bit in previous. */	cb->command |= cpu_to_le16(cb_s);	wmb();	cb->prev->command &= cpu_to_le16(~cb_s);	while(nic->cb_to_send != nic->cb_to_use) {		if(unlikely(e100_exec_cmd(nic, nic->cuc_cmd,			nic->cb_to_send->dma_addr))) {			/* Ok, here's where things get sticky.  It's			 * possible that we can't schedule the command			 * because the controller is too busy, so			 * let's just queue the command and try again			 * when another command is scheduled. */			break;		} else {			nic->cuc_cmd = cuc_resume;			nic->cb_to_send = nic->cb_to_send->next;		}	}err_unlock:	spin_unlock_irqrestore(&nic->cb_lock, flags);	return err;}static u16 mdio_ctrl(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data){	u32 data_out = 0;	unsigned int i;	writel((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);	for(i = 0; i < 100; i++) {		udelay(20);		if((data_out = readl(&nic->csr->mdi_ctrl)) & mdi_ready)			break;	}	DPRINTK(HW, DEBUG,		"%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",		dir == mdi_read ? "READ" : "WRITE", addr, reg, data, data_out);	return (u16)data_out;}static int mdio_read(struct net_device *netdev, int addr, int reg){	return mdio_ctrl(netdev_priv(netdev), addr, mdi_read, reg, 0);}static void mdio_write(struct net_device *netdev, int addr, int reg, int data){	mdio_ctrl(netdev_priv(netdev), addr, mdi_write, reg, data);}static void e100_get_defaults(struct nic *nic){	struct param_range rfds = { .min = 64, .max = 256, .count = 64 };	struct param_range cbs  = { .min = 64, .max = 256, .count = 64 };	pci_read_config_byte(nic->pdev, PCI_REVISION_ID, &nic->rev_id);	/* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */	nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->rev_id;	if(nic->mac == mac_unknown)		nic->mac = mac_82557_D100_A;	nic->params.rfds = rfds;	nic->params.cbs = cbs;	/* Quadwords to DMA into FIFO before starting frame transmit */	nic->tx_threshold = 0xE0;	nic->tx_command = cpu_to_le16(cb_tx | cb_i | cb_tx_sf |		((nic->mac >= mac_82558_D101_A4) ? cb_cid : 0));	/* Template for a freshly allocated RFD */	nic->blank_rfd.command = cpu_to_le16(cb_el);	nic->blank_rfd.rbd = 0xFFFFFFFF;	nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN);	/* MII setup */	nic->mii.phy_id_mask = 0x1F;	nic->mii.reg_num_mask = 0x1F;	nic->mii.dev = nic->netdev;	nic->mii.mdio_read = mdio_read;	nic->mii.mdio_write = mdio_write;}static void e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb){	struct config *config = &cb->u.config;	u8 *c = (u8 *)config;	cb->command = cpu_to_le16(cb_config);	memset(config, 0, sizeof(struct config));	config->byte_count = 0x16;		/* bytes in this struct */	config->rx_fifo_limit = 0x8;		/* bytes in FIFO before DMA */	config->direct_rx_dma = 0x1;		/* reserved */	config->standard_tcb = 0x1;		/* 1=standard, 0=extended */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?