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.12 2005/05/28 01:16:30 swildner 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>
74 #include <bus/cam/cam.h>
75 #include <bus/cam/cam_ccb.h>
76 #include <bus/cam/cam_sim.h>
77 #include <bus/cam/cam_debug.h>
78 #include <bus/cam/cam_periph.h>
80 #include <bus/cam/scsi/scsi_all.h>
81 #include <bus/cam/scsi/scsi_message.h>
82 #include <bus/cam/scsi/scsi_low.h>
84 /**************************************************************
86 **************************************************************/
87 #define SCSI_LOW_POLL_HZ 1000
89 /* functions return values */
90 #define SCSI_LOW_START_NO_QTAG 0
91 #define SCSI_LOW_START_QTAG 1
93 #define SCSI_LOW_DONE_COMPLETE 0
94 #define SCSI_LOW_DONE_RETRY 1
96 /* internal disk flags */
97 #define SCSI_LOW_DISK_DISC 0x00000001
98 #define SCSI_LOW_DISK_QTAG 0x00000002
99 #define SCSI_LOW_DISK_LINK 0x00000004
100 #define SCSI_LOW_DISK_PARITY 0x00000008
101 #define SCSI_LOW_DISK_SYNC 0x00010000
102 #define SCSI_LOW_DISK_WIDE_16 0x00020000
103 #define SCSI_LOW_DISK_WIDE_32 0x00040000
104 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
105 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
106 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
108 /**************************************************************
110 **************************************************************/
111 /* static */ void scsi_low_info (struct scsi_low_softc *, struct targ_info *, u_char *);
112 static void scsi_low_engage (void *);
113 static struct slccb *scsi_low_establish_ccb (struct targ_info *, struct lun_info *, scsi_low_tag_t);
114 static int scsi_low_done (struct scsi_low_softc *, struct slccb *);
115 static int scsi_low_setup_done (struct scsi_low_softc *, struct slccb *);
116 static void scsi_low_bus_release (struct scsi_low_softc *, struct targ_info *);
117 static void scsi_low_twiddle_wait (void);
118 static struct lun_info *scsi_low_alloc_li (struct targ_info *, int, int);
119 static struct targ_info *scsi_low_alloc_ti (struct scsi_low_softc *, int);
120 static void scsi_low_calcf_lun (struct lun_info *);
121 static void scsi_low_calcf_target (struct targ_info *);
122 static void scsi_low_calcf_show (struct lun_info *);
123 static void scsi_low_reset_nexus (struct scsi_low_softc *, int);
124 static void scsi_low_reset_nexus_target (struct scsi_low_softc *, struct targ_info *, int);
125 static void scsi_low_reset_nexus_lun (struct scsi_low_softc *, struct lun_info *, int);
126 static int scsi_low_init (struct scsi_low_softc *, u_int);
127 static void scsi_low_start (struct scsi_low_softc *);
128 static void scsi_low_free_ti (struct scsi_low_softc *);
130 static int scsi_low_alloc_qtag (struct slccb *);
131 static int scsi_low_dealloc_qtag (struct slccb *);
132 static int scsi_low_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
133 static int scsi_low_message_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
134 static void scsi_low_unit_ready_cmd (struct slccb *);
135 static void scsi_low_timeout (void *);
136 static int scsi_low_timeout_check (struct scsi_low_softc *);
137 #ifdef SCSI_LOW_START_UP_CHECK
138 static int scsi_low_start_up (struct scsi_low_softc *);
139 #endif /* SCSI_LOW_START_UP_CHECK */
140 static int scsi_low_abort_ccb (struct scsi_low_softc *, struct slccb *);
141 static struct slccb *scsi_low_revoke_ccb (struct scsi_low_softc *, struct slccb *, int);
143 int scsi_low_version_major = 2;
144 int scsi_low_version_minor = 17;
146 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
148 /**************************************************************
149 * Debug, Run test and Statics
150 **************************************************************/
151 #ifdef SCSI_LOW_INFO_DETAIL
152 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
153 #else /* !SCSI_LOW_INFO_DETAIL */
154 #define SCSI_LOW_INFO(slp, ti, s) printf("%s: %s\n", (slp)->sl_xname, (s))
155 #endif /* !SCSI_LOW_INFO_DETAIL */
157 #ifdef SCSI_LOW_STATICS
158 struct scsi_low_statics {
161 int nexus_disconnected;
162 int nexus_reselected;
165 #endif /* SCSI_LOW_STATICS */
167 #ifdef SCSI_LOW_DEBUG
168 #define SCSI_LOW_DEBUG_DONE 0x00001
169 #define SCSI_LOW_DEBUG_DISC 0x00002
170 #define SCSI_LOW_DEBUG_SENSE 0x00004
171 #define SCSI_LOW_DEBUG_CALCF 0x00008
172 #define SCSI_LOW_DEBUG_ACTION 0x10000
173 int scsi_low_debug = 0;
175 #define SCSI_LOW_MAX_ATTEN_CHECK 32
176 #define SCSI_LOW_ATTEN_CHECK 0x0001
177 #define SCSI_LOW_CMDLNK_CHECK 0x0002
178 #define SCSI_LOW_ABORT_CHECK 0x0004
179 #define SCSI_LOW_NEXUS_CHECK 0x0008
180 int scsi_low_test = 0;
181 int scsi_low_test_id = 0;
183 static void scsi_low_test_abort (struct scsi_low_softc *, struct targ_info *, struct lun_info *);
184 static void scsi_low_test_cmdlnk (struct scsi_low_softc *, struct slccb *);
185 static void scsi_low_test_atten (struct scsi_low_softc *, struct targ_info *, u_int);
186 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
187 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
188 #define SCSI_LOW_DEBUG_GO(fl, id) \
189 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
190 #endif /* SCSI_LOW_DEBUG */
192 /**************************************************************
194 **************************************************************/
195 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
196 GENERIC_CCB(scsi_low, slccb, ccb_chain)
198 /**************************************************************
200 **************************************************************/
201 #define SCSI_LOW_INLINE static __inline
202 SCSI_LOW_INLINE void scsi_low_activate_qtag (struct slccb *);
203 SCSI_LOW_INLINE void scsi_low_deactivate_qtag (struct slccb *);
204 SCSI_LOW_INLINE void scsi_low_ccb_message_assert (struct slccb *, u_int);
205 SCSI_LOW_INLINE void scsi_low_ccb_message_exec (struct scsi_low_softc *, struct slccb *);
206 SCSI_LOW_INLINE void scsi_low_ccb_message_retry (struct slccb *);
207 SCSI_LOW_INLINE void scsi_low_ccb_message_clear (struct slccb *);
208 SCSI_LOW_INLINE void scsi_low_init_msgsys (struct scsi_low_softc *, struct targ_info *);
211 scsi_low_activate_qtag(cb)
214 struct lun_info *li = cb->li;
216 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
220 cb->ccb_tag = cb->ccb_otag;
224 scsi_low_deactivate_qtag(cb)
227 struct lun_info *li = cb->li;
229 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
233 cb->ccb_tag = SCSI_LOW_UNKTAG;
237 scsi_low_ccb_message_exec(slp, cb)
238 struct scsi_low_softc *slp;
242 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
243 cb->ccb_msgoutflag = 0;
247 scsi_low_ccb_message_assert(cb, msg)
252 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
256 scsi_low_ccb_message_retry(cb)
259 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
263 scsi_low_ccb_message_clear(cb)
266 cb->ccb_msgoutflag = 0;
270 scsi_low_init_msgsys(slp, ti)
271 struct scsi_low_softc *slp;
272 struct targ_info *ti;
276 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
277 SCSI_LOW_DEASSERT_ATN(slp);
278 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
281 /*=============================================================
282 * START OF OS switch (All OS depend fucntions should be here)
283 =============================================================*/
284 /* common os depend utitlities */
285 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
286 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
287 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
289 static u_int8_t scsi_low_cmd_flags[256] = {
290 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
291 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
292 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
293 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
294 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
297 struct scsi_low_error_code {
302 static struct slccb *scsi_low_find_ccb (struct scsi_low_softc *, u_int, u_int, void *);
303 static int scsi_low_translate_error_code (struct slccb *, struct scsi_low_error_code *);
305 static struct slccb *
306 scsi_low_find_ccb(slp, target, lun, osdep)
307 struct scsi_low_softc *slp;
311 struct targ_info *ti;
315 ti = slp->sl_ti[target];
316 li = scsi_low_alloc_li(ti, lun, 0);
320 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
323 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
324 cb = TAILQ_NEXT(cb, ccb_chain))
326 if (cb->osdep == osdep)
330 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL;
331 cb = TAILQ_NEXT(cb, ccb_chain))
333 if (cb->osdep == osdep)
340 scsi_low_translate_error_code(cb, tp)
342 struct scsi_low_error_code *tp;
345 if (cb->ccb_error == 0)
346 return tp->error_code;
348 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
350 return tp->error_code;
353 /**************************************************************
354 * SCSI INTERFACE (CAM)
355 **************************************************************/
356 #define SCSI_LOW_MALLOC(size) malloc((size), M_DEVBUF, M_INTWAIT)
357 #define SCSI_LOW_FREE(pt) free((pt), M_DEVBUF)
358 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
360 static void scsi_low_poll_cam (struct cam_sim *);
361 static void scsi_low_cam_rescan_callback (struct cam_periph *, union ccb *);
362 static void scsi_low_rescan_bus_cam (struct scsi_low_softc *);
363 void scsi_low_scsi_action_cam (struct cam_sim *, union ccb *);
365 static int scsi_low_attach_cam (struct scsi_low_softc *);
366 static int scsi_low_world_start_cam (struct scsi_low_softc *);
367 static int scsi_low_dettach_cam (struct scsi_low_softc *);
368 static int scsi_low_ccb_setup_cam (struct scsi_low_softc *, struct slccb *);
369 static int scsi_low_done_cam (struct scsi_low_softc *, struct slccb *);
370 static void scsi_low_timeout_cam (struct scsi_low_softc *, int, int);
372 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
374 scsi_low_world_start_cam,
375 scsi_low_dettach_cam,
376 scsi_low_ccb_setup_cam,
381 struct scsi_low_error_code scsi_low_error_code_cam[] = {
383 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
384 {SENSEERR, CAM_AUTOSENSE_FAIL},
385 {UACAERR, CAM_SCSI_STATUS_ERROR},
386 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
387 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
388 {TIMEOUTIO, CAM_CMD_TIMEOUT},
389 {PDMAERR, CAM_DATA_RUN_ERR},
390 {PARITYERR, CAM_UNCOR_PARITY},
391 {UBFERR, CAM_UNEXP_BUSFREE},
392 {ABORTIO, CAM_REQ_ABORTED},
393 {-1, CAM_UNREC_HBA_ERROR}
396 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
399 * Please check a polling hz, currently we assume scsi_low_poll() is
402 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
405 scsi_low_poll_cam(sim)
408 struct scsi_low_softc *slp = SIM2SLP(sim);
410 (*slp->sl_funcs->scsi_low_poll) (slp);
412 if (slp->sl_si.si_poll_count ++ >=
413 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
415 slp->sl_si.si_poll_count = 0;
416 scsi_low_timeout_check(slp);
421 scsi_low_cam_rescan_callback(periph, ccb)
422 struct cam_periph *periph;
426 xpt_free_path(ccb->ccb_h.path);
431 scsi_low_rescan_bus_cam(slp)
432 struct scsi_low_softc *slp;
434 struct cam_path *path;
435 union ccb *ccb = malloc(sizeof(union ccb), M_DEVBUF, M_INTWAIT | M_ZERO);
438 status = xpt_create_path(&path, xpt_periph,
439 cam_sim_path(slp->sl_si.sim), -1, 0);
440 if (status != CAM_REQ_CMP)
443 xpt_setup_ccb(&ccb->ccb_h, path, 5);
444 ccb->ccb_h.func_code = XPT_SCAN_BUS;
445 ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
446 ccb->crcn.flags = CAM_FLAG_NONE;
451 scsi_low_scsi_action_cam(sim, ccb)
455 struct scsi_low_softc *slp = SIM2SLP(sim);
456 struct targ_info *ti;
459 u_int lun, flags, msg, target;
462 target = (u_int) (ccb->ccb_h.target_id);
463 lun = (u_int) ccb->ccb_h.target_lun;
465 #ifdef SCSI_LOW_DEBUG
466 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
468 printf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
469 slp->sl_xname, ccb->ccb_h.func_code, target, lun);
471 #endif /* SCSI_LOW_DEBUG */
473 switch (ccb->ccb_h.func_code) {
474 case XPT_SCSI_IO: /* Execute the requested I/O operation */
475 #ifdef SCSI_LOW_DIAGNOSTIC
476 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
478 printf("%s: invalid target/lun\n", slp->sl_xname);
479 ccb->ccb_h.status = CAM_REQ_INVALID;
483 #endif /* SCSI_LOW_DIAGNOSTIC */
485 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
486 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
491 ti = slp->sl_ti[target];
494 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
495 flags = CCB_AUTOSENSE | CCB_SCSIIO;
499 s = SCSI_LOW_SPLSCSI();
500 li = scsi_low_alloc_li(ti, lun, 1);
502 if (ti->ti_setup_msg != 0)
504 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
507 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
509 #ifdef SCSI_LOW_DEBUG
510 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
512 scsi_low_test_abort(slp, ti, li);
514 #endif /* SCSI_LOW_DEBUG */
518 case XPT_EN_LUN: /* Enable LUN as a target */
519 case XPT_TARGET_IO: /* Execute target I/O request */
520 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
521 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
523 ccb->ccb_h.status = CAM_REQ_INVALID;
527 case XPT_ABORT: /* Abort the specified CCB */
528 #ifdef SCSI_LOW_DIAGNOSTIC
529 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
531 printf("%s: invalid target/lun\n", slp->sl_xname);
532 ccb->ccb_h.status = CAM_REQ_INVALID;
536 #endif /* SCSI_LOW_DIAGNOSTIC */
538 s = SCSI_LOW_SPLSCSI();
539 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
540 rv = scsi_low_abort_ccb(slp, cb);
544 ccb->ccb_h.status = CAM_REQ_CMP;
546 ccb->ccb_h.status = CAM_REQ_INVALID;
550 case XPT_SET_TRAN_SETTINGS: {
551 struct ccb_trans_settings *cts;
554 #ifdef SCSI_LOW_DIAGNOSTIC
555 if (target == CAM_TARGET_WILDCARD)
557 printf("%s: invalid target\n", slp->sl_xname);
558 ccb->ccb_h.status = CAM_REQ_INVALID;
562 #endif /* SCSI_LOW_DIAGNOSTIC */
564 ti = slp->sl_ti[target];
565 if (lun == CAM_LUN_WILDCARD)
568 s = SCSI_LOW_SPLSCSI();
569 if ((cts->valid & (CCB_TRANS_BUS_WIDTH_VALID |
570 CCB_TRANS_SYNC_RATE_VALID |
571 CCB_TRANS_SYNC_OFFSET_VALID)) != 0)
573 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
574 val = cts->bus_width;
575 if (val < ti->ti_width)
578 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
579 val = cts->sync_period;
580 if (val == 0 || val > ti->ti_maxsynch.period)
581 ti->ti_maxsynch.period = val;
583 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) {
584 val = cts->sync_offset;
585 if (val < ti->ti_maxsynch.offset)
586 ti->ti_maxsynch.offset = val;
589 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
590 scsi_low_calcf_target(ti);
593 if ((cts->valid & (CCB_TRANS_DISC_VALID |
594 CCB_TRANS_TQ_VALID)) != 0)
596 li = scsi_low_alloc_li(ti, lun, 1);
597 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0)
599 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
600 li->li_quirks |= SCSI_LOW_DISK_DISC;
602 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
604 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
606 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
607 li->li_quirks |= SCSI_LOW_DISK_QTAG;
609 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
612 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
613 scsi_low_calcf_target(ti);
614 scsi_low_calcf_lun(li);
615 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
616 scsi_low_calcf_show(li);
620 ccb->ccb_h.status = CAM_REQ_CMP;
625 case XPT_GET_TRAN_SETTINGS: {
626 struct ccb_trans_settings *cts;
630 #ifdef SCSI_LOW_DIAGNOSTIC
631 if (target == CAM_TARGET_WILDCARD)
633 printf("%s: invalid target\n", slp->sl_xname);
634 ccb->ccb_h.status = CAM_REQ_INVALID;
638 #endif /* SCSI_LOW_DIAGNOSTIC */
639 ti = slp->sl_ti[target];
640 if (lun == CAM_LUN_WILDCARD)
643 s = SCSI_LOW_SPLSCSI();
644 li = scsi_low_alloc_li(ti, lun, 1);
645 #ifdef CAM_NEW_TRAN_CODE
646 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
647 struct ccb_trans_settings_scsi *scsi =
648 &cts->proto_specific.scsi;
649 struct ccb_trans_settings_spi *spi =
650 &cts->xport_specific.spi;
651 #ifdef SCSI_LOW_DIAGNOSTIC
652 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
654 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
655 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
659 #endif /* SCSI_LOW_DIAGNOSTIC */
660 cts->protocol = PROTO_SCSI;
661 cts->protocol_version = SCSI_REV_2;
662 cts->transport = XPORT_SPI;
663 cts->transport_version = 2;
665 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
666 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
668 diskflags = li->li_diskflags & li->li_cfgflags;
669 if (diskflags & SCSI_LOW_DISK_DISC)
670 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
671 if (diskflags & SCSI_LOW_DISK_QTAG)
672 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
674 spi->sync_period = ti->ti_maxsynch.period;
675 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
676 spi->sync_offset = ti->ti_maxsynch.offset;
677 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
679 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
680 spi->bus_width = ti->ti_width;
682 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
683 scsi->valid = CTS_SCSI_VALID_TQ;
684 spi->valid |= CTS_SPI_VALID_DISC;
688 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
690 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0)
692 #ifdef SCSI_LOW_DIAGNOSTIC
693 if ((li->li_flags_valid & SCSI_LOW_LUN_FLAGS_DISK_VALID) == 0)
695 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
696 printf("%s: invalid GET_TRANS_USER_SETTINGS call\n",
700 #endif /* SCSI_LOW_DIAGNOSTIC */
701 diskflags = li->li_diskflags & li->li_cfgflags;
702 if ((diskflags & SCSI_LOW_DISK_DISC) != 0)
703 cts->flags |= CCB_TRANS_DISC_ENB;
705 cts->flags &= ~CCB_TRANS_DISC_ENB;
706 if ((diskflags & SCSI_LOW_DISK_QTAG) != 0)
707 cts->flags |= CCB_TRANS_TAG_ENB;
709 cts->flags &= ~CCB_TRANS_TAG_ENB;
711 else if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
713 #ifdef SCSI_LOW_DIAGNOSTIC
714 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
716 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
717 printf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
721 #endif /* SCSI_LOW_DIAGNOSTIC */
722 if ((li->li_flags & SCSI_LOW_DISC) != 0)
723 cts->flags |= CCB_TRANS_DISC_ENB;
725 cts->flags &= ~CCB_TRANS_DISC_ENB;
726 if ((li->li_flags & SCSI_LOW_QTAG) != 0)
727 cts->flags |= CCB_TRANS_TAG_ENB;
729 cts->flags &= ~CCB_TRANS_TAG_ENB;
733 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
737 cts->sync_period = ti->ti_maxsynch.period;
738 cts->sync_offset = ti->ti_maxsynch.offset;
739 cts->bus_width = ti->ti_width;
741 cts->valid = CCB_TRANS_SYNC_RATE_VALID
742 | CCB_TRANS_SYNC_OFFSET_VALID
743 | CCB_TRANS_BUS_WIDTH_VALID
744 | CCB_TRANS_DISC_VALID
745 | CCB_TRANS_TQ_VALID;
746 ccb->ccb_h.status = CAM_REQ_CMP;
754 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
755 cam_calc_geometry(&ccb->ccg, /*extended*/1);
760 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
761 s = SCSI_LOW_SPLSCSI();
762 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
764 ccb->ccb_h.status = CAM_REQ_CMP;
768 case XPT_TERM_IO: /* Terminate the I/O process */
769 ccb->ccb_h.status = CAM_REQ_INVALID;
773 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
774 #ifdef SCSI_LOW_DIAGNOSTIC
775 if (target == CAM_TARGET_WILDCARD)
777 printf("%s: invalid target\n", slp->sl_xname);
778 ccb->ccb_h.status = CAM_REQ_INVALID;
782 #endif /* SCSI_LOW_DIAGNOSTIC */
784 msg = SCSI_LOW_MSG_RESET;
785 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
787 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
792 ti = slp->sl_ti[target];
793 if (lun == CAM_LUN_WILDCARD)
797 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
798 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
800 flags = CCB_NORETRY | CCB_URGENT;
802 s = SCSI_LOW_SPLSCSI();
803 li = scsi_low_alloc_li(ti, lun, 1);
804 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
808 case XPT_PATH_INQ: { /* Path routing inquiry */
809 struct ccb_pathinq *cpi = &ccb->cpi;
811 cpi->version_num = scsi_low_version_major;
812 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
813 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
814 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
815 cpi->hba_inquiry |= PI_WIDE_16;
816 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
817 cpi->hba_inquiry |= PI_WIDE_32;
818 if (ti->ti_maxsynch.offset > 0)
819 cpi->hba_inquiry |= PI_SDTR_ABLE;
820 cpi->target_sprt = 0;
822 cpi->hba_eng_cnt = 0;
823 cpi->max_target = slp->sl_ntargs - 1;
824 cpi->max_lun = slp->sl_nluns - 1;
825 cpi->initiator_id = slp->sl_hostid;
826 cpi->bus_id = cam_sim_bus(sim);
827 cpi->base_transfer_speed = 3300;
828 #ifdef CAM_NEW_TRAN_CODE
829 cpi->transport = XPORT_SPI;
830 cpi->transport_version = 2;
831 cpi->protocol = PROTO_SCSI;
832 cpi->protocol_version = SCSI_REV_2;
834 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
835 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
836 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
837 cpi->unit_number = cam_sim_unit(sim);
838 cpi->ccb_h.status = CAM_REQ_CMP;
844 printf("scsi_low: non support func_code = %d ",
845 ccb->ccb_h.func_code);
846 ccb->ccb_h.status = CAM_REQ_INVALID;
853 scsi_low_attach_cam(slp)
854 struct scsi_low_softc *slp;
856 struct cam_devq *devq;
859 sprintf(slp->sl_xname, "%s%d",
860 DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
862 devq = cam_simq_alloc(SCSI_LOW_NCCB);
867 * ask the adapter what subunits are present
869 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
870 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
872 DEVPORT_DEVNAME(slp->sl_dev), slp,
873 DEVPORT_DEVUNIT(slp->sl_dev),
874 slp->sl_openings, tagged_openings, devq);
875 cam_simq_release(devq);
876 if (slp->sl_si.sim == NULL) {
880 if (xpt_bus_register(slp->sl_si.sim, 0) != CAM_SUCCESS) {
881 cam_sim_free(slp->sl_si.sim);
882 slp->sl_si.sim = NULL;
886 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
887 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
888 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
889 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
890 cam_sim_free(slp->sl_si.sim);
891 slp->sl_si.sim = NULL;
895 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
900 scsi_low_world_start_cam(slp)
901 struct scsi_low_softc *slp;
905 scsi_low_rescan_bus_cam(slp);
910 scsi_low_dettach_cam(slp)
911 struct scsi_low_softc *slp;
914 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
915 xpt_free_path(slp->sl_si.path);
916 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
917 cam_sim_free(slp->sl_si.sim);
918 slp->sl_si.sim = NULL;
923 scsi_low_ccb_setup_cam(slp, cb)
924 struct scsi_low_softc *slp;
927 union ccb *ccb = (union ccb *) cb->osdep;
929 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
931 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
932 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
933 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
934 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
935 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
936 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
937 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
938 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
939 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
943 scsi_low_unit_ready_cmd(cb);
945 return SCSI_LOW_START_QTAG;
949 scsi_low_done_cam(slp, cb)
950 struct scsi_low_softc *slp;
955 ccb = (union ccb *) cb->osdep;
956 if (cb->ccb_error == 0)
958 ccb->ccb_h.status = CAM_REQ_CMP;
963 if (cb->ccb_rcnt >= slp->sl_max_retry)
964 cb->ccb_error |= ABORTIO;
966 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
967 (cb->ccb_error & ABORTIO) == 0)
970 if ((cb->ccb_error & SENSEIO) != 0)
972 memcpy(&ccb->csio.sense_data,
974 sizeof(ccb->csio.sense_data));
977 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
978 &scsi_low_error_code_cam[0]);
980 #ifdef SCSI_LOW_DIAGNOSTIC
981 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
982 cb->ccb_scp.scp_cmdlen > 0 &&
983 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
984 SCSI_LOW_CMD_ABORT_WARNING) != 0)
986 printf("%s: WARNING: scsi_low IO abort\n",
988 scsi_low_print(slp, NULL);
990 #endif /* SCSI_LOW_DIAGNOSTIC */
993 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
994 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
996 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
997 ccb->csio.scsi_status = 0; /* XXX */
999 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
1001 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
1007 scsi_low_timeout_cam(slp, ch, action)
1008 struct scsi_low_softc *slp;
1015 case SCSI_LOW_TIMEOUT_CH_IO:
1018 case SCSI_LOW_TIMEOUT_START:
1019 callout_reset(&slp->sl_si.timeout_ch,
1020 hz / SCSI_LOW_TIMEOUT_HZ, scsi_low_timeout, slp);
1022 case SCSI_LOW_TIMEOUT_STOP:
1023 callout_stop(&slp->sl_si.timeout_ch);
1028 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
1031 case SCSI_LOW_TIMEOUT_START:
1032 callout_reset(&slp->sl_si.engage_ch, 1,
1033 scsi_low_engage, slp);
1035 case SCSI_LOW_TIMEOUT_STOP:
1036 callout_stop(&slp->sl_si.engage_ch);
1040 case SCSI_LOW_TIMEOUT_CH_RECOVER:
1045 /**************************************************************
1046 * scsi low deactivate and activate
1047 **************************************************************/
1049 scsi_low_is_busy(slp)
1050 struct scsi_low_softc *slp;
1053 if (slp->sl_nio > 0)
1059 scsi_low_deactivate(slp)
1060 struct scsi_low_softc *slp;
1064 s = SCSI_LOW_SPLSCSI();
1065 slp->sl_flags |= HW_INACTIVE;
1066 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1067 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
1068 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1069 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1075 scsi_low_activate(slp)
1076 struct scsi_low_softc *slp;
1080 s = SCSI_LOW_SPLSCSI();
1081 slp->sl_flags &= ~HW_INACTIVE;
1082 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
1084 slp->sl_flags |= HW_INACTIVE;
1089 slp->sl_timeout_count = 0;
1090 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1091 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1096 /**************************************************************
1098 **************************************************************/
1099 #ifdef SCSI_LOW_DIAGNOSTIC
1100 static void scsi_low_msg_log_init (struct scsi_low_msg_log *);
1101 static void scsi_low_msg_log_write (struct scsi_low_msg_log *, u_int8_t *,
1103 static void scsi_low_msg_log_show (struct scsi_low_msg_log *, char *, int);
1106 scsi_low_msg_log_init(slmlp)
1107 struct scsi_low_msg_log *slmlp;
1110 slmlp->slml_ptr = 0;
1114 scsi_low_msg_log_write(slmlp, datap, len)
1115 struct scsi_low_msg_log *slmlp;
1121 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1124 ptr = slmlp->slml_ptr ++;
1125 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1126 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1127 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1128 slmlp->slml_msg[ptr].msg[ind] = 0;
1132 scsi_low_msg_log_show(slmlp, s, len)
1133 struct scsi_low_msg_log *slmlp;
1139 printf("%s: (%d) ", s, slmlp->slml_ptr);
1140 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1142 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1145 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1151 #endif /* SCSI_LOW_DIAGNOSTIC */
1153 /**************************************************************
1155 **************************************************************/
1157 scsi_low_engage(arg)
1160 struct scsi_low_softc *slp = arg;
1161 int s = SCSI_LOW_SPLSCSI();
1163 switch (slp->sl_rstep)
1167 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1168 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1169 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1174 slp->sl_flags &= ~HW_RESUME;
1175 scsi_low_start(slp);
1185 scsi_low_init(slp, flags)
1186 struct scsi_low_softc *slp;
1191 slp->sl_flags |= HW_INITIALIZING;
1193 /* clear power control timeout */
1194 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1196 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1197 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1198 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1200 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1203 /* reset current nexus */
1204 scsi_low_reset_nexus(slp, flags);
1205 if ((slp->sl_flags & HW_INACTIVE) != 0)
1211 if (flags != SCSI_LOW_RESTART_SOFT)
1213 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1217 slp->sl_flags &= ~HW_INITIALIZING;
1221 /**************************************************************
1223 **************************************************************/
1224 static struct lun_info *
1225 scsi_low_alloc_li(ti, lun, alloc)
1226 struct targ_info *ti;
1230 struct scsi_low_softc *slp = ti->ti_sc;
1231 struct lun_info *li;
1233 li = LIST_FIRST(&ti->ti_litab);
1236 if (li->li_lun == lun)
1239 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1241 if (li->li_lun == lun)
1243 LIST_REMOVE(li, lun_chain);
1244 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1253 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1255 panic("no lun info mem");
1257 SCSI_LOW_BZERO(li, ti->ti_lunsize);
1261 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1263 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1264 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1265 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1266 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1267 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1269 li->li_qtagbits = (u_int) -1;
1271 TAILQ_INIT(&li->li_discq);
1272 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1274 /* host specific structure initialization per lun */
1275 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1276 (*slp->sl_funcs->scsi_low_lun_init)
1277 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1278 scsi_low_calcf_lun(li);
1282 /**************************************************************
1283 * allocate targ_info
1284 **************************************************************/
1285 static struct targ_info *
1286 scsi_low_alloc_ti(slp, targ)
1287 struct scsi_low_softc *slp;
1290 struct targ_info *ti;
1292 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1293 TAILQ_INIT(&slp->sl_titab);
1295 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1297 panic("%s short of memory", slp->sl_xname);
1299 SCSI_LOW_BZERO(ti, slp->sl_targsize);
1303 slp->sl_ti[targ] = ti;
1304 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1305 LIST_INIT(&ti->ti_litab);
1307 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1308 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1309 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1310 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1311 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1312 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1314 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1316 (*slp->sl_funcs->scsi_low_targ_init)
1317 (slp, ti, SCSI_LOW_INFO_ALLOC);
1319 scsi_low_calcf_target(ti);
1324 scsi_low_free_ti(slp)
1325 struct scsi_low_softc *slp;
1327 struct targ_info *ti, *tib;
1328 struct lun_info *li, *nli;
1330 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1332 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1334 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1336 (*slp->sl_funcs->scsi_low_lun_init)
1337 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1339 nli = LIST_NEXT(li, lun_chain);
1343 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1345 (*slp->sl_funcs->scsi_low_targ_init)
1346 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1348 tib = TAILQ_NEXT(ti, ti_chain);
1353 /**************************************************************
1355 **************************************************************/
1357 scsi_low_bus_idle(slp)
1358 struct scsi_low_softc *slp;
1361 slp->sl_retry_sel = 0;
1362 if (slp->sl_Tnexus == NULL)
1363 scsi_low_start(slp);
1367 scsi_low_timeout(arg)
1370 struct scsi_low_softc *slp = arg;
1373 s = SCSI_LOW_SPLSCSI();
1374 (void) scsi_low_timeout_check(slp);
1375 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1376 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1381 scsi_low_timeout_check(slp)
1382 struct scsi_low_softc *slp;
1384 struct targ_info *ti;
1385 struct lun_info *li;
1386 struct slccb *cb = NULL; /* XXX */
1388 /* selection restart */
1389 if (slp->sl_retry_sel != 0)
1391 slp->sl_retry_sel = 0;
1392 if (slp->sl_Tnexus != NULL)
1395 cb = TAILQ_FIRST(&slp->sl_start);
1399 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1401 cb->ccb_flags |= CCB_NORETRY;
1402 cb->ccb_error |= SELTIMEOUTIO;
1403 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1404 panic("%s: ccb not finished", slp->sl_xname);
1407 if (slp->sl_Tnexus == NULL)
1408 scsi_low_start(slp);
1411 /* call hardware timeout */
1413 if (slp->sl_funcs->scsi_low_timeout != NULL)
1415 (*slp->sl_funcs->scsi_low_timeout) (slp);
1418 if (slp->sl_timeout_count ++ <
1419 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1422 slp->sl_timeout_count = 0;
1423 if (slp->sl_nio > 0)
1425 if ((cb = slp->sl_Qnexus) != NULL)
1427 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1431 else if (slp->sl_disc == 0)
1433 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1436 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1440 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1441 ti = TAILQ_NEXT(ti, ti_chain))
1443 if (ti->ti_disc == 0)
1446 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1447 li = LIST_NEXT(li, lun_chain))
1449 for (cb = TAILQ_FIRST(&li->li_discq);
1451 cb = TAILQ_NEXT(cb, ccb_chain))
1454 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1462 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1464 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1467 if (slp->sl_active != 0)
1469 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1475 if (slp->sl_powc < 0)
1477 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1478 slp->sl_flags |= HW_POWDOWN;
1479 (*slp->sl_funcs->scsi_low_power)
1480 (slp, SCSI_LOW_POWDOWN);
1486 cb->ccb_error |= TIMEOUTIO;
1487 printf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
1488 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1489 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1490 scsi_low_start(slp);
1496 scsi_low_abort_ccb(slp, cb)
1497 struct scsi_low_softc *slp;
1500 struct targ_info *ti;
1501 struct lun_info *li;
1506 if ((cb->ccb_omsgoutflag &
1507 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1512 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1513 msg = SCSI_LOW_MSG_ABORT;
1515 msg = SCSI_LOW_MSG_ABORT_QTAG;
1517 cb->ccb_error |= ABORTIO;
1518 cb->ccb_flags |= CCB_NORETRY;
1519 scsi_low_ccb_message_assert(cb, msg);
1521 if (cb == slp->sl_Qnexus)
1523 scsi_low_assert_msg(slp, ti, msg, 1);
1525 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1527 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1528 panic("%s: revoked ccb done", slp->sl_xname);
1530 cb->ccb_flags |= CCB_STARTQ;
1531 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1533 if (slp->sl_Tnexus == NULL)
1534 scsi_low_start(slp);
1538 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1539 panic("%s: revoked ccb retried", slp->sl_xname);
1544 /**************************************************************
1545 * Generic SCSI INTERFACE
1546 **************************************************************/
1548 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1549 struct scsi_low_softc *slp;
1550 int openings, ntargs, nluns, targsize, lunsize;
1552 struct targ_info *ti;
1553 struct lun_info *li;
1556 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
1558 if (slp->sl_osdep_fp == NULL)
1559 panic("scsi_low: interface not spcified");
1561 if (ntargs > SCSI_LOW_NTARGETS)
1563 printf("scsi_low: %d targets are too large\n", ntargs);
1564 printf("change kernel options SCSI_LOW_NTARGETS");
1569 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1571 slp->sl_openings = openings;
1572 slp->sl_ntargs = ntargs;
1573 slp->sl_nluns = nluns;
1574 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1576 if (lunsize < sizeof(struct lun_info))
1577 lunsize = sizeof(struct lun_info);
1579 if (targsize < sizeof(struct targ_info))
1580 targsize = sizeof(struct targ_info);
1582 slp->sl_targsize = targsize;
1583 for (i = 0; i < ntargs; i ++)
1585 ti = scsi_low_alloc_ti(slp, i);
1586 ti->ti_lunsize = lunsize;
1587 li = scsi_low_alloc_li(ti, 0, 1);
1590 /* initialize queue */
1591 nccb = openings * ntargs;
1592 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1593 nccb = SCSI_LOW_NCCB;
1594 scsi_low_init_ccbque(nccb);
1595 TAILQ_INIT(&slp->sl_start);
1597 /* call os depend attach */
1598 callout_init(&slp->sl_si.timeout_ch);
1599 callout_init(&slp->sl_si.engage_ch);
1601 s = SCSI_LOW_SPLSCSI();
1602 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
1606 printf("%s: scsi_low_attach: osdep attach failed\n",
1611 /* check hardware */
1612 SCSI_LOW_DELAY(1000); /* wait for 1ms */
1613 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1616 printf("%s: scsi_low_attach: initialization failed\n",
1621 /* start watch dog */
1622 slp->sl_timeout_count = 0;
1623 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1624 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1625 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1628 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1630 #ifdef SCSI_LOW_START_UP_CHECK
1631 /* probing devices */
1632 scsi_low_start_up(slp);
1633 #endif /* SCSI_LOW_START_UP_CHECK */
1635 /* call os depend attach done*/
1636 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
1642 scsi_low_dettach(slp)
1643 struct scsi_low_softc *slp;
1647 s = SCSI_LOW_SPLSCSI();
1648 if (scsi_low_is_busy(slp) != 0)
1654 scsi_low_deactivate(slp);
1656 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
1663 scsi_low_free_ti(slp);
1664 LIST_REMOVE(slp, sl_chain);
1669 /**************************************************************
1671 **************************************************************/
1673 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
1674 struct scsi_low_softc *slp;
1675 struct targ_info *ti;
1676 struct lun_info *li;
1684 scsi_low_ccb_message_assert(cb, msg);
1686 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1687 scsi_low_alloc_qtag(cb);
1689 cb->ccb_flags = flags | CCB_STARTQ;
1690 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1691 cb->ccb_error |= PENDINGIO;
1693 if ((flags & CCB_URGENT) != 0)
1695 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1699 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1704 if (slp->sl_Tnexus == NULL)
1705 scsi_low_start(slp);
1710 scsi_low_message_enqueue(slp, ti, li, flags)
1711 struct scsi_low_softc *slp;
1712 struct targ_info *ti;
1713 struct lun_info *li;
1719 tmsgflags = ti->ti_setup_msg;
1720 ti->ti_setup_msg = 0;
1722 flags |= CCB_NORETRY;
1723 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1728 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1732 /**************************************************************
1733 * Generic Start & Done
1734 **************************************************************/
1735 #define SLSC_MODE_SENSE_SHORT 0x1a
1736 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1737 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1738 sizeof(struct scsi_low_mode_sense_data), 0};
1739 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1740 sizeof(struct scsi_low_inq_data), 0};
1741 static u_int8_t unit_ready_cmd[6];
1742 static int scsi_low_setup_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1743 static int scsi_low_sense_abort_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1744 static int scsi_low_resume (struct scsi_low_softc *);
1747 scsi_low_unit_ready_cmd(cb)
1751 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1752 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1753 cb->ccb_scp.scp_datalen = 0;
1754 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1759 scsi_low_sense_abort_start(slp, ti, li, cb)
1760 struct scsi_low_softc *slp;
1761 struct targ_info *ti;
1762 struct lun_info *li;
1766 cb->ccb_scp.scp_cmdlen = 6;
1767 SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1768 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1769 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1770 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1771 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1772 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1773 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1775 scsi_low_ccb_message_clear(cb);
1776 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1778 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1782 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
1783 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1784 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1785 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1788 return SCSI_LOW_START_NO_QTAG;
1792 scsi_low_setup_start(slp, ti, li, cb)
1793 struct scsi_low_softc *slp;
1794 struct targ_info *ti;
1795 struct lun_info *li;
1799 switch(li->li_state)
1801 case SCSI_LOW_LUN_SLEEP:
1802 scsi_low_unit_ready_cmd(cb);
1805 case SCSI_LOW_LUN_START:
1806 cb->ccb_scp.scp_cmd = ss_cmd;
1807 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1808 cb->ccb_scp.scp_datalen = 0;
1809 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1813 case SCSI_LOW_LUN_INQ:
1814 cb->ccb_scp.scp_cmd = inq_cmd;
1815 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1816 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1817 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1818 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1822 case SCSI_LOW_LUN_MODEQ:
1823 cb->ccb_scp.scp_cmd = sms_cmd;
1824 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1825 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1826 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1827 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1829 return SCSI_LOW_START_QTAG;
1832 panic("%s: no setup phase", slp->sl_xname);
1835 return SCSI_LOW_START_NO_QTAG;
1839 scsi_low_resume(slp)
1840 struct scsi_low_softc *slp;
1843 if (slp->sl_flags & HW_RESUME)
1845 slp->sl_flags &= ~HW_POWDOWN;
1846 if (slp->sl_funcs->scsi_low_power != NULL)
1848 slp->sl_flags |= HW_RESUME;
1850 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1851 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1852 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
1853 SCSI_LOW_TIMEOUT_START);
1861 struct scsi_low_softc *slp;
1863 struct targ_info *ti;
1864 struct lun_info *li;
1868 /* check hardware exists or under initializations ? */
1869 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1872 /* check hardware power up ? */
1873 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1876 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1878 if (scsi_low_resume(slp) == EJUSTRETURN)
1884 #ifdef SCSI_LOW_DIAGNOSTIC
1885 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1887 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
1888 panic("%s: inconsistent", slp->sl_xname);
1890 #endif /* SCSI_LOW_DIAGNOSTIC */
1892 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
1893 cb = TAILQ_NEXT(cb, ccb_chain))
1897 if (li->li_disc == 0)
1899 goto scsi_low_cmd_start;
1901 else if (li->li_nqio > 0)
1903 if (li->li_nqio < li->li_maxnqio ||
1904 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1905 goto scsi_low_cmd_start;
1911 cb->ccb_flags &= ~CCB_STARTQ;
1912 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1915 /* clear all error flag bits (for restart) */
1917 cb->ccb_datalen = -1;
1918 cb->ccb_scp.scp_status = ST_UNKNOWN;
1920 /* setup nexus pointer */
1921 slp->sl_Qnexus = cb;
1922 slp->sl_Lnexus = li;
1923 slp->sl_Tnexus = ti;
1925 /* initialize msgsys */
1926 scsi_low_init_msgsys(slp, ti);
1929 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1931 /* CA state or forced abort */
1932 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1934 else if (li->li_state >= SCSI_LOW_LUN_OK)
1936 cb->ccb_flags &= ~CCB_INTERNAL;
1937 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
1938 if (cb->ccb_msgoutflag != 0)
1940 scsi_low_ccb_message_exec(slp, cb);
1945 cb->ccb_flags |= CCB_INTERNAL;
1946 rv = scsi_low_setup_start(slp, ti, li, cb);
1950 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1952 if (rv == SCSI_LOW_START_QTAG &&
1953 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1958 scsi_low_activate_qtag(cb);
1959 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1960 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1961 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1962 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1963 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1965 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1966 scsi_low_assert_msg(slp, ti, qmsg, 0);
1970 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1971 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1972 cb->ccb_tc = cb->ccb_tcmax;
1974 /* setup saved scsi data pointer */
1975 cb->ccb_sscp = cb->ccb_scp;
1977 /* setup current scsi pointer */
1978 slp->sl_scp = cb->ccb_sscp;
1979 slp->sl_error = cb->ccb_error;
1981 /* assert always an identify msg */
1982 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1985 #ifdef SCSI_LOW_DIAGNOSTIC
1986 scsi_low_msg_log_init(&ti->ti_log_msgin);
1987 scsi_low_msg_log_init(&ti->ti_log_msgout);
1988 #endif /* SCSI_LOW_DIAGNOSTIC */
1990 /* selection start */
1992 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1993 if (rv == SCSI_LOW_START_OK)
1995 #ifdef SCSI_LOW_STATICS
1996 scsi_low_statics.nexus_win ++;
1997 #endif /* SCSI_LOW_STATICS */
2001 scsi_low_arbit_fail(slp, cb);
2002 #ifdef SCSI_LOW_STATICS
2003 scsi_low_statics.nexus_fail ++;
2004 #endif /* SCSI_LOW_STATICS */
2008 scsi_low_arbit_fail(slp, cb)
2009 struct scsi_low_softc *slp;
2012 struct targ_info *ti = cb->ti;
2014 scsi_low_deactivate_qtag(cb);
2015 scsi_low_ccb_message_retry(cb);
2016 cb->ccb_flags |= CCB_STARTQ;
2017 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
2019 scsi_low_bus_release(slp, ti);
2022 if (slp->sl_disc == 0)
2024 #ifdef SCSI_LOW_DIAGNOSTIC
2025 printf("%s: try selection again\n", slp->sl_xname);
2026 #endif /* SCSI_LOW_DIAGNOSTIC */
2027 slp->sl_retry_sel = 1;
2032 scsi_low_bus_release(slp, ti)
2033 struct scsi_low_softc *slp;
2034 struct targ_info *ti;
2037 if (ti->ti_disc > 0)
2039 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
2043 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
2046 /* clear all nexus pointer */
2047 slp->sl_Qnexus = NULL;
2048 slp->sl_Lnexus = NULL;
2049 slp->sl_Tnexus = NULL;
2051 /* clear selection assert */
2052 slp->sl_selid = NULL;
2054 /* clear nexus data */
2055 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
2057 /* clear phase change counter */
2058 slp->sl_ph_count = 0;
2062 scsi_low_setup_done(slp, cb)
2063 struct scsi_low_softc *slp;
2066 struct targ_info *ti;
2067 struct lun_info *li;
2072 if (cb->ccb_rcnt >= slp->sl_max_retry)
2074 cb->ccb_error |= ABORTIO;
2075 return SCSI_LOW_DONE_COMPLETE;
2078 /* XXX: special huck for selection timeout */
2079 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
2080 (cb->ccb_error & SELTIMEOUTIO) != 0)
2082 cb->ccb_error |= ABORTIO;
2083 return SCSI_LOW_DONE_COMPLETE;
2086 switch(li->li_state)
2088 case SCSI_LOW_LUN_INQ:
2089 if (cb->ccb_error != 0)
2092 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2096 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2098 else if ((li->li_inq.sd_version & 7) >= 2 ||
2099 (li->li_inq.sd_len >= 4))
2101 if ((li->li_inq.sd_support & 0x2) == 0)
2102 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2103 if ((li->li_inq.sd_support & 0x8) == 0)
2104 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2107 if ((li->li_inq.sd_support & 0x10) == 0)
2108 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2109 if ((li->li_inq.sd_support & 0x20) == 0)
2110 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2111 if ((li->li_inq.sd_support & 0x40) == 0)
2112 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2117 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2120 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2122 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2124 scsi_low_calcf_target(ti);
2125 scsi_low_calcf_lun(li);
2128 case SCSI_LOW_LUN_MODEQ:
2129 if (cb->ccb_error != 0)
2131 if (cb->ccb_error & SENSEIO)
2133 #ifdef SCSI_LOW_DEBUG
2134 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2136 printf("SENSE: [%x][%x][%x][%x][%x]\n",
2137 (u_int) cb->ccb_sense.error_code,
2138 (u_int) cb->ccb_sense.segment,
2139 (u_int) cb->ccb_sense.flags,
2140 (u_int) cb->ccb_sense.add_sense_code,
2141 (u_int) cb->ccb_sense.add_sense_code_qual);
2143 #endif /* SCSI_LOW_DEBUG */
2147 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2150 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2152 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2153 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2155 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2156 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2157 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2159 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2160 scsi_low_calcf_lun(li);
2168 if (li->li_state == SCSI_LOW_LUN_OK)
2170 scsi_low_calcf_target(ti);
2171 scsi_low_calcf_lun(li);
2172 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2173 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2175 scsi_low_calcf_show(li);
2180 return SCSI_LOW_DONE_RETRY;
2184 scsi_low_done(slp, cb)
2185 struct scsi_low_softc *slp;
2190 if (cb->ccb_error == 0)
2192 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2194 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2196 * SCSI-2 draft suggests
2197 * page 0x0a QErr bit determins if
2198 * the target aborts or continues
2199 * the queueing io's after CA state resolved.
2200 * However many targets seem not to support
2201 * the page 0x0a. Thus we should manually clear the
2202 * queuing io's after CA state.
2204 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2207 cb->ccb_flags |= CCB_CLEARQ;
2210 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2212 if ((cb->ccb_flags & CCB_SENSE) != 0)
2213 cb->ccb_error |= (SENSEIO | ABORTIO);
2214 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2216 else switch (cb->ccb_sscp.scp_status)
2222 if (cb->ccb_datalen == 0 ||
2223 cb->ccb_scp.scp_datalen == 0)
2226 if (cb->ccb_scp.scp_cmdlen > 0 &&
2227 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2228 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2231 cb->ccb_error |= PDMAERR;
2236 cb->ccb_error |= (BUSYERR | STATERR);
2240 cb->ccb_error |= (STATERR | ABORTIO);
2245 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2248 cb->ccb_flags |= CCB_SENSE;
2251 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2256 cb->ccb_error |= FATALIO;
2262 if (cb->ccb_flags & CCB_SENSE)
2264 cb->ccb_error |= (SENSEERR | ABORTIO);
2266 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2270 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2272 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2276 /* check a ccb msgout flag */
2277 if (cb->ccb_omsgoutflag != 0)
2279 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2280 SCSI_LOW_MSG_ABORT_QTAG | \
2281 SCSI_LOW_MSG_CLEAR_QTAG | \
2282 SCSI_LOW_MSG_TERMIO)
2284 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2286 cb->ccb_error |= ABORTIO;
2290 /* call OS depend done */
2291 if (cb->osdep != NULL)
2293 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2294 if (rv == EJUSTRETURN)
2297 else if (cb->ccb_error != 0)
2299 if (cb->ccb_rcnt >= slp->sl_max_retry)
2300 cb->ccb_error |= ABORTIO;
2302 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2303 (cb->ccb_error & ABORTIO) == 0)
2307 /* free our target */
2308 #ifdef SCSI_LOW_DEBUG
2309 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2311 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2312 scsi_low_print(slp, NULL);
2314 #endif /* SCSI_LOW_DEBUG */
2316 scsi_low_deactivate_qtag(cb);
2317 scsi_low_dealloc_qtag(cb);
2318 scsi_low_free_ccb(cb);
2320 return SCSI_LOW_DONE_COMPLETE;
2323 #ifdef SCSI_LOW_DEBUG
2324 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2326 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2327 scsi_low_print(slp, NULL);
2329 #endif /* SCSI_LOW_DEBUG */
2332 scsi_low_deactivate_qtag(cb);
2333 scsi_low_ccb_message_retry(cb);
2334 return SCSI_LOW_DONE_RETRY;
2337 /**************************************************************
2339 **************************************************************/
2341 scsi_low_reset_nexus_target(slp, ti, fdone)
2342 struct scsi_low_softc *slp;
2343 struct targ_info *ti;
2346 struct lun_info *li;
2348 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2349 li = LIST_NEXT(li, lun_chain))
2351 scsi_low_reset_nexus_lun(slp, li, fdone);
2352 li->li_state = SCSI_LOW_LUN_SLEEP;
2357 ti->ti_setup_msg = 0;
2358 ti->ti_setup_msg_done = 0;
2360 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2361 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2363 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2364 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2366 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2368 ((*slp->sl_funcs->scsi_low_targ_init)
2369 (slp, ti, SCSI_LOW_INFO_REVOKE));
2371 scsi_low_calcf_target(ti);
2373 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2374 li = LIST_NEXT(li, lun_chain))
2378 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2379 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2381 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2383 ((*slp->sl_funcs->scsi_low_lun_init)
2384 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2386 scsi_low_calcf_lun(li);
2391 scsi_low_reset_nexus(slp, fdone)
2392 struct scsi_low_softc *slp;
2395 struct targ_info *ti;
2396 struct slccb *cb, *topcb;
2398 if ((cb = slp->sl_Qnexus) != NULL)
2400 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2407 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2408 ti = TAILQ_NEXT(ti, ti_chain))
2410 scsi_low_reset_nexus_target(slp, ti, fdone);
2411 scsi_low_bus_release(slp, ti);
2412 scsi_low_init_msgsys(slp, ti);
2417 topcb->ccb_flags |= CCB_STARTQ;
2418 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2422 slp->sl_retry_sel = 0;
2423 slp->sl_flags &= ~HW_PDMASTART;
2428 static char tw_chars[] = "|/-\\";
2429 #define TWIDDLEWAIT 10000
2432 scsi_low_twiddle_wait(void)
2436 cnputc(tw_chars[tw_pos++]);
2437 tw_pos %= (sizeof(tw_chars) - 1);
2438 SCSI_LOW_DELAY(TWIDDLEWAIT);
2442 scsi_low_bus_reset(slp)
2443 struct scsi_low_softc *slp;
2447 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2449 printf("%s: try to reset scsi bus ", slp->sl_xname);
2450 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2451 scsi_low_twiddle_wait();
2457 scsi_low_restart(slp, flags, s)
2458 struct scsi_low_softc *slp;
2465 printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
2467 if ((error = scsi_low_init(slp, flags)) != 0)
2470 scsi_low_start(slp);
2474 /**************************************************************
2475 * disconnect and reselect
2476 **************************************************************/
2477 #define MSGCMD_LUN(msg) (msg & 0x07)
2479 static struct slccb *
2480 scsi_low_establish_ccb(ti, li, tag)
2481 struct targ_info *ti;
2482 struct lun_info *li;
2485 struct scsi_low_softc *slp = ti->ti_sc;
2491 cb = TAILQ_FIRST(&li->li_discq);
2492 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2493 if (cb->ccb_tag == tag)
2498 * establish our ccb nexus
2501 #ifdef SCSI_LOW_DEBUG
2502 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2504 printf("%s: nexus(0x%lx) abort check start\n",
2505 slp->sl_xname, (u_long) cb);
2506 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2507 scsi_low_revoke_ccb(slp, cb, 1);
2511 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2513 if (cb->ccb_omsgoutflag == 0)
2514 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2516 #endif /* SCSI_LOW_DEBUG */
2518 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2519 cb->ccb_flags &= ~CCB_DISCQ;
2520 slp->sl_Qnexus = cb;
2522 slp->sl_scp = cb->ccb_sscp;
2523 slp->sl_error |= cb->ccb_error;
2529 /* inform "ccb nexus established" to the host driver */
2530 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2533 if (cb->ccb_msgoutflag != 0)
2535 scsi_low_ccb_message_exec(slp, cb);
2542 scsi_low_reselected(slp, targ)
2543 struct scsi_low_softc *slp;
2546 struct targ_info *ti;
2551 * Check select vs reselected collision.
2554 if ((cb = slp->sl_selid) != NULL)
2556 scsi_low_arbit_fail(slp, cb);
2557 #ifdef SCSI_LOW_STATICS
2558 scsi_low_statics.nexus_conflict ++;
2559 #endif /* SCSI_LOW_STATICS */
2563 * Check if no current active nexus.
2565 if (slp->sl_Tnexus != NULL)
2572 * Check a valid target id asserted ?
2574 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2576 s = "scsi id illegal";
2581 * Check the target scsi status.
2583 ti = slp->sl_ti[targ];
2584 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2586 s = "phase mismatch";
2594 scsi_low_init_msgsys(slp, ti);
2597 * Establish our target nexus
2599 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2600 slp->sl_Tnexus = ti;
2601 #ifdef SCSI_LOW_STATICS
2602 scsi_low_statics.nexus_reselected ++;
2603 #endif /* SCSI_LOW_STATICS */
2607 printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
2608 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2609 "reselect: scsi world confused");
2613 /**************************************************************
2614 * cmd out pointer setup
2615 **************************************************************/
2617 scsi_low_cmd(slp, ti)
2618 struct scsi_low_softc *slp;
2619 struct targ_info *ti;
2621 struct slccb *cb = slp->sl_Qnexus;
2623 slp->sl_ph_count ++;
2629 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2630 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2631 slp->sl_scp.scp_datalen = 0;
2632 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2633 slp->sl_error |= FATALIO;
2634 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2635 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2640 #ifdef SCSI_LOW_DEBUG
2641 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2643 scsi_low_test_cmdlnk(slp, cb);
2645 #endif /* SCSI_LOW_DEBUG */
2650 /**************************************************************
2651 * data out pointer setup
2652 **************************************************************/
2654 scsi_low_data(slp, ti, bp, direction)
2655 struct scsi_low_softc *slp;
2656 struct targ_info *ti;
2660 struct slccb *cb = slp->sl_Qnexus;
2662 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2668 slp->sl_error |= (FATALIO | PDMAERR);
2669 slp->sl_scp.scp_datalen = 0;
2670 slp->sl_scp.scp_direction = direction;
2671 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2672 if (ti->ti_ophase != ti->ti_phase)
2677 s = "DATA PHASE: ccb nexus not found";
2679 s = "DATA PHASE: xfer direction mismatch";
2680 SCSI_LOW_INFO(slp, ti, s);
2687 /**************************************************************
2689 **************************************************************/
2690 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2691 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2692 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2693 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2694 #define MSGIN_DATA_LAST 0x30
2696 static int scsi_low_errfunc_synch (struct scsi_low_softc *, u_int);
2697 static int scsi_low_errfunc_wide (struct scsi_low_softc *, u_int);
2698 static int scsi_low_errfunc_identify (struct scsi_low_softc *, u_int);
2699 static int scsi_low_errfunc_qtag (struct scsi_low_softc *, u_int);
2701 static int scsi_low_msgfunc_synch (struct scsi_low_softc *);
2702 static int scsi_low_msgfunc_wide (struct scsi_low_softc *);
2703 static int scsi_low_msgfunc_identify (struct scsi_low_softc *);
2704 static int scsi_low_msgfunc_abort (struct scsi_low_softc *);
2705 static int scsi_low_msgfunc_qabort (struct scsi_low_softc *);
2706 static int scsi_low_msgfunc_qtag (struct scsi_low_softc *);
2707 static int scsi_low_msgfunc_reset (struct scsi_low_softc *);
2709 struct scsi_low_msgout_data {
2712 int (*md_msgfunc) (struct scsi_low_softc *);
2713 int (*md_errfunc) (struct scsi_low_softc *, u_int);
2714 #define MSG_RELEASE_ATN 0x0001
2718 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2719 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2720 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2721 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2722 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2723 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2724 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2725 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2726 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2727 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2728 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2729 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2730 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2731 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2732 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2733 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2734 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2737 static int scsi_low_msginfunc_ext (struct scsi_low_softc *);
2738 static int scsi_low_synch (struct scsi_low_softc *);
2739 static int scsi_low_wide (struct scsi_low_softc *);
2740 static int scsi_low_msginfunc_msg_reject (struct scsi_low_softc *);
2741 static int scsi_low_msginfunc_rejop (struct scsi_low_softc *);
2742 static int scsi_low_msginfunc_rp (struct scsi_low_softc *);
2743 static int scsi_low_msginfunc_sdp (struct scsi_low_softc *);
2744 static int scsi_low_msginfunc_disc (struct scsi_low_softc *);
2745 static int scsi_low_msginfunc_cc (struct scsi_low_softc *);
2746 static int scsi_low_msginfunc_lcc (struct scsi_low_softc *);
2747 static int scsi_low_msginfunc_parity (struct scsi_low_softc *);
2748 static int scsi_low_msginfunc_noop (struct scsi_low_softc *);
2749 static int scsi_low_msginfunc_simple_qtag (struct scsi_low_softc *);
2750 static int scsi_low_msginfunc_i_wide_residue (struct scsi_low_softc *);
2752 struct scsi_low_msgin_data {
2754 int (*md_msgfunc) (struct scsi_low_softc *);
2757 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2758 /* 0 */ {1, scsi_low_msginfunc_cc},
2759 /* 1 */ {2, scsi_low_msginfunc_ext},
2760 /* 2 */ {1, scsi_low_msginfunc_sdp},
2761 /* 3 */ {1, scsi_low_msginfunc_rp},
2762 /* 4 */ {1, scsi_low_msginfunc_disc},
2763 /* 5 */ {1, scsi_low_msginfunc_rejop},
2764 /* 6 */ {1, scsi_low_msginfunc_rejop},
2765 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2766 /* 8 */ {1, scsi_low_msginfunc_noop},
2767 /* 9 */ {1, scsi_low_msginfunc_parity},
2768 /* a */ {1, scsi_low_msginfunc_lcc},
2769 /* b */ {1, scsi_low_msginfunc_lcc},
2770 /* c */ {1, scsi_low_msginfunc_rejop},
2771 /* d */ {2, scsi_low_msginfunc_rejop},
2772 /* e */ {1, scsi_low_msginfunc_rejop},
2773 /* f */ {1, scsi_low_msginfunc_rejop},
2774 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2775 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2776 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2777 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2778 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2779 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2780 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2781 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2782 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2783 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2784 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2785 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2786 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2787 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2788 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2789 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2790 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2791 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2792 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2793 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2794 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2795 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2796 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2797 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2798 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2799 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2800 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2801 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2802 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2803 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2804 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2805 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2806 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2809 /**************************************************************
2811 **************************************************************/
2813 scsi_low_msgfunc_synch(slp)
2814 struct scsi_low_softc *slp;
2816 struct targ_info *ti = slp->sl_Tnexus;
2817 int ptr = ti->ti_msgoutlen;
2819 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2820 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2821 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2822 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2823 return MSG_EXTEND_SYNCHLEN + 2;
2827 scsi_low_msgfunc_wide(slp)
2828 struct scsi_low_softc *slp;
2830 struct targ_info *ti = slp->sl_Tnexus;
2831 int ptr = ti->ti_msgoutlen;
2833 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2834 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2835 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2836 return MSG_EXTEND_WIDELEN + 2;
2840 scsi_low_msgfunc_identify(slp)
2841 struct scsi_low_softc *slp;
2843 struct targ_info *ti = slp->sl_Tnexus;
2844 struct lun_info *li = slp->sl_Lnexus;
2845 struct slccb *cb = slp->sl_Qnexus;
2846 int ptr = ti->ti_msgoutlen;
2852 slp->sl_error |= FATALIO;
2853 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2854 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2858 if (scsi_low_is_disconnect_ok(cb) != 0)
2859 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2863 if (ti->ti_phase == PH_MSGOUT)
2865 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2866 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2868 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2872 ti->ti_msgoutstr[ptr + 0] = msg;
2877 scsi_low_msgfunc_abort(slp)
2878 struct scsi_low_softc *slp;
2881 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2886 scsi_low_msgfunc_qabort(slp)
2887 struct scsi_low_softc *slp;
2890 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2895 scsi_low_msgfunc_reset(slp)
2896 struct scsi_low_softc *slp;
2899 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2904 scsi_low_msgfunc_qtag(slp)
2905 struct scsi_low_softc *slp;
2907 struct targ_info *ti = slp->sl_Tnexus;
2908 struct slccb *cb = slp->sl_Qnexus;
2909 int ptr = ti->ti_msgoutlen;
2911 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2913 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2918 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2919 if (ti->ti_phase == PH_MSGOUT)
2921 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2928 * The following functions are called when targets give unexpected
2929 * responces in msgin (after msgout).
2932 scsi_low_errfunc_identify(slp, msgflags)
2933 struct scsi_low_softc *slp;
2937 if (slp->sl_Lnexus != NULL)
2939 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2940 scsi_low_calcf_lun(slp->sl_Lnexus);
2946 scsi_low_errfunc_synch(slp, msgflags)
2947 struct scsi_low_softc *slp;
2950 struct targ_info *ti = slp->sl_Tnexus;
2952 MSGIN_PERIOD(ti) = 0;
2953 MSGIN_OFFSET(ti) = 0;
2954 scsi_low_synch(slp);
2959 scsi_low_errfunc_wide(slp, msgflags)
2960 struct scsi_low_softc *slp;
2963 struct targ_info *ti = slp->sl_Tnexus;
2965 MSGIN_WIDTHP(ti) = 0;
2971 scsi_low_errfunc_qtag(slp, msgflags)
2972 struct scsi_low_softc *slp;
2976 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2978 if (slp->sl_Qnexus != NULL)
2980 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2982 if (slp->sl_Lnexus != NULL)
2984 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2985 scsi_low_calcf_lun(slp->sl_Lnexus);
2987 printf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
2994 scsi_low_msgout(slp, ti, fl)
2995 struct scsi_low_softc *slp;
2996 struct targ_info *ti;
2999 struct scsi_low_msgout_data *mdp;
3002 #ifdef SCSI_LOW_DIAGNOSTIC
3003 if (ti != slp->sl_Tnexus)
3005 scsi_low_print(slp, NULL);
3006 panic("scsi_low_msgout: Target nexus inconsistent");
3008 #endif /* SCSI_LOW_DIAGNOSTIC */
3010 slp->sl_ph_count ++;
3011 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3013 printf("%s: too many phase changes\n", slp->sl_xname);
3014 slp->sl_error |= FATALIO;
3015 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3019 * Scsi phase changes.
3020 * Previously msgs asserted are accepted by our target or
3021 * processed by scsi_low_msgin.
3022 * Thus clear all saved informations.
3024 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
3026 ti->ti_omsgflags = 0;
3027 ti->ti_emsgflags = 0;
3029 else if (slp->sl_atten == 0)
3032 * We did not assert attention, however still our target required
3033 * msgs. Resend previous msgs.
3035 ti->ti_msgflags |= ti->ti_omsgflags;
3036 ti->ti_omsgflags = 0;
3037 #ifdef SCSI_LOW_DIAGNOSTIC
3038 printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
3039 #endif /* SCSI_LOW_DIAGNOSTIC */
3043 * We have no msgs. send MSG_NOOP (OK?)
3045 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3046 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
3051 ti->ti_msgoutlen = 0;
3052 slp->sl_clear_atten = 0;
3053 mdp = &scsi_low_msgout_data[0];
3054 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3056 if ((ti->ti_msgflags & mdp->md_flags) != 0)
3058 ti->ti_omsgflags |= mdp->md_flags;
3059 ti->ti_msgflags &= ~mdp->md_flags;
3060 ti->ti_emsgflags = mdp->md_flags;
3062 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
3063 if (mdp->md_msgfunc != NULL)
3064 len = (*mdp->md_msgfunc) (slp);
3068 #ifdef SCSI_LOW_DIAGNOSTIC
3069 scsi_low_msg_log_write(&ti->ti_log_msgout,
3070 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
3071 #endif /* SCSI_LOW_DIAGNOSTIC */
3073 ti->ti_msgoutlen += len;
3074 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
3076 slp->sl_clear_atten = 1;
3080 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
3081 ti->ti_msgflags == 0)
3084 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3089 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3090 slp->sl_clear_atten = 1;
3092 return ti->ti_msgoutlen;
3095 /**************************************************************
3097 **************************************************************/
3099 scsi_low_msginfunc_noop(slp)
3100 struct scsi_low_softc *slp;
3107 scsi_low_msginfunc_rejop(slp)
3108 struct scsi_low_softc *slp;
3110 struct targ_info *ti = slp->sl_Tnexus;
3111 u_int8_t msg = ti->ti_msgin[0];
3113 printf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
3114 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3119 scsi_low_msginfunc_cc(slp)
3120 struct scsi_low_softc *slp;
3122 struct lun_info *li;
3124 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3126 /* validate status */
3127 if (slp->sl_Qnexus == NULL)
3130 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3131 li = slp->sl_Lnexus;
3132 switch (slp->sl_scp.scp_status)
3135 li->li_maxnqio = li->li_maxnexus;
3140 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3141 scsi_low_reset_nexus_lun(slp, li, 0);
3149 if (li->li_maxnexus >= li->li_nqio)
3150 li->li_maxnexus = li->li_nqio - 1;
3151 li->li_maxnqio = li->li_maxnexus;
3156 slp->sl_error |= MSGERR;
3166 scsi_low_msginfunc_lcc(slp)
3167 struct scsi_low_softc *slp;
3169 struct targ_info *ti;
3170 struct lun_info *li;
3171 struct slccb *ncb, *cb;
3173 ti = slp->sl_Tnexus;
3174 li = slp->sl_Lnexus;
3175 if ((cb = slp->sl_Qnexus) == NULL)
3178 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3179 switch (slp->sl_scp.scp_status)
3183 li->li_maxnqio = li->li_maxnexus;
3187 slp->sl_error |= MSGERR;
3191 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3194 cb->ccb_error |= slp->sl_error;
3195 if (cb->ccb_error != 0)
3198 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3199 ncb = TAILQ_NEXT(ncb, ccb_chain))
3202 goto cmd_link_start;
3207 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3208 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3212 ncb->ccb_flags &= ~CCB_STARTQ;
3213 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3215 scsi_low_dealloc_qtag(ncb);
3216 ncb->ccb_tag = cb->ccb_tag;
3217 ncb->ccb_otag = cb->ccb_otag;
3218 cb->ccb_tag = SCSI_LOW_UNKTAG;
3219 cb->ccb_otag = SCSI_LOW_UNKTAG;
3220 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3221 panic("%s: linked ccb retried", slp->sl_xname);
3223 slp->sl_Qnexus = ncb;
3224 slp->sl_ph_count = 0;
3227 ncb->ccb_datalen = -1;
3228 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3229 ncb->ccb_flags &= ~CCB_INTERNAL;
3231 scsi_low_init_msgsys(slp, ti);
3233 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3235 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3236 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3237 ncb->ccb_tc = ncb->ccb_tcmax;
3239 /* setup saved scsi data pointer */
3240 ncb->ccb_sscp = ncb->ccb_scp;
3241 slp->sl_scp = ncb->ccb_sscp;
3242 slp->sl_error = ncb->ccb_error;
3244 #ifdef SCSI_LOW_DIAGNOSTIC
3245 scsi_low_msg_log_init(&ti->ti_log_msgin);
3246 scsi_low_msg_log_init(&ti->ti_log_msgout);
3247 #endif /* SCSI_LOW_DIAGNOSTIC */
3252 scsi_low_msginfunc_disc(slp)
3253 struct scsi_low_softc *slp;
3256 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3261 scsi_low_msginfunc_sdp(slp)
3262 struct scsi_low_softc *slp;
3264 struct slccb *cb = slp->sl_Qnexus;
3268 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3269 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3272 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3277 scsi_low_msginfunc_rp(slp)
3278 struct scsi_low_softc *slp;
3281 if (slp->sl_Qnexus != NULL)
3282 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3284 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3290 struct scsi_low_softc *slp;
3292 struct targ_info *ti = slp->sl_Tnexus;
3293 u_int period = 0, offset = 0, speed;
3297 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3298 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3299 MSGIN_OFFSET(ti) == 0)
3301 if ((offset = MSGIN_OFFSET(ti)) != 0)
3302 period = MSGIN_PERIOD(ti);
3303 s = offset ? "synchronous" : "async";
3308 * Target seems to be brain damaged.
3309 * Force async transfer.
3311 ti->ti_maxsynch.period = 0;
3312 ti->ti_maxsynch.offset = 0;
3313 printf("%s: target brain damaged. async transfer\n",
3318 ti->ti_maxsynch.period = period;
3319 ti->ti_maxsynch.offset = offset;
3321 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3325 * Current period and offset are not acceptable
3327 * The adapter changes max synch and max offset.
3329 printf("%s: synch neg failed. retry synch msg neg ...\n",
3334 ti->ti_osynch = ti->ti_maxsynch;
3337 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3341 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3343 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3344 struct slccb *cb = slp->sl_Qnexus;
3346 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3348 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3350 printf("%s(%d:*): <%s> offset %d period %dns ",
3351 slp->sl_xname, ti->ti_id, s, offset, period * 4);
3355 speed = 1000 * 10 / (period * 4);
3356 printf("%d.%d M/s", speed / 10, speed % 10);
3365 struct scsi_low_softc *slp;
3367 struct targ_info *ti = slp->sl_Tnexus;
3370 ti->ti_width = MSGIN_WIDTHP(ti);
3371 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3375 * Current width is not acceptable for our adapter.
3376 * The adapter changes max width.
3378 printf("%s: wide neg failed. retry wide msg neg ...\n",
3383 ti->ti_owidth = ti->ti_width;
3384 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3386 ti->ti_setup_msg_done |=
3387 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3391 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3393 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3394 struct slccb *cb = slp->sl_Qnexus;
3396 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3398 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3400 printf("%s(%d:*): transfer width %d bits\n",
3401 slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3407 scsi_low_msginfunc_simple_qtag(slp)
3408 struct scsi_low_softc *slp;
3410 struct targ_info *ti = slp->sl_Tnexus;
3411 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3413 if (slp->sl_Qnexus != NULL)
3415 if (slp->sl_Qnexus->ccb_tag != etag)
3417 slp->sl_error |= FATALIO;
3418 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3419 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3422 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3424 #ifdef SCSI_LOW_DEBUG
3425 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3427 #endif /* SCSI_LOW_DEBUG */
3429 slp->sl_error |= FATALIO;
3430 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3431 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3437 scsi_low_msginfunc_i_wide_residue(slp)
3438 struct scsi_low_softc *slp;
3440 struct targ_info *ti = slp->sl_Tnexus;
3441 struct slccb *cb = slp->sl_Qnexus;
3442 int res = (int) ti->ti_msgin[1];
3444 if (cb == NULL || res <= 0 ||
3445 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3446 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3449 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3452 slp->sl_scp.scp_datalen += res;
3453 slp->sl_scp.scp_data -= res;
3454 scsi_low_data_finish(slp);
3459 scsi_low_msginfunc_ext(slp)
3460 struct scsi_low_softc *slp;
3462 struct slccb *cb = slp->sl_Qnexus;
3463 struct lun_info *li = slp->sl_Lnexus;
3464 struct targ_info *ti = slp->sl_Tnexus;
3468 if (ti->ti_msginptr == 2)
3470 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3474 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3476 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3480 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3481 count = (int) htonl((long) (*ptr));
3482 if(slp->sl_scp.scp_datalen - count < 0 ||
3483 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3486 slp->sl_scp.scp_datalen -= count;
3487 slp->sl_scp.scp_data += count;
3490 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3494 retry = scsi_low_synch(slp);
3495 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3496 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3498 #ifdef SCSI_LOW_DEBUG
3499 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3501 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3503 #endif /* SCSI_LOW_DEBUG */
3506 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3510 retry = scsi_low_wide(slp);
3511 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3512 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3520 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3525 scsi_low_msginfunc_parity(slp)
3526 struct scsi_low_softc *slp;
3528 struct targ_info *ti = slp->sl_Tnexus;
3530 /* only I -> T, invalid! */
3531 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3536 scsi_low_msginfunc_msg_reject(slp)
3537 struct scsi_low_softc *slp;
3539 struct targ_info *ti = slp->sl_Tnexus;
3540 struct scsi_low_msgout_data *mdp;
3543 if (ti->ti_emsgflags != 0)
3545 printf("%s: msg flags [0x%x] rejected\n",
3546 slp->sl_xname, ti->ti_emsgflags);
3547 msgflags = SCSI_LOW_MSG_REJECT;
3548 mdp = &scsi_low_msgout_data[0];
3549 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3551 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3553 ti->ti_emsgflags &= ~mdp->md_flags;
3554 if (mdp->md_errfunc != NULL)
3555 (*mdp->md_errfunc) (slp, msgflags);
3563 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3564 slp->sl_error |= MSGERR;
3570 scsi_low_msgin(slp, ti, c)
3571 struct scsi_low_softc *slp;
3572 struct targ_info *ti;
3575 struct scsi_low_msgin_data *sdp;
3576 struct lun_info *li;
3579 #ifdef SCSI_LOW_DIAGNOSTIC
3580 if (ti != slp->sl_Tnexus)
3582 scsi_low_print(slp, NULL);
3583 panic("scsi_low_msgin: Target nexus inconsistent");
3585 #endif /* SCSI_LOW_DIAGNOSTIC */
3588 * Phase changes, clear the pointer.
3590 if (ti->ti_ophase != ti->ti_phase)
3593 ti->ti_msgin_parity_error = 0;
3595 slp->sl_ph_count ++;
3596 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3598 printf("%s: too many phase changes\n", slp->sl_xname);
3599 slp->sl_error |= FATALIO;
3600 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3605 * Store a current messages byte into buffer and
3606 * wait for the completion of the current msg.
3608 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3609 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3611 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3612 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3616 * Check parity errors.
3618 if ((c & SCSI_LOW_DATA_PE) != 0)
3620 ti->ti_msgin_parity_error ++;
3621 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3625 if (ti->ti_msgin_parity_error != 0)
3629 * Calculate messages length.
3631 msg = ti->ti_msgin[0];
3632 if (msg < MSGIN_DATA_LAST)
3633 sdp = &scsi_low_msgin_data[msg];
3635 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3637 if (ti->ti_msginlen == 0)
3639 ti->ti_msginlen = sdp->md_len;
3645 if (ti->ti_msginptr < ti->ti_msginlen)
3651 if ((msg & MSG_IDENTIFY) == 0)
3653 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3658 li = slp->sl_Lnexus;
3661 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3664 slp->sl_Lnexus = li;
3665 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3669 if (MSGCMD_LUN(msg) != li->li_lun)
3673 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3675 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3677 #ifdef SCSI_LOW_DEBUG
3678 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3682 #endif /* SCSI_LOW_DEBUG */
3690 * Msg process completed, reset msgin pointer and assert ATN if desired.
3693 slp->sl_error |= FATALIO;
3694 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3695 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3698 if (ti->ti_msginptr < ti->ti_msginlen)
3701 #ifdef SCSI_LOW_DIAGNOSTIC
3702 scsi_low_msg_log_write(&ti->ti_log_msgin,
3703 &ti->ti_msgin[0], ti->ti_msginlen);
3704 #endif /* SCSI_LOW_DIAGNOSTIC */
3710 /**********************************************************
3712 **********************************************************/
3714 scsi_low_disconnected(slp, ti)
3715 struct scsi_low_softc *slp;
3716 struct targ_info *ti;
3718 struct slccb *cb = slp->sl_Qnexus;
3720 /* check phase completion */
3721 switch (slp->sl_msgphase)
3724 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3725 scsi_low_msginfunc_cc(slp);
3726 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3730 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3731 scsi_low_msginfunc_cc(slp);
3732 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3736 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3737 scsi_low_msginfunc_cc(slp);
3743 struct lun_info *li;
3746 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3747 cb->ccb_flags |= CCB_DISCQ;
3748 cb->ccb_error |= slp->sl_error;
3754 #ifdef SCSI_LOW_STATICS
3755 scsi_low_statics.nexus_disconnected ++;
3756 #endif /* SCSI_LOW_STATICS */
3758 #ifdef SCSI_LOW_DEBUG
3759 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3761 printf("## SCSI_LOW_DISCONNECTED ===============\n");
3762 scsi_low_print(slp, NULL);
3764 #endif /* SCSI_LOW_DEBUG */
3768 slp->sl_error |= FATALIO;
3769 if (ti->ti_phase == PH_SELSTART)
3770 slp->sl_error |= SELTIMEOUTIO;
3772 slp->sl_error |= UBFERR;
3781 #ifdef SCSI_LOW_DEBUG
3782 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3784 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3785 (cb->ccb_msgoutflag != 0 ||
3786 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3788 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3791 #endif /* SCSI_LOW_DEBUG */
3793 cb->ccb_error |= slp->sl_error;
3794 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3796 cb->ccb_flags |= CCB_STARTQ;
3797 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3802 scsi_low_bus_release(slp, ti);
3803 scsi_low_start(slp);
3807 /**********************************************************
3809 **********************************************************/
3811 scsi_low_alloc_qtag(cb)
3814 struct lun_info *li = cb->li;
3815 scsi_low_tag_t etag;
3817 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3820 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3821 etag = ffs(li->li_qtagbits);
3825 li->li_qtagbits &= ~(1 << (etag - 1));
3826 cb->ccb_otag = etag;
3829 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3830 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3831 if (li->li_qtagarray[li->li_qd] == 0)
3834 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3835 if (li->li_qtagarray[li->li_qd] == 0)
3841 li->li_qtagarray[li->li_qd] ++;
3842 cb->ccb_otag = (li->li_qd ++);
3844 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3848 scsi_low_dealloc_qtag(cb)
3851 struct lun_info *li = cb->li;
3852 scsi_low_tag_t etag;
3854 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3857 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3858 etag = cb->ccb_otag - 1;
3859 #ifdef SCSI_LOW_DIAGNOSTIC
3860 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3861 panic("scsi_low_dealloc_tag: illegal tag");
3862 #endif /* SCSI_LOW_DIAGNOSTIC */
3863 li->li_qtagbits |= (1 << etag);
3865 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3866 etag = cb->ccb_otag;
3867 #ifdef SCSI_LOW_DIAGNOSTIC
3868 if (etag >= SCSI_LOW_MAXNEXUS)
3869 panic("scsi_low_dealloc_tag: illegal tag");
3870 #endif /* SCSI_LOW_DIAGNOSTIC */
3871 li->li_qtagarray[etag] --;
3872 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3874 cb->ccb_otag = SCSI_LOW_UNKTAG;
3879 scsi_low_revoke_ccb(slp, cb, fdone)
3880 struct scsi_low_softc *slp;
3884 struct targ_info *ti = cb->ti;
3885 struct lun_info *li = cb->li;
3887 #ifdef SCSI_LOW_DIAGNOSTIC
3888 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3889 (CCB_STARTQ | CCB_DISCQ))
3891 panic("%s: ccb in both queue", slp->sl_xname);
3893 #endif /* SCSI_LOW_DIAGNOSTIC */
3895 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3897 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3900 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3902 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3908 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3909 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3912 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3913 (cb->ccb_flags & CCB_NORETRY) != 0))
3915 cb->ccb_error |= FATALIO;
3916 cb->ccb_flags &= ~CCB_AUTOSENSE;
3917 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3918 panic("%s: done ccb retried", slp->sl_xname);
3923 cb->ccb_error |= PENDINGIO;
3924 scsi_low_deactivate_qtag(cb);
3925 scsi_low_ccb_message_retry(cb);
3926 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3932 scsi_low_reset_nexus_lun(slp, li, fdone)
3933 struct scsi_low_softc *slp;
3934 struct lun_info *li;
3937 struct slccb *cb, *ncb, *ecb;
3943 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3945 ncb = TAILQ_NEXT(cb, ccb_chain);
3946 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3950 * presumely keep ordering of io
3952 cb->ccb_flags |= CCB_STARTQ;
3955 TAILQ_INSERT_HEAD(&slp->sl_start,\
3960 TAILQ_INSERT_AFTER(&slp->sl_start,\
3961 ecb, cb, ccb_chain);
3968 /**************************************************************
3970 **************************************************************/
3972 scsi_low_calcf_lun(li)
3973 struct lun_info *li;
3975 struct targ_info *ti = li->li_ti;
3976 struct scsi_low_softc *slp = ti->ti_sc;
3977 u_int cfgflags, diskflags;
3979 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3980 cfgflags = li->li_cfgflags;
3984 diskflags = li->li_diskflags & li->li_quirks;
3987 li->li_flags &= ~SCSI_LOW_DISC;
3988 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3989 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3990 (cfgflags & SCSI_LOW_DISC) != 0)
3991 li->li_flags |= SCSI_LOW_DISC;
3994 li->li_flags |= SCSI_LOW_NOPARITY;
3995 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3996 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3997 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3998 li->li_flags &= ~SCSI_LOW_NOPARITY;
4001 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
4002 (cfgflags & SCSI_LOW_QTAG) != 0 &&
4003 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
4005 li->li_flags |= SCSI_LOW_QTAG;
4006 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
4007 li->li_maxnqio = li->li_maxnexus;
4011 li->li_flags &= ~SCSI_LOW_QTAG;
4012 li->li_maxnexus = 0;
4013 li->li_maxnqio = li->li_maxnexus;
4017 li->li_flags &= ~SCSI_LOW_LINK;
4018 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
4019 (diskflags & SCSI_LOW_DISK_LINK) != 0)
4020 li->li_flags |= SCSI_LOW_LINK;
4022 /* compatible flags */
4023 li->li_flags &= ~SCSI_LOW_SYNC;
4024 if (ti->ti_maxsynch.offset > 0)
4025 li->li_flags |= SCSI_LOW_SYNC;
4027 #ifdef SCSI_LOW_DEBUG
4028 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4030 scsi_low_calcf_show(li);
4032 #endif /* SCSI_LOW_DEBUG */
4036 scsi_low_calcf_target(ti)
4037 struct targ_info *ti;
4039 struct scsi_low_softc *slp = ti->ti_sc;
4040 u_int offset, period, diskflags;
4042 diskflags = ti->ti_diskflags & ti->ti_quirks;
4045 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
4046 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
4048 offset = ti->ti_maxsynch.offset;
4049 period = ti->ti_maxsynch.period;
4050 if (offset == 0 || period == 0)
4051 offset = period = 0;
4055 offset = period = 0;
4058 ti->ti_maxsynch.offset = offset;
4059 ti->ti_maxsynch.period = period;
4062 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
4063 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
4064 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
4066 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
4067 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
4068 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
4070 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
4072 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
4073 ti->ti_maxsynch.period != ti->ti_osynch.period)
4074 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
4075 if (ti->ti_width != ti->ti_owidth)
4076 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
4078 ti->ti_osynch = ti->ti_maxsynch;
4079 ti->ti_owidth = ti->ti_width;
4082 #ifdef SCSI_LOW_DEBUG
4083 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4085 printf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
4086 slp->sl_xname, ti->ti_id,
4087 ti->ti_maxsynch.period * 4,
4088 ti->ti_maxsynch.offset,
4091 #endif /* SCSI_LOW_DEBUG */
4095 scsi_low_calcf_show(li)
4096 struct lun_info *li;
4098 struct targ_info *ti = li->li_ti;
4099 struct scsi_low_softc *slp = ti->ti_sc;
4101 printf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4102 slp->sl_xname, ti->ti_id, li->li_lun,
4103 ti->ti_maxsynch.period * 4,
4104 ti->ti_maxsynch.offset,
4106 li->li_flags, SCSI_LOW_BITS);
4109 #ifdef SCSI_LOW_START_UP_CHECK
4110 /**************************************************************
4111 * scsi world start up
4112 **************************************************************/
4113 static int scsi_low_poll (struct scsi_low_softc *, struct slccb *);
4116 scsi_low_start_up(slp)
4117 struct scsi_low_softc *slp;
4119 struct targ_info *ti;
4120 struct lun_info *li;
4124 printf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
4126 for (target = 0; target < slp->sl_ntargs; target ++)
4128 if (target == slp->sl_hostid)
4130 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4132 printf("%s: scsi_low: target %d (host card)\n",
4133 slp->sl_xname, target);
4138 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4140 printf("%s: scsi_low: target %d lun ",
4141 slp->sl_xname, target);
4144 ti = slp->sl_ti[target];
4145 for (lun = 0; lun < slp->sl_nluns; lun ++)
4147 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4153 li = scsi_low_alloc_li(ti, lun, 1);
4155 scsi_low_enqueue(slp, ti, li, cb,
4156 CCB_AUTOSENSE | CCB_POLLED, 0);
4158 scsi_low_poll(slp, cb);
4160 if (li->li_state != SCSI_LOW_LUN_OK)
4163 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4169 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4178 scsi_low_poll(slp, cb)
4179 struct scsi_low_softc *slp;
4185 while (slp->sl_nio > 0)
4187 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4189 (*slp->sl_funcs->scsi_low_poll) (slp);
4190 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4194 scsi_low_timeout_check(slp);
4199 #endif /* SCSI_LOW_START_UP_CHECK */
4201 /**********************************************************
4203 **********************************************************/
4204 #ifdef SCSI_LOW_DEBUG
4206 scsi_low_test_abort(slp, ti, li)
4207 struct scsi_low_softc *slp;
4208 struct targ_info *ti;
4209 struct lun_info *li;
4213 if (li->li_disc > 1)
4215 acb = TAILQ_FIRST(&li->li_discq);
4216 if (scsi_low_abort_ccb(slp, acb) == 0)
4218 printf("%s: aborting ccb(0x%lx) start\n",
4219 slp->sl_xname, (u_long) acb);
4225 scsi_low_test_atten(slp, ti, msg)
4226 struct scsi_low_softc *slp;
4227 struct targ_info *ti;
4231 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4232 scsi_low_assert_msg(slp, ti, msg, 0);
4234 printf("%s: atten check OK\n", slp->sl_xname);
4238 scsi_low_test_cmdlnk(slp, cb)
4239 struct scsi_low_softc *slp;
4242 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4244 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4247 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4248 slp->sl_scp.scp_cmdlen);
4249 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4250 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4252 #endif /* SCSI_LOW_DEBUG */
4255 scsi_low_info(slp, ti, s)
4256 struct scsi_low_softc *slp;
4257 struct targ_info *ti;
4262 slp = LIST_FIRST(&sl_tab);
4266 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4269 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4270 ti = TAILQ_NEXT(ti, ti_chain))
4272 scsi_low_print(slp, ti);
4277 scsi_low_print(slp, ti);
4281 static u_char *phase[] =
4283 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4284 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4288 scsi_low_print(slp, ti)
4289 struct scsi_low_softc *slp;
4290 struct targ_info *ti;
4292 struct lun_info *li;
4296 if (ti == NULL || ti == slp->sl_Tnexus)
4298 ti = slp->sl_Tnexus;
4299 li = slp->sl_Lnexus;
4300 cb = slp->sl_Qnexus;
4304 li = LIST_FIRST(&ti->ti_litab);
4305 cb = TAILQ_FIRST(&li->li_discq);
4309 printf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4310 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4316 u_int flags = 0, maxnqio = 0, nqio = 0;
4322 flags = li->li_flags;
4323 maxnqio = li->li_maxnqio;
4327 printf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4329 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4330 phase[(int) ti->ti_phase], ti->ti_disc,
4335 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4336 (u_int) cb->ccb_scp.scp_cmd[0],
4337 cb->ccb_scp.scp_cmdlen,
4339 cb->ccb_scp.scp_datalen,
4340 (u_int) cb->ccb_sscp.scp_status,
4341 cb->ccb_error, SCSI_LOW_ERRORBITS);
4344 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4345 (u_int) (ti->ti_msginptr),
4346 (u_int) (ti->ti_msgin[0]),
4347 (u_int) (ti->ti_msgin[1]),
4348 (u_int) (ti->ti_msgin[2]),
4349 (u_int) (ti->ti_msgin[3]),
4350 (u_int) (ti->ti_msgin[4]),
4353 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4354 (u_int) ti->ti_msgflags,
4355 (u_int) (ti->ti_msgoutstr[0]),
4356 (u_int) (ti->ti_msgoutstr[1]),
4357 (u_int) (ti->ti_msgoutstr[2]),
4358 (u_int) (ti->ti_msgoutstr[3]),
4359 (u_int) (ti->ti_msgoutstr[4]),
4361 flags, SCSI_LOW_BITS);
4363 #ifdef SCSI_LOW_DIAGNOSTIC
4364 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4365 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4366 #endif /* SCSI_LOW_DIAGNOSTIC */
4370 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4371 (u_long) sp->scp_data,
4373 (u_int) sp->scp_status,
4374 slp->sl_error, SCSI_LOW_ERRORBITS);