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