firewirereg.h revision 171457
1132451Sroberto/*-
2132451Sroberto * Copyright (c) 2003 Hidetoshi Shimokawa
3290001Sglebius * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4290001Sglebius * All rights reserved.
5290001Sglebius *
6290001Sglebius * Redistribution and use in source and binary forms, with or without
7290001Sglebius * modification, are permitted provided that the following conditions
8290001Sglebius * are met:
9290001Sglebius * 1. Redistributions of source code must retain the above copyright
10290001Sglebius *    notice, this list of conditions and the following disclaimer.
11290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
12290001Sglebius *    notice, this list of conditions and the following disclaimer in the
13290001Sglebius *    documentation and/or other materials provided with the distribution.
14290001Sglebius * 3. All advertising materials mentioning features or use of this software
15290001Sglebius *    must display the acknowledgement as bellow:
16290001Sglebius *
17290001Sglebius *    This product includes software developed by K. Kobayashi and H. Shimokawa
18290001Sglebius *
19290001Sglebius * 4. The name of the author may not be used to endorse or promote products
20290001Sglebius *    derived from this software without specific prior written permission.
21290001Sglebius *
22290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24290001Sglebius * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25290001Sglebius * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26290001Sglebius * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27290001Sglebius * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28290001Sglebius * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29290001Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30290001Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31290001Sglebius * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32290001Sglebius * POSSIBILITY OF SUCH DAMAGE.
33290001Sglebius *
34132451Sroberto * $FreeBSD: head/sys/dev/firewire/firewirereg.h 171457 2007-07-15 13:00:29Z simokawa $
35132451Sroberto *
36290001Sglebius */
37290001Sglebius
38132451Sroberto#ifdef __DragonFly__
39132451Srobertotypedef	d_thread_t fw_proc;
40132451Sroberto#include <sys/select.h>
41132451Sroberto#elif __FreeBSD_version >= 500000
42132451Srobertotypedef	struct thread fw_proc;
43132451Sroberto#include <sys/selinfo.h>
44290001Sglebius#else
45290001Sglebiustypedef	struct proc fw_proc;
46290001Sglebius#include <sys/select.h>
47290001Sglebius#endif
48290001Sglebius
49290001Sglebius#include <sys/uio.h>
50290001Sglebius#include <sys/mutex.h>
51290001Sglebius#include <sys/taskqueue.h>
52290001Sglebius
53290001Sglebius#define	splfw splimp
54290001Sglebius
55290001SglebiusSTAILQ_HEAD(fw_xferlist, fw_xfer);
56290001Sglebius
57290001Sglebiusstruct fw_device{
58290001Sglebius	uint16_t dst;
59290001Sglebius	struct fw_eui64 eui;
60290001Sglebius	uint8_t speed;
61290001Sglebius	uint8_t maxrec;
62290001Sglebius	uint8_t nport;
63290001Sglebius	uint8_t power;
64290001Sglebius#define CSRROMOFF 0x400
65290001Sglebius#define CSRROMSIZE 0x400
66290001Sglebius	int rommax;	/* offset from 0xffff f000 0000 */
67290001Sglebius	uint32_t csrrom[CSRROMSIZE/4];
68290001Sglebius	int rcnt;
69290001Sglebius	struct firewire_comm *fc;
70290001Sglebius	uint32_t status;
71290001Sglebius#define FWDEVINIT	1
72#define FWDEVATTACHED	2
73#define FWDEVINVAL	3
74	STAILQ_ENTRY(fw_device) link;
75};
76
77struct firewire_softc {
78#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
79	struct cdev *dev;
80#endif
81	struct firewire_comm *fc;
82};
83
84#define FW_MAX_DMACH 0x20
85#define FW_MAX_DEVCH FW_MAX_DMACH
86#define FW_XFERTIMEOUT 1
87
88struct firewire_dev_comm {
89	device_t dev;
90	struct firewire_comm *fc;
91	void (*post_busreset) (void *);
92	void (*post_explore) (void *);
93};
94
95struct tcode_info {
96	u_char hdr_len;	/* IEEE1394 header length */
97	u_char flag;
98#define FWTI_REQ	(1 << 0)
99#define FWTI_RES	(1 << 1)
100#define FWTI_TLABEL	(1 << 2)
101#define FWTI_BLOCK_STR	(1 << 3)
102#define FWTI_BLOCK_ASY	(1 << 4)
103	u_char valid_res;
104};
105
106struct firewire_comm{
107	device_t dev;
108	device_t bdev;
109	uint16_t busid:10,
110		  nodeid:6;
111	u_int mode;
112	u_int nport;
113	u_int speed;
114	u_int maxrec;
115	u_int irm;
116	u_int max_node;
117	u_int max_hop;
118#define FWPHYASYST (1 << 0)
119	uint32_t status;
120#define	FWBUSDETACH	(-2)
121#define	FWBUSNOTREADY	(-1)
122#define	FWBUSRESET	0
123#define	FWBUSINIT	1
124#define	FWBUSCYMELECT	2
125#define	FWBUSMGRELECT	3
126#define	FWBUSMGRDONE	4
127#define	FWBUSEXPLORE	5
128#define	FWBUSPHYCONF	6
129#define	FWBUSEXPDONE	7
130#define	FWBUSCOMPLETION	10
131	int nisodma;
132	struct fw_eui64 eui;
133	struct fw_xferq
134		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
135	struct fw_xferlist tlabels[0x40];
136	u_char last_tlabel[0x40];
137	STAILQ_HEAD(, fw_bind) binds;
138	STAILQ_HEAD(, fw_device) devices;
139	u_int  sid_cnt;
140#define CSRSIZE 0x4000
141	uint32_t csr_arc[CSRSIZE/4];
142#define CROMSIZE 0x400
143	uint32_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 task task_timeout;
153	uint32_t (*cyctimer) (struct  firewire_comm *);
154	void (*ibr) (struct firewire_comm *);
155	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
156	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	struct mtx mtx;
169	struct mtx wait_lock;
170	struct taskqueue *taskqueue;
171	struct proc *probe_thread;
172};
173#define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
174
175#define FW_GMTX(fc)		(&(fc)->mtx)
176#define FW_GLOCK(fc)		mtx_lock(FW_GMTX(fc))
177#define FW_GUNLOCK(fc)		mtx_unlock(FW_GMTX(fc))
178#define FW_GLOCK_ASSERT(fc)	mtx_assert(FW_GMTX(fc), MA_OWNED)
179
180struct fw_xferq {
181	int flag;
182#define FWXFERQ_CHTAGMASK 0xff
183#define FWXFERQ_RUNNING (1 << 8)
184#define FWXFERQ_STREAM (1 << 9)
185
186#define FWXFERQ_BULK (1 << 11)
187#define FWXFERQ_MODEMASK (7 << 10)
188
189#define FWXFERQ_EXTBUF (1 << 13)
190#define FWXFERQ_OPEN (1 << 14)
191
192#define FWXFERQ_HANDLER (1 << 16)
193#define FWXFERQ_WAKEUP (1 << 17)
194	void (*start) (struct firewire_comm*);
195	int dmach;
196	struct fw_xferlist q;
197	u_int queued;
198	u_int maxq;
199	u_int psize;
200	struct fwdma_alloc_multi *buf;
201	u_int bnchunk;
202	u_int bnpacket;
203	struct fw_bulkxfer *bulkxfer;
204	STAILQ_HEAD(, fw_bulkxfer) stvalid;
205	STAILQ_HEAD(, fw_bulkxfer) stfree;
206	STAILQ_HEAD(, fw_bulkxfer) stdma;
207	struct fw_bulkxfer *stproc;
208	struct selinfo rsel;
209	caddr_t sc;
210	void (*hand) (struct fw_xferq *);
211};
212
213struct fw_bulkxfer{
214	int poffset;
215	struct mbuf *mbuf;
216	STAILQ_ENTRY(fw_bulkxfer) link;
217	caddr_t start;
218	caddr_t end;
219	int resp;
220};
221
222struct fw_bind{
223	u_int64_t start;
224	u_int64_t end;
225	struct fw_xferlist xferlist;
226	STAILQ_ENTRY(fw_bind) fclist;
227	STAILQ_ENTRY(fw_bind) chlist;
228	void *sc;
229};
230
231struct fw_xfer{
232	caddr_t sc;
233	struct firewire_comm *fc;
234	struct fw_xferq *q;
235	struct timeval tv;
236	int8_t resp;
237#define FWXF_INIT	0x00
238#define FWXF_INQ	0x01
239#define FWXF_START	0x02
240#define FWXF_SENT	0x04
241#define FWXF_SENTERR	0x08
242#define FWXF_BUSY	0x10
243#define FWXF_RCVD	0x20
244
245#define FWXF_WAKE	0x80
246	uint8_t flag;
247	int8_t tl;
248	void (*hand) (struct fw_xfer *);
249	struct {
250		struct fw_pkt hdr;
251		uint32_t *payload;
252		uint16_t pay_len;
253		uint8_t spd;
254	} send, recv;
255	struct mbuf *mbuf;
256	STAILQ_ENTRY(fw_xfer) link;
257	STAILQ_ENTRY(fw_xfer) tlabel;
258	struct malloc_type *malloc;
259};
260
261struct fw_rcv_buf {
262	struct firewire_comm *fc;
263	struct fw_xfer *xfer;
264	struct iovec *vec;
265	u_int nvec;
266	uint8_t spd;
267};
268
269void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
270void fw_rcv (struct fw_rcv_buf *);
271void fw_xfer_unload ( struct fw_xfer*);
272void fw_xfer_free_buf ( struct fw_xfer*);
273void fw_xfer_free ( struct fw_xfer*);
274struct fw_xfer *fw_xfer_alloc (struct malloc_type *);
275struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
276void fw_init (struct firewire_comm *);
277int fw_tbuf_update (struct firewire_comm *, int, int);
278int fw_rbuf_update (struct firewire_comm *, int, int);
279int fw_bindadd (struct firewire_comm *, struct fw_bind *);
280int fw_bindremove (struct firewire_comm *, struct fw_bind *);
281int fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int,
282    struct firewire_comm *, void *, void (*)(struct fw_xfer *));
283void fw_xferlist_remove (struct fw_xferlist *);
284int fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
285void fw_busreset (struct firewire_comm *, uint32_t);
286uint16_t fw_crc16 (uint32_t *, uint32_t);
287void fw_xfer_timeout (void *);
288void fw_xfer_done (struct fw_xfer *);
289void fw_xferwake (struct fw_xfer *);
290int fw_xferwait (struct fw_xfer *);
291void fw_asy_callback_free (struct fw_xfer *);
292struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
293struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
294struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
295void fw_drain_txq (struct firewire_comm *);
296int fwdev_makedev (struct firewire_softc *);
297int fwdev_destroydev (struct firewire_softc *);
298void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **);
299int fw_open_isodma(struct firewire_comm *, int);
300
301extern int firewire_debug;
302extern devclass_t firewire_devclass;
303extern int firewire_phydma_enable;
304
305#ifdef __DragonFly__
306#define		FWPRI		PCATCH
307#else
308#define		FWPRI		((PZERO+8)|PCATCH)
309#endif
310
311#if defined(__DragonFly__) || __FreeBSD_version < 500000
312#define CALLOUT_INIT(x) callout_init(x)
313#else
314#define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */)
315#endif
316
317#if defined(__DragonFly__) || __FreeBSD_version < 500000
318/* compatibility shim for 4.X */
319#define bio buf
320#define bio_bcount b_bcount
321#define bio_cmd b_flags
322#define bio_count b_count
323#define bio_data b_data
324#define bio_dev b_dev
325#define bio_error b_error
326#define bio_flags b_flags
327#define bio_offset b_offset
328#define bio_resid b_resid
329#define BIO_ERROR B_ERROR
330#define BIO_READ B_READ
331#define BIO_WRITE B_WRITE
332#define MIN(a,b) (((a)<(b))?(a):(b))
333#define MAX(a,b) (((a)>(b))?(a):(b))
334#endif
335
336MALLOC_DECLARE(M_FW);
337MALLOC_DECLARE(M_FWXFER);
338