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/netatm/atm_subr.c,v 1.7 2000/02/13 03:31:59 peter Exp $
27 * @(#) $DragonFly: src/sys/netproto/atm/atm_subr.c,v 1.14 2004/04/22 05:09:43 dillon Exp $
34 * Miscellaneous ATM subroutines
38 #include "kern_include.h"
40 #include <sys/thread2.h>
41 #include <sys/msgport2.h>
46 struct atm_pif *atm_interface_head = NULL;
47 struct atm_ncm *atm_netconv_head = NULL;
48 Atm_endpoint *atm_endpoints[ENDPT_MAX+1] = {NULL};
49 struct sp_info *atm_pool_head = NULL;
50 struct stackq_entry *atm_stackq_head = NULL, *atm_stackq_tail;
54 struct atm_sock_stat atm_sock_stat = { { 0 } };
57 int atm_dev_print = 0;
58 int atm_print_data = 0;
59 int atm_version = ATM_VERSION;
60 struct timeval atm_debugtime = {0, 0};
61 struct ifqueue atm_intrq;
63 struct sp_info atm_attributes_pool = {
64 "atm attributes pool", /* si_name */
65 sizeof(Atm_attributes), /* si_blksiz */
74 static void atm_compact (struct atm_time *);
75 static KTimeout_ret atm_timexp (void *);
76 static int atm_intr(struct netmsg *);
81 static struct atm_time *atm_timeq = NULL;
82 static struct atm_time atm_compactimer = {0, 0};
84 static struct sp_info atm_stackq_pool = {
85 "Service stack queue pool", /* si_name */
86 sizeof(struct stackq_entry), /* si_blksiz */
93 * Initialize ATM kernel
95 * Performs any initialization required before things really get underway.
96 * Called from ATM domain initialization or from first registration function
110 * Never called from interrupts, so no locking needed
116 #ifndef __DragonFly__
118 * Add ATM protocol family
120 (void) protocol_family(&atmdomain, NULL, NULL);
123 atm_intrq.ifq_maxlen = ATM_INTRQ_MAX;
125 atm_intr_index = register_isr(atm_intr);
128 netisr_register(NETISR_ATM, cpu0_portfn, atm_intr);
132 * Initialize subsystems
139 (void) timeout(atm_timexp, (void *)0, hz/ATM_HZ);
142 * Start the compaction timer
144 atm_timeout(&atm_compactimer, SPOOL_COMPACT, atm_compact);
149 * Allocate a Control Block
151 * Gets a new control block allocated from the specified storage pool,
152 * acquiring memory for new pool chunks if required. The returned control
153 * block's contents will be cleared.
156 * sip pointer to sp_info for storage pool
159 * addr pointer to allocated control block
160 * 0 allocation failed
168 struct sp_chunk *scp;
178 * Are there any free in the pool?
183 * Find first chunk with a free block
185 for (scp = sip->si_poolh; scp; scp = scp->sc_next) {
186 if (scp->sc_freeh != NULL)
193 * No free blocks - have to allocate a new
194 * chunk (but put a limit to this)
196 struct sp_link *slp_next;
200 * First time for this pool??
202 if (sip->si_chunksiz == 0) {
206 * Initialize pool information
208 n = sizeof(struct sp_chunk) +
210 (sip->si_blksiz + sizeof(struct sp_link));
211 sip->si_chunksiz = roundup(n, SPOOL_ROUNDUP);
214 * Place pool on kernel chain
216 LINK2TAIL(sip, struct sp_info, atm_pool_head, si_next);
219 if (sip->si_chunks >= sip->si_maxallow) {
225 scp = KM_ALLOC(sip->si_chunksiz, M_DEVBUF,
226 M_INTWAIT | M_NULLOK);
234 scp->sc_magic = SPOOL_MAGIC;
238 * Divy up chunk into free blocks
240 slp = (struct sp_link *)(scp + 1);
243 for (i = sip->si_blkcnt; i > 1; i--) {
244 slp_next = (struct sp_link *)((caddr_t)(slp + 1) +
246 slp->sl_u.slu_next = slp_next;
249 slp->sl_u.slu_next = NULL;
253 * Add new chunk to end of pool
256 sip->si_poolt->sc_next = scp;
262 sip->si_total += sip->si_blkcnt;
263 sip->si_free += sip->si_blkcnt;
264 if (sip->si_chunks > sip->si_maxused)
265 sip->si_maxused = sip->si_chunks;
269 * Allocate the first free block in chunk
272 scp->sc_freeh = slp->sl_u.slu_next;
278 * Save link back to pool chunk
280 slp->sl_u.slu_chunk = scp;
285 KM_ZERO(bp, sip->si_blksiz);
293 * Free a Control Block
295 * Returns a previously allocated control block back to the owners
299 * bp pointer to block to be freed
310 struct sp_chunk *scp;
315 * Get containing chunk and pool info
317 slp = (struct sp_link *)bp;
319 scp = slp->sl_u.slu_chunk;
320 if (scp->sc_magic != SPOOL_MAGIC)
321 panic("atm_free: chunk magic missing");
325 * Add block to free chain
328 scp->sc_freet->sl_u.slu_next = slp;
331 scp->sc_freeh = scp->sc_freet = slp;
332 slp->sl_u.slu_next = NULL;
342 * Storage Pool Compaction
344 * Called periodically in order to perform compaction of the
345 * storage pools. Each pool will be checked to see if any chunks
346 * can be freed, taking some care to avoid freeing too many chunks
347 * in order to avoid memory thrashing.
352 * tip pointer to timer control block (atm_compactimer)
360 struct atm_time *tip;
363 struct sp_chunk *scp;
365 struct sp_chunk *scp_prev;
368 * Check out all storage pools
370 for (sip = atm_pool_head; sip; sip = sip->si_next) {
373 * Always keep a minimum number of chunks around
375 if (sip->si_chunks <= SPOOL_MIN_CHUNK)
379 * Maximum chunks to free at one time will leave
380 * pool with at least 50% utilization, but never
381 * go below minimum chunk count.
383 i = ((sip->si_free * 2) - sip->si_total) / sip->si_blkcnt;
384 i = MIN(i, sip->si_chunks - SPOOL_MIN_CHUNK);
387 * Look for chunks to free
390 for (scp = sip->si_poolh; scp && i > 0; ) {
392 if (scp->sc_used == 0) {
395 * Found a chunk to free, so do it
398 scp_prev->sc_next = scp->sc_next;
399 if (sip->si_poolt == scp)
400 sip->si_poolt = scp_prev;
402 sip->si_poolh = scp->sc_next;
404 KM_FREE((caddr_t)scp, sip->si_chunksiz,
408 * Update pool controls
411 sip->si_total -= sip->si_blkcnt;
412 sip->si_free -= sip->si_blkcnt;
415 scp = scp_prev->sc_next;
426 * Restart the compaction timer
428 atm_timeout(&atm_compactimer, SPOOL_COMPACT, atm_compact);
435 * Release a Storage Pool
437 * Frees all dynamic storage acquired for a storage pool.
438 * This function is normally called just prior to a module's unloading.
441 * sip pointer to sp_info for storage pool
448 atm_release_pool(sip)
451 struct sp_chunk *scp, *scp_next;
455 * Free each chunk in pool
457 for (scp = sip->si_poolh; scp; scp = scp_next) {
460 * Check for memory leaks
463 panic("atm_release_pool: unfreed blocks");
465 scp_next = scp->sc_next;
467 KM_FREE((caddr_t)scp, sip->si_chunksiz, M_DEVBUF);
471 * Update pool controls
473 sip->si_poolh = NULL;
479 * Unlink pool from active chain
481 sip->si_chunksiz = 0;
482 UNLINK(sip, struct sp_info, atm_pool_head, si_next);
490 * Handle timer tick expiration
492 * Decrement tick count in first block on timer queue. If there
493 * are blocks with expired timers, call their timeout function.
494 * This function is called ATM_HZ times per second.
497 * arg argument passed on timeout() call
507 struct atm_time *tip;
512 * Decrement tick count
514 if (((tip = atm_timeq) == NULL) || (--tip->ti_ticks > 0)) {
519 * Stack queue should have been drained
522 if (atm_stackq_head != NULL)
523 panic("atm_timexp: stack queue not empty");
527 * Dispatch expired timers
529 while (((tip = atm_timeq) != NULL) && (tip->ti_ticks == 0)) {
530 void (*func)(struct atm_time *);
533 * Remove expired block from queue
535 atm_timeq = tip->ti_next;
536 tip->ti_flag &= ~TIF_QUEUED;
539 * Call timeout handler (with network interrupts locked out)
549 * Drain any deferred calls
559 (void) timeout(atm_timexp, (void *)0, hz/ATM_HZ);
566 * Schedule a control block timeout
568 * Place the supplied timer control block on the timer queue. The
569 * function (func) will be called in 't' timer ticks with the
570 * control block address as its only argument. There are ATM_HZ
571 * timer ticks per second. The ticks value stored in each block is
572 * a delta of the number of ticks from the previous block in the queue.
573 * Thus, for each tick interval, only the first block in the queue
574 * needs to have its tick value decremented.
577 * tip pointer to timer control block
578 * t number of timer ticks until expiration
579 * func pointer to function to call at expiration
586 atm_timeout(tip, t, func)
587 struct atm_time *tip;
589 void (*func)(struct atm_time *);
591 struct atm_time *tip1, *tip2;
596 * Check for double queueing error
598 if (tip->ti_flag & TIF_QUEUED)
599 panic("atm_timeout: double queueing");
602 * Make sure we delay at least a little bit
608 * Find out where we belong on the queue
611 for (tip1 = NULL, tip2 = atm_timeq; tip2 && (tip2->ti_ticks <= t);
612 tip1 = tip2, tip2 = tip1->ti_next) {
617 * Place ourselves on queue and update timer deltas
631 tip->ti_flag |= TIF_QUEUED;
643 * Remove the supplied timer control block from the timer queue.
646 * tip pointer to timer control block
649 * 0 control block successfully dequeued
650 * 1 control block not on timer queue
655 struct atm_time *tip;
657 struct atm_time *tip1, *tip2;
661 * Is control block queued?
663 if ((tip->ti_flag & TIF_QUEUED) == 0)
667 * Find control block on the queue
670 for (tip1 = NULL, tip2 = atm_timeq; tip2 && (tip2 != tip);
671 tip1 = tip2, tip2 = tip1->ti_next) {
680 * Remove block from queue and update timer deltas
686 tip1->ti_next = tip2;
689 tip2->ti_ticks += tip->ti_ticks;
694 tip->ti_flag &= ~TIF_QUEUED;
704 * Queues a stack call which must be deferred to the global stack queue.
705 * The call parameters are stored in entries which are allocated from the
706 * stack queue storage pool.
710 * func destination function
711 * token destination layer's token
712 * cvp pointer to connection vcc
713 * arg1 command argument
714 * arg2 command argument
718 * errno call not queued - reason indicated
722 atm_stack_enq(cmd, func, token, cvp, arg1, arg2)
724 void (*func)(int, void *, int, int);
730 struct stackq_entry *sqp;
734 * Get a new queue entry for this call
736 sqp = (struct stackq_entry *)atm_allocate(&atm_stackq_pool);
748 sqp->sq_token = token;
751 sqp->sq_connvc = cvp;
754 * Put new entry at end of queue
756 if (atm_stackq_head == NULL)
757 atm_stackq_head = sqp;
759 atm_stackq_tail->sq_next = sqp;
760 atm_stackq_tail = sqp;
768 * Drain the Stack Queue
770 * Dequeues and processes entries from the global stack queue.
782 struct stackq_entry *sqp, *qprev, *qnext;
787 * Loop thru entire queue until queue is empty
788 * (but panic rather loop forever)
793 for (sqp = atm_stackq_head; sqp; ) {
796 * Got an eligible entry, do STACK_CALL stuff
798 if (sqp->sq_cmd & STKCMD_UP) {
799 if (sqp->sq_connvc->cvc_downcnt) {
802 * Cant process now, skip it
810 * OK, dispatch the call
812 sqp->sq_connvc->cvc_upcnt++;
813 (*sqp->sq_func)(sqp->sq_cmd,
817 sqp->sq_connvc->cvc_upcnt--;
819 if (sqp->sq_connvc->cvc_upcnt) {
822 * Cant process now, skip it
830 * OK, dispatch the call
832 sqp->sq_connvc->cvc_downcnt++;
833 (*sqp->sq_func)(sqp->sq_cmd,
837 sqp->sq_connvc->cvc_downcnt--;
841 * Dequeue processed entry and free it
844 qnext = sqp->sq_next;
846 qprev->sq_next = qnext;
848 atm_stackq_head = qnext;
850 atm_stackq_tail = qprev;
851 atm_free((caddr_t)sqp);
857 * Make sure entire queue was drained
859 if (atm_stackq_head != NULL)
860 panic("atm_stack_drain: Queue not emptied");
867 * Process Interrupt Queue
869 * Processes entries on the ATM interrupt queue. This queue is used by
870 * device interface drivers in order to schedule events from the driver's
871 * lower (interrupt) half to the driver's stack services.
873 * The interrupt routines must store the stack processing function to call
874 * and a token (typically a driver/stack control block) at the front of the
875 * queued buffer. We assume that the function pointer and token values are
876 * both contained (and properly aligned) in the first buffer of the chain.
886 atm_intr(struct netmsg *msg)
888 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
890 atm_intr_func_t func;
894 * Get function to call and token value
896 KB_DATASTART(m, cp, caddr_t);
897 func = *(atm_intr_func_t *)cp;
899 token = *(void **)cp;
900 KB_HEADADJ(m, -(sizeof(func) + sizeof(token)));
901 if (KB_LEN(m) == 0) {
903 KB_UNLINKHEAD(m, m1);
908 * Call processing function
913 * Drain any deferred calls
916 lwkt_replymsg(&msg->nm_lmsg, 0);
921 * Print a pdu buffer chain
924 * m pointer to pdu buffer chain
925 * msg pointer to message header string
932 atm_pdu_print(m, msg)
942 KB_DATASTART(m, cp, caddr_t);
943 printf("%cbfr=%p data=%p len=%d: ",
944 c, m, cp, KB_LEN(m));
946 if (atm_print_data) {
947 for (i = 0; i < KB_LEN(m); i++) {
948 printf("%2x ", (u_char)*cp++);
950 printf("<end_bfr>\n");