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