xref: /illumos-gate/usr/src/uts/sun4/io/efcode/fcpci.c (revision 89b43686)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*
23b1a80202SZach Kissel  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*89b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * fcpci.c: Framework PCI fcode ops
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/pci.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
387c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
397c478bd9Sstevel@tonic-gate #include <sys/pci.h>
407c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
427c478bd9Sstevel@tonic-gate #include <sys/fcode.h>
437c478bd9Sstevel@tonic-gate #include <sys/promif.h>
447c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
454ab75253Smrj #include <sys/ddi_implfuncs.h>
467c478bd9Sstevel@tonic-gate 
4734d3f749Saa #define	PCI_NPT_bits		(PCI_RELOCAT_B | PCI_PREFETCH_B | PCI_ALIAS_B)
4834d3f749Saa #define	PCI_BDF_bits		(PCI_REG_BDFR_M & ~PCI_REG_REG_M)
4934d3f749Saa 
507c478bd9Sstevel@tonic-gate #define	PCICFG_CONF_INDIRECT_MAP	1
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int pfc_map_in(dev_info_t *, fco_handle_t, fc_ci_t *);
537c478bd9Sstevel@tonic-gate static int pfc_map_out(dev_info_t *, fco_handle_t, fc_ci_t *);
547c478bd9Sstevel@tonic-gate static int pfc_dma_map_in(dev_info_t *, fco_handle_t, fc_ci_t *);
557c478bd9Sstevel@tonic-gate static int pfc_dma_map_out(dev_info_t *, fco_handle_t, fc_ci_t *);
567c478bd9Sstevel@tonic-gate static int pfc_dma_sync(dev_info_t *, fco_handle_t, fc_ci_t *);
577c478bd9Sstevel@tonic-gate static int pfc_dma_cleanup(dev_info_t *, fco_handle_t, fc_ci_t *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int pfc_register_fetch(dev_info_t *, fco_handle_t, fc_ci_t *);
607c478bd9Sstevel@tonic-gate static int pfc_register_store(dev_info_t *, fco_handle_t, fc_ci_t *);
617c478bd9Sstevel@tonic-gate static int pfc_config_fetch(dev_info_t *, fco_handle_t, fc_ci_t *);
627c478bd9Sstevel@tonic-gate static int pfc_config_store(dev_info_t *, fco_handle_t, fc_ci_t *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static int pfc_probe_address(dev_info_t *, fco_handle_t, fc_ci_t *);
657c478bd9Sstevel@tonic-gate static int pfc_probe_space(dev_info_t *, fco_handle_t, fc_ci_t *);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int pfc_config_child(dev_info_t *, fco_handle_t, fc_ci_t *);
687c478bd9Sstevel@tonic-gate static int pfc_get_fcode_size(dev_info_t *, fco_handle_t, fc_ci_t *);
697c478bd9Sstevel@tonic-gate static int pfc_get_fcode(dev_info_t *, fco_handle_t, fc_ci_t *);
707c478bd9Sstevel@tonic-gate int prom_get_fcode_size(char *);
717c478bd9Sstevel@tonic-gate int prom_get_fcode(char *, char *);
727c478bd9Sstevel@tonic-gate int pfc_update_assigned_prop(dev_info_t *, pci_regspec_t *);
737c478bd9Sstevel@tonic-gate int pfc_remove_assigned_prop(dev_info_t *, pci_regspec_t *);
747c478bd9Sstevel@tonic-gate int pci_alloc_resource(dev_info_t *, pci_regspec_t);
757c478bd9Sstevel@tonic-gate int pci_free_resource(dev_info_t *, pci_regspec_t);
767c478bd9Sstevel@tonic-gate int pci_alloc_mem_chunk(dev_info_t *,  uint64_t, uint64_t *,  uint64_t *);
777c478bd9Sstevel@tonic-gate int pci_alloc_io_chunk(dev_info_t *,  uint64_t,  uint64_t *, uint64_t *);
787c478bd9Sstevel@tonic-gate static int fcpci_indirect_map(dev_info_t *);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int fcpci_unloadable;
817c478bd9Sstevel@tonic-gate 
826e90a4e7Smrj static ddi_dma_attr_t fcpci_dma_attr = {
836e90a4e7Smrj 	DMA_ATTR_V0,	/* version number */
846e90a4e7Smrj 	0x0,		/* lowest usable address */
856e90a4e7Smrj 	0xFFFFFFFFull,	/* high DMA address range */
866e90a4e7Smrj 	0xFFFFFFFFull,	/* DMA counter register */
876e90a4e7Smrj 	1,		/* DMA address alignment */
886e90a4e7Smrj 	1,		/* DMA burstsizes */
896e90a4e7Smrj 	1,		/* min effective DMA size */
906e90a4e7Smrj 	0xFFFFFFFFull,	/* max DMA xfer size */
916e90a4e7Smrj 	0xFFFFFFFFull,	/* segment boundary */
926e90a4e7Smrj 	1,		 /* s/g list length */
936e90a4e7Smrj 	1,		/* granularity of device */
946e90a4e7Smrj 	0		/* DMA transfer flags */
956e90a4e7Smrj };
966e90a4e7Smrj 
977c478bd9Sstevel@tonic-gate #define	HIADDR(n) ((uint32_t)(((uint64_t)(n) & 0xFFFFFFFF00000000)>> 32))
987c478bd9Sstevel@tonic-gate #define	LOADDR(n)((uint32_t)((uint64_t)(n) & 0x00000000FFFFFFFF))
997c478bd9Sstevel@tonic-gate #define	LADDR(lo, hi)    (((uint64_t)(hi) << 32) | (uint32_t)(lo))
1007c478bd9Sstevel@tonic-gate #define	PCI_4GIG_LIMIT 0xFFFFFFFFUL
1017c478bd9Sstevel@tonic-gate #define	PCI_MEMGRAN 0x100000
1027c478bd9Sstevel@tonic-gate #define	PCI_IOGRAN 0x1000
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
109f500b196SRichard Bean 	&mod_miscops, "FCode pci bus functions"
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1137c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
_init(void)1177c478bd9Sstevel@tonic-gate _init(void)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
_fini(void)1237c478bd9Sstevel@tonic-gate _fini(void)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	if (fcpci_unloadable)
1267c478bd9Sstevel@tonic-gate 		return (mod_remove(&modlinkage));
1277c478bd9Sstevel@tonic-gate 	return (EBUSY);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1317c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate struct pfc_ops_v {
1387c478bd9Sstevel@tonic-gate 	char *svc_name;
1397c478bd9Sstevel@tonic-gate 	fc_ops_t *f;
1407c478bd9Sstevel@tonic-gate };
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static struct pfc_ops_v pov[] = {
1437c478bd9Sstevel@tonic-gate 	{	"map-in",		pfc_map_in},
1447c478bd9Sstevel@tonic-gate 	{	"map-out",		pfc_map_out},
1457c478bd9Sstevel@tonic-gate 	{	"dma-map-in",		pfc_dma_map_in},
1467c478bd9Sstevel@tonic-gate 	{	"dma-map-out",		pfc_dma_map_out},
1477c478bd9Sstevel@tonic-gate 	{	"dma-sync",		pfc_dma_sync},
1487c478bd9Sstevel@tonic-gate 	{	"rx@",			pfc_register_fetch},
1497c478bd9Sstevel@tonic-gate 	{	"rl@",			pfc_register_fetch},
1507c478bd9Sstevel@tonic-gate 	{	"rw@",			pfc_register_fetch},
1517c478bd9Sstevel@tonic-gate 	{	"rb@",			pfc_register_fetch},
1527c478bd9Sstevel@tonic-gate 	{	"rx!",			pfc_register_store},
1537c478bd9Sstevel@tonic-gate 	{	"rl!",			pfc_register_store},
1547c478bd9Sstevel@tonic-gate 	{	"rw!",			pfc_register_store},
1557c478bd9Sstevel@tonic-gate 	{	"rb!",			pfc_register_store},
1567c478bd9Sstevel@tonic-gate 	{	"config-l@",		pfc_config_fetch},
1577c478bd9Sstevel@tonic-gate 	{	"config-w@",		pfc_config_fetch},
1587c478bd9Sstevel@tonic-gate 	{	"config-b@",		pfc_config_fetch},
1597c478bd9Sstevel@tonic-gate 	{	"config-l!",		pfc_config_store},
1607c478bd9Sstevel@tonic-gate 	{	"config-w!",		pfc_config_store},
1617c478bd9Sstevel@tonic-gate 	{	"config-b!",		pfc_config_store},
1627c478bd9Sstevel@tonic-gate 	{	FC_PROBE_ADDRESS,	pfc_probe_address},
1637c478bd9Sstevel@tonic-gate 	{	FC_PROBE_SPACE,		pfc_probe_space},
1647c478bd9Sstevel@tonic-gate 	{	FC_SVC_EXIT,		pfc_dma_cleanup},
1657c478bd9Sstevel@tonic-gate 	{	FC_CONFIG_CHILD,	pfc_config_child},
1667c478bd9Sstevel@tonic-gate 	{	FC_GET_FCODE_SIZE,	pfc_get_fcode_size},
1677c478bd9Sstevel@tonic-gate 	{	FC_GET_FCODE,		pfc_get_fcode},
1687c478bd9Sstevel@tonic-gate 	{	NULL,			NULL}
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static struct pfc_ops_v shared_pov[] = {
1727c478bd9Sstevel@tonic-gate 	{	FC_SVC_EXIT,		pfc_dma_cleanup},
1737c478bd9Sstevel@tonic-gate 	{	NULL,			NULL}
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate int pci_map_phys(dev_info_t *, pci_regspec_t *,
1777c478bd9Sstevel@tonic-gate     caddr_t *, ddi_device_acc_attr_t *, ddi_acc_handle_t *);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate void pci_unmap_phys(ddi_acc_handle_t *, pci_regspec_t *);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate fco_handle_t
pci_fc_ops_alloc_handle(dev_info_t * ap,dev_info_t * child,void * fcode,size_t fcode_size,char * unit_address,struct pci_ops_bus_args * up)1827c478bd9Sstevel@tonic-gate pci_fc_ops_alloc_handle(dev_info_t *ap, dev_info_t *child,
1837c478bd9Sstevel@tonic-gate     void *fcode, size_t fcode_size, char *unit_address,
1847c478bd9Sstevel@tonic-gate     struct pci_ops_bus_args *up)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	fco_handle_t rp;
1877c478bd9Sstevel@tonic-gate 	struct pci_ops_bus_args *bp = NULL;
1887c478bd9Sstevel@tonic-gate 	phandle_t h;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	rp = kmem_zalloc(sizeof (struct fc_resource_list), KM_SLEEP);
1917c478bd9Sstevel@tonic-gate 	rp->next_handle = fc_ops_alloc_handle(ap, child, fcode, fcode_size,
1927c478bd9Sstevel@tonic-gate 	    unit_address, NULL);
1937c478bd9Sstevel@tonic-gate 	rp->ap = ap;
1947c478bd9Sstevel@tonic-gate 	rp->child = child;
1957c478bd9Sstevel@tonic-gate 	rp->fcode = fcode;
1967c478bd9Sstevel@tonic-gate 	rp->fcode_size = fcode_size;
1977c478bd9Sstevel@tonic-gate 	if (unit_address) {
1987c478bd9Sstevel@tonic-gate 		char *buf;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		buf = kmem_zalloc(strlen(unit_address) + 1, KM_SLEEP);
2017c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, unit_address);
2027c478bd9Sstevel@tonic-gate 		rp->unit_address = buf;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	bp = kmem_zalloc(sizeof (struct pci_ops_bus_args), KM_SLEEP);
2067c478bd9Sstevel@tonic-gate 	*bp = *up;
2077c478bd9Sstevel@tonic-gate 	rp->bus_args = bp;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Add the child's nodeid to our table...
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	h = ddi_get_nodeid(rp->child);
2137c478bd9Sstevel@tonic-gate 	fc_add_dip_to_phandle(fc_handle_to_phandle_head(rp), rp->child, h);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (rp);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate void
pci_fc_ops_free_handle(fco_handle_t rp)2197c478bd9Sstevel@tonic-gate pci_fc_ops_free_handle(fco_handle_t rp)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	struct pci_ops_bus_args *bp;
2227c478bd9Sstevel@tonic-gate 	struct fc_resource *ip, *np;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	ASSERT(rp);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (rp->next_handle)
2277c478bd9Sstevel@tonic-gate 		fc_ops_free_handle(rp->next_handle);
2287c478bd9Sstevel@tonic-gate 	if (rp->unit_address)
2297c478bd9Sstevel@tonic-gate 		kmem_free(rp->unit_address, strlen(rp->unit_address) + 1);
2307c478bd9Sstevel@tonic-gate 	if ((bp = rp->bus_args) != NULL)
2317c478bd9Sstevel@tonic-gate 		kmem_free(bp, sizeof (struct pci_ops_bus_args));
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Release all the resources from the resource list
2357c478bd9Sstevel@tonic-gate 	 * XXX: We don't handle 'unknown' types, but we don't create them.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = np) {
2387c478bd9Sstevel@tonic-gate 		np = ip->next;
2397c478bd9Sstevel@tonic-gate 		switch (ip->type) {
2407c478bd9Sstevel@tonic-gate 		case RT_MAP:
2417c478bd9Sstevel@tonic-gate 			FC_DEBUG1(1, CE_CONT, "pci_fc_ops_free: "
2427c478bd9Sstevel@tonic-gate 			    "pci_unmap_phys(%p)\n", ip->fc_map_handle);
2437c478bd9Sstevel@tonic-gate 			pci_unmap_phys(&ip->fc_map_handle, ip->fc_regspec);
2447c478bd9Sstevel@tonic-gate 			kmem_free(ip->fc_regspec, sizeof (pci_regspec_t));
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		case RT_DMA:
2477c478bd9Sstevel@tonic-gate 			/* DMA has to be freed up at exit time */
2487c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pfc_fc_ops_free: DMA seen!\n");
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		default:
2517c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pci_fc_ops_free: "
2527c478bd9Sstevel@tonic-gate 			    "unknown resource type %d\n", ip->type);
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 		fc_rem_resource(rp, ip);
2567c478bd9Sstevel@tonic-gate 		kmem_free(ip, sizeof (struct fc_resource));
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	kmem_free(rp, sizeof (struct fc_resource_list));
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate int
pci_fc_ops(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)2627c478bd9Sstevel@tonic-gate pci_fc_ops(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	struct pfc_ops_v *pv;
2657c478bd9Sstevel@tonic-gate 	char *name = fc_cell2ptr(cp->svc_name);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	ASSERT(rp);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * First try the generic fc_ops. If the ops is a shared op,
2717c478bd9Sstevel@tonic-gate 	 * also call our local function.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	if (fc_ops(ap, rp->next_handle, cp) == 0) {
2747c478bd9Sstevel@tonic-gate 		for (pv = shared_pov; pv->svc_name != NULL; ++pv)
2757c478bd9Sstevel@tonic-gate 			if (strcmp(pv->svc_name, name) == 0)
2767c478bd9Sstevel@tonic-gate 				return (pv->f(ap, rp, cp));
2777c478bd9Sstevel@tonic-gate 		return (0);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	for (pv = pov; pv->svc_name != NULL; ++pv)
2817c478bd9Sstevel@tonic-gate 		if (strcmp(pv->svc_name, name) == 0)
2827c478bd9Sstevel@tonic-gate 			return (pv->f(ap, rp, cp));
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	FC_DEBUG1(9, CE_CONT, "pci_fc_ops: <%s> not serviced\n", name);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	return (-1);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Create a dma mapping for a given user address.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static int
pfc_dma_map_in(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)2937c478bd9Sstevel@tonic-gate pfc_dma_map_in(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	ddi_dma_handle_t h;
2967c478bd9Sstevel@tonic-gate 	int error;
2977c478bd9Sstevel@tonic-gate 	caddr_t virt;
2987c478bd9Sstevel@tonic-gate 	size_t len;
2997c478bd9Sstevel@tonic-gate 	uint_t flags = DDI_DMA_RDWR | DDI_DMA_CONSISTENT;
3007c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
3017c478bd9Sstevel@tonic-gate 	ddi_dma_cookie_t c;
3027c478bd9Sstevel@tonic-gate 	struct buf *bp;
3034ab75253Smrj 	uint_t ccnt;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 3)
3067c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 3"));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
3097c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * XXX: It's not clear what we should do with a non-cacheable request
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 2));
3157c478bd9Sstevel@tonic-gate 	len = fc_cell2size(fc_arg(cp, 1));
3167c478bd9Sstevel@tonic-gate #ifdef	notdef
3177c478bd9Sstevel@tonic-gate 	cacheable = fc_cell2int(fc_arg(cp, 0));	/* XXX: do what? */
3187c478bd9Sstevel@tonic-gate #endif
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	FC_DEBUG2(6, CE_CONT, "pcf_dma_map_in: virt %p, len %d\n", virt, len);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Set up the address space for physio from userland
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	error = fc_physio_setup(&bp, virt, len);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (error)  {
3287c478bd9Sstevel@tonic-gate 		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: fc_physio_setup failed "
3297c478bd9Sstevel@tonic-gate 		    "error: %d  virt: %p  len %d\n", error, virt, len);
3307c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "fc_physio_setup failed"));
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	FC_DEBUG1(9, CE_CONT, "pfc_dma_map_in: dma_map_in; bp = %p\n", bp);
3346e90a4e7Smrj 	error = fc_ddi_dma_alloc_handle(ap, &fcpci_dma_attr, DDI_DMA_SLEEP,
3356e90a4e7Smrj 	    NULL, &h);
3364ab75253Smrj 	if (error != DDI_SUCCESS)  {
3377c478bd9Sstevel@tonic-gate 		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: real dma-map-in failed "
3387c478bd9Sstevel@tonic-gate 		    "error: %d  virt: %p  len %d\n", error, virt, len);
3397c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "real dma-map-in failed"));
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3424ab75253Smrj 	error = fc_ddi_dma_buf_bind_handle(h, bp, flags, DDI_DMA_SLEEP, NULL,
3434ab75253Smrj 	    &c, &ccnt);
3444ab75253Smrj 	if ((error != DDI_DMA_MAPPED) || (ccnt != 1)) {
3454ab75253Smrj 		fc_ddi_dma_free_handle(&h);
3464ab75253Smrj 		FC_DEBUG3(1, CE_CONT, "pfc_dma_map_in: real dma-map-in failed "
3474ab75253Smrj 		    "error: %d  virt: %p  len %d\n", error, virt, len);
3484ab75253Smrj 		return (fc_priv_error(cp, "real dma-map-in failed"));
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (c.dmac_size < len)  {
3524ab75253Smrj 		error = fc_ddi_dma_unbind_handle(h);
3534ab75253Smrj 		if (error != DDI_SUCCESS) {
3544ab75253Smrj 			return (fc_priv_error(cp, "ddi_dma_unbind error"));
3554ab75253Smrj 		}
3564ab75253Smrj 		fc_ddi_dma_free_handle(&h);
3574ab75253Smrj 		return (fc_priv_error(cp, "ddi_dma_buf_bind size < len"));
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	FC_DEBUG1(9, CE_CONT, "pfc_dma_map_in: returning devaddr %x\n",
361b1a80202SZach Kissel 	    c.dmac_address);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
3647c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = fc_uint32_t2cell(c.dmac_address);	/* XXX size */
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Now we have to log this resource saving the handle and buf header
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	ip = kmem_zalloc(sizeof (struct fc_resource), KM_SLEEP);
3707c478bd9Sstevel@tonic-gate 	ip->type = RT_DMA;
3717c478bd9Sstevel@tonic-gate 	ip->fc_dma_virt = virt;
3727c478bd9Sstevel@tonic-gate 	ip->fc_dma_len = len;
3737c478bd9Sstevel@tonic-gate 	ip->fc_dma_handle = h;
3747c478bd9Sstevel@tonic-gate 	ip->fc_dma_devaddr = c.dmac_address;
3757c478bd9Sstevel@tonic-gate 	ip->fc_dma_bp = bp;
3767c478bd9Sstevel@tonic-gate 	fc_add_resource(rp, ip);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static int
pfc_dma_sync(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)3827c478bd9Sstevel@tonic-gate pfc_dma_sync(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	void *virt;
3857c478bd9Sstevel@tonic-gate 	size_t len;
3867c478bd9Sstevel@tonic-gate 	uint32_t devaddr;
3877c478bd9Sstevel@tonic-gate 	int error;
3887c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 3)
3917c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 3"));
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 2));
3947c478bd9Sstevel@tonic-gate 	devaddr = fc_cell2uint32_t(fc_arg(cp, 1));
3957c478bd9Sstevel@tonic-gate 	len = fc_cell2size(fc_arg(cp, 0));
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Find if this virt is 'within' a request we know about
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
4017c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next) {
4027c478bd9Sstevel@tonic-gate 		if (ip->type != RT_DMA)
4037c478bd9Sstevel@tonic-gate 			continue;
4047c478bd9Sstevel@tonic-gate 		if (ip->fc_dma_devaddr != devaddr)
4057c478bd9Sstevel@tonic-gate 			continue;
4067c478bd9Sstevel@tonic-gate 		if (((char *)virt >= (char *)ip->fc_dma_virt) &&
4077c478bd9Sstevel@tonic-gate 		    (((char *)virt + len) <=
4087c478bd9Sstevel@tonic-gate 		    ((char *)ip->fc_dma_virt + ip->fc_dma_len)))
4097c478bd9Sstevel@tonic-gate 			break;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (ip == NULL)
4147c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "request not within a "
4157c478bd9Sstevel@tonic-gate 		    "known dma mapping"));
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * We know about this request, so we trust it enough to sync it.
4197c478bd9Sstevel@tonic-gate 	 * Unfortunately, we don't know which direction, so we'll do
4207c478bd9Sstevel@tonic-gate 	 * both directions.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	error = fc_ddi_dma_sync(ip->fc_dma_handle,
4247c478bd9Sstevel@tonic-gate 	    (char *)virt - (char *)ip->fc_dma_virt, len, DDI_DMA_SYNC_FORCPU);
4257c478bd9Sstevel@tonic-gate 	error |= fc_ddi_dma_sync(ip->fc_dma_handle,
4267c478bd9Sstevel@tonic-gate 	    (char *)virt - (char *)ip->fc_dma_virt, len, DDI_DMA_SYNC_FORDEV);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (error)
4297c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "Call to ddi_dma_sync failed"));
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
4327c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate static int
pfc_dma_map_out(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)4367c478bd9Sstevel@tonic-gate pfc_dma_map_out(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	void *virt;
4397c478bd9Sstevel@tonic-gate 	size_t len;
4407c478bd9Sstevel@tonic-gate 	uint32_t devaddr;
4417c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
4424ab75253Smrj 	int e;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 3)
4457c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 3"));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 2));
4487c478bd9Sstevel@tonic-gate 	devaddr = fc_cell2uint32_t(fc_arg(cp, 1));
4497c478bd9Sstevel@tonic-gate 	len = fc_cell2size(fc_arg(cp, 0));
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Find if this virt matches a request we know about
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
4557c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next) {
4567c478bd9Sstevel@tonic-gate 		if (ip->type != RT_DMA)
4577c478bd9Sstevel@tonic-gate 			continue;
4587c478bd9Sstevel@tonic-gate 		if (ip->fc_dma_devaddr != devaddr)
4597c478bd9Sstevel@tonic-gate 			continue;
4607c478bd9Sstevel@tonic-gate 		if (ip->fc_dma_virt != virt)
4617c478bd9Sstevel@tonic-gate 			continue;
4627c478bd9Sstevel@tonic-gate 		if (len == ip->fc_dma_len)
4637c478bd9Sstevel@tonic-gate 			break;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (ip == NULL)
4687c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "request doesn't match a "
4697c478bd9Sstevel@tonic-gate 		    "known dma mapping"));
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4724ab75253Smrj 	 * ddi_dma_unbind_handle does an implied sync ...
4737c478bd9Sstevel@tonic-gate 	 */
4744ab75253Smrj 	e = fc_ddi_dma_unbind_handle(ip->fc_dma_handle);
4754ab75253Smrj 	if (e != DDI_SUCCESS) {
4764ab75253Smrj 		cmn_err(CE_CONT, "pfc_dma_map_out: ddi_dma_unbind failed!\n");
4774ab75253Smrj 	}
4784ab75253Smrj 	fc_ddi_dma_free_handle(&ip->fc_dma_handle);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Tear down the physio mappings
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	fc_physio_free(&ip->fc_dma_bp, ip->fc_dma_virt, ip->fc_dma_len);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * remove the resource from the list and release it.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	fc_rem_resource(rp, ip);
4897c478bd9Sstevel@tonic-gate 	kmem_free(ip, sizeof (struct fc_resource));
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
4927c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static struct fc_resource *
next_dma_resource(fco_handle_t rp)4967c478bd9Sstevel@tonic-gate next_dma_resource(fco_handle_t rp)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
5017c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next)
5027c478bd9Sstevel@tonic-gate 		if (ip->type == RT_DMA)
5037c478bd9Sstevel@tonic-gate 			break;
5047c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (ip);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
pfc_dma_cleanup(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)5107c478bd9Sstevel@tonic-gate pfc_dma_cleanup(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
5134ab75253Smrj 	int e;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	while ((ip = next_dma_resource(rp)) != NULL) {
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		FC_DEBUG2(9, CE_CONT, "pfc_dma_cleanup: virt %x len %x\n",
518b1a80202SZach Kissel 		    ip->fc_dma_virt, ip->fc_dma_len);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		/*
5217c478bd9Sstevel@tonic-gate 		 * Free the dma handle
5227c478bd9Sstevel@tonic-gate 		 */
5234ab75253Smrj 		e = fc_ddi_dma_unbind_handle(ip->fc_dma_handle);
5244ab75253Smrj 		if (e != DDI_SUCCESS) {
5257c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "pfc_dma_cleanup: "
5264ab75253Smrj 			    "ddi_dma_unbind failed!\n");
5274ab75253Smrj 		}
5284ab75253Smrj 		fc_ddi_dma_free_handle(&ip->fc_dma_handle);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * Tear down the userland mapping and free the buf header
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		fc_physio_free(&ip->fc_dma_bp, ip->fc_dma_virt, ip->fc_dma_len);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		fc_rem_resource(rp, ip);
5367c478bd9Sstevel@tonic-gate 		kmem_free(ip, sizeof (struct fc_resource));
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
5407c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate static int
pfc_map_in(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)5447c478bd9Sstevel@tonic-gate pfc_map_in(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	size_t len;
5477c478bd9Sstevel@tonic-gate 	int error;
5487c478bd9Sstevel@tonic-gate 	caddr_t virt;
5497c478bd9Sstevel@tonic-gate 	pci_regspec_t p, *ph;
5507c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
5517c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t acc;
5527c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t h;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 4)
5557c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 4"));
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
5587c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	p.pci_size_hi = 0;
5617c478bd9Sstevel@tonic-gate 	p.pci_size_low = len = fc_cell2size(fc_arg(cp, 0));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 1));
5647c478bd9Sstevel@tonic-gate 	p.pci_phys_mid = fc_cell2uint(fc_arg(cp, 2));
5657c478bd9Sstevel@tonic-gate 	p.pci_phys_low = fc_cell2uint(fc_arg(cp, 3));
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * Fcode is expecting the bytes are not swapped.
5717c478bd9Sstevel@tonic-gate 	 */
5727c478bd9Sstevel@tonic-gate 	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
5737c478bd9Sstevel@tonic-gate 	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * First We need to allocate the PCI Resource.
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	error = pci_alloc_resource(rp->child, p);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (error)  {
5817c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci map-in failed"));
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (error)  {
5877c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci map-in failed"));
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
5917c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = fc_ptr2cell(virt);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Log this resource ...
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	ip = kmem_zalloc(sizeof (struct fc_resource), KM_SLEEP);
5977c478bd9Sstevel@tonic-gate 	ip->type = RT_MAP;
5987c478bd9Sstevel@tonic-gate 	ip->fc_map_virt = virt;
5997c478bd9Sstevel@tonic-gate 	ip->fc_map_len = len;
6007c478bd9Sstevel@tonic-gate 	ip->fc_map_handle = h;
6017c478bd9Sstevel@tonic-gate 	ph = kmem_zalloc(sizeof (pci_regspec_t), KM_SLEEP);
6027c478bd9Sstevel@tonic-gate 	*ph = p;
6037c478bd9Sstevel@tonic-gate 	ip->fc_regspec = ph;	/* cache a copy of the reg spec */
6047c478bd9Sstevel@tonic-gate 	fc_add_resource(rp, ip);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate static int
pfc_map_out(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)6107c478bd9Sstevel@tonic-gate pfc_map_out(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	caddr_t virt;
6137c478bd9Sstevel@tonic-gate 	size_t len;
6147c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 2)
6177c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 2"));
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 1));
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	len = fc_cell2size(fc_arg(cp, 0));
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Find if this request matches a mapping resource we set up.
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
6277c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next) {
6287c478bd9Sstevel@tonic-gate 		if (ip->type != RT_MAP)
6297c478bd9Sstevel@tonic-gate 			continue;
6307c478bd9Sstevel@tonic-gate 		if (ip->fc_map_virt != virt)
6317c478bd9Sstevel@tonic-gate 			continue;
6327c478bd9Sstevel@tonic-gate 		if (ip->fc_map_len == len)
6337c478bd9Sstevel@tonic-gate 			break;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (ip == NULL)
6387c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "request doesn't match a "
6397c478bd9Sstevel@tonic-gate 		    "known mapping"));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	pci_unmap_phys(&ip->fc_map_handle, ip->fc_regspec);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	kmem_free(ip->fc_regspec, sizeof (pci_regspec_t));
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/*
6467c478bd9Sstevel@tonic-gate 	 * remove the resource from the list and release it.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	fc_rem_resource(rp, ip);
6497c478bd9Sstevel@tonic-gate 	kmem_free(ip, sizeof (struct fc_resource));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
6527c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate static int
pfc_register_fetch(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)6567c478bd9Sstevel@tonic-gate pfc_register_fetch(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	size_t len;
6597c478bd9Sstevel@tonic-gate 	caddr_t virt;
6607c478bd9Sstevel@tonic-gate 	int error;
6617c478bd9Sstevel@tonic-gate 	uint64_t x;
6627c478bd9Sstevel@tonic-gate 	uint32_t l;
6637c478bd9Sstevel@tonic-gate 	uint16_t w;
6647c478bd9Sstevel@tonic-gate 	uint8_t b;
6657c478bd9Sstevel@tonic-gate 	char *name = fc_cell2ptr(cp->svc_name);
6667c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 1)
6697c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 1"));
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
6727c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 0));
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Determine the access width .. we can switch on the 2nd
6787c478bd9Sstevel@tonic-gate 	 * character of the name which is "rx@", "rl@", "rb@" or "rw@"
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 	switch (*(name + 1)) {
6817c478bd9Sstevel@tonic-gate 	case 'x':	len = sizeof (x); break;
6827c478bd9Sstevel@tonic-gate 	case 'l':	len = sizeof (l); break;
6837c478bd9Sstevel@tonic-gate 	case 'w':	len = sizeof (w); break;
6847c478bd9Sstevel@tonic-gate 	case 'b':	len = sizeof (b); break;
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * Check the alignment ...
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	if (((intptr_t)virt & (len - 1)) != 0)
6917c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "unaligned access"));
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * Find if this virt is 'within' a request we know about
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
6977c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next) {
6987c478bd9Sstevel@tonic-gate 		if (ip->type != RT_MAP)
6997c478bd9Sstevel@tonic-gate 			continue;
7007c478bd9Sstevel@tonic-gate 		if ((virt >= (caddr_t)ip->fc_map_virt) && ((virt + len) <=
7017c478bd9Sstevel@tonic-gate 		    ((caddr_t)ip->fc_map_virt + ip->fc_map_len)))
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if (ip == NULL)
7077c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "request not within a "
7087c478bd9Sstevel@tonic-gate 		    "known mapping"));
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * XXX: We need access handle versions of peek/poke to move
7127c478bd9Sstevel@tonic-gate 	 * beyond the prototype ... we assume that we have hardware
7137c478bd9Sstevel@tonic-gate 	 * byte swapping enabled for pci register access here which
7147c478bd9Sstevel@tonic-gate 	 * is a huge dependency on the current implementation.
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	switch (len) {
7177c478bd9Sstevel@tonic-gate 	case sizeof (x):
7187c478bd9Sstevel@tonic-gate 		error = ddi_peek64(rp->child, (int64_t *)virt, (int64_t *)&x);
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 	case sizeof (l):
7217c478bd9Sstevel@tonic-gate 		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&l);
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 	case sizeof (w):
7247c478bd9Sstevel@tonic-gate 		error = ddi_peek16(rp->child, (int16_t *)virt, (int16_t *)&w);
7257c478bd9Sstevel@tonic-gate 		break;
7267c478bd9Sstevel@tonic-gate 	case sizeof (b):
7277c478bd9Sstevel@tonic-gate 		error = ddi_peek8(rp->child, (int8_t *)virt, (int8_t *)&b);
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (error) {
7327c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "access error"));
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
7367c478bd9Sstevel@tonic-gate 	switch (len) {
7377c478bd9Sstevel@tonic-gate 	case sizeof (x): fc_result(cp, 0) = x; break;
7387c478bd9Sstevel@tonic-gate 	case sizeof (l): fc_result(cp, 0) = fc_uint32_t2cell(l); break;
7397c478bd9Sstevel@tonic-gate 	case sizeof (w): fc_result(cp, 0) = fc_uint16_t2cell(w); break;
7407c478bd9Sstevel@tonic-gate 	case sizeof (b): fc_result(cp, 0) = fc_uint8_t2cell(b); break;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static int
pfc_register_store(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)7467c478bd9Sstevel@tonic-gate pfc_register_store(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	size_t len;
7497c478bd9Sstevel@tonic-gate 	caddr_t virt;
7507c478bd9Sstevel@tonic-gate 	int error;
7517c478bd9Sstevel@tonic-gate 	uint64_t x;
7527c478bd9Sstevel@tonic-gate 	uint32_t l;
7537c478bd9Sstevel@tonic-gate 	uint16_t w;
7547c478bd9Sstevel@tonic-gate 	uint8_t b;
7557c478bd9Sstevel@tonic-gate 	char *name = fc_cell2ptr(cp->svc_name);
7567c478bd9Sstevel@tonic-gate 	struct fc_resource *ip;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 2)
7597c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 2"));
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 0));
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * Determine the access width .. we can switch on the 2nd
7657c478bd9Sstevel@tonic-gate 	 * character of the name which is "rl!", "rb!" or "rw!"
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	switch (*(name + 1)) {
7687c478bd9Sstevel@tonic-gate 	case 'x': len = sizeof (x); x = fc_arg(cp, 1); break;
7697c478bd9Sstevel@tonic-gate 	case 'l': len = sizeof (l); l = fc_cell2uint32_t(fc_arg(cp, 1)); break;
7707c478bd9Sstevel@tonic-gate 	case 'w': len = sizeof (w); w = fc_cell2uint16_t(fc_arg(cp, 1)); break;
7717c478bd9Sstevel@tonic-gate 	case 'b': len = sizeof (b); b = fc_cell2uint8_t(fc_arg(cp, 1)); break;
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/*
7757c478bd9Sstevel@tonic-gate 	 * Check the alignment ...
7767c478bd9Sstevel@tonic-gate 	 */
7777c478bd9Sstevel@tonic-gate 	if (((intptr_t)virt & (len - 1)) != 0)
7787c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "unaligned access"));
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * Find if this virt is 'within' a request we know about
7827c478bd9Sstevel@tonic-gate 	 */
7837c478bd9Sstevel@tonic-gate 	fc_lock_resource_list(rp);
7847c478bd9Sstevel@tonic-gate 	for (ip = rp->head; ip != NULL; ip = ip->next) {
7857c478bd9Sstevel@tonic-gate 		if (ip->type != RT_MAP)
7867c478bd9Sstevel@tonic-gate 			continue;
7877c478bd9Sstevel@tonic-gate 		if ((virt >= (caddr_t)ip->fc_map_virt) && ((virt + len) <=
7887c478bd9Sstevel@tonic-gate 		    ((caddr_t)ip->fc_map_virt + ip->fc_map_len)))
7897c478bd9Sstevel@tonic-gate 			break;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	fc_unlock_resource_list(rp);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if (ip == NULL)
7947c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "request not within a "
7957c478bd9Sstevel@tonic-gate 		    "known mapping"));
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/*
7987c478bd9Sstevel@tonic-gate 	 * XXX: We need access handle versions of peek/poke to move
7997c478bd9Sstevel@tonic-gate 	 * beyond the prototype ... we assume that we have hardware
8007c478bd9Sstevel@tonic-gate 	 * byte swapping enabled for pci register access here which
8017c478bd9Sstevel@tonic-gate 	 * is a huge dependency on the current implementation.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	switch (len) {
8047c478bd9Sstevel@tonic-gate 	case sizeof (x):
8057c478bd9Sstevel@tonic-gate 		error = ddi_poke64(rp->child, (int64_t *)virt, x);
8067c478bd9Sstevel@tonic-gate 		break;
8077c478bd9Sstevel@tonic-gate 	case sizeof (l):
8087c478bd9Sstevel@tonic-gate 		error = ddi_poke32(rp->child, (int32_t *)virt, l);
8097c478bd9Sstevel@tonic-gate 		break;
8107c478bd9Sstevel@tonic-gate 	case sizeof (w):
8117c478bd9Sstevel@tonic-gate 		error = ddi_poke16(rp->child, (int16_t *)virt, w);
8127c478bd9Sstevel@tonic-gate 		break;
8137c478bd9Sstevel@tonic-gate 	case sizeof (b):
8147c478bd9Sstevel@tonic-gate 		error = ddi_poke8(rp->child, (int8_t *)virt, b);
8157c478bd9Sstevel@tonic-gate 		break;
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (error) {
8197c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "access error"));
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
8237c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate static int
pfc_config_fetch(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)8277c478bd9Sstevel@tonic-gate pfc_config_fetch(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate 	caddr_t virt, v;
8307c478bd9Sstevel@tonic-gate 	int error, reg, flags = 0;
8317c478bd9Sstevel@tonic-gate 	size_t len;
8327c478bd9Sstevel@tonic-gate 	uint32_t l, tmp;
8337c478bd9Sstevel@tonic-gate 	uint16_t w;
8347c478bd9Sstevel@tonic-gate 	uint8_t b;
8357c478bd9Sstevel@tonic-gate 	char *name = fc_cell2ptr(cp->svc_name);
8367c478bd9Sstevel@tonic-gate 	pci_regspec_t p;
8377c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t acc;
8387c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t h;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 1)
8417c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 1"));
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
8447c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * Construct a config address pci reg property from the args.
8487c478bd9Sstevel@tonic-gate 	 * arg[0] is the configuration address.
8497c478bd9Sstevel@tonic-gate 	 */
8507c478bd9Sstevel@tonic-gate 	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 0));
8517c478bd9Sstevel@tonic-gate 	p.pci_phys_mid = p.pci_phys_low = 0;
8527c478bd9Sstevel@tonic-gate 	p.pci_size_hi = p.pci_size_low = 0;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/*
8557c478bd9Sstevel@tonic-gate 	 * Verify that the address is a configuration space address
85634d3f749Saa 	 * ss must be zero.
8577c478bd9Sstevel@tonic-gate 	 */
85834d3f749Saa 	if ((p.pci_phys_hi & PCI_ADDR_MASK) != PCI_ADDR_CONFIG) {
8597c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pfc_config_fetch: "
8607c478bd9Sstevel@tonic-gate 		    "invalid config addr: %x\n", p.pci_phys_hi);
8617c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "non-config addr"));
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/*
8657c478bd9Sstevel@tonic-gate 	 * Extract the register number from the config address and
8667c478bd9Sstevel@tonic-gate 	 * remove the register number from the physical address.
8677c478bd9Sstevel@tonic-gate 	 */
86834d3f749Saa 
86934d3f749Saa 	reg = (p.pci_phys_hi & PCI_REG_REG_M) |
87034d3f749Saa 	    (((p.pci_phys_hi & PCI_REG_EXTREG_M) >> PCI_REG_EXTREG_SHIFT) << 8);
87134d3f749Saa 
87234d3f749Saa 	p.pci_phys_hi &= PCI_BDF_bits;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/*
8757c478bd9Sstevel@tonic-gate 	 * Determine the access width .. we can switch on the 9th
8767c478bd9Sstevel@tonic-gate 	 * character of the name which is "config-{l,w,b}@"
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	switch (*(name + 7)) {
8797c478bd9Sstevel@tonic-gate 	case 'l':	len = sizeof (l); break;
8807c478bd9Sstevel@tonic-gate 	case 'w':	len = sizeof (w); break;
8817c478bd9Sstevel@tonic-gate 	case 'b':	len = sizeof (b); break;
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * Verify that the access is properly aligned
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	if ((reg & (len - 1)) != 0)
8887c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "unaligned access"));
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Map in configuration space (temporarily)
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
8947c478bd9Sstevel@tonic-gate 	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
8957c478bd9Sstevel@tonic-gate 	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (error)  {
9007c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci config map-in failed"));
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (fcpci_indirect_map(rp->child) == DDI_SUCCESS)
9047c478bd9Sstevel@tonic-gate 		flags |= PCICFG_CONF_INDIRECT_MAP;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (flags & PCICFG_CONF_INDIRECT_MAP) {
9077c478bd9Sstevel@tonic-gate 		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
9087c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
9097c478bd9Sstevel@tonic-gate 	} else
9107c478bd9Sstevel@tonic-gate 		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&tmp);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
9137c478bd9Sstevel@tonic-gate 		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
9147c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
9157c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "fcpcii: conf probe failed.l=%x", tmp);
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
9197c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci config fetch failed"));
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * XXX: We need access handle versions of peek/poke to move
9257c478bd9Sstevel@tonic-gate 	 * beyond the prototype ... we assume that we have hardware
9267c478bd9Sstevel@tonic-gate 	 * byte swapping enabled for pci register access here which
9277c478bd9Sstevel@tonic-gate 	 * is a huge dependency on the current implementation.
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	v = virt + reg;
9307c478bd9Sstevel@tonic-gate 	switch (len) {
9317c478bd9Sstevel@tonic-gate 	case sizeof (l):
9327c478bd9Sstevel@tonic-gate 		l = (int32_t)ddi_get32(h, (uint32_t *)v);
9337c478bd9Sstevel@tonic-gate 		break;
9347c478bd9Sstevel@tonic-gate 	case sizeof (w):
9357c478bd9Sstevel@tonic-gate 		w = (int16_t)ddi_get16(h, (uint16_t *)v);
9367c478bd9Sstevel@tonic-gate 		break;
9377c478bd9Sstevel@tonic-gate 	case sizeof (b):
9387c478bd9Sstevel@tonic-gate 		b = (int8_t)ddi_get8(h, (uint8_t *)v);
9397c478bd9Sstevel@tonic-gate 		break;
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	 * Remove the temporary config space mapping
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	pci_unmap_phys(&h, &p);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (error) {
9487c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "access error"));
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
9527c478bd9Sstevel@tonic-gate 	switch (len) {
9537c478bd9Sstevel@tonic-gate 	case sizeof (l): fc_result(cp, 0) = fc_uint32_t2cell(l); break;
9547c478bd9Sstevel@tonic-gate 	case sizeof (w): fc_result(cp, 0) = fc_uint16_t2cell(w); break;
9557c478bd9Sstevel@tonic-gate 	case sizeof (b): fc_result(cp, 0) = fc_uint8_t2cell(b); break;
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate static int
pfc_config_store(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)9627c478bd9Sstevel@tonic-gate pfc_config_store(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	caddr_t virt, v;
9657c478bd9Sstevel@tonic-gate 	int error, reg, flags = 0;
9667c478bd9Sstevel@tonic-gate 	size_t len;
9677c478bd9Sstevel@tonic-gate 	uint32_t l, tmp;
9687c478bd9Sstevel@tonic-gate 	uint16_t w;
9697c478bd9Sstevel@tonic-gate 	uint8_t b;
9707c478bd9Sstevel@tonic-gate 	char *name = fc_cell2ptr(cp->svc_name);
9717c478bd9Sstevel@tonic-gate 	pci_regspec_t p;
9727c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t acc;
9737c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t h;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 2)
9767c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 2"));
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Construct a config address pci reg property from the args.
9807c478bd9Sstevel@tonic-gate 	 * arg[0] is the configuration address. arg[1] is the data.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	p.pci_phys_hi = fc_cell2uint(fc_arg(cp, 0));
9837c478bd9Sstevel@tonic-gate 	p.pci_phys_mid = p.pci_phys_low = 0;
9847c478bd9Sstevel@tonic-gate 	p.pci_size_hi = p.pci_size_low = 0;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Verify that the address is a configuration space address
98834d3f749Saa 	 * ss must be zero.
9897c478bd9Sstevel@tonic-gate 	 */
99034d3f749Saa 	if ((p.pci_phys_hi & PCI_ADDR_MASK) != PCI_ADDR_CONFIG) {
9917c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "pfc_config_store: "
9927c478bd9Sstevel@tonic-gate 		    "invalid config addr: %x\n", p.pci_phys_hi);
9937c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "non-config addr"));
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * Extract the register number from the config address and
9987c478bd9Sstevel@tonic-gate 	 * remove the register number from the physical address.
9997c478bd9Sstevel@tonic-gate 	 */
100034d3f749Saa 	reg = (p.pci_phys_hi & PCI_REG_REG_M) |
100134d3f749Saa 	    (((p.pci_phys_hi & PCI_REG_EXTREG_M) >> PCI_REG_EXTREG_SHIFT) << 8);
100234d3f749Saa 
100334d3f749Saa 	p.pci_phys_hi &= PCI_BDF_bits;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Determine the access width .. we can switch on the 8th
10077c478bd9Sstevel@tonic-gate 	 * character of the name which is "config-{l,w,b}@"
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	switch (*(name + 7)) {
10107c478bd9Sstevel@tonic-gate 	case 'l': len = sizeof (l); l = fc_cell2uint32_t(fc_arg(cp, 1)); break;
10117c478bd9Sstevel@tonic-gate 	case 'w': len = sizeof (w); w = fc_cell2uint16_t(fc_arg(cp, 1)); break;
10127c478bd9Sstevel@tonic-gate 	case 'b': len = sizeof (b); b = fc_cell2uint8_t(fc_arg(cp, 1)); break;
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * Verify that the access is properly aligned
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	if ((reg & (len - 1)) != 0)
10197c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "unaligned access"));
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * Map in configuration space (temporarily)
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
10257c478bd9Sstevel@tonic-gate 	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
10267c478bd9Sstevel@tonic-gate 	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	error = pci_map_phys(rp->child, &p, &virt, &acc, &h);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (error)  {
10317c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci config map-in failed"));
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (fcpci_indirect_map(rp->child) == DDI_SUCCESS)
10357c478bd9Sstevel@tonic-gate 		flags |= PCICFG_CONF_INDIRECT_MAP;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (flags & PCICFG_CONF_INDIRECT_MAP) {
10387c478bd9Sstevel@tonic-gate 		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
10397c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
10407c478bd9Sstevel@tonic-gate 	} else
10417c478bd9Sstevel@tonic-gate 		error = ddi_peek32(rp->child, (int32_t *)virt, (int32_t *)&tmp);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
10447c478bd9Sstevel@tonic-gate 		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
10457c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
10467c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "fcpci: conf probe failed.l=%x", tmp);
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
10507c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "pci config store failed"));
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	/*
10557c478bd9Sstevel@tonic-gate 	 * XXX: We need access handle versions of peek/poke to move
10567c478bd9Sstevel@tonic-gate 	 * beyond the prototype ... we assume that we have hardware
10577c478bd9Sstevel@tonic-gate 	 * byte swapping enabled for pci register access here which
10587c478bd9Sstevel@tonic-gate 	 * is a huge dependency on the current implementation.
10597c478bd9Sstevel@tonic-gate 	 */
10607c478bd9Sstevel@tonic-gate 	v = virt + reg;
10617c478bd9Sstevel@tonic-gate 	switch (len) {
10627c478bd9Sstevel@tonic-gate 	case sizeof (l):
10637c478bd9Sstevel@tonic-gate 		ddi_put32(h, (uint32_t *)v, (uint32_t)l);
10647c478bd9Sstevel@tonic-gate 		break;
10657c478bd9Sstevel@tonic-gate 	case sizeof (w):
10667c478bd9Sstevel@tonic-gate 		ddi_put16(h, (uint16_t *)v, (uint16_t)w);
10677c478bd9Sstevel@tonic-gate 		break;
10687c478bd9Sstevel@tonic-gate 	case sizeof (b):
10697c478bd9Sstevel@tonic-gate 		ddi_put8(h, (uint8_t *)v, (uint8_t)b);
10707c478bd9Sstevel@tonic-gate 		break;
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/*
10747c478bd9Sstevel@tonic-gate 	 * Remove the temporary config space mapping
10757c478bd9Sstevel@tonic-gate 	 */
10767c478bd9Sstevel@tonic-gate 	pci_unmap_phys(&h, &p);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if (error) {
10797c478bd9Sstevel@tonic-gate 		return (fc_priv_error(cp, "access error"));
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(0);
10837c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate static int
pfc_get_fcode(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)10887c478bd9Sstevel@tonic-gate pfc_get_fcode(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	caddr_t name_virt, fcode_virt;
10917c478bd9Sstevel@tonic-gate 	char *name, *fcode;
10927c478bd9Sstevel@tonic-gate 	int fcode_len, status;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 3)
10957c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 3"));
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
10987c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	name_virt = fc_cell2ptr(fc_arg(cp, 0));
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	fcode_virt = fc_cell2ptr(fc_arg(cp, 1));
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	fcode_len = fc_cell2int(fc_arg(cp, 2));
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (copyinstr(fc_cell2ptr(name_virt), name,
11097c478bd9Sstevel@tonic-gate 	    FC_SVC_NAME_LEN - 1, NULL))  {
11107c478bd9Sstevel@tonic-gate 		status = 0;
11117c478bd9Sstevel@tonic-gate 	} else {
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		fcode = kmem_zalloc(fcode_len, KM_SLEEP);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		if ((status = prom_get_fcode(name, fcode)) != 0) {
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			if (copyout((void *)fcode, (void *)fcode_virt,
11187c478bd9Sstevel@tonic-gate 			    fcode_len)) {
11197c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, " pfc_get_fcode: Unable "
11207c478bd9Sstevel@tonic-gate 				    "to copy out fcode image\n");
11217c478bd9Sstevel@tonic-gate 				status = 0;
11227c478bd9Sstevel@tonic-gate 			}
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		kmem_free(fcode, fcode_len);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	kmem_free(name, FC_SVC_NAME_LEN);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
11317c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = status;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate static int
pfc_get_fcode_size(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)11377c478bd9Sstevel@tonic-gate pfc_get_fcode_size(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	caddr_t virt;
11407c478bd9Sstevel@tonic-gate 	char *name;
11417c478bd9Sstevel@tonic-gate 	int len;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 1)
11447c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 1"));
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
11477c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	virt = fc_cell2ptr(fc_arg(cp, 0));
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (copyinstr(fc_cell2ptr(virt), name,
11547c478bd9Sstevel@tonic-gate 	    FC_SVC_NAME_LEN - 1, NULL))  {
11557c478bd9Sstevel@tonic-gate 		len = 0;
11567c478bd9Sstevel@tonic-gate 	} else {
11577c478bd9Sstevel@tonic-gate 		len = prom_get_fcode_size(name);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	kmem_free(name, FC_SVC_NAME_LEN);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
11637c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = len;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /*
11697c478bd9Sstevel@tonic-gate  * Return the physical probe address: lo=0, mid=0, hi-config-addr
11707c478bd9Sstevel@tonic-gate  */
11717c478bd9Sstevel@tonic-gate static int
pfc_probe_address(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)11727c478bd9Sstevel@tonic-gate pfc_probe_address(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 0)
11757c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 0"));
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 2)
11787c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 3"));
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(2);
11817c478bd9Sstevel@tonic-gate 	fc_result(cp, 1) = fc_int2cell(0);	/* phys.lo */
11827c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = fc_int2cell(0);	/* phys.mid */
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * Return the phys.hi component of the probe address.
11897c478bd9Sstevel@tonic-gate  */
11907c478bd9Sstevel@tonic-gate static int
pfc_probe_space(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)11917c478bd9Sstevel@tonic-gate pfc_probe_space(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	struct pci_ops_bus_args *ba = rp->bus_args;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	ASSERT(ba);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 0)
11987c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 0"));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
12017c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
12047c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = fc_uint32_t2cell(ba->config_address); /* phys.hi */
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate static int
pfc_config_child(dev_info_t * ap,fco_handle_t rp,fc_ci_t * cp)12107c478bd9Sstevel@tonic-gate pfc_config_child(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	fc_phandle_t h;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nargs) != 0)
12157c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nargs must be 0"));
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (fc_cell2int(cp->nresults) < 1)
12187c478bd9Sstevel@tonic-gate 		return (fc_syntax_error(cp, "nresults must be >= 1"));
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), rp->child);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	cp->nresults = fc_int2cell(1);
12237c478bd9Sstevel@tonic-gate 	fc_result(cp, 0) = fc_phandle2cell(h);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	return (fc_success_op(ap, rp, cp));
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate int
pci_alloc_mem_chunk(dev_info_t * dip,uint64_t mem_align,uint64_t * mem_size,uint64_t * mem_answer)12297c478bd9Sstevel@tonic-gate pci_alloc_mem_chunk(dev_info_t *dip, uint64_t mem_align, uint64_t *mem_size,
12307c478bd9Sstevel@tonic-gate     uint64_t *mem_answer)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
12337c478bd9Sstevel@tonic-gate 	int rval;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
12367c478bd9Sstevel@tonic-gate 	req.ra_flags = NDI_RA_ALLOC_BOUNDED;
12377c478bd9Sstevel@tonic-gate 	req.ra_boundbase = 0;
12387c478bd9Sstevel@tonic-gate 	req.ra_boundlen = PCI_4GIG_LIMIT;
12397c478bd9Sstevel@tonic-gate 	req.ra_len = *mem_size;
12407c478bd9Sstevel@tonic-gate 	req.ra_align_mask = mem_align - 1;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	rval = ndi_ra_alloc(dip, &req, mem_answer, mem_size,
12437c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_MEM, NDI_RA_PASS);
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	return (rval);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate int
pci_alloc_io_chunk(dev_info_t * dip,uint64_t io_align,uint64_t * io_size,uint64_t * io_answer)12487c478bd9Sstevel@tonic-gate pci_alloc_io_chunk(dev_info_t *dip, uint64_t io_align, uint64_t *io_size,
12497c478bd9Sstevel@tonic-gate     uint64_t *io_answer)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
12527c478bd9Sstevel@tonic-gate 	int rval;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&req, sizeof (ndi_ra_request_t));
12557c478bd9Sstevel@tonic-gate 	req.ra_flags = (NDI_RA_ALLOC_BOUNDED | NDI_RA_ALLOC_PARTIAL_OK);
12567c478bd9Sstevel@tonic-gate 	req.ra_boundbase = 0;
12577c478bd9Sstevel@tonic-gate 	req.ra_boundlen = PCI_4GIG_LIMIT;
12587c478bd9Sstevel@tonic-gate 	req.ra_len = *io_size;
12597c478bd9Sstevel@tonic-gate 	req.ra_align_mask = io_align - 1;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	rval = ndi_ra_alloc(dip, &req, io_answer, io_size,
12627c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_IO, NDI_RA_PASS);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	return (rval);
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate int
pci_alloc_resource(dev_info_t * dip,pci_regspec_t phys_spec)12687c478bd9Sstevel@tonic-gate pci_alloc_resource(dev_info_t *dip, pci_regspec_t phys_spec)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	uint64_t answer;
12717c478bd9Sstevel@tonic-gate 	uint64_t alen;
12727c478bd9Sstevel@tonic-gate 	int offset, tmp;
12737c478bd9Sstevel@tonic-gate 	pci_regspec_t config;
12747c478bd9Sstevel@tonic-gate 	caddr_t virt, v;
12757c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t acc;
12767c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t h;
12777c478bd9Sstevel@tonic-gate 	ndi_ra_request_t request;
12787c478bd9Sstevel@tonic-gate 	pci_regspec_t *assigned;
12797c478bd9Sstevel@tonic-gate 	int assigned_len, entries, i, l, flags = 0, error;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	l = phys_spec.pci_size_low;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
12847c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "assigned-addresses", (caddr_t)&assigned,
12857c478bd9Sstevel@tonic-gate 	    &assigned_len) == DDI_PROP_SUCCESS) {
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		entries = assigned_len / (sizeof (pci_regspec_t));
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		/*
12907c478bd9Sstevel@tonic-gate 		 * Walk through the assigned-addresses entries. If there is
12917c478bd9Sstevel@tonic-gate 		 * a match, there is no need to allocate the resource.
12927c478bd9Sstevel@tonic-gate 		 */
12937c478bd9Sstevel@tonic-gate 		for (i = 0; i < entries; i++) {
12947c478bd9Sstevel@tonic-gate 			if (assigned[i].pci_phys_hi == phys_spec.pci_phys_hi) {
12957c478bd9Sstevel@tonic-gate 				if (assigned[i].pci_size_low >=
12967c478bd9Sstevel@tonic-gate 				    phys_spec.pci_size_low) {
12977c478bd9Sstevel@tonic-gate 					kmem_free(assigned, assigned_len);
12987c478bd9Sstevel@tonic-gate 					return (0);
12997c478bd9Sstevel@tonic-gate 				}
13007c478bd9Sstevel@tonic-gate 				/*
13017c478bd9Sstevel@tonic-gate 				 * Fcode wants to assign more than what
13027c478bd9Sstevel@tonic-gate 				 * probe found.
13037c478bd9Sstevel@tonic-gate 				 */
13047c478bd9Sstevel@tonic-gate 				(void) pci_free_resource(dip, assigned[i]);
13057c478bd9Sstevel@tonic-gate 				/*
13067c478bd9Sstevel@tonic-gate 				 * Go on to allocate resources.
13077c478bd9Sstevel@tonic-gate 				 */
13087c478bd9Sstevel@tonic-gate 				break;
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			/*
13117c478bd9Sstevel@tonic-gate 			 * Check if Fcode wants to map using different
13127c478bd9Sstevel@tonic-gate 			 * NPT bits.
13137c478bd9Sstevel@tonic-gate 			 */
13147c478bd9Sstevel@tonic-gate 			if (PCI_REG_BDFR_G(assigned[i].pci_phys_hi) ==
13157c478bd9Sstevel@tonic-gate 			    PCI_REG_BDFR_G(phys_spec.pci_phys_hi)) {
13167c478bd9Sstevel@tonic-gate 				/*
13177c478bd9Sstevel@tonic-gate 				 * It is an error to change SS bits
13187c478bd9Sstevel@tonic-gate 				 */
13197c478bd9Sstevel@tonic-gate 				if (PCI_REG_ADDR_G(assigned[i].pci_phys_hi) !=
13207c478bd9Sstevel@tonic-gate 				    PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {
13217c478bd9Sstevel@tonic-gate 
1322ba640a72Sjj 					FC_DEBUG2(2, CE_WARN, "Fcode changing "
1323ba640a72Sjj 					    "ss bits in reg %x -- %x",
13247c478bd9Sstevel@tonic-gate 					    assigned[i].pci_phys_hi,
13257c478bd9Sstevel@tonic-gate 					    phys_spec.pci_phys_hi);
13267c478bd9Sstevel@tonic-gate 				}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 				/*
13297c478bd9Sstevel@tonic-gate 				 * Allocate enough
13307c478bd9Sstevel@tonic-gate 				 */
13317c478bd9Sstevel@tonic-gate 				l = MAX(assigned[i].pci_size_low,
13327c478bd9Sstevel@tonic-gate 				    phys_spec.pci_size_low);
13337c478bd9Sstevel@tonic-gate 
13343f9ec228SZhi-Jun Robin Fu 				phys_spec.pci_size_low = l;
13353f9ec228SZhi-Jun Robin Fu 
13367c478bd9Sstevel@tonic-gate 				(void) pci_free_resource(dip, assigned[i]);
13377c478bd9Sstevel@tonic-gate 				/*
13387c478bd9Sstevel@tonic-gate 				 * Go on to allocate resources.
13397c478bd9Sstevel@tonic-gate 				 */
13407c478bd9Sstevel@tonic-gate 				break;
13417c478bd9Sstevel@tonic-gate 			}
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 		kmem_free(assigned, assigned_len);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	config.pci_phys_hi = PCI_CONF_ADDR_MASK & phys_spec.pci_phys_hi;
13497c478bd9Sstevel@tonic-gate 	config.pci_phys_hi &= ~PCI_REG_REG_M;
13507c478bd9Sstevel@tonic-gate 	config.pci_phys_mid = config.pci_phys_low = 0;
13517c478bd9Sstevel@tonic-gate 	config.pci_size_hi = config.pci_size_low = 0;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	/*
13547c478bd9Sstevel@tonic-gate 	 * Map in configuration space (temporarily)
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
13577c478bd9Sstevel@tonic-gate 	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
13587c478bd9Sstevel@tonic-gate 	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if (error = pci_map_phys(dip, &config, &virt, &acc, &h)) {
13617c478bd9Sstevel@tonic-gate 		return (1);
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	if (fcpci_indirect_map(dip) == DDI_SUCCESS)
13657c478bd9Sstevel@tonic-gate 		flags |= PCICFG_CONF_INDIRECT_MAP;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (flags & PCICFG_CONF_INDIRECT_MAP) {
13687c478bd9Sstevel@tonic-gate 		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
13697c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
13707c478bd9Sstevel@tonic-gate 	} else
13717c478bd9Sstevel@tonic-gate 		error = ddi_peek32(dip, (int32_t *)virt, (int32_t *)&tmp);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
13747c478bd9Sstevel@tonic-gate 		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
13757c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
13797c478bd9Sstevel@tonic-gate 		return (1);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	request.ra_flags |= NDI_RA_ALIGN_SIZE;
13837c478bd9Sstevel@tonic-gate 	request.ra_boundbase = 0;
13847c478bd9Sstevel@tonic-gate 	request.ra_boundlen = PCI_4GIG_LIMIT;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	offset = PCI_REG_REG_G(phys_spec.pci_phys_hi);
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	v = virt + offset;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	if (PCI_REG_REG_G(phys_spec.pci_phys_hi) == PCI_CONF_ROM) {
13917c478bd9Sstevel@tonic-gate 		request.ra_len = l;
13927c478bd9Sstevel@tonic-gate 		request.ra_flags ^= NDI_RA_ALLOC_BOUNDED;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 		/* allocate memory space from the allocator */
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		if (ndi_ra_alloc(ddi_get_parent(dip),
1397b1a80202SZach Kissel 		    &request, &answer, &alen, NDI_RA_TYPE_MEM,
1398b1a80202SZach Kissel 		    NDI_RA_PASS) != NDI_SUCCESS) {
13997c478bd9Sstevel@tonic-gate 			pci_unmap_phys(&h, &config);
14007c478bd9Sstevel@tonic-gate 			return (1);
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 		FC_DEBUG3(1, CE_CONT, "ROM addr = [0x%x.%x] len [0x%x]\n",
1403b1a80202SZach Kissel 		    HIADDR(answer), LOADDR(answer), alen);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		/* program the low word */
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		ddi_put32(h, (uint32_t *)v, LOADDR(answer));
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		phys_spec.pci_phys_low = LOADDR(answer);
14107c478bd9Sstevel@tonic-gate 		phys_spec.pci_phys_mid = HIADDR(answer);
14117c478bd9Sstevel@tonic-gate 	} else {
14127c478bd9Sstevel@tonic-gate 		request.ra_len = l;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		switch (PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {
14157c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
14167c478bd9Sstevel@tonic-gate 			request.ra_flags ^= NDI_RA_ALLOC_BOUNDED;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
14197c478bd9Sstevel@tonic-gate 				/*
14207c478bd9Sstevel@tonic-gate 				 * If it is a non relocatable address,
14217c478bd9Sstevel@tonic-gate 				 * then specify the address we want.
14227c478bd9Sstevel@tonic-gate 				 */
14237c478bd9Sstevel@tonic-gate 				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
14247c478bd9Sstevel@tonic-gate 				request.ra_addr = (uint64_t)LADDR(
14257c478bd9Sstevel@tonic-gate 				    phys_spec.pci_phys_low,
14267c478bd9Sstevel@tonic-gate 				    phys_spec.pci_phys_mid);
14277c478bd9Sstevel@tonic-gate 			}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 			/* allocate memory space from the allocator */
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 			if (ndi_ra_alloc(ddi_get_parent(dip),
1432b1a80202SZach Kissel 			    &request, &answer, &alen, NDI_RA_TYPE_MEM,
1433b1a80202SZach Kissel 			    NDI_RA_PASS) != NDI_SUCCESS) {
14347c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
1435b1a80202SZach Kissel 				if (request.ra_flags == NDI_RA_ALLOC_SPECIFIED)
14367c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Unable to allocate "
14377c478bd9Sstevel@tonic-gate 					    "non relocatable address 0x%p\n",
14387c478bd9Sstevel@tonic-gate 					    (void *) request.ra_addr);
14397c478bd9Sstevel@tonic-gate 				return (1);
14407c478bd9Sstevel@tonic-gate 			}
14417c478bd9Sstevel@tonic-gate 			FC_DEBUG3(1, CE_CONT,
14427c478bd9Sstevel@tonic-gate 			    "64 addr = [0x%x.%x] len [0x%x]\n",
14437c478bd9Sstevel@tonic-gate 			    HIADDR(answer),
14447c478bd9Sstevel@tonic-gate 			    LOADDR(answer),
14457c478bd9Sstevel@tonic-gate 			    alen);
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 			/* program the low word */
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 			ddi_put32(h, (uint32_t *)v, LOADDR(answer));
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 			/* program the high word with value zero */
14527c478bd9Sstevel@tonic-gate 			v += 4;
14537c478bd9Sstevel@tonic-gate 			ddi_put32(h, (uint32_t *)v, HIADDR(answer));
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 			phys_spec.pci_phys_low = LOADDR(answer);
14567c478bd9Sstevel@tonic-gate 			phys_spec.pci_phys_mid = HIADDR(answer);
1457ba640a72Sjj 			/*
1458ba640a72Sjj 			 * currently support 32b address space
1459ba640a72Sjj 			 * assignments only.
1460ba640a72Sjj 			 */
1461ba640a72Sjj 			phys_spec.pci_phys_hi ^= PCI_ADDR_MEM64 ^
1462b1a80202SZach Kissel 			    PCI_ADDR_MEM32;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 			break;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
14677c478bd9Sstevel@tonic-gate 			request.ra_flags |= NDI_RA_ALLOC_BOUNDED;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
14707c478bd9Sstevel@tonic-gate 				/*
14717c478bd9Sstevel@tonic-gate 				 * If it is a non relocatable address,
14727c478bd9Sstevel@tonic-gate 				 * then specify the address we want.
14737c478bd9Sstevel@tonic-gate 				 */
14747c478bd9Sstevel@tonic-gate 				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
14757c478bd9Sstevel@tonic-gate 				request.ra_addr = (uint64_t)
14767c478bd9Sstevel@tonic-gate 				    phys_spec.pci_phys_low;
14777c478bd9Sstevel@tonic-gate 			}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 			/* allocate memory space from the allocator */
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 			if (ndi_ra_alloc(ddi_get_parent(dip),
1482b1a80202SZach Kissel 			    &request, &answer, &alen, NDI_RA_TYPE_MEM,
1483b1a80202SZach Kissel 			    NDI_RA_PASS) != NDI_SUCCESS) {
14847c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
1485b1a80202SZach Kissel 				if (request.ra_flags == NDI_RA_ALLOC_SPECIFIED)
14867c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Unable to allocate "
14877c478bd9Sstevel@tonic-gate 					    "non relocatable address 0x%p\n",
14887c478bd9Sstevel@tonic-gate 					    (void *) request.ra_addr);
14897c478bd9Sstevel@tonic-gate 				return (1);
14907c478bd9Sstevel@tonic-gate 			}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 			FC_DEBUG3(1, CE_CONT,
14937c478bd9Sstevel@tonic-gate 			    "32 addr = [0x%x.%x] len [0x%x]\n",
14947c478bd9Sstevel@tonic-gate 			    HIADDR(answer),
14957c478bd9Sstevel@tonic-gate 			    LOADDR(answer),
14967c478bd9Sstevel@tonic-gate 			    alen);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 			/* program the low word */
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 			ddi_put32(h, (uint32_t *)v, LOADDR(answer));
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 			phys_spec.pci_phys_low = LOADDR(answer);
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 			break;
15057c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
15067c478bd9Sstevel@tonic-gate 			request.ra_flags |= NDI_RA_ALLOC_BOUNDED;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 			if (phys_spec.pci_phys_hi & PCI_REG_REL_M) {
15097c478bd9Sstevel@tonic-gate 				/*
15107c478bd9Sstevel@tonic-gate 				 * If it is a non relocatable address,
15117c478bd9Sstevel@tonic-gate 				 * then specify the address we want.
15127c478bd9Sstevel@tonic-gate 				 */
15137c478bd9Sstevel@tonic-gate 				request.ra_flags = NDI_RA_ALLOC_SPECIFIED;
15147c478bd9Sstevel@tonic-gate 				request.ra_addr = (uint64_t)
15157c478bd9Sstevel@tonic-gate 				    phys_spec.pci_phys_low;
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 			/* allocate I/O space from the allocator */
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 			if (ndi_ra_alloc(ddi_get_parent(dip),
1521b1a80202SZach Kissel 			    &request, &answer, &alen, NDI_RA_TYPE_IO,
1522b1a80202SZach Kissel 			    NDI_RA_PASS) != NDI_SUCCESS) {
15237c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
15247c478bd9Sstevel@tonic-gate 				if (request.ra_flags ==
15257c478bd9Sstevel@tonic-gate 				    NDI_RA_ALLOC_SPECIFIED)
15267c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "Unable to allocate "
15277c478bd9Sstevel@tonic-gate 					    "non relocatable IO Space 0x%p\n",
15287c478bd9Sstevel@tonic-gate 					    (void *) request.ra_addr);
15297c478bd9Sstevel@tonic-gate 				return (1);
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 			FC_DEBUG3(1, CE_CONT,
15327c478bd9Sstevel@tonic-gate 			    "I/O addr = [0x%x.%x] len [0x%x]\n",
15337c478bd9Sstevel@tonic-gate 			    HIADDR(answer),
15347c478bd9Sstevel@tonic-gate 			    LOADDR(answer),
15357c478bd9Sstevel@tonic-gate 			    alen);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 			ddi_put32(h, (uint32_t *)v, LOADDR(answer));
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 			phys_spec.pci_phys_low = LOADDR(answer);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 			break;
15427c478bd9Sstevel@tonic-gate 		default:
15437c478bd9Sstevel@tonic-gate 			pci_unmap_phys(&h, &config);
15447c478bd9Sstevel@tonic-gate 			return (1);
15457c478bd9Sstevel@tonic-gate 		} /* switch */
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * Now that memory locations are assigned,
15507c478bd9Sstevel@tonic-gate 	 * update the assigned address property.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	if (pfc_update_assigned_prop(dip, &phys_spec)) {
15537c478bd9Sstevel@tonic-gate 		pci_unmap_phys(&h, &config);
15547c478bd9Sstevel@tonic-gate 		return (1);
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	pci_unmap_phys(&h, &config);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	return (0);
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate int
pci_free_resource(dev_info_t * dip,pci_regspec_t phys_spec)15637c478bd9Sstevel@tonic-gate pci_free_resource(dev_info_t *dip, pci_regspec_t phys_spec)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	int offset, tmp;
15667c478bd9Sstevel@tonic-gate 	pci_regspec_t config;
15677c478bd9Sstevel@tonic-gate 	caddr_t virt, v;
15687c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t acc;
15697c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t h;
15707c478bd9Sstevel@tonic-gate 	ndi_ra_request_t request;
15717c478bd9Sstevel@tonic-gate 	int l, error, flags = 0;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&request, sizeof (ndi_ra_request_t));
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	config.pci_phys_hi = PCI_CONF_ADDR_MASK & phys_spec.pci_phys_hi;
15767c478bd9Sstevel@tonic-gate 	config.pci_phys_hi &= ~PCI_REG_REG_M;
15777c478bd9Sstevel@tonic-gate 	config.pci_phys_mid = config.pci_phys_low = 0;
15787c478bd9Sstevel@tonic-gate 	config.pci_size_hi = config.pci_size_low = 0;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	/*
15817c478bd9Sstevel@tonic-gate 	 * Map in configuration space (temporarily)
15827c478bd9Sstevel@tonic-gate 	 */
15837c478bd9Sstevel@tonic-gate 	acc.devacc_attr_version = DDI_DEVICE_ATTR_V0;
15847c478bd9Sstevel@tonic-gate 	acc.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
15857c478bd9Sstevel@tonic-gate 	acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	if (error = pci_map_phys(dip, &config, &virt, &acc, &h)) {
15887c478bd9Sstevel@tonic-gate 		return (1);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 	if (fcpci_indirect_map(dip) == DDI_SUCCESS)
15917c478bd9Sstevel@tonic-gate 		flags |= PCICFG_CONF_INDIRECT_MAP;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (flags & PCICFG_CONF_INDIRECT_MAP) {
15947c478bd9Sstevel@tonic-gate 		tmp = (int32_t)ddi_get32(h, (uint32_t *)virt);
15957c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
15967c478bd9Sstevel@tonic-gate 	} else
15977c478bd9Sstevel@tonic-gate 		error = ddi_peek32(dip, (int32_t *)virt, (int32_t *)&tmp);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	if (error == DDI_SUCCESS)
16007c478bd9Sstevel@tonic-gate 		if ((tmp == (int32_t)0xffffffff) || (tmp == -1)) {
16017c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
16047c478bd9Sstevel@tonic-gate 		return (1);
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	offset = PCI_REG_REG_G(phys_spec.pci_phys_hi);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	v = virt + offset;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	/*
16137c478bd9Sstevel@tonic-gate 	 * Pick up the size to be freed. It may be different from
16147c478bd9Sstevel@tonic-gate 	 * what probe finds.
16157c478bd9Sstevel@tonic-gate 	 */
16167c478bd9Sstevel@tonic-gate 	l = phys_spec.pci_size_low;
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	if (PCI_REG_REG_G(phys_spec.pci_phys_hi) == PCI_CONF_ROM) {
16197c478bd9Sstevel@tonic-gate 		/* free memory back to the allocator */
16207c478bd9Sstevel@tonic-gate 		if (ndi_ra_free(ddi_get_parent(dip), phys_spec.pci_phys_low,
16217c478bd9Sstevel@tonic-gate 		    l, NDI_RA_TYPE_MEM,
16227c478bd9Sstevel@tonic-gate 		    NDI_RA_PASS) != NDI_SUCCESS) {
16237c478bd9Sstevel@tonic-gate 			pci_unmap_phys(&h, &config);
16247c478bd9Sstevel@tonic-gate 			return (1);
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		/* Unmap the BAR by writing a zero */
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		ddi_put32(h, (uint32_t *)v, 0);
16307c478bd9Sstevel@tonic-gate 	} else {
16317c478bd9Sstevel@tonic-gate 		switch (PCI_REG_ADDR_G(phys_spec.pci_phys_hi)) {
16327c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
16337c478bd9Sstevel@tonic-gate 			/* free memory back to the allocator */
16347c478bd9Sstevel@tonic-gate 			if (ndi_ra_free(ddi_get_parent(dip),
16357c478bd9Sstevel@tonic-gate 			    LADDR(phys_spec.pci_phys_low,
16367c478bd9Sstevel@tonic-gate 			    phys_spec.pci_phys_mid),
16377c478bd9Sstevel@tonic-gate 			    l, NDI_RA_TYPE_MEM,
16387c478bd9Sstevel@tonic-gate 			    NDI_RA_PASS) != NDI_SUCCESS) {
16397c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
16407c478bd9Sstevel@tonic-gate 				return (1);
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 			break;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
16467c478bd9Sstevel@tonic-gate 			/* free memory back to the allocator */
16477c478bd9Sstevel@tonic-gate 			if (ndi_ra_free(ddi_get_parent(dip),
16487c478bd9Sstevel@tonic-gate 			    phys_spec.pci_phys_low,
16497c478bd9Sstevel@tonic-gate 			    l, NDI_RA_TYPE_MEM,
16507c478bd9Sstevel@tonic-gate 			    NDI_RA_PASS) != NDI_SUCCESS) {
16517c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
16527c478bd9Sstevel@tonic-gate 				return (1);
16537c478bd9Sstevel@tonic-gate 			}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 			break;
16567c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
16577c478bd9Sstevel@tonic-gate 			/* free I/O space back to the allocator */
16587c478bd9Sstevel@tonic-gate 			if (ndi_ra_free(ddi_get_parent(dip),
16597c478bd9Sstevel@tonic-gate 			    phys_spec.pci_phys_low,
16607c478bd9Sstevel@tonic-gate 			    l, NDI_RA_TYPE_IO,
16617c478bd9Sstevel@tonic-gate 			    NDI_RA_PASS) != NDI_SUCCESS) {
16627c478bd9Sstevel@tonic-gate 				pci_unmap_phys(&h, &config);
16637c478bd9Sstevel@tonic-gate 				return (1);
16647c478bd9Sstevel@tonic-gate 			}
16657c478bd9Sstevel@tonic-gate 			break;
16667c478bd9Sstevel@tonic-gate 		default:
16677c478bd9Sstevel@tonic-gate 			pci_unmap_phys(&h, &config);
16687c478bd9Sstevel@tonic-gate 			return (1);
16697c478bd9Sstevel@tonic-gate 		} /* switch */
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * Now that memory locations are assigned,
16747c478bd9Sstevel@tonic-gate 	 * update the assigned address property.
16757c478bd9Sstevel@tonic-gate 	 */
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	FC_DEBUG1(1, CE_CONT, "updating assigned-addresss for %x\n",
16787c478bd9Sstevel@tonic-gate 	    phys_spec.pci_phys_hi);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	if (pfc_remove_assigned_prop(dip, &phys_spec)) {
16817c478bd9Sstevel@tonic-gate 		pci_unmap_phys(&h, &config);
16827c478bd9Sstevel@tonic-gate 		return (1);
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	pci_unmap_phys(&h, &config);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	return (0);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate int
pci_map_phys(dev_info_t * dip,pci_regspec_t * phys_spec,caddr_t * addrp,ddi_device_acc_attr_t * accattrp,ddi_acc_handle_t * handlep)16927c478bd9Sstevel@tonic-gate pci_map_phys(dev_info_t *dip, pci_regspec_t *phys_spec,
16937c478bd9Sstevel@tonic-gate 	caddr_t *addrp, ddi_device_acc_attr_t *accattrp,
16947c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t *handlep)
16957c478bd9Sstevel@tonic-gate {
16967c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
16977c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
16987c478bd9Sstevel@tonic-gate 	int result;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	*handlep = impl_acc_hdl_alloc(KM_SLEEP, NULL);
17017c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handlep);
17027c478bd9Sstevel@tonic-gate 	hp->ah_vers = VERS_ACCHDL;
17037c478bd9Sstevel@tonic-gate 	hp->ah_dip = dip;
17047c478bd9Sstevel@tonic-gate 	hp->ah_rnumber = 0;
17057c478bd9Sstevel@tonic-gate 	hp->ah_offset = 0;
17067c478bd9Sstevel@tonic-gate 	hp->ah_len = 0;
17077c478bd9Sstevel@tonic-gate 	hp->ah_acc = *accattrp;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_MAP_LOCKED;
17107c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_REGSPEC;
17117c478bd9Sstevel@tonic-gate 	mr.map_obj.rp = (struct regspec *)phys_spec;
17127c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
17137c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
17147c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
17157c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	result = ddi_map(dip, &mr, 0, 0, addrp);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	if (result != DDI_SUCCESS) {
17207c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(*handlep);
17217c478bd9Sstevel@tonic-gate 		*handlep = (ddi_acc_handle_t)NULL;
17227c478bd9Sstevel@tonic-gate 	} else {
17237c478bd9Sstevel@tonic-gate 		hp->ah_addr = *addrp;
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	return (result);
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate void
pci_unmap_phys(ddi_acc_handle_t * handlep,pci_regspec_t * ph)17307c478bd9Sstevel@tonic-gate pci_unmap_phys(ddi_acc_handle_t *handlep, pci_regspec_t *ph)
17317c478bd9Sstevel@tonic-gate {
17327c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
17337c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	hp = impl_acc_hdl_get(*handlep);
17367c478bd9Sstevel@tonic-gate 	ASSERT(hp);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	mr.map_op = DDI_MO_UNMAP;
17397c478bd9Sstevel@tonic-gate 	mr.map_type = DDI_MT_REGSPEC;
17407c478bd9Sstevel@tonic-gate 	mr.map_obj.rp = (struct regspec *)ph;
17417c478bd9Sstevel@tonic-gate 	mr.map_prot = PROT_READ | PROT_WRITE;
17427c478bd9Sstevel@tonic-gate 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
17437c478bd9Sstevel@tonic-gate 	mr.map_handlep = hp;
17447c478bd9Sstevel@tonic-gate 	mr.map_vers = DDI_MAP_VERSION;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
1747b1a80202SZach Kissel 	    hp->ah_len, &hp->ah_addr);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	impl_acc_hdl_free(*handlep);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	*handlep = (ddi_acc_handle_t)NULL;
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate int
pfc_update_assigned_prop(dev_info_t * dip,pci_regspec_t * newone)17567c478bd9Sstevel@tonic-gate pfc_update_assigned_prop(dev_info_t *dip, pci_regspec_t *newone)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	int		alen;
17597c478bd9Sstevel@tonic-gate 	pci_regspec_t	*assigned;
17607c478bd9Sstevel@tonic-gate 	caddr_t		newreg;
17617c478bd9Sstevel@tonic-gate 	uint_t		status;
17627c478bd9Sstevel@tonic-gate 
1763a3282898Scth 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1764b1a80202SZach Kissel 	    "assigned-addresses", (caddr_t)&assigned, &alen);
17657c478bd9Sstevel@tonic-gate 	switch (status) {
17667c478bd9Sstevel@tonic-gate 		case DDI_PROP_SUCCESS:
17677c478bd9Sstevel@tonic-gate 		break;
17687c478bd9Sstevel@tonic-gate 		case DDI_PROP_NO_MEMORY:
17697c478bd9Sstevel@tonic-gate 			return (1);
17707c478bd9Sstevel@tonic-gate 		default:
17717c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1772b1a80202SZach Kissel 			    "assigned-addresses", (int *)newone,
1773b1a80202SZach Kissel 			    sizeof (*newone)/sizeof (int));
17747c478bd9Sstevel@tonic-gate 			return (0);
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	/*
17787c478bd9Sstevel@tonic-gate 	 * Allocate memory for the existing
17797c478bd9Sstevel@tonic-gate 	 * assigned-addresses(s) plus one and then
17807c478bd9Sstevel@tonic-gate 	 * build it.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	newreg = kmem_zalloc(alen+sizeof (*newone), KM_SLEEP);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	bcopy(assigned, newreg, alen);
17867c478bd9Sstevel@tonic-gate 	bcopy(newone, newreg + alen, sizeof (*newone));
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/*
17897c478bd9Sstevel@tonic-gate 	 * Write out the new "assigned-addresses" spec
17907c478bd9Sstevel@tonic-gate 	 */
17917c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1792b1a80202SZach Kissel 	    "assigned-addresses", (int *)newreg,
1793b1a80202SZach Kissel 	    (alen + sizeof (*newone))/sizeof (int));
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)newreg, alen+sizeof (*newone));
1796b1a80202SZach Kissel 	kmem_free(assigned, alen);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	return (0);
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate int
pfc_remove_assigned_prop(dev_info_t * dip,pci_regspec_t * oldone)18017c478bd9Sstevel@tonic-gate pfc_remove_assigned_prop(dev_info_t *dip, pci_regspec_t *oldone)
18027c478bd9Sstevel@tonic-gate {
18037c478bd9Sstevel@tonic-gate 	int		alen, new_len, num_entries, i;
18047c478bd9Sstevel@tonic-gate 	pci_regspec_t	*assigned;
18057c478bd9Sstevel@tonic-gate 	uint_t		status;
18067c478bd9Sstevel@tonic-gate 
1807a3282898Scth 	status = ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1808b1a80202SZach Kissel 	    "assigned-addresses", (caddr_t)&assigned, &alen);
18097c478bd9Sstevel@tonic-gate 	switch (status) {
18107c478bd9Sstevel@tonic-gate 		case DDI_PROP_SUCCESS:
18117c478bd9Sstevel@tonic-gate 		break;
18127c478bd9Sstevel@tonic-gate 		case DDI_PROP_NO_MEMORY:
18137c478bd9Sstevel@tonic-gate 			return (1);
18147c478bd9Sstevel@tonic-gate 		default:
18157c478bd9Sstevel@tonic-gate 			return (0);
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	num_entries = alen / sizeof (pci_regspec_t);
18197c478bd9Sstevel@tonic-gate 	new_len = alen - sizeof (pci_regspec_t);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/*
18227c478bd9Sstevel@tonic-gate 	 * Search for the memory being removed.
18237c478bd9Sstevel@tonic-gate 	 */
18247c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_entries; i++) {
18257c478bd9Sstevel@tonic-gate 		if (assigned[i].pci_phys_hi == oldone->pci_phys_hi) {
18267c478bd9Sstevel@tonic-gate 			if (new_len == 0) {
18277c478bd9Sstevel@tonic-gate 				(void) ndi_prop_remove(DDI_DEV_T_NONE, dip,
18287c478bd9Sstevel@tonic-gate 				    "assigned-addresses");
18297c478bd9Sstevel@tonic-gate 				break;
18307c478bd9Sstevel@tonic-gate 			}
18317c478bd9Sstevel@tonic-gate 			if ((new_len - (i * sizeof (pci_regspec_t)))
18327c478bd9Sstevel@tonic-gate 			    == 0) {
18337c478bd9Sstevel@tonic-gate 				FC_DEBUG1(1, CE_CONT, "assigned-address entry "
18347c478bd9Sstevel@tonic-gate 				    "%x removed from property (last entry)\n",
18357c478bd9Sstevel@tonic-gate 				    oldone->pci_phys_hi);
18367c478bd9Sstevel@tonic-gate 			} else {
18377c478bd9Sstevel@tonic-gate 				bcopy((void *)(assigned + i + 1),
18387c478bd9Sstevel@tonic-gate 				    (void *)(assigned + i),
18397c478bd9Sstevel@tonic-gate 				    (new_len - (i * sizeof (pci_regspec_t))));
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 				FC_DEBUG1(1, CE_CONT, "assigned-address entry "
18427c478bd9Sstevel@tonic-gate 				    "%x removed from property\n",
18437c478bd9Sstevel@tonic-gate 				    oldone->pci_phys_hi);
18447c478bd9Sstevel@tonic-gate 			}
18457c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
18467c478bd9Sstevel@tonic-gate 			    dip, "assigned-addresses", (int *)assigned,
18477c478bd9Sstevel@tonic-gate 			    (new_len/sizeof (int)));
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 			break;
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
1853b1a80202SZach Kissel 	kmem_free(assigned, alen);
1854b1a80202SZach Kissel 
18557c478bd9Sstevel@tonic-gate 	return (0);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate /*
18587c478bd9Sstevel@tonic-gate  * we recognize the non transparent bridge child nodes with the
18597c478bd9Sstevel@tonic-gate  * following property. This is specific to this implementation only.
18607c478bd9Sstevel@tonic-gate  * This property is specific to AP nodes only.
18617c478bd9Sstevel@tonic-gate  */
18627c478bd9Sstevel@tonic-gate #define	PCICFG_DEV_CONF_MAP_PROP		"pci-parent-indirect"
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate  * If a non transparent bridge drives a hotplug/hotswap bus, then
18667c478bd9Sstevel@tonic-gate  * the following property must be defined for the node either by
18677c478bd9Sstevel@tonic-gate  * the driver or the OBP.
18687c478bd9Sstevel@tonic-gate  */
18697c478bd9Sstevel@tonic-gate #define	PCICFG_BUS_CONF_MAP_PROP		"pci-conf-indirect"
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate /*
18727c478bd9Sstevel@tonic-gate  * this function is called only for SPARC platforms, where we may have
18737c478bd9Sstevel@tonic-gate  * a mix n' match of direct vs indirectly mapped configuration space.
18747c478bd9Sstevel@tonic-gate  */
18757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18767c478bd9Sstevel@tonic-gate static int
fcpci_indirect_map(dev_info_t * dip)18777c478bd9Sstevel@tonic-gate fcpci_indirect_map(dev_info_t *dip)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	int rc = DDI_FAILURE;
18807c478bd9Sstevel@tonic-gate 
1881ba640a72Sjj 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
1882b1a80202SZach Kissel 	    PCICFG_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
18837c478bd9Sstevel@tonic-gate 		rc = DDI_SUCCESS;
18847c478bd9Sstevel@tonic-gate 	else
18857c478bd9Sstevel@tonic-gate 		if (ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(dip),
1886b1a80202SZach Kissel 		    0, PCICFG_BUS_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
18877c478bd9Sstevel@tonic-gate 			rc = DDI_SUCCESS;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	return (rc);
18907c478bd9Sstevel@tonic-gate }
1891