summaryrefslogtreecommitdiffstats
path: root/private/ntos/miniport/always/rqm.c
blob: e68ad9b6299e123d99cd2967d8a95b542f0c4c27 (plain) (blame)
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
/* Copyright (C) 1991, 1992 by Always Technology Corporation.
   This module contains information proprietary to
   Always Technology Corporation, and is be treated as confidential.
*/

#include "environ.h"
#include "rqm.h"
#include "api.h"
#include "apiscsi.h"
#include "debug.h"

/*
  Add the device index for a request to the tail of the FIFO.  AddReqFIFO
  is called only when a request is moved to the head of the queue, i.e.,
  the first command for a device or when the current request for a device
  completes, and the second command moves to the head.
*/

void REGPARMS
AddReqFIFO (ADAPTER_PTR HA, DEVICE_PTR DevP)
{
  DevP->FIFO_Next = NILL;

  TRACE(3,("AddReqFIFO(): Adding device %02x to FIFO\n", DevP));

  if (HA->FIFO_Head == DevP) {

    PanicMsg("AddReqFIFO(): Circular device chain\n");

  }

  critical(HA);
  if (HA->FIFO_Head == NILL)
    HA->FIFO_Head = DevP;
  else
    HA->FIFO_Tail->FIFO_Next = DevP;

  HA->FIFO_Tail = DevP;
  uncritical(HA);

}



/*
  Get the next request in the request FIFO.  The FIFO is not incremented
  until AcceptReq is called.  See the comment below for more details.
*/

IO_REQ_PTR REGPARMS
GetReqFIFO (ADAPTER_PTR HA)
{
  IO_REQ_PTR Req=NILL;

  // Because an abort request may empty the pending queue, we will scan the
  // device FIFO until we find a device with an non-empty request list.  If we
  // don't find one, return NULL, which says there are not more pending requests.
  while (HA->FIFO_Head != NILL) {

    Req = HA->FIFO_Head->PendingReqList;
    if (Req == NILL)
      HA->FIFO_Head = HA->FIFO_Head->FIFO_Next;
    else
      break;

  }

  return Req;
}



/* Start the next request for a given adapter: */
void
StartNext (ADAPTER_PTR HA, int StartLevel)
{
  IO_REQ_PTR Req;

  if (!(HA->State.Busy) && (Req = GetReqFIFO(HA)) != NILL) {

    critical(HA);
    HA->Ext->MI_Count = HA->Ext->MI_Needed = HA->Ext->MO_Count = 0;    /* Clear msg buffers */
    HA->CurrReq = Req;
    HA->CurrDev = (DEVICE_PTR)ReqDevP(Req);
    HA->Service(HA_INITIATE, HA, (long)StartLevel);
    uncritical(HA);
    TRACE(3,("StartNext(): Starting request: %lx\n", (long)Req));

  } else {

    TRACE(3,("StartNext(): Empty queue\n"));

  }
}


/*
  If the adapter has taken a request off the queue, and has attempted
  to initiate it, it will set the "ReqStarting" flag.  When the controller
  acknowledges the start of the command, then the following procedure is
  called to remove it from the queue.  If the controller does not accept the
  command (usually caused if a reselection occuring before the new
  target is selected), the flag will be cleared, and the request not removed
  from the queue.  So the next time GetReqFIFO is called, the same request
  will be returned.
*/

void
AcceptReq (ADAPTER_PTR HA)
{
  DEVICE_PTR DevP;
  IO_REQ_PTR Req;

  if ((HA->ReqStarting !=0) && (--(HA->ReqStarting) == 0)) {


    critical(HA);

    DevP = HA->FIFO_Head;

    DevP->CurrDepth++;				// Bump the numberof active requests
    HA->FIFO_Head = DevP->FIFO_Next;		// Advance the next device w/ pending requests

    Req = DevP->PendingReqList;

    DevP->PendingReqList = ReqNext(Req);
    ReqNext(Req) = DevP->AcceptedList;
    DevP->AcceptedList = Req;

    ReqState(Req).Connected = 1;

    uncritical(HA);

#if defined(KEEP_STATS)
    HA->ReqsAccepted++;
#endif

    TRACE(5,("AcceptReq(): Request (@%lx) accepted for device %x\n", Req, DevP));

  }
}



/*
  Find a request block by device and queue tag;  returns a pointer to
  the request, or NULL if it not found.
*/
IO_REQ_PTR
Find_Req (DEVICE_PTR DevP, const unsigned LUN, const unsigned QID)
{
  IO_REQ_PTR Req = DevP->AcceptedList;

  while ((Req != NILL) && ((ReqTargetLUN(Req) != LUN)
#if defined(ReqQID)
   || (ReqQID(Req) != QID)
#endif
       )) {

    Req = ReqNext(Req);

  }
  return Req;
}



int
Reselect_Req (ADAPTER_PTR HA, IO_REQ_PTR Req)
{

  HA->CurrReq = Req;
  HA->CurrDev = (DEVICE_PTR)ReqDevP(Req);
  HA->State.Busy = 1;
  HA->State.Reselecting = 0;
  HA->Ext->MI_Count = HA->Ext->MI_Needed = HA->Ext->MO_Count = 0; /* Clear message buffers */

  /* Reselection performs an implied restore pointers: */
  HA->ReqCurrentIndex = ReqSavedIndex(HA->CurrReq);
  TRACE(3, ("Reselect_Req(): Implied restore data ptr, ReqSavedIndex() = %d\n",
      ReqSavedIndex(HA->CurrReq)));
  // CurrentCounts == 0 will cause a GetXferSegment from the above offset
  HA->ReqCurrentCount = 0;

  TRACE(4,("Reselect_Req(): reselect on TID %d, req @ %lx\n", ReqTargetID(Req), Req));
  HA->Service(HA_LED, HA, 1);

  ReqState(Req).Connected = 1;
  return 0;

}



int
Reselect (ADAPTER_PTR HA, const unsigned TID, const unsigned LUN, const unsigned QID)
{
  DEVICE_PTR DevP;
  IO_REQ_PTR Req;

  HA->CurrReq = NILL;
  DevP = APIFindDev(HA, TID, LUN);
  if (DevP == NILL)
    return 1;

  Req = Find_Req(DevP, LUN, QID);
  if (Req == NILL)
    return 1;
  return Reselect_Req(HA, Req);

}


/*
  This function handles completion of an arbitrary request by:

    1) De-queuing the request

    2) Handling of temporary device descriptor disposal

    3) Handling sync. negotiations which fail with a check condition status

    4) Notify the request call back (if ReqFlags(Req).Post is true)

    5) --->> Sets HA->CurrReq to NILL; <<---
       This is highlighted because the request passed in is not necessarly
       the one in HA->CurrReq. However, if an adapter is completing a
       request, then it's attention can't be on a differant one too.
*/

void
ReqDone (ADAPTER_PTR HA, IO_REQ_PTR Req)
{
  DEVICE_PTR DevP;


  if (Req == (IO_REQ_PTR)NILL) {

    TRACE(0, ("ReqDone(): Completing NULL request\n"));
    return;

  }

  /* Get the descriptor for the device which processed this request: */
  DevP = (DEVICE_PTR)ReqDevP(Req);
  if (DevP->PendingReqList == Req)
    AcceptReq(HA);

  DevP->CurrDepth--;				// Dec. number of active reqs.

  if (DevP->AcceptedList == Req)
    DevP->AcceptedList = ReqNext(Req);
  else {

    /* Search for the pointer to this request in the accepted list: */
    IO_REQ_PTR Req2 = DevP->AcceptedList;
    while (ReqNext(Req2) != Req && Req2 != NILL)
      Req2 = ReqNext(Req2);

    if (Req2 == NILL)
      PanicMsg("ReqDone(): Request not on list.  Catastrophic failure.\n");

    /*  Unlink by pointing this one's predicessor to this one's next. */
    ReqNext(Req2) = ReqNext(Req);

  }


  /*
    Since we just finished up one request, add the next (if any) to the FIFO
    of devices with pending requests.
    */
  if ((DevP->PendingReqList != NILL) && (DevP->CurrDepth < DevP->MaxDepth))
    AddReqFIFO(HA, DevP);

  /* If we get a check condition during sync. negotiaitons (Need_Sync still
     true, then the target rejected the synchronous request via by 1) not
     completing the negotiaition (no msg in phase); & 2) going directly
     to the status phase (by passing command phase).  Clean up the sync.
     control bits for the device, saying that sync. is not allowed, don't
     use it, and don't ask for it.
     */

  if ((ReqAPIStatus(Req) == S_TAR_CHECKCOND) && (HA->DevInfo[ReqTargetID(Req)].Flags.NeedSync)) {

    HA->DevInfo[ReqTargetID(Req)].Flags.UseSync = HA->DevInfo[ReqTargetID(Req)].Flags.AllowSync =
       HA->DevInfo[ReqTargetID(Req)].Flags.NeedSync = FALSE;
    TRACE(3,("ReqDone(): Sync. negotiation aborted by CHECK CONDITION\n"));

  }

  HA->CurrReq = NILL;

  /* If we are to notify someone of the command completion, do so now: */

  TRACE(4,("ReqDone(): Status = %04x\n", ReqAPIStatus(Req)));

#if defined(KEEP_STATS)
  HA->ReqsCompleted++;
#endif

  ReqState(Req).Connected = 0;
  APISetStatus(Req, ReqAPIStatus(Req), Terminal, HA->Supports.OnBoardQueuing ? NotSenseable : Senseable);

}



LOCAL IO_REQ_PTR * REGPARMS
ScanReqList (IO_REQ_PTR *List, IO_REQ_PTR Req)
{
  if (List == (IO_REQ_PTR *)NILL)
    return List;

  while (*List != (IO_REQ_PTR)NILL && *List != Req)
    List = (IO_REQ_PTR *)&(ReqNext(*List));
  return List;

}


BOOLEAN
AbortRequest (ADAPTER_PTR HA, DEVICE_PTR DevP, IO_REQ_PTR Req)
{
  IO_REQ_PTR *ReqScan = &(DevP->PendingReqList);
  BOOLEAN OnAccepted=FALSE;

  if (HA->CurrReq == Req) {

    TRACE(1, ("AbortRequest(): Request is active; not aborting\n"));
    return FALSE;

  }

  if (*ReqScan == Req) {

    TRACE(2, ("AbortRequest(): Request to abort found at PendingHead\n"));
    if ((DevP->PendingReqList = ReqNext(Req)) == NILL) {

      // We have removed the only request for this device, so now
      // remove the device from the chain of pending devices:
      if (HA->FIFO_Head == DevP) {

	HA->FIFO_Head = DevP->FIFO_Next;
	DevP->FIFO_Next = NILL;

      } else {

	DEVICE_PTR ScanDev=HA->FIFO_Head;
	while (ScanDev != NILL && ScanDev->FIFO_Next != DevP)
	  ScanDev = ScanDev->FIFO_Next;

	if (ScanDev != NILL)
	  ScanDev->FIFO_Next = DevP->FIFO_Next;

	if (HA->FIFO_Tail == DevP)
	  HA->FIFO_Tail = ScanDev;

      }
    }

  } else {

    if ((ReqScan = ScanReqList(ReqScan, Req)) == NILL) {

      TRACE(2, ("AbortRequest(): Scanning accepted list\n"));
      ReqScan =  ScanReqList(&(DevP->AcceptedList), Req);
      OnAccepted = TRUE;

    }

  }

  if (ReqScan == NILL) {

    TRACE(2, ("AbortRequest(): Request not found in pending list\n"));
    return FALSE;				//  but the abort didn't work directly

  }

  // At this point, ReqScan points to pointer to the request we want to abort
  *ReqScan = ReqNext(Req);			// Take Req out of chain

  // OK, so Req has been verified as being on the pending queue, and has
  // been removed.  Now we can complete the request, with an aborted status.

  APISetStatus(Req, S_REQ_ABORT, Terminal, NotSenseable);

  if (OnAccepted) {

    // This was an accpted request.  Decrement the pending count to allow
    // more requests to pass to the target.
    DevP->CurrDepth--;

  }

  return TRUE;					// Abort was successful

}


/*
  Queue up a request.  The queues are kept by Target ID.  If an entry already
  exists for a particular Target ID, then the new request is appended to the
  queue.  If the queue is empty, the request is added as the head, and the
  target ID is added to the FIFO of targets with requests pending.
*/

void
QueueReq (ADAPTER_PTR HA, IO_REQ_PTR Req, int AtHead)
{
  DEVICE_PTR DevP = (DEVICE_PTR)ReqDevP(Req);

  TRACE(4,("QueueReq(): Got request @%lx for TID/LUN %d/%d(device %lx)\n", Req, ReqTargetID(Req), ReqTargetLUN(Req), DevP));

  DmsPause(6, 1000);

  if (HA->State.OffLine) {

    APISetStatus(Req, S_AD_OFF, Terminal, NotSenseable);
    return;

  }

  ReqAPIStatus(Req) = S_REQ_ACCEPTED;
  APISetStatus(Req, S_REQ_ACCEPTED, NonTerminal, NotSenseable);

  if (ReqCDBLen(Req) == 10 || ReqCDBLen(Req) == 6 || ReqCDBLen(Req) == 12) {

  } else {

    TRACE(0,("QueueReq(): Request rejected; CDB Length = %d\n", ReqCDBLen(Req)));
    APISetStatus(Req, S_REQ_OPCODE, NonTerminal, NotSenseable);
    return;

  }

  ReqSavedIndex(Req) = 0;		// Index used for saved data pointers

  /* If we support linked commands, see if this one is linked; if it is not,
    force the link chain pointer to NILL.  If linked commands are not
    supported, then always force link field to zero
  */

  critical(HA);

  /* At head will only come from an auto-request sense; therefore, the first
     entry (if any) will NOT be in progress.  If AtHEad is requested at any
     other time, the top request on the queue must be checked to see if it
     in progress; and if so, the AtHead request must be made the second
     request on the queue.
  */

  if (AtHead) {

    TRACE(5, ("QueueReq(): Queing at head\n"));

    ReqNext(Req) = DevP->PendingReqList;
    DevP->PendingReqList = Req;
    if ((ReqNext(Req) == NILL) && (DevP->CurrDepth < DevP->MaxDepth))
        AddReqFIFO(HA, DevP);

  } else {

#if defined(RF_LINKED)
    if (!(ReqFlags(Req) & RF_LINKED))
      ReqNext(Req) = NILL;
#else
    ReqNext(Req) = NILL;
#endif

    if (DevP->PendingReqList == NILL) {

      DevP->PendingReqList = Req;
      if (DevP->CurrDepth < DevP->MaxDepth)
        AddReqFIFO(HA, DevP);

    } else {

      IO_REQ_PTR ReqScan = DevP->PendingReqList;
      while (ReqNext(ReqScan) != NILL)
	ReqScan = ReqNext(ReqScan);
      ReqNext(ReqScan) = Req;

    }

  }

#if defined(KEEP_STATS)
  HA->ReqsQueued++;
#endif

  TRACE(2,("QueueReq(): Request @ %0lx", Req));
  TRACE(2,(" CDB[0] = %02x, Data @%08lx, Count = %ld\n", ReqCDB(Req)[0], ReqDataPtr(Req), ReqDataCount(Req)));

  DmsPause(6, 2000);
  if (DevP->CurrDepth < DevP->MaxDepth) {

    TRACE(5,("About to tickle adapter\n"));
    HA->Service(HA_TICKLE, HA, (long)0);

  }

  uncritical(HA);

}



void
BlowAwayRequests (ADAPTER_PTR HA, int Which, APIStatus Status)
{

  unsigned ID, LUN;
  DEVICE_PTR DevP;
  IO_REQ_PTR Req;

  /* Walk the device tables, and abort accepted commands.  Then set the
  device states to require re-negotiating sync. xfers */

  TRACE(1, ("BlowAwayRequests(): Scaning for device IDs 0-%d on HA %x\n", HA->Max_TID, HA));
  for (ID = 0; ID <= HA->Max_TID; ID++) {

    HA->DevInfo[ID].Flags.UseSync = 0;
    HA->DevInfo[ID].Flags.NeedSync = HA->DevInfo[ID].Flags.AllowSync;

    for (LUN = 0; LUN <= 7; LUN++) {

      DevP = APIFindDev(HA, ID, LUN);
      if (DevP == NILL || DevP->Flags.Initialized == 0)
	continue;

      TRACE(2, ("BlowAwayRequests(): Got device descriptor for %d/%d\n", ID, LUN));
      if (Which == ACTIVE_REQUESTS || Which == ALL_REQUESTS) {

	while (DevP->AcceptedList != NILL) {

	  TRACE(4, ("BlowAwayRequests(): Completeing request %x\n", DevP->AcceptedList));
	  ReqAPIStatus(DevP->AcceptedList) = Status;
          if (HA->Supports.OnBoardQueuing)
            HA->Service(HA_ABORT_REQ, HA, (U32)DevP->AcceptedList);
          else
            ReqDone(HA, DevP->AcceptedList);

	}
      }							// if (Which ...

      if (Which == PENDING_REQUESTS || Which == ALL_REQUESTS) {

	for (Req=DevP->PendingReqList; Req != NILL; Req=DevP->PendingReqList) {

	  TRACE(4, ("BlowAwayRequests(): Completeing request %x\n", Req));
	  DevP->PendingReqList = ReqNext(Req);
	  APISetStatus(Req, Status, Terminal, NotSenseable);

	}						// for (Req ...
      }							// if (Which ...
    }							// for (LUN = ...
  }							// for (ID = ...
}



void
HAParmChange (ADAPTER_PTR HA)
{
  HA->Service(HA_PARM_CHANGE, HA, (U32)0);
}