1 /* $NecBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $ */
7 #define SCSI_LOW_STATICS
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
12 /* #define SCSI_LOW_INFO_DETAIL */
14 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
15 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
17 #define SCSI_LOW_FLAGS_QUIRKS_OK
20 * SPDX-License-Identifier: BSD-3-Clause
22 * [NetBSD for NEC PC-98 series]
23 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
24 * NetBSD/pc98 porting staff. All rights reserved.
25 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
26 * Naofumi HONDA. All rights reserved.
28 * [Ported for FreeBSD CAM]
29 * Copyright (c) 2000, 2001
30 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
48 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
53 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 * POSSIBILITY OF SUCH DAMAGE.
57 /* <On the nexus establishment>
58 * When our host is reselected,
59 * nexus establish processes are little complicated.
60 * Normal steps are followings:
61 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
62 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
63 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
72 #include <sys/queue.h>
73 #include <sys/malloc.h>
74 #include <sys/errno.h>
77 #include <cam/cam_ccb.h>
78 #include <cam/cam_sim.h>
79 #include <cam/cam_debug.h>
80 #include <cam/cam_periph.h>
81 #include <cam/cam_xpt_periph.h>
83 #include <cam/scsi/scsi_all.h>
84 #include <cam/scsi/scsi_message.h>
86 #include <cam/scsi/scsi_low.h>
90 /**************************************************************
92 **************************************************************/
93 #define SCSI_LOW_POLL_HZ 1000
95 /* functions return values */
96 #define SCSI_LOW_START_NO_QTAG 0
97 #define SCSI_LOW_START_QTAG 1
99 #define SCSI_LOW_DONE_COMPLETE 0
100 #define SCSI_LOW_DONE_RETRY 1
102 /* internal disk flags */
103 #define SCSI_LOW_DISK_DISC 0x00000001
104 #define SCSI_LOW_DISK_QTAG 0x00000002
105 #define SCSI_LOW_DISK_LINK 0x00000004
106 #define SCSI_LOW_DISK_PARITY 0x00000008
107 #define SCSI_LOW_DISK_SYNC 0x00010000
108 #define SCSI_LOW_DISK_WIDE_16 0x00020000
109 #define SCSI_LOW_DISK_WIDE_32 0x00040000
110 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
111 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
112 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
114 static MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
116 /**************************************************************
118 **************************************************************/
119 /* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
120 static void scsi_low_engage(void *);
121 static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
122 static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
123 static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
124 static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
125 static void scsi_low_twiddle_wait(void);
126 static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
127 static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
128 static void scsi_low_calcf_lun(struct lun_info *);
129 static void scsi_low_calcf_target(struct targ_info *);
130 static void scsi_low_calcf_show(struct lun_info *);
131 static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
132 static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
133 static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
134 static int scsi_low_init(struct scsi_low_softc *, u_int);
135 static void scsi_low_start(struct scsi_low_softc *);
136 static void scsi_low_free_ti(struct scsi_low_softc *);
138 static int scsi_low_alloc_qtag(struct slccb *);
139 static int scsi_low_dealloc_qtag(struct slccb *);
140 static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
141 static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
142 static void scsi_low_unit_ready_cmd(struct slccb *);
143 static void scsi_low_timeout(void *);
144 static int scsi_low_timeout_check(struct scsi_low_softc *);
145 #ifdef SCSI_LOW_START_UP_CHECK
146 static int scsi_low_start_up(struct scsi_low_softc *);
147 #endif /* SCSI_LOW_START_UP_CHECK */
148 static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
149 static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);
151 int scsi_low_version_major = 2;
152 int scsi_low_version_minor = 17;
154 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
155 static struct mtx sl_tab_lock;
156 MTX_SYSINIT(sl_tab_lock, &sl_tab_lock, "scsi low table", MTX_DEF);
158 /**************************************************************
159 * Debug, Run test and Statics
160 **************************************************************/
161 #ifdef SCSI_LOW_INFO_DETAIL
162 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
163 #else /* !SCSI_LOW_INFO_DETAIL */
164 #define SCSI_LOW_INFO(slp, ti, s) device_printf((slp)->sl_dev, "%s\n", (s))
165 #endif /* !SCSI_LOW_INFO_DETAIL */
167 #ifdef SCSI_LOW_STATICS
168 static struct scsi_low_statics {
171 int nexus_disconnected;
172 int nexus_reselected;
175 #endif /* SCSI_LOW_STATICS */
177 #ifdef SCSI_LOW_DEBUG
178 #define SCSI_LOW_DEBUG_DONE 0x00001
179 #define SCSI_LOW_DEBUG_DISC 0x00002
180 #define SCSI_LOW_DEBUG_SENSE 0x00004
181 #define SCSI_LOW_DEBUG_CALCF 0x00008
182 #define SCSI_LOW_DEBUG_ACTION 0x10000
183 int scsi_low_debug = 0;
185 #define SCSI_LOW_MAX_ATTEN_CHECK 32
186 #define SCSI_LOW_ATTEN_CHECK 0x0001
187 #define SCSI_LOW_CMDLNK_CHECK 0x0002
188 #define SCSI_LOW_ABORT_CHECK 0x0004
189 #define SCSI_LOW_NEXUS_CHECK 0x0008
190 int scsi_low_test = 0;
191 int scsi_low_test_id = 0;
193 static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
194 static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
195 static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
196 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
197 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
198 #define SCSI_LOW_DEBUG_GO(fl, id) \
199 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
200 #endif /* SCSI_LOW_DEBUG */
202 /**************************************************************
204 **************************************************************/
205 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
206 GENERIC_CCB(scsi_low, slccb, ccb_chain)
208 /**************************************************************
210 **************************************************************/
211 #define SCSI_LOW_INLINE static __inline
212 SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
213 SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
214 SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
215 SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
216 SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
217 SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
218 SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);
221 scsi_low_activate_qtag(cb)
224 struct lun_info *li = cb->li;
226 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
230 cb->ccb_tag = cb->ccb_otag;
234 scsi_low_deactivate_qtag(cb)
237 struct lun_info *li = cb->li;
239 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
243 cb->ccb_tag = SCSI_LOW_UNKTAG;
247 scsi_low_ccb_message_exec(slp, cb)
248 struct scsi_low_softc *slp;
252 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
253 cb->ccb_msgoutflag = 0;
257 scsi_low_ccb_message_assert(cb, msg)
262 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
266 scsi_low_ccb_message_retry(cb)
269 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
273 scsi_low_ccb_message_clear(cb)
276 cb->ccb_msgoutflag = 0;
280 scsi_low_init_msgsys(slp, ti)
281 struct scsi_low_softc *slp;
282 struct targ_info *ti;
286 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
287 SCSI_LOW_DEASSERT_ATN(slp);
288 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
291 /*=============================================================
292 * START OF OS switch (All OS depend fucntions should be here)
293 =============================================================*/
294 /* common os depend utitlities */
295 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
296 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
297 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
299 static u_int8_t scsi_low_cmd_flags[256] = {
300 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
301 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
302 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
303 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
304 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
307 struct scsi_low_error_code {
312 static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
313 static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);
315 static struct slccb *
316 scsi_low_find_ccb(slp, target, lun, osdep)
317 struct scsi_low_softc *slp;
321 struct targ_info *ti;
325 ti = slp->sl_ti[target];
326 li = scsi_low_alloc_li(ti, lun, 0);
330 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
333 TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
335 if (cb->osdep == osdep)
339 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
341 if (cb->osdep == osdep)
348 scsi_low_translate_error_code(cb, tp)
350 struct scsi_low_error_code *tp;
353 if (cb->ccb_error == 0)
354 return tp->error_code;
356 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
358 return tp->error_code;
361 /**************************************************************
362 * SCSI INTERFACE (CAM)
363 **************************************************************/
364 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
365 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
366 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
368 static void scsi_low_poll_cam(struct cam_sim *);
369 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
371 static int scsi_low_attach_cam(struct scsi_low_softc *);
372 static int scsi_low_detach_cam(struct scsi_low_softc *);
373 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
374 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
376 struct scsi_low_error_code scsi_low_error_code_cam[] = {
378 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
379 {SENSEERR, CAM_AUTOSENSE_FAIL},
380 {UACAERR, CAM_SCSI_STATUS_ERROR},
381 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
382 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
383 {TIMEOUTIO, CAM_CMD_TIMEOUT},
384 {PDMAERR, CAM_DATA_RUN_ERR},
385 {PARITYERR, CAM_UNCOR_PARITY},
386 {UBFERR, CAM_UNEXP_BUSFREE},
387 {ABORTIO, CAM_REQ_ABORTED},
388 {-1, CAM_UNREC_HBA_ERROR}
391 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
394 * Please check a polling hz, currently we assume scsi_low_poll() is
397 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
400 scsi_low_poll_cam(sim)
403 struct scsi_low_softc *slp = SIM2SLP(sim);
405 SCSI_LOW_ASSERT_LOCKED(slp);
406 (*slp->sl_funcs->scsi_low_poll) (slp);
408 if (slp->sl_poll_count ++ >=
409 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
411 slp->sl_poll_count = 0;
412 scsi_low_timeout_check(slp);
417 scsi_low_scsi_action_cam(sim, ccb)
421 struct scsi_low_softc *slp = SIM2SLP(sim);
422 struct targ_info *ti;
425 u_int lun, flags, msg, target;
428 SCSI_LOW_ASSERT_LOCKED(slp);
429 target = (u_int) (ccb->ccb_h.target_id);
430 lun = (u_int) ccb->ccb_h.target_lun;
432 #ifdef SCSI_LOW_DEBUG
433 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
435 device_printf(slp->sl_dev,
436 "cam_action: func code 0x%x target: %d, lun: %d\n",
437 ccb->ccb_h.func_code, target, lun);
439 #endif /* SCSI_LOW_DEBUG */
441 switch (ccb->ccb_h.func_code) {
442 case XPT_SCSI_IO: /* Execute the requested I/O operation */
443 #ifdef SCSI_LOW_DIAGNOSTIC
444 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
446 device_printf(slp->sl_dev, "invalid target/lun\n");
447 ccb->ccb_h.status = CAM_REQ_INVALID;
451 #endif /* SCSI_LOW_DIAGNOSTIC */
453 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
454 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
459 ti = slp->sl_ti[target];
462 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
463 flags = CCB_AUTOSENSE | CCB_SCSIIO;
467 li = scsi_low_alloc_li(ti, lun, 1);
469 if (ti->ti_setup_msg != 0)
471 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
474 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
476 #ifdef SCSI_LOW_DEBUG
477 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
479 scsi_low_test_abort(slp, ti, li);
481 #endif /* SCSI_LOW_DEBUG */
484 case XPT_ABORT: /* Abort the specified CCB */
485 #ifdef SCSI_LOW_DIAGNOSTIC
486 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
488 device_printf(slp->sl_dev, "invalid target/lun\n");
489 ccb->ccb_h.status = CAM_REQ_INVALID;
493 #endif /* SCSI_LOW_DIAGNOSTIC */
495 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
496 rv = scsi_low_abort_ccb(slp, cb);
499 ccb->ccb_h.status = CAM_REQ_CMP;
501 ccb->ccb_h.status = CAM_REQ_INVALID;
505 case XPT_SET_TRAN_SETTINGS: {
506 struct ccb_trans_settings_scsi *scsi;
507 struct ccb_trans_settings_spi *spi;
508 struct ccb_trans_settings *cts;
511 #ifdef SCSI_LOW_DIAGNOSTIC
512 if (target == CAM_TARGET_WILDCARD)
514 device_printf(slp->sl_dev, "invalid target\n");
515 ccb->ccb_h.status = CAM_REQ_INVALID;
519 #endif /* SCSI_LOW_DIAGNOSTIC */
521 ti = slp->sl_ti[target];
522 if (lun == CAM_LUN_WILDCARD)
525 scsi = &cts->proto_specific.scsi;
526 spi = &cts->xport_specific.spi;
527 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
528 CTS_SPI_VALID_SYNC_RATE |
529 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
531 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
532 val = spi->bus_width;
533 if (val < ti->ti_width)
536 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
537 val = spi->sync_period;
538 if (val == 0 || val > ti->ti_maxsynch.period)
539 ti->ti_maxsynch.period = val;
541 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
542 val = spi->sync_offset;
543 if (val < ti->ti_maxsynch.offset)
544 ti->ti_maxsynch.offset = val;
546 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
547 scsi_low_calcf_target(ti);
550 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
551 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
553 li = scsi_low_alloc_li(ti, lun, 1);
554 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
555 li->li_quirks |= SCSI_LOW_DISK_DISC;
557 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
560 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
561 li->li_quirks |= SCSI_LOW_DISK_QTAG;
563 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
565 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
566 scsi_low_calcf_target(ti);
567 scsi_low_calcf_lun(li);
568 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
569 scsi_low_calcf_show(li);
572 ccb->ccb_h.status = CAM_REQ_CMP;
577 case XPT_GET_TRAN_SETTINGS: {
578 struct ccb_trans_settings *cts;
582 #ifdef SCSI_LOW_DIAGNOSTIC
583 if (target == CAM_TARGET_WILDCARD)
585 device_printf(slp->sl_dev, "invalid target\n");
586 ccb->ccb_h.status = CAM_REQ_INVALID;
590 #endif /* SCSI_LOW_DIAGNOSTIC */
591 ti = slp->sl_ti[target];
592 if (lun == CAM_LUN_WILDCARD)
595 li = scsi_low_alloc_li(ti, lun, 1);
596 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
597 struct ccb_trans_settings_scsi *scsi =
598 &cts->proto_specific.scsi;
599 struct ccb_trans_settings_spi *spi =
600 &cts->xport_specific.spi;
601 #ifdef SCSI_LOW_DIAGNOSTIC
602 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
604 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
605 device_printf(slp->sl_dev,
606 "invalid GET_TRANS_CURRENT_SETTINGS call\n");
609 #endif /* SCSI_LOW_DIAGNOSTIC */
610 cts->protocol = PROTO_SCSI;
611 cts->protocol_version = SCSI_REV_2;
612 cts->transport = XPORT_SPI;
613 cts->transport_version = 2;
615 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
616 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
618 diskflags = li->li_diskflags & li->li_cfgflags;
619 if (diskflags & SCSI_LOW_DISK_DISC)
620 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
621 if (diskflags & SCSI_LOW_DISK_QTAG)
622 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
624 spi->sync_period = ti->ti_maxsynch.period;
625 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
626 spi->sync_offset = ti->ti_maxsynch.offset;
627 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
629 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
630 spi->bus_width = ti->ti_width;
632 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
633 scsi->valid = CTS_SCSI_VALID_TQ;
634 spi->valid |= CTS_SPI_VALID_DISC;
638 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
644 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
645 cam_calc_geometry(&ccb->ccg, /*extended*/1);
650 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
651 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
652 ccb->ccb_h.status = CAM_REQ_CMP;
656 case XPT_TERM_IO: /* Terminate the I/O process */
657 ccb->ccb_h.status = CAM_REQ_INVALID;
661 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
662 #ifdef SCSI_LOW_DIAGNOSTIC
663 if (target == CAM_TARGET_WILDCARD)
665 device_printf(slp->sl_dev, "invalid target\n");
666 ccb->ccb_h.status = CAM_REQ_INVALID;
670 #endif /* SCSI_LOW_DIAGNOSTIC */
672 msg = SCSI_LOW_MSG_RESET;
673 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
675 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
680 ti = slp->sl_ti[target];
681 if (lun == CAM_LUN_WILDCARD)
685 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
686 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
688 flags = CCB_NORETRY | CCB_URGENT;
690 li = scsi_low_alloc_li(ti, lun, 1);
691 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
694 case XPT_PATH_INQ: { /* Path routing inquiry */
695 struct ccb_pathinq *cpi = &ccb->cpi;
697 cpi->version_num = scsi_low_version_major;
698 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
699 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
700 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
701 cpi->hba_inquiry |= PI_WIDE_16;
702 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
703 cpi->hba_inquiry |= PI_WIDE_32;
704 if (ti->ti_maxsynch.offset > 0)
705 cpi->hba_inquiry |= PI_SDTR_ABLE;
706 cpi->target_sprt = 0;
708 cpi->hba_eng_cnt = 0;
709 cpi->max_target = slp->sl_ntargs - 1;
710 cpi->max_lun = slp->sl_nluns - 1;
711 cpi->initiator_id = slp->sl_hostid;
712 cpi->bus_id = cam_sim_bus(sim);
713 cpi->base_transfer_speed = 3300;
714 cpi->transport = XPORT_SPI;
715 cpi->transport_version = 2;
716 cpi->protocol = PROTO_SCSI;
717 cpi->protocol_version = SCSI_REV_2;
718 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
719 strlcpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
720 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
721 cpi->unit_number = cam_sim_unit(sim);
722 cpi->ccb_h.status = CAM_REQ_CMP;
728 printf("scsi_low: non support func_code = %d ",
729 ccb->ccb_h.func_code);
730 ccb->ccb_h.status = CAM_REQ_INVALID;
737 scsi_low_attach_cam(slp)
738 struct scsi_low_softc *slp;
740 struct cam_devq *devq;
743 devq = cam_simq_alloc(SCSI_LOW_NCCB);
748 * ask the adapter what subunits are present
750 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
751 slp->sl_sim = cam_sim_alloc(scsi_low_scsi_action_cam,
753 device_get_name(slp->sl_dev), slp,
754 device_get_unit(slp->sl_dev), &slp->sl_lock,
755 slp->sl_openings, tagged_openings, devq);
757 if (slp->sl_sim == NULL) {
763 if (xpt_bus_register(slp->sl_sim, slp->sl_dev, 0) != CAM_SUCCESS) {
764 cam_sim_free(slp->sl_sim, TRUE);
765 SCSI_LOW_UNLOCK(slp);
769 if (xpt_create_path(&slp->sl_path, /*periph*/NULL,
770 cam_sim_path(slp->sl_sim), CAM_TARGET_WILDCARD,
771 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
772 xpt_bus_deregister(cam_sim_path(slp->sl_sim));
773 cam_sim_free(slp->sl_sim, /*free_simq*/TRUE);
774 SCSI_LOW_UNLOCK(slp);
778 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
779 SCSI_LOW_UNLOCK(slp);
784 scsi_low_detach_cam(slp)
785 struct scsi_low_softc *slp;
788 xpt_async(AC_LOST_DEVICE, slp->sl_path, NULL);
789 xpt_free_path(slp->sl_path);
790 xpt_bus_deregister(cam_sim_path(slp->sl_sim));
791 cam_sim_free(slp->sl_sim, /* free_devq */ TRUE);
796 scsi_low_ccb_setup_cam(slp, cb)
797 struct scsi_low_softc *slp;
800 union ccb *ccb = (union ccb *) cb->osdep;
802 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
804 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
805 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
806 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
807 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
808 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
809 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
810 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
811 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
812 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
816 scsi_low_unit_ready_cmd(cb);
818 return SCSI_LOW_START_QTAG;
822 scsi_low_done_cam(slp, cb)
823 struct scsi_low_softc *slp;
828 ccb = (union ccb *) cb->osdep;
829 if (cb->ccb_error == 0)
831 ccb->ccb_h.status = CAM_REQ_CMP;
836 if (cb->ccb_rcnt >= slp->sl_max_retry)
837 cb->ccb_error |= ABORTIO;
839 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
840 (cb->ccb_error & ABORTIO) == 0)
843 if ((cb->ccb_error & SENSEIO) != 0)
845 memcpy(&ccb->csio.sense_data,
847 sizeof(ccb->csio.sense_data));
850 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
851 &scsi_low_error_code_cam[0]);
853 #ifdef SCSI_LOW_DIAGNOSTIC
854 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
855 cb->ccb_scp.scp_cmdlen > 0 &&
856 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
857 SCSI_LOW_CMD_ABORT_WARNING) != 0)
859 device_printf(slp->sl_dev,
860 "WARNING: scsi_low IO abort\n");
861 scsi_low_print(slp, NULL);
863 #endif /* SCSI_LOW_DIAGNOSTIC */
866 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
867 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
869 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
870 ccb->csio.scsi_status = 0; /* XXX */
872 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
874 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
879 /**************************************************************
880 * scsi low deactivate and activate
881 **************************************************************/
883 scsi_low_is_busy(slp)
884 struct scsi_low_softc *slp;
893 scsi_low_deactivate(slp)
894 struct scsi_low_softc *slp;
897 slp->sl_flags |= HW_INACTIVE;
898 callout_stop(&slp->sl_timeout_timer);
899 callout_stop(&slp->sl_engage_timer);
904 scsi_low_activate(slp)
905 struct scsi_low_softc *slp;
909 slp->sl_flags &= ~HW_INACTIVE;
910 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
912 slp->sl_flags |= HW_INACTIVE;
916 slp->sl_timeout_count = 0;
917 callout_reset(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ,
918 scsi_low_timeout, slp);
922 /**************************************************************
924 **************************************************************/
925 #ifdef SCSI_LOW_DIAGNOSTIC
926 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
927 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
928 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
931 scsi_low_msg_log_init(slmlp)
932 struct scsi_low_msg_log *slmlp;
939 scsi_low_msg_log_write(slmlp, datap, len)
940 struct scsi_low_msg_log *slmlp;
946 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
949 ptr = slmlp->slml_ptr ++;
950 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
951 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
952 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
953 slmlp->slml_msg[ptr].msg[ind] = 0;
957 scsi_low_msg_log_show(slmlp, s, len)
958 struct scsi_low_msg_log *slmlp;
964 printf("%s: (%d) ", s, slmlp->slml_ptr);
965 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
967 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
970 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
976 #endif /* SCSI_LOW_DIAGNOSTIC */
978 /**************************************************************
980 **************************************************************/
985 struct scsi_low_softc *slp = arg;
987 SCSI_LOW_ASSERT_LOCKED(slp);
988 switch (slp->sl_rstep)
992 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
993 callout_reset(&slp->sl_engage_timer, hz / 1000,
994 scsi_low_engage, slp);
999 slp->sl_flags &= ~HW_RESUME;
1000 scsi_low_start(slp);
1009 scsi_low_init(slp, flags)
1010 struct scsi_low_softc *slp;
1015 slp->sl_flags |= HW_INITIALIZING;
1017 /* clear power control timeout */
1018 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1020 callout_stop(&slp->sl_engage_timer);
1021 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1023 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1026 /* reset current nexus */
1027 scsi_low_reset_nexus(slp, flags);
1028 if ((slp->sl_flags & HW_INACTIVE) != 0)
1034 if (flags != SCSI_LOW_RESTART_SOFT)
1036 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1040 slp->sl_flags &= ~HW_INITIALIZING;
1044 /**************************************************************
1046 **************************************************************/
1047 static struct lun_info *
1048 scsi_low_alloc_li(ti, lun, alloc)
1049 struct targ_info *ti;
1053 struct scsi_low_softc *slp = ti->ti_sc;
1054 struct lun_info *li;
1056 li = LIST_FIRST(&ti->ti_litab);
1059 if (li->li_lun == lun)
1062 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1064 if (li->li_lun == lun)
1066 LIST_REMOVE(li, lun_chain);
1067 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1076 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1078 panic("no lun info mem");
1080 bzero(li, ti->ti_lunsize);
1084 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1086 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1087 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1088 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1089 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1090 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1092 li->li_qtagbits = (u_int) -1;
1094 TAILQ_INIT(&li->li_discq);
1095 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1097 /* host specific structure initialization per lun */
1098 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1099 (*slp->sl_funcs->scsi_low_lun_init)
1100 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1101 scsi_low_calcf_lun(li);
1105 /**************************************************************
1106 * allocate targ_info
1107 **************************************************************/
1108 static struct targ_info *
1109 scsi_low_alloc_ti(slp, targ)
1110 struct scsi_low_softc *slp;
1113 struct targ_info *ti;
1115 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1116 TAILQ_INIT(&slp->sl_titab);
1118 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1120 panic("%s short of memory", device_get_nameunit(slp->sl_dev));
1122 bzero(ti, slp->sl_targsize);
1126 slp->sl_ti[targ] = ti;
1127 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1128 LIST_INIT(&ti->ti_litab);
1130 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1131 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1132 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1133 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1134 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1135 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1137 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1139 (*slp->sl_funcs->scsi_low_targ_init)
1140 (slp, ti, SCSI_LOW_INFO_ALLOC);
1142 scsi_low_calcf_target(ti);
1147 scsi_low_free_ti(slp)
1148 struct scsi_low_softc *slp;
1150 struct targ_info *ti, *tib;
1151 struct lun_info *li, *nli;
1153 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1155 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1157 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1159 (*slp->sl_funcs->scsi_low_lun_init)
1160 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1162 nli = LIST_NEXT(li, lun_chain);
1166 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1168 (*slp->sl_funcs->scsi_low_targ_init)
1169 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1171 tib = TAILQ_NEXT(ti, ti_chain);
1176 /**************************************************************
1178 **************************************************************/
1180 scsi_low_bus_idle(slp)
1181 struct scsi_low_softc *slp;
1184 slp->sl_retry_sel = 0;
1185 if (slp->sl_Tnexus == NULL)
1186 scsi_low_start(slp);
1190 scsi_low_timeout(arg)
1193 struct scsi_low_softc *slp = arg;
1195 SCSI_LOW_ASSERT_LOCKED(slp);
1196 (void) scsi_low_timeout_check(slp);
1197 callout_schedule(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ);
1201 scsi_low_timeout_check(slp)
1202 struct scsi_low_softc *slp;
1204 struct targ_info *ti;
1205 struct lun_info *li;
1206 struct slccb *cb = NULL; /* XXX */
1208 /* selection restart */
1209 if (slp->sl_retry_sel != 0)
1211 slp->sl_retry_sel = 0;
1212 if (slp->sl_Tnexus != NULL)
1215 cb = TAILQ_FIRST(&slp->sl_start);
1219 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1221 cb->ccb_flags |= CCB_NORETRY;
1222 cb->ccb_error |= SELTIMEOUTIO;
1223 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1224 panic("%s: ccb not finished",
1225 device_get_nameunit(slp->sl_dev));
1228 if (slp->sl_Tnexus == NULL)
1229 scsi_low_start(slp);
1232 /* call hardware timeout */
1234 if (slp->sl_funcs->scsi_low_timeout != NULL)
1236 (*slp->sl_funcs->scsi_low_timeout) (slp);
1239 if (slp->sl_timeout_count ++ <
1240 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1243 slp->sl_timeout_count = 0;
1244 if (slp->sl_nio > 0)
1246 if ((cb = slp->sl_Qnexus) != NULL)
1248 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1252 else if (slp->sl_disc == 0)
1254 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1257 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1261 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1262 ti = TAILQ_NEXT(ti, ti_chain))
1264 if (ti->ti_disc == 0)
1267 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1268 li = LIST_NEXT(li, lun_chain))
1270 for (cb = TAILQ_FIRST(&li->li_discq);
1272 cb = TAILQ_NEXT(cb, ccb_chain))
1275 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1283 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1285 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1288 if (slp->sl_active != 0)
1290 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1296 if (slp->sl_powc < 0)
1298 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1299 slp->sl_flags |= HW_POWDOWN;
1300 (*slp->sl_funcs->scsi_low_power)
1301 (slp, SCSI_LOW_POWDOWN);
1307 cb->ccb_error |= TIMEOUTIO;
1308 device_printf(slp->sl_dev, "slccb (0x%lx) timeout!\n", (u_long) cb);
1309 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1310 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1311 scsi_low_start(slp);
1317 scsi_low_abort_ccb(slp, cb)
1318 struct scsi_low_softc *slp;
1321 struct targ_info *ti;
1322 struct lun_info *li;
1327 if ((cb->ccb_omsgoutflag &
1328 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1333 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1334 msg = SCSI_LOW_MSG_ABORT;
1336 msg = SCSI_LOW_MSG_ABORT_QTAG;
1338 cb->ccb_error |= ABORTIO;
1339 cb->ccb_flags |= CCB_NORETRY;
1340 scsi_low_ccb_message_assert(cb, msg);
1342 if (cb == slp->sl_Qnexus)
1344 scsi_low_assert_msg(slp, ti, msg, 1);
1346 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1348 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1349 panic("%s: revoked ccb done",
1350 device_get_nameunit(slp->sl_dev));
1352 cb->ccb_flags |= CCB_STARTQ;
1353 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1355 if (slp->sl_Tnexus == NULL)
1356 scsi_low_start(slp);
1360 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1361 panic("%s: revoked ccb retried",
1362 device_get_nameunit(slp->sl_dev));
1367 /**************************************************************
1368 * Generic SCSI INTERFACE
1369 **************************************************************/
1371 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1372 struct scsi_low_softc *slp;
1373 int openings, ntargs, nluns, targsize, lunsize;
1375 struct targ_info *ti;
1376 struct lun_info *li;
1379 if (ntargs > SCSI_LOW_NTARGETS)
1381 printf("scsi_low: %d targets are too large\n", ntargs);
1382 printf("change kernel options SCSI_LOW_NTARGETS");
1387 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1389 slp->sl_openings = openings;
1390 slp->sl_ntargs = ntargs;
1391 slp->sl_nluns = nluns;
1392 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1394 if (lunsize < sizeof(struct lun_info))
1395 lunsize = sizeof(struct lun_info);
1397 if (targsize < sizeof(struct targ_info))
1398 targsize = sizeof(struct targ_info);
1400 slp->sl_targsize = targsize;
1401 for (i = 0; i < ntargs; i ++)
1403 ti = scsi_low_alloc_ti(slp, i);
1404 ti->ti_lunsize = lunsize;
1405 li = scsi_low_alloc_li(ti, 0, 1);
1408 /* initialize queue */
1409 nccb = openings * ntargs;
1410 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1411 nccb = SCSI_LOW_NCCB;
1412 scsi_low_init_ccbque(nccb);
1413 TAILQ_INIT(&slp->sl_start);
1415 /* call os depend attach */
1416 rv = scsi_low_attach_cam(slp);
1419 device_printf(slp->sl_dev,
1420 "scsi_low_attach: osdep attach failed\n");
1424 /* check hardware */
1425 DELAY(1000); /* wait for 1ms */
1427 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1429 device_printf(slp->sl_dev,
1430 "scsi_low_attach: initialization failed\n");
1431 SCSI_LOW_UNLOCK(slp);
1435 /* start watch dog */
1436 slp->sl_timeout_count = 0;
1437 callout_reset(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ,
1438 scsi_low_timeout, slp);
1439 mtx_lock(&sl_tab_lock);
1440 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1441 mtx_unlock(&sl_tab_lock);
1444 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1446 #ifdef SCSI_LOW_START_UP_CHECK
1447 /* probing devices */
1448 scsi_low_start_up(slp);
1449 #endif /* SCSI_LOW_START_UP_CHECK */
1450 SCSI_LOW_UNLOCK(slp);
1456 scsi_low_detach(slp)
1457 struct scsi_low_softc *slp;
1462 if (scsi_low_is_busy(slp) != 0)
1464 SCSI_LOW_UNLOCK(slp);
1468 scsi_low_deactivate(slp);
1470 rv = scsi_low_detach_cam(slp);
1473 SCSI_LOW_UNLOCK(slp);
1477 scsi_low_free_ti(slp);
1478 SCSI_LOW_UNLOCK(slp);
1479 callout_drain(&slp->sl_timeout_timer);
1480 callout_drain(&slp->sl_engage_timer);
1481 mtx_lock(&sl_tab_lock);
1482 LIST_REMOVE(slp, sl_chain);
1483 mtx_unlock(&sl_tab_lock);
1487 /**************************************************************
1489 **************************************************************/
1491 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
1492 struct scsi_low_softc *slp;
1493 struct targ_info *ti;
1494 struct lun_info *li;
1502 scsi_low_ccb_message_assert(cb, msg);
1504 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1505 scsi_low_alloc_qtag(cb);
1507 cb->ccb_flags = flags | CCB_STARTQ;
1508 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1509 cb->ccb_error |= PENDINGIO;
1511 if ((flags & CCB_URGENT) != 0)
1513 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1517 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1522 if (slp->sl_Tnexus == NULL)
1523 scsi_low_start(slp);
1528 scsi_low_message_enqueue(slp, ti, li, flags)
1529 struct scsi_low_softc *slp;
1530 struct targ_info *ti;
1531 struct lun_info *li;
1537 tmsgflags = ti->ti_setup_msg;
1538 ti->ti_setup_msg = 0;
1540 flags |= CCB_NORETRY;
1541 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1546 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1550 /**************************************************************
1551 * Generic Start & Done
1552 **************************************************************/
1553 #define SLSC_MODE_SENSE_SHORT 0x1a
1554 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1555 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1556 sizeof(struct scsi_low_mode_sense_data), 0};
1557 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1558 sizeof(struct scsi_low_inq_data), 0};
1559 static u_int8_t unit_ready_cmd[6];
1560 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1561 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1562 static int scsi_low_resume(struct scsi_low_softc *);
1565 scsi_low_unit_ready_cmd(cb)
1569 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1570 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1571 cb->ccb_scp.scp_datalen = 0;
1572 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1577 scsi_low_sense_abort_start(slp, ti, li, cb)
1578 struct scsi_low_softc *slp;
1579 struct targ_info *ti;
1580 struct lun_info *li;
1584 cb->ccb_scp.scp_cmdlen = 6;
1585 bzero(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1586 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1587 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1588 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1589 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1590 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1591 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1593 scsi_low_ccb_message_clear(cb);
1594 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1596 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1600 bzero(&cb->ccb_sense, sizeof(cb->ccb_sense));
1601 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1602 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1603 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1606 return SCSI_LOW_START_NO_QTAG;
1610 scsi_low_setup_start(slp, ti, li, cb)
1611 struct scsi_low_softc *slp;
1612 struct targ_info *ti;
1613 struct lun_info *li;
1617 switch(li->li_state)
1619 case SCSI_LOW_LUN_SLEEP:
1620 scsi_low_unit_ready_cmd(cb);
1623 case SCSI_LOW_LUN_START:
1624 cb->ccb_scp.scp_cmd = ss_cmd;
1625 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1626 cb->ccb_scp.scp_datalen = 0;
1627 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1631 case SCSI_LOW_LUN_INQ:
1632 cb->ccb_scp.scp_cmd = inq_cmd;
1633 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1634 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1635 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1636 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1640 case SCSI_LOW_LUN_MODEQ:
1641 cb->ccb_scp.scp_cmd = sms_cmd;
1642 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1643 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1644 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1645 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1647 return SCSI_LOW_START_QTAG;
1650 panic("%s: no setup phase", device_get_nameunit(slp->sl_dev));
1653 return SCSI_LOW_START_NO_QTAG;
1657 scsi_low_resume(slp)
1658 struct scsi_low_softc *slp;
1661 if (slp->sl_flags & HW_RESUME)
1663 slp->sl_flags &= ~HW_POWDOWN;
1664 if (slp->sl_funcs->scsi_low_power != NULL)
1666 slp->sl_flags |= HW_RESUME;
1668 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1669 callout_reset(&slp->sl_engage_timer, hz / 1000,
1670 scsi_low_engage, slp);
1678 struct scsi_low_softc *slp;
1680 struct targ_info *ti;
1681 struct lun_info *li;
1685 /* check hardware exists or under initializations ? */
1686 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1689 /* check hardware power up ? */
1690 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1693 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1695 if (scsi_low_resume(slp) == EJUSTRETURN)
1701 #ifdef SCSI_LOW_DIAGNOSTIC
1702 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1704 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
1705 panic("%s: inconsistent", device_get_nameunit(slp->sl_dev));
1707 #endif /* SCSI_LOW_DIAGNOSTIC */
1709 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
1710 cb = TAILQ_NEXT(cb, ccb_chain))
1714 if (li->li_disc == 0)
1716 goto scsi_low_cmd_start;
1718 else if (li->li_nqio > 0)
1720 if (li->li_nqio < li->li_maxnqio ||
1721 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1722 goto scsi_low_cmd_start;
1728 cb->ccb_flags &= ~CCB_STARTQ;
1729 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1732 /* clear all error flag bits (for restart) */
1734 cb->ccb_datalen = -1;
1735 cb->ccb_scp.scp_status = ST_UNKNOWN;
1737 /* setup nexus pointer */
1738 slp->sl_Qnexus = cb;
1739 slp->sl_Lnexus = li;
1740 slp->sl_Tnexus = ti;
1742 /* initialize msgsys */
1743 scsi_low_init_msgsys(slp, ti);
1746 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1748 /* CA state or forced abort */
1749 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1751 else if (li->li_state >= SCSI_LOW_LUN_OK)
1753 cb->ccb_flags &= ~CCB_INTERNAL;
1754 rv = scsi_low_ccb_setup_cam(slp, cb);
1755 if (cb->ccb_msgoutflag != 0)
1757 scsi_low_ccb_message_exec(slp, cb);
1762 cb->ccb_flags |= CCB_INTERNAL;
1763 rv = scsi_low_setup_start(slp, ti, li, cb);
1767 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1769 if (rv == SCSI_LOW_START_QTAG &&
1770 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1775 scsi_low_activate_qtag(cb);
1776 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1777 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1778 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1779 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1780 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1782 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1783 scsi_low_assert_msg(slp, ti, qmsg, 0);
1787 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1788 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1789 cb->ccb_tc = cb->ccb_tcmax;
1791 /* setup saved scsi data pointer */
1792 cb->ccb_sscp = cb->ccb_scp;
1794 /* setup current scsi pointer */
1795 slp->sl_scp = cb->ccb_sscp;
1796 slp->sl_error = cb->ccb_error;
1798 /* assert always an identify msg */
1799 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1802 #ifdef SCSI_LOW_DIAGNOSTIC
1803 scsi_low_msg_log_init(&ti->ti_log_msgin);
1804 scsi_low_msg_log_init(&ti->ti_log_msgout);
1805 #endif /* SCSI_LOW_DIAGNOSTIC */
1807 /* selection start */
1809 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1810 if (rv == SCSI_LOW_START_OK)
1812 #ifdef SCSI_LOW_STATICS
1813 scsi_low_statics.nexus_win ++;
1814 #endif /* SCSI_LOW_STATICS */
1818 scsi_low_arbit_fail(slp, cb);
1819 #ifdef SCSI_LOW_STATICS
1820 scsi_low_statics.nexus_fail ++;
1821 #endif /* SCSI_LOW_STATICS */
1825 scsi_low_arbit_fail(slp, cb)
1826 struct scsi_low_softc *slp;
1829 struct targ_info *ti = cb->ti;
1831 scsi_low_deactivate_qtag(cb);
1832 scsi_low_ccb_message_retry(cb);
1833 cb->ccb_flags |= CCB_STARTQ;
1834 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1836 scsi_low_bus_release(slp, ti);
1839 if (slp->sl_disc == 0)
1841 #ifdef SCSI_LOW_DIAGNOSTIC
1842 device_printf(slp->sl_dev, "try selection again\n");
1843 #endif /* SCSI_LOW_DIAGNOSTIC */
1844 slp->sl_retry_sel = 1;
1849 scsi_low_bus_release(slp, ti)
1850 struct scsi_low_softc *slp;
1851 struct targ_info *ti;
1854 if (ti->ti_disc > 0)
1856 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1860 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1863 /* clear all nexus pointer */
1864 slp->sl_Qnexus = NULL;
1865 slp->sl_Lnexus = NULL;
1866 slp->sl_Tnexus = NULL;
1868 /* clear selection assert */
1869 slp->sl_selid = NULL;
1871 /* clear nexus data */
1872 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1874 /* clear phase change counter */
1875 slp->sl_ph_count = 0;
1879 scsi_low_setup_done(slp, cb)
1880 struct scsi_low_softc *slp;
1883 struct targ_info *ti;
1884 struct lun_info *li;
1889 if (cb->ccb_rcnt >= slp->sl_max_retry)
1891 cb->ccb_error |= ABORTIO;
1892 return SCSI_LOW_DONE_COMPLETE;
1895 /* XXX: special huck for selection timeout */
1896 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1897 (cb->ccb_error & SELTIMEOUTIO) != 0)
1899 cb->ccb_error |= ABORTIO;
1900 return SCSI_LOW_DONE_COMPLETE;
1903 switch(li->li_state)
1905 case SCSI_LOW_LUN_INQ:
1906 if (cb->ccb_error != 0)
1909 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
1913 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
1915 else if ((li->li_inq.sd_version & 7) >= 2 ||
1916 (li->li_inq.sd_len >= 4))
1918 if ((li->li_inq.sd_support & 0x2) == 0)
1919 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1920 if ((li->li_inq.sd_support & 0x8) == 0)
1921 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
1924 if ((li->li_inq.sd_support & 0x10) == 0)
1925 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
1926 if ((li->li_inq.sd_support & 0x20) == 0)
1927 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
1928 if ((li->li_inq.sd_support & 0x40) == 0)
1929 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
1934 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
1937 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
1939 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
1941 scsi_low_calcf_target(ti);
1942 scsi_low_calcf_lun(li);
1945 case SCSI_LOW_LUN_MODEQ:
1946 if (cb->ccb_error != 0)
1948 if (cb->ccb_error & SENSEIO)
1950 #ifdef SCSI_LOW_DEBUG
1951 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
1953 int error_code, sense_key, asc, ascq;
1955 scsi_extract_sense(&cb->ccb_sense,
1960 printf("SENSE: [%x][%x][%x][%x]\n",
1961 error_code, sense_key, asc,
1964 #endif /* SCSI_LOW_DEBUG */
1968 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1971 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
1973 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
1974 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
1976 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
1977 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
1978 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1980 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
1981 scsi_low_calcf_lun(li);
1989 if (li->li_state == SCSI_LOW_LUN_OK)
1991 scsi_low_calcf_target(ti);
1992 scsi_low_calcf_lun(li);
1993 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
1994 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
1996 scsi_low_calcf_show(li);
2001 return SCSI_LOW_DONE_RETRY;
2005 scsi_low_done(slp, cb)
2006 struct scsi_low_softc *slp;
2011 if (cb->ccb_error == 0)
2013 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2015 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2017 * SCSI-2 draft suggests
2018 * page 0x0a QErr bit determins if
2019 * the target aborts or continues
2020 * the queueing io's after CA state resolved.
2021 * However many targets seem not to support
2022 * the page 0x0a. Thus we should manually clear the
2023 * queuing io's after CA state.
2025 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2028 cb->ccb_flags |= CCB_CLEARQ;
2031 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2033 if ((cb->ccb_flags & CCB_SENSE) != 0)
2034 cb->ccb_error |= (SENSEIO | ABORTIO);
2035 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2037 else switch (cb->ccb_sscp.scp_status)
2043 if (cb->ccb_datalen == 0 ||
2044 cb->ccb_scp.scp_datalen == 0)
2047 if (cb->ccb_scp.scp_cmdlen > 0 &&
2048 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2049 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2052 cb->ccb_error |= PDMAERR;
2057 cb->ccb_error |= (BUSYERR | STATERR);
2061 cb->ccb_error |= (STATERR | ABORTIO);
2066 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2069 cb->ccb_flags |= CCB_SENSE;
2072 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2077 cb->ccb_error |= FATALIO;
2083 if (cb->ccb_flags & CCB_SENSE)
2085 cb->ccb_error |= (SENSEERR | ABORTIO);
2087 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2091 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2093 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2097 /* check a ccb msgout flag */
2098 if (cb->ccb_omsgoutflag != 0)
2100 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2101 SCSI_LOW_MSG_ABORT_QTAG | \
2102 SCSI_LOW_MSG_CLEAR_QTAG | \
2103 SCSI_LOW_MSG_TERMIO)
2105 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2107 cb->ccb_error |= ABORTIO;
2111 /* call OS depend done */
2112 if (cb->osdep != NULL)
2114 rv = scsi_low_done_cam(slp, cb);
2115 if (rv == EJUSTRETURN)
2118 else if (cb->ccb_error != 0)
2120 if (cb->ccb_rcnt >= slp->sl_max_retry)
2121 cb->ccb_error |= ABORTIO;
2123 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2124 (cb->ccb_error & ABORTIO) == 0)
2128 /* free our target */
2129 #ifdef SCSI_LOW_DEBUG
2130 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2132 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2133 scsi_low_print(slp, NULL);
2135 #endif /* SCSI_LOW_DEBUG */
2137 scsi_low_deactivate_qtag(cb);
2138 scsi_low_dealloc_qtag(cb);
2139 scsi_low_free_ccb(cb);
2141 return SCSI_LOW_DONE_COMPLETE;
2144 #ifdef SCSI_LOW_DEBUG
2145 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2147 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2148 scsi_low_print(slp, NULL);
2150 #endif /* SCSI_LOW_DEBUG */
2153 scsi_low_deactivate_qtag(cb);
2154 scsi_low_ccb_message_retry(cb);
2155 return SCSI_LOW_DONE_RETRY;
2158 /**************************************************************
2160 **************************************************************/
2162 scsi_low_reset_nexus_target(slp, ti, fdone)
2163 struct scsi_low_softc *slp;
2164 struct targ_info *ti;
2167 struct lun_info *li;
2169 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2170 li = LIST_NEXT(li, lun_chain))
2172 scsi_low_reset_nexus_lun(slp, li, fdone);
2173 li->li_state = SCSI_LOW_LUN_SLEEP;
2178 ti->ti_setup_msg = 0;
2179 ti->ti_setup_msg_done = 0;
2181 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2182 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2184 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2185 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2187 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2189 ((*slp->sl_funcs->scsi_low_targ_init)
2190 (slp, ti, SCSI_LOW_INFO_REVOKE));
2192 scsi_low_calcf_target(ti);
2194 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2195 li = LIST_NEXT(li, lun_chain))
2199 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2200 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2202 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2204 ((*slp->sl_funcs->scsi_low_lun_init)
2205 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2207 scsi_low_calcf_lun(li);
2212 scsi_low_reset_nexus(slp, fdone)
2213 struct scsi_low_softc *slp;
2216 struct targ_info *ti;
2217 struct slccb *cb, *topcb;
2219 if ((cb = slp->sl_Qnexus) != NULL)
2221 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2228 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2229 ti = TAILQ_NEXT(ti, ti_chain))
2231 scsi_low_reset_nexus_target(slp, ti, fdone);
2232 scsi_low_bus_release(slp, ti);
2233 scsi_low_init_msgsys(slp, ti);
2238 topcb->ccb_flags |= CCB_STARTQ;
2239 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2243 slp->sl_retry_sel = 0;
2244 slp->sl_flags &= ~HW_PDMASTART;
2249 static char tw_chars[] = "|/-\\";
2250 #define TWIDDLEWAIT 10000
2253 scsi_low_twiddle_wait(void)
2257 cnputc(tw_chars[tw_pos++]);
2258 tw_pos %= (sizeof(tw_chars) - 1);
2263 scsi_low_bus_reset(slp)
2264 struct scsi_low_softc *slp;
2268 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2270 device_printf(slp->sl_dev, "try to reset scsi bus ");
2271 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2272 scsi_low_twiddle_wait();
2278 scsi_low_restart(slp, flags, s)
2279 struct scsi_low_softc *slp;
2286 device_printf(slp->sl_dev, "scsi bus restart. reason: %s\n", s);
2288 if ((error = scsi_low_init(slp, flags)) != 0)
2291 scsi_low_start(slp);
2295 /**************************************************************
2296 * disconnect and reselect
2297 **************************************************************/
2298 #define MSGCMD_LUN(msg) (msg & 0x07)
2300 static struct slccb *
2301 scsi_low_establish_ccb(ti, li, tag)
2302 struct targ_info *ti;
2303 struct lun_info *li;
2306 struct scsi_low_softc *slp = ti->ti_sc;
2312 cb = TAILQ_FIRST(&li->li_discq);
2313 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2314 if (cb->ccb_tag == tag)
2319 * establish our ccb nexus
2322 #ifdef SCSI_LOW_DEBUG
2323 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2325 device_printf(slp->sl_dev, "nexus(0x%lx) abort check start\n",
2327 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2328 scsi_low_revoke_ccb(slp, cb, 1);
2332 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2334 if (cb->ccb_omsgoutflag == 0)
2335 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2337 #endif /* SCSI_LOW_DEBUG */
2339 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2340 cb->ccb_flags &= ~CCB_DISCQ;
2341 slp->sl_Qnexus = cb;
2343 slp->sl_scp = cb->ccb_sscp;
2344 slp->sl_error |= cb->ccb_error;
2350 /* inform "ccb nexus established" to the host driver */
2351 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2354 if (cb->ccb_msgoutflag != 0)
2356 scsi_low_ccb_message_exec(slp, cb);
2363 scsi_low_reselected(slp, targ)
2364 struct scsi_low_softc *slp;
2367 struct targ_info *ti;
2372 * Check select vs reselected collision.
2375 if ((cb = slp->sl_selid) != NULL)
2377 scsi_low_arbit_fail(slp, cb);
2378 #ifdef SCSI_LOW_STATICS
2379 scsi_low_statics.nexus_conflict ++;
2380 #endif /* SCSI_LOW_STATICS */
2384 * Check if no current active nexus.
2386 if (slp->sl_Tnexus != NULL)
2393 * Check a valid target id asserted ?
2395 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2397 s = "scsi id illegal";
2402 * Check the target scsi status.
2404 ti = slp->sl_ti[targ];
2405 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2407 s = "phase mismatch";
2415 scsi_low_init_msgsys(slp, ti);
2418 * Establish our target nexus
2420 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2421 slp->sl_Tnexus = ti;
2422 #ifdef SCSI_LOW_STATICS
2423 scsi_low_statics.nexus_reselected ++;
2424 #endif /* SCSI_LOW_STATICS */
2428 device_printf(slp->sl_dev, "reselect(%x:unknown) %s\n", targ, s);
2429 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2430 "reselect: scsi world confused");
2434 /**************************************************************
2435 * cmd out pointer setup
2436 **************************************************************/
2438 scsi_low_cmd(slp, ti)
2439 struct scsi_low_softc *slp;
2440 struct targ_info *ti;
2442 struct slccb *cb = slp->sl_Qnexus;
2444 slp->sl_ph_count ++;
2450 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2451 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2452 slp->sl_scp.scp_datalen = 0;
2453 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2454 slp->sl_error |= FATALIO;
2455 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2456 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2461 #ifdef SCSI_LOW_DEBUG
2462 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2464 scsi_low_test_cmdlnk(slp, cb);
2466 #endif /* SCSI_LOW_DEBUG */
2471 /**************************************************************
2472 * data out pointer setup
2473 **************************************************************/
2475 scsi_low_data(slp, ti, bp, direction)
2476 struct scsi_low_softc *slp;
2477 struct targ_info *ti;
2481 struct slccb *cb = slp->sl_Qnexus;
2483 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2489 slp->sl_error |= (FATALIO | PDMAERR);
2490 slp->sl_scp.scp_datalen = 0;
2491 slp->sl_scp.scp_direction = direction;
2492 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2493 if (ti->ti_ophase != ti->ti_phase)
2498 s = "DATA PHASE: ccb nexus not found";
2500 s = "DATA PHASE: xfer direction mismatch";
2501 SCSI_LOW_INFO(slp, ti, s);
2508 /**************************************************************
2510 **************************************************************/
2511 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2512 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2513 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2514 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2515 #define MSGIN_DATA_LAST 0x30
2517 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
2518 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
2519 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
2520 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
2522 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
2523 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
2524 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
2525 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
2526 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
2527 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
2528 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
2530 struct scsi_low_msgout_data {
2533 int (*md_msgfunc)(struct scsi_low_softc *);
2534 int (*md_errfunc)(struct scsi_low_softc *, u_int);
2535 #define MSG_RELEASE_ATN 0x0001
2539 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2540 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2541 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2542 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2543 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2544 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2545 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2546 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2547 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2548 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2549 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2550 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2551 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2552 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2553 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2554 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2555 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2558 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
2559 static int scsi_low_synch(struct scsi_low_softc *);
2560 static int scsi_low_wide(struct scsi_low_softc *);
2561 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
2562 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
2563 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
2564 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
2565 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
2566 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
2567 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
2568 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
2569 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
2570 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
2571 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
2573 struct scsi_low_msgin_data {
2575 int (*md_msgfunc)(struct scsi_low_softc *);
2578 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2579 /* 0 */ {1, scsi_low_msginfunc_cc},
2580 /* 1 */ {2, scsi_low_msginfunc_ext},
2581 /* 2 */ {1, scsi_low_msginfunc_sdp},
2582 /* 3 */ {1, scsi_low_msginfunc_rp},
2583 /* 4 */ {1, scsi_low_msginfunc_disc},
2584 /* 5 */ {1, scsi_low_msginfunc_rejop},
2585 /* 6 */ {1, scsi_low_msginfunc_rejop},
2586 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2587 /* 8 */ {1, scsi_low_msginfunc_noop},
2588 /* 9 */ {1, scsi_low_msginfunc_parity},
2589 /* a */ {1, scsi_low_msginfunc_lcc},
2590 /* b */ {1, scsi_low_msginfunc_lcc},
2591 /* c */ {1, scsi_low_msginfunc_rejop},
2592 /* d */ {2, scsi_low_msginfunc_rejop},
2593 /* e */ {1, scsi_low_msginfunc_rejop},
2594 /* f */ {1, scsi_low_msginfunc_rejop},
2595 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2596 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2597 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2598 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2599 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2600 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2601 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2602 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2603 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2604 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2605 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2606 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2607 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2608 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2609 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2610 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2611 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2612 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2613 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2614 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2615 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2616 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2617 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2618 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2619 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2620 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2621 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2622 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2623 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2624 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2625 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2626 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2627 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2630 /**************************************************************
2632 **************************************************************/
2634 scsi_low_msgfunc_synch(slp)
2635 struct scsi_low_softc *slp;
2637 struct targ_info *ti = slp->sl_Tnexus;
2638 int ptr = ti->ti_msgoutlen;
2640 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2641 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2642 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2643 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2644 return MSG_EXTEND_SYNCHLEN + 2;
2648 scsi_low_msgfunc_wide(slp)
2649 struct scsi_low_softc *slp;
2651 struct targ_info *ti = slp->sl_Tnexus;
2652 int ptr = ti->ti_msgoutlen;
2654 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2655 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2656 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2657 return MSG_EXTEND_WIDELEN + 2;
2661 scsi_low_msgfunc_identify(slp)
2662 struct scsi_low_softc *slp;
2664 struct targ_info *ti = slp->sl_Tnexus;
2665 struct lun_info *li = slp->sl_Lnexus;
2666 struct slccb *cb = slp->sl_Qnexus;
2667 int ptr = ti->ti_msgoutlen;
2673 slp->sl_error |= FATALIO;
2674 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2675 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2679 if (scsi_low_is_disconnect_ok(cb) != 0)
2680 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2684 if (ti->ti_phase == PH_MSGOUT)
2686 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2687 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2689 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2693 ti->ti_msgoutstr[ptr + 0] = msg;
2698 scsi_low_msgfunc_abort(slp)
2699 struct scsi_low_softc *slp;
2702 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2707 scsi_low_msgfunc_qabort(slp)
2708 struct scsi_low_softc *slp;
2711 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2716 scsi_low_msgfunc_reset(slp)
2717 struct scsi_low_softc *slp;
2720 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2725 scsi_low_msgfunc_qtag(slp)
2726 struct scsi_low_softc *slp;
2728 struct targ_info *ti = slp->sl_Tnexus;
2729 struct slccb *cb = slp->sl_Qnexus;
2730 int ptr = ti->ti_msgoutlen;
2732 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2734 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2739 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2740 if (ti->ti_phase == PH_MSGOUT)
2742 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2749 * The following functions are called when targets give unexpected
2750 * responces in msgin (after msgout).
2753 scsi_low_errfunc_identify(slp, msgflags)
2754 struct scsi_low_softc *slp;
2758 if (slp->sl_Lnexus != NULL)
2760 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2761 scsi_low_calcf_lun(slp->sl_Lnexus);
2767 scsi_low_errfunc_synch(slp, msgflags)
2768 struct scsi_low_softc *slp;
2771 struct targ_info *ti = slp->sl_Tnexus;
2773 MSGIN_PERIOD(ti) = 0;
2774 MSGIN_OFFSET(ti) = 0;
2775 scsi_low_synch(slp);
2780 scsi_low_errfunc_wide(slp, msgflags)
2781 struct scsi_low_softc *slp;
2784 struct targ_info *ti = slp->sl_Tnexus;
2786 MSGIN_WIDTHP(ti) = 0;
2792 scsi_low_errfunc_qtag(slp, msgflags)
2793 struct scsi_low_softc *slp;
2797 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2799 if (slp->sl_Qnexus != NULL)
2801 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2803 if (slp->sl_Lnexus != NULL)
2805 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2806 scsi_low_calcf_lun(slp->sl_Lnexus);
2808 device_printf(slp->sl_dev, "scsi_low: qtag msg rejected\n");
2815 scsi_low_msgout(slp, ti, fl)
2816 struct scsi_low_softc *slp;
2817 struct targ_info *ti;
2820 struct scsi_low_msgout_data *mdp;
2823 #ifdef SCSI_LOW_DIAGNOSTIC
2824 if (ti != slp->sl_Tnexus)
2826 scsi_low_print(slp, NULL);
2827 panic("scsi_low_msgout: Target nexus inconsistent");
2829 #endif /* SCSI_LOW_DIAGNOSTIC */
2831 slp->sl_ph_count ++;
2832 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2834 device_printf(slp->sl_dev, "too many phase changes\n");
2835 slp->sl_error |= FATALIO;
2836 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2840 * Scsi phase changes.
2841 * Previously msgs asserted are accepted by our target or
2842 * processed by scsi_low_msgin.
2843 * Thus clear all saved informations.
2845 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2847 ti->ti_omsgflags = 0;
2848 ti->ti_emsgflags = 0;
2850 else if (slp->sl_atten == 0)
2853 * We did not assert attention, however still our target required
2854 * msgs. Resend previous msgs.
2856 ti->ti_msgflags |= ti->ti_omsgflags;
2857 ti->ti_omsgflags = 0;
2858 #ifdef SCSI_LOW_DIAGNOSTIC
2859 device_printf(slp->sl_dev, "scsi_low_msgout: retry msgout\n");
2860 #endif /* SCSI_LOW_DIAGNOSTIC */
2864 * We have no msgs. send MSG_NOOP (OK?)
2866 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2867 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2872 ti->ti_msgoutlen = 0;
2873 slp->sl_clear_atten = 0;
2874 mdp = &scsi_low_msgout_data[0];
2875 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2877 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2879 ti->ti_omsgflags |= mdp->md_flags;
2880 ti->ti_msgflags &= ~mdp->md_flags;
2881 ti->ti_emsgflags = mdp->md_flags;
2883 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2884 if (mdp->md_msgfunc != NULL)
2885 len = (*mdp->md_msgfunc) (slp);
2889 #ifdef SCSI_LOW_DIAGNOSTIC
2890 scsi_low_msg_log_write(&ti->ti_log_msgout,
2891 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2892 #endif /* SCSI_LOW_DIAGNOSTIC */
2894 ti->ti_msgoutlen += len;
2895 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2897 slp->sl_clear_atten = 1;
2901 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2902 ti->ti_msgflags == 0)
2905 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
2910 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2911 slp->sl_clear_atten = 1;
2913 return ti->ti_msgoutlen;
2916 /**************************************************************
2918 **************************************************************/
2920 scsi_low_msginfunc_noop(slp)
2921 struct scsi_low_softc *slp;
2928 scsi_low_msginfunc_rejop(slp)
2929 struct scsi_low_softc *slp;
2931 struct targ_info *ti = slp->sl_Tnexus;
2932 u_int8_t msg = ti->ti_msgin[0];
2934 device_printf(slp->sl_dev, "MSGIN: msg 0x%x rejected\n", (u_int) msg);
2935 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
2940 scsi_low_msginfunc_cc(slp)
2941 struct scsi_low_softc *slp;
2943 struct lun_info *li;
2945 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
2947 /* validate status */
2948 if (slp->sl_Qnexus == NULL)
2951 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2952 li = slp->sl_Lnexus;
2953 switch (slp->sl_scp.scp_status)
2956 li->li_maxnqio = li->li_maxnexus;
2961 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
2962 scsi_low_reset_nexus_lun(slp, li, 0);
2970 if (li->li_maxnexus >= li->li_nqio)
2971 li->li_maxnexus = li->li_nqio - 1;
2972 li->li_maxnqio = li->li_maxnexus;
2977 slp->sl_error |= MSGERR;
2987 scsi_low_msginfunc_lcc(slp)
2988 struct scsi_low_softc *slp;
2990 struct targ_info *ti;
2991 struct lun_info *li;
2992 struct slccb *ncb, *cb;
2994 ti = slp->sl_Tnexus;
2995 li = slp->sl_Lnexus;
2996 if ((cb = slp->sl_Qnexus) == NULL)
2999 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3000 switch (slp->sl_scp.scp_status)
3004 li->li_maxnqio = li->li_maxnexus;
3008 slp->sl_error |= MSGERR;
3012 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3015 cb->ccb_error |= slp->sl_error;
3016 if (cb->ccb_error != 0)
3019 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3020 ncb = TAILQ_NEXT(ncb, ccb_chain))
3023 goto cmd_link_start;
3028 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3029 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3033 ncb->ccb_flags &= ~CCB_STARTQ;
3034 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3036 scsi_low_dealloc_qtag(ncb);
3037 ncb->ccb_tag = cb->ccb_tag;
3038 ncb->ccb_otag = cb->ccb_otag;
3039 cb->ccb_tag = SCSI_LOW_UNKTAG;
3040 cb->ccb_otag = SCSI_LOW_UNKTAG;
3041 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3042 panic("%s: linked ccb retried",
3043 device_get_nameunit(slp->sl_dev));
3045 slp->sl_Qnexus = ncb;
3046 slp->sl_ph_count = 0;
3049 ncb->ccb_datalen = -1;
3050 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3051 ncb->ccb_flags &= ~CCB_INTERNAL;
3053 scsi_low_init_msgsys(slp, ti);
3055 scsi_low_ccb_setup_cam(slp, ncb);
3057 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3058 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3059 ncb->ccb_tc = ncb->ccb_tcmax;
3061 /* setup saved scsi data pointer */
3062 ncb->ccb_sscp = ncb->ccb_scp;
3063 slp->sl_scp = ncb->ccb_sscp;
3064 slp->sl_error = ncb->ccb_error;
3066 #ifdef SCSI_LOW_DIAGNOSTIC
3067 scsi_low_msg_log_init(&ti->ti_log_msgin);
3068 scsi_low_msg_log_init(&ti->ti_log_msgout);
3069 #endif /* SCSI_LOW_DIAGNOSTIC */
3074 scsi_low_msginfunc_disc(slp)
3075 struct scsi_low_softc *slp;
3078 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3083 scsi_low_msginfunc_sdp(slp)
3084 struct scsi_low_softc *slp;
3086 struct slccb *cb = slp->sl_Qnexus;
3090 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3091 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3094 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3099 scsi_low_msginfunc_rp(slp)
3100 struct scsi_low_softc *slp;
3103 if (slp->sl_Qnexus != NULL)
3104 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3106 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3112 struct scsi_low_softc *slp;
3114 struct targ_info *ti = slp->sl_Tnexus;
3115 u_int period = 0, offset = 0, speed;
3119 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3120 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3121 MSGIN_OFFSET(ti) == 0)
3123 if ((offset = MSGIN_OFFSET(ti)) != 0)
3124 period = MSGIN_PERIOD(ti);
3125 s = offset ? "synchronous" : "async";
3130 * Target seems to be brain damaged.
3131 * Force async transfer.
3133 ti->ti_maxsynch.period = 0;
3134 ti->ti_maxsynch.offset = 0;
3135 device_printf(slp->sl_dev,
3136 "target brain damaged. async transfer\n");
3140 ti->ti_maxsynch.period = period;
3141 ti->ti_maxsynch.offset = offset;
3143 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3147 * Current period and offset are not acceptable
3149 * The adapter changes max synch and max offset.
3151 device_printf(slp->sl_dev,
3152 "synch neg failed. retry synch msg neg ...\n");
3156 ti->ti_osynch = ti->ti_maxsynch;
3159 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3163 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3165 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3166 struct slccb *cb = slp->sl_Qnexus;
3168 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3170 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3172 device_printf(slp->sl_dev,
3173 "(%d:*): <%s> offset %d period %dns ",
3174 ti->ti_id, s, offset, period * 4);
3178 speed = 1000 * 10 / (period * 4);
3179 printf("%d.%d M/s", speed / 10, speed % 10);
3188 struct scsi_low_softc *slp;
3190 struct targ_info *ti = slp->sl_Tnexus;
3193 ti->ti_width = MSGIN_WIDTHP(ti);
3194 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3198 * Current width is not acceptable for our adapter.
3199 * The adapter changes max width.
3201 device_printf(slp->sl_dev,
3202 "wide neg failed. retry wide msg neg ...\n");
3206 ti->ti_owidth = ti->ti_width;
3207 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3209 ti->ti_setup_msg_done |=
3210 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3214 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3216 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3217 struct slccb *cb = slp->sl_Qnexus;
3219 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3221 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3223 device_printf(slp->sl_dev, "(%d:*): transfer width %d bits\n",
3224 ti->ti_id, 1 << (3 + ti->ti_width));
3230 scsi_low_msginfunc_simple_qtag(slp)
3231 struct scsi_low_softc *slp;
3233 struct targ_info *ti = slp->sl_Tnexus;
3234 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3236 if (slp->sl_Qnexus != NULL)
3238 if (slp->sl_Qnexus->ccb_tag != etag)
3240 slp->sl_error |= FATALIO;
3241 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3242 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3245 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3247 #ifdef SCSI_LOW_DEBUG
3248 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3250 #endif /* SCSI_LOW_DEBUG */
3252 slp->sl_error |= FATALIO;
3253 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3254 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3260 scsi_low_msginfunc_i_wide_residue(slp)
3261 struct scsi_low_softc *slp;
3263 struct targ_info *ti = slp->sl_Tnexus;
3264 struct slccb *cb = slp->sl_Qnexus;
3265 int res = (int) ti->ti_msgin[1];
3267 if (cb == NULL || res <= 0 ||
3268 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3269 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3272 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3275 slp->sl_scp.scp_datalen += res;
3276 slp->sl_scp.scp_data -= res;
3277 scsi_low_data_finish(slp);
3282 scsi_low_msginfunc_ext(slp)
3283 struct scsi_low_softc *slp;
3285 struct slccb *cb = slp->sl_Qnexus;
3286 struct lun_info *li = slp->sl_Lnexus;
3287 struct targ_info *ti = slp->sl_Tnexus;
3291 if (ti->ti_msginptr == 2)
3293 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3297 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3299 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3303 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3304 count = (int) htonl((long) (*ptr));
3305 if(slp->sl_scp.scp_datalen - count < 0 ||
3306 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3309 slp->sl_scp.scp_datalen -= count;
3310 slp->sl_scp.scp_data += count;
3313 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3317 retry = scsi_low_synch(slp);
3318 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3319 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3321 #ifdef SCSI_LOW_DEBUG
3322 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3324 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3326 #endif /* SCSI_LOW_DEBUG */
3329 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3333 retry = scsi_low_wide(slp);
3334 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3335 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3343 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3348 scsi_low_msginfunc_parity(slp)
3349 struct scsi_low_softc *slp;
3351 struct targ_info *ti = slp->sl_Tnexus;
3353 /* only I -> T, invalid! */
3354 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3359 scsi_low_msginfunc_msg_reject(slp)
3360 struct scsi_low_softc *slp;
3362 struct targ_info *ti = slp->sl_Tnexus;
3363 struct scsi_low_msgout_data *mdp;
3366 if (ti->ti_emsgflags != 0)
3368 device_printf(slp->sl_dev, "msg flags [0x%x] rejected\n",
3370 msgflags = SCSI_LOW_MSG_REJECT;
3371 mdp = &scsi_low_msgout_data[0];
3372 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3374 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3376 ti->ti_emsgflags &= ~mdp->md_flags;
3377 if (mdp->md_errfunc != NULL)
3378 (*mdp->md_errfunc) (slp, msgflags);
3386 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3387 slp->sl_error |= MSGERR;
3393 scsi_low_msgin(slp, ti, c)
3394 struct scsi_low_softc *slp;
3395 struct targ_info *ti;
3398 struct scsi_low_msgin_data *sdp;
3399 struct lun_info *li;
3402 #ifdef SCSI_LOW_DIAGNOSTIC
3403 if (ti != slp->sl_Tnexus)
3405 scsi_low_print(slp, NULL);
3406 panic("scsi_low_msgin: Target nexus inconsistent");
3408 #endif /* SCSI_LOW_DIAGNOSTIC */
3411 * Phase changes, clear the pointer.
3413 if (ti->ti_ophase != ti->ti_phase)
3416 ti->ti_msgin_parity_error = 0;
3418 slp->sl_ph_count ++;
3419 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3421 device_printf(slp->sl_dev, "too many phase changes\n");
3422 slp->sl_error |= FATALIO;
3423 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3428 * Store a current messages byte into buffer and
3429 * wait for the completion of the current msg.
3431 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3432 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3434 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3435 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3439 * Check parity errors.
3441 if ((c & SCSI_LOW_DATA_PE) != 0)
3443 ti->ti_msgin_parity_error ++;
3444 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3448 if (ti->ti_msgin_parity_error != 0)
3452 * Calculate messages length.
3454 msg = ti->ti_msgin[0];
3455 if (msg < MSGIN_DATA_LAST)
3456 sdp = &scsi_low_msgin_data[msg];
3458 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3460 if (ti->ti_msginlen == 0)
3462 ti->ti_msginlen = sdp->md_len;
3468 if (ti->ti_msginptr < ti->ti_msginlen)
3474 if ((msg & MSG_IDENTIFY) == 0)
3476 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3481 li = slp->sl_Lnexus;
3484 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3487 slp->sl_Lnexus = li;
3488 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3492 if (MSGCMD_LUN(msg) != li->li_lun)
3496 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3498 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3500 #ifdef SCSI_LOW_DEBUG
3501 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3505 #endif /* SCSI_LOW_DEBUG */
3513 * Msg process completed, reset msgin pointer and assert ATN if desired.
3516 slp->sl_error |= FATALIO;
3517 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3518 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3521 if (ti->ti_msginptr < ti->ti_msginlen)
3524 #ifdef SCSI_LOW_DIAGNOSTIC
3525 scsi_low_msg_log_write(&ti->ti_log_msgin,
3526 &ti->ti_msgin[0], ti->ti_msginlen);
3527 #endif /* SCSI_LOW_DIAGNOSTIC */
3533 /**********************************************************
3535 **********************************************************/
3537 scsi_low_disconnected(slp, ti)
3538 struct scsi_low_softc *slp;
3539 struct targ_info *ti;
3541 struct slccb *cb = slp->sl_Qnexus;
3543 /* check phase completion */
3544 switch (slp->sl_msgphase)
3547 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3548 scsi_low_msginfunc_cc(slp);
3549 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3553 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3554 scsi_low_msginfunc_cc(slp);
3555 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3559 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3560 scsi_low_msginfunc_cc(slp);
3566 struct lun_info *li;
3569 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3570 cb->ccb_flags |= CCB_DISCQ;
3571 cb->ccb_error |= slp->sl_error;
3577 #ifdef SCSI_LOW_STATICS
3578 scsi_low_statics.nexus_disconnected ++;
3579 #endif /* SCSI_LOW_STATICS */
3581 #ifdef SCSI_LOW_DEBUG
3582 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3584 printf("## SCSI_LOW_DISCONNECTED ===============\n");
3585 scsi_low_print(slp, NULL);
3587 #endif /* SCSI_LOW_DEBUG */
3591 slp->sl_error |= FATALIO;
3592 if (ti->ti_phase == PH_SELSTART)
3593 slp->sl_error |= SELTIMEOUTIO;
3595 slp->sl_error |= UBFERR;
3604 #ifdef SCSI_LOW_DEBUG
3605 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3607 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3608 (cb->ccb_msgoutflag != 0 ||
3609 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3611 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3614 #endif /* SCSI_LOW_DEBUG */
3616 cb->ccb_error |= slp->sl_error;
3617 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3619 cb->ccb_flags |= CCB_STARTQ;
3620 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3625 scsi_low_bus_release(slp, ti);
3626 scsi_low_start(slp);
3630 /**********************************************************
3632 **********************************************************/
3634 scsi_low_alloc_qtag(cb)
3637 struct lun_info *li = cb->li;
3638 scsi_low_tag_t etag;
3640 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3643 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3644 etag = ffs(li->li_qtagbits);
3648 li->li_qtagbits &= ~(1 << (etag - 1));
3649 cb->ccb_otag = etag;
3652 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3653 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3654 if (li->li_qtagarray[li->li_qd] == 0)
3657 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3658 if (li->li_qtagarray[li->li_qd] == 0)
3664 li->li_qtagarray[li->li_qd] ++;
3665 cb->ccb_otag = (li->li_qd ++);
3667 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3671 scsi_low_dealloc_qtag(cb)
3674 struct lun_info *li = cb->li;
3675 scsi_low_tag_t etag;
3677 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3680 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3681 etag = cb->ccb_otag - 1;
3682 #ifdef SCSI_LOW_DIAGNOSTIC
3683 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3684 panic("scsi_low_dealloc_tag: illegal tag");
3685 #endif /* SCSI_LOW_DIAGNOSTIC */
3686 li->li_qtagbits |= (1 << etag);
3688 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3689 etag = cb->ccb_otag;
3690 #ifdef SCSI_LOW_DIAGNOSTIC
3691 if (etag >= SCSI_LOW_MAXNEXUS)
3692 panic("scsi_low_dealloc_tag: illegal tag");
3693 #endif /* SCSI_LOW_DIAGNOSTIC */
3694 li->li_qtagarray[etag] --;
3695 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3697 cb->ccb_otag = SCSI_LOW_UNKTAG;
3701 static struct slccb *
3702 scsi_low_revoke_ccb(slp, cb, fdone)
3703 struct scsi_low_softc *slp;
3707 struct targ_info *ti = cb->ti;
3708 struct lun_info *li = cb->li;
3710 #ifdef SCSI_LOW_DIAGNOSTIC
3711 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3712 (CCB_STARTQ | CCB_DISCQ))
3714 panic("%s: ccb in both queue",
3715 device_get_nameunit(slp->sl_dev));
3717 #endif /* SCSI_LOW_DIAGNOSTIC */
3719 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3721 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3724 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3726 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3732 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3733 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3736 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3737 (cb->ccb_flags & CCB_NORETRY) != 0))
3739 cb->ccb_error |= FATALIO;
3740 cb->ccb_flags &= ~CCB_AUTOSENSE;
3741 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3742 panic("%s: done ccb retried",
3743 device_get_nameunit(slp->sl_dev));
3748 cb->ccb_error |= PENDINGIO;
3749 scsi_low_deactivate_qtag(cb);
3750 scsi_low_ccb_message_retry(cb);
3751 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3757 scsi_low_reset_nexus_lun(slp, li, fdone)
3758 struct scsi_low_softc *slp;
3759 struct lun_info *li;
3762 struct slccb *cb, *ncb, *ecb;
3768 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3770 ncb = TAILQ_NEXT(cb, ccb_chain);
3771 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3775 * presumely keep ordering of io
3777 cb->ccb_flags |= CCB_STARTQ;
3780 TAILQ_INSERT_HEAD(&slp->sl_start,\
3785 TAILQ_INSERT_AFTER(&slp->sl_start,\
3786 ecb, cb, ccb_chain);
3793 /**************************************************************
3795 **************************************************************/
3797 scsi_low_calcf_lun(li)
3798 struct lun_info *li;
3800 struct targ_info *ti = li->li_ti;
3801 struct scsi_low_softc *slp = ti->ti_sc;
3802 u_int cfgflags, diskflags;
3804 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3805 cfgflags = li->li_cfgflags;
3809 diskflags = li->li_diskflags & li->li_quirks;
3812 li->li_flags &= ~SCSI_LOW_DISC;
3813 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3814 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3815 (cfgflags & SCSI_LOW_DISC) != 0)
3816 li->li_flags |= SCSI_LOW_DISC;
3819 li->li_flags |= SCSI_LOW_NOPARITY;
3820 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3821 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3822 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3823 li->li_flags &= ~SCSI_LOW_NOPARITY;
3826 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3827 (cfgflags & SCSI_LOW_QTAG) != 0 &&
3828 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3830 li->li_flags |= SCSI_LOW_QTAG;
3831 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3832 li->li_maxnqio = li->li_maxnexus;
3836 li->li_flags &= ~SCSI_LOW_QTAG;
3837 li->li_maxnexus = 0;
3838 li->li_maxnqio = li->li_maxnexus;
3842 li->li_flags &= ~SCSI_LOW_LINK;
3843 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3844 (diskflags & SCSI_LOW_DISK_LINK) != 0)
3845 li->li_flags |= SCSI_LOW_LINK;
3847 /* compatible flags */
3848 li->li_flags &= ~SCSI_LOW_SYNC;
3849 if (ti->ti_maxsynch.offset > 0)
3850 li->li_flags |= SCSI_LOW_SYNC;
3852 #ifdef SCSI_LOW_DEBUG
3853 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3855 scsi_low_calcf_show(li);
3857 #endif /* SCSI_LOW_DEBUG */
3861 scsi_low_calcf_target(ti)
3862 struct targ_info *ti;
3864 struct scsi_low_softc *slp = ti->ti_sc;
3865 u_int offset, period, diskflags;
3867 diskflags = ti->ti_diskflags & ti->ti_quirks;
3870 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3871 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3873 offset = ti->ti_maxsynch.offset;
3874 period = ti->ti_maxsynch.period;
3875 if (offset == 0 || period == 0)
3876 offset = period = 0;
3880 offset = period = 0;
3883 ti->ti_maxsynch.offset = offset;
3884 ti->ti_maxsynch.period = period;
3887 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3888 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3889 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3891 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3892 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3893 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3895 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3897 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3898 ti->ti_maxsynch.period != ti->ti_osynch.period)
3899 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3900 if (ti->ti_width != ti->ti_owidth)
3901 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3903 ti->ti_osynch = ti->ti_maxsynch;
3904 ti->ti_owidth = ti->ti_width;
3907 #ifdef SCSI_LOW_DEBUG
3908 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3910 device_printf(slp->sl_dev,
3911 "(%d:*): max period(%dns) offset(%d) width(%d)\n",
3913 ti->ti_maxsynch.period * 4,
3914 ti->ti_maxsynch.offset,
3917 #endif /* SCSI_LOW_DEBUG */
3921 scsi_low_calcf_show(li)
3922 struct lun_info *li;
3924 struct targ_info *ti = li->li_ti;
3925 struct scsi_low_softc *slp = ti->ti_sc;
3927 device_printf(slp->sl_dev,
3928 "(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
3929 ti->ti_id, li->li_lun,
3930 ti->ti_maxsynch.period * 4,
3931 ti->ti_maxsynch.offset,
3933 li->li_flags, SCSI_LOW_BITS);
3936 #ifdef SCSI_LOW_START_UP_CHECK
3937 /**************************************************************
3938 * scsi world start up
3939 **************************************************************/
3940 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
3943 scsi_low_start_up(slp)
3944 struct scsi_low_softc *slp;
3946 struct targ_info *ti;
3947 struct lun_info *li;
3951 device_printf(slp->sl_dev, "scsi_low: probing all devices ....\n");
3953 for (target = 0; target < slp->sl_ntargs; target ++)
3955 if (target == slp->sl_hostid)
3957 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3959 device_printf(slp->sl_dev,
3960 "scsi_low: target %d (host card)\n",
3966 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3968 device_printf(slp->sl_dev, "scsi_low: target %d lun ",
3972 ti = slp->sl_ti[target];
3973 for (lun = 0; lun < slp->sl_nluns; lun ++)
3975 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
3981 li = scsi_low_alloc_li(ti, lun, 1);
3983 scsi_low_enqueue(slp, ti, li, cb,
3984 CCB_AUTOSENSE | CCB_POLLED, 0);
3986 scsi_low_poll(slp, cb);
3988 if (li->li_state != SCSI_LOW_LUN_OK)
3991 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3997 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4006 scsi_low_poll(slp, cb)
4007 struct scsi_low_softc *slp;
4013 while (slp->sl_nio > 0)
4015 DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4017 (*slp->sl_funcs->scsi_low_poll) (slp);
4018 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4022 scsi_low_timeout_check(slp);
4027 #endif /* SCSI_LOW_START_UP_CHECK */
4029 /**********************************************************
4031 **********************************************************/
4032 #ifdef SCSI_LOW_DEBUG
4034 scsi_low_test_abort(slp, ti, li)
4035 struct scsi_low_softc *slp;
4036 struct targ_info *ti;
4037 struct lun_info *li;
4041 if (li->li_disc > 1)
4043 acb = TAILQ_FIRST(&li->li_discq);
4044 if (scsi_low_abort_ccb(slp, acb) == 0)
4046 device_printf(slp->sl_dev,
4047 "aborting ccb(0x%lx) start\n", (u_long) acb);
4053 scsi_low_test_atten(slp, ti, msg)
4054 struct scsi_low_softc *slp;
4055 struct targ_info *ti;
4059 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4060 scsi_low_assert_msg(slp, ti, msg, 0);
4062 device_printf(slp->sl_dev, "atten check OK\n");
4066 scsi_low_test_cmdlnk(slp, cb)
4067 struct scsi_low_softc *slp;
4070 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4072 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4075 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4076 slp->sl_scp.scp_cmdlen);
4077 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4078 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4080 #endif /* SCSI_LOW_DEBUG */
4083 scsi_low_info(slp, ti, s)
4084 struct scsi_low_softc *slp;
4085 struct targ_info *ti;
4090 slp = LIST_FIRST(&sl_tab);
4094 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4097 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
4099 scsi_low_print(slp, ti);
4104 scsi_low_print(slp, ti);
4108 static u_char *phase[] =
4110 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4111 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4115 scsi_low_print(slp, ti)
4116 struct scsi_low_softc *slp;
4117 struct targ_info *ti;
4119 struct lun_info *li;
4123 if (ti == NULL || ti == slp->sl_Tnexus)
4125 ti = slp->sl_Tnexus;
4126 li = slp->sl_Lnexus;
4127 cb = slp->sl_Qnexus;
4131 li = LIST_FIRST(&ti->ti_litab);
4132 cb = TAILQ_FIRST(&li->li_discq);
4136 device_printf(slp->sl_dev,
4137 "=== NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4138 (u_long) ti, (u_long) li, (u_long) cb, slp->sl_nio);
4143 u_int flags = 0, maxnqio = 0, nqio = 0;
4144 int lun = CAM_LUN_WILDCARD;
4149 flags = li->li_flags;
4150 maxnqio = li->li_maxnqio;
4154 device_printf(slp->sl_dev,
4155 "(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4156 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4157 phase[(int) ti->ti_phase], ti->ti_disc,
4162 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4163 (u_int) cb->ccb_scp.scp_cmd[0],
4164 cb->ccb_scp.scp_cmdlen,
4166 cb->ccb_scp.scp_datalen,
4167 (u_int) cb->ccb_sscp.scp_status,
4168 cb->ccb_error, SCSI_LOW_ERRORBITS);
4171 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4172 (u_int) (ti->ti_msginptr),
4173 (u_int) (ti->ti_msgin[0]),
4174 (u_int) (ti->ti_msgin[1]),
4175 (u_int) (ti->ti_msgin[2]),
4176 (u_int) (ti->ti_msgin[3]),
4177 (u_int) (ti->ti_msgin[4]),
4180 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4181 (u_int) ti->ti_msgflags,
4182 (u_int) (ti->ti_msgoutstr[0]),
4183 (u_int) (ti->ti_msgoutstr[1]),
4184 (u_int) (ti->ti_msgoutstr[2]),
4185 (u_int) (ti->ti_msgoutstr[3]),
4186 (u_int) (ti->ti_msgoutstr[4]),
4188 flags, SCSI_LOW_BITS);
4190 #ifdef SCSI_LOW_DIAGNOSTIC
4191 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4192 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4193 #endif /* SCSI_LOW_DIAGNOSTIC */
4197 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4198 (u_long) sp->scp_data,
4200 (u_int) sp->scp_status,
4201 slp->sl_error, SCSI_LOW_ERRORBITS);