2 * $FreeBSD: src/sys/cam/scsi/scsi_low.c,v 1.1.2.5 2003/08/09 06:18:30 non Exp $
3 * $DragonFly: src/sys/bus/cam/scsi/scsi_low.c,v 1.7 2004/02/11 17:46:33 joerg Exp $
4 * $NetBSD: 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 */
13 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
14 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
17 #define SCSI_LOW_TARGET_OPEN
18 #endif /* __NetBSD__ */
21 #define SCSI_LOW_FLAGS_QUIRKS_OK
22 #endif /* __DragonFly__ */
25 * [NetBSD for NEC PC-98 series]
26 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
27 * NetBSD/pc98 porting staff. All rights reserved.
28 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
29 * Naofumi HONDA. All rights reserved.
31 * [Ported for FreeBSD CAM]
32 * Copyright (c) 2000, 2001
33 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
51 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
60 /* <On the nexus establishment>
61 * When our host is reselected,
62 * nexus establish processes are little complicated.
63 * Normal steps are followings:
64 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
65 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
66 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
75 #include <machine/clock.h>
76 #include <sys/devicestat.h>
77 #endif /* __DragonFly__ */
80 #include <sys/queue.h>
81 #include <sys/malloc.h>
82 #include <sys/errno.h>
85 #include <sys/device.h>
88 #include <machine/bus.h>
89 #include <machine/intr.h>
90 #include <machine/dvcfg.h>
94 #include <dev/scsipi/scsipi_all.h>
95 #include <dev/scsipi/scsipiconf.h>
96 #include <dev/scsipi/scsipi_disk.h>
97 #include <dev/scsipi/scsi_all.h>
98 #include <dev/scsipi/scsiconf.h>
99 #include <sys/scsiio.h>
101 #include <i386/Cbus/dev/scsi_low.h>
102 #endif /* __NetBSD__ */
105 #include <bus/cam/cam.h>
106 #include <bus/cam/cam_ccb.h>
107 #include <bus/cam/cam_sim.h>
108 #include <bus/cam/cam_debug.h>
109 #include <bus/cam/cam_periph.h>
111 #include "scsi_all.h"
112 #include "scsi_message.h"
114 #include "scsi_low.h"
116 #include <sys/cons.h>
117 #endif /* __DragonFly__ */
119 /**************************************************************
121 **************************************************************/
122 #define SCSI_LOW_POLL_HZ 1000
124 /* functions return values */
125 #define SCSI_LOW_START_NO_QTAG 0
126 #define SCSI_LOW_START_QTAG 1
128 #define SCSI_LOW_DONE_COMPLETE 0
129 #define SCSI_LOW_DONE_RETRY 1
131 /* internal disk flags */
132 #define SCSI_LOW_DISK_DISC 0x00000001
133 #define SCSI_LOW_DISK_QTAG 0x00000002
134 #define SCSI_LOW_DISK_LINK 0x00000004
135 #define SCSI_LOW_DISK_PARITY 0x00000008
136 #define SCSI_LOW_DISK_SYNC 0x00010000
137 #define SCSI_LOW_DISK_WIDE_16 0x00020000
138 #define SCSI_LOW_DISK_WIDE_32 0x00040000
139 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
140 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
141 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
143 /**************************************************************
145 **************************************************************/
146 /* static */ void scsi_low_info (struct scsi_low_softc *, struct targ_info *, u_char *);
147 static void scsi_low_engage (void *);
148 static struct slccb *scsi_low_establish_ccb (struct targ_info *, struct lun_info *, scsi_low_tag_t);
149 static int scsi_low_done (struct scsi_low_softc *, struct slccb *);
150 static int scsi_low_setup_done (struct scsi_low_softc *, struct slccb *);
151 static void scsi_low_bus_release (struct scsi_low_softc *, struct targ_info *);
152 static void scsi_low_twiddle_wait (void);
153 static struct lun_info *scsi_low_alloc_li (struct targ_info *, int, int);
154 static struct targ_info *scsi_low_alloc_ti (struct scsi_low_softc *, int);
155 static void scsi_low_calcf_lun (struct lun_info *);
156 static void scsi_low_calcf_target (struct targ_info *);
157 static void scsi_low_calcf_show (struct lun_info *);
158 static void scsi_low_reset_nexus (struct scsi_low_softc *, int);
159 static void scsi_low_reset_nexus_target (struct scsi_low_softc *, struct targ_info *, int);
160 static void scsi_low_reset_nexus_lun (struct scsi_low_softc *, struct lun_info *, int);
161 static int scsi_low_init (struct scsi_low_softc *, u_int);
162 static void scsi_low_start (struct scsi_low_softc *);
163 static void scsi_low_free_ti (struct scsi_low_softc *);
165 static int scsi_low_alloc_qtag (struct slccb *);
166 static int scsi_low_dealloc_qtag (struct slccb *);
167 static int scsi_low_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
168 static int scsi_low_message_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
169 static void scsi_low_unit_ready_cmd (struct slccb *);
170 static void scsi_low_timeout (void *);
171 static int scsi_low_timeout_check (struct scsi_low_softc *);
172 #ifdef SCSI_LOW_START_UP_CHECK
173 static int scsi_low_start_up (struct scsi_low_softc *);
174 #endif /* SCSI_LOW_START_UP_CHECK */
175 static int scsi_low_abort_ccb (struct scsi_low_softc *, struct slccb *);
176 static struct slccb *scsi_low_revoke_ccb (struct scsi_low_softc *, struct slccb *, int);
178 int scsi_low_version_major = 2;
179 int scsi_low_version_minor = 17;
181 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
183 /**************************************************************
184 * Debug, Run test and Statics
185 **************************************************************/
186 #ifdef SCSI_LOW_INFO_DETAIL
187 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
188 #else /* !SCSI_LOW_INFO_DETAIL */
189 #define SCSI_LOW_INFO(slp, ti, s) printf("%s: %s\n", (slp)->sl_xname, (s))
190 #endif /* !SCSI_LOW_INFO_DETAIL */
192 #ifdef SCSI_LOW_STATICS
193 struct scsi_low_statics {
196 int nexus_disconnected;
197 int nexus_reselected;
200 #endif /* SCSI_LOW_STATICS */
202 #ifdef SCSI_LOW_DEBUG
203 #define SCSI_LOW_DEBUG_DONE 0x00001
204 #define SCSI_LOW_DEBUG_DISC 0x00002
205 #define SCSI_LOW_DEBUG_SENSE 0x00004
206 #define SCSI_LOW_DEBUG_CALCF 0x00008
207 #define SCSI_LOW_DEBUG_ACTION 0x10000
208 int scsi_low_debug = 0;
210 #define SCSI_LOW_MAX_ATTEN_CHECK 32
211 #define SCSI_LOW_ATTEN_CHECK 0x0001
212 #define SCSI_LOW_CMDLNK_CHECK 0x0002
213 #define SCSI_LOW_ABORT_CHECK 0x0004
214 #define SCSI_LOW_NEXUS_CHECK 0x0008
215 int scsi_low_test = 0;
216 int scsi_low_test_id = 0;
218 static void scsi_low_test_abort (struct scsi_low_softc *, struct targ_info *, struct lun_info *);
219 static void scsi_low_test_cmdlnk (struct scsi_low_softc *, struct slccb *);
220 static void scsi_low_test_atten (struct scsi_low_softc *, struct targ_info *, u_int);
221 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
222 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
223 #define SCSI_LOW_DEBUG_GO(fl, id) \
224 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
225 #endif /* SCSI_LOW_DEBUG */
227 /**************************************************************
229 **************************************************************/
230 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
231 GENERIC_CCB(scsi_low, slccb, ccb_chain)
233 /**************************************************************
235 **************************************************************/
236 #define SCSI_LOW_INLINE static __inline
237 SCSI_LOW_INLINE void scsi_low_activate_qtag (struct slccb *);
238 SCSI_LOW_INLINE void scsi_low_deactivate_qtag (struct slccb *);
239 SCSI_LOW_INLINE void scsi_low_ccb_message_assert (struct slccb *, u_int);
240 SCSI_LOW_INLINE void scsi_low_ccb_message_exec (struct scsi_low_softc *, struct slccb *);
241 SCSI_LOW_INLINE void scsi_low_ccb_message_retry (struct slccb *);
242 SCSI_LOW_INLINE void scsi_low_ccb_message_clear (struct slccb *);
243 SCSI_LOW_INLINE void scsi_low_init_msgsys (struct scsi_low_softc *, struct targ_info *);
246 scsi_low_activate_qtag(cb)
249 struct lun_info *li = cb->li;
251 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
255 cb->ccb_tag = cb->ccb_otag;
259 scsi_low_deactivate_qtag(cb)
262 struct lun_info *li = cb->li;
264 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
268 cb->ccb_tag = SCSI_LOW_UNKTAG;
272 scsi_low_ccb_message_exec(slp, cb)
273 struct scsi_low_softc *slp;
277 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
278 cb->ccb_msgoutflag = 0;
282 scsi_low_ccb_message_assert(cb, msg)
287 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
291 scsi_low_ccb_message_retry(cb)
294 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
298 scsi_low_ccb_message_clear(cb)
301 cb->ccb_msgoutflag = 0;
305 scsi_low_init_msgsys(slp, ti)
306 struct scsi_low_softc *slp;
307 struct targ_info *ti;
311 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
312 SCSI_LOW_DEASSERT_ATN(slp);
313 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
316 /*=============================================================
317 * START OF OS switch (All OS depend fucntions should be here)
318 =============================================================*/
319 /* common os depend utitlities */
320 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
321 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
322 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
324 static u_int8_t scsi_low_cmd_flags[256] = {
325 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
326 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
327 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
328 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
329 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
332 struct scsi_low_error_code {
337 static struct slccb *scsi_low_find_ccb (struct scsi_low_softc *, u_int, u_int, void *);
338 static int scsi_low_translate_error_code (struct slccb *, struct scsi_low_error_code *);
340 static struct slccb *
341 scsi_low_find_ccb(slp, target, lun, osdep)
342 struct scsi_low_softc *slp;
346 struct targ_info *ti;
350 ti = slp->sl_ti[target];
351 li = scsi_low_alloc_li(ti, lun, 0);
355 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
358 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
359 cb = TAILQ_NEXT(cb, ccb_chain))
361 if (cb->osdep == osdep)
365 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL;
366 cb = TAILQ_NEXT(cb, ccb_chain))
368 if (cb->osdep == osdep)
375 scsi_low_translate_error_code(cb, tp)
377 struct scsi_low_error_code *tp;
380 if (cb->ccb_error == 0)
381 return tp->error_code;
383 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
385 return tp->error_code;
388 #ifdef SCSI_LOW_INTERFACE_XS
389 /**************************************************************
390 * SCSI INTERFACE (XS)
391 **************************************************************/
392 #define SCSI_LOW_MINPHYS 0x10000
393 #define SCSI_LOW_MALLOC(size) malloc((size), M_DEVBUF, M_NOWAIT)
394 #define SCSI_LOW_FREE(pt) free((pt), M_DEVBUF)
395 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb((flags))
396 #define SCSI_LOW_XS_POLL_HZ 1000
398 static int scsi_low_poll_xs (struct scsi_low_softc *, struct slccb *));
399 static void scsi_low_scsi_minphys_xs (struct buf *);
400 #ifdef SCSI_LOW_TARGET_OPEN
401 static int scsi_low_target_open (struct scsipi_link *, struct cfdata *);
402 #endif /* SCSI_LOW_TARGET_OPEN */
403 static int scsi_low_scsi_cmd_xs (struct scsipi_xfer *);
404 static int scsi_low_enable_xs (void *, int);
405 static int scsi_low_ioctl_xs (struct scsipi_link *, u_long, caddr_t, int, struct proc *);
407 static int scsi_low_attach_xs (struct scsi_low_softc *);
408 static int scsi_low_world_start_xs (struct scsi_low_softc *);
409 static int scsi_low_dettach_xs (struct scsi_low_softc *);
410 static int scsi_low_ccb_setup_xs (struct scsi_low_softc *, struct slccb *);
411 static int scsi_low_done_xs (struct scsi_low_softc *, struct slccb *);
412 static void scsi_low_timeout_xs (struct scsi_low_softc *, int, int);
413 static u_int scsi_low_translate_quirks_xs (u_int);
414 static void scsi_low_setup_quirks_xs (struct targ_info *, struct lun_info *, u_int);
416 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_xs = {
418 scsi_low_world_start_xs,
420 scsi_low_ccb_setup_xs,
425 struct scsipi_device scsi_low_dev = {
426 NULL, /* Use default error handler */
427 NULL, /* have a queue, served by this */
428 NULL, /* have no async handler */
429 NULL, /* Use default 'done' routine */
432 struct scsi_low_error_code scsi_low_error_code_xs[] = {
436 {SELTIMEOUTIO, XS_SELTIMEOUT},
437 {TIMEOUTIO, XS_TIMEOUT},
438 {-1, XS_DRIVER_STUFFUP}
442 scsi_low_ioctl_xs(link, cmd, addr, flag, p)
443 struct scsipi_link *link;
449 struct scsi_low_softc *slp;
450 int s, error = ENOTTY;
452 slp = (struct scsi_low_softc *) link->adapter_softc;
453 if ((slp->sl_flags & HW_INACTIVE) != 0)
456 if (cmd == SCBUSIORESET)
458 s = SCSI_LOW_SPLSCSI();
459 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
463 else if (slp->sl_funcs->scsi_low_ioctl != 0)
465 error = (*slp->sl_funcs->scsi_low_ioctl)
466 (slp, cmd, addr, flag, p);
473 scsi_low_enable_xs(arg, enable)
477 struct scsi_low_softc *slp = arg;
481 if ((slp->sl_flags & HW_INACTIVE) != 0)
486 if ((slp->sl_flags & HW_INACTIVE) != 0 ||
487 (slp->sl_flags & HW_POWERCTRL) == 0)
490 slp->sl_flags |= HW_POWDOWN;
491 if (slp->sl_funcs->scsi_low_power != NULL)
493 (*slp->sl_funcs->scsi_low_power)
494 (slp, SCSI_LOW_POWDOWN);
501 scsi_low_scsi_minphys_xs(bp)
505 if (bp->b_bcount > SCSI_LOW_MINPHYS)
506 bp->b_bcount = SCSI_LOW_MINPHYS;
511 scsi_low_poll_xs(slp, cb)
512 struct scsi_low_softc *slp;
515 struct scsipi_xfer *xs = cb->osdep;
518 cb->ccb_flags |= CCB_NOSDONE;
521 while (slp->sl_nio > 0)
523 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_XS_POLL_HZ);
525 (*slp->sl_funcs->scsi_low_poll) (slp);
527 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
529 cb->ccb_flags |= CCB_NORETRY;
530 cb->ccb_error |= FATALIO;
531 (void) scsi_low_revoke_ccb(slp, cb, 1);
532 printf("%s: hardware inactive in poll mode\n",
536 if ((xs->flags & ITSDONE) != 0)
539 if (tcount ++ < SCSI_LOW_XS_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
543 scsi_low_timeout_check(slp);
546 xs->flags |= ITSDONE;
552 scsi_low_scsi_cmd_xs(xs)
553 struct scsipi_xfer *xs;
555 struct scsipi_link *splp = xs->sc_link;
556 struct scsi_low_softc *slp = splp->adapter_softc;
557 struct targ_info *ti;
560 int s, targ, lun, flags, rv;
562 if ((cb = SCSI_LOW_ALLOC_CCB(xs->flags & SCSI_NOSLEEP)) == NULL)
563 return TRY_AGAIN_LATER;
565 targ = splp->scsipi_scsi.target,
566 lun = splp->scsipi_scsi.lun;
567 ti = slp->sl_ti[targ];
572 if ((xs->flags & SCSI_POLL) == 0)
573 flags = CCB_AUTOSENSE;
575 flags = CCB_AUTOSENSE | CCB_POLLED;
578 s = SCSI_LOW_SPLSCSI();
579 li = scsi_low_alloc_li(ti, lun, 1);
580 if ((u_int) splp->quirks != li->li_sloi.sloi_quirks)
582 scsi_low_setup_quirks_xs(ti, li, (u_int) splp->quirks);
585 if ((xs->flags & SCSI_RESET) != 0)
587 flags |= CCB_NORETRY | CCB_URGENT;
588 scsi_low_enqueue(slp, ti, li, cb, flags, SCSI_LOW_MSG_RESET);
592 if (ti->ti_setup_msg != 0)
594 scsi_low_message_enqueue(slp, ti, li, flags);
598 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
601 #ifdef SCSI_LOW_DEBUG
602 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, ti->ti_id) != 0)
604 scsi_low_test_abort(slp, ti, li);
606 #endif /* SCSI_LOW_DEBUG */
608 if ((cb->ccb_flags & CCB_POLLED) != 0)
610 rv = scsi_low_poll_xs(slp, cb);
614 rv = SUCCESSFULLY_QUEUED;
621 scsi_low_attach_xs(slp)
622 struct scsi_low_softc *slp;
624 struct scsipi_adapter *sap;
625 struct scsipi_link *splp;
627 strncpy(slp->sl_xname, slp->sl_dev.dv_xname, 16);
629 sap = SCSI_LOW_MALLOC(sizeof(*sap));
632 splp = SCSI_LOW_MALLOC(sizeof(*splp));
636 SCSI_LOW_BZERO(sap, sizeof(*sap));
637 SCSI_LOW_BZERO(splp, sizeof(*splp));
639 sap->scsipi_cmd = scsi_low_scsi_cmd_xs;
640 sap->scsipi_minphys = scsi_low_scsi_minphys_xs;
641 sap->scsipi_enable = scsi_low_enable_xs;
642 sap->scsipi_ioctl = scsi_low_ioctl_xs;
643 #ifdef SCSI_LOW_TARGET_OPEN
644 sap->open_target_lu = scsi_low_target_open;
645 #endif /* SCSI_LOW_TARGET_OPEN */
647 splp->adapter_softc = slp;
648 splp->scsipi_scsi.adapter_target = slp->sl_hostid;
649 splp->scsipi_scsi.max_target = slp->sl_ntargs - 1;
650 splp->scsipi_scsi.max_lun = slp->sl_nluns - 1;
651 splp->scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
652 splp->openings = slp->sl_openings;
653 splp->type = BUS_SCSI;
654 splp->adapter_softc = slp;
656 splp->device = &scsi_low_dev;
658 slp->sl_si.si_splp = splp;
659 slp->sl_show_result = SHOW_ALL_NEG;
664 scsi_low_world_start_xs(slp)
665 struct scsi_low_softc *slp;
672 scsi_low_dettach_xs(slp)
673 struct scsi_low_softc *slp;
677 * scsipi does not have dettach bus fucntion.
679 scsipi_dettach_scsibus(slp->sl_si.si_splp);
685 scsi_low_ccb_setup_xs(slp, cb)
686 struct scsi_low_softc *slp;
689 struct scsipi_xfer *xs = (struct scsipi_xfer *) cb->osdep;
691 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
693 cb->ccb_scp.scp_cmd = (u_int8_t *) xs->cmd;
694 cb->ccb_scp.scp_cmdlen = xs->cmdlen;
695 cb->ccb_scp.scp_data = xs->data;
696 cb->ccb_scp.scp_datalen = xs->datalen;
697 cb->ccb_scp.scp_direction = (xs->flags & SCSI_DATA_OUT) ?
698 SCSI_LOW_WRITE : SCSI_LOW_READ;
699 cb->ccb_tcmax = xs->timeout / 1000;
703 scsi_low_unit_ready_cmd(cb);
705 return SCSI_LOW_START_QTAG;
709 scsi_low_done_xs(slp, cb)
710 struct scsi_low_softc *slp;
713 struct scsipi_xfer *xs;
715 xs = (struct scsipi_xfer *) cb->osdep;
716 if (cb->ccb_error == 0)
718 xs->error = XS_NOERROR;
723 if (cb->ccb_rcnt >= slp->sl_max_retry)
724 cb->ccb_error |= ABORTIO;
726 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
727 (cb->ccb_error & ABORTIO) == 0)
730 if ((cb->ccb_error & SENSEIO) != 0)
732 xs->sense.scsi_sense = cb->ccb_sense;
735 xs->error = scsi_low_translate_error_code(cb,
736 &scsi_low_error_code_xs[0]);
738 #ifdef SCSI_LOW_DIAGNOSTIC
739 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
740 cb->ccb_scp.scp_cmdlen > 0 &&
741 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
742 SCSI_LOW_CMD_ABORT_WARNING) != 0)
744 printf("%s: WARNING: scsi_low IO abort\n",
746 scsi_low_print(slp, NULL);
748 #endif /* SCSI_LOW_DIAGNOSTIC */
751 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
752 xs->status = 0; /* XXX */
754 xs->status = cb->ccb_scp.scp_status;
756 xs->flags |= ITSDONE;
757 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
764 scsi_low_timeout_xs(slp, ch, action)
765 struct scsi_low_softc *slp;
772 case SCSI_LOW_TIMEOUT_CH_IO:
775 case SCSI_LOW_TIMEOUT_START:
776 timeout(scsi_low_timeout, slp,
777 hz / SCSI_LOW_TIMEOUT_HZ);
779 case SCSI_LOW_TIMEOUT_STOP:
780 untimeout(scsi_low_timeout, slp);
785 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
788 case SCSI_LOW_TIMEOUT_START:
789 timeout(scsi_low_engage, slp, 1);
791 case SCSI_LOW_TIMEOUT_STOP:
792 untimeout(scsi_low_engage, slp);
797 case SCSI_LOW_TIMEOUT_CH_RECOVER:
803 scsi_low_translate_quirks_xs(quirks)
808 flags = SCSI_LOW_DISK_LFLAGS | SCSI_LOW_DISK_TFLAGS;
811 if (quirks & SDEV_NODISC)
812 flags &= ~SCSI_LOW_DISK_DISC;
813 #endif /* SDEV_NODISC */
815 if (quirks & SDEV_NOPARITY)
816 flags &= ~SCSI_LOW_DISK_PARITY;
817 #endif /* SDEV_NOPARITY */
819 if (quirks & SDEV_NOCMDLNK)
820 flags &= ~SCSI_LOW_DISK_LINK;
821 #endif /* SDEV_NOCMDLNK */
823 if (quirks & SDEV_NOTAG)
824 flags &= ~SCSI_LOW_DISK_QTAG;
825 #endif /* SDEV_NOTAG */
827 if (quirks & SDEV_NOSYNC)
828 flags &= ~SCSI_LOW_DISK_SYNC;
829 #endif /* SDEV_NOSYNC */
835 scsi_low_setup_quirks_xs(ti, li, flags)
836 struct targ_info *ti;
842 li->li_sloi.sloi_quirks = flags;
843 quirks = scsi_low_translate_quirks_xs(flags);
844 ti->ti_quirks = quirks & SCSI_LOW_DISK_TFLAGS;
845 li->li_quirks = quirks & SCSI_LOW_DISK_LFLAGS;
846 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
847 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
848 scsi_low_calcf_target(ti);
849 scsi_low_calcf_lun(li);
850 scsi_low_calcf_show(li);
853 #ifdef SCSI_LOW_TARGET_OPEN
855 scsi_low_target_open(link, cf)
856 struct scsipi_link *link;
859 u_int target = link->scsipi_scsi.target;
860 u_int lun = link->scsipi_scsi.lun;
861 struct scsi_low_softc *slp;
862 struct targ_info *ti;
865 slp = (struct scsi_low_softc *) link->adapter_softc;
866 ti = slp->sl_ti[target];
867 li = scsi_low_alloc_li(ti, lun, 0);
871 li->li_cfgflags = cf->cf_flags;
872 scsi_low_setup_quirks_xs(ti, li, (u_int) link->quirks);
875 #endif /* SCSI_LOW_TARGET_OPEN */
877 #endif /* SCSI_LOW_INTERFACE_XS */
879 #ifdef SCSI_LOW_INTERFACE_CAM
880 /**************************************************************
881 * SCSI INTERFACE (CAM)
882 **************************************************************/
883 #define SCSI_LOW_MALLOC(size) malloc((size), M_DEVBUF, M_NOWAIT)
884 #define SCSI_LOW_FREE(pt) free((pt), M_DEVBUF)
885 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
887 static void scsi_low_poll_cam (struct cam_sim *);
888 static void scsi_low_cam_rescan_callback (struct cam_periph *, union ccb *);
889 static void scsi_low_rescan_bus_cam (struct scsi_low_softc *);
890 void scsi_low_scsi_action_cam (struct cam_sim *, union ccb *);
892 static int scsi_low_attach_cam (struct scsi_low_softc *);
893 static int scsi_low_world_start_cam (struct scsi_low_softc *);
894 static int scsi_low_dettach_cam (struct scsi_low_softc *);
895 static int scsi_low_ccb_setup_cam (struct scsi_low_softc *, struct slccb *);
896 static int scsi_low_done_cam (struct scsi_low_softc *, struct slccb *);
897 static void scsi_low_timeout_cam (struct scsi_low_softc *, int, int);
899 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
901 scsi_low_world_start_cam,
902 scsi_low_dettach_cam,
903 scsi_low_ccb_setup_cam,
908 struct scsi_low_error_code scsi_low_error_code_cam[] = {
910 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
911 {SENSEERR, CAM_AUTOSENSE_FAIL},
912 {UACAERR, CAM_SCSI_STATUS_ERROR},
913 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
914 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
915 {TIMEOUTIO, CAM_CMD_TIMEOUT},
916 {PDMAERR, CAM_DATA_RUN_ERR},
917 {PARITYERR, CAM_UNCOR_PARITY},
918 {UBFERR, CAM_UNEXP_BUSFREE},
919 {ABORTIO, CAM_REQ_ABORTED},
920 {-1, CAM_UNREC_HBA_ERROR}
923 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
926 * Please check a polling hz, currently we assume scsi_low_poll() is
929 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
932 scsi_low_poll_cam(sim)
935 struct scsi_low_softc *slp = SIM2SLP(sim);
937 (*slp->sl_funcs->scsi_low_poll) (slp);
939 if (slp->sl_si.si_poll_count ++ >=
940 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
942 slp->sl_si.si_poll_count = 0;
943 scsi_low_timeout_check(slp);
948 scsi_low_cam_rescan_callback(periph, ccb)
949 struct cam_periph *periph;
953 xpt_free_path(ccb->ccb_h.path);
958 scsi_low_rescan_bus_cam(slp)
959 struct scsi_low_softc *slp;
961 struct cam_path *path;
962 union ccb *ccb = malloc(sizeof(union ccb), M_DEVBUF, M_WAITOK);
965 bzero(ccb, sizeof(union ccb));
967 status = xpt_create_path(&path, xpt_periph,
968 cam_sim_path(slp->sl_si.sim), -1, 0);
969 if (status != CAM_REQ_CMP)
972 xpt_setup_ccb(&ccb->ccb_h, path, 5);
973 ccb->ccb_h.func_code = XPT_SCAN_BUS;
974 ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
975 ccb->crcn.flags = CAM_FLAG_NONE;
980 scsi_low_scsi_action_cam(sim, ccb)
984 struct scsi_low_softc *slp = SIM2SLP(sim);
985 struct targ_info *ti;
988 u_int lun, flags, msg, target;
991 target = (u_int) (ccb->ccb_h.target_id);
992 lun = (u_int) ccb->ccb_h.target_lun;
994 #ifdef SCSI_LOW_DEBUG
995 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
997 printf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
998 slp->sl_xname, ccb->ccb_h.func_code, target, lun);
1000 #endif /* SCSI_LOW_DEBUG */
1002 switch (ccb->ccb_h.func_code) {
1003 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1004 #ifdef SCSI_LOW_DIAGNOSTIC
1005 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
1007 printf("%s: invalid target/lun\n", slp->sl_xname);
1008 ccb->ccb_h.status = CAM_REQ_INVALID;
1012 #endif /* SCSI_LOW_DIAGNOSTIC */
1014 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
1015 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1020 ti = slp->sl_ti[target];
1023 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1024 flags = CCB_AUTOSENSE | CCB_SCSIIO;
1028 s = SCSI_LOW_SPLSCSI();
1029 li = scsi_low_alloc_li(ti, lun, 1);
1031 if (ti->ti_setup_msg != 0)
1033 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
1036 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
1038 #ifdef SCSI_LOW_DEBUG
1039 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
1041 scsi_low_test_abort(slp, ti, li);
1043 #endif /* SCSI_LOW_DEBUG */
1047 case XPT_EN_LUN: /* Enable LUN as a target */
1048 case XPT_TARGET_IO: /* Execute target I/O request */
1049 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1050 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1052 ccb->ccb_h.status = CAM_REQ_INVALID;
1056 case XPT_ABORT: /* Abort the specified CCB */
1057 #ifdef SCSI_LOW_DIAGNOSTIC
1058 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
1060 printf("%s: invalid target/lun\n", slp->sl_xname);
1061 ccb->ccb_h.status = CAM_REQ_INVALID;
1065 #endif /* SCSI_LOW_DIAGNOSTIC */
1067 s = SCSI_LOW_SPLSCSI();
1068 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
1069 rv = scsi_low_abort_ccb(slp, cb);
1073 ccb->ccb_h.status = CAM_REQ_CMP;
1075 ccb->ccb_h.status = CAM_REQ_INVALID;
1079 case XPT_SET_TRAN_SETTINGS: {
1080 struct ccb_trans_settings *cts;
1083 #ifdef SCSI_LOW_DIAGNOSTIC
1084 if (target == CAM_TARGET_WILDCARD)
1086 printf("%s: invalid target\n", slp->sl_xname);
1087 ccb->ccb_h.status = CAM_REQ_INVALID;
1091 #endif /* SCSI_LOW_DIAGNOSTIC */
1093 ti = slp->sl_ti[target];
1094 if (lun == CAM_LUN_WILDCARD)
1097 s = SCSI_LOW_SPLSCSI();
1098 if ((cts->valid & (CCB_TRANS_BUS_WIDTH_VALID |
1099 CCB_TRANS_SYNC_RATE_VALID |
1100 CCB_TRANS_SYNC_OFFSET_VALID)) != 0)
1102 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
1103 val = cts->bus_width;
1104 if (val < ti->ti_width)
1107 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
1108 val = cts->sync_period;
1109 if (val == 0 || val > ti->ti_maxsynch.period)
1110 ti->ti_maxsynch.period = val;
1112 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) {
1113 val = cts->sync_offset;
1114 if (val < ti->ti_maxsynch.offset)
1115 ti->ti_maxsynch.offset = val;
1118 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1119 scsi_low_calcf_target(ti);
1122 if ((cts->valid & (CCB_TRANS_DISC_VALID |
1123 CCB_TRANS_TQ_VALID)) != 0)
1125 li = scsi_low_alloc_li(ti, lun, 1);
1126 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0)
1128 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
1129 li->li_quirks |= SCSI_LOW_DISK_DISC;
1131 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
1133 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
1135 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
1136 li->li_quirks |= SCSI_LOW_DISK_QTAG;
1138 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
1141 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1142 scsi_low_calcf_target(ti);
1143 scsi_low_calcf_lun(li);
1144 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
1145 scsi_low_calcf_show(li);
1149 ccb->ccb_h.status = CAM_REQ_CMP;
1154 case XPT_GET_TRAN_SETTINGS: {
1155 struct ccb_trans_settings *cts;
1159 #ifdef SCSI_LOW_DIAGNOSTIC
1160 if (target == CAM_TARGET_WILDCARD)
1162 printf("%s: invalid target\n", slp->sl_xname);
1163 ccb->ccb_h.status = CAM_REQ_INVALID;
1167 #endif /* SCSI_LOW_DIAGNOSTIC */
1168 ti = slp->sl_ti[target];
1169 if (lun == CAM_LUN_WILDCARD)
1172 s = SCSI_LOW_SPLSCSI();
1173 li = scsi_low_alloc_li(ti, lun, 1);
1174 #ifdef CAM_NEW_TRAN_CODE
1175 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1176 struct ccb_trans_settings_scsi *scsi =
1177 &cts->proto_specific.scsi;
1178 struct ccb_trans_settings_spi *spi =
1179 &cts->xport_specific.spi;
1180 #ifdef SCSI_LOW_DIAGNOSTIC
1181 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
1183 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1184 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
1188 #endif /* SCSI_LOW_DIAGNOSTIC */
1189 cts->protocol = PROTO_SCSI;
1190 cts->protocol_version = SCSI_REV_2;
1191 cts->transport = XPORT_SPI;
1192 cts->transport_version = 2;
1194 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1195 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1197 diskflags = li->li_diskflags & li->li_cfgflags;
1198 if (diskflags & SCSI_LOW_DISK_DISC)
1199 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1200 if (diskflags & SCSI_LOW_DISK_QTAG)
1201 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1203 spi->sync_period = ti->ti_maxsynch.period;
1204 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1205 spi->sync_offset = ti->ti_maxsynch.offset;
1206 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1208 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1209 spi->bus_width = ti->ti_width;
1211 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1212 scsi->valid = CTS_SCSI_VALID_TQ;
1213 spi->valid |= CTS_SPI_VALID_DISC;
1217 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1219 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0)
1221 #ifdef SCSI_LOW_DIAGNOSTIC
1222 if ((li->li_flags_valid & SCSI_LOW_LUN_FLAGS_DISK_VALID) == 0)
1224 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1225 printf("%s: invalid GET_TRANS_USER_SETTINGS call\n",
1229 #endif /* SCSI_LOW_DIAGNOSTIC */
1230 diskflags = li->li_diskflags & li->li_cfgflags;
1231 if ((diskflags & SCSI_LOW_DISK_DISC) != 0)
1232 cts->flags |= CCB_TRANS_DISC_ENB;
1234 cts->flags &= ~CCB_TRANS_DISC_ENB;
1235 if ((diskflags & SCSI_LOW_DISK_QTAG) != 0)
1236 cts->flags |= CCB_TRANS_TAG_ENB;
1238 cts->flags &= ~CCB_TRANS_TAG_ENB;
1240 else if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
1242 #ifdef SCSI_LOW_DIAGNOSTIC
1243 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
1245 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1246 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
1250 #endif /* SCSI_LOW_DIAGNOSTIC */
1251 if ((li->li_flags & SCSI_LOW_DISC) != 0)
1252 cts->flags |= CCB_TRANS_DISC_ENB;
1254 cts->flags &= ~CCB_TRANS_DISC_ENB;
1255 if ((li->li_flags & SCSI_LOW_QTAG) != 0)
1256 cts->flags |= CCB_TRANS_TAG_ENB;
1258 cts->flags &= ~CCB_TRANS_TAG_ENB;
1262 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1266 cts->sync_period = ti->ti_maxsynch.period;
1267 cts->sync_offset = ti->ti_maxsynch.offset;
1268 cts->bus_width = ti->ti_width;
1270 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1271 | CCB_TRANS_SYNC_OFFSET_VALID
1272 | CCB_TRANS_BUS_WIDTH_VALID
1273 | CCB_TRANS_DISC_VALID
1274 | CCB_TRANS_TQ_VALID;
1275 ccb->ccb_h.status = CAM_REQ_CMP;
1283 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
1284 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1289 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1290 s = SCSI_LOW_SPLSCSI();
1291 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
1293 ccb->ccb_h.status = CAM_REQ_CMP;
1297 case XPT_TERM_IO: /* Terminate the I/O process */
1298 ccb->ccb_h.status = CAM_REQ_INVALID;
1302 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1303 #ifdef SCSI_LOW_DIAGNOSTIC
1304 if (target == CAM_TARGET_WILDCARD)
1306 printf("%s: invalid target\n", slp->sl_xname);
1307 ccb->ccb_h.status = CAM_REQ_INVALID;
1311 #endif /* SCSI_LOW_DIAGNOSTIC */
1313 msg = SCSI_LOW_MSG_RESET;
1314 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
1316 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1321 ti = slp->sl_ti[target];
1322 if (lun == CAM_LUN_WILDCARD)
1326 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1327 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
1329 flags = CCB_NORETRY | CCB_URGENT;
1331 s = SCSI_LOW_SPLSCSI();
1332 li = scsi_low_alloc_li(ti, lun, 1);
1333 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
1337 case XPT_PATH_INQ: { /* Path routing inquiry */
1338 struct ccb_pathinq *cpi = &ccb->cpi;
1340 cpi->version_num = scsi_low_version_major;
1341 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
1342 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
1343 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
1344 cpi->hba_inquiry |= PI_WIDE_16;
1345 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
1346 cpi->hba_inquiry |= PI_WIDE_32;
1347 if (ti->ti_maxsynch.offset > 0)
1348 cpi->hba_inquiry |= PI_SDTR_ABLE;
1349 cpi->target_sprt = 0;
1351 cpi->hba_eng_cnt = 0;
1352 cpi->max_target = slp->sl_ntargs - 1;
1353 cpi->max_lun = slp->sl_nluns - 1;
1354 cpi->initiator_id = slp->sl_hostid;
1355 cpi->bus_id = cam_sim_bus(sim);
1356 cpi->base_transfer_speed = 3300;
1357 #ifdef CAM_NEW_TRAN_CODE
1358 cpi->transport = XPORT_SPI;
1359 cpi->transport_version = 2;
1360 cpi->protocol = PROTO_SCSI;
1361 cpi->protocol_version = SCSI_REV_2;
1363 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1364 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
1365 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1366 cpi->unit_number = cam_sim_unit(sim);
1367 cpi->ccb_h.status = CAM_REQ_CMP;
1373 printf("scsi_low: non support func_code = %d ",
1374 ccb->ccb_h.func_code);
1375 ccb->ccb_h.status = CAM_REQ_INVALID;
1382 scsi_low_attach_cam(slp)
1383 struct scsi_low_softc *slp;
1385 struct cam_devq *devq;
1386 int tagged_openings;
1388 sprintf(slp->sl_xname, "%s%d",
1389 DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
1391 devq = cam_simq_alloc(SCSI_LOW_NCCB);
1396 * ask the adapter what subunits are present
1398 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
1399 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
1401 DEVPORT_DEVNAME(slp->sl_dev), slp,
1402 DEVPORT_DEVUNIT(slp->sl_dev),
1403 slp->sl_openings, tagged_openings, devq);
1405 if (slp->sl_si.sim == NULL) {
1406 cam_simq_free(devq);
1410 if (xpt_bus_register(slp->sl_si.sim, 0) != CAM_SUCCESS) {
1411 free(slp->sl_si.sim, M_DEVBUF);
1415 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
1416 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
1417 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1418 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1419 cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
1420 free(slp->sl_si.sim, M_DEVBUF);
1424 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
1429 scsi_low_world_start_cam(slp)
1430 struct scsi_low_softc *slp;
1434 scsi_low_rescan_bus_cam(slp);
1439 scsi_low_dettach_cam(slp)
1440 struct scsi_low_softc *slp;
1443 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
1444 xpt_free_path(slp->sl_si.path);
1445 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
1446 cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
1451 scsi_low_ccb_setup_cam(slp, cb)
1452 struct scsi_low_softc *slp;
1455 union ccb *ccb = (union ccb *) cb->osdep;
1457 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
1459 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
1460 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
1461 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
1462 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
1463 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1464 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
1465 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
1466 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1467 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
1471 scsi_low_unit_ready_cmd(cb);
1473 return SCSI_LOW_START_QTAG;
1477 scsi_low_done_cam(slp, cb)
1478 struct scsi_low_softc *slp;
1483 ccb = (union ccb *) cb->osdep;
1484 if (cb->ccb_error == 0)
1486 ccb->ccb_h.status = CAM_REQ_CMP;
1487 ccb->csio.resid = 0;
1491 if (cb->ccb_rcnt >= slp->sl_max_retry)
1492 cb->ccb_error |= ABORTIO;
1494 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
1495 (cb->ccb_error & ABORTIO) == 0)
1498 if ((cb->ccb_error & SENSEIO) != 0)
1500 memcpy(&ccb->csio.sense_data,
1502 sizeof(ccb->csio.sense_data));
1505 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
1506 &scsi_low_error_code_cam[0]);
1508 #ifdef SCSI_LOW_DIAGNOSTIC
1509 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
1510 cb->ccb_scp.scp_cmdlen > 0 &&
1511 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1512 SCSI_LOW_CMD_ABORT_WARNING) != 0)
1514 printf("%s: WARNING: scsi_low IO abort\n",
1516 scsi_low_print(slp, NULL);
1518 #endif /* SCSI_LOW_DIAGNOSTIC */
1521 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
1522 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1524 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
1525 ccb->csio.scsi_status = 0; /* XXX */
1527 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
1529 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
1535 scsi_low_timeout_cam(slp, ch, action)
1536 struct scsi_low_softc *slp;
1543 case SCSI_LOW_TIMEOUT_CH_IO:
1546 case SCSI_LOW_TIMEOUT_START:
1547 slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
1548 hz / SCSI_LOW_TIMEOUT_HZ);
1550 case SCSI_LOW_TIMEOUT_STOP:
1551 untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
1556 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
1559 case SCSI_LOW_TIMEOUT_START:
1560 slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
1562 case SCSI_LOW_TIMEOUT_STOP:
1563 untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
1567 case SCSI_LOW_TIMEOUT_CH_RECOVER:
1572 #endif /* SCSI_LOW_INTERFACE_CAM */
1574 /*=============================================================
1575 * END OF OS switch (All OS depend fucntions should be above)
1576 =============================================================*/
1578 /**************************************************************
1579 * scsi low deactivate and activate
1580 **************************************************************/
1582 scsi_low_is_busy(slp)
1583 struct scsi_low_softc *slp;
1586 if (slp->sl_nio > 0)
1592 scsi_low_deactivate(slp)
1593 struct scsi_low_softc *slp;
1597 s = SCSI_LOW_SPLSCSI();
1598 slp->sl_flags |= HW_INACTIVE;
1599 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1600 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
1601 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1602 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1608 scsi_low_activate(slp)
1609 struct scsi_low_softc *slp;
1613 s = SCSI_LOW_SPLSCSI();
1614 slp->sl_flags &= ~HW_INACTIVE;
1615 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
1617 slp->sl_flags |= HW_INACTIVE;
1622 slp->sl_timeout_count = 0;
1623 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1624 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1629 /**************************************************************
1631 **************************************************************/
1632 #ifdef SCSI_LOW_DIAGNOSTIC
1633 static void scsi_low_msg_log_init (struct scsi_low_msg_log *);
1634 static void scsi_low_msg_log_write (struct scsi_low_msg_log *, u_int8_t *,
1636 static void scsi_low_msg_log_show (struct scsi_low_msg_log *, char *, int);
1639 scsi_low_msg_log_init(slmlp)
1640 struct scsi_low_msg_log *slmlp;
1643 slmlp->slml_ptr = 0;
1647 scsi_low_msg_log_write(slmlp, datap, len)
1648 struct scsi_low_msg_log *slmlp;
1654 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1657 ptr = slmlp->slml_ptr ++;
1658 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1659 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1660 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1661 slmlp->slml_msg[ptr].msg[ind] = 0;
1665 scsi_low_msg_log_show(slmlp, s, len)
1666 struct scsi_low_msg_log *slmlp;
1672 printf("%s: (%d) ", s, slmlp->slml_ptr);
1673 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1675 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1678 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1684 #endif /* SCSI_LOW_DIAGNOSTIC */
1686 /**************************************************************
1688 **************************************************************/
1690 scsi_low_engage(arg)
1693 struct scsi_low_softc *slp = arg;
1694 int s = SCSI_LOW_SPLSCSI();
1696 switch (slp->sl_rstep)
1700 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1701 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1702 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1707 slp->sl_flags &= ~HW_RESUME;
1708 scsi_low_start(slp);
1718 scsi_low_init(slp, flags)
1719 struct scsi_low_softc *slp;
1724 slp->sl_flags |= HW_INITIALIZING;
1726 /* clear power control timeout */
1727 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1729 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1730 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1731 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1733 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1736 /* reset current nexus */
1737 scsi_low_reset_nexus(slp, flags);
1738 if ((slp->sl_flags & HW_INACTIVE) != 0)
1744 if (flags != SCSI_LOW_RESTART_SOFT)
1746 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1750 slp->sl_flags &= ~HW_INITIALIZING;
1754 /**************************************************************
1756 **************************************************************/
1757 static struct lun_info *
1758 scsi_low_alloc_li(ti, lun, alloc)
1759 struct targ_info *ti;
1763 struct scsi_low_softc *slp = ti->ti_sc;
1764 struct lun_info *li;
1766 li = LIST_FIRST(&ti->ti_litab);
1769 if (li->li_lun == lun)
1772 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1774 if (li->li_lun == lun)
1776 LIST_REMOVE(li, lun_chain);
1777 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1786 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1788 panic("no lun info mem");
1790 SCSI_LOW_BZERO(li, ti->ti_lunsize);
1794 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1796 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1797 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1798 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1799 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1800 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1802 li->li_qtagbits = (u_int) -1;
1804 TAILQ_INIT(&li->li_discq);
1805 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1807 /* host specific structure initialization per lun */
1808 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1809 (*slp->sl_funcs->scsi_low_lun_init)
1810 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1811 scsi_low_calcf_lun(li);
1815 /**************************************************************
1816 * allocate targ_info
1817 **************************************************************/
1818 static struct targ_info *
1819 scsi_low_alloc_ti(slp, targ)
1820 struct scsi_low_softc *slp;
1823 struct targ_info *ti;
1825 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1826 TAILQ_INIT(&slp->sl_titab);
1828 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1830 panic("%s short of memory", slp->sl_xname);
1832 SCSI_LOW_BZERO(ti, slp->sl_targsize);
1836 slp->sl_ti[targ] = ti;
1837 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1838 LIST_INIT(&ti->ti_litab);
1840 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1841 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1842 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1843 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1844 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1845 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1847 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1849 (*slp->sl_funcs->scsi_low_targ_init)
1850 (slp, ti, SCSI_LOW_INFO_ALLOC);
1852 scsi_low_calcf_target(ti);
1857 scsi_low_free_ti(slp)
1858 struct scsi_low_softc *slp;
1860 struct targ_info *ti, *tib;
1861 struct lun_info *li, *nli;
1863 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1865 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1867 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1869 (*slp->sl_funcs->scsi_low_lun_init)
1870 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1872 nli = LIST_NEXT(li, lun_chain);
1876 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1878 (*slp->sl_funcs->scsi_low_targ_init)
1879 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1881 tib = TAILQ_NEXT(ti, ti_chain);
1886 /**************************************************************
1888 **************************************************************/
1890 scsi_low_bus_idle(slp)
1891 struct scsi_low_softc *slp;
1894 slp->sl_retry_sel = 0;
1895 if (slp->sl_Tnexus == NULL)
1896 scsi_low_start(slp);
1900 scsi_low_timeout(arg)
1903 struct scsi_low_softc *slp = arg;
1906 s = SCSI_LOW_SPLSCSI();
1907 (void) scsi_low_timeout_check(slp);
1908 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1909 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1914 scsi_low_timeout_check(slp)
1915 struct scsi_low_softc *slp;
1917 struct targ_info *ti;
1918 struct lun_info *li;
1919 struct slccb *cb = NULL; /* XXX */
1921 /* selection restart */
1922 if (slp->sl_retry_sel != 0)
1924 slp->sl_retry_sel = 0;
1925 if (slp->sl_Tnexus != NULL)
1928 cb = TAILQ_FIRST(&slp->sl_start);
1932 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1934 cb->ccb_flags |= CCB_NORETRY;
1935 cb->ccb_error |= SELTIMEOUTIO;
1936 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1937 panic("%s: ccb not finished", slp->sl_xname);
1940 if (slp->sl_Tnexus == NULL)
1941 scsi_low_start(slp);
1944 /* call hardware timeout */
1946 if (slp->sl_funcs->scsi_low_timeout != NULL)
1948 (*slp->sl_funcs->scsi_low_timeout) (slp);
1951 if (slp->sl_timeout_count ++ <
1952 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1955 slp->sl_timeout_count = 0;
1956 if (slp->sl_nio > 0)
1958 if ((cb = slp->sl_Qnexus) != NULL)
1960 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1964 else if (slp->sl_disc == 0)
1966 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1969 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1973 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1974 ti = TAILQ_NEXT(ti, ti_chain))
1976 if (ti->ti_disc == 0)
1979 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1980 li = LIST_NEXT(li, lun_chain))
1982 for (cb = TAILQ_FIRST(&li->li_discq);
1984 cb = TAILQ_NEXT(cb, ccb_chain))
1987 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1995 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1997 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
2000 if (slp->sl_active != 0)
2002 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
2008 if (slp->sl_powc < 0)
2010 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
2011 slp->sl_flags |= HW_POWDOWN;
2012 (*slp->sl_funcs->scsi_low_power)
2013 (slp, SCSI_LOW_POWDOWN);
2019 cb->ccb_error |= TIMEOUTIO;
2020 printf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
2021 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
2022 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
2023 scsi_low_start(slp);
2029 scsi_low_abort_ccb(slp, cb)
2030 struct scsi_low_softc *slp;
2033 struct targ_info *ti;
2034 struct lun_info *li;
2039 if ((cb->ccb_omsgoutflag &
2040 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
2045 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2046 msg = SCSI_LOW_MSG_ABORT;
2048 msg = SCSI_LOW_MSG_ABORT_QTAG;
2050 cb->ccb_error |= ABORTIO;
2051 cb->ccb_flags |= CCB_NORETRY;
2052 scsi_low_ccb_message_assert(cb, msg);
2054 if (cb == slp->sl_Qnexus)
2056 scsi_low_assert_msg(slp, ti, msg, 1);
2058 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
2060 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
2061 panic("%s: revoked ccb done", slp->sl_xname);
2063 cb->ccb_flags |= CCB_STARTQ;
2064 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2066 if (slp->sl_Tnexus == NULL)
2067 scsi_low_start(slp);
2071 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
2072 panic("%s: revoked ccb retried", slp->sl_xname);
2077 /**************************************************************
2078 * Generic SCSI INTERFACE
2079 **************************************************************/
2081 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
2082 struct scsi_low_softc *slp;
2083 int openings, ntargs, nluns, targsize, lunsize;
2085 struct targ_info *ti;
2086 struct lun_info *li;
2089 #ifdef SCSI_LOW_INTERFACE_XS
2090 slp->sl_osdep_fp = &scsi_low_osdep_funcs_xs;
2091 #endif /* SCSI_LOW_INTERFACE_XS */
2092 #ifdef SCSI_LOW_INTERFACE_CAM
2093 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
2094 #endif /* SCSI_LOW_INTERFACE_CAM */
2096 if (slp->sl_osdep_fp == NULL)
2097 panic("scsi_low: interface not spcified");
2099 if (ntargs > SCSI_LOW_NTARGETS)
2101 printf("scsi_low: %d targets are too large\n", ntargs);
2102 printf("change kernel options SCSI_LOW_NTARGETS");
2107 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
2109 slp->sl_openings = openings;
2110 slp->sl_ntargs = ntargs;
2111 slp->sl_nluns = nluns;
2112 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
2114 if (lunsize < sizeof(struct lun_info))
2115 lunsize = sizeof(struct lun_info);
2117 if (targsize < sizeof(struct targ_info))
2118 targsize = sizeof(struct targ_info);
2120 slp->sl_targsize = targsize;
2121 for (i = 0; i < ntargs; i ++)
2123 ti = scsi_low_alloc_ti(slp, i);
2124 ti->ti_lunsize = lunsize;
2125 li = scsi_low_alloc_li(ti, 0, 1);
2128 /* initialize queue */
2129 nccb = openings * ntargs;
2130 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
2131 nccb = SCSI_LOW_NCCB;
2132 scsi_low_init_ccbque(nccb);
2133 TAILQ_INIT(&slp->sl_start);
2135 /* call os depend attach */
2136 s = SCSI_LOW_SPLSCSI();
2137 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
2141 printf("%s: scsi_low_attach: osdep attach failed\n",
2146 /* check hardware */
2147 SCSI_LOW_DELAY(1000); /* wait for 1ms */
2148 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
2151 printf("%s: scsi_low_attach: initialization failed\n",
2156 /* start watch dog */
2157 slp->sl_timeout_count = 0;
2158 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2159 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
2160 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
2163 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
2165 #ifdef SCSI_LOW_START_UP_CHECK
2166 /* probing devices */
2167 scsi_low_start_up(slp);
2168 #endif /* SCSI_LOW_START_UP_CHECK */
2170 /* call os depend attach done*/
2171 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
2177 scsi_low_dettach(slp)
2178 struct scsi_low_softc *slp;
2182 s = SCSI_LOW_SPLSCSI();
2183 if (scsi_low_is_busy(slp) != 0)
2189 scsi_low_deactivate(slp);
2191 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
2198 scsi_low_free_ti(slp);
2199 LIST_REMOVE(slp, sl_chain);
2204 /**************************************************************
2206 **************************************************************/
2208 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
2209 struct scsi_low_softc *slp;
2210 struct targ_info *ti;
2211 struct lun_info *li;
2219 scsi_low_ccb_message_assert(cb, msg);
2221 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
2222 scsi_low_alloc_qtag(cb);
2224 cb->ccb_flags = flags | CCB_STARTQ;
2225 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2226 cb->ccb_error |= PENDINGIO;
2228 if ((flags & CCB_URGENT) != 0)
2230 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2234 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
2239 if (slp->sl_Tnexus == NULL)
2240 scsi_low_start(slp);
2245 scsi_low_message_enqueue(slp, ti, li, flags)
2246 struct scsi_low_softc *slp;
2247 struct targ_info *ti;
2248 struct lun_info *li;
2254 tmsgflags = ti->ti_setup_msg;
2255 ti->ti_setup_msg = 0;
2257 flags |= CCB_NORETRY;
2258 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
2263 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
2267 /**************************************************************
2268 * Generic Start & Done
2269 **************************************************************/
2270 #define SLSC_MODE_SENSE_SHORT 0x1a
2271 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
2272 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
2273 sizeof(struct scsi_low_mode_sense_data), 0};
2274 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
2275 sizeof(struct scsi_low_inq_data), 0};
2276 static u_int8_t unit_ready_cmd[6];
2277 static int scsi_low_setup_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2278 static int scsi_low_sense_abort_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
2279 static int scsi_low_resume (struct scsi_low_softc *);
2282 scsi_low_unit_ready_cmd(cb)
2286 cb->ccb_scp.scp_cmd = unit_ready_cmd;
2287 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2288 cb->ccb_scp.scp_datalen = 0;
2289 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2294 scsi_low_sense_abort_start(slp, ti, li, cb)
2295 struct scsi_low_softc *slp;
2296 struct targ_info *ti;
2297 struct lun_info *li;
2301 cb->ccb_scp.scp_cmdlen = 6;
2302 SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
2303 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
2304 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
2305 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
2306 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
2307 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
2308 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2310 scsi_low_ccb_message_clear(cb);
2311 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
2313 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2317 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
2318 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
2319 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
2320 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
2323 return SCSI_LOW_START_NO_QTAG;
2327 scsi_low_setup_start(slp, ti, li, cb)
2328 struct scsi_low_softc *slp;
2329 struct targ_info *ti;
2330 struct lun_info *li;
2334 switch(li->li_state)
2336 case SCSI_LOW_LUN_SLEEP:
2337 scsi_low_unit_ready_cmd(cb);
2340 case SCSI_LOW_LUN_START:
2341 cb->ccb_scp.scp_cmd = ss_cmd;
2342 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
2343 cb->ccb_scp.scp_datalen = 0;
2344 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2348 case SCSI_LOW_LUN_INQ:
2349 cb->ccb_scp.scp_cmd = inq_cmd;
2350 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
2351 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
2352 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
2353 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2357 case SCSI_LOW_LUN_MODEQ:
2358 cb->ccb_scp.scp_cmd = sms_cmd;
2359 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
2360 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
2361 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
2362 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
2364 return SCSI_LOW_START_QTAG;
2367 panic("%s: no setup phase", slp->sl_xname);
2370 return SCSI_LOW_START_NO_QTAG;
2374 scsi_low_resume(slp)
2375 struct scsi_low_softc *slp;
2378 if (slp->sl_flags & HW_RESUME)
2380 slp->sl_flags &= ~HW_POWDOWN;
2381 if (slp->sl_funcs->scsi_low_power != NULL)
2383 slp->sl_flags |= HW_RESUME;
2385 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
2386 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
2387 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
2388 SCSI_LOW_TIMEOUT_START);
2396 struct scsi_low_softc *slp;
2398 struct targ_info *ti;
2399 struct lun_info *li;
2403 /* check hardware exists or under initializations ? */
2404 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
2407 /* check hardware power up ? */
2408 if ((slp->sl_flags & HW_POWERCTRL) != 0)
2411 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
2413 if (scsi_low_resume(slp) == EJUSTRETURN)
2419 #ifdef SCSI_LOW_DIAGNOSTIC
2420 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
2422 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
2423 panic("%s: inconsistent", slp->sl_xname);
2425 #endif /* SCSI_LOW_DIAGNOSTIC */
2427 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
2428 cb = TAILQ_NEXT(cb, ccb_chain))
2432 if (li->li_disc == 0)
2434 goto scsi_low_cmd_start;
2436 else if (li->li_nqio > 0)
2438 if (li->li_nqio < li->li_maxnqio ||
2439 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2440 goto scsi_low_cmd_start;
2446 cb->ccb_flags &= ~CCB_STARTQ;
2447 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
2450 /* clear all error flag bits (for restart) */
2452 cb->ccb_datalen = -1;
2453 cb->ccb_scp.scp_status = ST_UNKNOWN;
2455 /* setup nexus pointer */
2456 slp->sl_Qnexus = cb;
2457 slp->sl_Lnexus = li;
2458 slp->sl_Tnexus = ti;
2460 /* initialize msgsys */
2461 scsi_low_init_msgsys(slp, ti);
2464 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2466 /* CA state or forced abort */
2467 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
2469 else if (li->li_state >= SCSI_LOW_LUN_OK)
2471 cb->ccb_flags &= ~CCB_INTERNAL;
2472 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
2473 if (cb->ccb_msgoutflag != 0)
2475 scsi_low_ccb_message_exec(slp, cb);
2480 cb->ccb_flags |= CCB_INTERNAL;
2481 rv = scsi_low_setup_start(slp, ti, li, cb);
2485 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
2487 if (rv == SCSI_LOW_START_QTAG &&
2488 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
2493 scsi_low_activate_qtag(cb);
2494 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2495 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
2496 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
2497 else if ((cb->ccb_flags & CCB_URGENT) != 0)
2498 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
2500 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
2501 scsi_low_assert_msg(slp, ti, qmsg, 0);
2505 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
2506 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
2507 cb->ccb_tc = cb->ccb_tcmax;
2509 /* setup saved scsi data pointer */
2510 cb->ccb_sscp = cb->ccb_scp;
2512 /* setup current scsi pointer */
2513 slp->sl_scp = cb->ccb_sscp;
2514 slp->sl_error = cb->ccb_error;
2516 /* assert always an identify msg */
2517 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
2520 #ifdef SCSI_LOW_DIAGNOSTIC
2521 scsi_low_msg_log_init(&ti->ti_log_msgin);
2522 scsi_low_msg_log_init(&ti->ti_log_msgout);
2523 #endif /* SCSI_LOW_DIAGNOSTIC */
2525 /* selection start */
2527 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
2528 if (rv == SCSI_LOW_START_OK)
2530 #ifdef SCSI_LOW_STATICS
2531 scsi_low_statics.nexus_win ++;
2532 #endif /* SCSI_LOW_STATICS */
2536 scsi_low_arbit_fail(slp, cb);
2537 #ifdef SCSI_LOW_STATICS
2538 scsi_low_statics.nexus_fail ++;
2539 #endif /* SCSI_LOW_STATICS */
2543 scsi_low_arbit_fail(slp, cb)
2544 struct scsi_low_softc *slp;
2547 struct targ_info *ti = cb->ti;
2549 scsi_low_deactivate_qtag(cb);
2550 scsi_low_ccb_message_retry(cb);
2551 cb->ccb_flags |= CCB_STARTQ;
2552 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2554 scsi_low_bus_release(slp, ti);
2557 if (slp->sl_disc == 0)
2559 #ifdef SCSI_LOW_DIAGNOSTIC
2560 printf("%s: try selection again\n", slp->sl_xname);
2561 #endif /* SCSI_LOW_DIAGNOSTIC */
2562 slp->sl_retry_sel = 1;
2567 scsi_low_bus_release(slp, ti)
2568 struct scsi_low_softc *slp;
2569 struct targ_info *ti;
2572 if (ti->ti_disc > 0)
2574 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
2578 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
2581 /* clear all nexus pointer */
2582 slp->sl_Qnexus = NULL;
2583 slp->sl_Lnexus = NULL;
2584 slp->sl_Tnexus = NULL;
2586 /* clear selection assert */
2587 slp->sl_selid = NULL;
2589 /* clear nexus data */
2590 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
2592 /* clear phase change counter */
2593 slp->sl_ph_count = 0;
2597 scsi_low_setup_done(slp, cb)
2598 struct scsi_low_softc *slp;
2601 struct targ_info *ti;
2602 struct lun_info *li;
2607 if (cb->ccb_rcnt >= slp->sl_max_retry)
2609 cb->ccb_error |= ABORTIO;
2610 return SCSI_LOW_DONE_COMPLETE;
2613 /* XXX: special huck for selection timeout */
2614 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
2615 (cb->ccb_error & SELTIMEOUTIO) != 0)
2617 cb->ccb_error |= ABORTIO;
2618 return SCSI_LOW_DONE_COMPLETE;
2621 switch(li->li_state)
2623 case SCSI_LOW_LUN_INQ:
2624 if (cb->ccb_error != 0)
2627 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2631 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2633 else if ((li->li_inq.sd_version & 7) >= 2 ||
2634 (li->li_inq.sd_len >= 4))
2636 if ((li->li_inq.sd_support & 0x2) == 0)
2637 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2638 if ((li->li_inq.sd_support & 0x8) == 0)
2639 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2642 if ((li->li_inq.sd_support & 0x10) == 0)
2643 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2644 if ((li->li_inq.sd_support & 0x20) == 0)
2645 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2646 if ((li->li_inq.sd_support & 0x40) == 0)
2647 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2652 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2655 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2657 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2659 scsi_low_calcf_target(ti);
2660 scsi_low_calcf_lun(li);
2663 case SCSI_LOW_LUN_MODEQ:
2664 if (cb->ccb_error != 0)
2666 if (cb->ccb_error & SENSEIO)
2668 #ifdef SCSI_LOW_DEBUG
2669 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2671 printf("SENSE: [%x][%x][%x][%x][%x]\n",
2672 (u_int) cb->ccb_sense.error_code,
2673 (u_int) cb->ccb_sense.segment,
2674 (u_int) cb->ccb_sense.flags,
2675 (u_int) cb->ccb_sense.add_sense_code,
2676 (u_int) cb->ccb_sense.add_sense_code_qual);
2678 #endif /* SCSI_LOW_DEBUG */
2682 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2685 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2687 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2688 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2690 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2691 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2692 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2694 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2695 scsi_low_calcf_lun(li);
2703 if (li->li_state == SCSI_LOW_LUN_OK)
2705 scsi_low_calcf_target(ti);
2706 scsi_low_calcf_lun(li);
2707 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2708 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2710 scsi_low_calcf_show(li);
2715 return SCSI_LOW_DONE_RETRY;
2719 scsi_low_done(slp, cb)
2720 struct scsi_low_softc *slp;
2725 if (cb->ccb_error == 0)
2727 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2729 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2731 * SCSI-2 draft suggests
2732 * page 0x0a QErr bit determins if
2733 * the target aborts or continues
2734 * the queueing io's after CA state resolved.
2735 * However many targets seem not to support
2736 * the page 0x0a. Thus we should manually clear the
2737 * queuing io's after CA state.
2739 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2742 cb->ccb_flags |= CCB_CLEARQ;
2745 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2747 if ((cb->ccb_flags & CCB_SENSE) != 0)
2748 cb->ccb_error |= (SENSEIO | ABORTIO);
2749 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2751 else switch (cb->ccb_sscp.scp_status)
2757 if (cb->ccb_datalen == 0 ||
2758 cb->ccb_scp.scp_datalen == 0)
2761 if (cb->ccb_scp.scp_cmdlen > 0 &&
2762 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2763 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2766 cb->ccb_error |= PDMAERR;
2771 cb->ccb_error |= (BUSYERR | STATERR);
2775 cb->ccb_error |= (STATERR | ABORTIO);
2780 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2783 cb->ccb_flags |= CCB_SENSE;
2786 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2791 cb->ccb_error |= FATALIO;
2797 if (cb->ccb_flags & CCB_SENSE)
2799 cb->ccb_error |= (SENSEERR | ABORTIO);
2801 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2805 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2807 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2811 /* check a ccb msgout flag */
2812 if (cb->ccb_omsgoutflag != 0)
2814 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2815 SCSI_LOW_MSG_ABORT_QTAG | \
2816 SCSI_LOW_MSG_CLEAR_QTAG | \
2817 SCSI_LOW_MSG_TERMIO)
2819 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2821 cb->ccb_error |= ABORTIO;
2825 /* call OS depend done */
2826 if (cb->osdep != NULL)
2828 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2829 if (rv == EJUSTRETURN)
2832 else if (cb->ccb_error != 0)
2834 if (cb->ccb_rcnt >= slp->sl_max_retry)
2835 cb->ccb_error |= ABORTIO;
2837 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2838 (cb->ccb_error & ABORTIO) == 0)
2842 /* free our target */
2843 #ifdef SCSI_LOW_DEBUG
2844 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2846 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2847 scsi_low_print(slp, NULL);
2849 #endif /* SCSI_LOW_DEBUG */
2851 scsi_low_deactivate_qtag(cb);
2852 scsi_low_dealloc_qtag(cb);
2853 scsi_low_free_ccb(cb);
2855 return SCSI_LOW_DONE_COMPLETE;
2858 #ifdef SCSI_LOW_DEBUG
2859 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2861 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2862 scsi_low_print(slp, NULL);
2864 #endif /* SCSI_LOW_DEBUG */
2867 scsi_low_deactivate_qtag(cb);
2868 scsi_low_ccb_message_retry(cb);
2869 return SCSI_LOW_DONE_RETRY;
2872 /**************************************************************
2874 **************************************************************/
2876 scsi_low_reset_nexus_target(slp, ti, fdone)
2877 struct scsi_low_softc *slp;
2878 struct targ_info *ti;
2881 struct lun_info *li;
2883 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2884 li = LIST_NEXT(li, lun_chain))
2886 scsi_low_reset_nexus_lun(slp, li, fdone);
2887 li->li_state = SCSI_LOW_LUN_SLEEP;
2892 ti->ti_setup_msg = 0;
2893 ti->ti_setup_msg_done = 0;
2895 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2896 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2898 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2899 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2901 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2903 ((*slp->sl_funcs->scsi_low_targ_init)
2904 (slp, ti, SCSI_LOW_INFO_REVOKE));
2906 scsi_low_calcf_target(ti);
2908 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2909 li = LIST_NEXT(li, lun_chain))
2913 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2914 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2916 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2918 ((*slp->sl_funcs->scsi_low_lun_init)
2919 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2921 scsi_low_calcf_lun(li);
2926 scsi_low_reset_nexus(slp, fdone)
2927 struct scsi_low_softc *slp;
2930 struct targ_info *ti;
2931 struct slccb *cb, *topcb;
2933 if ((cb = slp->sl_Qnexus) != NULL)
2935 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2942 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2943 ti = TAILQ_NEXT(ti, ti_chain))
2945 scsi_low_reset_nexus_target(slp, ti, fdone);
2946 scsi_low_bus_release(slp, ti);
2947 scsi_low_init_msgsys(slp, ti);
2952 topcb->ccb_flags |= CCB_STARTQ;
2953 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2957 slp->sl_retry_sel = 0;
2958 slp->sl_flags &= ~HW_PDMASTART;
2963 static char tw_chars[] = "|/-\\";
2964 #define TWIDDLEWAIT 10000
2967 scsi_low_twiddle_wait(void)
2971 cnputc(tw_chars[tw_pos++]);
2972 tw_pos %= (sizeof(tw_chars) - 1);
2973 SCSI_LOW_DELAY(TWIDDLEWAIT);
2977 scsi_low_bus_reset(slp)
2978 struct scsi_low_softc *slp;
2982 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2984 printf("%s: try to reset scsi bus ", slp->sl_xname);
2985 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2986 scsi_low_twiddle_wait();
2992 scsi_low_restart(slp, flags, s)
2993 struct scsi_low_softc *slp;
3000 printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
3002 if ((error = scsi_low_init(slp, flags)) != 0)
3005 scsi_low_start(slp);
3009 /**************************************************************
3010 * disconnect and reselect
3011 **************************************************************/
3012 #define MSGCMD_LUN(msg) (msg & 0x07)
3014 static struct slccb *
3015 scsi_low_establish_ccb(ti, li, tag)
3016 struct targ_info *ti;
3017 struct lun_info *li;
3020 struct scsi_low_softc *slp = ti->ti_sc;
3026 cb = TAILQ_FIRST(&li->li_discq);
3027 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
3028 if (cb->ccb_tag == tag)
3033 * establish our ccb nexus
3036 #ifdef SCSI_LOW_DEBUG
3037 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3039 printf("%s: nexus(0x%lx) abort check start\n",
3040 slp->sl_xname, (u_long) cb);
3041 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
3042 scsi_low_revoke_ccb(slp, cb, 1);
3046 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
3048 if (cb->ccb_omsgoutflag == 0)
3049 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
3051 #endif /* SCSI_LOW_DEBUG */
3053 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3054 cb->ccb_flags &= ~CCB_DISCQ;
3055 slp->sl_Qnexus = cb;
3057 slp->sl_scp = cb->ccb_sscp;
3058 slp->sl_error |= cb->ccb_error;
3064 /* inform "ccb nexus established" to the host driver */
3065 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3068 if (cb->ccb_msgoutflag != 0)
3070 scsi_low_ccb_message_exec(slp, cb);
3077 scsi_low_reselected(slp, targ)
3078 struct scsi_low_softc *slp;
3081 struct targ_info *ti;
3086 * Check select vs reselected collision.
3089 if ((cb = slp->sl_selid) != NULL)
3091 scsi_low_arbit_fail(slp, cb);
3092 #ifdef SCSI_LOW_STATICS
3093 scsi_low_statics.nexus_conflict ++;
3094 #endif /* SCSI_LOW_STATICS */
3098 * Check if no current active nexus.
3100 if (slp->sl_Tnexus != NULL)
3107 * Check a valid target id asserted ?
3109 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
3111 s = "scsi id illegal";
3116 * Check the target scsi status.
3118 ti = slp->sl_ti[targ];
3119 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
3121 s = "phase mismatch";
3129 scsi_low_init_msgsys(slp, ti);
3132 * Establish our target nexus
3134 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
3135 slp->sl_Tnexus = ti;
3136 #ifdef SCSI_LOW_STATICS
3137 scsi_low_statics.nexus_reselected ++;
3138 #endif /* SCSI_LOW_STATICS */
3142 printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
3143 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
3144 "reselect: scsi world confused");
3148 /**************************************************************
3149 * cmd out pointer setup
3150 **************************************************************/
3152 scsi_low_cmd(slp, ti)
3153 struct scsi_low_softc *slp;
3154 struct targ_info *ti;
3156 struct slccb *cb = slp->sl_Qnexus;
3158 slp->sl_ph_count ++;
3164 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
3165 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
3166 slp->sl_scp.scp_datalen = 0;
3167 slp->sl_scp.scp_direction = SCSI_LOW_READ;
3168 slp->sl_error |= FATALIO;
3169 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3170 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
3175 #ifdef SCSI_LOW_DEBUG
3176 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
3178 scsi_low_test_cmdlnk(slp, cb);
3180 #endif /* SCSI_LOW_DEBUG */
3185 /**************************************************************
3186 * data out pointer setup
3187 **************************************************************/
3189 scsi_low_data(slp, ti, bp, direction)
3190 struct scsi_low_softc *slp;
3191 struct targ_info *ti;
3195 struct slccb *cb = slp->sl_Qnexus;
3197 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
3203 slp->sl_error |= (FATALIO | PDMAERR);
3204 slp->sl_scp.scp_datalen = 0;
3205 slp->sl_scp.scp_direction = direction;
3206 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3207 if (ti->ti_ophase != ti->ti_phase)
3212 s = "DATA PHASE: ccb nexus not found";
3214 s = "DATA PHASE: xfer direction mismatch";
3215 SCSI_LOW_INFO(slp, ti, s);
3222 /**************************************************************
3224 **************************************************************/
3225 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
3226 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
3227 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
3228 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
3229 #define MSGIN_DATA_LAST 0x30
3231 static int scsi_low_errfunc_synch (struct scsi_low_softc *, u_int);
3232 static int scsi_low_errfunc_wide (struct scsi_low_softc *, u_int);
3233 static int scsi_low_errfunc_identify (struct scsi_low_softc *, u_int);
3234 static int scsi_low_errfunc_qtag (struct scsi_low_softc *, u_int);
3236 static int scsi_low_msgfunc_synch (struct scsi_low_softc *);
3237 static int scsi_low_msgfunc_wide (struct scsi_low_softc *);
3238 static int scsi_low_msgfunc_identify (struct scsi_low_softc *);
3239 static int scsi_low_msgfunc_abort (struct scsi_low_softc *);
3240 static int scsi_low_msgfunc_qabort (struct scsi_low_softc *);
3241 static int scsi_low_msgfunc_qtag (struct scsi_low_softc *);
3242 static int scsi_low_msgfunc_reset (struct scsi_low_softc *);
3244 struct scsi_low_msgout_data {
3247 int (*md_msgfunc) (struct scsi_low_softc *);
3248 int (*md_errfunc) (struct scsi_low_softc *, u_int);
3249 #define MSG_RELEASE_ATN 0x0001
3253 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
3254 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
3255 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
3256 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
3257 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
3258 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
3259 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3260 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
3261 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3262 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3263 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
3264 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
3265 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
3266 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
3267 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
3268 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
3269 /* 15 */{SCSI_LOW_MSG_ALL, 0},
3272 static int scsi_low_msginfunc_ext (struct scsi_low_softc *);
3273 static int scsi_low_synch (struct scsi_low_softc *);
3274 static int scsi_low_wide (struct scsi_low_softc *);
3275 static int scsi_low_msginfunc_msg_reject (struct scsi_low_softc *);
3276 static int scsi_low_msginfunc_rejop (struct scsi_low_softc *);
3277 static int scsi_low_msginfunc_rp (struct scsi_low_softc *);
3278 static int scsi_low_msginfunc_sdp (struct scsi_low_softc *);
3279 static int scsi_low_msginfunc_disc (struct scsi_low_softc *);
3280 static int scsi_low_msginfunc_cc (struct scsi_low_softc *);
3281 static int scsi_low_msginfunc_lcc (struct scsi_low_softc *);
3282 static int scsi_low_msginfunc_parity (struct scsi_low_softc *);
3283 static int scsi_low_msginfunc_noop (struct scsi_low_softc *);
3284 static int scsi_low_msginfunc_simple_qtag (struct scsi_low_softc *);
3285 static int scsi_low_msginfunc_i_wide_residue (struct scsi_low_softc *);
3287 struct scsi_low_msgin_data {
3289 int (*md_msgfunc) (struct scsi_low_softc *);
3292 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
3293 /* 0 */ {1, scsi_low_msginfunc_cc},
3294 /* 1 */ {2, scsi_low_msginfunc_ext},
3295 /* 2 */ {1, scsi_low_msginfunc_sdp},
3296 /* 3 */ {1, scsi_low_msginfunc_rp},
3297 /* 4 */ {1, scsi_low_msginfunc_disc},
3298 /* 5 */ {1, scsi_low_msginfunc_rejop},
3299 /* 6 */ {1, scsi_low_msginfunc_rejop},
3300 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
3301 /* 8 */ {1, scsi_low_msginfunc_noop},
3302 /* 9 */ {1, scsi_low_msginfunc_parity},
3303 /* a */ {1, scsi_low_msginfunc_lcc},
3304 /* b */ {1, scsi_low_msginfunc_lcc},
3305 /* c */ {1, scsi_low_msginfunc_rejop},
3306 /* d */ {2, scsi_low_msginfunc_rejop},
3307 /* e */ {1, scsi_low_msginfunc_rejop},
3308 /* f */ {1, scsi_low_msginfunc_rejop},
3309 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
3310 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
3311 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
3312 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
3313 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
3314 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
3315 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
3316 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
3317 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
3318 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
3319 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
3320 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
3321 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
3322 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
3323 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
3324 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
3325 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
3326 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
3327 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
3328 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
3329 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
3330 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
3331 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
3332 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
3333 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
3334 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
3335 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
3336 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
3337 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
3338 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
3339 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
3340 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
3341 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
3344 /**************************************************************
3346 **************************************************************/
3348 scsi_low_msgfunc_synch(slp)
3349 struct scsi_low_softc *slp;
3351 struct targ_info *ti = slp->sl_Tnexus;
3352 int ptr = ti->ti_msgoutlen;
3354 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
3355 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
3356 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
3357 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
3358 return MSG_EXTEND_SYNCHLEN + 2;
3362 scsi_low_msgfunc_wide(slp)
3363 struct scsi_low_softc *slp;
3365 struct targ_info *ti = slp->sl_Tnexus;
3366 int ptr = ti->ti_msgoutlen;
3368 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
3369 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
3370 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
3371 return MSG_EXTEND_WIDELEN + 2;
3375 scsi_low_msgfunc_identify(slp)
3376 struct scsi_low_softc *slp;
3378 struct targ_info *ti = slp->sl_Tnexus;
3379 struct lun_info *li = slp->sl_Lnexus;
3380 struct slccb *cb = slp->sl_Qnexus;
3381 int ptr = ti->ti_msgoutlen;
3387 slp->sl_error |= FATALIO;
3388 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3389 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
3393 if (scsi_low_is_disconnect_ok(cb) != 0)
3394 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
3398 if (ti->ti_phase == PH_MSGOUT)
3400 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3401 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
3403 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3407 ti->ti_msgoutstr[ptr + 0] = msg;
3412 scsi_low_msgfunc_abort(slp)
3413 struct scsi_low_softc *slp;
3416 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
3421 scsi_low_msgfunc_qabort(slp)
3422 struct scsi_low_softc *slp;
3425 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
3430 scsi_low_msgfunc_reset(slp)
3431 struct scsi_low_softc *slp;
3434 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
3439 scsi_low_msgfunc_qtag(slp)
3440 struct scsi_low_softc *slp;
3442 struct targ_info *ti = slp->sl_Tnexus;
3443 struct slccb *cb = slp->sl_Qnexus;
3444 int ptr = ti->ti_msgoutlen;
3446 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
3448 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
3453 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
3454 if (ti->ti_phase == PH_MSGOUT)
3456 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
3463 * The following functions are called when targets give unexpected
3464 * responces in msgin (after msgout).
3467 scsi_low_errfunc_identify(slp, msgflags)
3468 struct scsi_low_softc *slp;
3472 if (slp->sl_Lnexus != NULL)
3474 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
3475 scsi_low_calcf_lun(slp->sl_Lnexus);
3481 scsi_low_errfunc_synch(slp, msgflags)
3482 struct scsi_low_softc *slp;
3485 struct targ_info *ti = slp->sl_Tnexus;
3487 MSGIN_PERIOD(ti) = 0;
3488 MSGIN_OFFSET(ti) = 0;
3489 scsi_low_synch(slp);
3494 scsi_low_errfunc_wide(slp, msgflags)
3495 struct scsi_low_softc *slp;
3498 struct targ_info *ti = slp->sl_Tnexus;
3500 MSGIN_WIDTHP(ti) = 0;
3506 scsi_low_errfunc_qtag(slp, msgflags)
3507 struct scsi_low_softc *slp;
3511 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
3513 if (slp->sl_Qnexus != NULL)
3515 scsi_low_deactivate_qtag(slp->sl_Qnexus);
3517 if (slp->sl_Lnexus != NULL)
3519 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
3520 scsi_low_calcf_lun(slp->sl_Lnexus);
3522 printf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
3529 scsi_low_msgout(slp, ti, fl)
3530 struct scsi_low_softc *slp;
3531 struct targ_info *ti;
3534 struct scsi_low_msgout_data *mdp;
3537 #ifdef SCSI_LOW_DIAGNOSTIC
3538 if (ti != slp->sl_Tnexus)
3540 scsi_low_print(slp, NULL);
3541 panic("scsi_low_msgout: Target nexus inconsistent");
3543 #endif /* SCSI_LOW_DIAGNOSTIC */
3545 slp->sl_ph_count ++;
3546 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3548 printf("%s: too many phase changes\n", slp->sl_xname);
3549 slp->sl_error |= FATALIO;
3550 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3554 * Scsi phase changes.
3555 * Previously msgs asserted are accepted by our target or
3556 * processed by scsi_low_msgin.
3557 * Thus clear all saved informations.
3559 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
3561 ti->ti_omsgflags = 0;
3562 ti->ti_emsgflags = 0;
3564 else if (slp->sl_atten == 0)
3567 * We did not assert attention, however still our target required
3568 * msgs. Resend previous msgs.
3570 ti->ti_msgflags |= ti->ti_omsgflags;
3571 ti->ti_omsgflags = 0;
3572 #ifdef SCSI_LOW_DIAGNOSTIC
3573 printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
3574 #endif /* SCSI_LOW_DIAGNOSTIC */
3578 * We have no msgs. send MSG_NOOP (OK?)
3580 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3581 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
3586 ti->ti_msgoutlen = 0;
3587 slp->sl_clear_atten = 0;
3588 mdp = &scsi_low_msgout_data[0];
3589 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3591 if ((ti->ti_msgflags & mdp->md_flags) != 0)
3593 ti->ti_omsgflags |= mdp->md_flags;
3594 ti->ti_msgflags &= ~mdp->md_flags;
3595 ti->ti_emsgflags = mdp->md_flags;
3597 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
3598 if (mdp->md_msgfunc != NULL)
3599 len = (*mdp->md_msgfunc) (slp);
3603 #ifdef SCSI_LOW_DIAGNOSTIC
3604 scsi_low_msg_log_write(&ti->ti_log_msgout,
3605 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
3606 #endif /* SCSI_LOW_DIAGNOSTIC */
3608 ti->ti_msgoutlen += len;
3609 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
3611 slp->sl_clear_atten = 1;
3615 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
3616 ti->ti_msgflags == 0)
3619 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3624 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3625 slp->sl_clear_atten = 1;
3627 return ti->ti_msgoutlen;
3630 /**************************************************************
3632 **************************************************************/
3634 scsi_low_msginfunc_noop(slp)
3635 struct scsi_low_softc *slp;
3642 scsi_low_msginfunc_rejop(slp)
3643 struct scsi_low_softc *slp;
3645 struct targ_info *ti = slp->sl_Tnexus;
3646 u_int8_t msg = ti->ti_msgin[0];
3648 printf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
3649 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3654 scsi_low_msginfunc_cc(slp)
3655 struct scsi_low_softc *slp;
3657 struct lun_info *li;
3659 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3661 /* validate status */
3662 if (slp->sl_Qnexus == NULL)
3665 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3666 li = slp->sl_Lnexus;
3667 switch (slp->sl_scp.scp_status)
3670 li->li_maxnqio = li->li_maxnexus;
3675 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3676 scsi_low_reset_nexus_lun(slp, li, 0);
3684 if (li->li_maxnexus >= li->li_nqio)
3685 li->li_maxnexus = li->li_nqio - 1;
3686 li->li_maxnqio = li->li_maxnexus;
3691 slp->sl_error |= MSGERR;
3701 scsi_low_msginfunc_lcc(slp)
3702 struct scsi_low_softc *slp;
3704 struct targ_info *ti;
3705 struct lun_info *li;
3706 struct slccb *ncb, *cb;
3708 ti = slp->sl_Tnexus;
3709 li = slp->sl_Lnexus;
3710 if ((cb = slp->sl_Qnexus) == NULL)
3713 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3714 switch (slp->sl_scp.scp_status)
3718 li->li_maxnqio = li->li_maxnexus;
3722 slp->sl_error |= MSGERR;
3726 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3729 cb->ccb_error |= slp->sl_error;
3730 if (cb->ccb_error != 0)
3733 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3734 ncb = TAILQ_NEXT(ncb, ccb_chain))
3737 goto cmd_link_start;
3742 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3743 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3747 ncb->ccb_flags &= ~CCB_STARTQ;
3748 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3750 scsi_low_dealloc_qtag(ncb);
3751 ncb->ccb_tag = cb->ccb_tag;
3752 ncb->ccb_otag = cb->ccb_otag;
3753 cb->ccb_tag = SCSI_LOW_UNKTAG;
3754 cb->ccb_otag = SCSI_LOW_UNKTAG;
3755 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3756 panic("%s: linked ccb retried", slp->sl_xname);
3758 slp->sl_Qnexus = ncb;
3759 slp->sl_ph_count = 0;
3762 ncb->ccb_datalen = -1;
3763 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3764 ncb->ccb_flags &= ~CCB_INTERNAL;
3766 scsi_low_init_msgsys(slp, ti);
3768 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3770 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3771 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3772 ncb->ccb_tc = ncb->ccb_tcmax;
3774 /* setup saved scsi data pointer */
3775 ncb->ccb_sscp = ncb->ccb_scp;
3776 slp->sl_scp = ncb->ccb_sscp;
3777 slp->sl_error = ncb->ccb_error;
3779 #ifdef SCSI_LOW_DIAGNOSTIC
3780 scsi_low_msg_log_init(&ti->ti_log_msgin);
3781 scsi_low_msg_log_init(&ti->ti_log_msgout);
3782 #endif /* SCSI_LOW_DIAGNOSTIC */
3787 scsi_low_msginfunc_disc(slp)
3788 struct scsi_low_softc *slp;
3791 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3796 scsi_low_msginfunc_sdp(slp)
3797 struct scsi_low_softc *slp;
3799 struct slccb *cb = slp->sl_Qnexus;
3803 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3804 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3807 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3812 scsi_low_msginfunc_rp(slp)
3813 struct scsi_low_softc *slp;
3816 if (slp->sl_Qnexus != NULL)
3817 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3819 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3825 struct scsi_low_softc *slp;
3827 struct targ_info *ti = slp->sl_Tnexus;
3828 u_int period = 0, offset = 0, speed;
3832 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3833 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3834 MSGIN_OFFSET(ti) == 0)
3836 if ((offset = MSGIN_OFFSET(ti)) != 0)
3837 period = MSGIN_PERIOD(ti);
3838 s = offset ? "synchronous" : "async";
3843 * Target seems to be brain damaged.
3844 * Force async transfer.
3846 ti->ti_maxsynch.period = 0;
3847 ti->ti_maxsynch.offset = 0;
3848 printf("%s: target brain damaged. async transfer\n",
3853 ti->ti_maxsynch.period = period;
3854 ti->ti_maxsynch.offset = offset;
3856 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3860 * Current period and offset are not acceptable
3862 * The adapter changes max synch and max offset.
3864 printf("%s: synch neg failed. retry synch msg neg ...\n",
3869 ti->ti_osynch = ti->ti_maxsynch;
3872 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3876 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3878 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3879 struct slccb *cb = slp->sl_Qnexus;
3881 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3883 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3885 printf("%s(%d:*): <%s> offset %d period %dns ",
3886 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3890 speed = 1000 * 10 / (period * 4);
3891 printf("%d.%d M/s", speed / 10, speed % 10);
3900 struct scsi_low_softc *slp;
3902 struct targ_info *ti = slp->sl_Tnexus;
3905 ti->ti_width = MSGIN_WIDTHP(ti);
3906 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3910 * Current width is not acceptable for our adapter.
3911 * The adapter changes max width.
3913 printf("%s: wide neg failed. retry wide msg neg ...\n",
3918 ti->ti_owidth = ti->ti_width;
3919 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3921 ti->ti_setup_msg_done |=
3922 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3926 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3928 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3929 struct slccb *cb = slp->sl_Qnexus;
3931 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3933 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3935 printf("%s(%d:*): transfer width %d bits\n",
3936 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3942 scsi_low_msginfunc_simple_qtag(slp)
3943 struct scsi_low_softc *slp;
3945 struct targ_info *ti = slp->sl_Tnexus;
3946 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3948 if (slp->sl_Qnexus != NULL)
3950 if (slp->sl_Qnexus->ccb_tag != etag)
3952 slp->sl_error |= FATALIO;
3953 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3954 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3957 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3959 #ifdef SCSI_LOW_DEBUG
3960 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3962 #endif /* SCSI_LOW_DEBUG */
3964 slp->sl_error |= FATALIO;
3965 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3966 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3972 scsi_low_msginfunc_i_wide_residue(slp)
3973 struct scsi_low_softc *slp;
3975 struct targ_info *ti = slp->sl_Tnexus;
3976 struct slccb *cb = slp->sl_Qnexus;
3977 int res = (int) ti->ti_msgin[1];
3979 if (cb == NULL || res <= 0 ||
3980 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3981 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3984 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3987 slp->sl_scp.scp_datalen += res;
3988 slp->sl_scp.scp_data -= res;
3989 scsi_low_data_finish(slp);
3994 scsi_low_msginfunc_ext(slp)
3995 struct scsi_low_softc *slp;
3997 struct slccb *cb = slp->sl_Qnexus;
3998 struct lun_info *li = slp->sl_Lnexus;
3999 struct targ_info *ti = slp->sl_Tnexus;
4003 if (ti->ti_msginptr == 2)
4005 ti->ti_msginlen = ti->ti_msgin[1] + 2;
4009 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
4011 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
4015 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
4016 count = (int) htonl((long) (*ptr));
4017 if(slp->sl_scp.scp_datalen - count < 0 ||
4018 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
4021 slp->sl_scp.scp_datalen -= count;
4022 slp->sl_scp.scp_data += count;
4025 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
4029 retry = scsi_low_synch(slp);
4030 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
4031 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
4033 #ifdef SCSI_LOW_DEBUG
4034 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4036 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
4038 #endif /* SCSI_LOW_DEBUG */
4041 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
4045 retry = scsi_low_wide(slp);
4046 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
4047 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
4055 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4060 scsi_low_msginfunc_parity(slp)
4061 struct scsi_low_softc *slp;
4063 struct targ_info *ti = slp->sl_Tnexus;
4065 /* only I -> T, invalid! */
4066 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4071 scsi_low_msginfunc_msg_reject(slp)
4072 struct scsi_low_softc *slp;
4074 struct targ_info *ti = slp->sl_Tnexus;
4075 struct scsi_low_msgout_data *mdp;
4078 if (ti->ti_emsgflags != 0)
4080 printf("%s: msg flags [0x%x] rejected\n",
4081 slp->sl_xname, ti->ti_emsgflags);
4082 msgflags = SCSI_LOW_MSG_REJECT;
4083 mdp = &scsi_low_msgout_data[0];
4084 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
4086 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
4088 ti->ti_emsgflags &= ~mdp->md_flags;
4089 if (mdp->md_errfunc != NULL)
4090 (*mdp->md_errfunc) (slp, msgflags);
4098 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
4099 slp->sl_error |= MSGERR;
4105 scsi_low_msgin(slp, ti, c)
4106 struct scsi_low_softc *slp;
4107 struct targ_info *ti;
4110 struct scsi_low_msgin_data *sdp;
4111 struct lun_info *li;
4114 #ifdef SCSI_LOW_DIAGNOSTIC
4115 if (ti != slp->sl_Tnexus)
4117 scsi_low_print(slp, NULL);
4118 panic("scsi_low_msgin: Target nexus inconsistent");
4120 #endif /* SCSI_LOW_DIAGNOSTIC */
4123 * Phase changes, clear the pointer.
4125 if (ti->ti_ophase != ti->ti_phase)
4128 ti->ti_msgin_parity_error = 0;
4130 slp->sl_ph_count ++;
4131 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
4133 printf("%s: too many phase changes\n", slp->sl_xname);
4134 slp->sl_error |= FATALIO;
4135 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4140 * Store a current messages byte into buffer and
4141 * wait for the completion of the current msg.
4143 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
4144 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
4146 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
4147 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
4151 * Check parity errors.
4153 if ((c & SCSI_LOW_DATA_PE) != 0)
4155 ti->ti_msgin_parity_error ++;
4156 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
4160 if (ti->ti_msgin_parity_error != 0)
4164 * Calculate messages length.
4166 msg = ti->ti_msgin[0];
4167 if (msg < MSGIN_DATA_LAST)
4168 sdp = &scsi_low_msgin_data[msg];
4170 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
4172 if (ti->ti_msginlen == 0)
4174 ti->ti_msginlen = sdp->md_len;
4180 if (ti->ti_msginptr < ti->ti_msginlen)
4186 if ((msg & MSG_IDENTIFY) == 0)
4188 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
4193 li = slp->sl_Lnexus;
4196 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
4199 slp->sl_Lnexus = li;
4200 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
4204 if (MSGCMD_LUN(msg) != li->li_lun)
4208 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
4210 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
4212 #ifdef SCSI_LOW_DEBUG
4213 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
4217 #endif /* SCSI_LOW_DEBUG */
4225 * Msg process completed, reset msgin pointer and assert ATN if desired.
4228 slp->sl_error |= FATALIO;
4229 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
4230 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
4233 if (ti->ti_msginptr < ti->ti_msginlen)
4236 #ifdef SCSI_LOW_DIAGNOSTIC
4237 scsi_low_msg_log_write(&ti->ti_log_msgin,
4238 &ti->ti_msgin[0], ti->ti_msginlen);
4239 #endif /* SCSI_LOW_DIAGNOSTIC */
4245 /**********************************************************
4247 **********************************************************/
4249 scsi_low_disconnected(slp, ti)
4250 struct scsi_low_softc *slp;
4251 struct targ_info *ti;
4253 struct slccb *cb = slp->sl_Qnexus;
4255 /* check phase completion */
4256 switch (slp->sl_msgphase)
4259 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4260 scsi_low_msginfunc_cc(slp);
4261 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
4265 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4266 scsi_low_msginfunc_cc(slp);
4267 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
4271 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
4272 scsi_low_msginfunc_cc(slp);
4278 struct lun_info *li;
4281 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
4282 cb->ccb_flags |= CCB_DISCQ;
4283 cb->ccb_error |= slp->sl_error;
4289 #ifdef SCSI_LOW_STATICS
4290 scsi_low_statics.nexus_disconnected ++;
4291 #endif /* SCSI_LOW_STATICS */
4293 #ifdef SCSI_LOW_DEBUG
4294 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
4296 printf("## SCSI_LOW_DISCONNECTED ===============\n");
4297 scsi_low_print(slp, NULL);
4299 #endif /* SCSI_LOW_DEBUG */
4303 slp->sl_error |= FATALIO;
4304 if (ti->ti_phase == PH_SELSTART)
4305 slp->sl_error |= SELTIMEOUTIO;
4307 slp->sl_error |= UBFERR;
4316 #ifdef SCSI_LOW_DEBUG
4317 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
4319 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
4320 (cb->ccb_msgoutflag != 0 ||
4321 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
4323 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
4326 #endif /* SCSI_LOW_DEBUG */
4328 cb->ccb_error |= slp->sl_error;
4329 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
4331 cb->ccb_flags |= CCB_STARTQ;
4332 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
4337 scsi_low_bus_release(slp, ti);
4338 scsi_low_start(slp);
4342 /**********************************************************
4344 **********************************************************/
4346 scsi_low_alloc_qtag(cb)
4349 struct lun_info *li = cb->li;
4350 scsi_low_tag_t etag;
4352 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
4355 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4356 etag = ffs(li->li_qtagbits);
4360 li->li_qtagbits &= ~(1 << (etag - 1));
4361 cb->ccb_otag = etag;
4364 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4365 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
4366 if (li->li_qtagarray[li->li_qd] == 0)
4369 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
4370 if (li->li_qtagarray[li->li_qd] == 0)
4376 li->li_qtagarray[li->li_qd] ++;
4377 cb->ccb_otag = (li->li_qd ++);
4379 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4383 scsi_low_dealloc_qtag(cb)
4386 struct lun_info *li = cb->li;
4387 scsi_low_tag_t etag;
4389 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
4392 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
4393 etag = cb->ccb_otag - 1;
4394 #ifdef SCSI_LOW_DIAGNOSTIC
4395 if (etag >= sizeof(li->li_qtagbits) * NBBY)
4396 panic("scsi_low_dealloc_tag: illegal tag");
4397 #endif /* SCSI_LOW_DIAGNOSTIC */
4398 li->li_qtagbits |= (1 << etag);
4400 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4401 etag = cb->ccb_otag;
4402 #ifdef SCSI_LOW_DIAGNOSTIC
4403 if (etag >= SCSI_LOW_MAXNEXUS)
4404 panic("scsi_low_dealloc_tag: illegal tag");
4405 #endif /* SCSI_LOW_DIAGNOSTIC */
4406 li->li_qtagarray[etag] --;
4407 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
4409 cb->ccb_otag = SCSI_LOW_UNKTAG;
4414 scsi_low_revoke_ccb(slp, cb, fdone)
4415 struct scsi_low_softc *slp;
4419 struct targ_info *ti = cb->ti;
4420 struct lun_info *li = cb->li;
4422 #ifdef SCSI_LOW_DIAGNOSTIC
4423 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
4424 (CCB_STARTQ | CCB_DISCQ))
4426 panic("%s: ccb in both queue", slp->sl_xname);
4428 #endif /* SCSI_LOW_DIAGNOSTIC */
4430 if ((cb->ccb_flags & CCB_STARTQ) != 0)
4432 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
4435 if ((cb->ccb_flags & CCB_DISCQ) != 0)
4437 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
4443 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
4444 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
4447 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
4448 (cb->ccb_flags & CCB_NORETRY) != 0))
4450 cb->ccb_error |= FATALIO;
4451 cb->ccb_flags &= ~CCB_AUTOSENSE;
4452 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
4453 panic("%s: done ccb retried", slp->sl_xname);
4458 cb->ccb_error |= PENDINGIO;
4459 scsi_low_deactivate_qtag(cb);
4460 scsi_low_ccb_message_retry(cb);
4461 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
4467 scsi_low_reset_nexus_lun(slp, li, fdone)
4468 struct scsi_low_softc *slp;
4469 struct lun_info *li;
4472 struct slccb *cb, *ncb, *ecb;
4478 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
4480 ncb = TAILQ_NEXT(cb, ccb_chain);
4481 cb = scsi_low_revoke_ccb(slp, cb, fdone);
4485 * presumely keep ordering of io
4487 cb->ccb_flags |= CCB_STARTQ;
4490 TAILQ_INSERT_HEAD(&slp->sl_start,\
4495 TAILQ_INSERT_AFTER(&slp->sl_start,\
4496 ecb, cb, ccb_chain);
4503 /**************************************************************
4505 **************************************************************/
4507 scsi_low_calcf_lun(li)
4508 struct lun_info *li;
4510 struct targ_info *ti = li->li_ti;
4511 struct scsi_low_softc *slp = ti->ti_sc;
4512 u_int cfgflags, diskflags;
4514 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
4515 cfgflags = li->li_cfgflags;
4519 diskflags = li->li_diskflags & li->li_quirks;
4522 li->li_flags &= ~SCSI_LOW_DISC;
4523 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
4524 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
4525 (cfgflags & SCSI_LOW_DISC) != 0)
4526 li->li_flags |= SCSI_LOW_DISC;
4529 li->li_flags |= SCSI_LOW_NOPARITY;
4530 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
4531 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
4532 (cfgflags & SCSI_LOW_NOPARITY) == 0)
4533 li->li_flags &= ~SCSI_LOW_NOPARITY;
4536 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
4537 (cfgflags & SCSI_LOW_QTAG) != 0 &&
4538 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
4540 li->li_flags |= SCSI_LOW_QTAG;
4541 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
4542 li->li_maxnqio = li->li_maxnexus;
4546 li->li_flags &= ~SCSI_LOW_QTAG;
4547 li->li_maxnexus = 0;
4548 li->li_maxnqio = li->li_maxnexus;
4552 li->li_flags &= ~SCSI_LOW_LINK;
4553 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
4554 (diskflags & SCSI_LOW_DISK_LINK) != 0)
4555 li->li_flags |= SCSI_LOW_LINK;
4557 /* compatible flags */
4558 li->li_flags &= ~SCSI_LOW_SYNC;
4559 if (ti->ti_maxsynch.offset > 0)
4560 li->li_flags |= SCSI_LOW_SYNC;
4562 #ifdef SCSI_LOW_DEBUG
4563 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4565 scsi_low_calcf_show(li);
4567 #endif /* SCSI_LOW_DEBUG */
4571 scsi_low_calcf_target(ti)
4572 struct targ_info *ti;
4574 struct scsi_low_softc *slp = ti->ti_sc;
4575 u_int offset, period, diskflags;
4577 diskflags = ti->ti_diskflags & ti->ti_quirks;
4580 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
4581 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
4583 offset = ti->ti_maxsynch.offset;
4584 period = ti->ti_maxsynch.period;
4585 if (offset == 0 || period == 0)
4586 offset = period = 0;
4590 offset = period = 0;
4593 ti->ti_maxsynch.offset = offset;
4594 ti->ti_maxsynch.period = period;
4597 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
4598 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
4599 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
4601 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
4602 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
4603 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
4605 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
4607 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
4608 ti->ti_maxsynch.period != ti->ti_osynch.period)
4609 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
4610 if (ti->ti_width != ti->ti_owidth)
4611 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
4613 ti->ti_osynch = ti->ti_maxsynch;
4614 ti->ti_owidth = ti->ti_width;
4617 #ifdef SCSI_LOW_DEBUG
4618 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4620 printf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
4621 slp->sl_xname, ti->ti_id,
4622 ti->ti_maxsynch.period * 4,
4623 ti->ti_maxsynch.offset,
4626 #endif /* SCSI_LOW_DEBUG */
4630 scsi_low_calcf_show(li)
4631 struct lun_info *li;
4633 struct targ_info *ti = li->li_ti;
4634 struct scsi_low_softc *slp = ti->ti_sc;
4636 printf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4637 slp->sl_xname, ti->ti_id, li->li_lun,
4638 ti->ti_maxsynch.period * 4,
4639 ti->ti_maxsynch.offset,
4641 li->li_flags, SCSI_LOW_BITS);
4644 #ifdef SCSI_LOW_START_UP_CHECK
4645 /**************************************************************
4646 * scsi world start up
4647 **************************************************************/
4648 static int scsi_low_poll (struct scsi_low_softc *, struct slccb *);
4651 scsi_low_start_up(slp)
4652 struct scsi_low_softc *slp;
4654 struct targ_info *ti;
4655 struct lun_info *li;
4659 printf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
4661 for (target = 0; target < slp->sl_ntargs; target ++)
4663 if (target == slp->sl_hostid)
4665 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4667 printf("%s: scsi_low: target %d (host card)\n",
4668 slp->sl_xname, target);
4673 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4675 printf("%s: scsi_low: target %d lun ",
4676 slp->sl_xname, target);
4679 ti = slp->sl_ti[target];
4680 for (lun = 0; lun < slp->sl_nluns; lun ++)
4682 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4688 li = scsi_low_alloc_li(ti, lun, 1);
4690 scsi_low_enqueue(slp, ti, li, cb,
4691 CCB_AUTOSENSE | CCB_POLLED, 0);
4693 scsi_low_poll(slp, cb);
4695 if (li->li_state != SCSI_LOW_LUN_OK)
4698 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4704 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4713 scsi_low_poll(slp, cb)
4714 struct scsi_low_softc *slp;
4720 while (slp->sl_nio > 0)
4722 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4724 (*slp->sl_funcs->scsi_low_poll) (slp);
4725 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4729 scsi_low_timeout_check(slp);
4734 #endif /* SCSI_LOW_START_UP_CHECK */
4736 /**********************************************************
4738 **********************************************************/
4739 #ifdef SCSI_LOW_DEBUG
4741 scsi_low_test_abort(slp, ti, li)
4742 struct scsi_low_softc *slp;
4743 struct targ_info *ti;
4744 struct lun_info *li;
4748 if (li->li_disc > 1)
4750 acb = TAILQ_FIRST(&li->li_discq);
4751 if (scsi_low_abort_ccb(slp, acb) == 0)
4753 printf("%s: aborting ccb(0x%lx) start\n",
4754 slp->sl_xname, (u_long) acb);
4760 scsi_low_test_atten(slp, ti, msg)
4761 struct scsi_low_softc *slp;
4762 struct targ_info *ti;
4766 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4767 scsi_low_assert_msg(slp, ti, msg, 0);
4769 printf("%s: atten check OK\n", slp->sl_xname);
4773 scsi_low_test_cmdlnk(slp, cb)
4774 struct scsi_low_softc *slp;
4777 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4779 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4782 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4783 slp->sl_scp.scp_cmdlen);
4784 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4785 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4787 #endif /* SCSI_LOW_DEBUG */
4790 scsi_low_info(slp, ti, s)
4791 struct scsi_low_softc *slp;
4792 struct targ_info *ti;
4797 slp = LIST_FIRST(&sl_tab);
4801 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4804 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4805 ti = TAILQ_NEXT(ti, ti_chain))
4807 scsi_low_print(slp, ti);
4812 scsi_low_print(slp, ti);
4816 static u_char *phase[] =
4818 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4819 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4823 scsi_low_print(slp, ti)
4824 struct scsi_low_softc *slp;
4825 struct targ_info *ti;
4827 struct lun_info *li;
4831 if (ti == NULL || ti == slp->sl_Tnexus)
4833 ti = slp->sl_Tnexus;
4834 li = slp->sl_Lnexus;
4835 cb = slp->sl_Qnexus;
4839 li = LIST_FIRST(&ti->ti_litab);
4840 cb = TAILQ_FIRST(&li->li_discq);
4844 printf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4845 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4851 u_int flags = 0, maxnqio = 0, nqio = 0;
4857 flags = li->li_flags;
4858 maxnqio = li->li_maxnqio;
4862 printf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4864 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4865 phase[(int) ti->ti_phase], ti->ti_disc,
4870 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4871 (u_int) cb->ccb_scp.scp_cmd[0],
4872 cb->ccb_scp.scp_cmdlen,
4874 cb->ccb_scp.scp_datalen,
4875 (u_int) cb->ccb_sscp.scp_status,
4876 cb->ccb_error, SCSI_LOW_ERRORBITS);
4879 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4880 (u_int) (ti->ti_msginptr),
4881 (u_int) (ti->ti_msgin[0]),
4882 (u_int) (ti->ti_msgin[1]),
4883 (u_int) (ti->ti_msgin[2]),
4884 (u_int) (ti->ti_msgin[3]),
4885 (u_int) (ti->ti_msgin[4]),
4888 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4889 (u_int) ti->ti_msgflags,
4890 (u_int) (ti->ti_msgoutstr[0]),
4891 (u_int) (ti->ti_msgoutstr[1]),
4892 (u_int) (ti->ti_msgoutstr[2]),
4893 (u_int) (ti->ti_msgoutstr[3]),
4894 (u_int) (ti->ti_msgoutstr[4]),
4896 flags, SCSI_LOW_BITS);
4898 #ifdef SCSI_LOW_DIAGNOSTIC
4899 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4900 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4901 #endif /* SCSI_LOW_DIAGNOSTIC */
4905 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4906 (u_long) sp->scp_data,
4908 (u_int) sp->scp_status,
4909 slp->sl_error, SCSI_LOW_ERRORBITS);