Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / mac68k / dev / ncr5380.c
blobcdf063908eb90e33ba88940eb03117036b69c7f4
1 /* $NetBSD: ncr5380.c,v 1.62 2006/02/25 00:58:35 wiz 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.62 2006/02/25 00:58:35 wiz 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 u_char ncr_will_link = 0x00;
53 #ifdef TRY_SCSI_LINKED_COMMANDS
54 u_char ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
55 #else
56 u_char ncr_test_link = 0x7f;
57 #endif
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 *);
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 *);
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 * -- A sleeping Fujitsu M2512 is even worse; try 2.5 sec -hf 20 Jun
101 extern inline int wait_req_true(void)
103 int timeout = 2500000;
105 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
106 delay(1);
107 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
111 * Wait for request-line to become inactive. When it doesn't return 0.
112 * Otherwise return != 0.
114 extern inline int wait_req_false(void)
116 int timeout = 2500000;
118 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
119 delay(1);
120 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
123 extern inline void ack_message(void)
125 SET_5380_REG(NCR5380_ICOM, 0);
128 extern inline void nack_message(SC_REQ *reqp, u_char msg)
130 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
131 reqp->msgout = msg;
134 extern inline void finish_req(SC_REQ *reqp)
136 int sps;
137 struct scsipi_xfer *xs = reqp->xs;
139 #ifdef REAL_DMA
141 * If we bounced, free the bounce buffer
143 if (reqp->dr_flag & DRIVER_BOUNCING)
144 free_bounceb(reqp->bounceb);
145 #endif /* REAL_DMA */
146 #ifdef DBG_REQ
147 if (dbg_target_mask & (1 << reqp->targ_id))
148 show_request(reqp, "DONE");
149 #endif
150 #ifdef DBG_ERR_RET
151 if (reqp->xs->error != 0)
152 show_request(reqp, "ERR_RET");
153 #endif
155 * Return request to free-q
157 sps = splbio();
158 reqp->next = free_head;
159 free_head = reqp;
160 splx(sps);
162 xs->xs_status |= XS_STS_DONE;
163 if (!(reqp->dr_flag & DRIVER_LINKCHK))
164 scsipi_done(xs);
168 * Auto config stuff....
170 void ncr_attach(struct device *, struct device *, void *);
171 int ncr_match(struct device *, struct cfdata *, void *);
174 * Tricks to make driver-name configurable
176 #define CFNAME(n) __CONCAT(n,_cd)
177 #define CANAME(n) __CONCAT(n,_ca)
178 #define CFSTRING(n) __STRING(n)
179 #define CFDRNAME(n) n
181 CFATTACH_DECL(CFDRNAME(DRNAME), sizeof(struct ncr_softc),
182 ncr_match, ncr_attach, NULL, NULL);
184 extern struct cfdriver CFNAME(DRNAME);
187 ncr_match(struct device *parent, struct cfdata *cf, void *aux)
189 return (machine_match(parent, cf, aux, &CFNAME(DRNAME)));
192 void
193 ncr_attach(struct device *pdp, struct device *dp, void *auxp)
195 struct ncr_softc *sc;
196 int i;
198 sc = (struct ncr_softc *)dp;
200 sc->sc_adapter.adapt_dev = &sc->sc_dev;
201 sc->sc_adapter.adapt_openings = 7;
202 sc->sc_adapter.adapt_max_periph = 1;
203 sc->sc_adapter.adapt_ioctl = NULL;
204 sc->sc_adapter.adapt_minphys = ncr5380_minphys;
205 sc->sc_adapter.adapt_request = ncr5380_scsi_request;
207 sc->sc_channel.chan_adapter = &sc->sc_adapter;
208 sc->sc_channel.chan_bustype = &scsi_bustype;
209 sc->sc_channel.chan_channel = 0;
210 sc->sc_channel.chan_ntargets = 8;
211 sc->sc_channel.chan_nluns = 8;
212 sc->sc_channel.chan_id = 7;
215 * bitmasks
217 sc->sc_noselatn = 0;
218 sc->sc_selected = 0;
221 * Initialize machine-type specific things...
223 scsi_mach_init(sc);
224 printf("\n");
227 * Initialize request queue freelist.
229 for (i = 0; i < NREQ; i++) {
230 req_queue[i].next = free_head;
231 free_head = &req_queue[i];
235 * Initialize the host adapter
237 scsi_idisable();
238 ENABLE_NCR5380(sc);
239 SET_5380_REG(NCR5380_ICOM, 0);
240 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
241 SET_5380_REG(NCR5380_TCOM, 0);
242 SET_5380_REG(NCR5380_IDSTAT, 0);
243 scsi_ienable();
246 * attach all scsi units on us
248 config_found(dp, &sc->sc_channel, scsiprint);
252 * End of auto config stuff....
256 * Carry out a request from the high level driver.
258 static void
259 ncr5380_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
260 void *arg)
262 struct scsipi_xfer *xs;
263 struct scsipi_periph *periph;
264 struct ncr_softc *sc = (void *)chan->chan_adapter->adapt_dev;
265 int sps, flags;
266 SC_REQ *reqp, *link, *tmp;
268 switch (req) {
269 case ADAPTER_REQ_RUN_XFER:
270 xs = arg;
271 flags = xs->xs_control;
272 periph = xs->xs_periph;
275 * We do not queue RESET commands
277 if (flags & XS_CTL_RESET) {
278 scsi_reset_verbose(sc, "Got reset-command");
279 scsipi_done(xs);
280 return;
284 * Get a request block
286 sps = splbio();
287 if ((reqp = free_head) == 0) {
288 xs->error = XS_RESOURCE_SHORTAGE;
289 scsipi_done(xs);
290 return;
292 free_head = reqp->next;
293 reqp->next = NULL;
294 splx(sps);
297 * Initialize our private fields
299 reqp->dr_flag = (flags & XS_CTL_POLL) ? DRIVER_NOINT : 0;
300 reqp->phase = NR_PHASE;
301 reqp->msgout = MSG_NOOP;
302 reqp->status = SCSGOOD;
303 reqp->message = 0xff;
304 reqp->link = NULL;
305 reqp->xs = xs;
306 reqp->targ_id = xs->xs_periph->periph_target;
307 reqp->targ_lun = xs->xs_periph->periph_lun;
308 reqp->xdata_ptr = (u_char*)xs->data;
309 reqp->xdata_len = xs->datalen;
310 memcpy(&reqp->xcmd, xs->cmd, xs->cmdlen);
311 reqp->xcmd_len = xs->cmdlen;
312 reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
314 #ifdef REAL_DMA
316 * Check if DMA can be used on this request
318 if (scsi_dmaok(reqp))
319 reqp->dr_flag |= DRIVER_DMAOK;
320 #endif /* REAL_DMA */
323 * Insert the command into the issue queue. Note that
324 * 'REQUEST SENSE' commands are inserted at the head of the
325 * queue since any command will clear the existing contingent
326 * allegience condition and the sense data is only valid while
327 * the condition exists.
328 * When possible, link the command to a previous command to
329 * the same target. This is not very sensible when AUTO_SENSE
330 * is not defined! Interrupts are disabled while we are
331 * fiddling with the issue-queue.
333 sps = splbio();
334 link = NULL;
335 if ((issue_q == NULL) ||
336 (reqp->xcmd.opcode == SCSI_REQUEST_SENSE)) {
337 reqp->next = issue_q;
338 issue_q = reqp;
339 } else {
340 tmp = issue_q;
341 do {
342 if (!link && (tmp->targ_id == reqp->targ_id) &&
343 !tmp->link)
344 link = tmp;
345 } while (tmp->next && (tmp = tmp->next));
346 tmp->next = reqp;
347 #ifdef AUTO_SENSE
348 if (link && (ncr_will_link & (1<<reqp->targ_id))) {
349 link->link = reqp;
350 link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
352 #endif
354 #ifdef AUTO_SENSE
356 * If we haven't already, check the target for link support.
357 * Do this by prefixing the current command with a dummy
358 * Request_Sense command, link the dummy to the current
359 * command, and insert the dummy command at the head of the
360 * issue queue. Set the DRIVER_LINKCHK flag so that we'll
361 * ignore the results of the dummy command, since we only
362 * care about whether it was accepted or not.
364 if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
365 (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
366 free_head = tmp->next;
367 tmp->dr_flag =
368 (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
369 tmp->phase = NR_PHASE;
370 tmp->msgout = MSG_NOOP;
371 tmp->status = SCSGOOD;
372 tmp->xs = reqp->xs;
373 tmp->targ_id = reqp->targ_id;
374 tmp->targ_lun = reqp->targ_lun;
375 memcpy(&tmp->xcmd, sense_cmd, sizeof(sense_cmd));
376 tmp->xcmd_len = sizeof(sense_cmd);
377 tmp->xdata_ptr = (u_char *)&tmp->xs->sense.scsi_sense;
378 tmp->xdata_len = sizeof(tmp->xs->sense.scsi_sense);
379 ncr_test_link |= 1<<tmp->targ_id;
380 tmp->link = reqp;
381 tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
382 tmp->next = issue_q;
383 issue_q = tmp;
384 #ifdef DBG_REQ
385 if (dbg_target_mask & (1 << tmp->targ_id))
386 show_request(tmp, "LINKCHK");
387 #endif
389 #endif
390 splx(sps);
392 #ifdef DBG_REQ
393 if (dbg_target_mask & (1 << reqp->targ_id))
394 show_request(reqp,
395 (reqp->xcmd.opcode == SCSI_REQUEST_SENSE) ?
396 "HEAD":"TAIL");
397 #endif
399 run_main(sc);
400 return;
402 case ADAPTER_REQ_GROW_RESOURCES:
403 /* XXX Not supported. */
404 return;
406 case ADAPTER_REQ_SET_XFER_MODE:
407 /* XXX Not supported. */
408 return;
412 static void
413 ncr5380_minphys(struct buf *bp)
415 if (bp->b_bcount > MIN_PHYS)
416 bp->b_bcount = MIN_PHYS;
417 minphys(bp);
419 #undef MIN_PHYS
421 static void
422 ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
424 u_char *b = (u_char *) xs->cmd;
425 int i = 0;
427 scsipi_printaddr(xs->xs_periph);
428 if (!(xs->xs_control & XS_CTL_RESET)) {
429 while (i < xs->cmdlen) {
430 if (i)
431 printf(",");
432 printf("%x",b[i++]);
434 printf("-\n");
436 else {
437 printf("-RESET-\n");
442 * The body of the driver.
444 static void
445 scsi_main(struct ncr_softc *sc)
447 SC_REQ *req, *prev;
448 int itype;
449 int sps;
452 * While running in the driver SCSI-interrupts are disabled.
454 scsi_idisable();
455 ENABLE_NCR5380(sc);
457 PID("scsi_main1");
458 for (;;) {
459 sps = splbio();
460 if (!connected) {
462 * Check if it is fair keep any exclusive access to DMA
463 * claimed. If not, stop queueing new jobs so the discon_q
464 * will be eventually drained and DMA can be given up.
466 if (!fair_to_keep_dma())
467 goto main_exit;
470 * Search through the issue-queue for a command
471 * destined for a target that isn't busy.
473 prev = NULL;
474 for (req=issue_q; req != NULL; prev = req, req = req->next) {
475 if (!(busy & (1 << req->targ_id))) {
477 * Found one, remove it from the issue queue
479 if (prev == NULL)
480 issue_q = req->next;
481 else prev->next = req->next;
482 req->next = NULL;
483 break;
488 * When a request has just ended, we get here before an other
489 * device detects that the bus is free and that it can
490 * reconnect. The problem is that when this happens, we always
491 * baffle the device because our (initiator) id is higher. This
492 * can cause a sort of starvation on slow devices. So we check
493 * for a pending reselection here.
494 * Note that 'connected' will be non-null if the reselection
495 * succeeds.
497 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
498 == (SC_S_SEL|SC_S_IO)){
499 if (req != NULL) {
500 req->next = issue_q;
501 issue_q = req;
503 splx(sps);
505 reselect(sc);
506 scsi_clr_ipend();
507 goto connected;
511 * The host is not connected and there is no request
512 * pending, exit.
514 if (req == NULL) {
515 PID("scsi_main2");
516 goto main_exit;
520 * Re-enable interrupts before handling the request.
522 splx(sps);
524 #ifdef DBG_REQ
525 if (dbg_target_mask & (1 << req->targ_id))
526 show_request(req, "TARGET");
527 #endif
529 * We found a request. Try to connect to the target. If the
530 * initiator fails arbitration, the command is put back in the
531 * issue queue.
533 if (scsi_select(req, 0)) {
534 sps = splbio();
535 req->next = issue_q;
536 issue_q = req;
537 splx(sps);
538 #ifdef DBG_REQ
539 if (dbg_target_mask & (1 << req->targ_id))
540 ncr_tprint(req, "Select failed\n");
541 #endif
544 else splx(sps);
545 connected:
546 if (connected) {
548 * If the host is currently connected but a 'real-DMA' transfer
549 * is in progress, the 'end-of-DMA' interrupt restarts main.
550 * So quit.
552 sps = splbio();
553 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
554 PID("scsi_main3");
555 goto main_exit;
557 splx(sps);
560 * Let the target guide us through the bus-phases
562 while (information_transfer(sc) == -1)
566 /* NEVER TO REACH HERE */
567 panic("ncr5380-SCSI: not designed to come here");
569 main_exit:
571 * We enter here with interrupts disabled. We are about to exit main
572 * so interrupts should be re-enabled. Because interrupts are edge
573 * triggered, we could already have missed the interrupt. Therefore
574 * we check the IRQ-line here and re-enter when we really missed a
575 * valid interrupt.
577 PID("scsi_main4");
578 scsi_ienable();
581 * If we're not currently connected, enable reselection
582 * interrupts.
584 if (!connected)
585 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
587 if (scsi_ipending()) {
588 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
589 scsi_idisable();
590 splx(sps);
592 if (itype == INTR_RESEL)
593 reselect(sc);
594 #ifdef REAL_DMA
595 else dma_ready();
596 #else
597 else {
598 if (pdma_ready())
599 goto connected;
600 panic("Got DMA interrupt without DMA");
602 #endif
603 scsi_clr_ipend();
604 goto connected;
607 reconsider_dma();
609 main_running = 0;
610 splx(sps);
611 PID("scsi_main5");
614 #ifdef REAL_DMA
616 * The SCSI-DMA interrupt.
617 * This interrupt can only be triggered when running in non-polled DMA
618 * mode. When DMA is not active, it will be silently ignored, it is usually
619 * to late because the EOP interrupt of the controller happens just a tiny
620 * bit earlier. It might become usefull when scatter/gather is implemented,
621 * because in that case only part of the DATAIN/DATAOUT transfer is taken
622 * out of a single buffer.
624 static void
625 ncr_dma_intr(struct ncr_softc *sc)
627 SC_REQ *reqp;
628 int dma_done;
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;
637 run_main(sc);
640 #endif /* REAL_DMA */
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.
647 static void
648 ncr_ctrl_intr(struct ncr_softc *sc)
650 int itype;
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 tphase = PH_MSGOUT;
907 u_char msg = MSG_ABORT;
909 transfer_pio(&tphase, &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 DBG_INFPRINT(show_phase, reqp, phase);
985 else {
987 * Same data-phase. If same error give up
989 if ((reqp->msgout == MSG_ABORT)
990 && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
991 busy &= ~(1 << reqp->targ_id);
992 connected = NULL;
993 finish_req(reqp);
994 scsi_reset_verbose(sc, "Failure to abort command");
995 return (0);
999 switch (phase) {
1000 case PH_DATAOUT:
1001 #ifdef DBG_NOWRITE
1002 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1003 reqp->msgout = MSG_ABORT;
1004 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1005 return (-1);
1006 #endif /* DBG_NOWRITE */
1008 * If this is the first write using DMA, fill
1009 * the bounce buffer.
1011 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1012 if (reqp->dr_flag & DRIVER_BOUNCING)
1013 memcpy(reqp->bounceb, reqp->xdata_ptr, reqp->xdata_len);
1016 case PH_DATAIN:
1017 if (reqp->xdata_len <= 0) {
1019 * Target keeps requesting data. Try to get into
1020 * message-out phase by feeding/taking 100 byte.
1022 ncr_tprint(reqp, "Target requests too much data\n");
1023 reqp->msgout = MSG_ABORT;
1024 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1025 reach_msg_out(sc, 100);
1026 return (-1);
1028 #ifdef REAL_DMA
1029 if (reqp->dr_flag & DRIVER_DMAOK) {
1030 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1031 transfer_dma(reqp, phase, poll);
1032 if (!poll)
1033 return (0);
1035 else
1036 #endif
1038 PID("info_transf3");
1039 len = reqp->xdata_len;
1040 #ifdef USE_PDMA
1041 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1042 return (0);
1043 #else
1044 transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1045 #endif
1046 reqp->xdata_ptr += reqp->xdata_len - len;
1047 reqp->xdata_len = len;
1049 return (-1);
1050 case PH_MSGIN:
1052 * We only expect single byte messages here.
1054 len = 1;
1055 transfer_pio(&phase, &tmp, &len, 1);
1056 reqp->message = tmp;
1057 return (handle_message(reqp, tmp));
1058 case PH_MSGOUT:
1059 len = 1;
1060 transfer_pio(&phase, &reqp->msgout, &len, 0);
1061 if (reqp->msgout == MSG_ABORT) {
1062 busy &= ~(1 << reqp->targ_id);
1063 connected = NULL;
1064 if (!reqp->xs->error)
1065 reqp->xs->error = XS_DRIVER_STUFFUP;
1066 finish_req(reqp);
1067 PID("info_transf4");
1068 return (0);
1070 reqp->msgout = MSG_NOOP;
1071 return (-1);
1072 case PH_CMD :
1073 len = reqp->xcmd_len;
1074 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1075 PID("info_transf5");
1076 return (-1);
1077 case PH_STATUS:
1078 len = 1;
1079 transfer_pio(&phase, &tmp, &len, 0);
1080 reqp->status = tmp;
1081 PID("info_transf6");
1082 return (-1);
1083 default :
1084 ncr_tprint(reqp, "Unknown phase\n");
1086 PID("info_transf7");
1087 return (-1);
1091 * Handle the message 'msg' send to us by the target.
1092 * Return values:
1093 * 0 : The current command has completed.
1094 * -1 : Get on to the next phase.
1096 static int
1097 handle_message(SC_REQ *reqp, u_int msg)
1099 int sps;
1100 SC_REQ *prev, *req;
1102 PID("hmessage1");
1103 switch (msg) {
1105 * Linking lets us reduce the time required to get
1106 * the next command to the device, skipping the arbitration
1107 * and selection time. In the current implementation,
1108 * we merely have to start the next command pointed
1109 * to by 'next_link'.
1111 case MSG_LINK_CMD_COMPLETE:
1112 case MSG_LINK_CMD_COMPLETEF:
1113 if (reqp->link == NULL) {
1114 ncr_tprint(reqp, "No link for linked command");
1115 nack_message(reqp, MSG_ABORT);
1116 PID("hmessage2");
1117 return (-1);
1119 ack_message();
1120 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1121 reqp->xs->resid = reqp->xdata_len;
1122 reqp->xs->error = 0;
1125 #ifdef AUTO_SENSE
1126 if (check_autosense(reqp, 1) == -1)
1127 return (-1);
1128 #endif /* AUTO_SENSE */
1130 #ifdef DBG_REQ
1131 if (dbg_target_mask & (1 << reqp->targ_id))
1132 show_request(reqp->link, "LINK");
1133 #endif
1134 connected = reqp->link;
1137 * Unlink the 'linked' request from the issue_q
1139 sps = splbio();
1140 prev = NULL;
1141 req = issue_q;
1142 for (; req != NULL; prev = req, req = req->next) {
1143 if (req == connected)
1144 break;
1146 if (req == NULL)
1147 panic("Inconsistent issue_q");
1148 if (prev == NULL)
1149 issue_q = req->next;
1150 else prev->next = req->next;
1151 req->next = NULL;
1152 splx(sps);
1154 finish_req(reqp);
1155 PID("hmessage3");
1156 return (-1);
1157 case MSG_ABORT:
1158 case MSG_CMDCOMPLETE:
1159 ack_message();
1160 connected = NULL;
1161 busy &= ~(1 << reqp->targ_id);
1162 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1163 reqp->xs->resid = reqp->xdata_len;
1164 reqp->xs->error = 0;
1167 #ifdef AUTO_SENSE
1168 if (check_autosense(reqp, 0) == -1) {
1169 PID("hmessage4");
1170 return (0);
1172 #endif /* AUTO_SENSE */
1174 finish_req(reqp);
1175 PID("hmessage5");
1176 return (0);
1177 case MSG_MESSAGE_REJECT:
1178 ack_message();
1179 PID("hmessage6");
1180 return (-1);
1181 case MSG_DISCONNECT:
1182 ack_message();
1183 #ifdef DBG_REQ
1184 if (dbg_target_mask & (1 << reqp->targ_id))
1185 show_request(reqp, "DISCON");
1186 #endif
1187 sps = splbio();
1188 connected = NULL;
1189 reqp->next = discon_q;
1190 discon_q = reqp;
1191 splx(sps);
1192 PID("hmessage7");
1193 return (0);
1194 case MSG_SAVEDATAPOINTER:
1195 case MSG_RESTOREPOINTERS:
1197 * We save pointers implicitely at disconnect.
1198 * So we can ignore these messages.
1200 ack_message();
1201 PID("hmessage8");
1202 return (-1);
1203 case MSG_EXTENDED:
1204 nack_message(reqp, MSG_MESSAGE_REJECT);
1205 PID("hmessage9");
1206 return (-1);
1207 default:
1208 if ((msg & 0x80) && !(msg & 0x18)) { /* IDENTIFY */
1209 PID("hmessage10");
1210 ack_message();
1211 return (0);
1212 } else {
1213 ncr_tprint(reqp,
1214 "Unknown message %x. Rejecting.\n",
1215 msg);
1216 nack_message(reqp, MSG_MESSAGE_REJECT);
1218 return (-1);
1220 PID("hmessage11");
1221 return (-1);
1225 * Handle reselection. If a valid reconnection occurs, connected
1226 * points at the reconnected command. The command is removed from the
1227 * disconnected queue.
1229 static void
1230 reselect(struct ncr_softc *sc)
1232 u_char phase;
1233 u_long len;
1234 u_char msg;
1235 u_char target_mask;
1236 int abort = 0;
1237 SC_REQ *tmp, *prev;
1239 PID("reselect1");
1240 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1243 * At this point, we have detected that our SCSI-id is on the bus,
1244 * SEL is true and BSY was false for at least one bus settle
1245 * delay (400 ns.).
1246 * We must assert BSY ourselves, until the target drops the SEL signal.
1247 * The SCSI-spec specifies no maximum time for this, so we have to
1248 * choose something long enough to suit all targets.
1250 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1251 len = 250000;
1252 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1253 delay(1);
1254 len--;
1256 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1257 /* Damn SEL isn't dropping */
1258 scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1259 return;
1262 SET_5380_REG(NCR5380_ICOM, 0);
1265 * Check if the reselection is still valid. Check twice because
1266 * of possible line glitches - cheaper than delay(1) and we need
1267 * only a few nanoseconds.
1269 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1270 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1271 ncr_aprint(sc, "Stepped into the reselection timeout\n");
1272 return;
1277 * Get the expected identify message.
1279 phase = PH_MSGIN;
1280 len = 1;
1281 transfer_pio(&phase, &msg, &len, 0);
1282 if (len || !MSG_ISIDENTIFY(msg)) {
1283 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1284 abort = 1;
1285 tmp = NULL;
1287 else {
1289 * Find the command reconnecting
1291 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1292 if (target_mask == (1 << tmp->targ_id)) {
1293 if (prev)
1294 prev->next = tmp->next;
1295 else discon_q = tmp->next;
1296 tmp->next = NULL;
1297 break;
1300 if (tmp == NULL) {
1301 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1302 target_mask);
1303 abort = 1;
1306 if (abort) {
1307 msg = MSG_ABORT;
1308 len = 1;
1309 phase = PH_MSGOUT;
1311 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1312 if (transfer_pio(&phase, &msg, &len, 0) || len)
1313 scsi_reset_verbose(sc, "Failure to abort reselection");
1315 else {
1316 connected = tmp;
1317 #ifdef DBG_REQ
1318 if (dbg_target_mask & (1 << tmp->targ_id))
1319 show_request(tmp, "RECON");
1320 #endif
1322 PID("reselect2");
1326 * Transfer data in a given phase using programmed I/O.
1327 * Returns -1 when a different phase is entered without transferring the
1328 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1329 * phase.
1331 static int
1332 transfer_pio(u_char *phase, u_char *data, u_long *len, int dont_drop_ack)
1334 u_int cnt = *len;
1335 u_char ph = *phase;
1336 u_char tmp, new_icom;
1338 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1339 PID("tpio1");
1340 SET_5380_REG(NCR5380_TCOM, ph);
1341 do {
1342 if (!wait_req_true()) {
1343 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1344 break;
1346 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1347 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1348 break;
1350 if (PH_IN(ph)) {
1351 *data++ = GET_5380_REG(NCR5380_DATA);
1352 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1353 if ((cnt == 1) && dont_drop_ack)
1354 new_icom = SC_A_ACK;
1355 else new_icom = 0;
1357 else {
1358 SET_5380_REG(NCR5380_DATA, *data++);
1361 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1362 * the initiator should drop ATN on the last byte of the
1363 * message phase after REQ has been asserted for the handshake
1364 * but before the initiator raises ACK.
1366 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1367 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1368 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1369 new_icom = 0;
1371 else {
1372 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1373 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1374 new_icom = SC_A_ATN;
1377 if (!wait_req_false()) {
1378 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1379 break;
1381 SET_5380_REG(NCR5380_ICOM, new_icom);
1383 } while (--cnt);
1385 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1386 *phase = (tmp >> 2) & 7;
1387 else *phase = NR_PHASE;
1388 *len = cnt;
1389 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1390 *phase, cnt);
1391 PID("tpio2");
1392 if (!cnt || (*phase == ph))
1393 return (0);
1394 return (-1);
1397 #ifdef REAL_DMA
1399 * Start a DMA-transfer on the device using the current pointers.
1400 * If 'poll' is true, the function busy-waits until DMA has completed.
1402 static void
1403 transfer_dma(SC_REQ *reqp, u_int phase, int poll)
1405 int dma_done;
1406 u_char mbase = 0;
1407 int sps;
1409 again:
1410 PID("tdma1");
1413 * We should be in phase, otherwise we are not allowed to
1414 * drive the bus.
1416 SET_5380_REG(NCR5380_TCOM, phase);
1419 * Defer interrupts until DMA is fully running.
1421 sps = splbio();
1424 * Clear pending interrupts and parity errors.
1426 scsi_clr_ipend();
1428 if (!poll) {
1430 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1431 * to the interrupts we want enabled.
1433 scsi_ienable();
1434 reqp->dr_flag |= DRIVER_IN_DMA;
1435 mbase = SC_E_EOPI | SC_MON_BSY;
1437 else scsi_idisable();
1438 mbase |= IMODE_BASE | SC_M_DMA;
1439 scsi_dma_setup(reqp, phase, mbase);
1441 splx(sps);
1443 if (poll) {
1445 * On polled-DMA transfers, we wait here until the
1446 * 'end-of-DMA' condition occurs.
1448 poll_edma(reqp);
1449 if (!(dma_done = dma_ready()))
1450 goto again;
1452 PID("tdma2");
1456 * Check results of a DMA data-transfer.
1458 static int
1459 dma_ready(void)
1461 SC_REQ *reqp = connected;
1462 int dmstat, is_edma;
1463 long bytes_left, bytes_done;
1465 is_edma = get_dma_result(reqp, &bytes_left);
1466 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1469 * Check if the call is sensible and not caused by any spurious
1470 * interrupt.
1472 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1473 && (dmstat & SC_PHS_MTCH) ) {
1474 ncr_tprint(reqp, "dma_ready: spurious call "
1475 "(dm:%x,last_hit: %s)\n",
1476 #ifdef DBG_PID
1477 dmstat, last_hit[DBG_PID-1]);
1478 #else
1479 dmstat, "unknown");
1480 #endif
1481 return (0);
1485 * Clear all (pending) interrupts.
1487 scsi_clr_ipend();
1490 * Update various transfer-pointers/lengths
1492 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1494 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1496 * Copy the bytes read until now from the bounce buffer
1497 * to the 'real' destination. Flush the data-cache
1498 * before copying.
1500 PCIA();
1501 memcpy(reqp->xdata_ptr, reqp->bouncerp, bytes_done);
1502 reqp->bouncerp += bytes_done;
1505 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1506 reqp->xdata_len -= bytes_done; /* XXX */
1507 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1508 reqp->dm_cur++;
1509 else reqp->dm_cur->dm_addr += bytes_done;
1511 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1512 if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1513 ncr_tprint(reqp, "parity error in data-phase\n");
1514 reqp->xs->error = XS_TIMEOUT;
1519 * DMA mode should always be reset even when we will continue with the
1520 * next chain. It is also essential to clear the MON_BUSY because
1521 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1522 * the bus....
1524 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1527 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1528 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1531 * Tell interrupt functions DMA mode has ended.
1533 reqp->dr_flag &= ~DRIVER_IN_DMA;
1536 * Clear mode and icom
1538 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1539 SET_5380_REG(NCR5380_ICOM, 0);
1541 if (dmstat & SC_BSY_ERR) {
1542 if (!reqp->xs->error)
1543 reqp->xs->error = XS_TIMEOUT;
1544 finish_req(reqp);
1545 PID("dma_ready1");
1546 return (1);
1549 if (reqp->xs->error != 0) {
1550 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1551 reqp->msgout = MSG_ABORT;
1552 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1554 PID("dma_ready2");
1555 return (1);
1557 return (0);
1559 #endif /* REAL_DMA */
1561 static int
1562 check_autosense(SC_REQ *reqp, int linked)
1564 int sps;
1567 * If this is the driver's Link Check for this target, ignore
1568 * the results of the command. All we care about is whether we
1569 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1571 PID("linkcheck");
1572 if (reqp->dr_flag & DRIVER_LINKCHK) {
1573 if (linked)
1574 ncr_will_link |= 1<<reqp->targ_id;
1575 else ncr_tprint(reqp, "Does not support linked commands\n");
1576 return (0);
1579 * If we not executing an auto-sense and the status code
1580 * is request-sense, we automatically issue a request
1581 * sense command.
1583 PID("cautos1");
1584 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1585 switch (reqp->status & SCSMASK) {
1586 case SCSCHKC:
1587 memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1588 reqp->xcmd_len = sizeof(sense_cmd);
1589 reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
1590 reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
1591 reqp->dr_flag |= DRIVER_AUTOSEN;
1592 reqp->dr_flag &= ~DRIVER_DMAOK;
1593 if (!linked) {
1594 sps = splbio();
1595 reqp->next = issue_q;
1596 issue_q = reqp;
1597 splx(sps);
1599 else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1601 #ifdef DBG_REQ
1602 memset(reqp->xdata_ptr, 0, reqp->xdata_len);
1603 if (dbg_target_mask & (1 << reqp->targ_id))
1604 show_request(reqp, "AUTO-SENSE");
1605 #endif
1606 PID("cautos2");
1607 return (-1);
1608 case SCSBUSY:
1609 reqp->xs->error = XS_BUSY;
1610 return (0);
1613 else {
1615 * An auto-sense has finished
1617 if ((reqp->status & SCSMASK) != SCSGOOD)
1618 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1619 else reqp->xs->error = XS_SENSE;
1620 reqp->status = SCSCHKC;
1622 PID("cautos3");
1623 return (0);
1626 static int
1627 reach_msg_out(struct ncr_softc *sc, u_long len)
1629 u_char phase;
1630 u_char data;
1631 u_long n = len;
1633 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1634 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1635 phase = (phase >> 2) & 7;
1636 else return (-1);
1637 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1638 if (phase == PH_MSGOUT)
1639 return (0);
1641 SET_5380_REG(NCR5380_TCOM, phase);
1643 do {
1644 if (!wait_req_true())
1645 break;
1646 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1647 break;
1648 if (PH_IN(phase)) {
1649 data = GET_5380_REG(NCR5380_DATA);
1650 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1652 else {
1653 SET_5380_REG(NCR5380_DATA, 0);
1654 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1656 if (!wait_req_false())
1657 break;
1658 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1659 } while (--n);
1661 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1662 phase = (phase >> 2) & 7;
1663 if (phase == PH_MSGOUT) {
1664 ncr_aprint(sc, "Message-out phase reached after "
1665 "%ld bytes.\n", len - n);
1666 return (0);
1669 return (-1);
1672 void
1673 scsi_reset(void)
1675 SC_REQ *tmp, *next;
1676 int sps;
1678 PID("scsi_reset1");
1679 sps = splbio();
1680 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1681 delay(100);
1682 SET_5380_REG(NCR5380_ICOM, 0);
1683 scsi_clr_ipend();
1686 * None of the jobs in the discon_q will ever be reconnected,
1687 * notify this to the higher level code.
1689 for (tmp = discon_q; tmp ;) {
1690 next = tmp->next;
1691 tmp->next = NULL;
1692 tmp->xs->error = XS_TIMEOUT;
1693 busy &= ~(1 << tmp->targ_id);
1694 finish_req(tmp);
1695 tmp = next;
1697 discon_q = NULL;
1700 * The current job will never finish either.
1701 * The problem is that we can't finish the job because an instance
1702 * of main is running on it. Our best guess is that the job is currently
1703 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1704 * the job because it detects BSY-loss.
1706 if ((tmp = connected) != NULL) {
1707 if (tmp->dr_flag & DRIVER_IN_DMA) {
1708 tmp->xs->error = XS_DRIVER_STUFFUP;
1709 #ifdef REAL_DMA
1710 dma_ready();
1711 #endif
1714 splx(sps);
1715 PID("scsi_reset2");
1718 * Give the attached devices some time to handle the reset. This
1719 * value is arbitrary but should be relatively long.
1721 delay(100000);
1724 static void
1725 scsi_reset_verbose(struct ncr_softc *sc, const char *why)
1727 ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1729 scsi_reset();
1733 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1734 * the appropriate action is carried out (reselection or DMA ready) and
1735 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1736 * and INTR_SPURIOUS is returned.
1738 static int
1739 check_intr(struct ncr_softc *sc)
1741 SC_REQ *reqp;
1743 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1744 ==(SC_S_SEL|SC_S_IO))
1745 return (INTR_RESEL);
1746 else {
1747 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1748 reqp->dr_flag &= ~DRIVER_IN_DMA;
1749 return (INTR_DMA);
1752 scsi_clr_ipend();
1753 printf("-->");
1754 scsi_show();
1755 ncr_aprint(sc, "Spurious interrupt.\n");
1756 return (INTR_SPURIOUS);
1759 #ifdef REAL_DMA
1761 * Check if DMA can be used for this request. This function also builds
1762 * the DMA-chain.
1764 static int
1765 scsi_dmaok(SC_REQ *reqp)
1767 u_long phy_buf;
1768 u_long phy_len;
1769 void *req_addr;
1770 u_long req_len;
1771 struct dma_chain *dm;
1774 * Initialize locals and requests' DMA-chain.
1776 req_len = reqp->xdata_len;
1777 req_addr = (void*)reqp->xdata_ptr;
1778 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1779 dm->dm_count = dm->dm_addr = 0;
1780 reqp->dr_flag &= ~DRIVER_BOUNCING;
1783 * Do not accept zero length DMA.
1785 if (req_len == 0)
1786 return (0);
1789 * LWP: I think that this restriction is not strictly nessecary.
1791 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1792 return (0);
1795 * Build the DMA-chain.
1797 dm->dm_addr = phy_buf = kvtop(req_addr);
1798 while (req_len) {
1799 if (req_len <
1800 (phy_len = PAGE_SIZE - m68k_page_offset(req_addr)))
1801 phy_len = req_len;
1803 req_addr += phy_len;
1804 req_len -= phy_len;
1805 dm->dm_count += phy_len;
1807 if (req_len) {
1808 u_long tmp = kvtop(req_addr);
1810 if ((phy_buf + phy_len) != tmp) {
1811 if (wrong_dma_range(reqp, dm)) {
1812 if (reqp->dr_flag & DRIVER_BOUNCING)
1813 goto bounceit;
1814 return (0);
1817 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1818 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1819 return (0);
1821 dm->dm_count = 0;
1822 dm->dm_addr = tmp;
1824 phy_buf = tmp;
1827 if (wrong_dma_range(reqp, dm)) {
1828 if (reqp->dr_flag & DRIVER_BOUNCING)
1829 goto bounceit;
1830 return (0);
1832 reqp->dm_last = dm;
1833 return (1);
1835 bounceit:
1836 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1838 * If we can't get a bounce buffer, forget DMA
1840 reqp->dr_flag &= ~DRIVER_BOUNCING;
1841 return(0);
1844 * Initialize a single DMA-range containing the bounced request
1846 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1847 dm->dm_addr = kvtop(reqp->bounceb);
1848 dm->dm_count = reqp->xdata_len;
1849 reqp->bouncerp = reqp->bounceb;
1851 return (1);
1853 #endif /* REAL_DMA */
1855 static void
1856 run_main(struct ncr_softc *sc)
1858 int sps = splbio();
1860 if (!main_running) {
1862 * If shared resources are required, claim them
1863 * before entering 'scsi_main'. If we can't get them
1864 * now, assume 'run_main' will be called when the resource
1865 * becomes available.
1867 if (!claimed_dma()) {
1868 splx(sps);
1869 return;
1871 main_running = 1;
1872 splx(sps);
1873 scsi_main(sc);
1875 else splx(sps);
1879 * Prefix message with full target info.
1881 static void
1882 ncr_tprint(SC_REQ *reqp, const char *fmt, ...)
1884 va_list ap;
1886 va_start(ap, fmt);
1887 scsipi_printaddr(reqp->xs->xs_periph);
1888 vprintf(fmt, ap);
1889 va_end(ap);
1893 * Prefix message with adapter info.
1895 static void
1896 ncr_aprint(struct ncr_softc *sc, const char *fmt, ...)
1898 va_list ap;
1900 va_start(ap, fmt);
1901 printf("%s: ", sc->sc_dev.dv_xname);
1902 vprintf(fmt, ap);
1903 va_end(ap);
1905 /****************************************************************************
1906 * Start Debugging Functions *
1907 ****************************************************************************/
1908 static void
1909 show_data_sense(struct scsipi_xfer *xs)
1911 u_char *p1, *p2;
1912 int i;
1914 p1 = (u_char *) xs->cmd;
1915 p2 = (u_char *)&xs->sense.scsi_sense;
1916 if(*p2 == 0)
1917 return; /* No(n)sense */
1918 printf("cmd[%d]: ", xs->cmdlen);
1919 for (i = 0; i < xs->cmdlen; i++)
1920 printf("%x ", p1[i]);
1921 printf("\nsense: ");
1922 for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
1923 printf("%x ", p2[i]);
1924 printf("\n");
1927 static void
1928 show_request(SC_REQ *reqp, const char *qtxt)
1930 printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
1931 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1932 reqp->xcmd.opcode, reqp->status, reqp->message,
1933 reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
1934 reqp->link ? "L":"");
1935 if (reqp->status == SCSCHKC)
1936 show_data_sense(reqp->xs);
1939 static const char *sig_names[] = {
1940 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
1941 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
1944 static void
1945 show_signals(u_char dmstat, u_char idstat)
1947 u_short tmp, mask;
1948 int j, need_pipe;
1950 tmp = idstat | ((dmstat & 3) << 8);
1951 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
1952 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
1953 if (tmp & mask)
1954 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1956 printf("\nDma status (%02x): ", dmstat);
1957 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
1958 if (dmstat & mask)
1959 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
1961 printf("\n");
1964 void
1965 scsi_show(void)
1967 SC_REQ *tmp;
1968 int sps = splhigh();
1969 u_char idstat, dmstat;
1970 #ifdef DBG_PID
1971 int i;
1972 #endif
1974 printf("scsi_show: scsi_main is%s running\n",
1975 main_running ? "" : " not");
1976 for (tmp = issue_q; tmp; tmp = tmp->next)
1977 show_request(tmp, "ISSUED");
1978 for (tmp = discon_q; tmp; tmp = tmp->next)
1979 show_request(tmp, "DISCONNECTED");
1980 if (connected)
1981 show_request(connected, "CONNECTED");
1982 idstat = GET_5380_REG(NCR5380_IDSTAT);
1983 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1984 show_signals(dmstat, idstat);
1985 if (connected)
1986 printf("phase = %d, ", connected->phase);
1987 printf("busy:%x, spl:%04x\n", busy, sps);
1988 #ifdef DBG_PID
1989 for (i=0; i<DBG_PID; i++)
1990 printf("\t%d\t%s\n", i, last_hit[i]);
1991 #endif
1993 splx(sps);