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