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