firewirereg.h revision 116978
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3103285Sikob * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/firewirereg.h 116978 2003-06-28 11:11:36Z simokawa $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#if __FreeBSD_version >= 500000
39103285Sikobtypedef	struct thread fw_proc;
40103285Sikob#include <sys/selinfo.h>
41103285Sikob#else
42103285Sikobtypedef	struct proc fw_proc;
43103285Sikob#include <sys/select.h>
44103285Sikob#endif
45103285Sikob
46113584Ssimokawa#include <sys/uio.h>
47113584Ssimokawa
48103285Sikob#define	splfw splimp
49103285Sikob
50103285Sikobstruct fw_device{
51103285Sikob	u_int16_t dst;
52103285Sikob	struct fw_eui64 eui;
53103285Sikob	u_int8_t speed;
54103285Sikob	u_int8_t maxrec;
55103285Sikob	u_int8_t nport;
56103285Sikob	u_int8_t power;
57103285Sikob#define CSRROMOFF 0x400
58103285Sikob#define CSRROMSIZE 0x400
59103285Sikob	int rommax;	/* offset from 0xffff f000 0000 */
60103285Sikob	u_int32_t csrrom[CSRROMSIZE/4];
61103285Sikob	int rcnt;
62106810Ssimokawa	struct firewire_comm *fc;
63103285Sikob	u_int32_t status;
64103285Sikob#define FWDEVINIT	1
65103285Sikob#define FWDEVATTACHED	2
66103285Sikob#define FWDEVINVAL	3
67110193Ssimokawa	STAILQ_ENTRY(fw_device) link;
68103285Sikob};
69109645Ssimokawa
70103285Sikobstruct firewire_softc {
71103285Sikob#if __FreeBSD_version >= 500000
72103285Sikob	dev_t dev;
73103285Sikob#else
74103285Sikob	dev_t dev[FWMAXNDMA+1];
75103285Sikob#endif
76103285Sikob	struct firewire_comm *fc;
77103285Sikob};
78109645Ssimokawa
79103285Sikob#define FW_MAX_DMACH 0x20
80103285Sikob#define FW_MAX_DEVCH FW_MAX_DMACH
81103285Sikob#define FW_XFERTIMEOUT 1
82109645Ssimokawa
83103285Sikobstruct firewire_dev_comm {
84103285Sikob	device_t dev;
85103285Sikob	struct firewire_comm *fc;
86113584Ssimokawa	void (*post_busreset) __P((void *));
87103285Sikob	void (*post_explore) __P((void *));
88103285Sikob};
89103285Sikob
90103285Sikobstruct tcode_info {
91103285Sikob	u_char hdr_len;	/* IEEE1394 header length */
92103285Sikob	u_char flag;
93103285Sikob#define FWTI_REQ	(1 << 0)
94103285Sikob#define FWTI_RES	(1 << 1)
95103285Sikob#define FWTI_TLABEL	(1 << 2)
96103285Sikob#define FWTI_BLOCK_STR	(1 << 3)
97103285Sikob#define FWTI_BLOCK_ASY	(1 << 4)
98103285Sikob};
99103285Sikob
100103285Sikobstruct firewire_comm{
101103285Sikob	device_t dev;
102103285Sikob	device_t bdev;
103103285Sikob	u_int16_t busid:10,
104103285Sikob		  nodeid:6;
105103285Sikob	u_int mode;
106103285Sikob	u_int nport;
107103285Sikob	u_int speed;
108103285Sikob	u_int maxrec;
109103285Sikob	u_int irm;
110103285Sikob	u_int max_node;
111103285Sikob	u_int max_hop;
112103285Sikob	u_int max_asyretry;
113103285Sikob#define FWPHYASYST (1 << 0)
114103285Sikob	u_int retry_count;
115103285Sikob	u_int32_t ongobus:10,
116103285Sikob		  ongonode:6,
117103285Sikob		  ongoaddr:16;
118103285Sikob	struct fw_device *ongodev;
119103285Sikob	struct fw_eui64 ongoeui;
120103285Sikob#define	FWMAXCSRDIR     16
121103285Sikob	SLIST_HEAD(, csrdir) ongocsr;
122103285Sikob	SLIST_HEAD(, csrdir) csrfree;
123103285Sikob	u_int32_t status;
124116978Ssimokawa#define	FWBUSNOTREADY	(-1)
125103285Sikob#define	FWBUSRESET	0
126103285Sikob#define	FWBUSINIT	1
127103285Sikob#define	FWBUSCYMELECT	2
128103285Sikob#define	FWBUSMGRELECT	3
129103285Sikob#define	FWBUSMGRDONE	4
130103285Sikob#define	FWBUSEXPLORE	5
131103285Sikob#define	FWBUSPHYCONF	6
132103285Sikob#define	FWBUSEXPDONE	7
133103285Sikob#define	FWBUSCOMPLETION	10
134103285Sikob	int nisodma;
135109814Ssimokawa	struct fw_eui64 eui;
136103285Sikob	struct fw_xferq
137103285Sikob		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
138103285Sikob	STAILQ_HEAD(, tlabel) tlabels[0x40];
139103285Sikob	STAILQ_HEAD(, fw_bind) binds;
140110193Ssimokawa	STAILQ_HEAD(, fw_device) devices;
141103285Sikob	STAILQ_HEAD(, fw_xfer)	pending;
142103285Sikob	u_int  sid_cnt;
143103285Sikob#define CSRSIZE 0x4000
144103285Sikob	u_int32_t csr_arc[CSRSIZE/4];
145103285Sikob#define CROMSIZE 0x400
146103285Sikob	u_int32_t *config_rom;
147116376Ssimokawa	struct crom_src_buf *crom_src_buf;
148116376Ssimokawa	struct crom_src *crom_src;
149116376Ssimokawa	struct crom_chunk *crom_root;
150103285Sikob	struct fw_topology_map *topology_map;
151103285Sikob	struct fw_speed_map *speed_map;
152108853Ssimokawa	struct callout busprobe_callout;
153110193Ssimokawa	struct callout bmr_callout;
154110193Ssimokawa	struct callout timeout_callout;
155110193Ssimokawa	struct callout retry_probe_callout;
156103285Sikob	u_int32_t (*cyctimer) __P((struct  firewire_comm *));
157103285Sikob	void (*ibr) __P((struct firewire_comm *));
158103285Sikob	u_int32_t (*set_bmr) __P((struct firewire_comm *, u_int32_t));
159103285Sikob	int (*ioctl) __P((dev_t, u_long, caddr_t, int, fw_proc *));
160103285Sikob	int (*irx_enable) __P((struct firewire_comm *, int));
161103285Sikob	int (*irx_disable) __P((struct firewire_comm *, int));
162103285Sikob	int (*itx_enable) __P((struct firewire_comm *, int));
163103285Sikob	int (*itx_disable) __P((struct firewire_comm *, int));
164103285Sikob	void (*timeout) __P((void *));
165103285Sikob	void (*poll) __P((struct firewire_comm *, int, int));
166103285Sikob	void (*set_intr) __P((struct firewire_comm *, int));
167103285Sikob	void (*irx_post) __P((struct firewire_comm *, u_int32_t *));
168103285Sikob	void (*itx_post) __P((struct firewire_comm *, u_int32_t *));
169103285Sikob	struct tcode_info *tcode;
170113584Ssimokawa	bus_dma_tag_t dmat;
171103285Sikob};
172103285Sikob#define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
173103285Sikob
174109645Ssimokawastruct csrdir{
175109645Ssimokawa	u_int32_t ongoaddr;
176109645Ssimokawa	u_int32_t off;
177109645Ssimokawa	SLIST_ENTRY(csrdir) link;
178109645Ssimokawa};
179103285Sikob
180109645Ssimokawastruct fw_xferq {
181109645Ssimokawa	int flag;
182109645Ssimokawa#define FWXFERQ_CHTAGMASK 0xff
183109645Ssimokawa#define FWXFERQ_RUNNING (1 << 8)
184109645Ssimokawa#define FWXFERQ_STREAM (1 << 9)
185109645Ssimokawa
186109645Ssimokawa#define FWXFERQ_BULK (1 << 11)
187109645Ssimokawa#define FWXFERQ_MODEMASK (7 << 10)
188109645Ssimokawa
189109645Ssimokawa#define FWXFERQ_EXTBUF (1 << 13)
190109645Ssimokawa#define FWXFERQ_OPEN (1 << 14)
191109645Ssimokawa
192109645Ssimokawa#define FWXFERQ_HANDLER (1 << 16)
193109645Ssimokawa#define FWXFERQ_WAKEUP (1 << 17)
194109645Ssimokawa
195109645Ssimokawa	void (*start) __P((struct firewire_comm*));
196113584Ssimokawa	STAILQ_HEAD(, fw_xfer) q;
197109645Ssimokawa	u_int queued;
198109645Ssimokawa	u_int maxq;
199109645Ssimokawa	u_int psize;
200109645Ssimokawa	STAILQ_HEAD(, fw_bind) binds;
201113584Ssimokawa	struct fwdma_alloc_multi *buf;
202109645Ssimokawa	u_int bnchunk;
203109645Ssimokawa	u_int bnpacket;
204109645Ssimokawa	struct fw_bulkxfer *bulkxfer;
205109645Ssimokawa	STAILQ_HEAD(, fw_bulkxfer) stvalid;
206109645Ssimokawa	STAILQ_HEAD(, fw_bulkxfer) stfree;
207109890Ssimokawa	STAILQ_HEAD(, fw_bulkxfer) stdma;
208109645Ssimokawa	struct fw_bulkxfer *stproc;
209109645Ssimokawa	struct selinfo rsel;
210109645Ssimokawa	caddr_t sc;
211109645Ssimokawa	void (*hand) __P((struct fw_xferq *));
212109645Ssimokawa};
213109645Ssimokawa
214109645Ssimokawastruct fw_bulkxfer{
215113584Ssimokawa	int poffset;
216111942Ssimokawa	struct mbuf *mbuf;
217109645Ssimokawa	STAILQ_ENTRY(fw_bulkxfer) link;
218109645Ssimokawa	caddr_t start;
219109645Ssimokawa	caddr_t end;
220111942Ssimokawa	int resp;
221109645Ssimokawa};
222109645Ssimokawa
223109645Ssimokawastruct tlabel{
224109645Ssimokawa	struct fw_xfer  *xfer;
225109645Ssimokawa	STAILQ_ENTRY(tlabel) link;
226109645Ssimokawa};
227109645Ssimokawa
228109645Ssimokawastruct fw_bind{
229109645Ssimokawa	u_int32_t start_hi, start_lo, addrlen;
230113584Ssimokawa	STAILQ_HEAD(, fw_xfer) xferlist;
231109645Ssimokawa	STAILQ_ENTRY(fw_bind) fclist;
232109645Ssimokawa	STAILQ_ENTRY(fw_bind) chlist;
233113584Ssimokawa#define FWACT_NULL	0
234113584Ssimokawa#define FWACT_XFER	2
235113584Ssimokawa#define FWACT_CH	3
236113584Ssimokawa	u_int8_t act_type;
237113584Ssimokawa	u_int8_t sub;
238109645Ssimokawa};
239109645Ssimokawa
240103285Sikobstruct fw_xfer{
241103285Sikob	caddr_t sc;
242103285Sikob	struct firewire_comm *fc;
243103285Sikob	struct fw_xferq *q;
244110577Ssimokawa	struct timeval tv;
245113584Ssimokawa	/* XXX should be removed */
246113584Ssimokawa	u_int32_t dst; /* XXX for if_fwe */
247103285Sikob	u_int8_t spd;
248113584Ssimokawa	int8_t resp;
249103285Sikob#define FWXF_INIT 0
250103285Sikob#define FWXF_INQ 1
251103285Sikob#define FWXF_START 2
252103285Sikob#define FWXF_SENT 3
253103285Sikob#define FWXF_SENTERR 4
254103285Sikob#define FWXF_BUSY 8
255103285Sikob#define FWXF_RCVD 10
256113584Ssimokawa	u_int8_t state;
257103285Sikob	u_int8_t retry;
258103285Sikob	u_int8_t tl;
259103285Sikob	void (*retry_req) __P((struct fw_xfer *));
260103285Sikob	union{
261103285Sikob		void (*hand) __P((struct fw_xfer *));
262103285Sikob	} act;
263103285Sikob	struct {
264113584Ssimokawa		int len;
265103285Sikob		caddr_t buf;
266103285Sikob	} send, recv;
267103285Sikob	struct mbuf *mbuf;
268103285Sikob	STAILQ_ENTRY(fw_xfer) link;
269110269Ssimokawa	struct malloc_type *malloc;
270103285Sikob};
271113584Ssimokawavoid fw_sidrcv __P((struct firewire_comm *, u_int32_t *, u_int));
272113584Ssimokawavoid fw_rcv __P((struct firewire_comm *, struct iovec *, int, u_int, u_int));
273113584Ssimokawavoid fw_xfer_unload __P(( struct fw_xfer*));
274103285Sikobvoid fw_xfer_free __P(( struct fw_xfer*));
275110269Ssimokawastruct fw_xfer *fw_xfer_alloc __P((struct malloc_type *));
276113584Ssimokawastruct fw_xfer *fw_xfer_alloc_buf __P((struct malloc_type *, int, int));
277103285Sikobvoid fw_init __P((struct firewire_comm *));
278103285Sikobint fw_tbuf_update __P((struct firewire_comm *, int, int));
279103285Sikobint fw_rbuf_update __P((struct firewire_comm *, int, int));
280103285Sikobvoid fw_asybusy __P((struct fw_xfer *));
281103285Sikobint fw_bindadd __P((struct firewire_comm *, struct fw_bind *));
282103285Sikobint fw_bindremove __P((struct firewire_comm *, struct fw_bind *));
283103285Sikobint fw_asyreq __P((struct firewire_comm *, int, struct fw_xfer*));
284103285Sikobvoid fw_busreset __P((struct firewire_comm *));
285103285Sikobu_int16_t fw_crc16 __P((u_int32_t *, u_int32_t));
286103285Sikobvoid fw_xfer_timeout __P((void *));
287103285Sikobvoid fw_xfer_done __P((struct fw_xfer *));
288103285Sikobvoid fw_asy_callback __P((struct fw_xfer *));
289110072Ssimokawastruct fw_device *fw_noderesolve_nodeid __P((struct firewire_comm *, int));
290110582Ssimokawastruct fw_device *fw_noderesolve_eui64 __P((struct firewire_comm *, struct fw_eui64 *));
291106813Ssimokawastruct fw_bind *fw_bindlookup __P((struct firewire_comm *, u_int32_t, u_int32_t));
292110798Ssimokawavoid fw_drain_txq __P((struct firewire_comm *));
293103285Sikob
294106813Ssimokawa
295103285Sikobextern int firewire_debug;
296103285Sikobextern devclass_t firewire_devclass;
297103285Sikob
298103285Sikob#define		FWPRI		((PZERO+8)|PCATCH)
299103285Sikob
300111615Ssimokawa#if __FreeBSD_version >= 500000
301111615Ssimokawa#define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
302111615Ssimokawa#else
303111615Ssimokawa#define CALLOUT_INIT(x) callout_init(x)
304111615Ssimokawa#endif
305111615Ssimokawa
306110195SsimokawaMALLOC_DECLARE(M_FW);
307110269SsimokawaMALLOC_DECLARE(M_FWXFER);
308