xref: /freebsd/sys/dev/firewire/firewirereg.h (revision f5dc2263ab1be8a35a7e27e82103f9ccd41ae584)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2003 Hidetoshi Shimokawa
5  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the acknowledgement as bellow:
18  *
19  *    This product includes software developed by K. Kobayashi and H. Shimokawa
20  *
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 typedef	struct thread fw_proc;
39 #include <sys/selinfo.h>
40 
41 #include <sys/uio.h>
42 #include <sys/mutex.h>
43 #include <sys/taskqueue.h>
44 
45 STAILQ_HEAD(fw_xferlist, fw_xfer);
46 
47 struct fw_device {
48 	uint16_t dst;
49 	struct fw_eui64 eui;
50 	uint8_t speed;
51 	uint8_t maxrec;
52 	uint8_t nport;
53 	uint8_t power;
54 #define CSRROMOFF 0x400
55 #define CSRROMSIZE 0x400
56 	int rommax;	/* offset from 0xffff f000 0000 */
57 	uint32_t csrrom[CSRROMSIZE / 4];
58 	int rcnt;
59 	int rom_changed;
60 	struct firewire_comm *fc;
61 	uint32_t status;
62 #define FWDEVINIT	1
63 #define FWDEVATTACHED	2
64 #define FWDEVINVAL	3
65 	STAILQ_HEAD(, fw_unit) units;
66 	STAILQ_ENTRY(fw_device) link;
67 };
68 
69 struct fw_unit {
70 	device_t dev;
71 	struct fw_device *fwdev;
72 	uint32_t spec_id;
73 	uint32_t sw_version;
74 	uint32_t dir_offset;
75 	STAILQ_ENTRY(fw_unit) link;
76 };
77 
78 enum fw_child_type {
79 	FW_CHILD_BUS,
80 	FW_CHILD_UNIT,
81 };
82 
83 struct fw_child_ivars {
84 	enum fw_child_type type;
85 	union {
86 		struct firewire_comm *fc;
87 		struct fw_unit *unit;
88 	} u;
89 };
90 
91 struct firewire_softc {
92 	struct cdev *dev;
93 	struct firewire_comm *fc;
94 	int watchdog_clock;
95 };
96 
97 #define FW_MAX_DMACH 0x20
98 #define FW_MAX_DEVCH FW_MAX_DMACH
99 #define FW_XFERTIMEOUT 1
100 
101 /* 6-bit transaction label space (IEEE 1394 6.2.4.2) */
102 #define FW_NUM_TLABELS	0x40
103 #define FW_TLABEL_MASK	0x3f
104 
105 /* 6-bit node ID fields */
106 #define FW_MAX_NODES	64
107 #define FW_NODE_MASK	0x3f
108 
109 /* BUS_MGR_ID register value when no bus manager is elected */
110 #define FW_NO_BUS_MANAGER 0x3f
111 
112 struct firewire_dev_comm {
113 	device_t dev;
114 	struct firewire_comm *fc;
115 	void (*post_busreset) (void *);
116 	void (*post_explore) (void *);
117 };
118 
119 struct tcode_info {
120 	u_char hdr_len;	/* IEEE1394 header length */
121 	u_char flag;
122 #define FWTI_REQ	(1 << 0)
123 #define FWTI_RES	(1 << 1)
124 #define FWTI_TLABEL	(1 << 2)
125 #define FWTI_BLOCK_STR	(1 << 3)
126 #define FWTI_BLOCK_ASY	(1 << 4)
127 	u_char valid_res;
128 };
129 
130 struct firewire_comm {
131 	device_t dev;
132 	device_t bdev;
133 	uint16_t busid:10,
134 		 nodeid:6;
135 	u_int mode;
136 	u_int nport;
137 	u_int speed;
138 	u_int maxrec;
139 	u_int irm;
140 	u_int max_node;
141 	u_int max_hop;
142 #define FWPHYASYST (1 << 0)
143 	uint32_t status;
144 #define	FWBUSDETACH	(-2)
145 #define	FWBUSNOTREADY	(-1)
146 #define	FWBUSRESET	0
147 #define	FWBUSINIT	1
148 #define	FWBUSCYMELECT	2
149 #define	FWBUSMGRELECT	3
150 #define	FWBUSMGRDONE	4
151 #define	FWBUSEXPLORE	5
152 #define	FWBUSPHYCONF	6
153 #define	FWBUSEXPDONE	7
154 #define	FWBUSCOMPLETION	10
155 	int nisodma;
156 	struct fw_eui64 eui;
157 	struct fw_xferq
158 		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
159 	struct fw_xferlist tlabels[FW_NUM_TLABELS];
160 	u_char last_tlabel[FW_NUM_TLABELS];
161 	struct mtx tlabel_lock;
162 	STAILQ_HEAD(, fw_bind) binds;
163 	STAILQ_HEAD(, fw_device) devices;
164 	u_int  sid_cnt;
165 #define CSRSIZE 0x4000
166 	uint32_t csr_arc[CSRSIZE / 4];
167 #define CROMSIZE 0x400
168 	uint32_t *config_rom;
169 	struct crom_src_buf *crom_src_buf;
170 	struct crom_src *crom_src;
171 	struct crom_chunk *crom_root;
172 	struct fw_topology_map *topology_map;
173 	struct fw_speed_map *speed_map;
174 	struct callout busprobe_callout;
175 	struct callout bmr_callout;
176 	struct callout timeout_callout;
177 	struct task task_timeout;
178 	uint32_t (*cyctimer) (struct firewire_comm *);
179 	void (*ibr) (struct firewire_comm *);
180 	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
181 	int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *);
182 	int (*irx_enable) (struct firewire_comm *, int);
183 	int (*irx_disable) (struct firewire_comm *, int);
184 	int (*itx_enable) (struct firewire_comm *, int);
185 	int (*itx_disable) (struct firewire_comm *, int);
186 	void (*timeout) (void *);
187 	void (*poll) (struct firewire_comm *, int, int);
188 	void (*set_intr) (struct firewire_comm *, int);
189 	void (*irx_post) (struct firewire_comm *, uint32_t *);
190 	void (*itx_post) (struct firewire_comm *, uint32_t *);
191 	struct tcode_info *tcode;
192 	bus_dma_tag_t dmat;
193 	struct mtx mtx;
194 	struct mtx wait_lock;
195 	struct taskqueue *taskqueue;
196 	struct proc *probe_thread;
197 };
198 #define CSRARC(sc, offset) ((sc)->csr_arc[(offset) / 4])
199 
200 #define FW_GMTX(fc)		(&(fc)->mtx)
201 #define FW_GLOCK(fc)		mtx_lock(FW_GMTX(fc))
202 #define FW_GUNLOCK(fc)		mtx_unlock(FW_GMTX(fc))
203 #define FW_GLOCK_ASSERT(fc)	mtx_assert(FW_GMTX(fc), MA_OWNED)
204 
205 struct fw_xferq {
206 	int flag;
207 #define FWXFERQ_CHTAGMASK 0xff
208 #define FWXFERQ_RUNNING (1 << 8)
209 #define FWXFERQ_STREAM (1 << 9)
210 
211 #define FWXFERQ_BULK (1 << 11)
212 #define FWXFERQ_MODEMASK (7 << 10)
213 
214 #define FWXFERQ_EXTBUF (1 << 13)
215 #define FWXFERQ_OPEN (1 << 14)
216 
217 #define FWXFERQ_HANDLER (1 << 16)
218 #define FWXFERQ_WAKEUP (1 << 17)
219 	void (*start) (struct firewire_comm *);
220 	int dmach;
221 	struct fw_xferlist q;
222 	u_int queued;
223 	u_int maxq;
224 	u_int psize;
225 	struct fwdma_alloc_multi *buf;
226 	u_int bnchunk;
227 	u_int bnpacket;
228 	struct fw_bulkxfer *bulkxfer;
229 	STAILQ_HEAD(, fw_bulkxfer) stvalid;
230 	STAILQ_HEAD(, fw_bulkxfer) stfree;
231 	STAILQ_HEAD(, fw_bulkxfer) stdma;
232 	struct fw_bulkxfer *stproc;
233 	struct selinfo rsel;
234 	caddr_t sc;
235 	void (*hand) (struct fw_xferq *);
236 };
237 
238 struct fw_bulkxfer {
239 	int poffset;
240 	struct mbuf *mbuf;
241 	STAILQ_ENTRY(fw_bulkxfer) link;
242 	caddr_t start;
243 	caddr_t end;
244 	int resp;
245 };
246 
247 struct fw_bind {
248 	u_int64_t start;
249 	u_int64_t end;
250 	struct fw_xferlist xferlist;
251 	STAILQ_ENTRY(fw_bind) fclist;
252 	STAILQ_ENTRY(fw_bind) chlist;
253 	void *sc;
254 };
255 
256 struct fw_xfer {
257 	caddr_t sc;
258 	struct firewire_comm *fc;
259 	struct fw_xferq *q;
260 	struct timeval tv;
261 	int8_t resp;
262 #define FWXF_INIT	0x00
263 #define FWXF_INQ	0x01
264 #define FWXF_START	0x02
265 #define FWXF_SENT	0x04
266 #define FWXF_SENTERR	0x08
267 #define FWXF_BUSY	0x10
268 #define FWXF_RCVD	0x20
269 
270 #define FWXF_WAKE	0x80
271 	uint8_t flag;
272 	int8_t tl;
273 	void (*hand) (struct fw_xfer *);
274 	struct {
275 		struct fw_pkt hdr;
276 		uint32_t *payload;
277 		uint16_t pay_len;
278 		uint8_t spd;
279 	} send, recv;
280 	struct mbuf *mbuf;
281 	STAILQ_ENTRY(fw_xfer) link;
282 	STAILQ_ENTRY(fw_xfer) tlabel;
283 	struct malloc_type *malloc;
284 };
285 
286 struct fw_rcv_buf {
287 	struct firewire_comm *fc;
288 	struct fw_xfer *xfer;
289 	struct iovec *vec;
290 	u_int nvec;
291 	uint8_t spd;
292 };
293 
294 void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
295 void fw_rcv (struct fw_rcv_buf *);
296 void fw_xfer_unload (struct fw_xfer *);
297 void fw_xfer_free_buf (struct fw_xfer *);
298 void fw_xfer_free (struct fw_xfer*);
299 struct fw_xfer *fw_xfer_alloc (struct malloc_type *);
300 struct fw_xfer *fw_xfer_alloc_buf (struct malloc_type *, int, int);
301 void fw_init (struct firewire_comm *);
302 int fw_tbuf_update (struct firewire_comm *, int, int);
303 int fw_rbuf_update (struct firewire_comm *, int, int);
304 int fw_bindadd (struct firewire_comm *, struct fw_bind *);
305 int fw_bindremove (struct firewire_comm *, struct fw_bind *);
306 int fw_xferlist_add (struct fw_xferlist *, struct malloc_type *, int, int, int,
307     struct firewire_comm *, void *, void (*)(struct fw_xfer *));
308 void fw_xferlist_remove (struct fw_xferlist *);
309 int fw_asyreq (struct firewire_comm *, int, struct fw_xfer *);
310 void fw_busreset (struct firewire_comm *, uint32_t);
311 uint16_t fw_crc16 (uint32_t *, uint32_t);
312 void fw_xfer_timeout (void *);
313 void fw_xfer_done (struct fw_xfer *);
314 void fw_xferwake (struct fw_xfer *);
315 int fw_xferwait (struct fw_xfer *);
316 void fw_asy_callback_free (struct fw_xfer *);
317 struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
318 struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
319 struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
320 void fw_drain_txq (struct firewire_comm *);
321 int fwdev_makedev (struct firewire_softc *);
322 int fwdev_destroydev (struct firewire_softc *);
323 void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **);
324 int fw_open_isodma(struct firewire_comm *, int);
325 
326 extern int firewire_debug;
327 extern devclass_t firewire_devclass;
328 extern int firewire_phydma_enable;
329 
330 static __inline struct fw_child_ivars *
331 fw_get_ivars(device_t dev)
332 {
333 
334 	return ((struct fw_child_ivars *)device_get_ivars(dev));
335 }
336 
337 static __inline struct firewire_comm *
338 fw_get_comm(device_t dev)
339 {
340 	struct fw_child_ivars *iv;
341 
342 	iv = fw_get_ivars(dev);
343 	if (iv->type == FW_CHILD_BUS)
344 		return (iv->u.fc);
345 	return (iv->u.unit->fwdev->fc);
346 }
347 
348 static __inline struct fw_unit *
349 fw_get_unit(device_t dev)
350 {
351 	struct fw_child_ivars *iv;
352 
353 	iv = fw_get_ivars(dev);
354 	if (iv->type == FW_CHILD_UNIT)
355 		return (iv->u.unit);
356 	return (NULL);
357 }
358 
359 #define	FWPRI		(PWAIT | PCATCH)
360 
361 #define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */)
362 
363 MALLOC_DECLARE(M_FW);
364 MALLOC_DECLARE(M_FWXFER);
365