xref: /freebsd/sys/dev/ixl/i40e_osdep.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
161ae650dSJack F Vogel /******************************************************************************
261ae650dSJack F Vogel 
3f4cc2d17SEric Joyner   Copyright (c) 2013-2018, 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 
344294f337SSean Bruno #include <sys/limits.h>
35b4a7ce06SEric Joyner #include <sys/time.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
i40e_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)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 }
4961ae650dSJack F Vogel 
5061ae650dSJack F Vogel i40e_status
i40e_allocate_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)51d94ca7cfSBjoern A. Zeeb i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem, u32 size)
5261ae650dSJack F Vogel {
53*7d4dceecSKrzysztof Galazka 	mem->va = malloc(size, M_IXL, M_NOWAIT | M_ZERO);
54ea022094SBjoern A. Zeeb 	return (mem->va == NULL);
5561ae650dSJack F Vogel }
5661ae650dSJack F Vogel 
5761ae650dSJack F Vogel i40e_status
i40e_free_virt_mem(struct i40e_hw * hw,struct i40e_virt_mem * mem)58d94ca7cfSBjoern A. Zeeb i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
5961ae650dSJack F Vogel {
60*7d4dceecSKrzysztof Galazka 	free(mem->va, M_IXL);
61ceebc2f3SEric Joyner 	mem->va = NULL;
62ceebc2f3SEric Joyner 
63b4a7ce06SEric Joyner 	return (I40E_SUCCESS);
6461ae650dSJack F Vogel }
6561ae650dSJack F Vogel 
6661ae650dSJack F Vogel i40e_status
i40e_allocate_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem,enum i40e_memory_type type __unused,u64 size,u32 alignment)67d94ca7cfSBjoern A. Zeeb i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
68d94ca7cfSBjoern A. Zeeb 	enum i40e_memory_type type __unused, u64 size, u32 alignment)
6961ae650dSJack F Vogel {
7061ae650dSJack F Vogel 	device_t	dev = ((struct i40e_osdep *)hw->back)->dev;
7161ae650dSJack F Vogel 	int		err;
7261ae650dSJack F Vogel 
7361ae650dSJack F Vogel 
7461ae650dSJack F Vogel 	err = bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
7561ae650dSJack F Vogel 			       alignment, 0,	/* alignment, bounds */
7661ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* lowaddr */
7761ae650dSJack F Vogel 			       BUS_SPACE_MAXADDR,	/* highaddr */
7861ae650dSJack F Vogel 			       NULL, NULL,	/* filter, filterarg */
7961ae650dSJack F Vogel 			       size,	/* maxsize */
8061ae650dSJack F Vogel 			       1,	/* nsegments */
8161ae650dSJack F Vogel 			       size,	/* maxsegsize */
8261ae650dSJack F Vogel 			       BUS_DMA_ALLOCNOW, /* flags */
8361ae650dSJack F Vogel 			       NULL,	/* lockfunc */
8461ae650dSJack F Vogel 			       NULL,	/* lockfuncarg */
85ea022094SBjoern A. Zeeb 			       &mem->tag);
8661ae650dSJack F Vogel 	if (err != 0) {
8761ae650dSJack F Vogel 		device_printf(dev,
8861ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dma_tag_create failed, "
8961ae650dSJack F Vogel 		    "error %u\n", err);
9061ae650dSJack F Vogel 		goto fail_0;
9161ae650dSJack F Vogel 	}
92ea022094SBjoern A. Zeeb 	err = bus_dmamem_alloc(mem->tag, (void **)&mem->va,
93ea022094SBjoern A. Zeeb 			     BUS_DMA_NOWAIT | BUS_DMA_ZERO, &mem->map);
9461ae650dSJack F Vogel 	if (err != 0) {
9561ae650dSJack F Vogel 		device_printf(dev,
9661ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamem_alloc failed, "
9761ae650dSJack F Vogel 		    "error %u\n", err);
9861ae650dSJack F Vogel 		goto fail_1;
9961ae650dSJack F Vogel 	}
100ea022094SBjoern A. Zeeb 	err = bus_dmamap_load(mem->tag, mem->map, mem->va,
10161ae650dSJack F Vogel 			    size,
10261ae650dSJack F Vogel 			    i40e_dmamap_cb,
103ea022094SBjoern A. Zeeb 			    &mem->pa,
10461ae650dSJack F Vogel 			    BUS_DMA_NOWAIT);
10561ae650dSJack F Vogel 	if (err != 0) {
10661ae650dSJack F Vogel 		device_printf(dev,
10761ae650dSJack F Vogel 		    "i40e_allocate_dma: bus_dmamap_load failed, "
10861ae650dSJack F Vogel 		    "error %u\n", err);
10961ae650dSJack F Vogel 		goto fail_2;
11061ae650dSJack F Vogel 	}
111ea022094SBjoern A. Zeeb 	mem->size = size;
112ea022094SBjoern A. Zeeb 	bus_dmamap_sync(mem->tag, mem->map,
11361ae650dSJack F Vogel 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
114b4a7ce06SEric Joyner 	return (I40E_SUCCESS);
11561ae650dSJack F Vogel fail_2:
116ea022094SBjoern A. Zeeb 	bus_dmamem_free(mem->tag, mem->va, mem->map);
11761ae650dSJack F Vogel fail_1:
118ea022094SBjoern A. Zeeb 	bus_dma_tag_destroy(mem->tag);
11961ae650dSJack F Vogel fail_0:
120ea022094SBjoern A. Zeeb 	mem->map = NULL;
121ea022094SBjoern A. Zeeb 	mem->tag = NULL;
12261ae650dSJack F Vogel 	return (err);
12361ae650dSJack F Vogel }
12461ae650dSJack F Vogel 
12561ae650dSJack F Vogel i40e_status
i40e_free_dma_mem(struct i40e_hw * hw,struct i40e_dma_mem * mem)126d94ca7cfSBjoern A. Zeeb i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
12761ae650dSJack F Vogel {
128ea022094SBjoern A. Zeeb 	bus_dmamap_sync(mem->tag, mem->map,
12961ae650dSJack F Vogel 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
130ea022094SBjoern A. Zeeb 	bus_dmamap_unload(mem->tag, mem->map);
131ea022094SBjoern A. Zeeb 	bus_dmamem_free(mem->tag, mem->va, mem->map);
132ea022094SBjoern A. Zeeb 	bus_dma_tag_destroy(mem->tag);
1331031d839SEric Joyner 	return (I40E_SUCCESS);
13461ae650dSJack F Vogel }
13561ae650dSJack F Vogel 
13661ae650dSJack F Vogel void
i40e_init_spinlock(struct i40e_spinlock * lock)13761ae650dSJack F Vogel i40e_init_spinlock(struct i40e_spinlock *lock)
13861ae650dSJack F Vogel {
13961ae650dSJack F Vogel 	mtx_init(&lock->mutex, "mutex",
1404294f337SSean Bruno 	    "ixl spinlock", MTX_DEF | MTX_DUPOK);
14161ae650dSJack F Vogel }
14261ae650dSJack F Vogel 
14361ae650dSJack F Vogel void
i40e_acquire_spinlock(struct i40e_spinlock * lock)14461ae650dSJack F Vogel i40e_acquire_spinlock(struct i40e_spinlock *lock)
14561ae650dSJack F Vogel {
14661ae650dSJack F Vogel 	mtx_lock(&lock->mutex);
14761ae650dSJack F Vogel }
14861ae650dSJack F Vogel 
14961ae650dSJack F Vogel void
i40e_release_spinlock(struct i40e_spinlock * lock)15061ae650dSJack F Vogel i40e_release_spinlock(struct i40e_spinlock *lock)
15161ae650dSJack F Vogel {
15261ae650dSJack F Vogel 	mtx_unlock(&lock->mutex);
15361ae650dSJack F Vogel }
15461ae650dSJack F Vogel 
15561ae650dSJack F Vogel void
i40e_destroy_spinlock(struct i40e_spinlock * lock)15661ae650dSJack F Vogel i40e_destroy_spinlock(struct i40e_spinlock *lock)
15761ae650dSJack F Vogel {
1586d011ad5SEric Joyner 	if (mtx_initialized(&lock->mutex))
15961ae650dSJack F Vogel 		mtx_destroy(&lock->mutex);
16061ae650dSJack F Vogel }
16161ae650dSJack F Vogel 
162b4a7ce06SEric Joyner #ifndef MSEC_2_TICKS
163b4a7ce06SEric Joyner #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
164b4a7ce06SEric Joyner 	  (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
165b4a7ce06SEric Joyner #endif
16677c1fcecSEric Joyner 
1674294f337SSean Bruno void
i40e_msec_pause(int msecs)1684294f337SSean Bruno i40e_msec_pause(int msecs)
1694294f337SSean Bruno {
170b4a7ce06SEric Joyner 	pause("i40e_msec_pause", MSEC_2_TICKS(msecs));
1714294f337SSean Bruno }
1724294f337SSean Bruno 
17361ae650dSJack F Vogel /*
1746c426059SEric Joyner  * Helper function for debug statement printing
17561ae650dSJack F Vogel  */
1766c426059SEric Joyner void
i40e_debug_shared(struct i40e_hw * hw,enum i40e_debug_mask mask,char * fmt,...)1774294f337SSean Bruno i40e_debug_shared(struct i40e_hw *hw, enum i40e_debug_mask mask, char *fmt, ...)
17861ae650dSJack F Vogel {
17961ae650dSJack F Vogel 	va_list args;
180cb6b8299SEric Joyner 	device_t dev;
18161ae650dSJack F Vogel 
18261ae650dSJack F Vogel 	if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
18361ae650dSJack F Vogel 		return;
18461ae650dSJack F Vogel 
185cb6b8299SEric Joyner 	dev = ((struct i40e_osdep *)hw->back)->dev;
186cb6b8299SEric Joyner 
187cb6b8299SEric Joyner 	/* Re-implement device_printf() */
188cb6b8299SEric Joyner 	device_print_prettyname(dev);
18961ae650dSJack F Vogel 	va_start(args, fmt);
190cb6b8299SEric Joyner 	vprintf(fmt, args);
19161ae650dSJack F Vogel 	va_end(args);
19261ae650dSJack F Vogel }
19361ae650dSJack F Vogel 
194cb6b8299SEric Joyner const char *
ixl_vc_opcode_str(uint16_t op)195cb6b8299SEric Joyner ixl_vc_opcode_str(uint16_t op)
196cb6b8299SEric Joyner {
197cb6b8299SEric Joyner 	switch (op) {
198ceebc2f3SEric Joyner 	case VIRTCHNL_OP_VERSION:
199cb6b8299SEric Joyner 		return ("VERSION");
200ceebc2f3SEric Joyner 	case VIRTCHNL_OP_RESET_VF:
201cb6b8299SEric Joyner 		return ("RESET_VF");
202ceebc2f3SEric Joyner 	case VIRTCHNL_OP_GET_VF_RESOURCES:
203cb6b8299SEric Joyner 		return ("GET_VF_RESOURCES");
204ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_TX_QUEUE:
205cb6b8299SEric Joyner 		return ("CONFIG_TX_QUEUE");
206ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_RX_QUEUE:
207cb6b8299SEric Joyner 		return ("CONFIG_RX_QUEUE");
208ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
209cb6b8299SEric Joyner 		return ("CONFIG_VSI_QUEUES");
210ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
211cb6b8299SEric Joyner 		return ("CONFIG_IRQ_MAP");
212ceebc2f3SEric Joyner 	case VIRTCHNL_OP_ENABLE_QUEUES:
213cb6b8299SEric Joyner 		return ("ENABLE_QUEUES");
214ceebc2f3SEric Joyner 	case VIRTCHNL_OP_DISABLE_QUEUES:
215cb6b8299SEric Joyner 		return ("DISABLE_QUEUES");
216ceebc2f3SEric Joyner 	case VIRTCHNL_OP_ADD_ETH_ADDR:
217ceebc2f3SEric Joyner 		return ("ADD_ETH_ADDR");
218ceebc2f3SEric Joyner 	case VIRTCHNL_OP_DEL_ETH_ADDR:
219ceebc2f3SEric Joyner 		return ("DEL_ETH_ADDR");
220ceebc2f3SEric Joyner 	case VIRTCHNL_OP_ADD_VLAN:
221cb6b8299SEric Joyner 		return ("ADD_VLAN");
222ceebc2f3SEric Joyner 	case VIRTCHNL_OP_DEL_VLAN:
223cb6b8299SEric Joyner 		return ("DEL_VLAN");
224ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
225cb6b8299SEric Joyner 		return ("CONFIG_PROMISCUOUS_MODE");
226ceebc2f3SEric Joyner 	case VIRTCHNL_OP_GET_STATS:
227cb6b8299SEric Joyner 		return ("GET_STATS");
228ceebc2f3SEric Joyner 	case VIRTCHNL_OP_RSVD:
229ceebc2f3SEric Joyner 		return ("RSVD");
230ceebc2f3SEric Joyner 	case VIRTCHNL_OP_EVENT:
231cb6b8299SEric Joyner 		return ("EVENT");
232ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
233cb6b8299SEric Joyner 		return ("CONFIG_RSS_KEY");
234ceebc2f3SEric Joyner 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
235cb6b8299SEric Joyner 		return ("CONFIG_RSS_LUT");
236ceebc2f3SEric Joyner 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
237cb6b8299SEric Joyner 		return ("GET_RSS_HENA_CAPS");
238ceebc2f3SEric Joyner 	case VIRTCHNL_OP_SET_RSS_HENA:
239cb6b8299SEric Joyner 		return ("SET_RSS_HENA");
240cb6b8299SEric Joyner 	default:
241cb6b8299SEric Joyner 		return ("UNKNOWN");
242cb6b8299SEric Joyner 	}
243cb6b8299SEric Joyner }
244cb6b8299SEric Joyner 
24561ae650dSJack F Vogel u16
i40e_read_pci_cfg(struct i40e_hw * hw,u32 reg)24661ae650dSJack F Vogel i40e_read_pci_cfg(struct i40e_hw *hw, u32 reg)
24761ae650dSJack F Vogel {
24861ae650dSJack F Vogel         u16 value;
24961ae650dSJack F Vogel 
25061ae650dSJack F Vogel         value = pci_read_config(((struct i40e_osdep *)hw->back)->dev,
25161ae650dSJack F Vogel             reg, 2);
25261ae650dSJack F Vogel 
25361ae650dSJack F Vogel         return (value);
25461ae650dSJack F Vogel }
25561ae650dSJack F Vogel 
25661ae650dSJack F Vogel void
i40e_write_pci_cfg(struct i40e_hw * hw,u32 reg,u16 value)25761ae650dSJack F Vogel i40e_write_pci_cfg(struct i40e_hw *hw, u32 reg, u16 value)
25861ae650dSJack F Vogel {
25961ae650dSJack F Vogel         pci_write_config(((struct i40e_osdep *)hw->back)->dev,
26061ae650dSJack F Vogel             reg, value, 2);
26161ae650dSJack F Vogel }
26261ae650dSJack F Vogel 
263