e1000_main.c

来自「linux 内核源代码」· C语言 代码 · 共 2,237 行 · 第 1/5 页

C
2,237
字号
 * number of queues at compile-time.  The polling_netdev array is * intended for Multiqueue, but should work fine with a single queue. **/static int __devinite1000_alloc_queues(struct e1000_adapter *adapter){	adapter->tx_ring = kcalloc(adapter->num_tx_queues,	                           sizeof(struct e1000_tx_ring), GFP_KERNEL);	if (!adapter->tx_ring)		return -ENOMEM;	adapter->rx_ring = kcalloc(adapter->num_rx_queues,	                           sizeof(struct e1000_rx_ring), GFP_KERNEL);	if (!adapter->rx_ring) {		kfree(adapter->tx_ring);		return -ENOMEM;	}#ifdef CONFIG_E1000_NAPI	adapter->polling_netdev = kcalloc(adapter->num_rx_queues,	                                  sizeof(struct net_device),	                                  GFP_KERNEL);	if (!adapter->polling_netdev) {		kfree(adapter->tx_ring);		kfree(adapter->rx_ring);		return -ENOMEM;	}#endif	return E1000_SUCCESS;}/** * e1000_open - Called when a network interface is made active * @netdev: network interface device structure * * Returns 0 on success, negative value on failure * * The open entry point is called when a network interface is made * active by the system (IFF_UP).  At this point all resources needed * for transmit and receive operations are allocated, the interrupt * handler is registered with the OS, the watchdog timer is started, * and the stack is notified that the interface is ready. **/static inte1000_open(struct net_device *netdev){	struct e1000_adapter *adapter = netdev_priv(netdev);	int err;	/* disallow open during test */	if (test_bit(__E1000_TESTING, &adapter->flags))		return -EBUSY;	/* allocate transmit descriptors */	err = e1000_setup_all_tx_resources(adapter);	if (err)		goto err_setup_tx;	/* allocate receive descriptors */	err = e1000_setup_all_rx_resources(adapter);	if (err)		goto err_setup_rx;	e1000_power_up_phy(adapter);	adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;	if ((adapter->hw.mng_cookie.status &			  E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {		e1000_update_mng_vlan(adapter);	}	/* If AMT is enabled, let the firmware know that the network	 * interface is now open */	if (adapter->hw.mac_type == e1000_82573 &&	    e1000_check_mng_mode(&adapter->hw))		e1000_get_hw_control(adapter);	/* before we allocate an interrupt, we must be ready to handle it.	 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt	 * as soon as we call pci_request_irq, so we have to setup our	 * clean_rx handler before we do so.  */	e1000_configure(adapter);	err = e1000_request_irq(adapter);	if (err)		goto err_req_irq;	/* From here on the code is the same as e1000_up() */	clear_bit(__E1000_DOWN, &adapter->flags);#ifdef CONFIG_E1000_NAPI	napi_enable(&adapter->napi);#endif	e1000_irq_enable(adapter);	/* fire a link status change interrupt to start the watchdog */	E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_LSC);	return E1000_SUCCESS;err_req_irq:	e1000_release_hw_control(adapter);	e1000_power_down_phy(adapter);	e1000_free_all_rx_resources(adapter);err_setup_rx:	e1000_free_all_tx_resources(adapter);err_setup_tx:	e1000_reset(adapter);	return err;}/** * e1000_close - Disables a network interface * @netdev: network interface device structure * * Returns 0, this is not allowed to fail * * The close entry point is called when an interface is de-activated * by the OS.  The hardware is still under the drivers control, but * needs to be disabled.  A global MAC reset is issued to stop the * hardware, and all transmit and receive resources are freed. **/static inte1000_close(struct net_device *netdev){	struct e1000_adapter *adapter = netdev_priv(netdev);	WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));	e1000_down(adapter);	e1000_power_down_phy(adapter);	e1000_free_irq(adapter);	e1000_free_all_tx_resources(adapter);	e1000_free_all_rx_resources(adapter);	/* kill manageability vlan ID if supported, but not if a vlan with	 * the same ID is registered on the host OS (let 8021q kill it) */	if ((adapter->hw.mng_cookie.status &			  E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&	     !(adapter->vlgrp &&	       vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id))) {		e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);	}	/* If AMT is enabled, let the firmware know that the network	 * interface is now closed */	if (adapter->hw.mac_type == e1000_82573 &&	    e1000_check_mng_mode(&adapter->hw))		e1000_release_hw_control(adapter);	return 0;}/** * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary * @adapter: address of board private structure * @start: address of beginning of memory * @len: length of memory **/static boolean_te1000_check_64k_bound(struct e1000_adapter *adapter,		      void *start, unsigned long len){	unsigned long begin = (unsigned long) start;	unsigned long end = begin + len;	/* First rev 82545 and 82546 need to not allow any memory	 * write location to cross 64k boundary due to errata 23 */	if (adapter->hw.mac_type == e1000_82545 ||	    adapter->hw.mac_type == e1000_82546) {		return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;	}	return TRUE;}/** * e1000_setup_tx_resources - allocate Tx resources (Descriptors) * @adapter: board private structure * @txdr:    tx descriptor ring (for a specific queue) to setup * * Return 0 on success, negative on failure **/static inte1000_setup_tx_resources(struct e1000_adapter *adapter,                         struct e1000_tx_ring *txdr){	struct pci_dev *pdev = adapter->pdev;	int size;	size = sizeof(struct e1000_buffer) * txdr->count;	txdr->buffer_info = vmalloc(size);	if (!txdr->buffer_info) {		DPRINTK(PROBE, ERR,		"Unable to allocate memory for the transmit descriptor ring\n");		return -ENOMEM;	}	memset(txdr->buffer_info, 0, size);	/* round up to nearest 4K */	txdr->size = txdr->count * sizeof(struct e1000_tx_desc);	txdr->size = ALIGN(txdr->size, 4096);	txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);	if (!txdr->desc) {setup_tx_desc_die:		vfree(txdr->buffer_info);		DPRINTK(PROBE, ERR,		"Unable to allocate memory for the transmit descriptor ring\n");		return -ENOMEM;	}	/* Fix for errata 23, can't cross 64kB boundary */	if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {		void *olddesc = txdr->desc;		dma_addr_t olddma = txdr->dma;		DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "				     "at %p\n", txdr->size, txdr->desc);		/* Try again, without freeing the previous */		txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);		/* Failed allocation, critical failure */		if (!txdr->desc) {			pci_free_consistent(pdev, txdr->size, olddesc, olddma);			goto setup_tx_desc_die;		}		if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {			/* give up */			pci_free_consistent(pdev, txdr->size, txdr->desc,					    txdr->dma);			pci_free_consistent(pdev, txdr->size, olddesc, olddma);			DPRINTK(PROBE, ERR,				"Unable to allocate aligned memory "				"for the transmit descriptor ring\n");			vfree(txdr->buffer_info);			return -ENOMEM;		} else {			/* Free old allocation, new allocation was successful */			pci_free_consistent(pdev, txdr->size, olddesc, olddma);		}	}	memset(txdr->desc, 0, txdr->size);	txdr->next_to_use = 0;	txdr->next_to_clean = 0;	spin_lock_init(&txdr->tx_lock);	return 0;}/** * e1000_setup_all_tx_resources - wrapper to allocate Tx resources * 				  (Descriptors) for all queues * @adapter: board private structure * * Return 0 on success, negative on failure **/inte1000_setup_all_tx_resources(struct e1000_adapter *adapter){	int i, err = 0;	for (i = 0; i < adapter->num_tx_queues; i++) {		err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);		if (err) {			DPRINTK(PROBE, ERR,				"Allocation for Tx Queue %u failed\n", i);			for (i-- ; i >= 0; i--)				e1000_free_tx_resources(adapter,							&adapter->tx_ring[i]);			break;		}	}	return err;}/** * e1000_configure_tx - Configure 8254x Transmit Unit after Reset * @adapter: board private structure * * Configure the Tx unit of the MAC after a reset. **/static voide1000_configure_tx(struct e1000_adapter *adapter){	uint64_t tdba;	struct e1000_hw *hw = &adapter->hw;	uint32_t tdlen, tctl, tipg, tarc;	uint32_t ipgr1, ipgr2;	/* Setup the HW Tx Head and Tail descriptor pointers */	switch (adapter->num_tx_queues) {	case 1:	default:		tdba = adapter->tx_ring[0].dma;		tdlen = adapter->tx_ring[0].count *			sizeof(struct e1000_tx_desc);		E1000_WRITE_REG(hw, TDLEN, tdlen);		E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));		E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));		E1000_WRITE_REG(hw, TDT, 0);		E1000_WRITE_REG(hw, TDH, 0);		adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);		adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);		break;	}	/* Set the default values for the Tx Inter Packet Gap timer */	if (adapter->hw.mac_type <= e1000_82547_rev_2 &&	    (hw->media_type == e1000_media_type_fiber ||	     hw->media_type == e1000_media_type_internal_serdes))		tipg = DEFAULT_82543_TIPG_IPGT_FIBER;	else		tipg = DEFAULT_82543_TIPG_IPGT_COPPER;	switch (hw->mac_type) {	case e1000_82542_rev2_0:	case e1000_82542_rev2_1:		tipg = DEFAULT_82542_TIPG_IPGT;		ipgr1 = DEFAULT_82542_TIPG_IPGR1;		ipgr2 = DEFAULT_82542_TIPG_IPGR2;		break;	case e1000_80003es2lan:		ipgr1 = DEFAULT_82543_TIPG_IPGR1;		ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;		break;	default:		ipgr1 = DEFAULT_82543_TIPG_IPGR1;		ipgr2 = DEFAULT_82543_TIPG_IPGR2;		break;	}	tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;	tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;	E1000_WRITE_REG(hw, TIPG, tipg);	/* Set the Tx Interrupt Delay register */	E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);	if (hw->mac_type >= e1000_82540)		E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);	/* Program the Transmit Control Register */	tctl = E1000_READ_REG(hw, TCTL);	tctl &= ~E1000_TCTL_CT;	tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |		(E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);	if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {		tarc = E1000_READ_REG(hw, TARC0);		/* set the speed mode bit, we'll clear it if we're not at		 * gigabit link later */		tarc |= (1 << 21);		E1000_WRITE_REG(hw, TARC0, tarc);	} else if (hw->mac_type == e1000_80003es2lan) {		tarc = E1000_READ_REG(hw, TARC0);		tarc |= 1;		E1000_WRITE_REG(hw, TARC0, tarc);		tarc = E1000_READ_REG(hw, TARC1);		tarc |= 1;		E1000_WRITE_REG(hw, TARC1, tarc);	}	e1000_config_collision_dist(hw);	/* Setup Transmit Descriptor Settings for eop descriptor */	adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;	/* only set IDE if we are delaying interrupts using the timers */	if (adapter->tx_int_delay)		adapter->txd_cmd |= E1000_TXD_CMD_IDE;	if (hw->mac_type < e1000_82543)		adapter->txd_cmd |= E1000_TXD_CMD_RPS;	else		adapter->txd_cmd |= E1000_TXD_CMD_RS;	/* Cache if we're 82544 running in PCI-X because we'll	 * need this to apply a workaround later in the send path. */	if (hw->mac_type == e1000_82544 &&	    hw->bus_type == e1000_bus_type_pcix)		adapter->pcix_82544 = 1;	E1000_WRITE_REG(hw, TCTL, tctl);}/** * e1000_setup_rx_resources - allocate Rx resources (Descriptors) * @adapter: board private structure * @rxdr:    rx descriptor ring (for a specific queue) to setup * * Returns 0 on success, negative on failure **/static inte1000_setup_rx_resources(struct e1000_adapter *adapter,                         struct e1000_rx_ring *rxdr){	struct pci_dev *pdev = adapter->pdev;	int size, desc_len;	size = sizeof(struct e1000_buffer) * rxdr->count;	rxdr->buffer_info = vmalloc(size);	if (!rxdr->buffer_info) {		DPRINTK(PROBE, ERR,		"Unable to allocate memory for the receive descriptor ring\n");		return -ENOMEM;	}	memset(rxdr->buffer_info, 0, size);	rxdr->ps_page = kcalloc(rxdr->count, sizeof(struct e1000_ps_page),	                        GFP_KERNEL);	if (!rxdr->ps_page) {		vfree(rxdr->buffer_info);		DPRINTK(PROBE, ERR,		"Unable to allocate memory for the receive descriptor ring\n");		return -ENOMEM;	}	rxdr->ps_page_dma = kcalloc(rxdr->count,	                            sizeof(struct e1000_ps_page_dma),	                            GFP_KERNEL);	if (!rxdr->ps_page_dma) {		vfree(rxdr->buffer_info);		kfree(rxdr->ps_page);		DPRINTK(PROBE, ERR,		"Unable to allocate memory for the receive descriptor ring\n");		return -ENOMEM;	}	if (adapter->hw.mac_type <= e1000_82547_rev_2)		desc_len = sizeof(struct e1000_rx_desc);	else		desc_len = sizeof(union e1000_rx_desc_packet_split);

⌨️ 快捷键说明

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