xref: /netbsd-src/sys/external/bsd/ena-com/ena_plat.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright (c) 2015-2017 Amazon.com, Inc. or its affiliates.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef ENA_PLAT_H_
35 #define ENA_PLAT_H_
36 
37 #include <sys/cdefs.h>
38 #if 0
39 __FBSDID("$FreeBSD: head/sys/contrib/ena-com/ena_plat.h 333453 2018-05-10 09:25:51Z mw $");
40 #endif
41 __KERNEL_RCSID(0, "$NetBSD: ena_plat.h,v 1.3 2018/06/16 15:00:35 jdolecek Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 
46 #include <sys/bus.h>
47 #include <sys/condvar.h>
48 #include <sys/endian.h>
49 #include <sys/kernel.h>
50 #include <sys/kthread.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/module.h>
54 #include <sys/proc.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/types.h>
59 #include <sys/bus.h>
60 #include <sys/atomic.h>
61 
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_ether.h>
66 
67 #include <net/bpf.h>
68 
69 #include <net/rss_config.h>
70 
71 #include <netinet/in.h>			/* XXX for struct ip */
72 #include <netinet/in_systm.h>		/* XXX for struct ip */
73 #include <netinet/ip.h>			/* XXX for struct ip */
74 #include <netinet/ip6.h>		/* XXX for struct ip6_hdr */
75 #include <netinet/tcp.h>		/* XXX for struct tcphdr */
76 
77 #include <dev/pci/pcivar.h>
78 #include <dev/pci/pcireg.h>
79 
80 extern struct ena_bus_space ebs;
81 
82 /* Levels */
83 #define ENA_ALERT 	(1 << 0) /* Alerts are providing more error info.     */
84 #define ENA_WARNING 	(1 << 1) /* Driver output is more error sensitive.    */
85 #define ENA_INFO 	(1 << 2) /* Provides additional driver info. 	      */
86 #define ENA_DBG 	(1 << 3) /* Driver output for debugging.	      */
87 /* Detailed info that will be printed with ENA_INFO or ENA_DEBUG flag. 	      */
88 #define ENA_TXPTH 	(1 << 4) /* Allows TX path tracing. 		      */
89 #define ENA_RXPTH 	(1 << 5) /* Allows RX path tracing.		      */
90 #define ENA_RSC 	(1 << 6) /* Goes with TXPTH or RXPTH, free/alloc res. */
91 #define ENA_IOQ 	(1 << 7) /* Detailed info about IO queues. 	      */
92 #define ENA_ADMQ	(1 << 8) /* Detailed info about admin queue. 	      */
93 
94 extern int ena_log_level;
95 
96 #define ena_trace_raw(level, fmt, args...)			\
97 	do {							\
98 		if (((level) & ena_log_level) != (level))	\
99 			break;					\
100 		printf(fmt, ##args);				\
101 	} while (0)
102 
103 #define ena_trace(level, fmt, args...)				\
104 	ena_trace_raw(level, "%s() [LID:%d]: "			\
105 	    fmt " \n", __func__, curlwp->l_lid, ##args)
106 
107 
108 #define ena_trc_dbg(format, arg...) 	ena_trace(ENA_DBG, format, ##arg)
109 #define ena_trc_info(format, arg...) 	ena_trace(ENA_INFO, format, ##arg)
110 #define ena_trc_warn(format, arg...) 	ena_trace(ENA_WARNING, format, ##arg)
111 #define ena_trc_err(format, arg...) 	ena_trace(ENA_ALERT, format, ##arg)
112 
113 #define unlikely(x)	__predict_false(x)
114 #define likely(x)  	__predict_true(x)
115 
116 #define __iomem
117 #define ____cacheline_aligned __aligned(CACHE_LINE_SIZE)
118 
119 #define MAX_ERRNO 4095
120 #define IS_ERR_VALUE(x) unlikely((x) <= (unsigned long)MAX_ERRNO)
121 
122 #define ENA_ASSERT(cond, format, arg...)				\
123 	do {								\
124 		if (unlikely(!(cond))) {				\
125 			ena_trc_err(					\
126 				"Assert failed on %s:%s:%d:" format,	\
127 				__FILE__, __func__, __LINE__, ##arg);	\
128 		}							\
129 	} while (0)
130 
131 #define ENA_WARN(cond, format, arg...)					\
132 	do {								\
133 		if (unlikely((cond))) {					\
134 			ena_trc_warn(format, ##arg);			\
135 		}							\
136 	} while (0)
137 
138 static inline long IS_ERR(const void *ptr)
139 {
140 	return IS_ERR_VALUE((unsigned long)ptr);
141 }
142 
143 static inline void *ERR_PTR(long error)
144 {
145 	return (void *)error;
146 }
147 
148 static inline long PTR_ERR(const void *ptr)
149 {
150 	return (long) ptr;
151 }
152 
153 #define GENMASK(h, l)		(((1U << ((h) - (l) + 1)) - 1) << (l))
154 #define GENMASK_ULL(h, l)	(((~0ULL) << (l)) & (~0ULL >> (64 - 1 - (h))))
155 #define BIT(x)			(1UL << (x))
156 
157 #define ENA_ABORT() 		BUG()
158 #define BUG() 			panic("ENA BUG")
159 
160 #define SZ_256			(256)
161 #define SZ_4K			(4096)
162 
163 #define	ENA_COM_OK		0
164 #define ENA_COM_FAULT		EFAULT
165 #define	ENA_COM_INVAL		EINVAL
166 #define ENA_COM_NO_MEM		ENOMEM
167 #define	ENA_COM_NO_SPACE	ENOSPC
168 #define ENA_COM_TRY_AGAIN	-1
169 #define	ENA_COM_UNSUPPORTED	EOPNOTSUPP
170 #define	ENA_COM_NO_DEVICE	ENODEV
171 #define	ENA_COM_PERMISSION	EPERM
172 #define ENA_COM_TIMER_EXPIRED	ETIMEDOUT
173 
174 #define ENA_MSLEEP(x) 		kpause("enaw", false, mstohz(x), NULL)
175 #define ENA_UDELAY(x) 		DELAY(x)
176 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \
177 	mstohz(timeout_us * (1000 / 100))	/* XXX assumes 100 ms sleep */
178 #define ENA_TIME_EXPIRE(timeout)  ((timeout)-- <= 0)
179 #define ENA_MIGHT_SLEEP()
180 
181 #define min_t(type, _x, _y) ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
182 #define max_t(type, _x, _y) ((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
183 
184 #define ENA_MIN32(x,y) 	MIN(x, y)
185 #define ENA_MIN16(x,y)	MIN(x, y)
186 #define ENA_MIN8(x,y)	MIN(x, y)
187 
188 #define ENA_MAX32(x,y) 	MAX(x, y)
189 #define ENA_MAX16(x,y) 	MAX(x, y)
190 #define ENA_MAX8(x,y) 	MAX(x, y)
191 
192 /* Spinlock related methods */
193 #define ena_spinlock_t 	kmutex_t
194 #define ENA_SPINLOCK_INIT(spinlock)				\
195 	mutex_init(&(spinlock), MUTEX_DEFAULT, IPL_NET)
196 #define ENA_SPINLOCK_DESTROY(spinlock)				\
197 	do {							\
198 		mutex_destroy(&(spinlock));			\
199 	} while (0)
200 #define ENA_SPINLOCK_LOCK(spinlock, flags)			\
201 	do {							\
202 		(void)(flags);					\
203 		mutex_enter(&(spinlock));			\
204 	} while (0)
205 #define ENA_SPINLOCK_UNLOCK(spinlock, flags)			\
206 	do {							\
207 		(void)(flags);					\
208 		mutex_exit(&(spinlock));			\
209 	} while (0)
210 
211 
212 /* Wait queue related methods */
213 #define ena_wait_event_t struct { kcondvar_t wq; kmutex_t mtx; }
214 #define ENA_WAIT_EVENT_INIT(waitqueue)					\
215 	do {								\
216 		cv_init(&((waitqueue).wq), "enacv");			\
217 		mutex_init(&((waitqueue).mtx), MUTEX_DEFAULT, IPL_NET);	\
218 	} while (0)
219 #define ENA_WAIT_EVENT_DESTROY(waitqueue)				\
220 	do {								\
221 		cv_destroy(&((waitqueue).wq));				\
222 		mutex_destroy(&((waitqueue).mtx));			\
223 	} while (0)
224 #define ENA_WAIT_EVENT_CLEAR(waitqueue)					\
225 	cv_init(&((waitqueue).wq), "enacv")
226 #define ENA_WAIT_EVENT_WAIT(waitqueue, timeout_us)			\
227 	do {								\
228 		mutex_enter(&((waitqueue).mtx));			\
229 		cv_timedwait(&((waitqueue).wq), &((waitqueue).mtx),	\
230 		    timeout_us * hz / 1000 / 1000 );			\
231 		mutex_exit(&((waitqueue).mtx));				\
232 	} while (0)
233 #define ENA_WAIT_EVENT_SIGNAL(waitqueue)		\
234 	do {						\
235 		mutex_enter(&((waitqueue).mtx));	\
236 		cv_broadcast(&((waitqueue).wq));	\
237 		mutex_exit(&((waitqueue).mtx));		\
238 	} while (0)
239 
240 #define dma_addr_t 	bus_addr_t
241 #define u8 		uint8_t
242 #define u16 		uint16_t
243 #define u32 		uint32_t
244 #define u64 		uint64_t
245 
246 typedef struct {
247 	paddr_t                 paddr;
248 	void                    *vaddr;
249         bus_dma_tag_t           tag;
250 	bus_dmamap_t            map;
251         bus_dma_segment_t       seg;
252 	int                     nseg;
253 } ena_mem_handle_t;
254 
255 struct ena_bus {
256 	bus_space_handle_t 	reg_bar_h;
257 	bus_space_tag_t 	reg_bar_t;
258 	bus_space_handle_t	mem_bar_h;
259 	bus_space_tag_t 	mem_bar_t;
260 };
261 
262 typedef uint32_t ena_atomic32_t;
263 
264 void	ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg,
265     int error);
266 int	ena_dma_alloc(device_t dmadev, bus_size_t size, ena_mem_handle_t *dma,
267     int mapflags);
268 
269 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size)				\
270 	do {								\
271 		int count, i;						\
272 		volatile uint64_t *to = (volatile uint64_t *)(dst);	\
273 		const uint64_t *from = (const uint64_t *)(src);		\
274 		count = (size) / 8;					\
275 									\
276 		for (i = 0; i < count; i++, from++, to++)		\
277 			*to = *from;					\
278 	} while (0)
279 
280 #define ENA_MEM_ALLOC(dmadev, size) malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO)
281 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) (virt = NULL)
282 #define ENA_MEM_FREE(dmadev, ptr) free(ptr, M_DEVBUF)
283 #define ENA_MEM_ALLOC_COHERENT_NODE(dmadev, size, virt, phys, handle, node, \
284     dev_node)								\
285 	do {								\
286 		((virt) = NULL);					\
287 		(void)(dev_node);					\
288 	} while (0)
289 
290 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, dma)		\
291 	do {								\
292 		ena_dma_alloc((dmadev), (size), &(dma), 0);		\
293 		(virt) = (void *)(dma).vaddr;				\
294 		(phys) = (dma).paddr;					\
295 	} while (0)
296 
297 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, dma)		\
298 	do {								\
299 		(void)size;						\
300 		bus_dmamap_unload((dma).tag, (dma).map);		\
301 		bus_dmamem_free((dma).tag, &(dma).seg, (dma).nseg);	\
302 		bus_dma_tag_destroy((dma).tag);	/* XXX remove */	\
303 		(dma).tag = NULL;					\
304 		(virt) = NULL;						\
305 	} while (0)
306 
307 /* Register R/W methods */
308 #define ENA_REG_WRITE32(bus, value, offset)				\
309 	bus_space_write_4(						\
310 			  ((struct ena_bus*)bus)->reg_bar_t,		\
311 			  ((struct ena_bus*)bus)->reg_bar_h,		\
312 			  (bus_size_t)(offset), (value))
313 
314 #define ENA_REG_READ32(bus, offset)					\
315 	bus_space_read_4(						\
316 			 ((struct ena_bus*)bus)->reg_bar_t,		\
317 			 ((struct ena_bus*)bus)->reg_bar_h,		\
318 			 (bus_size_t)(offset))
319 
320 #define ENA_DB_SYNC(mem_handle)	bus_dmamap_sync((mem_handle)->tag,	\
321 	(mem_handle)->map, 0, (mem_handle)->map->dm_mapsize,		\
322 	BUS_DMASYNC_PREREAD)
323 
324 #define time_after(a,b)	((long)((unsigned long)(b) - (unsigned long)(a)) < 0)
325 
326 #define VLAN_HLEN 	sizeof(struct ether_vlan_header)
327 #define CSUM_OFFLOAD 	(M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)
328 
329 #if defined(__i386__) || defined(__amd64__)
330 static __inline
331 void prefetch(void *x)
332 {
333 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
334 }
335 #else
336 #define prefetch(x)
337 #endif
338 
339 /* DMA buffers access */
340 #define	dma_unmap_addr(p, name)			((p)->dma->name)
341 #define	dma_unmap_addr_set(p, name, v)		(((p)->dma->name) = (v))
342 #define	dma_unmap_len(p, name)			((p)->name)
343 #define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
344 
345 #define memcpy_toio memcpy
346 
347 #define ATOMIC32_INC(I32_PTR)		atomic_inc_32(I32_PTR)
348 #define ATOMIC32_DEC(I32_PTR) 		atomic_dec_32(I32_PTR)
349 #define ATOMIC32_READ(I32_PTR) 		atomic_cas_32(I32_PTR, 0, 0)
350 #define ATOMIC32_SET(I32_PTR, VAL) 	atomic_swap_32(I32_PTR, VAL)
351 
352 #define	barrier() __asm__ __volatile__("": : :"memory")
353 #define	ACCESS_ONCE(x) (*(volatile __typeof(x) *)&(x))
354 #define READ_ONCE(x)  ({			\
355 			__typeof(x) __var;	\
356 			barrier();		\
357 			__var = ACCESS_ONCE(x);	\
358 			barrier();		\
359 			__var;			\
360 		})
361 
362 #include "ena_defs/ena_includes.h"
363 
364 #define	rmb()		membar_enter()
365 #define	wmb()		membar_exit()
366 #define	mb()		membar_sync()
367 
368 #endif /* ENA_PLAT_H_ */
369