ncr5380.c revision 1.7 1 /* $NetBSD: ncr5380.c,v 1.7 1995/09/05 07:02:21 leo Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34 #ifdef DBG_NOSTATIC
35 # define static
36 #endif
37 #ifdef DBG_SEL
38 # define DBG_SELPRINT(a,b) printf(a,b)
39 #else
40 # define DBG_SELPRINT(a,b)
41 #endif
42 #ifdef DBG_PIO
43 # define DBG_PIOPRINT(a,b,c) printf(a,b,c)
44 #else
45 # define DBG_PIOPRINT(a,b,c)
46 #endif
47 #ifdef DBG_INF
48 # define DBG_INFPRINT(a,b,c) a(b,c)
49 #else
50 # define DBG_INFPRINT(a,b,c)
51 #endif
52 #ifdef DBG_PID
53 static char *last_hit = NULL, *olast_hit = NULL;
54 # define PID(a) olast_hit = last_hit; last_hit = a;
55 #else
56 # define PID(a)
57 #endif
58
59 /*
60 * Bit mask of targets you want debugging to be shown
61 */
62 u_char dbg_target_mask = 0x7f;
63
64 /*
65 * Set bit for target when parity checking must be disabled.
66 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50%
67 * of all transfers while the data is correct!?
68 */
69 u_char ncr5380_no_parchk = 0xff;
70
71 /*
72 * This is the default sense-command we send.
73 */
74 static u_char sense_cmd[] = {
75 REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
76 };
77
78 /*
79 * True if the main co-routine is running
80 */
81 static volatile int main_running = 0;
82
83 /*
84 * Mask of targets selected
85 */
86 static u_char busy;
87
88 static void ncr5380_minphys(struct buf *bp);
89 static int ncr5380_scsi_cmd(struct scsi_xfer *xs);
90 static int ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
91
92 struct scsi_adapter ncr5380_switch = {
93 ncr5380_scsi_cmd, /* scsi_cmd() */
94 ncr5380_minphys, /* scsi_minphys() */
95 0, /* open_target_lu() */
96 0 /* close_target_lu() */
97 };
98
99 struct scsi_device ncr5380_dev = {
100 NULL, /* use default error handler */
101 NULL, /* do not have a start functio */
102 NULL, /* have no async handler */
103 NULL /* Use default done routine */
104 };
105
106
107 static SC_REQ req_queue[NREQ];
108 static SC_REQ *free_head = NULL; /* Free request structures */
109
110
111 /*
112 * Inline functions:
113 */
114
115 /*
116 * Determine the size of a SCSI command.
117 */
118 extern __inline__ int command_size(opcode)
119 u_char opcode;
120 {
121 switch ((opcode >> 4) & 0xf) {
122 case 0:
123 case 1:
124 return (6);
125 case 2:
126 case 3:
127 return (10);
128 }
129 return (12);
130 }
131
132
133 /*
134 * Wait for request-line to become active. When it doesn't return 0.
135 * Otherwise return != 0.
136 * The timeouts in the 'wait_req_*' functions are arbitrary and rather
137 * large. In 99% of the invocations nearly no timeout is needed but in
138 * some cases (especially when using my tapedrive, a Tandberg 3600) the
139 * device is busy internally and the first SCSI-phase will be delayed.
140 */
141 extern __inline__ int wait_req_true(void)
142 {
143 int timeout = 25000;
144
145 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
146 delay(1);
147 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
148 }
149
150 /*
151 * Wait for request-line to become inactive. When it doesn't return 0.
152 * Otherwise return != 0.
153 */
154 extern __inline__ int wait_req_false(void)
155 {
156 int timeout = 25000;
157
158 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
159 delay(1);
160 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
161 }
162
163 extern __inline__ void finish_req(SC_REQ *reqp)
164 {
165 int sps;
166 struct scsi_xfer *xs = reqp->xs;
167
168 #ifdef REAL_DMA
169 /*
170 * If we bounced, free the bounce buffer
171 */
172 if (reqp->dr_flag & DRIVER_BOUNCING)
173 free_bounceb(reqp->bounceb);
174 #endif /* REAL_DMA */
175 #ifdef DBG_REQ
176 if (dbg_target_mask & (1 << reqp->targ_id))
177 show_request(reqp, "DONE");
178 #endif
179 #ifdef DBG_ERR_RET
180 if (reqp->xs->error != 0)
181 show_request(reqp, "ERR_RET");
182 #endif
183
184 /*
185 * Return request to free-q
186 */
187 sps = splbio();
188 reqp->next = free_head;
189 free_head = reqp;
190 splx(sps);
191
192 xs->flags |= ITSDONE;
193 scsi_done(xs);
194 }
195
196 /*
197 * Auto config stuff....
198 */
199 int ncr_cprint __P((void *auxp, char *));
200 void ncr_attach __P((struct device *, struct device *, void *));
201 int ncr_match __P((struct device *, struct cfdata *, void *));
202
203 /*
204 * Tricks to make driver-name configurable
205 */
206 #define CFNAME(n) __CONCAT(n,cd)
207 #define CFSTRING(n) __STRING(n)
208
209 struct cfdriver CFNAME(DRNAME) = {
210 NULL, CFSTRING(DRNAME), (cfmatch_t)ncr_match, ncr_attach,
211 DV_DULL, sizeof(struct ncr_softc), NULL, 0 };
212
213 int
214 ncr_match(pdp, cdp, auxp)
215 struct device *pdp;
216 struct cfdata *cdp;
217 void *auxp;
218 {
219 return (machine_match(pdp, cdp, auxp, &CFNAME(DRNAME)));
220 }
221
222 void
223 ncr_attach(pdp, dp, auxp)
224 struct device *pdp, *dp;
225 void *auxp;
226 {
227 struct ncr_softc *sc;
228 int i;
229
230 sc = (struct ncr_softc *)dp;
231
232 sc->sc_link.adapter_softc = sc;
233 sc->sc_link.adapter_target = 7;
234 sc->sc_link.adapter = &ncr5380_switch;
235 sc->sc_link.device = &ncr5380_dev;
236 sc->sc_link.openings = NREQ - 1;
237
238 /*
239 * Initialize machine-type specific things...
240 */
241 scsi_mach_init(sc);
242 printf("\n");
243
244 /*
245 * Initialize request queue freelist.
246 */
247 for (i = 0; i < NREQ; i++) {
248 req_queue[i].next = free_head;
249 free_head = &req_queue[i];
250 }
251
252 /*
253 * Initialize the host adapter
254 */
255 scsi_idisable();
256 ENABLE_NCR5380(sc);
257 SET_5380_REG(NCR5380_ICOM, 0);
258 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
259 SET_5380_REG(NCR5380_TCOM, 0);
260 SET_5380_REG(NCR5380_IDSTAT, 0);
261 scsi_ienable();
262
263 /*
264 * attach all scsi units on us
265 */
266 config_found(dp, &sc->sc_link, ncr_cprint);
267 }
268
269 /*
270 * print diag if name is NULL else just extra
271 */
272 int
273 ncr_cprint(auxp, name)
274 void *auxp;
275 char *name;
276 {
277 if (name == NULL)
278 return (UNCONF);
279 return (QUIET);
280 }
281 /*
282 * End of auto config stuff....
283 */
284
285 /*
286 * Carry out a request from the high level driver.
287 */
288 static int
289 ncr5380_scsi_cmd(struct scsi_xfer *xs)
290 {
291 int sps;
292 SC_REQ *reqp;
293 int flags = xs->flags;
294
295 /*
296 * We do not queue RESET commands
297 */
298 if (flags & SCSI_RESET) {
299 scsi_reset(xs->sc_link->adapter_softc);
300 return (COMPLETE);
301 }
302
303 /*
304 * Get a request block
305 */
306 sps = splbio();
307 if ((reqp = free_head) == 0) {
308 splx(sps);
309 return (TRY_AGAIN_LATER);
310 }
311 free_head = reqp->next;
312 reqp->next = NULL;
313 splx(sps);
314
315 /*
316 * Initialize our private fields
317 */
318 reqp->dr_flag = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
319 reqp->phase = NR_PHASE;
320 reqp->msgout = MSG_NOOP;
321 reqp->status = SCSGOOD;
322 reqp->link = NULL;
323 reqp->xs = xs;
324 reqp->targ_id = xs->sc_link->target;
325 reqp->targ_lun = xs->sc_link->lun;
326 reqp->xdata_ptr = (u_char*)xs->data;
327 reqp->xdata_len = xs->datalen;
328 memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
329 reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
330
331 /*
332 * Sanity check on flags...
333 */
334 if (flags & ITSDONE) {
335 ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
336 xs->flags &= ~ITSDONE;
337 }
338 if (!(flags & INUSE)) {
339 ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
340 xs->flags |= ~INUSE;
341 }
342
343 #ifdef REAL_DMA
344 /*
345 * Check if DMA can be used on this request
346 */
347 if (scsi_dmaok(reqp))
348 reqp->dr_flag |= DRIVER_DMAOK;
349 #endif /* REAL_DMA */
350
351 /*
352 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
353 * commands are inserted at the head of the queue since any command
354 * will clear the existing contingent allegience condition and the sense
355 * data is only valid while the condition exists.
356 * When possible, link the command to a previous command to the same
357 * target. This is not very sensible when AUTO_SENSE is not defined!
358 * Interrupts are disabled while we are fiddling with the issue-queue.
359 */
360 sps = splbio();
361 if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
362 reqp->next = issue_q;
363 issue_q = reqp;
364 }
365 else {
366 SC_REQ *tmp, *link;
367
368 tmp = issue_q;
369 link = NULL;
370 do {
371 if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
372 link = tmp;
373 } while (tmp->next && (tmp = tmp->next));
374 tmp->next = reqp;
375 #ifdef AUTO_SENSE
376 if (link) {
377 link->link = reqp;
378 link->xcmd.bytes[link->xs->cmdlen-1] |= 1;
379 }
380 #endif
381 }
382 splx(sps);
383
384 #ifdef DBG_REQ
385 if (dbg_target_mask & (1 << reqp->targ_id))
386 show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
387 "HEAD":"TAIL");
388 #endif
389
390 run_main(xs->sc_link->adapter_softc);
391
392 if (xs->flags & SCSI_POLL)
393 return (COMPLETE); /* We're booting */
394 return (SUCCESSFULLY_QUEUED);
395 }
396
397 #define MIN_PHYS 65536 /*BARF!!!!*/
398 static void
399 ncr5380_minphys(struct buf *bp)
400 {
401 if (bp->b_bcount > MIN_PHYS)
402 bp->b_bcount = MIN_PHYS;
403 minphys(bp);
404 }
405 #undef MIN_PHYS
406
407 static int
408 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
409 {
410 u_char *b = (u_char *) xs->cmd;
411 int i = 0;
412
413 if (!(xs->flags & SCSI_RESET)) {
414 printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
415 xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
416 while (i < xs->cmdlen) {
417 if (i)
418 printf(",");
419 printf("%x",b[i++]);
420 }
421 printf("-\n");
422 }
423 else {
424 printf("(%d:%d:%d)-RESET-\n",
425 xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
426 }
427 }
428
429 /*
430 * The body of the driver.
431 */
432 static void
433 scsi_main(sc)
434 struct ncr_softc *sc;
435 {
436 SC_REQ *req, *prev;
437 int itype;
438 int sps;
439
440 /*
441 * While running in the driver SCSI-interrupts are disabled.
442 */
443 scsi_idisable();
444 ENABLE_NCR5380(sc);
445
446 PID("scsi_main1");
447 for (;;) {
448 sps = splbio();
449 if (!connected) {
450 /*
451 * Check if it is fair keep any exclusive access to DMA
452 * claimed. If not, stop queueing new jobs so the discon_q
453 * will be eventually drained and DMA can be given up.
454 */
455 if (!fair_to_keep_dma())
456 goto main_exit;
457
458 /*
459 * Search through the issue-queue for a command
460 * destined for a target that isn't busy.
461 */
462 prev = NULL;
463 for (req=issue_q; req != NULL; prev = req, req = req->next) {
464 if (!(busy & (1 << req->targ_id))) {
465 /*
466 * Found one, remove it from the issue queue
467 */
468 if (prev == NULL)
469 issue_q = req->next;
470 else prev->next = req->next;
471 req->next = NULL;
472 break;
473 }
474 }
475
476 /*
477 * When a request has just ended, we get here before an other
478 * device detects that the bus is free and that it can
479 * reconnect. The problem is that when this happens, we always
480 * baffle the device because our (initiator) id is higher. This
481 * can cause a sort of starvation on slow devices. So we check
482 * for a pending reselection here.
483 * Note that 'connected' will be non-null if the reselection
484 * succeeds.
485 */
486 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
487 == (SC_S_SEL|SC_S_IO)){
488 if (req != NULL) {
489 req->next = issue_q;
490 issue_q = req;
491 }
492 splx(sps);
493
494 reselect(sc);
495 scsi_clr_ipend();
496 goto connected;
497 }
498
499 /*
500 * The host is not connected and there is no request
501 * pending, exit.
502 */
503 if (req == NULL) {
504 PID("scsi_main2");
505 goto main_exit;
506 }
507
508 /*
509 * Re-enable interrupts before handling the request.
510 */
511 splx(sps);
512
513 #ifdef DBG_REQ
514 if (dbg_target_mask & (1 << req->targ_id))
515 show_request(req, "TARGET");
516 #endif
517 /*
518 * We found a request. Try to connect to the target. If the
519 * initiator fails arbitration, the command is put back in the
520 * issue queue.
521 */
522 if (scsi_select(req, 0)) {
523 sps = splbio();
524 req->next = issue_q;
525 issue_q = req;
526 splx(sps);
527 #ifdef DBG_REQ
528 if (dbg_target_mask & (1 << req->targ_id))
529 ncr_tprint(req, "Select failed\n");
530 #endif
531 }
532 }
533 else splx(sps);
534 connected:
535 if (connected) {
536 /*
537 * If the host is currently connected but a 'real-dma' transfer
538 * is in progress, the 'end-of-dma' interrupt restarts main.
539 * So quit.
540 */
541 sps = splbio();
542 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
543 PID("scsi_main3");
544 goto main_exit;
545 }
546 splx(sps);
547
548 /*
549 * Let the target guide us through the bus-phases
550 */
551 while (information_transfer() == -1)
552 ;
553 }
554 }
555 /* NEVER TO REACH HERE */
556 panic("ncr5380-SCSI: not designed to come here");
557
558 main_exit:
559 /*
560 * We enter here with interrupts disabled. We are about to exit main
561 * so interrupts should be re-enabled. Because interrupts are edge
562 * triggered, we could already have missed the interrupt. Therefore
563 * we check the IRQ-line here and re-enter when we really missed a
564 * valid interrupt.
565 */
566 PID("scsi_main4");
567 scsi_ienable();
568 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
569 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
570 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
571 scsi_idisable();
572 splx(sps);
573
574 if (itype == INTR_RESEL)
575 reselect(sc);
576 #ifdef REAL_DMA
577 else dma_ready();
578 #else
579 else panic("Got DMA interrupt without DMA");
580 #endif
581 scsi_clr_ipend();
582 goto connected;
583 }
584 }
585 reconsider_dma();
586
587 main_running = 0;
588 splx(sps);
589 PID("scsi_main5");
590 }
591
592 #ifdef REAL_DMA
593 /*
594 * The SCSI-DMA interrupt.
595 * This interrupt can only be triggered when running in non-polled DMA
596 * mode. When DMA is not active, it will be silently ignored, it is usually
597 * to late because the EOP interrupt of the controller happens just a tiny
598 * bit earlier. It might become usefull when scatter/gather is implemented,
599 * because in that case only part of the DATAIN/DATAOUT transfer is taken
600 * out of a single buffer.
601 */
602 static void
603 ncr_dma_intr(sc)
604 struct ncr_softc *sc;
605 {
606 SC_REQ *reqp;
607 int dma_done;
608
609 PID("ncr_dma_intr");
610 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
611 scsi_idisable();
612 if (!(dma_done = dma_ready())) {
613 transfer_dma(reqp, reqp->phase, 0);
614 return;
615 }
616 run_main(sc);
617 }
618 }
619 #endif /* REAL_DMA */
620
621 /*
622 * The SCSI-controller interrupt. This interrupt occurs on reselections and
623 * at the end of non-polled DMA-interrupts. It is assumed to be called from
624 * the machine-dependent hardware interrupt.
625 */
626 static void
627 ncr_ctrl_intr(sc)
628 struct ncr_softc *sc;
629 {
630 int itype;
631 int dma_done;
632
633 while (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
634 scsi_idisable();
635 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
636 if (itype == INTR_RESEL)
637 reselect(sc);
638 else {
639 #ifdef REAL_DMA
640 if (!(dma_done = dma_ready())) {
641 transfer_dma(connected, connected->phase, 0);
642 return;
643 }
644 #else
645 panic("Got DMA interrupt without DMA\n");
646 #endif
647 }
648 scsi_clr_ipend();
649 }
650 run_main(sc);
651 return;
652 }
653 PID("ncr_ctrl_intr1");
654 }
655
656 /*
657 * Initiate a connection path between the host and the target. The function
658 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
659 * is selected and an 'IDENTIFY' message is send.
660 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
661 * the target does not respond (to either selection or 'MESSAGE OUT') the
662 * 'done' function is executed.
663 * The result code given by the driver can be influenced by setting 'code'
664 * to a non-zero value. This is the case when 'select' is called by abort.
665 */
666 static int
667 scsi_select(reqp, code)
668 SC_REQ *reqp;
669 {
670 u_long timeout;
671 u_char tmp[1];
672 u_char phase;
673 u_long cnt;
674 int sps;
675 struct ncr_softc *sc;
676
677 sc = reqp->xs->sc_link->adapter_softc;
678 DBG_SELPRINT ("Starting arbitration\n", 0);
679 PID("scsi_select1");
680
681 sps = splbio();
682
683 /*
684 * Prevent a race condition here. If a reslection interrupt occurred
685 * between the decision to pick a new request and the call to select,
686 * we abort the selection.
687 * Interrupts are lowered when the 5380 is setup to arbitrate for the
688 * bus.
689 */
690 if (connected || (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
691 splx(sps);
692 PID("scsi_select2");
693 return (-1);
694 }
695
696 /*
697 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
698 * selection.
699 */
700 SET_5380_REG(NCR5380_TCOM, 0);
701 SET_5380_REG(NCR5380_ICOM, 0);
702
703 /*
704 * Arbitrate for the bus.
705 */
706 SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
707 SET_5380_REG(NCR5380_MODE, SC_ARBIT);
708
709 splx(sps);
710
711 cnt = 10000;
712 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
713 delay(1);
714
715 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
716 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
717 delay(1);
718 PID("scsi_select3");
719
720 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) {
721 /*
722 * Damn, we have a connected target that we don't know
723 * of. Some targets seem to respond to a selection
724 * AFTER the selection-timeout. Try to get the target
725 * into the Message-out phase so we can send an ABORT
726 * message. We try to avoid resetting the SCSI-bus!
727 */
728 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
729 u_long len = 1;
730 u_char phase = PH_MSGOUT;
731 u_char msg = MSG_ABORT;
732
733 transfer_pio(&phase, &msg, &len);
734 }
735 else if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
736 scsi_reset(sc);
737 }
738 PID("scsi_select4");
739 return (-1);
740 }
741
742 /* The arbitration delay is 2.2 usecs */
743 delay(3);
744
745 /*
746 * Check the result of the arbitration. If we failed, return -1.
747 */
748 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
749 /*
750 * The spec requires that we should read the data register to
751 * check for higher id's and check the SC_LA again.
752 */
753 tmp[0] = GET_5380_REG(NCR5380_DATA);
754 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
755 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
756 SET_5380_REG(NCR5380_ICOM, 0);
757 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
758 PID("scsi_select5");
759 return (-1);
760 }
761 }
762 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
763 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
764 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
765 SET_5380_REG(NCR5380_ICOM, 0);
766 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
767 PID("scsi_select6");
768 return (-1);
769 }
770 /* Bus settle delay + Bus clear delay = 1.2 usecs */
771 delay(2);
772 DBG_SELPRINT ("Arbitration complete\n", 0);
773
774 /*
775 * Now that we won the arbitration, start the selection.
776 */
777 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | (1 << reqp->targ_id));
778
779 /*
780 * Raise ATN while SEL is true before BSY goes false from arbitration,
781 * since this is the only way to guarantee that we'll get a MESSAGE OUT
782 * phase immediately after the selection.
783 */
784 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | SC_A_ATN | SC_ADTB);
785 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
786
787 /*
788 * Turn off reselection interrupts
789 */
790 SET_5380_REG(NCR5380_IDSTAT, 0);
791
792 /*
793 * Reset BSY. The delay following it, surpresses a glitch in the
794 * 5380 which causes us to see our own BSY signal instead of that of
795 * the target.
796 */
797 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN | SC_ADTB);
798 delay(1);
799
800 /*
801 * Wait for the target to react, the specs call for a timeout of
802 * 250 ms.
803 */
804 cnt = 25000 /* 250 */;
805 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
806 delay(10);
807
808 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
809 /*
810 * There is no reaction from the target, start the selection
811 * timeout procedure. We release the databus but keep SEL
812 * asserted. After that we wait a 'selection abort time' (200
813 * usecs) and 2 deskew delays (90 ns) and check BSY again.
814 * When BSY is asserted, we assume the selection succeeded,
815 * otherwise we release the bus.
816 */
817 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN);
818 delay(201);
819 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
820 SET_5380_REG(NCR5380_ICOM, 0);
821 reqp->xs->error = code ? code : XS_SELTIMEOUT;
822 DBG_SELPRINT ("Target %d not responding to sel\n",
823 reqp->targ_id);
824 finish_req(reqp);
825 PID("scsi_select7");
826 return (0);
827 }
828 }
829 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
830
831 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
832
833 /*
834 * The SCSI-interrupts are disabled while a request is being handled.
835 */
836 scsi_idisable();
837
838 /*
839 * Since we followed the SCSI-spec and raised ATN while SEL was true
840 * but before BSY was false during the selection, a 'MESSAGE OUT'
841 * phase should follow. Here we send an 'IDENTIFY' message.
842 * Allow disconnect only when interrups are allowed.
843 */
844 tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
845 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
846 cnt = 1;
847 phase = PH_MSGOUT;
848 if (transfer_pio(&phase, tmp, &cnt) || cnt) {
849 DBG_SELPRINT ("Target %d: failed to send identify\n",
850 reqp->targ_id);
851 /*
852 * Try to disconnect from the target. We cannot leave it just
853 * hanging here.
854 */
855 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
856 u_long len = 1;
857 u_char phase = PH_MSGOUT;
858 u_char msg = MSG_ABORT;
859
860 transfer_pio(&phase, &msg, &len);
861 }
862 else scsi_reset(sc);
863
864 SET_5380_REG(NCR5380_ICOM, 0);
865 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
866 finish_req(reqp);
867 PID("scsi_select8");
868 return (0);
869 }
870 reqp->phase = PH_MSGOUT;
871
872 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
873 /*
874 * Command is connected, start timer ticking.
875 */
876 ccb_p->xtimeout = ccb_p->timeout + Lbolt;
877 #endif
878
879 connected = reqp;
880 busy |= 1 << reqp->targ_id;
881 PID("scsi_select9");
882 return (0);
883 }
884
885 /*
886 * Return codes:
887 * -1: quit main, trigger on interrupt
888 * 0: keep on running main.
889 */
890 static int
891 information_transfer()
892 {
893 SC_REQ *reqp = connected;
894 u_char tmp, phase;
895 u_long len;
896
897 PID("info_transf1");
898 /*
899 * Clear pending interrupts from 5380-chip.
900 */
901 scsi_clr_ipend();
902
903 /*
904 * We only have a valid SCSI-phase when REQ is asserted. Something
905 * is deadly wrong when BSY has dropped.
906 */
907 tmp = GET_5380_REG(NCR5380_IDSTAT);
908
909 if (!(tmp & SC_S_BSY)) {
910 busy &= ~(1 << reqp->targ_id);
911 connected = NULL;
912 reqp->xs->error = XS_BUSY;
913 finish_req(reqp);
914 PID("info_transf2");
915 return (0);
916 }
917
918 if (tmp & SC_S_REQ) {
919 phase = (tmp >> 2) & 7;
920 if (phase != reqp->phase) {
921 reqp->phase = phase;
922 DBG_INFPRINT(show_phase, reqp, phase);
923 }
924 }
925 else return (-1);
926
927 switch (phase) {
928 case PH_DATAOUT:
929
930 #ifdef DBG_NOWRITE
931 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
932 reqp->msgout = MSG_ABORT;
933 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
934 return (-1);
935 #endif /* DBG_NOWRITE */
936 /*
937 * If this is the first write using DMA, fill
938 * the bounce buffer.
939 */
940 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
941 if (reqp->dr_flag & DRIVER_BOUNCING)
942 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
943 }
944
945 case PH_DATAIN:
946 #ifdef REAL_DMA
947 if (reqp->dr_flag & DRIVER_DMAOK) {
948 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
949 transfer_dma(reqp, phase, poll);
950 if (!poll)
951 return (0);
952 }
953 else
954 #endif
955 {
956 PID("info_transf3");
957 len = reqp->xdata_len;
958 #ifdef USE_PDMA
959 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
960 return (0);
961 #else
962 transfer_pio(&phase, reqp->xdata_ptr, &len);
963 #endif
964 reqp->xdata_ptr += reqp->xdata_len - len;
965 reqp->xdata_len = len;
966 }
967 return (-1);
968 case PH_MSGIN:
969 /*
970 * We only expect single byte messages here.
971 */
972 len = 1;
973 transfer_pio(&phase, &tmp, &len);
974 reqp->message = tmp;
975 return (handle_message(reqp, tmp));
976 case PH_MSGOUT:
977 len = 1;
978 transfer_pio(&phase, &reqp->msgout, &len);
979 if (reqp->msgout == MSG_ABORT) {
980 busy &= ~(1 << reqp->targ_id);
981 connected = NULL;
982 reqp->xs->error = XS_DRIVER_STUFFUP;
983 finish_req(reqp);
984 PID("info_transf4");
985 return (0);
986 }
987 reqp->msgout = MSG_NOOP;
988 return (-1);
989 case PH_CMD :
990 len = command_size(reqp->xcmd.opcode);
991 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len);
992 PID("info_transf5");
993 return (-1);
994 case PH_STATUS:
995 len = 1;
996 transfer_pio(&phase, &tmp, &len);
997 reqp->status = tmp;
998 PID("info_transf6");
999 return (-1);
1000 default :
1001 ncr_tprint(reqp, "Unknown phase\n");
1002 }
1003 PID("info_transf7");
1004 return (-1);
1005 }
1006
1007 /*
1008 * Handle the message 'msg' send to us by the target.
1009 * Return values:
1010 * 0 : The current command has completed.
1011 * -1 : Get on to the next phase.
1012 */
1013 static int
1014 handle_message(reqp, msg)
1015 SC_REQ *reqp;
1016 u_int msg;
1017 {
1018 int sps;
1019
1020 PID("hmessage1");
1021 switch (msg) {
1022 /*
1023 * Linking lets us reduce the time required to get
1024 * the next command to the device, skipping the arbitration
1025 * and selection time. In the current implementation,
1026 * we merely have to start the next command pointed
1027 * to by 'next_link'.
1028 */
1029 case MSG_LINK_CMD_COMPLETE:
1030 case MSG_LINK_CMD_COMPLETEF:
1031 if (reqp->link == NULL) {
1032 ncr_tprint(reqp, "No link for linked command");
1033 reqp->msgout = MSG_ABORT;
1034 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1035 PID("hmessage2");
1036 return (-1);
1037 }
1038 reqp->xs->error = 0;
1039
1040 #ifdef AUTO_SENSE
1041 if (check_autosense(reqp, 0) == -1)
1042 return (-1);
1043 #endif /* AUTO_SENSE */
1044
1045 #ifdef DBG_REQ
1046 if (dbg_target_mask & (1 << reqp->targ_id))
1047 show_request(reqp->link, "LINK");
1048 #endif
1049 connected = reqp->link;
1050 finish_req(reqp);
1051 PID("hmessage3");
1052 return (-1);
1053 case MSG_ABORT:
1054 case MSG_CMDCOMPLETE:
1055 connected = NULL;
1056 busy &= ~(1 << reqp->targ_id);
1057 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1058 reqp->xs->resid = reqp->xdata_len;
1059 reqp->xs->error = 0;
1060 }
1061
1062 #ifdef AUTO_SENSE
1063 if (check_autosense(reqp, 0) == -1) {
1064 PID("hmessage4");
1065 return (0);
1066 }
1067 #endif /* AUTO_SENSE */
1068
1069 finish_req(reqp);
1070 PID("hmessage5");
1071 return (0);
1072 case MSG_MESSAGE_REJECT:
1073 PID("hmessage6");
1074 return (-1);
1075 case MSG_DISCONNECT:
1076 #ifdef DBG_REQ
1077 if (dbg_target_mask & (1 << reqp->targ_id))
1078 show_request(reqp, "DISCON");
1079 #endif
1080 sps = splbio();
1081 connected = NULL;
1082 reqp->next = discon_q;
1083 discon_q = reqp;
1084 splx(sps);
1085 PID("hmessage7");
1086 return (0);
1087 case MSG_SAVEDATAPOINTER:
1088 case MSG_RESTOREPOINTERS:
1089 /*
1090 * We save pointers implicitely at disconnect.
1091 * So we can ignore these messages.
1092 */
1093 PID("hmessage8");
1094 return (-1);
1095 default:
1096 ncr_tprint(reqp, "Unkown message %x\n", msg);
1097 return (-1);
1098 }
1099 PID("hmessage9");
1100 return (-1);
1101 }
1102
1103 /*
1104 * Handle reselection. If a valid reconnection occurs, connected
1105 * points at the reconnected command. The command is removed from the
1106 * disconnected queue.
1107 */
1108 static void
1109 reselect(sc)
1110 struct ncr_softc *sc;
1111 {
1112 u_char phase;
1113 u_long len;
1114 u_char msg;
1115 u_char target_mask;
1116 int abort = 0;
1117 SC_REQ *tmp, *prev;
1118
1119 PID("reselect1");
1120 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1121
1122 /*
1123 * At this point, we have detected that our SCSI-id is on the bus,
1124 * SEL is true and BSY was false for at least one bus settle
1125 * delay (400 ns.).
1126 * We must assert BSY ourselves, until the target drops the SEL signal.
1127 * This should happen within 2 deskew delays (2 * 45ns.) We wait too
1128 * long for this, but it's better to wait for slow targets than to
1129 * reset the bus too early...
1130 */
1131 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1132 len = 2;
1133 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1134 delay(1);
1135 len--;
1136 }
1137 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1138 /* Damn SEL isn't dropping */
1139 scsi_reset(sc);
1140 return;
1141 }
1142
1143 SET_5380_REG(NCR5380_ICOM, 0);
1144
1145 /*
1146 * Get the expected identify message.
1147 */
1148 phase = PH_MSGIN;
1149 len = 1;
1150 transfer_pio(&phase, &msg, &len);
1151 if (len || !MSG_ISIDENTIFY(msg)) {
1152 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1153 abort = 1;
1154 }
1155 else {
1156 /*
1157 * Find the command reconnecting
1158 */
1159 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1160 if (target_mask == (1 << tmp->targ_id)) {
1161 if (prev)
1162 prev->next = tmp->next;
1163 else discon_q = tmp->next;
1164 tmp->next = NULL;
1165 break;
1166 }
1167 }
1168 if (tmp == NULL) {
1169 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1170 target_mask);
1171 abort = 1;
1172 }
1173 }
1174 if (abort) {
1175 msg = MSG_ABORT;
1176 len = 1;
1177 phase = PH_MSGOUT;
1178
1179 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1180 transfer_pio(&phase, &msg, &len);
1181 }
1182 else {
1183 connected = tmp;
1184 #ifdef DBG_REQ
1185 if (dbg_target_mask & (1 << tmp->targ_id))
1186 show_request(tmp, "RECON");
1187 #endif
1188 }
1189 PID("reselect2");
1190 }
1191
1192 /*
1193 * Transfer data in a given phase using programmed I/O.
1194 * Returns -1 when a different phase is entered without transferring the
1195 * maximum number of bytes, 0 if all bytes or exit is in the same
1196 * phase.
1197 */
1198 static int
1199 transfer_pio(phase, data, len)
1200 u_char *phase;
1201 u_char *data;
1202 u_long *len;
1203 {
1204 u_int cnt = *len;
1205 u_char ph = *phase;
1206 u_char tmp;
1207
1208 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1209 PID("tpio1");
1210 SET_5380_REG(NCR5380_TCOM, ph);
1211 do {
1212 if (!wait_req_true()) {
1213 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1214 break;
1215 }
1216 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1217 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1218 break;
1219 }
1220 if (PH_IN(ph)) {
1221 *data++ = GET_5380_REG(NCR5380_DATA);
1222 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1223 }
1224 else {
1225 SET_5380_REG(NCR5380_DATA, *data++);
1226
1227 /*
1228 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1229 * the initiator should drop ATN on the last byte of the
1230 * message phase after REQ has been asserted for the handshake
1231 * but before the initiator raises ACK.
1232 */
1233 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1234 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1235 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1236 }
1237 else {
1238 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1239 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1240 }
1241 }
1242 if (!wait_req_false()) {
1243 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1244 break;
1245 }
1246
1247 if (!( (ph == PH_MSGOUT) && (cnt > 1) ))
1248 SET_5380_REG(NCR5380_ICOM, 0);
1249 else SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1250 } while (--cnt);
1251
1252 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1253 *phase = (tmp >> 2) & 7;
1254 else *phase = NR_PHASE;
1255 *len = cnt;
1256 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1257 *phase, cnt);
1258 PID("tpio2");
1259 if (!cnt || (*phase == ph))
1260 return (0);
1261 return (-1);
1262 }
1263
1264 #ifdef REAL_DMA
1265 /*
1266 * Start a DMA-transfer on the device using the current pointers.
1267 * If 'poll' is true, the function busy-waits until DMA has completed.
1268 */
1269 static void
1270 transfer_dma(reqp, phase, poll)
1271 SC_REQ *reqp;
1272 u_int phase;
1273 int poll;
1274 {
1275 int dma_done;
1276 u_char mbase = 0;
1277 int sps;
1278
1279 again:
1280 PID("tdma1");
1281
1282 /*
1283 * We should be in phase, otherwise we are not allowed to
1284 * drive the bus.
1285 */
1286 SET_5380_REG(NCR5380_TCOM, phase);
1287
1288 /*
1289 * Defer interrupts until DMA is fully running.
1290 */
1291 sps = splbio();
1292
1293 /*
1294 * Clear pending interrupts and parity errors.
1295 */
1296 scsi_clr_ipend();
1297
1298 if (!poll) {
1299 /*
1300 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1301 * to the interrupts we want enabled.
1302 */
1303 scsi_ienable();
1304 reqp->dr_flag |= DRIVER_IN_DMA;
1305 mbase = SC_E_EOPI | SC_MON_BSY;
1306 }
1307 else scsi_idisable();
1308 mbase |= IMODE_BASE | SC_M_DMA;
1309 scsi_dma_setup(reqp, phase, mbase);
1310
1311 splx(sps);
1312
1313 if (poll) {
1314 /*
1315 * On polled-dma transfers, we wait here until the
1316 * 'end-of-dma' condition occurs.
1317 */
1318 poll_edma(reqp);
1319 if (!(dma_done = dma_ready()))
1320 goto again;
1321 }
1322 PID("tdma2");
1323 }
1324
1325 /*
1326 * Check results of a DMA data-transfer.
1327 */
1328 static int
1329 dma_ready()
1330 {
1331 SC_REQ *reqp = connected;
1332 int dmstat, is_edma;
1333 long bytes_left, bytes_done;
1334
1335 is_edma = get_dma_result(reqp, &bytes_left);
1336 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1337
1338 /*
1339 * Check if the call is sensible and not caused by any spurious
1340 * interrupt.
1341 */
1342 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1343 && (dmstat & SC_PHS_MTCH) ) {
1344 ncr_tprint(reqp, "dma_ready: spurious call"
1345 "(dm:%x,last_hit: %s)\n",
1346 #ifdef DBG_PID
1347 dmstat, last_hit);
1348 #else
1349 dmstat, "unknown");
1350 #endif
1351 return (0);
1352 }
1353
1354 /*
1355 * Clear all (pending) interrupts.
1356 */
1357 scsi_clr_ipend();
1358
1359 /*
1360 * Update various transfer-pointers/lengths
1361 */
1362 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1363
1364 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1365 /*
1366 * Copy the bytes read until now from the bounce buffer
1367 * to the 'real' destination. Flush the data-cache
1368 * before copying.
1369 */
1370 PCIA();
1371 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1372 reqp->bouncerp += bytes_done;
1373 }
1374
1375 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1376 reqp->xdata_len -= bytes_done; /* XXX */
1377 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1378 reqp->dm_cur++;
1379 else reqp->dm_cur->dm_addr += bytes_done;
1380
1381 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1382 if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
1383 /* XXX: Should be parity error ???? */
1384 reqp->xs->error = XS_DRIVER_STUFFUP;
1385 }
1386
1387 /*
1388 * DMA mode should always be reset even when we will continue with the
1389 * next chain.
1390 */
1391 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
1392
1393
1394 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1395 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1396
1397 /*
1398 * Tell interrupt functions DMA mode has ended.
1399 */
1400 reqp->dr_flag &= ~DRIVER_IN_DMA;
1401
1402 /*
1403 * Clear mode and icom
1404 */
1405 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1406 SET_5380_REG(NCR5380_ICOM, 0);
1407
1408 if (dmstat & SC_BSY_ERR) {
1409 if (!reqp->xs->error)
1410 reqp->xs->error = XS_BUSY;
1411 finish_req(reqp);
1412 PID("dma_ready1");
1413 return (1);
1414 }
1415
1416 if (reqp->xs->error != 0) {
1417 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1418 reqp->msgout = MSG_ABORT;
1419 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1420 }
1421 PID("dma_ready2");
1422 return (1);
1423 }
1424 return (0);
1425 }
1426 #endif /* REAL_DMA */
1427
1428 static int
1429 check_autosense(reqp, linked)
1430 SC_REQ *reqp;
1431 int linked;
1432 {
1433 int sps;
1434
1435 /*
1436 * If we not executing an auto-sense and the status code
1437 * is request-sense, we automatically issue a request
1438 * sense command.
1439 */
1440 PID("cautos1");
1441 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1442 if (reqp->status == SCSCHKC) {
1443 memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1444 reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
1445 reqp->xdata_len = sizeof(reqp->xs->sense);
1446 reqp->dr_flag |= DRIVER_AUTOSEN;
1447 reqp->dr_flag &= ~DRIVER_DMAOK;
1448 if (!linked) {
1449 sps = splbio();
1450 reqp->next = issue_q;
1451 issue_q = reqp;
1452 splx(sps);
1453 }
1454 else reqp->xcmd.bytes[4] |= 1;
1455
1456 #ifdef DBG_REQ
1457 bzero(reqp->xdata_ptr, reqp->xdata_len);
1458 if (dbg_target_mask & (1 << reqp->targ_id))
1459 show_request(reqp, "AUTO-SENSE");
1460 #endif
1461 PID("cautos2");
1462 return (-1);
1463 }
1464 }
1465 else {
1466 /*
1467 * An auto-sense has finished
1468 */
1469 if ((reqp->status & SCSMASK) != SCSGOOD)
1470 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1471 else reqp->xs->error = XS_SENSE;
1472 reqp->status = SCSCHKC;
1473 }
1474 PID("cautos3");
1475 return (0);
1476 }
1477
1478 static int
1479 reach_msg_out(sc, len)
1480 struct ncr_softc *sc;
1481 u_long len;
1482 {
1483 u_char phase;
1484 u_char data;
1485
1486 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1487 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1488 phase = (phase >> 2) & 7;
1489 else return (-1);
1490 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1491 if (phase == PH_MSGOUT)
1492 return (0);
1493
1494 SET_5380_REG(NCR5380_TCOM, phase);
1495
1496 do {
1497 if (!wait_req_true())
1498 break;
1499 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1500 break;
1501 if (PH_IN(phase)) {
1502 data = GET_5380_REG(NCR5380_DATA);
1503 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1504 }
1505 else {
1506 SET_5380_REG(NCR5380_DATA, 0);
1507 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1508 }
1509 if (!wait_req_false())
1510 break;
1511 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1512 } while (--len);
1513
1514 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1515 phase = (phase >> 2) & 7;
1516 if (phase == PH_MSGOUT) {
1517 ncr_aprint(sc, "Message-out phase reached.\n");
1518 return (0);
1519 }
1520 }
1521 return (-1);
1522 }
1523
1524 static void
1525 scsi_reset(sc)
1526 struct ncr_softc *sc;
1527 {
1528 SC_REQ *tmp, *next;
1529 int sps;
1530
1531 ncr_aprint(sc, "Resetting SCSI-bus\n");
1532
1533 PID("scsi_reset1");
1534 sps = splbio();
1535 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1536 delay(1);
1537 SET_5380_REG(NCR5380_ICOM, 0);
1538
1539 /*
1540 * None of the jobs in the discon_q will ever be reconnected,
1541 * notify this to the higher level code.
1542 */
1543 for (tmp = discon_q; tmp ;) {
1544 next = tmp->next;
1545 tmp->next = NULL;
1546 tmp->xs->error = XS_TIMEOUT;
1547 busy &= ~(1 << tmp->targ_id);
1548 finish_req(tmp);
1549 tmp = next;
1550 }
1551 discon_q = NULL;
1552
1553 /*
1554 * The current job will never finish either.
1555 * The problem is that we can't finish the job because an instance
1556 * of main is running on it. Our best guess is that the job is currently
1557 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1558 * the job because it detects BSY-loss.
1559 */
1560 if (tmp = connected) {
1561 if (tmp->dr_flag & DRIVER_IN_DMA) {
1562 tmp->xs->error = XS_DRIVER_STUFFUP;
1563 #ifdef REAL_DMA
1564 dma_ready();
1565 #endif
1566 }
1567 }
1568 splx(sps);
1569 PID("scsi_reset2");
1570 }
1571
1572 /*
1573 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1574 * the appropriate action is carried out (reselection or DMA ready) and
1575 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1576 * and INTR_SPURIOUS is returned.
1577 */
1578 static int
1579 check_intr(sc)
1580 struct ncr_softc *sc;
1581 {
1582 SC_REQ *reqp;
1583
1584 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1585 ==(SC_S_SEL|SC_S_IO))
1586 return (INTR_RESEL);
1587 else {
1588 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1589 reqp->dr_flag &= ~DRIVER_IN_DMA;
1590 return (INTR_DMA);
1591 }
1592 }
1593 scsi_clr_ipend();
1594 printf("-->");
1595 scsi_show();
1596 ncr_aprint(sc, "Spurious interrupt.\n");
1597 return (INTR_SPURIOUS);
1598 }
1599
1600 #ifdef REAL_DMA
1601 /*
1602 * Check if DMA can be used for this request. This function also builds
1603 * the dma-chain.
1604 */
1605 static int
1606 scsi_dmaok(reqp)
1607 SC_REQ *reqp;
1608 {
1609 u_long phy_buf;
1610 u_long phy_len;
1611 void *req_addr;
1612 u_long req_len;
1613 struct dma_chain *dm;
1614
1615 /*
1616 * Initialize locals and requests' DMA-chain.
1617 */
1618 req_len = reqp->xdata_len;
1619 req_addr = (void*)reqp->xdata_ptr;
1620 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1621 dm->dm_count = dm->dm_addr = 0;
1622 reqp->dr_flag &= ~DRIVER_BOUNCING;
1623
1624 /*
1625 * Do not accept zero length DMA.
1626 */
1627 if (req_len == 0)
1628 return (0);
1629
1630 /*
1631 * LWP: I think that this restriction is not strictly nessecary.
1632 */
1633 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1634 return (0);
1635
1636 /*
1637 * Build the DMA-chain.
1638 */
1639 dm->dm_addr = phy_buf = kvtop(req_addr);
1640 while (req_len) {
1641 if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1642 phy_len = req_len;
1643
1644 req_addr += phy_len;
1645 req_len -= phy_len;
1646 dm->dm_count += phy_len;
1647
1648 if (req_len) {
1649 u_long tmp = kvtop(req_addr);
1650
1651 if ((phy_buf + phy_len) != tmp) {
1652 if (wrong_dma_range(reqp, dm)) {
1653 if (reqp->dr_flag & DRIVER_BOUNCING)
1654 goto bounceit;
1655 return (0);
1656 }
1657
1658 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1659 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1660 return (0);
1661 }
1662 dm->dm_count = 0;
1663 dm->dm_addr = tmp;
1664 }
1665 phy_buf = tmp;
1666 }
1667 }
1668 if (wrong_dma_range(reqp, dm)) {
1669 if (reqp->dr_flag & DRIVER_BOUNCING)
1670 goto bounceit;
1671 return (0);
1672 }
1673 reqp->dm_last = dm;
1674 return (1);
1675
1676 bounceit:
1677 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1678 /*
1679 * If we can't get a bounce buffer, forget DMA
1680 */
1681 reqp->dr_flag &= ~DRIVER_BOUNCING;
1682 return(0);
1683 }
1684 /*
1685 * Initialize a single DMA-range containing the bounced request
1686 */
1687 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1688 dm->dm_addr = kvtop(reqp->bounceb);
1689 dm->dm_count = reqp->xdata_len;
1690 reqp->bouncerp = reqp->bounceb;
1691
1692 return (1);
1693 }
1694 #endif /* REAL_DMA */
1695
1696 static void
1697 run_main(sc)
1698 struct ncr_softc *sc;
1699 {
1700 int sps = splbio();
1701
1702 if (!main_running) {
1703 /*
1704 * If shared resources are required, claim them
1705 * before entering 'scsi_main'. If we can't get them
1706 * now, assume 'run_main' will be called when the resource
1707 * becomes available.
1708 */
1709 if (!claimed_dma()) {
1710 splx(sps);
1711 return;
1712 }
1713 main_running = 1;
1714 splx(sps);
1715 scsi_main(sc);
1716 }
1717 else splx(sps);
1718 }
1719
1720 /*
1721 * Prefix message with full target info.
1722 */
1723 static void
1724 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1725 {
1726 va_list ap;
1727
1728 va_start(ap, fmt);
1729 sc_print_addr(reqp->xs->sc_link);
1730 printf("%r", fmt, ap);
1731 va_end(ap);
1732 }
1733
1734 /*
1735 * Prefix message with adapter info.
1736 */
1737 static void
1738 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1739 {
1740 va_list ap;
1741
1742 va_start(ap, fmt);
1743 printf("%s : %r", sc->sc_dev.dv_xname, fmt, ap);
1744 va_end(ap);
1745 }
1746 /****************************************************************************
1747 * Start Debugging Functions *
1748 ****************************************************************************/
1749 static void
1750 show_data_sense(xs)
1751 struct scsi_xfer *xs;
1752 {
1753 u_char *p1, *p2;
1754 int i;
1755 int sz;
1756
1757 p1 = (u_char *) xs->cmd;
1758 p2 = (u_char *)&xs->sense;
1759 if(*p2 == 0)
1760 return; /* No(n)sense */
1761 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
1762 for (i = 0; i < sz; i++)
1763 printf("%x ", p1[i]);
1764 printf("\nsense: ");
1765 for (i = 0; i < sizeof(xs->sense); i++)
1766 printf("%x ", p2[i]);
1767 printf("\n");
1768 }
1769
1770 static void
1771 show_request(reqp, qtxt)
1772 SC_REQ *reqp;
1773 char *qtxt;
1774 {
1775 printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x resid=%d %s\n",
1776 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1777 reqp->xcmd.opcode, reqp->status, reqp->message,
1778 reqp->xs->error, reqp->xs->resid, reqp->link ? "L":"");
1779 if (reqp->status == SCSCHKC)
1780 show_data_sense(reqp->xs);
1781 }
1782
1783 scsi_show()
1784 {
1785 SC_REQ *tmp;
1786 int sps = splhigh();
1787
1788 #ifndef DBG_PID
1789 #define last_hit ""
1790 #define olast_hit ""
1791 #endif
1792
1793 for (tmp = issue_q; tmp; tmp = tmp->next)
1794 show_request(tmp, "ISSUED");
1795 for (tmp = discon_q; tmp; tmp = tmp->next)
1796 show_request(tmp, "DISCONNECTED");
1797 if (connected)
1798 show_request(connected, "CONNECTED");
1799 printf("idstat: %x, dmstat: %x\n", GET_5380_REG(NCR5380_IDSTAT),
1800 GET_5380_REG(NCR5380_DMSTAT));
1801 /* show_signals(); */
1802 if (connected)
1803 printf("phase = %d, ", connected->phase);
1804 printf("busy:%x, last_hit:%s, olast_hit:%s spl:%04x\n", busy,
1805 last_hit, olast_hit, sps);
1806
1807 splx(sps);
1808 }
1809