2 * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "smartpqi_includes.h"
30 MALLOC_DEFINE(M_SMARTPQI, "smartpqi", "Buffers for the smartpqi driver");
33 * DMA map load callback function
36 os_dma_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
38 bus_addr_t *paddr = (bus_addr_t *)arg;
39 *paddr = segs[0].ds_addr;
43 os_dma_setup(pqisrc_softstate_t *softs)
47 return PQI_STATUS_SUCCESS;
51 os_dma_destroy(pqisrc_softstate_t *softs)
55 return PQI_STATUS_SUCCESS;
59 os_update_dma_attributes(pqisrc_softstate_t *softs)
66 * DMA mem resource allocation wrapper function
69 os_dma_mem_alloc(pqisrc_softstate_t *softs, struct dma_mem *dma_mem)
71 int ret = BSD_SUCCESS;
73 /* DBG_FUNC("IN\n"); */
75 /* Make sure the alignment is at least 4 bytes */
76 ASSERT(dma_mem->align >= 4);
78 /* DMA memory needed - allocate it */
79 if ((ret = bus_dma_tag_create(
80 softs->os_specific.pqi_parent_dmat, /* parent */
81 dma_mem->align, 0, /* algnmnt, boundary */
82 BUS_SPACE_MAXADDR, /* lowaddr */
83 BUS_SPACE_MAXADDR, /* highaddr */
84 NULL, NULL, /* filter, filterarg */
85 dma_mem->size, /* maxsize */
87 dma_mem->size, /* maxsegsize */
89 NULL, NULL, /* No locking needed */
90 &dma_mem->dma_tag)) != 0 ) {
91 DBG_ERR("can't allocate DMA tag with error = 0x%x\n", ret);
95 if (!dma_mem->dma_tag) {
96 DBG_ERR("dma tag is NULL\n");
101 if ((ret = bus_dmamem_alloc(dma_mem->dma_tag, (void **)&dma_mem->virt_addr,
102 BUS_DMA_WAITOK, &dma_mem->dma_map)) != 0) {
103 DBG_ERR("can't allocate DMA memory for required object \
104 with error = 0x%x\n", ret);
108 if((ret = bus_dmamap_load(dma_mem->dma_tag, dma_mem->dma_map,
109 dma_mem->virt_addr, dma_mem->size,
110 os_dma_map, &dma_mem->dma_addr, 0)) != 0) {
111 DBG_ERR("can't load DMA memory for required \
112 object with error = 0x%x\n", ret);
116 memset(dma_mem->virt_addr, 0, dma_mem->size);
118 ret = bsd_status_to_pqi_status(ret);
120 /* DBG_FUNC("OUT\n"); */
124 if(dma_mem->virt_addr)
125 bus_dmamem_free(dma_mem->dma_tag, dma_mem->virt_addr,
129 bus_dma_tag_destroy(dma_mem->dma_tag);
131 DBG_FUNC("failed OUT\n");
133 ret = bsd_status_to_pqi_status(ret);
139 * DMA mem resource deallocation wrapper function
142 os_dma_mem_free(pqisrc_softstate_t *softs, struct dma_mem *dma_mem)
144 /* DBG_FUNC("IN\n"); */
146 if(dma_mem->dma_addr) {
147 bus_dmamap_unload(dma_mem->dma_tag, dma_mem->dma_map);
148 dma_mem->dma_addr = 0;
151 if(dma_mem->virt_addr) {
152 bus_dmamem_free(dma_mem->dma_tag, dma_mem->virt_addr,
154 dma_mem->virt_addr = NULL;
157 if(dma_mem->dma_tag) {
158 bus_dma_tag_destroy(dma_mem->dma_tag);
159 dma_mem->dma_tag = NULL;
162 /* DBG_FUNC("OUT\n"); */
167 * Mem resource allocation wrapper function
170 *os_mem_alloc(pqisrc_softstate_t *softs, size_t size)
174 /* DBG_FUNC("IN\n"); */
176 addr = malloc((unsigned long)size, M_SMARTPQI,
179 /* DBG_FUNC("OUT\n"); */
185 * Mem resource deallocation wrapper function
188 os_mem_free(pqisrc_softstate_t *softs, char *addr, size_t size)
190 /* DBG_FUNC("IN\n"); */
192 free((void*)addr, M_SMARTPQI);
194 /* DBG_FUNC("OUT\n"); */
198 * dma/bus resource deallocation wrapper function
201 os_resource_free(pqisrc_softstate_t *softs)
203 if(softs->os_specific.pqi_parent_dmat)
204 bus_dma_tag_destroy(softs->os_specific.pqi_parent_dmat);
206 if (softs->os_specific.pqi_regs_res0 != NULL)
207 bus_release_resource(softs->os_specific.pqi_dev,
209 softs->os_specific.pqi_regs_rid0,
210 softs->os_specific.pqi_regs_res0);