xref: /titanic_52/usr/src/uts/sun4v/io/px/px_lib4v.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
30 #include <sys/ddi.h>
31 #include <sys/async.h>
32 #include <sys/sunddi.h>
33 #include <sys/ddifm.h>
34 #include <sys/fm/protocol.h>
35 #include <sys/vmem.h>
36 #include <sys/intr.h>
37 #include <sys/ivintr.h>
38 #include <sys/errno.h>
39 #include <sys/hypervisor_api.h>
40 #include <sys/hsvc.h>
41 #include <px_obj.h>
42 #include <sys/machsystm.h>
43 #include <sys/hotplug/pci/pcihp.h>
44 #include "px_lib4v.h"
45 #include "px_err.h"
46 
47 /* mask for the ranges property in calculating the real PFN range */
48 uint_t px_ranges_phi_mask = ((1 << 28) -1);
49 
50 /*
51  * Hypervisor VPCI services information for the px nexus driver.
52  */
53 static	uint64_t	px_vpci_min_ver; /* Negotiated VPCI API minor version */
54 static	uint_t		px_vpci_users = 0; /* VPCI API users */
55 
56 static hsvc_info_t px_hsvc = {
57 	HSVC_REV_1, NULL, HSVC_GROUP_VPCI, PX_VPCI_MAJOR_VER,
58 	PX_VPCI_MINOR_VER, "PX"
59 };
60 
61 int
62 px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
63 {
64 	px_nexus_regspec_t	*rp;
65 	uint_t			reglen;
66 	int			ret;
67 
68 	uint64_t mjrnum;
69 	uint64_t mnrnum;
70 
71 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p\n", dip);
72 
73 	/*
74 	 * Check HV intr group api versioning.
75 	 * This driver uses the old interrupt routines which are supported
76 	 * in old firmware in the CORE API group and in newer firmware in
77 	 * the INTR API group.  Support for these calls will be dropped
78 	 * once the INTR API group major goes to 2.
79 	 */
80 	if ((hsvc_version(HSVC_GROUP_INTR, &mjrnum, &mnrnum) == 0) &&
81 	    (mjrnum > 1)) {
82 		cmn_err(CE_WARN, "niumx: unsupported intr api group: "
83 		    "maj:0x%lx, min:0x%lx", mjrnum, mnrnum);
84 		return (ENOTSUP);
85 	}
86 
87 	ret = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
88 	    "reg", (uchar_t **)&rp, &reglen);
89 	if (ret != DDI_PROP_SUCCESS) {
90 		DBG(DBG_ATTACH, dip, "px_lib_dev_init failed ret=%d\n", ret);
91 		return (DDI_FAILURE);
92 	}
93 
94 	/*
95 	 * Initilize device handle. The device handle uniquely identifies
96 	 * a SUN4V device. It consists of the lower 28-bits of the hi-cell
97 	 * of the first entry of the SUN4V device's "reg" property as
98 	 * defined by the SUN4V Bus Binding to Open Firmware.
99 	 */
100 	*dev_hdl = (devhandle_t)((rp->phys_addr >> 32) & DEVHDLE_MASK);
101 	ddi_prop_free(rp);
102 
103 	/*
104 	 * hotplug implementation requires this property to be associated with
105 	 * any indirect PCI config access services
106 	 */
107 	(void) ddi_prop_update_int(makedevice(ddi_driver_major(dip),
108 	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR)), dip,
109 	    PCI_BUS_CONF_MAP_PROP, 1);
110 
111 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
112 
113 	/*
114 	 * Negotiate the API version for VPCI hypervisor services.
115 	 */
116 	if (px_vpci_users++)
117 		return (DDI_SUCCESS);
118 
119 	if ((ret = hsvc_register(&px_hsvc, &px_vpci_min_ver)) != 0) {
120 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
121 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d\n",
122 		    px_hsvc.hsvc_modname, px_hsvc.hsvc_group,
123 		    px_hsvc.hsvc_major, px_hsvc.hsvc_minor, ret);
124 
125 		return (DDI_FAILURE);
126 	}
127 
128 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: negotiated VPCI API version, "
129 	    "major 0x%lx minor 0x%lx\n", px_hsvc.hsvc_major, px_vpci_min_ver);
130 
131 	return (DDI_SUCCESS);
132 }
133 
134 /*ARGSUSED*/
135 int
136 px_lib_dev_fini(dev_info_t *dip)
137 {
138 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
139 
140 	(void) ddi_prop_remove(makedevice(ddi_driver_major(dip),
141 	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR)), dip,
142 	    PCI_BUS_CONF_MAP_PROP);
143 
144 	if (--px_vpci_users == 0)
145 		(void) hsvc_unregister(&px_hsvc);
146 
147 	return (DDI_SUCCESS);
148 }
149 
150 /*ARGSUSED*/
151 int
152 px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
153     sysino_t *sysino)
154 {
155 	uint64_t	ret;
156 
157 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
158 	    "devino 0x%x\n", dip, devino);
159 
160 	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
161 	    devino, sysino)) != H_EOK) {
162 		DBG(DBG_LIB_INT, dip,
163 		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
164 		return (DDI_FAILURE);
165 	}
166 
167 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
168 	    *sysino);
169 
170 	return (DDI_SUCCESS);
171 }
172 
173 /*ARGSUSED*/
174 int
175 px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
176     intr_valid_state_t *intr_valid_state)
177 {
178 	uint64_t	ret;
179 
180 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
181 	    dip, sysino);
182 
183 	if ((ret = hvio_intr_getvalid(sysino,
184 	    (int *)intr_valid_state)) != H_EOK) {
185 		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
186 		    ret);
187 		return (DDI_FAILURE);
188 	}
189 
190 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
191 	    *intr_valid_state);
192 
193 	return (DDI_SUCCESS);
194 }
195 
196 /*ARGSUSED*/
197 int
198 px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
199     intr_valid_state_t intr_valid_state)
200 {
201 	uint64_t	ret;
202 
203 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
204 	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);
205 
206 	if ((ret = hvio_intr_setvalid(sysino, intr_valid_state)) != H_EOK) {
207 		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
208 		    ret);
209 		return (DDI_FAILURE);
210 	}
211 
212 	return (DDI_SUCCESS);
213 }
214 
215 /*ARGSUSED*/
216 int
217 px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
218     intr_state_t *intr_state)
219 {
220 	uint64_t	ret;
221 
222 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
223 	    dip, sysino);
224 
225 	if ((ret = hvio_intr_getstate(sysino, (int *)intr_state)) != H_EOK) {
226 		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
227 		    ret);
228 		return (DDI_FAILURE);
229 	}
230 
231 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
232 	    *intr_state);
233 
234 	return (DDI_SUCCESS);
235 }
236 
237 /*ARGSUSED*/
238 int
239 px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
240     intr_state_t intr_state)
241 {
242 	uint64_t	ret;
243 
244 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
245 	    "intr_state 0x%x\n", dip, sysino, intr_state);
246 
247 	if ((ret = hvio_intr_setstate(sysino, intr_state)) != H_EOK) {
248 		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
249 		    ret);
250 		return (DDI_FAILURE);
251 	}
252 
253 	return (DDI_SUCCESS);
254 }
255 
256 /*ARGSUSED*/
257 int
258 px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
259 {
260 	uint64_t	ret;
261 
262 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
263 	    dip, sysino);
264 
265 	if ((ret = hvio_intr_gettarget(sysino, cpuid)) != H_EOK) {
266 		DBG(DBG_LIB_INT, dip,
267 		    "hvio_intr_gettarget failed, ret 0x%lx\n", ret);
268 		return (DDI_FAILURE);
269 	}
270 
271 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", cpuid);
272 
273 	return (DDI_SUCCESS);
274 }
275 
276 /*ARGSUSED*/
277 int
278 px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
279 {
280 	uint64_t	ret;
281 
282 	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
283 	    "cpuid 0x%x\n", dip, sysino, cpuid);
284 
285 	if ((ret = hvio_intr_settarget(sysino, cpuid)) != H_EOK) {
286 		DBG(DBG_LIB_INT, dip,
287 		    "hvio_intr_settarget failed, ret 0x%lx\n", ret);
288 		return (DDI_FAILURE);
289 	}
290 
291 	return (DDI_SUCCESS);
292 }
293 
294 /*ARGSUSED*/
295 int
296 px_lib_intr_reset(dev_info_t *dip)
297 {
298 	px_t		*px_p = DIP_TO_STATE(dip);
299 	px_ib_t		*ib_p = px_p->px_ib_p;
300 	px_ino_t	*ino_p;
301 
302 	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);
303 
304 	mutex_enter(&ib_p->ib_ino_lst_mutex);
305 
306 	/* Reset all Interrupts */
307 	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
308 		if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
309 		    INTR_IDLE_STATE) != DDI_SUCCESS)
310 			return (BF_FATAL);
311 	}
312 
313 	mutex_exit(&ib_p->ib_ino_lst_mutex);
314 
315 	return (BF_NONE);
316 }
317 
318 /*ARGSUSED*/
319 int
320 px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
321     io_attributes_t attr, void *addr, size_t pfn_index, int flags)
322 {
323 	tsbnum_t	tsb_num = PCI_TSBID_TO_TSBNUM(tsbid);
324 	tsbindex_t	tsb_index = PCI_TSBID_TO_TSBINDEX(tsbid);
325 	io_page_list_t	*pfns, *pfn_p;
326 	pages_t		ttes_mapped = 0;
327 	int		i, err = DDI_SUCCESS;
328 
329 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
330 	    "pages 0x%x attr 0x%x addr 0x%p pfn_index 0x%llx flags 0x%x\n",
331 	    dip, tsbid, pages, attr, addr, pfn_index, flags);
332 
333 	if ((pfns = pfn_p = kmem_zalloc((pages * sizeof (io_page_list_t)),
334 	    KM_NOSLEEP)) == NULL) {
335 		DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: kmem_zalloc failed\n");
336 		return (DDI_FAILURE);
337 	}
338 
339 	for (i = 0; i < pages; i++)
340 		pfns[i] = MMU_PTOB(PX_ADDR2PFN(addr, pfn_index, flags, i));
341 
342 	/*
343 	 * If HV VPCI version is 1.1 and higher, pass BDF, phantom function,
344 	 * and relaxed ordering attributes. Otherwise, pass only read or write
345 	 * attribute.
346 	 */
347 	if (px_vpci_min_ver == PX_VPCI_MINOR_VER_0)
348 		attr = attr & (PCI_MAP_ATTR_READ | PCI_MAP_ATTR_WRITE);
349 
350 	while ((ttes_mapped = pfn_p - pfns) < pages) {
351 		uintptr_t	ra = va_to_pa(pfn_p);
352 		pages_t		ttes2map;
353 		uint64_t	ret;
354 
355 		ttes2map = (MMU_PAGE_SIZE - P2PHASE(ra, MMU_PAGE_SIZE)) >> 3;
356 		ra = MMU_PTOB(MMU_BTOP(ra));
357 
358 		for (ttes2map = MIN(ttes2map, pages - ttes_mapped); ttes2map;
359 		    ttes2map -= ttes_mapped, pfn_p += ttes_mapped) {
360 
361 			ttes_mapped = 0;
362 			if ((ret = hvio_iommu_map(DIP_TO_HANDLE(dip),
363 			    PCI_TSBID(tsb_num, tsb_index + (pfn_p - pfns)),
364 			    ttes2map, attr, (io_page_list_t *)(ra |
365 			    ((uintptr_t)pfn_p & MMU_PAGE_OFFSET)),
366 			    &ttes_mapped)) != H_EOK) {
367 				DBG(DBG_LIB_DMA, dip, "hvio_iommu_map failed "
368 				    "ret 0x%lx\n", ret);
369 
370 				ttes_mapped = pfn_p - pfns;
371 				err = DDI_FAILURE;
372 				goto cleanup;
373 			}
374 
375 			DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: tsb_num 0x%x "
376 			    "tsb_index 0x%lx ttes_to_map 0x%lx attr 0x%x "
377 			    "ra 0x%p ttes_mapped 0x%x\n", tsb_num,
378 			    tsb_index + (pfn_p - pfns), ttes2map, attr,
379 			    ra | ((uintptr_t)pfn_p & MMU_PAGE_OFFSET),
380 			    ttes_mapped);
381 		}
382 	}
383 
384 cleanup:
385 	if ((err == DDI_FAILURE) && ttes_mapped)
386 		(void) px_lib_iommu_demap(dip, tsbid, ttes_mapped);
387 
388 	kmem_free(pfns, pages * sizeof (io_page_list_t));
389 	return (err);
390 }
391 
392 /*ARGSUSED*/
393 int
394 px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
395 {
396 	tsbnum_t	tsb_num = PCI_TSBID_TO_TSBNUM(tsbid);
397 	tsbindex_t	tsb_index = PCI_TSBID_TO_TSBINDEX(tsbid);
398 	pages_t		ttes2demap, ttes_demapped = 0;
399 	uint64_t	ret;
400 
401 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
402 	    "pages 0x%x\n", dip, tsbid, pages);
403 
404 	for (ttes2demap = pages; ttes2demap;
405 	    ttes2demap -= ttes_demapped, tsb_index += ttes_demapped) {
406 		if ((ret = hvio_iommu_demap(DIP_TO_HANDLE(dip),
407 		    PCI_TSBID(tsb_num, tsb_index), ttes2demap,
408 		    &ttes_demapped)) != H_EOK) {
409 			DBG(DBG_LIB_DMA, dip, "hvio_iommu_demap failed, "
410 			    "ret 0x%lx\n", ret);
411 
412 			return (DDI_FAILURE);
413 		}
414 
415 		DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: tsb_num 0x%x "
416 		    "tsb_index 0x%lx ttes_to_demap 0x%lx ttes_demapped 0x%x\n",
417 		    tsb_num, tsb_index, ttes2demap, ttes_demapped);
418 	}
419 
420 	return (DDI_SUCCESS);
421 }
422 
423 /*ARGSUSED*/
424 int
425 px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid, io_attributes_t *attr_p,
426     r_addr_t *r_addr_p)
427 {
428 	uint64_t	ret;
429 
430 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
431 	    dip, tsbid);
432 
433 	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), tsbid,
434 	    attr_p, r_addr_p)) != H_EOK) {
435 		DBG(DBG_LIB_DMA, dip,
436 		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);
437 
438 		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
439 	}
440 
441 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%x r_addr 0x%llx\n",
442 	    *attr_p, *r_addr_p);
443 
444 	return (DDI_SUCCESS);
445 }
446 
447 /*ARGSUSED*/
448 uint64_t
449 px_get_rng_parent_hi_mask(px_t *px_p)
450 {
451 	return (PX_RANGE_PROP_MASK);
452 }
453 
454 /*
455  * Checks dma attributes against system bypass ranges
456  * A sun4v device must be capable of generating the entire 64-bit
457  * address in order to perform bypass DMA.
458  */
459 /*ARGSUSED*/
460 int
461 px_lib_dma_bypass_rngchk(dev_info_t *dip, ddi_dma_attr_t *attr_p,
462     uint64_t *lo_p, uint64_t *hi_p)
463 {
464 	if ((attr_p->dma_attr_addr_lo != 0ull) ||
465 	    (attr_p->dma_attr_addr_hi != UINT64_MAX)) {
466 
467 		return (DDI_DMA_BADATTR);
468 	}
469 
470 	*lo_p = 0ull;
471 	*hi_p = UINT64_MAX;
472 
473 	return (DDI_SUCCESS);
474 }
475 
476 
477 /*ARGSUSED*/
478 int
479 px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra, io_attributes_t attr,
480     io_addr_t *io_addr_p)
481 {
482 	uint64_t	ret;
483 
484 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
485 	    "attr 0x%x\n", dip, ra, attr);
486 
487 	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), ra,
488 	    attr, io_addr_p)) != H_EOK) {
489 		DBG(DBG_LIB_DMA, dip,
490 		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
491 		return (ret == H_ENOTSUPPORTED ? DDI_ENOTSUP : DDI_FAILURE);
492 	}
493 
494 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
495 	    *io_addr_p);
496 
497 	return (DDI_SUCCESS);
498 }
499 
500 /*ARGSUSED*/
501 int
502 px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
503 	off_t off, size_t len, uint_t cache_flags)
504 {
505 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
506 	uint64_t sync_dir;
507 	size_t bytes_synced;
508 	int end, idx;
509 	off_t pg_off;
510 	devhandle_t hdl = DIP_TO_HANDLE(dip); /* need to cache hdl */
511 
512 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
513 	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
514 	    dip, rdip, handle, off, len, cache_flags);
515 
516 	if (!(mp->dmai_flags & PX_DMAI_FLAGS_INUSE)) {
517 		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
518 		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
519 		return (DDI_FAILURE);
520 	}
521 
522 	if (mp->dmai_flags & PX_DMAI_FLAGS_NOSYNC)
523 		return (DDI_SUCCESS);
524 
525 	if (!len)
526 		len = mp->dmai_size;
527 
528 	if (mp->dmai_rflags & DDI_DMA_READ)
529 		sync_dir = HVIO_DMA_SYNC_DIR_FROM_DEV;
530 	else
531 		sync_dir = HVIO_DMA_SYNC_DIR_TO_DEV;
532 
533 	off += mp->dmai_offset;
534 	pg_off = off & MMU_PAGEOFFSET;
535 
536 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: page offset %x size %x\n",
537 	    pg_off, len);
538 
539 	/* sync on page basis */
540 	end = MMU_BTOPR(off + len - 1);
541 	for (idx = MMU_BTOP(off); idx < end; idx++,
542 	    len -= bytes_synced, pg_off = 0) {
543 		size_t bytes_to_sync = bytes_to_sync =
544 		    MIN(len, MMU_PAGESIZE - pg_off);
545 
546 		if (hvio_dma_sync(hdl, MMU_PTOB(PX_GET_MP_PFN(mp, idx)) +
547 		    pg_off, bytes_to_sync, sync_dir, &bytes_synced) != H_EOK)
548 			break;
549 
550 		DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: Called hvio_dma_sync "
551 		    "ra = %p bytes to sync = %x bytes synced %x\n",
552 		    MMU_PTOB(PX_GET_MP_PFN(mp, idx)) + pg_off, bytes_to_sync,
553 		    bytes_synced);
554 
555 		if (bytes_to_sync != bytes_synced)
556 			break;
557 	}
558 
559 	return (len ? DDI_FAILURE : DDI_SUCCESS);
560 }
561 
562 
563 /*
564  * MSIQ Functions:
565  */
566 
567 /*ARGSUSED*/
568 int
569 px_lib_msiq_init(dev_info_t *dip)
570 {
571 	px_t		*px_p = DIP_TO_STATE(dip);
572 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
573 	r_addr_t	ra;
574 	size_t		msiq_size;
575 	uint_t		rec_cnt;
576 	int		i, err = DDI_SUCCESS;
577 	uint64_t	ret;
578 
579 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
580 
581 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
582 
583 	for (i = 0; i < msiq_state_p->msiq_cnt; i++) {
584 		ra = (r_addr_t)va_to_pa((caddr_t)msiq_state_p->msiq_buf_p +
585 		    (i * msiq_size));
586 
587 		if ((ret = hvio_msiq_conf(DIP_TO_HANDLE(dip),
588 		    (i + msiq_state_p->msiq_1st_msiq_id),
589 		    ra, msiq_state_p->msiq_rec_cnt)) != H_EOK) {
590 			DBG(DBG_LIB_MSIQ, dip,
591 			    "hvio_msiq_conf failed, ret 0x%lx\n", ret);
592 			err = DDI_FAILURE;
593 			break;
594 		}
595 
596 		if ((err = px_lib_msiq_info(dip,
597 		    (i + msiq_state_p->msiq_1st_msiq_id),
598 		    &ra, &rec_cnt)) != DDI_SUCCESS) {
599 			DBG(DBG_LIB_MSIQ, dip,
600 			    "px_lib_msiq_info failed, ret 0x%x\n", err);
601 			err = DDI_FAILURE;
602 			break;
603 		}
604 
605 		DBG(DBG_LIB_MSIQ, dip,
606 		    "px_lib_msiq_init: ra 0x%p rec_cnt 0x%x\n", ra, rec_cnt);
607 	}
608 
609 	return (err);
610 }
611 
612 /*ARGSUSED*/
613 int
614 px_lib_msiq_fini(dev_info_t *dip)
615 {
616 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
617 
618 	return (DDI_SUCCESS);
619 }
620 
621 /*ARGSUSED*/
622 int
623 px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
624     uint_t *msiq_rec_cnt_p)
625 {
626 	uint64_t	ret;
627 
628 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
629 	    dip, msiq_id);
630 
631 	if ((ret = hvio_msiq_info(DIP_TO_HANDLE(dip),
632 	    msiq_id, ra_p, msiq_rec_cnt_p)) != H_EOK) {
633 		DBG(DBG_LIB_MSIQ, dip,
634 		    "hvio_msiq_info failed, ret 0x%lx\n", ret);
635 		return (DDI_FAILURE);
636 	}
637 
638 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
639 	    ra_p, *msiq_rec_cnt_p);
640 
641 	return (DDI_SUCCESS);
642 }
643 
644 /*ARGSUSED*/
645 int
646 px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
647     pci_msiq_valid_state_t *msiq_valid_state)
648 {
649 	uint64_t	ret;
650 
651 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
652 	    dip, msiq_id);
653 
654 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
655 	    msiq_id, msiq_valid_state)) != H_EOK) {
656 		DBG(DBG_LIB_MSIQ, dip,
657 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
658 		return (DDI_FAILURE);
659 	}
660 
661 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
662 	    *msiq_valid_state);
663 
664 	return (DDI_SUCCESS);
665 }
666 
667 /*ARGSUSED*/
668 int
669 px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
670     pci_msiq_valid_state_t msiq_valid_state)
671 {
672 	uint64_t	ret;
673 
674 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
675 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
676 
677 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
678 	    msiq_id, msiq_valid_state)) != H_EOK) {
679 		DBG(DBG_LIB_MSIQ, dip,
680 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
681 		return (DDI_FAILURE);
682 	}
683 
684 	return (DDI_SUCCESS);
685 }
686 
687 /*ARGSUSED*/
688 int
689 px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
690     pci_msiq_state_t *msiq_state)
691 {
692 	uint64_t	ret;
693 
694 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
695 	    dip, msiq_id);
696 
697 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
698 	    msiq_id, msiq_state)) != H_EOK) {
699 		DBG(DBG_LIB_MSIQ, dip,
700 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
701 		return (DDI_FAILURE);
702 	}
703 
704 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
705 	    *msiq_state);
706 
707 	return (DDI_SUCCESS);
708 }
709 
710 /*ARGSUSED*/
711 int
712 px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
713     pci_msiq_state_t msiq_state)
714 {
715 	uint64_t	ret;
716 
717 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
718 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
719 
720 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
721 	    msiq_id, msiq_state)) != H_EOK) {
722 		DBG(DBG_LIB_MSIQ, dip,
723 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
724 		return (DDI_FAILURE);
725 	}
726 
727 	return (DDI_SUCCESS);
728 }
729 
730 /*ARGSUSED*/
731 int
732 px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
733     msiqhead_t *msiq_head_p)
734 {
735 	uint64_t	ret;
736 
737 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
738 	    dip, msiq_id);
739 
740 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
741 	    msiq_id, msiq_head_p)) != H_EOK) {
742 		DBG(DBG_LIB_MSIQ, dip,
743 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
744 		return (DDI_FAILURE);
745 	}
746 
747 	*msiq_head_p =  (*msiq_head_p / sizeof (msiq_rec_t));
748 
749 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_gethead: msiq_head 0x%x\n",
750 	    *msiq_head_p);
751 
752 	return (DDI_SUCCESS);
753 }
754 
755 /*ARGSUSED*/
756 int
757 px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
758     msiqhead_t msiq_head)
759 {
760 	uint64_t	ret;
761 
762 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
763 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
764 
765 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
766 	    msiq_id, msiq_head * sizeof (msiq_rec_t))) != H_EOK) {
767 		DBG(DBG_LIB_MSIQ, dip,
768 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
769 		return (DDI_FAILURE);
770 	}
771 
772 	return (DDI_SUCCESS);
773 }
774 
775 /*ARGSUSED*/
776 int
777 px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
778     msiqtail_t *msiq_tail_p)
779 {
780 	uint64_t	ret;
781 
782 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
783 	    dip, msiq_id);
784 
785 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
786 	    msiq_id, msiq_tail_p)) != H_EOK) {
787 		DBG(DBG_LIB_MSIQ, dip,
788 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
789 		return (DDI_FAILURE);
790 	}
791 
792 	*msiq_tail_p =  (*msiq_tail_p / sizeof (msiq_rec_t));
793 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
794 	    *msiq_tail_p);
795 
796 	return (DDI_SUCCESS);
797 }
798 
799 /*ARGSUSED*/
800 void
801 px_lib_get_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p,
802     msiq_rec_t *msiq_rec_p)
803 {
804 	msiq_rec_t	*curr_msiq_rec_p = (msiq_rec_t *)msiq_head_p;
805 
806 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p\n", dip);
807 
808 	if (!curr_msiq_rec_p->msiq_rec_type) {
809 		/* Set msiq_rec_type to zero */
810 		msiq_rec_p->msiq_rec_type = 0;
811 
812 		return;
813 	}
814 
815 	*msiq_rec_p = *curr_msiq_rec_p;
816 }
817 
818 /*ARGSUSED*/
819 void
820 px_lib_clr_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p)
821 {
822 	msiq_rec_t	*curr_msiq_rec_p = (msiq_rec_t *)msiq_head_p;
823 
824 	DBG(DBG_LIB_MSIQ, dip, "px_lib_clr_msiq_rec: dip 0x%p\n", dip);
825 
826 	/* Zero out msiq_rec_type field */
827 	curr_msiq_rec_p->msiq_rec_type  = 0;
828 }
829 
830 /*
831  * MSI Functions:
832  */
833 
834 /*ARGSUSED*/
835 int
836 px_lib_msi_init(dev_info_t *dip)
837 {
838 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
839 
840 	/* Noop */
841 	return (DDI_SUCCESS);
842 }
843 
844 /*ARGSUSED*/
845 int
846 px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
847     msiqid_t *msiq_id)
848 {
849 	uint64_t	ret;
850 
851 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
852 	    dip, msi_num);
853 
854 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
855 	    msi_num, msiq_id)) != H_EOK) {
856 		DBG(DBG_LIB_MSI, dip,
857 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
858 		return (DDI_FAILURE);
859 	}
860 
861 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
862 	    *msiq_id);
863 
864 	return (DDI_SUCCESS);
865 }
866 
867 /*ARGSUSED*/
868 int
869 px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
870     msiqid_t msiq_id, msi_type_t msitype)
871 {
872 	uint64_t	ret;
873 
874 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
875 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
876 
877 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
878 	    msi_num, msiq_id, msitype)) != H_EOK) {
879 		DBG(DBG_LIB_MSI, dip,
880 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
881 		return (DDI_FAILURE);
882 	}
883 
884 	return (DDI_SUCCESS);
885 }
886 
887 /*ARGSUSED*/
888 int
889 px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
890     pci_msi_valid_state_t *msi_valid_state)
891 {
892 	uint64_t	ret;
893 
894 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
895 	    dip, msi_num);
896 
897 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
898 	    msi_num, msi_valid_state)) != H_EOK) {
899 		DBG(DBG_LIB_MSI, dip,
900 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
901 		return (DDI_FAILURE);
902 	}
903 
904 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
905 	    *msi_valid_state);
906 
907 	return (DDI_SUCCESS);
908 }
909 
910 /*ARGSUSED*/
911 int
912 px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
913     pci_msi_valid_state_t msi_valid_state)
914 {
915 	uint64_t	ret;
916 
917 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
918 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
919 
920 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
921 	    msi_num, msi_valid_state)) != H_EOK) {
922 		DBG(DBG_LIB_MSI, dip,
923 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
924 		return (DDI_FAILURE);
925 	}
926 
927 	return (DDI_SUCCESS);
928 }
929 
930 /*ARGSUSED*/
931 int
932 px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
933     pci_msi_state_t *msi_state)
934 {
935 	uint64_t	ret;
936 
937 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
938 	    dip, msi_num);
939 
940 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
941 	    msi_num, msi_state)) != H_EOK) {
942 		DBG(DBG_LIB_MSI, dip,
943 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
944 		return (DDI_FAILURE);
945 	}
946 
947 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
948 	    *msi_state);
949 
950 	return (DDI_SUCCESS);
951 }
952 
953 /*ARGSUSED*/
954 int
955 px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
956     pci_msi_state_t msi_state)
957 {
958 	uint64_t	ret;
959 
960 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
961 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
962 
963 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
964 	    msi_num, msi_state)) != H_EOK) {
965 		DBG(DBG_LIB_MSI, dip,
966 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
967 		return (DDI_FAILURE);
968 	}
969 
970 	return (DDI_SUCCESS);
971 }
972 
973 /*
974  * MSG Functions:
975  */
976 
977 /*ARGSUSED*/
978 int
979 px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
980     msiqid_t *msiq_id)
981 {
982 	uint64_t	ret;
983 
984 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
985 	    dip, msg_type);
986 
987 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
988 	    msg_type, msiq_id)) != H_EOK) {
989 		DBG(DBG_LIB_MSG, dip,
990 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
991 		return (DDI_FAILURE);
992 	}
993 
994 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
995 	    *msiq_id);
996 
997 	return (DDI_SUCCESS);
998 }
999 
1000 /*ARGSUSED*/
1001 int
1002 px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
1003     msiqid_t msiq_id)
1004 {
1005 	uint64_t	ret;
1006 
1007 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setmsiq: dip 0x%p msg_type 0x%x "
1008 	    "msq_id 0x%x\n", dip, msg_type, msiq_id);
1009 
1010 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
1011 	    msg_type, msiq_id)) != H_EOK) {
1012 		DBG(DBG_LIB_MSG, dip,
1013 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
1014 		return (DDI_FAILURE);
1015 	}
1016 
1017 	return (DDI_SUCCESS);
1018 }
1019 
1020 /*ARGSUSED*/
1021 int
1022 px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
1023     pcie_msg_valid_state_t *msg_valid_state)
1024 {
1025 	uint64_t	ret;
1026 
1027 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
1028 	    dip, msg_type);
1029 
1030 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
1031 	    msg_valid_state)) != H_EOK) {
1032 		DBG(DBG_LIB_MSG, dip,
1033 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
1034 		return (DDI_FAILURE);
1035 	}
1036 
1037 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
1038 	    *msg_valid_state);
1039 
1040 	return (DDI_SUCCESS);
1041 }
1042 
1043 /*ARGSUSED*/
1044 int
1045 px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
1046     pcie_msg_valid_state_t msg_valid_state)
1047 {
1048 	uint64_t	ret;
1049 
1050 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
1051 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
1052 
1053 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
1054 	    msg_valid_state)) != H_EOK) {
1055 		DBG(DBG_LIB_MSG, dip,
1056 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
1057 		return (DDI_FAILURE);
1058 	}
1059 
1060 	return (DDI_SUCCESS);
1061 }
1062 
1063 /*
1064  * Suspend/Resume Functions:
1065  * Currently unsupported by hypervisor and all functions are noops.
1066  */
1067 /*ARGSUSED*/
1068 int
1069 px_lib_suspend(dev_info_t *dip)
1070 {
1071 	DBG(DBG_ATTACH, dip, "px_lib_suspend: Not supported\n");
1072 
1073 	/* Not supported */
1074 	return (DDI_FAILURE);
1075 }
1076 
1077 /*ARGSUSED*/
1078 void
1079 px_lib_resume(dev_info_t *dip)
1080 {
1081 	DBG(DBG_ATTACH, dip, "px_lib_resume: Not supported\n");
1082 
1083 	/* Noop */
1084 }
1085 
1086 /*
1087  * Misc Functions:
1088  * Currently unsupported by hypervisor and all functions are noops.
1089  */
1090 /*ARGSUSED*/
1091 static int
1092 px_lib_config_get(dev_info_t *dip, pci_device_t bdf, pci_config_offset_t off,
1093     uint8_t size, pci_cfg_data_t *data_p)
1094 {
1095 	uint64_t	ret;
1096 
1097 	DBG(DBG_LIB_CFG, dip, "px_lib_config_get: dip 0x%p, bdf 0x%llx "
1098 	    "off 0x%x size 0x%x\n", dip, bdf, off, size);
1099 
1100 	if ((ret = hvio_config_get(DIP_TO_HANDLE(dip), bdf, off,
1101 	    size, data_p)) != H_EOK) {
1102 		DBG(DBG_LIB_CFG, dip,
1103 		    "hvio_config_get failed, ret 0x%lx\n", ret);
1104 		return (DDI_FAILURE);
1105 	}
1106 	DBG(DBG_LIB_CFG, dip, "px_config_get: data 0x%x\n", data_p->dw);
1107 
1108 	return (DDI_SUCCESS);
1109 }
1110 
1111 /*ARGSUSED*/
1112 static int
1113 px_lib_config_put(dev_info_t *dip, pci_device_t bdf, pci_config_offset_t off,
1114     uint8_t size, pci_cfg_data_t data)
1115 {
1116 	uint64_t	ret;
1117 
1118 	DBG(DBG_LIB_CFG, dip, "px_lib_config_put: dip 0x%p, bdf 0x%llx "
1119 	    "off 0x%x size 0x%x data 0x%llx\n", dip, bdf, off, size, data.qw);
1120 
1121 	if ((ret = hvio_config_put(DIP_TO_HANDLE(dip), bdf, off,
1122 	    size, data)) != H_EOK) {
1123 		DBG(DBG_LIB_CFG, dip,
1124 		    "hvio_config_put failed, ret 0x%lx\n", ret);
1125 		return (DDI_FAILURE);
1126 	}
1127 
1128 	return (DDI_SUCCESS);
1129 }
1130 
1131 static uint32_t
1132 px_pci_config_get(ddi_acc_impl_t *handle, uint32_t *addr, int size)
1133 {
1134 	px_config_acc_pvt_t *px_pvt = (px_config_acc_pvt_t *)
1135 	    handle->ahi_common.ah_bus_private;
1136 	uint32_t pci_dev_addr = px_pvt->raddr;
1137 	uint32_t vaddr = px_pvt->vaddr;
1138 	uint16_t off = (uint16_t)(uintptr_t)(addr - vaddr) & 0xfff;
1139 	uint32_t rdata = 0;
1140 
1141 	if (px_lib_config_get(px_pvt->dip, pci_dev_addr, off,
1142 	    size, (pci_cfg_data_t *)&rdata) != DDI_SUCCESS)
1143 		/* XXX update error kstats */
1144 		return (0xffffffff);
1145 	return (rdata);
1146 }
1147 
1148 static void
1149 px_pci_config_put(ddi_acc_impl_t *handle, uint32_t *addr,
1150 		int size, pci_cfg_data_t wdata)
1151 {
1152 	px_config_acc_pvt_t *px_pvt = (px_config_acc_pvt_t *)
1153 	    handle->ahi_common.ah_bus_private;
1154 	uint32_t pci_dev_addr = px_pvt->raddr;
1155 	uint32_t vaddr = px_pvt->vaddr;
1156 	uint16_t off = (uint16_t)(uintptr_t)(addr - vaddr) & 0xfff;
1157 
1158 	if (px_lib_config_put(px_pvt->dip, pci_dev_addr, off,
1159 	    size, wdata) != DDI_SUCCESS) {
1160 		/*EMPTY*/
1161 		/* XXX update error kstats */
1162 	}
1163 }
1164 
1165 static uint8_t
1166 px_pci_config_get8(ddi_acc_impl_t *handle, uint8_t *addr)
1167 {
1168 	return ((uint8_t)px_pci_config_get(handle, (uint32_t *)addr, 1));
1169 }
1170 
1171 static uint16_t
1172 px_pci_config_get16(ddi_acc_impl_t *handle, uint16_t *addr)
1173 {
1174 	return ((uint16_t)px_pci_config_get(handle, (uint32_t *)addr, 2));
1175 }
1176 
1177 static uint32_t
1178 px_pci_config_get32(ddi_acc_impl_t *handle, uint32_t *addr)
1179 {
1180 	return ((uint32_t)px_pci_config_get(handle, (uint32_t *)addr, 4));
1181 }
1182 
1183 static uint64_t
1184 px_pci_config_get64(ddi_acc_impl_t *handle, uint64_t *addr)
1185 {
1186 	uint32_t rdatah, rdatal;
1187 
1188 	rdatal = (uint32_t)px_pci_config_get(handle, (uint32_t *)addr, 4);
1189 	rdatah = (uint32_t)px_pci_config_get(handle,
1190 	    (uint32_t *)((char *)addr+4), 4);
1191 	return (((uint64_t)rdatah << 32) | rdatal);
1192 }
1193 
1194 static void
1195 px_pci_config_put8(ddi_acc_impl_t *handle, uint8_t *addr, uint8_t data)
1196 {
1197 	pci_cfg_data_t wdata = { 0 };
1198 
1199 	wdata.qw = (uint8_t)data;
1200 	px_pci_config_put(handle, (uint32_t *)addr, 1, wdata);
1201 }
1202 
1203 static void
1204 px_pci_config_put16(ddi_acc_impl_t *handle, uint16_t *addr, uint16_t data)
1205 {
1206 	pci_cfg_data_t wdata = { 0 };
1207 
1208 	wdata.qw = (uint16_t)data;
1209 	px_pci_config_put(handle, (uint32_t *)addr, 2, wdata);
1210 }
1211 
1212 static void
1213 px_pci_config_put32(ddi_acc_impl_t *handle, uint32_t *addr, uint32_t data)
1214 {
1215 	pci_cfg_data_t wdata = { 0 };
1216 
1217 	wdata.qw = (uint32_t)data;
1218 	px_pci_config_put(handle, (uint32_t *)addr, 4, wdata);
1219 }
1220 
1221 static void
1222 px_pci_config_put64(ddi_acc_impl_t *handle, uint64_t *addr, uint64_t data)
1223 {
1224 	pci_cfg_data_t wdata = { 0 };
1225 
1226 	wdata.qw = (uint32_t)(data & 0xffffffff);
1227 	px_pci_config_put(handle, (uint32_t *)addr, 4, wdata);
1228 	wdata.qw = (uint32_t)((data >> 32) & 0xffffffff);
1229 	px_pci_config_put(handle, (uint32_t *)((char *)addr+4), 4, wdata);
1230 }
1231 
1232 static void
1233 px_pci_config_rep_get8(ddi_acc_impl_t *handle, uint8_t *host_addr,
1234 			uint8_t *dev_addr, size_t repcount, uint_t flags)
1235 {
1236 	if (flags == DDI_DEV_AUTOINCR)
1237 		for (; repcount; repcount--)
1238 			*host_addr++ = px_pci_config_get8(handle, dev_addr++);
1239 	else
1240 		for (; repcount; repcount--)
1241 			*host_addr++ = px_pci_config_get8(handle, dev_addr);
1242 }
1243 
1244 /*
1245  * Function to rep read 16 bit data off the PCI configuration space behind
1246  * the 21554's host interface.
1247  */
1248 static void
1249 px_pci_config_rep_get16(ddi_acc_impl_t *handle, uint16_t *host_addr,
1250 			uint16_t *dev_addr, size_t repcount, uint_t flags)
1251 {
1252 	if (flags == DDI_DEV_AUTOINCR)
1253 		for (; repcount; repcount--)
1254 			*host_addr++ = px_pci_config_get16(handle, dev_addr++);
1255 	else
1256 		for (; repcount; repcount--)
1257 			*host_addr++ = px_pci_config_get16(handle, dev_addr);
1258 }
1259 
1260 /*
1261  * Function to rep read 32 bit data off the PCI configuration space behind
1262  * the 21554's host interface.
1263  */
1264 static void
1265 px_pci_config_rep_get32(ddi_acc_impl_t *handle, uint32_t *host_addr,
1266 			uint32_t *dev_addr, size_t repcount, uint_t flags)
1267 {
1268 	if (flags == DDI_DEV_AUTOINCR)
1269 		for (; repcount; repcount--)
1270 			*host_addr++ = px_pci_config_get32(handle, dev_addr++);
1271 	else
1272 		for (; repcount; repcount--)
1273 			*host_addr++ = px_pci_config_get32(handle, dev_addr);
1274 }
1275 
1276 /*
1277  * Function to rep read 64 bit data off the PCI configuration space behind
1278  * the 21554's host interface.
1279  */
1280 static void
1281 px_pci_config_rep_get64(ddi_acc_impl_t *handle, uint64_t *host_addr,
1282 			uint64_t *dev_addr, size_t repcount, uint_t flags)
1283 {
1284 	if (flags == DDI_DEV_AUTOINCR)
1285 		for (; repcount; repcount--)
1286 			*host_addr++ = px_pci_config_get64(handle, dev_addr++);
1287 	else
1288 		for (; repcount; repcount--)
1289 			*host_addr++ = px_pci_config_get64(handle, dev_addr);
1290 }
1291 
1292 /*
1293  * Function to rep write 8 bit data into the PCI configuration space behind
1294  * the 21554's host interface.
1295  */
1296 static void
1297 px_pci_config_rep_put8(ddi_acc_impl_t *handle, uint8_t *host_addr,
1298 			uint8_t *dev_addr, size_t repcount, uint_t flags)
1299 {
1300 	if (flags == DDI_DEV_AUTOINCR)
1301 		for (; repcount; repcount--)
1302 			px_pci_config_put8(handle, dev_addr++, *host_addr++);
1303 	else
1304 		for (; repcount; repcount--)
1305 			px_pci_config_put8(handle, dev_addr, *host_addr++);
1306 }
1307 
1308 /*
1309  * Function to rep write 16 bit data into the PCI configuration space behind
1310  * the 21554's host interface.
1311  */
1312 static void
1313 px_pci_config_rep_put16(ddi_acc_impl_t *handle, uint16_t *host_addr,
1314 			uint16_t *dev_addr, size_t repcount, uint_t flags)
1315 {
1316 	if (flags == DDI_DEV_AUTOINCR)
1317 		for (; repcount; repcount--)
1318 			px_pci_config_put16(handle, dev_addr++, *host_addr++);
1319 	else
1320 		for (; repcount; repcount--)
1321 			px_pci_config_put16(handle, dev_addr, *host_addr++);
1322 }
1323 
1324 /*
1325  * Function to rep write 32 bit data into the PCI configuration space behind
1326  * the 21554's host interface.
1327  */
1328 static void
1329 px_pci_config_rep_put32(ddi_acc_impl_t *handle, uint32_t *host_addr,
1330 			uint32_t *dev_addr, size_t repcount, uint_t flags)
1331 {
1332 	if (flags == DDI_DEV_AUTOINCR)
1333 		for (; repcount; repcount--)
1334 			px_pci_config_put32(handle, dev_addr++, *host_addr++);
1335 	else
1336 		for (; repcount; repcount--)
1337 			px_pci_config_put32(handle, dev_addr, *host_addr++);
1338 }
1339 
1340 /*
1341  * Function to rep write 64 bit data into the PCI configuration space behind
1342  * the 21554's host interface.
1343  */
1344 static void
1345 px_pci_config_rep_put64(ddi_acc_impl_t *handle, uint64_t *host_addr,
1346 			uint64_t *dev_addr, size_t repcount, uint_t flags)
1347 {
1348 	if (flags == DDI_DEV_AUTOINCR)
1349 		for (; repcount; repcount--)
1350 			px_pci_config_put64(handle, dev_addr++, *host_addr++);
1351 	else
1352 		for (; repcount; repcount--)
1353 			px_pci_config_put64(handle, dev_addr, *host_addr++);
1354 }
1355 
1356 /*
1357  * Provide a private access handle to route config access calls to Hypervisor.
1358  * Beware: Do all error checking for config space accesses before calling
1359  * this function. ie. do error checking from the calling function.
1360  * Due to a lack of meaningful error code in DDI, the gauranteed return of
1361  * DDI_SUCCESS from here makes the code organization readable/easier from
1362  * the generic code.
1363  */
1364 /*ARGSUSED*/
1365 int
1366 px_lib_map_vconfig(dev_info_t *dip,
1367 	ddi_map_req_t *mp, pci_config_offset_t off,
1368 	pci_regspec_t *rp, caddr_t *addrp)
1369 {
1370 	int fmcap;
1371 	ndi_err_t *errp;
1372 	on_trap_data_t *otp;
1373 	ddi_acc_hdl_t *hp;
1374 	ddi_acc_impl_t *ap;
1375 	uchar_t busnum;	/* bus number */
1376 	uchar_t devnum;	/* device number */
1377 	uchar_t funcnum; /* function number */
1378 	px_config_acc_pvt_t *px_pvt;
1379 
1380 	hp = (ddi_acc_hdl_t *)mp->map_handlep;
1381 	ap = (ddi_acc_impl_t *)hp->ah_platform_private;
1382 
1383 	/* Check for mapping teardown operation */
1384 	if ((mp->map_op == DDI_MO_UNMAP) ||
1385 	    (mp->map_op == DDI_MO_UNLOCK)) {
1386 		/* free up memory allocated for the private access handle. */
1387 		px_pvt = (px_config_acc_pvt_t *)hp->ah_bus_private;
1388 		kmem_free((void *)px_pvt, sizeof (px_config_acc_pvt_t));
1389 
1390 		/* unmap operation of PCI IO/config space. */
1391 		return (DDI_SUCCESS);
1392 	}
1393 
1394 	fmcap = ddi_fm_capable(dip);
1395 	if (DDI_FM_ACC_ERR_CAP(fmcap)) {
1396 		errp = ((ddi_acc_impl_t *)hp)->ahi_err;
1397 		otp = (on_trap_data_t *)errp->err_ontrap;
1398 		otp->ot_handle = (void *)(hp);
1399 		otp->ot_prot = OT_DATA_ACCESS;
1400 		errp->err_status = DDI_FM_OK;
1401 		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
1402 		errp->err_cf = px_err_cfg_hdl_check;
1403 	}
1404 
1405 	ap->ahi_get8 = px_pci_config_get8;
1406 	ap->ahi_get16 = px_pci_config_get16;
1407 	ap->ahi_get32 = px_pci_config_get32;
1408 	ap->ahi_get64 = px_pci_config_get64;
1409 	ap->ahi_put8 = px_pci_config_put8;
1410 	ap->ahi_put16 = px_pci_config_put16;
1411 	ap->ahi_put32 = px_pci_config_put32;
1412 	ap->ahi_put64 = px_pci_config_put64;
1413 	ap->ahi_rep_get8 = px_pci_config_rep_get8;
1414 	ap->ahi_rep_get16 = px_pci_config_rep_get16;
1415 	ap->ahi_rep_get32 = px_pci_config_rep_get32;
1416 	ap->ahi_rep_get64 = px_pci_config_rep_get64;
1417 	ap->ahi_rep_put8 = px_pci_config_rep_put8;
1418 	ap->ahi_rep_put16 = px_pci_config_rep_put16;
1419 	ap->ahi_rep_put32 = px_pci_config_rep_put32;
1420 	ap->ahi_rep_put64 = px_pci_config_rep_put64;
1421 
1422 	/* Initialize to default check/notify functions */
1423 	ap->ahi_fault = 0;
1424 	ap->ahi_fault_check = i_ddi_acc_fault_check;
1425 	ap->ahi_fault_notify = i_ddi_acc_fault_notify;
1426 
1427 	/* allocate memory for our private handle */
1428 	px_pvt = (px_config_acc_pvt_t *)
1429 	    kmem_zalloc(sizeof (px_config_acc_pvt_t), KM_SLEEP);
1430 	hp->ah_bus_private = (void *)px_pvt;
1431 
1432 	busnum = PCI_REG_BUS_G(rp->pci_phys_hi);
1433 	devnum = PCI_REG_DEV_G(rp->pci_phys_hi);
1434 	funcnum = PCI_REG_FUNC_G(rp->pci_phys_hi);
1435 
1436 	/* set up private data for use during IO routines */
1437 
1438 	/* addr needed by the HV APIs */
1439 	px_pvt->raddr = busnum << 16 | devnum << 11 | funcnum << 8;
1440 	/*
1441 	 * Address that specifies the actual offset into the 256MB
1442 	 * memory mapped configuration space, 4K per device.
1443 	 * First 12bits form the offset into 4K config space.
1444 	 * This address is only used during the IO routines to calculate
1445 	 * the offset at which the transaction must be performed.
1446 	 * Drivers bypassing DDI functions to access PCI config space will
1447 	 * panic the system since the following is a bogus virtual address.
1448 	 */
1449 	px_pvt->vaddr = busnum << 20 | devnum << 15 | funcnum << 12 | off;
1450 	px_pvt->dip = dip;
1451 
1452 	DBG(DBG_LIB_CFG, dip, "px_config_setup: raddr 0x%x, vaddr 0x%x\n",
1453 	    px_pvt->raddr, px_pvt->vaddr);
1454 	*addrp = (caddr_t)(uintptr_t)px_pvt->vaddr;
1455 	return (DDI_SUCCESS);
1456 }
1457 
1458 /*ARGSUSED*/
1459 void
1460 px_lib_map_attr_check(ddi_map_req_t *mp)
1461 {
1462 }
1463 
1464 /*
1465  * px_lib_log_safeacc_err:
1466  * Imitate a cpu/mem trap call when a peek/poke fails.
1467  * This will initiate something similar to px_fm_callback.
1468  */
1469 static void
1470 px_lib_log_safeacc_err(px_t *px_p, ddi_acc_handle_t handle, int fme_flag,
1471     r_addr_t addr)
1472 {
1473 	uint32_t	addr_high, addr_low;
1474 	pcie_req_id_t	bdf;
1475 	px_ranges_t	*ranges_p;
1476 	int		range_len, i;
1477 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
1478 	ddi_fm_error_t derr;
1479 
1480 	derr.fme_status = DDI_FM_NONFATAL;
1481 	derr.fme_version = DDI_FME_VERSION;
1482 	derr.fme_flag = fme_flag;
1483 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
1484 	derr.fme_acc_handle = handle;
1485 	if (hp)
1486 		hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
1487 
1488 	addr_high = (uint32_t)(addr >> 32);
1489 	addr_low = (uint32_t)addr;
1490 
1491 	/*
1492 	 * Make sure this failed load came from this PCIe port.  Check by
1493 	 * matching the upper 32 bits of the address with the ranges property.
1494 	 */
1495 	range_len = px_p->px_ranges_length / sizeof (px_ranges_t);
1496 	i = 0;
1497 	for (ranges_p = px_p->px_ranges_p; i < range_len; i++, ranges_p++) {
1498 		if (ranges_p->parent_high == addr_high) {
1499 			switch (ranges_p->child_high & PCI_ADDR_MASK) {
1500 			case PCI_ADDR_CONFIG:
1501 				bdf = (pcie_req_id_t)(addr_low >> 12);
1502 				break;
1503 			default:
1504 				bdf = NULL;
1505 				break;
1506 			}
1507 			break;
1508 		}
1509 	}
1510 
1511 	px_rp_en_q(px_p, bdf, addr, NULL);
1512 
1513 	if (px_fm_enter(px_p) == DDI_SUCCESS) {
1514 		(void) px_scan_fabric(px_p, px_p->px_dip, &derr);
1515 		px_fm_exit(px_p);
1516 	}
1517 }
1518 
1519 
1520 #ifdef  DEBUG
1521 int	px_peekfault_cnt = 0;
1522 int	px_pokefault_cnt = 0;
1523 #endif  /* DEBUG */
1524 
1525 /*
1526  * Do a safe write to a device.
1527  *
1528  * When this function is given a handle (cautious access), all errors are
1529  * suppressed.
1530  *
1531  * When this function is not given a handle (poke), only Unsupported Request
1532  * and Completer Abort errors are suppressed.
1533  *
1534  * In all cases, all errors are returned in the function return status.
1535  */
1536 
1537 int
1538 px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
1539     peekpoke_ctlops_t *in_args)
1540 {
1541 	px_t *px_p = DIP_TO_STATE(dip);
1542 	px_pec_t *pec_p = px_p->px_pec_p;
1543 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
1544 
1545 	size_t repcount = in_args->repcount;
1546 	size_t size = in_args->size;
1547 	uintptr_t dev_addr = in_args->dev_addr;
1548 	uintptr_t host_addr = in_args->host_addr;
1549 
1550 	int err	= DDI_SUCCESS;
1551 	uint64_t hvio_poke_status;
1552 	uint32_t wrt_stat;
1553 
1554 	r_addr_t ra;
1555 	uint64_t pokeval;
1556 	pcie_req_id_t bdf;
1557 
1558 	ra = (r_addr_t)va_to_pa((void *)dev_addr);
1559 	for (; repcount; repcount--) {
1560 
1561 		switch (size) {
1562 		case sizeof (uint8_t):
1563 			pokeval = *(uint8_t *)host_addr;
1564 			break;
1565 		case sizeof (uint16_t):
1566 			pokeval = *(uint16_t *)host_addr;
1567 			break;
1568 		case sizeof (uint32_t):
1569 			pokeval = *(uint32_t *)host_addr;
1570 			break;
1571 		case sizeof (uint64_t):
1572 			pokeval = *(uint64_t *)host_addr;
1573 			break;
1574 		default:
1575 			DBG(DBG_MAP, px_p->px_dip,
1576 			    "poke: invalid size %d passed\n", size);
1577 			err = DDI_FAILURE;
1578 			goto done;
1579 		}
1580 
1581 		/*
1582 		 * Grab pokefault mutex since hypervisor does not guarantee
1583 		 * poke serialization.
1584 		 */
1585 		if (hp) {
1586 			i_ndi_busop_access_enter(hp->ahi_common.ah_dip,
1587 			    (ddi_acc_handle_t)hp);
1588 			pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
1589 		} else {
1590 			mutex_enter(&pec_p->pec_pokefault_mutex);
1591 			pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
1592 		}
1593 
1594 		if (pcie_get_bdf_from_dip(rdip, &bdf) != DDI_SUCCESS) {
1595 			err = DDI_FAILURE;
1596 			goto done;
1597 		}
1598 
1599 		hvio_poke_status = hvio_poke(px_p->px_dev_hdl, ra, size,
1600 		    pokeval, bdf << 8, &wrt_stat);
1601 
1602 		if ((hvio_poke_status != H_EOK) || (wrt_stat != H_EOK)) {
1603 			err = DDI_FAILURE;
1604 #ifdef  DEBUG
1605 			px_pokefault_cnt++;
1606 #endif
1607 			/*
1608 			 * For CAUTIOUS and POKE access, notify FMA to
1609 			 * cleanup.  Imitate a cpu/mem trap call like in sun4u.
1610 			 */
1611 			px_lib_log_safeacc_err(px_p, (ddi_acc_handle_t)hp,
1612 			    (hp ? DDI_FM_ERR_EXPECTED :
1613 			    DDI_FM_ERR_POKE), ra);
1614 
1615 			pec_p->pec_ontrap_data = NULL;
1616 			pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1617 			if (hp) {
1618 				i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
1619 				    (ddi_acc_handle_t)hp);
1620 			} else {
1621 				mutex_exit(&pec_p->pec_pokefault_mutex);
1622 			}
1623 			goto done;
1624 		}
1625 
1626 		pec_p->pec_ontrap_data = NULL;
1627 		pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1628 		if (hp) {
1629 			i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
1630 			    (ddi_acc_handle_t)hp);
1631 		} else {
1632 			mutex_exit(&pec_p->pec_pokefault_mutex);
1633 		}
1634 
1635 		host_addr += size;
1636 
1637 		if (in_args->flags == DDI_DEV_AUTOINCR) {
1638 			dev_addr += size;
1639 			ra = (r_addr_t)va_to_pa((void *)dev_addr);
1640 		}
1641 	}
1642 
1643 done:
1644 	return (err);
1645 }
1646 
1647 
1648 /*ARGSUSED*/
1649 int
1650 px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
1651     peekpoke_ctlops_t *in_args, void *result)
1652 {
1653 	px_t *px_p = DIP_TO_STATE(dip);
1654 	px_pec_t *pec_p = px_p->px_pec_p;
1655 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
1656 
1657 	size_t repcount = in_args->repcount;
1658 	uintptr_t dev_addr = in_args->dev_addr;
1659 	uintptr_t host_addr = in_args->host_addr;
1660 
1661 	r_addr_t ra;
1662 	uint32_t read_status;
1663 	uint64_t hvio_peek_status;
1664 	uint64_t peekval;
1665 	int err = DDI_SUCCESS;
1666 
1667 	result = (void *)in_args->host_addr;
1668 
1669 	ra = (r_addr_t)va_to_pa((void *)dev_addr);
1670 	for (; repcount; repcount--) {
1671 
1672 		/* Lock pokefault mutex so read doesn't mask a poke fault. */
1673 		if (hp) {
1674 			i_ndi_busop_access_enter(hp->ahi_common.ah_dip,
1675 			    (ddi_acc_handle_t)hp);
1676 			pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
1677 		} else {
1678 			mutex_enter(&pec_p->pec_pokefault_mutex);
1679 			pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
1680 		}
1681 
1682 		hvio_peek_status = hvio_peek(px_p->px_dev_hdl, ra,
1683 		    in_args->size, &read_status, &peekval);
1684 
1685 		if ((hvio_peek_status != H_EOK) || (read_status != H_EOK)) {
1686 			err = DDI_FAILURE;
1687 
1688 			/*
1689 			 * For CAUTIOUS and PEEK access, notify FMA to
1690 			 * cleanup.  Imitate a cpu/mem trap call like in sun4u.
1691 			 */
1692 			px_lib_log_safeacc_err(px_p, (ddi_acc_handle_t)hp,
1693 			    (hp ? DDI_FM_ERR_EXPECTED :
1694 			    DDI_FM_ERR_PEEK), ra);
1695 
1696 			/* Stuff FFs in host addr if peek. */
1697 			if (hp == NULL) {
1698 				int i;
1699 				uint8_t *ff_addr = (uint8_t *)host_addr;
1700 				for (i = 0; i < in_args->size; i++)
1701 					*ff_addr++ = 0xff;
1702 			}
1703 #ifdef  DEBUG
1704 			px_peekfault_cnt++;
1705 #endif
1706 			pec_p->pec_ontrap_data = NULL;
1707 			pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1708 			if (hp) {
1709 				i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
1710 				    (ddi_acc_handle_t)hp);
1711 			} else {
1712 				mutex_exit(&pec_p->pec_pokefault_mutex);
1713 			}
1714 			goto done;
1715 
1716 		}
1717 		pec_p->pec_ontrap_data = NULL;
1718 		pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1719 		if (hp) {
1720 			i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
1721 			    (ddi_acc_handle_t)hp);
1722 		} else {
1723 			mutex_exit(&pec_p->pec_pokefault_mutex);
1724 		}
1725 
1726 		switch (in_args->size) {
1727 		case sizeof (uint8_t):
1728 			*(uint8_t *)host_addr = (uint8_t)peekval;
1729 			break;
1730 		case sizeof (uint16_t):
1731 			*(uint16_t *)host_addr = (uint16_t)peekval;
1732 			break;
1733 		case sizeof (uint32_t):
1734 			*(uint32_t *)host_addr = (uint32_t)peekval;
1735 			break;
1736 		case sizeof (uint64_t):
1737 			*(uint64_t *)host_addr = (uint64_t)peekval;
1738 			break;
1739 		default:
1740 			DBG(DBG_MAP, px_p->px_dip,
1741 			    "peek: invalid size %d passed\n",
1742 			    in_args->size);
1743 			err = DDI_FAILURE;
1744 			goto done;
1745 		}
1746 
1747 		host_addr += in_args->size;
1748 
1749 		if (in_args->flags == DDI_DEV_AUTOINCR) {
1750 			dev_addr += in_args->size;
1751 			ra = (r_addr_t)va_to_pa((void *)dev_addr);
1752 		}
1753 	}
1754 done:
1755 	return (err);
1756 }
1757 
1758 
1759 /* add interrupt vector */
1760 int
1761 px_err_add_intr(px_fault_t *px_fault_p)
1762 {
1763 	px_t	*px_p = DIP_TO_STATE(px_fault_p->px_fh_dip);
1764 
1765 	DBG(DBG_LIB_INT, px_p->px_dip,
1766 	    "px_err_add_intr: calling add_ivintr");
1767 
1768 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
1769 	    (intrfunc)px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL,
1770 	    (caddr_t)&px_fault_p->px_intr_payload[0]) == 0);
1771 
1772 	DBG(DBG_LIB_INT, px_p->px_dip,
1773 	    "px_err_add_intr: ib_intr_enable ");
1774 
1775 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
1776 
1777 	return (DDI_SUCCESS);
1778 }
1779 
1780 /* remove interrupt vector */
1781 void
1782 px_err_rem_intr(px_fault_t *px_fault_p)
1783 {
1784 	px_t	*px_p = DIP_TO_STATE(px_fault_p->px_fh_dip);
1785 
1786 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
1787 	    IB_INTR_WAIT);
1788 
1789 	VERIFY(rem_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL) == 0);
1790 }
1791 
1792 void
1793 px_cb_intr_redist(void *arg)
1794 {
1795 	px_t	*px_p = (px_t *)arg;
1796 	px_ib_intr_dist_en(px_p->px_dip, intr_dist_cpuid(),
1797 	    px_p->px_inos[PX_INTR_XBC], B_FALSE);
1798 }
1799 
1800 int
1801 px_cb_add_intr(px_fault_t *f_p)
1802 {
1803 	px_t	*px_p = DIP_TO_STATE(f_p->px_fh_dip);
1804 
1805 	DBG(DBG_LIB_INT, px_p->px_dip,
1806 	    "px_err_add_intr: calling add_ivintr");
1807 
1808 	VERIFY(add_ivintr(f_p->px_fh_sysino, PX_ERR_PIL,
1809 	    (intrfunc)f_p->px_err_func, (caddr_t)f_p, NULL,
1810 	    (caddr_t)&f_p->px_intr_payload[0]) == 0);
1811 
1812 	intr_dist_add(px_cb_intr_redist, px_p);
1813 
1814 	DBG(DBG_LIB_INT, px_p->px_dip,
1815 	    "px_err_add_intr: ib_intr_enable ");
1816 
1817 	px_ib_intr_enable(px_p, intr_dist_cpuid(), f_p->px_intr_ino);
1818 
1819 	return (DDI_SUCCESS);
1820 }
1821 
1822 void
1823 px_cb_rem_intr(px_fault_t *f_p)
1824 {
1825 	intr_dist_rem(px_cb_intr_redist, DIP_TO_STATE(f_p->px_fh_dip));
1826 	px_err_rem_intr(f_p);
1827 }
1828 
1829 #ifdef FMA
1830 void
1831 px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
1832 {
1833 	px_pec_err_t	*err_pkt;
1834 
1835 	err_pkt = (px_pec_err_t *)px_fault_p->px_intr_payload;
1836 
1837 	/* initialise all the structure members */
1838 	rc_status->status_valid = 0;
1839 
1840 	if (err_pkt->pec_descr.P) {
1841 		/* PCI Status Register */
1842 		rc_status->pci_err_status = err_pkt->pci_err_status;
1843 		rc_status->status_valid |= PCI_ERR_STATUS_VALID;
1844 	}
1845 
1846 	if (err_pkt->pec_descr.E) {
1847 		/* PCIe Status Register */
1848 		rc_status->pcie_err_status = err_pkt->pcie_err_status;
1849 		rc_status->status_valid |= PCIE_ERR_STATUS_VALID;
1850 	}
1851 
1852 	if (err_pkt->pec_descr.U) {
1853 		rc_status->ue_status = err_pkt->ue_reg_status;
1854 		rc_status->status_valid |= UE_STATUS_VALID;
1855 	}
1856 
1857 	if (err_pkt->pec_descr.H) {
1858 		rc_status->ue_hdr1 = err_pkt->hdr[0];
1859 		rc_status->status_valid |= UE_HDR1_VALID;
1860 	}
1861 
1862 	if (err_pkt->pec_descr.I) {
1863 		rc_status->ue_hdr2 = err_pkt->hdr[1];
1864 		rc_status->status_valid |= UE_HDR2_VALID;
1865 	}
1866 
1867 	/* ue_fst_err_ptr - not available for sun4v?? */
1868 
1869 
1870 	if (err_pkt->pec_descr.S) {
1871 		rc_status->source_id = err_pkt->err_src_reg;
1872 		rc_status->status_valid |= SOURCE_ID_VALID;
1873 	}
1874 
1875 	if (err_pkt->pec_descr.R) {
1876 		rc_status->root_err_status = err_pkt->root_err_status;
1877 		rc_status->status_valid |= CE_STATUS_VALID;
1878 	}
1879 }
1880 #endif
1881 
1882 /*ARGSUSED*/
1883 int
1884 px_lib_pmctl(int cmd, px_t *px_p)
1885 {
1886 	return (DDI_FAILURE);
1887 }
1888 
1889 /*ARGSUSED*/
1890 uint_t
1891 px_pmeq_intr(caddr_t arg)
1892 {
1893 	return (DDI_INTR_CLAIMED);
1894 }
1895 
1896 /*
1897  * Unprotected raw reads/writes of fabric device's config space.
1898  * Only used for temporary PCI-E Fabric Error Handling.
1899  */
1900 uint32_t
1901 px_fab_get(px_t *px_p, pcie_req_id_t bdf, uint16_t offset) {
1902 	uint32_t 	data = 0;
1903 
1904 	(void) hvio_config_get(px_p->px_dev_hdl,
1905 	    (bdf << PX_RA_BDF_SHIFT), offset, 4,
1906 	    (pci_cfg_data_t *)&data);
1907 
1908 	return (data);
1909 }
1910 
1911 void
1912 px_fab_set(px_t *px_p, pcie_req_id_t bdf, uint16_t offset,
1913     uint32_t val) {
1914 	pci_cfg_data_t	wdata = { 0 };
1915 
1916 	wdata.qw = (uint32_t)val;
1917 	(void) hvio_config_put(px_p->px_dev_hdl,
1918 	    (bdf << PX_RA_BDF_SHIFT), offset, 4, wdata);
1919 }
1920 
1921 /*ARGSUSED*/
1922 int
1923 px_lib_hotplug_init(dev_info_t *dip, void *arg)
1924 {
1925 	return (DDI_ENOTSUP);
1926 }
1927 
1928 /*ARGSUSED*/
1929 void
1930 px_lib_hotplug_uninit(dev_info_t *dip)
1931 {
1932 }
1933 
1934 /*ARGSUSED*/
1935 void
1936 px_hp_intr_redist(px_t *px_p)
1937 {
1938 }
1939 
1940 /* Dummy cpr add callback */
1941 /*ARGSUSED*/
1942 void
1943 px_cpr_add_callb(px_t *px_p)
1944 {
1945 }
1946 
1947 /* Dummy cpr rem callback */
1948 /*ARGSUSED*/
1949 void
1950 px_cpr_rem_callb(px_t *px_p)
1951 {
1952 }
1953 
1954 /*ARGSUSED*/
1955 boolean_t
1956 px_lib_is_in_drain_state(px_t *px_p)
1957 {
1958 	return (B_FALSE);
1959 }
1960 
1961 /*
1962  * There is no IOAPI to get the BDF of the pcie root port nexus at this moment.
1963  * Assume it is 0x0000, until otherwise noted.  For now, all sun4v platforms
1964  * have programmed the BDF to be 0x0000.
1965  */
1966 /*ARGSUSED*/
1967 pcie_req_id_t
1968 px_lib_get_bdf(px_t *px_p)
1969 {
1970 	return (0x0000);
1971 }
1972