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