hptnr_os_bsd.c revision 270810
1/* $Id: os_bsd.c,v 1.13 2010/05/11 03:12:11 lcn Exp $ */
2/*-
3 * HighPoint RAID Driver for FreeBSD
4 * Copyright (C) 2005-2011 HighPoint Technologies, Inc. All Rights Reserved.
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: stable/10/sys/dev/hptnr/hptnr_os_bsd.c 270810 2014-08-29 13:06:30Z delphij $
29 */
30
31#include <dev/hptnr/hptnr_config.h>
32#include <dev/hptnr/os_bsd.h>
33
34BUS_ADDRESS get_dmapool_phy_addr(void *osext, void * dmapool_virt_addr);
35
36/* hardware access */
37HPT_U8   os_inb  (void *port) { return inb((unsigned)(HPT_UPTR)port); }
38HPT_U16  os_inw  (void *port) { return inw((unsigned)(HPT_UPTR)port); }
39HPT_U32  os_inl  (void *port) { return inl((unsigned)(HPT_UPTR)port); }
40
41void os_outb (void *port, HPT_U8 value) { outb((unsigned)(HPT_UPTR)port, (value)); }
42void os_outw (void *port, HPT_U16 value) { outw((unsigned)(HPT_UPTR)port, (value)); }
43void os_outl (void *port, HPT_U32 value) { outl((unsigned)(HPT_UPTR)port, (value)); }
44
45void os_insw (void *port, HPT_U16 *buffer, HPT_U32 count)
46{ insw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
47
48void os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count)
49{ outsw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
50
51HPT_U32 __dummy_reg = 0;
52
53/* PCI configuration space */
54HPT_U8  os_pci_readb (void *osext, HPT_U8 offset)
55{
56    return  pci_read_config(((PHBA)osext)->pcidev, offset, 1);
57}
58
59HPT_U16 os_pci_readw (void *osext, HPT_U8 offset)
60{
61    return  pci_read_config(((PHBA)osext)->pcidev, offset, 2);
62}
63
64HPT_U32 os_pci_readl (void *osext, HPT_U8 offset)
65{
66    return  pci_read_config(((PHBA)osext)->pcidev, offset, 4);
67}
68
69void os_pci_writeb (void *osext, HPT_U8 offset, HPT_U8 value)
70{
71    pci_write_config(((PHBA)osext)->pcidev, offset, value, 1);
72}
73
74void os_pci_writew (void *osext, HPT_U8 offset, HPT_U16 value)
75{
76    pci_write_config(((PHBA)osext)->pcidev, offset, value, 2);
77}
78
79void os_pci_writel (void *osext, HPT_U8 offset, HPT_U32 value)
80{
81    pci_write_config(((PHBA)osext)->pcidev, offset, value, 4);
82}
83
84BUS_ADDRESS get_dmapool_phy_addr(void *osext, void * dmapool_virt_addr)
85{
86	return (BUS_ADDRESS)vtophys(dmapool_virt_addr);
87}
88
89#if __FreeBSD_version < 500043
90HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
91{
92	HPT_U32 v;
93	pcicfgregs pciref;
94
95	pciref.bus  = bus;
96	pciref.slot = dev;
97	pciref.func = func;
98
99	v = pci_cfgread(&pciref, reg, 4);
100	return v;
101}/* PCI space access */
102#else
103HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg)
104{
105	return (HPT_U32)pci_cfgregread(bus, dev, func, reg, 4);;
106}/* PCI space access */
107#endif
108
109void *os_map_pci_bar(
110    void *osext,
111    int index,
112    HPT_U32 offset,
113    HPT_U32 length
114)
115{
116	PHBA hba = (PHBA)osext;
117	HPT_U32 base;
118
119	hba->pcibar[index].rid = 0x10 + index * 4;
120	base = pci_read_config(hba->pcidev, hba->pcibar[index].rid, 4);
121
122	if (base & 1) {
123		hba->pcibar[index].type = SYS_RES_IOPORT;
124		hba->pcibar[index].res = bus_alloc_resource(hba->pcidev,
125			hba->pcibar[index].type, &hba->pcibar[index].rid, 0, ~0, length, RF_ACTIVE);
126		hba->pcibar[index].base = (void *)(unsigned long)(base & ~0x1);
127	} else {
128		hba->pcibar[index].type = SYS_RES_MEMORY;
129		hba->pcibar[index].res = bus_alloc_resource(hba->pcidev,
130			hba->pcibar[index].type, &hba->pcibar[index].rid, 0, ~0, length, RF_ACTIVE);
131		hba->pcibar[index].base = (char *)rman_get_virtual(hba->pcibar[index].res) + offset;
132	}
133
134	return hba->pcibar[index].base;
135}
136
137void os_unmap_pci_bar(void *osext, void *base)
138{
139	PHBA hba = (PHBA)osext;
140	int index;
141
142	for (index=0; index<6; index++) {
143		if (hba->pcibar[index].base==base) {
144			bus_release_resource(hba->pcidev, hba->pcibar[index].type,
145				hba->pcibar[index].rid, hba->pcibar[index].res);
146			hba->pcibar[index].base = 0;
147			return;
148		}
149	}
150}
151
152void freelist_reserve(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT count)
153{
154    PVBUS_EXT vbus_ext = osext;
155
156    if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
157        vbus_ext = ((PHBA)osext)->vbus_ext;
158
159    list->next = vbus_ext->freelist_head;
160    vbus_ext->freelist_head = list;
161    list->dma = 0;
162    list->size = size;
163    list->head = 0;
164#if DBG
165    list->reserved_count =
166#endif
167    list->count = count;
168}
169
170void *freelist_get(struct freelist *list)
171{
172    void * result;
173    if (list->count) {
174        HPT_ASSERT(list->head);
175        result = list->head;
176        list->head = *(void **)result;
177        list->count--;
178        return result;
179    }
180    return 0;
181}
182
183void freelist_put(struct freelist * list, void *p)
184{
185    HPT_ASSERT(list->dma==0);
186    list->count++;
187    *(void **)p = list->head;
188    list->head = p;
189}
190
191void freelist_reserve_dma(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT alignment, HPT_UINT count)
192{
193    PVBUS_EXT vbus_ext = osext;
194
195    if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
196        vbus_ext = ((PHBA)osext)->vbus_ext;
197
198    list->next = vbus_ext->freelist_dma_head;
199    vbus_ext->freelist_dma_head = list;
200    list->dma = 1;
201    list->alignment = alignment;
202    list->size = size;
203    list->head = 0;
204#if DBG
205    list->reserved_count =
206#endif
207    list->count = count;
208}
209
210void *freelist_get_dma(struct freelist *list, BUS_ADDRESS *busaddr)
211{
212    void *result;
213    HPT_ASSERT(list->dma);
214    result = freelist_get(list);
215    if (result)
216        *busaddr = *(BUS_ADDRESS *)((void **)result+1);
217    return result;
218}
219
220void freelist_put_dma(struct freelist *list, void *p, BUS_ADDRESS busaddr)
221{
222    HPT_ASSERT(list->dma);
223    list->count++;
224    *(void **)p = list->head;
225    *(BUS_ADDRESS *)((void **)p+1) = busaddr;
226    list->head = p;
227}
228
229HPT_U32 os_get_stamp(void)
230{
231    HPT_U32 stamp;
232    do { stamp = random(); } while (stamp==0);
233    return stamp;
234}
235
236void os_stallexec(HPT_U32 microseconds)
237{
238    DELAY(microseconds);
239}
240
241static void os_timer_for_ldm(void *arg)
242{
243	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
244	ldm_on_timer((PVBUS)vbus_ext->vbus);
245}
246
247void  os_request_timer(void * osext, HPT_U32 interval)
248{
249	PVBUS_EXT vbus_ext = osext;
250
251	HPT_ASSERT(vbus_ext->ext_type==EXT_TYPE_VBUS);
252
253	untimeout(os_timer_for_ldm, vbus_ext, vbus_ext->timer);
254	vbus_ext->timer = timeout(os_timer_for_ldm, vbus_ext, interval * hz / 1000000);
255}
256
257HPT_TIME os_query_time(void)
258{
259	return ticks * (1000000 / hz);
260}
261
262void os_schedule_task(void *osext, OSM_TASK *task)
263{
264	PVBUS_EXT vbus_ext = osext;
265
266	HPT_ASSERT(task->next==0);
267
268	if (vbus_ext->tasks==0)
269		vbus_ext->tasks = task;
270	else {
271		OSM_TASK *t = vbus_ext->tasks;
272		while (t->next) t = t->next;
273		t->next = task;
274	}
275
276	if (vbus_ext->worker.ta_context)
277		TASK_ENQUEUE(&vbus_ext->worker);
278}
279
280int os_revalidate_device(void *osext, int id)
281{
282
283    return 0;
284}
285
286int os_query_remove_device(void *osext, int id)
287{
288    return 0;
289}
290
291HPT_U8 os_get_vbus_seq(void *osext)
292{
293    return ((PVBUS_EXT)osext)->sim->path_id;
294}
295
296int  os_printk(char *fmt, ...)
297{
298    va_list args;
299    static char buf[512];
300
301    va_start(args, fmt);
302    vsnprintf(buf, sizeof(buf), fmt, args);
303    va_end(args);
304    return printf("%s: %s\n", driver_name, buf);
305}
306
307#if DBG
308void os_check_stack(const char *location, int size){}
309
310void __os_dbgbreak(const char *file, int line)
311{
312    printf("*** break at %s:%d ***", file, line);
313    while (1);
314}
315
316int hpt_dbg_level = 1;
317#endif
318