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