xref: /freebsd/sys/dev/hptrr/hptrr_os_bsd.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) HighPoint Technologies, Inc.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 #include <dev/hptrr/hptrr_config.h>
31 /* $Id: os_bsd.c,v 1.11 2005/06/03 14:06:38 kdh Exp $
32  *
33  * HighPoint RAID Driver for FreeBSD
34  * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
35  */
36 
37 #include <dev/hptrr/os_bsd.h>
38 
39 /* hardware access */
40 HPT_U8   os_inb  (void *port) { return inb((unsigned)(HPT_UPTR)port); }
41 HPT_U16  os_inw  (void *port) { return inw((unsigned)(HPT_UPTR)port); }
42 HPT_U32  os_inl  (void *port) { return inl((unsigned)(HPT_UPTR)port); }
43 
44 void os_outb (void *port, HPT_U8 value) { outb((unsigned)(HPT_UPTR)port, (value)); }
45 void os_outw (void *port, HPT_U16 value) { outw((unsigned)(HPT_UPTR)port, (value)); }
46 void os_outl (void *port, HPT_U32 value) { outl((unsigned)(HPT_UPTR)port, (value)); }
47 
48 void os_insw (void *port, HPT_U16 *buffer, HPT_U32 count)
49 { insw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
50 
51 void os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count)
52 { outsw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
53 
54 HPT_U32 __dummy_reg = 0;
55 
56 /* PCI configuration space */
57 HPT_U8  os_pci_readb (void *osext, HPT_U8 offset)
58 {
59     return  pci_read_config(((PHBA)osext)->pcidev, offset, 1);
60 }
61 
62 HPT_U16 os_pci_readw (void *osext, HPT_U8 offset)
63 {
64     return  pci_read_config(((PHBA)osext)->pcidev, offset, 2);
65 }
66 
67 HPT_U32 os_pci_readl (void *osext, HPT_U8 offset)
68 {
69     return  pci_read_config(((PHBA)osext)->pcidev, offset, 4);
70 }
71 
72 void os_pci_writeb (void *osext, HPT_U8 offset, HPT_U8 value)
73 {
74     pci_write_config(((PHBA)osext)->pcidev, offset, value, 1);
75 }
76 
77 void os_pci_writew (void *osext, HPT_U8 offset, HPT_U16 value)
78 {
79     pci_write_config(((PHBA)osext)->pcidev, offset, value, 2);
80 }
81 
82 void os_pci_writel (void *osext, HPT_U8 offset, HPT_U32 value)
83 {
84     pci_write_config(((PHBA)osext)->pcidev, offset, value, 4);
85 }
86 
87 void *os_map_pci_bar(
88     void *osext,
89     int index,
90     HPT_U32 offset,
91     HPT_U32 length
92 )
93 {
94 	PHBA hba = (PHBA)osext;
95 
96     hba->pcibar[index].rid = 0x10 + index * 4;
97 
98     if (pci_read_config(hba->pcidev, hba->pcibar[index].rid, 4) & 1)
99     	hba->pcibar[index].type = SYS_RES_IOPORT;
100     else
101     	hba->pcibar[index].type = SYS_RES_MEMORY;
102 
103     hba->pcibar[index].res = bus_alloc_resource_any(hba->pcidev,
104 		hba->pcibar[index].type, &hba->pcibar[index].rid, RF_ACTIVE);
105 
106 	hba->pcibar[index].base = (char *)rman_get_virtual(hba->pcibar[index].res) + offset;
107 	return hba->pcibar[index].base;
108 }
109 
110 void os_unmap_pci_bar(void *osext, void *base)
111 {
112 	PHBA hba = (PHBA)osext;
113 	int index;
114 
115 	for (index=0; index<6; index++) {
116 		if (hba->pcibar[index].base==base) {
117 			bus_release_resource(hba->pcidev, hba->pcibar[index].type,
118 				hba->pcibar[index].rid, hba->pcibar[index].res);
119 			hba->pcibar[index].base = 0;
120 			return;
121 		}
122 	}
123 }
124 
125 void freelist_reserve(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT count)
126 {
127     PVBUS_EXT vbus_ext = osext;
128 
129     if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
130         vbus_ext = ((PHBA)osext)->vbus_ext;
131 
132     list->next = vbus_ext->freelist_head;
133     vbus_ext->freelist_head = list;
134     list->dma = 0;
135     list->size = size;
136     list->head = 0;
137 #if DBG
138     list->reserved_count =
139 #endif
140     list->count = count;
141 }
142 
143 void *freelist_get(struct freelist *list)
144 {
145     void * result;
146     if (list->count) {
147         HPT_ASSERT(list->head);
148         result = list->head;
149         list->head = *(void **)result;
150         list->count--;
151         return result;
152     }
153     return 0;
154 }
155 
156 void freelist_put(struct freelist * list, void *p)
157 {
158     HPT_ASSERT(list->dma==0);
159     list->count++;
160     *(void **)p = list->head;
161     list->head = p;
162 }
163 
164 void freelist_reserve_dma(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT alignment, HPT_UINT count)
165 {
166     PVBUS_EXT vbus_ext = osext;
167 
168     if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
169         vbus_ext = ((PHBA)osext)->vbus_ext;
170 
171     list->next = vbus_ext->freelist_dma_head;
172     vbus_ext->freelist_dma_head = list;
173     list->dma = 1;
174     list->alignment = alignment;
175     list->size = size;
176     list->head = 0;
177 #if DBG
178     list->reserved_count =
179 #endif
180     list->count = count;
181 }
182 
183 void *freelist_get_dma(struct freelist *list, BUS_ADDRESS *busaddr)
184 {
185     void *result;
186     HPT_ASSERT(list->dma);
187     result = freelist_get(list);
188     if (result)
189         *busaddr = *(BUS_ADDRESS *)((void **)result+1);
190     return result;
191 }
192 
193 void freelist_put_dma(struct freelist *list, void *p, BUS_ADDRESS busaddr)
194 {
195     HPT_ASSERT(list->dma);
196     list->count++;
197     *(void **)p = list->head;
198     *(BUS_ADDRESS *)((void **)p+1) = busaddr;
199     list->head = p;
200 }
201 
202 HPT_U32 os_get_stamp(void)
203 {
204     HPT_U32 stamp;
205     do { stamp = random(); } while (stamp==0);
206     return stamp;
207 }
208 
209 void os_stallexec(HPT_U32 microseconds)
210 {
211     DELAY(microseconds);
212 }
213 
214 static void os_timer_for_ldm(void *arg)
215 {
216 	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
217 	ldm_on_timer((PVBUS)vbus_ext->vbus);
218 }
219 
220 void  os_request_timer(void * osext, HPT_U32 interval)
221 {
222 	PVBUS_EXT vbus_ext = osext;
223 
224 	HPT_ASSERT(vbus_ext->ext_type==EXT_TYPE_VBUS);
225 
226 	callout_reset_sbt(&vbus_ext->timer, SBT_1US * interval, 0,
227 	    os_timer_for_ldm, vbus_ext, 0);
228 }
229 
230 HPT_TIME os_query_time(void)
231 {
232 	return ticks * (1000000 / hz);
233 }
234 
235 void os_schedule_task(void *osext, OSM_TASK *task)
236 {
237 	PVBUS_EXT vbus_ext = osext;
238 
239 	HPT_ASSERT(task->next==0);
240 
241 	if (vbus_ext->tasks==0)
242 		vbus_ext->tasks = task;
243 	else {
244 		OSM_TASK *t = vbus_ext->tasks;
245 		while (t->next) t = t->next;
246 		t->next = task;
247 	}
248 
249 	if (vbus_ext->worker.ta_context)
250 		TASK_ENQUEUE(&vbus_ext->worker);
251 }
252 
253 int os_revalidate_device(void *osext, int id)
254 {
255 
256     return 0;
257 }
258 
259 int os_query_remove_device(void *osext, int id)
260 {
261 	return 0;
262 }
263 
264 HPT_U8 os_get_vbus_seq(void *osext)
265 {
266     return ((PVBUS_EXT)osext)->sim->path_id;
267 }
268 
269 int  os_printk(char *fmt, ...)
270 {
271     va_list args;
272     static char buf[512];
273 
274     va_start(args, fmt);
275     vsnprintf(buf, sizeof(buf), fmt, args);
276     va_end(args);
277     return printf("%s: %s\n", driver_name, buf);
278 }
279 
280 #if DBG
281 void os_check_stack(const char *location, int size){}
282 
283 void __os_dbgbreak(const char *file, int line)
284 {
285     printf("*** break at %s:%d ***", file, line);
286     while (1);
287 }
288 
289 int hptrr_dbg_level = 1;
290 #endif
291