xref: /titanic_53/usr/src/uts/common/io/igb/igb_rx.c (revision e5513923451d9bed546d16298b8bbb046bd602be)
1c869993eSxy150489 /*
2c869993eSxy150489  * CDDL HEADER START
3c869993eSxy150489  *
480a11ad2Schenlu chen - Sun Microsystems - Beijing China  * Copyright(c) 2007-2009 Intel Corporation. All rights reserved.
5c869993eSxy150489  * The contents of this file are subject to the terms of the
6c869993eSxy150489  * Common Development and Distribution License (the "License").
7c869993eSxy150489  * You may not use this file except in compliance with the License.
8c869993eSxy150489  *
90dc2366fSVenugopal Iyer  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100dc2366fSVenugopal Iyer  * or http://www.opensolaris.org/os/licensing.
11c869993eSxy150489  * See the License for the specific language governing permissions
12c869993eSxy150489  * and limitations under the License.
13c869993eSxy150489  *
140dc2366fSVenugopal Iyer  * When distributing Covered Code, include this CDDL HEADER in each
150dc2366fSVenugopal Iyer  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16c869993eSxy150489  * If applicable, add the following below this CDDL HEADER, with the
17c869993eSxy150489  * fields enclosed by brackets "[]" replaced with your own identifying
18c869993eSxy150489  * information: Portions Copyright [yyyy] [name of copyright owner]
19c869993eSxy150489  *
20c869993eSxy150489  * CDDL HEADER END
21c869993eSxy150489  */
22c869993eSxy150489 
23c869993eSxy150489 /*
24ac7f5757Schenlu chen - Sun Microsystems - Beijing China  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
250dc2366fSVenugopal Iyer  * Use is subject to license terms.
26da14cebeSEric Cheng  */
27c869993eSxy150489 
28c869993eSxy150489 #include "igb_sw.h"
29c869993eSxy150489 
30c869993eSxy150489 /* function prototypes */
31ac7f5757Schenlu chen - Sun Microsystems - Beijing China static mblk_t *igb_rx_bind(igb_rx_data_t *, uint32_t, uint32_t);
32ac7f5757Schenlu chen - Sun Microsystems - Beijing China static mblk_t *igb_rx_copy(igb_rx_data_t *, uint32_t, uint32_t);
33c869993eSxy150489 static void igb_rx_assoc_hcksum(mblk_t *, uint32_t);
34c869993eSxy150489 
35c869993eSxy150489 #ifndef IGB_DEBUG
36c869993eSxy150489 #pragma inline(igb_rx_assoc_hcksum)
37c869993eSxy150489 #endif
38c869993eSxy150489 
39c869993eSxy150489 
40c869993eSxy150489 /*
41c869993eSxy150489  * igb_rx_recycle - the call-back function to reclaim rx buffer
42c869993eSxy150489  *
43c869993eSxy150489  * This function is called when an mp is freed by the user thru
44c869993eSxy150489  * freeb call (Only for mp constructed through desballoc call).
45c869993eSxy150489  * It returns back the freed buffer to the free list.
46c869993eSxy150489  */
47c869993eSxy150489 void
igb_rx_recycle(caddr_t arg)48c869993eSxy150489 igb_rx_recycle(caddr_t arg)
49c869993eSxy150489 {
50ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb_t *igb;
51c869993eSxy150489 	igb_rx_ring_t *rx_ring;
52ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb_rx_data_t	*rx_data;
53c869993eSxy150489 	rx_control_block_t *recycle_rcb;
54c869993eSxy150489 	uint32_t free_index;
55ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	uint32_t ref_cnt;
56c869993eSxy150489 
57c869993eSxy150489 	recycle_rcb = (rx_control_block_t *)(uintptr_t)arg;
58ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data = recycle_rcb->rx_data;
59ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_ring = rx_data->rx_ring;
60ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb = rx_ring->igb;
61c869993eSxy150489 
62ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	if (recycle_rcb->ref_cnt == 0) {
63ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		/*
64ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 * This case only happens when rx buffers are being freed
65ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 * in igb_stop() and freemsg() is called.
66ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 */
67c869993eSxy150489 		return;
68ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	}
69c869993eSxy150489 
70c869993eSxy150489 	ASSERT(recycle_rcb->mp == NULL);
71c869993eSxy150489 
72c869993eSxy150489 	/*
73c869993eSxy150489 	 * Using the recycled data buffer to generate a new mblk
74c869993eSxy150489 	 */
75c869993eSxy150489 	recycle_rcb->mp = desballoc((unsigned char *)
7680a11ad2Schenlu chen - Sun Microsystems - Beijing China 	    recycle_rcb->rx_buf.address,
7780a11ad2Schenlu chen - Sun Microsystems - Beijing China 	    recycle_rcb->rx_buf.size,
78c869993eSxy150489 	    0, &recycle_rcb->free_rtn);
79c869993eSxy150489 
80c869993eSxy150489 	/*
81c869993eSxy150489 	 * Put the recycled rx control block into free list
82c869993eSxy150489 	 */
83ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	mutex_enter(&rx_data->recycle_lock);
84c869993eSxy150489 
85ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	free_index = rx_data->rcb_tail;
86ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	ASSERT(rx_data->free_list[free_index] == NULL);
87c869993eSxy150489 
88ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->free_list[free_index] = recycle_rcb;
89ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->rcb_tail = NEXT_INDEX(free_index, 1, rx_data->free_list_size);
90c869993eSxy150489 
91ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	mutex_exit(&rx_data->recycle_lock);
92c869993eSxy150489 
93c869993eSxy150489 	/*
94c869993eSxy150489 	 * The atomic operation on the number of the available rx control
95c869993eSxy150489 	 * blocks in the free list is used to make the recycling mutual
96c869993eSxy150489 	 * exclusive with the receiving.
97c869993eSxy150489 	 */
98ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	atomic_inc_32(&rx_data->rcb_free);
99ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	ASSERT(rx_data->rcb_free <= rx_data->free_list_size);
100ac7f5757Schenlu chen - Sun Microsystems - Beijing China 
101ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	/*
102ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	 * Considering the case that the interface is unplumbed
103ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	 * and there are still some buffers held by the upper layer.
104ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	 * When the buffer is returned back, we need to free it.
105ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	 */
106ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	ref_cnt = atomic_dec_32_nv(&recycle_rcb->ref_cnt);
107ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	if (ref_cnt == 0) {
108ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		if (recycle_rcb->mp != NULL) {
109ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			freemsg(recycle_rcb->mp);
110ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			recycle_rcb->mp = NULL;
111ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		}
112ac7f5757Schenlu chen - Sun Microsystems - Beijing China 
113ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		igb_free_dma_buffer(&recycle_rcb->rx_buf);
114ac7f5757Schenlu chen - Sun Microsystems - Beijing China 
115ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		mutex_enter(&igb->rx_pending_lock);
116ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		atomic_dec_32(&rx_data->rcb_pending);
117ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		atomic_dec_32(&igb->rcb_pending);
118ac7f5757Schenlu chen - Sun Microsystems - Beijing China 
119ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		/*
120ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 * When there is not any buffer belonging to this rx_data
121ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 * held by the upper layer, the rx_data can be freed.
122ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 */
123ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		if ((rx_data->flag & IGB_RX_STOPPED) &&
124ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		    (rx_data->rcb_pending == 0))
125ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			igb_free_rx_ring_data(rx_data);
126ac7f5757Schenlu chen - Sun Microsystems - Beijing China 
127ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		mutex_exit(&igb->rx_pending_lock);
128ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	}
129c869993eSxy150489 }
130c869993eSxy150489 
131c869993eSxy150489 /*
132c869993eSxy150489  * igb_rx_copy - Use copy to process the received packet
133c869993eSxy150489  *
134c869993eSxy150489  * This function will use bcopy to process the packet
135c869993eSxy150489  * and send the copied packet upstream
136c869993eSxy150489  */
137c869993eSxy150489 static mblk_t *
igb_rx_copy(igb_rx_data_t * rx_data,uint32_t index,uint32_t pkt_len)138ac7f5757Schenlu chen - Sun Microsystems - Beijing China igb_rx_copy(igb_rx_data_t *rx_data, uint32_t index, uint32_t pkt_len)
139c869993eSxy150489 {
140c869993eSxy150489 	rx_control_block_t *current_rcb;
141c869993eSxy150489 	mblk_t *mp;
142ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb_t *igb = rx_data->rx_ring->igb;
143c869993eSxy150489 
144ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	current_rcb = rx_data->work_list[index];
145c869993eSxy150489 
146c869993eSxy150489 	DMA_SYNC(&current_rcb->rx_buf, DDI_DMA_SYNC_FORKERNEL);
147c869993eSxy150489 
1488bb4b220Sgl147354 	if (igb_check_dma_handle(
1498bb4b220Sgl147354 	    current_rcb->rx_buf.dma_handle) != DDI_FM_OK) {
1508bb4b220Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
151cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		atomic_or_32(&igb->igb_state, IGB_ERROR);
152cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		return (NULL);
1538bb4b220Sgl147354 	}
1548bb4b220Sgl147354 
155c869993eSxy150489 	/*
156c869993eSxy150489 	 * Allocate buffer to receive this packet
157c869993eSxy150489 	 */
158c869993eSxy150489 	mp = allocb(pkt_len + IPHDR_ALIGN_ROOM, 0);
159c869993eSxy150489 	if (mp == NULL) {
160*e5513923SYuri Pankov 		igb_log(igb, IGB_LOG_INFO,
161*e5513923SYuri Pankov 		    "igb_rx_copy: allocate buffer failed");
162c869993eSxy150489 		return (NULL);
163c869993eSxy150489 	}
164c869993eSxy150489 
165c869993eSxy150489 	/*
166c869993eSxy150489 	 * Copy the data received into the new cluster
167c869993eSxy150489 	 */
168c869993eSxy150489 	mp->b_rptr += IPHDR_ALIGN_ROOM;
169c869993eSxy150489 	bcopy(current_rcb->rx_buf.address, mp->b_rptr, pkt_len);
170c869993eSxy150489 	mp->b_wptr = mp->b_rptr + pkt_len;
171c869993eSxy150489 
172c869993eSxy150489 	return (mp);
173c869993eSxy150489 }
174c869993eSxy150489 
175c869993eSxy150489 /*
176c869993eSxy150489  * igb_rx_bind - Use existing DMA buffer to build mblk for receiving
177c869993eSxy150489  *
178c869993eSxy150489  * This function will use pre-bound DMA buffer to receive the packet
179c869993eSxy150489  * and build mblk that will be sent upstream.
180c869993eSxy150489  */
181c869993eSxy150489 static mblk_t *
igb_rx_bind(igb_rx_data_t * rx_data,uint32_t index,uint32_t pkt_len)182ac7f5757Schenlu chen - Sun Microsystems - Beijing China igb_rx_bind(igb_rx_data_t *rx_data, uint32_t index, uint32_t pkt_len)
183c869993eSxy150489 {
184c869993eSxy150489 	rx_control_block_t *current_rcb;
185c869993eSxy150489 	rx_control_block_t *free_rcb;
186c869993eSxy150489 	uint32_t free_index;
187c869993eSxy150489 	mblk_t *mp;
188ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb_t *igb = rx_data->rx_ring->igb;
189c869993eSxy150489 
190c869993eSxy150489 	/*
191c869993eSxy150489 	 * If the free list is empty, we cannot proceed to send
192c869993eSxy150489 	 * the current DMA buffer upstream. We'll have to return
193c869993eSxy150489 	 * and use bcopy to process the packet.
194c869993eSxy150489 	 */
195ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	if (igb_atomic_reserve(&rx_data->rcb_free, 1) < 0)
196c869993eSxy150489 		return (NULL);
197c869993eSxy150489 
198ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	current_rcb = rx_data->work_list[index];
199c869993eSxy150489 	/*
200c869993eSxy150489 	 * If the mp of the rx control block is NULL, try to do
201c869993eSxy150489 	 * desballoc again.
202c869993eSxy150489 	 */
203c869993eSxy150489 	if (current_rcb->mp == NULL) {
204c869993eSxy150489 		current_rcb->mp = desballoc((unsigned char *)
20580a11ad2Schenlu chen - Sun Microsystems - Beijing China 		    current_rcb->rx_buf.address,
20680a11ad2Schenlu chen - Sun Microsystems - Beijing China 		    current_rcb->rx_buf.size,
207c869993eSxy150489 		    0, &current_rcb->free_rtn);
208c869993eSxy150489 		/*
209c869993eSxy150489 		 * If it is failed to built a mblk using the current
210c869993eSxy150489 		 * DMA buffer, we have to return and use bcopy to
211c869993eSxy150489 		 * process the packet.
212c869993eSxy150489 		 */
213c869993eSxy150489 		if (current_rcb->mp == NULL) {
214ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			atomic_inc_32(&rx_data->rcb_free);
215c869993eSxy150489 			return (NULL);
216c869993eSxy150489 		}
217c869993eSxy150489 	}
218c869993eSxy150489 	/*
219c869993eSxy150489 	 * Sync up the data received
220c869993eSxy150489 	 */
221c869993eSxy150489 	DMA_SYNC(&current_rcb->rx_buf, DDI_DMA_SYNC_FORKERNEL);
222c869993eSxy150489 
2238bb4b220Sgl147354 	if (igb_check_dma_handle(
2248bb4b220Sgl147354 	    current_rcb->rx_buf.dma_handle) != DDI_FM_OK) {
2258bb4b220Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
226cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		atomic_or_32(&igb->igb_state, IGB_ERROR);
227ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		atomic_inc_32(&rx_data->rcb_free);
228cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		return (NULL);
2298bb4b220Sgl147354 	}
2308bb4b220Sgl147354 
231c869993eSxy150489 	mp = current_rcb->mp;
232c869993eSxy150489 	current_rcb->mp = NULL;
233ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	atomic_inc_32(&current_rcb->ref_cnt);
234c869993eSxy150489 
235c869993eSxy150489 	mp->b_wptr = mp->b_rptr + pkt_len;
236c869993eSxy150489 	mp->b_next = mp->b_cont = NULL;
237c869993eSxy150489 
238c869993eSxy150489 	/*
239c869993eSxy150489 	 * Strip off one free rx control block from the free list
240c869993eSxy150489 	 */
241ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	free_index = rx_data->rcb_head;
242ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	free_rcb = rx_data->free_list[free_index];
243c869993eSxy150489 	ASSERT(free_rcb != NULL);
244ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->free_list[free_index] = NULL;
245ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->rcb_head = NEXT_INDEX(free_index, 1, rx_data->free_list_size);
246c869993eSxy150489 
247c869993eSxy150489 	/*
248c869993eSxy150489 	 * Put the rx control block to the work list
249c869993eSxy150489 	 */
250ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->work_list[index] = free_rcb;
251c869993eSxy150489 
252c869993eSxy150489 	return (mp);
253c869993eSxy150489 }
254c869993eSxy150489 
255c869993eSxy150489 /*
256c869993eSxy150489  * igb_rx_assoc_hcksum
257c869993eSxy150489  *
258c869993eSxy150489  * Check the rx hardware checksum status and associate the hcksum flags
259c869993eSxy150489  */
260c869993eSxy150489 static void
igb_rx_assoc_hcksum(mblk_t * mp,uint32_t status_error)261c869993eSxy150489 igb_rx_assoc_hcksum(mblk_t *mp, uint32_t status_error)
262c869993eSxy150489 {
263c869993eSxy150489 	uint32_t hcksum_flags = 0;
264c869993eSxy150489 
265c869993eSxy150489 	/* Ignore Checksum Indication */
266c869993eSxy150489 	if (status_error & E1000_RXD_STAT_IXSM)
267c869993eSxy150489 		return;
268c869993eSxy150489 
269c869993eSxy150489 	/*
270c869993eSxy150489 	 * Check TCP/UDP checksum
271c869993eSxy150489 	 */
272c869993eSxy150489 	if (((status_error & E1000_RXD_STAT_TCPCS) ||
273c869993eSxy150489 	    (status_error & E1000_RXD_STAT_UDPCS)) &&
274c869993eSxy150489 	    !(status_error & E1000_RXDEXT_STATERR_TCPE))
2750dc2366fSVenugopal Iyer 		hcksum_flags |= HCK_FULLCKSUM_OK;
276c869993eSxy150489 
277c869993eSxy150489 	/*
278c869993eSxy150489 	 * Check IP Checksum
279c869993eSxy150489 	 */
280c869993eSxy150489 	if ((status_error & E1000_RXD_STAT_IPCS) &&
281c869993eSxy150489 	    !(status_error & E1000_RXDEXT_STATERR_IPE))
2820dc2366fSVenugopal Iyer 		hcksum_flags |= HCK_IPV4_HDRCKSUM_OK;
283c869993eSxy150489 
284c869993eSxy150489 	if (hcksum_flags != 0) {
2850dc2366fSVenugopal Iyer 		mac_hcksum_set(mp, 0, 0, 0, 0, hcksum_flags);
286c869993eSxy150489 	}
287c869993eSxy150489 }
288c869993eSxy150489 
289da14cebeSEric Cheng mblk_t *
igb_rx_ring_poll(void * arg,int bytes)290da14cebeSEric Cheng igb_rx_ring_poll(void *arg, int bytes)
291da14cebeSEric Cheng {
292da14cebeSEric Cheng 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg;
293da14cebeSEric Cheng 	mblk_t *mp = NULL;
294da14cebeSEric Cheng 
295da14cebeSEric Cheng 	ASSERT(bytes >= 0);
296da14cebeSEric Cheng 
297cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 	if ((bytes == 0) || (rx_ring->igb->igb_state & IGB_SUSPENDED) ||
298cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 	    !(rx_ring->igb->igb_state & IGB_STARTED))
299cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		return (NULL);
300da14cebeSEric Cheng 
301da14cebeSEric Cheng 	mutex_enter(&rx_ring->rx_lock);
302da14cebeSEric Cheng 	mp = igb_rx(rx_ring, bytes);
303da14cebeSEric Cheng 	mutex_exit(&rx_ring->rx_lock);
304da14cebeSEric Cheng 
305da14cebeSEric Cheng 	return (mp);
306da14cebeSEric Cheng }
307da14cebeSEric Cheng 
308c869993eSxy150489 /*
309c869993eSxy150489  * igb_rx - Receive the data of one ring
310c869993eSxy150489  *
311c869993eSxy150489  * This function goes throught h/w descriptor in one specified rx ring,
312c869993eSxy150489  * receives the data if the descriptor status shows the data is ready.
313c869993eSxy150489  * It returns a chain of mblks containing the received data, to be
314c869993eSxy150489  * passed up to mac_rx().
315c869993eSxy150489  */
316c869993eSxy150489 mblk_t *
igb_rx(igb_rx_ring_t * rx_ring,int poll_bytes)317da14cebeSEric Cheng igb_rx(igb_rx_ring_t *rx_ring, int poll_bytes)
318c869993eSxy150489 {
319c869993eSxy150489 	union e1000_adv_rx_desc *current_rbd;
320c869993eSxy150489 	rx_control_block_t *current_rcb;
321c869993eSxy150489 	mblk_t *mp;
322c869993eSxy150489 	mblk_t *mblk_head;
323c869993eSxy150489 	mblk_t **mblk_tail;
324c869993eSxy150489 	uint32_t rx_next;
325c869993eSxy150489 	uint32_t rx_tail;
326c869993eSxy150489 	uint32_t pkt_len;
327c869993eSxy150489 	uint32_t status_error;
328c869993eSxy150489 	uint32_t pkt_num;
329da14cebeSEric Cheng 	uint32_t total_bytes;
330c869993eSxy150489 	igb_t *igb = rx_ring->igb;
331ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	igb_rx_data_t *rx_data = rx_ring->rx_data;
332c869993eSxy150489 
333c869993eSxy150489 	mblk_head = NULL;
334c869993eSxy150489 	mblk_tail = &mblk_head;
335c869993eSxy150489 
336cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 	if (igb->igb_state & IGB_ERROR)
337cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		return (NULL);
338cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 
339c869993eSxy150489 	/*
340c869993eSxy150489 	 * Sync the receive descriptors before
341c869993eSxy150489 	 * accepting the packets
342c869993eSxy150489 	 */
343ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	DMA_SYNC(&rx_data->rbd_area, DDI_DMA_SYNC_FORKERNEL);
344c869993eSxy150489 
3458bb4b220Sgl147354 	if (igb_check_dma_handle(
346ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	    rx_data->rbd_area.dma_handle) != DDI_FM_OK) {
3478bb4b220Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
348cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		atomic_or_32(&igb->igb_state, IGB_ERROR);
349cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		return (NULL);
3508bb4b220Sgl147354 	}
3518bb4b220Sgl147354 
352c869993eSxy150489 	/*
353c869993eSxy150489 	 * Get the start point of rx bd ring which should be examined
354c869993eSxy150489 	 * during this cycle.
355c869993eSxy150489 	 */
356ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_next = rx_data->rbd_next;
357c869993eSxy150489 
358ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	current_rbd = &rx_data->rbd_ring[rx_next];
359c869993eSxy150489 	pkt_num = 0;
360da14cebeSEric Cheng 	total_bytes = 0;
361c869993eSxy150489 	status_error = current_rbd->wb.upper.status_error;
362c869993eSxy150489 	while (status_error & E1000_RXD_STAT_DD) {
363c869993eSxy150489 		/*
364c869993eSxy150489 		 * If hardware has found the errors, but the error
365c869993eSxy150489 		 * is hardware checksum error, here does not discard the
366c869993eSxy150489 		 * packet, and let upper layer compute the checksum;
367c869993eSxy150489 		 * Otherwise discard the packet.
368c869993eSxy150489 		 */
369c869993eSxy150489 		if ((status_error & E1000_RXDEXT_ERR_FRAME_ERR_MASK) ||
370c869993eSxy150489 		    !(status_error & E1000_RXD_STAT_EOP)) {
371c869993eSxy150489 			IGB_DEBUG_STAT(rx_ring->stat_frame_error);
372c869993eSxy150489 			goto rx_discard;
373c869993eSxy150489 		}
374c869993eSxy150489 
375c869993eSxy150489 		IGB_DEBUG_STAT_COND(rx_ring->stat_cksum_error,
376c869993eSxy150489 		    (status_error & E1000_RXDEXT_STATERR_TCPE) ||
377c869993eSxy150489 		    (status_error & E1000_RXDEXT_STATERR_IPE));
378c869993eSxy150489 
379c869993eSxy150489 		pkt_len = current_rbd->wb.upper.length;
380da14cebeSEric Cheng 
381da14cebeSEric Cheng 		if ((poll_bytes != IGB_NO_POLL) &&
382da14cebeSEric Cheng 		    ((pkt_len + total_bytes) > poll_bytes))
383da14cebeSEric Cheng 			break;
384da14cebeSEric Cheng 
385da14cebeSEric Cheng 		IGB_DEBUG_STAT(rx_ring->stat_pkt_cnt);
386da14cebeSEric Cheng 		total_bytes += pkt_len;
387da14cebeSEric Cheng 
388c869993eSxy150489 		mp = NULL;
389c869993eSxy150489 		/*
390c869993eSxy150489 		 * For packets with length more than the copy threshold,
391c869993eSxy150489 		 * we'll firstly try to use the existed DMA buffer to built
392c869993eSxy150489 		 * a mblk and send the mblk upstream.
393c869993eSxy150489 		 *
394c869993eSxy150489 		 * If the first method fails, or the packet length is less
395c869993eSxy150489 		 * than the copy threshold, we'll allocate a new mblk and
396c869993eSxy150489 		 * copy the packet data to the mblk.
397c869993eSxy150489 		 */
398ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		if (pkt_len > igb->rx_copy_thresh)
399ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			mp = igb_rx_bind(rx_data, rx_next, pkt_len);
400c869993eSxy150489 
401c869993eSxy150489 		if (mp == NULL)
402ac7f5757Schenlu chen - Sun Microsystems - Beijing China 			mp = igb_rx_copy(rx_data, rx_next, pkt_len);
403c869993eSxy150489 
404c869993eSxy150489 		if (mp != NULL) {
405c869993eSxy150489 			/*
406c869993eSxy150489 			 * Check h/w checksum offload status
407c869993eSxy150489 			 */
408c869993eSxy150489 			if (igb->rx_hcksum_enable)
409c869993eSxy150489 				igb_rx_assoc_hcksum(mp, status_error);
410c869993eSxy150489 
411c869993eSxy150489 			*mblk_tail = mp;
412c869993eSxy150489 			mblk_tail = &mp->b_next;
413c869993eSxy150489 		}
414c869993eSxy150489 
4150dc2366fSVenugopal Iyer 		/* Update per-ring rx statistics */
4160dc2366fSVenugopal Iyer 		rx_ring->rx_pkts++;
4170dc2366fSVenugopal Iyer 		rx_ring->rx_bytes += pkt_len;
4180dc2366fSVenugopal Iyer 
419c869993eSxy150489 rx_discard:
420c869993eSxy150489 		/*
421c869993eSxy150489 		 * Reset rx descriptor read bits
422c869993eSxy150489 		 */
423ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		current_rcb = rx_data->work_list[rx_next];
424c869993eSxy150489 		current_rbd->read.pkt_addr = current_rcb->rx_buf.dma_address;
425c869993eSxy150489 		current_rbd->read.hdr_addr = 0;
426c869993eSxy150489 
427ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		rx_next = NEXT_INDEX(rx_next, 1, rx_data->ring_size);
428c869993eSxy150489 
429c869993eSxy150489 		/*
430c869993eSxy150489 		 * The receive function is in interrupt context, so here
431ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		 * rx_limit_per_intr is used to avoid doing receiving too long
432c869993eSxy150489 		 * per interrupt.
433c869993eSxy150489 		 */
434ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		if (++pkt_num > igb->rx_limit_per_intr) {
435c869993eSxy150489 			IGB_DEBUG_STAT(rx_ring->stat_exceed_pkt);
436c869993eSxy150489 			break;
437c869993eSxy150489 		}
438c869993eSxy150489 
439ac7f5757Schenlu chen - Sun Microsystems - Beijing China 		current_rbd = &rx_data->rbd_ring[rx_next];
440c869993eSxy150489 		status_error = current_rbd->wb.upper.status_error;
441c869993eSxy150489 	}
442c869993eSxy150489 
443ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	DMA_SYNC(&rx_data->rbd_area, DDI_DMA_SYNC_FORDEV);
444c869993eSxy150489 
445ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_data->rbd_next = rx_next;
446c869993eSxy150489 
447c869993eSxy150489 	/*
448c869993eSxy150489 	 * Update the h/w tail accordingly
449c869993eSxy150489 	 */
450ac7f5757Schenlu chen - Sun Microsystems - Beijing China 	rx_tail = PREV_INDEX(rx_next, 1, rx_data->ring_size);
451c869993eSxy150489 
452c869993eSxy150489 	E1000_WRITE_REG(&igb->hw, E1000_RDT(rx_ring->index), rx_tail);
453c869993eSxy150489 
4548bb4b220Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
4558bb4b220Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
456cf8dcc9bSzhefeng xu - Sun Microsystems - Beijing China 		atomic_or_32(&igb->igb_state, IGB_ERROR);
4578bb4b220Sgl147354 	}
4588bb4b220Sgl147354 
459c869993eSxy150489 	return (mblk_head);
460c869993eSxy150489 }
461