1 /* $NecBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $ */
7 #define SCSI_LOW_STATICS
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
12 /* #define SCSI_LOW_INFO_DETAIL */
14 /* #define SCSI_LOW_QCLEAR_AFTER_CA */
15 /* #define SCSI_LOW_FLAGS_QUIRKS_OK */
17 #define SCSI_LOW_FLAGS_QUIRKS_OK
20 * [NetBSD for NEC PC-98 series]
21 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
22 * NetBSD/pc98 porting staff. All rights reserved.
23 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
24 * Naofumi HONDA. All rights reserved.
26 * [Ported for FreeBSD CAM]
27 * Copyright (c) 2000, 2001
28 * MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The name of the author may not be used to endorse or promote products
40 * derived from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
46 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
55 /* <On the nexus establishment>
56 * When our host is reselected,
57 * nexus establish processes are little complicated.
58 * Normal steps are followings:
59 * 1) Our host selected by target => target nexus (slp->sl_Tnexus)
60 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
61 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
70 #include <sys/queue.h>
71 #include <sys/malloc.h>
72 #include <sys/errno.h>
75 #include <cam/cam_ccb.h>
76 #include <cam/cam_sim.h>
77 #include <cam/cam_debug.h>
78 #include <cam/cam_periph.h>
79 #include <cam/cam_xpt_periph.h>
81 #include <cam/scsi/scsi_all.h>
82 #include <cam/scsi/scsi_message.h>
84 #include <cam/scsi/scsi_low.h>
88 /**************************************************************
90 **************************************************************/
91 #define SCSI_LOW_POLL_HZ 1000
93 /* functions return values */
94 #define SCSI_LOW_START_NO_QTAG 0
95 #define SCSI_LOW_START_QTAG 1
97 #define SCSI_LOW_DONE_COMPLETE 0
98 #define SCSI_LOW_DONE_RETRY 1
100 /* internal disk flags */
101 #define SCSI_LOW_DISK_DISC 0x00000001
102 #define SCSI_LOW_DISK_QTAG 0x00000002
103 #define SCSI_LOW_DISK_LINK 0x00000004
104 #define SCSI_LOW_DISK_PARITY 0x00000008
105 #define SCSI_LOW_DISK_SYNC 0x00010000
106 #define SCSI_LOW_DISK_WIDE_16 0x00020000
107 #define SCSI_LOW_DISK_WIDE_32 0x00040000
108 #define SCSI_LOW_DISK_WIDE (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
109 #define SCSI_LOW_DISK_LFLAGS 0x0000ffff
110 #define SCSI_LOW_DISK_TFLAGS 0xffff0000
112 static MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
114 /**************************************************************
116 **************************************************************/
117 /* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
118 static void scsi_low_engage(void *);
119 static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
120 static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
121 static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
122 static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
123 static void scsi_low_twiddle_wait(void);
124 static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
125 static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
126 static void scsi_low_calcf_lun(struct lun_info *);
127 static void scsi_low_calcf_target(struct targ_info *);
128 static void scsi_low_calcf_show(struct lun_info *);
129 static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
130 static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
131 static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
132 static int scsi_low_init(struct scsi_low_softc *, u_int);
133 static void scsi_low_start(struct scsi_low_softc *);
134 static void scsi_low_free_ti(struct scsi_low_softc *);
136 static int scsi_low_alloc_qtag(struct slccb *);
137 static int scsi_low_dealloc_qtag(struct slccb *);
138 static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
139 static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
140 static void scsi_low_unit_ready_cmd(struct slccb *);
141 static void scsi_low_timeout(void *);
142 static int scsi_low_timeout_check(struct scsi_low_softc *);
143 #ifdef SCSI_LOW_START_UP_CHECK
144 static int scsi_low_start_up(struct scsi_low_softc *);
145 #endif /* SCSI_LOW_START_UP_CHECK */
146 static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
147 static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);
149 int scsi_low_version_major = 2;
150 int scsi_low_version_minor = 17;
152 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
154 /**************************************************************
155 * Debug, Run test and Statics
156 **************************************************************/
157 #ifdef SCSI_LOW_INFO_DETAIL
158 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
159 #else /* !SCSI_LOW_INFO_DETAIL */
160 #define SCSI_LOW_INFO(slp, ti, s) device_printf((slp)->sl_dev, "%s\n", (s))
161 #endif /* !SCSI_LOW_INFO_DETAIL */
163 #ifdef SCSI_LOW_STATICS
164 static struct scsi_low_statics {
167 int nexus_disconnected;
168 int nexus_reselected;
171 #endif /* SCSI_LOW_STATICS */
173 #ifdef SCSI_LOW_DEBUG
174 #define SCSI_LOW_DEBUG_DONE 0x00001
175 #define SCSI_LOW_DEBUG_DISC 0x00002
176 #define SCSI_LOW_DEBUG_SENSE 0x00004
177 #define SCSI_LOW_DEBUG_CALCF 0x00008
178 #define SCSI_LOW_DEBUG_ACTION 0x10000
179 int scsi_low_debug = 0;
181 #define SCSI_LOW_MAX_ATTEN_CHECK 32
182 #define SCSI_LOW_ATTEN_CHECK 0x0001
183 #define SCSI_LOW_CMDLNK_CHECK 0x0002
184 #define SCSI_LOW_ABORT_CHECK 0x0004
185 #define SCSI_LOW_NEXUS_CHECK 0x0008
186 int scsi_low_test = 0;
187 int scsi_low_test_id = 0;
189 static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
190 static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
191 static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
192 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
193 ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
194 #define SCSI_LOW_DEBUG_GO(fl, id) \
195 ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
196 #endif /* SCSI_LOW_DEBUG */
198 /**************************************************************
200 **************************************************************/
201 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
202 GENERIC_CCB(scsi_low, slccb, ccb_chain)
204 /**************************************************************
206 **************************************************************/
207 #define SCSI_LOW_INLINE static __inline
208 SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
209 SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
210 SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
211 SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
212 SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
213 SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
214 SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);
217 scsi_low_activate_qtag(cb)
220 struct lun_info *li = cb->li;
222 if (cb->ccb_tag != SCSI_LOW_UNKTAG)
226 cb->ccb_tag = cb->ccb_otag;
230 scsi_low_deactivate_qtag(cb)
233 struct lun_info *li = cb->li;
235 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
239 cb->ccb_tag = SCSI_LOW_UNKTAG;
243 scsi_low_ccb_message_exec(slp, cb)
244 struct scsi_low_softc *slp;
248 scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
249 cb->ccb_msgoutflag = 0;
253 scsi_low_ccb_message_assert(cb, msg)
258 cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
262 scsi_low_ccb_message_retry(cb)
265 cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
269 scsi_low_ccb_message_clear(cb)
272 cb->ccb_msgoutflag = 0;
276 scsi_low_init_msgsys(slp, ti)
277 struct scsi_low_softc *slp;
278 struct targ_info *ti;
282 ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
283 SCSI_LOW_DEASSERT_ATN(slp);
284 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
287 /*=============================================================
288 * START OF OS switch (All OS depend fucntions should be here)
289 =============================================================*/
290 /* common os depend utitlities */
291 #define SCSI_LOW_CMD_RESIDUAL_CHK 0x0001
292 #define SCSI_LOW_CMD_ORDERED_QTAG 0x0002
293 #define SCSI_LOW_CMD_ABORT_WARNING 0x0004
295 static u_int8_t scsi_low_cmd_flags[256] = {
296 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
297 /*0*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
298 /*1*/ 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
299 /*2*/ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
300 /*3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
303 struct scsi_low_error_code {
308 static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
309 static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);
311 static struct slccb *
312 scsi_low_find_ccb(slp, target, lun, osdep)
313 struct scsi_low_softc *slp;
317 struct targ_info *ti;
321 ti = slp->sl_ti[target];
322 li = scsi_low_alloc_li(ti, lun, 0);
326 if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
329 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
330 cb = TAILQ_NEXT(cb, ccb_chain))
332 if (cb->osdep == osdep)
336 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL;
337 cb = TAILQ_NEXT(cb, ccb_chain))
339 if (cb->osdep == osdep)
346 scsi_low_translate_error_code(cb, tp)
348 struct scsi_low_error_code *tp;
351 if (cb->ccb_error == 0)
352 return tp->error_code;
354 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
356 return tp->error_code;
359 /**************************************************************
360 * SCSI INTERFACE (CAM)
361 **************************************************************/
362 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
363 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
364 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
366 static void scsi_low_poll_cam(struct cam_sim *);
367 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
369 static int scsi_low_attach_cam(struct scsi_low_softc *);
370 static int scsi_low_world_start_cam(struct scsi_low_softc *);
371 static int scsi_low_dettach_cam(struct scsi_low_softc *);
372 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
373 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
374 static void scsi_low_timeout_cam(struct scsi_low_softc *, int, int);
376 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
378 scsi_low_world_start_cam,
379 scsi_low_dettach_cam,
380 scsi_low_ccb_setup_cam,
385 struct scsi_low_error_code scsi_low_error_code_cam[] = {
387 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
388 {SENSEERR, CAM_AUTOSENSE_FAIL},
389 {UACAERR, CAM_SCSI_STATUS_ERROR},
390 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
391 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
392 {TIMEOUTIO, CAM_CMD_TIMEOUT},
393 {PDMAERR, CAM_DATA_RUN_ERR},
394 {PARITYERR, CAM_UNCOR_PARITY},
395 {UBFERR, CAM_UNEXP_BUSFREE},
396 {ABORTIO, CAM_REQ_ABORTED},
397 {-1, CAM_UNREC_HBA_ERROR}
400 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
403 * Please check a polling hz, currently we assume scsi_low_poll() is
406 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
409 scsi_low_poll_cam(sim)
412 struct scsi_low_softc *slp = SIM2SLP(sim);
414 (*slp->sl_funcs->scsi_low_poll) (slp);
416 if (slp->sl_si.si_poll_count ++ >=
417 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
419 slp->sl_si.si_poll_count = 0;
420 scsi_low_timeout_check(slp);
425 scsi_low_scsi_action_cam(sim, ccb)
429 struct scsi_low_softc *slp = SIM2SLP(sim);
430 struct targ_info *ti;
433 u_int lun, flags, msg, target;
436 target = (u_int) (ccb->ccb_h.target_id);
437 lun = (u_int) ccb->ccb_h.target_lun;
439 #ifdef SCSI_LOW_DEBUG
440 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
442 device_printf(slp->sl_dev,
443 "cam_action: func code 0x%x target: %d, lun: %d\n",
444 ccb->ccb_h.func_code, target, lun);
446 #endif /* SCSI_LOW_DEBUG */
448 switch (ccb->ccb_h.func_code) {
449 case XPT_SCSI_IO: /* Execute the requested I/O operation */
450 #ifdef SCSI_LOW_DIAGNOSTIC
451 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
453 device_printf(slp->sl_dev, "invalid target/lun\n");
454 ccb->ccb_h.status = CAM_REQ_INVALID;
458 #endif /* SCSI_LOW_DIAGNOSTIC */
460 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
461 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
466 ti = slp->sl_ti[target];
469 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
470 flags = CCB_AUTOSENSE | CCB_SCSIIO;
475 li = scsi_low_alloc_li(ti, lun, 1);
477 if (ti->ti_setup_msg != 0)
479 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
482 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
484 #ifdef SCSI_LOW_DEBUG
485 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
487 scsi_low_test_abort(slp, ti, li);
489 #endif /* SCSI_LOW_DEBUG */
493 case XPT_EN_LUN: /* Enable LUN as a target */
494 case XPT_TARGET_IO: /* Execute target I/O request */
495 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
496 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
498 ccb->ccb_h.status = CAM_REQ_INVALID;
502 case XPT_ABORT: /* Abort the specified CCB */
503 #ifdef SCSI_LOW_DIAGNOSTIC
504 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
506 device_printf(slp->sl_dev, "invalid target/lun\n");
507 ccb->ccb_h.status = CAM_REQ_INVALID;
511 #endif /* SCSI_LOW_DIAGNOSTIC */
514 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
515 rv = scsi_low_abort_ccb(slp, cb);
519 ccb->ccb_h.status = CAM_REQ_CMP;
521 ccb->ccb_h.status = CAM_REQ_INVALID;
525 case XPT_SET_TRAN_SETTINGS: {
526 struct ccb_trans_settings_scsi *scsi;
527 struct ccb_trans_settings_spi *spi;
528 struct ccb_trans_settings *cts;
531 #ifdef SCSI_LOW_DIAGNOSTIC
532 if (target == CAM_TARGET_WILDCARD)
534 device_printf(slp->sl_dev, "invalid target\n");
535 ccb->ccb_h.status = CAM_REQ_INVALID;
539 #endif /* SCSI_LOW_DIAGNOSTIC */
541 ti = slp->sl_ti[target];
542 if (lun == CAM_LUN_WILDCARD)
546 scsi = &cts->proto_specific.scsi;
547 spi = &cts->xport_specific.spi;
548 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
549 CTS_SPI_VALID_SYNC_RATE |
550 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
552 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
553 val = spi->bus_width;
554 if (val < ti->ti_width)
557 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
558 val = spi->sync_period;
559 if (val == 0 || val > ti->ti_maxsynch.period)
560 ti->ti_maxsynch.period = val;
562 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
563 val = spi->sync_offset;
564 if (val < ti->ti_maxsynch.offset)
565 ti->ti_maxsynch.offset = val;
567 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
568 scsi_low_calcf_target(ti);
571 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
572 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
574 li = scsi_low_alloc_li(ti, lun, 1);
575 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
576 li->li_quirks |= SCSI_LOW_DISK_DISC;
578 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
581 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
582 li->li_quirks |= SCSI_LOW_DISK_QTAG;
584 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
586 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
587 scsi_low_calcf_target(ti);
588 scsi_low_calcf_lun(li);
589 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
590 scsi_low_calcf_show(li);
594 ccb->ccb_h.status = CAM_REQ_CMP;
599 case XPT_GET_TRAN_SETTINGS: {
600 struct ccb_trans_settings *cts;
604 #ifdef SCSI_LOW_DIAGNOSTIC
605 if (target == CAM_TARGET_WILDCARD)
607 device_printf(slp->sl_dev, "invalid target\n");
608 ccb->ccb_h.status = CAM_REQ_INVALID;
612 #endif /* SCSI_LOW_DIAGNOSTIC */
613 ti = slp->sl_ti[target];
614 if (lun == CAM_LUN_WILDCARD)
618 li = scsi_low_alloc_li(ti, lun, 1);
619 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
620 struct ccb_trans_settings_scsi *scsi =
621 &cts->proto_specific.scsi;
622 struct ccb_trans_settings_spi *spi =
623 &cts->xport_specific.spi;
624 #ifdef SCSI_LOW_DIAGNOSTIC
625 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
627 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
628 device_printf(slp->sl_dev,
629 "invalid GET_TRANS_CURRENT_SETTINGS call\n");
632 #endif /* SCSI_LOW_DIAGNOSTIC */
633 cts->protocol = PROTO_SCSI;
634 cts->protocol_version = SCSI_REV_2;
635 cts->transport = XPORT_SPI;
636 cts->transport_version = 2;
638 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
639 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
641 diskflags = li->li_diskflags & li->li_cfgflags;
642 if (diskflags & SCSI_LOW_DISK_DISC)
643 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
644 if (diskflags & SCSI_LOW_DISK_QTAG)
645 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
647 spi->sync_period = ti->ti_maxsynch.period;
648 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
649 spi->sync_offset = ti->ti_maxsynch.offset;
650 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
652 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
653 spi->bus_width = ti->ti_width;
655 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
656 scsi->valid = CTS_SCSI_VALID_TQ;
657 spi->valid |= CTS_SPI_VALID_DISC;
661 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
668 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
669 cam_calc_geometry(&ccb->ccg, /*extended*/1);
674 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
676 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
678 ccb->ccb_h.status = CAM_REQ_CMP;
682 case XPT_TERM_IO: /* Terminate the I/O process */
683 ccb->ccb_h.status = CAM_REQ_INVALID;
687 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
688 #ifdef SCSI_LOW_DIAGNOSTIC
689 if (target == CAM_TARGET_WILDCARD)
691 device_printf(slp->sl_dev, "invalid target\n");
692 ccb->ccb_h.status = CAM_REQ_INVALID;
696 #endif /* SCSI_LOW_DIAGNOSTIC */
698 msg = SCSI_LOW_MSG_RESET;
699 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
701 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
706 ti = slp->sl_ti[target];
707 if (lun == CAM_LUN_WILDCARD)
711 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
712 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
714 flags = CCB_NORETRY | CCB_URGENT;
717 li = scsi_low_alloc_li(ti, lun, 1);
718 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
722 case XPT_PATH_INQ: { /* Path routing inquiry */
723 struct ccb_pathinq *cpi = &ccb->cpi;
725 cpi->version_num = scsi_low_version_major;
726 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
727 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
728 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
729 cpi->hba_inquiry |= PI_WIDE_16;
730 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
731 cpi->hba_inquiry |= PI_WIDE_32;
732 if (ti->ti_maxsynch.offset > 0)
733 cpi->hba_inquiry |= PI_SDTR_ABLE;
734 cpi->target_sprt = 0;
736 cpi->hba_eng_cnt = 0;
737 cpi->max_target = slp->sl_ntargs - 1;
738 cpi->max_lun = slp->sl_nluns - 1;
739 cpi->initiator_id = slp->sl_hostid;
740 cpi->bus_id = cam_sim_bus(sim);
741 cpi->base_transfer_speed = 3300;
742 cpi->transport = XPORT_SPI;
743 cpi->transport_version = 2;
744 cpi->protocol = PROTO_SCSI;
745 cpi->protocol_version = SCSI_REV_2;
746 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
747 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
748 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
749 cpi->unit_number = cam_sim_unit(sim);
750 cpi->ccb_h.status = CAM_REQ_CMP;
756 printf("scsi_low: non support func_code = %d ",
757 ccb->ccb_h.func_code);
758 ccb->ccb_h.status = CAM_REQ_INVALID;
765 scsi_low_attach_cam(slp)
766 struct scsi_low_softc *slp;
768 struct cam_devq *devq;
771 devq = cam_simq_alloc(SCSI_LOW_NCCB);
776 * ask the adapter what subunits are present
778 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
779 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
781 device_get_name(slp->sl_dev), slp,
782 device_get_unit(slp->sl_dev), &Giant,
783 slp->sl_openings, tagged_openings, devq);
785 if (slp->sl_si.sim == NULL) {
790 if (xpt_bus_register(slp->sl_si.sim, NULL, 0) != CAM_SUCCESS) {
791 free(slp->sl_si.sim, M_SCSILOW);
795 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
796 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
797 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
798 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
799 cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
803 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
808 scsi_low_world_start_cam(slp)
809 struct scsi_low_softc *slp;
816 scsi_low_dettach_cam(slp)
817 struct scsi_low_softc *slp;
820 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
821 xpt_free_path(slp->sl_si.path);
822 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
823 cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
828 scsi_low_ccb_setup_cam(slp, cb)
829 struct scsi_low_softc *slp;
832 union ccb *ccb = (union ccb *) cb->osdep;
834 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
836 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
837 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
838 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
839 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
840 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
841 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
842 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
843 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
844 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
848 scsi_low_unit_ready_cmd(cb);
850 return SCSI_LOW_START_QTAG;
854 scsi_low_done_cam(slp, cb)
855 struct scsi_low_softc *slp;
860 ccb = (union ccb *) cb->osdep;
861 if (cb->ccb_error == 0)
863 ccb->ccb_h.status = CAM_REQ_CMP;
868 if (cb->ccb_rcnt >= slp->sl_max_retry)
869 cb->ccb_error |= ABORTIO;
871 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
872 (cb->ccb_error & ABORTIO) == 0)
875 if ((cb->ccb_error & SENSEIO) != 0)
877 memcpy(&ccb->csio.sense_data,
879 sizeof(ccb->csio.sense_data));
882 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
883 &scsi_low_error_code_cam[0]);
885 #ifdef SCSI_LOW_DIAGNOSTIC
886 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
887 cb->ccb_scp.scp_cmdlen > 0 &&
888 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
889 SCSI_LOW_CMD_ABORT_WARNING) != 0)
891 device_printf(slp->sl_dev,
892 "WARNING: scsi_low IO abort\n");
893 scsi_low_print(slp, NULL);
895 #endif /* SCSI_LOW_DIAGNOSTIC */
898 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
899 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
901 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
902 ccb->csio.scsi_status = 0; /* XXX */
904 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
906 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
912 scsi_low_timeout_cam(slp, ch, action)
913 struct scsi_low_softc *slp;
920 case SCSI_LOW_TIMEOUT_CH_IO:
923 case SCSI_LOW_TIMEOUT_START:
924 slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
925 hz / SCSI_LOW_TIMEOUT_HZ);
927 case SCSI_LOW_TIMEOUT_STOP:
928 untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
933 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
936 case SCSI_LOW_TIMEOUT_START:
937 slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
939 case SCSI_LOW_TIMEOUT_STOP:
940 untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
944 case SCSI_LOW_TIMEOUT_CH_RECOVER:
949 /*=============================================================
950 * END OF OS switch (All OS depend fucntions should be above)
951 =============================================================*/
953 /**************************************************************
954 * scsi low deactivate and activate
955 **************************************************************/
957 scsi_low_is_busy(slp)
958 struct scsi_low_softc *slp;
967 scsi_low_deactivate(slp)
968 struct scsi_low_softc *slp;
973 slp->sl_flags |= HW_INACTIVE;
974 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
975 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
976 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
977 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
983 scsi_low_activate(slp)
984 struct scsi_low_softc *slp;
989 slp->sl_flags &= ~HW_INACTIVE;
990 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
992 slp->sl_flags |= HW_INACTIVE;
997 slp->sl_timeout_count = 0;
998 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
999 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1004 /**************************************************************
1006 **************************************************************/
1007 #ifdef SCSI_LOW_DIAGNOSTIC
1008 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
1009 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
1010 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
1013 scsi_low_msg_log_init(slmlp)
1014 struct scsi_low_msg_log *slmlp;
1017 slmlp->slml_ptr = 0;
1021 scsi_low_msg_log_write(slmlp, datap, len)
1022 struct scsi_low_msg_log *slmlp;
1028 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1031 ptr = slmlp->slml_ptr ++;
1032 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1033 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1034 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1035 slmlp->slml_msg[ptr].msg[ind] = 0;
1039 scsi_low_msg_log_show(slmlp, s, len)
1040 struct scsi_low_msg_log *slmlp;
1046 printf("%s: (%d) ", s, slmlp->slml_ptr);
1047 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1049 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1052 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1058 #endif /* SCSI_LOW_DIAGNOSTIC */
1060 /**************************************************************
1062 **************************************************************/
1064 scsi_low_engage(arg)
1067 struct scsi_low_softc *slp = arg;
1070 switch (slp->sl_rstep)
1074 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1075 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1076 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1081 slp->sl_flags &= ~HW_RESUME;
1082 scsi_low_start(slp);
1092 scsi_low_init(slp, flags)
1093 struct scsi_low_softc *slp;
1098 slp->sl_flags |= HW_INITIALIZING;
1100 /* clear power control timeout */
1101 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1103 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1104 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1105 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1107 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1110 /* reset current nexus */
1111 scsi_low_reset_nexus(slp, flags);
1112 if ((slp->sl_flags & HW_INACTIVE) != 0)
1118 if (flags != SCSI_LOW_RESTART_SOFT)
1120 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1124 slp->sl_flags &= ~HW_INITIALIZING;
1128 /**************************************************************
1130 **************************************************************/
1131 static struct lun_info *
1132 scsi_low_alloc_li(ti, lun, alloc)
1133 struct targ_info *ti;
1137 struct scsi_low_softc *slp = ti->ti_sc;
1138 struct lun_info *li;
1140 li = LIST_FIRST(&ti->ti_litab);
1143 if (li->li_lun == lun)
1146 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1148 if (li->li_lun == lun)
1150 LIST_REMOVE(li, lun_chain);
1151 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1160 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1162 panic("no lun info mem");
1164 bzero(li, ti->ti_lunsize);
1168 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1170 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1171 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1172 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1173 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1174 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1176 li->li_qtagbits = (u_int) -1;
1178 TAILQ_INIT(&li->li_discq);
1179 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1181 /* host specific structure initialization per lun */
1182 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1183 (*slp->sl_funcs->scsi_low_lun_init)
1184 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1185 scsi_low_calcf_lun(li);
1189 /**************************************************************
1190 * allocate targ_info
1191 **************************************************************/
1192 static struct targ_info *
1193 scsi_low_alloc_ti(slp, targ)
1194 struct scsi_low_softc *slp;
1197 struct targ_info *ti;
1199 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1200 TAILQ_INIT(&slp->sl_titab);
1202 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1204 panic("%s short of memory", device_get_nameunit(slp->sl_dev));
1206 bzero(ti, slp->sl_targsize);
1210 slp->sl_ti[targ] = ti;
1211 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1212 LIST_INIT(&ti->ti_litab);
1214 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1215 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1216 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1217 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1218 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1219 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1221 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1223 (*slp->sl_funcs->scsi_low_targ_init)
1224 (slp, ti, SCSI_LOW_INFO_ALLOC);
1226 scsi_low_calcf_target(ti);
1231 scsi_low_free_ti(slp)
1232 struct scsi_low_softc *slp;
1234 struct targ_info *ti, *tib;
1235 struct lun_info *li, *nli;
1237 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1239 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1241 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1243 (*slp->sl_funcs->scsi_low_lun_init)
1244 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1246 nli = LIST_NEXT(li, lun_chain);
1250 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1252 (*slp->sl_funcs->scsi_low_targ_init)
1253 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1255 tib = TAILQ_NEXT(ti, ti_chain);
1260 /**************************************************************
1262 **************************************************************/
1264 scsi_low_bus_idle(slp)
1265 struct scsi_low_softc *slp;
1268 slp->sl_retry_sel = 0;
1269 if (slp->sl_Tnexus == NULL)
1270 scsi_low_start(slp);
1274 scsi_low_timeout(arg)
1277 struct scsi_low_softc *slp = arg;
1281 (void) scsi_low_timeout_check(slp);
1282 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1283 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1288 scsi_low_timeout_check(slp)
1289 struct scsi_low_softc *slp;
1291 struct targ_info *ti;
1292 struct lun_info *li;
1293 struct slccb *cb = NULL; /* XXX */
1295 /* selection restart */
1296 if (slp->sl_retry_sel != 0)
1298 slp->sl_retry_sel = 0;
1299 if (slp->sl_Tnexus != NULL)
1302 cb = TAILQ_FIRST(&slp->sl_start);
1306 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1308 cb->ccb_flags |= CCB_NORETRY;
1309 cb->ccb_error |= SELTIMEOUTIO;
1310 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1311 panic("%s: ccb not finished",
1312 device_get_nameunit(slp->sl_dev));
1315 if (slp->sl_Tnexus == NULL)
1316 scsi_low_start(slp);
1319 /* call hardware timeout */
1321 if (slp->sl_funcs->scsi_low_timeout != NULL)
1323 (*slp->sl_funcs->scsi_low_timeout) (slp);
1326 if (slp->sl_timeout_count ++ <
1327 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1330 slp->sl_timeout_count = 0;
1331 if (slp->sl_nio > 0)
1333 if ((cb = slp->sl_Qnexus) != NULL)
1335 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1339 else if (slp->sl_disc == 0)
1341 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1344 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1348 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1349 ti = TAILQ_NEXT(ti, ti_chain))
1351 if (ti->ti_disc == 0)
1354 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1355 li = LIST_NEXT(li, lun_chain))
1357 for (cb = TAILQ_FIRST(&li->li_discq);
1359 cb = TAILQ_NEXT(cb, ccb_chain))
1362 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1370 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1372 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1375 if (slp->sl_active != 0)
1377 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1383 if (slp->sl_powc < 0)
1385 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1386 slp->sl_flags |= HW_POWDOWN;
1387 (*slp->sl_funcs->scsi_low_power)
1388 (slp, SCSI_LOW_POWDOWN);
1394 cb->ccb_error |= TIMEOUTIO;
1395 device_printf(slp->sl_dev, "slccb (0x%lx) timeout!\n", (u_long) cb);
1396 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1397 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1398 scsi_low_start(slp);
1404 scsi_low_abort_ccb(slp, cb)
1405 struct scsi_low_softc *slp;
1408 struct targ_info *ti;
1409 struct lun_info *li;
1414 if ((cb->ccb_omsgoutflag &
1415 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1420 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1421 msg = SCSI_LOW_MSG_ABORT;
1423 msg = SCSI_LOW_MSG_ABORT_QTAG;
1425 cb->ccb_error |= ABORTIO;
1426 cb->ccb_flags |= CCB_NORETRY;
1427 scsi_low_ccb_message_assert(cb, msg);
1429 if (cb == slp->sl_Qnexus)
1431 scsi_low_assert_msg(slp, ti, msg, 1);
1433 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1435 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1436 panic("%s: revoked ccb done",
1437 device_get_nameunit(slp->sl_dev));
1439 cb->ccb_flags |= CCB_STARTQ;
1440 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1442 if (slp->sl_Tnexus == NULL)
1443 scsi_low_start(slp);
1447 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1448 panic("%s: revoked ccb retried",
1449 device_get_nameunit(slp->sl_dev));
1454 /**************************************************************
1455 * Generic SCSI INTERFACE
1456 **************************************************************/
1458 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1459 struct scsi_low_softc *slp;
1460 int openings, ntargs, nluns, targsize, lunsize;
1462 struct targ_info *ti;
1463 struct lun_info *li;
1466 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
1468 if (slp->sl_osdep_fp == NULL)
1469 panic("scsi_low: interface not spcified");
1471 if (ntargs > SCSI_LOW_NTARGETS)
1473 printf("scsi_low: %d targets are too large\n", ntargs);
1474 printf("change kernel options SCSI_LOW_NTARGETS");
1479 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1481 slp->sl_openings = openings;
1482 slp->sl_ntargs = ntargs;
1483 slp->sl_nluns = nluns;
1484 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1486 if (lunsize < sizeof(struct lun_info))
1487 lunsize = sizeof(struct lun_info);
1489 if (targsize < sizeof(struct targ_info))
1490 targsize = sizeof(struct targ_info);
1492 slp->sl_targsize = targsize;
1493 for (i = 0; i < ntargs; i ++)
1495 ti = scsi_low_alloc_ti(slp, i);
1496 ti->ti_lunsize = lunsize;
1497 li = scsi_low_alloc_li(ti, 0, 1);
1500 /* initialize queue */
1501 nccb = openings * ntargs;
1502 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1503 nccb = SCSI_LOW_NCCB;
1504 scsi_low_init_ccbque(nccb);
1505 TAILQ_INIT(&slp->sl_start);
1507 /* call os depend attach */
1509 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
1513 device_printf(slp->sl_dev,
1514 "scsi_low_attach: osdep attach failed\n");
1518 /* check hardware */
1519 DELAY(1000); /* wait for 1ms */
1520 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1523 device_printf(slp->sl_dev,
1524 "scsi_low_attach: initialization failed\n");
1528 /* start watch dog */
1529 slp->sl_timeout_count = 0;
1530 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1531 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1532 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1535 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1537 #ifdef SCSI_LOW_START_UP_CHECK
1538 /* probing devices */
1539 scsi_low_start_up(slp);
1540 #endif /* SCSI_LOW_START_UP_CHECK */
1542 /* call os depend attach done*/
1543 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
1549 scsi_low_dettach(slp)
1550 struct scsi_low_softc *slp;
1555 if (scsi_low_is_busy(slp) != 0)
1561 scsi_low_deactivate(slp);
1563 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
1570 scsi_low_free_ti(slp);
1571 LIST_REMOVE(slp, sl_chain);
1576 /**************************************************************
1578 **************************************************************/
1580 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
1581 struct scsi_low_softc *slp;
1582 struct targ_info *ti;
1583 struct lun_info *li;
1591 scsi_low_ccb_message_assert(cb, msg);
1593 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1594 scsi_low_alloc_qtag(cb);
1596 cb->ccb_flags = flags | CCB_STARTQ;
1597 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1598 cb->ccb_error |= PENDINGIO;
1600 if ((flags & CCB_URGENT) != 0)
1602 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1606 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1611 if (slp->sl_Tnexus == NULL)
1612 scsi_low_start(slp);
1617 scsi_low_message_enqueue(slp, ti, li, flags)
1618 struct scsi_low_softc *slp;
1619 struct targ_info *ti;
1620 struct lun_info *li;
1626 tmsgflags = ti->ti_setup_msg;
1627 ti->ti_setup_msg = 0;
1629 flags |= CCB_NORETRY;
1630 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1635 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1639 /**************************************************************
1640 * Generic Start & Done
1641 **************************************************************/
1642 #define SLSC_MODE_SENSE_SHORT 0x1a
1643 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1644 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1645 sizeof(struct scsi_low_mode_sense_data), 0};
1646 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1647 sizeof(struct scsi_low_inq_data), 0};
1648 static u_int8_t unit_ready_cmd[6];
1649 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1650 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1651 static int scsi_low_resume(struct scsi_low_softc *);
1654 scsi_low_unit_ready_cmd(cb)
1658 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1659 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1660 cb->ccb_scp.scp_datalen = 0;
1661 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1666 scsi_low_sense_abort_start(slp, ti, li, cb)
1667 struct scsi_low_softc *slp;
1668 struct targ_info *ti;
1669 struct lun_info *li;
1673 cb->ccb_scp.scp_cmdlen = 6;
1674 bzero(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1675 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1676 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1677 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1678 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1679 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1680 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1682 scsi_low_ccb_message_clear(cb);
1683 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1685 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1689 bzero(&cb->ccb_sense, sizeof(cb->ccb_sense));
1690 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1691 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1692 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1695 return SCSI_LOW_START_NO_QTAG;
1699 scsi_low_setup_start(slp, ti, li, cb)
1700 struct scsi_low_softc *slp;
1701 struct targ_info *ti;
1702 struct lun_info *li;
1706 switch(li->li_state)
1708 case SCSI_LOW_LUN_SLEEP:
1709 scsi_low_unit_ready_cmd(cb);
1712 case SCSI_LOW_LUN_START:
1713 cb->ccb_scp.scp_cmd = ss_cmd;
1714 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1715 cb->ccb_scp.scp_datalen = 0;
1716 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1720 case SCSI_LOW_LUN_INQ:
1721 cb->ccb_scp.scp_cmd = inq_cmd;
1722 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1723 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1724 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1725 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1729 case SCSI_LOW_LUN_MODEQ:
1730 cb->ccb_scp.scp_cmd = sms_cmd;
1731 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1732 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1733 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1734 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1736 return SCSI_LOW_START_QTAG;
1739 panic("%s: no setup phase", device_get_nameunit(slp->sl_dev));
1742 return SCSI_LOW_START_NO_QTAG;
1746 scsi_low_resume(slp)
1747 struct scsi_low_softc *slp;
1750 if (slp->sl_flags & HW_RESUME)
1752 slp->sl_flags &= ~HW_POWDOWN;
1753 if (slp->sl_funcs->scsi_low_power != NULL)
1755 slp->sl_flags |= HW_RESUME;
1757 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1758 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1759 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
1760 SCSI_LOW_TIMEOUT_START);
1768 struct scsi_low_softc *slp;
1770 struct targ_info *ti;
1771 struct lun_info *li;
1775 /* check hardware exists or under initializations ? */
1776 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1779 /* check hardware power up ? */
1780 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1783 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1785 if (scsi_low_resume(slp) == EJUSTRETURN)
1791 #ifdef SCSI_LOW_DIAGNOSTIC
1792 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1794 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
1795 panic("%s: inconsistent", device_get_nameunit(slp->sl_dev));
1797 #endif /* SCSI_LOW_DIAGNOSTIC */
1799 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
1800 cb = TAILQ_NEXT(cb, ccb_chain))
1804 if (li->li_disc == 0)
1806 goto scsi_low_cmd_start;
1808 else if (li->li_nqio > 0)
1810 if (li->li_nqio < li->li_maxnqio ||
1811 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1812 goto scsi_low_cmd_start;
1818 cb->ccb_flags &= ~CCB_STARTQ;
1819 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1822 /* clear all error flag bits (for restart) */
1824 cb->ccb_datalen = -1;
1825 cb->ccb_scp.scp_status = ST_UNKNOWN;
1827 /* setup nexus pointer */
1828 slp->sl_Qnexus = cb;
1829 slp->sl_Lnexus = li;
1830 slp->sl_Tnexus = ti;
1832 /* initialize msgsys */
1833 scsi_low_init_msgsys(slp, ti);
1836 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1838 /* CA state or forced abort */
1839 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1841 else if (li->li_state >= SCSI_LOW_LUN_OK)
1843 cb->ccb_flags &= ~CCB_INTERNAL;
1844 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
1845 if (cb->ccb_msgoutflag != 0)
1847 scsi_low_ccb_message_exec(slp, cb);
1852 cb->ccb_flags |= CCB_INTERNAL;
1853 rv = scsi_low_setup_start(slp, ti, li, cb);
1857 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1859 if (rv == SCSI_LOW_START_QTAG &&
1860 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1865 scsi_low_activate_qtag(cb);
1866 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1867 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1868 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1869 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1870 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1872 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1873 scsi_low_assert_msg(slp, ti, qmsg, 0);
1877 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1878 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1879 cb->ccb_tc = cb->ccb_tcmax;
1881 /* setup saved scsi data pointer */
1882 cb->ccb_sscp = cb->ccb_scp;
1884 /* setup current scsi pointer */
1885 slp->sl_scp = cb->ccb_sscp;
1886 slp->sl_error = cb->ccb_error;
1888 /* assert always an identify msg */
1889 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1892 #ifdef SCSI_LOW_DIAGNOSTIC
1893 scsi_low_msg_log_init(&ti->ti_log_msgin);
1894 scsi_low_msg_log_init(&ti->ti_log_msgout);
1895 #endif /* SCSI_LOW_DIAGNOSTIC */
1897 /* selection start */
1899 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1900 if (rv == SCSI_LOW_START_OK)
1902 #ifdef SCSI_LOW_STATICS
1903 scsi_low_statics.nexus_win ++;
1904 #endif /* SCSI_LOW_STATICS */
1908 scsi_low_arbit_fail(slp, cb);
1909 #ifdef SCSI_LOW_STATICS
1910 scsi_low_statics.nexus_fail ++;
1911 #endif /* SCSI_LOW_STATICS */
1915 scsi_low_arbit_fail(slp, cb)
1916 struct scsi_low_softc *slp;
1919 struct targ_info *ti = cb->ti;
1921 scsi_low_deactivate_qtag(cb);
1922 scsi_low_ccb_message_retry(cb);
1923 cb->ccb_flags |= CCB_STARTQ;
1924 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1926 scsi_low_bus_release(slp, ti);
1929 if (slp->sl_disc == 0)
1931 #ifdef SCSI_LOW_DIAGNOSTIC
1932 device_printf(slp->sl_dev, "try selection again\n");
1933 #endif /* SCSI_LOW_DIAGNOSTIC */
1934 slp->sl_retry_sel = 1;
1939 scsi_low_bus_release(slp, ti)
1940 struct scsi_low_softc *slp;
1941 struct targ_info *ti;
1944 if (ti->ti_disc > 0)
1946 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1950 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1953 /* clear all nexus pointer */
1954 slp->sl_Qnexus = NULL;
1955 slp->sl_Lnexus = NULL;
1956 slp->sl_Tnexus = NULL;
1958 /* clear selection assert */
1959 slp->sl_selid = NULL;
1961 /* clear nexus data */
1962 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1964 /* clear phase change counter */
1965 slp->sl_ph_count = 0;
1969 scsi_low_setup_done(slp, cb)
1970 struct scsi_low_softc *slp;
1973 struct targ_info *ti;
1974 struct lun_info *li;
1979 if (cb->ccb_rcnt >= slp->sl_max_retry)
1981 cb->ccb_error |= ABORTIO;
1982 return SCSI_LOW_DONE_COMPLETE;
1985 /* XXX: special huck for selection timeout */
1986 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1987 (cb->ccb_error & SELTIMEOUTIO) != 0)
1989 cb->ccb_error |= ABORTIO;
1990 return SCSI_LOW_DONE_COMPLETE;
1993 switch(li->li_state)
1995 case SCSI_LOW_LUN_INQ:
1996 if (cb->ccb_error != 0)
1999 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2003 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2005 else if ((li->li_inq.sd_version & 7) >= 2 ||
2006 (li->li_inq.sd_len >= 4))
2008 if ((li->li_inq.sd_support & 0x2) == 0)
2009 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2010 if ((li->li_inq.sd_support & 0x8) == 0)
2011 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2014 if ((li->li_inq.sd_support & 0x10) == 0)
2015 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2016 if ((li->li_inq.sd_support & 0x20) == 0)
2017 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2018 if ((li->li_inq.sd_support & 0x40) == 0)
2019 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2024 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2027 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2029 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2031 scsi_low_calcf_target(ti);
2032 scsi_low_calcf_lun(li);
2035 case SCSI_LOW_LUN_MODEQ:
2036 if (cb->ccb_error != 0)
2038 if (cb->ccb_error & SENSEIO)
2040 #ifdef SCSI_LOW_DEBUG
2041 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2043 int error_code, sense_key, asc, ascq;
2045 scsi_extract_sense(&cb->ccb_sense,
2050 printf("SENSE: [%x][%x][%x][%x]\n",
2051 error_code, sense_key, asc,
2054 #endif /* SCSI_LOW_DEBUG */
2058 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2061 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2063 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2064 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2066 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2067 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2068 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2070 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2071 scsi_low_calcf_lun(li);
2079 if (li->li_state == SCSI_LOW_LUN_OK)
2081 scsi_low_calcf_target(ti);
2082 scsi_low_calcf_lun(li);
2083 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2084 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2086 scsi_low_calcf_show(li);
2091 return SCSI_LOW_DONE_RETRY;
2095 scsi_low_done(slp, cb)
2096 struct scsi_low_softc *slp;
2101 if (cb->ccb_error == 0)
2103 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2105 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2107 * SCSI-2 draft suggests
2108 * page 0x0a QErr bit determins if
2109 * the target aborts or continues
2110 * the queueing io's after CA state resolved.
2111 * However many targets seem not to support
2112 * the page 0x0a. Thus we should manually clear the
2113 * queuing io's after CA state.
2115 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2118 cb->ccb_flags |= CCB_CLEARQ;
2121 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2123 if ((cb->ccb_flags & CCB_SENSE) != 0)
2124 cb->ccb_error |= (SENSEIO | ABORTIO);
2125 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2127 else switch (cb->ccb_sscp.scp_status)
2133 if (cb->ccb_datalen == 0 ||
2134 cb->ccb_scp.scp_datalen == 0)
2137 if (cb->ccb_scp.scp_cmdlen > 0 &&
2138 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2139 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2142 cb->ccb_error |= PDMAERR;
2147 cb->ccb_error |= (BUSYERR | STATERR);
2151 cb->ccb_error |= (STATERR | ABORTIO);
2156 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2159 cb->ccb_flags |= CCB_SENSE;
2162 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2167 cb->ccb_error |= FATALIO;
2173 if (cb->ccb_flags & CCB_SENSE)
2175 cb->ccb_error |= (SENSEERR | ABORTIO);
2177 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2181 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2183 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2187 /* check a ccb msgout flag */
2188 if (cb->ccb_omsgoutflag != 0)
2190 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2191 SCSI_LOW_MSG_ABORT_QTAG | \
2192 SCSI_LOW_MSG_CLEAR_QTAG | \
2193 SCSI_LOW_MSG_TERMIO)
2195 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2197 cb->ccb_error |= ABORTIO;
2201 /* call OS depend done */
2202 if (cb->osdep != NULL)
2204 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2205 if (rv == EJUSTRETURN)
2208 else if (cb->ccb_error != 0)
2210 if (cb->ccb_rcnt >= slp->sl_max_retry)
2211 cb->ccb_error |= ABORTIO;
2213 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2214 (cb->ccb_error & ABORTIO) == 0)
2218 /* free our target */
2219 #ifdef SCSI_LOW_DEBUG
2220 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2222 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2223 scsi_low_print(slp, NULL);
2225 #endif /* SCSI_LOW_DEBUG */
2227 scsi_low_deactivate_qtag(cb);
2228 scsi_low_dealloc_qtag(cb);
2229 scsi_low_free_ccb(cb);
2231 return SCSI_LOW_DONE_COMPLETE;
2234 #ifdef SCSI_LOW_DEBUG
2235 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2237 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2238 scsi_low_print(slp, NULL);
2240 #endif /* SCSI_LOW_DEBUG */
2243 scsi_low_deactivate_qtag(cb);
2244 scsi_low_ccb_message_retry(cb);
2245 return SCSI_LOW_DONE_RETRY;
2248 /**************************************************************
2250 **************************************************************/
2252 scsi_low_reset_nexus_target(slp, ti, fdone)
2253 struct scsi_low_softc *slp;
2254 struct targ_info *ti;
2257 struct lun_info *li;
2259 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2260 li = LIST_NEXT(li, lun_chain))
2262 scsi_low_reset_nexus_lun(slp, li, fdone);
2263 li->li_state = SCSI_LOW_LUN_SLEEP;
2268 ti->ti_setup_msg = 0;
2269 ti->ti_setup_msg_done = 0;
2271 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2272 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2274 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2275 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2277 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2279 ((*slp->sl_funcs->scsi_low_targ_init)
2280 (slp, ti, SCSI_LOW_INFO_REVOKE));
2282 scsi_low_calcf_target(ti);
2284 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2285 li = LIST_NEXT(li, lun_chain))
2289 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2290 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2292 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2294 ((*slp->sl_funcs->scsi_low_lun_init)
2295 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2297 scsi_low_calcf_lun(li);
2302 scsi_low_reset_nexus(slp, fdone)
2303 struct scsi_low_softc *slp;
2306 struct targ_info *ti;
2307 struct slccb *cb, *topcb;
2309 if ((cb = slp->sl_Qnexus) != NULL)
2311 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2318 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2319 ti = TAILQ_NEXT(ti, ti_chain))
2321 scsi_low_reset_nexus_target(slp, ti, fdone);
2322 scsi_low_bus_release(slp, ti);
2323 scsi_low_init_msgsys(slp, ti);
2328 topcb->ccb_flags |= CCB_STARTQ;
2329 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2333 slp->sl_retry_sel = 0;
2334 slp->sl_flags &= ~HW_PDMASTART;
2339 static char tw_chars[] = "|/-\\";
2340 #define TWIDDLEWAIT 10000
2343 scsi_low_twiddle_wait(void)
2347 cnputc(tw_chars[tw_pos++]);
2348 tw_pos %= (sizeof(tw_chars) - 1);
2353 scsi_low_bus_reset(slp)
2354 struct scsi_low_softc *slp;
2358 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2360 device_printf(slp->sl_dev, "try to reset scsi bus ");
2361 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2362 scsi_low_twiddle_wait();
2368 scsi_low_restart(slp, flags, s)
2369 struct scsi_low_softc *slp;
2376 device_printf(slp->sl_dev, "scsi bus restart. reason: %s\n", s);
2378 if ((error = scsi_low_init(slp, flags)) != 0)
2381 scsi_low_start(slp);
2385 /**************************************************************
2386 * disconnect and reselect
2387 **************************************************************/
2388 #define MSGCMD_LUN(msg) (msg & 0x07)
2390 static struct slccb *
2391 scsi_low_establish_ccb(ti, li, tag)
2392 struct targ_info *ti;
2393 struct lun_info *li;
2396 struct scsi_low_softc *slp = ti->ti_sc;
2402 cb = TAILQ_FIRST(&li->li_discq);
2403 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2404 if (cb->ccb_tag == tag)
2409 * establish our ccb nexus
2412 #ifdef SCSI_LOW_DEBUG
2413 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2415 device_printf(slp->sl_dev, "nexus(0x%lx) abort check start\n",
2417 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2418 scsi_low_revoke_ccb(slp, cb, 1);
2422 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2424 if (cb->ccb_omsgoutflag == 0)
2425 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2427 #endif /* SCSI_LOW_DEBUG */
2429 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2430 cb->ccb_flags &= ~CCB_DISCQ;
2431 slp->sl_Qnexus = cb;
2433 slp->sl_scp = cb->ccb_sscp;
2434 slp->sl_error |= cb->ccb_error;
2440 /* inform "ccb nexus established" to the host driver */
2441 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2444 if (cb->ccb_msgoutflag != 0)
2446 scsi_low_ccb_message_exec(slp, cb);
2453 scsi_low_reselected(slp, targ)
2454 struct scsi_low_softc *slp;
2457 struct targ_info *ti;
2462 * Check select vs reselected collision.
2465 if ((cb = slp->sl_selid) != NULL)
2467 scsi_low_arbit_fail(slp, cb);
2468 #ifdef SCSI_LOW_STATICS
2469 scsi_low_statics.nexus_conflict ++;
2470 #endif /* SCSI_LOW_STATICS */
2474 * Check if no current active nexus.
2476 if (slp->sl_Tnexus != NULL)
2483 * Check a valid target id asserted ?
2485 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2487 s = "scsi id illegal";
2492 * Check the target scsi status.
2494 ti = slp->sl_ti[targ];
2495 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2497 s = "phase mismatch";
2505 scsi_low_init_msgsys(slp, ti);
2508 * Establish our target nexus
2510 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2511 slp->sl_Tnexus = ti;
2512 #ifdef SCSI_LOW_STATICS
2513 scsi_low_statics.nexus_reselected ++;
2514 #endif /* SCSI_LOW_STATICS */
2518 device_printf(slp->sl_dev, "reselect(%x:unknown) %s\n", targ, s);
2519 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2520 "reselect: scsi world confused");
2524 /**************************************************************
2525 * cmd out pointer setup
2526 **************************************************************/
2528 scsi_low_cmd(slp, ti)
2529 struct scsi_low_softc *slp;
2530 struct targ_info *ti;
2532 struct slccb *cb = slp->sl_Qnexus;
2534 slp->sl_ph_count ++;
2540 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2541 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2542 slp->sl_scp.scp_datalen = 0;
2543 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2544 slp->sl_error |= FATALIO;
2545 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2546 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2551 #ifdef SCSI_LOW_DEBUG
2552 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2554 scsi_low_test_cmdlnk(slp, cb);
2556 #endif /* SCSI_LOW_DEBUG */
2561 /**************************************************************
2562 * data out pointer setup
2563 **************************************************************/
2565 scsi_low_data(slp, ti, bp, direction)
2566 struct scsi_low_softc *slp;
2567 struct targ_info *ti;
2571 struct slccb *cb = slp->sl_Qnexus;
2573 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2579 slp->sl_error |= (FATALIO | PDMAERR);
2580 slp->sl_scp.scp_datalen = 0;
2581 slp->sl_scp.scp_direction = direction;
2582 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2583 if (ti->ti_ophase != ti->ti_phase)
2588 s = "DATA PHASE: ccb nexus not found";
2590 s = "DATA PHASE: xfer direction mismatch";
2591 SCSI_LOW_INFO(slp, ti, s);
2598 /**************************************************************
2600 **************************************************************/
2601 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2602 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2603 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2604 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2605 #define MSGIN_DATA_LAST 0x30
2607 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
2608 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
2609 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
2610 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
2612 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
2613 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
2614 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
2615 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
2616 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
2617 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
2618 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
2620 struct scsi_low_msgout_data {
2623 int (*md_msgfunc)(struct scsi_low_softc *);
2624 int (*md_errfunc)(struct scsi_low_softc *, u_int);
2625 #define MSG_RELEASE_ATN 0x0001
2629 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2630 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2631 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2632 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2633 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2634 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2635 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2636 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2637 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2638 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2639 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2640 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2641 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2642 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2643 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2644 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2645 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2648 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
2649 static int scsi_low_synch(struct scsi_low_softc *);
2650 static int scsi_low_wide(struct scsi_low_softc *);
2651 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
2652 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
2653 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
2654 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
2655 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
2656 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
2657 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
2658 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
2659 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
2660 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
2661 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
2663 struct scsi_low_msgin_data {
2665 int (*md_msgfunc)(struct scsi_low_softc *);
2668 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2669 /* 0 */ {1, scsi_low_msginfunc_cc},
2670 /* 1 */ {2, scsi_low_msginfunc_ext},
2671 /* 2 */ {1, scsi_low_msginfunc_sdp},
2672 /* 3 */ {1, scsi_low_msginfunc_rp},
2673 /* 4 */ {1, scsi_low_msginfunc_disc},
2674 /* 5 */ {1, scsi_low_msginfunc_rejop},
2675 /* 6 */ {1, scsi_low_msginfunc_rejop},
2676 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2677 /* 8 */ {1, scsi_low_msginfunc_noop},
2678 /* 9 */ {1, scsi_low_msginfunc_parity},
2679 /* a */ {1, scsi_low_msginfunc_lcc},
2680 /* b */ {1, scsi_low_msginfunc_lcc},
2681 /* c */ {1, scsi_low_msginfunc_rejop},
2682 /* d */ {2, scsi_low_msginfunc_rejop},
2683 /* e */ {1, scsi_low_msginfunc_rejop},
2684 /* f */ {1, scsi_low_msginfunc_rejop},
2685 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2686 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2687 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2688 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2689 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2690 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2691 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2692 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2693 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2694 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2695 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2696 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2697 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2698 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2699 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2700 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2701 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2702 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2703 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2704 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2705 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2706 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2707 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2708 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2709 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2710 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2711 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2712 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2713 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2714 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2715 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2716 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2717 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2720 /**************************************************************
2722 **************************************************************/
2724 scsi_low_msgfunc_synch(slp)
2725 struct scsi_low_softc *slp;
2727 struct targ_info *ti = slp->sl_Tnexus;
2728 int ptr = ti->ti_msgoutlen;
2730 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2731 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2732 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2733 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2734 return MSG_EXTEND_SYNCHLEN + 2;
2738 scsi_low_msgfunc_wide(slp)
2739 struct scsi_low_softc *slp;
2741 struct targ_info *ti = slp->sl_Tnexus;
2742 int ptr = ti->ti_msgoutlen;
2744 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2745 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2746 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2747 return MSG_EXTEND_WIDELEN + 2;
2751 scsi_low_msgfunc_identify(slp)
2752 struct scsi_low_softc *slp;
2754 struct targ_info *ti = slp->sl_Tnexus;
2755 struct lun_info *li = slp->sl_Lnexus;
2756 struct slccb *cb = slp->sl_Qnexus;
2757 int ptr = ti->ti_msgoutlen;
2763 slp->sl_error |= FATALIO;
2764 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2765 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2769 if (scsi_low_is_disconnect_ok(cb) != 0)
2770 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2774 if (ti->ti_phase == PH_MSGOUT)
2776 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2777 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2779 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2783 ti->ti_msgoutstr[ptr + 0] = msg;
2788 scsi_low_msgfunc_abort(slp)
2789 struct scsi_low_softc *slp;
2792 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2797 scsi_low_msgfunc_qabort(slp)
2798 struct scsi_low_softc *slp;
2801 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2806 scsi_low_msgfunc_reset(slp)
2807 struct scsi_low_softc *slp;
2810 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2815 scsi_low_msgfunc_qtag(slp)
2816 struct scsi_low_softc *slp;
2818 struct targ_info *ti = slp->sl_Tnexus;
2819 struct slccb *cb = slp->sl_Qnexus;
2820 int ptr = ti->ti_msgoutlen;
2822 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2824 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2829 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2830 if (ti->ti_phase == PH_MSGOUT)
2832 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2839 * The following functions are called when targets give unexpected
2840 * responces in msgin (after msgout).
2843 scsi_low_errfunc_identify(slp, msgflags)
2844 struct scsi_low_softc *slp;
2848 if (slp->sl_Lnexus != NULL)
2850 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2851 scsi_low_calcf_lun(slp->sl_Lnexus);
2857 scsi_low_errfunc_synch(slp, msgflags)
2858 struct scsi_low_softc *slp;
2861 struct targ_info *ti = slp->sl_Tnexus;
2863 MSGIN_PERIOD(ti) = 0;
2864 MSGIN_OFFSET(ti) = 0;
2865 scsi_low_synch(slp);
2870 scsi_low_errfunc_wide(slp, msgflags)
2871 struct scsi_low_softc *slp;
2874 struct targ_info *ti = slp->sl_Tnexus;
2876 MSGIN_WIDTHP(ti) = 0;
2882 scsi_low_errfunc_qtag(slp, msgflags)
2883 struct scsi_low_softc *slp;
2887 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2889 if (slp->sl_Qnexus != NULL)
2891 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2893 if (slp->sl_Lnexus != NULL)
2895 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2896 scsi_low_calcf_lun(slp->sl_Lnexus);
2898 device_printf(slp->sl_dev, "scsi_low: qtag msg rejected\n");
2905 scsi_low_msgout(slp, ti, fl)
2906 struct scsi_low_softc *slp;
2907 struct targ_info *ti;
2910 struct scsi_low_msgout_data *mdp;
2913 #ifdef SCSI_LOW_DIAGNOSTIC
2914 if (ti != slp->sl_Tnexus)
2916 scsi_low_print(slp, NULL);
2917 panic("scsi_low_msgout: Target nexus inconsistent");
2919 #endif /* SCSI_LOW_DIAGNOSTIC */
2921 slp->sl_ph_count ++;
2922 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2924 device_printf(slp->sl_dev, "too many phase changes\n");
2925 slp->sl_error |= FATALIO;
2926 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2930 * Scsi phase changes.
2931 * Previously msgs asserted are accepted by our target or
2932 * processed by scsi_low_msgin.
2933 * Thus clear all saved informations.
2935 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2937 ti->ti_omsgflags = 0;
2938 ti->ti_emsgflags = 0;
2940 else if (slp->sl_atten == 0)
2943 * We did not assert attention, however still our target required
2944 * msgs. Resend previous msgs.
2946 ti->ti_msgflags |= ti->ti_omsgflags;
2947 ti->ti_omsgflags = 0;
2948 #ifdef SCSI_LOW_DIAGNOSTIC
2949 device_printf(slp->sl_dev, "scsi_low_msgout: retry msgout\n");
2950 #endif /* SCSI_LOW_DIAGNOSTIC */
2954 * We have no msgs. send MSG_NOOP (OK?)
2956 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2957 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2962 ti->ti_msgoutlen = 0;
2963 slp->sl_clear_atten = 0;
2964 mdp = &scsi_low_msgout_data[0];
2965 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2967 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2969 ti->ti_omsgflags |= mdp->md_flags;
2970 ti->ti_msgflags &= ~mdp->md_flags;
2971 ti->ti_emsgflags = mdp->md_flags;
2973 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2974 if (mdp->md_msgfunc != NULL)
2975 len = (*mdp->md_msgfunc) (slp);
2979 #ifdef SCSI_LOW_DIAGNOSTIC
2980 scsi_low_msg_log_write(&ti->ti_log_msgout,
2981 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2982 #endif /* SCSI_LOW_DIAGNOSTIC */
2984 ti->ti_msgoutlen += len;
2985 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2987 slp->sl_clear_atten = 1;
2991 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2992 ti->ti_msgflags == 0)
2995 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
3000 if (scsi_low_is_msgout_continue(ti, 0) == 0)
3001 slp->sl_clear_atten = 1;
3003 return ti->ti_msgoutlen;
3006 /**************************************************************
3008 **************************************************************/
3010 scsi_low_msginfunc_noop(slp)
3011 struct scsi_low_softc *slp;
3018 scsi_low_msginfunc_rejop(slp)
3019 struct scsi_low_softc *slp;
3021 struct targ_info *ti = slp->sl_Tnexus;
3022 u_int8_t msg = ti->ti_msgin[0];
3024 device_printf(slp->sl_dev, "MSGIN: msg 0x%x rejected\n", (u_int) msg);
3025 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3030 scsi_low_msginfunc_cc(slp)
3031 struct scsi_low_softc *slp;
3033 struct lun_info *li;
3035 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3037 /* validate status */
3038 if (slp->sl_Qnexus == NULL)
3041 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3042 li = slp->sl_Lnexus;
3043 switch (slp->sl_scp.scp_status)
3046 li->li_maxnqio = li->li_maxnexus;
3051 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3052 scsi_low_reset_nexus_lun(slp, li, 0);
3060 if (li->li_maxnexus >= li->li_nqio)
3061 li->li_maxnexus = li->li_nqio - 1;
3062 li->li_maxnqio = li->li_maxnexus;
3067 slp->sl_error |= MSGERR;
3077 scsi_low_msginfunc_lcc(slp)
3078 struct scsi_low_softc *slp;
3080 struct targ_info *ti;
3081 struct lun_info *li;
3082 struct slccb *ncb, *cb;
3084 ti = slp->sl_Tnexus;
3085 li = slp->sl_Lnexus;
3086 if ((cb = slp->sl_Qnexus) == NULL)
3089 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3090 switch (slp->sl_scp.scp_status)
3094 li->li_maxnqio = li->li_maxnexus;
3098 slp->sl_error |= MSGERR;
3102 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3105 cb->ccb_error |= slp->sl_error;
3106 if (cb->ccb_error != 0)
3109 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3110 ncb = TAILQ_NEXT(ncb, ccb_chain))
3113 goto cmd_link_start;
3118 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3119 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3123 ncb->ccb_flags &= ~CCB_STARTQ;
3124 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3126 scsi_low_dealloc_qtag(ncb);
3127 ncb->ccb_tag = cb->ccb_tag;
3128 ncb->ccb_otag = cb->ccb_otag;
3129 cb->ccb_tag = SCSI_LOW_UNKTAG;
3130 cb->ccb_otag = SCSI_LOW_UNKTAG;
3131 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3132 panic("%s: linked ccb retried",
3133 device_get_nameunit(slp->sl_dev));
3135 slp->sl_Qnexus = ncb;
3136 slp->sl_ph_count = 0;
3139 ncb->ccb_datalen = -1;
3140 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3141 ncb->ccb_flags &= ~CCB_INTERNAL;
3143 scsi_low_init_msgsys(slp, ti);
3145 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3147 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3148 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3149 ncb->ccb_tc = ncb->ccb_tcmax;
3151 /* setup saved scsi data pointer */
3152 ncb->ccb_sscp = ncb->ccb_scp;
3153 slp->sl_scp = ncb->ccb_sscp;
3154 slp->sl_error = ncb->ccb_error;
3156 #ifdef SCSI_LOW_DIAGNOSTIC
3157 scsi_low_msg_log_init(&ti->ti_log_msgin);
3158 scsi_low_msg_log_init(&ti->ti_log_msgout);
3159 #endif /* SCSI_LOW_DIAGNOSTIC */
3164 scsi_low_msginfunc_disc(slp)
3165 struct scsi_low_softc *slp;
3168 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3173 scsi_low_msginfunc_sdp(slp)
3174 struct scsi_low_softc *slp;
3176 struct slccb *cb = slp->sl_Qnexus;
3180 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3181 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3184 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3189 scsi_low_msginfunc_rp(slp)
3190 struct scsi_low_softc *slp;
3193 if (slp->sl_Qnexus != NULL)
3194 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3196 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3202 struct scsi_low_softc *slp;
3204 struct targ_info *ti = slp->sl_Tnexus;
3205 u_int period = 0, offset = 0, speed;
3209 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3210 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3211 MSGIN_OFFSET(ti) == 0)
3213 if ((offset = MSGIN_OFFSET(ti)) != 0)
3214 period = MSGIN_PERIOD(ti);
3215 s = offset ? "synchronous" : "async";
3220 * Target seems to be brain damaged.
3221 * Force async transfer.
3223 ti->ti_maxsynch.period = 0;
3224 ti->ti_maxsynch.offset = 0;
3225 device_printf(slp->sl_dev,
3226 "target brain damaged. async transfer\n");
3230 ti->ti_maxsynch.period = period;
3231 ti->ti_maxsynch.offset = offset;
3233 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3237 * Current period and offset are not acceptable
3239 * The adapter changes max synch and max offset.
3241 device_printf(slp->sl_dev,
3242 "synch neg failed. retry synch msg neg ...\n");
3246 ti->ti_osynch = ti->ti_maxsynch;
3249 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3253 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3255 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3256 struct slccb *cb = slp->sl_Qnexus;
3258 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3260 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3262 device_printf(slp->sl_dev,
3263 "(%d:*): <%s> offset %d period %dns ",
3264 ti->ti_id, s, offset, period * 4);
3268 speed = 1000 * 10 / (period * 4);
3269 printf("%d.%d M/s", speed / 10, speed % 10);
3278 struct scsi_low_softc *slp;
3280 struct targ_info *ti = slp->sl_Tnexus;
3283 ti->ti_width = MSGIN_WIDTHP(ti);
3284 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3288 * Current width is not acceptable for our adapter.
3289 * The adapter changes max width.
3291 device_printf(slp->sl_dev,
3292 "wide neg failed. retry wide msg neg ...\n");
3296 ti->ti_owidth = ti->ti_width;
3297 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3299 ti->ti_setup_msg_done |=
3300 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3304 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3306 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3307 struct slccb *cb = slp->sl_Qnexus;
3309 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3311 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3313 device_printf(slp->sl_dev, "(%d:*): transfer width %d bits\n",
3314 ti->ti_id, 1 << (3 + ti->ti_width));
3320 scsi_low_msginfunc_simple_qtag(slp)
3321 struct scsi_low_softc *slp;
3323 struct targ_info *ti = slp->sl_Tnexus;
3324 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3326 if (slp->sl_Qnexus != NULL)
3328 if (slp->sl_Qnexus->ccb_tag != etag)
3330 slp->sl_error |= FATALIO;
3331 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3332 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3335 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3337 #ifdef SCSI_LOW_DEBUG
3338 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3340 #endif /* SCSI_LOW_DEBUG */
3342 slp->sl_error |= FATALIO;
3343 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3344 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3350 scsi_low_msginfunc_i_wide_residue(slp)
3351 struct scsi_low_softc *slp;
3353 struct targ_info *ti = slp->sl_Tnexus;
3354 struct slccb *cb = slp->sl_Qnexus;
3355 int res = (int) ti->ti_msgin[1];
3357 if (cb == NULL || res <= 0 ||
3358 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3359 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3362 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3365 slp->sl_scp.scp_datalen += res;
3366 slp->sl_scp.scp_data -= res;
3367 scsi_low_data_finish(slp);
3372 scsi_low_msginfunc_ext(slp)
3373 struct scsi_low_softc *slp;
3375 struct slccb *cb = slp->sl_Qnexus;
3376 struct lun_info *li = slp->sl_Lnexus;
3377 struct targ_info *ti = slp->sl_Tnexus;
3381 if (ti->ti_msginptr == 2)
3383 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3387 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3389 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3393 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3394 count = (int) htonl((long) (*ptr));
3395 if(slp->sl_scp.scp_datalen - count < 0 ||
3396 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3399 slp->sl_scp.scp_datalen -= count;
3400 slp->sl_scp.scp_data += count;
3403 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3407 retry = scsi_low_synch(slp);
3408 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3409 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3411 #ifdef SCSI_LOW_DEBUG
3412 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3414 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3416 #endif /* SCSI_LOW_DEBUG */
3419 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3423 retry = scsi_low_wide(slp);
3424 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3425 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3433 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3438 scsi_low_msginfunc_parity(slp)
3439 struct scsi_low_softc *slp;
3441 struct targ_info *ti = slp->sl_Tnexus;
3443 /* only I -> T, invalid! */
3444 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3449 scsi_low_msginfunc_msg_reject(slp)
3450 struct scsi_low_softc *slp;
3452 struct targ_info *ti = slp->sl_Tnexus;
3453 struct scsi_low_msgout_data *mdp;
3456 if (ti->ti_emsgflags != 0)
3458 device_printf(slp->sl_dev, "msg flags [0x%x] rejected\n",
3460 msgflags = SCSI_LOW_MSG_REJECT;
3461 mdp = &scsi_low_msgout_data[0];
3462 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3464 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3466 ti->ti_emsgflags &= ~mdp->md_flags;
3467 if (mdp->md_errfunc != NULL)
3468 (*mdp->md_errfunc) (slp, msgflags);
3476 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3477 slp->sl_error |= MSGERR;
3483 scsi_low_msgin(slp, ti, c)
3484 struct scsi_low_softc *slp;
3485 struct targ_info *ti;
3488 struct scsi_low_msgin_data *sdp;
3489 struct lun_info *li;
3492 #ifdef SCSI_LOW_DIAGNOSTIC
3493 if (ti != slp->sl_Tnexus)
3495 scsi_low_print(slp, NULL);
3496 panic("scsi_low_msgin: Target nexus inconsistent");
3498 #endif /* SCSI_LOW_DIAGNOSTIC */
3501 * Phase changes, clear the pointer.
3503 if (ti->ti_ophase != ti->ti_phase)
3506 ti->ti_msgin_parity_error = 0;
3508 slp->sl_ph_count ++;
3509 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3511 device_printf(slp->sl_dev, "too many phase changes\n");
3512 slp->sl_error |= FATALIO;
3513 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3518 * Store a current messages byte into buffer and
3519 * wait for the completion of the current msg.
3521 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3522 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3524 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3525 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3529 * Check parity errors.
3531 if ((c & SCSI_LOW_DATA_PE) != 0)
3533 ti->ti_msgin_parity_error ++;
3534 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3538 if (ti->ti_msgin_parity_error != 0)
3542 * Calculate messages length.
3544 msg = ti->ti_msgin[0];
3545 if (msg < MSGIN_DATA_LAST)
3546 sdp = &scsi_low_msgin_data[msg];
3548 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3550 if (ti->ti_msginlen == 0)
3552 ti->ti_msginlen = sdp->md_len;
3558 if (ti->ti_msginptr < ti->ti_msginlen)
3564 if ((msg & MSG_IDENTIFY) == 0)
3566 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3571 li = slp->sl_Lnexus;
3574 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3577 slp->sl_Lnexus = li;
3578 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3582 if (MSGCMD_LUN(msg) != li->li_lun)
3586 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3588 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3590 #ifdef SCSI_LOW_DEBUG
3591 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3595 #endif /* SCSI_LOW_DEBUG */
3603 * Msg process completed, reset msgin pointer and assert ATN if desired.
3606 slp->sl_error |= FATALIO;
3607 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3608 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3611 if (ti->ti_msginptr < ti->ti_msginlen)
3614 #ifdef SCSI_LOW_DIAGNOSTIC
3615 scsi_low_msg_log_write(&ti->ti_log_msgin,
3616 &ti->ti_msgin[0], ti->ti_msginlen);
3617 #endif /* SCSI_LOW_DIAGNOSTIC */
3623 /**********************************************************
3625 **********************************************************/
3627 scsi_low_disconnected(slp, ti)
3628 struct scsi_low_softc *slp;
3629 struct targ_info *ti;
3631 struct slccb *cb = slp->sl_Qnexus;
3633 /* check phase completion */
3634 switch (slp->sl_msgphase)
3637 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3638 scsi_low_msginfunc_cc(slp);
3639 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3643 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3644 scsi_low_msginfunc_cc(slp);
3645 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3649 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3650 scsi_low_msginfunc_cc(slp);
3656 struct lun_info *li;
3659 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3660 cb->ccb_flags |= CCB_DISCQ;
3661 cb->ccb_error |= slp->sl_error;
3667 #ifdef SCSI_LOW_STATICS
3668 scsi_low_statics.nexus_disconnected ++;
3669 #endif /* SCSI_LOW_STATICS */
3671 #ifdef SCSI_LOW_DEBUG
3672 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3674 printf("## SCSI_LOW_DISCONNECTED ===============\n");
3675 scsi_low_print(slp, NULL);
3677 #endif /* SCSI_LOW_DEBUG */
3681 slp->sl_error |= FATALIO;
3682 if (ti->ti_phase == PH_SELSTART)
3683 slp->sl_error |= SELTIMEOUTIO;
3685 slp->sl_error |= UBFERR;
3694 #ifdef SCSI_LOW_DEBUG
3695 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3697 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3698 (cb->ccb_msgoutflag != 0 ||
3699 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3701 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3704 #endif /* SCSI_LOW_DEBUG */
3706 cb->ccb_error |= slp->sl_error;
3707 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3709 cb->ccb_flags |= CCB_STARTQ;
3710 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3715 scsi_low_bus_release(slp, ti);
3716 scsi_low_start(slp);
3720 /**********************************************************
3722 **********************************************************/
3724 scsi_low_alloc_qtag(cb)
3727 struct lun_info *li = cb->li;
3728 scsi_low_tag_t etag;
3730 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3733 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3734 etag = ffs(li->li_qtagbits);
3738 li->li_qtagbits &= ~(1 << (etag - 1));
3739 cb->ccb_otag = etag;
3742 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3743 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3744 if (li->li_qtagarray[li->li_qd] == 0)
3747 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3748 if (li->li_qtagarray[li->li_qd] == 0)
3754 li->li_qtagarray[li->li_qd] ++;
3755 cb->ccb_otag = (li->li_qd ++);
3757 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3761 scsi_low_dealloc_qtag(cb)
3764 struct lun_info *li = cb->li;
3765 scsi_low_tag_t etag;
3767 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3770 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3771 etag = cb->ccb_otag - 1;
3772 #ifdef SCSI_LOW_DIAGNOSTIC
3773 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3774 panic("scsi_low_dealloc_tag: illegal tag");
3775 #endif /* SCSI_LOW_DIAGNOSTIC */
3776 li->li_qtagbits |= (1 << etag);
3778 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3779 etag = cb->ccb_otag;
3780 #ifdef SCSI_LOW_DIAGNOSTIC
3781 if (etag >= SCSI_LOW_MAXNEXUS)
3782 panic("scsi_low_dealloc_tag: illegal tag");
3783 #endif /* SCSI_LOW_DIAGNOSTIC */
3784 li->li_qtagarray[etag] --;
3785 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3787 cb->ccb_otag = SCSI_LOW_UNKTAG;
3791 static struct slccb *
3792 scsi_low_revoke_ccb(slp, cb, fdone)
3793 struct scsi_low_softc *slp;
3797 struct targ_info *ti = cb->ti;
3798 struct lun_info *li = cb->li;
3800 #ifdef SCSI_LOW_DIAGNOSTIC
3801 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3802 (CCB_STARTQ | CCB_DISCQ))
3804 panic("%s: ccb in both queue",
3805 device_get_nameunit(slp->sl_dev));
3807 #endif /* SCSI_LOW_DIAGNOSTIC */
3809 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3811 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3814 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3816 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3822 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3823 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3826 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3827 (cb->ccb_flags & CCB_NORETRY) != 0))
3829 cb->ccb_error |= FATALIO;
3830 cb->ccb_flags &= ~CCB_AUTOSENSE;
3831 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3832 panic("%s: done ccb retried",
3833 device_get_nameunit(slp->sl_dev));
3838 cb->ccb_error |= PENDINGIO;
3839 scsi_low_deactivate_qtag(cb);
3840 scsi_low_ccb_message_retry(cb);
3841 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3847 scsi_low_reset_nexus_lun(slp, li, fdone)
3848 struct scsi_low_softc *slp;
3849 struct lun_info *li;
3852 struct slccb *cb, *ncb, *ecb;
3858 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3860 ncb = TAILQ_NEXT(cb, ccb_chain);
3861 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3865 * presumely keep ordering of io
3867 cb->ccb_flags |= CCB_STARTQ;
3870 TAILQ_INSERT_HEAD(&slp->sl_start,\
3875 TAILQ_INSERT_AFTER(&slp->sl_start,\
3876 ecb, cb, ccb_chain);
3883 /**************************************************************
3885 **************************************************************/
3887 scsi_low_calcf_lun(li)
3888 struct lun_info *li;
3890 struct targ_info *ti = li->li_ti;
3891 struct scsi_low_softc *slp = ti->ti_sc;
3892 u_int cfgflags, diskflags;
3894 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3895 cfgflags = li->li_cfgflags;
3899 diskflags = li->li_diskflags & li->li_quirks;
3902 li->li_flags &= ~SCSI_LOW_DISC;
3903 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3904 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3905 (cfgflags & SCSI_LOW_DISC) != 0)
3906 li->li_flags |= SCSI_LOW_DISC;
3909 li->li_flags |= SCSI_LOW_NOPARITY;
3910 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3911 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3912 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3913 li->li_flags &= ~SCSI_LOW_NOPARITY;
3916 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3917 (cfgflags & SCSI_LOW_QTAG) != 0 &&
3918 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3920 li->li_flags |= SCSI_LOW_QTAG;
3921 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3922 li->li_maxnqio = li->li_maxnexus;
3926 li->li_flags &= ~SCSI_LOW_QTAG;
3927 li->li_maxnexus = 0;
3928 li->li_maxnqio = li->li_maxnexus;
3932 li->li_flags &= ~SCSI_LOW_LINK;
3933 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3934 (diskflags & SCSI_LOW_DISK_LINK) != 0)
3935 li->li_flags |= SCSI_LOW_LINK;
3937 /* compatible flags */
3938 li->li_flags &= ~SCSI_LOW_SYNC;
3939 if (ti->ti_maxsynch.offset > 0)
3940 li->li_flags |= SCSI_LOW_SYNC;
3942 #ifdef SCSI_LOW_DEBUG
3943 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3945 scsi_low_calcf_show(li);
3947 #endif /* SCSI_LOW_DEBUG */
3951 scsi_low_calcf_target(ti)
3952 struct targ_info *ti;
3954 struct scsi_low_softc *slp = ti->ti_sc;
3955 u_int offset, period, diskflags;
3957 diskflags = ti->ti_diskflags & ti->ti_quirks;
3960 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3961 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3963 offset = ti->ti_maxsynch.offset;
3964 period = ti->ti_maxsynch.period;
3965 if (offset == 0 || period == 0)
3966 offset = period = 0;
3970 offset = period = 0;
3973 ti->ti_maxsynch.offset = offset;
3974 ti->ti_maxsynch.period = period;
3977 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3978 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3979 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3981 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3982 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3983 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3985 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3987 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3988 ti->ti_maxsynch.period != ti->ti_osynch.period)
3989 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3990 if (ti->ti_width != ti->ti_owidth)
3991 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3993 ti->ti_osynch = ti->ti_maxsynch;
3994 ti->ti_owidth = ti->ti_width;
3997 #ifdef SCSI_LOW_DEBUG
3998 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
4000 device_printf(slp->sl_dev,
4001 "(%d:*): max period(%dns) offset(%d) width(%d)\n",
4003 ti->ti_maxsynch.period * 4,
4004 ti->ti_maxsynch.offset,
4007 #endif /* SCSI_LOW_DEBUG */
4011 scsi_low_calcf_show(li)
4012 struct lun_info *li;
4014 struct targ_info *ti = li->li_ti;
4015 struct scsi_low_softc *slp = ti->ti_sc;
4017 device_printf(slp->sl_dev,
4018 "(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4019 ti->ti_id, li->li_lun,
4020 ti->ti_maxsynch.period * 4,
4021 ti->ti_maxsynch.offset,
4023 li->li_flags, SCSI_LOW_BITS);
4026 #ifdef SCSI_LOW_START_UP_CHECK
4027 /**************************************************************
4028 * scsi world start up
4029 **************************************************************/
4030 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
4033 scsi_low_start_up(slp)
4034 struct scsi_low_softc *slp;
4036 struct targ_info *ti;
4037 struct lun_info *li;
4041 device_printf(slp->sl_dev, "scsi_low: probing all devices ....\n");
4043 for (target = 0; target < slp->sl_ntargs; target ++)
4045 if (target == slp->sl_hostid)
4047 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4049 device_printf(slp->sl_dev,
4050 "scsi_low: target %d (host card)\n",
4056 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4058 device_printf(slp->sl_dev, "scsi_low: target %d lun ",
4062 ti = slp->sl_ti[target];
4063 for (lun = 0; lun < slp->sl_nluns; lun ++)
4065 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4071 li = scsi_low_alloc_li(ti, lun, 1);
4073 scsi_low_enqueue(slp, ti, li, cb,
4074 CCB_AUTOSENSE | CCB_POLLED, 0);
4076 scsi_low_poll(slp, cb);
4078 if (li->li_state != SCSI_LOW_LUN_OK)
4081 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4087 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4096 scsi_low_poll(slp, cb)
4097 struct scsi_low_softc *slp;
4103 while (slp->sl_nio > 0)
4105 DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4107 (*slp->sl_funcs->scsi_low_poll) (slp);
4108 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4112 scsi_low_timeout_check(slp);
4117 #endif /* SCSI_LOW_START_UP_CHECK */
4119 /**********************************************************
4121 **********************************************************/
4122 #ifdef SCSI_LOW_DEBUG
4124 scsi_low_test_abort(slp, ti, li)
4125 struct scsi_low_softc *slp;
4126 struct targ_info *ti;
4127 struct lun_info *li;
4131 if (li->li_disc > 1)
4133 acb = TAILQ_FIRST(&li->li_discq);
4134 if (scsi_low_abort_ccb(slp, acb) == 0)
4136 device_printf(slp->sl_dev,
4137 "aborting ccb(0x%lx) start\n", (u_long) acb);
4143 scsi_low_test_atten(slp, ti, msg)
4144 struct scsi_low_softc *slp;
4145 struct targ_info *ti;
4149 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4150 scsi_low_assert_msg(slp, ti, msg, 0);
4152 device_printf(slp->sl_dev, "atten check OK\n");
4156 scsi_low_test_cmdlnk(slp, cb)
4157 struct scsi_low_softc *slp;
4160 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4162 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4165 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4166 slp->sl_scp.scp_cmdlen);
4167 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4168 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4170 #endif /* SCSI_LOW_DEBUG */
4173 scsi_low_info(slp, ti, s)
4174 struct scsi_low_softc *slp;
4175 struct targ_info *ti;
4180 slp = LIST_FIRST(&sl_tab);
4184 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4187 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
4188 ti = TAILQ_NEXT(ti, ti_chain))
4190 scsi_low_print(slp, ti);
4195 scsi_low_print(slp, ti);
4199 static u_char *phase[] =
4201 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4202 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4206 scsi_low_print(slp, ti)
4207 struct scsi_low_softc *slp;
4208 struct targ_info *ti;
4210 struct lun_info *li;
4214 if (ti == NULL || ti == slp->sl_Tnexus)
4216 ti = slp->sl_Tnexus;
4217 li = slp->sl_Lnexus;
4218 cb = slp->sl_Qnexus;
4222 li = LIST_FIRST(&ti->ti_litab);
4223 cb = TAILQ_FIRST(&li->li_discq);
4227 device_printf(slp->sl_dev,
4228 "=== NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4229 (u_long) ti, (u_long) li, (u_long) cb, slp->sl_nio);
4234 u_int flags = 0, maxnqio = 0, nqio = 0;
4240 flags = li->li_flags;
4241 maxnqio = li->li_maxnqio;
4245 device_printf(slp->sl_dev,
4246 "(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4247 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4248 phase[(int) ti->ti_phase], ti->ti_disc,
4253 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4254 (u_int) cb->ccb_scp.scp_cmd[0],
4255 cb->ccb_scp.scp_cmdlen,
4257 cb->ccb_scp.scp_datalen,
4258 (u_int) cb->ccb_sscp.scp_status,
4259 cb->ccb_error, SCSI_LOW_ERRORBITS);
4262 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4263 (u_int) (ti->ti_msginptr),
4264 (u_int) (ti->ti_msgin[0]),
4265 (u_int) (ti->ti_msgin[1]),
4266 (u_int) (ti->ti_msgin[2]),
4267 (u_int) (ti->ti_msgin[3]),
4268 (u_int) (ti->ti_msgin[4]),
4271 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4272 (u_int) ti->ti_msgflags,
4273 (u_int) (ti->ti_msgoutstr[0]),
4274 (u_int) (ti->ti_msgoutstr[1]),
4275 (u_int) (ti->ti_msgoutstr[2]),
4276 (u_int) (ti->ti_msgoutstr[3]),
4277 (u_int) (ti->ti_msgoutstr[4]),
4279 flags, SCSI_LOW_BITS);
4281 #ifdef SCSI_LOW_DIAGNOSTIC
4282 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4283 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4284 #endif /* SCSI_LOW_DIAGNOSTIC */
4288 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4289 (u_long) sp->scp_data,
4291 (u_int) sp->scp_status,
4292 slp->sl_error, SCSI_LOW_ERRORBITS);