1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
/*
*
* ===================================
* HARP | Host ATM Research Platform
* ===================================
*
*
* This Host ATM Research Platform ("HARP") file (the "Software") is
* made available by Network Computing Services, Inc. ("NetworkCS")
* "AS IS". NetworkCS does not provide maintenance, improvements or
* support of any kind.
*
* NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
* INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
* SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
* In no event shall NetworkCS be responsible for any damages, including
* but not limited to consequential damages, arising from or relating to
* any use of the Software or related support.
*
* Copyright 1994-1998 Network Computing Services, Inc.
*
* Copies of this Software may be made, however, the above copyright
* notice must be reproduced on all copies.
*
* @(#) $FreeBSD$
*
*/
/*
* Core ATM Services
* -----------------
*
* ATM device support functions
*
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <netatm/port.h>
#include <netatm/queue.h>
#include <netatm/atm.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_cm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_vc.h>
#include <netatm/atm_stack.h>
#include <netatm/atm_pcb.h>
#include <netatm/atm_var.h>
#ifndef lint
__RCSID("@(#) $FreeBSD$");
#endif
/*
* Private structures for managing allocated kernel memory resources
*
* For each allocation of kernel memory, one Mem_ent will be used.
* The Mem_ent structures will be allocated in blocks inside of a
* Mem_blk structure.
*/
#define MEM_NMEMENT 10 /* How many Mem_ent's in a Mem_blk */
struct mem_ent {
void *me_kaddr; /* Allocated memory address */
u_int me_ksize; /* Allocated memory length */
void *me_uaddr; /* Memory address returned to caller */
u_int me_flags; /* Flags (see below) */
};
typedef struct mem_ent Mem_ent;
/*
* Memory entry flags
*/
#define MEF_NONCACHE 1 /* Memory is noncacheable */
struct mem_blk {
struct mem_blk *mb_next; /* Next block in chain */
Mem_ent mb_mement[MEM_NMEMENT]; /* Allocated memory entries */
};
typedef struct mem_blk Mem_blk;
static Mem_blk *atm_mem_head = NULL;
static struct t_atm_cause atm_dev_cause = {
T_ATM_ITU_CODING,
T_ATM_LOC_USER,
T_ATM_CAUSE_VPCI_VCI_ASSIGNMENT_FAILURE,
{0, 0, 0, 0}
};
/*
* ATM Device Stack Instantiation
*
* Called at splnet.
*
* Arguments
* ssp pointer to array of stack definition pointers
* for connection
* ssp[0] points to upper layer's stack definition
* ssp[1] points to this layer's stack definition
* ssp[2] points to lower layer's stack definition
* cvcp pointer to connection vcc for this stack
*
* Returns
* 0 instantiation successful
* err instantiation failed - reason indicated
*
*/
int
atm_dev_inst(ssp, cvcp)
struct stack_defn **ssp;
Atm_connvc *cvcp;
{
Cmn_unit *cup = (Cmn_unit *)cvcp->cvc_attr.nif->nif_pif;
Cmn_vcc *cvp;
int err;
/*
* Check to see if device has been initialized
*/
if ((cup->cu_flags & CUF_INITED) == 0)
return ( EIO );
/*
* Validate lower SAP
*/
/*
* Device driver is the lowest layer - no need to validate
*/
/*
* Validate PVC vpi.vci
*/
if (cvcp->cvc_attr.called.addr.address_format == T_ATM_PVC_ADDR) {
/*
* Look through existing circuits - return error if found
*/
Atm_addr_pvc *pp;
pp = (Atm_addr_pvc *)cvcp->cvc_attr.called.addr.address;
if (atm_dev_vcc_find(cup, ATM_PVC_GET_VPI(pp),
ATM_PVC_GET_VCI(pp), 0))
return ( EADDRINUSE );
}
/*
* Validate our SAP type
*/
switch ((*(ssp+1))->sd_sap) {
case SAP_CPCS_AAL3_4:
case SAP_CPCS_AAL5:
case SAP_ATM:
break;
default:
return (EINVAL);
}
/*
* Allocate a VCC control block
*/
cvp = uma_zalloc(cup->cu_vcc_zone, M_WAITOK);
if (cvp == NULL)
return (ENOMEM);
cvp->cv_state = CVS_INST;
cvp->cv_toku = (*ssp)->sd_toku;
cvp->cv_upper = (*ssp)->sd_upper;
cvp->cv_connvc = cvcp;
/*
* Let device have a look at the connection request
*/
err = (*cup->cu_instvcc)(cup, cvp);
if (err) {
uma_zfree(cup->cu_vcc_zone, cvp);
return (err);
}
/*
* Looks good so far, so link in device VCC
*/
LINK2TAIL ( cvp, Cmn_vcc, cup->cu_vcc, cv_next );
/*
* Save my token
*/
(*++ssp)->sd_toku = cvp;
/*
* Pass instantiation down the stack
*/
/*
* No need - we're the lowest point.
*/
/* err = (*(ssp + 1))->sd_inst(ssp, cvcp); */
/*
* Save the lower layer's interface info
*/
/*
* No need - we're the lowest point
*/
/* cvp->cv_lower = (*++ssp)->sd_lower; */
/* cvp->cv_tok1 = (*ssp)->sd_toku; */
return (0);
}
/*
* ATM Device Stack Command Handler
*
* Arguments
* cmd stack command code
* tok session token (Cmn_vcc)
* arg1 command specific argument
* arg2 command specific argument
*
* Returns
* none
*
*/
/*ARGSUSED*/
void
atm_dev_lower(cmd, tok, arg1, arg2)
int cmd;
void *tok;
intptr_t arg1;
intptr_t arg2;
{
Cmn_vcc *cvp = (Cmn_vcc *)tok;
Atm_connvc *cvcp = cvp->cv_connvc;
Cmn_unit *cup = (Cmn_unit *)cvcp->cvc_attr.nif->nif_pif;
struct vccb *vcp;
u_int state;
int s;
switch ( cmd ) {
case CPCS_INIT:
/*
* Sanity check
*/
if ( cvp->cv_state != CVS_INST ) {
log ( LOG_ERR,
"atm_dev_lower: INIT: tok=%p, state=%d\n",
tok, cvp->cv_state );
break;
}
vcp = cvp->cv_connvc->cvc_vcc;
/*
* Validate SVC vpi.vci
*/
if ( vcp->vc_type & VCC_SVC ) {
if (atm_dev_vcc_find(cup, vcp->vc_vpi, vcp->vc_vci,
vcp->vc_type & (VCC_IN | VCC_OUT))
!= cvp){
log ( LOG_ERR,
"atm_dev_lower: dup SVC (%d,%d) tok=%p\n",
vcp->vc_vpi, vcp->vc_vci, tok );
atm_cm_abort(cvp->cv_connvc, &atm_dev_cause);
break;
}
}
/*
* Tell the device to open the VCC
*/
cvp->cv_state = CVS_INITED;
s = splimp();
if ((*cup->cu_openvcc)(cup, cvp)) {
atm_cm_abort(cvp->cv_connvc, &atm_dev_cause);
(void) splx(s);
break;
}
(void) splx(s);
break;
case CPCS_TERM: {
KBuffer *m, *prev, *next;
int *ip;
s = splimp();
/*
* Disconnect the VCC - ignore return code
*/
if ((cvp->cv_state == CVS_INITED) ||
(cvp->cv_state == CVS_ACTIVE)) {
(void) (*cup->cu_closevcc)(cup, cvp);
}
cvp->cv_state = CVS_TERM;
/*
* Remove from interface list
*/
UNLINK ( cvp, Cmn_vcc, cup->cu_vcc, cv_next );
/*
* Free any buffers from this VCC on the ATM interrupt queue
*/
prev = NULL;
IF_LOCK(&atm_intrq);
for (m = atm_intrq.ifq_head; m; m = next) {
next = KB_QNEXT(m);
/*
* See if this entry is for the terminating VCC
*/
KB_DATASTART(m, ip, int *);
ip++;
if (*ip == (intptr_t)cvp) {
/*
* Yep, so dequeue the entry
*/
if (prev == NULL)
atm_intrq.ifq_head = next;
else
KB_QNEXT(prev) = next;
if (next == NULL)
atm_intrq.ifq_tail = prev;
atm_intrq.ifq_len--;
/*
* Free the unwanted buffers
*/
KB_FREEALL(m);
} else {
prev = m;
}
}
IF_UNLOCK(&atm_intrq);
(void) splx(s);
/*
* Free VCC resources
*/
uma_zfree(cup->cu_vcc_zone, cvp);
break;
}
case CPCS_UNITDATA_INV:
/*
* Sanity check
*
* Use temp state variable since we dont want to lock out
* interrupts, but initial VC activation interrupt may
* happen here, changing state somewhere in the middle.
*/
state = cvp->cv_state;
if ((state != CVS_ACTIVE) &&
(state != CVS_INITED)) {
log ( LOG_ERR,
"atm_dev_lower: UNITDATA: tok=%p, state=%d\n",
tok, state );
KB_FREEALL((KBuffer *)arg1);
break;
}
/*
* Hand the data off to the device
*/
(*cup->cu_output)(cup, cvp, (KBuffer *)arg1);
break;
case CPCS_UABORT_INV:
log ( LOG_ERR,
"atm_dev_lower: unimplemented stack cmd 0x%x, tok=%p\n",
cmd, tok );
break;
default:
log ( LOG_ERR,
"atm_dev_lower: unknown stack cmd 0x%x, tok=%p\n",
cmd, tok );
}
return;
}
/*
* Allocate kernel memory block
*
* This function will allocate a kernel memory block of the type specified
* in the flags parameter. The returned address will point to a memory
* block of the requested size and alignment. The memory block will also
* be zeroed. The alloc/free functions will manage/mask both the OS-specific
* kernel memory management requirements and the bookkeeping required to
* deal with data alignment issues.
*
* This function should not be called from interrupt level.
*
* Arguments:
* size size of memory block to allocate
* align data alignment requirement
* flags allocation flags (ATM_DEV_*)
*
* Returns:
* uaddr pointer to aligned memory block
* NULL unable to allocate memory
*
*/
void *
atm_dev_alloc(size, align, flags)
u_int size;
u_int align;
u_int flags;
{
Mem_blk *mbp;
Mem_ent *mep;
u_int kalign, ksize;
int s, i;
s = splimp();
/*
* Find a free Mem_ent
*/
mep = NULL;
for (mbp = atm_mem_head; mbp && mep == NULL; mbp = mbp->mb_next) {
for (i = 0; i < MEM_NMEMENT; i++) {
if (mbp->mb_mement[i].me_uaddr == NULL) {
mep = &mbp->mb_mement[i];
break;
}
}
}
/*
* If there are no free Mem_ent's, then allocate a new Mem_blk
* and link it into the chain
*/
if (mep == NULL) {
mbp = malloc(sizeof(Mem_blk), M_DEVBUF, M_NOWAIT|M_ZERO);
if (mbp == NULL) {
log(LOG_ERR, "atm_dev_alloc: Mem_blk failure\n");
(void) splx(s);
return (NULL);
}
mbp->mb_next = atm_mem_head;
atm_mem_head = mbp;
mep = mbp->mb_mement;
}
/*
* Now we need to get the kernel's allocation alignment minimum
*
* This is obviously very OS-specific stuff
*/
kalign = MINALLOCSIZE;
/*
* Figure out how much memory we must allocate to satify the
* user's size and alignment needs
*/
if (align <= kalign)
ksize = size;
else
ksize = size + align - kalign;
/*
* Finally, go get the memory
*/
if (flags & ATM_DEV_NONCACHE) {
mep->me_kaddr = malloc(ksize, M_DEVBUF, M_NOWAIT);
} else {
mep->me_kaddr = malloc(ksize, M_DEVBUF, M_NOWAIT);
}
if (mep->me_kaddr == NULL) {
log(LOG_ERR, "atm_dev_alloc: %skernel memory unavailable\n",
(flags & ATM_DEV_NONCACHE) ? "non-cacheable " : "");
(void) splx(s);
return (NULL);
}
/*
* Calculate correct alignment address to pass back to user
*/
mep->me_uaddr = (void *) roundup((uintptr_t)mep->me_kaddr, align);
mep->me_ksize = ksize;
mep->me_flags = flags;
/*
* Clear memory for user
*/
bzero(mep->me_uaddr, size);
ATM_DEBUG4("atm_dev_alloc: size=%d, align=%d, flags=%d, uaddr=%p\n",
size, align, flags, mep->me_uaddr);
(void) splx(s);
return (mep->me_uaddr);
}
/*
* Free kernel memory block
*
* This function will free a kernel memory block previously allocated by
* the atm_dev_alloc function.
*
* This function should not be called from interrupt level.
*
* Arguments:
* uaddr pointer to allocated aligned memory block
*
* Returns:
* none
*
*/
void
atm_dev_free(uaddr)
volatile void *uaddr;
{
Mem_blk *mbp;
Mem_ent *mep;
int s, i;
ATM_DEBUG1("atm_dev_free: uaddr=%p\n", uaddr);
s = splimp();
/*
* Protect ourselves...
*/
if (uaddr == NULL)
panic("atm_dev_free: trying to free null address");
/*
* Find our associated entry
*/
mep = NULL;
for (mbp = atm_mem_head; mbp && mep == NULL; mbp = mbp->mb_next) {
for (i = 0; i < MEM_NMEMENT; i++) {
if (mbp->mb_mement[i].me_uaddr == uaddr) {
mep = &mbp->mb_mement[i];
break;
}
}
}
/*
* If we didn't find our entry, then unceremoniously let the caller
* know they screwed up (it certainly couldn't be a bug here...)
*/
if (mep == NULL)
panic("atm_dev_free: trying to free unknown address");
/*
* Give the memory space back to the kernel
*/
if (mep->me_flags & ATM_DEV_NONCACHE) {
free(mep->me_kaddr, M_DEVBUF);
} else {
free(mep->me_kaddr, M_DEVBUF);
}
/*
* Free our entry
*/
mep->me_uaddr = NULL;
(void) splx(s);
return;
}
/*
* Compress buffer chain
*
* This function will compress a supplied buffer chain into a minimum number
* of kernel buffers. Typically, this function will be used because the
* number of buffers in an output buffer chain is too large for a device's
* DMA capabilities. This should only be called as a last resort, since
* all the data copying will surely kill any hopes of decent performance.
*
* Arguments:
* m pointer to source buffer chain
*
* Returns:
* n pointer to compressed buffer chain
*
*/
KBuffer *
atm_dev_compress(m)
KBuffer *m;
{
KBuffer *n, *n0, **np;
int len, space;
caddr_t src, dst;
n = n0 = NULL;
np = &n0;
dst = NULL;
space = 0;
/*
* Copy each source buffer into compressed chain
*/
while (m) {
if (space == 0) {
/*
* Allocate another buffer for compressed chain
*/
KB_ALLOCEXT(n, ATM_DEV_CMPR_LG, KB_F_NOWAIT, KB_T_DATA);
if (n) {
space = ATM_DEV_CMPR_LG;
} else {
KB_ALLOC(n, ATM_DEV_CMPR_SM, KB_F_NOWAIT,
KB_T_DATA);
if (n) {
space = ATM_DEV_CMPR_SM;
} else {
/*
* Unable to get any new buffers, so
* just return the partially compressed
* chain and hope...
*/
*np = m;
break;
}
}
KB_HEADSET(n, 0);
KB_LEN(n) = 0;
KB_BFRSTART(n, dst, caddr_t);
*np = n;
np = &KB_NEXT(n);
}
/*
* Copy what we can from source buffer
*/
len = MIN(space, KB_LEN(m));
KB_DATASTART(m, src, caddr_t);
bcopy(src, dst, len);
/*
* Adjust for copied data
*/
dst += len;
space -= len;
KB_HEADADJ(m, -len);
KB_TAILADJ(n, len);
/*
* If we've exhausted our current source buffer, free it
* and move to the next one
*/
if (KB_LEN(m) == 0) {
KB_FREEONE(m, m);
}
}
return (n0);
}
/*
* Locate VCC entry
*
* This function will return the VCC entry for a specified interface and
* VPI/VCI value.
*
* Arguments:
* cup pointer to interface unit structure
* vpi VPI value
* vci VCI value
* type VCC type
*
* Returns:
* vcp pointer to located VCC entry matching
* NULL no VCC found
*
*/
Cmn_vcc *
atm_dev_vcc_find(cup, vpi, vci, type)
Cmn_unit *cup;
u_int vpi;
u_int vci;
u_int type;
{
Cmn_vcc *cvp;
int s = splnet();
/*
* Go find VCC
*
* (Probably should stick in a hash table some time)
*/
for (cvp = cup->cu_vcc; cvp; cvp = cvp->cv_next) {
struct vccb *vcp;
vcp = cvp->cv_connvc->cvc_vcc;
if ((vcp->vc_vci == vci) && (vcp->vc_vpi == vpi) &&
((vcp->vc_type & type) == type))
break;
}
(void) splx(s);
return (cvp);
}
#ifdef notdef
/*
* Module unloading notification
*
* This function must be called just prior to unloading the module from
* memory. All allocated memory will be freed here and anything else that
* needs cleaning up.
*
* Arguments:
* none
*
* Returns:
* none
*
*/
void
atm_unload()
{
Mem_blk *mbp;
Mem_ent *mep;
int s, i;
s = splimp();
/*
* Free up all of our memory management storage
*/
while (mbp = atm_mem_head) {
/*
* Make sure users have freed up all of their memory
*/
for (i = 0; i < MEM_NMEMENT; i++) {
if (mbp->mb_mement[i].me_uaddr != NULL) {
panic("atm_unload: unfreed memory");
}
}
atm_mem_head = mbp->mb_next;
/*
* Hand this block back to the kernel
*/
free((caddr_t)mbp, M_DEVBUF);
}
(void) splx(s);
return;
}
#endif /* notdef */
/*
* Print a PDU
*
* Arguments:
* cup pointer to device unit
* cvp pointer to VCC control block
* m pointer to pdu buffer chain
* msg pointer to message string
*
* Returns:
* none
*
*/
void
atm_dev_pdu_print(cup, cvp, m, msg)
Cmn_unit *cup;
Cmn_vcc *cvp;
KBuffer *m;
char *msg;
{
char buf[128];
snprintf(buf, sizeof(buf), "%s vcc=(%d,%d)", msg,
cvp->cv_connvc->cvc_vcc->vc_vpi,
cvp->cv_connvc->cvc_vcc->vc_vci);
atm_pdu_print(m, buf);
}
|