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