hptrr_os_bsd.c revision 176031
1316722Sdelphij/*
2275970Scy * Copyright (c) HighPoint Technologies, Inc.
3275970Scy * All rights reserved.
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6316722Sdelphij * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy *
14275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17275970Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24275970Scy * SUCH DAMAGE.
25275970Scy *
26275970Scy * $FreeBSD: head/sys/dev/hptrr/hptrr_os_bsd.c 176031 2008-02-06 05:33:17Z scottl $
27275970Scy */
28275970Scy#include <dev/hptrr/hptrr_config.h>
29275970Scy/* $Id: os_bsd.c,v 1.11 2005/06/03 14:06:38 kdh Exp $
30275970Scy *
31275970Scy * HighPoint RAID Driver for FreeBSD
32275970Scy * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
33275970Scy */
34275970Scy
35275970Scy#include <dev/hptrr/os_bsd.h>
36275970Scy
37275970Scy/* hardware access */
38275970ScyHPT_U8   os_inb  (void *port) { return inb((unsigned)(HPT_UPTR)port); }
39275970ScyHPT_U16  os_inw  (void *port) { return inw((unsigned)(HPT_UPTR)port); }
40275970ScyHPT_U32  os_inl  (void *port) { return inl((unsigned)(HPT_UPTR)port); }
41275970Scy
42275970Scyvoid os_outb (void *port, HPT_U8 value) { outb((unsigned)(HPT_UPTR)port, (value)); }
43275970Scyvoid os_outw (void *port, HPT_U16 value) { outw((unsigned)(HPT_UPTR)port, (value)); }
44275970Scyvoid os_outl (void *port, HPT_U32 value) { outl((unsigned)(HPT_UPTR)port, (value)); }
45275970Scy
46275970Scyvoid os_insw (void *port, HPT_U16 *buffer, HPT_U32 count)
47275970Scy{ insw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
48275970Scy
49275970Scyvoid os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count)
50275970Scy{ outsw((unsigned)(HPT_UPTR)port, (void *)buffer, count); }
51275970Scy
52275970ScyHPT_U32 __dummy_reg = 0;
53275970Scy
54275970Scy/* PCI configuration space */
55275970ScyHPT_U8  os_pci_readb (void *osext, HPT_U8 offset)
56275970Scy{
57275970Scy    return  pci_read_config(((PHBA)osext)->pcidev, offset, 1);
58275970Scy}
59275970Scy
60275970ScyHPT_U16 os_pci_readw (void *osext, HPT_U8 offset)
61275970Scy{
62275970Scy    return  pci_read_config(((PHBA)osext)->pcidev, offset, 2);
63275970Scy}
64275970Scy
65275970ScyHPT_U32 os_pci_readl (void *osext, HPT_U8 offset)
66275970Scy{
67275970Scy    return  pci_read_config(((PHBA)osext)->pcidev, offset, 4);
68275970Scy}
69275970Scy
70275970Scyvoid os_pci_writeb (void *osext, HPT_U8 offset, HPT_U8 value)
71275970Scy{
72275970Scy    pci_write_config(((PHBA)osext)->pcidev, offset, value, 1);
73275970Scy}
74275970Scy
75275970Scyvoid os_pci_writew (void *osext, HPT_U8 offset, HPT_U16 value)
76275970Scy{
77275970Scy    pci_write_config(((PHBA)osext)->pcidev, offset, value, 2);
78275970Scy}
79275970Scy
80275970Scyvoid os_pci_writel (void *osext, HPT_U8 offset, HPT_U32 value)
81275970Scy{
82275970Scy    pci_write_config(((PHBA)osext)->pcidev, offset, value, 4);
83275970Scy}
84275970Scy
85275970Scyvoid *os_map_pci_bar(
86275970Scy    void *osext,
87275970Scy    int index,
88275970Scy    HPT_U32 offset,
89275970Scy    HPT_U32 length
90275970Scy)
91275970Scy{
92275970Scy	PHBA hba = (PHBA)osext;
93275970Scy
94275970Scy    hba->pcibar[index].rid = 0x10 + index * 4;
95275970Scy
96275970Scy    if (pci_read_config(hba->pcidev, hba->pcibar[index].rid, 4) & 1)
97275970Scy    	hba->pcibar[index].type = SYS_RES_IOPORT;
98275970Scy    else
99275970Scy    	hba->pcibar[index].type = SYS_RES_MEMORY;
100275970Scy
101275970Scy    hba->pcibar[index].res = bus_alloc_resource(hba->pcidev,
102275970Scy		hba->pcibar[index].type, &hba->pcibar[index].rid, 0, ~0, length, RF_ACTIVE);
103275970Scy
104275970Scy	hba->pcibar[index].base = (char *)rman_get_virtual(hba->pcibar[index].res) + offset;
105275970Scy	return hba->pcibar[index].base;
106275970Scy}
107275970Scy
108275970Scyvoid os_unmap_pci_bar(void *osext, void *base)
109275970Scy{
110275970Scy	PHBA hba = (PHBA)osext;
111275970Scy	int index;
112275970Scy
113316722Sdelphij	for (index=0; index<6; index++) {
114275970Scy		if (hba->pcibar[index].base==base) {
115275970Scy			bus_release_resource(hba->pcidev, hba->pcibar[index].type,
116275970Scy				hba->pcibar[index].rid, hba->pcibar[index].res);
117275970Scy			hba->pcibar[index].base = 0;
118275970Scy			return;
119275970Scy		}
120275970Scy	}
121275970Scy}
122275970Scy
123275970Scyvoid freelist_reserve(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT count)
124275970Scy{
125275970Scy    PVBUS_EXT vbus_ext = osext;
126275970Scy
127275970Scy    if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
128275970Scy        vbus_ext = ((PHBA)osext)->vbus_ext;
129275970Scy
130275970Scy    list->next = vbus_ext->freelist_head;
131275970Scy    vbus_ext->freelist_head = list;
132275970Scy    list->dma = 0;
133275970Scy    list->size = size;
134275970Scy    list->head = 0;
135275970Scy#ifdef DBG
136275970Scy    list->reserved_count =
137275970Scy#endif
138275970Scy    list->count = count;
139275970Scy}
140275970Scy
141275970Scyvoid *freelist_get(struct freelist *list)
142275970Scy{
143275970Scy    void * result;
144275970Scy    if (list->count) {
145        HPT_ASSERT(list->head);
146        result = list->head;
147        list->head = *(void **)result;
148        list->count--;
149        return result;
150    }
151    return 0;
152}
153
154void freelist_put(struct freelist * list, void *p)
155{
156    HPT_ASSERT(list->dma==0);
157    list->count++;
158    *(void **)p = list->head;
159    list->head = p;
160}
161
162void freelist_reserve_dma(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT alignment, HPT_UINT count)
163{
164    PVBUS_EXT vbus_ext = osext;
165
166    if (vbus_ext->ext_type!=EXT_TYPE_VBUS)
167        vbus_ext = ((PHBA)osext)->vbus_ext;
168
169    list->next = vbus_ext->freelist_dma_head;
170    vbus_ext->freelist_dma_head = list;
171    list->dma = 1;
172    list->alignment = alignment;
173    list->size = size;
174    list->head = 0;
175#ifdef DBG
176    list->reserved_count =
177#endif
178    list->count = count;
179}
180
181void *freelist_get_dma(struct freelist *list, BUS_ADDRESS *busaddr)
182{
183    void *result;
184    HPT_ASSERT(list->dma);
185    result = freelist_get(list);
186    if (result)
187        *busaddr = *(BUS_ADDRESS *)((void **)result+1);
188    return result;
189}
190
191void freelist_put_dma(struct freelist *list, void *p, BUS_ADDRESS busaddr)
192{
193    HPT_ASSERT(list->dma);
194    list->count++;
195    *(void **)p = list->head;
196    *(BUS_ADDRESS *)((void **)p+1) = busaddr;
197    list->head = p;
198}
199
200HPT_U32 os_get_stamp(void)
201{
202    HPT_U32 stamp;
203    do { stamp = random(); } while (stamp==0);
204    return stamp;
205}
206
207void os_stallexec(HPT_U32 microseconds)
208{
209    DELAY(microseconds);
210}
211
212static void os_timer_for_ldm(void *arg)
213{
214	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
215	ldm_on_timer((PVBUS)vbus_ext->vbus);
216}
217
218void  os_request_timer(void * osext, HPT_U32 interval)
219{
220	PVBUS_EXT vbus_ext = osext;
221
222	HPT_ASSERT(vbus_ext->ext_type==EXT_TYPE_VBUS);
223
224	untimeout(os_timer_for_ldm, vbus_ext, vbus_ext->timer);
225	vbus_ext->timer = timeout(os_timer_for_ldm, vbus_ext, interval * hz / 1000000);
226}
227
228HPT_TIME os_query_time(void)
229{
230	return ticks * (1000000 / hz);
231}
232
233void os_schedule_task(void *osext, OSM_TASK *task)
234{
235	PVBUS_EXT vbus_ext = osext;
236
237	HPT_ASSERT(task->next==0);
238
239	if (vbus_ext->tasks==0)
240		vbus_ext->tasks = task;
241	else {
242		OSM_TASK *t = vbus_ext->tasks;
243		while (t->next) t = t->next;
244		t->next = task;
245	}
246
247	if (vbus_ext->worker.ta_context)
248		TASK_ENQUEUE(&vbus_ext->worker);
249}
250
251int os_revalidate_device(void *osext, int id)
252{
253
254    return 0;
255}
256
257int os_query_remove_device(void *osext, int id)
258{
259	PVBUS_EXT				vbus_ext = (PVBUS_EXT)osext;
260	struct cam_periph		*periph = NULL;
261    struct cam_path			*path;
262    int						status,retval = 0;
263
264    status = xpt_create_path(&path, NULL, vbus_ext->sim->path_id, id, 0);
265    if (status == CAM_REQ_CMP) {
266		if((periph = cam_periph_find(path, "da")) != NULL){
267			if(periph->refcount >= 1)
268				retval = -1;
269		}
270		xpt_free_path(path);
271    }
272
273    return retval;
274}
275
276HPT_U8 os_get_vbus_seq(void *osext)
277{
278    return ((PVBUS_EXT)osext)->sim->path_id;
279}
280
281int  os_printk(char *fmt, ...)
282{
283    va_list args;
284    static char buf[512];
285
286    va_start(args, fmt);
287    vsnprintf(buf, sizeof(buf), fmt, args);
288    va_end(args);
289    return printf("%s: %s\n", driver_name, buf);
290}
291
292#ifdef DBG
293void os_check_stack(const char *location, int size){}
294
295void __os_dbgbreak(const char *file, int line)
296{
297    printf("*** break at %s:%d ***", file, line);
298    while (1);
299}
300
301int hptrr_dbg_level = 1;
302#endif
303