firewirereg.h revision 130585
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 130585 2004-06-16 09:47:26Z phk $
35103285Sikob *
36103285Sikob */
37103285Sikob
38127468Ssimokawa#ifdef __DragonFly__
39127468Ssimokawatypedef	d_thread_t fw_proc;
40127468Ssimokawa#include <sys/select.h>
41127468Ssimokawa#elif __FreeBSD_version >= 500000
42103285Sikobtypedef	struct thread fw_proc;
43103285Sikob#include <sys/selinfo.h>
44103285Sikob#else
45103285Sikobtypedef	struct proc fw_proc;
46103285Sikob#include <sys/select.h>
47103285Sikob#endif
48103285Sikob
49113584Ssimokawa#include <sys/uio.h>
50113584Ssimokawa
51103285Sikob#define	splfw splimp
52103285Sikob
53103285Sikobstruct fw_device{
54129585Sdfr	uint16_t dst;
55103285Sikob	struct fw_eui64 eui;
56129585Sdfr	uint8_t speed;
57129585Sdfr	uint8_t maxrec;
58129585Sdfr	uint8_t nport;
59129585Sdfr	uint8_t power;
60103285Sikob#define CSRROMOFF 0x400
61103285Sikob#define CSRROMSIZE 0x400
62103285Sikob	int rommax;	/* offset from 0xffff f000 0000 */
63129585Sdfr	uint32_t csrrom[CSRROMSIZE/4];
64103285Sikob	int rcnt;
65106810Ssimokawa	struct firewire_comm *fc;
66129585Sdfr	uint32_t status;
67103285Sikob#define FWDEVINIT	1
68103285Sikob#define FWDEVATTACHED	2
69103285Sikob#define FWDEVINVAL	3
70110193Ssimokawa	STAILQ_ENTRY(fw_device) link;
71103285Sikob};
72109645Ssimokawa
73103285Sikobstruct firewire_softc {
74127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
75130585Sphk	struct cdev *dev;
76103285Sikob#endif
77103285Sikob	struct firewire_comm *fc;
78103285Sikob};
79109645Ssimokawa
80103285Sikob#define FW_MAX_DMACH 0x20
81103285Sikob#define FW_MAX_DEVCH FW_MAX_DMACH
82103285Sikob#define FW_XFERTIMEOUT 1
83109645Ssimokawa
84103285Sikobstruct firewire_dev_comm {
85103285Sikob	device_t dev;
86103285Sikob	struct firewire_comm *fc;
87124169Ssimokawa	void (*post_busreset) (void *);
88124169Ssimokawa	void (*post_explore) (void *);
89103285Sikob};
90103285Sikob
91103285Sikobstruct tcode_info {
92103285Sikob	u_char hdr_len;	/* IEEE1394 header length */
93103285Sikob	u_char flag;
94103285Sikob#define FWTI_REQ	(1 << 0)
95103285Sikob#define FWTI_RES	(1 << 1)
96103285Sikob#define FWTI_TLABEL	(1 << 2)
97103285Sikob#define FWTI_BLOCK_STR	(1 << 3)
98103285Sikob#define FWTI_BLOCK_ASY	(1 << 4)
99103285Sikob};
100103285Sikob
101103285Sikobstruct firewire_comm{
102103285Sikob	device_t dev;
103103285Sikob	device_t bdev;
104129585Sdfr	uint16_t busid:10,
105103285Sikob		  nodeid:6;
106103285Sikob	u_int mode;
107103285Sikob	u_int nport;
108103285Sikob	u_int speed;
109103285Sikob	u_int maxrec;
110103285Sikob	u_int irm;
111103285Sikob	u_int max_node;
112103285Sikob	u_int max_hop;
113103285Sikob	u_int max_asyretry;
114103285Sikob#define FWPHYASYST (1 << 0)
115103285Sikob	u_int retry_count;
116129585Sdfr	uint32_t ongobus:10,
117103285Sikob		  ongonode:6,
118103285Sikob		  ongoaddr:16;
119103285Sikob	struct fw_device *ongodev;
120103285Sikob	struct fw_eui64 ongoeui;
121103285Sikob#define	FWMAXCSRDIR     16
122103285Sikob	SLIST_HEAD(, csrdir) ongocsr;
123103285Sikob	SLIST_HEAD(, csrdir) csrfree;
124129585Sdfr	uint32_t status;
125116978Ssimokawa#define	FWBUSNOTREADY	(-1)
126103285Sikob#define	FWBUSRESET	0
127103285Sikob#define	FWBUSINIT	1
128103285Sikob#define	FWBUSCYMELECT	2
129103285Sikob#define	FWBUSMGRELECT	3
130103285Sikob#define	FWBUSMGRDONE	4
131103285Sikob#define	FWBUSEXPLORE	5
132103285Sikob#define	FWBUSPHYCONF	6
133103285Sikob#define	FWBUSEXPDONE	7
134103285Sikob#define	FWBUSCOMPLETION	10
135103285Sikob	int nisodma;
136109814Ssimokawa	struct fw_eui64 eui;
137103285Sikob	struct fw_xferq
138103285Sikob		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
139103285Sikob	STAILQ_HEAD(, tlabel) tlabels[0x40];
140103285Sikob	STAILQ_HEAD(, fw_bind) binds;
141110193Ssimokawa	STAILQ_HEAD(, fw_device) devices;
142103285Sikob	u_int  sid_cnt;
143103285Sikob#define CSRSIZE 0x4000
144129585Sdfr	uint32_t csr_arc[CSRSIZE/4];
145103285Sikob#define CROMSIZE 0x400
146129585Sdfr	uint32_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;
156129585Sdfr	uint32_t (*cyctimer) (struct  firewire_comm *);
157124169Ssimokawa	void (*ibr) (struct firewire_comm *);
158129585Sdfr	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
159130585Sphk	int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *);
160124169Ssimokawa	int (*irx_enable) (struct firewire_comm *, int);
161124169Ssimokawa	int (*irx_disable) (struct firewire_comm *, int);
162124169Ssimokawa	int (*itx_enable) (struct firewire_comm *, int);
163124169Ssimokawa	int (*itx_disable) (struct firewire_comm *, int);
164124169Ssimokawa	void (*timeout) (void *);
165124169Ssimokawa	void (*poll) (struct firewire_comm *, int, int);
166124169Ssimokawa	void (*set_intr) (struct firewire_comm *, int);
167129585Sdfr	void (*irx_post) (struct firewire_comm *, uint32_t *);
168129585Sdfr	void (*itx_post) (struct firewire_comm *, uint32_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{
175129585Sdfr	uint32_t ongoaddr;
176129585Sdfr	uint32_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)
194124169Ssimokawa	void (*start) (struct firewire_comm*);
195118293Ssimokawa	int dmach;
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;
211124169Ssimokawa	void (*hand) (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{
229120660Ssimokawa	u_int64_t start;
230120660Ssimokawa	u_int64_t end;
231113584Ssimokawa	STAILQ_HEAD(, fw_xfer) xferlist;
232109645Ssimokawa	STAILQ_ENTRY(fw_bind) fclist;
233109645Ssimokawa	STAILQ_ENTRY(fw_bind) chlist;
234113584Ssimokawa#define FWACT_NULL	0
235113584Ssimokawa#define FWACT_XFER	2
236113584Ssimokawa#define FWACT_CH	3
237129585Sdfr	uint8_t act_type;
238129585Sdfr	uint8_t sub;
239109645Ssimokawa};
240109645Ssimokawa
241103285Sikobstruct fw_xfer{
242103285Sikob	caddr_t sc;
243103285Sikob	struct firewire_comm *fc;
244103285Sikob	struct fw_xferq *q;
245110577Ssimokawa	struct timeval tv;
246113584Ssimokawa	int8_t resp;
247103285Sikob#define FWXF_INIT 0
248103285Sikob#define FWXF_INQ 1
249103285Sikob#define FWXF_START 2
250103285Sikob#define FWXF_SENT 3
251103285Sikob#define FWXF_SENTERR 4
252103285Sikob#define FWXF_BUSY 8
253103285Sikob#define FWXF_RCVD 10
254129585Sdfr	uint8_t state;
255129585Sdfr	uint8_t retry;
256129585Sdfr	uint8_t tl;
257124169Ssimokawa	void (*retry_req) (struct fw_xfer *);
258103285Sikob	union{
259124169Ssimokawa		void (*hand) (struct fw_xfer *);
260103285Sikob	} act;
261103285Sikob	struct {
262120660Ssimokawa		struct fw_pkt hdr;
263129585Sdfr		uint32_t *payload;
264129585Sdfr		uint16_t pay_len;
265129585Sdfr		uint8_t spd;
266103285Sikob	} send, recv;
267103285Sikob	struct mbuf *mbuf;
268103285Sikob	STAILQ_ENTRY(fw_xfer) link;
269110269Ssimokawa	struct malloc_type *malloc;
270103285Sikob};
271120660Ssimokawa
272120660Ssimokawastruct fw_rcv_buf {
273120660Ssimokawa	struct firewire_comm *fc;
274120660Ssimokawa	struct fw_xfer *xfer;
275120660Ssimokawa	struct iovec *vec;
276120660Ssimokawa	u_int nvec;
277129585Sdfr	uint8_t spd;
278120660Ssimokawa};
279120660Ssimokawa
280129585Sdfrvoid fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
281124169Ssimokawavoid fw_rcv (struct fw_rcv_buf *);
282124169Ssimokawavoid fw_xfer_unload ( struct fw_xfer*);
283124169Ssimokawavoid fw_xfer_free_buf ( struct fw_xfer*);
284124169Ssimokawavoid fw_xfer_free ( struct fw_xfer*);
285124169Ssimokawastruct fw_xfer *fw_xfer_alloc (struct malloc_type *);
286124169Ssimokawastruct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
287124169Ssimokawavoid fw_init (struct firewire_comm *);
288124169Ssimokawaint fw_tbuf_update (struct firewire_comm *, int, int);
289124169Ssimokawaint fw_rbuf_update (struct firewire_comm *, int, int);
290124169Ssimokawavoid fw_asybusy (struct fw_xfer *);
291124169Ssimokawaint fw_bindadd (struct firewire_comm *, struct fw_bind *);
292124169Ssimokawaint fw_bindremove (struct firewire_comm *, struct fw_bind *);
293124169Ssimokawaint fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
294124169Ssimokawavoid fw_busreset (struct firewire_comm *);
295129585Sdfruint16_t fw_crc16 (uint32_t *, uint32_t);
296124169Ssimokawavoid fw_xfer_timeout (void *);
297124169Ssimokawavoid fw_xfer_done (struct fw_xfer *);
298124169Ssimokawavoid fw_asy_callback (struct fw_xfer *);
299124169Ssimokawavoid fw_asy_callback_free (struct fw_xfer *);
300124169Ssimokawastruct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
301124169Ssimokawastruct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
302129585Sdfrstruct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
303124169Ssimokawavoid fw_drain_txq (struct firewire_comm *);
304124169Ssimokawaint fwdev_makedev (struct firewire_softc *);
305124169Ssimokawaint fwdev_destroydev (struct firewire_softc *);
306130585Sphkvoid fwdev_clone (void *, char *, int, struct cdev **);
307103285Sikob
308103285Sikobextern int firewire_debug;
309103285Sikobextern devclass_t firewire_devclass;
310103285Sikob
311127468Ssimokawa#ifdef __DragonFly__
312127468Ssimokawa#define		FWPRI		PCATCH
313127468Ssimokawa#else
314103285Sikob#define		FWPRI		((PZERO+8)|PCATCH)
315127468Ssimokawa#endif
316103285Sikob
317127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
318127468Ssimokawa#define CALLOUT_INIT(x) callout_init(x)
319127468Ssimokawa#else
320111615Ssimokawa#define CALLOUT_INIT(x) callout_init(x, 0 /* mpsafe */)
321111615Ssimokawa#endif
322111615Ssimokawa
323127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
324120660Ssimokawa/* compatibility shim for 4.X */
325120660Ssimokawa#define bio buf
326120660Ssimokawa#define bio_bcount b_bcount
327120660Ssimokawa#define bio_cmd b_flags
328120660Ssimokawa#define bio_count b_count
329120660Ssimokawa#define bio_data b_data
330120660Ssimokawa#define bio_dev b_dev
331120660Ssimokawa#define bio_error b_error
332120660Ssimokawa#define bio_flags b_flags
333120660Ssimokawa#define bio_offset b_offset
334120660Ssimokawa#define bio_resid b_resid
335120660Ssimokawa#define BIO_ERROR B_ERROR
336120660Ssimokawa#define BIO_READ B_READ
337120660Ssimokawa#define BIO_WRITE B_WRITE
338120660Ssimokawa#define MIN(a,b) (((a)<(b))?(a):(b))
339121780Ssimokawa#define MAX(a,b) (((a)>(b))?(a):(b))
340120660Ssimokawa#endif
341120660Ssimokawa
342110195SsimokawaMALLOC_DECLARE(M_FW);
343110269SsimokawaMALLOC_DECLARE(M_FWXFER);
344