1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_1394_TARGETS_AV1394_IMPL_H
27 #define	_SYS_1394_TARGETS_AV1394_IMPL_H
28 
29 /*
30  * av1394 driver definitions
31  */
32 
33 #include <sys/note.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/strsun.h>
37 #include <sys/mkdev.h>
38 #include <sys/av/iec61883.h>
39 #include <sys/1394/t1394.h>
40 #include <sys/1394/targets/av1394/av1394_isoch.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * byte swapping support, stolen from SBP2
48  */
49 #ifdef _LITTLE_ENDIAN
50 #define	AV_SWAP16(data) \
51 	((((data) & 0xff) << 8) | ((data) >> 8))
52 
53 #define	AV_SWAP32(data) \
54 	(((uint32_t)AV_SWAP16((uint16_t)((data) & 0xffff)) << 16) |   \
55 	(uint32_t)AV_SWAP16((uint16_t)((data) >> 16)))
56 #else
57 #define	AV_SWAP16(data)	(data)
58 #define	AV_SWAP32(data)	(data)
59 #endif
60 
61 
62 /*
63  * double-linked list
64  */
65 typedef struct av1394_list_item_s {
66 	struct av1394_list_item_s	*i_next;
67 	struct av1394_list_item_s	*i_prev;
68 } av1394_list_item_t;
69 
70 typedef struct av1394_list_s {
71 	av1394_list_item_t	*l_head;	/* first item */
72 	av1394_list_item_t	*l_tail;	/* last item */
73 	int			l_cnt;		/* number of items */
74 } av1394_list_t;
75 
76 
77 /*
78  * queue
79  */
80 typedef struct av1394_queue_s {
81 	kmutex_t	q_mutex;	/* mutex */
82 	av1394_list_t	q_list;		/* list of mblk's */
83 	int		q_size;		/* current data size */
84 	int		q_max;		/* max data size */
85 	kcondvar_t	q_cv;		/* data cv */
86 } av1394_queue_t;
87 
88 _NOTE(MUTEX_PROTECTS_DATA(av1394_queue_s::q_mutex, av1394_queue_s))
89 
90 #define	AV1394_ENTERQ(q)	mutex_enter(&(q)->q_mutex)
91 #define	AV1394_LEAVEQ(q)	mutex_exit(&(q)->q_mutex)
92 
93 
94 /*
95  * asynchronous module definitions
96  *
97  *
98  * command structure
99  */
100 typedef struct av1394_fcp_cmd_s {
101 	cmd1394_cmd_t	*fc_cmd;	/* 1394 command */
102 	boolean_t	fc_busy;	/* command is in use */
103 	kcondvar_t	fc_busy_cv;	/* busy cv */
104 	boolean_t	fc_xmit;	/* transmit in progress */
105 	kcondvar_t	fc_xmit_cv;	/* transmit completion cv */
106 } av1394_fcp_cmd_t;
107 
108 /*
109  * per-instance FCP structure
110  */
111 typedef struct av1394_fcp_s {
112 	av1394_fcp_cmd_t	fcp_cmd;	/* outgoing FCP command */
113 	av1394_fcp_cmd_t	fcp_resp;	/* outgoing FCP response */
114 } av1394_fcp_t;
115 
116 enum {
117 	AV1394_FCP_ARQ_LEN_MAX	 = 0x200	/* maximum FCP ARQ length */
118 };
119 
120 
121 /*
122  * configuration ROM
123  */
124 #define	AV1394_CFGROM_INFO_LEN_ADDR	(IEEE1394_CONFIG_ROM_ADDR + 0x00)
125 #define	AV1394_CFGROM_BUS_NAME_ADDR	(IEEE1394_CONFIG_ROM_ADDR + 0x04)
126 #define	AV1394_CFGROM_EUI64_HI_ADDR	(IEEE1394_CONFIG_ROM_ADDR + 0x0c)
127 #define	AV1394_CFGROM_EUI64_LO_ADDR	(IEEE1394_CONFIG_ROM_ADDR + 0x10)
128 
129 /* offsets in quadlets */
130 #define	AV1394_CFGROM_BUS_NAME_OFF	1
131 #define	AV1394_CFGROM_EUI64_HI_OFF	3
132 #define	AV1394_CFGROM_EUI64_LO_OFF	4
133 
134 typedef struct av1394_cfgrom_text_leaf_s {
135 	uint64_t	tl_addr;	/* leaf entry address */
136 	uint32_t	tl_desc_entry;	/* entry described by this leaf */
137 } av1394_cfgrom_text_leaf_t;
138 
139 typedef struct av1394_cfgrom_parsed_dir_s {
140 	av1394_cfgrom_text_leaf_t *pd_tl;	/* text leaf array */
141 	int			pd_tl_size;	/* total # of array entries */
142 	int			pd_tl_next;	/* first unused entry index */
143 } av1394_cfgrom_parsed_dir_t;
144 
145 typedef struct av1394_cfgrom_parse_arg_s {
146 	int			pa_depth;	/* parser depth */
147 	uint32_t		pa_desc_entry;	/* described entry */
148 	uint8_t			pa_parent_k;	/* parent entry's key value */
149 	uint64_t		pa_addr;	/* directory address */
150 	uint16_t		pa_len;		/* directory length */
151 	av1394_cfgrom_parsed_dir_t *pa_dir;	/* current directory */
152 } av1394_cfgrom_parse_arg_t;
153 
154 enum {
155 	AV1394_CFGROM_PARSE_MAX_DEPTH	= 5	/* maximum parse depth */
156 };
157 
158 typedef struct av1394_cfgrom_s {
159 	krwlock_t		cr_rwlock;	/* structure lock */
160 	boolean_t		cr_parsed;	/* node ConfigROM was parsed */
161 	av1394_cfgrom_parsed_dir_t cr_root_dir;	/* root directory */
162 	av1394_cfgrom_parsed_dir_t cr_unit_dir;	/* unit directory */
163 } av1394_cfgrom_t;
164 
165 
166 /*
167  * async command
168  */
169 typedef struct av1394_async_cmd_s {
170 	kmutex_t	ac_mutex;
171 	boolean_t	ac_busy;
172 	kcondvar_t	ac_cv;
173 	cmd1394_cmd_t	*ac_cmd;
174 } av1394_async_cmd_t;
175 
176 /*
177  * per-instance soft state structure
178  */
179 typedef struct av1394_async_s {
180 	kmutex_t		a_mutex;	/* structure mutex */
181 	int			a_nopen;	/* number of opens */
182 	int			a_oflag;	/* open flags */
183 	t1394_targetinfo_t	a_targetinfo;	/* target info */
184 	uint_t			a_bus_generation; /* bus generation */
185 	av1394_fcp_t		a_fcp;		/* FCP module */
186 	av1394_cfgrom_t		a_cfgrom;	/* config ROM module */
187 	av1394_queue_t		a_rq;		/* read queue */
188 	struct pollhead		a_pollhead;	/* poll(2) support */
189 	short			a_pollevents;	/* polled events */
190 } av1394_async_t;
191 
192 _NOTE(MUTEX_PROTECTS_DATA(av1394_async_s::a_mutex, av1394_async_s))
193 _NOTE(DATA_READABLE_WITHOUT_LOCK(av1394_async_s::{
194 	a_oflag
195 }))
196 
197 
198 /* we use special message types for the read queue */
199 enum {
200 	AV1394_M_FCP_RESP	= 0x01,	/* FCP response */
201 	AV1394_M_FCP_CMD	= 0x02,	/* FCP command */
202 	AV1394_M_BUS_RESET	= 0x03,	/* bus reset event */
203 	/*
204 	 * For efficiency, we only store 1394 request data on the read queue.
205 	 * ARQ headers (iec61883_arq_t) are generated when an application
206 	 * calls read(2). Because applications may read header separately
207 	 * from the data, we need to mark each mblk when its header was read
208 	 * but not the data - the following flag is used for this purpose.
209 	 */
210 	AV1394_M_NOHDR		= 0x80
211 };
212 
213 #define	AV1394_DBTYPE(bp)	(DB_TYPE(bp) & ~AV1394_M_NOHDR)
214 #define	AV1394_MARK_NOHDR(bp)	(DB_TYPE(bp) |= AV1394_M_NOHDR)
215 #define	AV1394_IS_NOHDR(bp)	(DB_TYPE(bp) & AV1394_M_NOHDR)
216 
217 
218 /*
219  * device state:
220  *
221  *                     AV1394_DEV_DISCONNECTED
222  *                     |                |   ^
223  *                     |                |   |
224  *                  detach       reconnect disconnect
225  *                     |                |   |
226  *                     v                v   |
227  *       AV1394_DEV_INIT ----attach---> AV1394_DEV_ONLINE
228  *      (initial state)  <---detach---  |   ^
229  *                                      |   |
230  *                             cpr suspend cpr resume
231  *                                      |   |
232  *                                      v   |
233  *                        AV1394_DEV_SUSPENDED
234  */
235 typedef enum {
236 	AV1394_DEV_INIT		= 0,
237 	AV1394_DEV_ONLINE,
238 	AV1394_DEV_SUSPENDED,
239 	AV1394_DEV_DISCONNECTED
240 } av1394_dev_state_t;
241 
242 /*
243  * per-instance soft state structure
244  */
245 typedef struct av1394_inst_s {
246 	kmutex_t		av_mutex;	/* structure mutex */
247 	dev_info_t		*av_dip;	/* device information */
248 	int			av_instance;	/* instance number */
249 	av1394_dev_state_t	av_dev_state;	/* device state */
250 	av1394_dev_state_t	av_prev_dev_state; /* previous device state */
251 	t1394_attachinfo_t	av_attachinfo;	/* 1394 attach info */
252 	t1394_handle_t		av_t1394_hdl;	/* 1394 handle */
253 	av1394_async_t		av_a;		/* asynchronous module */
254 	av1394_isoch_t		av_i;		/* isochronous module */
255 	ddi_callback_id_t	av_reset_cb;	/* reset event cb id */
256 	ddi_callback_id_t	av_remove_cb; 	/* remove event cb id */
257 	ddi_callback_id_t	av_insert_cb;	/* insert event cb id */
258 } av1394_inst_t;
259 
260 _NOTE(MUTEX_PROTECTS_DATA(av1394_inst_s::av_mutex, av1394_inst_s::{
261 	av_dip
262 	av_instance
263 	av_attachinfo
264 	av_t1394_hdl
265 }))
266 /* these are set during attach (single-threaded) and don't change afterwards */
267 _NOTE(DATA_READABLE_WITHOUT_LOCK(av1394_inst_s::{
268 	av_dip
269 	av_instance
270 	av_attachinfo
271 	av_t1394_hdl
272 }))
273 
274 _NOTE(SCHEME_PROTECTS_DATA("one per call", msgb datab cmd1394_cmd
275 	iec61883_arq_t iec61883_isoch_init_t iec61883_plug_init_t))
276 
277 /*
278  * minor <-> instance mapping
279  */
280 #define	AV1394_MINOR_TYPE_MASK		(1 << (NBITSMINOR32 - 1))
281 #define	AV1394_ISOCH_INST2MINOR(inst)	(inst)
282 #define	AV1394_ASYNC_INST2MINOR(inst)	((inst) | AV1394_MINOR_TYPE_MASK)
283 #define	AV1394_DEV_IS_ISOCH(dev)	\
284 		((getminor(dev) & AV1394_MINOR_TYPE_MASK) == 0)
285 #define	AV1394_DEV_IS_ASYNC(dev)	\
286 		((getminor(dev) & AV1394_MINOR_TYPE_MASK) != 0)
287 #define	AV1394_DEV2INST(dev)		\
288 		((getminor(dev)) & ~AV1394_MINOR_TYPE_MASK)
289 
290 /* misc constants */
291 enum {
292 	AV1394_CLEANUP_LEVEL_MAX	= 256
293 };
294 
295 /* current interface version */
296 #define	AV1394_IEC61883_VER		IEC61883_V1_0
297 
298 /* misc */
299 #define	NELEM(a)	(sizeof (a) / sizeof (*(a)))
300 
301 
302 /* double-linked list */
303 void	av1394_list_init(av1394_list_t *lp);
304 void	*av1394_list_head(av1394_list_t *lp);
305 void	av1394_list_put_tail(av1394_list_t *lp, void *item);
306 void	av1394_list_put_head(av1394_list_t *lp, void *item);
307 void	*av1394_list_get_head(av1394_list_t *lp);
308 
309 /* queue */
310 void	av1394_initq(av1394_queue_t *q, ddi_iblock_cookie_t ibc, int max);
311 void	av1394_destroyq(av1394_queue_t *q);
312 void	av1394_setmaxq(av1394_queue_t *q, int max);
313 int	av1394_getmaxq(av1394_queue_t *q);
314 void	av1394_flushq(av1394_queue_t *q);
315 int	av1394_putq(av1394_queue_t *q, mblk_t *bp);
316 int	av1394_putbq(av1394_queue_t *q, mblk_t *bp);
317 mblk_t	*av1394_getq(av1394_queue_t *q);
318 mblk_t	*av1394_peekq(av1394_queue_t *q);
319 mblk_t	*av1394_peekq_locked(av1394_queue_t *q);
320 int	av1394_qwait_sig(av1394_queue_t *q);
321 
322 /* FCP */
323 int	av1394_fcp_attach(av1394_inst_t *);
324 void	av1394_fcp_detach(av1394_inst_t *);
325 int	av1394_fcp_open(av1394_inst_t *, int);
326 int	av1394_fcp_close(av1394_inst_t *, int);
327 int	av1394_fcp_write(av1394_inst_t *, iec61883_arq_t *, struct uio *);
328 
329 /* config ROM */
330 int	av1394_cfgrom_init(av1394_inst_t *);
331 void	av1394_cfgrom_fini(av1394_inst_t *);
332 void	av1394_cfgrom_close(av1394_inst_t *);
333 int	av1394_ioctl_node_get_bus_name(av1394_inst_t *, void *, int);
334 int	av1394_ioctl_node_get_uid(av1394_inst_t *, void *, int);
335 int	av1394_ioctl_node_get_text_leaf(av1394_inst_t *, void *, int);
336 
337 /* async module */
338 int	av1394_async_attach(av1394_inst_t *);
339 void	av1394_async_detach(av1394_inst_t *);
340 int	av1394_async_cpr_suspend(av1394_inst_t *);
341 int	av1394_async_cpr_resume(av1394_inst_t *);
342 void	av1394_async_bus_reset(av1394_inst_t *);
343 void	av1394_async_disconnect(av1394_inst_t *);
344 void	av1394_async_reconnect(av1394_inst_t *);
345 int	av1394_async_open(av1394_inst_t *, int);
346 int	av1394_async_close(av1394_inst_t *, int);
347 int	av1394_async_read(av1394_inst_t *, struct uio *);
348 int	av1394_async_write(av1394_inst_t *, struct uio *);
349 int	av1394_async_ioctl(av1394_inst_t *, int, intptr_t, int, int *);
350 int	av1394_async_poll(av1394_inst_t *, short, int, short *,
351 		struct pollhead **);
352 void	av1394_async_putq_rq(av1394_inst_t *, mblk_t *);
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif /* _SYS_1394_TARGETS_AV1394_IMPL_H */
359