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_receive.c,v 1.5.2.2 2003/01/23 21:06:43 sam Exp $
27 * @(#) $DragonFly: src/sys/dev/atm/hfa/fore_receive.c,v 1.6 2005/02/01 00:51:50 joerg Exp $
31 * FORE Systems 200-Series Adapter Support
32 * ---------------------------------------
34 * Receive queue management
38 #include "fore_include.h"
43 static void fore_recv_stack (void *, KBuffer *);
47 * Allocate Receive Queue Data Structures
50 * fup pointer to device unit structure
53 * 0 allocations successful
54 * else allocation failed
57 fore_recv_allocate(fup)
63 * Allocate non-cacheable memory for receive status words
65 memp = atm_dev_alloc(sizeof(Q_status) * RECV_QUELEN,
66 QSTAT_ALIGN, ATM_DEV_NONCACHE);
70 fup->fu_recv_stat = (Q_status *) memp;
72 memp = DMA_GET_ADDR(fup->fu_recv_stat, sizeof(Q_status) * RECV_QUELEN,
73 QSTAT_ALIGN, ATM_DEV_NONCACHE);
77 fup->fu_recv_statd = (Q_status *) memp;
80 * Allocate memory for receive descriptors
82 memp = atm_dev_alloc(sizeof(Recv_descr) * RECV_QUELEN,
87 fup->fu_recv_desc = (Recv_descr *) memp;
89 memp = DMA_GET_ADDR(fup->fu_recv_desc,
90 sizeof(Recv_descr) * RECV_QUELEN, RECV_DESCR_ALIGN, 0);
94 fup->fu_recv_descd = (Recv_descr *) memp;
101 * Receive Queue Initialization
103 * Allocate and initialize the host-resident receive queue structures
104 * and then initialize the CP-resident queue structures.
106 * Called at interrupt level.
109 * fup pointer to device unit structure
115 fore_recv_initialize(fup)
118 Aali *aap = fup->fu_aali;
128 * Point to CP-resident receive queue
130 cqp = (Recv_queue *)(fup->fu_ram + CP_READ(aap->aali_recv_q));
133 * Point to host-resident receive queue structures
135 hrp = fup->fu_recv_q;
136 qsp = fup->fu_recv_stat;
137 qsp_dma = fup->fu_recv_statd;
138 rdp = fup->fu_recv_desc;
139 rdp_dma = fup->fu_recv_descd;
142 * Loop thru all queue entries and do whatever needs doing
144 for (i = 0; i < RECV_QUELEN; i++) {
147 * Set queue status word to free
152 * Set up host queue entry and link into ring
154 hrp->hrq_cpelem = cqp;
155 hrp->hrq_status = qsp;
156 hrp->hrq_descr = rdp;
157 hrp->hrq_descr_dma = rdp_dma;
158 if (i == (RECV_QUELEN - 1))
159 hrp->hrq_next = fup->fu_recv_q;
161 hrp->hrq_next = hrp + 1;
164 * Now let the CP into the game
166 cqp->cq_descr = (CP_dma) CP_WRITE(rdp_dma);
167 cqp->cq_status = (CP_dma) CP_WRITE(qsp_dma);
170 * Bump all queue pointers
181 * Initialize queue pointers
183 fup->fu_recv_head = fup->fu_recv_q;
190 * Drain Receive Queue
192 * This function will process all completed entries at the head of the
193 * receive queue. The received segments will be linked into a received
194 * PDU buffer chain and it will then be passed up the PDU's VCC stack for
195 * processing by the next higher protocol layer.
197 * May be called in interrupt state.
198 * Must be called with interrupts locked out.
201 * fup pointer to device unit structure
210 H_recv_queue *hrp = NULL;
216 KBuffer *m, *mhead, *mtail;
220 int i, pdulen, retries = 0, error;
222 /* Silence the compiler */
227 * Process each completed entry
230 while (*fup->fu_recv_head->hrq_status & QSTAT_COMPLETED) {
233 * Get completed entry's receive descriptor
235 hrp = fup->fu_recv_head;
236 rdp = hrp->hrq_descr;
240 * Cache flush receive descriptor
243 vac_flush((addr_t)rdp, sizeof(Recv_descr));
247 hdr = rdp->rd_cell_hdr;
248 nsegs = rdp->rd_nsegs;
255 * Locate incoming VCC for this PDU
257 fvp = (Fore_vcc *) atm_dev_vcc_find((Cmn_unit *)fup,
258 ATM_HDR_GET_VPI(hdr), ATM_HDR_GET_VCI(hdr), VCC_IN);
261 * Check for a receive error
263 * Apparently the receive descriptor itself contains valid
264 * information, but the received pdu data is probably bogus.
265 * We'll arrange for the receive buffer segments to be tossed.
267 if (*hrp->hrq_status & QSTAT_ERROR) {
269 fup->fu_pif.pif_ierrors++;
271 vcp = fvp->fv_connvc->cvc_vcc;
274 vcp->vc_nif->nif_if.if_ierrors++;
276 ATM_DEBUG1("fore receive error: hdr=0x%lx\n", hdr);
281 * Build PDU buffer chain from receive segments
283 for (i = 0, rsp = rdp->rd_seg; i < nsegs; i++, rsp++) {
285 bhp = rsp->rsd_handle;
286 seglen = rsp->rsd_len;
289 * Remove buffer from our supplied queue and get
290 * to the underlying buffer
292 switch (bhp->bh_type) {
295 DEQUEUE(bhp, Buf_handle, bh_qelem,
298 m = (KBuffer *) ((caddr_t)bhp - BUF1_SM_HOFF);
299 KB_DATASTART(m, cp, caddr_t);
300 DMA_FREE_ADDR(cp, bhp->bh_dma, BUF1_SM_SIZE, 0);
304 DEQUEUE(bhp, Buf_handle, bh_qelem,
307 m = (KBuffer *) ((caddr_t)bhp - BUF1_LG_HOFF);
308 KB_DATASTART(m, cp, caddr_t);
309 DMA_FREE_ADDR(cp, bhp->bh_dma, BUF1_LG_SIZE, 0);
314 "fore_recv_drain: bhp=%p type=0x%x\n",
316 panic("fore_recv_drain: bad buffer type");
320 * Toss any zero-length or receive error buffers
322 if ((seglen == 0) || error) {
328 * Link buffer into chain
331 type0 = bhp->bh_type;
332 KB_LINKHEAD(m, mhead);
342 * Flush received buffer data
348 KB_DATASTART(m, dp, addr_t);
355 * Make sure we've got a non-null PDU
362 * We only support user data PDUs (for now)
364 if (hdr & ATM_HDR_SET_PT(ATM_PT_NONUSER)) {
370 * Toss the data if there's no VCC
373 fup->fu_stats->st_drv.drv_rv_novcc++;
380 atm_dev_pdu_print((Cmn_unit *)fup, (Cmn_vcc *)fvp,
385 * Make sure we have our queueing headroom at the front
386 * of the buffer chain
388 if (type0 != BHT_S1_SMALL) {
391 * Small buffers already have headroom built-in, but
392 * if CP had to use a large buffer for the first
393 * buffer, then we have to allocate a buffer here to
394 * contain the headroom.
396 fup->fu_stats->st_drv.drv_rv_nosbf++;
398 KB_ALLOCPKT(m, BUF1_SM_SIZE, KB_F_NOWAIT, KB_T_DATA);
400 fup->fu_stats->st_drv.drv_rv_nomb++;
406 * Put new buffer at head of PDU chain
408 KB_LINKHEAD(m, mhead);
410 KB_HEADSET(m, BUF1_SM_DOFF);
415 * It looks like we've got a valid PDU - count it quick!!
417 mhead->m_pkthdr.rcvif = NULL;
418 mhead->m_pkthdr.csum_flags = 0;
419 SLIST_INIT(&mhead->m_pkthdr.tags);
420 KB_PLENSET(mhead, pdulen);
421 fup->fu_pif.pif_ipdus++;
422 fup->fu_pif.pif_ibytes += pdulen;
423 vcp = fvp->fv_connvc->cvc_vcc;
425 vcp->vc_ibytes += pdulen;
427 vcp->vc_nif->nif_ibytes += pdulen;
428 vcp->vc_nif->nif_if.if_ipackets++;
429 vcp->vc_nif->nif_if.if_ibytes += pdulen;
433 * The STACK_CALL needs to happen at splnet() in order
434 * for the stack sequence processing to work. Schedule an
435 * interrupt queue callback at splnet() since we are
436 * currently at device level.
440 * Prepend callback function pointer and token value to buffer.
441 * We have already guaranteed that the space is available
442 * in the first buffer.
444 KB_HEADADJ(mhead, sizeof(atm_intr_func_t) + sizeof(int));
445 KB_DATASTART(mhead, cp, caddr_t);
446 *((atm_intr_func_t *)cp) = fore_recv_stack;
447 cp += sizeof(atm_intr_func_t);
448 *((void **)cp) = (void *)fvp;
453 if (netisr_queue(NETISR_ATM, mhead)) {
454 fup->fu_stats->st_drv.drv_rv_ifull++;
461 * Mark this entry free for use and bump head pointer
462 * to the next entry in the queue
464 *hrp->hrq_status = QSTAT_FREE;
465 hrp->hrq_cpelem->cq_descr =
466 (CP_dma) CP_WRITE((u_long)hrp->hrq_descr_dma);
467 fup->fu_recv_head = hrp->hrq_next;
471 * Nearly all of the interrupts generated by the CP will be due
472 * to PDU reception. However, we may receive an interrupt before
473 * the CP has completed the status word DMA to host memory. Thus,
474 * if we haven't processed any PDUs during this interrupt, we will
475 * wait a bit for completed work on the receive queue, rather than
476 * having to field an extra interrupt very soon.
479 if (++retries <= FORE_RECV_RETRY) {
480 DELAY(FORE_RECV_DELAY);
490 * Pass Incoming PDU up Stack
492 * This function is called via the core ATM interrupt queue callback
493 * set in fore_recv_drain(). It will pass the supplied incoming
494 * PDU up the incoming VCC's stack.
499 * tok token to identify stack instantiation
500 * m pointer to incoming PDU buffer chain
506 fore_recv_stack(tok, m)
510 Fore_vcc *fvp = (Fore_vcc *)tok;
514 * Send the data up the stack
516 STACK_CALL(CPCS_UNITDATA_SIG, fvp->fv_upper,
517 fvp->fv_toku, fvp->fv_connvc, (int)m, 0, err);
526 * Free Receive Queue Data Structures
529 * fup pointer to device unit structure
539 * We'll just let fore_buf_free() take care of freeing any
540 * buffers sitting on the receive queue (which are also still
541 * on the fu_*_bq queue).
543 if (fup->fu_flags & CUF_INITED) {
547 * Free the status words
549 if (fup->fu_recv_stat) {
550 if (fup->fu_recv_statd) {
551 DMA_FREE_ADDR(fup->fu_recv_stat, fup->fu_recv_statd,
552 sizeof(Q_status) * RECV_QUELEN,
555 atm_dev_free((volatile void *)fup->fu_recv_stat);
556 fup->fu_recv_stat = NULL;
557 fup->fu_recv_statd = NULL;
561 * Free the receive descriptors
563 if (fup->fu_recv_desc) {
564 if (fup->fu_recv_descd) {
565 DMA_FREE_ADDR(fup->fu_recv_desc, fup->fu_recv_descd,
566 sizeof(Recv_descr) * RECV_QUELEN, 0);
568 atm_dev_free(fup->fu_recv_desc);
569 fup->fu_recv_desc = NULL;
570 fup->fu_recv_descd = NULL;