2 * $FreeBSD: src/sys/cam/scsi/scsi_low.c,v 1.1.2.5 2003/08/09 06:18:30 non Exp $
3 * $DragonFly: src/sys/bus/cam/scsi/scsi_low.c,v 1.28 2008/05/18 20:30:20 pavalos Exp $
4 * $NetBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $
7 #define SCSI_LOW_STATICS
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
12 /* #define SCSI_LOW_INFO_DETAIL */
13 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
14 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
16 #define SCSI_LOW_FLAGS_QUIRKS_OK
19 * [NetBSD for NEC PC-98 series]
20 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
21 * NetBSD/pc98 porting staff. All rights reserved.
22 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
23 * Naofumi HONDA. All rights reserved.
25 * [Ported for FreeBSD CAM]
26 * Copyright (c) 2000, 2001
27 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. The name of the author may not be used to endorse or promote products
39 * derived from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
45 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
50 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
54 /* <On the nexus establishment>
55 * When our host is reselected,
56 * nexus establish processes are little complicated.
57 * Normal steps are followings:
58 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
59 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
60 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
64 #include <sys/param.h>
67 #include <sys/devicestat.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/malloc.h>
71 #include <sys/queue.h>
72 #include <sys/systm.h>
73 #include <sys/thread2.h>
75 #include <bus/cam/cam.h>
76 #include <bus/cam/cam_ccb.h>
77 #include <bus/cam/cam_sim.h>
78 #include <bus/cam/cam_debug.h>
79 #include <bus/cam/cam_periph.h>
80 #include <bus/cam/cam_xpt_periph.h>
82 #include <bus/cam/scsi/scsi_all.h>
83 #include <bus/cam/scsi/scsi_low.h>
85 /**************************************************************
87 **************************************************************/
88 #define SCSI_LOW_POLL_HZ 1000
90 /* functions return values */
91 #define SCSI_LOW_START_NO_QTAG 0
92 #define SCSI_LOW_START_QTAG 1
94 #define SCSI_LOW_DONE_COMPLETE 0
95 #define SCSI_LOW_DONE_RETRY 1
97 /* internal disk flags */
98 #define SCSI_LOW_DISK_DISC 0x00000001
99 #define SCSI_LOW_DISK_QTAG 0x00000002
100 #define SCSI_LOW_DISK_LINK 0x00000004
101 #define SCSI_LOW_DISK_PARITY 0x00000008
102 #define SCSI_LOW_DISK_SYNC 0x00010000
103 #define SCSI_LOW_DISK_WIDE_16 0x00020000
104 #define SCSI_LOW_DISK_WIDE_32 0x00040000
105 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
106 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
107 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
109 MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
111 /**************************************************************
113 **************************************************************/
114 /* static */ void scsi_low_info (struct scsi_low_softc *, struct targ_info *, u_char *);
115 static void scsi_low_engage (void *);
116 static struct slccb *scsi_low_establish_ccb (struct targ_info *, struct lun_info *, scsi_low_tag_t);
117 static int scsi_low_done (struct scsi_low_softc *, struct slccb *);
118 static int scsi_low_setup_done (struct scsi_low_softc *, struct slccb *);
119 static void scsi_low_bus_release (struct scsi_low_softc *, struct targ_info *);
120 static void scsi_low_twiddle_wait (void);
121 static struct lun_info *scsi_low_alloc_li (struct targ_info *, int, int);
122 static struct targ_info *scsi_low_alloc_ti (struct scsi_low_softc *, int);
123 static void scsi_low_calcf_lun (struct lun_info *);
124 static void scsi_low_calcf_target (struct targ_info *);
125 static void scsi_low_calcf_show (struct lun_info *);
126 static void scsi_low_reset_nexus (struct scsi_low_softc *, int);
127 static void scsi_low_reset_nexus_target (struct scsi_low_softc *, struct targ_info *, int);
128 static void scsi_low_reset_nexus_lun (struct scsi_low_softc *, struct lun_info *, int);
129 static int scsi_low_init (struct scsi_low_softc *, u_int);
130 static void scsi_low_start (struct scsi_low_softc *);
131 static void scsi_low_free_ti (struct scsi_low_softc *);
133 static int scsi_low_alloc_qtag (struct slccb *);
134 static int scsi_low_dealloc_qtag (struct slccb *);
135 static int scsi_low_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
136 static int scsi_low_message_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
137 static void scsi_low_unit_ready_cmd (struct slccb *);
138 static void scsi_low_timeout (void *);
139 static int scsi_low_timeout_check (struct scsi_low_softc *);
140 #ifdef SCSI_LOW_START_UP_CHECK
141 static int scsi_low_start_up (struct scsi_low_softc *);
142 #endif /* SCSI_LOW_START_UP_CHECK */
143 static int scsi_low_abort_ccb (struct scsi_low_softc *, struct slccb *);
144 static struct slccb *scsi_low_revoke_ccb (struct scsi_low_softc *, struct slccb *, int);
146 int scsi_low_version_major = 2;
147 int scsi_low_version_minor = 17;
149 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
151 /**************************************************************
152 * Debug, Run test and Statics
153 **************************************************************/
154 #ifdef SCSI_LOW_INFO_DETAIL
155 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
156 #else /* !SCSI_LOW_INFO_DETAIL */
157 #define SCSI_LOW_INFO(slp, ti, s) kprintf("%s: %s\n", (slp)->sl_xname, (s))
158 #endif /* !SCSI_LOW_INFO_DETAIL */
160 #ifdef SCSI_LOW_STATICS
161 static struct scsi_low_statics {
164 int nexus_disconnected;
165 int nexus_reselected;
168 #endif /* SCSI_LOW_STATICS */
170 #ifdef SCSI_LOW_DEBUG
171 #define SCSI_LOW_DEBUG_DONE 0x00001
172 #define SCSI_LOW_DEBUG_DISC 0x00002
173 #define SCSI_LOW_DEBUG_SENSE 0x00004
174 #define SCSI_LOW_DEBUG_CALCF 0x00008
175 #define SCSI_LOW_DEBUG_ACTION 0x10000
176 int scsi_low_debug = 0;
178 #define SCSI_LOW_MAX_ATTEN_CHECK 32
179 #define SCSI_LOW_ATTEN_CHECK 0x0001
180 #define SCSI_LOW_CMDLNK_CHECK 0x0002
181 #define SCSI_LOW_ABORT_CHECK 0x0004
182 #define SCSI_LOW_NEXUS_CHECK 0x0008
183 int scsi_low_test = 0;
184 int scsi_low_test_id = 0;
186 static void scsi_low_test_abort (struct scsi_low_softc *, struct targ_info *, struct lun_info *);
187 static void scsi_low_test_cmdlnk (struct scsi_low_softc *, struct slccb *);
188 static void scsi_low_test_atten (struct scsi_low_softc *, struct targ_info *, u_int);
189 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
190 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
191 #define SCSI_LOW_DEBUG_GO(fl, id) \
192 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
193 #endif /* SCSI_LOW_DEBUG */
195 /**************************************************************
197 **************************************************************/
198 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
199 GENERIC_CCB(scsi_low, slccb, ccb_chain)
201 /**************************************************************
203 **************************************************************/
204 #define SCSI_LOW_INLINE static __inline
205 SCSI_LOW_INLINE void scsi_low_activate_qtag (struct slccb *);
206 SCSI_LOW_INLINE void scsi_low_deactivate_qtag (struct slccb *);
207 SCSI_LOW_INLINE void scsi_low_ccb_message_assert (struct slccb *, u_int);
208 SCSI_LOW_INLINE void scsi_low_ccb_message_exec (struct scsi_low_softc *, struct slccb *);
209 SCSI_LOW_INLINE void scsi_low_ccb_message_retry (struct slccb *);
210 SCSI_LOW_INLINE void scsi_low_ccb_message_clear (struct slccb *);
211 SCSI_LOW_INLINE void scsi_low_init_msgsys (struct scsi_low_softc *, struct targ_info *);
214 scsi_low_activate_qtag(struct slccb *cb)
216 struct lun_info *li = cb->li;
218 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
222 cb->ccb_tag = cb->ccb_otag;
226 scsi_low_deactivate_qtag(struct slccb *cb)
228 struct lun_info *li = cb->li;
230 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
234 cb->ccb_tag = SCSI_LOW_UNKTAG;
238 scsi_low_ccb_message_exec(struct scsi_low_softc *slp, struct slccb *cb)
240 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
241 cb->ccb_msgoutflag = 0;
245 scsi_low_ccb_message_assert(struct slccb *cb, u_int msg)
247 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
251 scsi_low_ccb_message_retry(struct slccb *cb)
253 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
257 scsi_low_ccb_message_clear(struct slccb *cb)
259 cb->ccb_msgoutflag = 0;
263 scsi_low_init_msgsys(struct scsi_low_softc *slp, struct targ_info *ti)
266 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
267 SCSI_LOW_DEASSERT_ATN(slp);
268 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
271 /*=============================================================
272 * START OF OS switch (All OS depend fucntions should be here)
273 =============================================================*/
274 /* common os depend utitlities */
275 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
276 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
277 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
279 static u_int8_t scsi_low_cmd_flags[256] = {
280 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
281 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
282 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
283 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
284 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
287 struct scsi_low_error_code {
292 static struct slccb *scsi_low_find_ccb (struct scsi_low_softc *, u_int, u_int, void *);
293 static int scsi_low_translate_error_code (struct slccb *, struct scsi_low_error_code *);
295 static struct slccb *
296 scsi_low_find_ccb(struct scsi_low_softc *slp, u_int target, u_int lun, void *osdep)
298 struct targ_info *ti;
302 ti = slp->sl_ti[target];
303 li = scsi_low_alloc_li(ti, lun, 0);
307 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
310 TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
312 if (cb->osdep == osdep)
316 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
318 if (cb->osdep == osdep)
325 scsi_low_translate_error_code(struct slccb *cb, struct scsi_low_error_code *tp)
327 if (cb->ccb_error == 0)
328 return tp->error_code;
330 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
332 return tp->error_code;
335 /**************************************************************
336 * SCSI INTERFACE (CAM)
337 **************************************************************/
338 #define SCSI_LOW_MALLOC(size) kmalloc((size), M_SCSILOW, M_INTWAIT)
339 #define SCSI_LOW_FREE(pt) kfree((pt), M_SCSILOW)
340 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
342 static void scsi_low_poll_cam (struct cam_sim *);
343 static void scsi_low_cam_rescan_callback (struct cam_periph *, union ccb *);
344 static void scsi_low_rescan_bus_cam (struct scsi_low_softc *);
345 void scsi_low_scsi_action_cam (struct cam_sim *, union ccb *);
347 static int scsi_low_attach_cam (struct scsi_low_softc *);
348 static int scsi_low_world_start_cam (struct scsi_low_softc *);
349 static int scsi_low_dettach_cam (struct scsi_low_softc *);
350 static int scsi_low_ccb_setup_cam (struct scsi_low_softc *, struct slccb *);
351 static int scsi_low_done_cam (struct scsi_low_softc *, struct slccb *);
352 static void scsi_low_timeout_cam (struct scsi_low_softc *, int, int);
354 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
356 scsi_low_world_start_cam,
357 scsi_low_dettach_cam,
358 scsi_low_ccb_setup_cam,
363 struct scsi_low_error_code scsi_low_error_code_cam[] = {
365 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
366 {SENSEERR, CAM_AUTOSENSE_FAIL},
367 {UACAERR, CAM_SCSI_STATUS_ERROR},
368 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
369 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
370 {TIMEOUTIO, CAM_CMD_TIMEOUT},
371 {PDMAERR, CAM_DATA_RUN_ERR},
372 {PARITYERR, CAM_UNCOR_PARITY},
373 {UBFERR, CAM_UNEXP_BUSFREE},
374 {ABORTIO, CAM_REQ_ABORTED},
375 {-1, CAM_UNREC_HBA_ERROR}
378 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
381 * Please check a polling hz, currently we assume scsi_low_poll() is
384 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
387 scsi_low_poll_cam(struct cam_sim *sim)
389 struct scsi_low_softc *slp = SIM2SLP(sim);
391 (*slp->sl_funcs->scsi_low_poll) (slp);
393 if (slp->sl_si.si_poll_count ++ >=
394 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
396 slp->sl_si.si_poll_count = 0;
397 scsi_low_timeout_check(slp);
402 scsi_low_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
404 xpt_free_path(ccb->ccb_h.path);
409 scsi_low_rescan_bus_cam(struct scsi_low_softc *slp)
411 struct cam_path *path;
412 union ccb *ccb = xpt_alloc_ccb();
415 status = xpt_create_path(&path, xpt_periph,
416 cam_sim_path(slp->sl_si.sim), -1, 0);
417 if (status != CAM_REQ_CMP)
420 xpt_setup_ccb(&ccb->ccb_h, path, 5);
421 ccb->ccb_h.func_code = XPT_SCAN_BUS;
422 ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
423 ccb->crcn.flags = CAM_FLAG_NONE;
428 scsi_low_scsi_action_cam(struct cam_sim *sim, union ccb *ccb)
430 struct scsi_low_softc *slp = SIM2SLP(sim);
431 struct targ_info *ti;
434 u_int lun, flags, msg, target;
437 target = (u_int) (ccb->ccb_h.target_id);
438 lun = (u_int) ccb->ccb_h.target_lun;
440 #ifdef SCSI_LOW_DEBUG
441 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
443 kprintf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
444 slp->sl_xname, ccb->ccb_h.func_code, target, lun);
446 #endif /* SCSI_LOW_DEBUG */
448 switch (ccb->ccb_h.func_code) {
449 case XPT_SCSI_IO: /* Execute the requested I/O operation */
450 #ifdef SCSI_LOW_DIAGNOSTIC
451 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
453 kprintf("%s: invalid target/lun\n", slp->sl_xname);
454 ccb->ccb_h.status = CAM_REQ_INVALID;
458 #endif /* SCSI_LOW_DIAGNOSTIC */
460 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
461 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
466 ti = slp->sl_ti[target];
469 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
470 flags = CCB_AUTOSENSE | CCB_SCSIIO;
475 li = scsi_low_alloc_li(ti, lun, 1);
477 if (ti->ti_setup_msg != 0)
479 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
482 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
484 #ifdef SCSI_LOW_DEBUG
485 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
487 scsi_low_test_abort(slp, ti, li);
489 #endif /* SCSI_LOW_DEBUG */
493 case XPT_EN_LUN: /* Enable LUN as a target */
494 case XPT_TARGET_IO: /* Execute target I/O request */
495 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
496 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
498 ccb->ccb_h.status = CAM_REQ_INVALID;
502 case XPT_ABORT: /* Abort the specified CCB */
503 #ifdef SCSI_LOW_DIAGNOSTIC
504 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
506 kprintf("%s: invalid target/lun\n", slp->sl_xname);
507 ccb->ccb_h.status = CAM_REQ_INVALID;
511 #endif /* SCSI_LOW_DIAGNOSTIC */
514 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
515 rv = scsi_low_abort_ccb(slp, cb);
519 ccb->ccb_h.status = CAM_REQ_CMP;
521 ccb->ccb_h.status = CAM_REQ_INVALID;
525 case XPT_SET_TRAN_SETTINGS: {
526 struct ccb_trans_settings_scsi *scsi;
527 struct ccb_trans_settings_spi *spi;
528 struct ccb_trans_settings *cts;
531 #ifdef SCSI_LOW_DIAGNOSTIC
532 if (target == CAM_TARGET_WILDCARD)
534 kprintf("%s: invalid target\n", slp->sl_xname);
535 ccb->ccb_h.status = CAM_REQ_INVALID;
539 #endif /* SCSI_LOW_DIAGNOSTIC */
541 ti = slp->sl_ti[target];
542 if (lun == CAM_LUN_WILDCARD)
546 scsi = &cts->proto_specific.scsi;
547 spi = &cts->xport_specific.spi;
548 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
549 CTS_SPI_VALID_SYNC_RATE |
550 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
552 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
553 val = spi->bus_width;
554 if (val < ti->ti_width)
557 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
558 val = spi->sync_period;
559 if (val == 0 || val > ti->ti_maxsynch.period)
560 ti->ti_maxsynch.period = val;
562 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
563 val = spi->sync_offset;
564 if (val < ti->ti_maxsynch.offset)
565 ti->ti_maxsynch.offset = val;
567 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
568 scsi_low_calcf_target(ti);
571 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
572 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
574 li = scsi_low_alloc_li(ti, lun, 1);
575 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
576 li->li_quirks |= SCSI_LOW_DISK_DISC;
578 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
581 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
582 li->li_quirks |= SCSI_LOW_DISK_QTAG;
584 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
586 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
587 scsi_low_calcf_target(ti);
588 scsi_low_calcf_lun(li);
589 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
590 scsi_low_calcf_show(li);
594 ccb->ccb_h.status = CAM_REQ_CMP;
599 case XPT_GET_TRAN_SETTINGS: {
600 struct ccb_trans_settings *cts;
604 #ifdef SCSI_LOW_DIAGNOSTIC
605 if (target == CAM_TARGET_WILDCARD)
607 kprintf("%s: invalid target\n", slp->sl_xname);
608 ccb->ccb_h.status = CAM_REQ_INVALID;
612 #endif /* SCSI_LOW_DIAGNOSTIC */
613 ti = slp->sl_ti[target];
614 if (lun == CAM_LUN_WILDCARD)
618 li = scsi_low_alloc_li(ti, lun, 1);
619 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
620 struct ccb_trans_settings_scsi *scsi =
621 &cts->proto_specific.scsi;
622 struct ccb_trans_settings_spi *spi =
623 &cts->xport_specific.spi;
624 #ifdef SCSI_LOW_DIAGNOSTIC
625 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
627 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
628 kprintf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
632 #endif /* SCSI_LOW_DIAGNOSTIC */
633 cts->protocol = PROTO_SCSI;
634 cts->protocol_version = SCSI_REV_2;
635 cts->transport = XPORT_SPI;
636 cts->transport_version = 2;
638 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
639 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
641 diskflags = li->li_diskflags & li->li_cfgflags;
642 if (diskflags & SCSI_LOW_DISK_DISC)
643 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
644 if (diskflags & SCSI_LOW_DISK_QTAG)
645 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
647 spi->sync_period = ti->ti_maxsynch.period;
648 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
649 spi->sync_offset = ti->ti_maxsynch.offset;
650 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
652 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
653 spi->bus_width = ti->ti_width;
655 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
656 scsi->valid = CTS_SCSI_VALID_TQ;
657 spi->valid |= CTS_SPI_VALID_DISC;
661 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
668 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
669 cam_calc_geometry(&ccb->ccg, /*extended*/1);
674 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
676 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
678 ccb->ccb_h.status = CAM_REQ_CMP;
682 case XPT_TERM_IO: /* Terminate the I/O process */
683 ccb->ccb_h.status = CAM_REQ_INVALID;
687 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
688 #ifdef SCSI_LOW_DIAGNOSTIC
689 if (target == CAM_TARGET_WILDCARD)
691 kprintf("%s: invalid target\n", slp->sl_xname);
692 ccb->ccb_h.status = CAM_REQ_INVALID;
696 #endif /* SCSI_LOW_DIAGNOSTIC */
698 msg = SCSI_LOW_MSG_RESET;
699 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
701 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
706 ti = slp->sl_ti[target];
707 if (lun == CAM_LUN_WILDCARD)
711 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
712 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
714 flags = CCB_NORETRY | CCB_URGENT;
717 li = scsi_low_alloc_li(ti, lun, 1);
718 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
722 case XPT_PATH_INQ: { /* Path routing inquiry */
723 struct ccb_pathinq *cpi = &ccb->cpi;
725 cpi->version_num = scsi_low_version_major;
726 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
727 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
728 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
729 cpi->hba_inquiry |= PI_WIDE_16;
730 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
731 cpi->hba_inquiry |= PI_WIDE_32;
732 if (ti->ti_maxsynch.offset > 0)
733 cpi->hba_inquiry |= PI_SDTR_ABLE;
734 cpi->target_sprt = 0;
736 cpi->hba_eng_cnt = 0;
737 cpi->max_target = slp->sl_ntargs - 1;
738 cpi->max_lun = slp->sl_nluns - 1;
739 cpi->initiator_id = slp->sl_hostid;
740 cpi->bus_id = cam_sim_bus(sim);
741 cpi->base_transfer_speed = 3300;
742 cpi->transport = XPORT_SPI;
743 cpi->transport_version = 2;
744 cpi->protocol = PROTO_SCSI;
745 cpi->protocol_version = SCSI_REV_2;
746 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
747 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
748 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
749 cpi->unit_number = cam_sim_unit(sim);
750 cpi->ccb_h.status = CAM_REQ_CMP;
756 kprintf("scsi_low: non support func_code = %d ",
757 ccb->ccb_h.func_code);
758 ccb->ccb_h.status = CAM_REQ_INVALID;
765 scsi_low_attach_cam(struct scsi_low_softc *slp)
767 struct cam_devq *devq;
770 ksprintf(slp->sl_xname, "%s%d",
771 DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
773 devq = cam_simq_alloc(SCSI_LOW_NCCB);
778 * ask the adapter what subunits are present
780 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
781 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
783 DEVPORT_DEVNAME(slp->sl_dev), slp,
784 DEVPORT_DEVUNIT(slp->sl_dev), &sim_mplock,
785 slp->sl_openings, tagged_openings, devq);
786 cam_simq_release(devq);
787 if (slp->sl_si.sim == NULL) {
791 if (xpt_bus_register(slp->sl_si.sim, 0) != CAM_SUCCESS) {
792 cam_sim_free(slp->sl_si.sim);
793 slp->sl_si.sim = NULL;
797 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
798 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
799 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
800 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
801 cam_sim_free(slp->sl_si.sim);
802 slp->sl_si.sim = NULL;
806 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
811 scsi_low_world_start_cam(struct scsi_low_softc *slp)
815 scsi_low_rescan_bus_cam(slp);
817 scsi_low_rescan_bus_cam(slp);
822 scsi_low_dettach_cam(struct scsi_low_softc *slp)
824 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
825 xpt_free_path(slp->sl_si.path);
826 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
827 cam_sim_free(slp->sl_si.sim);
828 slp->sl_si.sim = NULL;
833 scsi_low_ccb_setup_cam(struct scsi_low_softc *slp, struct slccb *cb)
835 union ccb *ccb = (union ccb *) cb->osdep;
837 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
839 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
840 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
841 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
842 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
843 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
844 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
845 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
846 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
847 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
851 scsi_low_unit_ready_cmd(cb);
853 return SCSI_LOW_START_QTAG;
857 scsi_low_done_cam(struct scsi_low_softc *slp, struct slccb *cb)
861 ccb = (union ccb *) cb->osdep;
862 if (cb->ccb_error == 0)
864 ccb->ccb_h.status = CAM_REQ_CMP;
869 if (cb->ccb_rcnt >= slp->sl_max_retry)
870 cb->ccb_error |= ABORTIO;
872 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
873 (cb->ccb_error & ABORTIO) == 0)
876 if ((cb->ccb_error & SENSEIO) != 0)
878 memcpy(&ccb->csio.sense_data,
880 sizeof(ccb->csio.sense_data));
883 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
884 &scsi_low_error_code_cam[0]);
886 #ifdef SCSI_LOW_DIAGNOSTIC
887 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
888 cb->ccb_scp.scp_cmdlen > 0 &&
889 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
890 SCSI_LOW_CMD_ABORT_WARNING) != 0)
892 kprintf("%s: WARNING: scsi_low IO abort\n",
894 scsi_low_print(slp, NULL);
896 #endif /* SCSI_LOW_DIAGNOSTIC */
899 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
900 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
902 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
903 ccb->csio.scsi_status = 0; /* XXX */
905 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
907 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
913 scsi_low_timeout_cam(struct scsi_low_softc *slp, int ch, int action)
917 case SCSI_LOW_TIMEOUT_CH_IO:
920 case SCSI_LOW_TIMEOUT_START:
921 callout_reset(&slp->sl_si.timeout_ch,
922 hz / SCSI_LOW_TIMEOUT_HZ, scsi_low_timeout, slp);
924 case SCSI_LOW_TIMEOUT_STOP:
925 callout_stop(&slp->sl_si.timeout_ch);
930 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
933 case SCSI_LOW_TIMEOUT_START:
934 callout_reset(&slp->sl_si.engage_ch, 1,
935 scsi_low_engage, slp);
937 case SCSI_LOW_TIMEOUT_STOP:
938 callout_stop(&slp->sl_si.engage_ch);
942 case SCSI_LOW_TIMEOUT_CH_RECOVER:
947 /**************************************************************
948 * scsi low deactivate and activate
949 **************************************************************/
951 scsi_low_is_busy(struct scsi_low_softc *slp)
959 scsi_low_deactivate(struct scsi_low_softc *slp)
962 slp->sl_flags |= HW_INACTIVE;
963 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
964 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
965 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
966 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
972 scsi_low_activate(struct scsi_low_softc *slp)
977 slp->sl_flags &= ~HW_INACTIVE;
978 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
980 slp->sl_flags |= HW_INACTIVE;
985 slp->sl_timeout_count = 0;
986 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
987 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
992 /**************************************************************
994 **************************************************************/
995 #ifdef SCSI_LOW_DIAGNOSTIC
996 static void scsi_low_msg_log_init (struct scsi_low_msg_log *);
997 static void scsi_low_msg_log_write (struct scsi_low_msg_log *, u_int8_t *,
999 static void scsi_low_msg_log_show (struct scsi_low_msg_log *, char *, int);
1002 scsi_low_msg_log_init(struct scsi_low_msg_log *slmlp)
1004 slmlp->slml_ptr = 0;
1008 scsi_low_msg_log_write(struct scsi_low_msg_log *slmlp, u_int8_t *datap, int len)
1012 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1015 ptr = slmlp->slml_ptr ++;
1016 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1017 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1018 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1019 slmlp->slml_msg[ptr].msg[ind] = 0;
1023 scsi_low_msg_log_show(struct scsi_low_msg_log *slmlp, char *s, int len)
1027 kprintf("%s: (%d) ", s, slmlp->slml_ptr);
1028 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1030 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1033 kprintf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1039 #endif /* SCSI_LOW_DIAGNOSTIC */
1041 /**************************************************************
1043 **************************************************************/
1045 scsi_low_engage(void *arg)
1047 struct scsi_low_softc *slp = arg;
1051 switch (slp->sl_rstep)
1055 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1056 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1057 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1062 slp->sl_flags &= ~HW_RESUME;
1063 scsi_low_start(slp);
1073 scsi_low_init(struct scsi_low_softc *slp, u_int flags)
1077 slp->sl_flags |= HW_INITIALIZING;
1079 /* clear power control timeout */
1080 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1082 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1083 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1084 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1086 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1089 /* reset current nexus */
1090 scsi_low_reset_nexus(slp, flags);
1091 if ((slp->sl_flags & HW_INACTIVE) != 0)
1097 if (flags != SCSI_LOW_RESTART_SOFT)
1099 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1103 slp->sl_flags &= ~HW_INITIALIZING;
1107 /**************************************************************
1109 **************************************************************/
1110 static struct lun_info *
1111 scsi_low_alloc_li(struct targ_info *ti, int lun, int alloc)
1113 struct scsi_low_softc *slp = ti->ti_sc;
1114 struct lun_info *li;
1116 li = LIST_FIRST(&ti->ti_litab);
1119 if (li->li_lun == lun)
1122 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1124 if (li->li_lun == lun)
1126 LIST_REMOVE(li, lun_chain);
1127 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1136 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1138 panic("no lun info mem");
1140 SCSI_LOW_BZERO(li, ti->ti_lunsize);
1144 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1146 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1147 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1148 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1149 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1150 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1152 li->li_qtagbits = (u_int) -1;
1154 TAILQ_INIT(&li->li_discq);
1155 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1157 /* host specific structure initialization per lun */
1158 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1159 (*slp->sl_funcs->scsi_low_lun_init)
1160 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1161 scsi_low_calcf_lun(li);
1165 /**************************************************************
1166 * allocate targ_info
1167 **************************************************************/
1168 static struct targ_info *
1169 scsi_low_alloc_ti(struct scsi_low_softc *slp, int targ)
1171 struct targ_info *ti;
1173 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1174 TAILQ_INIT(&slp->sl_titab);
1176 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1178 panic("%s short of memory", slp->sl_xname);
1180 SCSI_LOW_BZERO(ti, slp->sl_targsize);
1184 slp->sl_ti[targ] = ti;
1185 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1186 LIST_INIT(&ti->ti_litab);
1188 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1189 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1190 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1191 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1192 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1193 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1195 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1197 (*slp->sl_funcs->scsi_low_targ_init)
1198 (slp, ti, SCSI_LOW_INFO_ALLOC);
1200 scsi_low_calcf_target(ti);
1205 scsi_low_free_ti(struct scsi_low_softc *slp)
1207 struct targ_info *ti, *tib;
1208 struct lun_info *li, *nli;
1210 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1212 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1214 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1216 (*slp->sl_funcs->scsi_low_lun_init)
1217 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1219 nli = LIST_NEXT(li, lun_chain);
1223 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1225 (*slp->sl_funcs->scsi_low_targ_init)
1226 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1228 tib = TAILQ_NEXT(ti, ti_chain);
1233 /**************************************************************
1235 **************************************************************/
1237 scsi_low_bus_idle(struct scsi_low_softc *slp)
1239 slp->sl_retry_sel = 0;
1240 if (slp->sl_Tnexus == NULL)
1241 scsi_low_start(slp);
1245 scsi_low_timeout(void *arg)
1247 struct scsi_low_softc *slp = arg;
1250 scsi_low_timeout_check(slp);
1251 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1252 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1257 scsi_low_timeout_check(struct scsi_low_softc *slp)
1259 struct targ_info *ti;
1260 struct lun_info *li;
1261 struct slccb *cb = NULL; /* XXX */
1263 /* selection restart */
1264 if (slp->sl_retry_sel != 0)
1266 slp->sl_retry_sel = 0;
1267 if (slp->sl_Tnexus != NULL)
1270 cb = TAILQ_FIRST(&slp->sl_start);
1274 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1276 cb->ccb_flags |= CCB_NORETRY;
1277 cb->ccb_error |= SELTIMEOUTIO;
1278 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1279 panic("%s: ccb not finished", slp->sl_xname);
1282 if (slp->sl_Tnexus == NULL)
1283 scsi_low_start(slp);
1286 /* call hardware timeout */
1288 if (slp->sl_funcs->scsi_low_timeout != NULL)
1290 (*slp->sl_funcs->scsi_low_timeout) (slp);
1293 if (slp->sl_timeout_count ++ <
1294 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1297 slp->sl_timeout_count = 0;
1298 if (slp->sl_nio > 0)
1300 if ((cb = slp->sl_Qnexus) != NULL)
1302 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1306 else if (slp->sl_disc == 0)
1308 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1311 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1315 else TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
1317 if (ti->ti_disc == 0)
1320 LIST_FOREACH(li, &ti->ti_litab, lun_chain)
1322 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
1325 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1333 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1335 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1338 if (slp->sl_active != 0)
1340 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1346 if (slp->sl_powc < 0)
1348 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1349 slp->sl_flags |= HW_POWDOWN;
1350 (*slp->sl_funcs->scsi_low_power)
1351 (slp, SCSI_LOW_POWDOWN);
1357 cb->ccb_error |= TIMEOUTIO;
1358 kprintf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
1359 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1360 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1361 scsi_low_start(slp);
1367 scsi_low_abort_ccb(struct scsi_low_softc *slp, struct slccb *cb)
1369 struct targ_info *ti;
1370 struct lun_info *li;
1375 if ((cb->ccb_omsgoutflag &
1376 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1381 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1382 msg = SCSI_LOW_MSG_ABORT;
1384 msg = SCSI_LOW_MSG_ABORT_QTAG;
1386 cb->ccb_error |= ABORTIO;
1387 cb->ccb_flags |= CCB_NORETRY;
1388 scsi_low_ccb_message_assert(cb, msg);
1390 if (cb == slp->sl_Qnexus)
1392 scsi_low_assert_msg(slp, ti, msg, 1);
1394 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1396 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1397 panic("%s: revoked ccb done", slp->sl_xname);
1399 cb->ccb_flags |= CCB_STARTQ;
1400 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1402 if (slp->sl_Tnexus == NULL)
1403 scsi_low_start(slp);
1407 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1408 panic("%s: revoked ccb retried", slp->sl_xname);
1413 /**************************************************************
1414 * Generic SCSI INTERFACE
1415 **************************************************************/
1417 scsi_low_attach(struct scsi_low_softc *slp, int openings, int ntargs, int nluns,
1418 int targsize, int lunsize)
1420 struct targ_info *ti;
1421 struct lun_info *li;
1424 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
1426 if (slp->sl_osdep_fp == NULL)
1427 panic("scsi_low: interface not spcified");
1429 if (ntargs > SCSI_LOW_NTARGETS)
1431 kprintf("scsi_low: %d targets are too large\n", ntargs);
1432 kprintf("change kernel options SCSI_LOW_NTARGETS");
1437 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1439 slp->sl_openings = openings;
1440 slp->sl_ntargs = ntargs;
1441 slp->sl_nluns = nluns;
1442 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1444 if (lunsize < sizeof(struct lun_info))
1445 lunsize = sizeof(struct lun_info);
1447 if (targsize < sizeof(struct targ_info))
1448 targsize = sizeof(struct targ_info);
1450 slp->sl_targsize = targsize;
1451 for (i = 0; i < ntargs; i ++)
1453 ti = scsi_low_alloc_ti(slp, i);
1454 ti->ti_lunsize = lunsize;
1455 li = scsi_low_alloc_li(ti, 0, 1);
1458 /* initialize queue */
1459 nccb = openings * ntargs;
1460 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1461 nccb = SCSI_LOW_NCCB;
1462 scsi_low_init_ccbque(nccb);
1463 TAILQ_INIT(&slp->sl_start);
1465 /* call os depend attach */
1466 callout_init(&slp->sl_si.timeout_ch);
1467 callout_init(&slp->sl_si.engage_ch);
1470 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
1474 kprintf("%s: scsi_low_attach: osdep attach failed\n",
1479 /* check hardware */
1480 SCSI_LOW_DELAY(1000); /* wait for 1ms */
1481 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1484 kprintf("%s: scsi_low_attach: initialization failed\n",
1489 /* start watch dog */
1490 slp->sl_timeout_count = 0;
1491 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1492 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1493 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1496 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1498 #ifdef SCSI_LOW_START_UP_CHECK
1499 /* probing devices */
1500 scsi_low_start_up(slp);
1501 #endif /* SCSI_LOW_START_UP_CHECK */
1503 /* call os depend attach done*/
1504 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
1510 scsi_low_dettach(struct scsi_low_softc *slp)
1515 if (scsi_low_is_busy(slp) != 0)
1521 scsi_low_deactivate(slp);
1523 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
1530 scsi_low_free_ti(slp);
1531 LIST_REMOVE(slp, sl_chain);
1536 /**************************************************************
1538 **************************************************************/
1540 scsi_low_enqueue(struct scsi_low_softc *slp, struct targ_info *ti,
1541 struct lun_info *li, struct slccb *cb, u_int flags,
1548 scsi_low_ccb_message_assert(cb, msg);
1550 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1551 scsi_low_alloc_qtag(cb);
1553 cb->ccb_flags = flags | CCB_STARTQ;
1554 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1555 cb->ccb_error |= PENDINGIO;
1557 if ((flags & CCB_URGENT) != 0)
1559 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1563 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1568 if (slp->sl_Tnexus == NULL)
1569 scsi_low_start(slp);
1574 scsi_low_message_enqueue(struct scsi_low_softc *slp, struct targ_info *ti,
1575 struct lun_info *li, u_int flags)
1580 tmsgflags = ti->ti_setup_msg;
1581 ti->ti_setup_msg = 0;
1583 flags |= CCB_NORETRY;
1584 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1589 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1593 /**************************************************************
1594 * Generic Start & Done
1595 **************************************************************/
1596 #define SLSC_MODE_SENSE_SHORT 0x1a
1597 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1598 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1599 sizeof(struct scsi_low_mode_sense_data), 0};
1600 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1601 sizeof(struct scsi_low_inq_data), 0};
1602 static u_int8_t unit_ready_cmd[6];
1603 static int scsi_low_setup_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1604 static int scsi_low_sense_abort_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1605 static int scsi_low_resume (struct scsi_low_softc *);
1608 scsi_low_unit_ready_cmd(struct slccb *cb)
1610 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1611 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1612 cb->ccb_scp.scp_datalen = 0;
1613 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1618 scsi_low_sense_abort_start(struct scsi_low_softc *slp, struct targ_info *ti,
1619 struct lun_info *li, struct slccb *cb)
1621 cb->ccb_scp.scp_cmdlen = 6;
1622 SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1623 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1624 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1625 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1626 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1627 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1628 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1630 scsi_low_ccb_message_clear(cb);
1631 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1633 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1637 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
1638 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1639 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1640 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1643 return SCSI_LOW_START_NO_QTAG;
1647 scsi_low_setup_start(struct scsi_low_softc *slp, struct targ_info *ti,
1648 struct lun_info *li, struct slccb *cb)
1650 switch(li->li_state)
1652 case SCSI_LOW_LUN_SLEEP:
1653 scsi_low_unit_ready_cmd(cb);
1656 case SCSI_LOW_LUN_START:
1657 cb->ccb_scp.scp_cmd = ss_cmd;
1658 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1659 cb->ccb_scp.scp_datalen = 0;
1660 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1664 case SCSI_LOW_LUN_INQ:
1665 cb->ccb_scp.scp_cmd = inq_cmd;
1666 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1667 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1668 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1669 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1673 case SCSI_LOW_LUN_MODEQ:
1674 cb->ccb_scp.scp_cmd = sms_cmd;
1675 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1676 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1677 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1678 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1680 return SCSI_LOW_START_QTAG;
1683 panic("%s: no setup phase", slp->sl_xname);
1686 return SCSI_LOW_START_NO_QTAG;
1690 scsi_low_resume(struct scsi_low_softc *slp)
1692 if (slp->sl_flags & HW_RESUME)
1694 slp->sl_flags &= ~HW_POWDOWN;
1695 if (slp->sl_funcs->scsi_low_power != NULL)
1697 slp->sl_flags |= HW_RESUME;
1699 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1700 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1701 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
1702 SCSI_LOW_TIMEOUT_START);
1709 scsi_low_start(struct scsi_low_softc *slp)
1711 struct targ_info *ti;
1712 struct lun_info *li;
1716 /* check hardware exists or under initializations ? */
1717 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1720 /* check hardware power up ? */
1721 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1724 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1726 if (scsi_low_resume(slp) == EJUSTRETURN)
1732 #ifdef SCSI_LOW_DIAGNOSTIC
1733 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1735 scsi_low_info(slp, NULL, "NEXUS INCONSISTENT");
1736 panic("%s: inconsistent", slp->sl_xname);
1738 #endif /* SCSI_LOW_DIAGNOSTIC */
1740 TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
1744 if (li->li_disc == 0)
1746 goto scsi_low_cmd_start;
1748 else if (li->li_nqio > 0)
1750 if (li->li_nqio < li->li_maxnqio ||
1751 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1752 goto scsi_low_cmd_start;
1758 cb->ccb_flags &= ~CCB_STARTQ;
1759 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1762 /* clear all error flag bits (for restart) */
1764 cb->ccb_datalen = -1;
1765 cb->ccb_scp.scp_status = ST_UNKNOWN;
1767 /* setup nexus pointer */
1768 slp->sl_Qnexus = cb;
1769 slp->sl_Lnexus = li;
1770 slp->sl_Tnexus = ti;
1772 /* initialize msgsys */
1773 scsi_low_init_msgsys(slp, ti);
1776 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1778 /* CA state or forced abort */
1779 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1781 else if (li->li_state >= SCSI_LOW_LUN_OK)
1783 cb->ccb_flags &= ~CCB_INTERNAL;
1784 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
1785 if (cb->ccb_msgoutflag != 0)
1787 scsi_low_ccb_message_exec(slp, cb);
1792 cb->ccb_flags |= CCB_INTERNAL;
1793 rv = scsi_low_setup_start(slp, ti, li, cb);
1797 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1799 if (rv == SCSI_LOW_START_QTAG &&
1800 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1805 scsi_low_activate_qtag(cb);
1806 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1807 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1808 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1809 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1810 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1812 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1813 scsi_low_assert_msg(slp, ti, qmsg, 0);
1817 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1818 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1819 cb->ccb_tc = cb->ccb_tcmax;
1821 /* setup saved scsi data pointer */
1822 cb->ccb_sscp = cb->ccb_scp;
1824 /* setup current scsi pointer */
1825 slp->sl_scp = cb->ccb_sscp;
1826 slp->sl_error = cb->ccb_error;
1828 /* assert always an identify msg */
1829 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1832 #ifdef SCSI_LOW_DIAGNOSTIC
1833 scsi_low_msg_log_init(&ti->ti_log_msgin);
1834 scsi_low_msg_log_init(&ti->ti_log_msgout);
1835 #endif /* SCSI_LOW_DIAGNOSTIC */
1837 /* selection start */
1839 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1840 if (rv == SCSI_LOW_START_OK)
1842 #ifdef SCSI_LOW_STATICS
1843 scsi_low_statics.nexus_win ++;
1844 #endif /* SCSI_LOW_STATICS */
1848 scsi_low_arbit_fail(slp, cb);
1849 #ifdef SCSI_LOW_STATICS
1850 scsi_low_statics.nexus_fail ++;
1851 #endif /* SCSI_LOW_STATICS */
1855 scsi_low_arbit_fail(struct scsi_low_softc *slp, struct slccb *cb)
1857 struct targ_info *ti = cb->ti;
1859 scsi_low_deactivate_qtag(cb);
1860 scsi_low_ccb_message_retry(cb);
1861 cb->ccb_flags |= CCB_STARTQ;
1862 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1864 scsi_low_bus_release(slp, ti);
1867 if (slp->sl_disc == 0)
1869 #ifdef SCSI_LOW_DIAGNOSTIC
1870 kprintf("%s: try selection again\n", slp->sl_xname);
1871 #endif /* SCSI_LOW_DIAGNOSTIC */
1872 slp->sl_retry_sel = 1;
1877 scsi_low_bus_release(struct scsi_low_softc *slp, struct targ_info *ti)
1879 if (ti->ti_disc > 0)
1881 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1885 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1888 /* clear all nexus pointer */
1889 slp->sl_Qnexus = NULL;
1890 slp->sl_Lnexus = NULL;
1891 slp->sl_Tnexus = NULL;
1893 /* clear selection assert */
1894 slp->sl_selid = NULL;
1896 /* clear nexus data */
1897 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1899 /* clear phase change counter */
1900 slp->sl_ph_count = 0;
1904 scsi_low_setup_done(struct scsi_low_softc *slp, struct slccb *cb)
1906 struct targ_info *ti;
1907 struct lun_info *li;
1912 if (cb->ccb_rcnt >= slp->sl_max_retry)
1914 cb->ccb_error |= ABORTIO;
1915 return SCSI_LOW_DONE_COMPLETE;
1918 /* XXX: special huck for selection timeout */
1919 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1920 (cb->ccb_error & SELTIMEOUTIO) != 0)
1922 cb->ccb_error |= ABORTIO;
1923 return SCSI_LOW_DONE_COMPLETE;
1926 switch(li->li_state)
1928 case SCSI_LOW_LUN_INQ:
1929 if (cb->ccb_error != 0)
1932 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
1936 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
1938 else if ((li->li_inq.sd_version & 7) >= 2 ||
1939 (li->li_inq.sd_len >= 4))
1941 if ((li->li_inq.sd_support & 0x2) == 0)
1942 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1943 if ((li->li_inq.sd_support & 0x8) == 0)
1944 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
1947 if ((li->li_inq.sd_support & 0x10) == 0)
1948 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
1949 if ((li->li_inq.sd_support & 0x20) == 0)
1950 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
1951 if ((li->li_inq.sd_support & 0x40) == 0)
1952 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
1957 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
1960 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
1962 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
1964 scsi_low_calcf_target(ti);
1965 scsi_low_calcf_lun(li);
1968 case SCSI_LOW_LUN_MODEQ:
1969 if (cb->ccb_error != 0)
1971 if (cb->ccb_error & SENSEIO)
1973 #ifdef SCSI_LOW_DEBUG
1974 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
1976 kprintf("SENSE: [%x][%x][%x][%x][%x]\n",
1977 (u_int) cb->ccb_sense.error_code,
1978 (u_int) cb->ccb_sense.segment,
1979 (u_int) cb->ccb_sense.flags,
1980 (u_int) cb->ccb_sense.add_sense_code,
1981 (u_int) cb->ccb_sense.add_sense_code_qual);
1983 #endif /* SCSI_LOW_DEBUG */
1987 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1990 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
1992 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
1993 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
1995 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
1996 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
1997 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
1999 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2000 scsi_low_calcf_lun(li);
2008 if (li->li_state == SCSI_LOW_LUN_OK)
2010 scsi_low_calcf_target(ti);
2011 scsi_low_calcf_lun(li);
2012 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2013 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2015 scsi_low_calcf_show(li);
2020 return SCSI_LOW_DONE_RETRY;
2024 scsi_low_done(struct scsi_low_softc *slp, struct slccb *cb)
2028 if (cb->ccb_error == 0)
2030 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2032 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2034 * SCSI-2 draft suggests
2035 * page 0x0a QErr bit determins if
2036 * the target aborts or continues
2037 * the queueing io's after CA state resolved.
2038 * However many targets seem not to support
2039 * the page 0x0a. Thus we should manually clear the
2040 * queuing io's after CA state.
2042 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2045 cb->ccb_flags |= CCB_CLEARQ;
2048 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2050 if ((cb->ccb_flags & CCB_SENSE) != 0)
2051 cb->ccb_error |= (SENSEIO | ABORTIO);
2052 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2054 else switch (cb->ccb_sscp.scp_status)
2060 if (cb->ccb_datalen == 0 ||
2061 cb->ccb_scp.scp_datalen == 0)
2064 if (cb->ccb_scp.scp_cmdlen > 0 &&
2065 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2066 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2069 cb->ccb_error |= PDMAERR;
2074 cb->ccb_error |= (BUSYERR | STATERR);
2078 cb->ccb_error |= (STATERR | ABORTIO);
2083 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2086 cb->ccb_flags |= CCB_SENSE;
2089 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2094 cb->ccb_error |= FATALIO;
2100 if (cb->ccb_flags & CCB_SENSE)
2102 cb->ccb_error |= (SENSEERR | ABORTIO);
2104 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2108 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2110 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2114 /* check a ccb msgout flag */
2115 if (cb->ccb_omsgoutflag != 0)
2117 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2118 SCSI_LOW_MSG_ABORT_QTAG | \
2119 SCSI_LOW_MSG_CLEAR_QTAG | \
2120 SCSI_LOW_MSG_TERMIO)
2122 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2124 cb->ccb_error |= ABORTIO;
2128 /* call OS depend done */
2129 if (cb->osdep != NULL)
2131 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2132 if (rv == EJUSTRETURN)
2135 else if (cb->ccb_error != 0)
2137 if (cb->ccb_rcnt >= slp->sl_max_retry)
2138 cb->ccb_error |= ABORTIO;
2140 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2141 (cb->ccb_error & ABORTIO) == 0)
2145 /* free our target */
2146 #ifdef SCSI_LOW_DEBUG
2147 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2149 kprintf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2150 scsi_low_print(slp, NULL);
2152 #endif /* SCSI_LOW_DEBUG */
2154 scsi_low_deactivate_qtag(cb);
2155 scsi_low_dealloc_qtag(cb);
2156 scsi_low_free_ccb(cb);
2158 return SCSI_LOW_DONE_COMPLETE;
2161 #ifdef SCSI_LOW_DEBUG
2162 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2164 kprintf("** SCSI_LOW_DONE_RETRY ===============\n");
2165 scsi_low_print(slp, NULL);
2167 #endif /* SCSI_LOW_DEBUG */
2170 scsi_low_deactivate_qtag(cb);
2171 scsi_low_ccb_message_retry(cb);
2172 return SCSI_LOW_DONE_RETRY;
2175 /**************************************************************
2177 **************************************************************/
2179 scsi_low_reset_nexus_target(struct scsi_low_softc *slp, struct targ_info *ti,
2182 struct lun_info *li;
2184 LIST_FOREACH(li, &ti->ti_litab, lun_chain)
2186 scsi_low_reset_nexus_lun(slp, li, fdone);
2187 li->li_state = SCSI_LOW_LUN_SLEEP;
2192 ti->ti_setup_msg = 0;
2193 ti->ti_setup_msg_done = 0;
2195 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2196 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2198 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2199 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2201 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2203 ((*slp->sl_funcs->scsi_low_targ_init)
2204 (slp, ti, SCSI_LOW_INFO_REVOKE));
2206 scsi_low_calcf_target(ti);
2208 LIST_FOREACH(li, &ti->ti_litab, lun_chain)
2212 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2213 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2215 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2217 ((*slp->sl_funcs->scsi_low_lun_init)
2218 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2220 scsi_low_calcf_lun(li);
2225 scsi_low_reset_nexus(struct scsi_low_softc *slp, int fdone)
2227 struct targ_info *ti;
2228 struct slccb *cb, *topcb;
2230 if ((cb = slp->sl_Qnexus) != NULL)
2232 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2239 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
2241 scsi_low_reset_nexus_target(slp, ti, fdone);
2242 scsi_low_bus_release(slp, ti);
2243 scsi_low_init_msgsys(slp, ti);
2248 topcb->ccb_flags |= CCB_STARTQ;
2249 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2253 slp->sl_retry_sel = 0;
2254 slp->sl_flags &= ~HW_PDMASTART;
2259 static char tw_chars[] = "|/-\\";
2260 #define TWIDDLEWAIT 10000
2263 scsi_low_twiddle_wait(void)
2265 kprintf("\b%c", tw_chars[tw_pos++]);
2266 tw_pos %= (sizeof(tw_chars) - 1);
2267 SCSI_LOW_DELAY(TWIDDLEWAIT);
2271 scsi_low_bus_reset(struct scsi_low_softc *slp)
2275 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2277 kprintf("%s: try to reset scsi bus ", slp->sl_xname);
2278 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2279 scsi_low_twiddle_wait();
2284 scsi_low_restart(struct scsi_low_softc *slp, int flags, u_char *s)
2289 kprintf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
2291 if ((error = scsi_low_init(slp, flags)) != 0)
2294 scsi_low_start(slp);
2298 /**************************************************************
2299 * disconnect and reselect
2300 **************************************************************/
2301 #define MSGCMD_LUN(msg) (msg & 0x07)
2303 static struct slccb *
2304 scsi_low_establish_ccb(struct targ_info *ti, struct lun_info *li, scsi_low_tag_t tag)
2306 struct scsi_low_softc *slp = ti->ti_sc;
2312 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
2313 if (cb->ccb_tag == tag)
2318 * establish our ccb nexus
2321 #ifdef SCSI_LOW_DEBUG
2322 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2324 kprintf("%s: nexus(0x%lx) abort check start\n",
2325 slp->sl_xname, (u_long) cb);
2326 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2327 scsi_low_revoke_ccb(slp, cb, 1);
2331 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2333 if (cb->ccb_omsgoutflag == 0)
2334 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2336 #endif /* SCSI_LOW_DEBUG */
2338 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2339 cb->ccb_flags &= ~CCB_DISCQ;
2340 slp->sl_Qnexus = cb;
2342 slp->sl_scp = cb->ccb_sscp;
2343 slp->sl_error |= cb->ccb_error;
2349 /* inform "ccb nexus established" to the host driver */
2350 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2353 if (cb->ccb_msgoutflag != 0)
2355 scsi_low_ccb_message_exec(slp, cb);
2362 scsi_low_reselected(struct scsi_low_softc *slp, u_int targ)
2364 struct targ_info *ti;
2369 * Check select vs reselected collision.
2372 if ((cb = slp->sl_selid) != NULL)
2374 scsi_low_arbit_fail(slp, cb);
2375 #ifdef SCSI_LOW_STATICS
2376 scsi_low_statics.nexus_conflict ++;
2377 #endif /* SCSI_LOW_STATICS */
2381 * Check if no current active nexus.
2383 if (slp->sl_Tnexus != NULL)
2390 * Check a valid target id asserted ?
2392 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2394 s = "scsi id illegal";
2399 * Check the target scsi status.
2401 ti = slp->sl_ti[targ];
2402 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2404 s = "phase mismatch";
2412 scsi_low_init_msgsys(slp, ti);
2415 * Establish our target nexus
2417 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2418 slp->sl_Tnexus = ti;
2419 #ifdef SCSI_LOW_STATICS
2420 scsi_low_statics.nexus_reselected ++;
2421 #endif /* SCSI_LOW_STATICS */
2425 kprintf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
2426 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2427 "reselect: scsi world confused");
2431 /**************************************************************
2432 * cmd out pointer setup
2433 **************************************************************/
2435 scsi_low_cmd(struct scsi_low_softc *slp, struct targ_info *ti)
2437 struct slccb *cb = slp->sl_Qnexus;
2439 slp->sl_ph_count ++;
2445 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2446 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2447 slp->sl_scp.scp_datalen = 0;
2448 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2449 slp->sl_error |= FATALIO;
2450 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2451 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2456 #ifdef SCSI_LOW_DEBUG
2457 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2459 scsi_low_test_cmdlnk(slp, cb);
2461 #endif /* SCSI_LOW_DEBUG */
2466 /**************************************************************
2467 * data out pointer setup
2468 **************************************************************/
2470 scsi_low_data(struct scsi_low_softc *slp, struct targ_info *ti,
2471 struct buf **bp, int direction)
2473 struct slccb *cb = slp->sl_Qnexus;
2475 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2481 slp->sl_error |= (FATALIO | PDMAERR);
2482 slp->sl_scp.scp_datalen = 0;
2483 slp->sl_scp.scp_direction = direction;
2484 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2485 if (ti->ti_ophase != ti->ti_phase)
2490 s = "DATA PHASE: ccb nexus not found";
2492 s = "DATA PHASE: xfer direction mismatch";
2493 SCSI_LOW_INFO(slp, ti, s);
2500 /**************************************************************
2502 **************************************************************/
2503 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2504 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2505 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2506 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2507 #define MSGIN_DATA_LAST 0x30
2509 static int scsi_low_errfunc_synch (struct scsi_low_softc *, u_int);
2510 static int scsi_low_errfunc_wide (struct scsi_low_softc *, u_int);
2511 static int scsi_low_errfunc_identify (struct scsi_low_softc *, u_int);
2512 static int scsi_low_errfunc_qtag (struct scsi_low_softc *, u_int);
2514 static int scsi_low_msgfunc_synch (struct scsi_low_softc *);
2515 static int scsi_low_msgfunc_wide (struct scsi_low_softc *);
2516 static int scsi_low_msgfunc_identify (struct scsi_low_softc *);
2517 static int scsi_low_msgfunc_abort (struct scsi_low_softc *);
2518 static int scsi_low_msgfunc_qabort (struct scsi_low_softc *);
2519 static int scsi_low_msgfunc_qtag (struct scsi_low_softc *);
2520 static int scsi_low_msgfunc_reset (struct scsi_low_softc *);
2522 struct scsi_low_msgout_data {
2525 int (*md_msgfunc) (struct scsi_low_softc *);
2526 int (*md_errfunc) (struct scsi_low_softc *, u_int);
2527 #define MSG_RELEASE_ATN 0x0001
2531 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2532 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2533 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2534 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2535 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2536 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2537 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2538 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2539 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2540 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2541 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2542 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2543 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2544 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2545 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2546 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2547 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2550 static int scsi_low_msginfunc_ext (struct scsi_low_softc *);
2551 static int scsi_low_synch (struct scsi_low_softc *);
2552 static int scsi_low_wide (struct scsi_low_softc *);
2553 static int scsi_low_msginfunc_msg_reject (struct scsi_low_softc *);
2554 static int scsi_low_msginfunc_rejop (struct scsi_low_softc *);
2555 static int scsi_low_msginfunc_rp (struct scsi_low_softc *);
2556 static int scsi_low_msginfunc_sdp (struct scsi_low_softc *);
2557 static int scsi_low_msginfunc_disc (struct scsi_low_softc *);
2558 static int scsi_low_msginfunc_cc (struct scsi_low_softc *);
2559 static int scsi_low_msginfunc_lcc (struct scsi_low_softc *);
2560 static int scsi_low_msginfunc_parity (struct scsi_low_softc *);
2561 static int scsi_low_msginfunc_noop (struct scsi_low_softc *);
2562 static int scsi_low_msginfunc_simple_qtag (struct scsi_low_softc *);
2563 static int scsi_low_msginfunc_i_wide_residue (struct scsi_low_softc *);
2565 struct scsi_low_msgin_data {
2567 int (*md_msgfunc) (struct scsi_low_softc *);
2570 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2571 /* 0 */ {1, scsi_low_msginfunc_cc},
2572 /* 1 */ {2, scsi_low_msginfunc_ext},
2573 /* 2 */ {1, scsi_low_msginfunc_sdp},
2574 /* 3 */ {1, scsi_low_msginfunc_rp},
2575 /* 4 */ {1, scsi_low_msginfunc_disc},
2576 /* 5 */ {1, scsi_low_msginfunc_rejop},
2577 /* 6 */ {1, scsi_low_msginfunc_rejop},
2578 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2579 /* 8 */ {1, scsi_low_msginfunc_noop},
2580 /* 9 */ {1, scsi_low_msginfunc_parity},
2581 /* a */ {1, scsi_low_msginfunc_lcc},
2582 /* b */ {1, scsi_low_msginfunc_lcc},
2583 /* c */ {1, scsi_low_msginfunc_rejop},
2584 /* d */ {2, scsi_low_msginfunc_rejop},
2585 /* e */ {1, scsi_low_msginfunc_rejop},
2586 /* f */ {1, scsi_low_msginfunc_rejop},
2587 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2588 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2589 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2590 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2591 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2592 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2593 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2594 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2595 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2596 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2597 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2598 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2599 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2600 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2601 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2602 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2603 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2604 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2605 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2606 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2607 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2608 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2609 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2610 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2611 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2612 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2613 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2614 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2615 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2616 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2617 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2618 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2619 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2622 /**************************************************************
2624 **************************************************************/
2626 scsi_low_msgfunc_synch(struct scsi_low_softc *slp)
2628 struct targ_info *ti = slp->sl_Tnexus;
2629 int ptr = ti->ti_msgoutlen;
2631 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2632 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2633 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2634 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2635 return MSG_EXTEND_SYNCHLEN + 2;
2639 scsi_low_msgfunc_wide(struct scsi_low_softc *slp)
2641 struct targ_info *ti = slp->sl_Tnexus;
2642 int ptr = ti->ti_msgoutlen;
2644 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2645 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2646 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2647 return MSG_EXTEND_WIDELEN + 2;
2651 scsi_low_msgfunc_identify(struct scsi_low_softc *slp)
2653 struct targ_info *ti = slp->sl_Tnexus;
2654 struct lun_info *li = slp->sl_Lnexus;
2655 struct slccb *cb = slp->sl_Qnexus;
2656 int ptr = ti->ti_msgoutlen;
2662 slp->sl_error |= FATALIO;
2663 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2664 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2668 if (scsi_low_is_disconnect_ok(cb) != 0)
2669 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2673 if (ti->ti_phase == PH_MSGOUT)
2675 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2676 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2678 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2682 ti->ti_msgoutstr[ptr + 0] = msg;
2687 scsi_low_msgfunc_abort(struct scsi_low_softc *slp)
2689 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2694 scsi_low_msgfunc_qabort(struct scsi_low_softc *slp)
2696 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2701 scsi_low_msgfunc_reset(struct scsi_low_softc *slp)
2703 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2708 scsi_low_msgfunc_qtag(struct scsi_low_softc *slp)
2710 struct targ_info *ti = slp->sl_Tnexus;
2711 struct slccb *cb = slp->sl_Qnexus;
2712 int ptr = ti->ti_msgoutlen;
2714 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2716 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2721 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2722 if (ti->ti_phase == PH_MSGOUT)
2724 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2731 * The following functions are called when targets give unexpected
2732 * responces in msgin (after msgout).
2735 scsi_low_errfunc_identify(struct scsi_low_softc *slp, u_int msgflags)
2737 if (slp->sl_Lnexus != NULL)
2739 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2740 scsi_low_calcf_lun(slp->sl_Lnexus);
2746 scsi_low_errfunc_synch(struct scsi_low_softc *slp, u_int msgflags)
2748 struct targ_info *ti = slp->sl_Tnexus;
2750 MSGIN_PERIOD(ti) = 0;
2751 MSGIN_OFFSET(ti) = 0;
2752 scsi_low_synch(slp);
2757 scsi_low_errfunc_wide(struct scsi_low_softc *slp, u_int msgflags)
2759 struct targ_info *ti = slp->sl_Tnexus;
2761 MSGIN_WIDTHP(ti) = 0;
2767 scsi_low_errfunc_qtag(struct scsi_low_softc *slp, u_int msgflags)
2769 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2771 if (slp->sl_Qnexus != NULL)
2773 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2775 if (slp->sl_Lnexus != NULL)
2777 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2778 scsi_low_calcf_lun(slp->sl_Lnexus);
2780 kprintf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
2787 scsi_low_msgout(struct scsi_low_softc *slp, struct targ_info *ti, u_int fl)
2789 struct scsi_low_msgout_data *mdp;
2792 #ifdef SCSI_LOW_DIAGNOSTIC
2793 if (ti != slp->sl_Tnexus)
2795 scsi_low_print(slp, NULL);
2796 panic("scsi_low_msgout: Target nexus inconsistent");
2798 #endif /* SCSI_LOW_DIAGNOSTIC */
2800 slp->sl_ph_count ++;
2801 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2803 kprintf("%s: too many phase changes\n", slp->sl_xname);
2804 slp->sl_error |= FATALIO;
2805 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2809 * Scsi phase changes.
2810 * Previously msgs asserted are accepted by our target or
2811 * processed by scsi_low_msgin.
2812 * Thus clear all saved informations.
2814 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2816 ti->ti_omsgflags = 0;
2817 ti->ti_emsgflags = 0;
2819 else if (slp->sl_atten == 0)
2822 * We did not assert attention, however still our target required
2823 * msgs. Resend previous msgs.
2825 ti->ti_msgflags |= ti->ti_omsgflags;
2826 ti->ti_omsgflags = 0;
2827 #ifdef SCSI_LOW_DIAGNOSTIC
2828 kprintf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
2829 #endif /* SCSI_LOW_DIAGNOSTIC */
2833 * We have no msgs. send MSG_NOOP (OK?)
2835 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2836 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2841 ti->ti_msgoutlen = 0;
2842 slp->sl_clear_atten = 0;
2843 mdp = &scsi_low_msgout_data[0];
2844 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2846 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2848 ti->ti_omsgflags |= mdp->md_flags;
2849 ti->ti_msgflags &= ~mdp->md_flags;
2850 ti->ti_emsgflags = mdp->md_flags;
2852 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2853 if (mdp->md_msgfunc != NULL)
2854 len = (*mdp->md_msgfunc) (slp);
2858 #ifdef SCSI_LOW_DIAGNOSTIC
2859 scsi_low_msg_log_write(&ti->ti_log_msgout,
2860 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2861 #endif /* SCSI_LOW_DIAGNOSTIC */
2863 ti->ti_msgoutlen += len;
2864 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2866 slp->sl_clear_atten = 1;
2870 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2871 ti->ti_msgflags == 0)
2874 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
2879 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2880 slp->sl_clear_atten = 1;
2882 return ti->ti_msgoutlen;
2885 /**************************************************************
2887 **************************************************************/
2889 scsi_low_msginfunc_noop(struct scsi_low_softc *slp)
2895 scsi_low_msginfunc_rejop(struct scsi_low_softc *slp)
2897 struct targ_info *ti = slp->sl_Tnexus;
2898 u_int8_t msg = ti->ti_msgin[0];
2900 kprintf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
2901 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
2906 scsi_low_msginfunc_cc(struct scsi_low_softc *slp)
2908 struct lun_info *li;
2910 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
2912 /* validate status */
2913 if (slp->sl_Qnexus == NULL)
2916 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2917 li = slp->sl_Lnexus;
2918 switch (slp->sl_scp.scp_status)
2921 li->li_maxnqio = li->li_maxnexus;
2926 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
2927 scsi_low_reset_nexus_lun(slp, li, 0);
2935 if (li->li_maxnexus >= li->li_nqio)
2936 li->li_maxnexus = li->li_nqio - 1;
2937 li->li_maxnqio = li->li_maxnexus;
2942 slp->sl_error |= MSGERR;
2952 scsi_low_msginfunc_lcc(struct scsi_low_softc *slp)
2954 struct targ_info *ti;
2955 struct lun_info *li;
2956 struct slccb *ncb, *cb;
2958 ti = slp->sl_Tnexus;
2959 li = slp->sl_Lnexus;
2960 if ((cb = slp->sl_Qnexus) == NULL)
2963 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2964 switch (slp->sl_scp.scp_status)
2968 li->li_maxnqio = li->li_maxnexus;
2972 slp->sl_error |= MSGERR;
2976 if ((li->li_flags & SCSI_LOW_LINK) == 0)
2979 cb->ccb_error |= slp->sl_error;
2980 if (cb->ccb_error != 0)
2983 TAILQ_FOREACH(ncb, &slp->sl_start, ccb_chain)
2986 goto cmd_link_start;
2991 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
2992 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
2996 ncb->ccb_flags &= ~CCB_STARTQ;
2997 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
2999 scsi_low_dealloc_qtag(ncb);
3000 ncb->ccb_tag = cb->ccb_tag;
3001 ncb->ccb_otag = cb->ccb_otag;
3002 cb->ccb_tag = SCSI_LOW_UNKTAG;
3003 cb->ccb_otag = SCSI_LOW_UNKTAG;
3004 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3005 panic("%s: linked ccb retried", slp->sl_xname);
3007 slp->sl_Qnexus = ncb;
3008 slp->sl_ph_count = 0;
3011 ncb->ccb_datalen = -1;
3012 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3013 ncb->ccb_flags &= ~CCB_INTERNAL;
3015 scsi_low_init_msgsys(slp, ti);
3017 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3019 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3020 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3021 ncb->ccb_tc = ncb->ccb_tcmax;
3023 /* setup saved scsi data pointer */
3024 ncb->ccb_sscp = ncb->ccb_scp;
3025 slp->sl_scp = ncb->ccb_sscp;
3026 slp->sl_error = ncb->ccb_error;
3028 #ifdef SCSI_LOW_DIAGNOSTIC
3029 scsi_low_msg_log_init(&ti->ti_log_msgin);
3030 scsi_low_msg_log_init(&ti->ti_log_msgout);
3031 #endif /* SCSI_LOW_DIAGNOSTIC */
3036 scsi_low_msginfunc_disc(struct scsi_low_softc *slp)
3038 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3043 scsi_low_msginfunc_sdp(struct scsi_low_softc *slp)
3045 struct slccb *cb = slp->sl_Qnexus;
3049 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3050 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3053 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3058 scsi_low_msginfunc_rp(struct scsi_low_softc *slp)
3060 if (slp->sl_Qnexus != NULL)
3061 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3063 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3068 scsi_low_synch(struct scsi_low_softc *slp)
3070 struct targ_info *ti = slp->sl_Tnexus;
3071 u_int period = 0, offset = 0, speed;
3075 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3076 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3077 MSGIN_OFFSET(ti) == 0)
3079 if ((offset = MSGIN_OFFSET(ti)) != 0)
3080 period = MSGIN_PERIOD(ti);
3081 s = offset ? "synchronous" : "async";
3086 * Target seems to be brain damaged.
3087 * Force async transfer.
3089 ti->ti_maxsynch.period = 0;
3090 ti->ti_maxsynch.offset = 0;
3091 kprintf("%s: target brain damaged. async transfer\n",
3096 ti->ti_maxsynch.period = period;
3097 ti->ti_maxsynch.offset = offset;
3099 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3103 * Current period and offset are not acceptable
3105 * The adapter changes max synch and max offset.
3107 kprintf("%s: synch neg failed. retry synch msg neg ...\n",
3112 ti->ti_osynch = ti->ti_maxsynch;
3115 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3119 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3121 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3122 struct slccb *cb = slp->sl_Qnexus;
3124 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3126 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3128 kprintf("%s(%d:*): <%s> offset %d period %dns ",
3129 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3133 speed = 1000 * 10 / (period * 4);
3134 kprintf("%d.%d M/s", speed / 10, speed % 10);
3142 scsi_low_wide(struct scsi_low_softc *slp)
3144 struct targ_info *ti = slp->sl_Tnexus;
3147 ti->ti_width = MSGIN_WIDTHP(ti);
3148 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3152 * Current width is not acceptable for our adapter.
3153 * The adapter changes max width.
3155 kprintf("%s: wide neg failed. retry wide msg neg ...\n",
3160 ti->ti_owidth = ti->ti_width;
3161 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3163 ti->ti_setup_msg_done |=
3164 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3168 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3170 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3171 struct slccb *cb = slp->sl_Qnexus;
3173 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3175 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3177 kprintf("%s(%d:*): transfer width %d bits\n",
3178 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3184 scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *slp)
3186 struct targ_info *ti = slp->sl_Tnexus;
3187 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3189 if (slp->sl_Qnexus != NULL)
3191 if (slp->sl_Qnexus->ccb_tag != etag)
3193 slp->sl_error |= FATALIO;
3194 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3195 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3198 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3200 #ifdef SCSI_LOW_DEBUG
3201 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3203 #endif /* SCSI_LOW_DEBUG */
3205 slp->sl_error |= FATALIO;
3206 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3207 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3213 scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *slp)
3215 struct targ_info *ti = slp->sl_Tnexus;
3216 struct slccb *cb = slp->sl_Qnexus;
3217 int res = (int) ti->ti_msgin[1];
3219 if (cb == NULL || res <= 0 ||
3220 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3221 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3224 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3227 slp->sl_scp.scp_datalen += res;
3228 slp->sl_scp.scp_data -= res;
3229 scsi_low_data_finish(slp);
3234 scsi_low_msginfunc_ext(struct scsi_low_softc *slp)
3236 struct slccb *cb = slp->sl_Qnexus;
3237 struct lun_info *li = slp->sl_Lnexus;
3238 struct targ_info *ti = slp->sl_Tnexus;
3242 if (ti->ti_msginptr == 2)
3244 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3248 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3250 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3254 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3255 count = (int) htonl((long) (*ptr));
3256 if(slp->sl_scp.scp_datalen - count < 0 ||
3257 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3260 slp->sl_scp.scp_datalen -= count;
3261 slp->sl_scp.scp_data += count;
3264 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3268 retry = scsi_low_synch(slp);
3269 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3270 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3272 #ifdef SCSI_LOW_DEBUG
3273 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3275 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3277 #endif /* SCSI_LOW_DEBUG */
3280 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3284 retry = scsi_low_wide(slp);
3285 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3286 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3294 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3299 scsi_low_msginfunc_parity(struct scsi_low_softc *slp)
3301 struct targ_info *ti = slp->sl_Tnexus;
3303 /* only I -> T, invalid! */
3304 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3309 scsi_low_msginfunc_msg_reject(struct scsi_low_softc *slp)
3311 struct targ_info *ti = slp->sl_Tnexus;
3312 struct scsi_low_msgout_data *mdp;
3315 if (ti->ti_emsgflags != 0)
3317 kprintf("%s: msg flags [0x%x] rejected\n",
3318 slp->sl_xname, ti->ti_emsgflags);
3319 msgflags = SCSI_LOW_MSG_REJECT;
3320 mdp = &scsi_low_msgout_data[0];
3321 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3323 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3325 ti->ti_emsgflags &= ~mdp->md_flags;
3326 if (mdp->md_errfunc != NULL)
3327 (*mdp->md_errfunc) (slp, msgflags);
3335 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3336 slp->sl_error |= MSGERR;
3342 scsi_low_msgin(struct scsi_low_softc *slp, struct targ_info *ti, u_int c)
3344 struct scsi_low_msgin_data *sdp;
3345 struct lun_info *li;
3348 #ifdef SCSI_LOW_DIAGNOSTIC
3349 if (ti != slp->sl_Tnexus)
3351 scsi_low_print(slp, NULL);
3352 panic("scsi_low_msgin: Target nexus inconsistent");
3354 #endif /* SCSI_LOW_DIAGNOSTIC */
3357 * Phase changes, clear the pointer.
3359 if (ti->ti_ophase != ti->ti_phase)
3362 ti->ti_msgin_parity_error = 0;
3364 slp->sl_ph_count ++;
3365 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3367 kprintf("%s: too many phase changes\n", slp->sl_xname);
3368 slp->sl_error |= FATALIO;
3369 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3374 * Store a current messages byte into buffer and
3375 * wait for the completion of the current msg.
3377 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3378 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3380 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3381 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3385 * Check parity errors.
3387 if ((c & SCSI_LOW_DATA_PE) != 0)
3389 ti->ti_msgin_parity_error ++;
3390 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3394 if (ti->ti_msgin_parity_error != 0)
3398 * Calculate messages length.
3400 msg = ti->ti_msgin[0];
3401 if (msg < MSGIN_DATA_LAST)
3402 sdp = &scsi_low_msgin_data[msg];
3404 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3406 if (ti->ti_msginlen == 0)
3408 ti->ti_msginlen = sdp->md_len;
3414 if (ti->ti_msginptr < ti->ti_msginlen)
3420 if ((msg & MSG_IDENTIFY) == 0)
3422 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3427 li = slp->sl_Lnexus;
3430 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3433 slp->sl_Lnexus = li;
3434 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3438 if (MSGCMD_LUN(msg) != li->li_lun)
3442 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3444 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3446 #ifdef SCSI_LOW_DEBUG
3447 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3451 #endif /* SCSI_LOW_DEBUG */
3459 * Msg process completed, reset msgin pointer and assert ATN if desired.
3462 slp->sl_error |= FATALIO;
3463 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3464 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3467 if (ti->ti_msginptr < ti->ti_msginlen)
3470 #ifdef SCSI_LOW_DIAGNOSTIC
3471 scsi_low_msg_log_write(&ti->ti_log_msgin,
3472 &ti->ti_msgin[0], ti->ti_msginlen);
3473 #endif /* SCSI_LOW_DIAGNOSTIC */
3479 /**********************************************************
3481 **********************************************************/
3483 scsi_low_disconnected(struct scsi_low_softc *slp, struct targ_info *ti)
3485 struct slccb *cb = slp->sl_Qnexus;
3487 /* check phase completion */
3488 switch (slp->sl_msgphase)
3491 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3492 scsi_low_msginfunc_cc(slp);
3493 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3497 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3498 scsi_low_msginfunc_cc(slp);
3499 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3503 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3504 scsi_low_msginfunc_cc(slp);
3510 struct lun_info *li;
3513 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3514 cb->ccb_flags |= CCB_DISCQ;
3515 cb->ccb_error |= slp->sl_error;
3521 #ifdef SCSI_LOW_STATICS
3522 scsi_low_statics.nexus_disconnected ++;
3523 #endif /* SCSI_LOW_STATICS */
3525 #ifdef SCSI_LOW_DEBUG
3526 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3528 kprintf("## SCSI_LOW_DISCONNECTED ===============\n");
3529 scsi_low_print(slp, NULL);
3531 #endif /* SCSI_LOW_DEBUG */
3535 slp->sl_error |= FATALIO;
3536 if (ti->ti_phase == PH_SELSTART)
3537 slp->sl_error |= SELTIMEOUTIO;
3539 slp->sl_error |= UBFERR;
3548 #ifdef SCSI_LOW_DEBUG
3549 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3551 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3552 (cb->ccb_msgoutflag != 0 ||
3553 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3555 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3558 #endif /* SCSI_LOW_DEBUG */
3560 cb->ccb_error |= slp->sl_error;
3561 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3563 cb->ccb_flags |= CCB_STARTQ;
3564 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3569 scsi_low_bus_release(slp, ti);
3570 scsi_low_start(slp);
3574 /**********************************************************
3576 **********************************************************/
3578 scsi_low_alloc_qtag(struct slccb *cb)
3580 struct lun_info *li = cb->li;
3581 scsi_low_tag_t etag;
3583 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3586 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3587 etag = ffs(li->li_qtagbits);
3591 li->li_qtagbits &= ~(1 << (etag - 1));
3592 cb->ccb_otag = etag;
3595 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3596 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3597 if (li->li_qtagarray[li->li_qd] == 0)
3600 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3601 if (li->li_qtagarray[li->li_qd] == 0)
3607 li->li_qtagarray[li->li_qd] ++;
3608 cb->ccb_otag = (li->li_qd ++);
3610 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3614 scsi_low_dealloc_qtag(struct slccb *cb)
3616 struct lun_info *li = cb->li;
3617 scsi_low_tag_t etag;
3619 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3622 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3623 etag = cb->ccb_otag - 1;
3624 #ifdef SCSI_LOW_DIAGNOSTIC
3625 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3626 panic("scsi_low_dealloc_tag: illegal tag");
3627 #endif /* SCSI_LOW_DIAGNOSTIC */
3628 li->li_qtagbits |= (1 << etag);
3630 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3631 etag = cb->ccb_otag;
3632 #ifdef SCSI_LOW_DIAGNOSTIC
3633 if (etag >= SCSI_LOW_MAXNEXUS)
3634 panic("scsi_low_dealloc_tag: illegal tag");
3635 #endif /* SCSI_LOW_DIAGNOSTIC */
3636 li->li_qtagarray[etag] --;
3637 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3639 cb->ccb_otag = SCSI_LOW_UNKTAG;
3643 static struct slccb *
3644 scsi_low_revoke_ccb(struct scsi_low_softc *slp, struct slccb *cb, int fdone)
3646 struct targ_info *ti = cb->ti;
3647 struct lun_info *li = cb->li;
3649 #ifdef SCSI_LOW_DIAGNOSTIC
3650 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3651 (CCB_STARTQ | CCB_DISCQ))
3653 panic("%s: ccb in both queue", slp->sl_xname);
3655 #endif /* SCSI_LOW_DIAGNOSTIC */
3657 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3659 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3662 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3664 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3670 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3671 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3674 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3675 (cb->ccb_flags & CCB_NORETRY) != 0))
3677 cb->ccb_error |= FATALIO;
3678 cb->ccb_flags &= ~CCB_AUTOSENSE;
3679 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3680 panic("%s: done ccb retried", slp->sl_xname);
3685 cb->ccb_error |= PENDINGIO;
3686 scsi_low_deactivate_qtag(cb);
3687 scsi_low_ccb_message_retry(cb);
3688 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3694 scsi_low_reset_nexus_lun(struct scsi_low_softc *slp, struct lun_info *li, int fdone)
3696 struct slccb *cb, *ncb, *ecb;
3702 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3704 ncb = TAILQ_NEXT(cb, ccb_chain);
3705 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3709 * presumely keep ordering of io
3711 cb->ccb_flags |= CCB_STARTQ;
3714 TAILQ_INSERT_HEAD(&slp->sl_start,\
3719 TAILQ_INSERT_AFTER(&slp->sl_start,\
3720 ecb, cb, ccb_chain);
3727 /**************************************************************
3729 **************************************************************/
3731 scsi_low_calcf_lun(struct lun_info *li)
3733 struct targ_info *ti = li->li_ti;
3734 struct scsi_low_softc *slp = ti->ti_sc;
3735 u_int cfgflags, diskflags;
3737 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3738 cfgflags = li->li_cfgflags;
3742 diskflags = li->li_diskflags & li->li_quirks;
3745 li->li_flags &= ~SCSI_LOW_DISC;
3746 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3747 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3748 (cfgflags & SCSI_LOW_DISC) != 0)
3749 li->li_flags |= SCSI_LOW_DISC;
3752 li->li_flags |= SCSI_LOW_NOPARITY;
3753 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3754 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3755 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3756 li->li_flags &= ~SCSI_LOW_NOPARITY;
3759 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3760 (cfgflags & SCSI_LOW_QTAG) != 0 &&
3761 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3763 li->li_flags |= SCSI_LOW_QTAG;
3764 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3765 li->li_maxnqio = li->li_maxnexus;
3769 li->li_flags &= ~SCSI_LOW_QTAG;
3770 li->li_maxnexus = 0;
3771 li->li_maxnqio = li->li_maxnexus;
3775 li->li_flags &= ~SCSI_LOW_LINK;
3776 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3777 (diskflags & SCSI_LOW_DISK_LINK) != 0)
3778 li->li_flags |= SCSI_LOW_LINK;
3780 /* compatible flags */
3781 li->li_flags &= ~SCSI_LOW_SYNC;
3782 if (ti->ti_maxsynch.offset > 0)
3783 li->li_flags |= SCSI_LOW_SYNC;
3785 #ifdef SCSI_LOW_DEBUG
3786 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3788 scsi_low_calcf_show(li);
3790 #endif /* SCSI_LOW_DEBUG */
3794 scsi_low_calcf_target(struct targ_info *ti)
3796 struct scsi_low_softc *slp = ti->ti_sc;
3797 u_int offset, period, diskflags;
3799 diskflags = ti->ti_diskflags & ti->ti_quirks;
3802 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3803 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3805 offset = ti->ti_maxsynch.offset;
3806 period = ti->ti_maxsynch.period;
3807 if (offset == 0 || period == 0)
3808 offset = period = 0;
3812 offset = period = 0;
3815 ti->ti_maxsynch.offset = offset;
3816 ti->ti_maxsynch.period = period;
3819 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3820 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3821 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3823 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3824 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3825 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3827 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3829 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3830 ti->ti_maxsynch.period != ti->ti_osynch.period)
3831 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3832 if (ti->ti_width != ti->ti_owidth)
3833 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3835 ti->ti_osynch = ti->ti_maxsynch;
3836 ti->ti_owidth = ti->ti_width;
3839 #ifdef SCSI_LOW_DEBUG
3840 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3842 kprintf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
3843 slp->sl_xname, ti->ti_id,
3844 ti->ti_maxsynch.period * 4,
3845 ti->ti_maxsynch.offset,
3848 #endif /* SCSI_LOW_DEBUG */
3852 scsi_low_calcf_show(struct lun_info *li)
3854 struct targ_info *ti = li->li_ti;
3855 struct scsi_low_softc *slp = ti->ti_sc;
3857 kprintf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
3858 slp->sl_xname, ti->ti_id, li->li_lun,
3859 ti->ti_maxsynch.period * 4,
3860 ti->ti_maxsynch.offset,
3862 li->li_flags, SCSI_LOW_BITS);
3865 #ifdef SCSI_LOW_START_UP_CHECK
3866 /**************************************************************
3867 * scsi world start up
3868 **************************************************************/
3869 static int scsi_low_poll (struct scsi_low_softc *, struct slccb *);
3872 scsi_low_start_up(struct scsi_low_softc *slp)
3874 struct targ_info *ti;
3875 struct lun_info *li;
3879 kprintf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
3881 for (target = 0; target < slp->sl_ntargs; target ++)
3883 if (target == slp->sl_hostid)
3885 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3887 kprintf("%s: scsi_low: target %d (host card)\n",
3888 slp->sl_xname, target);
3893 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3895 kprintf("%s: scsi_low: target %d lun ",
3896 slp->sl_xname, target);
3899 ti = slp->sl_ti[target];
3900 for (lun = 0; lun < slp->sl_nluns; lun ++)
3902 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
3908 li = scsi_low_alloc_li(ti, lun, 1);
3910 scsi_low_enqueue(slp, ti, li, cb,
3911 CCB_AUTOSENSE | CCB_POLLED, 0);
3913 scsi_low_poll(slp, cb);
3915 if (li->li_state != SCSI_LOW_LUN_OK)
3918 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3920 kprintf("%d ", lun);
3924 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3933 scsi_low_poll(struct scsi_low_softc *slp, struct slccb *cb)
3938 while (slp->sl_nio > 0)
3940 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
3942 (*slp->sl_funcs->scsi_low_poll) (slp);
3943 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
3947 scsi_low_timeout_check(slp);
3952 #endif /* SCSI_LOW_START_UP_CHECK */
3954 /**********************************************************
3956 **********************************************************/
3957 #ifdef SCSI_LOW_DEBUG
3959 scsi_low_test_abort(struct scsi_low_softc *slp, struct targ_info *ti,
3960 struct lun_info *li)
3964 if (li->li_disc > 1)
3966 acb = TAILQ_FIRST(&li->li_discq);
3967 if (scsi_low_abort_ccb(slp, acb) == 0)
3969 kprintf("%s: aborting ccb(0x%lx) start\n",
3970 slp->sl_xname, (u_long) acb);
3976 scsi_low_test_atten(struct scsi_low_softc *slp, struct targ_info *ti, u_int msg)
3978 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
3979 scsi_low_assert_msg(slp, ti, msg, 0);
3981 kprintf("%s: atten check OK\n", slp->sl_xname);
3985 scsi_low_test_cmdlnk(struct scsi_low_softc *slp, struct slccb *cb)
3987 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
3989 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
3992 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
3993 slp->sl_scp.scp_cmdlen);
3994 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
3995 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
3997 #endif /* SCSI_LOW_DEBUG */
4000 scsi_low_info(struct scsi_low_softc *slp, struct targ_info *ti, u_char *s)
4003 slp = LIST_FIRST(&sl_tab);
4007 kprintf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4010 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
4012 scsi_low_print(slp, ti);
4017 scsi_low_print(slp, ti);
4021 static u_char *phase[] =
4023 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4024 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4028 scsi_low_print(struct scsi_low_softc *slp, struct targ_info *ti)
4030 struct lun_info *li;
4034 if (ti == NULL || ti == slp->sl_Tnexus)
4036 ti = slp->sl_Tnexus;
4037 li = slp->sl_Lnexus;
4038 cb = slp->sl_Qnexus;
4042 li = LIST_FIRST(&ti->ti_litab);
4043 cb = TAILQ_FIRST(&li->li_discq);
4047 kprintf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4048 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4054 u_int flags = 0, maxnqio = 0, nqio = 0;
4060 flags = li->li_flags;
4061 maxnqio = li->li_maxnqio;
4065 kprintf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4067 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4068 phase[(int) ti->ti_phase], ti->ti_disc,
4073 kprintf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4074 (u_int) cb->ccb_scp.scp_cmd[0],
4075 cb->ccb_scp.scp_cmdlen,
4077 cb->ccb_scp.scp_datalen,
4078 (u_int) cb->ccb_sscp.scp_status,
4079 cb->ccb_error, SCSI_LOW_ERRORBITS);
4082 kprintf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4083 (u_int) (ti->ti_msginptr),
4084 (u_int) (ti->ti_msgin[0]),
4085 (u_int) (ti->ti_msgin[1]),
4086 (u_int) (ti->ti_msgin[2]),
4087 (u_int) (ti->ti_msgin[3]),
4088 (u_int) (ti->ti_msgin[4]),
4091 kprintf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4092 (u_int) ti->ti_msgflags,
4093 (u_int) (ti->ti_msgoutstr[0]),
4094 (u_int) (ti->ti_msgoutstr[1]),
4095 (u_int) (ti->ti_msgoutstr[2]),
4096 (u_int) (ti->ti_msgoutstr[3]),
4097 (u_int) (ti->ti_msgoutstr[4]),
4099 flags, SCSI_LOW_BITS);
4101 #ifdef SCSI_LOW_DIAGNOSTIC
4102 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4103 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4104 #endif /* SCSI_LOW_DIAGNOSTIC */
4108 kprintf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4109 (u_long) sp->scp_data,
4111 (u_int) sp->scp_status,
4112 slp->sl_error, SCSI_LOW_ERRORBITS);