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 /*
2212212SZhijun.Fu@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/mkdev.h>
27117Sschwartz #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/sunddi.h>
290Sstevel@tonic-gate #include <vm/seg_kmem.h>
300Sstevel@tonic-gate #include <sys/machparam.h>
31916Sschwartz #include <sys/sunndi.h>
320Sstevel@tonic-gate #include <sys/ontrap.h>
33916Sschwartz #include <sys/psm.h>
34881Sjohnny #include <sys/pcie.h>
350Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
360Sstevel@tonic-gate #include <sys/pci_tools.h>
371083Sanish #include <io/pci/pci_tools_ext.h>
383446Smrj #include <sys/apic.h>
39*12683SJimmy.Vetayases@oracle.com #include <sys/apix.h>
40916Sschwartz #include <io/pci/pci_var.h>
4110923SEvan.Yan@Sun.COM #include <sys/pci_impl.h>
420Sstevel@tonic-gate #include <sys/promif.h>
431083Sanish #include <sys/x86_archext.h>
442434Sanish #include <sys/cpuvar.h>
4511245SZhijun.Fu@Sun.COM #include <sys/pci_cfgacc.h>
460Sstevel@tonic-gate
475084Sjohnlev #ifdef __xpv
485084Sjohnlev #include <sys/hypervisor.h>
495084Sjohnlev #endif
505084Sjohnlev
51777Sschwartz #define PCIEX_BDF_OFFSET_DELTA 4
52777Sschwartz #define PCIEX_REG_FUNC_SHIFT (PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
53777Sschwartz #define PCIEX_REG_DEV_SHIFT (PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
54777Sschwartz #define PCIEX_REG_BUS_SHIFT (PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
55777Sschwartz
560Sstevel@tonic-gate #define SUCCESS 0
570Sstevel@tonic-gate
5811245SZhijun.Fu@Sun.COM extern uint64_t mcfg_mem_base;
590Sstevel@tonic-gate int pcitool_debug = 0;
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * Offsets of BARS in config space. First entry of 0 means config space.
630Sstevel@tonic-gate * Entries here correlate to pcitool_bars_t enumerated type.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate static uint8_t pci_bars[] = {
660Sstevel@tonic-gate 0x0,
670Sstevel@tonic-gate PCI_CONF_BASE0,
680Sstevel@tonic-gate PCI_CONF_BASE1,
690Sstevel@tonic-gate PCI_CONF_BASE2,
700Sstevel@tonic-gate PCI_CONF_BASE3,
710Sstevel@tonic-gate PCI_CONF_BASE4,
720Sstevel@tonic-gate PCI_CONF_BASE5,
730Sstevel@tonic-gate PCI_CONF_ROM
740Sstevel@tonic-gate };
750Sstevel@tonic-gate
76777Sschwartz /* Max offset allowed into config space for a particular device. */
77777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
78777Sschwartz
790Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
8011245SZhijun.Fu@Sun.COM static int pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
8111245SZhijun.Fu@Sun.COM boolean_t io_access);
8211245SZhijun.Fu@Sun.COM static int pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag);
8311245SZhijun.Fu@Sun.COM static int pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr,
840Sstevel@tonic-gate boolean_t write_flag);
850Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
860Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
870Sstevel@tonic-gate
884397Sschwartz /* Extern declarations */
89916Sschwartz extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
90916Sschwartz psm_intr_op_t, int *);
91916Sschwartz
92117Sschwartz int
pcitool_init(dev_info_t * dip,boolean_t is_pciex)93777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
94117Sschwartz {
95117Sschwartz int instance = ddi_get_instance(dip);
96117Sschwartz
97117Sschwartz /* Create pcitool nodes for register access and interrupt routing. */
98117Sschwartz
99117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
10010923SEvan.Yan@Sun.COM PCI_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
101117Sschwartz DDI_NT_REGACC, 0) != DDI_SUCCESS) {
102117Sschwartz return (DDI_FAILURE);
103117Sschwartz }
104117Sschwartz
105117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
10610923SEvan.Yan@Sun.COM PCI_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
107117Sschwartz DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
108117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG);
109117Sschwartz return (DDI_FAILURE);
110117Sschwartz }
111117Sschwartz
112777Sschwartz if (is_pciex)
113777Sschwartz max_cfg_size = PCIE_CONF_HDR_SIZE;
114777Sschwartz
115117Sschwartz return (DDI_SUCCESS);
116117Sschwartz }
117117Sschwartz
118117Sschwartz void
pcitool_uninit(dev_info_t * dip)119117Sschwartz pcitool_uninit(dev_info_t *dip)
120117Sschwartz {
121117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_INTR);
122117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG);
123117Sschwartz }
124117Sschwartz
125916Sschwartz /*ARGSUSED*/
126916Sschwartz static int
pcitool_set_intr(dev_info_t * dip,void * arg,int mode)127916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
128916Sschwartz {
129916Sschwartz ddi_intr_handle_impl_t info_hdl;
130916Sschwartz pcitool_intr_set_t iset;
131916Sschwartz uint32_t old_cpu;
132916Sschwartz int ret, result;
1334397Sschwartz size_t copyinout_size;
134916Sschwartz int rval = SUCCESS;
135*12683SJimmy.Vetayases@oracle.com apic_get_type_t type_info;
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
15910053SEvan.Yan@Sun.COM if (iset.flags & PCITOOL_INTR_FLAG_SET_MSI) {
16010053SEvan.Yan@Sun.COM rval = ENOTSUP;
16110053SEvan.Yan@Sun.COM iset.status = PCITOOL_IO_ERROR;
16210053SEvan.Yan@Sun.COM goto done_set_intr;
16310053SEvan.Yan@Sun.COM }
16410053SEvan.Yan@Sun.COM
165*12683SJimmy.Vetayases@oracle.com info_hdl.ih_private = &type_info;
166*12683SJimmy.Vetayases@oracle.com
167*12683SJimmy.Vetayases@oracle.com if ((*psm_intr_ops)(NULL, &info_hdl,
168*12683SJimmy.Vetayases@oracle.com PSM_INTR_OP_APIC_TYPE, NULL) != PSM_SUCCESS) {
169*12683SJimmy.Vetayases@oracle.com rval = ENOTSUP;
170*12683SJimmy.Vetayases@oracle.com iset.status = PCITOOL_IO_ERROR;
171*12683SJimmy.Vetayases@oracle.com goto done_set_intr;
172*12683SJimmy.Vetayases@oracle.com }
173*12683SJimmy.Vetayases@oracle.com
174*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
175*12683SJimmy.Vetayases@oracle.com if (iset.old_cpu > type_info.avgi_num_cpu) {
176*12683SJimmy.Vetayases@oracle.com rval = EINVAL;
177*12683SJimmy.Vetayases@oracle.com iset.status = PCITOOL_INVALID_CPUID;
178*12683SJimmy.Vetayases@oracle.com goto done_set_intr;
179*12683SJimmy.Vetayases@oracle.com }
180*12683SJimmy.Vetayases@oracle.com old_cpu = iset.old_cpu;
181*12683SJimmy.Vetayases@oracle.com } else {
182*12683SJimmy.Vetayases@oracle.com if ((old_cpu =
183*12683SJimmy.Vetayases@oracle.com pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
184*12683SJimmy.Vetayases@oracle.com iset.status = PCITOOL_IO_ERROR;
185*12683SJimmy.Vetayases@oracle.com rval = EINVAL;
186*12683SJimmy.Vetayases@oracle.com goto done_set_intr;
187*12683SJimmy.Vetayases@oracle.com }
188*12683SJimmy.Vetayases@oracle.com }
189*12683SJimmy.Vetayases@oracle.com
190*12683SJimmy.Vetayases@oracle.com if (iset.ino > type_info.avgi_num_intr) {
191916Sschwartz rval = EINVAL;
192916Sschwartz iset.status = PCITOOL_INVALID_INO;
193916Sschwartz goto done_set_intr;
194916Sschwartz }
195916Sschwartz
196916Sschwartz iset.status = PCITOOL_SUCCESS;
197916Sschwartz
198916Sschwartz old_cpu &= ~PSMGI_CPU_USER_BOUND;
199916Sschwartz
200916Sschwartz /*
201916Sschwartz * For this locally-declared and used handle, ih_private will contain a
202916Sschwartz * CPU value, not an ihdl_plat_t as used for global interrupt handling.
203916Sschwartz */
204*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
205*12683SJimmy.Vetayases@oracle.com info_hdl.ih_vector = APIX_VIRTVECTOR(old_cpu, iset.ino);
206*12683SJimmy.Vetayases@oracle.com } else {
207*12683SJimmy.Vetayases@oracle.com info_hdl.ih_vector = iset.ino;
208*12683SJimmy.Vetayases@oracle.com }
209916Sschwartz info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
21010053SEvan.Yan@Sun.COM info_hdl.ih_flags = PSMGI_INTRBY_VEC;
2114397Sschwartz if (pcitool_debug)
2124397Sschwartz prom_printf("user version:%d, flags:0x%x\n",
2134397Sschwartz iset.user_version, iset.flags);
214916Sschwartz
2154397Sschwartz result = ENOTSUP;
2164397Sschwartz if ((iset.user_version >= PCITOOL_V2) &&
21710053SEvan.Yan@Sun.COM (iset.flags & PCITOOL_INTR_FLAG_SET_GROUP)) {
2184397Sschwartz ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
2194397Sschwartz &result);
2204397Sschwartz } else {
2214397Sschwartz ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
2224397Sschwartz &result);
2234397Sschwartz }
2244397Sschwartz
225916Sschwartz if (ret != PSM_SUCCESS) {
226916Sschwartz switch (result) {
227916Sschwartz case EIO: /* Error making the change */
228916Sschwartz rval = EIO;
229916Sschwartz iset.status = PCITOOL_IO_ERROR;
230916Sschwartz break;
231916Sschwartz case ENXIO: /* Couldn't convert vector to irq */
232916Sschwartz rval = EINVAL;
233916Sschwartz iset.status = PCITOOL_INVALID_INO;
234916Sschwartz break;
235916Sschwartz case EINVAL: /* CPU out of range */
236916Sschwartz rval = EINVAL;
237916Sschwartz iset.status = PCITOOL_INVALID_CPUID;
238916Sschwartz break;
2394397Sschwartz case ENOTSUP: /* Requested PSM intr ops missing */
2404397Sschwartz rval = ENOTSUP;
2414397Sschwartz iset.status = PCITOOL_IO_ERROR;
2424397Sschwartz break;
243916Sschwartz }
244916Sschwartz }
245916Sschwartz
246916Sschwartz /* Return original CPU. */
247916Sschwartz iset.cpu_id = old_cpu;
248916Sschwartz
249*12683SJimmy.Vetayases@oracle.com /* Return new vector */
250*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
251*12683SJimmy.Vetayases@oracle.com iset.ino = APIX_VIRTVEC_VECTOR(info_hdl.ih_vector);
252*12683SJimmy.Vetayases@oracle.com }
253*12683SJimmy.Vetayases@oracle.com
254916Sschwartz done_set_intr:
2554397Sschwartz iset.drvr_version = PCITOOL_VERSION;
2564397Sschwartz if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
257916Sschwartz rval = EFAULT;
258916Sschwartz return (rval);
259916Sschwartz }
260916Sschwartz
261916Sschwartz
262916Sschwartz /* It is assumed that dip != NULL */
263916Sschwartz static void
pcitool_get_intr_dev_info(dev_info_t * dip,pcitool_intr_dev_t * devs)264916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
265916Sschwartz {
266916Sschwartz (void) strncpy(devs->driver_name,
2679537SErwin.Tsaur@Sun.COM ddi_driver_name(dip), MAXMODCONFNAME-2);
2689537SErwin.Tsaur@Sun.COM devs->driver_name[MAXMODCONFNAME-1] = '\0';
269916Sschwartz (void) ddi_pathname(dip, devs->path);
270916Sschwartz devs->dev_inst = ddi_get_instance(dip);
271916Sschwartz }
272916Sschwartz
273916Sschwartz static int
pcitool_get_intr(dev_info_t * dip,void * arg,int mode)274916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
275916Sschwartz {
276916Sschwartz /* Array part isn't used here, but oh well... */
277916Sschwartz pcitool_intr_get_t partial_iget;
278916Sschwartz pcitool_intr_get_t *iget = &partial_iget;
279916Sschwartz size_t iget_kmem_alloc_size = 0;
280916Sschwartz uint8_t num_devs_ret;
281916Sschwartz int copyout_rval;
282916Sschwartz int rval = SUCCESS;
283916Sschwartz int circ;
284916Sschwartz int i;
285916Sschwartz
286916Sschwartz ddi_intr_handle_impl_t info_hdl;
287916Sschwartz apic_get_intr_t intr_info;
288*12683SJimmy.Vetayases@oracle.com apic_get_type_t type_info;
289916Sschwartz
290916Sschwartz /* Read in just the header part, no array section. */
291916Sschwartz if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
292916Sschwartz DDI_SUCCESS)
293916Sschwartz return (EFAULT);
294916Sschwartz
29510053SEvan.Yan@Sun.COM if (partial_iget.flags & PCITOOL_INTR_FLAG_GET_MSI) {
29610053SEvan.Yan@Sun.COM partial_iget.status = PCITOOL_IO_ERROR;
29710053SEvan.Yan@Sun.COM partial_iget.num_devs_ret = 0;
29810053SEvan.Yan@Sun.COM rval = ENOTSUP;
29910053SEvan.Yan@Sun.COM goto done_get_intr;
30010053SEvan.Yan@Sun.COM }
30110053SEvan.Yan@Sun.COM
302*12683SJimmy.Vetayases@oracle.com info_hdl.ih_private = &type_info;
303*12683SJimmy.Vetayases@oracle.com
304*12683SJimmy.Vetayases@oracle.com if ((*psm_intr_ops)(NULL, &info_hdl,
305*12683SJimmy.Vetayases@oracle.com PSM_INTR_OP_APIC_TYPE, NULL) != PSM_SUCCESS) {
306*12683SJimmy.Vetayases@oracle.com iget->status = PCITOOL_IO_ERROR;
307*12683SJimmy.Vetayases@oracle.com iget->num_devs_ret = 0;
308*12683SJimmy.Vetayases@oracle.com rval = EINVAL;
309*12683SJimmy.Vetayases@oracle.com goto done_get_intr;
310*12683SJimmy.Vetayases@oracle.com }
311*12683SJimmy.Vetayases@oracle.com
312*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
313*12683SJimmy.Vetayases@oracle.com if (partial_iget.cpu_id > type_info.avgi_num_cpu) {
314*12683SJimmy.Vetayases@oracle.com partial_iget.status = PCITOOL_INVALID_CPUID;
315*12683SJimmy.Vetayases@oracle.com partial_iget.num_devs_ret = 0;
316*12683SJimmy.Vetayases@oracle.com rval = EINVAL;
317*12683SJimmy.Vetayases@oracle.com goto done_get_intr;
318*12683SJimmy.Vetayases@oracle.com }
319*12683SJimmy.Vetayases@oracle.com }
320*12683SJimmy.Vetayases@oracle.com
321916Sschwartz /* Validate argument. */
322*12683SJimmy.Vetayases@oracle.com if ((partial_iget.ino & APIX_VIRTVEC_VECMASK) >
323*12683SJimmy.Vetayases@oracle.com type_info.avgi_num_intr) {
324916Sschwartz partial_iget.status = PCITOOL_INVALID_INO;
325916Sschwartz partial_iget.num_devs_ret = 0;
326916Sschwartz rval = EINVAL;
327916Sschwartz goto done_get_intr;
328916Sschwartz }
329916Sschwartz
330916Sschwartz num_devs_ret = partial_iget.num_devs_ret;
331916Sschwartz intr_info.avgi_dip_list = NULL;
332916Sschwartz intr_info.avgi_req_flags =
333916Sschwartz PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
334916Sschwartz /*
335916Sschwartz * For this locally-declared and used handle, ih_private will contain a
336916Sschwartz * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
337916Sschwartz * global interrupt handling.
338916Sschwartz */
339916Sschwartz info_hdl.ih_private = &intr_info;
340*12683SJimmy.Vetayases@oracle.com
341*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
342*12683SJimmy.Vetayases@oracle.com info_hdl.ih_vector =
343*12683SJimmy.Vetayases@oracle.com APIX_VIRTVECTOR(partial_iget.cpu_id, partial_iget.ino);
344*12683SJimmy.Vetayases@oracle.com } else {
345*12683SJimmy.Vetayases@oracle.com info_hdl.ih_vector = partial_iget.ino;
346*12683SJimmy.Vetayases@oracle.com }
347916Sschwartz
348916Sschwartz /* Caller wants device information returned. */
349916Sschwartz if (num_devs_ret > 0) {
350916Sschwartz
351916Sschwartz intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
352916Sschwartz
353916Sschwartz /*
354916Sschwartz * Allocate room.
355916Sschwartz * If num_devs_ret == 0 iget remains pointing to partial_iget.
356916Sschwartz */
357916Sschwartz iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
358916Sschwartz iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
359916Sschwartz
360916Sschwartz /* Read in whole structure to verify there's room. */
361916Sschwartz if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
362916Sschwartz SUCCESS) {
363916Sschwartz
364916Sschwartz /* Be consistent and just return EFAULT here. */
365916Sschwartz kmem_free(iget, iget_kmem_alloc_size);
366916Sschwartz
367916Sschwartz return (EFAULT);
368916Sschwartz }
369916Sschwartz }
370916Sschwartz
371916Sschwartz bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
372916Sschwartz iget->ino = info_hdl.ih_vector;
373916Sschwartz
374916Sschwartz /*
375916Sschwartz * Lock device tree branch from the pci root nexus on down if info will
376916Sschwartz * be extracted from dips returned from the tree.
377916Sschwartz */
378916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
379916Sschwartz ndi_devi_enter(dip, &circ);
380916Sschwartz }
381916Sschwartz
382916Sschwartz /* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
383916Sschwartz if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
384916Sschwartz PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
385916Sschwartz iget->status = PCITOOL_IO_ERROR;
386916Sschwartz iget->num_devs_ret = 0;
387916Sschwartz rval = EINVAL;
388916Sschwartz goto done_get_intr;
389916Sschwartz }
390916Sschwartz
391916Sschwartz /*
392916Sschwartz * Fill in the pcitool_intr_get_t to be returned,
393916Sschwartz * with the CPU, num_devs_ret and num_devs.
394916Sschwartz */
39512243SEvan.Yan@Sun.COM if (intr_info.avgi_cpu_id == IRQ_UNBOUND ||
39612243SEvan.Yan@Sun.COM intr_info.avgi_cpu_id == IRQ_UNINIT)
39712243SEvan.Yan@Sun.COM iget->cpu_id = 0;
39812243SEvan.Yan@Sun.COM else
39912243SEvan.Yan@Sun.COM iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
400916Sschwartz
401916Sschwartz /* Number of devices returned by apic. */
402916Sschwartz iget->num_devs = intr_info.avgi_num_devs;
403916Sschwartz
404916Sschwartz /* Device info was returned. */
405916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
406916Sschwartz
407916Sschwartz /*
408916Sschwartz * num devs returned is num devs ret by apic,
409916Sschwartz * space permitting.
410916Sschwartz */
411916Sschwartz iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
412916Sschwartz
413916Sschwartz /*
414916Sschwartz * Loop thru list of dips and extract driver, name and instance.
415916Sschwartz * Fill in the pcitool_intr_dev_t's with this info.
416916Sschwartz */
417916Sschwartz for (i = 0; i < iget->num_devs_ret; i++)
418916Sschwartz pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
419916Sschwartz &iget->dev[i]);
420916Sschwartz
421916Sschwartz /* Free kmem_alloc'ed memory of the apic_get_intr_t */
422916Sschwartz kmem_free(intr_info.avgi_dip_list,
423916Sschwartz intr_info.avgi_num_devs * sizeof (dev_info_t *));
424916Sschwartz }
425916Sschwartz
426916Sschwartz done_get_intr:
427916Sschwartz
428916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
429916Sschwartz ndi_devi_exit(dip, circ);
430916Sschwartz }
431916Sschwartz
4324397Sschwartz iget->drvr_version = PCITOOL_VERSION;
433916Sschwartz copyout_rval = ddi_copyout(iget, arg,
434916Sschwartz PCITOOL_IGET_SIZE(num_devs_ret), mode);
435916Sschwartz
436916Sschwartz if (iget_kmem_alloc_size > 0)
437916Sschwartz kmem_free(iget, iget_kmem_alloc_size);
438916Sschwartz
439916Sschwartz if (copyout_rval != DDI_SUCCESS)
440916Sschwartz rval = EFAULT;
441916Sschwartz
442916Sschwartz return (rval);
443916Sschwartz }
444916Sschwartz
4454397Sschwartz /*ARGSUSED*/
4464397Sschwartz static int
pcitool_intr_info(dev_info_t * dip,void * arg,int mode)4474397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
4484397Sschwartz {
4494397Sschwartz pcitool_intr_info_t intr_info;
4504397Sschwartz ddi_intr_handle_impl_t info_hdl;
4514397Sschwartz int rval = SUCCESS;
452*12683SJimmy.Vetayases@oracle.com apic_get_type_t type_info;
4534397Sschwartz
4544397Sschwartz /* If we need user_version, and to ret same user version as passed in */
4554397Sschwartz if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
4564397Sschwartz DDI_SUCCESS) {
4574397Sschwartz if (pcitool_debug)
4584397Sschwartz prom_printf("Error reading arguments\n");
4594397Sschwartz return (EFAULT);
4604397Sschwartz }
4614397Sschwartz
46210053SEvan.Yan@Sun.COM if (intr_info.flags & PCITOOL_INTR_FLAG_GET_MSI)
46310053SEvan.Yan@Sun.COM return (ENOTSUP);
46410053SEvan.Yan@Sun.COM
465*12683SJimmy.Vetayases@oracle.com info_hdl.ih_private = &type_info;
466*12683SJimmy.Vetayases@oracle.com
4674397Sschwartz /* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
4684397Sschwartz if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
4694397Sschwartz PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
4704397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
4714397Sschwartz intr_info.ctlr_version = 0;
472*12683SJimmy.Vetayases@oracle.com intr_info.num_intr = APIC_MAX_VECTOR;
4734397Sschwartz } else {
4744397Sschwartz intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
475*12683SJimmy.Vetayases@oracle.com intr_info.num_cpu = type_info.avgi_num_cpu;
476*12683SJimmy.Vetayases@oracle.com if (strcmp(type_info.avgi_type,
477*12683SJimmy.Vetayases@oracle.com APIC_PCPLUSMP_NAME) == 0) {
4784397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
479*12683SJimmy.Vetayases@oracle.com intr_info.num_intr = type_info.avgi_num_intr;
480*12683SJimmy.Vetayases@oracle.com } else if (strcmp(type_info.avgi_type,
481*12683SJimmy.Vetayases@oracle.com APIC_APIX_NAME) == 0) {
482*12683SJimmy.Vetayases@oracle.com intr_info.ctlr_type = PCITOOL_CTLR_TYPE_APIX;
483*12683SJimmy.Vetayases@oracle.com intr_info.num_intr = type_info.avgi_num_intr;
484*12683SJimmy.Vetayases@oracle.com } else {
4854397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
486*12683SJimmy.Vetayases@oracle.com intr_info.num_intr = APIC_MAX_VECTOR;
487*12683SJimmy.Vetayases@oracle.com }
4884397Sschwartz }
4894397Sschwartz
4904397Sschwartz intr_info.drvr_version = PCITOOL_VERSION;
4914397Sschwartz if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4924397Sschwartz DDI_SUCCESS) {
4934397Sschwartz if (pcitool_debug)
4944397Sschwartz prom_printf("Error returning arguments.\n");
4954397Sschwartz rval = EFAULT;
4964397Sschwartz }
4974397Sschwartz
4984397Sschwartz return (rval);
4994397Sschwartz }
5004397Sschwartz
5014397Sschwartz
502916Sschwartz
503916Sschwartz /*
504916Sschwartz * Main function for handling interrupt CPU binding requests and queries.
505916Sschwartz * Need to implement later
506916Sschwartz */
507916Sschwartz int
pcitool_intr_admn(dev_info_t * dip,void * arg,int cmd,int mode)508916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
509916Sschwartz {
510916Sschwartz int rval;
511916Sschwartz
512916Sschwartz switch (cmd) {
513916Sschwartz
514916Sschwartz /* Associate a new CPU with a given vector */
515916Sschwartz case PCITOOL_DEVICE_SET_INTR:
516916Sschwartz rval = pcitool_set_intr(dip, arg, mode);
517916Sschwartz break;
518916Sschwartz
519916Sschwartz case PCITOOL_DEVICE_GET_INTR:
520916Sschwartz rval = pcitool_get_intr(dip, arg, mode);
521916Sschwartz break;
522916Sschwartz
5234397Sschwartz case PCITOOL_SYSTEM_INTR_INFO:
5244397Sschwartz rval = pcitool_intr_info(dip, arg, mode);
525916Sschwartz break;
526916Sschwartz
527916Sschwartz default:
528916Sschwartz rval = ENOTSUP;
529916Sschwartz }
530916Sschwartz
531916Sschwartz return (rval);
532916Sschwartz }
533916Sschwartz
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate * Perform register accesses on the nexus device itself.
5360Sstevel@tonic-gate * No explicit PCI nexus device for X86, so not applicable.
5370Sstevel@tonic-gate */
538916Sschwartz
5390Sstevel@tonic-gate /*ARGSUSED*/
5400Sstevel@tonic-gate int
pcitool_bus_reg_ops(dev_info_t * dip,void * arg,int cmd,int mode)541777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate return (ENOTSUP);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate /* Swap endianness. */
5470Sstevel@tonic-gate static uint64_t
pcitool_swap_endian(uint64_t data,int size)5480Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate typedef union {
5510Sstevel@tonic-gate uint64_t data64;
5520Sstevel@tonic-gate uint8_t data8[8];
5530Sstevel@tonic-gate } data_split_t;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate data_split_t orig_data;
5560Sstevel@tonic-gate data_split_t returned_data;
5570Sstevel@tonic-gate int i;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate orig_data.data64 = data;
5600Sstevel@tonic-gate returned_data.data64 = 0;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate for (i = 0; i < size; i++) {
5630Sstevel@tonic-gate returned_data.data8[i] = orig_data.data8[size - 1 - i];
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate return (returned_data.data64);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate
569777Sschwartz /*
57011245SZhijun.Fu@Sun.COM * A note about ontrap handling:
571777Sschwartz *
57211245SZhijun.Fu@Sun.COM * X86 systems on which this module was tested return FFs instead of bus errors
57311245SZhijun.Fu@Sun.COM * when accessing devices with invalid addresses. Ontrap handling, which
57411245SZhijun.Fu@Sun.COM * gracefully handles kernel bus errors, is installed anyway for I/O and mem
57511245SZhijun.Fu@Sun.COM * space accessing (not for pci config space), in case future X86 platforms
57611245SZhijun.Fu@Sun.COM * require it.
577777Sschwartz */
578777Sschwartz
5790Sstevel@tonic-gate /* Access device. prg is modified. */
5800Sstevel@tonic-gate static int
pcitool_cfg_access(pcitool_reg_t * prg,boolean_t write_flag,boolean_t io_access)58111245SZhijun.Fu@Sun.COM pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
58211245SZhijun.Fu@Sun.COM boolean_t io_access)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5850Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5860Sstevel@tonic-gate int rval = SUCCESS;
5870Sstevel@tonic-gate uint64_t local_data;
58811245SZhijun.Fu@Sun.COM pci_cfgacc_req_t req;
58911245SZhijun.Fu@Sun.COM uint32_t max_offset;
59011245SZhijun.Fu@Sun.COM
59111245SZhijun.Fu@Sun.COM if ((size <= 0) || (size > 8) || ((size & (size - 1)) != 0)) {
59211245SZhijun.Fu@Sun.COM prg->status = PCITOOL_INVALID_SIZE;
59311245SZhijun.Fu@Sun.COM return (ENOTSUP);
59411245SZhijun.Fu@Sun.COM }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate /*
5978772SDan.Mick@Sun.COM * NOTE: there is no way to verify whether or not the address is
5988772SDan.Mick@Sun.COM * valid other than that it is within the maximum offset. The
59911245SZhijun.Fu@Sun.COM * put functions return void and the get functions return -1 on error.
6000Sstevel@tonic-gate */
6018772SDan.Mick@Sun.COM
60211245SZhijun.Fu@Sun.COM if (io_access)
60311245SZhijun.Fu@Sun.COM max_offset = 0xFF;
60411245SZhijun.Fu@Sun.COM else
60511245SZhijun.Fu@Sun.COM max_offset = 0xFFF;
60611245SZhijun.Fu@Sun.COM if (prg->offset + size - 1 > max_offset) {
6078772SDan.Mick@Sun.COM prg->status = PCITOOL_INVALID_ADDRESS;
6088772SDan.Mick@Sun.COM return (ENOTSUP);
6098772SDan.Mick@Sun.COM }
6108772SDan.Mick@Sun.COM
6110Sstevel@tonic-gate prg->status = PCITOOL_SUCCESS;
6120Sstevel@tonic-gate
61311245SZhijun.Fu@Sun.COM req.rcdip = NULL;
61411245SZhijun.Fu@Sun.COM req.bdf = PCI_GETBDF(prg->bus_no, prg->dev_no, prg->func_no);
61511245SZhijun.Fu@Sun.COM req.offset = prg->offset;
61611245SZhijun.Fu@Sun.COM req.size = size;
61711245SZhijun.Fu@Sun.COM req.write = write_flag;
61811245SZhijun.Fu@Sun.COM req.ioacc = io_access;
6190Sstevel@tonic-gate if (write_flag) {
6200Sstevel@tonic-gate if (big_endian) {
6210Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size);
6220Sstevel@tonic-gate } else {
6230Sstevel@tonic-gate local_data = prg->data;
6240Sstevel@tonic-gate }
62511245SZhijun.Fu@Sun.COM VAL64(&req) = local_data;
62611245SZhijun.Fu@Sun.COM pci_cfgacc_acc(&req);
6270Sstevel@tonic-gate } else {
62811245SZhijun.Fu@Sun.COM pci_cfgacc_acc(&req);
62912212SZhijun.Fu@Sun.COM switch (size) {
63012212SZhijun.Fu@Sun.COM case 1:
63112212SZhijun.Fu@Sun.COM local_data = VAL8(&req);
63212212SZhijun.Fu@Sun.COM break;
63312212SZhijun.Fu@Sun.COM case 2:
63412212SZhijun.Fu@Sun.COM local_data = VAL16(&req);
63512212SZhijun.Fu@Sun.COM break;
63612212SZhijun.Fu@Sun.COM case 4:
63712212SZhijun.Fu@Sun.COM local_data = VAL32(&req);
63812212SZhijun.Fu@Sun.COM break;
63912212SZhijun.Fu@Sun.COM case 8:
64012212SZhijun.Fu@Sun.COM local_data = VAL64(&req);
64112212SZhijun.Fu@Sun.COM break;
64212212SZhijun.Fu@Sun.COM }
64311245SZhijun.Fu@Sun.COM if (big_endian) {
64411245SZhijun.Fu@Sun.COM prg->data =
64511245SZhijun.Fu@Sun.COM pcitool_swap_endian(local_data, size);
64611245SZhijun.Fu@Sun.COM } else {
64711245SZhijun.Fu@Sun.COM prg->data = local_data;
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate }
65011245SZhijun.Fu@Sun.COM /*
65111245SZhijun.Fu@Sun.COM * Check if legacy IO config access is used, in which case
65211245SZhijun.Fu@Sun.COM * only first 256 bytes are valid.
65311245SZhijun.Fu@Sun.COM */
65411245SZhijun.Fu@Sun.COM if (req.ioacc && (prg->offset + size - 1 > 0xFF)) {
65511245SZhijun.Fu@Sun.COM prg->status = PCITOOL_INVALID_ADDRESS;
65611245SZhijun.Fu@Sun.COM return (ENOTSUP);
65711245SZhijun.Fu@Sun.COM }
65811245SZhijun.Fu@Sun.COM
65911245SZhijun.Fu@Sun.COM /* Set phys_addr only if MMIO is used */
66011245SZhijun.Fu@Sun.COM prg->phys_addr = 0;
66111245SZhijun.Fu@Sun.COM if (!req.ioacc && mcfg_mem_base != 0) {
66211245SZhijun.Fu@Sun.COM prg->phys_addr = mcfg_mem_base + prg->offset +
66311245SZhijun.Fu@Sun.COM ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
66411245SZhijun.Fu@Sun.COM (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
66511245SZhijun.Fu@Sun.COM (prg->func_no << PCIEX_REG_FUNC_SHIFT));
66611245SZhijun.Fu@Sun.COM }
66711245SZhijun.Fu@Sun.COM
6680Sstevel@tonic-gate return (rval);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate static int
pcitool_io_access(pcitool_reg_t * prg,boolean_t write_flag)67211245SZhijun.Fu@Sun.COM pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate int port = (int)prg->phys_addr;
6750Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6760Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6770Sstevel@tonic-gate int rval = SUCCESS;
6780Sstevel@tonic-gate on_trap_data_t otd;
6790Sstevel@tonic-gate uint64_t local_data;
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate * on_trap works like setjmp.
6840Sstevel@tonic-gate *
6850Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error.
6860Sstevel@tonic-gate *
6870Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup.
6880Sstevel@tonic-gate */
6890Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) {
6900Sstevel@tonic-gate no_trap();
6910Sstevel@tonic-gate if (pcitool_debug)
6920Sstevel@tonic-gate prom_printf(
6934397Sschwartz "pcitool_io_access: on_trap caught an error...\n");
6940Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS;
6950Sstevel@tonic-gate return (EFAULT);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate if (write_flag) {
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate if (big_endian) {
7010Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size);
7020Sstevel@tonic-gate } else {
7030Sstevel@tonic-gate local_data = prg->data;
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate if (pcitool_debug)
7070Sstevel@tonic-gate prom_printf("Writing %ld byte(s) to port 0x%x\n",
7080Sstevel@tonic-gate size, port);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate switch (size) {
7110Sstevel@tonic-gate case 1:
7120Sstevel@tonic-gate outb(port, (uint8_t)local_data);
7130Sstevel@tonic-gate break;
7140Sstevel@tonic-gate case 2:
7150Sstevel@tonic-gate outw(port, (uint16_t)local_data);
7160Sstevel@tonic-gate break;
7170Sstevel@tonic-gate case 4:
7180Sstevel@tonic-gate outl(port, (uint32_t)local_data);
7190Sstevel@tonic-gate break;
7200Sstevel@tonic-gate default:
7210Sstevel@tonic-gate rval = ENOTSUP;
7220Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE;
7230Sstevel@tonic-gate break;
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate } else {
7260Sstevel@tonic-gate if (pcitool_debug)
7270Sstevel@tonic-gate prom_printf("Reading %ld byte(s) from port 0x%x\n",
7280Sstevel@tonic-gate size, port);
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate switch (size) {
7310Sstevel@tonic-gate case 1:
7320Sstevel@tonic-gate local_data = inb(port);
7330Sstevel@tonic-gate break;
7340Sstevel@tonic-gate case 2:
7350Sstevel@tonic-gate local_data = inw(port);
7360Sstevel@tonic-gate break;
7370Sstevel@tonic-gate case 4:
7380Sstevel@tonic-gate local_data = inl(port);
7390Sstevel@tonic-gate break;
7400Sstevel@tonic-gate default:
7410Sstevel@tonic-gate rval = ENOTSUP;
7420Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE;
7430Sstevel@tonic-gate break;
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate if (rval == SUCCESS) {
7470Sstevel@tonic-gate if (big_endian) {
7480Sstevel@tonic-gate prg->data =
7490Sstevel@tonic-gate pcitool_swap_endian(local_data, size);
7500Sstevel@tonic-gate } else {
7510Sstevel@tonic-gate prg->data = local_data;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate no_trap();
7570Sstevel@tonic-gate return (rval);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate static int
pcitool_mem_access(pcitool_reg_t * prg,uint64_t virt_addr,boolean_t write_flag)76111245SZhijun.Fu@Sun.COM pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr, boolean_t write_flag)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
7640Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
7650Sstevel@tonic-gate int rval = DDI_SUCCESS;
7660Sstevel@tonic-gate on_trap_data_t otd;
7670Sstevel@tonic-gate uint64_t local_data;
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate * on_trap works like setjmp.
7710Sstevel@tonic-gate *
7720Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error.
7730Sstevel@tonic-gate *
7740Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) {
7770Sstevel@tonic-gate no_trap();
7780Sstevel@tonic-gate if (pcitool_debug)
7790Sstevel@tonic-gate prom_printf(
7800Sstevel@tonic-gate "pcitool_mem_access: on_trap caught an error...\n");
7810Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS;
7820Sstevel@tonic-gate return (EFAULT);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate if (write_flag) {
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate if (big_endian) {
7880Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size);
7890Sstevel@tonic-gate } else {
7900Sstevel@tonic-gate local_data = prg->data;
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate switch (size) {
7940Sstevel@tonic-gate case 1:
7950Sstevel@tonic-gate *((uint8_t *)(uintptr_t)virt_addr) = local_data;
7960Sstevel@tonic-gate break;
7970Sstevel@tonic-gate case 2:
7980Sstevel@tonic-gate *((uint16_t *)(uintptr_t)virt_addr) = local_data;
7990Sstevel@tonic-gate break;
8000Sstevel@tonic-gate case 4:
8010Sstevel@tonic-gate *((uint32_t *)(uintptr_t)virt_addr) = local_data;
8020Sstevel@tonic-gate break;
8030Sstevel@tonic-gate case 8:
8040Sstevel@tonic-gate *((uint64_t *)(uintptr_t)virt_addr) = local_data;
8050Sstevel@tonic-gate break;
8060Sstevel@tonic-gate default:
8070Sstevel@tonic-gate rval = ENOTSUP;
8080Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE;
8090Sstevel@tonic-gate break;
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate } else {
8120Sstevel@tonic-gate switch (size) {
8130Sstevel@tonic-gate case 1:
8140Sstevel@tonic-gate local_data = *((uint8_t *)(uintptr_t)virt_addr);
8150Sstevel@tonic-gate break;
8160Sstevel@tonic-gate case 2:
8170Sstevel@tonic-gate local_data = *((uint16_t *)(uintptr_t)virt_addr);
8180Sstevel@tonic-gate break;
8190Sstevel@tonic-gate case 4:
8200Sstevel@tonic-gate local_data = *((uint32_t *)(uintptr_t)virt_addr);
8210Sstevel@tonic-gate break;
8220Sstevel@tonic-gate case 8:
8230Sstevel@tonic-gate local_data = *((uint64_t *)(uintptr_t)virt_addr);
8240Sstevel@tonic-gate break;
8250Sstevel@tonic-gate default:
8260Sstevel@tonic-gate rval = ENOTSUP;
8270Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE;
8280Sstevel@tonic-gate break;
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate if (rval == SUCCESS) {
8320Sstevel@tonic-gate if (big_endian) {
8330Sstevel@tonic-gate prg->data =
8340Sstevel@tonic-gate pcitool_swap_endian(local_data, size);
8350Sstevel@tonic-gate } else {
8360Sstevel@tonic-gate prg->data = local_data;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate no_trap();
8420Sstevel@tonic-gate return (rval);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate * Map up to 2 pages which contain the address we want to access.
8470Sstevel@tonic-gate *
8480Sstevel@tonic-gate * Mapping should span no more than 8 bytes. With X86 it is possible for an
8490Sstevel@tonic-gate * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
8500Sstevel@tonic-gate * We'll never have to map more than two pages.
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate static uint64_t
pcitool_map(uint64_t phys_addr,size_t size,size_t * num_pages)8540Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
8580Sstevel@tonic-gate uint64_t offset = phys_addr & MMU_PAGEOFFSET;
8590Sstevel@tonic-gate void *virt_base;
8600Sstevel@tonic-gate uint64_t returned_addr;
8613446Smrj pfn_t pfn;
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate if (pcitool_debug)
8640Sstevel@tonic-gate prom_printf("pcitool_map: Called with PA:0x%p\n",
8657632SNick.Todd@Sun.COM (void *)(uintptr_t)phys_addr);
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate *num_pages = 1;
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate /* Desired mapping would span more than two pages. */
8700Sstevel@tonic-gate if ((offset + size) > (MMU_PAGESIZE * 2)) {
8710Sstevel@tonic-gate if (pcitool_debug)
8720Sstevel@tonic-gate prom_printf("boundary violation: "
873777Sschwartz "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
874777Sschwartz offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8750Sstevel@tonic-gate return (NULL);
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate } else if ((offset + size) > MMU_PAGESIZE) {
8780Sstevel@tonic-gate (*num_pages)++;
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate /* Get page(s) of virtual space. */
8820Sstevel@tonic-gate virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8830Sstevel@tonic-gate if (virt_base == NULL) {
8840Sstevel@tonic-gate if (pcitool_debug)
8850Sstevel@tonic-gate prom_printf("Couldn't get virtual base address.\n");
8860Sstevel@tonic-gate return (NULL);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate if (pcitool_debug)
8900Sstevel@tonic-gate prom_printf("Got base virtual address:0x%p\n", virt_base);
8910Sstevel@tonic-gate
8925084Sjohnlev #ifdef __xpv
8935084Sjohnlev /*
8945084Sjohnlev * We should only get here if we are dom0.
8955084Sjohnlev * We're using a real device so we need to translate the MA to a PFN.
8965084Sjohnlev */
8975084Sjohnlev ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
8985084Sjohnlev pfn = xen_assign_pfn(mmu_btop(page_base));
8995084Sjohnlev #else
9003446Smrj pfn = btop(page_base);
9015084Sjohnlev #endif
9023446Smrj
9030Sstevel@tonic-gate /* Now map the allocated virtual space to the physical address. */
9043446Smrj hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
9053446Smrj PROT_READ | PROT_WRITE | HAT_STRICTORDER,
9060Sstevel@tonic-gate HAT_LOAD_LOCK);
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate returned_addr = ((uintptr_t)(virt_base)) + offset;
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate if (pcitool_debug)
9110Sstevel@tonic-gate prom_printf("pcitool_map: returning VA:0x%p\n",
9120Sstevel@tonic-gate (void *)(uintptr_t)returned_addr);
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate return (returned_addr);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate /* Unmap the mapped page(s). */
9180Sstevel@tonic-gate static void
pcitool_unmap(uint64_t virt_addr,size_t num_pages)9190Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
9200Sstevel@tonic-gate {
9210Sstevel@tonic-gate void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
9240Sstevel@tonic-gate HAT_UNLOAD_UNLOCK);
9250Sstevel@tonic-gate vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
93011245SZhijun.Fu@Sun.COM /*ARGSUSED*/
9310Sstevel@tonic-gate int
pcitool_dev_reg_ops(dev_info_t * dip,void * arg,int cmd,int mode)932777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate boolean_t write_flag = B_FALSE;
93511245SZhijun.Fu@Sun.COM boolean_t io_access = B_TRUE;
9360Sstevel@tonic-gate int rval = 0;
9370Sstevel@tonic-gate pcitool_reg_t prg;
9380Sstevel@tonic-gate uint8_t size;
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate uint64_t base_addr;
9410Sstevel@tonic-gate uint64_t virt_addr;
9420Sstevel@tonic-gate size_t num_virt_pages;
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate switch (cmd) {
9450Sstevel@tonic-gate case (PCITOOL_DEVICE_SET_REG):
9460Sstevel@tonic-gate write_flag = B_TRUE;
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate /*FALLTHRU*/
9490Sstevel@tonic-gate case (PCITOOL_DEVICE_GET_REG):
9500Sstevel@tonic-gate if (pcitool_debug)
9510Sstevel@tonic-gate prom_printf("pci_dev_reg_ops set/get reg\n");
9520Sstevel@tonic-gate if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
9530Sstevel@tonic-gate DDI_SUCCESS) {
9540Sstevel@tonic-gate if (pcitool_debug)
9550Sstevel@tonic-gate prom_printf("Error reading arguments\n");
9560Sstevel@tonic-gate return (EFAULT);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
9600Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE;
9610Sstevel@tonic-gate rval = EINVAL;
9620Sstevel@tonic-gate goto done_reg;
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate if (pcitool_debug)
9660Sstevel@tonic-gate prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9670Sstevel@tonic-gate prg.bus_no, prg.dev_no, prg.func_no);
9680Sstevel@tonic-gate /* Validate address arguments of bus / dev / func */
9690Sstevel@tonic-gate if (((prg.bus_no &
9700Sstevel@tonic-gate (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9710Sstevel@tonic-gate prg.bus_no) ||
9720Sstevel@tonic-gate ((prg.dev_no &
9730Sstevel@tonic-gate (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9740Sstevel@tonic-gate prg.dev_no) ||
9750Sstevel@tonic-gate ((prg.func_no &
9760Sstevel@tonic-gate (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9770Sstevel@tonic-gate prg.func_no)) {
9780Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS;
9790Sstevel@tonic-gate rval = EINVAL;
9800Sstevel@tonic-gate goto done_reg;
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate /* Proper config space desired. */
9860Sstevel@tonic-gate if (prg.barnum == 0) {
9870Sstevel@tonic-gate
988777Sschwartz if (pcitool_debug)
989777Sschwartz prom_printf(
990777Sschwartz "config access: offset:0x%" PRIx64 ", "
991777Sschwartz "phys_addr:0x%" PRIx64 "\n",
992777Sschwartz prg.offset, prg.phys_addr);
993777Sschwartz
994777Sschwartz if (prg.offset >= max_cfg_size) {
9950Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE;
9960Sstevel@tonic-gate rval = EINVAL;
9970Sstevel@tonic-gate goto done_reg;
9980Sstevel@tonic-gate }
99911245SZhijun.Fu@Sun.COM if (max_cfg_size == PCIE_CONF_HDR_SIZE)
100011245SZhijun.Fu@Sun.COM io_access = B_FALSE;
10010Sstevel@tonic-gate
100211245SZhijun.Fu@Sun.COM rval = pcitool_cfg_access(&prg, write_flag, io_access);
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 */
103111245SZhijun.Fu@Sun.COM rval = pcitool_cfg_access(&prg2, B_FALSE, B_TRUE);
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
107511245SZhijun.Fu@Sun.COM rval = pcitool_io_access(&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;
111311245SZhijun.Fu@Sun.COM rval = pcitool_cfg_access(&prg2,
111411245SZhijun.Fu@Sun.COM B_FALSE, B_TRUE);
11150Sstevel@tonic-gate if (rval != SUCCESS) {
11160Sstevel@tonic-gate prg.status = prg2.status;
11170Sstevel@tonic-gate goto done_reg;
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate if (prg2.data == 0xffffffff) {
11210Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS;
11220Sstevel@tonic-gate prg.status = EFAULT;
11230Sstevel@tonic-gate goto done_reg;
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate prg2.data = (prg2.data << 32) + low_bytes;
11270Sstevel@tonic-gate if (pcitool_debug)
11280Sstevel@tonic-gate prom_printf(
11290Sstevel@tonic-gate "64 bit mem space. "
11300Sstevel@tonic-gate "64-bit bar is 0x%" PRIx64 "\n",
11310Sstevel@tonic-gate prg2.data);
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate /* Mem32 space, including ROM */
11340Sstevel@tonic-gate } else {
11350Sstevel@tonic-gate
11360Sstevel@tonic-gate if (prg2.offset == PCI_CONF_ROM) {
11370Sstevel@tonic-gate if (pcitool_debug)
11380Sstevel@tonic-gate prom_printf(
11390Sstevel@tonic-gate "Additional ROM "
11400Sstevel@tonic-gate "checking\n");
11410Sstevel@tonic-gate /* Can't write to ROM */
11420Sstevel@tonic-gate if (write_flag) {
11430Sstevel@tonic-gate prg.status = PCITOOL_ROM_WRITE;
11440Sstevel@tonic-gate rval = EIO;
11450Sstevel@tonic-gate goto done_reg;
11460Sstevel@tonic-gate
11470Sstevel@tonic-gate /* ROM disabled for reading */
11480Sstevel@tonic-gate } else if (!(prg2.data & 0x00000001)) {
11490Sstevel@tonic-gate prg.status =
11500Sstevel@tonic-gate PCITOOL_ROM_DISABLED;
11510Sstevel@tonic-gate rval = EIO;
11520Sstevel@tonic-gate goto done_reg;
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate if (pcitool_debug)
11570Sstevel@tonic-gate prom_printf("32 bit mem space\n");
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate
11600Sstevel@tonic-gate /* Common code for all IO/MEM range spaces. */
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate base_addr = prg2.data;
11630Sstevel@tonic-gate if (pcitool_debug)
11640Sstevel@tonic-gate prom_printf(
11650Sstevel@tonic-gate "addr portion of bar is 0x%" PRIx64 ", "
11660Sstevel@tonic-gate "base=0x%" PRIx64 ", "
11670Sstevel@tonic-gate "offset:0x%" PRIx64 "\n",
11680Sstevel@tonic-gate prg2.data, base_addr, prg.offset);
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate * Use offset provided by caller to index into
11710Sstevel@tonic-gate * desired space, then access.
11720Sstevel@tonic-gate * Note that prg.status is modified on error.
11730Sstevel@tonic-gate */
11740Sstevel@tonic-gate prg.phys_addr = base_addr + prg.offset;
11750Sstevel@tonic-gate
11760Sstevel@tonic-gate virt_addr = pcitool_map(prg.phys_addr, size,
11770Sstevel@tonic-gate &num_virt_pages);
11780Sstevel@tonic-gate if (virt_addr == NULL) {
11790Sstevel@tonic-gate prg.status = PCITOOL_IO_ERROR;
11800Sstevel@tonic-gate rval = EIO;
11810Sstevel@tonic-gate goto done_reg;
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
118411245SZhijun.Fu@Sun.COM rval = pcitool_mem_access(&prg, virt_addr, 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