xref: /freebsd/sys/dev/hptnr/osm.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /* $Id: osm.h,v 1.10 2010/05/11 10:30:33 lcn Exp $ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <dev/hptnr/hptnr_config.h>
32 #ifndef _HPT_OSM_H_
33 #define _HPT_OSM_H_
34 
35 #define VERMAGIC_OSM 6
36 
37 
38 #define MAX_MEMBERS 64
39 
40 #define os_max_queue_comm 32
41 #define os_max_sg_descriptors 18
42 
43 
44 extern int os_max_cache_size;
45 
46 
47 #define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
48 #define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
49 
50 /* data types */
51 typedef unsigned int HPT_UINT, HPT_U32;
52 typedef unsigned long HPT_UPTR;
53 typedef unsigned short HPT_U16;
54 typedef unsigned char HPT_U8;
55 typedef unsigned long HPT_TIME;
56 typedef unsigned long long HPT_U64;
57 
58 #define CPU_TO_LE64(x) (x)
59 #define CPU_TO_LE32(x) (x)
60 #define CPU_TO_LE16(x) (x)
61 #define LE32_TO_CPU(x) (x)
62 #define LE16_TO_CPU(x) (x)
63 #define LE64_TO_CPU(x) (x)
64 
65 static __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
66 {
67 	HPT_U8 *p = (HPT_U8 *)&x;
68 	return ((HPT_U64)p[0] << 56) |
69 		((HPT_U64)p[1] << 48) |
70 		((HPT_U64)p[2] << 40) |
71 		((HPT_U64)p[3] << 32) |
72 		((HPT_U64)p[4] << 24) |
73 		((HPT_U64)p[5] << 16) |
74 		((HPT_U64)p[6] << 8) |
75 		p[7];
76 }
77 
78 static __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
79 {
80 	HPT_U8 *p = (HPT_U8 *)&x;
81 	return ((HPT_U32)p[0] << 24) |
82 		((HPT_U32)p[1] << 16) |
83 		((HPT_U32)p[2] << 8) | p[3];
84 }
85 
86 static __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
87 {
88 	return ((HPT_U8)x << 8) | (x>>8);
89 }
90 
91 #define BE16_TO_CPU(x) CPU_TO_BE16(x)
92 #define BE32_TO_CPU(x) CPU_TO_BE32(x)
93 #define BE64_TO_CPU(x) CPU_TO_BE64(x)
94 
95 #define FAR
96 #define EXTERN_C
97 
98 typedef void * HPT_PTR;
99 
100 typedef HPT_U64 HPT_LBA;
101 typedef HPT_U64 HPT_RAW_LBA;
102 #define MAX_LBA_VALUE 0xffffffffffffffffull
103 #define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
104 #define RAW_LBA(x) (x)
105 #define LO_LBA(x) ((HPT_U32)(x))
106 #define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
107 #define LBA_FORMAT_STR "0x%llX"
108 
109 typedef HPT_U64 BUS_ADDRESS;
110 #define LO_BUSADDR(x) ((HPT_U32)(x))
111 #define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
112 
113 typedef unsigned char HPT_BOOL;
114 #define HPT_TRUE  1
115 #define HPT_FALSE 0
116 
117 typedef struct _TIME_RECORD {
118    HPT_U32        seconds:6;      /* 0 - 59 */
119    HPT_U32        minutes:6;      /* 0 - 59 */
120    HPT_U32        month:4;        /* 1 - 12 */
121    HPT_U32        hours:6;        /* 0 - 59 */
122    HPT_U32        day:5;          /* 1 - 31 */
123    HPT_U32        year:5;         /* 0=2000, 31=2031 */
124 } TIME_RECORD;
125 
126 /* hardware access */
127 HPT_U8   os_inb  (void *port);
128 HPT_U16  os_inw  (void *port);
129 HPT_U32  os_inl  (void *port);
130 void     os_outb (void *port, HPT_U8 value);
131 void     os_outw (void *port, HPT_U16 value);
132 void     os_outl (void *port, HPT_U32 value);
133 void     os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
134 void     os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
135 
136 extern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
137 
138 #define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
139 #define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
140 #define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
141 
142 #define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
143 #define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
144 #define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
145 
146 /* PCI configuration space for specified device*/
147 HPT_U8   os_pci_readb (void *osext, HPT_U8 offset);
148 HPT_U16  os_pci_readw (void *osext, HPT_U8 offset);
149 HPT_U32  os_pci_readl (void *osext, HPT_U8 offset);
150 void     os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
151 void     os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
152 void     os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
153 
154 /* obsolute interface */
155 #define MAX_PCI_BUS_NUMBER 0xff
156 #define MAX_PCI_DEVICE_NUMBER 32
157 #define MAX_PCI_FUNC_NUMBER 1
158 HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
159 HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
160 void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
161 void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
162 
163 
164 void *os_map_pci_bar(
165 	void *osext,
166 	int index,
167 	HPT_U32 offset,
168 	HPT_U32 length
169 );
170 
171 
172 void os_unmap_pci_bar(void *osext, void *base);
173 
174 #define os_kmap_sgptr(psg) (psg->addr._logical)
175 #define os_kunmap_sgptr(ptr)
176 #define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
177 
178 /* timer */
179 void *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
180 void  os_del_timer(void *handle);
181 void  os_request_timer(void * osext, HPT_U32 interval);
182 HPT_TIME os_query_time(void);
183 
184 /* task */
185 #define OS_SUPPORT_TASK
186 
187 typedef struct _OSM_TASK {
188 	struct _OSM_TASK *next;
189 	void (*func)(void *vbus, void *data);
190 	void *data;
191 }
192 OSM_TASK;
193 
194 void os_schedule_task(void *osext, OSM_TASK *task);
195 
196 /* misc */
197 HPT_U32 os_get_stamp(void);
198 void os_stallexec(HPT_U32 microseconds);
199 
200 #ifndef _SYS_LIBKERN_H_
201 #define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
202 #define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
203 #define strcpy(dst, src) __builtin_strcpy((dst), (src))
204 static __inline void * memset(void *dst, int c, unsigned long size)
205 {
206 	char *p;
207 	for (p=(char*)dst; size; size--,p++) *p = c;
208 	return dst;
209 }
210 #endif
211 
212 #define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
213 
214 
215 #define os_register_device(osext, target_id)
216 #define os_unregister_device(osext, target_id)
217 int os_query_remove_device(void *osext, int target_id);
218 int os_revalidate_device(void *osext, int target_id);
219 
220 HPT_U8 os_get_vbus_seq(void *osext);
221 
222 /* debug support */
223 int  os_printk(char *fmt, ...);
224 
225 #if DBG
226 extern int hpt_dbg_level;
227 #define KdPrint(x)  do { if (hpt_dbg_level) os_printk x; } while (0)
228 void __os_dbgbreak(const char *file, int line);
229 #define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
230 #define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
231 void os_check_stack(const char *location, int size);
232 #define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
233 #else
234 #define KdPrint(x)
235 #define HPT_ASSERT(x)
236 #define HPT_CHECK_STACK(size)
237 #endif
238 
239 #define OsPrint(x) do { os_printk x; } while (0)
240 
241 #endif
242