1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
5 * Copyright (c) 2004-05 Vinod Kashyap.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * AMCC'S 3ware driver for 9000 series storage controllers.
32 *
33 * Author: Vinod Kashyap
34 * Modifications by: Adam Radford
35 */
36
37 #ifndef TW_OSL_INLINE_H
38
39 #define TW_OSL_INLINE_H
40
41 /*
42 * Inline functions shared between OSL and CL, and defined by OSL.
43 */
44
45 #include <dev/twa/tw_osl.h>
46
47 /*
48 * Function name: tw_osl_init_lock
49 * Description: Initializes a lock.
50 *
51 * Input: ctlr_handle -- ptr to controller handle
52 * lock_name -- string indicating name of the lock
53 * Output: lock -- ptr to handle to the initialized lock
54 * Return value: None
55 */
56 #define tw_osl_init_lock(ctlr_handle, lock_name, lock) \
57 mtx_init(lock, lock_name, NULL, MTX_SPIN)
58
59 /*
60 * Function name: tw_osl_destroy_lock
61 * Description: Destroys a previously initialized lock.
62 *
63 * Input: ctlr_handle -- ptr to controller handle
64 * lock -- ptr to handle to the lock to be
65 * destroyed
66 * Output: None
67 * Return value: None
68 */
69 #define tw_osl_destroy_lock(ctlr_handle, lock) \
70 mtx_destroy(lock)
71
72 /*
73 * Function name: tw_osl_get_lock
74 * Description: Acquires the specified lock.
75 *
76 * Input: ctlr_handle -- ptr to controller handle
77 * lock -- ptr to handle to the lock to be
78 * acquired
79 * Output: None
80 * Return value: None
81 */
82 #define tw_osl_get_lock(ctlr_handle, lock) \
83 mtx_lock_spin(lock)
84
85 /*
86 * Function name: tw_osl_free_lock
87 * Description: Frees a previously acquired lock.
88 *
89 * Input: ctlr_handle -- ptr to controller handle
90 * lock -- ptr to handle to the lock to be freed
91 * Output: None
92 * Return value: None
93 */
94 #define tw_osl_free_lock(ctlr_handle, lock) \
95 mtx_unlock_spin(lock)
96
97 #ifdef TW_OSL_DEBUG
98
99 /*
100 * Function name: tw_osl_dbg_printf
101 * Description: Prints passed info (prefixed by ctlr name)to syslog
102 *
103 * Input: ctlr_handle -- controller handle
104 * fmt -- format string for the arguments to follow
105 * ... -- variable number of arguments, to be printed
106 * based on the fmt string
107 * Output: None
108 * Return value: Number of bytes printed
109 */
110 #define tw_osl_dbg_printf(ctlr_handle, fmt, args...) \
111 twa_printf((ctlr_handle->osl_ctlr_ctxt), fmt, ##args)
112
113 #endif /* TW_OSL_DEBUG */
114
115 /*
116 * Function name: tw_osl_notify_event
117 * Description: Prints passed event info (prefixed by ctlr name)
118 * to syslog
119 *
120 * Input: ctlr_handle -- controller handle
121 * event -- ptr to a packet describing the event/error
122 * Output: None
123 * Return value: None
124 */
125 #define tw_osl_notify_event(ctlr_handle, event) \
126 twa_printf((ctlr_handle->osl_ctlr_ctxt), \
127 "%s: (0x%02X: 0x%04X): %s: %s\n", \
128 event->severity_str, \
129 event->event_src, \
130 event->aen_code, \
131 event->parameter_data + \
132 strlen(event->parameter_data) + 1, \
133 event->parameter_data)
134
135 /*
136 * Function name: tw_osl_read_reg
137 * Description: Reads a register on the controller
138 *
139 * Input: ctlr_handle -- controller handle
140 * offset -- offset from Base Address
141 * size -- # of bytes to read
142 * Output: None
143 * Return value: Value read
144 */
145 #define tw_osl_read_reg tw_osl_read_reg_inline
146 static __inline TW_UINT32
tw_osl_read_reg_inline(struct tw_cl_ctlr_handle * ctlr_handle,TW_INT32 offset,TW_INT32 size)147 tw_osl_read_reg_inline(struct tw_cl_ctlr_handle *ctlr_handle,
148 TW_INT32 offset, TW_INT32 size)
149 {
150 bus_space_tag_t bus_tag =
151 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
152 bus_space_handle_t bus_handle =
153 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
154
155 if (size == 4)
156 return((TW_UINT32)bus_space_read_4(bus_tag, bus_handle,
157 offset));
158 else if (size == 2)
159 return((TW_UINT32)bus_space_read_2(bus_tag, bus_handle,
160 offset));
161 else
162 return((TW_UINT32)bus_space_read_1(bus_tag, bus_handle,
163 offset));
164 }
165
166 /*
167 * Function name: tw_osl_write_reg
168 * Description: Writes to a register on the controller
169 *
170 * Input: ctlr_handle -- controller handle
171 * offset -- offset from Base Address
172 * value -- value to write
173 * size -- # of bytes to write
174 * Output: None
175 * Return value: None
176 */
177 #define tw_osl_write_reg tw_osl_write_reg_inline
178 static __inline TW_VOID
tw_osl_write_reg_inline(struct tw_cl_ctlr_handle * ctlr_handle,TW_INT32 offset,TW_INT32 value,TW_INT32 size)179 tw_osl_write_reg_inline(struct tw_cl_ctlr_handle *ctlr_handle,
180 TW_INT32 offset, TW_INT32 value, TW_INT32 size)
181 {
182 bus_space_tag_t bus_tag =
183 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
184 bus_space_handle_t bus_handle =
185 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
186
187 if (size == 4)
188 bus_space_write_4(bus_tag, bus_handle, offset, value);
189 else if (size == 2)
190 bus_space_write_2(bus_tag, bus_handle, offset, (TW_INT16)value);
191 else
192 bus_space_write_1(bus_tag, bus_handle, offset, (TW_INT8)value);
193 }
194
195 #ifdef TW_OSL_PCI_CONFIG_ACCESSIBLE
196
197 /*
198 * Function name: tw_osl_read_pci_config
199 * Description: Reads from the PCI config space.
200 *
201 * Input: sc -- ptr to per ctlr structure
202 * offset -- register offset
203 * size -- # of bytes to be read
204 * Output: None
205 * Return value: Value read
206 */
207 #define tw_osl_read_pci_config(ctlr_handle, offset, size) \
208 pci_read_config( \
209 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_dev, \
210 offset, size)
211
212 /*
213 * Function name: tw_osl_write_pci_config
214 * Description: Writes to the PCI config space.
215 *
216 * Input: sc -- ptr to per ctlr structure
217 * offset -- register offset
218 * value -- value to write
219 * size -- # of bytes to be written
220 * Output: None
221 * Return value: None
222 */
223 #define tw_osl_write_pci_config(ctlr_handle, offset, value, size) \
224 pci_write_config( \
225 ((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_dev, \
226 offset/*PCIR_STATUS*/, value, size)
227
228 #endif /* TW_OSL_PCI_CONFIG_ACCESSIBLE */
229
230 /*
231 * Function name: tw_osl_get_local_time
232 * Description: Gets the local time
233 *
234 * Input: None
235 * Output: None
236 * Return value: local time
237 */
238 #define tw_osl_get_local_time() \
239 (time_second - utc_offset())
240
241 /*
242 * Function name: tw_osl_delay
243 * Description: Spin for the specified time
244 *
245 * Input: usecs -- micro-seconds to spin
246 * Output: None
247 * Return value: None
248 */
249 #define tw_osl_delay(usecs) DELAY(usecs)
250
251 #ifdef TW_OSL_CAN_SLEEP
252
253 /*
254 * Function name: tw_osl_sleep
255 * Description: Sleep for the specified time, or until woken up
256 *
257 * Input: ctlr_handle -- controller handle
258 * sleep_handle -- handle to sleep on
259 * timeout -- time period (in ms) to sleep
260 * Output: None
261 * Return value: 0 -- successfully woken up
262 * EWOULDBLOCK -- time out
263 * ERESTART -- woken up by a signal
264 */
265 #define tw_osl_sleep(ctlr_handle, sleep_handle, timeout) \
266 tsleep((TW_VOID *)sleep_handle, PRIBIO, NULL, timeout)
267
268 /*
269 * Function name: tw_osl_wakeup
270 * Description: Wake up a sleeping process
271 *
272 * Input: ctlr_handle -- controller handle
273 * sleep_handle -- handle of sleeping process to be
274 woken up
275 * Output: None
276 * Return value: None
277 */
278 #define tw_osl_wakeup(ctlr_handle, sleep_handle) \
279 wakeup_one(sleep_handle)
280
281 #endif /* TW_OSL_CAN_SLEEP */
282
283 /* Allows setting breakpoints in the CL code for debugging purposes. */
284 #define tw_osl_breakpoint() breakpoint()
285
286 /* Text name of current function. */
287 #define tw_osl_cur_func() __func__
288
289 /* Copy 'size' bytes from 'src' to 'dest'. */
290 #define tw_osl_memcpy(dest, src, size) bcopy(src, dest, size)
291
292 /* Zero 'size' bytes starting at 'addr'. */
293 #define tw_osl_memzero bzero
294
295 /* Standard sprintf. */
296 #define tw_osl_sprintf sprintf
297
298 /* Copy string 'src' to 'dest'. */
299 #define tw_osl_strcpy strcpy
300
301 /* Return length of string pointed at by 'str'. */
302 #define tw_osl_strlen strlen
303
304 /* Standard vsprintf. */
305 #define tw_osl_vsprintf vsprintf
306
307 #endif /* TW_OSL_INLINE_H */
308