xref: /titanic_50/usr/src/uts/common/io/bge/bge_send.c (revision 60c807700988885656502665e0cf8afd4b4346f7)
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 2006 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_impl2.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 
117 	srp->tc_next = slot;
118 	bge_atomic_renounce(&srp->tx_free, n);
119 
120 	/*
121 	 * Reset the watchdog count: to 0 if all buffers are
122 	 * now free, or to 1 if some are still outstanding.
123 	 * Note: non-synchonised access here means we may get
124 	 * the "wrong" answer, but only in a harmless fashion
125 	 * (i.e. we deactivate the watchdog because all buffers
126 	 * are apparently free, even though another thread may
127 	 * have claimed one before we leave here; in this case
128 	 * the watchdog will restart on the next send() call).
129 	 */
130 	bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1;
131 }
132 
133 /*
134  * Recycle all returned slots in all rings.
135  *
136  * To give priority to low-numbered rings, whenever we have recycled any
137  * slots in any ring except 0, we restart scanning again from ring 0.
138  * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the
139  * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0:
140  *
141  *	0	found some - recycle them
142  *	1..2					none found
143  *	3	found some - recycle them	and restart scan
144  *	0..9					none found
145  *	10	found some - recycle them	and restart scan
146  *	0..2					none found
147  *	3	found some more - recycle them	and restart scan
148  *	0	found some more - recycle them
149  *	0..9					none found
150  *	10	found some more - recycle them	and restart scan
151  *	0	found some more - recycle them
152  *	1..15					none found
153  *
154  * The routine returns only when a complete scan has been performed
155  * without finding any slots to recycle.
156  *
157  * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time
158  * constant and allows the compiler to optimise away the outer do-loop
159  * if only one send ring is being used.
160  */
161 void bge_recycle(bge_t *bgep, bge_status_t *bsp);
162 #pragma	no_inline(bge_recycle)
163 
164 void
165 bge_recycle(bge_t *bgep, bge_status_t *bsp)
166 {
167 	send_ring_t *srp;
168 	uint64_t ring;
169 	uint64_t tx_rings = bgep->chipid.tx_rings;
170 
171 restart:
172 	ring = 0;
173 	srp = &bgep->send[ring];
174 	do {
175 		/*
176 		 * For each ring, (srp->cons_index_p) points to the
177 		 * proper index within the status block (which has
178 		 * already been sync'd by the caller).
179 		 */
180 		ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring));
181 
182 		if (*srp->cons_index_p == srp->tc_next)
183 			continue;		/* no slots to recycle	*/
184 
185 		mutex_enter(srp->tc_lock);
186 		bge_recycle_ring(bgep, srp);
187 		mutex_exit(srp->tc_lock);
188 
189 		if (bgep->resched_needed && !bgep->resched_running) {
190 			bgep->resched_running = B_TRUE;
191 			ddi_trigger_softintr(bgep->resched_id);
192 		}
193 		/*
194 		 * Restart from ring 0, if we're not on ring 0 already.
195 		 * As H/W selects send BDs totally based on priority and
196 		 * available BDs on the higher priority ring are always
197 		 * selected first, driver should keep consistence with H/W
198 		 * and gives lower-numbered ring with higher priority.
199 		 */
200 		if (tx_rings > 1 && ring > 0)
201 			goto restart;
202 
203 		/*
204 		 * Loop over all rings (if there *are* multiple rings)
205 		 */
206 	} while (++srp, ++ring < tx_rings);
207 }
208 
209 
210 /*
211  * ========== Send-side transmit routines ==========
212  */
213 
214 /*
215  * CLAIM an already-reserved place on the next train
216  *
217  * This is the point of no return!
218  */
219 static uint64_t bge_send_claim(bge_t *bgep, send_ring_t *srp);
220 #pragma	inline(bge_send_claim)
221 
222 static uint64_t
223 bge_send_claim(bge_t *bgep, send_ring_t *srp)
224 {
225 	uint64_t slot;
226 
227 	mutex_enter(srp->tx_lock);
228 	atomic_add_64(&srp->tx_flow, 1);
229 	slot = bge_atomic_claim(&srp->tx_next, srp->desc.nslots);
230 	mutex_exit(srp->tx_lock);
231 
232 	/*
233 	 * Bump the watchdog counter, thus guaranteeing that it's
234 	 * nonzero (watchdog activated).  Note that non-synchonised
235 	 * access here means we may race with the reclaim() code
236 	 * above, but the outcome will be harmless.  At worst, the
237 	 * counter may not get reset on a partial reclaim; but the
238 	 * large trigger threshold makes false positives unlikely
239 	 */
240 	bgep->watchdog += 1;
241 
242 	return (slot);
243 }
244 
245 /*
246  * Send a message by copying it into a preallocated (and premapped) buffer
247  */
248 static enum send_status bge_send_copy(bge_t *bgep, mblk_t *mp,
249 	send_ring_t *srp, uint16_t tci);
250 #pragma	inline(bge_send_copy)
251 
252 static enum send_status
253 bge_send_copy(bge_t *bgep, mblk_t *mp, send_ring_t *srp, uint16_t tci)
254 {
255 	bge_sbd_t *hw_sbd_p;
256 	sw_sbd_t *ssbdp;
257 	mblk_t *bp;
258 	char *txb;
259 	uint64_t slot;
260 	size_t totlen;
261 	size_t mblen;
262 	uint32_t pflags;
263 
264 	BGE_TRACE(("bge_send_copy($%p, $%p, $%p, 0x%x)",
265 		(void *)bgep, (void *)mp, (void *)srp));
266 
267 	/*
268 	 * IMPORTANT:
269 	 *	Up to the point where it claims a place, a send_msg()
270 	 *	routine can indicate failure by returning SEND_FAIL.
271 	 *	Once it's claimed a place, it mustn't fail.
272 	 *
273 	 * In this version, there's no setup to be done here, and there's
274 	 * nothing that can fail, so we can go straight to claiming our
275 	 * already-reserved place on the train.
276 	 *
277 	 * This is the point of no return!
278 	 */
279 	slot = bge_send_claim(bgep, srp);
280 	ssbdp = &srp->sw_sbds[slot];
281 
282 	/*
283 	 * Copy the data into a pre-mapped buffer, which avoids the
284 	 * overhead (and complication) of mapping/unmapping STREAMS
285 	 * buffers and keeping hold of them until the DMA has completed.
286 	 *
287 	 * Because all buffers are the same size, and larger than the
288 	 * longest single valid message, we don't have to bother about
289 	 * splitting the message across multiple buffers either.
290 	 */
291 	txb = DMA_VPTR(ssbdp->pbuf);
292 	for (totlen = 0, bp = mp; bp != NULL; bp = bp->b_cont) {
293 		mblen = bp->b_wptr - bp->b_rptr;
294 		if ((totlen += mblen) <= bgep->chipid.ethmax_size) {
295 			bcopy(bp->b_rptr, txb, mblen);
296 			txb += mblen;
297 		}
298 	}
299 
300 	/*
301 	 * We'e reached the end of the chain; and we should have
302 	 * collected no more than ETHERMAX bytes into our buffer.
303 	 */
304 	ASSERT(bp == NULL);
305 	ASSERT(totlen <= bgep->chipid.ethmax_size);
306 	DMA_SYNC(ssbdp->pbuf, DDI_DMA_SYNC_FORDEV);
307 
308 	/*
309 	 * Update the hardware send buffer descriptor; then we're done.
310 	 * The return status indicates that the message can be freed
311 	 * right away, as we've already copied the contents ...
312 	 */
313 	hw_sbd_p = DMA_VPTR(ssbdp->desc);
314 	hw_sbd_p->host_buf_addr = ssbdp->pbuf.cookie.dmac_laddress;
315 	hw_sbd_p->len = totlen;
316 	hw_sbd_p->flags = SBD_FLAG_PACKET_END;
317 	if (tci != 0) {
318 		hw_sbd_p->vlan_tci = tci;
319 		hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
320 	}
321 
322 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
323 	if (pflags & HCK_IPV4_HDRCKSUM)
324 		hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
325 	if (pflags & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))
326 		hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
327 
328 	return (SEND_FREE);
329 }
330 
331 static boolean_t
332 bge_send(bge_t *bgep, mblk_t *mp)
333 {
334 	send_ring_t *srp;
335 	enum send_status status;
336 	struct ether_vlan_header *ehp;
337 	boolean_t need_strip = B_FALSE;
338 	bge_status_t *bsp;
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 	if (srp->tx_free <= 16) {
403 		bsp = DMA_VPTR(bgep->status_block);
404 		bge_recycle(bgep, bsp);
405 	}
406 	/*
407 	 * We've reserved a place :-)
408 	 * These ASSERTions check that our invariants still hold:
409 	 *	there must still be at least one free place
410 	 *	there must be at least one place NOT free (ours!)
411 	 */
412 	ASSERT(srp->tx_free > 0);
413 
414 	if ((status = bge_send_copy(bgep, mp, srp, tci)) == SEND_FAIL) {
415 		/*
416 		 * The send routine failed :(  So we have to renounce
417 		 * our reservation before returning the error.
418 		 */
419 		bge_atomic_renounce(&srp->tx_free, 1);
420 		bgep->resched_needed = B_TRUE;
421 		return (B_FALSE);
422 	}
423 
424 	/*
425 	 * The send routine succeeded; it will have updated the
426 	 * h/w ring descriptor, and the <tx_next> and <tx_flow>
427 	 * counters.
428 	 *
429 	 * Because there can be multiple concurrent threads in
430 	 * transit through this code, we only want to prod the
431 	 * hardware once the last one is departing ...
432 	 */
433 	mutex_enter(srp->tx_lock);
434 	if (--srp->tx_flow == 0) {
435 		DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
436 		bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next);
437 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
438 			bgep->bge_chip_state = BGE_CHIP_ERROR;
439 	}
440 	mutex_exit(srp->tx_lock);
441 
442 	if (status == SEND_FREE)
443 		freemsg(mp);
444 	return (B_TRUE);
445 }
446 
447 uint_t
448 bge_reschedule(caddr_t arg)
449 {
450 	bge_t *bgep;
451 
452 	bgep = (bge_t *)arg;
453 
454 	BGE_TRACE(("bge_reschedule($%p)", (void *)bgep));
455 
456 	if (bgep->bge_mac_state == BGE_MAC_STARTED && bgep->resched_needed) {
457 		mac_tx_update(bgep->macp);
458 		bgep->resched_needed = B_FALSE;
459 		bgep->resched_running = B_FALSE;
460 	}
461 
462 	return (DDI_INTR_CLAIMED);
463 }
464 
465 /*
466  * bge_m_tx() - send a chain of packets
467  */
468 mblk_t *
469 bge_m_tx(void *arg, mblk_t *mp)
470 {
471 	bge_t *bgep = arg;		/* private device info	*/
472 	mblk_t *next;
473 
474 	BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
475 
476 	ASSERT(mp != NULL);
477 	ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
478 
479 	if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
480 		BGE_DEBUG(("bge_m_tx: chip not running"));
481 		return (mp);
482 	}
483 
484 	rw_enter(bgep->errlock, RW_READER);
485 	while (mp != NULL) {
486 		next = mp->b_next;
487 		mp->b_next = NULL;
488 
489 		if (!bge_send(bgep, mp)) {
490 			mp->b_next = next;
491 			break;
492 		}
493 
494 		mp = next;
495 	}
496 	rw_exit(bgep->errlock);
497 
498 	return (mp);
499 }
500