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