1 /* $NecBSD: bs.c,v 1.1 1997/07/18 09:18:59 kmatsuda Exp $ */
3 /* $FreeBSD: src/sys/i386/isa/bs/bs.c,v 1.8 1999/12/03 11:58:11 nyan Exp $ */
4 /* $DragonFly: src/sys/dev/disk/i386/bs/Attic/bs.c,v 1.7 2004/08/02 13:22:32 joerg 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>
40 #if defined(__DragonFly__)|| defined(__FreeBSD__)
44 #include <bus/cam/cam.h>
45 #include <bus/cam/cam_ccb.h>
46 #include <bus/cam/cam_sim.h>
47 #include <bus/cam/cam_xpt_sim.h>
48 #include <bus/cam/cam_debug.h>
50 #include <bus/cam/scsi/scsi_all.h>
51 #include <bus/cam/scsi/scsi_message.h>
53 /*****************************************************************
55 *****************************************************************/
56 /* static inline declare */
57 static BS_INLINE struct targ_info *bs_reselect (struct bs_softc *);
58 static BS_INLINE void bs_sat_continue (struct bs_softc *, struct targ_info *, struct bsccb *);
59 static BS_INLINE struct targ_info *bs_selected (struct bs_softc *, struct targ_info *, struct bsccb *);
60 static BS_INLINE u_int8_t bs_read_1byte (struct bs_softc *);
61 static BS_INLINE void bs_write_1byte (struct bs_softc *, u_int8_t);
62 static BS_INLINE void bs_commandout (struct bs_softc *, struct targ_info *, struct bsccb *);
63 static BS_INLINE void bs_status_check (struct bs_softc *, struct targ_info *);
64 static BS_INLINE void bs_msgin (struct bs_softc *, struct targ_info *);
65 static BS_INLINE void bs_msgout (struct bs_softc *, struct targ_info *, struct bsccb *);
66 static BS_INLINE void bs_disconnect_phase (struct bs_softc *, struct targ_info *, struct bsccb *);
67 static void bs_phase_error (struct targ_info *, struct bsccb *);
68 static int bs_scsi_cmd_poll_internal (struct targ_info *);
69 static int bs_xfer (struct bs_softc *, char *, int);
70 static void bs_io_xfer (struct targ_info *);
71 static void bs_quick_abort (struct targ_info *, u_int);
72 static void bs_msgin_error (struct targ_info *, u_int);
73 static void bs_msgin_ext (struct targ_info *);
74 static void bs_msg_reject (struct targ_info *);
75 static void bshoststart (struct bs_softc *, struct targ_info *);
77 /*****************************************************************
79 *****************************************************************/
81 bs_scsi_cmd(struct cam_sim *sim, union ccb *ccb)
83 struct bs_softc *bsc = (struct bs_softc *) cam_sim_softc(sim);
84 int s, target = (u_int) (ccb->ccb_h.target_id);
88 switch (ccb->ccb_h.func_code) {
89 case XPT_SCSI_IO: /* Execute the requested I/O operation */
90 ti = bsc->sc_ti[target];
91 if ((cb = bs_get_ccb()) == NULL) {
92 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
99 cb->lun = ccb->ccb_h.target_lun;
100 cb->cmd = ccb->csio.cdb_io.cdb_bytes;
101 cb->cmdlen = (int) ccb->csio.cdb_len;
102 cb->data = ccb->csio.data_ptr;
103 cb->datalen = (int) ccb->csio.dxfer_len;
107 bs_targ_flags(ti, cb);
108 cb->tcmax = 0;/*(xs->timeout >> 10); default HN2*/
109 if (cb->tcmax < BS_DEFAULT_TIMEOUT_SECOND)
110 cb->tcmax = BS_DEFAULT_TIMEOUT_SECOND;
114 TAILQ_INSERT_TAIL(&ti->ti_ctab, cb, ccb_chain);
116 if (ti->ti_phase == FREE) {
117 if (ti->ti_state == BS_TARG_START)
118 bs_start_syncmsg(ti, NULL, BS_SYNCMSG_ASSERT);
119 bscmdstart(ti, BSCMDSTART);
124 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
125 case XPT_EN_LUN: /* Enable LUN as a target */
126 case XPT_TARGET_IO: /* Execute target I/O request */
127 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
128 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
129 case XPT_ABORT: /* Abort the specified CCB */
131 ccb->ccb_h.status = CAM_REQ_INVALID;
134 case XPT_SET_TRAN_SETTINGS:
136 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
139 case XPT_GET_TRAN_SETTINGS: {
140 struct ccb_trans_settings *cts;
141 struct targ_info *ti;
145 ti = bsc->sc_ti[ccb->ccb_h.target_id];
147 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
148 if (ti->ti_cfgflags & BS_SCSI_DISC)
149 cts->flags = CCB_TRANS_DISC_ENB;
152 if (ti->ti_cfgflags & BS_SCSI_QTAG)
153 cts->flags |= CCB_TRANS_TAG_ENB;
154 cts->sync_period = ti->ti_syncnow.period;
155 cts->sync_offset = ti->ti_syncnow.offset;
156 cts->bus_width = 0;/*HN2*/
158 cts->valid = CCB_TRANS_SYNC_RATE_VALID
159 | CCB_TRANS_SYNC_OFFSET_VALID
160 | CCB_TRANS_BUS_WIDTH_VALID
161 | CCB_TRANS_DISC_VALID
162 | CCB_TRANS_TQ_VALID;
163 ccb->ccb_h.status = CAM_REQ_CMP;
165 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
171 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
172 struct ccb_calc_geometry *ccg;
174 u_int32_t secs_per_cylinder;
177 size_mb = ccg->volume_size
178 / ((1024L * 1024L) / ccg->block_size);
181 ccg->secs_per_track = 34;
183 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
184 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
185 ccb->ccb_h.status = CAM_REQ_CMP;
189 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
190 bshw_chip_reset(bsc); /* XXX need perfect RESET? */
191 ccb->ccb_h.status = CAM_REQ_CMP;
194 case XPT_TERM_IO: /* Terminate the I/O process */
196 ccb->ccb_h.status = CAM_REQ_INVALID;
199 case XPT_PATH_INQ: { /* Path routing inquiry */
200 struct ccb_pathinq *cpi = &ccb->cpi;
202 cpi->version_num = 1; /* XXX??? */
203 cpi->hba_inquiry = PI_SDTR_ABLE;
204 cpi->target_sprt = 0;
206 cpi->hba_eng_cnt = 0;
207 cpi->max_target = NTARGETS - 1;
209 cpi->initiator_id = bsc->sc_hostid;
210 cpi->bus_id = cam_sim_bus(sim);
211 cpi->base_transfer_speed = 3300;
212 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
213 strncpy(cpi->hba_vid, "NEC", HBA_IDLEN);
214 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
215 cpi->unit_number = cam_sim_unit(sim);
216 cpi->ccb_h.status = CAM_REQ_CMP;
221 /*printf("bs: non support func_code = %d ", ccb->ccb_h.func_code);*/
222 ccb->ccb_h.status = CAM_REQ_INVALID;
228 /**************************************************
229 * ### NEXUS START and TERMINATE ###
230 **************************************************/
232 * FLAGS : BSCMDRESTART restart in case of error.
235 bscmdstart(ti, flags)
236 struct targ_info *ti;
240 struct bs_softc *bsc = ti->ti_bsc;
242 if ((cb = TAILQ_FIRST(&ti->ti_ctab)) == NULL)
244 if (bsc->sc_nexus == NULL)
245 bshoststart(bsc, NULL);
249 ti->ti_lun = cb->lun;
251 ti->ti_scsp.data = cb->data;
252 ti->ti_scsp.datalen = cb->datalen;
253 ti->ti_scsp.seglen = 0;
255 cb->bsccb_flags &= ~(BSSAT | BSLINK);
256 ti->ti_flags &= ~BSCFLAGSMASK;
257 ti->ti_flags |= cb->bsccb_flags & BSCFLAGSMASK;
261 if (ti->ti_phase == FREE)
263 if (bsc->sc_nexus == NULL)
264 bshoststart(bsc, ti);
267 if (flags & BSCMDRESTART)
268 bs_hostque_head(bsc, ti);
270 bs_hostque_tail(bsc, ti);
271 BS_SETUP_PHASE(HOSTQUEUE)
274 else if (bsc->sc_nexus == NULL)
275 bshoststart(bsc, NULL);
282 struct targ_info *ti;
284 struct bs_softc *bsc = ti->ti_bsc;
285 struct bsccb *cb = TAILQ_FIRST(&ti->ti_ctab);
289 if (ti->ti_state == BS_TARG_SYNCH)
291 if (bs_analyze_syncmsg(ti, cb))
295 if (bsc->sc_p.datalen != 0)
296 ti->ti_error |= BSDMAABNORMAL;
298 cb->error = ti->ti_error;
305 if (cb->bsccb_flags & (BSITSDONE | BSSENSECCB | BSCASTAT))
307 if (cb->bsccb_flags & BSSENSECCB)
309 cb->error &= ~BSDMAABNORMAL;
311 ti->ti_flags |= BSCASTAT;
313 ti->ti_flags |= BSERROROK;
315 else if (cb->bsccb_flags & BSCASTAT)
317 if (ti->ti_flags & BSCASTAT)
319 ti->ti_flags &= ~BSCASTAT;
320 error = CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
322 ccb->csio.sense_data = ti->sense;/* XXX may not be csio.... */
325 error = CAM_AUTOSENSE_FAIL;
326 ti->ti_flags |= BSERROROK;
328 bs_panic(bsc, "internal error");
333 if (ti->ti_flags & BSERROROK)
336 if (cb->rcnt >= bsc->sc_retry || (cb->error & BSFATALIO))
338 if (cb->error & (BSSELTIMEOUT | BSTIMEOUT))
339 error = CAM_CMD_TIMEOUT;
340 else if (cb->error & BSTARGETBUSY)
341 error = CAM_SCSI_STATUS_ERROR;
343 error = CAM_REQ_CMP_ERR;
347 if (cb->error & BSREQSENSE)
349 /* must clear the target's sense state */
351 cb->bsccb_flags |= (BSITSDONE | BSCASTAT);
352 cb->error &= ~BSREQSENSE;
353 return bs_request_sense(ti);
356 /* XXX: compat with upper driver */
357 if ((cb->error & BSDMAABNORMAL) &&
358 BSHW_CMD_CHECK(cb, BSERROROK))
360 cb->error &= ~BSDMAABNORMAL;
363 if (/*(xs && xs->bp) || can't know whether bufferd i/o or not */
364 (cb->error & BSSELTIMEOUT) == 0)
365 bs_debug_print(bsc, ti);
371 cb->bsccb_flags |= BSITSDONE;
375 bsc->sc_flags |= BSJOBDONE;
376 if (bsc->sc_outccb == cb)
377 bsc->sc_flags |= BSPOLLDONE;
380 TAILQ_REMOVE(&ti->ti_ctab, cb, ccb_chain);
384 ccb->ccb_h.status = error;
385 ccb->csio.scsi_status = ti->ti_status;/*XXX*/
390 cb = TAILQ_FIRST(&ti->ti_ctab);
393 while (cb != NULL && (cb->bsccb_flags & BSITSDONE) != 0);
399 /**************************************************
400 * ### PHASE FUNCTIONS ###
401 **************************************************/
402 /**************************************************
404 **************************************************/
407 struct bs_softc *bsc;
408 struct targ_info *ti;
413 if (bsc->sc_flags & BSINACTIVE)
419 if ((ti = TAILQ_FIRST(&bsc->sc_sttab)) == NULL)
421 bs_hostque_delete(bsc, ti);
424 if ((cb = TAILQ_FIRST(&ti->ti_ctab)) == NULL)
426 bs_printf(ti, "bshoststart", "Warning: No ccb");
427 BS_SETUP_PHASE(FREE);
433 if (cb->bsccb_flags & BSITSDONE)
434 bs_panic(bsc, "bshoststart: already done");
436 if (bsc->sc_nexus || (ti->ti_flags & BSNEXUS))
438 char *s = ((ti->ti_flags & BSNEXUS) ?
439 "nexus already established" : "scsi board busy");
441 bs_debug_print(bsc, ti);
442 bs_printf(ti, "bshoststart", s);
447 bs_statics[ti->ti_id].select++;
448 #endif /* BS_STATICS */
450 if (ti->ti_cfgflags & BS_SCSI_WAIT)
452 struct targ_info *tmpti;
454 TAILQ_FOREACH(tmpti, &bsc->sc_titab; ti_tchain) {
455 if (tmpti->ti_phase >= DISCONNECTED)
460 /* start selection */
461 ti->ti_status = ST_UNK;
462 if (bs_check_sat(ti))
464 if ((bshw_get_auxstat(bsc) & STR_BUSY) == 0)
467 bshw_set_dst_id(bsc, ti->ti_id, ti->ti_lun);
468 bshw_setup_ctrl_reg(bsc, ti->ti_cfgflags);
469 bshw_cmd_pass(bsc, 0);
470 bshw_set_sync_reg(bsc, ti->ti_sync);
471 bshw_issue_satcmd(bsc, cb, bs_check_link(ti, cb));
472 if (bs_check_smit(ti) || bsc->sc_p.datalen <= 0)
473 bshw_set_count(bsc, 0);
475 bs_dma_xfer(ti, BSHW_CMD_CHECK(cb, BSREAD));
478 if ((bshw_get_auxstat(bsc) & STR_BUSY) == 0)
481 * Reload a lun again here.
483 bshw_set_lun(bsc, ti->ti_lun);
484 bshw_start_sat(bsc, bs_check_disc(ti));
485 if ((bshw_get_auxstat(bsc) & STR_LCI) == 0)
490 BS_SETUP_PHASE(SATSEL);
491 ti->ti_omsgoutlen = 0;
492 ti->ti_msgout = bs_identify_msg(ti);
494 ti->ti_flags |= BSNEXUS;
497 bs_statics[ti->ti_id].select_win++;
498 #endif /* BS_STATICS */
504 if (bs_check_smit(ti) == 0)
505 bshw_dmaabort(bsc, ti);
507 bs_statics[ti->ti_id].select_miss_in_assert++;
508 #endif /* BS_STATICS */
514 if ((bshw_get_auxstat(bsc) & STR_BUSY) == 0)
516 bshw_set_dst_id(bsc, ti->ti_id, ti->ti_lun);
517 bshw_setup_ctrl_reg(bsc, ti->ti_cfgflags);
518 bshw_set_sync_reg(bsc, ti->ti_sync);
519 bshw_assert_select(bsc);
521 if ((bshw_get_auxstat(bsc) & STR_LCI) == 0)
526 BS_SETUP_PHASE(SELECTASSERT);
528 bs_statics[ti->ti_id].select_win++;
529 #endif /* BS_STATICS */
533 bs_statics[ti->ti_id].select_miss_in_assert++;
534 #endif /* BS_STATICS */
542 bs_statics[ti->ti_id].select_miss++;
543 #endif /* BS_STATICS */
544 bs_hostque_head(bsc, ti);
545 BS_SETUP_PHASE(HOSTQUEUE)
548 static BS_INLINE struct targ_info *
549 bs_selected(bsc, ti, cb)
550 struct bs_softc *bsc;
551 struct targ_info *ti;
555 if (bsc->sc_busstat != BSR_SELECTED)
557 bs_phase_error(ti, cb);
562 if (bsc->sc_selwait != ti)
563 panic("%s selection internal error\n", bsc->sc_dvname);
565 ti->ti_flags |= BSNEXUS;
568 /* clear select wait state */
569 BS_SETUP_PHASE(SELECTED);
570 BS_SELECTION_TERMINATE;
575 /**************************************************
577 **************************************************/
578 static BS_INLINE struct targ_info *
580 struct bs_softc *bsc;
583 struct targ_info *ti;
585 /* check collision */
586 if ((ti = bsc->sc_selwait) != NULL)
588 if (ti->ti_phase == SATSEL)
591 ti->ti_flags &= ~BSNEXUS;
594 if (bs_check_smit(ti) == 0)
595 bshw_dmaabort(bsc, ti);
597 bs_hostque_head(bsc, ti);
598 BS_SELECTION_TERMINATE
599 BS_SETUP_PHASE(HOSTQUEUE)
601 bs_statics[ti->ti_id].select_miss_by_reselect++;
602 bs_statics[ti->ti_id].select_miss++;
603 #endif /* BS_STATICS */
607 target = bshw_get_src_id(bsc);
608 if ((ti = bsc->sc_ti[target]) == NULL)
610 bs_debug_print_all(bsc);
611 printf("reselect: miss reselect. target(%d)\n", target);
618 bshw_setup_ctrl_reg(bsc, ti->ti_cfgflags);
619 if (TAILQ_EMPTY(&ti->ti_ctab) || ti->ti_phase != DISCONNECTED)
621 bs_printf(ti, "reselect", "phase mismatch");
622 BS_SETUP_PHASE(UNDEF)
624 bs_hostque_delete(bsc, ti);
630 bshw_set_dst_id(bsc, ti->ti_id, ti->ti_lun);
631 bshw_set_sync_reg(bsc, ti->ti_sync);
633 BS_SETUP_PHASE(RESELECTED)
635 bs_statics[ti->ti_id].reselect++;
636 #endif /* BS_STATICS */
640 static BS_INLINE void
641 bs_sat_continue(bsc, ti, cb)
642 struct bs_softc *bsc;
643 struct targ_info *ti;
647 BS_SETUP_PHASE(SATRESEL);
648 bshw_set_dst_id(bsc, ti->ti_id, ti->ti_lun);
649 bshw_cmd_pass(bsc, 0x44);
650 bshw_set_sync_reg(bsc, ti->ti_sync);
651 bshw_issue_satcmd(bsc, cb, 0);
652 if (bs_check_smit(ti) || bsc->sc_p.datalen <= 0)
653 bshw_set_count(bsc, 0);
655 bs_dma_xfer(ti, BSHW_CMD_CHECK(cb, BSREAD));
656 bshw_set_lun(bsc, ti->ti_lun); /* XXX */
657 bshw_start_sat(bsc, 0);
660 /*************************************************
662 *************************************************/
663 #define DR (STR_BSY | STR_DBR)
666 bs_poll_timeout(bsc, s)
667 struct bs_softc *bsc;
670 struct targ_info *ti;
672 bs_printf(NULL, s, "timeout");
673 bsc->sc_flags |= BSRESET;
674 if ((ti = bsc->sc_nexus) && !TAILQ_EMPTY(&ti->ti_ctab))
675 ti->ti_error |= BSTIMEOUT;
678 static BS_INLINE u_int8_t
680 struct bs_softc *bsc;
684 bshw_start_sxfer(bsc);
685 for (wc = bsc->sc_wc; (bshw_get_auxstat(bsc) & DR) != DR && --wc; );
687 return bshw_read_data(bsc);
689 bs_poll_timeout(bsc, "read_1byte");
694 static BS_INLINE void
695 bs_write_1byte(bsc, data)
696 struct bs_softc *bsc;
701 bshw_start_sxfer(bsc);
702 for (wc = bsc->sc_wc; (bshw_get_auxstat(bsc) & DR) != DR && --wc; );
704 bshw_write_data(bsc, data);
706 bs_poll_timeout(bsc, "write_1byte");
710 bs_xfer(bsc, data, len)
711 struct bs_softc *bsc;
718 bshw_set_count(bsc, len);
719 bshw_start_xfer(bsc);
721 for (count = 0, wc = bsc->sc_wc; count < len && --wc; )
723 if (((aux = bshw_get_auxstat(bsc)) & DR) == DR)
725 if (bsc->sc_busstat & BSHW_READ)
726 *(data++) = bshw_read_data(bsc);
728 bshw_write_data(bsc, *(data++));
738 bs_poll_timeout(bsc, "bs_xfer");
746 struct targ_info *ti;
748 struct bs_softc *bsc = ti->ti_bsc;
749 struct sc_p *sp = &bsc->sc_p;
752 /* switch dma trasnfr mode */
753 bshw_set_poll_trans(bsc, ti->ti_cfgflags);
757 if (sp->datalen <= 0)
759 ti->ti_error |= BSDMAABNORMAL;
761 count = bs_xfer(bsc, (u_int8_t *) &dummy, 1);
764 count = bs_xfer(bsc, sp->data, sp->datalen);
767 sp->datalen -= count;
770 /************************************************
772 ************************************************/
773 static BS_INLINE void
774 bs_commandout(bsc, ti, cb)
775 struct bs_softc *bsc;
776 struct targ_info *ti;
779 u_int8_t scsi_cmd[16];
782 BS_SETUP_PHASE(CMDPHASE);
784 if (bs_check_link(ti, cb))
786 bcopy(cb->cmd, scsi_cmd, cb->cmdlen);
787 scsi_cmd[cb->cmdlen - 1] |= 0x01;
788 len = bs_xfer(bsc, scsi_cmd, cb->cmdlen);
791 len = bs_xfer(bsc, cb->cmd, cb->cmdlen);
793 if (len != cb->cmdlen)
794 ti->ti_error |= BSCMDABNORMAL;
797 /************************************************
799 ************************************************/
800 static BS_INLINE void
801 bs_status_check(bsc, ti)
802 struct bs_softc *bsc;
803 struct targ_info *ti;
806 if (ti->ti_status == ST_GOOD || ti->ti_status == ST_INTERGOOD)
809 switch (ti->ti_status)
814 ti->ti_error |= BSREQSENSE;
818 ti->ti_error |= BSTARGETBUSY;
822 ti->ti_error |= BSSTATUSERROR;
827 /************************************************
829 ************************************************/
830 #define MSGWAIT(cnt) { if (ti->ti_msginptr < (cnt)) return; }
833 bs_quick_abort(ti, msg)
834 struct targ_info *ti;
839 if ((cb = TAILQ_FIRST(&ti->ti_ctab)) == NULL)
846 ti->ti_error |= BSMSGERROR;
850 bs_msgin_error(ti, count)
851 struct targ_info *ti;
858 bs_printf(ti, "msgin", "illegal msg");
860 for (n = 0; n < ti->ti_msginptr; n ++)
861 printf("[0x%x] ", (u_int) ti->ti_msgin[n]);
864 bs_quick_abort(ti, MSG_REJECT);
870 struct targ_info *ti;
872 struct bs_softc *bsc = ti->ti_bsc;
873 struct bsccb *cb = TAILQ_FIRST(&ti->ti_ctab);
881 reqlen = ti->ti_msgin[1];
885 if (ti->ti_msginptr >= MAXMSGLEN)
886 ti->ti_msginptr = 3; /* XXX */
890 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
892 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
893 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
894 count = (int) htonl((long) (*ptr));
896 bsc->sc_p.seglen = ti->ti_scsp.seglen = 0;
897 if (bsc->sc_p.datalen - count >= 0 &&
898 bsc->sc_p.datalen - count <= cb->datalen)
900 bsc->sc_p.datalen -= count;
901 bsc->sc_p.data += count;
904 bs_msgin_error(ti, 7);
907 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
908 ti->ti_syncnow.period = ti->ti_msgin[3];
909 ti->ti_syncnow.offset = ti->ti_msgin[4];
910 if (ti->ti_syncnow.offset == 0)
911 ti->ti_syncnow.period = 0;
913 if (ti->ti_syncnow.state != BS_SYNCMSG_ASSERT)
915 bs_start_syncmsg(ti, NULL, BS_SYNCMSG_REQUESTED);
916 bscmdstart(ti, BSCMDSTART);
919 BS_SETUP_SYNCSTATE(BS_SYNCMSG_ACCEPT)
922 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
923 msg.msglen = MSG_EXTEND_WIDELEN + 2;
924 msg.msg[0] = MSG_EXTEND;
925 msg.msg[1] = MSG_EXTEND_WIDELEN;
926 msg.msg[2] = MSG_EXTEND_WIDECODE;
929 bs_make_msg_ccb(ti, cb->lun, cb, &msg, 0);
933 bs_msgin_error(ti, 0);
943 struct targ_info *ti;
945 struct bs_softc *bsc = ti->ti_bsc;
946 struct bsccb *cb = TAILQ_FIRST(&ti->ti_ctab);
947 char *s = "unexpected msg reject";
949 switch (ti->ti_ophase)
953 cb->bsccb_flags &= ~BSLINK;
954 BS_SETUP_MSGPHASE(IOCOMPLETED);
958 if (ti->ti_msgout & 0x80)
960 s = "identify msg rejected";
961 cb->bsccb_flags &= ~BSDISC;
962 BS_SETUP_MSGPHASE(IOCOMPLETED);
964 else if (ti->ti_msgout == MSG_EXTEND)
966 switch (ti->ti_emsgout)
968 case MSG_EXTEND_SYNCHCODE:
969 BS_SETUP_SYNCSTATE(BS_SYNCMSG_REJECT);
982 bs_debug_print(bsc, ti);
983 bs_printf(ti, "msgin", s);
984 ti->ti_error |= BSMSGERROR;
987 static BS_INLINE void
989 struct bs_softc *bsc;
990 struct targ_info *ti;
993 BS_SETUP_PHASE(MSGIN);
995 switch (ti->ti_msgin[0])
1003 bs_printf(ti, "msgin", "restore scsi pointer");
1013 case MSG_I_ERROR:/* all I -> T : nothing to do*/
1018 bs_msgin_error(ti, 1);
1029 bs_msgin_error(ti, 2);
1033 BS_SETUP_MSGPHASE(DISCONNECTASSERT);
1037 BS_SETUP_MSGPHASE(IOCOMPLETED);
1042 bs_status_check(bsc, ti);
1043 if (bscmddone(ti) == NULL)
1045 if (bscmdstart(ti, BSCMDSTART) == 0)
1047 bs_printf(ti, "msgin", "cmd line miss");
1052 bscmdstart(ti, BSCMDRESTART);
1054 bs_linkcmd_count[ti->ti_id]++;
1055 #endif /* BS_STATICS */
1057 ti->ti_status = ST_UNK;
1061 if (ti->ti_msgin[0] & 0x80)
1063 if ((ti->ti_msgin[0] & 0x07) != ti->ti_lun)
1065 ti->ti_lun = (ti->ti_msgin[0] & 0x07);
1066 bshw_set_dst_id(bsc, ti->ti_id, ti->ti_lun);
1067 bshw_set_sync_reg(bsc, ti->ti_sync);
1069 bs_printf(ti, "msgin", "lun error");
1070 bs_quick_abort(ti, MSG_ABORT);
1074 else if (ti->ti_msgin[0] < 0x20)
1075 bs_msgin_error(ti, 1);
1076 else if (ti->ti_msgin[0] < 0x30)
1077 bs_msgin_error(ti, 2);
1079 bs_msgin_error(ti, 1);
1083 ti->ti_msginptr = 0;
1089 /************************************************
1091 ************************************************/
1092 static BS_INLINE void
1093 bs_msgout(bsc, ti, cb)
1094 struct bs_softc *bsc;
1095 struct targ_info *ti;
1098 u_int8_t msg[MAXMSGLEN + 1];
1100 if (ti->ti_phase == MSGOUT)
1102 if (cb->rcnt ++ < bsc->sc_retry)
1103 cb->msgoutlen = ti->ti_omsgoutlen;
1106 BS_SETUP_PHASE(MSGOUT);
1108 if (ti->ti_ophase == SELECTED)
1111 if (cb->msgoutlen == 0)
1113 ti->ti_msgout = bs_identify_msg(ti);
1114 ti->ti_omsgoutlen = 0;
1115 bs_write_1byte(bsc, ti->ti_msgout);
1119 if (cb->msgout[0] != MSG_RESET &&
1120 cb->msgout[0] != MSG_ABORT)
1122 msg[0] = bs_identify_msg(ti);
1123 bcopy(cb->msgout, &msg[1], cb->msgoutlen);
1124 bs_xfer(bsc, msg, cb->msgoutlen + 1);
1127 bs_xfer(bsc, cb->msgout, cb->msgoutlen);
1129 ti->ti_msgout = cb->msgout[0];
1130 ti->ti_emsgout = cb->msgout[2];
1131 ti->ti_omsgoutlen = cb->msgoutlen;
1137 if (ti->ti_ophase == SATSEL)
1140 * Maybe identify msg rejected due to
1141 * a parity error in target side.
1144 bs_printf(ti, "msgout", "msg identify retry (SAT)");
1148 if (cb->msgoutlen == 0)
1150 ti->ti_msgout = MSG_REJECT;
1151 ti->ti_omsgoutlen = 0;
1152 bs_write_1byte(bsc, ti->ti_msgout);
1156 ti->ti_msgout = cb->msgout[0];
1157 ti->ti_emsgout = cb->msgout[2];
1158 ti->ti_omsgoutlen = cb->msgoutlen;
1159 bs_xfer(bsc, cb->msgout, cb->msgoutlen);
1164 /************************************************
1166 ************************************************/
1167 static BS_INLINE void
1168 bs_disconnect_phase(bsc, ti, cb)
1169 struct bs_softc *bsc;
1170 struct targ_info *ti;
1174 switch (bsc->sc_msgphase)
1177 panic("%s unknown msg phase\n", bsc->sc_dvname);
1180 case DISCONNECTASSERT:
1183 bs_statics[ti->ti_id].disconnected++;
1184 #endif /* BS_STATICS */
1185 if (ti->ti_cfgflags & BS_SCSI_SAVESP)
1188 BS_SETUP_PHASE(DISCONNECTED);
1190 bshoststart(bsc, NULL);
1194 bs_status_check(bsc, ti);
1197 ti->ti_flags &= ~BSNEXUS;
1198 #endif /* BS_DIAG */
1199 BS_SETUP_PHASE(FREE);
1200 if (cb || TAILQ_EMPTY(&bsc->sc_sttab))
1203 bscmdstart(ti, BSCMDSTART);
1207 /* give a chance to other target */
1208 bscmdstart(ti, BSCMDSTART);
1210 bshoststart(bsc, NULL);
1215 BS_SETUP_MSGPHASE(FREE);
1218 /**************************************************
1220 **************************************************/
1221 #define scsi_status (bsc->sc_busstat)
1229 struct bs_err bs_cmderr[] = {
1230 /*0*/ { "illegal cmd", BSABNORMAL, UNDEF },
1231 /*1*/ { "unexpected bus free", BSABNORMAL, FREE },
1232 /*2*/ { NULL, BSSELTIMEOUT, FREE},
1233 /*3*/ { "scsi bus parity error", BSPARITY, UNDEF },
1234 /*4*/ { "scsi bus parity error", BSPARITY, UNDEF },
1235 /*5*/ { "unknown" , BSFATALIO, UNDEF },
1236 /*6*/ { "miss reselection (target mode)", BSFATALIO, UNDEF },
1237 /*7*/ { "wrong status byte", BSPARITY, STATUSIN },
1241 bs_phase_error(ti, cb)
1242 struct targ_info *ti;
1245 struct bs_softc *bsc = ti->ti_bsc;
1248 if ((scsi_status & BSR_CM) == BSR_CMDERR &&
1249 (scsi_status & BSR_PHVALID) == 0)
1251 pep = &bs_cmderr[scsi_status & BSR_PM];
1252 ti->ti_error |= pep->pe_err;
1255 bs_debug_print(bsc, ti);
1256 bs_printf(ti, "bsintr", pep->pe_msg);
1258 BS_SETUP_PHASE(pep->pe_ph);
1262 ti->ti_error |= BSABNORMAL;
1263 bs_debug_print(bsc, ti);
1264 bs_printf(ti, "bsintr", "phase error");
1265 BS_SETUP_PHASE(UNDEF);
1268 BS_SETUP_MSGPHASE(FREE);
1269 switch (ti->ti_phase)
1272 BS_SETUP_PHASE(UNDEF);
1275 ti->ti_flags &= ~BSNEXUS;
1276 #endif /* BS_DIAG */
1278 BS_SETUP_PHASE(FREE);
1279 bscmdstart(ti, ((cb == NULL) ? BSCMDSTART : BSCMDRESTART));
1283 ti->ti_error |= BSSTATUSERROR;
1284 ti->ti_status = bshw_get_status_insat(bsc); /* XXX SAT */
1285 bs_debug_print(bsc, ti);
1290 ti->ti_error |= BSABNORMAL;
1291 bs_reset_nexus(bsc);
1296 /**************************************************
1297 * ### SCSI PHASE SEQUENCER ###
1298 **************************************************/
1299 static BS_INLINE void bs_ack_wait (struct bs_softc *, struct targ_info *, struct bsccb *);
1301 static BS_INLINE void
1302 bs_ack_wait(bsc, ti, cb)
1303 struct bs_softc *bsc;
1304 struct targ_info *ti;
1307 int wc = bsc->sc_wc;
1309 for (wc = bsc->sc_wc; bshw_get_busstat(bsc) != BSR_ACKREQ && wc > 0; )
1314 bs_printf(ti, "bs_ack_wait", "timeout I");
1318 bshw_get_auxstat(bsc);
1319 scsi_status = bshw_get_busstat(bsc);
1321 if (cb->msgoutlen > 0)
1323 bshw_assert_atn(bsc);
1325 BS_SETUP_PHASE(ATTENTIONASSERT);
1328 bshw_negate_ack(bsc);
1331 for (wc = bsc->sc_wc; bshw_get_busstat(bsc) == BSR_ACKREQ && wc > 0; )
1335 bs_printf(ti, "bs_ack_wait", "timeout II");
1336 #endif /* WAITNEXTP */
1341 struct bs_softc *bsc;
1343 struct targ_info *ti;
1346 /**************************************************
1348 **************************************************/
1349 if (bsc->sc_flags & (BSRESET | BSINACTIVE))
1351 if (bsc->sc_flags & BSRESET)
1352 bs_reset_nexus(bsc);
1356 /**************************************************
1357 * Get status & bus phase
1358 **************************************************/
1359 if ((bshw_get_auxstat(bsc) & STR_INT) == 0)
1362 scsi_status = bshw_get_busstat(bsc);
1363 if (scsi_status == ((u_int8_t) -1))
1365 bs_debug_print_all(bsc);
1368 /**************************************************
1369 * Check reselection, or nexus
1370 **************************************************/
1371 if (scsi_status == BSR_RESEL)
1378 if (ti == NULL || (cb = TAILQ_FIRST(&ti->ti_ctab)) == NULL)
1380 bs_debug_print_all(bsc);
1381 bs_printf(ti, "bsintr", "no nexus");
1382 bs_reset_nexus(bsc);
1386 /**************************************************
1388 **************************************************/
1392 bs_debug_print(bsc, ti);
1393 if (bs_debug_flag > 1)
1396 #endif /* BS_DEBUG */
1398 /**************************************************
1399 * internal scsi phase
1400 **************************************************/
1401 switch (ti->ti_phase)
1404 bs_selected(bsc, ti, cb);
1408 BS_SELECTION_TERMINATE;
1411 if (bsc->sc_flags & (BSDMASTART | BSSMITSTART))
1413 if (bsc->sc_flags & BSSMITSTART)
1415 bs_debug_print_all(bsc);
1416 bs_reset_nexus(bsc);
1417 bs_printf(ti, "bsintr", "smit transfer");
1421 BS_SETUP_PHASE(DATAPHASE); /* XXX */
1422 bs_dma_xfer_end(ti);
1423 ti->ti_phase = ti->ti_ophase; /* XXX */
1429 * check check check for safety !!
1431 if (bsc->sc_selwait)
1433 /* Ghaaa! phase error! retry! */
1434 bs_phase_error(ti, cb);
1438 if (bsc->sc_flags & (BSDMASTART | BSSMITSTART))
1440 if (bsc->sc_flags & BSDMASTART)
1441 bs_dma_xfer_end(ti);
1443 bs_smit_xfer_end(ti);
1448 /**************************************************
1450 **************************************************/
1451 if (scsi_status & BSR_PHVALID)
1453 /**************************************************
1454 * Normal SCSI phase.
1455 **************************************************/
1456 if ((scsi_status & BSR_CM) == BSR_CMDABT)
1458 bs_phase_error(ti, cb);
1462 switch (scsi_status & BSR_PM)
1466 BS_SETUP_PHASE(DATAPHASE);
1468 if (bsc->sc_p.datalen <= 0 ||
1469 (ti->ti_flags & BSFORCEIOPOLL))
1475 if (bs_check_smit(ti) &&
1476 (bsc->sc_p.datalen % sizeof(u_int32_t)) == 0)
1478 bs_lc_smit_xfer(ti, scsi_status & BSR_IOR);
1482 bs_dma_xfer(ti, scsi_status & BSR_IOR);
1483 bshw_start_xfer(bsc);
1487 bs_commandout(bsc, ti, cb);
1491 if (bs_check_sat(ti))
1493 BS_SETUP_PHASE(SATCOMPSEQ);
1494 bshw_set_count(bsc, 0);
1495 bshw_cmd_pass(bsc, 0x41);
1496 bshw_start_sat(bsc, 0);
1500 BS_SETUP_PHASE(STATUSIN);
1501 ti->ti_status = bs_read_1byte(bsc);
1507 bs_debug_print(bsc, ti);
1508 bs_printf(ti, "bsintr", "illegal bus phase");
1512 bs_msgout(bsc, ti, cb);
1515 case BSR_MSGIN:/* msg in */
1516 if (bs_check_sat(ti))
1518 if (ti->ti_phase == RESELECTED)
1520 bs_sat_continue(bsc, ti, cb);
1524 if (ti->ti_status == ST_UNK)
1525 ti->ti_status = bshw_get_status_insat(bsc);
1528 ti->ti_msgin[ti->ti_msginptr ++] = bs_read_1byte(bsc);
1530 if (bsc->sc_cfgflags & BSC_FASTACK)
1531 bs_ack_wait(bsc, ti, cb);
1538 /**************************************************
1539 * Special SCSI phase
1540 **************************************************/
1541 switch (scsi_status)
1543 case BSR_SATSDP:/* SAT with save data pointer */
1545 bshw_cmd_pass(bsc, 0x41);
1546 bshw_start_sat(bsc, 0);
1547 BS_SETUP_PHASE(SATSDP)
1550 case BSR_SATFIN:/* SAT COMPLETE */
1551 ti->ti_status = bshw_get_status_insat(bsc);
1552 BS_SETUP_MSGPHASE(IOCOMPLETED);
1553 bs_disconnect_phase(bsc, ti, cb);
1556 case BSR_ACKREQ:/* negate ACK */
1557 if (cb->msgoutlen > 0)
1559 bshw_assert_atn(bsc);
1561 BS_SETUP_PHASE(ATTENTIONASSERT);
1563 bshw_negate_ack(bsc);
1566 case BSR_DISC:/* disconnect */
1567 bs_disconnect_phase(bsc, ti, cb);
1575 bs_phase_error(ti, cb);
1579 /*****************************************************************
1580 * INTERNAL POLLING FUNCTIONS
1581 *****************************************************************/
1583 bs_scsi_cmd_poll_internal(cti)
1584 struct targ_info *cti;
1586 struct bs_softc *bsc = cti->ti_bsc;
1587 struct targ_info *ti;
1589 int i, waits, delay_count;
1593 /* setup timeout count */
1594 if ((ti = bsc->sc_nexus) == NULL ||
1595 (cb = TAILQ_FIRST(&ti->ti_ctab)) == NULL)
1596 waits = BS_DEFAULT_TIMEOUT_SECOND * 1000000;
1598 waits = cb->tcmax * 1000000;
1600 /* force all current jobs into the polling state. */
1601 for (i = 0; i < NTARGETS; i++)
1603 if ((ti = bsc->sc_ti[i]) != NULL)
1605 ti->ti_flags |= BSFORCEIOPOLL;
1606 if ((cb = TAILQ_FIRST(&ti->ti_ctab)) != NULL)
1607 cb->bsccb_flags |= BSFORCEIOPOLL;
1612 bsc->sc_flags &= ~BSJOBDONE;
1615 delay_count = ((bsc->sc_flags & BSDMASTART) ? 1000000 : 100);
1617 waits -= delay_count;
1620 while (waits >= 0 && (bsc->sc_flags & (BSUNDERRESET | BSJOBDONE)) == 0);
1624 if (waits < 0 || (bsc->sc_flags & BSUNDERRESET))
1626 bs_printf(NULL, "cmd_poll", "timeout or fatal");
1634 bs_scsi_cmd_poll(cti, targetcb)
1635 struct targ_info *cti;
1636 struct bsccb *targetcb;
1638 struct bs_softc *bsc = cti->ti_bsc;
1639 struct targ_info *ti;
1640 int s, error = COMPLETE;
1643 bs_terminate_timeout(bsc);
1645 if (bsc->sc_hstate == BSC_TARG_CHECK)
1647 if ((error = bs_scsi_cmd_poll_internal(cti)) != COMPLETE)
1648 bs_reset_nexus(bsc);
1653 bs_panic(bsc, "bs_cmd_poll: internal error");
1655 bsc->sc_flags &= ~BSPOLLDONE;
1656 bsc->sc_outccb = targetcb;
1658 while ((bsc->sc_flags & BSPOLLDONE) == 0)
1660 if (bs_scsi_cmd_poll_internal(cti) != COMPLETE)
1662 if ((ti = bsc->sc_nexus) &&
1663 !TAILQ_EMPTY(&ti->ti_ctab))
1664 ti->ti_error |= (BSTIMEOUT | BSABNORMAL);
1665 bs_reset_nexus(bsc);
1669 bsc->sc_outccb = NULL;
1672 bs_start_timeout(bsc);
1673 softintr(bsc->sc_irq);