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: src/sys/dev/firewire/firewirereg.h,v 1.50 2007/07/20 03:42:57 simokawa Exp $
35 *
36 */
37#ifndef _FIREWIREREG_H
38#define _FIREWIREREG_H
39#ifdef __HAIKU__
40#include <OS.h>
41#else
42#ifdef __DragonFly__
43typedef	d_thread_t fw_proc;
44#include <sys/select.h>
45#elif __FreeBSD_version >= 500000
46typedef	struct thread fw_proc;
47#include <sys/selinfo.h>
48#else
49typedef	struct proc fw_proc;
50#include <sys/select.h>
51#endif
52#endif/*__HAIKU__*/
53
54#include <sys/uio.h>
55
56#ifndef __HAIKU__
57#include <sys/mutex.h>
58#include <sys/taskqueue.h>
59#define	splfw splimp
60#else
61#include <lock.h>
62#include <dpc.h>
63#include "fwglue.h"
64#include "timer.h"
65#endif
66
67#define MAX_REQCOUNT	0xffff
68
69STAILQ_HEAD(fw_xferlist, fw_xfer);
70
71struct fw_device{
72	uint16_t dst;
73	struct fw_eui64 eui;
74	uint8_t speed;
75	uint8_t maxrec;
76	uint8_t nport;
77	uint8_t power;
78#define CSRROMOFF 0x400
79#define CSRROMSIZE 0x400
80	int rommax;	/* offset from 0xffff f000 0000 */
81	uint32_t csrrom[CSRROMSIZE/4];
82	int rcnt;
83	struct firewire_comm *fc;
84	uint32_t status;
85#define FWDEVINIT	1
86#define FWDEVATTACHED	2
87#define FWDEVINVAL	3
88	STAILQ_ENTRY(fw_device) link;
89};
90
91struct firewire_softc {
92#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
93	struct cdev *dev;
94#endif
95	struct firewire_comm *fc;
96};
97
98#define FW_MAX_DMACH 0x20
99#define FW_MAX_DEVCH FW_MAX_DMACH
100#define FW_XFERTIMEOUT 1
101
102struct firewire_dev_comm {
103#ifndef __HAIKU__
104	device_t dev;
105#endif
106	struct firewire_comm *fc;
107	void (*post_busreset) (void *);
108	void (*post_explore) (void *);
109};
110
111struct tcode_info {
112	u_char hdr_len;	/* IEEE1394 header length */
113	u_char flag;
114#define FWTI_REQ	(1 << 0)
115#define FWTI_RES	(1 << 1)
116#define FWTI_TLABEL	(1 << 2)
117#define FWTI_BLOCK_STR	(1 << 3)
118#define FWTI_BLOCK_ASY	(1 << 4)
119	u_char valid_res;
120};
121
122#ifdef __HAIKU__
123struct crom_src_buf {
124	struct crom_src	src;
125	struct crom_chunk root;
126	struct crom_chunk vendor;
127	struct crom_chunk hw;
128};//move from firewire.c to here
129
130struct firewire_notify_hooks {
131	status_t		(*device_attach)(struct firewire_softc *sc, void **cookie);
132	status_t		(*device_detach)(struct firewire_softc *sc, void *cookie);
133};
134
135struct firewire_child_info {
136	const char *child_name;
137	void *cookie;
138	struct firewire_notify_hooks notify_hooks;
139	struct firewire_child_info *link;
140};
141#endif
142
143struct firewire_comm{
144#ifndef __HAIKU__
145	device_t dev;
146	device_t bdev;
147#endif
148	uint16_t busid:10,
149		  nodeid:6;
150	u_int mode;
151	u_int nport;
152	u_int speed;
153	u_int maxrec;
154	u_int irm;
155	u_int max_node;
156	u_int max_hop;
157#define FWPHYASYST (1 << 0)
158	uint32_t status;
159#define	FWBUSDETACH	(-2)
160#define	FWBUSNOTREADY	(-1)
161#define	FWBUSRESET	0
162#define	FWBUSINIT	1
163#define	FWBUSCYMELECT	2
164#define	FWBUSMGRELECT	3
165#define	FWBUSMGRDONE	4
166#define	FWBUSEXPLORE	5
167#define	FWBUSPHYCONF	6
168#define	FWBUSEXPDONE	7
169#define	FWBUSCOMPLETION	10
170	int nisodma;
171	struct fw_eui64 eui;
172	struct fw_xferq
173		*arq, *atq, *ars, *ats, *it[FW_MAX_DMACH],*ir[FW_MAX_DMACH];
174	struct fw_xferlist tlabels[0x40];
175	u_char last_tlabel[0x40];
176#ifndef __HAIKU__
177	struct mtx tlabel_lock;
178#else
179	mutex tlabel_lock;
180#endif
181	STAILQ_HEAD(, fw_bind) binds;
182	STAILQ_HEAD(, fw_device) devices;
183	u_int  sid_cnt;
184#define CSRSIZE 0x4000
185	uint32_t csr_arc[CSRSIZE/4];
186#define CROMSIZE 0x400
187	uint32_t *config_rom;
188	struct crom_src_buf *crom_src_buf;
189	struct crom_src *crom_src;
190	struct crom_chunk *crom_root;
191	struct fw_topology_map *topology_map;
192	struct fw_speed_map *speed_map;
193#ifndef __HAIKU__
194	struct callout busprobe_callout;
195	struct callout bmr_callout;
196	struct callout timeout_callout;
197	struct task task_timeout;
198#else
199	timer_id busprobe_callout;
200	timer_id bmr_callout;
201	timer_id timeout_callout;
202#endif
203	uint32_t (*cyctimer) (struct  firewire_comm *);
204	void (*ibr) (struct firewire_comm *);
205	uint32_t (*set_bmr) (struct firewire_comm *, uint32_t);
206#ifndef __HAIKU__
207	int (*ioctl) (struct cdev *, u_long, caddr_t, int, fw_proc *);
208#else
209	int (*ioctl) (void *cookie, uint32 cmd, void *arg, size_t len);
210#endif
211	int (*irx_enable) (struct firewire_comm *, int);
212	int (*irx_disable) (struct firewire_comm *, int);
213	int (*itx_enable) (struct firewire_comm *, int);
214	int (*itx_disable) (struct firewire_comm *, int);
215	void (*timeout) (void *);
216	void (*poll) (struct firewire_comm *, int, int);
217	void (*set_intr) (struct firewire_comm *, int);
218	void (*irx_post) (struct firewire_comm *, uint32_t *);
219	void (*itx_post) (struct firewire_comm *, uint32_t *);
220	struct tcode_info *tcode;
221#ifndef __HAIKU__
222	bus_dma_tag_t dmat;
223	struct mtx mtx;
224	struct mtx wait_lock;
225	struct taskqueue *taskqueue;
226	struct proc *probe_thread;
227#else
228	mutex mtx;
229	mutex wait_lock;
230	void *taskqueue;
231	thread_id probe_thread;
232	sem_id Sem;
233	area_id crom_sid_Area;
234	struct firewire_child_info *childList;
235#endif
236};
237#define CSRARC(sc, offset) ((sc)->csr_arc[(offset)/4])
238
239#define FW_GMTX(fc)		(&(fc)->mtx)
240#define FW_GLOCK(fc)		mtx_lock(FW_GMTX(fc))
241#define FW_GUNLOCK(fc)		mtx_unlock(FW_GMTX(fc))
242#ifndef __HAIKU__
243#define FW_GLOCK_ASSERT(fc)	mtx_assert(FW_GMTX(fc), MA_OWNED)
244#else
245#define FW_GLOCK_ASSERT(fc)
246#endif
247
248struct fw_xferq {
249	int flag;
250#define FWXFERQ_CHTAGMASK 0xff
251#define FWXFERQ_RUNNING (1 << 8)
252#define FWXFERQ_STREAM (1 << 9)
253
254#define FWXFERQ_BULK (1 << 11)
255#define FWXFERQ_MODEMASK (7 << 10)
256
257#define FWXFERQ_EXTBUF (1 << 13)
258#define FWXFERQ_OPEN (1 << 14)
259
260#define FWXFERQ_HANDLER (1 << 16)
261#define FWXFERQ_WAKEUP (1 << 17)
262	void (*start) (struct firewire_comm*);
263	int dmach;
264	struct fw_xferlist q;
265	u_int queued;
266	u_int maxq;
267	u_int psize;
268	struct fwdma_alloc_multi *buf;
269	u_int bnchunk;
270	u_int bnpacket;
271	struct fw_bulkxfer *bulkxfer;
272	STAILQ_HEAD(, fw_bulkxfer) stvalid;
273	STAILQ_HEAD(, fw_bulkxfer) stfree;
274	STAILQ_HEAD(, fw_bulkxfer) stdma;
275	struct fw_bulkxfer *stproc;
276//	struct selinfo rsel;
277	caddr_t sc;
278	void (*hand) (struct fw_xferq *);
279	spinlock Spinlock;
280	sem_id Sem;
281};
282
283struct fw_bulkxfer{
284	int poffset;
285//	struct mbuf *mbuf;
286	STAILQ_ENTRY(fw_bulkxfer) link;
287	caddr_t start;
288	caddr_t end;
289	int resp;
290};
291
292struct fw_bind{
293	u_int64_t start;
294	u_int64_t end;
295	struct fw_xferlist xferlist;
296	STAILQ_ENTRY(fw_bind) fclist;
297	STAILQ_ENTRY(fw_bind) chlist;
298	void *sc;
299};
300
301struct fw_xfer{
302	caddr_t sc;
303	struct firewire_comm *fc;
304	struct fw_xferq *q;
305//	struct timeval tv;
306	bigtime_t tv;
307
308	int32_t resp;
309#define FWXF_INIT	0x00
310#define FWXF_INQ	0x01
311#define FWXF_START	0x02
312#define FWXF_SENT	0x04
313#define FWXF_SENTERR	0x08
314#define FWXF_BUSY	0x10
315#define FWXF_RCVD	0x20
316
317#define FWXF_WAKE	0x80
318	uint8_t flag;
319	int8_t tl;
320	void (*hand) (struct fw_xfer *);
321	struct send_recv{
322		struct fw_pkt hdr;
323		uint32_t *payload;
324		area_id payArea;
325		bus_addr_t bus_addr;
326		uint16_t pay_len;
327		uint8_t spd;
328	} send, recv;
329//	struct mbuf *mbuf;
330	STAILQ_ENTRY(fw_xfer) link;
331	STAILQ_ENTRY(fw_xfer) tlabel;
332//	struct malloc_type *malloc;
333	sem_id Sem;
334};
335
336struct fw_rcv_buf {
337	struct firewire_comm *fc;
338	struct fw_xfer *xfer;
339	struct iovec *vec;
340	u_int nvec;
341	uint8_t spd;
342};
343
344void fw_sidrcv (struct firewire_comm *, uint32_t *, u_int);
345void fw_rcv (struct fw_rcv_buf *);
346void fw_xfer_unload ( struct fw_xfer*);
347void fw_xfer_free_buf ( struct fw_xfer*);
348void fw_xfer_free ( struct fw_xfer*);
349struct fw_xfer *fw_xfer_alloc (void);
350struct fw_xfer *fw_xfer_alloc_buf (int, int);
351void fw_init (struct firewire_comm *);
352int fw_tbuf_update (struct firewire_comm *, int, int);
353int fw_rbuf_update (struct firewire_comm *, int, int);
354int fw_bindadd (struct firewire_comm *, struct fw_bind *);
355int fw_bindremove (struct firewire_comm *, struct fw_bind *);
356int fw_xferlist_add (struct fw_xferlist *,  int, int, int,
357    struct firewire_comm *, void *, void (*)(struct fw_xfer *));
358void fw_xferlist_remove (struct fw_xferlist *);
359int fw_asyreq (struct firewire_comm *, int, struct fw_xfer*);
360void fw_busreset (struct firewire_comm *, uint32_t);
361uint16_t fw_crc16 (uint32_t *, uint32_t);
362void fw_xfer_timeout (void *);
363void fw_xfer_done (struct fw_xfer *);
364void fw_xferwake (struct fw_xfer *);
365int fw_xferwait (struct fw_xfer *);
366void fw_asy_callback_free (struct fw_xfer *);
367struct fw_device *fw_noderesolve_nodeid (struct firewire_comm *, int);
368struct fw_device *fw_noderesolve_eui64 (struct firewire_comm *, struct fw_eui64 *);
369struct fw_bind *fw_bindlookup (struct firewire_comm *, uint16_t, uint32_t);
370void fw_drain_txq (struct firewire_comm *);
371//int fwdev_makedev (struct firewire_softc *);
372//int fwdev_destroydev (struct firewire_softc *);
373//void fwdev_clone (void *, struct ucred *, char *, int, struct cdev **);
374int fw_open_isodma(struct firewire_comm *, int);
375
376extern int firewire_debug;
377#ifndef __HAIKU__
378extern devclass_t firewire_devclass;
379#endif
380extern int firewire_phydma_enable;
381
382#ifndef __HAIKU__
383#ifdef __DragonFly__
384#define		FWPRI		PCATCH
385#else
386#define		FWPRI		((PZERO+8)|PCATCH)
387#endif
388
389#if defined(__DragonFly__) || __FreeBSD_version < 500000
390#define CALLOUT_INIT(x) callout_init(x)
391#else
392#define CALLOUT_INIT(x) callout_init(x, 1 /* mpsafe */)
393#endif
394
395#if defined(__DragonFly__) || __FreeBSD_version < 500000
396/* compatibility shim for 4.X */
397#define bio buf
398#define bio_bcount b_bcount
399#define bio_cmd b_flags
400#define bio_count b_count
401#define bio_data b_data
402#define bio_dev b_dev
403#define bio_error b_error
404#define bio_flags b_flags
405#define bio_offset b_offset
406#define bio_resid b_resid
407#define BIO_ERROR B_ERROR
408#define BIO_READ B_READ
409#define BIO_WRITE B_WRITE
410#define MIN(a,b) (((a)<(b))?(a):(b))
411#define MAX(a,b) (((a)>(b))?(a):(b))
412#endif
413
414MALLOC_DECLARE(M_FW);
415MALLOC_DECLARE(M_FWXFER);
416#endif /*__HAIKU__*/
417#ifdef __HAIKU__
418int firewire_attach(struct firewire_comm *fc, struct firewire_softc *sc);
419void firewire_detach(struct firewire_softc *sc);
420
421status_t fwohci_pci_attach(int index);
422status_t fwohci_pci_detach(int index);
423#endif
424#endif
425