10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52434Sanish  * Common Development and Distribution License (the "License").
62434Sanish  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8772SDan.Mick@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/mkdev.h>
28117Sschwartz #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/sunddi.h>
300Sstevel@tonic-gate #include <vm/seg_kmem.h>
310Sstevel@tonic-gate #include <sys/machparam.h>
32916Sschwartz #include <sys/sunndi.h>
330Sstevel@tonic-gate #include <sys/ontrap.h>
34916Sschwartz #include <sys/psm.h>
35881Sjohnny #include <sys/pcie.h>
360Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
370Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
380Sstevel@tonic-gate #include <sys/pci_tools.h>
391083Sanish #include <io/pci/pci_tools_ext.h>
403446Smrj #include <sys/apic.h>
41916Sschwartz #include <io/pci/pci_var.h>
420Sstevel@tonic-gate #include <sys/promif.h>
431083Sanish #include <sys/x86_archext.h>
442434Sanish #include <sys/cpuvar.h>
450Sstevel@tonic-gate 
465084Sjohnlev #ifdef __xpv
475084Sjohnlev #include <sys/hypervisor.h>
485084Sjohnlev #endif
495084Sjohnlev 
50777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
51777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
52777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
53777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
54777Sschwartz 
550Sstevel@tonic-gate #define	SUCCESS	0
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int pcitool_debug = 0;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
610Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static uint8_t pci_bars[] = {
640Sstevel@tonic-gate 	0x0,
650Sstevel@tonic-gate 	PCI_CONF_BASE0,
660Sstevel@tonic-gate 	PCI_CONF_BASE1,
670Sstevel@tonic-gate 	PCI_CONF_BASE2,
680Sstevel@tonic-gate 	PCI_CONF_BASE3,
690Sstevel@tonic-gate 	PCI_CONF_BASE4,
700Sstevel@tonic-gate 	PCI_CONF_BASE5,
710Sstevel@tonic-gate 	PCI_CONF_ROM
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
74777Sschwartz /* Max offset allowed into config space for a particular device. */
75777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
76777Sschwartz 
770Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
78777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
79777Sschwartz     boolean_t write_flag);
800Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
810Sstevel@tonic-gate     boolean_t write_flag);
820Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg,
830Sstevel@tonic-gate     boolean_t write_flag);
840Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg,
850Sstevel@tonic-gate     uint64_t virt_addr, boolean_t write_flag);
860Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
870Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
880Sstevel@tonic-gate 
894397Sschwartz /* Extern declarations */
90916Sschwartz extern int	(*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
91916Sschwartz 		    psm_intr_op_t, int *);
92916Sschwartz 
93117Sschwartz int
94777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
95117Sschwartz {
96117Sschwartz 	int instance = ddi_get_instance(dip);
97117Sschwartz 
98117Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
99117Sschwartz 
100117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
101117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
102117Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
103117Sschwartz 		return (DDI_FAILURE);
104117Sschwartz 	}
105117Sschwartz 
106117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
107117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
108117Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
109117Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
110117Sschwartz 		return (DDI_FAILURE);
111117Sschwartz 	}
112117Sschwartz 
113777Sschwartz 	if (is_pciex)
114777Sschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
115777Sschwartz 
116117Sschwartz 	return (DDI_SUCCESS);
117117Sschwartz }
118117Sschwartz 
119117Sschwartz void
120117Sschwartz pcitool_uninit(dev_info_t *dip)
121117Sschwartz {
122117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
123117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
124117Sschwartz }
125117Sschwartz 
126916Sschwartz /*ARGSUSED*/
127916Sschwartz static int
128916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
129916Sschwartz {
130916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
131916Sschwartz 	pcitool_intr_set_t iset;
132916Sschwartz 	uint32_t old_cpu;
133916Sschwartz 	int ret, result;
1344397Sschwartz 	size_t copyinout_size;
135916Sschwartz 	int rval = SUCCESS;
136916Sschwartz 
1374397Sschwartz 	/* Version 1 of pcitool_intr_set_t doesn't have flags. */
1384397Sschwartz 	copyinout_size = (size_t)&iset.flags - (size_t)&iset;
1394397Sschwartz 
1404397Sschwartz 	if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
141916Sschwartz 		return (EFAULT);
142916Sschwartz 
1434397Sschwartz 	switch (iset.user_version) {
1444397Sschwartz 	case PCITOOL_V1:
1454397Sschwartz 		break;
1464397Sschwartz 
1474397Sschwartz 	case PCITOOL_V2:
1484397Sschwartz 		copyinout_size = sizeof (pcitool_intr_set_t);
1494397Sschwartz 		if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
1504397Sschwartz 			return (EFAULT);
1514397Sschwartz 		break;
1524397Sschwartz 
1534397Sschwartz 	default:
1544397Sschwartz 		iset.status = PCITOOL_OUT_OF_RANGE;
1554397Sschwartz 		rval = ENOTSUP;
1564397Sschwartz 		goto done_set_intr;
1574397Sschwartz 	}
1584397Sschwartz 
159916Sschwartz 	if (iset.ino > APIC_MAX_VECTOR) {
160916Sschwartz 		rval = EINVAL;
161916Sschwartz 		iset.status = PCITOOL_INVALID_INO;
162916Sschwartz 		goto done_set_intr;
163916Sschwartz 	}
164916Sschwartz 
165916Sschwartz 	iset.status = PCITOOL_SUCCESS;
166916Sschwartz 
167916Sschwartz 	if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
168916Sschwartz 		iset.status = PCITOOL_IO_ERROR;
169916Sschwartz 		rval = EINVAL;
170916Sschwartz 		goto done_set_intr;
171916Sschwartz 	}
172916Sschwartz 
1734397Sschwartz 
174916Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
175916Sschwartz 
176916Sschwartz 	/*
177916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
178916Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
179916Sschwartz 	 */
180916Sschwartz 	info_hdl.ih_vector = iset.ino;
181916Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
1824397Sschwartz 	if (pcitool_debug)
1834397Sschwartz 		prom_printf("user version:%d, flags:0x%x\n",
1844397Sschwartz 		    iset.user_version, iset.flags);
185916Sschwartz 
1864397Sschwartz 	result = ENOTSUP;
1874397Sschwartz 	if ((iset.user_version >= PCITOOL_V2) &&
1884397Sschwartz 	    (iset.flags & PCITOOL_INTR_SET_FLAG_GROUP)) {
1894397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
1904397Sschwartz 		    &result);
1914397Sschwartz 	} else {
1924397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
1934397Sschwartz 		    &result);
1944397Sschwartz 	}
1954397Sschwartz 
196916Sschwartz 	if (ret != PSM_SUCCESS) {
197916Sschwartz 		switch (result) {
198916Sschwartz 		case EIO:		/* Error making the change */
199916Sschwartz 			rval = EIO;
200916Sschwartz 			iset.status = PCITOOL_IO_ERROR;
201916Sschwartz 			break;
202916Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
203916Sschwartz 			rval = EINVAL;
204916Sschwartz 			iset.status = PCITOOL_INVALID_INO;
205916Sschwartz 			break;
206916Sschwartz 		case EINVAL:		/* CPU out of range */
207916Sschwartz 			rval = EINVAL;
208916Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
209916Sschwartz 			break;
2104397Sschwartz 		case ENOTSUP:		/* Requested PSM intr ops missing */
2114397Sschwartz 			rval = ENOTSUP;
2124397Sschwartz 			iset.status = PCITOOL_IO_ERROR;
2134397Sschwartz 			break;
214916Sschwartz 		}
215916Sschwartz 	}
216916Sschwartz 
217916Sschwartz 	/* Return original CPU. */
218916Sschwartz 	iset.cpu_id = old_cpu;
219916Sschwartz 
220916Sschwartz done_set_intr:
2214397Sschwartz 	iset.drvr_version = PCITOOL_VERSION;
2224397Sschwartz 	if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
223916Sschwartz 		rval = EFAULT;
224916Sschwartz 	return (rval);
225916Sschwartz }
226916Sschwartz 
227916Sschwartz 
228916Sschwartz /* It is assumed that dip != NULL */
229916Sschwartz static void
230916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
231916Sschwartz {
232916Sschwartz 	(void) strncpy(devs->driver_name,
233916Sschwartz 	    ddi_driver_name(dip), MAXMODCONFNAME-1);
234916Sschwartz 	devs->driver_name[MAXMODCONFNAME] = '\0';
235916Sschwartz 	(void) ddi_pathname(dip, devs->path);
236916Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
237916Sschwartz }
238916Sschwartz 
239916Sschwartz 
240916Sschwartz /*ARGSUSED*/
241916Sschwartz static int
242916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
243916Sschwartz {
244916Sschwartz 	/* Array part isn't used here, but oh well... */
245916Sschwartz 	pcitool_intr_get_t partial_iget;
246916Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
247916Sschwartz 	size_t	iget_kmem_alloc_size = 0;
248916Sschwartz 	uint8_t num_devs_ret;
249916Sschwartz 	int copyout_rval;
250916Sschwartz 	int rval = SUCCESS;
251916Sschwartz 	int circ;
252916Sschwartz 	int i;
253916Sschwartz 
254916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
255916Sschwartz 	apic_get_intr_t intr_info;
256916Sschwartz 
257916Sschwartz 	/* Read in just the header part, no array section. */
258916Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
259916Sschwartz 	    DDI_SUCCESS)
260916Sschwartz 		return (EFAULT);
261916Sschwartz 
262916Sschwartz 	/* Validate argument. */
263916Sschwartz 	if (partial_iget.ino > APIC_MAX_VECTOR) {
264916Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
265916Sschwartz 		partial_iget.num_devs_ret = 0;
266916Sschwartz 		rval = EINVAL;
267916Sschwartz 		goto done_get_intr;
268916Sschwartz 	}
269916Sschwartz 
270916Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
271916Sschwartz 	intr_info.avgi_dip_list = NULL;
272916Sschwartz 	intr_info.avgi_req_flags =
273916Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
274916Sschwartz 	/*
275916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
276916Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
277916Sschwartz 	 * global interrupt handling.
278916Sschwartz 	 */
279916Sschwartz 	info_hdl.ih_private = &intr_info;
280916Sschwartz 	info_hdl.ih_vector = partial_iget.ino;
281916Sschwartz 
282916Sschwartz 	/* Caller wants device information returned. */
283916Sschwartz 	if (num_devs_ret > 0) {
284916Sschwartz 
285916Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
286916Sschwartz 
287916Sschwartz 		/*
288916Sschwartz 		 * Allocate room.
289916Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
290916Sschwartz 		 */
291916Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
292916Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
293916Sschwartz 
294916Sschwartz 		/* Read in whole structure to verify there's room. */
295916Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
296916Sschwartz 		    SUCCESS) {
297916Sschwartz 
298916Sschwartz 			/* Be consistent and just return EFAULT here. */
299916Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
300916Sschwartz 
301916Sschwartz 			return (EFAULT);
302916Sschwartz 		}
303916Sschwartz 	}
304916Sschwartz 
305916Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
306916Sschwartz 	iget->ino = info_hdl.ih_vector;
307916Sschwartz 
308916Sschwartz 	/*
309916Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
310916Sschwartz 	 * be extracted from dips returned from the tree.
311916Sschwartz 	 */
312916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
313916Sschwartz 		ndi_devi_enter(dip, &circ);
314916Sschwartz 	}
315916Sschwartz 
316916Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
317916Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
318916Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
319916Sschwartz 		iget->status = PCITOOL_IO_ERROR;
320916Sschwartz 		iget->num_devs_ret = 0;
321916Sschwartz 		rval = EINVAL;
322916Sschwartz 		goto done_get_intr;
323916Sschwartz 	}
324916Sschwartz 
325916Sschwartz 	/*
326916Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
327916Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
328916Sschwartz 	 */
329916Sschwartz 	iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
330916Sschwartz 
331916Sschwartz 	/* Number of devices returned by apic. */
332916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
333916Sschwartz 
334916Sschwartz 	/* Device info was returned. */
335916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
336916Sschwartz 
337916Sschwartz 		/*
338916Sschwartz 		 * num devs returned is num devs ret by apic,
339916Sschwartz 		 * space permitting.
340916Sschwartz 		 */
341916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
342916Sschwartz 
343916Sschwartz 		/*
344916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
345916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
346916Sschwartz 		 */
347916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
348916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
349916Sschwartz 			    &iget->dev[i]);
350916Sschwartz 
351916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
352916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
353916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
354916Sschwartz 	}
355916Sschwartz 
356916Sschwartz done_get_intr:
357916Sschwartz 
358916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
359916Sschwartz 		ndi_devi_exit(dip, circ);
360916Sschwartz 	}
361916Sschwartz 
3624397Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
363916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
364916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
365916Sschwartz 
366916Sschwartz 	if (iget_kmem_alloc_size > 0)
367916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
368916Sschwartz 
369916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
370916Sschwartz 		rval = EFAULT;
371916Sschwartz 
372916Sschwartz 	return (rval);
373916Sschwartz }
374916Sschwartz 
3754397Sschwartz /*ARGSUSED*/
3764397Sschwartz static int
3774397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
3784397Sschwartz {
3794397Sschwartz 	pcitool_intr_info_t intr_info;
3804397Sschwartz 	ddi_intr_handle_impl_t info_hdl;
3814397Sschwartz 	int rval = SUCCESS;
3824397Sschwartz 
3834397Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
3844397Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
3854397Sschwartz 	    DDI_SUCCESS) {
3864397Sschwartz 		if (pcitool_debug)
3874397Sschwartz 			prom_printf("Error reading arguments\n");
3884397Sschwartz 		return (EFAULT);
3894397Sschwartz 	}
3904397Sschwartz 
3914397Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
3924397Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
3934397Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
3944397Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
3954397Sschwartz 		intr_info.ctlr_version = 0;
3964397Sschwartz 
3974397Sschwartz 	} else {
3984397Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
3994397Sschwartz 		if (strcmp((char *)info_hdl.ih_private,
4004397Sschwartz 		    APIC_PCPLUSMP_NAME) == 0)
4014397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
4024397Sschwartz 		else
4034397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
4044397Sschwartz 	}
4054397Sschwartz 
4064397Sschwartz 	intr_info.num_intr = APIC_MAX_VECTOR;
4074397Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
4084397Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4094397Sschwartz 	    DDI_SUCCESS) {
4104397Sschwartz 		if (pcitool_debug)
4114397Sschwartz 			prom_printf("Error returning arguments.\n");
4124397Sschwartz 		rval = EFAULT;
4134397Sschwartz 	}
4144397Sschwartz 
4154397Sschwartz 	return (rval);
4164397Sschwartz }
4174397Sschwartz 
4184397Sschwartz 
419916Sschwartz 
420916Sschwartz /*
421916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
422916Sschwartz  * Need to implement later
423916Sschwartz  */
424916Sschwartz /*ARGSUSED*/
425916Sschwartz int
426916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
427916Sschwartz {
428916Sschwartz 	int rval;
429916Sschwartz 
430916Sschwartz 	switch (cmd) {
431916Sschwartz 
432916Sschwartz 	/* Associate a new CPU with a given vector */
433916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
434916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
435916Sschwartz 		break;
436916Sschwartz 
437916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
438916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
439916Sschwartz 		break;
440916Sschwartz 
4414397Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
4424397Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
443916Sschwartz 		break;
444916Sschwartz 
445916Sschwartz 	default:
446916Sschwartz 		rval = ENOTSUP;
447916Sschwartz 	}
448916Sschwartz 
449916Sschwartz 	return (rval);
450916Sschwartz }
451916Sschwartz 
452916Sschwartz 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * A note about ontrap handling:
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
4570Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
4580Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
4590Sstevel@tonic-gate  * X86 platforms require it.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
4640Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
4650Sstevel@tonic-gate  */
466916Sschwartz 
4670Sstevel@tonic-gate /*ARGSUSED*/
4680Sstevel@tonic-gate int
469777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	return (ENOTSUP);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /* Swap endianness. */
4750Sstevel@tonic-gate static uint64_t
4760Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	typedef union {
4790Sstevel@tonic-gate 		uint64_t data64;
4800Sstevel@tonic-gate 		uint8_t data8[8];
4810Sstevel@tonic-gate 	} data_split_t;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	data_split_t orig_data;
4840Sstevel@tonic-gate 	data_split_t returned_data;
4850Sstevel@tonic-gate 	int i;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	orig_data.data64 = data;
4880Sstevel@tonic-gate 	returned_data.data64 = 0;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4910Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	return (returned_data.data64);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 
498777Sschwartz /*
499777Sschwartz  * Access device.  prg is modified.
500777Sschwartz  *
501777Sschwartz  * Extended config space is available only through memory-mapped access.
502777Sschwartz  * Standard config space on pci express devices is available either way,
503*8772SDan.Mick@Sun.COM  * so do it memory-mapped here too, for simplicity, if allowed by MCFG.
504*8772SDan.Mick@Sun.COM  * If anything fails, return EINVAL so caller can try I/O access.
505777Sschwartz  */
506777Sschwartz /*ARGSUSED*/
507777Sschwartz static int
508777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
509777Sschwartz     boolean_t write_flag)
510777Sschwartz {
511777Sschwartz 	int rval = SUCCESS;
512777Sschwartz 	uint64_t virt_addr;
513777Sschwartz 	size_t	num_virt_pages;
514*8772SDan.Mick@Sun.COM 	int first_bus, last_bus;
515*8772SDan.Mick@Sun.COM 	int64_t *ecfginfo;
516*8772SDan.Mick@Sun.COM 	uint_t nelem;
517777Sschwartz 
518777Sschwartz 	prg->status = PCITOOL_SUCCESS;
519777Sschwartz 
520*8772SDan.Mick@Sun.COM 	if (ddi_prop_lookup_int64_array(DDI_DEV_T_ANY, dip, 0,
521*8772SDan.Mick@Sun.COM 	    "ecfg", &ecfginfo, &nelem) == DDI_PROP_SUCCESS) {
522*8772SDan.Mick@Sun.COM 
523*8772SDan.Mick@Sun.COM 		/*
524*8772SDan.Mick@Sun.COM 		 * We must have a four-element property; base addr [0] must
525*8772SDan.Mick@Sun.COM 		 * be nonzero.  Also, segment [1] must be 0 for now; we don't
526*8772SDan.Mick@Sun.COM 		 * handle nonzero segments (or create a property containing
527*8772SDan.Mick@Sun.COM 		 * them)
528*8772SDan.Mick@Sun.COM 		 */
529*8772SDan.Mick@Sun.COM 		if ((nelem != 4) || (ecfginfo[0] == 0) || (ecfginfo[1] != 0)) {
530*8772SDan.Mick@Sun.COM 			ddi_prop_free(ecfginfo);
531*8772SDan.Mick@Sun.COM 			return (EINVAL);
532*8772SDan.Mick@Sun.COM 		}
533*8772SDan.Mick@Sun.COM 
534*8772SDan.Mick@Sun.COM 		prg->phys_addr = ecfginfo[0];
535*8772SDan.Mick@Sun.COM 		first_bus = ecfginfo[2];
536*8772SDan.Mick@Sun.COM 		last_bus = ecfginfo[3];
537*8772SDan.Mick@Sun.COM 
538*8772SDan.Mick@Sun.COM 		ddi_prop_free(ecfginfo);
539*8772SDan.Mick@Sun.COM 
540*8772SDan.Mick@Sun.COM 		if (prg->bus_no < first_bus || prg->bus_no > last_bus)
541*8772SDan.Mick@Sun.COM 			return (EINVAL);
542*8772SDan.Mick@Sun.COM 	} else {
543*8772SDan.Mick@Sun.COM 		return (EINVAL);
544777Sschwartz 	}
545777Sschwartz 
546777Sschwartz 	prg->phys_addr += prg->offset +
547777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
548777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
549777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
550777Sschwartz 
551777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
552777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
553*8772SDan.Mick@Sun.COM 
554*8772SDan.Mick@Sun.COM 	if (virt_addr == NULL)
555*8772SDan.Mick@Sun.COM 		return (EINVAL);
556777Sschwartz 
557777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
558777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
559777Sschwartz 	return (rval);
560777Sschwartz }
561777Sschwartz 
5620Sstevel@tonic-gate /* Access device.  prg is modified. */
5630Sstevel@tonic-gate /*ARGSUSED*/
5640Sstevel@tonic-gate static int
5650Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5680Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5690Sstevel@tonic-gate 	int rval = SUCCESS;
5700Sstevel@tonic-gate 	uint64_t local_data;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
573*8772SDan.Mick@Sun.COM 	 * NOTE: there is no way to verify whether or not the address is
574*8772SDan.Mick@Sun.COM 	 * valid other than that it is within the maximum offset.  The
575*8772SDan.Mick@Sun.COM 	 * put functions return void and the get functions return ff on
5760Sstevel@tonic-gate 	 * error.
5770Sstevel@tonic-gate 	 */
578*8772SDan.Mick@Sun.COM 
579*8772SDan.Mick@Sun.COM 	if (prg->offset + size - 1 > 0xFF) {
580*8772SDan.Mick@Sun.COM 		prg->status = PCITOOL_INVALID_ADDRESS;
581*8772SDan.Mick@Sun.COM 		return (ENOTSUP);
582*8772SDan.Mick@Sun.COM 	}
583*8772SDan.Mick@Sun.COM 
5840Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (write_flag) {
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		if (big_endian) {
5890Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5900Sstevel@tonic-gate 		} else {
5910Sstevel@tonic-gate 			local_data = prg->data;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		switch (size) {
5950Sstevel@tonic-gate 		case 1:
5960Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
5970Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5980Sstevel@tonic-gate 			break;
5990Sstevel@tonic-gate 		case 2:
6000Sstevel@tonic-gate 			(*pci_putw_func)(prg->bus_no, prg->dev_no,
6010Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		case 4:
6040Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
6050Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 		default:
6080Sstevel@tonic-gate 			rval = ENOTSUP;
6090Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6100Sstevel@tonic-gate 			break;
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 	} else {
6130Sstevel@tonic-gate 		switch (size) {
6140Sstevel@tonic-gate 		case 1:
6150Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
6160Sstevel@tonic-gate 			    prg->func_no, prg->offset);
6170Sstevel@tonic-gate 			break;
6180Sstevel@tonic-gate 		case 2:
6190Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
6200Sstevel@tonic-gate 			    prg->func_no, prg->offset);
6210Sstevel@tonic-gate 			break;
6220Sstevel@tonic-gate 		case 4:
6230Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
6240Sstevel@tonic-gate 			    prg->func_no, prg->offset);
6250Sstevel@tonic-gate 			break;
6260Sstevel@tonic-gate 		default:
6270Sstevel@tonic-gate 			rval = ENOTSUP;
6280Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6290Sstevel@tonic-gate 			break;
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (rval == SUCCESS) {
6330Sstevel@tonic-gate 			if (big_endian) {
6340Sstevel@tonic-gate 				prg->data =
6350Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6360Sstevel@tonic-gate 			} else {
6370Sstevel@tonic-gate 				prg->data = local_data;
6380Sstevel@tonic-gate 			}
6390Sstevel@tonic-gate 		}
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
6420Sstevel@tonic-gate 	return (rval);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*ARGSUSED*/
6470Sstevel@tonic-gate static int
6480Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6510Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6520Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6530Sstevel@tonic-gate 	int rval = SUCCESS;
6540Sstevel@tonic-gate 	on_trap_data_t otd;
6550Sstevel@tonic-gate 	uint64_t local_data;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * on_trap works like setjmp.
6600Sstevel@tonic-gate 	 *
6610Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6620Sstevel@tonic-gate 	 *
6630Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6640Sstevel@tonic-gate 	 */
6650Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6660Sstevel@tonic-gate 		no_trap();
6670Sstevel@tonic-gate 		if (pcitool_debug)
6680Sstevel@tonic-gate 			prom_printf(
6694397Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
6700Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6710Sstevel@tonic-gate 		return (EFAULT);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (write_flag) {
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		if (big_endian) {
6770Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6780Sstevel@tonic-gate 		} else {
6790Sstevel@tonic-gate 			local_data = prg->data;
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		if (pcitool_debug)
6830Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
6840Sstevel@tonic-gate 			    size, port);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		switch (size) {
6870Sstevel@tonic-gate 		case 1:
6880Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
6890Sstevel@tonic-gate 			break;
6900Sstevel@tonic-gate 		case 2:
6910Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 		case 4:
6940Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6950Sstevel@tonic-gate 			break;
6960Sstevel@tonic-gate 		default:
6970Sstevel@tonic-gate 			rval = ENOTSUP;
6980Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6990Sstevel@tonic-gate 			break;
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 	} else {
7020Sstevel@tonic-gate 		if (pcitool_debug)
7030Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
7040Sstevel@tonic-gate 			    size, port);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		switch (size) {
7070Sstevel@tonic-gate 		case 1:
7080Sstevel@tonic-gate 			local_data = inb(port);
7090Sstevel@tonic-gate 			break;
7100Sstevel@tonic-gate 		case 2:
7110Sstevel@tonic-gate 			local_data = inw(port);
7120Sstevel@tonic-gate 			break;
7130Sstevel@tonic-gate 		case 4:
7140Sstevel@tonic-gate 			local_data = inl(port);
7150Sstevel@tonic-gate 			break;
7160Sstevel@tonic-gate 		default:
7170Sstevel@tonic-gate 			rval = ENOTSUP;
7180Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7190Sstevel@tonic-gate 			break;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		if (rval == SUCCESS) {
7230Sstevel@tonic-gate 			if (big_endian) {
7240Sstevel@tonic-gate 				prg->data =
7250Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7260Sstevel@tonic-gate 			} else {
7270Sstevel@tonic-gate 				prg->data = local_data;
7280Sstevel@tonic-gate 			}
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	no_trap();
7330Sstevel@tonic-gate 	return (rval);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate /*ARGSUSED*/
7370Sstevel@tonic-gate static int
7380Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
739117Sschwartz 	boolean_t write_flag)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
7420Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
7430Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
7440Sstevel@tonic-gate 	on_trap_data_t otd;
7450Sstevel@tonic-gate 	uint64_t local_data;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * on_trap works like setjmp.
7490Sstevel@tonic-gate 	 *
7500Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7510Sstevel@tonic-gate 	 *
7520Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7550Sstevel@tonic-gate 		no_trap();
7560Sstevel@tonic-gate 		if (pcitool_debug)
7570Sstevel@tonic-gate 			prom_printf(
7580Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7590Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7600Sstevel@tonic-gate 		return (EFAULT);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (write_flag) {
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		if (big_endian) {
7660Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7670Sstevel@tonic-gate 		} else {
7680Sstevel@tonic-gate 			local_data = prg->data;
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 		switch (size) {
7720Sstevel@tonic-gate 		case 1:
7730Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
7740Sstevel@tonic-gate 			break;
7750Sstevel@tonic-gate 		case 2:
7760Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
7770Sstevel@tonic-gate 			break;
7780Sstevel@tonic-gate 		case 4:
7790Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
7800Sstevel@tonic-gate 			break;
7810Sstevel@tonic-gate 		case 8:
7820Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
7830Sstevel@tonic-gate 			break;
7840Sstevel@tonic-gate 		default:
7850Sstevel@tonic-gate 			rval = ENOTSUP;
7860Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7870Sstevel@tonic-gate 			break;
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	} else {
7900Sstevel@tonic-gate 		switch (size) {
7910Sstevel@tonic-gate 		case 1:
7920Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
7930Sstevel@tonic-gate 			break;
7940Sstevel@tonic-gate 		case 2:
7950Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7960Sstevel@tonic-gate 			break;
7970Sstevel@tonic-gate 		case 4:
7980Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7990Sstevel@tonic-gate 			break;
8000Sstevel@tonic-gate 		case 8:
8010Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
8020Sstevel@tonic-gate 			break;
8030Sstevel@tonic-gate 		default:
8040Sstevel@tonic-gate 			rval = ENOTSUP;
8050Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
8060Sstevel@tonic-gate 			break;
8070Sstevel@tonic-gate 		}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		if (rval == SUCCESS) {
8100Sstevel@tonic-gate 			if (big_endian) {
8110Sstevel@tonic-gate 				prg->data =
8120Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
8130Sstevel@tonic-gate 			} else {
8140Sstevel@tonic-gate 				prg->data = local_data;
8150Sstevel@tonic-gate 			}
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	no_trap();
8200Sstevel@tonic-gate 	return (rval);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
8250Sstevel@tonic-gate  *
8260Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
8270Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
8280Sstevel@tonic-gate  * We'll never have to map more than two pages.
8290Sstevel@tonic-gate  */
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate static uint64_t
8320Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
8360Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
8370Sstevel@tonic-gate 	void *virt_base;
8380Sstevel@tonic-gate 	uint64_t returned_addr;
8393446Smrj 	pfn_t pfn;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	if (pcitool_debug)
8420Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
8437632SNick.Todd@Sun.COM 		    (void *)(uintptr_t)phys_addr);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	*num_pages = 1;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8480Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8490Sstevel@tonic-gate 		if (pcitool_debug)
8500Sstevel@tonic-gate 			prom_printf("boundary violation: "
851777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
852777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8530Sstevel@tonic-gate 		return (NULL);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8560Sstevel@tonic-gate 		(*num_pages)++;
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8600Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8610Sstevel@tonic-gate 	if (virt_base == NULL) {
8620Sstevel@tonic-gate 		if (pcitool_debug)
8630Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
8640Sstevel@tonic-gate 		return (NULL);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (pcitool_debug)
8680Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8690Sstevel@tonic-gate 
8705084Sjohnlev #ifdef __xpv
8715084Sjohnlev 	/*
8725084Sjohnlev 	 * We should only get here if we are dom0.
8735084Sjohnlev 	 * We're using a real device so we need to translate the MA to a PFN.
8745084Sjohnlev 	 */
8755084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
8765084Sjohnlev 	pfn = xen_assign_pfn(mmu_btop(page_base));
8775084Sjohnlev #else
8783446Smrj 	pfn = btop(page_base);
8795084Sjohnlev #endif
8803446Smrj 
8810Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
8823446Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
8833446Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
8840Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (pcitool_debug)
8890Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
8900Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	return (returned_addr);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /* Unmap the mapped page(s). */
8960Sstevel@tonic-gate static void
8970Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
9020Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
9030Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
9080Sstevel@tonic-gate int
909777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
9120Sstevel@tonic-gate 	int		rval = 0;
9130Sstevel@tonic-gate 	pcitool_reg_t	prg;
9140Sstevel@tonic-gate 	uint8_t		size;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	uint64_t	base_addr;
9170Sstevel@tonic-gate 	uint64_t	virt_addr;
9180Sstevel@tonic-gate 	size_t		num_virt_pages;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	switch (cmd) {
9210Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
9220Sstevel@tonic-gate 		write_flag = B_TRUE;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	/*FALLTHRU*/
9250Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
9260Sstevel@tonic-gate 		if (pcitool_debug)
9270Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
9280Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
9290Sstevel@tonic-gate 		    DDI_SUCCESS) {
9300Sstevel@tonic-gate 			if (pcitool_debug)
9310Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
9320Sstevel@tonic-gate 			return (EFAULT);
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
9360Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
9370Sstevel@tonic-gate 			rval = EINVAL;
9380Sstevel@tonic-gate 			goto done_reg;
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		if (pcitool_debug)
9420Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9430Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
9440Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
9450Sstevel@tonic-gate 		if (((prg.bus_no &
9460Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9470Sstevel@tonic-gate 		    prg.bus_no) ||
9480Sstevel@tonic-gate 		    ((prg.dev_no &
9490Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9500Sstevel@tonic-gate 		    prg.dev_no) ||
9510Sstevel@tonic-gate 		    ((prg.func_no &
9520Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9530Sstevel@tonic-gate 		    prg.func_no)) {
9540Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9550Sstevel@tonic-gate 			rval = EINVAL;
9560Sstevel@tonic-gate 			goto done_reg;
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		/* Proper config space desired. */
9620Sstevel@tonic-gate 		if (prg.barnum == 0) {
9630Sstevel@tonic-gate 
964777Sschwartz 			if (pcitool_debug)
965777Sschwartz 				prom_printf(
966777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
967777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
968777Sschwartz 				    prg.offset, prg.phys_addr);
969777Sschwartz 
970777Sschwartz 			if (prg.offset >= max_cfg_size) {
9710Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
9720Sstevel@tonic-gate 				rval = EINVAL;
9730Sstevel@tonic-gate 				goto done_reg;
9740Sstevel@tonic-gate 			}
9750Sstevel@tonic-gate 
9761083Sanish 			/*
9771083Sanish 			 * Access device.  prg is modified.
9781083Sanish 			 * First, check for AMD northbridges for I/O access
9791083Sanish 			 * (This fix will move in future to pcitool user-land)
9801083Sanish 			 * Next, check for PCIe devices and do
9811083Sanish 			 * memory-mapped access
9821083Sanish 			 * Lastly, check for PCI devices and do I/O access
9831083Sanish 			 */
9842434Sanish 			if ((prg.bus_no == 0) &&
9852434Sanish 			    (prg.dev_no >= 0x18) &&
9867222Sdwoods 			    (prg.dev_no < (0x18 + ncpus)) &&
9877222Sdwoods 			    (cpuid_getvendor(CPU) == X86_VENDOR_AMD)) {
9887222Sdwoods 				rval = pcitool_cfg_access(dip, &prg,
9897222Sdwoods 				    write_flag);
990*8772SDan.Mick@Sun.COM 			} else if (max_cfg_size == PCIE_CONF_HDR_SIZE) {
991777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
992777Sschwartz 				    write_flag);
993*8772SDan.Mick@Sun.COM 				if (rval == EINVAL) {
994*8772SDan.Mick@Sun.COM 					/* Not valid for MMIO; try IO */
995*8772SDan.Mick@Sun.COM 					rval = pcitool_cfg_access(dip, &prg,
996*8772SDan.Mick@Sun.COM 					    write_flag);
997*8772SDan.Mick@Sun.COM 				}
998*8772SDan.Mick@Sun.COM 			} else {
999777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
1000777Sschwartz 				    write_flag);
1001*8772SDan.Mick@Sun.COM 			}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 			if (pcitool_debug)
10040Sstevel@tonic-gate 				prom_printf(
10050Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
10060Sstevel@tonic-gate 				    prg.data);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
10090Sstevel@tonic-gate 		} else {
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			pcitool_reg_t	prg2;
10120Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 			/*
10150Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
10160Sstevel@tonic-gate 			 * the device's config space.
10170Sstevel@tonic-gate 			 */
10180Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
10190Sstevel@tonic-gate 			prg2.acc_attr =
10200Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 			if (pcitool_debug)
10230Sstevel@tonic-gate 				prom_printf(
10240Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
10250Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
10260Sstevel@tonic-gate 			/*
10270Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
10280Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
10290Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
10300Sstevel@tonic-gate 			 */
10310Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
10320Sstevel@tonic-gate 			if (rval != SUCCESS) {
10330Sstevel@tonic-gate 				if (pcitool_debug)
10340Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
10350Sstevel@tonic-gate 				prg.status = prg2.status;
10360Sstevel@tonic-gate 				goto done_reg;
10370Sstevel@tonic-gate 			}
10380Sstevel@tonic-gate 			/*
10390Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
10400Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
10410Sstevel@tonic-gate 			 * BAR first.
10420Sstevel@tonic-gate 			 */
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 			if (pcitool_debug)
10450Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
10460Sstevel@tonic-gate 				    prg2.data);
10470Sstevel@tonic-gate 			if (!prg2.data) {
10480Sstevel@tonic-gate 				if (pcitool_debug)
10490Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
10500Sstevel@tonic-gate 				rval = EINVAL;
10510Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10520Sstevel@tonic-gate 				goto done_reg;
10530Sstevel@tonic-gate 			}
10540Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
10550Sstevel@tonic-gate 				if (pcitool_debug)
10560Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
10570Sstevel@tonic-gate 				rval = EINVAL;
10580Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10590Sstevel@tonic-gate 				goto done_reg;
10600Sstevel@tonic-gate 			}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
10640Sstevel@tonic-gate 			 * this is the ROM address register.
10650Sstevel@tonic-gate 			 */
10660Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
10670Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
10680Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10690Sstevel@tonic-gate 				if (pcitool_debug)
10700Sstevel@tonic-gate 					prom_printf("IO space\n");
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10730Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
10760Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10770Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 				goto done_reg;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 			/*
10830Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
10840Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10850Sstevel@tonic-gate 			 *
10860Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10870Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10880Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10890Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10900Sstevel@tonic-gate 			 */
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
10930Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 				uint32_t low_bytes =
10960Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 				/*
10990Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
11000Sstevel@tonic-gate 				 * past the end of BARs.
11010Sstevel@tonic-gate 				 */
11020Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
11030Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
11040Sstevel@tonic-gate 					rval = EIO;
11050Sstevel@tonic-gate 					goto done_reg;
11060Sstevel@tonic-gate 				}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 				/*
11090Sstevel@tonic-gate 				 * Access device.
11100Sstevel@tonic-gate 				 * prg2.status is modified on error.
11110Sstevel@tonic-gate 				 */
11120Sstevel@tonic-gate 				prg2.offset += 4;
11130Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
11140Sstevel@tonic-gate 				if (rval != SUCCESS) {
11150Sstevel@tonic-gate 					prg.status = prg2.status;
11160Sstevel@tonic-gate 					goto done_reg;
11170Sstevel@tonic-gate 				}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
11200Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
11210Sstevel@tonic-gate 					prg.status = EFAULT;
11220Sstevel@tonic-gate 					goto done_reg;
11230Sstevel@tonic-gate 				}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
11260Sstevel@tonic-gate 				if (pcitool_debug)
11270Sstevel@tonic-gate 					prom_printf(
11280Sstevel@tonic-gate 					    "64 bit mem space.  "
11290Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
11300Sstevel@tonic-gate 					    prg2.data);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 			/* Mem32 space, including ROM */
11330Sstevel@tonic-gate 			} else {
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
11360Sstevel@tonic-gate 					if (pcitool_debug)
11370Sstevel@tonic-gate 						prom_printf(
11380Sstevel@tonic-gate 						    "Additional ROM "
11390Sstevel@tonic-gate 						    "checking\n");
11400Sstevel@tonic-gate 					/* Can't write to ROM */
11410Sstevel@tonic-gate 					if (write_flag) {
11420Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
11430Sstevel@tonic-gate 						rval = EIO;
11440Sstevel@tonic-gate 						goto done_reg;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 					/* ROM disabled for reading */
11470Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
11480Sstevel@tonic-gate 						prg.status =
11490Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
11500Sstevel@tonic-gate 						rval = EIO;
11510Sstevel@tonic-gate 						goto done_reg;
11520Sstevel@tonic-gate 					}
11530Sstevel@tonic-gate 				}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 				if (pcitool_debug)
11560Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
11570Sstevel@tonic-gate 			}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 			base_addr = prg2.data;
11620Sstevel@tonic-gate 			if (pcitool_debug)
11630Sstevel@tonic-gate 				prom_printf(
11640Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
11650Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
11660Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
11670Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
11680Sstevel@tonic-gate 			/*
11690Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11700Sstevel@tonic-gate 			 * desired space, then access.
11710Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11720Sstevel@tonic-gate 			 */
11730Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11760Sstevel@tonic-gate 			    &num_virt_pages);
11770Sstevel@tonic-gate 			if (virt_addr == NULL) {
11780Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11790Sstevel@tonic-gate 				rval = EIO;
11800Sstevel@tonic-gate 				goto done_reg;
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
11840Sstevel@tonic-gate 			    write_flag);
11850Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate done_reg:
11884397Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11890Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11900Sstevel@tonic-gate 		    DDI_SUCCESS) {
11910Sstevel@tonic-gate 			if (pcitool_debug)
11920Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
11930Sstevel@tonic-gate 			rval = EFAULT;
11940Sstevel@tonic-gate 		}
11950Sstevel@tonic-gate 		break;
11960Sstevel@tonic-gate 	default:
11970Sstevel@tonic-gate 		rval = ENOTTY;
11980Sstevel@tonic-gate 		break;
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 	return (rval);
12010Sstevel@tonic-gate }
1202