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