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