ncr5380.c revision 1.2 1 /* $NetBSD: ncr5380.c,v 1.2 1995/09/05 11:21:34 briggs 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 {
580 if (scsi_main_irq())
581 goto connected;
582 panic("Got DMA interrupt without DMA");
583 }
584 #endif
585 scsi_clr_ipend();
586 goto connected;
587 }
588 }
589 reconsider_dma();
590
591 main_running = 0;
592 splx(sps);
593 PID("scsi_main5");
594 }
595
596 #ifdef REAL_DMA
597 /*
598 * The SCSI-DMA interrupt.
599 * This interrupt can only be triggered when running in non-polled DMA
600 * mode. When DMA is not active, it will be silently ignored, it is usually
601 * to late because the EOP interrupt of the controller happens just a tiny
602 * bit earlier. It might become usefull when scatter/gather is implemented,
603 * because in that case only part of the DATAIN/DATAOUT transfer is taken
604 * out of a single buffer.
605 */
606 static void
607 ncr_dma_intr(sc)
608 struct ncr_softc *sc;
609 {
610 SC_REQ *reqp;
611 int dma_done;
612
613 PID("ncr_dma_intr");
614 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
615 scsi_idisable();
616 if (!(dma_done = dma_ready())) {
617 transfer_dma(reqp, reqp->phase, 0);
618 return;
619 }
620 run_main(sc);
621 }
622 }
623 #endif /* REAL_DMA */
624
625 /*
626 * The SCSI-controller interrupt. This interrupt occurs on reselections and
627 * at the end of non-polled DMA-interrupts. It is assumed to be called from
628 * the machine-dependent hardware interrupt.
629 */
630 static void
631 ncr_ctrl_intr(sc)
632 struct ncr_softc *sc;
633 {
634 int itype;
635 int dma_done;
636
637 while (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
638 scsi_idisable();
639 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
640 if (itype == INTR_RESEL)
641 reselect(sc);
642 else {
643 #ifdef REAL_DMA
644 if (!(dma_done = dma_ready())) {
645 transfer_dma(connected, connected->phase, 0);
646 return;
647 }
648 #else
649 if (scsi_main_irq())
650 return;
651 panic("Got DMA interrupt without DMA\n");
652 #endif
653 }
654 scsi_clr_ipend();
655 }
656 run_main(sc);
657 return;
658 }
659 PID("ncr_ctrl_intr1");
660 }
661
662 /*
663 * Initiate a connection path between the host and the target. The function
664 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
665 * is selected and an 'IDENTIFY' message is send.
666 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
667 * the target does not respond (to either selection or 'MESSAGE OUT') the
668 * 'done' function is executed.
669 * The result code given by the driver can be influenced by setting 'code'
670 * to a non-zero value. This is the case when 'select' is called by abort.
671 */
672 static int
673 scsi_select(reqp, code)
674 SC_REQ *reqp;
675 {
676 u_long timeout;
677 u_char tmp[1];
678 u_char phase;
679 u_long cnt;
680 int sps;
681 struct ncr_softc *sc;
682
683 sc = reqp->xs->sc_link->adapter_softc;
684 DBG_SELPRINT ("Starting arbitration\n", 0);
685 PID("scsi_select1");
686
687 sps = splbio();
688
689 /*
690 * Prevent a race condition here. If a reslection interrupt occurred
691 * between the decision to pick a new request and the call to select,
692 * we abort the selection.
693 * Interrupts are lowered when the 5380 is setup to arbitrate for the
694 * bus.
695 */
696 if (connected || (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
697 splx(sps);
698 PID("scsi_select2");
699 return (-1);
700 }
701
702 /*
703 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
704 * selection.
705 */
706 SET_5380_REG(NCR5380_TCOM, 0);
707 SET_5380_REG(NCR5380_ICOM, 0);
708
709 /*
710 * Arbitrate for the bus.
711 */
712 SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
713 SET_5380_REG(NCR5380_MODE, SC_ARBIT);
714
715 splx(sps);
716
717 cnt = 10000;
718 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
719 delay(1);
720
721 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
722 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
723 delay(1);
724 PID("scsi_select3");
725
726 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) {
727 /*
728 * Damn, we have a connected target that we don't know
729 * of. Some targets seem to respond to a selection
730 * AFTER the selection-timeout. Try to get the target
731 * into the Message-out phase so we can send an ABORT
732 * message. We try to avoid resetting the SCSI-bus!
733 */
734 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
735 u_long len = 1;
736 u_char phase = PH_MSGOUT;
737 u_char msg = MSG_ABORT;
738
739 transfer_pio(&phase, &msg, &len);
740 }
741 else if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
742 scsi_reset(sc);
743 }
744 PID("scsi_select4");
745 return (-1);
746 }
747
748 /* The arbitration delay is 2.2 usecs */
749 delay(3);
750
751 /*
752 * Check the result of the arbitration. If we failed, return -1.
753 */
754 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
755 /*
756 * The spec requires that we should read the data register to
757 * check for higher id's and check the SC_LA again.
758 */
759 tmp[0] = GET_5380_REG(NCR5380_DATA);
760 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
761 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
762 SET_5380_REG(NCR5380_ICOM, 0);
763 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
764 PID("scsi_select5");
765 return (-1);
766 }
767 }
768 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
769 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
770 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
771 SET_5380_REG(NCR5380_ICOM, 0);
772 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
773 PID("scsi_select6");
774 return (-1);
775 }
776 /* Bus settle delay + Bus clear delay = 1.2 usecs */
777 delay(2);
778 DBG_SELPRINT ("Arbitration complete\n", 0);
779
780 /*
781 * Now that we won the arbitration, start the selection.
782 */
783 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | (1 << reqp->targ_id));
784
785 /*
786 * Raise ATN while SEL is true before BSY goes false from arbitration,
787 * since this is the only way to guarantee that we'll get a MESSAGE OUT
788 * phase immediately after the selection.
789 */
790 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | SC_A_ATN | SC_ADTB);
791 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
792
793 /*
794 * Turn off reselection interrupts
795 */
796 SET_5380_REG(NCR5380_IDSTAT, 0);
797
798 /*
799 * Reset BSY. The delay following it, surpresses a glitch in the
800 * 5380 which causes us to see our own BSY signal instead of that of
801 * the target.
802 */
803 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN | SC_ADTB);
804 delay(1);
805
806 /*
807 * Wait for the target to react, the specs call for a timeout of
808 * 250 ms.
809 */
810 cnt = 25000 /* 250 */;
811 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
812 delay(10);
813
814 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
815 /*
816 * There is no reaction from the target, start the selection
817 * timeout procedure. We release the databus but keep SEL
818 * asserted. After that we wait a 'selection abort time' (200
819 * usecs) and 2 deskew delays (90 ns) and check BSY again.
820 * When BSY is asserted, we assume the selection succeeded,
821 * otherwise we release the bus.
822 */
823 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN);
824 delay(201);
825 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
826 SET_5380_REG(NCR5380_ICOM, 0);
827 reqp->xs->error = code ? code : XS_SELTIMEOUT;
828 DBG_SELPRINT ("Target %d not responding to sel\n",
829 reqp->targ_id);
830 finish_req(reqp);
831 PID("scsi_select7");
832 return (0);
833 }
834 }
835 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
836
837 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
838
839 /*
840 * The SCSI-interrupts are disabled while a request is being handled.
841 */
842 scsi_idisable();
843
844 /*
845 * Since we followed the SCSI-spec and raised ATN while SEL was true
846 * but before BSY was false during the selection, a 'MESSAGE OUT'
847 * phase should follow. Here we send an 'IDENTIFY' message.
848 * Allow disconnect only when interrups are allowed.
849 */
850 tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
851 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
852 cnt = 1;
853 phase = PH_MSGOUT;
854 if (transfer_pio(&phase, tmp, &cnt) || cnt) {
855 DBG_SELPRINT ("Target %d: failed to send identify\n",
856 reqp->targ_id);
857 /*
858 * Try to disconnect from the target. We cannot leave it just
859 * hanging here.
860 */
861 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
862 u_long len = 1;
863 u_char phase = PH_MSGOUT;
864 u_char msg = MSG_ABORT;
865
866 transfer_pio(&phase, &msg, &len);
867 }
868 else scsi_reset(sc);
869
870 SET_5380_REG(NCR5380_ICOM, 0);
871 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
872 finish_req(reqp);
873 PID("scsi_select8");
874 return (0);
875 }
876 reqp->phase = PH_MSGOUT;
877
878 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
879 /*
880 * Command is connected, start timer ticking.
881 */
882 ccb_p->xtimeout = ccb_p->timeout + Lbolt;
883 #endif
884
885 connected = reqp;
886 busy |= 1 << reqp->targ_id;
887 PID("scsi_select9");
888 return (0);
889 }
890
891 /*
892 * Return codes:
893 * -1: quit main, trigger on interrupt
894 * 0: keep on running main.
895 */
896 static int
897 information_transfer()
898 {
899 SC_REQ *reqp = connected;
900 u_char tmp, phase;
901 u_long len;
902
903 PID("info_transf1");
904 /*
905 * Clear pending interrupts from 5380-chip.
906 */
907 scsi_clr_ipend();
908
909 /*
910 * We only have a valid SCSI-phase when REQ is asserted. Something
911 * is deadly wrong when BSY has dropped.
912 */
913 tmp = GET_5380_REG(NCR5380_IDSTAT);
914
915 if (!(tmp & SC_S_BSY)) {
916 busy &= ~(1 << reqp->targ_id);
917 connected = NULL;
918 reqp->xs->error = XS_BUSY;
919 finish_req(reqp);
920 PID("info_transf2");
921 return (0);
922 }
923
924 if (tmp & SC_S_REQ) {
925 phase = (tmp >> 2) & 7;
926 if (phase != reqp->phase) {
927 reqp->phase = phase;
928 DBG_INFPRINT(show_phase, reqp, phase);
929 }
930 }
931 else return (-1);
932
933 switch (phase) {
934 case PH_DATAOUT:
935
936 #ifdef DBG_NOWRITE
937 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
938 reqp->msgout = MSG_ABORT;
939 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
940 return (-1);
941 #endif /* DBG_NOWRITE */
942 /*
943 * If this is the first write using DMA, fill
944 * the bounce buffer.
945 */
946 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
947 if (reqp->dr_flag & DRIVER_BOUNCING)
948 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
949 }
950
951 case PH_DATAIN:
952 #ifdef REAL_DMA
953 if (reqp->dr_flag & DRIVER_DMAOK) {
954 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
955 transfer_dma(reqp, phase, poll);
956 if (!poll)
957 return (0);
958 }
959 else
960 #endif
961 {
962 PID("info_transf3");
963 len = reqp->xdata_len;
964 #ifdef USE_PDMA
965 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
966 return 0;
967 #else
968 transfer_pio(&phase, reqp->xdata_ptr, &len);
969 #endif
970 reqp->xdata_ptr += reqp->xdata_len - len;
971 reqp->xdata_len = len;
972 }
973 return (-1);
974 case PH_MSGIN:
975 /*
976 * We only expect single byte messages here.
977 */
978 len = 1;
979 transfer_pio(&phase, &tmp, &len);
980 reqp->message = tmp;
981 return (handle_message(reqp, tmp));
982 case PH_MSGOUT:
983 len = 1;
984 transfer_pio(&phase, &reqp->msgout, &len);
985 if (reqp->msgout == MSG_ABORT) {
986 busy &= ~(1 << reqp->targ_id);
987 connected = NULL;
988 reqp->xs->error = XS_DRIVER_STUFFUP;
989 finish_req(reqp);
990 PID("info_transf4");
991 return (0);
992 }
993 reqp->msgout = MSG_NOOP;
994 return (-1);
995 case PH_CMD :
996 len = command_size(reqp->xcmd.opcode);
997 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len);
998 PID("info_transf5");
999 return (-1);
1000 case PH_STATUS:
1001 len = 1;
1002 transfer_pio(&phase, &tmp, &len);
1003 reqp->status = tmp;
1004 PID("info_transf6");
1005 return (-1);
1006 default :
1007 ncr_tprint(reqp, "Unknown phase\n");
1008 }
1009 PID("info_transf7");
1010 return (-1);
1011 }
1012
1013 /*
1014 * Handle the message 'msg' send to us by the target.
1015 * Return values:
1016 * 0 : The current command has completed.
1017 * -1 : Get on to the next phase.
1018 */
1019 static int
1020 handle_message(reqp, msg)
1021 SC_REQ *reqp;
1022 u_int msg;
1023 {
1024 int sps;
1025
1026 PID("hmessage1");
1027 switch (msg) {
1028 /*
1029 * Linking lets us reduce the time required to get
1030 * the next command to the device, skipping the arbitration
1031 * and selection time. In the current implementation,
1032 * we merely have to start the next command pointed
1033 * to by 'next_link'.
1034 */
1035 case MSG_LINK_CMD_COMPLETE:
1036 case MSG_LINK_CMD_COMPLETEF:
1037 if (reqp->link == NULL) {
1038 ncr_tprint(reqp, "No link for linked command");
1039 reqp->msgout = MSG_ABORT;
1040 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1041 PID("hmessage2");
1042 return (-1);
1043 }
1044 reqp->xs->error = 0;
1045
1046 #ifdef AUTO_SENSE
1047 if (check_autosense(reqp, 0) == -1)
1048 return (-1);
1049 #endif /* AUTO_SENSE */
1050
1051 #ifdef DBG_REQ
1052 if (dbg_target_mask & (1 << reqp->targ_id))
1053 show_request(reqp->link, "LINK");
1054 #endif
1055 connected = reqp->link;
1056 finish_req(reqp);
1057 PID("hmessage3");
1058 return (-1);
1059 case MSG_ABORT:
1060 case MSG_CMDCOMPLETE:
1061 connected = NULL;
1062 busy &= ~(1 << reqp->targ_id);
1063 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1064 reqp->xs->resid = reqp->xdata_len;
1065 reqp->xs->error = 0;
1066 }
1067
1068 #ifdef AUTO_SENSE
1069 if (check_autosense(reqp, 0) == -1) {
1070 PID("hmessage4");
1071 return (0);
1072 }
1073 #endif /* AUTO_SENSE */
1074
1075 finish_req(reqp);
1076 PID("hmessage5");
1077 return (0);
1078 case MSG_MESSAGE_REJECT:
1079 PID("hmessage6");
1080 return (-1);
1081 case MSG_DISCONNECT:
1082 #ifdef DBG_REQ
1083 if (dbg_target_mask & (1 << reqp->targ_id))
1084 show_request(reqp, "DISCON");
1085 #endif
1086 sps = splbio();
1087 connected = NULL;
1088 reqp->next = discon_q;
1089 discon_q = reqp;
1090 splx(sps);
1091 PID("hmessage7");
1092 return (0);
1093 case MSG_SAVEDATAPOINTER:
1094 case MSG_RESTOREPOINTERS:
1095 /*
1096 * We save pointers implicitely at disconnect.
1097 * So we can ignore these messages.
1098 */
1099 PID("hmessage8");
1100 return (-1);
1101 default:
1102 ncr_tprint(reqp, "Unkown message %x\n", msg);
1103 return (-1);
1104 }
1105 PID("hmessage9");
1106 return (-1);
1107 }
1108
1109 /*
1110 * Handle reselection. If a valid reconnection occurs, connected
1111 * points at the reconnected command. The command is removed from the
1112 * disconnected queue.
1113 */
1114 static void
1115 reselect(sc)
1116 struct ncr_softc *sc;
1117 {
1118 u_char phase;
1119 u_long len;
1120 u_char msg;
1121 u_char target_mask;
1122 int abort = 0;
1123 SC_REQ *tmp, *prev;
1124
1125 PID("reselect1");
1126 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1127
1128 /*
1129 * At this point, we have detected that our SCSI-id is on the bus,
1130 * SEL is true and BSY was false for at least one bus settle
1131 * delay (400 ns.).
1132 * We must assert BSY ourselves, until the target drops the SEL signal.
1133 * This should happen within 2 deskew delays (2 * 45ns.) We wait too
1134 * long for this, but it's better to wait for slow targets than to
1135 * reset the bus too early...
1136 */
1137 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1138 len = 2;
1139 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1140 delay(1);
1141 len--;
1142 }
1143 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1144 /* Damn SEL isn't dropping */
1145 scsi_reset(sc);
1146 return;
1147 }
1148
1149 SET_5380_REG(NCR5380_ICOM, 0);
1150
1151 /*
1152 * Get the expected identify message.
1153 */
1154 phase = PH_MSGIN;
1155 len = 1;
1156 transfer_pio(&phase, &msg, &len);
1157 if (len || !MSG_ISIDENTIFY(msg)) {
1158 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1159 abort = 1;
1160 }
1161 else {
1162 /*
1163 * Find the command reconnecting
1164 */
1165 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1166 if (target_mask == (1 << tmp->targ_id)) {
1167 if (prev)
1168 prev->next = tmp->next;
1169 else discon_q = tmp->next;
1170 tmp->next = NULL;
1171 break;
1172 }
1173 }
1174 if (tmp == NULL) {
1175 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1176 target_mask);
1177 abort = 1;
1178 }
1179 }
1180 if (abort) {
1181 msg = MSG_ABORT;
1182 len = 1;
1183 phase = PH_MSGOUT;
1184
1185 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1186 transfer_pio(&phase, &msg, &len);
1187 }
1188 else {
1189 connected = tmp;
1190 #ifdef DBG_REQ
1191 if (dbg_target_mask & (1 << tmp->targ_id))
1192 show_request(tmp, "RECON");
1193 #endif
1194 }
1195 PID("reselect2");
1196 }
1197
1198 /*
1199 * Transfer data in a given phase using programmed I/O.
1200 * Returns -1 when a different phase is entered without transferring the
1201 * maximum number of bytes, 0 if all bytes or exit is in the same
1202 * phase.
1203 */
1204 static int
1205 transfer_pio(phase, data, len)
1206 u_char *phase;
1207 u_char *data;
1208 u_long *len;
1209 {
1210 u_int cnt = *len;
1211 u_char ph = *phase;
1212 u_char tmp;
1213
1214 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1215 PID("tpio1");
1216 SET_5380_REG(NCR5380_TCOM, ph);
1217 do {
1218 if (!wait_req_true()) {
1219 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1220 break;
1221 }
1222 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1223 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1224 break;
1225 }
1226 if (PH_IN(ph)) {
1227 *data++ = GET_5380_REG(NCR5380_DATA);
1228 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1229 }
1230 else {
1231 SET_5380_REG(NCR5380_DATA, *data++);
1232
1233 /*
1234 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1235 * the initiator should drop ATN on the last byte of the
1236 * message phase after REQ has been asserted for the handshake
1237 * but before the initiator raises ACK.
1238 */
1239 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1240 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1241 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1242 }
1243 else {
1244 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1245 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1246 }
1247 }
1248 if (!wait_req_false()) {
1249 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1250 break;
1251 }
1252
1253 if (!( (ph == PH_MSGOUT) && (cnt > 1) ))
1254 SET_5380_REG(NCR5380_ICOM, 0);
1255 else SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1256 } while (--cnt);
1257
1258 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1259 *phase = (tmp >> 2) & 7;
1260 else *phase = NR_PHASE;
1261 *len = cnt;
1262 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1263 *phase, cnt);
1264 PID("tpio2");
1265 if (!cnt || (*phase == ph))
1266 return (0);
1267 return (-1);
1268 }
1269
1270 #ifdef REAL_DMA
1271 /*
1272 * Start a DMA-transfer on the device using the current pointers.
1273 * If 'poll' is true, the function busy-waits until DMA has completed.
1274 */
1275 static void
1276 transfer_dma(reqp, phase, poll)
1277 SC_REQ *reqp;
1278 u_int phase;
1279 int poll;
1280 {
1281 int dma_done;
1282 u_char mbase = 0;
1283 int sps;
1284
1285 again:
1286 PID("tdma1");
1287
1288 /*
1289 * We should be in phase, otherwise we are not allowed to
1290 * drive the bus.
1291 */
1292 SET_5380_REG(NCR5380_TCOM, phase);
1293
1294 /*
1295 * Defer interrupts until DMA is fully running.
1296 */
1297 sps = splbio();
1298
1299 /*
1300 * Clear pending interrupts and parity errors.
1301 */
1302 scsi_clr_ipend();
1303
1304 if (!poll) {
1305 /*
1306 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1307 * to the interrupts we want enabled.
1308 */
1309 scsi_ienable();
1310 reqp->dr_flag |= DRIVER_IN_DMA;
1311 mbase = SC_E_EOPI | SC_MON_BSY;
1312 }
1313 else scsi_idisable();
1314 mbase |= IMODE_BASE | SC_M_DMA;
1315 scsi_dma_setup(reqp, phase, mbase);
1316
1317 splx(sps);
1318
1319 if (poll) {
1320 /*
1321 * On polled-dma transfers, we wait here until the
1322 * 'end-of-dma' condition occurs.
1323 */
1324 poll_edma(reqp);
1325 if (!(dma_done = dma_ready()))
1326 goto again;
1327 }
1328 PID("tdma2");
1329 }
1330
1331 /*
1332 * Check results of a DMA data-transfer.
1333 */
1334 static int
1335 dma_ready()
1336 {
1337 SC_REQ *reqp = connected;
1338 int dmstat, is_edma;
1339 long bytes_left, bytes_done;
1340
1341 is_edma = get_dma_result(reqp, &bytes_left);
1342 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1343
1344 /*
1345 * Check if the call is sensible and not caused by any spurious
1346 * interrupt.
1347 */
1348 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1349 && (dmstat & SC_PHS_MTCH) ) {
1350 ncr_tprint(reqp, "dma_ready: spurious call"
1351 "(dm:%x,last_hit: %s)\n",
1352 #ifdef DBG_PID
1353 dmstat, last_hit);
1354 #else
1355 dmstat, "unknown");
1356 #endif
1357 return (0);
1358 }
1359
1360 /*
1361 * Clear all (pending) interrupts.
1362 */
1363 scsi_clr_ipend();
1364
1365 /*
1366 * Update various transfer-pointers/lengths
1367 */
1368 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1369
1370 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1371 /*
1372 * Copy the bytes read until now from the bounce buffer
1373 * to the 'real' destination. Flush the data-cache
1374 * before copying.
1375 */
1376 PCIA();
1377 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1378 reqp->bouncerp += bytes_done;
1379 }
1380
1381 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1382 reqp->xdata_len -= bytes_done; /* XXX */
1383 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1384 reqp->dm_cur++;
1385 else reqp->dm_cur->dm_addr += bytes_done;
1386
1387 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1388 if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
1389 /* XXX: Should be parity error ???? */
1390 reqp->xs->error = XS_DRIVER_STUFFUP;
1391 }
1392
1393 /*
1394 * DMA mode should always be reset even when we will continue with the
1395 * next chain.
1396 */
1397 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
1398
1399
1400 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1401 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1402
1403 /*
1404 * Tell interrupt functions DMA mode has ended.
1405 */
1406 reqp->dr_flag &= ~DRIVER_IN_DMA;
1407
1408 /*
1409 * Clear mode and icom
1410 */
1411 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1412 SET_5380_REG(NCR5380_ICOM, 0);
1413
1414 if (dmstat & SC_BSY_ERR) {
1415 if (!reqp->xs->error)
1416 reqp->xs->error = XS_BUSY;
1417 finish_req(reqp);
1418 PID("dma_ready1");
1419 return (1);
1420 }
1421
1422 if (reqp->xs->error != 0) {
1423 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1424 reqp->msgout = MSG_ABORT;
1425 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1426 }
1427 PID("dma_ready2");
1428 return (1);
1429 }
1430 return (0);
1431 }
1432 #endif /* REAL_DMA */
1433
1434 static int
1435 check_autosense(reqp, linked)
1436 SC_REQ *reqp;
1437 int linked;
1438 {
1439 int sps;
1440
1441 /*
1442 * If we not executing an auto-sense and the status code
1443 * is request-sense, we automatically issue a request
1444 * sense command.
1445 */
1446 PID("cautos1");
1447 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1448 if (reqp->status == SCSCHKC) {
1449 memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1450 reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
1451 reqp->xdata_len = sizeof(reqp->xs->sense);
1452 reqp->dr_flag |= DRIVER_AUTOSEN;
1453 reqp->dr_flag &= ~DRIVER_DMAOK;
1454 if (!linked) {
1455 sps = splbio();
1456 reqp->next = issue_q;
1457 issue_q = reqp;
1458 splx(sps);
1459 }
1460 else reqp->xcmd.bytes[4] |= 1;
1461
1462 #ifdef DBG_REQ
1463 bzero(reqp->xdata_ptr, reqp->xdata_len);
1464 if (dbg_target_mask & (1 << reqp->targ_id))
1465 show_request(reqp, "AUTO-SENSE");
1466 #endif
1467 PID("cautos2");
1468 return (-1);
1469 }
1470 }
1471 else {
1472 /*
1473 * An auto-sense has finished
1474 */
1475 if ((reqp->status & SCSMASK) != SCSGOOD)
1476 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1477 else reqp->xs->error = XS_SENSE;
1478 reqp->status = SCSCHKC;
1479 }
1480 PID("cautos3");
1481 return (0);
1482 }
1483
1484 static int
1485 reach_msg_out(sc, len)
1486 struct ncr_softc *sc;
1487 u_long len;
1488 {
1489 u_char phase;
1490 u_char data;
1491
1492 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1493 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1494 phase = (phase >> 2) & 7;
1495 else return (-1);
1496 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1497 if (phase == PH_MSGOUT)
1498 return (0);
1499
1500 SET_5380_REG(NCR5380_TCOM, phase);
1501
1502 do {
1503 if (!wait_req_true())
1504 break;
1505 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1506 break;
1507 if (PH_IN(phase)) {
1508 data = GET_5380_REG(NCR5380_DATA);
1509 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1510 }
1511 else {
1512 SET_5380_REG(NCR5380_DATA, 0);
1513 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1514 }
1515 if (!wait_req_false())
1516 break;
1517 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1518 } while (--len);
1519
1520 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1521 phase = (phase >> 2) & 7;
1522 if (phase == PH_MSGOUT) {
1523 ncr_aprint(sc, "Message-out phase reached.\n");
1524 return (0);
1525 }
1526 }
1527 return (-1);
1528 }
1529
1530 static void
1531 scsi_reset(sc)
1532 struct ncr_softc *sc;
1533 {
1534 SC_REQ *tmp, *next;
1535 int sps;
1536
1537 ncr_aprint(sc, "Resetting SCSI-bus\n");
1538
1539 PID("scsi_reset1");
1540 sps = splbio();
1541 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1542 delay(1);
1543 SET_5380_REG(NCR5380_ICOM, 0);
1544
1545 /*
1546 * None of the jobs in the discon_q will ever be reconnected,
1547 * notify this to the higher level code.
1548 */
1549 for (tmp = discon_q; tmp ;) {
1550 next = tmp->next;
1551 tmp->next = NULL;
1552 tmp->xs->error = XS_TIMEOUT;
1553 busy &= ~(1 << tmp->targ_id);
1554 finish_req(tmp);
1555 tmp = next;
1556 }
1557 discon_q = NULL;
1558
1559 /*
1560 * The current job will never finish either.
1561 * The problem is that we can't finish the job because an instance
1562 * of main is running on it. Our best guess is that the job is currently
1563 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1564 * the job because it detects BSY-loss.
1565 */
1566 if (tmp = connected) {
1567 if (tmp->dr_flag & DRIVER_IN_DMA) {
1568 tmp->xs->error = XS_DRIVER_STUFFUP;
1569 #ifdef REAL_DMA
1570 dma_ready();
1571 #endif
1572 }
1573 }
1574 splx(sps);
1575 PID("scsi_reset2");
1576 }
1577
1578 /*
1579 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1580 * the appropriate action is carried out (reselection or DMA ready) and
1581 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1582 * and INTR_SPURIOUS is returned.
1583 */
1584 static int
1585 check_intr(sc)
1586 struct ncr_softc *sc;
1587 {
1588 SC_REQ *reqp;
1589
1590 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1591 ==(SC_S_SEL|SC_S_IO))
1592 return (INTR_RESEL);
1593 else {
1594 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1595 reqp->dr_flag &= ~DRIVER_IN_DMA;
1596 return (INTR_DMA);
1597 }
1598 }
1599 scsi_clr_ipend();
1600 printf("-->");
1601 scsi_show();
1602 ncr_aprint(sc, "Spurious interrupt.\n");
1603 return (INTR_SPURIOUS);
1604 }
1605
1606 #ifdef REAL_DMA
1607 /*
1608 * Check if DMA can be used for this request. This function also builds
1609 * the dma-chain.
1610 */
1611 static int
1612 scsi_dmaok(reqp)
1613 SC_REQ *reqp;
1614 {
1615 u_long phy_buf;
1616 u_long phy_len;
1617 void *req_addr;
1618 u_long req_len;
1619 struct dma_chain *dm;
1620
1621 /*
1622 * Initialize locals and requests' DMA-chain.
1623 */
1624 req_len = reqp->xdata_len;
1625 req_addr = (void*)reqp->xdata_ptr;
1626 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1627 dm->dm_count = dm->dm_addr = 0;
1628 reqp->dr_flag &= ~DRIVER_BOUNCING;
1629
1630 /*
1631 * Do not accept zero length DMA.
1632 */
1633 if (req_len == 0)
1634 return (0);
1635
1636 /*
1637 * LWP: I think that this restriction is not strictly nessecary.
1638 */
1639 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1640 return (0);
1641
1642 /*
1643 * Build the DMA-chain.
1644 */
1645 dm->dm_addr = phy_buf = kvtop(req_addr);
1646 while (req_len) {
1647 if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1648 phy_len = req_len;
1649
1650 req_addr += phy_len;
1651 req_len -= phy_len;
1652 dm->dm_count += phy_len;
1653
1654 if (req_len) {
1655 u_long tmp = kvtop(req_addr);
1656
1657 if ((phy_buf + phy_len) != tmp) {
1658 if (wrong_dma_range(reqp, dm)) {
1659 if (reqp->dr_flag & DRIVER_BOUNCING)
1660 goto bounceit;
1661 return (0);
1662 }
1663
1664 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1665 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1666 return (0);
1667 }
1668 dm->dm_count = 0;
1669 dm->dm_addr = tmp;
1670 }
1671 phy_buf = tmp;
1672 }
1673 }
1674 if (wrong_dma_range(reqp, dm)) {
1675 if (reqp->dr_flag & DRIVER_BOUNCING)
1676 goto bounceit;
1677 return (0);
1678 }
1679 reqp->dm_last = dm;
1680 return (1);
1681
1682 bounceit:
1683 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1684 /*
1685 * If we can't get a bounce buffer, forget DMA
1686 */
1687 reqp->dr_flag &= ~DRIVER_BOUNCING;
1688 return(0);
1689 }
1690 /*
1691 * Initialize a single DMA-range containing the bounced request
1692 */
1693 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1694 dm->dm_addr = kvtop(reqp->bounceb);
1695 dm->dm_count = reqp->xdata_len;
1696 reqp->bouncerp = reqp->bounceb;
1697
1698 return (1);
1699 }
1700 #endif /* REAL_DMA */
1701
1702 static void
1703 run_main(sc)
1704 struct ncr_softc *sc;
1705 {
1706 int sps = splbio();
1707
1708 if (!main_running) {
1709 /*
1710 * If shared resources are required, claim them
1711 * before entering 'scsi_main'. If we can't get them
1712 * now, assume 'run_main' will be called when the resource
1713 * becomes available.
1714 */
1715 if (!claimed_dma()) {
1716 splx(sps);
1717 return;
1718 }
1719 main_running = 1;
1720 splx(sps);
1721 scsi_main(sc);
1722 }
1723 else splx(sps);
1724 }
1725
1726 /*
1727 * Prefix message with full target info.
1728 */
1729 static void
1730 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1731 {
1732 va_list ap;
1733
1734 va_start(ap, fmt);
1735 sc_print_addr(reqp->xs->sc_link);
1736 printf("%r", fmt, ap);
1737 va_end(ap);
1738 }
1739
1740 /*
1741 * Prefix message with adapter info.
1742 */
1743 static void
1744 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1745 {
1746 va_list ap;
1747
1748 va_start(ap, fmt);
1749 printf("%s : %r", sc->sc_dev.dv_xname, fmt, ap);
1750 va_end(ap);
1751 }
1752 /****************************************************************************
1753 * Start Debugging Functions *
1754 ****************************************************************************/
1755 static void
1756 show_data_sense(xs)
1757 struct scsi_xfer *xs;
1758 {
1759 u_char *p1, *p2;
1760 int i;
1761 int sz;
1762
1763 p1 = (u_char *) xs->cmd;
1764 p2 = (u_char *)&xs->sense;
1765 if(*p2 == 0)
1766 return; /* No(n)sense */
1767 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
1768 for (i = 0; i < sz; i++)
1769 printf("%x ", p1[i]);
1770 printf("\nsense: ");
1771 for (i = 0; i < sizeof(xs->sense); i++)
1772 printf("%x ", p2[i]);
1773 printf("\n");
1774 }
1775
1776 static void
1777 show_request(reqp, qtxt)
1778 SC_REQ *reqp;
1779 char *qtxt;
1780 {
1781 printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x resid=%d %s\n",
1782 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1783 reqp->xcmd.opcode, reqp->status, reqp->message,
1784 reqp->xs->error, reqp->xs->resid, reqp->link ? "L":"");
1785 if (reqp->status == SCSCHKC)
1786 show_data_sense(reqp->xs);
1787 }
1788
1789 scsi_show()
1790 {
1791 SC_REQ *tmp;
1792 int sps = splhigh();
1793
1794 #ifndef DBG_PID
1795 #define last_hit ""
1796 #define olast_hit ""
1797 #endif
1798
1799 for (tmp = issue_q; tmp; tmp = tmp->next)
1800 show_request(tmp, "ISSUED");
1801 for (tmp = discon_q; tmp; tmp = tmp->next)
1802 show_request(tmp, "DISCONNECTED");
1803 if (connected)
1804 show_request(connected, "CONNECTED");
1805 printf("idstat: %x, dmstat: %x\n", GET_5380_REG(NCR5380_IDSTAT),
1806 GET_5380_REG(NCR5380_DMSTAT));
1807 /* show_signals(); */
1808 if (connected)
1809 printf("phase = %d, ", connected->phase);
1810 printf("busy:%x, last_hit:%s, olast_hit:%s spl:%04x\n", busy,
1811 last_hit, olast_hit, sps);
1812
1813 splx(sps);
1814 }
1815