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