f187895200a8d001e8b1c63251c9760458f647ec
[dragonfly.git] / sys / bus / cam / scsi / scsi_low.c
1 /*
2  * $FreeBSD: src/sys/cam/scsi/scsi_low.c,v 1.1.2.5 2003/08/09 06:18:30 non Exp $
3  * $DragonFly: src/sys/bus/cam/scsi/scsi_low.c,v 1.25 2007/12/01 22:21:17 pavalos Exp $
4  * $NetBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $
5  */
6
7 #define SCSI_LOW_STATICS
8 #define SCSI_LOW_DEBUG
9 #define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
10 #define SCSI_LOW_START_UP_CHECK
11
12 /* #define      SCSI_LOW_INFO_DETAIL */
13 /* #define      SCSI_LOW_QCLEAR_AFTER_CA */
14 /* #define      SCSI_LOW_FLAGS_QUIRKS_OK */
15
16 #define SCSI_LOW_FLAGS_QUIRKS_OK
17
18 /*
19  * [NetBSD for NEC PC-98 series]
20  *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
21  *      NetBSD/pc98 porting staff. All rights reserved.
22  *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
23  *      Naofumi HONDA. All rights reserved.
24  *
25  * [Ported for FreeBSD CAM]
26  *  Copyright (c) 2000, 2001
27  *      MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
28  *      All rights reserved.
29  * 
30  *  Redistribution and use in source and binary forms, with or without
31  *  modification, are permitted provided that the following conditions
32  *  are met:
33  *  1. Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *  2. Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in the
37  *     documentation and/or other materials provided with the distribution.
38  *  3. The name of the author may not be used to endorse or promote products
39  *     derived from this software without specific prior written permission.
40  * 
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
45  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
50  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  */
53
54 /* <On the nexus establishment>
55  * When our host is reselected, 
56  * nexus establish processes are little complicated.
57  * Normal steps are followings:
58  * 1) Our host selected by target => target nexus (slp->sl_Tnexus) 
59  * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
60  * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
61  */
62 #include "opt_ddb.h"
63
64 #include <sys/param.h>
65 #include <sys/buf.h>
66 #include <sys/cons.h>
67 #include <sys/devicestat.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/malloc.h>
71 #include <sys/queue.h>
72 #include <sys/systm.h>
73 #include <sys/thread2.h>
74
75 #include <bus/cam/cam.h>
76 #include <bus/cam/cam_ccb.h>
77 #include <bus/cam/cam_sim.h>
78 #include <bus/cam/cam_debug.h>
79 #include <bus/cam/cam_periph.h>
80 #include <bus/cam/cam_xpt_periph.h>
81
82 #include <bus/cam/scsi/scsi_all.h>
83 #include <bus/cam/scsi/scsi_low.h>
84
85 /**************************************************************
86  * Constants
87  **************************************************************/
88 #define SCSI_LOW_POLL_HZ        1000
89
90 /* functions return values */
91 #define SCSI_LOW_START_NO_QTAG  0
92 #define SCSI_LOW_START_QTAG     1
93
94 #define SCSI_LOW_DONE_COMPLETE  0
95 #define SCSI_LOW_DONE_RETRY     1
96
97 /* internal disk flags */
98 #define SCSI_LOW_DISK_DISC      0x00000001
99 #define SCSI_LOW_DISK_QTAG      0x00000002
100 #define SCSI_LOW_DISK_LINK      0x00000004
101 #define SCSI_LOW_DISK_PARITY    0x00000008
102 #define SCSI_LOW_DISK_SYNC      0x00010000
103 #define SCSI_LOW_DISK_WIDE_16   0x00020000
104 #define SCSI_LOW_DISK_WIDE_32   0x00040000
105 #define SCSI_LOW_DISK_WIDE      (SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
106 #define SCSI_LOW_DISK_LFLAGS    0x0000ffff
107 #define SCSI_LOW_DISK_TFLAGS    0xffff0000
108
109 MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");
110
111 /**************************************************************
112  * Declarations
113  **************************************************************/
114 /* static */ void scsi_low_info (struct scsi_low_softc *, struct targ_info *, u_char *);
115 static void scsi_low_engage (void *);
116 static struct slccb *scsi_low_establish_ccb (struct targ_info *, struct lun_info *, scsi_low_tag_t);
117 static int scsi_low_done (struct scsi_low_softc *, struct slccb *);
118 static int scsi_low_setup_done (struct scsi_low_softc *, struct slccb *);
119 static void scsi_low_bus_release (struct scsi_low_softc *, struct targ_info *);
120 static void scsi_low_twiddle_wait (void);
121 static struct lun_info *scsi_low_alloc_li (struct targ_info *, int, int);
122 static struct targ_info *scsi_low_alloc_ti (struct scsi_low_softc *, int);
123 static void scsi_low_calcf_lun (struct lun_info *);
124 static void scsi_low_calcf_target (struct targ_info *);
125 static void scsi_low_calcf_show (struct lun_info *);
126 static void scsi_low_reset_nexus (struct scsi_low_softc *, int);
127 static void scsi_low_reset_nexus_target (struct scsi_low_softc *, struct targ_info *, int);
128 static void scsi_low_reset_nexus_lun (struct scsi_low_softc *, struct lun_info *, int);
129 static int scsi_low_init (struct scsi_low_softc *, u_int);
130 static void scsi_low_start (struct scsi_low_softc *);
131 static void scsi_low_free_ti (struct scsi_low_softc *);
132
133 static int scsi_low_alloc_qtag (struct slccb *);
134 static int scsi_low_dealloc_qtag (struct slccb *);
135 static int scsi_low_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
136 static int scsi_low_message_enqueue (struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
137 static void scsi_low_unit_ready_cmd (struct slccb *);
138 static void scsi_low_timeout (void *);
139 static int scsi_low_timeout_check (struct scsi_low_softc *);
140 #ifdef  SCSI_LOW_START_UP_CHECK
141 static int scsi_low_start_up (struct scsi_low_softc *);
142 #endif  /* SCSI_LOW_START_UP_CHECK */
143 static int scsi_low_abort_ccb (struct scsi_low_softc *, struct slccb *);
144 static struct slccb *scsi_low_revoke_ccb (struct scsi_low_softc *, struct slccb *, int);
145
146 int scsi_low_version_major = 2;
147 int scsi_low_version_minor = 17;
148
149 static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);
150
151 /**************************************************************
152  * Debug, Run test and Statics
153  **************************************************************/
154 #ifdef  SCSI_LOW_INFO_DETAIL
155 #define SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
156 #else   /* !SCSI_LOW_INFO_DETAIL */
157 #define SCSI_LOW_INFO(slp, ti, s) kprintf("%s: %s\n", (slp)->sl_xname, (s))
158 #endif  /* !SCSI_LOW_INFO_DETAIL */
159
160 #ifdef  SCSI_LOW_STATICS
161 static struct scsi_low_statics {
162         int nexus_win;
163         int nexus_fail;
164         int nexus_disconnected;
165         int nexus_reselected;
166         int nexus_conflict;
167 } scsi_low_statics;
168 #endif  /* SCSI_LOW_STATICS */
169
170 #ifdef  SCSI_LOW_DEBUG
171 #define SCSI_LOW_DEBUG_DONE     0x00001
172 #define SCSI_LOW_DEBUG_DISC     0x00002
173 #define SCSI_LOW_DEBUG_SENSE    0x00004
174 #define SCSI_LOW_DEBUG_CALCF    0x00008
175 #define SCSI_LOW_DEBUG_ACTION   0x10000
176 int scsi_low_debug = 0;
177
178 #define SCSI_LOW_MAX_ATTEN_CHECK        32
179 #define SCSI_LOW_ATTEN_CHECK    0x0001
180 #define SCSI_LOW_CMDLNK_CHECK   0x0002
181 #define SCSI_LOW_ABORT_CHECK    0x0004
182 #define SCSI_LOW_NEXUS_CHECK    0x0008
183 int scsi_low_test = 0;
184 int scsi_low_test_id = 0;
185
186 static void scsi_low_test_abort (struct scsi_low_softc *, struct targ_info *, struct lun_info *);
187 static void scsi_low_test_cmdlnk (struct scsi_low_softc *, struct slccb *);
188 static void scsi_low_test_atten (struct scsi_low_softc *, struct targ_info *, u_int);
189 #define SCSI_LOW_DEBUG_TEST_GO(fl, id) \
190         ((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
191 #define SCSI_LOW_DEBUG_GO(fl, id) \
192         ((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
193 #endif  /* SCSI_LOW_DEBUG */
194
195 /**************************************************************
196  * CCB
197  **************************************************************/
198 GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
199 GENERIC_CCB(scsi_low, slccb, ccb_chain)
200
201 /**************************************************************
202  * Inline functions
203  **************************************************************/
204 #define SCSI_LOW_INLINE static __inline
205 SCSI_LOW_INLINE void scsi_low_activate_qtag (struct slccb *);
206 SCSI_LOW_INLINE void scsi_low_deactivate_qtag (struct slccb *);
207 SCSI_LOW_INLINE void scsi_low_ccb_message_assert (struct slccb *, u_int);
208 SCSI_LOW_INLINE void scsi_low_ccb_message_exec (struct scsi_low_softc *, struct slccb *);
209 SCSI_LOW_INLINE void scsi_low_ccb_message_retry (struct slccb *);
210 SCSI_LOW_INLINE void scsi_low_ccb_message_clear (struct slccb *);
211 SCSI_LOW_INLINE void scsi_low_init_msgsys (struct scsi_low_softc *, struct targ_info *);
212
213 SCSI_LOW_INLINE void
214 scsi_low_activate_qtag(struct slccb *cb)
215 {
216         struct lun_info *li = cb->li;
217
218         if (cb->ccb_tag != SCSI_LOW_UNKTAG)
219                 return;
220
221         li->li_nqio ++;
222         cb->ccb_tag = cb->ccb_otag;
223 }
224         
225 SCSI_LOW_INLINE void
226 scsi_low_deactivate_qtag(struct slccb *cb)
227 {
228         struct lun_info *li = cb->li;
229
230         if (cb->ccb_tag == SCSI_LOW_UNKTAG)
231                 return;
232
233         li->li_nqio --;
234         cb->ccb_tag = SCSI_LOW_UNKTAG;
235 }
236         
237 SCSI_LOW_INLINE void
238 scsi_low_ccb_message_exec(struct scsi_low_softc *slp, struct slccb *cb)
239 {
240         scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
241         cb->ccb_msgoutflag = 0;
242 }
243
244 SCSI_LOW_INLINE void
245 scsi_low_ccb_message_assert(struct slccb *cb, u_int msg)
246 {
247         cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
248 }
249
250 SCSI_LOW_INLINE void
251 scsi_low_ccb_message_retry(struct slccb *cb)
252 {
253         cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
254 }
255
256 SCSI_LOW_INLINE void
257 scsi_low_ccb_message_clear(struct slccb *cb)
258 {
259         cb->ccb_msgoutflag = 0;
260 }
261
262 SCSI_LOW_INLINE void
263 scsi_low_init_msgsys(struct scsi_low_softc *slp, struct targ_info *ti)
264 {
265         ti->ti_msginptr = 0;
266         ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
267         SCSI_LOW_DEASSERT_ATN(slp);
268         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
269 }
270
271 /*=============================================================
272  * START OF OS switch  (All OS depend fucntions should be here)
273  =============================================================*/
274 /* common os depend utitlities */
275 #define SCSI_LOW_CMD_RESIDUAL_CHK       0x0001
276 #define SCSI_LOW_CMD_ORDERED_QTAG       0x0002
277 #define SCSI_LOW_CMD_ABORT_WARNING      0x0004
278
279 static u_int8_t scsi_low_cmd_flags[256] = {
280 /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
281 /*0*/   0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
282 /*1*/   0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
283 /*2*/   0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
284 /*3*/   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
285 };
286
287 struct scsi_low_error_code {
288         int error_bits;
289         int error_code;
290 };
291
292 static struct slccb *scsi_low_find_ccb (struct scsi_low_softc *, u_int, u_int, void *);
293 static int scsi_low_translate_error_code (struct slccb *, struct scsi_low_error_code *);
294
295 static struct slccb *
296 scsi_low_find_ccb(struct scsi_low_softc *slp, u_int target, u_int lun, void *osdep)
297 {
298         struct targ_info *ti;
299         struct lun_info *li;
300         struct slccb *cb;
301
302         ti = slp->sl_ti[target];
303         li = scsi_low_alloc_li(ti, lun, 0);
304         if (li == NULL)
305                 return NULL;
306
307         if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
308                 return cb;
309
310         TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
311         {
312                 if (cb->osdep == osdep)
313                         return cb;
314         }
315
316         TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
317         {
318                 if (cb->osdep == osdep)
319                         return cb;
320         }
321         return NULL;
322 }
323
324 static int 
325 scsi_low_translate_error_code(struct slccb *cb, struct scsi_low_error_code *tp)
326 {
327         if (cb->ccb_error == 0)
328                 return tp->error_code;
329
330         for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
331                 ;
332         return tp->error_code;
333 }
334
335 /**************************************************************
336  * SCSI INTERFACE (CAM)
337  **************************************************************/
338 #define SCSI_LOW_MALLOC(size)           kmalloc((size), M_SCSILOW, M_INTWAIT)
339 #define SCSI_LOW_FREE(pt)               kfree((pt), M_SCSILOW)
340 #define SCSI_LOW_ALLOC_CCB(flags)       scsi_low_get_ccb()
341
342 static void scsi_low_poll_cam (struct cam_sim *);
343 static void scsi_low_cam_rescan_callback (struct cam_periph *, union ccb *);
344 static void scsi_low_rescan_bus_cam (struct scsi_low_softc *);
345 void scsi_low_scsi_action_cam (struct cam_sim *, union ccb *);
346
347 static int scsi_low_attach_cam (struct scsi_low_softc *);
348 static int scsi_low_world_start_cam (struct scsi_low_softc *);
349 static int scsi_low_dettach_cam (struct scsi_low_softc *);
350 static int scsi_low_ccb_setup_cam (struct scsi_low_softc *, struct slccb *);
351 static int scsi_low_done_cam (struct scsi_low_softc *, struct slccb *);
352 static void scsi_low_timeout_cam (struct scsi_low_softc *, int, int);
353
354 struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
355         scsi_low_attach_cam,
356         scsi_low_world_start_cam,
357         scsi_low_dettach_cam,
358         scsi_low_ccb_setup_cam,
359         scsi_low_done_cam,
360         scsi_low_timeout_cam
361 };
362         
363 struct scsi_low_error_code scsi_low_error_code_cam[] = {
364         {0,                     CAM_REQ_CMP},
365         {SENSEIO,               CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
366         {SENSEERR,              CAM_AUTOSENSE_FAIL},
367         {UACAERR,               CAM_SCSI_STATUS_ERROR},
368         {BUSYERR | STATERR,     CAM_SCSI_STATUS_ERROR},
369         {SELTIMEOUTIO,          CAM_SEL_TIMEOUT},       
370         {TIMEOUTIO,             CAM_CMD_TIMEOUT},
371         {PDMAERR,               CAM_DATA_RUN_ERR},
372         {PARITYERR,             CAM_UNCOR_PARITY},
373         {UBFERR,                CAM_UNEXP_BUSFREE},
374         {ABORTIO,               CAM_REQ_ABORTED},
375         {-1,                    CAM_UNREC_HBA_ERROR}
376 };
377
378 #define SIM2SLP(sim)    ((struct scsi_low_softc *) cam_sim_softc((sim)))
379
380 /* XXX:
381  * Please check a polling hz, currently we assume scsi_low_poll() is
382  * called each 1 ms.
383  */
384 #define SCSI_LOW_CAM_POLL_HZ    1000    /* OK ? */
385
386 static void
387 scsi_low_poll_cam(struct cam_sim *sim)
388 {
389         struct scsi_low_softc *slp = SIM2SLP(sim);
390
391         (*slp->sl_funcs->scsi_low_poll) (slp);
392
393         if (slp->sl_si.si_poll_count ++ >= 
394             SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
395         {
396                 slp->sl_si.si_poll_count = 0;
397                 scsi_low_timeout_check(slp);
398         }
399 }
400
401 static void
402 scsi_low_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
403 {
404         xpt_free_path(ccb->ccb_h.path);
405         xpt_free_ccb(ccb);
406 }
407
408 static void
409 scsi_low_rescan_bus_cam(struct scsi_low_softc *slp)
410 {
411         struct cam_path *path;
412         union ccb *ccb = xpt_alloc_ccb();
413         cam_status status;
414
415         status = xpt_create_path(&path, xpt_periph,
416                                  cam_sim_path(slp->sl_si.sim), -1, 0);
417         if (status != CAM_REQ_CMP)
418                 return;
419
420         xpt_setup_ccb(&ccb->ccb_h, path, 5);
421         ccb->ccb_h.func_code = XPT_SCAN_BUS;
422         ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
423         ccb->crcn.flags = CAM_FLAG_NONE;
424         xpt_action(ccb);
425 }
426
427 void
428 scsi_low_scsi_action_cam(struct cam_sim *sim, union ccb *ccb)
429 {
430         struct scsi_low_softc *slp = SIM2SLP(sim);
431         struct targ_info *ti;
432         struct lun_info *li;
433         struct slccb *cb;
434         u_int lun, flags, msg, target;
435         int rv;
436
437         target = (u_int) (ccb->ccb_h.target_id);
438         lun = (u_int) ccb->ccb_h.target_lun;
439
440 #ifdef  SCSI_LOW_DEBUG
441         if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
442         {
443                 kprintf("%s: cam_action: func code 0x%x target: %d, lun: %d\n",
444                         slp->sl_xname, ccb->ccb_h.func_code, target, lun);
445         }
446 #endif  /* SCSI_LOW_DEBUG */
447
448         switch (ccb->ccb_h.func_code) {
449         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
450 #ifdef  SCSI_LOW_DIAGNOSTIC
451                 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
452                 {
453                         kprintf("%s: invalid target/lun\n", slp->sl_xname);
454                         ccb->ccb_h.status = CAM_REQ_INVALID;
455                         xpt_done(ccb);
456                         return;
457                 }
458 #endif  /* SCSI_LOW_DIAGNOSTIC */
459
460                 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
461                         ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
462                         xpt_done(ccb);
463                         return;
464                 }
465
466                 ti = slp->sl_ti[target];
467                 cb->osdep = ccb;
468                 cb->bp = NULL;
469                 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
470                         flags = CCB_AUTOSENSE | CCB_SCSIIO;
471                 else
472                         flags = CCB_SCSIIO;
473
474                 crit_enter();
475                 li = scsi_low_alloc_li(ti, lun, 1);
476
477                 if (ti->ti_setup_msg != 0)
478                 {
479                         scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
480                 }
481
482                 scsi_low_enqueue(slp, ti, li, cb, flags, 0);
483
484 #ifdef  SCSI_LOW_DEBUG
485                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
486                 {
487                         scsi_low_test_abort(slp, ti, li);
488                 }
489 #endif  /* SCSI_LOW_DEBUG */
490                 crit_exit();
491                 break;
492
493         case XPT_EN_LUN:                /* Enable LUN as a target */
494         case XPT_TARGET_IO:             /* Execute target I/O request */
495         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
496         case XPT_CONT_TARGET_IO:        /* Continue Host Target I/O Connection*/
497                 /* XXX Implement */
498                 ccb->ccb_h.status = CAM_REQ_INVALID;
499                 xpt_done(ccb);
500                 break;
501
502         case XPT_ABORT:                 /* Abort the specified CCB */
503 #ifdef  SCSI_LOW_DIAGNOSTIC
504                 if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
505                 {
506                         kprintf("%s: invalid target/lun\n", slp->sl_xname);
507                         ccb->ccb_h.status = CAM_REQ_INVALID;
508                         xpt_done(ccb);
509                         return;
510                 }
511 #endif  /* SCSI_LOW_DIAGNOSTIC */
512
513                 crit_enter();
514                 cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
515                 rv = scsi_low_abort_ccb(slp, cb);
516                 crit_exit();
517
518                 if (rv == 0)
519                         ccb->ccb_h.status = CAM_REQ_CMP;
520                 else
521                         ccb->ccb_h.status = CAM_REQ_INVALID;
522                 xpt_done(ccb);
523                 break;
524
525         case XPT_SET_TRAN_SETTINGS: {
526                 struct ccb_trans_settings *cts;
527                 u_int val;
528
529 #ifdef  SCSI_LOW_DIAGNOSTIC
530                 if (target == CAM_TARGET_WILDCARD)
531                 {
532                         kprintf("%s: invalid target\n", slp->sl_xname);
533                         ccb->ccb_h.status = CAM_REQ_INVALID;
534                         xpt_done(ccb);
535                         return;
536                 }
537 #endif  /* SCSI_LOW_DIAGNOSTIC */
538                 cts = &ccb->cts;
539                 ti = slp->sl_ti[target];
540                 if (lun == CAM_LUN_WILDCARD)
541                         lun = 0;
542
543                 crit_enter();
544                 if ((cts->valid & (CCB_TRANS_BUS_WIDTH_VALID |
545                                    CCB_TRANS_SYNC_RATE_VALID |
546                                    CCB_TRANS_SYNC_OFFSET_VALID)) != 0)
547                 {
548                         if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
549                                 val = cts->bus_width;
550                                 if (val < ti->ti_width)
551                                         ti->ti_width = val;
552                         }
553                         if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
554                                 val = cts->sync_period;
555                                 if (val == 0 || val > ti->ti_maxsynch.period)
556                                         ti->ti_maxsynch.period = val;
557                         }
558                         if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) {
559                                 val = cts->sync_offset;
560                                 if (val < ti->ti_maxsynch.offset)
561                                         ti->ti_maxsynch.offset = val;
562                         }
563
564                         ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
565                         scsi_low_calcf_target(ti);
566                 }
567
568                 if ((cts->valid & (CCB_TRANS_DISC_VALID |
569                                    CCB_TRANS_TQ_VALID)) != 0)
570                 {
571                         li = scsi_low_alloc_li(ti, lun, 1);
572                         if ((cts->valid & CCB_TRANS_DISC_VALID) != 0)
573                         {
574                                 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
575                                         li->li_quirks |= SCSI_LOW_DISK_DISC;
576                                 else
577                                         li->li_quirks &= ~SCSI_LOW_DISK_DISC;
578                         }
579                         if ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
580                         {
581                                 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
582                                         li->li_quirks |= SCSI_LOW_DISK_QTAG;
583                                 else
584                                         li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
585                         }
586
587                         li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
588                         scsi_low_calcf_target(ti);
589                         scsi_low_calcf_lun(li);
590                         if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
591                                 scsi_low_calcf_show(li);
592                 }
593                 crit_exit();
594
595                 ccb->ccb_h.status = CAM_REQ_CMP;
596                 xpt_done(ccb);
597                 break;
598         }
599
600         case XPT_GET_TRAN_SETTINGS: {
601                 struct ccb_trans_settings *cts;
602                 u_int diskflags;
603
604                 cts = &ccb->cts;
605 #ifdef  SCSI_LOW_DIAGNOSTIC
606                 if (target == CAM_TARGET_WILDCARD)
607                 {
608                         kprintf("%s: invalid target\n", slp->sl_xname);
609                         ccb->ccb_h.status = CAM_REQ_INVALID;
610                         xpt_done(ccb);
611                         return;
612                 }
613 #endif  /* SCSI_LOW_DIAGNOSTIC */
614                 ti = slp->sl_ti[target];
615                 if (lun == CAM_LUN_WILDCARD)
616                         lun = 0;
617
618                 crit_enter();
619                 li = scsi_low_alloc_li(ti, lun, 1);
620 #ifdef CAM_NEW_TRAN_CODE
621                 if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
622                         struct ccb_trans_settings_scsi *scsi =
623                                 &cts->proto_specific.scsi;
624                         struct ccb_trans_settings_spi *spi =
625                                 &cts->xport_specific.spi;
626 #ifdef  SCSI_LOW_DIAGNOSTIC
627                         if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
628                         {
629                                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
630                                 kprintf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
631                                         slp->sl_xname);
632                                 goto settings_out;
633                         }
634 #endif  /* SCSI_LOW_DIAGNOSTIC */
635                         cts->protocol = PROTO_SCSI;
636                         cts->protocol_version = SCSI_REV_2;
637                         cts->transport = XPORT_SPI;
638                         cts->transport_version = 2;
639
640                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
641                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
642
643                         diskflags = li->li_diskflags & li->li_cfgflags;
644                         if (diskflags & SCSI_LOW_DISK_DISC)
645                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
646                         if (diskflags & SCSI_LOW_DISK_QTAG)
647                                 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
648
649                         spi->sync_period = ti->ti_maxsynch.period;
650                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
651                         spi->sync_offset = ti->ti_maxsynch.offset;
652                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
653
654                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
655                         spi->bus_width = ti->ti_width;
656
657                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
658                                 scsi->valid = CTS_SCSI_VALID_TQ;
659                                 spi->valid |= CTS_SPI_VALID_DISC;
660                         } else
661                                 scsi->valid = 0;
662                 } else
663                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
664 #else
665                 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0)
666                 {
667 #ifdef  SCSI_LOW_DIAGNOSTIC
668                         if ((li->li_flags_valid & SCSI_LOW_LUN_FLAGS_DISK_VALID) == 0)
669                         {
670                                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
671                                 kprintf("%s: invalid GET_TRANS_USER_SETTINGS call\n",
672                                         slp->sl_xname);
673                                 goto settings_out;
674                         }
675 #endif  /* SCSI_LOW_DIAGNOSTIC */
676                         diskflags = li->li_diskflags & li->li_cfgflags;
677                         if ((diskflags & SCSI_LOW_DISK_DISC) != 0)
678                                 cts->flags |= CCB_TRANS_DISC_ENB;
679                         else
680                                 cts->flags &= ~CCB_TRANS_DISC_ENB;
681                         if ((diskflags & SCSI_LOW_DISK_QTAG) != 0)
682                                 cts->flags |= CCB_TRANS_TAG_ENB;
683                         else
684                                 cts->flags &= ~CCB_TRANS_TAG_ENB;
685                 }
686                 else if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
687                 {
688 #ifdef  SCSI_LOW_DIAGNOSTIC
689                         if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
690                         {
691                                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
692                                 kprintf("%s: invalid GET_TRANS_CURRENT_SETTINGS call\n",
693                                         slp->sl_xname);
694                                 goto settings_out;
695                         }
696 #endif  /* SCSI_LOW_DIAGNOSTIC */
697                         if ((li->li_flags & SCSI_LOW_DISC) != 0)
698                                 cts->flags |= CCB_TRANS_DISC_ENB;
699                         else
700                                 cts->flags &= ~CCB_TRANS_DISC_ENB;
701                         if ((li->li_flags & SCSI_LOW_QTAG) != 0)
702                                 cts->flags |= CCB_TRANS_TAG_ENB;
703                         else
704                                 cts->flags &= ~CCB_TRANS_TAG_ENB;
705                 }
706                 else
707                 {
708                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
709                         goto settings_out;
710                 }
711
712                 cts->sync_period = ti->ti_maxsynch.period;
713                 cts->sync_offset = ti->ti_maxsynch.offset;
714                 cts->bus_width = ti->ti_width;
715
716                 cts->valid = CCB_TRANS_SYNC_RATE_VALID
717                            | CCB_TRANS_SYNC_OFFSET_VALID
718                            | CCB_TRANS_BUS_WIDTH_VALID
719                            | CCB_TRANS_DISC_VALID
720                            | CCB_TRANS_TQ_VALID;
721                 ccb->ccb_h.status = CAM_REQ_CMP;
722 #endif
723 settings_out:
724                 crit_exit();
725                 xpt_done(ccb);
726                 break;
727         }
728
729         case XPT_CALC_GEOMETRY: { /* not yet HN2 */
730                 cam_calc_geometry(&ccb->ccg, /*extended*/1);
731                 xpt_done(ccb);
732                 break;
733         }
734
735         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
736                 crit_enter();
737                 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
738                 crit_exit();
739                 ccb->ccb_h.status = CAM_REQ_CMP;
740                 xpt_done(ccb);
741                 break;
742
743         case XPT_TERM_IO:       /* Terminate the I/O process */
744                 ccb->ccb_h.status = CAM_REQ_INVALID;
745                 xpt_done(ccb);
746                 break;
747
748         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
749 #ifdef  SCSI_LOW_DIAGNOSTIC
750                 if (target == CAM_TARGET_WILDCARD)
751                 {
752                         kprintf("%s: invalid target\n", slp->sl_xname);
753                         ccb->ccb_h.status = CAM_REQ_INVALID;
754                         xpt_done(ccb);
755                         return;
756                 }
757 #endif  /* SCSI_LOW_DIAGNOSTIC */
758
759                 msg = SCSI_LOW_MSG_RESET;
760                 if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
761                 {
762                         ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
763                         xpt_done(ccb);
764                         return;
765                 }
766
767                 ti = slp->sl_ti[target];
768                 if (lun == CAM_LUN_WILDCARD)
769                         lun = 0;
770                 cb->osdep = ccb;
771                 cb->bp = NULL;
772                 if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
773                         flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
774                 else
775                         flags = CCB_NORETRY | CCB_URGENT;
776
777                 crit_enter();
778                 li = scsi_low_alloc_li(ti, lun, 1);
779                 scsi_low_enqueue(slp, ti, li, cb, flags, msg);
780                 crit_exit();
781                 break;
782
783         case XPT_PATH_INQ: {            /* Path routing inquiry */
784                 struct ccb_pathinq *cpi = &ccb->cpi;
785                 
786                 cpi->version_num = scsi_low_version_major;
787                 cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
788                 ti = slp->sl_ti[slp->sl_hostid];        /* host id */
789                 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
790                         cpi->hba_inquiry |= PI_WIDE_16;
791                 if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
792                         cpi->hba_inquiry |= PI_WIDE_32;
793                 if (ti->ti_maxsynch.offset > 0)
794                         cpi->hba_inquiry |= PI_SDTR_ABLE;
795                 cpi->target_sprt = 0;
796                 cpi->hba_misc = 0;
797                 cpi->hba_eng_cnt = 0;
798                 cpi->max_target = slp->sl_ntargs - 1;
799                 cpi->max_lun = slp->sl_nluns - 1;
800                 cpi->initiator_id = slp->sl_hostid;
801                 cpi->bus_id = cam_sim_bus(sim);
802                 cpi->base_transfer_speed = 3300;
803 #ifdef CAM_NEW_TRAN_CODE
804                 cpi->transport = XPORT_SPI;
805                 cpi->transport_version = 2;
806                 cpi->protocol = PROTO_SCSI;
807                 cpi->protocol_version = SCSI_REV_2;
808 #endif
809                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
810                 strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
811                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
812                 cpi->unit_number = cam_sim_unit(sim);
813                 cpi->ccb_h.status = CAM_REQ_CMP;
814                 xpt_done(ccb);
815                 break;
816         }
817
818         default:
819                 kprintf("scsi_low: non support func_code = %d ",
820                         ccb->ccb_h.func_code);
821                 ccb->ccb_h.status = CAM_REQ_INVALID;
822                 xpt_done(ccb);
823                 break;
824         }
825 }
826
827 static int
828 scsi_low_attach_cam(struct scsi_low_softc *slp)
829 {
830         struct cam_devq *devq;
831         int tagged_openings;
832
833         ksprintf(slp->sl_xname, "%s%d",
834                  DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev));
835
836         devq = cam_simq_alloc(SCSI_LOW_NCCB);
837         if (devq == NULL)
838                 return (ENOMEM);
839
840         /*
841          * ask the adapter what subunits are present
842          */
843         tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
844         slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
845                                 scsi_low_poll_cam,
846                                 DEVPORT_DEVNAME(slp->sl_dev), slp,
847                                 DEVPORT_DEVUNIT(slp->sl_dev), 
848                                 slp->sl_openings, tagged_openings, devq);
849         cam_simq_release(devq);
850         if (slp->sl_si.sim == NULL) {
851                 return ENODEV;
852         }
853
854         if (xpt_bus_register(slp->sl_si.sim, 0) != CAM_SUCCESS) {
855                 cam_sim_free(slp->sl_si.sim);
856                 slp->sl_si.sim = NULL;
857                 return ENODEV;
858         }
859        
860         if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
861                         cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
862                         CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
863                 xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
864                 cam_sim_free(slp->sl_si.sim);
865                 slp->sl_si.sim = NULL;
866                 return ENODEV;
867         }
868
869         slp->sl_show_result = SHOW_CALCF_RES;           /* OK ? */
870         return 0;
871 }
872
873 static int
874 scsi_low_world_start_cam(struct scsi_low_softc *slp)
875 {
876 #if 0
877         if (!cold)
878                 scsi_low_rescan_bus_cam(slp);
879 #endif
880         scsi_low_rescan_bus_cam(slp);
881         return 0;
882 }
883
884 static int
885 scsi_low_dettach_cam(struct scsi_low_softc *slp)
886 {
887         xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
888         xpt_free_path(slp->sl_si.path);
889         xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
890         cam_sim_free(slp->sl_si.sim);
891         slp->sl_si.sim = NULL;
892         return 0;
893 }
894
895 static int
896 scsi_low_ccb_setup_cam(struct scsi_low_softc *slp, struct slccb *cb)
897 {
898         union ccb *ccb = (union ccb *) cb->osdep;
899
900         if ((cb->ccb_flags & CCB_SCSIIO) != 0)
901         {
902                 cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
903                 cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
904                 cb->ccb_scp.scp_data = ccb->csio.data_ptr;
905                 cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
906                 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
907                         cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
908                 else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
909                         cb->ccb_scp.scp_direction = SCSI_LOW_READ;
910                 cb->ccb_tcmax = ccb->ccb_h.timeout / 1000;
911         }
912         else
913         {
914                 scsi_low_unit_ready_cmd(cb);
915         }
916         return SCSI_LOW_START_QTAG;
917 }
918
919 static int
920 scsi_low_done_cam(struct scsi_low_softc *slp, struct slccb *cb)
921 {
922         union ccb *ccb;
923
924         ccb = (union ccb *) cb->osdep;
925         if (cb->ccb_error == 0)
926         {
927                 ccb->ccb_h.status = CAM_REQ_CMP;
928                 ccb->csio.resid = 0;
929         }
930         else    
931         {
932                 if (cb->ccb_rcnt >= slp->sl_max_retry)
933                         cb->ccb_error |= ABORTIO;
934
935                 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
936                     (cb->ccb_error & ABORTIO) == 0)
937                         return EJUSTRETURN;
938
939                 if ((cb->ccb_error & SENSEIO) != 0)
940                 {
941                         memcpy(&ccb->csio.sense_data,
942                                &cb->ccb_sense,
943                                sizeof(ccb->csio.sense_data));
944                 }
945
946                 ccb->ccb_h.status = scsi_low_translate_error_code(cb,
947                                         &scsi_low_error_code_cam[0]);
948         
949 #ifdef  SCSI_LOW_DIAGNOSTIC
950                 if ((cb->ccb_flags & CCB_SILENT) == 0 &&
951                     cb->ccb_scp.scp_cmdlen > 0 &&
952                     (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
953                      SCSI_LOW_CMD_ABORT_WARNING) != 0)
954                 {
955                         kprintf("%s: WARNING: scsi_low IO abort\n",
956                                 slp->sl_xname);
957                         scsi_low_print(slp, NULL);
958                 }
959 #endif  /* SCSI_LOW_DIAGNOSTIC */
960         }
961
962         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
963                 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
964
965         if (cb->ccb_scp.scp_status == ST_UNKNOWN)
966                 ccb->csio.scsi_status = 0;      /* XXX */
967         else
968                 ccb->csio.scsi_status = cb->ccb_scp.scp_status;
969
970         if ((cb->ccb_flags & CCB_NOSDONE) == 0)
971                 xpt_done(ccb);
972         return 0;
973 }
974
975 static void
976 scsi_low_timeout_cam(struct scsi_low_softc *slp, int ch, int action)
977 {
978         switch (ch)
979         {
980         case SCSI_LOW_TIMEOUT_CH_IO:
981                 switch (action)
982                 {
983                 case SCSI_LOW_TIMEOUT_START:
984                         callout_reset(&slp->sl_si.timeout_ch,
985                             hz / SCSI_LOW_TIMEOUT_HZ, scsi_low_timeout, slp);
986                         break;
987                 case SCSI_LOW_TIMEOUT_STOP:
988                         callout_stop(&slp->sl_si.timeout_ch);
989                         break;
990                 }
991                 break;
992
993         case SCSI_LOW_TIMEOUT_CH_ENGAGE:
994                 switch (action)
995                 {
996                 case SCSI_LOW_TIMEOUT_START:
997                         callout_reset(&slp->sl_si.engage_ch, 1,
998                                       scsi_low_engage, slp);
999                         break;
1000                 case SCSI_LOW_TIMEOUT_STOP:
1001                         callout_stop(&slp->sl_si.engage_ch);
1002                         break;
1003                 }
1004                 break;
1005         case SCSI_LOW_TIMEOUT_CH_RECOVER:
1006                 break;
1007         }
1008 }
1009
1010 /**************************************************************
1011  * scsi low deactivate and activate
1012  **************************************************************/
1013 int
1014 scsi_low_is_busy(struct scsi_low_softc *slp)
1015 {
1016         if (slp->sl_nio > 0)
1017                 return EBUSY;
1018         return 0;
1019 }
1020
1021 int
1022 scsi_low_deactivate(struct scsi_low_softc *slp)
1023 {
1024         crit_enter();
1025         slp->sl_flags |= HW_INACTIVE;
1026         (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1027                 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
1028         (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1029                 (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1030         crit_exit();
1031         return 0;
1032 }
1033
1034 int
1035 scsi_low_activate(struct scsi_low_softc *slp)
1036 {
1037         int error;
1038
1039         crit_enter();
1040         slp->sl_flags &= ~HW_INACTIVE;
1041         if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
1042         {
1043                 slp->sl_flags |= HW_INACTIVE;
1044                 crit_exit();
1045                 return error;
1046         }
1047
1048         slp->sl_timeout_count = 0;
1049         (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1050                 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1051         crit_exit();
1052         return 0;
1053 }
1054
1055 /**************************************************************
1056  * scsi low log
1057  **************************************************************/
1058 #ifdef  SCSI_LOW_DIAGNOSTIC
1059 static void scsi_low_msg_log_init (struct scsi_low_msg_log *);
1060 static void scsi_low_msg_log_write (struct scsi_low_msg_log *, u_int8_t *,
1061 int);
1062 static void scsi_low_msg_log_show (struct scsi_low_msg_log *, char *, int);
1063
1064 static void
1065 scsi_low_msg_log_init(struct scsi_low_msg_log *slmlp)
1066 {
1067         slmlp->slml_ptr = 0;
1068 }
1069
1070 static void
1071 scsi_low_msg_log_write(struct scsi_low_msg_log *slmlp, u_int8_t *datap, int len)
1072 {
1073         int ptr, ind;
1074
1075         if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
1076                 return;
1077
1078         ptr = slmlp->slml_ptr ++;
1079         for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
1080                 slmlp->slml_msg[ptr].msg[ind] = datap[ind];
1081         for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
1082                 slmlp->slml_msg[ptr].msg[ind] = 0;
1083 }
1084         
1085 static void
1086 scsi_low_msg_log_show(struct scsi_low_msg_log *slmlp, char *s, int len)
1087 {
1088         int ptr, ind;
1089
1090         kprintf("%s: (%d) ", s, slmlp->slml_ptr);
1091         for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
1092         {
1093                 for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
1094                      ind ++)
1095                 {
1096                         kprintf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
1097                 }
1098                 kprintf(">");
1099         }
1100         kprintf("\n");
1101 }
1102 #endif  /* SCSI_LOW_DIAGNOSTIC */
1103
1104 /**************************************************************
1105  * power control
1106  **************************************************************/
1107 static void
1108 scsi_low_engage(void *arg)
1109 {
1110         struct scsi_low_softc *slp = arg;
1111
1112         crit_enter();
1113
1114         switch (slp->sl_rstep)
1115         {
1116         case 0:
1117                 slp->sl_rstep ++;
1118                 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1119                 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp, 
1120                         SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
1121                 break;
1122
1123         case 1:
1124                 slp->sl_rstep ++;
1125                 slp->sl_flags &= ~HW_RESUME;
1126                 scsi_low_start(slp);
1127                 break;
1128
1129         case 2:
1130                 break;
1131         }
1132         crit_exit();
1133 }
1134
1135 static int
1136 scsi_low_init(struct scsi_low_softc *slp, u_int flags)
1137 {
1138         int rv = 0;
1139
1140         slp->sl_flags |= HW_INITIALIZING;
1141
1142         /* clear power control timeout */
1143         if ((slp->sl_flags & HW_POWERCTRL) != 0)
1144         {
1145                 (*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp, 
1146                         SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
1147                 slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
1148                 slp->sl_active = 1;
1149                 slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1150         }
1151
1152         /* reset current nexus */
1153         scsi_low_reset_nexus(slp, flags);
1154         if ((slp->sl_flags & HW_INACTIVE) != 0)
1155         {
1156                 rv = EBUSY;
1157                 goto out;
1158         }
1159
1160         if (flags != SCSI_LOW_RESTART_SOFT)
1161         {
1162                 rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
1163         }
1164
1165 out:
1166         slp->sl_flags &= ~HW_INITIALIZING;
1167         return rv;
1168 }
1169
1170 /**************************************************************
1171  * allocate lun_info
1172  **************************************************************/
1173 static struct lun_info *
1174 scsi_low_alloc_li(struct targ_info *ti, int lun, int alloc)
1175 {
1176         struct scsi_low_softc *slp = ti->ti_sc;
1177         struct lun_info *li;
1178
1179         li = LIST_FIRST(&ti->ti_litab); 
1180         if (li != NULL)
1181         { 
1182                 if (li->li_lun == lun)
1183                         return li;
1184
1185                 while ((li = LIST_NEXT(li, lun_chain)) != NULL)
1186                 {
1187                         if (li->li_lun == lun)
1188                         {
1189                                 LIST_REMOVE(li, lun_chain);
1190                                 LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1191                                 return li;
1192                         }
1193                 }
1194         }
1195
1196         if (alloc == 0)
1197                 return li;
1198
1199         li = SCSI_LOW_MALLOC(ti->ti_lunsize);
1200         if (li == NULL)
1201                 panic("no lun info mem");
1202
1203         SCSI_LOW_BZERO(li, ti->ti_lunsize);
1204         li->li_lun = lun;
1205         li->li_ti = ti;
1206
1207         li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
1208                           SCSI_LOW_QTAG;
1209         li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
1210         li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
1211 #ifdef  SCSI_LOW_FLAGS_QUIRKS_OK
1212         li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
1213 #endif  /* SCSI_LOW_FLAGS_QUIRKS_OK */
1214
1215         li->li_qtagbits = (u_int) -1;
1216
1217         TAILQ_INIT(&li->li_discq);
1218         LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
1219
1220         /* host specific structure initialization per lun */
1221         if (slp->sl_funcs->scsi_low_lun_init != NULL)
1222                 (*slp->sl_funcs->scsi_low_lun_init)
1223                         (slp, ti, li, SCSI_LOW_INFO_ALLOC);
1224         scsi_low_calcf_lun(li);
1225         return li;
1226 }
1227
1228 /**************************************************************
1229  * allocate targ_info
1230  **************************************************************/
1231 static struct targ_info *
1232 scsi_low_alloc_ti(struct scsi_low_softc *slp, int targ)
1233 {
1234         struct targ_info *ti;
1235
1236         if (TAILQ_FIRST(&slp->sl_titab) == NULL)
1237                 TAILQ_INIT(&slp->sl_titab);
1238
1239         ti = SCSI_LOW_MALLOC(slp->sl_targsize);
1240         if (ti == NULL)
1241                 panic("%s short of memory", slp->sl_xname);
1242
1243         SCSI_LOW_BZERO(ti, slp->sl_targsize);
1244         ti->ti_id = targ;
1245         ti->ti_sc = slp;
1246
1247         slp->sl_ti[targ] = ti;
1248         TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
1249         LIST_INIT(&ti->ti_litab);
1250
1251         ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
1252         ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
1253         ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
1254 #ifdef  SCSI_LOW_FLAGS_QUIRKS_OK
1255         ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
1256 #endif  /* SCSI_LOW_FLAGS_QUIRKS_OK */
1257
1258         if (slp->sl_funcs->scsi_low_targ_init != NULL)
1259         {
1260                 (*slp->sl_funcs->scsi_low_targ_init)
1261                         (slp, ti, SCSI_LOW_INFO_ALLOC);
1262         }
1263         scsi_low_calcf_target(ti);
1264         return ti;
1265 }
1266
1267 static void
1268 scsi_low_free_ti(struct scsi_low_softc *slp)
1269 {
1270         struct targ_info *ti, *tib;
1271         struct lun_info *li, *nli;
1272
1273         for (ti = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
1274         {
1275                 for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
1276                 {
1277                         if (slp->sl_funcs->scsi_low_lun_init != NULL)
1278                         {
1279                                 (*slp->sl_funcs->scsi_low_lun_init)
1280                                         (slp, ti, li, SCSI_LOW_INFO_DEALLOC);
1281                         }
1282                         nli = LIST_NEXT(li, lun_chain);
1283                         SCSI_LOW_FREE(li);
1284                 }
1285
1286                 if (slp->sl_funcs->scsi_low_targ_init != NULL)
1287                 {
1288                         (*slp->sl_funcs->scsi_low_targ_init)
1289                                 (slp, ti, SCSI_LOW_INFO_DEALLOC);
1290                 }
1291                 tib = TAILQ_NEXT(ti, ti_chain);
1292                 SCSI_LOW_FREE(ti);
1293         }
1294 }
1295
1296 /**************************************************************
1297  * timeout
1298  **************************************************************/
1299 void
1300 scsi_low_bus_idle(struct scsi_low_softc *slp)
1301 {
1302         slp->sl_retry_sel = 0;
1303         if (slp->sl_Tnexus == NULL)
1304                 scsi_low_start(slp);
1305 }
1306
1307 static void
1308 scsi_low_timeout(void *arg)
1309 {
1310         struct scsi_low_softc *slp = arg;
1311
1312         crit_enter();
1313         scsi_low_timeout_check(slp);
1314         (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1315                 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1316         crit_exit();
1317 }
1318
1319 static int
1320 scsi_low_timeout_check(struct scsi_low_softc *slp)
1321 {
1322         struct targ_info *ti;
1323         struct lun_info *li;
1324         struct slccb *cb = NULL;                /* XXX */
1325
1326         /* selection restart */
1327         if (slp->sl_retry_sel != 0)
1328         {
1329                 slp->sl_retry_sel = 0;
1330                 if (slp->sl_Tnexus != NULL)
1331                         goto step1;
1332
1333                 cb = TAILQ_FIRST(&slp->sl_start);
1334                 if (cb == NULL)
1335                         goto step1;
1336
1337                 if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
1338                 {
1339                         cb->ccb_flags |= CCB_NORETRY;
1340                         cb->ccb_error |= SELTIMEOUTIO;
1341                         if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1342                                 panic("%s: ccb not finished", slp->sl_xname);
1343                 }
1344
1345                 if (slp->sl_Tnexus == NULL)
1346                         scsi_low_start(slp);
1347         }
1348
1349         /* call hardware timeout */
1350 step1:
1351         if (slp->sl_funcs->scsi_low_timeout != NULL)
1352         {
1353                 (*slp->sl_funcs->scsi_low_timeout) (slp);
1354         }
1355         
1356         if (slp->sl_timeout_count ++ < 
1357             SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
1358                 return 0;
1359
1360         slp->sl_timeout_count = 0;
1361         if (slp->sl_nio > 0)
1362         {
1363                 if ((cb = slp->sl_Qnexus) != NULL)
1364                 {
1365                         cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1366                         if (cb->ccb_tc < 0)
1367                                 goto bus_reset;
1368                 }
1369                 else if (slp->sl_disc == 0)
1370                 {
1371                         if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
1372                                 return 0;
1373
1374                         cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1375                         if (cb->ccb_tc < 0)
1376                                 goto bus_reset;
1377                 }
1378                 else TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
1379                 {
1380                         if (ti->ti_disc == 0)
1381                                 continue;
1382
1383                         LIST_FOREACH(li, &ti->ti_litab, lun_chain)
1384                         {
1385                                 TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
1386                                 {
1387                                         cb->ccb_tc -=
1388                                                 SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
1389                                         if (cb->ccb_tc < 0)
1390                                                 goto bus_reset;
1391                                 }
1392                         }
1393                 }
1394
1395         }
1396         else if ((slp->sl_flags & HW_POWERCTRL) != 0)
1397         {
1398                 if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
1399                         return 0;
1400
1401                 if (slp->sl_active != 0)
1402                 {
1403                         slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1404                         slp->sl_active = 0;
1405                         return 0;
1406                 }
1407
1408                 slp->sl_powc --;
1409                 if (slp->sl_powc < 0)
1410                 {
1411                         slp->sl_powc = SCSI_LOW_POWDOWN_TC;
1412                         slp->sl_flags |= HW_POWDOWN;
1413                         (*slp->sl_funcs->scsi_low_power)
1414                                         (slp, SCSI_LOW_POWDOWN);
1415                 }
1416         }
1417         return 0;
1418
1419 bus_reset:
1420         cb->ccb_error |= TIMEOUTIO;
1421         kprintf("%s: slccb (0x%lx) timeout!\n", slp->sl_xname, (u_long) cb);
1422         scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
1423         scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
1424         scsi_low_start(slp);
1425         return ERESTART;
1426 }
1427
1428
1429 static int
1430 scsi_low_abort_ccb(struct scsi_low_softc *slp, struct slccb *cb)
1431 {
1432         struct targ_info *ti;
1433         struct lun_info *li;
1434         u_int msg;
1435
1436         if (cb == NULL)
1437                 return EINVAL;
1438         if ((cb->ccb_omsgoutflag & 
1439              (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
1440                 return EBUSY;
1441
1442         ti = cb->ti;
1443         li = cb->li;
1444         if (cb->ccb_tag == SCSI_LOW_UNKTAG)
1445                 msg = SCSI_LOW_MSG_ABORT;
1446         else
1447                 msg = SCSI_LOW_MSG_ABORT_QTAG;
1448
1449         cb->ccb_error |= ABORTIO;
1450         cb->ccb_flags |= CCB_NORETRY;
1451         scsi_low_ccb_message_assert(cb, msg);
1452
1453         if (cb == slp->sl_Qnexus)
1454         {
1455                 scsi_low_assert_msg(slp, ti, msg, 1);
1456         }
1457         else if ((cb->ccb_flags & CCB_DISCQ) != 0)
1458         {
1459                 if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
1460                         panic("%s: revoked ccb done", slp->sl_xname);
1461
1462                 cb->ccb_flags |= CCB_STARTQ;
1463                 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1464
1465                 if (slp->sl_Tnexus == NULL)
1466                         scsi_low_start(slp);
1467         }
1468         else
1469         {
1470                 if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
1471                         panic("%s: revoked ccb retried", slp->sl_xname);
1472         }
1473         return 0;
1474 }
1475
1476 /**************************************************************
1477  * Generic SCSI INTERFACE
1478  **************************************************************/
1479 int
1480 scsi_low_attach(struct scsi_low_softc *slp, int openings, int ntargs, int nluns,
1481                 int targsize, int lunsize)
1482 {
1483         struct targ_info *ti;
1484         struct lun_info *li;
1485         int i, nccb, rv;
1486
1487         slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;
1488
1489         if (slp->sl_osdep_fp == NULL)
1490                 panic("scsi_low: interface not spcified");
1491
1492         if (ntargs > SCSI_LOW_NTARGETS)
1493         {
1494                 kprintf("scsi_low: %d targets are too large\n", ntargs);
1495                 kprintf("change kernel options SCSI_LOW_NTARGETS");
1496                 return EINVAL;
1497         }
1498
1499         if (openings <= 0)
1500                 slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
1501         else
1502                 slp->sl_openings = openings;
1503         slp->sl_ntargs = ntargs;
1504         slp->sl_nluns = nluns;
1505         slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
1506
1507         if (lunsize < sizeof(struct lun_info))
1508                 lunsize = sizeof(struct lun_info);
1509
1510         if (targsize < sizeof(struct targ_info))
1511                 targsize = sizeof(struct targ_info);
1512
1513         slp->sl_targsize = targsize;
1514         for (i = 0; i < ntargs; i ++)
1515         {
1516                 ti = scsi_low_alloc_ti(slp, i);
1517                 ti->ti_lunsize = lunsize;
1518                 li = scsi_low_alloc_li(ti, 0, 1);
1519         }
1520
1521         /* initialize queue */
1522         nccb = openings * ntargs;
1523         if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
1524                 nccb = SCSI_LOW_NCCB;
1525         scsi_low_init_ccbque(nccb);
1526         TAILQ_INIT(&slp->sl_start);
1527
1528         /* call os depend attach */
1529         callout_init(&slp->sl_si.timeout_ch);
1530         callout_init(&slp->sl_si.engage_ch);
1531
1532         crit_enter();
1533         rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
1534         if (rv != 0)
1535         {
1536                 crit_exit();
1537                 kprintf("%s: scsi_low_attach: osdep attach failed\n",
1538                         slp->sl_xname);
1539                 return EINVAL;
1540         }
1541
1542         /* check hardware */
1543         SCSI_LOW_DELAY(1000);   /* wait for 1ms */
1544         if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
1545         {
1546                 crit_exit();
1547                 kprintf("%s: scsi_low_attach: initialization failed\n",
1548                         slp->sl_xname);
1549                 return EINVAL;
1550         }
1551
1552         /* start watch dog */
1553         slp->sl_timeout_count = 0;
1554         (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1555                  (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
1556         LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);
1557
1558         /* fake call */
1559         scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));
1560
1561 #ifdef  SCSI_LOW_START_UP_CHECK
1562         /* probing devices */
1563         scsi_low_start_up(slp);
1564 #endif  /* SCSI_LOW_START_UP_CHECK */
1565
1566         /* call os depend attach done*/
1567         (*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
1568         crit_exit();
1569         return 0;
1570 }
1571
1572 int
1573 scsi_low_dettach(struct scsi_low_softc *slp)
1574 {
1575         int rv;
1576
1577         crit_enter();
1578         if (scsi_low_is_busy(slp) != 0)
1579         {
1580                 crit_exit();
1581                 return EBUSY;
1582         }
1583
1584         scsi_low_deactivate(slp);
1585
1586         rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
1587         if (rv != 0)
1588         {
1589                 crit_exit();
1590                 return EBUSY;
1591         }
1592
1593         scsi_low_free_ti(slp);
1594         LIST_REMOVE(slp, sl_chain);
1595         crit_exit();
1596         return 0;
1597 }
1598
1599 /**************************************************************
1600  * Generic enqueue
1601  **************************************************************/
1602 static int
1603 scsi_low_enqueue(struct scsi_low_softc *slp, struct targ_info *ti,
1604                  struct lun_info *li, struct slccb *cb, u_int flags,
1605                  u_int msg)
1606 {       
1607
1608         cb->ti = ti;
1609         cb->li = li;
1610
1611         scsi_low_ccb_message_assert(cb, msg);
1612
1613         cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
1614         scsi_low_alloc_qtag(cb);
1615
1616         cb->ccb_flags = flags | CCB_STARTQ;
1617         cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1618         cb->ccb_error |= PENDINGIO;
1619
1620         if ((flags & CCB_URGENT) != 0)
1621         {
1622                 TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1623         }
1624         else
1625         {
1626                 TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
1627         }
1628
1629         slp->sl_nio ++;
1630
1631         if (slp->sl_Tnexus == NULL)
1632                 scsi_low_start(slp);
1633         return 0;
1634 }
1635
1636 static int
1637 scsi_low_message_enqueue(struct scsi_low_softc *slp, struct targ_info *ti,
1638                          struct lun_info *li, u_int flags)
1639 {
1640         struct slccb *cb;
1641         u_int tmsgflags;
1642
1643         tmsgflags = ti->ti_setup_msg;
1644         ti->ti_setup_msg = 0;
1645
1646         flags |= CCB_NORETRY;
1647         if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
1648                 return ENOMEM;
1649
1650         cb->osdep = NULL;
1651         cb->bp = NULL;
1652         scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
1653         return 0;
1654 }
1655
1656 /**************************************************************
1657  * Generic Start & Done
1658  **************************************************************/
1659 #define SLSC_MODE_SENSE_SHORT   0x1a
1660 static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0}; 
1661 static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0, 
1662                               sizeof(struct scsi_low_mode_sense_data), 0}; 
1663 static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0, 
1664                               sizeof(struct scsi_low_inq_data), 0}; 
1665 static u_int8_t unit_ready_cmd[6];
1666 static int scsi_low_setup_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1667 static int scsi_low_sense_abort_start (struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
1668 static int scsi_low_resume (struct scsi_low_softc *);
1669
1670 static void
1671 scsi_low_unit_ready_cmd(struct slccb *cb)
1672 {
1673         cb->ccb_scp.scp_cmd = unit_ready_cmd;
1674         cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
1675         cb->ccb_scp.scp_datalen = 0;
1676         cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1677         cb->ccb_tcmax = 15;
1678 }
1679
1680 static int
1681 scsi_low_sense_abort_start(struct scsi_low_softc *slp, struct targ_info *ti,
1682                            struct lun_info *li, struct slccb *cb)
1683 {
1684         cb->ccb_scp.scp_cmdlen = 6;
1685         SCSI_LOW_BZERO(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
1686         cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
1687         cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
1688         cb->ccb_scp.scp_cmd = cb->ccb_scsi_cmd;
1689         cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
1690         cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
1691         cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1692         cb->ccb_tcmax = 15;
1693         scsi_low_ccb_message_clear(cb);
1694         if ((cb->ccb_flags & CCB_CLEARQ) != 0)
1695         {
1696                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
1697         }
1698         else
1699         {
1700                 SCSI_LOW_BZERO(&cb->ccb_sense, sizeof(cb->ccb_sense));
1701 #ifdef  SCSI_LOW_NEGOTIATE_BEFORE_SENSE
1702                 scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
1703 #endif  /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
1704         }
1705
1706         return SCSI_LOW_START_NO_QTAG;
1707 }
1708
1709 static int
1710 scsi_low_setup_start(struct scsi_low_softc *slp, struct targ_info *ti,
1711                      struct lun_info *li, struct slccb *cb)
1712 {
1713         switch(li->li_state)
1714         {
1715         case SCSI_LOW_LUN_SLEEP:
1716                 scsi_low_unit_ready_cmd(cb);
1717                 break;
1718
1719         case SCSI_LOW_LUN_START:
1720                 cb->ccb_scp.scp_cmd = ss_cmd;
1721                 cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
1722                 cb->ccb_scp.scp_datalen = 0;
1723                 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1724                 cb->ccb_tcmax = 30;
1725                 break;
1726
1727         case SCSI_LOW_LUN_INQ:
1728                 cb->ccb_scp.scp_cmd = inq_cmd;
1729                 cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
1730                 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
1731                 cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
1732                 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1733                 cb->ccb_tcmax = 15;
1734                 break;
1735
1736         case SCSI_LOW_LUN_MODEQ:
1737                 cb->ccb_scp.scp_cmd = sms_cmd;
1738                 cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
1739                 cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
1740                 cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
1741                 cb->ccb_scp.scp_direction = SCSI_LOW_READ;
1742                 cb->ccb_tcmax = 15;
1743                 return SCSI_LOW_START_QTAG;
1744
1745         default:
1746                 panic("%s: no setup phase", slp->sl_xname);
1747         }
1748
1749         return SCSI_LOW_START_NO_QTAG;
1750 }
1751
1752 static int
1753 scsi_low_resume(struct scsi_low_softc *slp)
1754 {
1755         if (slp->sl_flags & HW_RESUME)
1756                 return EJUSTRETURN;
1757         slp->sl_flags &= ~HW_POWDOWN;
1758         if (slp->sl_funcs->scsi_low_power != NULL)
1759         {
1760                 slp->sl_flags |= HW_RESUME;
1761                 slp->sl_rstep = 0;
1762                 (*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
1763                 (*slp->sl_osdep_fp->scsi_low_osdep_timeout)
1764                                         (slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
1765                                          SCSI_LOW_TIMEOUT_START);
1766                 return EJUSTRETURN;
1767         }
1768         return 0;
1769 }
1770
1771 static void
1772 scsi_low_start(struct scsi_low_softc *slp)
1773 {
1774         struct targ_info *ti;
1775         struct lun_info *li;
1776         struct slccb *cb;
1777         int rv;
1778
1779         /* check hardware exists or under initializations ? */
1780         if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 0)
1781                 return;
1782
1783         /* check hardware power up ? */
1784         if ((slp->sl_flags & HW_POWERCTRL) != 0)
1785         {
1786                 slp->sl_active ++;
1787                 if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
1788                 {
1789                         if (scsi_low_resume(slp) == EJUSTRETURN)
1790                                 return;
1791                 }
1792         }
1793
1794         /* setup nexus */
1795 #ifdef  SCSI_LOW_DIAGNOSTIC
1796         if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
1797         {
1798                 scsi_low_info(slp, NULL, "NEXUS INCONSISTENT");
1799                 panic("%s: inconsistent", slp->sl_xname);
1800         }
1801 #endif  /* SCSI_LOW_DIAGNOSTIC */
1802
1803         TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
1804         {
1805                 li = cb->li;
1806
1807                 if (li->li_disc == 0)
1808                 {
1809                         goto scsi_low_cmd_start;
1810                 }
1811                 else if (li->li_nqio > 0)
1812                 {
1813                         if (li->li_nqio < li->li_maxnqio ||
1814                             (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1815                                 goto scsi_low_cmd_start;
1816                 }
1817         }
1818         return;
1819
1820 scsi_low_cmd_start:
1821         cb->ccb_flags &= ~CCB_STARTQ;
1822         TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
1823         ti = cb->ti;
1824
1825         /* clear all error flag bits (for restart) */
1826         cb->ccb_error = 0;
1827         cb->ccb_datalen = -1;
1828         cb->ccb_scp.scp_status = ST_UNKNOWN;
1829
1830         /* setup nexus pointer */
1831         slp->sl_Qnexus = cb;
1832         slp->sl_Lnexus = li;
1833         slp->sl_Tnexus = ti;
1834
1835         /* initialize msgsys */
1836         scsi_low_init_msgsys(slp, ti);
1837
1838         /* exec cmd */
1839         if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
1840         {
1841                 /* CA state or forced abort */
1842                 rv = scsi_low_sense_abort_start(slp, ti, li, cb);
1843         }
1844         else if (li->li_state >= SCSI_LOW_LUN_OK)
1845         {
1846                 cb->ccb_flags &= ~CCB_INTERNAL;
1847                 rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
1848                 if (cb->ccb_msgoutflag != 0)
1849                 {
1850                         scsi_low_ccb_message_exec(slp, cb);
1851                 }
1852         }
1853         else
1854         {
1855                 cb->ccb_flags |= CCB_INTERNAL;
1856                 rv = scsi_low_setup_start(slp, ti, li, cb);
1857         }
1858
1859         /* allocate qtag */
1860 #define SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)
1861
1862         if (rv == SCSI_LOW_START_QTAG &&
1863             (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
1864             li->li_maxnqio > 0)
1865         {
1866                 u_int qmsg;
1867
1868                 scsi_low_activate_qtag(cb);
1869                 if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
1870                      SCSI_LOW_CMD_ORDERED_QTAG) != 0)
1871                         qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
1872                 else if ((cb->ccb_flags & CCB_URGENT) != 0)
1873                         qmsg = SCSI_LOW_MSG_HEAD_QTAG;
1874                 else
1875                         qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
1876                 scsi_low_assert_msg(slp, ti, qmsg, 0);
1877         }
1878
1879         /* timeout */
1880         if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
1881                 cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
1882         cb->ccb_tc = cb->ccb_tcmax;
1883
1884         /* setup saved scsi data pointer */
1885         cb->ccb_sscp = cb->ccb_scp;
1886
1887         /* setup current scsi pointer */ 
1888         slp->sl_scp = cb->ccb_sscp;
1889         slp->sl_error = cb->ccb_error;
1890
1891         /* assert always an identify msg */
1892         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);
1893
1894         /* debug section */
1895 #ifdef  SCSI_LOW_DIAGNOSTIC
1896         scsi_low_msg_log_init(&ti->ti_log_msgin);
1897         scsi_low_msg_log_init(&ti->ti_log_msgout);
1898 #endif  /* SCSI_LOW_DIAGNOSTIC */
1899
1900         /* selection start */
1901         slp->sl_selid = cb;
1902         rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
1903         if (rv == SCSI_LOW_START_OK)
1904         {
1905 #ifdef  SCSI_LOW_STATICS
1906                 scsi_low_statics.nexus_win ++;
1907 #endif  /* SCSI_LOW_STATICS */
1908                 return;
1909         }
1910
1911         scsi_low_arbit_fail(slp, cb);
1912 #ifdef  SCSI_LOW_STATICS
1913         scsi_low_statics.nexus_fail ++;
1914 #endif  /* SCSI_LOW_STATICS */
1915 }
1916
1917 void
1918 scsi_low_arbit_fail(struct scsi_low_softc *slp, struct slccb *cb)
1919 {
1920         struct targ_info *ti = cb->ti;
1921
1922         scsi_low_deactivate_qtag(cb);
1923         scsi_low_ccb_message_retry(cb);
1924         cb->ccb_flags |= CCB_STARTQ;
1925         TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
1926
1927         scsi_low_bus_release(slp, ti);
1928
1929         cb->ccb_selrcnt ++;
1930         if (slp->sl_disc == 0)
1931         {
1932 #ifdef  SCSI_LOW_DIAGNOSTIC
1933                 kprintf("%s: try selection again\n", slp->sl_xname);
1934 #endif  /* SCSI_LOW_DIAGNOSTIC */
1935                 slp->sl_retry_sel = 1;
1936         }
1937 }
1938
1939 static void
1940 scsi_low_bus_release(struct scsi_low_softc *slp, struct targ_info *ti)
1941 {
1942         if (ti->ti_disc > 0)
1943         {
1944                 SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
1945         }
1946         else
1947         {
1948                 SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
1949         }
1950
1951         /* clear all nexus pointer */
1952         slp->sl_Qnexus = NULL;
1953         slp->sl_Lnexus = NULL;
1954         slp->sl_Tnexus = NULL;
1955
1956         /* clear selection assert */
1957         slp->sl_selid = NULL;
1958
1959         /* clear nexus data */
1960         slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
1961
1962         /* clear phase change counter */
1963         slp->sl_ph_count = 0;
1964 }
1965
1966 static int
1967 scsi_low_setup_done(struct scsi_low_softc *slp, struct slccb *cb)
1968 {
1969         struct targ_info *ti;
1970         struct lun_info *li;
1971
1972         ti = cb->ti;
1973         li = cb->li;
1974
1975         if (cb->ccb_rcnt >= slp->sl_max_retry)
1976         {
1977                 cb->ccb_error |= ABORTIO;
1978                 return SCSI_LOW_DONE_COMPLETE;
1979         }
1980
1981         /* XXX: special huck for selection timeout */
1982         if (li->li_state == SCSI_LOW_LUN_SLEEP &&
1983             (cb->ccb_error & SELTIMEOUTIO) != 0)
1984         {
1985                 cb->ccb_error |= ABORTIO;
1986                 return SCSI_LOW_DONE_COMPLETE;
1987         }
1988
1989         switch(li->li_state)
1990         {
1991         case SCSI_LOW_LUN_INQ:
1992                 if (cb->ccb_error != 0)
1993                 {
1994                         li->li_diskflags &= 
1995                                 ~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
1996                         if (li->li_lun > 0)
1997                                 goto resume;
1998                         ti->ti_diskflags &=
1999                                 ~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
2000                 }
2001                 else if ((li->li_inq.sd_version & 7) >= 2 ||
2002                          (li->li_inq.sd_len >= 4))
2003                 {
2004                         if ((li->li_inq.sd_support & 0x2) == 0)
2005                                 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2006                         if ((li->li_inq.sd_support & 0x8) == 0)
2007                                 li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
2008                         if (li->li_lun > 0)
2009                                 goto resume;
2010                         if ((li->li_inq.sd_support & 0x10) == 0)
2011                                 ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
2012                         if ((li->li_inq.sd_support & 0x20) == 0)
2013                                 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
2014                         if ((li->li_inq.sd_support & 0x40) == 0)
2015                                 ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
2016                 }
2017                 else
2018                 {
2019                         li->li_diskflags &= 
2020                                 ~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
2021                         if (li->li_lun > 0)
2022                                 goto resume;
2023                         ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
2024                 }
2025                 ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
2026 resume:
2027                 scsi_low_calcf_target(ti);
2028                 scsi_low_calcf_lun(li);
2029                 break;
2030
2031         case SCSI_LOW_LUN_MODEQ:
2032                 if (cb->ccb_error != 0)
2033                 {
2034                         if (cb->ccb_error & SENSEIO)
2035                         {
2036 #ifdef  SCSI_LOW_DEBUG
2037                                 if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
2038                                 {
2039                                         kprintf("SENSE: [%x][%x][%x][%x][%x]\n",
2040                                         (u_int) cb->ccb_sense.error_code,
2041                                         (u_int) cb->ccb_sense.segment,
2042                                         (u_int) cb->ccb_sense.flags,
2043                                         (u_int) cb->ccb_sense.add_sense_code,
2044                                         (u_int) cb->ccb_sense.add_sense_code_qual);
2045                                 }
2046 #endif  /* SCSI_LOW_DEBUG */
2047                         }
2048                         else
2049                         {
2050                                 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2051                         }
2052                 }
2053                 else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
2054                 {       
2055                         if (li->li_sms.sms_cmp.cmp_qc & 0x02)
2056                                 li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
2057                         else
2058                                 li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
2059                         if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
2060                                 li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
2061                 }
2062                 li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
2063                 scsi_low_calcf_lun(li);
2064                 break;
2065
2066         default:
2067                 break;
2068         }
2069
2070         li->li_state ++;
2071         if (li->li_state == SCSI_LOW_LUN_OK)
2072         {
2073                 scsi_low_calcf_target(ti);
2074                 scsi_low_calcf_lun(li);
2075                 if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
2076                     (slp->sl_show_result & SHOW_CALCF_RES) != 0)
2077                 {
2078                         scsi_low_calcf_show(li);
2079                 }       
2080         }
2081
2082         cb->ccb_rcnt --;
2083         return SCSI_LOW_DONE_RETRY;
2084 }
2085
2086 static int
2087 scsi_low_done(struct scsi_low_softc *slp, struct slccb *cb)
2088 {
2089         int rv;
2090
2091         if (cb->ccb_error == 0)
2092         {
2093                 if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
2094                 {
2095 #ifdef  SCSI_LOW_QCLEAR_AFTER_CA
2096                         /* XXX:
2097                          * SCSI-2 draft suggests 
2098                          * page 0x0a QErr bit determins if
2099                          * the target aborts or continues
2100                          * the queueing io's after CA state resolved.
2101                          * However many targets seem not to support
2102                          * the page 0x0a. Thus we should manually clear the
2103                          * queuing io's after CA state.
2104                          */
2105                         if ((cb->ccb_flags & CCB_CLEARQ) == 0)
2106                         {
2107                                 cb->ccb_rcnt --;
2108                                 cb->ccb_flags |= CCB_CLEARQ;
2109                                 goto retry;
2110                         }
2111 #endif  /* SCSI_LOW_QCLEAR_AFTER_CA */
2112
2113                         if ((cb->ccb_flags & CCB_SENSE) != 0)
2114                                 cb->ccb_error |= (SENSEIO | ABORTIO);
2115                         cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
2116                 }
2117                 else switch (cb->ccb_sscp.scp_status)
2118                 {
2119                 case ST_GOOD:
2120                 case ST_MET:
2121                 case ST_INTERGOOD:
2122                 case ST_INTERMET:
2123                         if (cb->ccb_datalen == 0 ||
2124                             cb->ccb_scp.scp_datalen == 0)
2125                                 break;
2126
2127                         if (cb->ccb_scp.scp_cmdlen > 0 &&
2128                             (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
2129                              SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
2130                                 break;
2131
2132                         cb->ccb_error |= PDMAERR;
2133                         break;
2134
2135                 case ST_BUSY:
2136                 case ST_QUEFULL:
2137                         cb->ccb_error |= (BUSYERR | STATERR);
2138                         break;
2139
2140                 case ST_CONFLICT:
2141                         cb->ccb_error |= (STATERR | ABORTIO);
2142                         break;
2143
2144                 case ST_CHKCOND:
2145                 case ST_CMDTERM:
2146                         if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
2147                         {
2148                                 cb->ccb_rcnt --;
2149                                 cb->ccb_flags |= CCB_SENSE;
2150                                 goto retry;
2151                         }
2152                         cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
2153                         break;
2154
2155                 case ST_UNKNOWN:
2156                 default:
2157                         cb->ccb_error |= FATALIO;
2158                         break;
2159                 }
2160         }
2161         else
2162         {
2163                 if (cb->ccb_flags & CCB_SENSE)
2164                 {
2165                         cb->ccb_error |= (SENSEERR | ABORTIO);
2166                 }
2167                 cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
2168         }
2169
2170         /* internal ccb */
2171         if ((cb->ccb_flags & CCB_INTERNAL) != 0)
2172         {
2173                 if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
2174                         goto retry;
2175         }
2176
2177         /* check a ccb msgout flag */
2178         if (cb->ccb_omsgoutflag != 0)
2179         {
2180 #define SCSI_LOW_MSG_ABORT_OK   (SCSI_LOW_MSG_ABORT | \
2181                                  SCSI_LOW_MSG_ABORT_QTAG | \
2182                                  SCSI_LOW_MSG_CLEAR_QTAG | \
2183                                  SCSI_LOW_MSG_TERMIO)
2184
2185                 if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
2186                 {
2187                         cb->ccb_error |= ABORTIO;
2188                 }
2189         }
2190
2191         /* call OS depend done */
2192         if (cb->osdep != NULL)
2193         {
2194                 rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
2195                 if (rv == EJUSTRETURN)
2196                         goto retry;
2197         }
2198         else if (cb->ccb_error != 0)
2199         {
2200                 if (cb->ccb_rcnt >= slp->sl_max_retry)
2201                         cb->ccb_error |= ABORTIO;
2202
2203                 if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
2204                     (cb->ccb_error & ABORTIO) == 0)
2205                         goto retry;
2206         }
2207
2208         /* free our target */
2209 #ifdef  SCSI_LOW_DEBUG
2210         if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2211         {
2212                 kprintf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
2213                 scsi_low_print(slp, NULL);
2214         }
2215 #endif  /* SCSI_LOW_DEBUG */
2216
2217         scsi_low_deactivate_qtag(cb);
2218         scsi_low_dealloc_qtag(cb);
2219         scsi_low_free_ccb(cb);
2220         slp->sl_nio --;
2221         return SCSI_LOW_DONE_COMPLETE;
2222
2223 retry:
2224 #ifdef  SCSI_LOW_DEBUG
2225         if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
2226         {
2227                 kprintf("** SCSI_LOW_DONE_RETRY ===============\n");
2228                 scsi_low_print(slp, NULL);
2229         }
2230 #endif  /* SCSI_LOW_DEBUG */
2231                 
2232         cb->ccb_rcnt ++;
2233         scsi_low_deactivate_qtag(cb);
2234         scsi_low_ccb_message_retry(cb);
2235         return SCSI_LOW_DONE_RETRY;
2236 }
2237
2238 /**************************************************************
2239  * Reset
2240  **************************************************************/
2241 static void
2242 scsi_low_reset_nexus_target(struct scsi_low_softc *slp, struct targ_info *ti,
2243                             int fdone)
2244 {
2245         struct lun_info *li;
2246
2247         LIST_FOREACH(li, &ti->ti_litab, lun_chain)
2248         {
2249                 scsi_low_reset_nexus_lun(slp, li, fdone);
2250                 li->li_state = SCSI_LOW_LUN_SLEEP;
2251                 li->li_maxnqio = 0;
2252         }
2253
2254         ti->ti_disc = 0;
2255         ti->ti_setup_msg = 0;
2256         ti->ti_setup_msg_done = 0;
2257
2258         ti->ti_osynch.offset = ti->ti_osynch.period = 0;
2259         ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
2260
2261         ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
2262         ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;
2263
2264         if (slp->sl_funcs->scsi_low_targ_init != NULL)
2265         {
2266                 ((*slp->sl_funcs->scsi_low_targ_init)
2267                         (slp, ti, SCSI_LOW_INFO_REVOKE));
2268         }
2269         scsi_low_calcf_target(ti);
2270
2271         LIST_FOREACH(li, &ti->ti_litab, lun_chain)
2272         {
2273                 li->li_flags = 0;
2274
2275                 li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
2276                 li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;
2277
2278                 if (slp->sl_funcs->scsi_low_lun_init != NULL)
2279                 {
2280                         ((*slp->sl_funcs->scsi_low_lun_init)
2281                                 (slp, ti, li, SCSI_LOW_INFO_REVOKE));
2282                 }
2283                 scsi_low_calcf_lun(li);
2284         }
2285 }
2286
2287 static void
2288 scsi_low_reset_nexus(struct scsi_low_softc *slp, int fdone)
2289 {
2290         struct targ_info *ti;
2291         struct slccb *cb, *topcb;
2292
2293         if ((cb = slp->sl_Qnexus) != NULL)
2294         {
2295                 topcb = scsi_low_revoke_ccb(slp, cb, fdone);
2296         }
2297         else
2298         {
2299                 topcb = NULL;
2300         }
2301
2302         TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
2303         {
2304                 scsi_low_reset_nexus_target(slp, ti, fdone);
2305                 scsi_low_bus_release(slp, ti);
2306                 scsi_low_init_msgsys(slp, ti);
2307         }
2308
2309         if (topcb != NULL)
2310         {
2311                 topcb->ccb_flags |= CCB_STARTQ;
2312                 TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
2313         }
2314
2315         slp->sl_disc = 0;
2316         slp->sl_retry_sel = 0;
2317         slp->sl_flags &= ~HW_PDMASTART;
2318 }
2319
2320 /* misc */
2321 static int tw_pos;
2322 static char tw_chars[] = "|/-\\";
2323 #define TWIDDLEWAIT             10000
2324
2325 static void
2326 scsi_low_twiddle_wait(void)
2327 {
2328         cnputc('\b');
2329         cnputc(tw_chars[tw_pos++]);
2330         tw_pos %= (sizeof(tw_chars) - 1);
2331         SCSI_LOW_DELAY(TWIDDLEWAIT);
2332 }
2333
2334 void
2335 scsi_low_bus_reset(struct scsi_low_softc *slp)
2336 {
2337         int i;
2338
2339         (*slp->sl_funcs->scsi_low_bus_reset) (slp);
2340
2341         kprintf("%s: try to reset scsi bus  ", slp->sl_xname);
2342         for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
2343                 scsi_low_twiddle_wait();
2344         cnputc('\b');
2345         kprintf("\n");
2346 }
2347
2348 int
2349 scsi_low_restart(struct scsi_low_softc *slp, int flags, u_char *s)
2350 {
2351         int error;
2352
2353         if (s != NULL)
2354                 kprintf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);
2355
2356         if ((error = scsi_low_init(slp, flags)) != 0)
2357                 return error;
2358
2359         scsi_low_start(slp);
2360         return 0;
2361 }
2362
2363 /**************************************************************
2364  * disconnect and reselect
2365  **************************************************************/
2366 #define MSGCMD_LUN(msg) (msg & 0x07)
2367
2368 static struct slccb *
2369 scsi_low_establish_ccb(struct targ_info *ti, struct lun_info *li, scsi_low_tag_t tag)
2370 {
2371         struct scsi_low_softc *slp = ti->ti_sc;
2372         struct slccb *cb;
2373
2374         if (li == NULL)
2375                 return NULL;
2376
2377         TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
2378                 if (cb->ccb_tag == tag)
2379                         goto found;
2380         return cb;
2381
2382         /* 
2383          * establish our ccb nexus
2384          */
2385 found:
2386 #ifdef  SCSI_LOW_DEBUG
2387         if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
2388         {
2389                 kprintf("%s: nexus(0x%lx) abort check start\n",
2390                         slp->sl_xname, (u_long) cb);
2391                 cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
2392                 scsi_low_revoke_ccb(slp, cb, 1);
2393                 return NULL;
2394         }
2395
2396         if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
2397         {
2398                 if (cb->ccb_omsgoutflag == 0)
2399                         scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
2400         }
2401 #endif  /* SCSI_LOW_DEBUG */
2402
2403         TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
2404         cb->ccb_flags &= ~CCB_DISCQ;
2405         slp->sl_Qnexus = cb;
2406
2407         slp->sl_scp = cb->ccb_sscp;
2408         slp->sl_error |= cb->ccb_error;
2409
2410         slp->sl_disc --;
2411         ti->ti_disc --;
2412         li->li_disc --;
2413
2414         /* inform "ccb nexus established" to the host driver */
2415         (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2416
2417         /* check msg */
2418         if (cb->ccb_msgoutflag != 0)
2419         {
2420                 scsi_low_ccb_message_exec(slp, cb);
2421         }
2422
2423         return cb;
2424 }
2425
2426 struct targ_info *
2427 scsi_low_reselected(struct scsi_low_softc *slp, u_int targ)
2428 {
2429         struct targ_info *ti;
2430         struct slccb *cb;
2431         u_char *s;
2432
2433         /* 
2434          * Check select vs reselected collision.
2435          */
2436
2437         if ((cb = slp->sl_selid) != NULL)
2438         {
2439                 scsi_low_arbit_fail(slp, cb);
2440 #ifdef  SCSI_LOW_STATICS
2441                 scsi_low_statics.nexus_conflict ++;
2442 #endif  /* SCSI_LOW_STATICS */
2443         }
2444
2445         /* 
2446          * Check if no current active nexus.
2447          */
2448         if (slp->sl_Tnexus != NULL)
2449         {
2450                 s = "host busy";
2451                 goto world_restart;
2452         }
2453
2454         /* 
2455          * Check a valid target id asserted ?
2456          */
2457         if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
2458         {
2459                 s = "scsi id illegal";
2460                 goto world_restart;
2461         }
2462
2463         /* 
2464          * Check the target scsi status.
2465          */
2466         ti = slp->sl_ti[targ];
2467         if (ti->ti_phase != PH_DISC && ti->ti_phase != PH_NULL)
2468         {
2469                 s = "phase mismatch";
2470                 goto world_restart;
2471         }
2472
2473         /* 
2474          * Setup init msgsys
2475          */
2476         slp->sl_error = 0;
2477         scsi_low_init_msgsys(slp, ti);
2478
2479         /* 
2480          * Establish our target nexus
2481          */
2482         SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
2483         slp->sl_Tnexus = ti;
2484 #ifdef  SCSI_LOW_STATICS
2485         scsi_low_statics.nexus_reselected ++;
2486 #endif  /* SCSI_LOW_STATICS */
2487         return ti;
2488
2489 world_restart:
2490         kprintf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
2491         scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, 
2492                          "reselect: scsi world confused");
2493         return NULL;
2494 }
2495
2496 /**************************************************************
2497  * cmd out pointer setup
2498  **************************************************************/
2499 int
2500 scsi_low_cmd(struct scsi_low_softc *slp, struct targ_info *ti)
2501 {
2502         struct slccb *cb = slp->sl_Qnexus;
2503         
2504         slp->sl_ph_count ++;
2505         if (cb == NULL)
2506         {
2507                 /*
2508                  * no ccb, abort!
2509                  */
2510                 slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
2511                 slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
2512                 slp->sl_scp.scp_datalen = 0;
2513                 slp->sl_scp.scp_direction = SCSI_LOW_READ;
2514                 slp->sl_error |= FATALIO;
2515                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2516                 SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
2517                 return EINVAL;
2518         }
2519         else 
2520         {
2521 #ifdef  SCSI_LOW_DEBUG
2522                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
2523                 {
2524                         scsi_low_test_cmdlnk(slp, cb);
2525                 }
2526 #endif  /* SCSI_LOW_DEBUG */
2527         }
2528         return 0;
2529 }
2530
2531 /**************************************************************
2532  * data out pointer setup
2533  **************************************************************/
2534 int
2535 scsi_low_data(struct scsi_low_softc *slp, struct targ_info *ti,
2536               struct buf **bp, int direction)
2537 {
2538         struct slccb *cb = slp->sl_Qnexus;
2539
2540         if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
2541         {
2542                 *bp = cb->bp;
2543                 return 0;
2544         }
2545
2546         slp->sl_error |= (FATALIO | PDMAERR);
2547         slp->sl_scp.scp_datalen = 0;
2548         slp->sl_scp.scp_direction = direction;
2549         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2550         if (ti->ti_ophase != ti->ti_phase)
2551         {
2552                 char *s;
2553
2554                 if (cb == NULL)
2555                         s = "DATA PHASE: ccb nexus not found";
2556                 else
2557                         s = "DATA PHASE: xfer direction mismatch";
2558                 SCSI_LOW_INFO(slp, ti, s);
2559         }
2560
2561         *bp = NULL;
2562         return EINVAL;
2563 }
2564
2565 /**************************************************************
2566  * MSG_SYS 
2567  **************************************************************/
2568 #define MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
2569 #define MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
2570 #define MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
2571 #define MSGIN_WIDTHP(ti) ((ti)->ti_msgin[3])
2572 #define MSGIN_DATA_LAST 0x30
2573
2574 static int scsi_low_errfunc_synch (struct scsi_low_softc *, u_int);
2575 static int scsi_low_errfunc_wide (struct scsi_low_softc *, u_int);
2576 static int scsi_low_errfunc_identify (struct scsi_low_softc *, u_int);
2577 static int scsi_low_errfunc_qtag (struct scsi_low_softc *, u_int);
2578
2579 static int scsi_low_msgfunc_synch (struct scsi_low_softc *);
2580 static int scsi_low_msgfunc_wide (struct scsi_low_softc *);
2581 static int scsi_low_msgfunc_identify (struct scsi_low_softc *);
2582 static int scsi_low_msgfunc_abort (struct scsi_low_softc *);
2583 static int scsi_low_msgfunc_qabort (struct scsi_low_softc *);
2584 static int scsi_low_msgfunc_qtag (struct scsi_low_softc *);
2585 static int scsi_low_msgfunc_reset (struct scsi_low_softc *);
2586
2587 struct scsi_low_msgout_data {
2588         u_int   md_flags;
2589         u_int8_t md_msg;
2590         int (*md_msgfunc) (struct scsi_low_softc *);
2591         int (*md_errfunc) (struct scsi_low_softc *, u_int);
2592 #define MSG_RELEASE_ATN 0x0001
2593         u_int md_condition;
2594 };
2595
2596 struct scsi_low_msgout_data scsi_low_msgout_data[] = {
2597 /* 0 */ {SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
2598 /* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
2599 /* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
2600 /* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
2601 /* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
2602 /* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2603 /* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
2604 /* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG,  MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2605 /* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2606 /* 9 */{SCSI_LOW_MSG_HEAD_QTAG,  MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
2607 /* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL,  MSG_RELEASE_ATN},
2608 /* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
2609 /* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
2610 /* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
2611 /* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
2612 /* 15 */{SCSI_LOW_MSG_ALL, 0},
2613 };
2614
2615 static int scsi_low_msginfunc_ext (struct scsi_low_softc *);
2616 static int scsi_low_synch (struct scsi_low_softc *);
2617 static int scsi_low_wide (struct scsi_low_softc *);
2618 static int scsi_low_msginfunc_msg_reject (struct scsi_low_softc *);
2619 static int scsi_low_msginfunc_rejop (struct scsi_low_softc *);
2620 static int scsi_low_msginfunc_rp (struct scsi_low_softc *);
2621 static int scsi_low_msginfunc_sdp (struct scsi_low_softc *);
2622 static int scsi_low_msginfunc_disc (struct scsi_low_softc *);
2623 static int scsi_low_msginfunc_cc (struct scsi_low_softc *);
2624 static int scsi_low_msginfunc_lcc (struct scsi_low_softc *);
2625 static int scsi_low_msginfunc_parity (struct scsi_low_softc *);
2626 static int scsi_low_msginfunc_noop (struct scsi_low_softc *);
2627 static int scsi_low_msginfunc_simple_qtag (struct scsi_low_softc *);
2628 static int scsi_low_msginfunc_i_wide_residue (struct scsi_low_softc *);
2629
2630 struct scsi_low_msgin_data {
2631         u_int md_len;
2632         int (*md_msgfunc) (struct scsi_low_softc *);
2633 };
2634
2635 struct scsi_low_msgin_data scsi_low_msgin_data[] = {
2636 /* 0 */ {1,     scsi_low_msginfunc_cc},
2637 /* 1 */ {2,     scsi_low_msginfunc_ext},
2638 /* 2 */ {1,     scsi_low_msginfunc_sdp},
2639 /* 3 */ {1,     scsi_low_msginfunc_rp},
2640 /* 4 */ {1,     scsi_low_msginfunc_disc},
2641 /* 5 */ {1,     scsi_low_msginfunc_rejop},
2642 /* 6 */ {1,     scsi_low_msginfunc_rejop},
2643 /* 7 */ {1,     scsi_low_msginfunc_msg_reject},
2644 /* 8 */ {1,     scsi_low_msginfunc_noop},
2645 /* 9 */ {1,     scsi_low_msginfunc_parity},
2646 /* a */ {1,     scsi_low_msginfunc_lcc},
2647 /* b */ {1,     scsi_low_msginfunc_lcc},
2648 /* c */ {1,     scsi_low_msginfunc_rejop},
2649 /* d */ {2,     scsi_low_msginfunc_rejop},
2650 /* e */ {1,     scsi_low_msginfunc_rejop},
2651 /* f */ {1,     scsi_low_msginfunc_rejop},
2652 /* 0x10 */ {1,  scsi_low_msginfunc_rejop},
2653 /* 0x11 */ {1,  scsi_low_msginfunc_rejop},
2654 /* 0x12 */ {1,  scsi_low_msginfunc_rejop},
2655 /* 0x13 */ {1,  scsi_low_msginfunc_rejop},
2656 /* 0x14 */ {1,  scsi_low_msginfunc_rejop},
2657 /* 0x15 */ {1,  scsi_low_msginfunc_rejop},
2658 /* 0x16 */ {1,  scsi_low_msginfunc_rejop},
2659 /* 0x17 */ {1,  scsi_low_msginfunc_rejop},
2660 /* 0x18 */ {1,  scsi_low_msginfunc_rejop},
2661 /* 0x19 */ {1,  scsi_low_msginfunc_rejop},
2662 /* 0x1a */ {1,  scsi_low_msginfunc_rejop},
2663 /* 0x1b */ {1,  scsi_low_msginfunc_rejop},
2664 /* 0x1c */ {1,  scsi_low_msginfunc_rejop},
2665 /* 0x1d */ {1,  scsi_low_msginfunc_rejop},
2666 /* 0x1e */ {1,  scsi_low_msginfunc_rejop},
2667 /* 0x1f */ {1,  scsi_low_msginfunc_rejop},
2668 /* 0x20 */ {2,  scsi_low_msginfunc_simple_qtag},
2669 /* 0x21 */ {2,  scsi_low_msginfunc_rejop},
2670 /* 0x22 */ {2,  scsi_low_msginfunc_rejop},
2671 /* 0x23 */ {2,  scsi_low_msginfunc_i_wide_residue},
2672 /* 0x24 */ {2,  scsi_low_msginfunc_rejop},
2673 /* 0x25 */ {2,  scsi_low_msginfunc_rejop},
2674 /* 0x26 */ {2,  scsi_low_msginfunc_rejop},
2675 /* 0x27 */ {2,  scsi_low_msginfunc_rejop},
2676 /* 0x28 */ {2,  scsi_low_msginfunc_rejop},
2677 /* 0x29 */ {2,  scsi_low_msginfunc_rejop},
2678 /* 0x2a */ {2,  scsi_low_msginfunc_rejop},
2679 /* 0x2b */ {2,  scsi_low_msginfunc_rejop},
2680 /* 0x2c */ {2,  scsi_low_msginfunc_rejop},
2681 /* 0x2d */ {2,  scsi_low_msginfunc_rejop},
2682 /* 0x2e */ {2,  scsi_low_msginfunc_rejop},
2683 /* 0x2f */ {2,  scsi_low_msginfunc_rejop},
2684 /* 0x30 */ {1,  scsi_low_msginfunc_rejop}       /* default rej op */
2685 };
2686
2687 /**************************************************************
2688  * msgout
2689  **************************************************************/
2690 static int
2691 scsi_low_msgfunc_synch(struct scsi_low_softc *slp)
2692 {
2693         struct targ_info *ti = slp->sl_Tnexus;
2694         int ptr = ti->ti_msgoutlen;
2695
2696         ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
2697         ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
2698         ti->ti_msgoutstr[ptr + 3] = ti->ti_maxsynch.period;
2699         ti->ti_msgoutstr[ptr + 4] = ti->ti_maxsynch.offset;
2700         return MSG_EXTEND_SYNCHLEN + 2;
2701 }
2702
2703 static int
2704 scsi_low_msgfunc_wide(struct scsi_low_softc *slp)
2705 {
2706         struct targ_info *ti = slp->sl_Tnexus;
2707         int ptr = ti->ti_msgoutlen;
2708
2709         ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
2710         ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
2711         ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
2712         return MSG_EXTEND_WIDELEN + 2;
2713 }
2714
2715 static int
2716 scsi_low_msgfunc_identify(struct scsi_low_softc *slp)
2717 {
2718         struct targ_info *ti = slp->sl_Tnexus;
2719         struct lun_info *li = slp->sl_Lnexus;
2720         struct slccb *cb = slp->sl_Qnexus;
2721         int ptr = ti->ti_msgoutlen;
2722         u_int8_t msg;
2723
2724         msg = MSG_IDENTIFY;
2725         if (cb == NULL)
2726         {
2727                 slp->sl_error |= FATALIO;
2728                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2729                 SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
2730         }
2731         else
2732         {
2733                 if (scsi_low_is_disconnect_ok(cb) != 0)
2734                         msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
2735                 else
2736                         msg |= li->li_lun;
2737
2738                 if (ti->ti_phase == PH_MSGOUT)
2739                 {
2740                         (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
2741                         if (cb->ccb_tag == SCSI_LOW_UNKTAG)
2742                         {
2743                                 (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2744                         }
2745                 }
2746         }
2747         ti->ti_msgoutstr[ptr + 0] = msg;
2748         return 1;
2749 }
2750
2751 static int
2752 scsi_low_msgfunc_abort(struct scsi_low_softc *slp)
2753 {
2754         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
2755         return 1;
2756 }
2757
2758 static int
2759 scsi_low_msgfunc_qabort(struct scsi_low_softc *slp)
2760 {
2761         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
2762         return 1;
2763 }
2764
2765 static int
2766 scsi_low_msgfunc_reset(struct scsi_low_softc *slp)
2767 {
2768         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
2769         return 1;
2770 }
2771
2772 static int
2773 scsi_low_msgfunc_qtag(struct scsi_low_softc *slp)
2774 {
2775         struct targ_info *ti = slp->sl_Tnexus;
2776         struct slccb *cb = slp->sl_Qnexus;
2777         int ptr = ti->ti_msgoutlen;
2778
2779         if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
2780         {
2781                 ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
2782                 return 1;
2783         }
2784         else
2785         {
2786                 ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
2787                 if (ti->ti_phase == PH_MSGOUT)
2788                 {
2789                         (*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
2790                 }
2791         }
2792         return 2;
2793 }
2794
2795 /*
2796  * The following functions are called when targets give unexpected
2797  * responces in msgin (after msgout).
2798  */
2799 static int
2800 scsi_low_errfunc_identify(struct scsi_low_softc *slp, u_int msgflags)
2801 {
2802         if (slp->sl_Lnexus != NULL)
2803         {
2804                 slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
2805                 scsi_low_calcf_lun(slp->sl_Lnexus);
2806         }
2807         return 0;
2808 }
2809
2810 static int
2811 scsi_low_errfunc_synch(struct scsi_low_softc *slp, u_int msgflags)
2812 {
2813         struct targ_info *ti = slp->sl_Tnexus;
2814
2815         MSGIN_PERIOD(ti) = 0;
2816         MSGIN_OFFSET(ti) = 0;
2817         scsi_low_synch(slp);
2818         return 0;
2819 }
2820
2821 static int
2822 scsi_low_errfunc_wide(struct scsi_low_softc *slp, u_int msgflags)
2823 {
2824         struct targ_info *ti = slp->sl_Tnexus;
2825
2826         MSGIN_WIDTHP(ti) = 0;
2827         scsi_low_wide(slp);
2828         return 0;
2829 }
2830
2831 static int
2832 scsi_low_errfunc_qtag(struct scsi_low_softc *slp, u_int msgflags)
2833 {
2834         if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
2835         {
2836                 if (slp->sl_Qnexus != NULL)
2837                 {
2838                         scsi_low_deactivate_qtag(slp->sl_Qnexus);
2839                 }
2840                 if (slp->sl_Lnexus != NULL)
2841                 {
2842                         slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
2843                         scsi_low_calcf_lun(slp->sl_Lnexus);
2844                 }
2845                 kprintf("%s: scsi_low: qtag msg rejected\n", slp->sl_xname);
2846         }
2847         return 0;
2848 }
2849
2850
2851 int
2852 scsi_low_msgout(struct scsi_low_softc *slp, struct targ_info *ti, u_int fl)
2853 {
2854         struct scsi_low_msgout_data *mdp;
2855         int len = 0;
2856
2857 #ifdef  SCSI_LOW_DIAGNOSTIC
2858         if (ti != slp->sl_Tnexus)
2859         {
2860                 scsi_low_print(slp, NULL);
2861                 panic("scsi_low_msgout: Target nexus inconsistent");
2862         }
2863 #endif  /* SCSI_LOW_DIAGNOSTIC */
2864
2865         slp->sl_ph_count ++;
2866         if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
2867         {
2868                 kprintf("%s: too many phase changes\n", slp->sl_xname);
2869                 slp->sl_error |= FATALIO;
2870                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
2871         }
2872                 
2873         /* STEP I.
2874          * Scsi phase changes.
2875          * Previously msgs asserted are accepted by our target or
2876          * processed by scsi_low_msgin.
2877          * Thus clear all saved informations.
2878          */
2879         if ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
2880         {
2881                 ti->ti_omsgflags = 0;
2882                 ti->ti_emsgflags = 0;
2883         }
2884         else if (slp->sl_atten == 0)
2885         {
2886         /* STEP II.
2887          * We did not assert attention, however still our target required
2888          * msgs. Resend previous msgs. 
2889          */
2890                 ti->ti_msgflags |= ti->ti_omsgflags;
2891                 ti->ti_omsgflags = 0;
2892 #ifdef  SCSI_LOW_DIAGNOSTIC
2893                 kprintf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
2894 #endif  /* SCSI_LOW_DIAGNOSTIC */
2895         }
2896
2897         /* STEP III.
2898          * We have no msgs. send MSG_NOOP (OK?)
2899          */
2900         if (scsi_low_is_msgout_continue(ti, 0) == 0)
2901                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);
2902
2903         /* STEP IV.
2904          * Process all msgs
2905          */
2906         ti->ti_msgoutlen = 0;
2907         slp->sl_clear_atten = 0;
2908         mdp = &scsi_low_msgout_data[0];
2909         for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
2910         {
2911                 if ((ti->ti_msgflags & mdp->md_flags) != 0)
2912                 {
2913                         ti->ti_omsgflags |= mdp->md_flags;
2914                         ti->ti_msgflags &= ~mdp->md_flags;
2915                         ti->ti_emsgflags = mdp->md_flags;
2916
2917                         ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
2918                         if (mdp->md_msgfunc != NULL)
2919                                 len = (*mdp->md_msgfunc) (slp);
2920                         else
2921                                 len = 1;
2922
2923 #ifdef  SCSI_LOW_DIAGNOSTIC
2924                         scsi_low_msg_log_write(&ti->ti_log_msgout,
2925                                &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
2926 #endif  /* SCSI_LOW_DIAGNOSTIC */
2927
2928                         ti->ti_msgoutlen += len;
2929                         if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
2930                         {
2931                                 slp->sl_clear_atten = 1;
2932                                 break;
2933                         }
2934
2935                         if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
2936                             ti->ti_msgflags == 0)
2937                                 break;
2938
2939                         if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
2940                                 break;
2941                 }
2942         }
2943
2944         if (scsi_low_is_msgout_continue(ti, 0) == 0)
2945                 slp->sl_clear_atten = 1;
2946
2947         return ti->ti_msgoutlen;
2948 }
2949
2950 /**************************************************************
2951  * msgin
2952  **************************************************************/
2953 static int
2954 scsi_low_msginfunc_noop(struct scsi_low_softc *slp)
2955 {
2956         return 0;
2957 }
2958
2959 static int
2960 scsi_low_msginfunc_rejop(struct scsi_low_softc *slp)
2961 {
2962         struct targ_info *ti = slp->sl_Tnexus;
2963         u_int8_t msg = ti->ti_msgin[0];
2964
2965         kprintf("%s: MSGIN: msg 0x%x rejected\n", slp->sl_xname, (u_int) msg);
2966         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
2967         return 0;
2968 }
2969
2970 static int
2971 scsi_low_msginfunc_cc(struct scsi_low_softc *slp)
2972 {
2973         struct lun_info *li;
2974
2975         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
2976
2977         /* validate status */
2978         if (slp->sl_Qnexus == NULL)
2979                 return ENOENT;
2980
2981         slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
2982         li = slp->sl_Lnexus;
2983         switch (slp->sl_scp.scp_status)
2984         {
2985         case ST_GOOD:
2986                 li->li_maxnqio = li->li_maxnexus;
2987                 break;
2988
2989         case ST_CHKCOND:
2990                 li->li_maxnqio = 0;
2991                 if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
2992                         scsi_low_reset_nexus_lun(slp, li, 0);
2993                 break;
2994
2995         case ST_BUSY:
2996                 li->li_maxnqio = 0;
2997                 break;
2998
2999         case ST_QUEFULL:
3000                 if (li->li_maxnexus >= li->li_nqio)
3001                         li->li_maxnexus = li->li_nqio - 1;
3002                 li->li_maxnqio = li->li_maxnexus;
3003                 break;
3004
3005         case ST_INTERGOOD:
3006         case ST_INTERMET:
3007                 slp->sl_error |= MSGERR;
3008                 break;
3009
3010         default:
3011                 break;
3012         }
3013         return 0;
3014 }
3015
3016 static int
3017 scsi_low_msginfunc_lcc(struct scsi_low_softc *slp)
3018 {
3019         struct targ_info *ti;
3020         struct lun_info *li;
3021         struct slccb *ncb, *cb;
3022
3023         ti = slp->sl_Tnexus;
3024         li = slp->sl_Lnexus;
3025         if ((cb = slp->sl_Qnexus) == NULL)
3026                 goto bad;
3027                 
3028         cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
3029         switch (slp->sl_scp.scp_status)
3030         {
3031         case ST_INTERGOOD:
3032         case ST_INTERMET:
3033                 li->li_maxnqio = li->li_maxnexus;
3034                 break;
3035
3036         default:
3037                 slp->sl_error |= MSGERR;
3038                 break;
3039         }
3040
3041         if ((li->li_flags & SCSI_LOW_LINK) == 0)
3042                 goto bad;
3043
3044         cb->ccb_error |= slp->sl_error;
3045         if (cb->ccb_error != 0)
3046                 goto bad;
3047
3048         TAILQ_FOREACH(ncb, &slp->sl_start, ccb_chain)
3049         {
3050                 if (ncb->li == li)
3051                         goto cmd_link_start;
3052         }
3053
3054
3055 bad:
3056         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
3057         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3058         return EIO;
3059
3060 cmd_link_start:
3061         ncb->ccb_flags &= ~CCB_STARTQ;
3062         TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);
3063
3064         scsi_low_dealloc_qtag(ncb);
3065         ncb->ccb_tag = cb->ccb_tag;
3066         ncb->ccb_otag = cb->ccb_otag;
3067         cb->ccb_tag = SCSI_LOW_UNKTAG;
3068         cb->ccb_otag = SCSI_LOW_UNKTAG;
3069         if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3070                 panic("%s: linked ccb retried", slp->sl_xname);
3071
3072         slp->sl_Qnexus = ncb;
3073         slp->sl_ph_count = 0;
3074
3075         ncb->ccb_error = 0;
3076         ncb->ccb_datalen = -1;
3077         ncb->ccb_scp.scp_status = ST_UNKNOWN;
3078         ncb->ccb_flags &= ~CCB_INTERNAL;
3079
3080         scsi_low_init_msgsys(slp, ti);
3081
3082         (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);
3083
3084         if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
3085                 ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3086         ncb->ccb_tc = ncb->ccb_tcmax;
3087
3088         /* setup saved scsi data pointer */
3089         ncb->ccb_sscp = ncb->ccb_scp;
3090         slp->sl_scp = ncb->ccb_sscp;
3091         slp->sl_error = ncb->ccb_error;
3092
3093 #ifdef  SCSI_LOW_DIAGNOSTIC
3094         scsi_low_msg_log_init(&ti->ti_log_msgin);
3095         scsi_low_msg_log_init(&ti->ti_log_msgout);
3096 #endif  /* SCSI_LOW_DIAGNOSTIC */
3097         return EJUSTRETURN;
3098 }
3099
3100 static int
3101 scsi_low_msginfunc_disc(struct scsi_low_softc *slp)
3102 {
3103         SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
3104         return 0;
3105 }
3106
3107 static int
3108 scsi_low_msginfunc_sdp(struct scsi_low_softc *slp)
3109 {
3110         struct slccb *cb = slp->sl_Qnexus;
3111
3112         if (cb != NULL)
3113         {
3114                 cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
3115                 cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
3116         }
3117         else
3118                 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3119         return 0;
3120 }
3121
3122 static int
3123 scsi_low_msginfunc_rp(struct scsi_low_softc *slp)
3124 {
3125         if (slp->sl_Qnexus != NULL)
3126                 slp->sl_scp = slp->sl_Qnexus->ccb_sscp;
3127         else
3128                 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
3129         return 0;
3130 }
3131
3132 static int
3133 scsi_low_synch(struct scsi_low_softc *slp)
3134 {
3135         struct targ_info *ti = slp->sl_Tnexus;
3136         u_int period = 0, offset = 0, speed;
3137         u_char *s;
3138         int error;
3139
3140         if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
3141              MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
3142              MSGIN_OFFSET(ti) == 0)
3143         {
3144                 if ((offset = MSGIN_OFFSET(ti)) != 0)
3145                         period = MSGIN_PERIOD(ti);
3146                 s = offset ? "synchronous" : "async";
3147         }
3148         else
3149         {
3150                 /* XXX:
3151                  * Target seems to be brain damaged.
3152                  * Force async transfer.
3153                  */
3154                 ti->ti_maxsynch.period = 0;
3155                 ti->ti_maxsynch.offset = 0;
3156                 kprintf("%s: target brain damaged. async transfer\n",
3157                         slp->sl_xname);
3158                 return EINVAL;
3159         }
3160
3161         ti->ti_maxsynch.period = period;
3162         ti->ti_maxsynch.offset = offset;
3163
3164         error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
3165         if (error != 0)
3166         {
3167                 /* XXX:
3168                  * Current period and offset are not acceptable 
3169                  * for our adapter.
3170                  * The adapter changes max synch and max offset.
3171                  */
3172                 kprintf("%s: synch neg failed. retry synch msg neg ...\n",
3173                         slp->sl_xname);
3174                 return error;
3175         }
3176
3177         ti->ti_osynch = ti->ti_maxsynch;
3178         if (offset > 0)
3179         {
3180                 ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
3181         }
3182
3183         /* inform data */
3184         if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
3185         {
3186 #ifdef  SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3187                 struct slccb *cb = slp->sl_Qnexus;
3188
3189                 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3190                         return 0;
3191 #endif  /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3192
3193                 kprintf("%s(%d:*): <%s> offset %d period %dns ",
3194                         slp->sl_xname, ti->ti_id, s, offset, period * 4);
3195
3196                 if (period != 0)
3197                 {
3198                         speed = 1000 * 10 / (period * 4);
3199                         kprintf("%d.%d M/s", speed / 10, speed % 10);
3200                 }
3201                 kprintf("\n");
3202         }
3203         return 0;
3204 }
3205
3206 static int
3207 scsi_low_wide(struct scsi_low_softc *slp)
3208 {
3209         struct targ_info *ti = slp->sl_Tnexus;
3210         int error;
3211
3212         ti->ti_width = MSGIN_WIDTHP(ti);
3213         error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
3214         if (error != 0)
3215         {
3216                 /* XXX:
3217                  * Current width is not acceptable for our adapter.
3218                  * The adapter changes max width.
3219                  */
3220                 kprintf("%s: wide neg failed. retry wide msg neg ...\n",
3221                         slp->sl_xname);
3222                 return error;
3223         }
3224
3225         ti->ti_owidth = ti->ti_width;
3226         if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3227         {
3228                 ti->ti_setup_msg_done |= 
3229                         (SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
3230         }
3231                 
3232         /* inform data */
3233         if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
3234         {
3235 #ifdef  SCSI_LOW_NEGOTIATE_BEFORE_SENSE
3236                 struct slccb *cb = slp->sl_Qnexus;
3237
3238                 if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
3239                         return 0;
3240 #endif  /* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
3241
3242                 kprintf("%s(%d:*): transfer width %d bits\n",
3243                         slp->sl_xname, ti->ti_id, 1 << (3 + ti->ti_width));
3244         }
3245         return 0;
3246 }
3247
3248 static int
3249 scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *slp)
3250 {
3251         struct targ_info *ti = slp->sl_Tnexus;
3252         scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];
3253
3254         if (slp->sl_Qnexus != NULL)
3255         {
3256                 if (slp->sl_Qnexus->ccb_tag != etag)
3257                 {
3258                         slp->sl_error |= FATALIO;
3259                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3260                         SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
3261                 }
3262         }
3263         else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
3264         {
3265 #ifdef  SCSI_LOW_DEBUG
3266                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
3267                         return 0;
3268 #endif  /* SCSI_LOW_DEBUG */
3269
3270                 slp->sl_error |= FATALIO;
3271                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
3272                 SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
3273         }
3274         return 0;
3275 }
3276
3277 static int
3278 scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *slp)
3279 {
3280         struct targ_info *ti = slp->sl_Tnexus;
3281         struct slccb *cb = slp->sl_Qnexus;
3282         int res = (int) ti->ti_msgin[1];
3283
3284         if (cb == NULL || res <= 0 ||
3285             (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
3286             (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
3287                 return EINVAL;
3288                 
3289         if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
3290                 return EINVAL;
3291
3292         slp->sl_scp.scp_datalen += res;
3293         slp->sl_scp.scp_data -= res;
3294         scsi_low_data_finish(slp);
3295         return 0;
3296 }
3297
3298 static int
3299 scsi_low_msginfunc_ext(struct scsi_low_softc *slp)
3300 {
3301         struct slccb *cb = slp->sl_Qnexus;
3302         struct lun_info *li = slp->sl_Lnexus;
3303         struct targ_info *ti = slp->sl_Tnexus;
3304         int count, retry;
3305         u_int32_t *ptr;
3306
3307         if (ti->ti_msginptr == 2)
3308         {
3309                 ti->ti_msginlen = ti->ti_msgin[1] + 2;
3310                 return 0;
3311         }
3312
3313         switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
3314         {
3315         case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
3316                 if (cb == NULL)
3317                         break;
3318
3319                 ptr = (u_int32_t *)(&ti->ti_msgin[3]);
3320                 count = (int) htonl((long) (*ptr));
3321                 if(slp->sl_scp.scp_datalen - count < 0 || 
3322                    slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
3323                         break;
3324
3325                 slp->sl_scp.scp_datalen -= count;
3326                 slp->sl_scp.scp_data += count;
3327                 return 0;
3328
3329         case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
3330                 if (li == NULL)
3331                         break;
3332
3333                 retry = scsi_low_synch(slp);
3334                 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
3335                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
3336
3337 #ifdef  SCSI_LOW_DEBUG
3338                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3339                 {
3340                         scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
3341                 }
3342 #endif  /* SCSI_LOW_DEBUG */
3343                 return 0;
3344
3345         case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
3346                 if (li == NULL)
3347                         break;
3348
3349                 retry = scsi_low_wide(slp);
3350                 if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
3351                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
3352
3353                 return 0;
3354
3355         default:
3356                 break;
3357         }
3358
3359         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3360         return EINVAL;
3361 }
3362
3363 static int
3364 scsi_low_msginfunc_parity(struct scsi_low_softc *slp)
3365 {
3366         struct targ_info *ti = slp->sl_Tnexus;
3367
3368         /* only I -> T, invalid! */
3369         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3370         return 0;
3371 }
3372
3373 static int
3374 scsi_low_msginfunc_msg_reject(struct scsi_low_softc *slp)
3375 {
3376         struct targ_info *ti = slp->sl_Tnexus;
3377         struct scsi_low_msgout_data *mdp;
3378         u_int msgflags;
3379
3380         if (ti->ti_emsgflags != 0)
3381         {
3382                 kprintf("%s: msg flags [0x%x] rejected\n",
3383                        slp->sl_xname, ti->ti_emsgflags);
3384                 msgflags = SCSI_LOW_MSG_REJECT;
3385                 mdp = &scsi_low_msgout_data[0];
3386                 for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
3387                 {
3388                         if ((ti->ti_emsgflags & mdp->md_flags) != 0)
3389                         {
3390                                 ti->ti_emsgflags &= ~mdp->md_flags;
3391                                 if (mdp->md_errfunc != NULL)
3392                                         (*mdp->md_errfunc) (slp, msgflags);
3393                                 break;
3394                         }
3395                 }
3396                 return 0;
3397         }
3398         else
3399         {
3400                 SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
3401                 slp->sl_error |= MSGERR;
3402         }
3403         return EINVAL;
3404 }
3405
3406 int
3407 scsi_low_msgin(struct scsi_low_softc *slp, struct targ_info *ti, u_int c)
3408 {
3409         struct scsi_low_msgin_data *sdp;
3410         struct lun_info *li;
3411         u_int8_t msg;
3412
3413 #ifdef  SCSI_LOW_DIAGNOSTIC
3414         if (ti != slp->sl_Tnexus)
3415         {
3416                 scsi_low_print(slp, NULL);
3417                 panic("scsi_low_msgin: Target nexus inconsistent");
3418         }
3419 #endif  /* SCSI_LOW_DIAGNOSTIC */
3420
3421         /*
3422          * Phase changes, clear the pointer.
3423          */
3424         if (ti->ti_ophase != ti->ti_phase)
3425         {
3426                 MSGINPTR_CLR(ti);
3427                 ti->ti_msgin_parity_error = 0;
3428
3429                 slp->sl_ph_count ++;
3430                 if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
3431                 {
3432                         kprintf("%s: too many phase changes\n", slp->sl_xname);
3433                         slp->sl_error |= FATALIO;
3434                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3435                 }
3436         }
3437
3438         /*
3439          * Store a current messages byte into buffer and 
3440          * wait for the completion of the current msg.
3441          */
3442         ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) c;
3443         if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
3444         {
3445                 ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
3446                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
3447         }       
3448
3449         /*
3450          * Check parity errors.
3451          */
3452         if ((c & SCSI_LOW_DATA_PE) != 0)
3453         {
3454                 ti->ti_msgin_parity_error ++;
3455                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
3456                 goto out;
3457         }
3458
3459         if (ti->ti_msgin_parity_error != 0)
3460                 goto out;
3461
3462         /*
3463          * Calculate messages length.
3464          */
3465         msg = ti->ti_msgin[0];
3466         if (msg < MSGIN_DATA_LAST)
3467                 sdp = &scsi_low_msgin_data[msg];
3468         else
3469                 sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];
3470
3471         if (ti->ti_msginlen == 0)
3472         {
3473                 ti->ti_msginlen = sdp->md_len;
3474         }
3475
3476         /*
3477          * Check comletion.
3478          */
3479         if (ti->ti_msginptr < ti->ti_msginlen)
3480                 return EJUSTRETURN;
3481
3482         /*
3483          * Do process.
3484          */
3485         if ((msg & MSG_IDENTIFY) == 0)
3486         {
3487                 if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
3488                         return EJUSTRETURN;
3489         }
3490         else
3491         {
3492                 li = slp->sl_Lnexus;
3493                 if (li == NULL)
3494                 {
3495                         li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
3496                         if (li == NULL)
3497                                 goto badlun;
3498                         slp->sl_Lnexus = li;
3499                         (*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
3500                 }       
3501                 else
3502                 {
3503                         if (MSGCMD_LUN(msg) != li->li_lun)
3504                                 goto badlun;
3505                 }
3506
3507                 if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
3508                 {
3509                         if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
3510                         {
3511 #ifdef  SCSI_LOW_DEBUG
3512                                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
3513                                 {
3514                                         goto out;
3515                                 }
3516 #endif  /* SCSI_LOW_DEBUG */
3517                                 goto badlun;
3518                         }
3519                 }
3520         }
3521         goto out;
3522
3523         /*
3524          * Msg process completed, reset msgin pointer and assert ATN if desired.
3525          */
3526 badlun:
3527         slp->sl_error |= FATALIO;
3528         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
3529         SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");
3530
3531 out:
3532         if (ti->ti_msginptr < ti->ti_msginlen)
3533                 return EJUSTRETURN;
3534
3535 #ifdef  SCSI_LOW_DIAGNOSTIC
3536         scsi_low_msg_log_write(&ti->ti_log_msgin,
3537                                &ti->ti_msgin[0], ti->ti_msginlen);
3538 #endif  /* SCSI_LOW_DIAGNOSTIC */
3539
3540         MSGINPTR_CLR(ti);
3541         return 0;
3542 }
3543
3544 /**********************************************************
3545  * disconnect
3546  **********************************************************/
3547 int
3548 scsi_low_disconnected(struct scsi_low_softc *slp, struct targ_info *ti)
3549 {
3550         struct slccb *cb = slp->sl_Qnexus;
3551
3552         /* check phase completion */
3553         switch (slp->sl_msgphase)
3554         {
3555         case MSGPH_RESET:
3556                 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3557                 scsi_low_msginfunc_cc(slp);
3558                 scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
3559                 goto io_resume;
3560
3561         case MSGPH_ABORT:
3562                 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3563                 scsi_low_msginfunc_cc(slp);
3564                 scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
3565                 goto io_resume;
3566
3567         case MSGPH_TERM:
3568                 scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
3569                 scsi_low_msginfunc_cc(slp);
3570                 goto io_resume;
3571
3572         case MSGPH_DISC:
3573                 if (cb != NULL)
3574                 {
3575                         struct lun_info *li;
3576
3577                         li = cb->li;
3578                         TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
3579                         cb->ccb_flags |= CCB_DISCQ;
3580                         cb->ccb_error |= slp->sl_error;
3581                         li->li_disc ++;
3582                         ti->ti_disc ++;
3583                         slp->sl_disc ++;
3584                 }
3585
3586 #ifdef  SCSI_LOW_STATICS
3587                 scsi_low_statics.nexus_disconnected ++;
3588 #endif  /* SCSI_LOW_STATICS */
3589
3590 #ifdef  SCSI_LOW_DEBUG
3591                 if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
3592                 {
3593                         kprintf("## SCSI_LOW_DISCONNECTED ===============\n");
3594                         scsi_low_print(slp, NULL);
3595                 }
3596 #endif  /* SCSI_LOW_DEBUG */
3597                 break;
3598
3599         case MSGPH_NULL:
3600                 slp->sl_error |= FATALIO;
3601                 if (ti->ti_phase == PH_SELSTART)
3602                         slp->sl_error |= SELTIMEOUTIO;
3603                 else
3604                         slp->sl_error |= UBFERR;
3605                 /* fall through */
3606
3607         case MSGPH_LCTERM:
3608         case MSGPH_CMDC:
3609 io_resume:
3610                 if (cb == NULL)
3611                         break;
3612
3613 #ifdef  SCSI_LOW_DEBUG
3614                 if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
3615                 {
3616                         if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
3617                             (cb->ccb_msgoutflag != 0 ||
3618                              (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
3619                         {
3620                                 scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
3621                         }
3622                 }
3623 #endif  /* SCSI_LOW_DEBUG */
3624
3625                 cb->ccb_error |= slp->sl_error;
3626                 if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
3627                 {
3628                         cb->ccb_flags |= CCB_STARTQ;
3629                         TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
3630                 }
3631                 break;
3632         }
3633
3634         scsi_low_bus_release(slp, ti);  
3635         scsi_low_start(slp);
3636         return 1;
3637 }
3638
3639 /**********************************************************
3640  * TAG operations
3641  **********************************************************/
3642 static int
3643 scsi_low_alloc_qtag(struct slccb *cb)
3644 {
3645         struct lun_info *li = cb->li;
3646         scsi_low_tag_t etag;
3647
3648         if (cb->ccb_otag != SCSI_LOW_UNKTAG)
3649                 return 0;
3650
3651 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3652         etag = ffs(li->li_qtagbits);
3653         if (etag == 0)
3654                 return ENOSPC;
3655
3656         li->li_qtagbits &= ~(1 << (etag - 1));
3657         cb->ccb_otag = etag;
3658         return 0;
3659
3660 #else   /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3661         for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
3662                 if (li->li_qtagarray[li->li_qd] == 0)
3663                         goto found;
3664
3665         for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
3666                 if (li->li_qtagarray[li->li_qd] == 0)
3667                         goto found;
3668
3669         return ENOSPC;
3670
3671 found:
3672         li->li_qtagarray[li->li_qd] ++;
3673         cb->ccb_otag = (li->li_qd ++);
3674         return 0;
3675 #endif  /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3676 }
3677         
3678 static int
3679 scsi_low_dealloc_qtag(struct slccb *cb)
3680 {
3681         struct lun_info *li = cb->li;
3682         scsi_low_tag_t etag;
3683
3684         if (cb->ccb_otag == SCSI_LOW_UNKTAG)
3685                 return 0;
3686
3687 #ifndef SCSI_LOW_ALT_QTAG_ALLOCATE
3688         etag = cb->ccb_otag - 1;
3689 #ifdef  SCSI_LOW_DIAGNOSTIC
3690         if (etag >= sizeof(li->li_qtagbits) * NBBY)
3691                 panic("scsi_low_dealloc_tag: illegal tag");
3692 #endif  /* SCSI_LOW_DIAGNOSTIC */
3693         li->li_qtagbits |= (1 << etag);
3694
3695 #else   /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3696         etag = cb->ccb_otag;
3697 #ifdef  SCSI_LOW_DIAGNOSTIC
3698         if (etag >= SCSI_LOW_MAXNEXUS)
3699                 panic("scsi_low_dealloc_tag: illegal tag");
3700 #endif  /* SCSI_LOW_DIAGNOSTIC */
3701         li->li_qtagarray[etag] --;
3702 #endif  /* SCSI_LOW_ALT_QTAG_ALLOCATE */
3703
3704         cb->ccb_otag = SCSI_LOW_UNKTAG;
3705         return 0;
3706 }
3707
3708 static struct slccb *
3709 scsi_low_revoke_ccb(struct scsi_low_softc *slp, struct slccb *cb, int fdone)
3710 {
3711         struct targ_info *ti = cb->ti;
3712         struct lun_info *li = cb->li;
3713
3714 #ifdef  SCSI_LOW_DIAGNOSTIC
3715         if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) == 
3716             (CCB_STARTQ | CCB_DISCQ))
3717         {
3718                 panic("%s: ccb in both queue", slp->sl_xname);
3719         }
3720 #endif  /* SCSI_LOW_DIAGNOSTIC */
3721
3722         if ((cb->ccb_flags & CCB_STARTQ) != 0)
3723         {
3724                 TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
3725         }
3726
3727         if ((cb->ccb_flags & CCB_DISCQ) != 0)
3728         {
3729                 TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
3730                 li->li_disc --;
3731                 ti->ti_disc --;
3732                 slp->sl_disc --;
3733         }
3734
3735         cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ | 
3736                            CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);
3737
3738         if (fdone != 0 &&
3739             (cb->ccb_rcnt ++ >= slp->sl_max_retry || 
3740              (cb->ccb_flags & CCB_NORETRY) != 0))
3741         {
3742                 cb->ccb_error |= FATALIO;
3743                 cb->ccb_flags &= ~CCB_AUTOSENSE;
3744                 if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
3745                         panic("%s: done ccb retried", slp->sl_xname);
3746                 return NULL;
3747         }
3748         else
3749         {
3750                 cb->ccb_error |= PENDINGIO;
3751                 scsi_low_deactivate_qtag(cb);
3752                 scsi_low_ccb_message_retry(cb);
3753                 cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
3754                 return cb;
3755         }
3756 }
3757
3758 static void
3759 scsi_low_reset_nexus_lun(struct scsi_low_softc *slp, struct lun_info *li, int fdone)
3760 {
3761         struct slccb *cb, *ncb, *ecb;
3762
3763         if (li == NULL)
3764                 return;
3765
3766         ecb = NULL;
3767         for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
3768         {
3769                 ncb = TAILQ_NEXT(cb, ccb_chain);
3770                 cb = scsi_low_revoke_ccb(slp, cb, fdone);
3771                 if (cb != NULL)
3772                 {
3773                         /*
3774                          * presumely keep ordering of io
3775                          */
3776                         cb->ccb_flags |= CCB_STARTQ;
3777                         if (ecb == NULL)
3778                         {
3779                                 TAILQ_INSERT_HEAD(&slp->sl_start,\
3780                                                   cb, ccb_chain);
3781                         }
3782                         else
3783                         {
3784                                 TAILQ_INSERT_AFTER(&slp->sl_start,\
3785                                                    ecb, cb, ccb_chain);
3786                         }
3787                         ecb = cb;
3788                 }
3789         }
3790 }
3791         
3792 /**************************************************************
3793  * Qurik setup
3794  **************************************************************/
3795 static void
3796 scsi_low_calcf_lun(struct lun_info *li)
3797 {
3798         struct targ_info *ti = li->li_ti;
3799         struct scsi_low_softc *slp = ti->ti_sc;
3800         u_int cfgflags, diskflags;
3801
3802         if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
3803                 cfgflags = li->li_cfgflags;
3804         else
3805                 cfgflags = 0;
3806
3807         diskflags = li->li_diskflags & li->li_quirks;
3808
3809         /* disconnect */
3810         li->li_flags &= ~SCSI_LOW_DISC;
3811         if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
3812             (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
3813             (cfgflags & SCSI_LOW_DISC) != 0)
3814                 li->li_flags |= SCSI_LOW_DISC;
3815
3816         /* parity */
3817         li->li_flags |= SCSI_LOW_NOPARITY;
3818         if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
3819             (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
3820             (cfgflags & SCSI_LOW_NOPARITY) == 0)
3821                 li->li_flags &= ~SCSI_LOW_NOPARITY;
3822
3823         /* qtag */
3824         if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
3825             (cfgflags & SCSI_LOW_QTAG) != 0 &&
3826             (diskflags & SCSI_LOW_DISK_QTAG) != 0)
3827         {
3828                 li->li_flags |= SCSI_LOW_QTAG;
3829                 li->li_maxnexus = SCSI_LOW_MAXNEXUS;
3830                 li->li_maxnqio = li->li_maxnexus;
3831         }
3832         else
3833         {
3834                 li->li_flags &= ~SCSI_LOW_QTAG;
3835                 li->li_maxnexus = 0;
3836                 li->li_maxnqio = li->li_maxnexus;
3837         }
3838
3839         /* cmd link */
3840         li->li_flags &= ~SCSI_LOW_LINK;
3841         if ((cfgflags & SCSI_LOW_LINK) != 0 &&
3842             (diskflags & SCSI_LOW_DISK_LINK) != 0)
3843                 li->li_flags |= SCSI_LOW_LINK;
3844
3845         /* compatible flags */
3846         li->li_flags &= ~SCSI_LOW_SYNC;
3847         if (ti->ti_maxsynch.offset > 0)
3848                 li->li_flags |= SCSI_LOW_SYNC;
3849
3850 #ifdef  SCSI_LOW_DEBUG
3851         if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3852         {
3853                 scsi_low_calcf_show(li);
3854         }
3855 #endif  /* SCSI_LOW_DEBUG */
3856 }
3857
3858 static void
3859 scsi_low_calcf_target(struct targ_info *ti)
3860 {
3861         struct scsi_low_softc *slp = ti->ti_sc;
3862         u_int offset, period, diskflags;
3863
3864         diskflags = ti->ti_diskflags & ti->ti_quirks;
3865
3866         /* synch */
3867         if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
3868             (diskflags & SCSI_LOW_DISK_SYNC) != 0)
3869         {
3870                 offset = ti->ti_maxsynch.offset;
3871                 period = ti->ti_maxsynch.period;
3872                 if (offset == 0 || period == 0)
3873                         offset = period = 0;
3874         }
3875         else
3876         {
3877                 offset = period = 0;
3878         }
3879         
3880         ti->ti_maxsynch.offset = offset;
3881         ti->ti_maxsynch.period = period;
3882
3883         /* wide */
3884         if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
3885              ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
3886                 ti->ti_width = SCSI_LOW_BUS_WIDTH_16;
3887
3888         if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
3889             ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
3890                 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
3891
3892         if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
3893         {
3894                 if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
3895                     ti->ti_maxsynch.period != ti->ti_osynch.period)
3896                         ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
3897                 if (ti->ti_width != ti->ti_owidth)
3898                         ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);
3899
3900                 ti->ti_osynch = ti->ti_maxsynch;
3901                 ti->ti_owidth = ti->ti_width;
3902         }
3903
3904 #ifdef  SCSI_LOW_DEBUG
3905         if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
3906         {
3907                 kprintf("%s(%d:*): max period(%dns) offset(%d) width(%d)\n",
3908                         slp->sl_xname, ti->ti_id,
3909                         ti->ti_maxsynch.period * 4,
3910                         ti->ti_maxsynch.offset,
3911                         ti->ti_width);
3912         }
3913 #endif  /* SCSI_LOW_DEBUG */
3914 }
3915
3916 static void
3917 scsi_low_calcf_show(struct lun_info *li)
3918 {
3919         struct targ_info *ti = li->li_ti;
3920         struct scsi_low_softc *slp = ti->ti_sc;
3921
3922         kprintf("%s(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
3923                 slp->sl_xname, ti->ti_id, li->li_lun,
3924                 ti->ti_maxsynch.period * 4,
3925                 ti->ti_maxsynch.offset,
3926                 ti->ti_width,
3927                 li->li_flags, SCSI_LOW_BITS);
3928 }
3929
3930 #ifdef  SCSI_LOW_START_UP_CHECK
3931 /**************************************************************
3932  * scsi world start up
3933  **************************************************************/
3934 static int scsi_low_poll (struct scsi_low_softc *, struct slccb *);
3935
3936 static int
3937 scsi_low_start_up(struct scsi_low_softc *slp)
3938 {
3939         struct targ_info *ti;
3940         struct lun_info *li;
3941         struct slccb *cb;
3942         int target, lun;
3943
3944         kprintf("%s: scsi_low: probing all devices ....\n", slp->sl_xname);
3945
3946         for (target = 0; target < slp->sl_ntargs; target ++)
3947         {
3948                 if (target == slp->sl_hostid)
3949                 {
3950                         if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3951                         {
3952                                 kprintf("%s: scsi_low: target %d (host card)\n",
3953                                         slp->sl_xname, target);
3954                         }
3955                         continue;
3956                 }
3957
3958                 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3959                 {
3960                         kprintf("%s: scsi_low: target %d lun ",
3961                                 slp->sl_xname, target);
3962                 }
3963
3964                 ti = slp->sl_ti[target];
3965                 for (lun = 0; lun < slp->sl_nluns; lun ++)
3966                 {
3967                         if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
3968                                 break;
3969
3970                         cb->osdep = NULL;
3971                         cb->bp = NULL;
3972
3973                         li = scsi_low_alloc_li(ti, lun, 1);
3974
3975                         scsi_low_enqueue(slp, ti, li, cb,
3976                                          CCB_AUTOSENSE | CCB_POLLED, 0);
3977
3978                         scsi_low_poll(slp, cb);
3979
3980                         if (li->li_state != SCSI_LOW_LUN_OK)
3981                                 break;
3982
3983                         if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3984                         {
3985                                 kprintf("%d ", lun);            
3986                         }
3987                 }
3988
3989                 if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
3990                 {
3991                         kprintf("\n");
3992                 }
3993         }
3994         return 0;
3995 }
3996
3997 static int
3998 scsi_low_poll(struct scsi_low_softc *slp, struct slccb *cb)
3999 {
4000         int tcount;
4001
4002         tcount = 0;
4003         while (slp->sl_nio > 0)
4004         {
4005                 SCSI_LOW_DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);
4006
4007                 (*slp->sl_funcs->scsi_low_poll) (slp);
4008                 if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
4009                         continue;
4010
4011                 tcount = 0;
4012                 scsi_low_timeout_check(slp);
4013         }
4014
4015         return 0;
4016 }
4017 #endif  /* SCSI_LOW_START_UP_CHECK */
4018
4019 /**********************************************************
4020  * DEBUG SECTION
4021  **********************************************************/
4022 #ifdef  SCSI_LOW_DEBUG
4023 static void
4024 scsi_low_test_abort(struct scsi_low_softc *slp, struct targ_info *ti,
4025                     struct lun_info *li)
4026 {
4027         struct slccb *acb;
4028
4029         if (li->li_disc > 1)
4030         {
4031                 acb = TAILQ_FIRST(&li->li_discq); 
4032                 if (scsi_low_abort_ccb(slp, acb) == 0)
4033                 {
4034                         kprintf("%s: aborting ccb(0x%lx) start\n",
4035                                 slp->sl_xname, (u_long) acb);
4036                 }
4037         }
4038 }
4039
4040 static void
4041 scsi_low_test_atten(struct scsi_low_softc *slp, struct targ_info *ti, u_int msg)
4042 {
4043         if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
4044                 scsi_low_assert_msg(slp, ti, msg, 0);
4045         else
4046                 kprintf("%s: atten check OK\n", slp->sl_xname);
4047 }
4048
4049 static void
4050 scsi_low_test_cmdlnk(struct scsi_low_softc *slp, struct slccb *cb)
4051 {
4052 #define SCSI_LOW_CMDLNK_NOK     (CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)
4053
4054         if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
4055                 return;
4056
4057         memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
4058                slp->sl_scp.scp_cmdlen);
4059         cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
4060         slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
4061 }
4062 #endif  /* SCSI_LOW_DEBUG */
4063
4064 /* static */ void
4065 scsi_low_info(struct scsi_low_softc *slp, struct targ_info *ti, u_char *s)
4066 {
4067         if (slp == NULL)
4068                 slp = LIST_FIRST(&sl_tab);
4069         if (s == NULL)
4070                 s = "no message";
4071
4072         kprintf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
4073         if (ti == NULL)
4074         {
4075                 TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
4076                 {
4077                         scsi_low_print(slp, ti);
4078                 }
4079         }
4080         else
4081         {
4082                 scsi_low_print(slp, ti);
4083         }
4084 }
4085
4086 static u_char *phase[] =
4087 {
4088         "FREE", "ARBSTART", "SELSTART", "SELECTED",
4089         "CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
4090 };
4091
4092 void
4093 scsi_low_print(struct scsi_low_softc *slp, struct targ_info *ti)
4094 {
4095         struct lun_info *li;
4096         struct slccb *cb;
4097         struct sc_p *sp;
4098
4099         if (ti == NULL || ti == slp->sl_Tnexus)
4100         {
4101                 ti = slp->sl_Tnexus;
4102                 li = slp->sl_Lnexus;
4103                 cb = slp->sl_Qnexus;
4104         }
4105         else
4106         {
4107                 li = LIST_FIRST(&ti->ti_litab);
4108                 cb = TAILQ_FIRST(&li->li_discq);
4109         }
4110         sp = &slp->sl_scp;
4111
4112         kprintf("%s: === NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
4113                 slp->sl_xname, (u_long) ti, (u_long) li, (u_long) cb,
4114                 slp->sl_nio);
4115
4116         /* target stat */
4117         if (ti != NULL)
4118         {
4119                 u_int flags = 0, maxnqio = 0, nqio = 0;
4120                 int lun = -1;
4121
4122                 if (li != NULL)
4123                 {
4124                         lun = li->li_lun;
4125                         flags = li->li_flags;
4126                         maxnqio = li->li_maxnqio;
4127                         nqio = li->li_nqio;
4128                 }
4129
4130                 kprintf("%s(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
4131                         slp->sl_xname,
4132                        ti->ti_id, lun, phase[(int) ti->ti_ophase], 
4133                        phase[(int) ti->ti_phase], ti->ti_disc,
4134                        nqio, maxnqio);
4135
4136                 if (cb != NULL)
4137                 {
4138 kprintf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
4139                        (u_int) cb->ccb_scp.scp_cmd[0],
4140                        cb->ccb_scp.scp_cmdlen, 
4141                        cb->ccb_datalen,
4142                        cb->ccb_scp.scp_datalen,
4143                        (u_int) cb->ccb_sscp.scp_status,
4144                        cb->ccb_error, SCSI_LOW_ERRORBITS);
4145                 }
4146
4147 kprintf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\n",
4148                (u_int) (ti->ti_msginptr), 
4149                (u_int) (ti->ti_msgin[0]),
4150                (u_int) (ti->ti_msgin[1]),
4151                (u_int) (ti->ti_msgin[2]),
4152                (u_int) (ti->ti_msgin[3]),
4153                (u_int) (ti->ti_msgin[4]),
4154                slp->sl_atten);
4155
4156 kprintf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
4157                 (u_int) ti->ti_msgflags,
4158                 (u_int) (ti->ti_msgoutstr[0]), 
4159                 (u_int) (ti->ti_msgoutstr[1]), 
4160                 (u_int) (ti->ti_msgoutstr[2]), 
4161                 (u_int) (ti->ti_msgoutstr[3]), 
4162                 (u_int) (ti->ti_msgoutstr[4]), 
4163                 ti->ti_msgoutlen,
4164                 flags, SCSI_LOW_BITS);
4165
4166 #ifdef  SCSI_LOW_DIAGNOSTIC
4167                 scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
4168                 scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
4169 #endif  /* SCSI_LOW_DIAGNOSTIC */
4170
4171         }
4172
4173         kprintf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
4174                (u_long) sp->scp_data,
4175                sp->scp_datalen,
4176                (u_int) sp->scp_status,
4177                slp->sl_error, SCSI_LOW_ERRORBITS);
4178 }