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