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