1252867Sdelphij/* $Id: osm.h,v 1.10 2010/05/11 10:30:33 lcn Exp $ */
2252867Sdelphij/*-
3252867Sdelphij * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
4252867Sdelphij * All rights reserved.
5252867Sdelphij *
6252867Sdelphij * Redistribution and use in source and binary forms, with or without
7252867Sdelphij * modification, are permitted provided that the following conditions
8252867Sdelphij * are met:
9252867Sdelphij * 1. Redistributions of source code must retain the above copyright
10252867Sdelphij *    notice, this list of conditions and the following disclaimer.
11252867Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
12252867Sdelphij *    notice, this list of conditions and the following disclaimer in the
13252867Sdelphij *    documentation and/or other materials provided with the distribution.
14252867Sdelphij *
15252867Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16252867Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17252867Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18252867Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19252867Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20252867Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21252867Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22252867Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23252867Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24252867Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25252867Sdelphij * SUCH DAMAGE.
26252867Sdelphij *
27252867Sdelphij * $FreeBSD$
28252867Sdelphij */
29252867Sdelphij#include <dev/hptnr/hptnr_config.h>
30252867Sdelphij#ifndef _HPT_OSM_H_
31252867Sdelphij#define _HPT_OSM_H_
32252867Sdelphij
33252867Sdelphij#define VERMAGIC_OSM 6
34252867Sdelphij
35252867Sdelphij
36252867Sdelphij#define MAX_MEMBERS 64
37252867Sdelphij
38252867Sdelphij#define os_max_queue_comm 32
39252867Sdelphij#define os_max_sg_descriptors 18
40252867Sdelphij
41252867Sdelphij
42252867Sdelphijextern int os_max_cache_size;
43252867Sdelphij
44252867Sdelphij
45252867Sdelphij#define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
46252867Sdelphij#define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
47252867Sdelphij
48252867Sdelphij/* data types */
49252867Sdelphijtypedef unsigned int HPT_UINT, HPT_U32;
50252867Sdelphijtypedef unsigned long HPT_UPTR;
51252867Sdelphijtypedef unsigned short HPT_U16;
52252867Sdelphijtypedef unsigned char HPT_U8;
53252867Sdelphijtypedef unsigned long HPT_TIME;
54252867Sdelphijtypedef unsigned long long HPT_U64;
55252867Sdelphij
56252867Sdelphij#define CPU_TO_LE64(x) (x)
57252867Sdelphij#define CPU_TO_LE32(x) (x)
58252867Sdelphij#define CPU_TO_LE16(x) (x)
59252867Sdelphij#define LE32_TO_CPU(x) (x)
60252867Sdelphij#define LE16_TO_CPU(x) (x)
61252867Sdelphij#define LE64_TO_CPU(x) (x)
62252867Sdelphij
63252867Sdelphijstatic __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
64252867Sdelphij{
65252867Sdelphij	HPT_U8 *p = (HPT_U8 *)&x;
66252867Sdelphij	return ((HPT_U64)p[0] << 56) |
67252867Sdelphij		((HPT_U64)p[1] << 48) |
68252867Sdelphij		((HPT_U64)p[2] << 40) |
69252867Sdelphij		((HPT_U64)p[3] << 32) |
70252867Sdelphij		((HPT_U64)p[4] << 24) |
71252867Sdelphij		((HPT_U64)p[5] << 16) |
72252867Sdelphij		((HPT_U64)p[6] << 8) |
73252867Sdelphij		p[7];
74252867Sdelphij}
75252867Sdelphij
76252867Sdelphijstatic __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
77252867Sdelphij{
78252867Sdelphij	HPT_U8 *p = (HPT_U8 *)&x;
79252867Sdelphij	return ((HPT_U32)p[0] << 24) |
80252867Sdelphij		((HPT_U32)p[1] << 16) |
81252867Sdelphij		((HPT_U32)p[2] << 8) | p[3];
82252867Sdelphij}
83252867Sdelphij
84252867Sdelphijstatic __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
85252867Sdelphij{
86252867Sdelphij	return ((HPT_U8)x << 8) | (x>>8);
87252867Sdelphij}
88252867Sdelphij
89252867Sdelphij#define BE16_TO_CPU(x) CPU_TO_BE16(x)
90252867Sdelphij#define BE32_TO_CPU(x) CPU_TO_BE32(x)
91252867Sdelphij#define BE64_TO_CPU(x) CPU_TO_BE64(x)
92252867Sdelphij
93252867Sdelphij#define FAR
94252867Sdelphij#define EXTERN_C
95252867Sdelphij
96252867Sdelphijtypedef void * HPT_PTR;
97252867Sdelphij
98252867Sdelphijtypedef HPT_U64 HPT_LBA;
99252867Sdelphijtypedef HPT_U64 HPT_RAW_LBA;
100252867Sdelphij#define MAX_LBA_VALUE 0xffffffffffffffffull
101252867Sdelphij#define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
102252867Sdelphij#define RAW_LBA(x) (x)
103252867Sdelphij#define LO_LBA(x) ((HPT_U32)(x))
104252867Sdelphij#define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
105252867Sdelphij#define LBA_FORMAT_STR "0x%llX"
106252867Sdelphij
107252867Sdelphijtypedef HPT_U64 BUS_ADDRESS;
108252867Sdelphij#define LO_BUSADDR(x) ((HPT_U32)(x))
109252867Sdelphij#define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
110252867Sdelphij
111252867Sdelphijtypedef unsigned char HPT_BOOL;
112252867Sdelphij#define HPT_TRUE  1
113252867Sdelphij#define HPT_FALSE 0
114252867Sdelphij
115252867Sdelphijtypedef struct _TIME_RECORD {
116252867Sdelphij   HPT_U32        seconds:6;      /* 0 - 59 */
117252867Sdelphij   HPT_U32        minutes:6;      /* 0 - 59 */
118252867Sdelphij   HPT_U32        month:4;        /* 1 - 12 */
119252867Sdelphij   HPT_U32        hours:6;        /* 0 - 59 */
120252867Sdelphij   HPT_U32        day:5;          /* 1 - 31 */
121252867Sdelphij   HPT_U32        year:5;         /* 0=2000, 31=2031 */
122252867Sdelphij} TIME_RECORD;
123252867Sdelphij
124252867Sdelphij/* hardware access */
125252867SdelphijHPT_U8   os_inb  (void *port);
126252867SdelphijHPT_U16  os_inw  (void *port);
127252867SdelphijHPT_U32  os_inl  (void *port);
128252867Sdelphijvoid     os_outb (void *port, HPT_U8 value);
129252867Sdelphijvoid     os_outw (void *port, HPT_U16 value);
130252867Sdelphijvoid     os_outl (void *port, HPT_U32 value);
131252867Sdelphijvoid     os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
132252867Sdelphijvoid     os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
133252867Sdelphij
134252867Sdelphijextern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
135252867Sdelphij
136252867Sdelphij#define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
137252867Sdelphij#define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
138252867Sdelphij#define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
139252867Sdelphij
140252867Sdelphij#define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
141252867Sdelphij#define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
142252867Sdelphij#define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
143252867Sdelphij
144252867Sdelphij/* PCI configuration space for specified device*/
145252867SdelphijHPT_U8   os_pci_readb (void *osext, HPT_U8 offset);
146252867SdelphijHPT_U16  os_pci_readw (void *osext, HPT_U8 offset);
147252867SdelphijHPT_U32  os_pci_readl (void *osext, HPT_U8 offset);
148252867Sdelphijvoid     os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
149252867Sdelphijvoid     os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
150252867Sdelphijvoid     os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
151252867Sdelphij
152252867Sdelphij/* obsolute interface */
153252867Sdelphij#define MAX_PCI_BUS_NUMBER 0xff
154252867Sdelphij#define MAX_PCI_DEVICE_NUMBER 32
155252867Sdelphij#define MAX_PCI_FUNC_NUMBER 1
156284792SdelphijHPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
157252867SdelphijHPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
158284792Sdelphijvoid pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
159284792Sdelphijvoid pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
160252867Sdelphij
161252867Sdelphij
162252867Sdelphijvoid *os_map_pci_bar(
163252867Sdelphij	void *osext,
164252867Sdelphij	int index,
165252867Sdelphij	HPT_U32 offset,
166252867Sdelphij	HPT_U32 length
167252867Sdelphij);
168252867Sdelphij
169252867Sdelphij
170252867Sdelphijvoid os_unmap_pci_bar(void *osext, void *base);
171252867Sdelphij
172252867Sdelphij#define os_kmap_sgptr(psg) (psg->addr._logical)
173252867Sdelphij#define os_kunmap_sgptr(ptr)
174252867Sdelphij#define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
175252867Sdelphij
176252867Sdelphij/* timer */
177252867Sdelphijvoid *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
178252867Sdelphijvoid  os_del_timer(void *handle);
179252867Sdelphijvoid  os_request_timer(void * osext, HPT_U32 interval);
180252867SdelphijHPT_TIME os_query_time(void);
181252867Sdelphij
182252867Sdelphij/* task */
183252867Sdelphij#define OS_SUPPORT_TASK
184252867Sdelphij
185252867Sdelphijtypedef struct _OSM_TASK {
186252867Sdelphij	struct _OSM_TASK *next;
187252867Sdelphij	void (*func)(void *vbus, void *data);
188252867Sdelphij	void *data;
189252867Sdelphij}
190252867SdelphijOSM_TASK;
191252867Sdelphij
192252867Sdelphijvoid os_schedule_task(void *osext, OSM_TASK *task);
193252867Sdelphij
194252867Sdelphij/* misc */
195252867SdelphijHPT_U32 os_get_stamp(void);
196252867Sdelphijvoid os_stallexec(HPT_U32 microseconds);
197252867Sdelphij
198252867Sdelphij#ifndef _SYS_LIBKERN_H_
199252867Sdelphij#define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
200252867Sdelphij#define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
201252867Sdelphij#define strcpy(dst, src) __builtin_strcpy((dst), (src))
202252867Sdelphijstatic __inline void * memset(void *dst, int c, unsigned long size)
203252867Sdelphij{
204252867Sdelphij	char *p;
205252867Sdelphij	for (p=(char*)dst; size; size--,p++) *p = c;
206252867Sdelphij	return dst;
207252867Sdelphij}
208252867Sdelphij#endif
209252867Sdelphij
210252867Sdelphij#define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
211252867Sdelphij
212252867Sdelphij
213252867Sdelphij#define os_register_device(osext, target_id)
214252867Sdelphij#define os_unregister_device(osext, target_id)
215252867Sdelphijint os_query_remove_device(void *osext, int target_id);
216252867Sdelphijint os_revalidate_device(void *osext, int target_id);
217252867Sdelphij
218252867SdelphijHPT_U8 os_get_vbus_seq(void *osext);
219252867Sdelphij
220252867Sdelphij/* debug support */
221252867Sdelphijint  os_printk(char *fmt, ...);
222252867Sdelphij
223252867Sdelphij#if DBG
224252867Sdelphijextern int hpt_dbg_level;
225252867Sdelphij#define KdPrint(x)  do { if (hpt_dbg_level) os_printk x; } while (0)
226252867Sdelphijvoid __os_dbgbreak(const char *file, int line);
227252867Sdelphij#define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
228252867Sdelphij#define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
229252867Sdelphijvoid os_check_stack(const char *location, int size);
230252867Sdelphij#define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
231252867Sdelphij#else
232252867Sdelphij#define KdPrint(x)
233252867Sdelphij#define HPT_ASSERT(x)
234252867Sdelphij#define HPT_CHECK_STACK(size)
235252867Sdelphij#endif
236252867Sdelphij
237252867Sdelphij#define OsPrint(x) do { os_printk x; } while (0)
238252867Sdelphij
239252867Sdelphij#endif
240