xref: /freebsd/sys/dev/ixl/i40e_osdep.c (revision 61ae650d55553d48c55fbe023706dfa4b97483bb)
1*61ae650dSJack F Vogel /******************************************************************************
2*61ae650dSJack F Vogel 
3*61ae650dSJack F Vogel   Copyright (c) 2013-2014, Intel Corporation
4*61ae650dSJack F Vogel   All rights reserved.
5*61ae650dSJack F Vogel 
6*61ae650dSJack F Vogel   Redistribution and use in source and binary forms, with or without
7*61ae650dSJack F Vogel   modification, are permitted provided that the following conditions are met:
8*61ae650dSJack F Vogel 
9*61ae650dSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
10*61ae650dSJack F Vogel       this list of conditions and the following disclaimer.
11*61ae650dSJack F Vogel 
12*61ae650dSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
13*61ae650dSJack F Vogel       notice, this list of conditions and the following disclaimer in the
14*61ae650dSJack F Vogel       documentation and/or other materials provided with the distribution.
15*61ae650dSJack F Vogel 
16*61ae650dSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
17*61ae650dSJack F Vogel       contributors may be used to endorse or promote products derived from
18*61ae650dSJack F Vogel       this software without specific prior written permission.
19*61ae650dSJack F Vogel 
20*61ae650dSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21*61ae650dSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*61ae650dSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*61ae650dSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24*61ae650dSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*61ae650dSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*61ae650dSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*61ae650dSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*61ae650dSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*61ae650dSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*61ae650dSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
31*61ae650dSJack F Vogel 
32*61ae650dSJack F Vogel ******************************************************************************/
33*61ae650dSJack F Vogel /*$FreeBSD$*/
34*61ae650dSJack F Vogel 
35*61ae650dSJack F Vogel #include <machine/stdarg.h>
36*61ae650dSJack F Vogel 
37*61ae650dSJack F Vogel #include "ixl.h"
38*61ae650dSJack F Vogel 
39*61ae650dSJack F Vogel /********************************************************************
40*61ae650dSJack F Vogel  * Manage DMA'able memory.
41*61ae650dSJack F Vogel  *******************************************************************/
42*61ae650dSJack F Vogel static void
43*61ae650dSJack F Vogel i40e_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error)
44*61ae650dSJack F Vogel {
45*61ae650dSJack F Vogel         if (error)
46*61ae650dSJack F Vogel                 return;
47*61ae650dSJack F Vogel         *(bus_addr_t *) arg = segs->ds_addr;
48*61ae650dSJack F Vogel         return;
49*61ae650dSJack F Vogel }
50*61ae650dSJack F Vogel 
51*61ae650dSJack F Vogel i40e_status
52*61ae650dSJack F Vogel i40e_allocate_virt(struct i40e_hw *hw, struct i40e_virt_mem *m, u32 size)
53*61ae650dSJack F Vogel {
54*61ae650dSJack F Vogel 	m->va = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
55*61ae650dSJack F Vogel 	return(m->va == NULL);
56*61ae650dSJack F Vogel }
57*61ae650dSJack F Vogel 
58*61ae650dSJack F Vogel i40e_status
59*61ae650dSJack F Vogel i40e_free_virt(struct i40e_hw *hw, struct i40e_virt_mem *m)
60*61ae650dSJack F Vogel {
61*61ae650dSJack F Vogel 	free(m->va, M_DEVBUF);
62*61ae650dSJack F Vogel 	return(0);
63*61ae650dSJack F Vogel }
64*61ae650dSJack F Vogel 
65*61ae650dSJack F Vogel i40e_status
66*61ae650dSJack F Vogel i40e_allocate_dma(struct i40e_hw *hw, struct i40e_dma_mem *dma,
67*61ae650dSJack F Vogel 	bus_size_t size, u32 alignment)
68*61ae650dSJack F Vogel {
69*61ae650dSJack F Vogel 	device_t	dev = ((struct i40e_osdep *)hw->back)->dev;
70*61ae650dSJack F Vogel 	int		err;
71*61ae650dSJack F Vogel 
72*61ae650dSJack F Vogel 
73*61ae650dSJack F Vogel 	err = bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
74*61ae650dSJack F Vogel 			       alignment, 0,	/* alignment, bounds */
75*61ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* lowaddr */
76*61ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* highaddr */
77*61ae650dSJack F Vogel 			       NULL, NULL,	/* filter, filterarg */
78*61ae650dSJack F Vogel 			       size,	/* maxsize */
79*61ae650dSJack F Vogel 			       1,	/* nsegments */
80*61ae650dSJack F Vogel 			       size,	/* maxsegsize */
81*61ae650dSJack F Vogel 			       BUS_DMA_ALLOCNOW, /* flags */
82*61ae650dSJack F Vogel 			       NULL,	/* lockfunc */
83*61ae650dSJack F Vogel 			       NULL,	/* lockfuncarg */
84*61ae650dSJack F Vogel 			       &dma->tag);
85*61ae650dSJack F Vogel 	if (err != 0) {
86*61ae650dSJack F Vogel 		device_printf(dev,
87*61ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dma_tag_create failed, "
88*61ae650dSJack F Vogel 		    "error %u\n", err);
89*61ae650dSJack F Vogel 		goto fail_0;
90*61ae650dSJack F Vogel 	}
91*61ae650dSJack F Vogel 	err = bus_dmamem_alloc(dma->tag, (void **)&dma->va,
92*61ae650dSJack F Vogel 			     BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dma->map);
93*61ae650dSJack F Vogel 	if (err != 0) {
94*61ae650dSJack F Vogel 		device_printf(dev,
95*61ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamem_alloc failed, "
96*61ae650dSJack F Vogel 		    "error %u\n", err);
97*61ae650dSJack F Vogel 		goto fail_1;
98*61ae650dSJack F Vogel 	}
99*61ae650dSJack F Vogel 	err = bus_dmamap_load(dma->tag, dma->map, dma->va,
100*61ae650dSJack F Vogel 			    size,
101*61ae650dSJack F Vogel 			    i40e_dmamap_cb,
102*61ae650dSJack F Vogel 			    &dma->pa,
103*61ae650dSJack F Vogel 			    BUS_DMA_NOWAIT);
104*61ae650dSJack F Vogel 	if (err != 0) {
105*61ae650dSJack F Vogel 		device_printf(dev,
106*61ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamap_load failed, "
107*61ae650dSJack F Vogel 		    "error %u\n", err);
108*61ae650dSJack F Vogel 		goto fail_2;
109*61ae650dSJack F Vogel 	}
110*61ae650dSJack F Vogel 	dma->size = size;
111*61ae650dSJack F Vogel 	bus_dmamap_sync(dma->tag, dma->map,
112*61ae650dSJack F Vogel 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
113*61ae650dSJack F Vogel 	return (0);
114*61ae650dSJack F Vogel fail_2:
115*61ae650dSJack F Vogel 	bus_dmamem_free(dma->tag, dma->va, dma->map);
116*61ae650dSJack F Vogel fail_1:
117*61ae650dSJack F Vogel 	bus_dma_tag_destroy(dma->tag);
118*61ae650dSJack F Vogel fail_0:
119*61ae650dSJack F Vogel 	dma->map = NULL;
120*61ae650dSJack F Vogel 	dma->tag = NULL;
121*61ae650dSJack F Vogel 	return (err);
122*61ae650dSJack F Vogel }
123*61ae650dSJack F Vogel 
124*61ae650dSJack F Vogel i40e_status
125*61ae650dSJack F Vogel i40e_free_dma(struct i40e_hw *hw, struct i40e_dma_mem *dma)
126*61ae650dSJack F Vogel {
127*61ae650dSJack F Vogel 	bus_dmamap_sync(dma->tag, dma->map,
128*61ae650dSJack F Vogel 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
129*61ae650dSJack F Vogel 	bus_dmamap_unload(dma->tag, dma->map);
130*61ae650dSJack F Vogel 	bus_dmamem_free(dma->tag, dma->va, dma->map);
131*61ae650dSJack F Vogel 	bus_dma_tag_destroy(dma->tag);
132*61ae650dSJack F Vogel 	return (0);
133*61ae650dSJack F Vogel }
134*61ae650dSJack F Vogel 
135*61ae650dSJack F Vogel void
136*61ae650dSJack F Vogel i40e_init_spinlock(struct i40e_spinlock *lock)
137*61ae650dSJack F Vogel {
138*61ae650dSJack F Vogel 	mtx_init(&lock->mutex, "mutex",
139*61ae650dSJack F Vogel 	    MTX_NETWORK_LOCK, MTX_DEF | MTX_DUPOK);
140*61ae650dSJack F Vogel }
141*61ae650dSJack F Vogel 
142*61ae650dSJack F Vogel void
143*61ae650dSJack F Vogel i40e_acquire_spinlock(struct i40e_spinlock *lock)
144*61ae650dSJack F Vogel {
145*61ae650dSJack F Vogel 	mtx_lock(&lock->mutex);
146*61ae650dSJack F Vogel }
147*61ae650dSJack F Vogel 
148*61ae650dSJack F Vogel void
149*61ae650dSJack F Vogel i40e_release_spinlock(struct i40e_spinlock *lock)
150*61ae650dSJack F Vogel {
151*61ae650dSJack F Vogel 	mtx_unlock(&lock->mutex);
152*61ae650dSJack F Vogel }
153*61ae650dSJack F Vogel 
154*61ae650dSJack F Vogel void
155*61ae650dSJack F Vogel i40e_destroy_spinlock(struct i40e_spinlock *lock)
156*61ae650dSJack F Vogel {
157*61ae650dSJack F Vogel 	mtx_destroy(&lock->mutex);
158*61ae650dSJack F Vogel }
159*61ae650dSJack F Vogel 
160*61ae650dSJack F Vogel /*
161*61ae650dSJack F Vogel ** i40e_debug_d - OS dependent version of shared code debug printing
162*61ae650dSJack F Vogel */
163*61ae650dSJack F Vogel void i40e_debug_d(void *hw, u32 mask, char *fmt, ...)
164*61ae650dSJack F Vogel {
165*61ae650dSJack F Vogel         char buf[512];
166*61ae650dSJack F Vogel         va_list args;
167*61ae650dSJack F Vogel 
168*61ae650dSJack F Vogel         if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
169*61ae650dSJack F Vogel                 return;
170*61ae650dSJack F Vogel 
171*61ae650dSJack F Vogel 	va_start(args, fmt);
172*61ae650dSJack F Vogel         vsnprintf(buf, sizeof(buf), fmt, args);
173*61ae650dSJack F Vogel 	va_end(args);
174*61ae650dSJack F Vogel 
175*61ae650dSJack F Vogel         /* the debug string is already formatted with a newline */
176*61ae650dSJack F Vogel         printf("%s", buf);
177*61ae650dSJack F Vogel }
178*61ae650dSJack F Vogel 
179*61ae650dSJack F Vogel u16
180*61ae650dSJack F Vogel i40e_read_pci_cfg(struct i40e_hw *hw, u32 reg)
181*61ae650dSJack F Vogel {
182*61ae650dSJack F Vogel         u16 value;
183*61ae650dSJack F Vogel 
184*61ae650dSJack F Vogel         value = pci_read_config(((struct i40e_osdep *)hw->back)->dev,
185*61ae650dSJack F Vogel             reg, 2);
186*61ae650dSJack F Vogel 
187*61ae650dSJack F Vogel         return (value);
188*61ae650dSJack F Vogel }
189*61ae650dSJack F Vogel 
190*61ae650dSJack F Vogel void
191*61ae650dSJack F Vogel i40e_write_pci_cfg(struct i40e_hw *hw, u32 reg, u16 value)
192*61ae650dSJack F Vogel {
193*61ae650dSJack F Vogel         pci_write_config(((struct i40e_osdep *)hw->back)->dev,
194*61ae650dSJack F Vogel             reg, value, 2);
195*61ae650dSJack F Vogel 
196*61ae650dSJack F Vogel         return;
197*61ae650dSJack F Vogel }
198*61ae650dSJack F Vogel 
199