xref: /freebsd/sys/dev/hpt27xx/osm.h (revision 7cc42f6d25ef2e19059d088fa7d4853fe9afefb5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2011 HighPoint Technologies, Inc.
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$
29  */
30 
31 #include <dev/hpt27xx/hpt27xx_config.h>
32 
33 #ifndef _HPT_OSM_H_
34 #define _HPT_OSM_H_
35 
36 #define VERMAGIC_OSM 6
37 
38 
39 #define MAX_MEMBERS 64
40 
41 #define os_max_queue_comm 32
42 #define os_max_sg_descriptors 18
43 
44 
45 extern int os_max_cache_size;
46 
47 
48 #define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
49 #define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
50 
51 /* data types */
52 typedef unsigned int HPT_UINT, HPT_U32;
53 typedef unsigned long HPT_UPTR;
54 typedef unsigned short HPT_U16;
55 typedef unsigned char HPT_U8;
56 typedef unsigned long HPT_TIME;
57 typedef unsigned long long HPT_U64;
58 typedef long long HPT_64;
59 
60 #define CPU_TO_LE64(x) (x)
61 #define CPU_TO_LE32(x) (x)
62 #define CPU_TO_LE16(x) (x)
63 #define LE32_TO_CPU(x) (x)
64 #define LE16_TO_CPU(x) (x)
65 #define LE64_TO_CPU(x) (x)
66 
67 static __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
68 {
69 	HPT_U8 *p = (HPT_U8 *)&x;
70 	return ((HPT_U64)p[0] << 56) |
71 		((HPT_U64)p[1] << 48) |
72 		((HPT_U64)p[2] << 40) |
73 		((HPT_U64)p[3] << 32) |
74 		((HPT_U64)p[4] << 24) |
75 		((HPT_U64)p[5] << 16) |
76 		((HPT_U64)p[6] << 8) |
77 		p[7];
78 }
79 
80 static __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
81 {
82 	HPT_U8 *p = (HPT_U8 *)&x;
83 	return ((HPT_U32)p[0] << 24) |
84 		((HPT_U32)p[1] << 16) |
85 		((HPT_U32)p[2] << 8) | p[3];
86 }
87 
88 static __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
89 {
90 	return ((HPT_U8)x << 8) | (x>>8);
91 }
92 
93 #define BE16_TO_CPU(x) CPU_TO_BE16(x)
94 #define BE32_TO_CPU(x) CPU_TO_BE32(x)
95 #define BE64_TO_CPU(x) CPU_TO_BE64(x)
96 
97 #define FAR
98 #define EXTERN_C
99 
100 typedef void * HPT_PTR;
101 
102 typedef HPT_U64 HPT_LBA;
103 typedef HPT_U64 HPT_RAW_LBA;
104 #define MAX_LBA_VALUE 0xffffffffffffffffull
105 #define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
106 #define RAW_LBA(x) (x)
107 #define LO_LBA(x) ((HPT_U32)(x))
108 #define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
109 #define LBA_FORMAT_STR "0x%llX"
110 
111 typedef HPT_U64 BUS_ADDRESS;
112 #define LO_BUSADDR(x) ((HPT_U32)(x))
113 #define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
114 
115 typedef unsigned char HPT_BOOL;
116 #define HPT_TRUE  1
117 #define HPT_FALSE 0
118 
119 typedef struct _TIME_RECORD {
120    HPT_U32        seconds:6;      /* 0 - 59 */
121    HPT_U32        minutes:6;      /* 0 - 59 */
122    HPT_U32        month:4;        /* 1 - 12 */
123    HPT_U32        hours:6;        /* 0 - 59 */
124    HPT_U32        day:5;          /* 1 - 31 */
125    HPT_U32        year:5;         /* 0=2000, 31=2031 */
126 } TIME_RECORD;
127 
128 /* hardware access */
129 HPT_U8   os_inb  (void *port);
130 HPT_U16  os_inw  (void *port);
131 HPT_U32  os_inl  (void *port);
132 void     os_outb (void *port, HPT_U8 value);
133 void     os_outw (void *port, HPT_U16 value);
134 void     os_outl (void *port, HPT_U32 value);
135 void     os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
136 void     os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
137 
138 extern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
139 
140 #define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
141 #define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
142 #define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
143 
144 #define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
145 #define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
146 #define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
147 
148 /* PCI configuration space for specified device*/
149 HPT_U8   os_pci_readb (void *osext, HPT_U8 offset);
150 HPT_U16  os_pci_readw (void *osext, HPT_U8 offset);
151 HPT_U32  os_pci_readl (void *osext, HPT_U8 offset);
152 void     os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
153 void     os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
154 void     os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
155 
156 /* obsolute interface */
157 #define MAX_PCI_BUS_NUMBER 0xff
158 #define MAX_PCI_DEVICE_NUMBER 32
159 #define MAX_PCI_FUNC_NUMBER 1
160 HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
161 HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
162 void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
163 void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
164 
165 
166 void *os_map_pci_bar(
167 	void *osext,
168 	int index,
169 	HPT_U32 offset,
170 	HPT_U32 length
171 );
172 
173 
174 void os_unmap_pci_bar(void *osext, void *base);
175 
176 #define os_kmap_sgptr(psg) (psg->addr._logical)
177 #define os_kunmap_sgptr(ptr)
178 #define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
179 
180 /* timer */
181 void *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
182 void  os_del_timer(void *handle);
183 void  os_request_timer(void * osext, HPT_U32 interval);
184 HPT_TIME os_query_time(void);
185 
186 /* task */
187 #define OS_SUPPORT_TASK
188 
189 typedef struct _OSM_TASK {
190 	struct _OSM_TASK *next;
191 	void (*func)(void *vbus, void *data);
192 	void *data;
193 }
194 OSM_TASK;
195 
196 void os_schedule_task(void *osext, OSM_TASK *task);
197 
198 /* misc */
199 HPT_U32 os_get_stamp(void);
200 void os_stallexec(HPT_U32 microseconds);
201 
202 #ifndef _SYS_LIBKERN_H_
203 #define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
204 #define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
205 #define strcpy(dst, src) __builtin_strcpy((dst), (src))
206 static __inline void * memset(void *dst, int c, unsigned long size)
207 {
208 	char *p;
209 	for (p=(char*)dst; size; size--,p++) *p = c;
210 	return dst;
211 }
212 #endif
213 
214 #define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
215 
216 
217 #define os_register_device(osext, target_id)
218 #define os_unregister_device(osext, target_id)
219 int os_query_remove_device(void *osext, int target_id);
220 int os_revalidate_device(void *osext, int target_id);
221 
222 HPT_U8 os_get_vbus_seq(void *osext);
223 
224 /* debug support */
225 int  os_printk(char *fmt, ...);
226 
227 #if DBG
228 extern int hpt_dbg_level;
229 #define dbg_printk(fmt, args...) \
230 	do {\
231 		char *__p = fmt;\
232 		int __level;\
233 		if (__p[0]=='<' && __p[2]=='>') {\
234 			__level = __p[1] - '0';\
235 			__p += 3;\
236 		} else\
237 			__level = 7;\
238 		if (hpt_dbg_level >= __level)\
239 			os_printk(__p, ##args);\
240 	} while (0)
241 
242 #define KdPrint(x)  do { dbg_printk x; } while (0)
243 void __os_dbgbreak(const char *file, int line);
244 #define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
245 #define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
246 void os_check_stack(const char *location, int size);
247 #define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
248 #else
249 #define KdPrint(x)
250 #define HPT_ASSERT(x)
251 #define HPT_CHECK_STACK(size)
252 #endif
253 
254 #define OsPrint(x) do { os_printk x; } while (0)
255 
256 #endif
257