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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or 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 distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 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 "sys/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 /* 75 * ========== Send-side recycle routines ========== 76 */ 77 78 /* 79 * Recycle all the completed buffers in the specified send ring up to 80 * (but not including) the consumer index in the status block. 81 * 82 * This function must advance (srp->tc_next) AND adjust (srp->tx_free) 83 * to account for the packets it has recycled. 84 * 85 * This is a trivial version that just does that and nothing more, but 86 * it suffices while there's only one method for sending messages (by 87 * copying) and that method doesn't need any special per-buffer action 88 * for recycling. 89 */ 90 static void bge_recycle_ring(bge_t *bgep, send_ring_t *srp); 91 #pragma inline(bge_recycle_ring) 92 93 static void 94 bge_recycle_ring(bge_t *bgep, send_ring_t *srp) 95 { 96 uint64_t slot; 97 uint64_t n; 98 99 _NOTE(ARGUNUSED(bgep)) 100 101 ASSERT(mutex_owned(srp->tc_lock)); 102 103 slot = *srp->cons_index_p; /* volatile */ 104 n = slot - srp->tc_next; 105 if (slot < srp->tc_next) 106 n += srp->desc.nslots; 107 108 /* 109 * We're about to release one or more places :-) 110 * These ASSERTions check that our invariants still hold: 111 * there must always be at least one free place 112 * at this point, there must be at least one place NOT free 113 * we're not about to free more places than were claimed! 114 */ 115 ASSERT(srp->tx_free > 0); 116 ASSERT(srp->tx_free < srp->desc.nslots); 117 ASSERT(srp->tx_free + n <= srp->desc.nslots); 118 119 srp->tc_next = slot; 120 bge_atomic_renounce(&srp->tx_free, n); 121 122 /* 123 * Reset the watchdog count: to 0 if all buffers are 124 * now free, or to 1 if some are still outstanding. 125 * Note: non-synchonised access here means we may get 126 * the "wrong" answer, but only in a harmless fashion 127 * (i.e. we deactivate the watchdog because all buffers 128 * are apparently free, even though another thread may 129 * have claimed one before we leave here; in this case 130 * the watchdog will restart on the next send() call). 131 */ 132 bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1; 133 } 134 135 /* 136 * Recycle all returned slots in all rings. 137 * 138 * To give priority to low-numbered rings, whenever we have recycled any 139 * slots in any ring except 0, we restart scanning again from ring 0. 140 * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the 141 * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0: 142 * 143 * 0 found some - recycle them 144 * 1..2 none found 145 * 3 found some - recycle them and restart scan 146 * 0..9 none found 147 * 10 found some - recycle them and restart scan 148 * 0..2 none found 149 * 3 found some more - recycle them and restart scan 150 * 0 found some more - recycle them 151 * 0..9 none found 152 * 10 found some more - recycle them and restart scan 153 * 0 found some more - recycle them 154 * 1..15 none found 155 * 156 * The routine returns only when a complete scan has been performed 157 * without finding any slots to recycle. 158 * 159 * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time 160 * constant and allows the compiler to optimise away the outer do-loop 161 * if only one send ring is being used. 162 */ 163 void bge_recycle(bge_t *bgep, bge_status_t *bsp); 164 #pragma no_inline(bge_recycle) 165 166 void 167 bge_recycle(bge_t *bgep, bge_status_t *bsp) 168 { 169 send_ring_t *srp; 170 uint64_t ring; 171 uint64_t tx_rings = bgep->chipid.tx_rings; 172 173 restart: 174 ring = 0; 175 srp = &bgep->send[ring]; 176 do { 177 /* 178 * For each ring, (srp->cons_index_p) points to the 179 * proper index within the status block (which has 180 * already been sync'd by the caller). 181 */ 182 ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring)); 183 184 if (*srp->cons_index_p == srp->tc_next) 185 continue; /* no slots to recycle */ 186 if (mutex_tryenter(srp->tc_lock) == 0) 187 continue; /* already in process */ 188 bge_recycle_ring(bgep, srp); 189 mutex_exit(srp->tc_lock); 190 191 if (bgep->resched_needed) 192 ddi_trigger_softintr(bgep->resched_id); 193 194 /* 195 * Restart from ring 0, if we're not on ring 0 already. 196 * As H/W selects send BDs totally based on priority and 197 * available BDs on the higher priority ring are always 198 * selected first, driver should keep consistence with H/W 199 * and gives lower-numbered ring with higher priority. 200 */ 201 if (tx_rings > 1 && ring > 0) 202 goto restart; 203 204 /* 205 * Loop over all rings (if there *are* multiple rings) 206 */ 207 } while (++srp, ++ring < tx_rings); 208 } 209 210 211 /* 212 * ========== Send-side transmit routines ========== 213 */ 214 215 /* 216 * CLAIM an already-reserved place on the next train 217 * 218 * This is the point of no return! 219 */ 220 static uint64_t bge_send_claim(bge_t *bgep, send_ring_t *srp); 221 #pragma inline(bge_send_claim) 222 223 static uint64_t 224 bge_send_claim(bge_t *bgep, send_ring_t *srp) 225 { 226 uint64_t slot; 227 228 rw_enter(srp->tx_lock, RW_READER); 229 atomic_add_64(&srp->tx_flow, 1); 230 slot = bge_atomic_claim(&srp->tx_next, srp->desc.nslots); 231 rw_exit(srp->tx_lock); 232 233 /* 234 * Bump the watchdog counter, thus guaranteeing that it's 235 * nonzero (watchdog activated). Note that non-synchonised 236 * access here means we may race with the reclaim() code 237 * above, but the outcome will be harmless. At worst, the 238 * counter may not get reset on a partial reclaim; but the 239 * large trigger threshold makes false positives unlikely 240 */ 241 bgep->watchdog += 1; 242 243 return (slot); 244 } 245 246 /* 247 * Send a message by copying it into a preallocated (and premapped) buffer 248 */ 249 static enum send_status bge_send_copy(bge_t *bgep, mblk_t *mp, 250 send_ring_t *srp, uint16_t tci); 251 #pragma inline(bge_send_copy) 252 253 static enum send_status 254 bge_send_copy(bge_t *bgep, mblk_t *mp, send_ring_t *srp, uint16_t tci) 255 { 256 bge_sbd_t *hw_sbd_p; 257 sw_sbd_t *ssbdp; 258 mblk_t *bp; 259 char *txb; 260 uint64_t slot; 261 size_t totlen; 262 size_t mblen; 263 uint32_t pflags; 264 265 BGE_TRACE(("bge_send_copy($%p, $%p, $%p, 0x%x)", 266 (void *)bgep, (void *)mp, (void *)srp)); 267 268 /* 269 * IMPORTANT: 270 * Up to the point where it claims a place, a send_msg() 271 * routine can indicate failure by returning SEND_FAIL. 272 * Once it's claimed a place, it mustn't fail. 273 * 274 * In this version, there's no setup to be done here, and there's 275 * nothing that can fail, so we can go straight to claiming our 276 * already-reserved place on the train. 277 * 278 * This is the point of no return! 279 */ 280 slot = bge_send_claim(bgep, srp); 281 ssbdp = &srp->sw_sbds[slot]; 282 283 /* 284 * Copy the data into a pre-mapped buffer, which avoids the 285 * overhead (and complication) of mapping/unmapping STREAMS 286 * buffers and keeping hold of them until the DMA has completed. 287 * 288 * Because all buffers are the same size, and larger than the 289 * longest single valid message, we don't have to bother about 290 * splitting the message across multiple buffers either. 291 */ 292 txb = DMA_VPTR(ssbdp->pbuf); 293 for (totlen = 0, bp = mp; bp != NULL; bp = bp->b_cont) { 294 mblen = bp->b_wptr - bp->b_rptr; 295 if ((totlen += mblen) <= bgep->chipid.ethmax_size) { 296 bcopy(bp->b_rptr, txb, mblen); 297 txb += mblen; 298 } 299 } 300 301 /* 302 * We'e reached the end of the chain; and we should have 303 * collected no more than ETHERMAX bytes into our buffer. 304 */ 305 ASSERT(bp == NULL); 306 ASSERT(totlen <= bgep->chipid.ethmax_size); 307 DMA_SYNC(ssbdp->pbuf, DDI_DMA_SYNC_FORDEV); 308 309 /* 310 * Update the hardware send buffer descriptor; then we're done. 311 * The return status indicates that the message can be freed 312 * right away, as we've already copied the contents ... 313 */ 314 hw_sbd_p = DMA_VPTR(ssbdp->desc); 315 hw_sbd_p->host_buf_addr = ssbdp->pbuf.cookie.dmac_laddress; 316 hw_sbd_p->len = totlen; 317 hw_sbd_p->flags = SBD_FLAG_PACKET_END; 318 if (tci != 0) { 319 hw_sbd_p->vlan_tci = tci; 320 hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG; 321 } 322 323 hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags); 324 if (pflags & HCK_IPV4_HDRCKSUM) 325 hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM; 326 if (pflags & HCK_FULLCKSUM) 327 hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM; 328 329 return (SEND_FREE); 330 } 331 332 static boolean_t 333 bge_send(bge_t *bgep, mblk_t *mp) 334 { 335 send_ring_t *srp; 336 enum send_status status; 337 struct ether_vlan_header *ehp; 338 boolean_t need_strip = B_FALSE; 339 uint16_t tci; 340 uint_t ring = 0; 341 342 ASSERT(mp->b_next == NULL); 343 344 /* 345 * Determine if the packet is VLAN tagged. 346 */ 347 ASSERT(MBLKL(mp) >= sizeof (struct ether_header)); 348 ehp = (struct ether_vlan_header *)mp->b_rptr; 349 350 if (ehp->ether_tpid == htons(VLAN_TPID)) { 351 if (MBLKL(mp) < sizeof (struct ether_vlan_header)) { 352 uint32_t pflags; 353 354 /* 355 * Need to preserve checksum flags across pullup. 356 */ 357 hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, 358 NULL, &pflags); 359 360 if (!pullupmsg(mp, 361 sizeof (struct ether_vlan_header))) { 362 BGE_DEBUG(("bge_send: pullup failure")); 363 bgep->resched_needed = B_TRUE; 364 return (B_FALSE); 365 } 366 367 (void) hcksum_assoc(mp, NULL, NULL, NULL, NULL, NULL, 368 NULL, pflags, KM_NOSLEEP); 369 } 370 371 ehp = (struct ether_vlan_header *)mp->b_rptr; 372 need_strip = B_TRUE; 373 } 374 375 /* 376 * Try to reserve a place in the chosen ring. Shouldn't try next 377 * higher-numbered (lower-priority) ring, if there aren't any 378 * available. Otherwise, packets with same priority may get 379 * transmission starvation. 380 */ 381 srp = &bgep->send[ring]; 382 if (!bge_atomic_reserve(&srp->tx_free, 1)) { 383 BGE_DEBUG(("bge_send: no free slots")); 384 bgep->resched_needed = B_TRUE; 385 return (B_FALSE); 386 } 387 388 /* 389 * Now that we know that there is space to transmit the packet 390 * strip any VLAN tag that is present. 391 */ 392 if (need_strip) { 393 tci = ntohs(ehp->ether_tci); 394 395 (void) memmove(mp->b_rptr + VLAN_TAGSZ, mp->b_rptr, 396 2 * ETHERADDRL); 397 mp->b_rptr += VLAN_TAGSZ; 398 } else { 399 tci = 0; 400 } 401 402 /* 403 * We've reserved a place :-) 404 * These ASSERTions check that our invariants still hold: 405 * there must still be at least one free place 406 * there must be at least one place NOT free (ours!) 407 */ 408 ASSERT(srp->tx_free > 0); 409 ASSERT(srp->tx_free < srp->desc.nslots); 410 411 if ((status = bge_send_copy(bgep, mp, srp, tci)) == SEND_FAIL) { 412 /* 413 * The send routine failed :( So we have to renounce 414 * our reservation before returning the error. 415 */ 416 bge_atomic_renounce(&srp->tx_free, 1); 417 bgep->resched_needed = B_TRUE; 418 return (B_FALSE); 419 } 420 421 /* 422 * The send routine succeeded; it will have updated the 423 * h/w ring descriptor, and the <tx_next> and <tx_flow> 424 * counters. 425 * 426 * Because there can be multiple concurrent threads in 427 * transit through this code, we only want to prod the 428 * hardware once the last one is departing ... 429 */ 430 rw_enter(srp->tx_lock, RW_WRITER); 431 if (--srp->tx_flow == 0) { 432 DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV); 433 bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next); 434 } 435 rw_exit(srp->tx_lock); 436 437 if (status == SEND_FREE) 438 freemsg(mp); 439 return (B_TRUE); 440 } 441 442 uint_t 443 bge_reschedule(caddr_t arg) 444 { 445 bge_t *bgep; 446 uint_t rslt; 447 448 bgep = (bge_t *)arg; 449 rslt = DDI_INTR_UNCLAIMED; 450 451 BGE_TRACE(("bge_reschedule($%p)", (void *)bgep)); 452 453 if (bgep->bge_mac_state == BGE_MAC_STARTED && bgep->resched_needed) { 454 mac_tx_update(bgep->macp); 455 bgep->resched_needed = B_FALSE; 456 rslt = DDI_INTR_CLAIMED; 457 } 458 459 return (rslt); 460 } 461 462 /* 463 * bge_m_tx() - send a chain of packets 464 */ 465 mblk_t * 466 bge_m_tx(void *arg, mblk_t *mp) 467 { 468 bge_t *bgep = arg; /* private device info */ 469 mblk_t *next; 470 471 BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp)); 472 473 ASSERT(mp != NULL); 474 ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED); 475 476 if (bgep->bge_chip_state != BGE_CHIP_RUNNING) { 477 BGE_DEBUG(("bge_m_tx: chip not running")); 478 return (mp); 479 } 480 481 while (mp != NULL) { 482 next = mp->b_next; 483 mp->b_next = NULL; 484 485 if (!bge_send(bgep, mp)) { 486 mp->b_next = next; 487 break; 488 } 489 490 mp = next; 491 } 492 493 return (mp); 494 } 495