xref: /freebsd/sys/dev/firewire/firewirereg.h (revision f02f7422801bb39f5eaab8fc383fa7b70c467ff9)
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$
35  *
36  */
37 
38 #ifdef __DragonFly__
39 typedef	d_thread_t fw_proc;
40 #include <sys/select.h>
41 #elif __FreeBSD_version >= 500000
42 typedef	struct thread fw_proc;
43 #include <sys/selinfo.h>
44 #else
45 typedef	struct proc fw_proc;
46 #include <sys/select.h>
47 #endif
48 
49 #include <sys/uio.h>
50 #include <sys/mutex.h>
51 #include <sys/taskqueue.h>
52 
53 #define	splfw splimp
54 
55 STAILQ_HEAD(fw_xferlist, fw_xfer);
56 
57 struct fw_device{
58 	uint16_t dst;
59 	struct fw_eui64 eui;
60 	uint8_t speed;
61 	uint8_t maxrec;
62 	uint8_t nport;
63 	uint8_t power;
64 #define CSRROMOFF 0x400
65 #define CSRROMSIZE 0x400
66 	int rommax;	/* offset from 0xffff f000 0000 */
67 	uint32_t csrrom[CSRROMSIZE/4];
68 	int rcnt;
69 	struct firewire_comm *fc;
70 	uint32_t status;
71 #define FWDEVINIT	1
72 #define FWDEVATTACHED	2
73 #define FWDEVINVAL	3
74 	STAILQ_ENTRY(fw_device) link;
75 };
76 
77 struct firewire_softc {
78 	struct cdev *dev;
79 	struct firewire_comm *fc;
80 };
81 
82 #define FW_MAX_DMACH 0x20
83 #define FW_MAX_DEVCH FW_MAX_DMACH
84 #define FW_XFERTIMEOUT 1
85 
86 struct firewire_dev_comm {
87 	device_t dev;
88 	struct firewire_comm *fc;
89 	void (*post_busreset) (void *);
90 	void (*post_explore) (void *);
91 };
92 
93 struct tcode_info {
94 	u_char hdr_len;	/* IEEE1394 header length */
95 	u_char flag;
96 #define FWTI_REQ	(1 << 0)
97 #define FWTI_RES	(1 << 1)
98 #define FWTI_TLABEL	(1 << 2)
99 #define FWTI_BLOCK_STR	(1 << 3)
100 #define FWTI_BLOCK_ASY	(1 << 4)
101 	u_char valid_res;
102 };
103 
104 struct firewire_comm{
105 	device_t dev;
106 	device_t bdev;
107 	uint16_t busid:10,
108 		  nodeid:6;
109 	u_int mode;
110 	u_int nport;
111 	u_int speed;
112 	u_int maxrec;
113 	u_int irm;
114 	u_int max_node;
115 	u_int max_hop;
116 #define FWPHYASYST (1 << 0)
117 	uint32_t status;
118 #define	FWBUSDETACH	(-2)
119 #define	FWBUSNOTREADY	(-1)
120 #define	FWBUSRESET	0
121 #define	FWBUSINIT	1
122 #define	FWBUSCYMELECT	2
123 #define	FWBUSMGRELECT	3
124 #define	FWBUSMGRDONE	4
125 #define	FWBUSEXPLORE	5
126 #define	FWBUSPHYCONF	6
127 #define	FWBUSEXPDONE	7
128 #define	FWBUSCOMPLETION	10
129 	int nisodma;
130 	struct fw_eui64 eui;
131 	struct fw_xferq
132 		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
133 	struct fw_xferlist tlabels[0x40];
134 	u_char last_tlabel[0x40];
135 	struct mtx tlabel_lock;
136 	STAILQ_HEAD(, fw_bind) binds;
137 	STAILQ_HEAD(, fw_device) devices;
138 	u_int  sid_cnt;
139 #define CSRSIZE 0x4000
140 	uint32_t csr_arc[CSRSIZE/4];
141 #define CROMSIZE 0x400
142 	uint32_t *config_rom;
143 	struct crom_src_buf *crom_src_buf;
144 	struct crom_src *crom_src;
145 	struct crom_chunk *crom_root;
146 	struct fw_topology_map *topology_map;
147 	struct fw_speed_map *speed_map;
148 	struct callout busprobe_callout;
149 	struct callout bmr_callout;
150 	struct callout timeout_callout;
151 	struct task task_timeout;
152 	uint32_t (*cyctimer) (struct  firewire_comm *);
153 	void (*ibr) (struct firewire_comm *);
154 	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
155 	int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *);
156 	int (*irx_enable) (struct firewire_comm *, int);
157 	int (*irx_disable) (struct firewire_comm *, int);
158 	int (*itx_enable) (struct firewire_comm *, int);
159 	int (*itx_disable) (struct firewire_comm *, int);
160 	void (*timeout) (void *);
161 	void (*poll) (struct firewire_comm *, int, int);
162 	void (*set_intr) (struct firewire_comm *, int);
163 	void (*irx_post) (struct firewire_comm *, uint32_t *);
164 	void (*itx_post) (struct firewire_comm *, uint32_t *);
165 	struct tcode_info *tcode;
166 	bus_dma_tag_t dmat;
167 	struct mtx mtx;
168 	struct mtx wait_lock;
169 	struct taskqueue *taskqueue;
170 	struct proc *probe_thread;
171 };
172 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
173 
174 #define FW_GMTX(fc)		(&(fc)->mtx)
175 #define FW_GLOCK(fc)		mtx_lock(FW_GMTX(fc))
176 #define FW_GUNLOCK(fc)		mtx_unlock(FW_GMTX(fc))
177 #define FW_GLOCK_ASSERT(fc)	mtx_assert(FW_GMTX(fc), MA_OWNED)
178 
179 struct fw_xferq {
180 	int flag;
181 #define FWXFERQ_CHTAGMASK 0xff
182 #define FWXFERQ_RUNNING (1 << 8)
183 #define FWXFERQ_STREAM (1 << 9)
184 
185 #define FWXFERQ_BULK (1 << 11)
186 #define FWXFERQ_MODEMASK (7 << 10)
187 
188 #define FWXFERQ_EXTBUF (1 << 13)
189 #define FWXFERQ_OPEN (1 << 14)
190 
191 #define FWXFERQ_HANDLER (1 << 16)
192 #define FWXFERQ_WAKEUP (1 << 17)
193 	void (*start) (struct firewire_comm*);
194 	int dmach;
195 	struct fw_xferlist q;
196 	u_int queued;
197 	u_int maxq;
198 	u_int psize;
199 	struct fwdma_alloc_multi *buf;
200 	u_int bnchunk;
201 	u_int bnpacket;
202 	struct fw_bulkxfer *bulkxfer;
203 	STAILQ_HEAD(, fw_bulkxfer) stvalid;
204 	STAILQ_HEAD(, fw_bulkxfer) stfree;
205 	STAILQ_HEAD(, fw_bulkxfer) stdma;
206 	struct fw_bulkxfer *stproc;
207 	struct selinfo rsel;
208 	caddr_t sc;
209 	void (*hand) (struct fw_xferq *);
210 };
211 
212 struct fw_bulkxfer{
213 	int poffset;
214 	struct mbuf *mbuf;
215 	STAILQ_ENTRY(fw_bulkxfer) link;
216 	caddr_t start;
217 	caddr_t end;
218 	int resp;
219 };
220 
221 struct fw_bind{
222 	u_int64_t start;
223 	u_int64_t end;
224 	struct fw_xferlist xferlist;
225 	STAILQ_ENTRY(fw_bind) fclist;
226 	STAILQ_ENTRY(fw_bind) chlist;
227 	void *sc;
228 };
229 
230 struct fw_xfer{
231 	caddr_t sc;
232 	struct firewire_comm *fc;
233 	struct fw_xferq *q;
234 	struct timeval tv;
235 	int8_t resp;
236 #define FWXF_INIT	0x00
237 #define FWXF_INQ	0x01
238 #define FWXF_START	0x02
239 #define FWXF_SENT	0x04
240 #define FWXF_SENTERR	0x08
241 #define FWXF_BUSY	0x10
242 #define FWXF_RCVD	0x20
243 
244 #define FWXF_WAKE	0x80
245 	uint8_t flag;
246 	int8_t tl;
247 	void (*hand) (struct fw_xfer *);
248 	struct {
249 		struct fw_pkt hdr;
250 		uint32_t *payload;
251 		uint16_t pay_len;
252 		uint8_t spd;
253 	} send, recv;
254 	struct mbuf *mbuf;
255 	STAILQ_ENTRY(fw_xfer) link;
256 	STAILQ_ENTRY(fw_xfer) tlabel;
257 	struct malloc_type *malloc;
258 };
259 
260 struct fw_rcv_buf {
261 	struct firewire_comm *fc;
262 	struct fw_xfer *xfer;
263 	struct iovec *vec;
264 	u_int nvec;
265 	uint8_t spd;
266 };
267 
268 void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
269 void fw_rcv (struct fw_rcv_buf *);
270 void fw_xfer_unload ( struct fw_xfer*);
271 void fw_xfer_free_buf ( struct fw_xfer*);
272 void fw_xfer_free ( struct fw_xfer*);
273 struct fw_xfer *fw_xfer_alloc (struct malloc_type *);
274 struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
275 void fw_init (struct firewire_comm *);
276 int fw_tbuf_update (struct firewire_comm *, int, int);
277 int fw_rbuf_update (struct firewire_comm *, int, int);
278 int fw_bindadd (struct firewire_comm *, struct fw_bind *);
279 int fw_bindremove (struct firewire_comm *, struct fw_bind *);
280 int fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int,
281     struct firewire_comm *, void *, void (*)(struct fw_xfer *));
282 void fw_xferlist_remove (struct fw_xferlist *);
283 int fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
284 void fw_busreset (struct firewire_comm *, uint32_t);
285 uint16_t fw_crc16 (uint32_t *, uint32_t);
286 void fw_xfer_timeout (void *);
287 void fw_xfer_done (struct fw_xfer *);
288 void fw_xferwake (struct fw_xfer *);
289 int fw_xferwait (struct fw_xfer *);
290 void fw_asy_callback_free (struct fw_xfer *);
291 struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
292 struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
293 struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
294 void fw_drain_txq (struct firewire_comm *);
295 int fwdev_makedev (struct firewire_softc *);
296 int fwdev_destroydev (struct firewire_softc *);
297 void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **);
298 int fw_open_isodma(struct firewire_comm *, int);
299 
300 extern int firewire_debug;
301 extern devclass_t firewire_devclass;
302 extern int firewire_phydma_enable;
303 
304 #define		FWPRI		((PZERO+8)|PCATCH)
305 
306 #define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */)
307 
308 MALLOC_DECLARE(M_FW);
309 MALLOC_DECLARE(M_FWXFER);
310