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