xref: /illumos-gate/usr/src/uts/common/io/1394/adapters/hci1394_buf.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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 2004 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 /*
30  * hci1394_buf.c
31  *   These routines handle IO mapped memory.  They include routines to alloc and
32  *   free  IO mapped memory and a routine to get the adapters default dma
33  *   attributes. These routines are meant to be called from the base context.
34  *   They should not be called from an interrupt handler.
35  */
36 
37 #include <sys/conf.h>
38 #include <sys/ddi.h>
39 #include <sys/modctl.h>
40 #include <sys/stat.h>
41 #include <sys/sunddi.h>
42 #include <sys/kmem.h>
43 
44 #include <sys/1394/h1394.h>
45 #include <sys/1394/adapters/hci1394.h>
46 
47 
48 /*
49  * hci1394_buffer_attr_get()
50  *    returns (in dma_attr) the default DMA attributes for this adapter.
51  */
52 void
53 hci1394_buf_attr_get(ddi_dma_attr_t *dma_attr)
54 {
55 	dma_attr->dma_attr_version = DMA_ATTR_V0;
56 	dma_attr->dma_attr_addr_lo = (uint64_t)0x00000000;
57 	dma_attr->dma_attr_addr_hi = (uint64_t)0xFFFFFFFF;
58 	dma_attr->dma_attr_count_max = (uint64_t)0xFFFFFFFF;
59 	dma_attr->dma_attr_align = 64;
60 	dma_attr->dma_attr_burstsizes = 0x3FF;
61 	dma_attr->dma_attr_minxfer = 1;
62 	dma_attr->dma_attr_maxxfer = (uint64_t)0xFFFFFFFF;
63 	dma_attr->dma_attr_seg = (uint64_t)0xFFFFFFFF;
64 	dma_attr->dma_attr_sgllen = 0x7FFFFFFF;
65 	dma_attr->dma_attr_granular = 4;
66 	dma_attr->dma_attr_flags = 0;
67 
68 #if defined(__i386) || defined(__amd64)
69 	/* XXX - Not sure why x86 wants the dma_attr_seg to be 0x7FFF?? */
70 	dma_attr->dma_attr_seg = (uint64_t)0x7FFF;
71 #endif
72 }
73 
74 
75 /*
76  * hci1394_buf_alloc()
77  *    Allocate an IO mapped buffer. drvinfo is passed in and contains generic
78  *    driver info, like dip, instance, buf_attr, etc.  Parms is passed in and
79  *    contains the input parameters for alloc, ow much memory to alloc, how many
80  *    cookies can we handle, and alignment requirements. info is returned with
81  *    all the info about the mapped buffer.  handle is returned. It should be
82  *    used when calling hci1394_buf_free().
83  */
84 int
85 hci1394_buf_alloc(hci1394_drvinfo_t *drvinfo, hci1394_buf_parms_t *parms,
86     hci1394_buf_info_t *info, hci1394_buf_handle_t *handle)
87 {
88 	ddi_dma_attr_t dma_attr;
89 	hci1394_buf_t *buf;
90 	int status;
91 
92 
93 	ASSERT(drvinfo != NULL);
94 	ASSERT(parms != NULL);
95 	ASSERT(info != NULL);
96 	ASSERT(handle != NULL);
97 	TNF_PROBE_0_DEBUG(hci1394_buf_alloc_enter, HCI1394_TNF_HAL_STACK, "");
98 
99 	/* alloc the space to keep track of the buffer */
100 	buf = kmem_alloc(sizeof (hci1394_buf_t), KM_SLEEP);
101 
102 	/* setup the return parameter */
103 	*handle = buf;
104 
105 	/* save away pointer to general info */
106 	buf->bu_drvinfo = drvinfo;
107 
108 	/* Get the default DMA attributes and override sgllen and alignment */
109 
110 	_NOTE(SCHEME_PROTECTS_DATA("unique (on stack)", ddi_dma_attr_t))
111 	hci1394_buf_attr_get(&dma_attr);
112 	dma_attr.dma_attr_sgllen = parms->bp_max_cookies;
113 	dma_attr.dma_attr_align = parms->bp_alignment;
114 
115 	status = ddi_dma_alloc_handle(drvinfo->di_dip, &dma_attr,
116 	    DDI_DMA_SLEEP, NULL, &buf->bu_dma_handle);
117 	if (status != DDI_SUCCESS) {
118 		kmem_free(buf, sizeof (hci1394_buf_t));
119 		TNF_PROBE_0(hci1394_buf_alloc_dah_fail, HCI1394_TNF_HAL_ERROR,
120 		    "");
121 		TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
122 		    "");
123 		return (DDI_FAILURE);
124 	}
125 
126 	status = ddi_dma_mem_alloc(buf->bu_dma_handle, parms->bp_length,
127 	    &drvinfo->di_buf_attr, DDI_DMA_STREAMING, DDI_DMA_SLEEP,
128 	    NULL, &info->bi_kaddr, &info->bi_real_length, &buf->bu_handle);
129 	if (status != DDI_SUCCESS) {
130 		ddi_dma_free_handle(&buf->bu_dma_handle);
131 		kmem_free(buf, sizeof (hci1394_buf_t));
132 		TNF_PROBE_0(hci1394_buf_alloc_dam_fail, HCI1394_TNF_HAL_ERROR,
133 		    "");
134 		TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
135 		    "");
136 		return (DDI_FAILURE);
137 	}
138 
139 	status = ddi_dma_addr_bind_handle(buf->bu_dma_handle, NULL,
140 	    info->bi_kaddr, info->bi_real_length, DDI_DMA_RDWR |
141 	    DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, &info->bi_cookie,
142 	    &info->bi_cookie_count);
143 	if (status != DDI_SUCCESS) {
144 		ddi_dma_mem_free(&buf->bu_handle);
145 		ddi_dma_free_handle(&buf->bu_dma_handle);
146 		kmem_free(buf, sizeof (hci1394_buf_t));
147 		TNF_PROBE_0(hci1394_buf_alloc_dbh_fail, HCI1394_TNF_HAL_ERROR,
148 		    "");
149 		TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK,
150 		    "");
151 		return (DDI_FAILURE);
152 	}
153 
154 	/* setup rest of buffer info returned to caller */
155 	info->bi_handle = buf->bu_handle;
156 	info->bi_dma_handle = buf->bu_dma_handle;
157 	info->bi_length = parms->bp_length;
158 
159 	TNF_PROBE_0_DEBUG(hci1394_buf_alloc_exit, HCI1394_TNF_HAL_STACK, "");
160 
161 	return (DDI_SUCCESS);
162 }
163 
164 
165 /*
166  * hci1394_buf_free()
167  *    Free IO mapped buffer. Notice that a pointer to the handle is used for
168  *    the parameter.  free() will set your handle to NULL before returning.
169  */
170 void
171 hci1394_buf_free(hci1394_buf_handle_t *handle)
172 {
173 	hci1394_buf_t *buf;
174 
175 	ASSERT(handle != NULL);
176 	TNF_PROBE_0_DEBUG(hci1394_buf_free_enter, HCI1394_TNF_HAL_STACK, "");
177 
178 	buf = *handle;
179 	(void) ddi_dma_unbind_handle(buf->bu_dma_handle);
180 	ddi_dma_mem_free(&buf->bu_handle);
181 	ddi_dma_free_handle(&buf->bu_dma_handle);
182 
183 	/* free the space to keep track of the buffer */
184 	kmem_free(buf, sizeof (hci1394_buf_t));
185 
186 	/* set the handle to NULL to help catch bugs */
187 	*handle = NULL;
188 
189 	TNF_PROBE_0_DEBUG(hci1394_buf_free_exit, HCI1394_TNF_HAL_STACK, "");
190 }
191