xref: /titanic_41/usr/src/uts/sun4/io/px/px_dma.c (revision 0b6016e6ff70af39f99c9cc28e0c2207c8f5413c)
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 2006 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 /*
29  * PCI Express nexus DVMA and DMA core routines:
30  *	dma_map/dma_bind_handle implementation
31  *	bypass and peer-to-peer support
32  *	fast track DVMA space allocation
33  *	runtime DVMA debug
34  */
35 #include <sys/types.h>
36 #include <sys/kmem.h>
37 #include <sys/async.h>
38 #include <sys/sysmacros.h>
39 #include <sys/sunddi.h>
40 #include <sys/ddi_impldefs.h>
41 #include "px_obj.h"
42 
43 /*LINTLIBRARY*/
44 
45 /*
46  * px_dma_allocmp - Allocate a pci dma implementation structure
47  *
48  * An extra ddi_dma_attr structure is bundled with the usual ddi_dma_impl
49  * to hold unmodified device limits. The ddi_dma_attr inside the
50  * ddi_dma_impl structure is augumented with system limits to enhance
51  * DVMA performance at runtime. The unaugumented device limits saved
52  * right after (accessed through (ddi_dma_attr_t *)(mp + 1)) is used
53  * strictly for peer-to-peer transfers which do not obey system limits.
54  *
55  * return: DDI_SUCCESS DDI_DMA_NORESOURCES
56  */
57 ddi_dma_impl_t *
58 px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip, int (*waitfp)(caddr_t),
59 	caddr_t arg)
60 {
61 	register ddi_dma_impl_t *mp;
62 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
63 
64 	/* Caution: we don't use zalloc to enhance performance! */
65 	if ((mp = kmem_alloc(sizeof (px_dma_hdl_t), sleep)) == 0) {
66 		DBG(DBG_DMA_MAP, dip, "can't alloc dma_handle\n");
67 		if (waitfp != DDI_DMA_DONTWAIT) {
68 			DBG(DBG_DMA_MAP, dip, "alloc_mp kmem cb\n");
69 			ddi_set_callback(waitfp, arg, &px_kmem_clid);
70 		}
71 		return (mp);
72 	}
73 
74 	mp->dmai_rdip = rdip;
75 	mp->dmai_flags = 0;
76 	mp->dmai_pfnlst = NULL;
77 	mp->dmai_winlst = NULL;
78 
79 	/*
80 	 * kmem_alloc debug: the following fields are not zero-ed
81 	 * mp->dmai_mapping = 0;
82 	 * mp->dmai_size = 0;
83 	 * mp->dmai_offset = 0;
84 	 * mp->dmai_minxfer = 0;
85 	 * mp->dmai_burstsizes = 0;
86 	 * mp->dmai_ndvmapages = 0;
87 	 * mp->dmai_pool/roffset = 0;
88 	 * mp->dmai_rflags = 0;
89 	 * mp->dmai_inuse/flags
90 	 * mp->dmai_nwin = 0;
91 	 * mp->dmai_winsize = 0;
92 	 * mp->dmai_nexus_private/tte = 0;
93 	 * mp->dmai_iopte/pfnlst
94 	 * mp->dmai_sbi/pfn0 = 0;
95 	 * mp->dmai_minfo/winlst/fdvma
96 	 * mp->dmai_rdip
97 	 * bzero(&mp->dmai_object, sizeof (ddi_dma_obj_t));
98 	 * bzero(&mp->dmai_attr, sizeof (ddi_dma_attr_t));
99 	 * mp->dmai_cookie = 0;
100 	 */
101 
102 	mp->dmai_attr.dma_attr_version = (uint_t)DMA_ATTR_VERSION;
103 	mp->dmai_attr.dma_attr_flags = (uint_t)0;
104 	mp->dmai_fault = 0;
105 	mp->dmai_fault_check = NULL;
106 	mp->dmai_fault_notify = NULL;
107 
108 	mp->dmai_error.err_ena = 0;
109 	mp->dmai_error.err_status = DDI_FM_OK;
110 	mp->dmai_error.err_expected = DDI_FM_ERR_UNEXPECTED;
111 	mp->dmai_error.err_ontrap = NULL;
112 	mp->dmai_error.err_fep = NULL;
113 	mp->dmai_error.err_cf = NULL;
114 
115 	if (px_child_prefetch(mp->dmai_rdip))
116 		mp->dmai_flags |= (PX_DMAI_FLAGS_MAP_BUFZONE |
117 		    PX_DMAI_FLAGS_REDZONE);
118 
119 	return (mp);
120 }
121 
122 void
123 px_dma_freemp(ddi_dma_impl_t *mp)
124 {
125 	if (mp->dmai_ndvmapages > 1)
126 		px_dma_freepfn(mp);
127 	if (mp->dmai_winlst)
128 		px_dma_freewin(mp);
129 	kmem_free(mp, sizeof (px_dma_hdl_t));
130 }
131 
132 void
133 px_dma_freepfn(ddi_dma_impl_t *mp)
134 {
135 	void *addr = mp->dmai_pfnlst;
136 	if (addr) {
137 		size_t npages = mp->dmai_ndvmapages;
138 		if (npages > 1)
139 			kmem_free(addr, npages * sizeof (px_iopfn_t));
140 		mp->dmai_pfnlst = NULL;
141 	}
142 	mp->dmai_ndvmapages = 0;
143 }
144 
145 /*
146  * px_dma_lmts2hdl - alloate a ddi_dma_impl_t, validate practical limits
147  *			and convert dmareq->dmar_limits to mp->dmai_attr
148  *
149  * ddi_dma_impl_t member modified     input
150  * ------------------------------------------------------------------------
151  * mp->dmai_minxfer		    - dev
152  * mp->dmai_burstsizes		    - dev
153  * mp->dmai_flags		    - no limit? peer-to-peer only?
154  *
155  * ddi_dma_attr member modified       input
156  * ------------------------------------------------------------------------
157  * mp->dmai_attr.dma_attr_addr_lo   - dev lo, sys lo
158  * mp->dmai_attr.dma_attr_addr_hi   - dev hi, sys hi
159  * mp->dmai_attr.dma_attr_count_max - dev count max, dev/sys lo/hi delta
160  * mp->dmai_attr.dma_attr_seg       - 0         (no nocross   restriction)
161  * mp->dmai_attr.dma_attr_align     - 1         (no alignment restriction)
162  *
163  * The dlim_dmaspeed member of dmareq->dmar_limits is ignored.
164  */
165 ddi_dma_impl_t *
166 px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip, px_mmu_t *mmu_p,
167 	ddi_dma_req_t *dmareq)
168 {
169 	ddi_dma_impl_t *mp;
170 	ddi_dma_attr_t *attr_p;
171 	uint64_t syslo		= mmu_p->mmu_dvma_base;
172 	uint64_t syshi		= mmu_p->mmu_dvma_end;
173 	uint64_t fasthi		= mmu_p->mmu_dvma_fast_end;
174 	ddi_dma_lim_t *lim_p	= dmareq->dmar_limits;
175 	uint32_t count_max	= lim_p->dlim_cntr_max;
176 	uint64_t lo		= lim_p->dlim_addr_lo;
177 	uint64_t hi		= lim_p->dlim_addr_hi;
178 	if (hi <= lo) {
179 		DBG(DBG_DMA_MAP, dip, "Bad limits\n");
180 		return ((ddi_dma_impl_t *)DDI_DMA_NOMAPPING);
181 	}
182 	if (!count_max)
183 		count_max--;
184 
185 	if (!(mp = px_dma_allocmp(dip, rdip, dmareq->dmar_fp,
186 		dmareq->dmar_arg)))
187 		return (NULL);
188 
189 	/* store original dev input at the 2nd ddi_dma_attr */
190 	attr_p = PX_DEV_ATTR(mp);
191 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
192 	SET_DMAALIGN(attr_p, 1);
193 
194 	lo = MAX(lo, syslo);
195 	hi = MIN(hi, syshi);
196 	if (hi <= lo)
197 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
198 	count_max = MIN(count_max, hi - lo);
199 
200 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, fasthi, 1))
201 		mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT |
202 			PX_DMAI_FLAGS_NOSYSLIMIT;
203 	else {
204 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, 1))
205 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
206 	}
207 	if (PX_DMA_NOCTX(rdip))
208 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
209 
210 	/* store augumented dev input to mp->dmai_attr */
211 	mp->dmai_minxfer	= lim_p->dlim_minxfer;
212 	mp->dmai_burstsizes	= lim_p->dlim_burstsizes;
213 	attr_p = &mp->dmai_attr;
214 	SET_DMAATTR(attr_p, lo, hi, -1, count_max);
215 	SET_DMAALIGN(attr_p, 1);
216 	return (mp);
217 }
218 
219 /*
220  * Called from px_attach to check for bypass dma support and set
221  * flags accordingly.
222  */
223 int
224 px_dma_attach(px_t *px_p)
225 {
226 	uint64_t baddr;
227 
228 	if (px_lib_iommu_getbypass(px_p->px_dip, 0ull,
229 			PCI_MAP_ATTR_WRITE|PCI_MAP_ATTR_READ,
230 			&baddr) != DDI_ENOTSUP)
231 		/* ignore all other errors */
232 		px_p->px_dev_caps |= PX_BYPASS_DMA_ALLOWED;
233 
234 	return (DDI_SUCCESS);
235 }
236 
237 /*
238  * px_dma_attr2hdl
239  *
240  * This routine is called from the alloc handle entry point to sanity check the
241  * dma attribute structure.
242  *
243  * use by: px_dma_allochdl()
244  *
245  * return value:
246  *
247  *	DDI_SUCCESS		- on success
248  *	DDI_DMA_BADATTR		- attribute has invalid version number
249  *				  or address limits exclude dvma space
250  */
251 int
252 px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp)
253 {
254 	px_mmu_t *mmu_p = px_p->px_mmu_p;
255 	uint64_t syslo, syshi;
256 	int	ret;
257 	ddi_dma_attr_t *attrp		= PX_DEV_ATTR(mp);
258 	uint64_t hi			= attrp->dma_attr_addr_hi;
259 	uint64_t lo			= attrp->dma_attr_addr_lo;
260 	uint64_t align			= attrp->dma_attr_align;
261 	uint64_t nocross		= attrp->dma_attr_seg;
262 	uint64_t count_max		= attrp->dma_attr_count_max;
263 
264 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "attrp=%p cntr_max=%x.%08x\n",
265 		attrp, HI32(count_max), LO32(count_max));
266 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x lo=%x.%08x\n",
267 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
268 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "seg=%x.%08x align=%x.%08x\n",
269 		HI32(nocross), LO32(nocross), HI32(align), LO32(align));
270 
271 	if (!nocross)
272 		nocross--;
273 	if (attrp->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL) { /* BYPASS */
274 
275 		DBG(DBG_DMA_ALLOCH, px_p->px_dip, "bypass mode\n");
276 		/*
277 		 * If Bypass DMA is not supported, return error so that
278 		 * target driver can fall back to dvma mode of operation
279 		 */
280 		if (!(px_p->px_dev_caps & PX_BYPASS_DMA_ALLOWED))
281 			return (DDI_DMA_BADATTR);
282 		mp->dmai_flags |= PX_DMAI_FLAGS_BYPASSREQ;
283 		if (nocross != UINT64_MAX)
284 			return (DDI_DMA_BADATTR);
285 		if (align && (align > MMU_PAGE_SIZE))
286 			return (DDI_DMA_BADATTR);
287 		align = 1; /* align on 1 page boundary */
288 
289 		/* do a range check and get the limits */
290 		ret = px_lib_dma_bypass_rngchk(px_p->px_dip, attrp,
291 				&syslo, &syshi);
292 		if (ret != DDI_SUCCESS)
293 			return (ret);
294 	} else { /* MMU_XLATE or PEER_TO_PEER */
295 		align = MAX(align, MMU_PAGE_SIZE) - 1;
296 		if ((align & nocross) != align) {
297 			dev_info_t *rdip = mp->dmai_rdip;
298 			cmn_err(CE_WARN, "%s%d dma_attr_seg not aligned",
299 				NAMEINST(rdip));
300 			return (DDI_DMA_BADATTR);
301 		}
302 		align = MMU_BTOP(align + 1);
303 		syslo = mmu_p->mmu_dvma_base;
304 		syshi = mmu_p->mmu_dvma_end;
305 	}
306 	if (hi <= lo) {
307 		dev_info_t *rdip = mp->dmai_rdip;
308 		cmn_err(CE_WARN, "%s%d limits out of range", NAMEINST(rdip));
309 		return (DDI_DMA_BADATTR);
310 	}
311 	lo = MAX(lo, syslo);
312 	hi = MIN(hi, syshi);
313 	if (!count_max)
314 		count_max--;
315 
316 	DBG(DBG_DMA_ALLOCH, px_p->px_dip, "hi=%x.%08x, lo=%x.%08x\n",
317 		HI32(hi), LO32(hi), HI32(lo), LO32(lo));
318 	if (hi <= lo) { /* peer transfers cannot have alignment & nocross */
319 		dev_info_t *rdip = mp->dmai_rdip;
320 		cmn_err(CE_WARN, "%s%d peer only dev %p", NAMEINST(rdip), mp);
321 		if ((nocross < UINT32_MAX) || (align > 1)) {
322 			cmn_err(CE_WARN, "%s%d peer only device bad attr",
323 				NAMEINST(rdip));
324 			return (DDI_DMA_BADATTR);
325 		}
326 		mp->dmai_flags |= PX_DMAI_FLAGS_PEER_ONLY;
327 	} else /* set practical counter_max value */
328 		count_max = MIN(count_max, hi - lo);
329 
330 	if (PX_DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align))
331 		mp->dmai_flags |= PX_DMAI_FLAGS_NOSYSLIMIT |
332 			PX_DMAI_FLAGS_NOFASTLIMIT;
333 	else {
334 		syshi = mmu_p->mmu_dvma_fast_end;
335 		if (PX_DEV_NOFASTLIMIT(lo, hi, syslo, syshi, align))
336 			mp->dmai_flags |= PX_DMAI_FLAGS_NOFASTLIMIT;
337 	}
338 	if (PX_DMA_NOCTX(mp->dmai_rdip))
339 		mp->dmai_flags |= PX_DMAI_FLAGS_NOCTX;
340 
341 	mp->dmai_minxfer	= attrp->dma_attr_minxfer;
342 	mp->dmai_burstsizes	= attrp->dma_attr_burstsizes;
343 	attrp = &mp->dmai_attr;
344 	SET_DMAATTR(attrp, lo, hi, nocross, count_max);
345 	return (DDI_SUCCESS);
346 }
347 
348 #define	TGT_PFN_INBETWEEN(pfn, bgn, end) ((pfn >= bgn) && (pfn <= end))
349 
350 /*
351  * px_dma_type - determine which of the three types DMA (peer-to-peer,
352  *		mmu bypass, or mmu translate) we are asked to do.
353  *		Also checks pfn0 and rejects any non-peer-to-peer
354  *		requests for peer-only devices.
355  *
356  *	return values:
357  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
358  *		DDI_SUCCESS
359  *
360  *	dma handle members affected (set on exit):
361  *	mp->dmai_object		- dmareq->dmar_object
362  *	mp->dmai_rflags		- consistent?, nosync?, dmareq->dmar_flags
363  *	mp->dmai_flags   	- DMA type
364  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
365  *	mp->dmai_roffset 	- initialized to starting MMU page offset
366  *	mp->dmai_ndvmapages	- # of total MMU pages of entire object
367  */
368 int
369 px_dma_type(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
370 {
371 	dev_info_t *dip = px_p->px_dip;
372 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
373 	px_pec_t *pec_p = px_p->px_pec_p;
374 	uint32_t offset;
375 	pfn_t pfn0;
376 
377 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS | DMP_NOSYNC;
378 
379 	switch (dobj_p->dmao_type) {
380 	case DMA_OTYP_BUFVADDR:
381 	case DMA_OTYP_VADDR: {
382 		page_t **pplist = dobj_p->dmao_obj.virt_obj.v_priv;
383 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
384 
385 		DBG(DBG_DMA_MAP, dip, "vaddr=%p pplist=%p\n", vaddr, pplist);
386 		offset = (ulong_t)vaddr & MMU_PAGE_OFFSET;
387 		if (pplist) {				/* shadow list */
388 			mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
389 			pfn0 = page_pptonum(*pplist);
390 		} else {
391 			struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
392 			struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
393 			pfn0 = hat_getpfnum(hat_p, vaddr);
394 		}
395 		}
396 		break;
397 
398 	case DMA_OTYP_PAGES:
399 		offset = dobj_p->dmao_obj.pp_obj.pp_offset;
400 		mp->dmai_flags |= PX_DMAI_FLAGS_PGPFN;
401 		pfn0 = page_pptonum(dobj_p->dmao_obj.pp_obj.pp_pp);
402 		break;
403 
404 	case DMA_OTYP_PADDR:
405 	default:
406 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
407 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
408 		return (DDI_DMA_NOMAPPING);
409 	}
410 	if (pfn0 == PFN_INVALID) {
411 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
412 			NAMEINST(dip), dobj_p);
413 		return (DDI_DMA_NOMAPPING);
414 	}
415 	if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base32_pfn,
416 			pec_p->pec_last32_pfn)) {
417 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP32;
418 		goto done;	/* leave bypass and dvma flag as 0 */
419 	} else if (TGT_PFN_INBETWEEN(pfn0, pec_p->pec_base64_pfn,
420 			pec_p->pec_last64_pfn)) {
421 		mp->dmai_flags |= PX_DMAI_FLAGS_PTP|PX_DMAI_FLAGS_PTP64;
422 		goto done;	/* leave bypass and dvma flag as 0 */
423 	}
424 	if (PX_DMA_ISPEERONLY(mp)) {
425 		dev_info_t *rdip = mp->dmai_rdip;
426 		cmn_err(CE_WARN, "Bad peer-to-peer req %s%d", NAMEINST(rdip));
427 		return (DDI_DMA_NOMAPPING);
428 	}
429 	mp->dmai_flags |= (mp->dmai_flags & PX_DMAI_FLAGS_BYPASSREQ) ?
430 	    PX_DMAI_FLAGS_BYPASS : PX_DMAI_FLAGS_DVMA |
431 	    (mp->dmai_rflags & DDI_DMA_REDZONE ? PX_DMAI_FLAGS_REDZONE : 0);
432 done:
433 	mp->dmai_object	 = *dobj_p;			/* whole object    */
434 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0	   */
435 	mp->dmai_roffset = offset;			/* win0 pg0 offset */
436 	mp->dmai_ndvmapages = MMU_BTOPR(offset + mp->dmai_object.dmao_size);
437 	return (DDI_SUCCESS);
438 }
439 
440 /*
441  * px_dma_pgpfn - set up pfnlst array according to pages
442  *	VA/size pair: <shadow IO, bypass, peer-to-peer>, or OTYP_PAGES
443  */
444 /*ARGSUSED*/
445 static int
446 px_dma_pgpfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
447 {
448 	int i;
449 	dev_info_t *dip = px_p->px_dip;
450 
451 	switch (mp->dmai_object.dmao_type) {
452 	case DMA_OTYP_BUFVADDR:
453 	case DMA_OTYP_VADDR: {
454 		page_t **pplist = mp->dmai_object.dmao_obj.virt_obj.v_priv;
455 		DBG(DBG_DMA_MAP, dip, "shadow pplist=%p, %x pages, pfns=",
456 			pplist, npages);
457 		for (i = 1; i < npages; i++) {
458 			px_iopfn_t pfn = page_pptonum(pplist[i]);
459 			PX_SET_MP_PFN1(mp, i, pfn);
460 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
461 		}
462 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
463 		}
464 		break;
465 
466 	case DMA_OTYP_PAGES: {
467 		page_t *pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp->p_next;
468 		DBG(DBG_DMA_MAP, dip, "pp=%p pfns=", pp);
469 		for (i = 1; i < npages; i++, pp = pp->p_next) {
470 			px_iopfn_t pfn = page_pptonum(pp);
471 			PX_SET_MP_PFN1(mp, i, pfn);
472 			DBG(DBG_DMA_MAP|DBG_CONT, dip, "%x ", pfn);
473 		}
474 		DBG(DBG_DMA_MAP|DBG_CONT, dip, "\n");
475 		}
476 		break;
477 
478 	default:	/* check is already done by px_dma_type */
479 		ASSERT(0);
480 		break;
481 	}
482 	return (DDI_SUCCESS);
483 }
484 
485 /*
486  * px_dma_vapfn - set up pfnlst array according to VA
487  *	VA/size pair: <normal, bypass, peer-to-peer>
488  *	pfn0 is skipped as it is already done.
489  *	In this case, the cached pfn0 is used to fill pfnlst[0]
490  */
491 static int
492 px_dma_vapfn(px_t *px_p, ddi_dma_impl_t *mp, uint_t npages)
493 {
494 	dev_info_t *dip = px_p->px_dip;
495 	int i;
496 	caddr_t vaddr = (caddr_t)mp->dmai_object.dmao_obj.virt_obj.v_as;
497 	struct hat *hat_p = vaddr ? ((struct as *)vaddr)->a_hat : kas.a_hat;
498 
499 	vaddr = mp->dmai_object.dmao_obj.virt_obj.v_addr + MMU_PAGE_SIZE;
500 	for (i = 1; i < npages; i++, vaddr += MMU_PAGE_SIZE) {
501 		px_iopfn_t pfn = hat_getpfnum(hat_p, vaddr);
502 		if (pfn == PFN_INVALID)
503 			goto err_badpfn;
504 		PX_SET_MP_PFN1(mp, i, pfn);
505 		DBG(DBG_DMA_BINDH, dip, "px_dma_vapfn: mp=%p pfnlst[%x]=%x\n",
506 			mp, i, pfn);
507 	}
508 	return (DDI_SUCCESS);
509 err_badpfn:
510 	cmn_err(CE_WARN, "%s%d: bad page frame vaddr=%p", NAMEINST(dip), vaddr);
511 	return (DDI_DMA_NOMAPPING);
512 }
513 
514 /*
515  * px_dma_pfn - Fills pfn list for all pages being DMA-ed.
516  *
517  * dependencies:
518  *	mp->dmai_ndvmapages	- set to total # of dma pages
519  *
520  * return value:
521  *	DDI_SUCCESS
522  *	DDI_DMA_NOMAPPING
523  */
524 int
525 px_dma_pfn(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
526 {
527 	uint32_t npages = mp->dmai_ndvmapages;
528 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
529 	int i, ret, peer = PX_DMA_ISPTP(mp);
530 	int peer32 = PX_DMA_ISPTP32(mp);
531 	dev_info_t *dip = px_p->px_dip;
532 
533 	px_pec_t *pec_p = px_p->px_pec_p;
534 	px_iopfn_t pfn_base = peer32 ? pec_p->pec_base32_pfn :
535 					pec_p->pec_base64_pfn;
536 	px_iopfn_t pfn_last = peer32 ? pec_p->pec_last32_pfn :
537 					pec_p->pec_last64_pfn;
538 	px_iopfn_t pfn_adj = peer ? pfn_base : 0;
539 
540 	DBG(DBG_DMA_BINDH, dip, "px_dma_pfn: mp=%p pfn0=%x\n",
541 		mp, PX_MP_PFN0(mp) - pfn_adj);
542 	/* 1 page: no array alloc/fill, no mixed mode check */
543 	if (npages == 1) {
544 		PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);
545 		return (DDI_SUCCESS);
546 	}
547 	/* allocate pfn array */
548 	if (!(mp->dmai_pfnlst = kmem_alloc(npages * sizeof (px_iopfn_t),
549 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
550 		if (waitfp != DDI_DMA_DONTWAIT)
551 			ddi_set_callback(waitfp, dmareq->dmar_arg,
552 				&px_kmem_clid);
553 		return (DDI_DMA_NORESOURCES);
554 	}
555 	/* fill pfn array */
556 	PX_SET_MP_PFN(mp, 0, PX_MP_PFN0(mp) - pfn_adj);	/* pfnlst[0] */
557 	if ((ret = PX_DMA_ISPGPFN(mp) ? px_dma_pgpfn(px_p, mp, npages) :
558 		px_dma_vapfn(px_p, mp, npages)) != DDI_SUCCESS)
559 		goto err;
560 
561 	/* skip pfn0, check mixed mode and adjust peer to peer pfn */
562 	for (i = 1; i < npages; i++) {
563 		px_iopfn_t pfn = PX_GET_MP_PFN1(mp, i);
564 		if (peer ^ TGT_PFN_INBETWEEN(pfn, pfn_base, pfn_last)) {
565 			cmn_err(CE_WARN, "%s%d mixed mode DMA %lx %lx",
566 				NAMEINST(mp->dmai_rdip), PX_MP_PFN0(mp), pfn);
567 			ret = DDI_DMA_NOMAPPING;	/* mixed mode */
568 			goto err;
569 		}
570 		DBG(DBG_DMA_MAP, dip,
571 			"px_dma_pfn: pfnlst[%x]=%x-%x\n", i, pfn, pfn_adj);
572 		if (pfn_adj)
573 			PX_SET_MP_PFN1(mp, i, pfn - pfn_adj);
574 	}
575 	return (DDI_SUCCESS);
576 err:
577 	px_dma_freepfn(mp);
578 	return (ret);
579 }
580 
581 /*
582  * px_dvma_win() - trim requested DVMA size down to window size
583  *	The 1st window starts from offset and ends at page-aligned boundary.
584  *	From the 2nd window on, each window starts and ends at page-aligned
585  *	boundary except the last window ends at wherever requested.
586  *
587  *	accesses the following mp-> members:
588  *	mp->dmai_attr.dma_attr_count_max
589  *	mp->dmai_attr.dma_attr_seg
590  *	mp->dmai_roffset   - start offset of 1st window
591  *	mp->dmai_rflags (redzone)
592  *	mp->dmai_ndvmapages (for 1 page fast path)
593  *
594  *	sets the following mp-> members:
595  *	mp->dmai_size	   - xfer size, != winsize if 1st/last win  (not fixed)
596  *	mp->dmai_winsize   - window size (no redzone), n * page size    (fixed)
597  *	mp->dmai_nwin	   - # of DMA windows of entire object		(fixed)
598  *	mp->dmai_rflags	   - remove partial flag if nwin == 1		(fixed)
599  *	mp->dmai_winlst	   - NULL, window objects not used for DVMA	(fixed)
600  *
601  *	fixed - not changed across different DMA windows
602  */
603 /*ARGSUSED*/
604 int
605 px_dvma_win(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
606 {
607 	uint32_t redzone_sz	= PX_HAS_REDZONE(mp) ? MMU_PAGE_SIZE : 0;
608 	size_t obj_sz		= mp->dmai_object.dmao_size;
609 	size_t xfer_sz;
610 	ulong_t pg_off;
611 
612 	if ((mp->dmai_ndvmapages == 1) && !redzone_sz) {
613 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
614 		mp->dmai_size = obj_sz;
615 		mp->dmai_winsize = MMU_PAGE_SIZE;
616 		mp->dmai_nwin = 1;
617 		goto done;
618 	}
619 
620 	pg_off	= mp->dmai_roffset;
621 	xfer_sz	= obj_sz + redzone_sz;
622 
623 	/* include redzone in nocross check */ {
624 		uint64_t nocross = mp->dmai_attr.dma_attr_seg;
625 		if (xfer_sz + pg_off - 1 > nocross)
626 			xfer_sz = nocross - pg_off + 1;
627 		if (redzone_sz && (xfer_sz <= redzone_sz)) {
628 			DBG(DBG_DMA_MAP, px_p->px_dip,
629 			    "nocross too small: "
630 			    "%lx(%lx)+%lx+%lx < %llx\n",
631 			    xfer_sz, obj_sz, pg_off, redzone_sz, nocross);
632 			return (DDI_DMA_TOOBIG);
633 		}
634 	}
635 	xfer_sz -= redzone_sz;		/* restore transfer size  */
636 	/* check counter max */ {
637 		uint32_t count_max = mp->dmai_attr.dma_attr_count_max;
638 		if (xfer_sz - 1 > count_max)
639 			xfer_sz = count_max + 1;
640 	}
641 	if (xfer_sz >= obj_sz) {
642 		mp->dmai_rflags &= ~DDI_DMA_PARTIAL;
643 		mp->dmai_size = xfer_sz;
644 		mp->dmai_winsize = P2ROUNDUP(xfer_sz + pg_off, MMU_PAGE_SIZE);
645 		mp->dmai_nwin = 1;
646 		goto done;
647 	}
648 	if (!(dmareq->dmar_flags & DDI_DMA_PARTIAL)) {
649 		DBG(DBG_DMA_MAP, px_p->px_dip, "too big: %lx+%lx+%lx > %lx\n",
650 			obj_sz, pg_off, redzone_sz, xfer_sz);
651 		return (DDI_DMA_TOOBIG);
652 	}
653 
654 	xfer_sz = MMU_PTOB(MMU_BTOP(xfer_sz + pg_off)); /* page align */
655 	mp->dmai_size = xfer_sz - pg_off;	/* 1st window xferrable size */
656 	mp->dmai_winsize = xfer_sz;		/* redzone not in winsize */
657 	mp->dmai_nwin = (obj_sz + pg_off + xfer_sz - 1) / xfer_sz;
658 done:
659 	mp->dmai_winlst = NULL;
660 	px_dump_dma_handle(DBG_DMA_MAP, px_p->px_dip, mp);
661 	return (DDI_SUCCESS);
662 }
663 
664 /*
665  * fast track cache entry to mmu context, inserts 3 0 bits between
666  * upper 6-bits and lower 3-bits of the 9-bit cache entry
667  */
668 #define	MMU_FCE_TO_CTX(i)	(((i) << 3) | ((i) & 0x7) | 0x38)
669 
670 /*
671  * px_dvma_map_fast - attempts to map fast trackable DVMA
672  */
673 /*ARGSUSED*/
674 int
675 px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
676 {
677 	uint_t clustsz = px_dvma_page_cache_clustsz;
678 	uint_t entries = px_dvma_page_cache_entries;
679 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
680 	    mp->dmai_attr.dma_attr_flags);
681 	int i = mmu_p->mmu_dvma_addr_scan_start;
682 	uint8_t *lock_addr = mmu_p->mmu_dvma_cache_locks + i;
683 	px_dvma_addr_t dvma_pg;
684 	size_t npages = MMU_BTOP(mp->dmai_winsize);
685 	dev_info_t *dip = mmu_p->mmu_px_p->px_dip;
686 
687 	extern uint8_t ldstub(uint8_t *);
688 	ASSERT(MMU_PTOB(npages) == mp->dmai_winsize);
689 	ASSERT(npages + PX_HAS_REDZONE(mp) <= clustsz);
690 
691 	for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
692 	if (i >= entries) {
693 		lock_addr = mmu_p->mmu_dvma_cache_locks;
694 		i = 0;
695 		for (; i < entries && ldstub(lock_addr); i++, lock_addr++);
696 		if (i >= entries) {
697 #ifdef	PX_DMA_PROF
698 			px_dvmaft_exhaust++;
699 #endif	/* PX_DMA_PROF */
700 			return (DDI_DMA_NORESOURCES);
701 		}
702 	}
703 	mmu_p->mmu_dvma_addr_scan_start = (i + 1) & (entries - 1);
704 
705 	i *= clustsz;
706 	dvma_pg = mmu_p->dvma_base_pg + i;
707 
708 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i), npages, attr,
709 	    (void *)mp, 0, MMU_MAP_PFN) != DDI_SUCCESS) {
710 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
711 		    "px_lib_iommu_map failed\n");
712 
713 		return (DDI_FAILURE);
714 	}
715 
716 	if (!PX_MAP_BUFZONE(mp))
717 		goto done;
718 
719 	DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: redzone pg=%x\n", i + npages);
720 
721 	ASSERT(PX_HAS_REDZONE(mp));
722 
723 	if (px_lib_iommu_map(dip, PCI_TSBID(0, i + npages), 1, attr,
724 	    (void *)mp, npages - 1, MMU_MAP_PFN) != DDI_SUCCESS) {
725 		DBG(DBG_MAP_WIN, dip, "px_dvma_map_fast: "
726 		    "mapping REDZONE page failed\n");
727 
728 		(void) px_lib_iommu_demap(dip, PCI_TSBID(0, i), npages);
729 		return (DDI_FAILURE);
730 	}
731 
732 done:
733 #ifdef PX_DMA_PROF
734 	px_dvmaft_success++;
735 #endif
736 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
737 	mp->dmai_offset = 0;
738 	mp->dmai_flags |= PX_DMAI_FLAGS_FASTTRACK;
739 	PX_SAVE_MP_TTE(mp, attr);	/* save TTE template for unmapping */
740 	if (PX_DVMA_DBG_ON(mmu_p))
741 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
742 			mp->dmai_size, mp);
743 	return (DDI_SUCCESS);
744 }
745 
746 /*
747  * px_dvma_map: map non-fasttrack DMA
748  *		Use quantum cache if single page DMA.
749  */
750 int
751 px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq, px_mmu_t *mmu_p)
752 {
753 	uint_t npages = PX_DMA_WINNPGS(mp);
754 	px_dvma_addr_t dvma_pg, dvma_pg_index;
755 	void *dvma_addr;
756 	uint64_t tte = PX_GET_TTE_ATTR(mp->dmai_rflags,
757 	    mp->dmai_attr.dma_attr_flags);
758 	int sleep = dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP;
759 	dev_info_t *dip = mp->dmai_rdip;
760 	int	ret = DDI_SUCCESS;
761 
762 	/*
763 	 * allocate dvma space resource and map in the first window.
764 	 * (vmem_t *vmp, size_t size,
765 	 *	size_t align, size_t phase, size_t nocross,
766 	 *	void *minaddr, void *maxaddr, int vmflag)
767 	 */
768 	if ((npages == 1) && !PX_HAS_REDZONE(mp) && PX_HAS_NOSYSLIMIT(mp)) {
769 		dvma_addr = vmem_alloc(mmu_p->mmu_dvma_map,
770 			MMU_PAGE_SIZE, sleep);
771 		mp->dmai_flags |= PX_DMAI_FLAGS_VMEMCACHE;
772 #ifdef	PX_DMA_PROF
773 		px_dvma_vmem_alloc++;
774 #endif	/* PX_DMA_PROF */
775 	} else {
776 		dvma_addr = vmem_xalloc(mmu_p->mmu_dvma_map,
777 			MMU_PTOB(npages + PX_HAS_REDZONE(mp)),
778 			MAX(mp->dmai_attr.dma_attr_align, MMU_PAGE_SIZE),
779 			0,
780 			mp->dmai_attr.dma_attr_seg + 1,
781 			(void *)mp->dmai_attr.dma_attr_addr_lo,
782 			(void *)(mp->dmai_attr.dma_attr_addr_hi + 1),
783 			sleep);
784 #ifdef	PX_DMA_PROF
785 		px_dvma_vmem_xalloc++;
786 #endif	/* PX_DMA_PROF */
787 	}
788 	dvma_pg = MMU_BTOP((ulong_t)dvma_addr);
789 	dvma_pg_index = dvma_pg - mmu_p->dvma_base_pg;
790 	DBG(DBG_DMA_MAP, dip, "fallback dvma_pages: dvma_pg=%x index=%x\n",
791 		dvma_pg, dvma_pg_index);
792 	if (dvma_pg == 0)
793 		goto noresource;
794 
795 	mp->dmai_mapping = mp->dmai_roffset | MMU_PTOB(dvma_pg);
796 	mp->dmai_offset = 0;
797 	PX_SAVE_MP_TTE(mp, tte);	/* mp->dmai_tte = tte */
798 
799 	if ((ret = px_mmu_map_pages(mmu_p,
800 	    mp, dvma_pg, npages, 0)) != DDI_SUCCESS) {
801 		if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
802 			vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
803 			    MMU_PAGE_SIZE);
804 #ifdef PX_DMA_PROF
805 			px_dvma_vmem_free++;
806 #endif /* PX_DMA_PROF */
807 		} else {
808 			vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
809 			    MMU_PTOB(npages + PX_HAS_REDZONE(mp)));
810 #ifdef PX_DMA_PROF
811 			px_dvma_vmem_xfree++;
812 #endif /* PX_DMA_PROF */
813 		}
814 	}
815 
816 	return (ret);
817 noresource:
818 	if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
819 		DBG(DBG_DMA_MAP, dip, "dvma_pg 0 - set callback\n");
820 		ddi_set_callback(dmareq->dmar_fp, dmareq->dmar_arg,
821 			&mmu_p->mmu_dvma_clid);
822 	}
823 	DBG(DBG_DMA_MAP, dip, "vmem_xalloc - DDI_DMA_NORESOURCES\n");
824 	return (DDI_DMA_NORESOURCES);
825 }
826 
827 void
828 px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
829 {
830 	px_dvma_addr_t dvma_addr = (px_dvma_addr_t)mp->dmai_mapping;
831 	px_dvma_addr_t dvma_pg = MMU_BTOP(dvma_addr);
832 	dvma_addr = MMU_PTOB(dvma_pg);
833 
834 	if (mp->dmai_flags & PX_DMAI_FLAGS_FASTTRACK) {
835 		px_iopfn_t index = dvma_pg - mmu_p->dvma_base_pg;
836 		ASSERT(index % px_dvma_page_cache_clustsz == 0);
837 		index /= px_dvma_page_cache_clustsz;
838 		ASSERT(index < px_dvma_page_cache_entries);
839 		mmu_p->mmu_dvma_cache_locks[index] = 0;
840 #ifdef	PX_DMA_PROF
841 		px_dvmaft_free++;
842 #endif	/* PX_DMA_PROF */
843 		return;
844 	}
845 
846 	if (mp->dmai_flags & PX_DMAI_FLAGS_VMEMCACHE) {
847 		vmem_free(mmu_p->mmu_dvma_map, (void *)dvma_addr,
848 			MMU_PAGE_SIZE);
849 #ifdef PX_DMA_PROF
850 		px_dvma_vmem_free++;
851 #endif /* PX_DMA_PROF */
852 	} else {
853 		size_t npages = MMU_BTOP(mp->dmai_winsize) + PX_HAS_REDZONE(mp);
854 		vmem_xfree(mmu_p->mmu_dvma_map, (void *)dvma_addr,
855 			MMU_PTOB(npages));
856 #ifdef PX_DMA_PROF
857 		px_dvma_vmem_xfree++;
858 #endif /* PX_DMA_PROF */
859 	}
860 }
861 
862 /*
863  * DVMA mappings may have multiple windows, but each window always have
864  * one segment.
865  */
866 int
867 px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
868 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
869 	uint_t cache_flags)
870 {
871 	switch (cmd) {
872 	case DDI_DMA_SYNC:
873 		return (px_lib_dma_sync(dip, rdip, (ddi_dma_handle_t)mp,
874 		    *offp, *lenp, cache_flags));
875 
876 	case DDI_DMA_HTOC: {
877 		int ret;
878 		off_t wo_off, off = *offp;	/* wo_off: wnd's obj offset */
879 		uint_t win_size = mp->dmai_winsize;
880 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)objp;
881 
882 		if (off >= mp->dmai_object.dmao_size) {
883 			cmn_err(CE_WARN, "%s%d invalid dma_htoc offset %lx",
884 				NAMEINST(mp->dmai_rdip), off);
885 			return (DDI_FAILURE);
886 		}
887 		off += mp->dmai_roffset;
888 		ret = px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
889 		    off / win_size, &wo_off, NULL, cp, NULL); /* lenp == NULL */
890 		if (ret)
891 			return (ret);
892 		DBG(DBG_DMA_CTL, dip, "HTOC:cookie=%x+%lx off=%lx,%lx\n",
893 			cp->dmac_address, cp->dmac_size, off, *offp);
894 
895 		/* adjust cookie addr/len if we are not on window boundary */
896 		ASSERT((off % win_size) == (off -
897 			(PX_DMA_CURWIN(mp) ? mp->dmai_roffset : 0) - wo_off));
898 		off = PX_DMA_CURWIN(mp) ? off % win_size : *offp;
899 		ASSERT(cp->dmac_size > off);
900 		cp->dmac_laddress += off;
901 		cp->dmac_size -= off;
902 		DBG(DBG_DMA_CTL, dip, "HTOC:mp=%p cookie=%x+%lx off=%lx,%lx\n",
903 			mp, cp->dmac_address, cp->dmac_size, off, wo_off);
904 		}
905 		return (DDI_SUCCESS);
906 
907 	case DDI_DMA_REPWIN:
908 		*offp = mp->dmai_offset;
909 		*lenp = mp->dmai_size;
910 		return (DDI_SUCCESS);
911 
912 	case DDI_DMA_MOVWIN: {
913 		off_t off = *offp;
914 		if (off >= mp->dmai_object.dmao_size)
915 			return (DDI_FAILURE);
916 		off += mp->dmai_roffset;
917 		return (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
918 		    off / mp->dmai_winsize, offp, lenp,
919 		    (ddi_dma_cookie_t *)objp, NULL));
920 		}
921 
922 	case DDI_DMA_NEXTWIN: {
923 		px_window_t win = PX_DMA_CURWIN(mp);
924 		if (offp) {
925 			if (*(px_window_t *)offp != win) {
926 				/* window not active */
927 				*(px_window_t *)objp = win; /* return cur win */
928 				return (DDI_DMA_STALE);
929 			}
930 			win++;
931 		} else	/* map win 0 */
932 			win = 0;
933 		if (win >= mp->dmai_nwin) {
934 			*(px_window_t *)objp = win - 1;
935 			return (DDI_DMA_DONE);
936 		}
937 		if (px_dma_win(dip, rdip, (ddi_dma_handle_t)mp,
938 		    win, 0, 0, 0, 0)) {
939 			*(px_window_t *)objp = win - 1;
940 			return (DDI_FAILURE);
941 		}
942 		*(px_window_t *)objp = win;
943 		}
944 		return (DDI_SUCCESS);
945 
946 	case DDI_DMA_NEXTSEG:
947 		if (*(px_window_t *)offp != PX_DMA_CURWIN(mp))
948 			return (DDI_DMA_STALE);
949 		if (lenp)				/* only 1 seg allowed */
950 			return (DDI_DMA_DONE);
951 
952 		/* return mp as seg 0 */
953 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
954 		return (DDI_SUCCESS);
955 
956 	case DDI_DMA_SEGTOC:
957 		MAKE_DMA_COOKIE((ddi_dma_cookie_t *)objp, mp->dmai_mapping,
958 			mp->dmai_size);
959 		*offp = mp->dmai_offset;
960 		*lenp = mp->dmai_size;
961 		return (DDI_SUCCESS);
962 
963 	case DDI_DMA_COFF: {
964 		ddi_dma_cookie_t *cp = (ddi_dma_cookie_t *)offp;
965 		if (cp->dmac_address < mp->dmai_mapping ||
966 			(cp->dmac_address + cp->dmac_size) >
967 			(mp->dmai_mapping + mp->dmai_size))
968 			return (DDI_FAILURE);
969 		*objp = (caddr_t)(cp->dmac_address - mp->dmai_mapping +
970 			mp->dmai_offset);
971 		}
972 		return (DDI_SUCCESS);
973 	default:
974 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
975 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
976 		break;
977 	}
978 	return (DDI_FAILURE);
979 }
980 
981 void
982 px_dma_freewin(ddi_dma_impl_t *mp)
983 {
984 	px_dma_win_t *win_p = mp->dmai_winlst, *win2_p;
985 	for (win2_p = win_p; win_p; win2_p = win_p) {
986 		win_p = win2_p->win_next;
987 		kmem_free(win2_p, sizeof (px_dma_win_t) +
988 			sizeof (ddi_dma_cookie_t) * win2_p->win_ncookies);
989 	}
990 	mp->dmai_nwin = 0;
991 	mp->dmai_winlst = NULL;
992 }
993 
994 /*
995  * px_dma_newwin - create a dma window object and cookies
996  *
997  *	After the initial scan in px_dma_physwin(), which identifies
998  *	a portion of the pfn array that belongs to a dma window,
999  *	we are called to allocate and initialize representing memory
1000  *	resources. We know from the 1st scan the number of cookies
1001  *	or dma segment in this window so we can allocate a contiguous
1002  *	memory array for the dma cookies (The implementation of
1003  *	ddi_dma_nextcookie(9f) dictates dma cookies be contiguous).
1004  *
1005  *	A second round scan is done on the pfn array to identify
1006  *	each dma segment and initialize its corresponding dma cookie.
1007  *	We don't need to do all the safety checking and we know they
1008  *	all belong to the same dma window.
1009  *
1010  *	Input:	cookie_no - # of cookies identified by the 1st scan
1011  *		start_idx - subscript of the pfn array for the starting pfn
1012  *		end_idx   - subscript of the last pfn in dma window
1013  *		win_pp    - pointer to win_next member of previous window
1014  *	Return:	DDI_SUCCESS - with **win_pp as newly created window object
1015  *		DDI_DMA_NORESROUCE - caller frees all previous window objs
1016  *	Note:	Each cookie and window size are all initialized on page
1017  *		boundary. This is not true for the 1st cookie of the 1st
1018  *		window and the last cookie of the last window.
1019  *		We fix that later in upper layer which has access to size
1020  *		and offset info.
1021  *
1022  */
1023 /*ARGSUSED*/
1024 static int
1025 px_dma_newwin(dev_info_t *dip, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp,
1026 	uint32_t cookie_no, uint32_t start_idx, uint32_t end_idx,
1027 	px_dma_win_t **win_pp, uint64_t count_max, uint64_t bypass)
1028 {
1029 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
1030 	ddi_dma_cookie_t *cookie_p;
1031 	uint32_t pfn_no = 1;
1032 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, start_idx);
1033 	px_iopfn_t prev_pfn = pfn;
1034 	uint64_t baddr, seg_pfn0 = pfn;
1035 	size_t sz = cookie_no * sizeof (ddi_dma_cookie_t);
1036 	px_dma_win_t *win_p = kmem_zalloc(sizeof (px_dma_win_t) + sz,
1037 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP);
1038 	io_attributes_t	attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
1039 	    mp->dmai_attr.dma_attr_flags);
1040 
1041 	if (!win_p)
1042 		goto noresource;
1043 
1044 	win_p->win_next = NULL;
1045 	win_p->win_ncookies = cookie_no;
1046 	win_p->win_curseg = 0;	/* start from segment 0 */
1047 	win_p->win_size = MMU_PTOB(end_idx - start_idx + 1);
1048 	/* win_p->win_offset is left uninitialized */
1049 
1050 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1051 	start_idx++;
1052 	for (; start_idx <= end_idx; start_idx++, prev_pfn = pfn, pfn_no++) {
1053 		pfn = PX_GET_MP_PFN1(mp, start_idx);
1054 		if ((pfn == prev_pfn + 1) &&
1055 			(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
1056 			continue;
1057 
1058 		/* close up the cookie up to (including) prev_pfn */
1059 		baddr = MMU_PTOB(seg_pfn0);
1060 		if (bypass && (px_lib_iommu_getbypass(dip,
1061 				baddr, attr, &baddr) != DDI_SUCCESS))
1062 			return (DDI_FAILURE);
1063 
1064 		MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
1065 		DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages)\n",
1066 			MMU_PTOB(seg_pfn0), pfn_no);
1067 
1068 		cookie_p++;	/* advance to next available cookie cell */
1069 		pfn_no = 0;
1070 		seg_pfn0 = pfn;	/* start a new segment from current pfn */
1071 	}
1072 
1073 	baddr = MMU_PTOB(seg_pfn0);
1074 	if (bypass && (px_lib_iommu_getbypass(dip,
1075 			baddr, attr, &baddr) != DDI_SUCCESS))
1076 		return (DDI_FAILURE);
1077 
1078 	MAKE_DMA_COOKIE(cookie_p, baddr, MMU_PTOB(pfn_no));
1079 	DBG(DBG_BYPASS, mp->dmai_rdip, "cookie %p (%x pages) of total %x\n",
1080 		MMU_PTOB(seg_pfn0), pfn_no, cookie_no);
1081 #ifdef	DEBUG
1082 	cookie_p++;
1083 	ASSERT((cookie_p - (ddi_dma_cookie_t *)(win_p + 1)) == cookie_no);
1084 #endif	/* DEBUG */
1085 	*win_pp = win_p;
1086 	return (DDI_SUCCESS);
1087 noresource:
1088 	if (waitfp != DDI_DMA_DONTWAIT)
1089 		ddi_set_callback(waitfp, dmareq->dmar_arg, &px_kmem_clid);
1090 	return (DDI_DMA_NORESOURCES);
1091 }
1092 
1093 /*
1094  * px_dma_adjust - adjust 1st and last cookie and window sizes
1095  *	remove initial dma page offset from 1st cookie and window size
1096  *	remove last dma page remainder from last cookie and window size
1097  *	fill win_offset of each dma window according to just fixed up
1098  *		each window sizes
1099  *	px_dma_win_t members modified:
1100  *	win_p->win_offset - this window's offset within entire DMA object
1101  *	win_p->win_size	  - xferrable size (in bytes) for this window
1102  *
1103  *	ddi_dma_impl_t members modified:
1104  *	mp->dmai_size	  - 1st window xferrable size
1105  *	mp->dmai_offset   - 0, which is the dma offset of the 1st window
1106  *
1107  *	ddi_dma_cookie_t members modified:
1108  *	cookie_p->dmac_size - 1st and last cookie remove offset or remainder
1109  *	cookie_p->dmac_laddress - 1st cookie add page offset
1110  */
1111 static void
1112 px_dma_adjust(ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp, px_dma_win_t *win_p)
1113 {
1114 	ddi_dma_cookie_t *cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1115 	size_t pg_offset = mp->dmai_roffset;
1116 	size_t win_offset = 0;
1117 
1118 	cookie_p->dmac_size -= pg_offset;
1119 	cookie_p->dmac_laddress |= pg_offset;
1120 	win_p->win_size -= pg_offset;
1121 	DBG(DBG_BYPASS, mp->dmai_rdip, "pg0 adjust %lx\n", pg_offset);
1122 
1123 	mp->dmai_size = win_p->win_size;
1124 	mp->dmai_offset = 0;
1125 
1126 	pg_offset += mp->dmai_object.dmao_size;
1127 	pg_offset &= MMU_PAGE_OFFSET;
1128 	if (pg_offset)
1129 		pg_offset = MMU_PAGE_SIZE - pg_offset;
1130 	DBG(DBG_BYPASS, mp->dmai_rdip, "last pg adjust %lx\n", pg_offset);
1131 
1132 	for (; win_p->win_next; win_p = win_p->win_next) {
1133 		DBG(DBG_BYPASS, mp->dmai_rdip, "win off %p\n", win_offset);
1134 		win_p->win_offset = win_offset;
1135 		win_offset += win_p->win_size;
1136 	}
1137 	/* last window */
1138 	win_p->win_offset = win_offset;
1139 	cookie_p = (ddi_dma_cookie_t *)(win_p + 1);
1140 	cookie_p[win_p->win_ncookies - 1].dmac_size -= pg_offset;
1141 	win_p->win_size -= pg_offset;
1142 	ASSERT((win_offset + win_p->win_size) == mp->dmai_object.dmao_size);
1143 }
1144 
1145 /*
1146  * px_dma_physwin() - carve up dma windows using physical addresses.
1147  *	Called to handle mmu bypass and pci peer-to-peer transfers.
1148  *	Calls px_dma_newwin() to allocate window objects.
1149  *
1150  * Dependency: mp->dmai_pfnlst points to an array of pfns
1151  *
1152  * 1. Each dma window is represented by a px_dma_win_t object.
1153  *	The object will be casted to ddi_dma_win_t and returned
1154  *	to leaf driver through the DDI interface.
1155  * 2. Each dma window can have several dma segments with each
1156  *	segment representing a physically contiguous either memory
1157  *	space (if we are doing an mmu bypass transfer) or pci address
1158  *	space (if we are doing a peer-to-peer transfer).
1159  * 3. Each segment has a DMA cookie to program the DMA engine.
1160  *	The cookies within each DMA window must be located in a
1161  *	contiguous array per ddi_dma_nextcookie(9f).
1162  * 4. The number of DMA segments within each DMA window cannot exceed
1163  *	mp->dmai_attr.dma_attr_sgllen. If the transfer size is
1164  *	too large to fit in the sgllen, the rest needs to be
1165  *	relocated to the next dma window.
1166  * 5. Peer-to-peer DMA segment follows device hi, lo, count_max,
1167  *	and nocross restrictions while bypass DMA follows the set of
1168  *	restrictions with system limits factored in.
1169  *
1170  * Return:
1171  *	mp->dmai_winlst	 - points to a link list of px_dma_win_t objects.
1172  *		Each px_dma_win_t object on the link list contains
1173  *		infomation such as its window size (# of pages),
1174  *		starting offset (also see Restriction), an array of
1175  *		DMA cookies, and # of cookies in the array.
1176  *	mp->dmai_pfnlst	 - NULL, the pfn list is freed to conserve memory.
1177  *	mp->dmai_nwin	 - # of total DMA windows on mp->dmai_winlst.
1178  *	mp->dmai_mapping - starting cookie address
1179  *	mp->dmai_rflags	 - consistent, nosync, no redzone
1180  *	mp->dmai_cookie	 - start of cookie table of the 1st DMA window
1181  *
1182  * Restriction:
1183  *	Each px_dma_win_t object can theoratically start from any offset
1184  *	since the mmu is not involved. However, this implementation
1185  *	always make windows start from page aligned offset (except
1186  *	the 1st window, which follows the requested offset) due to the
1187  *	fact that we are handed a pfn list. This does require device's
1188  *	count_max and attr_seg to be at least MMU_PAGE_SIZE aligned.
1189  */
1190 int
1191 px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq, ddi_dma_impl_t *mp)
1192 {
1193 	uint_t npages = mp->dmai_ndvmapages;
1194 	int ret, sgllen = mp->dmai_attr.dma_attr_sgllen;
1195 	px_iopfn_t pfn_lo, pfn_hi, prev_pfn;
1196 	px_iopfn_t pfn = PX_GET_MP_PFN(mp, 0);
1197 	uint32_t i, win_no = 0, pfn_no = 1, win_pfn0_index = 0, cookie_no = 0;
1198 	uint64_t count_max, bypass_addr = 0;
1199 	px_dma_win_t **win_pp = (px_dma_win_t **)&mp->dmai_winlst;
1200 	ddi_dma_cookie_t *cookie0_p;
1201 	io_attributes_t attr = PX_GET_TTE_ATTR(mp->dmai_rflags,
1202 	    mp->dmai_attr.dma_attr_flags);
1203 	dev_info_t *dip = px_p->px_dip;
1204 
1205 	ASSERT(PX_DMA_ISPTP(mp) || PX_DMA_ISBYPASS(mp));
1206 	if (PX_DMA_ISPTP(mp)) { /* ignore sys limits for peer-to-peer */
1207 		ddi_dma_attr_t *dev_attr_p = PX_DEV_ATTR(mp);
1208 		uint64_t nocross = dev_attr_p->dma_attr_seg;
1209 		px_pec_t *pec_p = px_p->px_pec_p;
1210 		px_iopfn_t pfn_last = PX_DMA_ISPTP32(mp) ?
1211 				pec_p->pec_last32_pfn - pec_p->pec_base32_pfn :
1212 				pec_p->pec_last64_pfn - pec_p->pec_base64_pfn;
1213 
1214 		if (nocross && (nocross < UINT32_MAX))
1215 			return (DDI_DMA_NOMAPPING);
1216 		if (dev_attr_p->dma_attr_align > MMU_PAGE_SIZE)
1217 			return (DDI_DMA_NOMAPPING);
1218 		pfn_lo = MMU_BTOP(dev_attr_p->dma_attr_addr_lo);
1219 		pfn_hi = MMU_BTOP(dev_attr_p->dma_attr_addr_hi);
1220 		pfn_hi = MIN(pfn_hi, pfn_last);
1221 		if ((pfn_lo > pfn_hi) || (pfn < pfn_lo))
1222 			return (DDI_DMA_NOMAPPING);
1223 
1224 		count_max = dev_attr_p->dma_attr_count_max;
1225 		count_max = MIN(count_max, nocross);
1226 		/*
1227 		 * the following count_max trim is not done because we are
1228 		 * making sure pfn_lo <= pfn <= pfn_hi inside the loop
1229 		 * count_max=MIN(count_max, MMU_PTOB(pfn_hi - pfn_lo + 1)-1);
1230 		 */
1231 	} else { /* bypass hi/lo/count_max have been processed by attr2hdl() */
1232 		count_max = mp->dmai_attr.dma_attr_count_max;
1233 		pfn_lo = MMU_BTOP(mp->dmai_attr.dma_attr_addr_lo);
1234 		pfn_hi = MMU_BTOP(mp->dmai_attr.dma_attr_addr_hi);
1235 
1236 		if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn),
1237 				attr, &bypass_addr) != DDI_SUCCESS) {
1238 			cmn_err(CE_WARN, "bypass cookie failure %lx\n", pfn);
1239 			return (DDI_DMA_NOMAPPING);
1240 		}
1241 		pfn = MMU_BTOP(bypass_addr);
1242 	}
1243 
1244 	/* pfn: absolute (bypass mode) or relative (p2p mode) */
1245 	for (prev_pfn = pfn, i = 1; i < npages;
1246 	    i++, prev_pfn = pfn, pfn_no++) {
1247 		pfn = PX_GET_MP_PFN1(mp, i);
1248 		if (bypass_addr) {
1249 			if (px_lib_iommu_getbypass(dip, MMU_PTOB(pfn), attr,
1250 					&bypass_addr) != DDI_SUCCESS) {
1251 				ret = DDI_DMA_NOMAPPING;
1252 				goto err;
1253 			}
1254 			pfn = MMU_BTOP(bypass_addr);
1255 		}
1256 		if ((pfn == prev_pfn + 1) &&
1257 				(MMU_PTOB(pfn_no + 1) - 1 <= count_max))
1258 			continue;
1259 		if ((pfn < pfn_lo) || (prev_pfn > pfn_hi)) {
1260 			ret = DDI_DMA_NOMAPPING;
1261 			goto err;
1262 		}
1263 		cookie_no++;
1264 		pfn_no = 0;
1265 		if (cookie_no < sgllen)
1266 			continue;
1267 
1268 		DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1269 			win_pfn0_index, i - 1, cookie_no);
1270 		if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no,
1271 			win_pfn0_index, i - 1, win_pp, count_max, bypass_addr))
1272 			goto err;
1273 
1274 		win_pp = &(*win_pp)->win_next;	/* win_pp = *(win_pp) */
1275 		win_no++;
1276 		win_pfn0_index = i;
1277 		cookie_no = 0;
1278 	}
1279 	if (pfn > pfn_hi) {
1280 		ret = DDI_DMA_NOMAPPING;
1281 		goto err;
1282 	}
1283 	cookie_no++;
1284 	DBG(DBG_BYPASS, mp->dmai_rdip, "newwin pfn[%x-%x] %x cks\n",
1285 		win_pfn0_index, i - 1, cookie_no);
1286 	if (ret = px_dma_newwin(dip, dmareq, mp, cookie_no, win_pfn0_index,
1287 		i - 1, win_pp, count_max, bypass_addr))
1288 		goto err;
1289 	win_no++;
1290 	px_dma_adjust(dmareq, mp, mp->dmai_winlst);
1291 	mp->dmai_nwin = win_no;
1292 	mp->dmai_rflags |= DDI_DMA_CONSISTENT | DMP_NOSYNC;
1293 	mp->dmai_rflags &= ~DDI_DMA_REDZONE;
1294 	mp->dmai_flags |= PX_DMAI_FLAGS_NOSYNC;
1295 	cookie0_p = (ddi_dma_cookie_t *)(PX_WINLST(mp) + 1);
1296 	mp->dmai_cookie = PX_WINLST(mp)->win_ncookies > 1 ? cookie0_p + 1 : 0;
1297 	mp->dmai_mapping = cookie0_p->dmac_laddress;
1298 
1299 	px_dma_freepfn(mp);
1300 	return (DDI_DMA_MAPPED);
1301 err:
1302 	px_dma_freewin(mp);
1303 	return (ret);
1304 }
1305 
1306 int
1307 px_dma_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_impl_t *mp,
1308 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1309 	uint_t cache_flags)
1310 {
1311 	switch (cmd) {
1312 	case DDI_DMA_SYNC:
1313 		return (DDI_SUCCESS);
1314 
1315 	case DDI_DMA_HTOC: {
1316 		off_t off = *offp;
1317 		ddi_dma_cookie_t *loop_cp, *cp;
1318 		px_dma_win_t *win_p = mp->dmai_winlst;
1319 
1320 		if (off >= mp->dmai_object.dmao_size)
1321 			return (DDI_FAILURE);
1322 
1323 		/* locate window */
1324 		while (win_p->win_offset + win_p->win_size <= off)
1325 			win_p = win_p->win_next;
1326 
1327 		loop_cp = cp = (ddi_dma_cookie_t *)(win_p + 1);
1328 		mp->dmai_offset = win_p->win_offset;
1329 		mp->dmai_size   = win_p->win_size;
1330 		mp->dmai_mapping = cp->dmac_laddress; /* cookie0 start addr */
1331 
1332 		/* adjust cookie addr/len if we are not on cookie boundary */
1333 		off -= win_p->win_offset;	   /* offset within window */
1334 		for (; off >= loop_cp->dmac_size; loop_cp++)
1335 			off -= loop_cp->dmac_size; /* offset within cookie */
1336 
1337 		mp->dmai_cookie = loop_cp + 1;
1338 		win_p->win_curseg = loop_cp - cp;
1339 		cp = (ddi_dma_cookie_t *)objp;
1340 		MAKE_DMA_COOKIE(cp, loop_cp->dmac_laddress + off,
1341 			loop_cp->dmac_size - off);
1342 
1343 		DBG(DBG_DMA_CTL, dip,
1344 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1345 			cp->dmac_laddress, cp->dmac_size);
1346 		}
1347 		return (DDI_SUCCESS);
1348 
1349 	case DDI_DMA_REPWIN:
1350 		*offp = mp->dmai_offset;
1351 		*lenp = mp->dmai_size;
1352 		return (DDI_SUCCESS);
1353 
1354 	case DDI_DMA_MOVWIN: {
1355 		off_t off = *offp;
1356 		ddi_dma_cookie_t *cp;
1357 		px_dma_win_t *win_p = mp->dmai_winlst;
1358 
1359 		if (off >= mp->dmai_object.dmao_size)
1360 			return (DDI_FAILURE);
1361 
1362 		/* locate window */
1363 		while (win_p->win_offset + win_p->win_size <= off)
1364 			win_p = win_p->win_next;
1365 
1366 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1367 		mp->dmai_offset = win_p->win_offset;
1368 		mp->dmai_size   = win_p->win_size;
1369 		mp->dmai_mapping = cp->dmac_laddress;	/* cookie0 star addr */
1370 		mp->dmai_cookie = cp + 1;
1371 		win_p->win_curseg = 0;
1372 
1373 		*(ddi_dma_cookie_t *)objp = *cp;
1374 		*offp = win_p->win_offset;
1375 		*lenp = win_p->win_size;
1376 		DBG(DBG_DMA_CTL, dip,
1377 			"HTOC: cookie - dmac_laddress=%p dmac_size=%x\n",
1378 			cp->dmac_laddress, cp->dmac_size);
1379 		}
1380 		return (DDI_SUCCESS);
1381 
1382 	case DDI_DMA_NEXTWIN: {
1383 		px_dma_win_t *win_p = *(px_dma_win_t **)offp;
1384 		px_dma_win_t **nw_pp = (px_dma_win_t **)objp;
1385 		ddi_dma_cookie_t *cp;
1386 		if (!win_p) {
1387 			*nw_pp = mp->dmai_winlst;
1388 			return (DDI_SUCCESS);
1389 		}
1390 
1391 		if (win_p->win_offset != mp->dmai_offset)
1392 			return (DDI_DMA_STALE);
1393 		if (!win_p->win_next)
1394 			return (DDI_DMA_DONE);
1395 		win_p = win_p->win_next;
1396 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1397 		mp->dmai_offset = win_p->win_offset;
1398 		mp->dmai_size   = win_p->win_size;
1399 		mp->dmai_mapping = cp->dmac_laddress;   /* cookie0 star addr */
1400 		mp->dmai_cookie = cp + 1;
1401 		win_p->win_curseg = 0;
1402 		*nw_pp = win_p;
1403 		}
1404 		return (DDI_SUCCESS);
1405 
1406 	case DDI_DMA_NEXTSEG: {
1407 		px_dma_win_t *w_p = *(px_dma_win_t **)offp;
1408 		if (w_p->win_offset != mp->dmai_offset)
1409 			return (DDI_DMA_STALE);
1410 		if (w_p->win_curseg + 1 >= w_p->win_ncookies)
1411 			return (DDI_DMA_DONE);
1412 		w_p->win_curseg++;
1413 		}
1414 		*(ddi_dma_seg_t *)objp = (ddi_dma_seg_t)mp;
1415 		return (DDI_SUCCESS);
1416 
1417 	case DDI_DMA_SEGTOC: {
1418 		px_dma_win_t *win_p = mp->dmai_winlst;
1419 		off_t off = mp->dmai_offset;
1420 		ddi_dma_cookie_t *cp;
1421 		int i;
1422 
1423 		/* locate active window */
1424 		for (; win_p->win_offset != off; win_p = win_p->win_next);
1425 		cp = (ddi_dma_cookie_t *)(win_p + 1);
1426 		for (i = 0; i < win_p->win_curseg; i++, cp++)
1427 			off += cp->dmac_size;
1428 		*offp = off;
1429 		*lenp = cp->dmac_size;
1430 		*(ddi_dma_cookie_t *)objp = *cp;	/* copy cookie */
1431 		}
1432 		return (DDI_SUCCESS);
1433 
1434 	case DDI_DMA_COFF: {
1435 		px_dma_win_t *win_p;
1436 		ddi_dma_cookie_t *cp;
1437 		uint64_t addr, key = ((ddi_dma_cookie_t *)offp)->dmac_laddress;
1438 		size_t win_off;
1439 
1440 		for (win_p = mp->dmai_winlst; win_p; win_p = win_p->win_next) {
1441 			int i;
1442 			win_off = 0;
1443 			cp = (ddi_dma_cookie_t *)(win_p + 1);
1444 			for (i = 0; i < win_p->win_ncookies; i++, cp++) {
1445 				size_t sz = cp->dmac_size;
1446 
1447 				addr = cp->dmac_laddress;
1448 				if ((addr <= key) && (addr + sz >= key))
1449 					goto found;
1450 				win_off += sz;
1451 			}
1452 		}
1453 		return (DDI_FAILURE);
1454 found:
1455 		*objp = (caddr_t)(win_p->win_offset + win_off + (key - addr));
1456 		return (DDI_SUCCESS);
1457 		}
1458 	default:
1459 		DBG(DBG_DMA_CTL, dip, "unknown command (%x): rdip=%s%d\n",
1460 			cmd, ddi_driver_name(rdip), ddi_get_instance(rdip));
1461 		break;
1462 	}
1463 	return (DDI_FAILURE);
1464 }
1465 
1466 static void
1467 px_dvma_debug_init(px_mmu_t *mmu_p)
1468 {
1469 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
1470 	ASSERT(MUTEX_HELD(&mmu_p->dvma_debug_lock));
1471 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat ON", mmu_p);
1472 
1473 	mmu_p->dvma_alloc_rec = kmem_alloc(sz, KM_SLEEP);
1474 	mmu_p->dvma_free_rec = kmem_alloc(sz, KM_SLEEP);
1475 
1476 	mmu_p->dvma_active_list = NULL;
1477 	mmu_p->dvma_alloc_rec_index = 0;
1478 	mmu_p->dvma_free_rec_index = 0;
1479 	mmu_p->dvma_active_count = 0;
1480 }
1481 
1482 void
1483 px_dvma_debug_fini(px_mmu_t *mmu_p)
1484 {
1485 	struct px_dvma_rec *prev, *ptr;
1486 	size_t sz = sizeof (struct px_dvma_rec) * px_dvma_debug_rec;
1487 	uint64_t mask = ~(1ull << mmu_p->mmu_inst);
1488 	cmn_err(CE_NOTE, "PCI Express DVMA %p stat OFF", mmu_p);
1489 
1490 	kmem_free(mmu_p->dvma_alloc_rec, sz);
1491 	kmem_free(mmu_p->dvma_free_rec, sz);
1492 	mmu_p->dvma_alloc_rec = mmu_p->dvma_free_rec = NULL;
1493 
1494 	prev = mmu_p->dvma_active_list;
1495 	if (!prev)
1496 		return;
1497 	for (ptr = prev->next; ptr; prev = ptr, ptr = ptr->next)
1498 		kmem_free(prev, sizeof (struct px_dvma_rec));
1499 	kmem_free(prev, sizeof (struct px_dvma_rec));
1500 
1501 	mmu_p->dvma_active_list = NULL;
1502 	mmu_p->dvma_alloc_rec_index = 0;
1503 	mmu_p->dvma_free_rec_index = 0;
1504 	mmu_p->dvma_active_count = 0;
1505 
1506 	px_dvma_debug_off &= mask;
1507 	px_dvma_debug_on &= mask;
1508 }
1509 
1510 void
1511 px_dvma_alloc_debug(px_mmu_t *mmu_p, char *address, uint_t len,
1512 	ddi_dma_impl_t *mp)
1513 {
1514 	struct px_dvma_rec *ptr;
1515 	mutex_enter(&mmu_p->dvma_debug_lock);
1516 
1517 	if (!mmu_p->dvma_alloc_rec)
1518 		px_dvma_debug_init(mmu_p);
1519 	if (PX_DVMA_DBG_OFF(mmu_p)) {
1520 		px_dvma_debug_fini(mmu_p);
1521 		goto done;
1522 	}
1523 
1524 	ptr = &mmu_p->dvma_alloc_rec[mmu_p->dvma_alloc_rec_index];
1525 	ptr->dvma_addr = address;
1526 	ptr->len = len;
1527 	ptr->mp = mp;
1528 	if (++mmu_p->dvma_alloc_rec_index == px_dvma_debug_rec)
1529 		mmu_p->dvma_alloc_rec_index = 0;
1530 
1531 	ptr = kmem_alloc(sizeof (struct px_dvma_rec), KM_SLEEP);
1532 	ptr->dvma_addr = address;
1533 	ptr->len = len;
1534 	ptr->mp = mp;
1535 
1536 	ptr->next = mmu_p->dvma_active_list;
1537 	mmu_p->dvma_active_list = ptr;
1538 	mmu_p->dvma_active_count++;
1539 done:
1540 	mutex_exit(&mmu_p->dvma_debug_lock);
1541 }
1542 
1543 void
1544 px_dvma_free_debug(px_mmu_t *mmu_p, char *address, uint_t len,
1545     ddi_dma_impl_t *mp)
1546 {
1547 	struct px_dvma_rec *ptr, *ptr_save;
1548 	mutex_enter(&mmu_p->dvma_debug_lock);
1549 
1550 	if (!mmu_p->dvma_alloc_rec)
1551 		px_dvma_debug_init(mmu_p);
1552 	if (PX_DVMA_DBG_OFF(mmu_p)) {
1553 		px_dvma_debug_fini(mmu_p);
1554 		goto done;
1555 	}
1556 
1557 	ptr = &mmu_p->dvma_free_rec[mmu_p->dvma_free_rec_index];
1558 	ptr->dvma_addr = address;
1559 	ptr->len = len;
1560 	ptr->mp = mp;
1561 	if (++mmu_p->dvma_free_rec_index == px_dvma_debug_rec)
1562 		mmu_p->dvma_free_rec_index = 0;
1563 
1564 	ptr_save = mmu_p->dvma_active_list;
1565 	for (ptr = ptr_save; ptr; ptr = ptr->next) {
1566 		if ((ptr->dvma_addr == address) && (ptr->len = len))
1567 			break;
1568 		ptr_save = ptr;
1569 	}
1570 	if (!ptr) {
1571 		cmn_err(CE_WARN, "bad dvma free addr=%lx len=%x",
1572 			(long)address, len);
1573 		goto done;
1574 	}
1575 	if (ptr == mmu_p->dvma_active_list)
1576 		mmu_p->dvma_active_list = ptr->next;
1577 	else
1578 		ptr_save->next = ptr->next;
1579 	kmem_free(ptr, sizeof (struct px_dvma_rec));
1580 	mmu_p->dvma_active_count--;
1581 done:
1582 	mutex_exit(&mmu_p->dvma_debug_lock);
1583 }
1584 
1585 #ifdef	DEBUG
1586 void
1587 px_dump_dma_handle(uint64_t flag, dev_info_t *dip, ddi_dma_impl_t *hp)
1588 {
1589 	DBG(flag, dip, "mp(%p): flags=%x mapping=%lx xfer_size=%x\n",
1590 		hp, hp->dmai_inuse, hp->dmai_mapping, hp->dmai_size);
1591 	DBG(flag|DBG_CONT, dip, "\tnpages=%x roffset=%x rflags=%x nwin=%x\n",
1592 		hp->dmai_ndvmapages, hp->dmai_roffset, hp->dmai_rflags,
1593 		hp->dmai_nwin);
1594 	DBG(flag|DBG_CONT, dip, "\twinsize=%x tte=%p pfnlst=%p pfn0=%p\n",
1595 		hp->dmai_winsize, hp->dmai_tte, hp->dmai_pfnlst, hp->dmai_pfn0);
1596 	DBG(flag|DBG_CONT, dip, "\twinlst=%x obj=%p attr=%p ckp=%p\n",
1597 		hp->dmai_winlst, &hp->dmai_object, &hp->dmai_attr,
1598 		hp->dmai_cookie);
1599 }
1600 #endif	/* DEBUG */
1601