Merge from vendor branch GDB:
[dragonfly.git] / sys / bus / firewire / firewirereg.h
1 /*
2  * Copyright (c) 2003 Hidetoshi Shimokawa
3  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the acknowledgement as bellow:
16  *
17  *    This product includes software developed by K. Kobayashi and H. Shimokawa
18  *
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  * 
34  * $FreeBSD: src/sys/dev/firewire/firewirereg.h,v 1.33 2004/01/06 14:30:46 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/firewirereg.h,v 1.10 2006/06/10 20:00:12 dillon Exp $
36  *
37  */
38
39 #ifdef __DragonFly__
40 typedef d_thread_t fw_proc;
41 #elif __FreeBSD_version >= 500000
42 typedef struct thread fw_proc;
43 #else
44 typedef struct proc fw_proc;
45 #endif
46
47 #include <sys/selinfo.h>
48 #include <sys/uio.h>
49
50 struct fw_device{
51         u_int16_t dst;
52         struct fw_eui64 eui;
53         u_int8_t speed;
54         u_int8_t maxrec;
55         u_int8_t nport;
56         u_int8_t power;
57 #define CSRROMOFF 0x400
58 #define CSRROMSIZE 0x400
59         int rommax;     /* offset from 0xffff f000 0000 */
60         u_int32_t csrrom[CSRROMSIZE/4];
61         int rcnt;
62         struct firewire_comm *fc;
63         u_int32_t status;
64 #define FWDEVINIT       1
65 #define FWDEVATTACHED   2
66 #define FWDEVINVAL      3
67         STAILQ_ENTRY(fw_device) link;
68 };
69
70 struct firewire_softc {
71 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
72         dev_t dev;
73 #endif
74         struct firewire_comm *fc;
75 };
76
77 #define FW_MAX_DMACH 0x20
78 #define FW_MAX_DEVCH FW_MAX_DMACH
79 #define FW_XFERTIMEOUT 1
80
81 struct firewire_dev_comm {
82         device_t dev;
83         struct firewire_comm *fc;
84         void (*post_busreset) (void *);
85         void (*post_explore) (void *);
86 };
87
88 struct tcode_info {
89         u_char hdr_len; /* IEEE1394 header length */
90         u_char flag;
91 #define FWTI_REQ        (1 << 0)
92 #define FWTI_RES        (1 << 1)
93 #define FWTI_TLABEL     (1 << 2)
94 #define FWTI_BLOCK_STR  (1 << 3)
95 #define FWTI_BLOCK_ASY  (1 << 4)
96 };
97
98 struct firewire_comm{
99         device_t dev;
100         device_t bdev;
101         u_int16_t busid:10,
102                   nodeid:6;
103         u_int mode;
104         u_int nport;
105         u_int speed;
106         u_int maxrec;
107         u_int irm;
108         u_int max_node;
109         u_int max_hop;
110         u_int max_asyretry;
111 #define FWPHYASYST (1 << 0)
112         u_int retry_count;
113         u_int32_t ongobus:10,
114                   ongonode:6,
115                   ongoaddr:16;
116         struct fw_device *ongodev;
117         struct fw_eui64 ongoeui;
118 #define FWMAXCSRDIR     16
119         SLIST_HEAD(, csrdir) ongocsr;
120         SLIST_HEAD(, csrdir) csrfree;
121         u_int32_t status;
122 #define FWBUSNOTREADY   (-1)
123 #define FWBUSRESET      0
124 #define FWBUSINIT       1
125 #define FWBUSCYMELECT   2
126 #define FWBUSMGRELECT   3
127 #define FWBUSMGRDONE    4
128 #define FWBUSEXPLORE    5
129 #define FWBUSPHYCONF    6
130 #define FWBUSEXPDONE    7
131 #define FWBUSCOMPLETION 10
132         int nisodma;
133         struct fw_eui64 eui;
134         struct fw_xferq
135                 *arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
136         STAILQ_HEAD(, tlabel) tlabels[0x40];
137         STAILQ_HEAD(, fw_bind) binds;
138         STAILQ_HEAD(, fw_device) devices;
139         u_int  sid_cnt;
140 #define CSRSIZE 0x4000
141         u_int32_t csr_arc[CSRSIZE/4];
142 #define CROMSIZE 0x400
143         u_int32_t *config_rom;
144         struct crom_src_buf *crom_src_buf;
145         struct crom_src *crom_src;
146         struct crom_chunk *crom_root;
147         struct fw_topology_map *topology_map;
148         struct fw_speed_map *speed_map;
149         struct callout busprobe_callout;
150         struct callout bmr_callout;
151         struct callout timeout_callout;
152         struct callout retry_probe_callout;
153         u_int32_t (*cyctimer) (struct  firewire_comm *);
154         void (*ibr) (struct firewire_comm *);
155         u_int32_t (*set_bmr) (struct firewire_comm *, u_int32_t);
156         int (*ioctl) (dev_t, u_long, caddr_t, int, fw_proc *);
157         int (*irx_enable) (struct firewire_comm *, int);
158         int (*irx_disable) (struct firewire_comm *, int);
159         int (*itx_enable) (struct firewire_comm *, int);
160         int (*itx_disable) (struct firewire_comm *, int);
161         void (*timeout) (void *);
162         void (*poll) (struct firewire_comm *, int, int);
163         void (*set_intr) (struct firewire_comm *, int);
164         void (*irx_post) (struct firewire_comm *, u_int32_t *);
165         void (*itx_post) (struct firewire_comm *, u_int32_t *);
166         struct tcode_info *tcode;
167         bus_dma_tag_t dmat;
168 };
169 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
170
171 struct csrdir{
172         u_int32_t ongoaddr;
173         u_int32_t off;
174         SLIST_ENTRY(csrdir) link;
175 };
176
177 struct fw_xferq {
178         int flag;
179 #define FWXFERQ_CHTAGMASK 0xff
180 #define FWXFERQ_RUNNING (1 << 8)
181 #define FWXFERQ_STREAM (1 << 9)
182
183 #define FWXFERQ_BULK (1 << 11)
184 #define FWXFERQ_MODEMASK (7 << 10)
185
186 #define FWXFERQ_EXTBUF (1 << 13)
187 #define FWXFERQ_OPEN (1 << 14)
188
189 #define FWXFERQ_HANDLER (1 << 16)
190 #define FWXFERQ_WAKEUP (1 << 17)
191         void (*start) (struct firewire_comm*);
192         int dmach;
193         STAILQ_HEAD(, fw_xfer) q;
194         u_int queued;
195         u_int maxq;
196         u_int psize;
197         STAILQ_HEAD(, fw_bind) binds;
198         struct fwdma_alloc_multi *buf;
199         u_int bnchunk;
200         u_int bnpacket;
201         struct fw_bulkxfer *bulkxfer;
202         STAILQ_HEAD(, fw_bulkxfer) stvalid;
203         STAILQ_HEAD(, fw_bulkxfer) stfree;
204         STAILQ_HEAD(, fw_bulkxfer) stdma;
205         struct fw_bulkxfer *stproc;
206         struct selinfo rsel;
207         caddr_t sc;
208         void (*hand) (struct fw_xferq *);
209 };
210
211 struct fw_bulkxfer{
212         int poffset;
213         struct mbuf *mbuf;
214         STAILQ_ENTRY(fw_bulkxfer) link;
215         caddr_t start;
216         caddr_t end;
217         int resp;
218 };
219
220 struct tlabel{
221         struct fw_xfer  *xfer;
222         STAILQ_ENTRY(tlabel) link;
223 };
224
225 struct fw_bind{
226         u_int64_t start;
227         u_int64_t end;
228         STAILQ_HEAD(, fw_xfer) xferlist;
229         STAILQ_ENTRY(fw_bind) fclist;
230         STAILQ_ENTRY(fw_bind) chlist;
231 #define FWACT_NULL      0
232 #define FWACT_XFER      2
233 #define FWACT_CH        3
234         u_int8_t act_type;
235         u_int8_t sub;
236 };
237
238 struct fw_xfer{
239         caddr_t sc;
240         struct firewire_comm *fc;
241         struct fw_xferq *q;
242         struct timeval tv;
243         int8_t resp;
244 #define FWXF_INIT 0
245 #define FWXF_INQ 1
246 #define FWXF_START 2
247 #define FWXF_SENT 3
248 #define FWXF_SENTERR 4
249 #define FWXF_BUSY 8
250 #define FWXF_RCVD 10
251         u_int8_t state;
252         u_int8_t retry;
253         u_int8_t tl;
254         void (*retry_req) (struct fw_xfer *);
255         union{
256                 void (*hand) (struct fw_xfer *);
257         } act;
258         struct {
259                 struct fw_pkt hdr;
260                 u_int32_t *payload;
261                 u_int16_t pay_len;
262                 u_int8_t spd;
263         } send, recv;
264         struct mbuf *mbuf;
265         STAILQ_ENTRY(fw_xfer) link;
266         struct malloc_type *malloc;
267 };
268
269 struct fw_rcv_buf {
270         struct firewire_comm *fc;
271         struct fw_xfer *xfer;
272         struct iovec *vec;
273         u_int nvec;
274         u_int8_t spd;
275 };
276
277 void fw_sidrcv (struct firewire_comm *, u_int32_t *, u_int);
278 void fw_rcv (struct fw_rcv_buf *);
279 void fw_xfer_unload ( struct fw_xfer*);
280 void fw_xfer_free_buf ( struct fw_xfer*);
281 void fw_xfer_free ( struct fw_xfer*);
282 struct fw_xfer *fw_xfer_alloc (struct malloc_type *);
283 struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
284 void fw_init (struct firewire_comm *);
285 int fw_tbuf_update (struct firewire_comm *, int, int);
286 int fw_rbuf_update (struct firewire_comm *, int, int);
287 void fw_asybusy (struct fw_xfer *);
288 int fw_bindadd (struct firewire_comm *, struct fw_bind *);
289 int fw_bindremove (struct firewire_comm *, struct fw_bind *);
290 int fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
291 void fw_busreset (struct firewire_comm *);
292 u_int16_t fw_crc16 (u_int32_t *, u_int32_t);
293 void fw_xfer_timeout (void *);
294 void fw_xfer_done (struct fw_xfer *);
295 void fw_asy_callback (struct fw_xfer *);
296 void fw_asy_callback_free (struct fw_xfer *);
297 struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
298 struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
299 struct fw_bind *fw_bindlookup (struct firewire_comm *, u_int16_t, u_int32_t);
300 void fw_drain_txq (struct firewire_comm *);
301 int fwdev_makedev (struct firewire_softc *);
302 int fwdev_destroydev (struct firewire_softc *);
303 void fwdev_clone (void *, char *, int, dev_t *);
304
305 extern int firewire_debug;
306 extern devclass_t firewire_devclass;
307
308 #ifdef __DragonFly__
309 #define         FWPRI           PCATCH
310 #else
311 #define         FWPRI           ((PZERO+8)|PCATCH)
312 #endif
313
314 #if defined(__DragonFly__) || __FreeBSD_version < 500000
315 #define CALLOUT_INIT(x) callout_init(x)
316 #else
317 #define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
318 #endif
319
320 #if defined(__DragonFly__)
321 #define MIN(a,b) (((a)<(b))?(a):(b))
322 #define MAX(a,b) (((a)>(b))?(a):(b))
323 #endif
324
325 MALLOC_DECLARE(M_FW);
326 MALLOC_DECLARE(M_FWXFER);