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 * @(#) $Id: atm_subr.c,v 1.3 1998/10/31 20:06:54 phk Exp $
34 * Miscellaneous ATM subroutines
38 #include <netatm/kern_include.h>
41 __RCSID("@(#) $Id: atm_subr.c,v 1.3 1998/10/31 20:06:54 phk Exp $");
48 struct atm_pif *atm_interface_head = NULL;
49 struct atm_ncm *atm_netconv_head = NULL;
50 Atm_endpoint *atm_endpoints[ENDPT_MAX+1] = {NULL};
51 struct sp_info *atm_pool_head = NULL;
52 struct stackq_entry *atm_stackq_head = NULL, *atm_stackq_tail;
53 struct ifqueue atm_intrq;
57 struct atm_sock_stat atm_sock_stat = { { 0 } };
60 int atm_dev_print = 0;
61 int atm_print_data = 0;
62 int atm_version = ATM_VERSION;
63 struct timeval atm_debugtime = {0, 0};
65 struct sp_info atm_attributes_pool = {
66 "atm attributes pool", /* si_name */
67 sizeof(Atm_attributes), /* si_blksiz */
76 static void atm_compact __P((struct atm_time *));
77 static KTimeout_ret atm_timexp __P((void *));
82 static struct atm_time *atm_timeq = NULL;
83 static struct atm_time atm_compactimer = {0, 0};
85 static struct sp_info atm_stackq_pool = {
86 "Service stack queue pool", /* si_name */
87 sizeof(struct stackq_entry), /* si_blksiz */
94 * Initialize ATM kernel
96 * Performs any initialization required before things really get underway.
97 * Called from ATM domain initialization or from first registration function
111 * Never called from interrupts, so no locking needed
119 * Add ATM protocol family
121 (void) protocol_family(&atmdomain, NULL, NULL);
124 atm_intrq.ifq_maxlen = ATM_INTRQ_MAX;
126 atm_intr_index = register_isr(atm_intr);
130 * Initialize subsystems
137 (void) timeout(atm_timexp, (void *)0, hz/ATM_HZ);
140 * Start the compaction timer
142 atm_timeout(&atm_compactimer, SPOOL_COMPACT, atm_compact);
147 * Allocate a Control Block
149 * Gets a new control block allocated from the specified storage pool,
150 * acquiring memory for new pool chunks if required. The returned control
151 * block's contents will be cleared.
154 * sip pointer to sp_info for storage pool
157 * addr pointer to allocated control block
158 * 0 allocation failed
166 struct sp_chunk *scp;
176 * Are there any free in the pool?
181 * Find first chunk with a free block
183 for (scp = sip->si_poolh; scp; scp = scp->sc_next) {
184 if (scp->sc_freeh != NULL)
191 * No free blocks - have to allocate a new
192 * chunk (but put a limit to this)
194 struct sp_link *slp_next;
198 * First time for this pool??
200 if (sip->si_chunksiz == 0) {
204 * Initialize pool information
206 n = sizeof(struct sp_chunk) +
208 (sip->si_blksiz + sizeof(struct sp_link));
209 sip->si_chunksiz = roundup(n, SPOOL_ROUNDUP);
212 * Place pool on kernel chain
214 LINK2TAIL(sip, struct sp_info, atm_pool_head, si_next);
217 if (sip->si_chunks >= sip->si_maxallow) {
223 scp = (struct sp_chunk *)
224 KM_ALLOC(sip->si_chunksiz, M_DEVBUF, M_NOWAIT);
232 scp->sc_magic = SPOOL_MAGIC;
236 * Divy up chunk into free blocks
238 slp = (struct sp_link *)(scp + 1);
241 for (i = sip->si_blkcnt; i > 1; i--) {
242 slp_next = (struct sp_link *)((caddr_t)(slp + 1) +
244 slp->sl_u.slu_next = slp_next;
247 slp->sl_u.slu_next = NULL;
251 * Add new chunk to end of pool
254 sip->si_poolt->sc_next = scp;
260 sip->si_total += sip->si_blkcnt;
261 sip->si_free += sip->si_blkcnt;
262 if (sip->si_chunks > sip->si_maxused)
263 sip->si_maxused = sip->si_chunks;
267 * Allocate the first free block in chunk
270 scp->sc_freeh = slp->sl_u.slu_next;
276 * Save link back to pool chunk
278 slp->sl_u.slu_chunk = scp;
283 KM_ZERO(bp, sip->si_blksiz);
291 * Free a Control Block
293 * Returns a previously allocated control block back to the owners
297 * bp pointer to block to be freed
308 struct sp_chunk *scp;
313 * Get containing chunk and pool info
315 slp = (struct sp_link *)bp;
317 scp = slp->sl_u.slu_chunk;
318 if (scp->sc_magic != SPOOL_MAGIC)
319 panic("atm_free: chunk magic missing");
323 * Add block to free chain
326 scp->sc_freet->sl_u.slu_next = slp;
329 scp->sc_freeh = scp->sc_freet = slp;
330 slp->sl_u.slu_next = NULL;
340 * Storage Pool Compaction
342 * Called periodically in order to perform compaction of the
343 * storage pools. Each pool will be checked to see if any chunks
344 * can be freed, taking some care to avoid freeing too many chunks
345 * in order to avoid memory thrashing.
350 * tip pointer to timer control block (atm_compactimer)
358 struct atm_time *tip;
361 struct sp_chunk *scp;
363 struct sp_chunk *scp_prev;
366 * Check out all storage pools
368 for (sip = atm_pool_head; sip; sip = sip->si_next) {
371 * Always keep a minimum number of chunks around
373 if (sip->si_chunks <= SPOOL_MIN_CHUNK)
377 * Maximum chunks to free at one time will leave
378 * pool with at least 50% utilization, but never
379 * go below minimum chunk count.
381 i = ((sip->si_free * 2) - sip->si_total) / sip->si_blkcnt;
382 i = MIN(i, sip->si_chunks - SPOOL_MIN_CHUNK);
385 * Look for chunks to free
388 for (scp = sip->si_poolh; scp && i > 0; ) {
390 if (scp->sc_used == 0) {
393 * Found a chunk to free, so do it
396 scp_prev->sc_next = scp->sc_next;
397 if (sip->si_poolt == scp)
398 sip->si_poolt = scp_prev;
400 sip->si_poolh = scp->sc_next;
402 KM_FREE((caddr_t)scp, sip->si_chunksiz,
406 * Update pool controls
409 sip->si_total -= sip->si_blkcnt;
410 sip->si_free -= sip->si_blkcnt;
413 scp = scp_prev->sc_next;
424 * Restart the compaction timer
426 atm_timeout(&atm_compactimer, SPOOL_COMPACT, atm_compact);
433 * Release a Storage Pool
435 * Frees all dynamic storage acquired for a storage pool.
436 * This function is normally called just prior to a module's unloading.
439 * sip pointer to sp_info for storage pool
446 atm_release_pool(sip)
449 struct sp_chunk *scp, *scp_next;
453 * Free each chunk in pool
455 for (scp = sip->si_poolh; scp; scp = scp_next) {
458 * Check for memory leaks
461 panic("atm_release_pool: unfreed blocks");
463 scp_next = scp->sc_next;
465 KM_FREE((caddr_t)scp, sip->si_chunksiz, M_DEVBUF);
469 * Update pool controls
471 sip->si_poolh = NULL;
477 * Unlink pool from active chain
479 sip->si_chunksiz = 0;
480 UNLINK(sip, struct sp_info, atm_pool_head, si_next);
488 * Handle timer tick expiration
490 * Decrement tick count in first block on timer queue. If there
491 * are blocks with expired timers, call their timeout function.
492 * This function is called ATM_HZ times per second.
495 * arg argument passed on timeout() call
505 struct atm_time *tip;
510 * Decrement tick count
512 if (((tip = atm_timeq) == NULL) || (--tip->ti_ticks > 0)) {
517 * Stack queue should have been drained
520 if (atm_stackq_head != NULL)
521 panic("atm_timexp: stack queue not empty");
525 * Dispatch expired timers
527 while (((tip = atm_timeq) != NULL) && (tip->ti_ticks == 0)) {
528 void (*func)__P((struct atm_time *));
531 * Remove expired block from queue
533 atm_timeq = tip->ti_next;
534 tip->ti_flag &= ~TIF_QUEUED;
537 * Call timeout handler (with network interrupts locked out)
547 * Drain any deferred calls
557 (void) timeout(atm_timexp, (void *)0, hz/ATM_HZ);
564 * Schedule a control block timeout
566 * Place the supplied timer control block on the timer queue. The
567 * function (func) will be called in 't' timer ticks with the
568 * control block address as its only argument. There are ATM_HZ
569 * timer ticks per second. The ticks value stored in each block is
570 * a delta of the number of ticks from the previous block in the queue.
571 * Thus, for each tick interval, only the first block in the queue
572 * needs to have its tick value decremented.
575 * tip pointer to timer control block
576 * t number of timer ticks until expiration
577 * func pointer to function to call at expiration
584 atm_timeout(tip, t, func)
585 struct atm_time *tip;
587 void (*func)__P((struct atm_time *));
589 struct atm_time *tip1, *tip2;
594 * Check for double queueing error
596 if (tip->ti_flag & TIF_QUEUED)
597 panic("atm_timeout: double queueing");
600 * Make sure we delay at least a little bit
606 * Find out where we belong on the queue
609 for (tip1 = NULL, tip2 = atm_timeq; tip2 && (tip2->ti_ticks <= t);
610 tip1 = tip2, tip2 = tip1->ti_next) {
615 * Place ourselves on queue and update timer deltas
629 tip->ti_flag |= TIF_QUEUED;
641 * Remove the supplied timer control block from the timer queue.
644 * tip pointer to timer control block
647 * 0 control block successfully dequeued
648 * 1 control block not on timer queue
653 struct atm_time *tip;
655 struct atm_time *tip1, *tip2;
659 * Is control block queued?
661 if ((tip->ti_flag & TIF_QUEUED) == 0)
665 * Find control block on the queue
668 for (tip1 = NULL, tip2 = atm_timeq; tip2 && (tip2 != tip);
669 tip1 = tip2, tip2 = tip1->ti_next) {
678 * Remove block from queue and update timer deltas
684 tip1->ti_next = tip2;
687 tip2->ti_ticks += tip->ti_ticks;
692 tip->ti_flag &= ~TIF_QUEUED;
702 * Queues a stack call which must be deferred to the global stack queue.
703 * The call parameters are stored in entries which are allocated from the
704 * stack queue storage pool.
708 * func destination function
709 * token destination layer's token
710 * cvp pointer to connection vcc
711 * arg1 command argument
712 * arg2 command argument
716 * errno call not queued - reason indicated
720 atm_stack_enq(cmd, func, token, cvp, arg1, arg2)
722 void (*func)__P((int, void *, int, int));
728 struct stackq_entry *sqp;
732 * Get a new queue entry for this call
734 sqp = (struct stackq_entry *)atm_allocate(&atm_stackq_pool);
746 sqp->sq_token = token;
749 sqp->sq_connvc = cvp;
752 * Put new entry at end of queue
754 if (atm_stackq_head == NULL)
755 atm_stackq_head = sqp;
757 atm_stackq_tail->sq_next = sqp;
758 atm_stackq_tail = sqp;
766 * Drain the Stack Queue
768 * Dequeues and processes entries from the global stack queue.
780 struct stackq_entry *sqp, *qprev, *qnext;
785 * Loop thru entire queue until queue is empty
786 * (but panic rather loop forever)
791 for (sqp = atm_stackq_head; sqp; ) {
794 * Got an eligible entry, do STACK_CALL stuff
796 if (sqp->sq_cmd & STKCMD_UP) {
797 if (sqp->sq_connvc->cvc_downcnt) {
800 * Cant process now, skip it
808 * OK, dispatch the call
810 sqp->sq_connvc->cvc_upcnt++;
811 (*sqp->sq_func)(sqp->sq_cmd,
815 sqp->sq_connvc->cvc_upcnt--;
817 if (sqp->sq_connvc->cvc_upcnt) {
820 * Cant process now, skip it
828 * OK, dispatch the call
830 sqp->sq_connvc->cvc_downcnt++;
831 (*sqp->sq_func)(sqp->sq_cmd,
835 sqp->sq_connvc->cvc_downcnt--;
839 * Dequeue processed entry and free it
842 qnext = sqp->sq_next;
844 qprev->sq_next = qnext;
846 atm_stackq_head = qnext;
848 atm_stackq_tail = qprev;
849 atm_free((caddr_t)sqp);
855 * Make sure entire queue was drained
857 if (atm_stackq_head != NULL)
858 panic("atm_stack_drain: Queue not emptied");
865 * Process Interrupt Queue
867 * Processes entries on the ATM interrupt queue. This queue is used by
868 * device interface drivers in order to schedule events from the driver's
869 * lower (interrupt) half to the driver's stack services.
871 * The interrupt routines must store the stack processing function to call
872 * and a token (typically a driver/stack control block) at the front of the
873 * queued buffer. We assume that the function pointer and token values are
874 * both contained (and properly aligned) in the first buffer of the chain.
888 atm_intr_func_t func;
894 * Get next buffer from queue
897 IF_DEQUEUE(&atm_intrq, m);
903 * Get function to call and token value
905 KB_DATASTART(m, cp, caddr_t);
906 func = *(atm_intr_func_t *)cp;
908 token = *(void **)cp;
909 KB_HEADADJ(m, -(sizeof(func) + sizeof(token)));
910 if (KB_LEN(m) == 0) {
912 KB_UNLINKHEAD(m, m1);
917 * Call processing function
922 * Drain any deferred calls
929 NETISR_SET(NETISR_ATM, atm_intr);
934 * Print a pdu buffer chain
937 * m pointer to pdu buffer chain
938 * msg pointer to message header string
945 atm_pdu_print(m, msg)
955 KB_DATASTART(m, cp, caddr_t);
956 printf("%cbfr=%p data=%p len=%d: ",
957 c, m, cp, KB_LEN(m));
959 if (atm_print_data) {
960 for (i = 0; i < KB_LEN(m); i++) {
961 printf("%2x ", (u_char)*cp++);
963 printf("<end_bfr>\n");