Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / i386 / bs / bsfunc.h
1 /*      $NecBSD: bsfunc.h,v 1.1 1997/07/18 09:19:03 kmatsuda 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 /*
32  * Copyright (c) 1994, 1995, 1996 Naofumi HONDA.  All rights reserved.
33  */
34
35 /**************************************************
36  * FUNC
37  **************************************************/
38 /* timeout */
39 void bstimeout __P((void *));
40
41 /* ctrl setup */
42 void bs_setup_ctrl __P((struct targ_info *, u_int, u_int));
43 struct targ_info *bs_init_target_info __P((struct bs_softc *, int));
44
45 /* msg op */
46 int bs_send_msg __P((struct targ_info *, u_int, struct msgbase *, int));
47 struct bsccb *bs_request_sense __P((struct targ_info *));
48
49 /* sync msg op */
50 int bs_start_syncmsg __P((struct targ_info *, struct bsccb *, int));
51 int bs_send_syncmsg __P((struct targ_info *));
52 int bs_analyze_syncmsg __P((struct targ_info *, struct bsccb *));
53
54 /* reset device */
55 void bs_scsibus_start __P((struct bs_softc *));
56 void bs_reset_nexus __P((struct bs_softc *));
57 struct bsccb *bs_force_abort __P((struct targ_info *));
58 void bs_reset_device __P((struct targ_info *));
59
60 /* ccb */
61 struct bsccb *bs_make_internal_ccb __P((struct targ_info *, u_int, u_int8_t *, u_int, u_int8_t *, u_int, u_int, int));
62 struct bsccb *bs_make_msg_ccb __P((struct targ_info *, u_int, struct bsccb *, struct msgbase *, u_int));
63
64 /* misc funcs */
65 void bs_printf __P((struct targ_info *, char *, char *));
66 void bs_panic __P((struct bs_softc *, u_char *));
67
68 /* misc debug */
69 u_int bsr __P((u_int));
70 u_int bsw __P((u_int, int));
71 void bs_debug_print_all __P((struct bs_softc *));
72 void bs_debug_print __P((struct bs_softc *, struct targ_info *));
73
74 /**************************************************
75  * TARG FLAGS
76  *************************************************/
77 static BS_INLINE int bs_check_sat __P((struct targ_info *));
78 static BS_INLINE int bs_check_smit __P((struct targ_info *));
79 static BS_INLINE int bs_check_disc __P((struct targ_info *));
80 static BS_INLINE int bs_check_link __P((struct targ_info *, struct bsccb *));
81 static BS_INLINE u_int8_t bs_identify_msg __P((struct targ_info *));
82 static BS_INLINE void bs_targ_flags __P((struct targ_info *, struct bsccb *));
83
84 static BS_INLINE int
85 bs_check_disc(ti)
86         struct targ_info *ti;
87 {
88
89         return (ti->ti_flags & BSDISC);
90 }
91
92 static BS_INLINE int
93 bs_check_sat(ti)
94         struct targ_info *ti;
95 {
96
97         return (ti->ti_flags & BSSAT);
98 }
99
100 static BS_INLINE int
101 bs_check_smit(ti)
102         struct targ_info *ti;
103 {
104
105         return (ti->ti_flags & BSSMIT);
106 }
107
108 static BS_INLINE int
109 bs_check_link(ti, cb)
110         struct targ_info *ti;
111         struct bsccb *cb;
112 {
113         struct bsccb *nextcb;
114
115         return ((ti->ti_flags & BSLINK) &&
116                 (nextcb = cb->ccb_chain.tqe_next) &&
117                 (nextcb->bsccb_flags & BSLINK));
118 }
119
120 static BS_INLINE u_int8_t
121 bs_identify_msg(ti)
122         struct targ_info *ti;
123 {
124
125         return ((bs_check_disc(ti) ? 0xc0 : 0x80) | ti->ti_lun);
126 }
127
128 static BS_INLINE void
129 bs_targ_flags(ti, cb)
130         struct targ_info *ti;
131         struct bsccb *cb;
132 {
133         u_int cmf = (u_int) bshw_cmd[cb->cmd[0]];
134
135         cb->bsccb_flags |= ((cmf & (BSSAT | BSSMIT | BSLINK)) | BSDISC);
136         cb->bsccb_flags &= ti->ti_mflags;
137
138         if (cb->datalen < DEV_BSIZE)
139                 cb->bsccb_flags &= ~BSSMIT;
140         if (cb->bsccb_flags & BSFORCEIOPOLL)
141                 cb->bsccb_flags &= ~(BSLINK | BSSMIT | BSSAT | BSDISC);
142 }
143
144 /**************************************************
145  * QUEUE OP
146  **************************************************/
147 static BS_INLINE void bs_hostque_init __P((struct bs_softc *));
148 static BS_INLINE void bs_hostque_head __P((struct bs_softc *, struct targ_info *));
149 static BS_INLINE void bs_hostque_tail __P((struct bs_softc *, struct targ_info *));
150 static BS_INLINE void bs_hostque_delete __P((struct bs_softc *, struct targ_info *));
151
152 static BS_INLINE void
153 bs_hostque_init(bsc)
154         struct bs_softc *bsc;
155 {
156
157         TAILQ_INIT(&bsc->sc_sttab);
158         TAILQ_INIT(&bsc->sc_titab);
159 }
160
161 static BS_INLINE void
162 bs_hostque_head(bsc, ti)
163         struct bs_softc *bsc;
164         struct targ_info *ti;
165 {
166
167         if (ti->ti_flags & BSQUEUED) {
168                 TAILQ_REMOVE(&bsc->sc_sttab, ti, ti_wchain);
169         } else {
170                 ti->ti_flags |= BSQUEUED;
171         }
172         TAILQ_INSERT_HEAD(&bsc->sc_sttab, ti, ti_wchain);
173 }
174
175 static BS_INLINE void
176 bs_hostque_tail(bsc, ti)
177         struct bs_softc *bsc;
178         struct targ_info *ti;
179 {
180
181         if (ti->ti_flags & BSQUEUED) {
182                 TAILQ_REMOVE(&bsc->sc_sttab, ti, ti_wchain);
183         } else {
184                 ti->ti_flags |= BSQUEUED;
185         }
186         TAILQ_INSERT_TAIL(&bsc->sc_sttab, ti, ti_wchain);
187 }
188
189 static BS_INLINE void
190 bs_hostque_delete(bsc, ti)
191         struct bs_softc *bsc;
192         struct targ_info *ti;
193 {
194
195         if (ti->ti_flags & BSQUEUED)
196         {
197                 ti->ti_flags &= ~BSQUEUED;
198                 TAILQ_REMOVE(&bsc->sc_sttab, ti, ti_wchain);
199         }
200 }
201
202 /*************************************************************
203  * TIMEOUT
204  ************************************************************/
205 static BS_INLINE void bs_start_timeout __P((struct bs_softc *));
206 static BS_INLINE void bs_terminate_timeout __P((struct bs_softc *));
207
208 static BS_INLINE void
209 bs_start_timeout(bsc)
210         struct bs_softc *bsc;
211 {
212
213         if ((bsc->sc_flags & BSSTARTTIMEOUT) == 0)
214         {
215                 bsc->sc_flags |= BSSTARTTIMEOUT;
216 #ifdef __FreeBSD__
217                 bsc->timeout_ch =
218 #endif
219                 timeout(bstimeout, bsc, BS_TIMEOUT_INTERVAL);
220         }
221 }
222
223 static BS_INLINE void
224 bs_terminate_timeout(bsc)
225         struct bs_softc *bsc;
226 {
227
228         if (bsc->sc_flags & BSSTARTTIMEOUT)
229         {
230 #ifdef __FreeBSD__
231                 untimeout(bstimeout, bsc,
232                                   bsc->timeout_ch);
233 #else
234                 untimeout(bstimeout, bsc);
235 #endif
236                 bsc->sc_flags &= ~BSSTARTTIMEOUT;
237         }
238 }