1 /*- 2 * Copyright (c) 1998 - 2003 S�ren Schmidt <sos@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/ata.h> 34 #include <sys/kernel.h> 35 #include <sys/endian.h> 36 #include <sys/malloc.h> 37 #include <sys/bus.h> 38 #include <pci/pcivar.h> 39 #include <machine/bus.h> 40 #include <sys/rman.h> 41 #include <dev/ata/ata-all.h> 42 #include <dev/ata/ata-pci.h> 43 44 /* prototypes */ 45 static void ata_dmasetupc_cb(void *, bus_dma_segment_t *, int, int); 46 static int ata_dmaalloc(struct ata_channel *); 47 static void ata_dmafree(struct ata_channel *); 48 static void ata_dmasetupd_cb(void *, bus_dma_segment_t *, int, int); 49 static int ata_dmasetup(struct ata_device *, caddr_t, int32_t); 50 51 /* local vars */ 52 static MALLOC_DEFINE(M_ATADMA, "ATA DMA", "ATA driver DMA"); 53 54 /* misc defines */ 55 #define MAXSEGSZ PAGE_SIZE 56 #define MAXTABSZ PAGE_SIZE 57 #define MAXCTLDMASZ (2 * (MAXTABSZ + MAXPHYS)) 58 59 struct ata_dc_cb_args { 60 bus_addr_t maddr; 61 int error; 62 }; 63 64 int 65 ata_dmainit(struct ata_channel *ch) 66 { 67 if (!(ch->dma = 68 malloc(sizeof(struct ata_dma_data), M_ATADMA, M_NOWAIT | M_ZERO))) 69 return ENOMEM; 70 ch->dma->alloc = ata_dmaalloc; 71 ch->dma->free = ata_dmafree; 72 ch->dma->setup = ata_dmasetup; 73 ch->dma->start = ata_dmastart; 74 ch->dma->stop = ata_dmastop; 75 ch->dma->alignment = 2; 76 return 0; 77 } 78 79 80 static void 81 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 82 { 83 struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc; 84 85 if (!(cba->error = error)) 86 cba->maddr = segs[0].ds_addr; 87 } 88 89 static int 90 ata_dmaalloc(struct ata_channel *ch) 91 { 92 struct ata_dc_cb_args ccba; 93 int error; 94 95 if (!ch->dma->dmatag) { 96 if (bus_dma_tag_create(NULL, 1, 0, 97 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, 98 NULL, NULL, MAXCTLDMASZ, ATA_DMA_ENTRIES, 99 BUS_SPACE_MAXSIZE_32BIT, 0, &ch->dma->dmatag)) { 100 printf("DMA tag allocation failed, disabling DMA\n"); 101 } 102 } 103 if (!ch->dma->cdmatag) { 104 if ((error = bus_dma_tag_create(ch->dma->dmatag, 1, PAGE_SIZE, 105 BUS_SPACE_MAXADDR_32BIT, 106 BUS_SPACE_MAXADDR, NULL, NULL, 107 MAXTABSZ, 1, MAXTABSZ, 108 BUS_DMA_ALLOCNOW, &ch->dma->cdmatag))) 109 return error; 110 } 111 if (!ch->dma->ddmatag) { 112 if ((error = bus_dma_tag_create(ch->dma->dmatag, ch->dma->alignment, 0, 113 BUS_SPACE_MAXADDR_32BIT, 114 BUS_SPACE_MAXADDR, NULL, NULL, 115 MAXPHYS, ATA_DMA_ENTRIES, MAXSEGSZ, 116 BUS_DMA_ALLOCNOW, &ch->dma->ddmatag))) 117 return error; 118 } 119 if (!ch->dma->mdmatab) { 120 if ((error = bus_dmamem_alloc(ch->dma->cdmatag, 121 (void **)&ch->dma->dmatab, 0, 122 &ch->dma->cdmamap))) 123 return error; 124 125 if ((error = bus_dmamap_load(ch->dma->cdmatag, ch->dma->cdmamap, 126 ch->dma->dmatab, MAXTABSZ, 127 ata_dmasetupc_cb, &ccba, 0)) != 0 || 128 ccba.error != 0) { 129 bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab,ch->dma->cdmamap); 130 return error; 131 } 132 ch->dma->mdmatab = ccba.maddr; 133 } 134 if (!ch->dma->ddmamap) { 135 if ((error = bus_dmamap_create(ch->dma->ddmatag, 0, 136 &ch->dma->ddmamap)) != 0) 137 return error; 138 } 139 return 0; 140 } 141 142 static void 143 ata_dmafree(struct ata_channel *ch) 144 { 145 if (ch->dma->mdmatab) { 146 bus_dmamap_unload(ch->dma->cdmatag, ch->dma->cdmamap); 147 bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap); 148 ch->dma->mdmatab = 0; 149 ch->dma->cdmamap = NULL; 150 ch->dma->dmatab = NULL; 151 } 152 if (ch->dma->ddmamap) { 153 bus_dmamap_destroy(ch->dma->ddmatag, ch->dma->ddmamap); 154 ch->dma->ddmamap = NULL; 155 } 156 if (ch->dma->cdmatag) { 157 bus_dma_tag_destroy(ch->dma->cdmatag); 158 ch->dma->cdmatag = NULL; 159 } 160 if (ch->dma->ddmatag) { 161 bus_dma_tag_destroy(ch->dma->ddmatag); 162 ch->dma->ddmatag = NULL; 163 } 164 if (ch->dma->dmatag) { 165 bus_dma_tag_destroy(ch->dma->dmatag); 166 ch->dma->dmatag = NULL; 167 } 168 } 169 170 struct ata_dmasetup_data_cb_args { 171 struct ata_dmaentry *dmatab; 172 int error; 173 }; 174 175 static void 176 ata_dmasetupd_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 177 { 178 struct ata_dmasetup_data_cb_args *cba = 179 (struct ata_dmasetup_data_cb_args *)xsc; 180 bus_size_t cnt; 181 u_int32_t lastcount; 182 int i, j; 183 184 cba->error = error; 185 if (error != 0) 186 return; 187 lastcount = j = 0; 188 for (i = 0; i < nsegs; i++) { 189 /* 190 * A maximum segment size was specified for bus_dma_tag_create, but 191 * some busdma code does not seem to honor this, so fix up if needed. 192 */ 193 for (cnt = 0; cnt < segs[i].ds_len; cnt += MAXSEGSZ, j++) { 194 cba->dmatab[j].base = htole32(segs[i].ds_addr + cnt); 195 lastcount = ulmin(segs[i].ds_len - cnt, MAXSEGSZ) & 0xffff; 196 cba->dmatab[j].count = htole32(lastcount); 197 } 198 } 199 cba->dmatab[j - 1].count = htole32(lastcount | ATA_DMA_EOT); 200 } 201 202 static int 203 ata_dmasetup(struct ata_device *atadev, caddr_t data, int32_t count) 204 { 205 struct ata_channel *ch = atadev->channel; 206 207 if (((uintptr_t)data & (ch->dma->alignment - 1)) || 208 (count & (ch->dma->alignment - 1))) { 209 ata_prtdev(atadev, "non aligned DMA transfer attempted\n"); 210 return -1; 211 } 212 213 if (!count) { 214 ata_prtdev(atadev, "zero length DMA transfer attempted\n"); 215 return -1; 216 } 217 return 0; 218 } 219 220 int 221 ata_dmastart(struct ata_channel *ch, caddr_t data, int32_t count, int dir) 222 { 223 struct ata_dmasetup_data_cb_args cba; 224 225 if (ch->dma->flags & ATA_DMA_ACTIVE) 226 panic("ata_dmasetup: transfer active on this device!"); 227 228 cba.dmatab = ch->dma->dmatab; 229 bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_PREWRITE); 230 231 if (bus_dmamap_load(ch->dma->ddmatag, ch->dma->ddmamap, data, count, 232 ata_dmasetupd_cb, &cba, 0) || cba.error) 233 return -1; 234 235 bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_POSTWRITE); 236 bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap, 237 dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 238 239 ch->dma->flags = dir ? (ATA_DMA_ACTIVE | ATA_DMA_READ) : ATA_DMA_ACTIVE; 240 return 0; 241 } 242 243 int 244 ata_dmastop(struct ata_channel *ch) 245 { 246 bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap, 247 (ch->dma->flags & ATA_DMA_READ) != 0 ? 248 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 249 bus_dmamap_unload(ch->dma->ddmatag, ch->dma->ddmamap); 250 ch->dma->flags = 0; 251 return 0; 252 } 253