e100.c

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

C
2,332 行
字号
		nic->flags |= wol_magic;	else		nic->flags &= ~wol_magic;	pci_enable_wake(nic->pdev, 0, nic->flags & (wol_magic | e100_asf(nic)));	e100_exec_cb(nic, NULL, e100_configure);	return 0;}static u32 e100_get_msglevel(struct net_device *netdev){	struct nic *nic = netdev_priv(netdev);	return nic->msg_enable;}static void e100_set_msglevel(struct net_device *netdev, u32 value){	struct nic *nic = netdev_priv(netdev);	nic->msg_enable = value;}static int e100_nway_reset(struct net_device *netdev){	struct nic *nic = netdev_priv(netdev);	return mii_nway_restart(&nic->mii);}static u32 e100_get_link(struct net_device *netdev){	struct nic *nic = netdev_priv(netdev);	return mii_link_ok(&nic->mii);}static int e100_get_eeprom_len(struct net_device *netdev){	struct nic *nic = netdev_priv(netdev);	return nic->eeprom_wc << 1;}#define E100_EEPROM_MAGIC	0x1234static int e100_get_eeprom(struct net_device *netdev,	struct ethtool_eeprom *eeprom, u8 *bytes){	struct nic *nic = netdev_priv(netdev);	eeprom->magic = E100_EEPROM_MAGIC;	memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);	return 0;}static int e100_set_eeprom(struct net_device *netdev,	struct ethtool_eeprom *eeprom, u8 *bytes){	struct nic *nic = netdev_priv(netdev);	if(eeprom->magic != E100_EEPROM_MAGIC)		return -EINVAL;	memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);	return e100_eeprom_save(nic, eeprom->offset >> 1,		(eeprom->len >> 1) + 1);}static void e100_get_ringparam(struct net_device *netdev,	struct ethtool_ringparam *ring){	struct nic *nic = netdev_priv(netdev);	struct param_range *rfds = &nic->params.rfds;	struct param_range *cbs = &nic->params.cbs;	ring->rx_max_pending = rfds->max;	ring->tx_max_pending = cbs->max;	ring->rx_mini_max_pending = 0;	ring->rx_jumbo_max_pending = 0;	ring->rx_pending = rfds->count;	ring->tx_pending = cbs->count;	ring->rx_mini_pending = 0;	ring->rx_jumbo_pending = 0;}static int e100_set_ringparam(struct net_device *netdev,	struct ethtool_ringparam *ring){	struct nic *nic = netdev_priv(netdev);	struct param_range *rfds = &nic->params.rfds;	struct param_range *cbs = &nic->params.cbs;	if(netif_running(netdev))		e100_down(nic);	rfds->count = max(ring->rx_pending, rfds->min);	rfds->count = min(rfds->count, rfds->max);	cbs->count = max(ring->tx_pending, cbs->min);	cbs->count = min(cbs->count, cbs->max);	if(netif_running(netdev))		e100_up(nic);	return 0;}static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {	"Link test     (on/offline)",	"Eeprom test   (on/offline)",	"Self test        (offline)",	"Mac loopback     (offline)",	"Phy loopback     (offline)",};#define E100_TEST_LEN	sizeof(e100_gstrings_test) / ETH_GSTRING_LENstatic int e100_diag_test_count(struct net_device *netdev){	return E100_TEST_LEN;}static void e100_diag_test(struct net_device *netdev,	struct ethtool_test *test, u64 *data){	struct ethtool_cmd cmd;	struct nic *nic = netdev_priv(netdev);	int i, err;	memset(data, 0, E100_TEST_LEN * sizeof(u64));	data[0] = !mii_link_ok(&nic->mii);	data[1] = e100_eeprom_load(nic);	if(test->flags & ETH_TEST_FL_OFFLINE) {		/* save speed, duplex & autoneg settings */		err = mii_ethtool_gset(&nic->mii, &cmd);		if(netif_running(netdev))			e100_down(nic);		data[2] = e100_self_test(nic);		data[3] = e100_loopback_test(nic, lb_mac);		data[4] = e100_loopback_test(nic, lb_phy);		/* restore speed, duplex & autoneg settings */		err = mii_ethtool_sset(&nic->mii, &cmd);		if(netif_running(netdev))			e100_up(nic);	}	for(i = 0; i < E100_TEST_LEN; i++)		test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;}static int e100_phys_id(struct net_device *netdev, u32 data){	struct nic *nic = netdev_priv(netdev);	if(!data || data > (u32)(MAX_SCHEDULE_TIMEOUT / HZ))		data = (u32)(MAX_SCHEDULE_TIMEOUT / HZ);	mod_timer(&nic->blink_timer, jiffies);	set_current_state(TASK_INTERRUPTIBLE);	schedule_timeout(data * HZ);	del_timer_sync(&nic->blink_timer);	mdio_write(netdev, nic->mii.phy_id, MII_LED_CONTROL, 0);	return 0;}static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {	"rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",	"tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",	"rx_length_errors", "rx_over_errors", "rx_crc_errors",	"rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",	"tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",	"tx_heartbeat_errors", "tx_window_errors",	/* device-specific stats */	"tx_deferred", "tx_single_collisions", "tx_multi_collisions",	"tx_flow_control_pause", "rx_flow_control_pause",	"rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",};#define E100_NET_STATS_LEN	21#define E100_STATS_LEN	sizeof(e100_gstrings_stats) / ETH_GSTRING_LENstatic int e100_get_stats_count(struct net_device *netdev){	return E100_STATS_LEN;}static void e100_get_ethtool_stats(struct net_device *netdev,	struct ethtool_stats *stats, u64 *data){	struct nic *nic = netdev_priv(netdev);	int i;	for(i = 0; i < E100_NET_STATS_LEN; i++)		data[i] = ((unsigned long *)&nic->net_stats)[i];	data[i++] = nic->tx_deferred;	data[i++] = nic->tx_single_collisions;	data[i++] = nic->tx_multiple_collisions;	data[i++] = nic->tx_fc_pause;	data[i++] = nic->rx_fc_pause;	data[i++] = nic->rx_fc_unsupported;	data[i++] = nic->tx_tco_frames;	data[i++] = nic->rx_tco_frames;}static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data){	switch(stringset) {	case ETH_SS_TEST:		memcpy(data, *e100_gstrings_test, sizeof(e100_gstrings_test));		break;	case ETH_SS_STATS:		memcpy(data, *e100_gstrings_stats, sizeof(e100_gstrings_stats));		break;	}}static struct ethtool_ops e100_ethtool_ops = {	.get_settings		= e100_get_settings,	.set_settings		= e100_set_settings,	.get_drvinfo		= e100_get_drvinfo,	.get_regs_len		= e100_get_regs_len,	.get_regs		= e100_get_regs,	.get_wol		= e100_get_wol,	.set_wol		= e100_set_wol,	.get_msglevel		= e100_get_msglevel,	.set_msglevel		= e100_set_msglevel,	.nway_reset		= e100_nway_reset,	.get_link		= e100_get_link,	.get_eeprom_len		= e100_get_eeprom_len,	.get_eeprom		= e100_get_eeprom,	.set_eeprom		= e100_set_eeprom,	.get_ringparam		= e100_get_ringparam,	.set_ringparam		= e100_set_ringparam,	.self_test_count	= e100_diag_test_count,	.self_test		= e100_diag_test,	.get_strings		= e100_get_strings,	.phys_id		= e100_phys_id,	.get_stats_count	= e100_get_stats_count,	.get_ethtool_stats	= e100_get_ethtool_stats,};static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd){	struct nic *nic = netdev_priv(netdev);	return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);}static int e100_alloc(struct nic *nic){	nic->mem = pci_alloc_consistent(nic->pdev, sizeof(struct mem),		&nic->dma_addr);	return nic->mem ? 0 : -ENOMEM;}static void e100_free(struct nic *nic){	if(nic->mem) {		pci_free_consistent(nic->pdev, sizeof(struct mem),			nic->mem, nic->dma_addr);		nic->mem = NULL;	}}static int e100_open(struct net_device *netdev){	struct nic *nic = netdev_priv(netdev);	int err = 0;	netif_carrier_off(netdev);	if((err = e100_up(nic)))		DPRINTK(IFUP, ERR, "Cannot open interface, aborting.\n");	return err;}static int e100_close(struct net_device *netdev){	e100_down(netdev_priv(netdev));	return 0;}static int __devinit e100_probe(struct pci_dev *pdev,	const struct pci_device_id *ent){	struct net_device *netdev;	struct nic *nic;	int err;	if(!(netdev = alloc_etherdev(sizeof(struct nic)))) {		if(((1 << debug) - 1) & NETIF_MSG_PROBE)			printk(KERN_ERR PFX "Etherdev alloc failed, abort.\n");		return -ENOMEM;	}	netdev->open = e100_open;	netdev->stop = e100_close;	netdev->hard_start_xmit = e100_xmit_frame;	netdev->get_stats = e100_get_stats;	netdev->set_multicast_list = e100_set_multicast_list;	netdev->set_mac_address = e100_set_mac_address;	netdev->change_mtu = e100_change_mtu;	netdev->do_ioctl = e100_do_ioctl;	SET_ETHTOOL_OPS(netdev, &e100_ethtool_ops);	netdev->tx_timeout = e100_tx_timeout;	netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;	netdev->poll = e100_poll;	netdev->weight = E100_NAPI_WEIGHT;#ifdef CONFIG_NET_POLL_CONTROLLER	netdev->poll_controller = e100_netpoll;#endif	nic = netdev_priv(netdev);	nic->netdev = netdev;	nic->pdev = pdev;	nic->msg_enable = (1 << debug) - 1;	pci_set_drvdata(pdev, netdev);	if((err = pci_enable_device(pdev))) {		DPRINTK(PROBE, ERR, "Cannot enable PCI device, aborting.\n");		goto err_out_free_dev;	}	if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {		DPRINTK(PROBE, ERR, "Cannot find proper PCI device "			"base address, aborting.\n");		err = -ENODEV;		goto err_out_disable_pdev;	}	if((err = pci_request_regions(pdev, DRV_NAME))) {		DPRINTK(PROBE, ERR, "Cannot obtain PCI resources, aborting.\n");		goto err_out_disable_pdev;	}	pci_set_master(pdev);	if((err = pci_set_dma_mask(pdev, 0xFFFFFFFFULL))) {		DPRINTK(PROBE, ERR, "No usable DMA configuration, aborting.\n");		goto err_out_free_res;	}	SET_MODULE_OWNER(netdev);	SET_NETDEV_DEV(netdev, &pdev->dev);	nic->csr = ioremap(pci_resource_start(pdev, 0), sizeof(struct csr));	if(!nic->csr) {		DPRINTK(PROBE, ERR, "Cannot map device registers, aborting.\n");		err = -ENOMEM;		goto err_out_free_res;	}	if(ent->driver_data)		nic->flags |= ich;	else		nic->flags &= ~ich;	spin_lock_init(&nic->cb_lock);	spin_lock_init(&nic->cmd_lock);	init_timer(&nic->watchdog);	nic->watchdog.function = e100_watchdog;	nic->watchdog.data = (unsigned long)nic;	init_timer(&nic->blink_timer);	nic->blink_timer.function = e100_blink_led;	nic->blink_timer.data = (unsigned long)nic;	if((err = e100_alloc(nic))) {		DPRINTK(PROBE, ERR, "Cannot alloc driver memory, aborting.\n");		goto err_out_iounmap;	}	e100_get_defaults(nic);	e100_hw_reset(nic);	e100_phy_init(nic);	if((err = e100_eeprom_load(nic)))		goto err_out_free;	memcpy(netdev->dev_addr, nic->eeprom, ETH_ALEN);	if(!is_valid_ether_addr(netdev->dev_addr)) {		DPRINTK(PROBE, ERR, "Invalid MAC address from "			"EEPROM, aborting.\n");		err = -EAGAIN;		goto err_out_free;	}	/* Wol magic packet can be enabled from eeprom */	if((nic->mac >= mac_82558_D101_A4) &&	   (nic->eeprom[eeprom_id] & eeprom_id_wol))		nic->flags |= wol_magic;	pci_enable_wake(pdev, 0, nic->flags & (wol_magic | e100_asf(nic)));	if((err = register_netdev(netdev))) {		DPRINTK(PROBE, ERR, "Cannot register net device, aborting.\n");		goto err_out_free;	}	DPRINTK(PROBE, INFO, "addr 0x%lx, irq %d, "		"MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n",		pci_resource_start(pdev, 0), pdev->irq,		netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],		netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);	return 0;err_out_free:	e100_free(nic);err_out_iounmap:	iounmap(nic->csr);err_out_free_res:	pci_release_regions(pdev);err_out_disable_pdev:	pci_disable_device(pdev);err_out_free_dev:	pci_set_drvdata(pdev, NULL);	free_netdev(netdev);	return err;}static void __devexit e100_remove(struct pci_dev *pdev){	struct net_device *netdev = pci_get_drvdata(pdev);	if(netdev) {		struct nic *nic = netdev_priv(netdev);		unregister_netdev(netdev);		e100_free(nic);		iounmap(nic->csr);		free_netdev(netdev);		pci_release_regions(pdev);		pci_disable_device(pdev);		pci_set_drvdata(pdev, NULL);	}}#ifdef CONFIG_PMstatic int e100_suspend(struct pci_dev *pdev, u32 state){	struct net_device *netdev = pci_get_drvdata(pdev);	struct nic *nic = netdev_priv(netdev);	if(netif_running(netdev))		e100_down(nic);	e100_hw_reset(nic);	netif_device_detach(netdev);	pci_save_state(pdev, nic->pm_state);	pci_enable_wake(pdev, state, nic->flags & (wol_magic | e100_asf(nic)));	pci_disable_device(pdev);	pci_set_power_state(pdev, state);	return 0;}static int e100_resume(struct pci_dev *pdev){	struct net_device *netdev = pci_get_drvdata(pdev);	struct nic *nic = netdev_priv(netdev);	pci_set_power_state(pdev, 0);	pci_restore_state(pdev, nic->pm_state);	e100_hw_init(nic);	netif_device_attach(netdev);	if(netif_running(netdev))		e100

⌨️ 快捷键说明

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