1 /* $NecBSD: bsfunc.c,v 1.2 1997/10/31 17:43:37 honda Exp $ */
4 * [NetBSD for NEC PC98 series]
5 * Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1994, 1995, 1996 Naofumi HONDA. All rights reserved.
36 #include <i386/Cbus/dev/bs/bsif.h>
39 #include <i386/isa/bs/bsif.h>
43 struct bs_statics bs_statics[NTARGETS];
44 u_int bs_linkcmd_count[NTARGETS];
45 u_int bs_bounce_used[NTARGETS];
46 #endif /* BS_STATICS */
49 int bs_debug_flag = 0;
52 static void bs_print_syncmsg __P((struct targ_info *, char*));
53 static void bs_timeout_target __P((struct targ_info *));
54 static void bs_kill_msg __P((struct bsccb *cb));
56 static int bs_start_target __P((struct targ_info *));
57 static int bs_check_target __P((struct targ_info *));
59 /*************************************************************
61 ************************************************************/
62 GENERIC_CCB_STATIC_ALLOC(bs, bsccb)
63 GENERIC_CCB(bs, bsccb, ccb_chain)
65 /*************************************************************
67 ************************************************************/
72 struct bs_softc *bsc = ti->ti_bsc;
74 ti->ti_error |= BSTIMEOUT;
75 bsc->sc_flags |= BSRESET;
77 if (ti->ti_herrcnt ++ >= HARDRETRIES)
79 bs_printf(ti, "timeout", "async transfer!");
80 ti->ti_syncmax.period = ti->ti_syncmax.offset = 0;
88 struct bs_softc *bsc = (struct bs_softc *) arg;
94 bsc->sc_flags &= ~BSSTARTTIMEOUT;
97 if ((ti = bsc->sc_nexus) && (cb = ti->ti_ctab.tqh_first))
99 if ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0)
100 bs_timeout_target(ti);
102 else for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
104 if (bsc->sc_dtgnum && ti->ti_phase < DISCONNECTED)
107 cb = ti->ti_ctab.tqh_first;
108 if (cb && ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0))
109 bs_timeout_target(ti);
113 if (bsc->sc_flags & BSRESET)
115 bs_debug_print_all(bsc);
116 bs_printf(ti, "timeout", "bus hang up");
120 bs_start_timeout(bsc);
124 /**************************************************
126 *************************************************/
127 static u_int8_t cmd_unit_ready[6];
130 bs_make_internal_ccb(ti, lun, cmd, cmdlen, data, datalen, flags, timeout)
131 struct targ_info *ti;
142 if ((cb = bs_get_ccb()) == NULL)
143 bs_panic(ti->ti_bsc, "can not get ccb mem");
147 cb->cmd = (cmd ? cmd : cmd_unit_ready);
148 cb->cmdlen = (cmd ? cmdlen : sizeof(cmd_unit_ready));
150 cb->datalen = (data ? datalen : 0);
152 cb->bsccb_flags = flags & BSCFLAGSMASK;
153 bs_targ_flags(ti, cb);
155 cb->tcmax = (timeout > BS_DEFAULT_TIMEOUT_SECOND ? timeout :
156 BS_DEFAULT_TIMEOUT_SECOND);
158 TAILQ_INSERT_HEAD(&ti->ti_ctab, cb, ccb_chain);
164 bs_make_msg_ccb(ti, lun, cb, msg, timex)
165 struct targ_info *ti;
173 flags = BSFORCEIOPOLL | msg->flag;
175 cb = bs_make_internal_ccb(ti, lun, NULL, 0, NULL, 0,
178 cb->bsccb_flags |= flags & BSCFLAGSMASK;
180 cb->msgoutlen = msg->msglen;
181 bcopy(msg->msg, cb->msgout, msg->msglen);
186 bs_send_msg(ti, lun, msg, timex)
187 struct targ_info *ti;
194 cb = bs_make_msg_ccb(ti, lun, NULL, msg, timex);
195 bscmdstart(ti, BSCMDSTART);
196 return bs_scsi_cmd_poll(ti, cb);
206 /**************************************************
208 **************************************************/
211 struct targ_info *ti;
215 bzero(ti->scsi_cmd, sizeof(struct scsi_sense));
216 bzero(&ti->sense, sizeof(struct scsi_sense_data));
217 ti->scsi_cmd[0] = REQUEST_SENSE;
218 ti->scsi_cmd[1] = (ti->ti_lun << 5);
219 ti->scsi_cmd[4] = sizeof(struct scsi_sense_data);
220 cb = bs_make_internal_ccb(ti, ti->ti_lun, ti->scsi_cmd,
221 sizeof(struct scsi_sense),
222 (u_int8_t *) & ti->sense,
223 sizeof(struct scsi_sense_data),
225 BS_DEFAULT_TIMEOUT_SECOND);
226 cb->bsccb_flags |= BSSENSECCB;
230 /**************************************************
232 *************************************************/
235 bs_start_syncmsg(ti, cb, flag)
236 struct targ_info *ti;
240 struct syncdata *negp, *maxp;
244 negp = &ti->ti_syncnow;
245 maxp = &ti->ti_syncmax;
247 ti->ti_state = BS_TARG_SYNCH;
249 if (flag == BS_SYNCMSG_REQUESTED)
251 if (negp->offset > maxp->offset)
252 negp->offset = maxp->offset;
253 if (negp->offset != 0 && negp->period < maxp->period)
254 negp->period = maxp->period;
259 cb = ti->ti_ctab.tqh_first;
261 else if (ti->ti_cfgflags & BS_SCSI_SYNC)
263 negp->offset = maxp->offset;
264 negp->period = maxp->period;
266 msg.flag = BSERROROK;
271 ti->ti_state = BS_TARG_RDY;
275 BS_SETUP_SYNCSTATE(flag);
276 msg.msg[0] = MSG_EXTEND;
277 msg.msg[1] = MSG_EXTEND_SYNCHLEN;
278 msg.msg[2] = MSG_EXTEND_SYNCHCODE;
279 msg.msg[3] = negp->period;
280 msg.msg[4] = negp->offset;
281 msg.msglen = MSG_EXTEND_SYNCHLEN + 2;
283 bs_make_msg_ccb(ti, lun, cb, &msg, BS_SYNC_TIMEOUT);
288 bs_print_syncmsg(ti, s)
289 struct targ_info *ti;
292 struct bs_softc *bsc = ti->ti_bsc;
293 struct syncdata *negp;
296 negp = &ti->ti_syncnow;
297 speed = (negp->offset && negp->period) ?
298 (2500 / ((u_int) negp->period)) : 0;
300 printf("%s(%d:%d): <%s> ", bsc->sc_dvname, ti->ti_id, ti->ti_lun, s);
301 printf("period 0x%x offset %d chip (0x%x)", negp->period, negp->offset,
304 printf(" %d.%d M/s", speed / 10, speed % 10);
309 bs_analyze_syncmsg(ti, cb)
310 struct targ_info *ti;
313 struct bs_softc *bsc = ti->ti_bsc;
314 u_int8_t ans = ti->ti_syncnow.state;
315 struct syncdata *negp, *maxp;
316 struct syncdata bdata;
320 negp = &ti->ti_syncnow;
322 maxp = &ti->ti_syncmax;
326 case BS_SYNCMSG_REJECT:
331 case BS_SYNCMSG_ASSERT:
337 if (negp->offset != 0 && negp->period < maxp->period)
340 s = "illegal(period)";
342 else if (negp->offset > maxp->offset)
345 s = "illegal(offset)";
348 period = negp->offset ? negp->period : 0;
354 bshw_adj_syncdata(negp);
357 if (ans == BS_SYNCMSG_REQUESTED)
360 s = negp->offset ? "synchronous" : "async";
364 negp->offset = maxp->offset = 0;
365 bshw_adj_syncdata(negp);
366 bshw_adj_syncdata(maxp);
369 /* really setup hardware */
370 bshw_set_synchronous(bsc, ti);
371 if (cb == NULL || (period >= negp->period && period <= negp->period + 2))
373 bs_print_syncmsg(ti, s);
374 BS_SETUP_TARGSTATE(BS_TARG_RDY);
375 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
383 bs_printf(ti, "bs_analyze_syncmsg",
384 "sync(period) mismatch, retry neg...");
385 printf("expect(%d:0x%x) => reply(%d:0x%x)\n",
386 bdata.offset, bdata.period, negp->offset, negp->period);
388 bs_start_syncmsg(ti, cb, BS_SYNCMSG_ASSERT);
393 /**************************************************
394 * ABORT AND RESET MSG
395 **************************************************/
396 /* send device reset msg and wait */
399 struct targ_info *ti;
404 msg.msg[0] = MSG_RESET;
407 bs_send_msg(ti, 0, &msg, 0);
409 delay(ti->ti_bsc->sc_RSTdelay);
416 struct targ_info *ti;
418 struct bs_softc *bsc = ti->ti_bsc;
420 struct bsccb *cb = ti->ti_ctab.tqh_first;
432 msg.msg[0] = MSG_ABORT;
435 cb = bs_make_msg_ccb(ti, lun, NULL, &msg, 0);
436 bscmdstart(ti, BSCMDSTART);
438 if (bsc->sc_nexus == ti)
444 /**************************************************
445 * COMPLETE SCSI BUS RESET
446 *************************************************/
449 * 1) reset scsi bus (ie. all target reseted).
451 * 3) check target status.
452 * 4) sync neg with all targets.
453 * 5) setup sync reg in host.
454 * 6) recover previous nexus.
457 bs_scsibus_start(bsc)
458 struct bs_softc *bsc;
460 struct targ_info *ti, *nextti = NULL;
461 int error = HASERROR;
462 u_int querm, bits, skip = 0;
464 querm = (bsc->sc_hstate == BSC_BOOTUP);
465 bsc->sc_hstate = BSC_TARG_CHECK;
470 if (error != COMPLETE)
472 printf("%s: scsi bus reset and try to restart ...",
475 bshw_dmaabort(bsc, NULL);
476 bshw_chip_reset(bsc);
478 bshw_chip_reset(bsc);
479 printf(" done. scsi bus ready.\n");
480 nextti = bsc->sc_titab.tqh_first;
484 if ((ti = nextti) == NULL)
486 nextti = ti->ti_tchain.tqe_next;
488 bits = (1 << ti->ti_id);
492 if ((error = bs_check_target(ti)) != COMPLETE)
496 TAILQ_REMOVE(&bsc->sc_titab, ti, ti_tchain);
497 bsc->sc_openf &= ~bits;
500 if (error == NOTARGET)
509 bsc->sc_hstate = BSC_RDY;
512 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
514 ti->ti_ctab = ti->ti_bctab;
515 TAILQ_INIT(&ti->ti_bctab);
516 if (ti->ti_ctab.tqh_first)
517 bscmdstart(ti, BSCMDSTART);
523 struct bs_softc *bsc;
525 struct targ_info *ti;
528 bsc->sc_flags &= ~(BSRESET | BSUNDERRESET);
531 bsc->sc_flags |= BSUNDERRESET;
535 /* host state clear */
537 BS_SETUP_MSGPHASE(FREE)
540 /* target state clear */
541 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
543 if (ti->ti_state == BS_TARG_SYNCH)
544 bs_analyze_syncmsg(ti, NULL);
545 if (ti->ti_state > BS_TARG_START)
546 BS_SETUP_TARGSTATE(BS_TARG_START);
548 BS_SETUP_PHASE(UNDEF)
549 bs_hostque_delete(bsc, ti);
550 if ((cb = ti->ti_ctab.tqh_first) != NULL)
552 if (bsc->sc_hstate == BSC_TARG_CHECK)
554 ti->ti_error |= BSFATALIO;
557 else if (cb->rcnt >= bsc->sc_retry)
559 ti->ti_error |= BSABNORMAL;
562 else if (ti->ti_error)
566 /* target state clear */
568 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
569 ti->ti_flags &= ~BSCFLAGSMASK;
572 ti->ti_flags &= ~BSNEXUS;
575 for ( ; cb; cb = cb->ccb_chain.tqe_next)
578 cb->bsccb_flags &= ~(BSITSDONE | BSCASTAT);
582 if (bsc->sc_hstate != BSC_TARG_CHECK &&
583 ti->ti_bctab.tqh_first == NULL)
584 ti->ti_bctab = ti->ti_ctab;
586 TAILQ_INIT(&ti->ti_ctab);
589 if (bsc->sc_hstate != BSC_TARG_CHECK)
590 bs_scsibus_start(bsc);
593 /**************************************************
594 * CHECK TARGETS AND START TARGETS
595 *************************************************/
598 struct targ_info *ti;
601 struct scsi_start_stop_unit cmd;
603 bzero(&cmd, sizeof(struct scsi_start_stop_unit));
604 cmd.opcode = START_STOP;
607 cb = bs_make_internal_ccb(ti, 0, (u_int8_t *) &cmd,
608 sizeof(struct scsi_start_stop_unit),
609 NULL, 0, BSFORCEIOPOLL, BS_MOTOR_TIMEOUT);
610 bscmdstart(ti, BSCMDSTART);
611 return bs_scsi_cmd_poll(ti, cb);
614 /* test unit ready and check ATN msgout response */
617 struct targ_info *ti;
619 struct bs_softc *bsc = ti->ti_bsc;
620 struct scsi_inquiry scsi_cmd;
621 struct scsi_inquiry_data scsi_inquiry_data;
623 int count, retry = bsc->sc_retry;
624 int s, error = COMPLETE;
631 bzero(&scsi_cmd, sizeof(scsi_cmd));
632 scsi_cmd.opcode = INQUIRY;
633 scsi_cmd.length = sizeof(struct scsi_inquiry_data);
634 cb = bs_make_internal_ccb(ti, 0,
635 (u_int8_t *) &scsi_cmd, sizeof(scsi_cmd),
636 (u_int8_t *) &scsi_inquiry_data,
637 sizeof(scsi_inquiry_data),
638 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
639 bscmdstart(ti, BSCMDSTART);
640 error = bs_scsi_cmd_poll(ti, cb);
641 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
643 ti->targ_type = scsi_inquiry_data.device;
644 ti->targ_support = scsi_inquiry_data.flags;
646 /* test unit ready twice */
647 for (count = 0; count < 2; count++)
649 cb = bs_make_internal_ccb(ti, 0, NULL, 0, NULL, 0,
650 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
651 bscmdstart(ti, BSCMDSTART);
652 error = bs_scsi_cmd_poll(ti, cb);
653 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
657 if (cb->bsccb_flags & BSCASTAT)
658 bs_printf(ti, "check", "could not clear CA state");
662 bsc->sc_retry = retry;
664 if (ti->ti_error & BSSELTIMEOUT)
667 if (error == COMPLETE)
668 error = bs_start_target(ti);
674 /**************************************************
676 **************************************************/
678 bs_init_target_info(bsc, target)
679 struct bs_softc *bsc;
682 struct targ_info *ti;
684 ti = malloc(sizeof(struct targ_info), M_DEVBUF, M_NOWAIT);
687 bs_printf(NULL, "bs_init_targ_info", "no target info memory");
691 bzero(ti, sizeof(*ti));
696 ti->ti_cfgflags = BS_SCSI_NOPARITY | BS_SCSI_NOSAT;
697 ti->ti_mflags = ~(BSSAT | BSDISC | BSSMIT | BSLINK);
698 BS_SETUP_TARGSTATE(BS_TARG_CTRL);
700 TAILQ_INIT(&ti->ti_ctab);
703 if (ti->bounce_addr == NULL)
709 TAILQ_INSERT_TAIL(&bsc->sc_titab, ti, ti_tchain);
710 bsc->sc_ti[target] = ti;
711 bsc->sc_openf |= (1 << target);
717 bs_setup_ctrl(ti, quirks, flags)
718 struct targ_info *ti;
722 struct bs_softc *bsc = ti->ti_bsc;
723 u_int offset, period, maxperiod;
725 if (ti->ti_state == BS_TARG_CTRL)
727 ti->ti_cfgflags = BS_SCSI_POSITIVE;
728 ti->ti_syncmax.offset = BSHW_MAX_OFFSET;
729 BS_SETUP_TARGSTATE(BS_TARG_START);
732 flags |= ti->ti_cfgflags & BS_SCSI_NEGATIVE;
734 #ifdef BS_TARG_SAFEMODE
735 if (ti->targ_type != 0)
737 flags &= ~(BS_SCSI_DISC | BS_SCSI_SYNC);
738 flags |= BS_SCSI_NOPARITY;
743 if (quirks & SDEV_NODISC)
744 flags &= ~BS_SCSI_DISC;
747 if (quirks & SDEV_NOPARITY)
748 flags |= BS_SCSI_NOPARITY;
751 if (quirks & SDEV_NOCMDLNK)
752 flags &= ~BS_SCSI_LINK;
755 if (quirks & SDEV_ASYNC)
756 flags &= ~BS_SCSI_SYNC;
759 if (quirks & SDEV_AUTOSAVE)
760 flags |= BS_SCSI_SAVESP;
763 if (quirks & SD_Q_NO_SYNC)
764 flags &= ~BS_SCSI_SYNC;
767 if ((flags & BS_SCSI_DISC) == 0 ||
768 (ti->targ_support & SID_Linked) == 0)
769 flags &= ~BS_SCSI_LINK;
771 ti->sm_offset = (flags & BS_SCSI_NOSMIT) ? 0 : bsc->sm_offset;
772 if (ti->sm_offset == 0)
773 flags |= BS_SCSI_NOSMIT;
774 else if (bsc->sc_cfgflags & BSC_SMITSAT_DISEN)
775 flags |= BS_SCSI_NOSAT;
777 flags &= (ti->ti_cfgflags & BS_SCSI_POSITIVE) | (~BS_SCSI_POSITIVE);
778 ti->ti_cfgflags = flags;
780 /* calculate synch setup */
781 period = BS_SCSI_PERIOD(flags);
782 offset = (flags & BS_SCSI_SYNC) ? BS_SCSI_OFFSET(flags) : 0;
784 maxperiod = (bsc->sc_cspeed & IDR_FS_15_20) ? 100 : 50;
785 if (period > maxperiod)
789 period = 2500 / period;
791 if (ti->ti_syncmax.offset > offset)
792 ti->ti_syncmax.offset = offset;
793 if (ti->ti_syncmax.period < period)
794 ti->ti_syncmax.period = period;
796 bshw_adj_syncdata(&ti->ti_syncmax);
798 /* finally report our info */
799 printf("%s(%d:%d): {%d:0x%x:0x%x:%s} flags 0x%b\n",
800 bsc->sc_dvname, ti->ti_id, ti->ti_lun,
801 (u_int) ti->targ_type,
802 (u_int) ti->targ_support,
803 (u_int) ti->bounce_size,
804 (flags & BS_SCSI_NOSMIT) ? "dma" : "pdma",
805 flags, BS_SCSI_BITS);
807 /* internal representation */
809 if ((ti->ti_cfgflags & BS_SCSI_DISC) == 0)
810 ti->ti_mflags &= ~BSDISC;
811 if ((ti->ti_cfgflags & BS_SCSI_LINK) == 0)
812 ti->ti_mflags &= ~BSLINK;
813 if (ti->ti_cfgflags & BS_SCSI_NOSAT)
814 ti->ti_mflags &= ~BSSAT;
815 if (ti->ti_cfgflags & BS_SCSI_NOSMIT)
816 ti->ti_mflags &= ~BSSMIT;
819 /**************************************************
821 **************************************************/
824 struct targ_info *ti;
830 printf("%s(%d:%d): <%s> %s\n",
831 ti->ti_bsc->sc_dvname, ti->ti_id, ti->ti_lun, ph, c);
833 printf("bs*(*:*): <%s> %s\n", ph, c);
838 struct bs_softc *bsc;
842 panic("%s %s\n", bsc->sc_dvname, c);
845 /**************************************************
847 **************************************************/
848 #ifdef BS_DEBUG_ROUTINE
868 #endif /* BS_DEBUG_ROUTINE */
871 bs_debug_print_all(bsc)
872 struct bs_softc *bsc;
874 struct targ_info *ti;
876 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
877 bs_debug_print(bsc, ti);
880 static u_char *phase[] =
882 "FREE", "HOSTQUE", "DISC", "COMPMSG", "ATN", "DISCMSG", "SELECT",
883 "SELECTED", "RESELECTED", "MSGIN", "MSGOUT", "STATIN", "CMDOUT",
884 "DATA", "SATSEL", "SATRESEL", "SATSDP", "SATCOMPSEQ", "UNDEF",
888 bs_debug_print(bsc, ti)
889 struct bs_softc *bsc;
890 struct targ_info *ti;
895 printf("%s <DEBUG INFO> nexus %lx bs %lx bus status %lx \n",
896 bsc->sc_dvname, (u_long) ti, (u_long) bsc->sc_nexus, (u_long) bsc->sc_busstat);
901 struct sc_p *sp = &bsc->sc_p;
903 printf("%s(%d:%d) ph<%s> ", bsc->sc_dvname, ti->ti_id,
904 ti->ti_lun, phase[(int) ti->ti_phase]);
905 printf("msgptr %x msg[0] %x status %x tqh %lx fl %x\n",
906 (u_int) (ti->ti_msginptr), (u_int) (ti->ti_msgin[0]),
907 ti->ti_status, (u_long) (cb = ti->ti_ctab.tqh_first),
910 printf("cmdlen %x cmdaddr %lx cmd[0] %x\n",
911 cb->cmdlen, (u_long) cb->cmd, (int) cb->cmd[0]);
912 printf("datalen %x dataaddr %lx seglen %x ",
913 sp->datalen, (u_long) sp->data, sp->seglen);
915 printf("odatalen %x flags %x\n",
916 cb->datalen, cb->bsccb_flags);
919 printf("error flags %b\n", ti->ti_error, BSERRORBITS);