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