1/* SPDX-License-Identifier: BSD-3-Clause */
2/* Copyright(c) 2007-2022 Intel Corporation */
3#ifndef QAT_FREEBSD_H_
4#define QAT_FREEBSD_H_
5
6#include <sys/param.h>
7#include <sys/module.h>
8#include <sys/bus.h>
9#include <sys/param.h>
10#include <sys/malloc.h>
11#include <sys/firmware.h>
12#include <sys/rman.h>
13#include <sys/types.h>
14#include <sys/ctype.h>
15#include <sys/ioccom.h>
16#include <sys/param.h>
17#include <sys/lock.h>
18#include <linux/device.h>
19#include <linux/dma-mapping.h>
20#include <linux/completion.h>
21#include <linux/list.h>
22#include <machine/bus.h>
23#include <machine/bus_dma.h>
24#include <sys/firmware.h>
25#include <asm/uaccess.h>
26#include <linux/math64.h>
27#include <linux/spinlock.h>
28
29#define PCI_VENDOR_ID_INTEL 0x8086
30
31#if !defined(__bool_true_false_are_defined)
32#define __bool_true_false_are_defined 1
33#define false 0
34#define true 1
35#if __STDC_VERSION__ < 199901L && __GNUC__ < 3 && !defined(__INTEL_COMPILER)
36typedef int _Bool;
37#endif
38typedef _Bool bool;
39#endif /* !__bool_true_false_are_defined && !__cplusplus */
40
41#if __STDC_VERSION__ < 199901L && __GNUC__ < 3 && !defined(__INTEL_COMPILER)
42typedef int _Bool;
43#endif
44
45#define pause_ms(wmesg, ms) pause_sbt(wmesg, (ms)*SBT_1MS, 0, C_HARDCLOCK)
46
47/* Function sets the MaxPayload size of a PCI device. */
48int pci_set_max_payload(device_t dev, int payload_size);
49
50device_t pci_find_pf(device_t vf);
51
52MALLOC_DECLARE(M_QAT);
53
54struct msix_entry {
55	struct resource *irq;
56	void *cookie;
57};
58
59struct pci_device_id {
60	uint16_t vendor;
61	uint16_t device;
62};
63
64struct bus_dmamem {
65	bus_dma_tag_t dma_tag;
66	bus_dmamap_t dma_map;
67	void *dma_vaddr;
68	bus_addr_t dma_baddr;
69};
70
71/*
72 * Allocate a mapping.	On success, zero is returned and the 'dma_vaddr'
73 * and 'dma_baddr' fields are populated with the virtual and bus addresses,
74 * respectively, of the mapping.
75 */
76int bus_dma_mem_create(struct bus_dmamem *mem,
77		       bus_dma_tag_t parent,
78		       bus_size_t alignment,
79		       bus_addr_t lowaddr,
80		       bus_size_t len,
81		       int flags);
82
83/*
84 * Release a mapping created by bus_dma_mem_create().
85 */
86void bus_dma_mem_free(struct bus_dmamem *mem);
87
88#define list_for_each_prev_safe(p, n, h)                                       \
89	for (p = (h)->prev, n = (p)->prev; p != (h); p = n, n = (p)->prev)
90
91static inline int
92compat_strtoul(const char *cp, unsigned int base, unsigned long *res)
93{
94	char *end;
95
96	*res = strtoul(cp, &end, base);
97
98	/* skip newline character, if any */
99	if (*end == '\n')
100		end++;
101	if (*cp == 0 || *end != 0)
102		return (-EINVAL);
103	return (0);
104}
105
106static inline int
107compat_strtouint(const char *cp, unsigned int base, unsigned int *res)
108{
109	char *end;
110	unsigned long temp;
111
112	*res = temp = strtoul(cp, &end, base);
113
114	/* skip newline character, if any */
115	if (*end == '\n')
116		end++;
117	if (*cp == 0 || *end != 0)
118		return (-EINVAL);
119	if (temp != (unsigned int)temp)
120		return (-ERANGE);
121	return (0);
122}
123
124static inline int
125compat_strtou8(const char *cp, unsigned int base, unsigned char *res)
126{
127	char *end;
128	unsigned long temp;
129
130	*res = temp = strtoul(cp, &end, base);
131
132	/* skip newline character, if any */
133	if (*end == '\n')
134		end++;
135	if (*cp == 0 || *end != 0)
136		return -EINVAL;
137	if (temp != (unsigned char)temp)
138		return -ERANGE;
139	return 0;
140}
141
142#if __FreeBSD_version >= 1300500
143#undef dev_to_node
144static inline int
145dev_to_node(device_t dev)
146{
147	int numa_domain;
148
149	if (!dev || bus_get_domain(dev, &numa_domain) != 0)
150		return (-1);
151	else
152		return (numa_domain);
153}
154#endif
155#endif
156