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/sunddi.h>
330Sstevel@tonic-gate #include <vm/seg_kmem.h>
340Sstevel@tonic-gate #include <sys/machparam.h>
350Sstevel@tonic-gate #include <sys/ontrap.h>
36*881Sjohnny #include <sys/pcie.h>
370Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
380Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
390Sstevel@tonic-gate #include <sys/pci_tools.h>
40777Sschwartz #include "pci_tools_ext.h"
410Sstevel@tonic-gate #include "pci_var.h"
420Sstevel@tonic-gate #include <sys/promif.h>
430Sstevel@tonic-gate 
44777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
45777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
46777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
47777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
48777Sschwartz 
490Sstevel@tonic-gate #define	SUCCESS	0
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int pcitool_debug = 0;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
550Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static uint8_t pci_bars[] = {
580Sstevel@tonic-gate 	0x0,
590Sstevel@tonic-gate 	PCI_CONF_BASE0,
600Sstevel@tonic-gate 	PCI_CONF_BASE1,
610Sstevel@tonic-gate 	PCI_CONF_BASE2,
620Sstevel@tonic-gate 	PCI_CONF_BASE3,
630Sstevel@tonic-gate 	PCI_CONF_BASE4,
640Sstevel@tonic-gate 	PCI_CONF_BASE5,
650Sstevel@tonic-gate 	PCI_CONF_ROM
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
68777Sschwartz /* Max offset allowed into config space for a particular device. */
69777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
70777Sschwartz 
710Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
72777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
73777Sschwartz     boolean_t write_flag);
740Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
750Sstevel@tonic-gate     boolean_t write_flag);
760Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg,
770Sstevel@tonic-gate     boolean_t write_flag);
780Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg,
790Sstevel@tonic-gate     uint64_t virt_addr, boolean_t write_flag);
800Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
810Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
820Sstevel@tonic-gate 
83117Sschwartz int
84777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
85117Sschwartz {
86117Sschwartz 	int instance = ddi_get_instance(dip);
87117Sschwartz 
88117Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
89117Sschwartz 
90117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
91117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
92117Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
93117Sschwartz 		return (DDI_FAILURE);
94117Sschwartz 	}
95117Sschwartz 
96117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
97117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
98117Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
99117Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
100117Sschwartz 		return (DDI_FAILURE);
101117Sschwartz 	}
102117Sschwartz 
103777Sschwartz 	if (is_pciex)
104777Sschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
105777Sschwartz 
106117Sschwartz 	return (DDI_SUCCESS);
107117Sschwartz }
108117Sschwartz 
109117Sschwartz void
110117Sschwartz pcitool_uninit(dev_info_t *dip)
111117Sschwartz {
112117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
113117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
114117Sschwartz }
115117Sschwartz 
116117Sschwartz 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * A note about ontrap handling:
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
1210Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
1220Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
1230Sstevel@tonic-gate  * X86 platforms require it.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Main function for handling interrupt CPU binding requests and queries.
1280Sstevel@tonic-gate  * Need to implement later
1290Sstevel@tonic-gate  */
1300Sstevel@tonic-gate /*ARGSUSED*/
1310Sstevel@tonic-gate int
132777Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (ENOTSUP);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
1400Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate /*ARGSUSED*/
1430Sstevel@tonic-gate int
144777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	return (ENOTSUP);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* Swap endianness. */
1500Sstevel@tonic-gate static uint64_t
1510Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	typedef union {
1540Sstevel@tonic-gate 		uint64_t data64;
1550Sstevel@tonic-gate 		uint8_t data8[8];
1560Sstevel@tonic-gate 	} data_split_t;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	data_split_t orig_data;
1590Sstevel@tonic-gate 	data_split_t returned_data;
1600Sstevel@tonic-gate 	int i;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	orig_data.data64 = data;
1630Sstevel@tonic-gate 	returned_data.data64 = 0;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
1660Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	return (returned_data.data64);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
173777Sschwartz /*
174777Sschwartz  * Access device.  prg is modified.
175777Sschwartz  *
176777Sschwartz  * Extended config space is available only through memory-mapped access.
177777Sschwartz  * Standard config space on pci express devices is available either way,
178777Sschwartz  * so do it memory-mapped here too, for simplicity.
179777Sschwartz  */
180777Sschwartz /*ARGSUSED*/
181777Sschwartz static int
182777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
183777Sschwartz     boolean_t write_flag)
184777Sschwartz {
185777Sschwartz 	int rval = SUCCESS;
186777Sschwartz 	uint64_t virt_addr;
187777Sschwartz 	size_t	num_virt_pages;
188777Sschwartz 
189777Sschwartz 	prg->status = PCITOOL_SUCCESS;
190777Sschwartz 
191777Sschwartz 	prg->phys_addr = ddi_prop_get_int64(DDI_DEV_T_ANY, dip, 0,
192*881Sjohnny 	    "ecfga-base-address", 0);
193777Sschwartz 	if (prg->phys_addr == 0) {
194777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
195777Sschwartz 		return (EIO);
196777Sschwartz 	}
197777Sschwartz 
198777Sschwartz 	prg->phys_addr += prg->offset +
199777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
200777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
201777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
202777Sschwartz 
203777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
204777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
205777Sschwartz 	if (virt_addr == NULL) {
206777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
207777Sschwartz 		return (EIO);
208777Sschwartz 	}
209777Sschwartz 
210777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
211777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
212777Sschwartz 	return (rval);
213777Sschwartz }
214777Sschwartz 
2150Sstevel@tonic-gate /* Access device.  prg is modified. */
2160Sstevel@tonic-gate /*ARGSUSED*/
2170Sstevel@tonic-gate static int
2180Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
2210Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
2220Sstevel@tonic-gate 	int rval = SUCCESS;
2230Sstevel@tonic-gate 	uint64_t local_data;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * NOTE: there is no way to verify whether or not the address is valid.
2270Sstevel@tonic-gate 	 * The put functions return void and the get functions return ff on
2280Sstevel@tonic-gate 	 * error.
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (write_flag) {
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		if (big_endian) {
2350Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
2360Sstevel@tonic-gate 		} else {
2370Sstevel@tonic-gate 			local_data = prg->data;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		switch (size) {
2410Sstevel@tonic-gate 		case 1:
2420Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
2430Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2440Sstevel@tonic-gate 			break;
2450Sstevel@tonic-gate 		case 2:
2460Sstevel@tonic-gate 			(*pci_putw_func)(prg->bus_no, prg->dev_no,
2470Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 		case 4:
2500Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
2510Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
2520Sstevel@tonic-gate 			break;
2530Sstevel@tonic-gate 		default:
2540Sstevel@tonic-gate 			rval = ENOTSUP;
2550Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
2560Sstevel@tonic-gate 			break;
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	} else {
2590Sstevel@tonic-gate 		switch (size) {
2600Sstevel@tonic-gate 		case 1:
2610Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
2620Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 		case 2:
2650Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
2660Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2670Sstevel@tonic-gate 			break;
2680Sstevel@tonic-gate 		case 4:
2690Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
2700Sstevel@tonic-gate 			    prg->func_no, prg->offset);
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 		default:
2730Sstevel@tonic-gate 			rval = ENOTSUP;
2740Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 		if (rval == SUCCESS) {
2790Sstevel@tonic-gate 			if (big_endian) {
2800Sstevel@tonic-gate 				prg->data =
2810Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
2820Sstevel@tonic-gate 			} else {
2830Sstevel@tonic-gate 				prg->data = local_data;
2840Sstevel@tonic-gate 			}
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
2880Sstevel@tonic-gate 	return (rval);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*ARGSUSED*/
2930Sstevel@tonic-gate static int
2940Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
2970Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
2980Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
2990Sstevel@tonic-gate 	int rval = SUCCESS;
3000Sstevel@tonic-gate 	on_trap_data_t otd;
3010Sstevel@tonic-gate 	uint64_t local_data;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * on_trap works like setjmp.
3060Sstevel@tonic-gate 	 *
3070Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
3080Sstevel@tonic-gate 	 *
3090Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
3120Sstevel@tonic-gate 		no_trap();
3130Sstevel@tonic-gate 		if (pcitool_debug)
3140Sstevel@tonic-gate 			prom_printf(
3150Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
3160Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
3170Sstevel@tonic-gate 		return (EFAULT);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if (write_flag) {
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		if (big_endian) {
3230Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
3240Sstevel@tonic-gate 		} else {
3250Sstevel@tonic-gate 			local_data = prg->data;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		if (pcitool_debug)
3290Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
3300Sstevel@tonic-gate 			    size, port);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		switch (size) {
3330Sstevel@tonic-gate 		case 1:
3340Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
3350Sstevel@tonic-gate 			break;
3360Sstevel@tonic-gate 		case 2:
3370Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
3380Sstevel@tonic-gate 			break;
3390Sstevel@tonic-gate 		case 4:
3400Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
3410Sstevel@tonic-gate 			break;
3420Sstevel@tonic-gate 		default:
3430Sstevel@tonic-gate 			rval = ENOTSUP;
3440Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
3450Sstevel@tonic-gate 			break;
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 	} else {
3480Sstevel@tonic-gate 		if (pcitool_debug)
3490Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
3500Sstevel@tonic-gate 			    size, port);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		switch (size) {
3530Sstevel@tonic-gate 		case 1:
3540Sstevel@tonic-gate 			local_data = inb(port);
3550Sstevel@tonic-gate 			break;
3560Sstevel@tonic-gate 		case 2:
3570Sstevel@tonic-gate 			local_data = inw(port);
3580Sstevel@tonic-gate 			break;
3590Sstevel@tonic-gate 		case 4:
3600Sstevel@tonic-gate 			local_data = inl(port);
3610Sstevel@tonic-gate 			break;
3620Sstevel@tonic-gate 		default:
3630Sstevel@tonic-gate 			rval = ENOTSUP;
3640Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		if (rval == SUCCESS) {
3690Sstevel@tonic-gate 			if (big_endian) {
3700Sstevel@tonic-gate 				prg->data =
3710Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
3720Sstevel@tonic-gate 			} else {
3730Sstevel@tonic-gate 				prg->data = local_data;
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	no_trap();
3790Sstevel@tonic-gate 	return (rval);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*ARGSUSED*/
3830Sstevel@tonic-gate static int
3840Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
385117Sschwartz 	boolean_t write_flag)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
3880Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
3890Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
3900Sstevel@tonic-gate 	on_trap_data_t otd;
3910Sstevel@tonic-gate 	uint64_t local_data;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * on_trap works like setjmp.
3950Sstevel@tonic-gate 	 *
3960Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
3970Sstevel@tonic-gate 	 *
3980Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
4010Sstevel@tonic-gate 		no_trap();
4020Sstevel@tonic-gate 		if (pcitool_debug)
4030Sstevel@tonic-gate 			prom_printf(
4040Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
4050Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
4060Sstevel@tonic-gate 		return (EFAULT);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (write_flag) {
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		if (big_endian) {
4120Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
4130Sstevel@tonic-gate 		} else {
4140Sstevel@tonic-gate 			local_data = prg->data;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		switch (size) {
4180Sstevel@tonic-gate 		case 1:
4190Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
4200Sstevel@tonic-gate 			break;
4210Sstevel@tonic-gate 		case 2:
4220Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
4230Sstevel@tonic-gate 			break;
4240Sstevel@tonic-gate 		case 4:
4250Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
4260Sstevel@tonic-gate 			break;
4270Sstevel@tonic-gate 		case 8:
4280Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
4290Sstevel@tonic-gate 			break;
4300Sstevel@tonic-gate 		default:
4310Sstevel@tonic-gate 			rval = ENOTSUP;
4320Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 	} else {
4360Sstevel@tonic-gate 		switch (size) {
4370Sstevel@tonic-gate 		case 1:
4380Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
4390Sstevel@tonic-gate 			break;
4400Sstevel@tonic-gate 		case 2:
4410Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 		case 4:
4440Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
4450Sstevel@tonic-gate 			break;
4460Sstevel@tonic-gate 		case 8:
4470Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
4480Sstevel@tonic-gate 			break;
4490Sstevel@tonic-gate 		default:
4500Sstevel@tonic-gate 			rval = ENOTSUP;
4510Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
4520Sstevel@tonic-gate 			break;
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		if (rval == SUCCESS) {
4560Sstevel@tonic-gate 			if (big_endian) {
4570Sstevel@tonic-gate 				prg->data =
4580Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
4590Sstevel@tonic-gate 			} else {
4600Sstevel@tonic-gate 				prg->data = local_data;
4610Sstevel@tonic-gate 			}
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	no_trap();
4660Sstevel@tonic-gate 	return (rval);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
4710Sstevel@tonic-gate  *
4720Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
4730Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
4740Sstevel@tonic-gate  * We'll never have to map more than two pages.
4750Sstevel@tonic-gate  */
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static uint64_t
4780Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
4820Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
4830Sstevel@tonic-gate 	void *virt_base;
4840Sstevel@tonic-gate 	uint64_t returned_addr;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (pcitool_debug)
4870Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
4880Sstevel@tonic-gate 		    (uint8_t *)(uintptr_t)phys_addr);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	*num_pages = 1;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
4930Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
4940Sstevel@tonic-gate 		if (pcitool_debug)
4950Sstevel@tonic-gate 			prom_printf("boundary violation: "
496777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
497777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
4980Sstevel@tonic-gate 		return (NULL);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
5010Sstevel@tonic-gate 		(*num_pages)++;
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
5050Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
5060Sstevel@tonic-gate 	if (virt_base == NULL) {
5070Sstevel@tonic-gate 		if (pcitool_debug)
5080Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
5090Sstevel@tonic-gate 		return (NULL);
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if (pcitool_debug)
5130Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
5160Sstevel@tonic-gate 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages),
5170Sstevel@tonic-gate 	    mmu_btop(page_base), PROT_READ | PROT_WRITE | HAT_STRICTORDER,
5180Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (pcitool_debug)
5230Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
5240Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	return (returned_addr);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate /* Unmap the mapped page(s). */
5300Sstevel@tonic-gate static void
5310Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
5360Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
5370Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
5420Sstevel@tonic-gate int
543777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
5460Sstevel@tonic-gate 	int		rval = 0;
5470Sstevel@tonic-gate 	pcitool_reg_t	prg;
5480Sstevel@tonic-gate 	uint8_t		size;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	uint64_t	base_addr;
5510Sstevel@tonic-gate 	uint64_t	virt_addr;
5520Sstevel@tonic-gate 	size_t		num_virt_pages;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	switch (cmd) {
5550Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
5560Sstevel@tonic-gate 		write_flag = B_TRUE;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*FALLTHRU*/
5590Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
5600Sstevel@tonic-gate 		if (pcitool_debug)
5610Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
5620Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
5630Sstevel@tonic-gate 		    DDI_SUCCESS) {
5640Sstevel@tonic-gate 			if (pcitool_debug)
5650Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
5660Sstevel@tonic-gate 			return (EFAULT);
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
5700Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
5710Sstevel@tonic-gate 			rval = EINVAL;
5720Sstevel@tonic-gate 			goto done_reg;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		if (pcitool_debug)
5760Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
5770Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
5780Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
5790Sstevel@tonic-gate 		if (((prg.bus_no &
5800Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
5810Sstevel@tonic-gate 		    prg.bus_no) ||
5820Sstevel@tonic-gate 		    ((prg.dev_no &
5830Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
5840Sstevel@tonic-gate 		    prg.dev_no) ||
5850Sstevel@tonic-gate 		    ((prg.func_no &
5860Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
5870Sstevel@tonic-gate 		    prg.func_no)) {
5880Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
5890Sstevel@tonic-gate 			rval = EINVAL;
5900Sstevel@tonic-gate 			goto done_reg;
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		/* Proper config space desired. */
5960Sstevel@tonic-gate 		if (prg.barnum == 0) {
5970Sstevel@tonic-gate 
598777Sschwartz 			if (pcitool_debug)
599777Sschwartz 				prom_printf(
600777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
601777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
602777Sschwartz 				    prg.offset, prg.phys_addr);
603777Sschwartz 
604777Sschwartz 			if (prg.offset >= max_cfg_size) {
6050Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
6060Sstevel@tonic-gate 				rval = EINVAL;
6070Sstevel@tonic-gate 				goto done_reg;
6080Sstevel@tonic-gate 			}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			/* Access device.  prg is modified. */
611777Sschwartz 			if (max_cfg_size == PCIE_CONF_HDR_SIZE)
612777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
613777Sschwartz 				    write_flag);
614777Sschwartz 			else
615777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
616777Sschwartz 				    write_flag);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 			if (pcitool_debug)
6190Sstevel@tonic-gate 				prom_printf(
6200Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
6210Sstevel@tonic-gate 				    prg.data);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
6240Sstevel@tonic-gate 		} else {
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 			pcitool_reg_t	prg2;
6270Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 			/*
6300Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
6310Sstevel@tonic-gate 			 * the device's config space.
6320Sstevel@tonic-gate 			 */
6330Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
6340Sstevel@tonic-gate 			prg2.acc_attr =
6350Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 			if (pcitool_debug)
6380Sstevel@tonic-gate 				prom_printf(
6390Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
6400Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
6410Sstevel@tonic-gate 			/*
6420Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
6430Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
6440Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
6450Sstevel@tonic-gate 			 */
6460Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
6470Sstevel@tonic-gate 			if (rval != SUCCESS) {
6480Sstevel@tonic-gate 				if (pcitool_debug)
6490Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
6500Sstevel@tonic-gate 				prg.status = prg2.status;
6510Sstevel@tonic-gate 				goto done_reg;
6520Sstevel@tonic-gate 			}
6530Sstevel@tonic-gate 			/*
6540Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
6550Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
6560Sstevel@tonic-gate 			 * BAR first.
6570Sstevel@tonic-gate 			 */
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 			if (pcitool_debug)
6600Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
6610Sstevel@tonic-gate 				    prg2.data);
6620Sstevel@tonic-gate 			if (!prg2.data) {
6630Sstevel@tonic-gate 				if (pcitool_debug)
6640Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
6650Sstevel@tonic-gate 				rval = EINVAL;
6660Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
6670Sstevel@tonic-gate 				goto done_reg;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
6700Sstevel@tonic-gate 				if (pcitool_debug)
6710Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
6720Sstevel@tonic-gate 				rval = EINVAL;
6730Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
6740Sstevel@tonic-gate 				goto done_reg;
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 			/*
6780Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
6790Sstevel@tonic-gate 			 * this is the ROM address register.
6800Sstevel@tonic-gate 			 */
6810Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
6820Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
6830Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
6840Sstevel@tonic-gate 				if (pcitool_debug)
6850Sstevel@tonic-gate 					prom_printf("IO space\n");
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
6880Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
6910Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
6920Sstevel@tonic-gate 					prom_printf("IO access failed\n");
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 				goto done_reg;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 			/*
6980Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
6990Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
7000Sstevel@tonic-gate 			 *
7010Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
7020Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
7030Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
7040Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
7050Sstevel@tonic-gate 			 */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
7080Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 				uint32_t low_bytes =
7110Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 				/*
7140Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
7150Sstevel@tonic-gate 				 * past the end of BARs.
7160Sstevel@tonic-gate 				 */
7170Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
7180Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
7190Sstevel@tonic-gate 					rval = EIO;
7200Sstevel@tonic-gate 					goto done_reg;
7210Sstevel@tonic-gate 				}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 				/*
7240Sstevel@tonic-gate 				 * Access device.
7250Sstevel@tonic-gate 				 * prg2.status is modified on error.
7260Sstevel@tonic-gate 				 */
7270Sstevel@tonic-gate 				prg2.offset += 4;
7280Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
7290Sstevel@tonic-gate 				if (rval != SUCCESS) {
7300Sstevel@tonic-gate 					prg.status = prg2.status;
7310Sstevel@tonic-gate 					goto done_reg;
7320Sstevel@tonic-gate 				}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
7350Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
7360Sstevel@tonic-gate 					prg.status = EFAULT;
7370Sstevel@tonic-gate 					goto done_reg;
7380Sstevel@tonic-gate 				}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
7410Sstevel@tonic-gate 				if (pcitool_debug)
7420Sstevel@tonic-gate 					prom_printf(
7430Sstevel@tonic-gate 					    "64 bit mem space.  "
7440Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
7450Sstevel@tonic-gate 					    prg2.data);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 			/* Mem32 space, including ROM */
7480Sstevel@tonic-gate 			} else {
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
7510Sstevel@tonic-gate 					if (pcitool_debug)
7520Sstevel@tonic-gate 						prom_printf(
7530Sstevel@tonic-gate 						    "Additional ROM "
7540Sstevel@tonic-gate 						    "checking\n");
7550Sstevel@tonic-gate 					/* Can't write to ROM */
7560Sstevel@tonic-gate 					if (write_flag) {
7570Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
7580Sstevel@tonic-gate 						rval = EIO;
7590Sstevel@tonic-gate 						goto done_reg;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 					/* ROM disabled for reading */
7620Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
7630Sstevel@tonic-gate 						prg.status =
7640Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
7650Sstevel@tonic-gate 						rval = EIO;
7660Sstevel@tonic-gate 						goto done_reg;
7670Sstevel@tonic-gate 					}
7680Sstevel@tonic-gate 				}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 				if (pcitool_debug)
7710Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 			base_addr = prg2.data;
7770Sstevel@tonic-gate 			if (pcitool_debug)
7780Sstevel@tonic-gate 				prom_printf(
7790Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
7800Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
7810Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
7820Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
7830Sstevel@tonic-gate 			/*
7840Sstevel@tonic-gate 			 * Use offset provided by caller to index into
7850Sstevel@tonic-gate 			 * desired space, then access.
7860Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
7870Sstevel@tonic-gate 			 */
7880Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
7910Sstevel@tonic-gate 			    &num_virt_pages);
7920Sstevel@tonic-gate 			if (virt_addr == NULL) {
7930Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
7940Sstevel@tonic-gate 				rval = EIO;
7950Sstevel@tonic-gate 				goto done_reg;
7960Sstevel@tonic-gate 			}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
7990Sstevel@tonic-gate 			    write_flag);
8000Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate done_reg:
8030Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
8040Sstevel@tonic-gate 		    DDI_SUCCESS) {
8050Sstevel@tonic-gate 			if (pcitool_debug)
8060Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
8070Sstevel@tonic-gate 			rval = EFAULT;
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		break;
8100Sstevel@tonic-gate 	default:
8110Sstevel@tonic-gate 		rval = ENOTTY;
8120Sstevel@tonic-gate 		break;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 	return (rval);
8150Sstevel@tonic-gate }
816