1 /* $NecBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $ */
7 #define SCSI_LOW_STATICS
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
12 /* #define SCSI_LOW_INFO_DETAIL */
14 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
15 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
18 #define SCSI_LOW_TARGET_OPEN
19 #endif /* __NetBSD__ */
22 #define SCSI_LOW_FLAGS_QUIRKS_OK
23 #endif /* __FreeBSD__ */
26 * [NetBSD for NEC PC-98 series]
27 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
28 * NetBSD/pc98 porting staff. All rights reserved.
29 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
30 * Naofumi HONDA. All rights reserved.
32 * [Ported for FreeBSD CAM]
33 * Copyright (c) 2000, 2001
34 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. The name of the author may not be used to endorse or promote products
46 * derived from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
52 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
57 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
61 /* <On the nexus establishment>
62 * When our host is reselected,
63 * nexus establish processes are little complicated.
64 * Normal steps are followings:
65 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
66 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
67 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
76 #if __FreeBSD_version >= 500001
79 #include <machine/clock.h>
81 #endif /* __FreeBSD__ */
84 #include <sys/queue.h>
85 #include <sys/malloc.h>
86 #include <sys/errno.h>
89 #include <sys/device.h>
92 #include <machine/bus.h>
93 #include <machine/intr.h>
94 #include <machine/dvcfg.h>
98 #include <dev/scsipi/scsipi_all.h>
99 #include <dev/scsipi/scsipiconf.h>
100 #include <dev/scsipi/scsipi_disk.h>
101 #include <dev/scsipi/scsi_all.h>
102 #include <dev/scsipi/scsiconf.h>
103 #include <sys/scsiio.h>
105 #include <i386/Cbus/dev/scsi_low.h>
106 #endif /* __NetBSD__ */
110 #include <cam/cam_ccb.h>
111 #include <cam/cam_sim.h>
112 #include <cam/cam_debug.h>
113 #include <cam/cam_periph.h>
114 #include <cam/cam_xpt_periph.h>
116 #include <cam/scsi/scsi_all.h>
117 #include <cam/scsi/scsi_message.h>
119 #include <cam/scsi/scsi_low.h>
121 #include <sys/cons.h>
122 #endif /* __FreeBSD__ */
124 /**************************************************************
126 **************************************************************/
127 #define SCSI_LOW_POLL_HZ 1000
129 /* functions return values */
130 #define SCSI_LOW_START_NO_QTAG 0
131 #define SCSI_LOW_START_QTAG 1
133 #define SCSI_LOW_DONE_COMPLETE 0
134 #define SCSI_LOW_DONE_RETRY 1
136 /* internal disk flags */
137 #define SCSI_LOW_DISK_DISC 0x00000001
138 #define SCSI_LOW_DISK_QTAG 0x00000002
139 #define SCSI_LOW_DISK_LINK 0x00000004
140 #define SCSI_LOW_DISK_PARITY 0x00000008
141 #define SCSI_LOW_DISK_SYNC 0x00010000
142 #define SCSI_LOW_DISK_WIDE_16 0x00020000
143 #define SCSI_LOW_DISK_WIDE_32 0x00040000
144 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
145 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
146 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
148 MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
150 /**************************************************************
152 **************************************************************/
153 /* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
154 static void scsi_low_engage(void *);
155 static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
156 static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
157 static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
158 static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
159 static void scsi_low_twiddle_wait(void);
160 static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
161 static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
162 static void scsi_low_calcf_lun(struct lun_info *);
163 static void scsi_low_calcf_target(struct targ_info *);
164 static void scsi_low_calcf_show(struct lun_info *);
165 static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
166 static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
167 static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
168 static int scsi_low_init(struct scsi_low_softc *, u_int);
169 static void scsi_low_start(struct scsi_low_softc *);
170 static void scsi_low_free_ti(struct scsi_low_softc *);
172 static int scsi_low_alloc_qtag(struct slccb *);
173 static int scsi_low_dealloc_qtag(struct slccb *);
174 static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
175 static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
176 static void scsi_low_unit_ready_cmd(struct slccb *);
177 static void scsi_low_timeout(void *);
178 static int scsi_low_timeout_check(struct scsi_low_softc *);
179 #ifdef SCSI_LOW_START_UP_CHECK
180 static int scsi_low_start_up(struct scsi_low_softc *);
181 #endif /* SCSI_LOW_START_UP_CHECK */
182 static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
183 static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);
185 int scsi_low_version_major = 2;
186 int scsi_low_version_minor = 17;
188 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
190 /**************************************************************
191 * Debug, Run test and Statics
192 **************************************************************/
193 #ifdef SCSI_LOW_INFO_DETAIL
194 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
195 #else /* !SCSI_LOW_INFO_DETAIL */
196 #define SCSI_LOW_INFO(slp, ti, s) printf("%s: %s\n", (slp)->sl_xname, (s))
197 #endif /* !SCSI_LOW_INFO_DETAIL */
199 #ifdef SCSI_LOW_STATICS
200 static struct scsi_low_statics {
203 int nexus_disconnected;
204 int nexus_reselected;
207 #endif /* SCSI_LOW_STATICS */
209 #ifdef SCSI_LOW_DEBUG
210 #define SCSI_LOW_DEBUG_DONE 0x00001
211 #define SCSI_LOW_DEBUG_DISC 0x00002
212 #define SCSI_LOW_DEBUG_SENSE 0x00004
213 #define SCSI_LOW_DEBUG_CALCF 0x00008
214 #define SCSI_LOW_DEBUG_ACTION 0x10000
215 int scsi_low_debug = 0;
217 #define SCSI_LOW_MAX_ATTEN_CHECK 32
218 #define SCSI_LOW_ATTEN_CHECK 0x0001
219 #define SCSI_LOW_CMDLNK_CHECK 0x0002
220 #define SCSI_LOW_ABORT_CHECK 0x0004
221 #define SCSI_LOW_NEXUS_CHECK 0x0008
222 int scsi_low_test = 0;
223 int scsi_low_test_id = 0;
225 static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
226 static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
227 static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
228 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
229 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
230 #define SCSI_LOW_DEBUG_GO(fl, id) \
231 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
232 #endif /* SCSI_LOW_DEBUG */
234 /**************************************************************
236 **************************************************************/
237 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
238 GENERIC_CCB(scsi_low, slccb, ccb_chain)
240 /**************************************************************
242 **************************************************************/
243 #define SCSI_LOW_INLINE static __inline
244 SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
245 SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
246 SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
247 SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
248 SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
249 SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
250 SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);
253 scsi_low_activate_qtag(cb)
256 struct lun_info *li = cb->li;
258 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
262 cb->ccb_tag = cb->ccb_otag;
266 scsi_low_deactivate_qtag(cb)
269 struct lun_info *li = cb->li;
271 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
275 cb->ccb_tag = SCSI_LOW_UNKTAG;
279 scsi_low_ccb_message_exec(slp, cb)
280 struct scsi_low_softc *slp;
284 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
285 cb->ccb_msgoutflag = 0;
289 scsi_low_ccb_message_assert(cb, msg)
294 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
298 scsi_low_ccb_message_retry(cb)
301 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
305 scsi_low_ccb_message_clear(cb)
308 cb->ccb_msgoutflag = 0;
312 scsi_low_init_msgsys(slp, ti)
313 struct scsi_low_softc *slp;
314 struct targ_info *ti;
318 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
319 SCSI_LOW_DEASSERT_ATN(slp);
320 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
323 /*=============================================================
324 * START OF OS switch (All OS depend fucntions should be here)
325 =============================================================*/
326 /* common os depend utitlities */
327 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
328 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
329 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
331 static u_int8_t scsi_low_cmd_flags[256] = {
332 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
333 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
334 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
335 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
336 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
339 struct scsi_low_error_code {
344 static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
345 static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);
347 static struct slccb *
348 scsi_low_find_ccb(slp, target, lun, osdep)
349 struct scsi_low_softc *slp;
353 struct targ_info *ti;
357 ti = slp->sl_ti[target];
358 li = scsi_low_alloc_li(ti, lun, 0);
362 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
365 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
366 cb = TAILQ_NEXT(cb, ccb_chain))
368 if (cb->osdep == osdep)
372 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL;
373 cb = TAILQ_NEXT(cb, ccb_chain))
375 if (cb->osdep == osdep)
382 scsi_low_translate_error_code(cb, tp)
384 struct scsi_low_error_code *tp;
387 if (cb->ccb_error == 0)
388 return tp->error_code;
390 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
392 return tp->error_code;
395 #ifdef SCSI_LOW_INTERFACE_XS
396 /**************************************************************
397 * SCSI INTERFACE (XS)
398 **************************************************************/
399 #define SCSI_LOW_MINPHYS 0x10000
400 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
401 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
402 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb((flags))
403 #define SCSI_LOW_XS_POLL_HZ 1000
405 static int scsi_low_poll_xs(struct scsi_low_softc *, struct slccb *);
406 static void scsi_low_scsi_minphys_xs(struct buf *);
407 #ifdef SCSI_LOW_TARGET_OPEN
408 static int scsi_low_target_open(struct scsipi_link *, struct cfdata *);
409 #endif /* SCSI_LOW_TARGET_OPEN */
410 static int scsi_low_scsi_cmd_xs(struct scsipi_xfer *);
411 static int scsi_low_enable_xs(void *, int);
412 static int scsi_low_ioctl_xs(struct scsipi_link *, u_long, caddr_t, int, struct proc *);
414 static int scsi_low_attach_xs(struct scsi_low_softc *);
415 static int scsi_low_world_start_xs(struct scsi_low_softc *);
416 static int scsi_low_dettach_xs(struct scsi_low_softc *);
417 static int scsi_low_ccb_setup_xs(struct scsi_low_softc *, struct slccb *);
418 static int scsi_low_done_xs(struct scsi_low_softc *, struct slccb *);
419 static void scsi_low_timeout_xs(struct scsi_low_softc *, int, int);
420 static u_int scsi_low_translate_quirks_xs(u_int);
421 static void scsi_low_setup_quirks_xs(struct targ_info *, struct lun_info *, u_int);
423 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_xs = {
425 scsi_low_world_start_xs,
427 scsi_low_ccb_setup_xs,
432 struct scsipi_device scsi_low_dev = {
433 NULL, /* Use default error handler */
434 NULL, /* have a queue, served by this */
435 NULL, /* have no async handler */
436 NULL, /* Use default 'done' routine */
439 struct scsi_low_error_code scsi_low_error_code_xs[] = {
443 {SELTIMEOUTIO, XS_SELTIMEOUT},
444 {TIMEOUTIO, XS_TIMEOUT},
445 {-1, XS_DRIVER_STUFFUP}
449 scsi_low_ioctl_xs(link, cmd, addr, flag, p)
450 struct scsipi_link *link;
456 struct scsi_low_softc *slp;
457 int s, error = ENOTTY;
459 slp = (struct scsi_low_softc *) link->adapter_softc;
460 if ((slp->sl_flags & HW_INACTIVE) != 0)
463 if (cmd == SCBUSIORESET)
465 s = SCSI_LOW_SPLSCSI();
466 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
470 else if (slp->sl_funcs->scsi_low_ioctl != 0)
472 error = (*slp->sl_funcs->scsi_low_ioctl)
473 (slp, cmd, addr, flag, p);
480 scsi_low_enable_xs(arg, enable)
484 struct scsi_low_softc *slp = arg;
488 if ((slp->sl_flags & HW_INACTIVE) != 0)
493 if ((slp->sl_flags & HW_INACTIVE) != 0 ||
494 (slp->sl_flags & HW_POWERCTRL) == 0)
497 slp->sl_flags |= HW_POWDOWN;
498 if (slp->sl_funcs->scsi_low_power != NULL)
500 (*slp->sl_funcs->scsi_low_power)
501 (slp, SCSI_LOW_POWDOWN);
508 scsi_low_scsi_minphys_xs(bp)
512 if (bp->b_bcount > SCSI_LOW_MINPHYS)
513 bp->b_bcount = SCSI_LOW_MINPHYS;
518 scsi_low_poll_xs(slp, cb)
519 struct scsi_low_softc *slp;
522 struct scsipi_xfer *xs = cb->osdep;
525 cb->ccb_flags |= CCB_NOSDONE;
528 while (slp->sl_nio > 0)
530 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_XS_POLL_HZ);
532 (*slp->sl_funcs->scsi_low_poll) (slp);
534 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
536 cb->ccb_flags |= CCB_NORETRY;
537 cb->ccb_error |= FATALIO;
538 (void) scsi_low_revoke_ccb(slp, cb, 1);
539 printf("%s: hardware inactive in poll mode\n",
543 if ((xs->flags & ITSDONE) != 0)
546 if (tcount ++ < SCSI_LOW_XS_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
550 scsi_low_timeout_check(slp);
553 xs->flags |= ITSDONE;
559 scsi_low_scsi_cmd_xs(xs)
560 struct scsipi_xfer *xs;
562 struct scsipi_link *splp = xs->sc_link;
563 struct scsi_low_softc *slp = splp->adapter_softc;
564 struct targ_info *ti;
567 int s, targ, lun, flags, rv;
569 if ((cb = SCSI_LOW_ALLOC_CCB(xs->flags & SCSI_NOSLEEP)) == NULL)
570 return TRY_AGAIN_LATER;
572 targ = splp->scsipi_scsi.target,
573 lun = splp->scsipi_scsi.lun;
574 ti = slp->sl_ti[targ];
579 if ((xs->flags & SCSI_POLL) == 0)
580 flags = CCB_AUTOSENSE;
582 flags = CCB_AUTOSENSE | CCB_POLLED;
585 s = SCSI_LOW_SPLSCSI();
586 li = scsi_low_alloc_li(ti, lun, 1);
587 if ((u_int) splp->quirks != li->li_sloi.sloi_quirks)
589 scsi_low_setup_quirks_xs(ti, li, (u_int) splp->quirks);
592 if ((xs->flags & SCSI_RESET) != 0)
594 flags |= CCB_NORETRY | CCB_URGENT;
595 scsi_low_enqueue(slp, ti, li, cb, flags, SCSI_LOW_MSG_RESET);
599 if (ti->ti_setup_msg != 0)
601 scsi_low_message_enqueue(slp, ti, li, flags);
605 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
608 #ifdef SCSI_LOW_DEBUG
609 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, ti->ti_id) != 0)
611 scsi_low_test_abort(slp, ti, li);
613 #endif /* SCSI_LOW_DEBUG */
615 if ((cb->ccb_flags & CCB_POLLED) != 0)
617 rv = scsi_low_poll_xs(slp, cb);
621 rv = SUCCESSFULLY_QUEUED;
628 scsi_low_attach_xs(slp)
629 struct scsi_low_softc *slp;
631 struct scsipi_adapter *sap;
632 struct scsipi_link *splp;
634 strncpy(slp->sl_xname, slp->sl_dev.dv_xname, 16);
636 sap = SCSI_LOW_MALLOC(sizeof(*sap));
639 splp = SCSI_LOW_MALLOC(sizeof(*splp));
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 printf("SENSE: [%x][%x][%x][%x][%x]\n",
2581 (u_int) cb->ccb_sense.error_code,
2582 (u_int) cb->ccb_sense.segment,
2583 (u_int) cb->ccb_sense.flags,
2584 (u_int) cb->ccb_sense.add_sense_code,
2585 (u_int) cb->ccb_sense.add_sense_code_qual);
2587 #endif /* SCSI_LOW_DEBUG */
2591 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2594 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2596 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2597 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2599 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2600 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2601 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2603 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2604 scsi_low_calcf_lun(li);
2612 if (li->li_state == SCSI_LOW_LUN_OK)
2614 scsi_low_calcf_target(ti);
2615 scsi_low_calcf_lun(li);
2616 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2617 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2619 scsi_low_calcf_show(li);
2624 return SCSI_LOW_DONE_RETRY;
2628 scsi_low_done(slp, cb)
2629 struct scsi_low_softc *slp;
2634 if (cb->ccb_error == 0)
2636 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2638 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2640 * SCSI-2 draft suggests
2641 * page 0x0a QErr bit determins if
2642 * the target aborts or continues
2643 * the queueing io's after CA state resolved.
2644 * However many targets seem not to support
2645 * the page 0x0a. Thus we should manually clear the
2646 * queuing io's after CA state.
2648 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2651 cb->ccb_flags |= CCB_CLEARQ;
2654 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2656 if ((cb->ccb_flags & CCB_SENSE) != 0)
2657 cb->ccb_error |= (SENSEIO | ABORTIO);
2658 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2660 else switch (cb->ccb_sscp.scp_status)
2666 if (cb->ccb_datalen == 0 ||
2667 cb->ccb_scp.scp_datalen == 0)
2670 if (cb->ccb_scp.scp_cmdlen > 0 &&
2671 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2672 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2675 cb->ccb_error |= PDMAERR;
2680 cb->ccb_error |= (BUSYERR | STATERR);
2684 cb->ccb_error |= (STATERR | ABORTIO);
2689 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2692 cb->ccb_flags |= CCB_SENSE;
2695 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2700 cb->ccb_error |= FATALIO;
2706 if (cb->ccb_flags & CCB_SENSE)
2708 cb->ccb_error |= (SENSEERR | ABORTIO);
2710 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2714 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2716 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2720 /* check a ccb msgout flag */
2721 if (cb->ccb_omsgoutflag != 0)
2723 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2724 SCSI_LOW_MSG_ABORT_QTAG | \
2725 SCSI_LOW_MSG_CLEAR_QTAG | \
2726 SCSI_LOW_MSG_TERMIO)
2728 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2730 cb->ccb_error |= ABORTIO;
2734 /* call OS depend done */
2735 if (cb->osdep != NULL)
2737 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2738 if (rv == EJUSTRETURN)
2741 else if (cb->ccb_error != 0)
2743 if (cb->ccb_rcnt >= slp->sl_max_retry)
2744 cb->ccb_error |= ABORTIO;
2746 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2747 (cb->ccb_error & ABORTIO) == 0)
2751 /* free our target */
2752 #ifdef SCSI_LOW_DEBUG
2753 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2755 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2756 scsi_low_print(slp, NULL);
2758 #endif /* SCSI_LOW_DEBUG */
2760 scsi_low_deactivate_qtag(cb);
2761 scsi_low_dealloc_qtag(cb);
2762 scsi_low_free_ccb(cb);
2764 return SCSI_LOW_DONE_COMPLETE;
2767 #ifdef SCSI_LOW_DEBUG
2768 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2770 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2771 scsi_low_print(slp, NULL);
2773 #endif /* SCSI_LOW_DEBUG */
2776 scsi_low_deactivate_qtag(cb);
2777 scsi_low_ccb_message_retry(cb);
2778 return SCSI_LOW_DONE_RETRY;
2781 /**************************************************************
2783 **************************************************************/
2785 scsi_low_reset_nexus_target(slp, ti, fdone)
2786 struct scsi_low_softc *slp;
2787 struct targ_info *ti;
2790 struct lun_info *li;
2792 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2793 li = LIST_NEXT(li, lun_chain))
2795 scsi_low_reset_nexus_lun(slp, li, fdone);
2796 li->li_state = SCSI_LOW_LUN_SLEEP;
2801 ti->ti_setup_msg = 0;
2802 ti->ti_setup_msg_done = 0;
2804 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2805 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2807 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2808 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2810 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2812 ((*slp->sl_funcs->scsi_low_targ_init)
2813 (slp, ti, SCSI_LOW_INFO_REVOKE));
2815 scsi_low_calcf_target(ti);
2817 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2818 li = LIST_NEXT(li, lun_chain))
2822 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2823 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2825 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2827 ((*slp->sl_funcs->scsi_low_lun_init)
2828 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2830 scsi_low_calcf_lun(li);
2835 scsi_low_reset_nexus(slp, fdone)
2836 struct scsi_low_softc *slp;
2839 struct targ_info *ti;
2840 struct slccb *cb, *topcb;
2842 if ((cb = slp->sl_Qnexus) != NULL)
2844 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2851 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2852 ti = TAILQ_NEXT(ti, ti_chain))
2854 scsi_low_reset_nexus_target(slp, ti, fdone);
2855 scsi_low_bus_release(slp, ti);
2856 scsi_low_init_msgsys(slp, ti);
2861 topcb->ccb_flags |= CCB_STARTQ;
2862 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2866 slp->sl_retry_sel = 0;
2867 slp->sl_flags &= ~HW_PDMASTART;
2872 static char tw_chars[] = "|/-\\";
2873 #define TWIDDLEWAIT 10000
2876 scsi_low_twiddle_wait(void)
2880 cnputc(tw_chars[tw_pos++]);
2881 tw_pos %= (sizeof(tw_chars) - 1);
2882 SCSI_LOW_DELAY(TWIDDLEWAIT);
2886 scsi_low_bus_reset(slp)
2887 struct scsi_low_softc *slp;
2891 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2893 printf("%s: try to reset scsi bus ", slp->sl_xname);
2894 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2895 scsi_low_twiddle_wait();
2901 scsi_low_restart(slp, flags, s)
2902 struct scsi_low_softc *slp;
2909 printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
2911 if ((error = scsi_low_init(slp, flags)) != 0)
2914 scsi_low_start(slp);
2918 /**************************************************************
2919 * disconnect and reselect
2920 **************************************************************/
2921 #define MSGCMD_LUN(msg) (msg & 0x07)
2923 static struct slccb *
2924 scsi_low_establish_ccb(ti, li, tag)
2925 struct targ_info *ti;
2926 struct lun_info *li;
2929 struct scsi_low_softc *slp = ti->ti_sc;
2935 cb = TAILQ_FIRST(&li->li_discq);
2936 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2937 if (cb->ccb_tag == tag)
2942 * establish our ccb nexus
2945 #ifdef SCSI_LOW_DEBUG
2946 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2948 printf("%s: nexus(0x%lx) abort check start\n",
2949 slp->sl_xname, (u_long) cb);
2950 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2951 scsi_low_revoke_ccb(slp, cb, 1);
2955 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2957 if (cb->ccb_omsgoutflag == 0)
2958 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2960 #endif /* SCSI_LOW_DEBUG */
2962 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2963 cb->ccb_flags &= ~CCB_DISCQ;
2964 slp->sl_Qnexus = cb;
2966 slp->sl_scp = cb->ccb_sscp;
2967 slp->sl_error |= cb->ccb_error;
2973 /* inform "ccb nexus established" to the host driver */
2974 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2977 if (cb->ccb_msgoutflag != 0)
2979 scsi_low_ccb_message_exec(slp, cb);
2986 scsi_low_reselected(slp, targ)
2987 struct scsi_low_softc *slp;
2990 struct targ_info *ti;
2995 * Check select vs reselected collision.
2998 if ((cb = slp->sl_selid) != NULL)
3000 scsi_low_arbit_fail(slp, cb);
3001 #ifdef SCSI_LOW_STATICS
3002 scsi_low_statics.nexus_conflict ++;
3003 #endif /* SCSI_LOW_STATICS */
3007 * Check if no current active nexus.
3009 if (slp->sl_Tnexus != NULL)
3016 * Check a valid target id asserted ?
3018 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
3020 s = "scsi id illegal";
3025 * Check the target scsi status.
3027 ti = slp->sl_ti[targ];
3028 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
3030 s = "phase mismatch";
3038 scsi_low_init_msgsys(slp, ti);
3041 * Establish our target nexus
3043 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
3044 slp->sl_Tnexus = ti;
3045 #ifdef SCSI_LOW_STATICS
3046 scsi_low_statics.nexus_reselected ++;
3047 #endif /* SCSI_LOW_STATICS */
3051 printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
3052 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
3053 "reselect: scsi world confused");
3057 /**************************************************************
3058 * cmd out pointer setup
3059 **************************************************************/
3061 scsi_low_cmd(slp, ti)
3062 struct scsi_low_softc *slp;
3063 struct targ_info *ti;
3065 struct slccb *cb = slp->sl_Qnexus;
3067 slp->sl_ph_count ++;
3073 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
3074 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
3075 slp->sl_scp.scp_datalen = 0;
3076 slp->sl_scp.scp_direction = SCSI_LOW_READ;
3077 slp->sl_error |= FATALIO;
3078 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3079 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
3084 #ifdef SCSI_LOW_DEBUG
3085 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
3087 scsi_low_test_cmdlnk(slp, cb);
3089 #endif /* SCSI_LOW_DEBUG */
3094 /**************************************************************
3095 * data out pointer setup
3096 **************************************************************/
3098 scsi_low_data(slp, ti, bp, direction)
3099 struct scsi_low_softc *slp;
3100 struct targ_info *ti;
3104 struct slccb *cb = slp->sl_Qnexus;
3106 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
3112 slp->sl_error |= (FATALIO | PDMAERR);
3113 slp->sl_scp.scp_datalen = 0;
3114 slp->sl_scp.scp_direction = direction;
3115 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3116 if (ti->ti_ophase != ti->ti_phase)
3121 s = "DATA PHASE: ccb nexus not found";
3123 s = "DATA PHASE: xfer direction mismatch";
3124 SCSI_LOW_INFO(slp, ti, s);
3131 /**************************************************************
3133 **************************************************************/
3134 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
3135 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
3136 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
3137 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
3138 #define MSGIN_DATA_LAST 0x30
3140 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
3141 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
3142 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
3143 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
3145 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
3146 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
3147 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
3148 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
3149 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
3150 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
3151 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
3153 struct scsi_low_msgout_data {
3156 int (*md_msgfunc)(struct scsi_low_softc *);
3157 int (*md_errfunc)(struct scsi_low_softc *, u_int);
3158 #define MSG_RELEASE_ATN 0x0001
3162 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
3163 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
3164 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
3165 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
3166 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
3167 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
3168 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3169 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
3170 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3171 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3172 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3173 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
3174 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3175 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
3176 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
3177 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
3178 /* 15 */{SCSI_LOW_MSG_ALL, 0},
3181 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
3182 static int scsi_low_synch(struct scsi_low_softc *);
3183 static int scsi_low_wide(struct scsi_low_softc *);
3184 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
3185 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
3186 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
3187 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
3188 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
3189 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
3190 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
3191 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
3192 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
3193 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
3194 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
3196 struct scsi_low_msgin_data {
3198 int (*md_msgfunc)(struct scsi_low_softc *);
3201 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
3202 /* 0 */ {1, scsi_low_msginfunc_cc},
3203 /* 1 */ {2, scsi_low_msginfunc_ext},
3204 /* 2 */ {1, scsi_low_msginfunc_sdp},
3205 /* 3 */ {1, scsi_low_msginfunc_rp},
3206 /* 4 */ {1, scsi_low_msginfunc_disc},
3207 /* 5 */ {1, scsi_low_msginfunc_rejop},
3208 /* 6 */ {1, scsi_low_msginfunc_rejop},
3209 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
3210 /* 8 */ {1, scsi_low_msginfunc_noop},
3211 /* 9 */ {1, scsi_low_msginfunc_parity},
3212 /* a */ {1, scsi_low_msginfunc_lcc},
3213 /* b */ {1, scsi_low_msginfunc_lcc},
3214 /* c */ {1, scsi_low_msginfunc_rejop},
3215 /* d */ {2, scsi_low_msginfunc_rejop},
3216 /* e */ {1, scsi_low_msginfunc_rejop},
3217 /* f */ {1, scsi_low_msginfunc_rejop},
3218 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
3219 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
3220 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
3221 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
3222 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
3223 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
3224 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
3225 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
3226 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
3227 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
3228 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
3229 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
3230 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
3231 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
3232 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
3233 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
3234 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
3235 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
3236 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
3237 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
3238 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
3239 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
3240 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
3241 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
3242 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
3243 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
3244 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
3245 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
3246 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
3247 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
3248 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
3249 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
3250 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
3253 /**************************************************************
3255 **************************************************************/
3257 scsi_low_msgfunc_synch(slp)
3258 struct scsi_low_softc *slp;
3260 struct targ_info *ti = slp->sl_Tnexus;
3261 int ptr = ti->ti_msgoutlen;
3263 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
3264 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
3265 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
3266 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
3267 return MSG_EXTEND_SYNCHLEN + 2;
3271 scsi_low_msgfunc_wide(slp)
3272 struct scsi_low_softc *slp;
3274 struct targ_info *ti = slp->sl_Tnexus;
3275 int ptr = ti->ti_msgoutlen;
3277 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
3278 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
3279 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
3280 return MSG_EXTEND_WIDELEN + 2;
3284 scsi_low_msgfunc_identify(slp)
3285 struct scsi_low_softc *slp;
3287 struct targ_info *ti = slp->sl_Tnexus;
3288 struct lun_info *li = slp->sl_Lnexus;
3289 struct slccb *cb = slp->sl_Qnexus;
3290 int ptr = ti->ti_msgoutlen;
3296 slp->sl_error |= FATALIO;
3297 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3298 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
3302 if (scsi_low_is_disconnect_ok(cb) != 0)
3303 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
3307 if (ti->ti_phase == PH_MSGOUT)
3309 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3310 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
3312 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3316 ti->ti_msgoutstr[ptr + 0] = msg;
3321 scsi_low_msgfunc_abort(slp)
3322 struct scsi_low_softc *slp;
3325 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
3330 scsi_low_msgfunc_qabort(slp)
3331 struct scsi_low_softc *slp;
3334 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
3339 scsi_low_msgfunc_reset(slp)
3340 struct scsi_low_softc *slp;
3343 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
3348 scsi_low_msgfunc_qtag(slp)
3349 struct scsi_low_softc *slp;
3351 struct targ_info *ti = slp->sl_Tnexus;
3352 struct slccb *cb = slp->sl_Qnexus;
3353 int ptr = ti->ti_msgoutlen;
3355 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
3357 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
3362 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
3363 if (ti->ti_phase == PH_MSGOUT)
3365 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3372 * The following functions are called when targets give unexpected
3373 * responces in msgin (after msgout).
3376 scsi_low_errfunc_identify(slp, msgflags)
3377 struct scsi_low_softc *slp;
3381 if (slp->sl_Lnexus != NULL)
3383 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
3384 scsi_low_calcf_lun(slp->sl_Lnexus);
3390 scsi_low_errfunc_synch(slp, msgflags)
3391 struct scsi_low_softc *slp;
3394 struct targ_info *ti = slp->sl_Tnexus;
3396 MSGIN_PERIOD(ti) = 0;
3397 MSGIN_OFFSET(ti) = 0;
3398 scsi_low_synch(slp);
3403 scsi_low_errfunc_wide(slp, msgflags)
3404 struct scsi_low_softc *slp;
3407 struct targ_info *ti = slp->sl_Tnexus;
3409 MSGIN_WIDTHP(ti) = 0;
3415 scsi_low_errfunc_qtag(slp, msgflags)
3416 struct scsi_low_softc *slp;
3420 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
3422 if (slp->sl_Qnexus != NULL)
3424 scsi_low_deactivate_qtag(slp->sl_Qnexus);
3426 if (slp->sl_Lnexus != NULL)
3428 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
3429 scsi_low_calcf_lun(slp->sl_Lnexus);
3431 printf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
3438 scsi_low_msgout(slp, ti, fl)
3439 struct scsi_low_softc *slp;
3440 struct targ_info *ti;
3443 struct scsi_low_msgout_data *mdp;
3446 #ifdef SCSI_LOW_DIAGNOSTIC
3447 if (ti != slp->sl_Tnexus)
3449 scsi_low_print(slp, NULL);
3450 panic("scsi_low_msgout: Target nexus inconsistent");
3452 #endif /* SCSI_LOW_DIAGNOSTIC */
3454 slp->sl_ph_count ++;
3455 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3457 printf("%s: too many phase changes\n", slp->sl_xname);
3458 slp->sl_error |= FATALIO;
3459 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3463 * Scsi phase changes.
3464 * Previously msgs asserted are accepted by our target or
3465 * processed by scsi_low_msgin.
3466 * Thus clear all saved informations.
3468 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
3470 ti->ti_omsgflags = 0;
3471 ti->ti_emsgflags = 0;
3473 else if (slp->sl_atten == 0)
3476 * We did not assert attention, however still our target required
3477 * msgs. Resend previous msgs.
3479 ti->ti_msgflags |= ti->ti_omsgflags;
3480 ti->ti_omsgflags = 0;
3481 #ifdef SCSI_LOW_DIAGNOSTIC
3482 printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
3483 #endif /* SCSI_LOW_DIAGNOSTIC */
3487 * We have no msgs. send MSG_NOOP (OK?)
3489 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3490 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
3495 ti->ti_msgoutlen = 0;
3496 slp->sl_clear_atten = 0;
3497 mdp = &scsi_low_msgout_data[0];
3498 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3500 if ((ti->ti_msgflags & mdp->md_flags) != 0)
3502 ti->ti_omsgflags |= mdp->md_flags;
3503 ti->ti_msgflags &= ~mdp->md_flags;
3504 ti->ti_emsgflags = mdp->md_flags;
3506 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
3507 if (mdp->md_msgfunc != NULL)
3508 len = (*mdp->md_msgfunc) (slp);
3512 #ifdef SCSI_LOW_DIAGNOSTIC
3513 scsi_low_msg_log_write(&ti->ti_log_msgout,
3514 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
3515 #endif /* SCSI_LOW_DIAGNOSTIC */
3517 ti->ti_msgoutlen += len;
3518 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
3520 slp->sl_clear_atten = 1;
3524 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
3525 ti->ti_msgflags == 0)
3528 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3533 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3534 slp->sl_clear_atten = 1;
3536 return ti->ti_msgoutlen;
3539 /**************************************************************
3541 **************************************************************/
3543 scsi_low_msginfunc_noop(slp)
3544 struct scsi_low_softc *slp;
3551 scsi_low_msginfunc_rejop(slp)
3552 struct scsi_low_softc *slp;
3554 struct targ_info *ti = slp->sl_Tnexus;
3555 u_int8_t msg = ti->ti_msgin[0];
3557 printf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
3558 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3563 scsi_low_msginfunc_cc(slp)
3564 struct scsi_low_softc *slp;
3566 struct lun_info *li;
3568 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3570 /* validate status */
3571 if (slp->sl_Qnexus == NULL)
3574 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3575 li = slp->sl_Lnexus;
3576 switch (slp->sl_scp.scp_status)
3579 li->li_maxnqio = li->li_maxnexus;
3584 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3585 scsi_low_reset_nexus_lun(slp, li, 0);
3593 if (li->li_maxnexus >= li->li_nqio)
3594 li->li_maxnexus = li->li_nqio - 1;
3595 li->li_maxnqio = li->li_maxnexus;
3600 slp->sl_error |= MSGERR;
3610 scsi_low_msginfunc_lcc(slp)
3611 struct scsi_low_softc *slp;
3613 struct targ_info *ti;
3614 struct lun_info *li;
3615 struct slccb *ncb, *cb;
3617 ti = slp->sl_Tnexus;
3618 li = slp->sl_Lnexus;
3619 if ((cb = slp->sl_Qnexus) == NULL)
3622 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3623 switch (slp->sl_scp.scp_status)
3627 li->li_maxnqio = li->li_maxnexus;
3631 slp->sl_error |= MSGERR;
3635 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3638 cb->ccb_error |= slp->sl_error;
3639 if (cb->ccb_error != 0)
3642 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3643 ncb = TAILQ_NEXT(ncb, ccb_chain))
3646 goto cmd_link_start;
3651 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3652 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3656 ncb->ccb_flags &= ~CCB_STARTQ;
3657 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3659 scsi_low_dealloc_qtag(ncb);
3660 ncb->ccb_tag = cb->ccb_tag;
3661 ncb->ccb_otag = cb->ccb_otag;
3662 cb->ccb_tag = SCSI_LOW_UNKTAG;
3663 cb->ccb_otag = SCSI_LOW_UNKTAG;
3664 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3665 panic("%s: linked ccb retried", slp->sl_xname);
3667 slp->sl_Qnexus = ncb;
3668 slp->sl_ph_count = 0;
3671 ncb->ccb_datalen = -1;
3672 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3673 ncb->ccb_flags &= ~CCB_INTERNAL;
3675 scsi_low_init_msgsys(slp, ti);
3677 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3679 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3680 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3681 ncb->ccb_tc = ncb->ccb_tcmax;
3683 /* setup saved scsi data pointer */
3684 ncb->ccb_sscp = ncb->ccb_scp;
3685 slp->sl_scp = ncb->ccb_sscp;
3686 slp->sl_error = ncb->ccb_error;
3688 #ifdef SCSI_LOW_DIAGNOSTIC
3689 scsi_low_msg_log_init(&ti->ti_log_msgin);
3690 scsi_low_msg_log_init(&ti->ti_log_msgout);
3691 #endif /* SCSI_LOW_DIAGNOSTIC */
3696 scsi_low_msginfunc_disc(slp)
3697 struct scsi_low_softc *slp;
3700 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3705 scsi_low_msginfunc_sdp(slp)
3706 struct scsi_low_softc *slp;
3708 struct slccb *cb = slp->sl_Qnexus;
3712 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3713 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3716 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3721 scsi_low_msginfunc_rp(slp)
3722 struct scsi_low_softc *slp;
3725 if (slp->sl_Qnexus != NULL)
3726 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3728 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3734 struct scsi_low_softc *slp;
3736 struct targ_info *ti = slp->sl_Tnexus;
3737 u_int period = 0, offset = 0, speed;
3741 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3742 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3743 MSGIN_OFFSET(ti) == 0)
3745 if ((offset = MSGIN_OFFSET(ti)) != 0)
3746 period = MSGIN_PERIOD(ti);
3747 s = offset ? "synchronous" : "async";
3752 * Target seems to be brain damaged.
3753 * Force async transfer.
3755 ti->ti_maxsynch.period = 0;
3756 ti->ti_maxsynch.offset = 0;
3757 printf("%s: target brain damaged. async transfer\n",
3762 ti->ti_maxsynch.period = period;
3763 ti->ti_maxsynch.offset = offset;
3765 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3769 * Current period and offset are not acceptable
3771 * The adapter changes max synch and max offset.
3773 printf("%s: synch neg failed. retry synch msg neg ...\n",
3778 ti->ti_osynch = ti->ti_maxsynch;
3781 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3785 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3787 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3788 struct slccb *cb = slp->sl_Qnexus;
3790 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3792 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3794 printf("%s(%d:*): <%s> offset %d period %dns ",
3795 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3799 speed = 1000 * 10 / (period * 4);
3800 printf("%d.%d M/s", speed / 10, speed % 10);
3809 struct scsi_low_softc *slp;
3811 struct targ_info *ti = slp->sl_Tnexus;
3814 ti->ti_width = MSGIN_WIDTHP(ti);
3815 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3819 * Current width is not acceptable for our adapter.
3820 * The adapter changes max width.
3822 printf("%s: wide neg failed. retry wide msg neg ...\n",
3827 ti->ti_owidth = ti->ti_width;
3828 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3830 ti->ti_setup_msg_done |=
3831 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3835 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3837 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3838 struct slccb *cb = slp->sl_Qnexus;
3840 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3842 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3844 printf("%s(%d:*): transfer width %d bits\n",
3845 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3851 scsi_low_msginfunc_simple_qtag(slp)
3852 struct scsi_low_softc *slp;
3854 struct targ_info *ti = slp->sl_Tnexus;
3855 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3857 if (slp->sl_Qnexus != NULL)
3859 if (slp->sl_Qnexus->ccb_tag != etag)
3861 slp->sl_error |= FATALIO;
3862 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3863 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3866 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3868 #ifdef SCSI_LOW_DEBUG
3869 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3871 #endif /* SCSI_LOW_DEBUG */
3873 slp->sl_error |= FATALIO;
3874 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3875 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3881 scsi_low_msginfunc_i_wide_residue(slp)
3882 struct scsi_low_softc *slp;
3884 struct targ_info *ti = slp->sl_Tnexus;
3885 struct slccb *cb = slp->sl_Qnexus;
3886 int res = (int) ti->ti_msgin[1];
3888 if (cb == NULL || res <= 0 ||
3889 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3890 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3893 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3896 slp->sl_scp.scp_datalen += res;
3897 slp->sl_scp.scp_data -= res;
3898 scsi_low_data_finish(slp);
3903 scsi_low_msginfunc_ext(slp)
3904 struct scsi_low_softc *slp;
3906 struct slccb *cb = slp->sl_Qnexus;
3907 struct lun_info *li = slp->sl_Lnexus;
3908 struct targ_info *ti = slp->sl_Tnexus;
3912 if (ti->ti_msginptr == 2)
3914 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3918 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3920 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3924 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3925 count = (int) htonl((long) (*ptr));
3926 if(slp->sl_scp.scp_datalen - count < 0 ||
3927 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3930 slp->sl_scp.scp_datalen -= count;
3931 slp->sl_scp.scp_data += count;
3934 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3938 retry = scsi_low_synch(slp);
3939 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3940 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3942 #ifdef SCSI_LOW_DEBUG
3943 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3945 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3947 #endif /* SCSI_LOW_DEBUG */
3950 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3954 retry = scsi_low_wide(slp);
3955 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3956 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3964 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3969 scsi_low_msginfunc_parity(slp)
3970 struct scsi_low_softc *slp;
3972 struct targ_info *ti = slp->sl_Tnexus;
3974 /* only I -> T, invalid! */
3975 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3980 scsi_low_msginfunc_msg_reject(slp)
3981 struct scsi_low_softc *slp;
3983 struct targ_info *ti = slp->sl_Tnexus;
3984 struct scsi_low_msgout_data *mdp;
3987 if (ti->ti_emsgflags != 0)
3989 printf("%s: msg flags [0x%x] rejected\n",
3990 slp->sl_xname, ti->ti_emsgflags);
3991 msgflags = SCSI_LOW_MSG_REJECT;
3992 mdp = &scsi_low_msgout_data[0];
3993 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3995 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3997 ti->ti_emsgflags &= ~mdp->md_flags;
3998 if (mdp->md_errfunc != NULL)
3999 (*mdp->md_errfunc) (slp, msgflags);
4007 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
4008 slp->sl_error |= MSGERR;
4014 scsi_low_msgin(slp, ti, c)
4015 struct scsi_low_softc *slp;
4016 struct targ_info *ti;
4019 struct scsi_low_msgin_data *sdp;
4020 struct lun_info *li;
4023 #ifdef SCSI_LOW_DIAGNOSTIC
4024 if (ti != slp->sl_Tnexus)
4026 scsi_low_print(slp, NULL);
4027 panic("scsi_low_msgin: Target nexus inconsistent");
4029 #endif /* SCSI_LOW_DIAGNOSTIC */
4032 * Phase changes, clear the pointer.
4034 if (ti->ti_ophase != ti->ti_phase)
4037 ti->ti_msgin_parity_error = 0;
4039 slp->sl_ph_count ++;
4040 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
4042 printf("%s: too many phase changes\n", slp->sl_xname);
4043 slp->sl_error |= FATALIO;
4044 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4049 * Store a current messages byte into buffer and
4050 * wait for the completion of the current msg.
4052 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
4053 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
4055 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
4056 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4060 * Check parity errors.
4062 if ((c & SCSI_LOW_DATA_PE) != 0)
4064 ti->ti_msgin_parity_error ++;
4065 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
4069 if (ti->ti_msgin_parity_error != 0)
4073 * Calculate messages length.
4075 msg = ti->ti_msgin[0];
4076 if (msg < MSGIN_DATA_LAST)
4077 sdp = &scsi_low_msgin_data[msg];
4079 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
4081 if (ti->ti_msginlen == 0)
4083 ti->ti_msginlen = sdp->md_len;
4089 if (ti->ti_msginptr < ti->ti_msginlen)
4095 if ((msg & MSG_IDENTIFY) == 0)
4097 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
4102 li = slp->sl_Lnexus;
4105 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
4108 slp->sl_Lnexus = li;
4109 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
4113 if (MSGCMD_LUN(msg) != li->li_lun)
4117 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
4119 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
4121 #ifdef SCSI_LOW_DEBUG
4122 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
4126 #endif /* SCSI_LOW_DEBUG */
4134 * Msg process completed, reset msgin pointer and assert ATN if desired.
4137 slp->sl_error |= FATALIO;
4138 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4139 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
4142 if (ti->ti_msginptr < ti->ti_msginlen)
4145 #ifdef SCSI_LOW_DIAGNOSTIC
4146 scsi_low_msg_log_write(&ti->ti_log_msgin,
4147 &ti->ti_msgin[0], ti->ti_msginlen);
4148 #endif /* SCSI_LOW_DIAGNOSTIC */
4154 /**********************************************************
4156 **********************************************************/
4158 scsi_low_disconnected(slp, ti)
4159 struct scsi_low_softc *slp;
4160 struct targ_info *ti;
4162 struct slccb *cb = slp->sl_Qnexus;
4164 /* check phase completion */
4165 switch (slp->sl_msgphase)
4168 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4169 scsi_low_msginfunc_cc(slp);
4170 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
4174 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4175 scsi_low_msginfunc_cc(slp);
4176 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
4180 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4181 scsi_low_msginfunc_cc(slp);
4187 struct lun_info *li;
4190 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
4191 cb->ccb_flags |= CCB_DISCQ;
4192 cb->ccb_error |= slp->sl_error;
4198 #ifdef SCSI_LOW_STATICS
4199 scsi_low_statics.nexus_disconnected ++;
4200 #endif /* SCSI_LOW_STATICS */
4202 #ifdef SCSI_LOW_DEBUG
4203 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
4205 printf("## SCSI_LOW_DISCONNECTED ===============\n");
4206 scsi_low_print(slp, NULL);
4208 #endif /* SCSI_LOW_DEBUG */
4212 slp->sl_error |= FATALIO;
4213 if (ti->ti_phase == PH_SELSTART)
4214 slp->sl_error |= SELTIMEOUTIO;
4216 slp->sl_error |= UBFERR;
4225 #ifdef SCSI_LOW_DEBUG
4226 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4228 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
4229 (cb->ccb_msgoutflag != 0 ||
4230 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
4232 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
4235 #endif /* SCSI_LOW_DEBUG */
4237 cb->ccb_error |= slp->sl_error;
4238 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
4240 cb->ccb_flags |= CCB_STARTQ;
4241 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
4246 scsi_low_bus_release(slp, ti);
4247 scsi_low_start(slp);
4251 /**********************************************************
4253 **********************************************************/
4255 scsi_low_alloc_qtag(cb)
4258 struct lun_info *li = cb->li;
4259 scsi_low_tag_t etag;
4261 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
4264 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4265 etag = ffs(li->li_qtagbits);
4269 li->li_qtagbits &= ~(1 << (etag - 1));
4270 cb->ccb_otag = etag;
4273 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4274 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
4275 if (li->li_qtagarray[li->li_qd] == 0)
4278 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
4279 if (li->li_qtagarray[li->li_qd] == 0)
4285 li->li_qtagarray[li->li_qd] ++;
4286 cb->ccb_otag = (li->li_qd ++);
4288 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4292 scsi_low_dealloc_qtag(cb)
4295 struct lun_info *li = cb->li;
4296 scsi_low_tag_t etag;
4298 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
4301 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4302 etag = cb->ccb_otag - 1;
4303 #ifdef SCSI_LOW_DIAGNOSTIC
4304 if (etag >= sizeof(li->li_qtagbits) * NBBY)
4305 panic("scsi_low_dealloc_tag: illegal tag");
4306 #endif /* SCSI_LOW_DIAGNOSTIC */
4307 li->li_qtagbits |= (1 << etag);
4309 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4310 etag = cb->ccb_otag;
4311 #ifdef SCSI_LOW_DIAGNOSTIC
4312 if (etag >= SCSI_LOW_MAXNEXUS)
4313 panic("scsi_low_dealloc_tag: illegal tag");
4314 #endif /* SCSI_LOW_DIAGNOSTIC */
4315 li->li_qtagarray[etag] --;
4316 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4318 cb->ccb_otag = SCSI_LOW_UNKTAG;
4322 static struct slccb *
4323 scsi_low_revoke_ccb(slp, cb, fdone)
4324 struct scsi_low_softc *slp;
4328 struct targ_info *ti = cb->ti;
4329 struct lun_info *li = cb->li;
4331 #ifdef SCSI_LOW_DIAGNOSTIC
4332 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
4333 (CCB_STARTQ | CCB_DISCQ))
4335 panic("%s: ccb in both queue", slp->sl_xname);
4337 #endif /* SCSI_LOW_DIAGNOSTIC */
4339 if ((cb->ccb_flags & CCB_STARTQ) != 0)
4341 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
4344 if ((cb->ccb_flags & CCB_DISCQ) != 0)
4346 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
4352 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
4353 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
4356 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
4357 (cb->ccb_flags & CCB_NORETRY) != 0))
4359 cb->ccb_error |= FATALIO;
4360 cb->ccb_flags &= ~CCB_AUTOSENSE;
4361 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
4362 panic("%s: done ccb retried", slp->sl_xname);
4367 cb->ccb_error |= PENDINGIO;
4368 scsi_low_deactivate_qtag(cb);
4369 scsi_low_ccb_message_retry(cb);
4370 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
4376 scsi_low_reset_nexus_lun(slp, li, fdone)
4377 struct scsi_low_softc *slp;
4378 struct lun_info *li;
4381 struct slccb *cb, *ncb, *ecb;
4387 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
4389 ncb = TAILQ_NEXT(cb, ccb_chain);
4390 cb = scsi_low_revoke_ccb(slp, cb, fdone);
4394 * presumely keep ordering of io
4396 cb->ccb_flags |= CCB_STARTQ;
4399 TAILQ_INSERT_HEAD(&slp->sl_start,\
4404 TAILQ_INSERT_AFTER(&slp->sl_start,\
4405 ecb, cb, ccb_chain);
4412 /**************************************************************
4414 **************************************************************/
4416 scsi_low_calcf_lun(li)
4417 struct lun_info *li;
4419 struct targ_info *ti = li->li_ti;
4420 struct scsi_low_softc *slp = ti->ti_sc;
4421 u_int cfgflags, diskflags;
4423 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
4424 cfgflags = li->li_cfgflags;
4428 diskflags = li->li_diskflags & li->li_quirks;
4431 li->li_flags &= ~SCSI_LOW_DISC;
4432 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
4433 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
4434 (cfgflags & SCSI_LOW_DISC) != 0)
4435 li->li_flags |= SCSI_LOW_DISC;
4438 li->li_flags |= SCSI_LOW_NOPARITY;
4439 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
4440 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
4441 (cfgflags & SCSI_LOW_NOPARITY) == 0)
4442 li->li_flags &= ~SCSI_LOW_NOPARITY;
4445 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
4446 (cfgflags & SCSI_LOW_QTAG) != 0 &&
4447 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
4449 li->li_flags |= SCSI_LOW_QTAG;
4450 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
4451 li->li_maxnqio = li->li_maxnexus;
4455 li->li_flags &= ~SCSI_LOW_QTAG;
4456 li->li_maxnexus = 0;
4457 li->li_maxnqio = li->li_maxnexus;
4461 li->li_flags &= ~SCSI_LOW_LINK;
4462 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
4463 (diskflags & SCSI_LOW_DISK_LINK) != 0)
4464 li->li_flags |= SCSI_LOW_LINK;
4466 /* compatible flags */
4467 li->li_flags &= ~SCSI_LOW_SYNC;
4468 if (ti->ti_maxsynch.offset > 0)
4469 li->li_flags |= SCSI_LOW_SYNC;
4471 #ifdef SCSI_LOW_DEBUG
4472 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4474 scsi_low_calcf_show(li);
4476 #endif /* SCSI_LOW_DEBUG */
4480 scsi_low_calcf_target(ti)
4481 struct targ_info *ti;
4483 struct scsi_low_softc *slp = ti->ti_sc;
4484 u_int offset, period, diskflags;
4486 diskflags = ti->ti_diskflags & ti->ti_quirks;
4489 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
4490 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
4492 offset = ti->ti_maxsynch.offset;
4493 period = ti->ti_maxsynch.period;
4494 if (offset == 0 || period == 0)
4495 offset = period = 0;
4499 offset = period = 0;
4502 ti->ti_maxsynch.offset = offset;
4503 ti->ti_maxsynch.period = period;
4506 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
4507 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
4508 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
4510 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
4511 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
4512 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
4514 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
4516 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
4517 ti->ti_maxsynch.period != ti->ti_osynch.period)
4518 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
4519 if (ti->ti_width != ti->ti_owidth)
4520 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
4522 ti->ti_osynch = ti->ti_maxsynch;
4523 ti->ti_owidth = ti->ti_width;
4526 #ifdef SCSI_LOW_DEBUG
4527 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4529 printf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
4530 slp->sl_xname, ti->ti_id,
4531 ti->ti_maxsynch.period * 4,
4532 ti->ti_maxsynch.offset,
4535 #endif /* SCSI_LOW_DEBUG */
4539 scsi_low_calcf_show(li)
4540 struct lun_info *li;
4542 struct targ_info *ti = li->li_ti;
4543 struct scsi_low_softc *slp = ti->ti_sc;
4545 printf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4546 slp->sl_xname, ti->ti_id, li->li_lun,
4547 ti->ti_maxsynch.period * 4,
4548 ti->ti_maxsynch.offset,
4550 li->li_flags, SCSI_LOW_BITS);
4553 #ifdef SCSI_LOW_START_UP_CHECK
4554 /**************************************************************
4555 * scsi world start up
4556 **************************************************************/
4557 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
4560 scsi_low_start_up(slp)
4561 struct scsi_low_softc *slp;
4563 struct targ_info *ti;
4564 struct lun_info *li;
4568 printf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
4570 for (target = 0; target < slp->sl_ntargs; target ++)
4572 if (target == slp->sl_hostid)
4574 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4576 printf("%s: scsi_low: target %d (host card)\n",
4577 slp->sl_xname, target);
4582 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4584 printf("%s: scsi_low: target %d lun ",
4585 slp->sl_xname, target);
4588 ti = slp->sl_ti[target];
4589 for (lun = 0; lun < slp->sl_nluns; lun ++)
4591 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4597 li = scsi_low_alloc_li(ti, lun, 1);
4599 scsi_low_enqueue(slp, ti, li, cb,
4600 CCB_AUTOSENSE | CCB_POLLED, 0);
4602 scsi_low_poll(slp, cb);
4604 if (li->li_state != SCSI_LOW_LUN_OK)
4607 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4613 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4622 scsi_low_poll(slp, cb)
4623 struct scsi_low_softc *slp;
4629 while (slp->sl_nio > 0)
4631 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4633 (*slp->sl_funcs->scsi_low_poll) (slp);
4634 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4638 scsi_low_timeout_check(slp);
4643 #endif /* SCSI_LOW_START_UP_CHECK */
4645 /**********************************************************
4647 **********************************************************/
4648 #ifdef SCSI_LOW_DEBUG
4650 scsi_low_test_abort(slp, ti, li)
4651 struct scsi_low_softc *slp;
4652 struct targ_info *ti;
4653 struct lun_info *li;
4657 if (li->li_disc > 1)
4659 acb = TAILQ_FIRST(&li->li_discq);
4660 if (scsi_low_abort_ccb(slp, acb) == 0)
4662 printf("%s: aborting ccb(0x%lx) start\n",
4663 slp->sl_xname, (u_long) acb);
4669 scsi_low_test_atten(slp, ti, msg)
4670 struct scsi_low_softc *slp;
4671 struct targ_info *ti;
4675 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4676 scsi_low_assert_msg(slp, ti, msg, 0);
4678 printf("%s: atten check OK\n", slp->sl_xname);
4682 scsi_low_test_cmdlnk(slp, cb)
4683 struct scsi_low_softc *slp;
4686 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4688 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4691 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4692 slp->sl_scp.scp_cmdlen);
4693 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4694 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4696 #endif /* SCSI_LOW_DEBUG */
4699 scsi_low_info(slp, ti, s)
4700 struct scsi_low_softc *slp;
4701 struct targ_info *ti;
4706 slp = LIST_FIRST(&sl_tab);
4710 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4713 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4714 ti = TAILQ_NEXT(ti, ti_chain))
4716 scsi_low_print(slp, ti);
4721 scsi_low_print(slp, ti);
4725 static u_char *phase[] =
4727 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4728 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4732 scsi_low_print(slp, ti)
4733 struct scsi_low_softc *slp;
4734 struct targ_info *ti;
4736 struct lun_info *li;
4740 if (ti == NULL || ti == slp->sl_Tnexus)
4742 ti = slp->sl_Tnexus;
4743 li = slp->sl_Lnexus;
4744 cb = slp->sl_Qnexus;
4748 li = LIST_FIRST(&ti->ti_litab);
4749 cb = TAILQ_FIRST(&li->li_discq);
4753 printf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4754 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4760 u_int flags = 0, maxnqio = 0, nqio = 0;
4766 flags = li->li_flags;
4767 maxnqio = li->li_maxnqio;
4771 printf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4773 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4774 phase[(int) ti->ti_phase], ti->ti_disc,
4779 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4780 (u_int) cb->ccb_scp.scp_cmd[0],
4781 cb->ccb_scp.scp_cmdlen,
4783 cb->ccb_scp.scp_datalen,
4784 (u_int) cb->ccb_sscp.scp_status,
4785 cb->ccb_error, SCSI_LOW_ERRORBITS);
4788 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4789 (u_int) (ti->ti_msginptr),
4790 (u_int) (ti->ti_msgin[0]),
4791 (u_int) (ti->ti_msgin[1]),
4792 (u_int) (ti->ti_msgin[2]),
4793 (u_int) (ti->ti_msgin[3]),
4794 (u_int) (ti->ti_msgin[4]),
4797 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4798 (u_int) ti->ti_msgflags,
4799 (u_int) (ti->ti_msgoutstr[0]),
4800 (u_int) (ti->ti_msgoutstr[1]),
4801 (u_int) (ti->ti_msgoutstr[2]),
4802 (u_int) (ti->ti_msgoutstr[3]),
4803 (u_int) (ti->ti_msgoutstr[4]),
4805 flags, SCSI_LOW_BITS);
4807 #ifdef SCSI_LOW_DIAGNOSTIC
4808 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4809 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4810 #endif /* SCSI_LOW_DIAGNOSTIC */
4814 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4815 (u_long) sp->scp_data,
4817 (u_int) sp->scp_status,
4818 slp->sl_error, SCSI_LOW_ERRORBITS);