1 /* $NecBSD: bsfunc.c,v 1.2 1997/10/31 17:43:37 honda Exp $ */
5 * [NetBSD for NEC PC98 series]
6 * Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1994, 1995, 1996 Naofumi HONDA. All rights reserved.
37 #include <i386/Cbus/dev/bs/bsif.h>
40 #include <i386/isa/bs/bsif.h>
44 struct bs_statics bs_statics[NTARGETS];
45 u_int bs_linkcmd_count[NTARGETS];
46 u_int bs_bounce_used[NTARGETS];
47 #endif /* BS_STATICS */
50 int bs_debug_flag = 0;
53 static void bs_print_syncmsg __P((struct targ_info *, char*));
54 static void bs_timeout_target __P((struct targ_info *));
55 static void bs_kill_msg __P((struct bsccb *cb));
57 static int bs_start_target __P((struct targ_info *));
58 static int bs_check_target __P((struct targ_info *));
60 /*************************************************************
62 ************************************************************/
63 GENERIC_CCB_STATIC_ALLOC(bs, bsccb)
64 GENERIC_CCB(bs, bsccb, ccb_chain)
66 /*************************************************************
68 ************************************************************/
73 struct bs_softc *bsc = ti->ti_bsc;
75 ti->ti_error |= BSTIMEOUT;
76 bsc->sc_flags |= BSRESET;
78 if (ti->ti_herrcnt ++ >= HARDRETRIES)
80 bs_printf(ti, "timeout", "async transfer!");
81 ti->ti_syncmax.period = ti->ti_syncmax.offset = 0;
89 struct bs_softc *bsc = (struct bs_softc *) arg;
95 bsc->sc_flags &= ~BSSTARTTIMEOUT;
98 if ((ti = bsc->sc_nexus) && (cb = ti->ti_ctab.tqh_first))
100 if ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0)
101 bs_timeout_target(ti);
103 else for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
105 if (bsc->sc_dtgnum && ti->ti_phase < DISCONNECTED)
108 cb = ti->ti_ctab.tqh_first;
109 if (cb && ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0))
110 bs_timeout_target(ti);
114 if (bsc->sc_flags & BSRESET)
116 bs_debug_print_all(bsc);
117 bs_printf(ti, "timeout", "bus hang up");
121 bs_start_timeout(bsc);
125 /**************************************************
127 *************************************************/
128 static u_int8_t cmd_unit_ready[6];
131 bs_make_internal_ccb(ti, lun, cmd, cmdlen, data, datalen, flags, timeout)
132 struct targ_info *ti;
143 if ((cb = bs_get_ccb()) == NULL)
144 bs_panic(ti->ti_bsc, "can not get ccb mem");
148 cb->cmd = (cmd ? cmd : cmd_unit_ready);
149 cb->cmdlen = (cmd ? cmdlen : sizeof(cmd_unit_ready));
151 cb->datalen = (data ? datalen : 0);
153 cb->bsccb_flags = flags & BSCFLAGSMASK;
154 bs_targ_flags(ti, cb);
156 cb->tcmax = (timeout > BS_DEFAULT_TIMEOUT_SECOND ? timeout :
157 BS_DEFAULT_TIMEOUT_SECOND);
159 TAILQ_INSERT_HEAD(&ti->ti_ctab, cb, ccb_chain);
165 bs_make_msg_ccb(ti, lun, cb, msg, timex)
166 struct targ_info *ti;
174 flags = BSFORCEIOPOLL | msg->flag;
176 cb = bs_make_internal_ccb(ti, lun, NULL, 0, NULL, 0,
179 cb->bsccb_flags |= flags & BSCFLAGSMASK;
181 cb->msgoutlen = msg->msglen;
182 bcopy(msg->msg, cb->msgout, msg->msglen);
187 bs_send_msg(ti, lun, msg, timex)
188 struct targ_info *ti;
195 cb = bs_make_msg_ccb(ti, lun, NULL, msg, timex);
196 bscmdstart(ti, BSCMDSTART);
197 return bs_scsi_cmd_poll(ti, cb);
207 /**************************************************
209 **************************************************/
212 struct targ_info *ti;
216 bzero(ti->scsi_cmd, sizeof(struct scsi_sense));
217 bzero(&ti->sense, sizeof(struct scsi_sense_data));
218 ti->scsi_cmd[0] = REQUEST_SENSE;
219 ti->scsi_cmd[1] = (ti->ti_lun << 5);
220 ti->scsi_cmd[4] = sizeof(struct scsi_sense_data);
221 cb = bs_make_internal_ccb(ti, ti->ti_lun, ti->scsi_cmd,
222 sizeof(struct scsi_sense),
223 (u_int8_t *) & ti->sense,
224 sizeof(struct scsi_sense_data),
226 BS_DEFAULT_TIMEOUT_SECOND);
227 cb->bsccb_flags |= BSSENSECCB;
231 /**************************************************
233 *************************************************/
236 bs_start_syncmsg(ti, cb, flag)
237 struct targ_info *ti;
241 struct syncdata *negp, *maxp;
245 negp = &ti->ti_syncnow;
246 maxp = &ti->ti_syncmax;
248 ti->ti_state = BS_TARG_SYNCH;
250 if (flag == BS_SYNCMSG_REQUESTED)
252 if (negp->offset > maxp->offset)
253 negp->offset = maxp->offset;
254 if (negp->offset != 0 && negp->period < maxp->period)
255 negp->period = maxp->period;
260 cb = ti->ti_ctab.tqh_first;
262 else if (ti->ti_cfgflags & BS_SCSI_SYNC)
264 negp->offset = maxp->offset;
265 negp->period = maxp->period;
267 msg.flag = BSERROROK;
272 ti->ti_state = BS_TARG_RDY;
276 BS_SETUP_SYNCSTATE(flag);
277 msg.msg[0] = MSG_EXTEND;
278 msg.msg[1] = MSG_EXTEND_SYNCHLEN;
279 msg.msg[2] = MSG_EXTEND_SYNCHCODE;
280 msg.msg[3] = negp->period;
281 msg.msg[4] = negp->offset;
282 msg.msglen = MSG_EXTEND_SYNCHLEN + 2;
284 bs_make_msg_ccb(ti, lun, cb, &msg, BS_SYNC_TIMEOUT);
289 bs_print_syncmsg(ti, s)
290 struct targ_info *ti;
293 struct bs_softc *bsc = ti->ti_bsc;
294 struct syncdata *negp;
297 negp = &ti->ti_syncnow;
298 speed = (negp->offset && negp->period) ?
299 (2500 / ((u_int) negp->period)) : 0;
301 printf("%s(%d:%d): <%s> ", bsc->sc_dvname, ti->ti_id, ti->ti_lun, s);
302 printf("period 0x%x offset %d chip (0x%x)", negp->period, negp->offset,
305 printf(" %d.%d M/s", speed / 10, speed % 10);
310 bs_analyze_syncmsg(ti, cb)
311 struct targ_info *ti;
314 struct bs_softc *bsc = ti->ti_bsc;
315 u_int8_t ans = ti->ti_syncnow.state;
316 struct syncdata *negp, *maxp;
317 struct syncdata bdata;
321 negp = &ti->ti_syncnow;
323 maxp = &ti->ti_syncmax;
327 case BS_SYNCMSG_REJECT:
332 case BS_SYNCMSG_ASSERT:
338 if (negp->offset != 0 && negp->period < maxp->period)
341 s = "illegal(period)";
343 else if (negp->offset > maxp->offset)
346 s = "illegal(offset)";
349 period = negp->offset ? negp->period : 0;
355 bshw_adj_syncdata(negp);
358 if (ans == BS_SYNCMSG_REQUESTED)
361 s = negp->offset ? "synchronous" : "async";
365 negp->offset = maxp->offset = 0;
366 bshw_adj_syncdata(negp);
367 bshw_adj_syncdata(maxp);
370 /* really setup hardware */
371 bshw_set_synchronous(bsc, ti);
372 if (cb == NULL || (period >= negp->period && period <= negp->period + 2))
374 bs_print_syncmsg(ti, s);
375 BS_SETUP_TARGSTATE(BS_TARG_RDY);
376 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
384 bs_printf(ti, "bs_analyze_syncmsg",
385 "sync(period) mismatch, retry neg...");
386 printf("expect(%d:0x%x) => reply(%d:0x%x)\n",
387 bdata.offset, bdata.period, negp->offset, negp->period);
389 bs_start_syncmsg(ti, cb, BS_SYNCMSG_ASSERT);
394 /**************************************************
395 * ABORT AND RESET MSG
396 **************************************************/
397 /* send device reset msg and wait */
400 struct targ_info *ti;
405 msg.msg[0] = MSG_RESET;
408 bs_send_msg(ti, 0, &msg, 0);
410 delay(ti->ti_bsc->sc_RSTdelay);
417 struct targ_info *ti;
419 struct bs_softc *bsc = ti->ti_bsc;
421 struct bsccb *cb = ti->ti_ctab.tqh_first;
433 msg.msg[0] = MSG_ABORT;
436 cb = bs_make_msg_ccb(ti, lun, NULL, &msg, 0);
437 bscmdstart(ti, BSCMDSTART);
439 if (bsc->sc_nexus == ti)
445 /**************************************************
446 * COMPLETE SCSI BUS RESET
447 *************************************************/
450 * 1) reset scsi bus (ie. all target reseted).
452 * 3) check target status.
453 * 4) sync neg with all targets.
454 * 5) setup sync reg in host.
455 * 6) recover previous nexus.
458 bs_scsibus_start(bsc)
459 struct bs_softc *bsc;
461 struct targ_info *ti, *nextti = NULL;
462 int error = HASERROR;
463 u_int querm, bits, skip = 0;
465 querm = (bsc->sc_hstate == BSC_BOOTUP);
466 bsc->sc_hstate = BSC_TARG_CHECK;
471 if (error != COMPLETE)
473 printf("%s: scsi bus reset and try to restart ...",
476 bshw_dmaabort(bsc, NULL);
477 bshw_chip_reset(bsc);
479 bshw_chip_reset(bsc);
480 printf(" done. scsi bus ready.\n");
481 nextti = bsc->sc_titab.tqh_first;
485 if ((ti = nextti) == NULL)
487 nextti = ti->ti_tchain.tqe_next;
489 bits = (1 << ti->ti_id);
493 if ((error = bs_check_target(ti)) != COMPLETE)
497 TAILQ_REMOVE(&bsc->sc_titab, ti, ti_tchain);
498 bsc->sc_openf &= ~bits;
501 if (error == NOTARGET)
510 bsc->sc_hstate = BSC_RDY;
513 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
515 ti->ti_ctab = ti->ti_bctab;
516 TAILQ_INIT(&ti->ti_bctab);
517 if (ti->ti_ctab.tqh_first)
518 bscmdstart(ti, BSCMDSTART);
524 struct bs_softc *bsc;
526 struct targ_info *ti;
529 bsc->sc_flags &= ~(BSRESET | BSUNDERRESET);
532 bsc->sc_flags |= BSUNDERRESET;
536 /* host state clear */
538 BS_SETUP_MSGPHASE(FREE)
541 /* target state clear */
542 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
544 if (ti->ti_state == BS_TARG_SYNCH)
545 bs_analyze_syncmsg(ti, NULL);
546 if (ti->ti_state > BS_TARG_START)
547 BS_SETUP_TARGSTATE(BS_TARG_START);
549 BS_SETUP_PHASE(UNDEF)
550 bs_hostque_delete(bsc, ti);
551 if ((cb = ti->ti_ctab.tqh_first) != NULL)
553 if (bsc->sc_hstate == BSC_TARG_CHECK)
555 ti->ti_error |= BSFATALIO;
558 else if (cb->rcnt >= bsc->sc_retry)
560 ti->ti_error |= BSABNORMAL;
563 else if (ti->ti_error)
567 /* target state clear */
569 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
570 ti->ti_flags &= ~BSCFLAGSMASK;
573 ti->ti_flags &= ~BSNEXUS;
576 for ( ; cb; cb = cb->ccb_chain.tqe_next)
579 cb->bsccb_flags &= ~(BSITSDONE | BSCASTAT);
583 if (bsc->sc_hstate != BSC_TARG_CHECK &&
584 ti->ti_bctab.tqh_first == NULL)
585 ti->ti_bctab = ti->ti_ctab;
587 TAILQ_INIT(&ti->ti_ctab);
590 if (bsc->sc_hstate != BSC_TARG_CHECK)
591 bs_scsibus_start(bsc);
594 /**************************************************
595 * CHECK TARGETS AND START TARGETS
596 *************************************************/
599 struct targ_info *ti;
602 struct scsi_start_stop_unit cmd;
604 bzero(&cmd, sizeof(struct scsi_start_stop_unit));
605 cmd.opcode = START_STOP;
608 cb = bs_make_internal_ccb(ti, 0, (u_int8_t *) &cmd,
609 sizeof(struct scsi_start_stop_unit),
610 NULL, 0, BSFORCEIOPOLL, BS_MOTOR_TIMEOUT);
611 bscmdstart(ti, BSCMDSTART);
612 return bs_scsi_cmd_poll(ti, cb);
615 /* test unit ready and check ATN msgout response */
618 struct targ_info *ti;
620 struct bs_softc *bsc = ti->ti_bsc;
621 struct scsi_inquiry scsi_cmd;
622 struct scsi_inquiry_data scsi_inquiry_data;
624 int count, retry = bsc->sc_retry;
625 int s, error = COMPLETE;
632 bzero(&scsi_cmd, sizeof(scsi_cmd));
633 scsi_cmd.opcode = INQUIRY;
634 scsi_cmd.length = sizeof(struct scsi_inquiry_data);
635 cb = bs_make_internal_ccb(ti, 0,
636 (u_int8_t *) &scsi_cmd, sizeof(scsi_cmd),
637 (u_int8_t *) &scsi_inquiry_data,
638 sizeof(scsi_inquiry_data),
639 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
640 bscmdstart(ti, BSCMDSTART);
641 error = bs_scsi_cmd_poll(ti, cb);
642 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
644 ti->targ_type = scsi_inquiry_data.device;
645 ti->targ_support = scsi_inquiry_data.flags;
647 /* test unit ready twice */
648 for (count = 0; count < 2; count++)
650 cb = bs_make_internal_ccb(ti, 0, NULL, 0, NULL, 0,
651 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
652 bscmdstart(ti, BSCMDSTART);
653 error = bs_scsi_cmd_poll(ti, cb);
654 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
658 if (cb->bsccb_flags & BSCASTAT)
659 bs_printf(ti, "check", "could not clear CA state");
663 bsc->sc_retry = retry;
665 if (ti->ti_error & BSSELTIMEOUT)
668 if (error == COMPLETE)
669 error = bs_start_target(ti);
675 /**************************************************
677 **************************************************/
679 bs_init_target_info(bsc, target)
680 struct bs_softc *bsc;
683 struct targ_info *ti;
685 ti = malloc(sizeof(struct targ_info), M_DEVBUF, M_NOWAIT);
688 bs_printf(NULL, "bs_init_targ_info", "no target info memory");
692 bzero(ti, sizeof(*ti));
697 ti->ti_cfgflags = BS_SCSI_NOPARITY | BS_SCSI_NOSAT;
698 ti->ti_mflags = ~(BSSAT | BSDISC | BSSMIT | BSLINK);
699 BS_SETUP_TARGSTATE(BS_TARG_CTRL);
701 TAILQ_INIT(&ti->ti_ctab);
704 if (ti->bounce_addr == NULL)
710 TAILQ_INSERT_TAIL(&bsc->sc_titab, ti, ti_tchain);
711 bsc->sc_ti[target] = ti;
712 bsc->sc_openf |= (1 << target);
718 bs_setup_ctrl(ti, quirks, flags)
719 struct targ_info *ti;
723 struct bs_softc *bsc = ti->ti_bsc;
724 u_int offset, period, maxperiod;
726 if (ti->ti_state == BS_TARG_CTRL)
728 ti->ti_cfgflags = BS_SCSI_POSITIVE;
729 ti->ti_syncmax.offset = BSHW_MAX_OFFSET;
730 BS_SETUP_TARGSTATE(BS_TARG_START);
733 flags |= ti->ti_cfgflags & BS_SCSI_NEGATIVE;
735 #ifdef BS_TARG_SAFEMODE
736 if (ti->targ_type != 0)
738 flags &= ~(BS_SCSI_DISC | BS_SCSI_SYNC);
739 flags |= BS_SCSI_NOPARITY;
744 if (quirks & SDEV_NODISC)
745 flags &= ~BS_SCSI_DISC;
748 if (quirks & SDEV_NOPARITY)
749 flags |= BS_SCSI_NOPARITY;
752 if (quirks & SDEV_NOCMDLNK)
753 flags &= ~BS_SCSI_LINK;
756 if (quirks & SDEV_ASYNC)
757 flags &= ~BS_SCSI_SYNC;
760 if (quirks & SDEV_AUTOSAVE)
761 flags |= BS_SCSI_SAVESP;
764 if (quirks & SD_Q_NO_SYNC)
765 flags &= ~BS_SCSI_SYNC;
768 if ((flags & BS_SCSI_DISC) == 0 ||
769 (ti->targ_support & SID_Linked) == 0)
770 flags &= ~BS_SCSI_LINK;
772 ti->sm_offset = (flags & BS_SCSI_NOSMIT) ? 0 : bsc->sm_offset;
773 if (ti->sm_offset == 0)
774 flags |= BS_SCSI_NOSMIT;
775 else if (bsc->sc_cfgflags & BSC_SMITSAT_DISEN)
776 flags |= BS_SCSI_NOSAT;
778 flags &= (ti->ti_cfgflags & BS_SCSI_POSITIVE) | (~BS_SCSI_POSITIVE);
779 ti->ti_cfgflags = flags;
781 /* calculate synch setup */
782 period = BS_SCSI_PERIOD(flags);
783 offset = (flags & BS_SCSI_SYNC) ? BS_SCSI_OFFSET(flags) : 0;
785 maxperiod = (bsc->sc_cspeed & IDR_FS_15_20) ? 100 : 50;
786 if (period > maxperiod)
790 period = 2500 / period;
792 if (ti->ti_syncmax.offset > offset)
793 ti->ti_syncmax.offset = offset;
794 if (ti->ti_syncmax.period < period)
795 ti->ti_syncmax.period = period;
797 bshw_adj_syncdata(&ti->ti_syncmax);
799 /* finally report our info */
800 printf("%s(%d:%d): {%d:0x%x:0x%x:%s} flags 0x%b\n",
801 bsc->sc_dvname, ti->ti_id, ti->ti_lun,
802 (u_int) ti->targ_type,
803 (u_int) ti->targ_support,
804 (u_int) ti->bounce_size,
805 (flags & BS_SCSI_NOSMIT) ? "dma" : "pdma",
806 flags, BS_SCSI_BITS);
808 /* internal representation */
810 if ((ti->ti_cfgflags & BS_SCSI_DISC) == 0)
811 ti->ti_mflags &= ~BSDISC;
812 if ((ti->ti_cfgflags & BS_SCSI_LINK) == 0)
813 ti->ti_mflags &= ~BSLINK;
814 if (ti->ti_cfgflags & BS_SCSI_NOSAT)
815 ti->ti_mflags &= ~BSSAT;
816 if (ti->ti_cfgflags & BS_SCSI_NOSMIT)
817 ti->ti_mflags &= ~BSSMIT;
820 /**************************************************
822 **************************************************/
825 struct targ_info *ti;
831 printf("%s(%d:%d): <%s> %s\n",
832 ti->ti_bsc->sc_dvname, ti->ti_id, ti->ti_lun, ph, c);
834 printf("bs*(*:*): <%s> %s\n", ph, c);
839 struct bs_softc *bsc;
843 panic("%s %s\n", bsc->sc_dvname, c);
846 /**************************************************
848 **************************************************/
849 #ifdef BS_DEBUG_ROUTINE
869 #endif /* BS_DEBUG_ROUTINE */
872 bs_debug_print_all(bsc)
873 struct bs_softc *bsc;
875 struct targ_info *ti;
877 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
878 bs_debug_print(bsc, ti);
881 static u_char *phase[] =
883 "FREE", "HOSTQUE", "DISC", "COMPMSG", "ATN", "DISCMSG", "SELECT",
884 "SELECTED", "RESELECTED", "MSGIN", "MSGOUT", "STATIN", "CMDOUT",
885 "DATA", "SATSEL", "SATRESEL", "SATSDP", "SATCOMPSEQ", "UNDEF",
889 bs_debug_print(bsc, ti)
890 struct bs_softc *bsc;
891 struct targ_info *ti;
896 printf("%s <DEBUG INFO> nexus %lx bs %lx bus status %lx \n",
897 bsc->sc_dvname, (u_long) ti, (u_long) bsc->sc_nexus, (u_long) bsc->sc_busstat);
902 struct sc_p *sp = &bsc->sc_p;
904 printf("%s(%d:%d) ph<%s> ", bsc->sc_dvname, ti->ti_id,
905 ti->ti_lun, phase[(int) ti->ti_phase]);
906 printf("msgptr %x msg[0] %x status %x tqh %lx fl %x\n",
907 (u_int) (ti->ti_msginptr), (u_int) (ti->ti_msgin[0]),
908 ti->ti_status, (u_long) (cb = ti->ti_ctab.tqh_first),
911 printf("cmdlen %x cmdaddr %lx cmd[0] %x\n",
912 cb->cmdlen, (u_long) cb->cmd, (int) cb->cmd[0]);
913 printf("datalen %x dataaddr %lx seglen %x ",
914 sp->datalen, (u_long) sp->data, sp->seglen);
916 printf("odatalen %x flags %x\n",
917 cb->datalen, cb->bsccb_flags);
920 printf("error flags %b\n", ti->ti_error, BSERRORBITS);