Merge from vendor branch HEIMDAL:
[dragonfly.git] / sys / bus / cam / scsi / scsi_low.h
1 /*      $FreeBSD: src/sys/cam/scsi/scsi_low.h,v 1.1.2.4 2001/07/22 00:21:41 non Exp $   */
2 /*      $DragonFly: src/sys/bus/cam/scsi/scsi_low.h,v 1.7 2004/09/17 02:38:10 joerg Exp $       */
3 /*      $NecBSD: scsi_low.h,v 1.24.10.5 2001/06/26 07:31:46 honda Exp $ */
4 /*      $NetBSD$        */
5
6 #define SCSI_LOW_DIAGNOSTIC
7 #define SCSI_LOW_ALT_QTAG_ALLOCATE
8
9 /*
10  * [NetBSD for NEC PC-98 series]
11  *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
12  *      NetBSD/pc98 porting staff. All rights reserved.
13  *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
14  *      Naofumi HONDA. All rights reserved.
15  *
16  * [Ported for FreeBSD CAM]
17  *  Copyright (c) 2000, 2001
18  *      MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
19  *      All rights reserved.
20  * 
21  *  Redistribution and use in source and binary forms, with or without
22  *  modification, are permitted provided that the following conditions
23  *  are met:
24  *  1. Redistributions of source code must retain the above copyright
25  *     notice, this list of conditions and the following disclaimer.
26  *  2. Redistributions in binary form must reproduce the above copyright
27  *     notice, this list of conditions and the following disclaimer in the
28  *     documentation and/or other materials provided with the distribution.
29  *  3. The name of the author may not be used to endorse or promote products
30  *     derived from this software without specific prior written permission.
31  * 
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGE.
43  */
44
45 #ifndef _SCSI_LOW_H_
46 #define _SCSI_LOW_H_
47
48 #define SCSI_LOW_INTERFACE_CAM
49
50 #include <sys/device_port.h>
51 #include <bus/cam/cam.h>
52 #include <bus/cam/cam_ccb.h>
53 #include <bus/cam/cam_sim.h>
54 #include <bus/cam/cam_xpt_sim.h>
55 #include <bus/cam/cam_debug.h>
56
57 #include "scsi_dvcfg.h"
58 #include <i386/isa/ccbque.h>
59
60 #undef  MSG_IDENTIFY
61 #define SCSI_LOW_DEBUGGER(dev)  Debugger((dev))
62 #define SCSI_LOW_DELAY(mu)      DELAY((mu))
63 #define SCSI_LOW_SPLSCSI        splcam
64 #define SCSI_LOW_BZERO(pt, size)        bzero((pt), (size))
65
66 typedef struct scsi_sense_data scsi_low_osdep_sense_data_t;
67
68 struct scsi_low_osdep_interface {
69         DEVPORT_DEVICE si_dev;
70
71         struct cam_sim *sim;
72         struct cam_path *path;
73
74         int si_poll_count;
75
76         struct callout engage_ch;
77         struct callout timeout_ch;
78 };
79
80 struct scsi_low_osdep_targ_interface {
81 };
82
83 struct scsi_low_osdep_lun_interface {
84 };
85
86 /******** os depend interface functions *************/
87 struct slccb;
88 struct scsi_low_softc;
89 #define SCSI_LOW_TIMEOUT_STOP           0
90 #define SCSI_LOW_TIMEOUT_START          1
91 #define SCSI_LOW_TIMEOUT_CH_IO          0
92 #define SCSI_LOW_TIMEOUT_CH_ENGAGE      1
93 #define SCSI_LOW_TIMEOUT_CH_RECOVER     2
94
95 struct scsi_low_osdep_funcs {
96         int (*scsi_low_osdep_attach) \
97                         (struct scsi_low_softc *);
98         int (*scsi_low_osdep_world_start) \
99                         (struct scsi_low_softc *);
100         int (*scsi_low_osdep_dettach) \
101                         (struct scsi_low_softc *);
102         int (*scsi_low_osdep_ccb_setup) \
103                         (struct scsi_low_softc *, struct slccb *);
104         int (*scsi_low_osdep_done) \
105                         (struct scsi_low_softc *, struct slccb *);
106         void (*scsi_low_osdep_timeout) \
107                         (struct scsi_low_softc *, int, int);
108 };
109
110 /*================================================
111  * Generic Scsi Low header file 
112  * (All os depend structures should be above!)
113  ================================================*/
114 /*************************************************
115  * Scsi low definitions
116  *************************************************/
117 #define SCSI_LOW_SYNC           DVF_SCSI_SYNC
118 #define SCSI_LOW_DISC           DVF_SCSI_DISC
119 #define SCSI_LOW_WAIT           DVF_SCSI_WAIT
120 #define SCSI_LOW_LINK           DVF_SCSI_LINK
121 #define SCSI_LOW_QTAG           DVF_SCSI_QTAG
122 #define SCSI_LOW_NOPARITY       DVF_SCSI_NOPARITY
123 #define SCSI_LOW_SAVESP         DVF_SCSI_SAVESP
124 #define SCSI_LOW_DEFCFG         DVF_SCSI_DEFCFG
125 #define SCSI_LOW_BITS           DVF_SCSI_BITS
126
127 #define SCSI_LOW_PERIOD(n)      DVF_SCSI_PERIOD(n)
128 #define SCSI_LOW_OFFSET(n)      DVF_SCSI_OFFSET(n)
129
130 /* host scsi id and targets macro */
131 #ifndef SCSI_LOW_NTARGETS
132 #define SCSI_LOW_NTARGETS                       8
133 #endif  /* SCSI_LOW_NTARGETS */
134 #define SCSI_LOW_NCCB                           128
135
136 #define SCSI_LOW_MAX_RETRY                      3
137 #define SCSI_LOW_MAX_SELECTION_RETRY            10
138
139 /* timeout control macro */
140 #define SCSI_LOW_TIMEOUT_HZ                     10
141 #define SCSI_LOW_MIN_TOUT                       12
142 #define SCSI_LOW_TIMEOUT_CHECK_INTERVAL         1
143 #define SCSI_LOW_POWDOWN_TC                     15
144 #define SCSI_LOW_MAX_PHCHANGES                  256
145 #define SCSI2_RESET_DELAY                       5000000
146
147 /* msg */
148 #define SCSI_LOW_MAX_MSGLEN                     32
149 #define SCSI_LOW_MSG_LOG_DATALEN                8
150
151 /*************************************************
152  * Scsi Data Pointer
153  *************************************************/
154 /* scsi pointer */
155 struct sc_p {
156         u_int8_t *scp_data;
157         int scp_datalen;
158
159         u_int8_t *scp_cmd;
160         int scp_cmdlen;
161
162         u_int8_t scp_direction;
163 #define SCSI_LOW_RWUNK  (-1)
164 #define SCSI_LOW_WRITE  0
165 #define SCSI_LOW_READ   1
166         u_int8_t scp_status;
167         u_int8_t scp_spare[2];
168 };
169
170 /*************************************************
171  * Command Control Block Structure
172  *************************************************/
173 typedef int scsi_low_tag_t;                     
174 struct targ_info;
175
176 #define SCSI_LOW_UNKLUN ((u_int) -1)
177 #define SCSI_LOW_UNKTAG ((scsi_low_tag_t) -1)
178
179 struct slccb {
180         TAILQ_ENTRY(slccb) ccb_chain;
181
182         void *osdep;                    /* os depend structure */
183
184         struct targ_info *ti;           /* targ_info */
185         struct lun_info *li;            /* lun info */
186         struct buf *bp;                 /* io bufs */
187
188         scsi_low_tag_t ccb_tag;         /* effective qtag */
189         scsi_low_tag_t ccb_otag;        /* allocated qtag */
190
191         /*****************************************
192          * Scsi data pointers (original and saved)
193          *****************************************/
194         struct sc_p ccb_scp;            /* given */
195         struct sc_p ccb_sscp;           /* saved scsi data pointer */
196         int ccb_datalen;                /* transfered data counter */
197
198         /*****************************************
199          * Msgout 
200          *****************************************/
201         u_int ccb_msgoutflag;
202         u_int ccb_omsgoutflag;
203
204         /*****************************************
205          * Error or Timeout counters
206          *****************************************/
207         u_int ccb_flags;
208 #define CCB_INTERNAL    0x0001
209 #define CCB_SENSE       0x0002
210 #define CCB_CLEARQ      0x0004
211 #define CCB_DISCQ       0x0008
212 #define CCB_STARTQ      0x0010
213 #define CCB_POLLED      0x0100  /* polling ccb */
214 #define CCB_NORETRY     0x0200  /* do NOT retry */
215 #define CCB_AUTOSENSE   0x0400  /* do a sence after CA */
216 #define CCB_URGENT      0x0800  /* an urgent ccb */
217 #define CCB_NOSDONE     0x1000  /* do not call an os done routine */
218 #define CCB_SCSIIO      0x2000  /* a normal scsi io coming from upper layer */
219 #define CCB_SILENT      0x4000  /* no terminate messages */
220
221         u_int ccb_error;
222
223         int ccb_rcnt;                   /* retry counter */
224         int ccb_selrcnt;                /* selection retry counter */
225         int ccb_tc;                     /* timer counter */
226         int ccb_tcmax;                  /* max timeout */
227
228         /*****************************************
229          * Sense data buffer
230          *****************************************/
231         u_int8_t ccb_scsi_cmd[12];
232         scsi_low_osdep_sense_data_t ccb_sense;
233 };
234
235 /*************************************************
236  * Slccb functions
237  *************************************************/
238 GENERIC_CCB_ASSERT(scsi_low, slccb)
239
240 /*************************************************
241  * Target and Lun structures
242  *************************************************/
243 struct scsi_low_softc;
244 LIST_HEAD(scsi_low_softc_tab, scsi_low_softc);
245 TAILQ_HEAD(targ_info_tab, targ_info);
246 LIST_HEAD(lun_info_tab, lun_info);
247
248 struct lun_info {
249         struct scsi_low_osdep_lun_interface li_sloi;
250
251         int li_lun;
252         struct targ_info *li_ti;                /* my target */
253
254         LIST_ENTRY(lun_info) lun_chain;         /* targ_info link */
255
256         struct slccbtab li_discq;                       /* disconnect queue */
257
258         /*
259          * qtag control
260          */
261         int li_maxnexus;
262         int li_maxnqio; 
263         int li_nqio;
264         int li_disc;
265
266 #define SCSI_LOW_MAXNEXUS (sizeof(u_int) * NBBY)
267         u_int li_qtagbits;
268
269 #ifdef  SCSI_LOW_ALT_QTAG_ALLOCATE
270         u_int8_t li_qtagarray[SCSI_LOW_MAXNEXUS];
271         u_int li_qd;
272 #endif  /* SCSI_LOW_ALT_QTAG_ALLOCATE */
273
274 #define SCSI_LOW_QFLAG_CA_QCLEAR        0x01
275         u_int li_qflags;
276
277         /*
278          * lun state
279          */
280 #define SCSI_LOW_LUN_SLEEP      0x00
281 #define SCSI_LOW_LUN_START      0x01
282 #define SCSI_LOW_LUN_INQ        0x02
283 #define SCSI_LOW_LUN_MODEQ      0x03
284 #define SCSI_LOW_LUN_OK         0x04
285         u_int li_state;                         /* target lun state */
286
287         /*
288          * lun control flags
289          */
290         u_int li_flags_valid;   /* valid flags */
291 #define SCSI_LOW_LUN_FLAGS_USER_VALID   0x0001
292 #define SCSI_LOW_LUN_FLAGS_DISK_VALID   0x0002
293 #define SCSI_LOW_LUN_FLAGS_QUIRKS_VALID 0x0004
294 #define SCSI_LOW_LUN_FLAGS_ALL_VALID \
295         (SCSI_LOW_LUN_FLAGS_USER_VALID | \
296          SCSI_LOW_LUN_FLAGS_DISK_VALID | SCSI_LOW_LUN_FLAGS_QUIRKS_VALID)
297
298         u_int li_flags;         /* real lun control flags */
299         u_int li_cfgflags;      /* lun control flags given by user */
300         u_int li_diskflags;     /* lun control flags given by hardware info */
301         u_int li_quirks;        /* lun control flags given by upper layer */
302
303         /* inq buffer */
304         struct scsi_low_inq_data {
305                 u_int8_t sd_type;       
306                 u_int8_t sd_sp1;
307                 u_int8_t sd_version;
308                 u_int8_t sd_resp;
309                 u_int8_t sd_len;
310                 u_int8_t sd_sp2[2];
311                 u_int8_t sd_support;
312         } __attribute__((packed)) li_inq;
313
314         /* modeq buffer */
315         struct scsi_low_mode_sense_data {
316                 u_int8_t sms_header[4];
317                 struct {
318                         u_int8_t cmp_page;
319                         u_int8_t cmp_length;
320                         u_int8_t cmp_rlec;
321                         u_int8_t cmp_qc;
322                         u_int8_t cmp_eca;
323                         u_int8_t cmp_spare[3];
324                 } __attribute__((packed)) sms_cmp;      
325         
326         } li_sms;       
327 };
328
329 struct scsi_low_msg_log {
330         int slml_ptr;
331         struct {
332                 u_int8_t msg[2];
333         } slml_msg[SCSI_LOW_MSG_LOG_DATALEN];
334 };
335
336 struct targ_info {
337         struct scsi_low_osdep_targ_interface ti_slti;
338
339         TAILQ_ENTRY(targ_info) ti_chain;        /* targ_info link */
340
341         struct scsi_low_softc *ti_sc;           /* our softc */
342         u_int ti_id;                            /* scsi id */
343
344         /*
345          * Lun chain
346          */
347         struct lun_info_tab ti_litab;           /* lun chain */
348
349         /*
350          * total disconnected nexus
351          */
352         int ti_disc;
353
354         /*
355          * Scsi phase control
356          */
357
358 #define PH_NULL         0x00
359 #define PH_ARBSTART     0x01
360 #define PH_SELSTART     0x02
361 #define PH_SELECTED     0x03
362 #define PH_CMD          0x04
363 #define PH_DATA         0x05
364 #define PH_MSGIN        0x06
365 #define PH_MSGOUT       0x07
366 #define PH_STAT         0x08
367 #define PH_DISC         0x09
368 #define PH_RESEL        0x0a
369         u_int ti_phase;                         /* scsi phase */
370         u_int ti_ophase;                        /* old scsi phase */
371
372         /*
373          * Msg in
374          */
375         u_int ti_msginptr;                      /* msgin ptr */
376         u_int ti_msginlen;                      /* expected msg length */
377         int ti_msgin_parity_error;              /* parity error detected */
378         u_int8_t ti_msgin[SCSI_LOW_MAX_MSGLEN]; /* msgin buffer */
379
380         /*
381          * Msg out
382          */
383         u_int ti_msgflags;                      /* msgs to be asserted */
384         u_int ti_omsgflags;                     /* msgs asserted */
385         u_int ti_emsgflags;                     /* a msg currently asserted */
386 #define SCSI_LOW_MSG_RESET      0x00000001
387 #define SCSI_LOW_MSG_REJECT     0x00000002
388 #define SCSI_LOW_MSG_PARITY     0x00000004
389 #define SCSI_LOW_MSG_ERROR      0x00000008
390 #define SCSI_LOW_MSG_IDENTIFY   0x00000010
391 #define SCSI_LOW_MSG_ABORT      0x00000020
392 #define SCSI_LOW_MSG_TERMIO     0x00000040
393 #define SCSI_LOW_MSG_SIMPLE_QTAG        0x00000080
394 #define SCSI_LOW_MSG_ORDERED_QTAG       0x00000100
395 #define SCSI_LOW_MSG_HEAD_QTAG          0x00000200
396 #define SCSI_LOW_MSG_ABORT_QTAG 0x00000400
397 #define SCSI_LOW_MSG_CLEAR_QTAG 0x00000800
398 #define SCSI_LOW_MSG_WIDE       0x00001000
399 #define SCSI_LOW_MSG_SYNCH      0x00002000
400 #define SCSI_LOW_MSG_NOOP       0x00004000
401 #define SCSI_LOW_MSG_LAST       0x00008000
402 #define SCSI_LOW_MSG_ALL        0xffffffff
403
404         /* msgout buffer */
405         u_int8_t ti_msgoutstr[SCSI_LOW_MAX_MSGLEN];     /* scsi msgout */
406         u_int ti_msgoutlen;                     /* msgout strlen */
407
408         /*
409          * target initialize msgout 
410          */
411         u_int ti_setup_msg;             /* setup msgout requests */
412         u_int ti_setup_msg_done;
413
414         /*
415          * synch and wide data info
416          */
417         u_int ti_flags_valid;   /* valid flags */
418 #define SCSI_LOW_TARG_FLAGS_USER_VALID          0x0001
419 #define SCSI_LOW_TARG_FLAGS_DISK_VALID          0x0002
420 #define SCSI_LOW_TARG_FLAGS_QUIRKS_VALID        0x0004
421 #define SCSI_LOW_TARG_FLAGS_ALL_VALID \
422         (SCSI_LOW_TARG_FLAGS_USER_VALID | \
423          SCSI_LOW_TARG_FLAGS_DISK_VALID | SCSI_LOW_TARG_FLAGS_QUIRKS_VALID)
424
425         u_int ti_diskflags;     /* given target disk flags */
426         u_int ti_quirks;        /* given target quirk */
427
428         struct synch {
429                 u_int8_t offset;
430                 u_int8_t period;
431         } ti_osynch, ti_maxsynch;               /* synch data */
432
433 #define SCSI_LOW_BUS_WIDTH_8    0
434 #define SCSI_LOW_BUS_WIDTH_16   1
435 #define SCSI_LOW_BUS_WIDTH_32   2
436         u_int ti_owidth, ti_width;
437
438         /*
439          * lun info size.
440          */
441         int ti_lunsize; 
442
443 #ifdef  SCSI_LOW_DIAGNOSTIC
444         struct scsi_low_msg_log ti_log_msgout;
445         struct scsi_low_msg_log ti_log_msgin;
446 #endif  /* SCSI_LOW_DIAGNOSTIC */
447 };
448
449 /*************************************************
450  * COMMON HEADER STRUCTURE
451  *************************************************/
452 struct scsi_low_softc;
453 struct proc;
454 typedef struct scsi_low_softc *sc_low_t;
455
456 #define SCSI_LOW_START_OK       0
457 #define SCSI_LOW_START_FAIL     1
458 #define SCSI_LOW_INFO_ALLOC     0
459 #define SCSI_LOW_INFO_REVOKE    1
460 #define SCSI_LOW_INFO_DEALLOC   2
461 #define SCSI_LOW_POWDOWN        1
462 #define SCSI_LOW_ENGAGE         2
463
464 #define SC_LOW_INIT_T (int (*) (sc_low_t, int))
465 #define SC_LOW_BUSRST_T (void (*) (sc_low_t))
466 #define SC_LOW_TARG_INIT_T (int (*) (sc_low_t, struct targ_info *, int))
467 #define SC_LOW_LUN_INIT_T (int (*) (sc_low_t, struct targ_info *, struct lun_info *, int))
468 #define SC_LOW_SELECT_T (int (*) (sc_low_t, struct slccb *))
469 #define SC_LOW_ATTEN_T (void (*) (sc_low_t))
470 #define SC_LOW_NEXUS_T (int (*) (sc_low_t))
471 #define SC_LOW_MSG_T (int (*) (sc_low_t, struct targ_info *, u_int))
472 #define SC_LOW_POLL_T (int (*) (void *))
473 #define SC_LOW_POWER_T (int (*) (sc_low_t, u_int))
474 #define SC_LOW_TIMEOUT_T (int (*) (sc_low_t))
475
476 struct scsi_low_funcs {
477         int (*scsi_low_init) (sc_low_t, int);
478         void (*scsi_low_bus_reset) (sc_low_t);
479         int (*scsi_low_targ_init) (sc_low_t, struct targ_info *, int);
480         int (*scsi_low_lun_init) (sc_low_t, struct targ_info *, struct lun_info *, int);
481         int (*scsi_low_start_bus) (sc_low_t, struct slccb *);
482         int (*scsi_low_establish_lun_nexus) (sc_low_t);
483         int (*scsi_low_establish_ccb_nexus) (sc_low_t);
484         void (*scsi_low_attention) (sc_low_t);
485         int (*scsi_low_msg) (sc_low_t, struct targ_info *, u_int);
486         int (*scsi_low_timeout) (sc_low_t);
487         int (*scsi_low_poll) (void *);
488         int (*scsi_low_power) (sc_low_t, u_int);
489         int (*scsi_low_ioctl) (sc_low_t, u_long, caddr_t, int, struct proc *);
490 };
491
492 struct scsi_low_softc {
493         /* os depend structure */
494         struct scsi_low_osdep_interface sl_si;
495 #define sl_dev  sl_si.si_dev
496         struct scsi_low_osdep_funcs *sl_osdep_fp;
497         u_char sl_xname[16];
498                                 
499         /* our chain */
500         LIST_ENTRY(scsi_low_softc) sl_chain;
501
502         /* my targets */
503         struct targ_info *sl_ti[SCSI_LOW_NTARGETS];
504         struct targ_info_tab sl_titab;
505
506         /* current active T_L_Q nexus */
507         struct targ_info *sl_Tnexus;            /* Target nexus */
508         struct lun_info *sl_Lnexus;             /* Lun nexus */
509         struct slccb *sl_Qnexus;                        /* Qtag nexus */
510         int sl_nexus_call;
511
512         /* ccb start queue */
513         struct slccbtab sl_start;       
514
515         /* retry limit and phase change counter */
516         int sl_max_retry;
517         int sl_ph_count;
518         int sl_timeout_count;
519
520         /* selection & total num disconnect targets */
521         int sl_nio;
522         int sl_disc;
523         int sl_retry_sel;
524         struct slccb *sl_selid;
525
526         /* attention */
527         int sl_atten;                   /* ATN asserted */
528         int sl_clear_atten;             /* negate ATN required */
529
530         /* scsi phase suggested by scsi msg */
531         u_int sl_msgphase;      
532 #define MSGPH_NULL      0x00            /* no msg */
533 #define MSGPH_DISC      0x01            /* disconnect msg */
534 #define MSGPH_CMDC      0x02            /* cmd complete msg */
535 #define MSGPH_ABORT     0x03            /* abort seq */
536 #define MSGPH_TERM      0x04            /* current io terminate */
537 #define MSGPH_LCTERM    0x05            /* cmd link terminated */
538 #define MSGPH_RESET     0x06            /* reset target */
539
540         /* error */
541         u_int sl_error;                 /* error flags */
542 #define FATALIO         0x0001          /* generic io error & retry io */
543 #define ABORTIO         0x0002          /* generic io error & terminate io */
544 #define TIMEOUTIO       0x0004          /* watch dog timeout */
545 #define SELTIMEOUTIO    0x0008          /* selection timeout */
546 #define PDMAERR         0x0010          /* dma xfer error */
547 #define MSGERR          0x0020          /* msgsys error */
548 #define PARITYERR       0x0040          /* parity error */
549 #define BUSYERR         0x0080          /* target busy error */
550 #define STATERR         0x0100          /* status error */
551 #define UACAERR         0x0200          /* target CA state, no sense check */
552 #define SENSEIO         0x1000          /* cmd not excuted but sense data ok */
553 #define SENSEERR        0x2000          /* cmd not excuted and sense data bad */
554 #define UBFERR          0x4000          /* unexpected bus free */
555 #define PENDINGIO       0x8000          /* ccb start not yet */
556 #define SCSI_LOW_ERRORBITS "\020\017ubferr\016senseerr\015senseio\012uacaerr\011staterr\010busy\007parity\006msgerr\005pdmaerr\004seltimeout\003timeout\002abort\001fatal"
557
558         /* current scsi data pointer */
559         struct sc_p sl_scp;
560
561         /* power control */
562         u_int sl_active;                /* host is busy state */
563         int sl_powc;                    /* power down timer counter */
564         u_int sl_rstep;                 /* resume step */
565
566         /* configuration flags */
567         u_int sl_flags;         
568 #define HW_POWDOWN      0x0001
569 #define HW_RESUME       0x0002
570 #define HW_PDMASTART    0x0004
571 #define HW_INACTIVE     0x0008
572 #define HW_POWERCTRL    0x0010
573 #define HW_INITIALIZING 0x0020
574 #define HW_READ_PADDING         0x1000
575 #define HW_WRITE_PADDING        0x2000
576
577         u_int sl_cfgflags;
578 #define CFG_NODISC              0x0001
579 #define CFG_NOPARITY            0x0002
580 #define CFG_NOATTEN             0x0004
581 #define CFG_ASYNC               0x0008
582 #define CFG_NOQTAG              0x0010
583
584         int sl_show_result;
585 #define SHOW_SYNCH_NEG  0x0001
586 #define SHOW_WIDE_NEG   0x0002
587 #define SHOW_CALCF_RES  0x0010
588 #define SHOW_PROBE_RES  0x0020
589 #define SHOW_ALL_NEG    -1
590
591         /* host informations */
592         u_int sl_hostid;
593         int sl_nluns;
594         int sl_ntargs;
595         int sl_openings;
596
597         /* interface functions */
598         struct scsi_low_funcs *sl_funcs;
599
600         /* targinfo size */
601         int sl_targsize;
602
603 #if     defined(i386) || defined(__i386__)
604         u_int sl_irq;           /* XXX */
605 #endif  /* i386 */
606 };
607
608 /*************************************************
609  * SCSI LOW service functions
610  *************************************************/
611 /* 
612  * Scsi low attachment function.
613  */
614 int scsi_low_attach (struct scsi_low_softc *, int, int, int, int, int);
615 int scsi_low_dettach (struct scsi_low_softc *);
616
617 /* 
618  * Scsi low interface activate or deactivate functions
619  */
620 int scsi_low_is_busy (struct scsi_low_softc *);
621 int scsi_low_activate (struct scsi_low_softc *);
622 int scsi_low_deactivate (struct scsi_low_softc *);
623
624 /* 
625  * Scsi phase "bus service" functions.
626  * These functions are corresponding to each scsi bus phaeses.
627  */
628 /* bus idle phase (other initiators or targets release bus) */
629 void scsi_low_bus_idle (struct scsi_low_softc *);
630
631 /* arbitration and selection phase */
632 void scsi_low_arbit_fail (struct scsi_low_softc *, struct slccb *);
633 static __inline void scsi_low_arbit_win (struct scsi_low_softc *);
634
635 /* msgout phase */
636 #define SCSI_LOW_MSGOUT_INIT            0x00000001
637 #define SCSI_LOW_MSGOUT_UNIFY           0x00000002
638 int scsi_low_msgout (struct scsi_low_softc *, struct targ_info *, u_int);
639
640 /* msgin phase */
641 #define SCSI_LOW_DATA_PE        0x80000000
642 int scsi_low_msgin (struct scsi_low_softc *, struct targ_info *, u_int);
643
644 /* statusin phase */
645 static __inline int scsi_low_statusin (struct scsi_low_softc *, struct targ_info *, u_int);
646
647 /* data phase */
648 int scsi_low_data (struct scsi_low_softc *, struct targ_info *, struct buf **, int);
649 static __inline void scsi_low_data_finish (struct scsi_low_softc *);
650
651 /* cmd phase */
652 int scsi_low_cmd (struct scsi_low_softc *, struct targ_info *);
653
654 /* reselection phase */
655 struct targ_info *scsi_low_reselected (struct scsi_low_softc *, u_int);
656
657 /* disconnection phase */
658 int scsi_low_disconnected (struct scsi_low_softc *, struct targ_info *);
659
660 /* 
661  * Scsi bus restart function.
662  * Canncel all established nexuses => scsi system initialized => restart jobs.
663  */
664 #define SCSI_LOW_RESTART_HARD   1
665 #define SCSI_LOW_RESTART_SOFT   0
666 int scsi_low_restart (struct scsi_low_softc *, int, u_char *);
667
668 /* 
669  * Scsi utility fucntions
670  */
671 /* print current status */
672 void scsi_low_print (struct scsi_low_softc *, struct targ_info *);
673
674 /* bus reset utility */
675 void scsi_low_bus_reset (struct scsi_low_softc *);
676
677 /*************************************************
678  * Message macro defs
679  *************************************************/
680 #define SCSI_LOW_SETUP_PHASE(ti, phase)                 \
681 {                                                       \
682         (ti)->ti_ophase = ti->ti_phase;                 \
683         (ti)->ti_phase = (phase);                       \
684 }
685
686 #define SCSI_LOW_SETUP_MSGPHASE(slp, PHASE)             \
687 {                                                       \
688         (slp)->sl_msgphase = (PHASE);                   \
689 }
690
691 #define SCSI_LOW_ASSERT_ATN(slp)                        \
692 {                                                       \
693         (slp)->sl_atten = 1;                            \
694 }
695
696 #define SCSI_LOW_DEASSERT_ATN(slp)                      \
697 {                                                       \
698         (slp)->sl_atten = 0;                            \
699 }
700
701 /*************************************************
702  * Inline functions
703  *************************************************/
704 static __inline void scsi_low_attention (struct scsi_low_softc *);
705 static __inline int scsi_low_is_msgout_continue (struct targ_info *, u_int);
706 static __inline int scsi_low_assert_msg (struct scsi_low_softc *, struct targ_info *, u_int, int);
707 static __inline int scsi_low_is_disconnect_ok (struct slccb *);
708
709 static __inline int
710 scsi_low_is_msgout_continue(ti, mask)
711         struct targ_info *ti;
712         u_int mask;
713 {
714         
715         return ((ti->ti_msgflags & (~mask)) != 0);
716 }
717
718 static __inline int
719 scsi_low_is_disconnect_ok(cb)
720         struct slccb *cb;
721 {
722
723         return ((cb->li->li_flags & SCSI_LOW_DISC) != 0 &&
724                     (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) == 0);
725 }
726
727 static __inline void
728 scsi_low_attention(slp)
729         struct scsi_low_softc *slp;
730 {
731
732         if (slp->sl_atten != 0)
733                 return;
734
735         (*slp->sl_funcs->scsi_low_attention) (slp);
736         SCSI_LOW_ASSERT_ATN(slp);
737 }
738
739 static __inline int
740 scsi_low_assert_msg(slp, ti, msg, now)
741         struct scsi_low_softc *slp;
742         struct targ_info *ti;
743         u_int msg;
744         int now;
745 {
746
747         ti->ti_msgflags |= msg;
748         if (now != 0)
749                 scsi_low_attention(slp);
750         return 0;
751 }
752
753 static __inline void
754 scsi_low_arbit_win(slp)
755         struct scsi_low_softc *slp;
756 {
757
758         slp->sl_selid = NULL;
759 }
760
761 static __inline void
762 scsi_low_data_finish(slp)
763         struct scsi_low_softc *slp;
764 {
765
766         if (slp->sl_Qnexus != NULL)
767         {
768                 slp->sl_Qnexus->ccb_datalen = slp->sl_scp.scp_datalen;
769         }
770 }
771
772 static __inline int
773 scsi_low_statusin(slp, ti, c)
774         struct scsi_low_softc *slp;
775         struct targ_info *ti;
776         u_int c;
777 {
778
779         slp->sl_ph_count ++;
780         if ((c & SCSI_LOW_DATA_PE) != 0)
781         {
782                 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 0);
783                 return EIO;
784         }
785         slp->sl_scp.scp_status = (u_int8_t) c;
786         return 0;
787 }
788
789 /*************************************************
790  * Message out defs
791  *************************************************/
792 /* XXX: use scsi_message.h */
793 #define ST_GOOD         0x00
794 #define ST_CHKCOND      0x02
795 #define ST_MET          0x04
796 #define ST_BUSY         0x08
797 #define ST_INTERGOOD    0x10
798 #define ST_INTERMET     0x14
799 #define ST_CONFLICT     0x18
800 #define ST_CMDTERM      0x22
801 #define ST_QUEFULL      0x28
802 #define ST_UNKNOWN      0xff
803
804 #define MSG_COMP        0x00
805 #define MSG_EXTEND      0x01
806
807 #define MKMSG_EXTEND(XLEN, XCODE) ((((u_int)(XLEN)) << NBBY) | ((u_int)(XCODE)))
808 #define MSG_EXTEND_MDPCODE      0x00
809 #define MSG_EXTEND_MDPLEN       0x05
810 #define MSG_EXTEND_SYNCHCODE    0x01
811 #define MSG_EXTEND_SYNCHLEN     0x03
812 #define MSG_EXTEND_WIDECODE     0x03
813 #define MSG_EXTEND_WIDELEN      0x02
814
815 #define MSG_SAVESP      0x02
816 #define MSG_RESTORESP   0x03
817 #define MSG_DISCON      0x04
818 #define MSG_I_ERROR     0x05
819 #define MSG_ABORT       0x06
820 #define MSG_REJECT      0x07
821 #define MSG_NOOP        0x08
822 #define MSG_PARITY      0x09
823 #define MSG_LCOMP       0x0a
824 #define MSG_LCOMP_F     0x0b
825 #define MSG_RESET       0x0c
826 #define MSG_ABORT_QTAG  0x0d
827 #define MSG_CLEAR_QTAG  0x0e
828 #define MSG_TERM_IO     0x11
829 #define MSG_SIMPLE_QTAG 0x20
830 #define MSG_HEAD_QTAG   0x21
831 #define MSG_ORDERED_QTAG        0x22
832 #define MSG_IDENTIFY            0x80
833 #define MSG_IDENTIFY_DISCPRIV   0x40
834 #endif  /* !_SCSI_LOW_H_ */