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