summaryrefslogtreecommitdiffstats
path: root/sys/dev/oce/oce_util.c
blob: 7b227acb4a11bff401ffbcd50db2dc69bb65d6a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*-
 * Copyright (C) 2012 Emulex
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Emulex Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contact Information:
 * freebsd-drivers@emulex.com
 *
 * Emulex
 * 3333 Susan Street
 * Costa Mesa, CA 92626
 */

/* $FreeBSD$ */

#include "oce_if.h"

static void oce_dma_map_ring(void *arg,
			     bus_dma_segment_t *segs,
			     int nseg,
			     int error);

/**
 * @brief		Allocate DMA memory
 * @param sc		software handle to the device
 * @param size		bus size
 * @param dma		dma memory area
 * @param flags		creation flags
 * @returns		0 on success, error otherwize
 */
int
oce_dma_alloc(POCE_SOFTC sc, bus_size_t size, POCE_DMA_MEM dma, int flags)
{
	int rc;


	memset(dma, 0, sizeof(OCE_DMA_MEM));

	rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
				8, 0,
				BUS_SPACE_MAXADDR,
				BUS_SPACE_MAXADDR,
				NULL, NULL,
				size, 1, size, 0, NULL, NULL, &dma->tag);

	if (rc == 0) {
		rc = bus_dmamem_alloc(dma->tag,
				      &dma->ptr,
				      BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
				      &dma->map);
	}

	dma->paddr = 0;
	if (rc == 0) {
		rc = bus_dmamap_load(dma->tag,
				     dma->map,
				     dma->ptr,
				     size,
				     oce_dma_map_addr,
				     &dma->paddr, flags | BUS_DMA_NOWAIT);
		if (dma->paddr == 0)
			rc = ENXIO;
	}

	if (rc != 0)
		oce_dma_free(sc, dma);

	return rc;
}

/**
 * @brief		Free DMA memory
 * @param sc		software handle to the device
 * @param dma		dma area to free
 */
void
oce_dma_free(POCE_SOFTC sc, POCE_DMA_MEM dma)
{
	if (dma->tag == NULL)
		return;

	if (dma->map != NULL) {
		bus_dmamap_sync(dma->tag, dma->map,
				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(dma->tag, dma->map);
	}

	if (dma->ptr != NULL) {
		bus_dmamem_free(dma->tag, dma->ptr, dma->map);
		dma->map = NULL;
		dma->ptr = NULL;
	}

	bus_dma_tag_destroy(dma->tag);
	dma->tag = NULL;
	
	return;
}



/**
 * @brief		Map DMA memory segment addresses
 * @param arg		physical address pointer
 * @param segs		dma memory segments
 * @param nseg		number of dma memory segments
 * @param error		if error, zeroes the physical address
 */
void
oce_dma_map_addr(void *arg, bus_dma_segment_t * segs, int nseg, int error)
{
	bus_addr_t *paddr = arg;

	if (error)
		*paddr = 0;
	else
		*paddr = segs->ds_addr;
}



/**
 * @brief		Destroy a ring buffer
 * @param sc		software handle to the device
 * @param ring		ring buffer
 */

void
oce_destroy_ring_buffer(POCE_SOFTC sc, oce_ring_buffer_t *ring)
{
	oce_dma_free(sc, &ring->dma);
	free(ring, M_DEVBUF);
}



oce_ring_buffer_t *
oce_create_ring_buffer(POCE_SOFTC sc,
		uint32_t q_len, uint32_t item_size)
{
	uint32_t size = q_len * item_size;
	int rc;
	oce_ring_buffer_t *ring;


	ring = malloc(sizeof(oce_ring_buffer_t), M_DEVBUF, M_NOWAIT | M_ZERO);
	if (ring == NULL) 
		return NULL;

	ring->item_size = item_size;
	ring->num_items = q_len;

	rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
				4096, 0,
				BUS_SPACE_MAXADDR,
				BUS_SPACE_MAXADDR,
				NULL, NULL,
				size, 8, 4096, 0, NULL, NULL, &ring->dma.tag);
	if (rc)
		goto fail;


	rc = bus_dmamem_alloc(ring->dma.tag,
				&ring->dma.ptr,
				BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
				&ring->dma.map);
	if (rc)
		goto fail;

	bzero(ring->dma.ptr, size);
	bus_dmamap_sync(ring->dma.tag, ring->dma.map,
			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
	ring->dma.paddr = 0;
	
	return ring;
	
fail:
	oce_dma_free(sc, &ring->dma);
	free(ring, M_DEVBUF);
	ring = NULL;
	return NULL;
}



struct _oce_dmamap_paddr_table {
	uint32_t max_entries;
	uint32_t num_entries;
	struct phys_addr *paddrs;
};



/**
 * @brief		Map ring buffer
 * @param arg		dma map phyical address table pointer
 * @param segs		dma memory segments
 * @param nseg		number of dma memory segments
 * @param error		maps only if error is 0
 */
static void
oce_dma_map_ring(void *arg, bus_dma_segment_t * segs, int nseg, int error)
{
	int i;
	struct _oce_dmamap_paddr_table *dpt =
	    (struct _oce_dmamap_paddr_table *)arg;

	if (error == 0) {
		if (nseg <= dpt->max_entries) {
			for (i = 0; i < nseg; i++) {
				dpt->paddrs[i].lo = ADDR_LO(segs[i].ds_addr);
				dpt->paddrs[i].hi = ADDR_HI(segs[i].ds_addr);
			}
			dpt->num_entries = nseg;
		}
	}
}



/**
 * @brief		Load bus dma map for a ring buffer
 * @param ring		ring buffer pointer
 * @param pa_list	physical address list
 * @returns		number entries
 */
uint32_t
oce_page_list(oce_ring_buffer_t *ring, struct phys_addr *pa_list)
{
	struct _oce_dmamap_paddr_table dpt;

	dpt.max_entries = 8;
	dpt.num_entries = 0;
	dpt.paddrs = pa_list;

	bus_dmamap_load(ring->dma.tag,
			ring->dma.map,
			ring->dma.ptr,
			ring->item_size * ring->num_items,
			oce_dma_map_ring, &dpt, BUS_DMA_NOWAIT);

	return dpt.num_entries;
}
OpenPOWER on IntegriCloud