xref: /titanic_41/usr/src/uts/common/pcmcia/nexus/pcmcia.c (revision 4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * PCMCIA NEXUS
28  *	The PCMCIA module is a generalized interface for
29  *	implementing PCMCIA nexus drivers.  It preserves
30  *	the logical socket name space while allowing multiple
31  *	instances of the hardware to be properly represented
32  *	in the device tree.
33  *
34  *	The nexus also exports events to an event manager
35  *	driver if it has registered.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/user.h>
41 #include <sys/buf.h>
42 #include <sys/file.h>
43 #include <sys/uio.h>
44 #include <sys/conf.h>
45 #include <sys/stat.h>
46 #include <sys/autoconf.h>
47 #include <sys/vtoc.h>
48 #include <sys/dkio.h>
49 #include <sys/ddi.h>
50 #include <sys/debug.h>
51 #include <sys/sunddi.h>
52 #include <sys/sunndi.h>
53 #include <sys/cred.h>
54 #include <sys/kstat.h>
55 #include <sys/kmem.h>
56 #include <sys/modctl.h>
57 #include <sys/kobj.h>
58 #include <sys/callb.h>
59 #include <sys/param.h>
60 #include <sys/thread.h>
61 #include <sys/proc.h>
62 
63 #include <sys/pctypes.h>
64 #include <sys/pcmcia.h>
65 #include <sys/sservice.h>
66 #include <pcmcia/sys/cs_types.h>
67 #include <pcmcia/sys/cis.h>
68 #include <pcmcia/sys/cis_handlers.h>
69 #include <pcmcia/sys/cs.h>
70 #include <pcmcia/sys/cs_priv.h>
71 
72 #ifdef sparc
73 #include <sys/ddi_subrdefs.h>
74 
75 #elif defined(__x86) || defined(__amd64)
76 #include <sys/mach_intr.h>
77 #endif
78 
79 #undef SocketServices
80 
81 /* some bus specific stuff */
82 
83 /* need PCI regspec size for worst case at present */
84 #include <sys/pci.h>
85 
86 typedef struct pcmcia_logical_socket {
87 	int			ls_socket; /* adapter's socket number */
88 	uint32_t		ls_flags;
89 	struct pcmcia_adapter	*ls_adapter;
90 	pcmcia_if_t		*ls_if;
91 	dev_info_t		*ls_sockdrv;
92 	dev_info_t		*ls_dip[PCMCIA_MAX_FUNCTIONS];
93 	dev_info_t		*ls_mfintr_dip;
94 	int			ls_functions;
95 	uint32_t		ls_cs_events;
96 	uint32_t		ls_intr_pri;
97 	uint32_t		ls_intr_vec;
98 	int			ls_intrrefs;
99 	struct intrspec		ls_intrspec; /* MFC intrspec */
100 	inthandler_t		*ls_inthandlers; /* for multifunction cards */
101 	ddi_iblock_cookie_t	ls_iblk;
102 	ddi_idevice_cookie_t	ls_idev;
103 	kmutex_t		ls_ilock;
104 	int			ls_error; /* error for CS return */
105 } pcmcia_logical_socket_t;
106 
107 /*
108  * entry points used by the true nexus
109  */
110 int pcmcia_detach(dev_info_t *, ddi_detach_cmd_t);
111 int pcmcia_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
112 int pcmcia_prop_op(dev_t, dev_info_t *, dev_info_t *, ddi_prop_op_t,
113 			int, char *, caddr_t, int *);
114 void pcmcia_set_assigned(dev_info_t *, int, ra_return_t *);
115 int pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
116     ddi_intr_handle_impl_t *hdlp, void *result);
117 
118 /*
119  * prototypes used internally by the nexus and sometimes Card Services
120  */
121 int SocketServices(int function, ...);
122 
123 
124 void *CISParser(int function, ...);
125 extern void *(*cis_parser)(int, ...);
126 
127 struct regspec *pcmcia_cons_regspec(dev_info_t *, int, uchar_t *,
128 					ra_return_t *);
129 
130 static int (*pcmcia_card_services)(int, ...) = NULL;
131 
132 /*
133  * variables used in the logical/physical mappings
134  * that the nexus common code maintains.
135  */
136 struct pcmcia_adapter *pcmcia_adapters[PCMCIA_MAX_ADAPTERS];
137 int    pcmcia_num_adapters;
138 pcmcia_logical_socket_t *pcmcia_sockets[PCMCIA_MAX_SOCKETS];
139 int    pcmcia_num_sockets;
140 pcmcia_logical_window_t *pcmcia_windows[PCMCIA_MAX_WINDOWS];
141 int    pcmcia_num_windows;
142 struct power_entry pcmcia_power_table[PCMCIA_MAX_POWER];
143 int	pcmcia_num_power;
144 
145 struct pcmcia_mif *pcmcia_mif_handlers = NULL;
146 pcm_dev_node_t *pcmcia_devnodes = NULL;
147 
148 kmutex_t pcmcia_global_lock;
149 kcondvar_t pcmcia_condvar;
150 kmutex_t pcmcia_enum_lock;
151 
152 /*
153  * Mapping of the device "type" to names acceptable to
154  * the DDI
155  */
156 static char *pcmcia_dev_type[] = {
157 	"multifunction",
158 	"byte",
159 	"serial",
160 	"parallel",
161 	"block",
162 	"display",
163 	"network",
164 	"block",
165 	"byte"
166 };
167 
168 char *pcmcia_default_pm_mode = "parental-suspend-resume";
169 
170 /*
171  * generic names from the approved list:
172  *	disk tape pci sbus scsi token-ring isa keyboard display mouse
173  *	audio ethernet timer memory parallel serial rtc nvram scanner
174  *	floppy(controller) fddi isdn atm ide pccard video-in video-out
175  * in some cases there will need to be device class dependent names.
176  * network -> ethernet, token-ring, etc.
177  * this list is a first guess and is used when all else fails.
178  */
179 
180 char *pcmcia_generic_names[] = {
181 	"multifunction",
182 	"memory",
183 	"serial",
184 	"parallel",
185 	"disk",
186 	"video",		/* no spec for video-out yet */
187 	"network",
188 	"aims",
189 	"scsi",
190 	"security"
191 };
192 
193 #define	PCM_GENNAME_SIZE	(sizeof (pcmcia_generic_names) / \
194 					sizeof (char *))
195 #define	PCMCIA_MAP_IO	0x0
196 #define	PCMCIA_MAP_MEM	0x1
197 #define	PPB_SUBTRACTIVE	((PCI_CLASS_BRIDGE << 16) | (PCI_BRIDGE_PCI << 8) | \
198 		(PCI_BRIDGE_PCI_IF_SUBDECODE))
199 
200 /*
201  * The following should be 2^^n - 1
202  */
203 #define	PCMCIA_SOCKET_BITS	0x7f
204 
205 #ifdef PCMCIA_DEBUG
206 int pcmcia_debug = 0x0;
207 static void pcmcia_dump_minors(dev_info_t *);
208 #endif
209 
210 static f_tt *pcmcia_cs_event = NULL;
211 int pcmcia_timer_id;
212 dev_info_t	*pcmcia_dip;
213 /*
214  * XXX - See comments in cs.c
215  */
216 static f_tt *pcmcia_cis_parser = NULL;
217 
218 extern struct pc_socket_services pc_socket_services;
219 
220 /* some function declarations */
221 static int pcm_adapter_callback(dev_info_t *, int, int, int);
222 extern void pcmcia_init_adapter(anp_t *, dev_info_t *);
223 extern void pcmcia_find_cards(anp_t *);
224 extern void pcmcia_merge_power(struct power_entry *);
225 extern void pcmcia_do_resume(int, pcmcia_logical_socket_t *);
226 extern void pcmcia_resume(int, pcmcia_logical_socket_t *);
227 extern void pcmcia_do_suspend(int, pcmcia_logical_socket_t *);
228 extern void pcm_event_manager(int, int, void *);
229 static void pcmcia_create_dev_info(int);
230 static int pcmcia_create_device(ss_make_device_node_t *);
231 static void pcmcia_init_devinfo(dev_info_t *, struct pcm_device_info *);
232 void pcmcia_fix_string(char *str);
233 dev_info_t *pcmcia_number_socket(dev_info_t *, int);
234 static int pcmcia_merge_conf(dev_info_t *);
235 static uint32_t pcmcia_mfc_intr(caddr_t, caddr_t);
236 void pcmcia_free_resources(dev_info_t *);
237 static void pcmcia_ppd_free(struct pcmcia_parent_private *ppd);
238 int pcmcia_get_intr(dev_info_t *, int);
239 int pcmcia_return_intr(dev_info_t *, int);
240 int pcmcia_ra_alloc(dev_info_t *, ndi_ra_request_t *, ra_return_t *, char *,
241 		dev_info_t **);
242 int pcmcia_ra_free(dev_info_t *, ra_return_t *, char *);
243 
244 extern int cs_init(void);
245 extern int cs_deinit(void);
246 extern void cisp_init(void);
247 extern void cis_deinit(void);
248 
249 /*
250  * non-DDI compliant functions are listed here
251  * some will be declared while others that have
252  * entries in .h files. All will be commented on.
253  *
254  * with declarations:
255  *	ddi_add_child
256  *	ddi_binding_name
257  *	ddi_bus_prop_op
258  *	ddi_ctlops
259  *	ddi_find_devinfo
260  *	ddi_get_name_addr
261  *	ddi_get_parent_data
262  *	ddi_hold_installed_driver
263  *	ddi_name_to_major
264  *	ddi_node_name
265  *	ddi_pathname
266  *	ddi_rele_driver
267  *	ddi_set_name_addr
268  *	ddi_set_parent_data
269  *	ddi_unorphan_devs
270  *	i_ddi_bind_node_to_driver
271  *	i_ddi_bind_node_to_driver
272  *	i_ddi_bus_map
273  *	i_ddi_map_fault
274  *	i_ddi_mem_alloc
275  *	i_ddi_mem_alloc
276  *	i_ddi_mem_free
277  *	i_ddi_mem_free
278  *	modload
279  *	modunload
280  */
281 
282 extern void ddi_unorphan_devs(major_t);
283 
284 /* Card&Socket Services entry points */
285 static int GetCookiesAndDip(sservice_t *);
286 static int SSGetAdapter(get_adapter_t *);
287 static int SSGetPage(get_page_t *);
288 static int SSGetSocket(get_socket_t *);
289 static int SSGetStatus(get_ss_status_t *);
290 static int SSGetWindow(get_window_t *);
291 static int SSInquireAdapter(inquire_adapter_t *);
292 static int SSInquireSocket(inquire_socket_t *);
293 static int SSInquireWindow(inquire_window_t *);
294 static int SSResetSocket(int, int);
295 static int SSSetPage(set_page_t *);
296 static int SSSetSocket(set_socket_t *);
297 static int SSSetWindow(set_window_t *);
298 static int SSSetIRQHandler(set_irq_handler_t *);
299 static int SSClearIRQHandler(clear_irq_handler_t *);
300 
301 static struct modldrv modlmisc = {
302 	&mod_miscops,		/* Type of module. This one is a driver */
303 	"PCMCIA Nexus Support", /* Name of the module. */
304 };
305 
306 static struct modlinkage modlinkage = {
307 	MODREV_1, (void *)&modlmisc, NULL
308 };
309 
310 int
311 _init()
312 {
313 	int	ret;
314 
315 	cisp_init();
316 
317 	if (cs_init() != CS_SUCCESS) {
318 		if (cs_deinit() != CS_SUCCESS)
319 			cmn_err(CE_CONT, "pcmcia: _init cs_deinit error\n");
320 		return (-1);
321 	}
322 
323 	mutex_init(&pcmcia_global_lock, NULL, MUTEX_DEFAULT, NULL);
324 	cv_init(&pcmcia_condvar, NULL, CV_DRIVER, NULL);
325 	mutex_init(&pcmcia_enum_lock, NULL, MUTEX_DEFAULT, NULL);
326 
327 	if ((ret = mod_install(&modlinkage)) != 0) {
328 		mutex_destroy(&pcmcia_global_lock);
329 		cv_destroy(&pcmcia_condvar);
330 		mutex_destroy(&pcmcia_enum_lock);
331 	}
332 	return (ret);
333 }
334 
335 int
336 _fini()
337 {
338 	int	ret;
339 
340 	if ((ret = mod_remove(&modlinkage)) == 0) {
341 		mutex_destroy(&pcmcia_global_lock);
342 		cv_destroy(&pcmcia_condvar);
343 		mutex_destroy(&pcmcia_enum_lock);
344 		cis_deinit();
345 		if (cs_deinit() != CS_SUCCESS) {
346 			cmn_err(CE_CONT, "pcmcia: _fini cs_deinit error\n");
347 		}
348 	}
349 	return (ret);
350 }
351 
352 int
353 _info(struct modinfo *modinfop)
354 {
355 	return (mod_info(&modlinkage, modinfop));
356 }
357 
358 extern pri_t minclsyspri;
359 
360 /*
361  * pcmcia_attach()
362  *	the attach routine must make sure that everything needed is present
363  *	including real hardware.  The sequence of events is:
364  *		attempt to load all adapter drivers
365  *		attempt to load Card Services (which _depends_on pcmcia)
366  *		initialize logical sockets
367  *		report the nexus exists
368  */
369 
370 int
371 pcmcia_attach(dev_info_t *dip, anp_t *adapter)
372 {
373 	int count, done, i;
374 
375 #if defined(PCMCIA_DEBUG)
376 	if (pcmcia_debug) {
377 		cmn_err(CE_CONT, "pcmcia_attach: dip=0x%p adapter=0x%p\n",
378 		    (void *)dip, (void *)adapter);
379 	}
380 #endif
381 
382 	pcmcia_dip = dip;
383 
384 	mutex_enter(&pcmcia_enum_lock);
385 	mutex_enter(&pcmcia_global_lock);
386 	if (pcmcia_num_adapters == 0) {
387 		pcmcia_cis_parser = (f_tt *)CISParser;
388 		cis_parser = (void *(*)(int, ...)) CISParser;
389 		pcmcia_cs_event = (f_tt *)cs_event;
390 		cs_socket_services = SocketServices;
391 		/* tell CS we are up with basic init level */
392 		(void) cs_event(PCE_SS_INIT_STATE, PCE_SS_STATE_INIT, 0);
393 	}
394 
395 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
396 	    PCM_DEVICETYPE, "pccard");
397 
398 	ddi_report_dev(dip);	/* directory/device naming */
399 
400 	/*
401 	 * now setup any power management stuff necessary.
402 	 * we do it here in order to ensure that all PC Card nexi
403 	 * implement it.
404 	 */
405 
406 	if (pm_create_components(dip, 1) != DDI_SUCCESS) {
407 		cmn_err(CE_WARN, "%s: not power managed\n",
408 		    ddi_get_name_addr(dip));
409 	} else {
410 		pm_set_normal_power(dip, 0, 1);
411 	}
412 
413 	/*
414 	 * setup the info necessary for Card Services/SocketServices
415 	 * and notify CS when ready.
416 	 */
417 
418 	pcmcia_free_resources(dip);
419 	pcmcia_init_adapter(adapter, dip);
420 	/* exit mutex so CS can run for any cards found */
421 	mutex_exit(&pcmcia_global_lock);
422 
423 	/*
424 	 * make sure the devices are identified before
425 	 * returning.  We do this by checking each socket to see if
426 	 * a card is present.  If there is one, and there isn't a dip,
427 	 * we can't be done.  We scan the list of sockets doing the
428 	 * check. if we aren't done, wait for a condition variable to
429 	 * wakeup.
430 	 * Because we can miss a wakeup and because things can
431 	 * take time, we do eventually give up and have a timeout.
432 	 */
433 
434 	for (count = 0, done = 0;
435 	    done == 0 && count < max(pcmcia_num_sockets, 16);
436 	    count++) {
437 		done = 1;
438 		/* block CS while checking so we don't miss anything */
439 		mutex_enter(&pcmcia_global_lock);
440 		for (i = 0; i < pcmcia_num_sockets; i++) {
441 			get_ss_status_t status;
442 			if (pcmcia_sockets[i] == NULL)
443 				continue;
444 			bzero(&status, sizeof (status));
445 			status.socket = i;
446 			if (SSGetStatus(&status) == SUCCESS) {
447 				if (status.CardState & SBM_CD &&
448 				    pcmcia_sockets[i]->ls_dip[0] == NULL) {
449 					done = 0;
450 				}
451 			}
452 		}
453 		/* only wait if we aren't done with this set */
454 		if (!done) {
455 			mutex_exit(&pcmcia_global_lock);
456 			delay(10); /* give up CPU for a time */
457 			mutex_enter(&pcmcia_global_lock);
458 		}
459 		mutex_exit(&pcmcia_global_lock);
460 	}
461 
462 	mutex_exit(&pcmcia_enum_lock);
463 	return (DDI_SUCCESS);
464 }
465 
466 /*
467  * pcmcia_detach
468  *	unload everything and then detach the nexus
469  */
470 /* ARGSUSED */
471 int
472 pcmcia_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
473 {
474 	switch (cmd) {
475 	case DDI_DETACH:
476 		pm_destroy_components(dip);
477 		return (DDI_SUCCESS);
478 
479 	/*
480 	 * resume from a checkpoint
481 	 * We don't do anything special here since the adapter
482 	 * driver will generate resume events that we intercept
483 	 * and convert to insert events.
484 	 */
485 	case DDI_SUSPEND:
486 	case DDI_PM_SUSPEND:
487 		return (DDI_SUCCESS);
488 
489 	default:
490 		return (DDI_FAILURE);
491 	}
492 }
493 
494 /*
495  * card_services_error()
496  *	used to make 2.4/2.5 drivers get an error when
497  *	they try to initialize.
498  */
499 static int
500 card_services_error()
501 {
502 	return (CS_BAD_VERSION);
503 }
504 static int (*cs_error_ptr)() = card_services_error;
505 
506 /*
507  * pcmcia_ctlops
508  *	handle the nexus control operations for the cases where
509  *	a PC Card driver gets called and we need to modify the
510  *	devinfo structure or otherwise do bus specific operations
511  */
512 int
513 pcmcia_ctlops(dev_info_t *dip, dev_info_t *rdip,
514 	ddi_ctl_enum_t ctlop, void *arg, void *result)
515 {
516 	int e;
517 	char name[64];
518 	struct pcmcia_parent_private *ppd;
519 	power_req_t *pm;
520 
521 #if defined(PCMCIA_DEBUG)
522 	if (pcmcia_debug) {
523 		cmn_err(CE_CONT, "pcmcia_ctlops(%p, %p, %d, %p, %p)\n",
524 		    (void *)dip, (void *)rdip, ctlop, (void *)arg,
525 		    (void *)result);
526 		if (rdip != NULL && ddi_get_name(rdip) != NULL)
527 			cmn_err(CE_CONT, "\t[%s]\n", ddi_get_name(rdip));
528 	}
529 #endif
530 
531 	switch (ctlop) {
532 	case DDI_CTLOPS_REPORTDEV:
533 		if (rdip == (dev_info_t *)0)
534 			return (DDI_FAILURE);
535 
536 		if (strcmp("pcs", ddi_node_name(rdip)) == 0)
537 			cmn_err(CE_CONT, "?PCCard socket %d at %s@%s\n",
538 			    ddi_get_instance(rdip),
539 			    ddi_driver_name(dip), ddi_get_name_addr(dip));
540 		else
541 			cmn_err(CE_CONT, "?%s%d at %s@%s in socket %d\n",
542 			    ddi_driver_name(rdip),
543 			    ddi_get_instance(rdip),
544 			    ddi_driver_name(dip),
545 			    ddi_get_name_addr(dip),
546 			    CS_GET_SOCKET_NUMBER(
547 			    ddi_getprop(DDI_DEV_T_NONE, rdip,
548 			    DDI_PROP_DONTPASS,
549 			    PCM_DEV_SOCKET, -1)));
550 
551 		return (DDI_SUCCESS);
552 
553 	case DDI_CTLOPS_INITCHILD:
554 		/*
555 		 * we get control here before the child is called.
556 		 * we can change things if necessary.  This is where
557 		 * the CardServices hook gets planted.
558 		 */
559 #if defined(PCMCIA_DEBUG)
560 		if (pcmcia_debug) {
561 			cmn_err(CE_CONT, "pcmcia: init child: %s(%d) @%p\n",
562 			    ddi_node_name(arg), ddi_get_instance(arg),
563 			    (void *)arg);
564 			if (DEVI(arg)->devi_binding_name != NULL)
565 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
566 				    DEVI(arg)->devi_binding_name);
567 			if (DEVI(arg)->devi_node_name != NULL)
568 				cmn_err(CE_CONT, "\tnode_name=%s\n",
569 				    DEVI(arg)->devi_node_name);
570 		}
571 #endif
572 
573 		ppd = (struct pcmcia_parent_private *)
574 		    ddi_get_parent_data((dev_info_t *)arg);
575 		if (ppd == NULL)
576 			return (DDI_FAILURE);
577 
578 		if (strcmp("pcs", ddi_node_name((dev_info_t *)arg)) == 0) {
579 			if (ppd == NULL)
580 				return (DDI_FAILURE);
581 			(void) sprintf(name, "%x",
582 			    (int)ppd->ppd_reg[0].phys_hi);
583 			ddi_set_name_addr((dev_info_t *)arg, name);
584 			return (DDI_SUCCESS);
585 		}
586 
587 		/*
588 		 * We don't want driver.conf files that stay in
589 		 * pseudo device form.	It is acceptable to have
590 		 * .conf files add properties only.
591 		 */
592 		if (ndi_dev_is_persistent_node((dev_info_t *)arg) == 0) {
593 			(void) pcmcia_merge_conf((dev_info_t *)arg);
594 			cmn_err(CE_WARN, "%s%d: %s.conf invalid",
595 			    ddi_get_name((dev_info_t *)arg),
596 			    ddi_get_instance((dev_info_t *)arg),
597 			    ddi_get_name((dev_info_t *)arg));
598 			return (DDI_FAILURE);
599 		}
600 
601 
602 #if defined(PCMCIA_DEBUG)
603 		if (pcmcia_debug && ppd != NULL) {
604 			cmn_err(CE_CONT, "\tnreg=%x, intr=%x, socket=%x,"
605 			    " function=%x, active=%x, flags=%x\n",
606 			    ppd->ppd_nreg, ppd->ppd_intr,
607 			    ppd->ppd_socket, ppd->ppd_function,
608 			    ppd->ppd_active, ppd->ppd_flags);
609 		}
610 #endif
611 
612 		/*
613 		 * make sure names are relative to socket number
614 		 */
615 		if (ppd->ppd_function > 0) {
616 			int sock;
617 			int func;
618 			sock = ppd->ppd_socket;
619 			func = ppd->ppd_function;
620 			(void) sprintf(name, "%x,%x", sock, func);
621 		} else {
622 			(void) sprintf(name, "%x", ppd->ppd_socket);
623 		}
624 		ddi_set_name_addr((dev_info_t *)arg, name);
625 
626 #if defined(PCMCIA_DEBUG)
627 		if (pcmcia_debug)
628 			cmn_err(CE_CONT, "pcmcia: system init done for %s [%s] "
629 			    "nodeid: %x @%s\n",
630 			    ddi_get_name(arg), ddi_get_name_addr(arg),
631 			    DEVI(arg)->devi_nodeid, name);
632 		if (pcmcia_debug > 1)
633 			pcmcia_dump_minors((dev_info_t *)arg);
634 #endif
635 
636 		return (DDI_SUCCESS);
637 
638 	case DDI_CTLOPS_UNINITCHILD:
639 
640 #if defined(PCMCIA_DEBUG)
641 		if (pcmcia_debug) {
642 			cmn_err(CE_CONT, "pcmcia: uninit child: %s(%d) @%p\n",
643 			    ddi_node_name(arg), ddi_get_instance(arg),
644 			    (void *)arg);
645 			if (DEVI(arg)->devi_binding_name != NULL)
646 				cmn_err(CE_CONT, "\tbinding_name=%s\n",
647 				    DEVI(arg)->devi_binding_name);
648 			if (DEVI(arg)->devi_node_name != NULL)
649 				cmn_err(CE_CONT, "\tnode_name=%s\n",
650 				    DEVI(arg)->devi_node_name);
651 		}
652 #endif
653 
654 		ddi_set_name_addr((dev_info_t *)arg, NULL);
655 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
656 		return (DDI_SUCCESS);
657 
658 	case DDI_CTLOPS_SLAVEONLY:
659 		/* PCMCIA devices can't ever be busmaster until CardBus */
660 		ppd = (struct pcmcia_parent_private *)
661 		    ddi_get_parent_data(rdip);
662 		if (ppd != NULL && ppd->ppd_flags & PPD_CB_BUSMASTER)
663 			return (DDI_FAILURE); /* at most */
664 		return (DDI_SUCCESS);
665 
666 	case DDI_CTLOPS_SIDDEV:
667 		/* in general this is true. */
668 		return (DDI_SUCCESS);
669 
670 	case DDI_CTLOPS_NREGS:
671 		ppd = (struct pcmcia_parent_private *)
672 		    ddi_get_parent_data(rdip);
673 		if (ppd != NULL)
674 			*((uint32_t *)result) = (ppd->ppd_nreg);
675 		else
676 			*((uint32_t *)result) = 0;
677 		return (DDI_SUCCESS);
678 
679 	case DDI_CTLOPS_REGSIZE:
680 		ppd = (struct pcmcia_parent_private *)
681 		    ddi_get_parent_data(rdip);
682 		if (ppd != NULL && ppd->ppd_nreg > 0)
683 			*((off_t *)result) =  sizeof (struct pcm_regs);
684 		else
685 			*((off_t *)result) = 0;
686 		return (DDI_SUCCESS);
687 
688 	case DDI_CTLOPS_POWER:
689 		ppd = (struct pcmcia_parent_private *)
690 		    ddi_get_parent_data(rdip);
691 
692 		if (ppd == NULL)
693 			return (DDI_FAILURE);
694 		/*
695 		 * if this is not present, don't bother (claim success)
696 		 * since it is already in the right state.  Don't
697 		 * do any resume either since the card insertion will
698 		 * happen independently.
699 		 */
700 		if (!ppd->ppd_active)
701 			return (DDI_SUCCESS);
702 		for (e = 0; e < pcmcia_num_adapters; e++)
703 			if (pcmcia_adapters[e] ==
704 			    pcmcia_sockets[ppd->ppd_socket]->ls_adapter)
705 				break;
706 		if (e == pcmcia_num_adapters)
707 			return (DDI_FAILURE);
708 		pm = (power_req_t *)arg;
709 #if defined(PCMCIA_DEBUG)
710 		if (pcmcia_debug) {
711 			cmn_err(CE_WARN, "power: %d: %p, %d, %d [%s]\n",
712 			    pm->request_type,
713 			    (void *)pm->req.set_power_req.who,
714 			    pm->req.set_power_req.cmpt,
715 			    pm->req.set_power_req.level,
716 			    ddi_get_name_addr(rdip));
717 		}
718 #endif
719 		e = ppd->ppd_socket;
720 		switch (pm->request_type) {
721 		case PMR_SUSPEND:
722 			if (!(pcmcia_sockets[e]->ls_flags &
723 			    PCS_SUSPENDED)) {
724 				pcmcia_do_suspend(ppd->ppd_socket,
725 				    pcmcia_sockets[e]);
726 			}
727 			ppd->ppd_flags |= PPD_SUSPENDED;
728 			return (DDI_SUCCESS);
729 		case PMR_RESUME:
730 			/* for now, we just succeed since the rest is done */
731 			return (DDI_SUCCESS);
732 		case PMR_SET_POWER:
733 			/*
734 			 * not sure how to handle power control
735 			 * for now, we let the child handle it itself
736 			 */
737 			(void) pcmcia_power(pm->req.set_power_req.who,
738 			    pm->req.set_power_req.cmpt,
739 			    pm->req.set_power_req.level);
740 			break;
741 		default:
742 			break;
743 		}
744 		return (DDI_FAILURE);
745 		/* These CTLOPS will need to be implemented for new form */
746 		/* let CardServices know about this */
747 	case DDI_CTLOPS_DETACH:
748 		return (DDI_SUCCESS);
749 	case DDI_CTLOPS_ATTACH:
750 		return (DDI_SUCCESS);
751 
752 	default:
753 		/* if we don't understand, pass up the tree */
754 		/* most things default to general ops */
755 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
756 	}
757 }
758 
759 struct pcmcia_props {
760 	char *name;
761 	int   len;
762 	int   prop;
763 } pcmcia_internal_props[] = {
764 	{ PCM_DEV_ACTIVE, 0, PCMCIA_PROP_ACTIVE },
765 	{ PCM_DEV_R2TYPE, 0, PCMCIA_PROP_R2TYPE },
766 	{ PCM_DEV_CARDBUS, 0, PCMCIA_PROP_CARDBUS },
767 	{ CS_PROP, sizeof (void *), PCMCIA_PROP_OLDCS },
768 	{ "reg", 0, PCMCIA_PROP_REG },
769 	{ "interrupts", sizeof (int), PCMCIA_PROP_INTR },
770 	{ "pm-hardware-state", 0, PCMCIA_PROP_DEFAULT_PM },
771 };
772 
773 /*
774  * pcmcia_prop_decode(name)
775  *	decode the name and determine if this is a property
776  *	we construct on the fly, one we have on the prop list
777  *	or one that requires calling the CIS code.
778  */
779 static int
780 pcmcia_prop_decode(char *name)
781 {
782 	int i;
783 	if (strncmp(name, "cistpl_", 7) == 0)
784 		return (PCMCIA_PROP_CIS);
785 
786 	for (i = 0; i < (sizeof (pcmcia_internal_props) /
787 	    sizeof (struct pcmcia_props)); i++) {
788 		if (strcmp(name, pcmcia_internal_props[i].name) == 0)
789 			return (i);
790 	}
791 
792 	return (PCMCIA_PROP_UNKNOWN);
793 }
794 
795 /*
796  * pcmcia_prop_op()
797  *	we don't have properties in PROM per se so look for them
798  *	only in the devinfo node.  Future may allow us to find
799  *	certain CIS tuples via this interface if a user asks for
800  *	a property of the form "cistpl-<tuplename>" but not yet.
801  *
802  *	The addition of 1275 properties adds to the necessity.
803  */
804 int
805 pcmcia_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
806     ddi_prop_op_t prop_op, int mod_flags,
807     char *name, caddr_t valuep, int *lengthp)
808 {
809 	int len, proplen, which, flags;
810 	caddr_t buff, propptr;
811 	struct pcmcia_parent_private *ppd;
812 
813 	len = *lengthp;
814 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(ch_dip);
815 
816 	switch (which = pcmcia_prop_decode(name)) {
817 	default:
818 		if (ppd == NULL)
819 			return (DDI_PROP_NOT_FOUND);
820 
821 		/* note that proplen may get modified */
822 		proplen = pcmcia_internal_props[which].len;
823 		switch (pcmcia_internal_props[which].prop) {
824 		case PCMCIA_PROP_DEFAULT_PM:
825 			propptr = pcmcia_default_pm_mode;
826 			proplen = strlen(propptr) + 1;
827 			break;
828 		case PCMCIA_PROP_OLDCS:
829 			propptr = (caddr_t)&cs_error_ptr;
830 			break;
831 		case PCMCIA_PROP_REG:
832 			propptr = (caddr_t)ppd->ppd_reg;
833 			proplen = ppd->ppd_nreg * sizeof (struct pcm_regs);
834 			break;
835 		case PCMCIA_PROP_INTR:
836 			propptr = (caddr_t)&ppd->ppd_intr;
837 			break;
838 
839 		/* the next set are boolean values */
840 		case PCMCIA_PROP_ACTIVE:
841 			propptr = NULL;
842 			if (!ppd->ppd_active) {
843 				return (DDI_PROP_NOT_FOUND);
844 			}
845 			break;
846 		case PCMCIA_PROP_R2TYPE:
847 			propptr = NULL;
848 			if (ppd->ppd_flags & PPD_CARD_CARDBUS)
849 				return (DDI_PROP_NOT_FOUND);
850 			break;
851 		case PCMCIA_PROP_CARDBUS:
852 			propptr = NULL;
853 			if (!(ppd->ppd_flags * PPD_CARD_CARDBUS))
854 				return (DDI_PROP_NOT_FOUND);
855 			break;
856 		}
857 
858 		break;
859 
860 	case PCMCIA_PROP_CIS:
861 		/*
862 		 * once we have the lookup code in place
863 		 * it is sufficient to break out of the switch
864 		 * once proplen and propptr are set.
865 		 * The common prop_op code deals with the rest.
866 		 */
867 	case PCMCIA_PROP_UNKNOWN:
868 		return (ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
869 		    mod_flags | DDI_PROP_NOTPROM,
870 		    name, valuep, lengthp));
871 	}
872 
873 	if (prop_op == PROP_LEN) {
874 		/* just the length */
875 		*lengthp = proplen;
876 		return (DDI_PROP_SUCCESS);
877 	}
878 	switch (prop_op) {
879 	case PROP_LEN_AND_VAL_ALLOC:
880 		if (mod_flags & DDI_PROP_CANSLEEP)
881 			flags = KM_SLEEP;
882 		else
883 			flags = KM_NOSLEEP;
884 		buff = kmem_alloc((size_t)proplen, flags);
885 		if (buff == NULL)
886 			return (DDI_PROP_NO_MEMORY);
887 		*(caddr_t *)valuep = (caddr_t)buff;
888 		break;
889 	case PROP_LEN_AND_VAL_BUF:
890 		buff = (caddr_t)valuep;
891 		if (len < proplen)
892 			return (DDI_PROP_BUF_TOO_SMALL);
893 		break;
894 	default:
895 		break;
896 	}
897 
898 	if (proplen > 0)
899 		bcopy(propptr, buff, proplen);
900 	*lengthp = proplen;
901 	return (DDI_PROP_SUCCESS);
902 }
903 
904 
905 struct regspec *
906 pcmcia_rnum_to_regspec(dev_info_t *dip, int rnumber)
907 {
908 	struct pcmcia_parent_private *ppd;
909 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
910 	if (ppd->ppd_nreg < rnumber)
911 		return (NULL);
912 	return ((struct regspec *)&ppd->ppd_reg[rnumber]);
913 }
914 
915 struct regspec *
916 pcmcia_rnum_to_mapped(dev_info_t *dip, int rnumber)
917 {
918 	struct pcmcia_parent_private *ppd;
919 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
920 	if (ppd->ppd_nreg < rnumber)
921 		return (NULL);
922 	if (ppd->ppd_assigned == NULL)
923 		return (NULL);
924 	if (ppd->ppd_assigned[rnumber].phys_len == 0)
925 		return (NULL);
926 	else
927 		return ((struct regspec *)&ppd->ppd_assigned[rnumber]);
928 }
929 
930 int
931 pcmcia_find_rnum(dev_info_t *dip, struct regspec *reg)
932 {
933 	struct pcmcia_parent_private *ppd;
934 	struct regspec *regp;
935 	int i;
936 
937 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
938 	if (ppd == NULL)
939 		return (-1);
940 	for (regp = (struct regspec *)ppd->ppd_reg, i = 0;
941 	    i < ppd->ppd_nreg; i++, regp++) {
942 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
943 			return (i);
944 	}
945 	for (regp = (struct regspec *)ppd->ppd_assigned, i = 0;
946 	    i < ppd->ppd_nreg; i++, regp++) {
947 		if (bcmp(reg, regp, sizeof (struct regspec)) == 0)
948 			return (i);
949 	}
950 
951 	return (-1);
952 }
953 
954 int
955 pcmcia_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
956 	off_t offset, off_t len, caddr_t *vaddrp)
957 {
958 	struct pcm_regs *regs, *mregs = NULL, tmp_reg;
959 	ddi_map_req_t mr = *mp;
960 	ra_return_t ret;
961 	int check, rnum = -1;
962 	uint32_t base;
963 	uchar_t regbuf[sizeof (pci_regspec_t)];
964 
965 	mp = &mr;		/* a copy of original request */
966 
967 	/* check for register number */
968 	switch (mp->map_type) {
969 	case DDI_MT_REGSPEC:
970 		regs = (struct pcm_regs *)mp->map_obj.rp;
971 		mregs = (struct pcm_regs *)mp->map_obj.rp;
972 		/*
973 		 * when using regspec, must not be relocatable
974 		 * and should be from assigned space.
975 		 */
976 		if (!PC_REG_RELOC(regs->phys_hi))
977 			return (DDI_FAILURE);
978 		rnum = pcmcia_find_rnum(rdip, (struct regspec *)mregs);
979 		break;
980 	case DDI_MT_RNUMBER:
981 		regs = (struct pcm_regs *)
982 		    pcmcia_rnum_to_regspec(rdip, mp->map_obj.rnumber);
983 		mregs = (struct pcm_regs *)
984 		    pcmcia_rnum_to_mapped(rdip, mp->map_obj.rnumber);
985 		rnum = mp->map_obj.rnumber;
986 		if (regs == NULL)
987 			return (DDI_FAILURE);
988 		mp->map_type = DDI_MT_REGSPEC;
989 		mp->map_obj.rp = (struct regspec *)mregs;
990 		break;
991 	default:
992 		return (DDI_ME_INVAL);
993 	}
994 
995 	/* basic sanity checks */
996 	switch (mp->map_op) {
997 	default:
998 		return (DDI_ME_UNIMPLEMENTED);
999 	case DDI_MO_UNMAP:
1000 		if (mregs == NULL)
1001 			return (DDI_FAILURE);
1002 		regs = mregs;
1003 		break;
1004 	case DDI_MO_MAP_LOCKED:
1005 	case DDI_MO_MAP_HANDLE:
1006 		panic("unsupported bus operation");
1007 		/*NOTREACHED*/
1008 	}
1009 
1010 	/*
1011 	 * we need a private copy for manipulation and
1012 	 * calculation of the correct ranges
1013 	 */
1014 	tmp_reg = *regs;
1015 	mp->map_obj.rp = (struct regspec *)(regs = &tmp_reg);
1016 	base = regs->phys_lo;
1017 	if (base == 0 && offset != 0) {
1018 		/*
1019 		 * for now this is an error.  What does it really mean
1020 		 * to ask for an offset from an address that hasn't
1021 		 * been allocated yet.
1022 		 */
1023 		return (DDI_ME_INVAL);
1024 	}
1025 	regs->phys_lo += (uint32_t)offset;
1026 	if (len != 0) {
1027 		if (len > regs->phys_len) {
1028 			return (DDI_ME_INVAL);
1029 		}
1030 		regs->phys_len = len;
1031 	}
1032 
1033 	/*
1034 	 * basic sanity is checked so now make sure
1035 	 * we can actually allocate something for this
1036 	 * request and then convert to a "standard"
1037 	 * regspec for the next layer up (pci/isa/rootnex/etc.)
1038 	 */
1039 
1040 	switch (PC_GET_REG_TYPE(regs->phys_hi)) {
1041 	case PC_REG_SPACE_IO:
1042 		check = PCA_RES_NEED_IO;
1043 		break;
1044 	case PC_REG_SPACE_MEMORY:
1045 		check = PCA_RES_NEED_MEM;
1046 		break;
1047 	default:
1048 		/* not a valid register type */
1049 		return (DDI_FAILURE);
1050 	}
1051 
1052 	mr.map_type = DDI_MT_REGSPEC;
1053 	ret.ra_addr_hi = 0;
1054 	ret.ra_addr_lo = regs->phys_lo;
1055 	ret.ra_len = regs->phys_len;
1056 	mr.map_obj.rp = pcmcia_cons_regspec(dip,
1057 	    (check == PCA_RES_NEED_IO) ?
1058 	    PCMCIA_MAP_IO : PCMCIA_MAP_MEM,
1059 	    regbuf, &ret);
1060 	switch (mp->map_op) {
1061 	case DDI_MO_UNMAP:
1062 		pcmcia_set_assigned(rdip, rnum, NULL);
1063 		break;
1064 	default:
1065 		break;
1066 	}
1067 	return (ddi_map(dip, &mr, (off_t)0, (off_t)0, vaddrp));
1068 }
1069 
1070 /*
1071  * pcmcia_cons_regspec()
1072  * based on parent's bus type, construct a regspec that is usable
1073  * by that parent to map the resource into the system.
1074  */
1075 #define	PTYPE_PCI	1
1076 #define	PTYPE_ISA	0
1077 struct regspec *
1078 pcmcia_cons_regspec(dev_info_t *dip, int type, uchar_t *buff, ra_return_t *ret)
1079 {
1080 	int ptype = -1, len, bus;
1081 	char device_type[MODMAXNAMELEN + 1];
1082 	dev_info_t *pdip;
1083 	struct regspec *defreg;
1084 	pci_regspec_t *pcireg;
1085 
1086 	pdip = ddi_get_parent(dip);
1087 	if (pdip != ddi_root_node()) {
1088 		/* we're not a child of root so find out what */
1089 		len = sizeof (device_type);
1090 		if (ddi_prop_op(DDI_DEV_T_ANY, pdip, PROP_LEN_AND_VAL_BUF, 0,
1091 		    "device_type", (caddr_t)device_type, &len) ==
1092 		    DDI_PROP_SUCCESS) {
1093 			/* check things out */
1094 			if (strcmp(device_type, "pci") == 0)
1095 				ptype = PTYPE_PCI;
1096 			else if (strcmp(device_type, "isa") == 0)
1097 				ptype = PTYPE_ISA;
1098 		}
1099 	}
1100 	switch (ptype) {
1101 	case PTYPE_PCI:
1102 		/* XXX need to look at carefully */
1103 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1104 		    "reg", (caddr_t)&pcireg, &len) == DDI_SUCCESS) {
1105 			bus = PCI_REG_BUS_G(pcireg->pci_phys_hi);
1106 			kmem_free(pcireg, len);
1107 		} else {
1108 			bus = 0;
1109 		}
1110 		pcireg = (pci_regspec_t *)buff;
1111 		pcireg->pci_phys_hi = (type == PCMCIA_MAP_IO ? PCI_ADDR_IO :
1112 		    PCI_ADDR_MEM32) | PCI_RELOCAT_B | (bus << 16);
1113 		pcireg->pci_phys_mid = ret->ra_addr_hi;
1114 		pcireg->pci_phys_low = ret->ra_addr_lo;
1115 		if (type == PCMCIA_MAP_IO)
1116 			pcireg->pci_phys_low &= 0xFFFF;
1117 		pcireg->pci_size_hi = 0;
1118 		pcireg->pci_size_low = ret->ra_len;
1119 		break;
1120 	default:
1121 		/* default case is to use struct regspec */
1122 		defreg = (struct regspec *)buff;
1123 		defreg->regspec_bustype = type == PCMCIA_MAP_IO ? 1 : 0;
1124 		defreg->regspec_addr = ret->ra_addr_lo;
1125 		defreg->regspec_size = ret->ra_len;
1126 		break;
1127 	}
1128 	return ((struct regspec *)buff);
1129 }
1130 
1131 /*
1132  * pcmcia_init_adapter
1133  *	Initialize the per-adapter structures and check to see if
1134  *	there are possible other instances coming.
1135  */
1136 void
1137 pcmcia_init_adapter(anp_t *adapter, dev_info_t *dip)
1138 {
1139 	int i, n;
1140 	pcmcia_if_t *ls_if;
1141 
1142 	i = pcmcia_num_adapters++;
1143 	pcmcia_adapters[i] = kmem_zalloc(sizeof (struct pcmcia_adapter),
1144 	    KM_SLEEP);
1145 	pcmcia_adapters[i]->pca_dip = dip;
1146 	/* should this be pca_winshift??? */
1147 	pcmcia_adapters[i]->pca_module = ddi_driver_major(dip);
1148 	pcmcia_adapters[i]->pca_unit = ddi_get_instance(dip);
1149 	pcmcia_adapters[i]->pca_iblock = adapter->an_iblock;
1150 	pcmcia_adapters[i]->pca_idev = adapter->an_idev;
1151 	pcmcia_adapters[i]->pca_if = ls_if = adapter->an_if;
1152 	pcmcia_adapters[i]->pca_number = i;
1153 	(void) strcpy(pcmcia_adapters[i]->pca_name, ddi_get_name(dip));
1154 	pcmcia_adapters[i]->
1155 	    pca_name[sizeof (pcmcia_adapters[i]->pca_name) - 1] = NULL;
1156 
1157 	if (ls_if != NULL) {
1158 		inquire_adapter_t conf;
1159 		int sock, win;
1160 
1161 		if (ls_if->pcif_inquire_adapter != NULL)
1162 			GET_CONFIG(ls_if, dip, &conf);
1163 
1164 		/* resources - assume worst case and fix from there */
1165 		pcmcia_adapters[i]->pca_flags = PCA_RES_NEED_IRQ |
1166 		    PCA_RES_NEED_IO | PCA_RES_NEED_MEM;
1167 		/* indicate first socket not initialized */
1168 		pcmcia_adapters[i]->pca_first_socket = -1;
1169 
1170 		if (conf.ResourceFlags & RES_OWN_IRQ)
1171 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IRQ;
1172 		if (conf.ResourceFlags & RES_OWN_IO)
1173 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_IO;
1174 		if (conf.ResourceFlags & RES_OWN_MEM)
1175 			pcmcia_adapters[i]->pca_flags &= ~PCA_RES_NEED_MEM;
1176 		if (conf.ResourceFlags & RES_IRQ_SHAREABLE)
1177 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SHAREABLE;
1178 		if (conf.ResourceFlags & RES_IRQ_NEXUS)
1179 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_SMI_SHARE;
1180 
1181 		/* need to know interrupt limitations */
1182 		if (conf.ActiveLow) {
1183 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveLow;
1184 			pcmcia_adapters[i]->pca_flags |= PCA_IRQ_ISA;
1185 		} else
1186 			pcmcia_adapters[i]->pca_avail_intr = conf.ActiveHigh;
1187 
1188 		/* power entries for adapter */
1189 		pcmcia_adapters[i]->pca_power = conf.power_entry;
1190 		pcmcia_adapters[i]->pca_numpower = conf.NumPower;
1191 
1192 		for (n = 0; n < conf.NumPower; n++)
1193 			pcmcia_merge_power(&conf.power_entry[n]);
1194 
1195 		/* now setup the per socket info */
1196 		for (sock = 0; sock < conf.NumSockets;
1197 		    sock++) {
1198 			dev_info_t *sockdrv = NULL;
1199 			sockdrv = pcmcia_number_socket(dip, sock);
1200 			if (sockdrv == NULL)
1201 				n = sock + pcmcia_num_sockets;
1202 			else {
1203 				n = ddi_get_instance(sockdrv);
1204 			}
1205 			/* make sure we know first socket on adapter */
1206 			if (pcmcia_adapters[i]->pca_first_socket == -1)
1207 				pcmcia_adapters[i]->pca_first_socket = n;
1208 
1209 			/*
1210 			 * the number of sockets is weird.
1211 			 * we might have only two sockets but
1212 			 * due to persistence of instances we
1213 			 * will need to call them something other
1214 			 * than 0 and 1.  So, we use the largest
1215 			 * instance number as the number and
1216 			 * have some that just don't get used.
1217 			 */
1218 			if (n >= pcmcia_num_sockets)
1219 				pcmcia_num_sockets = n + 1;
1220 #if defined(PCMCIA_DEBUG)
1221 			if (pcmcia_debug) {
1222 				cmn_err(CE_CONT,
1223 				    "pcmcia_init: new socket added %d "
1224 				    "(%d)\n",
1225 				    n, pcmcia_num_sockets);
1226 			}
1227 #endif
1228 
1229 			pcmcia_sockets[n] =
1230 			    kmem_zalloc(sizeof (pcmcia_logical_socket_t),
1231 			    KM_SLEEP);
1232 			pcmcia_sockets[n]->ls_socket = sock;
1233 			pcmcia_sockets[n]->ls_if = ls_if;
1234 			pcmcia_sockets[n]->ls_adapter =
1235 			    pcmcia_adapters[i];
1236 			pcmcia_sockets[n]->ls_cs_events = 0L;
1237 			pcmcia_sockets[n]->ls_sockdrv = sockdrv;
1238 			/* Prototype of intrspec */
1239 			pcmcia_sockets[n]->ls_intr_pri = adapter->an_ipl;
1240 #if defined(PCMCIA_DEBUG)
1241 			if (pcmcia_debug)
1242 				cmn_err(CE_CONT,
1243 				    "phys sock %d, log sock %d\n",
1244 				    sock, n);
1245 #endif
1246 			mutex_init(&pcmcia_sockets[n]->ls_ilock, NULL,
1247 			    MUTEX_DRIVER, *adapter->an_iblock);
1248 		}
1249 
1250 		pcmcia_adapters[i]->pca_numsockets = conf.NumSockets;
1251 		/* now setup the per window information */
1252 		for (win = 0; win < conf.NumWindows; win++) {
1253 			n = win + pcmcia_num_windows;
1254 			pcmcia_windows[n] =
1255 			    kmem_zalloc(sizeof (pcmcia_logical_window_t),
1256 			    KM_SLEEP);
1257 			pcmcia_windows[n]->lw_window = win;
1258 			pcmcia_windows[n]->lw_if = ls_if;
1259 			pcmcia_windows[n]->lw_adapter =
1260 			    pcmcia_adapters[i];
1261 		}
1262 		pcmcia_num_windows += conf.NumWindows;
1263 		SET_CALLBACK(ls_if, dip,
1264 		    pcm_adapter_callback, i);
1265 
1266 		/* now tell CS about each socket */
1267 		for (sock = 0; sock < pcmcia_num_sockets; sock++) {
1268 #if defined(PCMCIA_DEBUG)
1269 			if (pcmcia_debug) {
1270 				cmn_err(CE_CONT,
1271 				    "pcmcia_init: notify CS socket %d "
1272 				    "sockp=%p\n",
1273 				    sock, (void *)pcmcia_sockets[sock]);
1274 			}
1275 #endif
1276 			if (pcmcia_sockets[sock] == NULL ||
1277 			    (pcmcia_sockets[sock]->ls_flags &
1278 			    PCS_SOCKET_ADDED)) {
1279 				/* skip the ones that are done already */
1280 				continue;
1281 			}
1282 			pcmcia_sockets[sock]->ls_flags |= PCS_SOCKET_ADDED;
1283 			if (cs_event(PCE_ADD_SOCKET, sock, 0) !=
1284 			    CS_SUCCESS) {
1285 				/* flag socket as broken */
1286 				pcmcia_sockets[sock]->ls_flags = 0;
1287 			} else {
1288 				pcm_event_manager(PCE_ADD_SOCKET,
1289 				    sock, NULL);
1290 			}
1291 		}
1292 
1293 	}
1294 #if defined(PCMCIA_DEBUG)
1295 	if (pcmcia_debug) {
1296 		cmn_err(CE_CONT, "logical sockets:\n");
1297 		for (i = 0; i < pcmcia_num_sockets; i++) {
1298 			if (pcmcia_sockets[i] == NULL)
1299 				continue;
1300 			cmn_err(CE_CONT,
1301 			    "\t%d: phys sock=%d, if=%p, adapt=%p\n",
1302 			    i, pcmcia_sockets[i]->ls_socket,
1303 			    (void *)pcmcia_sockets[i]->ls_if,
1304 			    (void *)pcmcia_sockets[i]->ls_adapter);
1305 		}
1306 		cmn_err(CE_CONT, "logical windows:\n");
1307 		for (i = 0; i < pcmcia_num_windows; i++) {
1308 			cmn_err(CE_CONT,
1309 			    "\t%d: phys_window=%d, if=%p, adapt=%p\n",
1310 			    i, pcmcia_windows[i]->lw_window,
1311 			    (void *)pcmcia_windows[i]->lw_if,
1312 			    (void *)pcmcia_windows[i]->lw_adapter);
1313 		}
1314 		cmn_err(CE_CONT, "\tpcmcia_num_power=%d\n", pcmcia_num_power);
1315 		for (n = 0; n < pcmcia_num_power; n++)
1316 			cmn_err(CE_CONT,
1317 			    "\t\tPowerLevel: %d\tValidSignals: %x\n",
1318 			    pcmcia_power_table[n].PowerLevel,
1319 			    pcmcia_power_table[n].ValidSignals);
1320 	}
1321 #endif
1322 }
1323 
1324 /*
1325  * pcmcia_find_cards()
1326  *	check the adapter to see if there are cards present at
1327  *	driver attach time.  If there are, generate an artificial
1328  *	card insertion event to get CS running and the PC Card ultimately
1329  *	identified.
1330  */
1331 void
1332 pcmcia_find_cards(anp_t *adapt)
1333 {
1334 	int i;
1335 	get_ss_status_t status;
1336 	for (i = 0; i < pcmcia_num_sockets; i++) {
1337 		if (pcmcia_sockets[i] &&
1338 		    pcmcia_sockets[i]->ls_if == adapt->an_if) {
1339 			/* check the status */
1340 			status.socket = i;
1341 			if (SSGetStatus(&status) == SUCCESS &&
1342 			    status.IFType != IF_CARDBUS &&
1343 			    status.CardState & SBM_CD &&
1344 			    pcmcia_sockets[i]->ls_dip[0] == NULL) {
1345 				(void) cs_event(PCE_CARD_INSERT, i, 0);
1346 				delay(1);
1347 			}
1348 		}
1349 	}
1350 }
1351 
1352 /*
1353  * pcmcia_number_socket(dip, adapt)
1354  *	we determine socket number by creating a driver for each
1355  *	socket on the adapter and then forcing it to attach.  This
1356  *	results in an instance being assigned which becomes the
1357  *	logical socket number.	If it fails, then we are the first
1358  *	set of sockets and renumbering occurs later.  We do this
1359  *	one socket at a time and return the dev_info_t so the
1360  *	instance number can be used.
1361  */
1362 dev_info_t *
1363 pcmcia_number_socket(dev_info_t *dip, int localsocket)
1364 {
1365 	dev_info_t *child = NULL;
1366 	struct pcmcia_parent_private *ppd;
1367 
1368 	if (ndi_devi_alloc(dip, "pcs", (pnode_t)DEVI_SID_NODEID,
1369 	    &child) == NDI_SUCCESS) {
1370 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
1371 		    KM_SLEEP);
1372 		ppd->ppd_reg = kmem_zalloc(sizeof (struct pcm_regs), KM_SLEEP);
1373 		ppd->ppd_nreg = 1;
1374 		ppd->ppd_reg[0].phys_hi = localsocket;
1375 		ddi_set_parent_data(child, (caddr_t)ppd);
1376 		if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
1377 			kmem_free(ppd->ppd_reg, sizeof (struct pcm_regs));
1378 			kmem_free(ppd, sizeof (struct pcmcia_parent_private));
1379 			(void) ndi_devi_free(child);
1380 			child = NULL;
1381 		}
1382 	}
1383 	return (child);
1384 }
1385 
1386 /*
1387  * pcm_phys_to_log_socket()
1388  *	from an adapter and socket number return the logical socket
1389  */
1390 int
1391 pcm_phys_to_log_socket(struct pcmcia_adapter *adapt, int socket)
1392 {
1393 	register pcmcia_logical_socket_t *sockp;
1394 	int i;
1395 
1396 	for (i = 0, sockp = pcmcia_sockets[0];
1397 	    i < pcmcia_num_sockets; i++, sockp = pcmcia_sockets[i]) {
1398 		if (sockp == NULL)
1399 			continue;
1400 		if (sockp->ls_socket == socket && sockp->ls_adapter == adapt)
1401 			break;
1402 	}
1403 	if (i >= pcmcia_num_sockets) {
1404 #if defined(PCMCIA_DEBUG)
1405 		if (pcmcia_debug)
1406 			cmn_err(CE_CONT,
1407 			    "\tbad socket/adapter: %x/%p != %x/%x\n",
1408 			    socket, (void *)adapt, pcmcia_num_sockets,
1409 			    pcmcia_num_adapters);
1410 #endif
1411 		return (-1);
1412 	}
1413 
1414 	return (i);		/* want logical socket */
1415 }
1416 
1417 /*
1418  * pcm_adapter_callback()
1419  *	this function is called back by the adapter driver at interrupt time.
1420  *	It is here that events should get generated for the event manager if it
1421  *	is present.  It would also be the time where a device information
1422  *	tree could be constructed for a card that was added in if we
1423  *	choose to create them dynamically.
1424  */
1425 
1426 #if defined(PCMCIA_DEBUG)
1427 char *cblist[] = {
1428 	"removal",
1429 	"insert",
1430 	"ready",
1431 	"battery-warn",
1432 	"battery-dead",
1433 	"status-change",
1434 	"write-protect", "reset", "unlock", "client-info", "eject-complete",
1435 	"eject-request", "erase-complete", "exclusive-complete",
1436 	"exclusive-request", "insert-complete", "insert-request",
1437 	"reset-complete", "reset-request", "timer-expired",
1438 	"resume", "suspend"
1439 };
1440 #endif
1441 
1442 /*ARGSUSED*/
1443 static int
1444 pcm_adapter_callback(dev_info_t *dip, int adapter, int event, int socket)
1445 {
1446 	pcmcia_logical_socket_t *sockp;
1447 
1448 #if defined(PCMCIA_DEBUG)
1449 	if (pcmcia_debug) {
1450 		cmn_err(CE_CONT, "pcm_adapter_callback: %p %x %x %x: ",
1451 		    (void *)dip, adapter, event, socket);
1452 		cmn_err(CE_CONT, "[%s]\n", cblist[event]);
1453 	}
1454 #endif
1455 
1456 	if (adapter >= pcmcia_num_adapters || adapter < 0) {
1457 #if defined(PCMCIA_DEBUG)
1458 		if (pcmcia_debug)
1459 			cmn_err(CE_CONT, "\tbad adapter number: %d : %d\n",
1460 			    adapter, pcmcia_num_adapters);
1461 #endif
1462 		return (1);
1463 	}
1464 
1465 	/* get the logical socket since that is what CS knows */
1466 	socket = pcm_phys_to_log_socket(pcmcia_adapters[adapter], socket);
1467 	if (socket == -1) {
1468 		cmn_err(CE_WARN, "pcmcia callback - bad logical socket\n");
1469 		return (0);
1470 	}
1471 	sockp = pcmcia_sockets[socket];
1472 	switch (event) {
1473 	case -1:		/* special case of adapter going away */
1474 	case PCE_CARD_INSERT:
1475 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
1476 		    PCE_E2M(PCE_CARD_REMOVAL);
1477 		break;
1478 	case PCE_CARD_REMOVAL:
1479 				/* disable interrupts at this point */
1480 		sockp->ls_cs_events |= PCE_E2M(PCE_CARD_INSERT) |
1481 		    PCE_E2M(PCE_CARD_REMOVAL);
1482 		/* remove children that never attached */
1483 
1484 		break;
1485 	case PCE_PM_RESUME:
1486 		pcmcia_do_resume(socket, sockp);
1487 		/* event = PCE_CARD_INSERT; */
1488 		break;
1489 	case PCE_PM_SUSPEND:
1490 		pcmcia_do_suspend(socket, sockp);
1491 		/* event = PCE_CARD_REMOVAL; */
1492 		break;
1493 	default:
1494 		/* nothing to do */
1495 		break;
1496 	}
1497 
1498 #if defined(PCMCIA_DEBUG)
1499 	if (pcmcia_debug) {
1500 		cmn_err(CE_CONT,
1501 		    "\tevent %d, event mask=%x, match=%x (log socket=%d)\n",
1502 		    event,
1503 		    (int)sockp->ls_cs_events,
1504 		    (int)(sockp->ls_cs_events & PCE_E2M(event)), socket);
1505 	}
1506 #endif
1507 
1508 	if (pcmcia_cs_event && sockp->ls_cs_events & (1 << event)) {
1509 #if defined(PCMCIA_DEBUG)
1510 		if (pcmcia_debug)
1511 			cmn_err(CE_CONT, "\tcalling CS event handler (%p) "
1512 			    "with event=%d\n",
1513 			    (void *)pcmcia_cs_event, event);
1514 #endif
1515 		CS_EVENT(event, socket, 0);
1516 	}
1517 
1518 	/* let the event manager(s) know about the event */
1519 	pcm_event_manager(event, socket, NULL);
1520 
1521 	return (0);
1522 }
1523 
1524 /*
1525  * pcm_event_manager()
1526  *	checks for registered management driver callback handlers
1527  *	if there are any, call them if the event warrants it
1528  */
1529 void
1530 pcm_event_manager(int event, int socket, void *arg)
1531 {
1532 	struct pcmcia_mif *mif;
1533 
1534 	for (mif = pcmcia_mif_handlers; mif != NULL; mif = mif->mif_next) {
1535 #if defined(PCMCIA_DEBUG)
1536 		if (pcmcia_debug)
1537 			cmn_err(CE_CONT,
1538 			    "pcm_event_manager: event=%d, mif_events=%x"
1539 			    " (tst:%d)\n",
1540 			    event, (int)*(uint32_t *)mif->mif_events,
1541 			    PR_GET(mif->mif_events, event));
1542 #endif
1543 		if (PR_GET(mif->mif_events, event)) {
1544 			mif->mif_function(mif->mif_id, event, socket, arg);
1545 		}
1546 	}
1547 
1548 }
1549 
1550 /*
1551  * pcm_search_devinfo(dev_info_t *, pcm_device_info *, int)
1552  * search for an immediate child node to the nexus and not siblings of nexus
1553  * and not grandchildren.  We follow the same sequence that name binding
1554  * follows so we match same class of device (modem == modem) and don't
1555  * have to depend on features that might not exist.
1556  */
1557 dev_info_t *
1558 pcm_search_devinfo(dev_info_t *self, struct pcm_device_info *info, int socket)
1559 {
1560 	char bf[256];
1561 	struct pcmcia_parent_private *ppd;
1562 	dev_info_t *dip;
1563 	int circ;
1564 
1565 #if defined(PCMCIA_DEBUG)
1566 	if (pcmcia_debug)
1567 		cmn_err(CE_CONT,
1568 		    "pcm_search_devinfo: socket=%x [%s|%s|%s] pd_flags=%x\n",
1569 		    socket, info->pd_bind_name, info->pd_generic_name,
1570 		    info->pd_vers1_name, info->pd_flags);
1571 #endif
1572 
1573 	ndi_devi_enter(self, &circ);
1574 	/* do searches in compatible property order */
1575 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
1576 	    dip != NULL;
1577 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
1578 		int ppd_socket;
1579 		ppd = (struct pcmcia_parent_private *)
1580 		    ddi_get_parent_data(dip);
1581 		if (ppd == NULL) {
1582 #if defined(PCMCIA_DEBUG)
1583 			cmn_err(CE_WARN, "No parent private data\n");
1584 #endif
1585 			continue;
1586 		}
1587 		ppd_socket = CS_MAKE_SOCKET_NUMBER(ppd->ppd_socket,
1588 		    ppd->ppd_function);
1589 #if defined(PCMCIA_DEBUG)
1590 		if (pcmcia_debug) {
1591 			cmn_err(CE_CONT, "\tbind=[%s], node=[%s]\n",
1592 			    DEVI(dip)->devi_binding_name,
1593 			    DEVI(dip)->devi_node_name);
1594 		}
1595 #endif
1596 		if (info->pd_flags & PCM_NAME_VERS1) {
1597 			(void) strcpy(bf, info->pd_vers1_name);
1598 			pcmcia_fix_string(bf);
1599 			if (DEVI(dip)->devi_binding_name &&
1600 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
1601 			    socket == ppd_socket)
1602 				break;
1603 		}
1604 		if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
1605 		    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
1606 			(void) sprintf(bf, "%s,%x", info->pd_bind_name,
1607 			    info->pd_function);
1608 			if (strcmp(bf, DEVI(dip)->devi_binding_name) == 0 &&
1609 			    socket == ppd->ppd_socket)
1610 				break;
1611 		}
1612 		if (info->pd_flags & PCM_NAME_1275) {
1613 			if (DEVI(dip)->devi_binding_name &&
1614 			    strcmp(DEVI(dip)->devi_binding_name,
1615 			    info->pd_bind_name) == 0 &&
1616 			    socket == ppd_socket)
1617 				break;
1618 		}
1619 		if (info->pd_flags & PCM_NAME_GENERIC) {
1620 			(void) sprintf(bf, "%s,%s", PCMDEV_NAMEPREF,
1621 			    info->pd_generic_name);
1622 			if (DEVI(dip)->devi_binding_name &&
1623 			    strcmp(DEVI(dip)->devi_binding_name, bf) == 0 &&
1624 			    socket == ppd_socket)
1625 				break;
1626 		}
1627 		if (info->pd_flags & PCM_NAME_GENERIC) {
1628 			if (DEVI(dip)->devi_binding_name &&
1629 			    strcmp(DEVI(dip)->devi_binding_name,
1630 			    info->pd_generic_name) == 0 &&
1631 			    socket == ppd_socket)
1632 				break;
1633 		}
1634 		if (info->pd_flags & PCM_NO_CONFIG) {
1635 			if (DEVI(dip)->devi_binding_name &&
1636 			    strcmp(DEVI(dip)->devi_binding_name,
1637 			    "pccard,memory") == 0 &&
1638 			    socket == ppd_socket)
1639 				break;
1640 		}
1641 	}
1642 	ndi_devi_exit(self, circ);
1643 	return (dip);
1644 }
1645 
1646 /*
1647  * pcm_find_devinfo()
1648  *	this is a wrapper around DDI calls to "find" any
1649  *	devinfo node and then from there find the one associated
1650  *	with the socket
1651  */
1652 dev_info_t *
1653 pcm_find_devinfo(dev_info_t *pdip, struct pcm_device_info *info, int socket)
1654 {
1655 	dev_info_t *dip;
1656 
1657 	dip = pcm_search_devinfo(pdip, info, socket);
1658 	if (dip == NULL)
1659 		return (NULL);
1660 	/*
1661 	 * we have at least a base level dip
1662 	 * see if there is one (this or a sibling)
1663 	 * that has the correct socket number
1664 	 * if there is, return that one else
1665 	 * NULL so a new one is created
1666 	 */
1667 #if defined(PCMCIA_DEBUG)
1668 	if (pcmcia_debug)
1669 		cmn_err(CE_CONT, "find: initial dip = %p, socket=%d, name=%s "
1670 		    "(instance=%d, socket=%d, name=%s)\n",
1671 		    (void *)dip, socket, info->pd_bind_name,
1672 		    ddi_get_instance(dip),
1673 		    ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1674 		    PCM_DEV_SOCKET, -1),
1675 		    ddi_get_name(dip));
1676 #endif
1677 
1678 #if defined(PCMCIA_DEBUG)
1679 	if (pcmcia_debug && dip != NULL)
1680 		cmn_err(CE_CONT, "\treturning non-NULL dip (%s)\n",
1681 		    ddi_get_name(dip));
1682 #endif
1683 	return (dip);
1684 }
1685 
1686 /*
1687  * pcm_find_parent_dip(socket)
1688  *	find the correct parent dip for this logical socket
1689  */
1690 dev_info_t *
1691 pcm_find_parent_dip(int socket)
1692 {
1693 	if ((socket < 0 || socket >= pcmcia_num_sockets) ||
1694 	    pcmcia_sockets[socket] == NULL)
1695 		return (NULL);
1696 	return (pcmcia_sockets[socket]->ls_adapter->pca_dip);
1697 }
1698 
1699 /*
1700  * pcmcia_set_em_handler()
1701  *	This is called by the management and event driver to tell
1702  *	the nexus what to call.	 Multiple drivers are allowed
1703  *	but normally only one will exist.
1704  */
1705 int
1706 pcmcia_set_em_handler(int (*handler)(), caddr_t events, int elen,
1707 			uint32_t id, void **cs, void **ss)
1708 {
1709 	struct pcmcia_mif *mif, *tmp;
1710 
1711 	if (handler == NULL) {
1712 		/* NULL means remove the handler based on the ID */
1713 		if (pcmcia_mif_handlers == NULL)
1714 			return (0);
1715 		mutex_enter(&pcmcia_global_lock);
1716 		if (pcmcia_mif_handlers->mif_id == id) {
1717 			mif = pcmcia_mif_handlers;
1718 			pcmcia_mif_handlers = mif->mif_next;
1719 			kmem_free(mif, sizeof (struct pcmcia_mif));
1720 		} else {
1721 			for (mif = pcmcia_mif_handlers;
1722 			    mif->mif_next != NULL &&
1723 			    mif->mif_next->mif_id != id;
1724 			    mif = mif->mif_next)
1725 				;
1726 			if (mif->mif_next != NULL &&
1727 			    mif->mif_next->mif_id == id) {
1728 				tmp = mif->mif_next;
1729 				mif->mif_next = tmp->mif_next;
1730 				kmem_free(tmp, sizeof (struct pcmcia_mif));
1731 			}
1732 		}
1733 		mutex_exit(&pcmcia_global_lock);
1734 	} else {
1735 
1736 		if (pcmcia_num_adapters == 0) {
1737 			return (ENXIO);
1738 		}
1739 		if (elen > EM_EVENTSIZE)
1740 			return (EINVAL);
1741 
1742 		mif = (struct pcmcia_mif *)
1743 		    kmem_zalloc(sizeof (struct pcmcia_mif), KM_NOSLEEP);
1744 		if (mif == NULL)
1745 			return (ENOSPC);
1746 
1747 		mif->mif_function = (void (*)())handler;
1748 		bcopy(events, mif->mif_events, elen);
1749 		mif->mif_id = id;
1750 		mutex_enter(&pcmcia_global_lock);
1751 		mif->mif_next = pcmcia_mif_handlers;
1752 		pcmcia_mif_handlers = mif;
1753 		if (cs != NULL)
1754 			*cs = (void *)pcmcia_card_services;
1755 		if (ss != NULL) {
1756 			*ss = (void *)SocketServices;
1757 		}
1758 
1759 		mutex_exit(&pcmcia_global_lock);
1760 	}
1761 	return (0);
1762 }
1763 
1764 /*
1765  * pcm_fix_bits(uchar_t *data, int num, int dir)
1766  *	shift socket bits left(0) or right(0)
1767  *	This is used when mapping logical and physical
1768  */
1769 void
1770 pcm_fix_bits(socket_enum_t src, socket_enum_t dst, int num, int dir)
1771 {
1772 	int i;
1773 
1774 	PR_ZERO(dst);
1775 
1776 	if (dir == 0) {
1777 				/* LEFT */
1778 		for (i = 0; i <= (sizeof (dst) * PR_WORDSIZE) - num; i++) {
1779 			if (PR_GET(src, i))
1780 				PR_SET(dst, i + num);
1781 		}
1782 	} else {
1783 				/* RIGHT */
1784 		for (i = num; i < sizeof (dst) * PR_WORDSIZE; i++) {
1785 			if (PR_GET(src, i))
1786 				PR_SET(dst, i - num);
1787 		}
1788 	}
1789 }
1790 
1791 uint32_t
1792 genmask(int len)
1793 {
1794 	uint32_t mask;
1795 	for (mask = 0; len > 0; len--) {
1796 		mask |= 1 << (len - 1);
1797 	}
1798 	return (mask);
1799 }
1800 
1801 int
1802 genp2(int val)
1803 {
1804 	int i;
1805 	if (val == 0)
1806 		return (0);
1807 	for (i = 0; i < 32; i++)
1808 		if (val > (1 << i))
1809 			return (i);
1810 	return (0);
1811 }
1812 
1813 #if defined(PCMCIA_DEBUG)
1814 char *ssfuncs[128] = {
1815 	"GetAdapter", "GetPage", "GetSocket", "GetStatus", "GetWindow",
1816 	"InquireAdapter", "InquireSocket", "InquireWindow", "ResetSocket",
1817 	"SetPage", "SetAdapter", "SetSocket", "SetWindow", "SetIRQHandler",
1818 	"ClearIRQHandler",
1819 	/* 15 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1820 	/* 25 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1821 	/* 35 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1822 	/* 45 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1823 	/* 55 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1824 	/* 65 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1825 	/* 75 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1826 	/* 85 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1827 	/* 95 */ NULL, NULL, NULL,
1828 	"CSIsActiveDip",
1829 	"CSInitDev", "CSRegister", "CSCISInit", "CSUnregister",
1830 	"CISGetAddress", "CISSetAddress", "CSCardRemoved", "CSGetCookiesAndDip"
1831 };
1832 #endif
1833 
1834 /*
1835  * SocketServices
1836  *	general entrypoint for Card Services to find
1837  *	Socket Services.  Finding the entry requires
1838  *	a _depends_on[] relationship.
1839  *
1840  *	In some cases, the work is done locally but usually
1841  *	the parameters are adjusted and the adapter driver
1842  *	code asked to do the work.
1843  */
1844 int
1845 SocketServices(int function, ...)
1846 {
1847 	va_list arglist;
1848 	uint32_t args[16];
1849 	csregister_t *reg;
1850 	sservice_t *serv;
1851 	dev_info_t *dip;
1852 	int socket, func;
1853 	int error = SUCCESS;
1854 	pcmcia_logical_socket_t *sockp;
1855 
1856 	va_start(arglist, function);
1857 
1858 #if defined(PCMCIA_DEBUG)
1859 	if (pcmcia_debug > 1)
1860 		cmn_err(CE_CONT, "SocketServices called for function %d [%s]\n",
1861 		    function,
1862 		    ((function < 128) && ssfuncs[function] != NULL) ?
1863 		    ssfuncs[function] : "UNKNOWN");
1864 #endif
1865 	switch (function) {
1866 	case CSRegister:
1867 	case CISGetAddress:
1868 	case CISSetAddress:
1869 
1870 		reg = va_arg(arglist, csregister_t *);
1871 
1872 		if (reg->cs_magic != PCCS_MAGIC ||
1873 		    reg->cs_version != PCCS_VERSION) {
1874 			cmn_err(CE_WARN,
1875 			    "pcmcia: CSRegister (%x, %x, %p, %p) *ERROR*",
1876 			    reg->cs_magic, reg->cs_version,
1877 			    (void *)reg->cs_card_services,
1878 			    (void *)reg->cs_event);
1879 			error = BAD_FUNCTION;
1880 			break;
1881 		}
1882 
1883 		switch (function) {
1884 		case CISGetAddress:
1885 			reg->cs_event = pcmcia_cis_parser;
1886 			break;
1887 		case CISSetAddress:
1888 			pcmcia_cis_parser = reg->cs_event;
1889 			break;
1890 		case CSRegister:
1891 			break;
1892 		}
1893 		break;
1894 
1895 	case CSUnregister:
1896 		break;
1897 
1898 	case CSCISInit:
1899 		args[0] = va_arg(arglist, int);
1900 #if defined(PCMCIA_DEBUG)
1901 		if (pcmcia_debug)
1902 			cmn_err(CE_CONT,
1903 			    "CSCISInit: CIS is initialized on socket %d\n",
1904 			    (int)args[0]);
1905 #endif
1906 		/*
1907 		 * now that the CIS has been parsed (there may not
1908 		 * be one but the work is done) we can create the
1909 		 * device information structures.
1910 		 *
1911 		 * we serialize the node creation to avoid problems
1912 		 * with initial probe/attach of nexi.
1913 		 */
1914 
1915 		mutex_enter(&pcmcia_global_lock);
1916 		pcmcia_create_dev_info(args[0]);
1917 		cv_broadcast(&pcmcia_condvar); /* wakeup the nexus attach */
1918 		mutex_exit(&pcmcia_global_lock);
1919 		break;
1920 
1921 	case CSInitDev:
1922 #if defined(PCMCIA_DEBUG)
1923 		if (pcmcia_debug)
1924 			cmn_err(CE_CONT, "CSInitDev: initialize device\n");
1925 #endif
1926 		/*
1927 		 * this is where we create the /devices entries
1928 		 * that let us out into the world
1929 		 */
1930 
1931 		(void) pcmcia_create_device(va_arg(arglist,
1932 		    ss_make_device_node_t *));
1933 		break;
1934 
1935 	case CSCardRemoved:
1936 		args[0] = va_arg(arglist, uint32_t);
1937 		socket = CS_GET_SOCKET_NUMBER(args[0]);
1938 		func = CS_GET_FUNCTION_NUMBER(args[0]);
1939 #if defined(PCMCIA_DEBUG)
1940 		if (pcmcia_debug)
1941 			cmn_err(CE_CONT,
1942 			    "CSCardRemoved! (socket=%d)\n", (int)args[0]);
1943 #endif
1944 		if (socket >= pcmcia_num_sockets)
1945 			break;
1946 
1947 		sockp = pcmcia_sockets[socket];
1948 		if (sockp == NULL) {
1949 			cmn_err(CE_WARN,
1950 			    "pcmcia: bad socket = %x", socket);
1951 			break;
1952 		}
1953 
1954 		if (!(sockp->ls_flags & PCS_SUSPENDED)) {
1955 			for (func = 0; func < sockp->ls_functions; func++) {
1956 				/*
1957 				 * break the association of dip and socket
1958 				 * for all functions on that socket
1959 				 */
1960 				dip = sockp->ls_dip[func];
1961 				sockp->ls_dip[func] = NULL;
1962 				if (dip != NULL) {
1963 					struct pcmcia_parent_private *ppd;
1964 					ppd = (struct pcmcia_parent_private *)
1965 					    ddi_get_parent_data(dip);
1966 					ppd->ppd_active = 0;
1967 					(void) ndi_devi_offline(dip,
1968 					    NDI_DEVI_REMOVE);
1969 
1970 					pcmcia_ppd_free(ppd);
1971 				}
1972 #if defined(PCMCIA_DEBUG)
1973 				else {
1974 					if (pcmcia_debug)
1975 						cmn_err(CE_CONT,
1976 						    "CardRemoved: no "
1977 						    "dip present "
1978 						    "on socket %d!\n",
1979 						    (int)args[0]);
1980 				}
1981 #endif
1982 			}
1983 		} else {
1984 			mutex_enter(&pcmcia_global_lock);
1985 			sockp->ls_flags &= ~PCS_SUSPENDED;
1986 			cv_broadcast(&pcmcia_condvar);
1987 			mutex_exit(&pcmcia_global_lock);
1988 		}
1989 		break;
1990 
1991 	case CSGetCookiesAndDip:
1992 		serv = va_arg(arglist, sservice_t *);
1993 		if (serv != NULL)
1994 			error = GetCookiesAndDip(serv);
1995 		else
1996 			error = BAD_SOCKET;
1997 		break;
1998 
1999 	case CSGetActiveDip:
2000 		/*
2001 		 * get the dip associated with the card currently
2002 		 * in the specified socket
2003 		 */
2004 		args[0] = va_arg(arglist, uint32_t);
2005 		socket = CS_GET_SOCKET_NUMBER(args[0]);
2006 		func = CS_GET_FUNCTION_NUMBER(args[0]);
2007 		error = (long)pcmcia_sockets[socket]->ls_dip[func];
2008 		break;
2009 
2010 		/*
2011 		 * the remaining entries are SocketServices calls
2012 		 */
2013 	case SS_GetAdapter:
2014 		error = SSGetAdapter(va_arg(arglist, get_adapter_t *));
2015 		break;
2016 	case SS_GetPage:
2017 		error = SSGetPage(va_arg(arglist, get_page_t *));
2018 		break;
2019 	case SS_GetSocket:
2020 		error = SSGetSocket(va_arg(arglist, get_socket_t *));
2021 		break;
2022 	case SS_GetStatus:
2023 		error = SSGetStatus(va_arg(arglist, get_ss_status_t *));
2024 		break;
2025 	case SS_GetWindow:
2026 		error = SSGetWindow(va_arg(arglist, get_window_t *));
2027 		break;
2028 	case SS_InquireAdapter:
2029 		error = SSInquireAdapter(va_arg(arglist, inquire_adapter_t *));
2030 		break;
2031 	case SS_InquireSocket:
2032 		error = SSInquireSocket(va_arg(arglist, inquire_socket_t *));
2033 		break;
2034 	case SS_InquireWindow:
2035 		error = SSInquireWindow(va_arg(arglist, inquire_window_t *));
2036 		break;
2037 	case SS_ResetSocket:
2038 		args[0] = va_arg(arglist, uint32_t);
2039 		args[1] = va_arg(arglist, int);
2040 		error = SSResetSocket(args[0], args[1]);
2041 		break;
2042 	case SS_SetPage:
2043 		error = SSSetPage(va_arg(arglist, set_page_t *));
2044 		break;
2045 	case SS_SetSocket:
2046 		error = SSSetSocket(va_arg(arglist, set_socket_t *));
2047 		break;
2048 	case SS_SetWindow:
2049 		error = SSSetWindow(va_arg(arglist, set_window_t *));
2050 		break;
2051 	case SS_SetIRQHandler:
2052 		error = SSSetIRQHandler(va_arg(arglist, set_irq_handler_t *));
2053 		break;
2054 	case SS_ClearIRQHandler:
2055 		error = SSClearIRQHandler(va_arg(arglist,
2056 		    clear_irq_handler_t *));
2057 		break;
2058 	default:
2059 		error = BAD_FUNCTION;
2060 		break;
2061 	}
2062 	va_end(arglist);
2063 	return (error);
2064 }
2065 
2066 /*
2067  * pcmcia_merge_power()
2068  *	The adapters may have different power tables so it
2069  *	is necessary to construct a single power table that
2070  *	can be used throughout the system.  The result is
2071  *	a merger of all capabilities.  The nexus adds
2072  *	power table entries one at a time.
2073  */
2074 void
2075 pcmcia_merge_power(struct power_entry *power)
2076 {
2077 	int i;
2078 	struct power_entry pwr;
2079 
2080 	pwr = *power;
2081 
2082 	for (i = 0; i < pcmcia_num_power; i++) {
2083 		if (pwr.PowerLevel == pcmcia_power_table[i].PowerLevel) {
2084 			if (pwr.ValidSignals ==
2085 			    pcmcia_power_table[i].ValidSignals) {
2086 				return;
2087 			} else {
2088 				/* partial match */
2089 				pwr.ValidSignals &=
2090 				    ~pcmcia_power_table[i].ValidSignals;
2091 			}
2092 		}
2093 	}
2094 	/* what's left becomes a new entry */
2095 	if (pcmcia_num_power == PCMCIA_MAX_POWER)
2096 		return;
2097 	pcmcia_power_table[pcmcia_num_power++] = pwr;
2098 }
2099 
2100 /*
2101  * pcmcia_do_suspend()
2102  *	tell CS that a suspend has happened by passing a
2103  *	card removal event.  Then cleanup the socket state
2104  *	to fake the cards being removed so resume works
2105  */
2106 void
2107 pcmcia_do_suspend(int socket, pcmcia_logical_socket_t *sockp)
2108 {
2109 	get_ss_status_t stat;
2110 	struct pcmcia_adapter *adapt;
2111 	pcmcia_if_t *ls_if;
2112 	dev_info_t *dip;
2113 	int i;
2114 
2115 #ifdef	XXX
2116 	if (pcmcia_cs_event == NULL) {
2117 		return;
2118 	}
2119 #endif
2120 
2121 	ls_if = sockp->ls_if;
2122 	adapt = sockp->ls_adapter;
2123 
2124 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
2125 		return;
2126 	}
2127 
2128 	stat.socket = socket;
2129 #if defined(PCMCIA_DEBUG)
2130 	if (pcmcia_debug) {
2131 		cmn_err(CE_CONT,
2132 		    "pcmcia_do_suspend(%d, %p)\n", socket, (void *)sockp);
2133 	}
2134 #endif
2135 
2136 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) != SUCCESS)
2137 		return;
2138 
2139 	/*
2140 	 * If there is a card in the socket, then we need to send
2141 	 *	everyone a PCE_CARD_REMOVAL event, and remove the
2142 	 *	card active property.
2143 	 */
2144 
2145 	for (i = 0; i < sockp->ls_functions; i++) {
2146 		struct pcmcia_parent_private *ppd;
2147 		dip = sockp->ls_dip[i];
2148 		if (dip != NULL) {
2149 			ppd = (struct pcmcia_parent_private *)
2150 			    ddi_get_parent_data(dip);
2151 			ppd->ppd_flags |= PPD_SUSPENDED;
2152 		}
2153 #if 0
2154 		sockp->ls_dip[i] = NULL;
2155 #endif
2156 	}
2157 	sockp->ls_flags |= PCS_SUSPENDED;
2158 
2159 	if (pcmcia_cs_event &&
2160 	    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
2161 		CS_EVENT(PCE_PM_SUSPEND, socket, 0);
2162 	}
2163 	pcm_event_manager(PCE_PM_SUSPEND, socket, NULL);
2164 }
2165 
2166 /*
2167  * pcmcia_do_resume()
2168  *	tell CS that a suspend has happened by passing a
2169  *	card removal event.  Then cleanup the socket state
2170  *	to fake the cards being removed so resume works
2171  */
2172 void
2173 pcmcia_do_resume(int socket, pcmcia_logical_socket_t *sockp)
2174 {
2175 	get_ss_status_t stat;
2176 	struct pcmcia_adapter *adapt;
2177 	pcmcia_if_t *ls_if;
2178 
2179 #ifdef	XXX
2180 	if (pcmcia_cs_event == NULL) {
2181 		return;
2182 	}
2183 #endif
2184 
2185 	ls_if = sockp->ls_if;
2186 	adapt = sockp->ls_adapter;
2187 
2188 	if (ls_if == NULL || ls_if->pcif_get_status == NULL) {
2189 		return;
2190 	}
2191 
2192 	stat.socket = socket;
2193 #if defined(PCMCIA_DEBUG)
2194 	if (pcmcia_debug) {
2195 		cmn_err(CE_CONT,
2196 		    "pcmcia_do_resume(%d, %p)\n", socket, (void *)sockp);
2197 	}
2198 #endif
2199 	if (GET_STATUS(ls_if, adapt->pca_dip, &stat) ==
2200 	    SUCCESS) {
2201 
2202 #if defined(PCMCIA_DEBUG)
2203 		if (pcmcia_debug)
2204 			cmn_err(CE_CONT, "\tsocket=%x, CardState=%x\n",
2205 			    socket, stat.CardState);
2206 #endif
2207 #if 0
2208 		/* now have socket info -- do we have events? */
2209 		if ((stat.CardState & SBM_CD) == SBM_CD) {
2210 			if (pcmcia_cs_event &&
2211 			    (sockp->ls_cs_events & (1 << PCE_CARD_INSERT))) {
2212 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
2213 			}
2214 
2215 			/* we should have card removed from CS soon */
2216 			pcm_event_manager(PCE_CARD_INSERT, socket, NULL);
2217 		}
2218 #else
2219 		if (pcmcia_cs_event &&
2220 		    (sockp->ls_cs_events & (1 << PCE_PM_SUSPEND))) {
2221 			CS_EVENT(PCE_PM_RESUME, socket, 0);
2222 			CS_EVENT(PCE_CARD_REMOVAL, socket, 0);
2223 			if ((stat.CardState & SBM_CD) == SBM_CD)
2224 				CS_EVENT(PCE_CARD_INSERT, socket, 0);
2225 		}
2226 #endif
2227 	}
2228 }
2229 
2230 /*
2231  * pcmcia_map_power_set()
2232  *	Given a power table entry and level, find it in the
2233  *	master table and return the index in the adapter table.
2234  */
2235 static int
2236 pcmcia_map_power_set(struct pcmcia_adapter *adapt, int level, int which)
2237 {
2238 	int plevel, i;
2239 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
2240 	plevel = pcmcia_power_table[level].PowerLevel;
2241 	/* mask = pcmcia_power_table[level].ValidSignals; */
2242 	for (i = 0; i < adapt->pca_numpower; i++)
2243 		if (plevel == pwr[i].PowerLevel &&
2244 		    pwr[i].ValidSignals & which)
2245 			return (i);
2246 	return (0);
2247 }
2248 
2249 /*
2250  * pcmcia_map_power_get()
2251  *	Given an adapter power entry, find the appropriate index
2252  *	in the master table.
2253  */
2254 static int
2255 pcmcia_map_power_get(struct pcmcia_adapter *adapt, int level, int which)
2256 {
2257 	int plevel, i;
2258 	struct power_entry *pwr = (struct power_entry *)adapt->pca_power;
2259 	plevel = pwr[level].PowerLevel;
2260 	/* mask = pwr[level].ValidSignals; */
2261 	for (i = 0; i < pcmcia_num_power; i++)
2262 		if (plevel == pcmcia_power_table[i].PowerLevel &&
2263 		    pcmcia_power_table[i].ValidSignals & which)
2264 			return (i);
2265 	return (0);
2266 }
2267 
2268 /*
2269  * XXX - SS really needs a way to allow the caller to express
2270  *	interest in PCE_CARD_STATUS_CHANGE events.
2271  */
2272 static uint32_t
2273 pcm_event_map[32] = {
2274 	PCE_E2M(PCE_CARD_WRITE_PROTECT)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2275 	PCE_E2M(PCE_CARD_UNLOCK)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2276 	PCE_E2M(PCE_EJECTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2277 	PCE_E2M(PCE_INSERTION_REQUEST)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2278 	PCE_E2M(PCE_CARD_BATTERY_WARN)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2279 	PCE_E2M(PCE_CARD_BATTERY_DEAD)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2280 	PCE_E2M(PCE_CARD_READY)|PCE_E2M(PCE_CARD_STATUS_CHANGE),
2281 	PCE_E2M(PCE_CARD_REMOVAL)|PCE_E2M(PCE_CARD_INSERT)|
2282 					PCE_E2M(PCE_CARD_STATUS_CHANGE),
2283 	PCE_E2M(PCE_PM_SUSPEND)|PCE_E2M(PCE_PM_RESUME),
2284 };
2285 
2286 static int
2287 pcm_mapevents(uint32_t eventmask)
2288 {
2289 	uint32_t mask;
2290 	int i;
2291 
2292 	for (i = 0, mask = 0; eventmask && i < 32; i++) {
2293 		if (eventmask & (1 << i)) {
2294 			mask |= pcm_event_map[i];
2295 			eventmask &= ~(1 << i);
2296 		}
2297 	}
2298 	return (mask);
2299 }
2300 
2301 
2302 /*
2303  * PCMCIA Generic Naming Support
2304  *
2305  * With 2.6, PCMCIA naming moves to the 1275 and generic naming model.
2306  * Consequently, the whole naming mechanism is to be changed.  This is
2307  * not backward compatible with the current names but that isn't a problem
2308  * due to so few drivers existing.
2309  *
2310  * For cards with a device_id tuple, a generic name will be used.
2311  * if there is no device_id, then the 1275 name will be used if possible.
2312  * The 1275 name is of the form pccardNNNN,MMMM from the manfid tuple.
2313  * if there is not manfid tuple, an attempt will be made to bind the
2314  * node to the version_1 strings.
2315  *
2316  * In all cases, a "compatible" property is created with a number
2317  * of names.  The most generic name will be last in the list.
2318  */
2319 
2320 /*
2321  * pcmcia_fix_string()
2322  * want to avoid special characters in alias strings so convert
2323  * to something innocuous
2324  */
2325 
2326 void
2327 pcmcia_fix_string(char *str)
2328 {
2329 	for (; str && *str; str++) {
2330 		switch (*str) {
2331 			case ' ':
2332 			case '\t':
2333 				*str = '_';
2334 				break;
2335 		}
2336 	}
2337 }
2338 
2339 void
2340 pcmcia_1275_name(int socket, struct pcm_device_info *info,
2341 			client_handle_t handle)
2342 {
2343 	cistpl_manfid_t manfid;
2344 	cistpl_jedec_t jedec;
2345 	tuple_t tuple;
2346 	int i;
2347 
2348 	tuple.Socket = socket;
2349 
2350 	/* get MANFID if it exists -- this is most important form */
2351 	tuple.DesiredTuple = CISTPL_MANFID;
2352 	tuple.Attributes = 0;
2353 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2354 	    SUCCESS) {
2355 		i = csx_Parse_CISTPL_MANFID(handle, &tuple,
2356 		    &manfid);
2357 		if (i == SUCCESS) {
2358 			(void) sprintf(info->pd_bind_name, "%s%x,%x",
2359 			    PCMDEV_NAMEPREF,
2360 			    manfid.manf, manfid.card);
2361 			info->pd_flags |= PCM_NAME_1275;
2362 		}
2363 	} else {
2364 		tuple.Attributes = 0;
2365 		tuple.DesiredTuple = CISTPL_JEDEC_A;
2366 		if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2367 		    SUCCESS) {
2368 			i = csx_Parse_CISTPL_JEDEC_A(handle, &tuple,
2369 			    &jedec);
2370 			if (i == SUCCESS) {
2371 				(void) sprintf(info->pd_bind_name, "%s%x,%x",
2372 				    PCMDEV_NAMEPREF,
2373 				    jedec.jid[0].id, jedec.jid[0].info);
2374 				info->pd_flags |= PCM_NAME_1275;
2375 			}
2376 		}
2377 	}
2378 }
2379 
2380 void
2381 pcmcia_vers1_name(int socket, struct pcm_device_info *info,
2382 			client_handle_t handle)
2383 {
2384 	cistpl_vers_1_t vers1;
2385 	tuple_t tuple;
2386 	int which = 0;
2387 	int i, len, space;
2388 
2389 	tuple.Socket = socket;
2390 	info->pd_vers1_name[0] = '\0';
2391 
2392 	/* Version 1 strings */
2393 	tuple.DesiredTuple = CISTPL_VERS_1;
2394 	tuple.Attributes = 0;
2395 	if (!which &&
2396 	    (i = csx_GetFirstTuple(handle, &tuple)) == SUCCESS) {
2397 		i = csx_Parse_CISTPL_VERS_1(handle, &tuple, &vers1);
2398 		if (i == SUCCESS) {
2399 			for (i = 0, len = 0, space = 0; i < vers1.ns; i++) {
2400 			    if ((space + len + strlen(info->pd_vers1_name)) >=
2401 				sizeof (info->pd_vers1_name))
2402 				    break;
2403 			    if (space) {
2404 				    info->pd_vers1_name[len++] = ',';
2405 			    }
2406 			    (void) strcpy(info->pd_vers1_name + len,
2407 				(char *)vers1.pi[i]);
2408 			    len += strlen((char *)vers1.pi[i]);
2409 			    /* strip trailing spaces off of string */
2410 			    while (info->pd_vers1_name[len - 1] == ' ' &&
2411 				    len > 0)
2412 				    len--;
2413 			    space = 1;
2414 			}
2415 			info->pd_vers1_name[len] = '\0';
2416 			info->pd_flags |= PCM_NAME_VERS1;
2417 		}
2418 	}
2419 }
2420 
2421 
2422 int
2423 pcmcia_get_funce(client_handle_t handle, tuple_t *tuple)
2424 {
2425 	int ret = 0;
2426 
2427 	tuple->Attributes = 0;
2428 	while (csx_GetNextTuple(handle, tuple) == SUCCESS) {
2429 		if (tuple->TupleCode == CISTPL_FUNCID) {
2430 			break;
2431 		}
2432 		if (tuple->TupleCode == CISTPL_FUNCE) {
2433 			ret = 1;
2434 			break;
2435 		}
2436 		tuple->Attributes = 0;
2437 	}
2438 	return (ret);
2439 }
2440 
2441 char *pcmcia_lan_types[] = {
2442 	"arcnet",
2443 	"ethernet",
2444 	"token-ring",
2445 	"localtalk",
2446 	"fddi",
2447 	"atm",
2448 	"wireless",
2449 	"reserved"
2450 };
2451 
2452 void
2453 pcmcia_generic_name(int socket, struct pcm_device_info *info,
2454 			client_handle_t handle)
2455 {
2456 	cistpl_funcid_t funcid;
2457 	cistpl_funce_t funce;
2458 	tuple_t tuple;
2459 	int which = 0;
2460 	int i;
2461 
2462 	tuple.Socket = socket;
2463 
2464 	tuple.DesiredTuple = CISTPL_FUNCID;
2465 	tuple.Attributes = 0;
2466 	if ((i = csx_GetFirstTuple(handle, &tuple)) ==
2467 	    SUCCESS) {
2468 		/*
2469 		 * need to make sure that CISTPL_FUNCID is not
2470 		 * present in both a global and local CIS for MF
2471 		 * cards.  3COM seems to do this erroneously
2472 		 */
2473 
2474 		if (info->pd_flags & PCM_MULTI_FUNCTION &&
2475 		    tuple.Flags & CISTPLF_GLOBAL_CIS) {
2476 			tuple_t ltuple;
2477 			ltuple = tuple;
2478 			ltuple.DesiredTuple = CISTPL_FUNCID;
2479 			ltuple.Attributes = 0;
2480 			if ((i = csx_GetNextTuple(handle, &ltuple)) ==
2481 			    SUCCESS) {
2482 				/* this is the per-function funcid */
2483 				tuple = ltuple;
2484 			}
2485 		}
2486 
2487 		i = csx_Parse_CISTPL_FUNCID(handle, &tuple, &funcid);
2488 		if (i == SUCCESS) {
2489 			/* in case no function extension */
2490 			if (funcid.function < PCM_GENNAME_SIZE)
2491 				(void) strcpy(info->pd_generic_name,
2492 				    pcmcia_generic_names[funcid.function]);
2493 			else
2494 				(void) sprintf(info->pd_generic_name,
2495 				    "class,%x",
2496 				    funcid.function);
2497 		}
2498 		info->pd_type = funcid.function;
2499 		switch (funcid.function) {
2500 		case TPLFUNC_LAN:
2501 			which = pcmcia_get_funce(handle, &tuple);
2502 			if (which) {
2503 				i = csx_Parse_CISTPL_FUNCE(handle,
2504 				    &tuple,
2505 				    &funce, TPLFUNC_LAN);
2506 				if (i == SUCCESS) {
2507 					i = funce.data.lan.tech;
2508 					if (i >= sizeof (pcmcia_lan_types) /
2509 					    sizeof (char *)) {
2510 						break;
2511 					}
2512 					(void) strcpy(info->pd_generic_name,
2513 					    pcmcia_lan_types[i]);
2514 				}
2515 			}
2516 			break;
2517 		case TPLFUNC_VIDEO:
2518 #ifdef future_pcmcia_spec
2519 			which = pcmcia_get_funce(handle, &tuple);
2520 			if (which) {
2521 				i = csx_Parse_CISTPL_FUNCE(handle,
2522 				    &tuple,
2523 				    &funce, TPLFUNC_VIDEO);
2524 				if (i == SUCCESS) {
2525 					i = funce.video.tech;
2526 					if (i > sizeof (pcmcia_lan_types) /
2527 					    sizeof (char *)) {
2528 						break;
2529 					}
2530 					(void) strcpy(info->pd_generic_names,
2531 					    pcmcia_lan_types[i]);
2532 				}
2533 			}
2534 #endif
2535 			break;
2536 		}
2537 		info->pd_flags |= PCM_NAME_GENERIC;
2538 	} else {
2539 		/* if no FUNCID, do we have CONFIG */
2540 		tuple.DesiredTuple = CISTPL_CONFIG;
2541 		tuple.Attributes = 0;
2542 		if (csx_GetFirstTuple(handle, &tuple) != SUCCESS) {
2543 			info->pd_flags |= PCM_NO_CONFIG | PCM_NAME_GENERIC;
2544 			(void) strcpy(info->pd_generic_name,
2545 			    pcmcia_generic_names[PCM_TYPE_MEMORY]);
2546 			info->pd_type = PCM_TYPE_MEMORY;
2547 		}
2548 	}
2549 }
2550 
2551 
2552 /*
2553  * pcmcia_add_compatible()
2554  * add the cached compatible property list.
2555  */
2556 void
2557 pcmcia_add_compatible(dev_info_t *dip, struct pcm_device_info *info)
2558 {
2559 	int length = 0, i;
2560 	char buff[MAXNAMELEN];
2561 	char *compat_name[8];
2562 	int ci = 0;
2563 
2564 	bzero(compat_name, sizeof (compat_name));
2565 
2566 	if (info->pd_flags & PCM_NAME_VERS1) {
2567 		(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
2568 		    info->pd_vers1_name);
2569 		pcmcia_fix_string(buff); /* don't want spaces */
2570 		length = strlen(buff) + 1;
2571 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2572 		(void) strcpy(compat_name[ci++], buff);
2573 	}
2574 
2575 	if ((info->pd_flags & (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) ==
2576 	    (PCM_NAME_1275 | PCM_MULTI_FUNCTION)) {
2577 		(void) sprintf(buff, "%s,%x", info->pd_bind_name,
2578 		    info->pd_function);
2579 		length = strlen(buff) + 1;
2580 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2581 		(void) strcpy(compat_name[ci++], buff);
2582 	}
2583 
2584 	if (info->pd_flags & PCM_NAME_1275) {
2585 		length = strlen(info->pd_bind_name) + 1;
2586 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2587 		(void) strcpy(compat_name[ci++], info->pd_bind_name);
2588 	}
2589 
2590 	if (info->pd_flags & PCM_NAME_GENERIC) {
2591 		if (strncmp(info->pd_generic_name, "class,", 6) == 0) {
2592 			/* no generic without "pccard" */
2593 			(void) sprintf(buff, "%s%s", PCMDEV_NAMEPREF,
2594 			    info->pd_generic_name);
2595 		} else {
2596 			/* first pccard,generic-name */
2597 			(void) sprintf(buff, "%s,%s", PCMDEV_NAMEPREF,
2598 			    info->pd_generic_name);
2599 		}
2600 		length = strlen(buff) + 1;
2601 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2602 		(void) strcpy(compat_name[ci++], buff);
2603 
2604 		/* now the simple generic name */
2605 		length = strlen(info->pd_generic_name) + 1;
2606 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2607 		(void) strcpy(compat_name[ci++], info->pd_generic_name);
2608 	}
2609 
2610 	if (info->pd_flags & PCM_NO_CONFIG) {
2611 		char *mem = "pccard,memory";
2612 		/*
2613 		 * I/O cards are required to have a config tuple.
2614 		 * there are some that violate the spec and don't
2615 		 * but it is most likely that this is a memory card
2616 		 * so tag it as such.  "memory" is more general
2617 		 * than other things so needs to come last.
2618 		 */
2619 		length = strlen(mem) + 1;
2620 		compat_name[ci] = kmem_alloc(length, KM_SLEEP);
2621 		(void) strcpy(compat_name[ci++], mem);
2622 	}
2623 
2624 	if (ci == 0)
2625 		return;
2626 
2627 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
2628 	    "compatible", (char **)compat_name, ci) != DDI_PROP_SUCCESS)
2629 		cmn_err(CE_WARN, "pcmcia: unable to create compatible prop");
2630 
2631 	for (i = 0; i < ci; i++)
2632 		kmem_free(compat_name[i], strlen(compat_name[i]) + 1);
2633 }
2634 /*
2635  * CIS parsing and other PC Card specific code
2636  */
2637 
2638 /*
2639  * pcmcia_get_mem_regs()
2640  */
2641 static int
2642 pcmcia_get_mem_regs(struct pcm_regs *regs, struct pcm_device_info *info,
2643 			int type, int pctype)
2644 {
2645 	int num_regs = 0;
2646 	tuple_t tuple;
2647 	cistpl_device_t device;
2648 	uint32_t curr_base;
2649 	int ret, len;
2650 	int space;
2651 
2652 	/*
2653 	 * current plan for reg spec:
2654 	 * device_a will be accumulated to determine max size of
2655 	 * attribute memory.  device for common.  Then config
2656 	 * tuples to get a worst case I/O size.
2657 	 */
2658 	bzero(&tuple, sizeof (tuple));
2659 	tuple.Socket = info->pd_socket;
2660 
2661 	tuple.DesiredTuple = (cisdata_t)type;
2662 
2663 	space = (type == CISTPL_DEVICE_A) ? PC_REG_SPACE_ATTRIBUTE :
2664 	    PC_REG_SPACE_MEMORY;
2665 	if ((ret = csx_GetFirstTuple(info->pd_handle, &tuple)) == CS_SUCCESS) {
2666 		bzero(&device, sizeof (device));
2667 
2668 		if (type == CISTPL_DEVICE)
2669 			ret = csx_Parse_CISTPL_DEVICE(info->pd_handle, &tuple,
2670 			    &device);
2671 		else
2672 			ret = csx_Parse_CISTPL_DEVICE_A(info->pd_handle, &tuple,
2673 			    &device);
2674 
2675 		if (ret == CS_SUCCESS) {
2676 		    curr_base = 0;
2677 		    for (ret = 0; ret < device.num_devices;
2678 			ret++) {
2679 			    /* need to order these for real mem first */
2680 			    if (device.devnode[ret].type !=
2681 				CISTPL_DEVICE_DTYPE_NULL) {
2682 				    /* how to represent types??? */
2683 				    regs[num_regs].phys_hi =
2684 					PC_REG_PHYS_HI(0, 0,
2685 					    pctype,
2686 					    space,
2687 					    info->pd_socket,
2688 					    info->pd_function,
2689 					    0);
2690 				    regs[num_regs].phys_lo = curr_base;
2691 				    len = device.devnode[ret].size_in_bytes;
2692 				    curr_base += len;
2693 				    regs[num_regs].phys_len = len;
2694 				    num_regs++;
2695 			    } else {
2696 				/*
2697 				 * NULL device is a "hole"
2698 				 */
2699 				    curr_base +=
2700 					    device.devnode[ret].size_in_bytes;
2701 			    }
2702 			}
2703 	    }
2704 	}
2705 	return (num_regs);
2706 }
2707 
2708 /*
2709  *
2710  */
2711 static int
2712 pcmcia_get_io_regs(struct pcm_regs *regs, struct pcm_device_info *info,
2713 		    int pctype)
2714 {
2715 	int num_regs = 0;
2716 	tuple_t tuple;
2717 	uint32_t curr_base;
2718 	int len, curr, i, curr_len;
2719 	cistpl_config_t config;
2720 	cistpl_cftable_entry_t cftable;
2721 	struct pcm_regs tmp[16];
2722 	int found = 0;
2723 
2724 	bzero(&tuple, sizeof (tuple));
2725 	tuple.DesiredTuple = CISTPL_CONFIG;
2726 	tuple.Socket = info->pd_socket;
2727 	tuple.Attributes = 0;
2728 	curr_base = 0;
2729 	len = 0;
2730 
2731 	if (csx_GetFirstTuple(info->pd_handle, &tuple) == CS_SUCCESS) {
2732 	    if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
2733 					&tuple, &config) != CS_SUCCESS) {
2734 		info->pd_flags |= PCM_NO_CONFIG; /* must be memory */
2735 		return (0);
2736 	}
2737 	curr = 0;
2738 
2739 	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
2740 	tuple.Socket = info->pd_socket;
2741 	tuple.Attributes = 0;
2742 	bzero(tmp, sizeof (tmp));
2743 	while (csx_GetNextTuple(info->pd_handle, &tuple) == CS_SUCCESS) {
2744 	    bzero(&cftable, sizeof (cftable));
2745 	    if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
2746 						&tuple, &cftable) ==
2747 		CS_SUCCESS) {
2748 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IO) {
2749 		    /* we have an I/O entry */
2750 		    if (cftable.io.flags &
2751 			CISTPL_CFTABLE_TPCE_FS_IO_RANGE) {
2752 			len = cftable.io.addr_lines;
2753 			if (len != 0)
2754 				len = 1 << len;
2755 			for (i = 0; i < cftable.io.ranges && curr < 16; i++) {
2756 			    curr_base = cftable.io.range[i].addr;
2757 			    curr_len = cftable.io.range[i].length;
2758 			    if (curr_len == 0)
2759 				    curr_len = len;
2760 			    if (len != 0 || cftable.io.addr_lines == 0) {
2761 				/* we have potential relocation */
2762 				int mask;
2763 				mask = cftable.io.addr_lines ?
2764 				    cftable.io.addr_lines : genp2(len);
2765 				mask = genmask(mask);
2766 				if ((mask & curr_base) == 0) {
2767 					/* more accurate length */
2768 					regs->phys_len = curr_len;
2769 					regs->phys_lo = 0;
2770 					regs->phys_hi =
2771 					    PC_REG_PHYS_HI(0,
2772 					    0,
2773 					    pctype,
2774 					    PC_REG_SPACE_IO,
2775 					    info->pd_socket,
2776 					    info->pd_function,
2777 					    0);
2778 					num_regs++;
2779 					found = 2;
2780 					break;
2781 				}
2782 			    }
2783 			    tmp[curr].phys_len = curr_len;
2784 			    tmp[curr].phys_lo = curr_base;
2785 			    curr++;
2786 			    found = 1;
2787 			}
2788 			if (found == 2)
2789 				break;
2790 		    } else {
2791 			/* no I/O range so just a mask */
2792 			regs->phys_len = 1 << cftable.io.addr_lines;
2793 			regs->phys_hi =
2794 			    PC_REG_PHYS_HI(0,
2795 			    0,
2796 			    pctype,
2797 			    PC_REG_SPACE_IO,
2798 			    info->pd_socket,
2799 			    info->pd_function,
2800 			    0);
2801 			regs->phys_lo = 0;
2802 			num_regs++;
2803 			regs++;
2804 			/* quit on "good" entry */
2805 			break;
2806 		    }
2807 		    /* was this the last CFTABLE Entry? */
2808 		    if (config.last == cftable.index)
2809 			    break;
2810 		}
2811 	    }
2812 	}
2813 	if (found == 1) {
2814 		/*
2815 		 * have some non-relocatable values
2816 		 * so we include them all for now
2817 		 */
2818 		for (i = 0; i < curr && num_regs < 8; i++) {
2819 		    regs->phys_len = tmp[i].phys_len;
2820 		    regs->phys_lo = tmp[i].phys_lo;
2821 		    regs->phys_hi = PC_REG_PHYS_HI(1, 0, pctype,
2822 			    PC_REG_SPACE_IO, info->pd_socket,
2823 			    info->pd_function, 0);
2824 		    regs++;
2825 		    num_regs++;
2826 		}
2827 	    }
2828 	}
2829 	return (num_regs);
2830 }
2831 
2832 /*
2833  * pcmcia_create_regs()
2834  *	create a valid set of regspecs for the card
2835  *	The first one is always for CIS access and naming
2836  */
2837 /*ARGSUSED*/
2838 static void
2839 pcmcia_find_regs(dev_info_t *dip, struct pcm_device_info *info,
2840 			struct pcmcia_parent_private *ppd)
2841 {
2842 	struct pcm_regs regs[32]; /* assume worst case */
2843 	int num_regs = 0;
2844 	int len;
2845 	int bustype;
2846 
2847 	if (ppd->ppd_flags & PPD_CARD_CARDBUS) {
2848 		/* always have a CIS map */
2849 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_CARDBUS,
2850 		    PC_REG_SPACE_CONFIG,
2851 		    info->pd_socket,
2852 		    info->pd_function, 0);
2853 		bustype = PC_REG_TYPE_CARDBUS;
2854 	} else {
2855 		/* always have a CIS map */
2856 		regs[0].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
2857 		    PC_REG_SPACE_ATTRIBUTE,
2858 		    info->pd_socket,
2859 		    info->pd_function, 0);
2860 		bustype = PC_REG_TYPE_16BIT;
2861 	}
2862 	regs[0].phys_lo = 0;	/* always starts at zero */
2863 	regs[0].phys_len = 0;
2864 	num_regs++;
2865 	/*
2866 	 * need to search CIS for other memory instances
2867 	 */
2868 
2869 	if (info->pd_flags & PCM_OTHER_NOCIS) {
2870 		/* special case of memory only card without CIS */
2871 		regs[1].phys_hi = PC_REG_PHYS_HI(0, 0, PC_REG_TYPE_16BIT,
2872 		    PC_REG_SPACE_MEMORY,
2873 		    info->pd_socket,
2874 		    info->pd_function, 0);
2875 		regs[1].phys_lo = 0;
2876 		regs[1].phys_len = PCM_MAX_R2_MEM;
2877 		num_regs++;
2878 	} else {
2879 		/*
2880 		 * want to get any other memory and/or I/O regions
2881 		 * on the card and represent them here.
2882 		 */
2883 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
2884 		    CISTPL_DEVICE_A, bustype);
2885 		num_regs += pcmcia_get_mem_regs(&regs[num_regs], info,
2886 		    CISTPL_DEVICE, bustype);
2887 
2888 		/* now look for an I/O space to configure */
2889 		num_regs += pcmcia_get_io_regs(&regs[num_regs], info,
2890 		    bustype);
2891 
2892 	}
2893 
2894 	len = num_regs * sizeof (uint32_t) * 3;
2895 	ppd->ppd_nreg = num_regs;
2896 	ppd->ppd_reg = kmem_alloc(len, KM_SLEEP);
2897 	bcopy(regs, ppd->ppd_reg, len);
2898 	len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
2899 	ppd->ppd_assigned = kmem_zalloc(len, KM_SLEEP);
2900 }
2901 
2902 
2903 /*
2904  * pcmcia_need_intr()
2905  *	check to see if an interrupt tuple exists.
2906  *	existence means we need one in the intrspec.
2907  */
2908 static int
2909 pcmcia_need_intr(int socket, struct pcm_device_info *info)
2910 {
2911 	cistpl_config_t config;
2912 	cistpl_cftable_entry_t cftable;
2913 	tuple_t tuple;
2914 	int i;
2915 
2916 	bzero(&tuple, sizeof (tuple));
2917 	tuple.DesiredTuple = CISTPL_CONFIG;
2918 	tuple.Socket = socket;
2919 	tuple.Attributes = 0;
2920 	if (csx_GetFirstTuple(info->pd_handle, &tuple) != CS_SUCCESS) {
2921 		return (0);
2922 	}
2923 #if defined(PCMCIA_DEBUG)
2924 	if (pcmcia_debug) {
2925 		cmn_err(CE_CONT, "pcmcia_need_intr: have config tuple\n");
2926 	}
2927 #endif
2928 	bzero(&config, sizeof (config));
2929 	if (csx_Parse_CISTPL_CONFIG(info->pd_handle,
2930 	    &tuple, &config) != CS_SUCCESS) {
2931 		cmn_err(CE_WARN, "pcmcia: config failed to parse\n");
2932 		return (0);
2933 	}
2934 
2935 	for (cftable.index = (int)-1, i = -1;
2936 	    i != config.last; i = cftable.index) {
2937 		tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
2938 		tuple.Attributes = 0;
2939 		if (csx_GetNextTuple(info->pd_handle,
2940 		    &tuple) != CS_SUCCESS) {
2941 			cmn_err(CE_WARN, "pcmcia: get cftable failed\n");
2942 			break;
2943 		}
2944 		bzero(&cftable, sizeof (cftable));
2945 		if (csx_Parse_CISTPL_CFTABLE_ENTRY(info->pd_handle,
2946 		    &tuple, &cftable) !=
2947 		    CS_SUCCESS) {
2948 			cmn_err(CE_WARN, "pcmcia: parse cftable failed\n");
2949 			break;
2950 		}
2951 #if defined(PCMCIA_DEBUG)
2952 		if (pcmcia_debug)
2953 			cmn_err(CE_CONT, "\t%x: flags=%x (%x)\n",
2954 			    i, cftable.flags,
2955 			    cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ);
2956 #endif
2957 		if (cftable.flags & CISTPL_CFTABLE_TPCE_FS_IRQ)
2958 			return (1);
2959 	}
2960 	return (0);
2961 
2962 }
2963 
2964 /*
2965  * pcmcia_num_funcs()
2966  *	look for a CISTPL_LONGLINK_MFC
2967  *	if there is one, return the number of functions
2968  *	if there isn't one, then there is one function
2969  */
2970 static int
2971 pcmcia_num_funcs(int socket, client_handle_t handle)
2972 {
2973 	int count = 1;
2974 	cistpl_longlink_mfc_t mfc;
2975 	tuple_t tuple;
2976 
2977 	bzero(&tuple, sizeof (tuple_t));
2978 	tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
2979 	tuple.Socket = socket;
2980 	tuple.Attributes = 0;
2981 	if (csx_GetFirstTuple(handle, &tuple) == CS_SUCCESS) {
2982 		/* this is a multifunction card */
2983 		if (csx_ParseTuple(handle, &tuple, (cisparse_t *)&mfc,
2984 		    CISTPL_LONGLINK_MFC) == CS_SUCCESS) {
2985 			count = mfc.nfuncs;
2986 		}
2987 	}
2988 	return (count);
2989 }
2990 
2991 client_handle_t pcmcia_cs_handle;
2992 
2993 /*
2994  * pcmcia_create_dev_info(socket)
2995  *	either find or create the device information structure
2996  *	for the card(s) just inserted.	We don't care about removal yet.
2997  *	In any case, we will only do this at CS request
2998  */
2999 static void
3000 pcmcia_create_dev_info(int socket)
3001 {
3002 	struct pcm_device_info card_info;
3003 	client_reg_t reg;
3004 	cisinfo_t cisinfo;
3005 	int i;
3006 	dev_info_t *pdip;
3007 	static int handle_def = 0;
3008 
3009 #if defined(PCMCIA_DEBUG)
3010 	if (pcmcia_debug)
3011 		cmn_err(CE_CONT, "create dev_info_t for device in socket %d\n",
3012 		    socket);
3013 #endif
3014 
3015 	/*
3016 	 * before we can do anything else, we need the parent
3017 	 * devinfo of the socket.  This gets things in the right
3018 	 * place in the device tree.
3019 	 */
3020 
3021 	pdip = pcm_find_parent_dip(socket);
3022 	if (pdip == NULL)
3023 		return;
3024 
3025 	/* Card Services calls needed to get CIS info */
3026 	reg.dip = NULL;
3027 	reg.Attributes = INFO_SOCKET_SERVICES;
3028 	reg.EventMask = 0;
3029 	reg.event_handler = NULL;
3030 	reg.Version = CS_VERSION;
3031 
3032 	bzero(&card_info, sizeof (card_info));
3033 
3034 	if (handle_def == 0) {
3035 		if (csx_RegisterClient(&pcmcia_cs_handle,
3036 		    &reg) != CS_SUCCESS) {
3037 #if defined(PCMCIA_DEBUG)
3038 			if (pcmcia_debug)
3039 				cmn_err(CE_CONT,
3040 				    "pcmcia: RegisterClient failed\n");
3041 #endif
3042 			return;
3043 		}
3044 		handle_def++;
3045 	}
3046 	card_info.pd_handle = pcmcia_cs_handle;
3047 
3048 #if defined(PCMCIA_DEBUG)
3049 	if (pcmcia_debug)
3050 		cmn_err(CE_CONT,
3051 		    "pcmcia_create_dev_info: handle = %x\n",
3052 		    (int)card_info.pd_handle);
3053 #endif
3054 	card_info.pd_type = -1; /* no type to start */
3055 	card_info.pd_socket = socket;
3056 	card_info.pd_function = 0;
3057 	pcmcia_sockets[socket]->ls_functions = 1; /* default */
3058 
3059 	cisinfo.Socket = socket;
3060 
3061 	if ((i = csx_ValidateCIS(card_info.pd_handle,
3062 	    &cisinfo)) != SUCCESS ||
3063 	    cisinfo.Tuples == 0) {
3064 		/* no CIS means memory */
3065 		(void) strcpy(card_info.pd_generic_name, "memory");
3066 		card_info.pd_flags |= PCM_NAME_GENERIC |
3067 		    PCM_OTHER_NOCIS | PCM_NAME_1275;
3068 		(void) strcpy(card_info.pd_bind_name, "pccard,memory");
3069 		(void) strcpy(card_info.pd_generic_name, "memory");
3070 		card_info.pd_type = PCM_TYPE_MEMORY;
3071 	} else {
3072 		int functions, lsocket;
3073 		card_info.pd_tuples = cisinfo.Tuples;
3074 
3075 		/*
3076 		 * how many functions on the card?
3077 		 * we need to know and then we do one
3078 		 * child node for each function using
3079 		 * the function specific tuples.
3080 		 */
3081 		lsocket = CS_MAKE_SOCKET_NUMBER(socket, CS_GLOBAL_CIS);
3082 		functions = pcmcia_num_funcs(lsocket,
3083 		    card_info.pd_handle);
3084 		pcmcia_sockets[socket]->ls_functions = functions;
3085 		if (functions > 1) {
3086 			card_info.pd_flags |= PCM_MULTI_FUNCTION;
3087 		}
3088 		for (i = 0; i < functions; i++) {
3089 		    register int flags;
3090 		    lsocket = CS_MAKE_SOCKET_NUMBER(socket, i);
3091 		    card_info.pd_socket = socket;
3092 		    card_info.pd_function = i;
3093 			/*
3094 			 * new name construction
3095 			 */
3096 		    if (functions != 1) {
3097 			    /* need per function handle */
3098 			    card_info.pd_function = i;
3099 			    /* get new handle */
3100 		    }
3101 		    pcmcia_1275_name(lsocket, &card_info,
3102 			card_info.pd_handle);
3103 		    pcmcia_vers1_name(lsocket, &card_info,
3104 			card_info.pd_handle);
3105 		    pcmcia_generic_name(lsocket, &card_info,
3106 			card_info.pd_handle);
3107 		    flags = card_info.pd_flags;
3108 		    if (!(flags & PCM_NAME_1275)) {
3109 			if (flags & PCM_NAME_VERS1) {
3110 			    (void) strcpy(card_info.pd_bind_name,
3111 				PCMDEV_NAMEPREF);
3112 			    card_info.pd_bind_name[sizeof (PCMDEV_NAMEPREF)] =
3113 				',';
3114 			    (void) strncpy(card_info.pd_bind_name +
3115 				sizeof (PCMDEV_NAMEPREF),
3116 				card_info.pd_vers1_name,
3117 				MODMAXNAMELEN -
3118 				sizeof (PCMDEV_NAMEPREF));
3119 			    pcmcia_fix_string(card_info.pd_bind_name);
3120 			} else {
3121 				/*
3122 				 * have a CIS but not the right info
3123 				 * so treat as generic "pccard"
3124 				 */
3125 				(void) strcpy(card_info.pd_generic_name,
3126 				    "pccard,memory");
3127 				card_info.pd_flags |= PCM_NAME_GENERIC;
3128 				(void) strcpy(card_info.pd_bind_name,
3129 				    "pccard,memory");
3130 			}
3131 		    }
3132 		    pcmcia_init_devinfo(pdip, &card_info);
3133 		}
3134 		return;
3135 	}
3136 
3137 	pcmcia_init_devinfo(pdip, &card_info);
3138 }
3139 
3140 /*
3141  * pcmcia_init_devinfo()
3142  *	if there isn't a device info structure, create one
3143  *	if there is, we don't do much.
3144  *
3145  *	Note: this will need updating as 1275 finalizes their spec.
3146  */
3147 static void
3148 pcmcia_init_devinfo(dev_info_t *pdip, struct pcm_device_info *info)
3149 {
3150 	int unit;
3151 	dev_info_t *dip;
3152 	char *name;
3153 	struct pcmcia_parent_private *ppd;
3154 
3155 #if defined(PCMCIA_DEBUG)
3156 	if (pcmcia_debug)
3157 		cmn_err(CE_CONT, "init_devinfo(%s, %d)\n", info->pd_bind_name,
3158 		    info->pd_socket);
3159 #endif
3160 
3161 	/*
3162 	 * find out if there is already an instance of this
3163 	 * device.  We don't want to create a new one unnecessarily
3164 	 */
3165 	unit = CS_MAKE_SOCKET_NUMBER(info->pd_socket, info->pd_function);
3166 
3167 	dip = pcm_find_devinfo(pdip, info, unit);
3168 	if ((dip != NULL) && (ddi_getprop(DDI_DEV_T_NONE, dip,
3169 	    DDI_PROP_DONTPASS, PCM_DEV_SOCKET, -1) != -1)) {
3170 		/* it already exist but isn't a .conf file */
3171 
3172 #if defined(PCMCIA_DEBUG)
3173 		if (pcmcia_debug)
3174 			cmn_err(CE_CONT, "\tfound existing device node (%s)\n",
3175 			    ddi_get_name(dip));
3176 #endif
3177 		if (strlen(info->pd_vers1_name) > 0)
3178 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
3179 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
3180 
3181 		ppd = (struct pcmcia_parent_private *)
3182 		    ddi_get_parent_data(dip);
3183 
3184 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
3185 		    dip;
3186 
3187 		ppd->ppd_active = 1;
3188 
3189 		if (ndi_devi_online(dip, 0) == NDI_FAILURE) {
3190 			pcmcia_sockets[info->pd_socket]-> \
3191 			    ls_dip[info->pd_function] = NULL;
3192 			ppd->ppd_active = 0;
3193 		}
3194 	} else {
3195 
3196 		char *dtype;
3197 
3198 #if defined(PCMCIA_DEBUG)
3199 		if (pcmcia_debug)
3200 			cmn_err(CE_CONT, "pcmcia: create child [%s](%d): %s\n",
3201 			    info->pd_bind_name, info->pd_socket,
3202 			    info->pd_generic_name);
3203 #endif
3204 
3205 		if (info->pd_flags & PCM_NAME_GENERIC)
3206 			name = info->pd_generic_name;
3207 		else
3208 			name = info->pd_bind_name;
3209 
3210 		if (ndi_devi_alloc(pdip, name, (pnode_t)DEVI_SID_NODEID,
3211 		    &dip) !=
3212 		    NDI_SUCCESS) {
3213 			cmn_err(CE_WARN,
3214 			    "pcmcia: unable to create device [%s](%d)\n",
3215 			    name, info->pd_socket);
3216 			return;
3217 		}
3218 		/*
3219 		 * construct the "compatible" property if the device
3220 		 * has a generic name
3221 		 */
3222 		pcmcia_add_compatible(dip, info);
3223 
3224 		ppd = kmem_zalloc(sizeof (struct pcmcia_parent_private),
3225 		    KM_SLEEP);
3226 
3227 		ppd->ppd_socket = info->pd_socket;
3228 		ppd->ppd_function = info->pd_function;
3229 
3230 		/*
3231 		 * add the "socket" property
3232 		 * the value of this property contains the logical PCMCIA
3233 		 * socket number the device has been inserted in, along
3234 		 * with the function # if the device is part of a
3235 		 * multi-function device.
3236 		 */
3237 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3238 		    PCM_DEV_SOCKET, unit);
3239 
3240 		if (info->pd_flags & PCM_MULTI_FUNCTION)
3241 			ppd->ppd_flags |= PPD_CARD_MULTI;
3242 
3243 		/*
3244 		 * determine all the properties we need for PPD
3245 		 * then create the properties
3246 		 */
3247 		/* socket is unique */
3248 		pcmcia_find_regs(dip, info, ppd);
3249 
3250 		ppd->ppd_intr = pcmcia_need_intr(unit, info);
3251 
3252 		if (ppd->ppd_nreg > 0)
3253 			(void) ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
3254 			    "reg", (int *)ppd->ppd_reg, ppd->ppd_nreg *
3255 			    sizeof (struct pcm_regs) / sizeof (int));
3256 		if (ppd->ppd_intr) {
3257 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
3258 			    "interrupts", ppd->ppd_intr);
3259 			ppd->ppd_intrspec =
3260 			    kmem_zalloc(sizeof (struct intrspec), KM_SLEEP);
3261 		}
3262 
3263 		/* set parent private - our own format */
3264 		ddi_set_parent_data(dip, (caddr_t)ppd);
3265 
3266 		/* init the device type */
3267 		if (info->pd_type >= 0 &&
3268 		    info->pd_type < (sizeof (pcmcia_dev_type) /
3269 		    (sizeof (char *))))
3270 			dtype = pcmcia_dev_type[info->pd_type];
3271 		else
3272 			dtype = "unknown";
3273 
3274 		if (strlen(info->pd_vers1_name) > 0)
3275 			(void) ndi_prop_update_string(DDI_DEV_T_NONE,
3276 			    dip, PCM_DEV_MODEL, info->pd_vers1_name);
3277 
3278 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
3279 		    PCM_DEVICETYPE, dtype);
3280 
3281 		/* set PC Card as active and present in socket */
3282 		pcmcia_sockets[info->pd_socket]->ls_dip[info->pd_function] =
3283 		    dip;
3284 
3285 		ppd->ppd_active = 1;
3286 
3287 		/*
3288 		 * We should not call ndi_devi_online here if
3289 		 * pcmcia attach is in progress. This causes a deadlock.
3290 		 */
3291 		if (pcmcia_dip != dip) {
3292 			if (ndi_devi_online_async(dip, 0)
3293 			    != NDI_SUCCESS) {
3294 				pcmcia_sockets[info->pd_socket]->\
3295 				    ls_dip[info->pd_function] = NULL;
3296 				pcmcia_ppd_free(ppd);
3297 				(void) ndi_devi_free(dip);
3298 				return;
3299 			}
3300 		}
3301 
3302 #if defined(PCMCIA_DEBUG)
3303 	if (pcmcia_debug)
3304 		cmn_err(CE_CONT, "\tjust added \"active\" to %s in %d\n",
3305 		    ddi_get_name(dip), info->pd_socket);
3306 #endif
3307 	}
3308 
3309 	/*
3310 	 * inform the event manager that a child was added
3311 	 * to the device tree.
3312 	 */
3313 	pcm_event_manager(PCE_DEV_IDENT, unit, ddi_get_name(dip));
3314 
3315 #if defined(PCMCIA_DEBUG)
3316 	if (pcmcia_debug > 1) {
3317 		pcmcia_dump_minors(dip);
3318 	}
3319 #endif
3320 }
3321 
3322 /*
3323  * free any allocated parent-private data
3324  */
3325 static void
3326 pcmcia_ppd_free(struct pcmcia_parent_private *ppd)
3327 {
3328 	size_t len;
3329 
3330 	if (ppd->ppd_nreg != 0) {
3331 		len = ppd->ppd_nreg * sizeof (uint32_t) * 3;
3332 		kmem_free(ppd->ppd_reg, len);
3333 		len = sizeof (struct pcm_regs) * ppd->ppd_nreg;
3334 		kmem_free(ppd->ppd_assigned, len);
3335 	}
3336 
3337 	/*
3338 	 * pcmcia only allocates 1 intrspec today
3339 	 */
3340 	if (ppd->ppd_intr != 0) {
3341 		len = sizeof (struct intrspec) * ppd->ppd_intr;
3342 		kmem_free(ppd->ppd_intrspec, len);
3343 	}
3344 
3345 	kmem_free(ppd, sizeof (*ppd));
3346 }
3347 
3348 
3349 /*
3350  * pcmcia_get_devinfo(socket)
3351  *	entry point to allow finding the device info structure
3352  *	for a given logical socket.  Used by event manager
3353  */
3354 dev_info_t *
3355 pcmcia_get_devinfo(int socket)
3356 {
3357 	int func = CS_GET_FUNCTION_NUMBER(socket);
3358 	socket = CS_GET_SOCKET_NUMBER(socket);
3359 	if (pcmcia_sockets[socket])
3360 		return (pcmcia_sockets[socket]->ls_dip[func]);
3361 	return ((dev_info_t *)NULL);
3362 }
3363 
3364 /*
3365  * CSGetCookiesAndDip()
3366  *	get info needed by CS to setup soft interrupt handler and provide
3367  *		socket-specific adapter information
3368  */
3369 static int
3370 GetCookiesAndDip(sservice_t *serv)
3371 {
3372 	pcmcia_logical_socket_t *socket;
3373 	csss_adapter_info_t *ai;
3374 	int sock;
3375 
3376 	sock = CS_GET_SOCKET_NUMBER(serv->get_cookies.socket);
3377 
3378 	if (sock >= pcmcia_num_sockets ||
3379 	    (int)serv->get_cookies.socket < 0)
3380 		return (BAD_SOCKET);
3381 
3382 	socket = pcmcia_sockets[sock];
3383 	ai = &serv->get_cookies.adapter_info;
3384 	serv->get_cookies.dip = socket->ls_adapter->pca_dip;
3385 	serv->get_cookies.iblock = socket->ls_adapter->pca_iblock;
3386 	serv->get_cookies.idevice = socket->ls_adapter->pca_idev;
3387 
3388 	/*
3389 	 * Setup the adapter info for Card Services
3390 	 */
3391 	(void) strcpy(ai->name, socket->ls_adapter->pca_name);
3392 	ai->major = socket->ls_adapter->pca_module;
3393 	ai->minor = socket->ls_adapter->pca_unit;
3394 	ai->number = socket->ls_adapter->pca_number;
3395 	ai->num_sockets = socket->ls_adapter->pca_numsockets;
3396 	ai->first_socket = socket->ls_adapter->pca_first_socket;
3397 
3398 	return (SUCCESS);
3399 }
3400 
3401 /*
3402  * Note:
3403  *	The following functions that start with 'SS'
3404  *	implement SocketServices interfaces.  They
3405  *	simply map the socket and/or window number to
3406  *	the adapter specific number based on the general
3407  *	value that CardServices uses.
3408  *
3409  *	See the descriptions in SocketServices for
3410  *	details.  Also refer to specific adapter drivers
3411  *	for implementation reference.
3412  */
3413 
3414 static int
3415 SSGetAdapter(get_adapter_t *adapter)
3416 {
3417 	int n;
3418 	get_adapter_t info;
3419 
3420 	adapter->state = (unsigned)0xFFFFFFFF;
3421 	adapter->SCRouting = 0xFFFFFFFF;
3422 
3423 	for (n = 0; n < pcmcia_num_adapters; n++) {
3424 		GET_ADAPTER(pcmcia_adapters[n]->pca_if,
3425 		    pcmcia_adapters[n]->pca_dip, &info);
3426 		adapter->state &= info.state;
3427 		adapter->SCRouting &= info.SCRouting;
3428 	}
3429 
3430 	return (SUCCESS);
3431 }
3432 
3433 static int
3434 SSGetPage(get_page_t *page)
3435 {
3436 	pcmcia_logical_window_t *window;
3437 	get_page_t newpage;
3438 	int retval, win;
3439 
3440 	if (page->window > pcmcia_num_windows) {
3441 		return (BAD_WINDOW);
3442 	}
3443 
3444 	window = pcmcia_windows[page->window];
3445 	newpage = *page;
3446 	win = newpage.window = window->lw_window; /* real window */
3447 
3448 	retval = GET_PAGE(window->lw_if, window->lw_adapter->pca_dip,
3449 	    &newpage);
3450 	if (retval == SUCCESS) {
3451 		*page = newpage;
3452 		page->window = win;
3453 	}
3454 	return (retval);
3455 }
3456 
3457 static int
3458 SSGetSocket(get_socket_t *socket)
3459 {
3460 	int retval, sock;
3461 	get_socket_t newsocket;
3462 	pcmcia_logical_socket_t *sockp;
3463 
3464 	sock = socket->socket;
3465 	if (sock > pcmcia_num_sockets ||
3466 	    (sockp = pcmcia_sockets[sock]) == NULL) {
3467 		return (BAD_SOCKET);
3468 	}
3469 
3470 	newsocket = *socket;
3471 	newsocket.socket = sockp->ls_socket;
3472 	retval = GET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3473 	    &newsocket);
3474 	if (retval == SUCCESS) {
3475 		newsocket.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
3476 		    newsocket.VccLevel,
3477 		    VCC);
3478 		newsocket.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
3479 		    newsocket.Vpp1Level,
3480 		    VPP1);
3481 		newsocket.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
3482 		    newsocket.Vpp2Level,
3483 		    VPP2);
3484 		*socket = newsocket;
3485 		socket->socket = sock;
3486 	}
3487 
3488 	return (retval);
3489 }
3490 
3491 static int
3492 SSGetStatus(get_ss_status_t *status)
3493 {
3494 	get_ss_status_t newstat;
3495 	int sock, retval;
3496 	pcmcia_logical_socket_t *sockp;
3497 
3498 	sock = status->socket;
3499 	if (sock > pcmcia_num_sockets ||
3500 	    (sockp = pcmcia_sockets[sock]) == NULL) {
3501 		return (BAD_SOCKET);
3502 	}
3503 
3504 	newstat = *status;
3505 	newstat.socket = sockp->ls_socket;
3506 	retval = GET_STATUS(sockp->ls_if, sockp->ls_adapter->pca_dip,
3507 	    &newstat);
3508 	if (retval == SUCCESS) {
3509 		*status = newstat;
3510 		status->socket = sock;
3511 	}
3512 
3513 	return (retval);
3514 }
3515 
3516 static int
3517 SSGetWindow(get_window_t *window)
3518 {
3519 	int win, retval;
3520 	get_window_t newwin;
3521 	pcmcia_logical_window_t *winp;
3522 
3523 	win = window->window;
3524 	winp = pcmcia_windows[win];
3525 	newwin = *window;
3526 	newwin.window = winp->lw_window;
3527 
3528 	retval = GET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
3529 	    &newwin);
3530 	if (retval == SUCCESS) {
3531 		newwin.socket = winp->lw_socket;
3532 		newwin.window = win;
3533 		*window = newwin;
3534 	}
3535 	return (retval);
3536 }
3537 
3538 /*
3539  * SSInquireAdapter()
3540  *	Get the capabilities of the "generic" adapter
3541  *	we are exporting to CS.
3542  */
3543 static int
3544 SSInquireAdapter(inquire_adapter_t *adapter)
3545 {
3546 	adapter->NumSockets = pcmcia_num_sockets;
3547 	adapter->NumWindows = pcmcia_num_windows;
3548 	adapter->NumEDCs = 0;
3549 	/*
3550 	 * notes: Adapter Capabilities are going to be difficult to
3551 	 * determine with reliability.	Fortunately, most of them
3552 	 * don't matter under Solaris or can be handled transparently
3553 	 */
3554 	adapter->AdpCaps = 0;	/* need to fix these */
3555 	/*
3556 	 * interrupts need a little work.  For x86, the valid IRQs will
3557 	 * be restricted to those that the system has exported to the nexus.
3558 	 * for SPARC, it will be the DoRight values.
3559 	 */
3560 	adapter->ActiveHigh = 0;
3561 	adapter->ActiveLow = 0;
3562 	adapter->power_entry = pcmcia_power_table; /* until we resolve this */
3563 	adapter->NumPower = pcmcia_num_power;
3564 	return (SUCCESS);
3565 }
3566 
3567 static int
3568 SSInquireSocket(inquire_socket_t *socket)
3569 {
3570 	int retval, sock;
3571 	inquire_socket_t newsocket;
3572 	pcmcia_logical_socket_t *sockp;
3573 
3574 	sock = socket->socket;
3575 	if (sock > pcmcia_num_sockets ||
3576 	    (sockp = pcmcia_sockets[sock]) == NULL)
3577 		return (BAD_SOCKET);
3578 	newsocket = *socket;
3579 	newsocket.socket = sockp->ls_socket;
3580 	retval = INQUIRE_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3581 	    &newsocket);
3582 	if (retval == SUCCESS) {
3583 		*socket = newsocket;
3584 		socket->socket = sock;
3585 	}
3586 	return (retval);
3587 }
3588 
3589 static int
3590 SSInquireWindow(inquire_window_t *window)
3591 {
3592 	int retval, win;
3593 	pcmcia_logical_window_t *winp;
3594 	inquire_window_t newwin;
3595 	int slide;
3596 
3597 	win = window->window;
3598 	if (win > pcmcia_num_windows)
3599 		return (BAD_WINDOW);
3600 
3601 	winp = pcmcia_windows[win];
3602 	newwin = *window;
3603 	newwin.window = winp->lw_window;
3604 	retval = INQUIRE_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip,
3605 	    &newwin);
3606 #if defined(PCMCIA_DEBUG)
3607 		if (pcmcia_debug > 1)
3608 			cmn_err(CE_CONT, "SSInquireWindow: win=%d, pwin=%d\n",
3609 			    win, newwin.window);
3610 #endif
3611 	if (retval == SUCCESS) {
3612 		*window = newwin;
3613 		/* just in case */
3614 		window->iowin_char.IOWndCaps &= ~WC_BASE;
3615 		slide = winp->lw_adapter->pca_first_socket;
3616 		/*
3617 		 * note that sockets are relative to the adapter.
3618 		 * we have to adjust the bits to show a logical
3619 		 * version.
3620 		 */
3621 
3622 		pcm_fix_bits(newwin.Sockets, window->Sockets, slide, 0);
3623 
3624 #if defined(PCMCIA_DEBUG)
3625 		if (pcmcia_debug > 1) {
3626 			cmn_err(CE_CONT, "iw: orig bits=%x, new bits=%x\n",
3627 			    (int)*(uint32_t *)newwin.Sockets,
3628 			    (int)*(uint32_t *)window->Sockets);
3629 			cmn_err(CE_CONT, "\t%x.%x.%x\n", window->WndCaps,
3630 			    window->mem_win_char.MemWndCaps,
3631 			    window->mem_win_char.MinSize);
3632 		}
3633 #endif
3634 		window->window = win;
3635 	}
3636 	return (retval);
3637 }
3638 
3639 static int
3640 SSResetSocket(int socket, int mode)
3641 {
3642 	pcmcia_logical_socket_t *sockp;
3643 
3644 	if (socket >= pcmcia_num_sockets ||
3645 	    (sockp = pcmcia_sockets[socket]) == NULL)
3646 		return (BAD_SOCKET);
3647 
3648 	return (RESET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3649 	    sockp->ls_socket, mode));
3650 }
3651 
3652 static int
3653 SSSetPage(set_page_t *page)
3654 {
3655 	int window, retval;
3656 	set_page_t newpage;
3657 	pcmcia_logical_window_t *winp;
3658 
3659 	window = page->window;
3660 	if (window > pcmcia_num_windows) {
3661 #if defined(PCMCIA_DEBUG)
3662 		if (pcmcia_debug > 1)
3663 			cmn_err(CE_CONT, "SSSetPage: window=%d (of %d)\n",
3664 			    window, pcmcia_num_windows);
3665 #endif
3666 		return (BAD_WINDOW);
3667 	}
3668 
3669 	winp = pcmcia_windows[window];
3670 	newpage = *page;
3671 	newpage.window = winp->lw_window;
3672 	retval = SET_PAGE(winp->lw_if, winp->lw_adapter->pca_dip, &newpage);
3673 	if (retval == SUCCESS) {
3674 		newpage.window = window;
3675 		*page = newpage;
3676 	}
3677 #if defined(PCMCIA_DEBUG)
3678 	if ((pcmcia_debug > 1) && retval != SUCCESS)
3679 		cmn_err(CE_CONT, "\tSetPage: returning error %x\n", retval);
3680 #endif
3681 	return (retval);
3682 }
3683 
3684 static int
3685 SSSetWindow(set_window_t *win)
3686 {
3687 	int socket, window, retval, func;
3688 	set_window_t newwin;
3689 	pcmcia_logical_window_t *winp;
3690 	pcmcia_logical_socket_t *sockp;
3691 
3692 	window = win->window;
3693 	if (window > pcmcia_num_windows)
3694 		return (BAD_WINDOW);
3695 
3696 	socket = CS_GET_SOCKET_NUMBER(win->socket);
3697 	func = CS_GET_FUNCTION_NUMBER(win->socket);
3698 
3699 	if (socket > pcmcia_num_sockets ||
3700 	    (sockp = pcmcia_sockets[socket]) == NULL) {
3701 		return (BAD_SOCKET);
3702 	}
3703 
3704 	winp = pcmcia_windows[window];
3705 	winp->lw_socket = win->socket; /* reverse map */
3706 	newwin = *win;
3707 	newwin.window = winp->lw_window;
3708 	newwin.socket = sockp->ls_socket;
3709 	newwin.child = sockp->ls_dip[func]; /* so we carry the dip around */
3710 
3711 	retval = SET_WINDOW(winp->lw_if, winp->lw_adapter->pca_dip, &newwin);
3712 	if (retval == SUCCESS) {
3713 		newwin.window = window;
3714 		newwin.socket = winp->lw_socket;
3715 		*win = newwin;
3716 	}
3717 	return (retval);
3718 }
3719 
3720 static int
3721 SSSetSocket(set_socket_t *socket)
3722 {
3723 	int sock, retval;
3724 	pcmcia_logical_socket_t *sockp;
3725 	set_socket_t newsock;
3726 
3727 	sock = socket->socket;
3728 	if (sock > pcmcia_num_sockets ||
3729 	    (sockp = pcmcia_sockets[sock]) == NULL) {
3730 		return (BAD_SOCKET);
3731 	}
3732 
3733 	newsock = *socket;
3734 	/* note: we force CS to always get insert/removal events */
3735 	sockp->ls_cs_events = pcm_mapevents(newsock.SCIntMask) |
3736 	    PCE_E2M(PCE_CARD_INSERT) | PCE_E2M(PCE_CARD_REMOVAL);
3737 #if defined(PCMCIA_DEBUG)
3738 	if (pcmcia_debug > 1)
3739 		cmn_err(CE_CONT,
3740 		    "SetSocket: SCIntMask = %x\n", newsock.SCIntMask);
3741 #endif
3742 	newsock.socket = sockp->ls_socket;
3743 	newsock.VccLevel = pcmcia_map_power_set(sockp->ls_adapter,
3744 	    newsock.VccLevel, VCC);
3745 	newsock.Vpp1Level = pcmcia_map_power_set(sockp->ls_adapter,
3746 	    newsock.Vpp1Level, VPP1);
3747 	newsock.Vpp2Level = pcmcia_map_power_set(sockp->ls_adapter,
3748 	    newsock.Vpp2Level, VPP2);
3749 	retval = SET_SOCKET(sockp->ls_if, sockp->ls_adapter->pca_dip,
3750 	    &newsock);
3751 	if (retval == SUCCESS) {
3752 		newsock.socket = sock;
3753 		newsock.VccLevel = pcmcia_map_power_get(sockp->ls_adapter,
3754 		    newsock.VccLevel,
3755 		    VCC);
3756 		newsock.Vpp1Level = pcmcia_map_power_get(sockp->ls_adapter,
3757 		    newsock.Vpp1Level,
3758 		    VPP1);
3759 		newsock.Vpp2Level = pcmcia_map_power_get(sockp->ls_adapter,
3760 		    newsock.Vpp2Level,
3761 		    VPP2);
3762 		*socket = newsock;
3763 		if (socket->IREQRouting & IRQ_ENABLE) {
3764 			sockp->ls_flags |= PCS_IRQ_ENABLED;
3765 		} else {
3766 			sockp->ls_flags &= ~PCS_IRQ_ENABLED;
3767 		}
3768 	}
3769 	return (retval);
3770 }
3771 
3772 /*
3773  * SSSetIRQHandler()
3774  *	arrange for IRQ to be allocated if appropriate and always
3775  *	arrange that PC Card interrupt handlers get called.
3776  */
3777 static int
3778 SSSetIRQHandler(set_irq_handler_t *handler)
3779 {
3780 	int sock, retval, func;
3781 	pcmcia_logical_socket_t *sockp;
3782 	struct pcmcia_parent_private *ppd;
3783 	dev_info_t *dip;
3784 	ddi_iblock_cookie_t iblk;
3785 	ddi_idevice_cookie_t idev;
3786 
3787 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
3788 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
3789 	if (sock > pcmcia_num_sockets ||
3790 	    (sockp = pcmcia_sockets[sock]) == NULL) {
3791 		return (BAD_SOCKET);
3792 	}
3793 #if defined(PCMCIA_DEBUG)
3794 	if (pcmcia_debug) {
3795 
3796 		cmn_err(CE_CONT, "SSSetIRQHandler: socket=%x, function=%x\n",
3797 		    sock, func);
3798 		cmn_err(CE_CONT, "\thandler(%p): socket=%x, irq=%x, id=%x\n",
3799 		    (void *)handler->handler, handler->socket, handler->irq,
3800 		    handler->handler_id);
3801 	}
3802 #endif
3803 	dip = sockp->ls_dip[func];
3804 
3805 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
3806 
3807 	handler->iblk_cookie = &iblk;
3808 	handler->idev_cookie = &idev;
3809 
3810 	retval = ddi_add_intr(dip, 0, handler->iblk_cookie,
3811 	    handler->idev_cookie,
3812 	    (uint32_t(*)(caddr_t)) handler->handler,
3813 	    handler->arg1);
3814 
3815 	if (retval == DDI_SUCCESS) {
3816 		handler->iblk_cookie = &sockp->ls_iblk;
3817 		handler->idev_cookie = &sockp->ls_idev;
3818 		handler->irq = ppd->ppd_intrspec->intrspec_vec;
3819 		retval = SUCCESS;
3820 	} else {
3821 		retval = sockp->ls_error;
3822 	}
3823 	return (retval);
3824 }
3825 
3826 /*
3827  * SSClearIRQHandler()
3828  *	Arrange to have the interrupt handler specified removed
3829  *	from the interrupt list.
3830  */
3831 static int
3832 SSClearIRQHandler(clear_irq_handler_t *handler)
3833 {
3834 	int sock, func;
3835 	pcmcia_logical_socket_t *sockp;
3836 	dev_info_t *dip;
3837 
3838 	sock = CS_GET_SOCKET_NUMBER(handler->socket);
3839 	func = CS_GET_FUNCTION_NUMBER(handler->socket);
3840 
3841 #if defined(PCMCIA_DEBUG)
3842 	if (pcmcia_debug) {
3843 
3844 		cmn_err(CE_CONT,
3845 		    "SSClearIRQHandler: socket=%x, function=%x\n",
3846 		    sock, func);
3847 		cmn_err(CE_CONT,
3848 		    "\thandler(%p): socket=%x, id=%x\n",
3849 		    (void *)handler, handler->socket,
3850 		    handler->handler_id);
3851 	}
3852 #endif
3853 
3854 	if (sock > pcmcia_num_sockets ||
3855 	    (sockp = pcmcia_sockets[sock]) == NULL) {
3856 		return (BAD_SOCKET);
3857 	}
3858 	dip = sockp->ls_dip[func];
3859 	if (dip) {
3860 		ddi_remove_intr(dip, 0, NULL);
3861 		return (SUCCESS);
3862 	}
3863 	return (BAD_SOCKET);
3864 }
3865 
3866 
3867 /*
3868  * pcm_pathname()
3869  *	make a partial path from dip.
3870  *	used to mknods relative to /devices/pcmcia/
3871  *
3872  * XXX - we now use ddi_get_name_addr to get the "address" portion
3873  *	of the name; that way, we only have to modify the name creation
3874  *	algorithm in one place
3875  */
3876 static void
3877 pcm_pathname(dev_info_t *dip, char *name, char *path)
3878 {
3879 	(void) sprintf(path, "%s@%s:%s", ddi_node_name(dip),
3880 	    ddi_get_name_addr(dip), name);
3881 }
3882 
3883 /*
3884  * pcmcia_create_device()
3885  *	create the /devices entries for the driver
3886  *	it is assumed that the PC Card driver will do a
3887  *	RegisterClient for each subdevice.
3888  *	The device type string is encoded here to match
3889  *	the standardized names when possible.
3890  * XXX - note that we may need to provide a way for the
3891  *	caller to specify the complete name string that
3892  *	we pass to ddi_set_name_addr
3893  */
3894 static int
3895 pcmcia_create_device(ss_make_device_node_t *init)
3896 {
3897 	int err = SUCCESS;
3898 	struct pcm_make_dev device;
3899 	struct dev_ops *ops;
3900 	major_t major;
3901 
3902 	/*
3903 	 * Now that we have the name, create it.
3904 	 */
3905 
3906 	bzero(&device, sizeof (device));
3907 	if (init->flags & SS_CSINITDEV_CREATE_DEVICE) {
3908 		if ((err = ddi_create_minor_node(init->dip,
3909 		    init->name,
3910 		    init->spec_type,
3911 		    init->minor_num,
3912 		    init->node_type,
3913 		    0)) != DDI_SUCCESS) {
3914 #if defined(PCMCIA_DEBUG)
3915 			if (pcmcia_debug)
3916 				cmn_err(CE_CONT,
3917 				    "pcmcia_create_device: failed "
3918 				    "create\n");
3919 #endif
3920 			return (BAD_ATTRIBUTE);
3921 		}
3922 
3923 		major = ddi_driver_major(init->dip);
3924 		ops = ddi_get_driver(init->dip);
3925 		LOCK_DEV_OPS(&devnamesp[major].dn_lock);
3926 		INCR_DEV_OPS_REF(ops);
3927 		(void) ddi_pathname(init->dip, device.path);
3928 		DECR_DEV_OPS_REF(ops);
3929 		UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
3930 		(void) sprintf(device.path + strlen(device.path), ":%s",
3931 		    init->name);
3932 
3933 		(void) strcpy(device.driver, ddi_binding_name(init->dip));
3934 #if defined(PCMCIA_DEBUG)
3935 		if (pcmcia_debug)
3936 			cmn_err(CE_CONT,
3937 			    "pcmcia_create_device: created %s "
3938 			    "from %s [%s]\n",
3939 			    device.path, init->name, device.driver);
3940 #endif
3941 		device.dev =
3942 		    makedevice(ddi_driver_major(init->dip), init->minor_num);
3943 		device.flags |= (init->flags & SS_CSINITDEV_MORE_DEVICES) ?
3944 		    PCM_EVENT_MORE : 0;
3945 		device.type = init->spec_type;
3946 		device.op = SS_CSINITDEV_CREATE_DEVICE;
3947 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
3948 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
3949 		    -1);
3950 	} else if (init->flags & SS_CSINITDEV_REMOVE_DEVICE) {
3951 		device.op = SS_CSINITDEV_REMOVE_DEVICE;
3952 		device.socket = ddi_getprop(DDI_DEV_T_ANY, init->dip,
3953 		    DDI_PROP_CANSLEEP, PCM_DEV_SOCKET,
3954 		    -1);
3955 		if (init->name != NULL)
3956 			(void) strcpy(device.path, init->name);
3957 		device.dev = makedevice(ddi_driver_major(init->dip), 0);
3958 		ddi_remove_minor_node(init->dip, init->name);
3959 	}
3960 
3961 	/*
3962 	 *	we send an event for ALL devices created.
3963 	 *	To do otherwise ties us to using drvconfig
3964 	 *	forever.  There are relatively few devices
3965 	 *	ever created so no need to do otherwise.
3966 	 *	The existence of the event manager must never
3967 	 *	be visible to a PCMCIA device driver.
3968 	 */
3969 	pcm_event_manager(PCE_INIT_DEV, device.socket, &device);
3970 
3971 	return (err);
3972 }
3973 
3974 /*
3975  * pcmcia_get_minors()
3976  *	We need to traverse the minor node list of the
3977  *	dip if there are any.  This takes two passes;
3978  *	one to get the count and buffer size and the
3979  *	other to actually copy the data into the buffer.
3980  *	The framework requires that the dip be locked
3981  *	during this time to avoid breakage as well as the
3982  *	driver being locked.
3983  */
3984 int
3985 pcmcia_get_minors(dev_info_t *dip, struct pcm_make_dev **minors)
3986 {
3987 	int circ;
3988 	int count = 0;
3989 	struct ddi_minor_data *dp;
3990 	struct pcm_make_dev *md;
3991 	int socket;
3992 	major_t major;
3993 	struct dev_ops *ops;
3994 
3995 	socket = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3996 	    PCM_DEV_SOCKET, -1);
3997 	ndi_devi_enter(dip, &circ);
3998 	if (DEVI(dip)->devi_minor != (struct ddi_minor_data *)NULL) {
3999 		for (dp = DEVI(dip)->devi_minor;
4000 		    dp != (struct ddi_minor_data *)NULL;
4001 		    dp = dp->next) {
4002 			count++; /* have one more */
4003 		}
4004 		/* we now know how many nodes to allocate */
4005 		md = kmem_zalloc(count * sizeof (struct pcm_make_dev),
4006 		    KM_NOSLEEP);
4007 		if (md != NULL) {
4008 			*minors = md;
4009 			for (dp = DEVI(dip)->devi_minor;
4010 			    dp != (struct ddi_minor_data *)NULL;
4011 			    dp = dp->next, md++) {
4012 #if defined(PCMCIA_DEBUG)
4013 				if (pcmcia_debug > 1) {
4014 					cmn_err(CE_CONT,
4015 					    "pcmcia_get_minors: name=%s,"
4016 					    "socket=%d, stype=%x, "
4017 					    "ntype=%s, dev_t=%x",
4018 					    dp->ddm_name,
4019 					    socket,
4020 					    dp->ddm_spec_type,
4021 					    dp->ddm_node_type,
4022 					    (int)dp->ddm_dev);
4023 					cmn_err(CE_CONT,
4024 					    "\tbind name = %s\n",
4025 					    ddi_binding_name(dip));
4026 				}
4027 #endif
4028 				md->socket = socket;
4029 				md->op = SS_CSINITDEV_CREATE_DEVICE;
4030 				md->dev = dp->ddm_dev;
4031 				md->type = dp->ddm_spec_type;
4032 				(void) strcpy(md->driver,
4033 				    ddi_binding_name(dip));
4034 				major = ddi_driver_major(dip);
4035 				ops = ddi_get_driver(dip);
4036 				LOCK_DEV_OPS(&devnamesp[major].dn_lock);
4037 				pcm_pathname(dip, dp->ddm_name, md->path);
4038 				INCR_DEV_OPS_REF(ops);
4039 				(void) ddi_pathname(dip, md->path);
4040 				DECR_DEV_OPS_REF(ops);
4041 				UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
4042 				(void) sprintf(md->path + strlen(md->path),
4043 				    ":%s", dp->ddm_name);
4044 				if (dp->next == NULL)
4045 					/* no more */
4046 					md->flags |= PCM_EVENT_MORE;
4047 			}
4048 		} else {
4049 			count = 0;
4050 		}
4051 	}
4052 	ndi_devi_exit(dip, circ);
4053 	return (count);
4054 }
4055 
4056 #if defined(PCMCIA_DEBUG)
4057 static char *ddmtypes[] = { "minor", "alias", "default", "internal" };
4058 
4059 static void
4060 pcmcia_dump_minors(dev_info_t *dip)
4061 {
4062 	int circ;
4063 	int count = 0;
4064 	struct ddi_minor_data *dp;
4065 	int unit, major;
4066 	dev_info_t *np;
4067 
4068 	unit = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4069 	    PCM_DEV_SOCKET, -1);
4070 	cmn_err(CE_CONT,
4071 	    "pcmcia_dump_minors: dip=%p, socket=%d\n", (void *)dip, unit);
4072 
4073 	major = ddi_driver_major(dip);
4074 	if (major != -1) {
4075 		for (np = devnamesp[major].dn_head; np != NULL;
4076 		    np = (dev_info_t *)DEVI(np)->devi_next) {
4077 			char *cf2 = "";
4078 			char *cur = "";
4079 			if (i_ddi_node_state(np) == DS_READY)
4080 				cf2 = "DS_READY";
4081 			if (np == dip)
4082 				cur = "CUR";
4083 			cmn_err(CE_CONT, "\tsibs: %s %s %s\n",
4084 			    ddi_binding_name(np), cf2, cur);
4085 
4086 			ndi_devi_enter(np, &circ);
4087 			if (DEVI(np)->devi_minor !=
4088 			    (struct ddi_minor_data *)NULL) {
4089 				for (dp = DEVI(np)->devi_minor;
4090 				    dp != (struct ddi_minor_data *)NULL;
4091 				    dp = dp->next) {
4092 					count++; /* have one more */
4093 				}
4094 				for (dp = DEVI(dip)->devi_minor;
4095 				    dp != (struct ddi_minor_data *)NULL;
4096 				    dp = dp->next) {
4097 					cmn_err(CE_CONT, "\ttype=%s, name=%s,"
4098 					    "socket=%d, stype=%x, "
4099 					    "ntype=%s, dev_t=%x",
4100 					    ddmtypes[dp->type],
4101 					    dp->ddm_name,
4102 					    unit,
4103 					    dp->ddm_spec_type,
4104 					    dp->ddm_node_type,
4105 					    (int)dp->ddm_dev);
4106 					cmn_err(CE_CONT, "\tbind name = %s\n",
4107 					    ddi_binding_name(np));
4108 				}
4109 			}
4110 			ndi_devi_exit(np, circ);
4111 		}
4112 	}
4113 }
4114 #endif
4115 
4116 /*
4117  * experimental merging code
4118  * what are the things that we should merge on?
4119  *	match something by name in the "compatible" property
4120  *	restrict to a specific "socket"
4121  *	restrict to a specific "instance"
4122  */
4123 /*ARGSUSED*/
4124 static int
4125 pcmcia_merge_conf(dev_info_t *dip)
4126 {
4127 	return (0);		/* merge failed */
4128 }
4129 
4130 /*
4131  * pcmcia_mfc_intr()
4132  *	Multifunction Card interrupt handler
4133  *	While some adapters share interrupts at the lowest
4134  *	level, some can't.  In order to be consistent, we
4135  *	split multifunction cards out with this intercept and
4136  *	allow the low level to do what is best for it.
4137  *	the arg is a pcmcia_socket structure and all interrupts
4138  *	are per-socket in this case.  We also have the option
4139  *	to optimize if the cards support it.  It also means
4140  *	that we can use the INTRACK mode if it proves desirable
4141  */
4142 /*ARGSUSED*/
4143 static uint32_t
4144 pcmcia_mfc_intr(caddr_t arg1, caddr_t arg2)
4145 {
4146 	pcmcia_logical_socket_t *sockp;
4147 	inthandler_t *intr, *first;
4148 	int done, result;
4149 
4150 	sockp = (pcmcia_logical_socket_t *)arg1;
4151 
4152 #if defined(PCMCIA_DEBUG)
4153 	if (pcmcia_debug > 1) {
4154 		cmn_err(CE_CONT, "pcmcia_mfc_intr sockp=%p"
4155 		    " ls_inthandlers=%p\n"
4156 		    "\t ls_flags=0x%x PCS_IRQ_ENABLED=0x%x \n",
4157 		    (void *) sockp, (void *) sockp->ls_inthandlers,
4158 		    sockp->ls_flags, PCS_IRQ_ENABLED);
4159 	}
4160 #endif
4161 
4162 	if (sockp == NULL || sockp->ls_inthandlers == NULL ||
4163 	    !(sockp->ls_flags & PCS_IRQ_ENABLED))
4164 		return (DDI_INTR_UNCLAIMED);
4165 
4166 	mutex_enter(&sockp->ls_ilock);
4167 	for (done = 0, result = 0, first = intr = sockp->ls_inthandlers;
4168 	    intr != NULL && !done; intr = intr->next) {
4169 		result |= intr->intr(intr->arg1, intr->arg2);
4170 		if (intr->next == first)
4171 			done++;
4172 	}
4173 	if (intr == NULL) {
4174 		cmn_err(CE_WARN, "pcmcia_mfc_intr: bad MFC handler list");
4175 	}
4176 	if (sockp->ls_inthandlers)
4177 		sockp->ls_inthandlers = sockp->ls_inthandlers->next;
4178 
4179 	mutex_exit(&sockp->ls_ilock);
4180 	return (result ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
4181 }
4182 
4183 /*
4184  * pcmcia_power(dip)
4185  *	control power for nexus and children
4186  */
4187 int
4188 pcmcia_power(dev_info_t *dip, int component, int level)
4189 {
4190 #if 0
4191 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
4192 	int i;
4193 	/*
4194 	 * for now, we only have one component.  Should there be one per-socket?
4195 	 * the level is only one (power on or off)
4196 	 */
4197 	if (component != 0 || level > 1)
4198 		return (DDI_FAILURE);
4199 
4200 	for (i = 0; i < pcic->pc_numsockets; i++) {
4201 		if (pcic->pc_callback)
4202 			PC_CALLBACK(dip, pcic->pc_cb_arg,
4203 			    (level == 0) ? PCE_PM_SUSPEND :
4204 			    PCE_PM_RESUME,
4205 			    i);
4206 	}
4207 #else
4208 	cmn_err(CE_WARN, "pcmcia_power: component=%d, level=%d for %s",
4209 	    component, level, ddi_get_name_addr(dip));
4210 	return (DDI_FAILURE);
4211 #endif
4212 }
4213 
4214 void
4215 pcmcia_begin_resume(dev_info_t *dip)
4216 {
4217 	int i;
4218 	struct pcmcia_adapter *adapt = NULL;
4219 	for (i = 0; i < pcmcia_num_adapters; i++) {
4220 		if (pcmcia_adapters[i]->pca_dip == dip) {
4221 			adapt = pcmcia_adapters[i];
4222 			break;
4223 		}
4224 	}
4225 	if (adapt == NULL)
4226 		return;
4227 
4228 	for (i = 0; i < adapt->pca_numsockets; i++) {
4229 		int s;
4230 		s = adapt->pca_first_socket + i;
4231 		if (pcmcia_sockets[s]->ls_flags & PCS_SUSPENDED) {
4232 			if (pcmcia_sockets[s]->ls_flags &
4233 			    (1 << PCE_PM_RESUME)) {
4234 				(void) cs_event(PCE_PM_RESUME, s, 0);
4235 				pcm_event_manager(PCE_PM_RESUME, s, NULL);
4236 			}
4237 			(void) cs_event(PCE_CARD_REMOVAL, s, 0);
4238 			pcm_event_manager(PCE_CARD_REMOVAL, s, NULL);
4239 		}
4240 	}
4241 }
4242 
4243 /*
4244  * mark a cardbus card as "suspended" in the pcmcia module
4245  */
4246 void
4247 pcmcia_cb_suspended(int socket)
4248 {
4249 	mutex_enter(&pcmcia_global_lock);
4250 	pcmcia_sockets[socket]->ls_flags |= PCS_SUSPENDED;
4251 	mutex_exit(&pcmcia_global_lock);
4252 
4253 }
4254 
4255 /*
4256  * mark a cardbus card as "resumed" in the pcmcia module
4257  */
4258 void
4259 pcmcia_cb_resumed(int socket)
4260 {
4261 	if (pcmcia_sockets[socket]->ls_flags & PCS_SUSPENDED) {
4262 		mutex_enter(&pcmcia_global_lock);
4263 		pcmcia_sockets[socket]->ls_flags &= ~PCS_SUSPENDED;
4264 		cv_broadcast(&pcmcia_condvar);
4265 		mutex_exit(&pcmcia_global_lock);
4266 #ifdef PCMCIA_DEBUG
4267 		if (pcmcia_debug) {
4268 			cmn_err(CE_NOTE, "pcmcia_cb_resume RESUMED");
4269 		}
4270 #endif
4271 	}
4272 
4273 }
4274 
4275 void
4276 pcmcia_wait_insert(dev_info_t *dip)
4277 {
4278 	int i, f, tries, done;
4279 	clock_t tm;
4280 	struct pcmcia_adapter *adapt = NULL;
4281 	anp_t *nexus;
4282 
4283 	for (i = 0; i < pcmcia_num_adapters; i++) {
4284 		if (pcmcia_adapters[i]->pca_dip == dip) {
4285 			adapt = pcmcia_adapters[i];
4286 			break;
4287 		}
4288 	}
4289 	if (adapt == NULL)
4290 		return;
4291 
4292 	for (tries = adapt->pca_numsockets * 10; tries > 0; tries--) {
4293 		done = 1;
4294 		mutex_enter(&pcmcia_global_lock);
4295 		for (i = 0; i < adapt->pca_numsockets; i++) {
4296 			int s;
4297 			s = adapt->pca_first_socket + i;
4298 			for (f = 0; f < PCMCIA_MAX_FUNCTIONS; f++)
4299 				if (pcmcia_sockets[s] &&
4300 				    pcmcia_sockets[s]->ls_flags &
4301 				    PCS_SUSPENDED) {
4302 
4303 #ifdef PCMCIA_DEBUG
4304 					if (pcmcia_debug) {
4305 						cmn_err(CE_NOTE,
4306 						    "pcmcia_wait_insert: "
4307 						    "socket in SUSPENDED "
4308 						    "state");
4309 					}
4310 #endif
4311 					done = 0;
4312 					break;
4313 				}
4314 		}
4315 		if (!done) {
4316 			tm = ddi_get_lbolt();
4317 			(void) cv_timedwait(&pcmcia_condvar,
4318 			    &pcmcia_global_lock,
4319 			    tm + drv_usectohz(100000));
4320 		} else {
4321 			tries = 0;
4322 		}
4323 		mutex_exit(&pcmcia_global_lock);
4324 	}
4325 
4326 	if (tries == 0) {
4327 		cmn_err(CE_NOTE, "pcmcia_wait_insert timed out");
4328 	}
4329 
4330 	nexus = (anp_t *)ddi_get_driver_private(dip);
4331 	pcmcia_find_cards(nexus);
4332 }
4333 
4334 int
4335 pcmcia_map_reg(dev_info_t *pdip, dev_info_t *dip, ra_return_t *ra,
4336 		uint32_t state, caddr_t *base,
4337 		ddi_acc_handle_t *handle, ddi_device_acc_attr_t *attrib,
4338 		uint32_t req_base)
4339 {
4340 	struct pcmcia_parent_private *ppd;
4341 	int rnum = 0, type = PCMCIA_MAP_MEM;
4342 	ddi_map_req_t mr;
4343 	ddi_acc_hdl_t *hp;
4344 	int result;
4345 	struct regspec *reg;
4346 	ddi_device_acc_attr_t attr;
4347 
4348 	if (dip != NULL) {
4349 		ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
4350 		if (ppd == NULL)
4351 			return (DDI_FAILURE);
4352 		for (rnum = 1; rnum < ppd->ppd_nreg; rnum++) {
4353 			struct pcm_regs *p;
4354 			p = &ppd->ppd_reg[rnum];
4355 			if (state & WS_IO) {
4356 				/* need I/O */
4357 				type = PCMCIA_MAP_IO;
4358 				/*
4359 				 * We want to find an IO regspec. When we
4360 				 *	find one, it either has to match
4361 				 *	the caller's requested base address
4362 				 *	or it has to be relocatable.
4363 				 * We match on the requested base address
4364 				 *	rather than the allocated base
4365 				 *	address so that we handle the case
4366 				 *	of adapters that have IO window base
4367 				 *	relocation registers.
4368 				 */
4369 				if ((p->phys_hi &
4370 				    PC_REG_SPACE(PC_REG_SPACE_IO)) &&
4371 				    ((req_base == p->phys_lo) ||
4372 				    !(p->phys_hi & PC_REG_RELOC(1))))
4373 					break;
4374 			} else {
4375 				/* need memory */
4376 				type = PCMCIA_MAP_MEM;
4377 				if (p->phys_hi &
4378 				    PC_REG_SPACE(PC_REG_SPACE_MEMORY|
4379 				    PC_REG_SPACE_ATTRIBUTE))
4380 					break;
4381 			}
4382 		}
4383 		if (rnum >= ppd->ppd_nreg)
4384 			return (DDI_FAILURE);
4385 	} else if (state & WS_IO) {
4386 		return (DDI_FAILURE);
4387 	}
4388 
4389 	reg = kmem_zalloc(sizeof (pci_regspec_t), KM_SLEEP);
4390 	reg = pcmcia_cons_regspec(pdip, type, (uchar_t *)reg, ra);
4391 
4392 	if (attrib == NULL ||
4393 	    attrib->devacc_attr_version != DDI_DEVICE_ATTR_V0) {
4394 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4395 		attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
4396 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4397 	} else {
4398 		attr = *attrib;
4399 	}
4400 	/*
4401 	 * Allocate and initialize the common elements of data access handle.
4402 	 */
4403 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
4404 	hp = impl_acc_hdl_get(*handle);
4405 	hp->ah_vers = VERS_ACCHDL;
4406 	hp->ah_dip = dip != NULL ? dip : pdip;
4407 	hp->ah_rnumber = rnum;
4408 	hp->ah_offset = 0;
4409 	hp->ah_len = ra->ra_len;
4410 	hp->ah_acc = attr;
4411 
4412 	/*
4413 	 * Set up the mapping request and call to parent.
4414 	 */
4415 	mr.map_op = DDI_MO_MAP_LOCKED;
4416 	mr.map_type = DDI_MT_REGSPEC;
4417 	mr.map_obj.rp = reg;
4418 	mr.map_prot = PROT_READ | PROT_WRITE;
4419 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
4420 	mr.map_handlep = hp;
4421 	mr.map_vers = DDI_MAP_VERSION;
4422 
4423 	result = ddi_map(pdip, &mr, 0, ra->ra_len, base);
4424 	if (result != DDI_SUCCESS) {
4425 		impl_acc_hdl_free(*handle);
4426 		*handle = (ddi_acc_handle_t)NULL;
4427 	} else {
4428 		hp->ah_addr = *base;
4429 		if (mr.map_op == DDI_MO_UNMAP)
4430 			ra = NULL;
4431 		if (dip != NULL)
4432 			pcmcia_set_assigned(dip, rnum, ra);
4433 	}
4434 
4435 	kmem_free(reg, sizeof (pci_regspec_t));
4436 
4437 	return (result);
4438 }
4439 
4440 struct pcmcia_adapter *
4441 pcmcia_get_adapter(dev_info_t *dip)
4442 {
4443 	int i;
4444 
4445 	for (i = 0; i < pcmcia_num_adapters; i++) {
4446 		if (pcmcia_adapters[i] &&
4447 		    pcmcia_adapters[i]->pca_dip == dip) {
4448 			return (pcmcia_adapters[i]);
4449 		}
4450 	}
4451 	return (NULL);
4452 }
4453 
4454 
4455 void
4456 pcmcia_set_assigned(dev_info_t *dip, int rnum, ra_return_t *ret)
4457 {
4458 	struct pcmcia_parent_private *ppd;
4459 	struct pcm_regs *reg, *assign;
4460 
4461 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(dip);
4462 	if (ppd) {
4463 		reg = &ppd->ppd_reg[rnum];
4464 		assign = &ppd->ppd_assigned[rnum];
4465 		if (ret) {
4466 			if (assign->phys_hi == 0) {
4467 				assign->phys_hi = reg->phys_hi;
4468 				assign->phys_lo = ret->ra_addr_lo;
4469 				assign->phys_len = ret->ra_len;
4470 			} else if (assign->phys_lo != ret->ra_addr_lo) {
4471 #ifdef PCMCIA_DEBUG
4472 				cmn_err(CE_WARN, "pcmcia: bad address:"
4473 				    "%s=<%x,%x>",
4474 				    ddi_get_name_addr(dip),
4475 				    ret->ra_addr_lo, assign->phys_lo);
4476 #else
4477 				cmn_err(CE_WARN, "!pcmcia: bad address:"
4478 				    "%s=<%x,%x>",
4479 				    ddi_get_name_addr(dip),
4480 				    ret->ra_addr_lo, (int)assign->phys_lo);
4481 #endif
4482 			}
4483 			assign->phys_hi = PC_INCR_REFCNT(assign->phys_hi);
4484 		} else {
4485 			int i;
4486 			assign->phys_hi = PC_DECR_REFCNT(assign->phys_hi);
4487 			i = PC_GET_REG_REFCNT(assign->phys_hi);
4488 			if (i == 0) {
4489 				assign->phys_hi = 0;
4490 				assign->phys_lo = 0;
4491 				assign->phys_len = 0;
4492 			}
4493 		}
4494 	}
4495 }
4496 
4497 int
4498 pcmcia_alloc_mem(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
4499 		dev_info_t **res_dip)
4500 {
4501 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_MEM, res_dip));
4502 }
4503 
4504 int
4505 pcmcia_alloc_io(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
4506 		dev_info_t **res_dip)
4507 {
4508 	return (pcmcia_ra_alloc(dip, req, ret, NDI_RA_TYPE_IO, res_dip));
4509 }
4510 
4511 static boolean_t
4512 is_subtractv(dev_info_t *dip)
4513 {
4514 	uint_t  class;
4515 
4516 	if (dip == NULL)
4517 		return (B_FALSE);
4518 	class = ddi_getprop(DDI_DEV_T_ANY, dip,
4519 	    DDI_PROP_CANSLEEP|DDI_PROP_DONTPASS,
4520 	    "class-code", 0xff);
4521 	if (class == PPB_SUBTRACTIVE) {
4522 		return (B_TRUE);
4523 	}
4524 	return (B_FALSE);
4525 }
4526 
4527 /*
4528  * pcmcia_pci_alloc()
4529  * 	allocate mem or I/O resource from the ancestor of the cardbus bridge.
4530  * 	First start from the parent node. If the parent is a subtractive
4531  * 	decode bridge and it does not have the requested resource, go up the
4532  * 	device tree to find the resource.
4533  *
4534  * 	dip		the parent node of the cardbus bridge
4535  *
4536  * 	res_dip		returns a pointer to the node from which the
4537  * 			resource is obtained. *res_dip could point to
4538  * 			the parent or a higher level ancestor. *res_dip
4539  * 			should be saved by the caller and later passed
4540  * 			to pcmcia_ra_free();
4541  */
4542 int
4543 pcmcia_pci_alloc(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
4544 		char *type, dev_info_t **res_dip)
4545 {
4546 	uint64_t base = 0;
4547 	uint64_t len = 0;
4548 
4549 	if ((ndi_ra_alloc(dip, req, &base, &len, type, NDI_RA_PASS)
4550 	    == NDI_FAILURE) ||
4551 	    ((base >> 32) != 0)) {
4552 		if (is_subtractv(dip)) {
4553 			return (pcmcia_pci_alloc(ddi_get_parent(dip),
4554 			    req, ret, type, res_dip));
4555 
4556 		} else {
4557 			ret->ra_addr_hi = 0;
4558 			ret->ra_addr_lo = 0;
4559 			ret->ra_len = 0;
4560 			return (DDI_FAILURE);
4561 		}
4562 	}
4563 	ret->ra_addr_lo =  base & 0xffffffff;
4564 	ret->ra_addr_hi = 0;
4565 	ret->ra_len = len;
4566 	*res_dip = dip;
4567 	return (DDI_SUCCESS);
4568 }
4569 
4570 int
4571 pcmcia_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, ra_return_t *ret,
4572 		char *type, dev_info_t **res_dip)
4573 {
4574 	uint64_t base = 0;
4575 	uint64_t len = 0;
4576 
4577 	/*
4578 	 * Allocate space from busra resource list
4579 	 * should not return an address > 32 bits
4580 	 */
4581 
4582 	if ((ndi_ra_alloc(dip, req, &base, &len, type, NDI_RA_PASS)
4583 	    == NDI_FAILURE) ||
4584 	    ((base >> 32) != 0)) {
4585 		return (pcmcia_pci_alloc(ddi_get_parent(dip), req, ret,
4586 		    type, res_dip));
4587 	} else {
4588 		ret->ra_addr_lo =  base & 0xffffffff;
4589 		ret->ra_addr_hi = 0;
4590 		ret->ra_len = len;
4591 		*res_dip = dip;
4592 		return (DDI_SUCCESS);
4593 	}
4594 }
4595 
4596 int
4597 pcmcia_free_mem(dev_info_t *dip, ra_return_t *ret)
4598 {
4599 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_MEM));
4600 }
4601 
4602 int
4603 pcmcia_free_io(dev_info_t *dip, ra_return_t *ret)
4604 {
4605 	return (pcmcia_ra_free(dip, ret, NDI_RA_TYPE_IO));
4606 }
4607 
4608 int
4609 pcmcia_ra_free(dev_info_t *dip, ra_return_t *ret, char *type)
4610 {
4611 	if (dip == (dev_info_t *)-1)
4612 		return (DDI_FAILURE);
4613 	if (ndi_ra_free(dip, (uint64_t)ret->ra_addr_lo, (uint64_t)ret->ra_len,
4614 	    type, NDI_RA_PASS) == NDI_SUCCESS) {
4615 		return (DDI_SUCCESS);
4616 	} else {
4617 		return (DDI_FAILURE);
4618 	}
4619 }
4620 
4621 
4622 /*
4623  * when the low level device configuration does resource assignment
4624  * (devconf) then free the allocated resources so we can reassign them
4625  * later.  Walk the child list to get them.
4626  */
4627 void
4628 pcmcia_free_resources(dev_info_t *self)
4629 {
4630 	struct regspec *assigned;
4631 	int len;
4632 	dev_info_t *dip;
4633 	int circ;
4634 
4635 	ndi_devi_enter(self, &circ);
4636 	/* do searches in compatible property order */
4637 	for (dip = (dev_info_t *)DEVI(self)->devi_child;
4638 	    dip != NULL;
4639 	    dip = (dev_info_t *)DEVI(dip)->devi_sibling) {
4640 		len = 0;
4641 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
4642 		    DDI_PROP_DONTPASS|DDI_PROP_CANSLEEP,
4643 		    "assigned-addresses",
4644 		    (caddr_t)&assigned,
4645 		    &len) == DDI_PROP_SUCCESS) {
4646 			/*
4647 			 * if there are assigned resources at this point,
4648 			 * then the OBP or devconf have assigned them and
4649 			 * they need to be freed.
4650 			 */
4651 			kmem_free(assigned, len);
4652 		}
4653 	}
4654 	ndi_devi_exit(self, circ);
4655 }
4656 
4657 /*
4658  * this is the equivalent of pcm_get_intr using ra_allocs.
4659  * returns -1 if failed, otherwise returns the allocated irq.
4660  * The input request, if less than zero it means not a specific
4661  * irq requested. If larger then 0 then we are requesting that specific
4662  * irq
4663  */
4664 int
4665 pcmcia_get_intr(dev_info_t *dip, int request)
4666 {
4667 	ndi_ra_request_t req;
4668 	uint64_t base;
4669 	uint64_t len;
4670 	int err;
4671 
4672 	bzero(&req, sizeof (req));
4673 	base = 0;
4674 	len = 1;
4675 	if (request >= 0) {
4676 		req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
4677 		req.ra_len = 1;
4678 		req.ra_addr = (uint64_t)request;
4679 	}
4680 
4681 	req.ra_boundbase = 0;
4682 	req.ra_boundlen = 0xffffffffUL;
4683 	req.ra_flags |= NDI_RA_ALLOC_BOUNDED;
4684 
4685 	err = ndi_ra_alloc(dip, &req, &base, &len, NDI_RA_TYPE_INTR,
4686 	    NDI_RA_PASS);
4687 
4688 	if (err == NDI_FAILURE) {
4689 		return (-1);
4690 	} else {
4691 		return ((int)base);
4692 	}
4693 }
4694 
4695 
4696 int
4697 pcmcia_return_intr(dev_info_t *dip, int request)
4698 {
4699 	if ((ndi_ra_free(dip, (uint64_t)request, 1, NDI_RA_TYPE_INTR,
4700 	    NDI_RA_PASS)) == NDI_SUCCESS) {
4701 		return (0);
4702 	} else
4703 		return (-1);
4704 
4705 }
4706 
4707 #ifdef sparc
4708 
4709 int
4710 pcmcia_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
4711     ddi_intr_handle_impl_t *hdlp)
4712 {
4713 
4714 	struct pcmcia_parent_private *ppd;
4715 	pcmcia_logical_socket_t *sockp;
4716 	int socket, ret;
4717 	struct pcmcia_adapter *adapt;
4718 	set_irq_handler_t handler;
4719 	struct intrspec *pispec;
4720 
4721 #if defined(PCMCIA_DEBUG)
4722 	if (pcmcia_debug) {
4723 		cmn_err(CE_CONT,
4724 		    "pcmcia_add_intr_impl() entered "
4725 		    "dip=%p rdip=%p hdlp=%p \n",
4726 		    (void *)dip, (void *)rdip, (void *)hdlp);
4727 	}
4728 #endif
4729 
4730 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4731 	socket = ppd->ppd_socket;
4732 	sockp = pcmcia_sockets[socket];
4733 	adapt = sockp->ls_adapter;
4734 
4735 #if defined(PCMCIA_DEBUG)
4736 	if (pcmcia_debug) {
4737 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
4738 		    " ppd_flags=0X%x PPD_CARD_MULTI=0X%x\n"
4739 		    " ppd_intrspec=%p ls_inthandlers=%p\n",
4740 		    ppd->ppd_flags, PPD_CARD_MULTI,
4741 		    (void *) ppd->ppd_intrspec,
4742 		    (void *)sockp->ls_inthandlers);
4743 	}
4744 #endif
4745 
4746 	/*
4747 	 * calculate IPL level when we support multiple levels
4748 	 */
4749 	pispec = ppd->ppd_intrspec;
4750 	if (pispec == NULL) {
4751 		sockp->ls_error = BAD_IRQ;
4752 		return (DDI_FAILURE);
4753 	}
4754 
4755 	handler.socket = sockp->ls_socket;
4756 	handler.irq = 0;	/* default case */
4757 	handler.handler = (f_tt *)hdlp->ih_cb_func;
4758 	handler.arg1 = hdlp->ih_cb_arg1;
4759 	handler.arg2 = hdlp->ih_cb_arg2;
4760 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
4761 
4762 	/*
4763 	 * check if multifunction and do the right thing
4764 	 * we put an intercept in between the mfc handler and
4765 	 * us so we can catch and process.  We might be able
4766 	 * to optimize this depending on the card features
4767 	 * (a future option).
4768 	 */
4769 	if (ppd->ppd_flags & PPD_CARD_MULTI) {
4770 		inthandler_t *intr;
4771 		/*
4772 		 * note that the first function is a special
4773 		 * case since it sets things up.  We fall through
4774 		 * to the lower code and get the hardware set up.
4775 		 * subsequent times we just lock the list and insert
4776 		 * the handler and all is well.
4777 		 */
4778 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
4779 		if (intr == NULL) {
4780 			sockp->ls_error = BAD_IRQ;
4781 			return (DDI_FAILURE);
4782 		}
4783 		intr->intr = hdlp->ih_cb_func;
4784 		intr->handler_id = (uint_t)(uintptr_t)rdip;
4785 		intr->arg1 = hdlp->ih_cb_arg1;
4786 		intr->arg2 = hdlp->ih_cb_arg2;
4787 		intr->socket = socket;
4788 
4789 		mutex_enter(&sockp->ls_ilock);
4790 		if (sockp->ls_inthandlers == NULL) {
4791 			intr->next = intr->prev = intr;
4792 			sockp->ls_inthandlers = intr;
4793 			sockp->ls_mfintr_dip = rdip;
4794 			mutex_exit(&sockp->ls_ilock);
4795 
4796 			/*
4797 			 * replace first function handler with
4798 			 * the mfc handler
4799 			 */
4800 			handler.handler =  (f_tt *)pcmcia_mfc_intr;
4801 			handler.arg1 = (caddr_t)sockp;
4802 			handler.arg2 = NULL;
4803 		} else {
4804 			insque(intr, sockp->ls_inthandlers);
4805 			mutex_exit(&sockp->ls_ilock);
4806 
4807 			pispec->intrspec_vec = sockp->ls_intr_vec;
4808 			pispec->intrspec_pri = sockp->ls_intr_pri;
4809 			hdlp->ih_pri = sockp->ls_intr_pri;
4810 
4811 			return (DDI_SUCCESS);
4812 		}
4813 	}
4814 
4815 #if defined(PCMCIA_DEBUG)
4816 	if (pcmcia_debug) {
4817 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() let adapter do it\n");
4818 	}
4819 #endif
4820 	pispec->intrspec_func = (uint32_t (*)())handler.handler;
4821 
4822 	/* set default IPL then check for override */
4823 
4824 	pispec->intrspec_pri = sockp->ls_intr_pri;
4825 	hdlp->ih_pri = pispec->intrspec_pri;
4826 
4827 #if defined(PCMCIA_DEBUG)
4828 	if (pcmcia_debug) {
4829 		cmn_err(CE_CONT, "pcmcia_add_intr_impl() socket=%d irq=%d"
4830 		    " handler_id=0X%x handler=%p arg1=%p arg2=%p\n",
4831 		    handler.socket, handler.irq,
4832 		    handler.handler_id, (void *)handler.handler, handler.arg1,
4833 		    handler.arg2);
4834 	}
4835 #endif
4836 
4837 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
4838 	    SUCCESS) {
4839 		sockp->ls_error = ret;
4840 		return (DDI_FAILURE);
4841 	}
4842 
4843 #if defined(PCMCIA_DEBUG)
4844 	if (pcmcia_debug) {
4845 		cmn_err(CE_CONT, "pcmcia_add_intr_impl()"
4846 		    " iblk_cookie=%p idev_cookie=%p\n"
4847 		    " ls_flags=0X%x PCS_COOKIES_VALID=0X%x\n",
4848 		    (void *)handler.iblk_cookie,
4849 		    (void *)handler.idev_cookie,
4850 		    sockp->ls_flags, PCS_COOKIES_VALID);
4851 	}
4852 #endif
4853 
4854 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
4855 		hdlp->ih_pri = (uint_t)(uintptr_t)*handler.iblk_cookie;
4856 		sockp->ls_iblk = *handler.iblk_cookie;
4857 		sockp->ls_idev = *handler.idev_cookie;
4858 		sockp->ls_flags |= PCS_COOKIES_VALID;
4859 	}
4860 
4861 	return (DDI_SUCCESS);
4862 }
4863 
4864 void
4865 pcmcia_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
4866     ddi_intr_handle_impl_t *hdlp)
4867 {
4868 
4869 	struct pcmcia_parent_private *ppd;
4870 	pcmcia_logical_socket_t *sockp;
4871 	clear_irq_handler_t handler;
4872 	struct intrspec *pispec;
4873 	int socket;
4874 
4875 #if defined(PCMCIA_DEBUG)
4876 	if (pcmcia_debug) {
4877 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl() entered"
4878 		    " dip=%p rdip=%p hdlp=%p\n",
4879 		    (void *)dip, (void *)rdip, (void *)hdlp);
4880 	}
4881 #endif
4882 
4883 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
4884 	socket = ppd->ppd_socket;
4885 	sockp = pcmcia_sockets[socket];
4886 	pispec = ppd->ppd_intrspec;
4887 
4888 #if defined(PCMCIA_DEBUG)
4889 	if (pcmcia_debug) {
4890 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
4891 		    " ls_inthandlers=%p ls_intrspec=%p\n",
4892 		    (void *)sockp->ls_inthandlers,
4893 		    (void *)&sockp->ls_intrspec);
4894 	}
4895 #endif
4896 
4897 	/* first handle the multifunction case since it is simple */
4898 	mutex_enter(&sockp->ls_ilock);
4899 	if (sockp->ls_inthandlers != NULL) {
4900 		/* we must be MFC */
4901 		inthandler_t *intr;
4902 		int remhandler = 0;
4903 		intr = sockp->ls_inthandlers;
4904 
4905 		/* Check if there is only one handler left */
4906 		if ((intr->next == intr) && (intr->prev == intr)) {
4907 			if (intr->handler_id == (unsigned)(uintptr_t)rdip) {
4908 				sockp->ls_inthandlers = NULL;
4909 				remhandler++;
4910 				kmem_free(intr, sizeof (inthandler_t));
4911 			}
4912 		} else {
4913 			inthandler_t *first;
4914 			int done;
4915 
4916 			for (done = 0, first = intr; !done; intr = intr->next) {
4917 				if (intr->next == first)
4918 					done++;
4919 				if (intr->handler_id ==
4920 				    (unsigned)(uintptr_t)rdip) {
4921 					done++;
4922 
4923 					/*
4924 					 * If we're about to remove the
4925 					 *	handler at the head of
4926 					 *	the list, make the next
4927 					 *	handler in line the head.
4928 					 */
4929 					if (sockp->ls_inthandlers == intr)
4930 						sockp->ls_inthandlers =
4931 						    intr->next;
4932 
4933 					remque(intr);
4934 					kmem_free(intr, sizeof (inthandler_t));
4935 					break;
4936 				} /* handler_id */
4937 			} /* for */
4938 		} /* intr->next */
4939 
4940 		if (!remhandler) {
4941 			mutex_exit(&sockp->ls_ilock);
4942 			return;
4943 		}
4944 
4945 		/* need to get the dip that was used to add the handler */
4946 		rdip = sockp->ls_mfintr_dip;
4947 	}
4948 
4949 	mutex_exit(&sockp->ls_ilock);
4950 
4951 #if defined(PCMCIA_DEBUG)
4952 	if (pcmcia_debug) {
4953 		cmn_err(CE_CONT, "pcmcia_remove_intr_impl()"
4954 		    " pispec=%p rdip=%p\n",
4955 		    (void *)pispec, (void *)rdip);
4956 	}
4957 #endif
4958 
4959 	handler.socket = sockp->ls_socket;
4960 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
4961 	handler.handler = (f_tt *)pispec->intrspec_func;
4962 	CLEAR_IRQ(sockp->ls_if, dip, &handler);
4963 }
4964 
4965 
4966 /* Consolidated interrupt processing interface */
4967 /*ARGSUSED*/
4968 int
4969 pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
4970     ddi_intr_handle_impl_t *hdlp, void *result)
4971 {
4972 	int	ret = DDI_SUCCESS;
4973 
4974 #if defined(PCMCIA_DEBUG)
4975 	if (pcmcia_debug) {
4976 		cmn_err(CE_CONT, "pcmcia_intr_ops() intr_op=%d\n",
4977 		    (int)intr_op);
4978 	}
4979 #endif
4980 
4981 	switch (intr_op) {
4982 	case DDI_INTROP_GETCAP:
4983 		*(int *)result = DDI_INTR_FLAG_LEVEL;
4984 		break;
4985 	case DDI_INTROP_SETCAP:
4986 		ret = DDI_ENOTSUP;
4987 		break;
4988 	case DDI_INTROP_ALLOC:
4989 		*(int *)result = hdlp->ih_scratch1;
4990 		break;
4991 	case DDI_INTROP_FREE:
4992 		break;
4993 	case DDI_INTROP_GETPRI:
4994 		if (pcmcia_add_intr_impl(dip, rdip, hdlp) != DDI_SUCCESS)
4995 			return (DDI_FAILURE);
4996 		*(int *)result = hdlp->ih_pri;
4997 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
4998 		break;
4999 	case DDI_INTROP_SETPRI:
5000 		break;
5001 	case DDI_INTROP_ADDISR:
5002 		ret = pcmcia_add_intr_impl(dip, rdip, hdlp);
5003 		break;
5004 	case DDI_INTROP_REMISR:
5005 		pcmcia_remove_intr_impl(dip, rdip, hdlp);
5006 		break;
5007 	case DDI_INTROP_ENABLE:
5008 	case DDI_INTROP_DISABLE:
5009 		break;
5010 	case DDI_INTROP_NINTRS:
5011 	case DDI_INTROP_NAVAIL:
5012 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
5013 		break;
5014 	case DDI_INTROP_SUPPORTED_TYPES:
5015 		/* PCI nexus driver supports only fixed interrupts */
5016 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
5017 		    DDI_INTR_TYPE_FIXED : 0;
5018 		break;
5019 	default:
5020 		ret = DDI_ENOTSUP;
5021 		break;
5022 	}
5023 
5024 	return (ret);
5025 }
5026 
5027 #elif defined(__x86) || defined(__amd64)
5028 
5029 static struct intrspec	*pcmcia_intr_get_ispec(dev_info_t *, int,
5030 			    pcmcia_logical_socket_t **);
5031 static struct intrspec	*pcmcia_intr_add_isr(dev_info_t *, dev_info_t *,
5032 			    ddi_intr_handle_impl_t *);
5033 static int		pcmcia_intr_enable_isr(dev_info_t *, dev_info_t *,
5034 			    ddi_intr_handle_impl_t *);
5035 static void		pcmcia_intr_remove_isr(dev_info_t *, dev_info_t *,
5036 			    ddi_intr_handle_impl_t *);
5037 static void		pcmcia_intr_disable_isr(dev_info_t *, dev_info_t *,
5038 			    ddi_intr_handle_impl_t *);
5039 
5040 /*
5041  * pcmcia_intr_get_ispec:
5042  *	This is mostly copied from older 'pcmcia_get_intrspec' function
5043  */
5044 static struct intrspec *
5045 pcmcia_intr_get_ispec(dev_info_t *rdip, int inum,
5046     pcmcia_logical_socket_t **sockp)
5047 {
5048 	int				socket;
5049 	struct intrspec			*intrspec;
5050 	struct pcmcia_parent_private	*ppd;
5051 
5052 	if ((int)inum > 0 || (ddi_getprop(DDI_DEV_T_ANY, rdip,
5053 	    DDI_PROP_DONTPASS, "interrupts", -1) < 0))
5054 		return (NULL);
5055 
5056 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5057 	if (ppd == NULL || ppd->ppd_intrspec == NULL)
5058 		return (NULL);
5059 
5060 	if ((socket = ppd->ppd_socket) < 0)
5061 		return (NULL);
5062 
5063 	if ((*sockp = pcmcia_sockets[socket]) == NULL)
5064 		return (NULL);
5065 
5066 	intrspec = ppd->ppd_intrspec;
5067 	if (intrspec->intrspec_vec == 0 && (*sockp)->ls_intr_vec != 0)
5068 		intrspec->intrspec_vec = (*sockp)->ls_intr_vec;
5069 
5070 	return (intrspec);
5071 }
5072 
5073 static struct intrspec *
5074 pcmcia_intr_add_isr(dev_info_t *dip, dev_info_t *rdip,
5075     ddi_intr_handle_impl_t *hdlp)
5076 {
5077 	int				socket;
5078 	struct intrspec			*ispecp;
5079 	struct pcmcia_adapter		*adapt;
5080 	pcmcia_logical_socket_t		*sockp;
5081 	struct pcmcia_parent_private	*ppd;
5082 
5083 #if defined(PCMCIA_DEBUG)
5084 	if (pcmcia_debug)
5085 		cmn_err(CE_CONT, "pcmcia_intr_add_isr: "
5086 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5087 		    (void *)dip, (void *)rdip, (void *)hdlp);
5088 #endif	/* PCMCIA_DEBUG */
5089 
5090 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5091 	socket = ppd->ppd_socket;
5092 	sockp = pcmcia_sockets[socket];
5093 	adapt = sockp->ls_adapter;
5094 
5095 	ispecp = ppd->ppd_intrspec;
5096 	if (ispecp == NULL) {
5097 		sockp->ls_error = BAD_IRQ;
5098 		return (ispecp);
5099 	}
5100 
5101 	/*
5102 	 * check if multifunction and do the right thing
5103 	 * we put an intercept in between the mfc handler and us so we can
5104 	 * catch and process. We might be able to optimize this depending
5105 	 * on the card features (a future option).
5106 	 */
5107 	if (ppd->ppd_flags & PPD_CARD_MULTI &&
5108 	    hdlp->ih_cb_func != pcmcia_mfc_intr) {
5109 		inthandler_t *intr;
5110 
5111 		/*
5112 		 * note that the first function is a special case since it
5113 		 * sets things up.  We fall through to the lower code and
5114 		 * get the hardware set up. Subsequent times we just lock
5115 		 * the list and insert the handler and all is well.
5116 		 */
5117 		intr = kmem_zalloc(sizeof (inthandler_t), KM_NOSLEEP);
5118 		if (intr == NULL) {
5119 			sockp->ls_error = BAD_IRQ;
5120 			return (NULL);
5121 		}
5122 
5123 		intr->intr = (uint32_t (*)())hdlp->ih_cb_func;
5124 		intr->handler_id = (uint32_t)(uintptr_t)rdip;
5125 		intr->arg1 = hdlp->ih_cb_arg1;
5126 		intr->arg2 = hdlp->ih_cb_arg2;
5127 		intr->socket = socket;
5128 		mutex_enter(&sockp->ls_ilock);
5129 		if (sockp->ls_inthandlers == NULL) {
5130 			intr->next = intr->prev = intr;
5131 			sockp->ls_inthandlers = intr;
5132 			sockp->ls_mfintr_dip = rdip;
5133 		} else {
5134 			insque(intr, sockp->ls_inthandlers);
5135 		}
5136 		mutex_exit(&sockp->ls_ilock);
5137 		return (ispecp);
5138 	}
5139 
5140 	/*
5141 	 * Do we need to allocate an IRQ at this point or not?
5142 	 */
5143 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5144 		int i, irq;
5145 
5146 		/*
5147 		 * this adapter needs IRQ allocations
5148 		 * this is only necessary if it is the first function on the
5149 		 * card being setup. The socket will keep the allocation info
5150 		 */
5151 		/* all functions use same intrspec except mfc handler */
5152 		if (hdlp->ih_cb_func == pcmcia_mfc_intr) {
5153 			/*
5154 			 * We treat this special in order to allow things to
5155 			 * work properly for MFC cards. The intrspec for the
5156 			 * mfc dispatcher is intercepted and taken from the
5157 			 * logical socket in order to not be trying to
5158 			 * multiplex the meaning when ENABLE is called.
5159 			 */
5160 			ispecp = &sockp->ls_intrspec;
5161 			((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
5162 		}
5163 
5164 		if (adapt->pca_flags & PCA_IRQ_ISA) {
5165 			for (irq = -1, i = 1; irq == -1 && i < 16; i++) {
5166 				/* find available and usable IRQ level */
5167 				if (adapt->pca_avail_intr & (1 << i))
5168 					irq = pcmcia_get_intr(dip, i);
5169 			}
5170 		}
5171 		if (irq < 0) {
5172 			sockp->ls_error = NO_RESOURCE;
5173 			return (NULL);
5174 		}
5175 		hdlp->ih_vector = sockp->ls_intr_vec = irq;
5176 
5177 
5178 #if defined(PCMCIA_DEBUG)
5179 		if (pcmcia_debug)
5180 			cmn_err(CE_CONT, "allocated irq=%x\n", irq);
5181 #endif	/* PCMCIA_DEBUG */
5182 
5183 		ispecp->intrspec_vec = sockp->ls_intr_vec;
5184 		ispecp->intrspec_pri = sockp->ls_intr_pri;
5185 		return (ispecp);
5186 	}
5187 
5188 	if (ispecp->intrspec_func != NULL)
5189 		ispecp->intrspec_func = hdlp->ih_cb_func;
5190 
5191 	/* set default IPL then check for override */
5192 	ispecp->intrspec_pri = sockp->ls_intr_pri;
5193 	return (ispecp);
5194 }
5195 
5196 
5197 static int
5198 pcmcia_intr_enable_isr(dev_info_t *dip, dev_info_t *rdip,
5199     ddi_intr_handle_impl_t *hdlp)
5200 {
5201 	int				socket, ret;
5202 	int				irq = 0;	/* default case */
5203 	dev_info_t			*parent = ddi_root_node();
5204 	struct intrspec			*ispecp;
5205 	set_irq_handler_t		handler;
5206 	struct pcmcia_adapter		*adapt;
5207 	pcmcia_logical_socket_t		*sockp;
5208 	struct pcmcia_parent_private	*ppd;
5209 
5210 #if defined(PCMCIA_DEBUG)
5211 	if (pcmcia_debug)
5212 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr: "
5213 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5214 		    (void *)dip, (void *)rdip, (void *)hdlp);
5215 #endif	/* PCMCIA_DEBUG */
5216 
5217 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5218 	socket = ppd->ppd_socket;
5219 	sockp = pcmcia_sockets[socket];
5220 	adapt = sockp->ls_adapter;
5221 
5222 	ispecp = ppd->ppd_intrspec;
5223 	ASSERT(ispecp);
5224 
5225 	mutex_enter(&sockp->ls_ilock);
5226 	if ((sockp->ls_inthandlers != NULL) &&
5227 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5228 	    &sockp->ls_intrspec) {
5229 		inthandler_t *intr = sockp->ls_inthandlers;
5230 
5231 		ASSERT(ppd->ppd_flags & PPD_CARD_MULTI);
5232 
5233 		/* Only one handler. So, call ddi_add_intr on it */
5234 		if ((intr->next == intr) && (intr->prev == intr)) {
5235 			hdlp->ih_cb_func = pcmcia_mfc_intr;
5236 			hdlp->ih_cb_arg1 = (caddr_t)sockp;
5237 			hdlp->ih_cb_arg2 = NULL;
5238 
5239 			ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->
5240 			    bus_intr_op))(parent, rdip, DDI_INTROP_ENABLE,
5241 			    hdlp, NULL);
5242 
5243 			if (ret == DDI_FAILURE) {
5244 				sockp->ls_inthandlers = NULL;
5245 				kmem_free(intr, sizeof (inthandler_t));
5246 				sockp->ls_error = BAD_IRQ;
5247 				mutex_exit(&sockp->ls_ilock);
5248 				return (ret);
5249 			}
5250 		}
5251 		mutex_exit(&sockp->ls_ilock);
5252 		hdlp->ih_vector = ispecp->intrspec_vec = sockp->ls_intr_vec;
5253 		hdlp->ih_pri = sockp->ls_intr_pri;
5254 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
5255 		    sockp->ls_intr_pri;
5256 		sockp->ls_idev.idev_vector = (ushort_t)hdlp->ih_vector;
5257 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
5258 		return (DDI_SUCCESS);
5259 	}
5260 	mutex_exit(&sockp->ls_ilock);
5261 
5262 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5263 		if (hdlp->ih_cb_func == pcmcia_mfc_intr)
5264 			ispecp = (struct intrspec *)&sockp->ls_intrspec;
5265 
5266 		/* XXX: remove it later as this is done in _add_isr as well */
5267 		ispecp->intrspec_vec = sockp->ls_intr_vec;
5268 		ispecp->intrspec_pri = sockp->ls_intr_pri;
5269 
5270 		/* Enable interrupts */
5271 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
5272 		    parent, rdip, DDI_INTROP_ENABLE, hdlp, NULL);
5273 
5274 		sockp->ls_iblk = (ddi_iblock_cookie_t)(uintptr_t)
5275 		    sockp->ls_intr_pri;
5276 		sockp->ls_idev.idev_vector = (ushort_t)sockp->ls_intr_vec;
5277 		sockp->ls_idev.idev_priority = (ushort_t)sockp->ls_intr_pri;
5278 
5279 		if (ret != DDI_SUCCESS)
5280 			sockp->ls_error = BAD_IRQ;
5281 		return (ret);
5282 	}
5283 
5284 #if defined(PCMCIA_DEBUG)
5285 	if (pcmcia_debug)
5286 		cmn_err(CE_CONT, "pcmcia_intr_enable_isr; let adapter do it\n");
5287 #endif	/* PCMCIA_DEBUG */
5288 
5289 	handler.socket = sockp->ls_socket;
5290 	handler.irq = irq;
5291 	handler.handler = (f_tt *)hdlp->ih_cb_func;
5292 	handler.arg1 = hdlp->ih_cb_arg1;
5293 	handler.arg2 = hdlp->ih_cb_arg2;
5294 	handler.handler_id = (uint32_t)(uintptr_t)rdip;
5295 	if (ispecp->intrspec_func != NULL)
5296 		ispecp->intrspec_func = hdlp->ih_cb_func;
5297 
5298 	/* set default IPL then check for override */
5299 	ispecp->intrspec_pri = sockp->ls_intr_pri;
5300 
5301 	if ((ret = SET_IRQ(sockp->ls_if, adapt->pca_dip, &handler)) !=
5302 	    SUCCESS) {
5303 		sockp->ls_error = ret;
5304 		return (DDI_FAILURE);
5305 	}
5306 	ispecp->intrspec_func = hdlp->ih_cb_func;
5307 	if (!(sockp->ls_flags & PCS_COOKIES_VALID)) {
5308 		sockp->ls_iblk = *handler.iblk_cookie;
5309 		sockp->ls_idev = *handler.idev_cookie;
5310 		sockp->ls_flags |= PCS_COOKIES_VALID;
5311 	}
5312 	return (DDI_SUCCESS);
5313 }
5314 
5315 /* ARGSUSED */
5316 static void
5317 pcmcia_intr_remove_isr(dev_info_t *dip, dev_info_t *rdip,
5318     ddi_intr_handle_impl_t *hdlp)
5319 {
5320 	int				done, remhandler = 0;
5321 	inthandler_t			*intr, *first;
5322 	struct intrspec			*ispecp;
5323 	pcmcia_logical_socket_t		*sockp;
5324 
5325 #if defined(PCMCIA_DEBUG)
5326 	if (pcmcia_debug)
5327 		cmn_err(CE_CONT, "pcmcia_intr_remove_isr: "
5328 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5329 		    (void *)dip, (void *)rdip, (void *)hdlp);
5330 #endif	/* PCMCIA_DEBUG */
5331 
5332 	ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5333 	ASSERT(ispecp);
5334 
5335 	/* first handle the multifunction case since it is simple */
5336 	mutex_enter(&sockp->ls_ilock);
5337 	if (sockp->ls_inthandlers != NULL &&
5338 	    ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp !=
5339 	    &sockp->ls_intrspec) {
5340 
5341 		intr = sockp->ls_inthandlers;
5342 
5343 		/* Check if there is only one handler left */
5344 		if ((intr->next == intr) && (intr->prev == intr)) {
5345 			if (intr->handler_id == (uint32_t)(uintptr_t)rdip) {
5346 				sockp->ls_inthandlers = NULL;
5347 				remhandler++;
5348 				kmem_free(intr, sizeof (inthandler_t));
5349 			}
5350 
5351 		} else {
5352 			for (done = 0, first = intr; !done; intr = intr->next) {
5353 				if (intr->next == first)
5354 					done++;
5355 				if (intr->handler_id ==
5356 				    (uint32_t)(uintptr_t)rdip) {
5357 					done++;
5358 
5359 					/*
5360 					 * If we're about to remove the handler
5361 					 * at the head of the list, make the
5362 					 * next handler in line the head.
5363 					 */
5364 					if (sockp->ls_inthandlers == intr)
5365 						sockp->ls_inthandlers =
5366 						    intr->next;
5367 
5368 					remque(intr);
5369 					kmem_free(intr, sizeof (inthandler_t));
5370 					break;
5371 				} /* handler_id */
5372 			} /* end of for */
5373 		} /* end of if intr->next */
5374 
5375 		if (!remhandler) {
5376 			mutex_exit(&sockp->ls_ilock);
5377 			return;
5378 		}
5379 	}
5380 	mutex_exit(&sockp->ls_ilock);
5381 
5382 	if (sockp->ls_adapter->pca_flags & PCA_RES_NEED_IRQ) {
5383 		sockp->ls_intr_vec = 0;
5384 		ispecp->intrspec_vec = 0;
5385 	}
5386 }
5387 
5388 
5389 static void
5390 pcmcia_intr_disable_isr(dev_info_t *dip, dev_info_t *rdip,
5391     ddi_intr_handle_impl_t *hdlp)
5392 {
5393 	int				socket, ret;
5394 	dev_info_t			*parent;
5395 	struct intrspec			*ispecp;
5396 	clear_irq_handler_t		handler;
5397 	struct pcmcia_adapter		*adapt;
5398 	pcmcia_logical_socket_t		*sockp;
5399 	struct pcmcia_parent_private	*ppd;
5400 	ihdl_plat_t			*ihdl_plat_datap =
5401 	    (ihdl_plat_t *)hdlp->ih_private;
5402 
5403 #if defined(PCMCIA_DEBUG)
5404 	if (pcmcia_debug)
5405 		cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5406 		    "dip=0x%p rdip=0x%p hdlp=0x%p\n",
5407 		    (void *)dip, (void *)rdip, (void *)hdlp);
5408 #endif	/* PCMCIA_DEBUG */
5409 
5410 	ppd = (struct pcmcia_parent_private *)ddi_get_parent_data(rdip);
5411 	socket = ppd->ppd_socket;
5412 	sockp = pcmcia_sockets[socket];
5413 	adapt = sockp->ls_adapter;
5414 	ispecp = ppd->ppd_intrspec;
5415 	ASSERT(ispecp);
5416 
5417 	mutex_enter(&sockp->ls_ilock);
5418 	if (sockp->ls_inthandlers != NULL &&
5419 	    ihdl_plat_datap->ip_ispecp != &sockp->ls_intrspec) {
5420 		inthandler_t	*intr = sockp->ls_inthandlers;
5421 
5422 		/* Check if there is only one handler left */
5423 		if ((intr->next == intr) && (intr->prev == intr)) {
5424 			if (intr->handler_id != (uint32_t)(uintptr_t)rdip)
5425 				/*
5426 				 * need to get the dip that was
5427 				 * used to add the handler
5428 				 */
5429 				rdip = sockp->ls_mfintr_dip;
5430 				ispecp = (struct intrspec *)&sockp->ls_intrspec;
5431 		} else {
5432 			/* Don't call cleanup if list still has members */
5433 			mutex_exit(&sockp->ls_ilock);
5434 			return;
5435 		}
5436 	}
5437 	mutex_exit(&sockp->ls_ilock);
5438 
5439 	if (ihdl_plat_datap->ip_ispecp ==
5440 	    (struct intrspec *)&sockp->ls_intrspec)
5441 		ispecp = ihdl_plat_datap->ip_ispecp;
5442 
5443 	if (adapt->pca_flags & PCA_RES_NEED_IRQ) {
5444 		ret = ispecp->intrspec_vec;
5445 		parent = ddi_root_node();
5446 		ret = (*(DEVI(parent)->devi_ops->devo_bus_ops->bus_intr_op))(
5447 		    parent, rdip, DDI_INTROP_DISABLE, hdlp, NULL);
5448 		(void) pcmcia_return_intr(dip, hdlp->ih_vector);
5449 #if defined(PCMCIA_DEBUG)
5450 		if (pcmcia_debug)
5451 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5452 			    "INTROP_DISABLE returned %x\n", ret);
5453 #endif	/* PCMCIA_DEBUG */
5454 	} else {
5455 		handler.socket = sockp->ls_socket;
5456 		handler.handler_id = (uint32_t)(uintptr_t)rdip;
5457 		handler.handler = (f_tt *)ispecp->intrspec_func;
5458 		ret = CLEAR_IRQ(sockp->ls_if, dip, &handler);
5459 #if defined(PCMCIA_DEBUG)
5460 		if (pcmcia_debug)
5461 			cmn_err(CE_CONT, "pcmcia_intr_disable_isr: "
5462 			    "CLEAR_IRQ returned %x\n", ret);
5463 #endif	/* PCMCIA_DEBUG */
5464 	}
5465 }
5466 
5467 /* Consolidated interrupt processing interface */
5468 int
5469 pcmcia_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
5470     ddi_intr_handle_impl_t *hdlp, void *result)
5471 {
5472 	struct intrspec		*ispecp;
5473 	pcmcia_logical_socket_t	*sockp;
5474 
5475 #if defined(PCMCIA_DEBUG)
5476 	if (pcmcia_debug)
5477 		cmn_err(CE_CONT, "pcmcia_intr_ops: "
5478 		    "dip=0x%p rdip=0x%p op=0x%x hdlp=0x%p\n",
5479 		    (void *)dip, (void *)rdip, intr_op, (void *)hdlp);
5480 #endif	/* PCMCIA_DEBUG */
5481 
5482 	switch (intr_op) {
5483 	case DDI_INTROP_SUPPORTED_TYPES:
5484 		if (ddi_get_parent_data(rdip) == NULL) {
5485 			*(int *)result = 0;
5486 			return (DDI_FAILURE);
5487 		}
5488 		*(int *)result = DDI_INTR_TYPE_FIXED;
5489 		break;
5490 	case DDI_INTROP_GETCAP:
5491 		*(int *)result = DDI_INTR_FLAG_LEVEL;
5492 		break;
5493 	case DDI_INTROP_NINTRS:
5494 	case DDI_INTROP_NAVAIL:
5495 		if (i_ddi_get_intx_nintrs(rdip) == 0) {
5496 			*(int *)result = 0;
5497 			return (DDI_FAILURE);
5498 		}
5499 		*(int *)result = 1;	/* for PCMCIA there is only one intr */
5500 		break;
5501 	case DDI_INTROP_ALLOC:
5502 		if ((ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum,
5503 		    &sockp)) == NULL)
5504 			return (DDI_FAILURE);
5505 		*(int *)result = hdlp->ih_scratch1;
5506 		break;
5507 	case DDI_INTROP_FREE:
5508 		break;
5509 	case DDI_INTROP_GETPRI:
5510 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5511 		if (ispecp == NULL) {
5512 			*(int *)result = 0;
5513 			return (DDI_FAILURE);
5514 		}
5515 
5516 		*(int *)result = ispecp->intrspec_pri = sockp->ls_intr_pri;
5517 		break;
5518 	case DDI_INTROP_SETPRI:
5519 		if (*(int *)result > LOCK_LEVEL)
5520 			return (DDI_FAILURE);
5521 		ispecp = pcmcia_intr_get_ispec(rdip, hdlp->ih_inum, &sockp);
5522 		ASSERT(ispecp);
5523 		ispecp->intrspec_pri = sockp->ls_intr_pri = *(int *)result;
5524 		break;
5525 	case DDI_INTROP_ADDISR:
5526 		if ((ispecp = pcmcia_intr_add_isr(dip, rdip, hdlp)) == NULL)
5527 			return (DDI_FAILURE);
5528 		((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp = ispecp;
5529 		break;
5530 	case DDI_INTROP_REMISR:
5531 		pcmcia_intr_remove_isr(dip, rdip, hdlp);
5532 		break;
5533 	case DDI_INTROP_ENABLE:
5534 		if (pcmcia_intr_enable_isr(dip, rdip, hdlp) != DDI_SUCCESS)
5535 			return (DDI_FAILURE);
5536 		break;
5537 	case DDI_INTROP_DISABLE:
5538 		pcmcia_intr_disable_isr(dip, rdip, hdlp);
5539 		break;
5540 	default:
5541 		return (DDI_ENOTSUP);
5542 	}
5543 
5544 	return (DDI_SUCCESS);
5545 }
5546 #endif
5547