xref: /illumos-gate/usr/src/uts/common/pcmcia/nexus/pcmcia.c (revision 7a364d25fde47aa82704b12b5251bf7fac37f02e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * PCMCIA NEXUS
317c478bd9Sstevel@tonic-gate  *	The PCMCIA module is a generalized interface for
327c478bd9Sstevel@tonic-gate  *	implementing PCMCIA nexus drivers.  It preserves
337c478bd9Sstevel@tonic-gate  *	the logical socket name space while allowing multiple
347c478bd9Sstevel@tonic-gate  *	instances of the hardware to be properly represented
357c478bd9Sstevel@tonic-gate  *	in the device tree.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *	The nexus also exports events to an event manager
387c478bd9Sstevel@tonic-gate  *	driver if it has registered.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/buf.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/uio.h>
477c478bd9Sstevel@tonic-gate #include <sys/conf.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
507c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
517c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
527c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
557c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
567c478bd9Sstevel@tonic-gate #include <sys/cred.h>
577c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
587c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
607c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
617c478bd9Sstevel@tonic-gate #include <sys/callb.h>
627c478bd9Sstevel@tonic-gate #include <sys/param.h>
637c478bd9Sstevel@tonic-gate #include <sys/thread.h>
647c478bd9Sstevel@tonic-gate #include <sys/proc.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/pctypes.h>
677c478bd9Sstevel@tonic-gate #include <sys/pcmcia.h>
687c478bd9Sstevel@tonic-gate #include <sys/sservice.h>
697c478bd9Sstevel@tonic-gate #include <pcmcia/sys/cs_types.h>
707c478bd9Sstevel@tonic-gate #include <pcmcia/sys/cis.h>
717c478bd9Sstevel@tonic-gate #include <pcmcia/sys/cis_handlers.h>
727c478bd9Sstevel@tonic-gate #include <pcmcia/sys/cs.h>
737c478bd9Sstevel@tonic-gate #include <pcmcia/sys/cs_priv.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef sparc
767c478bd9Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
77*7a364d25Sschwartz 
78*7a364d25Sschwartz #elif defined(__x86) || defined(__amd64)
79*7a364d25Sschwartz #include <sys/mach_intr.h>
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #undef SocketServices
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* some bus specific stuff */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* need PCI regspec size for worst case at present */
877c478bd9Sstevel@tonic-gate #include <sys/pci.h>
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef struct pcmcia_logical_socket {
907c478bd9Sstevel@tonic-gate 	int			ls_socket; /* adapter's socket number */
917c478bd9Sstevel@tonic-gate 	uint32_t		ls_flags;
927c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter	*ls_adapter;
937c478bd9Sstevel@tonic-gate 	pcmcia_if_t		*ls_if;
947c478bd9Sstevel@tonic-gate 	dev_info_t		*ls_sockdrv;
957c478bd9Sstevel@tonic-gate 	dev_info_t		*ls_dip[PCMCIA_MAX_FUNCTIONS];
967c478bd9Sstevel@tonic-gate 	dev_info_t		*ls_mfintr_dip;
977c478bd9Sstevel@tonic-gate 	int			ls_functions;
987c478bd9Sstevel@tonic-gate 	uint32_t		ls_cs_events;
997c478bd9Sstevel@tonic-gate 	uint32_t		ls_intr_pri;
1007c478bd9Sstevel@tonic-gate 	uint32_t		ls_intr_vec;
1017c478bd9Sstevel@tonic-gate 	int			ls_intrrefs;
1027c478bd9Sstevel@tonic-gate 	struct intrspec		ls_intrspec; /* MFC intrspec */
1037c478bd9Sstevel@tonic-gate 	inthandler_t		*ls_inthandlers; /* for multifunction cards */
1047c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t	ls_iblk;
1057c478bd9Sstevel@tonic-gate 	ddi_idevice_cookie_t	ls_idev;
1067c478bd9Sstevel@tonic-gate 	kmutex_t		ls_ilock;
1077c478bd9Sstevel@tonic-gate 	int			ls_error; /* error for CS return */
1087c478bd9Sstevel@tonic-gate } pcmcia_logical_socket_t;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * entry points used by the true nexus
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate int pcmcia_detach(dev_info_t *, ddi_detach_cmd_t);
1147c478bd9Sstevel@tonic-gate int pcmcia_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
1157c478bd9Sstevel@tonic-gate int pcmcia_prop_op(dev_t, dev_info_t *, dev_info_t *, ddi_prop_op_t,
1167c478bd9Sstevel@tonic-gate 			int, char *, caddr_t, int *);
1177c478bd9Sstevel@tonic-gate void pcmcia_set_assigned(dev_info_t *, int, ra_return_t *);
1187c478bd9Sstevel@tonic-gate int pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1197c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * prototypes used internally by the nexus and sometimes Card Services
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate int SocketServices(int function, ...);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate void *CISParser(int function, ...);
1287c478bd9Sstevel@tonic-gate extern void *(*cis_parser)(int, ...);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate struct regspec *pcmcia_cons_regspec(dev_info_t *, int, uchar_t *,
1317c478bd9Sstevel@tonic-gate 					ra_return_t *);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int (*pcmcia_card_services)(int, ...) = NULL;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * variables used in the logical/physical mappings
1377c478bd9Sstevel@tonic-gate  * that the nexus common code maintains.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate struct pcmcia_adapter *pcmcia_adapters[PCMCIA_MAX_ADAPTERS];
1407c478bd9Sstevel@tonic-gate int    pcmcia_num_adapters;
1417c478bd9Sstevel@tonic-gate pcmcia_logical_socket_t *pcmcia_sockets[PCMCIA_MAX_SOCKETS];
1427c478bd9Sstevel@tonic-gate int    pcmcia_num_sockets;
1437c478bd9Sstevel@tonic-gate pcmcia_logical_window_t *pcmcia_windows[PCMCIA_MAX_WINDOWS];
1447c478bd9Sstevel@tonic-gate int    pcmcia_num_windows;
1457c478bd9Sstevel@tonic-gate struct power_entry pcmcia_power_table[PCMCIA_MAX_POWER];
1467c478bd9Sstevel@tonic-gate int	pcmcia_num_power;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate struct pcmcia_mif *pcmcia_mif_handlers = NULL;
1497c478bd9Sstevel@tonic-gate pcm_dev_node_t *pcmcia_devnodes = NULL;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate kmutex_t pcmcia_global_lock;
1527c478bd9Sstevel@tonic-gate kcondvar_t pcmcia_condvar;
1537c478bd9Sstevel@tonic-gate kmutex_t pcmcia_enum_lock;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Mapping of the device "type" to names acceptable to
1577c478bd9Sstevel@tonic-gate  * the DDI
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate static char *pcmcia_dev_type[] = {
1607c478bd9Sstevel@tonic-gate 	"multifunction",
1617c478bd9Sstevel@tonic-gate 	"byte",
1627c478bd9Sstevel@tonic-gate 	"serial",
1637c478bd9Sstevel@tonic-gate 	"parallel",
1647c478bd9Sstevel@tonic-gate 	"block",
1657c478bd9Sstevel@tonic-gate 	"display",
1667c478bd9Sstevel@tonic-gate 	"network",
1677c478bd9Sstevel@tonic-gate 	"block",
1687c478bd9Sstevel@tonic-gate 	"byte"
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate char *pcmcia_default_pm_mode = "parental-suspend-resume";
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * generic names from the approved list:
1757c478bd9Sstevel@tonic-gate  *	disk tape pci sbus scsi token-ring isa keyboard display mouse
1767c478bd9Sstevel@tonic-gate  *	audio ethernet timer memory parallel serial rtc nvram scanner
1777c478bd9Sstevel@tonic-gate  *	floppy(controller) fddi isdn atm ide pccard video-in video-out
1787c478bd9Sstevel@tonic-gate  * in some cases there will need to be device class dependent names.
1797c478bd9Sstevel@tonic-gate  * network -> ethernet, token-ring, etc.
1807c478bd9Sstevel@tonic-gate  * this list is a first guess and is used when all else fails.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate char *pcmcia_generic_names[] = {
1847c478bd9Sstevel@tonic-gate 	"multifunction",
1857c478bd9Sstevel@tonic-gate 	"memory",
1867c478bd9Sstevel@tonic-gate 	"serial",
1877c478bd9Sstevel@tonic-gate 	"parallel",
1887c478bd9Sstevel@tonic-gate 	"disk",
1897c478bd9Sstevel@tonic-gate 	"video",		/* no spec for video-out yet */
1907c478bd9Sstevel@tonic-gate 	"network",
1917c478bd9Sstevel@tonic-gate 	"aims",
1927c478bd9Sstevel@tonic-gate 	"scsi",
1937c478bd9Sstevel@tonic-gate 	"security"
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #define	PCM_GENNAME_SIZE	(sizeof (pcmcia_generic_names) / \
1977c478bd9Sstevel@tonic-gate 					sizeof (char *))
1987c478bd9Sstevel@tonic-gate #define	PCMCIA_MAP_IO	0x0
1997c478bd9Sstevel@tonic-gate #define	PCMCIA_MAP_MEM	0x1
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * The following should be 2^^n - 1
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate #define	PCMCIA_SOCKET_BITS	0x7f
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #ifdef PCMCIA_DEBUG
2077c478bd9Sstevel@tonic-gate int pcmcia_debug = 0x0;
2087c478bd9Sstevel@tonic-gate static void pcmcia_dump_minors(dev_info_t *);
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static f_tt *pcmcia_cs_event = NULL;
2127c478bd9Sstevel@tonic-gate int pcmcia_timer_id;
2137c478bd9Sstevel@tonic-gate dev_info_t	*pcmcia_dip;
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * XXX - See comments in cs.c
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate static f_tt *pcmcia_cis_parser = NULL;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate extern struct pc_socket_services pc_socket_services;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /* some function declarations */
2227c478bd9Sstevel@tonic-gate static int pcm_adapter_callback(dev_info_t *, int, int, int);
2237c478bd9Sstevel@tonic-gate extern void pcmcia_init_adapter(anp_t *, dev_info_t *);
2247c478bd9Sstevel@tonic-gate static void pcmcia_enum_thread(anp_t *);
2257c478bd9Sstevel@tonic-gate extern void pcmcia_find_cards(anp_t *);
2267c478bd9Sstevel@tonic-gate extern void pcmcia_merge_power(struct power_entry *);
2277c478bd9Sstevel@tonic-gate extern void pcmcia_do_resume(int, pcmcia_logical_socket_t *);
2287c478bd9Sstevel@tonic-gate extern void pcmcia_resume(int, pcmcia_logical_socket_t *);
2297c478bd9Sstevel@tonic-gate extern void pcmcia_do_suspend(int, pcmcia_logical_socket_t *);
2307c478bd9Sstevel@tonic-gate extern void pcm_event_manager(int, int, void *);
2317c478bd9Sstevel@tonic-gate static void pcmcia_create_dev_info(int);
2327c478bd9Sstevel@tonic-gate static int pcmcia_create_device(ss_make_device_node_t *);
2337c478bd9Sstevel@tonic-gate static void pcmcia_init_devinfo(dev_info_t *, struct pcm_device_info *);
2347c478bd9Sstevel@tonic-gate void pcmcia_fix_string(char *str);
2357c478bd9Sstevel@tonic-gate dev_info_t *pcmcia_number_socket(dev_info_t *, int);
2367c478bd9Sstevel@tonic-gate static int pcmcia_merge_conf(dev_info_t *);
2377c478bd9Sstevel@tonic-gate static uint32_t pcmcia_mfc_intr(caddr_t, caddr_t);
2387c478bd9Sstevel@tonic-gate void pcmcia_free_resources(dev_info_t *);
2397c478bd9Sstevel@tonic-gate static void pcmcia_ppd_free(struct pcmcia_parent_private *ppd);
2407c478bd9Sstevel@tonic-gate int pcmcia_get_intr(dev_info_t *, int);
2417c478bd9Sstevel@tonic-gate int pcmcia_return_intr(dev_info_t *, int);
2427c478bd9Sstevel@tonic-gate int pcmcia_ra_alloc(dev_info_t *, ndi_ra_request_t *, ra_return_t *, char *);
2437c478bd9Sstevel@tonic-gate int pcmcia_ra_free(dev_info_t *, ra_return_t *, char *);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate extern int cs_init(void);
2467c478bd9Sstevel@tonic-gate extern int cs_deinit(void);
2477c478bd9Sstevel@tonic-gate extern void cisp_init(void);
2487c478bd9Sstevel@tonic-gate extern void cis_deinit(void);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * non-DDI compliant functions are listed here
2527c478bd9Sstevel@tonic-gate  * some will be declared while others that have
2537c478bd9Sstevel@tonic-gate  * entries in .h files. All will be commented on.
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * with declarations:
2567c478bd9Sstevel@tonic-gate  *	ddi_add_child
2577c478bd9Sstevel@tonic-gate  *	ddi_binding_name
2587c478bd9Sstevel@tonic-gate  *	ddi_bus_prop_op
2597c478bd9Sstevel@tonic-gate  *	ddi_ctlops
2607c478bd9Sstevel@tonic-gate  *	ddi_find_devinfo
2617c478bd9Sstevel@tonic-gate  *	ddi_get_name_addr
2627c478bd9Sstevel@tonic-gate  *	ddi_get_parent_data
2637c478bd9Sstevel@tonic-gate  *	ddi_hold_installed_driver
2647c478bd9Sstevel@tonic-gate  *	ddi_name_to_major
2657c478bd9Sstevel@tonic-gate  *	ddi_node_name
2667c478bd9Sstevel@tonic-gate  *	ddi_pathname
2677c478bd9Sstevel@tonic-gate  *	ddi_rele_driver
2687c478bd9Sstevel@tonic-gate  *	ddi_set_name_addr
2697c478bd9Sstevel@tonic-gate  *	ddi_set_parent_data
2707c478bd9Sstevel@tonic-gate  *	ddi_unorphan_devs
2717c478bd9Sstevel@tonic-gate  *	i_ddi_bind_node_to_driver
2727c478bd9Sstevel@tonic-gate  *	i_ddi_bind_node_to_driver
2737c478bd9Sstevel@tonic-gate  *	i_ddi_bus_map
2747c478bd9Sstevel@tonic-gate  *	i_ddi_map_fault
2757c478bd9Sstevel@tonic-gate  *	i_ddi_mem_alloc
2767c478bd9Sstevel@tonic-gate  *	i_ddi_mem_alloc
2777c478bd9Sstevel@tonic-gate  *	i_ddi_mem_free
2787c478bd9Sstevel@tonic-gate  *	i_ddi_mem_free
2797c478bd9Sstevel@tonic-gate  *	modload
2807c478bd9Sstevel@tonic-gate  *	modunload
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate extern void ddi_unorphan_devs(major_t);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /* Card&Socket Services entry points */
2867c478bd9Sstevel@tonic-gate static int GetCookiesAndDip(sservice_t *);
2877c478bd9Sstevel@tonic-gate static int SSGetAdapter(get_adapter_t *);
2887c478bd9Sstevel@tonic-gate static int SSGetPage(get_page_t *);
2897c478bd9Sstevel@tonic-gate static int SSGetSocket(get_socket_t *);
2907c478bd9Sstevel@tonic-gate static int SSGetStatus(get_ss_status_t *);
2917c478bd9Sstevel@tonic-gate static int SSGetWindow(get_window_t *);
2927c478bd9Sstevel@tonic-gate static int SSInquireAdapter(inquire_adapter_t *);
2937c478bd9Sstevel@tonic-gate static int SSInquireSocket(inquire_socket_t *);
2947c478bd9Sstevel@tonic-gate static int SSInquireWindow(inquire_window_t *);
2957c478bd9Sstevel@tonic-gate static int SSResetSocket(int, int);
2967c478bd9Sstevel@tonic-gate static int SSSetPage(set_page_t *);
2977c478bd9Sstevel@tonic-gate static int SSSetSocket(set_socket_t *);
2987c478bd9Sstevel@tonic-gate static int SSSetWindow(set_window_t *);
2997c478bd9Sstevel@tonic-gate static int SSSetIRQHandler(set_irq_handler_t *);
3007c478bd9Sstevel@tonic-gate static int SSClearIRQHandler(clear_irq_handler_t *);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static struct modldrv modlmisc = {
3037c478bd9Sstevel@tonic-gate 	&mod_miscops,		/* Type of module. This one is a driver */
3047c478bd9Sstevel@tonic-gate 	"PCMCIA Nexus Support %I%", /* Name of the module. */
3057c478bd9Sstevel@tonic-gate };
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3087c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
3097c478bd9Sstevel@tonic-gate };
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate int
3127c478bd9Sstevel@tonic-gate _init()
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	int	ret;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	cisp_init();
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (cs_init() != CS_SUCCESS) {
3197c478bd9Sstevel@tonic-gate 	    if (cs_deinit() != CS_SUCCESS)
3207c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia: _init cs_deinit error\n");
3217c478bd9Sstevel@tonic-gate 	    return (-1);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mutex_init(&pcmcia_global_lock, NULL, MUTEX_DEFAULT, NULL);
3257c478bd9Sstevel@tonic-gate 	cv_init(&pcmcia_condvar, NULL, CV_DRIVER, NULL);
3267c478bd9Sstevel@tonic-gate 	mutex_init(&pcmcia_enum_lock, NULL, MUTEX_DEFAULT, NULL);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
3297c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcmcia_global_lock);
3307c478bd9Sstevel@tonic-gate 		cv_destroy(&pcmcia_condvar);
3317c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcmcia_enum_lock);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	return (ret);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate int
3377c478bd9Sstevel@tonic-gate _fini()
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	int	ret;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if ((ret = mod_remove(&modlinkage)) == 0) {
3427c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcmcia_global_lock);
3437c478bd9Sstevel@tonic-gate 		cv_destroy(&pcmcia_condvar);
3447c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcmcia_enum_lock);
3457c478bd9Sstevel@tonic-gate 		cis_deinit();
3467c478bd9Sstevel@tonic-gate 		if (cs_deinit() != CS_SUCCESS) {
3477c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia: _fini cs_deinit error\n");
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	return (ret);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate int
3547c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate extern pri_t minclsyspri;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate  * pcmcia_attach()
3637c478bd9Sstevel@tonic-gate  *	the attach routine must make sure that everything needed is present
3647c478bd9Sstevel@tonic-gate  *	including real hardware.  The sequence of events is:
3657c478bd9Sstevel@tonic-gate  *		attempt to load all adapter drivers
3667c478bd9Sstevel@tonic-gate  *		attempt to load Card Services (which _depends_on pcmcia)
3677c478bd9Sstevel@tonic-gate  *		initialize logical sockets
3687c478bd9Sstevel@tonic-gate  *		report the nexus exists
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate int
3727c478bd9Sstevel@tonic-gate pcmcia_attach(dev_info_t *dip, anp_t *adapter)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	int count, done, i;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
3777c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
3787c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_attach: dip=0x%p adapter=0x%p\n",
3797c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)adapter);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate #endif
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	pcmcia_dip = dip;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	mutex_enter(&pcmcia_enum_lock);
3867c478bd9Sstevel@tonic-gate 	mutex_enter(&pcmcia_global_lock);
3877c478bd9Sstevel@tonic-gate 	if (pcmcia_num_adapters == 0) {
3887c478bd9Sstevel@tonic-gate 		pcmcia_cis_parser = (f_tt *)CISParser;
3897c478bd9Sstevel@tonic-gate 		cis_parser = (void *(*)(int, ...)) CISParser;
3907c478bd9Sstevel@tonic-gate 		pcmcia_cs_event = (f_tt *)cs_event;
3917c478bd9Sstevel@tonic-gate 		cs_socket_services = SocketServices;
3927c478bd9Sstevel@tonic-gate 		/* tell CS we are up with basic init level */
3937c478bd9Sstevel@tonic-gate 		(void) cs_event(PCE_SS_INIT_STATE, PCE_SS_STATE_INIT, 0);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
3977c478bd9Sstevel@tonic-gate 	    PCM_DEVICETYPE, "pccard");
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);	/* directory/device naming */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * now setup any power management stuff necessary.
4037c478bd9Sstevel@tonic-gate 	 * we do it here in order to ensure that all PC Card nexi
4047c478bd9Sstevel@tonic-gate 	 * implement it.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (pm_create_components(dip, 1) != DDI_SUCCESS) {
4087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: not power managed\n",
4097c478bd9Sstevel@tonic-gate 			ddi_get_name_addr(dip));
4107c478bd9Sstevel@tonic-gate 	} else {
4117c478bd9Sstevel@tonic-gate 		pm_set_normal_power(dip, 0, 1);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * setup the info necessary for Card Services/SocketServices
4167c478bd9Sstevel@tonic-gate 	 * and notify CS when ready.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	pcmcia_free_resources(dip);
4207c478bd9Sstevel@tonic-gate 	pcmcia_init_adapter(adapter, dip);
4217c478bd9Sstevel@tonic-gate 	/* exit mutex so CS can run for any cards found */
4227c478bd9Sstevel@tonic-gate 	mutex_exit(&pcmcia_global_lock);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * schedule an asynchronous thread to enumerate the cards
4267c478bd9Sstevel@tonic-gate 	 * present in the adapter at boot time
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, pcmcia_enum_thread, adapter, 0, &p0,
4297c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * make sure the devices are identified before
4337c478bd9Sstevel@tonic-gate 	 * returning.  We do this by checking each socket to see if
4347c478bd9Sstevel@tonic-gate 	 * a card is present.  If there is one, and there isn't a dip,
4357c478bd9Sstevel@tonic-gate 	 * we can't be done.  We scan the list of sockets doing the
4367c478bd9Sstevel@tonic-gate 	 * check. if we aren't done, wait for a condition variable to
4377c478bd9Sstevel@tonic-gate 	 * wakeup.
4387c478bd9Sstevel@tonic-gate 	 * Because we can miss a wakeup and because things can
4397c478bd9Sstevel@tonic-gate 	 * take time, we do eventually give up and have a timeout.
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	for (count = 0, done = 0;
4437c478bd9Sstevel@tonic-gate 	    done == 0 && count < max(pcmcia_num_sockets, 16);
4447c478bd9Sstevel@tonic-gate 	    count++) {
4457c478bd9Sstevel@tonic-gate 		done = 1;
4467c478bd9Sstevel@tonic-gate 		/* block CS while checking so we don't miss anything */
4477c478bd9Sstevel@tonic-gate 		mutex_enter(&pcmcia_global_lock);
4487c478bd9Sstevel@tonic-gate 		for (i = 0; i < pcmcia_num_sockets; i++) {
4497c478bd9Sstevel@tonic-gate 			get_ss_status_t status;
4507c478bd9Sstevel@tonic-gate 			if (pcmcia_sockets[i] == NULL)
4517c478bd9Sstevel@tonic-gate 				continue;
4527c478bd9Sstevel@tonic-gate 			bzero(&status, sizeof (status));
4537c478bd9Sstevel@tonic-gate 			status.socket = i;
4547c478bd9Sstevel@tonic-gate 			if (SSGetStatus(&status) == SUCCESS) {
4557c478bd9Sstevel@tonic-gate 				if (status.CardState & SBM_CD &&
4567c478bd9Sstevel@tonic-gate 				    pcmcia_sockets[i]->ls_dip[0] == NULL) {
4577c478bd9Sstevel@tonic-gate 					done = 0;
4587c478bd9Sstevel@tonic-gate 				}
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		/* only wait if we aren't done with this set */
4627c478bd9Sstevel@tonic-gate 		if (!done) {
4637c478bd9Sstevel@tonic-gate 			mutex_exit(&pcmcia_global_lock);
4647c478bd9Sstevel@tonic-gate 			delay(10); /* give up CPU for a time */
4657c478bd9Sstevel@tonic-gate 			mutex_enter(&pcmcia_global_lock);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		mutex_exit(&pcmcia_global_lock);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	mutex_exit(&pcmcia_enum_lock);
4717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * pcmcia_detach
4767c478bd9Sstevel@tonic-gate  *	unload everything and then detach the nexus
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate /* ARGSUSED */
4797c478bd9Sstevel@tonic-gate int
4807c478bd9Sstevel@tonic-gate pcmcia_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	switch (cmd) {
4837c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4847c478bd9Sstevel@tonic-gate 		pm_destroy_components(dip);
4857c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * resume from a checkpoint
4897c478bd9Sstevel@tonic-gate 	 * We don't do anything special here since the adapter
4907c478bd9Sstevel@tonic-gate 	 * driver will generate resume events that we intercept
4917c478bd9Sstevel@tonic-gate 	 * and convert to insert events.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4947c478bd9Sstevel@tonic-gate 	case DDI_PM_SUSPEND:
4957c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	default:
4987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * card_services_error()
5047c478bd9Sstevel@tonic-gate  *	used to make 2.4/2.5 drivers get an error when
5057c478bd9Sstevel@tonic-gate  *	they try to initialize.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static int
5087c478bd9Sstevel@tonic-gate card_services_error()
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	return (CS_BAD_VERSION);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate static int (*cs_error_ptr)() = card_services_error;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * pcmcia_ctlops
5167c478bd9Sstevel@tonic-gate  *	handle the nexus control operations for the cases where
5177c478bd9Sstevel@tonic-gate  *	a PC Card driver gets called and we need to modify the
5187c478bd9Sstevel@tonic-gate  *	devinfo structure or otherwise do bus specific operations
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate int
5217c478bd9Sstevel@tonic-gate pcmcia_ctlops(dev_info_t *dip, dev_info_t *rdip,
5227c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	int e;
5257c478bd9Sstevel@tonic-gate 	char name[64];
5267c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
5277c478bd9Sstevel@tonic-gate 	power_req_t *pm;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
5307c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
5317c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_ctlops(%p, %p, %d, %p, %p)\n",
5327c478bd9Sstevel@tonic-gate 			(void *)dip, (void *)rdip, ctlop, (void *)arg,
5337c478bd9Sstevel@tonic-gate 			(void *)result);
5347c478bd9Sstevel@tonic-gate 		if (rdip != NULL && ddi_get_name(rdip) != NULL)
5357c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\t[%s]\n", ddi_get_name(rdip));
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate #endif
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	switch (ctlop) {
5407c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
5417c478bd9Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
5427c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (strcmp("pcs", ddi_node_name(rdip)) == 0)
5457c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "?PCCard socket %d at %s@%s\n",
5467c478bd9Sstevel@tonic-gate 				ddi_get_instance(rdip),
5477c478bd9Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_name_addr(dip));
5487c478bd9Sstevel@tonic-gate 		else
5497c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "?%s%d at %s@%s in socket %d\n",
5507c478bd9Sstevel@tonic-gate 				ddi_driver_name(rdip),
5517c478bd9Sstevel@tonic-gate 				ddi_get_instance(rdip),
5527c478bd9Sstevel@tonic-gate 				ddi_driver_name(dip),
5537c478bd9Sstevel@tonic-gate 				ddi_get_name_addr(dip),
5547c478bd9Sstevel@tonic-gate 				CS_GET_SOCKET_NUMBER(
5557c478bd9Sstevel@tonic-gate 				    ddi_getprop(DDI_DEV_T_NONE, rdip,
5567c478bd9Sstevel@tonic-gate 				    DDI_PROP_DONTPASS,
5577c478bd9Sstevel@tonic-gate 				    PCM_DEV_SOCKET, -1)));
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * we get control here before the child is called.
5647c478bd9Sstevel@tonic-gate 		 * we can change things if necessary.  This is where
5657c478bd9Sstevel@tonic-gate 		 * the CardServices hook gets planted.
5667c478bd9Sstevel@tonic-gate 		 */
5677c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
5687c478bd9Sstevel@tonic-gate 		if (pcmcia_debug) {
5697c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia: init child: %s(%d) @%p\n",
5707c478bd9Sstevel@tonic-gate 				ddi_node_name(arg), ddi_get_instance(arg),
5717c478bd9Sstevel@tonic-gate 				(void *)arg);
5727c478bd9Sstevel@tonic-gate 			if (DEVI(arg)->devi_binding_name != NULL)
5737c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
5747c478bd9Sstevel@tonic-gate 					DEVI(arg)->devi_binding_name);
5757c478bd9Sstevel@tonic-gate 			if (DEVI(arg)->devi_node_name != NULL)
5767c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\tnode_name=%s\n",
5777c478bd9Sstevel@tonic-gate 					DEVI(arg)->devi_node_name);
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate #endif
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
5827c478bd9Sstevel@tonic-gate 			ddi_get_parent_data((dev_info_t *)arg);
5837c478bd9Sstevel@tonic-gate 		if (ppd == NULL)
5847c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		if (strcmp("pcs", ddi_node_name((dev_info_t *)arg)) == 0) {
5877c478bd9Sstevel@tonic-gate 			if (ppd == NULL)
5887c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
5897c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%x",
5907c478bd9Sstevel@tonic-gate 			    (int)ppd->ppd_reg[0].phys_hi);
5917c478bd9Sstevel@tonic-gate 			ddi_set_name_addr((dev_info_t *)arg, name);
5927c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * We don't want driver.conf files that stay in
5977c478bd9Sstevel@tonic-gate 		 * pseudo device form.	It is acceptable to have
5987c478bd9Sstevel@tonic-gate 		 * .conf files add properties only.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		if (ndi_dev_is_persistent_node((dev_info_t *)arg) == 0) {
6017c478bd9Sstevel@tonic-gate 			(void) pcmcia_merge_conf((dev_info_t *)arg);
6027c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: %s.conf invalid",
6037c478bd9Sstevel@tonic-gate 				ddi_get_name((dev_info_t *)arg),
6047c478bd9Sstevel@tonic-gate 				ddi_get_instance((dev_info_t *)arg),
6057c478bd9Sstevel@tonic-gate 				ddi_get_name((dev_info_t *)arg));
6067c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
6117c478bd9Sstevel@tonic-gate 		if (pcmcia_debug && ppd != NULL) {
6127c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tnreg=%x, intr=%x, socket=%x,"
6137c478bd9Sstevel@tonic-gate 				" function=%x, active=%x, flags=%x\n",
6147c478bd9Sstevel@tonic-gate 				ppd->ppd_nreg, ppd->ppd_intr,
6157c478bd9Sstevel@tonic-gate 				ppd->ppd_socket, ppd->ppd_function,
6167c478bd9Sstevel@tonic-gate 				ppd->ppd_active, ppd->ppd_flags);
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate #endif
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		/*
6217c478bd9Sstevel@tonic-gate 		 * make sure names are relative to socket number
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 		if (ppd->ppd_function > 0) {
6247c478bd9Sstevel@tonic-gate 			int sock;
6257c478bd9Sstevel@tonic-gate 			int func;
6267c478bd9Sstevel@tonic-gate 			sock = ppd->ppd_socket;
6277c478bd9Sstevel@tonic-gate 			func = ppd->ppd_function;
6287c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%x,%x", sock, func);
6297c478bd9Sstevel@tonic-gate 		} else {
6307c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "%x", ppd->ppd_socket);
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 		ddi_set_name_addr((dev_info_t *)arg, name);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
6357c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
6367c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia: system init done for %s [%s] "
6377c478bd9Sstevel@tonic-gate 				"nodeid: %x @%s\n",
6387c478bd9Sstevel@tonic-gate 				ddi_get_name(arg), ddi_get_name_addr(arg),
6397c478bd9Sstevel@tonic-gate 				DEVI(arg)->devi_nodeid, name);
6407c478bd9Sstevel@tonic-gate 		if (pcmcia_debug > 1)
6417c478bd9Sstevel@tonic-gate 			pcmcia_dump_minors((dev_info_t *)arg);
6427c478bd9Sstevel@tonic-gate #endif
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
6497c478bd9Sstevel@tonic-gate 		if (pcmcia_debug) {
6507c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia: uninit child: %s(%d) @%p\n",
6517c478bd9Sstevel@tonic-gate 				ddi_node_name(arg), ddi_get_instance(arg),
6527c478bd9Sstevel@tonic-gate 				(void *)arg);
6537c478bd9Sstevel@tonic-gate 			if (DEVI(arg)->devi_binding_name != NULL)
6547c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
6557c478bd9Sstevel@tonic-gate 					DEVI(arg)->devi_binding_name);
6567c478bd9Sstevel@tonic-gate 			if (DEVI(arg)->devi_node_name != NULL)
6577c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\tnode_name=%s\n",
6587c478bd9Sstevel@tonic-gate 					DEVI(arg)->devi_node_name);
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate #endif
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		ddi_set_name_addr((dev_info_t *)arg, NULL);
6637c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
6647c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SLAVEONLY:
6677c478bd9Sstevel@tonic-gate 		/* PCMCIA devices can't ever be busmaster until CardBus */
6687c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
6697c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(rdip);
6707c478bd9Sstevel@tonic-gate 		if (ppd != NULL && ppd->ppd_flags & PPD_CB_BUSMASTER)
6717c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE); /* at most */
6727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
6757c478bd9Sstevel@tonic-gate 		/* in general this is true. */
6767c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
6797c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
6807c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(rdip);
6817c478bd9Sstevel@tonic-gate 		if (ppd != NULL)
6827c478bd9Sstevel@tonic-gate 			*((uint32_t *)result) = (ppd->ppd_nreg);
6837c478bd9Sstevel@tonic-gate 		else
6847c478bd9Sstevel@tonic-gate 			*((uint32_t *)result) = 0;
6857c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
6887c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
6897c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(rdip);
6907c478bd9Sstevel@tonic-gate 		if (ppd != NULL && ppd->ppd_nreg > 0)
6917c478bd9Sstevel@tonic-gate 			*((off_t *)result) =  sizeof (struct pcm_regs);
6927c478bd9Sstevel@tonic-gate 		else
6937c478bd9Sstevel@tonic-gate 			*((off_t *)result) = 0;
6947c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
6977c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
6987c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(rdip);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		if (ppd == NULL)
7017c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7027c478bd9Sstevel@tonic-gate 		/*
7037c478bd9Sstevel@tonic-gate 		 * if this is not present, don't bother (claim success)
7047c478bd9Sstevel@tonic-gate 		 * since it is already in the right state.  Don't
7057c478bd9Sstevel@tonic-gate 		 * do any resume either since the card insertion will
7067c478bd9Sstevel@tonic-gate 		 * happen independently.
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		if (!ppd->ppd_active)
7097c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
7107c478bd9Sstevel@tonic-gate 		for (e = 0; e < pcmcia_num_adapters; e++)
7117c478bd9Sstevel@tonic-gate 			if (pcmcia_adapters[e] ==
7127c478bd9Sstevel@tonic-gate 			    pcmcia_sockets[ppd->ppd_socket]->ls_adapter)
7137c478bd9Sstevel@tonic-gate 				break;
7147c478bd9Sstevel@tonic-gate 		if (e == pcmcia_num_adapters)
7157c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7167c478bd9Sstevel@tonic-gate 		pm = (power_req_t *)arg;
7177c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
7187c478bd9Sstevel@tonic-gate 		if (pcmcia_debug) {
7197c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "power: %d: %p, %d, %d [%s]\n",
7207c478bd9Sstevel@tonic-gate 				pm->request_type,
7217c478bd9Sstevel@tonic-gate 				(void *)pm->req.set_power_req.who,
7227c478bd9Sstevel@tonic-gate 				pm->req.set_power_req.cmpt,
7237c478bd9Sstevel@tonic-gate 				pm->req.set_power_req.level,
7247c478bd9Sstevel@tonic-gate 				ddi_get_name_addr(rdip));
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate #endif
7277c478bd9Sstevel@tonic-gate 		e = ppd->ppd_socket;
7287c478bd9Sstevel@tonic-gate 		switch (pm->request_type) {
7297c478bd9Sstevel@tonic-gate 		case PMR_SUSPEND:
7307c478bd9Sstevel@tonic-gate 			if (!(pcmcia_sockets[e]->ls_flags &
7317c478bd9Sstevel@tonic-gate 			    PCS_SUSPENDED)) {
7327c478bd9Sstevel@tonic-gate 				pcmcia_do_suspend(ppd->ppd_socket,
7337c478bd9Sstevel@tonic-gate 				    pcmcia_sockets[e]);
7347c478bd9Sstevel@tonic-gate 			}
7357c478bd9Sstevel@tonic-gate 			ppd->ppd_flags |= PPD_SUSPENDED;
7367c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
7377c478bd9Sstevel@tonic-gate 		case PMR_RESUME:
7387c478bd9Sstevel@tonic-gate 			/* for now, we just succeed since the rest is done */
7397c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
7407c478bd9Sstevel@tonic-gate 		case PMR_SET_POWER:
7417c478bd9Sstevel@tonic-gate 			/*
7427c478bd9Sstevel@tonic-gate 			 * not sure how to handle power control
7437c478bd9Sstevel@tonic-gate 			 * for now, we let the child handle it itself
7447c478bd9Sstevel@tonic-gate 			 */
7457c478bd9Sstevel@tonic-gate 			(void) pcmcia_power(pm->req.set_power_req.who,
7467c478bd9Sstevel@tonic-gate 				pm->req.set_power_req.cmpt,
7477c478bd9Sstevel@tonic-gate 				pm->req.set_power_req.level);
7487c478bd9Sstevel@tonic-gate 			break;
7497c478bd9Sstevel@tonic-gate 		default:
7507c478bd9Sstevel@tonic-gate 			break;
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7537c478bd9Sstevel@tonic-gate 		/* These CTLOPS will need to be implemented for new form */
7547c478bd9Sstevel@tonic-gate 		/* let CardServices know about this */
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	default:
7577c478bd9Sstevel@tonic-gate 		/* if we don't understand, pass up the tree */
7587c478bd9Sstevel@tonic-gate 		/* most things default to general ops */
7597c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate struct pcmcia_props {
7647c478bd9Sstevel@tonic-gate 	char *name;
7657c478bd9Sstevel@tonic-gate 	int   len;
7667c478bd9Sstevel@tonic-gate 	int   prop;
7677c478bd9Sstevel@tonic-gate } pcmcia_internal_props[] = {
7687c478bd9Sstevel@tonic-gate 	{ PCM_DEV_ACTIVE, 0, PCMCIA_PROP_ACTIVE },
7697c478bd9Sstevel@tonic-gate 	{ PCM_DEV_R2TYPE, 0, PCMCIA_PROP_R2TYPE },
7707c478bd9Sstevel@tonic-gate 	{ PCM_DEV_CARDBUS, 0, PCMCIA_PROP_CARDBUS },
7717c478bd9Sstevel@tonic-gate 	{ CS_PROP, sizeof (void *), PCMCIA_PROP_OLDCS },
7727c478bd9Sstevel@tonic-gate 	{ "reg", 0, PCMCIA_PROP_REG },
7737c478bd9Sstevel@tonic-gate 	{ "interrupts", sizeof (int), PCMCIA_PROP_INTR },
7747c478bd9Sstevel@tonic-gate 	{ "pm-hardware-state", 0, PCMCIA_PROP_DEFAULT_PM },
7757c478bd9Sstevel@tonic-gate };
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*
7787c478bd9Sstevel@tonic-gate  * pcmcia_prop_decode(name)
7797c478bd9Sstevel@tonic-gate  *	decode the name and determine if this is a property
7807c478bd9Sstevel@tonic-gate  *	we construct on the fly, one we have on the prop list
7817c478bd9Sstevel@tonic-gate  *	or one that requires calling the CIS code.
7827c478bd9Sstevel@tonic-gate  */
7837c478bd9Sstevel@tonic-gate static int
7847c478bd9Sstevel@tonic-gate pcmcia_prop_decode(char *name)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	int i;
7877c478bd9Sstevel@tonic-gate 	if (strncmp(name, "cistpl_", 7) == 0)
7887c478bd9Sstevel@tonic-gate 		return (PCMCIA_PROP_CIS);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	for (i = 0; i < (sizeof (pcmcia_internal_props) /
7917c478bd9Sstevel@tonic-gate 	    sizeof (struct pcmcia_props)); i++) {
7927c478bd9Sstevel@tonic-gate 		if (strcmp(name, pcmcia_internal_props[i].name) == 0)
7937c478bd9Sstevel@tonic-gate 			return (i);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	return (PCMCIA_PROP_UNKNOWN);
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate  * pcmcia_prop_op()
8017c478bd9Sstevel@tonic-gate  *	we don't have properties in PROM per se so look for them
8027c478bd9Sstevel@tonic-gate  *	only in the devinfo node.  Future may allow us to find
8037c478bd9Sstevel@tonic-gate  *	certain CIS tuples via this interface if a user asks for
8047c478bd9Sstevel@tonic-gate  *	a property of the form "cistpl-<tuplename>" but not yet.
8057c478bd9Sstevel@tonic-gate  *
8067c478bd9Sstevel@tonic-gate  *	The addition of 1275 properties adds to the necessity.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate int
8097c478bd9Sstevel@tonic-gate pcmcia_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
8107c478bd9Sstevel@tonic-gate     ddi_prop_op_t prop_op, int mod_flags,
8117c478bd9Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	int len, proplen, which, flags;
8147c478bd9Sstevel@tonic-gate 	caddr_t buff, propptr;
8157c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	len = *lengthp;
8187c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(ch_dip);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	switch (which = pcmcia_prop_decode(name)) {
8217c478bd9Sstevel@tonic-gate 	default:
8227c478bd9Sstevel@tonic-gate 		if (ppd == NULL)
8237c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NOT_FOUND);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		/* note that proplen may get modified */
8267c478bd9Sstevel@tonic-gate 		proplen = pcmcia_internal_props[which].len;
8277c478bd9Sstevel@tonic-gate 		switch (pcmcia_internal_props[which].prop) {
8287c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_DEFAULT_PM:
8297c478bd9Sstevel@tonic-gate 			propptr = pcmcia_default_pm_mode;
8307c478bd9Sstevel@tonic-gate 			proplen = strlen(propptr) + 1;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_OLDCS:
8337c478bd9Sstevel@tonic-gate 			propptr = (caddr_t)&cs_error_ptr;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_REG:
8367c478bd9Sstevel@tonic-gate 			propptr = (caddr_t)ppd->ppd_reg;
8377c478bd9Sstevel@tonic-gate 			proplen = ppd->ppd_nreg * sizeof (struct pcm_regs);
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_INTR:
8407c478bd9Sstevel@tonic-gate 			propptr = (caddr_t)&ppd->ppd_intr;
8417c478bd9Sstevel@tonic-gate 			break;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		/* the next set are boolean values */
8447c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_ACTIVE:
8457c478bd9Sstevel@tonic-gate 			propptr = NULL;
8467c478bd9Sstevel@tonic-gate 			if (!ppd->ppd_active) {
8477c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NOT_FOUND);
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 			break;
8507c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_R2TYPE:
8517c478bd9Sstevel@tonic-gate 			propptr = NULL;
8527c478bd9Sstevel@tonic-gate 			if (ppd->ppd_flags & PPD_CARD_CARDBUS)
8537c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NOT_FOUND);
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case PCMCIA_PROP_CARDBUS:
8567c478bd9Sstevel@tonic-gate 			propptr = NULL;
8577c478bd9Sstevel@tonic-gate 			if (!(ppd->ppd_flags * PPD_CARD_CARDBUS))
8587c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NOT_FOUND);
8597c478bd9Sstevel@tonic-gate 			break;
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		break;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	case PCMCIA_PROP_CIS:
8657c478bd9Sstevel@tonic-gate 		/*
8667c478bd9Sstevel@tonic-gate 		 * once we have the lookup code in place
8677c478bd9Sstevel@tonic-gate 		 * it is sufficient to break out of the switch
8687c478bd9Sstevel@tonic-gate 		 * once proplen and propptr are set.
8697c478bd9Sstevel@tonic-gate 		 * The common prop_op code deals with the rest.
8707c478bd9Sstevel@tonic-gate 		 */
8717c478bd9Sstevel@tonic-gate 	case PCMCIA_PROP_UNKNOWN:
8727c478bd9Sstevel@tonic-gate 		return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
8737c478bd9Sstevel@tonic-gate 		    mod_flags | DDI_PROP_NOTPROM,
8747c478bd9Sstevel@tonic-gate 		    name, valuep, lengthp));
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	if (prop_op == PROP_LEN) {
8787c478bd9Sstevel@tonic-gate 		/* just the length */
8797c478bd9Sstevel@tonic-gate 		*lengthp = proplen;
8807c478bd9Sstevel@tonic-gate 		return (DDI_PROP_SUCCESS);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	switch (prop_op) {
8837c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_ALLOC:
8847c478bd9Sstevel@tonic-gate 		if (mod_flags & DDI_PROP_CANSLEEP)
8857c478bd9Sstevel@tonic-gate 			flags = KM_SLEEP;
8867c478bd9Sstevel@tonic-gate 		else
8877c478bd9Sstevel@tonic-gate 			flags = KM_NOSLEEP;
8887c478bd9Sstevel@tonic-gate 		buff = kmem_alloc((size_t)proplen, flags);
8897c478bd9Sstevel@tonic-gate 		if (buff == NULL)
8907c478bd9Sstevel@tonic-gate 			return (DDI_PROP_NO_MEMORY);
8917c478bd9Sstevel@tonic-gate 		*(caddr_t *)valuep = (caddr_t)buff;
8927c478bd9Sstevel@tonic-gate 		break;
8937c478bd9Sstevel@tonic-gate 	case PROP_LEN_AND_VAL_BUF:
8947c478bd9Sstevel@tonic-gate 		buff = (caddr_t)valuep;
8957c478bd9Sstevel@tonic-gate 		if (len < proplen)
8967c478bd9Sstevel@tonic-gate 			return (DDI_PROP_BUF_TOO_SMALL);
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 	default:
8997c478bd9Sstevel@tonic-gate 		break;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	if (proplen > 0)
9037c478bd9Sstevel@tonic-gate 		bcopy(propptr, buff, proplen);
9047c478bd9Sstevel@tonic-gate 	*lengthp = proplen;
9057c478bd9Sstevel@tonic-gate 	return (DDI_PROP_SUCCESS);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate struct regspec *
9107c478bd9Sstevel@tonic-gate pcmcia_rnum_to_regspec(dev_info_t *dip, int rnumber)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
9137c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
9147c478bd9Sstevel@tonic-gate 	if (ppd->ppd_nreg < rnumber)
9157c478bd9Sstevel@tonic-gate 		return (NULL);
9167c478bd9Sstevel@tonic-gate 	return ((struct regspec *)&ppd->ppd_reg[rnumber]);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate struct regspec *
9207c478bd9Sstevel@tonic-gate pcmcia_rnum_to_mapped(dev_info_t *dip, int rnumber)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
9237c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
9247c478bd9Sstevel@tonic-gate 	if (ppd->ppd_nreg < rnumber)
9257c478bd9Sstevel@tonic-gate 		return (NULL);
9267c478bd9Sstevel@tonic-gate 	if (ppd->ppd_assigned[rnumber].phys_len == 0)
9277c478bd9Sstevel@tonic-gate 		return (NULL);
9287c478bd9Sstevel@tonic-gate 	else
9297c478bd9Sstevel@tonic-gate 		return ((struct regspec *)&ppd->ppd_assigned[rnumber]);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate int
9337c478bd9Sstevel@tonic-gate pcmcia_find_rnum(dev_info_t *dip, struct regspec *reg)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
9367c478bd9Sstevel@tonic-gate 	struct regspec *regp;
9377c478bd9Sstevel@tonic-gate 	int i;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
9407c478bd9Sstevel@tonic-gate 	if (ppd == NULL)
9417c478bd9Sstevel@tonic-gate 		return (-1);
9427c478bd9Sstevel@tonic-gate 	for (regp = (struct regspec *)ppd->ppd_reg, i = 0;
9437c478bd9Sstevel@tonic-gate 	    i < ppd->ppd_nreg; i++, regp++) {
9447c478bd9Sstevel@tonic-gate 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
9457c478bd9Sstevel@tonic-gate 			return (i);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	for (regp = (struct regspec *)ppd->ppd_assigned, i = 0;
9487c478bd9Sstevel@tonic-gate 	    i < ppd->ppd_nreg; i++, regp++) {
9497c478bd9Sstevel@tonic-gate 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
9507c478bd9Sstevel@tonic-gate 			return (i);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	return (-1);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate int
9577c478bd9Sstevel@tonic-gate pcmcia_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
9587c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	struct pcm_regs *regs, *mregs = NULL, tmp_reg;
9617c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr = *mp;
9627c478bd9Sstevel@tonic-gate 	ra_return_t ret;
9637c478bd9Sstevel@tonic-gate 	int check, rnum = -1;
9647c478bd9Sstevel@tonic-gate 	uint32_t base;
9657c478bd9Sstevel@tonic-gate 	uchar_t regbuf[sizeof (pci_regspec_t)];
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	mp = &mr;		/* a copy of original request */
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/* check for register number */
9707c478bd9Sstevel@tonic-gate 	switch (mp->map_type) {
9717c478bd9Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
9727c478bd9Sstevel@tonic-gate 		regs = (struct pcm_regs *)mp->map_obj.rp;
9737c478bd9Sstevel@tonic-gate 		mregs = (struct pcm_regs *)mp->map_obj.rp;
9747c478bd9Sstevel@tonic-gate 		/*
9757c478bd9Sstevel@tonic-gate 		 * when using regspec, must not be relocatable
9767c478bd9Sstevel@tonic-gate 		 * and should be from assigned space.
9777c478bd9Sstevel@tonic-gate 		 */
9787c478bd9Sstevel@tonic-gate 		if (!PC_REG_RELOC(regs->phys_hi))
9797c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9807c478bd9Sstevel@tonic-gate 		rnum = pcmcia_find_rnum(rdip, (struct regspec *)mregs);
9817c478bd9Sstevel@tonic-gate 		break;
9827c478bd9Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
9837c478bd9Sstevel@tonic-gate 		regs = (struct pcm_regs *)
9847c478bd9Sstevel@tonic-gate 			pcmcia_rnum_to_regspec(rdip, mp->map_obj.rnumber);
9857c478bd9Sstevel@tonic-gate 		mregs = (struct pcm_regs *)
9867c478bd9Sstevel@tonic-gate 			pcmcia_rnum_to_mapped(rdip, mp->map_obj.rnumber);
9877c478bd9Sstevel@tonic-gate 		rnum = mp->map_obj.rnumber;
9887c478bd9Sstevel@tonic-gate 		if (regs == NULL)
9897c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9907c478bd9Sstevel@tonic-gate 		mp->map_type = DDI_MT_REGSPEC;
9917c478bd9Sstevel@tonic-gate 		mp->map_obj.rp = (struct regspec *)mregs;
9927c478bd9Sstevel@tonic-gate 		break;
9937c478bd9Sstevel@tonic-gate 	default:
9947c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/* basic sanity checks */
9987c478bd9Sstevel@tonic-gate 	switch (mp->map_op) {
9997c478bd9Sstevel@tonic-gate 	default:
10007c478bd9Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
10017c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
10027c478bd9Sstevel@tonic-gate 		if (mregs == NULL)
10037c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10047c478bd9Sstevel@tonic-gate 		regs = mregs;
10057c478bd9Sstevel@tonic-gate 		break;
10067c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
10077c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_HANDLE:
10087c478bd9Sstevel@tonic-gate 		panic("unsupported bus operation");
10097c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * we need a private copy for manipulation and
10147c478bd9Sstevel@tonic-gate 	 * calculation of the correct ranges
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	tmp_reg = *regs;
10177c478bd9Sstevel@tonic-gate 	mp->map_obj.rp = (struct regspec *)(regs = &tmp_reg);
10187c478bd9Sstevel@tonic-gate 	base = regs->phys_lo;
10197c478bd9Sstevel@tonic-gate 	if (base == 0 && offset != 0) {
10207c478bd9Sstevel@tonic-gate 		/*
10217c478bd9Sstevel@tonic-gate 		 * for now this is an error.  What does it really mean
10227c478bd9Sstevel@tonic-gate 		 * to ask for an offset from an address that hasn't
10237c478bd9Sstevel@tonic-gate 		 * been allocated yet.
10247c478bd9Sstevel@tonic-gate 		 */
10257c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	regs->phys_lo += (uint32_t)offset;
10287c478bd9Sstevel@tonic-gate 	if (len != 0) {
10297c478bd9Sstevel@tonic-gate 		if (len > regs->phys_len) {
10307c478bd9Sstevel@tonic-gate 			return (DDI_ME_INVAL);
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 		regs->phys_len = len;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * basic sanity is checked so now make sure
10377c478bd9Sstevel@tonic-gate 	 * we can actually allocate something for this
10387c478bd9Sstevel@tonic-gate 	 * request and then convert to a "standard"
10397c478bd9Sstevel@tonic-gate 	 * regspec for the next layer up (pci/isa/rootnex/etc.)
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	switch (PC_GET_REG_TYPE(regs->phys_hi)) {
10437c478bd9Sstevel@tonic-gate 	case PC_REG_SPACE_IO:
10447c478bd9Sstevel@tonic-gate 		check = PCA_RES_NEED_IO;
10457c478bd9Sstevel@tonic-gate 		break;
10467c478bd9Sstevel@tonic-gate 	case PC_REG_SPACE_MEMORY:
10477c478bd9Sstevel@tonic-gate 		check = PCA_RES_NEED_MEM;
10487c478bd9Sstevel@tonic-gate 		break;
10497c478bd9Sstevel@tonic-gate 	default:
10507c478bd9Sstevel@tonic-gate 		/* not a valid register type */
10517c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_REGSPEC;
10557c478bd9Sstevel@tonic-gate 	ret.ra_addr_hi = 0;
10567c478bd9Sstevel@tonic-gate 	ret.ra_addr_lo = regs->phys_lo;
10577c478bd9Sstevel@tonic-gate 	ret.ra_len = regs->phys_len;
10587c478bd9Sstevel@tonic-gate 	mr.map_obj.rp = pcmcia_cons_regspec(dip,
10597c478bd9Sstevel@tonic-gate 	    (check == PCA_RES_NEED_IO) ?
10607c478bd9Sstevel@tonic-gate 	    PCMCIA_MAP_IO : PCMCIA_MAP_MEM,
10617c478bd9Sstevel@tonic-gate 	    regbuf, &ret);
10627c478bd9Sstevel@tonic-gate 	switch (mp->map_op) {
10637c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
10647c478bd9Sstevel@tonic-gate 		pcmcia_set_assigned(rdip, rnum, NULL);
10657c478bd9Sstevel@tonic-gate 		break;
10667c478bd9Sstevel@tonic-gate 	default:
10677c478bd9Sstevel@tonic-gate 		break;
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, &mr, (off_t)0, (off_t)0, vaddrp));
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate /*
10737c478bd9Sstevel@tonic-gate  * pcmcia_cons_regspec()
10747c478bd9Sstevel@tonic-gate  * based on parent's bus type, construct a regspec that is usable
10757c478bd9Sstevel@tonic-gate  * by that parent to map the resource into the system.
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate #define	PTYPE_PCI	1
10787c478bd9Sstevel@tonic-gate #define	PTYPE_ISA	0
10797c478bd9Sstevel@tonic-gate struct regspec *
10807c478bd9Sstevel@tonic-gate pcmcia_cons_regspec(dev_info_t *dip, int type, uchar_t *buff, ra_return_t *ret)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	int ptype = -1, len, bus;
10837c478bd9Sstevel@tonic-gate 	char device_type[MODMAXNAMELEN + 1];
10847c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
10857c478bd9Sstevel@tonic-gate 	struct regspec *defreg;
10867c478bd9Sstevel@tonic-gate 	pci_regspec_t *pcireg;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
10897c478bd9Sstevel@tonic-gate 	if (pdip != ddi_root_node()) {
10907c478bd9Sstevel@tonic-gate 		/* we're not a child of root so find out what */
10917c478bd9Sstevel@tonic-gate 		len = sizeof (device_type);
10927c478bd9Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, pdip, PROP_LEN_AND_VAL_BUF, 0,
10937c478bd9Sstevel@tonic-gate 			"device_type", (caddr_t)device_type, &len) ==
10947c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
10957c478bd9Sstevel@tonic-gate 			/* check things out */
10967c478bd9Sstevel@tonic-gate 			if (strcmp(device_type, "pci") == 0)
10977c478bd9Sstevel@tonic-gate 				ptype = PTYPE_PCI;
10987c478bd9Sstevel@tonic-gate 			else if (strcmp(device_type, "isa") == 0)
10997c478bd9Sstevel@tonic-gate 				ptype = PTYPE_ISA;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 	switch (ptype) {
11037c478bd9Sstevel@tonic-gate 	case PTYPE_PCI:
11047c478bd9Sstevel@tonic-gate 		/* XXX need to look at carefully */
1105a3282898Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
11067c478bd9Sstevel@tonic-gate 			"reg", (caddr_t)&pcireg, &len) == DDI_SUCCESS) {
11077c478bd9Sstevel@tonic-gate 			bus = PCI_REG_BUS_G(pcireg->pci_phys_hi);
11087c478bd9Sstevel@tonic-gate 			kmem_free(pcireg, len);
11097c478bd9Sstevel@tonic-gate 		} else {
11107c478bd9Sstevel@tonic-gate 			bus = 0;
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		pcireg = (pci_regspec_t *)buff;
11137c478bd9Sstevel@tonic-gate 		pcireg->pci_phys_hi = (type == PCMCIA_MAP_IO ? PCI_ADDR_IO :
11147c478bd9Sstevel@tonic-gate 			PCI_ADDR_MEM32) | PCI_RELOCAT_B | (bus << 16);
11157c478bd9Sstevel@tonic-gate 		pcireg->pci_phys_mid = ret->ra_addr_hi;
11167c478bd9Sstevel@tonic-gate 		pcireg->pci_phys_low = ret->ra_addr_lo;
11177c478bd9Sstevel@tonic-gate 		if (type == PCMCIA_MAP_IO)
11187c478bd9Sstevel@tonic-gate 			pcireg->pci_phys_low &= 0xFFFF;
11197c478bd9Sstevel@tonic-gate 		pcireg->pci_size_hi = 0;
11207c478bd9Sstevel@tonic-gate 		pcireg->pci_size_low = ret->ra_len;
11217c478bd9Sstevel@tonic-gate 		break;
11227c478bd9Sstevel@tonic-gate 	default:
11237c478bd9Sstevel@tonic-gate 		/* default case is to use struct regspec */
11247c478bd9Sstevel@tonic-gate 		defreg = (struct regspec *)buff;
11257c478bd9Sstevel@tonic-gate 		defreg->regspec_bustype = type == PCMCIA_MAP_IO ? 1 : 0;
11267c478bd9Sstevel@tonic-gate 		defreg->regspec_addr = ret->ra_addr_lo;
11277c478bd9Sstevel@tonic-gate 		defreg->regspec_size = ret->ra_len;
11287c478bd9Sstevel@tonic-gate 		break;
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 	return ((struct regspec *)buff);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * pcmcia_init_adapter
11357c478bd9Sstevel@tonic-gate  *	Initialize the per-adapter structures and check to see if
11367c478bd9Sstevel@tonic-gate  *	there are possible other instances coming.
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate void
11397c478bd9Sstevel@tonic-gate pcmcia_init_adapter(anp_t *adapter, dev_info_t *dip)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	int i, n;
11427c478bd9Sstevel@tonic-gate 	pcmcia_if_t *ls_if;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	i = pcmcia_num_adapters++;
11457c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i] = kmem_zalloc(sizeof (struct pcmcia_adapter),
11467c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
11477c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_dip = dip;
11487c478bd9Sstevel@tonic-gate 	/* should this be pca_winshift??? */
11497c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_module = ddi_name_to_major(ddi_get_name(dip));
11507c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_unit = ddi_get_instance(dip);
11517c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_iblock = adapter->an_iblock;
11527c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_idev = adapter->an_idev;
11537c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_if = ls_if = adapter->an_if;
11547c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->pca_number = i;
11557c478bd9Sstevel@tonic-gate 	(void) strcpy(pcmcia_adapters[i]->pca_name, ddi_get_name(dip));
11567c478bd9Sstevel@tonic-gate 	pcmcia_adapters[i]->
11577c478bd9Sstevel@tonic-gate 		pca_name[sizeof (pcmcia_adapters[i]->pca_name) - 1] = NULL;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	if (ls_if != NULL) {
11607c478bd9Sstevel@tonic-gate 		inquire_adapter_t conf;
11617c478bd9Sstevel@tonic-gate 		int sock, win;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		if (ls_if->pcif_inquire_adapter != NULL)
11647c478bd9Sstevel@tonic-gate 			GET_CONFIG(ls_if, dip, &conf);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		/* resources - assume worst case and fix from there */
11677c478bd9Sstevel@tonic-gate 		pcmcia_adapters[i]->pca_flags = PCA_RES_NEED_IRQ |
11687c478bd9Sstevel@tonic-gate 			PCA_RES_NEED_IO | PCA_RES_NEED_MEM;
11697c478bd9Sstevel@tonic-gate 		/* indicate first socket not initialized */
11707c478bd9Sstevel@tonic-gate 		pcmcia_adapters[i]->pca_first_socket = -1;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		if (conf.ResourceFlags & RES_OWN_IRQ)
11737c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IRQ;
11747c478bd9Sstevel@tonic-gate 		if (conf.ResourceFlags & RES_OWN_IO)
11757c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IO;
11767c478bd9Sstevel@tonic-gate 		if (conf.ResourceFlags & RES_OWN_MEM)
11777c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_MEM;
11787c478bd9Sstevel@tonic-gate 		if (conf.ResourceFlags & RES_IRQ_SHAREABLE)
11797c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SHAREABLE;
11807c478bd9Sstevel@tonic-gate 		if (conf.ResourceFlags & RES_IRQ_NEXUS)
11817c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SMI_SHARE;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		/* need to know interrupt limitations */
11847c478bd9Sstevel@tonic-gate 		if (conf.ActiveLow) {
11857c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveLow;
11867c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_ISA;
11877c478bd9Sstevel@tonic-gate 		} else
11887c478bd9Sstevel@tonic-gate 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveHigh;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 		/* power entries for adapter */
11917c478bd9Sstevel@tonic-gate 		pcmcia_adapters[i]->pca_power =
11927c478bd9Sstevel@tonic-gate 			conf.power_entry;
11937c478bd9Sstevel@tonic-gate 		pcmcia_adapters[i]->pca_numpower =
11947c478bd9Sstevel@tonic-gate 			conf.NumPower;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 		for (n = 0; n < conf.NumPower; n++)
11977c478bd9Sstevel@tonic-gate 			pcmcia_merge_power(&conf.power_entry[n]);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		/* now setup the per socket info */
12007c478bd9Sstevel@tonic-gate 		for (sock = 0; sock < conf.NumSockets;
12017c478bd9Sstevel@tonic-gate 		    sock++) {
12027c478bd9Sstevel@tonic-gate 			dev_info_t *sockdrv = NULL;
12037c478bd9Sstevel@tonic-gate 			sockdrv = pcmcia_number_socket(dip, sock);
12047c478bd9Sstevel@tonic-gate 			if (sockdrv == NULL)
12057c478bd9Sstevel@tonic-gate 				n = sock + pcmcia_num_sockets;
12067c478bd9Sstevel@tonic-gate 			else {
12077c478bd9Sstevel@tonic-gate 				n = ddi_get_instance(sockdrv);
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 			/* make sure we know first socket on adapter */
12107c478bd9Sstevel@tonic-gate 			if (pcmcia_adapters[i]->pca_first_socket == -1)
12117c478bd9Sstevel@tonic-gate 				pcmcia_adapters[i]->pca_first_socket = n;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 			/*
12147c478bd9Sstevel@tonic-gate 			 * the number of sockets is weird.
12157c478bd9Sstevel@tonic-gate 			 * we might have only two sockets but
12167c478bd9Sstevel@tonic-gate 			 * due to persistence of instances we
12177c478bd9Sstevel@tonic-gate 			 * will need to call them something other
12187c478bd9Sstevel@tonic-gate 			 * than 0 and 1.  So, we use the largest
12197c478bd9Sstevel@tonic-gate 			 * instance number as the number and
12207c478bd9Sstevel@tonic-gate 			 * have some that just don't get used.
12217c478bd9Sstevel@tonic-gate 			 */
12227c478bd9Sstevel@tonic-gate 			if (n >= pcmcia_num_sockets)
12237c478bd9Sstevel@tonic-gate 				pcmcia_num_sockets = n + 1;
12247c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
12257c478bd9Sstevel@tonic-gate 			if (pcmcia_debug) {
12267c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
12277c478bd9Sstevel@tonic-gate 					"pcmcia_init: new socket added %d "
12287c478bd9Sstevel@tonic-gate 					"(%d)\n",
12297c478bd9Sstevel@tonic-gate 					n, pcmcia_num_sockets);
12307c478bd9Sstevel@tonic-gate 			}
12317c478bd9Sstevel@tonic-gate #endif
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n] =
12347c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (pcmcia_logical_socket_t),
12357c478bd9Sstevel@tonic-gate 				KM_SLEEP);
12367c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_socket = sock;
12377c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_if = ls_if;
12387c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_adapter =
12397c478bd9Sstevel@tonic-gate 				pcmcia_adapters[i];
12407c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_cs_events = 0L;
12417c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_sockdrv = sockdrv;
12427c478bd9Sstevel@tonic-gate 			/* Prototype of intrspec */
12437c478bd9Sstevel@tonic-gate 			pcmcia_sockets[n]->ls_intr_pri =
12447c478bd9Sstevel@tonic-gate 				adapter->an_ipl;
12457c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
12467c478bd9Sstevel@tonic-gate 			if (pcmcia_debug)
12477c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
12487c478bd9Sstevel@tonic-gate 					"phys sock %d, log sock %d\n",
12497c478bd9Sstevel@tonic-gate 					sock, n);
12507c478bd9Sstevel@tonic-gate #endif
12517c478bd9Sstevel@tonic-gate 			mutex_init(&pcmcia_sockets[n]->ls_ilock, NULL,
12527c478bd9Sstevel@tonic-gate 				MUTEX_DRIVER, *adapter->an_iblock);
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		pcmcia_adapters[i]->pca_numsockets = conf.NumSockets;
12567c478bd9Sstevel@tonic-gate 		/* now setup the per window information */
12577c478bd9Sstevel@tonic-gate 		for (win = 0; win < conf.NumWindows; win++) {
12587c478bd9Sstevel@tonic-gate 			n = win + pcmcia_num_windows;
12597c478bd9Sstevel@tonic-gate 			pcmcia_windows[n] =
12607c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (pcmcia_logical_window_t),
12617c478bd9Sstevel@tonic-gate 				    KM_SLEEP);
12627c478bd9Sstevel@tonic-gate 			pcmcia_windows[n]->lw_window = win;
12637c478bd9Sstevel@tonic-gate 			pcmcia_windows[n]->lw_if = ls_if;
12647c478bd9Sstevel@tonic-gate 			pcmcia_windows[n]->lw_adapter =
12657c478bd9Sstevel@tonic-gate 				pcmcia_adapters[i];
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 		pcmcia_num_windows += conf.NumWindows;
12687c478bd9Sstevel@tonic-gate 		SET_CALLBACK(ls_if, dip,
12697c478bd9Sstevel@tonic-gate 		    pcm_adapter_callback, i);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		/* now tell CS about each socket */
12727c478bd9Sstevel@tonic-gate 		for (sock = 0; sock < pcmcia_num_sockets; sock++) {
12737c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
12747c478bd9Sstevel@tonic-gate 			if (pcmcia_debug) {
12757c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
12767c478bd9Sstevel@tonic-gate 					"pcmcia_init: notify CS socket %d "
12777c478bd9Sstevel@tonic-gate 					"sockp=%p\n",
12787c478bd9Sstevel@tonic-gate 					sock, (void *)pcmcia_sockets[sock]);
12797c478bd9Sstevel@tonic-gate 			}
12807c478bd9Sstevel@tonic-gate #endif
12817c478bd9Sstevel@tonic-gate 			if (pcmcia_sockets[sock] == NULL ||
12827c478bd9Sstevel@tonic-gate 			    (pcmcia_sockets[sock]->ls_flags &
12837c478bd9Sstevel@tonic-gate 				PCS_SOCKET_ADDED)) {
12847c478bd9Sstevel@tonic-gate 				/* skip the ones that are done already */
12857c478bd9Sstevel@tonic-gate 				continue;
12867c478bd9Sstevel@tonic-gate 			}
12877c478bd9Sstevel@tonic-gate 			pcmcia_sockets[sock]->ls_flags |= PCS_SOCKET_ADDED;
12887c478bd9Sstevel@tonic-gate 			if (cs_event(PCE_ADD_SOCKET, sock, 0) !=
12897c478bd9Sstevel@tonic-gate 			    CS_SUCCESS) {
12907c478bd9Sstevel@tonic-gate 				/* flag socket as broken */
12917c478bd9Sstevel@tonic-gate 				pcmcia_sockets[sock]->ls_flags = 0;
12927c478bd9Sstevel@tonic-gate 			} else {
12937c478bd9Sstevel@tonic-gate 				pcm_event_manager(PCE_ADD_SOCKET,
12947c478bd9Sstevel@tonic-gate 				    sock, NULL);
12957c478bd9Sstevel@tonic-gate 			}
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
13007c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
13017c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "logical sockets:\n");
13027c478bd9Sstevel@tonic-gate 		for (i = 0; i < pcmcia_num_sockets; i++) {
13037c478bd9Sstevel@tonic-gate 			if (pcmcia_sockets[i] == NULL)
13047c478bd9Sstevel@tonic-gate 				continue;
13057c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
13067c478bd9Sstevel@tonic-gate 				"\t%d: phys sock=%d, if=%p, adapt=%p\n",
13077c478bd9Sstevel@tonic-gate 				i, pcmcia_sockets[i]->ls_socket,
13087c478bd9Sstevel@tonic-gate 				(void *)pcmcia_sockets[i]->ls_if,
13097c478bd9Sstevel@tonic-gate 				(void *)pcmcia_sockets[i]->ls_adapter);
13107c478bd9Sstevel@tonic-gate 		}
13117c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "logical windows:\n");
13127c478bd9Sstevel@tonic-gate 		for (i = 0; i < pcmcia_num_windows; i++) {
13137c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
13147c478bd9Sstevel@tonic-gate 				"\t%d: phys_window=%d, if=%p, adapt=%p\n",
13157c478bd9Sstevel@tonic-gate 				i, pcmcia_windows[i]->lw_window,
13167c478bd9Sstevel@tonic-gate 				(void *)pcmcia_windows[i]->lw_if,
13177c478bd9Sstevel@tonic-gate 				(void *)pcmcia_windows[i]->lw_adapter);
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tpcmcia_num_power=%d\n", pcmcia_num_power);
13207c478bd9Sstevel@tonic-gate 		for (n = 0; n < pcmcia_num_power; n++)
13217c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
13227c478bd9Sstevel@tonic-gate 				"\t\tPowerLevel: %d\tValidSignals: %x\n",
13237c478bd9Sstevel@tonic-gate 				pcmcia_power_table[n].PowerLevel,
13247c478bd9Sstevel@tonic-gate 				pcmcia_power_table[n].ValidSignals);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate #endif
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /*
13307c478bd9Sstevel@tonic-gate  * pcmcia_enum_thread()
13317c478bd9Sstevel@tonic-gate  *
13327c478bd9Sstevel@tonic-gate  *	This thread is scheduled by pcmcia_attach() to enumerate the
13337c478bd9Sstevel@tonic-gate  *	cards present in an adapter at boot time.
13347c478bd9Sstevel@tonic-gate  *	pcmcia_enum_lock is used to serialize the execution of this
13357c478bd9Sstevel@tonic-gate  *	thread with pcmcia_attach().
13367c478bd9Sstevel@tonic-gate  */
13377c478bd9Sstevel@tonic-gate static void
13387c478bd9Sstevel@tonic-gate pcmcia_enum_thread(anp_t *adapter)
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate 	mutex_enter(&pcmcia_enum_lock);
13417c478bd9Sstevel@tonic-gate 	pcmcia_find_cards(adapter);
13427c478bd9Sstevel@tonic-gate 	mutex_exit(&pcmcia_enum_lock);
13437c478bd9Sstevel@tonic-gate 	thread_exit();
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * pcmcia_find_cards()
13487c478bd9Sstevel@tonic-gate  *	check the adapter to see if there are cards present at
13497c478bd9Sstevel@tonic-gate  *	driver attach time.  If there are, generate an artificial
13507c478bd9Sstevel@tonic-gate  *	card insertion event to get CS running and the PC Card ultimately
13517c478bd9Sstevel@tonic-gate  *	identified.
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate void
13547c478bd9Sstevel@tonic-gate pcmcia_find_cards(anp_t *adapt)
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	int i;
13577c478bd9Sstevel@tonic-gate 	get_ss_status_t status;
13587c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_sockets; i++) {
13597c478bd9Sstevel@tonic-gate 		if (pcmcia_sockets[i] &&
13607c478bd9Sstevel@tonic-gate 		    pcmcia_sockets[i]->ls_if == adapt->an_if) {
13617c478bd9Sstevel@tonic-gate 			/* check the status */
13627c478bd9Sstevel@tonic-gate 			status.socket = i;
13637c478bd9Sstevel@tonic-gate 			if (SSGetStatus(&status) == SUCCESS &&
13647c478bd9Sstevel@tonic-gate 			    status.CardState & SBM_CD &&
13657c478bd9Sstevel@tonic-gate 			    pcmcia_sockets[i]->ls_dip[0] == NULL) {
13667c478bd9Sstevel@tonic-gate 				(void) cs_event(PCE_CARD_INSERT, i, 0);
13677c478bd9Sstevel@tonic-gate 				delay(1);
13687c478bd9Sstevel@tonic-gate 			}
13697c478bd9Sstevel@tonic-gate 		}
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate /*
13747c478bd9Sstevel@tonic-gate  * pcmcia_number_socket(dip, adapt)
13757c478bd9Sstevel@tonic-gate  *	we determine socket number by creating a driver for each
13767c478bd9Sstevel@tonic-gate  *	socket on the adapter and then forcing it to attach.  This
13777c478bd9Sstevel@tonic-gate  *	results in an instance being assigned which becomes the
13787c478bd9Sstevel@tonic-gate  *	logical socket number.	If it fails, then we are the first
13797c478bd9Sstevel@tonic-gate  *	set of sockets and renumbering occurs later.  We do this
13807c478bd9Sstevel@tonic-gate  *	one socket at a time and return the dev_info_t so the
13817c478bd9Sstevel@tonic-gate  *	instance number can be used.
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate dev_info_t *
13847c478bd9Sstevel@tonic-gate pcmcia_number_socket(dev_info_t *dip, int localsocket)
13857c478bd9Sstevel@tonic-gate {
13867c478bd9Sstevel@tonic-gate 	dev_info_t *child = NULL;
13877c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
13887c478bd9Sstevel@tonic-gate 
1389fa9e4066Sahrens 	if (ndi_devi_alloc(dip, "pcs", (pnode_t)DEVI_SID_NODEID,
13907c478bd9Sstevel@tonic-gate 	    &child) == NDI_SUCCESS) {
13917c478bd9Sstevel@tonic-gate 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
13927c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
13937c478bd9Sstevel@tonic-gate 		ppd->ppd_reg = kmem_zalloc(sizeof (struct pcm_regs), KM_SLEEP);
13947c478bd9Sstevel@tonic-gate 		ppd->ppd_nreg = 1;
13957c478bd9Sstevel@tonic-gate 		ppd->ppd_reg[0].phys_hi = localsocket;
13967c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, (caddr_t)ppd);
13977c478bd9Sstevel@tonic-gate 		if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
13987c478bd9Sstevel@tonic-gate 			(void) ndi_devi_free(child);
13997c478bd9Sstevel@tonic-gate 			child = NULL;
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 	return (child);
14037c478bd9Sstevel@tonic-gate }
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate /*
14067c478bd9Sstevel@tonic-gate  * pcm_phys_to_log_socket()
14077c478bd9Sstevel@tonic-gate  *	from an adapter and socket number return the logical socket
14087c478bd9Sstevel@tonic-gate  */
14097c478bd9Sstevel@tonic-gate int
14107c478bd9Sstevel@tonic-gate pcm_phys_to_log_socket(struct pcmcia_adapter *adapt, int socket)
14117c478bd9Sstevel@tonic-gate {
14127c478bd9Sstevel@tonic-gate 	register pcmcia_logical_socket_t *sockp;
14137c478bd9Sstevel@tonic-gate 	int i;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	for (i = 0, sockp = pcmcia_sockets[0];
14167c478bd9Sstevel@tonic-gate 		i < pcmcia_num_sockets; i++, sockp = pcmcia_sockets[i]) {
14177c478bd9Sstevel@tonic-gate 		if (sockp == NULL)
14187c478bd9Sstevel@tonic-gate 			continue;
14197c478bd9Sstevel@tonic-gate 		if (sockp->ls_socket == socket && sockp->ls_adapter == adapt)
14207c478bd9Sstevel@tonic-gate 			break;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	if (i >= pcmcia_num_sockets) {
14237c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
14247c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
14257c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
14267c478bd9Sstevel@tonic-gate 				"\tbad socket/adapter: %x/%p != %x/%x\n",
14277c478bd9Sstevel@tonic-gate 				socket, (void *)adapt, pcmcia_num_sockets,
14287c478bd9Sstevel@tonic-gate 				pcmcia_num_adapters);
14297c478bd9Sstevel@tonic-gate #endif
14307c478bd9Sstevel@tonic-gate 		return (-1);
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	return (i);		/* want logical socket */
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate /*
14377c478bd9Sstevel@tonic-gate  * pcm_adapter_callback()
14387c478bd9Sstevel@tonic-gate  *	this function is called back by the adapter driver at interrupt time.
14397c478bd9Sstevel@tonic-gate  *	It is here that events should get generated for the event manager if it
14407c478bd9Sstevel@tonic-gate  *	is present.  It would also be the time where a device information
14417c478bd9Sstevel@tonic-gate  *	tree could be constructed for a card that was added in if we
14427c478bd9Sstevel@tonic-gate  *	choose to create them dynamically.
14437c478bd9Sstevel@tonic-gate  */
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
14467c478bd9Sstevel@tonic-gate char *cblist[] = {
14477c478bd9Sstevel@tonic-gate 	"removal",
14487c478bd9Sstevel@tonic-gate 	"insert",
14497c478bd9Sstevel@tonic-gate 	"ready",
14507c478bd9Sstevel@tonic-gate 	"battery-warn",
14517c478bd9Sstevel@tonic-gate 	"battery-dead",
14527c478bd9Sstevel@tonic-gate 	"status-change",
14537c478bd9Sstevel@tonic-gate 	"write-protect", "reset", "unlock", "client-info", "eject-complete",
14547c478bd9Sstevel@tonic-gate 	"eject-request", "erase-complete", "exclusive-complete",
14557c478bd9Sstevel@tonic-gate 	"exclusive-request", "insert-complete", "insert-request",
14567c478bd9Sstevel@tonic-gate 	"reset-complete", "reset-request", "timer-expired",
14577c478bd9Sstevel@tonic-gate 	"resume", "suspend"
14587c478bd9Sstevel@tonic-gate };
14597c478bd9Sstevel@tonic-gate #endif
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14627c478bd9Sstevel@tonic-gate static int
14637c478bd9Sstevel@tonic-gate pcm_adapter_callback(dev_info_t *dip, int adapter, int event, int socket)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
14687c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
14697c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcm_adapter_callback: %p %x %x %x: ",
14707c478bd9Sstevel@tonic-gate 			(void *)dip, adapter, event, socket);
14717c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "[%s]\n", cblist[event]);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate #endif
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	if (adapter >= pcmcia_num_adapters || adapter < 0) {
14767c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
14777c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
14787c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tbad adapter number: %d : %d\n",
14797c478bd9Sstevel@tonic-gate 				adapter, pcmcia_num_adapters);
14807c478bd9Sstevel@tonic-gate #endif
14817c478bd9Sstevel@tonic-gate 		return (1);
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/* get the logical socket since that is what CS knows */
14857c478bd9Sstevel@tonic-gate 	socket = pcm_phys_to_log_socket(pcmcia_adapters[adapter], socket);
14867c478bd9Sstevel@tonic-gate 	if (socket == -1) {
14877c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcmcia callback - bad logical socket\n");
14887c478bd9Sstevel@tonic-gate 		return (0);
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
14917c478bd9Sstevel@tonic-gate 	switch (event) {
14927c478bd9Sstevel@tonic-gate 	case -1:		/* special case of adapter going away */
14937c478bd9Sstevel@tonic-gate 	case PCE_CARD_INSERT:
14947c478bd9Sstevel@tonic-gate 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
14957c478bd9Sstevel@tonic-gate 			PCE_E2M(PCE_CARD_REMOVAL);
14967c478bd9Sstevel@tonic-gate 		break;
14977c478bd9Sstevel@tonic-gate 	case PCE_CARD_REMOVAL:
14987c478bd9Sstevel@tonic-gate 				/* disable interrupts at this point */
14997c478bd9Sstevel@tonic-gate 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
15007c478bd9Sstevel@tonic-gate 			PCE_E2M(PCE_CARD_REMOVAL);
15017c478bd9Sstevel@tonic-gate 		/* remove children that never attached */
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		break;
15047c478bd9Sstevel@tonic-gate 	case PCE_PM_RESUME:
15057c478bd9Sstevel@tonic-gate 		pcmcia_do_resume(socket, sockp);
15067c478bd9Sstevel@tonic-gate 		/* event = PCE_CARD_INSERT; */
15077c478bd9Sstevel@tonic-gate 		break;
15087c478bd9Sstevel@tonic-gate 	case PCE_PM_SUSPEND:
15097c478bd9Sstevel@tonic-gate 		pcmcia_do_suspend(socket, sockp);
15107c478bd9Sstevel@tonic-gate 		/* event = PCE_CARD_REMOVAL; */
15117c478bd9Sstevel@tonic-gate 		break;
15127c478bd9Sstevel@tonic-gate 	default:
15137c478bd9Sstevel@tonic-gate 		/* nothing to do */
15147c478bd9Sstevel@tonic-gate 		break;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
15187c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
15197c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
15207c478bd9Sstevel@tonic-gate 			"\tevent %d, event mask=%x, match=%x (log socket=%d)\n",
15217c478bd9Sstevel@tonic-gate 			event,
15227c478bd9Sstevel@tonic-gate 			(int)sockp->ls_cs_events,
15237c478bd9Sstevel@tonic-gate 			(int)(sockp->ls_cs_events & PCE_E2M(event)), socket);
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate #endif
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (pcmcia_cs_event && sockp->ls_cs_events & (1 << event)) {
15287c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
15297c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
15307c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tcalling CS event handler (%p) "
15317c478bd9Sstevel@tonic-gate 				"with event=%d\n",
15327c478bd9Sstevel@tonic-gate 				(void *)pcmcia_cs_event, event);
15337c478bd9Sstevel@tonic-gate #endif
15347c478bd9Sstevel@tonic-gate 		CS_EVENT(event, socket, 0);
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/* let the event manager(s) know about the event */
15387c478bd9Sstevel@tonic-gate 	pcm_event_manager(event, socket, NULL);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	return (0);
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate /*
15447c478bd9Sstevel@tonic-gate  * pcm_event_manager()
15457c478bd9Sstevel@tonic-gate  *	checks for registered management driver callback handlers
15467c478bd9Sstevel@tonic-gate  *	if there are any, call them if the event warrants it
15477c478bd9Sstevel@tonic-gate  */
15487c478bd9Sstevel@tonic-gate void
15497c478bd9Sstevel@tonic-gate pcm_event_manager(int event, int socket, void *arg)
15507c478bd9Sstevel@tonic-gate {
15517c478bd9Sstevel@tonic-gate 	struct pcmcia_mif *mif;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	for (mif = pcmcia_mif_handlers; mif != NULL; mif = mif->mif_next) {
15547c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
15557c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
15567c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
15577c478bd9Sstevel@tonic-gate 				"pcm_event_manager: event=%d, mif_events=%x"
15587c478bd9Sstevel@tonic-gate 				" (tst:%d)\n",
15597c478bd9Sstevel@tonic-gate 				event, (int)*(uint32_t *)mif->mif_events,
15607c478bd9Sstevel@tonic-gate 				PR_GET(mif->mif_events, event));
15617c478bd9Sstevel@tonic-gate #endif
15627c478bd9Sstevel@tonic-gate 		if (PR_GET(mif->mif_events, event)) {
15637c478bd9Sstevel@tonic-gate 			mif->mif_function(mif->mif_id, event, socket, arg);
15647c478bd9Sstevel@tonic-gate 		}
15657c478bd9Sstevel@tonic-gate 	}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate }
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate /*
15707c478bd9Sstevel@tonic-gate  * pcm_search_devinfo(dev_info_t *, pcm_device_info *, int)
15717c478bd9Sstevel@tonic-gate  * search for an immediate child node to the nexus and not siblings of nexus
15727c478bd9Sstevel@tonic-gate  * and not grandchildren.  We follow the same sequence that name binding
15737c478bd9Sstevel@tonic-gate  * follows so we match same class of device (modem == modem) and don't
15747c478bd9Sstevel@tonic-gate  * have to depend on features that might not exist.
15757c478bd9Sstevel@tonic-gate  */
15767c478bd9Sstevel@tonic-gate dev_info_t *
15777c478bd9Sstevel@tonic-gate pcm_search_devinfo(dev_info_t *self, struct pcm_device_info *info, int socket)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	char bf[256];
15807c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
15817c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
15827c478bd9Sstevel@tonic-gate 	int circular;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
15857c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
15867c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
15877c478bd9Sstevel@tonic-gate 		    "pcm_search_devinfo: socket=%x [%s|%s|%s] pd_flags=%x\n",
15887c478bd9Sstevel@tonic-gate 		    socket, info->pd_bind_name, info->pd_generic_name,
15897c478bd9Sstevel@tonic-gate 		    info->pd_vers1_name, info->pd_flags);
15907c478bd9Sstevel@tonic-gate #endif
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	ndi_devi_enter(self, &circular);
15937c478bd9Sstevel@tonic-gate 	/* do searches in compatible property order */
15947c478bd9Sstevel@tonic-gate 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
15957c478bd9Sstevel@tonic-gate 	    dip != NULL;
15967c478bd9Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
15977c478bd9Sstevel@tonic-gate 		int ppd_socket;
15987c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
15997c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(dip);
16007c478bd9Sstevel@tonic-gate 		if (ppd == NULL) {
16017c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
16027c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "No parent private data\n");
16037c478bd9Sstevel@tonic-gate #endif
16047c478bd9Sstevel@tonic-gate 			continue;
16057c478bd9Sstevel@tonic-gate 		}
16067c478bd9Sstevel@tonic-gate 		ppd_socket = CS_MAKE_SOCKET_NUMBER(ppd->ppd_socket,
16077c478bd9Sstevel@tonic-gate 		    ppd->ppd_function);
16087c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
16097c478bd9Sstevel@tonic-gate 		if (pcmcia_debug) {
16107c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tbind=[%s], node=[%s]\n",
16117c478bd9Sstevel@tonic-gate 				DEVI(dip)->devi_binding_name,
16127c478bd9Sstevel@tonic-gate 				DEVI(dip)->devi_node_name);
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate #endif
16157c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NAME_VERS1) {
16167c478bd9Sstevel@tonic-gate 			(void) strcpy(bf, info->pd_vers1_name);
16177c478bd9Sstevel@tonic-gate 			pcmcia_fix_string(bf);
16187c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_binding_name &&
16197c478bd9Sstevel@tonic-gate 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
16207c478bd9Sstevel@tonic-gate 			    socket == ppd_socket)
16217c478bd9Sstevel@tonic-gate 				break;
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 		if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
16247c478bd9Sstevel@tonic-gate 		    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
16257c478bd9Sstevel@tonic-gate 			(void) sprintf(bf, "%s,%x", info->pd_bind_name,
16267c478bd9Sstevel@tonic-gate 				info->pd_function);
16277c478bd9Sstevel@tonic-gate 			if (strcmp(bf, DEVI(dip)->devi_binding_name) == 0 &&
16287c478bd9Sstevel@tonic-gate 			    socket == ppd->ppd_socket)
16297c478bd9Sstevel@tonic-gate 				break;
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NAME_1275) {
16327c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_binding_name &&
16337c478bd9Sstevel@tonic-gate 			    strcmp(DEVI(dip)->devi_binding_name,
16347c478bd9Sstevel@tonic-gate 				info->pd_bind_name) == 0 &&
16357c478bd9Sstevel@tonic-gate 			    socket == ppd_socket)
16367c478bd9Sstevel@tonic-gate 				break;
16377c478bd9Sstevel@tonic-gate 		}
16387c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NAME_GENERIC) {
16397c478bd9Sstevel@tonic-gate 			(void) sprintf(bf, "%s,%s", PCMDEV_NAMEPREF,
16407c478bd9Sstevel@tonic-gate 				info->pd_generic_name);
16417c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_binding_name &&
16427c478bd9Sstevel@tonic-gate 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
16437c478bd9Sstevel@tonic-gate 			    socket == ppd_socket)
16447c478bd9Sstevel@tonic-gate 				break;
16457c478bd9Sstevel@tonic-gate 		}
16467c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NAME_GENERIC) {
16477c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_binding_name &&
16487c478bd9Sstevel@tonic-gate 			    strcmp(DEVI(dip)->devi_binding_name,
16497c478bd9Sstevel@tonic-gate 				info->pd_generic_name) == 0 &&
16507c478bd9Sstevel@tonic-gate 			    socket == ppd_socket)
16517c478bd9Sstevel@tonic-gate 				break;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NO_CONFIG) {
16547c478bd9Sstevel@tonic-gate 			if (DEVI(dip)->devi_binding_name &&
16557c478bd9Sstevel@tonic-gate 			    strcmp(DEVI(dip)->devi_binding_name,
16567c478bd9Sstevel@tonic-gate 					"pccard,memory") == 0 &&
16577c478bd9Sstevel@tonic-gate 			    socket == ppd_socket)
16587c478bd9Sstevel@tonic-gate 				break;
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	ndi_devi_exit(self, circular);
16627c478bd9Sstevel@tonic-gate 	return (dip);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate /*
16667c478bd9Sstevel@tonic-gate  * pcm_find_devinfo()
16677c478bd9Sstevel@tonic-gate  *	this is a wrapper around DDI calls to "find" any
16687c478bd9Sstevel@tonic-gate  *	devinfo node and then from there find the one associated
16697c478bd9Sstevel@tonic-gate  *	with the socket
16707c478bd9Sstevel@tonic-gate  */
16717c478bd9Sstevel@tonic-gate dev_info_t *
16727c478bd9Sstevel@tonic-gate pcm_find_devinfo(dev_info_t *pdip, struct pcm_device_info *info, int socket)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	dip = pcm_search_devinfo(pdip, info, socket);
16777c478bd9Sstevel@tonic-gate 	if (dip == NULL)
16787c478bd9Sstevel@tonic-gate 		return (NULL);
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * we have at least a base level dip
16817c478bd9Sstevel@tonic-gate 	 * see if there is one (this or a sibling)
16827c478bd9Sstevel@tonic-gate 	 * that has the correct socket number
16837c478bd9Sstevel@tonic-gate 	 * if there is, return that one else
16847c478bd9Sstevel@tonic-gate 	 * NULL so a new one is created
16857c478bd9Sstevel@tonic-gate 	 */
16867c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
16877c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
16887c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "find: initial dip = %p, socket=%d, name=%s "
16897c478bd9Sstevel@tonic-gate 			"(instance=%d, socket=%d, name=%s)\n",
16907c478bd9Sstevel@tonic-gate 			(void *)dip, socket, info->pd_bind_name,
16917c478bd9Sstevel@tonic-gate 			ddi_get_instance(dip),
16927c478bd9Sstevel@tonic-gate 			ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
16937c478bd9Sstevel@tonic-gate 			PCM_DEV_SOCKET, -1),
16947c478bd9Sstevel@tonic-gate 			ddi_get_name(dip));
16957c478bd9Sstevel@tonic-gate #endif
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
16987c478bd9Sstevel@tonic-gate 	if (pcmcia_debug && dip != NULL)
16997c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\treturning non-NULL dip (%s)\n",
17007c478bd9Sstevel@tonic-gate 			ddi_get_name(dip));
17017c478bd9Sstevel@tonic-gate #endif
17027c478bd9Sstevel@tonic-gate 	return (dip);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /*
17067c478bd9Sstevel@tonic-gate  * pcm_find_parent_dip(socket)
17077c478bd9Sstevel@tonic-gate  *	find the correct parent dip for this logical socket
17087c478bd9Sstevel@tonic-gate  */
17097c478bd9Sstevel@tonic-gate dev_info_t *
17107c478bd9Sstevel@tonic-gate pcm_find_parent_dip(int socket)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	if ((socket < 0 || socket >= pcmcia_num_sockets) ||
17137c478bd9Sstevel@tonic-gate 	    pcmcia_sockets[socket] == NULL)
17147c478bd9Sstevel@tonic-gate 		return (NULL);
17157c478bd9Sstevel@tonic-gate 	return (pcmcia_sockets[socket]->ls_adapter->pca_dip);
17167c478bd9Sstevel@tonic-gate }
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate /*
17197c478bd9Sstevel@tonic-gate  * pcmcia_set_em_handler()
17207c478bd9Sstevel@tonic-gate  *	This is called by the management and event driver to tell
17217c478bd9Sstevel@tonic-gate  *	the nexus what to call.	 Multiple drivers are allowed
17227c478bd9Sstevel@tonic-gate  *	but normally only one will exist.
17237c478bd9Sstevel@tonic-gate  */
17247c478bd9Sstevel@tonic-gate int
17257c478bd9Sstevel@tonic-gate pcmcia_set_em_handler(int (*handler)(), caddr_t events, int elen,
17267c478bd9Sstevel@tonic-gate 			uint32_t id, void **cs, void **ss)
17277c478bd9Sstevel@tonic-gate {
17287c478bd9Sstevel@tonic-gate 	struct pcmcia_mif *mif, *tmp;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	if (handler == NULL) {
17317c478bd9Sstevel@tonic-gate 		/* NULL means remove the handler based on the ID */
17327c478bd9Sstevel@tonic-gate 		if (pcmcia_mif_handlers == NULL)
17337c478bd9Sstevel@tonic-gate 			return (0);
17347c478bd9Sstevel@tonic-gate 		mutex_enter(&pcmcia_global_lock);
17357c478bd9Sstevel@tonic-gate 		if (pcmcia_mif_handlers->mif_id == id) {
17367c478bd9Sstevel@tonic-gate 			mif = pcmcia_mif_handlers;
17377c478bd9Sstevel@tonic-gate 			pcmcia_mif_handlers = mif->mif_next;
17387c478bd9Sstevel@tonic-gate 			kmem_free(mif, sizeof (struct pcmcia_mif));
17397c478bd9Sstevel@tonic-gate 		} else {
17407c478bd9Sstevel@tonic-gate 			for (mif = pcmcia_mif_handlers;
17417c478bd9Sstevel@tonic-gate 			    mif->mif_next != NULL &&
17427c478bd9Sstevel@tonic-gate 			    mif->mif_next->mif_id != id;
17437c478bd9Sstevel@tonic-gate 			    mif = mif->mif_next)
17447c478bd9Sstevel@tonic-gate 				;
17457c478bd9Sstevel@tonic-gate 			if (mif->mif_next != NULL &&
17467c478bd9Sstevel@tonic-gate 			    mif->mif_next->mif_id == id) {
17477c478bd9Sstevel@tonic-gate 				tmp = mif->mif_next;
17487c478bd9Sstevel@tonic-gate 				mif->mif_next = tmp->mif_next;
17497c478bd9Sstevel@tonic-gate 				kmem_free(tmp, sizeof (struct pcmcia_mif));
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 		mutex_exit(&pcmcia_global_lock);
17537c478bd9Sstevel@tonic-gate 	} else {
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		if (pcmcia_num_adapters == 0) {
17567c478bd9Sstevel@tonic-gate 			return (ENXIO);
17577c478bd9Sstevel@tonic-gate 		}
17587c478bd9Sstevel@tonic-gate 		if (elen > EM_EVENTSIZE)
17597c478bd9Sstevel@tonic-gate 			return (EINVAL);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		mif = (struct pcmcia_mif *)
17627c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (struct pcmcia_mif),
17637c478bd9Sstevel@tonic-gate 			    KM_NOSLEEP);
17647c478bd9Sstevel@tonic-gate 		if (mif == NULL)
17657c478bd9Sstevel@tonic-gate 			return (ENOSPC);
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		mif->mif_function = (void (*)())handler;
17687c478bd9Sstevel@tonic-gate 		bcopy(events, mif->mif_events, elen);
17697c478bd9Sstevel@tonic-gate 		mif->mif_id = id;
17707c478bd9Sstevel@tonic-gate 		mutex_enter(&pcmcia_global_lock);
17717c478bd9Sstevel@tonic-gate 		mif->mif_next = pcmcia_mif_handlers;
17727c478bd9Sstevel@tonic-gate 		pcmcia_mif_handlers = mif;
17737c478bd9Sstevel@tonic-gate 		if (cs != NULL)
17747c478bd9Sstevel@tonic-gate 			*cs = (void *)pcmcia_card_services;
17757c478bd9Sstevel@tonic-gate 		if (ss != NULL) {
17767c478bd9Sstevel@tonic-gate 			*ss = (void *)SocketServices;
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 		mutex_exit(&pcmcia_global_lock);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 	return (0);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /*
17857c478bd9Sstevel@tonic-gate  * pcm_fix_bits(uchar_t *data, int num, int dir)
17867c478bd9Sstevel@tonic-gate  *	shift socket bits left(0) or right(0)
17877c478bd9Sstevel@tonic-gate  *	This is used when mapping logical and physical
17887c478bd9Sstevel@tonic-gate  */
17897c478bd9Sstevel@tonic-gate void
17907c478bd9Sstevel@tonic-gate pcm_fix_bits(socket_enum_t src, socket_enum_t dst, int num, int dir)
17917c478bd9Sstevel@tonic-gate {
17927c478bd9Sstevel@tonic-gate 	int i;
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	PR_ZERO(dst);
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if (dir == 0) {
17977c478bd9Sstevel@tonic-gate 				/* LEFT */
17987c478bd9Sstevel@tonic-gate 		for (i = 0; i <= (sizeof (dst) * PR_WORDSIZE) - num; i++) {
17997c478bd9Sstevel@tonic-gate 			if (PR_GET(src, i))
18007c478bd9Sstevel@tonic-gate 				PR_SET(dst, i + num);
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	} else {
18037c478bd9Sstevel@tonic-gate 				/* RIGHT */
18047c478bd9Sstevel@tonic-gate 		for (i = num; i < sizeof (dst) * PR_WORDSIZE; i++) {
18057c478bd9Sstevel@tonic-gate 			if (PR_GET(src, i))
18067c478bd9Sstevel@tonic-gate 			    PR_SET(dst, i - num);
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate uint32_t
18127c478bd9Sstevel@tonic-gate genmask(int len)
18137c478bd9Sstevel@tonic-gate {
18147c478bd9Sstevel@tonic-gate 	uint32_t mask;
18157c478bd9Sstevel@tonic-gate 	for (mask = 0; len > 0; len--) {
18167c478bd9Sstevel@tonic-gate 		mask |= 1 << (len - 1);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 	return (mask);
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate int
18227c478bd9Sstevel@tonic-gate genp2(int val)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	int i;
18257c478bd9Sstevel@tonic-gate 	if (val == 0)
18267c478bd9Sstevel@tonic-gate 		return (0);
18277c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++)
18287c478bd9Sstevel@tonic-gate 		if (val > (1 << i))
18297c478bd9Sstevel@tonic-gate 			return (i);
18307c478bd9Sstevel@tonic-gate 	return (0);
18317c478bd9Sstevel@tonic-gate }
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
18347c478bd9Sstevel@tonic-gate char *ssfuncs[128] = {
18357c478bd9Sstevel@tonic-gate 	"GetAdapter", "GetPage", "GetSocket", "GetStatus", "GetWindow",
18367c478bd9Sstevel@tonic-gate 	"InquireAdapter", "InquireSocket", "InquireWindow", "ResetSocket",
18377c478bd9Sstevel@tonic-gate 	"SetPage", "SetAdapter", "SetSocket", "SetWindow", "SetIRQHandler",
18387c478bd9Sstevel@tonic-gate 	"ClearIRQHandler",
18397c478bd9Sstevel@tonic-gate 	/* 15 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18407c478bd9Sstevel@tonic-gate 	/* 25 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18417c478bd9Sstevel@tonic-gate 	/* 35 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18427c478bd9Sstevel@tonic-gate 	/* 45 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18437c478bd9Sstevel@tonic-gate 	/* 55 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18447c478bd9Sstevel@tonic-gate 	/* 65 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18457c478bd9Sstevel@tonic-gate 	/* 75 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18467c478bd9Sstevel@tonic-gate 	/* 85 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
18477c478bd9Sstevel@tonic-gate 	/* 95 */ NULL, NULL, NULL,
18487c478bd9Sstevel@tonic-gate 	"CSIsActiveDip",
18497c478bd9Sstevel@tonic-gate 	"CSInitDev", "CSRegister", "CSCISInit", "CSUnregister",
18507c478bd9Sstevel@tonic-gate 	"CISGetAddress", "CISSetAddress", "CSCardRemoved", "CSGetCookiesAndDip"
18517c478bd9Sstevel@tonic-gate };
18527c478bd9Sstevel@tonic-gate #endif
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate /*
18557c478bd9Sstevel@tonic-gate  * SocketServices
18567c478bd9Sstevel@tonic-gate  *	general entrypoint for Card Services to find
18577c478bd9Sstevel@tonic-gate  *	Socket Services.  Finding the entry requires
18587c478bd9Sstevel@tonic-gate  *	a _depends_on[] relationship.
18597c478bd9Sstevel@tonic-gate  *
18607c478bd9Sstevel@tonic-gate  *	In some cases, the work is done locally but usually
18617c478bd9Sstevel@tonic-gate  *	the parameters are adjusted and the adapter driver
18627c478bd9Sstevel@tonic-gate  *	code asked to do the work.
18637c478bd9Sstevel@tonic-gate  */
18647c478bd9Sstevel@tonic-gate int
18657c478bd9Sstevel@tonic-gate SocketServices(int function, ...)
18667c478bd9Sstevel@tonic-gate {
18677c478bd9Sstevel@tonic-gate 	va_list arglist;
18687c478bd9Sstevel@tonic-gate 	uint32_t args[16];
18697c478bd9Sstevel@tonic-gate 	csregister_t *reg;
18707c478bd9Sstevel@tonic-gate 	sservice_t *serv;
18717c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
18727c478bd9Sstevel@tonic-gate 	int socket, func;
18737c478bd9Sstevel@tonic-gate 	int error = SUCCESS;
18747c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	va_start(arglist, function);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
18797c478bd9Sstevel@tonic-gate 	if (pcmcia_debug > 1)
18807c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "SocketServices called for function %d [%s]\n",
18817c478bd9Sstevel@tonic-gate 			function,
18827c478bd9Sstevel@tonic-gate 			((function < 128) && ssfuncs[function] != NULL) ?
18837c478bd9Sstevel@tonic-gate 			ssfuncs[function] : "UNKNOWN");
18847c478bd9Sstevel@tonic-gate #endif
18857c478bd9Sstevel@tonic-gate 	switch (function) {
18867c478bd9Sstevel@tonic-gate 	case CSRegister:
18877c478bd9Sstevel@tonic-gate 	case CISGetAddress:
18887c478bd9Sstevel@tonic-gate 	case CISSetAddress:
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		reg = va_arg(arglist, csregister_t *);
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 		if (reg->cs_magic != PCCS_MAGIC ||
18937c478bd9Sstevel@tonic-gate 		    reg->cs_version != PCCS_VERSION) {
18947c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
18957c478bd9Sstevel@tonic-gate 				"pcmcia: CSRegister (%x, %x, %p, %p) *ERROR*",
18967c478bd9Sstevel@tonic-gate 				reg->cs_magic, reg->cs_version,
18977c478bd9Sstevel@tonic-gate 				(void *)reg->cs_card_services,
18987c478bd9Sstevel@tonic-gate 				(void *)reg->cs_event);
18997c478bd9Sstevel@tonic-gate 			error = BAD_FUNCTION;
19007c478bd9Sstevel@tonic-gate 			break;
19017c478bd9Sstevel@tonic-gate 		}
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 		switch (function) {
19047c478bd9Sstevel@tonic-gate 		case CISGetAddress:
19057c478bd9Sstevel@tonic-gate 			reg->cs_event = pcmcia_cis_parser;
19067c478bd9Sstevel@tonic-gate 			break;
19077c478bd9Sstevel@tonic-gate 		case CISSetAddress:
19087c478bd9Sstevel@tonic-gate 			pcmcia_cis_parser = reg->cs_event;
19097c478bd9Sstevel@tonic-gate 			break;
19107c478bd9Sstevel@tonic-gate 		case CSRegister:
19117c478bd9Sstevel@tonic-gate 			break;
19127c478bd9Sstevel@tonic-gate 		}
19137c478bd9Sstevel@tonic-gate 		break;
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	case CSUnregister:
19167c478bd9Sstevel@tonic-gate 		break;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	case CSCISInit:
19197c478bd9Sstevel@tonic-gate 		args[0] = va_arg(arglist, int);
19207c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
19217c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
19227c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
19237c478bd9Sstevel@tonic-gate 				"CSCISInit: CIS is initialized on socket %d\n",
19247c478bd9Sstevel@tonic-gate 				(int)args[0]);
19257c478bd9Sstevel@tonic-gate #endif
19267c478bd9Sstevel@tonic-gate 		/*
19277c478bd9Sstevel@tonic-gate 		 * now that the CIS has been parsed (there may not
19287c478bd9Sstevel@tonic-gate 		 * be one but the work is done) we can create the
19297c478bd9Sstevel@tonic-gate 		 * device information structures.
19307c478bd9Sstevel@tonic-gate 		 *
19317c478bd9Sstevel@tonic-gate 		 * we serialize the node creation to avoid problems
19327c478bd9Sstevel@tonic-gate 		 * with initial probe/attach of nexi.
19337c478bd9Sstevel@tonic-gate 		 */
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		mutex_enter(&pcmcia_global_lock);
19367c478bd9Sstevel@tonic-gate 		pcmcia_create_dev_info(args[0]);
19377c478bd9Sstevel@tonic-gate 		cv_broadcast(&pcmcia_condvar); /* wakeup the nexus attach */
19387c478bd9Sstevel@tonic-gate 		mutex_exit(&pcmcia_global_lock);
19397c478bd9Sstevel@tonic-gate 		break;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	case CSInitDev:
19427c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
19437c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
19447c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "CSInitDev: initialize device\n");
19457c478bd9Sstevel@tonic-gate #endif
19467c478bd9Sstevel@tonic-gate 		/*
19477c478bd9Sstevel@tonic-gate 		 * this is where we create the /devices entries
19487c478bd9Sstevel@tonic-gate 		 * that let us out into the world
19497c478bd9Sstevel@tonic-gate 		 */
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		(void) pcmcia_create_device(va_arg(arglist,
19527c478bd9Sstevel@tonic-gate 		    ss_make_device_node_t *));
19537c478bd9Sstevel@tonic-gate 		break;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	case CSCardRemoved:
19567c478bd9Sstevel@tonic-gate 		args[0] = va_arg(arglist, uint32_t);
19577c478bd9Sstevel@tonic-gate 		socket = CS_GET_SOCKET_NUMBER(args[0]);
19587c478bd9Sstevel@tonic-gate 		func = CS_GET_FUNCTION_NUMBER(args[0]);
19597c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
19607c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
19617c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
19627c478bd9Sstevel@tonic-gate 				"CSCardRemoved! (socket=%d)\n", (int)args[0]);
19637c478bd9Sstevel@tonic-gate #endif
19647c478bd9Sstevel@tonic-gate 		if (socket >= pcmcia_num_sockets)
19657c478bd9Sstevel@tonic-gate 			break;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		sockp = pcmcia_sockets[socket];
19687c478bd9Sstevel@tonic-gate 		if (sockp == NULL) {
19697c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
19707c478bd9Sstevel@tonic-gate 			    "pcmcia: bad socket = %x", socket);
19717c478bd9Sstevel@tonic-gate 			break;
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 		for (func = 0; func < sockp->ls_functions; func++) {
19757c478bd9Sstevel@tonic-gate 			/*
19767c478bd9Sstevel@tonic-gate 			 * break the association of dip and socket
19777c478bd9Sstevel@tonic-gate 			 * for all functions on that socket
19787c478bd9Sstevel@tonic-gate 			 */
19797c478bd9Sstevel@tonic-gate 			dip = sockp->ls_dip[func];
19807c478bd9Sstevel@tonic-gate 			sockp->ls_dip[func] = NULL;
19817c478bd9Sstevel@tonic-gate 			if (dip != NULL) {
19827c478bd9Sstevel@tonic-gate 				struct pcmcia_parent_private *ppd;
19837c478bd9Sstevel@tonic-gate 				ppd = (struct pcmcia_parent_private *)
19847c478bd9Sstevel@tonic-gate 				    ddi_get_parent_data(dip);
19857c478bd9Sstevel@tonic-gate 				ppd->ppd_active = 0;
19867c478bd9Sstevel@tonic-gate 				(void) ndi_devi_offline(dip,
19877c478bd9Sstevel@tonic-gate 				    NDI_DEVI_REMOVE);
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
19907c478bd9Sstevel@tonic-gate 			else {
19917c478bd9Sstevel@tonic-gate 				if (pcmcia_debug)
19927c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT,
19937c478bd9Sstevel@tonic-gate 					    "CardRemoved: no "
19947c478bd9Sstevel@tonic-gate 					    "dip present "
19957c478bd9Sstevel@tonic-gate 					    "on socket %d!\n",
19967c478bd9Sstevel@tonic-gate 					    (int)args[0]);
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate #endif
19997c478bd9Sstevel@tonic-gate 		}
20007c478bd9Sstevel@tonic-gate 		if (sockp->ls_flags & PCS_SUSPENDED) {
20017c478bd9Sstevel@tonic-gate 			mutex_enter(&pcmcia_global_lock);
20027c478bd9Sstevel@tonic-gate 			sockp->ls_flags &= ~PCS_SUSPENDED;
20037c478bd9Sstevel@tonic-gate 			cv_broadcast(&pcmcia_condvar);
20047c478bd9Sstevel@tonic-gate 			mutex_exit(&pcmcia_global_lock);
20057c478bd9Sstevel@tonic-gate 		}
20067c478bd9Sstevel@tonic-gate 		break;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	case CSGetCookiesAndDip:
20097c478bd9Sstevel@tonic-gate 		serv = va_arg(arglist, sservice_t *);
20107c478bd9Sstevel@tonic-gate 		if (serv != NULL)
20117c478bd9Sstevel@tonic-gate 			error = GetCookiesAndDip(serv);
20127c478bd9Sstevel@tonic-gate 		else
20137c478bd9Sstevel@tonic-gate 			error = BAD_SOCKET;
20147c478bd9Sstevel@tonic-gate 		break;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	case CSGetActiveDip:
20177c478bd9Sstevel@tonic-gate 		/*
20187c478bd9Sstevel@tonic-gate 		 * get the dip associated with the card currently
20197c478bd9Sstevel@tonic-gate 		 * in the specified socket
20207c478bd9Sstevel@tonic-gate 		 */
20217c478bd9Sstevel@tonic-gate 		args[0] = va_arg(arglist, uint32_t);
20227c478bd9Sstevel@tonic-gate 		socket = CS_GET_SOCKET_NUMBER(args[0]);
20237c478bd9Sstevel@tonic-gate 		func = CS_GET_FUNCTION_NUMBER(args[0]);
20247c478bd9Sstevel@tonic-gate 		error = (long)pcmcia_sockets[socket]->ls_dip[func];
20257c478bd9Sstevel@tonic-gate 		break;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 		/*
20287c478bd9Sstevel@tonic-gate 		 * the remaining entries are SocketServices calls
20297c478bd9Sstevel@tonic-gate 		 */
20307c478bd9Sstevel@tonic-gate 	case SS_GetAdapter:
20317c478bd9Sstevel@tonic-gate 		error = SSGetAdapter(va_arg(arglist, get_adapter_t *));
20327c478bd9Sstevel@tonic-gate 		break;
20337c478bd9Sstevel@tonic-gate 	case SS_GetPage:
20347c478bd9Sstevel@tonic-gate 		error = SSGetPage(va_arg(arglist, get_page_t *));
20357c478bd9Sstevel@tonic-gate 		break;
20367c478bd9Sstevel@tonic-gate 	case SS_GetSocket:
20377c478bd9Sstevel@tonic-gate 		error = SSGetSocket(va_arg(arglist, get_socket_t *));
20387c478bd9Sstevel@tonic-gate 		break;
20397c478bd9Sstevel@tonic-gate 	case SS_GetStatus:
20407c478bd9Sstevel@tonic-gate 		error = SSGetStatus(va_arg(arglist, get_ss_status_t *));
20417c478bd9Sstevel@tonic-gate 		break;
20427c478bd9Sstevel@tonic-gate 	case SS_GetWindow:
20437c478bd9Sstevel@tonic-gate 		error = SSGetWindow(va_arg(arglist, get_window_t *));
20447c478bd9Sstevel@tonic-gate 		break;
20457c478bd9Sstevel@tonic-gate 	case SS_InquireAdapter:
20467c478bd9Sstevel@tonic-gate 		error = SSInquireAdapter(va_arg(arglist, inquire_adapter_t *));
20477c478bd9Sstevel@tonic-gate 		break;
20487c478bd9Sstevel@tonic-gate 	case SS_InquireSocket:
20497c478bd9Sstevel@tonic-gate 		error = SSInquireSocket(va_arg(arglist, inquire_socket_t *));
20507c478bd9Sstevel@tonic-gate 		break;
20517c478bd9Sstevel@tonic-gate 	case SS_InquireWindow:
20527c478bd9Sstevel@tonic-gate 		error = SSInquireWindow(va_arg(arglist, inquire_window_t *));
20537c478bd9Sstevel@tonic-gate 		break;
20547c478bd9Sstevel@tonic-gate 	case SS_ResetSocket:
20557c478bd9Sstevel@tonic-gate 		args[0] = va_arg(arglist, uint32_t);
20567c478bd9Sstevel@tonic-gate 		args[1] = va_arg(arglist, int);
20577c478bd9Sstevel@tonic-gate 		error = SSResetSocket(args[0], args[1]);
20587c478bd9Sstevel@tonic-gate 		break;
20597c478bd9Sstevel@tonic-gate 	case SS_SetPage:
20607c478bd9Sstevel@tonic-gate 		error = SSSetPage(va_arg(arglist, set_page_t *));
20617c478bd9Sstevel@tonic-gate 		break;
20627c478bd9Sstevel@tonic-gate 	case SS_SetSocket:
20637c478bd9Sstevel@tonic-gate 		error = SSSetSocket(va_arg(arglist, set_socket_t *));
20647c478bd9Sstevel@tonic-gate 		break;
20657c478bd9Sstevel@tonic-gate 	case SS_SetWindow:
20667c478bd9Sstevel@tonic-gate 		error = SSSetWindow(va_arg(arglist, set_window_t *));
20677c478bd9Sstevel@tonic-gate 		break;
20687c478bd9Sstevel@tonic-gate 	case SS_SetIRQHandler:
20697c478bd9Sstevel@tonic-gate 		error = SSSetIRQHandler(va_arg(arglist, set_irq_handler_t *));
20707c478bd9Sstevel@tonic-gate 		break;
20717c478bd9Sstevel@tonic-gate 	case SS_ClearIRQHandler:
20727c478bd9Sstevel@tonic-gate 		error = SSClearIRQHandler(va_arg(arglist,
20737c478bd9Sstevel@tonic-gate 		    clear_irq_handler_t *));
20747c478bd9Sstevel@tonic-gate 		break;
20757c478bd9Sstevel@tonic-gate 	default:
20767c478bd9Sstevel@tonic-gate 		error = BAD_FUNCTION;
20777c478bd9Sstevel@tonic-gate 		break;
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 	va_end(arglist);
20807c478bd9Sstevel@tonic-gate 	return (error);
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate /*
20847c478bd9Sstevel@tonic-gate  * pcmcia_merge_power()
20857c478bd9Sstevel@tonic-gate  *	The adapters may have different power tables so it
20867c478bd9Sstevel@tonic-gate  *	is necessary to construct a single power table that
20877c478bd9Sstevel@tonic-gate  *	can be used throughout the system.  The result is
20887c478bd9Sstevel@tonic-gate  *	a merger of all capabilities.  The nexus adds
20897c478bd9Sstevel@tonic-gate  *	power table entries one at a time.
20907c478bd9Sstevel@tonic-gate  */
20917c478bd9Sstevel@tonic-gate void
20927c478bd9Sstevel@tonic-gate pcmcia_merge_power(struct power_entry *power)
20937c478bd9Sstevel@tonic-gate {
20947c478bd9Sstevel@tonic-gate 	int i;
20957c478bd9Sstevel@tonic-gate 	struct power_entry pwr;
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	pwr = *power;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_power; i++) {
21007c478bd9Sstevel@tonic-gate 		if (pwr.PowerLevel == pcmcia_power_table[i].PowerLevel) {
21017c478bd9Sstevel@tonic-gate 			if (pwr.ValidSignals ==
21027c478bd9Sstevel@tonic-gate 				pcmcia_power_table[i].ValidSignals) {
21037c478bd9Sstevel@tonic-gate 				return;
21047c478bd9Sstevel@tonic-gate 			} else {
21057c478bd9Sstevel@tonic-gate 				/* partial match */
21067c478bd9Sstevel@tonic-gate 				pwr.ValidSignals &=
21077c478bd9Sstevel@tonic-gate 					~pcmcia_power_table[i].ValidSignals;
21087c478bd9Sstevel@tonic-gate 			}
21097c478bd9Sstevel@tonic-gate 		}
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 	/* what's left becomes a new entry */
21127c478bd9Sstevel@tonic-gate 	if (pcmcia_num_power == PCMCIA_MAX_POWER)
21137c478bd9Sstevel@tonic-gate 		return;
21147c478bd9Sstevel@tonic-gate 	pcmcia_power_table[pcmcia_num_power++] = pwr;
21157c478bd9Sstevel@tonic-gate }
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate  * pcmcia_do_suspend()
21197c478bd9Sstevel@tonic-gate  *	tell CS that a suspend has happened by passing a
21207c478bd9Sstevel@tonic-gate  *	card removal event.  Then cleanup the socket state
21217c478bd9Sstevel@tonic-gate  *	to fake the cards being removed so resume works
21227c478bd9Sstevel@tonic-gate  */
21237c478bd9Sstevel@tonic-gate void
21247c478bd9Sstevel@tonic-gate pcmcia_do_suspend(int socket, pcmcia_logical_socket_t *sockp)
21257c478bd9Sstevel@tonic-gate {
21267c478bd9Sstevel@tonic-gate 	get_ss_status_t stat;
21277c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter *adapt;
21287c478bd9Sstevel@tonic-gate 	pcmcia_if_t *ls_if;
21297c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
21307c478bd9Sstevel@tonic-gate 	int i;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate #ifdef	XXX
21337c478bd9Sstevel@tonic-gate 	if (pcmcia_cs_event == NULL) {
21347c478bd9Sstevel@tonic-gate 		return;
21357c478bd9Sstevel@tonic-gate 	}
21367c478bd9Sstevel@tonic-gate #endif
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	ls_if = sockp->ls_if;
21397c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
21427c478bd9Sstevel@tonic-gate 		return;
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	stat.socket = socket;
21467c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
21477c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
21487c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
21497c478bd9Sstevel@tonic-gate 			"pcmcia_do_suspend(%d, %p)\n", socket, (void *)sockp);
21507c478bd9Sstevel@tonic-gate 	}
21517c478bd9Sstevel@tonic-gate #endif
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) != SUCCESS)
21547c478bd9Sstevel@tonic-gate 		return;
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * If there is a card in the socket, then we need to send
21587c478bd9Sstevel@tonic-gate 	 *	everyone a PCE_CARD_REMOVAL event, and remove the
21597c478bd9Sstevel@tonic-gate 	 *	card active property.
21607c478bd9Sstevel@tonic-gate 	 */
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	for (i = 0; i < sockp->ls_functions; i++) {
21637c478bd9Sstevel@tonic-gate 		struct pcmcia_parent_private *ppd;
21647c478bd9Sstevel@tonic-gate 		dip = sockp->ls_dip[i];
21657c478bd9Sstevel@tonic-gate 		if (dip != NULL) {
21667c478bd9Sstevel@tonic-gate 			ppd = (struct pcmcia_parent_private *)
21677c478bd9Sstevel@tonic-gate 				ddi_get_parent_data(dip);
21687c478bd9Sstevel@tonic-gate 			ppd->ppd_flags |= PPD_SUSPENDED;
21697c478bd9Sstevel@tonic-gate 		}
21707c478bd9Sstevel@tonic-gate #if 0
21717c478bd9Sstevel@tonic-gate 		sockp->ls_dip[i] = NULL;
21727c478bd9Sstevel@tonic-gate #endif
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 	sockp->ls_flags |= PCS_SUSPENDED;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	if (pcmcia_cs_event &&
21777c478bd9Sstevel@tonic-gate 	    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
21787c478bd9Sstevel@tonic-gate 		CS_EVENT(PCE_PM_SUSPEND, socket, 0);
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 	pcm_event_manager(PCE_PM_SUSPEND, socket, NULL);
21817c478bd9Sstevel@tonic-gate }
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate /*
21847c478bd9Sstevel@tonic-gate  * pcmcia_do_resume()
21857c478bd9Sstevel@tonic-gate  *	tell CS that a suspend has happened by passing a
21867c478bd9Sstevel@tonic-gate  *	card removal event.  Then cleanup the socket state
21877c478bd9Sstevel@tonic-gate  *	to fake the cards being removed so resume works
21887c478bd9Sstevel@tonic-gate  */
21897c478bd9Sstevel@tonic-gate void
21907c478bd9Sstevel@tonic-gate pcmcia_do_resume(int socket, pcmcia_logical_socket_t *sockp)
21917c478bd9Sstevel@tonic-gate {
21927c478bd9Sstevel@tonic-gate 	get_ss_status_t stat;
21937c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter *adapt;
21947c478bd9Sstevel@tonic-gate 	pcmcia_if_t *ls_if;
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate #ifdef	XXX
21977c478bd9Sstevel@tonic-gate 	if (pcmcia_cs_event == NULL) {
21987c478bd9Sstevel@tonic-gate 		return;
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate #endif
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	ls_if = sockp->ls_if;
22037c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
22067c478bd9Sstevel@tonic-gate 		return;
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	stat.socket = socket;
22107c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
22117c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
22127c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
22137c478bd9Sstevel@tonic-gate 			"pcmcia_do_resume(%d, %p)\n", socket, (void *)sockp);
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate #endif
22167c478bd9Sstevel@tonic-gate 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) ==
22177c478bd9Sstevel@tonic-gate 	    SUCCESS) {
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
22207c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
22217c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tsocket=%x, CardState=%x\n",
22227c478bd9Sstevel@tonic-gate 				socket, stat.CardState);
22237c478bd9Sstevel@tonic-gate #endif
22247c478bd9Sstevel@tonic-gate #if 0
22257c478bd9Sstevel@tonic-gate 		/* now have socket info -- do we have events? */
22267c478bd9Sstevel@tonic-gate 		if ((stat.CardState & SBM_CD) == SBM_CD) {
22277c478bd9Sstevel@tonic-gate 			if (pcmcia_cs_event &&
22287c478bd9Sstevel@tonic-gate 			    (sockp->ls_cs_events & (1 << PCE_CARD_INSERT))) {
22297c478bd9Sstevel@tonic-gate 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
22307c478bd9Sstevel@tonic-gate 			}
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 			/* we should have card removed from CS soon */
22337c478bd9Sstevel@tonic-gate 			pcm_event_manager(PCE_CARD_INSERT, socket, NULL);
22347c478bd9Sstevel@tonic-gate 		}
22357c478bd9Sstevel@tonic-gate #else
22367c478bd9Sstevel@tonic-gate 		if (pcmcia_cs_event &&
22377c478bd9Sstevel@tonic-gate 		    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
22387c478bd9Sstevel@tonic-gate 			CS_EVENT(PCE_PM_RESUME, socket, 0);
22397c478bd9Sstevel@tonic-gate 			CS_EVENT(PCE_CARD_REMOVAL, socket, 0);
22407c478bd9Sstevel@tonic-gate 			if ((stat.CardState & SBM_CD) == SBM_CD)
22417c478bd9Sstevel@tonic-gate 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
22427c478bd9Sstevel@tonic-gate 		}
22437c478bd9Sstevel@tonic-gate #endif
22447c478bd9Sstevel@tonic-gate 	}
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate /*
22487c478bd9Sstevel@tonic-gate  * pcmcia_map_power_set()
22497c478bd9Sstevel@tonic-gate  *	Given a power table entry and level, find it in the
22507c478bd9Sstevel@tonic-gate  *	master table and return the index in the adapter table.
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate static int
22537c478bd9Sstevel@tonic-gate pcmcia_map_power_set(struct pcmcia_adapter *adapt, int level, int which)
22547c478bd9Sstevel@tonic-gate {
22557c478bd9Sstevel@tonic-gate 	int plevel, i;
22567c478bd9Sstevel@tonic-gate 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
22577c478bd9Sstevel@tonic-gate 	plevel = pcmcia_power_table[level].PowerLevel;
22587c478bd9Sstevel@tonic-gate 	/* mask = pcmcia_power_table[level].ValidSignals; */
22597c478bd9Sstevel@tonic-gate 	for (i = 0; i < adapt->pca_numpower; i++)
22607c478bd9Sstevel@tonic-gate 		if (plevel == pwr[i].PowerLevel &&
22617c478bd9Sstevel@tonic-gate 		    pwr[i].ValidSignals & which)
22627c478bd9Sstevel@tonic-gate 			return (i);
22637c478bd9Sstevel@tonic-gate 	return (0);
22647c478bd9Sstevel@tonic-gate }
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate /*
22677c478bd9Sstevel@tonic-gate  * pcmcia_map_power_get()
22687c478bd9Sstevel@tonic-gate  *	Given an adapter power entry, find the appropriate index
22697c478bd9Sstevel@tonic-gate  *	in the master table.
22707c478bd9Sstevel@tonic-gate  */
22717c478bd9Sstevel@tonic-gate static int
22727c478bd9Sstevel@tonic-gate pcmcia_map_power_get(struct pcmcia_adapter *adapt, int level, int which)
22737c478bd9Sstevel@tonic-gate {
22747c478bd9Sstevel@tonic-gate 	int plevel, i;
22757c478bd9Sstevel@tonic-gate 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
22767c478bd9Sstevel@tonic-gate 	plevel = pwr[level].PowerLevel;
22777c478bd9Sstevel@tonic-gate 	/* mask = pwr[level].ValidSignals; */
22787c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_power; i++)
22797c478bd9Sstevel@tonic-gate 		if (plevel == pcmcia_power_table[i].PowerLevel &&
22807c478bd9Sstevel@tonic-gate 		    pcmcia_power_table[i].ValidSignals & which)
22817c478bd9Sstevel@tonic-gate 			return (i);
22827c478bd9Sstevel@tonic-gate 	return (0);
22837c478bd9Sstevel@tonic-gate }
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate /*
22867c478bd9Sstevel@tonic-gate  * XXX - SS really needs a way to allow the caller to express
22877c478bd9Sstevel@tonic-gate  *	interest in PCE_CARD_STATUS_CHANGE events.
22887c478bd9Sstevel@tonic-gate  */
22897c478bd9Sstevel@tonic-gate static uint32_t
22907c478bd9Sstevel@tonic-gate pcm_event_map[32] = {
22917c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_WRITE_PROTECT)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22927c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_UNLOCK)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22937c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_EJECTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22947c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_INSERTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22957c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_BATTERY_WARN)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22967c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_BATTERY_DEAD)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22977c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_READY)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
22987c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_CARD_REMOVAL)|PCE_E2M(PCE_CARD_INSERT)|
22997c478bd9Sstevel@tonic-gate 					PCE_E2M(PCE_CARD_STATUS_CHANGE),
23007c478bd9Sstevel@tonic-gate 	PCE_E2M(PCE_PM_SUSPEND)|PCE_E2M(PCE_PM_RESUME),
23017c478bd9Sstevel@tonic-gate };
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate static int
23047c478bd9Sstevel@tonic-gate pcm_mapevents(uint32_t eventmask)
23057c478bd9Sstevel@tonic-gate {
23067c478bd9Sstevel@tonic-gate 	uint32_t mask;
23077c478bd9Sstevel@tonic-gate 	int i;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	for (i = 0, mask = 0; eventmask && i < 32; i++) {
23107c478bd9Sstevel@tonic-gate 		if (eventmask & (1 << i)) {
23117c478bd9Sstevel@tonic-gate 			mask |= pcm_event_map[i];
23127c478bd9Sstevel@tonic-gate 			eventmask &= ~(1 << i);
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate 	}
23157c478bd9Sstevel@tonic-gate 	return (mask);
23167c478bd9Sstevel@tonic-gate }
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate /*
23207c478bd9Sstevel@tonic-gate  * PCMCIA Generic Naming Support
23217c478bd9Sstevel@tonic-gate  *
23227c478bd9Sstevel@tonic-gate  * With 2.6, PCMCIA naming moves to the 1275 and generic naming model.
23237c478bd9Sstevel@tonic-gate  * Consequently, the whole naming mechanism is to be changed.  This is
23247c478bd9Sstevel@tonic-gate  * not backward compatible with the current names but that isn't a problem
23257c478bd9Sstevel@tonic-gate  * due to so few drivers existing.
23267c478bd9Sstevel@tonic-gate  *
23277c478bd9Sstevel@tonic-gate  * For cards with a device_id tuple, a generic name will be used.
23287c478bd9Sstevel@tonic-gate  * if there is no device_id, then the 1275 name will be used if possible.
23297c478bd9Sstevel@tonic-gate  * The 1275 name is of the form pccardNNNN,MMMM from the manfid tuple.
23307c478bd9Sstevel@tonic-gate  * if there is not manfid tuple, an attempt will be made to bind the
23317c478bd9Sstevel@tonic-gate  * node to the version_1 strings.
23327c478bd9Sstevel@tonic-gate  *
23337c478bd9Sstevel@tonic-gate  * In all cases, a "compatible" property is created with a number
23347c478bd9Sstevel@tonic-gate  * of names.  The most generic name will be last in the list.
23357c478bd9Sstevel@tonic-gate  */
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate /*
23387c478bd9Sstevel@tonic-gate  * pcmcia_fix_string()
23397c478bd9Sstevel@tonic-gate  * want to avoid special characters in alias strings so convert
23407c478bd9Sstevel@tonic-gate  * to something innocuous
23417c478bd9Sstevel@tonic-gate  */
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate void
23447c478bd9Sstevel@tonic-gate pcmcia_fix_string(char *str)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	for (; str && *str; str++) {
23477c478bd9Sstevel@tonic-gate 		switch (*str) {
23487c478bd9Sstevel@tonic-gate 			case ' ':
23497c478bd9Sstevel@tonic-gate 			case '\t':
23507c478bd9Sstevel@tonic-gate 				*str = '_';
23517c478bd9Sstevel@tonic-gate 				break;
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate void
23577c478bd9Sstevel@tonic-gate pcmcia_1275_name(int socket, struct pcm_device_info *info,
23587c478bd9Sstevel@tonic-gate 			client_handle_t handle)
23597c478bd9Sstevel@tonic-gate {
23607c478bd9Sstevel@tonic-gate 	cistpl_manfid_t manfid;
23617c478bd9Sstevel@tonic-gate 	cistpl_jedec_t jedec;
23627c478bd9Sstevel@tonic-gate 	tuple_t tuple;
23637c478bd9Sstevel@tonic-gate 	int i;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	tuple.Socket = socket;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	/* get MANFID if it exists -- this is most important form */
23687c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_MANFID;
23697c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
23707c478bd9Sstevel@tonic-gate 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
23717c478bd9Sstevel@tonic-gate 	    SUCCESS) {
23727c478bd9Sstevel@tonic-gate 		i = csx_Parse_CISTPL_MANFID(handle, &tuple,
23737c478bd9Sstevel@tonic-gate 		    &manfid);
23747c478bd9Sstevel@tonic-gate 		if (i == SUCCESS) {
23757c478bd9Sstevel@tonic-gate 			(void) sprintf(info->pd_bind_name, "%s%x,%x",
23767c478bd9Sstevel@tonic-gate 				PCMDEV_NAMEPREF,
23777c478bd9Sstevel@tonic-gate 				manfid.manf, manfid.card);
23787c478bd9Sstevel@tonic-gate 			info->pd_flags |= PCM_NAME_1275;
23797c478bd9Sstevel@tonic-gate 		}
23807c478bd9Sstevel@tonic-gate 	} else {
23817c478bd9Sstevel@tonic-gate 		tuple.Attributes = 0;
23827c478bd9Sstevel@tonic-gate 		tuple.DesiredTuple = CISTPL_JEDEC_A;
23837c478bd9Sstevel@tonic-gate 		if ((i = csx_GetFirstTuple(handle, &tuple)) ==
23847c478bd9Sstevel@tonic-gate 		    SUCCESS) {
23857c478bd9Sstevel@tonic-gate 			i = csx_Parse_CISTPL_JEDEC_A(handle, &tuple,
23867c478bd9Sstevel@tonic-gate 			    &jedec);
23877c478bd9Sstevel@tonic-gate 			if (i == SUCCESS) {
23887c478bd9Sstevel@tonic-gate 				(void) sprintf(info->pd_bind_name, "%s%x,%x",
23897c478bd9Sstevel@tonic-gate 					PCMDEV_NAMEPREF,
23907c478bd9Sstevel@tonic-gate 					jedec.jid[0].id, jedec.jid[0].info);
23917c478bd9Sstevel@tonic-gate 				info->pd_flags |= PCM_NAME_1275;
23927c478bd9Sstevel@tonic-gate 			}
23937c478bd9Sstevel@tonic-gate 		}
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate void
23987c478bd9Sstevel@tonic-gate pcmcia_vers1_name(int socket, struct pcm_device_info *info,
23997c478bd9Sstevel@tonic-gate 			client_handle_t handle)
24007c478bd9Sstevel@tonic-gate {
24017c478bd9Sstevel@tonic-gate 	cistpl_vers_1_t vers1;
24027c478bd9Sstevel@tonic-gate 	tuple_t tuple;
24037c478bd9Sstevel@tonic-gate 	int which = 0;
24047c478bd9Sstevel@tonic-gate 	int i, len, space;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	tuple.Socket = socket;
24077c478bd9Sstevel@tonic-gate 	info->pd_vers1_name[0] = '\0';
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	/* Version 1 strings */
24107c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_VERS_1;
24117c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
24127c478bd9Sstevel@tonic-gate 	if (!which &&
24137c478bd9Sstevel@tonic-gate 	    (i = csx_GetFirstTuple(handle, &tuple)) ==
24147c478bd9Sstevel@tonic-gate 		SUCCESS) {
24157c478bd9Sstevel@tonic-gate 		i = csx_Parse_CISTPL_VERS_1(handle, &tuple, &vers1);
24167c478bd9Sstevel@tonic-gate 		if (i == SUCCESS) {
24177c478bd9Sstevel@tonic-gate 			for (i = 0, len = 0, space = 0; i < vers1.ns; i++) {
24187c478bd9Sstevel@tonic-gate 			    if ((space + len + strlen(info->pd_vers1_name)) >=
24197c478bd9Sstevel@tonic-gate 				sizeof (info->pd_vers1_name))
24207c478bd9Sstevel@tonic-gate 				    break;
24217c478bd9Sstevel@tonic-gate 			    if (space) {
24227c478bd9Sstevel@tonic-gate 				    info->pd_vers1_name[len++] = ',';
24237c478bd9Sstevel@tonic-gate 			    }
24247c478bd9Sstevel@tonic-gate 			    (void) strcpy(info->pd_vers1_name + len,
24257c478bd9Sstevel@tonic-gate 				(char *)vers1.pi[i]);
24267c478bd9Sstevel@tonic-gate 			    len += strlen((char *)vers1.pi[i]);
24277c478bd9Sstevel@tonic-gate 			    /* strip trailing spaces off of string */
24287c478bd9Sstevel@tonic-gate 			    while (info->pd_vers1_name[len - 1] == ' ' &&
24297c478bd9Sstevel@tonic-gate 				    len > 0)
24307c478bd9Sstevel@tonic-gate 				    len--;
24317c478bd9Sstevel@tonic-gate 			    space = 1;
24327c478bd9Sstevel@tonic-gate 			}
24337c478bd9Sstevel@tonic-gate 			info->pd_vers1_name[len] = '\0';
24347c478bd9Sstevel@tonic-gate 			info->pd_flags |= PCM_NAME_VERS1;
24357c478bd9Sstevel@tonic-gate 		}
24367c478bd9Sstevel@tonic-gate 	}
24377c478bd9Sstevel@tonic-gate }
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate int
24417c478bd9Sstevel@tonic-gate pcmcia_get_funce(client_handle_t handle, tuple_t *tuple)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate 	int ret = 0;
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	tuple->Attributes = 0;
24467c478bd9Sstevel@tonic-gate 	while (csx_GetNextTuple(handle, tuple) == SUCCESS) {
24477c478bd9Sstevel@tonic-gate 		if (tuple->TupleCode == CISTPL_FUNCID) {
24487c478bd9Sstevel@tonic-gate 			break;
24497c478bd9Sstevel@tonic-gate 		}
24507c478bd9Sstevel@tonic-gate 		if (tuple->TupleCode == CISTPL_FUNCE) {
24517c478bd9Sstevel@tonic-gate 			ret = 1;
24527c478bd9Sstevel@tonic-gate 			break;
24537c478bd9Sstevel@tonic-gate 		}
24547c478bd9Sstevel@tonic-gate 		tuple->Attributes = 0;
24557c478bd9Sstevel@tonic-gate 	}
24567c478bd9Sstevel@tonic-gate 	return (ret);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate char *pcmcia_lan_types[] = {
24607c478bd9Sstevel@tonic-gate 	"arcnet",
24617c478bd9Sstevel@tonic-gate 	"ethernet",
24627c478bd9Sstevel@tonic-gate 	"token-ring",
24637c478bd9Sstevel@tonic-gate 	"localtalk",
24647c478bd9Sstevel@tonic-gate 	"fddi",
24657c478bd9Sstevel@tonic-gate 	"atm",
24667c478bd9Sstevel@tonic-gate 	"wireless",
24677c478bd9Sstevel@tonic-gate 	"reserved"
24687c478bd9Sstevel@tonic-gate };
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate void
24717c478bd9Sstevel@tonic-gate pcmcia_generic_name(int socket, struct pcm_device_info *info,
24727c478bd9Sstevel@tonic-gate 			client_handle_t handle)
24737c478bd9Sstevel@tonic-gate {
24747c478bd9Sstevel@tonic-gate 	cistpl_funcid_t funcid;
24757c478bd9Sstevel@tonic-gate 	cistpl_funce_t funce;
24767c478bd9Sstevel@tonic-gate 	tuple_t tuple;
24777c478bd9Sstevel@tonic-gate 	int which = 0;
24787c478bd9Sstevel@tonic-gate 	int i;
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	tuple.Socket = socket;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_FUNCID;
24837c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
24847c478bd9Sstevel@tonic-gate 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
24857c478bd9Sstevel@tonic-gate 	    SUCCESS) {
24867c478bd9Sstevel@tonic-gate 		/*
24877c478bd9Sstevel@tonic-gate 		 * need to make sure that CISTPL_FUNCID is not
24887c478bd9Sstevel@tonic-gate 		 * present in both a global and local CIS for MF
24897c478bd9Sstevel@tonic-gate 		 * cards.  3COM seems to do this erroneously
24907c478bd9Sstevel@tonic-gate 		 */
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_MULTI_FUNCTION &&
24937c478bd9Sstevel@tonic-gate 		    tuple.Flags & CISTPLF_GLOBAL_CIS) {
24947c478bd9Sstevel@tonic-gate 			tuple_t ltuple;
24957c478bd9Sstevel@tonic-gate 			ltuple = tuple;
24967c478bd9Sstevel@tonic-gate 			ltuple.DesiredTuple = CISTPL_FUNCID;
24977c478bd9Sstevel@tonic-gate 			ltuple.Attributes = 0;
24987c478bd9Sstevel@tonic-gate 			if ((i = csx_GetNextTuple(handle, &ltuple)) ==
24997c478bd9Sstevel@tonic-gate 			    SUCCESS) {
25007c478bd9Sstevel@tonic-gate 				/* this is the per-function funcid */
25017c478bd9Sstevel@tonic-gate 				tuple = ltuple;
25027c478bd9Sstevel@tonic-gate 			}
25037c478bd9Sstevel@tonic-gate 		}
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 		i = csx_Parse_CISTPL_FUNCID(handle, &tuple, &funcid);
25067c478bd9Sstevel@tonic-gate 		if (i == SUCCESS) {
25077c478bd9Sstevel@tonic-gate 			/* in case no function extension */
25087c478bd9Sstevel@tonic-gate 			if (funcid.function < PCM_GENNAME_SIZE)
25097c478bd9Sstevel@tonic-gate 				(void) strcpy(info->pd_generic_name,
25107c478bd9Sstevel@tonic-gate 				    pcmcia_generic_names[funcid.function]);
25117c478bd9Sstevel@tonic-gate 			else
25127c478bd9Sstevel@tonic-gate 				(void) sprintf(info->pd_generic_name,
25137c478bd9Sstevel@tonic-gate 					"class,%x",
25147c478bd9Sstevel@tonic-gate 					funcid.function);
25157c478bd9Sstevel@tonic-gate 		}
25167c478bd9Sstevel@tonic-gate 		info->pd_type = funcid.function;
25177c478bd9Sstevel@tonic-gate 		switch (funcid.function) {
25187c478bd9Sstevel@tonic-gate 		case TPLFUNC_LAN:
25197c478bd9Sstevel@tonic-gate 			which = pcmcia_get_funce(handle, &tuple);
25207c478bd9Sstevel@tonic-gate 			if (which) {
25217c478bd9Sstevel@tonic-gate 				i = csx_Parse_CISTPL_FUNCE(handle,
25227c478bd9Sstevel@tonic-gate 				    &tuple,
25237c478bd9Sstevel@tonic-gate 				    &funce, TPLFUNC_LAN);
25247c478bd9Sstevel@tonic-gate 				if (i == SUCCESS) {
25257c478bd9Sstevel@tonic-gate 					i = funce.data.lan.tech;
25267c478bd9Sstevel@tonic-gate 					if (i > sizeof (pcmcia_lan_types) /
25277c478bd9Sstevel@tonic-gate 					    sizeof (char *)) {
25287c478bd9Sstevel@tonic-gate 						break;
25297c478bd9Sstevel@tonic-gate 					}
25307c478bd9Sstevel@tonic-gate 					(void) strcpy(info->pd_generic_name,
25317c478bd9Sstevel@tonic-gate 						pcmcia_lan_types[i]);
25327c478bd9Sstevel@tonic-gate 				}
25337c478bd9Sstevel@tonic-gate 			}
25347c478bd9Sstevel@tonic-gate 			break;
25357c478bd9Sstevel@tonic-gate 		case TPLFUNC_VIDEO:
25367c478bd9Sstevel@tonic-gate #ifdef future_pcmcia_spec
25377c478bd9Sstevel@tonic-gate 			which = pcmcia_get_funce(handle, &tuple);
25387c478bd9Sstevel@tonic-gate 			if (which) {
25397c478bd9Sstevel@tonic-gate 				i = csx_Parse_CISTPL_FUNCE(handle,
25407c478bd9Sstevel@tonic-gate 				    &tuple,
25417c478bd9Sstevel@tonic-gate 				    &funce, TPLFUNC_VIDEO);
25427c478bd9Sstevel@tonic-gate 				if (i == SUCCESS) {
25437c478bd9Sstevel@tonic-gate 					i = funce.video.tech;
25447c478bd9Sstevel@tonic-gate 					if (i > sizeof (pcmcia_lan_types) /
25457c478bd9Sstevel@tonic-gate 					    sizeof (char *)) {
25467c478bd9Sstevel@tonic-gate 						break;
25477c478bd9Sstevel@tonic-gate 					}
25487c478bd9Sstevel@tonic-gate 					(void) strcpy(info->pd_generic_names,
25497c478bd9Sstevel@tonic-gate 						pcmcia_lan_types[i]);
25507c478bd9Sstevel@tonic-gate 				}
25517c478bd9Sstevel@tonic-gate 			}
25527c478bd9Sstevel@tonic-gate #endif
25537c478bd9Sstevel@tonic-gate 			break;
25547c478bd9Sstevel@tonic-gate 		}
25557c478bd9Sstevel@tonic-gate 		info->pd_flags |= PCM_NAME_GENERIC;
25567c478bd9Sstevel@tonic-gate 	} else {
25577c478bd9Sstevel@tonic-gate 		/* if no FUNCID, do we have CONFIG */
25587c478bd9Sstevel@tonic-gate 		tuple.DesiredTuple = CISTPL_CONFIG;
25597c478bd9Sstevel@tonic-gate 		tuple.Attributes = 0;
25607c478bd9Sstevel@tonic-gate 		if (csx_GetFirstTuple(handle, &tuple) != SUCCESS) {
25617c478bd9Sstevel@tonic-gate 			info->pd_flags |= PCM_NO_CONFIG | PCM_NAME_GENERIC;
25627c478bd9Sstevel@tonic-gate 			(void) strcpy(info->pd_generic_name,
25637c478bd9Sstevel@tonic-gate 				pcmcia_generic_names[PCM_TYPE_MEMORY]);
25647c478bd9Sstevel@tonic-gate 			info->pd_type = PCM_TYPE_MEMORY;
25657c478bd9Sstevel@tonic-gate 		}
25667c478bd9Sstevel@tonic-gate 	}
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate /*
25717c478bd9Sstevel@tonic-gate  * pcmcia_add_compatible()
25727c478bd9Sstevel@tonic-gate  * add the cached compatible property list.
25737c478bd9Sstevel@tonic-gate  */
25747c478bd9Sstevel@tonic-gate void
25757c478bd9Sstevel@tonic-gate pcmcia_add_compatible(dev_info_t *dip, struct pcm_device_info *info)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	int length = 0, i;
25787c478bd9Sstevel@tonic-gate 	char buff[MAXNAMELEN];
25797c478bd9Sstevel@tonic-gate 	char *compat_name[8];
25807c478bd9Sstevel@tonic-gate 	int ci = 0;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	bzero(compat_name, sizeof (compat_name));
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	if (info->pd_flags & PCM_NAME_VERS1) {
25857c478bd9Sstevel@tonic-gate 		(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
25867c478bd9Sstevel@tonic-gate 		    info->pd_vers1_name);
25877c478bd9Sstevel@tonic-gate 		pcmcia_fix_string(buff); /* don't want spaces */
25887c478bd9Sstevel@tonic-gate 		length = strlen(buff) + 1;
25897c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
25907c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], buff);
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
25947c478bd9Sstevel@tonic-gate 	    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
25957c478bd9Sstevel@tonic-gate 		(void) sprintf(buff, "%s,%x", info->pd_bind_name,
25967c478bd9Sstevel@tonic-gate 		    info->pd_function);
25977c478bd9Sstevel@tonic-gate 		length = strlen(buff) + 1;
25987c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
25997c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], buff);
26007c478bd9Sstevel@tonic-gate 	}
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	if (info->pd_flags & PCM_NAME_1275) {
26037c478bd9Sstevel@tonic-gate 		length = strlen(info->pd_bind_name) + 1;
26047c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
26057c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], info->pd_bind_name);
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	if (info->pd_flags & PCM_NAME_GENERIC) {
26097c478bd9Sstevel@tonic-gate 		if (strncmp(info->pd_generic_name, "class,", 6) == 0) {
26107c478bd9Sstevel@tonic-gate 			/* no generic without "pccard" */
26117c478bd9Sstevel@tonic-gate 			(void) sprintf(buff, "%s%s", PCMDEV_NAMEPREF,
26127c478bd9Sstevel@tonic-gate 				info->pd_generic_name);
26137c478bd9Sstevel@tonic-gate 		} else {
26147c478bd9Sstevel@tonic-gate 			/* first pccard,generic-name */
26157c478bd9Sstevel@tonic-gate 			(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
26167c478bd9Sstevel@tonic-gate 				info->pd_generic_name);
26177c478bd9Sstevel@tonic-gate 		}
26187c478bd9Sstevel@tonic-gate 		length = strlen(buff) + 1;
26197c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
26207c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], buff);
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 		/* now the simple generic name */
26237c478bd9Sstevel@tonic-gate 		length = strlen(info->pd_generic_name) + 1;
26247c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
26257c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], info->pd_generic_name);
26267c478bd9Sstevel@tonic-gate 	}
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	if (info->pd_flags & PCM_NO_CONFIG) {
26297c478bd9Sstevel@tonic-gate 		char *mem = "pccard,memory";
26307c478bd9Sstevel@tonic-gate 		/*
26317c478bd9Sstevel@tonic-gate 		 * I/O cards are required to have a config tuple.
26327c478bd9Sstevel@tonic-gate 		 * there are some that violate the spec and don't
26337c478bd9Sstevel@tonic-gate 		 * but it is most likely that this is a memory card
26347c478bd9Sstevel@tonic-gate 		 * so tag it as such.  "memory" is more general
26357c478bd9Sstevel@tonic-gate 		 * than other things so needs to come last.
26367c478bd9Sstevel@tonic-gate 		 */
26377c478bd9Sstevel@tonic-gate 		length = strlen(mem) + 1;
26387c478bd9Sstevel@tonic-gate 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
26397c478bd9Sstevel@tonic-gate 		(void) strcpy(compat_name[ci++], mem);
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	if (ci == 0)
26437c478bd9Sstevel@tonic-gate 		return;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
26467c478bd9Sstevel@tonic-gate 	    "compatible", (char **)compat_name, ci) != DDI_PROP_SUCCESS)
26477c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcmcia: unable to create compatible prop");
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	for (i = 0; i < ci; i++)
26507c478bd9Sstevel@tonic-gate 		kmem_free(compat_name[i], strlen(compat_name[i]) + 1);
26517c478bd9Sstevel@tonic-gate }
26527c478bd9Sstevel@tonic-gate /*
26537c478bd9Sstevel@tonic-gate  * CIS parsing and other PC Card specific code
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate /*
26577c478bd9Sstevel@tonic-gate  * pcmcia_get_mem_regs()
26587c478bd9Sstevel@tonic-gate  */
26597c478bd9Sstevel@tonic-gate static int
26607c478bd9Sstevel@tonic-gate pcmcia_get_mem_regs(struct pcm_regs *regs, struct pcm_device_info *info,
26617c478bd9Sstevel@tonic-gate 			int type, int pctype)
26627c478bd9Sstevel@tonic-gate {
26637c478bd9Sstevel@tonic-gate 	int num_regs = 0;
26647c478bd9Sstevel@tonic-gate 	tuple_t tuple;
26657c478bd9Sstevel@tonic-gate 	cistpl_device_t device;
26667c478bd9Sstevel@tonic-gate 	uint32_t curr_base;
26677c478bd9Sstevel@tonic-gate 	int ret, len;
26687c478bd9Sstevel@tonic-gate 	int space;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	/*
26717c478bd9Sstevel@tonic-gate 	 * current plan for reg spec:
26727c478bd9Sstevel@tonic-gate 	 * device_a will be accumulated to determine max size of
26737c478bd9Sstevel@tonic-gate 	 * attribute memory.  device for common.  Then config
26747c478bd9Sstevel@tonic-gate 	 * tuples to get a worst case I/O size.
26757c478bd9Sstevel@tonic-gate 	 */
26767c478bd9Sstevel@tonic-gate 	bzero(&tuple, sizeof (tuple));
26777c478bd9Sstevel@tonic-gate 	tuple.Socket = info->pd_socket;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = (cisdata_t)type;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	space = (type == CISTPL_DEVICE_A) ? PC_REG_SPACE_ATTRIBUTE :
26827c478bd9Sstevel@tonic-gate 		PC_REG_SPACE_MEMORY;
26837c478bd9Sstevel@tonic-gate 	if ((ret = csx_GetFirstTuple(info->pd_handle, &tuple)) == CS_SUCCESS) {
26847c478bd9Sstevel@tonic-gate 		bzero(&device, sizeof (device));
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 		if (type == CISTPL_DEVICE)
26877c478bd9Sstevel@tonic-gate 			ret = csx_Parse_CISTPL_DEVICE(info->pd_handle, &tuple,
26887c478bd9Sstevel@tonic-gate 			    &device);
26897c478bd9Sstevel@tonic-gate 		else
26907c478bd9Sstevel@tonic-gate 			ret = csx_Parse_CISTPL_DEVICE_A(info->pd_handle, &tuple,
26917c478bd9Sstevel@tonic-gate 			    &device);
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 		if (ret == CS_SUCCESS) {
26947c478bd9Sstevel@tonic-gate 		    curr_base = 0;
26957c478bd9Sstevel@tonic-gate 		    for (ret = 0; ret < device.num_devices;
26967c478bd9Sstevel@tonic-gate 			ret++) {
26977c478bd9Sstevel@tonic-gate 			    /* need to order these for real mem first */
26987c478bd9Sstevel@tonic-gate 			    if (device.devnode[ret].type !=
26997c478bd9Sstevel@tonic-gate 				CISTPL_DEVICE_DTYPE_NULL) {
27007c478bd9Sstevel@tonic-gate 				    /* how to represent types??? */
27017c478bd9Sstevel@tonic-gate 				    regs[num_regs].phys_hi =
27027c478bd9Sstevel@tonic-gate 					PC_REG_PHYS_HI(0, 0,
27037c478bd9Sstevel@tonic-gate 					    pctype,
27047c478bd9Sstevel@tonic-gate 					    space,
27057c478bd9Sstevel@tonic-gate 					    info->pd_socket,
27067c478bd9Sstevel@tonic-gate 					    info->pd_function,
27077c478bd9Sstevel@tonic-gate 					    0);
27087c478bd9Sstevel@tonic-gate 				    regs[num_regs].phys_lo = curr_base;
27097c478bd9Sstevel@tonic-gate 				    len = device.devnode[ret].size_in_bytes;
27107c478bd9Sstevel@tonic-gate 				    curr_base += len;
27117c478bd9Sstevel@tonic-gate 				    regs[num_regs].phys_len = len;
27127c478bd9Sstevel@tonic-gate 				    num_regs++;
27137c478bd9Sstevel@tonic-gate 			    } else {
27147c478bd9Sstevel@tonic-gate 				/*
27157c478bd9Sstevel@tonic-gate 				 * NULL device is a "hole"
27167c478bd9Sstevel@tonic-gate 				 */
27177c478bd9Sstevel@tonic-gate 				    curr_base +=
27187c478bd9Sstevel@tonic-gate 					    device.devnode[ret].size_in_bytes;
27197c478bd9Sstevel@tonic-gate 			    }
27207c478bd9Sstevel@tonic-gate 			}
27217c478bd9Sstevel@tonic-gate 	    }
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 	return (num_regs);
27247c478bd9Sstevel@tonic-gate }
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate /*
27277c478bd9Sstevel@tonic-gate  *
27287c478bd9Sstevel@tonic-gate  */
27297c478bd9Sstevel@tonic-gate static int
27307c478bd9Sstevel@tonic-gate pcmcia_get_io_regs(struct pcm_regs *regs, struct pcm_device_info *info,
27317c478bd9Sstevel@tonic-gate 		    int pctype)
27327c478bd9Sstevel@tonic-gate {
27337c478bd9Sstevel@tonic-gate 	int num_regs = 0;
27347c478bd9Sstevel@tonic-gate 	tuple_t tuple;
27357c478bd9Sstevel@tonic-gate 	uint32_t curr_base;
27367c478bd9Sstevel@tonic-gate 	int len, curr, i, curr_len;
27377c478bd9Sstevel@tonic-gate 	cistpl_config_t config;
27387c478bd9Sstevel@tonic-gate 	cistpl_cftable_entry_t cftable;
27397c478bd9Sstevel@tonic-gate 	struct pcm_regs tmp[16];
27407c478bd9Sstevel@tonic-gate 	int found = 0;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	bzero(&tuple, sizeof (tuple));
27437c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_CONFIG;
27447c478bd9Sstevel@tonic-gate 	tuple.Socket = info->pd_socket;
27457c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
27467c478bd9Sstevel@tonic-gate 	curr_base = 0;
27477c478bd9Sstevel@tonic-gate 	len = 0;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	if (csx_GetFirstTuple(info->pd_handle, &tuple) == CS_SUCCESS) {
27507c478bd9Sstevel@tonic-gate 	    if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
27517c478bd9Sstevel@tonic-gate 					&tuple, &config) != CS_SUCCESS) {
27527c478bd9Sstevel@tonic-gate 		info->pd_flags |= PCM_NO_CONFIG; /* must be memory */
27537c478bd9Sstevel@tonic-gate 		return (0);
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 	curr = 0;
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
27587c478bd9Sstevel@tonic-gate 	tuple.Socket = info->pd_socket;
27597c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
27607c478bd9Sstevel@tonic-gate 	bzero(tmp, sizeof (tmp));
27617c478bd9Sstevel@tonic-gate 	while (csx_GetNextTuple(info->pd_handle,
27627c478bd9Sstevel@tonic-gate 				&tuple) == CS_SUCCESS) {
27637c478bd9Sstevel@tonic-gate 	    bzero(&cftable, sizeof (cftable));
27647c478bd9Sstevel@tonic-gate 	    if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
27657c478bd9Sstevel@tonic-gate 						&tuple, &cftable) ==
27667c478bd9Sstevel@tonic-gate 		CS_SUCCESS) {
27677c478bd9Sstevel@tonic-gate 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IO) {
27687c478bd9Sstevel@tonic-gate 		    /* we have an I/O entry */
27697c478bd9Sstevel@tonic-gate 		    if (cftable.io.flags &
27707c478bd9Sstevel@tonic-gate 			CISTPL_CFTABLE_TPCE_FS_IO_RANGE) {
27717c478bd9Sstevel@tonic-gate 			len = cftable.io.addr_lines;
27727c478bd9Sstevel@tonic-gate 			if (len != 0)
27737c478bd9Sstevel@tonic-gate 				len = 1 << len;
27747c478bd9Sstevel@tonic-gate 			for (i = 0; i < cftable.io.ranges && curr < 16; i++) {
27757c478bd9Sstevel@tonic-gate 			    curr_base = cftable.io.range[i].addr;
27767c478bd9Sstevel@tonic-gate 			    curr_len = cftable.io.range[i].length;
27777c478bd9Sstevel@tonic-gate 			    if (curr_len == 0)
27787c478bd9Sstevel@tonic-gate 				    curr_len = len;
27797c478bd9Sstevel@tonic-gate 			    if (len != 0 || cftable.io.addr_lines == 0) {
27807c478bd9Sstevel@tonic-gate 				/* we have potential relocation */
27817c478bd9Sstevel@tonic-gate 				int mask;
27827c478bd9Sstevel@tonic-gate 				mask = cftable.io.addr_lines ?
27837c478bd9Sstevel@tonic-gate 						cftable.io.addr_lines :
27847c478bd9Sstevel@tonic-gate 							genp2(len);
27857c478bd9Sstevel@tonic-gate 				mask = genmask(mask);
27867c478bd9Sstevel@tonic-gate 				if ((mask & curr_base) == 0) {
27877c478bd9Sstevel@tonic-gate 					/* more accurate length */
27887c478bd9Sstevel@tonic-gate 					regs->phys_len = curr_len;
27897c478bd9Sstevel@tonic-gate 					regs->phys_lo = 0;
27907c478bd9Sstevel@tonic-gate 					regs->phys_hi =
27917c478bd9Sstevel@tonic-gate 					    PC_REG_PHYS_HI(0,
27927c478bd9Sstevel@tonic-gate 						0,
27937c478bd9Sstevel@tonic-gate 						pctype,
27947c478bd9Sstevel@tonic-gate 						PC_REG_SPACE_IO,
27957c478bd9Sstevel@tonic-gate 						info->pd_socket,
27967c478bd9Sstevel@tonic-gate 						info->pd_function,
27977c478bd9Sstevel@tonic-gate 						0);
27987c478bd9Sstevel@tonic-gate 					num_regs++;
27997c478bd9Sstevel@tonic-gate 					found = 2;
28007c478bd9Sstevel@tonic-gate 					break;
28017c478bd9Sstevel@tonic-gate 				}
28027c478bd9Sstevel@tonic-gate 			    }
28037c478bd9Sstevel@tonic-gate 			    tmp[curr].phys_len = curr_len;
28047c478bd9Sstevel@tonic-gate 			    tmp[curr].phys_lo = curr_base;
28057c478bd9Sstevel@tonic-gate 			    curr++;
28067c478bd9Sstevel@tonic-gate 			    found = 1;
28077c478bd9Sstevel@tonic-gate 			}
28087c478bd9Sstevel@tonic-gate 			if (found == 2)
28097c478bd9Sstevel@tonic-gate 				break;
28107c478bd9Sstevel@tonic-gate 		    } else {
28117c478bd9Sstevel@tonic-gate 			/* no I/O range so just a mask */
28127c478bd9Sstevel@tonic-gate 			regs->phys_len = 1 << cftable.io.addr_lines;
28137c478bd9Sstevel@tonic-gate 			regs->phys_hi =
28147c478bd9Sstevel@tonic-gate 				PC_REG_PHYS_HI(0,
28157c478bd9Sstevel@tonic-gate 						0,
28167c478bd9Sstevel@tonic-gate 						pctype,
28177c478bd9Sstevel@tonic-gate 						PC_REG_SPACE_IO,
28187c478bd9Sstevel@tonic-gate 						info->pd_socket,
28197c478bd9Sstevel@tonic-gate 						info->pd_function,
28207c478bd9Sstevel@tonic-gate 						0);
28217c478bd9Sstevel@tonic-gate 			regs->phys_lo = 0;
28227c478bd9Sstevel@tonic-gate 			num_regs++;
28237c478bd9Sstevel@tonic-gate 			regs++;
28247c478bd9Sstevel@tonic-gate 			/* quit on "good" entry */
28257c478bd9Sstevel@tonic-gate 			break;
28267c478bd9Sstevel@tonic-gate 		    }
28277c478bd9Sstevel@tonic-gate 		    /* was this the last CFTABLE Entry? */
28287c478bd9Sstevel@tonic-gate 		    if (config.last == cftable.index)
28297c478bd9Sstevel@tonic-gate 			    break;
28307c478bd9Sstevel@tonic-gate 		}
28317c478bd9Sstevel@tonic-gate 	    }
28327c478bd9Sstevel@tonic-gate 	}
28337c478bd9Sstevel@tonic-gate 	if (found == 1) {
28347c478bd9Sstevel@tonic-gate 		/*
28357c478bd9Sstevel@tonic-gate 		 * have some non-relocatable values
28367c478bd9Sstevel@tonic-gate 		 * so we include them all for now
28377c478bd9Sstevel@tonic-gate 		 */
28387c478bd9Sstevel@tonic-gate 		for (i = 0; i < curr && num_regs < 8; i++) {
28397c478bd9Sstevel@tonic-gate 		    regs->phys_len = tmp[i].phys_len;
28407c478bd9Sstevel@tonic-gate 		    regs->phys_lo = tmp[i].phys_lo;
28417c478bd9Sstevel@tonic-gate 		    regs->phys_hi = PC_REG_PHYS_HI(1, 0, pctype,
28427c478bd9Sstevel@tonic-gate 			    PC_REG_SPACE_IO, info->pd_socket,
28437c478bd9Sstevel@tonic-gate 			    info->pd_function, 0);
28447c478bd9Sstevel@tonic-gate 		    regs++;
28457c478bd9Sstevel@tonic-gate 		    num_regs++;
28467c478bd9Sstevel@tonic-gate 		}
28477c478bd9Sstevel@tonic-gate 	    }
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 	return (num_regs);
28507c478bd9Sstevel@tonic-gate }
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate /*
28537c478bd9Sstevel@tonic-gate  * pcmcia_create_regs()
28547c478bd9Sstevel@tonic-gate  *	create a valid set of regspecs for the card
28557c478bd9Sstevel@tonic-gate  *	The first one is always for CIS access and naming
28567c478bd9Sstevel@tonic-gate  */
28577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28587c478bd9Sstevel@tonic-gate static void
28597c478bd9Sstevel@tonic-gate pcmcia_find_regs(dev_info_t *dip, struct pcm_device_info *info,
28607c478bd9Sstevel@tonic-gate 			struct pcmcia_parent_private *ppd)
28617c478bd9Sstevel@tonic-gate {
28627c478bd9Sstevel@tonic-gate 	struct pcm_regs regs[32]; /* assume worst case */
28637c478bd9Sstevel@tonic-gate 	int num_regs = 0;
28647c478bd9Sstevel@tonic-gate 	int len;
28657c478bd9Sstevel@tonic-gate 	int bustype;
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	if (ppd->ppd_flags & PPD_CARD_CARDBUS) {
28687c478bd9Sstevel@tonic-gate 		/* always have a CIS map */
28697c478bd9Sstevel@tonic-gate 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_CARDBUS,
28707c478bd9Sstevel@tonic-gate 						PC_REG_SPACE_CONFIG,
28717c478bd9Sstevel@tonic-gate 						info->pd_socket,
28727c478bd9Sstevel@tonic-gate 						info->pd_function, 0);
28737c478bd9Sstevel@tonic-gate 		bustype = PC_REG_TYPE_CARDBUS;
28747c478bd9Sstevel@tonic-gate 	} else {
28757c478bd9Sstevel@tonic-gate 		/* always have a CIS map */
28767c478bd9Sstevel@tonic-gate 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
28777c478bd9Sstevel@tonic-gate 						PC_REG_SPACE_ATTRIBUTE,
28787c478bd9Sstevel@tonic-gate 						info->pd_socket,
28797c478bd9Sstevel@tonic-gate 						info->pd_function, 0);
28807c478bd9Sstevel@tonic-gate 		bustype = PC_REG_TYPE_16BIT;
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 	regs[0].phys_lo = 0;	/* always starts at zero */
28837c478bd9Sstevel@tonic-gate 	regs[0].phys_len = 0;
28847c478bd9Sstevel@tonic-gate 	num_regs++;
28857c478bd9Sstevel@tonic-gate 	/*
28867c478bd9Sstevel@tonic-gate 	 * need to search CIS for other memory instances
28877c478bd9Sstevel@tonic-gate 	 */
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	if (info->pd_flags & PCM_OTHER_NOCIS) {
28907c478bd9Sstevel@tonic-gate 		/* special case of memory only card without CIS */
28917c478bd9Sstevel@tonic-gate 		regs[1].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
28927c478bd9Sstevel@tonic-gate 						PC_REG_SPACE_MEMORY,
28937c478bd9Sstevel@tonic-gate 						info->pd_socket,
28947c478bd9Sstevel@tonic-gate 						info->pd_function, 0);
28957c478bd9Sstevel@tonic-gate 		regs[1].phys_lo = 0;
28967c478bd9Sstevel@tonic-gate 		regs[1].phys_len = PCM_MAX_R2_MEM;
28977c478bd9Sstevel@tonic-gate 		num_regs++;
28987c478bd9Sstevel@tonic-gate 	} else {
28997c478bd9Sstevel@tonic-gate 		/*
29007c478bd9Sstevel@tonic-gate 		 * want to get any other memory and/or I/O regions
29017c478bd9Sstevel@tonic-gate 		 * on the card and represent them here.
29027c478bd9Sstevel@tonic-gate 		 */
29037c478bd9Sstevel@tonic-gate 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
29047c478bd9Sstevel@tonic-gate 						CISTPL_DEVICE_A, bustype);
29057c478bd9Sstevel@tonic-gate 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
29067c478bd9Sstevel@tonic-gate 						CISTPL_DEVICE, bustype);
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 		/* now look for an I/O space to configure */
29097c478bd9Sstevel@tonic-gate 		num_regs += pcmcia_get_io_regs(&regs[num_regs], info,
29107c478bd9Sstevel@tonic-gate 						bustype);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	}
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	len = num_regs * sizeof (uint32_t) * 3;
29157c478bd9Sstevel@tonic-gate 	ppd->ppd_nreg = num_regs;
29167c478bd9Sstevel@tonic-gate 	ppd->ppd_reg = kmem_alloc(len, KM_SLEEP);
29177c478bd9Sstevel@tonic-gate 	bcopy(regs, ppd->ppd_reg, len);
29187c478bd9Sstevel@tonic-gate 	len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
29197c478bd9Sstevel@tonic-gate 	ppd->ppd_assigned = kmem_zalloc(len, KM_SLEEP);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate /*
29247c478bd9Sstevel@tonic-gate  * pcmcia_need_intr()
29257c478bd9Sstevel@tonic-gate  *	check to see if an interrupt tuple exists.
29267c478bd9Sstevel@tonic-gate  *	existence means we need one in the intrspec.
29277c478bd9Sstevel@tonic-gate  */
29287c478bd9Sstevel@tonic-gate static int
29297c478bd9Sstevel@tonic-gate pcmcia_need_intr(int socket, struct pcm_device_info *info)
29307c478bd9Sstevel@tonic-gate {
29317c478bd9Sstevel@tonic-gate 	cistpl_config_t config;
29327c478bd9Sstevel@tonic-gate 	cistpl_cftable_entry_t cftable;
29337c478bd9Sstevel@tonic-gate 	tuple_t tuple;
29347c478bd9Sstevel@tonic-gate 	int i;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	bzero(&tuple, sizeof (tuple));
29377c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_CONFIG;
29387c478bd9Sstevel@tonic-gate 	tuple.Socket = socket;
29397c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
29407c478bd9Sstevel@tonic-gate 	if (csx_GetFirstTuple(info->pd_handle, &tuple) != CS_SUCCESS) {
29417c478bd9Sstevel@tonic-gate 		return (0);
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
29447c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
29457c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_need_intr: have config tuple\n");
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate #endif
29487c478bd9Sstevel@tonic-gate 	bzero(&config, sizeof (config));
29497c478bd9Sstevel@tonic-gate 	if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
29507c478bd9Sstevel@tonic-gate 	    &tuple, &config) != CS_SUCCESS) {
29517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcmcia: config failed to parse\n");
29527c478bd9Sstevel@tonic-gate 		return (0);
29537c478bd9Sstevel@tonic-gate 	}
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	for (cftable.index = (int)-1, i = -1;
29567c478bd9Sstevel@tonic-gate 		i != config.last; i = cftable.index) {
29577c478bd9Sstevel@tonic-gate 		tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
29587c478bd9Sstevel@tonic-gate 		tuple.Attributes = 0;
29597c478bd9Sstevel@tonic-gate 		if (csx_GetNextTuple(info->pd_handle,
29607c478bd9Sstevel@tonic-gate 		    &tuple) != CS_SUCCESS) {
29617c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcmcia: get cftable failed\n");
29627c478bd9Sstevel@tonic-gate 			break;
29637c478bd9Sstevel@tonic-gate 		}
29647c478bd9Sstevel@tonic-gate 		bzero(&cftable, sizeof (cftable));
29657c478bd9Sstevel@tonic-gate 		if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
29667c478bd9Sstevel@tonic-gate 		    &tuple, &cftable) !=
29677c478bd9Sstevel@tonic-gate 		    CS_SUCCESS) {
29687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcmcia: parse cftable failed\n");
29697c478bd9Sstevel@tonic-gate 			break;
29707c478bd9Sstevel@tonic-gate 		}
29717c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
29727c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
29737c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\t%x: flags=%x (%x)\n",
29747c478bd9Sstevel@tonic-gate 				i, cftable.flags,
29757c478bd9Sstevel@tonic-gate 				cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ);
29767c478bd9Sstevel@tonic-gate #endif
29777c478bd9Sstevel@tonic-gate 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ)
29787c478bd9Sstevel@tonic-gate 			return (1);
29797c478bd9Sstevel@tonic-gate 	}
29807c478bd9Sstevel@tonic-gate 	return (0);
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate }
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate /*
29857c478bd9Sstevel@tonic-gate  * pcmcia_num_funcs()
29867c478bd9Sstevel@tonic-gate  *	look for a CISTPL_LONGLINK_MFC
29877c478bd9Sstevel@tonic-gate  *	if there is one, return the number of functions
29887c478bd9Sstevel@tonic-gate  *	if there isn't one, then there is one function
29897c478bd9Sstevel@tonic-gate  */
29907c478bd9Sstevel@tonic-gate static int
29917c478bd9Sstevel@tonic-gate pcmcia_num_funcs(int socket, client_handle_t handle)
29927c478bd9Sstevel@tonic-gate {
29937c478bd9Sstevel@tonic-gate 	int count = 1;
29947c478bd9Sstevel@tonic-gate 	cistpl_longlink_mfc_t mfc;
29957c478bd9Sstevel@tonic-gate 	tuple_t tuple;
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	bzero(&tuple, sizeof (tuple_t));
29987c478bd9Sstevel@tonic-gate 	tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
29997c478bd9Sstevel@tonic-gate 	tuple.Socket = socket;
30007c478bd9Sstevel@tonic-gate 	tuple.Attributes = 0;
30017c478bd9Sstevel@tonic-gate 	if (csx_GetFirstTuple(handle, &tuple) == CS_SUCCESS) {
30027c478bd9Sstevel@tonic-gate 		/* this is a multifunction card */
30037c478bd9Sstevel@tonic-gate 		if (csx_ParseTuple(handle, &tuple, (cisparse_t *)&mfc,
30047c478bd9Sstevel@tonic-gate 		    CISTPL_LONGLINK_MFC) == CS_SUCCESS) {
30057c478bd9Sstevel@tonic-gate 			count = mfc.nfuncs;
30067c478bd9Sstevel@tonic-gate 		}
30077c478bd9Sstevel@tonic-gate 	}
30087c478bd9Sstevel@tonic-gate 	return (count);
30097c478bd9Sstevel@tonic-gate }
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate client_handle_t pcmcia_cs_handle;
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate /*
30147c478bd9Sstevel@tonic-gate  * pcmcia_create_dev_info(socket)
30157c478bd9Sstevel@tonic-gate  *	either find or create the device information structure
30167c478bd9Sstevel@tonic-gate  *	for the card(s) just inserted.	We don't care about removal yet.
30177c478bd9Sstevel@tonic-gate  *	In any case, we will only do this at CS request
30187c478bd9Sstevel@tonic-gate  */
30197c478bd9Sstevel@tonic-gate static void
30207c478bd9Sstevel@tonic-gate pcmcia_create_dev_info(int socket)
30217c478bd9Sstevel@tonic-gate {
30227c478bd9Sstevel@tonic-gate 	struct pcm_device_info card_info;
30237c478bd9Sstevel@tonic-gate 	client_reg_t reg;
30247c478bd9Sstevel@tonic-gate 	cisinfo_t cisinfo;
30257c478bd9Sstevel@tonic-gate 	int i;
30267c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
30277c478bd9Sstevel@tonic-gate 	static int handle_def = 0;
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
30307c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
30317c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "create dev_info_t for device in socket %d\n",
30327c478bd9Sstevel@tonic-gate 			socket);
30337c478bd9Sstevel@tonic-gate #endif
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	/*
30367c478bd9Sstevel@tonic-gate 	 * before we can do anything else, we need the parent
30377c478bd9Sstevel@tonic-gate 	 * devinfo of the socket.  This gets things in the right
30387c478bd9Sstevel@tonic-gate 	 * place in the device tree.
30397c478bd9Sstevel@tonic-gate 	 */
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	pdip = pcm_find_parent_dip(socket);
30427c478bd9Sstevel@tonic-gate 	if (pdip == NULL)
30437c478bd9Sstevel@tonic-gate 		return;
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	/* Card Services calls needed to get CIS info */
30467c478bd9Sstevel@tonic-gate 	reg.dip = NULL;
30477c478bd9Sstevel@tonic-gate 	reg.Attributes = INFO_SOCKET_SERVICES;
30487c478bd9Sstevel@tonic-gate 	reg.EventMask = 0;
30497c478bd9Sstevel@tonic-gate 	reg.event_handler = NULL;
30507c478bd9Sstevel@tonic-gate 	reg.Version = CS_VERSION;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	bzero(&card_info, sizeof (card_info));
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	if (handle_def == 0) {
30557c478bd9Sstevel@tonic-gate 		if (csx_RegisterClient(&pcmcia_cs_handle,
30567c478bd9Sstevel@tonic-gate 		    &reg) != CS_SUCCESS) {
30577c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
30587c478bd9Sstevel@tonic-gate 			if (pcmcia_debug)
30597c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
30607c478bd9Sstevel@tonic-gate 					"pcmcia: RegisterClient failed\n");
30617c478bd9Sstevel@tonic-gate #endif
30627c478bd9Sstevel@tonic-gate 			return;
30637c478bd9Sstevel@tonic-gate 		}
30647c478bd9Sstevel@tonic-gate 		handle_def++;
30657c478bd9Sstevel@tonic-gate 	}
30667c478bd9Sstevel@tonic-gate 	card_info.pd_handle = pcmcia_cs_handle;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
30697c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
30707c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
30717c478bd9Sstevel@tonic-gate 			"pcmcia_create_dev_info: handle = %x\n",
30727c478bd9Sstevel@tonic-gate 			(int)card_info.pd_handle);
30737c478bd9Sstevel@tonic-gate #endif
30747c478bd9Sstevel@tonic-gate 	card_info.pd_type = -1; /* no type to start */
30757c478bd9Sstevel@tonic-gate 	card_info.pd_socket = socket;
30767c478bd9Sstevel@tonic-gate 	card_info.pd_function = 0;
30777c478bd9Sstevel@tonic-gate 	pcmcia_sockets[socket]->ls_functions = 1; /* default */
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	cisinfo.Socket = socket;
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if ((i = csx_ValidateCIS(card_info.pd_handle,
30827c478bd9Sstevel@tonic-gate 	    &cisinfo)) != SUCCESS ||
30837c478bd9Sstevel@tonic-gate 	    cisinfo.Tuples == 0) {
30847c478bd9Sstevel@tonic-gate 		/* no CIS means memory */
30857c478bd9Sstevel@tonic-gate 		(void) strcpy(card_info.pd_generic_name, "memory");
30867c478bd9Sstevel@tonic-gate 		card_info.pd_flags |= PCM_NAME_GENERIC |
30877c478bd9Sstevel@tonic-gate 			PCM_OTHER_NOCIS | PCM_NAME_1275;
30887c478bd9Sstevel@tonic-gate 		(void) strcpy(card_info.pd_bind_name, "pccard,memory");
30897c478bd9Sstevel@tonic-gate 		(void) strcpy(card_info.pd_generic_name, "memory");
30907c478bd9Sstevel@tonic-gate 		card_info.pd_type = PCM_TYPE_MEMORY;
30917c478bd9Sstevel@tonic-gate 	} else {
30927c478bd9Sstevel@tonic-gate 		int functions, lsocket;
30937c478bd9Sstevel@tonic-gate 		card_info.pd_tuples = cisinfo.Tuples;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 		/*
30967c478bd9Sstevel@tonic-gate 		 * how many functions on the card?
30977c478bd9Sstevel@tonic-gate 		 * we need to know and then we do one
30987c478bd9Sstevel@tonic-gate 		 * child node for each function using
30997c478bd9Sstevel@tonic-gate 		 * the function specific tuples.
31007c478bd9Sstevel@tonic-gate 		 */
31017c478bd9Sstevel@tonic-gate 		lsocket = CS_MAKE_SOCKET_NUMBER(socket, CS_GLOBAL_CIS);
31027c478bd9Sstevel@tonic-gate 		functions = pcmcia_num_funcs(lsocket,
31037c478bd9Sstevel@tonic-gate 		    card_info.pd_handle);
31047c478bd9Sstevel@tonic-gate 		pcmcia_sockets[socket]->ls_functions = functions;
31057c478bd9Sstevel@tonic-gate 		if (functions > 1) {
31067c478bd9Sstevel@tonic-gate 			card_info.pd_flags |= PCM_MULTI_FUNCTION;
31077c478bd9Sstevel@tonic-gate 		}
31087c478bd9Sstevel@tonic-gate 		for (i = 0; i < functions; i++) {
31097c478bd9Sstevel@tonic-gate 		    register int flags;
31107c478bd9Sstevel@tonic-gate 		    lsocket = CS_MAKE_SOCKET_NUMBER(socket, i);
31117c478bd9Sstevel@tonic-gate 		    card_info.pd_socket = socket;
31127c478bd9Sstevel@tonic-gate 		    card_info.pd_function = i;
31137c478bd9Sstevel@tonic-gate 			/*
31147c478bd9Sstevel@tonic-gate 			 * new name construction
31157c478bd9Sstevel@tonic-gate 			 */
31167c478bd9Sstevel@tonic-gate 		    if (functions != 1) {
31177c478bd9Sstevel@tonic-gate 			    /* need per function handle */
31187c478bd9Sstevel@tonic-gate 			    card_info.pd_function = i;
31197c478bd9Sstevel@tonic-gate 			    /* get new handle */
31207c478bd9Sstevel@tonic-gate 		    }
31217c478bd9Sstevel@tonic-gate 		    pcmcia_1275_name(lsocket, &card_info,
31227c478bd9Sstevel@tonic-gate 			card_info.pd_handle);
31237c478bd9Sstevel@tonic-gate 		    pcmcia_vers1_name(lsocket, &card_info,
31247c478bd9Sstevel@tonic-gate 			card_info.pd_handle);
31257c478bd9Sstevel@tonic-gate 		    pcmcia_generic_name(lsocket, &card_info,
31267c478bd9Sstevel@tonic-gate 			card_info.pd_handle);
31277c478bd9Sstevel@tonic-gate 		    flags = card_info.pd_flags;
31287c478bd9Sstevel@tonic-gate 		    if (!(flags & PCM_NAME_1275)) {
31297c478bd9Sstevel@tonic-gate 			if (flags & PCM_NAME_VERS1) {
31307c478bd9Sstevel@tonic-gate 			    (void) strcpy(card_info.pd_bind_name,
31317c478bd9Sstevel@tonic-gate 				PCMDEV_NAMEPREF);
31327c478bd9Sstevel@tonic-gate 			    card_info.pd_bind_name[sizeof (PCMDEV_NAMEPREF)] =
31337c478bd9Sstevel@tonic-gate 				',';
31347c478bd9Sstevel@tonic-gate 			    (void) strncpy(card_info.pd_bind_name +
31357c478bd9Sstevel@tonic-gate 				sizeof (PCMDEV_NAMEPREF),
31367c478bd9Sstevel@tonic-gate 				card_info.pd_vers1_name,
31377c478bd9Sstevel@tonic-gate 				MODMAXNAMELEN -
31387c478bd9Sstevel@tonic-gate 				sizeof (PCMDEV_NAMEPREF));
31397c478bd9Sstevel@tonic-gate 			    pcmcia_fix_string(card_info.pd_bind_name);
31407c478bd9Sstevel@tonic-gate 			} else {
31417c478bd9Sstevel@tonic-gate 				/*
31427c478bd9Sstevel@tonic-gate 				 * have a CIS but not the right info
31437c478bd9Sstevel@tonic-gate 				 * so treat as generic "pccard"
31447c478bd9Sstevel@tonic-gate 				 */
31457c478bd9Sstevel@tonic-gate 				(void) strcpy(card_info.pd_generic_name,
31467c478bd9Sstevel@tonic-gate 					"pccard,memory");
31477c478bd9Sstevel@tonic-gate 				card_info.pd_flags |= PCM_NAME_GENERIC;
31487c478bd9Sstevel@tonic-gate 				(void) strcpy(card_info.pd_bind_name,
31497c478bd9Sstevel@tonic-gate 					"pccard,memory");
31507c478bd9Sstevel@tonic-gate 			}
31517c478bd9Sstevel@tonic-gate 		    }
31527c478bd9Sstevel@tonic-gate 		    pcmcia_init_devinfo(pdip, &card_info);
31537c478bd9Sstevel@tonic-gate 		}
31547c478bd9Sstevel@tonic-gate 		return;
31557c478bd9Sstevel@tonic-gate 	}
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	pcmcia_init_devinfo(pdip, &card_info);
31587c478bd9Sstevel@tonic-gate }
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate /*
31617c478bd9Sstevel@tonic-gate  * pcmcia_init_devinfo()
31627c478bd9Sstevel@tonic-gate  *	if there isn't a device info structure, create one
31637c478bd9Sstevel@tonic-gate  *	if there is, we don't do much.
31647c478bd9Sstevel@tonic-gate  *
31657c478bd9Sstevel@tonic-gate  *	Note: this will need updating as 1275 finalizes their spec.
31667c478bd9Sstevel@tonic-gate  */
31677c478bd9Sstevel@tonic-gate static void
31687c478bd9Sstevel@tonic-gate pcmcia_init_devinfo(dev_info_t *pdip, struct pcm_device_info *info)
31697c478bd9Sstevel@tonic-gate {
31707c478bd9Sstevel@tonic-gate 	int unit;
31717c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
31727c478bd9Sstevel@tonic-gate 	char *name;
31737c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
31767c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
31777c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "init_devinfo(%s, %d)\n", info->pd_bind_name,
31787c478bd9Sstevel@tonic-gate 			info->pd_socket);
31797c478bd9Sstevel@tonic-gate #endif
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	/*
31827c478bd9Sstevel@tonic-gate 	 * find out if there is already an instance of this
31837c478bd9Sstevel@tonic-gate 	 * device.  We don't want to create a new one unnecessarily
31847c478bd9Sstevel@tonic-gate 	 */
31857c478bd9Sstevel@tonic-gate 	unit = CS_MAKE_SOCKET_NUMBER(info->pd_socket, info->pd_function);
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	dip = pcm_find_devinfo(pdip, info, unit);
31887c478bd9Sstevel@tonic-gate 	if ((dip != NULL) && (ddi_getprop(DDI_DEV_T_NONE, dip,
31897c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, PCM_DEV_SOCKET, -1) != -1)) {
31907c478bd9Sstevel@tonic-gate 		/* it already exist but isn't a .conf file */
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
31937c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
31947c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tfound existing device node (%s)\n",
31957c478bd9Sstevel@tonic-gate 				ddi_get_name(dip));
31967c478bd9Sstevel@tonic-gate #endif
31977c478bd9Sstevel@tonic-gate 		if (strlen(info->pd_vers1_name) > 0)
31987c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
31997c478bd9Sstevel@tonic-gate 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)
32027c478bd9Sstevel@tonic-gate 			ddi_get_parent_data(dip);
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
32057c478bd9Sstevel@tonic-gate 		    dip;
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 		ppd->ppd_active = 1;
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 		if (ndi_devi_online(dip, 0) == NDI_FAILURE) {
32107c478bd9Sstevel@tonic-gate 			pcmcia_sockets[info->pd_socket]-> \
32117c478bd9Sstevel@tonic-gate 			    ls_dip[info->pd_function] = NULL;
32127c478bd9Sstevel@tonic-gate 			ppd->ppd_active = 0;
32137c478bd9Sstevel@tonic-gate 		}
32147c478bd9Sstevel@tonic-gate 	} else {
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 		char *dtype;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
32197c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
32207c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia: create child [%s](%d): %s\n",
32217c478bd9Sstevel@tonic-gate 			    info->pd_bind_name, info->pd_socket,
32227c478bd9Sstevel@tonic-gate 			    info->pd_generic_name);
32237c478bd9Sstevel@tonic-gate #endif
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_NAME_GENERIC)
32267c478bd9Sstevel@tonic-gate 			name = info->pd_generic_name;
32277c478bd9Sstevel@tonic-gate 		else
32287c478bd9Sstevel@tonic-gate 			name = info->pd_bind_name;
32297c478bd9Sstevel@tonic-gate 
3230fa9e4066Sahrens 		if (ndi_devi_alloc(pdip, name, (pnode_t)DEVI_SID_NODEID,
32317c478bd9Sstevel@tonic-gate 		    &dip) !=
32327c478bd9Sstevel@tonic-gate 		    NDI_SUCCESS) {
32337c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
32347c478bd9Sstevel@tonic-gate 			    "pcmcia: unable to create device [%s](%d)\n",
32357c478bd9Sstevel@tonic-gate 			    name, info->pd_socket);
32367c478bd9Sstevel@tonic-gate 			return;
32377c478bd9Sstevel@tonic-gate 		}
32387c478bd9Sstevel@tonic-gate 		/*
32397c478bd9Sstevel@tonic-gate 		 * construct the "compatible" property if the device
32407c478bd9Sstevel@tonic-gate 		 * has a generic name
32417c478bd9Sstevel@tonic-gate 		 */
32427c478bd9Sstevel@tonic-gate 		pcmcia_add_compatible(dip, info);
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
32457c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 		ppd->ppd_socket = info->pd_socket;
32487c478bd9Sstevel@tonic-gate 		ppd->ppd_function = info->pd_function;
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 		/*
32517c478bd9Sstevel@tonic-gate 		 * add the "socket" property
32527c478bd9Sstevel@tonic-gate 		 * the value of this property contains the logical PCMCIA
32537c478bd9Sstevel@tonic-gate 		 * socket number the device has been inserted in, along
32547c478bd9Sstevel@tonic-gate 		 * with the function # if the device is part of a
32557c478bd9Sstevel@tonic-gate 		 * multi-function device.
32567c478bd9Sstevel@tonic-gate 		 */
32577c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
32587c478bd9Sstevel@tonic-gate 		    PCM_DEV_SOCKET, unit);
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 		if (info->pd_flags & PCM_MULTI_FUNCTION)
32617c478bd9Sstevel@tonic-gate 			ppd->ppd_flags |= PPD_CARD_MULTI;
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 		/*
32647c478bd9Sstevel@tonic-gate 		 * determine all the properties we need for PPD
32657c478bd9Sstevel@tonic-gate 		 * then create the properties
32667c478bd9Sstevel@tonic-gate 		 */
32677c478bd9Sstevel@tonic-gate 		/* socket is unique */
32687c478bd9Sstevel@tonic-gate 		pcmcia_find_regs(dip, info, ppd);
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 		ppd->ppd_intr = pcmcia_need_intr(unit, info);
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 		if (ppd->ppd_nreg > 0)
32737c478bd9Sstevel@tonic-gate 			(void) ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
32747c478bd9Sstevel@tonic-gate 			    "reg", (int *)ppd->ppd_reg, ppd->ppd_nreg *
32757c478bd9Sstevel@tonic-gate 			    sizeof (struct pcm_regs) / sizeof (int));
32767c478bd9Sstevel@tonic-gate 		if (ppd->ppd_intr) {
32777c478bd9Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
32787c478bd9Sstevel@tonic-gate 			    "interrupts", ppd->ppd_intr);
32797c478bd9Sstevel@tonic-gate 			ppd->ppd_intrspec =
32807c478bd9Sstevel@tonic-gate 			    kmem_zalloc(sizeof (struct intrspec), KM_SLEEP);
32817c478bd9Sstevel@tonic-gate 		}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 		/* set parent private - our own format */
32847c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(dip, (caddr_t)ppd);
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 		/* init the device type */
32877c478bd9Sstevel@tonic-gate 		if (info->pd_type >= 0 &&
32887c478bd9Sstevel@tonic-gate 		    info->pd_type < (sizeof (pcmcia_dev_type) /
32897c478bd9Sstevel@tonic-gate 		    (sizeof (char *))))
32907c478bd9Sstevel@tonic-gate 			dtype = pcmcia_dev_type[info->pd_type];
32917c478bd9Sstevel@tonic-gate 		else
32927c478bd9Sstevel@tonic-gate 			dtype = "unknown";
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 		if (strlen(info->pd_vers1_name) > 0)
32957c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
32967c478bd9Sstevel@tonic-gate 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
32997c478bd9Sstevel@tonic-gate 		    PCM_DEVICETYPE, dtype);
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 		/* set PC Card as active and present in socket */
33027c478bd9Sstevel@tonic-gate 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
33037c478bd9Sstevel@tonic-gate 			dip;
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 		ppd->ppd_active = 1;
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 		/*
33087c478bd9Sstevel@tonic-gate 		 * We should not call ndi_devi_online here if
33097c478bd9Sstevel@tonic-gate 		 * pcmcia attach is in progress. This causes a deadlock.
33107c478bd9Sstevel@tonic-gate 		 */
33117c478bd9Sstevel@tonic-gate 		if (pcmcia_dip != dip) {
33127c478bd9Sstevel@tonic-gate 			if (ndi_devi_online_async(dip, 0)
33137c478bd9Sstevel@tonic-gate 			    != NDI_SUCCESS) {
33147c478bd9Sstevel@tonic-gate 				pcmcia_sockets[info->pd_socket]->\
33157c478bd9Sstevel@tonic-gate 				ls_dip[info->pd_function] = NULL;
33167c478bd9Sstevel@tonic-gate 				pcmcia_ppd_free(ppd);
33177c478bd9Sstevel@tonic-gate 				(void) ndi_devi_free(dip);
33187c478bd9Sstevel@tonic-gate 				return;
33197c478bd9Sstevel@tonic-gate 			}
33207c478bd9Sstevel@tonic-gate 		}
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
33237c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
33247c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tjust added \"active\" to %s in %d\n",
33257c478bd9Sstevel@tonic-gate 			ddi_get_name(dip), info->pd_socket);
33267c478bd9Sstevel@tonic-gate #endif
33277c478bd9Sstevel@tonic-gate 	}
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	/*
33307c478bd9Sstevel@tonic-gate 	 * inform the event manager that a child was added
33317c478bd9Sstevel@tonic-gate 	 * to the device tree.
33327c478bd9Sstevel@tonic-gate 	 */
33337c478bd9Sstevel@tonic-gate 	pcm_event_manager(PCE_DEV_IDENT, unit, ddi_get_name(dip));
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
33367c478bd9Sstevel@tonic-gate 	if (pcmcia_debug > 1) {
33377c478bd9Sstevel@tonic-gate 		pcmcia_dump_minors(dip);
33387c478bd9Sstevel@tonic-gate 	}
33397c478bd9Sstevel@tonic-gate #endif
33407c478bd9Sstevel@tonic-gate }
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate /*
33437c478bd9Sstevel@tonic-gate  * free any allocated parent-private data
33447c478bd9Sstevel@tonic-gate  */
33457c478bd9Sstevel@tonic-gate static void
33467c478bd9Sstevel@tonic-gate pcmcia_ppd_free(struct pcmcia_parent_private *ppd)
33477c478bd9Sstevel@tonic-gate {
33487c478bd9Sstevel@tonic-gate 	size_t len;
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	if (ppd->ppd_nreg != 0) {
33517c478bd9Sstevel@tonic-gate 		len = ppd->ppd_nreg * sizeof (uint32_t) * 3;
33527c478bd9Sstevel@tonic-gate 		kmem_free(ppd->ppd_reg, len);
33537c478bd9Sstevel@tonic-gate 		len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
33547c478bd9Sstevel@tonic-gate 		kmem_free(ppd->ppd_assigned, len);
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	/*
33587c478bd9Sstevel@tonic-gate 	 * pcmcia only allocates 1 intrspec today
33597c478bd9Sstevel@tonic-gate 	 */
33607c478bd9Sstevel@tonic-gate 	if (ppd->ppd_intr != 0) {
33617c478bd9Sstevel@tonic-gate 		len = sizeof (struct intrspec) * ppd->ppd_intr;
33627c478bd9Sstevel@tonic-gate 		kmem_free(ppd->ppd_intrspec, len);
33637c478bd9Sstevel@tonic-gate 	}
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	kmem_free(ppd, sizeof (*ppd));
33667c478bd9Sstevel@tonic-gate }
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /*
33707c478bd9Sstevel@tonic-gate  * pcmcia_get_devinfo(socket)
33717c478bd9Sstevel@tonic-gate  *	entry point to allow finding the device info structure
33727c478bd9Sstevel@tonic-gate  *	for a given logical socket.  Used by event manager
33737c478bd9Sstevel@tonic-gate  */
33747c478bd9Sstevel@tonic-gate dev_info_t *
33757c478bd9Sstevel@tonic-gate pcmcia_get_devinfo(int socket)
33767c478bd9Sstevel@tonic-gate {
33777c478bd9Sstevel@tonic-gate 	int func = CS_GET_FUNCTION_NUMBER(socket);
33787c478bd9Sstevel@tonic-gate 	socket = CS_GET_SOCKET_NUMBER(socket);
33797c478bd9Sstevel@tonic-gate 	if (pcmcia_sockets[socket])
33807c478bd9Sstevel@tonic-gate 		return (pcmcia_sockets[socket]->ls_dip[func]);
33817c478bd9Sstevel@tonic-gate 	return ((dev_info_t *)NULL);
33827c478bd9Sstevel@tonic-gate }
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate /*
33857c478bd9Sstevel@tonic-gate  * CSGetCookiesAndDip()
33867c478bd9Sstevel@tonic-gate  *	get info needed by CS to setup soft interrupt handler and provide
33877c478bd9Sstevel@tonic-gate  *		socket-specific adapter information
33887c478bd9Sstevel@tonic-gate  */
33897c478bd9Sstevel@tonic-gate static int
33907c478bd9Sstevel@tonic-gate GetCookiesAndDip(sservice_t *serv)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *socket;
33937c478bd9Sstevel@tonic-gate 	csss_adapter_info_t *ai;
33947c478bd9Sstevel@tonic-gate 	int sock;
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	sock = CS_GET_SOCKET_NUMBER(serv->get_cookies.socket);
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	if (sock >= pcmcia_num_sockets ||
33997c478bd9Sstevel@tonic-gate 	    (int)serv->get_cookies.socket < 0)
34007c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate 	socket = pcmcia_sockets[sock];
34037c478bd9Sstevel@tonic-gate 	ai = &serv->get_cookies.adapter_info;
34047c478bd9Sstevel@tonic-gate 	serv->get_cookies.dip = socket->ls_adapter->pca_dip;
34057c478bd9Sstevel@tonic-gate 	serv->get_cookies.iblock = socket->ls_adapter->pca_iblock;
34067c478bd9Sstevel@tonic-gate 	serv->get_cookies.idevice = socket->ls_adapter->pca_idev;
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 	/*
34097c478bd9Sstevel@tonic-gate 	 * Setup the adapter info for Card Services
34107c478bd9Sstevel@tonic-gate 	 */
34117c478bd9Sstevel@tonic-gate 	(void) strcpy(ai->name, socket->ls_adapter->pca_name);
34127c478bd9Sstevel@tonic-gate 	ai->major = socket->ls_adapter->pca_module;
34137c478bd9Sstevel@tonic-gate 	ai->minor = socket->ls_adapter->pca_unit;
34147c478bd9Sstevel@tonic-gate 	ai->number = socket->ls_adapter->pca_number;
34157c478bd9Sstevel@tonic-gate 	ai->num_sockets = socket->ls_adapter->pca_numsockets;
34167c478bd9Sstevel@tonic-gate 	ai->first_socket = socket->ls_adapter->pca_first_socket;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	return (SUCCESS);
34197c478bd9Sstevel@tonic-gate }
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate /*
34227c478bd9Sstevel@tonic-gate  * Note:
34237c478bd9Sstevel@tonic-gate  *	The following functions that start with 'SS'
34247c478bd9Sstevel@tonic-gate  *	implement SocketServices interfaces.  They
34257c478bd9Sstevel@tonic-gate  *	simply map the socket and/or window number to
34267c478bd9Sstevel@tonic-gate  *	the adapter specific number based on the general
34277c478bd9Sstevel@tonic-gate  *	value that CardServices uses.
34287c478bd9Sstevel@tonic-gate  *
34297c478bd9Sstevel@tonic-gate  *	See the descriptions in SocketServices for
34307c478bd9Sstevel@tonic-gate  *	details.  Also refer to specific adapter drivers
34317c478bd9Sstevel@tonic-gate  *	for implementation reference.
34327c478bd9Sstevel@tonic-gate  */
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate static int
34357c478bd9Sstevel@tonic-gate SSGetAdapter(get_adapter_t *adapter)
34367c478bd9Sstevel@tonic-gate {
34377c478bd9Sstevel@tonic-gate 	int n;
34387c478bd9Sstevel@tonic-gate 	get_adapter_t info;
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	adapter->state = (unsigned)0xFFFFFFFF;
34417c478bd9Sstevel@tonic-gate 	adapter->SCRouting = 0xFFFFFFFF;
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 	for (n = 0; n < pcmcia_num_adapters; n++) {
34447c478bd9Sstevel@tonic-gate 		GET_ADAPTER(pcmcia_adapters[n]->pca_if,
34457c478bd9Sstevel@tonic-gate 			pcmcia_adapters[n]->pca_dip, &info);
34467c478bd9Sstevel@tonic-gate 		adapter->state &= info.state;
34477c478bd9Sstevel@tonic-gate 		adapter->SCRouting &= info.SCRouting;
34487c478bd9Sstevel@tonic-gate 	}
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	return (SUCCESS);
34517c478bd9Sstevel@tonic-gate }
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate static int
34547c478bd9Sstevel@tonic-gate SSGetPage(get_page_t *page)
34557c478bd9Sstevel@tonic-gate {
34567c478bd9Sstevel@tonic-gate 	pcmcia_logical_window_t *window;
34577c478bd9Sstevel@tonic-gate 	get_page_t newpage;
34587c478bd9Sstevel@tonic-gate 	int retval, win;
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 	if (page->window > pcmcia_num_windows) {
34617c478bd9Sstevel@tonic-gate 		return (BAD_WINDOW);
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	window = pcmcia_windows[page->window];
34657c478bd9Sstevel@tonic-gate 	newpage = *page;
34667c478bd9Sstevel@tonic-gate 	win = newpage.window = window->lw_window; /* real window */
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	retval = GET_PAGE(window->lw_if, window->lw_adapter->pca_dip,
34697c478bd9Sstevel@tonic-gate 		&newpage);
34707c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
34717c478bd9Sstevel@tonic-gate 		*page = newpage;
34727c478bd9Sstevel@tonic-gate 		page->window = win;
34737c478bd9Sstevel@tonic-gate 	}
34747c478bd9Sstevel@tonic-gate 	return (retval);
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate static int
34787c478bd9Sstevel@tonic-gate SSGetSocket(get_socket_t *socket)
34797c478bd9Sstevel@tonic-gate {
34807c478bd9Sstevel@tonic-gate 	int retval, sock;
34817c478bd9Sstevel@tonic-gate 	get_socket_t newsocket;
34827c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	sock = socket->socket;
34857c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
34867c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL) {
34877c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
34887c478bd9Sstevel@tonic-gate 	}
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	newsocket = *socket;
34917c478bd9Sstevel@tonic-gate 	newsocket.socket = sockp->ls_socket;
34927c478bd9Sstevel@tonic-gate 	retval = GET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
34937c478bd9Sstevel@tonic-gate 	    &newsocket);
34947c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
34957c478bd9Sstevel@tonic-gate 		newsocket.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
34967c478bd9Sstevel@tonic-gate 		    newsocket.VccLevel,
34977c478bd9Sstevel@tonic-gate 		    VCC);
34987c478bd9Sstevel@tonic-gate 		newsocket.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
34997c478bd9Sstevel@tonic-gate 		    newsocket.Vpp1Level,
35007c478bd9Sstevel@tonic-gate 		    VPP1);
35017c478bd9Sstevel@tonic-gate 		newsocket.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
35027c478bd9Sstevel@tonic-gate 		    newsocket.Vpp2Level,
35037c478bd9Sstevel@tonic-gate 		    VPP2);
35047c478bd9Sstevel@tonic-gate 		*socket = newsocket;
35057c478bd9Sstevel@tonic-gate 		socket->socket = sock;
35067c478bd9Sstevel@tonic-gate 	}
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	return (retval);
35097c478bd9Sstevel@tonic-gate }
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate static int
35127c478bd9Sstevel@tonic-gate SSGetStatus(get_ss_status_t *status)
35137c478bd9Sstevel@tonic-gate {
35147c478bd9Sstevel@tonic-gate 	get_ss_status_t newstat;
35157c478bd9Sstevel@tonic-gate 	int sock, retval;
35167c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	sock = status->socket;
35197c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
35207c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL) {
35217c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	newstat = *status;
35257c478bd9Sstevel@tonic-gate 	newstat.socket = sockp->ls_socket;
35267c478bd9Sstevel@tonic-gate 	retval = GET_STATUS(sockp->ls_if, sockp->ls_adapter->pca_dip,
35277c478bd9Sstevel@tonic-gate 	    &newstat);
35287c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
35297c478bd9Sstevel@tonic-gate 		*status = newstat;
35307c478bd9Sstevel@tonic-gate 		status->socket = sock;
35317c478bd9Sstevel@tonic-gate 	}
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	return (retval);
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate static int
35377c478bd9Sstevel@tonic-gate SSGetWindow(get_window_t *window)
35387c478bd9Sstevel@tonic-gate {
35397c478bd9Sstevel@tonic-gate 	int win, retval;
35407c478bd9Sstevel@tonic-gate 	get_window_t newwin;
35417c478bd9Sstevel@tonic-gate 	pcmcia_logical_window_t *winp;
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	win = window->window;
35447c478bd9Sstevel@tonic-gate 	winp = pcmcia_windows[win];
35457c478bd9Sstevel@tonic-gate 	newwin = *window;
35467c478bd9Sstevel@tonic-gate 	newwin.window = winp->lw_window;
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate 	retval = GET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
35497c478bd9Sstevel@tonic-gate 	    &newwin);
35507c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
35517c478bd9Sstevel@tonic-gate 		newwin.socket = winp->lw_socket;
35527c478bd9Sstevel@tonic-gate 		newwin.window = win;
35537c478bd9Sstevel@tonic-gate 		*window = newwin;
35547c478bd9Sstevel@tonic-gate 	}
35557c478bd9Sstevel@tonic-gate 	return (retval);
35567c478bd9Sstevel@tonic-gate }
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate /*
35597c478bd9Sstevel@tonic-gate  * SSInquireAdapter()
35607c478bd9Sstevel@tonic-gate  *	Get the capabilities of the "generic" adapter
35617c478bd9Sstevel@tonic-gate  *	we are exporting to CS.
35627c478bd9Sstevel@tonic-gate  */
35637c478bd9Sstevel@tonic-gate static int
35647c478bd9Sstevel@tonic-gate SSInquireAdapter(inquire_adapter_t *adapter)
35657c478bd9Sstevel@tonic-gate {
35667c478bd9Sstevel@tonic-gate 	adapter->NumSockets = pcmcia_num_sockets;
35677c478bd9Sstevel@tonic-gate 	adapter->NumWindows = pcmcia_num_windows;
35687c478bd9Sstevel@tonic-gate 	adapter->NumEDCs = 0;
35697c478bd9Sstevel@tonic-gate 	/*
35707c478bd9Sstevel@tonic-gate 	 * notes: Adapter Capabilities are going to be difficult to
35717c478bd9Sstevel@tonic-gate 	 * determine with reliability.	Fortunately, most of them
35727c478bd9Sstevel@tonic-gate 	 * don't matter under Solaris or can be handled transparently
35737c478bd9Sstevel@tonic-gate 	 */
35747c478bd9Sstevel@tonic-gate 	adapter->AdpCaps = 0;	/* need to fix these */
35757c478bd9Sstevel@tonic-gate 	/*
35767c478bd9Sstevel@tonic-gate 	 * interrupts need a little work.  For x86, the valid IRQs will
35777c478bd9Sstevel@tonic-gate 	 * be restricted to those that the system has exported to the nexus.
35787c478bd9Sstevel@tonic-gate 	 * for SPARC, it will be the DoRight values.
35797c478bd9Sstevel@tonic-gate 	 */
35807c478bd9Sstevel@tonic-gate 	adapter->ActiveHigh = 0;
35817c478bd9Sstevel@tonic-gate 	adapter->ActiveLow = 0;
35827c478bd9Sstevel@tonic-gate 	adapter->power_entry = pcmcia_power_table; /* until we resolve this */
35837c478bd9Sstevel@tonic-gate 	adapter->NumPower = pcmcia_num_power;
35847c478bd9Sstevel@tonic-gate 	return (SUCCESS);
35857c478bd9Sstevel@tonic-gate }
35867c478bd9Sstevel@tonic-gate 
35877c478bd9Sstevel@tonic-gate static int
35887c478bd9Sstevel@tonic-gate SSInquireSocket(inquire_socket_t *socket)
35897c478bd9Sstevel@tonic-gate {
35907c478bd9Sstevel@tonic-gate 	int retval, sock;
35917c478bd9Sstevel@tonic-gate 	inquire_socket_t newsocket;
35927c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	sock = socket->socket;
35957c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
35967c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL)
35977c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
35987c478bd9Sstevel@tonic-gate 	newsocket = *socket;
35997c478bd9Sstevel@tonic-gate 	newsocket.socket = sockp->ls_socket;
36007c478bd9Sstevel@tonic-gate 	retval = INQUIRE_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
36017c478bd9Sstevel@tonic-gate 	    &newsocket);
36027c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
36037c478bd9Sstevel@tonic-gate 		*socket = newsocket;
36047c478bd9Sstevel@tonic-gate 		socket->socket = sock;
36057c478bd9Sstevel@tonic-gate 	}
36067c478bd9Sstevel@tonic-gate 	return (retval);
36077c478bd9Sstevel@tonic-gate }
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate static int
36107c478bd9Sstevel@tonic-gate SSInquireWindow(inquire_window_t *window)
36117c478bd9Sstevel@tonic-gate {
36127c478bd9Sstevel@tonic-gate 	int retval, win;
36137c478bd9Sstevel@tonic-gate 	pcmcia_logical_window_t *winp;
36147c478bd9Sstevel@tonic-gate 	inquire_window_t newwin;
36157c478bd9Sstevel@tonic-gate 	int slide;
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate 	win = window->window;
36187c478bd9Sstevel@tonic-gate 	if (win > pcmcia_num_windows)
36197c478bd9Sstevel@tonic-gate 		return (BAD_WINDOW);
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	winp = pcmcia_windows[win];
36227c478bd9Sstevel@tonic-gate 	newwin = *window;
36237c478bd9Sstevel@tonic-gate 	newwin.window = winp->lw_window;
36247c478bd9Sstevel@tonic-gate 	retval = INQUIRE_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
36257c478bd9Sstevel@tonic-gate 	    &newwin);
36267c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
36277c478bd9Sstevel@tonic-gate 		if (pcmcia_debug > 1)
36287c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "SSInquireWindow: win=%d, pwin=%d\n",
36297c478bd9Sstevel@tonic-gate 				win, newwin.window);
36307c478bd9Sstevel@tonic-gate #endif
36317c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
36327c478bd9Sstevel@tonic-gate 		*window = newwin;
36337c478bd9Sstevel@tonic-gate 		/* just in case */
36347c478bd9Sstevel@tonic-gate 		window->iowin_char.IOWndCaps &= ~WC_BASE;
36357c478bd9Sstevel@tonic-gate 		slide = winp->lw_adapter->pca_first_socket;
36367c478bd9Sstevel@tonic-gate 		/*
36377c478bd9Sstevel@tonic-gate 		 * note that sockets are relative to the adapter.
36387c478bd9Sstevel@tonic-gate 		 * we have to adjust the bits to show a logical
36397c478bd9Sstevel@tonic-gate 		 * version.
36407c478bd9Sstevel@tonic-gate 		 */
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 		pcm_fix_bits(newwin.Sockets, window->Sockets, slide, 0);
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
36457c478bd9Sstevel@tonic-gate 		if (pcmcia_debug > 1) {
36467c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "iw: orig bits=%x, new bits=%x\n",
36477c478bd9Sstevel@tonic-gate 				(int)*(uint32_t *)newwin.Sockets,
36487c478bd9Sstevel@tonic-gate 				(int)*(uint32_t *)window->Sockets);
36497c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\t%x.%x.%x\n", window->WndCaps,
36507c478bd9Sstevel@tonic-gate 				window->mem_win_char.MemWndCaps,
36517c478bd9Sstevel@tonic-gate 				window->mem_win_char.MinSize);
36527c478bd9Sstevel@tonic-gate 		}
36537c478bd9Sstevel@tonic-gate #endif
36547c478bd9Sstevel@tonic-gate 		window->window = win;
36557c478bd9Sstevel@tonic-gate 	}
36567c478bd9Sstevel@tonic-gate 	return (retval);
36577c478bd9Sstevel@tonic-gate }
36587c478bd9Sstevel@tonic-gate 
36597c478bd9Sstevel@tonic-gate static int
36607c478bd9Sstevel@tonic-gate SSResetSocket(int socket, int mode)
36617c478bd9Sstevel@tonic-gate {
36627c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	if (socket >= pcmcia_num_sockets ||
36657c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[socket]) == NULL)
36667c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 	return (RESET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
36697c478bd9Sstevel@tonic-gate 	    sockp->ls_socket, mode));
36707c478bd9Sstevel@tonic-gate }
36717c478bd9Sstevel@tonic-gate 
36727c478bd9Sstevel@tonic-gate static int
36737c478bd9Sstevel@tonic-gate SSSetPage(set_page_t *page)
36747c478bd9Sstevel@tonic-gate {
36757c478bd9Sstevel@tonic-gate 	int window, retval;
36767c478bd9Sstevel@tonic-gate 	set_page_t newpage;
36777c478bd9Sstevel@tonic-gate 	pcmcia_logical_window_t *winp;
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	window = page->window;
36807c478bd9Sstevel@tonic-gate 	if (window > pcmcia_num_windows) {
36817c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
36827c478bd9Sstevel@tonic-gate 		if (pcmcia_debug > 1)
36837c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "SSSetPage: window=%d (of %d)\n",
36847c478bd9Sstevel@tonic-gate 				window, pcmcia_num_windows);
36857c478bd9Sstevel@tonic-gate #endif
36867c478bd9Sstevel@tonic-gate 		return (BAD_WINDOW);
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate 	winp = pcmcia_windows[window];
36907c478bd9Sstevel@tonic-gate 	newpage = *page;
36917c478bd9Sstevel@tonic-gate 	newpage.window = winp->lw_window;
36927c478bd9Sstevel@tonic-gate 	retval = SET_PAGE(winp->lw_if, winp->lw_adapter->pca_dip, &newpage);
36937c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
36947c478bd9Sstevel@tonic-gate 		newpage.window = window;
36957c478bd9Sstevel@tonic-gate 		*page = newpage;
36967c478bd9Sstevel@tonic-gate 	}
36977c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
36987c478bd9Sstevel@tonic-gate 	if ((pcmcia_debug > 1) && retval != SUCCESS)
36997c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\tSetPage: returning error %x\n", retval);
37007c478bd9Sstevel@tonic-gate #endif
37017c478bd9Sstevel@tonic-gate 	return (retval);
37027c478bd9Sstevel@tonic-gate }
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate static int
37057c478bd9Sstevel@tonic-gate SSSetWindow(set_window_t *win)
37067c478bd9Sstevel@tonic-gate {
37077c478bd9Sstevel@tonic-gate 	int socket, window, retval, func;
37087c478bd9Sstevel@tonic-gate 	set_window_t newwin;
37097c478bd9Sstevel@tonic-gate 	pcmcia_logical_window_t *winp;
37107c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	window = win->window;
37137c478bd9Sstevel@tonic-gate 	if (window > pcmcia_num_windows)
37147c478bd9Sstevel@tonic-gate 		return (BAD_WINDOW);
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	socket = CS_GET_SOCKET_NUMBER(win->socket);
37177c478bd9Sstevel@tonic-gate 	func = CS_GET_FUNCTION_NUMBER(win->socket);
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	if (socket > pcmcia_num_sockets ||
37207c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[socket]) == NULL) {
37217c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
37227c478bd9Sstevel@tonic-gate 	}
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 	winp = pcmcia_windows[window];
37257c478bd9Sstevel@tonic-gate 	winp->lw_socket = win->socket; /* reverse map */
37267c478bd9Sstevel@tonic-gate 	newwin = *win;
37277c478bd9Sstevel@tonic-gate 	newwin.window = winp->lw_window;
37287c478bd9Sstevel@tonic-gate 	newwin.socket = sockp->ls_socket;
37297c478bd9Sstevel@tonic-gate 	newwin.child = sockp->ls_dip[func]; /* so we carry the dip around */
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	retval = SET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip, &newwin);
37327c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
37337c478bd9Sstevel@tonic-gate 		newwin.window = window;
37347c478bd9Sstevel@tonic-gate 		newwin.socket = winp->lw_socket;
37357c478bd9Sstevel@tonic-gate 		*win = newwin;
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate 	return (retval);
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate static int
37417c478bd9Sstevel@tonic-gate SSSetSocket(set_socket_t *socket)
37427c478bd9Sstevel@tonic-gate {
37437c478bd9Sstevel@tonic-gate 	int sock, retval;
37447c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
37457c478bd9Sstevel@tonic-gate 	set_socket_t newsock;
37467c478bd9Sstevel@tonic-gate 
37477c478bd9Sstevel@tonic-gate 	sock = socket->socket;
37487c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
37497c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL) {
37507c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
37517c478bd9Sstevel@tonic-gate 	}
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	newsock = *socket;
37547c478bd9Sstevel@tonic-gate 	/* note: we force CS to always get insert/removal events */
37557c478bd9Sstevel@tonic-gate 	sockp->ls_cs_events = pcm_mapevents(newsock.SCIntMask) |
37567c478bd9Sstevel@tonic-gate 		PCE_E2M(PCE_CARD_INSERT) | PCE_E2M(PCE_CARD_REMOVAL);
37577c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
37587c478bd9Sstevel@tonic-gate 	if (pcmcia_debug > 1)
37597c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
37607c478bd9Sstevel@tonic-gate 			"SetSocket: SCIntMask = %x\n", newsock.SCIntMask);
37617c478bd9Sstevel@tonic-gate #endif
37627c478bd9Sstevel@tonic-gate 	newsock.socket = sockp->ls_socket;
37637c478bd9Sstevel@tonic-gate 	newsock.VccLevel = pcmcia_map_power_set(sockp->ls_adapter,
37647c478bd9Sstevel@tonic-gate 	    newsock.VccLevel, VCC);
37657c478bd9Sstevel@tonic-gate 	newsock.Vpp1Level = pcmcia_map_power_set(sockp->ls_adapter,
37667c478bd9Sstevel@tonic-gate 	    newsock.Vpp1Level, VPP1);
37677c478bd9Sstevel@tonic-gate 	newsock.Vpp2Level = pcmcia_map_power_set(sockp->ls_adapter,
37687c478bd9Sstevel@tonic-gate 	    newsock.Vpp2Level, VPP2);
37697c478bd9Sstevel@tonic-gate 	retval = SET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
37707c478bd9Sstevel@tonic-gate 	    &newsock);
37717c478bd9Sstevel@tonic-gate 	if (retval == SUCCESS) {
37727c478bd9Sstevel@tonic-gate 		newsock.socket = sock;
37737c478bd9Sstevel@tonic-gate 		newsock.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
37747c478bd9Sstevel@tonic-gate 		    newsock.VccLevel,
37757c478bd9Sstevel@tonic-gate 		    VCC);
37767c478bd9Sstevel@tonic-gate 		newsock.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
37777c478bd9Sstevel@tonic-gate 		    newsock.Vpp1Level,
37787c478bd9Sstevel@tonic-gate 		    VPP1);
37797c478bd9Sstevel@tonic-gate 		newsock.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
37807c478bd9Sstevel@tonic-gate 		    newsock.Vpp2Level,
37817c478bd9Sstevel@tonic-gate 		    VPP2);
37827c478bd9Sstevel@tonic-gate 		*socket = newsock;
37837c478bd9Sstevel@tonic-gate 		if (socket->IREQRouting & IRQ_ENABLE) {
37847c478bd9Sstevel@tonic-gate 			sockp->ls_flags |= PCS_IRQ_ENABLED;
37857c478bd9Sstevel@tonic-gate 		} else {
37867c478bd9Sstevel@tonic-gate 			sockp->ls_flags &= ~PCS_IRQ_ENABLED;
37877c478bd9Sstevel@tonic-gate 		}
37887c478bd9Sstevel@tonic-gate 	}
37897c478bd9Sstevel@tonic-gate 	return (retval);
37907c478bd9Sstevel@tonic-gate }
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate /*
37937c478bd9Sstevel@tonic-gate  * SSSetIRQHandler()
37947c478bd9Sstevel@tonic-gate  *	arrange for IRQ to be allocated if appropriate and always
37957c478bd9Sstevel@tonic-gate  *	arrange that PC Card interrupt handlers get called.
37967c478bd9Sstevel@tonic-gate  */
37977c478bd9Sstevel@tonic-gate static int
37987c478bd9Sstevel@tonic-gate SSSetIRQHandler(set_irq_handler_t *handler)
37997c478bd9Sstevel@tonic-gate {
38007c478bd9Sstevel@tonic-gate 	int sock, retval, func;
38017c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
38027c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
38037c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
38047c478bd9Sstevel@tonic-gate 	ddi_iblock_cookie_t iblk;
38057c478bd9Sstevel@tonic-gate 	ddi_idevice_cookie_t idev;
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
38087c478bd9Sstevel@tonic-gate 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
38097c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
38107c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL) {
38117c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
38127c478bd9Sstevel@tonic-gate 	}
38137c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
38147c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "SSSetIRQHandler: socket=%x, function=%x\n",
38177c478bd9Sstevel@tonic-gate 			sock, func);
38187c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\thandler(%p): socket=%x, irq=%x, id=%x\n",
38197c478bd9Sstevel@tonic-gate 			(void *)handler->handler, handler->socket, handler->irq,
38207c478bd9Sstevel@tonic-gate 			handler->handler_id);
38217c478bd9Sstevel@tonic-gate 	}
38227c478bd9Sstevel@tonic-gate #endif
38237c478bd9Sstevel@tonic-gate 	dip = sockp->ls_dip[func];
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	handler->iblk_cookie = &iblk;
38287c478bd9Sstevel@tonic-gate 	handler->idev_cookie = &idev;
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	retval = ddi_add_intr(dip, 0, handler->iblk_cookie,
38317c478bd9Sstevel@tonic-gate 	    handler->idev_cookie,
38327c478bd9Sstevel@tonic-gate 	    (uint32_t(*)(caddr_t)) handler->handler,
38337c478bd9Sstevel@tonic-gate 	    handler->arg1);
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	if (retval == DDI_SUCCESS) {
38367c478bd9Sstevel@tonic-gate 		handler->iblk_cookie = &sockp->ls_iblk;
38377c478bd9Sstevel@tonic-gate 		handler->idev_cookie = &sockp->ls_idev;
38387c478bd9Sstevel@tonic-gate 		handler->irq = ppd->ppd_intrspec->intrspec_vec;
38397c478bd9Sstevel@tonic-gate 		retval = SUCCESS;
38407c478bd9Sstevel@tonic-gate 	} else {
38417c478bd9Sstevel@tonic-gate 		retval = sockp->ls_error;
38427c478bd9Sstevel@tonic-gate 	}
38437c478bd9Sstevel@tonic-gate 	return (retval);
38447c478bd9Sstevel@tonic-gate }
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate /*
38477c478bd9Sstevel@tonic-gate  * SSClearIRQHandler()
38487c478bd9Sstevel@tonic-gate  *	Arrange to have the interrupt handler specified removed
38497c478bd9Sstevel@tonic-gate  *	from the interrupt list.
38507c478bd9Sstevel@tonic-gate  */
38517c478bd9Sstevel@tonic-gate static int
38527c478bd9Sstevel@tonic-gate SSClearIRQHandler(clear_irq_handler_t *handler)
38537c478bd9Sstevel@tonic-gate {
38547c478bd9Sstevel@tonic-gate 	int sock, func;
38557c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
38567c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
38597c478bd9Sstevel@tonic-gate 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
38627c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
38657c478bd9Sstevel@tonic-gate 			"SSClearIRQHandler: socket=%x, function=%x\n",
38667c478bd9Sstevel@tonic-gate 			sock, func);
38677c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
38687c478bd9Sstevel@tonic-gate 			"\thandler(%p): socket=%x, id=%x\n",
38697c478bd9Sstevel@tonic-gate 			(void *)handler, handler->socket,
38707c478bd9Sstevel@tonic-gate 			handler->handler_id);
38717c478bd9Sstevel@tonic-gate 	}
38727c478bd9Sstevel@tonic-gate #endif
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 	if (sock > pcmcia_num_sockets ||
38757c478bd9Sstevel@tonic-gate 		(sockp = pcmcia_sockets[sock]) == NULL) {
38767c478bd9Sstevel@tonic-gate 		return (BAD_SOCKET);
38777c478bd9Sstevel@tonic-gate 	}
38787c478bd9Sstevel@tonic-gate 	dip = sockp->ls_dip[func];
38797c478bd9Sstevel@tonic-gate 	if (dip) {
38807c478bd9Sstevel@tonic-gate 		ddi_remove_intr(dip, 0, NULL);
38817c478bd9Sstevel@tonic-gate 		return (SUCCESS);
38827c478bd9Sstevel@tonic-gate 	}
38837c478bd9Sstevel@tonic-gate 	return (BAD_SOCKET);
38847c478bd9Sstevel@tonic-gate }
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate /*
38887c478bd9Sstevel@tonic-gate  * pcm_pathname()
38897c478bd9Sstevel@tonic-gate  *	make a partial path from dip.
38907c478bd9Sstevel@tonic-gate  *	used to mknods relative to /devices/pcmcia/
38917c478bd9Sstevel@tonic-gate  *
38927c478bd9Sstevel@tonic-gate  * XXX - we now use ddi_get_name_addr to get the "address" portion
38937c478bd9Sstevel@tonic-gate  *	of the name; that way, we only have to modify the name creation
38947c478bd9Sstevel@tonic-gate  *	algorithm in one place
38957c478bd9Sstevel@tonic-gate  */
38967c478bd9Sstevel@tonic-gate static void
38977c478bd9Sstevel@tonic-gate pcm_pathname(dev_info_t *dip, char *name, char *path)
38987c478bd9Sstevel@tonic-gate {
38997c478bd9Sstevel@tonic-gate 	(void) sprintf(path, "%s@%s:%s", ddi_node_name(dip),
39007c478bd9Sstevel@tonic-gate 	    ddi_get_name_addr(dip), name);
39017c478bd9Sstevel@tonic-gate }
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate /*
39047c478bd9Sstevel@tonic-gate  * pcmcia_create_device()
39057c478bd9Sstevel@tonic-gate  *	create the /devices entries for the driver
39067c478bd9Sstevel@tonic-gate  *	it is assumed that the PC Card driver will do a
39077c478bd9Sstevel@tonic-gate  *	RegisterClient for each subdevice.
39087c478bd9Sstevel@tonic-gate  *	The device type string is encoded here to match
39097c478bd9Sstevel@tonic-gate  *	the standardized names when possible.
39107c478bd9Sstevel@tonic-gate  * XXX - note that we may need to provide a way for the
39117c478bd9Sstevel@tonic-gate  *	caller to specify the complete name string that
39127c478bd9Sstevel@tonic-gate  *	we pass to ddi_set_name_addr
39137c478bd9Sstevel@tonic-gate  */
39147c478bd9Sstevel@tonic-gate static int
39157c478bd9Sstevel@tonic-gate pcmcia_create_device(ss_make_device_node_t *init)
39167c478bd9Sstevel@tonic-gate {
39177c478bd9Sstevel@tonic-gate 	int err = SUCCESS;
39187c478bd9Sstevel@tonic-gate 	struct pcm_make_dev device;
39197c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
39207c478bd9Sstevel@tonic-gate 	major_t major;
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 	/*
39237c478bd9Sstevel@tonic-gate 	 * Now that we have the name, create it.
39247c478bd9Sstevel@tonic-gate 	 */
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	bzero(&device, sizeof (device));
39277c478bd9Sstevel@tonic-gate 	if (init->flags & SS_CSINITDEV_CREATE_DEVICE) {
39287c478bd9Sstevel@tonic-gate 		if ((err = ddi_create_minor_node(init->dip,
39297c478bd9Sstevel@tonic-gate 		    init->name,
39307c478bd9Sstevel@tonic-gate 		    init->spec_type,
39317c478bd9Sstevel@tonic-gate 		    init->minor_num,
39327c478bd9Sstevel@tonic-gate 		    init->node_type,
39337c478bd9Sstevel@tonic-gate 		    0)) != DDI_SUCCESS) {
39347c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
39357c478bd9Sstevel@tonic-gate 			if (pcmcia_debug)
39367c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
39377c478bd9Sstevel@tonic-gate 					"pcmcia_create_device: failed "
39387c478bd9Sstevel@tonic-gate 					"create\n");
39397c478bd9Sstevel@tonic-gate #endif
39407c478bd9Sstevel@tonic-gate 			return (BAD_ATTRIBUTE);
39417c478bd9Sstevel@tonic-gate 		}
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(ddi_binding_name(init->dip));
39447c478bd9Sstevel@tonic-gate 		ops = ddi_get_driver(init->dip);
39457c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&devnamesp[major].dn_lock);
39467c478bd9Sstevel@tonic-gate 		INCR_DEV_OPS_REF(ops);
39477c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(init->dip, device.path);
39487c478bd9Sstevel@tonic-gate 		DECR_DEV_OPS_REF(ops);
39497c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
39507c478bd9Sstevel@tonic-gate 		(void) sprintf(device.path + strlen(device.path), ":%s",
39517c478bd9Sstevel@tonic-gate 		    init->name);
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 		(void) strcpy(device.driver, ddi_binding_name(init->dip));
39547c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
39557c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
39567c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
39577c478bd9Sstevel@tonic-gate 				"pcmcia_create_device: created %s "
39587c478bd9Sstevel@tonic-gate 				"from %s [%s]\n",
39597c478bd9Sstevel@tonic-gate 				device.path, init->name, device.driver);
39607c478bd9Sstevel@tonic-gate #endif
39617c478bd9Sstevel@tonic-gate 		device.dev =
39627c478bd9Sstevel@tonic-gate 		    makedevice(ddi_name_to_major(ddi_get_name(init->dip)),
39637c478bd9Sstevel@tonic-gate 		    init->minor_num);
39647c478bd9Sstevel@tonic-gate 		device.flags |= (init->flags & SS_CSINITDEV_MORE_DEVICES) ?
39657c478bd9Sstevel@tonic-gate 		    PCM_EVENT_MORE : 0;
39667c478bd9Sstevel@tonic-gate 		device.type = init->spec_type;
39677c478bd9Sstevel@tonic-gate 		device.op = SS_CSINITDEV_CREATE_DEVICE;
39687c478bd9Sstevel@tonic-gate 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
39697c478bd9Sstevel@tonic-gate 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
39707c478bd9Sstevel@tonic-gate 		    -1);
39717c478bd9Sstevel@tonic-gate 	} else if (init->flags & SS_CSINITDEV_REMOVE_DEVICE) {
39727c478bd9Sstevel@tonic-gate 		device.op = SS_CSINITDEV_REMOVE_DEVICE;
39737c478bd9Sstevel@tonic-gate 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
39747c478bd9Sstevel@tonic-gate 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
39757c478bd9Sstevel@tonic-gate 		    -1);
39767c478bd9Sstevel@tonic-gate 		if (init->name != NULL)
39777c478bd9Sstevel@tonic-gate 			(void) strcpy(device.path, init->name);
39787c478bd9Sstevel@tonic-gate 		device.dev =
39797c478bd9Sstevel@tonic-gate 		    makedevice(ddi_name_to_major(ddi_get_name(init->dip)),
39807c478bd9Sstevel@tonic-gate 		    0);
39817c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(init->dip, init->name);
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	/*
39857c478bd9Sstevel@tonic-gate 	 *	we send an event for ALL devices created.
39867c478bd9Sstevel@tonic-gate 	 *	To do otherwise ties us to using drvconfig
39877c478bd9Sstevel@tonic-gate 	 *	forever.  There are relatively few devices
39887c478bd9Sstevel@tonic-gate 	 *	ever created so no need to do otherwise.
39897c478bd9Sstevel@tonic-gate 	 *	The existence of the event manager must never
39907c478bd9Sstevel@tonic-gate 	 *	be visible to a PCMCIA device driver.
39917c478bd9Sstevel@tonic-gate 	 */
39927c478bd9Sstevel@tonic-gate 	pcm_event_manager(PCE_INIT_DEV, device.socket, &device);
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	return (err);
39957c478bd9Sstevel@tonic-gate }
39967c478bd9Sstevel@tonic-gate 
39977c478bd9Sstevel@tonic-gate /*
39987c478bd9Sstevel@tonic-gate  * pcmcia_get_minors()
39997c478bd9Sstevel@tonic-gate  *	We need to traverse the minor node list of the
40007c478bd9Sstevel@tonic-gate  *	dip if there are any.  This takes two passes;
40017c478bd9Sstevel@tonic-gate  *	one to get the count and buffer size and the
40027c478bd9Sstevel@tonic-gate  *	other to actually copy the data into the buffer.
40037c478bd9Sstevel@tonic-gate  *	The framework requires that the dip be locked
40047c478bd9Sstevel@tonic-gate  *	during this time to avoid breakage as well as the
40057c478bd9Sstevel@tonic-gate  *	driver being locked.
40067c478bd9Sstevel@tonic-gate  */
40077c478bd9Sstevel@tonic-gate int
40087c478bd9Sstevel@tonic-gate pcmcia_get_minors(dev_info_t *dip, struct pcm_make_dev **minors)
40097c478bd9Sstevel@tonic-gate {
40107c478bd9Sstevel@tonic-gate 	int count = 0;
40117c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dp;
40127c478bd9Sstevel@tonic-gate 	struct pcm_make_dev *md;
40137c478bd9Sstevel@tonic-gate 	int socket;
40147c478bd9Sstevel@tonic-gate 	major_t major;
40157c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	socket = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
40187c478bd9Sstevel@tonic-gate 	    PCM_DEV_SOCKET, -1);
40197c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
40207c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_minor != (struct ddi_minor_data *)NULL) {
40217c478bd9Sstevel@tonic-gate 		for (dp = DEVI(dip)->devi_minor;
40227c478bd9Sstevel@tonic-gate 			dp != (struct ddi_minor_data *)NULL;
40237c478bd9Sstevel@tonic-gate 			dp = dp->next) {
40247c478bd9Sstevel@tonic-gate 			count++; /* have one more */
40257c478bd9Sstevel@tonic-gate 		}
40267c478bd9Sstevel@tonic-gate 		/* we now know how many nodes to allocate */
40277c478bd9Sstevel@tonic-gate 		md = kmem_zalloc(count * sizeof (struct pcm_make_dev),
40287c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
40297c478bd9Sstevel@tonic-gate 		if (md != NULL) {
40307c478bd9Sstevel@tonic-gate 			*minors = md;
40317c478bd9Sstevel@tonic-gate 			for (dp = DEVI(dip)->devi_minor;
40327c478bd9Sstevel@tonic-gate 				dp != (struct ddi_minor_data *)NULL;
40337c478bd9Sstevel@tonic-gate 				dp = dp->next, md++) {
40347c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
40357c478bd9Sstevel@tonic-gate 				if (pcmcia_debug > 1) {
40367c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT,
40377c478bd9Sstevel@tonic-gate 						"pcmcia_get_minors: name=%s,"
40387c478bd9Sstevel@tonic-gate 						"socket=%d, stype=%x, "
40397c478bd9Sstevel@tonic-gate 						"ntype=%s, dev_t=%x",
40407c478bd9Sstevel@tonic-gate 						dp->ddm_name,
40417c478bd9Sstevel@tonic-gate 						socket,
40427c478bd9Sstevel@tonic-gate 						dp->ddm_spec_type,
40437c478bd9Sstevel@tonic-gate 						dp->ddm_node_type,
40447c478bd9Sstevel@tonic-gate 						(int)dp->ddm_dev);
40457c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT,
40467c478bd9Sstevel@tonic-gate 						"\tbind name = %s\n",
40477c478bd9Sstevel@tonic-gate 						ddi_binding_name(dip));
40487c478bd9Sstevel@tonic-gate 				}
40497c478bd9Sstevel@tonic-gate #endif
40507c478bd9Sstevel@tonic-gate 				md->socket = socket;
40517c478bd9Sstevel@tonic-gate 				md->op = SS_CSINITDEV_CREATE_DEVICE;
40527c478bd9Sstevel@tonic-gate 				md->dev = dp->ddm_dev;
40537c478bd9Sstevel@tonic-gate 				md->type = dp->ddm_spec_type;
40547c478bd9Sstevel@tonic-gate 				(void) strcpy(md->driver,
40557c478bd9Sstevel@tonic-gate 				    ddi_binding_name(dip));
40567c478bd9Sstevel@tonic-gate 				major = ddi_name_to_major(md->driver);
40577c478bd9Sstevel@tonic-gate 				ops = ddi_get_driver(dip);
40587c478bd9Sstevel@tonic-gate 				LOCK_DEV_OPS(&devnamesp[major].dn_lock);
40597c478bd9Sstevel@tonic-gate 				pcm_pathname(dip, dp->ddm_name, md->path);
40607c478bd9Sstevel@tonic-gate 				INCR_DEV_OPS_REF(ops);
40617c478bd9Sstevel@tonic-gate 				(void) ddi_pathname(dip, md->path);
40627c478bd9Sstevel@tonic-gate 				DECR_DEV_OPS_REF(ops);
40637c478bd9Sstevel@tonic-gate 				UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
40647c478bd9Sstevel@tonic-gate 				(void) sprintf(md->path + strlen(md->path),
40657c478bd9Sstevel@tonic-gate 					":%s", dp->ddm_name);
40667c478bd9Sstevel@tonic-gate 				if (dp->next == NULL)
40677c478bd9Sstevel@tonic-gate 					/* no more */
40687c478bd9Sstevel@tonic-gate 					md->flags |= PCM_EVENT_MORE;
40697c478bd9Sstevel@tonic-gate 			}
40707c478bd9Sstevel@tonic-gate 		} else {
40717c478bd9Sstevel@tonic-gate 			count = 0;
40727c478bd9Sstevel@tonic-gate 		}
40737c478bd9Sstevel@tonic-gate 	}
40747c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
40757c478bd9Sstevel@tonic-gate 	return (count);
40767c478bd9Sstevel@tonic-gate }
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
40797c478bd9Sstevel@tonic-gate static char *ddmtypes[] = { "minor", "alias", "default", "internal" };
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate static void
40827c478bd9Sstevel@tonic-gate pcmcia_dump_minors(dev_info_t *dip)
40837c478bd9Sstevel@tonic-gate {
40847c478bd9Sstevel@tonic-gate 	int count = 0;
40857c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dp;
40867c478bd9Sstevel@tonic-gate 	int unit, major;
40877c478bd9Sstevel@tonic-gate 	dev_info_t *np;
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	unit = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
40907c478bd9Sstevel@tonic-gate 	    PCM_DEV_SOCKET, -1);
40917c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT,
40927c478bd9Sstevel@tonic-gate 		"pcmcia_dump_minors: dip=%p, socket=%d\n", (void *)dip, unit);
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
40957c478bd9Sstevel@tonic-gate 	if (major != -1) {
40967c478bd9Sstevel@tonic-gate 		for (np = devnamesp[major].dn_head; np != NULL;
40977c478bd9Sstevel@tonic-gate 		    np = (dev_info_t *)DEVI(np)->devi_next) {
40987c478bd9Sstevel@tonic-gate 			char *cf2 = "";
40997c478bd9Sstevel@tonic-gate 			char *cur = "";
41007c478bd9Sstevel@tonic-gate 			if (i_ddi_node_state(np) == DS_READY)
41017c478bd9Sstevel@tonic-gate 				cf2 = "DS_READY";
41027c478bd9Sstevel@tonic-gate 			if (np == dip)
41037c478bd9Sstevel@tonic-gate 				cur = "CUR";
41047c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\tsibs: %s %s %s\n",
41057c478bd9Sstevel@tonic-gate 				ddi_binding_name(np), cf2, cur);
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 			mutex_enter(&(DEVI(np)->devi_lock));
41087c478bd9Sstevel@tonic-gate 			if (DEVI(np)->devi_minor !=
41097c478bd9Sstevel@tonic-gate 			    (struct ddi_minor_data *)NULL) {
41107c478bd9Sstevel@tonic-gate 				for (dp = DEVI(np)->devi_minor;
41117c478bd9Sstevel@tonic-gate 				    dp != (struct ddi_minor_data *)NULL;
41127c478bd9Sstevel@tonic-gate 				    dp = dp->next) {
41137c478bd9Sstevel@tonic-gate 					count++; /* have one more */
41147c478bd9Sstevel@tonic-gate 				}
41157c478bd9Sstevel@tonic-gate 				for (dp = DEVI(dip)->devi_minor;
41167c478bd9Sstevel@tonic-gate 				    dp != (struct ddi_minor_data *)NULL;
41177c478bd9Sstevel@tonic-gate 				    dp = dp->next) {
41187c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, "\ttype=%s, name=%s,"
41197c478bd9Sstevel@tonic-gate 						"socket=%d, stype=%x, "
41207c478bd9Sstevel@tonic-gate 						"ntype=%s, dev_t=%x",
41217c478bd9Sstevel@tonic-gate 						ddmtypes[dp->type],
41227c478bd9Sstevel@tonic-gate 						dp->ddm_name,
41237c478bd9Sstevel@tonic-gate 						unit,
41247c478bd9Sstevel@tonic-gate 						dp->ddm_spec_type,
41257c478bd9Sstevel@tonic-gate 						dp->ddm_node_type,
41267c478bd9Sstevel@tonic-gate 						(int)dp->ddm_dev);
41277c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, "\tbind name = %s\n",
41287c478bd9Sstevel@tonic-gate 						ddi_binding_name(np));
41297c478bd9Sstevel@tonic-gate 				}
41307c478bd9Sstevel@tonic-gate 			}
41317c478bd9Sstevel@tonic-gate 			mutex_exit(&(DEVI(np)->devi_lock));
41327c478bd9Sstevel@tonic-gate 		}
41337c478bd9Sstevel@tonic-gate 	}
41347c478bd9Sstevel@tonic-gate }
41357c478bd9Sstevel@tonic-gate #endif
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate /*
41387c478bd9Sstevel@tonic-gate  * experimental merging code
41397c478bd9Sstevel@tonic-gate  * what are the things that we should merge on?
41407c478bd9Sstevel@tonic-gate  *	match something by name in the "compatible" property
41417c478bd9Sstevel@tonic-gate  *	restrict to a specific "socket"
41427c478bd9Sstevel@tonic-gate  *	restrict to a specific "instance"
41437c478bd9Sstevel@tonic-gate  */
41447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
41457c478bd9Sstevel@tonic-gate static int
41467c478bd9Sstevel@tonic-gate pcmcia_merge_conf(dev_info_t *dip)
41477c478bd9Sstevel@tonic-gate {
41487c478bd9Sstevel@tonic-gate 	return (0);		/* merge failed */
41497c478bd9Sstevel@tonic-gate }
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate /*
41527c478bd9Sstevel@tonic-gate  * pcmcia_mfc_intr()
41537c478bd9Sstevel@tonic-gate  *	Multifunction Card interrupt handler
41547c478bd9Sstevel@tonic-gate  *	While some adapters share interrupts at the lowest
41557c478bd9Sstevel@tonic-gate  *	level, some can't.  In order to be consistent, we
41567c478bd9Sstevel@tonic-gate  *	split multifunction cards out with this intercept and
41577c478bd9Sstevel@tonic-gate  *	allow the low level to do what is best for it.
41587c478bd9Sstevel@tonic-gate  *	the arg is a pcmcia_socket structure and all interrupts
41597c478bd9Sstevel@tonic-gate  *	are per-socket in this case.  We also have the option
41607c478bd9Sstevel@tonic-gate  *	to optimize if the cards support it.  It also means
41617c478bd9Sstevel@tonic-gate  *	that we can use the INTRACK mode if it proves desirable
41627c478bd9Sstevel@tonic-gate  */
41637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
41647c478bd9Sstevel@tonic-gate static uint32_t
41657c478bd9Sstevel@tonic-gate pcmcia_mfc_intr(caddr_t arg1, caddr_t arg2)
41667c478bd9Sstevel@tonic-gate {
41677c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
41687c478bd9Sstevel@tonic-gate 	inthandler_t *intr, *first;
41697c478bd9Sstevel@tonic-gate 	int done, result;
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 	sockp = (pcmcia_logical_socket_t *)arg1;
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
41747c478bd9Sstevel@tonic-gate 	if (pcmcia_debug > 1) {
41757c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_mfc_intr sockp=%p"
41767c478bd9Sstevel@tonic-gate 		    " ls_inthandlers=%p\n"
41777c478bd9Sstevel@tonic-gate 		    "\t ls_flags=0x%x PCS_IRQ_ENABLED=0x%x \n",
41787c478bd9Sstevel@tonic-gate 		    (void *) sockp, (void *) sockp->ls_inthandlers,
41797c478bd9Sstevel@tonic-gate 		    sockp->ls_flags, PCS_IRQ_ENABLED);
41807c478bd9Sstevel@tonic-gate 	}
41817c478bd9Sstevel@tonic-gate #endif
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	if (sockp == NULL || sockp->ls_inthandlers == NULL ||
41847c478bd9Sstevel@tonic-gate 	    !(sockp->ls_flags & PCS_IRQ_ENABLED))
41857c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 	mutex_enter(&sockp->ls_ilock);
41887c478bd9Sstevel@tonic-gate 	for (done = 0, result = 0, first = intr = sockp->ls_inthandlers;
41897c478bd9Sstevel@tonic-gate 		intr != NULL && !done; intr = intr->next) {
41907c478bd9Sstevel@tonic-gate 		result |= intr->intr(intr->arg1, intr->arg2);
41917c478bd9Sstevel@tonic-gate 		if (intr->next == first)
41927c478bd9Sstevel@tonic-gate 			done++;
41937c478bd9Sstevel@tonic-gate 	}
41947c478bd9Sstevel@tonic-gate 	if (intr == NULL) {
41957c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcmcia_mfc_intr: bad MFC handler list");
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 	if (sockp->ls_inthandlers)
41987c478bd9Sstevel@tonic-gate 		sockp->ls_inthandlers = sockp->ls_inthandlers->next;
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate 	mutex_exit(&sockp->ls_ilock);
42017c478bd9Sstevel@tonic-gate 	return (result ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
42027c478bd9Sstevel@tonic-gate }
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate /*
42057c478bd9Sstevel@tonic-gate  * pcmcia_power(dip)
42067c478bd9Sstevel@tonic-gate  *	control power for nexus and children
42077c478bd9Sstevel@tonic-gate  */
42087c478bd9Sstevel@tonic-gate int
42097c478bd9Sstevel@tonic-gate pcmcia_power(dev_info_t *dip, int component, int level)
42107c478bd9Sstevel@tonic-gate {
42117c478bd9Sstevel@tonic-gate #if 0
42127c478bd9Sstevel@tonic-gate 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
42137c478bd9Sstevel@tonic-gate 	int i;
42147c478bd9Sstevel@tonic-gate 	/*
42157c478bd9Sstevel@tonic-gate 	 * for now, we only have one component.  Should there be one per-socket?
42167c478bd9Sstevel@tonic-gate 	 * the level is only one (power on or off)
42177c478bd9Sstevel@tonic-gate 	 */
42187c478bd9Sstevel@tonic-gate 	if (component != 0 || level > 1)
42197c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcic->pc_numsockets; i++) {
42227c478bd9Sstevel@tonic-gate 		if (pcic->pc_callback)
42237c478bd9Sstevel@tonic-gate 			PC_CALLBACK(dip, pcic->pc_cb_arg,
42247c478bd9Sstevel@tonic-gate 			    (level == 0) ? PCE_PM_SUSPEND :
42257c478bd9Sstevel@tonic-gate 			    PCE_PM_RESUME,
42267c478bd9Sstevel@tonic-gate 			    i);
42277c478bd9Sstevel@tonic-gate 	}
42287c478bd9Sstevel@tonic-gate #else
42297c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "pcmcia_power: component=%d, level=%d for %s",
42307c478bd9Sstevel@tonic-gate 		component, level, ddi_get_name_addr(dip));
42317c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
42327c478bd9Sstevel@tonic-gate #endif
42337c478bd9Sstevel@tonic-gate }
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate void
42367c478bd9Sstevel@tonic-gate pcmcia_begin_resume(dev_info_t *dip)
42377c478bd9Sstevel@tonic-gate {
42387c478bd9Sstevel@tonic-gate 	int i;
42397c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter *adapt = NULL;
42407c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_adapters; i++) {
42417c478bd9Sstevel@tonic-gate 		if (pcmcia_adapters[i]->pca_dip == dip) {
42427c478bd9Sstevel@tonic-gate 			adapt = pcmcia_adapters[i];
42437c478bd9Sstevel@tonic-gate 			break;
42447c478bd9Sstevel@tonic-gate 		}
42457c478bd9Sstevel@tonic-gate 	}
42467c478bd9Sstevel@tonic-gate 	if (adapt == NULL)
42477c478bd9Sstevel@tonic-gate 		return;
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	for (i = 0; i < adapt->pca_numsockets; i++) {
42507c478bd9Sstevel@tonic-gate 		int s;
42517c478bd9Sstevel@tonic-gate 		s = adapt->pca_first_socket + i;
42527c478bd9Sstevel@tonic-gate 		if (pcmcia_sockets[s]->ls_flags & PCS_SUSPENDED) {
42537c478bd9Sstevel@tonic-gate 			if (pcmcia_sockets[s]->ls_flags &
42547c478bd9Sstevel@tonic-gate 			    (1 << PCE_PM_RESUME)) {
42557c478bd9Sstevel@tonic-gate 				(void) cs_event(PCE_PM_RESUME, s, 0);
42567c478bd9Sstevel@tonic-gate 				pcm_event_manager(PCE_PM_RESUME, s, NULL);
42577c478bd9Sstevel@tonic-gate 			}
42587c478bd9Sstevel@tonic-gate 			(void) cs_event(PCE_CARD_REMOVAL, s, 0);
42597c478bd9Sstevel@tonic-gate 			pcm_event_manager(PCE_CARD_REMOVAL, s, NULL);
42607c478bd9Sstevel@tonic-gate 		}
42617c478bd9Sstevel@tonic-gate 	}
42627c478bd9Sstevel@tonic-gate }
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate void
42657c478bd9Sstevel@tonic-gate pcmcia_wait_insert(dev_info_t *dip)
42667c478bd9Sstevel@tonic-gate {
42677c478bd9Sstevel@tonic-gate 	int i, f, tries, done;
42687c478bd9Sstevel@tonic-gate 	clock_t tm;
42697c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter *adapt = NULL;
42707c478bd9Sstevel@tonic-gate 	anp_t *nexus;
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_adapters; i++) {
42737c478bd9Sstevel@tonic-gate 		if (pcmcia_adapters[i]->pca_dip == dip) {
42747c478bd9Sstevel@tonic-gate 			adapt = pcmcia_adapters[i];
42757c478bd9Sstevel@tonic-gate 			break;
42767c478bd9Sstevel@tonic-gate 		}
42777c478bd9Sstevel@tonic-gate 	}
42787c478bd9Sstevel@tonic-gate 	if (adapt == NULL)
42797c478bd9Sstevel@tonic-gate 		return;
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 	for (tries = adapt->pca_numsockets * 10; tries > 0; tries--) {
42827c478bd9Sstevel@tonic-gate 		done = 1;
42837c478bd9Sstevel@tonic-gate 		mutex_enter(&pcmcia_global_lock);
42847c478bd9Sstevel@tonic-gate 		for (i = 0; i < adapt->pca_numsockets; i++) {
42857c478bd9Sstevel@tonic-gate 			int s;
42867c478bd9Sstevel@tonic-gate 			s = adapt->pca_first_socket + i;
42877c478bd9Sstevel@tonic-gate 			for (f = 0; f < PCMCIA_MAX_FUNCTIONS; f++)
42887c478bd9Sstevel@tonic-gate 				if (pcmcia_sockets[s] &&
42897c478bd9Sstevel@tonic-gate 				    pcmcia_sockets[s]->ls_flags &
42907c478bd9Sstevel@tonic-gate 				    PCS_SUSPENDED) {
42917c478bd9Sstevel@tonic-gate 					done = 0;
42927c478bd9Sstevel@tonic-gate 					break;
42937c478bd9Sstevel@tonic-gate 				}
42947c478bd9Sstevel@tonic-gate 		}
42957c478bd9Sstevel@tonic-gate 		if (!done) {
42967c478bd9Sstevel@tonic-gate 			tm = ddi_get_lbolt();
42977c478bd9Sstevel@tonic-gate 			(void) cv_timedwait(&pcmcia_condvar,
42987c478bd9Sstevel@tonic-gate 			    &pcmcia_global_lock,
42997c478bd9Sstevel@tonic-gate 			    tm + drv_usectohz(100000));
43007c478bd9Sstevel@tonic-gate 		} else {
43017c478bd9Sstevel@tonic-gate 			tries = 0;
43027c478bd9Sstevel@tonic-gate 		}
43037c478bd9Sstevel@tonic-gate 		mutex_exit(&pcmcia_global_lock);
43047c478bd9Sstevel@tonic-gate 	}
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate 	nexus = (anp_t *)ddi_get_driver_private(dip);
43077c478bd9Sstevel@tonic-gate 	pcmcia_find_cards(nexus);
43087c478bd9Sstevel@tonic-gate }
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate int
43117c478bd9Sstevel@tonic-gate pcmcia_map_reg(dev_info_t *pdip, dev_info_t *dip, ra_return_t *ra,
43127c478bd9Sstevel@tonic-gate 		uint32_t state, caddr_t *base,
43137c478bd9Sstevel@tonic-gate 		ddi_acc_handle_t *handle, ddi_device_acc_attr_t *attrib,
43147c478bd9Sstevel@tonic-gate 		uint32_t req_base)
43157c478bd9Sstevel@tonic-gate {
43167c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
43177c478bd9Sstevel@tonic-gate 	int rnum = 0, type = PCMCIA_MAP_MEM;
43187c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
43197c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
43207c478bd9Sstevel@tonic-gate 	int result;
43217c478bd9Sstevel@tonic-gate 	struct regspec *reg;
43227c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	if (dip != NULL) {
43257c478bd9Sstevel@tonic-gate 		ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
43267c478bd9Sstevel@tonic-gate 		if (ppd == NULL)
43277c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
43287c478bd9Sstevel@tonic-gate 		for (rnum = 1; rnum < ppd->ppd_nreg; rnum++) {
43297c478bd9Sstevel@tonic-gate 			struct pcm_regs *p;
43307c478bd9Sstevel@tonic-gate 			p = &ppd->ppd_reg[rnum];
43317c478bd9Sstevel@tonic-gate 			if (state & WS_IO) {
43327c478bd9Sstevel@tonic-gate 				/* need I/O */
43337c478bd9Sstevel@tonic-gate 				type = PCMCIA_MAP_IO;
43347c478bd9Sstevel@tonic-gate 				/*
43357c478bd9Sstevel@tonic-gate 				 * We want to find an IO regspec. When we
43367c478bd9Sstevel@tonic-gate 				 *	find one, it either has to match
43377c478bd9Sstevel@tonic-gate 				 *	the caller's requested base address
43387c478bd9Sstevel@tonic-gate 				 *	or it has to be relocatable.
43397c478bd9Sstevel@tonic-gate 				 * We match on the requested base address
43407c478bd9Sstevel@tonic-gate 				 *	rather than the allocated base
43417c478bd9Sstevel@tonic-gate 				 *	address so that we handle the case
43427c478bd9Sstevel@tonic-gate 				 *	of adapters that have IO window base
43437c478bd9Sstevel@tonic-gate 				 *	relocation registers.
43447c478bd9Sstevel@tonic-gate 				 */
43457c478bd9Sstevel@tonic-gate 				if ((p->phys_hi &
43467c478bd9Sstevel@tonic-gate 				    PC_REG_SPACE(PC_REG_SPACE_IO)) &&
43477c478bd9Sstevel@tonic-gate 					((req_base == p->phys_lo) ||
43487c478bd9Sstevel@tonic-gate 					!(p->phys_hi & PC_REG_RELOC(1))))
43497c478bd9Sstevel@tonic-gate 				    break;
43507c478bd9Sstevel@tonic-gate 			} else {
43517c478bd9Sstevel@tonic-gate 				/* need memory */
43527c478bd9Sstevel@tonic-gate 				type = PCMCIA_MAP_MEM;
43537c478bd9Sstevel@tonic-gate 				if (p->phys_hi &
43547c478bd9Sstevel@tonic-gate 				    PC_REG_SPACE(PC_REG_SPACE_MEMORY|
43557c478bd9Sstevel@tonic-gate 				    PC_REG_SPACE_ATTRIBUTE))
43567c478bd9Sstevel@tonic-gate 					break;
43577c478bd9Sstevel@tonic-gate 			}
43587c478bd9Sstevel@tonic-gate 		}
43597c478bd9Sstevel@tonic-gate 		if (rnum >= ppd->ppd_nreg)
43607c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
43617c478bd9Sstevel@tonic-gate 	} else if (state & WS_IO) {
43627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
43637c478bd9Sstevel@tonic-gate 	}
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	reg = kmem_zalloc(sizeof (pci_regspec_t), KM_SLEEP);
43667c478bd9Sstevel@tonic-gate 	reg = pcmcia_cons_regspec(pdip, type, (uchar_t *)reg, ra);
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 	if (attrib == NULL ||
43697c478bd9Sstevel@tonic-gate 	    attrib->devacc_attr_version != DDI_DEVICE_ATTR_V0) {
43707c478bd9Sstevel@tonic-gate 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
43717c478bd9Sstevel@tonic-gate 		attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
43727c478bd9Sstevel@tonic-gate 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
43737c478bd9Sstevel@tonic-gate 	} else {
43747c478bd9Sstevel@tonic-gate 		attr = *attrib;
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 	/*
43777c478bd9Sstevel@tonic-gate 	 * Allocate and initialize the common elements of data access handle.
43787c478bd9Sstevel@tonic-gate 	 */
43797c478bd9Sstevel@tonic-gate 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
43807c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handle);
43817c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
43827c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip != NULL ? dip : pdip;
43837c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = rnum;
43847c478bd9Sstevel@tonic-gate 	hp->ah_offset = 0;
43857c478bd9Sstevel@tonic-gate 	hp->ah_len = ra->ra_len;
43867c478bd9Sstevel@tonic-gate 	hp->ah_acc = attr;
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate 	/*
43897c478bd9Sstevel@tonic-gate 	 * Set up the mapping request and call to parent.
43907c478bd9Sstevel@tonic-gate 	 */
43917c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
43927c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_REGSPEC;
43937c478bd9Sstevel@tonic-gate 	mr.map_obj.rp = reg;
43947c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
43957c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
43967c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
43977c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 	result = ddi_map(pdip, &mr, 0, ra->ra_len, base);
44007c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
44017c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handle);
44027c478bd9Sstevel@tonic-gate 		*handle = (ddi_acc_handle_t)NULL;
44037c478bd9Sstevel@tonic-gate 		kmem_free(reg, sizeof (pci_regspec_t));
44047c478bd9Sstevel@tonic-gate 	} else {
44057c478bd9Sstevel@tonic-gate 		hp->ah_addr = *base;
44067c478bd9Sstevel@tonic-gate 		if (mr.map_op == DDI_MO_UNMAP)
44077c478bd9Sstevel@tonic-gate 			ra = NULL;
44087c478bd9Sstevel@tonic-gate 		if (dip != NULL)
44097c478bd9Sstevel@tonic-gate 			pcmcia_set_assigned(dip, rnum, ra);
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	return (result);
44137c478bd9Sstevel@tonic-gate }
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate struct pcmcia_adapter *
44167c478bd9Sstevel@tonic-gate pcmcia_get_adapter(dev_info_t *dip)
44177c478bd9Sstevel@tonic-gate {
44187c478bd9Sstevel@tonic-gate 	int i;
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcmcia_num_adapters; i++) {
44217c478bd9Sstevel@tonic-gate 		if (pcmcia_adapters[i] &&
44227c478bd9Sstevel@tonic-gate 		    pcmcia_adapters[i]->pca_dip == dip) {
44237c478bd9Sstevel@tonic-gate 			return (pcmcia_adapters[i]);
44247c478bd9Sstevel@tonic-gate 		}
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 	return (NULL);
44277c478bd9Sstevel@tonic-gate }
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate void
44317c478bd9Sstevel@tonic-gate pcmcia_set_assigned(dev_info_t *dip, int rnum, ra_return_t *ret)
44327c478bd9Sstevel@tonic-gate {
44337c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
44347c478bd9Sstevel@tonic-gate 	struct pcm_regs *reg, *assign;
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
44377c478bd9Sstevel@tonic-gate 	if (ppd) {
44387c478bd9Sstevel@tonic-gate 		reg = &ppd->ppd_reg[rnum];
44397c478bd9Sstevel@tonic-gate 		assign = &ppd->ppd_assigned[rnum];
44407c478bd9Sstevel@tonic-gate 		if (ret) {
44417c478bd9Sstevel@tonic-gate 			if (assign->phys_hi == 0) {
44427c478bd9Sstevel@tonic-gate 				assign->phys_hi = reg->phys_hi;
44437c478bd9Sstevel@tonic-gate 				assign->phys_lo = ret->ra_addr_lo;
44447c478bd9Sstevel@tonic-gate 				assign->phys_len = ret->ra_len;
44457c478bd9Sstevel@tonic-gate 			} else if (assign->phys_lo != ret->ra_addr_lo) {
44467c478bd9Sstevel@tonic-gate #ifdef PCMCIA_DEBUG
44477c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "pcmcia: bad address:"
44487c478bd9Sstevel@tonic-gate 					"%s=<%x,%x>",
44497c478bd9Sstevel@tonic-gate 					ddi_get_name_addr(dip),
44507c478bd9Sstevel@tonic-gate 					ret->ra_addr_lo, assign->phys_lo);
44517c478bd9Sstevel@tonic-gate #else
44527c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "!pcmcia: bad address:"
44537c478bd9Sstevel@tonic-gate 					"%s=<%x,%x>",
44547c478bd9Sstevel@tonic-gate 					ddi_get_name_addr(dip),
44557c478bd9Sstevel@tonic-gate 					ret->ra_addr_lo, (int)assign->phys_lo);
44567c478bd9Sstevel@tonic-gate #endif
44577c478bd9Sstevel@tonic-gate 			}
44587c478bd9Sstevel@tonic-gate 			assign->phys_hi = PC_INCR_REFCNT(assign->phys_hi);
44597c478bd9Sstevel@tonic-gate 		} else {
44607c478bd9Sstevel@tonic-gate 			int i;
44617c478bd9Sstevel@tonic-gate 			assign->phys_hi = PC_DECR_REFCNT(assign->phys_hi);
44627c478bd9Sstevel@tonic-gate 			i = PC_GET_REG_REFCNT(assign->phys_hi);
44637c478bd9Sstevel@tonic-gate 			if (i == 0) {
44647c478bd9Sstevel@tonic-gate 				assign->phys_hi = 0;
44657c478bd9Sstevel@tonic-gate 				assign->phys_lo = 0;
44667c478bd9Sstevel@tonic-gate 				assign->phys_len = 0;
44677c478bd9Sstevel@tonic-gate 			}
44687c478bd9Sstevel@tonic-gate 		}
44697c478bd9Sstevel@tonic-gate 	}
44707c478bd9Sstevel@tonic-gate }
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate int
44737c478bd9Sstevel@tonic-gate pcmcia_alloc_mem(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret)
44747c478bd9Sstevel@tonic-gate {
44757c478bd9Sstevel@tonic-gate 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_MEM));
44767c478bd9Sstevel@tonic-gate }
44777c478bd9Sstevel@tonic-gate 
44787c478bd9Sstevel@tonic-gate int
44797c478bd9Sstevel@tonic-gate pcmcia_alloc_io(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret)
44807c478bd9Sstevel@tonic-gate {
44817c478bd9Sstevel@tonic-gate 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_IO));
44827c478bd9Sstevel@tonic-gate }
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate int
44857c478bd9Sstevel@tonic-gate pcmcia_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
44867c478bd9Sstevel@tonic-gate 		char *type)
44877c478bd9Sstevel@tonic-gate {
44887c478bd9Sstevel@tonic-gate 	int rval;
44897c478bd9Sstevel@tonic-gate 	uint64_t base = 0;
44907c478bd9Sstevel@tonic-gate 	uint64_t len = 0;
44917c478bd9Sstevel@tonic-gate 
44927c478bd9Sstevel@tonic-gate 	/*
44937c478bd9Sstevel@tonic-gate 	 * Allocate space from busra resource list
44947c478bd9Sstevel@tonic-gate 	 * should not return an address > 32 bits
44957c478bd9Sstevel@tonic-gate 	 */
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 	if ((ndi_ra_alloc(dip, req, &base, &len, type, NDI_RA_PASS)
44987c478bd9Sstevel@tonic-gate 							== NDI_FAILURE) ||
44997c478bd9Sstevel@tonic-gate 	    ((base >> 32) != 0)) {
45007c478bd9Sstevel@tonic-gate 		ret->ra_addr_lo = 0;
45017c478bd9Sstevel@tonic-gate 		ret->ra_len = 0;
45027c478bd9Sstevel@tonic-gate 		rval = DDI_FAILURE;
45037c478bd9Sstevel@tonic-gate 	} else {
45047c478bd9Sstevel@tonic-gate 		ret->ra_addr_lo =  base & 0xffffffff;
45057c478bd9Sstevel@tonic-gate 		ret->ra_len = len;
45067c478bd9Sstevel@tonic-gate 		rval = DDI_SUCCESS;
45077c478bd9Sstevel@tonic-gate 	}
45087c478bd9Sstevel@tonic-gate 	ret->ra_addr_hi = 0;
45097c478bd9Sstevel@tonic-gate 	return (rval);
45107c478bd9Sstevel@tonic-gate }
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate int
45137c478bd9Sstevel@tonic-gate pcmcia_free_mem(dev_info_t *dip, ra_return_t *ret)
45147c478bd9Sstevel@tonic-gate {
45157c478bd9Sstevel@tonic-gate 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_MEM));
45167c478bd9Sstevel@tonic-gate }
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate int
45197c478bd9Sstevel@tonic-gate pcmcia_free_io(dev_info_t *dip, ra_return_t *ret)
45207c478bd9Sstevel@tonic-gate {
45217c478bd9Sstevel@tonic-gate 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_IO));
45227c478bd9Sstevel@tonic-gate }
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate int
45257c478bd9Sstevel@tonic-gate pcmcia_ra_free(dev_info_t *dip, ra_return_t *ret, char *type)
45267c478bd9Sstevel@tonic-gate {
45277c478bd9Sstevel@tonic-gate 	if (ndi_ra_free(dip, (uint64_t)ret->ra_addr_lo, (uint64_t)ret->ra_len,
45287c478bd9Sstevel@tonic-gate 	    type, NDI_RA_PASS) == NDI_SUCCESS) {
45297c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
45307c478bd9Sstevel@tonic-gate 	} else {
45317c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate }
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate /*
45377c478bd9Sstevel@tonic-gate  * when the low level device configuration does resource assignment
45387c478bd9Sstevel@tonic-gate  * (devconf) then free the allocated resources so we can reassign them
45397c478bd9Sstevel@tonic-gate  * later.  Walk the child list to get them.
45407c478bd9Sstevel@tonic-gate  */
45417c478bd9Sstevel@tonic-gate void
45427c478bd9Sstevel@tonic-gate pcmcia_free_resources(dev_info_t *self)
45437c478bd9Sstevel@tonic-gate {
45447c478bd9Sstevel@tonic-gate 	struct regspec *assigned;
45457c478bd9Sstevel@tonic-gate 	int len;
45467c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
45477c478bd9Sstevel@tonic-gate 	int circular;
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	ndi_devi_enter(self, &circular);
45507c478bd9Sstevel@tonic-gate 	/* do searches in compatible property order */
45517c478bd9Sstevel@tonic-gate 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
45527c478bd9Sstevel@tonic-gate 	    dip != NULL;
45537c478bd9Sstevel@tonic-gate 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
45547c478bd9Sstevel@tonic-gate 		len = 0;
4555a3282898Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
45567c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP,
45577c478bd9Sstevel@tonic-gate 		    "assigned-addresses",
45587c478bd9Sstevel@tonic-gate 		    (caddr_t)&assigned,
45597c478bd9Sstevel@tonic-gate 		    &len) == DDI_PROP_SUCCESS) {
45607c478bd9Sstevel@tonic-gate 			/*
45617c478bd9Sstevel@tonic-gate 			 * if there are assigned resources at this point,
45627c478bd9Sstevel@tonic-gate 			 * then the OBP or devconf have assigned them and
45637c478bd9Sstevel@tonic-gate 			 * they need to be freed.
45647c478bd9Sstevel@tonic-gate 			 */
45657c478bd9Sstevel@tonic-gate 			kmem_free(assigned, len);
45667c478bd9Sstevel@tonic-gate 		}
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 	ndi_devi_exit(self, circular);
45697c478bd9Sstevel@tonic-gate }
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate /*
45727c478bd9Sstevel@tonic-gate  * this is the equivalent of pcm_get_intr using ra_allocs.
45737c478bd9Sstevel@tonic-gate  * returns -1 if failed, otherwise returns the allocated irq.
45747c478bd9Sstevel@tonic-gate  * The input request, if less than zero it means not a specific
45757c478bd9Sstevel@tonic-gate  * irq requested. If larger then 0 then we are requesting that specific
45767c478bd9Sstevel@tonic-gate  * irq
45777c478bd9Sstevel@tonic-gate  */
45787c478bd9Sstevel@tonic-gate int
45797c478bd9Sstevel@tonic-gate pcmcia_get_intr(dev_info_t *dip, int request)
45807c478bd9Sstevel@tonic-gate {
45817c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
45827c478bd9Sstevel@tonic-gate 	uint64_t base;
45837c478bd9Sstevel@tonic-gate 	uint64_t len;
45847c478bd9Sstevel@tonic-gate 	int err;
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	bzero(&req, sizeof (req));
45877c478bd9Sstevel@tonic-gate 	base = 0;
45887c478bd9Sstevel@tonic-gate 	len = 1;
45897c478bd9Sstevel@tonic-gate 	if (request >= 0) {
45907c478bd9Sstevel@tonic-gate 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
45917c478bd9Sstevel@tonic-gate 		req.ra_len = 1;
45927c478bd9Sstevel@tonic-gate 		req.ra_addr = (uint64_t)request;
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	req.ra_boundbase = 0;
45967c478bd9Sstevel@tonic-gate 	req.ra_boundlen = 0xffffffffUL;
45977c478bd9Sstevel@tonic-gate 	req.ra_flags |= NDI_RA_ALLOC_BOUNDED;
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	err = ndi_ra_alloc(dip, &req, &base, &len, NDI_RA_TYPE_INTR,
46007c478bd9Sstevel@tonic-gate 	    NDI_RA_PASS);
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	if (err == NDI_FAILURE) {
46037c478bd9Sstevel@tonic-gate 		return (-1);
46047c478bd9Sstevel@tonic-gate 	} else {
46057c478bd9Sstevel@tonic-gate 		return ((int)base);
46067c478bd9Sstevel@tonic-gate 	}
46077c478bd9Sstevel@tonic-gate }
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate int
46117c478bd9Sstevel@tonic-gate pcmcia_return_intr(dev_info_t *dip, int request)
46127c478bd9Sstevel@tonic-gate {
46137c478bd9Sstevel@tonic-gate 	if ((ndi_ra_free(dip, (uint64_t)request, 1, NDI_RA_TYPE_INTR,
46147c478bd9Sstevel@tonic-gate 	    NDI_RA_PASS)) == NDI_SUCCESS) {
46157c478bd9Sstevel@tonic-gate 		return (0);
46167c478bd9Sstevel@tonic-gate 	} else
46177c478bd9Sstevel@tonic-gate 		return (-1);
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate }
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate #ifdef sparc
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate int
46247c478bd9Sstevel@tonic-gate pcmcia_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
46257c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
46267c478bd9Sstevel@tonic-gate {
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
46297c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
46307c478bd9Sstevel@tonic-gate 	int socket, ret;
46317c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter *adapt;
46327c478bd9Sstevel@tonic-gate 	set_irq_handler_t handler;
46337c478bd9Sstevel@tonic-gate 	struct intrspec *pispec;
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
46367c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
46377c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
46387c478bd9Sstevel@tonic-gate 		    "pcmcia_add_intr_impl() entered "
46397c478bd9Sstevel@tonic-gate 		    "dip=%p rdip=%p hdlp=%p \n",
46407c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
46417c478bd9Sstevel@tonic-gate 	}
46427c478bd9Sstevel@tonic-gate #endif
46437c478bd9Sstevel@tonic-gate 
46447c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
46457c478bd9Sstevel@tonic-gate 	socket = ppd->ppd_socket;
46467c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
46477c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
46487c478bd9Sstevel@tonic-gate 
46497c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
46507c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
46517c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
46527c478bd9Sstevel@tonic-gate 		    " ppd_flags=0X%x PPD_CARD_MULTI=0X%x\n"
46537c478bd9Sstevel@tonic-gate 		    " ppd_intrspec=%p ls_inthandlers=%p\n",
46547c478bd9Sstevel@tonic-gate 		    ppd->ppd_flags, PPD_CARD_MULTI,
46557c478bd9Sstevel@tonic-gate 		    (void *) ppd->ppd_intrspec,
46567c478bd9Sstevel@tonic-gate 		    (void *)sockp->ls_inthandlers);
46577c478bd9Sstevel@tonic-gate 	}
46587c478bd9Sstevel@tonic-gate #endif
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	/*
46617c478bd9Sstevel@tonic-gate 	 * calculate IPL level when we support multiple levels
46627c478bd9Sstevel@tonic-gate 	 */
46637c478bd9Sstevel@tonic-gate 	pispec = ppd->ppd_intrspec;
46647c478bd9Sstevel@tonic-gate 	if (pispec == NULL) {
46657c478bd9Sstevel@tonic-gate 		sockp->ls_error = BAD_IRQ;
46667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
46677c478bd9Sstevel@tonic-gate 	}
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	handler.socket = sockp->ls_socket;
46707c478bd9Sstevel@tonic-gate 	handler.irq = 0;	/* default case */
46717c478bd9Sstevel@tonic-gate 	handler.handler = (f_tt *)hdlp->ih_cb_func;
46727c478bd9Sstevel@tonic-gate 	handler.arg1 = hdlp->ih_cb_arg1;
46737c478bd9Sstevel@tonic-gate 	handler.arg2 = hdlp->ih_cb_arg2;
4674360e6f5eSmathue 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate 	/*
46777c478bd9Sstevel@tonic-gate 	 * check if multifunction and do the right thing
46787c478bd9Sstevel@tonic-gate 	 * we put an intercept in between the mfc handler and
46797c478bd9Sstevel@tonic-gate 	 * us so we can catch and process.  We might be able
46807c478bd9Sstevel@tonic-gate 	 * to optimize this depending on the card features
46817c478bd9Sstevel@tonic-gate 	 * (a future option).
46827c478bd9Sstevel@tonic-gate 	 */
46837c478bd9Sstevel@tonic-gate 	if (ppd->ppd_flags & PPD_CARD_MULTI) {
46847c478bd9Sstevel@tonic-gate 		inthandler_t *intr;
46857c478bd9Sstevel@tonic-gate 		/*
46867c478bd9Sstevel@tonic-gate 		 * note that the first function is a special
46877c478bd9Sstevel@tonic-gate 		 * case since it sets things up.  We fall through
46887c478bd9Sstevel@tonic-gate 		 * to the lower code and get the hardware set up.
46897c478bd9Sstevel@tonic-gate 		 * subsequent times we just lock the list and insert
46907c478bd9Sstevel@tonic-gate 		 * the handler and all is well.
46917c478bd9Sstevel@tonic-gate 		 */
46927c478bd9Sstevel@tonic-gate 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
46937c478bd9Sstevel@tonic-gate 		if (intr == NULL) {
46947c478bd9Sstevel@tonic-gate 			sockp->ls_error = BAD_IRQ;
46957c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
46967c478bd9Sstevel@tonic-gate 		}
46977c478bd9Sstevel@tonic-gate 		intr->intr = hdlp->ih_cb_func;
4698360e6f5eSmathue 		intr->handler_id = (uint_t)(uintptr_t)rdip;
46997c478bd9Sstevel@tonic-gate 		intr->arg1 = hdlp->ih_cb_arg1;
47007c478bd9Sstevel@tonic-gate 		intr->arg2 = hdlp->ih_cb_arg2;
47017c478bd9Sstevel@tonic-gate 		intr->socket = socket;
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 		mutex_enter(&sockp->ls_ilock);
47047c478bd9Sstevel@tonic-gate 		if (sockp->ls_inthandlers == NULL) {
47057c478bd9Sstevel@tonic-gate 			intr->next = intr->prev = intr;
47067c478bd9Sstevel@tonic-gate 			sockp->ls_inthandlers = intr;
47077c478bd9Sstevel@tonic-gate 			sockp->ls_mfintr_dip = rdip;
47087c478bd9Sstevel@tonic-gate 			mutex_exit(&sockp->ls_ilock);
47097c478bd9Sstevel@tonic-gate 
47107c478bd9Sstevel@tonic-gate 			/*
47117c478bd9Sstevel@tonic-gate 			 * replace first function handler with
47127c478bd9Sstevel@tonic-gate 			 * the mfc handler
47137c478bd9Sstevel@tonic-gate 			 */
47147c478bd9Sstevel@tonic-gate 			handler.handler =  (f_tt *)pcmcia_mfc_intr;
47157c478bd9Sstevel@tonic-gate 			handler.arg1 = (caddr_t)sockp;
47167c478bd9Sstevel@tonic-gate 			handler.arg2 = NULL;
47177c478bd9Sstevel@tonic-gate 		} else {
47187c478bd9Sstevel@tonic-gate 			insque(intr, sockp->ls_inthandlers);
47197c478bd9Sstevel@tonic-gate 			mutex_exit(&sockp->ls_ilock);
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 			pispec->intrspec_vec = sockp->ls_intr_vec;
47227c478bd9Sstevel@tonic-gate 			pispec->intrspec_pri = sockp->ls_intr_pri;
47237c478bd9Sstevel@tonic-gate 			hdlp->ih_pri = sockp->ls_intr_pri;
47247c478bd9Sstevel@tonic-gate 
47257c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
47267c478bd9Sstevel@tonic-gate 		}
47277c478bd9Sstevel@tonic-gate 	}
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
47307c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
47317c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() let adapter do it\n");
47327c478bd9Sstevel@tonic-gate 	}
47337c478bd9Sstevel@tonic-gate #endif
47347c478bd9Sstevel@tonic-gate 	pispec->intrspec_func = (uint32_t (*)())handler.handler;
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 	/* set default IPL then check for override */
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 	pispec->intrspec_pri = sockp->ls_intr_pri;
47397c478bd9Sstevel@tonic-gate 	hdlp->ih_pri = pispec->intrspec_pri;
47407c478bd9Sstevel@tonic-gate 
47417c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
47427c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
47437c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() socket=%d irq=%d"
47447c478bd9Sstevel@tonic-gate 		    " handler_id=0X%x handler=%p arg1=%p arg2=%p\n",
47457c478bd9Sstevel@tonic-gate 		    handler.socket, handler.irq,
47467c478bd9Sstevel@tonic-gate 		    handler.handler_id, (void *)handler.handler, handler.arg1,
47477c478bd9Sstevel@tonic-gate 		    handler.arg2);
47487c478bd9Sstevel@tonic-gate 	}
47497c478bd9Sstevel@tonic-gate #endif
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
47527c478bd9Sstevel@tonic-gate 	    SUCCESS) {
47537c478bd9Sstevel@tonic-gate 		sockp->ls_error = ret;
47547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
47557c478bd9Sstevel@tonic-gate 	}
47567c478bd9Sstevel@tonic-gate 
47577c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
47587c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
47597c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
47607c478bd9Sstevel@tonic-gate 		    " iblk_cookie=%p idev_cookie=%p\n"
47617c478bd9Sstevel@tonic-gate 		    " ls_flags=0X%x PCS_COOKIES_VALID=0X%x\n",
47627c478bd9Sstevel@tonic-gate 		    (void *)handler.iblk_cookie,
47637c478bd9Sstevel@tonic-gate 		    (void *)handler.idev_cookie,
47647c478bd9Sstevel@tonic-gate 		    sockp->ls_flags, PCS_COOKIES_VALID);
47657c478bd9Sstevel@tonic-gate 	}
47667c478bd9Sstevel@tonic-gate #endif
47677c478bd9Sstevel@tonic-gate 
47687c478bd9Sstevel@tonic-gate 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
4769360e6f5eSmathue 		hdlp->ih_pri = (uint_t)(uintptr_t)*handler.iblk_cookie;
47707c478bd9Sstevel@tonic-gate 		sockp->ls_iblk = *handler.iblk_cookie;
47717c478bd9Sstevel@tonic-gate 		sockp->ls_idev = *handler.idev_cookie;
47727c478bd9Sstevel@tonic-gate 		sockp->ls_flags |= PCS_COOKIES_VALID;
47737c478bd9Sstevel@tonic-gate 	}
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
47767c478bd9Sstevel@tonic-gate }
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate void
47797c478bd9Sstevel@tonic-gate pcmcia_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
47807c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
47817c478bd9Sstevel@tonic-gate {
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private *ppd;
47847c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t *sockp;
47857c478bd9Sstevel@tonic-gate 	clear_irq_handler_t handler;
47867c478bd9Sstevel@tonic-gate 	struct intrspec *pispec;
47877c478bd9Sstevel@tonic-gate 	int socket;
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
47907c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
47917c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl() entered"
47927c478bd9Sstevel@tonic-gate 		    " dip=%p rdip=%p hdlp=%p\n",
47937c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
47947c478bd9Sstevel@tonic-gate 	}
47957c478bd9Sstevel@tonic-gate #endif
47967c478bd9Sstevel@tonic-gate 
47977c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
47987c478bd9Sstevel@tonic-gate 	socket = ppd->ppd_socket;
47997c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
48007c478bd9Sstevel@tonic-gate 	pispec = ppd->ppd_intrspec;
48017c478bd9Sstevel@tonic-gate 
48027c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
48037c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
48047c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
48057c478bd9Sstevel@tonic-gate 		    " ls_inthandlers=%p ls_intrspec=%p\n",
48067c478bd9Sstevel@tonic-gate 		    (void *)sockp->ls_inthandlers,
48077c478bd9Sstevel@tonic-gate 		    (void *)&sockp->ls_intrspec);
48087c478bd9Sstevel@tonic-gate 	}
48097c478bd9Sstevel@tonic-gate #endif
48107c478bd9Sstevel@tonic-gate 
48117c478bd9Sstevel@tonic-gate 	/* first handle the multifunction case since it is simple */
48127c478bd9Sstevel@tonic-gate 	mutex_enter(&sockp->ls_ilock);
4813a195726fSgovinda 	if (sockp->ls_inthandlers != NULL) {
48147c478bd9Sstevel@tonic-gate 		/* we must be MFC */
48157c478bd9Sstevel@tonic-gate 		inthandler_t *intr;
48167c478bd9Sstevel@tonic-gate 		int remhandler = 0;
48177c478bd9Sstevel@tonic-gate 		intr = sockp->ls_inthandlers;
48187c478bd9Sstevel@tonic-gate 
48197c478bd9Sstevel@tonic-gate 		/* Check if there is only one handler left */
48207c478bd9Sstevel@tonic-gate 		if ((intr->next == intr) && (intr->prev == intr)) {
4821360e6f5eSmathue 			if (intr->handler_id == (unsigned)(uintptr_t)rdip) {
48227c478bd9Sstevel@tonic-gate 				sockp->ls_inthandlers = NULL;
48237c478bd9Sstevel@tonic-gate 				remhandler++;
48247c478bd9Sstevel@tonic-gate 				kmem_free(intr, sizeof (inthandler_t));
48257c478bd9Sstevel@tonic-gate 			}
48267c478bd9Sstevel@tonic-gate 		} else {
48277c478bd9Sstevel@tonic-gate 			inthandler_t *first;
48287c478bd9Sstevel@tonic-gate 			int done;
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 			for (done = 0, first = intr; !done; intr = intr->next) {
48317c478bd9Sstevel@tonic-gate 				if (intr->next == first)
48327c478bd9Sstevel@tonic-gate 					done++;
4833360e6f5eSmathue 				if (intr->handler_id ==
4834360e6f5eSmathue 				    (unsigned)(uintptr_t)rdip) {
48357c478bd9Sstevel@tonic-gate 					done++;
48367c478bd9Sstevel@tonic-gate 
48377c478bd9Sstevel@tonic-gate 					/*
48387c478bd9Sstevel@tonic-gate 					 * If we're about to remove the
48397c478bd9Sstevel@tonic-gate 					 *	handler at the head of
48407c478bd9Sstevel@tonic-gate 					 *	the list, make the next
48417c478bd9Sstevel@tonic-gate 					 *	handler in line the head.
48427c478bd9Sstevel@tonic-gate 					 */
48437c478bd9Sstevel@tonic-gate 					if (sockp->ls_inthandlers == intr)
48447c478bd9Sstevel@tonic-gate 						sockp->ls_inthandlers =
48457c478bd9Sstevel@tonic-gate 						    intr->next;
48467c478bd9Sstevel@tonic-gate 
48477c478bd9Sstevel@tonic-gate 					remque(intr);
48487c478bd9Sstevel@tonic-gate 					kmem_free(intr, sizeof (inthandler_t));
48497c478bd9Sstevel@tonic-gate 					break;
48507c478bd9Sstevel@tonic-gate 				} /* handler_id */
48517c478bd9Sstevel@tonic-gate 			} /* for */
48527c478bd9Sstevel@tonic-gate 		} /* intr->next */
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate 		if (!remhandler) {
48557c478bd9Sstevel@tonic-gate 			mutex_exit(&sockp->ls_ilock);
48567c478bd9Sstevel@tonic-gate 			return;
48577c478bd9Sstevel@tonic-gate 		}
48587c478bd9Sstevel@tonic-gate 
48597c478bd9Sstevel@tonic-gate 		/* need to get the dip that was used to add the handler */
48607c478bd9Sstevel@tonic-gate 		rdip = sockp->ls_mfintr_dip;
48617c478bd9Sstevel@tonic-gate 	}
48627c478bd9Sstevel@tonic-gate 
4863a195726fSgovinda 	mutex_exit(&sockp->ls_ilock);
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
48667c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
48677c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
48687c478bd9Sstevel@tonic-gate 		    " pispec=%p rdip=%p\n",
48697c478bd9Sstevel@tonic-gate 		    (void *)pispec, (void *)rdip);
48707c478bd9Sstevel@tonic-gate 	}
48717c478bd9Sstevel@tonic-gate #endif
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 	handler.socket = sockp->ls_socket;
4874360e6f5eSmathue 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
48757c478bd9Sstevel@tonic-gate 	handler.handler = (f_tt *)pispec->intrspec_func;
48767c478bd9Sstevel@tonic-gate 	CLEAR_IRQ(sockp->ls_if, dip, &handler);
48777c478bd9Sstevel@tonic-gate }
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 
48807c478bd9Sstevel@tonic-gate /* Consolidated interrupt processing interface */
48817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
48827c478bd9Sstevel@tonic-gate int
48837c478bd9Sstevel@tonic-gate pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
48847c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
48857c478bd9Sstevel@tonic-gate {
4886a195726fSgovinda 	int	ret = DDI_SUCCESS;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
48897c478bd9Sstevel@tonic-gate 	if (pcmcia_debug) {
48907c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_ops() intr_op=%d\n",
48917c478bd9Sstevel@tonic-gate 		    (int)intr_op);
48927c478bd9Sstevel@tonic-gate 	}
48937c478bd9Sstevel@tonic-gate #endif
48947c478bd9Sstevel@tonic-gate 
48957c478bd9Sstevel@tonic-gate 	switch (intr_op) {
48967c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
4897a195726fSgovinda 		*(int *)result = DDI_INTR_FLAG_LEVEL;
48987c478bd9Sstevel@tonic-gate 		break;
48997c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
49007c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
49017c478bd9Sstevel@tonic-gate 		break;
49027c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
49037c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
49047c478bd9Sstevel@tonic-gate 		break;
49057c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
49067c478bd9Sstevel@tonic-gate 		break;
49077c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
49087c478bd9Sstevel@tonic-gate 		if (pcmcia_add_intr_impl(dip, rdip, hdlp) != DDI_SUCCESS)
49097c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
49107c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_pri;
49117c478bd9Sstevel@tonic-gate 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
49127c478bd9Sstevel@tonic-gate 		break;
49137c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
49147c478bd9Sstevel@tonic-gate 		break;
49157c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
49167c478bd9Sstevel@tonic-gate 		ret = pcmcia_add_intr_impl(dip, rdip, hdlp);
49177c478bd9Sstevel@tonic-gate 		break;
49187c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
49197c478bd9Sstevel@tonic-gate 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
49207c478bd9Sstevel@tonic-gate 		break;
49217c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
49227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
49237c478bd9Sstevel@tonic-gate 		break;
49247c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
49257c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
49267c478bd9Sstevel@tonic-gate 		*(int *)result = i_ddi_get_nintrs(rdip);
49277c478bd9Sstevel@tonic-gate 		break;
49287c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
49297c478bd9Sstevel@tonic-gate 		/* PCI nexus driver supports only fixed interrupts */
49307c478bd9Sstevel@tonic-gate 		*(int *)result = i_ddi_get_nintrs(rdip) ?
49317c478bd9Sstevel@tonic-gate 		    DDI_INTR_TYPE_FIXED : 0;
49327c478bd9Sstevel@tonic-gate 		break;
49337c478bd9Sstevel@tonic-gate 	default:
49347c478bd9Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
49357c478bd9Sstevel@tonic-gate 		break;
49367c478bd9Sstevel@tonic-gate 	}
49377c478bd9Sstevel@tonic-gate 
49387c478bd9Sstevel@tonic-gate 	return (ret);
49397c478bd9Sstevel@tonic-gate }
49407c478bd9Sstevel@tonic-gate 
49417c478bd9Sstevel@tonic-gate #elif defined(__x86) || defined(__amd64)
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate static struct intrspec	*pcmcia_intr_get_ispec(dev_info_t *, int,
49447c478bd9Sstevel@tonic-gate 			    pcmcia_logical_socket_t **);
49457c478bd9Sstevel@tonic-gate static struct intrspec	*pcmcia_intr_add_isr(dev_info_t *, dev_info_t *,
49467c478bd9Sstevel@tonic-gate 			    ddi_intr_handle_impl_t *);
49477c478bd9Sstevel@tonic-gate static int		pcmcia_intr_enable_isr(dev_info_t *, dev_info_t *,
49487c478bd9Sstevel@tonic-gate 			    ddi_intr_handle_impl_t *);
49497c478bd9Sstevel@tonic-gate static void		pcmcia_intr_remove_isr(dev_info_t *, dev_info_t *,
49507c478bd9Sstevel@tonic-gate 			    ddi_intr_handle_impl_t *);
49517c478bd9Sstevel@tonic-gate static void		pcmcia_intr_disable_isr(dev_info_t *, dev_info_t *,
49527c478bd9Sstevel@tonic-gate 			    ddi_intr_handle_impl_t *);
49537c478bd9Sstevel@tonic-gate 
49547c478bd9Sstevel@tonic-gate /*
49557c478bd9Sstevel@tonic-gate  * pcmcia_intr_get_ispec:
49567c478bd9Sstevel@tonic-gate  *	This is mostly copied from older 'pcmcia_get_intrspec' function
49577c478bd9Sstevel@tonic-gate  */
49587c478bd9Sstevel@tonic-gate static struct intrspec *
49597c478bd9Sstevel@tonic-gate pcmcia_intr_get_ispec(dev_info_t *rdip, int inum,
49607c478bd9Sstevel@tonic-gate     pcmcia_logical_socket_t **sockp)
49617c478bd9Sstevel@tonic-gate {
49627c478bd9Sstevel@tonic-gate 	int				socket;
49637c478bd9Sstevel@tonic-gate 	struct intrspec			*intrspec;
49647c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private	*ppd;
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	if ((int)inum > 0 || (ddi_getprop(DDI_DEV_T_ANY, rdip,
49677c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "interrupts", -1) < 0))
49687c478bd9Sstevel@tonic-gate 		return (NULL);
49697c478bd9Sstevel@tonic-gate 
49707c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
49717c478bd9Sstevel@tonic-gate 	if (ppd == NULL || ppd->ppd_intrspec == NULL)
49727c478bd9Sstevel@tonic-gate 		return (NULL);
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate 	if ((socket = ppd->ppd_socket) < 0)
49757c478bd9Sstevel@tonic-gate 		return (NULL);
49767c478bd9Sstevel@tonic-gate 
49777c478bd9Sstevel@tonic-gate 	if ((*sockp = pcmcia_sockets[socket]) == NULL)
49787c478bd9Sstevel@tonic-gate 		return (NULL);
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	intrspec = ppd->ppd_intrspec;
49817c478bd9Sstevel@tonic-gate 	if (intrspec->intrspec_vec == 0 && (*sockp)->ls_intr_vec != 0)
49827c478bd9Sstevel@tonic-gate 		intrspec->intrspec_vec = (*sockp)->ls_intr_vec;
49837c478bd9Sstevel@tonic-gate 
49847c478bd9Sstevel@tonic-gate 	return (intrspec);
49857c478bd9Sstevel@tonic-gate }
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate static struct intrspec *
49887c478bd9Sstevel@tonic-gate pcmcia_intr_add_isr(dev_info_t *dip, dev_info_t *rdip,
49897c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
49907c478bd9Sstevel@tonic-gate {
49917c478bd9Sstevel@tonic-gate 	int				socket;
49927c478bd9Sstevel@tonic-gate 	struct intrspec			*ispecp;
49937c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter		*adapt;
49947c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t		*sockp;
49957c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private	*ppd;
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
49987c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
49997c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_add_isr: "
50007c478bd9Sstevel@tonic-gate 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
50017c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
50027c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
50057c478bd9Sstevel@tonic-gate 	socket = ppd->ppd_socket;
50067c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
50077c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 	ispecp = ppd->ppd_intrspec;
50107c478bd9Sstevel@tonic-gate 	if (ispecp == NULL) {
50117c478bd9Sstevel@tonic-gate 		sockp->ls_error = BAD_IRQ;
50127c478bd9Sstevel@tonic-gate 		return (ispecp);
50137c478bd9Sstevel@tonic-gate 	}
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	/*
50167c478bd9Sstevel@tonic-gate 	 * check if multifunction and do the right thing
50177c478bd9Sstevel@tonic-gate 	 * we put an intercept in between the mfc handler and us so we can
50187c478bd9Sstevel@tonic-gate 	 * catch and process. We might be able to optimize this depending
50197c478bd9Sstevel@tonic-gate 	 * on the card features (a future option).
50207c478bd9Sstevel@tonic-gate 	 */
50217c478bd9Sstevel@tonic-gate 	if (ppd->ppd_flags & PPD_CARD_MULTI &&
50227c478bd9Sstevel@tonic-gate 	    hdlp->ih_cb_func != pcmcia_mfc_intr) {
50237c478bd9Sstevel@tonic-gate 		inthandler_t *intr;
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 		/*
50267c478bd9Sstevel@tonic-gate 		 * note that the first function is a special case since it
50277c478bd9Sstevel@tonic-gate 		 * sets things up.  We fall through to the lower code and
50287c478bd9Sstevel@tonic-gate 		 * get the hardware set up. Subsequent times we just lock
50297c478bd9Sstevel@tonic-gate 		 * the list and insert the handler and all is well.
50307c478bd9Sstevel@tonic-gate 		 */
50317c478bd9Sstevel@tonic-gate 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
50327c478bd9Sstevel@tonic-gate 		if (intr == NULL) {
50337c478bd9Sstevel@tonic-gate 			sockp->ls_error = BAD_IRQ;
50347c478bd9Sstevel@tonic-gate 			return (NULL);
50357c478bd9Sstevel@tonic-gate 		}
50367c478bd9Sstevel@tonic-gate 
50377c478bd9Sstevel@tonic-gate 		intr->intr = (uint32_t (*)())hdlp->ih_cb_func;
50387c478bd9Sstevel@tonic-gate 		intr->handler_id = (uint32_t)(uintptr_t)rdip;
50397c478bd9Sstevel@tonic-gate 		intr->arg1 = hdlp->ih_cb_arg1;
50407c478bd9Sstevel@tonic-gate 		intr->arg2 = hdlp->ih_cb_arg2;
50417c478bd9Sstevel@tonic-gate 		intr->socket = socket;
50427c478bd9Sstevel@tonic-gate 		mutex_enter(&sockp->ls_ilock);
50437c478bd9Sstevel@tonic-gate 		if (sockp->ls_inthandlers == NULL) {
50447c478bd9Sstevel@tonic-gate 			intr->next = intr->prev = intr;
50457c478bd9Sstevel@tonic-gate 			sockp->ls_inthandlers = intr;
50467c478bd9Sstevel@tonic-gate 			sockp->ls_mfintr_dip = rdip;
50477c478bd9Sstevel@tonic-gate 		} else {
50487c478bd9Sstevel@tonic-gate 			insque(intr, sockp->ls_inthandlers);
50497c478bd9Sstevel@tonic-gate 		}
50507c478bd9Sstevel@tonic-gate 		mutex_exit(&sockp->ls_ilock);
50517c478bd9Sstevel@tonic-gate 		return (ispecp);
50527c478bd9Sstevel@tonic-gate 	}
50537c478bd9Sstevel@tonic-gate 
50547c478bd9Sstevel@tonic-gate 	/*
50557c478bd9Sstevel@tonic-gate 	 * Do we need to allocate an IRQ at this point or not?
50567c478bd9Sstevel@tonic-gate 	 */
50577c478bd9Sstevel@tonic-gate 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
50587c478bd9Sstevel@tonic-gate 		int i, irq;
50597c478bd9Sstevel@tonic-gate 
50607c478bd9Sstevel@tonic-gate 		/*
50617c478bd9Sstevel@tonic-gate 		 * this adapter needs IRQ allocations
50627c478bd9Sstevel@tonic-gate 		 * this is only necessary if it is the first function on the
50637c478bd9Sstevel@tonic-gate 		 * card being setup. The socket will keep the allocation info
50647c478bd9Sstevel@tonic-gate 		 */
50657c478bd9Sstevel@tonic-gate 		/* all functions use same intrspec except mfc handler */
50667c478bd9Sstevel@tonic-gate 		if (hdlp->ih_cb_func == pcmcia_mfc_intr) {
50677c478bd9Sstevel@tonic-gate 			/*
50687c478bd9Sstevel@tonic-gate 			 * We treat this special in order to allow things to
50697c478bd9Sstevel@tonic-gate 			 * work properly for MFC cards. The intrspec for the
50707c478bd9Sstevel@tonic-gate 			 * mfc dispatcher is intercepted and taken from the
50717c478bd9Sstevel@tonic-gate 			 * logical socket in order to not be trying to
50727c478bd9Sstevel@tonic-gate 			 * multiplex the meaning when ENABLE is called.
50737c478bd9Sstevel@tonic-gate 			 */
50747c478bd9Sstevel@tonic-gate 			ispecp = &sockp->ls_intrspec;
5075*7a364d25Sschwartz 			((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
50767c478bd9Sstevel@tonic-gate 		}
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 		if (adapt->pca_flags & PCA_IRQ_ISA) {
50797c478bd9Sstevel@tonic-gate 			for (irq = -1, i = 1; irq == -1 && i < 16; i++) {
50807c478bd9Sstevel@tonic-gate 				/* find available and usable IRQ level */
50817c478bd9Sstevel@tonic-gate 				if (adapt->pca_avail_intr & (1 << i))
50827c478bd9Sstevel@tonic-gate 					irq = pcmcia_get_intr(dip, i);
50837c478bd9Sstevel@tonic-gate 			}
50847c478bd9Sstevel@tonic-gate 		}
50857c478bd9Sstevel@tonic-gate 		if (irq < 0) {
50867c478bd9Sstevel@tonic-gate 			sockp->ls_error = NO_RESOURCE;
50877c478bd9Sstevel@tonic-gate 			return (NULL);
50887c478bd9Sstevel@tonic-gate 		}
50897c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = sockp->ls_intr_vec = irq;
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
50937c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
50947c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "allocated irq=%x\n", irq);
50957c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
50967c478bd9Sstevel@tonic-gate 
50977c478bd9Sstevel@tonic-gate 		ispecp->intrspec_vec = sockp->ls_intr_vec;
50987c478bd9Sstevel@tonic-gate 		ispecp->intrspec_pri = sockp->ls_intr_pri;
50997c478bd9Sstevel@tonic-gate 		return (ispecp);
51007c478bd9Sstevel@tonic-gate 	}
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	if (ispecp->intrspec_func != NULL)
51037c478bd9Sstevel@tonic-gate 		ispecp->intrspec_func = hdlp->ih_cb_func;
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	/* set default IPL then check for override */
51067c478bd9Sstevel@tonic-gate 	ispecp->intrspec_pri = sockp->ls_intr_pri;
51077c478bd9Sstevel@tonic-gate 	return (ispecp);
51087c478bd9Sstevel@tonic-gate }
51097c478bd9Sstevel@tonic-gate 
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate static int
51127c478bd9Sstevel@tonic-gate pcmcia_intr_enable_isr(dev_info_t *dip, dev_info_t *rdip,
51137c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
51147c478bd9Sstevel@tonic-gate {
51157c478bd9Sstevel@tonic-gate 	int				socket, ret;
51167c478bd9Sstevel@tonic-gate 	int				irq = 0;	/* default case */
51177c478bd9Sstevel@tonic-gate 	dev_info_t			*parent = ddi_root_node();
51187c478bd9Sstevel@tonic-gate 	struct intrspec			*ispecp;
51197c478bd9Sstevel@tonic-gate 	set_irq_handler_t		handler;
51207c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter		*adapt;
51217c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t		*sockp;
51227c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private	*ppd;
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
51257c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
51267c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr: "
51277c478bd9Sstevel@tonic-gate 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
51287c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
51297c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
51307c478bd9Sstevel@tonic-gate 
51317c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
51327c478bd9Sstevel@tonic-gate 	socket = ppd->ppd_socket;
51337c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
51347c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	ispecp = ppd->ppd_intrspec;
51377c478bd9Sstevel@tonic-gate 	ASSERT(ispecp);
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 	mutex_enter(&sockp->ls_ilock);
51407c478bd9Sstevel@tonic-gate 	if ((sockp->ls_inthandlers != NULL) &&
5141*7a364d25Sschwartz 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5142*7a364d25Sschwartz 	    &sockp->ls_intrspec) {
51437c478bd9Sstevel@tonic-gate 		inthandler_t *intr = sockp->ls_inthandlers;
51447c478bd9Sstevel@tonic-gate 
51457c478bd9Sstevel@tonic-gate 		ASSERT(ppd->ppd_flags & PPD_CARD_MULTI);
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate 		/* Only one handler. So, call ddi_add_intr on it */
51487c478bd9Sstevel@tonic-gate 		if ((intr->next == intr) && (intr->prev == intr)) {
51497c478bd9Sstevel@tonic-gate 			hdlp->ih_cb_func = pcmcia_mfc_intr;
51507c478bd9Sstevel@tonic-gate 			hdlp->ih_cb_arg1 = (caddr_t)sockp;
51517c478bd9Sstevel@tonic-gate 			hdlp->ih_cb_arg2 = NULL;
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate 			ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->
51547c478bd9Sstevel@tonic-gate 			    bus_intr_op))(parent, rdip, DDI_INTROP_ENABLE,
51557c478bd9Sstevel@tonic-gate 			    hdlp, NULL);
51567c478bd9Sstevel@tonic-gate 
51577c478bd9Sstevel@tonic-gate 			if (ret == DDI_FAILURE) {
51587c478bd9Sstevel@tonic-gate 				sockp->ls_inthandlers = NULL;
51597c478bd9Sstevel@tonic-gate 				kmem_free(intr, sizeof (inthandler_t));
51607c478bd9Sstevel@tonic-gate 				sockp->ls_error = BAD_IRQ;
51617c478bd9Sstevel@tonic-gate 				mutex_exit(&sockp->ls_ilock);
51627c478bd9Sstevel@tonic-gate 				return (ret);
51637c478bd9Sstevel@tonic-gate 			}
51647c478bd9Sstevel@tonic-gate 		}
51657c478bd9Sstevel@tonic-gate 		mutex_exit(&sockp->ls_ilock);
51667c478bd9Sstevel@tonic-gate 		hdlp->ih_vector = ispecp->intrspec_vec = sockp->ls_intr_vec;
51677c478bd9Sstevel@tonic-gate 		hdlp->ih_pri = sockp->ls_intr_pri;
5168abdbd06dSagiri 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
51697c478bd9Sstevel@tonic-gate 		    sockp->ls_intr_pri;
51707c478bd9Sstevel@tonic-gate 		sockp->ls_idev.idev_vector = (ushort_t)hdlp->ih_vector;
51717c478bd9Sstevel@tonic-gate 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
51727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
51737c478bd9Sstevel@tonic-gate 	}
51747c478bd9Sstevel@tonic-gate 	mutex_exit(&sockp->ls_ilock);
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
51777c478bd9Sstevel@tonic-gate 		if (hdlp->ih_cb_func == pcmcia_mfc_intr)
51787c478bd9Sstevel@tonic-gate 			ispecp = (struct intrspec *)&sockp->ls_intrspec;
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 		/* XXX: remove it later as this is done in _add_isr as well */
51817c478bd9Sstevel@tonic-gate 		ispecp->intrspec_vec = sockp->ls_intr_vec;
51827c478bd9Sstevel@tonic-gate 		ispecp->intrspec_pri = sockp->ls_intr_pri;
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 		/* Enable interrupts */
51857c478bd9Sstevel@tonic-gate 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
51867c478bd9Sstevel@tonic-gate 		    parent, rdip, DDI_INTROP_ENABLE, hdlp, NULL);
51877c478bd9Sstevel@tonic-gate 
5188abdbd06dSagiri 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
51897c478bd9Sstevel@tonic-gate 		    sockp->ls_intr_pri;
51907c478bd9Sstevel@tonic-gate 		sockp->ls_idev.idev_vector = (ushort_t)sockp->ls_intr_vec;
51917c478bd9Sstevel@tonic-gate 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS)
51947c478bd9Sstevel@tonic-gate 			sockp->ls_error = BAD_IRQ;
51957c478bd9Sstevel@tonic-gate 		return (ret);
51967c478bd9Sstevel@tonic-gate 	}
51977c478bd9Sstevel@tonic-gate 
51987c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
51997c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
52007c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr; let adapter do it\n");
52017c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
52027c478bd9Sstevel@tonic-gate 
52037c478bd9Sstevel@tonic-gate 	handler.socket = sockp->ls_socket;
52047c478bd9Sstevel@tonic-gate 	handler.irq = irq;
52057c478bd9Sstevel@tonic-gate 	handler.handler = (f_tt *)hdlp->ih_cb_func;
52067c478bd9Sstevel@tonic-gate 	handler.arg1 = hdlp->ih_cb_arg1;
52077c478bd9Sstevel@tonic-gate 	handler.arg2 = hdlp->ih_cb_arg2;
52087c478bd9Sstevel@tonic-gate 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
52097c478bd9Sstevel@tonic-gate 	if (ispecp->intrspec_func != NULL)
52107c478bd9Sstevel@tonic-gate 		ispecp->intrspec_func = hdlp->ih_cb_func;
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate 	/* set default IPL then check for override */
52137c478bd9Sstevel@tonic-gate 	ispecp->intrspec_pri = sockp->ls_intr_pri;
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
52167c478bd9Sstevel@tonic-gate 	    SUCCESS) {
52177c478bd9Sstevel@tonic-gate 		sockp->ls_error = ret;
52187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
52197c478bd9Sstevel@tonic-gate 	}
52207c478bd9Sstevel@tonic-gate 	ispecp->intrspec_func = hdlp->ih_cb_func;
52217c478bd9Sstevel@tonic-gate 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
52227c478bd9Sstevel@tonic-gate 		sockp->ls_iblk = *handler.iblk_cookie;
52237c478bd9Sstevel@tonic-gate 		sockp->ls_idev = *handler.idev_cookie;
52247c478bd9Sstevel@tonic-gate 		sockp->ls_flags |= PCS_COOKIES_VALID;
52257c478bd9Sstevel@tonic-gate 	}
52267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
52277c478bd9Sstevel@tonic-gate }
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate /* ARGSUSED */
52307c478bd9Sstevel@tonic-gate static void
52317c478bd9Sstevel@tonic-gate pcmcia_intr_remove_isr(dev_info_t *dip, dev_info_t *rdip,
52327c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
52337c478bd9Sstevel@tonic-gate {
52347c478bd9Sstevel@tonic-gate 	int				done, remhandler = 0;
52357c478bd9Sstevel@tonic-gate 	inthandler_t			*intr, *first;
52367c478bd9Sstevel@tonic-gate 	struct intrspec			*ispecp;
52377c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t		*sockp;
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
52407c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
52417c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_remove_isr: "
52427c478bd9Sstevel@tonic-gate 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
52437c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
52447c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 	ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
52477c478bd9Sstevel@tonic-gate 	ASSERT(ispecp);
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	/* first handle the multifunction case since it is simple */
52507c478bd9Sstevel@tonic-gate 	mutex_enter(&sockp->ls_ilock);
52517c478bd9Sstevel@tonic-gate 	if (sockp->ls_inthandlers != NULL &&
5252*7a364d25Sschwartz 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5253*7a364d25Sschwartz 	    &sockp->ls_intrspec) {
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 		intr = sockp->ls_inthandlers;
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate 		/* Check if there is only one handler left */
52587c478bd9Sstevel@tonic-gate 		if ((intr->next == intr) && (intr->prev == intr)) {
52597c478bd9Sstevel@tonic-gate 			if (intr->handler_id == (uint32_t)(uintptr_t)rdip) {
52607c478bd9Sstevel@tonic-gate 				sockp->ls_inthandlers = NULL;
52617c478bd9Sstevel@tonic-gate 				remhandler++;
52627c478bd9Sstevel@tonic-gate 				kmem_free(intr, sizeof (inthandler_t));
52637c478bd9Sstevel@tonic-gate 			}
52647c478bd9Sstevel@tonic-gate 
52657c478bd9Sstevel@tonic-gate 		} else {
52667c478bd9Sstevel@tonic-gate 			for (done = 0, first = intr; !done; intr = intr->next) {
52677c478bd9Sstevel@tonic-gate 				if (intr->next == first)
52687c478bd9Sstevel@tonic-gate 					done++;
52697c478bd9Sstevel@tonic-gate 				if (intr->handler_id ==
52707c478bd9Sstevel@tonic-gate 				    (uint32_t)(uintptr_t)rdip) {
52717c478bd9Sstevel@tonic-gate 					done++;
52727c478bd9Sstevel@tonic-gate 
52737c478bd9Sstevel@tonic-gate 					/*
52747c478bd9Sstevel@tonic-gate 					 * If we're about to remove the handler
52757c478bd9Sstevel@tonic-gate 					 * at the head of the list, make the
52767c478bd9Sstevel@tonic-gate 					 * next handler in line the head.
52777c478bd9Sstevel@tonic-gate 					 */
52787c478bd9Sstevel@tonic-gate 					if (sockp->ls_inthandlers == intr)
52797c478bd9Sstevel@tonic-gate 					    sockp->ls_inthandlers = intr->next;
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 					remque(intr);
52827c478bd9Sstevel@tonic-gate 					kmem_free(intr, sizeof (inthandler_t));
52837c478bd9Sstevel@tonic-gate 					break;
52847c478bd9Sstevel@tonic-gate 				} /* handler_id */
52857c478bd9Sstevel@tonic-gate 			} /* end of for */
52867c478bd9Sstevel@tonic-gate 		} /* end of if intr->next */
52877c478bd9Sstevel@tonic-gate 
52887c478bd9Sstevel@tonic-gate 		if (!remhandler) {
52897c478bd9Sstevel@tonic-gate 			mutex_exit(&sockp->ls_ilock);
52907c478bd9Sstevel@tonic-gate 			return;
52917c478bd9Sstevel@tonic-gate 		}
52927c478bd9Sstevel@tonic-gate 	}
52937c478bd9Sstevel@tonic-gate 	mutex_exit(&sockp->ls_ilock);
52947c478bd9Sstevel@tonic-gate 
52957c478bd9Sstevel@tonic-gate 	if (sockp->ls_adapter->pca_flags & PCA_RES_NEED_IRQ) {
52967c478bd9Sstevel@tonic-gate 		sockp->ls_intr_vec = 0;
52977c478bd9Sstevel@tonic-gate 		ispecp->intrspec_vec = 0;
52987c478bd9Sstevel@tonic-gate 	}
52997c478bd9Sstevel@tonic-gate }
53007c478bd9Sstevel@tonic-gate 
53017c478bd9Sstevel@tonic-gate 
53027c478bd9Sstevel@tonic-gate static void
53037c478bd9Sstevel@tonic-gate pcmcia_intr_disable_isr(dev_info_t *dip, dev_info_t *rdip,
53047c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp)
53057c478bd9Sstevel@tonic-gate {
53067c478bd9Sstevel@tonic-gate 	int				socket, ret;
53077c478bd9Sstevel@tonic-gate 	dev_info_t			*parent;
53087c478bd9Sstevel@tonic-gate 	struct intrspec			*ispecp;
53097c478bd9Sstevel@tonic-gate 	clear_irq_handler_t		handler;
53107c478bd9Sstevel@tonic-gate 	struct pcmcia_adapter		*adapt;
53117c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t		*sockp;
53127c478bd9Sstevel@tonic-gate 	struct pcmcia_parent_private	*ppd;
5313*7a364d25Sschwartz 	ihdl_plat_t			*ihdl_plat_datap =
5314*7a364d25Sschwartz 					    (ihdl_plat_t *)hdlp->ih_private;
53157c478bd9Sstevel@tonic-gate 
53167c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
53177c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
53187c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
53197c478bd9Sstevel@tonic-gate 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
53207c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, (void *)hdlp);
53217c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
53227c478bd9Sstevel@tonic-gate 
53237c478bd9Sstevel@tonic-gate 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
53247c478bd9Sstevel@tonic-gate 	socket = ppd->ppd_socket;
53257c478bd9Sstevel@tonic-gate 	sockp = pcmcia_sockets[socket];
53267c478bd9Sstevel@tonic-gate 	adapt = sockp->ls_adapter;
53277c478bd9Sstevel@tonic-gate 	ispecp = ppd->ppd_intrspec;
53287c478bd9Sstevel@tonic-gate 	ASSERT(ispecp);
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	mutex_enter(&sockp->ls_ilock);
53317c478bd9Sstevel@tonic-gate 	if (sockp->ls_inthandlers != NULL &&
5332*7a364d25Sschwartz 	    ihdl_plat_datap->ip_ispecp != &sockp->ls_intrspec) {
53337c478bd9Sstevel@tonic-gate 		inthandler_t	*intr = sockp->ls_inthandlers;
53347c478bd9Sstevel@tonic-gate 
53357c478bd9Sstevel@tonic-gate 		/* Check if there is only one handler left */
53367c478bd9Sstevel@tonic-gate 		if ((intr->next == intr) && (intr->prev == intr)) {
53377c478bd9Sstevel@tonic-gate 			if (intr->handler_id != (uint32_t)(uintptr_t)rdip)
53387c478bd9Sstevel@tonic-gate 				/*
53397c478bd9Sstevel@tonic-gate 				 * need to get the dip that was
53407c478bd9Sstevel@tonic-gate 				 * used to add the handler
53417c478bd9Sstevel@tonic-gate 				 */
53427c478bd9Sstevel@tonic-gate 				rdip = sockp->ls_mfintr_dip;
53437c478bd9Sstevel@tonic-gate 				ispecp = (struct intrspec *)&sockp->ls_intrspec;
53447c478bd9Sstevel@tonic-gate 		} else {
53457c478bd9Sstevel@tonic-gate 			/* Don't call cleanup if list still has members */
53467c478bd9Sstevel@tonic-gate 			mutex_exit(&sockp->ls_ilock);
53477c478bd9Sstevel@tonic-gate 			return;
53487c478bd9Sstevel@tonic-gate 		}
53497c478bd9Sstevel@tonic-gate 	}
53507c478bd9Sstevel@tonic-gate 	mutex_exit(&sockp->ls_ilock);
53517c478bd9Sstevel@tonic-gate 
5352*7a364d25Sschwartz 	if (ihdl_plat_datap->ip_ispecp ==
53537c478bd9Sstevel@tonic-gate 	    (struct intrspec *)&sockp->ls_intrspec)
5354*7a364d25Sschwartz 		ispecp = ihdl_plat_datap->ip_ispecp;
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
53577c478bd9Sstevel@tonic-gate 		ret = ispecp->intrspec_vec;
53587c478bd9Sstevel@tonic-gate 		parent = ddi_root_node();
53597c478bd9Sstevel@tonic-gate 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
53607c478bd9Sstevel@tonic-gate 		    parent, rdip, DDI_INTROP_DISABLE, hdlp, NULL);
53617c478bd9Sstevel@tonic-gate 		(void) pcmcia_return_intr(dip, hdlp->ih_vector);
53627c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
53637c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
53647c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
53657c478bd9Sstevel@tonic-gate 			    "INTROP_DISABLE returned %x\n", ret);
53667c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
53677c478bd9Sstevel@tonic-gate 	} else {
53687c478bd9Sstevel@tonic-gate 		handler.socket = sockp->ls_socket;
53697c478bd9Sstevel@tonic-gate 		handler.handler_id = (uint32_t)(uintptr_t)rdip;
53707c478bd9Sstevel@tonic-gate 		handler.handler = (f_tt *)ispecp->intrspec_func;
53717c478bd9Sstevel@tonic-gate 		ret = CLEAR_IRQ(sockp->ls_if, dip, &handler);
53727c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
53737c478bd9Sstevel@tonic-gate 		if (pcmcia_debug)
53747c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
53757c478bd9Sstevel@tonic-gate 			    "CLEAR_IRQ returned %x\n", ret);
53767c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate }
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate /* Consolidated interrupt processing interface */
53817c478bd9Sstevel@tonic-gate int
53827c478bd9Sstevel@tonic-gate pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
53837c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
53847c478bd9Sstevel@tonic-gate {
53857c478bd9Sstevel@tonic-gate 	struct intrspec		*ispecp;
53867c478bd9Sstevel@tonic-gate 	pcmcia_logical_socket_t	*sockp;
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate #if defined(PCMCIA_DEBUG)
53897c478bd9Sstevel@tonic-gate 	if (pcmcia_debug)
53907c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pcmcia_intr_ops: "
53917c478bd9Sstevel@tonic-gate 		    "dip=0x%p rdip=0x%p op=0x%x hdlp=0x%p\n",
53927c478bd9Sstevel@tonic-gate 		    (void *)dip, (void *)rdip, intr_op, (void *)hdlp);
53937c478bd9Sstevel@tonic-gate #endif	/* PCMCIA_DEBUG */
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate 	switch (intr_op) {
53967c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
53977c478bd9Sstevel@tonic-gate 		if (ddi_get_parent_data(rdip) == NULL) {
53987c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
53997c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54007c478bd9Sstevel@tonic-gate 		}
54017c478bd9Sstevel@tonic-gate 		*(int *)result = DDI_INTR_TYPE_FIXED;
54027c478bd9Sstevel@tonic-gate 		break;
54037c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
5404a195726fSgovinda 		*(int *)result = DDI_INTR_FLAG_LEVEL;
54057c478bd9Sstevel@tonic-gate 		break;
54067c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
54077c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
54087c478bd9Sstevel@tonic-gate 		if (ddi_get_parent_data(rdip) == NULL) {
54097c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
54107c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54117c478bd9Sstevel@tonic-gate 		}
54127c478bd9Sstevel@tonic-gate 		*(int *)result = 1;	/* for PCMCIA there is only one intr */
54137c478bd9Sstevel@tonic-gate 		break;
54147c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
54157c478bd9Sstevel@tonic-gate 		if ((ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum,
54167c478bd9Sstevel@tonic-gate 		    &sockp)) == NULL)
54177c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54187c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
54197c478bd9Sstevel@tonic-gate 		break;
54207c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
54217c478bd9Sstevel@tonic-gate 		break;
54227c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
54237c478bd9Sstevel@tonic-gate 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
54247c478bd9Sstevel@tonic-gate 		if (ispecp == NULL) {
54257c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
54267c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54277c478bd9Sstevel@tonic-gate 		}
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 		*(int *)result = ispecp->intrspec_pri = sockp->ls_intr_pri;
54307c478bd9Sstevel@tonic-gate 		break;
54317c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
54327c478bd9Sstevel@tonic-gate 		if (*(int *)result > LOCK_LEVEL)
54337c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54347c478bd9Sstevel@tonic-gate 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
54357c478bd9Sstevel@tonic-gate 		ASSERT(ispecp);
54367c478bd9Sstevel@tonic-gate 		ispecp->intrspec_pri = sockp->ls_intr_pri = *(int *)result;
54377c478bd9Sstevel@tonic-gate 		break;
54387c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
54397c478bd9Sstevel@tonic-gate 		if ((ispecp = pcmcia_intr_add_isr(dip, rdip, hdlp)) == NULL)
54407c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
5441*7a364d25Sschwartz 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
54427c478bd9Sstevel@tonic-gate 		break;
54437c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
54447c478bd9Sstevel@tonic-gate 		pcmcia_intr_remove_isr(dip, rdip, hdlp);
54457c478bd9Sstevel@tonic-gate 		break;
54467c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
54477c478bd9Sstevel@tonic-gate 		if (pcmcia_intr_enable_isr(dip, rdip, hdlp) != DDI_SUCCESS)
54487c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
54497c478bd9Sstevel@tonic-gate 		break;
54507c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
54517c478bd9Sstevel@tonic-gate 		pcmcia_intr_disable_isr(dip, rdip, hdlp);
54527c478bd9Sstevel@tonic-gate 		break;
54537c478bd9Sstevel@tonic-gate 	default:
54547c478bd9Sstevel@tonic-gate 		return (DDI_ENOTSUP);
54557c478bd9Sstevel@tonic-gate 	}
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
54587c478bd9Sstevel@tonic-gate }
54597c478bd9Sstevel@tonic-gate #endif
5460