xref: /titanic_41/usr/src/uts/common/io/cardbus/cardbus.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 
1815 #include <vm/seg_kmem.h>
1816 extern int	pf_is_memory(pfn_t);
1817 #define	BUSTYPE_TO_PFN(btype, pfn) (((btype) << 19) | ((pfn) & 0x7FFFF))
1818 
1819 static int
1820 cardbus_rootnex_map_regspec(ddi_map_req_t *mp, caddr_t *vaddrp,
1821 			uint_t mapping_attr)
1822 {
1823 	ulong_t base;
1824 	caddr_t kaddr;
1825 	pgcnt_t npages;
1826 	pfn_t	pfn;
1827 	uint_t	pgoffset;
1828 	struct regspec *rp = mp->map_obj.rp;
1829 
1830 	/* base addr */
1831 	base = (ulong_t)rp->regspec_addr & (~MMU_PAGEOFFSET);
1832 
1833 	/*
1834 	 * Take the bustype and the physical page base within the
1835 	 * bus space and turn it into a 28 bit page frame number.
1836 	 */
1837 	pfn = BUSTYPE_TO_PFN(rp->regspec_bustype, mmu_btop(base));
1838 
1839 	/*
1840 	 * Do a quick sanity check to make sure we are in I/O space.
1841 	 */
1842 	if (pf_is_memory(pfn))
1843 		return (DDI_ME_INVAL);
1844 
1845 	if (rp->regspec_size == 0) {
1846 		cardbus_err(NULL, 1,
1847 		    "cardbus_rootnex_map_regspec: zero regspec_size\n");
1848 		return (DDI_ME_INVAL);
1849 	}
1850 
1851 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING)
1852 		*vaddrp = (caddr_t)pfn;
1853 	else {
1854 		pgoffset = (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET;
1855 		npages = mmu_btopr(rp->regspec_size + pgoffset);
1856 
1857 		cardbus_err(NULL, 8,
1858 		    "cardbus_rootnex_map_regspec: "
1859 		    "Mapping %lu pages physical %x.%lx ",
1860 		    npages, rp->regspec_bustype, base);
1861 
1862 		kaddr = vmem_alloc(heap_arena, ptob(npages), VM_NOSLEEP);
1863 		if (kaddr == NULL)
1864 			return (DDI_ME_NORESOURCES);
1865 
1866 		/*
1867 		 * Now map in the pages we've allocated...
1868 		 */
1869 		hat_devload(kas.a_hat, kaddr, ptob(npages), pfn,
1870 		    mp->map_prot | mapping_attr, HAT_LOAD_LOCK);
1871 
1872 		*vaddrp = kaddr + pgoffset;
1873 	}
1874 
1875 	cardbus_err(NULL, 8,
1876 	    "cardbus_rootnex_map_regspec: at virtual %p\n", (void *)*vaddrp);
1877 	return (0);
1878 }
1879 
1880 static int
1881 cardbus_rootnex_unmap_regspec(ddi_map_req_t *mp, caddr_t *vaddrp)
1882 {
1883 	caddr_t addr = *vaddrp;
1884 	pgcnt_t npages;
1885 	uint_t  pgoffset;
1886 	caddr_t base;
1887 	struct regspec *rp;
1888 
1889 	if (mp->map_flags & DDI_MF_DEVICE_MAPPING)
1890 		return (0);
1891 
1892 	rp = mp->map_obj.rp;
1893 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
1894 
1895 	if (rp->regspec_size == 0) {
1896 		cardbus_err(NULL, 1,
1897 		    "cardbus_rootnex_unmap_regspec: zero regspec_size\n");
1898 		return (DDI_ME_INVAL);
1899 	}
1900 
1901 	base = addr - pgoffset;
1902 	npages = mmu_btopr(rp->regspec_size + pgoffset);
1903 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
1904 	vmem_free(heap_arena, base, ptob(npages));
1905 
1906 	/*
1907 	 * Destroy the pointer - the mapping has logically gone
1908 	 */
1909 	*vaddrp = (caddr_t)0;
1910 
1911 	return (0);
1912 }
1913 
1914 static int
1915 cardbus_rootnex_map_handle(ddi_map_req_t *mp)
1916 {
1917 	ddi_acc_hdl_t *hp;
1918 	uint_t hat_flags;
1919 	register struct regspec *rp;
1920 
1921 	/*
1922 	 * Set up the hat_flags for the mapping.
1923 	 */
1924 	hp = mp->map_handlep;
1925 
1926 	switch (hp->ah_acc.devacc_attr_endian_flags) {
1927 	case DDI_NEVERSWAP_ACC:
1928 		hat_flags = HAT_NEVERSWAP | HAT_STRICTORDER;
1929 		break;
1930 	case DDI_STRUCTURE_BE_ACC:
1931 		hat_flags = HAT_STRUCTURE_BE;
1932 		break;
1933 	case DDI_STRUCTURE_LE_ACC:
1934 		hat_flags = HAT_STRUCTURE_LE;
1935 		break;
1936 	default:
1937 		return (DDI_REGS_ACC_CONFLICT);
1938 	}
1939 
1940 	switch (hp->ah_acc.devacc_attr_dataorder) {
1941 	case DDI_STRICTORDER_ACC:
1942 		break;
1943 	case DDI_UNORDERED_OK_ACC:
1944 		hat_flags |= HAT_UNORDERED_OK;
1945 		break;
1946 	case DDI_MERGING_OK_ACC:
1947 		hat_flags |= HAT_MERGING_OK;
1948 		break;
1949 	case DDI_LOADCACHING_OK_ACC:
1950 		hat_flags |= HAT_LOADCACHING_OK;
1951 		break;
1952 	case DDI_STORECACHING_OK_ACC:
1953 		hat_flags |= HAT_STORECACHING_OK;
1954 		break;
1955 	default:
1956 		return (DDI_FAILURE);
1957 	}
1958 
1959 	rp = mp->map_obj.rp;
1960 	if (rp->regspec_size == 0)
1961 		return (DDI_ME_INVAL);
1962 
1963 	hp->ah_hat_flags = hat_flags;
1964 	hp->ah_pfn = mmu_btop((ulong_t)rp->regspec_addr & (~MMU_PAGEOFFSET));
1965 	hp->ah_pnum = mmu_btopr(rp->regspec_size +
1966 	    (ulong_t)rp->regspec_addr & MMU_PAGEOFFSET);
1967 	return (DDI_SUCCESS);
1968 }
1969 
1970 static int
1971 cardbus_rootnex_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
1972 		off_t offset, off_t len, caddr_t *vaddrp)
1973 {
1974 	struct regspec *rp, tmp_reg;
1975 	ddi_map_req_t mr = *mp;	/* Get private copy of request */
1976 	int	error;
1977 	uint_t mapping_attr;
1978 	ddi_acc_hdl_t *hp = NULL;
1979 
1980 	mp = &mr;
1981 
1982 	switch (mp->map_op)  {
1983 	case DDI_MO_MAP_LOCKED:
1984 	case DDI_MO_UNMAP:
1985 	case DDI_MO_MAP_HANDLE:
1986 		break;
1987 	default:
1988 		cardbus_err(dip, 1,
1989 		    "cardbus_rootnex_map: unimplemented map op %d.",
1990 		    mp->map_op);
1991 		return (DDI_ME_UNIMPLEMENTED);
1992 	}
1993 
1994 	if (mp->map_flags & DDI_MF_USER_MAPPING)  {
1995 		cardbus_err(dip, 1,
1996 		    "cardbus_rootnex_map: unimplemented map type: user.");
1997 		return (DDI_ME_UNIMPLEMENTED);
1998 	}
1999 
2000 	/*
2001 	 * First, if given an rnumber, convert it to a regspec...
2002 	 * (Presumably, this is on behalf of a child of the root node)
2003 	 */
2004 
2005 	if (mp->map_type == DDI_MT_RNUMBER)  {
2006 
2007 		int	rnumber = mp->map_obj.rnumber;
2008 
2009 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
2010 		if (rp == (struct regspec *)0)  {
2011 			cardbus_err(dip, 1,
2012 			    "cardbus_rootnex_map: Out of "
2013 			    "range rnumber <%d>, device <%s>",
2014 			    rnumber, ddi_get_name(rdip));
2015 			return (DDI_ME_RNUMBER_RANGE);
2016 		}
2017 
2018 		/*
2019 		 * Convert the given ddi_map_req_t from rnumber to regspec...
2020 		 */
2021 
2022 		mp->map_type = DDI_MT_REGSPEC;
2023 		mp->map_obj.rp = rp;
2024 	}
2025 
2026 	/*
2027 	 * Adjust offset and length correspnding to called values...
2028 	 */
2029 
2030 	tmp_reg = *(mp->map_obj.rp);	/* Preserve underlying data */
2031 	rp = mp->map_obj.rp = &tmp_reg;	/* Use tmp_reg in request */
2032 
2033 	rp->regspec_addr += (uint_t)offset;
2034 	if (len != 0)
2035 		rp->regspec_size = (uint_t)len;
2036 
2037 	/*
2038 	 * Apply any parent ranges at this level, if applicable.
2039 	 * (This is where nexus specific regspec translation takes place.
2040 	 * Use of this function is implicit agreement that translation is
2041 	 * provided via ddi_apply_range.)
2042 	 */
2043 	cardbus_err(dip, 1, "ppd: %p, %p\n",
2044 	    (void *) DEVI_PD(dip), (void *) DEVI_PD(rdip));
2045 
2046 	cardbus_err(dip, 8,
2047 	    "cardbus_rootnex_map: applying range of parent "
2048 	    "<%s> to child <%s>...\n", ddi_get_name(dip), ddi_get_name(rdip));
2049 
2050 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
2051 		return (error);
2052 
2053 	switch (mp->map_op)  {
2054 	case DDI_MO_MAP_LOCKED:
2055 		/*
2056 		 * Set up the locked down kernel mapping to the regspec...
2057 		 */
2058 
2059 		/*
2060 		 * If we were passed an access handle we need to determine
2061 		 * the "endian-ness" of the mapping and fill in the handle.
2062 		 */
2063 		if (mp->map_handlep) {
2064 			hp = mp->map_handlep;
2065 			switch (hp->ah_acc.devacc_attr_endian_flags) {
2066 			case DDI_NEVERSWAP_ACC:
2067 				mapping_attr = HAT_NEVERSWAP | HAT_STRICTORDER;
2068 				break;
2069 			case DDI_STRUCTURE_BE_ACC:
2070 				mapping_attr = HAT_STRUCTURE_BE;
2071 				break;
2072 			case DDI_STRUCTURE_LE_ACC:
2073 				mapping_attr = HAT_STRUCTURE_LE;
2074 				break;
2075 			default:
2076 				return (DDI_REGS_ACC_CONFLICT);
2077 			}
2078 
2079 			switch (hp->ah_acc.devacc_attr_dataorder) {
2080 			case DDI_STRICTORDER_ACC:
2081 				break;
2082 			case DDI_UNORDERED_OK_ACC:
2083 				mapping_attr |= HAT_UNORDERED_OK;
2084 				break;
2085 			case DDI_MERGING_OK_ACC:
2086 				mapping_attr |= HAT_MERGING_OK;
2087 				break;
2088 			case DDI_LOADCACHING_OK_ACC:
2089 				mapping_attr |= HAT_LOADCACHING_OK;
2090 				break;
2091 			case DDI_STORECACHING_OK_ACC:
2092 				mapping_attr |= HAT_STORECACHING_OK;
2093 				break;
2094 			default:
2095 				return (DDI_REGS_ACC_CONFLICT);
2096 			}
2097 		} else {
2098 			mapping_attr = HAT_NEVERSWAP | HAT_STRICTORDER;
2099 		}
2100 
2101 		/*
2102 		 * Set up the mapping.
2103 		 */
2104 		error = cardbus_rootnex_map_regspec(mp, vaddrp, mapping_attr);
2105 
2106 		/*
2107 		 * Fill in the access handle if needed.
2108 		 */
2109 		if (hp) {
2110 			hp->ah_addr = *vaddrp;
2111 			hp->ah_hat_flags = mapping_attr;
2112 			if (error == 0)
2113 				impl_acc_hdl_init(hp);
2114 		}
2115 		return (error);
2116 
2117 	case DDI_MO_UNMAP:
2118 		/*
2119 		 * Release mapping...
2120 		 */
2121 		return (cardbus_rootnex_unmap_regspec(mp, vaddrp));
2122 
2123 	case DDI_MO_MAP_HANDLE:
2124 		return (cardbus_rootnex_map_handle(mp));
2125 
2126 	}
2127 
2128 	return (DDI_ME_UNIMPLEMENTED);
2129 }
2130 
2131 /*
2132  * dip an ancestor of rdip. Allocate space on dip for rdip
2133  */
2134 static int
2135 cardbus_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2136 		off_t offset, off_t len, caddr_t *vaddrp)
2137 {
2138 	register dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
2139 	int	rc;
2140 
2141 	cardbus_err(dip, 9,
2142 	    "cardbus_bus_map(dip=0x%p, rdip=0x%p)\n",
2143 	    (void *) dip, (void *) rdip);
2144 
2145 	if (pdip == NULL)
2146 		return (DDI_FAILURE);
2147 
2148 	/* A child has asked us to set something up */
2149 	cardbus_err(dip, 9,
2150 	    "cardbus_bus_map(%s) calling %s - 0x%p, "
2151 	    "offset 0x%x, len 0x%x\n",
2152 	    ddi_driver_name(rdip),
2153 	    ddi_driver_name(pdip),
2154 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_map,
2155 	    (int)offset, (int)len);
2156 
2157 	rc = (DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
2158 	    (pdip, rdip, mp, offset, len, vaddrp);
2159 	/* rc = ddi_map(dip, mp, offset, len, vaddrp); */
2160 
2161 	if (rc != DDI_SUCCESS)
2162 		cardbus_err(rdip, 8, "cardbus_bus_map failed, rc = %d\n", rc);
2163 	else {
2164 		cardbus_err(rdip, 9, "cardbus_bus_map OK\n");
2165 		return (DDI_SUCCESS);
2166 	}
2167 
2168 	if (mp->map_type == DDI_MT_RNUMBER) {
2169 		int	rnumber = mp->map_obj.rnumber;
2170 		uint_t length;
2171 		pci_regspec_t *pci_rp;
2172 		struct regspec regspec;
2173 		ddi_map_req_t p_map_request;
2174 
2175 #if defined(CARDBUS_DEBUG)
2176 		if (cardbus_debug >= 9)
2177 			cardbus_dump_family_tree(dip);
2178 #endif
2179 
2180 		cardbus_err(rdip, 8,
2181 		    "cardbus_bus_map rnumber is %d\n", rnumber);
2182 
2183 		if (rnumber < 0)
2184 			return (DDI_ME_RNUMBER_RANGE);
2185 
2186 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
2187 		    DDI_PROP_DONTPASS, "reg",
2188 		    (int **)&pci_rp,
2189 		    &length) != DDI_PROP_SUCCESS)
2190 			return (DDI_FAILURE);
2191 
2192 		/*
2193 		 * I think this is bogus. You will never get a length of
2194 		 * zero if the lookup succeeds.
2195 		 */
2196 		if (length == 0) {
2197 			return (DDI_FAILURE);
2198 		}
2199 
2200 		/* cardbus_dump_reg(rdip, pci_rp, length); */
2201 
2202 		/* config space should have mapped in OK earlier */
2203 		ASSERT(rnumber > 0);
2204 		/* use as offset into assigned-addresses not reg */
2205 
2206 		if (rnumber >= length)
2207 			return (DDI_ME_RNUMBER_RANGE);
2208 
2209 		pci_rp += rnumber;
2210 
2211 		pcirp2rp(pci_rp, &regspec);
2212 		ddi_prop_free(pci_rp-rnumber);
2213 
2214 		regspec.regspec_addr += offset;
2215 		if (len)
2216 			regspec.regspec_size = len;
2217 
2218 		cardbus_err(dip, 1,
2219 		    "regspec: bustype 0x%x, addr 0x%x, size 0x%x\n",
2220 		    regspec.regspec_bustype,
2221 		    regspec.regspec_addr,
2222 		    regspec.regspec_size);
2223 
2224 		ASSERT(regspec.regspec_size > 0);
2225 
2226 		p_map_request = *mp;
2227 		p_map_request.map_type = DDI_MT_REGSPEC;
2228 		p_map_request.map_obj.rp = &regspec;
2229 
2230 		rc = cardbus_rootnex_map(dip, rdip, &p_map_request, 0, 0,
2231 		    vaddrp);
2232 		if (rc != DDI_SUCCESS)
2233 			cardbus_err(rdip, 8,
2234 			    "cardbus_bus_map(regspec): ddi_map failed, "
2235 			    "rc = %d\n", rc);
2236 		else {
2237 			cardbus_err(rdip, 7,
2238 			    "cardbus_bus_map(regspec): ddi_map OK\n");
2239 			return (DDI_SUCCESS);
2240 		}
2241 	}
2242 	return (rc);
2243 }
2244 
2245 static void
2246 pcirp2rp(const pci_regspec_t *pci_rp, struct regspec *rp)
2247 {
2248 	/* bus = PCI_REG_BUS_G(pci_rp->pci_phys_hi); */
2249 	if (PCI_REG_ADDR_G(pci_rp->pci_phys_hi) ==
2250 	    PCI_REG_ADDR_G(PCI_ADDR_IO)) {
2251 		/* I/O */
2252 		rp->regspec_bustype = 1;
2253 	} else {
2254 		/* memory */
2255 		rp->regspec_bustype = 0;
2256 	}
2257 	rp->regspec_addr = pci_rp->pci_phys_low;
2258 	rp->regspec_size = pci_rp->pci_size_low;
2259 }
2260 
2261 static int
2262 cardbus_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
2263 		int (*waitfp)(caddr_t), caddr_t arg,
2264 		ddi_dma_handle_t *handlep)
2265 {
2266 	dev_info_t *pdip = ddi_get_parent(dip);
2267 
2268 	cardbus_err(dip, 10,
2269 	    "cardbus_dma_allochdl(dip=0x%p, rdip=0x%p)\n",
2270 	    (void *) dip, (void *) rdip);
2271 
2272 	if (pdip == NULL)
2273 		return (DDI_FAILURE);
2274 
2275 	cardbus_err(dip, 11,
2276 	    "cardbus_dma_allochdl calling %s - 0x%p\n",
2277 	    ddi_driver_name(pdip),
2278 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_allochdl);
2279 
2280 	return (ddi_dma_allochdl(dip, rdip, attr, waitfp, arg, handlep));
2281 }
2282 
2283 static int
2284 cardbus_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
2285 		ddi_dma_handle_t handle)
2286 {
2287 	dev_info_t *pdip = ddi_get_parent(dip);
2288 
2289 	cardbus_err(dip, 10,
2290 	    "cardbus_dma_freehdl(dip=0x%p, rdip=0x%p)\n",
2291 	    (void *) dip, (void *) rdip);
2292 
2293 	if (pdip == NULL)
2294 		return (DDI_FAILURE);
2295 
2296 	cardbus_err(dip, 11,
2297 	    "cardbus_dma_freehdl calling %s - 0x%p\n",
2298 	    ddi_driver_name(pdip),
2299 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_freehdl);
2300 
2301 	return (ddi_dma_freehdl(dip, rdip, handle));
2302 }
2303 
2304 static int
2305 cardbus_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
2306 		ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
2307 		ddi_dma_cookie_t *cp, uint_t *ccountp)
2308 {
2309 	dev_info_t *pdip = ddi_get_parent(dip);
2310 
2311 	cardbus_err(dip, 10,
2312 	    "cardbus_dma_bindhdl(dip=0x%p, rdip=0x%p)\n",
2313 	    (void *) dip, (void *) rdip);
2314 
2315 	if (pdip == NULL)
2316 		return (DDI_FAILURE);
2317 
2318 	cardbus_err(dip, 11,
2319 	    "cardbus_dma_bindhdl calling %s - 0x%p\n",
2320 	    ddi_driver_name(pdip),
2321 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl);
2322 
2323 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_bindhdl(pdip,
2324 	    rdip, handle, dmareq, cp, ccountp));
2325 }
2326 
2327 static int
2328 cardbus_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
2329 		ddi_dma_handle_t handle)
2330 {
2331 	dev_info_t *pdip = ddi_get_parent(dip);
2332 
2333 	cardbus_err(dip, 10,
2334 	    "cardbus_dma_unbindhdl(dip=0x%p, rdip=0x%p)\n",
2335 	    (void *) dip, (void *) rdip);
2336 
2337 	if (pdip == NULL)
2338 		return (DDI_FAILURE);
2339 
2340 	cardbus_err(dip, 11,
2341 	    "cardbus_dma_unbindhdl calling %s - 0x%p\n",
2342 	    ddi_driver_name(pdip),
2343 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl);
2344 
2345 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl(pdip,
2346 	    rdip, handle));
2347 }
2348 
2349 static int
2350 cardbus_dma_flush(dev_info_t *dip, dev_info_t *rdip,
2351 		ddi_dma_handle_t handle, off_t off, size_t len,
2352 		uint_t cache_flags)
2353 {
2354 	dev_info_t *pdip = ddi_get_parent(dip);
2355 
2356 	cardbus_err(dip, 10,
2357 	    "cardbus_dma_flush(dip=0x%p, rdip=0x%p)\n",
2358 	    (void *) dip, (void *) rdip);
2359 
2360 	if (pdip == NULL)
2361 		return (DDI_FAILURE);
2362 
2363 	cardbus_err(dip, 11,
2364 	    "cardbus_dma_flush calling %s - 0x%p\n",
2365 	    ddi_driver_name(pdip),
2366 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush);
2367 
2368 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_flush(pdip, rdip,
2369 	    handle, off, len, cache_flags));
2370 }
2371 
2372 static int
2373 cardbus_dma_win(dev_info_t *dip, dev_info_t *rdip,
2374 		ddi_dma_handle_t handle, uint_t win, off_t *offp,
2375 		size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
2376 {
2377 	dev_info_t *pdip = ddi_get_parent(dip);
2378 	cardbus_err(dip, 6,
2379 	    "cardbus_dma_win(dip=0x%p, rdip=0x%p)\n",
2380 	    (void *) dip, (void *) rdip);
2381 
2382 	if (pdip == NULL)
2383 		return (DDI_FAILURE);
2384 
2385 	cardbus_err(dip, 8,
2386 	    "cardbus_dma_win calling %s - 0x%p\n",
2387 	    ddi_driver_name(pdip),
2388 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win);
2389 
2390 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_win(pdip, rdip,
2391 	    handle, win, offp, lenp, cookiep, ccountp));
2392 }
2393 
2394 static int
2395 cardbus_dma_map(dev_info_t *dip, dev_info_t *rdip,
2396 		struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
2397 {
2398 	dev_info_t *pdip = ddi_get_parent(dip);
2399 
2400 	cardbus_err(dip, 10,
2401 	    "cardbus_dma_map(dip=0x%p, rdip=0x%p)\n",
2402 	    (void *) dip, (void *) rdip);
2403 
2404 	if (pdip == NULL)
2405 		return (DDI_FAILURE);
2406 
2407 	cardbus_err(dip, 11,
2408 	    "cardbus_dma_map calling %s - 0x%p\n",
2409 	    ddi_driver_name(pdip),
2410 	    (void *) DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map);
2411 
2412 	return (DEVI(pdip)->devi_ops->devo_bus_ops->bus_dma_map(pdip, rdip,
2413 	    dmareqp, handlep));
2414 }
2415 
2416 static int
2417 cardbus_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2418 		char *eventname, ddi_eventcookie_t *cookiep)
2419 {
2420 	cbus_t *cbp;
2421 	int	cb_instance;
2422 	int	rc;
2423 
2424 	/*
2425 	 * get the soft state structure for the bus instance.
2426 	 */
2427 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2428 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2429 	ASSERT(cb_instance >= 0);
2430 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2431 
2432 	cardbus_err(dip, 6, "cardbus_get_eventcookie %s\n", eventname);
2433 
2434 	ASSERT(number_of_cardbus_cards != 0);
2435 
2436 	if (cbp->cb_ndi_event_hdl == NULL) {
2437 		/*
2438 		 * We can't handle up (probably called at the attachment
2439 		 * point) so pass it on up
2440 		 */
2441 		dev_info_t *pdip = ddi_get_parent(dip);
2442 		cardbus_err(dip, 8,
2443 		    "cardbus_get_eventcookie calling %s - 0x%p\n",
2444 		    ddi_driver_name(pdip),
2445 		    (void *)
2446 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie);
2447 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2448 		    bus_get_eventcookie(pdip, rdip, eventname, cookiep));
2449 	}
2450 
2451 	cardbus_err(dip, 8,
2452 	    "cardbus_get_eventcookie calling ndi_event_retrieve_cookie\n");
2453 
2454 	rc = ndi_event_retrieve_cookie(cbp->cb_ndi_event_hdl, rdip, eventname,
2455 	    cookiep, NDI_EVENT_NOPASS);
2456 
2457 	cardbus_err(dip, 7,
2458 	    "cardbus_get_eventcookie rc %d cookie %p\n", rc, (void *)*cookiep);
2459 	return (rc);
2460 }
2461 
2462 static int
2463 cardbus_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2464 		ddi_eventcookie_t cookie, void (*callback)(dev_info_t *dip,
2465 		ddi_eventcookie_t cookie, void *arg, void *bus_impldata),
2466 		void *arg, ddi_callback_id_t *cb_id)
2467 {
2468 	cbus_t *cbp;
2469 	int	cb_instance;
2470 	int	rc;
2471 
2472 	/*
2473 	 * get the soft state structure for the bus instance.
2474 	 */
2475 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2476 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2477 	ASSERT(cb_instance >= 0);
2478 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2479 
2480 	cardbus_err(dip, 6, "cardbus_add_eventcall\n");
2481 
2482 	ASSERT(number_of_cardbus_cards != 0);
2483 
2484 	if (cbp->cb_ndi_event_hdl == NULL) {
2485 		/*
2486 		 * We can't handle up (probably called at the attachment
2487 		 * point) so pass it on up
2488 		 */
2489 		dev_info_t *pdip = ddi_get_parent(dip);
2490 		cardbus_err(dip, 8,
2491 		    "cardbus_add_eventcall calling %s - 0x%p\n",
2492 		    ddi_driver_name(pdip),
2493 		    (void *)
2494 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_add_eventcall);
2495 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2496 		    bus_add_eventcall(pdip, rdip, cookie, callback,
2497 		    arg, cb_id));
2498 	}
2499 
2500 	cardbus_err(dip, 8,
2501 	    "cardbus_add_eventcall calling ndi_event_add_callback\n");
2502 
2503 	rc = ndi_event_add_callback(cbp->cb_ndi_event_hdl, rdip, cookie,
2504 	    callback, arg, NDI_EVENT_NOPASS, cb_id);
2505 	cardbus_err(dip, 7,
2506 	    "cardbus_add_eventcall rc %d cookie %p\n", rc, (void *)cookie);
2507 	return (rc);
2508 }
2509 
2510 static int
2511 cardbus_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2512 {
2513 	cbus_t *cbp;
2514 	int	cb_instance;
2515 
2516 	/*
2517 	 * get the soft state structure for the bus instance.
2518 	 */
2519 	cb_instance = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2520 	    DDI_PROP_DONTPASS, "cbus-instance", -1);
2521 	ASSERT(cb_instance >= 0);
2522 	cbp = (cbus_t *)ddi_get_soft_state(cardbus_state, cb_instance);
2523 
2524 	cardbus_err(dip, 6, "cardbus_remove_eventcall\n");
2525 
2526 	ASSERT(number_of_cardbus_cards != 0);
2527 
2528 	if (cbp->cb_ndi_event_hdl == NULL) {
2529 		/*
2530 		 * We can't handle up (probably called at the attachment
2531 		 * point) so pass it on up
2532 		 */
2533 		dev_info_t *pdip = ddi_get_parent(dip);
2534 		cardbus_err(dip, 8,
2535 		    "cardbus_remove_eventcall calling %s - 0x%p\n",
2536 		    ddi_driver_name(pdip),
2537 		    (void *)
2538 		    DEVI(pdip)->devi_ops->devo_bus_ops->bus_remove_eventcall);
2539 		return (DEVI(pdip)->devi_ops->devo_bus_ops->
2540 		    bus_remove_eventcall(pdip, cb_id));
2541 	}
2542 
2543 	return (ndi_event_remove_callback(cbp->cb_ndi_event_hdl, cb_id));
2544 }
2545 
2546 static int
2547 cardbus_post_event(dev_info_t *dip, dev_info_t *rdip,
2548 		ddi_eventcookie_t cookie, void *bus_impldata)
2549 {
2550 	_NOTE(ARGUNUSED(rdip, cookie, bus_impldata))
2551 	cardbus_err(dip, 1, "cardbus_post_event()\n");
2552 	return (DDI_FAILURE);
2553 }
2554 
2555 static int cardbus_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2556 		ddi_intr_handle_impl_t *hdlp);
2557 static int cardbus_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2558 		ddi_intr_handle_impl_t *hdlp);
2559 static int cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2560 		ddi_intr_handle_impl_t *hdlp);
2561 static int cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2562 		ddi_intr_handle_impl_t *hdlp);
2563 
2564 static int
2565 cardbus_get_pil(dev_info_t *dip)
2566 {
2567 	return ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2568 	    "interrupt-priorities", 6);
2569 }
2570 
2571 static int
2572 cardbus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
2573 	ddi_intr_handle_impl_t *hdlp, void *result)
2574 {
2575 	int ret = DDI_SUCCESS;
2576 
2577 #if defined(CARDBUS_DEBUG)
2578 	cardbus_err(dip, 8, "cardbus_intr_ops() intr_op=%d\n", (int)intr_op);
2579 #endif
2580 
2581 	switch (intr_op) {
2582 	case DDI_INTROP_GETCAP:
2583 		*(int *)result = DDI_INTR_FLAG_LEVEL;
2584 		break;
2585 	case DDI_INTROP_ALLOC:
2586 		*(int *)result = hdlp->ih_scratch1;
2587 		break;
2588 	case DDI_INTROP_FREE:
2589 		break;
2590 	case DDI_INTROP_GETPRI:
2591 		*(int *)result = hdlp->ih_pri ?
2592 		    hdlp->ih_pri : cardbus_get_pil(dip);
2593 		break;
2594 	case DDI_INTROP_SETPRI:
2595 		break;
2596 	case DDI_INTROP_ADDISR:
2597 	case DDI_INTROP_REMISR:
2598 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2599 			cardbus_err(dip, 1, "Only fixed interrupts\n");
2600 			return (DDI_FAILURE);
2601 		}
2602 		break;
2603 	case DDI_INTROP_ENABLE:
2604 		ret = cardbus_enable_intr_impl(dip, rdip, hdlp);
2605 		break;
2606 	case DDI_INTROP_DISABLE:
2607 		ret = cardbus_disable_intr_impl(dip, rdip, hdlp);
2608 		break;
2609 	case DDI_INTROP_NINTRS:
2610 	case DDI_INTROP_NAVAIL:
2611 #ifdef sparc
2612 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
2613 #else
2614 		*(int *)result = 1;
2615 #endif
2616 		break;
2617 	case DDI_INTROP_SUPPORTED_TYPES:
2618 		*(int *)result = DDI_INTR_TYPE_FIXED;
2619 		break;
2620 	default:
2621 		ret = DDI_ENOTSUP;
2622 		break;
2623 	}
2624 
2625 	return (ret);
2626 }
2627 
2628 static int
2629 cardbus_enable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2630 		ddi_intr_handle_impl_t *hdlp)
2631 {
2632 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
2633 	set_irq_handler_t sih;
2634 	uint_t socket = 0; /* We only support devices */
2635 			    /* with one socket per function */
2636 
2637 	ASSERT(anp != NULL);
2638 
2639 	cardbus_err(dip, 9,
2640 	    "cardbus_enable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
2641 	    "rdip=0x%p(%s)\n",
2642 	    (void *) hdlp->ih_cb_func,
2643 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
2644 	    (void *) rdip, ddi_driver_name(rdip));
2645 
2646 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2647 		cardbus_err(dip, 1, "Only fixed interrupts\n");
2648 		return (DDI_FAILURE);
2649 	}
2650 
2651 	sih.socket = socket;
2652 	sih.handler_id = (unsigned)(long)rdip;
2653 	sih.handler = (f_tt *)hdlp->ih_cb_func;
2654 	sih.arg1 = hdlp->ih_cb_arg1;
2655 	sih.arg2 = hdlp->ih_cb_arg2;
2656 	sih.irq = cardbus_get_pil(dip);
2657 
2658 	if ((*anp->an_if->pcif_set_interrupt)(dip, &sih) != SUCCESS)
2659 		return (DDI_FAILURE);
2660 
2661 	return (DDI_SUCCESS);
2662 }
2663 
2664 static int
2665 cardbus_disable_intr_impl(dev_info_t *dip, dev_info_t *rdip,
2666 		ddi_intr_handle_impl_t *hdlp)
2667 {
2668 	anp_t *anp = (anp_t *)ddi_get_driver_private(dip);
2669 	clear_irq_handler_t cih;
2670 	uint_t socket = 0; /* We only support devices with 1 socket per */
2671 			    /* function. */
2672 
2673 	ASSERT(anp != NULL);
2674 
2675 	cardbus_err(dip, 9,
2676 	    "cardbus_disable_intr_impl, intr=0x%p, arg1=0x%p, arg2=0x%p"
2677 	    "rdip=0x%p(%s%d)\n",
2678 	    (void *) hdlp->ih_cb_func,
2679 	    hdlp->ih_cb_arg1, hdlp->ih_cb_arg2,
2680 	    (void *) rdip, ddi_driver_name(rdip), ddi_get_instance(rdip));
2681 
2682 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) {
2683 		cardbus_err(dip, 1, "Only fixed interrupts\n");
2684 		return (DDI_FAILURE);
2685 	}
2686 
2687 	cih.socket = socket;
2688 	cih.handler_id = (unsigned)(long)rdip;
2689 	cih.handler = (f_tt *)hdlp->ih_cb_func;
2690 
2691 	if ((*anp->an_if->pcif_clr_interrupt)(dip, &cih) != SUCCESS)
2692 		return (DDI_FAILURE);
2693 
2694 	return (DDI_SUCCESS);
2695 }
2696 
2697 #if defined(CARDBUS_DEBUG)
2698 static int	cardbus_do_pprintf = 0;
2699 #endif
2700 
2701 /*PRINTFLIKE3*/
2702 void
2703 cardbus_err(dev_info_t *dip, int level, const char *fmt, ...)
2704 {
2705 	if (cardbus_debug && (level <= cardbus_debug)) {
2706 		va_list adx;
2707 		int	instance;
2708 		char	buf[256];
2709 		const char	*name;
2710 		char	*nl = "";
2711 #if !defined(CARDBUS_DEBUG)
2712 		int	ce;
2713 		char	qmark = 0;
2714 
2715 		if (level <= 3)
2716 			ce = CE_WARN;
2717 		else
2718 			ce = CE_CONT;
2719 		if (level == 4)
2720 			qmark = 1;
2721 #endif
2722 
2723 		if (dip) {
2724 			instance = ddi_get_instance(dip);
2725 			/* name = ddi_binding_name(dip); */
2726 			name = ddi_driver_name(dip);
2727 		} else {
2728 			instance = 0;
2729 			name = "";
2730 		}
2731 
2732 		va_start(adx, fmt);
2733 		/* vcmn_err(ce, fmt, adx); */
2734 		/* vprintf(fmt, adx); */
2735 		/* prom_vprintf(fmt, adx); */
2736 		(void) vsprintf(buf, fmt, adx);
2737 		va_end(adx);
2738 
2739 		if (buf[strlen(buf) - 1] != '\n')
2740 			nl = "\n";
2741 
2742 #if defined(CARDBUS_DEBUG)
2743 		if (cardbus_do_pprintf) {
2744 			if (dip) {
2745 				if (instance >= 0)
2746 					prom_printf("%s(%d),0x%p: %s%s",
2747 					    name, instance, dip, buf, nl);
2748 				else
2749 					prom_printf("%s,0x%p: %s%s", name,
2750 					    dip, buf, nl);
2751 			} else
2752 				prom_printf("%s%s", buf, nl);
2753 		} else {
2754 			if (dip) {
2755 				if (instance >= 0)
2756 					cmn_err(CE_CONT, "%s(%d),0x%p: %s%s",
2757 					    name, instance, (void *) dip,
2758 					    buf, nl);
2759 				else
2760 					cmn_err(CE_CONT, "%s,0x%p: %s%s",
2761 					    name, (void *) dip, buf, nl);
2762 			} else
2763 				cmn_err(CE_CONT, "%s%s", buf, nl);
2764 		}
2765 #else
2766 		if (dip)
2767 			cmn_err(ce, qmark ? "?%s%d: %s%s" : "%s%d: %s%s",
2768 			    name, instance, buf, nl);
2769 		else
2770 			cmn_err(ce, qmark ? "?%s%s" : "%s%s", buf, nl);
2771 #endif
2772 	}
2773 }
2774 
2775 static void cardbus_expand_busrange(dev_info_t *dip)
2776 {
2777 	dev_info_t *pdip;
2778 	cardbus_bus_range_t *bus_range;
2779 	int len;
2780 
2781 	pdip = ddi_get_parent(dip);
2782 
2783 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, "bus-range",
2784 	    (caddr_t)&bus_range, &len) == DDI_PROP_SUCCESS) {
2785 		ndi_ra_request_t req;
2786 		uint64_t next_bus, blen;
2787 		uint32_t ret;
2788 		ddi_acc_handle_t handle;
2789 
2790 		if (bus_range->lo != bus_range->hi)
2791 			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2792 			    "%u -> %u\n", bus_range->lo, bus_range->hi);
2793 		else {
2794 
2795 			bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
2796 			req.ra_addr = bus_range->lo + 1;
2797 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
2798 			req.ra_len = 12;
2799 
2800 			while ((req.ra_len > 0) &&
2801 			    (ret = ndi_ra_alloc(ddi_get_parent(pdip), &req,
2802 			    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
2803 			    NDI_RA_PASS)) != NDI_SUCCESS)
2804 				req.ra_len--;
2805 
2806 			if (ret != NDI_SUCCESS) {
2807 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2808 				    "fail to allocate bus number\n");
2809 				goto exit;
2810 			}
2811 
2812 			bus_range->hi = bus_range->lo + req.ra_len;
2813 			if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip,
2814 			    "bus-range", (int *)bus_range, 2) != DDI_SUCCESS) {
2815 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2816 				    "fail to update bus-range property\n");
2817 				goto exit;
2818 			}
2819 
2820 			if (pci_config_setup(pdip, &handle) != DDI_SUCCESS) {
2821 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2822 				    "fail to pci_config_setup\n");
2823 				goto exit;
2824 			}
2825 
2826 			pci_config_put8(handle, PCI_BCNF_SECBUS, bus_range->lo);
2827 			pci_config_put8(handle, PCI_BCNF_SUBBUS, bus_range->hi);
2828 
2829 			cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2830 			    "parent dip %u -> %u\n",
2831 			    pci_config_get8(handle, PCI_BCNF_SECBUS),
2832 			    pci_config_get8(handle, PCI_BCNF_SUBBUS));
2833 			pci_config_teardown(&handle);
2834 
2835 			if (ndi_ra_map_setup(pdip, NDI_RA_TYPE_PCI_BUSNUM)
2836 			    != NDI_SUCCESS) {
2837 				cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2838 				    "fail to ndi_ra_map_setup of bus number\n");
2839 				goto exit;
2840 			}
2841 
2842 			(void) ndi_ra_free(pdip,
2843 			    (uint64_t)bus_range->lo + 1, req.ra_len,
2844 			    NDI_RA_TYPE_PCI_BUSNUM, 0);
2845 		}
2846 
2847 		bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
2848 		req.ra_len = 2;
2849 
2850 		while ((req.ra_len > 0) &&
2851 		    (ret = ndi_ra_alloc(pdip, &req,
2852 		    &next_bus, &blen, NDI_RA_TYPE_PCI_BUSNUM,
2853 		    0)) != NDI_SUCCESS)
2854 			req.ra_len--;
2855 
2856 		cardbus_err(dip, 1, "cardbus_expand_busrange: "
2857 		    "cardbus dip base %u length %d\n",
2858 		    (int)next_bus, (int)req.ra_len);
2859 
2860 		if (ret != NDI_SUCCESS) {
2861 			cardbus_err(dip, 1, "cardbus_expand_busrange: "
2862 			    "fail to allocate bus number of length %d "
2863 			    "from parent\n",
2864 			    (int)req.ra_len);
2865 			goto exit;
2866 		}
2867 
2868 		if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM)
2869 		    != NDI_SUCCESS) {
2870 			cardbus_err(dip, 1, "cardbus_expand_busrange: "
2871 			    "fail to ndi_ra_map_setup of bus numbers\n");
2872 			goto exit;
2873 		}
2874 
2875 		(void) ndi_ra_free(dip,
2876 		    (uint64_t)next_bus, req.ra_len,
2877 		    NDI_RA_TYPE_PCI_BUSNUM, 0);
2878 exit:
2879 		kmem_free(bus_range, len);
2880 
2881 	} else
2882 		cardbus_err(pdip, 1, "cardbus_expand_busrange: "
2883 		    "parent dip doesn't have busrange prop\n");
2884 }
2885