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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/mkdev.h>
31117Sschwartz #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <vm/seg_kmem.h>
350Sstevel@tonic-gate #include <sys/machparam.h>
360Sstevel@tonic-gate #include <sys/ontrap.h>
370Sstevel@tonic-gate #include <sys/pci.h>
380Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
390Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
400Sstevel@tonic-gate #include <sys/pci_tools.h>
41*777Sschwartz #include "pci_tools_ext.h"
420Sstevel@tonic-gate #include "pci_var.h"
430Sstevel@tonic-gate #include <sys/promif.h>
440Sstevel@tonic-gate 
45*777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
46*777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
47*777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
48*777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
49*777Sschwartz 
500Sstevel@tonic-gate #define	SUCCESS	0
510Sstevel@tonic-gate 
520Sstevel@tonic-gate int pcitool_debug = 0;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
560Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static uint8_t pci_bars[] = {
590Sstevel@tonic-gate 	0x0,
600Sstevel@tonic-gate 	PCI_CONF_BASE0,
610Sstevel@tonic-gate 	PCI_CONF_BASE1,
620Sstevel@tonic-gate 	PCI_CONF_BASE2,
630Sstevel@tonic-gate 	PCI_CONF_BASE3,
640Sstevel@tonic-gate 	PCI_CONF_BASE4,
650Sstevel@tonic-gate 	PCI_CONF_BASE5,
660Sstevel@tonic-gate 	PCI_CONF_ROM
670Sstevel@tonic-gate };
680Sstevel@tonic-gate 
69*777Sschwartz /* Max offset allowed into config space for a particular device. */
70*777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
71*777Sschwartz 
720Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
73*777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
74*777Sschwartz     boolean_t write_flag);
750Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
760Sstevel@tonic-gate     boolean_t write_flag);
770Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg,
780Sstevel@tonic-gate     boolean_t write_flag);
790Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg,
800Sstevel@tonic-gate     uint64_t virt_addr, boolean_t write_flag);
810Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
820Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
830Sstevel@tonic-gate 
84117Sschwartz int
85*777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
86117Sschwartz {
87117Sschwartz 	int instance = ddi_get_instance(dip);
88117Sschwartz 
89117Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
90117Sschwartz 
91117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
92117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
93117Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
94117Sschwartz 		return (DDI_FAILURE);
95117Sschwartz 	}
96117Sschwartz 
97117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
98117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
99117Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
100117Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
101117Sschwartz 		return (DDI_FAILURE);
102117Sschwartz 	}
103117Sschwartz 
104*777Sschwartz 	if (is_pciex)
105*777Sschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
106*777Sschwartz 
107117Sschwartz 	return (DDI_SUCCESS);
108117Sschwartz }
109117Sschwartz 
110117Sschwartz void
111117Sschwartz pcitool_uninit(dev_info_t *dip)
112117Sschwartz {
113117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
114117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
115117Sschwartz }
116117Sschwartz 
117117Sschwartz 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * A note about ontrap handling:
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
1220Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
1230Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
1240Sstevel@tonic-gate  * X86 platforms require it.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Main function for handling interrupt CPU binding requests and queries.
1290Sstevel@tonic-gate  * Need to implement later
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate /*ARGSUSED*/
1320Sstevel@tonic-gate int
133*777Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	return (ENOTSUP);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
1410Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate /*ARGSUSED*/
1440Sstevel@tonic-gate int
145*777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	return (ENOTSUP);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /* Swap endianness. */
1510Sstevel@tonic-gate static uint64_t
1520Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	typedef union {
1550Sstevel@tonic-gate 		uint64_t data64;
1560Sstevel@tonic-gate 		uint8_t data8[8];
1570Sstevel@tonic-gate 	} data_split_t;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	data_split_t orig_data;
1600Sstevel@tonic-gate 	data_split_t returned_data;
1610Sstevel@tonic-gate 	int i;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	orig_data.data64 = data;
1640Sstevel@tonic-gate 	returned_data.data64 = 0;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
1670Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (returned_data.data64);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
174*777Sschwartz /*
175*777Sschwartz  * Access device.  prg is modified.
176*777Sschwartz  *
177*777Sschwartz  * Extended config space is available only through memory-mapped access.
178*777Sschwartz  * Standard config space on pci express devices is available either way,
179*777Sschwartz  * so do it memory-mapped here too, for simplicity.
180*777Sschwartz  */
181*777Sschwartz /*ARGSUSED*/
182*777Sschwartz static int
183*777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
184*777Sschwartz     boolean_t write_flag)
185*777Sschwartz {
186*777Sschwartz 	int rval = SUCCESS;
187*777Sschwartz 	uint64_t virt_addr;
188*777Sschwartz 	size_t	num_virt_pages;
189*777Sschwartz 
190*777Sschwartz 	prg->status = PCITOOL_SUCCESS;
191*777Sschwartz 
192*777Sschwartz 	/* XXX replace e0000000 value below with 0 once FW changes are made */
193*777Sschwartz 	prg->phys_addr = ddi_prop_get_int64(DDI_DEV_T_ANY, dip, 0,
194*777Sschwartz 	    "ecfga-base-address", 0xe00000000);
195*777Sschwartz 	if (prg->phys_addr == 0) {
196*777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
197*777Sschwartz 		return (EIO);
198*777Sschwartz 	}
199*777Sschwartz 
200*777Sschwartz 	prg->phys_addr += prg->offset +
201*777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
202*777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
203*777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
204*777Sschwartz 
205*777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
206*777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
207*777Sschwartz 	if (virt_addr == NULL) {
208*777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
209*777Sschwartz 		return (EIO);
210*777Sschwartz 	}
211*777Sschwartz 
212*777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
213*777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
214*777Sschwartz 	return (rval);
215*777Sschwartz }
216*777Sschwartz 
2170Sstevel@tonic-gate /* Access device.  prg is modified. */
2180Sstevel@tonic-gate /*ARGSUSED*/
2190Sstevel@tonic-gate static int
2200Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
2230Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
2240Sstevel@tonic-gate 	int rval = SUCCESS;
2250Sstevel@tonic-gate 	uint64_t local_data;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * NOTE: there is no way to verify whether or not the address is valid.
2290Sstevel@tonic-gate 	 * The put functions return void and the get functions return ff on
2300Sstevel@tonic-gate 	 * error.
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (write_flag) {
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		if (big_endian) {
2370Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
2380Sstevel@tonic-gate 		} else {
2390Sstevel@tonic-gate 			local_data = prg->data;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		switch (size) {
2430Sstevel@tonic-gate 		case 1:
2440Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
2450Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2460Sstevel@tonic-gate 			break;
2470Sstevel@tonic-gate 		case 2:
2480Sstevel@tonic-gate 			(*pci_putw_func)(prg->bus_no, prg->dev_no,
2490Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2500Sstevel@tonic-gate 			break;
2510Sstevel@tonic-gate 		case 4:
2520Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
2530Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		default:
2560Sstevel@tonic-gate 			rval = ENOTSUP;
2570Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	} else {
2610Sstevel@tonic-gate 		switch (size) {
2620Sstevel@tonic-gate 		case 1:
2630Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
2640Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2650Sstevel@tonic-gate 			break;
2660Sstevel@tonic-gate 		case 2:
2670Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
2680Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2690Sstevel@tonic-gate 			break;
2700Sstevel@tonic-gate 		case 4:
2710Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
2720Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2730Sstevel@tonic-gate 			break;
2740Sstevel@tonic-gate 		default:
2750Sstevel@tonic-gate 			rval = ENOTSUP;
2760Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		if (rval == SUCCESS) {
2810Sstevel@tonic-gate 			if (big_endian) {
2820Sstevel@tonic-gate 				prg->data =
2830Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
2840Sstevel@tonic-gate 			} else {
2850Sstevel@tonic-gate 				prg->data = local_data;
2860Sstevel@tonic-gate 			}
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
2900Sstevel@tonic-gate 	return (rval);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*ARGSUSED*/
2950Sstevel@tonic-gate static int
2960Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
2990Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
3000Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
3010Sstevel@tonic-gate 	int rval = SUCCESS;
3020Sstevel@tonic-gate 	on_trap_data_t otd;
3030Sstevel@tonic-gate 	uint64_t local_data;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * on_trap works like setjmp.
3080Sstevel@tonic-gate 	 *
3090Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
3100Sstevel@tonic-gate 	 *
3110Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
3140Sstevel@tonic-gate 		no_trap();
3150Sstevel@tonic-gate 		if (pcitool_debug)
3160Sstevel@tonic-gate 			prom_printf(
3170Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
3180Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
3190Sstevel@tonic-gate 		return (EFAULT);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (write_flag) {
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		if (big_endian) {
3250Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
3260Sstevel@tonic-gate 		} else {
3270Sstevel@tonic-gate 			local_data = prg->data;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		if (pcitool_debug)
3310Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
3320Sstevel@tonic-gate 			    size, port);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		switch (size) {
3350Sstevel@tonic-gate 		case 1:
3360Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
3370Sstevel@tonic-gate 			break;
3380Sstevel@tonic-gate 		case 2:
3390Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
3400Sstevel@tonic-gate 			break;
3410Sstevel@tonic-gate 		case 4:
3420Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
3430Sstevel@tonic-gate 			break;
3440Sstevel@tonic-gate 		default:
3450Sstevel@tonic-gate 			rval = ENOTSUP;
3460Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
3470Sstevel@tonic-gate 			break;
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 	} else {
3500Sstevel@tonic-gate 		if (pcitool_debug)
3510Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
3520Sstevel@tonic-gate 			    size, port);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		switch (size) {
3550Sstevel@tonic-gate 		case 1:
3560Sstevel@tonic-gate 			local_data = inb(port);
3570Sstevel@tonic-gate 			break;
3580Sstevel@tonic-gate 		case 2:
3590Sstevel@tonic-gate 			local_data = inw(port);
3600Sstevel@tonic-gate 			break;
3610Sstevel@tonic-gate 		case 4:
3620Sstevel@tonic-gate 			local_data = inl(port);
3630Sstevel@tonic-gate 			break;
3640Sstevel@tonic-gate 		default:
3650Sstevel@tonic-gate 			rval = ENOTSUP;
3660Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
3670Sstevel@tonic-gate 			break;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		if (rval == SUCCESS) {
3710Sstevel@tonic-gate 			if (big_endian) {
3720Sstevel@tonic-gate 				prg->data =
3730Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
3740Sstevel@tonic-gate 			} else {
3750Sstevel@tonic-gate 				prg->data = local_data;
3760Sstevel@tonic-gate 			}
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	no_trap();
3810Sstevel@tonic-gate 	return (rval);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate /*ARGSUSED*/
3850Sstevel@tonic-gate static int
3860Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
387117Sschwartz 	boolean_t write_flag)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
3900Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
3910Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
3920Sstevel@tonic-gate 	on_trap_data_t otd;
3930Sstevel@tonic-gate 	uint64_t local_data;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * on_trap works like setjmp.
3970Sstevel@tonic-gate 	 *
3980Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
3990Sstevel@tonic-gate 	 *
4000Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
4030Sstevel@tonic-gate 		no_trap();
4040Sstevel@tonic-gate 		if (pcitool_debug)
4050Sstevel@tonic-gate 			prom_printf(
4060Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
4070Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
4080Sstevel@tonic-gate 		return (EFAULT);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (write_flag) {
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 		if (big_endian) {
4140Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
4150Sstevel@tonic-gate 		} else {
4160Sstevel@tonic-gate 			local_data = prg->data;
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		switch (size) {
4200Sstevel@tonic-gate 		case 1:
4210Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
4220Sstevel@tonic-gate 			break;
4230Sstevel@tonic-gate 		case 2:
4240Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
4250Sstevel@tonic-gate 			break;
4260Sstevel@tonic-gate 		case 4:
4270Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 		case 8:
4300Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
4310Sstevel@tonic-gate 			break;
4320Sstevel@tonic-gate 		default:
4330Sstevel@tonic-gate 			rval = ENOTSUP;
4340Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
4350Sstevel@tonic-gate 			break;
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 	} else {
4380Sstevel@tonic-gate 		switch (size) {
4390Sstevel@tonic-gate 		case 1:
4400Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
4410Sstevel@tonic-gate 			break;
4420Sstevel@tonic-gate 		case 2:
4430Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
4440Sstevel@tonic-gate 			break;
4450Sstevel@tonic-gate 		case 4:
4460Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
4470Sstevel@tonic-gate 			break;
4480Sstevel@tonic-gate 		case 8:
4490Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
4500Sstevel@tonic-gate 			break;
4510Sstevel@tonic-gate 		default:
4520Sstevel@tonic-gate 			rval = ENOTSUP;
4530Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
4540Sstevel@tonic-gate 			break;
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if (rval == SUCCESS) {
4580Sstevel@tonic-gate 			if (big_endian) {
4590Sstevel@tonic-gate 				prg->data =
4600Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
4610Sstevel@tonic-gate 			} else {
4620Sstevel@tonic-gate 				prg->data = local_data;
4630Sstevel@tonic-gate 			}
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	no_trap();
4680Sstevel@tonic-gate 	return (rval);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
4750Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
4760Sstevel@tonic-gate  * We'll never have to map more than two pages.
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate static uint64_t
4800Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
4840Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
4850Sstevel@tonic-gate 	void *virt_base;
4860Sstevel@tonic-gate 	uint64_t returned_addr;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (pcitool_debug)
4890Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
4900Sstevel@tonic-gate 		    (uint8_t *)(uintptr_t)phys_addr);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	*num_pages = 1;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
4950Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
4960Sstevel@tonic-gate 		if (pcitool_debug)
4970Sstevel@tonic-gate 			prom_printf("boundary violation: "
498*777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
499*777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
5000Sstevel@tonic-gate 		return (NULL);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
5030Sstevel@tonic-gate 		(*num_pages)++;
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
5070Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
5080Sstevel@tonic-gate 	if (virt_base == NULL) {
5090Sstevel@tonic-gate 		if (pcitool_debug)
5100Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
5110Sstevel@tonic-gate 		return (NULL);
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (pcitool_debug)
5150Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
5180Sstevel@tonic-gate 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages),
5190Sstevel@tonic-gate 	    mmu_btop(page_base), PROT_READ | PROT_WRITE | HAT_STRICTORDER,
5200Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (pcitool_debug)
5250Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
5260Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	return (returned_addr);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /* Unmap the mapped page(s). */
5320Sstevel@tonic-gate static void
5330Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
5380Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
5390Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
5440Sstevel@tonic-gate int
545*777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
5480Sstevel@tonic-gate 	int		rval = 0;
5490Sstevel@tonic-gate 	pcitool_reg_t	prg;
5500Sstevel@tonic-gate 	uint8_t		size;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	uint64_t	base_addr;
5530Sstevel@tonic-gate 	uint64_t	virt_addr;
5540Sstevel@tonic-gate 	size_t		num_virt_pages;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	switch (cmd) {
5570Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
5580Sstevel@tonic-gate 		write_flag = B_TRUE;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/*FALLTHRU*/
5610Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
5620Sstevel@tonic-gate 		if (pcitool_debug)
5630Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
5640Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
5650Sstevel@tonic-gate 		    DDI_SUCCESS) {
5660Sstevel@tonic-gate 			if (pcitool_debug)
5670Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
5680Sstevel@tonic-gate 			return (EFAULT);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
5720Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
5730Sstevel@tonic-gate 			rval = EINVAL;
5740Sstevel@tonic-gate 			goto done_reg;
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 		if (pcitool_debug)
5780Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
5790Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
5800Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
5810Sstevel@tonic-gate 		if (((prg.bus_no &
5820Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
5830Sstevel@tonic-gate 		    prg.bus_no) ||
5840Sstevel@tonic-gate 		    ((prg.dev_no &
5850Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
5860Sstevel@tonic-gate 		    prg.dev_no) ||
5870Sstevel@tonic-gate 		    ((prg.func_no &
5880Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
5890Sstevel@tonic-gate 		    prg.func_no)) {
5900Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
5910Sstevel@tonic-gate 			rval = EINVAL;
5920Sstevel@tonic-gate 			goto done_reg;
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		/* Proper config space desired. */
5980Sstevel@tonic-gate 		if (prg.barnum == 0) {
5990Sstevel@tonic-gate 
600*777Sschwartz 			if (pcitool_debug)
601*777Sschwartz 				prom_printf(
602*777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
603*777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
604*777Sschwartz 				    prg.offset, prg.phys_addr);
605*777Sschwartz 
606*777Sschwartz 			if (prg.offset >= max_cfg_size) {
6070Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
6080Sstevel@tonic-gate 				rval = EINVAL;
6090Sstevel@tonic-gate 				goto done_reg;
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 			/* Access device.  prg is modified. */
613*777Sschwartz 			if (max_cfg_size == PCIE_CONF_HDR_SIZE)
614*777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
615*777Sschwartz 				    write_flag);
616*777Sschwartz 			else
617*777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
618*777Sschwartz 				    write_flag);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 			if (pcitool_debug)
6210Sstevel@tonic-gate 				prom_printf(
6220Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
6230Sstevel@tonic-gate 				    prg.data);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
6260Sstevel@tonic-gate 		} else {
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 			pcitool_reg_t	prg2;
6290Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 			/*
6320Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
6330Sstevel@tonic-gate 			 * the device's config space.
6340Sstevel@tonic-gate 			 */
6350Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
6360Sstevel@tonic-gate 			prg2.acc_attr =
6370Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 			if (pcitool_debug)
6400Sstevel@tonic-gate 				prom_printf(
6410Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
6420Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
6430Sstevel@tonic-gate 			/*
6440Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
6450Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
6460Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
6470Sstevel@tonic-gate 			 */
6480Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
6490Sstevel@tonic-gate 			if (rval != SUCCESS) {
6500Sstevel@tonic-gate 				if (pcitool_debug)
6510Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
6520Sstevel@tonic-gate 				prg.status = prg2.status;
6530Sstevel@tonic-gate 				goto done_reg;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 			/*
6560Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
6570Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
6580Sstevel@tonic-gate 			 * BAR first.
6590Sstevel@tonic-gate 			 */
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 			if (pcitool_debug)
6620Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
6630Sstevel@tonic-gate 				    prg2.data);
6640Sstevel@tonic-gate 			if (!prg2.data) {
6650Sstevel@tonic-gate 				if (pcitool_debug)
6660Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
6670Sstevel@tonic-gate 				rval = EINVAL;
6680Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
6690Sstevel@tonic-gate 				goto done_reg;
6700Sstevel@tonic-gate 			}
6710Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
6720Sstevel@tonic-gate 				if (pcitool_debug)
6730Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
6740Sstevel@tonic-gate 				rval = EINVAL;
6750Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
6760Sstevel@tonic-gate 				goto done_reg;
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
6810Sstevel@tonic-gate 			 * this is the ROM address register.
6820Sstevel@tonic-gate 			 */
6830Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
6840Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
6850Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
6860Sstevel@tonic-gate 				if (pcitool_debug)
6870Sstevel@tonic-gate 					prom_printf("IO space\n");
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
6900Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
6930Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
6940Sstevel@tonic-gate 					prom_printf("IO access failed\n");
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 				goto done_reg;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 			/*
7000Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
7010Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
7020Sstevel@tonic-gate 			 *
7030Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
7040Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
7050Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
7060Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
7070Sstevel@tonic-gate 			 */
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
7100Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 				uint32_t low_bytes =
7130Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 				/*
7160Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
7170Sstevel@tonic-gate 				 * past the end of BARs.
7180Sstevel@tonic-gate 				 */
7190Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
7200Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
7210Sstevel@tonic-gate 					rval = EIO;
7220Sstevel@tonic-gate 					goto done_reg;
7230Sstevel@tonic-gate 				}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 				/*
7260Sstevel@tonic-gate 				 * Access device.
7270Sstevel@tonic-gate 				 * prg2.status is modified on error.
7280Sstevel@tonic-gate 				 */
7290Sstevel@tonic-gate 				prg2.offset += 4;
7300Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
7310Sstevel@tonic-gate 				if (rval != SUCCESS) {
7320Sstevel@tonic-gate 					prg.status = prg2.status;
7330Sstevel@tonic-gate 					goto done_reg;
7340Sstevel@tonic-gate 				}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
7370Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
7380Sstevel@tonic-gate 					prg.status = EFAULT;
7390Sstevel@tonic-gate 					goto done_reg;
7400Sstevel@tonic-gate 				}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
7430Sstevel@tonic-gate 				if (pcitool_debug)
7440Sstevel@tonic-gate 					prom_printf(
7450Sstevel@tonic-gate 					    "64 bit mem space.  "
7460Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
7470Sstevel@tonic-gate 					    prg2.data);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 			/* Mem32 space, including ROM */
7500Sstevel@tonic-gate 			} else {
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
7530Sstevel@tonic-gate 					if (pcitool_debug)
7540Sstevel@tonic-gate 						prom_printf(
7550Sstevel@tonic-gate 						    "Additional ROM "
7560Sstevel@tonic-gate 						    "checking\n");
7570Sstevel@tonic-gate 					/* Can't write to ROM */
7580Sstevel@tonic-gate 					if (write_flag) {
7590Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
7600Sstevel@tonic-gate 						rval = EIO;
7610Sstevel@tonic-gate 						goto done_reg;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 					/* ROM disabled for reading */
7640Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
7650Sstevel@tonic-gate 						prg.status =
7660Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
7670Sstevel@tonic-gate 						rval = EIO;
7680Sstevel@tonic-gate 						goto done_reg;
7690Sstevel@tonic-gate 					}
7700Sstevel@tonic-gate 				}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 				if (pcitool_debug)
7730Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
7740Sstevel@tonic-gate 			}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 			base_addr = prg2.data;
7790Sstevel@tonic-gate 			if (pcitool_debug)
7800Sstevel@tonic-gate 				prom_printf(
7810Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
7820Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
7830Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
7840Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
7850Sstevel@tonic-gate 			/*
7860Sstevel@tonic-gate 			 * Use offset provided by caller to index into
7870Sstevel@tonic-gate 			 * desired space, then access.
7880Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
7890Sstevel@tonic-gate 			 */
7900Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
7930Sstevel@tonic-gate 			    &num_virt_pages);
7940Sstevel@tonic-gate 			if (virt_addr == NULL) {
7950Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
7960Sstevel@tonic-gate 				rval = EIO;
7970Sstevel@tonic-gate 				goto done_reg;
7980Sstevel@tonic-gate 			}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
8010Sstevel@tonic-gate 			    write_flag);
8020Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate done_reg:
8050Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
8060Sstevel@tonic-gate 		    DDI_SUCCESS) {
8070Sstevel@tonic-gate 			if (pcitool_debug)
8080Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
8090Sstevel@tonic-gate 			rval = EFAULT;
8100Sstevel@tonic-gate 		}
8110Sstevel@tonic-gate 		break;
8120Sstevel@tonic-gate 	default:
8130Sstevel@tonic-gate 		rval = ENOTTY;
8140Sstevel@tonic-gate 		break;
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	return (rval);
8170Sstevel@tonic-gate }
818