xref: /freebsd/sys/dev/liquidio/lio_main.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
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 Cavium, Inc. 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(S) 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 /*
35  *  \file lio_main.h
36  *  \brief Host Driver: This file is included by all host driver source files
37  *  to include common definitions.
38  */
39 
40 #ifndef _LIO_MAIN_H_
41 #define  _LIO_MAIN_H_
42 
43 extern unsigned int    lio_hwlro;
44 
45 #define LIO_CAST64(v)	((long long)(long)(v))
46 
47 #define LIO_DRV_NAME	"lio"
48 
49 /** Swap 8B blocks */
50 static inline void
51 lio_swap_8B_data(uint64_t *data, uint32_t blocks)
52 {
53 
54 	while (blocks) {
55 		*data = htobe64(*data);
56 		blocks--;
57 		data++;
58 	}
59 }
60 
61 /*
62  * \brief unmaps a PCI BAR
63  * @param oct Pointer to Octeon device
64  * @param baridx bar index
65  */
66 static inline void
67 lio_unmap_pci_barx(struct octeon_device *oct, int baridx)
68 {
69 
70 	lio_dev_dbg(oct, "Freeing PCI mapped regions for Bar%d\n", baridx);
71 
72 	if (oct->mem_bus_space[baridx].pci_mem != NULL) {
73 		bus_release_resource(oct->device, SYS_RES_MEMORY,
74 				     PCIR_BAR(baridx * 2),
75 				     oct->mem_bus_space[baridx].pci_mem);
76 		oct->mem_bus_space[baridx].pci_mem = NULL;
77 	}
78 }
79 
80 /*
81  * \brief maps a PCI BAR
82  * @param oct Pointer to Octeon device
83  * @param baridx bar index
84  */
85 static inline int
86 lio_map_pci_barx(struct octeon_device *oct, int baridx)
87 {
88 	int	rid = PCIR_BAR(baridx * 2);
89 
90 	oct->mem_bus_space[baridx].pci_mem =
91 		bus_alloc_resource_any(oct->device, SYS_RES_MEMORY, &rid,
92 				       RF_ACTIVE);
93 
94 	if (oct->mem_bus_space[baridx].pci_mem == NULL) {
95 		lio_dev_err(oct, "Unable to allocate bus resource: memory\n");
96 		return (ENXIO);
97 	}
98 
99 	/* Save bus_space values for READ/WRITE_REG macros */
100 	oct->mem_bus_space[baridx].tag =
101 		rman_get_bustag(oct->mem_bus_space[baridx].pci_mem);
102 	oct->mem_bus_space[baridx].handle =
103 		rman_get_bushandle(oct->mem_bus_space[baridx].pci_mem);
104 
105 	lio_dev_dbg(oct, "BAR%d Tag 0x%llx Handle 0x%llx\n",
106 		    baridx, LIO_CAST64(oct->mem_bus_space[baridx].tag),
107 		    LIO_CAST64(oct->mem_bus_space[baridx].handle));
108 
109 	return (0);
110 }
111 
112 static inline void
113 lio_sleep_cond(struct octeon_device *oct, volatile int *condition)
114 {
115 
116 	while (!(*condition)) {
117 		lio_mdelay(1);
118 		lio_flush_iq(oct, oct->instr_queue[0], 0);
119 		lio_process_ordered_list(oct, 0);
120 	}
121 }
122 
123 int	lio_console_debug_enabled(uint32_t console);
124 
125 #ifndef ROUNDUP4
126 #define ROUNDUP4(val)	(((val) + 3) & 0xfffffffc)
127 #endif
128 
129 #ifndef ROUNDUP8
130 #define ROUNDUP8(val)	(((val) + 7) & 0xfffffff8)
131 #endif
132 
133 #define BIT_ULL(nr)	(1ULL << (nr))
134 
135 void	lio_free_mbuf(struct lio_instr_queue *iq,
136 		      struct lio_mbuf_free_info *finfo);
137 void	lio_free_sgmbuf(struct lio_instr_queue *iq,
138 			struct lio_mbuf_free_info *finfo);
139 
140 #endif	/* _LIO_MAIN_H_ */
141