3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/sys/dev/hfa/fore_output.c,v 1.5 2000/01/15 21:01:04 mks Exp $
27 * @(#) $DragonFly: src/sys/dev/atm/hfa/fore_output.c,v 1.5 2005/06/02 21:36:09 dillon Exp $
31 * FORE Systems 200-Series Adapter Support
32 * ---------------------------------------
34 * PDU output processing
38 #include "fore_include.h"
43 static KBuffer * fore_xmit_segment (Fore_unit *, KBuffer *,
44 H_xmit_queue *, int *, int *);
45 static void fore_seg_dma_free (H_xmit_queue *, KBuffer *, int);
51 * This function is called via the common driver code after receiving a
52 * stack *_DATA* command. The common code has already validated most of
53 * the request so we just need to check a few more Fore-specific details.
54 * Then we just build a transmit descriptor request for the PDU and issue
55 * the command to the CP.
58 * cup pointer to device common unit
59 * cvp pointer to common VCC entry
60 * m pointer to output PDU buffer chain head
67 fore_output(cup, cvp, m)
72 Fore_unit *fup = (Fore_unit *)cup;
73 Fore_vcc *fvp = (Fore_vcc *)cvp;
78 int retry, nsegs, pdulen;
82 atm_dev_pdu_print(cup, cvp, m, "fore_output");
85 vcp = fvp->fv_connvc->cvc_vcc;
88 * If we're still waiting for activation to finish, delay for
89 * a little while before we toss the PDU
91 if (fvp->fv_state == CVS_INITED) {
93 while (retry-- && (fvp->fv_state == CVS_INITED))
95 if (fvp->fv_state != CVS_ACTIVE) {
97 * Activation still hasn't finished, oh well....
99 fup->fu_stats->st_drv.drv_xm_notact++;
102 vcp->vc_nif->nif_if.if_oerrors++;
109 * Queue PDU at end of transmit queue
111 * If queue is full we'll delay a bit before tossing the PDU
114 hxp = fup->fu_xmit_tail;
115 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
117 fup->fu_stats->st_drv.drv_xm_full++;
122 DEVICE_LOCK((Cmn_unit *)fup);
123 fore_xmit_drain(fup);
124 DEVICE_UNLOCK((Cmn_unit *)fup);
126 } while (--retry && (!((*hxp->hxq_status) & QSTAT_FREE)));
128 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
130 * Queue is still full, bye-bye PDU
132 fup->fu_pif.pif_oerrors++;
135 vcp->vc_nif->nif_if.if_oerrors++;
143 * We've got a free transmit queue entry
147 * Now build the transmit segment descriptors for this PDU
149 m = fore_xmit_segment(fup, m, hxp, &nsegs, &pdulen);
152 * The build failed, buffer chain has been freed
156 vcp->vc_nif->nif_if.if_oerrors++;
162 * Set up the descriptor header
164 xdp = hxp->hxq_descr;
165 xdp->xd_cell_hdr = ATM_HDR_SET(vcp->vc_vpi, vcp->vc_vci, 0, 0);
166 xdp->xd_spec = XDS_SET_SPEC(0, fvp->fv_aal, nsegs, pdulen);
167 xdp->xd_rate = FORE_DEF_RATE;
170 * Everything is ready to go, so officially claim the host queue
171 * entry and setup the CP-resident queue entry. The CP will grab
172 * the PDU when the descriptor pointer is set.
174 fup->fu_xmit_tail = hxp->hxq_next;
177 (*hxp->hxq_status) = QSTAT_PENDING;
178 cqp = hxp->hxq_cpelem;
179 cqp->cq_descr = (CP_dma)
180 CP_WRITE((u_long)hxp->hxq_descr_dma | XMIT_SEGS_TO_BLKS(nsegs));
185 * See if there are any completed queue entries
187 DEVICE_LOCK((Cmn_unit *)fup);
188 fore_xmit_drain(fup);
189 DEVICE_UNLOCK((Cmn_unit *)fup);
194 * Build Transmit Segment Descriptors
196 * This function will take a supplied buffer chain of data to be transmitted
197 * and build the transmit segment descriptors for the data. This will include
198 * the dreaded operation of ensuring that the data for each transmit segment
199 * is full-word aligned and (except for the last segment) is an integral number
200 * of words in length. If the data isn't already aligned and sized as
201 * required, then the data must be shifted (copied) into place - a sure
202 * performance killer. Note that we rely on the fact that all buffer data
203 * areas are allocated with (at least) full-word alignments/lengths.
205 * If any errors are encountered, the buffer chain will be freed.
208 * fup pointer to device unit
209 * m pointer to output PDU buffer chain head
210 * hxp pointer to host transmit queue entry
211 * segp pointer to return the number of transmit segments
212 * lenp pointer to return the pdu length
215 * m build successful, pointer to (possibly new) head of
216 * output PDU buffer chain
217 * NULL build failed, buffer chain freed
221 fore_xmit_segment(fup, m, hxp, segp, lenp)
228 Xmit_descr *xdp = hxp->hxq_descr;
231 KBuffer *m0, *m1, *mprev;
234 int pdulen, nsegs, len, align;
241 sdmap = hxp->hxq_dma;
247 * Loop thru each buffer in the chain, performing the necessary
248 * data positioning and then building a segment descriptor for
253 * Get rid of any zero-length buffers
255 if (KB_LEN(m) == 0) {
257 KB_UNLINK(m, mprev, m1);
259 KB_UNLINKHEAD(m, m1);
267 * Make sure we don't try to use too many segments
269 if (nsegs >= XMIT_MAX_SEGS) {
271 * First, free already allocated DMA addresses
273 fore_seg_dma_free(hxp, m0, nsegs);
276 * Try to compress buffer chain (but only once)
283 fup->fu_stats->st_drv.drv_xm_maxpdu++;
285 m = atm_dev_compress(m0);
291 * Build segment descriptors for compressed chain
299 * Get start of data onto full-word alignment
301 KB_DATASTART(m, cp, caddr_t);
302 if ((align = ((u_int)cp) & (XMIT_SEG_ALIGN - 1)) != 0) {
304 * Gotta slide the data up
306 fup->fu_stats->st_drv.drv_xm_segnoal++;
308 KM_COPY(cp, bfr, KB_LEN(m));
309 KB_HEADMOVE(m, -align);
312 * Data already aligned
318 * Now work on getting the data length correct
321 while ((align = (len & (XMIT_SEG_ALIGN - 1))) &&
325 * Have to move some data from following buffer(s)
326 * to word-fill this buffer
328 int ncopy = MIN(XMIT_SEG_ALIGN - align, KB_LEN(m1));
332 * Move data to current buffer
336 fup->fu_stats->st_drv.drv_xm_seglen++;
337 KB_DATASTART(m1, cp, caddr_t);
339 KB_HEADADJ(m1, -ncopy);
340 KB_TAILADJ(m, ncopy);
348 * If we've drained the buffer, free it
350 if (KB_LEN(m1) == 0) {
353 KB_UNLINK(m1, m, m2);
358 * Finally, build the segment descriptor
362 * Round last segment to fullword length (if needed)
364 if (len & (XMIT_SEG_ALIGN - 1))
365 xsp->xsd_len = KB_LEN(m) =
366 (len + XMIT_SEG_ALIGN) & ~(XMIT_SEG_ALIGN - 1);
368 xsp->xsd_len = KB_LEN(m) = len;
371 * Get a DMA address for the data
373 dma = DMA_GET_ADDR(bfr, xsp->xsd_len, XMIT_SEG_ALIGN, 0);
375 fup->fu_stats->st_drv.drv_xm_segdma++;
376 fore_seg_dma_free(hxp, m0, nsegs);
382 * Now we're really ready to call it a segment
384 *sdmap++ = xsp->xsd_buffer = (H_dma) dma;
387 * Bump counters and get ready for next buffer
397 * Validate PDU length
399 if (pdulen > XMIT_MAX_PDULEN) {
400 fup->fu_stats->st_drv.drv_xm_maxpdu++;
401 fore_seg_dma_free(hxp, m0, nsegs);
407 * Return the good news to the caller
417 * Free Transmit Segment Queue DMA addresses
420 * hxp pointer to host transmit queue entry
421 * m0 pointer to output PDU buffer chain head
422 * nsegs number of processed transmit segments
429 fore_seg_dma_free(hxp, m0, nsegs)
435 H_dma *sdmap = hxp->hxq_dma;
439 for (i = 0; i < nsegs; i++) {
440 KB_DATASTART(m, cp, caddr_t);
441 DMA_FREE_ADDR(cp, *sdmap, KB_LEN(m), 0);