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