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