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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * hci1394_buf.c
297c478bd9Sstevel@tonic-gate  *   These routines handle IO mapped memory.  They include routines to alloc and
307c478bd9Sstevel@tonic-gate  *   free  IO mapped memory and a routine to get the adapters default dma
317c478bd9Sstevel@tonic-gate  *   attributes. These routines are meant to be called from the base context.
327c478bd9Sstevel@tonic-gate  *   They should not be called from an interrupt handler.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
437c478bd9Sstevel@tonic-gate #include <sys/1394/adapters/hci1394.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * hci1394_buffer_attr_get()
487c478bd9Sstevel@tonic-gate  *    returns (in dma_attr) the default DMA attributes for this adapter.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate void
hci1394_buf_attr_get(ddi_dma_attr_t * dma_attr)517c478bd9Sstevel@tonic-gate hci1394_buf_attr_get(ddi_dma_attr_t *dma_attr)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_version = DMA_ATTR_V0;
547c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_addr_lo = (uint64_t)0x00000000;
557c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_addr_hi = (uint64_t)0xFFFFFFFF;
567c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_count_max = (uint64_t)0xFFFFFFFF;
577c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_align = 64;
587c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_burstsizes = 0x3FF;
597c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_minxfer = 1;
607c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_maxxfer = (uint64_t)0xFFFFFFFF;
617c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_seg = (uint64_t)0xFFFFFFFF;
627c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_sgllen = 0x7FFFFFFF;
637c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_granular = 4;
647c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_flags = 0;
657c478bd9Sstevel@tonic-gate 
66*86ef0a63SRichard Lowe #if defined(__x86)
677c478bd9Sstevel@tonic-gate 	/* XXX - Not sure why x86 wants the dma_attr_seg to be 0x7FFF?? */
687c478bd9Sstevel@tonic-gate 	dma_attr->dma_attr_seg = (uint64_t)0x7FFF;
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * hci1394_buf_alloc()
757c478bd9Sstevel@tonic-gate  *    Allocate an IO mapped buffer. drvinfo is passed in and contains generic
767c478bd9Sstevel@tonic-gate  *    driver info, like dip, instance, buf_attr, etc.  Parms is passed in and
777c478bd9Sstevel@tonic-gate  *    contains the input parameters for alloc, ow much memory to alloc, how many
787c478bd9Sstevel@tonic-gate  *    cookies can we handle, and alignment requirements. info is returned with
797c478bd9Sstevel@tonic-gate  *    all the info about the mapped buffer.  handle is returned. It should be
807c478bd9Sstevel@tonic-gate  *    used when calling hci1394_buf_free().
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate int
hci1394_buf_alloc(hci1394_drvinfo_t * drvinfo,hci1394_buf_parms_t * parms,hci1394_buf_info_t * info,hci1394_buf_handle_t * handle)837c478bd9Sstevel@tonic-gate hci1394_buf_alloc(hci1394_drvinfo_t *drvinfo, hci1394_buf_parms_t *parms,
847c478bd9Sstevel@tonic-gate     hci1394_buf_info_t *info, hci1394_buf_handle_t *handle)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t dma_attr;
877c478bd9Sstevel@tonic-gate 	hci1394_buf_t *buf;
887c478bd9Sstevel@tonic-gate 	int status;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	ASSERT(drvinfo != NULL);
927c478bd9Sstevel@tonic-gate 	ASSERT(parms != NULL);
937c478bd9Sstevel@tonic-gate 	ASSERT(info != NULL);
947c478bd9Sstevel@tonic-gate 	ASSERT(handle != NULL);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/* alloc the space to keep track of the buffer */
977c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(sizeof (hci1394_buf_t), KM_SLEEP);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* setup the return parameter */
1007c478bd9Sstevel@tonic-gate 	*handle = buf;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/* save away pointer to general info */
1037c478bd9Sstevel@tonic-gate 	buf->bu_drvinfo = drvinfo;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Get the default DMA attributes and override sgllen and alignment */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	_NOTE(SCHEME_PROTECTS_DATA("unique (on stack)", ddi_dma_attr_t))
1087c478bd9Sstevel@tonic-gate 	hci1394_buf_attr_get(&dma_attr);
1097c478bd9Sstevel@tonic-gate 	dma_attr.dma_attr_sgllen = parms->bp_max_cookies;
1107c478bd9Sstevel@tonic-gate 	dma_attr.dma_attr_align = parms->bp_alignment;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	status = ddi_dma_alloc_handle(drvinfo->di_dip, &dma_attr,
1137c478bd9Sstevel@tonic-gate 	    DDI_DMA_SLEEP, NULL, &buf->bu_dma_handle);
1147c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
1157c478bd9Sstevel@tonic-gate 		kmem_free(buf, sizeof (hci1394_buf_t));
1167c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	status = ddi_dma_mem_alloc(buf->bu_dma_handle, parms->bp_length,
1207c478bd9Sstevel@tonic-gate 	    &drvinfo->di_buf_attr, DDI_DMA_STREAMING, DDI_DMA_SLEEP,
1217c478bd9Sstevel@tonic-gate 	    NULL, &info->bi_kaddr, &info->bi_real_length, &buf->bu_handle);
1227c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
1237c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&buf->bu_dma_handle);
1247c478bd9Sstevel@tonic-gate 		kmem_free(buf, sizeof (hci1394_buf_t));
1257c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	status = ddi_dma_addr_bind_handle(buf->bu_dma_handle, NULL,
1297c478bd9Sstevel@tonic-gate 	    info->bi_kaddr, info->bi_real_length, DDI_DMA_RDWR |
1307c478bd9Sstevel@tonic-gate 	    DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, &info->bi_cookie,
1317c478bd9Sstevel@tonic-gate 	    &info->bi_cookie_count);
1327c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
1337c478bd9Sstevel@tonic-gate 		ddi_dma_mem_free(&buf->bu_handle);
1347c478bd9Sstevel@tonic-gate 		ddi_dma_free_handle(&buf->bu_dma_handle);
1357c478bd9Sstevel@tonic-gate 		kmem_free(buf, sizeof (hci1394_buf_t));
1367c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* setup rest of buffer info returned to caller */
1407c478bd9Sstevel@tonic-gate 	info->bi_handle = buf->bu_handle;
1417c478bd9Sstevel@tonic-gate 	info->bi_dma_handle = buf->bu_dma_handle;
1427c478bd9Sstevel@tonic-gate 	info->bi_length = parms->bp_length;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * hci1394_buf_free()
1507c478bd9Sstevel@tonic-gate  *    Free IO mapped buffer. Notice that a pointer to the handle is used for
1517c478bd9Sstevel@tonic-gate  *    the parameter.  free() will set your handle to NULL before returning.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate void
hci1394_buf_free(hci1394_buf_handle_t * handle)1547c478bd9Sstevel@tonic-gate hci1394_buf_free(hci1394_buf_handle_t *handle)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	hci1394_buf_t *buf;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	ASSERT(handle != NULL);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	buf = *handle;
1617c478bd9Sstevel@tonic-gate 	(void) ddi_dma_unbind_handle(buf->bu_dma_handle);
1627c478bd9Sstevel@tonic-gate 	ddi_dma_mem_free(&buf->bu_handle);
1637c478bd9Sstevel@tonic-gate 	ddi_dma_free_handle(&buf->bu_dma_handle);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* free the space to keep track of the buffer */
1667c478bd9Sstevel@tonic-gate 	kmem_free(buf, sizeof (hci1394_buf_t));
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* set the handle to NULL to help catch bugs */
1697c478bd9Sstevel@tonic-gate 	*handle = NULL;
1707c478bd9Sstevel@tonic-gate }
171