108057504Sxy /*
208057504Sxy  * This file is provided under a CDDLv1 license.  When using or
308057504Sxy  * redistributing this file, you may do so under this license.
408057504Sxy  * In redistributing this file this license must be included
508057504Sxy  * and no other modification of this header file is permitted.
608057504Sxy  *
708057504Sxy  * CDDL LICENSE SUMMARY
808057504Sxy  *
9d5c3073dSchenlu chen - Sun Microsystems - Beijing China  * Copyright(c) 1999 - 2009 Intel Corporation. All rights reserved.
1008057504Sxy  *
1108057504Sxy  * The contents of this file are subject to the terms of Version
1208057504Sxy  * 1.0 of the Common Development and Distribution License (the "License").
1308057504Sxy  *
1408057504Sxy  * You should have received a copy of the License with this software.
1508057504Sxy  * You can obtain a copy of the License at
1608057504Sxy  *	http://www.opensolaris.org/os/licensing.
1708057504Sxy  * See the License for the specific language governing permissions
1808057504Sxy  * and limitations under the License.
1908057504Sxy  */
2008057504Sxy 
2108057504Sxy /*
223fb4efefSchangqing li - Sun Microsystems - Beijing China  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2308057504Sxy  */
2408057504Sxy 
25dab7de2dSGarrett D'Amore /*
26dab7de2dSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27*49b78600SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
28dab7de2dSGarrett D'Amore  */
29dab7de2dSGarrett D'Amore 
3008057504Sxy /*
3108057504Sxy  * **********************************************************************
3208057504Sxy  *									*
3308057504Sxy  * Module Name:								*
3408057504Sxy  *   e1000g_rx.c							*
3508057504Sxy  *									*
3608057504Sxy  * Abstract:								*
3725f2d433Sxy  *   This file contains some routines that take care of Receive		*
3825f2d433Sxy  *   interrupt and also for the received packets it sends up to		*
3925f2d433Sxy  *   upper layer.							*
4008057504Sxy  *   It tries to do a zero copy if free buffers are available in	*
4125f2d433Sxy  *   the pool.								*
4208057504Sxy  *									*
4308057504Sxy  * **********************************************************************
4408057504Sxy  */
4508057504Sxy 
4608057504Sxy #include "e1000g_sw.h"
4708057504Sxy #include "e1000g_debug.h"
4808057504Sxy 
4954e0d7a5SMiles Xu, Sun Microsystems static p_rx_sw_packet_t e1000g_get_buf(e1000g_rx_data_t *rx_data);
5008057504Sxy #pragma	inline(e1000g_get_buf)
5108057504Sxy 
5208057504Sxy /*
5325f2d433Sxy  * e1000g_rxfree_func - the call-back function to reclaim rx buffer
5425f2d433Sxy  *
5525f2d433Sxy  * This function is called when an mp is freed by the user thru
5625f2d433Sxy  * freeb call (Only for mp constructed through desballoc call)
5725f2d433Sxy  * It returns back the freed buffer to the freelist
5808057504Sxy  */
5908057504Sxy void
e1000g_rxfree_func(p_rx_sw_packet_t packet)6025f2d433Sxy e1000g_rxfree_func(p_rx_sw_packet_t packet)
6108057504Sxy {
6254e0d7a5SMiles Xu, Sun Microsystems 	e1000g_rx_data_t *rx_data;
6354e0d7a5SMiles Xu, Sun Microsystems 	private_devi_list_t *devi_node;
6454e0d7a5SMiles Xu, Sun Microsystems 	struct e1000g *Adapter;
6554e0d7a5SMiles Xu, Sun Microsystems 	uint32_t ring_cnt;
6654e0d7a5SMiles Xu, Sun Microsystems 	uint32_t ref_cnt;
6754e0d7a5SMiles Xu, Sun Microsystems 	unsigned char *address;
68ea6b684aSyy 
6954e0d7a5SMiles Xu, Sun Microsystems 	if (packet->ref_cnt == 0) {
70ea6b684aSyy 		/*
7154e0d7a5SMiles Xu, Sun Microsystems 		 * This case only happens when rx buffers are being freed
7254e0d7a5SMiles Xu, Sun Microsystems 		 * in e1000g_stop() and freemsg() is called.
73ea6b684aSyy 		 */
7408057504Sxy 		return;
7508057504Sxy 	}
7608057504Sxy 
7754e0d7a5SMiles Xu, Sun Microsystems 	rx_data = (e1000g_rx_data_t *)(uintptr_t)packet->rx_data;
7808057504Sxy 
7908057504Sxy 	if (packet->mp == NULL) {
8008057504Sxy 		/*
8108057504Sxy 		 * Allocate a mblk that binds to the data buffer
8208057504Sxy 		 */
8354e0d7a5SMiles Xu, Sun Microsystems 		address = (unsigned char *)packet->rx_buf->address;
8454e0d7a5SMiles Xu, Sun Microsystems 		if (address != NULL) {
8554e0d7a5SMiles Xu, Sun Microsystems 			packet->mp = desballoc((unsigned char *)
8646ebaa55SMiles Xu, Sun Microsystems 			    address, packet->rx_buf->size,
8754e0d7a5SMiles Xu, Sun Microsystems 			    BPRI_MED, &packet->free_rtn);
8854e0d7a5SMiles Xu, Sun Microsystems 		}
8908057504Sxy 	}
9008057504Sxy 
91da14cebeSEric Cheng 	/*
92da14cebeSEric Cheng 	 * Enqueue the recycled packets in a recycle queue. When freelist
93da14cebeSEric Cheng 	 * dries up, move the entire chain of packets from recycle queue
94da14cebeSEric Cheng 	 * to freelist. This helps in avoiding per packet mutex contention
95da14cebeSEric Cheng 	 * around freelist.
96da14cebeSEric Cheng 	 */
9754e0d7a5SMiles Xu, Sun Microsystems 	mutex_enter(&rx_data->recycle_lock);
9854e0d7a5SMiles Xu, Sun Microsystems 	QUEUE_PUSH_TAIL(&rx_data->recycle_list, &packet->Link);
9954e0d7a5SMiles Xu, Sun Microsystems 	rx_data->recycle_freepkt++;
10054e0d7a5SMiles Xu, Sun Microsystems 	mutex_exit(&rx_data->recycle_lock);
10154e0d7a5SMiles Xu, Sun Microsystems 
10254e0d7a5SMiles Xu, Sun Microsystems 	ref_cnt = atomic_dec_32_nv(&packet->ref_cnt);
10354e0d7a5SMiles Xu, Sun Microsystems 	if (ref_cnt == 0) {
10482df3b26Schangqing li - Sun Microsystems - Beijing China 		mutex_enter(&e1000g_rx_detach_lock);
10554e0d7a5SMiles Xu, Sun Microsystems 		e1000g_free_rx_sw_packet(packet, B_FALSE);
10654e0d7a5SMiles Xu, Sun Microsystems 
10754e0d7a5SMiles Xu, Sun Microsystems 		atomic_dec_32(&rx_data->pending_count);
10854e0d7a5SMiles Xu, Sun Microsystems 		atomic_dec_32(&e1000g_mblks_pending);
10954e0d7a5SMiles Xu, Sun Microsystems 
11054e0d7a5SMiles Xu, Sun Microsystems 		if ((rx_data->pending_count == 0) &&
11154e0d7a5SMiles Xu, Sun Microsystems 		    (rx_data->flag & E1000G_RX_STOPPED)) {
11254e0d7a5SMiles Xu, Sun Microsystems 			devi_node = rx_data->priv_devi_node;
11354e0d7a5SMiles Xu, Sun Microsystems 
11454e0d7a5SMiles Xu, Sun Microsystems 			if (devi_node != NULL) {
11554e0d7a5SMiles Xu, Sun Microsystems 				ring_cnt = atomic_dec_32_nv(
11654e0d7a5SMiles Xu, Sun Microsystems 				    &devi_node->pending_rx_count);
11754e0d7a5SMiles Xu, Sun Microsystems 				if ((ring_cnt == 0) &&
11854e0d7a5SMiles Xu, Sun Microsystems 				    (devi_node->flag &
11954e0d7a5SMiles Xu, Sun Microsystems 				    E1000G_PRIV_DEVI_DETACH)) {
12054e0d7a5SMiles Xu, Sun Microsystems 					e1000g_free_priv_devi_node(
12154e0d7a5SMiles Xu, Sun Microsystems 					    devi_node);
12254e0d7a5SMiles Xu, Sun Microsystems 				}
12354e0d7a5SMiles Xu, Sun Microsystems 			} else {
12454e0d7a5SMiles Xu, Sun Microsystems 				Adapter = rx_data->rx_ring->adapter;
12554e0d7a5SMiles Xu, Sun Microsystems 				atomic_dec_32(
12654e0d7a5SMiles Xu, Sun Microsystems 				    &Adapter->pending_rx_count);
12754e0d7a5SMiles Xu, Sun Microsystems 			}
12846ebaa55SMiles Xu, Sun Microsystems 
12946ebaa55SMiles Xu, Sun Microsystems 			e1000g_free_rx_pending_buffers(rx_data);
13046ebaa55SMiles Xu, Sun Microsystems 			e1000g_free_rx_data(rx_data);
131ea6b684aSyy 		}
13254e0d7a5SMiles Xu, Sun Microsystems 		mutex_exit(&e1000g_rx_detach_lock);
133ea6b684aSyy 	}
134ea6b684aSyy }
135ea6b684aSyy 
13608057504Sxy /*
13725f2d433Sxy  * e1000g_rx_setup - setup rx data structures
13825f2d433Sxy  *
13925f2d433Sxy  * This routine initializes all of the receive related
14025f2d433Sxy  * structures. This includes the receive descriptors, the
14125f2d433Sxy  * actual receive buffers, and the rx_sw_packet software
14225f2d433Sxy  * structures.
14308057504Sxy  */
14408057504Sxy void
e1000g_rx_setup(struct e1000g * Adapter)14525f2d433Sxy e1000g_rx_setup(struct e1000g *Adapter)
14608057504Sxy {
14725f2d433Sxy 	struct e1000_hw *hw;
14825f2d433Sxy 	p_rx_sw_packet_t packet;
14908057504Sxy 	struct e1000_rx_desc *descriptor;
15025f2d433Sxy 	uint32_t buf_low;
15125f2d433Sxy 	uint32_t buf_high;
15208057504Sxy 	uint32_t reg_val;
1534d737963Sxiangtao you - Sun Microsystems - Beijing China 	uint32_t rctl;
1544d737963Sxiangtao you - Sun Microsystems - Beijing China 	uint32_t rxdctl;
1554d737963Sxiangtao you - Sun Microsystems - Beijing China 	uint32_t ert;
156caf05df5SMiles Xu, Sun Microsystems 	uint16_t phy_data;
15708057504Sxy 	int i;
15808057504Sxy 	int size;
15954e0d7a5SMiles Xu, Sun Microsystems 	e1000g_rx_data_t *rx_data;
16008057504Sxy 
16125f2d433Sxy 	hw = &Adapter->shared;
16254e0d7a5SMiles Xu, Sun Microsystems 	rx_data = Adapter->rx_ring->rx_data;
16308057504Sxy 
16408057504Sxy 	/*
16508057504Sxy 	 * zero out all of the receive buffer descriptor memory
16608057504Sxy 	 * assures any previous data or status is erased
16708057504Sxy 	 */
16854e0d7a5SMiles Xu, Sun Microsystems 	bzero(rx_data->rbd_area,
16925f2d433Sxy 	    sizeof (struct e1000_rx_desc) * Adapter->rx_desc_num);
17008057504Sxy 
17125f2d433Sxy 	if (!Adapter->rx_buffer_setup) {
17208057504Sxy 		/* Init the list of "Receive Buffer" */
17354e0d7a5SMiles Xu, Sun Microsystems 		QUEUE_INIT_LIST(&rx_data->recv_list);
17408057504Sxy 
17508057504Sxy 		/* Init the list of "Free Receive Buffer" */
17654e0d7a5SMiles Xu, Sun Microsystems 		QUEUE_INIT_LIST(&rx_data->free_list);
17708057504Sxy 
178da14cebeSEric Cheng 		/* Init the list of "Free Receive Buffer" */
17954e0d7a5SMiles Xu, Sun Microsystems 		QUEUE_INIT_LIST(&rx_data->recycle_list);
18008057504Sxy 		/*
18108057504Sxy 		 * Setup Receive list and the Free list. Note that
18208057504Sxy 		 * the both were allocated in one packet area.
18308057504Sxy 		 */
18454e0d7a5SMiles Xu, Sun Microsystems 		packet = rx_data->packet_area;
18554e0d7a5SMiles Xu, Sun Microsystems 		descriptor = rx_data->rbd_first;
18608057504Sxy 
18725f2d433Sxy 		for (i = 0; i < Adapter->rx_desc_num;
18808057504Sxy 		    i++, packet = packet->next, descriptor++) {
18908057504Sxy 			ASSERT(packet != NULL);
19008057504Sxy 			ASSERT(descriptor != NULL);
19108057504Sxy 			descriptor->buffer_addr =
19208057504Sxy 			    packet->rx_buf->dma_address;
19325f2d433Sxy 
19425f2d433Sxy 			/* Add this rx_sw_packet to the receive list */
19554e0d7a5SMiles Xu, Sun Microsystems 			QUEUE_PUSH_TAIL(&rx_data->recv_list,
19608057504Sxy 			    &packet->Link);
19708057504Sxy 		}
19808057504Sxy 
19925f2d433Sxy 		for (i = 0; i < Adapter->rx_freelist_num;
20008057504Sxy 		    i++, packet = packet->next) {
20108057504Sxy 			ASSERT(packet != NULL);
20225f2d433Sxy 			/* Add this rx_sw_packet to the free list */
20354e0d7a5SMiles Xu, Sun Microsystems 			QUEUE_PUSH_TAIL(&rx_data->free_list,
20408057504Sxy 			    &packet->Link);
20508057504Sxy 		}
20654e0d7a5SMiles Xu, Sun Microsystems 		rx_data->avail_freepkt = Adapter->rx_freelist_num;
20754e0d7a5SMiles Xu, Sun Microsystems 		rx_data->recycle_freepkt = 0;
20825f2d433Sxy 
20925f2d433Sxy 		Adapter->rx_buffer_setup = B_TRUE;
21008057504Sxy 	} else {
21108057504Sxy 		/* Setup the initial pointer to the first rx descriptor */
21225f2d433Sxy 		packet = (p_rx_sw_packet_t)
21354e0d7a5SMiles Xu, Sun Microsystems 		    QUEUE_GET_HEAD(&rx_data->recv_list);
21454e0d7a5SMiles Xu, Sun Microsystems 		descriptor = rx_data->rbd_first;
21508057504Sxy 
21625f2d433Sxy 		for (i = 0; i < Adapter->rx_desc_num; i++) {
21708057504Sxy 			ASSERT(packet != NULL);
21808057504Sxy 			ASSERT(descriptor != NULL);
21908057504Sxy 			descriptor->buffer_addr =
22008057504Sxy 			    packet->rx_buf->dma_address;
22125f2d433Sxy 
22225f2d433Sxy 			/* Get next rx_sw_packet */
22325f2d433Sxy 			packet = (p_rx_sw_packet_t)
22454e0d7a5SMiles Xu, Sun Microsystems 			    QUEUE_GET_NEXT(&rx_data->recv_list, &packet->Link);
22508057504Sxy 			descriptor++;
22608057504Sxy 		}
22708057504Sxy 	}
22808057504Sxy 
22947b7744cSyy 	E1000_WRITE_REG(&Adapter->shared, E1000_RDTR, Adapter->rx_intr_delay);
23047b7744cSyy 	E1000G_DEBUGLOG_1(Adapter, E1000G_INFO_LEVEL,
23147b7744cSyy 	    "E1000_RDTR: 0x%x\n", Adapter->rx_intr_delay);
23247b7744cSyy 	if (hw->mac.type >= e1000_82540) {
23347b7744cSyy 		E1000_WRITE_REG(&Adapter->shared, E1000_RADV,
23447b7744cSyy 		    Adapter->rx_intr_abs_delay);
23547b7744cSyy 		E1000G_DEBUGLOG_1(Adapter, E1000G_INFO_LEVEL,
23647b7744cSyy 		    "E1000_RADV: 0x%x\n", Adapter->rx_intr_abs_delay);
23747b7744cSyy 	}
23847b7744cSyy 
23908057504Sxy 	/*
24008057504Sxy 	 * Setup our descriptor pointers
24108057504Sxy 	 */
24254e0d7a5SMiles Xu, Sun Microsystems 	rx_data->rbd_next = rx_data->rbd_first;
24308057504Sxy 
24425f2d433Sxy 	size = Adapter->rx_desc_num * sizeof (struct e1000_rx_desc);
245592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDLEN(0), size);
246592a4d85Scc 	size = E1000_READ_REG(hw, E1000_RDLEN(0));
24708057504Sxy 
24808057504Sxy 	/* To get lower order bits */
24954e0d7a5SMiles Xu, Sun Microsystems 	buf_low = (uint32_t)rx_data->rbd_dma_addr;
25008057504Sxy 	/* To get the higher order bits */
25154e0d7a5SMiles Xu, Sun Microsystems 	buf_high = (uint32_t)(rx_data->rbd_dma_addr >> 32);
25208057504Sxy 
253592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDBAH(0), buf_high);
254592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDBAL(0), buf_low);
25508057504Sxy 
25608057504Sxy 	/*
25708057504Sxy 	 * Setup our HW Rx Head & Tail descriptor pointers
25808057504Sxy 	 */
259592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDT(0),
26054e0d7a5SMiles Xu, Sun Microsystems 	    (uint32_t)(rx_data->rbd_last - rx_data->rbd_first));
261592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDH(0), 0);
26208057504Sxy 
26308057504Sxy 	/*
26408057504Sxy 	 * Setup the Receive Control Register (RCTL), and ENABLE the
26508057504Sxy 	 * receiver. The initial configuration is to: Enable the receiver,
26608057504Sxy 	 * accept broadcasts, discard bad packets (and long packets),
26708057504Sxy 	 * disable VLAN filter checking, set the receive descriptor
26808057504Sxy 	 * minimum threshold size to 1/2, and the receive buffer size to
26908057504Sxy 	 * 2k.
27008057504Sxy 	 */
2714d737963Sxiangtao you - Sun Microsystems - Beijing China 	rctl = E1000_RCTL_EN |		/* Enable Receive Unit */
27208057504Sxy 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
27325f2d433Sxy 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
27408057504Sxy 	    E1000_RCTL_RDMTS_HALF |
27508057504Sxy 	    E1000_RCTL_LBM_NO;		/* Loopback Mode = none */
27608057504Sxy 
2773fb4efefSchangqing li - Sun Microsystems - Beijing China 	if (Adapter->default_mtu > ETHERMTU)
2783fb4efefSchangqing li - Sun Microsystems - Beijing China 		rctl |= E1000_RCTL_LPE;  /* Large Packet Enable bit */
2793fb4efefSchangqing li - Sun Microsystems - Beijing China 
2805633182fSyy 	if (Adapter->strip_crc)
2814d737963Sxiangtao you - Sun Microsystems - Beijing China 		rctl |= E1000_RCTL_SECRC;	/* Strip Ethernet CRC */
2825633182fSyy 
2833d15c084Schenlu chen - Sun Microsystems - Beijing China 	if (Adapter->mem_workaround_82546 &&
2843d15c084Schenlu chen - Sun Microsystems - Beijing China 	    ((hw->mac.type == e1000_82545) ||
285ede5269eSchenlu chen - Sun Microsystems - Beijing China 	    (hw->mac.type == e1000_82546) ||
2863d15c084Schenlu chen - Sun Microsystems - Beijing China 	    (hw->mac.type == e1000_82546_rev_3))) {
2874d737963Sxiangtao you - Sun Microsystems - Beijing China 		rctl |= E1000_RCTL_SZ_2048;
288ede5269eSchenlu chen - Sun Microsystems - Beijing China 	} else {
289ede5269eSchenlu chen - Sun Microsystems - Beijing China 		if ((Adapter->max_frame_size > FRAME_SIZE_UPTO_2K) &&
290ede5269eSchenlu chen - Sun Microsystems - Beijing China 		    (Adapter->max_frame_size <= FRAME_SIZE_UPTO_4K))
291ede5269eSchenlu chen - Sun Microsystems - Beijing China 			rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX;
292ede5269eSchenlu chen - Sun Microsystems - Beijing China 		else if ((Adapter->max_frame_size > FRAME_SIZE_UPTO_4K) &&
293ede5269eSchenlu chen - Sun Microsystems - Beijing China 		    (Adapter->max_frame_size <= FRAME_SIZE_UPTO_8K))
294ede5269eSchenlu chen - Sun Microsystems - Beijing China 			rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX;
295ede5269eSchenlu chen - Sun Microsystems - Beijing China 		else if ((Adapter->max_frame_size > FRAME_SIZE_UPTO_8K) &&
296ede5269eSchenlu chen - Sun Microsystems - Beijing China 		    (Adapter->max_frame_size <= FRAME_SIZE_UPTO_16K))
297ede5269eSchenlu chen - Sun Microsystems - Beijing China 			rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX;
298ede5269eSchenlu chen - Sun Microsystems - Beijing China 		else
299ede5269eSchenlu chen - Sun Microsystems - Beijing China 			rctl |= E1000_RCTL_SZ_2048;
300ede5269eSchenlu chen - Sun Microsystems - Beijing China 	}
30108057504Sxy 
30225f2d433Sxy 	if (e1000_tbi_sbp_enabled_82543(hw))
3034d737963Sxiangtao you - Sun Microsystems - Beijing China 		rctl |= E1000_RCTL_SBP;
30408057504Sxy 
30508057504Sxy 	/*
306d5c3073dSchenlu chen - Sun Microsystems - Beijing China 	 * Enable Early Receive Threshold (ERT) on supported devices.
307d5c3073dSchenlu chen - Sun Microsystems - Beijing China 	 * Only takes effect when packet size is equal or larger than the
308d5c3073dSchenlu chen - Sun Microsystems - Beijing China 	 * specified value (in 8 byte units), e.g. using jumbo frames.
30908057504Sxy 	 */
3104d737963Sxiangtao you - Sun Microsystems - Beijing China 	if ((hw->mac.type == e1000_82573) ||
3114d737963Sxiangtao you - Sun Microsystems - Beijing China 	    (hw->mac.type == e1000_82574) ||
3124d737963Sxiangtao you - Sun Microsystems - Beijing China 	    (hw->mac.type == e1000_ich9lan) ||
3134d737963Sxiangtao you - Sun Microsystems - Beijing China 	    (hw->mac.type == e1000_ich10lan)) {
31408057504Sxy 
3154d737963Sxiangtao you - Sun Microsystems - Beijing China 		ert = E1000_ERT_2048;
3164d737963Sxiangtao you - Sun Microsystems - Beijing China 
3174d737963Sxiangtao you - Sun Microsystems - Beijing China 		/*
3184d737963Sxiangtao you - Sun Microsystems - Beijing China 		 * Special modification when ERT and
3194d737963Sxiangtao you - Sun Microsystems - Beijing China 		 * jumbo frames are enabled
3204d737963Sxiangtao you - Sun Microsystems - Beijing China 		 */
3214d737963Sxiangtao you - Sun Microsystems - Beijing China 		if (Adapter->default_mtu > ETHERMTU) {
3224d737963Sxiangtao you - Sun Microsystems - Beijing China 			rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
3234d737963Sxiangtao you - Sun Microsystems - Beijing China 			E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 0x3);
3244d737963Sxiangtao you - Sun Microsystems - Beijing China 			ert |= (1 << 13);
3254d737963Sxiangtao you - Sun Microsystems - Beijing China 		}
3264d737963Sxiangtao you - Sun Microsystems - Beijing China 
3274d737963Sxiangtao you - Sun Microsystems - Beijing China 		E1000_WRITE_REG(hw, E1000_ERT, ert);
3284d737963Sxiangtao you - Sun Microsystems - Beijing China 	}
32908057504Sxy 
330caf05df5SMiles Xu, Sun Microsystems 	/* Workaround errata on 82577/8 adapters with large frames */
331caf05df5SMiles Xu, Sun Microsystems 	if ((hw->mac.type == e1000_pchlan) &&
332caf05df5SMiles Xu, Sun Microsystems 	    (Adapter->default_mtu > ETHERMTU)) {
333caf05df5SMiles Xu, Sun Microsystems 
33457ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 		(void) e1000_read_phy_reg(hw, PHY_REG(770, 26), &phy_data);
335caf05df5SMiles Xu, Sun Microsystems 		phy_data &= 0xfff8;
336caf05df5SMiles Xu, Sun Microsystems 		phy_data |= (1 << 2);
33757ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 		(void) e1000_write_phy_reg(hw, PHY_REG(770, 26), phy_data);
338caf05df5SMiles Xu, Sun Microsystems 
339caf05df5SMiles Xu, Sun Microsystems 		if (hw->phy.type == e1000_phy_82577) {
34057ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 			(void) e1000_read_phy_reg(hw, 22, &phy_data);
341caf05df5SMiles Xu, Sun Microsystems 			phy_data &= 0x0fff;
342caf05df5SMiles Xu, Sun Microsystems 			phy_data |= (1 << 14);
34357ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 			(void) e1000_write_phy_reg(hw, 0x10, 0x2823);
34457ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 			(void) e1000_write_phy_reg(hw, 0x11, 0x0003);
34557ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 			(void) e1000_write_phy_reg(hw, 22, phy_data);
346caf05df5SMiles Xu, Sun Microsystems 		}
347caf05df5SMiles Xu, Sun Microsystems 	}
348caf05df5SMiles Xu, Sun Microsystems 
349dab7de2dSGarrett D'Amore 	/* Workaround errata on 82579 adapters with large frames */
350dab7de2dSGarrett D'Amore 	if (hw->mac.type == e1000_pch2lan) {
351dab7de2dSGarrett D'Amore 		boolean_t enable_jumbo = (Adapter->default_mtu > ETHERMTU ?
352dab7de2dSGarrett D'Amore 		    B_TRUE : B_FALSE);
353dab7de2dSGarrett D'Amore 
354dab7de2dSGarrett D'Amore 		if (e1000_lv_jumbo_workaround_ich8lan(hw, enable_jumbo) != 0)
355dab7de2dSGarrett D'Amore 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
356dab7de2dSGarrett D'Amore 			    "failed to enable jumbo frame workaround mode\n");
357dab7de2dSGarrett D'Amore 	}
358dab7de2dSGarrett D'Amore 
35925f2d433Sxy 	reg_val =
36025f2d433Sxy 	    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum offload Enable */
36125f2d433Sxy 	    E1000_RXCSUM_IPOFL;		/* IP checksum offload Enable */
36208057504Sxy 
36325f2d433Sxy 	E1000_WRITE_REG(hw, E1000_RXCSUM, reg_val);
3648ede663fSzx 
3658ede663fSzx 	/*
3668ede663fSzx 	 * Workaround: Set bit 16 (IPv6_ExDIS) to disable the
3678ede663fSzx 	 * processing of received IPV6 extension headers
3688ede663fSzx 	 */
3698ede663fSzx 	if ((hw->mac.type == e1000_82571) || (hw->mac.type == e1000_82572)) {
3708ede663fSzx 		reg_val = E1000_READ_REG(hw, E1000_RFCTL);
3718ede663fSzx 		reg_val |= (E1000_RFCTL_IPV6_EX_DIS |
3728ede663fSzx 		    E1000_RFCTL_NEW_IPV6_EXT_DIS);
3738ede663fSzx 		E1000_WRITE_REG(hw, E1000_RFCTL, reg_val);
3748ede663fSzx 	}
3754d737963Sxiangtao you - Sun Microsystems - Beijing China 
3764d737963Sxiangtao you - Sun Microsystems - Beijing China 	/* Write to enable the receive unit */
3774d737963Sxiangtao you - Sun Microsystems - Beijing China 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
37808057504Sxy }
37908057504Sxy 
38008057504Sxy /*
38125f2d433Sxy  * e1000g_get_buf - get an rx sw packet from the free_list
38208057504Sxy  */
38325f2d433Sxy static p_rx_sw_packet_t
e1000g_get_buf(e1000g_rx_data_t * rx_data)38454e0d7a5SMiles Xu, Sun Microsystems e1000g_get_buf(e1000g_rx_data_t *rx_data)
38508057504Sxy {
38625f2d433Sxy 	p_rx_sw_packet_t packet;
3873fb4efefSchangqing li - Sun Microsystems - Beijing China 	struct e1000g *Adapter;
3883fb4efefSchangqing li - Sun Microsystems - Beijing China 
3893fb4efefSchangqing li - Sun Microsystems - Beijing China 	Adapter = rx_data->rx_ring->adapter;
39008057504Sxy 
39154e0d7a5SMiles Xu, Sun Microsystems 	mutex_enter(&rx_data->freelist_lock);
39225f2d433Sxy 	packet = (p_rx_sw_packet_t)
39354e0d7a5SMiles Xu, Sun Microsystems 	    QUEUE_POP_HEAD(&rx_data->free_list);
394da14cebeSEric Cheng 	if (packet != NULL) {
39554e0d7a5SMiles Xu, Sun Microsystems 		rx_data->avail_freepkt--;
3963fb4efefSchangqing li - Sun Microsystems - Beijing China 		goto end;
3973fb4efefSchangqing li - Sun Microsystems - Beijing China 	}
3983fb4efefSchangqing li - Sun Microsystems - Beijing China 
3993fb4efefSchangqing li - Sun Microsystems - Beijing China 	/*
4003fb4efefSchangqing li - Sun Microsystems - Beijing China 	 * If the freelist has no packets, check the recycle list
4013fb4efefSchangqing li - Sun Microsystems - Beijing China 	 * to see if there are any available descriptor there.
4023fb4efefSchangqing li - Sun Microsystems - Beijing China 	 */
4033fb4efefSchangqing li - Sun Microsystems - Beijing China 	mutex_enter(&rx_data->recycle_lock);
4043fb4efefSchangqing li - Sun Microsystems - Beijing China 	QUEUE_SWITCH(&rx_data->free_list, &rx_data->recycle_list);
4053fb4efefSchangqing li - Sun Microsystems - Beijing China 	rx_data->avail_freepkt = rx_data->recycle_freepkt;
4063fb4efefSchangqing li - Sun Microsystems - Beijing China 	rx_data->recycle_freepkt = 0;
4073fb4efefSchangqing li - Sun Microsystems - Beijing China 	mutex_exit(&rx_data->recycle_lock);
4083fb4efefSchangqing li - Sun Microsystems - Beijing China 	packet = (p_rx_sw_packet_t)QUEUE_POP_HEAD(&rx_data->free_list);
4093fb4efefSchangqing li - Sun Microsystems - Beijing China 	if (packet != NULL) {
4103fb4efefSchangqing li - Sun Microsystems - Beijing China 		rx_data->avail_freepkt--;
4113fb4efefSchangqing li - Sun Microsystems - Beijing China 		goto end;
4123fb4efefSchangqing li - Sun Microsystems - Beijing China 	}
4133fb4efefSchangqing li - Sun Microsystems - Beijing China 
4143fb4efefSchangqing li - Sun Microsystems - Beijing China 	if (Adapter->rx_freelist_num < Adapter->rx_freelist_limit) {
4153fb4efefSchangqing li - Sun Microsystems - Beijing China 		(void) e1000g_increase_rx_packets(rx_data);
416da14cebeSEric Cheng 		packet = (p_rx_sw_packet_t)
41754e0d7a5SMiles Xu, Sun Microsystems 		    QUEUE_POP_HEAD(&rx_data->free_list);
4183fb4efefSchangqing li - Sun Microsystems - Beijing China 		if (packet != NULL) {
41954e0d7a5SMiles Xu, Sun Microsystems 			rx_data->avail_freepkt--;
4203fb4efefSchangqing li - Sun Microsystems - Beijing China 		}
421da14cebeSEric Cheng 	}
42208057504Sxy 
4233fb4efefSchangqing li - Sun Microsystems - Beijing China end:
4243fb4efefSchangqing li - Sun Microsystems - Beijing China 	mutex_exit(&rx_data->freelist_lock);
42508057504Sxy 	return (packet);
42608057504Sxy }
42708057504Sxy 
42808057504Sxy /*
42925f2d433Sxy  * e1000g_receive - main receive routine
43025f2d433Sxy  *
43125f2d433Sxy  * This routine will process packets received in an interrupt
43208057504Sxy  */
43308057504Sxy mblk_t *
e1000g_receive(e1000g_rx_ring_t * rx_ring,mblk_t ** tail,uint_t sz)434ae6aa22aSVenugopal Iyer e1000g_receive(e1000g_rx_ring_t *rx_ring, mblk_t **tail, uint_t sz)
43508057504Sxy {
43625f2d433Sxy 	struct e1000_hw *hw;
43708057504Sxy 	mblk_t *nmp;
43808057504Sxy 	mblk_t *ret_mp;
43908057504Sxy 	mblk_t *ret_nmp;
44008057504Sxy 	struct e1000_rx_desc *current_desc;
44108057504Sxy 	struct e1000_rx_desc *last_desc;
44225f2d433Sxy 	p_rx_sw_packet_t packet;
44325f2d433Sxy 	p_rx_sw_packet_t newpkt;
4444d737963Sxiangtao you - Sun Microsystems - Beijing China 	uint16_t length;
44508057504Sxy 	uint32_t pkt_count;
44608057504Sxy 	uint32_t desc_count;
44725f2d433Sxy 	boolean_t accept_frame;
44808057504Sxy 	boolean_t end_of_packet;
44908057504Sxy 	boolean_t need_copy;
450da14cebeSEric Cheng 	struct e1000g *Adapter;
45108057504Sxy 	dma_buffer_t *rx_buf;
45208057504Sxy 	uint16_t cksumflags;
453ae6aa22aSVenugopal Iyer 	uint_t chain_sz = 0;
45454e0d7a5SMiles Xu, Sun Microsystems 	e1000g_rx_data_t *rx_data;
45546ebaa55SMiles Xu, Sun Microsystems 	uint32_t max_size;
45646ebaa55SMiles Xu, Sun Microsystems 	uint32_t min_size;
45708057504Sxy 
45808057504Sxy 	ret_mp = NULL;
45908057504Sxy 	ret_nmp = NULL;
46008057504Sxy 	pkt_count = 0;
46108057504Sxy 	desc_count = 0;
46208057504Sxy 	cksumflags = 0;
46308057504Sxy 
464da14cebeSEric Cheng 	Adapter = rx_ring->adapter;
46554e0d7a5SMiles Xu, Sun Microsystems 	rx_data = rx_ring->rx_data;
46625f2d433Sxy 	hw = &Adapter->shared;
46708057504Sxy 
46808057504Sxy 	/* Sync the Rx descriptor DMA buffers */
46954e0d7a5SMiles Xu, Sun Microsystems 	(void) ddi_dma_sync(rx_data->rbd_dma_handle,
47025f2d433Sxy 	    0, 0, DDI_DMA_SYNC_FORKERNEL);
47108057504Sxy 
47254e0d7a5SMiles Xu, Sun Microsystems 	if (e1000g_check_dma_handle(rx_data->rbd_dma_handle) != DDI_FM_OK) {
4739b6541b3Sgl 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
474d5c3073dSchenlu chen - Sun Microsystems - Beijing China 		Adapter->e1000g_state |= E1000G_ERROR;
475ec39b9cfSchangqing li - Sun Microsystems - Beijing China 		return (NULL);
4769b6541b3Sgl 	}
4779b6541b3Sgl 
47854e0d7a5SMiles Xu, Sun Microsystems 	current_desc = rx_data->rbd_next;
47908057504Sxy 	if (!(current_desc->status & E1000_RXD_STAT_DD)) {
48008057504Sxy 		/*
48108057504Sxy 		 * don't send anything up. just clear the RFD
48208057504Sxy 		 */
48325f2d433Sxy 		E1000G_DEBUG_STAT(rx_ring->stat_none);
484ec39b9cfSchangqing li - Sun Microsystems - Beijing China 		return (NULL);
48508057504Sxy 	}
48608057504Sxy 
48746ebaa55SMiles Xu, Sun Microsystems 	max_size = Adapter->max_frame_size - ETHERFCSL - VLAN_TAGSZ;
48846ebaa55SMiles Xu, Sun Microsystems 	min_size = ETHERMIN;
48946ebaa55SMiles Xu, Sun Microsystems 
49008057504Sxy 	/*
49108057504Sxy 	 * Loop through the receive descriptors starting at the last known
49208057504Sxy 	 * descriptor owned by the hardware that begins a packet.
49308057504Sxy 	 */
49408057504Sxy 	while ((current_desc->status & E1000_RXD_STAT_DD) &&
495ae6aa22aSVenugopal Iyer 	    (pkt_count < Adapter->rx_limit_onintr) &&
496ae6aa22aSVenugopal Iyer 	    ((sz == E1000G_CHAIN_NO_LIMIT) || (chain_sz <= sz))) {
49708057504Sxy 
49808057504Sxy 		desc_count++;
49908057504Sxy 		/*
50008057504Sxy 		 * Now this can happen in Jumbo frame situation.
50108057504Sxy 		 */
50208057504Sxy 		if (current_desc->status & E1000_RXD_STAT_EOP) {
50308057504Sxy 			/* packet has EOP set */
50408057504Sxy 			end_of_packet = B_TRUE;
50508057504Sxy 		} else {
50608057504Sxy 			/*
50708057504Sxy 			 * If this received buffer does not have the
50808057504Sxy 			 * End-Of-Packet bit set, the received packet
50908057504Sxy 			 * will consume multiple buffers. We won't send this
51008057504Sxy 			 * packet upstack till we get all the related buffers.
51108057504Sxy 			 */
51208057504Sxy 			end_of_packet = B_FALSE;
51308057504Sxy 		}
51408057504Sxy 
51508057504Sxy 		/*
51608057504Sxy 		 * Get a pointer to the actual receive buffer
51708057504Sxy 		 * The mp->b_rptr is mapped to The CurrentDescriptor
51808057504Sxy 		 * Buffer Address.
51908057504Sxy 		 */
52008057504Sxy 		packet =
52196ea4e93Schangqing li - Sun Microsystems - Beijing China 		    (p_rx_sw_packet_t)QUEUE_POP_HEAD(&rx_data->recv_list);
52208057504Sxy 		ASSERT(packet != NULL);
52308057504Sxy 
52408057504Sxy 		rx_buf = packet->rx_buf;
52508057504Sxy 
52608057504Sxy 		length = current_desc->length;
52708057504Sxy 
52808057504Sxy #ifdef __sparc
52925f2d433Sxy 		if (packet->dma_type == USE_DVMA)
53008057504Sxy 			dvma_sync(rx_buf->dma_handle, 0,
53108057504Sxy 			    DDI_DMA_SYNC_FORKERNEL);
53225f2d433Sxy 		else
53308057504Sxy 			(void) ddi_dma_sync(rx_buf->dma_handle,
53408057504Sxy 			    E1000G_IPALIGNROOM, length,
53525f2d433Sxy 			    DDI_DMA_SYNC_FORKERNEL);
53625f2d433Sxy #else
53725f2d433Sxy 		(void) ddi_dma_sync(rx_buf->dma_handle,
53825f2d433Sxy 		    E1000G_IPALIGNROOM, length,
53925f2d433Sxy 		    DDI_DMA_SYNC_FORKERNEL);
54025f2d433Sxy #endif
54108057504Sxy 
5429b6541b3Sgl 		if (e1000g_check_dma_handle(
5439b6541b3Sgl 		    rx_buf->dma_handle) != DDI_FM_OK) {
5449b6541b3Sgl 			ddi_fm_service_impact(Adapter->dip,
5459b6541b3Sgl 			    DDI_SERVICE_DEGRADED);
546d5c3073dSchenlu chen - Sun Microsystems - Beijing China 			Adapter->e1000g_state |= E1000G_ERROR;
547ec39b9cfSchangqing li - Sun Microsystems - Beijing China 
548ec39b9cfSchangqing li - Sun Microsystems - Beijing China 			goto rx_drop;
5499b6541b3Sgl 		}
5509b6541b3Sgl 
55125f2d433Sxy 		accept_frame = (current_desc->errors == 0) ||
55225f2d433Sxy 		    ((current_desc->errors &
55325f2d433Sxy 		    (E1000_RXD_ERR_TCPE | E1000_RXD_ERR_IPE)) != 0);
55408057504Sxy 
55525f2d433Sxy 		if (hw->mac.type == e1000_82543) {
55625f2d433Sxy 			unsigned char last_byte;
55708057504Sxy 
55825f2d433Sxy 			last_byte =
55925f2d433Sxy 			    *((unsigned char *)rx_buf->address + length - 1);
56025f2d433Sxy 
56125f2d433Sxy 			if (TBI_ACCEPT(hw,
56225f2d433Sxy 			    current_desc->status, current_desc->errors,
563592a4d85Scc 			    current_desc->length, last_byte,
564592a4d85Scc 			    Adapter->min_frame_size, Adapter->max_frame_size)) {
56525f2d433Sxy 
56625f2d433Sxy 				e1000_tbi_adjust_stats(Adapter,
56725f2d433Sxy 				    length, hw->mac.addr);
56825f2d433Sxy 
56925f2d433Sxy 				length--;
57025f2d433Sxy 				accept_frame = B_TRUE;
57125f2d433Sxy 			} else if (e1000_tbi_sbp_enabled_82543(hw) &&
57225f2d433Sxy 			    (current_desc->errors == E1000_RXD_ERR_CE)) {
57325f2d433Sxy 				accept_frame = B_TRUE;
57425f2d433Sxy 			}
57508057504Sxy 		}
57625f2d433Sxy 
57708057504Sxy 		/*
57808057504Sxy 		 * Indicate the packet to the NOS if it was good.
57908057504Sxy 		 * Normally, hardware will discard bad packets for us.
58008057504Sxy 		 * Check for the packet to be a valid Ethernet packet
58108057504Sxy 		 */
58225f2d433Sxy 		if (!accept_frame) {
58308057504Sxy 			/*
58408057504Sxy 			 * error in incoming packet, either the packet is not a
58508057504Sxy 			 * ethernet size packet, or the packet has an error. In
58608057504Sxy 			 * either case, the packet will simply be discarded.
58708057504Sxy 			 */
58825f2d433Sxy 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
58908057504Sxy 			    "Process Receive Interrupts: Error in Packet\n");
59008057504Sxy 
59125f2d433Sxy 			E1000G_STAT(rx_ring->stat_error);
59208057504Sxy 			/*
59308057504Sxy 			 * Returning here as we are done here. There is
59408057504Sxy 			 * no point in waiting for while loop to elapse
59508057504Sxy 			 * and the things which were done. More efficient
59608057504Sxy 			 * and less error prone...
59708057504Sxy 			 */
59808057504Sxy 			goto rx_drop;
59908057504Sxy 		}
60008057504Sxy 
6015633182fSyy 		/*
6025633182fSyy 		 * If the Ethernet CRC is not stripped by the hardware,
6035633182fSyy 		 * we need to strip it before sending it up to the stack.
6045633182fSyy 		 */
6055633182fSyy 		if (end_of_packet && !Adapter->strip_crc) {
6069ce7e93cScc 			if (length > ETHERFCSL) {
6079ce7e93cScc 				length -= ETHERFCSL;
6085633182fSyy 			} else {
6095633182fSyy 				/*
6105633182fSyy 				 * If the fragment is smaller than the CRC,
6115633182fSyy 				 * drop this fragment, do the processing of
6125633182fSyy 				 * the end of the packet.
6135633182fSyy 				 */
6143fb4efefSchangqing li - Sun Microsystems - Beijing China 				if (rx_data->rx_mblk_tail == NULL) {
6153fb4efefSchangqing li - Sun Microsystems - Beijing China 					E1000G_STAT(rx_ring->stat_crc_only_pkt);
6163fb4efefSchangqing li - Sun Microsystems - Beijing China 					goto rx_next_desc;
6173fb4efefSchangqing li - Sun Microsystems - Beijing China 				}
6183fb4efefSchangqing li - Sun Microsystems - Beijing China 
61954e0d7a5SMiles Xu, Sun Microsystems 				rx_data->rx_mblk_tail->b_wptr -=
6209ce7e93cScc 				    ETHERFCSL - length;
62154e0d7a5SMiles Xu, Sun Microsystems 				rx_data->rx_mblk_len -=
6229ce7e93cScc 				    ETHERFCSL - length;
6235633182fSyy 				goto rx_end_of_packet;
6245633182fSyy 			}
6255633182fSyy 		}
6265633182fSyy 
62708057504Sxy 		need_copy = B_TRUE;
62808057504Sxy 
62908057504Sxy 		if (length <= Adapter->rx_bcopy_thresh)
63008057504Sxy 			goto rx_copy;
63108057504Sxy 
63208057504Sxy 		/*
63308057504Sxy 		 * Get the pre-constructed mblk that was associated
63408057504Sxy 		 * to the receive data buffer.
63508057504Sxy 		 */
63608057504Sxy 		if (packet->mp == NULL) {
63708057504Sxy 			packet->mp = desballoc((unsigned char *)
63846ebaa55SMiles Xu, Sun Microsystems 			    rx_buf->address, length,
63908057504Sxy 			    BPRI_MED, &packet->free_rtn);
64008057504Sxy 		}
64108057504Sxy 
64208057504Sxy 		if (packet->mp != NULL) {
64308057504Sxy 			/*
64408057504Sxy 			 * We have two sets of buffer pool. One associated with
64508057504Sxy 			 * the Rxdescriptors and other a freelist buffer pool.
64608057504Sxy 			 * Each time we get a good packet, Try to get a buffer
64708057504Sxy 			 * from the freelist pool using e1000g_get_buf. If we
64808057504Sxy 			 * get free buffer, then replace the descriptor buffer
64908057504Sxy 			 * address with the free buffer we just got, and pass
65008057504Sxy 			 * the pre-constructed mblk upstack. (note no copying)
65108057504Sxy 			 *
65208057504Sxy 			 * If we failed to get a free buffer, then try to
65308057504Sxy 			 * allocate a new buffer(mp) and copy the recv buffer
65408057504Sxy 			 * content to our newly allocated buffer(mp). Don't
65508057504Sxy 			 * disturb the desriptor buffer address. (note copying)
65608057504Sxy 			 */
65754e0d7a5SMiles Xu, Sun Microsystems 			newpkt = e1000g_get_buf(rx_data);
65808057504Sxy 
65908057504Sxy 			if (newpkt != NULL) {
66008057504Sxy 				/*
66108057504Sxy 				 * Get the mblk associated to the data,
66208057504Sxy 				 * and strip it off the sw packet.
66308057504Sxy 				 */
66408057504Sxy 				nmp = packet->mp;
66508057504Sxy 				packet->mp = NULL;
66654e0d7a5SMiles Xu, Sun Microsystems 				atomic_inc_32(&packet->ref_cnt);
66708057504Sxy 
66808057504Sxy 				/*
66908057504Sxy 				 * Now replace old buffer with the new
67008057504Sxy 				 * one we got from free list
67108057504Sxy 				 * Both the RxSwPacket as well as the
67208057504Sxy 				 * Receive Buffer Descriptor will now
67308057504Sxy 				 * point to this new packet.
67408057504Sxy 				 */
67508057504Sxy 				packet = newpkt;
67625f2d433Sxy 
67708057504Sxy 				current_desc->buffer_addr =
67808057504Sxy 				    newpkt->rx_buf->dma_address;
67925f2d433Sxy 
68008057504Sxy 				need_copy = B_FALSE;
68108057504Sxy 			} else {
68257ef6f69Sguoqing zhu - Sun Microsystems - Beijing China 				/* EMPTY */
68325f2d433Sxy 				E1000G_DEBUG_STAT(rx_ring->stat_no_freepkt);
68408057504Sxy 			}
68508057504Sxy 		}
68608057504Sxy 
68708057504Sxy rx_copy:
68808057504Sxy 		if (need_copy) {
68908057504Sxy 			/*
69008057504Sxy 			 * No buffers available on free list,
69108057504Sxy 			 * bcopy the data from the buffer and
69208057504Sxy 			 * keep the original buffer. Dont want to
69308057504Sxy 			 * do this.. Yack but no other way
69408057504Sxy 			 */
69525f2d433Sxy 			if ((nmp = allocb(length + E1000G_IPALIGNROOM,
6965633182fSyy 			    BPRI_MED)) == NULL) {
69708057504Sxy 				/*
69808057504Sxy 				 * The system has no buffers available
69908057504Sxy 				 * to send up the incoming packet, hence
70008057504Sxy 				 * the packet will have to be processed
70108057504Sxy 				 * when there're more buffers available.
70208057504Sxy 				 */
70325f2d433Sxy 				E1000G_STAT(rx_ring->stat_allocb_fail);
70408057504Sxy 				goto rx_drop;
70508057504Sxy 			}
70608057504Sxy 			nmp->b_rptr += E1000G_IPALIGNROOM;
70708057504Sxy 			nmp->b_wptr += E1000G_IPALIGNROOM;
70808057504Sxy 			/*
70908057504Sxy 			 * The free list did not have any buffers
71008057504Sxy 			 * available, so, the received packet will
71108057504Sxy 			 * have to be copied into a mp and the original
71208057504Sxy 			 * buffer will have to be retained for future
71308057504Sxy 			 * packet reception.
71408057504Sxy 			 */
71525f2d433Sxy 			bcopy(rx_buf->address, nmp->b_wptr, length);
71608057504Sxy 		}
71708057504Sxy 
71808057504Sxy 		ASSERT(nmp != NULL);
71908057504Sxy 		nmp->b_wptr += length;
72008057504Sxy 
72154e0d7a5SMiles Xu, Sun Microsystems 		if (rx_data->rx_mblk == NULL) {
72208057504Sxy 			/*
72308057504Sxy 			 *  TCP/UDP checksum offload and
72408057504Sxy 			 *  IP checksum offload
72508057504Sxy 			 */
72625f2d433Sxy 			if (!(current_desc->status & E1000_RXD_STAT_IXSM)) {
72708057504Sxy 				/*
72808057504Sxy 				 * Check TCP/UDP checksum
72908057504Sxy 				 */
73008057504Sxy 				if ((current_desc->status &
7315633182fSyy 				    E1000_RXD_STAT_TCPCS) &&
73208057504Sxy 				    !(current_desc->errors &
7335633182fSyy 				    E1000_RXD_ERR_TCPE))
7340dc2366fSVenugopal Iyer 					cksumflags |= HCK_FULLCKSUM_OK;
73508057504Sxy 				/*
73608057504Sxy 				 * Check IP Checksum
73708057504Sxy 				 */
73808057504Sxy 				if ((current_desc->status &
7395633182fSyy 				    E1000_RXD_STAT_IPCS) &&
74008057504Sxy 				    !(current_desc->errors &
7415633182fSyy 				    E1000_RXD_ERR_IPE))
7420dc2366fSVenugopal Iyer 					cksumflags |= HCK_IPV4_HDRCKSUM_OK;
74308057504Sxy 			}
74408057504Sxy 		}
74508057504Sxy 
74608057504Sxy 		/*
74708057504Sxy 		 * We need to maintain our packet chain in the global
74808057504Sxy 		 * Adapter structure, for the Rx processing can end
74908057504Sxy 		 * with a fragment that has no EOP set.
75008057504Sxy 		 */
75154e0d7a5SMiles Xu, Sun Microsystems 		if (rx_data->rx_mblk == NULL) {
75208057504Sxy 			/* Get the head of the message chain */
75354e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk = nmp;
75454e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk_tail = nmp;
75554e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk_len = length;
75608057504Sxy 		} else {	/* Not the first packet */
75708057504Sxy 			/* Continue adding buffers */
75854e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk_tail->b_cont = nmp;
75954e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk_tail = nmp;
76054e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rx_mblk_len += length;
76108057504Sxy 		}
76254e0d7a5SMiles Xu, Sun Microsystems 		ASSERT(rx_data->rx_mblk != NULL);
76354e0d7a5SMiles Xu, Sun Microsystems 		ASSERT(rx_data->rx_mblk_tail != NULL);
76454e0d7a5SMiles Xu, Sun Microsystems 		ASSERT(rx_data->rx_mblk_tail->b_cont == NULL);
76508057504Sxy 
76608057504Sxy 		/*
76708057504Sxy 		 * Now this MP is ready to travel upwards but some more
76808057504Sxy 		 * fragments are coming.
76908057504Sxy 		 * We will send packet upwards as soon as we get EOP
77008057504Sxy 		 * set on the packet.
77108057504Sxy 		 */
77208057504Sxy 		if (!end_of_packet) {
77308057504Sxy 			/*
77408057504Sxy 			 * continue to get the next descriptor,
77508057504Sxy 			 * Tail would be advanced at the end
77608057504Sxy 			 */
77708057504Sxy 			goto rx_next_desc;
77808057504Sxy 		}
77908057504Sxy 
7805633182fSyy rx_end_of_packet:
78146ebaa55SMiles Xu, Sun Microsystems 		if (E1000G_IS_VLAN_PACKET(rx_data->rx_mblk->b_rptr))
78246ebaa55SMiles Xu, Sun Microsystems 			max_size = Adapter->max_frame_size - ETHERFCSL;
78346ebaa55SMiles Xu, Sun Microsystems 
78446ebaa55SMiles Xu, Sun Microsystems 		if ((rx_data->rx_mblk_len > max_size) ||
78546ebaa55SMiles Xu, Sun Microsystems 		    (rx_data->rx_mblk_len < min_size)) {
78646ebaa55SMiles Xu, Sun Microsystems 			E1000G_STAT(rx_ring->stat_size_error);
78746ebaa55SMiles Xu, Sun Microsystems 			goto rx_drop;
78846ebaa55SMiles Xu, Sun Microsystems 		}
78946ebaa55SMiles Xu, Sun Microsystems 
79008057504Sxy 		/*
79108057504Sxy 		 * Found packet with EOP
79208057504Sxy 		 * Process the last fragment.
79308057504Sxy 		 */
79408057504Sxy 		if (cksumflags != 0) {
7950dc2366fSVenugopal Iyer 			mac_hcksum_set(rx_data->rx_mblk,
7960dc2366fSVenugopal Iyer 			    0, 0, 0, 0, cksumflags);
79708057504Sxy 			cksumflags = 0;
79808057504Sxy 		}
79908057504Sxy 
80008057504Sxy 		/*
80108057504Sxy 		 * Count packets that span multi-descriptors
80208057504Sxy 		 */
80325f2d433Sxy 		E1000G_DEBUG_STAT_COND(rx_ring->stat_multi_desc,
80454e0d7a5SMiles Xu, Sun Microsystems 		    (rx_data->rx_mblk->b_cont != NULL));
80508057504Sxy 
80608057504Sxy 		/*
80708057504Sxy 		 * Append to list to send upstream
80808057504Sxy 		 */
80908057504Sxy 		if (ret_mp == NULL) {
81054e0d7a5SMiles Xu, Sun Microsystems 			ret_mp = ret_nmp = rx_data->rx_mblk;
81108057504Sxy 		} else {
81254e0d7a5SMiles Xu, Sun Microsystems 			ret_nmp->b_next = rx_data->rx_mblk;
81354e0d7a5SMiles Xu, Sun Microsystems 			ret_nmp = rx_data->rx_mblk;
81408057504Sxy 		}
81508057504Sxy 		ret_nmp->b_next = NULL;
816da14cebeSEric Cheng 		*tail = ret_nmp;
817ae6aa22aSVenugopal Iyer 		chain_sz += length;
81808057504Sxy 
81954e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk = NULL;
82054e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk_tail = NULL;
82154e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk_len = 0;
82208057504Sxy 
82308057504Sxy 		pkt_count++;
82408057504Sxy 
82508057504Sxy rx_next_desc:
82608057504Sxy 		/*
82708057504Sxy 		 * Zero out the receive descriptors status
82808057504Sxy 		 */
82908057504Sxy 		current_desc->status = 0;
83008057504Sxy 
83154e0d7a5SMiles Xu, Sun Microsystems 		if (current_desc == rx_data->rbd_last)
83254e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rbd_next = rx_data->rbd_first;
83308057504Sxy 		else
83454e0d7a5SMiles Xu, Sun Microsystems 			rx_data->rbd_next++;
83508057504Sxy 
83608057504Sxy 		last_desc = current_desc;
83754e0d7a5SMiles Xu, Sun Microsystems 		current_desc = rx_data->rbd_next;
83808057504Sxy 
83908057504Sxy 		/*
84008057504Sxy 		 * Put the buffer that we just indicated back
84108057504Sxy 		 * at the end of our list
84208057504Sxy 		 */
84354e0d7a5SMiles Xu, Sun Microsystems 		QUEUE_PUSH_TAIL(&rx_data->recv_list,
84408057504Sxy 		    &packet->Link);
84508057504Sxy 	}	/* while loop */
84608057504Sxy 
84708057504Sxy 	/* Sync the Rx descriptor DMA buffers */
84854e0d7a5SMiles Xu, Sun Microsystems 	(void) ddi_dma_sync(rx_data->rbd_dma_handle,
84925f2d433Sxy 	    0, 0, DDI_DMA_SYNC_FORDEV);
85008057504Sxy 
85108057504Sxy 	/*
85208057504Sxy 	 * Advance the E1000's Receive Queue #0 "Tail Pointer".
85308057504Sxy 	 */
854592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDT(0),
85554e0d7a5SMiles Xu, Sun Microsystems 	    (uint32_t)(last_desc - rx_data->rbd_first));
85608057504Sxy 
8579b6541b3Sgl 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
8589b6541b3Sgl 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
859d5c3073dSchenlu chen - Sun Microsystems - Beijing China 		Adapter->e1000g_state |= E1000G_ERROR;
8609b6541b3Sgl 	}
8619b6541b3Sgl 
86247b7744cSyy 	Adapter->rx_pkt_cnt = pkt_count;
86347b7744cSyy 
86408057504Sxy 	return (ret_mp);
86508057504Sxy 
86608057504Sxy rx_drop:
86708057504Sxy 	/*
86808057504Sxy 	 * Zero out the receive descriptors status
86908057504Sxy 	 */
87008057504Sxy 	current_desc->status = 0;
87108057504Sxy 
87208057504Sxy 	/* Sync the Rx descriptor DMA buffers */
87354e0d7a5SMiles Xu, Sun Microsystems 	(void) ddi_dma_sync(rx_data->rbd_dma_handle,
87425f2d433Sxy 	    0, 0, DDI_DMA_SYNC_FORDEV);
87508057504Sxy 
87654e0d7a5SMiles Xu, Sun Microsystems 	if (current_desc == rx_data->rbd_last)
87754e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rbd_next = rx_data->rbd_first;
87808057504Sxy 	else
87954e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rbd_next++;
88008057504Sxy 
88108057504Sxy 	last_desc = current_desc;
88208057504Sxy 
88354e0d7a5SMiles Xu, Sun Microsystems 	QUEUE_PUSH_TAIL(&rx_data->recv_list, &packet->Link);
88408057504Sxy 	/*
88508057504Sxy 	 * Reclaim all old buffers already allocated during
88608057504Sxy 	 * Jumbo receives.....for incomplete reception
88708057504Sxy 	 */
88854e0d7a5SMiles Xu, Sun Microsystems 	if (rx_data->rx_mblk != NULL) {
88954e0d7a5SMiles Xu, Sun Microsystems 		freemsg(rx_data->rx_mblk);
89054e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk = NULL;
89154e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk_tail = NULL;
89254e0d7a5SMiles Xu, Sun Microsystems 		rx_data->rx_mblk_len = 0;
89308057504Sxy 	}
89408057504Sxy 	/*
89508057504Sxy 	 * Advance the E1000's Receive Queue #0 "Tail Pointer".
89608057504Sxy 	 */
897592a4d85Scc 	E1000_WRITE_REG(hw, E1000_RDT(0),
89854e0d7a5SMiles Xu, Sun Microsystems 	    (uint32_t)(last_desc - rx_data->rbd_first));
89908057504Sxy 
9009b6541b3Sgl 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
9019b6541b3Sgl 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
902d5c3073dSchenlu chen - Sun Microsystems - Beijing China 		Adapter->e1000g_state |= E1000G_ERROR;
9039b6541b3Sgl 	}
9049b6541b3Sgl 
90508057504Sxy 	return (ret_mp);
90608057504Sxy }
907*49b78600SRobert Mustacchi 
908*49b78600SRobert Mustacchi /*
909*49b78600SRobert Mustacchi  * This is part of a workaround for the I219, see e1000g_flush_desc_rings() for
910*49b78600SRobert Mustacchi  * more information.
911*49b78600SRobert Mustacchi  *
912*49b78600SRobert Mustacchi  * Flush all descriptors in the rx ring and disable it.
913*49b78600SRobert Mustacchi  */
914*49b78600SRobert Mustacchi void
e1000g_flush_rx_ring(struct e1000g * Adapter)915*49b78600SRobert Mustacchi e1000g_flush_rx_ring(struct e1000g *Adapter)
916*49b78600SRobert Mustacchi {
917*49b78600SRobert Mustacchi 	struct e1000_hw	*hw = &Adapter->shared;
918*49b78600SRobert Mustacchi 	uint32_t rctl, rxdctl;
919*49b78600SRobert Mustacchi 
920*49b78600SRobert Mustacchi 	rctl = E1000_READ_REG(hw, E1000_RCTL);
921*49b78600SRobert Mustacchi 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
922*49b78600SRobert Mustacchi 	E1000_WRITE_FLUSH(hw);
923*49b78600SRobert Mustacchi 	usec_delay(150);
924*49b78600SRobert Mustacchi 
925*49b78600SRobert Mustacchi 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
926*49b78600SRobert Mustacchi 	/* Zero the lower 14 bits (prefetch and host thresholds). */
927*49b78600SRobert Mustacchi 	rxdctl &= 0xffffc000;
928*49b78600SRobert Mustacchi 	/*
929*49b78600SRobert Mustacchi 	 * Update thresholds: prefetch threshold to 31, host threshold to 1
930*49b78600SRobert Mustacchi 	 * and make sure the granularity is "descriptors" and not "cache lines"
931*49b78600SRobert Mustacchi 	 */
932*49b78600SRobert Mustacchi 	rxdctl |= (0x1F | (1 << 8) | E1000_RXDCTL_THRESH_UNIT_DESC);
933*49b78600SRobert Mustacchi 	E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl);
934*49b78600SRobert Mustacchi 
935*49b78600SRobert Mustacchi 	/* Momentarily enable the RX ring for the changes to take effect */
936*49b78600SRobert Mustacchi 	E1000_WRITE_REG(hw, E1000_RCTL, rctl | E1000_RCTL_EN);
937*49b78600SRobert Mustacchi 	E1000_WRITE_FLUSH(hw);
938*49b78600SRobert Mustacchi 	usec_delay(150);
939*49b78600SRobert Mustacchi 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
940*49b78600SRobert Mustacchi 
941*49b78600SRobert Mustacchi }
942