xref: /illumos-gate/usr/src/uts/sun4/io/efcode/fc_ddi.c (revision 48bbca81)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*
234ab75253Smrj  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
387c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
397c478bd9Sstevel@tonic-gate #include <sys/fcode.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
42*48bbca81SDaniel Hoffman  * We want to call the attachment point's dma ctl op, not its parent's
434ab75253Smrj  * dma ctl op, so we have to code this ourselves.
447c478bd9Sstevel@tonic-gate  */
454ab75253Smrj 
467c478bd9Sstevel@tonic-gate int
fc_ddi_dma_alloc_handle(dev_info_t * dip,ddi_dma_attr_t * attr,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)474ab75253Smrj fc_ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
484ab75253Smrj     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
497c478bd9Sstevel@tonic-gate {
504ab75253Smrj 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
514ab75253Smrj 	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);
527c478bd9Sstevel@tonic-gate 
534ab75253Smrj 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
544ab75253Smrj 	return ((*funcp)(dip, dip, attr, waitfp, arg, handlep));
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate int
fc_ddi_dma_buf_bind_handle(ddi_dma_handle_t handle,struct buf * bp,uint_t flags,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_cookie_t * cookiep,uint_t * ccountp)584ab75253Smrj fc_ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
594ab75253Smrj     uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
604ab75253Smrj     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
614ab75253Smrj {
624ab75253Smrj 	struct ddi_dma_req dmareq;
634ab75253Smrj 	ddi_dma_impl_t *hp;
644ab75253Smrj 	dev_info_t *dip;
654ab75253Smrj 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
664ab75253Smrj 	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);
674ab75253Smrj 
684ab75253Smrj 	hp = (ddi_dma_impl_t *)handle;
694ab75253Smrj 	dip = hp->dmai_rdip;
704ab75253Smrj 
714ab75253Smrj 	dmareq.dmar_flags = flags;
724ab75253Smrj 	dmareq.dmar_fp = waitfp;
734ab75253Smrj 	dmareq.dmar_arg = arg;
744ab75253Smrj 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
754ab75253Smrj 
764ab75253Smrj 	if ((bp->b_flags & (B_PAGEIO|B_REMAPPED)) == B_PAGEIO) {
774ab75253Smrj 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
784ab75253Smrj 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
794ab75253Smrj 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
804ab75253Smrj 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
814ab75253Smrj 	} else {
824ab75253Smrj 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
834ab75253Smrj 		if ((bp->b_flags & (B_SHADOW|B_REMAPPED)) == B_SHADOW) {
844ab75253Smrj 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
854ab75253Smrj 							bp->b_shadow;
864ab75253Smrj 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
874ab75253Smrj 		} else {
884ab75253Smrj 			dmareq.dmar_object.dmao_type =
894ab75253Smrj 				(bp->b_flags & (B_PHYS | B_REMAPPED))?
904ab75253Smrj 				DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
914ab75253Smrj 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
924ab75253Smrj 		}
934ab75253Smrj 
944ab75253Smrj 		/*
954ab75253Smrj 		 * If the buffer has no proc pointer, or the proc
964ab75253Smrj 		 * struct has the kernel address space, or the buffer has
974ab75253Smrj 		 * been marked B_REMAPPED (meaning that it is now
984ab75253Smrj 		 * mapped into the kernel's address space), then
994ab75253Smrj 		 * the address space is kas (kernel address space).
1004ab75253Smrj 		 */
1014ab75253Smrj 		if (bp->b_proc == NULL || bp->b_proc->p_as == &kas ||
1024ab75253Smrj 		    (bp->b_flags & B_REMAPPED) != 0) {
1034ab75253Smrj 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
1044ab75253Smrj 		} else {
1054ab75253Smrj 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
1064ab75253Smrj 			    bp->b_proc->p_as;
1074ab75253Smrj 		}
1084ab75253Smrj 	}
1094ab75253Smrj 
1104ab75253Smrj 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
1114ab75253Smrj 	return ((*funcp)(dip, dip, handle, &dmareq, cookiep, ccountp));
1124ab75253Smrj }
1134ab75253Smrj 
1144ab75253Smrj int
fc_ddi_dma_unbind_handle(ddi_dma_handle_t handle)1154ab75253Smrj fc_ddi_dma_unbind_handle(ddi_dma_handle_t handle)
1164ab75253Smrj {
1174ab75253Smrj 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
1184ab75253Smrj 	ddi_dma_impl_t *hp;
1194ab75253Smrj 	dev_info_t *dip;
1204ab75253Smrj 
1214ab75253Smrj 	hp = (ddi_dma_impl_t *)handle;
1224ab75253Smrj 	dip = hp->dmai_rdip;
1234ab75253Smrj 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
1244ab75253Smrj 	return ((*funcp)(dip, dip, handle));
1254ab75253Smrj }
1264ab75253Smrj 
1274ab75253Smrj void
fc_ddi_dma_free_handle(ddi_dma_handle_t * handlep)1284ab75253Smrj fc_ddi_dma_free_handle(ddi_dma_handle_t *handlep)
1297c478bd9Sstevel@tonic-gate {
1304ab75253Smrj 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
1314ab75253Smrj 	ddi_dma_impl_t *hp;
1324ab75253Smrj 	dev_info_t *dip;
1337c478bd9Sstevel@tonic-gate 
1344ab75253Smrj 	hp = (ddi_dma_impl_t *)*handlep;
1354ab75253Smrj 	dip = hp->dmai_rdip;
1364ab75253Smrj 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
1374ab75253Smrj 	(void) (*funcp)(dip, dip, *handlep);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
fc_ddi_dma_sync(ddi_dma_handle_t h,off_t o,size_t l,uint_t whom)1417c478bd9Sstevel@tonic-gate fc_ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
1447c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
1457c478bd9Sstevel@tonic-gate 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
1467c478bd9Sstevel@tonic-gate 		size_t, uint_t);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * the DMA nexus driver will set DMP_NOSYNC if the
1507c478bd9Sstevel@tonic-gate 	 * platform does not require any sync operation. For
1517c478bd9Sstevel@tonic-gate 	 * example if the memory is uncached or consistent
1527c478bd9Sstevel@tonic-gate 	 * and without any I/O write buffers involved.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
1557c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	dip = hp->dmai_rdip;
1587c478bd9Sstevel@tonic-gate 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
1597c478bd9Sstevel@tonic-gate 	return ((*funcp)(dip, dip, h, o, l, whom));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Create untyped properties, just like 1275 properties.
1647c478bd9Sstevel@tonic-gate  * XXX: Assumes property encoding is the natural byte order.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate int
fc_ndi_prop_update(dev_t match_dev,dev_info_t * dip,char * name,uchar_t * data,uint_t nelements)1677c478bd9Sstevel@tonic-gate fc_ndi_prop_update(dev_t match_dev, dev_info_t *dip,
1687c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1717c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY,
1727c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
1737c478bd9Sstevel@tonic-gate }
174