xref: /freebsd/sys/dev/ixl/i40e_osdep.h (revision ceebc2f348c028b21bf9bcc99f7a3c4b0cb7d926)
161ae650dSJack F Vogel /******************************************************************************
261ae650dSJack F Vogel 
3*ceebc2f3SEric Joyner   Copyright (c) 2013-2017, 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 #ifndef _I40E_OSDEP_H_
3661ae650dSJack F Vogel #define _I40E_OSDEP_H_
3761ae650dSJack F Vogel 
3861ae650dSJack F Vogel #include <sys/types.h>
3961ae650dSJack F Vogel #include <sys/param.h>
4061ae650dSJack F Vogel #include <sys/systm.h>
4161ae650dSJack F Vogel #include <sys/endian.h>
4261ae650dSJack F Vogel #include <sys/mbuf.h>
4361ae650dSJack F Vogel #include <sys/protosw.h>
4461ae650dSJack F Vogel #include <sys/socket.h>
4561ae650dSJack F Vogel #include <sys/malloc.h>
4661ae650dSJack F Vogel #include <sys/kernel.h>
4761ae650dSJack F Vogel #include <sys/bus.h>
4861ae650dSJack F Vogel #include <machine/bus.h>
4961ae650dSJack F Vogel #include <sys/rman.h>
5061ae650dSJack F Vogel #include <machine/resource.h>
5161ae650dSJack F Vogel #include <vm/vm.h>
5261ae650dSJack F Vogel #include <vm/pmap.h>
5361ae650dSJack F Vogel #include <machine/clock.h>
5461ae650dSJack F Vogel #include <dev/pci/pcivar.h>
5561ae650dSJack F Vogel #include <dev/pci/pcireg.h>
5661ae650dSJack F Vogel 
5761ae650dSJack F Vogel #define i40e_usec_delay(x) DELAY(x)
5861ae650dSJack F Vogel #define i40e_msec_delay(x) DELAY(1000 * (x))
5961ae650dSJack F Vogel 
6061ae650dSJack F Vogel #define DBG 0
6161ae650dSJack F Vogel #define MSGOUT(S, A, B)     printf(S "\n", A, B)
6261ae650dSJack F Vogel #define DEBUGFUNC(F)        DEBUGOUT(F);
6361ae650dSJack F Vogel #if DBG
6461ae650dSJack F Vogel 	#define DEBUGOUT(S)         printf(S "\n")
6561ae650dSJack F Vogel 	#define DEBUGOUT1(S,A)      printf(S "\n",A)
6661ae650dSJack F Vogel 	#define DEBUGOUT2(S,A,B)    printf(S "\n",A,B)
6761ae650dSJack F Vogel 	#define DEBUGOUT3(S,A,B,C)  printf(S "\n",A,B,C)
6861ae650dSJack F Vogel 	#define DEBUGOUT7(S,A,B,C,D,E,F,G)  printf(S "\n",A,B,C,D,E,F,G)
6961ae650dSJack F Vogel #else
7061ae650dSJack F Vogel 	#define DEBUGOUT(S)
7161ae650dSJack F Vogel 	#define DEBUGOUT1(S,A)
7261ae650dSJack F Vogel 	#define DEBUGOUT2(S,A,B)
7361ae650dSJack F Vogel 	#define DEBUGOUT3(S,A,B,C)
7461ae650dSJack F Vogel 	#define DEBUGOUT6(S,A,B,C,D,E,F)
7561ae650dSJack F Vogel 	#define DEBUGOUT7(S,A,B,C,D,E,F,G)
7661ae650dSJack F Vogel #endif
7761ae650dSJack F Vogel 
784294f337SSean Bruno /* Remove unused shared code macros */
7961ae650dSJack F Vogel #define UNREFERENCED_PARAMETER(_p)
8061ae650dSJack F Vogel #define UNREFERENCED_1PARAMETER(_p)
8161ae650dSJack F Vogel #define UNREFERENCED_2PARAMETER(_p, _q)
8261ae650dSJack F Vogel #define UNREFERENCED_3PARAMETER(_p, _q, _r)
8361ae650dSJack F Vogel #define UNREFERENCED_4PARAMETER(_p, _q, _r, _s)
844294f337SSean Bruno #define UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t)
8561ae650dSJack F Vogel 
8661ae650dSJack F Vogel #define STATIC	static
8761ae650dSJack F Vogel #define INLINE  inline
8861ae650dSJack F Vogel 
8961ae650dSJack F Vogel #define FALSE               0
9061ae650dSJack F Vogel #define false               0 /* shared code requires this */
9161ae650dSJack F Vogel #define TRUE                1
9261ae650dSJack F Vogel #define true                1
9361ae650dSJack F Vogel #define CMD_MEM_WRT_INVALIDATE          0x0010  /* BIT_4 */
9461ae650dSJack F Vogel #define PCI_COMMAND_REGISTER            PCIR_COMMAND
9561ae650dSJack F Vogel #define ARRAY_SIZE(a)		(sizeof(a) / sizeof((a)[0]))
9661ae650dSJack F Vogel 
9761ae650dSJack F Vogel #define i40e_memset(a, b, c, d)  memset((a), (b), (c))
9861ae650dSJack F Vogel #define i40e_memcpy(a, b, c, d)  memcpy((a), (b), (c))
9961ae650dSJack F Vogel 
10061ae650dSJack F Vogel #define CPU_TO_LE16(o)	htole16(o)
10161ae650dSJack F Vogel #define CPU_TO_LE32(s)	htole32(s)
10261ae650dSJack F Vogel #define CPU_TO_LE64(h)	htole64(h)
10361ae650dSJack F Vogel #define LE16_TO_CPU(a)	le16toh(a)
10461ae650dSJack F Vogel #define LE32_TO_CPU(c)	le32toh(c)
10561ae650dSJack F Vogel #define LE64_TO_CPU(k)	le64toh(k)
10661ae650dSJack F Vogel 
10761ae650dSJack F Vogel #define I40E_NTOHS(a)	ntohs(a)
10861ae650dSJack F Vogel #define I40E_NTOHL(a)	ntohl(a)
10961ae650dSJack F Vogel #define I40E_HTONS(a)	htons(a)
11061ae650dSJack F Vogel #define I40E_HTONL(a)	htonl(a)
11161ae650dSJack F Vogel 
11261ae650dSJack F Vogel #define FIELD_SIZEOF(x, y) (sizeof(((x*)0)->y))
11361ae650dSJack F Vogel 
11461ae650dSJack F Vogel typedef uint8_t		u8;
11561ae650dSJack F Vogel typedef int8_t		s8;
11661ae650dSJack F Vogel typedef uint16_t	u16;
11761ae650dSJack F Vogel typedef int16_t		s16;
11861ae650dSJack F Vogel typedef uint32_t	u32;
11961ae650dSJack F Vogel typedef int32_t		s32;
12061ae650dSJack F Vogel typedef uint64_t	u64;
12161ae650dSJack F Vogel 
12261ae650dSJack F Vogel /* long string relief */
12361ae650dSJack F Vogel typedef enum i40e_status_code i40e_status;
12461ae650dSJack F Vogel 
12561ae650dSJack F Vogel #define __le16  u16
12661ae650dSJack F Vogel #define __le32  u32
12761ae650dSJack F Vogel #define __le64  u64
12861ae650dSJack F Vogel #define __be16  u16
12961ae650dSJack F Vogel #define __be32  u32
13061ae650dSJack F Vogel #define __be64  u64
13161ae650dSJack F Vogel 
13261ae650dSJack F Vogel /* SW spinlock */
13361ae650dSJack F Vogel struct i40e_spinlock {
13461ae650dSJack F Vogel         struct mtx mutex;
13561ae650dSJack F Vogel };
13661ae650dSJack F Vogel 
13761ae650dSJack F Vogel #define le16_to_cpu
13861ae650dSJack F Vogel 
139f894ad88SBjoern A. Zeeb #if defined(__amd64__) || defined(i386)
14061ae650dSJack F Vogel static __inline
14161ae650dSJack F Vogel void prefetch(void *x)
14261ae650dSJack F Vogel {
14361ae650dSJack F Vogel 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
14461ae650dSJack F Vogel }
145f894ad88SBjoern A. Zeeb #else
146f894ad88SBjoern A. Zeeb #define	prefetch(x)
147f894ad88SBjoern A. Zeeb #endif
14861ae650dSJack F Vogel 
149e5100ee2SJack F Vogel struct i40e_osdep {
15061ae650dSJack F Vogel 	bus_space_tag_t		mem_bus_space_tag;
15161ae650dSJack F Vogel 	bus_space_handle_t	mem_bus_space_handle;
15261ae650dSJack F Vogel 	bus_size_t		mem_bus_space_size;
153cf3c0c32SRyan Stone 	uint32_t		flush_reg;
154cb6b8299SEric Joyner 	int			i2c_intfc_num;
155bd937497SJean-Sébastien Pédron 	device_t		dev;
15661ae650dSJack F Vogel };
15761ae650dSJack F Vogel 
15861ae650dSJack F Vogel struct i40e_dma_mem {
15961ae650dSJack F Vogel 	void			*va;
16061ae650dSJack F Vogel 	u64			pa;
16161ae650dSJack F Vogel 	bus_dma_tag_t		tag;
16261ae650dSJack F Vogel 	bus_dmamap_t		map;
16361ae650dSJack F Vogel 	bus_dma_segment_t	seg;
16461ae650dSJack F Vogel 	bus_size_t              size;
16561ae650dSJack F Vogel 	int			nseg;
16661ae650dSJack F Vogel 	int                     flags;
16761ae650dSJack F Vogel };
16861ae650dSJack F Vogel 
16961ae650dSJack F Vogel struct i40e_virt_mem {
17061ae650dSJack F Vogel 	void *va;
17161ae650dSJack F Vogel 	u32 size;
17261ae650dSJack F Vogel };
17361ae650dSJack F Vogel 
1746c426059SEric Joyner struct i40e_hw; /* forward decl */
1756c426059SEric Joyner u16	i40e_read_pci_cfg(struct i40e_hw *, u32);
1766c426059SEric Joyner void	i40e_write_pci_cfg(struct i40e_hw *, u32, u16);
1776c426059SEric Joyner 
17861ae650dSJack F Vogel /*
1796c426059SEric Joyner ** i40e_debug - OS dependent version of shared code debug printing
1806c426059SEric Joyner */
1816c426059SEric Joyner enum i40e_debug_mask;
1824294f337SSean Bruno #define i40e_debug(h, m, s, ...)  i40e_debug_shared(h, m, s, ##__VA_ARGS__)
1834294f337SSean Bruno extern void i40e_debug_shared(struct i40e_hw *hw, enum i40e_debug_mask mask,
1846c426059SEric Joyner     char *fmt_str, ...);
1856c426059SEric Joyner 
1864294f337SSean Bruno /* Non-busy-wait that uses kern_yield() */
1874294f337SSean Bruno void i40e_msec_pause(int);
1884294f337SSean Bruno 
189cb6b8299SEric Joyner const char * ixl_vc_opcode_str(uint16_t op);
190cb6b8299SEric Joyner 
1916c426059SEric Joyner /*
1926c426059SEric Joyner ** This hardware supports either 16 or 32 byte rx descriptors;
1936c426059SEric Joyner ** the driver only uses the 32 byte kind.
19461ae650dSJack F Vogel */
19561ae650dSJack F Vogel #define i40e_rx_desc i40e_32byte_rx_desc
19661ae650dSJack F Vogel 
19761ae650dSJack F Vogel static __inline uint32_t
19861ae650dSJack F Vogel rd32_osdep(struct i40e_osdep *osdep, uint32_t reg)
19961ae650dSJack F Vogel {
20061ae650dSJack F Vogel 
20161ae650dSJack F Vogel 	KASSERT(reg < osdep->mem_bus_space_size,
202be771cdaSJack F Vogel 	    ("ixl: register offset %#jx too large (max is %#jx)",
20313b40804SSteven Hartland 	    (uintmax_t)reg, (uintmax_t)osdep->mem_bus_space_size));
20461ae650dSJack F Vogel 
20561ae650dSJack F Vogel 	return (bus_space_read_4(osdep->mem_bus_space_tag,
20661ae650dSJack F Vogel 	    osdep->mem_bus_space_handle, reg));
20761ae650dSJack F Vogel }
20861ae650dSJack F Vogel 
20961ae650dSJack F Vogel static __inline void
21061ae650dSJack F Vogel wr32_osdep(struct i40e_osdep *osdep, uint32_t reg, uint32_t value)
21161ae650dSJack F Vogel {
21261ae650dSJack F Vogel 
21361ae650dSJack F Vogel 	KASSERT(reg < osdep->mem_bus_space_size,
214be771cdaSJack F Vogel 	    ("ixl: register offset %#jx too large (max is %#jx)",
21513b40804SSteven Hartland 	    (uintmax_t)reg, (uintmax_t)osdep->mem_bus_space_size));
21661ae650dSJack F Vogel 
21761ae650dSJack F Vogel 	bus_space_write_4(osdep->mem_bus_space_tag,
21861ae650dSJack F Vogel 	    osdep->mem_bus_space_handle, reg, value);
21961ae650dSJack F Vogel }
22061ae650dSJack F Vogel 
221cf3c0c32SRyan Stone static __inline void
222cf3c0c32SRyan Stone ixl_flush_osdep(struct i40e_osdep *osdep)
223cf3c0c32SRyan Stone {
224cf3c0c32SRyan Stone 	rd32_osdep(osdep, osdep->flush_reg);
225cf3c0c32SRyan Stone }
226cf3c0c32SRyan Stone 
22761ae650dSJack F Vogel #define rd32(a, reg)		rd32_osdep((a)->back, (reg))
22861ae650dSJack F Vogel #define wr32(a, reg, value)	wr32_osdep((a)->back, (reg), (value))
22961ae650dSJack F Vogel 
23061ae650dSJack F Vogel #define rd64(a, reg) (\
23161ae650dSJack F Vogel    bus_space_read_8( ((struct i40e_osdep *)(a)->back)->mem_bus_space_tag, \
23261ae650dSJack F Vogel                      ((struct i40e_osdep *)(a)->back)->mem_bus_space_handle, \
23361ae650dSJack F Vogel                      reg))
23461ae650dSJack F Vogel 
23561ae650dSJack F Vogel #define wr64(a, reg, value) (\
23661ae650dSJack F Vogel    bus_space_write_8( ((struct i40e_osdep *)(a)->back)->mem_bus_space_tag, \
23761ae650dSJack F Vogel                      ((struct i40e_osdep *)(a)->back)->mem_bus_space_handle, \
23861ae650dSJack F Vogel                      reg, value))
23961ae650dSJack F Vogel 
240cf3c0c32SRyan Stone #define ixl_flush(a)		ixl_flush_osdep((a)->back)
24161ae650dSJack F Vogel 
242*ceebc2f3SEric Joyner enum i40e_status_code i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
243*ceebc2f3SEric Joyner 					       u16 *data);
244*ceebc2f3SEric Joyner 
24561ae650dSJack F Vogel #endif /* _I40E_OSDEP_H_ */
246