2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
35 /* Modified for MP, 1996 by Tuyen Nguyen
36 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
40 #include <sys/errno.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <machine/spl.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/filedesc.h>
48 #include <sys/fcntl.h>
50 #include <sys/ioctl.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <vm/vm_kern.h> /* for kernel_map */
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/ddp.h>
58 #include <netat/at_pcb.h>
59 #include <netat/atp.h>
60 #include <netat/debug.h>
63 * The init routine creates all the free lists
64 * Version 1.4 of atp_open.c on 89/02/09 17:53:11
68 struct atp_rcb_qhead atp_need_rel
;
75 gref_t
*atp_inputQ
[256];
76 struct atp_state
*atp_used_list
;
81 register gref_t
*gref
;
83 switch (gbuf_type(mp
)) {
85 gref
= atp_inputQ
[((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
];
86 if ((gref
== 0) || (gref
== (gref_t
*)1)) {
87 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: no socket, skt=%d\n",
88 ((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
));
96 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
101 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: unknown msg, type=%d\n",
119 atp_trans_abort
.head
= NULL
;
120 atp_trans_abort
.tail
= NULL
;
122 atp_need_rel
.head
= NULL
;
123 atp_need_rel
.tail
= NULL
;
125 bzero(atp_inputQ
, sizeof(atp_inputQ
));
126 bzero(atp_pidM
, sizeof(atp_pidM
));
132 * The open routine allocates a state structure
136 int atp_open(gref
, flag
)
140 register struct atp_state
*atp
;
145 * Allocate and init state and reply control block lists
146 * if this is the first open
148 if (atp_rcb_data
== NULL
) {
149 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_rcb
) * NATP_RCB
) != KERN_SUCCESS
)
151 if (atp_rcb_data
== NULL
) {
152 bzero((caddr_t
)temp
, sizeof(struct atp_rcb
) * NATP_RCB
);
153 atp_rcb_data
= (struct atp_rcb
*)temp
;
154 for (i
= 0; i
< NATP_RCB
; i
++) {
155 atp_rcb_data
[i
].rc_list
.next
= atp_rcb_free_list
;
156 atp_rcb_free_list
= &atp_rcb_data
[i
];
159 kmem_free(kernel_map
, temp
, sizeof(struct atp_rcb
) * NATP_RCB
); /* already allocated by another process */
162 if (atp_state_data
== NULL
) {
163 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_state
) * NATP_STATE
) != KERN_SUCCESS
)
165 if (atp_state_data
== NULL
) {
166 bzero((caddr_t
)temp
, sizeof(struct atp_state
) * NATP_STATE
);
167 atp_state_data
= (struct atp_state
*) temp
;
168 for (i
= 0; i
< NATP_STATE
; i
++) {
169 atp_state_data
[i
].atp_trans_waiting
= atp_free_list
;
170 atp_free_list
= &atp_state_data
[i
];
173 kmem_free(kernel_map
, temp
, sizeof(struct atp_state
) * NATP_STATE
);
178 * If no atp structure available return failure
181 ATDISABLE(s
, atpall_lock
);
182 if ((atp
= atp_free_list
) == NULL
) {
183 ATENABLE(s
, atpall_lock
);
191 atp_free_list
= atp
->atp_trans_waiting
;
192 ATENABLE(s
, atpall_lock
);
195 * Initialize the data structure
199 atp
->atp_trans_wait
.head
= NULL
;
200 atp
->atp_trans_waiting
= NULL
;
201 atp
->atp_gref
= gref
;
203 atp
->atp_timeout
= HZ
/8;
204 atp
->atp_rcb_waiting
= NULL
;
205 atp
->atp_rcb
.head
= NULL
;
206 atp
->atp_flags
= T_MPSAFE
;
207 atp
->atp_socket_no
= -1;
208 atp
->atp_pid
= gref
->pid
;
210 ATLOCKINIT(atp
->atp_lock
);
211 ATLOCKINIT(atp
->atp_delay_lock
);
212 ATEVENTINIT(atp
->atp_event
);
213 ATEVENTINIT(atp
->atp_delay_event
);
214 gref
->info
= (void *)atp
;
221 ATDISABLE(s
, atpall_lock
);
222 if ((atp
->atp_trans_waiting
= atp_used_list
) != 0)
223 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
;
225 ATENABLE(s
, atpall_lock
);
231 * The close routine frees all the data structures
235 int atp_close(gref
, flag
)
239 extern void atp_req_timeout();
240 register struct atp_state
*atp
;
241 register struct atp_trans
*trp
;
242 register struct atp_rcb
*rcbp
;
247 atp
= (struct atp_state
*)gref
->info
;
249 atp
= (struct atp_state
*)atp
->atp_msgq
;
251 gbuf_freem(atp
->atp_msgq
);
255 ATDISABLE(s
, atp
->atp_lock
);
256 atp
->atp_flags
|= ATP_CLOSING
;
257 socket
= atp
->atp_socket_no
;
259 atp_inputQ
[socket
] = (gref_t
*)1;
262 * blow away all pending timers
264 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
)
265 atp_untimout(atp_req_timeout
, trp
);
268 * Release pending transactions + rcbs
270 while ((trp
= atp
->atp_trans_wait
.head
))
272 while ((rcbp
= atp
->atp_rcb
.head
))
274 while ((rcbp
= atp
->atp_attached
.head
))
276 ATENABLE(s
, atp
->atp_lock
);
278 if (flag
&& (socket
== -1))
279 atp_dequeue_atp(atp
);
282 * free the state variable
284 ATDISABLE(s
, atpall_lock
);
285 atp
->atp_socket_no
= -1;
286 atp
->atp_trans_waiting
= atp_free_list
;
288 ATENABLE(s
, atpall_lock
);
291 pid
= (pid_t
)atp_pidM
[socket
];
292 atp_pidM
[socket
] = 0;
293 atp_inputQ
[socket
] = NULL
;
295 ddp_notify_nbp(socket
, pid
, DDP_ATP
);