1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include "bge_impl.h" 30 31 32 /* 33 * The transmit-side code uses an allocation process which is similar 34 * to some theme park roller-coaster rides, where riders sit in cars 35 * that can go individually, but work better in a train. 36 * 37 * 1) RESERVE a place - this doesn't refer to any specific car or 38 * seat, just that you will get a ride. The attempt to RESERVE a 39 * place can fail if all spaces in all cars are already committed. 40 * 41 * 2) Prepare yourself; this may take an arbitrary (but not unbounded) 42 * time, and you can back out at this stage, in which case you must 43 * give up (RENOUNCE) your place. 44 * 45 * 3) CLAIM your space - a specific car (the next sequentially 46 * numbered one) is allocated at this stage, and is guaranteed 47 * to be part of the next train to depart. Once you've done 48 * this, you can't back out, nor wait for any external event 49 * or resource. 50 * 51 * 4) Occupy your car - when all CLAIMED cars are OCCUPIED, they 52 * all depart together as a single train! 53 * 54 * 5) At the end of the ride, you climb out of the car and RENOUNCE 55 * your right to it, so that it can be recycled for another rider. 56 * 57 * For each rider, these have to occur in this order, but the riders 58 * don't have to stay in the same order at each stage. In particular, 59 * they may overtake each other between RESERVING a place and CLAIMING 60 * it, or between CLAIMING and OCCUPYING a space. 61 * 62 * Once a car is CLAIMED, the train currently being assembled can't go 63 * without that car (this guarantees that the cars in a single train 64 * make up a consecutively-numbered set). Therefore, when any train 65 * leaves, we know there can't be any riders in transit between CLAIMING 66 * and OCCUPYING their cars. There can be some who have RESERVED but 67 * not yet CLAIMED their places. That's OK, though, because they'll go 68 * into the next train. 69 */ 70 71 #define BGE_DBG BGE_DBG_SEND /* debug flag for this code */ 72 73 /* 74 * ========== Send-side recycle routines ========== 75 */ 76 77 /* 78 * Recycle all the completed buffers in the specified send ring up to 79 * (but not including) the consumer index in the status block. 80 * 81 * This function must advance (srp->tc_next) AND adjust (srp->tx_free) 82 * to account for the packets it has recycled. 83 * 84 * This is a trivial version that just does that and nothing more, but 85 * it suffices while there's only one method for sending messages (by 86 * copying) and that method doesn't need any special per-buffer action 87 * for recycling. 88 */ 89 static void bge_recycle_ring(bge_t *bgep, send_ring_t *srp); 90 #pragma inline(bge_recycle_ring) 91 92 static void 93 bge_recycle_ring(bge_t *bgep, send_ring_t *srp) 94 { 95 sw_sbd_t *ssbdp; 96 bge_queue_item_t *buf_item; 97 bge_queue_item_t *buf_item_head; 98 bge_queue_item_t *buf_item_tail; 99 bge_queue_t *txbuf_queue; 100 uint64_t slot; 101 uint64_t n; 102 103 ASSERT(mutex_owned(srp->tc_lock)); 104 105 /* 106 * We're about to release one or more places :-) 107 * These ASSERTions check that our invariants still hold: 108 * there must always be at least one free place 109 * at this point, there must be at least one place NOT free 110 * we're not about to free more places than were claimed! 111 */ 112 ASSERT(srp->tx_free > 0); 113 ASSERT(srp->tx_free < srp->desc.nslots); 114 115 buf_item_head = buf_item_tail = NULL; 116 for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p; 117 slot = NEXT(slot, srp->desc.nslots)) { 118 ssbdp = &srp->sw_sbds[slot]; 119 ASSERT(ssbdp->pbuf != NULL); 120 buf_item = ssbdp->pbuf; 121 if (buf_item_head == NULL) 122 buf_item_head = buf_item_tail = buf_item; 123 else { 124 buf_item_tail->next = buf_item; 125 buf_item_tail = buf_item; 126 } 127 ssbdp->pbuf = NULL; 128 n++; 129 } 130 if (n == 0) 131 return; 132 133 /* 134 * Update recycle index and free tx BD number 135 */ 136 srp->tc_next = slot; 137 ASSERT(srp->tx_free + n <= srp->desc.nslots); 138 bge_atomic_renounce(&srp->tx_free, n); 139 140 /* 141 * Reset the watchdog count: to 0 if all buffers are 142 * now free, or to 1 if some are still outstanding. 143 * Note: non-synchonised access here means we may get 144 * the "wrong" answer, but only in a harmless fashion 145 * (i.e. we deactivate the watchdog because all buffers 146 * are apparently free, even though another thread may 147 * have claimed one before we leave here; in this case 148 * the watchdog will restart on the next send() call). 149 */ 150 bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1; 151 152 /* 153 * Return tx buffers to buffer push queue 154 */ 155 txbuf_queue = srp->txbuf_push_queue; 156 mutex_enter(txbuf_queue->lock); 157 buf_item_tail->next = txbuf_queue->head; 158 txbuf_queue->head = buf_item_head; 159 txbuf_queue->count += n; 160 mutex_exit(txbuf_queue->lock); 161 162 /* 163 * Check if we need exchange the tx buffer push and pop queue 164 */ 165 if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) && 166 (srp->txbuf_pop_queue->count < txbuf_queue->count)) { 167 srp->txbuf_push_queue = srp->txbuf_pop_queue; 168 srp->txbuf_pop_queue = txbuf_queue; 169 } 170 171 if (srp->tx_flow != 0 || bgep->tx_resched_needed) 172 ddi_trigger_softintr(bgep->drain_id); 173 } 174 175 /* 176 * Recycle all returned slots in all rings. 177 * 178 * To give priority to low-numbered rings, whenever we have recycled any 179 * slots in any ring except 0, we restart scanning again from ring 0. 180 * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the 181 * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0: 182 * 183 * 0 found some - recycle them 184 * 1..2 none found 185 * 3 found some - recycle them and restart scan 186 * 0..9 none found 187 * 10 found some - recycle them and restart scan 188 * 0..2 none found 189 * 3 found some more - recycle them and restart scan 190 * 0 found some more - recycle them 191 * 0..9 none found 192 * 10 found some more - recycle them and restart scan 193 * 0 found some more - recycle them 194 * 1..15 none found 195 * 196 * The routine returns only when a complete scan has been performed 197 * without finding any slots to recycle. 198 * 199 * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time 200 * constant and allows the compiler to optimise away the outer do-loop 201 * if only one send ring is being used. 202 */ 203 void bge_recycle(bge_t *bgep, bge_status_t *bsp); 204 #pragma no_inline(bge_recycle) 205 206 void 207 bge_recycle(bge_t *bgep, bge_status_t *bsp) 208 { 209 send_ring_t *srp; 210 uint64_t ring; 211 uint64_t tx_rings = bgep->chipid.tx_rings; 212 213 restart: 214 ring = 0; 215 srp = &bgep->send[ring]; 216 do { 217 /* 218 * For each ring, (srp->cons_index_p) points to the 219 * proper index within the status block (which has 220 * already been sync'd by the caller). 221 */ 222 ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring)); 223 224 if (*srp->cons_index_p == srp->tc_next) 225 continue; /* no slots to recycle */ 226 if (mutex_tryenter(srp->tc_lock) == 0) 227 continue; /* already in process */ 228 bge_recycle_ring(bgep, srp); 229 mutex_exit(srp->tc_lock); 230 231 /* 232 * Restart from ring 0, if we're not on ring 0 already. 233 * As H/W selects send BDs totally based on priority and 234 * available BDs on the higher priority ring are always 235 * selected first, driver should keep consistence with H/W 236 * and gives lower-numbered ring with higher priority. 237 */ 238 if (tx_rings > 1 && ring > 0) 239 goto restart; 240 241 /* 242 * Loop over all rings (if there *are* multiple rings) 243 */ 244 } while (++srp, ++ring < tx_rings); 245 } 246 247 248 /* 249 * ========== Send-side transmit routines ========== 250 */ 251 #define TCP_CKSUM_OFFSET 16 252 #define UDP_CKSUM_OFFSET 6 253 254 static void 255 bge_pseudo_cksum(uint8_t *buf) 256 { 257 uint32_t cksum; 258 uint16_t iphl; 259 uint16_t proto; 260 261 /* 262 * Point it to the ip header. 263 */ 264 buf += sizeof (struct ether_header); 265 266 /* 267 * Calculate the pseudo-header checksum. 268 */ 269 iphl = 4 * (buf[0] & 0xF); 270 cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl; 271 cksum += proto = buf[9]; 272 cksum += (((uint16_t)buf[12])<<8) + buf[13]; 273 cksum += (((uint16_t)buf[14])<<8) + buf[15]; 274 cksum += (((uint16_t)buf[16])<<8) + buf[17]; 275 cksum += (((uint16_t)buf[18])<<8) + buf[19]; 276 cksum = (cksum>>16) + (cksum & 0xFFFF); 277 cksum = (cksum>>16) + (cksum & 0xFFFF); 278 279 /* 280 * Point it to the TCP/UDP header, and 281 * update the checksum field. 282 */ 283 buf += iphl + ((proto == IPPROTO_TCP) ? 284 TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET); 285 286 *(uint16_t *)buf = htons((uint16_t)cksum); 287 } 288 289 static bge_queue_item_t * 290 bge_get_txbuf(bge_t *bgep, send_ring_t *srp) 291 { 292 bge_queue_item_t *txbuf_item; 293 bge_queue_t *txbuf_queue; 294 295 txbuf_queue = srp->txbuf_pop_queue; 296 mutex_enter(txbuf_queue->lock); 297 if (txbuf_queue->count == 0) { 298 mutex_exit(txbuf_queue->lock); 299 txbuf_queue = srp->txbuf_push_queue; 300 mutex_enter(txbuf_queue->lock); 301 if (txbuf_queue->count == 0) { 302 mutex_exit(txbuf_queue->lock); 303 /* Try to allocate more tx buffers */ 304 if (srp->tx_array < srp->tx_array_max) { 305 mutex_enter(srp->tx_lock); 306 txbuf_item = bge_alloc_txbuf_array(bgep, srp); 307 mutex_exit(srp->tx_lock); 308 } else 309 txbuf_item = NULL; 310 return (txbuf_item); 311 } 312 } 313 txbuf_item = txbuf_queue->head; 314 txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next; 315 txbuf_queue->count--; 316 mutex_exit(txbuf_queue->lock); 317 txbuf_item->next = NULL; 318 319 return (txbuf_item); 320 } 321 322 static void bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp); 323 #pragma inline(bge_send_fill_txbd) 324 325 static void 326 bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp) 327 { 328 bge_sbd_t *hw_sbd_p; 329 sw_sbd_t *ssbdp; 330 bge_queue_item_t *txbuf_item; 331 sw_txbuf_t *txbuf; 332 uint64_t slot; 333 334 ASSERT(mutex_owned(srp->tx_lock)); 335 336 /* 337 * Go straight to claiming our already-reserved places 338 * on the train! 339 */ 340 ASSERT(pktp->txbuf_item != NULL); 341 txbuf_item = pktp->txbuf_item; 342 txbuf = txbuf_item->item; 343 slot = srp->tx_next; 344 ssbdp = &srp->sw_sbds[slot]; 345 hw_sbd_p = DMA_VPTR(ssbdp->desc); 346 hw_sbd_p->flags = 0; 347 ASSERT(txbuf->copy_len != 0); 348 (void) ddi_dma_sync(txbuf->buf.dma_hdl, 0, 349 txbuf->copy_len, DDI_DMA_SYNC_FORDEV); 350 ASSERT(ssbdp->pbuf == NULL); 351 ssbdp->pbuf = txbuf_item; 352 srp->tx_next = NEXT(slot, srp->desc.nslots); 353 pktp->txbuf_item = NULL; 354 355 /* 356 * Setting hardware send buffer descriptor 357 */ 358 hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress; 359 hw_sbd_p->len = txbuf->copy_len; 360 if (pktp->vlan_tci != 0) { 361 hw_sbd_p->vlan_tci = pktp->vlan_tci; 362 hw_sbd_p->host_buf_addr += VLAN_TAGSZ; 363 hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG; 364 } 365 if (pktp->pflags & HCK_IPV4_HDRCKSUM) 366 hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM; 367 if (pktp->pflags & HCK_FULLCKSUM) 368 hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM; 369 hw_sbd_p->flags |= SBD_FLAG_PACKET_END; 370 } 371 372 /* 373 * Send a message by copying it into a preallocated (and premapped) buffer 374 */ 375 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp); 376 #pragma inline(bge_send_copy) 377 378 static void 379 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp) 380 { 381 mblk_t *bp; 382 uint32_t mblen; 383 char *pbuf; 384 385 txbuf->copy_len = 0; 386 pbuf = DMA_VPTR(txbuf->buf); 387 for (bp = mp; bp != NULL; bp = bp->b_cont) { 388 if ((mblen = MBLKL(bp)) == 0) 389 continue; 390 ASSERT(txbuf->copy_len + mblen <= 391 bgep->chipid.snd_buff_size); 392 bcopy(bp->b_rptr, pbuf, mblen); 393 pbuf += mblen; 394 txbuf->copy_len += mblen; 395 } 396 } 397 398 /* 399 * Fill the Tx buffer descriptors and trigger the h/w transmission 400 */ 401 static void 402 bge_send_serial(bge_t *bgep, send_ring_t *srp) 403 { 404 send_pkt_t *pktp; 405 uint64_t txfill_next; 406 uint32_t count; 407 uint32_t tx_next; 408 sw_sbd_t *ssbdp; 409 bge_status_t *bsp; 410 411 /* 412 * Try to hold the tx lock: 413 * If we are in an interrupt context, use mutex_enter() to 414 * ensure quick response for tx in interrupt context; 415 * Otherwise, use mutex_tryenter() to serialize this h/w tx 416 * BD filling and transmission triggering task. 417 */ 418 if (servicing_interrupt() != 0) 419 mutex_enter(srp->tx_lock); 420 else if (mutex_tryenter(srp->tx_lock) == 0) 421 return; /* already in process */ 422 423 bsp = DMA_VPTR(bgep->status_block); 424 txfill_next = srp->txfill_next; 425 start_tx: 426 tx_next = srp->tx_next; 427 ssbdp = &srp->sw_sbds[tx_next]; 428 for (count = 0; count < bgep->param_drain_max; ++count) { 429 pktp = &srp->pktp[txfill_next]; 430 if (!pktp->tx_ready) { 431 if (count == 0) 432 srp->tx_block++; 433 break; 434 } 435 436 /* 437 * If there are no enough BDs: try to recycle more 438 */ 439 if (srp->tx_free <= 1) 440 bge_recycle(bgep, bsp); 441 442 /* 443 * Reserved required BDs: 1 is enough 444 */ 445 if (!bge_atomic_reserve(&srp->tx_free, 1)) { 446 srp->tx_nobd++; 447 break; 448 } 449 450 /* 451 * Filling the tx BD 452 */ 453 bge_send_fill_txbd(srp, pktp); 454 txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX); 455 pktp->tx_ready = B_FALSE; 456 } 457 458 /* 459 * Trigger h/w to start transmission. 460 */ 461 if (count != 0) { 462 bge_atomic_sub64(&srp->tx_flow, count); 463 if (tx_next + count > srp->desc.nslots) { 464 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0, 465 (srp->desc.nslots - tx_next) * sizeof (bge_sbd_t), 466 DDI_DMA_SYNC_FORDEV); 467 count -= srp->desc.nslots - tx_next; 468 ssbdp = &srp->sw_sbds[0]; 469 } 470 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0, 471 count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV); 472 bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next); 473 srp->txfill_next = txfill_next; 474 bgep->watchdog++; 475 if (srp->tx_flow != 0 && srp->tx_free > 1) 476 goto start_tx; 477 } 478 479 mutex_exit(srp->tx_lock); 480 } 481 482 static boolean_t 483 bge_send(bge_t *bgep, mblk_t *mp) 484 { 485 uint_t ring = 0; /* use ring 0 */ 486 send_ring_t *srp; 487 struct ether_vlan_header *ehp; 488 bge_queue_item_t *txbuf_item; 489 sw_txbuf_t *txbuf; 490 send_pkt_t *pktp; 491 uint64_t pkt_slot; 492 uint16_t vlan_tci; 493 uint32_t pflags; 494 char *pbuf; 495 496 ASSERT(mp->b_next == NULL); 497 srp = &bgep->send[ring]; 498 499 /* 500 * Get a s/w tx buffer first 501 */ 502 txbuf_item = bge_get_txbuf(bgep, srp); 503 if (txbuf_item == NULL) { 504 /* no tx buffer available */ 505 srp->tx_nobuf++; 506 bgep->tx_resched_needed = B_TRUE; 507 bge_send_serial(bgep, srp); 508 return (B_FALSE); 509 } 510 511 /* 512 * Copy all mp fragments to the pkt buffer 513 */ 514 txbuf = txbuf_item->item; 515 bge_send_copy(bgep, txbuf, mp); 516 517 /* 518 * Determine if the packet is VLAN tagged. 519 */ 520 ASSERT(txbuf->copy_len >= sizeof (struct ether_header)); 521 pbuf = DMA_VPTR(txbuf->buf); 522 523 ehp = (struct ether_vlan_header *)pbuf; 524 if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) { 525 /* Strip the vlan tag */ 526 vlan_tci = ntohs(ehp->ether_tci); 527 pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL); 528 txbuf->copy_len -= VLAN_TAGSZ; 529 } else 530 vlan_tci = 0; 531 532 /* 533 * Retrieve checksum offloading info. 534 */ 535 hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags); 536 537 /* 538 * Calculate pseudo checksum if needed. 539 */ 540 if ((pflags & HCK_FULLCKSUM) && 541 (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)) 542 bge_pseudo_cksum((uint8_t *)pbuf); 543 544 /* 545 * Packet buffer is ready to send: get and fill pkt info 546 */ 547 pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX); 548 pktp = &srp->pktp[pkt_slot]; 549 ASSERT(pktp->txbuf_item == NULL); 550 pktp->txbuf_item = txbuf_item; 551 pktp->vlan_tci = vlan_tci; 552 pktp->pflags = pflags; 553 atomic_inc_64(&srp->tx_flow); 554 ASSERT(pktp->tx_ready == B_FALSE); 555 pktp->tx_ready = B_TRUE; 556 557 /* 558 * Filling the h/w bd and trigger the h/w to start transmission 559 */ 560 bge_send_serial(bgep, srp); 561 562 /* 563 * We've copied the contents, the message can be freed right away 564 */ 565 freemsg(mp); 566 567 return (B_TRUE); 568 } 569 570 uint_t 571 bge_send_drain(caddr_t arg) 572 { 573 uint_t ring = 0; /* use ring 0 */ 574 bge_t *bgep; 575 send_ring_t *srp; 576 577 bgep = (bge_t *)arg; 578 BGE_TRACE(("bge_send_drain($%p)", (void *)bgep)); 579 580 srp = &bgep->send[ring]; 581 bge_send_serial(bgep, srp); 582 583 if (bgep->tx_resched_needed && 584 (srp->tx_flow < srp->tx_buffers_low) && 585 (bgep->bge_mac_state == BGE_MAC_STARTED)) { 586 mac_tx_update(bgep->mh); 587 bgep->tx_resched_needed = B_FALSE; 588 bgep->tx_resched++; 589 } 590 591 return (DDI_INTR_CLAIMED); 592 } 593 594 /* 595 * bge_m_tx() - send a chain of packets 596 */ 597 mblk_t * 598 bge_m_tx(void *arg, mblk_t *mp) 599 { 600 bge_t *bgep = arg; /* private device info */ 601 mblk_t *next; 602 603 BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp)); 604 605 ASSERT(mp != NULL); 606 ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED); 607 608 rw_enter(bgep->errlock, RW_READER); 609 if (bgep->bge_chip_state != BGE_CHIP_RUNNING) { 610 BGE_DEBUG(("bge_m_tx: chip not running")); 611 freemsgchain(mp); 612 mp = NULL; 613 } 614 615 while (mp != NULL) { 616 next = mp->b_next; 617 mp->b_next = NULL; 618 619 if (!bge_send(bgep, mp)) { 620 mp->b_next = next; 621 break; 622 } 623 624 mp = next; 625 } 626 rw_exit(bgep->errlock); 627 628 return (mp); 629 } 630