firewirereg.h revision 167632
1296936Smmel/*-
2296936Smmel * Copyright (c) 2003 Hidetoshi Shimokawa
3296936Smmel * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4296936Smmel * All rights reserved.
5296936Smmel *
6296936Smmel * Redistribution and use in source and binary forms, with or without
7296936Smmel * modification, are permitted provided that the following conditions
8296936Smmel * are met:
9296936Smmel * 1. Redistributions of source code must retain the above copyright
10296936Smmel *    notice, this list of conditions and the following disclaimer.
11296936Smmel * 2. Redistributions in binary form must reproduce the above copyright
12296936Smmel *    notice, this list of conditions and the following disclaimer in the
13296936Smmel *    documentation and/or other materials provided with the distribution.
14296936Smmel * 3. All advertising materials mentioning features or use of this software
15296936Smmel *    must display the acknowledgement as bellow:
16296936Smmel *
17296936Smmel *    This product includes software developed by K. Kobayashi and H. Shimokawa
18296936Smmel *
19296936Smmel * 4. The name of the author may not be used to endorse or promote products
20296936Smmel *    derived from this software without specific prior written permission.
21296936Smmel *
22296936Smmel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23296936Smmel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24296936Smmel * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25296936Smmel * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26296936Smmel * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27296936Smmel * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28296936Smmel * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29296936Smmel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30296936Smmel * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31296936Smmel * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32296936Smmel * POSSIBILITY OF SUCH DAMAGE.
33296936Smmel *
34296936Smmel * $FreeBSD: head/sys/dev/firewire/firewirereg.h 167632 2007-03-16 05:39:33Z simokawa $
35296936Smmel *
36298627Sbr */
37296936Smmel
38296936Smmel#ifdef __DragonFly__
39296936Smmeltypedef	d_thread_t fw_proc;
40296936Smmel#include <sys/select.h>
41296936Smmel#elif __FreeBSD_version >= 500000
42296936Smmeltypedef	struct thread fw_proc;
43296936Smmel#include <sys/selinfo.h>
44296936Smmel#else
45296936Smmeltypedef	struct proc fw_proc;
46296936Smmel#include <sys/select.h>
47296936Smmel#endif
48296936Smmel
49296936Smmel#include <sys/uio.h>
50296936Smmel
51296936Smmel#define	splfw splimp
52296936Smmel
53296936Smmelstruct fw_device{
54296936Smmel	uint16_t dst;
55296936Smmel	struct fw_eui64 eui;
56296936Smmel	uint8_t speed;
57296936Smmel	uint8_t maxrec;
58296936Smmel	uint8_t nport;
59296936Smmel	uint8_t power;
60296936Smmel#define CSRROMOFF 0x400
61296936Smmel#define CSRROMSIZE 0x400
62296936Smmel	int rommax;	/* offset from 0xffff f000 0000 */
63296936Smmel	uint32_t csrrom[CSRROMSIZE/4];
64296936Smmel	int rcnt;
65296936Smmel	struct firewire_comm *fc;
66296936Smmel	uint32_t status;
67296936Smmel#define FWDEVINIT	1
68298627Sbr#define FWDEVATTACHED	2
69296936Smmel#define FWDEVINVAL	3
70296936Smmel	STAILQ_ENTRY(fw_device) link;
71296936Smmel};
72296936Smmel
73296936Smmelstruct firewire_softc {
74296936Smmel#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
75296936Smmel	struct cdev *dev;
76296936Smmel#endif
77296936Smmel	struct firewire_comm *fc;
78296936Smmel};
79296936Smmel
80296936Smmel#define FW_MAX_DMACH 0x20
81296936Smmel#define FW_MAX_DEVCH FW_MAX_DMACH
82296936Smmel#define FW_XFERTIMEOUT 1
83296936Smmel
84296936Smmelstruct firewire_dev_comm {
85296936Smmel	device_t dev;
86296936Smmel	struct firewire_comm *fc;
87296936Smmel	void (*post_busreset) (void *);
88296936Smmel	void (*post_explore) (void *);
89296936Smmel};
90296936Smmel
91296936Smmelstruct tcode_info {
92298627Sbr	u_char hdr_len;	/* IEEE1394 header length */
93296936Smmel	u_char flag;
94296936Smmel#define FWTI_REQ	(1 << 0)
95296936Smmel#define FWTI_RES	(1 << 1)
96331893Sgonzo#define FWTI_TLABEL	(1 << 2)
97331893Sgonzo#define FWTI_BLOCK_STR	(1 << 3)
98296936Smmel#define FWTI_BLOCK_ASY	(1 << 4)
99296936Smmel};
100296936Smmel
101296936Smmelstruct firewire_comm{
102296936Smmel	device_t dev;
103296936Smmel	device_t bdev;
104296936Smmel	uint16_t busid:10,
105296936Smmel		  nodeid:6;
106296936Smmel	u_int mode;
107296936Smmel	u_int nport;
108296936Smmel	u_int speed;
109296936Smmel	u_int maxrec;
110296936Smmel	u_int irm;
111296936Smmel	u_int max_node;
112296936Smmel	u_int max_hop;
113296936Smmel#define FWPHYASYST (1 << 0)
114296936Smmel	uint32_t ongobus:10,
115296936Smmel		  ongonode:6,
116296936Smmel		  ongoaddr:16;
117296936Smmel	struct fw_device *ongodev;
118296936Smmel	struct fw_eui64 ongoeui;
119296936Smmel#define	FWMAXCSRDIR     16
120296936Smmel	SLIST_HEAD(, csrdir) ongocsr;
121296936Smmel	SLIST_HEAD(, csrdir) csrfree;
122296936Smmel	uint32_t status;
123308336Smmel#define	FWBUSNOTREADY	(-1)
124308336Smmel#define	FWBUSRESET	0
125296936Smmel#define	FWBUSINIT	1
126296936Smmel#define	FWBUSCYMELECT	2
127308336Smmel#define	FWBUSMGRELECT	3
128296936Smmel#define	FWBUSMGRDONE	4
129296936Smmel#define	FWBUSEXPLORE	5
130296936Smmel#define	FWBUSPHYCONF	6
131296936Smmel#define	FWBUSEXPDONE	7
132308336Smmel#define	FWBUSCOMPLETION	10
133308336Smmel	int nisodma;
134308336Smmel	struct fw_eui64 eui;
135296936Smmel	struct fw_xferq
136296936Smmel		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
137296936Smmel	STAILQ_HEAD(, tlabel) tlabels[0x40];
138296936Smmel	STAILQ_HEAD(, fw_bind) binds;
139296936Smmel	STAILQ_HEAD(, fw_device) devices;
140296936Smmel	u_int  sid_cnt;
141296936Smmel#define CSRSIZE 0x4000
142296936Smmel	uint32_t csr_arc[CSRSIZE/4];
143296936Smmel#define CROMSIZE 0x400
144296936Smmel	uint32_t *config_rom;
145296936Smmel	struct crom_src_buf *crom_src_buf;
146296936Smmel	struct crom_src *crom_src;
147331893Sgonzo	struct crom_chunk *crom_root;
148331893Sgonzo	struct fw_topology_map *topology_map;
149296936Smmel	struct fw_speed_map *speed_map;
150296936Smmel	struct callout busprobe_callout;
151296936Smmel	struct callout bmr_callout;
152296936Smmel	struct callout timeout_callout;
153296936Smmel	uint32_t (*cyctimer) (struct  firewire_comm *);
154296936Smmel	void (*ibr) (struct firewire_comm *);
155296936Smmel	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
156317006Smmel	int (*ioctl) (struct cdev *, 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 *, uint32_t *);
165	void (*itx_post) (struct firewire_comm *, uint32_t *);
166	struct tcode_info *tcode;
167	bus_dma_tag_t dmat;
168};
169#define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
170
171struct csrdir{
172	uint32_t ongoaddr;
173	uint32_t off;
174	SLIST_ENTRY(csrdir) link;
175};
176
177struct 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
211struct 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
220struct tlabel{
221	struct fw_xfer  *xfer;
222	STAILQ_ENTRY(tlabel) link;
223};
224
225struct 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	uint8_t act_type;
235	uint8_t sub;
236};
237
238struct 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	uint8_t state;
252	uint8_t tl;
253	void (*hand) (struct fw_xfer *);
254	struct {
255		struct fw_pkt hdr;
256		uint32_t *payload;
257		uint16_t pay_len;
258		uint8_t spd;
259	} send, recv;
260	struct mbuf *mbuf;
261	STAILQ_ENTRY(fw_xfer) link;
262	struct malloc_type *malloc;
263};
264
265struct fw_rcv_buf {
266	struct firewire_comm *fc;
267	struct fw_xfer *xfer;
268	struct iovec *vec;
269	u_int nvec;
270	uint8_t spd;
271};
272
273void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
274void fw_rcv (struct fw_rcv_buf *);
275void fw_xfer_unload ( struct fw_xfer*);
276void fw_xfer_free_buf ( struct fw_xfer*);
277void fw_xfer_free ( struct fw_xfer*);
278struct fw_xfer *fw_xfer_alloc (struct malloc_type *);
279struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
280void fw_init (struct firewire_comm *);
281int fw_tbuf_update (struct firewire_comm *, int, int);
282int fw_rbuf_update (struct firewire_comm *, int, int);
283void fw_asybusy (struct fw_xfer *);
284int fw_bindadd (struct firewire_comm *, struct fw_bind *);
285int fw_bindremove (struct firewire_comm *, struct fw_bind *);
286int fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
287void fw_busreset (struct firewire_comm *);
288uint16_t fw_crc16 (uint32_t *, uint32_t);
289void fw_xfer_timeout (void *);
290void fw_xfer_done (struct fw_xfer *);
291void fw_asy_callback (struct fw_xfer *);
292void fw_asy_callback_free (struct fw_xfer *);
293struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
294struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
295struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
296void fw_drain_txq (struct firewire_comm *);
297int fwdev_makedev (struct firewire_softc *);
298int fwdev_destroydev (struct firewire_softc *);
299void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **);
300
301extern int firewire_debug;
302extern devclass_t firewire_devclass;
303
304#ifdef __DragonFly__
305#define		FWPRI		PCATCH
306#else
307#define		FWPRI		((PZERO+8)|PCATCH)
308#endif
309
310#if defined(__DragonFly__) || __FreeBSD_version < 500000
311#define CALLOUT_INIT(x) callout_init(x)
312#else
313#define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
314#endif
315
316#if defined(__DragonFly__) || __FreeBSD_version < 500000
317/* compatibility shim for 4.X */
318#define bio buf
319#define bio_bcount b_bcount
320#define bio_cmd b_flags
321#define bio_count b_count
322#define bio_data b_data
323#define bio_dev b_dev
324#define bio_error b_error
325#define bio_flags b_flags
326#define bio_offset b_offset
327#define bio_resid b_resid
328#define BIO_ERROR B_ERROR
329#define BIO_READ B_READ
330#define BIO_WRITE B_WRITE
331#define MIN(a,b) (((a)<(b))?(a):(b))
332#define MAX(a,b) (((a)>(b))?(a):(b))
333#endif
334
335MALLOC_DECLARE(M_FW);
336MALLOC_DECLARE(M_FWXFER);
337