xref: /illumos-gate/usr/src/uts/sun4u/io/px/px_lib4u.c (revision 7510e22871129c07d22d5fc9a77b746bdcc90abf)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/conf.h>
32 #include <sys/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/fm/protocol.h>
35 #include <sys/fm/util.h>
36 #include <sys/modctl.h>
37 #include <sys/disp.h>
38 #include <sys/stat.h>
39 #include <sys/ddi_impldefs.h>
40 #include <sys/vmem.h>
41 #include <sys/iommutsb.h>
42 #include <sys/cpuvar.h>
43 #include <sys/ivintr.h>
44 #include <px_obj.h>
45 #include <pcie_pwr.h>
46 #include "px_tools_var.h"
47 #include <px_regs.h>
48 #include <px_csr.h>
49 #include <sys/machsystm.h>
50 #include "px_lib4u.h"
51 #include "px_err.h"
52 
53 #pragma weak jbus_stst_order
54 
55 extern void jbus_stst_order();
56 
57 ulong_t px_mmu_dvma_end = 0xfffffffful;
58 uint_t px_ranges_phi_mask = 0xfffffffful;
59 
60 static int px_goto_l23ready(px_t *px_p);
61 static int px_goto_l0(px_t *px_p);
62 static int px_pre_pwron_check(px_t *px_p);
63 static uint32_t px_identity_chip(px_t *px_p);
64 static void px_lib_clr_errs(px_t *px_p, px_pec_t *pec_p);
65 
66 /*
67  * px_lib_map_registers
68  *
69  * This function is called from the attach routine to map the registers
70  * accessed by this driver.
71  *
72  * used by: px_attach()
73  *
74  * return value: DDI_FAILURE on failure
75  */
76 int
77 px_lib_map_regs(pxu_t *pxu_p, dev_info_t *dip)
78 {
79 	ddi_device_acc_attr_t	attr;
80 	px_reg_bank_t		reg_bank = PX_REG_CSR;
81 
82 	DBG(DBG_ATTACH, dip, "px_lib_map_regs: pxu_p:0x%p, dip 0x%p\n",
83 		pxu_p, dip);
84 
85 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
86 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
87 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
88 
89 	/*
90 	 * PCI CSR Base
91 	 */
92 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
93 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
94 		goto fail;
95 	}
96 
97 	reg_bank++;
98 
99 	/*
100 	 * XBUS CSR Base
101 	 */
102 	if (ddi_regs_map_setup(dip, reg_bank, &pxu_p->px_address[reg_bank],
103 	    0, 0, &attr, &pxu_p->px_ac[reg_bank]) != DDI_SUCCESS) {
104 		goto fail;
105 	}
106 
107 	pxu_p->px_address[reg_bank] -= FIRE_CONTROL_STATUS;
108 
109 done:
110 	for (; reg_bank >= PX_REG_CSR; reg_bank--) {
111 		DBG(DBG_ATTACH, dip, "reg_bank 0x%x address 0x%p\n",
112 		    reg_bank, pxu_p->px_address[reg_bank]);
113 	}
114 
115 	return (DDI_SUCCESS);
116 
117 fail:
118 	cmn_err(CE_WARN, "%s%d: unable to map reg entry %d\n",
119 	    ddi_driver_name(dip), ddi_get_instance(dip), reg_bank);
120 
121 	for (reg_bank--; reg_bank >= PX_REG_CSR; reg_bank--) {
122 		pxu_p->px_address[reg_bank] = NULL;
123 		ddi_regs_map_free(&pxu_p->px_ac[reg_bank]);
124 	}
125 
126 	return (DDI_FAILURE);
127 }
128 
129 /*
130  * px_lib_unmap_regs:
131  *
132  * This routine unmaps the registers mapped by map_px_registers.
133  *
134  * used by: px_detach(), and error conditions in px_attach()
135  *
136  * return value: none
137  */
138 void
139 px_lib_unmap_regs(pxu_t *pxu_p)
140 {
141 	int i;
142 
143 	for (i = 0; i < PX_REG_MAX; i++) {
144 		if (pxu_p->px_ac[i])
145 			ddi_regs_map_free(&pxu_p->px_ac[i]);
146 	}
147 }
148 
149 int
150 px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
151 {
152 	px_t		*px_p = DIP_TO_STATE(dip);
153 	caddr_t		xbc_csr_base, csr_base;
154 	px_dvma_range_prop_t	px_dvma_range;
155 	uint32_t	chip_id;
156 	pxu_t		*pxu_p;
157 
158 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p\n", dip);
159 
160 	if ((chip_id = px_identity_chip(px_p)) == PX_CHIP_UNIDENTIFIED)
161 		return (DDI_FAILURE);
162 
163 	switch (chip_id) {
164 	case FIRE_VER_10:
165 		cmn_err(CE_WARN, "FIRE Hardware Version 1.0 is not supported");
166 		return (DDI_FAILURE);
167 	case FIRE_VER_20:
168 		DBG(DBG_ATTACH, dip, "FIRE Hardware Version 2.0\n");
169 		break;
170 	default:
171 		cmn_err(CE_WARN, "%s%d: FIRE Hardware Version Unknown\n",
172 		    ddi_driver_name(dip), ddi_get_instance(dip));
173 		return (DDI_FAILURE);
174 	}
175 
176 	/*
177 	 * Allocate platform specific structure and link it to
178 	 * the px state structure.
179 	 */
180 	pxu_p = kmem_zalloc(sizeof (pxu_t), KM_SLEEP);
181 	pxu_p->chip_id = chip_id;
182 	pxu_p->portid  = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
183 	    "portid", -1);
184 
185 	/* Map in the registers */
186 	if (px_lib_map_regs(pxu_p, dip) == DDI_FAILURE) {
187 		kmem_free(pxu_p, sizeof (pxu_t));
188 
189 		return (DDI_FAILURE);
190 	}
191 
192 	xbc_csr_base = (caddr_t)pxu_p->px_address[PX_REG_XBC];
193 	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
194 
195 	pxu_p->tsb_cookie = iommu_tsb_alloc(pxu_p->portid);
196 	pxu_p->tsb_size = iommu_tsb_cookie_to_size(pxu_p->tsb_cookie);
197 	pxu_p->tsb_vaddr = iommu_tsb_cookie_to_va(pxu_p->tsb_cookie);
198 
199 	/*
200 	 * Create "virtual-dma" property to support child devices
201 	 * needing to know DVMA range.
202 	 */
203 	px_dvma_range.dvma_base = (uint32_t)px_mmu_dvma_end + 1
204 	    - ((pxu_p->tsb_size >> 3) << MMU_PAGE_SHIFT);
205 	px_dvma_range.dvma_len = (uint32_t)
206 	    px_mmu_dvma_end - px_dvma_range.dvma_base + 1;
207 
208 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
209 		"virtual-dma", (caddr_t)&px_dvma_range,
210 		sizeof (px_dvma_range_prop_t));
211 	/*
212 	 * Initilize all fire hardware specific blocks.
213 	 */
214 	hvio_cb_init(xbc_csr_base, pxu_p);
215 	hvio_ib_init(csr_base, pxu_p);
216 	hvio_pec_init(csr_base, pxu_p);
217 	hvio_mmu_init(csr_base, pxu_p);
218 
219 	px_p->px_plat_p = (void *)pxu_p;
220 
221 	/*
222 	 * Initialize all the interrupt handlers
223 	 */
224 	px_err_reg_enable(px_p, PX_ERR_JBC);
225 	px_err_reg_enable(px_p, PX_ERR_MMU);
226 	px_err_reg_enable(px_p, PX_ERR_IMU);
227 	px_err_reg_enable(px_p, PX_ERR_TLU_UE);
228 	px_err_reg_enable(px_p, PX_ERR_TLU_CE);
229 	px_err_reg_enable(px_p, PX_ERR_TLU_OE);
230 	px_err_reg_enable(px_p, PX_ERR_ILU);
231 	px_err_reg_enable(px_p, PX_ERR_LPU_LINK);
232 	px_err_reg_enable(px_p, PX_ERR_LPU_PHY);
233 	px_err_reg_enable(px_p, PX_ERR_LPU_RX);
234 	px_err_reg_enable(px_p, PX_ERR_LPU_TX);
235 	px_err_reg_enable(px_p, PX_ERR_LPU_LTSSM);
236 	px_err_reg_enable(px_p, PX_ERR_LPU_GIGABLZ);
237 
238 	/* Initilize device handle */
239 	*dev_hdl = (devhandle_t)csr_base;
240 
241 	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);
242 
243 	return (DDI_SUCCESS);
244 }
245 
246 int
247 px_lib_dev_fini(dev_info_t *dip)
248 {
249 	px_t	*px_p = DIP_TO_STATE(dip);
250 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
251 
252 	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);
253 
254 	/*
255 	 * Deinitialize all the interrupt handlers
256 	 */
257 	px_err_reg_disable(px_p, PX_ERR_JBC);
258 	px_err_reg_disable(px_p, PX_ERR_MMU);
259 	px_err_reg_disable(px_p, PX_ERR_IMU);
260 	px_err_reg_disable(px_p, PX_ERR_TLU_UE);
261 	px_err_reg_disable(px_p, PX_ERR_TLU_CE);
262 	px_err_reg_disable(px_p, PX_ERR_TLU_OE);
263 	px_err_reg_disable(px_p, PX_ERR_ILU);
264 	px_err_reg_disable(px_p, PX_ERR_LPU_LINK);
265 	px_err_reg_disable(px_p, PX_ERR_LPU_PHY);
266 	px_err_reg_disable(px_p, PX_ERR_LPU_RX);
267 	px_err_reg_disable(px_p, PX_ERR_LPU_TX);
268 	px_err_reg_disable(px_p, PX_ERR_LPU_LTSSM);
269 	px_err_reg_disable(px_p, PX_ERR_LPU_GIGABLZ);
270 
271 	iommu_tsb_free(pxu_p->tsb_cookie);
272 
273 	px_lib_unmap_regs((pxu_t *)px_p->px_plat_p);
274 	kmem_free(px_p->px_plat_p, sizeof (pxu_t));
275 	px_p->px_plat_p = NULL;
276 
277 	return (DDI_SUCCESS);
278 }
279 
280 /*ARGSUSED*/
281 int
282 px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
283     sysino_t *sysino)
284 {
285 	px_t	*px_p = DIP_TO_STATE(dip);
286 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
287 	uint64_t	ret;
288 
289 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
290 	    "devino 0x%x\n", dip, devino);
291 
292 	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
293 	    pxu_p, devino, sysino)) != H_EOK) {
294 		DBG(DBG_LIB_INT, dip,
295 		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
296 		return (DDI_FAILURE);
297 	}
298 
299 	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
300 	    *sysino);
301 
302 	return (DDI_SUCCESS);
303 }
304 
305 /*ARGSUSED*/
306 int
307 px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
308     intr_valid_state_t *intr_valid_state)
309 {
310 	uint64_t	ret;
311 
312 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
313 	    dip, sysino);
314 
315 	if ((ret = hvio_intr_getvalid(DIP_TO_HANDLE(dip),
316 	    sysino, intr_valid_state)) != H_EOK) {
317 		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
318 		    ret);
319 		return (DDI_FAILURE);
320 	}
321 
322 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
323 	    *intr_valid_state);
324 
325 	return (DDI_SUCCESS);
326 }
327 
328 /*ARGSUSED*/
329 int
330 px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
331     intr_valid_state_t intr_valid_state)
332 {
333 	uint64_t	ret;
334 
335 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
336 	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);
337 
338 	if ((ret = hvio_intr_setvalid(DIP_TO_HANDLE(dip),
339 	    sysino, intr_valid_state)) != H_EOK) {
340 		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
341 		    ret);
342 		return (DDI_FAILURE);
343 	}
344 
345 	return (DDI_SUCCESS);
346 }
347 
348 /*ARGSUSED*/
349 int
350 px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
351     intr_state_t *intr_state)
352 {
353 	uint64_t	ret;
354 
355 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
356 	    dip, sysino);
357 
358 	if ((ret = hvio_intr_getstate(DIP_TO_HANDLE(dip),
359 	    sysino, intr_state)) != H_EOK) {
360 		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
361 		    ret);
362 		return (DDI_FAILURE);
363 	}
364 
365 	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
366 	    *intr_state);
367 
368 	return (DDI_SUCCESS);
369 }
370 
371 /*ARGSUSED*/
372 int
373 px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
374     intr_state_t intr_state)
375 {
376 	uint64_t	ret;
377 
378 	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
379 	    "intr_state 0x%x\n", dip, sysino, intr_state);
380 
381 	if ((ret = hvio_intr_setstate(DIP_TO_HANDLE(dip),
382 	    sysino, intr_state)) != H_EOK) {
383 		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
384 		    ret);
385 		return (DDI_FAILURE);
386 	}
387 
388 	return (DDI_SUCCESS);
389 }
390 
391 /*ARGSUSED*/
392 int
393 px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
394 {
395 	uint64_t	ret;
396 
397 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
398 	    dip, sysino);
399 
400 	if ((ret = hvio_intr_gettarget(DIP_TO_HANDLE(dip),
401 	    sysino, cpuid)) != H_EOK) {
402 		DBG(DBG_LIB_INT, dip, "hvio_intr_gettarget failed, ret 0x%lx\n",
403 		    ret);
404 		return (DDI_FAILURE);
405 	}
406 
407 	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", cpuid);
408 
409 	return (DDI_SUCCESS);
410 }
411 
412 /*ARGSUSED*/
413 int
414 px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
415 {
416 	uint64_t	ret;
417 
418 	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
419 	    "cpuid 0x%x\n", dip, sysino, cpuid);
420 
421 	if ((ret = hvio_intr_settarget(DIP_TO_HANDLE(dip),
422 	    sysino, cpuid)) != H_EOK) {
423 		DBG(DBG_LIB_INT, dip, "hvio_intr_settarget failed, ret 0x%lx\n",
424 		    ret);
425 		return (DDI_FAILURE);
426 	}
427 
428 	return (DDI_SUCCESS);
429 }
430 
431 /*ARGSUSED*/
432 int
433 px_lib_intr_reset(dev_info_t *dip)
434 {
435 	devino_t	ino;
436 	sysino_t	sysino;
437 
438 	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);
439 
440 	/* Reset all Interrupts */
441 	for (ino = 0; ino < INTERRUPT_MAPPING_ENTRIES; ino++) {
442 		if (px_lib_intr_devino_to_sysino(dip, ino,
443 		    &sysino) != DDI_SUCCESS)
444 			return (BF_FATAL);
445 
446 		if (px_lib_intr_setstate(dip, sysino,
447 		    INTR_IDLE_STATE) != DDI_SUCCESS)
448 			return (BF_FATAL);
449 	}
450 
451 	return (BF_NONE);
452 }
453 
454 /*ARGSUSED*/
455 int
456 px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
457     io_attributes_t io_attributes, void *addr, size_t pfn_index,
458     int flag)
459 {
460 	px_t		*px_p = DIP_TO_STATE(dip);
461 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
462 	uint64_t	ret;
463 
464 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
465 	    "pages 0x%x atrr 0x%x addr 0x%p pfn_index 0x%llx, flag 0x%x\n",
466 	    dip, tsbid, pages, io_attributes, addr, pfn_index, flag);
467 
468 	if ((ret = hvio_iommu_map(px_p->px_dev_hdl, pxu_p, tsbid, pages,
469 	    io_attributes, addr, pfn_index, flag)) != H_EOK) {
470 		DBG(DBG_LIB_DMA, dip,
471 		    "px_lib_iommu_map failed, ret 0x%lx\n", ret);
472 		return (DDI_FAILURE);
473 	}
474 
475 	return (DDI_SUCCESS);
476 }
477 
478 /*ARGSUSED*/
479 int
480 px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
481 {
482 	px_t		*px_p = DIP_TO_STATE(dip);
483 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
484 	uint64_t	ret;
485 
486 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
487 	    "pages 0x%x\n", dip, tsbid, pages);
488 
489 	if ((ret = hvio_iommu_demap(px_p->px_dev_hdl, pxu_p, tsbid, pages))
490 	    != H_EOK) {
491 		DBG(DBG_LIB_DMA, dip,
492 		    "px_lib_iommu_demap failed, ret 0x%lx\n", ret);
493 
494 		return (DDI_FAILURE);
495 	}
496 
497 	return (DDI_SUCCESS);
498 }
499 
500 /*ARGSUSED*/
501 int
502 px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid,
503     io_attributes_t *attributes_p, r_addr_t *r_addr_p)
504 {
505 	px_t	*px_p = DIP_TO_STATE(dip);
506 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
507 	uint64_t	ret;
508 
509 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
510 	    dip, tsbid);
511 
512 	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), pxu_p, tsbid,
513 	    attributes_p, r_addr_p)) != H_EOK) {
514 		DBG(DBG_LIB_DMA, dip,
515 		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);
516 
517 		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
518 	}
519 
520 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%x r_addr 0x%llx\n",
521 	    *attributes_p, *r_addr_p);
522 
523 	return (DDI_SUCCESS);
524 }
525 
526 
527 /*
528  * Checks dma attributes against system bypass ranges
529  * The bypass range is determined by the hardware. Return them so the
530  * common code can do generic checking against them.
531  */
532 /*ARGSUSED*/
533 int
534 px_lib_dma_bypass_rngchk(ddi_dma_attr_t *attrp, uint64_t *lo_p, uint64_t *hi_p)
535 {
536 	*lo_p = MMU_BYPASS_BASE;
537 	*hi_p = MMU_BYPASS_END;
538 
539 	return (DDI_SUCCESS);
540 }
541 
542 
543 /*ARGSUSED*/
544 int
545 px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra,
546     io_attributes_t io_attributes, io_addr_t *io_addr_p)
547 {
548 	uint64_t	ret;
549 
550 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
551 	    "attr 0x%x\n", dip, ra, io_attributes);
552 
553 	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), ra,
554 	    io_attributes, io_addr_p)) != H_EOK) {
555 		DBG(DBG_LIB_DMA, dip,
556 		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
557 		return (DDI_FAILURE);
558 	}
559 
560 	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
561 	    *io_addr_p);
562 
563 	return (DDI_SUCCESS);
564 }
565 
566 /*
567  * bus dma sync entry point.
568  */
569 /*ARGSUSED*/
570 int
571 px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
572 	off_t off, size_t len, uint_t cache_flags)
573 {
574 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
575 
576 	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
577 	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
578 	    dip, rdip, handle, off, len, cache_flags);
579 
580 	/*
581 	 * jbus_stst_order is found only in certain cpu modules.
582 	 * Just return success if not present.
583 	 */
584 	if (&jbus_stst_order == NULL)
585 		return (DDI_SUCCESS);
586 
587 	if (!(mp->dmai_flags & DMAI_FLAGS_INUSE)) {
588 		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
589 		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
590 
591 		return (DDI_FAILURE);
592 	}
593 
594 	if (mp->dmai_flags & DMAI_FLAGS_NOSYNC)
595 		return (DDI_SUCCESS);
596 
597 	/*
598 	 * No flush needed when sending data from memory to device.
599 	 * Nothing to do to "sync" memory to what device would already see.
600 	 */
601 	if (!(mp->dmai_rflags & DDI_DMA_READ) ||
602 	    ((cache_flags & PX_DMA_SYNC_DDI_FLAGS) == DDI_DMA_SYNC_FORDEV))
603 		return (DDI_SUCCESS);
604 
605 	/*
606 	 * Perform necessary cpu workaround to ensure jbus ordering.
607 	 * CPU's internal "invalidate FIFOs" are flushed.
608 	 */
609 
610 #if !defined(lint)
611 	kpreempt_disable();
612 #endif
613 	jbus_stst_order();
614 #if !defined(lint)
615 	kpreempt_enable();
616 #endif
617 	return (DDI_SUCCESS);
618 }
619 
620 /*
621  * MSIQ Functions:
622  */
623 /*ARGSUSED*/
624 int
625 px_lib_msiq_init(dev_info_t *dip)
626 {
627 	px_t		*px_p = DIP_TO_STATE(dip);
628 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
629 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
630 	caddr_t		msiq_addr;
631 	px_dvma_addr_t	pg_index;
632 	size_t		size;
633 	int		ret;
634 
635 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);
636 
637 	/*
638 	 * Map the EQ memory into the Fire MMU (has to be 512KB aligned)
639 	 * and then initialize the base address register.
640 	 *
641 	 * Allocate entries from Fire IOMMU so that the resulting address
642 	 * is properly aligned.  Calculate the index of the first allocated
643 	 * entry.  Note: The size of the mapping is assumed to be a multiple
644 	 * of the page size.
645 	 */
646 	msiq_addr = (caddr_t)(((uint64_t)msiq_state_p->msiq_buf_p +
647 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
648 
649 	size = msiq_state_p->msiq_cnt *
650 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
651 
652 	pxu_p->msiq_mapped_p = vmem_xalloc(px_p->px_mmu_p->mmu_dvma_map,
653 	    size, (512 * 1024), 0, 0, NULL, NULL, VM_NOSLEEP | VM_BESTFIT);
654 
655 	if (pxu_p->msiq_mapped_p == NULL)
656 		return (DDI_FAILURE);
657 
658 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
659 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
660 
661 	if ((ret = px_lib_iommu_map(px_p->px_dip, PCI_TSBID(0, pg_index),
662 	    MMU_BTOP(size), PCI_MAP_ATTR_WRITE, (void *)msiq_addr, 0,
663 	    MMU_MAP_BUF)) != DDI_SUCCESS) {
664 		DBG(DBG_LIB_MSIQ, dip,
665 		    "hvio_msiq_init failed, ret 0x%lx\n", ret);
666 
667 		(void) px_lib_msiq_fini(dip);
668 		return (DDI_FAILURE);
669 	}
670 
671 	(void) hvio_msiq_init(DIP_TO_HANDLE(dip), pxu_p);
672 
673 	return (DDI_SUCCESS);
674 }
675 
676 /*ARGSUSED*/
677 int
678 px_lib_msiq_fini(dev_info_t *dip)
679 {
680 	px_t		*px_p = DIP_TO_STATE(dip);
681 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
682 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
683 	px_dvma_addr_t	pg_index;
684 	size_t		size;
685 
686 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
687 
688 	/*
689 	 * Unmap and free the EQ memory that had been mapped
690 	 * into the Fire IOMMU.
691 	 */
692 	size = msiq_state_p->msiq_cnt *
693 	    msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
694 
695 	pg_index = MMU_PAGE_INDEX(px_p->px_mmu_p,
696 	    MMU_BTOP((ulong_t)pxu_p->msiq_mapped_p));
697 
698 	(void) px_lib_iommu_demap(px_p->px_dip,
699 	    PCI_TSBID(0, pg_index), MMU_BTOP(size));
700 
701 	/* Free the entries from the Fire MMU */
702 	vmem_xfree(px_p->px_mmu_p->mmu_dvma_map,
703 	    (void *)pxu_p->msiq_mapped_p, size);
704 
705 	return (DDI_SUCCESS);
706 }
707 
708 /*ARGSUSED*/
709 int
710 px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
711     uint_t *msiq_rec_cnt_p)
712 {
713 	px_t		*px_p = DIP_TO_STATE(dip);
714 	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
715 	uint64_t	*msiq_addr;
716 	size_t		msiq_size;
717 
718 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
719 	    dip, msiq_id);
720 
721 	msiq_addr = (uint64_t *)(((uint64_t)msiq_state_p->msiq_buf_p +
722 	    (MMU_PAGE_SIZE - 1)) >> MMU_PAGE_SHIFT << MMU_PAGE_SHIFT);
723 	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);
724 	ra_p = (r_addr_t *)((caddr_t)msiq_addr + (msiq_id * msiq_size));
725 
726 	*msiq_rec_cnt_p = msiq_state_p->msiq_rec_cnt;
727 
728 	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
729 	    ra_p, *msiq_rec_cnt_p);
730 
731 	return (DDI_SUCCESS);
732 }
733 
734 /*ARGSUSED*/
735 int
736 px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
737     pci_msiq_valid_state_t *msiq_valid_state)
738 {
739 	uint64_t	ret;
740 
741 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
742 	    dip, msiq_id);
743 
744 	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
745 	    msiq_id, msiq_valid_state)) != H_EOK) {
746 		DBG(DBG_LIB_MSIQ, dip,
747 		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
748 		return (DDI_FAILURE);
749 	}
750 
751 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
752 	    *msiq_valid_state);
753 
754 	return (DDI_SUCCESS);
755 }
756 
757 /*ARGSUSED*/
758 int
759 px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
760     pci_msiq_valid_state_t msiq_valid_state)
761 {
762 	uint64_t	ret;
763 
764 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
765 	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);
766 
767 	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
768 	    msiq_id, msiq_valid_state)) != H_EOK) {
769 		DBG(DBG_LIB_MSIQ, dip,
770 		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
771 		return (DDI_FAILURE);
772 	}
773 
774 	return (DDI_SUCCESS);
775 }
776 
777 /*ARGSUSED*/
778 int
779 px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
780     pci_msiq_state_t *msiq_state)
781 {
782 	uint64_t	ret;
783 
784 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
785 	    dip, msiq_id);
786 
787 	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
788 	    msiq_id, msiq_state)) != H_EOK) {
789 		DBG(DBG_LIB_MSIQ, dip,
790 		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
791 		return (DDI_FAILURE);
792 	}
793 
794 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
795 	    *msiq_state);
796 
797 	return (DDI_SUCCESS);
798 }
799 
800 /*ARGSUSED*/
801 int
802 px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
803     pci_msiq_state_t msiq_state)
804 {
805 	uint64_t	ret;
806 
807 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
808 	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);
809 
810 	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
811 	    msiq_id, msiq_state)) != H_EOK) {
812 		DBG(DBG_LIB_MSIQ, dip,
813 		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
814 		return (DDI_FAILURE);
815 	}
816 
817 	return (DDI_SUCCESS);
818 }
819 
820 /*ARGSUSED*/
821 int
822 px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
823     msiqhead_t *msiq_head)
824 {
825 	uint64_t	ret;
826 
827 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
828 	    dip, msiq_id);
829 
830 	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
831 	    msiq_id, msiq_head)) != H_EOK) {
832 		DBG(DBG_LIB_MSIQ, dip,
833 		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
834 		return (DDI_FAILURE);
835 	}
836 
837 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: msiq_head 0x%x\n",
838 	    *msiq_head);
839 
840 	return (DDI_SUCCESS);
841 }
842 
843 /*ARGSUSED*/
844 int
845 px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
846     msiqhead_t msiq_head)
847 {
848 	uint64_t	ret;
849 
850 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
851 	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);
852 
853 	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
854 	    msiq_id, msiq_head)) != H_EOK) {
855 		DBG(DBG_LIB_MSIQ, dip,
856 		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
857 		return (DDI_FAILURE);
858 	}
859 
860 	return (DDI_SUCCESS);
861 }
862 
863 /*ARGSUSED*/
864 int
865 px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
866     msiqtail_t *msiq_tail)
867 {
868 	uint64_t	ret;
869 
870 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
871 	    dip, msiq_id);
872 
873 	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
874 	    msiq_id, msiq_tail)) != H_EOK) {
875 		DBG(DBG_LIB_MSIQ, dip,
876 		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
877 		return (DDI_FAILURE);
878 	}
879 
880 	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
881 	    *msiq_tail);
882 
883 	return (DDI_SUCCESS);
884 }
885 
886 /*ARGSUSED*/
887 void
888 px_lib_get_msiq_rec(dev_info_t *dip, px_msiq_t *msiq_p, msiq_rec_t *msiq_rec_p)
889 {
890 	eq_rec_t	*eq_rec_p = (eq_rec_t *)msiq_p->msiq_curr;
891 
892 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p eq_rec_p 0x%p\n",
893 	    dip, eq_rec_p);
894 
895 	if (!eq_rec_p->eq_rec_rid) {
896 		/* Set msiq_rec_rid to zero */
897 		msiq_rec_p->msiq_rec_rid = 0;
898 
899 		return;
900 	}
901 
902 	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: EQ RECORD, "
903 	    "eq_rec_rid 0x%llx eq_rec_fmt_type 0x%llx "
904 	    "eq_rec_len 0x%llx eq_rec_addr0 0x%llx "
905 	    "eq_rec_addr1 0x%llx eq_rec_data0 0x%llx "
906 	    "eq_rec_data1 0x%llx\n", eq_rec_p->eq_rec_rid,
907 	    eq_rec_p->eq_rec_fmt_type, eq_rec_p->eq_rec_len,
908 	    eq_rec_p->eq_rec_addr0, eq_rec_p->eq_rec_addr1,
909 	    eq_rec_p->eq_rec_data0, eq_rec_p->eq_rec_data1);
910 
911 	/*
912 	 * Only upper 4 bits of eq_rec_fmt_type is used
913 	 * to identify the EQ record type.
914 	 */
915 	switch (eq_rec_p->eq_rec_fmt_type >> 3) {
916 	case EQ_REC_MSI32:
917 		msiq_rec_p->msiq_rec_type = MSI32_REC;
918 
919 		msiq_rec_p->msiq_rec_data.msi.msi_data =
920 		    eq_rec_p->eq_rec_data0;
921 		break;
922 	case EQ_REC_MSI64:
923 		msiq_rec_p->msiq_rec_type = MSI64_REC;
924 
925 		msiq_rec_p->msiq_rec_data.msi.msi_data =
926 		    eq_rec_p->eq_rec_data0;
927 		break;
928 	case EQ_REC_MSG:
929 		msiq_rec_p->msiq_rec_type = MSG_REC;
930 
931 		msiq_rec_p->msiq_rec_data.msg.msg_route =
932 		    eq_rec_p->eq_rec_fmt_type & 7;
933 		msiq_rec_p->msiq_rec_data.msg.msg_targ = eq_rec_p->eq_rec_rid;
934 		msiq_rec_p->msiq_rec_data.msg.msg_code = eq_rec_p->eq_rec_data0;
935 		break;
936 	default:
937 		cmn_err(CE_WARN, "%s%d: px_lib_get_msiq_rec: "
938 		    "0x%lx is an unknown EQ record type",
939 		    ddi_driver_name(dip), ddi_get_instance(dip),
940 		    eq_rec_p->eq_rec_fmt_type);
941 		break;
942 	}
943 
944 	msiq_rec_p->msiq_rec_rid = eq_rec_p->eq_rec_rid;
945 	msiq_rec_p->msiq_rec_msi_addr = ((eq_rec_p->eq_rec_addr1 << 16) |
946 	    (eq_rec_p->eq_rec_addr0 << 2));
947 
948 	/* Zero out eq_rec_rid field */
949 	eq_rec_p->eq_rec_rid = 0;
950 }
951 
952 /*
953  * MSI Functions:
954  */
955 /*ARGSUSED*/
956 int
957 px_lib_msi_init(dev_info_t *dip)
958 {
959 	px_t		*px_p = DIP_TO_STATE(dip);
960 	px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
961 	uint64_t	ret;
962 
963 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);
964 
965 	if ((ret = hvio_msi_init(DIP_TO_HANDLE(dip),
966 	    msi_state_p->msi_addr32, msi_state_p->msi_addr64)) != H_EOK) {
967 		DBG(DBG_LIB_MSIQ, dip, "px_lib_msi_init failed, ret 0x%lx\n",
968 		    ret);
969 		return (DDI_FAILURE);
970 	}
971 
972 	return (DDI_SUCCESS);
973 }
974 
975 /*ARGSUSED*/
976 int
977 px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
978     msiqid_t *msiq_id)
979 {
980 	uint64_t	ret;
981 
982 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
983 	    dip, msi_num);
984 
985 	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
986 	    msi_num, msiq_id)) != H_EOK) {
987 		DBG(DBG_LIB_MSI, dip,
988 		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
989 		return (DDI_FAILURE);
990 	}
991 
992 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
993 	    *msiq_id);
994 
995 	return (DDI_SUCCESS);
996 }
997 
998 /*ARGSUSED*/
999 int
1000 px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
1001     msiqid_t msiq_id, msi_type_t msitype)
1002 {
1003 	uint64_t	ret;
1004 
1005 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
1006 	    "msq_id 0x%x\n", dip, msi_num, msiq_id);
1007 
1008 	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
1009 	    msi_num, msiq_id)) != H_EOK) {
1010 		DBG(DBG_LIB_MSI, dip,
1011 		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
1012 		return (DDI_FAILURE);
1013 	}
1014 
1015 	return (DDI_SUCCESS);
1016 }
1017 
1018 /*ARGSUSED*/
1019 int
1020 px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
1021     pci_msi_valid_state_t *msi_valid_state)
1022 {
1023 	uint64_t	ret;
1024 
1025 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
1026 	    dip, msi_num);
1027 
1028 	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
1029 	    msi_num, msi_valid_state)) != H_EOK) {
1030 		DBG(DBG_LIB_MSI, dip,
1031 		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
1032 		return (DDI_FAILURE);
1033 	}
1034 
1035 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
1036 	    *msi_valid_state);
1037 
1038 	return (DDI_SUCCESS);
1039 }
1040 
1041 /*ARGSUSED*/
1042 int
1043 px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
1044     pci_msi_valid_state_t msi_valid_state)
1045 {
1046 	uint64_t	ret;
1047 
1048 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
1049 	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);
1050 
1051 	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
1052 	    msi_num, msi_valid_state)) != H_EOK) {
1053 		DBG(DBG_LIB_MSI, dip,
1054 		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
1055 		return (DDI_FAILURE);
1056 	}
1057 
1058 	return (DDI_SUCCESS);
1059 }
1060 
1061 /*ARGSUSED*/
1062 int
1063 px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
1064     pci_msi_state_t *msi_state)
1065 {
1066 	uint64_t	ret;
1067 
1068 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
1069 	    dip, msi_num);
1070 
1071 	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
1072 	    msi_num, msi_state)) != H_EOK) {
1073 		DBG(DBG_LIB_MSI, dip,
1074 		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
1075 		return (DDI_FAILURE);
1076 	}
1077 
1078 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
1079 	    *msi_state);
1080 
1081 	return (DDI_SUCCESS);
1082 }
1083 
1084 /*ARGSUSED*/
1085 int
1086 px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
1087     pci_msi_state_t msi_state)
1088 {
1089 	uint64_t	ret;
1090 
1091 	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
1092 	    "msi_state 0x%x\n", dip, msi_num, msi_state);
1093 
1094 	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
1095 	    msi_num, msi_state)) != H_EOK) {
1096 		DBG(DBG_LIB_MSI, dip,
1097 		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
1098 		return (DDI_FAILURE);
1099 	}
1100 
1101 	return (DDI_SUCCESS);
1102 }
1103 
1104 /*
1105  * MSG Functions:
1106  */
1107 /*ARGSUSED*/
1108 int
1109 px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
1110     msiqid_t *msiq_id)
1111 {
1112 	uint64_t	ret;
1113 
1114 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
1115 	    dip, msg_type);
1116 
1117 	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
1118 	    msg_type, msiq_id)) != H_EOK) {
1119 		DBG(DBG_LIB_MSG, dip,
1120 		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
1121 		return (DDI_FAILURE);
1122 	}
1123 
1124 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
1125 	    *msiq_id);
1126 
1127 	return (DDI_SUCCESS);
1128 }
1129 
1130 /*ARGSUSED*/
1131 int
1132 px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
1133     msiqid_t msiq_id)
1134 {
1135 	uint64_t	ret;
1136 
1137 	DBG(DBG_LIB_MSG, dip, "px_lib_msi_setstate: dip 0x%p msg_type 0x%x "
1138 	    "msiq_id 0x%x\n", dip, msg_type, msiq_id);
1139 
1140 	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
1141 	    msg_type, msiq_id)) != H_EOK) {
1142 		DBG(DBG_LIB_MSG, dip,
1143 		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
1144 		return (DDI_FAILURE);
1145 	}
1146 
1147 	return (DDI_SUCCESS);
1148 }
1149 
1150 /*ARGSUSED*/
1151 int
1152 px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
1153     pcie_msg_valid_state_t *msg_valid_state)
1154 {
1155 	uint64_t	ret;
1156 
1157 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
1158 	    dip, msg_type);
1159 
1160 	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
1161 	    msg_valid_state)) != H_EOK) {
1162 		DBG(DBG_LIB_MSG, dip,
1163 		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
1164 		return (DDI_FAILURE);
1165 	}
1166 
1167 	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
1168 	    *msg_valid_state);
1169 
1170 	return (DDI_SUCCESS);
1171 }
1172 
1173 /*ARGSUSED*/
1174 int
1175 px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
1176     pcie_msg_valid_state_t msg_valid_state)
1177 {
1178 	uint64_t	ret;
1179 
1180 	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
1181 	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);
1182 
1183 	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
1184 	    msg_valid_state)) != H_EOK) {
1185 		DBG(DBG_LIB_MSG, dip,
1186 		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
1187 		return (DDI_FAILURE);
1188 	}
1189 
1190 	return (DDI_SUCCESS);
1191 }
1192 
1193 /*
1194  * Suspend/Resume Functions:
1195  * Currently unsupported by hypervisor
1196  */
1197 int
1198 px_lib_suspend(dev_info_t *dip)
1199 {
1200 	px_t		*px_p = DIP_TO_STATE(dip);
1201 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1202 	devhandle_t	dev_hdl, xbus_dev_hdl;
1203 	uint64_t	ret;
1204 
1205 	DBG(DBG_DETACH, dip, "px_lib_suspend: dip 0x%p\n", dip);
1206 
1207 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
1208 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
1209 
1210 	if ((ret = hvio_suspend(dev_hdl, pxu_p)) == H_EOK) {
1211 		px_p->px_cb_p->xbc_attachcnt--;
1212 		if (px_p->px_cb_p->xbc_attachcnt == 0)
1213 			if ((ret = hvio_cb_suspend(xbus_dev_hdl, pxu_p))
1214 			    != H_EOK)
1215 				px_p->px_cb_p->xbc_attachcnt++;
1216 	}
1217 
1218 	return ((ret != H_EOK) ? DDI_FAILURE: DDI_SUCCESS);
1219 }
1220 
1221 void
1222 px_lib_resume(dev_info_t *dip)
1223 {
1224 	px_t		*px_p = DIP_TO_STATE(dip);
1225 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1226 	devhandle_t	dev_hdl, xbus_dev_hdl;
1227 	devino_t	pec_ino = px_p->px_inos[PX_INTR_PEC];
1228 	devino_t	xbc_ino = px_p->px_inos[PX_INTR_XBC];
1229 
1230 	DBG(DBG_ATTACH, dip, "px_lib_resume: dip 0x%p\n", dip);
1231 
1232 	dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_CSR];
1233 	xbus_dev_hdl = (devhandle_t)pxu_p->px_address[PX_REG_XBC];
1234 
1235 	px_p->px_cb_p->xbc_attachcnt++;
1236 	if (px_p->px_cb_p->xbc_attachcnt == 1)
1237 		hvio_cb_resume(dev_hdl, xbus_dev_hdl, xbc_ino, pxu_p);
1238 	hvio_resume(dev_hdl, pec_ino, pxu_p);
1239 }
1240 
1241 /*
1242  * PCI tool Functions:
1243  * Currently unsupported by hypervisor
1244  */
1245 /*ARGSUSED*/
1246 int
1247 px_lib_tools_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
1248 {
1249 	px_t *px_p = DIP_TO_STATE(dip);
1250 
1251 	DBG(DBG_TOOLS, dip, "px_lib_tools_dev_reg_ops: dip 0x%p arg 0x%p "
1252 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
1253 
1254 	return (px_dev_reg_ops(dip, arg, cmd, mode, px_p));
1255 }
1256 
1257 /*ARGSUSED*/
1258 int
1259 px_lib_tools_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
1260 {
1261 	DBG(DBG_TOOLS, dip, "px_lib_tools_bus_reg_ops: dip 0x%p arg 0x%p "
1262 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
1263 
1264 	return (px_bus_reg_ops(dip, arg, cmd, mode));
1265 }
1266 
1267 /*ARGSUSED*/
1268 int
1269 px_lib_tools_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
1270 {
1271 	px_t *px_p = DIP_TO_STATE(dip);
1272 
1273 	DBG(DBG_TOOLS, dip, "px_lib_tools_intr_admn: dip 0x%p arg 0x%p "
1274 	    "cmd 0x%x mode 0x%x\n", dip, arg, cmd, mode);
1275 
1276 	return (px_intr_admn(dip, arg, cmd, mode, px_p));
1277 }
1278 
1279 /*
1280  * Misc Functions:
1281  * Currently unsupported by hypervisor
1282  */
1283 uint64_t
1284 px_lib_get_cb(dev_info_t *dip)
1285 {
1286 	px_t	*px_p = DIP_TO_STATE(dip);
1287 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
1288 
1289 	return (CSR_XR((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1));
1290 }
1291 
1292 void
1293 px_lib_set_cb(dev_info_t *dip, uint64_t val)
1294 {
1295 	px_t	*px_p = DIP_TO_STATE(dip);
1296 	pxu_t	*pxu_p = (pxu_t *)px_p->px_plat_p;
1297 
1298 	CSR_XS((caddr_t)pxu_p->px_address[PX_REG_XBC], JBUS_SCRATCH_1, val);
1299 }
1300 
1301 /*ARGSUSED*/
1302 int
1303 px_lib_map_vconfig(dev_info_t *dip,
1304 	ddi_map_req_t *mp, pci_config_offset_t off,
1305 		pci_regspec_t *rp, caddr_t *addrp)
1306 {
1307 	/*
1308 	 * No special config space access services in this layer.
1309 	 */
1310 	return (DDI_FAILURE);
1311 }
1312 
1313 static void
1314 px_lib_clr_errs(px_t *px_p, px_pec_t *pec_p)
1315 {
1316 	dev_info_t	*rpdip = px_p->px_dip;
1317 	px_cb_t		*cb_p = px_p->px_cb_p;
1318 	int		err = PX_OK, ret;
1319 	int		acctype = pec_p->pec_safeacc_type;
1320 	ddi_fm_error_t	derr;
1321 
1322 	/* Create the derr */
1323 	bzero(&derr, sizeof (ddi_fm_error_t));
1324 	derr.fme_version = DDI_FME_VERSION;
1325 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
1326 	derr.fme_flag = acctype;
1327 
1328 	if (acctype == DDI_FM_ERR_EXPECTED) {
1329 		derr.fme_status = DDI_FM_NONFATAL;
1330 		ndi_fm_acc_err_set(pec_p->pec_acc_hdl, &derr);
1331 	}
1332 
1333 	mutex_enter(&cb_p->xbc_fm_mutex);
1334 
1335 	/* send ereport/handle/clear fire registers */
1336 	err = px_err_handle(px_p, &derr, PX_LIB_CALL, B_TRUE);
1337 
1338 	/* Check all child devices for errors */
1339 	ret = ndi_fm_handler_dispatch(rpdip, NULL, &derr);
1340 
1341 	mutex_exit(&cb_p->xbc_fm_mutex);
1342 
1343 	/*
1344 	 * PX_FATAL_HW indicates a condition recovered from Fatal-Reset,
1345 	 * therefore it does not cause panic.
1346 	 */
1347 	if ((err & (PX_FATAL_GOS | PX_FATAL_SW)) || (ret == DDI_FM_FATAL))
1348 		fm_panic("Fatal System Port Error has occurred\n");
1349 }
1350 
1351 #ifdef  DEBUG
1352 int	px_peekfault_cnt = 0;
1353 int	px_pokefault_cnt = 0;
1354 #endif  /* DEBUG */
1355 
1356 /*ARGSUSED*/
1357 static int
1358 px_lib_do_poke(dev_info_t *dip, dev_info_t *rdip,
1359     peekpoke_ctlops_t *in_args)
1360 {
1361 	px_t *px_p = DIP_TO_STATE(dip);
1362 	px_pec_t *pec_p = px_p->px_pec_p;
1363 	int err = DDI_SUCCESS;
1364 	on_trap_data_t otd;
1365 
1366 	mutex_enter(&pec_p->pec_pokefault_mutex);
1367 	pec_p->pec_ontrap_data = &otd;
1368 	pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
1369 
1370 	/* Set up protected environment. */
1371 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
1372 		uintptr_t tramp = otd.ot_trampoline;
1373 
1374 		otd.ot_trampoline = (uintptr_t)&poke_fault;
1375 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
1376 		    (void *)in_args->host_addr);
1377 		otd.ot_trampoline = tramp;
1378 	} else
1379 		err = DDI_FAILURE;
1380 
1381 	px_lib_clr_errs(px_p, pec_p);
1382 
1383 	if (otd.ot_trap & OT_DATA_ACCESS)
1384 		err = DDI_FAILURE;
1385 
1386 	/* Take down protected environment. */
1387 	no_trap();
1388 
1389 	pec_p->pec_ontrap_data = NULL;
1390 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1391 	mutex_exit(&pec_p->pec_pokefault_mutex);
1392 
1393 #ifdef  DEBUG
1394 	if (err == DDI_FAILURE)
1395 		px_pokefault_cnt++;
1396 #endif
1397 	return (err);
1398 }
1399 
1400 /*ARGSUSED*/
1401 static int
1402 px_lib_do_caut_put(dev_info_t *dip, dev_info_t *rdip,
1403     peekpoke_ctlops_t *cautacc_ctlops_arg)
1404 {
1405 	size_t size = cautacc_ctlops_arg->size;
1406 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
1407 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
1408 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
1409 	size_t repcount = cautacc_ctlops_arg->repcount;
1410 	uint_t flags = cautacc_ctlops_arg->flags;
1411 
1412 	px_t *px_p = DIP_TO_STATE(dip);
1413 	px_pec_t *pec_p = px_p->px_pec_p;
1414 	int err = DDI_SUCCESS;
1415 
1416 	/*
1417 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
1418 	 * mutex.
1419 	 */
1420 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1421 
1422 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
1423 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
1424 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
1425 
1426 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1427 		for (; repcount; repcount--) {
1428 			switch (size) {
1429 
1430 			case sizeof (uint8_t):
1431 				i_ddi_put8(hp, (uint8_t *)dev_addr,
1432 				    *(uint8_t *)host_addr);
1433 				break;
1434 
1435 			case sizeof (uint16_t):
1436 				i_ddi_put16(hp, (uint16_t *)dev_addr,
1437 				    *(uint16_t *)host_addr);
1438 				break;
1439 
1440 			case sizeof (uint32_t):
1441 				i_ddi_put32(hp, (uint32_t *)dev_addr,
1442 				    *(uint32_t *)host_addr);
1443 				break;
1444 
1445 			case sizeof (uint64_t):
1446 				i_ddi_put64(hp, (uint64_t *)dev_addr,
1447 				    *(uint64_t *)host_addr);
1448 				break;
1449 			}
1450 
1451 			host_addr += size;
1452 
1453 			if (flags == DDI_DEV_AUTOINCR)
1454 				dev_addr += size;
1455 
1456 			px_lib_clr_errs(px_p, pec_p);
1457 
1458 			if (pec_p->pec_ontrap_data->ot_trap & OT_DATA_ACCESS) {
1459 				err = DDI_FAILURE;
1460 #ifdef  DEBUG
1461 				px_pokefault_cnt++;
1462 #endif
1463 				break;
1464 			}
1465 		}
1466 	}
1467 
1468 	i_ddi_notrap((ddi_acc_handle_t)hp);
1469 	pec_p->pec_ontrap_data = NULL;
1470 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1471 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1472 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
1473 
1474 	return (err);
1475 }
1476 
1477 
1478 int
1479 px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
1480     peekpoke_ctlops_t *in_args)
1481 {
1482 	return (in_args->handle ? px_lib_do_caut_put(dip, rdip, in_args) :
1483 	    px_lib_do_poke(dip, rdip, in_args));
1484 }
1485 
1486 
1487 /*ARGSUSED*/
1488 static int
1489 px_lib_do_peek(dev_info_t *dip, peekpoke_ctlops_t *in_args)
1490 {
1491 	px_t *px_p = DIP_TO_STATE(dip);
1492 	px_pec_t *pec_p = px_p->px_pec_p;
1493 	int err = DDI_SUCCESS;
1494 	on_trap_data_t otd;
1495 
1496 	mutex_enter(&pec_p->pec_pokefault_mutex);
1497 	pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
1498 
1499 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
1500 		uintptr_t tramp = otd.ot_trampoline;
1501 
1502 		otd.ot_trampoline = (uintptr_t)&peek_fault;
1503 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
1504 		    (void *)in_args->host_addr);
1505 		otd.ot_trampoline = tramp;
1506 	} else
1507 		err = DDI_FAILURE;
1508 
1509 	no_trap();
1510 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1511 	mutex_exit(&pec_p->pec_pokefault_mutex);
1512 
1513 #ifdef  DEBUG
1514 	if (err == DDI_FAILURE)
1515 		px_peekfault_cnt++;
1516 #endif
1517 	return (err);
1518 }
1519 
1520 
1521 static int
1522 px_lib_do_caut_get(dev_info_t *dip, peekpoke_ctlops_t *cautacc_ctlops_arg)
1523 {
1524 	size_t size = cautacc_ctlops_arg->size;
1525 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
1526 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
1527 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
1528 	size_t repcount = cautacc_ctlops_arg->repcount;
1529 	uint_t flags = cautacc_ctlops_arg->flags;
1530 
1531 	px_t *px_p = DIP_TO_STATE(dip);
1532 	px_pec_t *pec_p = px_p->px_pec_p;
1533 	int err = DDI_SUCCESS;
1534 
1535 	/*
1536 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
1537 	 * mutex.
1538 	 */
1539 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1540 
1541 	pec_p->pec_ontrap_data = (on_trap_data_t *)hp->ahi_err->err_ontrap;
1542 	pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
1543 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
1544 
1545 	if (repcount == 1) {
1546 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1547 			i_ddi_caut_get(size, (void *)dev_addr,
1548 			    (void *)host_addr);
1549 		} else {
1550 			int i;
1551 			uint8_t *ff_addr = (uint8_t *)host_addr;
1552 			for (i = 0; i < size; i++)
1553 				*ff_addr++ = 0xff;
1554 
1555 			err = DDI_FAILURE;
1556 #ifdef  DEBUG
1557 			px_peekfault_cnt++;
1558 #endif
1559 		}
1560 	} else {
1561 		if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1562 			for (; repcount; repcount--) {
1563 				i_ddi_caut_get(size, (void *)dev_addr,
1564 				    (void *)host_addr);
1565 
1566 				host_addr += size;
1567 
1568 				if (flags == DDI_DEV_AUTOINCR)
1569 					dev_addr += size;
1570 			}
1571 		} else {
1572 			err = DDI_FAILURE;
1573 #ifdef  DEBUG
1574 			px_peekfault_cnt++;
1575 #endif
1576 		}
1577 	}
1578 
1579 	i_ddi_notrap((ddi_acc_handle_t)hp);
1580 	pec_p->pec_ontrap_data = NULL;
1581 	pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
1582 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1583 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
1584 
1585 	return (err);
1586 }
1587 
1588 /*ARGSUSED*/
1589 int
1590 px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
1591     peekpoke_ctlops_t *in_args, void *result)
1592 {
1593 	result = (void *)in_args->host_addr;
1594 	return (in_args->handle ? px_lib_do_caut_get(dip, in_args) :
1595 	    px_lib_do_peek(dip, in_args));
1596 }
1597 
1598 /*
1599  * implements PPM interface
1600  */
1601 int
1602 px_lib_pmctl(int cmd, px_t *px_p)
1603 {
1604 	ASSERT((cmd & ~PPMREQ_MASK) == PPMREQ);
1605 	switch (cmd) {
1606 	case PPMREQ_PRE_PWR_OFF:
1607 		/*
1608 		 * Currently there is no device power management for
1609 		 * the root complex (fire). When there is we need to make
1610 		 * sure that it is at full power before trying to send the
1611 		 * PME_Turn_Off message.
1612 		 */
1613 		DBG(DBG_PWR, px_p->px_dip,
1614 		    "ioctl: request to send PME_Turn_Off\n");
1615 		return (px_goto_l23ready(px_p));
1616 
1617 	case PPMREQ_PRE_PWR_ON:
1618 		DBG(DBG_PWR, px_p->px_dip, "ioctl: PRE_PWR_ON request\n");
1619 		return (px_pre_pwron_check(px_p));
1620 
1621 	case PPMREQ_POST_PWR_ON:
1622 		DBG(DBG_PWR, px_p->px_dip, "ioctl: POST_PWR_ON request\n");
1623 		return (px_goto_l0(px_p));
1624 
1625 	default:
1626 		return (DDI_FAILURE);
1627 	}
1628 }
1629 
1630 /*
1631  * sends PME_Turn_Off message to put the link in L2/L3 ready state.
1632  * called by px_ioctl.
1633  * returns DDI_SUCCESS or DDI_FAILURE
1634  * 1. Wait for link to be in L1 state (link status reg)
1635  * 2. write to PME_Turn_off reg to boradcast
1636  * 3. set timeout
1637  * 4. If timeout, return failure.
1638  * 5. If PM_TO_Ack, wait till link is in L2/L3 ready
1639  */
1640 static int
1641 px_goto_l23ready(px_t *px_p)
1642 {
1643 	pcie_pwr_t	*pwr_p;
1644 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1645 	caddr_t	csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1646 	int		ret = DDI_SUCCESS;
1647 	clock_t		end, timeleft;
1648 	int		mutex_held = 1;
1649 
1650 	/* If no PM info, return failure */
1651 	if (!PCIE_PMINFO(px_p->px_dip) ||
1652 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1653 		return (DDI_FAILURE);
1654 
1655 	mutex_enter(&pwr_p->pwr_lock);
1656 	mutex_enter(&px_p->px_l23ready_lock);
1657 	/* Clear the PME_To_ACK receieved flag */
1658 	px_p->px_pm_flags &= ~PX_PMETOACK_RECVD;
1659 	if (px_send_pme_turnoff(csr_base) != DDI_SUCCESS) {
1660 		ret = DDI_FAILURE;
1661 		goto l23ready_done;
1662 	}
1663 	px_p->px_pm_flags |= PX_PME_TURNOFF_PENDING;
1664 
1665 	end = ddi_get_lbolt() + drv_usectohz(px_pme_to_ack_timeout);
1666 	while (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1667 		timeleft = cv_timedwait(&px_p->px_l23ready_cv,
1668 		    &px_p->px_l23ready_lock, end);
1669 		/*
1670 		 * if cv_timedwait returns -1, it is either
1671 		 * 1) timed out or
1672 		 * 2) there was a pre-mature wakeup but by the time
1673 		 * cv_timedwait is called again end < lbolt i.e.
1674 		 * end is in the past.
1675 		 * 3) By the time we make first cv_timedwait call,
1676 		 * end < lbolt is true.
1677 		 */
1678 		if (timeleft == -1)
1679 			break;
1680 	}
1681 	if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1682 		/*
1683 		 * Either timedout or interrupt didn't get a
1684 		 * chance to grab the mutex and set the flag.
1685 		 * release the mutex and delay for sometime.
1686 		 * This will 1) give a chance for interrupt to
1687 		 * set the flag 2) creates a delay between two
1688 		 * consequetive requests.
1689 		 */
1690 		mutex_exit(&px_p->px_l23ready_lock);
1691 		delay(5);
1692 		mutex_held = 0;
1693 		if (!(px_p->px_pm_flags & PX_PMETOACK_RECVD)) {
1694 			ret = DDI_FAILURE;
1695 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
1696 			    " for PME_TO_ACK\n");
1697 		}
1698 	}
1699 	px_p->px_pm_flags &= ~(PX_PME_TURNOFF_PENDING | PX_PMETOACK_RECVD);
1700 
1701 l23ready_done:
1702 	if (mutex_held)
1703 		mutex_exit(&px_p->px_l23ready_lock);
1704 	/*
1705 	 * Wait till link is in L1 idle, if sending PME_Turn_Off
1706 	 * was succesful.
1707 	 */
1708 	if (ret == DDI_SUCCESS) {
1709 		if (px_link_wait4l1idle(csr_base) != DDI_SUCCESS) {
1710 			DBG(DBG_PWR, px_p->px_dip, " Link is not at L1"
1711 			    "even though we received PME_To_ACK.\n");
1712 			ret = DDI_FAILURE;
1713 		} else
1714 			pwr_p->pwr_link_lvl = PM_LEVEL_L3;
1715 
1716 	}
1717 	mutex_exit(&pwr_p->pwr_lock);
1718 	return (ret);
1719 }
1720 
1721 /*
1722  * Message interrupt handler intended to be shared for both
1723  * PME and PME_TO_ACK msg handling, currently only handles
1724  * PME_To_ACK message.
1725  */
1726 uint_t
1727 px_pmeq_intr(caddr_t arg)
1728 {
1729 	px_t	*px_p = (px_t *)arg;
1730 
1731 	mutex_enter(&px_p->px_l23ready_lock);
1732 	cv_broadcast(&px_p->px_l23ready_cv);
1733 	if (px_p->px_pm_flags & PX_PME_TURNOFF_PENDING) {
1734 		px_p->px_pm_flags |= PX_PMETOACK_RECVD;
1735 	} else {
1736 		/*
1737 		 * This maybe the second ack received. If so then,
1738 		 * we should be receiving it during wait4L1 stage.
1739 		 */
1740 		px_p->px_pmetoack_ignored++;
1741 	}
1742 	mutex_exit(&px_p->px_l23ready_lock);
1743 	return (DDI_INTR_CLAIMED);
1744 }
1745 
1746 static int
1747 px_pre_pwron_check(px_t *px_p)
1748 {
1749 	pcie_pwr_t	*pwr_p;
1750 
1751 	/* If no PM info, return failure */
1752 	if (!PCIE_PMINFO(px_p->px_dip) ||
1753 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1754 		return (DDI_FAILURE);
1755 
1756 	return (pwr_p->pwr_link_lvl == PM_LEVEL_L3 ? DDI_SUCCESS : DDI_FAILURE);
1757 }
1758 
1759 static int
1760 px_goto_l0(px_t *px_p)
1761 {
1762 	pcie_pwr_t	*pwr_p;
1763 	pxu_t		*pxu_p = (pxu_t *)px_p->px_plat_p;
1764 	caddr_t csr_base = (caddr_t)pxu_p->px_address[PX_REG_CSR];
1765 	int		ret = DDI_SUCCESS;
1766 	clock_t		end, timeleft;
1767 	int		mutex_held = 1;
1768 
1769 	/* If no PM info, return failure */
1770 	if (!PCIE_PMINFO(px_p->px_dip) ||
1771 	    !(pwr_p = PCIE_NEXUS_PMINFO(px_p->px_dip)))
1772 		return (DDI_FAILURE);
1773 
1774 	mutex_enter(&pwr_p->pwr_lock);
1775 	mutex_enter(&px_p->px_lupsoft_lock);
1776 	/* Clear the LINKUP_RECVD receieved flag */
1777 	px_p->px_pm_flags &= ~PX_LINKUP_RECVD;
1778 	if (px_link_retrain(csr_base) != DDI_SUCCESS) {
1779 		ret = DDI_FAILURE;
1780 		goto l0_done;
1781 	}
1782 	px_p->px_pm_flags |= PX_LINKUP_PENDING;
1783 
1784 	end = ddi_get_lbolt() + drv_usectohz(px_linkup_timeout);
1785 	while (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1786 		timeleft = cv_timedwait(&px_p->px_lup_cv,
1787 		    &px_p->px_lupsoft_lock, end);
1788 		/*
1789 		 * if cv_timedwait returns -1, it is either
1790 		 * 1) timed out or
1791 		 * 2) there was a pre-mature wakeup but by the time
1792 		 * cv_timedwait is called again end < lbolt i.e.
1793 		 * end is in the past.
1794 		 * 3) By the time we make first cv_timedwait call,
1795 		 * end < lbolt is true.
1796 		 */
1797 		if (timeleft == -1)
1798 			break;
1799 	}
1800 	if (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1801 		/*
1802 		 * Either timedout or interrupt didn't get a
1803 		 * chance to grab the mutex and set the flag.
1804 		 * release the mutex and delay for sometime.
1805 		 * This will 1) give a chance for interrupt to
1806 		 * set the flag 2) creates a delay between two
1807 		 * consequetive requests.
1808 		 */
1809 		mutex_exit(&px_p->px_lupsoft_lock);
1810 		mutex_held = 0;
1811 		delay(5);
1812 		if (!(px_p->px_pm_flags & PX_LINKUP_RECVD)) {
1813 			ret = DDI_FAILURE;
1814 			DBG(DBG_PWR, px_p->px_dip, " Timed out while waiting"
1815 			    " for link up\n");
1816 		}
1817 	}
1818 	px_p->px_pm_flags &= ~(PX_LINKUP_PENDING | PX_LINKUP_RECVD);
1819 
1820 l0_done:
1821 	if (mutex_held)
1822 		mutex_exit(&px_p->px_lupsoft_lock);
1823 	if (ret == DDI_SUCCESS)
1824 		px_enable_detect_quiet(csr_base);
1825 	mutex_exit(&pwr_p->pwr_lock);
1826 	return (ret);
1827 }
1828 
1829 uint_t
1830 px_lup_softintr(caddr_t arg)
1831 {
1832 	px_t *px_p = (px_t *)arg;
1833 
1834 	mutex_enter(&px_p->px_lup_lock);
1835 	if (!(px_p->px_lupsoft_pending > 0)) {
1836 		/* Spurious */
1837 		mutex_exit(&px_p->px_lup_lock);
1838 		return (DDI_INTR_UNCLAIMED);
1839 	}
1840 	px_p->px_lupsoft_pending--;
1841 	if (px_p->px_lupsoft_pending > 0) {
1842 		/* More than one lup soft intr posted - unlikely */
1843 		mutex_exit(&px_p->px_lup_lock);
1844 		return (DDI_INTR_UNCLAIMED);
1845 	}
1846 	mutex_exit(&px_p->px_lup_lock);
1847 
1848 	mutex_enter(&px_p->px_lupsoft_lock);
1849 	cv_broadcast(&px_p->px_lup_cv);
1850 	if (px_p->px_pm_flags & PX_LINKUP_PENDING) {
1851 		px_p->px_pm_flags |= PX_LINKUP_RECVD;
1852 	} else {
1853 		/* Nobody waiting for this! */
1854 		px_p->px_lup_ignored++;
1855 	}
1856 	mutex_exit(&px_p->px_lupsoft_lock);
1857 	return (DDI_INTR_CLAIMED);
1858 }
1859 
1860 /*
1861  * Extract the drivers binding name to identify which chip we're binding to.
1862  * Whenever a new bus bridge is created, the driver alias entry should be
1863  * added here to identify the device if needed.  If a device isn't added,
1864  * the identity defaults to PX_CHIP_UNIDENTIFIED.
1865  */
1866 static uint32_t
1867 px_identity_chip(px_t *px_p)
1868 {
1869 	dev_info_t	*dip = px_p->px_dip;
1870 	char		*name = ddi_binding_name(dip);
1871 	uint32_t	revision = 0;
1872 
1873 	revision = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1874 	    "module-revision#", 0);
1875 
1876 	/* Check for Fire driver binding name */
1877 	if (strcmp(name, "pci108e,80f0") == 0) {
1878 		DBG(DBG_ATTACH, dip, "px_identity_chip: %s%d: "
1879 		    "name %s module-revision %d\n", ddi_driver_name(dip),
1880 		    ddi_get_instance(dip), name, revision);
1881 
1882 		return (PX_CHIP_ID(PX_CHIP_FIRE, revision, 0x00));
1883 	}
1884 
1885 	DBG(DBG_ATTACH, dip, "%s%d: Unknown PCI Express Host bridge %s %x\n",
1886 	    ddi_driver_name(dip), ddi_get_instance(dip), name, revision);
1887 
1888 	return (PX_CHIP_UNIDENTIFIED);
1889 }
1890 
1891 int
1892 px_err_add_intr(px_fault_t *px_fault_p)
1893 {
1894 	dev_info_t	*dip = px_fault_p->px_fh_dip;
1895 	px_t		*px_p = DIP_TO_STATE(dip);
1896 
1897 	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
1898 		px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL) == 0);
1899 
1900 	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);
1901 
1902 	return (DDI_SUCCESS);
1903 }
1904 
1905 void
1906 px_err_rem_intr(px_fault_t *px_fault_p)
1907 {
1908 	dev_info_t	*dip = px_fault_p->px_fh_dip;
1909 	px_t		*px_p = DIP_TO_STATE(dip);
1910 
1911 	rem_ivintr(px_fault_p->px_fh_sysino, NULL);
1912 
1913 	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
1914 		IB_INTR_WAIT);
1915 }
1916 
1917 #ifdef FMA
1918 void
1919 px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
1920 {
1921 	/* populate the rc_status by reading the registers - TBD */
1922 }
1923 #endif /* FMA */
1924