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 TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
331 if (cb->osdep == osdep)
335 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
337 if (cb->osdep == osdep)
344 scsi_low_translate_error_code(cb, tp)
346 struct scsi_low_error_code *tp;
349 if (cb->ccb_error == 0)
350 return tp->error_code;
352 for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
354 return tp->error_code;
357 /**************************************************************
358 * SCSI INTERFACE (CAM)
359 **************************************************************/
360 #define SCSI_LOW_MALLOC(size) malloc((size), M_SCSILOW, M_NOWAIT)
361 #define SCSI_LOW_FREE(pt) free((pt), M_SCSILOW)
362 #define SCSI_LOW_ALLOC_CCB(flags) scsi_low_get_ccb()
364 static void scsi_low_poll_cam(struct cam_sim *);
365 void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);
367 static int scsi_low_attach_cam(struct scsi_low_softc *);
368 static int scsi_low_world_start_cam(struct scsi_low_softc *);
369 static int scsi_low_dettach_cam(struct scsi_low_softc *);
370 static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
371 static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
372 static void scsi_low_timeout_cam(struct scsi_low_softc *, int, int);
374 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
376 scsi_low_world_start_cam,
377 scsi_low_dettach_cam,
378 scsi_low_ccb_setup_cam,
383 struct scsi_low_error_code scsi_low_error_code_cam[] = {
385 {SENSEIO, CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
386 {SENSEERR, CAM_AUTOSENSE_FAIL},
387 {UACAERR, CAM_SCSI_STATUS_ERROR},
388 {BUSYERR | STATERR, CAM_SCSI_STATUS_ERROR},
389 {SELTIMEOUTIO, CAM_SEL_TIMEOUT},
390 {TIMEOUTIO, CAM_CMD_TIMEOUT},
391 {PDMAERR, CAM_DATA_RUN_ERR},
392 {PARITYERR, CAM_UNCOR_PARITY},
393 {UBFERR, CAM_UNEXP_BUSFREE},
394 {ABORTIO, CAM_REQ_ABORTED},
395 {-1, CAM_UNREC_HBA_ERROR}
398 #define SIM2SLP(sim) ((struct scsi_low_softc *) cam_sim_softc((sim)))
401 * Please check a polling hz, currently we assume scsi_low_poll() is
404 #define SCSI_LOW_CAM_POLL_HZ 1000 /* OK ? */
407 scsi_low_poll_cam(sim)
410 struct scsi_low_softc *slp = SIM2SLP(sim);
412 (*slp->sl_funcs->scsi_low_poll) (slp);
414 if (slp->sl_si.si_poll_count ++ >=
415 SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
417 slp->sl_si.si_poll_count = 0;
418 scsi_low_timeout_check(slp);
423 scsi_low_scsi_action_cam(sim, ccb)
427 struct scsi_low_softc *slp = SIM2SLP(sim);
428 struct targ_info *ti;
431 u_int lun, flags, msg, target;
434 target = (u_int) (ccb->ccb_h.target_id);
435 lun = (u_int) ccb->ccb_h.target_lun;
437 #ifdef SCSI_LOW_DEBUG
438 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
440 device_printf(slp->sl_dev,
441 "cam_action: func code 0x%x target: %d, lun: %d\n",
442 ccb->ccb_h.func_code, target, lun);
444 #endif /* SCSI_LOW_DEBUG */
446 switch (ccb->ccb_h.func_code) {
447 case XPT_SCSI_IO: /* Execute the requested I/O operation */
448 #ifdef SCSI_LOW_DIAGNOSTIC
449 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
451 device_printf(slp->sl_dev, "invalid target/lun\n");
452 ccb->ccb_h.status = CAM_REQ_INVALID;
456 #endif /* SCSI_LOW_DIAGNOSTIC */
458 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
459 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
464 ti = slp->sl_ti[target];
467 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
468 flags = CCB_AUTOSENSE | CCB_SCSIIO;
473 li = scsi_low_alloc_li(ti, lun, 1);
475 if (ti->ti_setup_msg != 0)
477 scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
480 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
482 #ifdef SCSI_LOW_DEBUG
483 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
485 scsi_low_test_abort(slp, ti, li);
487 #endif /* SCSI_LOW_DEBUG */
491 case XPT_EN_LUN: /* Enable LUN as a target */
492 case XPT_TARGET_IO: /* Execute target I/O request */
493 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
494 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
496 ccb->ccb_h.status = CAM_REQ_INVALID;
500 case XPT_ABORT: /* Abort the specified CCB */
501 #ifdef SCSI_LOW_DIAGNOSTIC
502 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
504 device_printf(slp->sl_dev, "invalid target/lun\n");
505 ccb->ccb_h.status = CAM_REQ_INVALID;
509 #endif /* SCSI_LOW_DIAGNOSTIC */
512 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
513 rv = scsi_low_abort_ccb(slp, cb);
517 ccb->ccb_h.status = CAM_REQ_CMP;
519 ccb->ccb_h.status = CAM_REQ_INVALID;
523 case XPT_SET_TRAN_SETTINGS: {
524 struct ccb_trans_settings_scsi *scsi;
525 struct ccb_trans_settings_spi *spi;
526 struct ccb_trans_settings *cts;
529 #ifdef SCSI_LOW_DIAGNOSTIC
530 if (target == CAM_TARGET_WILDCARD)
532 device_printf(slp->sl_dev, "invalid target\n");
533 ccb->ccb_h.status = CAM_REQ_INVALID;
537 #endif /* SCSI_LOW_DIAGNOSTIC */
539 ti = slp->sl_ti[target];
540 if (lun == CAM_LUN_WILDCARD)
544 scsi = &cts->proto_specific.scsi;
545 spi = &cts->xport_specific.spi;
546 if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
547 CTS_SPI_VALID_SYNC_RATE |
548 CTS_SPI_VALID_SYNC_OFFSET)) != 0)
550 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
551 val = spi->bus_width;
552 if (val < ti->ti_width)
555 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
556 val = spi->sync_period;
557 if (val == 0 || val > ti->ti_maxsynch.period)
558 ti->ti_maxsynch.period = val;
560 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
561 val = spi->sync_offset;
562 if (val < ti->ti_maxsynch.offset)
563 ti->ti_maxsynch.offset = val;
565 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
566 scsi_low_calcf_target(ti);
569 if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
570 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
572 li = scsi_low_alloc_li(ti, lun, 1);
573 if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
574 li->li_quirks |= SCSI_LOW_DISK_DISC;
576 li->li_quirks &= ~SCSI_LOW_DISK_DISC;
579 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
580 li->li_quirks |= SCSI_LOW_DISK_QTAG;
582 li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
584 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
585 scsi_low_calcf_target(ti);
586 scsi_low_calcf_lun(li);
587 if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
588 scsi_low_calcf_show(li);
592 ccb->ccb_h.status = CAM_REQ_CMP;
597 case XPT_GET_TRAN_SETTINGS: {
598 struct ccb_trans_settings *cts;
602 #ifdef SCSI_LOW_DIAGNOSTIC
603 if (target == CAM_TARGET_WILDCARD)
605 device_printf(slp->sl_dev, "invalid target\n");
606 ccb->ccb_h.status = CAM_REQ_INVALID;
610 #endif /* SCSI_LOW_DIAGNOSTIC */
611 ti = slp->sl_ti[target];
612 if (lun == CAM_LUN_WILDCARD)
616 li = scsi_low_alloc_li(ti, lun, 1);
617 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
618 struct ccb_trans_settings_scsi *scsi =
619 &cts->proto_specific.scsi;
620 struct ccb_trans_settings_spi *spi =
621 &cts->xport_specific.spi;
622 #ifdef SCSI_LOW_DIAGNOSTIC
623 if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
625 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
626 device_printf(slp->sl_dev,
627 "invalid GET_TRANS_CURRENT_SETTINGS call\n");
630 #endif /* SCSI_LOW_DIAGNOSTIC */
631 cts->protocol = PROTO_SCSI;
632 cts->protocol_version = SCSI_REV_2;
633 cts->transport = XPORT_SPI;
634 cts->transport_version = 2;
636 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
637 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
639 diskflags = li->li_diskflags & li->li_cfgflags;
640 if (diskflags & SCSI_LOW_DISK_DISC)
641 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
642 if (diskflags & SCSI_LOW_DISK_QTAG)
643 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
645 spi->sync_period = ti->ti_maxsynch.period;
646 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
647 spi->sync_offset = ti->ti_maxsynch.offset;
648 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
650 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
651 spi->bus_width = ti->ti_width;
653 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
654 scsi->valid = CTS_SCSI_VALID_TQ;
655 spi->valid |= CTS_SPI_VALID_DISC;
659 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
666 case XPT_CALC_GEOMETRY: { /* not yet HN2 */
667 cam_calc_geometry(&ccb->ccg, /*extended*/1);
672 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
674 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
676 ccb->ccb_h.status = CAM_REQ_CMP;
680 case XPT_TERM_IO: /* Terminate the I/O process */
681 ccb->ccb_h.status = CAM_REQ_INVALID;
685 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
686 #ifdef SCSI_LOW_DIAGNOSTIC
687 if (target == CAM_TARGET_WILDCARD)
689 device_printf(slp->sl_dev, "invalid target\n");
690 ccb->ccb_h.status = CAM_REQ_INVALID;
694 #endif /* SCSI_LOW_DIAGNOSTIC */
696 msg = SCSI_LOW_MSG_RESET;
697 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
699 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
704 ti = slp->sl_ti[target];
705 if (lun == CAM_LUN_WILDCARD)
709 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
710 flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
712 flags = CCB_NORETRY | CCB_URGENT;
715 li = scsi_low_alloc_li(ti, lun, 1);
716 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
720 case XPT_PATH_INQ: { /* Path routing inquiry */
721 struct ccb_pathinq *cpi = &ccb->cpi;
723 cpi->version_num = scsi_low_version_major;
724 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
725 ti = slp->sl_ti[slp->sl_hostid]; /* host id */
726 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
727 cpi->hba_inquiry |= PI_WIDE_16;
728 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
729 cpi->hba_inquiry |= PI_WIDE_32;
730 if (ti->ti_maxsynch.offset > 0)
731 cpi->hba_inquiry |= PI_SDTR_ABLE;
732 cpi->target_sprt = 0;
734 cpi->hba_eng_cnt = 0;
735 cpi->max_target = slp->sl_ntargs - 1;
736 cpi->max_lun = slp->sl_nluns - 1;
737 cpi->initiator_id = slp->sl_hostid;
738 cpi->bus_id = cam_sim_bus(sim);
739 cpi->base_transfer_speed = 3300;
740 cpi->transport = XPORT_SPI;
741 cpi->transport_version = 2;
742 cpi->protocol = PROTO_SCSI;
743 cpi->protocol_version = SCSI_REV_2;
744 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
745 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
746 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
747 cpi->unit_number = cam_sim_unit(sim);
748 cpi->ccb_h.status = CAM_REQ_CMP;
754 printf("scsi_low: non support func_code = %d ",
755 ccb->ccb_h.func_code);
756 ccb->ccb_h.status = CAM_REQ_INVALID;
763 scsi_low_attach_cam(slp)
764 struct scsi_low_softc *slp;
766 struct cam_devq *devq;
769 devq = cam_simq_alloc(SCSI_LOW_NCCB);
774 * ask the adapter what subunits are present
776 tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
777 slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
779 device_get_name(slp->sl_dev), slp,
780 device_get_unit(slp->sl_dev), &Giant,
781 slp->sl_openings, tagged_openings, devq);
783 if (slp->sl_si.sim == NULL) {
788 if (xpt_bus_register(slp->sl_si.sim, NULL, 0) != CAM_SUCCESS) {
789 free(slp->sl_si.sim, M_SCSILOW);
793 if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
794 cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
795 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
796 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
797 cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
801 slp->sl_show_result = SHOW_CALCF_RES; /* OK ? */
806 scsi_low_world_start_cam(slp)
807 struct scsi_low_softc *slp;
814 scsi_low_dettach_cam(slp)
815 struct scsi_low_softc *slp;
818 xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
819 xpt_free_path(slp->sl_si.path);
820 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
821 cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
826 scsi_low_ccb_setup_cam(slp, cb)
827 struct scsi_low_softc *slp;
830 union ccb *ccb = (union ccb *) cb->osdep;
832 if ((cb->ccb_flags & CCB_SCSIIO) != 0)
834 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
835 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
836 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
837 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
838 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
839 cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
840 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
841 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
842 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
846 scsi_low_unit_ready_cmd(cb);
848 return SCSI_LOW_START_QTAG;
852 scsi_low_done_cam(slp, cb)
853 struct scsi_low_softc *slp;
858 ccb = (union ccb *) cb->osdep;
859 if (cb->ccb_error == 0)
861 ccb->ccb_h.status = CAM_REQ_CMP;
866 if (cb->ccb_rcnt >= slp->sl_max_retry)
867 cb->ccb_error |= ABORTIO;
869 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
870 (cb->ccb_error & ABORTIO) == 0)
873 if ((cb->ccb_error & SENSEIO) != 0)
875 memcpy(&ccb->csio.sense_data,
877 sizeof(ccb->csio.sense_data));
880 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
881 &scsi_low_error_code_cam[0]);
883 #ifdef SCSI_LOW_DIAGNOSTIC
884 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
885 cb->ccb_scp.scp_cmdlen > 0 &&
886 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
887 SCSI_LOW_CMD_ABORT_WARNING) != 0)
889 device_printf(slp->sl_dev,
890 "WARNING: scsi_low IO abort\n");
891 scsi_low_print(slp, NULL);
893 #endif /* SCSI_LOW_DIAGNOSTIC */
896 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
897 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
899 if (cb->ccb_scp.scp_status == ST_UNKNOWN)
900 ccb->csio.scsi_status = 0; /* XXX */
902 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
904 if ((cb->ccb_flags & CCB_NOSDONE) == 0)
910 scsi_low_timeout_cam(slp, ch, action)
911 struct scsi_low_softc *slp;
918 case SCSI_LOW_TIMEOUT_CH_IO:
921 case SCSI_LOW_TIMEOUT_START:
922 slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
923 hz / SCSI_LOW_TIMEOUT_HZ);
925 case SCSI_LOW_TIMEOUT_STOP:
926 untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
931 case SCSI_LOW_TIMEOUT_CH_ENGAGE:
934 case SCSI_LOW_TIMEOUT_START:
935 slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
937 case SCSI_LOW_TIMEOUT_STOP:
938 untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
942 case SCSI_LOW_TIMEOUT_CH_RECOVER:
947 /*=============================================================
948 * END OF OS switch (All OS depend fucntions should be above)
949 =============================================================*/
951 /**************************************************************
952 * scsi low deactivate and activate
953 **************************************************************/
955 scsi_low_is_busy(slp)
956 struct scsi_low_softc *slp;
965 scsi_low_deactivate(slp)
966 struct scsi_low_softc *slp;
971 slp->sl_flags |= HW_INACTIVE;
972 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
973 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
974 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
975 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
981 scsi_low_activate(slp)
982 struct scsi_low_softc *slp;
987 slp->sl_flags &= ~HW_INACTIVE;
988 if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
990 slp->sl_flags |= HW_INACTIVE;
995 slp->sl_timeout_count = 0;
996 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
997 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1002 /**************************************************************
1004 **************************************************************/
1005 #ifdef SCSI_LOW_DIAGNOSTIC
1006 static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
1007 static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
1008 static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);
1011 scsi_low_msg_log_init(slmlp)
1012 struct scsi_low_msg_log *slmlp;
1015 slmlp->slml_ptr = 0;
1019 scsi_low_msg_log_write(slmlp, datap, len)
1020 struct scsi_low_msg_log *slmlp;
1026 if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1029 ptr = slmlp->slml_ptr ++;
1030 for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1031 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1032 for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1033 slmlp->slml_msg[ptr].msg[ind] = 0;
1037 scsi_low_msg_log_show(slmlp, s, len)
1038 struct scsi_low_msg_log *slmlp;
1044 printf("%s: (%d) ", s, slmlp->slml_ptr);
1045 for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1047 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1050 printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1056 #endif /* SCSI_LOW_DIAGNOSTIC */
1058 /**************************************************************
1060 **************************************************************/
1062 scsi_low_engage(arg)
1065 struct scsi_low_softc *slp = arg;
1068 switch (slp->sl_rstep)
1072 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1073 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1074 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1079 slp->sl_flags &= ~HW_RESUME;
1080 scsi_low_start(slp);
1090 scsi_low_init(slp, flags)
1091 struct scsi_low_softc *slp;
1096 slp->sl_flags |= HW_INITIALIZING;
1098 /* clear power control timeout */
1099 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1101 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp,
1102 SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1103 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1105 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1108 /* reset current nexus */
1109 scsi_low_reset_nexus(slp, flags);
1110 if ((slp->sl_flags & HW_INACTIVE) != 0)
1116 if (flags != SCSI_LOW_RESTART_SOFT)
1118 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1122 slp->sl_flags &= ~HW_INITIALIZING;
1126 /**************************************************************
1128 **************************************************************/
1129 static struct lun_info *
1130 scsi_low_alloc_li(ti, lun, alloc)
1131 struct targ_info *ti;
1135 struct scsi_low_softc *slp = ti->ti_sc;
1136 struct lun_info *li;
1138 li = LIST_FIRST(&ti->ti_litab);
1141 if (li->li_lun == lun)
1144 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1146 if (li->li_lun == lun)
1148 LIST_REMOVE(li, lun_chain);
1149 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1158 li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1160 panic("no lun info mem");
1162 bzero(li, ti->ti_lunsize);
1166 li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1168 li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1169 li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1170 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1171 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1172 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1174 li->li_qtagbits = (u_int) -1;
1176 TAILQ_INIT(&li->li_discq);
1177 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1179 /* host specific structure initialization per lun */
1180 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1181 (*slp->sl_funcs->scsi_low_lun_init)
1182 (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1183 scsi_low_calcf_lun(li);
1187 /**************************************************************
1188 * allocate targ_info
1189 **************************************************************/
1190 static struct targ_info *
1191 scsi_low_alloc_ti(slp, targ)
1192 struct scsi_low_softc *slp;
1195 struct targ_info *ti;
1197 if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1198 TAILQ_INIT(&slp->sl_titab);
1200 ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1202 panic("%s short of memory", device_get_nameunit(slp->sl_dev));
1204 bzero(ti, slp->sl_targsize);
1208 slp->sl_ti[targ] = ti;
1209 TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1210 LIST_INIT(&ti->ti_litab);
1212 ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1213 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1214 ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1215 #ifdef SCSI_LOW_FLAGS_QUIRKS_OK
1216 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1217 #endif /* SCSI_LOW_FLAGS_QUIRKS_OK */
1219 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1221 (*slp->sl_funcs->scsi_low_targ_init)
1222 (slp, ti, SCSI_LOW_INFO_ALLOC);
1224 scsi_low_calcf_target(ti);
1229 scsi_low_free_ti(slp)
1230 struct scsi_low_softc *slp;
1232 struct targ_info *ti, *tib;
1233 struct lun_info *li, *nli;
1235 for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1237 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1239 if (slp->sl_funcs->scsi_low_lun_init != NULL)
1241 (*slp->sl_funcs->scsi_low_lun_init)
1242 (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1244 nli = LIST_NEXT(li, lun_chain);
1248 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1250 (*slp->sl_funcs->scsi_low_targ_init)
1251 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1253 tib = TAILQ_NEXT(ti, ti_chain);
1258 /**************************************************************
1260 **************************************************************/
1262 scsi_low_bus_idle(slp)
1263 struct scsi_low_softc *slp;
1266 slp->sl_retry_sel = 0;
1267 if (slp->sl_Tnexus == NULL)
1268 scsi_low_start(slp);
1272 scsi_low_timeout(arg)
1275 struct scsi_low_softc *slp = arg;
1279 (void) scsi_low_timeout_check(slp);
1280 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1281 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1286 scsi_low_timeout_check(slp)
1287 struct scsi_low_softc *slp;
1289 struct targ_info *ti;
1290 struct lun_info *li;
1291 struct slccb *cb = NULL; /* XXX */
1293 /* selection restart */
1294 if (slp->sl_retry_sel != 0)
1296 slp->sl_retry_sel = 0;
1297 if (slp->sl_Tnexus != NULL)
1300 cb = TAILQ_FIRST(&slp->sl_start);
1304 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1306 cb->ccb_flags |= CCB_NORETRY;
1307 cb->ccb_error |= SELTIMEOUTIO;
1308 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1309 panic("%s: ccb not finished",
1310 device_get_nameunit(slp->sl_dev));
1313 if (slp->sl_Tnexus == NULL)
1314 scsi_low_start(slp);
1317 /* call hardware timeout */
1319 if (slp->sl_funcs->scsi_low_timeout != NULL)
1321 (*slp->sl_funcs->scsi_low_timeout) (slp);
1324 if (slp->sl_timeout_count ++ <
1325 SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1328 slp->sl_timeout_count = 0;
1329 if (slp->sl_nio > 0)
1331 if ((cb = slp->sl_Qnexus) != NULL)
1333 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1337 else if (slp->sl_disc == 0)
1339 if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1342 cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1346 else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
1347 ti = TAILQ_NEXT(ti, ti_chain))
1349 if (ti->ti_disc == 0)
1352 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
1353 li = LIST_NEXT(li, lun_chain))
1355 for (cb = TAILQ_FIRST(&li->li_discq);
1357 cb = TAILQ_NEXT(cb, ccb_chain))
1360 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1368 else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1370 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1373 if (slp->sl_active != 0)
1375 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1381 if (slp->sl_powc < 0)
1383 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1384 slp->sl_flags |= HW_POWDOWN;
1385 (*slp->sl_funcs->scsi_low_power)
1386 (slp, SCSI_LOW_POWDOWN);
1392 cb->ccb_error |= TIMEOUTIO;
1393 device_printf(slp->sl_dev, "slccb (0x%lx) timeout!\n", (u_long) cb);
1394 scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1395 scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1396 scsi_low_start(slp);
1402 scsi_low_abort_ccb(slp, cb)
1403 struct scsi_low_softc *slp;
1406 struct targ_info *ti;
1407 struct lun_info *li;
1412 if ((cb->ccb_omsgoutflag &
1413 (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1418 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1419 msg = SCSI_LOW_MSG_ABORT;
1421 msg = SCSI_LOW_MSG_ABORT_QTAG;
1423 cb->ccb_error |= ABORTIO;
1424 cb->ccb_flags |= CCB_NORETRY;
1425 scsi_low_ccb_message_assert(cb, msg);
1427 if (cb == slp->sl_Qnexus)
1429 scsi_low_assert_msg(slp, ti, msg, 1);
1431 else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1433 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1434 panic("%s: revoked ccb done",
1435 device_get_nameunit(slp->sl_dev));
1437 cb->ccb_flags |= CCB_STARTQ;
1438 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1440 if (slp->sl_Tnexus == NULL)
1441 scsi_low_start(slp);
1445 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1446 panic("%s: revoked ccb retried",
1447 device_get_nameunit(slp->sl_dev));
1452 /**************************************************************
1453 * Generic SCSI INTERFACE
1454 **************************************************************/
1456 scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
1457 struct scsi_low_softc *slp;
1458 int openings, ntargs, nluns, targsize, lunsize;
1460 struct targ_info *ti;
1461 struct lun_info *li;
1464 slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
1466 if (slp->sl_osdep_fp == NULL)
1467 panic("scsi_low: interface not spcified");
1469 if (ntargs > SCSI_LOW_NTARGETS)
1471 printf("scsi_low: %d targets are too large\n", ntargs);
1472 printf("change kernel options SCSI_LOW_NTARGETS");
1477 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1479 slp->sl_openings = openings;
1480 slp->sl_ntargs = ntargs;
1481 slp->sl_nluns = nluns;
1482 slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1484 if (lunsize < sizeof(struct lun_info))
1485 lunsize = sizeof(struct lun_info);
1487 if (targsize < sizeof(struct targ_info))
1488 targsize = sizeof(struct targ_info);
1490 slp->sl_targsize = targsize;
1491 for (i = 0; i < ntargs; i ++)
1493 ti = scsi_low_alloc_ti(slp, i);
1494 ti->ti_lunsize = lunsize;
1495 li = scsi_low_alloc_li(ti, 0, 1);
1498 /* initialize queue */
1499 nccb = openings * ntargs;
1500 if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1501 nccb = SCSI_LOW_NCCB;
1502 scsi_low_init_ccbque(nccb);
1503 TAILQ_INIT(&slp->sl_start);
1505 /* call os depend attach */
1507 rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
1511 device_printf(slp->sl_dev,
1512 "scsi_low_attach: osdep attach failed\n");
1516 /* check hardware */
1517 DELAY(1000); /* wait for 1ms */
1518 if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1521 device_printf(slp->sl_dev,
1522 "scsi_low_attach: initialization failed\n");
1526 /* start watch dog */
1527 slp->sl_timeout_count = 0;
1528 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1529 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1530 LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1533 scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1535 #ifdef SCSI_LOW_START_UP_CHECK
1536 /* probing devices */
1537 scsi_low_start_up(slp);
1538 #endif /* SCSI_LOW_START_UP_CHECK */
1540 /* call os depend attach done*/
1541 (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
1547 scsi_low_dettach(slp)
1548 struct scsi_low_softc *slp;
1553 if (scsi_low_is_busy(slp) != 0)
1559 scsi_low_deactivate(slp);
1561 rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
1568 scsi_low_free_ti(slp);
1569 LIST_REMOVE(slp, sl_chain);
1574 /**************************************************************
1576 **************************************************************/
1578 scsi_low_enqueue(slp, ti, li, cb, flags, msg)
1579 struct scsi_low_softc *slp;
1580 struct targ_info *ti;
1581 struct lun_info *li;
1589 scsi_low_ccb_message_assert(cb, msg);
1591 cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1592 scsi_low_alloc_qtag(cb);
1594 cb->ccb_flags = flags | CCB_STARTQ;
1595 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1596 cb->ccb_error |= PENDINGIO;
1598 if ((flags & CCB_URGENT) != 0)
1600 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1604 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1609 if (slp->sl_Tnexus == NULL)
1610 scsi_low_start(slp);
1615 scsi_low_message_enqueue(slp, ti, li, flags)
1616 struct scsi_low_softc *slp;
1617 struct targ_info *ti;
1618 struct lun_info *li;
1624 tmsgflags = ti->ti_setup_msg;
1625 ti->ti_setup_msg = 0;
1627 flags |= CCB_NORETRY;
1628 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1633 scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1637 /**************************************************************
1638 * Generic Start & Done
1639 **************************************************************/
1640 #define SLSC_MODE_SENSE_SHORT 0x1a
1641 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0};
1642 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0,
1643 sizeof(struct scsi_low_mode_sense_data), 0};
1644 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0,
1645 sizeof(struct scsi_low_inq_data), 0};
1646 static u_int8_t unit_ready_cmd[6];
1647 static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1648 static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1649 static int scsi_low_resume(struct scsi_low_softc *);
1652 scsi_low_unit_ready_cmd(cb)
1656 cb->ccb_scp.scp_cmd = unit_ready_cmd;
1657 cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1658 cb->ccb_scp.scp_datalen = 0;
1659 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1664 scsi_low_sense_abort_start(slp, ti, li, cb)
1665 struct scsi_low_softc *slp;
1666 struct targ_info *ti;
1667 struct lun_info *li;
1671 cb->ccb_scp.scp_cmdlen = 6;
1672 bzero(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1673 cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1674 cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1675 cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1676 cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1677 cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1678 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1680 scsi_low_ccb_message_clear(cb);
1681 if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1683 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1687 bzero(&cb->ccb_sense, sizeof(cb->ccb_sense));
1688 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1689 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1690 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1693 return SCSI_LOW_START_NO_QTAG;
1697 scsi_low_setup_start(slp, ti, li, cb)
1698 struct scsi_low_softc *slp;
1699 struct targ_info *ti;
1700 struct lun_info *li;
1704 switch(li->li_state)
1706 case SCSI_LOW_LUN_SLEEP:
1707 scsi_low_unit_ready_cmd(cb);
1710 case SCSI_LOW_LUN_START:
1711 cb->ccb_scp.scp_cmd = ss_cmd;
1712 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1713 cb->ccb_scp.scp_datalen = 0;
1714 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1718 case SCSI_LOW_LUN_INQ:
1719 cb->ccb_scp.scp_cmd = inq_cmd;
1720 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1721 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1722 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1723 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1727 case SCSI_LOW_LUN_MODEQ:
1728 cb->ccb_scp.scp_cmd = sms_cmd;
1729 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1730 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1731 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1732 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1734 return SCSI_LOW_START_QTAG;
1737 panic("%s: no setup phase", device_get_nameunit(slp->sl_dev));
1740 return SCSI_LOW_START_NO_QTAG;
1744 scsi_low_resume(slp)
1745 struct scsi_low_softc *slp;
1748 if (slp->sl_flags & HW_RESUME)
1750 slp->sl_flags &= ~HW_POWDOWN;
1751 if (slp->sl_funcs->scsi_low_power != NULL)
1753 slp->sl_flags |= HW_RESUME;
1755 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1756 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1757 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
1758 SCSI_LOW_TIMEOUT_START);
1766 struct scsi_low_softc *slp;
1768 struct targ_info *ti;
1769 struct lun_info *li;
1773 /* check hardware exists or under initializations ? */
1774 if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1777 /* check hardware power up ? */
1778 if ((slp->sl_flags & HW_POWERCTRL) != 0)
1781 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1783 if (scsi_low_resume(slp) == EJUSTRETURN)
1789 #ifdef SCSI_LOW_DIAGNOSTIC
1790 if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1792 scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
1793 panic("%s: inconsistent", device_get_nameunit(slp->sl_dev));
1795 #endif /* SCSI_LOW_DIAGNOSTIC */
1797 for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
1798 cb = TAILQ_NEXT(cb, ccb_chain))
1802 if (li->li_disc == 0)
1804 goto scsi_low_cmd_start;
1806 else if (li->li_nqio > 0)
1808 if (li->li_nqio < li->li_maxnqio ||
1809 (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1810 goto scsi_low_cmd_start;
1816 cb->ccb_flags &= ~CCB_STARTQ;
1817 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1820 /* clear all error flag bits (for restart) */
1822 cb->ccb_datalen = -1;
1823 cb->ccb_scp.scp_status = ST_UNKNOWN;
1825 /* setup nexus pointer */
1826 slp->sl_Qnexus = cb;
1827 slp->sl_Lnexus = li;
1828 slp->sl_Tnexus = ti;
1830 /* initialize msgsys */
1831 scsi_low_init_msgsys(slp, ti);
1834 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1836 /* CA state or forced abort */
1837 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1839 else if (li->li_state >= SCSI_LOW_LUN_OK)
1841 cb->ccb_flags &= ~CCB_INTERNAL;
1842 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
1843 if (cb->ccb_msgoutflag != 0)
1845 scsi_low_ccb_message_exec(slp, cb);
1850 cb->ccb_flags |= CCB_INTERNAL;
1851 rv = scsi_low_setup_start(slp, ti, li, cb);
1855 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1857 if (rv == SCSI_LOW_START_QTAG &&
1858 (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1863 scsi_low_activate_qtag(cb);
1864 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1865 SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1866 qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1867 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1868 qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1870 qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1871 scsi_low_assert_msg(slp, ti, qmsg, 0);
1875 if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1876 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1877 cb->ccb_tc = cb->ccb_tcmax;
1879 /* setup saved scsi data pointer */
1880 cb->ccb_sscp = cb->ccb_scp;
1882 /* setup current scsi pointer */
1883 slp->sl_scp = cb->ccb_sscp;
1884 slp->sl_error = cb->ccb_error;
1886 /* assert always an identify msg */
1887 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1890 #ifdef SCSI_LOW_DIAGNOSTIC
1891 scsi_low_msg_log_init(&ti->ti_log_msgin);
1892 scsi_low_msg_log_init(&ti->ti_log_msgout);
1893 #endif /* SCSI_LOW_DIAGNOSTIC */
1895 /* selection start */
1897 rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1898 if (rv == SCSI_LOW_START_OK)
1900 #ifdef SCSI_LOW_STATICS
1901 scsi_low_statics.nexus_win ++;
1902 #endif /* SCSI_LOW_STATICS */
1906 scsi_low_arbit_fail(slp, cb);
1907 #ifdef SCSI_LOW_STATICS
1908 scsi_low_statics.nexus_fail ++;
1909 #endif /* SCSI_LOW_STATICS */
1913 scsi_low_arbit_fail(slp, cb)
1914 struct scsi_low_softc *slp;
1917 struct targ_info *ti = cb->ti;
1919 scsi_low_deactivate_qtag(cb);
1920 scsi_low_ccb_message_retry(cb);
1921 cb->ccb_flags |= CCB_STARTQ;
1922 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1924 scsi_low_bus_release(slp, ti);
1927 if (slp->sl_disc == 0)
1929 #ifdef SCSI_LOW_DIAGNOSTIC
1930 device_printf(slp->sl_dev, "try selection again\n");
1931 #endif /* SCSI_LOW_DIAGNOSTIC */
1932 slp->sl_retry_sel = 1;
1937 scsi_low_bus_release(slp, ti)
1938 struct scsi_low_softc *slp;
1939 struct targ_info *ti;
1942 if (ti->ti_disc > 0)
1944 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1948 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1951 /* clear all nexus pointer */
1952 slp->sl_Qnexus = NULL;
1953 slp->sl_Lnexus = NULL;
1954 slp->sl_Tnexus = NULL;
1956 /* clear selection assert */
1957 slp->sl_selid = NULL;
1959 /* clear nexus data */
1960 slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1962 /* clear phase change counter */
1963 slp->sl_ph_count = 0;
1967 scsi_low_setup_done(slp, cb)
1968 struct scsi_low_softc *slp;
1971 struct targ_info *ti;
1972 struct lun_info *li;
1977 if (cb->ccb_rcnt >= slp->sl_max_retry)
1979 cb->ccb_error |= ABORTIO;
1980 return SCSI_LOW_DONE_COMPLETE;
1983 /* XXX: special huck for selection timeout */
1984 if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1985 (cb->ccb_error & SELTIMEOUTIO) != 0)
1987 cb->ccb_error |= ABORTIO;
1988 return SCSI_LOW_DONE_COMPLETE;
1991 switch(li->li_state)
1993 case SCSI_LOW_LUN_INQ:
1994 if (cb->ccb_error != 0)
1997 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
2001 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2003 else if ((li->li_inq.sd_version & 7) >= 2 ||
2004 (li->li_inq.sd_len >= 4))
2006 if ((li->li_inq.sd_support & 0x2) == 0)
2007 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2008 if ((li->li_inq.sd_support & 0x8) == 0)
2009 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2012 if ((li->li_inq.sd_support & 0x10) == 0)
2013 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2014 if ((li->li_inq.sd_support & 0x20) == 0)
2015 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2016 if ((li->li_inq.sd_support & 0x40) == 0)
2017 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2022 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2025 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2027 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2029 scsi_low_calcf_target(ti);
2030 scsi_low_calcf_lun(li);
2033 case SCSI_LOW_LUN_MODEQ:
2034 if (cb->ccb_error != 0)
2036 if (cb->ccb_error & SENSEIO)
2038 #ifdef SCSI_LOW_DEBUG
2039 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2041 int error_code, sense_key, asc, ascq;
2043 scsi_extract_sense(&cb->ccb_sense,
2048 printf("SENSE: [%x][%x][%x][%x]\n",
2049 error_code, sense_key, asc,
2052 #endif /* SCSI_LOW_DEBUG */
2056 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2059 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2061 if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2062 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2064 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2065 if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2066 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2068 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2069 scsi_low_calcf_lun(li);
2077 if (li->li_state == SCSI_LOW_LUN_OK)
2079 scsi_low_calcf_target(ti);
2080 scsi_low_calcf_lun(li);
2081 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2082 (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2084 scsi_low_calcf_show(li);
2089 return SCSI_LOW_DONE_RETRY;
2093 scsi_low_done(slp, cb)
2094 struct scsi_low_softc *slp;
2099 if (cb->ccb_error == 0)
2101 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2103 #ifdef SCSI_LOW_QCLEAR_AFTER_CA
2105 * SCSI-2 draft suggests
2106 * page 0x0a QErr bit determins if
2107 * the target aborts or continues
2108 * the queueing io's after CA state resolved.
2109 * However many targets seem not to support
2110 * the page 0x0a. Thus we should manually clear the
2111 * queuing io's after CA state.
2113 if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2116 cb->ccb_flags |= CCB_CLEARQ;
2119 #endif /* SCSI_LOW_QCLEAR_AFTER_CA */
2121 if ((cb->ccb_flags & CCB_SENSE) != 0)
2122 cb->ccb_error |= (SENSEIO | ABORTIO);
2123 cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2125 else switch (cb->ccb_sscp.scp_status)
2131 if (cb->ccb_datalen == 0 ||
2132 cb->ccb_scp.scp_datalen == 0)
2135 if (cb->ccb_scp.scp_cmdlen > 0 &&
2136 (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2137 SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2140 cb->ccb_error |= PDMAERR;
2145 cb->ccb_error |= (BUSYERR | STATERR);
2149 cb->ccb_error |= (STATERR | ABORTIO);
2154 if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2157 cb->ccb_flags |= CCB_SENSE;
2160 cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2165 cb->ccb_error |= FATALIO;
2171 if (cb->ccb_flags & CCB_SENSE)
2173 cb->ccb_error |= (SENSEERR | ABORTIO);
2175 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2179 if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2181 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2185 /* check a ccb msgout flag */
2186 if (cb->ccb_omsgoutflag != 0)
2188 #define SCSI_LOW_MSG_ABORT_OK (SCSI_LOW_MSG_ABORT | \
2189 SCSI_LOW_MSG_ABORT_QTAG | \
2190 SCSI_LOW_MSG_CLEAR_QTAG | \
2191 SCSI_LOW_MSG_TERMIO)
2193 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2195 cb->ccb_error |= ABORTIO;
2199 /* call OS depend done */
2200 if (cb->osdep != NULL)
2202 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2203 if (rv == EJUSTRETURN)
2206 else if (cb->ccb_error != 0)
2208 if (cb->ccb_rcnt >= slp->sl_max_retry)
2209 cb->ccb_error |= ABORTIO;
2211 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2212 (cb->ccb_error & ABORTIO) == 0)
2216 /* free our target */
2217 #ifdef SCSI_LOW_DEBUG
2218 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2220 printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2221 scsi_low_print(slp, NULL);
2223 #endif /* SCSI_LOW_DEBUG */
2225 scsi_low_deactivate_qtag(cb);
2226 scsi_low_dealloc_qtag(cb);
2227 scsi_low_free_ccb(cb);
2229 return SCSI_LOW_DONE_COMPLETE;
2232 #ifdef SCSI_LOW_DEBUG
2233 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2235 printf("** SCSI_LOW_DONE_RETRY ===============\n");
2236 scsi_low_print(slp, NULL);
2238 #endif /* SCSI_LOW_DEBUG */
2241 scsi_low_deactivate_qtag(cb);
2242 scsi_low_ccb_message_retry(cb);
2243 return SCSI_LOW_DONE_RETRY;
2246 /**************************************************************
2248 **************************************************************/
2250 scsi_low_reset_nexus_target(slp, ti, fdone)
2251 struct scsi_low_softc *slp;
2252 struct targ_info *ti;
2255 struct lun_info *li;
2257 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2258 li = LIST_NEXT(li, lun_chain))
2260 scsi_low_reset_nexus_lun(slp, li, fdone);
2261 li->li_state = SCSI_LOW_LUN_SLEEP;
2266 ti->ti_setup_msg = 0;
2267 ti->ti_setup_msg_done = 0;
2269 ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2270 ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2272 ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2273 ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2275 if (slp->sl_funcs->scsi_low_targ_init != NULL)
2277 ((*slp->sl_funcs->scsi_low_targ_init)
2278 (slp, ti, SCSI_LOW_INFO_REVOKE));
2280 scsi_low_calcf_target(ti);
2282 for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
2283 li = LIST_NEXT(li, lun_chain))
2287 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2288 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2290 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2292 ((*slp->sl_funcs->scsi_low_lun_init)
2293 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2295 scsi_low_calcf_lun(li);
2300 scsi_low_reset_nexus(slp, fdone)
2301 struct scsi_low_softc *slp;
2304 struct targ_info *ti;
2305 struct slccb *cb, *topcb;
2307 if ((cb = slp->sl_Qnexus) != NULL)
2309 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2316 for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
2317 ti = TAILQ_NEXT(ti, ti_chain))
2319 scsi_low_reset_nexus_target(slp, ti, fdone);
2320 scsi_low_bus_release(slp, ti);
2321 scsi_low_init_msgsys(slp, ti);
2326 topcb->ccb_flags |= CCB_STARTQ;
2327 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2331 slp->sl_retry_sel = 0;
2332 slp->sl_flags &= ~HW_PDMASTART;
2337 static char tw_chars[] = "|/-\\";
2338 #define TWIDDLEWAIT 10000
2341 scsi_low_twiddle_wait(void)
2345 cnputc(tw_chars[tw_pos++]);
2346 tw_pos %= (sizeof(tw_chars) - 1);
2351 scsi_low_bus_reset(slp)
2352 struct scsi_low_softc *slp;
2356 (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2358 device_printf(slp->sl_dev, "try to reset scsi bus ");
2359 for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2360 scsi_low_twiddle_wait();
2366 scsi_low_restart(slp, flags, s)
2367 struct scsi_low_softc *slp;
2374 device_printf(slp->sl_dev, "scsi bus restart. reason: %s\n", s);
2376 if ((error = scsi_low_init(slp, flags)) != 0)
2379 scsi_low_start(slp);
2383 /**************************************************************
2384 * disconnect and reselect
2385 **************************************************************/
2386 #define MSGCMD_LUN(msg) (msg & 0x07)
2388 static struct slccb *
2389 scsi_low_establish_ccb(ti, li, tag)
2390 struct targ_info *ti;
2391 struct lun_info *li;
2394 struct scsi_low_softc *slp = ti->ti_sc;
2400 cb = TAILQ_FIRST(&li->li_discq);
2401 for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
2402 if (cb->ccb_tag == tag)
2407 * establish our ccb nexus
2410 #ifdef SCSI_LOW_DEBUG
2411 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2413 device_printf(slp->sl_dev, "nexus(0x%lx) abort check start\n",
2415 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2416 scsi_low_revoke_ccb(slp, cb, 1);
2420 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2422 if (cb->ccb_omsgoutflag == 0)
2423 scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2425 #endif /* SCSI_LOW_DEBUG */
2427 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2428 cb->ccb_flags &= ~CCB_DISCQ;
2429 slp->sl_Qnexus = cb;
2431 slp->sl_scp = cb->ccb_sscp;
2432 slp->sl_error |= cb->ccb_error;
2438 /* inform "ccb nexus established" to the host driver */
2439 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2442 if (cb->ccb_msgoutflag != 0)
2444 scsi_low_ccb_message_exec(slp, cb);
2451 scsi_low_reselected(slp, targ)
2452 struct scsi_low_softc *slp;
2455 struct targ_info *ti;
2460 * Check select vs reselected collision.
2463 if ((cb = slp->sl_selid) != NULL)
2465 scsi_low_arbit_fail(slp, cb);
2466 #ifdef SCSI_LOW_STATICS
2467 scsi_low_statics.nexus_conflict ++;
2468 #endif /* SCSI_LOW_STATICS */
2472 * Check if no current active nexus.
2474 if (slp->sl_Tnexus != NULL)
2481 * Check a valid target id asserted ?
2483 if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2485 s = "scsi id illegal";
2490 * Check the target scsi status.
2492 ti = slp->sl_ti[targ];
2493 if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2495 s = "phase mismatch";
2503 scsi_low_init_msgsys(slp, ti);
2506 * Establish our target nexus
2508 SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2509 slp->sl_Tnexus = ti;
2510 #ifdef SCSI_LOW_STATICS
2511 scsi_low_statics.nexus_reselected ++;
2512 #endif /* SCSI_LOW_STATICS */
2516 device_printf(slp->sl_dev, "reselect(%x:unknown) %s\n", targ, s);
2517 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD,
2518 "reselect: scsi world confused");
2522 /**************************************************************
2523 * cmd out pointer setup
2524 **************************************************************/
2526 scsi_low_cmd(slp, ti)
2527 struct scsi_low_softc *slp;
2528 struct targ_info *ti;
2530 struct slccb *cb = slp->sl_Qnexus;
2532 slp->sl_ph_count ++;
2538 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2539 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2540 slp->sl_scp.scp_datalen = 0;
2541 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2542 slp->sl_error |= FATALIO;
2543 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2544 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2549 #ifdef SCSI_LOW_DEBUG
2550 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2552 scsi_low_test_cmdlnk(slp, cb);
2554 #endif /* SCSI_LOW_DEBUG */
2559 /**************************************************************
2560 * data out pointer setup
2561 **************************************************************/
2563 scsi_low_data(slp, ti, bp, direction)
2564 struct scsi_low_softc *slp;
2565 struct targ_info *ti;
2569 struct slccb *cb = slp->sl_Qnexus;
2571 if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2577 slp->sl_error |= (FATALIO | PDMAERR);
2578 slp->sl_scp.scp_datalen = 0;
2579 slp->sl_scp.scp_direction = direction;
2580 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2581 if (ti->ti_ophase != ti->ti_phase)
2586 s = "DATA PHASE: ccb nexus not found";
2588 s = "DATA PHASE: xfer direction mismatch";
2589 SCSI_LOW_INFO(slp, ti, s);
2596 /**************************************************************
2598 **************************************************************/
2599 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2600 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2601 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2602 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2603 #define MSGIN_DATA_LAST 0x30
2605 static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
2606 static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
2607 static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
2608 static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);
2610 static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
2611 static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
2612 static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
2613 static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
2614 static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
2615 static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
2616 static int scsi_low_msgfunc_reset(struct scsi_low_softc *);
2618 struct scsi_low_msgout_data {
2621 int (*md_msgfunc)(struct scsi_low_softc *);
2622 int (*md_errfunc)(struct scsi_low_softc *, u_int);
2623 #define MSG_RELEASE_ATN 0x0001
2627 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2628 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2629 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2630 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2631 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2632 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2633 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2634 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2635 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG, MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2636 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2637 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG, MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2638 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL, MSG_RELEASE_ATN},
2639 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2640 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2641 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2642 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2643 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2646 static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
2647 static int scsi_low_synch(struct scsi_low_softc *);
2648 static int scsi_low_wide(struct scsi_low_softc *);
2649 static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
2650 static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
2651 static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
2652 static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
2653 static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
2654 static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
2655 static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
2656 static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
2657 static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
2658 static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
2659 static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);
2661 struct scsi_low_msgin_data {
2663 int (*md_msgfunc)(struct scsi_low_softc *);
2666 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2667 /* 0 */ {1, scsi_low_msginfunc_cc},
2668 /* 1 */ {2, scsi_low_msginfunc_ext},
2669 /* 2 */ {1, scsi_low_msginfunc_sdp},
2670 /* 3 */ {1, scsi_low_msginfunc_rp},
2671 /* 4 */ {1, scsi_low_msginfunc_disc},
2672 /* 5 */ {1, scsi_low_msginfunc_rejop},
2673 /* 6 */ {1, scsi_low_msginfunc_rejop},
2674 /* 7 */ {1, scsi_low_msginfunc_msg_reject},
2675 /* 8 */ {1, scsi_low_msginfunc_noop},
2676 /* 9 */ {1, scsi_low_msginfunc_parity},
2677 /* a */ {1, scsi_low_msginfunc_lcc},
2678 /* b */ {1, scsi_low_msginfunc_lcc},
2679 /* c */ {1, scsi_low_msginfunc_rejop},
2680 /* d */ {2, scsi_low_msginfunc_rejop},
2681 /* e */ {1, scsi_low_msginfunc_rejop},
2682 /* f */ {1, scsi_low_msginfunc_rejop},
2683 /* 0x10 */ {1, scsi_low_msginfunc_rejop},
2684 /* 0x11 */ {1, scsi_low_msginfunc_rejop},
2685 /* 0x12 */ {1, scsi_low_msginfunc_rejop},
2686 /* 0x13 */ {1, scsi_low_msginfunc_rejop},
2687 /* 0x14 */ {1, scsi_low_msginfunc_rejop},
2688 /* 0x15 */ {1, scsi_low_msginfunc_rejop},
2689 /* 0x16 */ {1, scsi_low_msginfunc_rejop},
2690 /* 0x17 */ {1, scsi_low_msginfunc_rejop},
2691 /* 0x18 */ {1, scsi_low_msginfunc_rejop},
2692 /* 0x19 */ {1, scsi_low_msginfunc_rejop},
2693 /* 0x1a */ {1, scsi_low_msginfunc_rejop},
2694 /* 0x1b */ {1, scsi_low_msginfunc_rejop},
2695 /* 0x1c */ {1, scsi_low_msginfunc_rejop},
2696 /* 0x1d */ {1, scsi_low_msginfunc_rejop},
2697 /* 0x1e */ {1, scsi_low_msginfunc_rejop},
2698 /* 0x1f */ {1, scsi_low_msginfunc_rejop},
2699 /* 0x20 */ {2, scsi_low_msginfunc_simple_qtag},
2700 /* 0x21 */ {2, scsi_low_msginfunc_rejop},
2701 /* 0x22 */ {2, scsi_low_msginfunc_rejop},
2702 /* 0x23 */ {2, scsi_low_msginfunc_i_wide_residue},
2703 /* 0x24 */ {2, scsi_low_msginfunc_rejop},
2704 /* 0x25 */ {2, scsi_low_msginfunc_rejop},
2705 /* 0x26 */ {2, scsi_low_msginfunc_rejop},
2706 /* 0x27 */ {2, scsi_low_msginfunc_rejop},
2707 /* 0x28 */ {2, scsi_low_msginfunc_rejop},
2708 /* 0x29 */ {2, scsi_low_msginfunc_rejop},
2709 /* 0x2a */ {2, scsi_low_msginfunc_rejop},
2710 /* 0x2b */ {2, scsi_low_msginfunc_rejop},
2711 /* 0x2c */ {2, scsi_low_msginfunc_rejop},
2712 /* 0x2d */ {2, scsi_low_msginfunc_rejop},
2713 /* 0x2e */ {2, scsi_low_msginfunc_rejop},
2714 /* 0x2f */ {2, scsi_low_msginfunc_rejop},
2715 /* 0x30 */ {1, scsi_low_msginfunc_rejop} /* default rej op */
2718 /**************************************************************
2720 **************************************************************/
2722 scsi_low_msgfunc_synch(slp)
2723 struct scsi_low_softc *slp;
2725 struct targ_info *ti = slp->sl_Tnexus;
2726 int ptr = ti->ti_msgoutlen;
2728 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2729 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2730 ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2731 ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2732 return MSG_EXTEND_SYNCHLEN + 2;
2736 scsi_low_msgfunc_wide(slp)
2737 struct scsi_low_softc *slp;
2739 struct targ_info *ti = slp->sl_Tnexus;
2740 int ptr = ti->ti_msgoutlen;
2742 ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2743 ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2744 ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2745 return MSG_EXTEND_WIDELEN + 2;
2749 scsi_low_msgfunc_identify(slp)
2750 struct scsi_low_softc *slp;
2752 struct targ_info *ti = slp->sl_Tnexus;
2753 struct lun_info *li = slp->sl_Lnexus;
2754 struct slccb *cb = slp->sl_Qnexus;
2755 int ptr = ti->ti_msgoutlen;
2761 slp->sl_error |= FATALIO;
2762 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2763 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2767 if (scsi_low_is_disconnect_ok(cb) != 0)
2768 msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2772 if (ti->ti_phase == PH_MSGOUT)
2774 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2775 if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2777 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2781 ti->ti_msgoutstr[ptr + 0] = msg;
2786 scsi_low_msgfunc_abort(slp)
2787 struct scsi_low_softc *slp;
2790 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2795 scsi_low_msgfunc_qabort(slp)
2796 struct scsi_low_softc *slp;
2799 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2804 scsi_low_msgfunc_reset(slp)
2805 struct scsi_low_softc *slp;
2808 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2813 scsi_low_msgfunc_qtag(slp)
2814 struct scsi_low_softc *slp;
2816 struct targ_info *ti = slp->sl_Tnexus;
2817 struct slccb *cb = slp->sl_Qnexus;
2818 int ptr = ti->ti_msgoutlen;
2820 if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2822 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2827 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2828 if (ti->ti_phase == PH_MSGOUT)
2830 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2837 * The following functions are called when targets give unexpected
2838 * responces in msgin (after msgout).
2841 scsi_low_errfunc_identify(slp, msgflags)
2842 struct scsi_low_softc *slp;
2846 if (slp->sl_Lnexus != NULL)
2848 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2849 scsi_low_calcf_lun(slp->sl_Lnexus);
2855 scsi_low_errfunc_synch(slp, msgflags)
2856 struct scsi_low_softc *slp;
2859 struct targ_info *ti = slp->sl_Tnexus;
2861 MSGIN_PERIOD(ti) = 0;
2862 MSGIN_OFFSET(ti) = 0;
2863 scsi_low_synch(slp);
2868 scsi_low_errfunc_wide(slp, msgflags)
2869 struct scsi_low_softc *slp;
2872 struct targ_info *ti = slp->sl_Tnexus;
2874 MSGIN_WIDTHP(ti) = 0;
2880 scsi_low_errfunc_qtag(slp, msgflags)
2881 struct scsi_low_softc *slp;
2885 if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2887 if (slp->sl_Qnexus != NULL)
2889 scsi_low_deactivate_qtag(slp->sl_Qnexus);
2891 if (slp->sl_Lnexus != NULL)
2893 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2894 scsi_low_calcf_lun(slp->sl_Lnexus);
2896 device_printf(slp->sl_dev, "scsi_low: qtag msg rejected\n");
2903 scsi_low_msgout(slp, ti, fl)
2904 struct scsi_low_softc *slp;
2905 struct targ_info *ti;
2908 struct scsi_low_msgout_data *mdp;
2911 #ifdef SCSI_LOW_DIAGNOSTIC
2912 if (ti != slp->sl_Tnexus)
2914 scsi_low_print(slp, NULL);
2915 panic("scsi_low_msgout: Target nexus inconsistent");
2917 #endif /* SCSI_LOW_DIAGNOSTIC */
2919 slp->sl_ph_count ++;
2920 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2922 device_printf(slp->sl_dev, "too many phase changes\n");
2923 slp->sl_error |= FATALIO;
2924 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2928 * Scsi phase changes.
2929 * Previously msgs asserted are accepted by our target or
2930 * processed by scsi_low_msgin.
2931 * Thus clear all saved informations.
2933 if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2935 ti->ti_omsgflags = 0;
2936 ti->ti_emsgflags = 0;
2938 else if (slp->sl_atten == 0)
2941 * We did not assert attention, however still our target required
2942 * msgs. Resend previous msgs.
2944 ti->ti_msgflags |= ti->ti_omsgflags;
2945 ti->ti_omsgflags = 0;
2946 #ifdef SCSI_LOW_DIAGNOSTIC
2947 device_printf(slp->sl_dev, "scsi_low_msgout: retry msgout\n");
2948 #endif /* SCSI_LOW_DIAGNOSTIC */
2952 * We have no msgs. send MSG_NOOP (OK?)
2954 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2955 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2960 ti->ti_msgoutlen = 0;
2961 slp->sl_clear_atten = 0;
2962 mdp = &scsi_low_msgout_data[0];
2963 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2965 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2967 ti->ti_omsgflags |= mdp->md_flags;
2968 ti->ti_msgflags &= ~mdp->md_flags;
2969 ti->ti_emsgflags = mdp->md_flags;
2971 ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2972 if (mdp->md_msgfunc != NULL)
2973 len = (*mdp->md_msgfunc) (slp);
2977 #ifdef SCSI_LOW_DIAGNOSTIC
2978 scsi_low_msg_log_write(&ti->ti_log_msgout,
2979 &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2980 #endif /* SCSI_LOW_DIAGNOSTIC */
2982 ti->ti_msgoutlen += len;
2983 if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2985 slp->sl_clear_atten = 1;
2989 if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2990 ti->ti_msgflags == 0)
2993 if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
2998 if (scsi_low_is_msgout_continue(ti, 0) == 0)
2999 slp->sl_clear_atten = 1;
3001 return ti->ti_msgoutlen;
3004 /**************************************************************
3006 **************************************************************/
3008 scsi_low_msginfunc_noop(slp)
3009 struct scsi_low_softc *slp;
3016 scsi_low_msginfunc_rejop(slp)
3017 struct scsi_low_softc *slp;
3019 struct targ_info *ti = slp->sl_Tnexus;
3020 u_int8_t msg = ti->ti_msgin[0];
3022 device_printf(slp->sl_dev, "MSGIN: msg 0x%x rejected\n", (u_int) msg);
3023 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3028 scsi_low_msginfunc_cc(slp)
3029 struct scsi_low_softc *slp;
3031 struct lun_info *li;
3033 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
3035 /* validate status */
3036 if (slp->sl_Qnexus == NULL)
3039 slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3040 li = slp->sl_Lnexus;
3041 switch (slp->sl_scp.scp_status)
3044 li->li_maxnqio = li->li_maxnexus;
3049 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
3050 scsi_low_reset_nexus_lun(slp, li, 0);
3058 if (li->li_maxnexus >= li->li_nqio)
3059 li->li_maxnexus = li->li_nqio - 1;
3060 li->li_maxnqio = li->li_maxnexus;
3065 slp->sl_error |= MSGERR;
3075 scsi_low_msginfunc_lcc(slp)
3076 struct scsi_low_softc *slp;
3078 struct targ_info *ti;
3079 struct lun_info *li;
3080 struct slccb *ncb, *cb;
3082 ti = slp->sl_Tnexus;
3083 li = slp->sl_Lnexus;
3084 if ((cb = slp->sl_Qnexus) == NULL)
3087 cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3088 switch (slp->sl_scp.scp_status)
3092 li->li_maxnqio = li->li_maxnexus;
3096 slp->sl_error |= MSGERR;
3100 if ((li->li_flags & SCSI_LOW_LINK) == 0)
3103 cb->ccb_error |= slp->sl_error;
3104 if (cb->ccb_error != 0)
3107 for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
3108 ncb = TAILQ_NEXT(ncb, ccb_chain))
3111 goto cmd_link_start;
3116 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3117 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3121 ncb->ccb_flags &= ~CCB_STARTQ;
3122 TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3124 scsi_low_dealloc_qtag(ncb);
3125 ncb->ccb_tag = cb->ccb_tag;
3126 ncb->ccb_otag = cb->ccb_otag;
3127 cb->ccb_tag = SCSI_LOW_UNKTAG;
3128 cb->ccb_otag = SCSI_LOW_UNKTAG;
3129 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3130 panic("%s: linked ccb retried",
3131 device_get_nameunit(slp->sl_dev));
3133 slp->sl_Qnexus = ncb;
3134 slp->sl_ph_count = 0;
3137 ncb->ccb_datalen = -1;
3138 ncb->ccb_scp.scp_status = ST_UNKNOWN;
3139 ncb->ccb_flags &= ~CCB_INTERNAL;
3141 scsi_low_init_msgsys(slp, ti);
3143 (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3145 if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3146 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3147 ncb->ccb_tc = ncb->ccb_tcmax;
3149 /* setup saved scsi data pointer */
3150 ncb->ccb_sscp = ncb->ccb_scp;
3151 slp->sl_scp = ncb->ccb_sscp;
3152 slp->sl_error = ncb->ccb_error;
3154 #ifdef SCSI_LOW_DIAGNOSTIC
3155 scsi_low_msg_log_init(&ti->ti_log_msgin);
3156 scsi_low_msg_log_init(&ti->ti_log_msgout);
3157 #endif /* SCSI_LOW_DIAGNOSTIC */
3162 scsi_low_msginfunc_disc(slp)
3163 struct scsi_low_softc *slp;
3166 SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3171 scsi_low_msginfunc_sdp(slp)
3172 struct scsi_low_softc *slp;
3174 struct slccb *cb = slp->sl_Qnexus;
3178 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3179 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3182 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3187 scsi_low_msginfunc_rp(slp)
3188 struct scsi_low_softc *slp;
3191 if (slp->sl_Qnexus != NULL)
3192 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3194 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3200 struct scsi_low_softc *slp;
3202 struct targ_info *ti = slp->sl_Tnexus;
3203 u_int period = 0, offset = 0, speed;
3207 if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3208 MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3209 MSGIN_OFFSET(ti) == 0)
3211 if ((offset = MSGIN_OFFSET(ti)) != 0)
3212 period = MSGIN_PERIOD(ti);
3213 s = offset ? "synchronous" : "async";
3218 * Target seems to be brain damaged.
3219 * Force async transfer.
3221 ti->ti_maxsynch.period = 0;
3222 ti->ti_maxsynch.offset = 0;
3223 device_printf(slp->sl_dev,
3224 "target brain damaged. async transfer\n");
3228 ti->ti_maxsynch.period = period;
3229 ti->ti_maxsynch.offset = offset;
3231 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3235 * Current period and offset are not acceptable
3237 * The adapter changes max synch and max offset.
3239 device_printf(slp->sl_dev,
3240 "synch neg failed. retry synch msg neg ...\n");
3244 ti->ti_osynch = ti->ti_maxsynch;
3247 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3251 if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3253 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3254 struct slccb *cb = slp->sl_Qnexus;
3256 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3258 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3260 device_printf(slp->sl_dev,
3261 "(%d:*): <%s> offset %d period %dns ",
3262 ti->ti_id, s, offset, period * 4);
3266 speed = 1000 * 10 / (period * 4);
3267 printf("%d.%d M/s", speed / 10, speed % 10);
3276 struct scsi_low_softc *slp;
3278 struct targ_info *ti = slp->sl_Tnexus;
3281 ti->ti_width = MSGIN_WIDTHP(ti);
3282 error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3286 * Current width is not acceptable for our adapter.
3287 * The adapter changes max width.
3289 device_printf(slp->sl_dev,
3290 "wide neg failed. retry wide msg neg ...\n");
3294 ti->ti_owidth = ti->ti_width;
3295 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3297 ti->ti_setup_msg_done |=
3298 (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3302 if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3304 #ifdef SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3305 struct slccb *cb = slp->sl_Qnexus;
3307 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3309 #endif /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3311 device_printf(slp->sl_dev, "(%d:*): transfer width %d bits\n",
3312 ti->ti_id, 1 << (3 + ti->ti_width));
3318 scsi_low_msginfunc_simple_qtag(slp)
3319 struct scsi_low_softc *slp;
3321 struct targ_info *ti = slp->sl_Tnexus;
3322 scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3324 if (slp->sl_Qnexus != NULL)
3326 if (slp->sl_Qnexus->ccb_tag != etag)
3328 slp->sl_error |= FATALIO;
3329 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3330 SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3333 else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3335 #ifdef SCSI_LOW_DEBUG
3336 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3338 #endif /* SCSI_LOW_DEBUG */
3340 slp->sl_error |= FATALIO;
3341 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3342 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3348 scsi_low_msginfunc_i_wide_residue(slp)
3349 struct scsi_low_softc *slp;
3351 struct targ_info *ti = slp->sl_Tnexus;
3352 struct slccb *cb = slp->sl_Qnexus;
3353 int res = (int) ti->ti_msgin[1];
3355 if (cb == NULL || res <= 0 ||
3356 (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3357 (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3360 if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3363 slp->sl_scp.scp_datalen += res;
3364 slp->sl_scp.scp_data -= res;
3365 scsi_low_data_finish(slp);
3370 scsi_low_msginfunc_ext(slp)
3371 struct scsi_low_softc *slp;
3373 struct slccb *cb = slp->sl_Qnexus;
3374 struct lun_info *li = slp->sl_Lnexus;
3375 struct targ_info *ti = slp->sl_Tnexus;
3379 if (ti->ti_msginptr == 2)
3381 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3385 switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3387 case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3391 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3392 count = (int) htonl((long) (*ptr));
3393 if(slp->sl_scp.scp_datalen - count < 0 ||
3394 slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3397 slp->sl_scp.scp_datalen -= count;
3398 slp->sl_scp.scp_data += count;
3401 case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3405 retry = scsi_low_synch(slp);
3406 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3407 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3409 #ifdef SCSI_LOW_DEBUG
3410 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3412 scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3414 #endif /* SCSI_LOW_DEBUG */
3417 case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3421 retry = scsi_low_wide(slp);
3422 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3423 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3431 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3436 scsi_low_msginfunc_parity(slp)
3437 struct scsi_low_softc *slp;
3439 struct targ_info *ti = slp->sl_Tnexus;
3441 /* only I -> T, invalid! */
3442 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3447 scsi_low_msginfunc_msg_reject(slp)
3448 struct scsi_low_softc *slp;
3450 struct targ_info *ti = slp->sl_Tnexus;
3451 struct scsi_low_msgout_data *mdp;
3454 if (ti->ti_emsgflags != 0)
3456 device_printf(slp->sl_dev, "msg flags [0x%x] rejected\n",
3458 msgflags = SCSI_LOW_MSG_REJECT;
3459 mdp = &scsi_low_msgout_data[0];
3460 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3462 if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3464 ti->ti_emsgflags &= ~mdp->md_flags;
3465 if (mdp->md_errfunc != NULL)
3466 (*mdp->md_errfunc) (slp, msgflags);
3474 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3475 slp->sl_error |= MSGERR;
3481 scsi_low_msgin(slp, ti, c)
3482 struct scsi_low_softc *slp;
3483 struct targ_info *ti;
3486 struct scsi_low_msgin_data *sdp;
3487 struct lun_info *li;
3490 #ifdef SCSI_LOW_DIAGNOSTIC
3491 if (ti != slp->sl_Tnexus)
3493 scsi_low_print(slp, NULL);
3494 panic("scsi_low_msgin: Target nexus inconsistent");
3496 #endif /* SCSI_LOW_DIAGNOSTIC */
3499 * Phase changes, clear the pointer.
3501 if (ti->ti_ophase != ti->ti_phase)
3504 ti->ti_msgin_parity_error = 0;
3506 slp->sl_ph_count ++;
3507 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3509 device_printf(slp->sl_dev, "too many phase changes\n");
3510 slp->sl_error |= FATALIO;
3511 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3516 * Store a current messages byte into buffer and
3517 * wait for the completion of the current msg.
3519 ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3520 if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3522 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3523 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3527 * Check parity errors.
3529 if ((c & SCSI_LOW_DATA_PE) != 0)
3531 ti->ti_msgin_parity_error ++;
3532 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3536 if (ti->ti_msgin_parity_error != 0)
3540 * Calculate messages length.
3542 msg = ti->ti_msgin[0];
3543 if (msg < MSGIN_DATA_LAST)
3544 sdp = &scsi_low_msgin_data[msg];
3546 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3548 if (ti->ti_msginlen == 0)
3550 ti->ti_msginlen = sdp->md_len;
3556 if (ti->ti_msginptr < ti->ti_msginlen)
3562 if ((msg & MSG_IDENTIFY) == 0)
3564 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3569 li = slp->sl_Lnexus;
3572 li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3575 slp->sl_Lnexus = li;
3576 (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3580 if (MSGCMD_LUN(msg) != li->li_lun)
3584 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3586 if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3588 #ifdef SCSI_LOW_DEBUG
3589 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3593 #endif /* SCSI_LOW_DEBUG */
3601 * Msg process completed, reset msgin pointer and assert ATN if desired.
3604 slp->sl_error |= FATALIO;
3605 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3606 SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3609 if (ti->ti_msginptr < ti->ti_msginlen)
3612 #ifdef SCSI_LOW_DIAGNOSTIC
3613 scsi_low_msg_log_write(&ti->ti_log_msgin,
3614 &ti->ti_msgin[0], ti->ti_msginlen);
3615 #endif /* SCSI_LOW_DIAGNOSTIC */
3621 /**********************************************************
3623 **********************************************************/
3625 scsi_low_disconnected(slp, ti)
3626 struct scsi_low_softc *slp;
3627 struct targ_info *ti;
3629 struct slccb *cb = slp->sl_Qnexus;
3631 /* check phase completion */
3632 switch (slp->sl_msgphase)
3635 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3636 scsi_low_msginfunc_cc(slp);
3637 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3641 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3642 scsi_low_msginfunc_cc(slp);
3643 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3647 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3648 scsi_low_msginfunc_cc(slp);
3654 struct lun_info *li;
3657 TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3658 cb->ccb_flags |= CCB_DISCQ;
3659 cb->ccb_error |= slp->sl_error;
3665 #ifdef SCSI_LOW_STATICS
3666 scsi_low_statics.nexus_disconnected ++;
3667 #endif /* SCSI_LOW_STATICS */
3669 #ifdef SCSI_LOW_DEBUG
3670 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3672 printf("## SCSI_LOW_DISCONNECTED ===============\n");
3673 scsi_low_print(slp, NULL);
3675 #endif /* SCSI_LOW_DEBUG */
3679 slp->sl_error |= FATALIO;
3680 if (ti->ti_phase == PH_SELSTART)
3681 slp->sl_error |= SELTIMEOUTIO;
3683 slp->sl_error |= UBFERR;
3692 #ifdef SCSI_LOW_DEBUG
3693 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3695 if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3696 (cb->ccb_msgoutflag != 0 ||
3697 (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3699 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3702 #endif /* SCSI_LOW_DEBUG */
3704 cb->ccb_error |= slp->sl_error;
3705 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3707 cb->ccb_flags |= CCB_STARTQ;
3708 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3713 scsi_low_bus_release(slp, ti);
3714 scsi_low_start(slp);
3718 /**********************************************************
3720 **********************************************************/
3722 scsi_low_alloc_qtag(cb)
3725 struct lun_info *li = cb->li;
3726 scsi_low_tag_t etag;
3728 if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3731 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3732 etag = ffs(li->li_qtagbits);
3736 li->li_qtagbits &= ~(1 << (etag - 1));
3737 cb->ccb_otag = etag;
3740 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3741 for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3742 if (li->li_qtagarray[li->li_qd] == 0)
3745 for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3746 if (li->li_qtagarray[li->li_qd] == 0)
3752 li->li_qtagarray[li->li_qd] ++;
3753 cb->ccb_otag = (li->li_qd ++);
3755 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3759 scsi_low_dealloc_qtag(cb)
3762 struct lun_info *li = cb->li;
3763 scsi_low_tag_t etag;
3765 if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3768 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3769 etag = cb->ccb_otag - 1;
3770 #ifdef SCSI_LOW_DIAGNOSTIC
3771 if (etag >= sizeof(li->li_qtagbits) * NBBY)
3772 panic("scsi_low_dealloc_tag: illegal tag");
3773 #endif /* SCSI_LOW_DIAGNOSTIC */
3774 li->li_qtagbits |= (1 << etag);
3776 #else /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3777 etag = cb->ccb_otag;
3778 #ifdef SCSI_LOW_DIAGNOSTIC
3779 if (etag >= SCSI_LOW_MAXNEXUS)
3780 panic("scsi_low_dealloc_tag: illegal tag");
3781 #endif /* SCSI_LOW_DIAGNOSTIC */
3782 li->li_qtagarray[etag] --;
3783 #endif /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3785 cb->ccb_otag = SCSI_LOW_UNKTAG;
3789 static struct slccb *
3790 scsi_low_revoke_ccb(slp, cb, fdone)
3791 struct scsi_low_softc *slp;
3795 struct targ_info *ti = cb->ti;
3796 struct lun_info *li = cb->li;
3798 #ifdef SCSI_LOW_DIAGNOSTIC
3799 if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) ==
3800 (CCB_STARTQ | CCB_DISCQ))
3802 panic("%s: ccb in both queue",
3803 device_get_nameunit(slp->sl_dev));
3805 #endif /* SCSI_LOW_DIAGNOSTIC */
3807 if ((cb->ccb_flags & CCB_STARTQ) != 0)
3809 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3812 if ((cb->ccb_flags & CCB_DISCQ) != 0)
3814 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3820 cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ |
3821 CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3824 (cb->ccb_rcnt ++ >= slp->sl_max_retry ||
3825 (cb->ccb_flags & CCB_NORETRY) != 0))
3827 cb->ccb_error |= FATALIO;
3828 cb->ccb_flags &= ~CCB_AUTOSENSE;
3829 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3830 panic("%s: done ccb retried",
3831 device_get_nameunit(slp->sl_dev));
3836 cb->ccb_error |= PENDINGIO;
3837 scsi_low_deactivate_qtag(cb);
3838 scsi_low_ccb_message_retry(cb);
3839 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3845 scsi_low_reset_nexus_lun(slp, li, fdone)
3846 struct scsi_low_softc *slp;
3847 struct lun_info *li;
3850 struct slccb *cb, *ncb, *ecb;
3856 for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3858 ncb = TAILQ_NEXT(cb, ccb_chain);
3859 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3863 * presumely keep ordering of io
3865 cb->ccb_flags |= CCB_STARTQ;
3868 TAILQ_INSERT_HEAD(&slp->sl_start,\
3873 TAILQ_INSERT_AFTER(&slp->sl_start,\
3874 ecb, cb, ccb_chain);
3881 /**************************************************************
3883 **************************************************************/
3885 scsi_low_calcf_lun(li)
3886 struct lun_info *li;
3888 struct targ_info *ti = li->li_ti;
3889 struct scsi_low_softc *slp = ti->ti_sc;
3890 u_int cfgflags, diskflags;
3892 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3893 cfgflags = li->li_cfgflags;
3897 diskflags = li->li_diskflags & li->li_quirks;
3900 li->li_flags &= ~SCSI_LOW_DISC;
3901 if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3902 (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3903 (cfgflags & SCSI_LOW_DISC) != 0)
3904 li->li_flags |= SCSI_LOW_DISC;
3907 li->li_flags |= SCSI_LOW_NOPARITY;
3908 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3909 (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3910 (cfgflags & SCSI_LOW_NOPARITY) == 0)
3911 li->li_flags &= ~SCSI_LOW_NOPARITY;
3914 if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3915 (cfgflags & SCSI_LOW_QTAG) != 0 &&
3916 (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3918 li->li_flags |= SCSI_LOW_QTAG;
3919 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3920 li->li_maxnqio = li->li_maxnexus;
3924 li->li_flags &= ~SCSI_LOW_QTAG;
3925 li->li_maxnexus = 0;
3926 li->li_maxnqio = li->li_maxnexus;
3930 li->li_flags &= ~SCSI_LOW_LINK;
3931 if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3932 (diskflags & SCSI_LOW_DISK_LINK) != 0)
3933 li->li_flags |= SCSI_LOW_LINK;
3935 /* compatible flags */
3936 li->li_flags &= ~SCSI_LOW_SYNC;
3937 if (ti->ti_maxsynch.offset > 0)
3938 li->li_flags |= SCSI_LOW_SYNC;
3940 #ifdef SCSI_LOW_DEBUG
3941 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3943 scsi_low_calcf_show(li);
3945 #endif /* SCSI_LOW_DEBUG */
3949 scsi_low_calcf_target(ti)
3950 struct targ_info *ti;
3952 struct scsi_low_softc *slp = ti->ti_sc;
3953 u_int offset, period, diskflags;
3955 diskflags = ti->ti_diskflags & ti->ti_quirks;
3958 if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3959 (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3961 offset = ti->ti_maxsynch.offset;
3962 period = ti->ti_maxsynch.period;
3963 if (offset == 0 || period == 0)
3964 offset = period = 0;
3968 offset = period = 0;
3971 ti->ti_maxsynch.offset = offset;
3972 ti->ti_maxsynch.period = period;
3975 if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3976 ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3977 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3979 if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3980 ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3981 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3983 if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3985 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3986 ti->ti_maxsynch.period != ti->ti_osynch.period)
3987 ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3988 if (ti->ti_width != ti->ti_owidth)
3989 ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3991 ti->ti_osynch = ti->ti_maxsynch;
3992 ti->ti_owidth = ti->ti_width;
3995 #ifdef SCSI_LOW_DEBUG
3996 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3998 device_printf(slp->sl_dev,
3999 "(%d:*): max period(%dns) offset(%d) width(%d)\n",
4001 ti->ti_maxsynch.period * 4,
4002 ti->ti_maxsynch.offset,
4005 #endif /* SCSI_LOW_DEBUG */
4009 scsi_low_calcf_show(li)
4010 struct lun_info *li;
4012 struct targ_info *ti = li->li_ti;
4013 struct scsi_low_softc *slp = ti->ti_sc;
4015 device_printf(slp->sl_dev,
4016 "(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
4017 ti->ti_id, li->li_lun,
4018 ti->ti_maxsynch.period * 4,
4019 ti->ti_maxsynch.offset,
4021 li->li_flags, SCSI_LOW_BITS);
4024 #ifdef SCSI_LOW_START_UP_CHECK
4025 /**************************************************************
4026 * scsi world start up
4027 **************************************************************/
4028 static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);
4031 scsi_low_start_up(slp)
4032 struct scsi_low_softc *slp;
4034 struct targ_info *ti;
4035 struct lun_info *li;
4039 device_printf(slp->sl_dev, "scsi_low: probing all devices ....\n");
4041 for (target = 0; target < slp->sl_ntargs; target ++)
4043 if (target == slp->sl_hostid)
4045 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4047 device_printf(slp->sl_dev,
4048 "scsi_low: target %d (host card)\n",
4054 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4056 device_printf(slp->sl_dev, "scsi_low: target %d lun ",
4060 ti = slp->sl_ti[target];
4061 for (lun = 0; lun < slp->sl_nluns; lun ++)
4063 if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
4069 li = scsi_low_alloc_li(ti, lun, 1);
4071 scsi_low_enqueue(slp, ti, li, cb,
4072 CCB_AUTOSENSE | CCB_POLLED, 0);
4074 scsi_low_poll(slp, cb);
4076 if (li->li_state != SCSI_LOW_LUN_OK)
4079 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4085 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
4094 scsi_low_poll(slp, cb)
4095 struct scsi_low_softc *slp;
4101 while (slp->sl_nio > 0)
4103 DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4105 (*slp->sl_funcs->scsi_low_poll) (slp);
4106 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4110 scsi_low_timeout_check(slp);
4115 #endif /* SCSI_LOW_START_UP_CHECK */
4117 /**********************************************************
4119 **********************************************************/
4120 #ifdef SCSI_LOW_DEBUG
4122 scsi_low_test_abort(slp, ti, li)
4123 struct scsi_low_softc *slp;
4124 struct targ_info *ti;
4125 struct lun_info *li;
4129 if (li->li_disc > 1)
4131 acb = TAILQ_FIRST(&li->li_discq);
4132 if (scsi_low_abort_ccb(slp, acb) == 0)
4134 device_printf(slp->sl_dev,
4135 "aborting ccb(0x%lx) start\n", (u_long) acb);
4141 scsi_low_test_atten(slp, ti, msg)
4142 struct scsi_low_softc *slp;
4143 struct targ_info *ti;
4147 if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4148 scsi_low_assert_msg(slp, ti, msg, 0);
4150 device_printf(slp->sl_dev, "atten check OK\n");
4154 scsi_low_test_cmdlnk(slp, cb)
4155 struct scsi_low_softc *slp;
4158 #define SCSI_LOW_CMDLNK_NOK (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4160 if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4163 memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4164 slp->sl_scp.scp_cmdlen);
4165 cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4166 slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4168 #endif /* SCSI_LOW_DEBUG */
4171 scsi_low_info(slp, ti, s)
4172 struct scsi_low_softc *slp;
4173 struct targ_info *ti;
4178 slp = LIST_FIRST(&sl_tab);
4182 printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4185 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
4187 scsi_low_print(slp, ti);
4192 scsi_low_print(slp, ti);
4196 static u_char *phase[] =
4198 "FREE", "ARBSTART", "SELSTART", "SELECTED",
4199 "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4203 scsi_low_print(slp, ti)
4204 struct scsi_low_softc *slp;
4205 struct targ_info *ti;
4207 struct lun_info *li;
4211 if (ti == NULL || ti == slp->sl_Tnexus)
4213 ti = slp->sl_Tnexus;
4214 li = slp->sl_Lnexus;
4215 cb = slp->sl_Qnexus;
4219 li = LIST_FIRST(&ti->ti_litab);
4220 cb = TAILQ_FIRST(&li->li_discq);
4224 device_printf(slp->sl_dev,
4225 "=== NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4226 (u_long) ti, (u_long) li, (u_long) cb, slp->sl_nio);
4231 u_int flags = 0, maxnqio = 0, nqio = 0;
4232 int lun = CAM_LUN_WILDCARD;
4237 flags = li->li_flags;
4238 maxnqio = li->li_maxnqio;
4242 device_printf(slp->sl_dev,
4243 "(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4244 ti->ti_id, lun, phase[(int) ti->ti_ophase],
4245 phase[(int) ti->ti_phase], ti->ti_disc,
4250 printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4251 (u_int) cb->ccb_scp.scp_cmd[0],
4252 cb->ccb_scp.scp_cmdlen,
4254 cb->ccb_scp.scp_datalen,
4255 (u_int) cb->ccb_sscp.scp_status,
4256 cb->ccb_error, SCSI_LOW_ERRORBITS);
4259 printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4260 (u_int) (ti->ti_msginptr),
4261 (u_int) (ti->ti_msgin[0]),
4262 (u_int) (ti->ti_msgin[1]),
4263 (u_int) (ti->ti_msgin[2]),
4264 (u_int) (ti->ti_msgin[3]),
4265 (u_int) (ti->ti_msgin[4]),
4268 printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4269 (u_int) ti->ti_msgflags,
4270 (u_int) (ti->ti_msgoutstr[0]),
4271 (u_int) (ti->ti_msgoutstr[1]),
4272 (u_int) (ti->ti_msgoutstr[2]),
4273 (u_int) (ti->ti_msgoutstr[3]),
4274 (u_int) (ti->ti_msgoutstr[4]),
4276 flags, SCSI_LOW_BITS);
4278 #ifdef SCSI_LOW_DIAGNOSTIC
4279 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4280 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4281 #endif /* SCSI_LOW_DIAGNOSTIC */
4285 printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4286 (u_long) sp->scp_data,
4288 (u_int) sp->scp_status,
4289 slp->sl_error, SCSI_LOW_ERRORBITS);