]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/sfxge/sfxge_dma.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / dev / sfxge / sfxge_dma.c
1 /*-
2  * Copyright (c) 2010-2015 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
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,
64                           int *nsegs, int maxsegs)
65 {
66         bus_dma_segment_t *psegs;
67         struct mbuf *m;
68         int seg_count;
69         int defragged;
70         int err;
71
72         m = *mp;
73         defragged = err = seg_count = 0;
74
75         KASSERT(m->m_pkthdr.len, ("packet has zero header length"));
76
77 retry:
78         psegs = segs;
79         seg_count = 0;
80         if (m->m_next == NULL) {
81                 sfxge_map_mbuf_fast(tag, map, m, segs);
82                 *nsegs = 1;
83                 return (0);
84         }
85 #if defined(__i386__) || defined(__amd64__)
86         while (m != NULL && seg_count < maxsegs) {
87                 /*
88                  * firmware doesn't like empty segments
89                  */
90                 if (m->m_len != 0) {
91                         seg_count++;
92                         sfxge_map_mbuf_fast(tag, map, m, psegs);
93                         psegs++;
94                 }
95                 m = m->m_next;
96         }
97 #else
98         err = bus_dmamap_load_mbuf_sg(tag, map, *mp, segs, &seg_count, 0);
99 #endif
100         if (seg_count == 0) {
101                 err = EFBIG;
102                 goto err_out;
103         } else if (err == EFBIG || seg_count >= maxsegs) {
104                 if (!defragged) {
105                         m = m_defrag(*mp, M_NOWAIT);
106                         if (m == NULL) {
107                                 err = ENOBUFS;
108                                 goto err_out;
109                         }
110                         *mp = m;
111                         defragged = 1;
112                         goto retry;
113                 }
114                 err = EFBIG;
115                 goto err_out;
116         }
117         *nsegs = seg_count;
118
119 err_out:
120         return (err);
121 }
122
123 void
124 sfxge_dma_free(efsys_mem_t *esmp)
125 {
126
127         bus_dmamap_unload(esmp->esm_tag, esmp->esm_map);
128         bus_dmamem_free(esmp->esm_tag, esmp->esm_base, esmp->esm_map);
129         bus_dma_tag_destroy(esmp->esm_tag);
130
131         esmp->esm_addr = 0;
132         esmp->esm_base = NULL;
133 }
134
135 int
136 sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len, efsys_mem_t *esmp)
137 {
138         void *vaddr;
139
140         /* Create the child DMA tag. */
141         if (bus_dma_tag_create(sc->parent_dma_tag, PAGE_SIZE, 0,
142             MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
143             NULL, len, 1, len, 0, NULL, NULL, &esmp->esm_tag) != 0) {
144                 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
145                 goto fail_tag_create;
146         }
147
148         /* Allocate kernel memory. */
149         if (bus_dmamem_alloc(esmp->esm_tag, (void **)&vaddr,
150             BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
151             &esmp->esm_map) != 0) {
152                 device_printf(sc->dev, "Couldn't allocate DMA memory\n");
153                 goto fail_alloc;
154         }
155
156         /* Load map into device memory. */
157         if (bus_dmamap_load(esmp->esm_tag, esmp->esm_map, vaddr, len,
158             sfxge_dma_cb, &esmp->esm_addr, 0) != 0) {
159                 device_printf(sc->dev, "Couldn't load DMA mapping\n");
160                 goto fail_load;
161         }
162
163         /*
164          * The callback gets error information about the mapping
165          * and will have set esm_addr to 0 if something went
166          * wrong.
167          */
168         if (esmp->esm_addr == 0)
169                 goto fail_load_check;
170
171         esmp->esm_base = vaddr;
172
173         return (0);
174
175 fail_load_check:
176 fail_load:
177         bus_dmamem_free(esmp->esm_tag, vaddr, esmp->esm_map);
178 fail_alloc:
179         bus_dma_tag_destroy(esmp->esm_tag);
180 fail_tag_create:
181         return (ENOMEM);
182 }
183
184 void
185 sfxge_dma_fini(struct sfxge_softc *sc)
186 {
187
188         bus_dma_tag_destroy(sc->parent_dma_tag);
189 }
190
191 int
192 sfxge_dma_init(struct sfxge_softc *sc)
193 {
194
195         /* Create the parent dma tag. */
196         if (bus_dma_tag_create(bus_get_dma_tag(sc->dev),        /* parent */
197             1, 0,                       /* algnmnt, boundary */
198             BUS_SPACE_MAXADDR,          /* lowaddr */
199             BUS_SPACE_MAXADDR,          /* highaddr */
200             NULL, NULL,                 /* filter, filterarg */
201             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
202             BUS_SPACE_UNRESTRICTED,     /* nsegments */
203             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
204             0,                          /* flags */
205             NULL, NULL,                 /* lock, lockarg */
206             &sc->parent_dma_tag) != 0) {
207                 device_printf(sc->dev, "Cannot allocate parent DMA tag\n");
208                 return (ENOMEM);
209         }
210
211         return (0);
212 }