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