]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/adsp_Close.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / netat / adsp_Close.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
30 * All Rights Reserved.
31 */
32
33/* dspClose.c
34 * From Mike Shoemaker v01.16 06/29/90 mbs
35 */
36/*
37 * Change log:
38 * 06/29/95 - Modified to handle flow control for writing (Tuyen Nguyen)
39 * Modified for MP, 1996 by Tuyen Nguyen
40 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
41 */
42
43#include <sys/errno.h>
44#include <sys/types.h>
45#include <sys/param.h>
46#include <machine/spl.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/proc.h>
50#include <sys/filedesc.h>
51#include <sys/fcntl.h>
52#include <sys/mbuf.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/time.h>
56
57#include <netat/sysglue.h>
58#include <netat/appletalk.h>
59#include <netat/ddp.h>
60#include <netat/at_pcb.h>
61#include <netat/debug.h>
62#include <netat/adsp.h>
63#include <netat/adsp_internal.h>
64
1c79356b
A
65
66static void qRemove(CCBPtr, CCBPtr);
67
68
69/*
70 * CheckOkToClose
71 *
72 * Check to see if it is OK to close this connection cleanly.
73 *
74 * INPUTS:
75 * Stream pointer
76 * OUTPUTS:
77 * True if no outstanding transactions and we can close cleanly
78 */
79int CheckOkToClose(sp) /* (CCBPtr sp) */
80 CCBPtr sp;
81{
82
83 if (sp->sData) /* Outstanding data ? */
84 return 0;
85
86 if (sp->sapb) /* Outstanding send attention ? */
87 return 0;
88
89 if (sp->frpb) /* Outstanding forward reset ? */
90 return 0;
91
92 if (sp->sendAttnAck)
93 return 0;
94
95 if (sp->sendDataAck)
96 return 0;
97
98 /*
99 * Must be OK to close
100 */
101 sp->sendCtl |= B_CTL_CLOSE; /* So, need to send close advice */
102 sp->callSend = 1;
103
104 return 1; /* It's OK to close */
105}
106
107
108/*
109 * CompleteQueue
110 *
111 * Given the address of the head of a queue of DSP parameter blocks, zero
112 * the queue, and complete each item on the queue with the given result
113 * code.
114 *
115 * INPUTS:
116 * qhead Address of ptr to first queue element
117 * code The result code
118 * OUTPUTS:
119 * none
120 */
121int CompleteQueue(qhead, code) /* (DSPPBPtr FPTR qhead, OSErr code) */
122 struct adspcmd **qhead;
123 int code;
124{
125 register struct adspcmd *p;
126 register struct adspcmd *n;
127 register gref_t *gref;
128 register int total = 0;
129 CCBPtr sp = 0;
1c79356b
A
130
131 n = *qhead; /* Get first item */
132 *qhead = 0; /* Zero out the queue */
133 if (n) {
134 gref = n->gref;
135 if (gref->info) {
136 sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
137 atalk_flush(sp->gref);
1c79356b
A
138 }
139 }
140
141 while (p = n) { /* while items left */
142 n = (struct adspcmd *)(p->qLink); /* Save next guy */
143 p->ioResult = code;
144 if (sp) {
145 completepb(sp, p); /* complete the copy of the request */
146 total++;
147 } else
148 gbuf_freem(p->mp);
149 } /* while */
1c79356b
A
150 return(total);
151}
152
153/*
154 * RemoveCCB
155 *
156 * Called from do close to free up the user's CCB. So, we remove the
157 * CCB from the list of CCB's.
158 *
159 * INPUTS:
160 * sp pointer to ccb
161 * pb a remove param block to complete when done
162 * OUTPUTS:
163 * none
164 */
165
166void RemoveCCB(sp, pb) /* (CCBPtr sp, DSPPBPtr pb) */
167 CCBPtr sp;
168 struct adspcmd *pb;
169{
170 gref_t *gref;
171
172 if (sp->gref == 0)
173 return;
174 /*
175 * Unlink CCB from list
176 */
55e303ae 177 qRemove((CCB *)AT_ADSP_STREAMS, sp); /* remove sp from active streams queue */
1c79356b
A
178
179 if (pb) {
180 pb->ioResult = 0;
181 if (pb->ioc) /* is this a current or queued request */
182 adspioc_ack(0, pb->ioc, pb->gref); /* current */
183 else {
184 completepb(sp, pb); /* queued */
185 }
186
187 if (sp->opb && (pb != sp->opb)) { /* if the pb requested is not the */
188 pb = sp->opb; /* waiting open pb, complete it too */
189 sp->opb = 0;
190 pb->ioResult = 0;
191 completepb(sp, pb);
192 } else {
193 sp->opb = 0;
194 }
195 }
196 gref = sp->gref;
197 sp->gref = 0;
198 if (gref->info == (char *)sp->sp_mp) { /* queue head is still valid */
199 unsigned char skt;
200
201 if ((skt = sp->localSocket) != 0) {
202 if (adspDeassignSocket(sp) == 0)
203 ddp_notify_nbp(skt, sp->pid, DDP_ADSP);
204 }
205
206 if (gref->info) {
207 gbuf_freem((gbuf_t *)gref->info); /* free the CCB */
208 gref->info = 0;
209 }
210 } else
211 gbuf_freem(sp->sp_mp); /* our head is already gone, be sure
212 * to release our resources too */
213}
214
215int AbortIO(sp, err)
216 CCBPtr sp;
217 short err;
218{
219 register int total;
220
221 if (sp->gref == 0)
222 return 0;
223 /*
224 * Complete all outstanding transactions.
225 */
91447636 226 total = CompleteQueue(&sp->sapb, err); /* Abort outstanding send attentions */
1c79356b
A
227 CompleteQueue(&sp->frpb, err); /* Abort outstanding forward resets */
228
229 if (sp->sbuf_mb) { /* clear the send queue */
230 gbuf_freel(sp->sbuf_mb);
231 sp->sbuf_mb = 0;
232 }
233
234 if (sp->csbuf_mb) {
235 gbuf_freem(sp->csbuf_mb);
236 sp->csbuf_mb = 0;
237 }
238 sp->sData = 0;
239
240 return(total);
241}
242
243/*
244 * DoClose
245 *
246 * Called from several places (probe timeout, recv close advice,
247 * dspRemove, etc.) to change state of connection to closed and
248 * complete all outstanding I/O.
249 *
250 * Will also remove the CCB if there is a dsp remove pending.
251 *
252 * INPUTS:
253 * sp An ADSP stream
254 * OUTPUTS:
255 * none
256 */
257void DoClose(sp, err, force_abort) /* (CCBPtr sp, OSErr err) */
258 register CCBPtr sp;
259 int err;
260{
261 register struct adspcmd *pb, *np;
262 register gbuf_t *mp;
263 int aborted_count;
264
265 dPrintf(D_M_ADSP, D_L_TRACE, ("DoClose: pid=%d,e=%d,a=%d,s=%d,r=%d\n",
266 sp->pid, err, force_abort, sp->localSocket, sp->removing));
267 sp->userFlags |= eClosed; /* Set flag */
268 sp->state = sClosed;
269 sp->openState = O_STATE_NOTHING;
270
271 /*
272 * Clean up any timer elements
273 */
274 RemoveTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer);
275 RemoveTimerElem(&adspGlobal.fastTimers, &sp->FlushTimer);
276 RemoveTimerElem(&adspGlobal.fastTimers, &sp->RetryTimer);
277 RemoveTimerElem(&adspGlobal.fastTimers, &sp->AttnTimer);
278 RemoveTimerElem(&adspGlobal.fastTimers, &sp->ResetTimer);
279
280 aborted_count = AbortIO(sp, err);
281 np = sp->opb; /* Get list of close/removes to complete */
282 sp->opb = 0; /* set this list null */
283
284 while (pb = np) { /* Handle all of the close/remove param blks */
285 np = (struct adspcmd *)pb->qLink; /* Get next guy (if any) */
286 pb->qLink = 0;
287 pb->ioResult = err;
288 completepb(sp, pb);
289 }
290 if (sp->removing && (force_abort >= 0)) { /* Abort outstanding receives */
291 aborted_count += CompleteQueue(&sp->rpb, err);
292
293 if (sp->deferred_mb) {
294 gbuf_freel(sp->deferred_mb);
295 sp->deferred_mb = 0;
296 }
297 if (sp->attn_mb) {
298 gbuf_freem(sp->attn_mb);
299 sp->attn_mb = 0;
300 }
301 if (sp->rbuf_mb) { /* clear the rcv queue */
302 gbuf_freem(sp->rbuf_mb);
303 sp->rbuf_mb = 0;
304 }
305 if (sp->crbuf_mb) {
306 gbuf_freem(sp->crbuf_mb);
307 sp->crbuf_mb = 0;
308 }
309 sp->rData = 0;
310
311 /* if our connection has been timed out */
312 /* and the user wasn't notified of the TearDown */
313 /* because of pending requests on this socket */
314 /* then fake a read completion to force the notification */
315
316 if (force_abort && aborted_count == 0) {
317 if (mp = gbuf_alloc(sizeof(struct adspcmd), PRI_HI)) {
318 pb = (struct adspcmd *)gbuf_rptr(mp);
319 gbuf_wset(mp,sizeof(struct adspcmd));
320
321 bzero((caddr_t) pb, sizeof(struct adspcmd));
322 pb->mp = mp;
323 pb->csCode = dspRead;
324 pb->ioResult = errAborted;
325 completepb(sp, pb); /* send fake read completion */
326 }
327 }
328 sp->removing = 0;
329 RemoveCCB(sp, 0); /* Will call completion routine */
330 }
331 sp->userFlags &= ~eClosed;
332}
333
334
335/*
336 * dspClose
337 *
338 * Also called for dspRemove and dspCLRemove.
339 * Must handle case of multiple close calls being issued (without
340 * abort bit set) Can only allow one pending remove though.
341 *
342 * INPUTS:
343 * --> ccbRefNum refnum of connection end
344 * --> abort abort the connection
345 *
346 * OUTPUTS:
347 * none
348 *
349 * ERRORS:
350 * errRefNum Bad connection Refnum
351 */
352int adspClose(sp, pb) /* (DSPPBPtr pb) */
353 register CCBPtr sp;
354 register struct adspcmd *pb;
355{
1c79356b
A
356 register gbuf_t *mp;
357
358 /* Must execute nearly all of this with ints off because user could
359 * be issuing a second dspRemove while the first is pending. Until
360 * we can detect this, we must not allow interrupts.
361 * Also, we can't handle the case where a close was issued earlier,
362 * and now this is the remove. If the write completion for the
363 * close advice packet occurs in the middle of this, we might
364 * foul up.
365 */
366
367 if (sp == 0) {
368 pb->ioResult = errRefNum;
369 return EINVAL;
370 }
371
372 /*
373 * Handle dspCLRemove
374 */
375 if (pb->csCode == (short)dspCLRemove) { /* Remove connection listener */
376 if (sp->state != (short)sListening) { /* But it's not a listener! */
377 pb->ioResult = errState;
378 return EINVAL;
379 }
380 CompleteQueue(&sp->opb, errAborted); /* Complete all dspListens */
381 RemoveCCB(sp, pb); /* Will call completion routine */
382 return 0;
383 }
384
385
386 /*
387 * Either dspClose or dspRemove
388 */
389
390 if (sp->removing) { /* Don't allow dspRemove or dspClose */
391 /* after one dspRemove has been issued. */
392 pb->ioResult = errState;
393 return EINVAL;
394 }
395
396
397 /*
398 * The previous Macintosh ADSP allowed you to call close on a
399 * connection that was in the process of opening or passively
400 * waiting for an open request. It is also legal to close a
401 * connection that is already closed. No error will be generated.
402 *
403 * It is also legal to issue a second close call while the first
404 * is still pending.
405 */
406 if (pb->csCode == (short)dspClose) {
1c79356b
A
407 if ((sp->state == (short)sPassive) || (sp->state == (short)sOpening)) {
408 sp->state = sClosed;
1c79356b
A
409 DoClose(sp, errAborted, 0);
410 pb->ioResult = 0;
411 adspioc_ack(0, pb->ioc, pb->gref);
412 return 0;
413 }
414
415 if (sp->state == (word)sClosed) { /* Ok to close a closed connection */
1c79356b
A
416 pb->ioResult = 0;
417 adspioc_ack(0, pb->ioc, pb->gref);
418 return 0;
419 }
420 if ((sp->state != (word)sOpen) && (sp->state != (word)sClosing)) {
1c79356b
A
421 pb->ioResult = errState;
422 return EINVAL;
423 }
424
425 sp->state = sClosing; /* No matter what, we're closing */
1c79356b
A
426 } /* dspClose */
427
428 else { /* dspRemove */
1c79356b
A
429 sp->removing = 1; /* Prevent allowing another dspClose. */
430 /* Tells completion routine of close */
431 /* packet to remove us. */
432
433 if (sp->state == sPassive || sp->state == sClosed ||
434 sp->state == sOpening) {
435 sp->state = sClosed;
1c79356b
A
436 DoClose(sp, errAborted, 0); /* Will remove CCB! */
437 return 0;
89b3af67 438 } else /* sClosing & sOpen */
1c79356b 439 sp->state = sClosing;
1c79356b
A
440
441 } /* dspRemove */
442
443 if (pb->u.closeParams.abort || CheckOkToClose(sp)) /* going to close */
444 {
445 AbortIO(sp, errAborted);
446 sp->sendCtl = B_CTL_CLOSE; /* Send close advice */
447 }
448
449 pb->ioResult = 1;
450 if ( (mp = gbuf_copym(pb->mp)) ) { /* duplicate user request */
451 adspioc_ack(0, pb->ioc, pb->gref); /* release user */
452 pb = (struct adspcmd *)gbuf_rptr(mp); /* get new parameter block */
453 pb->ioc = 0;
454 pb->mp = mp;
1c79356b 455 qAddToEnd(&sp->opb, pb); /* and save it */
1c79356b
A
456 } else {
457 pb->ioResult = 0;
458 adspioc_ack(0, pb->ioc, pb->gref); /* release user, and keep no copy
459 * for kernel bookkeeping, yetch!
460 */
461 }
462 CheckSend(sp);
463
464 return 0;
465}
466
467static void qRemove(qptr, elem)
468 register CCBPtr qptr;
469 register CCBPtr elem;
470{
1c79356b 471
1c79356b
A
472 while(qptr->ccbLink) {
473 if ((DSPPBPtr)(qptr->ccbLink) == (DSPPBPtr)elem) {
474 qptr->ccbLink = elem->ccbLink;
475 elem->ccbLink = 0;
1c79356b
A
476 return;
477 }
478 qptr = qptr->ccbLink;
479 }
1c79356b
A
480}
481
482int RxClose(sp)
483 register CCBPtr sp;
484{
485 register gbuf_t *mp;
486 register struct adspcmd *pb;
1c79356b 487
89b3af67 488 if ((sp->state == sClosing) || (sp->state == sClosed))
1c79356b 489 return 0;
89b3af67 490
1c79356b 491 sp->state = sClosed;
1c79356b
A
492 CheckReadQueue(sp); /* try to deliver all remaining data */
493
494 if ( (mp = gbuf_alloc(sizeof(struct adspcmd), PRI_HI)) ) {
495 pb = (struct adspcmd *)gbuf_rptr(mp);
496 gbuf_wset(mp,sizeof(struct adspcmd));
497 pb->ioc = 0;
498 pb->mp = mp;
499
500 pb->csCode = dspClose;
501 pb->ioResult = 0;
502 completepb(sp, pb); /* send close completion */
503 }
504
505if ((sp->userFlags & eClosed) == 0)
506 DoClose(sp, errAborted, -1); /* abort send requests and timers */
507
1c79356b
A
508 return 0;
509}