1 /*	$NetBSD: firewirereg.h,v 1.18 2012/08/04 03:55:43 riastradh Exp $	*/
2 /*-
3  * Copyright (c) 2003 Hidetoshi Shimokawa
4  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the acknowledgement as bellow:
17  *
18  *    This product includes software developed by K. Kobayashi and H. Shimokawa
19  *
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sys/dev/firewire/firewirereg.h,v 1.50 2007/07/20 03:42:57 simokawa Exp $
36  *
37  */
38 #ifndef _FIREWIREREG_H_
39 #define _FIREWIREREG_H_
40 
41 #include <sys/selinfo.h>
42 #include <sys/uio.h>
43 
44 #define PROJECT_STR		"The NetBSD Project"
45 
46 STAILQ_HEAD(fw_xferlist, fw_xfer);
47 
48 struct fw_device {
49 	uint16_t dst;
50 	struct fw_eui64 eui;
51 	uint8_t speed;
52 	uint8_t maxrec;
53 	uint8_t nport;
54 	uint8_t power;
55 #define CSRROMOFF 0x400
56 #define CSRROMSIZE 0x400
57 	int rommax;	/* offset from 0xffff f000 0000 */
58 	uint32_t csrrom[CSRROMSIZE/4];
59 	int rcnt;
60 	struct firewire_comm *fc;
61 	uint32_t status;
62 #define FWDEVNEW	0
63 #define FWDEVINIT	1
64 #define FWDEVATTACHED	2
65 #define FWDEVINVAL	3
66 	STAILQ_ENTRY(fw_device) link;
67 	device_t dev;
68 };
69 
70 struct firewire_softc {
71 	device_t dev;
72 	SLIST_HEAD(, firewire_dev_list) devlist;
73 	void *si_drv1;
74 	struct firewire_comm *fc;
75 };
76 
77 struct firewire_dev_list {
78 	SLIST_ENTRY(firewire_dev_list) link;
79 	device_t dev;
80 	struct fw_device *fwdev;
81 };
82 
83 #define FW_MAX_DMACH 0x20
84 #define FW_MAX_DEVCH FW_MAX_DMACH
85 #define FW_XFERTIMEOUT 1
86 
87 struct firewire_dev_comm {
88 	device_t dev;
89 	struct firewire_comm *fc;
90 	void (*post_busreset) (void *);
91 	void (*post_explore) (void *);
92 };
93 
94 struct tcode_info {
95 	u_char hdr_len;	/* IEEE1394 header length */
96 	u_char flag;
97 #define FWTI_REQ	(1 << 0)
98 #define FWTI_RES	(1 << 1)
99 #define FWTI_TLABEL	(1 << 2)
100 #define FWTI_BLOCK_STR	(1 << 3)
101 #define FWTI_BLOCK_ASY	(1 << 4)
102 	u_char valid_res;
103 };
104 
105 struct firewire_comm {
106 	device_t dev;
107 	device_t bdev;
108 	uint16_t busid:10,
109 		  nodeid:6;
110 	u_int mode;
111 	u_int nport;
112 	u_int speed;
113 	u_int maxrec;
114 	u_int irm;
115 	u_int max_node;
116 	u_int max_hop;
117 #define FWPHYASYST (1 << 0)
118 	uint32_t status;
119 #define	FWBUSDETACHOK	(-3)
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 	kmutex_t tlabel_lock;
138 	STAILQ_HEAD(, fw_bind) binds;
139 	STAILQ_HEAD(, fw_device) devices;
140 	u_int  sid_cnt;
141 #define CSRSIZE 0x4000
142 	uint32_t csr_arc[CSRSIZE/4];
143 #define CROMSIZE 0x400
144 	uint32_t *config_rom;
145 	struct crom_src_buf *crom_src_buf;
146 	struct crom_src *crom_src;
147 	struct crom_chunk *crom_root;
148 	struct fw_topology_map *topology_map;
149 	struct fw_speed_map *speed_map;
150 	struct callout busprobe_callout;
151 	struct callout bmr_callout;
152 	struct callout timeout_callout;
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) (dev_t, u_long, void *, int, struct lwp *);
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) (struct firewire_comm *);
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 	const struct tcode_info *tcode;
166 	bus_dma_tag_t dmat;
167 	kmutex_t fc_mtx;
168 	kmutex_t wait_lock;
169 	kcondvar_t fc_cv;
170 	struct lwp *probe_thread;
171 };
172 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
173 
174 
175 struct fw_xferq {
176 	int flag;
177 #define FWXFERQ_CHTAGMASK 0xff
178 #define FWXFERQ_RUNNING (1 << 8)
179 #define FWXFERQ_STREAM (1 << 9)
180 
181 #define FWXFERQ_BULK (1 << 11)
182 #define FWXFERQ_MODEMASK (7 << 10)
183 
184 #define FWXFERQ_EXTBUF (1 << 13)
185 #define FWXFERQ_OPEN (1 << 14)
186 
187 #define FWXFERQ_HANDLER (1 << 16)
188 #define FWXFERQ_WAKEUP (1 << 17)
189 	void (*start) (struct firewire_comm*);
190 	int dmach;
191 	struct fw_xferlist q;
192 	u_int queued;
193 	u_int maxq;
194 	u_int psize;
195 	struct fwdma_alloc_multi *buf;
196 	u_int bnchunk;
197 	u_int bnpacket;
198 	struct fw_bulkxfer *bulkxfer;
199 	STAILQ_HEAD(, fw_bulkxfer) stvalid;
200 	STAILQ_HEAD(, fw_bulkxfer) stfree;
201 	STAILQ_HEAD(, fw_bulkxfer) stdma;
202 	struct fw_bulkxfer *stproc;
203 	struct selinfo rsel;
204 	void *sc;
205 	void (*hand) (struct fw_xferq *);
206 	kmutex_t q_mtx;
207 };
208 
209 #define FW_GMTX(fc)             (&(fc)->fc_mtx)
210 #define FW_GLOCK(fc)            fw_mtx_lock(FW_GMTX(fc))
211 #define FW_GUNLOCK(fc)          fw_mtx_unlock(FW_GMTX(fc))
212 #define FW_GLOCK_ASSERT(fc)     fw_mtx_assert(FW_GMTX(fc), MA_OWNED)
213 
214 struct fw_bulkxfer {
215 	int poffset;
216 	struct mbuf *mbuf;
217 	STAILQ_ENTRY(fw_bulkxfer) link;
218 	void *start;
219 	void *end;
220 	int resp;
221 };
222 
223 struct fw_bind {
224 	u_int64_t start;
225 	u_int64_t end;
226 	struct fw_xferlist xferlist;
227 	STAILQ_ENTRY(fw_bind) fclist;
228 	STAILQ_ENTRY(fw_bind) chlist;
229 	void *sc;
230 	kmutex_t fwb_mtx;
231 };
232 
233 struct fw_xfer {
234 	void *sc;
235 	struct firewire_comm *fc;
236 	struct fw_xferq *q;
237 	struct timeval tv;
238 	int8_t resp;
239 #define FWXF_INIT	0x00
240 #define FWXF_INQ	0x01
241 #define FWXF_START	0x02
242 #define FWXF_SENT	0x04
243 #define FWXF_SENTERR	0x08
244 #define FWXF_BUSY	0x10
245 #define FWXF_RCVD	0x20
246 
247 #define FWXF_WAKE	0x80
248 	uint8_t flag;
249 	int8_t tl;
250 	void (*hand) (struct fw_xfer *);
251 	struct {
252 		struct fw_pkt hdr;
253 		uint32_t *payload;
254 		uint16_t pay_len;
255 		uint8_t spd;
256 	} send, recv;
257 	struct mbuf *mbuf;
258 	STAILQ_ENTRY(fw_xfer) link;
259 	STAILQ_ENTRY(fw_xfer) tlabel;
260 	struct malloc_type *malloc;
261 	kcondvar_t cv;
262 };
263 
264 struct fw_rcv_buf {
265 	struct firewire_comm *fc;
266 	struct fw_xfer *xfer;
267 	struct iovec *vec;
268 	u_int nvec;
269 	uint8_t spd;
270 };
271 
272 int fw_tbuf_update (struct firewire_comm *, int, int);
273 int fw_rbuf_update (struct firewire_comm *, int, int);
274 int fwdev_makedev (struct firewire_softc *);
275 int fwdev_destroydev (struct firewire_softc *);
276 
277 struct fw_device *fw_noderesolve_nodeid(struct firewire_comm *, int);
278 struct fw_device *fw_noderesolve_eui64(struct firewire_comm *,
279 				       struct fw_eui64 *);
280 int fw_asyreq(struct firewire_comm *, int, struct fw_xfer*);
281 void fw_xferwake(struct fw_xfer *);
282 int fw_xferwait(struct fw_xfer *);
283 void fw_drain_txq(struct firewire_comm *);
284 void fw_busreset(struct firewire_comm *, uint32_t);
285 void fw_init(struct firewire_comm *);
286 void fw_destroy(struct firewire_comm *);
287 struct fw_bind *fw_bindlookup(struct firewire_comm *, uint16_t, uint32_t);
288 int fw_bindadd(struct firewire_comm *, struct fw_bind *);
289 int fw_bindremove(struct firewire_comm *, struct fw_bind *);
290 int fw_xferlist_add(struct fw_xferlist *, struct malloc_type *, int, int, int,
291 		    struct firewire_comm *, void *, void (*)(struct fw_xfer *));
292 void fw_xferlist_remove(struct fw_xferlist *);
293 struct fw_xfer *fw_xfer_alloc(struct malloc_type *);
294 struct fw_xfer *fw_xfer_alloc_buf(struct malloc_type *, int, int);
295 void fw_xfer_done(struct fw_xfer *);
296 void fw_xfer_unload(struct fw_xfer*);
297 void fw_xfer_free(struct fw_xfer*);
298 void fw_xfer_free_buf(struct fw_xfer*);
299 void fw_asy_callback_free(struct fw_xfer *);
300 void fw_sidrcv(struct firewire_comm *, uint32_t *, u_int);
301 void fw_rcv(struct fw_rcv_buf *);
302 uint16_t fw_crc16(uint32_t *, uint32_t);
303 int fw_open_isodma(struct firewire_comm *, int);
304 
305 extern int firewire_debug;
306 extern int firewire_phydma_enable;
307 
308 #define		FWPRI		((PZERO+8)|PCATCH)
309 
310 /* compatibility shim for 4.X */
311 #define bio buf
312 #define bio_bcount b_bcount
313 #define bio_cmd b_flags
314 #define bio_count b_count
315 #define bio_data b_data
316 #define bio_dev b_dev
317 #define bio_error b_error
318 #define bio_flags b_flags
319 #define bio_offset b_blkno
320 #define bio_resid b_resid
321 #define BIO_READ B_READ
322 #define BIO_WRITE B_WRITE
323 
324 #include <sys/mallocvar.h>
325 
326 MALLOC_DECLARE(M_FW);
327 
328 #endif	/* _FIREWIREREG_H_ */
329