No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / atari / dev / ncr5380.c
blob083cc20dc13bc1ad2b876c4a6dffba9dbc467c65
1 /* $NetBSD: ncr5380.c,v 1.64 2009/07/19 05:43:22 tsutsui Exp $ */
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ncr5380.c,v 1.64 2009/07/19 05:43:22 tsutsui Exp $");
32 * Bit mask of targets you want debugging to be shown
34 u_char dbg_target_mask = 0x7f;
37 * Set bit for target when parity checking must be disabled.
38 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50%
39 * of all transfers while the data is correct!?
41 u_char ncr5380_no_parchk = 0xff;
43 #ifdef AUTO_SENSE
46 * Bit masks of targets that accept linked commands, and those
47 * that we've already checked out. Some devices will report
48 * that they support linked commands when they have problems with
49 * them. By default, don't try them on any devices. Allow an
50 * option to override.
52 #ifdef TRY_SCSI_LINKED_COMMANDS
53 u_char ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
54 #else
55 u_char ncr_test_link = 0x7f;
56 #endif
57 u_char ncr_will_link = 0x00;
59 #endif /* AUTO_SENSE */
62 * This is the default sense-command we send.
64 static u_char sense_cmd[] = {
65 SCSI_REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
69 * True if the main co-routine is running
71 static volatile int main_running = 0;
74 * Mask of targets selected
76 static u_char busy;
78 static void ncr5380_minphys(struct buf *bp);
79 static void ncr5380_scsi_request(struct scsipi_channel *,
80 scsipi_adapter_req_t, void *);
81 static void ncr5380_show_scsi_cmd(struct scsipi_xfer *xs);
83 static SC_REQ req_queue[NREQ];
84 static SC_REQ *free_head = NULL; /* Free request structures */
88 * Inline functions:
92 * Wait for request-line to become active. When it doesn't return 0.
93 * Otherwise return != 0.
94 * The timeouts in the 'wait_req_*' functions are arbitrary and rather
95 * large. In 99% of the invocations nearly no timeout is needed but in
96 * some cases (especially when using my tapedrive, a Tandberg 3600) the
97 * device is busy internally and the first SCSI-phase will be delayed.
99 extern inline int wait_req_true(void)
101 int timeout = 250000;
103 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
104 delay(1);
105 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
109 * Wait for request-line to become inactive. When it doesn't return 0.
110 * Otherwise return != 0.
112 extern inline int wait_req_false(void)
114 int timeout = 250000;
116 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
117 delay(1);
118 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
121 extern inline void ack_message(void)
123 SET_5380_REG(NCR5380_ICOM, 0);
126 extern inline void nack_message(SC_REQ *reqp, u_char msg)
128 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
129 reqp->msgout = msg;
132 extern inline void finish_req(SC_REQ *reqp)
134 int sps;
135 struct scsipi_xfer *xs = reqp->xs;
137 #ifdef REAL_DMA
139 * If we bounced, free the bounce buffer
141 if (reqp->dr_flag & DRIVER_BOUNCING)
142 free_bounceb(reqp->bounceb);
143 #endif /* REAL_DMA */
144 #ifdef DBG_REQ
145 if (dbg_target_mask & (1 << reqp->targ_id))
146 show_request(reqp, "DONE");
147 #endif
148 #ifdef DBG_ERR_RET
149 if ((dbg_target_mask & (1 << reqp->targ_id)) && (reqp->xs->error != 0))
150 show_request(reqp, "ERR_RET");
151 #endif
153 * Return request to free-q
155 sps = splbio();
156 reqp->next = free_head;
157 free_head = reqp;
158 splx(sps);
160 if (!(reqp->dr_flag & DRIVER_LINKCHK))
161 scsipi_done(xs);
165 * Auto config stuff....
167 void ncr_attach(struct device *, struct device *, void *);
168 int ncr_match(struct device *, struct cfdata *, void *);
171 * Tricks to make driver-name configurable
173 #define CFNAME(n) __CONCAT(n,_cd)
174 #define CANAME(n) __CONCAT(n,_ca)
175 #define CFSTRING(n) __STRING(n)
176 #define CFDRNAME(n) n
178 CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc),
179 ncr_match, ncr_attach, NULL, NULL);
181 extern struct cfdriver CFNAME(DRNAME);
184 ncr_match(struct device *pdp, struct cfdata *cfp, void *auxp)
186 return (machine_match(pdp, cfp, auxp, &CFNAME(DRNAME)));
189 void
190 ncr_attach(struct device *pdp, struct device *dp, void *auxp)
192 struct ncr_softc *sc;
193 int i;
195 sc = (struct ncr_softc *)dp;
197 sc->sc_adapter.adapt_dev = &sc->sc_dev;
198 sc->sc_adapter.adapt_openings = 7;
199 sc->sc_adapter.adapt_max_periph = 1;
200 sc->sc_adapter.adapt_ioctl = NULL;
201 sc->sc_adapter.adapt_minphys = ncr5380_minphys;
202 sc->sc_adapter.adapt_request = ncr5380_scsi_request;
204 sc->sc_channel.chan_adapter = &sc->sc_adapter;
205 sc->sc_channel.chan_bustype = &scsi_bustype;
206 sc->sc_channel.chan_channel = 0;
207 sc->sc_channel.chan_ntargets = 8;
208 sc->sc_channel.chan_nluns = 8;
209 sc->sc_channel.chan_id = 7;
212 * bitmasks
214 sc->sc_noselatn = 0;
215 sc->sc_selected = 0;
218 * Initialize machine-type specific things...
220 scsi_mach_init(sc);
221 printf("\n");
224 * Initialize request queue freelist.
226 for (i = 0; i < NREQ; i++) {
227 req_queue[i].next = free_head;
228 free_head = &req_queue[i];
232 * Initialize the host adapter
234 scsi_idisable();
235 ENABLE_NCR5380(sc);
236 SET_5380_REG(NCR5380_ICOM, 0);
237 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
238 SET_5380_REG(NCR5380_TCOM, 0);
239 SET_5380_REG(NCR5380_IDSTAT, 0);
240 scsi_ienable();
243 * attach all scsi units on us
245 config_found(dp, &sc->sc_channel, scsiprint);
249 * End of auto config stuff....
253 * Carry out a request from the high level driver.
255 static void
256 ncr5380_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
258 struct scsipi_xfer *xs;
259 struct scsipi_periph *periph;
260 struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev;
261 int sps;
262 SC_REQ *reqp, *link, *tmp;
263 int flags;
265 switch (req) {
266 case ADAPTER_REQ_RUN_XFER:
267 xs = arg;
268 periph = xs->xs_periph;
271 * We do not queue RESET commands
273 flags = xs->xs_control;
274 if (flags & XS_CTL_RESET) {
275 scsi_reset_verbose(sc, "Got reset-command");
276 scsipi_done(xs);
277 return;
281 * Get a request block
283 sps = splbio();
284 if ((reqp = free_head) == 0) {
285 xs->error = XS_RESOURCE_SHORTAGE;
286 scsipi_done(xs);
287 splx(sps);
288 return;
290 free_head = reqp->next;
291 reqp->next = NULL;
292 splx(sps);
295 * Initialize our private fields
297 reqp->dr_flag = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0;
298 reqp->phase = NR_PHASE;
299 reqp->msgout = MSG_NOOP;
300 reqp->status = SCSGOOD;
301 reqp->message = 0xff;
302 reqp->link = NULL;
303 reqp->xs = xs;
304 reqp->targ_id = xs->xs_periph->periph_target;
305 reqp->targ_lun = xs->xs_periph->periph_lun;
306 reqp->xdata_ptr = (u_char*)xs->data;
307 reqp->xdata_len = xs->datalen;
308 memcpy(&reqp->xcmd, xs->cmd, xs->cmdlen);
309 reqp->xcmd_len = xs->cmdlen;
310 reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
312 #ifdef REAL_DMA
314 * Check if DMA can be used on this request
316 if (scsi_dmaok(reqp))
317 reqp->dr_flag |= DRIVER_DMAOK;
318 #endif /* REAL_DMA */
321 * Insert the command into the issue queue. Note that
322 * 'REQUEST SENSE' commands are inserted at the head of the
323 * queue since any command will clear the existing contingent
324 * allegience condition and the sense data is only valid while
325 * the condition exists. When possible, link the command to a
326 * previous command to the same target. This is not very
327 * sensible when AUTO_SENSE is not defined! Interrupts are
328 * disabled while we are fiddling with the issue-queue.
330 sps = splbio();
331 link = NULL;
332 if ((issue_q == NULL) ||
333 (reqp->xcmd.opcode == SCSI_REQUEST_SENSE)) {
334 reqp->next = issue_q;
335 issue_q = reqp;
337 else {
338 tmp = issue_q;
339 do {
340 if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
341 link = tmp;
342 } while (tmp->next && (tmp = tmp->next));
343 tmp->next = reqp;
344 #ifdef AUTO_SENSE
345 if (link && (ncr_will_link & (1<<reqp->targ_id))) {
346 link->link = reqp;
347 link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
349 #endif
351 #ifdef AUTO_SENSE
353 * If we haven't already, check the target for link support.
354 * Do this by prefixing the current command with a dummy
355 * Request_Sense command, link the dummy to the current
356 * command, and insert the dummy command at the head of the
357 * issue queue. Set the DRIVER_LINKCHK flag so that we'll
358 * ignore the results of the dummy command, since we only
359 * care about whether it was accepted or not.
361 if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
362 (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
363 free_head = tmp->next;
364 tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
365 tmp->phase = NR_PHASE;
366 tmp->msgout = MSG_NOOP;
367 tmp->status = SCSGOOD;
368 tmp->xs = reqp->xs;
369 tmp->targ_id = reqp->targ_id;
370 tmp->targ_lun = reqp->targ_lun;
371 memcpy(&tmp->xcmd, sense_cmd, sizeof(sense_cmd));
372 tmp->xcmd_len = sizeof(sense_cmd);
373 tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense;
374 tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense);
375 ncr_test_link |= 1<<tmp->targ_id;
376 tmp->link = reqp;
377 tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
378 tmp->next = issue_q;
379 issue_q = tmp;
380 #ifdef DBG_REQ
381 if (dbg_target_mask & (1 << tmp->targ_id))
382 show_request(tmp, "LINKCHK");
383 #endif
385 #endif
386 splx(sps);
388 #ifdef DBG_REQ
389 if (dbg_target_mask & (1 << reqp->targ_id))
390 show_request(reqp,
391 (reqp->xcmd.opcode == SCSI_REQUEST_SENSE) ?
392 "HEAD":"TAIL");
393 #endif
395 run_main(sc);
396 return;
398 case ADAPTER_REQ_GROW_RESOURCES:
399 /* XXX Not supported. */
400 return;
402 case ADAPTER_REQ_SET_XFER_MODE:
403 /* XXX Not supported. */
404 return;
408 static void
409 ncr5380_minphys(struct buf *bp)
411 if (bp->b_bcount > MIN_PHYS)
412 bp->b_bcount = MIN_PHYS;
413 minphys(bp);
415 #undef MIN_PHYS
417 static void
418 ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
420 u_char *b = (u_char *) xs->cmd;
421 int i = 0;
423 scsipi_printaddr(xs->xs_periph);
424 if (!(xs->xs_control & XS_CTL_RESET)) {
425 while (i < xs->cmdlen) {
426 if (i)
427 printf(",");
428 printf("%x",b[i++]);
430 printf("-\n");
432 else {
434 printf("-RESET-\n");
439 * The body of the driver.
441 static void
442 scsi_main(struct ncr_softc *sc)
444 SC_REQ *req, *prev;
445 int itype;
446 int sps;
449 * While running in the driver SCSI-interrupts are disabled.
451 scsi_idisable();
452 ENABLE_NCR5380(sc);
454 PID("scsi_main1");
455 for (;;) {
456 sps = splbio();
457 if (!connected) {
459 * Check if it is fair keep any exclusive access to DMA
460 * claimed. If not, stop queueing new jobs so the discon_q
461 * will be eventually drained and DMA can be given up.
463 if (!fair_to_keep_dma())
464 goto main_exit;
467 * Search through the issue-queue for a command
468 * destined for a target that isn't busy.
470 prev = NULL;
471 for (req=issue_q; req != NULL; prev = req, req = req->next) {
472 if (!(busy & (1 << req->targ_id))) {
474 * Found one, remove it from the issue queue
476 if (prev == NULL)
477 issue_q = req->next;
478 else prev->next = req->next;
479 req->next = NULL;
480 break;
485 * When a request has just ended, we get here before an other
486 * device detects that the bus is free and that it can
487 * reconnect. The problem is that when this happens, we always
488 * baffle the device because our (initiator) id is higher. This
489 * can cause a sort of starvation on slow devices. So we check
490 * for a pending reselection here.
491 * Note that 'connected' will be non-null if the reselection
492 * succeeds.
494 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
495 == (SC_S_SEL|SC_S_IO)){
496 if (req != NULL) {
497 req->next = issue_q;
498 issue_q = req;
500 splx(sps);
502 reselect(sc);
503 scsi_clr_ipend();
504 goto connected;
508 * The host is not connected and there is no request
509 * pending, exit.
511 if (req == NULL) {
512 PID("scsi_main2");
513 goto main_exit;
517 * Re-enable interrupts before handling the request.
519 splx(sps);
521 #ifdef DBG_REQ
522 if (dbg_target_mask & (1 << req->targ_id))
523 show_request(req, "TARGET");
524 #endif
526 * We found a request. Try to connect to the target. If the
527 * initiator fails arbitration, the command is put back in the
528 * issue queue.
530 if (scsi_select(req, 0)) {
531 sps = splbio();
532 req->next = issue_q;
533 issue_q = req;
534 splx(sps);
535 #ifdef DBG_REQ
536 if (dbg_target_mask & (1 << req->targ_id))
537 ncr_tprint(req, "Select failed\n");
538 #endif
541 else splx(sps);
542 connected:
543 if (connected) {
545 * If the host is currently connected but a 'real-DMA' transfer
546 * is in progress, the 'end-of-DMA' interrupt restarts main.
547 * So quit.
549 sps = splbio();
550 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
551 PID("scsi_main3");
552 goto main_exit;
554 splx(sps);
557 * Let the target guide us through the bus-phases
559 while (information_transfer(sc) == -1)
563 /* NEVER TO REACH HERE */
564 show_phase(NULL, 0); /* XXX: Get rid of not used warning */
565 panic("ncr5380-SCSI: not designed to come here");
567 main_exit:
569 * We enter here with interrupts disabled. We are about to exit main
570 * so interrupts should be re-enabled. Because interrupts are edge
571 * triggered, we could already have missed the interrupt. Therefore
572 * we check the IRQ-line here and re-enter when we really missed a
573 * valid interrupt.
575 PID("scsi_main4");
576 scsi_ienable();
579 * If we're not currently connected, enable reselection
580 * interrupts.
582 if (!connected)
583 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
585 if (scsi_ipending()) {
586 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
587 scsi_idisable();
588 scsi_clr_ipend();
589 splx(sps);
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");
601 #endif
602 goto connected;
605 reconsider_dma();
606 main_running = 0;
607 splx(sps);
608 PID("scsi_main5");
611 #ifdef REAL_DMA
613 * The SCSI-DMA interrupt.
614 * This interrupt can only be triggered when running in non-polled DMA
615 * mode. When DMA is not active, it will be silently ignored, it is usually
616 * to late because the EOP interrupt of the controller happens just a tiny
617 * bit earlier. It might become usefull when scatter/gather is implemented,
618 * because in that case only part of the DATAIN/DATAOUT transfer is taken
619 * out of a single buffer.
621 static void
622 ncr_dma_intr(struct ncr_softc *sc)
624 SC_REQ *reqp;
625 int dma_done;
627 PID("ncr_dma_intr");
628 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
629 scsi_idisable();
630 if (!(dma_done = dma_ready())) {
631 transfer_dma(reqp, reqp->phase, 0);
632 return;
634 run_main(sc);
637 #endif /* REAL_DMA */
640 * The SCSI-controller interrupt. This interrupt occurs on reselections and
641 * at the end of non-polled DMA-interrupts. It is assumed to be called from
642 * the machine-dependent hardware interrupt.
644 static void
645 ncr_ctrl_intr(struct ncr_softc *sc)
647 int itype;
649 if (main_running)
650 return; /* scsi_main() should handle this one */
652 while (scsi_ipending()) {
653 scsi_idisable();
654 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
655 if (itype == INTR_RESEL)
656 reselect(sc);
657 else {
658 #ifdef REAL_DMA
659 int dma_done;
660 if (!(dma_done = dma_ready())) {
661 transfer_dma(connected, connected->phase, 0);
662 return;
664 #else
665 if (pdma_ready())
666 return;
667 panic("Got DMA interrupt without DMA");
668 #endif
670 scsi_clr_ipend();
672 run_main(sc);
673 return;
675 PID("ncr_ctrl_intr1");
679 * Initiate a connection path between the host and the target. The function
680 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
681 * is selected and an 'IDENTIFY' message is send.
682 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
683 * the target does not respond (to either selection or 'MESSAGE OUT') the
684 * 'done' function is executed.
685 * The result code given by the driver can be influenced by setting 'code'
686 * to a non-zero value. This is the case when 'select' is called by abort.
688 static int
689 scsi_select(SC_REQ *reqp, int code)
691 u_char tmp[1];
692 u_char phase;
693 u_long cnt;
694 int sps;
695 u_int8_t atn_flag;
696 u_int8_t targ_bit;
697 struct ncr_softc *sc;
699 sc = (void*)reqp->xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
700 DBG_SELPRINT ("Starting arbitration\n", 0);
701 PID("scsi_select1");
703 sps = splbio();
706 * Prevent a race condition here. If a reslection interrupt occurred
707 * between the decision to pick a new request and the call to select,
708 * we abort the selection.
709 * Interrupts are lowered when the 5380 is setup to arbitrate for the
710 * bus.
712 if (connected) {
713 splx(sps);
714 PID("scsi_select2");
715 return (-1);
719 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
720 * selection.
722 SET_5380_REG(NCR5380_TCOM, 0);
723 SET_5380_REG(NCR5380_ICOM, 0);
726 * Arbitrate for the bus.
728 SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
729 SET_5380_REG(NCR5380_MODE, SC_ARBIT);
731 splx(sps);
733 cnt = 10;
734 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
735 delay(1);
737 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
738 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
739 SET_5380_REG(NCR5380_ICOM, 0);
740 DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
741 PID("scsi_select3");
742 return (-1);
745 /* The arbitration delay is 2.2 usecs */
746 delay(3);
749 * Check the result of the arbitration. If we failed, return -1.
751 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
752 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
753 SET_5380_REG(NCR5380_ICOM, 0);
754 PID("scsi_select4");
755 return (-1);
759 * The spec requires that we should read the data register to
760 * check for higher id's and check the SC_LA again.
762 tmp[0] = GET_5380_REG(NCR5380_DATA);
763 if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
764 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
765 SET_5380_REG(NCR5380_ICOM, 0);
766 DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
767 PID("scsi_select5");
768 return (-1);
770 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
771 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
772 SET_5380_REG(NCR5380_ICOM, 0);
773 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
774 PID("scsi_select6");
775 return (-1);
777 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
778 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
779 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
780 SET_5380_REG(NCR5380_ICOM, 0);
781 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
782 PID("scsi_select7");
783 return (-1);
785 /* Bus settle delay + Bus clear delay = 1.2 usecs */
786 delay(2);
787 DBG_SELPRINT ("Arbitration complete\n", 0);
790 * Now that we won the arbitration, start the selection.
792 targ_bit = 1 << reqp->targ_id;
793 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
795 if (sc->sc_noselatn & targ_bit)
796 atn_flag = 0;
797 else
798 atn_flag = SC_A_ATN;
801 * Raise ATN while SEL is true before BSY goes false from arbitration,
802 * since this is the only way to guarantee that we'll get a MESSAGE OUT
803 * phase immediately after the selection.
805 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
806 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
809 * Turn off reselection interrupts
811 SET_5380_REG(NCR5380_IDSTAT, 0);
814 * Reset BSY. The delay following it, surpresses a glitch in the
815 * 5380 which causes us to see our own BSY signal instead of that of
816 * the target.
818 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
819 delay(1);
822 * Wait for the target to react, the specs call for a timeout of
823 * 250 ms.
825 cnt = 25000;
826 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
827 delay(10);
829 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
831 * There is no reaction from the target, start the selection
832 * timeout procedure. We release the databus but keep SEL
833 * asserted. After that we wait a 'selection abort time' (200
834 * usecs) and 2 deskew delays (90 ns) and check BSY again.
835 * When BSY is asserted, we assume the selection succeeded,
836 * otherwise we release the bus.
838 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
839 delay(201);
840 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
841 SET_5380_REG(NCR5380_ICOM, 0);
842 reqp->xs->error = code ? code : XS_SELTIMEOUT;
843 DBG_SELPRINT ("Target %d not responding to sel\n",
844 reqp->targ_id);
845 if (reqp->dr_flag & DRIVER_LINKCHK)
846 ncr_test_link &= ~(1<<reqp->targ_id);
847 finish_req(reqp);
848 PID("scsi_select8");
849 return (0);
852 SET_5380_REG(NCR5380_ICOM, atn_flag);
854 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
857 * The SCSI-interrupts are disabled while a request is being handled.
859 scsi_idisable();
862 * If we did not request ATN, then don't try to send IDENTIFY.
864 if (atn_flag == 0) {
865 reqp->phase = PH_CMD;
866 goto identify_failed;
870 * Here we prepare to send an 'IDENTIFY' message.
871 * Allow disconnect only when interrupts are allowed.
873 tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
874 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
875 cnt = 1;
876 phase = PH_MSGOUT;
879 * Since we followed the SCSI-spec and raised ATN while SEL was true
880 * but before BSY was false during the selection, a 'MESSAGE OUT'
881 * phase should follow. Unfortunately, this does not happen on
882 * all targets (Asante ethernet devices, for example), so we must
883 * check the actual mode if the message transfer fails--if the
884 * new phase is PH_CMD and has never been successfully selected
885 * w/ATN in the past, then we assume that it is an old device
886 * that doesn't support select w/ATN.
888 if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
890 if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
891 DBG_SELPRINT ("Target %d: not responding to ATN.\n",
892 reqp->targ_id);
893 sc->sc_noselatn |= targ_bit;
894 reqp->phase = PH_CMD;
895 goto identify_failed;
898 DBG_SELPRINT ("Target %d: failed to send identify\n",
899 reqp->targ_id);
901 * Try to disconnect from the target. We cannot leave
902 * it just hanging here.
904 if (!reach_msg_out(sc, sizeof(struct scsipi_generic))) {
905 u_long len = 1;
906 u_char phse = PH_MSGOUT;
907 u_char msg = MSG_ABORT;
909 transfer_pio(&phse, &msg, &len, 0);
911 else scsi_reset_verbose(sc, "Connected to unidentified target");
913 SET_5380_REG(NCR5380_ICOM, 0);
914 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
915 finish_req(reqp);
916 PID("scsi_select9");
917 return (0);
919 reqp->phase = PH_MSGOUT;
921 identify_failed:
922 sc->sc_selected |= targ_bit;
924 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
926 * Command is connected, start timer ticking.
928 ccb_p->xtimeout = ccb_p->timeout + Lbolt;
929 #endif
931 connected = reqp;
932 busy |= targ_bit;
933 PID("scsi_select10");
934 return (0);
938 * Return codes:
939 * 0: Job has finished or disconnected, find something else
940 * -1: keep on calling information_transfer() from scsi_main()
942 static int
943 information_transfer(struct ncr_softc *sc)
945 SC_REQ *reqp = connected;
946 u_char tmp, phase;
947 u_long len;
949 PID("info_transf1");
951 * Clear pending interrupts from 5380-chip.
953 scsi_clr_ipend();
956 * The SCSI-spec requires BSY to be true while connected to a target,
957 * loosing it means we lost the target...
958 * Also REQ needs to be asserted here to indicate that the bus-phase
959 * is valid. When the target does not supply REQ within a 'reasonable'
960 * amount of time, it's probably lost in it's own maze of twisting
961 * passages, we have to reset the bus to free it.
963 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
964 wait_req_true();
965 tmp = GET_5380_REG(NCR5380_IDSTAT);
968 if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) {
969 busy &= ~(1 << reqp->targ_id);
970 connected = NULL;
971 reqp->xs->error = XS_TIMEOUT;
972 finish_req(reqp);
973 if (!(tmp & SC_S_REQ))
974 scsi_reset_verbose(sc,
975 "Timeout waiting for phase-change");
976 PID("info_transf2");
977 return (0);
980 phase = (tmp >> 2) & 7;
981 if (phase != reqp->phase) {
982 reqp->phase = phase;
983 #ifdef DBG_INF
984 if (dbg_target_mask & (1 << reqp->targ_id))
985 DBG_INFPRINT(show_phase, reqp, phase);
986 #endif
988 else {
990 * Same data-phase. If same error give up
992 if ((reqp->msgout == MSG_ABORT)
993 && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
994 busy &= ~(1 << reqp->targ_id);
995 connected = NULL;
996 reqp->xs->error = XS_TIMEOUT;
997 finish_req(reqp);
998 scsi_reset_verbose(sc, "Failure to abort command");
999 return (0);
1003 switch (phase) {
1004 case PH_DATAOUT:
1005 #ifdef DBG_NOWRITE
1006 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1007 reqp->msgout = MSG_ABORT;
1008 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1009 return (-1);
1010 #endif /* DBG_NOWRITE */
1012 * If this is the first write using DMA, fill
1013 * the bounce buffer.
1015 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1016 if (reqp->dr_flag & DRIVER_BOUNCING)
1017 memcpy(reqp->bounceb, reqp->xdata_ptr, reqp->xdata_len);
1020 case PH_DATAIN:
1021 if (reqp->xdata_len <= 0) {
1023 * Target keeps requesting data. Try to get into
1024 * message-out phase by feeding/taking 100 byte.
1026 ncr_tprint(reqp, "Target requests too much data\n");
1027 reqp->msgout = MSG_ABORT;
1028 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1029 reach_msg_out(sc, 100);
1030 return (-1);
1032 #ifdef REAL_DMA
1033 if (reqp->dr_flag & DRIVER_DMAOK) {
1034 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1035 transfer_dma(reqp, phase, poll);
1036 if (!poll)
1037 return (0);
1039 else
1040 #endif
1042 PID("info_transf3");
1043 len = reqp->xdata_len;
1044 #ifdef USE_PDMA
1045 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1046 return (0);
1047 #else
1048 transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1049 #endif
1050 reqp->xdata_ptr += reqp->xdata_len - len;
1051 reqp->xdata_len = len;
1053 return (-1);
1054 case PH_MSGIN:
1056 * We only expect single byte messages here.
1058 len = 1;
1059 transfer_pio(&phase, &tmp, &len, 1);
1060 reqp->message = tmp;
1061 return (handle_message(reqp, tmp));
1062 case PH_MSGOUT:
1063 len = 1;
1064 transfer_pio(&phase, &reqp->msgout, &len, 0);
1065 if (reqp->msgout == MSG_ABORT) {
1066 busy &= ~(1 << reqp->targ_id);
1067 connected = NULL;
1068 if (!reqp->xs->error)
1069 reqp->xs->error = XS_DRIVER_STUFFUP;
1070 finish_req(reqp);
1071 PID("info_transf4");
1072 return (0);
1074 reqp->msgout = MSG_NOOP;
1075 return (-1);
1076 case PH_CMD :
1077 len = reqp->xcmd_len;
1078 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1079 PID("info_transf5");
1080 return (-1);
1081 case PH_STATUS:
1082 len = 1;
1083 transfer_pio(&phase, &tmp, &len, 0);
1084 reqp->status = tmp;
1085 PID("info_transf6");
1086 return (-1);
1087 default :
1088 ncr_tprint(reqp, "Unknown phase\n");
1090 PID("info_transf7");
1091 return (-1);
1095 * Handle the message 'msg' send to us by the target.
1096 * Return values:
1097 * 0 : The current command has completed.
1098 * -1 : Get on to the next phase.
1100 static int
1101 handle_message(SC_REQ *reqp, u_int msg)
1103 int sps;
1104 SC_REQ *prev, *req;
1106 PID("hmessage1");
1107 switch (msg) {
1109 * Linking lets us reduce the time required to get
1110 * the next command to the device, skipping the arbitration
1111 * and selection time. In the current implementation,
1112 * we merely have to start the next command pointed
1113 * to by 'next_link'.
1115 case MSG_LINK_CMD_COMPLETE:
1116 case MSG_LINK_CMD_COMPLETEF:
1117 if (reqp->link == NULL) {
1118 ncr_tprint(reqp, "No link for linked command");
1119 nack_message(reqp, MSG_ABORT);
1120 PID("hmessage2");
1121 return (-1);
1123 ack_message();
1124 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1125 reqp->xs->resid = reqp->xdata_len;
1126 reqp->xs->error = 0;
1129 #ifdef AUTO_SENSE
1130 if (check_autosense(reqp, 1) == -1)
1131 return (-1);
1132 #endif /* AUTO_SENSE */
1134 #ifdef DBG_REQ
1135 if (dbg_target_mask & (1 << reqp->targ_id))
1136 show_request(reqp->link, "LINK");
1137 #endif
1138 connected = reqp->link;
1141 * Unlink the 'linked' request from the issue_q
1143 sps = splbio();
1144 prev = NULL;
1145 req = issue_q;
1146 for (; req != NULL; prev = req, req = req->next) {
1147 if (req == connected)
1148 break;
1150 if (req == NULL)
1151 panic("Inconsistent issue_q");
1152 if (prev == NULL)
1153 issue_q = req->next;
1154 else prev->next = req->next;
1155 req->next = NULL;
1156 splx(sps);
1158 finish_req(reqp);
1159 PID("hmessage3");
1160 return (-1);
1161 case MSG_ABORT:
1162 case MSG_CMDCOMPLETE:
1163 ack_message();
1164 connected = NULL;
1165 busy &= ~(1 << reqp->targ_id);
1166 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1167 reqp->xs->resid = reqp->xdata_len;
1168 reqp->xs->error = 0;
1171 #ifdef AUTO_SENSE
1172 if (check_autosense(reqp, 0) == -1) {
1173 PID("hmessage4");
1174 return (0);
1176 #endif /* AUTO_SENSE */
1178 finish_req(reqp);
1179 PID("hmessage5");
1180 return (0);
1181 case MSG_MESSAGE_REJECT:
1182 ack_message();
1183 PID("hmessage6");
1184 return (-1);
1185 case MSG_DISCONNECT:
1186 ack_message();
1187 #ifdef DBG_REQ
1188 if (dbg_target_mask & (1 << reqp->targ_id))
1189 show_request(reqp, "DISCON");
1190 #endif
1191 sps = splbio();
1192 connected = NULL;
1193 reqp->next = discon_q;
1194 discon_q = reqp;
1195 splx(sps);
1196 PID("hmessage7");
1197 return (0);
1198 case MSG_SAVEDATAPOINTER:
1199 case MSG_RESTOREPOINTERS:
1201 * We save pointers implicitely at disconnect.
1202 * So we can ignore these messages.
1204 ack_message();
1205 PID("hmessage8");
1206 return (-1);
1207 case MSG_EXTENDED:
1208 nack_message(reqp, MSG_MESSAGE_REJECT);
1209 PID("hmessage9");
1210 return (-1);
1211 default:
1212 if ((msg & 0x80) && !(msg & 0x18)) { /* IDENTIFY */
1213 PID("hmessage10");
1214 ack_message();
1215 return (0);
1216 } else {
1217 ncr_tprint(reqp,
1218 "Unknown message %x. Rejecting.\n",
1219 msg);
1220 nack_message(reqp, MSG_MESSAGE_REJECT);
1222 return (-1);
1224 PID("hmessage11");
1225 return (-1);
1229 * Handle reselection. If a valid reconnection occurs, connected
1230 * points at the reconnected command. The command is removed from the
1231 * disconnected queue.
1233 static void
1234 reselect(struct ncr_softc *sc)
1236 u_char phase;
1237 u_long len;
1238 u_char msg;
1239 u_char target_mask;
1240 int abort = 0;
1241 SC_REQ *tmp, *prev;
1243 PID("reselect1");
1244 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1247 * At this point, we have detected that our SCSI-id is on the bus,
1248 * SEL is true and BSY was false for at least one bus settle
1249 * delay (400 ns.).
1250 * We must assert BSY ourselves, until the target drops the SEL signal.
1251 * The SCSI-spec specifies no maximum time for this, so we have to
1252 * choose something long enough to suit all targets.
1254 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1255 len = 250000;
1256 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1257 delay(1);
1258 len--;
1260 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1261 /* Damn SEL isn't dropping */
1262 scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1263 return;
1266 SET_5380_REG(NCR5380_ICOM, 0);
1269 * Check if the reselection is still valid. Check twice because
1270 * of possible line glitches - cheaper than delay(1) and we need
1271 * only a few nanoseconds.
1273 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1274 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1275 ncr_aprint(sc, "Stepped into the reselection timeout\n");
1276 return;
1281 * Get the expected identify message.
1283 phase = PH_MSGIN;
1284 len = 1;
1285 transfer_pio(&phase, &msg, &len, 0);
1286 if (len || !MSG_ISIDENTIFY(msg)) {
1287 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1288 abort = 1;
1289 tmp = NULL;
1291 else {
1293 * Find the command reconnecting
1295 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1296 if (target_mask == (1 << tmp->targ_id)) {
1297 if (prev)
1298 prev->next = tmp->next;
1299 else discon_q = tmp->next;
1300 tmp->next = NULL;
1301 break;
1304 if (tmp == NULL) {
1305 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1306 target_mask);
1307 abort = 1;
1310 if (abort) {
1311 msg = MSG_ABORT;
1312 len = 1;
1313 phase = PH_MSGOUT;
1315 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1316 if (transfer_pio(&phase, &msg, &len, 0) || len)
1317 scsi_reset_verbose(sc, "Failure to abort reselection");
1319 else {
1320 connected = tmp;
1321 #ifdef DBG_REQ
1322 if (dbg_target_mask & (1 << tmp->targ_id))
1323 show_request(tmp, "RECON");
1324 #endif
1326 PID("reselect2");
1330 * Transfer data in a given phase using programmed I/O.
1331 * Returns -1 when a different phase is entered without transferring the
1332 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1333 * phase.
1335 static int
1336 transfer_pio(u_char *phase, u_char *data, u_long *len, int dont_drop_ack)
1338 u_int cnt = *len;
1339 u_char ph = *phase;
1340 u_char tmp, new_icom;
1342 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1343 PID("tpio1");
1344 SET_5380_REG(NCR5380_TCOM, ph);
1345 do {
1346 if (!wait_req_true()) {
1347 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1348 break;
1350 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1351 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1352 break;
1354 if (PH_IN(ph)) {
1355 *data++ = GET_5380_REG(NCR5380_DATA);
1356 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1357 if ((cnt == 1) && dont_drop_ack)
1358 new_icom = SC_A_ACK;
1359 else new_icom = 0;
1361 else {
1362 SET_5380_REG(NCR5380_DATA, *data++);
1365 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1366 * the initiator should drop ATN on the last byte of the
1367 * message phase after REQ has been asserted for the handshake
1368 * but before the initiator raises ACK.
1370 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1371 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1372 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1373 new_icom = 0;
1375 else {
1376 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1377 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1378 new_icom = SC_A_ATN;
1381 if (!wait_req_false()) {
1382 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1383 break;
1385 SET_5380_REG(NCR5380_ICOM, new_icom);
1387 } while (--cnt);
1389 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1390 *phase = (tmp >> 2) & 7;
1391 else *phase = NR_PHASE;
1392 *len = cnt;
1393 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1394 *phase, cnt);
1395 PID("tpio2");
1396 if (!cnt || (*phase == ph))
1397 return (0);
1398 return (-1);
1401 #ifdef REAL_DMA
1404 * Start a DMA-transfer on the device using the current pointers.
1405 * If 'poll' is true, the function busy-waits until DMA has completed.
1407 static void
1408 transfer_dma(SC_REQ *reqp, u_int phase, int poll)
1410 int dma_done;
1411 u_char mbase = 0;
1412 int sps;
1414 again:
1415 PID("tdma1");
1418 * We should be in phase, otherwise we are not allowed to
1419 * drive the bus.
1421 SET_5380_REG(NCR5380_TCOM, phase);
1424 * Defer interrupts until DMA is fully running.
1426 sps = splbio();
1429 * Clear pending interrupts and parity errors.
1431 scsi_clr_ipend();
1433 if (!poll) {
1435 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1436 * to the interrupts we want enabled.
1438 scsi_ienable();
1439 reqp->dr_flag |= DRIVER_IN_DMA;
1440 mbase = SC_E_EOPI | SC_MON_BSY;
1442 else scsi_idisable();
1443 mbase |= IMODE_BASE | SC_M_DMA;
1444 scsi_dma_setup(reqp, phase, mbase);
1446 splx(sps);
1448 if (poll) {
1450 * On polled-DMA transfers, we wait here until the
1451 * 'end-of-DMA' condition occurs.
1453 poll_edma(reqp);
1454 if (!(dma_done = dma_ready()))
1455 goto again;
1457 PID("tdma2");
1461 * Check results of a DMA data-transfer.
1463 static int
1464 dma_ready(void)
1466 SC_REQ *reqp = connected;
1467 int dmstat, is_edma;
1468 long bytes_left, bytes_done;
1470 is_edma = get_dma_result(reqp, &bytes_left);
1471 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1474 * Check if the call is sensible and not caused by any spurious
1475 * interrupt.
1477 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1478 && (dmstat & SC_PHS_MTCH) ) {
1479 ncr_tprint(reqp, "dma_ready: spurious call "
1480 "(dm:%x,last_hit: %s)\n",
1481 #ifdef DBG_PID
1482 dmstat, last_hit[DBG_PID-1]);
1483 #else
1484 dmstat, "unknown");
1485 #endif
1486 return (0);
1490 * Clear all (pending) interrupts.
1492 scsi_clr_ipend();
1495 * Update various transfer-pointers/lengths
1497 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1499 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1501 * Copy the bytes read until now from the bounce buffer
1502 * to the 'real' destination. Flush the data-cache
1503 * before copying.
1505 PCIA();
1506 memcpy(reqp->xdata_ptr, reqp->bouncerp, bytes_done);
1507 reqp->bouncerp += bytes_done;
1510 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1511 reqp->xdata_len -= bytes_done; /* XXX */
1512 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1513 reqp->dm_cur++;
1514 else reqp->dm_cur->dm_addr += bytes_done;
1516 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1517 if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1518 ncr_tprint(reqp, "parity error in data-phase\n");
1519 reqp->xs->error = XS_TIMEOUT;
1524 * DMA mode should always be reset even when we will continue with the
1525 * next chain. It is also essential to clear the MON_BUSY because
1526 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1527 * the bus....
1529 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1532 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1533 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1536 * Tell interrupt functions DMA mode has ended.
1538 reqp->dr_flag &= ~DRIVER_IN_DMA;
1541 * Clear mode and icom
1543 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1544 SET_5380_REG(NCR5380_ICOM, 0);
1546 if (dmstat & SC_BSY_ERR) {
1547 if (!reqp->xs->error)
1548 reqp->xs->error = XS_TIMEOUT;
1549 finish_req(reqp);
1550 PID("dma_ready1");
1551 return (1);
1554 if (reqp->xs->error != 0) {
1555 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1556 reqp->msgout = MSG_ABORT;
1557 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1559 PID("dma_ready2");
1560 return (1);
1562 return (0);
1564 #endif /* REAL_DMA */
1566 static int
1567 check_autosense(SC_REQ *reqp, int linked)
1569 int sps;
1572 * If this is the driver's Link Check for this target, ignore
1573 * the results of the command. All we care about is whether we
1574 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1576 PID("linkcheck");
1577 if (reqp->dr_flag & DRIVER_LINKCHK) {
1578 if (linked)
1579 ncr_will_link |= 1<<reqp->targ_id;
1580 else ncr_tprint(reqp, "Does not support linked commands\n");
1581 return (0);
1584 * If we not executing an auto-sense and the status code
1585 * is request-sense, we automatically issue a request
1586 * sense command.
1588 PID("cautos1");
1589 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1590 switch (reqp->status & SCSMASK) {
1591 case SCSCHKC:
1592 memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1593 reqp->xcmd_len = sizeof(sense_cmd);
1594 reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
1595 reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
1596 reqp->dr_flag |= DRIVER_AUTOSEN;
1597 reqp->dr_flag &= ~DRIVER_DMAOK;
1598 if (!linked) {
1599 sps = splbio();
1600 reqp->next = issue_q;
1601 issue_q = reqp;
1602 splx(sps);
1604 else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1606 #ifdef DBG_REQ
1607 memset(reqp->xdata_ptr, 0, reqp->xdata_len);
1608 if (dbg_target_mask & (1 << reqp->targ_id))
1609 show_request(reqp, "AUTO-SENSE");
1610 #endif
1611 PID("cautos2");
1612 return (-1);
1613 case SCSBUSY:
1614 reqp->xs->error = XS_BUSY;
1615 return (0);
1618 else {
1620 * An auto-sense has finished
1622 if ((reqp->status & SCSMASK) != SCSGOOD)
1623 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1624 else reqp->xs->error = XS_SENSE;
1625 reqp->status = SCSCHKC;
1627 PID("cautos3");
1628 return (0);
1631 static int
1632 reach_msg_out(struct ncr_softc *sc, u_long len)
1634 u_char phase;
1635 u_char data;
1636 u_long n = len;
1638 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1639 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1640 phase = (phase >> 2) & 7;
1641 else return (-1);
1642 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1643 if (phase == PH_MSGOUT)
1644 return (0);
1646 SET_5380_REG(NCR5380_TCOM, phase);
1648 do {
1649 if (!wait_req_true())
1650 break;
1651 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1652 break;
1653 if (PH_IN(phase)) {
1654 data = GET_5380_REG(NCR5380_DATA);
1655 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1657 else {
1658 SET_5380_REG(NCR5380_DATA, 0);
1659 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN);
1660 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1662 if (!wait_req_false())
1663 break;
1664 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1665 } while (--n);
1667 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1668 phase = (phase >> 2) & 7;
1669 if (phase == PH_MSGOUT) {
1670 ncr_aprint(sc, "Message-out phase reached after "
1671 "%ld bytes.\n", len - n);
1672 return (0);
1674 ncr_aprint(sc, "Phase now: %d after %ld bytes.\n", phase,
1675 len - n);
1678 return (-1);
1681 void
1682 scsi_reset(void)
1684 SC_REQ *tmp, *next;
1685 int sps;
1687 PID("scsi_reset1");
1688 sps = splbio();
1689 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1690 delay(100);
1691 SET_5380_REG(NCR5380_ICOM, 0);
1692 scsi_clr_ipend();
1695 * None of the jobs in the discon_q will ever be reconnected,
1696 * notify this to the higher level code.
1698 for (tmp = discon_q; tmp ;) {
1699 next = tmp->next;
1700 tmp->next = NULL;
1701 tmp->xs->error = XS_TIMEOUT;
1702 busy &= ~(1 << tmp->targ_id);
1703 finish_req(tmp);
1704 tmp = next;
1706 discon_q = NULL;
1709 * The current job will never finish either.
1710 * The problem is that we can't finish the job because an instance
1711 * of main is running on it. Our best guess is that the job is currently
1712 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1713 * the job because it detects BSY-loss.
1715 if ((tmp = connected) != NULL) {
1716 if (tmp->dr_flag & DRIVER_IN_DMA) {
1717 tmp->xs->error = XS_DRIVER_STUFFUP;
1718 #ifdef REAL_DMA
1719 dma_ready();
1720 #endif
1723 splx(sps);
1724 PID("scsi_reset2");
1727 * Give the attached devices some time to handle the reset. This
1728 * value is arbitrary but should be relatively long.
1730 delay(100000);
1733 static void
1734 scsi_reset_verbose(struct ncr_softc *sc, const char *why)
1736 ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1738 scsi_reset();
1742 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1743 * the appropriate action is carried out (reselection or DMA ready) and
1744 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1745 * and INTR_SPURIOUS is returned.
1747 static int
1748 check_intr(struct ncr_softc *sc)
1750 SC_REQ *reqp;
1752 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1753 ==(SC_S_SEL|SC_S_IO))
1754 return (INTR_RESEL);
1755 else {
1756 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1757 reqp->dr_flag &= ~DRIVER_IN_DMA;
1758 return (INTR_DMA);
1761 printf("-->");
1762 scsi_show();
1763 scsi_clr_ipend();
1764 ncr_aprint(sc, "Spurious interrupt.\n");
1765 return (INTR_SPURIOUS);
1768 #ifdef REAL_DMA
1770 * Check if DMA can be used for this request. This function also builds
1771 * the DMA-chain.
1773 static int
1774 scsi_dmaok(SC_REQ *reqp)
1776 u_long phy_buf;
1777 u_long phy_len;
1778 char *req_addr;
1779 u_long req_len;
1780 struct dma_chain *dm;
1783 * To be safe, do not use DMA for Falcon
1785 if (machineid & ATARI_FALCON)
1786 return (0);
1789 * Initialize locals and requests' DMA-chain.
1791 req_len = reqp->xdata_len;
1792 req_addr = (void *)reqp->xdata_ptr;
1793 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1794 dm->dm_count = dm->dm_addr = 0;
1795 reqp->dr_flag &= ~DRIVER_BOUNCING;
1798 * Do not accept zero length DMA.
1800 if (req_len == 0)
1801 return (0);
1804 * If DMA is emulated in software, we don't have to breakup the
1805 * request. Just build a chain with a single element and stash in
1806 * the KVA and not the KPA.
1808 if (emulated_dma()) {
1809 dm->dm_addr = (u_long)req_addr;
1810 dm->dm_count = req_len;
1811 return (1);
1815 * LWP: I think that this restriction is not strictly nessecary.
1817 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1818 return (0);
1821 * Build the DMA-chain.
1823 dm->dm_addr = phy_buf = kvtop(req_addr);
1824 while (req_len) {
1825 if (req_len <
1826 (phy_len = PAGE_SIZE - ((u_long)req_addr & PGOFSET)))
1827 phy_len = req_len;
1829 req_addr += phy_len;
1830 req_len -= phy_len;
1831 dm->dm_count += phy_len;
1833 if (req_len) {
1834 u_long tmp = kvtop(req_addr);
1836 if ((phy_buf + phy_len) != tmp) {
1837 if (wrong_dma_range(reqp, dm)) {
1838 if (reqp->dr_flag & DRIVER_BOUNCING)
1839 goto bounceit;
1840 return (0);
1843 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1844 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1845 return (0);
1847 dm->dm_count = 0;
1848 dm->dm_addr = tmp;
1850 phy_buf = tmp;
1853 if (wrong_dma_range(reqp, dm)) {
1854 if (reqp->dr_flag & DRIVER_BOUNCING)
1855 goto bounceit;
1856 return (0);
1858 reqp->dm_last = dm;
1859 return (1);
1861 bounceit:
1862 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1864 * If we can't get a bounce buffer, forget DMA
1866 reqp->dr_flag &= ~DRIVER_BOUNCING;
1867 return(0);
1870 * Initialize a single DMA-range containing the bounced request
1872 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1873 dm->dm_addr = kvtop(reqp->bounceb);
1874 dm->dm_count = reqp->xdata_len;
1875 reqp->bouncerp = reqp->bounceb;
1877 return (1);
1879 #endif /* REAL_DMA */
1881 static void
1882 run_main(struct ncr_softc *sc)
1884 int sps = splbio();
1886 if (!main_running) {
1888 * If shared resources are required, claim them
1889 * before entering 'scsi_main'. If we can't get them
1890 * now, assume 'run_main' will be called when the resource
1891 * becomes available.
1893 if (!claimed_dma()) {
1894 splx(sps);
1895 return;
1897 main_running = 1;
1898 splx(sps);
1899 scsi_main(sc);
1901 else splx(sps);
1905 * Prefix message with full target info.
1907 static void
1908 ncr_tprint(SC_REQ *reqp, const char *fmt, ...)
1910 va_list ap;
1912 va_start(ap, fmt);
1913 scsipi_printaddr(reqp->xs->xs_periph);
1914 vprintf(fmt, ap);
1915 va_end(ap);
1919 * Prefix message with adapter info.
1921 static void
1922 ncr_aprint(struct ncr_softc *sc, const char *fmt, ...)
1924 va_list ap;
1925 char buf[256];
1927 va_start(ap, fmt);
1928 vsnprintf(buf, sizeof(buf), fmt, ap);
1929 va_end(ap);
1931 printf("%s: %s", sc->sc_dev.dv_xname, buf);
1933 /****************************************************************************
1934 * Start Debugging Functions *
1935 ****************************************************************************/
1936 static const char *phase_names[] = {
1937 "DATA_OUT", "DATA_IN", "COMMAND", "STATUS", "NONE", "NONE", "MSG_OUT",
1938 "MSG_IN"
1941 static void
1942 show_phase(SC_REQ *reqp, int phase)
1944 printf("INFTRANS: %d Phase = %s\n", reqp->targ_id, phase_names[phase]);
1947 static void
1948 show_data_sense(struct scsipi_xfer *xs)
1950 u_char *p1, *p2;
1951 int i;
1953 p1 = (u_char *) xs->cmd;
1954 p2 = (u_char *)&xs->sense.scsi_sense;
1955 if(*p2 == 0)
1956 return; /* No(n)sense */
1957 printf("cmd[%d]: ", xs->cmdlen);
1958 for (i = 0; i < xs->cmdlen; i++)
1959 printf("%x ", p1[i]);
1960 printf("\nsense: ");
1961 for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
1962 printf("%x ", p2[i]);
1963 printf("\n");
1966 static void
1967 show_request(SC_REQ *reqp, const char *qtxt)
1969 printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
1970 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1971 reqp->xcmd.opcode, reqp->status, reqp->message,
1972 reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
1973 reqp->link ? "L":"");
1974 if (reqp->status == SCSCHKC)
1975 show_data_sense(reqp->xs);
1978 static const char *sig_names[] = {
1979 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1980 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1983 static void
1984 show_signals(u_char dmstat, u_char idstat)
1986 u_short tmp, mask;
1987 int j, need_pipe;
1989 tmp = idstat | ((dmstat & 3) << 8);
1990 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1991 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1992 if (tmp & mask)
1993 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1995 printf("\nDma status (%02x): ", dmstat);
1996 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
1997 if (dmstat & mask)
1998 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
2000 printf("\n");
2003 void
2004 scsi_show(void)
2006 SC_REQ *tmp;
2007 int sps = splhigh();
2008 u_char idstat, dmstat;
2009 int i;
2011 printf("scsi_show: scsi_main is%s running\n",
2012 main_running ? "" : " not");
2013 for (tmp = issue_q; tmp; tmp = tmp->next)
2014 show_request(tmp, "ISSUED");
2015 for (tmp = discon_q; tmp; tmp = tmp->next)
2016 show_request(tmp, "DISCONNECTED");
2017 if (connected)
2018 show_request(connected, "CONNECTED");
2019 if (can_access_5380()) {
2020 idstat = GET_5380_REG(NCR5380_IDSTAT);
2021 dmstat = GET_5380_REG(NCR5380_DMSTAT);
2022 show_signals(dmstat, idstat);
2025 if (connected)
2026 printf("phase = %d, ", connected->phase);
2027 printf("busy:%x, spl:%04x\n", busy, sps);
2028 #ifdef DBG_PID
2029 for (i=0; i<DBG_PID; i++)
2030 printf("\t%d\t%s\n", i, last_hit[i]);
2031 #endif
2033 splx(sps);