Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / i386 / bs / bsvar.h
1 /*      $NecBSD: bsvar.h,v 1.2 1997/10/31 17:43:41 honda Exp $  */
2 /*      $NetBSD$        */
3 /*
4  * [NetBSD for NEC PC98 series]
5  *  Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
6  *  All rights reserved.
7  * 
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *  3. The name of the author may not be used to endorse or promote products
17  *     derived from this software without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/i386/isa/bs/bsvar.h,v 1.6.6.1 2000/03/22 03:36:45 nyan Exp $
32  */
33 /*
34  * Copyright (c) 1994, 1995, 1996 Naofumi HONDA.  All rights reserved.
35  */
36
37 #ifdef __FreeBSD__
38 #define BS_INLINE       __inline
39 #else
40 #define BS_INLINE       inline
41 #endif
42
43 /**************************************************
44  *      CONTROL FLAGS  (cf_flags)
45  *************************************************/
46 #define BSC_FASTACK             0x01
47 #define BSC_SMITSAT_DISEN       0x02
48 #define BSC_CHIP_CLOCK(dvcfg)   (((dvcfg) >> 4) & 0x03)
49
50 #define BS_SCSI_SYNC            DVF_SCSI_SYNC
51 #define BS_SCSI_DISC            DVF_SCSI_DISC
52 #define BS_SCSI_WAIT            DVF_SCSI_WAIT
53 #define BS_SCSI_LINK            DVF_SCSI_LINK
54 #define BS_SCSI_QTAG            DVF_SCSI_QTAG
55 #define BS_SCSI_NOSAT           DVF_SCSI_SP0
56 #define BS_SCSI_NOPARITY        DVF_SCSI_NOPARITY
57 #define BS_SCSI_SAVESP          DVF_SCSI_SAVESP
58 #define BS_SCSI_NOSMIT          DVF_SCSI_SP1
59 #define BS_SCSI_PERIOD(XXX)     DVF_SCSI_PERIOD(XXX)
60 #define BS_SCSI_OFFSET(XXX)     DVF_SCSI_OFFSET(XXX)
61 #define BS_SCSI_SYNCMASK        DVF_SCSI_SYNCMASK
62 #define BS_SCSI_BITS            DVF_SCSI_BITS
63
64 #define BS_SCSI_DEFCFG          (BS_SCSI_NOSAT | DVF_SCSI_DEFCFG)
65
66 #define BS_SCSI_POSITIVE (BS_SCSI_SYNC | BS_SCSI_DISC | BS_SCSI_LINK)
67 #define BS_SCSI_NEGATIVE (BS_SCSI_WAIT | BS_SCSI_NOSAT | BS_SCSI_NOPARITY |\
68                           BS_SCSI_SAVESP | BS_SCSI_NOSMIT)
69 /*******************************************
70  * CONFIG SECTION
71  ******************************************/
72 /* Enable timeout watch dog */
73 #define BS_DEFAULT_TIMEOUT_SECOND       16      /* default 16 sec */
74 #define BS_SYNC_TIMEOUT                 16
75 #define BS_STARTUP_TIMEOUT              60
76 #define BS_MOTOR_TIMEOUT                120
77 #define BS_TIMEOUT_CHECK_INTERVAL       4       /* check each 4 sec */
78
79 /* If you use memory over 16M */
80 #ifdef  SCSI_BOUNCE_SIZE
81 #define BS_BOUNCE_SIZE  SCSI_BOUNCE_SIZE
82 #else   /* !SCSI_BOUNCE_SIZE */
83 #define BS_BOUNCE_SIZE  0
84 #endif  /* !SCSI_BOUNCE_SIZE */
85
86 /* debug */
87 #define BS_STATICS              1
88 #define BS_DIAG                 1
89 #define BS_DEBUG_ROUTINE        1
90 #define BS_DEBUG                1
91 /* #define      SHOW_PORT       1 */
92
93 /**************************************************
94  *      PARAMETER
95  **************************************************/
96 #define NTARGETS        8
97 #define RETRIES         1       /* number of retries before giving up */
98 #define HARDRETRIES     3
99 #define XSMAX           4
100 #define BSDMABUFSIZ     0x10000
101 #define BS_MAX_CCB      (XSMAX * (NTARGETS - 1))
102
103 #define BSCMDSTART      0
104 #define BSCMDRESTART    0x01
105 #define BSCMDFORCE      0x02
106
107 #define BS_TIMEOUT_INTERVAL     (hz * BS_TIMEOUT_CHECK_INTERVAL)
108
109 /**************************************************
110  *      SCSI PHASE
111  **************************************************/
112 enum scsi_phase {
113         FREE = 0,
114         HOSTQUEUE,
115         DISCONNECTED,
116         IOCOMPLETED,
117         ATTENTIONASSERT,
118         DISCONNECTASSERT,
119         SELECTASSERT,
120         SELECTED,
121         RESELECTED,
122         MSGIN,
123         MSGOUT,
124         STATUSIN,
125         CMDPHASE,
126         DATAPHASE,
127         SATSEL,
128         SATRESEL,
129         SATSDP,
130         SATCOMPSEQ,
131         UNDEF,
132 };
133
134 /**************************************************
135  *      SCSI PHASE CONTROL MACRO
136  **************************************************/
137 #define BS_HOST_START                                   \
138 {                                                       \
139         bsc->sc_nexus = ti;                             \
140 }
141
142 #define BS_HOST_TERMINATE                               \
143 {                                                       \
144         bsc->sc_selwait = NULL;                         \
145         bsc->sc_nexus = NULL;                           \
146 }
147
148 #define BS_SELECTION_START                              \
149 {                                                       \
150         bsc->sc_selwait = ti;                           \
151 }
152
153 #define BS_SELECTION_TERMINATE                          \
154 {                                                       \
155         bsc->sc_selwait = NULL;                         \
156 }
157
158 #define BS_SETUP_PHASE(PHASE)                           \
159 {                                                       \
160         ti->ti_ophase = ti->ti_phase;                   \
161         ti->ti_phase = (PHASE);                         \
162 }
163
164 #define BS_SETUP_MSGPHASE(PHASE)                        \
165 {                                                       \
166         bsc->sc_msgphase = (PHASE);                     \
167 }
168
169 #define BS_SETUP_SYNCSTATE(STATE)                       \
170 {                                                       \
171         ti->ti_syncnow.state = (STATE);                 \
172 }
173
174 #define BS_SETUP_TARGSTATE(STATE)                       \
175 {                                                       \
176         ti->ti_state = (STATE);                         \
177 }
178
179 #define BS_LOAD_SDP                                     \
180 {                                                       \
181         bsc->sc_p.data = ti->ti_scsp.data;              \
182         bsc->sc_p.datalen = ti->ti_scsp.datalen;        \
183         bsc->sc_p.seglen = ti->ti_scsp.seglen;          \
184 }
185
186 #define BS_RESTORE_SDP                                  \
187 {                                                       \
188         bsc->sc_p = ti->ti_scsp;                        \
189 }
190
191 #define BS_SAVE_SDP                                     \
192 {                                                       \
193         ti->ti_scsp = bsc->sc_p;                        \
194 }
195
196 /**************************************************
197  * STRUCTURES
198  **************************************************/
199 struct msgbase {
200 #define MAXMSGLEN       8
201         u_int8_t msg[MAXMSGLEN];
202         u_int msglen;
203
204         u_int flag;
205 };
206
207 struct syncdata {
208         u_int8_t period;
209         u_int8_t offset;
210
211 #define BS_SYNCMSG_NULL         0x00
212 #define BS_SYNCMSG_ASSERT       0x01
213 #define BS_SYNCMSG_REJECT       0x02
214 #define BS_SYNCMSG_ACCEPT       0x03
215 #define BS_SYNCMSG_REQUESTED    0x04
216         u_int state;
217 };
218
219 struct sc_p {
220         u_int8_t *data;
221         int datalen;
222
223         u_int8_t *segaddr;
224         int seglen;
225
226         u_int8_t *bufp;
227 };
228
229 /* targ_info error flags */
230 #define BSDMAABNORMAL   0x01
231 #define BSCMDABNORMAL   0x02
232 #define BSPARITY        0x04
233 #define BSSTATUSERROR   0x08
234 #define BSTIMEOUT       0x10
235 #define BSREQSENSE      0x20
236 #define BSSELTIMEOUT    0x40
237 #define BSFATALIO       0x80
238 #define BSMSGERROR      0x100
239 #define BSTRYRECOV      0x200
240 #define BSABNORMAL      0x400
241 #define BSTARGETBUSY    0x800
242
243 #define BSERRORBITS     "\020\014busy\013abnormal\012retry\011msgerr\010fatal\007seltimeout\006sense\005timeout\004statuserr\003parity\002cmderr\001dmaerr"
244
245 /* bsccb bsccb_flags & targ_info flags & cmd flags*/
246 #define BSREAD          0x0001
247 #define BSSAT           0x0002
248 #define BSLINK          0x0004
249 #define BSERROROK       0x0008
250 #define BSSMIT          0x0010
251 #define BSDISC          0x1000
252 #define BSFORCEIOPOLL   0x2000
253
254 #define BSCASTAT        0x01000000
255 #define BSSENSECCB      0x02000000
256 #define BSQUEUED        0x04000000
257 #define BSALTBUF        0x08000000
258 #define BSITSDONE       0x10000000
259 #define BSNEXUS         0x20000000
260
261 #define BSCFLAGSMASK    (0xffff)
262
263 struct bsccb {
264         TAILQ_ENTRY(bsccb) ccb_chain;
265
266         union ccb *ccb;                 /* upper drivers info */
267
268         u_int lun;                      /* lun */
269
270         u_int bsccb_flags;              /* control flags */
271
272         int rcnt;                       /* retry counter of this ccb */
273
274         u_int error;                    /* recorded error */
275
276         /*****************************************
277          * scsi cmd & data
278          *****************************************/
279         u_int8_t *cmd;                  /* scsi cmd */
280         int cmdlen;
281
282         u_int8_t *data;                 /* scsi data */
283         int datalen;
284
285         u_int8_t msgout[MAXMSGLEN];     /* scsi msgout */
286         u_int msgoutlen;
287
288         /*****************************************
289          * timeout counter
290          *****************************************/
291         int tc;
292         int tcmax;
293 };
294
295 GENERIC_CCB_ASSERT(bs, bsccb)
296
297 /* target info */
298 struct targ_info {
299 /*0*/   TAILQ_ENTRY(targ_info) ti_tchain;       /* targ_info link */
300
301 /*4*/   TAILQ_ENTRY(targ_info) ti_wchain;       /* wait link */
302
303 /*8*/   struct bs_softc *ti_bsc;                /* our controller */
304 /*c*/   u_int ti_id;                            /* scsi id */
305 /*10*/  u_int ti_lun;                           /* current lun */
306
307 /*14*/  struct bsccbtab ti_ctab, ti_bctab;      /* ccb */
308
309 #define BS_TARG_NULL    0
310 #define BS_TARG_CTRL    1
311 #define BS_TARG_START   2
312 #define BS_TARG_SYNCH   3
313 #define BS_TARG_RDY     4
314 /*24*/  int ti_state;                           /* target state */
315
316 /*28*/  u_int ti_cfgflags;                      /* target cfg flags */
317
318 /*2c*/  u_int ti_flags;                         /* flags */
319 /*30*/  u_int ti_mflags;                        /* flags masks */
320
321 /*34*/  u_int ti_error;                         /* error flags */
322 /*38*/  u_int ti_herrcnt;                       /* hardware err retry counter */
323
324         /*****************************************
325          * scsi phase data
326          *****************************************/
327 /*3c*/  struct sc_p ti_scsp;                    /* saved scsi data pointer */
328
329 /*50*/  enum scsi_phase ti_phase;               /* scsi phase */
330 /*54*/  enum scsi_phase ti_ophase;              /* previous scsi phase */
331
332 /*58*/  u_int8_t ti_status;                     /* status in */
333
334 /*59*/  u_int8_t ti_msgin[MAXMSGLEN];           /* msgin buffer */
335 /*64*/  int ti_msginptr;
336
337 /*68*/  u_int8_t ti_msgout;                     /* last msgout byte */
338 /*69*/  u_int8_t ti_emsgout;                    /* last msgout byte */
339 /*6c*/  u_int ti_omsgoutlen;                    /* for retry msgout */
340
341 /*70*/  struct syncdata ti_syncmax;             /* synch data (scsi) */
342 /*72*/  struct syncdata ti_syncnow;
343 /*74*/  u_int8_t ti_sync;                       /* synch val (chip) */
344
345         /*****************************************
346          * bounce buffer & smit data pointer
347          *****************************************/
348 /*75*/  u_int8_t *bounce_phys;
349 /*76*/  u_int8_t *bounce_addr;
350 /*78*/  u_int bounce_size;
351
352 /*7c*/  u_long sm_offset;
353
354         /*****************************************
355          * target inq data
356          *****************************************/
357 /*79*/  u_int8_t targ_type;
358 /*7a*/  u_int8_t targ_support;
359
360         /*****************************************
361          * generic scsi cmd buffer for this target
362          *****************************************/
363 /*7b*/  u_int8_t scsi_cmd[12];
364         struct scsi_sense_data sense;
365 };
366
367 TAILQ_HEAD(titab, targ_info);
368 struct bshw;
369
370 struct bs_softc {
371         /*****************************************
372          * OS depend header
373          *****************************************/
374         OS_DEPEND_DEVICE_HEADER
375
376         OS_DEPEND_MISC_HEADER
377
378         /*****************************************
379          * target link
380          *****************************************/
381         struct targ_info *sc_ti[NTARGETS];
382         u_int sc_openf;
383
384         struct titab sc_sttab;
385         struct titab sc_titab;
386
387         /*****************************************
388          * current scsi phase
389          *****************************************/
390         struct targ_info *sc_nexus;             /* current active nexus */
391
392         enum scsi_phase sc_msgphase;            /* scsi phase pointed by msg */
393
394         struct targ_info *sc_selwait;           /* selection assert */
395
396         u_int sc_dtgnum;                        /* disconnected target */
397
398         /*****************************************
399          * current scsi data pointer
400          *****************************************/
401         struct sc_p sc_p;                       /* scsi data pointer */
402
403         int sc_dmadir;                          /* dma direction */
404
405         int sm_tdatalen;                        /* smit data transfer bytes */
406
407         /*****************************************
408          * parameter
409          *****************************************/
410         u_int sc_retry;                         /* max retry count */
411
412 #define BSDMATRANSFER   0x01
413 #define BSDMASTART      0x02
414 #define BSSMITSTART     0x04
415 #define BSUNDERRESET    0x08
416 #define BSRESET         0x10
417 #define BSSTARTTIMEOUT  0x20
418 #define BSPOLLDONE      0x100
419 #define BSJOBDONE       0x200
420 #define BSBSMODE        0x400
421 #define BSINACTIVE      0x800
422         volatile int sc_flags;                  /* host flags */
423
424 #define BSC_NULL        0
425 #define BSC_BOOTUP      1
426 #define BSC_TARG_CHECK  2
427 #define BSC_RDY         3
428         int sc_hstate;                          /* host state */
429
430         /*****************************************
431          * polling misc
432          *****************************************/
433         u_int sc_wc;                            /* weight count */
434
435         int sc_poll;
436         struct bsccb *sc_outccb;
437
438         /*****************************************
439          * wd33c93 chip depend section
440          *****************************************/
441         u_int sc_cfgflags;                      /* hardware cfg flags */
442
443         struct bshw *sc_hw;                     /* hw selection */
444
445         u_long sm_offset;                       /* smit buffer offset */
446
447         u_int sc_RSTdelay;
448
449         int sc_hwlock;                          /* hardware lock count */
450
451         int sc_iobase;                          /* iobase for FreeBSD */
452         u_int32_t sc_irq;                       /* irq */
453
454         u_int sc_dmachan;                       /* dma channel */
455         u_int8_t sc_busstat;                    /* scsi bus status register */
456         u_int8_t sc_hostid;                     /* host scsi id */
457         u_int8_t sc_cspeed;                     /* chip clock rate */
458         u_int8_t sc_membank;                    /* memory back (NEC) register */
459
460         /*****************************************
461          * our name
462          *****************************************/
463 #define BS_DVNAME_LEN   16
464         u_char sc_dvname[BS_DVNAME_LEN];
465
466         /*****************************************
467          * CAM support
468          *****************************************/
469         struct          cam_sim  *sim;
470         struct          cam_path *path;
471 };
472
473 /*************************************************
474  * debug
475  *************************************************/
476 #ifdef  BS_STATICS
477 struct bs_statics {
478         u_int select;
479         u_int select_miss_in_assert;
480         u_int select_miss_by_reselect;
481         u_int select_miss;
482         u_int select_win;
483         u_int selected;
484         u_int disconnected;
485         u_int reselect;
486 };
487
488 extern struct bs_statics bs_statics[NTARGETS];
489 extern u_int bs_linkcmd_count[];
490 extern u_int bs_bounce_used[];
491 #endif  /* BS_STATICS */
492
493 #ifdef  BS_DEBUG_ROUTINE
494 #ifndef DDB
495 #define Debugger()      panic("should call debugger here (bs.c)")
496 #endif  /* DDB */
497 #ifdef  BS_DEBUG
498 extern int bs_debug_flag;
499 #endif /* BS_DEBUG */
500 #endif /* BS_DEBUG_ROUTINE */
501
502 /*************************************************
503  * Function declare
504  *************************************************/
505 int bs_scsi_cmd_internal __P((struct bsccb *, u_int));
506 struct bsccb *bscmddone __P((struct targ_info *));
507 int bscmdstart __P((struct targ_info *, int));
508 int bs_scsi_cmd_poll __P((struct targ_info *, struct bsccb *));
509 int bs_sequencer __P((struct bs_softc *));
510 void bs_poll_timeout __P((struct bs_softc *, char *));
511
512 /*************************************************
513  * XXX
514  *************************************************/
515 /* misc error */
516 #define COMPLETE        2
517 #define NOTARGET        (-2)
518 #define HASERROR        (-1)
519
520 /* XXX: use scsi_message.h */
521 /* status */
522 #define ST_GOOD         0x00
523 #define ST_CHKCOND      0x02
524 #define ST_MET          0x04
525 #define ST_BUSY         0x08
526 #define ST_INTERGOOD    0x10
527 #define ST_INTERMET     0x14
528 #define ST_CONFLICT     0x18
529 #define ST_QUEFULL      0x28
530 #define ST_UNK          0xff
531
532 /* message */
533 #define MSG_COMP        0x00
534 #define MSG_EXTEND      0x01
535
536 #define MKMSG_EXTEND(XLEN, XCODE) ((((u_int)(XLEN)) << NBBY) | ((u_int)(XCODE)))
537 #define MSG_EXTEND_MDPCODE      0x00
538 #define MSG_EXTEND_MDPLEN       0x05
539 #define MSG_EXTEND_SYNCHCODE    0x01
540 #define MSG_EXTEND_SYNCHLEN     0x03
541 #define MSG_EXTEND_WIDECODE     0x03
542 #define MSG_EXTEND_WIDELEN      0x02
543
544 #define MSG_SAVESP      0x02
545 #define MSG_RESTORESP   0x03
546 #define MSG_DISCON      0x04
547 #define MSG_I_ERROR     0x05
548 #define MSG_ABORT       0x06
549 #define MSG_REJECT      0x07
550 #define MSG_NOOP        0x08
551 #define MSG_PARITY      0x09
552 #define MSG_LCOMP       0x0a
553 #define MSG_LCOMP_F     0x0b
554 #define MSG_RESET       0x0c