xref: /titanic_41/usr/src/uts/common/io/cardbus/cardbus.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c)  * Copyright (c) 2001 Tadpole Technology plc
28  * All rights reserved.
29  * From "@(#)pcicfg.c   1.31    99/06/18 SMI"
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 /*
35  * Cardbus module
36  */
37 
38 #include <sys/conf.h>
39 #include <sys/modctl.h>
40 
41 #include <sys/pci.h>
42 
43 #include <sys/ddi.h>
44 #include <sys/sunndi.h>
45 #include <sys/ddi_impldefs.h>
46 
47 #include <sys/hotplug/hpcsvc.h>
48 
49 #include <sys/pctypes.h>
50 #include <sys/pcmcia.h>
51 #include <sys/sservice.h>
52 #include <sys/note.h>
53 
54 #include <sys/pci/pci_types.h>
55 #include <sys/pci/pci_sc.h>
56 
57 #include <sys/pcic_reg.h>
58 #include <sys/pcic_var.h>
59 #include <sys/pcmcia.h>
60 
61 #ifdef sparc
62 #include <sys/ddi_subrdefs.h>
63 #elif defined(__x86) || defined(__amd64)
64 #include <sys/pci_intr_lib.h>
65 #include <sys/mach_intr.h>
66 #endif
67 
68 #include "cardbus.h"
69 #include "cardbus_parse.h"
70 #include "cardbus_hp.h"
71 #include "cardbus_cfg.h"
72 
73 static int cardbus_command_default = PCI_COMM_SERR_ENABLE |
74 				PCI_COMM_WAIT_CYC_ENAB |
75 				PCI_COMM_PARITY_DETECT |
76 				PCI_COMM_ME | PCI_COMM_MAE |
77 				PCI_COMM_IO;
78 
79 static int cardbus_next_instance = 0;
80 static int cardbus_count = 0;
81 int number_of_cardbus_cards = 0;
82 
83 static int cardbus_bus_map(dev_info_t *dip, dev_info_t *rdip,
84 		ddi_map_req_t *mp, off_t offset, off_t len, caddr_t *vaddrp);
85 static void pcirp2rp(const pci_regspec_t *pci_rp, struct regspec *rp);
86 
87 static int cardbus_ctlops(dev_info_t *, dev_info_t *,
88 			ddi_ctl_enum_t, void *arg, void *);
89 static void cardbus_init_child_regs(dev_info_t *child);
90 static int cardbus_initchild(dev_info_t *, dev_info_t *,
91 			dev_info_t *, void *);
92 static int cardbus_name_child(dev_info_t *, char *, int);
93 static void cardbus_removechild(dev_info_t *dip);
94 
95 static int cardbus_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
96 		ddi_dma_attr_t *attr, int (*waitfp)(caddr_t), caddr_t arg,
97 		ddi_dma_handle_t *handlep);
98 static int cardbus_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
99 		ddi_dma_handle_t handle);
100 static int cardbus_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
101 		ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
102 		ddi_dma_cookie_t *cp, uint_t *ccountp);
103 static int cardbus_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
104 		ddi_dma_handle_t handle);
105 static int cardbus_dma_flush(dev_info_t *dip, dev_info_t *rdip,
106 		ddi_dma_handle_t handle, off_t off, size_t len,
107 		uint_t cache_flags);
108 static int cardbus_dma_win(dev_info_t *dip, dev_info_t *rdip,
109 		ddi_dma_handle_t handle, uint_t win, off_t *offp,
110 		size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp);
111 static int cardbus_dma_map(dev_info_t *dip, dev_info_t *rdip,
112 		struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep);
113 
114 static int cardbus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
115 		ddi_prop_op_t prop_op, int mod_flags,
116 		char *name, caddr_t valuep, int *lengthp);
117 
118 static int cardbus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
119 		char *eventname, ddi_eventcookie_t *cookiep);
120 static int cardbus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
121 		ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
122 		ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
123 		void *arg, ddi_callback_id_t *cb_id);
124 static int cardbus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id);
125 static int cardbus_post_event(dev_info_t *dip, dev_info_t *rdip,
126 		ddi_eventcookie_t cookie, void *bus_impldata);
127 
128 static int cardbus_intr_ops(dev_info_t *dip, dev_info_t *rdip,
129 		ddi_intr_op_t intr_op,
130 		ddi_intr_handle_impl_t *hdlp, void *result);
131 
132 static int check_token(char *token, int *len);
133 static char *find_token(char **cp, int *l, char *endc);
134 static int parse_token(char *token);
135 static int token_to_hex(char *token, unsigned *val, int len);
136 static int token_to_dec(char *token, unsigned *val, int len);
137 static void cardbus_add_prop(struct cb_deviceset_props *cdsp, int type,
138 		char *name, caddr_t vp, int len);
139 static void cardbus_add_stringprop(struct cb_deviceset_props *cdsp,
140 		char *name, char *vp, int len);
141 static void cardbus_prop_free(ddi_prop_t *propp);
142 static void cardbus_devprops_free(struct cb_deviceset_props *cbdp);
143 static int cardbus_parse_devprop(cbus_t *cbp, char *cp);
144 static void cardbus_device_props(cbus_t *cbp);
145 
146 static void cardbus_expand_busrange(dev_info_t *dip);
147 
148 static int cardbus_convert_properties(dev_info_t *dip);
149 static void cardbus_revert_properties(dev_info_t *dip);
150 
151 /*
152  * driver global data
153  */
154 kmutex_t cardbus_list_mutex; /* Protects the probe handle list */
155 void *cardbus_state;
156 int cardbus_latency_timer = 0x40;
157 int cardbus_debug = 0;
158 
159 /*
160  * Module linkage information for the kernel.
161  */
162 extern struct mod_ops mod_miscops;
163 static struct modlmisc modlmisc = {
164 	&mod_miscops,
165 	"Cardbus Configurator support %I%",
166 };
167 
168 static struct modlinkage modlinkage = {
169 	MODREV_1,
170 	&modlmisc,
171 	NULL
172 };
173 
174 int
175 _init(void)
176 {
177 	int error;
178 
179 	error =  ddi_soft_state_init(&cardbus_state, sizeof (cbus_t), 0);
180 	if (error != 0)
181 		return (error);
182 
183 	mutex_init(&cardbus_list_mutex, NULL, MUTEX_DRIVER, NULL);
184 	if ((error = mod_install(&modlinkage)) != 0) {
185 		mutex_destroy(&cardbus_list_mutex);
186 	}
187 
188 	return (error);
189 }
190 
191 int
192 _fini(void)
193 {
194 	int error;
195 	if ((error = mod_remove(&modlinkage)) == 0) {
196 		mutex_destroy(&cardbus_list_mutex);
197 		ddi_soft_state_fini(&cardbus_state);
198 	}
199 	return (error);
200 }
201 
202 int
203 _info(struct modinfo *modinfop)
204 {
205 	return (mod_info(&modlinkage, modinfop));
206 }
207 
208 static
209 struct bus_ops cardbusbus_ops = {
210 	BUSO_REV,
211 	cardbus_bus_map,
212 	NULL,
213 	NULL,
214 	NULL,
215 	i_ddi_map_fault,
216 	cardbus_dma_map,
217 	cardbus_dma_allochdl,
218 	cardbus_dma_freehdl,
219 	cardbus_dma_bindhdl,
220 	cardbus_dma_unbindhdl,
221 	cardbus_dma_flush,
222 	cardbus_dma_win,
223 	ddi_dma_mctl,
224 	cardbus_ctlops,			/* (*bus_ctl)();		*/
225 	cardbus_prop_op,
226 	cardbus_get_eventcookie,	/* (*bus_get_eventcookie)();	*/
227 	cardbus_add_eventcall,		/* (*bus_add_eventcall)();	*/
228 	cardbus_remove_eventcall,	/* (*bus_remove_eventcall)();	*/
229 	cardbus_post_event,		/* (*bus_post_event)();		*/
230 	NULL,				/* (*bus_intr_ctl)();		*/
231 	NULL,				/* (*bus_config)();		*/
232 	NULL,				/* (*bus_unconfig)();		*/
233 	NULL,				/* (*bus_fm_init)();		*/
234 	NULL,				/* (*bus_fm_fini)();		*/
235 	NULL,				/* (*bus_enter)();		*/
236 	NULL,				/* (*bus_exit)();		*/
237 	NULL,				/* (*bus_power)();		*/
238 	cardbus_intr_ops		/* (*bus_intr_op)();		*/
239 };
240 
241 #define	CB_EVENT_TAG_INSERT	0
242 #define	CB_EVENT_TAG_REMOVE	1
243 
244 static ndi_event_definition_t cb_ndi_event_defs[] = {
245 	{ CB_EVENT_TAG_INSERT, DDI_DEVI_INSERT_EVENT, EPL_INTERRUPT, 0 },
246 	{ CB_EVENT_TAG_REMOVE, DDI_DEVI_REMOVE_EVENT, EPL_INTERRUPT, 0 }
247 };
248 
249 #define	CB_N_NDI_EVENTS \
250 	(sizeof (cb_ndi_event_defs) / sizeof (cb_ndi_event_defs[0]))
251 
252 #ifdef sparc
253 struct busnum_ctrl {
254 	int	rv;
255 	dev_info_t *dip;
256 	cardbus_bus_range_t *range;
257 };
258 
259 static int
260 cardbus_claim_pci_busnum(dev_info_t *dip, void *arg)
261 {
262 	cardbus_bus_range_t pci_bus_range;
263 	struct busnum_ctrl *ctrl;
264 	ndi_ra_request_t req;
265 	char bus_type[16] = "(unknown)";
266 	int len;
267 	uint64_t base;
268 	uint64_t retlen;
269 
270 	ctrl = (struct busnum_ctrl *)arg;
271 
272 	/* check if this is a PCI bus node */
273 	len = sizeof (bus_type);
274 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
275 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
276 	    "device_type",
277 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
278 		return (0);	/* (DDI_WALK_PRUNECHILD); */
279 
280 	if ((strcmp(bus_type, "pci") != 0) &&
281 	    (strcmp(bus_type, "pciex") != 0)) /* it is not a pci bus type */
282 		return (0);	/* (DDI_WALK_PRUNECHILD); */
283 
284 	/* look for the bus-range property */
285 	len = sizeof (struct cardbus_bus_range);
286 	if (ddi_getlongprop_buf(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
287 	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
288 		cardbus_err(dip, 1, "cardbus_claim_pci_busnum: %u -> %u \n",
289 			pci_bus_range.lo, pci_bus_range.hi);
290 		if ((pci_bus_range.lo >= ctrl->range->lo) &&
291 		    (pci_bus_range.hi <= ctrl->range->hi)) {
292 			cardbus_err(dip, 1,
293 			    "cardbus_claim_pci_busnum: claim %u -> %u \n",
294 			    pci_bus_range.lo, pci_bus_range.hi);
295 
296 			/* claim the bus range from the bus resource map */
297 			bzero((caddr_t)&req, sizeof (req));
298 			req.ra_addr = (uint64_t)pci_bus_range.lo;
299 			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
300 			req.ra_len = (uint64_t)pci_bus_range.hi -
301 			    (uint64_t)pci_bus_range.lo + 1;
302 
303 			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
304 			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
305 				return (0);	/* (DDI_WALK_PRUNECHILD); */
306 		}
307 	}
308 
309 	/*
310 	 * never Error return.
311 	 */
312 	ctrl->rv = DDI_SUCCESS;
313 	return (DDI_WALK_TERMINATE);
314 }
315 
316 static void
317 cardbus_walk_node_child(dev_info_t *parent,
318 	int (*f)(dev_info_t *, void *), void *arg)
319 {
320 	dev_info_t *dip;
321 	int ret;
322 
323 	for (dip = ddi_get_child(parent); dip;
324 	    dip = ddi_get_next_sibling(dip)) {
325 
326 		ret = (*f) (dip, arg);
327 		if (ret)
328 			return;
329 	}
330 }
331 
332 static void cardbus_fix_hostbridge_busrange(dev_info_t *dip)
333 {
334 	cardbus_bus_range_t bus_range;
335 	struct busnum_ctrl ctrl;
336 
337 	uint64_t next_bus;
338 	uint64_t blen;
339 	ndi_ra_request_t req;
340 	int	len;
341 
342 	cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange\n");
343 
344 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
345 	req.ra_len = 1;
346 	if (ndi_ra_alloc(dip, &req,
347 	    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
348 	    0) != NDI_SUCCESS) {
349 		(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);
350 
351 		if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM)
352 		    == NDI_FAILURE) {
353 			cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
354 			    "NDI_RA_TYPE_PCI_BUSNUM setup fail\n");
355 			return;
356 		}
357 
358 		bus_range.lo = 0;
359 		(void) ddi_getlongprop_buf(DDI_DEV_T_NONE, dip,
360 		DDI_PROP_DONTPASS, "bus-range", (caddr_t)&bus_range, &len);
361 		bus_range.hi = 255;
362 
363 		(void) ndi_ra_free(dip,
364 		    (uint64_t)bus_range.lo + 1,
365 		    (uint64_t)bus_range.hi - (uint64_t)bus_range.lo,
366 		    NDI_RA_TYPE_PCI_BUSNUM, 0);
367 
368 		ctrl.rv = DDI_SUCCESS;
369 		ctrl.dip = dip;
370 		ctrl.range = &bus_range;
371 
372 		cardbus_walk_node_child(dip, cardbus_claim_pci_busnum,
373 		    (void*)&ctrl);
374 
375 		if (ctrl.rv != DDI_SUCCESS)
376 			cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
377 			    "cardbus_walk_node_child fails\n");
378 
379 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
380 			    "bus-range", (int *)&bus_range, 2);
381 
382 	} else {
383 		cardbus_err(dip, 1, "cardbus_fix_hostbridge_busrange "
384 		    "already set up %x\n", (int)next_bus);
385 		(void) ndi_ra_free(dip, next_bus, (uint64_t)1,
386 		    NDI_RA_TYPE_PCI_BUSNUM, 0);
387 	}
388 }
389 
390 static dev_info_t *
391 cardbus_find_hsbridge_dip(dev_info_t *dip)
392 {
393 	dev_info_t *pdip;
394 
395 	pdip = ddi_get_parent(dip);
396 	while (pdip) {
397 		if (ddi_get_parent(pdip) == ddi_root_node())
398 			break;
399 		pdip = ddi_get_parent(pdip);
400 	}
401 
402 	return (pdip);
403 }
404 #endif /* sparc */
405 
406 /*
407  * Attach a device to the cardbus infrastructure.
408  */
409 int
410 cardbus_attach(dev_info_t *dip, cb_nexus_cb_t *nex_ops)
411 {
412 	cbus_t *cbp;
413 	int cb_instance;
414 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
415 	struct dev_info *devi = DEVI(dip);
416 
417 	mutex_enter(&cardbus_list_mutex);
418 
419 	/*
420 	 * Make sure that it is not already initialized.
421 	 */
422 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
423 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
424 	    "cbus-instance") == 1) {
425 		cmn_err(CE_WARN,
426 		    "%s%d: cardbus instance already initialized!\n",
427 		    ddi_driver_name(dip), ddi_get_instance(dip));
428 		    mutex_exit(&cardbus_list_mutex);
429 		return (DDI_FAILURE);
430 	}
431 
432 	/*
433 	 * initialize soft state structure for the bus instance.
434 	 */
435 	cb_instance = cardbus_next_instance++;
436 
437 	if (ddi_soft_state_zalloc(cardbus_state, cb_instance) != DDI_SUCCESS) {
438 		cmn_err(CE_WARN, "%s%d: can't allocate cardbus soft state\n",
439 		    ddi_driver_name(dip), ddi_get_instance(dip));
440 		mutex_exit(&cardbus_list_mutex);
441 		return (DDI_FAILURE);
442 	}
443 
444 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
445 	cbp->cb_instance = cb_instance;
446 	cbp->cb_dip = dip;
447 	mutex_init(&cbp->cb_mutex, NULL, MUTEX_DRIVER, NULL);
448 
449 	/*
450 	 * Save the instance number of the soft state structure for
451 	 * this bus as a devinfo property.
452 	 */
453 	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
454 	    "cbus-instance", (caddr_t)&cb_instance,
455 	    sizeof (cb_instance)) != DDI_SUCCESS) {
456 		cmn_err(CE_WARN,
457 		    "%s%d: failed to add the property 'cbus-instance'",
458 		    ddi_driver_name(dip), ddi_get_instance(dip));
459 		ddi_soft_state_free(cardbus_state, cb_instance);
460 		mutex_exit(&cardbus_list_mutex);
461 		return (DDI_FAILURE);
462 	}
463 
464 	cbp->cb_nex_ops = nex_ops;
465 	/*
466 	 * TODO - Should probably be some sort of locking on the devinfo here.
467 	 */
468 	cbp->orig_dopsp = devi->devi_ops;
469 	cbp->orig_bopsp = devi->devi_ops->devo_bus_ops;
470 	cbp->cb_dops = *devi->devi_ops;
471 	devi->devi_ops = &cbp->cb_dops;
472 
473 	if (ndi_event_alloc_hdl(dip, *anp->an_iblock, &cbp->cb_ndi_event_hdl,
474 	    NDI_SLEEP) == NDI_SUCCESS) {
475 		cbp->cb_ndi_events.ndi_n_events = CB_N_NDI_EVENTS;
476 		cbp->cb_ndi_events.ndi_events_version = NDI_EVENTS_REV1;
477 		cbp->cb_ndi_events.ndi_event_defs = cb_ndi_event_defs;
478 		if (ndi_event_bind_set(cbp->cb_ndi_event_hdl,
479 		    &cbp->cb_ndi_events,
480 		    NDI_SLEEP) != NDI_SUCCESS) {
481 			cardbus_err(dip, 1,
482 			    "cardbus_attach: ndi_event_bind_set failed\n");
483 		}
484 	}
485 
486 	/*
487 	 * Check for device initialization property.
488 	 */
489 	cardbus_device_props(cbp);
490 
491 	if (cardbus_init_hotplug(cbp) != DDI_SUCCESS) {
492 		ddi_soft_state_free(cardbus_state, cb_instance);
493 		mutex_exit(&cardbus_list_mutex);
494 		return (DDI_FAILURE);
495 	}
496 
497 #ifdef sparc
498 	/* a hack to fix the bus-range problem on pci root nodes */
499 	{
500 		dev_info_t *hs_dip;
501 
502 		hs_dip = cardbus_find_hsbridge_dip(dip);
503 		cardbus_fix_hostbridge_busrange(hs_dip);
504 	}
505 #endif
506 
507 	cardbus_expand_busrange(dip);
508 	cardbus_count++;
509 	mutex_exit(&cardbus_list_mutex);
510 	return (DDI_SUCCESS);
511 }
512 
513 #ifdef TODO
514 static int
515 cardbus_detach(dev_info_t *dip)
516 {
517 	int cb_instance;
518 	cbus_t *cbp;
519 
520 	mutex_enter(&cardbus_list_mutex);
521 	/* get the instance number for the cardbus soft state data */
522 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
523 			DDI_PROP_DONTPASS, "cbus-instance", -1);
524 	if (cb_instance < 0) {
525 		mutex_exit(&cardbus_list_mutex);
526 		return (DDI_FAILURE); /* no instance is setup for this bus */
527 	}
528 
529 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
530 
531 	if (cbp->cb_dsp) {
532 		struct cb_deviceset_props *cbdp, *ncbdp;
533 
534 		cbdp = cbp->cb_dsp;
535 		while (cbdp) {
536 			ncbdp = cbdp->next;
537 			cardbus_devprops_free(cbdp);
538 			cbdp = ncbdp;
539 		}
540 	}
541 	/*
542 	 * Unregister the bus with the HPS.
543 	 *
544 	 * (Note: It is assumed that the HPS framework uninstalls
545 	 *  event handlers for all the hot plug slots on this bus.)
546 	 */
547 	(void) hpc_nexus_unregister_bus(dip);
548 
549 	if (cbp->cb_ndi_event_hdl != NULL) {
550 		(void) ndi_event_unbind_set(cbp->cb_ndi_event_hdl,
551 		    &cbp->cb_ndi_events, NDI_SLEEP);
552 		ndi_event_free_hdl(cbp->cb_ndi_event_hdl);
553 	}
554 
555 	mutex_destroy(&cbp->cb_mutex);
556 	if (cbp->nexus_path)
557 		kmem_free(cbp->nexus_path, strlen(cbp->nexus_path) + 1);
558 	if (cbp->name)
559 		kmem_free(cbp->name, strlen(cbp->name) + 1);
560 
561 	ddi_soft_state_free(cardbus_state, cb_instance);
562 
563 	/* remove the 'cbus-instance' property from the devinfo node */
564 	(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "cbus-instance");
565 
566 	ASSERT(cardbus_count != 0);
567 	--cardbus_count;
568 
569 	mutex_exit(&cardbus_list_mutex);
570 	return (DDI_SUCCESS);
571 }
572 #endif
573 
574 boolean_t
575 cardbus_load_cardbus(dev_info_t *dip, uint_t socket, uint32_t pc_base)
576 {
577 #ifndef HOTPLUG
578 	struct cardbus_config_ctrl ctrl;
579 	int circular_count;
580 #endif
581 	int cb_instance;
582 	cbus_t *cbp;
583 	struct dev_info *devi = DEVI(dip);
584 
585 	_NOTE(ARGUNUSED(socket, pc_base))
586 
587 #if defined(CARDBUS_DEBUG)
588 	cardbus_err(dip, 6, "cardbus_load_cardbus\n");
589 #endif
590 
591 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
592 			DDI_PROP_DONTPASS, "cbus-instance", -1);
593 	ASSERT(cb_instance >= 0);
594 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
595 
596 	if (cbp->fatal_problem)
597 		return (B_FALSE);
598 
599 	if (cardbus_convert_properties(dip) == DDI_FAILURE)
600 		return (B_FALSE);
601 
602 	number_of_cardbus_cards++;
603 	devi->devi_ops->devo_bus_ops = &cardbusbus_ops;
604 
605 #ifdef HOTPLUG
606 	mutex_enter(&cbp->cb_mutex);
607 	cbp->card_present = B_TRUE;
608 
609 	(void) hpc_slot_event_notify(cbp->slot_handle,
610 	    HPC_EVENT_SLOT_INSERTION, 0);
611 	(void) hpc_slot_event_notify(cbp->slot_handle,
612 	    HPC_EVENT_SLOT_POWER_ON, 0);
613 	(void) hpc_slot_event_notify(cbp->slot_handle,
614 	    HPC_EVENT_SLOT_CONFIGURE, 0);
615 
616 	mutex_exit(&cbp->cb_mutex);
617 #else
618 	if (cardbus_configure(cbp) != PCICFG_SUCCESS) {
619 #if defined(CARDBUS_DEBUG)
620 		cardbus_err(dip, 6, "cardbus_configure failed\n");
621 #endif
622 		return (B_FALSE);
623 	}
624 
625 	ctrl.rv = NDI_SUCCESS;
626 	ctrl.busno = cardbus_primary_busno(dip);
627 	ctrl.op = PCICFG_OP_ONLINE;
628 	ctrl.dip = NULL;
629 	ctrl.flags = PCICFG_FLAGS_CONTINUE;
630 
631 	/*
632 	 * The child of the dip is the cardbus dip. The child of the
633 	 * cardbus dip is the device itself
634 	 */
635 #if defined(CARDBUS_DEBUG)
636 	cardbus_err(dip, 8, "cardbus_load_cardbus: calling cbus_configure\n");
637 #endif
638 	ndi_devi_enter(dip, &circular_count);
639 	ddi_walk_devs(ddi_get_child(dip), cbus_configure, (void *)&ctrl);
640 	ndi_devi_exit(dip, circular_count);
641 
642 	if (ctrl.rv != NDI_SUCCESS) {
643 		cardbus_err(dip, 1,
644 		    "cardbus_load_cardbus (%s%d): failed to attach (%d)\n",
645 		    ctrl.dip ? ddi_driver_name(ctrl.dip) : "Unknown",
646 		    ctrl.dip ? ddi_get_instance(ctrl.dip) : 0,
647 		    ctrl.rv);
648 
649 		/*
650 		 * Returning error here will cause the pcic_load_cardbus() call
651 		 * to fail. This will invoke pcic_unload_cardbus() which calls
652 		 * cardbus_unload_cardbus() below.
653 		 */
654 		return (B_FALSE);
655 	}
656 #endif
657 
658 #if defined(CARDBUS_DEBUG)
659 	cardbus_err(dip, 7, "cardbus_load_cardbus: returning TRUE\n");
660 #endif
661 
662 	return (B_TRUE);
663 }
664 
665 /*
666  * Unload the cardbus module
667  */
668 void
669 cardbus_unload_cardbus(dev_info_t *dip)
670 {
671 	int	cb_instance;
672 #ifndef HOTPLUG
673 	int	prim_bus = cardbus_primary_busno(dip);
674 	int	rval;
675 #endif
676 	cbus_t *cbp;
677 
678 	cardbus_err(dip, 6, "cardbus_unload_cardbus\n");
679 
680 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
681 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
682 	ASSERT(cb_instance >= 0);
683 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
684 
685 	if (number_of_cardbus_cards == 0)
686 		return;
687 
688 #ifdef HOTPLUG
689 	mutex_enter(&cbp->cb_mutex);
690 	cbp->card_present = B_FALSE;
691 
692 	(void) hpc_slot_event_notify(cbp->slot_handle,
693 	    HPC_EVENT_SLOT_POWER_OFF, 0);
694 	(void) hpc_slot_event_notify(cbp->slot_handle,
695 	    HPC_EVENT_SLOT_UNCONFIGURE, 0);
696 	(void) hpc_slot_event_notify(cbp->slot_handle,
697 	    HPC_EVENT_SLOT_REMOVAL, 0);
698 
699 	mutex_exit(&cbp->cb_mutex);
700 #else
701 
702 	cardbus_err(dip, 8,
703 	    "cardbus_unload_cardbus: calling cardbus_unconfigure_node\n");
704 
705 	rval = cardbus_unconfigure_node(dip, prim_bus, B_TRUE);
706 
707 	if (rval != NDI_SUCCESS) {
708 		cardbus_err(dip, 4,
709 		    "cardbus_unload_cardbus: "
710 		    "cardbus_unconfigure_node failed\n");
711 		number_of_cardbus_cards--;
712 		cbp->fatal_problem = B_TRUE;
713 		cmn_err(CE_WARN,
714 		    "cardbus(%s%d): Failed to remove device tree: "
715 		    "Slot disabled",
716 		    ddi_get_name(dip), ddi_get_instance(dip));
717 		return;
718 	}
719 
720 	(void) cardbus_unconfigure(cbp);
721 #endif
722 
723 	/*
724 	 * Inform the lower drivers that the card has been removed
725 	 */
726 	if (cbp->cb_ndi_event_hdl != NULL) {
727 		ddi_eventcookie_t cookie;
728 		if (ndi_event_retrieve_cookie(cbp->cb_ndi_event_hdl, dip,
729 		    DDI_DEVI_REMOVE_EVENT, &cookie, 0) == NDI_SUCCESS) {
730 			(void) ndi_event_run_callbacks(cbp->cb_ndi_event_hdl,
731 			    dip, cookie, NULL);
732 		}
733 	}
734 
735 	cardbus_revert_properties(dip);
736 }
737 
738 boolean_t
739 cardbus_can_suspend(dev_info_t *dip)
740 {
741 #ifdef HOTPLUG
742 	cbus_t *cbp;
743 	int	cb_instance;
744 
745 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
746 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
747 	ASSERT(cb_instance >= 0);
748 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
749 	if (cbp->ostate == AP_OSTATE_UNCONFIGURED)
750 		return (B_TRUE);
751 #endif
752 	return (B_FALSE);
753 }
754 
755 static int
756 cardbus_convert_properties(dev_info_t *dip)
757 {
758 	struct pcm_regs *pcic_avail_p, *old_avail_p;
759 	pci_regspec_t *cb_avail_p, *new_avail_p;
760 	pcic_ranges_t *pcic_range_p, *old_range_p;
761 	cardbus_range_t *cb_range_p, *new_range_p;
762 	int range_len, range_entries, i;
763 	int avail_len, avail_entries;
764 
765 #if defined(CARDBUS_DEBUG)
766 	cardbus_err(dip, 6, "cardbus_convert_properties\n");
767 #endif
768 
769 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
770 	    "#address-cells", 3) != DDI_SUCCESS) {
771 		cardbus_err(dip, 1, "cardbus_convert_properties: "
772 		    "failed to update #address-cells property\n");
773 		return (DDI_FAILURE);
774 	}
775 	if (ndi_prop_update_int(DDI_DEV_T_NONE, dip,
776 	    "#size-cells", 2) != DDI_SUCCESS) {
777 		cardbus_err(dip, 1, "cardbus_convert_properties: "
778 		    "failed to update #size-cells property\n");
779 		return (DDI_FAILURE);
780 	}
781 
782 	if (ddi_getlongprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS, "available",
783 	    (caddr_t)&pcic_avail_p, &avail_len) != DDI_PROP_SUCCESS) {
784 		cardbus_err(dip, 1, "cardbus_convert_properties: "
785 		    "no available property for pcmcia\n");
786 	} else {
787 		avail_entries = avail_len / sizeof (struct pcm_regs);
788 		cb_avail_p = kmem_alloc(sizeof (pci_regspec_t) * avail_entries,
789 		    KM_SLEEP);
790 
791 		old_avail_p = pcic_avail_p;
792 		new_avail_p = cb_avail_p;
793 		for (i = 0; i < avail_entries;
794 		    i++, old_avail_p++, new_avail_p++) {
795 			new_avail_p->pci_phys_hi = old_avail_p->phys_hi;
796 			new_avail_p->pci_phys_mid = 0;
797 			new_avail_p->pci_phys_low = old_avail_p->phys_lo;
798 			new_avail_p->pci_size_hi = 0;
799 			new_avail_p->pci_size_low = old_avail_p->phys_len;
800 		}
801 
802 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
803 		    "available",
804 		    (int *)cb_avail_p,
805 		    (sizeof (pci_regspec_t) * avail_entries)/sizeof (int));
806 
807 		kmem_free(pcic_avail_p, avail_len);
808 		kmem_free(cb_avail_p, sizeof (pci_regspec_t) * avail_entries);
809 	}
810 
811 	if (ddi_getlongprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS, "ranges",
812 	    (caddr_t)&pcic_range_p, &range_len) != DDI_PROP_SUCCESS) {
813 		cardbus_err(dip, 1, "cardbus_convert_properties: "
814 		    "no ranges property for pcmcia\n");
815 	} else {
816 		range_entries = range_len / sizeof (pcic_ranges_t);
817 		cb_range_p = kmem_alloc(
818 		    sizeof (cardbus_range_t) * range_entries, KM_SLEEP);
819 
820 		old_range_p = pcic_range_p;
821 		new_range_p = cb_range_p;
822 		for (i = 0; i < range_entries;
823 		    i++, old_range_p++, new_range_p++) {
824 			new_range_p->child_hi =
825 			    old_range_p->pcic_range_caddrhi;
826 			new_range_p->child_mid = 0;
827 			new_range_p->child_lo =
828 			    old_range_p->pcic_range_caddrlo;
829 			new_range_p->parent_hi =
830 			    old_range_p->pcic_range_paddrhi;
831 			new_range_p->parent_mid =
832 			    old_range_p->pcic_range_paddrmid;
833 			new_range_p->parent_lo =
834 			    old_range_p->pcic_range_paddrlo;
835 			new_range_p->size_hi = 0;
836 			new_range_p->size_lo = old_range_p->pcic_range_size;
837 		}
838 
839 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
840 		    (int *)cb_range_p,
841 		    (sizeof (cardbus_range_t) * range_entries)/sizeof (int));
842 
843 		kmem_free(pcic_range_p, range_len);
844 		kmem_free(cb_range_p, sizeof (cardbus_range_t) * range_entries);
845 	}
846 
847 	return (DDI_SUCCESS);
848 }
849 
850 static void
851 cardbus_revert_properties(dev_info_t *dip)
852 {
853 #if defined(CARDBUS_DEBUG)
854 	cardbus_err(dip, 6, "cardbus_revert_properties\n");
855 #endif
856 
857 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "#address-cells");
858 
859 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "#size-cells");
860 
861 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "available");
862 }
863 
864 static int
865 cardbus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
866 		ddi_prop_op_t prop_op, int mod_flags,
867 		char *name, caddr_t valuep, int *lengthp)
868 {
869 #if defined(CARDBUS_DEBUG)
870 	if ((ch_dip != dip) || (cardbus_debug >= 9))
871 		cardbus_err(dip, 6,
872 		    "cardbus_prop_op(%s) (dip=0x%p, op=%d, name=%s)\n",
873 		    ddi_driver_name(ch_dip), (void *) dip, prop_op, name);
874 #endif
875 	return (impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op,
876 	    mod_flags, name, valuep, lengthp));
877 }
878 
879 static int
880 cardbus_ctlops(dev_info_t *dip, dev_info_t *rdip,
881 	ddi_ctl_enum_t ctlop, void *arg, void *result)
882 {
883 	pci_regspec_t *regs;
884 	int	totreg, reglen;
885 	const char	*dname = ddi_driver_name(dip);
886 
887 	ASSERT(number_of_cardbus_cards != 0);
888 
889 	cardbus_err(dip, 6,
890 	    "cardbus_ctlops(%p, %p, %d, %p, %p)\n",
891 	    (void *)dip, (void *)rdip, ctlop, (void *)arg, (void *)result);
892 
893 	switch (ctlop) {
894 	case DDI_CTLOPS_UNINITCHILD:
895 		cardbus_removechild((dev_info_t *)arg);
896 		return (DDI_SUCCESS);
897 
898 	default:
899 		/*
900 		 * Do Nothing
901 		 */
902 		cardbus_err(dip, 8,
903 		    "cardbus_ctlops: Unsupported DDI_CTLOP %d\n", ctlop);
904 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
905 
906 	case DDI_CTLOPS_SIDDEV:		/* see ddi_dev_is_sid(9F) */
907 		return (DDI_SUCCESS);
908 
909 	case DDI_CTLOPS_SLAVEONLY:	/* see ddi_slaveonly(9F) */
910 		return (DDI_FAILURE);	/* cardbus */
911 
912 	case DDI_CTLOPS_REGSIZE:
913 	case DDI_CTLOPS_NREGS:
914 		if (rdip == (dev_info_t *)NULL) {
915 			*(int *)result = 0;
916 			return (DDI_FAILURE);
917 		}
918 		break;
919 
920 	case DDI_CTLOPS_IOMIN:
921 		/*
922 		 * If we are using the streaming cache, align at
923 		 * least on a cache line boundary. Otherwise use
924 		 * whatever alignment is passed in.
925 		 */
926 
927 		if (arg) {
928 			int	val = *((int *)result);
929 
930 #ifdef  PCI_SBUF_LINE_SIZE
931 			val = maxbit(val, PCI_SBUF_LINE_SIZE);
932 #else
933 			val = maxbit(val, 64);
934 #endif
935 			*((int *)result) = val;
936 		}
937 		return (DDI_SUCCESS);
938 
939 	case DDI_CTLOPS_INITCHILD:
940 		return (cardbus_initchild(rdip, dip, (dev_info_t *)arg,
941 					result));
942 
943 	case DDI_CTLOPS_REPORTDEV:
944 		if (rdip == (dev_info_t *)0)
945 			return (DDI_FAILURE);
946 
947 		if (strcmp("pcs", ddi_node_name(rdip)) == 0)
948 			cardbus_err(dip, 1,
949 			    "cardbus_ctlops: PCCard socket %d at %s@%s\n",
950 			    ddi_get_instance(rdip),
951 			    dname, ddi_get_name_addr(dip));
952 		else {
953 			pci_regspec_t *pci_rp;
954 			dev_info_t *next;
955 			int	length;
956 
957 			if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
958 			    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
959 			    (uint_t *)&length) != DDI_PROP_SUCCESS)
960 				return (DDI_FAILURE);
961 
962 			if (pci_rp->pci_phys_hi == 0)
963 				cardbus_err(dip, 1, "%s%d at %s@%s\n",
964 				    ddi_driver_name(rdip),
965 				    ddi_get_instance(rdip),
966 				    dname, ddi_get_name_addr(dip));
967 			else {
968 				uint8_t bus, device, function;
969 				int32_t val32;
970 				char	*ptr, buf[128];
971 
972 				bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
973 				device = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
974 				function = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
975 
976 				ptr = buf;
977 				(void) sprintf(ptr, "  "
978 				    "Bus %3d Device %2d Function %2d",
979 				    bus, device, function);
980 				ptr = &ptr[strlen(ptr)];
981 
982 				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
983 				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
984 				    "vendor-id", -1);
985 				if (val32 != -1) {
986 					(void) sprintf(ptr, " Vendor 0x%04x",
987 					    val32);
988 					ptr = &ptr[strlen(ptr)];
989 				}
990 				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
991 				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
992 				    "device-id", -1);
993 				if (val32 != -1) {
994 					(void) sprintf(ptr, " Device 0x%04x",
995 					    val32);
996 					ptr = &ptr[strlen(ptr)];
997 				}
998 				val32 = ddi_getprop(DDI_DEV_T_ANY, rdip,
999 				    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS,
1000 				    "class-code", -1);
1001 				if (val32 != -1) {
1002 					const char	*name;
1003 
1004 					if ((name = ddi_get_name(rdip)) !=
1005 					    NULL)
1006 						(void) sprintf(ptr, " Name %s",
1007 						    name);
1008 					else
1009 						(void) sprintf(ptr,
1010 						    " Class 0x%x", val32 >> 8);
1011 					ptr = &ptr[strlen(ptr)];
1012 				}
1013 
1014 				*ptr++ = '\n';
1015 				ASSERT(((caddr_t)ptr - (caddr_t)buf) <
1016 					sizeof (buf));
1017 				*ptr = '\0';
1018 
1019 				cardbus_err(dip, 1, buf);
1020 			}
1021 			ddi_prop_free(pci_rp);
1022 
1023 			for (next = ddi_get_child(rdip); next;
1024 			    next = ddi_get_next_sibling(next))
1025 				(void) cardbus_ctlops(next, next,
1026 				    DDI_CTLOPS_REPORTDEV, arg, result);
1027 		}
1028 		return (DDI_SUCCESS);
1029 	}
1030 	*(int *)result = 0;
1031 
1032 	if (ddi_getlongprop(DDI_DEV_T_NONE, rdip,
1033 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
1034 	    (caddr_t)&regs, &reglen) != DDI_SUCCESS)
1035 		return (DDI_FAILURE);
1036 
1037 	totreg = reglen / sizeof (pci_regspec_t);
1038 	if (ctlop == DDI_CTLOPS_NREGS) {
1039 		cardbus_err(dip, 6,
1040 		    "cardbus_ctlops, returning NREGS = %d\n", totreg);
1041 		*(int *)result = totreg;
1042 	} else if (ctlop == DDI_CTLOPS_REGSIZE) {
1043 		const int	rn = *(int *)arg;
1044 		if (rn > totreg)
1045 			return (DDI_FAILURE);
1046 		cardbus_err(dip, 6,
1047 		    "cardbus_ctlops, returning REGSIZE(%d) = %d\n",
1048 		    rn, regs[rn].pci_size_low);
1049 		*(off_t *)result = regs[rn].pci_size_low;
1050 	}
1051 	kmem_free(regs, reglen);
1052 	return (DDI_SUCCESS);
1053 }
1054 
1055 static void
1056 cardbus_init_child_regs(dev_info_t *child)
1057 {
1058 	ddi_acc_handle_t config_handle;
1059 	uint16_t command_preserve, command;
1060 #if !defined(__i386) && !defined(__amd64)
1061 	uint8_t bcr;
1062 #endif
1063 	uint8_t header_type;
1064 	uint8_t min_gnt, latency_timer;
1065 	uint_t n;
1066 
1067 	/*
1068 	 * Map the child configuration space to for initialization.
1069 	 *
1070 	 *  Set the latency-timer register to values appropriate
1071 	 *  for the devices on the bus (based on other devices
1072 	 *  MIN_GNT and MAX_LAT registers.
1073 	 *
1074 	 *  Set the fast back-to-back enable bit in the command
1075 	 *  register if it's supported and all devices on the bus
1076 	 *  have the capability.
1077 	 *
1078 	 */
1079 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS)
1080 		return;
1081 
1082 	cardbus_err(child, 6, "cardbus_init_child_regs()\n");
1083 
1084 	/*
1085 	 * Determine the configuration header type.
1086 	 */
1087 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
1088 
1089 	/*
1090 	 * Support for "command-preserve" property.  Note that we
1091 	 * add PCI_COMM_BACK2BACK_ENAB to the bits to be preserved
1092 	 * since the obp will set this if the device supports and
1093 	 * all targets on the same bus support it.  Since psycho
1094 	 * doesn't support PCI_COMM_BACK2BACK_ENAB, it will never
1095 	 * be set.  This is just here in case future revs do support
1096 	 * PCI_COMM_BACK2BACK_ENAB.
1097 	 */
1098 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1099 	    DDI_PROP_DONTPASS,
1100 	    "command-preserve", 0);
1101 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
1102 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
1103 	command |= (cardbus_command_default & ~command_preserve);
1104 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
1105 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
1106 
1107 #if !defined(__i386) && !defined(__amd64)
1108 	/*
1109 	 * If the device has a bus control register then program it
1110 	 * based on the settings in the command register.
1111 	 */
1112 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
1113 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
1114 		if (cardbus_command_default & PCI_COMM_PARITY_DETECT)
1115 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
1116 		if (cardbus_command_default & PCI_COMM_SERR_ENABLE)
1117 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
1118 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
1119 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
1120 	}
1121 #endif
1122 
1123 	/*
1124 	 * Initialize cache-line-size configuration register if needed.
1125 	 */
1126 	if (ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1127 	    "cache-line-size", 0) == 0) {
1128 
1129 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
1130 		    PCI_CACHE_LINE_SIZE);
1131 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
1132 		if (n != 0)
1133 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
1134 				"cache-line-size", n);
1135 	}
1136 
1137 	/*
1138 	 * Initialize latency timer registers if needed.
1139 	 */
1140 	if (ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1141 	    "latency-timer", 0) == 0) {
1142 
1143 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
1144 			latency_timer = cardbus_latency_timer;
1145 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
1146 					latency_timer);
1147 		} else {
1148 			min_gnt = pci_config_get8(config_handle,
1149 						PCI_CONF_MIN_G);
1150 
1151 			/*
1152 			 * Cardbus os only 33Mhz
1153 			 */
1154 			if (min_gnt != 0) {
1155 				latency_timer = min_gnt * 8;
1156 			}
1157 		}
1158 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
1159 				latency_timer);
1160 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
1161 		if (n != 0)
1162 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
1163 				"latency-timer", n);
1164 	}
1165 
1166 	pci_config_teardown(&config_handle);
1167 }
1168 
1169 static int
1170 cardbus_initchild(dev_info_t *rdip, dev_info_t *dip, dev_info_t *child,
1171 		void *result)
1172 {
1173 	char	name[MAXNAMELEN];
1174 	const char	*dname = ddi_driver_name(dip);
1175 	const struct cb_ops *cop;
1176 
1177 	_NOTE(ARGUNUSED(rdip, result))
1178 
1179 	cardbus_err(child, 6, "cardbus_initchild\n");
1180 
1181 	/*
1182 	 * Name the child
1183 	 */
1184 	if (cardbus_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
1185 		return (DDI_FAILURE);
1186 
1187 	ddi_set_name_addr(child, name);
1188 	ddi_set_parent_data(child, NULL);
1189 
1190 	if (ndi_dev_is_persistent_node(child) == 0) {
1191 		/*
1192 		 * Try to merge the properties from this prototype
1193 		 * node into real h/w nodes.
1194 		 */
1195 		if (ndi_merge_node(child, cardbus_name_child) == DDI_SUCCESS) {
1196 			/*
1197 			 * Merged ok - return failure to remove the node.
1198 			 */
1199 			cardbus_removechild(child);
1200 			return (DDI_FAILURE);
1201 		}
1202 		/*
1203 		 * The child was not merged into a h/w node,
1204 		 * but there's not much we can do with it other
1205 		 * than return failure to cause the node to be removed.
1206 		 */
1207 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
1208 		    ddi_driver_name(child), ddi_get_name_addr(child),
1209 		    ddi_driver_name(child));
1210 		cardbus_removechild(child);
1211 		return (DDI_NOT_WELL_FORMED);
1212 	}
1213 	cop = DEVI(dip)->devi_ops->devo_cb_ops;
1214 
1215 	if ((cop == NULL) || (!(cop->cb_flag & D_HOTPLUG))) {
1216 		cmn_err(CE_WARN, "%s: driver doesn't support HOTPLUG\n", dname);
1217 		return (DDI_FAILURE);
1218 	}
1219 
1220 	cardbus_init_child_regs(child);
1221 
1222 	/*
1223 	 * Create ppd if needed.
1224 	 */
1225 	if (ddi_get_parent_data(child) == NULL) {
1226 		struct cardbus_parent_private_data *ppd;
1227 
1228 #ifdef sparc
1229 		ppd = (struct cardbus_parent_private_data *)
1230 		kmem_zalloc(sizeof (struct cardbus_parent_private_data),
1231 		    KM_SLEEP);
1232 
1233 #elif defined(__x86) || defined(__amd64)
1234 		ppd = (struct cardbus_parent_private_data *)
1235 		    kmem_zalloc(sizeof (struct cardbus_parent_private_data)
1236 			+ sizeof (struct intrspec), KM_SLEEP);
1237 
1238 		ppd->ppd.par_intr = (struct intrspec *)(ppd + 1);
1239 		(ppd->ppd.par_intr)->intrspec_pri = 0;
1240 		(ppd->ppd.par_intr)->intrspec_vec = 0;
1241 		(ppd->ppd.par_intr)->intrspec_func = (uint_t (*)()) 0;
1242 #endif
1243 
1244 		if (ddi_getprop(DDI_DEV_T_NONE, child, DDI_PROP_DONTPASS,
1245 		    "interrupts", -1) != -1)
1246 			ppd->ppd.par_nintr = 1;
1247 
1248 		ppd->code = CB_PPD_CODE;
1249 
1250 		cardbus_err(child, 5,
1251 		    "cardbus_initchild: Creating empty ppd\n");
1252 		ppd->ppd.par_nreg = 0;
1253 		ppd->ppd.par_reg = NULL;
1254 
1255 		ddi_set_parent_data(child, (caddr_t)ppd);
1256 	}
1257 
1258 	return (DDI_SUCCESS);
1259 }
1260 
1261 static int
1262 cardbus_name_child(dev_info_t *child, char *name, int namelen)
1263 {
1264 	pci_regspec_t *pci_rp;
1265 	char	**unit_addr;
1266 	uint_t n;
1267 	int	bus, device, func;
1268 
1269 	/*
1270 	 * Pseudo nodes indicate a prototype node with per-instance
1271 	 * properties to be merged into the real h/w device node.
1272 	 * The interpretation of the unit-address is DD[,F]
1273 	 * where DD is the device id and F is the function.
1274 	 */
1275 	if (ndi_dev_is_persistent_node(child) == 0) {
1276 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
1277 		    DDI_PROP_DONTPASS,
1278 		    "unit-address", &unit_addr, &n) != DDI_PROP_SUCCESS) {
1279 			cmn_err(CE_WARN, "cannot name node from %s.conf",
1280 			    ddi_driver_name(child));
1281 			return (DDI_FAILURE);
1282 		}
1283 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
1284 			cmn_err(CE_WARN, "unit-address property in %s.conf"
1285 			    " not well-formed", ddi_driver_name(child));
1286 			ddi_prop_free(unit_addr);
1287 			return (DDI_FAILURE);
1288 		}
1289 		(void) snprintf(name, namelen, "%s", *unit_addr);
1290 		ddi_prop_free(unit_addr);
1291 		return (DDI_SUCCESS);
1292 	}
1293 
1294 	/*
1295 	 * Get the address portion of the node name based on
1296 	 * the function and device number.
1297 	 */
1298 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
1299 	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
1300 		return (DDI_FAILURE);
1301 	}
1302 
1303 	bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi);
1304 	device = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
1305 	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
1306 	ddi_prop_free(pci_rp);
1307 
1308 	if (func != 0)
1309 		(void) snprintf(name, namelen, "%x,%x", device, func);
1310 	else
1311 		(void) snprintf(name, namelen, "%x", device);
1312 
1313 	cardbus_err(child, 8,
1314 	    "cardbus_name_child: system init done [%x][%x][%x]"
1315 	    " for %s [%s] nodeid: %x @%s\n",
1316 	    bus, device, func,
1317 	    ddi_get_name(child), ddi_get_name_addr(child),
1318 	    DEVI(child)->devi_nodeid, name);
1319 
1320 	return (DDI_SUCCESS);
1321 }
1322 
1323 static void
1324 cardbus_removechild(dev_info_t *dip)
1325 {
1326 	struct cardbus_parent_private_data *ppd;
1327 
1328 	ddi_set_name_addr(dip, NULL);
1329 	impl_rem_dev_props(dip);
1330 	ppd = (struct cardbus_parent_private_data *)ddi_get_parent_data(dip);
1331 	if (ppd && (ppd->code == CB_PPD_CODE)) {
1332 		if (ppd->ppd.par_reg && (ppd->ppd.par_nreg > 0))
1333 			kmem_free((caddr_t)ppd->ppd.par_reg,
1334 			    ppd->ppd.par_nreg * sizeof (struct regspec));
1335 #ifdef sparc
1336 		kmem_free(ppd, sizeof (struct cardbus_parent_private_data));
1337 #elif defined(__x86) || defined(__amd64)
1338 		kmem_free(ppd, sizeof (struct cardbus_parent_private_data) +
1339 		    sizeof (struct intrspec));
1340 #endif
1341 		cardbus_err(dip, 5,
1342 		    "cardbus_removechild: ddi_set_parent_data(NULL)\n");
1343 		ddi_set_parent_data(dip, NULL);
1344 	}
1345 }
1346 
1347 
1348 static char	cb_bnamestr[] = "binding_name";
1349 static char	cb_venidstr[] = "VendorID";
1350 static char	cb_devidstr[] = "DeviceID";
1351 static char	cb_nnamestr[] = "nodename";
1352 
1353 static cb_props_parse_tree_t cb_props_parse_tree[] = {
1354 	{ cb_bnamestr, PT_STATE_STRING_VAR },
1355 	{ cb_venidstr, PT_STATE_HEX_VAR },
1356 	{ cb_devidstr, PT_STATE_HEX_VAR } };
1357 
1358 static int
1359 check_token(char *token, int *len)
1360 {
1361 	int	state = PT_STATE_DEC_VAR;
1362 	int	sl = strlen(token), il = 1;
1363 	char	c;
1364 
1365 	if (token[0] == '0' && token[2] && (token[1] == 'x' || token[1] ==
1366 	    'X')) {
1367 		state = PT_STATE_HEX_VAR;
1368 		token += 2;
1369 	}
1370 
1371 	while (c = *token++) {
1372 		if (isdigit(c))
1373 			continue;
1374 		if (c == PARSE_COMMA) {
1375 			il++;
1376 			if (token[0] == '0' && token[2] && isx(token[1])) {
1377 				state = PT_STATE_HEX_VAR;
1378 				token += 2;
1379 			}
1380 			continue;
1381 		}
1382 		if (!isxdigit(c)) {
1383 			*len = sl;
1384 			return (PT_STATE_STRING_VAR);
1385 		}
1386 		state = PT_STATE_HEX_VAR;
1387 	}
1388 	*len = il;
1389 	return (state);
1390 }
1391 
1392 
1393 static char *
1394 find_token(char **cp, int *l, char *endc)
1395 {
1396 	char	*cpp = *cp;
1397 
1398 	while ((**cp && (isalpha(**cp) || isxdigit(**cp) ||
1399 	    (**cp == PARSE_UNDERSCORE) ||
1400 	    (**cp == PARSE_COMMA) ||
1401 	    (**cp == PARSE_DASH)))) {
1402 		(*cp)++;
1403 		(*l)++;
1404 	}
1405 
1406 	*endc = **cp;
1407 	**cp = NULL;
1408 
1409 	return (cpp);
1410 }
1411 
1412 static int
1413 parse_token(char *token)
1414 {
1415 	cb_props_parse_tree_t *pt = cb_props_parse_tree;
1416 	int	k = sizeof (cb_props_parse_tree) /
1417 	    sizeof (cb_props_parse_tree_t);
1418 
1419 	while (k--) {
1420 		if (strcmp((char *)token, pt->token) == 0)
1421 			return (pt->state);
1422 		pt++;
1423 	}
1424 
1425 	return (PT_STATE_UNKNOWN);
1426 }
1427 
1428 static int
1429 token_to_hex(char *token, unsigned *val, int len)
1430 {
1431 	uchar_t c;
1432 
1433 	*val = 0;
1434 	if (token[0] == '0' && (token[1] == 'x' || token[1] == 'X')) {
1435 		token += 2;
1436 	}
1437 
1438 	while (*token) {
1439 		if (!isxdigit(*token)) {
1440 			if (*token == PARSE_COMMA) {
1441 				if (!(--len))
1442 					return (1);
1443 				val++;
1444 				*val = 0;
1445 				token++;
1446 				if (token[0] == '0' && (token[1] == 'x' ||
1447 				    token[1] == 'X')) {
1448 					token += 2;
1449 				}
1450 				continue;
1451 			}
1452 			return (0);
1453 		}
1454 		c = toupper(*token);
1455 		if (c >= 'A')
1456 			c = c - 'A' + 10 + '0';
1457 		*val = ((*val * 16) + (c - '0'));
1458 		token++;
1459 	}
1460 
1461 	return (1);
1462 }
1463 
1464 static int
1465 token_to_dec(char *token, unsigned *val, int len)
1466 {
1467 	*val = 0;
1468 
1469 	while (*token) {
1470 		if (!isdigit(*token)) {
1471 			if (*token == PARSE_COMMA) {
1472 				if (!(--len))
1473 					return (1);
1474 				val++;
1475 				*val = 0;
1476 				token++;
1477 				continue;
1478 			}
1479 			return (0);
1480 		}
1481 		*val = ((*val * 10) + (*token - '0'));
1482 		token++;
1483 	}
1484 
1485 	return (1);
1486 }
1487 
1488 static void
1489 cardbus_add_prop(struct cb_deviceset_props *cdsp, int type, char *name,
1490 		caddr_t vp, int len)
1491 {
1492 	ddi_prop_t *propp;
1493 	int	pnlen = strlen(name) + 1;
1494 
1495 	propp = (ddi_prop_t *)kmem_zalloc(sizeof (ddi_prop_t), KM_SLEEP);
1496 	propp->prop_name = (char *)kmem_alloc(pnlen, KM_SLEEP);
1497 	propp->prop_val = vp;
1498 	bcopy(name, propp->prop_name, pnlen);
1499 	propp->prop_len = len;
1500 	propp->prop_flags = type;
1501 	propp->prop_next = cdsp->prop_list;
1502 	cdsp->prop_list = propp;
1503 }
1504 
1505 static void
1506 cardbus_add_stringprop(struct cb_deviceset_props *cdsp, char *name,
1507 		char *vp, int len)
1508 {
1509 	char	*nstr = kmem_zalloc(len + 1, KM_SLEEP);
1510 
1511 	bcopy(vp, nstr, len);
1512 	cardbus_add_prop(cdsp, DDI_PROP_TYPE_STRING, name, (caddr_t)nstr,
1513 	    len + 1);
1514 }
1515 
1516 static void
1517 cardbus_prop_free(ddi_prop_t *propp)
1518 {
1519 	if (propp->prop_len) {
1520 		switch (propp->prop_flags) {
1521 		case DDI_PROP_TYPE_STRING:
1522 			kmem_free(propp->prop_val, propp->prop_len);
1523 			break;
1524 		case DDI_PROP_TYPE_INT:
1525 			kmem_free(propp->prop_val,
1526 			    propp->prop_len * sizeof (int));
1527 			break;
1528 		}
1529 	}
1530 	kmem_free(propp->prop_name, strlen(propp->prop_name) + 1);
1531 	kmem_free(propp, sizeof (ddi_prop_t *));
1532 }
1533 
1534 static void
1535 cardbus_devprops_free(struct cb_deviceset_props *cbdp)
1536 {
1537 	ddi_prop_t *propp, *npropp;
1538 
1539 	propp = cbdp->prop_list;
1540 	while (propp) {
1541 		npropp = propp->prop_next;
1542 		cardbus_prop_free(propp);
1543 		propp = npropp;
1544 	}
1545 	if (cbdp->nodename)
1546 		kmem_free(cbdp->nodename, strlen(cbdp->nodename) + 1);
1547 	if (cbdp->binding_name)
1548 		kmem_free(cbdp->binding_name, strlen(cbdp->binding_name) +
1549 		    1);
1550 	kmem_free(cbdp, sizeof (*cbdp));
1551 }
1552 
1553 /*
1554  * Format of "cb-device-init-props" property:
1555  * Anything before the semi-colon is an identifying equate, anything
1556  * after the semi-colon is a setting equate.
1557  *
1558  * "binding_name=xXxXxX VendorID=NNNN DeviceID=NNNN; nodename=NewName
1559  * 					Prop=PropVal"
1560  *
1561  */
1562 static int
1563 cardbus_parse_devprop(cbus_t *cbp, char *cp)
1564 {
1565 	int	state = PT_STATE_TOKEN, qm = 0, em = 0, smc = 0, l = 0;
1566 	int	length;
1567 	char	*token = "beginning of line";
1568 	char	*ptoken = NULL, *quote;
1569 	char	eq = NULL;
1570 	struct cb_deviceset_props *cdsp;
1571 
1572 	cdsp = (struct cb_deviceset_props *)kmem_zalloc(sizeof (*cdsp),
1573 	    KM_SLEEP);
1574 	length = strlen(cp);
1575 
1576 	while ((*cp) && (l < length)) {
1577 		/*
1578 		 * Check for escaped characters
1579 		 */
1580 		if (*cp == PARSE_ESCAPE) {
1581 			char	*cpp = cp, *cppp = cp + 1;
1582 
1583 			em = 1;
1584 
1585 			if (!qm) {
1586 				cmn_err(CE_CONT, "cardbus_parse_devprop: "
1587 				    "escape not allowed outside "
1588 				    "of quotes at [%s]\n", token);
1589 				return (DDI_FAILURE);
1590 
1591 			} /* if (!qm) */
1592 
1593 			while (*cppp)
1594 				*cpp++ = *cppp++;
1595 
1596 			l++;
1597 
1598 			*cpp = NULL;
1599 		} /* PARSE_ESCAPE */
1600 
1601 		/*
1602 		 * Check for quoted strings
1603 		 */
1604 		if (!em && (*cp == PARSE_QUOTE)) {
1605 			qm ^= 1;
1606 			if (qm) {
1607 				quote = cp + 1;
1608 			} else {
1609 				*cp = NULL;
1610 				if (state == PT_STATE_CHECK) {
1611 					if (strcmp(token, cb_nnamestr) == 0) {
1612 						cdsp->nodename = kmem_alloc(
1613 						    strlen(quote) + 1,
1614 						    KM_SLEEP);
1615 						(void) strcpy(cdsp->nodename,
1616 						    quote);
1617 					} else
1618 						cardbus_add_stringprop(cdsp,
1619 						    token, quote,
1620 						    strlen(quote));
1621 				} else if (state != PT_STATE_STRING_VAR) {
1622 					cmn_err(CE_CONT,
1623 					    "cardbus_parse_devprop: "
1624 					    "unexpected string [%s] after "
1625 					    "[%s]\n", quote, token);
1626 					return (DDI_FAILURE);
1627 				} else {
1628 					if (strcmp(token, cb_bnamestr) == 0) {
1629 						cdsp->binding_name = kmem_alloc(
1630 						    strlen(quote) + 1,
1631 						    KM_SLEEP);
1632 						(void) strcpy(
1633 						    cdsp->binding_name, quote);
1634 					}
1635 				}
1636 				state = PT_STATE_TOKEN;
1637 			} /* if (qm) */
1638 		} /* PARSE_QUOTE */
1639 
1640 		em = 0;
1641 
1642 		if (!qm && (*cp == PARSE_SEMICOLON)) {
1643 			smc = 1;
1644 		}
1645 
1646 		/*
1647 		 * Check for tokens
1648 		 */
1649 		else if (!qm && (isalpha(*cp) || isxdigit(*cp))) {
1650 			int	tl;
1651 			unsigned	*intp;
1652 			ptoken = token;
1653 			token = find_token(&cp, &l, &eq);
1654 
1655 			switch (state) {
1656 			case PT_STATE_TOKEN:
1657 				if (smc) {
1658 					if (eq == PARSE_EQUALS)
1659 						state = PT_STATE_CHECK;
1660 					else
1661 						cardbus_add_prop(cdsp,
1662 						    DDI_PROP_TYPE_ANY,
1663 						    token,
1664 						    NULL, 0);
1665 				} else if (eq == PARSE_EQUALS)
1666 					switch (state = parse_token(token)) {
1667 					case PT_STATE_UNKNOWN:
1668 						cmn_err(CE_CONT,
1669 						    "cardbus_parse_devprop: "
1670 						    "unknown token [%s]\n",
1671 						    token);
1672 						state = PT_STATE_TOKEN;
1673 					} /* switch (parse_token) */
1674 				else
1675 					state = PT_STATE_TOKEN;
1676 				break;
1677 
1678 			case PT_STATE_CHECK:
1679 				switch (check_token(token, &tl)) {
1680 				case PT_STATE_DEC_VAR:
1681 					intp = (unsigned *)kmem_alloc(
1682 					    sizeof (int)*tl,
1683 					    KM_SLEEP);
1684 					if (token_to_dec(token, intp, tl))
1685 						cardbus_add_prop(cdsp,
1686 						    DDI_PROP_TYPE_INT, ptoken,
1687 						    (caddr_t)intp, tl);
1688 					else
1689 						kmem_free(intp,
1690 						    sizeof (int)*tl);
1691 					break;
1692 				case PT_STATE_HEX_VAR:
1693 					intp = (unsigned *)kmem_alloc(
1694 					    sizeof (int)*tl,
1695 					    KM_SLEEP);
1696 					if (token_to_hex(token, intp, tl))
1697 						cardbus_add_prop(cdsp,
1698 						    DDI_PROP_TYPE_INT,
1699 						    ptoken,
1700 						    (caddr_t)intp, tl);
1701 					else
1702 						kmem_free(intp,
1703 						    sizeof (int)*tl);
1704 					break;
1705 				case PT_STATE_STRING_VAR:
1706 					if (strcmp(ptoken, cb_nnamestr) == 0) {
1707 						cdsp->nodename = kmem_alloc(
1708 						    tl + 1, KM_SLEEP);
1709 						(void) strcpy(cdsp->nodename,
1710 						    token);
1711 					} else
1712 						cardbus_add_stringprop(cdsp,
1713 						    ptoken, token, tl);
1714 					break;
1715 				}
1716 				state = PT_STATE_TOKEN;
1717 				break;
1718 
1719 			case PT_STATE_HEX_VAR:
1720 				if (strcmp(ptoken, cb_venidstr) == 0) {
1721 					uint_t val;
1722 					if (token_to_hex(token, &val, 1))
1723 						cdsp->venid = val;
1724 				} else if (strcmp(ptoken, cb_devidstr) == 0) {
1725 					uint_t val;
1726 					if (token_to_hex(token, &val, 1))
1727 						cdsp->devid = val;
1728 				}
1729 				state = PT_STATE_TOKEN;
1730 				break;
1731 
1732 			case PT_STATE_DEC_VAR:
1733 				if (strcmp(ptoken, cb_venidstr) == 0) {
1734 					uint_t val;
1735 					if (token_to_dec(token, &val, 1))
1736 						cdsp->venid = val;
1737 				} else if (strcmp(ptoken, cb_devidstr) == 0) {
1738 					uint_t val;
1739 					if (token_to_dec(token, &val, 1))
1740 						cdsp->devid = val;
1741 				}
1742 				state = PT_STATE_TOKEN;
1743 				break;
1744 
1745 			case PT_STATE_STRING_VAR:
1746 				if (strcmp(ptoken, cb_bnamestr) == 0) {
1747 					cdsp->binding_name = kmem_alloc(
1748 					    strlen(token) + 1, KM_SLEEP);
1749 					(void) strcpy(cdsp->binding_name,
1750 					    token);
1751 				}
1752 				state = PT_STATE_TOKEN;
1753 				break;
1754 
1755 			default:
1756 				cmn_err(CE_CONT, "cardbus_parse_devprop: "
1757 				    "unknown state machine state = %d\n",
1758 				    state);
1759 
1760 				cardbus_devprops_free(cdsp);
1761 				return (DDI_FAILURE);
1762 			} /* switch (state) */
1763 			if (eq == PARSE_SEMICOLON)
1764 				smc = 1;
1765 		}
1766 		cp++;
1767 		l++;
1768 	} /* while (*cp) */
1769 
1770 	if (qm) {
1771 		cmn_err(CE_CONT, "cb_props_parse_line: unterminated "
1772 		    "string = [%s]\n", quote);
1773 		cardbus_devprops_free(cdsp);
1774 		return (DDI_FAILURE);
1775 	}
1776 
1777 	if (state != PT_STATE_TOKEN) {
1778 		cmn_err(CE_CONT, "cardbus_parse_devprop: token [%s] "
1779 		    "requires value\n", token);
1780 		cardbus_devprops_free(cdsp);
1781 		return (DDI_FAILURE);
1782 	}
1783 
1784 	if (cdsp->venid == 0 || cdsp->devid == 0) {
1785 		cmn_err(CE_CONT, "cardbus_parse_devprop: Entry "
1786 		    "requires VendorID and DeviceID\n");
1787 		cardbus_devprops_free(cdsp);
1788 		return (DDI_FAILURE);
1789 	}
1790 
1791 	cdsp->next = cbp->cb_dsp;
1792 	cbp->cb_dsp = cdsp;
1793 	return (DDI_SUCCESS);
1794 }
1795 
1796 static void
1797 cardbus_device_props(cbus_t *cbp)
1798 {
1799 	char	**prop_array;
1800 	uint_t i, n;
1801 
1802 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, cbp->cb_dip,
1803 	    DDI_PROP_DONTPASS,
1804 	    "cb-device-init-props", &prop_array,
1805 	    &n) != DDI_PROP_SUCCESS)
1806 		return;
1807 
1808 	for (i = 0; i < n; i++)
1809 		(void) cardbus_parse_devprop(cbp, prop_array[i]);
1810 
1811 	ddi_prop_free(prop_array);
1812 }
1813 
1814 static int
1815 cardbus_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
1816 		off_t offset, off_t len, caddr_t *vaddrp)
1817 {
1818 	register dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
1819 	int	rc;
1820 
1821 	cardbus_err(dip, 9,
1822 	    "cardbus_bus_map(dip=0x%p, rdip=0x%p)\n",
1823 	    (void *) dip, (void *) rdip);
1824 
1825 	if (pdip == NULL)
1826 		return (DDI_FAILURE);
1827 
1828 	/* A child has asked us to set something up */
1829 	cardbus_err(dip, 9,
1830 	    "cardbus_bus_map(%s) calling %s - 0x%p, "
1831 	    "offset 0x%x, len 0x%x\n",
1832 	    ddi_driver_name(rdip),
1833 	    ddi_driver_name(pdip),
1834 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_map,
1835 	    (int)offset, (int)len);
1836 
1837 	rc = (DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
1838 	    (pdip, rdip, mp, offset, len, vaddrp);
1839 	/* rc = ddi_map(dip, mp, offset, len, vaddrp); */
1840 
1841 	if (rc != DDI_SUCCESS) {
1842 		cardbus_err(rdip, 8, "cardbus_bus_map failed, rc = %d\n", rc);
1843 		return (DDI_FAILURE);
1844 	} else {
1845 		cardbus_err(rdip, 9, "cardbus_bus_map OK\n");
1846 		return (DDI_SUCCESS);
1847 	}
1848 }
1849 
1850 static void
1851 pcirp2rp(const pci_regspec_t *pci_rp, struct regspec *rp)
1852 {
1853 	/* bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi); */
1854 	if (PCI_REG_ADDR_G(pci_rp->pci_phys_hi) ==
1855 	    PCI_REG_ADDR_G(PCI_ADDR_IO)) {
1856 		/* I/O */
1857 		rp->regspec_bustype = 1;
1858 	} else {
1859 		/* memory */
1860 		rp->regspec_bustype = 0;
1861 	}
1862 	rp->regspec_addr = pci_rp->pci_phys_low;
1863 	rp->regspec_size = pci_rp->pci_size_low;
1864 }
1865 
1866 static int
1867 cardbus_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
1868 		int (*waitfp)(caddr_t), caddr_t arg,
1869 		ddi_dma_handle_t *handlep)
1870 {
1871 	dev_info_t *pdip = ddi_get_parent(dip);
1872 
1873 	cardbus_err(dip, 10,
1874 	    "cardbus_dma_allochdl(dip=0x%p, rdip=0x%p)\n",
1875 	    (void *) dip, (void *) rdip);
1876 
1877 	if (pdip == NULL)
1878 		return (DDI_FAILURE);
1879 
1880 	cardbus_err(dip, 11,
1881 	    "cardbus_dma_allochdl calling %s - 0x%p\n",
1882 	    ddi_driver_name(pdip),
1883 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_allochdl);
1884 
1885 	return (ddi_dma_allochdl(dip, rdip, attr, waitfp, arg, handlep));
1886 }
1887 
1888 static int
1889 cardbus_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
1890 		ddi_dma_handle_t handle)
1891 {
1892 	dev_info_t *pdip = ddi_get_parent(dip);
1893 
1894 	cardbus_err(dip, 10,
1895 	    "cardbus_dma_freehdl(dip=0x%p, rdip=0x%p)\n",
1896 	    (void *) dip, (void *) rdip);
1897 
1898 	if (pdip == NULL)
1899 		return (DDI_FAILURE);
1900 
1901 	cardbus_err(dip, 11,
1902 	    "cardbus_dma_freehdl calling %s - 0x%p\n",
1903 	    ddi_driver_name(pdip),
1904 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_freehdl);
1905 
1906 	return (ddi_dma_freehdl(dip, rdip, handle));
1907 }
1908 
1909 static int
1910 cardbus_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
1911 		ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
1912 		ddi_dma_cookie_t *cp, uint_t *ccountp)
1913 {
1914 	dev_info_t *pdip = ddi_get_parent(dip);
1915 
1916 	cardbus_err(dip, 10,
1917 	    "cardbus_dma_bindhdl(dip=0x%p, rdip=0x%p)\n",
1918 	    (void *) dip, (void *) rdip);
1919 
1920 	if (pdip == NULL)
1921 		return (DDI_FAILURE);
1922 
1923 	cardbus_err(dip, 11,
1924 	    "cardbus_dma_bindhdl calling %s - 0x%p\n",
1925 	    ddi_driver_name(pdip),
1926 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl);
1927 
1928 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl(pdip,
1929 	    rdip, handle, dmareq, cp, ccountp));
1930 }
1931 
1932 static int
1933 cardbus_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
1934 		ddi_dma_handle_t handle)
1935 {
1936 	dev_info_t *pdip = ddi_get_parent(dip);
1937 
1938 	cardbus_err(dip, 10,
1939 	    "cardbus_dma_unbindhdl(dip=0x%p, rdip=0x%p)\n",
1940 	    (void *) dip, (void *) rdip);
1941 
1942 	if (pdip == NULL)
1943 		return (DDI_FAILURE);
1944 
1945 	cardbus_err(dip, 11,
1946 	    "cardbus_dma_unbindhdl calling %s - 0x%p\n",
1947 	    ddi_driver_name(pdip),
1948 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl);
1949 
1950 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl(pdip,
1951 	    rdip, handle));
1952 }
1953 
1954 static int
1955 cardbus_dma_flush(dev_info_t *dip, dev_info_t *rdip,
1956 		ddi_dma_handle_t handle, off_t off, size_t len,
1957 		uint_t cache_flags)
1958 {
1959 	dev_info_t *pdip = ddi_get_parent(dip);
1960 
1961 	cardbus_err(dip, 10,
1962 	    "cardbus_dma_flush(dip=0x%p, rdip=0x%p)\n",
1963 	    (void *) dip, (void *) rdip);
1964 
1965 	if (pdip == NULL)
1966 		return (DDI_FAILURE);
1967 
1968 	cardbus_err(dip, 11,
1969 	    "cardbus_dma_flush calling %s - 0x%p\n",
1970 	    ddi_driver_name(pdip),
1971 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush);
1972 
1973 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush(pdip, rdip,
1974 	    handle, off, len, cache_flags));
1975 }
1976 
1977 static int
1978 cardbus_dma_win(dev_info_t *dip, dev_info_t *rdip,
1979 		ddi_dma_handle_t handle, uint_t win, off_t *offp,
1980 		size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
1981 {
1982 	dev_info_t *pdip = ddi_get_parent(dip);
1983 	cardbus_err(dip, 6,
1984 	    "cardbus_dma_win(dip=0x%p, rdip=0x%p)\n",
1985 	    (void *) dip, (void *) rdip);
1986 
1987 	if (pdip == NULL)
1988 		return (DDI_FAILURE);
1989 
1990 	cardbus_err(dip, 8,
1991 	    "cardbus_dma_win calling %s - 0x%p\n",
1992 	    ddi_driver_name(pdip),
1993 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win);
1994 
1995 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win(pdip, rdip,
1996 	    handle, win, offp, lenp, cookiep, ccountp));
1997 }
1998 
1999 static int
2000 cardbus_dma_map(dev_info_t *dip, dev_info_t *rdip,
2001 		struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
2002 {
2003 	dev_info_t *pdip = ddi_get_parent(dip);
2004 
2005 	cardbus_err(dip, 10,
2006 	    "cardbus_dma_map(dip=0x%p, rdip=0x%p)\n",
2007 	    (void *) dip, (void *) rdip);
2008 
2009 	if (pdip == NULL)
2010 		return (DDI_FAILURE);
2011 
2012 	cardbus_err(dip, 11,
2013 	    "cardbus_dma_map calling %s - 0x%p\n",
2014 	    ddi_driver_name(pdip),
2015 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map);
2016 
2017 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map(pdip, rdip,
2018 	    dmareqp, handlep));
2019 }
2020 
2021 static int
2022 cardbus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2023 		char *eventname, ddi_eventcookie_t *cookiep)
2024 {
2025 	cbus_t *cbp;
2026 	int	cb_instance;
2027 	int	rc;
2028 
2029 	/*
2030 	 * get the soft state structure for the bus instance.
2031 	 */
2032 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2033 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2034 	ASSERT(cb_instance >= 0);
2035 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2036 
2037 	cardbus_err(dip, 6, "cardbus_get_eventcookie %s\n", eventname);
2038 
2039 	ASSERT(number_of_cardbus_cards != 0);
2040 
2041 	if (cbp->cb_ndi_event_hdl == NULL) {
2042 		/*
2043 		 * We can't handle up (probably called at the attachment
2044 		 * point) so pass it on up
2045 		 */
2046 		dev_info_t *pdip = ddi_get_parent(dip);
2047 		cardbus_err(dip, 8,
2048 		    "cardbus_get_eventcookie calling %s - 0x%p\n",
2049 		    ddi_driver_name(pdip),
2050 		    (void *)
2051 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie);
2052 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2053 		    bus_get_eventcookie(pdip, rdip, eventname, cookiep));
2054 	}
2055 
2056 	cardbus_err(dip, 8,
2057 	    "cardbus_get_eventcookie calling ndi_event_retrieve_cookie\n");
2058 
2059 	rc = ndi_event_retrieve_cookie(cbp->cb_ndi_event_hdl, rdip, eventname,
2060 	    cookiep, NDI_EVENT_NOPASS);
2061 
2062 	cardbus_err(dip, 7,
2063 	    "cardbus_get_eventcookie rc %d cookie %p\n", rc, (void *)*cookiep);
2064 	return (rc);
2065 }
2066 
2067 static int
2068 cardbus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2069 		ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
2070 		ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
2071 		void *arg, ddi_callback_id_t *cb_id)
2072 {
2073 	cbus_t *cbp;
2074 	int	cb_instance;
2075 	int	rc;
2076 
2077 	/*
2078 	 * get the soft state structure for the bus instance.
2079 	 */
2080 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2081 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2082 	ASSERT(cb_instance >= 0);
2083 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2084 
2085 	cardbus_err(dip, 6, "cardbus_add_eventcall\n");
2086 
2087 	ASSERT(number_of_cardbus_cards != 0);
2088 
2089 	if (cbp->cb_ndi_event_hdl == NULL) {
2090 		/*
2091 		 * We can't handle up (probably called at the attachment
2092 		 * point) so pass it on up
2093 		 */
2094 		dev_info_t *pdip = ddi_get_parent(dip);
2095 		cardbus_err(dip, 8,
2096 		    "cardbus_add_eventcall calling %s - 0x%p\n",
2097 		    ddi_driver_name(pdip),
2098 		    (void *)
2099 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_add_eventcall);
2100 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2101 		    bus_add_eventcall(pdip, rdip, cookie, callback,
2102 		    arg, cb_id));
2103 	}
2104 
2105 	cardbus_err(dip, 8,
2106 	    "cardbus_add_eventcall calling ndi_event_add_callback\n");
2107 
2108 	rc = ndi_event_add_callback(cbp->cb_ndi_event_hdl, rdip, cookie,
2109 	    callback, arg, NDI_EVENT_NOPASS, cb_id);
2110 	cardbus_err(dip, 7,
2111 	    "cardbus_add_eventcall rc %d cookie %p\n", rc, (void *)cookie);
2112 	return (rc);
2113 }
2114 
2115 static int
2116 cardbus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2117 {
2118 	cbus_t *cbp;
2119 	int	cb_instance;
2120 
2121 	/*
2122 	 * get the soft state structure for the bus instance.
2123 	 */
2124 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2125 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2126 	ASSERT(cb_instance >= 0);
2127 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2128 
2129 	cardbus_err(dip, 6, "cardbus_remove_eventcall\n");
2130 
2131 	ASSERT(number_of_cardbus_cards != 0);
2132 
2133 	if (cbp->cb_ndi_event_hdl == NULL) {
2134 		/*
2135 		 * We can't handle up (probably called at the attachment
2136 		 * point) so pass it on up
2137 		 */
2138 		dev_info_t *pdip = ddi_get_parent(dip);
2139 		cardbus_err(dip, 8,
2140 		    "cardbus_remove_eventcall calling %s - 0x%p\n",
2141 		    ddi_driver_name(pdip),
2142 		    (void *)
2143 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_remove_eventcall);
2144 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2145 		    bus_remove_eventcall(pdip, cb_id));
2146 	}
2147 
2148 	return (ndi_event_remove_callback(cbp->cb_ndi_event_hdl, cb_id));
2149 }
2150 
2151 static int
2152 cardbus_post_event(dev_info_t *dip, dev_info_t *rdip,
2153 		ddi_eventcookie_t cookie, void *bus_impldata)
2154 {
2155 	_NOTE(ARGUNUSED(rdip, cookie, bus_impldata))
2156 	cardbus_err(dip, 1, "cardbus_post_event()\n");
2157 	return (DDI_FAILURE);
2158 }
2159 
2160 static int cardbus_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2161 		ddi_intr_handle_impl_t *hdlp);
2162 static int cardbus_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2163 		ddi_intr_handle_impl_t *hdlp);
2164 static int cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2165 		ddi_intr_handle_impl_t *hdlp);
2166 static int cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2167 		ddi_intr_handle_impl_t *hdlp);
2168 
2169 static int
2170 cardbus_get_pil(dev_info_t *dip)
2171 {
2172 	return ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2173 	    "interrupt-priorities", 6);
2174 }
2175 
2176 static int
2177 cardbus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
2178 	ddi_intr_handle_impl_t *hdlp, void *result)
2179 {
2180 	int ret = DDI_SUCCESS;
2181 
2182 #if defined(CARDBUS_DEBUG)
2183 	cardbus_err(dip, 8, "cardbus_intr_ops() intr_op=%d\n", (int)intr_op);
2184 #endif
2185 
2186 	switch (intr_op) {
2187 	case DDI_INTROP_GETCAP:
2188 		*(int *)result = DDI_INTR_FLAG_LEVEL;
2189 		break;
2190 	case DDI_INTROP_ALLOC:
2191 		*(int *)result = hdlp->ih_scratch1;
2192 		break;
2193 	case DDI_INTROP_FREE:
2194 		break;
2195 	case DDI_INTROP_GETPRI:
2196 		*(int *)result = hdlp->ih_pri ?
2197 		    hdlp->ih_pri : cardbus_get_pil(dip);
2198 		break;
2199 	case DDI_INTROP_SETPRI:
2200 		break;
2201 	case DDI_INTROP_ADDISR:
2202 	case DDI_INTROP_REMISR:
2203 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2204 			cardbus_err(dip, 1, "Only fixed interrupts\n");
2205 			return (DDI_FAILURE);
2206 		}
2207 		break;
2208 	case DDI_INTROP_ENABLE:
2209 		ret = cardbus_enable_intr_impl(dip, rdip, hdlp);
2210 		break;
2211 	case DDI_INTROP_DISABLE:
2212 		ret = cardbus_disable_intr_impl(dip, rdip, hdlp);
2213 		break;
2214 	case DDI_INTROP_NINTRS:
2215 	case DDI_INTROP_NAVAIL:
2216 #ifdef sparc
2217 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
2218 #else
2219 		*(int *)result = 1;
2220 #endif
2221 		break;
2222 	case DDI_INTROP_SUPPORTED_TYPES:
2223 		*(int *)result = DDI_INTR_TYPE_FIXED;
2224 		break;
2225 	default:
2226 		ret = DDI_ENOTSUP;
2227 		break;
2228 	}
2229 
2230 	return (ret);
2231 }
2232 
2233 static int
2234 cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2235 		ddi_intr_handle_impl_t *hdlp)
2236 {
2237 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
2238 	set_irq_handler_t sih;
2239 	uint_t socket = 0; /* We only support devices */
2240 			    /* with one socket per function */
2241 
2242 	ASSERT(anp != NULL);
2243 
2244 	cardbus_err(dip, 9,
2245 	    "cardbus_enable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
2246 	    "rdip=0x%p(%s)\n",
2247 	    (void *) hdlp->ih_cb_func,
2248 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
2249 	    (void *) rdip, ddi_driver_name(rdip));
2250 
2251 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2252 		cardbus_err(dip, 1, "Only fixed interrupts\n");
2253 		return (DDI_FAILURE);
2254 	}
2255 
2256 	sih.socket = socket;
2257 	sih.handler_id = (unsigned)(long)rdip;
2258 	sih.handler = (f_tt *)hdlp->ih_cb_func;
2259 	sih.arg1 = hdlp->ih_cb_arg1;
2260 	sih.arg2 = hdlp->ih_cb_arg2;
2261 	sih.irq = cardbus_get_pil(dip);
2262 
2263 	if ((*anp->an_if->pcif_set_interrupt)(dip, &sih) != SUCCESS)
2264 		return (DDI_FAILURE);
2265 
2266 	return (DDI_SUCCESS);
2267 }
2268 
2269 static int
2270 cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2271 		ddi_intr_handle_impl_t *hdlp)
2272 {
2273 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
2274 	clear_irq_handler_t cih;
2275 	uint_t socket = 0; /* We only support devices with 1 socket per */
2276 			    /* function. */
2277 
2278 	ASSERT(anp != NULL);
2279 
2280 	cardbus_err(dip, 9,
2281 	    "cardbus_disable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
2282 	    "rdip=0x%p(%s%d)\n",
2283 	    (void *) hdlp->ih_cb_func,
2284 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
2285 	    (void *) rdip, ddi_driver_name(rdip), ddi_get_instance(rdip));
2286 
2287 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2288 		cardbus_err(dip, 1, "Only fixed interrupts\n");
2289 		return (DDI_FAILURE);
2290 	}
2291 
2292 	cih.socket = socket;
2293 	cih.handler_id = (unsigned)(long)rdip;
2294 	cih.handler = (f_tt *)hdlp->ih_cb_func;
2295 
2296 	if ((*anp->an_if->pcif_clr_interrupt)(dip, &cih) != SUCCESS)
2297 		return (DDI_FAILURE);
2298 
2299 	return (DDI_SUCCESS);
2300 }
2301 
2302 #if defined(CARDBUS_DEBUG)
2303 static int	cardbus_do_pprintf = 0;
2304 #endif
2305 
2306 /*PRINTFLIKE3*/
2307 void
2308 cardbus_err(dev_info_t *dip, int level, const char *fmt, ...)
2309 {
2310 	if (cardbus_debug && (level <= cardbus_debug)) {
2311 		va_list adx;
2312 		int	instance;
2313 		char	buf[256];
2314 		const char	*name;
2315 		char	*nl = "";
2316 #if !defined(CARDBUS_DEBUG)
2317 		int	ce;
2318 		char	qmark = 0;
2319 
2320 		if (level <= 3)
2321 			ce = CE_WARN;
2322 		else
2323 			ce = CE_CONT;
2324 		if (level == 4)
2325 			qmark = 1;
2326 #endif
2327 
2328 		if (dip) {
2329 			instance = ddi_get_instance(dip);
2330 			/* name = ddi_binding_name(dip); */
2331 			name = ddi_driver_name(dip);
2332 		} else {
2333 			instance = 0;
2334 			name = "";
2335 		}
2336 
2337 		va_start(adx, fmt);
2338 		/* vcmn_err(ce, fmt, adx); */
2339 		/* vprintf(fmt, adx); */
2340 		/* prom_vprintf(fmt, adx); */
2341 		(void) vsprintf(buf, fmt, adx);
2342 		va_end(adx);
2343 
2344 		if (buf[strlen(buf) - 1] != '\n')
2345 			nl = "\n";
2346 
2347 #if defined(CARDBUS_DEBUG)
2348 		if (cardbus_do_pprintf) {
2349 			if (dip) {
2350 				if (instance >= 0)
2351 					prom_printf("%s(%d),0x%p: %s%s",
2352 					    name, instance, dip, buf, nl);
2353 				else
2354 					prom_printf("%s,0x%p: %s%s", name,
2355 					    dip, buf, nl);
2356 			} else
2357 				prom_printf("%s%s", buf, nl);
2358 		} else {
2359 			if (dip) {
2360 				if (instance >= 0)
2361 					cmn_err(CE_CONT, "%s(%d),0x%p: %s%s",
2362 					    name, instance, (void *) dip,
2363 					    buf, nl);
2364 				else
2365 					cmn_err(CE_CONT, "%s,0x%p: %s%s",
2366 					    name, (void *) dip, buf, nl);
2367 			} else
2368 				cmn_err(CE_CONT, "%s%s", buf, nl);
2369 		}
2370 #else
2371 		if (dip)
2372 			cmn_err(ce, qmark ? "?%s%d: %s%s" : "%s%d: %s%s",
2373 			    name, instance, buf, nl);
2374 		else
2375 			cmn_err(ce, qmark ? "?%s%s" : "%s%s", buf, nl);
2376 #endif
2377 	}
2378 }
2379 
2380 static void cardbus_expand_busrange(dev_info_t *dip)
2381 {
2382 	dev_info_t *pdip;
2383 	cardbus_bus_range_t *bus_range;
2384 	int len;
2385 
2386 	pdip = ddi_get_parent(dip);
2387 
2388 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, "bus-range",
2389 	    (caddr_t)&bus_range, &len) == DDI_PROP_SUCCESS) {
2390 		ndi_ra_request_t req;
2391 		uint64_t next_bus, blen;
2392 		uint32_t ret;
2393 		ddi_acc_handle_t handle;
2394 
2395 		if (bus_range->lo != bus_range->hi)
2396 			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2397 			    "%u -> %u\n", bus_range->lo, bus_range->hi);
2398 		else {
2399 
2400 			bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
2401 			req.ra_addr = bus_range->lo + 1;
2402 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
2403 			req.ra_len = 12;
2404 
2405 			while ((req.ra_len > 0) &&
2406 			    (ret = ndi_ra_alloc(ddi_get_parent(pdip), &req,
2407 			    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
2408 			    NDI_RA_PASS)) != NDI_SUCCESS)
2409 				req.ra_len--;
2410 
2411 			if (ret != NDI_SUCCESS) {
2412 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2413 				    "fail to allocate bus number\n");
2414 				goto exit;
2415 			}
2416 
2417 			bus_range->hi = bus_range->lo + req.ra_len;
2418 			if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip,
2419 			    "bus-range", (int *)bus_range, 2) != DDI_SUCCESS) {
2420 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2421 				    "fail to update bus-range property\n");
2422 				goto exit;
2423 			}
2424 
2425 			if (pci_config_setup(pdip, &handle) != DDI_SUCCESS) {
2426 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2427 				    "fail to pci_config_setup\n");
2428 				goto exit;
2429 			}
2430 
2431 			pci_config_put8(handle, PCI_BCNF_SECBUS, bus_range->lo);
2432 			pci_config_put8(handle, PCI_BCNF_SUBBUS, bus_range->hi);
2433 
2434 			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2435 			    "parent dip %u -> %u\n",
2436 			    pci_config_get8(handle, PCI_BCNF_SECBUS),
2437 			    pci_config_get8(handle, PCI_BCNF_SUBBUS));
2438 			pci_config_teardown(&handle);
2439 
2440 			if (ndi_ra_map_setup(pdip, NDI_RA_TYPE_PCI_BUSNUM)
2441 			    != NDI_SUCCESS) {
2442 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2443 				    "fail to ndi_ra_map_setup of bus number\n");
2444 				goto exit;
2445 			}
2446 
2447 			(void) ndi_ra_free(pdip,
2448 			    (uint64_t)bus_range->lo + 1, req.ra_len,
2449 			    NDI_RA_TYPE_PCI_BUSNUM, 0);
2450 		}
2451 
2452 		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
2453 		req.ra_len = 2;
2454 
2455 		while ((req.ra_len > 0) &&
2456 		    (ret = ndi_ra_alloc(pdip, &req,
2457 		    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
2458 		    0)) != NDI_SUCCESS)
2459 			req.ra_len--;
2460 
2461 		cardbus_err(dip, 1, "cardbus_expand_busrange: "
2462 		    "cardbus dip base %u length %d\n",
2463 		    (int)next_bus, (int)req.ra_len);
2464 
2465 		if (ret != NDI_SUCCESS) {
2466 			cardbus_err(dip, 1, "cardbus_expand_busrange: "
2467 			    "fail to allocate bus number of length %d "
2468 			    "from parent\n",
2469 			    (int)req.ra_len);
2470 			goto exit;
2471 		}
2472 
2473 		if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM)
2474 		    != NDI_SUCCESS) {
2475 			cardbus_err(dip, 1, "cardbus_expand_busrange: "
2476 			    "fail to ndi_ra_map_setup of bus numbers\n");
2477 			goto exit;
2478 		}
2479 
2480 		(void) ndi_ra_free(dip,
2481 		    (uint64_t)next_bus, req.ra_len,
2482 		    NDI_RA_TYPE_PCI_BUSNUM, 0);
2483 exit:
2484 		kmem_free(bus_range, len);
2485 
2486 	} else
2487 		cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2488 		    "parent dip doesn't have busrange prop\n");
2489 }
2490