2 * Copyright (c) 2000 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
;
72 gref_t
*atp_inputQ
[256];
73 struct atp_state
*atp_used_list
;
78 register gref_t
*gref
;
80 switch (gbuf_type(mp
)) {
82 gref
= atp_inputQ
[((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
];
83 if ((gref
== 0) || (gref
== (gref_t
*)1)) {
84 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: no socket, skt=%d\n",
85 ((at_ddp_t
*)gbuf_rptr(mp
))->dst_socket
));
93 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
98 dPrintf(D_M_ATP
, D_L_WARNING
, ("atp_input: unknown msg, type=%d\n",
116 atp_trans_abort
.head
= NULL
;
117 atp_trans_abort
.tail
= NULL
;
119 atp_need_rel
.head
= NULL
;
120 atp_need_rel
.tail
= NULL
;
122 bzero(atp_inputQ
, sizeof(atp_inputQ
));
123 bzero(atp_pidM
, sizeof(atp_pidM
));
129 * The open routine allocates a state structure
133 int atp_open(gref
, flag
)
137 register struct atp_state
*atp
;
142 * Allocate and init state and reply control block lists
143 * if this is the first open
145 if (atp_rcb_data
== NULL
) {
146 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_rcb
) * NATP_RCB
) != KERN_SUCCESS
)
148 if (atp_rcb_data
== NULL
) {
149 bzero((caddr_t
)temp
, sizeof(struct atp_rcb
) * NATP_RCB
);
150 atp_rcb_data
= (struct atp_rcb
*)temp
;
151 for (i
= 0; i
< NATP_RCB
; i
++) {
152 atp_rcb_data
[i
].rc_list
.next
= atp_rcb_free_list
;
153 atp_rcb_free_list
= &atp_rcb_data
[i
];
156 kmem_free(kernel_map
, temp
, sizeof(struct atp_rcb
) * NATP_RCB
); /* already allocated by another process */
159 if (atp_state_data
== NULL
) {
160 if (kmem_alloc(kernel_map
, &temp
, sizeof(struct atp_state
) * NATP_STATE
) != KERN_SUCCESS
)
162 if (atp_state_data
== NULL
) {
163 bzero((caddr_t
)temp
, sizeof(struct atp_state
) * NATP_STATE
);
164 atp_state_data
= (struct atp_state
*) temp
;
165 for (i
= 0; i
< NATP_STATE
; i
++) {
166 atp_state_data
[i
].atp_trans_waiting
= atp_free_list
;
167 atp_free_list
= &atp_state_data
[i
];
170 kmem_free(kernel_map
, temp
, sizeof(struct atp_state
) * NATP_STATE
);
175 * If no atp structure available return failure
178 if ((atp
= atp_free_list
) == NULL
)
185 atp_free_list
= atp
->atp_trans_waiting
;
188 * Initialize the data structure
192 atp
->atp_trans_wait
.head
= NULL
;
193 atp
->atp_trans_waiting
= NULL
;
194 atp
->atp_gref
= gref
;
196 atp
->atp_timeout
= HZ
/8;
197 atp
->atp_rcb_waiting
= NULL
;
198 atp
->atp_rcb
.head
= NULL
;
199 atp
->atp_flags
= T_MPSAFE
;
200 atp
->atp_socket_no
= -1;
201 atp
->atp_pid
= gref
->pid
;
203 ATEVENTINIT(atp
->atp_event
);
204 ATEVENTINIT(atp
->atp_delay_event
);
205 gref
->info
= (void *)atp
;
212 if ((atp
->atp_trans_waiting
= atp_used_list
) != 0)
213 atp
->atp_trans_waiting
->atp_rcb_waiting
= atp
;
220 * The close routine frees all the data structures
224 int atp_close(gref
, flag
)
228 extern void atp_req_timeout();
229 register struct atp_state
*atp
;
230 register struct atp_trans
*trp
;
231 register struct atp_rcb
*rcbp
;
235 atp
= (struct atp_state
*)gref
->info
;
237 atp
= (struct atp_state
*)atp
->atp_msgq
;
239 gbuf_freem(atp
->atp_msgq
);
243 atp
->atp_flags
|= ATP_CLOSING
;
244 socket
= atp
->atp_socket_no
;
246 atp_inputQ
[socket
] = (gref_t
*)1;
249 * blow away all pending timers
251 for (trp
= atp
->atp_trans_wait
.head
; trp
; trp
= trp
->tr_list
.next
)
252 atp_untimout(atp_req_timeout
, trp
);
255 * Release pending transactions + rcbs
257 while ((trp
= atp
->atp_trans_wait
.head
))
259 while ((rcbp
= atp
->atp_rcb
.head
))
261 while ((rcbp
= atp
->atp_attached
.head
))
264 if (flag
&& (socket
== -1))
265 atp_dequeue_atp(atp
);
268 * free the state variable
270 atp
->atp_socket_no
= -1;
271 atp
->atp_trans_waiting
= atp_free_list
;
275 pid
= (pid_t
)atp_pidM
[socket
];
276 atp_pidM
[socket
] = 0;
277 atp_inputQ
[socket
] = NULL
;
279 ddp_notify_nbp(socket
, pid
, DDP_ATP
);