xref: /illumos-gate/usr/src/uts/common/pcmcia/sys/cs_priv.h (revision dd72704bd9e794056c558153663c739e2012d721)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _CS_PRIV_H
28 #define	_CS_PRIV_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 /*
35  * PCMCIA Card Services private header file
36  */
37 
38 /*
39  * typedef for function pointers to quiet lint and cc -v
40  */
41 typedef	int32_t (f_t)(int32_t, ...);	/* for lint - cc -v quieting */
42 
43 /*
44  * Magic number we use when talking with Socket Services
45  */
46 #define	CS_MAGIC	PCCS_MAGIC
47 
48 /*
49  * Make the calls to SocketServices and the CIS Parser look like
50  *	function calls.
51  */
52 #define	SocketServices	(*cs_socket_services)
53 #define	CIS_PARSER	(*cis_parser)
54 
55 /*
56  * CIS_DEFAULT_SPEED is the default speed to use to read the CIS
57  *	in AM space.  It is expressed in nS.
58  */
59 #define	CIS_DEFAULT_SPEED	250
60 
61 /*
62  * This is the IO window speed.
63  */
64 #define	IO_WIN_SPEED		250
65 
66 /*
67  * Flags to support various internal first/next functions. All of
68  *	these must be within CIS_GET_LTUPLE_OPMASK which is defined
69  *	in the cis.h file. Values outside this mask range are used
70  *	internally by the CIS parser.
71  */
72 #define	CS_GET_FIRST_FLAG	0x0001
73 #define	CS_GET_NEXT_FLAG	0x0002
74 
75 /*
76  * Macros to manipulate bits - only does up to uint32_t size
77  */
78 #define	CS_BIT_WORDSIZE		(sizeof (uint32_t))
79 
80 #define	CS_BIT_GET(val, bit)	\
81 			((uint32_t)(val) & (uint32_t)(1<<(uint32_t)(bit)))
82 
83 #define	CS_BIT_CLEAR(val, bit)	((val) &= (uint32_t)~(1<<(uint32_t)(bit)))
84 
85 #define	CS_BIT_SET(val, bit)	\
86 			((uint32_t)(val) |= (uint32_t)(1<<(uint32_t)(bit)))
87 
88 /*
89  * Minimum time to wait after socket reset before we are allowed to
90  *	access the card.  The PCMCIA specification says at least 20mS
91  *	must elapse from the time that the card is reset until the
92  *	first access of any kind can be made to the card. This time
93  *	value is expressed in mS.
94  */
95 #define	RESET_TIMEOUT_TIME	180
96 
97 /*
98  * Maximum time to wait for card ready after resetting the socket.
99  *	We wait for card ready a maximum of 20 seconds after card
100  *	reset before considering that we have an error condition.
101  * XXX - what does PCMCIA specify as the max time here??
102  */
103 #define	READY_TIMEOUT_TIME	(drv_usectohz(20000000))
104 
105 /*
106  * Time between periodically kicking the soft interrupt handler.
107  */
108 #define	SOFTINT_TIMEOUT_TIME	(drv_usectohz(2000000))
109 
110 /*
111  * Various delays are necessary when switching the card and socket
112  *	between IO and memory modes. All delays are in mS.
113  *
114  *  cs_request_configuration parameters:
115  *    CS_RC1_DELAY - delay between writing COR and switching socket
116  *			to IO mode
117  *    CS_RC2_DELAY - delay after switching socket to IO mode
118  *
119  *  cs_release_configuration parameters:
120  *	CS_RQ_DELAY - amount of time that the RESET bit in the COR is
121  *			held asserted
122  */
123 #define	CS_RC1_DELAY		20	/* COR->IO delay in mS */
124 #define	CS_RC2_DELAY		300	/* post-COR delay in mS */
125 #define	CS_RQ_DELAY		100	/* COR(RESET) delay in mS */
126 
127 /*
128  * Handy macro to do untimeout.
129  */
130 #define	UNTIMEOUT(id)		\
131 	if ((id)) {		\
132 	    (void) untimeout((id));	\
133 	    (id) = 0;		\
134 	}
135 
136 /*
137  * Macros to enter/exit event thread mutex
138  */
139 #define	EVENT_THREAD_MUTEX_ENTER(acq, sp)		\
140 	acq = !MUTEX_HELD(&sp->client_lock);		\
141 	if (acq)					\
142 	    mutex_enter(&sp->client_lock);
143 #define	EVENT_THREAD_MUTEX_EXIT(acq, sp)		\
144 	if (acq)					\
145 	    mutex_exit(&sp->client_lock);
146 
147 /*
148  * cisregister_t structure is used to support the CISRegister
149  *	and the CISUnregister function calls
150  */
151 typedef struct cisregister_t {
152 	uint32_t		cis_magic;
153 	uint32_t		cis_version;
154 	void *			(*cis_parser)(int32_t function, ...);
155 	cistpl_callout_t	*cistpl_std_callout; /* standard callout list */
156 } cisregister_t;
157 
158 /*
159  * These two defines are to support CISRegister and CISUnregister
160  */
161 #define	CIS_MAGIC	0x20434953
162 #define	CIS_VERSION	_VERSION(0, 1)
163 
164 /*
165  * CS_MAX_CIS defines the number of CIS chains that we hang off the per-socket
166  *	structure.
167  *
168  * CS_GLOBAL_CIS defines the index where the CIS parser puts the first CIS list
169  *	for a single-function card or the global CIS list for a multi-function
170  *	card.
171  *
172  * CS_MAX_CIS is one greater than CIS_MAX_FUNCTIONS since the CIS parser
173  *	puts the global CIS chain on the CS_GLOBAL_CIS function index as
174  * 	follows:
175  *
176  *	For single-function cards:
177  *	    sp->cis[0] - CIS chain
178  *	    sp->cis[1..(CIS_MAX_FUNCTIONS - 1)] - not used
179  *	    sp->cis[CS_GLOBAL_CIS] - not used
180  *
181  *	For multi-function cards:
182  *	    sp->cis[0..(CIS_MAX_FUNCTIONS - 1)] - global CIS chain followed
183  *					by per-function CIS chain
184  *	    sp->cis[CS_GLOBAL_CIS] - global CIS chain
185  */
186 #define	CS_MAX_CIS	(CIS_MAX_FUNCTIONS + 1)
187 #define	CS_GLOBAL_CIS	CIS_MAX_FUNCTIONS
188 
189 /*
190  * CS_SS_CLIENT_HANDLE is a special client handle that Socket Services gets
191  *	when it registers with RegisterClient.
192  */
193 #define	CS_SS_CLIENT_HANDLE	0x00000000
194 
195 /*
196  * Client handle, socket number, function number and socket pointer
197  *	macros. The client handle encoding is private to Card Services,
198  *	and external modules should not use these macros to manipulate
199  *	client handles.
200  *
201  *	The encoding of the client handle is:
202  *
203  *		xxxxxfff | xsssssss | cccccccc | cccccccc
204  *
205  *	f - function number bit
206  *	s - socket number bit
207  *	c - client number bit
208  *	x - don't care bits
209  */
210 #define	CLIENT_HANDLE_IS_SS(ch)		(!GET_CLIENT_MINOR((ch)))
211 #define	CS_MAX_SOCKETS_MASK		(PCMCIA_MAX_SOCKETS - 1)
212 #define	CS_MAX_FUNCTIONS_MASK		(CIS_MAX_FUNCTIONS - 1)
213 #define	CS_MAX_CLIENTS_MASK		0x0ffff
214 #define	CS_MAX_CLIENTS			(CS_MAX_CLIENTS_MASK - 2)
215 #define	MAKE_CLIENT_HANDLE(s, f, c)	((((f)&CS_MAX_FUNCTIONS_MASK)<<24) | \
216 					    (((s)&CS_MAX_SOCKETS_MASK)<<16) | \
217 					    ((c)&CS_MAX_CLIENTS_MASK))
218 #define	GET_CLIENT_SOCKET(ch)		(((ch)>>16)&CS_MAX_SOCKETS_MASK)
219 #define	GET_CLIENT_FUNCTION(ch)		(((ch)>>24)&CS_MAX_FUNCTIONS_MASK)
220 #define	GET_CLIENT_MINOR(ch)		((ch)&CS_MAX_CLIENTS_MASK)
221 
222 /*
223  * Socket number macros. These are used by Socket Services, CSI
224  *	drivers and the "super-client" driver to specify which
225  *	socket and function number on that socket they wish to
226  *	manipulate. This socket number encoding is typically passed
227  *	to various Card Services functions by these drivers.
228  *
229  *	The encoding of the socket number is:
230  *
231  *		xxxxxxxx | xxxxgfff | xxxxxxxx | xsssssss
232  *
233  *	g - global CIS bit
234  *	f - function number bit
235  *	s - socket number bit
236  *	x - don't care bits
237  */
238 #define	CS_GET_SOCKET_NUMBER(s)		((s)&CS_MAX_SOCKETS_MASK)
239 #define	CS_GET_FUNCTION_NUMBER(s)	(((s)>>16)&(CS_MAX_FUNCTIONS_MASK | \
240 							CIS_MAX_FUNCTIONS))
241 #define	CS_SET_SOCKET_NUMBER(s)		((s)&CS_MAX_SOCKETS_MASK)
242 #define	CS_SET_FUNCTION_NUMBER(f)	(((f)&(CS_MAX_FUNCTIONS_MASK | \
243 						CIS_MAX_FUNCTIONS))<<16)
244 #define	CS_MAKE_SOCKET_NUMBER(s, f)	(CS_SET_SOCKET_NUMBER(s) | \
245 						CS_SET_FUNCTION_NUMBER(f))
246 
247 /*
248  * DIP2SOCKET_NUM(dip) - this macro gets the PCM_DEV_SOCKET property from
249  *	the passed dip.  If the property can't be found, then the default
250  *	value of cs_globals.max_socket_num is returned.
251  */
252 #define	DIP2SOCKET_NUM(dip)		ddi_getprop(DDI_DEV_T_NONE, dip,\
253 						(DDI_PROP_CANSLEEP |	\
254 							DDI_PROP_NOTPROM), \
255 						PCM_DEV_SOCKET,		\
256 						cs_globals.max_socket_num)
257 
258 /*
259  * Range checking macros
260  *
261  * CHECK_SOCKET_NUM(socket_number, max_sockets) returns 1 if
262  *	socket_number is in range
263  */
264 #define	CHECK_SOCKET_NUM(sn, ms)	(((sn) >= (ms))?0:1)
265 
266 /*
267  * window macros
268  *
269  * These all expect that the window has been validated as a valid
270  *	window (i.e. CW_WINDOW_VALID is set in window state)
271  *
272  * Note that WINDOW_FOR_SOCKET expects a socket mask for the wsm
273  *	parameter (this is a socket_enum_t type, and NOT just a
274  *	plain old uint32_t)
275  */
276 #define	WINDOW_FOR_SOCKET(wsm, sn)	((wsm)[sn/PR_WORDSIZE] & \
277 						(1 << ((sn) & PR_MASK)))
278 #define	WINDOW_AVAILABLE_FOR_MEM(cwp)	(!(cwp->state & CW_WIN_IN_USE))
279 #define	WINDOW_AVAILABLE_FOR_IO(cwp)	\
280 		(!(cwp->state & (CW_CIS | CW_MEM | CW_ALLOCATED)))
281 
282 /*
283  * IO Base and NumPorts address frobnitz macros
284  */
285 #define	IOADDR_FROBNITZ(Base, IOAddrLines)	(Base&((1<<IOAddrLines)-1))
286 #define	IONUMPORTS_FROBNITZ(np)			(((np)&1)?((np)+1):(np))
287 
288 /*
289  * Structure that contains offsets to the card's configuration registers
290  *	as well as copies of the data written to them in RequestConfiguration.
291  *	We use an offset per register approach since not all cards have
292  *	all registers implemented, and by specifying a NULL register offset,
293  *	we know not to try to access that register.
294  */
295 typedef struct config_regs_t {
296 	cfg_regs_t	cor;		/* Configuration Option Register */
297 	uint32_t	cor_p;
298 	cfg_regs_t	ccsr;		/* Configuration and Status Register */
299 	uint32_t	ccsr_p;
300 	cfg_regs_t	prr;		/* Pin Replacement Register */
301 	uint32_t	prr_p;
302 	cfg_regs_t	scr;		/* Socket and Copy Register */
303 	uint32_t	scr_p;
304 	cfg_regs_t	exstat;		/* Extended Status Register */
305 	uint32_t	exstat_p;
306 	cfg_regs_t	iobase0;	/* IO Base 0 Register */
307 	uint32_t	iobase0_p;
308 	cfg_regs_t	iobase1;	/* IO Base 1 Register */
309 	uint32_t	iobase1_p;
310 	cfg_regs_t	iobase2;	/* IO Base 2 Register */
311 	uint32_t	iobase2_p;
312 	cfg_regs_t	iobase3;	/* IO Base 3 Register */
313 	uint32_t	iobase3_p;
314 	cfg_regs_t	iolimit;	/* IO Limit Register */
315 	uint32_t	iolimit_p;
316 } config_regs_t;
317 
318 /*
319  * Macro to make calling the client's event handler look like a function.
320  */
321 #define	CLIENT_EVENT_CALLBACK(cp, event, pri)		\
322 	    (cp)->event_callback_handler(event, pri,	\
323 			&(cp)->event_callback_args)
324 
325 /*
326  * Macro to return event in PRR - this also clears the changed bit if
327  *	the event occured.
328  */
329 #define	PRR_EVENT(prrx, pe, ps, ce, re)	\
330 	if (prrx & pe) {		\
331 	    if (prrx & ps)		\
332 		(re) |= ce;		\
333 	    prrx &= ~pe;		\
334 	    prrx |= ps;			\
335 	}
336 
337 /*
338  * io_alloc_t struct used to keep track of a client's IO window allocation
339  */
340 typedef struct io_alloc_t {
341 	uint32_t	Window1;	/* allocated IO window no. for set #1 */
342 	baseaddru_t	BasePort1;	/* 1st IO range base address or port */
343 	uint32_t	NumPorts1;	/* 1st IO range no. contiguous ports */
344 	uint32_t	Attributes1;	/* 1st IO range attributes */
345 	uint32_t	Window2;	/* allocated IO window no. for set #2 */
346 	baseaddru_t	BasePort2;	/* s2nd IO range base address or port */
347 	uint32_t	NumPorts2;	/* 2nd IO range no. contiguous ports */
348 	uint32_t	Attributes2;	/* second IO range attributes */
349 	uint32_t	IOAddrLines;	/* number of IO address lines decoded */
350 } io_alloc_t;
351 
352 /*
353  * irq_alloc_t structure used to keep track of a client's IRQ allocation
354  */
355 typedef struct irq_alloc_t {
356 	uint32_t	Attributes;	/* IRQ attribute flags */
357 	uint32_t	irq;		/* assigned IRQ number */
358 	uint32_t	handler_id;	/* IRQ handler ID for this IRQ */
359 	f_t		*irq_handler;
360 	void		*irq_handler_arg1;
361 	void		*irq_handler_arg2;
362 } irq_alloc_t;
363 
364 /*
365  * The client data structure
366  */
367 typedef struct client_t {
368 	client_handle_t	client_handle;	/* this client's client handle */
369 	unsigned	flags;		/* client flags */
370 	/* resource control */
371 	uint32_t	memwin_count;	/* number of mem windows allocated */
372 	io_alloc_t	io_alloc;	/* IO resource allocations */
373 	irq_alloc_t	irq_alloc;	/* IRQ resource allocations */
374 	/* event support */
375 	uint32_t	event_mask;	/* client event mask */
376 	uint32_t	global_mask;	/* client global event mask */
377 	uint32_t	events;		/* current events pending */
378 	uint32_t	pending_events;	/* events pending in RegisterClient */
379 	csfunction_t	*event_callback_handler;
380 	event_callback_args_t	event_callback_args;
381 	/* config registers support */
382 	config_regs_t	config_regs;	/* pointers to config registers */
383 	uint32_t	config_regs_offset; /* offset from start of AM */
384 	unsigned	pin;		/* valid bits in PRR */
385 	uint32_t	present;	/* which config registers present */
386 	/* DDI support */
387 	dev_info_t	*dip;		/* this client's dip */
388 	char		*driver_name;	/* client's driver name */
389 	int32_t		instance;	/* client's driver instance */
390 	/* list control */
391 	struct client_t	*next;		/* next client pointer */
392 	struct client_t	*prev;		/* previous client pointer */
393 } client_t;
394 
395 /*
396  * Flags for client structure - note that we share the client_t->flags
397  *	member with the definitions in cs.h that are used by the
398  *	RegisterClient function.
399  *
400  * We can start our flags from 0x00001000 and on up.
401  */
402 #define	REQ_CONFIGURATION_DONE	0x00001000	/* RequestConfiguration done */
403 #define	REQ_SOCKET_MASK_DONE	0x00002000	/* RequestSocketMask done */
404 #define	REQ_IO_DONE		0x00004000	/* RequestIO done */
405 #define	REQ_IRQ_DONE		0x00008000	/* RequestIRQ done */
406 #define	CLIENT_SUPER_CLIENT	0x00010000	/* "super-client" client */
407 #define	CLIENT_CSI_CLIENT	0x00020000	/* CSI client */
408 #define	CLIENT_CARD_INSERTED	0x00100000	/* current card for client */
409 #define	CLIENT_SENT_INSERTION	0x00200000	/* send CARD_INSERTION */
410 #define	CLIENT_MTD_IN_PROGRESS	0x01000000	/* MTD op in progress */
411 #define	CLIENT_IO_ALLOCATED	0x02000000	/* IO resources allocated */
412 #define	CLIENT_IRQ_ALLOCATED	0x04000000	/* IRQ resources allocated */
413 #define	CLIENT_WIN_ALLOCATED	0x08000000	/* window resources allocated */
414 
415 #ifdef	USE_IOMMAP_WINDOW
416 /*
417  * io_mmap_window_t structure that describes the memory-mapped IO
418  *	window on this socket
419  */
420 typedef struct io_mmap_window_t {
421 	uint32_t		flags;	/* window flags */
422 	uint32_t		number;	/* IO window number */
423 	uint32_t		size;	/* size of mapped IO window */
424 	ddi_acc_handle_t	handle;	/* window mapped base address */
425 	uint32_t		count;	/* referance count */
426 } io_mmap_window_t;
427 #endif	/* USE_IOMMAP_WINDOW */
428 
429 /*
430  * cis_info_t structure used to hold per-socket CIS information
431  */
432 typedef struct cis_info_t {
433 	uint32_t	flags;		/* CIS-specific flags */
434 	cistpl_t	*cis;		/* CIS linked lists */
435 	uint32_t	nchains;	/* number of tuple chains in CIS */
436 	uint32_t	ntuples;	/* number of tuples in CIS */
437 } cis_info_t;
438 
439 /*
440  * cs_adapter_t structure used to hold per-socket
441  *	adapter-specific info
442  */
443 typedef struct cs_adapter_t {
444 	uint32_t	flags;		/* adapter flags */
445 	char		name[MODMAXNAMELEN]; /* adapter module name */
446 	uint32_t	major;		/* adapter major number */
447 	uint32_t	minor;		/* adapter minor number */
448 	uint32_t	instance;	/* instance number of this adapter */
449 	uint32_t	number;		/* canonical adapter number */
450 	uint32_t	num_sockets;	/* # sockets on this adapter */
451 	uint32_t	first_socket;	/* first socket # on this adapter */
452 } cs_adapter_t;
453 
454 /*
455  * The per-socket structure.
456  */
457 typedef struct cs_socket_t {
458 	unsigned	socket_num;	/* socket number */
459 	uint32_t	flags;		/* socket flags */
460 	uint32_t	init_state;	/* cs_init state */
461 	cs_adapter_t	adapter;	/* adapter info */
462 	/* socket thread control and status */
463 	kthread_t	*event_thread;	/* per-socket work thread */
464 	uint32_t	thread_state;	/* socket thread state flags */
465 	kmutex_t	lock;		/* protects events and clients */
466 	kcondvar_t	thread_cv;	/* event handling synchronization */
467 	kcondvar_t	caller_cv;	/* event handling synchronization */
468 	kcondvar_t	reset_cv;	/* for use after card RESET */
469 	uint32_t	events;		/* socket events */
470 	uint32_t	event_mask;	/* socket event mask */
471 	ddi_softintr_t	softint_id;	/* soft interrupt handler ID */
472 	timeout_id_t	rdybsy_tmo_id;	/* timer ID for READY/BUSY timer */
473 	ddi_iblock_cookie_t	*iblk;	/* event iblk cookie */
474 	ddi_idevice_cookie_t	*idev;	/* event idev cookie */
475 	callb_cpr_t	cprinfo_cs;	/* CPR cookie for cs_event_thread */
476 	callb_cpr_t	cprinfo_ss;	/* CPR cookie for cs_ss_thread */
477 	/* client management */
478 	client_t	*client_list;	/* clients on this socket */
479 	unsigned	next_cl_minor;	/* next available client minor num */
480 	kmutex_t	client_lock;	/* protects client list */
481 	uint32_t	num_clients;	/* number of clients on this socket */
482 	/* CIS support */
483 	uint32_t	cis_win_num;	/* CIS window number */
484 	unsigned	cis_win_size;	/* CIS window size */
485 	uint32_t	cis_flags;
486 	uint32_t	nfuncs;		/* number of functions */
487 	cis_info_t	cis[CS_MAX_CIS]; /* CIS information */
488 	kmutex_t	cis_lock;	/* protects CIS */
489 #ifdef	USE_IOMMAP_WINDOW
490 	/* memory mapped IO window support */
491 	io_mmap_window_t *io_mmap_window;
492 #endif	/* USE_IOMMAP_WINDOW */
493 	/* Socket Services work thread control and status */
494 	kthread_t	*ss_thread;	/* SS work thread */
495 	uint32_t	ss_thread_state; /* SS work thread state */
496 	kcondvar_t	ss_thread_cv;	/* SS work thread synchronization */
497 	kcondvar_t	ss_caller_cv;	/* SS work thread synchronization */
498 	kmutex_t	ss_thread_lock;	/* protects SS work thread state */
499 	struct cs_socket_t	*next;	/* next socket in list */
500 } cs_socket_t;
501 
502 /*
503  * cs_socket_t->flags flags
504  */
505 #define	SOCKET_CARD_INSERTED		0x00000001	/* card is inserted */
506 #define	SOCKET_IS_IO			0x00000002	/* socket in IO mode */
507 #define	SOCKET_UNLOAD_MODULE		0x00000004	/* want to unload CS */
508 #define	SOCKET_NEEDS_THREAD		0x00000008	/* wake event thread */
509 #define	SOCKET_IS_VALID			0x00000020	/* socket OK to use */
510 
511 /*
512  * cs_socket_t->thread_state and cs_socket_t->ss_thread_state flags
513  */
514 
515 /* generic for all threads */
516 #define	SOCKET_THREAD_EXIT		0x00000001	/* exit event thread */
517 
518 /* only used for per-socket event thread */
519 #define	SOCKET_WAIT_FOR_READY		0x00001000	/* waiting for READY */
520 #define	SOCKET_RESET_TIMER		0x00002000	/* RESET timer */
521 #define	SOCKET_WAIT_SYNC		0x00004000	/* SYNC */
522 
523 /* only used for Socket Services work thread */
524 #define	SOCKET_THREAD_CSCISInit		0x00100000	/* call CSCISInit */
525 
526 /*
527  * cs_socket_t->cis_flags and cs_socket_t->cis_info_t->flags flags
528  */
529 #define	CW_VALID_CIS			0x00000001	/* valid CIS */
530 #define	CW_MULTI_FUNCTION_CIS		0x00000002	/* multifunction card */
531 #define	CW_LONGLINK_A_FOUND		0x00000004	/* CISTPL_LONGLINK_A */
532 #define	CW_LONGLINK_C_FOUND		0x00000008	/* CISTP_LONGLINK_C */
533 #define	CW_LONGLINK_MFC_FOUND		0x00000010	/* LONGLINK_MFC */
534 #define	CW_CHECK_LINKTARGET		0x00000020	/* check linktarget */
535 #define	CW_RET_ON_LINKTARGET_ERROR	0x00000040	/* linktarget invalid */
536 #define	CW_CHECK_PRIMARY_CHAIN		0x00000080	/* check for primary */
537 							/* chain tuples */
538 
539 /*
540  * CW_LONGLINK_FOUND - a combination of the various CW_LONGLINK_XXX_FOUND
541  *			flags used to make the code less dense.
542  */
543 #define	CW_LONGLINK_FOUND		(CW_LONGLINK_A_FOUND |	\
544 					CW_LONGLINK_C_FOUND |	\
545 					CW_LONGLINK_MFC_FOUND)
546 
547 /*
548  * macro to test for a valid CIS window on a socket
549  */
550 #define	SOCKET_HAS_CIS_WINDOW(sp)	(sp->cis_win_num != PCMCIA_MAX_WINDOWS)
551 
552 /*
553  * cs_socket_t->init_state flags - these flags are used to keep track of what
554  *	was allocated in cs_init so that things can be deallocated properly
555  *	in cs_deinit.
556  */
557 #define	SOCKET_INIT_STATE_MUTEX		0x00000001	/* mutexii are OK */
558 #define	SOCKET_INIT_STATE_CV		0x00000002	/* cvii are OK */
559 #define	SOCKET_INIT_STATE_THREAD	0x00000004	/* thread OK */
560 #define	SOCKET_INIT_STATE_READY		0x00000008	/* socket OK */
561 #define	SOCKET_INIT_STATE_SS_THREAD	0x00000010	/* SS thread OK */
562 /*
563  * While this next flag doesn't really describe a per-socket resource,
564  *	we still set it for each socket.  When the soft interrupt handler
565  *	finally gets removed in cs_deinit, this flag will get cleared.
566  *	The value of this flag should follow the previous SOCKET_INIT
567  *	flag values.
568  */
569 #define	SOCKET_INIT_STATE_SOFTINTR	0x00000020	/* softintr handler */
570 
571 /*
572  * Macro to create a socket event thread.
573  */
574 #define	CS_THREAD_PRIORITY		(v.v_maxsyspri - 4)
575 #define	CREATE_SOCKET_EVENT_THREAD(eh, csp)			\
576 	thread_create(NULL, 0, eh, (void *)csp,			\
577 	0, &p0, TS_RUN, CS_THREAD_PRIORITY)
578 
579 /*
580  * The per-window structure.
581  */
582 typedef struct cs_window_t {
583 	uint32_t	window_num;	/* window number */
584 	window_handle_t	window_handle;	/* unique window handle */
585 	client_handle_t	client_handle;	/* owner of this window */
586 	unsigned	socket_num;	/* socket number */
587 	unsigned	state;		/* window state flags */
588 	struct cs_window_t	*next;	/* next window in list */
589 } cs_window_t;
590 
591 /*
592  * Window structure state flags - if none of the bits in the
593  *	CW_WIN_IN_USE mask are set AND if CW_WINDOW_VALID is set,
594  *	it means that this window is available and not being used
595  *	by anyone.
596  * Setting the CW_ALLOCATED will prevent the window from being found
597  *	as an available window for memory or IO; since memory windows
598  *	are not shared between clients, RequestWindow will always set
599  *	the CW_ALLOCATED flag when it has assigned a memory window to
600  *	a client.  Since we can sometimes share IO windows, RequestIO
601  *	will only set the CW_ALLOCATED flag if it doesn't want the IO
602  *	window to be used by other calls to RequestIO.
603  * When CW_WINDOW_VALID is set, it means that this is a valid window
604  *	that has been added by the framework and can be used. If this
605  *	bit is not set, this window can not be used at all.
606  */
607 #define	CW_ALLOCATED	0x00000001	/* window is allocated  */
608 #define	CW_CIS		0x00000002	/* window being used as CIS window */
609 #define	CW_MEM		0x00000004	/* window being used as mem window */
610 #define	CW_IO		0x00000008	/* window being used as IO window */
611 #define	CW_WIN_IN_USE	0x0000ffff	/* window in use mask */
612 #define	CW_WINDOW_VALID	0x00010000	/* window is valid */
613 
614 /*
615  * window handle defines - the WINDOW_HANDLE_MASK implies the maximum number
616  *	of windows allowed
617  */
618 #define	WINDOW_HANDLE_MAGIC	0x574d0000
619 #define	WINDOW_HANDLE_MASK	0x0000ffff
620 #define	GET_WINDOW_NUMBER(wh)	((wh) & WINDOW_HANDLE_MASK)
621 #define	GET_WINDOW_MAGIC(wh)	((wh) & ~WINDOW_HANDLE_MASK)
622 
623 /*
624  * The client type structures, used to sequence events to clients on a
625  *	socket. The "type" flags are the same as are used for the
626  *	RegisterClient function.
627  */
628 typedef struct client_types_t {
629 	uint32_t		type;
630 	uint32_t		order;
631 	struct client_types_t	*next;
632 } client_types_t;
633 
634 /*
635  * Flags that specify the order of client event notifications for the
636  *	client_types_t structure.
637  */
638 #define	CLIENT_EVENTS_LIFO	0x00000001
639 #define	CLIENT_EVENTS_FIFO	0x00000002
640 
641 /*
642  * This is a structure that CS uses to keep track of items that are global
643  *	to all functions in the module.
644  */
645 typedef struct cs_globals_t {
646 	cs_socket_t	*sp;		/* head of socket list */
647 	cs_window_t	*cw;		/* head of window list */
648 	kmutex_t	global_lock;	/* protects this struct */
649 	kmutex_t	window_lock;	/* protects cs_windows */
650 	ddi_softintr_t	softint_id;	/* soft interrupt handler id */
651 	timeout_id_t	sotfint_tmo;	/* soft interrupt handler timeout id */
652 	uint32_t	init_state;	/* flags set in cs_init */
653 	uint32_t	flags;		/* general global flags */
654 	uint32_t	max_socket_num;	/* highest socket number plus one */
655 	uint32_t	num_sockets;	/* total number of sockets */
656 	uint32_t	num_windows;	/* total number of windows */
657 	struct sclient_list_t	*sclient_list;
658 } cs_globals_t;
659 
660 /*
661  * Flags for cs_globals_t->init_state
662  */
663 #define	GLOBAL_INIT_STATE_SOFTINTR	0x00010000	/* softintr handler */
664 #define	GLOBAL_INIT_STATE_MUTEX		0x00020000	/* global mutex init */
665 #define	GLOBAL_INIT_STATE_NO_CLIENTS	0x00040000	/* no new clients */
666 #define	GLOBAL_INIT_STATE_UNLOADING	0x00080000	/* cs_deinit running */
667 #define	GLOBAL_INIT_STATE_SS_READY	0x00100000	/* SS ready for */
668 							/* callbacks */
669 /*
670  * Flags for cs_globals_t->flags
671  */
672 #define	GLOBAL_SUPER_CLIENT_REGISTERED	0x00000001	/* "super-client" reg */
673 #define	GLOBAL_IN_SOFTINTR		0x00000002	/* in soft int code */
674 
675 /*
676  * sclient_reg_t struct for RegisterClient when a "super-client" is
677  *	registering.
678  * This structure is actually hung off of the client_reg_t.private
679  *	structure member.  Since we don't make public how to write
680  *	a "super-client", the actual structure that the client uses
681  *	is defined in this private header file.
682  */
683 typedef struct sclient_reg_t {
684 	uint32_t		max_socket_num;
685 	uint32_t		num_sockets;
686 	uint32_t		num_windows;
687 	uint32_t		num_clients;
688 	struct sclient_list_t {
689 		client_handle_t	client_handle;
690 		uint32_t	error;
691 	} **sclient_list;
692 } sclient_reg_t;
693 
694 /*
695  * structure for event text used for cs_ss_event_text
696  */
697 typedef struct cs_ss_event_text_t {
698 	event_t		ss_event;	/* SS event code */
699 	event_t		cs_event;	/* CS event code */
700 	char		*text;
701 } cs_ss_event_text_t;
702 
703 /*
704  * Flags for cs_read_event_status
705  */
706 #define	CS_RES_IGNORE_NO_CARD		0x0001	/* don't check for card */
707 
708 /*
709  * cs_csfunc2text_strings_t structure used internally in Error2Text
710  */
711 typedef struct cs_csfunc2text_strings_t {
712 	uint32_t	item;
713 	char		*text;
714 } cs_csfunc2text_strings_t;
715 
716 /*
717  * Flags for Error2Text - not used by clients; the struct is defined
718  *	in the cs.h header file.
719  */
720 #define	CSFUN2TEXT_FUNCTION	0x0001	/* return text of CS function code */
721 #define	CSFUN2TEXT_RETURN	0x0002	/* return text of CS return code */
722 
723 /*
724  * Macros to walk the local linked CIS list.
725  *
726  * These macros can take any valid local list tuple pointer.  They return
727  *	another tuple pointer or NULL if they fail.
728  */
729 #define	GET_NEXT_TUPLE(tp, f)		CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,  \
730 						NULL, GET_NEXT_LTUPLEF |     \
731 						(f & ~CIS_GET_LTUPLE_OPMASK))
732 #define	GET_PREV_TUPLE(tp, f)		CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,  \
733 						NULL, GET_PREV_LTUPLEF |     \
734 						(f & ~CIS_GET_LTUPLE_OPMASK))
735 #define	GET_FIRST_LTUPLE(tp, f)		CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
736 						NULL, GET_FIRST_LTUPLEF |     \
737 						(f & ~CIS_GET_LTUPLE_OPMASK))
738 #define	GET_LAST_LTUPLE(tp, f)		CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
739 						NULL, GET_LAST_LTUPLEF |      \
740 						(f & ~CIS_GET_LTUPLE_OPMASK))
741 #define	FIND_LTUPLE_FWD(tp, tu, f)	CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
742 						tu, FIND_LTUPLE_FWDF |        \
743 						(f & ~CIS_GET_LTUPLE_OPMASK))
744 #define	FIND_LTUPLE_BACK(tp, tu, f)	CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
745 						tu, FIND_LTUPLE_BACKF |       \
746 						(f & ~CIS_GET_LTUPLE_OPMASK))
747 #define	FIND_NEXT_LTUPLE(tp, tu, f)	CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
748 						tu, FIND_NEXT_LTUPLEF |       \
749 						(f & ~CIS_GET_LTUPLE_OPMASK))
750 #define	FIND_PREV_LTUPLE(tp, tu, f)	CIS_PARSER(CISP_CIS_GET_LTUPLE, tp,   \
751 						tu, FIND_PREV_LTUPLEF |       \
752 						(f & ~CIS_GET_LTUPLE_OPMASK))
753 #define	FIND_FIRST_LTUPLE(tp, tu, f)	FIND_LTUPLE_FWD(GET_FIRST_LTUPLE(tp,  \
754 								f), tu, f)
755 
756 
757 /*
758  * Card Services hooks and general nexus prototypes
759  */
760 int	 cs_init(void);
761 uint32_t cs_event(event_t, uint32_t, uint32_t);
762 int	 pcmcia_set_em_handler(int (*handler)(), caddr_t events,
763 	    int elen, uint32_t id, void **cs, void **ss);
764 
765 extern csfunction_t	*cs_socket_services;
766 
767 
768 #ifdef	__cplusplus
769 }
770 #endif
771 
772 #endif	/* _CS_PRIV_H */
773