2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1996-1998 Apple Computer, Inc.
27 * All Rights Reserved.
30 /* Modified for MP, 1996 by Tuyen Nguyen
31 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
45 #include <sys/ioctl.h>
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <vm/vm_kern.h> /* for kernel_map */
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/ddp.h>
53 #include <netat/at_pcb.h>
54 #include <netat/atp.h>
55 #include <netat/debug.h>
58 * The init routine creates all the free lists
59 * Version 1.4 of atp_open.c on 89/02/09 17:53:11
63 struct atp_rcb_qhead atp_need_rel
;
70 gref_t
*atp_inputQ
[256];
71 struct atp_state
*atp_used_list
;
76 register gref_t
*gref
;
78 switch (gbuf_type(mp
)) {
80 gref
= atp_inputQ
[((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
];
81 if ((gref
== 0) || (gref
== (gref_t
*)1)) {
82 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: no socket, skt=%d\n",
83 ((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
));
91 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
96 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: unknown msg, type=%d\n",
114 atp_trans_abort
.head
= NULL
;
115 atp_trans_abort
.tail
= NULL
;
117 atp_need_rel
.head
= NULL
;
118 atp_need_rel
.tail
= NULL
;
120 bzero(atp_inputQ
, sizeof(atp_inputQ
));
121 bzero(atp_pidM
, sizeof(atp_pidM
));
127 * The open routine allocates a state structure
131 int atp_open(gref
, flag
)
135 register struct atp_state
*atp
;
140 * Allocate and init state and reply control block lists
141 * if this is the first open
143 if (atp_rcb_data
== NULL
) {
144 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_rcb
) * NATP_RCB
) != KERN_SUCCESS
)
146 if (atp_rcb_data
== NULL
) { /* in case we lost funnel while allocating */
147 bzero((caddr_t
)temp
, sizeof(struct atp_rcb
) * NATP_RCB
);
148 atp_rcb_data
= (struct atp_rcb
*)temp
;
149 for (i
= 0; i
< NATP_RCB
; i
++) {
150 atp_rcb_data
[i
].rc_list
.next
= atp_rcb_free_list
;
151 atp_rcb_free_list
= &atp_rcb_data
[i
];
154 kmem_free(kernel_map
, temp
, sizeof(struct atp_rcb
) * NATP_RCB
); /* already allocated by another process */
157 if (atp_state_data
== NULL
) {
158 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_state
) * NATP_STATE
) != KERN_SUCCESS
)
160 if (atp_state_data
== NULL
) {
161 bzero((caddr_t
)temp
, sizeof(struct atp_state
) * NATP_STATE
);
162 atp_state_data
= (struct atp_state
*) temp
;
163 for (i
= 0; i
< NATP_STATE
; i
++) {
164 atp_state_data
[i
].atp_trans_waiting
= atp_free_list
;
165 atp_free_list
= &atp_state_data
[i
];
168 kmem_free(kernel_map
, temp
, sizeof(struct atp_state
) * NATP_STATE
);
173 * If no atp structure available return failure
176 ATDISABLE(s
, atpall_lock
);
177 if ((atp
= atp_free_list
) == NULL
) {
178 ATENABLE(s
, atpall_lock
);
186 atp_free_list
= atp
->atp_trans_waiting
;
187 ATENABLE(s
, atpall_lock
);
190 * Initialize the data structure
194 atp
->atp_trans_wait
.head
= NULL
;
195 atp
->atp_trans_waiting
= NULL
;
196 atp
->atp_gref
= gref
;
198 atp
->atp_timeout
= HZ
/8;
199 atp
->atp_rcb_waiting
= NULL
;
200 atp
->atp_rcb
.head
= NULL
;
201 atp
->atp_flags
= T_MPSAFE
;
202 atp
->atp_socket_no
= -1;
203 atp
->atp_pid
= gref
->pid
;
205 ATLOCKINIT(atp
->atp_lock
);
206 ATLOCKINIT(atp
->atp_delay_lock
);
207 ATEVENTINIT(atp
->atp_event
);
208 ATEVENTINIT(atp
->atp_delay_event
);
209 gref
->info
= (void *)atp
;
216 ATDISABLE(s
, atpall_lock
);
217 if ((atp
->atp_trans_waiting
= atp_used_list
) != 0)
218 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
;
220 ATENABLE(s
, atpall_lock
);
226 * The close routine frees all the data structures
230 int atp_close(gref
, flag
)
234 extern void atp_req_timeout();
235 register struct atp_state
*atp
;
236 register struct atp_trans
*trp
;
237 register struct atp_rcb
*rcbp
;
242 atp
= (struct atp_state
*)gref
->info
;
244 atp
= (struct atp_state
*)atp
->atp_msgq
;
246 gbuf_freem(atp
->atp_msgq
);
250 ATDISABLE(s
, atp
->atp_lock
);
251 atp
->atp_flags
|= ATP_CLOSING
;
252 socket
= atp
->atp_socket_no
;
254 atp_inputQ
[socket
] = (gref_t
*)1;
257 * blow away all pending timers
259 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
)
260 atp_untimout(atp_req_timeout
, trp
);
263 * Release pending transactions + rcbs
265 while ((trp
= atp
->atp_trans_wait
.head
))
267 while ((rcbp
= atp
->atp_rcb
.head
))
269 while ((rcbp
= atp
->atp_attached
.head
))
271 ATENABLE(s
, atp
->atp_lock
);
273 if (flag
&& (socket
== -1))
274 atp_dequeue_atp(atp
);
277 * free the state variable
279 ATDISABLE(s
, atpall_lock
);
280 atp
->atp_socket_no
= -1;
281 atp
->atp_trans_waiting
= atp_free_list
;
283 ATENABLE(s
, atpall_lock
);
286 pid
= (pid_t
)atp_pidM
[socket
];
287 atp_pidM
[socket
] = 0;
288 atp_inputQ
[socket
] = NULL
;
290 ddp_notify_nbp(socket
, pid
, DDP_ATP
);