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.
31 * FORE Systems 200-Series Adapter Support
32 * ---------------------------------------
34 * PDU output processing
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
47 #include <netatm/port.h>
48 #include <netatm/queue.h>
49 #include <netatm/atm.h>
50 #include <netatm/atm_sys.h>
51 #include <netatm/atm_sap.h>
52 #include <netatm/atm_cm.h>
53 #include <netatm/atm_if.h>
54 #include <netatm/atm_vc.h>
55 #include <netatm/atm_stack.h>
56 #include <netatm/atm_pcb.h>
57 #include <netatm/atm_var.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/hfa/fore.h>
60 #include <dev/hfa/fore_aali.h>
61 #include <dev/hfa/fore_slave.h>
62 #include <dev/hfa/fore_stats.h>
63 #include <dev/hfa/fore_var.h>
64 #include <dev/hfa/fore_include.h>
67 __RCSID("@(#) $FreeBSD$");
74 static KBuffer * fore_xmit_segment(Fore_unit *, KBuffer *,
75 H_xmit_queue *, int *, int *);
76 static void fore_seg_dma_free(H_xmit_queue *, KBuffer *, int);
82 * This function is called via the common driver code after receiving a
83 * stack *_DATA* command. The common code has already validated most of
84 * the request so we just need to check a few more Fore-specific details.
85 * Then we just build a transmit descriptor request for the PDU and issue
86 * the command to the CP.
89 * cup pointer to device common unit
90 * cvp pointer to common VCC entry
91 * m pointer to output PDU buffer chain head
98 fore_output(cup, cvp, m)
103 Fore_unit *fup = (Fore_unit *)cup;
104 Fore_vcc *fvp = (Fore_vcc *)cvp;
109 int retry, nsegs, pdulen;
114 atm_dev_pdu_print(cup, cvp, m, "fore_output");
117 vcp = fvp->fv_connvc->cvc_vcc;
120 * If we're still waiting for activation to finish, delay for
121 * a little while before we toss the PDU
123 if (fvp->fv_state == CVS_INITED) {
125 while (retry-- && (fvp->fv_state == CVS_INITED))
127 if (fvp->fv_state != CVS_ACTIVE) {
129 * Activation still hasn't finished, oh well....
131 fup->fu_stats->st_drv.drv_xm_notact++;
134 vcp->vc_nif->nif_if.if_oerrors++;
141 * Queue PDU at end of transmit queue
143 * If queue is full we'll delay a bit before tossing the PDU
146 hxp = fup->fu_xmit_tail;
147 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
149 fup->fu_stats->st_drv.drv_xm_full++;
154 DEVICE_LOCK((Cmn_unit *)fup);
155 fore_xmit_drain(fup);
156 DEVICE_UNLOCK((Cmn_unit *)fup);
158 } while (--retry && (!((*hxp->hxq_status) & QSTAT_FREE)));
160 if (!((*hxp->hxq_status) & QSTAT_FREE)) {
162 * Queue is still full, bye-bye PDU
164 fup->fu_pif.pif_oerrors++;
167 vcp->vc_nif->nif_if.if_oerrors++;
175 * We've got a free transmit queue entry
179 * Now build the transmit segment descriptors for this PDU
181 m = fore_xmit_segment(fup, m, hxp, &nsegs, &pdulen);
184 * The build failed, buffer chain has been freed
188 vcp->vc_nif->nif_if.if_oerrors++;
194 * Set up the descriptor header
196 xdp = hxp->hxq_descr;
197 xdp->xd_cell_hdr = ATM_HDR_SET(vcp->vc_vpi, vcp->vc_vci, 0, 0);
198 xdp->xd_spec = XDS_SET_SPEC(0, fvp->fv_aal, nsegs, pdulen);
199 xdp->xd_rate = fvp->rate;
202 * Everything is ready to go, so officially claim the host queue
203 * entry and setup the CP-resident queue entry. The CP will grab
204 * the PDU when the descriptor pointer is set.
206 fup->fu_xmit_tail = hxp->hxq_next;
209 (*hxp->hxq_status) = QSTAT_PENDING;
210 cqp = hxp->hxq_cpelem;
211 cqp->cq_descr = (CP_dma)
212 CP_WRITE((u_long)hxp->hxq_descr_dma | XMIT_SEGS_TO_BLKS(nsegs));
217 * See if there are any completed queue entries
219 DEVICE_LOCK((Cmn_unit *)fup);
220 fore_xmit_drain(fup);
221 DEVICE_UNLOCK((Cmn_unit *)fup);
228 * Build Transmit Segment Descriptors
230 * This function will take a supplied buffer chain of data to be transmitted
231 * and build the transmit segment descriptors for the data. This will include
232 * the dreaded operation of ensuring that the data for each transmit segment
233 * is full-word aligned and (except for the last segment) is an integral number
234 * of words in length. If the data isn't already aligned and sized as
235 * required, then the data must be shifted (copied) into place - a sure
236 * performance killer. Note that we rely on the fact that all buffer data
237 * areas are allocated with (at least) full-word alignments/lengths.
239 * If any errors are encountered, the buffer chain will be freed.
242 * fup pointer to device unit
243 * m pointer to output PDU buffer chain head
244 * hxp pointer to host transmit queue entry
245 * segp pointer to return the number of transmit segments
246 * lenp pointer to return the pdu length
249 * m build successful, pointer to (possibly new) head of
250 * output PDU buffer chain
251 * NULL build failed, buffer chain freed
255 fore_xmit_segment(fup, m, hxp, segp, lenp)
262 Xmit_descr *xdp = hxp->hxq_descr;
265 KBuffer *m0, *m1, *mprev;
268 int pdulen, nsegs, len, align;
275 sdmap = hxp->hxq_dma;
281 * Loop thru each buffer in the chain, performing the necessary
282 * data positioning and then building a segment descriptor for
287 * Get rid of any zero-length buffers
289 if (KB_LEN(m) == 0) {
291 KB_UNLINK(m, mprev, m1);
293 KB_UNLINKHEAD(m, m1);
301 * Make sure we don't try to use too many segments
303 if (nsegs >= XMIT_MAX_SEGS) {
305 * First, free already allocated DMA addresses
307 fore_seg_dma_free(hxp, m0, nsegs);
310 * Try to compress buffer chain (but only once)
317 fup->fu_stats->st_drv.drv_xm_maxpdu++;
319 m = atm_dev_compress(m0);
325 * Build segment descriptors for compressed chain
333 * Get start of data onto full-word alignment
335 KB_DATASTART(m, cp, caddr_t);
336 if ((align = ((uintptr_t)cp) & (XMIT_SEG_ALIGN - 1)) != 0) {
338 * Gotta slide the data up
340 fup->fu_stats->st_drv.drv_xm_segnoal++;
342 bcopy(cp, bfr, KB_LEN(m));
343 KB_HEADMOVE(m, -align);
346 * Data already aligned
352 * Now work on getting the data length correct
355 while ((align = (len & (XMIT_SEG_ALIGN - 1))) &&
359 * Have to move some data from following buffer(s)
360 * to word-fill this buffer
362 int ncopy = MIN(XMIT_SEG_ALIGN - align, KB_LEN(m1));
366 * Move data to current buffer
370 fup->fu_stats->st_drv.drv_xm_seglen++;
371 KB_DATASTART(m1, cp, caddr_t);
373 KB_HEADADJ(m1, -ncopy);
374 KB_TAILADJ(m, ncopy);
382 * If we've drained the buffer, free it
384 if (KB_LEN(m1) == 0) {
387 KB_UNLINK(m1, m, m2);
392 * Finally, build the segment descriptor
396 * Round last segment to fullword length (if needed)
398 if (len & (XMIT_SEG_ALIGN - 1))
399 xsp->xsd_len = KB_LEN(m) =
400 (len + XMIT_SEG_ALIGN) & ~(XMIT_SEG_ALIGN - 1);
402 xsp->xsd_len = KB_LEN(m) = len;
405 * Get a DMA address for the data
409 fup->fu_stats->st_drv.drv_xm_segdma++;
410 fore_seg_dma_free(hxp, m0, nsegs);
416 * Now we're really ready to call it a segment
418 *sdmap++ = xsp->xsd_buffer = (H_dma) dma;
421 * Bump counters and get ready for next buffer
431 * Validate PDU length
433 if (pdulen > XMIT_MAX_PDULEN) {
434 fup->fu_stats->st_drv.drv_xm_maxpdu++;
435 fore_seg_dma_free(hxp, m0, nsegs);
441 * Return the good news to the caller
451 * Free Transmit Segment Queue DMA addresses
454 * hxp pointer to host transmit queue entry
455 * m0 pointer to output PDU buffer chain head
456 * nsegs number of processed transmit segments
463 fore_seg_dma_free(hxp, m0, nsegs)
469 H_dma *sdmap = hxp->hxq_dma;
473 for (i = 0; i < nsegs; i++) {
474 KB_DATASTART(m, cp, caddr_t);