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.4 2003/08/27 10:35:16 rob 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;
83 atm_dev_pdu_print(cup, cvp, m, "fore_output");
86 vcp = fvp->fv_connvc->cvc_vcc;
89 * If we're still waiting for activation to finish, delay for
90 * a little while before we toss the PDU
92 if (fvp->fv_state == CVS_INITED) {
94 while (retry-- && (fvp->fv_state == CVS_INITED))
96 if (fvp->fv_state != CVS_ACTIVE) {
98 * Activation still hasn't finished, oh well....
100 fup->fu_stats->st_drv.drv_xm_notact++;
103 vcp->vc_nif->nif_if.if_oerrors++;
110 * Queue PDU at end of transmit queue
112 * If queue is full we'll delay a bit before tossing the PDU
115 hxp = fup->fu_xmit_tail;
116 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
118 fup->fu_stats->st_drv.drv_xm_full++;
123 DEVICE_LOCK((Cmn_unit *)fup);
124 fore_xmit_drain(fup);
125 DEVICE_UNLOCK((Cmn_unit *)fup);
127 } while (--retry && (!((*hxp->hxq_status) & QSTAT_FREE)));
129 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
131 * Queue is still full, bye-bye PDU
133 fup->fu_pif.pif_oerrors++;
136 vcp->vc_nif->nif_if.if_oerrors++;
144 * We've got a free transmit queue entry
148 * Now build the transmit segment descriptors for this PDU
150 m = fore_xmit_segment(fup, m, hxp, &nsegs, &pdulen);
153 * The build failed, buffer chain has been freed
157 vcp->vc_nif->nif_if.if_oerrors++;
163 * Set up the descriptor header
165 xdp = hxp->hxq_descr;
166 xdp->xd_cell_hdr = ATM_HDR_SET(vcp->vc_vpi, vcp->vc_vci, 0, 0);
167 xdp->xd_spec = XDS_SET_SPEC(0, fvp->fv_aal, nsegs, pdulen);
168 xdp->xd_rate = FORE_DEF_RATE;
171 * Everything is ready to go, so officially claim the host queue
172 * entry and setup the CP-resident queue entry. The CP will grab
173 * the PDU when the descriptor pointer is set.
175 fup->fu_xmit_tail = hxp->hxq_next;
178 (*hxp->hxq_status) = QSTAT_PENDING;
179 cqp = hxp->hxq_cpelem;
180 cqp->cq_descr = (CP_dma)
181 CP_WRITE((u_long)hxp->hxq_descr_dma | XMIT_SEGS_TO_BLKS(nsegs));
186 * See if there are any completed queue entries
188 DEVICE_LOCK((Cmn_unit *)fup);
189 fore_xmit_drain(fup);
190 DEVICE_UNLOCK((Cmn_unit *)fup);
197 * Build Transmit Segment Descriptors
199 * This function will take a supplied buffer chain of data to be transmitted
200 * and build the transmit segment descriptors for the data. This will include
201 * the dreaded operation of ensuring that the data for each transmit segment
202 * is full-word aligned and (except for the last segment) is an integral number
203 * of words in length. If the data isn't already aligned and sized as
204 * required, then the data must be shifted (copied) into place - a sure
205 * performance killer. Note that we rely on the fact that all buffer data
206 * areas are allocated with (at least) full-word alignments/lengths.
208 * If any errors are encountered, the buffer chain will be freed.
211 * fup pointer to device unit
212 * m pointer to output PDU buffer chain head
213 * hxp pointer to host transmit queue entry
214 * segp pointer to return the number of transmit segments
215 * lenp pointer to return the pdu length
218 * m build successful, pointer to (possibly new) head of
219 * output PDU buffer chain
220 * NULL build failed, buffer chain freed
224 fore_xmit_segment(fup, m, hxp, segp, lenp)
231 Xmit_descr *xdp = hxp->hxq_descr;
234 KBuffer *m0, *m1, *mprev;
237 int pdulen, nsegs, len, align;
244 sdmap = hxp->hxq_dma;
250 * Loop thru each buffer in the chain, performing the necessary
251 * data positioning and then building a segment descriptor for
256 * Get rid of any zero-length buffers
258 if (KB_LEN(m) == 0) {
260 KB_UNLINK(m, mprev, m1);
262 KB_UNLINKHEAD(m, m1);
270 * Make sure we don't try to use too many segments
272 if (nsegs >= XMIT_MAX_SEGS) {
274 * First, free already allocated DMA addresses
276 fore_seg_dma_free(hxp, m0, nsegs);
279 * Try to compress buffer chain (but only once)
286 fup->fu_stats->st_drv.drv_xm_maxpdu++;
288 m = atm_dev_compress(m0);
294 * Build segment descriptors for compressed chain
302 * Get start of data onto full-word alignment
304 KB_DATASTART(m, cp, caddr_t);
305 if ((align = ((u_int)cp) & (XMIT_SEG_ALIGN - 1)) != 0) {
307 * Gotta slide the data up
309 fup->fu_stats->st_drv.drv_xm_segnoal++;
311 KM_COPY(cp, bfr, KB_LEN(m));
312 KB_HEADMOVE(m, -align);
315 * Data already aligned
321 * Now work on getting the data length correct
324 while ((align = (len & (XMIT_SEG_ALIGN - 1))) &&
328 * Have to move some data from following buffer(s)
329 * to word-fill this buffer
331 int ncopy = MIN(XMIT_SEG_ALIGN - align, KB_LEN(m1));
335 * Move data to current buffer
339 fup->fu_stats->st_drv.drv_xm_seglen++;
340 KB_DATASTART(m1, cp, caddr_t);
342 KB_HEADADJ(m1, -ncopy);
343 KB_TAILADJ(m, ncopy);
351 * If we've drained the buffer, free it
353 if (KB_LEN(m1) == 0) {
356 KB_UNLINK(m1, m, m2);
361 * Finally, build the segment descriptor
365 * Round last segment to fullword length (if needed)
367 if (len & (XMIT_SEG_ALIGN - 1))
368 xsp->xsd_len = KB_LEN(m) =
369 (len + XMIT_SEG_ALIGN) & ~(XMIT_SEG_ALIGN - 1);
371 xsp->xsd_len = KB_LEN(m) = len;
374 * Get a DMA address for the data
376 dma = DMA_GET_ADDR(bfr, xsp->xsd_len, XMIT_SEG_ALIGN, 0);
378 fup->fu_stats->st_drv.drv_xm_segdma++;
379 fore_seg_dma_free(hxp, m0, nsegs);
385 * Now we're really ready to call it a segment
387 *sdmap++ = xsp->xsd_buffer = (H_dma) dma;
390 * Bump counters and get ready for next buffer
400 * Validate PDU length
402 if (pdulen > XMIT_MAX_PDULEN) {
403 fup->fu_stats->st_drv.drv_xm_maxpdu++;
404 fore_seg_dma_free(hxp, m0, nsegs);
410 * Return the good news to the caller
420 * Free Transmit Segment Queue DMA addresses
423 * hxp pointer to host transmit queue entry
424 * m0 pointer to output PDU buffer chain head
425 * nsegs number of processed transmit segments
432 fore_seg_dma_free(hxp, m0, nsegs)
438 H_dma *sdmap = hxp->hxq_dma;
442 for (i = 0; i < nsegs; i++) {
443 KB_DATASTART(m, cp, caddr_t);
444 DMA_FREE_ADDR(cp, *sdmap, KB_LEN(m), 0);