xref: /freebsd/sys/dev/sfxge/sfxge_dma.c (revision 5b9c547c)
1 /*-
2  * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/queue.h>
38 #include <sys/taskqueue.h>
39 
40 #include <machine/bus.h>
41 
42 #include "common/efx.h"
43 
44 #include "sfxge.h"
45 
46 static void
47 sfxge_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
48 {
49 	bus_addr_t *addr;
50 
51 	addr = arg;
52 
53 	if (error != 0) {
54 		*addr = 0;
55 		return;
56 	}
57 
58 	*addr = segs[0].ds_addr;
59 }
60 
61 int
62 sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
63     struct mbuf **mp, bus_dma_segment_t *segs, int *nsegs, int maxsegs)
64 {
65 	bus_dma_segment_t *psegs;
66 	struct mbuf *m;
67 	int seg_count;
68 	int defragged;
69 	int err;
70 
71 	m = *mp;
72 	defragged = err = seg_count = 0;
73 
74 	KASSERT(m->m_pkthdr.len, ("packet has zero header length"));
75 
76 retry:
77 	psegs = segs;
78 	seg_count = 0;
79 	if (m->m_next == NULL) {
80 		sfxge_map_mbuf_fast(tag, map, m, segs);
81 		*nsegs = 1;
82 		return (0);
83 	}
84 #if defined(__i386__) || defined(__amd64__)
85 	while (m != NULL && seg_count < maxsegs) {
86 		/*
87 		 * firmware doesn't like empty segments
88 		 */
89 		if (m->m_len != 0) {
90 			seg_count++;
91 			sfxge_map_mbuf_fast(tag, map, m, psegs);
92 			psegs++;
93 		}
94 		m = m->m_next;
95 	}
96 #else
97 	err = bus_dmamap_load_mbuf_sg(tag, map, *mp, segs, &seg_count, 0);
98 #endif
99 	if (seg_count == 0) {
100 		err = EFBIG;
101 		goto err_out;
102 	} else if (err == EFBIG || seg_count >= maxsegs) {
103 		if (!defragged) {
104 			m = m_defrag(*mp, M_NOWAIT);
105 			if (m == NULL) {
106 				err = ENOBUFS;
107 				goto err_out;
108 			}
109 			*mp = m;
110 			defragged = 1;
111 			goto retry;
112 		}
113 		err = EFBIG;
114 		goto err_out;
115 	}
116 	*nsegs = seg_count;
117 
118 err_out:
119 	return (err);
120 }
121 
122 void
123 sfxge_dma_free(efsys_mem_t *esmp)
124 {
125 
126 	bus_dmamap_unload(esmp->esm_tag, esmp->esm_map);
127 	bus_dmamem_free(esmp->esm_tag, esmp->esm_base, esmp->esm_map);
128 	bus_dma_tag_destroy(esmp->esm_tag);
129 
130 	esmp->esm_addr = 0;
131 	esmp->esm_base = NULL;
132 }
133 
134 int
135 sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len, efsys_mem_t *esmp)
136 {
137 	void *vaddr;
138 
139 	/* Create the child DMA tag. */
140 	if (bus_dma_tag_create(sc->parent_dma_tag, PAGE_SIZE, 0,
141 	    MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
142 	    NULL, len, 1, len, 0, NULL, NULL, &esmp->esm_tag) != 0) {
143 		device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
144 		goto fail_tag_create;
145 	}
146 
147 	/* Allocate kernel memory. */
148 	if (bus_dmamem_alloc(esmp->esm_tag, (void **)&vaddr,
149 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
150 	    &esmp->esm_map) != 0) {
151 		device_printf(sc->dev, "Couldn't allocate DMA memory\n");
152 		goto fail_alloc;
153 	}
154 
155 	/* Load map into device memory. */
156 	if (bus_dmamap_load(esmp->esm_tag, esmp->esm_map, vaddr, len,
157 	    sfxge_dma_cb, &esmp->esm_addr, 0) != 0) {
158 		device_printf(sc->dev, "Couldn't load DMA mapping\n");
159 		goto fail_load;
160 	}
161 
162 	/*
163 	 * The callback gets error information about the mapping
164 	 * and will have set esm_addr to 0 if something went
165 	 * wrong.
166 	 */
167 	if (esmp->esm_addr == 0)
168 		goto fail_load_check;
169 
170 	esmp->esm_base = vaddr;
171 
172 	return (0);
173 
174 fail_load_check:
175 fail_load:
176 	bus_dmamem_free(esmp->esm_tag, vaddr, esmp->esm_map);
177 fail_alloc:
178 	bus_dma_tag_destroy(esmp->esm_tag);
179 fail_tag_create:
180 	return (ENOMEM);
181 }
182 
183 void
184 sfxge_dma_fini(struct sfxge_softc *sc)
185 {
186 
187 	bus_dma_tag_destroy(sc->parent_dma_tag);
188 }
189 
190 int
191 sfxge_dma_init(struct sfxge_softc *sc)
192 {
193 
194 	/* Create the parent dma tag. */
195 	if (bus_dma_tag_create(bus_get_dma_tag(sc->dev),	/* parent */
196 	    1, 0,			/* algnmnt, boundary */
197 	    BUS_SPACE_MAXADDR,		/* lowaddr */
198 	    BUS_SPACE_MAXADDR,		/* highaddr */
199 	    NULL, NULL,			/* filter, filterarg */
200 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
201 	    BUS_SPACE_UNRESTRICTED,	/* nsegments */
202 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
203 	    0,				/* flags */
204 	    NULL, NULL,			/* lock, lockarg */
205 	    &sc->parent_dma_tag) != 0) {
206 		device_printf(sc->dev, "Cannot allocate parent DMA tag\n");
207 		return (ENOMEM);
208 	}
209 
210 	return (0);
211 }
212