xref: /freebsd/sys/dev/ixl/i40e_osdep.c (revision ea022094f2bafa4ae204c6cc0edc77b981870919)
161ae650dSJack F Vogel /******************************************************************************
261ae650dSJack F Vogel 
361ae650dSJack F Vogel   Copyright (c) 2013-2014, Intel Corporation
461ae650dSJack F Vogel   All rights reserved.
561ae650dSJack F Vogel 
661ae650dSJack F Vogel   Redistribution and use in source and binary forms, with or without
761ae650dSJack F Vogel   modification, are permitted provided that the following conditions are met:
861ae650dSJack F Vogel 
961ae650dSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
1061ae650dSJack F Vogel       this list of conditions and the following disclaimer.
1161ae650dSJack F Vogel 
1261ae650dSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
1361ae650dSJack F Vogel       notice, this list of conditions and the following disclaimer in the
1461ae650dSJack F Vogel       documentation and/or other materials provided with the distribution.
1561ae650dSJack F Vogel 
1661ae650dSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
1761ae650dSJack F Vogel       contributors may be used to endorse or promote products derived from
1861ae650dSJack F Vogel       this software without specific prior written permission.
1961ae650dSJack F Vogel 
2061ae650dSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2161ae650dSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261ae650dSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361ae650dSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2461ae650dSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2561ae650dSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2661ae650dSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2761ae650dSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2861ae650dSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2961ae650dSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3061ae650dSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
3161ae650dSJack F Vogel 
3261ae650dSJack F Vogel ******************************************************************************/
3361ae650dSJack F Vogel /*$FreeBSD$*/
3461ae650dSJack F Vogel 
3561ae650dSJack F Vogel #include <machine/stdarg.h>
3661ae650dSJack F Vogel 
3761ae650dSJack F Vogel #include "ixl.h"
3861ae650dSJack F Vogel 
3961ae650dSJack F Vogel /********************************************************************
4061ae650dSJack F Vogel  * Manage DMA'able memory.
4161ae650dSJack F Vogel  *******************************************************************/
4261ae650dSJack F Vogel static void
4361ae650dSJack F Vogel i40e_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error)
4461ae650dSJack F Vogel {
4561ae650dSJack F Vogel         if (error)
4661ae650dSJack F Vogel                 return;
4761ae650dSJack F Vogel         *(bus_addr_t *) arg = segs->ds_addr;
4861ae650dSJack F Vogel         return;
4961ae650dSJack F Vogel }
5061ae650dSJack F Vogel 
5161ae650dSJack F Vogel i40e_status
52*ea022094SBjoern A. Zeeb i40e_allocate_virt(struct i40e_hw *hw, struct i40e_virt_mem *mem, u32 size)
5361ae650dSJack F Vogel {
54*ea022094SBjoern A. Zeeb 	mem->va = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
55*ea022094SBjoern A. Zeeb 	return(mem->va == NULL);
5661ae650dSJack F Vogel }
5761ae650dSJack F Vogel 
5861ae650dSJack F Vogel i40e_status
59*ea022094SBjoern A. Zeeb i40e_free_virt(struct i40e_hw *hw, struct i40e_virt_mem *mem)
6061ae650dSJack F Vogel {
61*ea022094SBjoern A. Zeeb 	free(mem->va, M_DEVBUF);
6261ae650dSJack F Vogel 	return(0);
6361ae650dSJack F Vogel }
6461ae650dSJack F Vogel 
6561ae650dSJack F Vogel i40e_status
66*ea022094SBjoern A. Zeeb i40e_allocate_dma(struct i40e_hw *hw, struct i40e_dma_mem *mem,
6761ae650dSJack F Vogel 	bus_size_t size, u32 alignment)
6861ae650dSJack F Vogel {
6961ae650dSJack F Vogel 	device_t	dev = ((struct i40e_osdep *)hw->back)->dev;
7061ae650dSJack F Vogel 	int		err;
7161ae650dSJack F Vogel 
7261ae650dSJack F Vogel 
7361ae650dSJack F Vogel 	err = bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
7461ae650dSJack F Vogel 			       alignment, 0,	/* alignment, bounds */
7561ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* lowaddr */
7661ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* highaddr */
7761ae650dSJack F Vogel 			       NULL, NULL,	/* filter, filterarg */
7861ae650dSJack F Vogel 			       size,	/* maxsize */
7961ae650dSJack F Vogel 			       1,	/* nsegments */
8061ae650dSJack F Vogel 			       size,	/* maxsegsize */
8161ae650dSJack F Vogel 			       BUS_DMA_ALLOCNOW, /* flags */
8261ae650dSJack F Vogel 			       NULL,	/* lockfunc */
8361ae650dSJack F Vogel 			       NULL,	/* lockfuncarg */
84*ea022094SBjoern A. Zeeb 			       &mem->tag);
8561ae650dSJack F Vogel 	if (err != 0) {
8661ae650dSJack F Vogel 		device_printf(dev,
8761ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dma_tag_create failed, "
8861ae650dSJack F Vogel 		    "error %u\n", err);
8961ae650dSJack F Vogel 		goto fail_0;
9061ae650dSJack F Vogel 	}
91*ea022094SBjoern A. Zeeb 	err = bus_dmamem_alloc(mem->tag, (void **)&mem->va,
92*ea022094SBjoern A. Zeeb 			     BUS_DMA_NOWAIT | BUS_DMA_ZERO, &mem->map);
9361ae650dSJack F Vogel 	if (err != 0) {
9461ae650dSJack F Vogel 		device_printf(dev,
9561ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamem_alloc failed, "
9661ae650dSJack F Vogel 		    "error %u\n", err);
9761ae650dSJack F Vogel 		goto fail_1;
9861ae650dSJack F Vogel 	}
99*ea022094SBjoern A. Zeeb 	err = bus_dmamap_load(mem->tag, mem->map, mem->va,
10061ae650dSJack F Vogel 			    size,
10161ae650dSJack F Vogel 			    i40e_dmamap_cb,
102*ea022094SBjoern A. Zeeb 			    &mem->pa,
10361ae650dSJack F Vogel 			    BUS_DMA_NOWAIT);
10461ae650dSJack F Vogel 	if (err != 0) {
10561ae650dSJack F Vogel 		device_printf(dev,
10661ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamap_load failed, "
10761ae650dSJack F Vogel 		    "error %u\n", err);
10861ae650dSJack F Vogel 		goto fail_2;
10961ae650dSJack F Vogel 	}
110*ea022094SBjoern A. Zeeb 	mem->size = size;
111*ea022094SBjoern A. Zeeb 	bus_dmamap_sync(mem->tag, mem->map,
11261ae650dSJack F Vogel 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
11361ae650dSJack F Vogel 	return (0);
11461ae650dSJack F Vogel fail_2:
115*ea022094SBjoern A. Zeeb 	bus_dmamem_free(mem->tag, mem->va, mem->map);
11661ae650dSJack F Vogel fail_1:
117*ea022094SBjoern A. Zeeb 	bus_dma_tag_destroy(mem->tag);
11861ae650dSJack F Vogel fail_0:
119*ea022094SBjoern A. Zeeb 	mem->map = NULL;
120*ea022094SBjoern A. Zeeb 	mem->tag = NULL;
12161ae650dSJack F Vogel 	return (err);
12261ae650dSJack F Vogel }
12361ae650dSJack F Vogel 
12461ae650dSJack F Vogel i40e_status
125*ea022094SBjoern A. Zeeb i40e_free_dma(struct i40e_hw *hw, struct i40e_dma_mem *mem)
12661ae650dSJack F Vogel {
127*ea022094SBjoern A. Zeeb 	bus_dmamap_sync(mem->tag, mem->map,
12861ae650dSJack F Vogel 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
129*ea022094SBjoern A. Zeeb 	bus_dmamap_unload(mem->tag, mem->map);
130*ea022094SBjoern A. Zeeb 	bus_dmamem_free(mem->tag, mem->va, mem->map);
131*ea022094SBjoern A. Zeeb 	bus_dma_tag_destroy(mem->tag);
13261ae650dSJack F Vogel 	return (0);
13361ae650dSJack F Vogel }
13461ae650dSJack F Vogel 
13561ae650dSJack F Vogel void
13661ae650dSJack F Vogel i40e_init_spinlock(struct i40e_spinlock *lock)
13761ae650dSJack F Vogel {
13861ae650dSJack F Vogel 	mtx_init(&lock->mutex, "mutex",
13961ae650dSJack F Vogel 	    MTX_NETWORK_LOCK, MTX_DEF | MTX_DUPOK);
14061ae650dSJack F Vogel }
14161ae650dSJack F Vogel 
14261ae650dSJack F Vogel void
14361ae650dSJack F Vogel i40e_acquire_spinlock(struct i40e_spinlock *lock)
14461ae650dSJack F Vogel {
14561ae650dSJack F Vogel 	mtx_lock(&lock->mutex);
14661ae650dSJack F Vogel }
14761ae650dSJack F Vogel 
14861ae650dSJack F Vogel void
14961ae650dSJack F Vogel i40e_release_spinlock(struct i40e_spinlock *lock)
15061ae650dSJack F Vogel {
15161ae650dSJack F Vogel 	mtx_unlock(&lock->mutex);
15261ae650dSJack F Vogel }
15361ae650dSJack F Vogel 
15461ae650dSJack F Vogel void
15561ae650dSJack F Vogel i40e_destroy_spinlock(struct i40e_spinlock *lock)
15661ae650dSJack F Vogel {
15761ae650dSJack F Vogel 	mtx_destroy(&lock->mutex);
15861ae650dSJack F Vogel }
15961ae650dSJack F Vogel 
16061ae650dSJack F Vogel /*
16161ae650dSJack F Vogel ** i40e_debug_d - OS dependent version of shared code debug printing
16261ae650dSJack F Vogel */
16361ae650dSJack F Vogel void i40e_debug_d(void *hw, u32 mask, char *fmt, ...)
16461ae650dSJack F Vogel {
16561ae650dSJack F Vogel         char buf[512];
16661ae650dSJack F Vogel         va_list args;
16761ae650dSJack F Vogel 
16861ae650dSJack F Vogel         if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
16961ae650dSJack F Vogel                 return;
17061ae650dSJack F Vogel 
17161ae650dSJack F Vogel 	va_start(args, fmt);
17261ae650dSJack F Vogel         vsnprintf(buf, sizeof(buf), fmt, args);
17361ae650dSJack F Vogel 	va_end(args);
17461ae650dSJack F Vogel 
17561ae650dSJack F Vogel         /* the debug string is already formatted with a newline */
17661ae650dSJack F Vogel         printf("%s", buf);
17761ae650dSJack F Vogel }
17861ae650dSJack F Vogel 
17961ae650dSJack F Vogel u16
18061ae650dSJack F Vogel i40e_read_pci_cfg(struct i40e_hw *hw, u32 reg)
18161ae650dSJack F Vogel {
18261ae650dSJack F Vogel         u16 value;
18361ae650dSJack F Vogel 
18461ae650dSJack F Vogel         value = pci_read_config(((struct i40e_osdep *)hw->back)->dev,
18561ae650dSJack F Vogel             reg, 2);
18661ae650dSJack F Vogel 
18761ae650dSJack F Vogel         return (value);
18861ae650dSJack F Vogel }
18961ae650dSJack F Vogel 
19061ae650dSJack F Vogel void
19161ae650dSJack F Vogel i40e_write_pci_cfg(struct i40e_hw *hw, u32 reg, u16 value)
19261ae650dSJack F Vogel {
19361ae650dSJack F Vogel         pci_write_config(((struct i40e_osdep *)hw->back)->dev,
19461ae650dSJack F Vogel             reg, value, 2);
19561ae650dSJack F Vogel 
19661ae650dSJack F Vogel         return;
19761ae650dSJack F Vogel }
19861ae650dSJack F Vogel 
199