2 * Copyright (c) 2018 Microsemi Corporation.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "smartpqi_includes.h"
31 MALLOC_DEFINE(M_SMARTPQI, "smartpqi", "Buffers for the smartpqi(4) driver");
34 * DMA map load callback function
37 os_dma_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
39 bus_addr_t *paddr = (bus_addr_t *)arg;
40 *paddr = segs[0].ds_addr;
44 * DMA mem resource allocation wrapper function
46 int os_dma_mem_alloc(pqisrc_softstate_t *softs, struct dma_mem *dma_mem)
50 /* DBG_FUNC("IN\n"); */
52 /* DMA memory needed - allocate it */
53 if ((ret = bus_dma_tag_create(
54 softs->os_specific.pqi_parent_dmat, /* parent */
55 dma_mem->align, 0, /* algnmnt, boundary */
56 BUS_SPACE_MAXADDR, /* lowaddr */
57 BUS_SPACE_MAXADDR, /* highaddr */
58 NULL, NULL, /* filter, filterarg */
59 dma_mem->size, /* maxsize */
61 dma_mem->size, /* maxsegsize */
63 NULL, NULL, /* No locking needed */
64 &dma_mem->dma_tag)) != 0 ) {
65 DBG_ERR("can't allocate DMA tag with error = 0x%x\n", ret);
68 if ((ret = bus_dmamem_alloc(dma_mem->dma_tag, (void **)&dma_mem->virt_addr,
69 BUS_DMA_NOWAIT, &dma_mem->dma_map)) != 0) {
70 DBG_ERR("can't allocate DMA memory for required object \
71 with error = 0x%x\n", ret);
75 if((ret = bus_dmamap_load(dma_mem->dma_tag, dma_mem->dma_map,
76 dma_mem->virt_addr, dma_mem->size,
77 os_dma_map, &dma_mem->dma_addr, 0)) != 0) {
78 DBG_ERR("can't load DMA memory for required \
79 object with error = 0x%x\n", ret);
83 memset(dma_mem->virt_addr, 0, dma_mem->size);
85 /* DBG_FUNC("OUT\n"); */
89 if(dma_mem->virt_addr)
90 bus_dmamem_free(dma_mem->dma_tag, dma_mem->virt_addr,
94 bus_dma_tag_destroy(dma_mem->dma_tag);
96 DBG_FUNC("failed OUT\n");
101 * DMA mem resource deallocation wrapper function
103 void os_dma_mem_free(pqisrc_softstate_t *softs, struct dma_mem *dma_mem)
105 /* DBG_FUNC("IN\n"); */
107 if(dma_mem->dma_addr) {
108 bus_dmamap_unload(dma_mem->dma_tag, dma_mem->dma_map);
109 dma_mem->dma_addr = 0;
112 if(dma_mem->virt_addr) {
113 bus_dmamem_free(dma_mem->dma_tag, dma_mem->virt_addr,
115 dma_mem->virt_addr = NULL;
118 if(dma_mem->dma_tag) {
119 bus_dma_tag_destroy(dma_mem->dma_tag);
120 dma_mem->dma_tag = NULL;
123 /* DBG_FUNC("OUT\n"); */
128 * Mem resource allocation wrapper function
130 void *os_mem_alloc(pqisrc_softstate_t *softs, size_t size)
134 /* DBG_FUNC("IN\n"); */
136 addr = malloc((unsigned long)size, M_SMARTPQI,
139 /* DBG_FUNC("OUT\n"); */
145 * Mem resource deallocation wrapper function
147 void os_mem_free(pqisrc_softstate_t *softs,
148 char *addr, size_t size)
150 /* DBG_FUNC("IN\n"); */
152 free((void*)addr, M_SMARTPQI);
154 /* DBG_FUNC("OUT\n"); */
158 * dma/bus resource deallocation wrapper function
160 void os_resource_free(pqisrc_softstate_t *softs)
162 if(softs->os_specific.pqi_parent_dmat)
163 bus_dma_tag_destroy(softs->os_specific.pqi_parent_dmat);
165 if (softs->os_specific.pqi_regs_res0 != NULL)
166 bus_release_resource(softs->os_specific.pqi_dev,
168 softs->os_specific.pqi_regs_rid0,
169 softs->os_specific.pqi_regs_res0);