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 static 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));
646 SCSI_LOW_BZERO(sap, sizeof(*sap));
647 SCSI_LOW_BZERO(splp, sizeof(*splp));
649 sap->scsipi_cmd = scsi_low_scsi_cmd_xs;
650 sap->scsipi_minphys = scsi_low_scsi_minphys_xs;
651 sap->scsipi_enable = scsi_low_enable_xs;
652 sap->scsipi_ioctl = scsi_low_ioctl_xs;
653 #ifdef SCSI_LOW_TARGET_OPEN
654 sap->open_target_lu = scsi_low_target_open;
655 #endif /* SCSI_LOW_TARGET_OPEN */
657 splp->adapter_softc = slp;
658 splp->scsipi_scsi.adapter_target = slp->sl_hostid;
659 splp->scsipi_scsi.max_target = slp->sl_ntargs - 1;
660 splp->scsipi_scsi.max_lun = slp->sl_nluns - 1;
661 splp->scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
662 splp->openings = slp->sl_openings;
663 splp->type = BUS_SCSI;
664 splp->adapter_softc = slp;
666 splp->device = &scsi_low_dev;
668 slp->sl_si.si_splp = splp;
669 slp->sl_show_result = SHOW_ALL_NEG;
674 scsi_low_world_start_xs(slp)
675 struct scsi_low_softc *slp;
682 scsi_low_dettach_xs(slp)
683 struct scsi_low_softc *slp;
687 * scsipi does not have dettach bus fucntion.
689 scsipi_dettach_scsibus(slp->sl_si.si_splp);
695 scsi_low_ccb_setup_xs(slp, cb)
696 struct scsi_low_softc *slp;
699 struct scsipi_xfer *xs = (struct scsipi_xfer *) cb->osdep;
701 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
703 cb->ccb_scp.scp_cmd = (u_int8_t *) xs->cmd;
704 cb->ccb_scp.scp_cmdlen = xs->cmdlen;
705 cb->ccb_scp.scp_data = xs->data;
706 cb->ccb_scp.scp_datalen = xs->datalen;
707 cb->ccb_scp.scp_direction = (xs->flags & SCSI_DATA_OUT) ?
708 SCSI_LOW_WRITE : SCSI_LOW_READ;
709 cb->ccb_tcmax = xs->timeout / 1000;
713 scsi_low_unit_ready_cmd(cb);
715 return SCSI_LOW_START_QTAG;
719 scsi_low_done_xs(slp, cb)
720 struct scsi_low_softc *slp;
723 struct scsipi_xfer *xs;
725 xs = (struct scsipi_xfer *) cb->osdep;
726 if (cb->ccb_error == 0)
728 xs->error = XS_NOERROR;
733 if (cb->ccb_rcnt >= slp->sl_max_retry)
734 cb->ccb_error |= ABORTIO;
736 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
737 (cb->ccb_error & ABORTIO) == 0)
740 if ((cb->ccb_error & SENSEIO) != 0)
742 xs->sense.scsi_sense = cb->ccb_sense;
745 xs->error = scsi_low_translate_error_code(cb,
746 &scsi_low_error_code_xs[0]);
748 #ifdef SCSI_LOW_DIAGNOSTIC
749 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
750 cb->ccb_scp.scp_cmdlen > 0 &&
751 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
752 SCSI_LOW_CMD_ABORT_WARNING) != 0)
754 printf("%s: WARNING: scsi_low IO abort\n",
756 scsi_low_print(slp, NULL);
758 #endif /* SCSI_LOW_DIAGNOSTIC */
761 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
762 xs->status = 0; /* XXX */
764 xs->status = cb->ccb_scp.scp_status;
766 xs->flags |= ITSDONE;
767 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
774 scsi_low_timeout_xs(slp, ch, action)
775 struct scsi_low_softc *slp;
782 case SCSI_LOW_TIMEOUT_CH_IO:
785 case SCSI_LOW_TIMEOUT_START:
786 timeout(scsi_low_timeout, slp,
787 hz / SCSI_LOW_TIMEOUT_HZ);
789 case SCSI_LOW_TIMEOUT_STOP:
790 untimeout(scsi_low_timeout, slp);
795 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
798 case SCSI_LOW_TIMEOUT_START:
799 timeout(scsi_low_engage, slp, 1);
801 case SCSI_LOW_TIMEOUT_STOP:
802 untimeout(scsi_low_engage, slp);
807 case SCSI_LOW_TIMEOUT_CH_RECOVER:
813 scsi_low_translate_quirks_xs(quirks)
818 flags = SCSI_LOW_DISK_LFLAGS | SCSI_LOW_DISK_TFLAGS;
821 if (quirks & SDEV_NODISC)
822 flags &= ~SCSI_LOW_DISK_DISC;
823 #endif /* SDEV_NODISC */
825 if (quirks & SDEV_NOPARITY)
826 flags &= ~SCSI_LOW_DISK_PARITY;
827 #endif /* SDEV_NOPARITY */
829 if (quirks & SDEV_NOCMDLNK)
830 flags &= ~SCSI_LOW_DISK_LINK;
831 #endif /* SDEV_NOCMDLNK */
833 if (quirks & SDEV_NOTAG)
834 flags &= ~SCSI_LOW_DISK_QTAG;
835 #endif /* SDEV_NOTAG */
837 if (quirks & SDEV_NOSYNC)
838 flags &= ~SCSI_LOW_DISK_SYNC;
839 #endif /* SDEV_NOSYNC */
845 scsi_low_setup_quirks_xs(ti, li, flags)
846 struct targ_info *ti;
852 li->li_sloi.sloi_quirks = flags;
853 quirks = scsi_low_translate_quirks_xs(flags);
854 ti->ti_quirks = quirks & SCSI_LOW_DISK_TFLAGS;
855 li->li_quirks = quirks & SCSI_LOW_DISK_LFLAGS;
856 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
857 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
858 scsi_low_calcf_target(ti);
859 scsi_low_calcf_lun(li);
860 scsi_low_calcf_show(li);
863 #ifdef SCSI_LOW_TARGET_OPEN
865 scsi_low_target_open(link, cf)
866 struct scsipi_link *link;
869 u_int target = link->scsipi_scsi.target;
870 u_int lun = link->scsipi_scsi.lun;
871 struct scsi_low_softc *slp;
872 struct targ_info *ti;
875 slp = (struct scsi_low_softc *) link->adapter_softc;
876 ti = slp->sl_ti[target];
877 li = scsi_low_alloc_li(ti, lun, 0);
881 li->li_cfgflags = cf->cf_flags;
882 scsi_low_setup_quirks_xs(ti, li, (u_int) link->quirks);
885 #endif /* SCSI_LOW_TARGET_OPEN */
887 #endif /* SCSI_LOW_INTERFACE_XS */
889 #ifdef SCSI_LOW_INTERFACE_CAM
890 /**************************************************************
891 * SCSI INTERFACE (CAM)
892 **************************************************************/
893 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
894 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
895 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
897 static void scsi_low_poll_cam(struct cam_sim *);
898 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
900 static int scsi_low_attach_cam(struct scsi_low_softc *);
901 static int scsi_low_world_start_cam(struct scsi_low_softc *);
902 static int scsi_low_dettach_cam(struct scsi_low_softc *);
903 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
904 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
905 static void scsi_low_timeout_cam(struct scsi_low_softc *, int, int);
907 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
909 scsi_low_world_start_cam,
910 scsi_low_dettach_cam,
911 scsi_low_ccb_setup_cam,
916 struct scsi_low_error_code scsi_low_error_code_cam[] = {
918 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
919 {SENSEERR, CAM_AUTOSENSE_FAIL},
920 {UACAERR, CAM_SCSI_STATUS_ERROR},
921 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
922 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
923 {TIMEOUTIO, CAM_CMD_TIMEOUT},
924 {PDMAERR, CAM_DATA_RUN_ERR},
925 {PARITYERR, CAM_UNCOR_PARITY},
926 {UBFERR, CAM_UNEXP_BUSFREE},
927 {ABORTIO, CAM_REQ_ABORTED},
928 {-1, CAM_UNREC_HBA_ERROR}
931 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
934 * Please check a polling hz, currently we assume scsi_low_poll() is
937 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
940 scsi_low_poll_cam(sim)
943 struct scsi_low_softc *slp = SIM2SLP(sim);
945 (*slp->sl_funcs->scsi_low_poll) (slp);
947 if (slp->sl_si.si_poll_count ++ >=
948 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
950 slp->sl_si.si_poll_count = 0;
951 scsi_low_timeout_check(slp);
956 scsi_low_scsi_action_cam(sim, ccb)
960 struct scsi_low_softc *slp = SIM2SLP(sim);
961 struct targ_info *ti;
964 u_int lun, flags, msg, target;
967 target = (u_int) (ccb->ccb_h.target_id);
968 lun = (u_int) ccb->ccb_h.target_lun;
970 #ifdef SCSI_LOW_DEBUG
971 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
973 printf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
974 slp->sl_xname, ccb->ccb_h.func_code, target, lun);
976 #endif /* SCSI_LOW_DEBUG */
978 switch (ccb->ccb_h.func_code) {
979 case XPT_SCSI_IO: /* Execute the requested I/O operation */
980 #ifdef SCSI_LOW_DIAGNOSTIC
981 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
983 printf("%s: invalid target/lun\n", slp->sl_xname);
984 ccb->ccb_h.status = CAM_REQ_INVALID;
988 #endif /* SCSI_LOW_DIAGNOSTIC */
990 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
991 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
996 ti = slp->sl_ti[target];
999 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1000 flags = CCB_AUTOSENSE | CCB_SCSIIO;
1004 s = SCSI_LOW_SPLSCSI();
1005 li = scsi_low_alloc_li(ti, lun, 1);
1007 if (ti->ti_setup_msg != 0)
1009 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
1012 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
1014 #ifdef SCSI_LOW_DEBUG
1015 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
1017 scsi_low_test_abort(slp, ti, li);
1019 #endif /* SCSI_LOW_DEBUG */
1023 case XPT_EN_LUN: /* Enable LUN as a target */
1024 case XPT_TARGET_IO: /* Execute target I/O request */
1025 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1026 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1028 ccb->ccb_h.status = CAM_REQ_INVALID;
1032 case XPT_ABORT: /* Abort the specified CCB */
1033 #ifdef SCSI_LOW_DIAGNOSTIC
1034 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
1036 printf("%s: invalid target/lun\n", slp->sl_xname);
1037 ccb->ccb_h.status = CAM_REQ_INVALID;
1041 #endif /* SCSI_LOW_DIAGNOSTIC */
1043 s = SCSI_LOW_SPLSCSI();
1044 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
1045 rv = scsi_low_abort_ccb(slp, cb);
1049 ccb->ccb_h.status = CAM_REQ_CMP;
1051 ccb->ccb_h.status = CAM_REQ_INVALID;
1055 case XPT_SET_TRAN_SETTINGS: {
1056 struct ccb_trans_settings_scsi *scsi;
1057 struct ccb_trans_settings_spi *spi;
1058 struct ccb_trans_settings *cts;
1061 #ifdef SCSI_LOW_DIAGNOSTIC
1062 if (target == CAM_TARGET_WILDCARD)
1064 printf("%s: invalid target\n", slp->sl_xname);
1065 ccb->ccb_h.status = CAM_REQ_INVALID;
1069 #endif /* SCSI_LOW_DIAGNOSTIC */
1071 ti = slp->sl_ti[target];
1072 if (lun == CAM_LUN_WILDCARD)
1075 s = SCSI_LOW_SPLSCSI();
1076 scsi = &cts->proto_specific.scsi;
1077 spi = &cts->xport_specific.spi;
1078 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
1079 CTS_SPI_VALID_SYNC_RATE |
1080 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
1082 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
1083 val = spi->bus_width;
1084 if (val < ti->ti_width)
1087 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
1088 val = spi->sync_period;
1089 if (val == 0 || val > ti->ti_maxsynch.period)
1090 ti->ti_maxsynch.period = val;
1092 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
1093 val = spi->sync_offset;
1094 if (val < ti->ti_maxsynch.offset)
1095 ti->ti_maxsynch.offset = val;
1097 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1098 scsi_low_calcf_target(ti);
1101 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
1102 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1104 li = scsi_low_alloc_li(ti, lun, 1);
1105 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
1106 li->li_quirks |= SCSI_LOW_DISK_DISC;
1108 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
1111 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
1112 li->li_quirks |= SCSI_LOW_DISK_QTAG;
1114 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
1116 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1117 scsi_low_calcf_target(ti);
1118 scsi_low_calcf_lun(li);
1119 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
1120 scsi_low_calcf_show(li);
1124 ccb->ccb_h.status = CAM_REQ_CMP;
1129 case XPT_GET_TRAN_SETTINGS: {
1130 struct ccb_trans_settings *cts;
1134 #ifdef SCSI_LOW_DIAGNOSTIC
1135 if (target == CAM_TARGET_WILDCARD)
1137 printf("%s: invalid target\n", slp->sl_xname);
1138 ccb->ccb_h.status = CAM_REQ_INVALID;
1142 #endif /* SCSI_LOW_DIAGNOSTIC */
1143 ti = slp->sl_ti[target];
1144 if (lun == CAM_LUN_WILDCARD)
1147 s = SCSI_LOW_SPLSCSI();
1148 li = scsi_low_alloc_li(ti, lun, 1);
1149 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1150 struct ccb_trans_settings_scsi *scsi =
1151 &cts->proto_specific.scsi;
1152 struct ccb_trans_settings_spi *spi =
1153 &cts->xport_specific.spi;
1154 #ifdef SCSI_LOW_DIAGNOSTIC
1155 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
1157 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1158 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
1162 #endif /* SCSI_LOW_DIAGNOSTIC */
1163 cts->protocol = PROTO_SCSI;
1164 cts->protocol_version = SCSI_REV_2;
1165 cts->transport = XPORT_SPI;
1166 cts->transport_version = 2;
1168 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1169 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1171 diskflags = li->li_diskflags & li->li_cfgflags;
1172 if (diskflags & SCSI_LOW_DISK_DISC)
1173 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1174 if (diskflags & SCSI_LOW_DISK_QTAG)
1175 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1177 spi->sync_period = ti->ti_maxsynch.period;
1178 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1179 spi->sync_offset = ti->ti_maxsynch.offset;
1180 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1182 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1183 spi->bus_width = ti->ti_width;
1185 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1186 scsi->valid = CTS_SCSI_VALID_TQ;
1187 spi->valid |= CTS_SPI_VALID_DISC;
1191 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1198 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
1199 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1204 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1205 s = SCSI_LOW_SPLSCSI();
1206 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
1208 ccb->ccb_h.status = CAM_REQ_CMP;
1212 case XPT_TERM_IO: /* Terminate the I/O process */
1213 ccb->ccb_h.status = CAM_REQ_INVALID;
1217 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1218 #ifdef SCSI_LOW_DIAGNOSTIC
1219 if (target == CAM_TARGET_WILDCARD)
1221 printf("%s: invalid target\n", slp->sl_xname);
1222 ccb->ccb_h.status = CAM_REQ_INVALID;
1226 #endif /* SCSI_LOW_DIAGNOSTIC */
1228 msg = SCSI_LOW_MSG_RESET;
1229 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
1231 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1236 ti = slp->sl_ti[target];
1237 if (lun == CAM_LUN_WILDCARD)
1241 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1242 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
1244 flags = CCB_NORETRY | CCB_URGENT;
1246 s = SCSI_LOW_SPLSCSI();
1247 li = scsi_low_alloc_li(ti, lun, 1);
1248 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
1252 case XPT_PATH_INQ: { /* Path routing inquiry */
1253 struct ccb_pathinq *cpi = &ccb->cpi;
1255 cpi->version_num = scsi_low_version_major;
1256 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
1257 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
1258 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
1259 cpi->hba_inquiry |= PI_WIDE_16;
1260 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
1261 cpi->hba_inquiry |= PI_WIDE_32;
1262 if (ti->ti_maxsynch.offset > 0)
1263 cpi->hba_inquiry |= PI_SDTR_ABLE;
1264 cpi->target_sprt = 0;
1266 cpi->hba_eng_cnt = 0;
1267 cpi->max_target = slp->sl_ntargs - 1;
1268 cpi->max_lun = slp->sl_nluns - 1;
1269 cpi->initiator_id = slp->sl_hostid;
1270 cpi->bus_id = cam_sim_bus(sim);
1271 cpi->base_transfer_speed = 3300;
1272 cpi->transport = XPORT_SPI;
1273 cpi->transport_version = 2;
1274 cpi->protocol = PROTO_SCSI;
1275 cpi->protocol_version = SCSI_REV_2;
1276 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1277 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
1278 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1279 cpi->unit_number = cam_sim_unit(sim);
1280 cpi->ccb_h.status = CAM_REQ_CMP;
1286 printf("scsi_low: non support func_code = %d ",
1287 ccb->ccb_h.func_code);
1288 ccb->ccb_h.status = CAM_REQ_INVALID;
1295 scsi_low_attach_cam(slp)
1296 struct scsi_low_softc *slp;
1298 struct cam_devq *devq;
1299 int tagged_openings;
1301 sprintf(slp->sl_xname, "%s%d",
1302 DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
1304 devq = cam_simq_alloc(SCSI_LOW_NCCB);
1309 * ask the adapter what subunits are present
1311 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
1312 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
1314 DEVPORT_DEVNAME(slp->sl_dev), slp,
1315 DEVPORT_DEVUNIT(slp->sl_dev), &Giant,
1316 slp->sl_openings, tagged_openings, devq);
1318 if (slp->sl_si.sim == NULL) {
1319 cam_simq_free(devq);
1323 if (xpt_bus_register(slp->sl_si.sim, NULL, 0) != CAM_SUCCESS) {
1324 free(slp->sl_si.sim, M_SCSILOW);
1328 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
1329 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
1330 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1331 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1332 cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
1336 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
1341 scsi_low_world_start_cam(slp)
1342 struct scsi_low_softc *slp;
1349 scsi_low_dettach_cam(slp)
1350 struct scsi_low_softc *slp;
1353 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
1354 xpt_free_path(slp->sl_si.path);
1355 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1356 cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
1361 scsi_low_ccb_setup_cam(slp, cb)
1362 struct scsi_low_softc *slp;
1365 union ccb *ccb = (union ccb *) cb->osdep;
1367 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
1369 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
1370 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
1371 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
1372 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
1373 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1374 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
1375 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
1376 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1377 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
1381 scsi_low_unit_ready_cmd(cb);
1383 return SCSI_LOW_START_QTAG;
1387 scsi_low_done_cam(slp, cb)
1388 struct scsi_low_softc *slp;
1393 ccb = (union ccb *) cb->osdep;
1394 if (cb->ccb_error == 0)
1396 ccb->ccb_h.status = CAM_REQ_CMP;
1397 ccb->csio.resid = 0;
1401 if (cb->ccb_rcnt >= slp->sl_max_retry)
1402 cb->ccb_error |= ABORTIO;
1404 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
1405 (cb->ccb_error & ABORTIO) == 0)
1408 if ((cb->ccb_error & SENSEIO) != 0)
1410 memcpy(&ccb->csio.sense_data,
1412 sizeof(ccb->csio.sense_data));
1415 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
1416 &scsi_low_error_code_cam[0]);
1418 #ifdef SCSI_LOW_DIAGNOSTIC
1419 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
1420 cb->ccb_scp.scp_cmdlen > 0 &&
1421 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1422 SCSI_LOW_CMD_ABORT_WARNING) != 0)
1424 printf("%s: WARNING: scsi_low IO abort\n",
1426 scsi_low_print(slp, NULL);
1428 #endif /* SCSI_LOW_DIAGNOSTIC */
1431 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
1432 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1434 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
1435 ccb->csio.scsi_status = 0; /* XXX */
1437 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
1439 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
1445 scsi_low_timeout_cam(slp, ch, action)
1446 struct scsi_low_softc *slp;
1453 case SCSI_LOW_TIMEOUT_CH_IO:
1456 case SCSI_LOW_TIMEOUT_START:
1457 slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
1458 hz / SCSI_LOW_TIMEOUT_HZ);
1460 case SCSI_LOW_TIMEOUT_STOP:
1461 untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
1466 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
1469 case SCSI_LOW_TIMEOUT_START:
1470 slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
1472 case SCSI_LOW_TIMEOUT_STOP:
1473 untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
1477 case SCSI_LOW_TIMEOUT_CH_RECOVER:
1482 #endif /* SCSI_LOW_INTERFACE_CAM */
1484 /*=============================================================
1485 * END OF OS switch (All OS depend fucntions should be above)
1486 =============================================================*/
1488 /**************************************************************
1489 * scsi low deactivate and activate
1490 **************************************************************/
1492 scsi_low_is_busy(slp)
1493 struct scsi_low_softc *slp;
1496 if (slp->sl_nio > 0)
1502 scsi_low_deactivate(slp)
1503 struct scsi_low_softc *slp;
1507 s = SCSI_LOW_SPLSCSI();
1508 slp->sl_flags |= HW_INACTIVE;
1509 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1510 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
1511 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1512 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1518 scsi_low_activate(slp)
1519 struct scsi_low_softc *slp;
1523 s = SCSI_LOW_SPLSCSI();
1524 slp->sl_flags &= ~HW_INACTIVE;
1525 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
1527 slp->sl_flags |= HW_INACTIVE;
1532 slp->sl_timeout_count = 0;
1533 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1534 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1539 /**************************************************************
1541 **************************************************************/
1542 #ifdef SCSI_LOW_DIAGNOSTIC
1543 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
1544 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
1545 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
1548 scsi_low_msg_log_init(slmlp)
1549 struct scsi_low_msg_log *slmlp;
1552 slmlp->slml_ptr = 0;
1556 scsi_low_msg_log_write(slmlp, datap, len)
1557 struct scsi_low_msg_log *slmlp;
1563 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1566 ptr = slmlp->slml_ptr ++;
1567 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1568 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1569 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1570 slmlp->slml_msg[ptr].msg[ind] = 0;
1574 scsi_low_msg_log_show(slmlp, s, len)
1575 struct scsi_low_msg_log *slmlp;
1581 printf("%s: (%d) ", s, slmlp->slml_ptr);
1582 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1584 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1587 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1593 #endif /* SCSI_LOW_DIAGNOSTIC */
1595 /**************************************************************
1597 **************************************************************/
1599 scsi_low_engage(arg)
1602 struct scsi_low_softc *slp = arg;
1603 int s = SCSI_LOW_SPLSCSI();
1605 switch (slp->sl_rstep)
1609 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1610 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1611 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1616 slp->sl_flags &= ~HW_RESUME;
1617 scsi_low_start(slp);
1627 scsi_low_init(slp, flags)
1628 struct scsi_low_softc *slp;
1633 slp->sl_flags |= HW_INITIALIZING;
1635 /* clear power control timeout */
1636 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1638 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1639 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1640 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1642 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1645 /* reset current nexus */
1646 scsi_low_reset_nexus(slp, flags);
1647 if ((slp->sl_flags & HW_INACTIVE) != 0)
1653 if (flags != SCSI_LOW_RESTART_SOFT)
1655 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1659 slp->sl_flags &= ~HW_INITIALIZING;
1663 /**************************************************************
1665 **************************************************************/
1666 static struct lun_info *
1667 scsi_low_alloc_li(ti, lun, alloc)
1668 struct targ_info *ti;
1672 struct scsi_low_softc *slp = ti->ti_sc;
1673 struct lun_info *li;
1675 li = LIST_FIRST(&ti->ti_litab);
1678 if (li->li_lun == lun)
1681 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1683 if (li->li_lun == lun)
1685 LIST_REMOVE(li, lun_chain);
1686 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1695 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1697 panic("no lun info mem");
1699 SCSI_LOW_BZERO(li, ti->ti_lunsize);
1703 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1705 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1706 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1707 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1708 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1709 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1711 li->li_qtagbits = (u_int) -1;
1713 TAILQ_INIT(&li->li_discq);
1714 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1716 /* host specific structure initialization per lun */
1717 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1718 (*slp->sl_funcs->scsi_low_lun_init)
1719 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1720 scsi_low_calcf_lun(li);
1724 /**************************************************************
1725 * allocate targ_info
1726 **************************************************************/
1727 static struct targ_info *
1728 scsi_low_alloc_ti(slp, targ)
1729 struct scsi_low_softc *slp;
1732 struct targ_info *ti;
1734 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1735 TAILQ_INIT(&slp->sl_titab);
1737 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1739 panic("%s short of memory", slp->sl_xname);
1741 SCSI_LOW_BZERO(ti, slp->sl_targsize);
1745 slp->sl_ti[targ] = ti;
1746 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1747 LIST_INIT(&ti->ti_litab);
1749 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1750 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1751 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1752 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1753 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1754 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1756 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1758 (*slp->sl_funcs->scsi_low_targ_init)
1759 (slp, ti, SCSI_LOW_INFO_ALLOC);
1761 scsi_low_calcf_target(ti);
1766 scsi_low_free_ti(slp)
1767 struct scsi_low_softc *slp;
1769 struct targ_info *ti, *tib;
1770 struct lun_info *li, *nli;
1772 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1774 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1776 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1778 (*slp->sl_funcs->scsi_low_lun_init)
1779 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1781 nli = LIST_NEXT(li, lun_chain);
1785 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1787 (*slp->sl_funcs->scsi_low_targ_init)
1788 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1790 tib = TAILQ_NEXT(ti, ti_chain);
1795 /**************************************************************
1797 **************************************************************/
1799 scsi_low_bus_idle(slp)
1800 struct scsi_low_softc *slp;
1803 slp->sl_retry_sel = 0;
1804 if (slp->sl_Tnexus == NULL)
1805 scsi_low_start(slp);
1809 scsi_low_timeout(arg)
1812 struct scsi_low_softc *slp = arg;
1815 s = SCSI_LOW_SPLSCSI();
1816 (void) scsi_low_timeout_check(slp);
1817 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1818 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1823 scsi_low_timeout_check(slp)
1824 struct scsi_low_softc *slp;
1826 struct targ_info *ti;
1827 struct lun_info *li;
1828 struct slccb *cb = NULL; /* XXX */
1830 /* selection restart */
1831 if (slp->sl_retry_sel != 0)
1833 slp->sl_retry_sel = 0;
1834 if (slp->sl_Tnexus != NULL)
1837 cb = TAILQ_FIRST(&slp->sl_start);
1841 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1843 cb->ccb_flags |= CCB_NORETRY;
1844 cb->ccb_error |= SELTIMEOUTIO;
1845 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1846 panic("%s: ccb not finished", slp->sl_xname);
1849 if (slp->sl_Tnexus == NULL)
1850 scsi_low_start(slp);
1853 /* call hardware timeout */
1855 if (slp->sl_funcs->scsi_low_timeout != NULL)
1857 (*slp->sl_funcs->scsi_low_timeout) (slp);
1860 if (slp->sl_timeout_count ++ <
1861 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1864 slp->sl_timeout_count = 0;
1865 if (slp->sl_nio > 0)
1867 if ((cb = slp->sl_Qnexus) != NULL)
1869 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1873 else if (slp->sl_disc == 0)
1875 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1878 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1882 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1883 ti = TAILQ_NEXT(ti, ti_chain))
1885 if (ti->ti_disc == 0)
1888 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1889 li = LIST_NEXT(li, lun_chain))
1891 for (cb = TAILQ_FIRST(&li->li_discq);
1893 cb = TAILQ_NEXT(cb, ccb_chain))
1896 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1904 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1906 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1909 if (slp->sl_active != 0)
1911 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1917 if (slp->sl_powc < 0)
1919 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1920 slp->sl_flags |= HW_POWDOWN;
1921 (*slp->sl_funcs->scsi_low_power)
1922 (slp, SCSI_LOW_POWDOWN);
1928 cb->ccb_error |= TIMEOUTIO;
1929 printf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
1930 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1931 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1932 scsi_low_start(slp);
1938 scsi_low_abort_ccb(slp, cb)
1939 struct scsi_low_softc *slp;
1942 struct targ_info *ti;
1943 struct lun_info *li;
1948 if ((cb->ccb_omsgoutflag &
1949 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1954 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1955 msg = SCSI_LOW_MSG_ABORT;
1957 msg = SCSI_LOW_MSG_ABORT_QTAG;
1959 cb->ccb_error |= ABORTIO;
1960 cb->ccb_flags |= CCB_NORETRY;
1961 scsi_low_ccb_message_assert(cb, msg);
1963 if (cb == slp->sl_Qnexus)
1965 scsi_low_assert_msg(slp, ti, msg, 1);
1967 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1969 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1970 panic("%s: revoked ccb done", slp->sl_xname);
1972 cb->ccb_flags |= CCB_STARTQ;
1973 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1975 if (slp->sl_Tnexus == NULL)
1976 scsi_low_start(slp);
1980 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1981 panic("%s: revoked ccb retried", slp->sl_xname);
1986 /**************************************************************
1987 * Generic SCSI INTERFACE
1988 **************************************************************/
1990 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1991 struct scsi_low_softc *slp;
1992 int openings, ntargs, nluns, targsize, lunsize;
1994 struct targ_info *ti;
1995 struct lun_info *li;
1998 #ifdef SCSI_LOW_INTERFACE_XS
1999 slp->sl_osdep_fp = &scsi_low_osdep_funcs_xs;
2000 #endif /* SCSI_LOW_INTERFACE_XS */
2001 #ifdef SCSI_LOW_INTERFACE_CAM
2002 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
2003 #endif /* SCSI_LOW_INTERFACE_CAM */
2005 if (slp->sl_osdep_fp == NULL)
2006 panic("scsi_low: interface not spcified");
2008 if (ntargs > SCSI_LOW_NTARGETS)
2010 printf("scsi_low: %d targets are too large\n", ntargs);
2011 printf("change kernel options SCSI_LOW_NTARGETS");
2016 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
2018 slp->sl_openings = openings;
2019 slp->sl_ntargs = ntargs;
2020 slp->sl_nluns = nluns;
2021 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
2023 if (lunsize < sizeof(struct lun_info))
2024 lunsize = sizeof(struct lun_info);
2026 if (targsize < sizeof(struct targ_info))
2027 targsize = sizeof(struct targ_info);
2029 slp->sl_targsize = targsize;
2030 for (i = 0; i < ntargs; i ++)
2032 ti = scsi_low_alloc_ti(slp, i);
2033 ti->ti_lunsize = lunsize;
2034 li = scsi_low_alloc_li(ti, 0, 1);
2037 /* initialize queue */
2038 nccb = openings * ntargs;
2039 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
2040 nccb = SCSI_LOW_NCCB;
2041 scsi_low_init_ccbque(nccb);
2042 TAILQ_INIT(&slp->sl_start);
2044 /* call os depend attach */
2045 s = SCSI_LOW_SPLSCSI();
2046 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
2050 printf("%s: scsi_low_attach: osdep attach failed\n",
2055 /* check hardware */
2056 SCSI_LOW_DELAY(1000); /* wait for 1ms */
2057 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
2060 printf("%s: scsi_low_attach: initialization failed\n",
2065 /* start watch dog */
2066 slp->sl_timeout_count = 0;
2067 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2068 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
2069 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
2072 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
2074 #ifdef SCSI_LOW_START_UP_CHECK
2075 /* probing devices */
2076 scsi_low_start_up(slp);
2077 #endif /* SCSI_LOW_START_UP_CHECK */
2079 /* call os depend attach done*/
2080 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
2086 scsi_low_dettach(slp)
2087 struct scsi_low_softc *slp;
2091 s = SCSI_LOW_SPLSCSI();
2092 if (scsi_low_is_busy(slp) != 0)
2098 scsi_low_deactivate(slp);
2100 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
2107 scsi_low_free_ti(slp);
2108 LIST_REMOVE(slp, sl_chain);
2113 /**************************************************************
2115 **************************************************************/
2117 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
2118 struct scsi_low_softc *slp;
2119 struct targ_info *ti;
2120 struct lun_info *li;
2128 scsi_low_ccb_message_assert(cb, msg);
2130 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
2131 scsi_low_alloc_qtag(cb);
2133 cb->ccb_flags = flags | CCB_STARTQ;
2134 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2135 cb->ccb_error |= PENDINGIO;
2137 if ((flags & CCB_URGENT) != 0)
2139 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2143 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
2148 if (slp->sl_Tnexus == NULL)
2149 scsi_low_start(slp);
2154 scsi_low_message_enqueue(slp, ti, li, flags)
2155 struct scsi_low_softc *slp;
2156 struct targ_info *ti;
2157 struct lun_info *li;
2163 tmsgflags = ti->ti_setup_msg;
2164 ti->ti_setup_msg = 0;
2166 flags |= CCB_NORETRY;
2167 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
2172 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
2176 /**************************************************************
2177 * Generic Start & Done
2178 **************************************************************/
2179 #define SLSC_MODE_SENSE_SHORT 0x1a
2180 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
2181 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
2182 sizeof(struct scsi_low_mode_sense_data), 0};
2183 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
2184 sizeof(struct scsi_low_inq_data), 0};
2185 static u_int8_t unit_ready_cmd[6];
2186 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2187 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2188 static int scsi_low_resume(struct scsi_low_softc *);
2191 scsi_low_unit_ready_cmd(cb)
2195 cb->ccb_scp.scp_cmd = unit_ready_cmd;
2196 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2197 cb->ccb_scp.scp_datalen = 0;
2198 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2203 scsi_low_sense_abort_start(slp, ti, li, cb)
2204 struct scsi_low_softc *slp;
2205 struct targ_info *ti;
2206 struct lun_info *li;
2210 cb->ccb_scp.scp_cmdlen = 6;
2211 SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
2212 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
2213 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
2214 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
2215 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
2216 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
2217 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2219 scsi_low_ccb_message_clear(cb);
2220 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
2222 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2226 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
2227 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
2228 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
2229 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
2232 return SCSI_LOW_START_NO_QTAG;
2236 scsi_low_setup_start(slp, ti, li, cb)
2237 struct scsi_low_softc *slp;
2238 struct targ_info *ti;
2239 struct lun_info *li;
2243 switch(li->li_state)
2245 case SCSI_LOW_LUN_SLEEP:
2246 scsi_low_unit_ready_cmd(cb);
2249 case SCSI_LOW_LUN_START:
2250 cb->ccb_scp.scp_cmd = ss_cmd;
2251 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
2252 cb->ccb_scp.scp_datalen = 0;
2253 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2257 case SCSI_LOW_LUN_INQ:
2258 cb->ccb_scp.scp_cmd = inq_cmd;
2259 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
2260 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
2261 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
2262 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2266 case SCSI_LOW_LUN_MODEQ:
2267 cb->ccb_scp.scp_cmd = sms_cmd;
2268 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
2269 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
2270 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
2271 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2273 return SCSI_LOW_START_QTAG;
2276 panic("%s: no setup phase", slp->sl_xname);
2279 return SCSI_LOW_START_NO_QTAG;
2283 scsi_low_resume(slp)
2284 struct scsi_low_softc *slp;
2287 if (slp->sl_flags & HW_RESUME)
2289 slp->sl_flags &= ~HW_POWDOWN;
2290 if (slp->sl_funcs->scsi_low_power != NULL)
2292 slp->sl_flags |= HW_RESUME;
2294 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
2295 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2296 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
2297 SCSI_LOW_TIMEOUT_START);
2305 struct scsi_low_softc *slp;
2307 struct targ_info *ti;
2308 struct lun_info *li;
2312 /* check hardware exists or under initializations ? */
2313 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
2316 /* check hardware power up ? */
2317 if ((slp->sl_flags & HW_POWERCTRL) != 0)
2320 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
2322 if (scsi_low_resume(slp) == EJUSTRETURN)
2328 #ifdef SCSI_LOW_DIAGNOSTIC
2329 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
2331 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
2332 panic("%s: inconsistent", slp->sl_xname);
2334 #endif /* SCSI_LOW_DIAGNOSTIC */
2336 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
2337 cb = TAILQ_NEXT(cb, ccb_chain))
2341 if (li->li_disc == 0)
2343 goto scsi_low_cmd_start;
2345 else if (li->li_nqio > 0)
2347 if (li->li_nqio < li->li_maxnqio ||
2348 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2349 goto scsi_low_cmd_start;
2355 cb->ccb_flags &= ~CCB_STARTQ;
2356 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
2359 /* clear all error flag bits (for restart) */
2361 cb->ccb_datalen = -1;
2362 cb->ccb_scp.scp_status = ST_UNKNOWN;
2364 /* setup nexus pointer */
2365 slp->sl_Qnexus = cb;
2366 slp->sl_Lnexus = li;
2367 slp->sl_Tnexus = ti;
2369 /* initialize msgsys */
2370 scsi_low_init_msgsys(slp, ti);
2373 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2375 /* CA state or forced abort */
2376 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
2378 else if (li->li_state >= SCSI_LOW_LUN_OK)
2380 cb->ccb_flags &= ~CCB_INTERNAL;
2381 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
2382 if (cb->ccb_msgoutflag != 0)
2384 scsi_low_ccb_message_exec(slp, cb);
2389 cb->ccb_flags |= CCB_INTERNAL;
2390 rv = scsi_low_setup_start(slp, ti, li, cb);
2394 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
2396 if (rv == SCSI_LOW_START_QTAG &&
2397 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
2402 scsi_low_activate_qtag(cb);
2403 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2404 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
2405 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
2406 else if ((cb->ccb_flags & CCB_URGENT) != 0)
2407 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
2409 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
2410 scsi_low_assert_msg(slp, ti, qmsg, 0);
2414 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
2415 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2416 cb->ccb_tc = cb->ccb_tcmax;
2418 /* setup saved scsi data pointer */
2419 cb->ccb_sscp = cb->ccb_scp;
2421 /* setup current scsi pointer */
2422 slp->sl_scp = cb->ccb_sscp;
2423 slp->sl_error = cb->ccb_error;
2425 /* assert always an identify msg */
2426 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
2429 #ifdef SCSI_LOW_DIAGNOSTIC
2430 scsi_low_msg_log_init(&ti->ti_log_msgin);
2431 scsi_low_msg_log_init(&ti->ti_log_msgout);
2432 #endif /* SCSI_LOW_DIAGNOSTIC */
2434 /* selection start */
2436 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
2437 if (rv == SCSI_LOW_START_OK)
2439 #ifdef SCSI_LOW_STATICS
2440 scsi_low_statics.nexus_win ++;
2441 #endif /* SCSI_LOW_STATICS */
2445 scsi_low_arbit_fail(slp, cb);
2446 #ifdef SCSI_LOW_STATICS
2447 scsi_low_statics.nexus_fail ++;
2448 #endif /* SCSI_LOW_STATICS */
2452 scsi_low_arbit_fail(slp, cb)
2453 struct scsi_low_softc *slp;
2456 struct targ_info *ti = cb->ti;
2458 scsi_low_deactivate_qtag(cb);
2459 scsi_low_ccb_message_retry(cb);
2460 cb->ccb_flags |= CCB_STARTQ;
2461 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2463 scsi_low_bus_release(slp, ti);
2466 if (slp->sl_disc == 0)
2468 #ifdef SCSI_LOW_DIAGNOSTIC
2469 printf("%s: try selection again\n", slp->sl_xname);
2470 #endif /* SCSI_LOW_DIAGNOSTIC */
2471 slp->sl_retry_sel = 1;
2476 scsi_low_bus_release(slp, ti)
2477 struct scsi_low_softc *slp;
2478 struct targ_info *ti;
2481 if (ti->ti_disc > 0)
2483 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
2487 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
2490 /* clear all nexus pointer */
2491 slp->sl_Qnexus = NULL;
2492 slp->sl_Lnexus = NULL;
2493 slp->sl_Tnexus = NULL;
2495 /* clear selection assert */
2496 slp->sl_selid = NULL;
2498 /* clear nexus data */
2499 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
2501 /* clear phase change counter */
2502 slp->sl_ph_count = 0;
2506 scsi_low_setup_done(slp, cb)
2507 struct scsi_low_softc *slp;
2510 struct targ_info *ti;
2511 struct lun_info *li;
2516 if (cb->ccb_rcnt >= slp->sl_max_retry)
2518 cb->ccb_error |= ABORTIO;
2519 return SCSI_LOW_DONE_COMPLETE;
2522 /* XXX: special huck for selection timeout */
2523 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
2524 (cb->ccb_error & SELTIMEOUTIO) != 0)
2526 cb->ccb_error |= ABORTIO;
2527 return SCSI_LOW_DONE_COMPLETE;
2530 switch(li->li_state)
2532 case SCSI_LOW_LUN_INQ:
2533 if (cb->ccb_error != 0)
2536 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2540 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2542 else if ((li->li_inq.sd_version & 7) >= 2 ||
2543 (li->li_inq.sd_len >= 4))
2545 if ((li->li_inq.sd_support & 0x2) == 0)
2546 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2547 if ((li->li_inq.sd_support & 0x8) == 0)
2548 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2551 if ((li->li_inq.sd_support & 0x10) == 0)
2552 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2553 if ((li->li_inq.sd_support & 0x20) == 0)
2554 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2555 if ((li->li_inq.sd_support & 0x40) == 0)
2556 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2561 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2564 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2566 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2568 scsi_low_calcf_target(ti);
2569 scsi_low_calcf_lun(li);
2572 case SCSI_LOW_LUN_MODEQ:
2573 if (cb->ccb_error != 0)
2575 if (cb->ccb_error & SENSEIO)
2577 #ifdef SCSI_LOW_DEBUG
2578 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2580 int error_code, sense_key, asc, ascq;
2582 scsi_extract_sense(&cb->ccb_sense,
2587 printf("SENSE: [%x][%x][%x][%x]\n",
2588 error_code, sense_key, asc,
2591 #endif /* SCSI_LOW_DEBUG */
2595 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2598 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2600 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2601 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2603 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2604 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2605 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2607 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2608 scsi_low_calcf_lun(li);
2616 if (li->li_state == SCSI_LOW_LUN_OK)
2618 scsi_low_calcf_target(ti);
2619 scsi_low_calcf_lun(li);
2620 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2621 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2623 scsi_low_calcf_show(li);
2628 return SCSI_LOW_DONE_RETRY;
2632 scsi_low_done(slp, cb)
2633 struct scsi_low_softc *slp;
2638 if (cb->ccb_error == 0)
2640 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2642 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2644 * SCSI-2 draft suggests
2645 * page 0x0a QErr bit determins if
2646 * the target aborts or continues
2647 * the queueing io's after CA state resolved.
2648 * However many targets seem not to support
2649 * the page 0x0a. Thus we should manually clear the
2650 * queuing io's after CA state.
2652 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2655 cb->ccb_flags |= CCB_CLEARQ;
2658 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2660 if ((cb->ccb_flags & CCB_SENSE) != 0)
2661 cb->ccb_error |= (SENSEIO | ABORTIO);
2662 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2664 else switch (cb->ccb_sscp.scp_status)
2670 if (cb->ccb_datalen == 0 ||
2671 cb->ccb_scp.scp_datalen == 0)
2674 if (cb->ccb_scp.scp_cmdlen > 0 &&
2675 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2676 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2679 cb->ccb_error |= PDMAERR;
2684 cb->ccb_error |= (BUSYERR | STATERR);
2688 cb->ccb_error |= (STATERR | ABORTIO);
2693 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2696 cb->ccb_flags |= CCB_SENSE;
2699 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2704 cb->ccb_error |= FATALIO;
2710 if (cb->ccb_flags & CCB_SENSE)
2712 cb->ccb_error |= (SENSEERR | ABORTIO);
2714 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2718 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2720 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2724 /* check a ccb msgout flag */
2725 if (cb->ccb_omsgoutflag != 0)
2727 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2728 SCSI_LOW_MSG_ABORT_QTAG | \
2729 SCSI_LOW_MSG_CLEAR_QTAG | \
2730 SCSI_LOW_MSG_TERMIO)
2732 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2734 cb->ccb_error |= ABORTIO;
2738 /* call OS depend done */
2739 if (cb->osdep != NULL)
2741 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2742 if (rv == EJUSTRETURN)
2745 else if (cb->ccb_error != 0)
2747 if (cb->ccb_rcnt >= slp->sl_max_retry)
2748 cb->ccb_error |= ABORTIO;
2750 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2751 (cb->ccb_error & ABORTIO) == 0)
2755 /* free our target */
2756 #ifdef SCSI_LOW_DEBUG
2757 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2759 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2760 scsi_low_print(slp, NULL);
2762 #endif /* SCSI_LOW_DEBUG */
2764 scsi_low_deactivate_qtag(cb);
2765 scsi_low_dealloc_qtag(cb);
2766 scsi_low_free_ccb(cb);
2768 return SCSI_LOW_DONE_COMPLETE;
2771 #ifdef SCSI_LOW_DEBUG
2772 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2774 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2775 scsi_low_print(slp, NULL);
2777 #endif /* SCSI_LOW_DEBUG */
2780 scsi_low_deactivate_qtag(cb);
2781 scsi_low_ccb_message_retry(cb);
2782 return SCSI_LOW_DONE_RETRY;
2785 /**************************************************************
2787 **************************************************************/
2789 scsi_low_reset_nexus_target(slp, ti, fdone)
2790 struct scsi_low_softc *slp;
2791 struct targ_info *ti;
2794 struct lun_info *li;
2796 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2797 li = LIST_NEXT(li, lun_chain))
2799 scsi_low_reset_nexus_lun(slp, li, fdone);
2800 li->li_state = SCSI_LOW_LUN_SLEEP;
2805 ti->ti_setup_msg = 0;
2806 ti->ti_setup_msg_done = 0;
2808 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2809 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2811 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2812 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2814 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2816 ((*slp->sl_funcs->scsi_low_targ_init)
2817 (slp, ti, SCSI_LOW_INFO_REVOKE));
2819 scsi_low_calcf_target(ti);
2821 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2822 li = LIST_NEXT(li, lun_chain))
2826 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2827 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2829 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2831 ((*slp->sl_funcs->scsi_low_lun_init)
2832 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2834 scsi_low_calcf_lun(li);
2839 scsi_low_reset_nexus(slp, fdone)
2840 struct scsi_low_softc *slp;
2843 struct targ_info *ti;
2844 struct slccb *cb, *topcb;
2846 if ((cb = slp->sl_Qnexus) != NULL)
2848 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2855 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2856 ti = TAILQ_NEXT(ti, ti_chain))
2858 scsi_low_reset_nexus_target(slp, ti, fdone);
2859 scsi_low_bus_release(slp, ti);
2860 scsi_low_init_msgsys(slp, ti);
2865 topcb->ccb_flags |= CCB_STARTQ;
2866 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2870 slp->sl_retry_sel = 0;
2871 slp->sl_flags &= ~HW_PDMASTART;
2876 static char tw_chars[] = "|/-\\";
2877 #define TWIDDLEWAIT 10000
2880 scsi_low_twiddle_wait(void)
2884 cnputc(tw_chars[tw_pos++]);
2885 tw_pos %= (sizeof(tw_chars) - 1);
2886 SCSI_LOW_DELAY(TWIDDLEWAIT);
2890 scsi_low_bus_reset(slp)
2891 struct scsi_low_softc *slp;
2895 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2897 printf("%s: try to reset scsi bus ", slp->sl_xname);
2898 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2899 scsi_low_twiddle_wait();
2905 scsi_low_restart(slp, flags, s)
2906 struct scsi_low_softc *slp;
2913 printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
2915 if ((error = scsi_low_init(slp, flags)) != 0)
2918 scsi_low_start(slp);
2922 /**************************************************************
2923 * disconnect and reselect
2924 **************************************************************/
2925 #define MSGCMD_LUN(msg) (msg & 0x07)
2927 static struct slccb *
2928 scsi_low_establish_ccb(ti, li, tag)
2929 struct targ_info *ti;
2930 struct lun_info *li;
2933 struct scsi_low_softc *slp = ti->ti_sc;
2939 cb = TAILQ_FIRST(&li->li_discq);
2940 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2941 if (cb->ccb_tag == tag)
2946 * establish our ccb nexus
2949 #ifdef SCSI_LOW_DEBUG
2950 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2952 printf("%s: nexus(0x%lx) abort check start\n",
2953 slp->sl_xname, (u_long) cb);
2954 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2955 scsi_low_revoke_ccb(slp, cb, 1);
2959 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2961 if (cb->ccb_omsgoutflag == 0)
2962 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2964 #endif /* SCSI_LOW_DEBUG */
2966 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2967 cb->ccb_flags &= ~CCB_DISCQ;
2968 slp->sl_Qnexus = cb;
2970 slp->sl_scp = cb->ccb_sscp;
2971 slp->sl_error |= cb->ccb_error;
2977 /* inform "ccb nexus established" to the host driver */
2978 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2981 if (cb->ccb_msgoutflag != 0)
2983 scsi_low_ccb_message_exec(slp, cb);
2990 scsi_low_reselected(slp, targ)
2991 struct scsi_low_softc *slp;
2994 struct targ_info *ti;
2999 * Check select vs reselected collision.
3002 if ((cb = slp->sl_selid) != NULL)
3004 scsi_low_arbit_fail(slp, cb);
3005 #ifdef SCSI_LOW_STATICS
3006 scsi_low_statics.nexus_conflict ++;
3007 #endif /* SCSI_LOW_STATICS */
3011 * Check if no current active nexus.
3013 if (slp->sl_Tnexus != NULL)
3020 * Check a valid target id asserted ?
3022 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
3024 s = "scsi id illegal";
3029 * Check the target scsi status.
3031 ti = slp->sl_ti[targ];
3032 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
3034 s = "phase mismatch";
3042 scsi_low_init_msgsys(slp, ti);
3045 * Establish our target nexus
3047 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
3048 slp->sl_Tnexus = ti;
3049 #ifdef SCSI_LOW_STATICS
3050 scsi_low_statics.nexus_reselected ++;
3051 #endif /* SCSI_LOW_STATICS */
3055 printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
3056 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
3057 "reselect: scsi world confused");
3061 /**************************************************************
3062 * cmd out pointer setup
3063 **************************************************************/
3065 scsi_low_cmd(slp, ti)
3066 struct scsi_low_softc *slp;
3067 struct targ_info *ti;
3069 struct slccb *cb = slp->sl_Qnexus;
3071 slp->sl_ph_count ++;
3077 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
3078 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
3079 slp->sl_scp.scp_datalen = 0;
3080 slp->sl_scp.scp_direction = SCSI_LOW_READ;
3081 slp->sl_error |= FATALIO;
3082 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3083 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
3088 #ifdef SCSI_LOW_DEBUG
3089 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
3091 scsi_low_test_cmdlnk(slp, cb);
3093 #endif /* SCSI_LOW_DEBUG */
3098 /**************************************************************
3099 * data out pointer setup
3100 **************************************************************/
3102 scsi_low_data(slp, ti, bp, direction)
3103 struct scsi_low_softc *slp;
3104 struct targ_info *ti;
3108 struct slccb *cb = slp->sl_Qnexus;
3110 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
3116 slp->sl_error |= (FATALIO | PDMAERR);
3117 slp->sl_scp.scp_datalen = 0;
3118 slp->sl_scp.scp_direction = direction;
3119 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3120 if (ti->ti_ophase != ti->ti_phase)
3125 s = "DATA PHASE: ccb nexus not found";
3127 s = "DATA PHASE: xfer direction mismatch";
3128 SCSI_LOW_INFO(slp, ti, s);
3135 /**************************************************************
3137 **************************************************************/
3138 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
3139 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
3140 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
3141 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
3142 #define MSGIN_DATA_LAST 0x30
3144 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
3145 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
3146 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
3147 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
3149 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
3150 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
3151 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
3152 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
3153 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
3154 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
3155 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
3157 struct scsi_low_msgout_data {
3160 int (*md_msgfunc)(struct scsi_low_softc *);
3161 int (*md_errfunc)(struct scsi_low_softc *, u_int);
3162 #define MSG_RELEASE_ATN 0x0001
3166 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
3167 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
3168 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
3169 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
3170 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
3171 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
3172 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3173 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
3174 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3175 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3176 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3177 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
3178 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3179 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
3180 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
3181 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
3182 /* 15 */{SCSI_LOW_MSG_ALL, 0},
3185 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
3186 static int scsi_low_synch(struct scsi_low_softc *);
3187 static int scsi_low_wide(struct scsi_low_softc *);
3188 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
3189 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
3190 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
3191 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
3192 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
3193 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
3194 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
3195 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
3196 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
3197 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
3198 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
3200 struct scsi_low_msgin_data {
3202 int (*md_msgfunc)(struct scsi_low_softc *);
3205 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
3206 /* 0 */ {1, scsi_low_msginfunc_cc},
3207 /* 1 */ {2, scsi_low_msginfunc_ext},
3208 /* 2 */ {1, scsi_low_msginfunc_sdp},
3209 /* 3 */ {1, scsi_low_msginfunc_rp},
3210 /* 4 */ {1, scsi_low_msginfunc_disc},
3211 /* 5 */ {1, scsi_low_msginfunc_rejop},
3212 /* 6 */ {1, scsi_low_msginfunc_rejop},
3213 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
3214 /* 8 */ {1, scsi_low_msginfunc_noop},
3215 /* 9 */ {1, scsi_low_msginfunc_parity},
3216 /* a */ {1, scsi_low_msginfunc_lcc},
3217 /* b */ {1, scsi_low_msginfunc_lcc},
3218 /* c */ {1, scsi_low_msginfunc_rejop},
3219 /* d */ {2, scsi_low_msginfunc_rejop},
3220 /* e */ {1, scsi_low_msginfunc_rejop},
3221 /* f */ {1, scsi_low_msginfunc_rejop},
3222 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
3223 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
3224 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
3225 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
3226 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
3227 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
3228 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
3229 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
3230 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
3231 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
3232 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
3233 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
3234 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
3235 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
3236 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
3237 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
3238 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
3239 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
3240 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
3241 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
3242 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
3243 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
3244 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
3245 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
3246 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
3247 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
3248 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
3249 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
3250 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
3251 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
3252 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
3253 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
3254 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
3257 /**************************************************************
3259 **************************************************************/
3261 scsi_low_msgfunc_synch(slp)
3262 struct scsi_low_softc *slp;
3264 struct targ_info *ti = slp->sl_Tnexus;
3265 int ptr = ti->ti_msgoutlen;
3267 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
3268 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
3269 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
3270 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
3271 return MSG_EXTEND_SYNCHLEN + 2;
3275 scsi_low_msgfunc_wide(slp)
3276 struct scsi_low_softc *slp;
3278 struct targ_info *ti = slp->sl_Tnexus;
3279 int ptr = ti->ti_msgoutlen;
3281 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
3282 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
3283 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
3284 return MSG_EXTEND_WIDELEN + 2;
3288 scsi_low_msgfunc_identify(slp)
3289 struct scsi_low_softc *slp;
3291 struct targ_info *ti = slp->sl_Tnexus;
3292 struct lun_info *li = slp->sl_Lnexus;
3293 struct slccb *cb = slp->sl_Qnexus;
3294 int ptr = ti->ti_msgoutlen;
3300 slp->sl_error |= FATALIO;
3301 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3302 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
3306 if (scsi_low_is_disconnect_ok(cb) != 0)
3307 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
3311 if (ti->ti_phase == PH_MSGOUT)
3313 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3314 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
3316 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3320 ti->ti_msgoutstr[ptr + 0] = msg;
3325 scsi_low_msgfunc_abort(slp)
3326 struct scsi_low_softc *slp;
3329 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
3334 scsi_low_msgfunc_qabort(slp)
3335 struct scsi_low_softc *slp;
3338 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
3343 scsi_low_msgfunc_reset(slp)
3344 struct scsi_low_softc *slp;
3347 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
3352 scsi_low_msgfunc_qtag(slp)
3353 struct scsi_low_softc *slp;
3355 struct targ_info *ti = slp->sl_Tnexus;
3356 struct slccb *cb = slp->sl_Qnexus;
3357 int ptr = ti->ti_msgoutlen;
3359 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
3361 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
3366 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
3367 if (ti->ti_phase == PH_MSGOUT)
3369 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3376 * The following functions are called when targets give unexpected
3377 * responces in msgin (after msgout).
3380 scsi_low_errfunc_identify(slp, msgflags)
3381 struct scsi_low_softc *slp;
3385 if (slp->sl_Lnexus != NULL)
3387 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
3388 scsi_low_calcf_lun(slp->sl_Lnexus);
3394 scsi_low_errfunc_synch(slp, msgflags)
3395 struct scsi_low_softc *slp;
3398 struct targ_info *ti = slp->sl_Tnexus;
3400 MSGIN_PERIOD(ti) = 0;
3401 MSGIN_OFFSET(ti) = 0;
3402 scsi_low_synch(slp);
3407 scsi_low_errfunc_wide(slp, msgflags)
3408 struct scsi_low_softc *slp;
3411 struct targ_info *ti = slp->sl_Tnexus;
3413 MSGIN_WIDTHP(ti) = 0;
3419 scsi_low_errfunc_qtag(slp, msgflags)
3420 struct scsi_low_softc *slp;
3424 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
3426 if (slp->sl_Qnexus != NULL)
3428 scsi_low_deactivate_qtag(slp->sl_Qnexus);
3430 if (slp->sl_Lnexus != NULL)
3432 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
3433 scsi_low_calcf_lun(slp->sl_Lnexus);
3435 printf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
3442 scsi_low_msgout(slp, ti, fl)
3443 struct scsi_low_softc *slp;
3444 struct targ_info *ti;
3447 struct scsi_low_msgout_data *mdp;
3450 #ifdef SCSI_LOW_DIAGNOSTIC
3451 if (ti != slp->sl_Tnexus)
3453 scsi_low_print(slp, NULL);
3454 panic("scsi_low_msgout: Target nexus inconsistent");
3456 #endif /* SCSI_LOW_DIAGNOSTIC */
3458 slp->sl_ph_count ++;
3459 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3461 printf("%s: too many phase changes\n", slp->sl_xname);
3462 slp->sl_error |= FATALIO;
3463 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3467 * Scsi phase changes.
3468 * Previously msgs asserted are accepted by our target or
3469 * processed by scsi_low_msgin.
3470 * Thus clear all saved informations.
3472 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
3474 ti->ti_omsgflags = 0;
3475 ti->ti_emsgflags = 0;
3477 else if (slp->sl_atten == 0)
3480 * We did not assert attention, however still our target required
3481 * msgs. Resend previous msgs.
3483 ti->ti_msgflags |= ti->ti_omsgflags;
3484 ti->ti_omsgflags = 0;
3485 #ifdef SCSI_LOW_DIAGNOSTIC
3486 printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
3487 #endif /* SCSI_LOW_DIAGNOSTIC */
3491 * We have no msgs. send MSG_NOOP (OK?)
3493 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3494 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
3499 ti->ti_msgoutlen = 0;
3500 slp->sl_clear_atten = 0;
3501 mdp = &scsi_low_msgout_data[0];
3502 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3504 if ((ti->ti_msgflags & mdp->md_flags) != 0)
3506 ti->ti_omsgflags |= mdp->md_flags;
3507 ti->ti_msgflags &= ~mdp->md_flags;
3508 ti->ti_emsgflags = mdp->md_flags;
3510 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
3511 if (mdp->md_msgfunc != NULL)
3512 len = (*mdp->md_msgfunc) (slp);
3516 #ifdef SCSI_LOW_DIAGNOSTIC
3517 scsi_low_msg_log_write(&ti->ti_log_msgout,
3518 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
3519 #endif /* SCSI_LOW_DIAGNOSTIC */
3521 ti->ti_msgoutlen += len;
3522 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
3524 slp->sl_clear_atten = 1;
3528 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
3529 ti->ti_msgflags == 0)
3532 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3537 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3538 slp->sl_clear_atten = 1;
3540 return ti->ti_msgoutlen;
3543 /**************************************************************
3545 **************************************************************/
3547 scsi_low_msginfunc_noop(slp)
3548 struct scsi_low_softc *slp;
3555 scsi_low_msginfunc_rejop(slp)
3556 struct scsi_low_softc *slp;
3558 struct targ_info *ti = slp->sl_Tnexus;
3559 u_int8_t msg = ti->ti_msgin[0];
3561 printf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
3562 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3567 scsi_low_msginfunc_cc(slp)
3568 struct scsi_low_softc *slp;
3570 struct lun_info *li;
3572 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3574 /* validate status */
3575 if (slp->sl_Qnexus == NULL)
3578 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3579 li = slp->sl_Lnexus;
3580 switch (slp->sl_scp.scp_status)
3583 li->li_maxnqio = li->li_maxnexus;
3588 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3589 scsi_low_reset_nexus_lun(slp, li, 0);
3597 if (li->li_maxnexus >= li->li_nqio)
3598 li->li_maxnexus = li->li_nqio - 1;
3599 li->li_maxnqio = li->li_maxnexus;
3604 slp->sl_error |= MSGERR;
3614 scsi_low_msginfunc_lcc(slp)
3615 struct scsi_low_softc *slp;
3617 struct targ_info *ti;
3618 struct lun_info *li;
3619 struct slccb *ncb, *cb;
3621 ti = slp->sl_Tnexus;
3622 li = slp->sl_Lnexus;
3623 if ((cb = slp->sl_Qnexus) == NULL)
3626 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3627 switch (slp->sl_scp.scp_status)
3631 li->li_maxnqio = li->li_maxnexus;
3635 slp->sl_error |= MSGERR;
3639 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3642 cb->ccb_error |= slp->sl_error;
3643 if (cb->ccb_error != 0)
3646 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3647 ncb = TAILQ_NEXT(ncb, ccb_chain))
3650 goto cmd_link_start;
3655 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3656 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3660 ncb->ccb_flags &= ~CCB_STARTQ;
3661 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3663 scsi_low_dealloc_qtag(ncb);
3664 ncb->ccb_tag = cb->ccb_tag;
3665 ncb->ccb_otag = cb->ccb_otag;
3666 cb->ccb_tag = SCSI_LOW_UNKTAG;
3667 cb->ccb_otag = SCSI_LOW_UNKTAG;
3668 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3669 panic("%s: linked ccb retried", slp->sl_xname);
3671 slp->sl_Qnexus = ncb;
3672 slp->sl_ph_count = 0;
3675 ncb->ccb_datalen = -1;
3676 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3677 ncb->ccb_flags &= ~CCB_INTERNAL;
3679 scsi_low_init_msgsys(slp, ti);
3681 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3683 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3684 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3685 ncb->ccb_tc = ncb->ccb_tcmax;
3687 /* setup saved scsi data pointer */
3688 ncb->ccb_sscp = ncb->ccb_scp;
3689 slp->sl_scp = ncb->ccb_sscp;
3690 slp->sl_error = ncb->ccb_error;
3692 #ifdef SCSI_LOW_DIAGNOSTIC
3693 scsi_low_msg_log_init(&ti->ti_log_msgin);
3694 scsi_low_msg_log_init(&ti->ti_log_msgout);
3695 #endif /* SCSI_LOW_DIAGNOSTIC */
3700 scsi_low_msginfunc_disc(slp)
3701 struct scsi_low_softc *slp;
3704 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3709 scsi_low_msginfunc_sdp(slp)
3710 struct scsi_low_softc *slp;
3712 struct slccb *cb = slp->sl_Qnexus;
3716 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3717 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3720 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3725 scsi_low_msginfunc_rp(slp)
3726 struct scsi_low_softc *slp;
3729 if (slp->sl_Qnexus != NULL)
3730 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3732 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3738 struct scsi_low_softc *slp;
3740 struct targ_info *ti = slp->sl_Tnexus;
3741 u_int period = 0, offset = 0, speed;
3745 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3746 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3747 MSGIN_OFFSET(ti) == 0)
3749 if ((offset = MSGIN_OFFSET(ti)) != 0)
3750 period = MSGIN_PERIOD(ti);
3751 s = offset ? "synchronous" : "async";
3756 * Target seems to be brain damaged.
3757 * Force async transfer.
3759 ti->ti_maxsynch.period = 0;
3760 ti->ti_maxsynch.offset = 0;
3761 printf("%s: target brain damaged. async transfer\n",
3766 ti->ti_maxsynch.period = period;
3767 ti->ti_maxsynch.offset = offset;
3769 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3773 * Current period and offset are not acceptable
3775 * The adapter changes max synch and max offset.
3777 printf("%s: synch neg failed. retry synch msg neg ...\n",
3782 ti->ti_osynch = ti->ti_maxsynch;
3785 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3789 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3791 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3792 struct slccb *cb = slp->sl_Qnexus;
3794 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3796 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3798 printf("%s(%d:*): <%s> offset %d period %dns ",
3799 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3803 speed = 1000 * 10 / (period * 4);
3804 printf("%d.%d M/s", speed / 10, speed % 10);
3813 struct scsi_low_softc *slp;
3815 struct targ_info *ti = slp->sl_Tnexus;
3818 ti->ti_width = MSGIN_WIDTHP(ti);
3819 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3823 * Current width is not acceptable for our adapter.
3824 * The adapter changes max width.
3826 printf("%s: wide neg failed. retry wide msg neg ...\n",
3831 ti->ti_owidth = ti->ti_width;
3832 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3834 ti->ti_setup_msg_done |=
3835 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3839 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3841 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3842 struct slccb *cb = slp->sl_Qnexus;
3844 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3846 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3848 printf("%s(%d:*): transfer width %d bits\n",
3849 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3855 scsi_low_msginfunc_simple_qtag(slp)
3856 struct scsi_low_softc *slp;
3858 struct targ_info *ti = slp->sl_Tnexus;
3859 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3861 if (slp->sl_Qnexus != NULL)
3863 if (slp->sl_Qnexus->ccb_tag != etag)
3865 slp->sl_error |= FATALIO;
3866 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3867 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3870 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3872 #ifdef SCSI_LOW_DEBUG
3873 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3875 #endif /* SCSI_LOW_DEBUG */
3877 slp->sl_error |= FATALIO;
3878 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3879 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3885 scsi_low_msginfunc_i_wide_residue(slp)
3886 struct scsi_low_softc *slp;
3888 struct targ_info *ti = slp->sl_Tnexus;
3889 struct slccb *cb = slp->sl_Qnexus;
3890 int res = (int) ti->ti_msgin[1];
3892 if (cb == NULL || res <= 0 ||
3893 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3894 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3897 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3900 slp->sl_scp.scp_datalen += res;
3901 slp->sl_scp.scp_data -= res;
3902 scsi_low_data_finish(slp);
3907 scsi_low_msginfunc_ext(slp)
3908 struct scsi_low_softc *slp;
3910 struct slccb *cb = slp->sl_Qnexus;
3911 struct lun_info *li = slp->sl_Lnexus;
3912 struct targ_info *ti = slp->sl_Tnexus;
3916 if (ti->ti_msginptr == 2)
3918 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3922 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3924 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3928 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3929 count = (int) htonl((long) (*ptr));
3930 if(slp->sl_scp.scp_datalen - count < 0 ||
3931 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3934 slp->sl_scp.scp_datalen -= count;
3935 slp->sl_scp.scp_data += count;
3938 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3942 retry = scsi_low_synch(slp);
3943 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3944 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3946 #ifdef SCSI_LOW_DEBUG
3947 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3949 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3951 #endif /* SCSI_LOW_DEBUG */
3954 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3958 retry = scsi_low_wide(slp);
3959 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3960 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3968 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3973 scsi_low_msginfunc_parity(slp)
3974 struct scsi_low_softc *slp;
3976 struct targ_info *ti = slp->sl_Tnexus;
3978 /* only I -> T, invalid! */
3979 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3984 scsi_low_msginfunc_msg_reject(slp)
3985 struct scsi_low_softc *slp;
3987 struct targ_info *ti = slp->sl_Tnexus;
3988 struct scsi_low_msgout_data *mdp;
3991 if (ti->ti_emsgflags != 0)
3993 printf("%s: msg flags [0x%x] rejected\n",
3994 slp->sl_xname, ti->ti_emsgflags);
3995 msgflags = SCSI_LOW_MSG_REJECT;
3996 mdp = &scsi_low_msgout_data[0];
3997 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3999 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
4001 ti->ti_emsgflags &= ~mdp->md_flags;
4002 if (mdp->md_errfunc != NULL)
4003 (*mdp->md_errfunc) (slp, msgflags);
4011 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
4012 slp->sl_error |= MSGERR;
4018 scsi_low_msgin(slp, ti, c)
4019 struct scsi_low_softc *slp;
4020 struct targ_info *ti;
4023 struct scsi_low_msgin_data *sdp;
4024 struct lun_info *li;
4027 #ifdef SCSI_LOW_DIAGNOSTIC
4028 if (ti != slp->sl_Tnexus)
4030 scsi_low_print(slp, NULL);
4031 panic("scsi_low_msgin: Target nexus inconsistent");
4033 #endif /* SCSI_LOW_DIAGNOSTIC */
4036 * Phase changes, clear the pointer.
4038 if (ti->ti_ophase != ti->ti_phase)
4041 ti->ti_msgin_parity_error = 0;
4043 slp->sl_ph_count ++;
4044 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
4046 printf("%s: too many phase changes\n", slp->sl_xname);
4047 slp->sl_error |= FATALIO;
4048 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4053 * Store a current messages byte into buffer and
4054 * wait for the completion of the current msg.
4056 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
4057 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
4059 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
4060 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4064 * Check parity errors.
4066 if ((c & SCSI_LOW_DATA_PE) != 0)
4068 ti->ti_msgin_parity_error ++;
4069 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
4073 if (ti->ti_msgin_parity_error != 0)
4077 * Calculate messages length.
4079 msg = ti->ti_msgin[0];
4080 if (msg < MSGIN_DATA_LAST)
4081 sdp = &scsi_low_msgin_data[msg];
4083 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
4085 if (ti->ti_msginlen == 0)
4087 ti->ti_msginlen = sdp->md_len;
4093 if (ti->ti_msginptr < ti->ti_msginlen)
4099 if ((msg & MSG_IDENTIFY) == 0)
4101 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
4106 li = slp->sl_Lnexus;
4109 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
4112 slp->sl_Lnexus = li;
4113 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
4117 if (MSGCMD_LUN(msg) != li->li_lun)
4121 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
4123 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
4125 #ifdef SCSI_LOW_DEBUG
4126 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
4130 #endif /* SCSI_LOW_DEBUG */
4138 * Msg process completed, reset msgin pointer and assert ATN if desired.
4141 slp->sl_error |= FATALIO;
4142 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4143 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
4146 if (ti->ti_msginptr < ti->ti_msginlen)
4149 #ifdef SCSI_LOW_DIAGNOSTIC
4150 scsi_low_msg_log_write(&ti->ti_log_msgin,
4151 &ti->ti_msgin[0], ti->ti_msginlen);
4152 #endif /* SCSI_LOW_DIAGNOSTIC */
4158 /**********************************************************
4160 **********************************************************/
4162 scsi_low_disconnected(slp, ti)
4163 struct scsi_low_softc *slp;
4164 struct targ_info *ti;
4166 struct slccb *cb = slp->sl_Qnexus;
4168 /* check phase completion */
4169 switch (slp->sl_msgphase)
4172 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4173 scsi_low_msginfunc_cc(slp);
4174 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
4178 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4179 scsi_low_msginfunc_cc(slp);
4180 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
4184 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4185 scsi_low_msginfunc_cc(slp);
4191 struct lun_info *li;
4194 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
4195 cb->ccb_flags |= CCB_DISCQ;
4196 cb->ccb_error |= slp->sl_error;
4202 #ifdef SCSI_LOW_STATICS
4203 scsi_low_statics.nexus_disconnected ++;
4204 #endif /* SCSI_LOW_STATICS */
4206 #ifdef SCSI_LOW_DEBUG
4207 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
4209 printf("## SCSI_LOW_DISCONNECTED ===============\n");
4210 scsi_low_print(slp, NULL);
4212 #endif /* SCSI_LOW_DEBUG */
4216 slp->sl_error |= FATALIO;
4217 if (ti->ti_phase == PH_SELSTART)
4218 slp->sl_error |= SELTIMEOUTIO;
4220 slp->sl_error |= UBFERR;
4229 #ifdef SCSI_LOW_DEBUG
4230 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4232 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
4233 (cb->ccb_msgoutflag != 0 ||
4234 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
4236 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
4239 #endif /* SCSI_LOW_DEBUG */
4241 cb->ccb_error |= slp->sl_error;
4242 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
4244 cb->ccb_flags |= CCB_STARTQ;
4245 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
4250 scsi_low_bus_release(slp, ti);
4251 scsi_low_start(slp);
4255 /**********************************************************
4257 **********************************************************/
4259 scsi_low_alloc_qtag(cb)
4262 struct lun_info *li = cb->li;
4263 scsi_low_tag_t etag;
4265 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
4268 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4269 etag = ffs(li->li_qtagbits);
4273 li->li_qtagbits &= ~(1 << (etag - 1));
4274 cb->ccb_otag = etag;
4277 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4278 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
4279 if (li->li_qtagarray[li->li_qd] == 0)
4282 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
4283 if (li->li_qtagarray[li->li_qd] == 0)
4289 li->li_qtagarray[li->li_qd] ++;
4290 cb->ccb_otag = (li->li_qd ++);
4292 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4296 scsi_low_dealloc_qtag(cb)
4299 struct lun_info *li = cb->li;
4300 scsi_low_tag_t etag;
4302 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
4305 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4306 etag = cb->ccb_otag - 1;
4307 #ifdef SCSI_LOW_DIAGNOSTIC
4308 if (etag >= sizeof(li->li_qtagbits) * NBBY)
4309 panic("scsi_low_dealloc_tag: illegal tag");
4310 #endif /* SCSI_LOW_DIAGNOSTIC */
4311 li->li_qtagbits |= (1 << etag);
4313 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4314 etag = cb->ccb_otag;
4315 #ifdef SCSI_LOW_DIAGNOSTIC
4316 if (etag >= SCSI_LOW_MAXNEXUS)
4317 panic("scsi_low_dealloc_tag: illegal tag");
4318 #endif /* SCSI_LOW_DIAGNOSTIC */
4319 li->li_qtagarray[etag] --;
4320 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4322 cb->ccb_otag = SCSI_LOW_UNKTAG;
4326 static struct slccb *
4327 scsi_low_revoke_ccb(slp, cb, fdone)
4328 struct scsi_low_softc *slp;
4332 struct targ_info *ti = cb->ti;
4333 struct lun_info *li = cb->li;
4335 #ifdef SCSI_LOW_DIAGNOSTIC
4336 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
4337 (CCB_STARTQ | CCB_DISCQ))
4339 panic("%s: ccb in both queue", slp->sl_xname);
4341 #endif /* SCSI_LOW_DIAGNOSTIC */
4343 if ((cb->ccb_flags & CCB_STARTQ) != 0)
4345 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
4348 if ((cb->ccb_flags & CCB_DISCQ) != 0)
4350 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
4356 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
4357 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
4360 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
4361 (cb->ccb_flags & CCB_NORETRY) != 0))
4363 cb->ccb_error |= FATALIO;
4364 cb->ccb_flags &= ~CCB_AUTOSENSE;
4365 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
4366 panic("%s: done ccb retried", slp->sl_xname);
4371 cb->ccb_error |= PENDINGIO;
4372 scsi_low_deactivate_qtag(cb);
4373 scsi_low_ccb_message_retry(cb);
4374 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
4380 scsi_low_reset_nexus_lun(slp, li, fdone)
4381 struct scsi_low_softc *slp;
4382 struct lun_info *li;
4385 struct slccb *cb, *ncb, *ecb;
4391 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
4393 ncb = TAILQ_NEXT(cb, ccb_chain);
4394 cb = scsi_low_revoke_ccb(slp, cb, fdone);
4398 * presumely keep ordering of io
4400 cb->ccb_flags |= CCB_STARTQ;
4403 TAILQ_INSERT_HEAD(&slp->sl_start,\
4408 TAILQ_INSERT_AFTER(&slp->sl_start,\
4409 ecb, cb, ccb_chain);
4416 /**************************************************************
4418 **************************************************************/
4420 scsi_low_calcf_lun(li)
4421 struct lun_info *li;
4423 struct targ_info *ti = li->li_ti;
4424 struct scsi_low_softc *slp = ti->ti_sc;
4425 u_int cfgflags, diskflags;
4427 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
4428 cfgflags = li->li_cfgflags;
4432 diskflags = li->li_diskflags & li->li_quirks;
4435 li->li_flags &= ~SCSI_LOW_DISC;
4436 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
4437 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
4438 (cfgflags & SCSI_LOW_DISC) != 0)
4439 li->li_flags |= SCSI_LOW_DISC;
4442 li->li_flags |= SCSI_LOW_NOPARITY;
4443 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
4444 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
4445 (cfgflags & SCSI_LOW_NOPARITY) == 0)
4446 li->li_flags &= ~SCSI_LOW_NOPARITY;
4449 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
4450 (cfgflags & SCSI_LOW_QTAG) != 0 &&
4451 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
4453 li->li_flags |= SCSI_LOW_QTAG;
4454 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
4455 li->li_maxnqio = li->li_maxnexus;
4459 li->li_flags &= ~SCSI_LOW_QTAG;
4460 li->li_maxnexus = 0;
4461 li->li_maxnqio = li->li_maxnexus;
4465 li->li_flags &= ~SCSI_LOW_LINK;
4466 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
4467 (diskflags & SCSI_LOW_DISK_LINK) != 0)
4468 li->li_flags |= SCSI_LOW_LINK;
4470 /* compatible flags */
4471 li->li_flags &= ~SCSI_LOW_SYNC;
4472 if (ti->ti_maxsynch.offset > 0)
4473 li->li_flags |= SCSI_LOW_SYNC;
4475 #ifdef SCSI_LOW_DEBUG
4476 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4478 scsi_low_calcf_show(li);
4480 #endif /* SCSI_LOW_DEBUG */
4484 scsi_low_calcf_target(ti)
4485 struct targ_info *ti;
4487 struct scsi_low_softc *slp = ti->ti_sc;
4488 u_int offset, period, diskflags;
4490 diskflags = ti->ti_diskflags & ti->ti_quirks;
4493 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
4494 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
4496 offset = ti->ti_maxsynch.offset;
4497 period = ti->ti_maxsynch.period;
4498 if (offset == 0 || period == 0)
4499 offset = period = 0;
4503 offset = period = 0;
4506 ti->ti_maxsynch.offset = offset;
4507 ti->ti_maxsynch.period = period;
4510 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
4511 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
4512 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
4514 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
4515 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
4516 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
4518 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
4520 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
4521 ti->ti_maxsynch.period != ti->ti_osynch.period)
4522 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
4523 if (ti->ti_width != ti->ti_owidth)
4524 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
4526 ti->ti_osynch = ti->ti_maxsynch;
4527 ti->ti_owidth = ti->ti_width;
4530 #ifdef SCSI_LOW_DEBUG
4531 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4533 printf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
4534 slp->sl_xname, ti->ti_id,
4535 ti->ti_maxsynch.period * 4,
4536 ti->ti_maxsynch.offset,
4539 #endif /* SCSI_LOW_DEBUG */
4543 scsi_low_calcf_show(li)
4544 struct lun_info *li;
4546 struct targ_info *ti = li->li_ti;
4547 struct scsi_low_softc *slp = ti->ti_sc;
4549 printf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4550 slp->sl_xname, ti->ti_id, li->li_lun,
4551 ti->ti_maxsynch.period * 4,
4552 ti->ti_maxsynch.offset,
4554 li->li_flags, SCSI_LOW_BITS);
4557 #ifdef SCSI_LOW_START_UP_CHECK
4558 /**************************************************************
4559 * scsi world start up
4560 **************************************************************/
4561 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
4564 scsi_low_start_up(slp)
4565 struct scsi_low_softc *slp;
4567 struct targ_info *ti;
4568 struct lun_info *li;
4572 printf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
4574 for (target = 0; target < slp->sl_ntargs; target ++)
4576 if (target == slp->sl_hostid)
4578 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4580 printf("%s: scsi_low: target %d (host card)\n",
4581 slp->sl_xname, target);
4586 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4588 printf("%s: scsi_low: target %d lun ",
4589 slp->sl_xname, target);
4592 ti = slp->sl_ti[target];
4593 for (lun = 0; lun < slp->sl_nluns; lun ++)
4595 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4601 li = scsi_low_alloc_li(ti, lun, 1);
4603 scsi_low_enqueue(slp, ti, li, cb,
4604 CCB_AUTOSENSE | CCB_POLLED, 0);
4606 scsi_low_poll(slp, cb);
4608 if (li->li_state != SCSI_LOW_LUN_OK)
4611 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4617 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4626 scsi_low_poll(slp, cb)
4627 struct scsi_low_softc *slp;
4633 while (slp->sl_nio > 0)
4635 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4637 (*slp->sl_funcs->scsi_low_poll) (slp);
4638 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4642 scsi_low_timeout_check(slp);
4647 #endif /* SCSI_LOW_START_UP_CHECK */
4649 /**********************************************************
4651 **********************************************************/
4652 #ifdef SCSI_LOW_DEBUG
4654 scsi_low_test_abort(slp, ti, li)
4655 struct scsi_low_softc *slp;
4656 struct targ_info *ti;
4657 struct lun_info *li;
4661 if (li->li_disc > 1)
4663 acb = TAILQ_FIRST(&li->li_discq);
4664 if (scsi_low_abort_ccb(slp, acb) == 0)
4666 printf("%s: aborting ccb(0x%lx) start\n",
4667 slp->sl_xname, (u_long) acb);
4673 scsi_low_test_atten(slp, ti, msg)
4674 struct scsi_low_softc *slp;
4675 struct targ_info *ti;
4679 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4680 scsi_low_assert_msg(slp, ti, msg, 0);
4682 printf("%s: atten check OK\n", slp->sl_xname);
4686 scsi_low_test_cmdlnk(slp, cb)
4687 struct scsi_low_softc *slp;
4690 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4692 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4695 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4696 slp->sl_scp.scp_cmdlen);
4697 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4698 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4700 #endif /* SCSI_LOW_DEBUG */
4703 scsi_low_info(slp, ti, s)
4704 struct scsi_low_softc *slp;
4705 struct targ_info *ti;
4710 slp = LIST_FIRST(&sl_tab);
4714 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4717 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4718 ti = TAILQ_NEXT(ti, ti_chain))
4720 scsi_low_print(slp, ti);
4725 scsi_low_print(slp, ti);
4729 static u_char *phase[] =
4731 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4732 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4736 scsi_low_print(slp, ti)
4737 struct scsi_low_softc *slp;
4738 struct targ_info *ti;
4740 struct lun_info *li;
4744 if (ti == NULL || ti == slp->sl_Tnexus)
4746 ti = slp->sl_Tnexus;
4747 li = slp->sl_Lnexus;
4748 cb = slp->sl_Qnexus;
4752 li = LIST_FIRST(&ti->ti_litab);
4753 cb = TAILQ_FIRST(&li->li_discq);
4757 printf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4758 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4764 u_int flags = 0, maxnqio = 0, nqio = 0;
4765 int lun = CAM_LUN_WILDCARD;
4770 flags = li->li_flags;
4771 maxnqio = li->li_maxnqio;
4775 printf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4777 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4778 phase[(int) ti->ti_phase], ti->ti_disc,
4783 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4784 (u_int) cb->ccb_scp.scp_cmd[0],
4785 cb->ccb_scp.scp_cmdlen,
4787 cb->ccb_scp.scp_datalen,
4788 (u_int) cb->ccb_sscp.scp_status,
4789 cb->ccb_error, SCSI_LOW_ERRORBITS);
4792 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4793 (u_int) (ti->ti_msginptr),
4794 (u_int) (ti->ti_msgin[0]),
4795 (u_int) (ti->ti_msgin[1]),
4796 (u_int) (ti->ti_msgin[2]),
4797 (u_int) (ti->ti_msgin[3]),
4798 (u_int) (ti->ti_msgin[4]),
4801 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4802 (u_int) ti->ti_msgflags,
4803 (u_int) (ti->ti_msgoutstr[0]),
4804 (u_int) (ti->ti_msgoutstr[1]),
4805 (u_int) (ti->ti_msgoutstr[2]),
4806 (u_int) (ti->ti_msgoutstr[3]),
4807 (u_int) (ti->ti_msgoutstr[4]),
4809 flags, SCSI_LOW_BITS);
4811 #ifdef SCSI_LOW_DIAGNOSTIC
4812 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4813 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4814 #endif /* SCSI_LOW_DIAGNOSTIC */
4818 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4819 (u_long) sp->scp_data,
4821 (u_int) sp->scp_status,
4822 slp->sl_error, SCSI_LOW_ERRORBITS);