1 /* $NecBSD: bsfunc.c,v 1.2 1997/10/31 17:43:37 honda Exp $ */
3 /* $FreeBSD: src/sys/i386/isa/bs/bsfunc.c,v 1.7.2.2 2001/07/26 02:32:18 nyan Exp $ */
4 /* $DragonFly: src/sys/dev/disk/i386/bs/Attic/bsfunc.c,v 1.3 2003/08/07 21:16:52 dillon Exp $ */
6 * [NetBSD for NEC PC98 series]
7 * Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1994, 1995, 1996 Naofumi HONDA. All rights reserved.
38 #include <i386/Cbus/dev/bs/bsif.h>
45 struct bs_statics bs_statics[NTARGETS];
46 u_int bs_linkcmd_count[NTARGETS];
47 u_int bs_bounce_used[NTARGETS];
48 #endif /* BS_STATICS */
51 int bs_debug_flag = 0;
54 static void bs_print_syncmsg __P((struct targ_info *, char*));
55 static void bs_timeout_target __P((struct targ_info *));
56 static void bs_kill_msg __P((struct bsccb *cb));
58 static int bs_start_target __P((struct targ_info *));
59 static int bs_check_target __P((struct targ_info *));
61 /*************************************************************
63 ************************************************************/
64 GENERIC_CCB_STATIC_ALLOC(bs, bsccb)
65 GENERIC_CCB(bs, bsccb, ccb_chain)
67 /*************************************************************
69 ************************************************************/
74 struct bs_softc *bsc = ti->ti_bsc;
76 ti->ti_error |= BSTIMEOUT;
77 bsc->sc_flags |= BSRESET;
79 if (ti->ti_herrcnt ++ >= HARDRETRIES)
81 bs_printf(ti, "timeout", "async transfer!");
82 ti->ti_syncmax.period = ti->ti_syncmax.offset = 0;
90 struct bs_softc *bsc = (struct bs_softc *) arg;
96 bsc->sc_flags &= ~BSSTARTTIMEOUT;
99 if ((ti = bsc->sc_nexus) && (cb = ti->ti_ctab.tqh_first))
101 if ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0)
102 bs_timeout_target(ti);
104 else for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
106 if (bsc->sc_dtgnum && ti->ti_phase < DISCONNECTED)
109 cb = ti->ti_ctab.tqh_first;
110 if (cb && ((cb->tc -= BS_TIMEOUT_CHECK_INTERVAL) < 0))
111 bs_timeout_target(ti);
115 if (bsc->sc_flags & BSRESET)
117 bs_debug_print_all(bsc);
118 bs_printf(ti, "timeout", "bus hang up");
122 bs_start_timeout(bsc);
126 /**************************************************
128 *************************************************/
129 static u_int8_t cmd_unit_ready[6];
132 bs_make_internal_ccb(ti, lun, cmd, cmdlen, data, datalen, flags, timeout)
133 struct targ_info *ti;
144 if ((cb = bs_get_ccb()) == NULL)
145 bs_panic(ti->ti_bsc, "can not get ccb mem");
149 cb->cmd = (cmd ? cmd : cmd_unit_ready);
150 cb->cmdlen = (cmd ? cmdlen : sizeof(cmd_unit_ready));
152 cb->datalen = (data ? datalen : 0);
154 cb->bsccb_flags = flags & BSCFLAGSMASK;
155 bs_targ_flags(ti, cb);
157 cb->tcmax = (timeout > BS_DEFAULT_TIMEOUT_SECOND ? timeout :
158 BS_DEFAULT_TIMEOUT_SECOND);
160 TAILQ_INSERT_HEAD(&ti->ti_ctab, cb, ccb_chain);
166 bs_make_msg_ccb(ti, lun, cb, msg, timex)
167 struct targ_info *ti;
175 flags = BSFORCEIOPOLL | msg->flag;
177 cb = bs_make_internal_ccb(ti, lun, NULL, 0, NULL, 0,
180 cb->bsccb_flags |= flags & BSCFLAGSMASK;
182 cb->msgoutlen = msg->msglen;
183 bcopy(msg->msg, cb->msgout, msg->msglen);
188 bs_send_msg(ti, lun, msg, timex)
189 struct targ_info *ti;
196 cb = bs_make_msg_ccb(ti, lun, NULL, msg, timex);
197 bscmdstart(ti, BSCMDSTART);
198 return bs_scsi_cmd_poll(ti, cb);
208 /**************************************************
210 **************************************************/
213 struct targ_info *ti;
217 bzero(ti->scsi_cmd, sizeof(struct scsi_sense));
218 bzero(&ti->sense, sizeof(struct scsi_sense_data));
219 ti->scsi_cmd[0] = REQUEST_SENSE;
220 ti->scsi_cmd[1] = (ti->ti_lun << 5);
221 ti->scsi_cmd[4] = sizeof(struct scsi_sense_data);
222 cb = bs_make_internal_ccb(ti, ti->ti_lun, ti->scsi_cmd,
223 sizeof(struct scsi_sense),
224 (u_int8_t *) & ti->sense,
225 sizeof(struct scsi_sense_data),
227 BS_DEFAULT_TIMEOUT_SECOND);
228 cb->bsccb_flags |= BSSENSECCB;
232 /**************************************************
234 *************************************************/
237 bs_start_syncmsg(ti, cb, flag)
238 struct targ_info *ti;
242 struct syncdata *negp, *maxp;
246 negp = &ti->ti_syncnow;
247 maxp = &ti->ti_syncmax;
249 ti->ti_state = BS_TARG_SYNCH;
251 if (flag == BS_SYNCMSG_REQUESTED)
253 if (negp->offset > maxp->offset)
254 negp->offset = maxp->offset;
255 if (negp->offset != 0 && negp->period < maxp->period)
256 negp->period = maxp->period;
261 cb = ti->ti_ctab.tqh_first;
263 else if (ti->ti_cfgflags & BS_SCSI_SYNC)
265 negp->offset = maxp->offset;
266 negp->period = maxp->period;
268 msg.flag = BSERROROK;
273 ti->ti_state = BS_TARG_RDY;
277 BS_SETUP_SYNCSTATE(flag);
278 msg.msg[0] = MSG_EXTEND;
279 msg.msg[1] = MSG_EXTEND_SYNCHLEN;
280 msg.msg[2] = MSG_EXTEND_SYNCHCODE;
281 msg.msg[3] = negp->period;
282 msg.msg[4] = negp->offset;
283 msg.msglen = MSG_EXTEND_SYNCHLEN + 2;
285 bs_make_msg_ccb(ti, lun, cb, &msg, BS_SYNC_TIMEOUT);
290 bs_print_syncmsg(ti, s)
291 struct targ_info *ti;
294 struct bs_softc *bsc = ti->ti_bsc;
295 struct syncdata *negp;
298 negp = &ti->ti_syncnow;
299 speed = (negp->offset && negp->period) ?
300 (2500 / ((u_int) negp->period)) : 0;
302 printf("%s(%d:%d): <%s> ", bsc->sc_dvname, ti->ti_id, ti->ti_lun, s);
303 printf("period 0x%x offset %d chip (0x%x)", negp->period, negp->offset,
306 printf(" %d.%d M/s", speed / 10, speed % 10);
311 bs_analyze_syncmsg(ti, cb)
312 struct targ_info *ti;
315 struct bs_softc *bsc = ti->ti_bsc;
316 u_int8_t ans = ti->ti_syncnow.state;
317 struct syncdata *negp, *maxp;
318 struct syncdata bdata;
322 negp = &ti->ti_syncnow;
324 maxp = &ti->ti_syncmax;
328 case BS_SYNCMSG_REJECT:
333 case BS_SYNCMSG_ASSERT:
339 if (negp->offset != 0 && negp->period < maxp->period)
342 s = "illegal(period)";
344 else if (negp->offset > maxp->offset)
347 s = "illegal(offset)";
350 period = negp->offset ? negp->period : 0;
356 bshw_adj_syncdata(negp);
359 if (ans == BS_SYNCMSG_REQUESTED)
362 s = negp->offset ? "synchronous" : "async";
366 negp->offset = maxp->offset = 0;
367 bshw_adj_syncdata(negp);
368 bshw_adj_syncdata(maxp);
371 /* really setup hardware */
372 bshw_set_synchronous(bsc, ti);
373 if (cb == NULL || (period >= negp->period && period <= negp->period + 2))
375 bs_print_syncmsg(ti, s);
376 BS_SETUP_TARGSTATE(BS_TARG_RDY);
377 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
385 bs_printf(ti, "bs_analyze_syncmsg",
386 "sync(period) mismatch, retry neg...");
387 printf("expect(%d:0x%x) => reply(%d:0x%x)\n",
388 bdata.offset, bdata.period, negp->offset, negp->period);
390 bs_start_syncmsg(ti, cb, BS_SYNCMSG_ASSERT);
395 /**************************************************
396 * ABORT AND RESET MSG
397 **************************************************/
398 /* send device reset msg and wait */
401 struct targ_info *ti;
406 msg.msg[0] = MSG_RESET;
409 bs_send_msg(ti, 0, &msg, 0);
411 delay(ti->ti_bsc->sc_RSTdelay);
418 struct targ_info *ti;
420 struct bs_softc *bsc = ti->ti_bsc;
422 struct bsccb *cb = ti->ti_ctab.tqh_first;
434 msg.msg[0] = MSG_ABORT;
437 cb = bs_make_msg_ccb(ti, lun, NULL, &msg, 0);
438 bscmdstart(ti, BSCMDSTART);
440 if (bsc->sc_nexus == ti)
446 /**************************************************
447 * COMPLETE SCSI BUS RESET
448 *************************************************/
451 * 1) reset scsi bus (ie. all target reseted).
453 * 3) check target status.
454 * 4) sync neg with all targets.
455 * 5) setup sync reg in host.
456 * 6) recover previous nexus.
459 bs_scsibus_start(bsc)
460 struct bs_softc *bsc;
462 struct targ_info *ti, *nextti = NULL;
463 int error = HASERROR;
464 u_int querm, bits, skip = 0;
466 querm = (bsc->sc_hstate == BSC_BOOTUP);
467 bsc->sc_hstate = BSC_TARG_CHECK;
472 if (error != COMPLETE)
474 printf("%s: scsi bus reset and try to restart ...",
477 bshw_dmaabort(bsc, NULL);
478 bshw_chip_reset(bsc);
480 bshw_chip_reset(bsc);
481 printf(" done. scsi bus ready.\n");
482 nextti = bsc->sc_titab.tqh_first;
486 if ((ti = nextti) == NULL)
488 nextti = ti->ti_tchain.tqe_next;
490 bits = (1 << ti->ti_id);
494 if ((error = bs_check_target(ti)) != COMPLETE)
498 TAILQ_REMOVE(&bsc->sc_titab, ti, ti_tchain);
499 bsc->sc_openf &= ~bits;
502 if (error == NOTARGET)
511 bsc->sc_hstate = BSC_RDY;
514 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
516 ti->ti_ctab = ti->ti_bctab;
517 TAILQ_INIT(&ti->ti_bctab);
518 if (ti->ti_ctab.tqh_first)
519 bscmdstart(ti, BSCMDSTART);
525 struct bs_softc *bsc;
527 struct targ_info *ti;
530 bsc->sc_flags &= ~(BSRESET | BSUNDERRESET);
533 bsc->sc_flags |= BSUNDERRESET;
537 /* host state clear */
539 BS_SETUP_MSGPHASE(FREE)
542 /* target state clear */
543 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
545 if (ti->ti_state == BS_TARG_SYNCH)
546 bs_analyze_syncmsg(ti, NULL);
547 if (ti->ti_state > BS_TARG_START)
548 BS_SETUP_TARGSTATE(BS_TARG_START);
550 BS_SETUP_PHASE(UNDEF)
551 bs_hostque_delete(bsc, ti);
552 if ((cb = ti->ti_ctab.tqh_first) != NULL)
554 if (bsc->sc_hstate == BSC_TARG_CHECK)
556 ti->ti_error |= BSFATALIO;
559 else if (cb->rcnt >= bsc->sc_retry)
561 ti->ti_error |= BSABNORMAL;
564 else if (ti->ti_error)
568 /* target state clear */
570 BS_SETUP_SYNCSTATE(BS_SYNCMSG_NULL);
571 ti->ti_flags &= ~BSCFLAGSMASK;
574 ti->ti_flags &= ~BSNEXUS;
577 for ( ; cb; cb = cb->ccb_chain.tqe_next)
580 cb->bsccb_flags &= ~(BSITSDONE | BSCASTAT);
584 if (bsc->sc_hstate != BSC_TARG_CHECK &&
585 ti->ti_bctab.tqh_first == NULL)
586 ti->ti_bctab = ti->ti_ctab;
588 TAILQ_INIT(&ti->ti_ctab);
591 if (bsc->sc_hstate != BSC_TARG_CHECK)
592 bs_scsibus_start(bsc);
595 /**************************************************
596 * CHECK TARGETS AND START TARGETS
597 *************************************************/
600 struct targ_info *ti;
603 struct scsi_start_stop_unit cmd;
605 bzero(&cmd, sizeof(struct scsi_start_stop_unit));
606 cmd.opcode = START_STOP;
609 cb = bs_make_internal_ccb(ti, 0, (u_int8_t *) &cmd,
610 sizeof(struct scsi_start_stop_unit),
611 NULL, 0, BSFORCEIOPOLL, BS_MOTOR_TIMEOUT);
612 bscmdstart(ti, BSCMDSTART);
613 return bs_scsi_cmd_poll(ti, cb);
616 /* test unit ready and check ATN msgout response */
619 struct targ_info *ti;
621 struct bs_softc *bsc = ti->ti_bsc;
622 struct scsi_inquiry scsi_cmd;
623 struct scsi_inquiry_data scsi_inquiry_data;
625 int count, retry = bsc->sc_retry;
626 int s, error = COMPLETE;
633 bzero(&scsi_cmd, sizeof(scsi_cmd));
634 scsi_cmd.opcode = INQUIRY;
635 scsi_cmd.length = (u_int8_t) sizeof(struct scsi_inquiry_data);
636 cb = bs_make_internal_ccb(ti, 0,
637 (u_int8_t *) &scsi_cmd, sizeof(scsi_cmd),
638 (u_int8_t *) &scsi_inquiry_data,
639 sizeof(scsi_inquiry_data),
640 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
641 bscmdstart(ti, BSCMDSTART);
642 error = bs_scsi_cmd_poll(ti, cb);
643 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
645 ti->targ_type = scsi_inquiry_data.device;
646 ti->targ_support = scsi_inquiry_data.flags;
648 /* test unit ready twice */
649 for (count = 0; count < 2; count++)
651 cb = bs_make_internal_ccb(ti, 0, NULL, 0, NULL, 0,
652 BSFORCEIOPOLL, BS_STARTUP_TIMEOUT);
653 bscmdstart(ti, BSCMDSTART);
654 error = bs_scsi_cmd_poll(ti, cb);
655 if (error != COMPLETE || (ti->ti_error & BSSELTIMEOUT))
659 if (cb->bsccb_flags & BSCASTAT)
660 bs_printf(ti, "check", "could not clear CA state");
664 bsc->sc_retry = retry;
666 if (ti->ti_error & BSSELTIMEOUT)
669 if (error == COMPLETE)
670 error = bs_start_target(ti);
676 /**************************************************
678 **************************************************/
680 bs_init_target_info(bsc, target)
681 struct bs_softc *bsc;
684 struct targ_info *ti;
686 ti = malloc(sizeof(struct targ_info), M_DEVBUF, M_NOWAIT);
689 bs_printf(NULL, "bs_init_targ_info", "no target info memory");
693 bzero(ti, sizeof(*ti));
698 ti->ti_cfgflags = BS_SCSI_NOPARITY | BS_SCSI_NOSAT;
699 ti->ti_mflags = ~(BSSAT | BSDISC | BSSMIT | BSLINK);
700 BS_SETUP_TARGSTATE(BS_TARG_CTRL);
702 TAILQ_INIT(&ti->ti_ctab);
705 if (ti->bounce_addr == NULL)
711 TAILQ_INSERT_TAIL(&bsc->sc_titab, ti, ti_tchain);
712 bsc->sc_ti[target] = ti;
713 bsc->sc_openf |= (1 << target);
719 bs_setup_ctrl(ti, quirks, flags)
720 struct targ_info *ti;
724 struct bs_softc *bsc = ti->ti_bsc;
725 u_int offset, period, maxperiod;
727 if (ti->ti_state == BS_TARG_CTRL)
729 ti->ti_cfgflags = BS_SCSI_POSITIVE;
730 ti->ti_syncmax.offset = BSHW_MAX_OFFSET;
731 BS_SETUP_TARGSTATE(BS_TARG_START);
734 flags |= ti->ti_cfgflags & BS_SCSI_NEGATIVE;
736 #ifdef BS_TARG_SAFEMODE
737 if (ti->targ_type != 0)
739 flags &= ~(BS_SCSI_DISC | BS_SCSI_SYNC);
740 flags |= BS_SCSI_NOPARITY;
745 if (quirks & SDEV_NODISC)
746 flags &= ~BS_SCSI_DISC;
749 if (quirks & SDEV_NOPARITY)
750 flags |= BS_SCSI_NOPARITY;
753 if (quirks & SDEV_NOCMDLNK)
754 flags &= ~BS_SCSI_LINK;
757 if (quirks & SDEV_ASYNC)
758 flags &= ~BS_SCSI_SYNC;
761 if (quirks & SDEV_AUTOSAVE)
762 flags |= BS_SCSI_SAVESP;
765 if (quirks & SD_Q_NO_SYNC)
766 flags &= ~BS_SCSI_SYNC;
769 if ((flags & BS_SCSI_DISC) == 0 ||
770 (ti->targ_support & SID_Linked) == 0)
771 flags &= ~BS_SCSI_LINK;
773 ti->sm_offset = (flags & BS_SCSI_NOSMIT) ? 0 : bsc->sm_offset;
774 if (ti->sm_offset == 0)
775 flags |= BS_SCSI_NOSMIT;
776 else if (bsc->sc_cfgflags & BSC_SMITSAT_DISEN)
777 flags |= BS_SCSI_NOSAT;
779 flags &= (ti->ti_cfgflags & BS_SCSI_POSITIVE) | (~BS_SCSI_POSITIVE);
780 ti->ti_cfgflags = flags;
782 /* calculate synch setup */
783 period = BS_SCSI_PERIOD(flags);
784 offset = (flags & BS_SCSI_SYNC) ? BS_SCSI_OFFSET(flags) : 0;
786 maxperiod = (bsc->sc_cspeed & IDR_FS_16_20) ? 100 : 50;
787 if (period > maxperiod)
791 period = 2500 / period;
793 if (ti->ti_syncmax.offset > offset)
794 ti->ti_syncmax.offset = offset;
795 if (ti->ti_syncmax.period < period)
796 ti->ti_syncmax.period = period;
798 bshw_adj_syncdata(&ti->ti_syncmax);
800 /* finally report our info */
801 printf("%s(%d:%d): {%d:0x%x:0x%x:%s} flags 0x%b\n",
802 bsc->sc_dvname, ti->ti_id, ti->ti_lun,
803 (u_int) ti->targ_type,
804 (u_int) ti->targ_support,
805 (u_int) ti->bounce_size,
806 (flags & BS_SCSI_NOSMIT) ? "dma" : "pdma",
807 flags, BS_SCSI_BITS);
809 /* internal representation */
811 if ((ti->ti_cfgflags & BS_SCSI_DISC) == 0)
812 ti->ti_mflags &= ~BSDISC;
813 if ((ti->ti_cfgflags & BS_SCSI_LINK) == 0)
814 ti->ti_mflags &= ~BSLINK;
815 if (ti->ti_cfgflags & BS_SCSI_NOSAT)
816 ti->ti_mflags &= ~BSSAT;
817 if (ti->ti_cfgflags & BS_SCSI_NOSMIT)
818 ti->ti_mflags &= ~BSSMIT;
821 /**************************************************
823 **************************************************/
826 struct targ_info *ti;
832 printf("%s(%d:%d): <%s> %s\n",
833 ti->ti_bsc->sc_dvname, ti->ti_id, ti->ti_lun, ph, c);
835 printf("bs*(*:*): <%s> %s\n", ph, c);
840 struct bs_softc *bsc;
844 panic("%s %s\n", bsc->sc_dvname, c);
847 /**************************************************
849 **************************************************/
850 #ifdef BS_DEBUG_ROUTINE
870 #endif /* BS_DEBUG_ROUTINE */
873 bs_debug_print_all(bsc)
874 struct bs_softc *bsc;
876 struct targ_info *ti;
878 for (ti = bsc->sc_titab.tqh_first; ti; ti = ti->ti_tchain.tqe_next)
879 bs_debug_print(bsc, ti);
882 static u_char *phase[] =
884 "FREE", "HOSTQUE", "DISC", "COMPMSG", "ATN", "DISCMSG", "SELECT",
885 "SELECTED", "RESELECTED", "MSGIN", "MSGOUT", "STATIN", "CMDOUT",
886 "DATA", "SATSEL", "SATRESEL", "SATSDP", "SATCOMPSEQ", "UNDEF",
890 bs_debug_print(bsc, ti)
891 struct bs_softc *bsc;
892 struct targ_info *ti;
897 printf("%s <DEBUG INFO> nexus %lx bs %lx bus status %lx \n",
898 bsc->sc_dvname, (u_long) ti, (u_long) bsc->sc_nexus, (u_long) bsc->sc_busstat);
903 struct sc_p *sp = &bsc->sc_p;
905 printf("%s(%d:%d) ph<%s> ", bsc->sc_dvname, ti->ti_id,
906 ti->ti_lun, phase[(int) ti->ti_phase]);
907 printf("msgptr %x msg[0] %x status %x tqh %lx fl %x\n",
908 (u_int) (ti->ti_msginptr), (u_int) (ti->ti_msgin[0]),
909 ti->ti_status, (u_long) (cb = ti->ti_ctab.tqh_first),
912 printf("cmdlen %x cmdaddr %lx cmd[0] %x\n",
913 cb->cmdlen, (u_long) cb->cmd, (int) cb->cmd[0]);
914 printf("datalen %x dataaddr %lx seglen %x ",
915 sp->datalen, (u_long) sp->data, sp->seglen);
917 printf("odatalen %x flags %x\n",
918 cb->datalen, cb->bsccb_flags);
921 printf("error flags %b\n", ti->ti_error, BSERRORBITS);