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/param.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/queue.h> 41 #include <sys/taskqueue.h> 42 43 #include <machine/bus.h> 44 45 #include "common/efx.h" 46 47 #include "sfxge.h" 48 49 static void 50 sfxge_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 51 { 52 bus_addr_t *addr; 53 54 addr = arg; 55 56 if (error != 0) { 57 *addr = 0; 58 return; 59 } 60 61 *addr = segs[0].ds_addr; 62 } 63 64 int 65 sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map, 66 struct mbuf **mp, bus_dma_segment_t *segs, 67 int *nsegs, int maxsegs) 68 { 69 bus_dma_segment_t *psegs; 70 struct mbuf *m; 71 int seg_count; 72 int defragged; 73 int err; 74 75 m = *mp; 76 defragged = err = seg_count = 0; 77 78 KASSERT(m->m_pkthdr.len, ("packet has zero header length")); 79 80 retry: 81 psegs = segs; 82 seg_count = 0; 83 if (m->m_next == NULL) { 84 sfxge_map_mbuf_fast(tag, map, m, segs); 85 *nsegs = 1; 86 return (0); 87 } 88 #if defined(__i386__) || defined(__amd64__) 89 while (m != NULL && seg_count < maxsegs) { 90 /* 91 * firmware doesn't like empty segments 92 */ 93 if (m->m_len != 0) { 94 seg_count++; 95 sfxge_map_mbuf_fast(tag, map, m, psegs); 96 psegs++; 97 } 98 m = m->m_next; 99 } 100 #else 101 err = bus_dmamap_load_mbuf_sg(tag, map, *mp, segs, &seg_count, 0); 102 #endif 103 if (seg_count == 0) { 104 err = EFBIG; 105 goto err_out; 106 } else if (err == EFBIG || seg_count >= maxsegs) { 107 if (!defragged) { 108 m = m_defrag(*mp, M_NOWAIT); 109 if (m == NULL) { 110 err = ENOBUFS; 111 goto err_out; 112 } 113 *mp = m; 114 defragged = 1; 115 goto retry; 116 } 117 err = EFBIG; 118 goto err_out; 119 } 120 *nsegs = seg_count; 121 122 err_out: 123 return (err); 124 } 125 126 void 127 sfxge_dma_free(efsys_mem_t *esmp) 128 { 129 130 bus_dmamap_unload(esmp->esm_tag, esmp->esm_map); 131 bus_dmamem_free(esmp->esm_tag, esmp->esm_base, esmp->esm_map); 132 bus_dma_tag_destroy(esmp->esm_tag); 133 134 esmp->esm_addr = 0; 135 esmp->esm_base = NULL; 136 esmp->esm_size = 0; 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 esmp->esm_size = len; 177 178 return (0); 179 180 fail_load_check: 181 fail_load: 182 bus_dmamem_free(esmp->esm_tag, vaddr, esmp->esm_map); 183 fail_alloc: 184 bus_dma_tag_destroy(esmp->esm_tag); 185 fail_tag_create: 186 return (ENOMEM); 187 } 188 189 void 190 sfxge_dma_fini(struct sfxge_softc *sc) 191 { 192 193 bus_dma_tag_destroy(sc->parent_dma_tag); 194 } 195 196 int 197 sfxge_dma_init(struct sfxge_softc *sc) 198 { 199 200 /* Create the parent dma tag. */ 201 if (bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent */ 202 1, 0, /* algnmnt, boundary */ 203 BUS_SPACE_MAXADDR, /* lowaddr */ 204 BUS_SPACE_MAXADDR, /* highaddr */ 205 NULL, NULL, /* filter, filterarg */ 206 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 207 BUS_SPACE_UNRESTRICTED, /* nsegments */ 208 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 209 0, /* flags */ 210 NULL, NULL, /* lock, lockarg */ 211 &sc->parent_dma_tag) != 0) { 212 device_printf(sc->dev, "Cannot allocate parent DMA tag\n"); 213 return (ENOMEM); 214 } 215 216 return (0); 217 } 218