1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 *	$FreeBSD$
30 */
31
32/*
33 * AMCC'S 3ware driver for 9000 series storage controllers.
34 *
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 */
38
39#ifndef TW_OSL_INLINE_H
40
41#define TW_OSL_INLINE_H
42
43/*
44 * Inline functions shared between OSL and CL, and defined by OSL.
45 */
46
47#include <dev/twa/tw_osl.h>
48
49/*
50 * Function name:	tw_osl_init_lock
51 * Description:		Initializes a lock.
52 *
53 * Input:		ctlr_handle	-- ptr to controller handle
54 *			lock_name	-- string indicating name of the lock
55 * Output:		lock		-- ptr to handle to the initialized lock
56 * Return value:	None
57 */
58#define tw_osl_init_lock(ctlr_handle, lock_name, lock)	\
59	mtx_init(lock, lock_name, NULL, MTX_SPIN)
60
61/*
62 * Function name:	tw_osl_destroy_lock
63 * Description:		Destroys a previously initialized lock.
64 *
65 * Input:		ctlr_handle	-- ptr to controller handle
66 *			lock		-- ptr to handle to the lock to be
67 *						destroyed
68 * Output:		None
69 * Return value:	None
70 */
71#define tw_osl_destroy_lock(ctlr_handle, lock)	\
72	mtx_destroy(lock)
73
74/*
75 * Function name:	tw_osl_get_lock
76 * Description:		Acquires the specified lock.
77 *
78 * Input:		ctlr_handle	-- ptr to controller handle
79 *			lock		-- ptr to handle to the lock to be
80 *						acquired
81 * Output:		None
82 * Return value:	None
83 */
84#define tw_osl_get_lock(ctlr_handle, lock)	\
85	mtx_lock_spin(lock)
86
87/*
88 * Function name:	tw_osl_free_lock
89 * Description:		Frees a previously acquired lock.
90 *
91 * Input:		ctlr_handle	-- ptr to controller handle
92 *			lock		-- ptr to handle to the lock to be freed
93 * Output:		None
94 * Return value:	None
95 */
96#define tw_osl_free_lock(ctlr_handle, lock)	\
97	mtx_unlock_spin(lock)
98
99#ifdef TW_OSL_DEBUG
100
101/*
102 * Function name:	tw_osl_dbg_printf
103 * Description:		Prints passed info (prefixed by ctlr name)to syslog
104 *
105 * Input:		ctlr_handle -- controller handle
106 *			fmt -- format string for the arguments to follow
107 *			... -- variable number of arguments, to be printed
108 *				based on the fmt string
109 * Output:		None
110 * Return value:	Number of bytes printed
111 */
112#define tw_osl_dbg_printf(ctlr_handle, fmt, args...)			\
113	twa_printf((ctlr_handle->osl_ctlr_ctxt), fmt, ##args)
114
115#endif /* TW_OSL_DEBUG */
116
117/*
118 * Function name:	tw_osl_notify_event
119 * Description:		Prints passed event info (prefixed by ctlr name)
120 *			to syslog
121 *
122 * Input:		ctlr_handle -- controller handle
123 *			event -- ptr to a packet describing the event/error
124 * Output:		None
125 * Return value:	None
126 */
127#define tw_osl_notify_event(ctlr_handle, event)				\
128	twa_printf((ctlr_handle->osl_ctlr_ctxt),			\
129		"%s: (0x%02X: 0x%04X): %s: %s\n",			\
130		event->severity_str,					\
131		event->event_src,					\
132		event->aen_code,					\
133		event->parameter_data +					\
134			strlen(event->parameter_data) + 1,		\
135		event->parameter_data)
136
137/*
138 * Function name:	tw_osl_read_reg
139 * Description:		Reads a register on the controller
140 *
141 * Input:		ctlr_handle -- controller handle
142 *			offset -- offset from Base Address
143 *			size -- # of bytes to read
144 * Output:		None
145 * Return value:	Value read
146 */
147#define tw_osl_read_reg		tw_osl_read_reg_inline
148static __inline TW_UINT32
149tw_osl_read_reg_inline(struct tw_cl_ctlr_handle *ctlr_handle,
150	TW_INT32 offset, TW_INT32 size)
151{
152	bus_space_tag_t		bus_tag =
153		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
154	bus_space_handle_t	bus_handle =
155		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
156
157	if (size == 4)
158		return((TW_UINT32)bus_space_read_4(bus_tag, bus_handle,
159			offset));
160	else if (size == 2)
161		return((TW_UINT32)bus_space_read_2(bus_tag, bus_handle,
162			offset));
163	else
164		return((TW_UINT32)bus_space_read_1(bus_tag, bus_handle,
165			offset));
166}
167
168/*
169 * Function name:	tw_osl_write_reg
170 * Description:		Writes to a register on the controller
171 *
172 * Input:		ctlr_handle -- controller handle
173 *			offset -- offset from Base Address
174 *			value -- value to write
175 *			size -- # of bytes to write
176 * Output:		None
177 * Return value:	None
178 */
179#define tw_osl_write_reg	tw_osl_write_reg_inline
180static __inline TW_VOID
181tw_osl_write_reg_inline(struct tw_cl_ctlr_handle *ctlr_handle,
182	TW_INT32 offset, TW_INT32 value, TW_INT32 size)
183{
184	bus_space_tag_t		bus_tag =
185		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_tag;
186	bus_space_handle_t	bus_handle =
187		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_handle;
188
189	if (size == 4)
190		bus_space_write_4(bus_tag, bus_handle, offset, value);
191	else if (size == 2)
192		bus_space_write_2(bus_tag, bus_handle, offset, (TW_INT16)value);
193	else
194		bus_space_write_1(bus_tag, bus_handle, offset, (TW_INT8)value);
195}
196
197#ifdef TW_OSL_PCI_CONFIG_ACCESSIBLE
198
199/*
200 * Function name:	tw_osl_read_pci_config
201 * Description:		Reads from the PCI config space.
202 *
203 * Input:		sc	-- ptr to per ctlr structure
204 *			offset	-- register offset
205 *			size	-- # of bytes to be read
206 * Output:		None
207 * Return value:	Value read
208 */
209#define tw_osl_read_pci_config(ctlr_handle, offset, size)		\
210	pci_read_config(						\
211		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_dev, \
212		offset, size)
213
214/*
215 * Function name:	tw_osl_write_pci_config
216 * Description:		Writes to the PCI config space.
217 *
218 * Input:		sc	-- ptr to per ctlr structure
219 *			offset	-- register offset
220 *			value	-- value to write
221 *			size	-- # of bytes to be written
222 * Output:		None
223 * Return value:	None
224 */
225#define tw_osl_write_pci_config(ctlr_handle, offset, value, size)	\
226	pci_write_config(						\
227		((struct twa_softc *)(ctlr_handle->osl_ctlr_ctxt))->bus_dev, \
228		offset/*PCIR_STATUS*/, value, size)
229
230#endif /* TW_OSL_PCI_CONFIG_ACCESSIBLE */
231
232/*
233 * Function name:	tw_osl_get_local_time
234 * Description:		Gets the local time
235 *
236 * Input:		None
237 * Output:		None
238 * Return value:	local time
239 */
240#define tw_osl_get_local_time()						\
241	(time_second - utc_offset())
242
243/*
244 * Function name:	tw_osl_delay
245 * Description:		Spin for the specified time
246 *
247 * Input:		usecs -- micro-seconds to spin
248 * Output:		None
249 * Return value:	None
250 */
251#define tw_osl_delay(usecs)	DELAY(usecs)
252
253#ifdef TW_OSL_CAN_SLEEP
254
255/*
256 * Function name:	tw_osl_sleep
257 * Description:		Sleep for the specified time, or until woken up
258 *
259 * Input:		ctlr_handle -- controller handle
260 *			sleep_handle -- handle to sleep on
261 *			timeout -- time period (in ms) to sleep
262 * Output:		None
263 * Return value:	0 -- successfully woken up
264 *			EWOULDBLOCK -- time out
265 *			ERESTART -- woken up by a signal
266 */
267#define tw_osl_sleep(ctlr_handle, sleep_handle, timeout)		\
268	tsleep((TW_VOID *)sleep_handle, PRIBIO, NULL, timeout)
269
270/*
271 * Function name:	tw_osl_wakeup
272 * Description:		Wake up a sleeping process
273 *
274 * Input:		ctlr_handle -- controller handle
275 *			sleep_handle -- handle of sleeping process to be
276					woken up
277 * Output:		None
278 * Return value:	None
279 */
280#define tw_osl_wakeup(ctlr_handle, sleep_handle)			\
281	wakeup_one(sleep_handle)
282
283#endif /* TW_OSL_CAN_SLEEP */
284
285/* Allows setting breakpoints in the CL code for debugging purposes. */
286#define tw_osl_breakpoint()		breakpoint()
287
288/* Text name of current function. */
289#define tw_osl_cur_func()		__func__
290
291/* Copy 'size' bytes from 'src' to 'dest'. */
292#define tw_osl_memcpy(dest, src, size)	bcopy(src, dest, size)
293
294/* Zero 'size' bytes starting at 'addr'. */
295#define tw_osl_memzero			bzero
296
297/* Standard sprintf. */
298#define tw_osl_sprintf			sprintf
299
300/* Copy string 'src' to 'dest'. */
301#define tw_osl_strcpy			strcpy
302
303/* Return length of string pointed at by 'str'. */
304#define tw_osl_strlen			strlen
305
306/* Standard vsprintf. */
307#define tw_osl_vsprintf			vsprintf
308
309#endif /* TW_OSL_INLINE_H */
310