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 * [NetBSD for NEC PC-98 series]
21 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
22 * NetBSD/pc98 porting staff. All rights reserved.
23 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
24 * Naofumi HONDA. All rights reserved.
26 * [Ported for FreeBSD CAM]
27 * Copyright (c) 2000, 2001
28 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The name of the author may not be used to endorse or promote products
40 * derived from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
55 /* <On the nexus establishment>
56 * When our host is reselected,
57 * nexus establish processes are little complicated.
58 * Normal steps are followings:
59 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
60 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
61 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
70 #include <sys/queue.h>
71 #include <sys/malloc.h>
72 #include <sys/errno.h>
75 #include <cam/cam_ccb.h>
76 #include <cam/cam_sim.h>
77 #include <cam/cam_debug.h>
78 #include <cam/cam_periph.h>
79 #include <cam/cam_xpt_periph.h>
81 #include <cam/scsi/scsi_all.h>
82 #include <cam/scsi/scsi_message.h>
84 #include <cam/scsi/scsi_low.h>
88 /**************************************************************
90 **************************************************************/
91 #define SCSI_LOW_POLL_HZ 1000
93 /* functions return values */
94 #define SCSI_LOW_START_NO_QTAG 0
95 #define SCSI_LOW_START_QTAG 1
97 #define SCSI_LOW_DONE_COMPLETE 0
98 #define SCSI_LOW_DONE_RETRY 1
100 /* internal disk flags */
101 #define SCSI_LOW_DISK_DISC 0x00000001
102 #define SCSI_LOW_DISK_QTAG 0x00000002
103 #define SCSI_LOW_DISK_LINK 0x00000004
104 #define SCSI_LOW_DISK_PARITY 0x00000008
105 #define SCSI_LOW_DISK_SYNC 0x00010000
106 #define SCSI_LOW_DISK_WIDE_16 0x00020000
107 #define SCSI_LOW_DISK_WIDE_32 0x00040000
108 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
109 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
110 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
112 static MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
114 /**************************************************************
116 **************************************************************/
117 /* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
118 static void scsi_low_engage(void *);
119 static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
120 static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
121 static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
122 static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
123 static void scsi_low_twiddle_wait(void);
124 static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
125 static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
126 static void scsi_low_calcf_lun(struct lun_info *);
127 static void scsi_low_calcf_target(struct targ_info *);
128 static void scsi_low_calcf_show(struct lun_info *);
129 static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
130 static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
131 static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
132 static int scsi_low_init(struct scsi_low_softc *, u_int);
133 static void scsi_low_start(struct scsi_low_softc *);
134 static void scsi_low_free_ti(struct scsi_low_softc *);
136 static int scsi_low_alloc_qtag(struct slccb *);
137 static int scsi_low_dealloc_qtag(struct slccb *);
138 static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
139 static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
140 static void scsi_low_unit_ready_cmd(struct slccb *);
141 static void scsi_low_timeout(void *);
142 static int scsi_low_timeout_check(struct scsi_low_softc *);
143 #ifdef SCSI_LOW_START_UP_CHECK
144 static int scsi_low_start_up(struct scsi_low_softc *);
145 #endif /* SCSI_LOW_START_UP_CHECK */
146 static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
147 static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);
149 int scsi_low_version_major = 2;
150 int scsi_low_version_minor = 17;
152 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
153 static struct mtx sl_tab_lock;
154 MTX_SYSINIT(sl_tab_lock, &sl_tab_lock, "scsi low table", MTX_DEF);
156 /**************************************************************
157 * Debug, Run test and Statics
158 **************************************************************/
159 #ifdef SCSI_LOW_INFO_DETAIL
160 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
161 #else /* !SCSI_LOW_INFO_DETAIL */
162 #define SCSI_LOW_INFO(slp, ti, s) device_printf((slp)->sl_dev, "%s\n", (s))
163 #endif /* !SCSI_LOW_INFO_DETAIL */
165 #ifdef SCSI_LOW_STATICS
166 static struct scsi_low_statics {
169 int nexus_disconnected;
170 int nexus_reselected;
173 #endif /* SCSI_LOW_STATICS */
175 #ifdef SCSI_LOW_DEBUG
176 #define SCSI_LOW_DEBUG_DONE 0x00001
177 #define SCSI_LOW_DEBUG_DISC 0x00002
178 #define SCSI_LOW_DEBUG_SENSE 0x00004
179 #define SCSI_LOW_DEBUG_CALCF 0x00008
180 #define SCSI_LOW_DEBUG_ACTION 0x10000
181 int scsi_low_debug = 0;
183 #define SCSI_LOW_MAX_ATTEN_CHECK 32
184 #define SCSI_LOW_ATTEN_CHECK 0x0001
185 #define SCSI_LOW_CMDLNK_CHECK 0x0002
186 #define SCSI_LOW_ABORT_CHECK 0x0004
187 #define SCSI_LOW_NEXUS_CHECK 0x0008
188 int scsi_low_test = 0;
189 int scsi_low_test_id = 0;
191 static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
192 static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
193 static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
194 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
195 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
196 #define SCSI_LOW_DEBUG_GO(fl, id) \
197 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
198 #endif /* SCSI_LOW_DEBUG */
200 /**************************************************************
202 **************************************************************/
203 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
204 GENERIC_CCB(scsi_low, slccb, ccb_chain)
206 /**************************************************************
208 **************************************************************/
209 #define SCSI_LOW_INLINE static __inline
210 SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
211 SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
212 SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
213 SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
214 SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
215 SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
216 SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);
219 scsi_low_activate_qtag(cb)
222 struct lun_info *li = cb->li;
224 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
228 cb->ccb_tag = cb->ccb_otag;
232 scsi_low_deactivate_qtag(cb)
235 struct lun_info *li = cb->li;
237 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
241 cb->ccb_tag = SCSI_LOW_UNKTAG;
245 scsi_low_ccb_message_exec(slp, cb)
246 struct scsi_low_softc *slp;
250 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
251 cb->ccb_msgoutflag = 0;
255 scsi_low_ccb_message_assert(cb, msg)
260 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
264 scsi_low_ccb_message_retry(cb)
267 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
271 scsi_low_ccb_message_clear(cb)
274 cb->ccb_msgoutflag = 0;
278 scsi_low_init_msgsys(slp, ti)
279 struct scsi_low_softc *slp;
280 struct targ_info *ti;
284 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
285 SCSI_LOW_DEASSERT_ATN(slp);
286 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
289 /*=============================================================
290 * START OF OS switch (All OS depend fucntions should be here)
291 =============================================================*/
292 /* common os depend utitlities */
293 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
294 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
295 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
297 static u_int8_t scsi_low_cmd_flags[256] = {
298 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
299 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
300 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
301 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
302 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
305 struct scsi_low_error_code {
310 static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
311 static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);
313 static struct slccb *
314 scsi_low_find_ccb(slp, target, lun, osdep)
315 struct scsi_low_softc *slp;
319 struct targ_info *ti;
323 ti = slp->sl_ti[target];
324 li = scsi_low_alloc_li(ti, lun, 0);
328 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
331 TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
333 if (cb->osdep == osdep)
337 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
339 if (cb->osdep == osdep)
346 scsi_low_translate_error_code(cb, tp)
348 struct scsi_low_error_code *tp;
351 if (cb->ccb_error == 0)
352 return tp->error_code;
354 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
356 return tp->error_code;
359 /**************************************************************
360 * SCSI INTERFACE (CAM)
361 **************************************************************/
362 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
363 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
364 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
366 static void scsi_low_poll_cam(struct cam_sim *);
367 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
369 static int scsi_low_attach_cam(struct scsi_low_softc *);
370 static int scsi_low_detach_cam(struct scsi_low_softc *);
371 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
372 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
374 struct scsi_low_error_code scsi_low_error_code_cam[] = {
376 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
377 {SENSEERR, CAM_AUTOSENSE_FAIL},
378 {UACAERR, CAM_SCSI_STATUS_ERROR},
379 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
380 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
381 {TIMEOUTIO, CAM_CMD_TIMEOUT},
382 {PDMAERR, CAM_DATA_RUN_ERR},
383 {PARITYERR, CAM_UNCOR_PARITY},
384 {UBFERR, CAM_UNEXP_BUSFREE},
385 {ABORTIO, CAM_REQ_ABORTED},
386 {-1, CAM_UNREC_HBA_ERROR}
389 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
392 * Please check a polling hz, currently we assume scsi_low_poll() is
395 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
398 scsi_low_poll_cam(sim)
401 struct scsi_low_softc *slp = SIM2SLP(sim);
403 SCSI_LOW_ASSERT_LOCKED(slp);
404 (*slp->sl_funcs->scsi_low_poll) (slp);
406 if (slp->sl_poll_count ++ >=
407 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
409 slp->sl_poll_count = 0;
410 scsi_low_timeout_check(slp);
415 scsi_low_scsi_action_cam(sim, ccb)
419 struct scsi_low_softc *slp = SIM2SLP(sim);
420 struct targ_info *ti;
423 u_int lun, flags, msg, target;
426 SCSI_LOW_ASSERT_LOCKED(slp);
427 target = (u_int) (ccb->ccb_h.target_id);
428 lun = (u_int) ccb->ccb_h.target_lun;
430 #ifdef SCSI_LOW_DEBUG
431 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
433 device_printf(slp->sl_dev,
434 "cam_action: func code 0x%x target: %d, lun: %d\n",
435 ccb->ccb_h.func_code, target, lun);
437 #endif /* SCSI_LOW_DEBUG */
439 switch (ccb->ccb_h.func_code) {
440 case XPT_SCSI_IO: /* Execute the requested I/O operation */
441 #ifdef SCSI_LOW_DIAGNOSTIC
442 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
444 device_printf(slp->sl_dev, "invalid target/lun\n");
445 ccb->ccb_h.status = CAM_REQ_INVALID;
449 #endif /* SCSI_LOW_DIAGNOSTIC */
451 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
452 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
457 ti = slp->sl_ti[target];
460 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
461 flags = CCB_AUTOSENSE | CCB_SCSIIO;
465 li = scsi_low_alloc_li(ti, lun, 1);
467 if (ti->ti_setup_msg != 0)
469 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
472 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
474 #ifdef SCSI_LOW_DEBUG
475 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
477 scsi_low_test_abort(slp, ti, li);
479 #endif /* SCSI_LOW_DEBUG */
482 case XPT_ABORT: /* Abort the specified CCB */
483 #ifdef SCSI_LOW_DIAGNOSTIC
484 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
486 device_printf(slp->sl_dev, "invalid target/lun\n");
487 ccb->ccb_h.status = CAM_REQ_INVALID;
491 #endif /* SCSI_LOW_DIAGNOSTIC */
493 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
494 rv = scsi_low_abort_ccb(slp, cb);
497 ccb->ccb_h.status = CAM_REQ_CMP;
499 ccb->ccb_h.status = CAM_REQ_INVALID;
503 case XPT_SET_TRAN_SETTINGS: {
504 struct ccb_trans_settings_scsi *scsi;
505 struct ccb_trans_settings_spi *spi;
506 struct ccb_trans_settings *cts;
509 #ifdef SCSI_LOW_DIAGNOSTIC
510 if (target == CAM_TARGET_WILDCARD)
512 device_printf(slp->sl_dev, "invalid target\n");
513 ccb->ccb_h.status = CAM_REQ_INVALID;
517 #endif /* SCSI_LOW_DIAGNOSTIC */
519 ti = slp->sl_ti[target];
520 if (lun == CAM_LUN_WILDCARD)
523 scsi = &cts->proto_specific.scsi;
524 spi = &cts->xport_specific.spi;
525 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
526 CTS_SPI_VALID_SYNC_RATE |
527 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
529 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
530 val = spi->bus_width;
531 if (val < ti->ti_width)
534 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
535 val = spi->sync_period;
536 if (val == 0 || val > ti->ti_maxsynch.period)
537 ti->ti_maxsynch.period = val;
539 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
540 val = spi->sync_offset;
541 if (val < ti->ti_maxsynch.offset)
542 ti->ti_maxsynch.offset = val;
544 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
545 scsi_low_calcf_target(ti);
548 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
549 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
551 li = scsi_low_alloc_li(ti, lun, 1);
552 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
553 li->li_quirks |= SCSI_LOW_DISK_DISC;
555 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
558 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
559 li->li_quirks |= SCSI_LOW_DISK_QTAG;
561 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
563 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
564 scsi_low_calcf_target(ti);
565 scsi_low_calcf_lun(li);
566 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
567 scsi_low_calcf_show(li);
570 ccb->ccb_h.status = CAM_REQ_CMP;
575 case XPT_GET_TRAN_SETTINGS: {
576 struct ccb_trans_settings *cts;
580 #ifdef SCSI_LOW_DIAGNOSTIC
581 if (target == CAM_TARGET_WILDCARD)
583 device_printf(slp->sl_dev, "invalid target\n");
584 ccb->ccb_h.status = CAM_REQ_INVALID;
588 #endif /* SCSI_LOW_DIAGNOSTIC */
589 ti = slp->sl_ti[target];
590 if (lun == CAM_LUN_WILDCARD)
593 li = scsi_low_alloc_li(ti, lun, 1);
594 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
595 struct ccb_trans_settings_scsi *scsi =
596 &cts->proto_specific.scsi;
597 struct ccb_trans_settings_spi *spi =
598 &cts->xport_specific.spi;
599 #ifdef SCSI_LOW_DIAGNOSTIC
600 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
602 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
603 device_printf(slp->sl_dev,
604 "invalid GET_TRANS_CURRENT_SETTINGS call\n");
607 #endif /* SCSI_LOW_DIAGNOSTIC */
608 cts->protocol = PROTO_SCSI;
609 cts->protocol_version = SCSI_REV_2;
610 cts->transport = XPORT_SPI;
611 cts->transport_version = 2;
613 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
614 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
616 diskflags = li->li_diskflags & li->li_cfgflags;
617 if (diskflags & SCSI_LOW_DISK_DISC)
618 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
619 if (diskflags & SCSI_LOW_DISK_QTAG)
620 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
622 spi->sync_period = ti->ti_maxsynch.period;
623 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
624 spi->sync_offset = ti->ti_maxsynch.offset;
625 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
627 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
628 spi->bus_width = ti->ti_width;
630 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
631 scsi->valid = CTS_SCSI_VALID_TQ;
632 spi->valid |= CTS_SPI_VALID_DISC;
636 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
642 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
643 cam_calc_geometry(&ccb->ccg, /*extended*/1);
648 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
649 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
650 ccb->ccb_h.status = CAM_REQ_CMP;
654 case XPT_TERM_IO: /* Terminate the I/O process */
655 ccb->ccb_h.status = CAM_REQ_INVALID;
659 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
660 #ifdef SCSI_LOW_DIAGNOSTIC
661 if (target == CAM_TARGET_WILDCARD)
663 device_printf(slp->sl_dev, "invalid target\n");
664 ccb->ccb_h.status = CAM_REQ_INVALID;
668 #endif /* SCSI_LOW_DIAGNOSTIC */
670 msg = SCSI_LOW_MSG_RESET;
671 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
673 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
678 ti = slp->sl_ti[target];
679 if (lun == CAM_LUN_WILDCARD)
683 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
684 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
686 flags = CCB_NORETRY | CCB_URGENT;
688 li = scsi_low_alloc_li(ti, lun, 1);
689 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
692 case XPT_PATH_INQ: { /* Path routing inquiry */
693 struct ccb_pathinq *cpi = &ccb->cpi;
695 cpi->version_num = scsi_low_version_major;
696 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
697 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
698 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
699 cpi->hba_inquiry |= PI_WIDE_16;
700 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
701 cpi->hba_inquiry |= PI_WIDE_32;
702 if (ti->ti_maxsynch.offset > 0)
703 cpi->hba_inquiry |= PI_SDTR_ABLE;
704 cpi->target_sprt = 0;
706 cpi->hba_eng_cnt = 0;
707 cpi->max_target = slp->sl_ntargs - 1;
708 cpi->max_lun = slp->sl_nluns - 1;
709 cpi->initiator_id = slp->sl_hostid;
710 cpi->bus_id = cam_sim_bus(sim);
711 cpi->base_transfer_speed = 3300;
712 cpi->transport = XPORT_SPI;
713 cpi->transport_version = 2;
714 cpi->protocol = PROTO_SCSI;
715 cpi->protocol_version = SCSI_REV_2;
716 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
717 strlcpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
718 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
719 cpi->unit_number = cam_sim_unit(sim);
720 cpi->ccb_h.status = CAM_REQ_CMP;
726 printf("scsi_low: non support func_code = %d ",
727 ccb->ccb_h.func_code);
728 ccb->ccb_h.status = CAM_REQ_INVALID;
735 scsi_low_attach_cam(slp)
736 struct scsi_low_softc *slp;
738 struct cam_devq *devq;
741 devq = cam_simq_alloc(SCSI_LOW_NCCB);
746 * ask the adapter what subunits are present
748 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
749 slp->sl_sim = cam_sim_alloc(scsi_low_scsi_action_cam,
751 device_get_name(slp->sl_dev), slp,
752 device_get_unit(slp->sl_dev), &slp->sl_lock,
753 slp->sl_openings, tagged_openings, devq);
755 if (slp->sl_sim == NULL) {
761 if (xpt_bus_register(slp->sl_sim, slp->sl_dev, 0) != CAM_SUCCESS) {
762 cam_sim_free(slp->sl_sim, TRUE);
763 SCSI_LOW_UNLOCK(slp);
767 if (xpt_create_path(&slp->sl_path, /*periph*/NULL,
768 cam_sim_path(slp->sl_sim), CAM_TARGET_WILDCARD,
769 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
770 xpt_bus_deregister(cam_sim_path(slp->sl_sim));
771 cam_sim_free(slp->sl_sim, /*free_simq*/TRUE);
772 SCSI_LOW_UNLOCK(slp);
776 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
777 SCSI_LOW_UNLOCK(slp);
782 scsi_low_detach_cam(slp)
783 struct scsi_low_softc *slp;
786 xpt_async(AC_LOST_DEVICE, slp->sl_path, NULL);
787 xpt_free_path(slp->sl_path);
788 xpt_bus_deregister(cam_sim_path(slp->sl_sim));
789 cam_sim_free(slp->sl_sim, /* free_devq */ TRUE);
794 scsi_low_ccb_setup_cam(slp, cb)
795 struct scsi_low_softc *slp;
798 union ccb *ccb = (union ccb *) cb->osdep;
800 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
802 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
803 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
804 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
805 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
806 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
807 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
808 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
809 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
810 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
814 scsi_low_unit_ready_cmd(cb);
816 return SCSI_LOW_START_QTAG;
820 scsi_low_done_cam(slp, cb)
821 struct scsi_low_softc *slp;
826 ccb = (union ccb *) cb->osdep;
827 if (cb->ccb_error == 0)
829 ccb->ccb_h.status = CAM_REQ_CMP;
834 if (cb->ccb_rcnt >= slp->sl_max_retry)
835 cb->ccb_error |= ABORTIO;
837 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
838 (cb->ccb_error & ABORTIO) == 0)
841 if ((cb->ccb_error & SENSEIO) != 0)
843 memcpy(&ccb->csio.sense_data,
845 sizeof(ccb->csio.sense_data));
848 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
849 &scsi_low_error_code_cam[0]);
851 #ifdef SCSI_LOW_DIAGNOSTIC
852 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
853 cb->ccb_scp.scp_cmdlen > 0 &&
854 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
855 SCSI_LOW_CMD_ABORT_WARNING) != 0)
857 device_printf(slp->sl_dev,
858 "WARNING: scsi_low IO abort\n");
859 scsi_low_print(slp, NULL);
861 #endif /* SCSI_LOW_DIAGNOSTIC */
864 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
865 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
867 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
868 ccb->csio.scsi_status = 0; /* XXX */
870 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
872 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
877 /**************************************************************
878 * scsi low deactivate and activate
879 **************************************************************/
881 scsi_low_is_busy(slp)
882 struct scsi_low_softc *slp;
891 scsi_low_deactivate(slp)
892 struct scsi_low_softc *slp;
895 slp->sl_flags |= HW_INACTIVE;
896 callout_stop(&slp->sl_timeout_timer);
897 callout_stop(&slp->sl_engage_timer);
902 scsi_low_activate(slp)
903 struct scsi_low_softc *slp;
907 slp->sl_flags &= ~HW_INACTIVE;
908 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
910 slp->sl_flags |= HW_INACTIVE;
914 slp->sl_timeout_count = 0;
915 callout_reset(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ,
916 scsi_low_timeout, slp);
920 /**************************************************************
922 **************************************************************/
923 #ifdef SCSI_LOW_DIAGNOSTIC
924 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
925 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
926 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
929 scsi_low_msg_log_init(slmlp)
930 struct scsi_low_msg_log *slmlp;
937 scsi_low_msg_log_write(slmlp, datap, len)
938 struct scsi_low_msg_log *slmlp;
944 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
947 ptr = slmlp->slml_ptr ++;
948 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
949 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
950 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
951 slmlp->slml_msg[ptr].msg[ind] = 0;
955 scsi_low_msg_log_show(slmlp, s, len)
956 struct scsi_low_msg_log *slmlp;
962 printf("%s: (%d) ", s, slmlp->slml_ptr);
963 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
965 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
968 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
974 #endif /* SCSI_LOW_DIAGNOSTIC */
976 /**************************************************************
978 **************************************************************/
983 struct scsi_low_softc *slp = arg;
985 SCSI_LOW_ASSERT_LOCKED(slp);
986 switch (slp->sl_rstep)
990 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
991 callout_reset(&slp->sl_engage_timer, hz / 1000,
992 scsi_low_engage, slp);
997 slp->sl_flags &= ~HW_RESUME;
1007 scsi_low_init(slp, flags)
1008 struct scsi_low_softc *slp;
1013 slp->sl_flags |= HW_INITIALIZING;
1015 /* clear power control timeout */
1016 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1018 callout_stop(&slp->sl_engage_timer);
1019 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1021 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1024 /* reset current nexus */
1025 scsi_low_reset_nexus(slp, flags);
1026 if ((slp->sl_flags & HW_INACTIVE) != 0)
1032 if (flags != SCSI_LOW_RESTART_SOFT)
1034 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1038 slp->sl_flags &= ~HW_INITIALIZING;
1042 /**************************************************************
1044 **************************************************************/
1045 static struct lun_info *
1046 scsi_low_alloc_li(ti, lun, alloc)
1047 struct targ_info *ti;
1051 struct scsi_low_softc *slp = ti->ti_sc;
1052 struct lun_info *li;
1054 li = LIST_FIRST(&ti->ti_litab);
1057 if (li->li_lun == lun)
1060 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1062 if (li->li_lun == lun)
1064 LIST_REMOVE(li, lun_chain);
1065 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1074 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1076 panic("no lun info mem");
1078 bzero(li, ti->ti_lunsize);
1082 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1084 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1085 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1086 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1087 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1088 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1090 li->li_qtagbits = (u_int) -1;
1092 TAILQ_INIT(&li->li_discq);
1093 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1095 /* host specific structure initialization per lun */
1096 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1097 (*slp->sl_funcs->scsi_low_lun_init)
1098 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1099 scsi_low_calcf_lun(li);
1103 /**************************************************************
1104 * allocate targ_info
1105 **************************************************************/
1106 static struct targ_info *
1107 scsi_low_alloc_ti(slp, targ)
1108 struct scsi_low_softc *slp;
1111 struct targ_info *ti;
1113 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1114 TAILQ_INIT(&slp->sl_titab);
1116 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1118 panic("%s short of memory", device_get_nameunit(slp->sl_dev));
1120 bzero(ti, slp->sl_targsize);
1124 slp->sl_ti[targ] = ti;
1125 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1126 LIST_INIT(&ti->ti_litab);
1128 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1129 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1130 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1131 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1132 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1133 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1135 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1137 (*slp->sl_funcs->scsi_low_targ_init)
1138 (slp, ti, SCSI_LOW_INFO_ALLOC);
1140 scsi_low_calcf_target(ti);
1145 scsi_low_free_ti(slp)
1146 struct scsi_low_softc *slp;
1148 struct targ_info *ti, *tib;
1149 struct lun_info *li, *nli;
1151 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1153 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1155 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1157 (*slp->sl_funcs->scsi_low_lun_init)
1158 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1160 nli = LIST_NEXT(li, lun_chain);
1164 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1166 (*slp->sl_funcs->scsi_low_targ_init)
1167 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1169 tib = TAILQ_NEXT(ti, ti_chain);
1174 /**************************************************************
1176 **************************************************************/
1178 scsi_low_bus_idle(slp)
1179 struct scsi_low_softc *slp;
1182 slp->sl_retry_sel = 0;
1183 if (slp->sl_Tnexus == NULL)
1184 scsi_low_start(slp);
1188 scsi_low_timeout(arg)
1191 struct scsi_low_softc *slp = arg;
1193 SCSI_LOW_ASSERT_LOCKED(slp);
1194 (void) scsi_low_timeout_check(slp);
1195 callout_schedule(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ);
1199 scsi_low_timeout_check(slp)
1200 struct scsi_low_softc *slp;
1202 struct targ_info *ti;
1203 struct lun_info *li;
1204 struct slccb *cb = NULL; /* XXX */
1206 /* selection restart */
1207 if (slp->sl_retry_sel != 0)
1209 slp->sl_retry_sel = 0;
1210 if (slp->sl_Tnexus != NULL)
1213 cb = TAILQ_FIRST(&slp->sl_start);
1217 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1219 cb->ccb_flags |= CCB_NORETRY;
1220 cb->ccb_error |= SELTIMEOUTIO;
1221 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1222 panic("%s: ccb not finished",
1223 device_get_nameunit(slp->sl_dev));
1226 if (slp->sl_Tnexus == NULL)
1227 scsi_low_start(slp);
1230 /* call hardware timeout */
1232 if (slp->sl_funcs->scsi_low_timeout != NULL)
1234 (*slp->sl_funcs->scsi_low_timeout) (slp);
1237 if (slp->sl_timeout_count ++ <
1238 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1241 slp->sl_timeout_count = 0;
1242 if (slp->sl_nio > 0)
1244 if ((cb = slp->sl_Qnexus) != NULL)
1246 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1250 else if (slp->sl_disc == 0)
1252 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1255 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1259 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1260 ti = TAILQ_NEXT(ti, ti_chain))
1262 if (ti->ti_disc == 0)
1265 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1266 li = LIST_NEXT(li, lun_chain))
1268 for (cb = TAILQ_FIRST(&li->li_discq);
1270 cb = TAILQ_NEXT(cb, ccb_chain))
1273 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1281 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1283 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1286 if (slp->sl_active != 0)
1288 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1294 if (slp->sl_powc < 0)
1296 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1297 slp->sl_flags |= HW_POWDOWN;
1298 (*slp->sl_funcs->scsi_low_power)
1299 (slp, SCSI_LOW_POWDOWN);
1305 cb->ccb_error |= TIMEOUTIO;
1306 device_printf(slp->sl_dev, "slccb (0x%lx) timeout!\n", (u_long) cb);
1307 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1308 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1309 scsi_low_start(slp);
1315 scsi_low_abort_ccb(slp, cb)
1316 struct scsi_low_softc *slp;
1319 struct targ_info *ti;
1320 struct lun_info *li;
1325 if ((cb->ccb_omsgoutflag &
1326 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1331 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1332 msg = SCSI_LOW_MSG_ABORT;
1334 msg = SCSI_LOW_MSG_ABORT_QTAG;
1336 cb->ccb_error |= ABORTIO;
1337 cb->ccb_flags |= CCB_NORETRY;
1338 scsi_low_ccb_message_assert(cb, msg);
1340 if (cb == slp->sl_Qnexus)
1342 scsi_low_assert_msg(slp, ti, msg, 1);
1344 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1346 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1347 panic("%s: revoked ccb done",
1348 device_get_nameunit(slp->sl_dev));
1350 cb->ccb_flags |= CCB_STARTQ;
1351 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1353 if (slp->sl_Tnexus == NULL)
1354 scsi_low_start(slp);
1358 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1359 panic("%s: revoked ccb retried",
1360 device_get_nameunit(slp->sl_dev));
1365 /**************************************************************
1366 * Generic SCSI INTERFACE
1367 **************************************************************/
1369 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1370 struct scsi_low_softc *slp;
1371 int openings, ntargs, nluns, targsize, lunsize;
1373 struct targ_info *ti;
1374 struct lun_info *li;
1377 if (ntargs > SCSI_LOW_NTARGETS)
1379 printf("scsi_low: %d targets are too large\n", ntargs);
1380 printf("change kernel options SCSI_LOW_NTARGETS");
1385 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1387 slp->sl_openings = openings;
1388 slp->sl_ntargs = ntargs;
1389 slp->sl_nluns = nluns;
1390 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1392 if (lunsize < sizeof(struct lun_info))
1393 lunsize = sizeof(struct lun_info);
1395 if (targsize < sizeof(struct targ_info))
1396 targsize = sizeof(struct targ_info);
1398 slp->sl_targsize = targsize;
1399 for (i = 0; i < ntargs; i ++)
1401 ti = scsi_low_alloc_ti(slp, i);
1402 ti->ti_lunsize = lunsize;
1403 li = scsi_low_alloc_li(ti, 0, 1);
1406 /* initialize queue */
1407 nccb = openings * ntargs;
1408 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1409 nccb = SCSI_LOW_NCCB;
1410 scsi_low_init_ccbque(nccb);
1411 TAILQ_INIT(&slp->sl_start);
1413 /* call os depend attach */
1414 rv = scsi_low_attach_cam(slp);
1417 device_printf(slp->sl_dev,
1418 "scsi_low_attach: osdep attach failed\n");
1422 /* check hardware */
1423 DELAY(1000); /* wait for 1ms */
1425 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1427 device_printf(slp->sl_dev,
1428 "scsi_low_attach: initialization failed\n");
1429 SCSI_LOW_UNLOCK(slp);
1433 /* start watch dog */
1434 slp->sl_timeout_count = 0;
1435 callout_reset(&slp->sl_timeout_timer, hz / SCSI_LOW_TIMEOUT_HZ,
1436 scsi_low_timeout, slp);
1437 mtx_lock(&sl_tab_lock);
1438 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1439 mtx_unlock(&sl_tab_lock);
1442 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1444 #ifdef SCSI_LOW_START_UP_CHECK
1445 /* probing devices */
1446 scsi_low_start_up(slp);
1447 #endif /* SCSI_LOW_START_UP_CHECK */
1448 SCSI_LOW_UNLOCK(slp);
1454 scsi_low_detach(slp)
1455 struct scsi_low_softc *slp;
1460 if (scsi_low_is_busy(slp) != 0)
1462 SCSI_LOW_UNLOCK(slp);
1466 scsi_low_deactivate(slp);
1468 rv = scsi_low_detach_cam(slp);
1471 SCSI_LOW_UNLOCK(slp);
1475 scsi_low_free_ti(slp);
1476 SCSI_LOW_UNLOCK(slp);
1477 callout_drain(&slp->sl_timeout_timer);
1478 callout_drain(&slp->sl_engage_timer);
1479 mtx_lock(&sl_tab_lock);
1480 LIST_REMOVE(slp, sl_chain);
1481 mtx_unlock(&sl_tab_lock);
1485 /**************************************************************
1487 **************************************************************/
1489 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
1490 struct scsi_low_softc *slp;
1491 struct targ_info *ti;
1492 struct lun_info *li;
1500 scsi_low_ccb_message_assert(cb, msg);
1502 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1503 scsi_low_alloc_qtag(cb);
1505 cb->ccb_flags = flags | CCB_STARTQ;
1506 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1507 cb->ccb_error |= PENDINGIO;
1509 if ((flags & CCB_URGENT) != 0)
1511 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1515 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1520 if (slp->sl_Tnexus == NULL)
1521 scsi_low_start(slp);
1526 scsi_low_message_enqueue(slp, ti, li, flags)
1527 struct scsi_low_softc *slp;
1528 struct targ_info *ti;
1529 struct lun_info *li;
1535 tmsgflags = ti->ti_setup_msg;
1536 ti->ti_setup_msg = 0;
1538 flags |= CCB_NORETRY;
1539 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1544 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1548 /**************************************************************
1549 * Generic Start & Done
1550 **************************************************************/
1551 #define SLSC_MODE_SENSE_SHORT 0x1a
1552 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1553 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1554 sizeof(struct scsi_low_mode_sense_data), 0};
1555 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1556 sizeof(struct scsi_low_inq_data), 0};
1557 static u_int8_t unit_ready_cmd[6];
1558 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1559 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1560 static int scsi_low_resume(struct scsi_low_softc *);
1563 scsi_low_unit_ready_cmd(cb)
1567 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1568 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1569 cb->ccb_scp.scp_datalen = 0;
1570 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1575 scsi_low_sense_abort_start(slp, ti, li, cb)
1576 struct scsi_low_softc *slp;
1577 struct targ_info *ti;
1578 struct lun_info *li;
1582 cb->ccb_scp.scp_cmdlen = 6;
1583 bzero(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1584 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1585 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1586 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1587 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1588 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1589 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1591 scsi_low_ccb_message_clear(cb);
1592 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1594 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1598 bzero(&cb->ccb_sense, sizeof(cb->ccb_sense));
1599 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1600 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1601 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1604 return SCSI_LOW_START_NO_QTAG;
1608 scsi_low_setup_start(slp, ti, li, cb)
1609 struct scsi_low_softc *slp;
1610 struct targ_info *ti;
1611 struct lun_info *li;
1615 switch(li->li_state)
1617 case SCSI_LOW_LUN_SLEEP:
1618 scsi_low_unit_ready_cmd(cb);
1621 case SCSI_LOW_LUN_START:
1622 cb->ccb_scp.scp_cmd = ss_cmd;
1623 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1624 cb->ccb_scp.scp_datalen = 0;
1625 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1629 case SCSI_LOW_LUN_INQ:
1630 cb->ccb_scp.scp_cmd = inq_cmd;
1631 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1632 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1633 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1634 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1638 case SCSI_LOW_LUN_MODEQ:
1639 cb->ccb_scp.scp_cmd = sms_cmd;
1640 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1641 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1642 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1643 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1645 return SCSI_LOW_START_QTAG;
1648 panic("%s: no setup phase", device_get_nameunit(slp->sl_dev));
1651 return SCSI_LOW_START_NO_QTAG;
1655 scsi_low_resume(slp)
1656 struct scsi_low_softc *slp;
1659 if (slp->sl_flags & HW_RESUME)
1661 slp->sl_flags &= ~HW_POWDOWN;
1662 if (slp->sl_funcs->scsi_low_power != NULL)
1664 slp->sl_flags |= HW_RESUME;
1666 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1667 callout_reset(&slp->sl_engage_timer, hz / 1000,
1668 scsi_low_engage, slp);
1676 struct scsi_low_softc *slp;
1678 struct targ_info *ti;
1679 struct lun_info *li;
1683 /* check hardware exists or under initializations ? */
1684 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1687 /* check hardware power up ? */
1688 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1691 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1693 if (scsi_low_resume(slp) == EJUSTRETURN)
1699 #ifdef SCSI_LOW_DIAGNOSTIC
1700 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1702 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
1703 panic("%s: inconsistent", device_get_nameunit(slp->sl_dev));
1705 #endif /* SCSI_LOW_DIAGNOSTIC */
1707 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
1708 cb = TAILQ_NEXT(cb, ccb_chain))
1712 if (li->li_disc == 0)
1714 goto scsi_low_cmd_start;
1716 else if (li->li_nqio > 0)
1718 if (li->li_nqio < li->li_maxnqio ||
1719 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1720 goto scsi_low_cmd_start;
1726 cb->ccb_flags &= ~CCB_STARTQ;
1727 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1730 /* clear all error flag bits (for restart) */
1732 cb->ccb_datalen = -1;
1733 cb->ccb_scp.scp_status = ST_UNKNOWN;
1735 /* setup nexus pointer */
1736 slp->sl_Qnexus = cb;
1737 slp->sl_Lnexus = li;
1738 slp->sl_Tnexus = ti;
1740 /* initialize msgsys */
1741 scsi_low_init_msgsys(slp, ti);
1744 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1746 /* CA state or forced abort */
1747 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1749 else if (li->li_state >= SCSI_LOW_LUN_OK)
1751 cb->ccb_flags &= ~CCB_INTERNAL;
1752 rv = scsi_low_ccb_setup_cam(slp, cb);
1753 if (cb->ccb_msgoutflag != 0)
1755 scsi_low_ccb_message_exec(slp, cb);
1760 cb->ccb_flags |= CCB_INTERNAL;
1761 rv = scsi_low_setup_start(slp, ti, li, cb);
1765 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1767 if (rv == SCSI_LOW_START_QTAG &&
1768 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1773 scsi_low_activate_qtag(cb);
1774 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1775 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1776 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1777 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1778 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1780 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1781 scsi_low_assert_msg(slp, ti, qmsg, 0);
1785 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1786 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1787 cb->ccb_tc = cb->ccb_tcmax;
1789 /* setup saved scsi data pointer */
1790 cb->ccb_sscp = cb->ccb_scp;
1792 /* setup current scsi pointer */
1793 slp->sl_scp = cb->ccb_sscp;
1794 slp->sl_error = cb->ccb_error;
1796 /* assert always an identify msg */
1797 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1800 #ifdef SCSI_LOW_DIAGNOSTIC
1801 scsi_low_msg_log_init(&ti->ti_log_msgin);
1802 scsi_low_msg_log_init(&ti->ti_log_msgout);
1803 #endif /* SCSI_LOW_DIAGNOSTIC */
1805 /* selection start */
1807 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1808 if (rv == SCSI_LOW_START_OK)
1810 #ifdef SCSI_LOW_STATICS
1811 scsi_low_statics.nexus_win ++;
1812 #endif /* SCSI_LOW_STATICS */
1816 scsi_low_arbit_fail(slp, cb);
1817 #ifdef SCSI_LOW_STATICS
1818 scsi_low_statics.nexus_fail ++;
1819 #endif /* SCSI_LOW_STATICS */
1823 scsi_low_arbit_fail(slp, cb)
1824 struct scsi_low_softc *slp;
1827 struct targ_info *ti = cb->ti;
1829 scsi_low_deactivate_qtag(cb);
1830 scsi_low_ccb_message_retry(cb);
1831 cb->ccb_flags |= CCB_STARTQ;
1832 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1834 scsi_low_bus_release(slp, ti);
1837 if (slp->sl_disc == 0)
1839 #ifdef SCSI_LOW_DIAGNOSTIC
1840 device_printf(slp->sl_dev, "try selection again\n");
1841 #endif /* SCSI_LOW_DIAGNOSTIC */
1842 slp->sl_retry_sel = 1;
1847 scsi_low_bus_release(slp, ti)
1848 struct scsi_low_softc *slp;
1849 struct targ_info *ti;
1852 if (ti->ti_disc > 0)
1854 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1858 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1861 /* clear all nexus pointer */
1862 slp->sl_Qnexus = NULL;
1863 slp->sl_Lnexus = NULL;
1864 slp->sl_Tnexus = NULL;
1866 /* clear selection assert */
1867 slp->sl_selid = NULL;
1869 /* clear nexus data */
1870 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1872 /* clear phase change counter */
1873 slp->sl_ph_count = 0;
1877 scsi_low_setup_done(slp, cb)
1878 struct scsi_low_softc *slp;
1881 struct targ_info *ti;
1882 struct lun_info *li;
1887 if (cb->ccb_rcnt >= slp->sl_max_retry)
1889 cb->ccb_error |= ABORTIO;
1890 return SCSI_LOW_DONE_COMPLETE;
1893 /* XXX: special huck for selection timeout */
1894 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1895 (cb->ccb_error & SELTIMEOUTIO) != 0)
1897 cb->ccb_error |= ABORTIO;
1898 return SCSI_LOW_DONE_COMPLETE;
1901 switch(li->li_state)
1903 case SCSI_LOW_LUN_INQ:
1904 if (cb->ccb_error != 0)
1907 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
1911 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
1913 else if ((li->li_inq.sd_version & 7) >= 2 ||
1914 (li->li_inq.sd_len >= 4))
1916 if ((li->li_inq.sd_support & 0x2) == 0)
1917 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1918 if ((li->li_inq.sd_support & 0x8) == 0)
1919 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
1922 if ((li->li_inq.sd_support & 0x10) == 0)
1923 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
1924 if ((li->li_inq.sd_support & 0x20) == 0)
1925 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
1926 if ((li->li_inq.sd_support & 0x40) == 0)
1927 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
1932 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
1935 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
1937 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
1939 scsi_low_calcf_target(ti);
1940 scsi_low_calcf_lun(li);
1943 case SCSI_LOW_LUN_MODEQ:
1944 if (cb->ccb_error != 0)
1946 if (cb->ccb_error & SENSEIO)
1948 #ifdef SCSI_LOW_DEBUG
1949 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
1951 int error_code, sense_key, asc, ascq;
1953 scsi_extract_sense(&cb->ccb_sense,
1958 printf("SENSE: [%x][%x][%x][%x]\n",
1959 error_code, sense_key, asc,
1962 #endif /* SCSI_LOW_DEBUG */
1966 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1969 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
1971 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
1972 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
1974 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
1975 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
1976 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1978 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
1979 scsi_low_calcf_lun(li);
1987 if (li->li_state == SCSI_LOW_LUN_OK)
1989 scsi_low_calcf_target(ti);
1990 scsi_low_calcf_lun(li);
1991 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
1992 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
1994 scsi_low_calcf_show(li);
1999 return SCSI_LOW_DONE_RETRY;
2003 scsi_low_done(slp, cb)
2004 struct scsi_low_softc *slp;
2009 if (cb->ccb_error == 0)
2011 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2013 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2015 * SCSI-2 draft suggests
2016 * page 0x0a QErr bit determins if
2017 * the target aborts or continues
2018 * the queueing io's after CA state resolved.
2019 * However many targets seem not to support
2020 * the page 0x0a. Thus we should manually clear the
2021 * queuing io's after CA state.
2023 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2026 cb->ccb_flags |= CCB_CLEARQ;
2029 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2031 if ((cb->ccb_flags & CCB_SENSE) != 0)
2032 cb->ccb_error |= (SENSEIO | ABORTIO);
2033 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2035 else switch (cb->ccb_sscp.scp_status)
2041 if (cb->ccb_datalen == 0 ||
2042 cb->ccb_scp.scp_datalen == 0)
2045 if (cb->ccb_scp.scp_cmdlen > 0 &&
2046 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2047 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2050 cb->ccb_error |= PDMAERR;
2055 cb->ccb_error |= (BUSYERR | STATERR);
2059 cb->ccb_error |= (STATERR | ABORTIO);
2064 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2067 cb->ccb_flags |= CCB_SENSE;
2070 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2075 cb->ccb_error |= FATALIO;
2081 if (cb->ccb_flags & CCB_SENSE)
2083 cb->ccb_error |= (SENSEERR | ABORTIO);
2085 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2089 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2091 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2095 /* check a ccb msgout flag */
2096 if (cb->ccb_omsgoutflag != 0)
2098 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2099 SCSI_LOW_MSG_ABORT_QTAG | \
2100 SCSI_LOW_MSG_CLEAR_QTAG | \
2101 SCSI_LOW_MSG_TERMIO)
2103 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2105 cb->ccb_error |= ABORTIO;
2109 /* call OS depend done */
2110 if (cb->osdep != NULL)
2112 rv = scsi_low_done_cam(slp, cb);
2113 if (rv == EJUSTRETURN)
2116 else if (cb->ccb_error != 0)
2118 if (cb->ccb_rcnt >= slp->sl_max_retry)
2119 cb->ccb_error |= ABORTIO;
2121 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2122 (cb->ccb_error & ABORTIO) == 0)
2126 /* free our target */
2127 #ifdef SCSI_LOW_DEBUG
2128 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2130 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2131 scsi_low_print(slp, NULL);
2133 #endif /* SCSI_LOW_DEBUG */
2135 scsi_low_deactivate_qtag(cb);
2136 scsi_low_dealloc_qtag(cb);
2137 scsi_low_free_ccb(cb);
2139 return SCSI_LOW_DONE_COMPLETE;
2142 #ifdef SCSI_LOW_DEBUG
2143 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2145 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2146 scsi_low_print(slp, NULL);
2148 #endif /* SCSI_LOW_DEBUG */
2151 scsi_low_deactivate_qtag(cb);
2152 scsi_low_ccb_message_retry(cb);
2153 return SCSI_LOW_DONE_RETRY;
2156 /**************************************************************
2158 **************************************************************/
2160 scsi_low_reset_nexus_target(slp, ti, fdone)
2161 struct scsi_low_softc *slp;
2162 struct targ_info *ti;
2165 struct lun_info *li;
2167 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2168 li = LIST_NEXT(li, lun_chain))
2170 scsi_low_reset_nexus_lun(slp, li, fdone);
2171 li->li_state = SCSI_LOW_LUN_SLEEP;
2176 ti->ti_setup_msg = 0;
2177 ti->ti_setup_msg_done = 0;
2179 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2180 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2182 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2183 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2185 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2187 ((*slp->sl_funcs->scsi_low_targ_init)
2188 (slp, ti, SCSI_LOW_INFO_REVOKE));
2190 scsi_low_calcf_target(ti);
2192 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2193 li = LIST_NEXT(li, lun_chain))
2197 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2198 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2200 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2202 ((*slp->sl_funcs->scsi_low_lun_init)
2203 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2205 scsi_low_calcf_lun(li);
2210 scsi_low_reset_nexus(slp, fdone)
2211 struct scsi_low_softc *slp;
2214 struct targ_info *ti;
2215 struct slccb *cb, *topcb;
2217 if ((cb = slp->sl_Qnexus) != NULL)
2219 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2226 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2227 ti = TAILQ_NEXT(ti, ti_chain))
2229 scsi_low_reset_nexus_target(slp, ti, fdone);
2230 scsi_low_bus_release(slp, ti);
2231 scsi_low_init_msgsys(slp, ti);
2236 topcb->ccb_flags |= CCB_STARTQ;
2237 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2241 slp->sl_retry_sel = 0;
2242 slp->sl_flags &= ~HW_PDMASTART;
2247 static char tw_chars[] = "|/-\\";
2248 #define TWIDDLEWAIT 10000
2251 scsi_low_twiddle_wait(void)
2255 cnputc(tw_chars[tw_pos++]);
2256 tw_pos %= (sizeof(tw_chars) - 1);
2261 scsi_low_bus_reset(slp)
2262 struct scsi_low_softc *slp;
2266 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2268 device_printf(slp->sl_dev, "try to reset scsi bus ");
2269 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2270 scsi_low_twiddle_wait();
2276 scsi_low_restart(slp, flags, s)
2277 struct scsi_low_softc *slp;
2284 device_printf(slp->sl_dev, "scsi bus restart. reason: %s\n", s);
2286 if ((error = scsi_low_init(slp, flags)) != 0)
2289 scsi_low_start(slp);
2293 /**************************************************************
2294 * disconnect and reselect
2295 **************************************************************/
2296 #define MSGCMD_LUN(msg) (msg & 0x07)
2298 static struct slccb *
2299 scsi_low_establish_ccb(ti, li, tag)
2300 struct targ_info *ti;
2301 struct lun_info *li;
2304 struct scsi_low_softc *slp = ti->ti_sc;
2310 cb = TAILQ_FIRST(&li->li_discq);
2311 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2312 if (cb->ccb_tag == tag)
2317 * establish our ccb nexus
2320 #ifdef SCSI_LOW_DEBUG
2321 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2323 device_printf(slp->sl_dev, "nexus(0x%lx) abort check start\n",
2325 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2326 scsi_low_revoke_ccb(slp, cb, 1);
2330 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2332 if (cb->ccb_omsgoutflag == 0)
2333 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2335 #endif /* SCSI_LOW_DEBUG */
2337 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2338 cb->ccb_flags &= ~CCB_DISCQ;
2339 slp->sl_Qnexus = cb;
2341 slp->sl_scp = cb->ccb_sscp;
2342 slp->sl_error |= cb->ccb_error;
2348 /* inform "ccb nexus established" to the host driver */
2349 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2352 if (cb->ccb_msgoutflag != 0)
2354 scsi_low_ccb_message_exec(slp, cb);
2361 scsi_low_reselected(slp, targ)
2362 struct scsi_low_softc *slp;
2365 struct targ_info *ti;
2370 * Check select vs reselected collision.
2373 if ((cb = slp->sl_selid) != NULL)
2375 scsi_low_arbit_fail(slp, cb);
2376 #ifdef SCSI_LOW_STATICS
2377 scsi_low_statics.nexus_conflict ++;
2378 #endif /* SCSI_LOW_STATICS */
2382 * Check if no current active nexus.
2384 if (slp->sl_Tnexus != NULL)
2391 * Check a valid target id asserted ?
2393 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2395 s = "scsi id illegal";
2400 * Check the target scsi status.
2402 ti = slp->sl_ti[targ];
2403 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2405 s = "phase mismatch";
2413 scsi_low_init_msgsys(slp, ti);
2416 * Establish our target nexus
2418 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2419 slp->sl_Tnexus = ti;
2420 #ifdef SCSI_LOW_STATICS
2421 scsi_low_statics.nexus_reselected ++;
2422 #endif /* SCSI_LOW_STATICS */
2426 device_printf(slp->sl_dev, "reselect(%x:unknown) %s\n", targ, s);
2427 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2428 "reselect: scsi world confused");
2432 /**************************************************************
2433 * cmd out pointer setup
2434 **************************************************************/
2436 scsi_low_cmd(slp, ti)
2437 struct scsi_low_softc *slp;
2438 struct targ_info *ti;
2440 struct slccb *cb = slp->sl_Qnexus;
2442 slp->sl_ph_count ++;
2448 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2449 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2450 slp->sl_scp.scp_datalen = 0;
2451 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2452 slp->sl_error |= FATALIO;
2453 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2454 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2459 #ifdef SCSI_LOW_DEBUG
2460 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2462 scsi_low_test_cmdlnk(slp, cb);
2464 #endif /* SCSI_LOW_DEBUG */
2469 /**************************************************************
2470 * data out pointer setup
2471 **************************************************************/
2473 scsi_low_data(slp, ti, bp, direction)
2474 struct scsi_low_softc *slp;
2475 struct targ_info *ti;
2479 struct slccb *cb = slp->sl_Qnexus;
2481 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2487 slp->sl_error |= (FATALIO | PDMAERR);
2488 slp->sl_scp.scp_datalen = 0;
2489 slp->sl_scp.scp_direction = direction;
2490 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2491 if (ti->ti_ophase != ti->ti_phase)
2496 s = "DATA PHASE: ccb nexus not found";
2498 s = "DATA PHASE: xfer direction mismatch";
2499 SCSI_LOW_INFO(slp, ti, s);
2506 /**************************************************************
2508 **************************************************************/
2509 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2510 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2511 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2512 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2513 #define MSGIN_DATA_LAST 0x30
2515 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
2516 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
2517 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
2518 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
2520 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
2521 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
2522 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
2523 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
2524 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
2525 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
2526 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
2528 struct scsi_low_msgout_data {
2531 int (*md_msgfunc)(struct scsi_low_softc *);
2532 int (*md_errfunc)(struct scsi_low_softc *, u_int);
2533 #define MSG_RELEASE_ATN 0x0001
2537 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2538 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2539 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2540 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2541 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2542 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2543 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2544 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2545 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2546 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2547 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2548 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2549 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2550 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2551 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2552 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2553 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2556 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
2557 static int scsi_low_synch(struct scsi_low_softc *);
2558 static int scsi_low_wide(struct scsi_low_softc *);
2559 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
2560 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
2561 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
2562 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
2563 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
2564 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
2565 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
2566 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
2567 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
2568 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
2569 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
2571 struct scsi_low_msgin_data {
2573 int (*md_msgfunc)(struct scsi_low_softc *);
2576 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2577 /* 0 */ {1, scsi_low_msginfunc_cc},
2578 /* 1 */ {2, scsi_low_msginfunc_ext},
2579 /* 2 */ {1, scsi_low_msginfunc_sdp},
2580 /* 3 */ {1, scsi_low_msginfunc_rp},
2581 /* 4 */ {1, scsi_low_msginfunc_disc},
2582 /* 5 */ {1, scsi_low_msginfunc_rejop},
2583 /* 6 */ {1, scsi_low_msginfunc_rejop},
2584 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2585 /* 8 */ {1, scsi_low_msginfunc_noop},
2586 /* 9 */ {1, scsi_low_msginfunc_parity},
2587 /* a */ {1, scsi_low_msginfunc_lcc},
2588 /* b */ {1, scsi_low_msginfunc_lcc},
2589 /* c */ {1, scsi_low_msginfunc_rejop},
2590 /* d */ {2, scsi_low_msginfunc_rejop},
2591 /* e */ {1, scsi_low_msginfunc_rejop},
2592 /* f */ {1, scsi_low_msginfunc_rejop},
2593 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2594 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2595 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2596 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2597 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2598 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2599 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2600 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2601 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2602 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2603 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2604 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2605 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2606 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2607 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2608 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2609 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2610 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2611 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2612 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2613 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2614 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2615 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2616 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2617 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2618 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2619 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2620 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2621 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2622 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2623 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2624 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2625 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2628 /**************************************************************
2630 **************************************************************/
2632 scsi_low_msgfunc_synch(slp)
2633 struct scsi_low_softc *slp;
2635 struct targ_info *ti = slp->sl_Tnexus;
2636 int ptr = ti->ti_msgoutlen;
2638 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2639 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2640 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2641 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2642 return MSG_EXTEND_SYNCHLEN + 2;
2646 scsi_low_msgfunc_wide(slp)
2647 struct scsi_low_softc *slp;
2649 struct targ_info *ti = slp->sl_Tnexus;
2650 int ptr = ti->ti_msgoutlen;
2652 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2653 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2654 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2655 return MSG_EXTEND_WIDELEN + 2;
2659 scsi_low_msgfunc_identify(slp)
2660 struct scsi_low_softc *slp;
2662 struct targ_info *ti = slp->sl_Tnexus;
2663 struct lun_info *li = slp->sl_Lnexus;
2664 struct slccb *cb = slp->sl_Qnexus;
2665 int ptr = ti->ti_msgoutlen;
2671 slp->sl_error |= FATALIO;
2672 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2673 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2677 if (scsi_low_is_disconnect_ok(cb) != 0)
2678 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2682 if (ti->ti_phase == PH_MSGOUT)
2684 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2685 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2687 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2691 ti->ti_msgoutstr[ptr + 0] = msg;
2696 scsi_low_msgfunc_abort(slp)
2697 struct scsi_low_softc *slp;
2700 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2705 scsi_low_msgfunc_qabort(slp)
2706 struct scsi_low_softc *slp;
2709 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2714 scsi_low_msgfunc_reset(slp)
2715 struct scsi_low_softc *slp;
2718 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2723 scsi_low_msgfunc_qtag(slp)
2724 struct scsi_low_softc *slp;
2726 struct targ_info *ti = slp->sl_Tnexus;
2727 struct slccb *cb = slp->sl_Qnexus;
2728 int ptr = ti->ti_msgoutlen;
2730 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2732 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2737 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2738 if (ti->ti_phase == PH_MSGOUT)
2740 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2747 * The following functions are called when targets give unexpected
2748 * responces in msgin (after msgout).
2751 scsi_low_errfunc_identify(slp, msgflags)
2752 struct scsi_low_softc *slp;
2756 if (slp->sl_Lnexus != NULL)
2758 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2759 scsi_low_calcf_lun(slp->sl_Lnexus);
2765 scsi_low_errfunc_synch(slp, msgflags)
2766 struct scsi_low_softc *slp;
2769 struct targ_info *ti = slp->sl_Tnexus;
2771 MSGIN_PERIOD(ti) = 0;
2772 MSGIN_OFFSET(ti) = 0;
2773 scsi_low_synch(slp);
2778 scsi_low_errfunc_wide(slp, msgflags)
2779 struct scsi_low_softc *slp;
2782 struct targ_info *ti = slp->sl_Tnexus;
2784 MSGIN_WIDTHP(ti) = 0;
2790 scsi_low_errfunc_qtag(slp, msgflags)
2791 struct scsi_low_softc *slp;
2795 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2797 if (slp->sl_Qnexus != NULL)
2799 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2801 if (slp->sl_Lnexus != NULL)
2803 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2804 scsi_low_calcf_lun(slp->sl_Lnexus);
2806 device_printf(slp->sl_dev, "scsi_low: qtag msg rejected\n");
2813 scsi_low_msgout(slp, ti, fl)
2814 struct scsi_low_softc *slp;
2815 struct targ_info *ti;
2818 struct scsi_low_msgout_data *mdp;
2821 #ifdef SCSI_LOW_DIAGNOSTIC
2822 if (ti != slp->sl_Tnexus)
2824 scsi_low_print(slp, NULL);
2825 panic("scsi_low_msgout: Target nexus inconsistent");
2827 #endif /* SCSI_LOW_DIAGNOSTIC */
2829 slp->sl_ph_count ++;
2830 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2832 device_printf(slp->sl_dev, "too many phase changes\n");
2833 slp->sl_error |= FATALIO;
2834 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2838 * Scsi phase changes.
2839 * Previously msgs asserted are accepted by our target or
2840 * processed by scsi_low_msgin.
2841 * Thus clear all saved informations.
2843 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2845 ti->ti_omsgflags = 0;
2846 ti->ti_emsgflags = 0;
2848 else if (slp->sl_atten == 0)
2851 * We did not assert attention, however still our target required
2852 * msgs. Resend previous msgs.
2854 ti->ti_msgflags |= ti->ti_omsgflags;
2855 ti->ti_omsgflags = 0;
2856 #ifdef SCSI_LOW_DIAGNOSTIC
2857 device_printf(slp->sl_dev, "scsi_low_msgout: retry msgout\n");
2858 #endif /* SCSI_LOW_DIAGNOSTIC */
2862 * We have no msgs. send MSG_NOOP (OK?)
2864 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2865 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2870 ti->ti_msgoutlen = 0;
2871 slp->sl_clear_atten = 0;
2872 mdp = &scsi_low_msgout_data[0];
2873 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2875 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2877 ti->ti_omsgflags |= mdp->md_flags;
2878 ti->ti_msgflags &= ~mdp->md_flags;
2879 ti->ti_emsgflags = mdp->md_flags;
2881 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2882 if (mdp->md_msgfunc != NULL)
2883 len = (*mdp->md_msgfunc) (slp);
2887 #ifdef SCSI_LOW_DIAGNOSTIC
2888 scsi_low_msg_log_write(&ti->ti_log_msgout,
2889 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2890 #endif /* SCSI_LOW_DIAGNOSTIC */
2892 ti->ti_msgoutlen += len;
2893 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2895 slp->sl_clear_atten = 1;
2899 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2900 ti->ti_msgflags == 0)
2903 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
2908 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2909 slp->sl_clear_atten = 1;
2911 return ti->ti_msgoutlen;
2914 /**************************************************************
2916 **************************************************************/
2918 scsi_low_msginfunc_noop(slp)
2919 struct scsi_low_softc *slp;
2926 scsi_low_msginfunc_rejop(slp)
2927 struct scsi_low_softc *slp;
2929 struct targ_info *ti = slp->sl_Tnexus;
2930 u_int8_t msg = ti->ti_msgin[0];
2932 device_printf(slp->sl_dev, "MSGIN: msg 0x%x rejected\n", (u_int) msg);
2933 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
2938 scsi_low_msginfunc_cc(slp)
2939 struct scsi_low_softc *slp;
2941 struct lun_info *li;
2943 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
2945 /* validate status */
2946 if (slp->sl_Qnexus == NULL)
2949 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2950 li = slp->sl_Lnexus;
2951 switch (slp->sl_scp.scp_status)
2954 li->li_maxnqio = li->li_maxnexus;
2959 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
2960 scsi_low_reset_nexus_lun(slp, li, 0);
2968 if (li->li_maxnexus >= li->li_nqio)
2969 li->li_maxnexus = li->li_nqio - 1;
2970 li->li_maxnqio = li->li_maxnexus;
2975 slp->sl_error |= MSGERR;
2985 scsi_low_msginfunc_lcc(slp)
2986 struct scsi_low_softc *slp;
2988 struct targ_info *ti;
2989 struct lun_info *li;
2990 struct slccb *ncb, *cb;
2992 ti = slp->sl_Tnexus;
2993 li = slp->sl_Lnexus;
2994 if ((cb = slp->sl_Qnexus) == NULL)
2997 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2998 switch (slp->sl_scp.scp_status)
3002 li->li_maxnqio = li->li_maxnexus;
3006 slp->sl_error |= MSGERR;
3010 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3013 cb->ccb_error |= slp->sl_error;
3014 if (cb->ccb_error != 0)
3017 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3018 ncb = TAILQ_NEXT(ncb, ccb_chain))
3021 goto cmd_link_start;
3026 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3027 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3031 ncb->ccb_flags &= ~CCB_STARTQ;
3032 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3034 scsi_low_dealloc_qtag(ncb);
3035 ncb->ccb_tag = cb->ccb_tag;
3036 ncb->ccb_otag = cb->ccb_otag;
3037 cb->ccb_tag = SCSI_LOW_UNKTAG;
3038 cb->ccb_otag = SCSI_LOW_UNKTAG;
3039 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3040 panic("%s: linked ccb retried",
3041 device_get_nameunit(slp->sl_dev));
3043 slp->sl_Qnexus = ncb;
3044 slp->sl_ph_count = 0;
3047 ncb->ccb_datalen = -1;
3048 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3049 ncb->ccb_flags &= ~CCB_INTERNAL;
3051 scsi_low_init_msgsys(slp, ti);
3053 scsi_low_ccb_setup_cam(slp, ncb);
3055 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3056 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3057 ncb->ccb_tc = ncb->ccb_tcmax;
3059 /* setup saved scsi data pointer */
3060 ncb->ccb_sscp = ncb->ccb_scp;
3061 slp->sl_scp = ncb->ccb_sscp;
3062 slp->sl_error = ncb->ccb_error;
3064 #ifdef SCSI_LOW_DIAGNOSTIC
3065 scsi_low_msg_log_init(&ti->ti_log_msgin);
3066 scsi_low_msg_log_init(&ti->ti_log_msgout);
3067 #endif /* SCSI_LOW_DIAGNOSTIC */
3072 scsi_low_msginfunc_disc(slp)
3073 struct scsi_low_softc *slp;
3076 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3081 scsi_low_msginfunc_sdp(slp)
3082 struct scsi_low_softc *slp;
3084 struct slccb *cb = slp->sl_Qnexus;
3088 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3089 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3092 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3097 scsi_low_msginfunc_rp(slp)
3098 struct scsi_low_softc *slp;
3101 if (slp->sl_Qnexus != NULL)
3102 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3104 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3110 struct scsi_low_softc *slp;
3112 struct targ_info *ti = slp->sl_Tnexus;
3113 u_int period = 0, offset = 0, speed;
3117 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3118 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3119 MSGIN_OFFSET(ti) == 0)
3121 if ((offset = MSGIN_OFFSET(ti)) != 0)
3122 period = MSGIN_PERIOD(ti);
3123 s = offset ? "synchronous" : "async";
3128 * Target seems to be brain damaged.
3129 * Force async transfer.
3131 ti->ti_maxsynch.period = 0;
3132 ti->ti_maxsynch.offset = 0;
3133 device_printf(slp->sl_dev,
3134 "target brain damaged. async transfer\n");
3138 ti->ti_maxsynch.period = period;
3139 ti->ti_maxsynch.offset = offset;
3141 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3145 * Current period and offset are not acceptable
3147 * The adapter changes max synch and max offset.
3149 device_printf(slp->sl_dev,
3150 "synch neg failed. retry synch msg neg ...\n");
3154 ti->ti_osynch = ti->ti_maxsynch;
3157 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3161 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3163 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3164 struct slccb *cb = slp->sl_Qnexus;
3166 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3168 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3170 device_printf(slp->sl_dev,
3171 "(%d:*): <%s> offset %d period %dns ",
3172 ti->ti_id, s, offset, period * 4);
3176 speed = 1000 * 10 / (period * 4);
3177 printf("%d.%d M/s", speed / 10, speed % 10);
3186 struct scsi_low_softc *slp;
3188 struct targ_info *ti = slp->sl_Tnexus;
3191 ti->ti_width = MSGIN_WIDTHP(ti);
3192 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3196 * Current width is not acceptable for our adapter.
3197 * The adapter changes max width.
3199 device_printf(slp->sl_dev,
3200 "wide neg failed. retry wide msg neg ...\n");
3204 ti->ti_owidth = ti->ti_width;
3205 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3207 ti->ti_setup_msg_done |=
3208 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3212 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3214 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3215 struct slccb *cb = slp->sl_Qnexus;
3217 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3219 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3221 device_printf(slp->sl_dev, "(%d:*): transfer width %d bits\n",
3222 ti->ti_id, 1 << (3 + ti->ti_width));
3228 scsi_low_msginfunc_simple_qtag(slp)
3229 struct scsi_low_softc *slp;
3231 struct targ_info *ti = slp->sl_Tnexus;
3232 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3234 if (slp->sl_Qnexus != NULL)
3236 if (slp->sl_Qnexus->ccb_tag != etag)
3238 slp->sl_error |= FATALIO;
3239 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3240 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3243 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3245 #ifdef SCSI_LOW_DEBUG
3246 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3248 #endif /* SCSI_LOW_DEBUG */
3250 slp->sl_error |= FATALIO;
3251 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3252 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3258 scsi_low_msginfunc_i_wide_residue(slp)
3259 struct scsi_low_softc *slp;
3261 struct targ_info *ti = slp->sl_Tnexus;
3262 struct slccb *cb = slp->sl_Qnexus;
3263 int res = (int) ti->ti_msgin[1];
3265 if (cb == NULL || res <= 0 ||
3266 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3267 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3270 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3273 slp->sl_scp.scp_datalen += res;
3274 slp->sl_scp.scp_data -= res;
3275 scsi_low_data_finish(slp);
3280 scsi_low_msginfunc_ext(slp)
3281 struct scsi_low_softc *slp;
3283 struct slccb *cb = slp->sl_Qnexus;
3284 struct lun_info *li = slp->sl_Lnexus;
3285 struct targ_info *ti = slp->sl_Tnexus;
3289 if (ti->ti_msginptr == 2)
3291 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3295 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3297 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3301 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3302 count = (int) htonl((long) (*ptr));
3303 if(slp->sl_scp.scp_datalen - count < 0 ||
3304 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3307 slp->sl_scp.scp_datalen -= count;
3308 slp->sl_scp.scp_data += count;
3311 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3315 retry = scsi_low_synch(slp);
3316 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3317 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3319 #ifdef SCSI_LOW_DEBUG
3320 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3322 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3324 #endif /* SCSI_LOW_DEBUG */
3327 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3331 retry = scsi_low_wide(slp);
3332 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3333 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3341 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3346 scsi_low_msginfunc_parity(slp)
3347 struct scsi_low_softc *slp;
3349 struct targ_info *ti = slp->sl_Tnexus;
3351 /* only I -> T, invalid! */
3352 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3357 scsi_low_msginfunc_msg_reject(slp)
3358 struct scsi_low_softc *slp;
3360 struct targ_info *ti = slp->sl_Tnexus;
3361 struct scsi_low_msgout_data *mdp;
3364 if (ti->ti_emsgflags != 0)
3366 device_printf(slp->sl_dev, "msg flags [0x%x] rejected\n",
3368 msgflags = SCSI_LOW_MSG_REJECT;
3369 mdp = &scsi_low_msgout_data[0];
3370 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3372 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3374 ti->ti_emsgflags &= ~mdp->md_flags;
3375 if (mdp->md_errfunc != NULL)
3376 (*mdp->md_errfunc) (slp, msgflags);
3384 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3385 slp->sl_error |= MSGERR;
3391 scsi_low_msgin(slp, ti, c)
3392 struct scsi_low_softc *slp;
3393 struct targ_info *ti;
3396 struct scsi_low_msgin_data *sdp;
3397 struct lun_info *li;
3400 #ifdef SCSI_LOW_DIAGNOSTIC
3401 if (ti != slp->sl_Tnexus)
3403 scsi_low_print(slp, NULL);
3404 panic("scsi_low_msgin: Target nexus inconsistent");
3406 #endif /* SCSI_LOW_DIAGNOSTIC */
3409 * Phase changes, clear the pointer.
3411 if (ti->ti_ophase != ti->ti_phase)
3414 ti->ti_msgin_parity_error = 0;
3416 slp->sl_ph_count ++;
3417 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3419 device_printf(slp->sl_dev, "too many phase changes\n");
3420 slp->sl_error |= FATALIO;
3421 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3426 * Store a current messages byte into buffer and
3427 * wait for the completion of the current msg.
3429 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3430 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3432 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3433 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3437 * Check parity errors.
3439 if ((c & SCSI_LOW_DATA_PE) != 0)
3441 ti->ti_msgin_parity_error ++;
3442 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3446 if (ti->ti_msgin_parity_error != 0)
3450 * Calculate messages length.
3452 msg = ti->ti_msgin[0];
3453 if (msg < MSGIN_DATA_LAST)
3454 sdp = &scsi_low_msgin_data[msg];
3456 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3458 if (ti->ti_msginlen == 0)
3460 ti->ti_msginlen = sdp->md_len;
3466 if (ti->ti_msginptr < ti->ti_msginlen)
3472 if ((msg & MSG_IDENTIFY) == 0)
3474 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3479 li = slp->sl_Lnexus;
3482 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3485 slp->sl_Lnexus = li;
3486 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3490 if (MSGCMD_LUN(msg) != li->li_lun)
3494 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3496 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3498 #ifdef SCSI_LOW_DEBUG
3499 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3503 #endif /* SCSI_LOW_DEBUG */
3511 * Msg process completed, reset msgin pointer and assert ATN if desired.
3514 slp->sl_error |= FATALIO;
3515 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3516 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3519 if (ti->ti_msginptr < ti->ti_msginlen)
3522 #ifdef SCSI_LOW_DIAGNOSTIC
3523 scsi_low_msg_log_write(&ti->ti_log_msgin,
3524 &ti->ti_msgin[0], ti->ti_msginlen);
3525 #endif /* SCSI_LOW_DIAGNOSTIC */
3531 /**********************************************************
3533 **********************************************************/
3535 scsi_low_disconnected(slp, ti)
3536 struct scsi_low_softc *slp;
3537 struct targ_info *ti;
3539 struct slccb *cb = slp->sl_Qnexus;
3541 /* check phase completion */
3542 switch (slp->sl_msgphase)
3545 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3546 scsi_low_msginfunc_cc(slp);
3547 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3551 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3552 scsi_low_msginfunc_cc(slp);
3553 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3557 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3558 scsi_low_msginfunc_cc(slp);
3564 struct lun_info *li;
3567 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3568 cb->ccb_flags |= CCB_DISCQ;
3569 cb->ccb_error |= slp->sl_error;
3575 #ifdef SCSI_LOW_STATICS
3576 scsi_low_statics.nexus_disconnected ++;
3577 #endif /* SCSI_LOW_STATICS */
3579 #ifdef SCSI_LOW_DEBUG
3580 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3582 printf("## SCSI_LOW_DISCONNECTED ===============\n");
3583 scsi_low_print(slp, NULL);
3585 #endif /* SCSI_LOW_DEBUG */
3589 slp->sl_error |= FATALIO;
3590 if (ti->ti_phase == PH_SELSTART)
3591 slp->sl_error |= SELTIMEOUTIO;
3593 slp->sl_error |= UBFERR;
3602 #ifdef SCSI_LOW_DEBUG
3603 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3605 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3606 (cb->ccb_msgoutflag != 0 ||
3607 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3609 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3612 #endif /* SCSI_LOW_DEBUG */
3614 cb->ccb_error |= slp->sl_error;
3615 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3617 cb->ccb_flags |= CCB_STARTQ;
3618 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3623 scsi_low_bus_release(slp, ti);
3624 scsi_low_start(slp);
3628 /**********************************************************
3630 **********************************************************/
3632 scsi_low_alloc_qtag(cb)
3635 struct lun_info *li = cb->li;
3636 scsi_low_tag_t etag;
3638 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3641 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3642 etag = ffs(li->li_qtagbits);
3646 li->li_qtagbits &= ~(1 << (etag - 1));
3647 cb->ccb_otag = etag;
3650 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3651 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3652 if (li->li_qtagarray[li->li_qd] == 0)
3655 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3656 if (li->li_qtagarray[li->li_qd] == 0)
3662 li->li_qtagarray[li->li_qd] ++;
3663 cb->ccb_otag = (li->li_qd ++);
3665 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3669 scsi_low_dealloc_qtag(cb)
3672 struct lun_info *li = cb->li;
3673 scsi_low_tag_t etag;
3675 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3678 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3679 etag = cb->ccb_otag - 1;
3680 #ifdef SCSI_LOW_DIAGNOSTIC
3681 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3682 panic("scsi_low_dealloc_tag: illegal tag");
3683 #endif /* SCSI_LOW_DIAGNOSTIC */
3684 li->li_qtagbits |= (1 << etag);
3686 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3687 etag = cb->ccb_otag;
3688 #ifdef SCSI_LOW_DIAGNOSTIC
3689 if (etag >= SCSI_LOW_MAXNEXUS)
3690 panic("scsi_low_dealloc_tag: illegal tag");
3691 #endif /* SCSI_LOW_DIAGNOSTIC */
3692 li->li_qtagarray[etag] --;
3693 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3695 cb->ccb_otag = SCSI_LOW_UNKTAG;
3699 static struct slccb *
3700 scsi_low_revoke_ccb(slp, cb, fdone)
3701 struct scsi_low_softc *slp;
3705 struct targ_info *ti = cb->ti;
3706 struct lun_info *li = cb->li;
3708 #ifdef SCSI_LOW_DIAGNOSTIC
3709 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3710 (CCB_STARTQ | CCB_DISCQ))
3712 panic("%s: ccb in both queue",
3713 device_get_nameunit(slp->sl_dev));
3715 #endif /* SCSI_LOW_DIAGNOSTIC */
3717 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3719 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3722 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3724 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3730 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3731 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3734 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3735 (cb->ccb_flags & CCB_NORETRY) != 0))
3737 cb->ccb_error |= FATALIO;
3738 cb->ccb_flags &= ~CCB_AUTOSENSE;
3739 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3740 panic("%s: done ccb retried",
3741 device_get_nameunit(slp->sl_dev));
3746 cb->ccb_error |= PENDINGIO;
3747 scsi_low_deactivate_qtag(cb);
3748 scsi_low_ccb_message_retry(cb);
3749 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3755 scsi_low_reset_nexus_lun(slp, li, fdone)
3756 struct scsi_low_softc *slp;
3757 struct lun_info *li;
3760 struct slccb *cb, *ncb, *ecb;
3766 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3768 ncb = TAILQ_NEXT(cb, ccb_chain);
3769 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3773 * presumely keep ordering of io
3775 cb->ccb_flags |= CCB_STARTQ;
3778 TAILQ_INSERT_HEAD(&slp->sl_start,\
3783 TAILQ_INSERT_AFTER(&slp->sl_start,\
3784 ecb, cb, ccb_chain);
3791 /**************************************************************
3793 **************************************************************/
3795 scsi_low_calcf_lun(li)
3796 struct lun_info *li;
3798 struct targ_info *ti = li->li_ti;
3799 struct scsi_low_softc *slp = ti->ti_sc;
3800 u_int cfgflags, diskflags;
3802 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3803 cfgflags = li->li_cfgflags;
3807 diskflags = li->li_diskflags & li->li_quirks;
3810 li->li_flags &= ~SCSI_LOW_DISC;
3811 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3812 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3813 (cfgflags & SCSI_LOW_DISC) != 0)
3814 li->li_flags |= SCSI_LOW_DISC;
3817 li->li_flags |= SCSI_LOW_NOPARITY;
3818 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3819 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3820 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3821 li->li_flags &= ~SCSI_LOW_NOPARITY;
3824 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3825 (cfgflags & SCSI_LOW_QTAG) != 0 &&
3826 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3828 li->li_flags |= SCSI_LOW_QTAG;
3829 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3830 li->li_maxnqio = li->li_maxnexus;
3834 li->li_flags &= ~SCSI_LOW_QTAG;
3835 li->li_maxnexus = 0;
3836 li->li_maxnqio = li->li_maxnexus;
3840 li->li_flags &= ~SCSI_LOW_LINK;
3841 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3842 (diskflags & SCSI_LOW_DISK_LINK) != 0)
3843 li->li_flags |= SCSI_LOW_LINK;
3845 /* compatible flags */
3846 li->li_flags &= ~SCSI_LOW_SYNC;
3847 if (ti->ti_maxsynch.offset > 0)
3848 li->li_flags |= SCSI_LOW_SYNC;
3850 #ifdef SCSI_LOW_DEBUG
3851 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3853 scsi_low_calcf_show(li);
3855 #endif /* SCSI_LOW_DEBUG */
3859 scsi_low_calcf_target(ti)
3860 struct targ_info *ti;
3862 struct scsi_low_softc *slp = ti->ti_sc;
3863 u_int offset, period, diskflags;
3865 diskflags = ti->ti_diskflags & ti->ti_quirks;
3868 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3869 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3871 offset = ti->ti_maxsynch.offset;
3872 period = ti->ti_maxsynch.period;
3873 if (offset == 0 || period == 0)
3874 offset = period = 0;
3878 offset = period = 0;
3881 ti->ti_maxsynch.offset = offset;
3882 ti->ti_maxsynch.period = period;
3885 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3886 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3887 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3889 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3890 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3891 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3893 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3895 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3896 ti->ti_maxsynch.period != ti->ti_osynch.period)
3897 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3898 if (ti->ti_width != ti->ti_owidth)
3899 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3901 ti->ti_osynch = ti->ti_maxsynch;
3902 ti->ti_owidth = ti->ti_width;
3905 #ifdef SCSI_LOW_DEBUG
3906 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3908 device_printf(slp->sl_dev,
3909 "(%d:*): max period(%dns) offset(%d) width(%d)\n",
3911 ti->ti_maxsynch.period * 4,
3912 ti->ti_maxsynch.offset,
3915 #endif /* SCSI_LOW_DEBUG */
3919 scsi_low_calcf_show(li)
3920 struct lun_info *li;
3922 struct targ_info *ti = li->li_ti;
3923 struct scsi_low_softc *slp = ti->ti_sc;
3925 device_printf(slp->sl_dev,
3926 "(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
3927 ti->ti_id, li->li_lun,
3928 ti->ti_maxsynch.period * 4,
3929 ti->ti_maxsynch.offset,
3931 li->li_flags, SCSI_LOW_BITS);
3934 #ifdef SCSI_LOW_START_UP_CHECK
3935 /**************************************************************
3936 * scsi world start up
3937 **************************************************************/
3938 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
3941 scsi_low_start_up(slp)
3942 struct scsi_low_softc *slp;
3944 struct targ_info *ti;
3945 struct lun_info *li;
3949 device_printf(slp->sl_dev, "scsi_low: probing all devices ....\n");
3951 for (target = 0; target < slp->sl_ntargs; target ++)
3953 if (target == slp->sl_hostid)
3955 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3957 device_printf(slp->sl_dev,
3958 "scsi_low: target %d (host card)\n",
3964 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3966 device_printf(slp->sl_dev, "scsi_low: target %d lun ",
3970 ti = slp->sl_ti[target];
3971 for (lun = 0; lun < slp->sl_nluns; lun ++)
3973 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
3979 li = scsi_low_alloc_li(ti, lun, 1);
3981 scsi_low_enqueue(slp, ti, li, cb,
3982 CCB_AUTOSENSE | CCB_POLLED, 0);
3984 scsi_low_poll(slp, cb);
3986 if (li->li_state != SCSI_LOW_LUN_OK)
3989 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3995 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4004 scsi_low_poll(slp, cb)
4005 struct scsi_low_softc *slp;
4011 while (slp->sl_nio > 0)
4013 DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4015 (*slp->sl_funcs->scsi_low_poll) (slp);
4016 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4020 scsi_low_timeout_check(slp);
4025 #endif /* SCSI_LOW_START_UP_CHECK */
4027 /**********************************************************
4029 **********************************************************/
4030 #ifdef SCSI_LOW_DEBUG
4032 scsi_low_test_abort(slp, ti, li)
4033 struct scsi_low_softc *slp;
4034 struct targ_info *ti;
4035 struct lun_info *li;
4039 if (li->li_disc > 1)
4041 acb = TAILQ_FIRST(&li->li_discq);
4042 if (scsi_low_abort_ccb(slp, acb) == 0)
4044 device_printf(slp->sl_dev,
4045 "aborting ccb(0x%lx) start\n", (u_long) acb);
4051 scsi_low_test_atten(slp, ti, msg)
4052 struct scsi_low_softc *slp;
4053 struct targ_info *ti;
4057 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4058 scsi_low_assert_msg(slp, ti, msg, 0);
4060 device_printf(slp->sl_dev, "atten check OK\n");
4064 scsi_low_test_cmdlnk(slp, cb)
4065 struct scsi_low_softc *slp;
4068 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4070 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4073 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4074 slp->sl_scp.scp_cmdlen);
4075 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4076 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4078 #endif /* SCSI_LOW_DEBUG */
4081 scsi_low_info(slp, ti, s)
4082 struct scsi_low_softc *slp;
4083 struct targ_info *ti;
4088 slp = LIST_FIRST(&sl_tab);
4092 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4095 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
4097 scsi_low_print(slp, ti);
4102 scsi_low_print(slp, ti);
4106 static u_char *phase[] =
4108 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4109 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4113 scsi_low_print(slp, ti)
4114 struct scsi_low_softc *slp;
4115 struct targ_info *ti;
4117 struct lun_info *li;
4121 if (ti == NULL || ti == slp->sl_Tnexus)
4123 ti = slp->sl_Tnexus;
4124 li = slp->sl_Lnexus;
4125 cb = slp->sl_Qnexus;
4129 li = LIST_FIRST(&ti->ti_litab);
4130 cb = TAILQ_FIRST(&li->li_discq);
4134 device_printf(slp->sl_dev,
4135 "=== NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4136 (u_long) ti, (u_long) li, (u_long) cb, slp->sl_nio);
4141 u_int flags = 0, maxnqio = 0, nqio = 0;
4142 int lun = CAM_LUN_WILDCARD;
4147 flags = li->li_flags;
4148 maxnqio = li->li_maxnqio;
4152 device_printf(slp->sl_dev,
4153 "(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4154 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4155 phase[(int) ti->ti_phase], ti->ti_disc,
4160 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4161 (u_int) cb->ccb_scp.scp_cmd[0],
4162 cb->ccb_scp.scp_cmdlen,
4164 cb->ccb_scp.scp_datalen,
4165 (u_int) cb->ccb_sscp.scp_status,
4166 cb->ccb_error, SCSI_LOW_ERRORBITS);
4169 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4170 (u_int) (ti->ti_msginptr),
4171 (u_int) (ti->ti_msgin[0]),
4172 (u_int) (ti->ti_msgin[1]),
4173 (u_int) (ti->ti_msgin[2]),
4174 (u_int) (ti->ti_msgin[3]),
4175 (u_int) (ti->ti_msgin[4]),
4178 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4179 (u_int) ti->ti_msgflags,
4180 (u_int) (ti->ti_msgoutstr[0]),
4181 (u_int) (ti->ti_msgoutstr[1]),
4182 (u_int) (ti->ti_msgoutstr[2]),
4183 (u_int) (ti->ti_msgoutstr[3]),
4184 (u_int) (ti->ti_msgoutstr[4]),
4186 flags, SCSI_LOW_BITS);
4188 #ifdef SCSI_LOW_DIAGNOSTIC
4189 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4190 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4191 #endif /* SCSI_LOW_DIAGNOSTIC */
4195 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4196 (u_long) sp->scp_data,
4198 (u_int) sp->scp_status,
4199 slp->sl_error, SCSI_LOW_ERRORBITS);