ncr5380.c revision 1.29.8.1 1 /* $NetBSD: ncr5380.c,v 1.29.8.1 1997/07/01 17:33:53 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Bit mask of targets you want debugging to be shown
35 */
36 u_char dbg_target_mask = 0x7f;
37
38 /*
39 * Set bit for target when parity checking must be disabled.
40 * My (LWP) Maxtor 7245S seems to generate parity errors on about 50%
41 * of all transfers while the data is correct!?
42 */
43 u_char ncr5380_no_parchk = 0xff;
44
45 #ifdef AUTO_SENSE
46
47 /*
48 * Bit masks of targets that accept linked commands, and those
49 * that we've already checked out. Some devices will report
50 * that they support linked commands when they have problems with
51 * them. By default, don't try them on any devices. Allow an
52 * option to override.
53 */
54 #ifdef TRY_SCSI_LINKED_COMMANDS
55 u_char ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
56 #else
57 u_char ncr_test_link = 0x7f;
58 #endif
59 u_char ncr_will_link = 0x00;
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 extern __inline__ int wait_req_true(void)
134 {
135 int timeout = 250000;
136
137 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
138 delay(1);
139 return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
140 }
141
142 /*
143 * Wait for request-line to become inactive. When it doesn't return 0.
144 * Otherwise return != 0.
145 */
146 extern __inline__ int wait_req_false(void)
147 {
148 int timeout = 250000;
149
150 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
151 delay(1);
152 return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
153 }
154
155 extern __inline__ void ack_message()
156 {
157 SET_5380_REG(NCR5380_ICOM, 0);
158 }
159
160 extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
161 {
162 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
163 reqp->msgout = msg;
164 }
165
166 extern __inline__ void finish_req(SC_REQ *reqp)
167 {
168 int sps;
169 struct scsipi_xfer *xs = reqp->xs;
170
171 #ifdef REAL_DMA
172 /*
173 * If we bounced, free the bounce buffer
174 */
175 if (reqp->dr_flag & DRIVER_BOUNCING)
176 free_bounceb(reqp->bounceb);
177 #endif /* REAL_DMA */
178 #ifdef DBG_REQ
179 if (dbg_target_mask & (1 << reqp->targ_id))
180 show_request(reqp, "DONE");
181 #endif
182 #ifdef DBG_ERR_RET
183 if ((dbg_target_mask & (1 << reqp->targ_id)) && (reqp->xs->error != 0))
184 show_request(reqp, "ERR_RET");
185 #endif
186 /*
187 * Return request to free-q
188 */
189 sps = splbio();
190 reqp->next = free_head;
191 free_head = reqp;
192 splx(sps);
193
194 xs->flags |= ITSDONE;
195 if (!(reqp->dr_flag & DRIVER_LINKCHK))
196 scsipi_done(xs);
197 }
198
199 /*
200 * Auto config stuff....
201 */
202 void ncr_attach __P((struct device *, struct device *, void *));
203 int ncr_match __P((struct device *, struct cfdata *, void *));
204
205 /*
206 * Tricks to make driver-name configurable
207 */
208 #define CFNAME(n) __CONCAT(n,_cd)
209 #define CANAME(n) __CONCAT(n,_ca)
210 #define CFSTRING(n) __STRING(n)
211
212 struct cfattach CANAME(DRNAME) = {
213 sizeof(struct ncr_softc), ncr_match, ncr_attach
214 };
215
216 struct cfdriver CFNAME(DRNAME) = {
217 NULL, CFSTRING(DRNAME), DV_DULL, NULL, 0
218 };
219
220 int
221 ncr_match(pdp, cfp, auxp)
222 struct device *pdp;
223 struct cfdata *cfp;
224 void *auxp;
225 {
226 return (machine_match(pdp, cfp, auxp, &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 show_phase(NULL, 0); /* XXX: Get rid of not used warning */
596 panic("ncr5380-SCSI: not designed to come here");
597
598 main_exit:
599 /*
600 * We enter here with interrupts disabled. We are about to exit main
601 * so interrupts should be re-enabled. Because interrupts are edge
602 * triggered, we could already have missed the interrupt. Therefore
603 * we check the IRQ-line here and re-enter when we really missed a
604 * valid interrupt.
605 */
606 PID("scsi_main4");
607 scsi_ienable();
608
609 /*
610 * If we're not currently connected, enable reselection
611 * interrupts.
612 */
613 if (!connected)
614 SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
615
616 if (scsi_ipending()) {
617 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
618 scsi_idisable();
619 scsi_clr_ipend();
620 splx(sps);
621
622 if (itype == INTR_RESEL)
623 reselect(sc);
624 #ifdef REAL_DMA
625 else dma_ready();
626 #else
627 else {
628 if (pdma_ready())
629 goto connected;
630 panic("Got DMA interrupt without DMA");
631 }
632 #endif
633 goto connected;
634 }
635 }
636 reconsider_dma();
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 if (main_running)
683 return; /* scsi_main() should handle this one */
684
685 while (scsi_ipending()) {
686 scsi_idisable();
687 if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
688 if (itype == INTR_RESEL)
689 reselect(sc);
690 else {
691 #ifdef REAL_DMA
692 int dma_done;
693 if (!(dma_done = dma_ready())) {
694 transfer_dma(connected, connected->phase, 0);
695 return;
696 }
697 #else
698 if (pdma_ready())
699 return;
700 panic("Got DMA interrupt without DMA\n");
701 #endif
702 }
703 scsi_clr_ipend();
704 }
705 run_main(sc);
706 return;
707 }
708 PID("ncr_ctrl_intr1");
709 }
710
711 /*
712 * Initiate a connection path between the host and the target. The function
713 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
714 * is selected and an 'IDENTIFY' message is send.
715 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
716 * the target does not respond (to either selection or 'MESSAGE OUT') the
717 * 'done' function is executed.
718 * The result code given by the driver can be influenced by setting 'code'
719 * to a non-zero value. This is the case when 'select' is called by abort.
720 */
721 static int
722 scsi_select(reqp, code)
723 SC_REQ *reqp;
724 int code;
725 {
726 u_char tmp[1];
727 u_char phase;
728 u_long cnt;
729 int sps;
730 u_int8_t atn_flag;
731 u_int8_t targ_bit;
732 struct ncr_softc *sc;
733
734 sc = reqp->xs->sc_link->adapter_softc;
735 DBG_SELPRINT ("Starting arbitration\n", 0);
736 PID("scsi_select1");
737
738 sps = splbio();
739
740 /*
741 * Prevent a race condition here. If a reslection interrupt occurred
742 * between the decision to pick a new request and the call to select,
743 * we abort the selection.
744 * Interrupts are lowered when the 5380 is setup to arbitrate for the
745 * bus.
746 */
747 if (connected) {
748 splx(sps);
749 PID("scsi_select2");
750 return (-1);
751 }
752
753 /*
754 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
755 * selection.
756 */
757 SET_5380_REG(NCR5380_TCOM, 0);
758 SET_5380_REG(NCR5380_ICOM, 0);
759
760 /*
761 * Arbitrate for the bus.
762 */
763 SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
764 SET_5380_REG(NCR5380_MODE, SC_ARBIT);
765
766 splx(sps);
767
768 cnt = 10;
769 while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
770 delay(1);
771
772 if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
773 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
774 SET_5380_REG(NCR5380_ICOM, 0);
775 DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
776 PID("scsi_select3");
777 return (-1);
778 }
779
780 /* The arbitration delay is 2.2 usecs */
781 delay(3);
782
783 /*
784 * Check the result of the arbitration. If we failed, return -1.
785 */
786 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
787 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
788 SET_5380_REG(NCR5380_ICOM, 0);
789 PID("scsi_select4");
790 return (-1);
791 }
792
793 /*
794 * The spec requires that we should read the data register to
795 * check for higher id's and check the SC_LA again.
796 */
797 tmp[0] = GET_5380_REG(NCR5380_DATA);
798 if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
799 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
800 SET_5380_REG(NCR5380_ICOM, 0);
801 DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
802 PID("scsi_select5");
803 return (-1);
804 }
805 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
806 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
807 SET_5380_REG(NCR5380_ICOM, 0);
808 DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
809 PID("scsi_select6");
810 return (-1);
811 }
812 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
813 if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
814 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
815 SET_5380_REG(NCR5380_ICOM, 0);
816 DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
817 PID("scsi_select7");
818 return (-1);
819 }
820 /* Bus settle delay + Bus clear delay = 1.2 usecs */
821 delay(2);
822 DBG_SELPRINT ("Arbitration complete\n", 0);
823
824 /*
825 * Now that we won the arbitration, start the selection.
826 */
827 targ_bit = 1 << reqp->targ_id;
828 SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
829
830 if (sc->sc_noselatn & targ_bit)
831 atn_flag = 0;
832 else
833 atn_flag = SC_A_ATN;
834
835 /*
836 * Raise ATN while SEL is true before BSY goes false from arbitration,
837 * since this is the only way to guarantee that we'll get a MESSAGE OUT
838 * phase immediately after the selection.
839 */
840 SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
841 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
842
843 /*
844 * Turn off reselection interrupts
845 */
846 SET_5380_REG(NCR5380_IDSTAT, 0);
847
848 /*
849 * Reset BSY. The delay following it, surpresses a glitch in the
850 * 5380 which causes us to see our own BSY signal instead of that of
851 * the target.
852 */
853 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
854 delay(1);
855
856 /*
857 * Wait for the target to react, the specs call for a timeout of
858 * 250 ms.
859 */
860 cnt = 25000;
861 while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
862 delay(10);
863
864 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
865 /*
866 * There is no reaction from the target, start the selection
867 * timeout procedure. We release the databus but keep SEL
868 * asserted. After that we wait a 'selection abort time' (200
869 * usecs) and 2 deskew delays (90 ns) and check BSY again.
870 * When BSY is asserted, we assume the selection succeeded,
871 * otherwise we release the bus.
872 */
873 SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
874 delay(201);
875 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
876 SET_5380_REG(NCR5380_ICOM, 0);
877 reqp->xs->error = code ? code : XS_SELTIMEOUT;
878 DBG_SELPRINT ("Target %d not responding to sel\n",
879 reqp->targ_id);
880 if (reqp->dr_flag & DRIVER_LINKCHK)
881 ncr_test_link &= ~(1<<reqp->targ_id);
882 finish_req(reqp);
883 PID("scsi_select8");
884 return (0);
885 }
886 }
887 SET_5380_REG(NCR5380_ICOM, atn_flag);
888
889 DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
890
891 /*
892 * The SCSI-interrupts are disabled while a request is being handled.
893 */
894 scsi_idisable();
895
896 /*
897 * If we did not request ATN, then don't try to send IDENTIFY.
898 */
899 if (atn_flag == 0) {
900 reqp->phase = PH_CMD;
901 goto identify_failed;
902 }
903
904 /*
905 * Here we prepare to send an 'IDENTIFY' message.
906 * Allow disconnect only when interrups are allowed.
907 */
908 tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
909 (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
910 cnt = 1;
911 phase = PH_MSGOUT;
912
913 /*
914 * Since we followed the SCSI-spec and raised ATN while SEL was true
915 * but before BSY was false during the selection, a 'MESSAGE OUT'
916 * phase should follow. Unfortunately, this does not happen on
917 * all targets (Asante ethernet devices, for example), so we must
918 * check the actual mode if the message transfer fails--if the
919 * new phase is PH_CMD and has never been successfully selected
920 * w/ATN in the past, then we assume that it is an old device
921 * that doesn't support select w/ATN.
922 */
923 if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
924
925 if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
926 DBG_SELPRINT ("Target %d: not responding to ATN.\n",
927 reqp->targ_id);
928 sc->sc_noselatn |= targ_bit;
929 reqp->phase = PH_CMD;
930 goto identify_failed;
931 }
932
933 DBG_SELPRINT ("Target %d: failed to send identify\n",
934 reqp->targ_id);
935 /*
936 * Try to disconnect from the target. We cannot leave
937 * it just hanging here.
938 */
939 if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
940 u_long len = 1;
941 u_char phase = PH_MSGOUT;
942 u_char msg = MSG_ABORT;
943
944 transfer_pio(&phase, &msg, &len, 0);
945 }
946 else scsi_reset_verbose(sc, "Connected to unidentified target");
947
948 SET_5380_REG(NCR5380_ICOM, 0);
949 reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
950 finish_req(reqp);
951 PID("scsi_select9");
952 return (0);
953 }
954 reqp->phase = PH_MSGOUT;
955
956 identify_failed:
957 sc->sc_selected |= targ_bit;
958
959 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
960 /*
961 * Command is connected, start timer ticking.
962 */
963 ccb_p->xtimeout = ccb_p->timeout + Lbolt;
964 #endif
965
966 connected = reqp;
967 busy |= targ_bit;
968 PID("scsi_select10");
969 return (0);
970 }
971
972 /*
973 * Return codes:
974 * 0: Job has finished or disconnected, find something else
975 * -1: keep on calling information_transfer() from scsi_main()
976 */
977 static int
978 information_transfer(sc)
979 struct ncr_softc *sc;
980 {
981 SC_REQ *reqp = connected;
982 u_char tmp, phase;
983 u_long len;
984
985 PID("info_transf1");
986 /*
987 * Clear pending interrupts from 5380-chip.
988 */
989 scsi_clr_ipend();
990
991 /*
992 * The SCSI-spec requires BSY to be true while connected to a target,
993 * loosing it means we lost the target...
994 * Also REQ needs to be asserted here to indicate that the bus-phase
995 * is valid. When the target does not supply REQ within a 'reasonable'
996 * amount of time, it's probably lost in it's own maze of twisting
997 * passages, we have to reset the bus to free it.
998 */
999 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
1000 wait_req_true();
1001 tmp = GET_5380_REG(NCR5380_IDSTAT);
1002
1003
1004 if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) {
1005 busy &= ~(1 << reqp->targ_id);
1006 connected = NULL;
1007 reqp->xs->error = XS_TIMEOUT;
1008 finish_req(reqp);
1009 if (!(tmp & SC_S_REQ))
1010 scsi_reset_verbose(sc,
1011 "Timeout waiting for phase-change");
1012 PID("info_transf2");
1013 return (0);
1014 }
1015
1016 phase = (tmp >> 2) & 7;
1017 if (phase != reqp->phase) {
1018 reqp->phase = phase;
1019 #ifdef DBG_INF
1020 if (dbg_target_mask & (1 << reqp->targ_id))
1021 DBG_INFPRINT(show_phase, reqp, phase);
1022 #endif
1023 }
1024 else {
1025 /*
1026 * Same data-phase. If same error give up
1027 */
1028 if ((reqp->msgout == MSG_ABORT)
1029 && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
1030 busy &= ~(1 << reqp->targ_id);
1031 connected = NULL;
1032 reqp->xs->error = XS_TIMEOUT;
1033 finish_req(reqp);
1034 scsi_reset_verbose(sc, "Failure to abort command");
1035 return (0);
1036 }
1037 }
1038
1039 switch (phase) {
1040 case PH_DATAOUT:
1041 #ifdef DBG_NOWRITE
1042 ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
1043 reqp->msgout = MSG_ABORT;
1044 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1045 return (-1);
1046 #endif /* DBG_NOWRITE */
1047 /*
1048 * If this is the first write using DMA, fill
1049 * the bounce buffer.
1050 */
1051 if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
1052 if (reqp->dr_flag & DRIVER_BOUNCING)
1053 bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
1054 }
1055
1056 case PH_DATAIN:
1057 if (reqp->xdata_len <= 0) {
1058 /*
1059 * Target keeps requesting data. Try to get into
1060 * message-out phase by feeding/taking 100 byte.
1061 */
1062 ncr_tprint(reqp, "Target requests too much data\n");
1063 reqp->msgout = MSG_ABORT;
1064 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1065 reach_msg_out(sc, 100);
1066 return (-1);
1067 }
1068 #ifdef REAL_DMA
1069 if (reqp->dr_flag & DRIVER_DMAOK) {
1070 int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
1071 transfer_dma(reqp, phase, poll);
1072 if (!poll)
1073 return (0);
1074 }
1075 else
1076 #endif
1077 {
1078 PID("info_transf3");
1079 len = reqp->xdata_len;
1080 #ifdef USE_PDMA
1081 if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
1082 return (0);
1083 #else
1084 transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
1085 #endif
1086 reqp->xdata_ptr += reqp->xdata_len - len;
1087 reqp->xdata_len = len;
1088 }
1089 return (-1);
1090 case PH_MSGIN:
1091 /*
1092 * We only expect single byte messages here.
1093 */
1094 len = 1;
1095 transfer_pio(&phase, &tmp, &len, 1);
1096 reqp->message = tmp;
1097 return (handle_message(reqp, tmp));
1098 case PH_MSGOUT:
1099 len = 1;
1100 transfer_pio(&phase, &reqp->msgout, &len, 0);
1101 if (reqp->msgout == MSG_ABORT) {
1102 busy &= ~(1 << reqp->targ_id);
1103 connected = NULL;
1104 if (!reqp->xs->error)
1105 reqp->xs->error = XS_DRIVER_STUFFUP;
1106 finish_req(reqp);
1107 PID("info_transf4");
1108 return (0);
1109 }
1110 reqp->msgout = MSG_NOOP;
1111 return (-1);
1112 case PH_CMD :
1113 len = command_size(reqp->xcmd.opcode);
1114 transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
1115 PID("info_transf5");
1116 return (-1);
1117 case PH_STATUS:
1118 len = 1;
1119 transfer_pio(&phase, &tmp, &len, 0);
1120 reqp->status = tmp;
1121 PID("info_transf6");
1122 return (-1);
1123 default :
1124 ncr_tprint(reqp, "Unknown phase\n");
1125 }
1126 PID("info_transf7");
1127 return (-1);
1128 }
1129
1130 /*
1131 * Handle the message 'msg' send to us by the target.
1132 * Return values:
1133 * 0 : The current command has completed.
1134 * -1 : Get on to the next phase.
1135 */
1136 static int
1137 handle_message(reqp, msg)
1138 SC_REQ *reqp;
1139 u_int msg;
1140 {
1141 int sps;
1142 SC_REQ *prev, *req;
1143
1144 PID("hmessage1");
1145 switch (msg) {
1146 /*
1147 * Linking lets us reduce the time required to get
1148 * the next command to the device, skipping the arbitration
1149 * and selection time. In the current implementation,
1150 * we merely have to start the next command pointed
1151 * to by 'next_link'.
1152 */
1153 case MSG_LINK_CMD_COMPLETE:
1154 case MSG_LINK_CMD_COMPLETEF:
1155 if (reqp->link == NULL) {
1156 ncr_tprint(reqp, "No link for linked command");
1157 nack_message(reqp, MSG_ABORT);
1158 PID("hmessage2");
1159 return (-1);
1160 }
1161 ack_message();
1162 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1163 reqp->xs->resid = reqp->xdata_len;
1164 reqp->xs->error = 0;
1165 }
1166
1167 #ifdef AUTO_SENSE
1168 if (check_autosense(reqp, 1) == -1)
1169 return (-1);
1170 #endif /* AUTO_SENSE */
1171
1172 #ifdef DBG_REQ
1173 if (dbg_target_mask & (1 << reqp->targ_id))
1174 show_request(reqp->link, "LINK");
1175 #endif
1176 connected = reqp->link;
1177
1178 /*
1179 * Unlink the 'linked' request from the issue_q
1180 */
1181 sps = splbio();
1182 prev = NULL;
1183 req = issue_q;
1184 for (; req != NULL; prev = req, req = req->next) {
1185 if (req == connected)
1186 break;
1187 }
1188 if (req == NULL)
1189 panic("Inconsistent issue_q");
1190 if (prev == NULL)
1191 issue_q = req->next;
1192 else prev->next = req->next;
1193 req->next = NULL;
1194 splx(sps);
1195
1196 finish_req(reqp);
1197 PID("hmessage3");
1198 return (-1);
1199 case MSG_ABORT:
1200 case MSG_CMDCOMPLETE:
1201 ack_message();
1202 connected = NULL;
1203 busy &= ~(1 << reqp->targ_id);
1204 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1205 reqp->xs->resid = reqp->xdata_len;
1206 reqp->xs->error = 0;
1207 }
1208
1209 #ifdef AUTO_SENSE
1210 if (check_autosense(reqp, 0) == -1) {
1211 PID("hmessage4");
1212 return (0);
1213 }
1214 #endif /* AUTO_SENSE */
1215
1216 finish_req(reqp);
1217 PID("hmessage5");
1218 return (0);
1219 case MSG_MESSAGE_REJECT:
1220 ack_message();
1221 PID("hmessage6");
1222 return (-1);
1223 case MSG_DISCONNECT:
1224 ack_message();
1225 #ifdef DBG_REQ
1226 if (dbg_target_mask & (1 << reqp->targ_id))
1227 show_request(reqp, "DISCON");
1228 #endif
1229 sps = splbio();
1230 connected = NULL;
1231 reqp->next = discon_q;
1232 discon_q = reqp;
1233 splx(sps);
1234 PID("hmessage7");
1235 return (0);
1236 case MSG_SAVEDATAPOINTER:
1237 case MSG_RESTOREPOINTERS:
1238 /*
1239 * We save pointers implicitely at disconnect.
1240 * So we can ignore these messages.
1241 */
1242 ack_message();
1243 PID("hmessage8");
1244 return (-1);
1245 case MSG_EXTENDED:
1246 nack_message(reqp, MSG_MESSAGE_REJECT);
1247 PID("hmessage9");
1248 return (-1);
1249 default:
1250 if ((msg & 0x80) && !(msg & 0x18)) { /* IDENTIFY */
1251 PID("hmessage10");
1252 ack_message();
1253 return (0);
1254 } else {
1255 ncr_tprint(reqp,
1256 "Unknown message %x. Rejecting.\n",
1257 msg);
1258 nack_message(reqp, MSG_MESSAGE_REJECT);
1259 }
1260 return (-1);
1261 }
1262 PID("hmessage11");
1263 return (-1);
1264 }
1265
1266 /*
1267 * Handle reselection. If a valid reconnection occurs, connected
1268 * points at the reconnected command. The command is removed from the
1269 * disconnected queue.
1270 */
1271 static void
1272 reselect(sc)
1273 struct ncr_softc *sc;
1274 {
1275 u_char phase;
1276 u_long len;
1277 u_char msg;
1278 u_char target_mask;
1279 int abort = 0;
1280 SC_REQ *tmp, *prev;
1281
1282 PID("reselect1");
1283 target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1284
1285 /*
1286 * At this point, we have detected that our SCSI-id is on the bus,
1287 * SEL is true and BSY was false for at least one bus settle
1288 * delay (400 ns.).
1289 * We must assert BSY ourselves, until the target drops the SEL signal.
1290 * The SCSI-spec specifies no maximum time for this, so we have to
1291 * choose something long enough to suit all targets.
1292 */
1293 SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1294 len = 250000;
1295 while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1296 delay(1);
1297 len--;
1298 }
1299 if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1300 /* Damn SEL isn't dropping */
1301 scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
1302 return;
1303 }
1304
1305 SET_5380_REG(NCR5380_ICOM, 0);
1306
1307 /*
1308 * Check if the reselection is still valid. Check twice because
1309 * of possible line glitches - cheaper than delay(1) and we need
1310 * only a few nanoseconds.
1311 */
1312 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1313 if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
1314 ncr_aprint(sc, "Stepped into the reselection timeout\n");
1315 return;
1316 }
1317 }
1318
1319 /*
1320 * Get the expected identify message.
1321 */
1322 phase = PH_MSGIN;
1323 len = 1;
1324 transfer_pio(&phase, &msg, &len, 0);
1325 if (len || !MSG_ISIDENTIFY(msg)) {
1326 ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1327 abort = 1;
1328 tmp = NULL;
1329 }
1330 else {
1331 /*
1332 * Find the command reconnecting
1333 */
1334 for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1335 if (target_mask == (1 << tmp->targ_id)) {
1336 if (prev)
1337 prev->next = tmp->next;
1338 else discon_q = tmp->next;
1339 tmp->next = NULL;
1340 break;
1341 }
1342 }
1343 if (tmp == NULL) {
1344 ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1345 target_mask);
1346 abort = 1;
1347 }
1348 }
1349 if (abort) {
1350 msg = MSG_ABORT;
1351 len = 1;
1352 phase = PH_MSGOUT;
1353
1354 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1355 if (transfer_pio(&phase, &msg, &len, 0) || len)
1356 scsi_reset_verbose(sc, "Failure to abort reselection");
1357 }
1358 else {
1359 connected = tmp;
1360 #ifdef DBG_REQ
1361 if (dbg_target_mask & (1 << tmp->targ_id))
1362 show_request(tmp, "RECON");
1363 #endif
1364 }
1365 PID("reselect2");
1366 }
1367
1368 /*
1369 * Transfer data in a given phase using programmed I/O.
1370 * Returns -1 when a different phase is entered without transferring the
1371 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
1372 * phase.
1373 */
1374 static int
1375 transfer_pio(phase, data, len, dont_drop_ack)
1376 u_char *phase;
1377 u_char *data;
1378 u_long *len;
1379 int dont_drop_ack;
1380 {
1381 u_int cnt = *len;
1382 u_char ph = *phase;
1383 u_char tmp, new_icom;
1384
1385 DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1386 PID("tpio1");
1387 SET_5380_REG(NCR5380_TCOM, ph);
1388 do {
1389 if (!wait_req_true()) {
1390 DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1391 break;
1392 }
1393 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1394 DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1395 break;
1396 }
1397 if (PH_IN(ph)) {
1398 *data++ = GET_5380_REG(NCR5380_DATA);
1399 SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1400 if ((cnt == 1) && dont_drop_ack)
1401 new_icom = SC_A_ACK;
1402 else new_icom = 0;
1403 }
1404 else {
1405 SET_5380_REG(NCR5380_DATA, *data++);
1406
1407 /*
1408 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1409 * the initiator should drop ATN on the last byte of the
1410 * message phase after REQ has been asserted for the handshake
1411 * but before the initiator raises ACK.
1412 */
1413 if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1414 SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1415 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1416 new_icom = 0;
1417 }
1418 else {
1419 SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1420 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1421 new_icom = SC_A_ATN;
1422 }
1423 }
1424 if (!wait_req_false()) {
1425 DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1426 break;
1427 }
1428 SET_5380_REG(NCR5380_ICOM, new_icom);
1429
1430 } while (--cnt);
1431
1432 if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1433 *phase = (tmp >> 2) & 7;
1434 else *phase = NR_PHASE;
1435 *len = cnt;
1436 DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1437 *phase, cnt);
1438 PID("tpio2");
1439 if (!cnt || (*phase == ph))
1440 return (0);
1441 return (-1);
1442 }
1443
1444 #ifdef REAL_DMA
1445
1446 /*
1447 * Start a DMA-transfer on the device using the current pointers.
1448 * If 'poll' is true, the function busy-waits until DMA has completed.
1449 */
1450 static void
1451 transfer_dma(reqp, phase, poll)
1452 SC_REQ *reqp;
1453 u_int phase;
1454 int poll;
1455 {
1456 int dma_done;
1457 u_char mbase = 0;
1458 int sps;
1459
1460 again:
1461 PID("tdma1");
1462
1463 /*
1464 * We should be in phase, otherwise we are not allowed to
1465 * drive the bus.
1466 */
1467 SET_5380_REG(NCR5380_TCOM, phase);
1468
1469 /*
1470 * Defer interrupts until DMA is fully running.
1471 */
1472 sps = splbio();
1473
1474 /*
1475 * Clear pending interrupts and parity errors.
1476 */
1477 scsi_clr_ipend();
1478
1479 if (!poll) {
1480 /*
1481 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1482 * to the interrupts we want enabled.
1483 */
1484 scsi_ienable();
1485 reqp->dr_flag |= DRIVER_IN_DMA;
1486 mbase = SC_E_EOPI | SC_MON_BSY;
1487 }
1488 else scsi_idisable();
1489 mbase |= IMODE_BASE | SC_M_DMA;
1490 scsi_dma_setup(reqp, phase, mbase);
1491
1492 splx(sps);
1493
1494 if (poll) {
1495 /*
1496 * On polled-dma transfers, we wait here until the
1497 * 'end-of-dma' condition occurs.
1498 */
1499 poll_edma(reqp);
1500 if (!(dma_done = dma_ready()))
1501 goto again;
1502 }
1503 PID("tdma2");
1504 }
1505
1506 /*
1507 * Check results of a DMA data-transfer.
1508 */
1509 static int
1510 dma_ready()
1511 {
1512 SC_REQ *reqp = connected;
1513 int dmstat, is_edma;
1514 long bytes_left, bytes_done;
1515
1516 is_edma = get_dma_result(reqp, &bytes_left);
1517 dmstat = GET_5380_REG(NCR5380_DMSTAT);
1518
1519 /*
1520 * Check if the call is sensible and not caused by any spurious
1521 * interrupt.
1522 */
1523 if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1524 && (dmstat & SC_PHS_MTCH) ) {
1525 ncr_tprint(reqp, "dma_ready: spurious call "
1526 "(dm:%x,last_hit: %s)\n",
1527 #ifdef DBG_PID
1528 dmstat, last_hit[DBG_PID-1]);
1529 #else
1530 dmstat, "unknown");
1531 #endif
1532 return (0);
1533 }
1534
1535 /*
1536 * Clear all (pending) interrupts.
1537 */
1538 scsi_clr_ipend();
1539
1540 /*
1541 * Update various transfer-pointers/lengths
1542 */
1543 bytes_done = reqp->dm_cur->dm_count - bytes_left;
1544
1545 if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1546 /*
1547 * Copy the bytes read until now from the bounce buffer
1548 * to the 'real' destination. Flush the data-cache
1549 * before copying.
1550 */
1551 PCIA();
1552 bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1553 reqp->bouncerp += bytes_done;
1554 }
1555
1556 reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1557 reqp->xdata_len -= bytes_done; /* XXX */
1558 if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1559 reqp->dm_cur++;
1560 else reqp->dm_cur->dm_addr += bytes_done;
1561
1562 if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1563 if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
1564 ncr_tprint(reqp, "parity error in data-phase\n");
1565 reqp->xs->error = XS_TIMEOUT;
1566 }
1567 }
1568
1569 /*
1570 * DMA mode should always be reset even when we will continue with the
1571 * next chain. It is also essential to clear the MON_BUSY because
1572 * when LOST_BUSY is unexpectedly set, we will not be able to drive
1573 * the bus....
1574 */
1575 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1576
1577
1578 if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1579 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1580
1581 /*
1582 * Tell interrupt functions DMA mode has ended.
1583 */
1584 reqp->dr_flag &= ~DRIVER_IN_DMA;
1585
1586 /*
1587 * Clear mode and icom
1588 */
1589 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1590 SET_5380_REG(NCR5380_ICOM, 0);
1591
1592 if (dmstat & SC_BSY_ERR) {
1593 if (!reqp->xs->error)
1594 reqp->xs->error = XS_TIMEOUT;
1595 finish_req(reqp);
1596 PID("dma_ready1");
1597 return (1);
1598 }
1599
1600 if (reqp->xs->error != 0) {
1601 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1602 reqp->msgout = MSG_ABORT;
1603 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1604 }
1605 PID("dma_ready2");
1606 return (1);
1607 }
1608 return (0);
1609 }
1610 #endif /* REAL_DMA */
1611
1612 static int
1613 check_autosense(reqp, linked)
1614 SC_REQ *reqp;
1615 int linked;
1616 {
1617 int sps;
1618
1619 /*
1620 * If this is the driver's Link Check for this target, ignore
1621 * the results of the command. All we care about is whether we
1622 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
1623 */
1624 PID("linkcheck");
1625 if (reqp->dr_flag & DRIVER_LINKCHK) {
1626 if (linked)
1627 ncr_will_link |= 1<<reqp->targ_id;
1628 else ncr_tprint(reqp, "Does not support linked commands\n");
1629 return (0);
1630 }
1631 /*
1632 * If we not executing an auto-sense and the status code
1633 * is request-sense, we automatically issue a request
1634 * sense command.
1635 */
1636 PID("cautos1");
1637 if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1638 switch (reqp->status & SCSMASK) {
1639 case SCSCHKC:
1640 bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
1641 reqp->xdata_ptr = (u_char *)&reqp->xs->sense.scsi_sense;
1642 reqp->xdata_len = sizeof(reqp->xs->sense.scsi_sense);
1643 reqp->dr_flag |= DRIVER_AUTOSEN;
1644 reqp->dr_flag &= ~DRIVER_DMAOK;
1645 if (!linked) {
1646 sps = splbio();
1647 reqp->next = issue_q;
1648 issue_q = reqp;
1649 splx(sps);
1650 }
1651 else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
1652
1653 #ifdef DBG_REQ
1654 bzero(reqp->xdata_ptr, reqp->xdata_len);
1655 if (dbg_target_mask & (1 << reqp->targ_id))
1656 show_request(reqp, "AUTO-SENSE");
1657 #endif
1658 PID("cautos2");
1659 return (-1);
1660 case SCSBUSY:
1661 reqp->xs->error = XS_BUSY;
1662 return (0);
1663 }
1664 }
1665 else {
1666 /*
1667 * An auto-sense has finished
1668 */
1669 if ((reqp->status & SCSMASK) != SCSGOOD)
1670 reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1671 else reqp->xs->error = XS_SENSE;
1672 reqp->status = SCSCHKC;
1673 }
1674 PID("cautos3");
1675 return (0);
1676 }
1677
1678 static int
1679 reach_msg_out(sc, len)
1680 struct ncr_softc *sc;
1681 u_long len;
1682 {
1683 u_char phase;
1684 u_char data;
1685 u_long n = len;
1686
1687 ncr_aprint(sc, "Trying to reach Message-out phase\n");
1688 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1689 phase = (phase >> 2) & 7;
1690 else return (-1);
1691 ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1692 if (phase == PH_MSGOUT)
1693 return (0);
1694
1695 SET_5380_REG(NCR5380_TCOM, phase);
1696
1697 do {
1698 if (!wait_req_true())
1699 break;
1700 if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1701 break;
1702 if (PH_IN(phase)) {
1703 data = GET_5380_REG(NCR5380_DATA);
1704 SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1705 }
1706 else {
1707 SET_5380_REG(NCR5380_DATA, 0);
1708 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN);
1709 SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1710 }
1711 if (!wait_req_false())
1712 break;
1713 SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1714 } while (--n);
1715
1716 if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1717 phase = (phase >> 2) & 7;
1718 if (phase == PH_MSGOUT) {
1719 ncr_aprint(sc, "Message-out phase reached after "
1720 "%ld bytes.\n", len - n);
1721 return (0);
1722 }
1723 ncr_aprint(sc, "Phase now: %d after %ld bytes.\n", phase,
1724 len - n);
1725
1726 }
1727 return (-1);
1728 }
1729
1730 void
1731 scsi_reset()
1732 {
1733 SC_REQ *tmp, *next;
1734 int sps;
1735
1736 PID("scsi_reset1");
1737 sps = splbio();
1738 SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1739 delay(100);
1740 SET_5380_REG(NCR5380_ICOM, 0);
1741 scsi_clr_ipend();
1742
1743 /*
1744 * None of the jobs in the discon_q will ever be reconnected,
1745 * notify this to the higher level code.
1746 */
1747 for (tmp = discon_q; tmp ;) {
1748 next = tmp->next;
1749 tmp->next = NULL;
1750 tmp->xs->error = XS_TIMEOUT;
1751 busy &= ~(1 << tmp->targ_id);
1752 finish_req(tmp);
1753 tmp = next;
1754 }
1755 discon_q = NULL;
1756
1757 /*
1758 * The current job will never finish either.
1759 * The problem is that we can't finish the job because an instance
1760 * of main is running on it. Our best guess is that the job is currently
1761 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1762 * the job because it detects BSY-loss.
1763 */
1764 if ((tmp = connected) != NULL) {
1765 if (tmp->dr_flag & DRIVER_IN_DMA) {
1766 tmp->xs->error = XS_DRIVER_STUFFUP;
1767 #ifdef REAL_DMA
1768 dma_ready();
1769 #endif
1770 }
1771 }
1772 splx(sps);
1773 PID("scsi_reset2");
1774
1775 /*
1776 * Give the attached devices some time to handle the reset. This
1777 * value is arbitrary but should be relatively long.
1778 */
1779 delay(100000);
1780 }
1781
1782 static void
1783 scsi_reset_verbose(sc, why)
1784 struct ncr_softc *sc;
1785 const char *why;
1786 {
1787 ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
1788
1789 scsi_reset();
1790 }
1791
1792 /*
1793 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1794 * the appropriate action is carried out (reselection or DMA ready) and
1795 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1796 * and INTR_SPURIOUS is returned.
1797 */
1798 static int
1799 check_intr(sc)
1800 struct ncr_softc *sc;
1801 {
1802 SC_REQ *reqp;
1803
1804 if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1805 ==(SC_S_SEL|SC_S_IO))
1806 return (INTR_RESEL);
1807 else {
1808 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1809 reqp->dr_flag &= ~DRIVER_IN_DMA;
1810 return (INTR_DMA);
1811 }
1812 }
1813 printf("-->");
1814 scsi_show();
1815 scsi_clr_ipend();
1816 ncr_aprint(sc, "Spurious interrupt.\n");
1817 return (INTR_SPURIOUS);
1818 }
1819
1820 #ifdef REAL_DMA
1821 /*
1822 * Check if DMA can be used for this request. This function also builds
1823 * the dma-chain.
1824 */
1825 static int
1826 scsi_dmaok(reqp)
1827 SC_REQ *reqp;
1828 {
1829 u_long phy_buf;
1830 u_long phy_len;
1831 void *req_addr;
1832 u_long req_len;
1833 struct dma_chain *dm;
1834
1835 /*
1836 * Initialize locals and requests' DMA-chain.
1837 */
1838 req_len = reqp->xdata_len;
1839 req_addr = (void*)reqp->xdata_ptr;
1840 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1841 dm->dm_count = dm->dm_addr = 0;
1842 reqp->dr_flag &= ~DRIVER_BOUNCING;
1843
1844 /*
1845 * Do not accept zero length DMA.
1846 */
1847 if (req_len == 0)
1848 return (0);
1849
1850 /*
1851 * If DMA is emulated in software, we don't have to breakup the
1852 * request. Just build a chain with a single element and stash in
1853 * the KVA and not the KPA.
1854 */
1855 if (emulated_dma()) {
1856 dm->dm_addr = (u_long)req_addr;
1857 dm->dm_count = req_len;
1858 return (1);
1859 }
1860
1861 /*
1862 * LWP: I think that this restriction is not strictly nessecary.
1863 */
1864 if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1865 return (0);
1866
1867 /*
1868 * Build the DMA-chain.
1869 */
1870 dm->dm_addr = phy_buf = kvtop(req_addr);
1871 while (req_len) {
1872 if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1873 phy_len = req_len;
1874
1875 req_addr += phy_len;
1876 req_len -= phy_len;
1877 dm->dm_count += phy_len;
1878
1879 if (req_len) {
1880 u_long tmp = kvtop(req_addr);
1881
1882 if ((phy_buf + phy_len) != tmp) {
1883 if (wrong_dma_range(reqp, dm)) {
1884 if (reqp->dr_flag & DRIVER_BOUNCING)
1885 goto bounceit;
1886 return (0);
1887 }
1888
1889 if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1890 ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1891 return (0);
1892 }
1893 dm->dm_count = 0;
1894 dm->dm_addr = tmp;
1895 }
1896 phy_buf = tmp;
1897 }
1898 }
1899 if (wrong_dma_range(reqp, dm)) {
1900 if (reqp->dr_flag & DRIVER_BOUNCING)
1901 goto bounceit;
1902 return (0);
1903 }
1904 reqp->dm_last = dm;
1905 return (1);
1906
1907 bounceit:
1908 if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1909 /*
1910 * If we can't get a bounce buffer, forget DMA
1911 */
1912 reqp->dr_flag &= ~DRIVER_BOUNCING;
1913 return(0);
1914 }
1915 /*
1916 * Initialize a single DMA-range containing the bounced request
1917 */
1918 dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1919 dm->dm_addr = kvtop(reqp->bounceb);
1920 dm->dm_count = reqp->xdata_len;
1921 reqp->bouncerp = reqp->bounceb;
1922
1923 return (1);
1924 }
1925 #endif /* REAL_DMA */
1926
1927 static void
1928 run_main(sc)
1929 struct ncr_softc *sc;
1930 {
1931 int sps = splbio();
1932
1933 if (!main_running) {
1934 /*
1935 * If shared resources are required, claim them
1936 * before entering 'scsi_main'. If we can't get them
1937 * now, assume 'run_main' will be called when the resource
1938 * becomes available.
1939 */
1940 if (!claimed_dma()) {
1941 splx(sps);
1942 return;
1943 }
1944 main_running = 1;
1945 splx(sps);
1946 scsi_main(sc);
1947 }
1948 else splx(sps);
1949 }
1950
1951 /*
1952 * Prefix message with full target info.
1953 */
1954 static void
1955 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1956 {
1957 va_list ap;
1958
1959 va_start(ap, fmt);
1960 scsi_print_addr(reqp->xs->sc_link);
1961 printf("%:", fmt, ap);
1962 va_end(ap);
1963 }
1964
1965 /*
1966 * Prefix message with adapter info.
1967 */
1968 static void
1969 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1970 {
1971 va_list ap;
1972
1973 va_start(ap, fmt);
1974 printf("%s: %:", sc->sc_dev.dv_xname, fmt, ap);
1975 va_end(ap);
1976 }
1977 /****************************************************************************
1978 * Start Debugging Functions *
1979 ****************************************************************************/
1980 static char *phase_names[] = {
1981 "DATA_OUT", "DATA_IN", "COMMAND", "STATUS", "NONE", "NONE", "MSG_OUT",
1982 "MSG_IN"
1983 };
1984
1985 static void
1986 show_phase(reqp, phase)
1987 SC_REQ *reqp;
1988 int phase;
1989 {
1990 printf("INFTRANS: %d Phase = %s\n", reqp->targ_id, phase_names[phase]);
1991 }
1992
1993 static void
1994 show_data_sense(xs)
1995 struct scsipi_xfer *xs;
1996 {
1997 u_char *p1, *p2;
1998 int i;
1999 int sz;
2000
2001 p1 = (u_char *) xs->cmd;
2002 p2 = (u_char *)&xs->sense.scsi_sense;
2003 if(*p2 == 0)
2004 return; /* No(n)sense */
2005 printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
2006 for (i = 0; i < sz; i++)
2007 printf("%x ", p1[i]);
2008 printf("\nsense: ");
2009 for (i = 0; i < sizeof(xs->sense.scsi_sense); i++)
2010 printf("%x ", p2[i]);
2011 printf("\n");
2012 }
2013
2014 static void
2015 show_request(reqp, qtxt)
2016 SC_REQ *reqp;
2017 char *qtxt;
2018 {
2019 printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
2020 qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
2021 reqp->xcmd.opcode, reqp->status, reqp->message,
2022 reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
2023 reqp->link ? "L":"");
2024 if (reqp->status == SCSCHKC)
2025 show_data_sense(reqp->xs);
2026 }
2027
2028 static char *sig_names[] = {
2029 "PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
2030 "ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
2031 };
2032
2033 static void
2034 show_signals(dmstat, idstat)
2035 u_char dmstat, idstat;
2036 {
2037 u_short tmp, mask;
2038 int j, need_pipe;
2039
2040 tmp = idstat | ((dmstat & 3) << 8);
2041 printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
2042 for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
2043 if (tmp & mask)
2044 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
2045 }
2046 printf("\nDma status (%02x): ", dmstat);
2047 for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
2048 if (dmstat & mask)
2049 printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
2050 }
2051 printf("\n");
2052 }
2053
2054 void
2055 scsi_show()
2056 {
2057 SC_REQ *tmp;
2058 int sps = splhigh();
2059 u_char idstat, dmstat;
2060 int i;
2061
2062 printf("scsi_show: scsi_main is%s running\n",
2063 main_running ? "" : " not");
2064 for (tmp = issue_q; tmp; tmp = tmp->next)
2065 show_request(tmp, "ISSUED");
2066 for (tmp = discon_q; tmp; tmp = tmp->next)
2067 show_request(tmp, "DISCONNECTED");
2068 if (connected)
2069 show_request(connected, "CONNECTED");
2070 if (can_access_5380()) {
2071 idstat = GET_5380_REG(NCR5380_IDSTAT);
2072 dmstat = GET_5380_REG(NCR5380_DMSTAT);
2073 show_signals(dmstat, idstat);
2074 }
2075
2076 if (connected)
2077 printf("phase = %d, ", connected->phase);
2078 printf("busy:%x, spl:%04x\n", busy, sps);
2079 #ifdef DBG_PID
2080 for (i=0; i<DBG_PID; i++)
2081 printf("\t%d\t%s\n", i, last_hit[i]);
2082 #endif
2083
2084 splx(sps);
2085 }
2086