1 /* $NecBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $ */
7 #define SCSI_LOW_STATICS
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
12 /* #define SCSI_LOW_INFO_DETAIL */
14 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
15 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
18 #define SCSI_LOW_TARGET_OPEN
19 #endif /* __NetBSD__ */
22 #define SCSI_LOW_FLAGS_QUIRKS_OK
23 #endif /* __FreeBSD__ */
26 * [NetBSD for NEC PC-98 series]
27 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
28 * NetBSD/pc98 porting staff. All rights reserved.
29 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
30 * Naofumi HONDA. All rights reserved.
32 * [Ported for FreeBSD CAM]
33 * Copyright (c) 2000, 2001
34 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
52 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
61 /* <On the nexus establishment>
62 * When our host is reselected,
63 * nexus establish processes are little complicated.
64 * Normal steps are followings:
65 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
66 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
67 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
76 #if __FreeBSD_version >= 500001
79 #include <machine/clock.h>
81 #endif /* __FreeBSD__ */
84 #include <sys/queue.h>
85 #include <sys/malloc.h>
86 #include <sys/errno.h>
89 #include <sys/device.h>
92 #include <machine/bus.h>
93 #include <machine/intr.h>
94 #include <machine/dvcfg.h>
98 #include <dev/scsipi/scsipi_all.h>
99 #include <dev/scsipi/scsipiconf.h>
100 #include <dev/scsipi/scsipi_disk.h>
101 #include <dev/scsipi/scsi_all.h>
102 #include <dev/scsipi/scsiconf.h>
103 #include <sys/scsiio.h>
105 #include <i386/Cbus/dev/scsi_low.h>
106 #endif /* __NetBSD__ */
110 #include <cam/cam_ccb.h>
111 #include <cam/cam_sim.h>
112 #include <cam/cam_debug.h>
113 #include <cam/cam_periph.h>
114 #include <cam/cam_xpt_periph.h>
116 #include <cam/scsi/scsi_all.h>
117 #include <cam/scsi/scsi_message.h>
119 #include <cam/scsi/scsi_low.h>
121 #include <sys/cons.h>
122 #endif /* __FreeBSD__ */
124 /**************************************************************
126 **************************************************************/
127 #define SCSI_LOW_POLL_HZ 1000
129 /* functions return values */
130 #define SCSI_LOW_START_NO_QTAG 0
131 #define SCSI_LOW_START_QTAG 1
133 #define SCSI_LOW_DONE_COMPLETE 0
134 #define SCSI_LOW_DONE_RETRY 1
136 /* internal disk flags */
137 #define SCSI_LOW_DISK_DISC 0x00000001
138 #define SCSI_LOW_DISK_QTAG 0x00000002
139 #define SCSI_LOW_DISK_LINK 0x00000004
140 #define SCSI_LOW_DISK_PARITY 0x00000008
141 #define SCSI_LOW_DISK_SYNC 0x00010000
142 #define SCSI_LOW_DISK_WIDE_16 0x00020000
143 #define SCSI_LOW_DISK_WIDE_32 0x00040000
144 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
145 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
146 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
148 MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
150 /**************************************************************
152 **************************************************************/
153 /* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
154 static void scsi_low_engage(void *);
155 static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
156 static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
157 static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
158 static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
159 static void scsi_low_twiddle_wait(void);
160 static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
161 static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
162 static void scsi_low_calcf_lun(struct lun_info *);
163 static void scsi_low_calcf_target(struct targ_info *);
164 static void scsi_low_calcf_show(struct lun_info *);
165 static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
166 static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
167 static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
168 static int scsi_low_init(struct scsi_low_softc *, u_int);
169 static void scsi_low_start(struct scsi_low_softc *);
170 static void scsi_low_free_ti(struct scsi_low_softc *);
172 static int scsi_low_alloc_qtag(struct slccb *);
173 static int scsi_low_dealloc_qtag(struct slccb *);
174 static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
175 static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
176 static void scsi_low_unit_ready_cmd(struct slccb *);
177 static void scsi_low_timeout(void *);
178 static int scsi_low_timeout_check(struct scsi_low_softc *);
179 #ifdef SCSI_LOW_START_UP_CHECK
180 static int scsi_low_start_up(struct scsi_low_softc *);
181 #endif /* SCSI_LOW_START_UP_CHECK */
182 static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
183 static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);
185 int scsi_low_version_major = 2;
186 int scsi_low_version_minor = 17;
188 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
190 /**************************************************************
191 * Debug, Run test and Statics
192 **************************************************************/
193 #ifdef SCSI_LOW_INFO_DETAIL
194 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
195 #else /* !SCSI_LOW_INFO_DETAIL */
196 #define SCSI_LOW_INFO(slp, ti, s) printf("%s: %s\n", (slp)->sl_xname, (s))
197 #endif /* !SCSI_LOW_INFO_DETAIL */
199 #ifdef SCSI_LOW_STATICS
200 static struct scsi_low_statics {
203 int nexus_disconnected;
204 int nexus_reselected;
207 #endif /* SCSI_LOW_STATICS */
209 #ifdef SCSI_LOW_DEBUG
210 #define SCSI_LOW_DEBUG_DONE 0x00001
211 #define SCSI_LOW_DEBUG_DISC 0x00002
212 #define SCSI_LOW_DEBUG_SENSE 0x00004
213 #define SCSI_LOW_DEBUG_CALCF 0x00008
214 #define SCSI_LOW_DEBUG_ACTION 0x10000
215 int scsi_low_debug = 0;
217 #define SCSI_LOW_MAX_ATTEN_CHECK 32
218 #define SCSI_LOW_ATTEN_CHECK 0x0001
219 #define SCSI_LOW_CMDLNK_CHECK 0x0002
220 #define SCSI_LOW_ABORT_CHECK 0x0004
221 #define SCSI_LOW_NEXUS_CHECK 0x0008
222 int scsi_low_test = 0;
223 int scsi_low_test_id = 0;
225 static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
226 static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
227 static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
228 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
229 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
230 #define SCSI_LOW_DEBUG_GO(fl, id) \
231 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
232 #endif /* SCSI_LOW_DEBUG */
234 /**************************************************************
236 **************************************************************/
237 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
238 GENERIC_CCB(scsi_low, slccb, ccb_chain)
240 /**************************************************************
242 **************************************************************/
243 #define SCSI_LOW_INLINE static __inline
244 SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
245 SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
246 SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
247 SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
248 SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
249 SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
250 SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);
253 scsi_low_activate_qtag(cb)
256 struct lun_info *li = cb->li;
258 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
262 cb->ccb_tag = cb->ccb_otag;
266 scsi_low_deactivate_qtag(cb)
269 struct lun_info *li = cb->li;
271 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
275 cb->ccb_tag = SCSI_LOW_UNKTAG;
279 scsi_low_ccb_message_exec(slp, cb)
280 struct scsi_low_softc *slp;
284 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
285 cb->ccb_msgoutflag = 0;
289 scsi_low_ccb_message_assert(cb, msg)
294 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
298 scsi_low_ccb_message_retry(cb)
301 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
305 scsi_low_ccb_message_clear(cb)
308 cb->ccb_msgoutflag = 0;
312 scsi_low_init_msgsys(slp, ti)
313 struct scsi_low_softc *slp;
314 struct targ_info *ti;
318 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
319 SCSI_LOW_DEASSERT_ATN(slp);
320 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
323 /*=============================================================
324 * START OF OS switch (All OS depend fucntions should be here)
325 =============================================================*/
326 /* common os depend utitlities */
327 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
328 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
329 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
331 static u_int8_t scsi_low_cmd_flags[256] = {
332 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
333 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
334 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
335 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
336 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
339 struct scsi_low_error_code {
344 static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
345 static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);
347 static struct slccb *
348 scsi_low_find_ccb(slp, target, lun, osdep)
349 struct scsi_low_softc *slp;
353 struct targ_info *ti;
357 ti = slp->sl_ti[target];
358 li = scsi_low_alloc_li(ti, lun, 0);
362 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
365 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
366 cb = TAILQ_NEXT(cb, ccb_chain))
368 if (cb->osdep == osdep)
372 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL;
373 cb = TAILQ_NEXT(cb, ccb_chain))
375 if (cb->osdep == osdep)
382 scsi_low_translate_error_code(cb, tp)
384 struct scsi_low_error_code *tp;
387 if (cb->ccb_error == 0)
388 return tp->error_code;
390 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
392 return tp->error_code;
395 #ifdef SCSI_LOW_INTERFACE_XS
396 /**************************************************************
397 * SCSI INTERFACE (XS)
398 **************************************************************/
399 #define SCSI_LOW_MINPHYS 0x10000
400 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
401 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
402 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb((flags))
403 #define SCSI_LOW_XS_POLL_HZ 1000
405 static int scsi_low_poll_xs(struct scsi_low_softc *, struct slccb *);
406 static void scsi_low_scsi_minphys_xs(struct buf *);
407 #ifdef SCSI_LOW_TARGET_OPEN
408 static int scsi_low_target_open(struct scsipi_link *, struct cfdata *);
409 #endif /* SCSI_LOW_TARGET_OPEN */
410 static int scsi_low_scsi_cmd_xs(struct scsipi_xfer *);
411 static int scsi_low_enable_xs(void *, int);
412 static int scsi_low_ioctl_xs(struct scsipi_link *, u_long, caddr_t, int, struct proc *);
414 static int scsi_low_attach_xs(struct scsi_low_softc *);
415 static int scsi_low_world_start_xs(struct scsi_low_softc *);
416 static int scsi_low_dettach_xs(struct scsi_low_softc *);
417 static int scsi_low_ccb_setup_xs(struct scsi_low_softc *, struct slccb *);
418 static int scsi_low_done_xs(struct scsi_low_softc *, struct slccb *);
419 static void scsi_low_timeout_xs(struct scsi_low_softc *, int, int);
420 static u_int scsi_low_translate_quirks_xs(u_int);
421 static void scsi_low_setup_quirks_xs(struct targ_info *, struct lun_info *, u_int);
423 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_xs = {
425 scsi_low_world_start_xs,
427 scsi_low_ccb_setup_xs,
432 struct scsipi_device scsi_low_dev = {
433 NULL, /* Use default error handler */
434 NULL, /* have a queue, served by this */
435 NULL, /* have no async handler */
436 NULL, /* Use default 'done' routine */
439 struct scsi_low_error_code scsi_low_error_code_xs[] = {
443 {SELTIMEOUTIO, XS_SELTIMEOUT},
444 {TIMEOUTIO, XS_TIMEOUT},
445 {-1, XS_DRIVER_STUFFUP}
449 scsi_low_ioctl_xs(link, cmd, addr, flag, p)
450 struct scsipi_link *link;
456 struct scsi_low_softc *slp;
457 int s, error = ENOTTY;
459 slp = (struct scsi_low_softc *) link->adapter_softc;
460 if ((slp->sl_flags & HW_INACTIVE) != 0)
463 if (cmd == SCBUSIORESET)
465 s = SCSI_LOW_SPLSCSI();
466 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
470 else if (slp->sl_funcs->scsi_low_ioctl != 0)
472 error = (*slp->sl_funcs->scsi_low_ioctl)
473 (slp, cmd, addr, flag, p);
480 scsi_low_enable_xs(arg, enable)
484 struct scsi_low_softc *slp = arg;
488 if ((slp->sl_flags & HW_INACTIVE) != 0)
493 if ((slp->sl_flags & HW_INACTIVE) != 0 ||
494 (slp->sl_flags & HW_POWERCTRL) == 0)
497 slp->sl_flags |= HW_POWDOWN;
498 if (slp->sl_funcs->scsi_low_power != NULL)
500 (*slp->sl_funcs->scsi_low_power)
501 (slp, SCSI_LOW_POWDOWN);
508 scsi_low_scsi_minphys_xs(bp)
512 if (bp->b_bcount > SCSI_LOW_MINPHYS)
513 bp->b_bcount = SCSI_LOW_MINPHYS;
518 scsi_low_poll_xs(slp, cb)
519 struct scsi_low_softc *slp;
522 struct scsipi_xfer *xs = cb->osdep;
525 cb->ccb_flags |= CCB_NOSDONE;
528 while (slp->sl_nio > 0)
530 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_XS_POLL_HZ);
532 (*slp->sl_funcs->scsi_low_poll) (slp);
534 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
536 cb->ccb_flags |= CCB_NORETRY;
537 cb->ccb_error |= FATALIO;
538 (void) scsi_low_revoke_ccb(slp, cb, 1);
539 printf("%s: hardware inactive in poll mode\n",
543 if ((xs->flags & ITSDONE) != 0)
546 if (tcount ++ < SCSI_LOW_XS_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
550 scsi_low_timeout_check(slp);
553 xs->flags |= ITSDONE;
559 scsi_low_scsi_cmd_xs(xs)
560 struct scsipi_xfer *xs;
562 struct scsipi_link *splp = xs->sc_link;
563 struct scsi_low_softc *slp = splp->adapter_softc;
564 struct targ_info *ti;
567 int s, targ, lun, flags, rv;
569 if ((cb = SCSI_LOW_ALLOC_CCB(xs->flags & SCSI_NOSLEEP)) == NULL)
570 return TRY_AGAIN_LATER;
572 targ = splp->scsipi_scsi.target,
573 lun = splp->scsipi_scsi.lun;
574 ti = slp->sl_ti[targ];
579 if ((xs->flags & SCSI_POLL) == 0)
580 flags = CCB_AUTOSENSE;
582 flags = CCB_AUTOSENSE | CCB_POLLED;
585 s = SCSI_LOW_SPLSCSI();
586 li = scsi_low_alloc_li(ti, lun, 1);
587 if ((u_int) splp->quirks != li->li_sloi.sloi_quirks)
589 scsi_low_setup_quirks_xs(ti, li, (u_int) splp->quirks);
592 if ((xs->flags & SCSI_RESET) != 0)
594 flags |= CCB_NORETRY | CCB_URGENT;
595 scsi_low_enqueue(slp, ti, li, cb, flags, SCSI_LOW_MSG_RESET);
599 if (ti->ti_setup_msg != 0)
601 scsi_low_message_enqueue(slp, ti, li, flags);
605 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
608 #ifdef SCSI_LOW_DEBUG
609 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, ti->ti_id) != 0)
611 scsi_low_test_abort(slp, ti, li);
613 #endif /* SCSI_LOW_DEBUG */
615 if ((cb->ccb_flags & CCB_POLLED) != 0)
617 rv = scsi_low_poll_xs(slp, cb);
621 rv = SUCCESSFULLY_QUEUED;
628 scsi_low_attach_xs(slp)
629 struct scsi_low_softc *slp;
631 struct scsipi_adapter *sap;
632 struct scsipi_link *splp;
634 strncpy(slp->sl_xname, slp->sl_dev.dv_xname, 16);
636 sap = SCSI_LOW_MALLOC(sizeof(*sap));
639 splp = SCSI_LOW_MALLOC(sizeof(*splp));
643 SCSI_LOW_BZERO(sap, sizeof(*sap));
644 SCSI_LOW_BZERO(splp, sizeof(*splp));
646 sap->scsipi_cmd = scsi_low_scsi_cmd_xs;
647 sap->scsipi_minphys = scsi_low_scsi_minphys_xs;
648 sap->scsipi_enable = scsi_low_enable_xs;
649 sap->scsipi_ioctl = scsi_low_ioctl_xs;
650 #ifdef SCSI_LOW_TARGET_OPEN
651 sap->open_target_lu = scsi_low_target_open;
652 #endif /* SCSI_LOW_TARGET_OPEN */
654 splp->adapter_softc = slp;
655 splp->scsipi_scsi.adapter_target = slp->sl_hostid;
656 splp->scsipi_scsi.max_target = slp->sl_ntargs - 1;
657 splp->scsipi_scsi.max_lun = slp->sl_nluns - 1;
658 splp->scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
659 splp->openings = slp->sl_openings;
660 splp->type = BUS_SCSI;
661 splp->adapter_softc = slp;
663 splp->device = &scsi_low_dev;
665 slp->sl_si.si_splp = splp;
666 slp->sl_show_result = SHOW_ALL_NEG;
671 scsi_low_world_start_xs(slp)
672 struct scsi_low_softc *slp;
679 scsi_low_dettach_xs(slp)
680 struct scsi_low_softc *slp;
684 * scsipi does not have dettach bus fucntion.
686 scsipi_dettach_scsibus(slp->sl_si.si_splp);
692 scsi_low_ccb_setup_xs(slp, cb)
693 struct scsi_low_softc *slp;
696 struct scsipi_xfer *xs = (struct scsipi_xfer *) cb->osdep;
698 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
700 cb->ccb_scp.scp_cmd = (u_int8_t *) xs->cmd;
701 cb->ccb_scp.scp_cmdlen = xs->cmdlen;
702 cb->ccb_scp.scp_data = xs->data;
703 cb->ccb_scp.scp_datalen = xs->datalen;
704 cb->ccb_scp.scp_direction = (xs->flags & SCSI_DATA_OUT) ?
705 SCSI_LOW_WRITE : SCSI_LOW_READ;
706 cb->ccb_tcmax = xs->timeout / 1000;
710 scsi_low_unit_ready_cmd(cb);
712 return SCSI_LOW_START_QTAG;
716 scsi_low_done_xs(slp, cb)
717 struct scsi_low_softc *slp;
720 struct scsipi_xfer *xs;
722 xs = (struct scsipi_xfer *) cb->osdep;
723 if (cb->ccb_error == 0)
725 xs->error = XS_NOERROR;
730 if (cb->ccb_rcnt >= slp->sl_max_retry)
731 cb->ccb_error |= ABORTIO;
733 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
734 (cb->ccb_error & ABORTIO) == 0)
737 if ((cb->ccb_error & SENSEIO) != 0)
739 xs->sense.scsi_sense = cb->ccb_sense;
742 xs->error = scsi_low_translate_error_code(cb,
743 &scsi_low_error_code_xs[0]);
745 #ifdef SCSI_LOW_DIAGNOSTIC
746 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
747 cb->ccb_scp.scp_cmdlen > 0 &&
748 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
749 SCSI_LOW_CMD_ABORT_WARNING) != 0)
751 printf("%s: WARNING: scsi_low IO abort\n",
753 scsi_low_print(slp, NULL);
755 #endif /* SCSI_LOW_DIAGNOSTIC */
758 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
759 xs->status = 0; /* XXX */
761 xs->status = cb->ccb_scp.scp_status;
763 xs->flags |= ITSDONE;
764 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
771 scsi_low_timeout_xs(slp, ch, action)
772 struct scsi_low_softc *slp;
779 case SCSI_LOW_TIMEOUT_CH_IO:
782 case SCSI_LOW_TIMEOUT_START:
783 timeout(scsi_low_timeout, slp,
784 hz / SCSI_LOW_TIMEOUT_HZ);
786 case SCSI_LOW_TIMEOUT_STOP:
787 untimeout(scsi_low_timeout, slp);
792 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
795 case SCSI_LOW_TIMEOUT_START:
796 timeout(scsi_low_engage, slp, 1);
798 case SCSI_LOW_TIMEOUT_STOP:
799 untimeout(scsi_low_engage, slp);
804 case SCSI_LOW_TIMEOUT_CH_RECOVER:
810 scsi_low_translate_quirks_xs(quirks)
815 flags = SCSI_LOW_DISK_LFLAGS | SCSI_LOW_DISK_TFLAGS;
818 if (quirks & SDEV_NODISC)
819 flags &= ~SCSI_LOW_DISK_DISC;
820 #endif /* SDEV_NODISC */
822 if (quirks & SDEV_NOPARITY)
823 flags &= ~SCSI_LOW_DISK_PARITY;
824 #endif /* SDEV_NOPARITY */
826 if (quirks & SDEV_NOCMDLNK)
827 flags &= ~SCSI_LOW_DISK_LINK;
828 #endif /* SDEV_NOCMDLNK */
830 if (quirks & SDEV_NOTAG)
831 flags &= ~SCSI_LOW_DISK_QTAG;
832 #endif /* SDEV_NOTAG */
834 if (quirks & SDEV_NOSYNC)
835 flags &= ~SCSI_LOW_DISK_SYNC;
836 #endif /* SDEV_NOSYNC */
842 scsi_low_setup_quirks_xs(ti, li, flags)
843 struct targ_info *ti;
849 li->li_sloi.sloi_quirks = flags;
850 quirks = scsi_low_translate_quirks_xs(flags);
851 ti->ti_quirks = quirks & SCSI_LOW_DISK_TFLAGS;
852 li->li_quirks = quirks & SCSI_LOW_DISK_LFLAGS;
853 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
854 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
855 scsi_low_calcf_target(ti);
856 scsi_low_calcf_lun(li);
857 scsi_low_calcf_show(li);
860 #ifdef SCSI_LOW_TARGET_OPEN
862 scsi_low_target_open(link, cf)
863 struct scsipi_link *link;
866 u_int target = link->scsipi_scsi.target;
867 u_int lun = link->scsipi_scsi.lun;
868 struct scsi_low_softc *slp;
869 struct targ_info *ti;
872 slp = (struct scsi_low_softc *) link->adapter_softc;
873 ti = slp->sl_ti[target];
874 li = scsi_low_alloc_li(ti, lun, 0);
878 li->li_cfgflags = cf->cf_flags;
879 scsi_low_setup_quirks_xs(ti, li, (u_int) link->quirks);
882 #endif /* SCSI_LOW_TARGET_OPEN */
884 #endif /* SCSI_LOW_INTERFACE_XS */
886 #ifdef SCSI_LOW_INTERFACE_CAM
887 /**************************************************************
888 * SCSI INTERFACE (CAM)
889 **************************************************************/
890 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
891 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
892 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
894 static void scsi_low_poll_cam(struct cam_sim *);
895 static void scsi_low_cam_rescan_callback(struct cam_periph *, union ccb *);
896 static void scsi_low_rescan_bus_cam(struct scsi_low_softc *);
897 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
899 static int scsi_low_attach_cam(struct scsi_low_softc *);
900 static int scsi_low_world_start_cam(struct scsi_low_softc *);
901 static int scsi_low_dettach_cam(struct scsi_low_softc *);
902 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
903 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
904 static void scsi_low_timeout_cam(struct scsi_low_softc *, int, int);
906 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
908 scsi_low_world_start_cam,
909 scsi_low_dettach_cam,
910 scsi_low_ccb_setup_cam,
915 struct scsi_low_error_code scsi_low_error_code_cam[] = {
917 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
918 {SENSEERR, CAM_AUTOSENSE_FAIL},
919 {UACAERR, CAM_SCSI_STATUS_ERROR},
920 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
921 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
922 {TIMEOUTIO, CAM_CMD_TIMEOUT},
923 {PDMAERR, CAM_DATA_RUN_ERR},
924 {PARITYERR, CAM_UNCOR_PARITY},
925 {UBFERR, CAM_UNEXP_BUSFREE},
926 {ABORTIO, CAM_REQ_ABORTED},
927 {-1, CAM_UNREC_HBA_ERROR}
930 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
933 * Please check a polling hz, currently we assume scsi_low_poll() is
936 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
939 scsi_low_poll_cam(sim)
942 struct scsi_low_softc *slp = SIM2SLP(sim);
944 (*slp->sl_funcs->scsi_low_poll) (slp);
946 if (slp->sl_si.si_poll_count ++ >=
947 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
949 slp->sl_si.si_poll_count = 0;
950 scsi_low_timeout_check(slp);
955 scsi_low_cam_rescan_callback(periph, ccb)
956 struct cam_periph *periph;
960 xpt_free_path(ccb->ccb_h.path);
965 scsi_low_rescan_bus_cam(slp)
966 struct scsi_low_softc *slp;
968 struct cam_path *path;
969 union ccb *ccb = xpt_alloc_ccb();
972 bzero(ccb, sizeof(union ccb));
974 status = xpt_create_path(&path, xpt_periph,
975 cam_sim_path(slp->sl_si.sim), -1, 0);
976 if (status != CAM_REQ_CMP)
979 xpt_setup_ccb(&ccb->ccb_h, path, 5);
980 ccb->ccb_h.func_code = XPT_SCAN_BUS;
981 ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
982 ccb->crcn.flags = CAM_FLAG_NONE;
987 scsi_low_scsi_action_cam(sim, ccb)
991 struct scsi_low_softc *slp = SIM2SLP(sim);
992 struct targ_info *ti;
995 u_int lun, flags, msg, target;
998 target = (u_int) (ccb->ccb_h.target_id);
999 lun = (u_int) ccb->ccb_h.target_lun;
1001 #ifdef SCSI_LOW_DEBUG
1002 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
1004 printf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
1005 slp->sl_xname, ccb->ccb_h.func_code, target, lun);
1007 #endif /* SCSI_LOW_DEBUG */
1009 switch (ccb->ccb_h.func_code) {
1010 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1011 #ifdef SCSI_LOW_DIAGNOSTIC
1012 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
1014 printf("%s: invalid target/lun\n", slp->sl_xname);
1015 ccb->ccb_h.status = CAM_REQ_INVALID;
1019 #endif /* SCSI_LOW_DIAGNOSTIC */
1021 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
1022 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1027 ti = slp->sl_ti[target];
1030 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1031 flags = CCB_AUTOSENSE | CCB_SCSIIO;
1035 s = SCSI_LOW_SPLSCSI();
1036 li = scsi_low_alloc_li(ti, lun, 1);
1038 if (ti->ti_setup_msg != 0)
1040 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
1043 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
1045 #ifdef SCSI_LOW_DEBUG
1046 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
1048 scsi_low_test_abort(slp, ti, li);
1050 #endif /* SCSI_LOW_DEBUG */
1054 case XPT_EN_LUN: /* Enable LUN as a target */
1055 case XPT_TARGET_IO: /* Execute target I/O request */
1056 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1057 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1059 ccb->ccb_h.status = CAM_REQ_INVALID;
1063 case XPT_ABORT: /* Abort the specified CCB */
1064 #ifdef SCSI_LOW_DIAGNOSTIC
1065 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
1067 printf("%s: invalid target/lun\n", slp->sl_xname);
1068 ccb->ccb_h.status = CAM_REQ_INVALID;
1072 #endif /* SCSI_LOW_DIAGNOSTIC */
1074 s = SCSI_LOW_SPLSCSI();
1075 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
1076 rv = scsi_low_abort_ccb(slp, cb);
1080 ccb->ccb_h.status = CAM_REQ_CMP;
1082 ccb->ccb_h.status = CAM_REQ_INVALID;
1086 case XPT_SET_TRAN_SETTINGS: {
1087 struct ccb_trans_settings *cts;
1090 #ifdef SCSI_LOW_DIAGNOSTIC
1091 if (target == CAM_TARGET_WILDCARD)
1093 printf("%s: invalid target\n", slp->sl_xname);
1094 ccb->ccb_h.status = CAM_REQ_INVALID;
1098 #endif /* SCSI_LOW_DIAGNOSTIC */
1100 ti = slp->sl_ti[target];
1101 if (lun == CAM_LUN_WILDCARD)
1104 s = SCSI_LOW_SPLSCSI();
1105 if ((cts->valid & (CCB_TRANS_BUS_WIDTH_VALID |
1106 CCB_TRANS_SYNC_RATE_VALID |
1107 CCB_TRANS_SYNC_OFFSET_VALID)) != 0)
1109 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
1110 val = cts->bus_width;
1111 if (val < ti->ti_width)
1114 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
1115 val = cts->sync_period;
1116 if (val == 0 || val > ti->ti_maxsynch.period)
1117 ti->ti_maxsynch.period = val;
1119 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) {
1120 val = cts->sync_offset;
1121 if (val < ti->ti_maxsynch.offset)
1122 ti->ti_maxsynch.offset = val;
1125 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1126 scsi_low_calcf_target(ti);
1129 if ((cts->valid & (CCB_TRANS_DISC_VALID |
1130 CCB_TRANS_TQ_VALID)) != 0)
1132 li = scsi_low_alloc_li(ti, lun, 1);
1133 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0)
1135 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
1136 li->li_quirks |= SCSI_LOW_DISK_DISC;
1138 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
1140 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
1142 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
1143 li->li_quirks |= SCSI_LOW_DISK_QTAG;
1145 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
1148 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1149 scsi_low_calcf_target(ti);
1150 scsi_low_calcf_lun(li);
1151 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
1152 scsi_low_calcf_show(li);
1156 ccb->ccb_h.status = CAM_REQ_CMP;
1161 case XPT_GET_TRAN_SETTINGS: {
1162 struct ccb_trans_settings *cts;
1166 #ifdef SCSI_LOW_DIAGNOSTIC
1167 if (target == CAM_TARGET_WILDCARD)
1169 printf("%s: invalid target\n", slp->sl_xname);
1170 ccb->ccb_h.status = CAM_REQ_INVALID;
1174 #endif /* SCSI_LOW_DIAGNOSTIC */
1175 ti = slp->sl_ti[target];
1176 if (lun == CAM_LUN_WILDCARD)
1179 s = SCSI_LOW_SPLSCSI();
1180 li = scsi_low_alloc_li(ti, lun, 1);
1181 #ifdef CAM_NEW_TRAN_CODE
1182 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1183 struct ccb_trans_settings_scsi *scsi =
1184 &cts->proto_specific.scsi;
1185 struct ccb_trans_settings_spi *spi =
1186 &cts->xport_specific.spi;
1187 #ifdef SCSI_LOW_DIAGNOSTIC
1188 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
1190 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1191 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
1195 #endif /* SCSI_LOW_DIAGNOSTIC */
1196 cts->protocol = PROTO_SCSI;
1197 cts->protocol_version = SCSI_REV_2;
1198 cts->transport = XPORT_SPI;
1199 cts->transport_version = 2;
1201 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1202 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1204 diskflags = li->li_diskflags & li->li_cfgflags;
1205 if (diskflags & SCSI_LOW_DISK_DISC)
1206 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1207 if (diskflags & SCSI_LOW_DISK_QTAG)
1208 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1210 spi->sync_period = ti->ti_maxsynch.period;
1211 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1212 spi->sync_offset = ti->ti_maxsynch.offset;
1213 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1215 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1216 spi->bus_width = ti->ti_width;
1218 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1219 scsi->valid = CTS_SCSI_VALID_TQ;
1220 spi->valid |= CTS_SPI_VALID_DISC;
1224 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1226 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0)
1228 #ifdef SCSI_LOW_DIAGNOSTIC
1229 if ((li->li_flags_valid & SCSI_LOW_LUN_FLAGS_DISK_VALID) == 0)
1231 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1232 printf("%s: invalid GET_TRANS_USER_SETTINGS call\n",
1236 #endif /* SCSI_LOW_DIAGNOSTIC */
1237 diskflags = li->li_diskflags & li->li_cfgflags;
1238 if ((diskflags & SCSI_LOW_DISK_DISC) != 0)
1239 cts->flags |= CCB_TRANS_DISC_ENB;
1241 cts->flags &= ~CCB_TRANS_DISC_ENB;
1242 if ((diskflags & SCSI_LOW_DISK_QTAG) != 0)
1243 cts->flags |= CCB_TRANS_TAG_ENB;
1245 cts->flags &= ~CCB_TRANS_TAG_ENB;
1247 else if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
1249 #ifdef SCSI_LOW_DIAGNOSTIC
1250 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
1252 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1253 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
1257 #endif /* SCSI_LOW_DIAGNOSTIC */
1258 if ((li->li_flags & SCSI_LOW_DISC) != 0)
1259 cts->flags |= CCB_TRANS_DISC_ENB;
1261 cts->flags &= ~CCB_TRANS_DISC_ENB;
1262 if ((li->li_flags & SCSI_LOW_QTAG) != 0)
1263 cts->flags |= CCB_TRANS_TAG_ENB;
1265 cts->flags &= ~CCB_TRANS_TAG_ENB;
1269 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1273 cts->sync_period = ti->ti_maxsynch.period;
1274 cts->sync_offset = ti->ti_maxsynch.offset;
1275 cts->bus_width = ti->ti_width;
1277 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1278 | CCB_TRANS_SYNC_OFFSET_VALID
1279 | CCB_TRANS_BUS_WIDTH_VALID
1280 | CCB_TRANS_DISC_VALID
1281 | CCB_TRANS_TQ_VALID;
1282 ccb->ccb_h.status = CAM_REQ_CMP;
1290 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
1291 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1296 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1297 s = SCSI_LOW_SPLSCSI();
1298 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
1300 ccb->ccb_h.status = CAM_REQ_CMP;
1304 case XPT_TERM_IO: /* Terminate the I/O process */
1305 ccb->ccb_h.status = CAM_REQ_INVALID;
1309 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1310 #ifdef SCSI_LOW_DIAGNOSTIC
1311 if (target == CAM_TARGET_WILDCARD)
1313 printf("%s: invalid target\n", slp->sl_xname);
1314 ccb->ccb_h.status = CAM_REQ_INVALID;
1318 #endif /* SCSI_LOW_DIAGNOSTIC */
1320 msg = SCSI_LOW_MSG_RESET;
1321 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
1323 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1328 ti = slp->sl_ti[target];
1329 if (lun == CAM_LUN_WILDCARD)
1333 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1334 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
1336 flags = CCB_NORETRY | CCB_URGENT;
1338 s = SCSI_LOW_SPLSCSI();
1339 li = scsi_low_alloc_li(ti, lun, 1);
1340 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
1344 case XPT_PATH_INQ: { /* Path routing inquiry */
1345 struct ccb_pathinq *cpi = &ccb->cpi;
1347 cpi->version_num = scsi_low_version_major;
1348 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
1349 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
1350 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
1351 cpi->hba_inquiry |= PI_WIDE_16;
1352 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
1353 cpi->hba_inquiry |= PI_WIDE_32;
1354 if (ti->ti_maxsynch.offset > 0)
1355 cpi->hba_inquiry |= PI_SDTR_ABLE;
1356 cpi->target_sprt = 0;
1358 cpi->hba_eng_cnt = 0;
1359 cpi->max_target = slp->sl_ntargs - 1;
1360 cpi->max_lun = slp->sl_nluns - 1;
1361 cpi->initiator_id = slp->sl_hostid;
1362 cpi->bus_id = cam_sim_bus(sim);
1363 cpi->base_transfer_speed = 3300;
1364 #ifdef CAM_NEW_TRAN_CODE
1365 cpi->transport = XPORT_SPI;
1366 cpi->transport_version = 2;
1367 cpi->protocol = PROTO_SCSI;
1368 cpi->protocol_version = SCSI_REV_2;
1370 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1371 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
1372 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1373 cpi->unit_number = cam_sim_unit(sim);
1374 cpi->ccb_h.status = CAM_REQ_CMP;
1380 printf("scsi_low: non support func_code = %d ",
1381 ccb->ccb_h.func_code);
1382 ccb->ccb_h.status = CAM_REQ_INVALID;
1389 scsi_low_attach_cam(slp)
1390 struct scsi_low_softc *slp;
1392 struct cam_devq *devq;
1393 int tagged_openings;
1395 sprintf(slp->sl_xname, "%s%d",
1396 DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
1398 devq = cam_simq_alloc(SCSI_LOW_NCCB);
1403 * ask the adapter what subunits are present
1405 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
1406 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
1408 DEVPORT_DEVNAME(slp->sl_dev), slp,
1409 DEVPORT_DEVUNIT(slp->sl_dev),
1410 slp->sl_openings, tagged_openings, devq);
1412 if (slp->sl_si.sim == NULL) {
1413 cam_simq_free(devq);
1417 if (xpt_bus_register(slp->sl_si.sim, 0) != CAM_SUCCESS) {
1418 free(slp->sl_si.sim, M_SCSILOW);
1422 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
1423 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
1424 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1425 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1426 cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
1430 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
1435 scsi_low_world_start_cam(slp)
1436 struct scsi_low_softc *slp;
1440 scsi_low_rescan_bus_cam(slp);
1445 scsi_low_dettach_cam(slp)
1446 struct scsi_low_softc *slp;
1449 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
1450 xpt_free_path(slp->sl_si.path);
1451 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1452 cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
1457 scsi_low_ccb_setup_cam(slp, cb)
1458 struct scsi_low_softc *slp;
1461 union ccb *ccb = (union ccb *) cb->osdep;
1463 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
1465 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
1466 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
1467 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
1468 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
1469 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1470 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
1471 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
1472 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1473 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
1477 scsi_low_unit_ready_cmd(cb);
1479 return SCSI_LOW_START_QTAG;
1483 scsi_low_done_cam(slp, cb)
1484 struct scsi_low_softc *slp;
1489 ccb = (union ccb *) cb->osdep;
1490 if (cb->ccb_error == 0)
1492 ccb->ccb_h.status = CAM_REQ_CMP;
1493 ccb->csio.resid = 0;
1497 if (cb->ccb_rcnt >= slp->sl_max_retry)
1498 cb->ccb_error |= ABORTIO;
1500 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
1501 (cb->ccb_error & ABORTIO) == 0)
1504 if ((cb->ccb_error & SENSEIO) != 0)
1506 memcpy(&ccb->csio.sense_data,
1508 sizeof(ccb->csio.sense_data));
1511 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
1512 &scsi_low_error_code_cam[0]);
1514 #ifdef SCSI_LOW_DIAGNOSTIC
1515 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
1516 cb->ccb_scp.scp_cmdlen > 0 &&
1517 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1518 SCSI_LOW_CMD_ABORT_WARNING) != 0)
1520 printf("%s: WARNING: scsi_low IO abort\n",
1522 scsi_low_print(slp, NULL);
1524 #endif /* SCSI_LOW_DIAGNOSTIC */
1527 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
1528 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1530 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
1531 ccb->csio.scsi_status = 0; /* XXX */
1533 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
1535 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
1541 scsi_low_timeout_cam(slp, ch, action)
1542 struct scsi_low_softc *slp;
1549 case SCSI_LOW_TIMEOUT_CH_IO:
1552 case SCSI_LOW_TIMEOUT_START:
1553 slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
1554 hz / SCSI_LOW_TIMEOUT_HZ);
1556 case SCSI_LOW_TIMEOUT_STOP:
1557 untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
1562 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
1565 case SCSI_LOW_TIMEOUT_START:
1566 slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
1568 case SCSI_LOW_TIMEOUT_STOP:
1569 untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
1573 case SCSI_LOW_TIMEOUT_CH_RECOVER:
1578 #endif /* SCSI_LOW_INTERFACE_CAM */
1580 /*=============================================================
1581 * END OF OS switch (All OS depend fucntions should be above)
1582 =============================================================*/
1584 /**************************************************************
1585 * scsi low deactivate and activate
1586 **************************************************************/
1588 scsi_low_is_busy(slp)
1589 struct scsi_low_softc *slp;
1592 if (slp->sl_nio > 0)
1598 scsi_low_deactivate(slp)
1599 struct scsi_low_softc *slp;
1603 s = SCSI_LOW_SPLSCSI();
1604 slp->sl_flags |= HW_INACTIVE;
1605 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1606 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
1607 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1608 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1614 scsi_low_activate(slp)
1615 struct scsi_low_softc *slp;
1619 s = SCSI_LOW_SPLSCSI();
1620 slp->sl_flags &= ~HW_INACTIVE;
1621 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
1623 slp->sl_flags |= HW_INACTIVE;
1628 slp->sl_timeout_count = 0;
1629 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1630 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1635 /**************************************************************
1637 **************************************************************/
1638 #ifdef SCSI_LOW_DIAGNOSTIC
1639 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
1640 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
1641 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
1644 scsi_low_msg_log_init(slmlp)
1645 struct scsi_low_msg_log *slmlp;
1648 slmlp->slml_ptr = 0;
1652 scsi_low_msg_log_write(slmlp, datap, len)
1653 struct scsi_low_msg_log *slmlp;
1659 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1662 ptr = slmlp->slml_ptr ++;
1663 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1664 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1665 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1666 slmlp->slml_msg[ptr].msg[ind] = 0;
1670 scsi_low_msg_log_show(slmlp, s, len)
1671 struct scsi_low_msg_log *slmlp;
1677 printf("%s: (%d) ", s, slmlp->slml_ptr);
1678 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1680 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1683 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1689 #endif /* SCSI_LOW_DIAGNOSTIC */
1691 /**************************************************************
1693 **************************************************************/
1695 scsi_low_engage(arg)
1698 struct scsi_low_softc *slp = arg;
1699 int s = SCSI_LOW_SPLSCSI();
1701 switch (slp->sl_rstep)
1705 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1706 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1707 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1712 slp->sl_flags &= ~HW_RESUME;
1713 scsi_low_start(slp);
1723 scsi_low_init(slp, flags)
1724 struct scsi_low_softc *slp;
1729 slp->sl_flags |= HW_INITIALIZING;
1731 /* clear power control timeout */
1732 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1734 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1735 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1736 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1738 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1741 /* reset current nexus */
1742 scsi_low_reset_nexus(slp, flags);
1743 if ((slp->sl_flags & HW_INACTIVE) != 0)
1749 if (flags != SCSI_LOW_RESTART_SOFT)
1751 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1755 slp->sl_flags &= ~HW_INITIALIZING;
1759 /**************************************************************
1761 **************************************************************/
1762 static struct lun_info *
1763 scsi_low_alloc_li(ti, lun, alloc)
1764 struct targ_info *ti;
1768 struct scsi_low_softc *slp = ti->ti_sc;
1769 struct lun_info *li;
1771 li = LIST_FIRST(&ti->ti_litab);
1774 if (li->li_lun == lun)
1777 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1779 if (li->li_lun == lun)
1781 LIST_REMOVE(li, lun_chain);
1782 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1791 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1793 panic("no lun info mem");
1795 SCSI_LOW_BZERO(li, ti->ti_lunsize);
1799 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1801 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1802 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1803 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1804 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1805 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1807 li->li_qtagbits = (u_int) -1;
1809 TAILQ_INIT(&li->li_discq);
1810 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1812 /* host specific structure initialization per lun */
1813 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1814 (*slp->sl_funcs->scsi_low_lun_init)
1815 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1816 scsi_low_calcf_lun(li);
1820 /**************************************************************
1821 * allocate targ_info
1822 **************************************************************/
1823 static struct targ_info *
1824 scsi_low_alloc_ti(slp, targ)
1825 struct scsi_low_softc *slp;
1828 struct targ_info *ti;
1830 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1831 TAILQ_INIT(&slp->sl_titab);
1833 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1835 panic("%s short of memory", slp->sl_xname);
1837 SCSI_LOW_BZERO(ti, slp->sl_targsize);
1841 slp->sl_ti[targ] = ti;
1842 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1843 LIST_INIT(&ti->ti_litab);
1845 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1846 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1847 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1848 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1849 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1850 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1852 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1854 (*slp->sl_funcs->scsi_low_targ_init)
1855 (slp, ti, SCSI_LOW_INFO_ALLOC);
1857 scsi_low_calcf_target(ti);
1862 scsi_low_free_ti(slp)
1863 struct scsi_low_softc *slp;
1865 struct targ_info *ti, *tib;
1866 struct lun_info *li, *nli;
1868 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1870 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1872 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1874 (*slp->sl_funcs->scsi_low_lun_init)
1875 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1877 nli = LIST_NEXT(li, lun_chain);
1881 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1883 (*slp->sl_funcs->scsi_low_targ_init)
1884 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1886 tib = TAILQ_NEXT(ti, ti_chain);
1891 /**************************************************************
1893 **************************************************************/
1895 scsi_low_bus_idle(slp)
1896 struct scsi_low_softc *slp;
1899 slp->sl_retry_sel = 0;
1900 if (slp->sl_Tnexus == NULL)
1901 scsi_low_start(slp);
1905 scsi_low_timeout(arg)
1908 struct scsi_low_softc *slp = arg;
1911 s = SCSI_LOW_SPLSCSI();
1912 (void) scsi_low_timeout_check(slp);
1913 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1914 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1919 scsi_low_timeout_check(slp)
1920 struct scsi_low_softc *slp;
1922 struct targ_info *ti;
1923 struct lun_info *li;
1924 struct slccb *cb = NULL; /* XXX */
1926 /* selection restart */
1927 if (slp->sl_retry_sel != 0)
1929 slp->sl_retry_sel = 0;
1930 if (slp->sl_Tnexus != NULL)
1933 cb = TAILQ_FIRST(&slp->sl_start);
1937 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1939 cb->ccb_flags |= CCB_NORETRY;
1940 cb->ccb_error |= SELTIMEOUTIO;
1941 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1942 panic("%s: ccb not finished", slp->sl_xname);
1945 if (slp->sl_Tnexus == NULL)
1946 scsi_low_start(slp);
1949 /* call hardware timeout */
1951 if (slp->sl_funcs->scsi_low_timeout != NULL)
1953 (*slp->sl_funcs->scsi_low_timeout) (slp);
1956 if (slp->sl_timeout_count ++ <
1957 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1960 slp->sl_timeout_count = 0;
1961 if (slp->sl_nio > 0)
1963 if ((cb = slp->sl_Qnexus) != NULL)
1965 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1969 else if (slp->sl_disc == 0)
1971 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1974 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1978 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1979 ti = TAILQ_NEXT(ti, ti_chain))
1981 if (ti->ti_disc == 0)
1984 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1985 li = LIST_NEXT(li, lun_chain))
1987 for (cb = TAILQ_FIRST(&li->li_discq);
1989 cb = TAILQ_NEXT(cb, ccb_chain))
1992 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
2000 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
2002 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
2005 if (slp->sl_active != 0)
2007 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
2013 if (slp->sl_powc < 0)
2015 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
2016 slp->sl_flags |= HW_POWDOWN;
2017 (*slp->sl_funcs->scsi_low_power)
2018 (slp, SCSI_LOW_POWDOWN);
2024 cb->ccb_error |= TIMEOUTIO;
2025 printf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
2026 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
2027 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
2028 scsi_low_start(slp);
2034 scsi_low_abort_ccb(slp, cb)
2035 struct scsi_low_softc *slp;
2038 struct targ_info *ti;
2039 struct lun_info *li;
2044 if ((cb->ccb_omsgoutflag &
2045 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
2050 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2051 msg = SCSI_LOW_MSG_ABORT;
2053 msg = SCSI_LOW_MSG_ABORT_QTAG;
2055 cb->ccb_error |= ABORTIO;
2056 cb->ccb_flags |= CCB_NORETRY;
2057 scsi_low_ccb_message_assert(cb, msg);
2059 if (cb == slp->sl_Qnexus)
2061 scsi_low_assert_msg(slp, ti, msg, 1);
2063 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
2065 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
2066 panic("%s: revoked ccb done", slp->sl_xname);
2068 cb->ccb_flags |= CCB_STARTQ;
2069 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2071 if (slp->sl_Tnexus == NULL)
2072 scsi_low_start(slp);
2076 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
2077 panic("%s: revoked ccb retried", slp->sl_xname);
2082 /**************************************************************
2083 * Generic SCSI INTERFACE
2084 **************************************************************/
2086 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
2087 struct scsi_low_softc *slp;
2088 int openings, ntargs, nluns, targsize, lunsize;
2090 struct targ_info *ti;
2091 struct lun_info *li;
2094 #ifdef SCSI_LOW_INTERFACE_XS
2095 slp->sl_osdep_fp = &scsi_low_osdep_funcs_xs;
2096 #endif /* SCSI_LOW_INTERFACE_XS */
2097 #ifdef SCSI_LOW_INTERFACE_CAM
2098 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
2099 #endif /* SCSI_LOW_INTERFACE_CAM */
2101 if (slp->sl_osdep_fp == NULL)
2102 panic("scsi_low: interface not spcified");
2104 if (ntargs > SCSI_LOW_NTARGETS)
2106 printf("scsi_low: %d targets are too large\n", ntargs);
2107 printf("change kernel options SCSI_LOW_NTARGETS");
2112 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
2114 slp->sl_openings = openings;
2115 slp->sl_ntargs = ntargs;
2116 slp->sl_nluns = nluns;
2117 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
2119 if (lunsize < sizeof(struct lun_info))
2120 lunsize = sizeof(struct lun_info);
2122 if (targsize < sizeof(struct targ_info))
2123 targsize = sizeof(struct targ_info);
2125 slp->sl_targsize = targsize;
2126 for (i = 0; i < ntargs; i ++)
2128 ti = scsi_low_alloc_ti(slp, i);
2129 ti->ti_lunsize = lunsize;
2130 li = scsi_low_alloc_li(ti, 0, 1);
2133 /* initialize queue */
2134 nccb = openings * ntargs;
2135 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
2136 nccb = SCSI_LOW_NCCB;
2137 scsi_low_init_ccbque(nccb);
2138 TAILQ_INIT(&slp->sl_start);
2140 /* call os depend attach */
2141 s = SCSI_LOW_SPLSCSI();
2142 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
2146 printf("%s: scsi_low_attach: osdep attach failed\n",
2151 /* check hardware */
2152 SCSI_LOW_DELAY(1000); /* wait for 1ms */
2153 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
2156 printf("%s: scsi_low_attach: initialization failed\n",
2161 /* start watch dog */
2162 slp->sl_timeout_count = 0;
2163 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2164 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
2165 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
2168 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
2170 #ifdef SCSI_LOW_START_UP_CHECK
2171 /* probing devices */
2172 scsi_low_start_up(slp);
2173 #endif /* SCSI_LOW_START_UP_CHECK */
2175 /* call os depend attach done*/
2176 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
2182 scsi_low_dettach(slp)
2183 struct scsi_low_softc *slp;
2187 s = SCSI_LOW_SPLSCSI();
2188 if (scsi_low_is_busy(slp) != 0)
2194 scsi_low_deactivate(slp);
2196 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
2203 scsi_low_free_ti(slp);
2204 LIST_REMOVE(slp, sl_chain);
2209 /**************************************************************
2211 **************************************************************/
2213 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
2214 struct scsi_low_softc *slp;
2215 struct targ_info *ti;
2216 struct lun_info *li;
2224 scsi_low_ccb_message_assert(cb, msg);
2226 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
2227 scsi_low_alloc_qtag(cb);
2229 cb->ccb_flags = flags | CCB_STARTQ;
2230 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2231 cb->ccb_error |= PENDINGIO;
2233 if ((flags & CCB_URGENT) != 0)
2235 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2239 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
2244 if (slp->sl_Tnexus == NULL)
2245 scsi_low_start(slp);
2250 scsi_low_message_enqueue(slp, ti, li, flags)
2251 struct scsi_low_softc *slp;
2252 struct targ_info *ti;
2253 struct lun_info *li;
2259 tmsgflags = ti->ti_setup_msg;
2260 ti->ti_setup_msg = 0;
2262 flags |= CCB_NORETRY;
2263 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
2268 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
2272 /**************************************************************
2273 * Generic Start & Done
2274 **************************************************************/
2275 #define SLSC_MODE_SENSE_SHORT 0x1a
2276 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
2277 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
2278 sizeof(struct scsi_low_mode_sense_data), 0};
2279 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
2280 sizeof(struct scsi_low_inq_data), 0};
2281 static u_int8_t unit_ready_cmd[6];
2282 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2283 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2284 static int scsi_low_resume(struct scsi_low_softc *);
2287 scsi_low_unit_ready_cmd(cb)
2291 cb->ccb_scp.scp_cmd = unit_ready_cmd;
2292 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2293 cb->ccb_scp.scp_datalen = 0;
2294 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2299 scsi_low_sense_abort_start(slp, ti, li, cb)
2300 struct scsi_low_softc *slp;
2301 struct targ_info *ti;
2302 struct lun_info *li;
2306 cb->ccb_scp.scp_cmdlen = 6;
2307 SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
2308 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
2309 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
2310 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
2311 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
2312 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
2313 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2315 scsi_low_ccb_message_clear(cb);
2316 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
2318 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2322 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
2323 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
2324 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
2325 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
2328 return SCSI_LOW_START_NO_QTAG;
2332 scsi_low_setup_start(slp, ti, li, cb)
2333 struct scsi_low_softc *slp;
2334 struct targ_info *ti;
2335 struct lun_info *li;
2339 switch(li->li_state)
2341 case SCSI_LOW_LUN_SLEEP:
2342 scsi_low_unit_ready_cmd(cb);
2345 case SCSI_LOW_LUN_START:
2346 cb->ccb_scp.scp_cmd = ss_cmd;
2347 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
2348 cb->ccb_scp.scp_datalen = 0;
2349 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2353 case SCSI_LOW_LUN_INQ:
2354 cb->ccb_scp.scp_cmd = inq_cmd;
2355 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
2356 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
2357 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
2358 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2362 case SCSI_LOW_LUN_MODEQ:
2363 cb->ccb_scp.scp_cmd = sms_cmd;
2364 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
2365 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
2366 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
2367 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2369 return SCSI_LOW_START_QTAG;
2372 panic("%s: no setup phase", slp->sl_xname);
2375 return SCSI_LOW_START_NO_QTAG;
2379 scsi_low_resume(slp)
2380 struct scsi_low_softc *slp;
2383 if (slp->sl_flags & HW_RESUME)
2385 slp->sl_flags &= ~HW_POWDOWN;
2386 if (slp->sl_funcs->scsi_low_power != NULL)
2388 slp->sl_flags |= HW_RESUME;
2390 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
2391 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2392 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
2393 SCSI_LOW_TIMEOUT_START);
2401 struct scsi_low_softc *slp;
2403 struct targ_info *ti;
2404 struct lun_info *li;
2408 /* check hardware exists or under initializations ? */
2409 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
2412 /* check hardware power up ? */
2413 if ((slp->sl_flags & HW_POWERCTRL) != 0)
2416 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
2418 if (scsi_low_resume(slp) == EJUSTRETURN)
2424 #ifdef SCSI_LOW_DIAGNOSTIC
2425 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
2427 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
2428 panic("%s: inconsistent", slp->sl_xname);
2430 #endif /* SCSI_LOW_DIAGNOSTIC */
2432 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
2433 cb = TAILQ_NEXT(cb, ccb_chain))
2437 if (li->li_disc == 0)
2439 goto scsi_low_cmd_start;
2441 else if (li->li_nqio > 0)
2443 if (li->li_nqio < li->li_maxnqio ||
2444 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2445 goto scsi_low_cmd_start;
2451 cb->ccb_flags &= ~CCB_STARTQ;
2452 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
2455 /* clear all error flag bits (for restart) */
2457 cb->ccb_datalen = -1;
2458 cb->ccb_scp.scp_status = ST_UNKNOWN;
2460 /* setup nexus pointer */
2461 slp->sl_Qnexus = cb;
2462 slp->sl_Lnexus = li;
2463 slp->sl_Tnexus = ti;
2465 /* initialize msgsys */
2466 scsi_low_init_msgsys(slp, ti);
2469 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2471 /* CA state or forced abort */
2472 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
2474 else if (li->li_state >= SCSI_LOW_LUN_OK)
2476 cb->ccb_flags &= ~CCB_INTERNAL;
2477 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
2478 if (cb->ccb_msgoutflag != 0)
2480 scsi_low_ccb_message_exec(slp, cb);
2485 cb->ccb_flags |= CCB_INTERNAL;
2486 rv = scsi_low_setup_start(slp, ti, li, cb);
2490 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
2492 if (rv == SCSI_LOW_START_QTAG &&
2493 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
2498 scsi_low_activate_qtag(cb);
2499 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2500 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
2501 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
2502 else if ((cb->ccb_flags & CCB_URGENT) != 0)
2503 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
2505 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
2506 scsi_low_assert_msg(slp, ti, qmsg, 0);
2510 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
2511 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2512 cb->ccb_tc = cb->ccb_tcmax;
2514 /* setup saved scsi data pointer */
2515 cb->ccb_sscp = cb->ccb_scp;
2517 /* setup current scsi pointer */
2518 slp->sl_scp = cb->ccb_sscp;
2519 slp->sl_error = cb->ccb_error;
2521 /* assert always an identify msg */
2522 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
2525 #ifdef SCSI_LOW_DIAGNOSTIC
2526 scsi_low_msg_log_init(&ti->ti_log_msgin);
2527 scsi_low_msg_log_init(&ti->ti_log_msgout);
2528 #endif /* SCSI_LOW_DIAGNOSTIC */
2530 /* selection start */
2532 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
2533 if (rv == SCSI_LOW_START_OK)
2535 #ifdef SCSI_LOW_STATICS
2536 scsi_low_statics.nexus_win ++;
2537 #endif /* SCSI_LOW_STATICS */
2541 scsi_low_arbit_fail(slp, cb);
2542 #ifdef SCSI_LOW_STATICS
2543 scsi_low_statics.nexus_fail ++;
2544 #endif /* SCSI_LOW_STATICS */
2548 scsi_low_arbit_fail(slp, cb)
2549 struct scsi_low_softc *slp;
2552 struct targ_info *ti = cb->ti;
2554 scsi_low_deactivate_qtag(cb);
2555 scsi_low_ccb_message_retry(cb);
2556 cb->ccb_flags |= CCB_STARTQ;
2557 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2559 scsi_low_bus_release(slp, ti);
2562 if (slp->sl_disc == 0)
2564 #ifdef SCSI_LOW_DIAGNOSTIC
2565 printf("%s: try selection again\n", slp->sl_xname);
2566 #endif /* SCSI_LOW_DIAGNOSTIC */
2567 slp->sl_retry_sel = 1;
2572 scsi_low_bus_release(slp, ti)
2573 struct scsi_low_softc *slp;
2574 struct targ_info *ti;
2577 if (ti->ti_disc > 0)
2579 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
2583 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
2586 /* clear all nexus pointer */
2587 slp->sl_Qnexus = NULL;
2588 slp->sl_Lnexus = NULL;
2589 slp->sl_Tnexus = NULL;
2591 /* clear selection assert */
2592 slp->sl_selid = NULL;
2594 /* clear nexus data */
2595 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
2597 /* clear phase change counter */
2598 slp->sl_ph_count = 0;
2602 scsi_low_setup_done(slp, cb)
2603 struct scsi_low_softc *slp;
2606 struct targ_info *ti;
2607 struct lun_info *li;
2612 if (cb->ccb_rcnt >= slp->sl_max_retry)
2614 cb->ccb_error |= ABORTIO;
2615 return SCSI_LOW_DONE_COMPLETE;
2618 /* XXX: special huck for selection timeout */
2619 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
2620 (cb->ccb_error & SELTIMEOUTIO) != 0)
2622 cb->ccb_error |= ABORTIO;
2623 return SCSI_LOW_DONE_COMPLETE;
2626 switch(li->li_state)
2628 case SCSI_LOW_LUN_INQ:
2629 if (cb->ccb_error != 0)
2632 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2636 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2638 else if ((li->li_inq.sd_version & 7) >= 2 ||
2639 (li->li_inq.sd_len >= 4))
2641 if ((li->li_inq.sd_support & 0x2) == 0)
2642 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2643 if ((li->li_inq.sd_support & 0x8) == 0)
2644 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2647 if ((li->li_inq.sd_support & 0x10) == 0)
2648 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2649 if ((li->li_inq.sd_support & 0x20) == 0)
2650 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2651 if ((li->li_inq.sd_support & 0x40) == 0)
2652 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2657 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2660 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2662 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2664 scsi_low_calcf_target(ti);
2665 scsi_low_calcf_lun(li);
2668 case SCSI_LOW_LUN_MODEQ:
2669 if (cb->ccb_error != 0)
2671 if (cb->ccb_error & SENSEIO)
2673 #ifdef SCSI_LOW_DEBUG
2674 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2676 printf("SENSE: [%x][%x][%x][%x][%x]\n",
2677 (u_int) cb->ccb_sense.error_code,
2678 (u_int) cb->ccb_sense.segment,
2679 (u_int) cb->ccb_sense.flags,
2680 (u_int) cb->ccb_sense.add_sense_code,
2681 (u_int) cb->ccb_sense.add_sense_code_qual);
2683 #endif /* SCSI_LOW_DEBUG */
2687 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2690 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2692 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2693 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2695 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2696 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2697 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2699 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2700 scsi_low_calcf_lun(li);
2708 if (li->li_state == SCSI_LOW_LUN_OK)
2710 scsi_low_calcf_target(ti);
2711 scsi_low_calcf_lun(li);
2712 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2713 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2715 scsi_low_calcf_show(li);
2720 return SCSI_LOW_DONE_RETRY;
2724 scsi_low_done(slp, cb)
2725 struct scsi_low_softc *slp;
2730 if (cb->ccb_error == 0)
2732 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2734 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2736 * SCSI-2 draft suggests
2737 * page 0x0a QErr bit determins if
2738 * the target aborts or continues
2739 * the queueing io's after CA state resolved.
2740 * However many targets seem not to support
2741 * the page 0x0a. Thus we should manually clear the
2742 * queuing io's after CA state.
2744 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2747 cb->ccb_flags |= CCB_CLEARQ;
2750 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2752 if ((cb->ccb_flags & CCB_SENSE) != 0)
2753 cb->ccb_error |= (SENSEIO | ABORTIO);
2754 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2756 else switch (cb->ccb_sscp.scp_status)
2762 if (cb->ccb_datalen == 0 ||
2763 cb->ccb_scp.scp_datalen == 0)
2766 if (cb->ccb_scp.scp_cmdlen > 0 &&
2767 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2768 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2771 cb->ccb_error |= PDMAERR;
2776 cb->ccb_error |= (BUSYERR | STATERR);
2780 cb->ccb_error |= (STATERR | ABORTIO);
2785 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2788 cb->ccb_flags |= CCB_SENSE;
2791 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2796 cb->ccb_error |= FATALIO;
2802 if (cb->ccb_flags & CCB_SENSE)
2804 cb->ccb_error |= (SENSEERR | ABORTIO);
2806 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2810 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2812 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2816 /* check a ccb msgout flag */
2817 if (cb->ccb_omsgoutflag != 0)
2819 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2820 SCSI_LOW_MSG_ABORT_QTAG | \
2821 SCSI_LOW_MSG_CLEAR_QTAG | \
2822 SCSI_LOW_MSG_TERMIO)
2824 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2826 cb->ccb_error |= ABORTIO;
2830 /* call OS depend done */
2831 if (cb->osdep != NULL)
2833 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2834 if (rv == EJUSTRETURN)
2837 else if (cb->ccb_error != 0)
2839 if (cb->ccb_rcnt >= slp->sl_max_retry)
2840 cb->ccb_error |= ABORTIO;
2842 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2843 (cb->ccb_error & ABORTIO) == 0)
2847 /* free our target */
2848 #ifdef SCSI_LOW_DEBUG
2849 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2851 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2852 scsi_low_print(slp, NULL);
2854 #endif /* SCSI_LOW_DEBUG */
2856 scsi_low_deactivate_qtag(cb);
2857 scsi_low_dealloc_qtag(cb);
2858 scsi_low_free_ccb(cb);
2860 return SCSI_LOW_DONE_COMPLETE;
2863 #ifdef SCSI_LOW_DEBUG
2864 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2866 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2867 scsi_low_print(slp, NULL);
2869 #endif /* SCSI_LOW_DEBUG */
2872 scsi_low_deactivate_qtag(cb);
2873 scsi_low_ccb_message_retry(cb);
2874 return SCSI_LOW_DONE_RETRY;
2877 /**************************************************************
2879 **************************************************************/
2881 scsi_low_reset_nexus_target(slp, ti, fdone)
2882 struct scsi_low_softc *slp;
2883 struct targ_info *ti;
2886 struct lun_info *li;
2888 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2889 li = LIST_NEXT(li, lun_chain))
2891 scsi_low_reset_nexus_lun(slp, li, fdone);
2892 li->li_state = SCSI_LOW_LUN_SLEEP;
2897 ti->ti_setup_msg = 0;
2898 ti->ti_setup_msg_done = 0;
2900 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2901 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2903 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2904 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2906 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2908 ((*slp->sl_funcs->scsi_low_targ_init)
2909 (slp, ti, SCSI_LOW_INFO_REVOKE));
2911 scsi_low_calcf_target(ti);
2913 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2914 li = LIST_NEXT(li, lun_chain))
2918 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2919 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2921 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2923 ((*slp->sl_funcs->scsi_low_lun_init)
2924 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2926 scsi_low_calcf_lun(li);
2931 scsi_low_reset_nexus(slp, fdone)
2932 struct scsi_low_softc *slp;
2935 struct targ_info *ti;
2936 struct slccb *cb, *topcb;
2938 if ((cb = slp->sl_Qnexus) != NULL)
2940 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2947 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2948 ti = TAILQ_NEXT(ti, ti_chain))
2950 scsi_low_reset_nexus_target(slp, ti, fdone);
2951 scsi_low_bus_release(slp, ti);
2952 scsi_low_init_msgsys(slp, ti);
2957 topcb->ccb_flags |= CCB_STARTQ;
2958 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2962 slp->sl_retry_sel = 0;
2963 slp->sl_flags &= ~HW_PDMASTART;
2968 static char tw_chars[] = "|/-\\";
2969 #define TWIDDLEWAIT 10000
2972 scsi_low_twiddle_wait(void)
2976 cnputc(tw_chars[tw_pos++]);
2977 tw_pos %= (sizeof(tw_chars) - 1);
2978 SCSI_LOW_DELAY(TWIDDLEWAIT);
2982 scsi_low_bus_reset(slp)
2983 struct scsi_low_softc *slp;
2987 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2989 printf("%s: try to reset scsi bus ", slp->sl_xname);
2990 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2991 scsi_low_twiddle_wait();
2997 scsi_low_restart(slp, flags, s)
2998 struct scsi_low_softc *slp;
3005 printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
3007 if ((error = scsi_low_init(slp, flags)) != 0)
3010 scsi_low_start(slp);
3014 /**************************************************************
3015 * disconnect and reselect
3016 **************************************************************/
3017 #define MSGCMD_LUN(msg) (msg & 0x07)
3019 static struct slccb *
3020 scsi_low_establish_ccb(ti, li, tag)
3021 struct targ_info *ti;
3022 struct lun_info *li;
3025 struct scsi_low_softc *slp = ti->ti_sc;
3031 cb = TAILQ_FIRST(&li->li_discq);
3032 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
3033 if (cb->ccb_tag == tag)
3038 * establish our ccb nexus
3041 #ifdef SCSI_LOW_DEBUG
3042 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3044 printf("%s: nexus(0x%lx) abort check start\n",
3045 slp->sl_xname, (u_long) cb);
3046 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
3047 scsi_low_revoke_ccb(slp, cb, 1);
3051 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
3053 if (cb->ccb_omsgoutflag == 0)
3054 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
3056 #endif /* SCSI_LOW_DEBUG */
3058 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3059 cb->ccb_flags &= ~CCB_DISCQ;
3060 slp->sl_Qnexus = cb;
3062 slp->sl_scp = cb->ccb_sscp;
3063 slp->sl_error |= cb->ccb_error;
3069 /* inform "ccb nexus established" to the host driver */
3070 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3073 if (cb->ccb_msgoutflag != 0)
3075 scsi_low_ccb_message_exec(slp, cb);
3082 scsi_low_reselected(slp, targ)
3083 struct scsi_low_softc *slp;
3086 struct targ_info *ti;
3091 * Check select vs reselected collision.
3094 if ((cb = slp->sl_selid) != NULL)
3096 scsi_low_arbit_fail(slp, cb);
3097 #ifdef SCSI_LOW_STATICS
3098 scsi_low_statics.nexus_conflict ++;
3099 #endif /* SCSI_LOW_STATICS */
3103 * Check if no current active nexus.
3105 if (slp->sl_Tnexus != NULL)
3112 * Check a valid target id asserted ?
3114 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
3116 s = "scsi id illegal";
3121 * Check the target scsi status.
3123 ti = slp->sl_ti[targ];
3124 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
3126 s = "phase mismatch";
3134 scsi_low_init_msgsys(slp, ti);
3137 * Establish our target nexus
3139 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
3140 slp->sl_Tnexus = ti;
3141 #ifdef SCSI_LOW_STATICS
3142 scsi_low_statics.nexus_reselected ++;
3143 #endif /* SCSI_LOW_STATICS */
3147 printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
3148 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
3149 "reselect: scsi world confused");
3153 /**************************************************************
3154 * cmd out pointer setup
3155 **************************************************************/
3157 scsi_low_cmd(slp, ti)
3158 struct scsi_low_softc *slp;
3159 struct targ_info *ti;
3161 struct slccb *cb = slp->sl_Qnexus;
3163 slp->sl_ph_count ++;
3169 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
3170 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
3171 slp->sl_scp.scp_datalen = 0;
3172 slp->sl_scp.scp_direction = SCSI_LOW_READ;
3173 slp->sl_error |= FATALIO;
3174 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3175 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
3180 #ifdef SCSI_LOW_DEBUG
3181 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
3183 scsi_low_test_cmdlnk(slp, cb);
3185 #endif /* SCSI_LOW_DEBUG */
3190 /**************************************************************
3191 * data out pointer setup
3192 **************************************************************/
3194 scsi_low_data(slp, ti, bp, direction)
3195 struct scsi_low_softc *slp;
3196 struct targ_info *ti;
3200 struct slccb *cb = slp->sl_Qnexus;
3202 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
3208 slp->sl_error |= (FATALIO | PDMAERR);
3209 slp->sl_scp.scp_datalen = 0;
3210 slp->sl_scp.scp_direction = direction;
3211 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3212 if (ti->ti_ophase != ti->ti_phase)
3217 s = "DATA PHASE: ccb nexus not found";
3219 s = "DATA PHASE: xfer direction mismatch";
3220 SCSI_LOW_INFO(slp, ti, s);
3227 /**************************************************************
3229 **************************************************************/
3230 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
3231 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
3232 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
3233 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
3234 #define MSGIN_DATA_LAST 0x30
3236 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
3237 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
3238 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
3239 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
3241 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
3242 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
3243 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
3244 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
3245 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
3246 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
3247 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
3249 struct scsi_low_msgout_data {
3252 int (*md_msgfunc)(struct scsi_low_softc *);
3253 int (*md_errfunc)(struct scsi_low_softc *, u_int);
3254 #define MSG_RELEASE_ATN 0x0001
3258 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
3259 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
3260 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
3261 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
3262 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
3263 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
3264 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3265 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
3266 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3267 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3268 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3269 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
3270 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3271 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
3272 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
3273 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
3274 /* 15 */{SCSI_LOW_MSG_ALL, 0},
3277 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
3278 static int scsi_low_synch(struct scsi_low_softc *);
3279 static int scsi_low_wide(struct scsi_low_softc *);
3280 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
3281 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
3282 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
3283 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
3284 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
3285 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
3286 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
3287 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
3288 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
3289 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
3290 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
3292 struct scsi_low_msgin_data {
3294 int (*md_msgfunc)(struct scsi_low_softc *);
3297 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
3298 /* 0 */ {1, scsi_low_msginfunc_cc},
3299 /* 1 */ {2, scsi_low_msginfunc_ext},
3300 /* 2 */ {1, scsi_low_msginfunc_sdp},
3301 /* 3 */ {1, scsi_low_msginfunc_rp},
3302 /* 4 */ {1, scsi_low_msginfunc_disc},
3303 /* 5 */ {1, scsi_low_msginfunc_rejop},
3304 /* 6 */ {1, scsi_low_msginfunc_rejop},
3305 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
3306 /* 8 */ {1, scsi_low_msginfunc_noop},
3307 /* 9 */ {1, scsi_low_msginfunc_parity},
3308 /* a */ {1, scsi_low_msginfunc_lcc},
3309 /* b */ {1, scsi_low_msginfunc_lcc},
3310 /* c */ {1, scsi_low_msginfunc_rejop},
3311 /* d */ {2, scsi_low_msginfunc_rejop},
3312 /* e */ {1, scsi_low_msginfunc_rejop},
3313 /* f */ {1, scsi_low_msginfunc_rejop},
3314 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
3315 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
3316 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
3317 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
3318 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
3319 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
3320 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
3321 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
3322 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
3323 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
3324 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
3325 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
3326 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
3327 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
3328 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
3329 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
3330 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
3331 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
3332 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
3333 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
3334 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
3335 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
3336 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
3337 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
3338 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
3339 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
3340 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
3341 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
3342 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
3343 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
3344 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
3345 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
3346 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
3349 /**************************************************************
3351 **************************************************************/
3353 scsi_low_msgfunc_synch(slp)
3354 struct scsi_low_softc *slp;
3356 struct targ_info *ti = slp->sl_Tnexus;
3357 int ptr = ti->ti_msgoutlen;
3359 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
3360 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
3361 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
3362 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
3363 return MSG_EXTEND_SYNCHLEN + 2;
3367 scsi_low_msgfunc_wide(slp)
3368 struct scsi_low_softc *slp;
3370 struct targ_info *ti = slp->sl_Tnexus;
3371 int ptr = ti->ti_msgoutlen;
3373 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
3374 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
3375 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
3376 return MSG_EXTEND_WIDELEN + 2;
3380 scsi_low_msgfunc_identify(slp)
3381 struct scsi_low_softc *slp;
3383 struct targ_info *ti = slp->sl_Tnexus;
3384 struct lun_info *li = slp->sl_Lnexus;
3385 struct slccb *cb = slp->sl_Qnexus;
3386 int ptr = ti->ti_msgoutlen;
3392 slp->sl_error |= FATALIO;
3393 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3394 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
3398 if (scsi_low_is_disconnect_ok(cb) != 0)
3399 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
3403 if (ti->ti_phase == PH_MSGOUT)
3405 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3406 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
3408 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3412 ti->ti_msgoutstr[ptr + 0] = msg;
3417 scsi_low_msgfunc_abort(slp)
3418 struct scsi_low_softc *slp;
3421 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
3426 scsi_low_msgfunc_qabort(slp)
3427 struct scsi_low_softc *slp;
3430 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
3435 scsi_low_msgfunc_reset(slp)
3436 struct scsi_low_softc *slp;
3439 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
3444 scsi_low_msgfunc_qtag(slp)
3445 struct scsi_low_softc *slp;
3447 struct targ_info *ti = slp->sl_Tnexus;
3448 struct slccb *cb = slp->sl_Qnexus;
3449 int ptr = ti->ti_msgoutlen;
3451 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
3453 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
3458 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
3459 if (ti->ti_phase == PH_MSGOUT)
3461 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3468 * The following functions are called when targets give unexpected
3469 * responces in msgin (after msgout).
3472 scsi_low_errfunc_identify(slp, msgflags)
3473 struct scsi_low_softc *slp;
3477 if (slp->sl_Lnexus != NULL)
3479 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
3480 scsi_low_calcf_lun(slp->sl_Lnexus);
3486 scsi_low_errfunc_synch(slp, msgflags)
3487 struct scsi_low_softc *slp;
3490 struct targ_info *ti = slp->sl_Tnexus;
3492 MSGIN_PERIOD(ti) = 0;
3493 MSGIN_OFFSET(ti) = 0;
3494 scsi_low_synch(slp);
3499 scsi_low_errfunc_wide(slp, msgflags)
3500 struct scsi_low_softc *slp;
3503 struct targ_info *ti = slp->sl_Tnexus;
3505 MSGIN_WIDTHP(ti) = 0;
3511 scsi_low_errfunc_qtag(slp, msgflags)
3512 struct scsi_low_softc *slp;
3516 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
3518 if (slp->sl_Qnexus != NULL)
3520 scsi_low_deactivate_qtag(slp->sl_Qnexus);
3522 if (slp->sl_Lnexus != NULL)
3524 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
3525 scsi_low_calcf_lun(slp->sl_Lnexus);
3527 printf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
3534 scsi_low_msgout(slp, ti, fl)
3535 struct scsi_low_softc *slp;
3536 struct targ_info *ti;
3539 struct scsi_low_msgout_data *mdp;
3542 #ifdef SCSI_LOW_DIAGNOSTIC
3543 if (ti != slp->sl_Tnexus)
3545 scsi_low_print(slp, NULL);
3546 panic("scsi_low_msgout: Target nexus inconsistent");
3548 #endif /* SCSI_LOW_DIAGNOSTIC */
3550 slp->sl_ph_count ++;
3551 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3553 printf("%s: too many phase changes\n", slp->sl_xname);
3554 slp->sl_error |= FATALIO;
3555 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3559 * Scsi phase changes.
3560 * Previously msgs asserted are accepted by our target or
3561 * processed by scsi_low_msgin.
3562 * Thus clear all saved informations.
3564 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
3566 ti->ti_omsgflags = 0;
3567 ti->ti_emsgflags = 0;
3569 else if (slp->sl_atten == 0)
3572 * We did not assert attention, however still our target required
3573 * msgs. Resend previous msgs.
3575 ti->ti_msgflags |= ti->ti_omsgflags;
3576 ti->ti_omsgflags = 0;
3577 #ifdef SCSI_LOW_DIAGNOSTIC
3578 printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
3579 #endif /* SCSI_LOW_DIAGNOSTIC */
3583 * We have no msgs. send MSG_NOOP (OK?)
3585 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3586 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
3591 ti->ti_msgoutlen = 0;
3592 slp->sl_clear_atten = 0;
3593 mdp = &scsi_low_msgout_data[0];
3594 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3596 if ((ti->ti_msgflags & mdp->md_flags) != 0)
3598 ti->ti_omsgflags |= mdp->md_flags;
3599 ti->ti_msgflags &= ~mdp->md_flags;
3600 ti->ti_emsgflags = mdp->md_flags;
3602 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
3603 if (mdp->md_msgfunc != NULL)
3604 len = (*mdp->md_msgfunc) (slp);
3608 #ifdef SCSI_LOW_DIAGNOSTIC
3609 scsi_low_msg_log_write(&ti->ti_log_msgout,
3610 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
3611 #endif /* SCSI_LOW_DIAGNOSTIC */
3613 ti->ti_msgoutlen += len;
3614 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
3616 slp->sl_clear_atten = 1;
3620 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
3621 ti->ti_msgflags == 0)
3624 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3629 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3630 slp->sl_clear_atten = 1;
3632 return ti->ti_msgoutlen;
3635 /**************************************************************
3637 **************************************************************/
3639 scsi_low_msginfunc_noop(slp)
3640 struct scsi_low_softc *slp;
3647 scsi_low_msginfunc_rejop(slp)
3648 struct scsi_low_softc *slp;
3650 struct targ_info *ti = slp->sl_Tnexus;
3651 u_int8_t msg = ti->ti_msgin[0];
3653 printf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
3654 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3659 scsi_low_msginfunc_cc(slp)
3660 struct scsi_low_softc *slp;
3662 struct lun_info *li;
3664 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3666 /* validate status */
3667 if (slp->sl_Qnexus == NULL)
3670 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3671 li = slp->sl_Lnexus;
3672 switch (slp->sl_scp.scp_status)
3675 li->li_maxnqio = li->li_maxnexus;
3680 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3681 scsi_low_reset_nexus_lun(slp, li, 0);
3689 if (li->li_maxnexus >= li->li_nqio)
3690 li->li_maxnexus = li->li_nqio - 1;
3691 li->li_maxnqio = li->li_maxnexus;
3696 slp->sl_error |= MSGERR;
3706 scsi_low_msginfunc_lcc(slp)
3707 struct scsi_low_softc *slp;
3709 struct targ_info *ti;
3710 struct lun_info *li;
3711 struct slccb *ncb, *cb;
3713 ti = slp->sl_Tnexus;
3714 li = slp->sl_Lnexus;
3715 if ((cb = slp->sl_Qnexus) == NULL)
3718 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3719 switch (slp->sl_scp.scp_status)
3723 li->li_maxnqio = li->li_maxnexus;
3727 slp->sl_error |= MSGERR;
3731 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3734 cb->ccb_error |= slp->sl_error;
3735 if (cb->ccb_error != 0)
3738 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3739 ncb = TAILQ_NEXT(ncb, ccb_chain))
3742 goto cmd_link_start;
3747 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3748 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3752 ncb->ccb_flags &= ~CCB_STARTQ;
3753 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3755 scsi_low_dealloc_qtag(ncb);
3756 ncb->ccb_tag = cb->ccb_tag;
3757 ncb->ccb_otag = cb->ccb_otag;
3758 cb->ccb_tag = SCSI_LOW_UNKTAG;
3759 cb->ccb_otag = SCSI_LOW_UNKTAG;
3760 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3761 panic("%s: linked ccb retried", slp->sl_xname);
3763 slp->sl_Qnexus = ncb;
3764 slp->sl_ph_count = 0;
3767 ncb->ccb_datalen = -1;
3768 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3769 ncb->ccb_flags &= ~CCB_INTERNAL;
3771 scsi_low_init_msgsys(slp, ti);
3773 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3775 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3776 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3777 ncb->ccb_tc = ncb->ccb_tcmax;
3779 /* setup saved scsi data pointer */
3780 ncb->ccb_sscp = ncb->ccb_scp;
3781 slp->sl_scp = ncb->ccb_sscp;
3782 slp->sl_error = ncb->ccb_error;
3784 #ifdef SCSI_LOW_DIAGNOSTIC
3785 scsi_low_msg_log_init(&ti->ti_log_msgin);
3786 scsi_low_msg_log_init(&ti->ti_log_msgout);
3787 #endif /* SCSI_LOW_DIAGNOSTIC */
3792 scsi_low_msginfunc_disc(slp)
3793 struct scsi_low_softc *slp;
3796 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3801 scsi_low_msginfunc_sdp(slp)
3802 struct scsi_low_softc *slp;
3804 struct slccb *cb = slp->sl_Qnexus;
3808 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3809 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3812 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3817 scsi_low_msginfunc_rp(slp)
3818 struct scsi_low_softc *slp;
3821 if (slp->sl_Qnexus != NULL)
3822 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3824 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3830 struct scsi_low_softc *slp;
3832 struct targ_info *ti = slp->sl_Tnexus;
3833 u_int period = 0, offset = 0, speed;
3837 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3838 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3839 MSGIN_OFFSET(ti) == 0)
3841 if ((offset = MSGIN_OFFSET(ti)) != 0)
3842 period = MSGIN_PERIOD(ti);
3843 s = offset ? "synchronous" : "async";
3848 * Target seems to be brain damaged.
3849 * Force async transfer.
3851 ti->ti_maxsynch.period = 0;
3852 ti->ti_maxsynch.offset = 0;
3853 printf("%s: target brain damaged. async transfer\n",
3858 ti->ti_maxsynch.period = period;
3859 ti->ti_maxsynch.offset = offset;
3861 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3865 * Current period and offset are not acceptable
3867 * The adapter changes max synch and max offset.
3869 printf("%s: synch neg failed. retry synch msg neg ...\n",
3874 ti->ti_osynch = ti->ti_maxsynch;
3877 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3881 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3883 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3884 struct slccb *cb = slp->sl_Qnexus;
3886 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3888 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3890 printf("%s(%d:*): <%s> offset %d period %dns ",
3891 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3895 speed = 1000 * 10 / (period * 4);
3896 printf("%d.%d M/s", speed / 10, speed % 10);
3905 struct scsi_low_softc *slp;
3907 struct targ_info *ti = slp->sl_Tnexus;
3910 ti->ti_width = MSGIN_WIDTHP(ti);
3911 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3915 * Current width is not acceptable for our adapter.
3916 * The adapter changes max width.
3918 printf("%s: wide neg failed. retry wide msg neg ...\n",
3923 ti->ti_owidth = ti->ti_width;
3924 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3926 ti->ti_setup_msg_done |=
3927 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3931 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3933 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3934 struct slccb *cb = slp->sl_Qnexus;
3936 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3938 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3940 printf("%s(%d:*): transfer width %d bits\n",
3941 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3947 scsi_low_msginfunc_simple_qtag(slp)
3948 struct scsi_low_softc *slp;
3950 struct targ_info *ti = slp->sl_Tnexus;
3951 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3953 if (slp->sl_Qnexus != NULL)
3955 if (slp->sl_Qnexus->ccb_tag != etag)
3957 slp->sl_error |= FATALIO;
3958 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3959 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3962 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3964 #ifdef SCSI_LOW_DEBUG
3965 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3967 #endif /* SCSI_LOW_DEBUG */
3969 slp->sl_error |= FATALIO;
3970 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3971 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3977 scsi_low_msginfunc_i_wide_residue(slp)
3978 struct scsi_low_softc *slp;
3980 struct targ_info *ti = slp->sl_Tnexus;
3981 struct slccb *cb = slp->sl_Qnexus;
3982 int res = (int) ti->ti_msgin[1];
3984 if (cb == NULL || res <= 0 ||
3985 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3986 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3989 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3992 slp->sl_scp.scp_datalen += res;
3993 slp->sl_scp.scp_data -= res;
3994 scsi_low_data_finish(slp);
3999 scsi_low_msginfunc_ext(slp)
4000 struct scsi_low_softc *slp;
4002 struct slccb *cb = slp->sl_Qnexus;
4003 struct lun_info *li = slp->sl_Lnexus;
4004 struct targ_info *ti = slp->sl_Tnexus;
4008 if (ti->ti_msginptr == 2)
4010 ti->ti_msginlen = ti->ti_msgin[1] + 2;
4014 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
4016 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
4020 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
4021 count = (int) htonl((long) (*ptr));
4022 if(slp->sl_scp.scp_datalen - count < 0 ||
4023 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
4026 slp->sl_scp.scp_datalen -= count;
4027 slp->sl_scp.scp_data += count;
4030 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
4034 retry = scsi_low_synch(slp);
4035 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
4036 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
4038 #ifdef SCSI_LOW_DEBUG
4039 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4041 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
4043 #endif /* SCSI_LOW_DEBUG */
4046 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
4050 retry = scsi_low_wide(slp);
4051 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
4052 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
4060 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4065 scsi_low_msginfunc_parity(slp)
4066 struct scsi_low_softc *slp;
4068 struct targ_info *ti = slp->sl_Tnexus;
4070 /* only I -> T, invalid! */
4071 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4076 scsi_low_msginfunc_msg_reject(slp)
4077 struct scsi_low_softc *slp;
4079 struct targ_info *ti = slp->sl_Tnexus;
4080 struct scsi_low_msgout_data *mdp;
4083 if (ti->ti_emsgflags != 0)
4085 printf("%s: msg flags [0x%x] rejected\n",
4086 slp->sl_xname, ti->ti_emsgflags);
4087 msgflags = SCSI_LOW_MSG_REJECT;
4088 mdp = &scsi_low_msgout_data[0];
4089 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
4091 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
4093 ti->ti_emsgflags &= ~mdp->md_flags;
4094 if (mdp->md_errfunc != NULL)
4095 (*mdp->md_errfunc) (slp, msgflags);
4103 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
4104 slp->sl_error |= MSGERR;
4110 scsi_low_msgin(slp, ti, c)
4111 struct scsi_low_softc *slp;
4112 struct targ_info *ti;
4115 struct scsi_low_msgin_data *sdp;
4116 struct lun_info *li;
4119 #ifdef SCSI_LOW_DIAGNOSTIC
4120 if (ti != slp->sl_Tnexus)
4122 scsi_low_print(slp, NULL);
4123 panic("scsi_low_msgin: Target nexus inconsistent");
4125 #endif /* SCSI_LOW_DIAGNOSTIC */
4128 * Phase changes, clear the pointer.
4130 if (ti->ti_ophase != ti->ti_phase)
4133 ti->ti_msgin_parity_error = 0;
4135 slp->sl_ph_count ++;
4136 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
4138 printf("%s: too many phase changes\n", slp->sl_xname);
4139 slp->sl_error |= FATALIO;
4140 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4145 * Store a current messages byte into buffer and
4146 * wait for the completion of the current msg.
4148 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
4149 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
4151 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
4152 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4156 * Check parity errors.
4158 if ((c & SCSI_LOW_DATA_PE) != 0)
4160 ti->ti_msgin_parity_error ++;
4161 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
4165 if (ti->ti_msgin_parity_error != 0)
4169 * Calculate messages length.
4171 msg = ti->ti_msgin[0];
4172 if (msg < MSGIN_DATA_LAST)
4173 sdp = &scsi_low_msgin_data[msg];
4175 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
4177 if (ti->ti_msginlen == 0)
4179 ti->ti_msginlen = sdp->md_len;
4185 if (ti->ti_msginptr < ti->ti_msginlen)
4191 if ((msg & MSG_IDENTIFY) == 0)
4193 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
4198 li = slp->sl_Lnexus;
4201 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
4204 slp->sl_Lnexus = li;
4205 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
4209 if (MSGCMD_LUN(msg) != li->li_lun)
4213 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
4215 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
4217 #ifdef SCSI_LOW_DEBUG
4218 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
4222 #endif /* SCSI_LOW_DEBUG */
4230 * Msg process completed, reset msgin pointer and assert ATN if desired.
4233 slp->sl_error |= FATALIO;
4234 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4235 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
4238 if (ti->ti_msginptr < ti->ti_msginlen)
4241 #ifdef SCSI_LOW_DIAGNOSTIC
4242 scsi_low_msg_log_write(&ti->ti_log_msgin,
4243 &ti->ti_msgin[0], ti->ti_msginlen);
4244 #endif /* SCSI_LOW_DIAGNOSTIC */
4250 /**********************************************************
4252 **********************************************************/
4254 scsi_low_disconnected(slp, ti)
4255 struct scsi_low_softc *slp;
4256 struct targ_info *ti;
4258 struct slccb *cb = slp->sl_Qnexus;
4260 /* check phase completion */
4261 switch (slp->sl_msgphase)
4264 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4265 scsi_low_msginfunc_cc(slp);
4266 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
4270 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4271 scsi_low_msginfunc_cc(slp);
4272 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
4276 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4277 scsi_low_msginfunc_cc(slp);
4283 struct lun_info *li;
4286 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
4287 cb->ccb_flags |= CCB_DISCQ;
4288 cb->ccb_error |= slp->sl_error;
4294 #ifdef SCSI_LOW_STATICS
4295 scsi_low_statics.nexus_disconnected ++;
4296 #endif /* SCSI_LOW_STATICS */
4298 #ifdef SCSI_LOW_DEBUG
4299 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
4301 printf("## SCSI_LOW_DISCONNECTED ===============\n");
4302 scsi_low_print(slp, NULL);
4304 #endif /* SCSI_LOW_DEBUG */
4308 slp->sl_error |= FATALIO;
4309 if (ti->ti_phase == PH_SELSTART)
4310 slp->sl_error |= SELTIMEOUTIO;
4312 slp->sl_error |= UBFERR;
4321 #ifdef SCSI_LOW_DEBUG
4322 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4324 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
4325 (cb->ccb_msgoutflag != 0 ||
4326 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
4328 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
4331 #endif /* SCSI_LOW_DEBUG */
4333 cb->ccb_error |= slp->sl_error;
4334 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
4336 cb->ccb_flags |= CCB_STARTQ;
4337 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
4342 scsi_low_bus_release(slp, ti);
4343 scsi_low_start(slp);
4347 /**********************************************************
4349 **********************************************************/
4351 scsi_low_alloc_qtag(cb)
4354 struct lun_info *li = cb->li;
4355 scsi_low_tag_t etag;
4357 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
4360 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4361 etag = ffs(li->li_qtagbits);
4365 li->li_qtagbits &= ~(1 << (etag - 1));
4366 cb->ccb_otag = etag;
4369 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4370 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
4371 if (li->li_qtagarray[li->li_qd] == 0)
4374 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
4375 if (li->li_qtagarray[li->li_qd] == 0)
4381 li->li_qtagarray[li->li_qd] ++;
4382 cb->ccb_otag = (li->li_qd ++);
4384 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4388 scsi_low_dealloc_qtag(cb)
4391 struct lun_info *li = cb->li;
4392 scsi_low_tag_t etag;
4394 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
4397 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4398 etag = cb->ccb_otag - 1;
4399 #ifdef SCSI_LOW_DIAGNOSTIC
4400 if (etag >= sizeof(li->li_qtagbits) * NBBY)
4401 panic("scsi_low_dealloc_tag: illegal tag");
4402 #endif /* SCSI_LOW_DIAGNOSTIC */
4403 li->li_qtagbits |= (1 << etag);
4405 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4406 etag = cb->ccb_otag;
4407 #ifdef SCSI_LOW_DIAGNOSTIC
4408 if (etag >= SCSI_LOW_MAXNEXUS)
4409 panic("scsi_low_dealloc_tag: illegal tag");
4410 #endif /* SCSI_LOW_DIAGNOSTIC */
4411 li->li_qtagarray[etag] --;
4412 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4414 cb->ccb_otag = SCSI_LOW_UNKTAG;
4418 static struct slccb *
4419 scsi_low_revoke_ccb(slp, cb, fdone)
4420 struct scsi_low_softc *slp;
4424 struct targ_info *ti = cb->ti;
4425 struct lun_info *li = cb->li;
4427 #ifdef SCSI_LOW_DIAGNOSTIC
4428 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
4429 (CCB_STARTQ | CCB_DISCQ))
4431 panic("%s: ccb in both queue", slp->sl_xname);
4433 #endif /* SCSI_LOW_DIAGNOSTIC */
4435 if ((cb->ccb_flags & CCB_STARTQ) != 0)
4437 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
4440 if ((cb->ccb_flags & CCB_DISCQ) != 0)
4442 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
4448 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
4449 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
4452 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
4453 (cb->ccb_flags & CCB_NORETRY) != 0))
4455 cb->ccb_error |= FATALIO;
4456 cb->ccb_flags &= ~CCB_AUTOSENSE;
4457 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
4458 panic("%s: done ccb retried", slp->sl_xname);
4463 cb->ccb_error |= PENDINGIO;
4464 scsi_low_deactivate_qtag(cb);
4465 scsi_low_ccb_message_retry(cb);
4466 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
4472 scsi_low_reset_nexus_lun(slp, li, fdone)
4473 struct scsi_low_softc *slp;
4474 struct lun_info *li;
4477 struct slccb *cb, *ncb, *ecb;
4483 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
4485 ncb = TAILQ_NEXT(cb, ccb_chain);
4486 cb = scsi_low_revoke_ccb(slp, cb, fdone);
4490 * presumely keep ordering of io
4492 cb->ccb_flags |= CCB_STARTQ;
4495 TAILQ_INSERT_HEAD(&slp->sl_start,\
4500 TAILQ_INSERT_AFTER(&slp->sl_start,\
4501 ecb, cb, ccb_chain);
4508 /**************************************************************
4510 **************************************************************/
4512 scsi_low_calcf_lun(li)
4513 struct lun_info *li;
4515 struct targ_info *ti = li->li_ti;
4516 struct scsi_low_softc *slp = ti->ti_sc;
4517 u_int cfgflags, diskflags;
4519 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
4520 cfgflags = li->li_cfgflags;
4524 diskflags = li->li_diskflags & li->li_quirks;
4527 li->li_flags &= ~SCSI_LOW_DISC;
4528 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
4529 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
4530 (cfgflags & SCSI_LOW_DISC) != 0)
4531 li->li_flags |= SCSI_LOW_DISC;
4534 li->li_flags |= SCSI_LOW_NOPARITY;
4535 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
4536 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
4537 (cfgflags & SCSI_LOW_NOPARITY) == 0)
4538 li->li_flags &= ~SCSI_LOW_NOPARITY;
4541 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
4542 (cfgflags & SCSI_LOW_QTAG) != 0 &&
4543 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
4545 li->li_flags |= SCSI_LOW_QTAG;
4546 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
4547 li->li_maxnqio = li->li_maxnexus;
4551 li->li_flags &= ~SCSI_LOW_QTAG;
4552 li->li_maxnexus = 0;
4553 li->li_maxnqio = li->li_maxnexus;
4557 li->li_flags &= ~SCSI_LOW_LINK;
4558 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
4559 (diskflags & SCSI_LOW_DISK_LINK) != 0)
4560 li->li_flags |= SCSI_LOW_LINK;
4562 /* compatible flags */
4563 li->li_flags &= ~SCSI_LOW_SYNC;
4564 if (ti->ti_maxsynch.offset > 0)
4565 li->li_flags |= SCSI_LOW_SYNC;
4567 #ifdef SCSI_LOW_DEBUG
4568 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4570 scsi_low_calcf_show(li);
4572 #endif /* SCSI_LOW_DEBUG */
4576 scsi_low_calcf_target(ti)
4577 struct targ_info *ti;
4579 struct scsi_low_softc *slp = ti->ti_sc;
4580 u_int offset, period, diskflags;
4582 diskflags = ti->ti_diskflags & ti->ti_quirks;
4585 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
4586 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
4588 offset = ti->ti_maxsynch.offset;
4589 period = ti->ti_maxsynch.period;
4590 if (offset == 0 || period == 0)
4591 offset = period = 0;
4595 offset = period = 0;
4598 ti->ti_maxsynch.offset = offset;
4599 ti->ti_maxsynch.period = period;
4602 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
4603 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
4604 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
4606 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
4607 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
4608 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
4610 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
4612 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
4613 ti->ti_maxsynch.period != ti->ti_osynch.period)
4614 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
4615 if (ti->ti_width != ti->ti_owidth)
4616 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
4618 ti->ti_osynch = ti->ti_maxsynch;
4619 ti->ti_owidth = ti->ti_width;
4622 #ifdef SCSI_LOW_DEBUG
4623 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4625 printf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
4626 slp->sl_xname, ti->ti_id,
4627 ti->ti_maxsynch.period * 4,
4628 ti->ti_maxsynch.offset,
4631 #endif /* SCSI_LOW_DEBUG */
4635 scsi_low_calcf_show(li)
4636 struct lun_info *li;
4638 struct targ_info *ti = li->li_ti;
4639 struct scsi_low_softc *slp = ti->ti_sc;
4641 printf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4642 slp->sl_xname, ti->ti_id, li->li_lun,
4643 ti->ti_maxsynch.period * 4,
4644 ti->ti_maxsynch.offset,
4646 li->li_flags, SCSI_LOW_BITS);
4649 #ifdef SCSI_LOW_START_UP_CHECK
4650 /**************************************************************
4651 * scsi world start up
4652 **************************************************************/
4653 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
4656 scsi_low_start_up(slp)
4657 struct scsi_low_softc *slp;
4659 struct targ_info *ti;
4660 struct lun_info *li;
4664 printf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
4666 for (target = 0; target < slp->sl_ntargs; target ++)
4668 if (target == slp->sl_hostid)
4670 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4672 printf("%s: scsi_low: target %d (host card)\n",
4673 slp->sl_xname, target);
4678 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4680 printf("%s: scsi_low: target %d lun ",
4681 slp->sl_xname, target);
4684 ti = slp->sl_ti[target];
4685 for (lun = 0; lun < slp->sl_nluns; lun ++)
4687 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4693 li = scsi_low_alloc_li(ti, lun, 1);
4695 scsi_low_enqueue(slp, ti, li, cb,
4696 CCB_AUTOSENSE | CCB_POLLED, 0);
4698 scsi_low_poll(slp, cb);
4700 if (li->li_state != SCSI_LOW_LUN_OK)
4703 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4709 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4718 scsi_low_poll(slp, cb)
4719 struct scsi_low_softc *slp;
4725 while (slp->sl_nio > 0)
4727 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4729 (*slp->sl_funcs->scsi_low_poll) (slp);
4730 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4734 scsi_low_timeout_check(slp);
4739 #endif /* SCSI_LOW_START_UP_CHECK */
4741 /**********************************************************
4743 **********************************************************/
4744 #ifdef SCSI_LOW_DEBUG
4746 scsi_low_test_abort(slp, ti, li)
4747 struct scsi_low_softc *slp;
4748 struct targ_info *ti;
4749 struct lun_info *li;
4753 if (li->li_disc > 1)
4755 acb = TAILQ_FIRST(&li->li_discq);
4756 if (scsi_low_abort_ccb(slp, acb) == 0)
4758 printf("%s: aborting ccb(0x%lx) start\n",
4759 slp->sl_xname, (u_long) acb);
4765 scsi_low_test_atten(slp, ti, msg)
4766 struct scsi_low_softc *slp;
4767 struct targ_info *ti;
4771 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4772 scsi_low_assert_msg(slp, ti, msg, 0);
4774 printf("%s: atten check OK\n", slp->sl_xname);
4778 scsi_low_test_cmdlnk(slp, cb)
4779 struct scsi_low_softc *slp;
4782 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4784 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4787 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4788 slp->sl_scp.scp_cmdlen);
4789 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4790 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4792 #endif /* SCSI_LOW_DEBUG */
4795 scsi_low_info(slp, ti, s)
4796 struct scsi_low_softc *slp;
4797 struct targ_info *ti;
4802 slp = LIST_FIRST(&sl_tab);
4806 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4809 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4810 ti = TAILQ_NEXT(ti, ti_chain))
4812 scsi_low_print(slp, ti);
4817 scsi_low_print(slp, ti);
4821 static u_char *phase[] =
4823 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4824 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4828 scsi_low_print(slp, ti)
4829 struct scsi_low_softc *slp;
4830 struct targ_info *ti;
4832 struct lun_info *li;
4836 if (ti == NULL || ti == slp->sl_Tnexus)
4838 ti = slp->sl_Tnexus;
4839 li = slp->sl_Lnexus;
4840 cb = slp->sl_Qnexus;
4844 li = LIST_FIRST(&ti->ti_litab);
4845 cb = TAILQ_FIRST(&li->li_discq);
4849 printf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4850 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4856 u_int flags = 0, maxnqio = 0, nqio = 0;
4862 flags = li->li_flags;
4863 maxnqio = li->li_maxnqio;
4867 printf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4869 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4870 phase[(int) ti->ti_phase], ti->ti_disc,
4875 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4876 (u_int) cb->ccb_scp.scp_cmd[0],
4877 cb->ccb_scp.scp_cmdlen,
4879 cb->ccb_scp.scp_datalen,
4880 (u_int) cb->ccb_sscp.scp_status,
4881 cb->ccb_error, SCSI_LOW_ERRORBITS);
4884 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4885 (u_int) (ti->ti_msginptr),
4886 (u_int) (ti->ti_msgin[0]),
4887 (u_int) (ti->ti_msgin[1]),
4888 (u_int) (ti->ti_msgin[2]),
4889 (u_int) (ti->ti_msgin[3]),
4890 (u_int) (ti->ti_msgin[4]),
4893 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4894 (u_int) ti->ti_msgflags,
4895 (u_int) (ti->ti_msgoutstr[0]),
4896 (u_int) (ti->ti_msgoutstr[1]),
4897 (u_int) (ti->ti_msgoutstr[2]),
4898 (u_int) (ti->ti_msgoutstr[3]),
4899 (u_int) (ti->ti_msgoutstr[4]),
4901 flags, SCSI_LOW_BITS);
4903 #ifdef SCSI_LOW_DIAGNOSTIC
4904 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4905 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4906 #endif /* SCSI_LOW_DIAGNOSTIC */
4910 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4911 (u_long) sp->scp_data,
4913 (u_int) sp->scp_status,
4914 slp->sl_error, SCSI_LOW_ERRORBITS);