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/hea/eni_transmit.c,v 1.6 1999/12/21 08:24:35 eivind Exp $
27 * @(#) $DragonFly: src/sys/dev/atm/hea/eni_transmit.c,v 1.5 2004/01/08 18:39:17 asmodai Exp $
31 * Efficient ENI Adapter Support
32 * -----------------------------
34 * Transmit queue management and PDU output processing
39 #include <netproto/atm/kern_include.h>
41 #include "eni_stats.h"
46 * Make a variable which controls printing of PDUs
47 * as they travel through the driver.
50 int eni_pdu_print = 0;
54 * Some PCI chipsets do not handle one or more of the 8WORD or
55 * 4WORD DMA transfer sizes. Default to using only 1WORD transfer
56 * sizes unless the user wishes to experiment.
58 * Make sure that these have to be changed here in this module.
64 * Create a DMA list entry
66 * DMA entries consist of a control word and a physical address.
67 * Control words are comprised of a DMA type, a count of type transfers
68 * to occur, and a variable which for TX requests is the TX channel
69 * number and for RX requests is the VCC number.
72 * eup pointer to unit structure
74 * dma_list pointer to DMA list structure
75 * list_size length of DMA list structure
76 * idx pointer to current list entry
77 * val TX channel or RX vcc
78 * addr virtual DMA address of data buffer
79 * size size in bytes of DMA request to be built
82 * dma_list updated with new entries
83 * idx points to next list entry
84 * -1 no room in DMA list structure or DMA_GET_ADDR failed
87 eni_set_dma ( eup, rx, dma_list, list_size, idx, val, addr, size )
96 int dsize; /* Size of current DMA request */
99 * Round up to multiple of word and convert to number
100 * of words rather then number of bytes.
102 size = ( size + 3 ) >> 2;
106 * Check for room in DMA list - we need two entires
108 if ( *idx + 2 >= list_size )
112 * Here is the big win. Move as much data possible with
116 * Check if we can do one or more 8WORD DMAs
120 dma_list[(*idx)++] = ( dsize >> 3 ) << DMA_COUNT_SHIFT |
121 val << DMA_VCC_SHIFT | DMA_8WORD;
122 dma_list[*idx] = (u_long)DMA_GET_ADDR ( addr, dsize, 0, 0 );
123 if ( dma_list[*idx] == 0 ) {
125 eup->eu_stats.eni_st_drv.drv_rv_segdma++;
127 eup->eu_stats.eni_st_drv.drv_xm_segdma++;
128 return ( -1 ); /* DMA_GET_ADDR failed */
130 (*idx)++; /* increment index */
132 * Adjust addr and size
137 #endif /* DMA_USE_8WORD */
141 * Check for room in DMA list - we need two entries
143 if ( *idx + 2 >= list_size )
147 * Kindof a tossup from this point on. Since we hacked as many
148 * 8WORD DMAs off as possible, we are left with 0-7 words
149 * of remaining data. We could do upto one 4WORD with 0-3
150 * words left, or upto three 2WORDS with 0-1 words left,
151 * or upto seven WORDS with nothing left. Someday we should
152 * experiment with performance and see if any particular
153 * combination is a better win then some other...
156 * Check if we can do one or more 4WORD DMAs
160 dma_list[(*idx)++] = ( dsize >> 2 ) << DMA_COUNT_SHIFT |
161 val << DMA_VCC_SHIFT | DMA_4WORD;
162 dma_list[*idx] = (u_long)DMA_GET_ADDR ( addr, dsize, 0, 0 );
163 if ( dma_list[*idx] == 0 ) {
165 eup->eu_stats.eni_st_drv.drv_rv_segdma++;
167 eup->eu_stats.eni_st_drv.drv_xm_segdma++;
168 return ( -1 ); /* DMA_GET_ADDR failed */
170 (*idx)++; /* increment index */
172 * Adjust addr and size
177 #endif /* DMA_USE_4WORD */
180 * Check for room in DMA list - we need two entries
182 if ( *idx + 2 >= list_size )
186 * Hard to know if one 2WORD and 0/1 WORD DMA would be better
187 * then 2/3 WORD DMAs. For now, skip 2WORD DMAs in favor of
192 * Finish remaining size a 1WORD DMAs
195 dma_list[(*idx)++] = ( size ) << DMA_COUNT_SHIFT |
196 val << DMA_VCC_SHIFT | DMA_WORD;
197 dma_list[*idx] = (u_long)DMA_GET_ADDR ( addr, size, 0, 0 );
198 if ( dma_list[*idx] == 0 ) {
200 eup->eu_stats.eni_st_drv.drv_rv_segdma++;
202 eup->eu_stats.eni_st_drv.drv_xm_segdma++;
203 return ( -1 ); /* DMA_GET_ADDR failed */
205 (*idx)++; /* increment index */
209 * Inserted descriptor okay
215 * Drain Transmit queue
217 * As PDUs are given to the adapter to be transmitted, we
218 * place them into a private ifqueue so that we can free
219 * any resources AFTER we know they've been successfully DMAed.
220 * As part of the output processing, we record the PDUs start
221 * and stop entries in the DMA list, and prevent wrapping. When
222 * we pull the top element off, we simply check that the current
223 * DMA location is outside this PDU and if so, it's okay to free
226 * PDUs are always in ascending order in the queue.
229 * eup pointer to device unit structure
236 eni_xmit_drain ( eup )
248 * Pull the top element (PDU) off
250 IF_DEQUEUE ( &eup->eu_txqueue, m );
252 * As long as there are valid elements
258 * Find start of buffer
260 KB_DATASTART ( m, up, u_long * );
263 * First word is the VCC for this PDU
266 * NOTE: There is a potential problem here in that
267 * if the VCC is closed after this buffer was transmitted
268 * but before we get here, that while evp is non-null,
269 * it will not reference a valid vccb. We need to either
270 * delay closing the VCC until all references are removed
271 * from the drain stacks, actually go through the drain
272 * stacks and remove any references, or find someway of
273 * indicating that this vccb is nolonger usable.
275 evp = (Eni_vcc *)*up++;
277 * Second word is the start and stop DMA pointers
280 stop = *up++ & 0xffff;
282 * Find out where the TX engine is at
284 dmap = eup->eu_midway[MIDWAY_TX_RD];
286 * Check to see if TX engine has processed this
287 * PDU yet. Remember that everything is circular
288 * and that stop might be less than start numerically.
290 if ( start > stop ) {
291 if ( !(dmap >= stop && dmap < start) ) {
293 * Haven't finished this PDU yet - replace
294 * it as the head of list.
296 IF_PREPEND ( &eup->eu_txqueue, m );
298 * If this one isn't done, none of the others
305 if ( dmap < stop && dmap >= start ) {
307 * Haven't finished this PDU yet - replace
308 * it as the head of list.
310 IF_PREPEND ( &eup->eu_txqueue, m );
312 * If this one isn't done, none of the others
321 * Count the PDU stats for this interface
323 eup->eu_pif.pif_opdus++;
325 * Third word is PDU length from eni_output().
328 eup->eu_txfirst = (eup->eu_txfirst + *up) &
329 (eup->eu_txsize - 1);
330 eup->eu_pif.pif_obytes += pdulen;
333 * Now lookup the VCC entry and counts the stats for
337 vcp = evp->ev_connvc->cvc_vcc;
340 vcp->vc_obytes += pdulen;
342 * If we also have a network interface, count the PDU
346 vcp->vc_nif->nif_obytes += pdulen;
347 vcp->vc_nif->nif_if.if_opackets++;
348 #if (defined(BSD) && (BSD >= 199103))
349 vcp->vc_nif->nif_if.if_obytes += pdulen;
355 * Free the buffer chain
360 * Advance DMA write okay pointer
362 eup->eu_txdmawr = stop;
365 * Look for next completed transmit PDU
367 IF_DEQUEUE ( &eup->eu_txqueue, m );
370 * We've drained the queue...
379 * This function is called via the common driver code after receiving a
380 * stack *_DATA* command. The common code has already validated most of
381 * the request so we just need to check a few more ENI-specific details.
382 * Then we just build a segmentation structure for the PDU and place the
383 * address into the DMA_Transmit_queue.
386 * cup pointer to device common unit
387 * cvp pointer to common VCC entry
388 * m pointer to output PDU buffer chain head
395 eni_output ( cup, cvp, m )
400 Eni_unit *eup = (Eni_unit *)cup;
401 Eni_vcc *evp = (Eni_vcc *)cvp;
406 u_long dma_rd, dma_wr;
407 u_long dma[TEMP_DMA_SIZE];
414 KBuffer *m0 = m, *m1, *mprev = NULL;
421 atm_dev_pdu_print ( cup, cvp, m, "eni output" );
425 * Re-entry point for after buffer compression (if needed)
430 * We can avoid traversing the buffer list twice by building
431 * the middle (minus header and trailer) dma list at the
432 * same time we massage address and size alignments. Since
433 * this list remains local until we determine we've enough
434 * room, we're not going to trash anything by not checking
435 * sizes, etc. yet. Skip first entry to be used later to skip
440 * Do data positioning for address and length alignment
443 u_long buf_addr; /* For passing addr to eni_set_dma() */
446 * Get rid of any zero length buffers
448 if ( KB_LEN ( m ) == 0 ) {
450 KB_UNLINK ( m, mprev, m1 );
452 KB_UNLINKHEAD ( m, m1 );
459 * Get start of data onto full-word alignment
461 KB_DATASTART ( m, cp, caddr_t );
462 if ((align = ((u_int)cp) & (sizeof(u_long)-1)) != 0) {
464 * Gotta slide the data up
466 eup->eu_stats.eni_st_drv.drv_xm_segnoal++;
468 KM_COPY ( cp, bfr, KB_LEN ( m ) );
469 KB_HEADMOVE ( m, -align );
472 * Data already aligned
477 * Now work on getting the data length correct
480 while ( ( align = ( len & (sizeof(u_long)-1))) &&
481 (m1 = KB_NEXT ( m ) ) ) {
484 * Have to move some data from following buffer(s)
485 * to word-fill this buffer
487 u_int ncopy = MIN ( sizeof(u_long) - align,
492 * Move data to current buffer
496 eup->eu_stats.eni_st_drv.drv_xm_seglen++;
497 KB_DATASTART ( m1, cp, caddr_t );
499 KB_HEADADJ ( m1, -ncopy );
500 KB_TAILADJ ( m, ncopy );
508 * If we've drained the buffer, free it
510 if ( KB_LEN ( m1 ) == 0 ) {
513 KB_UNLINK ( m1, m, m2 );
518 * Address and size are now aligned. Build dma list
519 * using TX channel 0. Also, round length up to a word
520 * size which should only effect the last buffer in the
521 * chain. This works because the PDU length is maintained
522 * separately and we're not really adjusting the buffer's
523 * idea of its length.
525 KB_DATASTART ( m, buf_addr, u_long );
526 if ( eni_set_dma ( eup, 0, dma, TEMP_DMA_SIZE, &j, 0,
527 buf_addr, KB_LEN ( m ) ) < 0 ) {
529 * Failed to build DMA list. First, we'll try to
530 * compress the buffer chain into a smaller number
531 * of buffers. After compressing, we'll try to send
532 * the new buffer chain. If we still fail, then
533 * we'll drop the pdu.
538 "eni_output: eni_set_dma failed\n" );
540 eup->eu_pif.pif_oerrors++;
545 eup->eu_stats.eni_st_drv.drv_xm_maxpdu++;
547 m = atm_dev_compress ( m0 );
551 "eni_output: atm_dev_compress() failed\n" );
553 eup->eu_pif.pif_oerrors++;
558 * Reset to new head of buffer chain
563 * Indicate we've been through here
568 * Retry to build the DMA descriptors for the newly
569 * compressed buffer chain
576 * Now count the length
578 pdulen += KB_LEN ( m );
581 * Bump counters and get ready for next buffer
588 * Get a buffer to use in a private queue so that we can
589 * reclaim resources after the DMA has finished.
591 KB_ALLOC ( m, ENI_SMALL_BSIZE, KB_F_NOWAIT, KB_T_DATA );
594 * Link the PDU onto our new head
599 * Drop this PDU and let the sender try again.
601 eup->eu_stats.eni_st_drv.drv_xm_norsc++;
603 log(LOG_ERR, "eni_output: Unable to allocate drain buffer.\n");
605 eup->eu_pif.pif_oerrors++;
613 * Calculate size of buffer necessary to store PDU. If this
614 * is an AAL5 PDU, we'll need to know where to stuff the length
615 * value in the trailer.
618 * AAL5 PDUs need an extra two words for control/length and
619 * CRC. Check for AAL5 and add requirements here.
621 if ((aal5 = (evp->ev_connvc->cvc_attr.aal.type == ATM_AAL5)) != 0)
622 size = pdulen + 2 * sizeof(long);
626 * Pad to next complete cell boundary
628 size += (BYTES_PER_CELL - 1);
629 size -= size % BYTES_PER_CELL;
631 * Convert size to words and add 2 words overhead for every
632 * PDU (descriptor and cell header).
634 size = (size >> 2) + 2;
637 * First, check to see if there's enough buffer space to
638 * store the PDU. We do this by checking to see if the size
639 * required crosses the eu_txfirst pointer. However, we don't
640 * want to exactly fill the buffer, because we won't be able to
641 * distinguish between a full and empty buffer.
643 if ( eup->eu_txpos == eup->eu_txfirst )
644 buf_avail = eup->eu_txsize;
646 if ( eup->eu_txpos > eup->eu_txfirst )
647 buf_avail = eup->eu_txsize - ( eup->eu_txpos - eup->eu_txfirst );
649 buf_avail = eup->eu_txfirst - eup->eu_txpos;
651 if ( size >= buf_avail )
654 * No buffer space in the adapter to store this PDU.
655 * Drop PDU and return.
657 eup->eu_stats.eni_st_drv.drv_xm_nobuf++;
660 "eni_output: not enough room in buffer\n" );
662 eup->eu_pif.pif_oerrors++;
669 * Find out where current DMA pointers are at
671 dma_start = dma_wr = eup->eu_midway[MIDWAY_TX_WR];
672 dma_rd = eup->eu_midway[MIDWAY_TX_RD];
675 * Figure out how much DMA room we have available
677 if ( dma_rd == dma_wr ) { /* Queue is empty */
678 dma_avail = DMA_LIST_SIZE;
680 dma_avail = ( dma_rd + DMA_LIST_SIZE - dma_wr )
681 & ( DMA_LIST_SIZE - 1 );
684 * Check to see if we can describe this PDU or if we're:
685 * out of room, will wrap past recovered resources.
687 if ( dma_avail < (j / 2 + 4) ||
688 ( dma_wr + (j / 2 + 4) > eup->eu_txdmawr + DMA_LIST_SIZE ) ) {
690 * No space to insert DMA list into queue. Drop this PDU.
692 eup->eu_stats.eni_st_drv.drv_xm_nodma++;
695 "eni_output: not enough room in DMA queue\n" );
697 eup->eu_pif.pif_oerrors++;
704 * Create DMA descriptor for header. There is a descriptor word
705 * and also a cell header word which we'll set manually.
707 dma[0] = (((int)(eup->eu_txpos + 2) & (eup->eu_txsize-1)) <<
708 DMA_COUNT_SHIFT) | DMA_JK;
712 * JK for AAL5 trailer. Set END bit as well.
715 dma[j++] = (((int)(eup->eu_txpos+size) & (eup->eu_txsize-1)) <<
716 DMA_COUNT_SHIFT) | DMA_END_BIT | DMA_JK;
719 dma[j-2] |= DMA_END_BIT; /* Backup and set END bit */
723 * Find out where in adapter memory this TX buffer starts.
726 ((((int)eup->eu_midway[MIDWAY_TXPLACE] & 0x7ff) << ENI_LOC_PREDIV) +
730 * Set descriptor word
732 tx_send[eup->eu_txpos] =
733 (MIDWAY_UNQ_ID << 28) | (aal5 ? 1 << 27 : 0)
734 | (size / WORDS_PER_CELL);
738 tx_send[(eup->eu_txpos+1)&(eup->eu_txsize-1)] =
739 evp->ev_connvc->cvc_vcc->vc_vci << 4;
742 * We've got all our resources, count the stats
746 * If this is an AAL5 PDU, we need to set the length
748 tx_send[(eup->eu_txpos+size-2) &
749 (eup->eu_txsize-1)] = pdulen;
751 * Increment AAL5 stats
753 eup->eu_stats.eni_st_aal5.aal5_pdu_xmit++;
754 eup->eu_stats.eni_st_aal5.aal5_xmit += (size - 2) / WORDS_PER_CELL;
757 * Increment AAL0 stats
759 eup->eu_stats.eni_st_aal0.aal0_xmit += (size - 2) / WORDS_PER_CELL;
762 * Increment ATM stats
764 eup->eu_stats.eni_st_atm.atm_xmit += (size - 2) / WORDS_PER_CELL;
770 for ( i = 0; i < j; i++ ) {
771 eup->eu_txdma[dma_wr*2] = dma[i*2];
772 eup->eu_txdma[dma_wr*2+1] = dma[i*2+1];
773 dma_wr = (dma_wr+1) & (DMA_LIST_SIZE-1);
779 * We toss four words in to help keep track of this
780 * PDU. The first is a pointer to the VC control block
781 * so we can find which VCI this went out on, the second
782 * is the start and stop pointers for the DMA list which
783 * describes this PDU, the third is the PDU length
784 * since we'll want to know that for stats gathering,
785 * and the fourth is the number of DMA words.
787 KB_DATASTART ( m, up, u_long * );
789 *up++ = dma_start << 16 | dma_wr;
794 * Set length of our buffer
796 KB_LEN ( m ) = 4 * sizeof ( long );
799 * Place buffers onto transmit queue for draining
802 IF_ENQUEUE ( &eup->eu_txqueue, m );
806 * Update next word to be stored
808 eup->eu_txpos = ((eup->eu_txpos + size) & (eup->eu_txsize - 1));
811 * Update MIDWAY_TX_WR pointer
813 eup->eu_midway[MIDWAY_TX_WR] = dma_wr;