xref: /freebsd/sys/dev/hpt27xx/osm.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
181966bceSXin LI /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4f29c86f1SXin LI  * Copyright (c) 2005-2011 HighPoint Technologies, Inc.
581966bceSXin LI  * All rights reserved.
681966bceSXin LI  *
781966bceSXin LI  * Redistribution and use in source and binary forms, with or without
881966bceSXin LI  * modification, are permitted provided that the following conditions
981966bceSXin LI  * are met:
1081966bceSXin LI  * 1. Redistributions of source code must retain the above copyright
1181966bceSXin LI  *    notice, this list of conditions and the following disclaimer.
1281966bceSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
1381966bceSXin LI  *    notice, this list of conditions and the following disclaimer in the
1481966bceSXin LI  *    documentation and/or other materials provided with the distribution.
1581966bceSXin LI  *
1681966bceSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1781966bceSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1881966bceSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1981966bceSXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2081966bceSXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2181966bceSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2281966bceSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2381966bceSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2481966bceSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2581966bceSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2681966bceSXin LI  * SUCH DAMAGE.
2781966bceSXin LI  */
2881966bceSXin LI 
2981966bceSXin LI #include <dev/hpt27xx/hpt27xx_config.h>
3081966bceSXin LI 
3181966bceSXin LI #ifndef _HPT_OSM_H_
3281966bceSXin LI #define _HPT_OSM_H_
3381966bceSXin LI 
3481966bceSXin LI #define VERMAGIC_OSM 6
3581966bceSXin LI 
3681966bceSXin LI 
3781966bceSXin LI #define MAX_MEMBERS 64
3881966bceSXin LI 
3981966bceSXin LI #define os_max_queue_comm 32
4081966bceSXin LI #define os_max_sg_descriptors 18
4181966bceSXin LI 
4281966bceSXin LI 
4381966bceSXin LI extern int os_max_cache_size;
4481966bceSXin LI 
4581966bceSXin LI 
4681966bceSXin LI #define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
4781966bceSXin LI #define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
4881966bceSXin LI 
4981966bceSXin LI /* data types */
5081966bceSXin LI typedef unsigned int HPT_UINT, HPT_U32;
5181966bceSXin LI typedef unsigned long HPT_UPTR;
5281966bceSXin LI typedef unsigned short HPT_U16;
5381966bceSXin LI typedef unsigned char HPT_U8;
5481966bceSXin LI typedef unsigned long HPT_TIME;
5581966bceSXin LI typedef unsigned long long HPT_U64;
56f29c86f1SXin LI typedef long long HPT_64;
5781966bceSXin LI 
5881966bceSXin LI #define CPU_TO_LE64(x) (x)
5981966bceSXin LI #define CPU_TO_LE32(x) (x)
6081966bceSXin LI #define CPU_TO_LE16(x) (x)
6181966bceSXin LI #define LE32_TO_CPU(x) (x)
6281966bceSXin LI #define LE16_TO_CPU(x) (x)
6381966bceSXin LI #define LE64_TO_CPU(x) (x)
6481966bceSXin LI 
CPU_TO_BE64(HPT_U64 x)6581966bceSXin LI static __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
6681966bceSXin LI {
6781966bceSXin LI 	HPT_U8 *p = (HPT_U8 *)&x;
6881966bceSXin LI 	return ((HPT_U64)p[0] << 56) |
6981966bceSXin LI 		((HPT_U64)p[1] << 48) |
7081966bceSXin LI 		((HPT_U64)p[2] << 40) |
7181966bceSXin LI 		((HPT_U64)p[3] << 32) |
7281966bceSXin LI 		((HPT_U64)p[4] << 24) |
7381966bceSXin LI 		((HPT_U64)p[5] << 16) |
7481966bceSXin LI 		((HPT_U64)p[6] << 8) |
7581966bceSXin LI 		p[7];
7681966bceSXin LI }
7781966bceSXin LI 
CPU_TO_BE32(HPT_U32 x)7881966bceSXin LI static __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
7981966bceSXin LI {
8081966bceSXin LI 	HPT_U8 *p = (HPT_U8 *)&x;
8181966bceSXin LI 	return ((HPT_U32)p[0] << 24) |
8281966bceSXin LI 		((HPT_U32)p[1] << 16) |
8381966bceSXin LI 		((HPT_U32)p[2] << 8) | p[3];
8481966bceSXin LI }
8581966bceSXin LI 
CPU_TO_BE16(HPT_U16 x)8681966bceSXin LI static __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
8781966bceSXin LI {
8881966bceSXin LI 	return ((HPT_U8)x << 8) | (x>>8);
8981966bceSXin LI }
9081966bceSXin LI 
9181966bceSXin LI #define BE16_TO_CPU(x) CPU_TO_BE16(x)
9281966bceSXin LI #define BE32_TO_CPU(x) CPU_TO_BE32(x)
9381966bceSXin LI #define BE64_TO_CPU(x) CPU_TO_BE64(x)
9481966bceSXin LI 
9581966bceSXin LI #define FAR
9681966bceSXin LI #define EXTERN_C
9781966bceSXin LI 
9881966bceSXin LI typedef void * HPT_PTR;
9981966bceSXin LI 
10081966bceSXin LI typedef HPT_U64 HPT_LBA;
10181966bceSXin LI typedef HPT_U64 HPT_RAW_LBA;
10281966bceSXin LI #define MAX_LBA_VALUE 0xffffffffffffffffull
10381966bceSXin LI #define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
10481966bceSXin LI #define RAW_LBA(x) (x)
10581966bceSXin LI #define LO_LBA(x) ((HPT_U32)(x))
10681966bceSXin LI #define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
10781966bceSXin LI #define LBA_FORMAT_STR "0x%llX"
10881966bceSXin LI 
10981966bceSXin LI typedef HPT_U64 BUS_ADDRESS;
11081966bceSXin LI #define LO_BUSADDR(x) ((HPT_U32)(x))
11181966bceSXin LI #define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
11281966bceSXin LI 
11381966bceSXin LI typedef unsigned char HPT_BOOL;
11481966bceSXin LI #define HPT_TRUE  1
11581966bceSXin LI #define HPT_FALSE 0
11681966bceSXin LI 
11781966bceSXin LI typedef struct _TIME_RECORD {
11881966bceSXin LI    HPT_U32        seconds:6;      /* 0 - 59 */
11981966bceSXin LI    HPT_U32        minutes:6;      /* 0 - 59 */
12081966bceSXin LI    HPT_U32        month:4;        /* 1 - 12 */
12181966bceSXin LI    HPT_U32        hours:6;        /* 0 - 59 */
12281966bceSXin LI    HPT_U32        day:5;          /* 1 - 31 */
12381966bceSXin LI    HPT_U32        year:5;         /* 0=2000, 31=2031 */
12481966bceSXin LI } TIME_RECORD;
12581966bceSXin LI 
12681966bceSXin LI /* hardware access */
12781966bceSXin LI HPT_U8   os_inb  (void *port);
12881966bceSXin LI HPT_U16  os_inw  (void *port);
12981966bceSXin LI HPT_U32  os_inl  (void *port);
13081966bceSXin LI void     os_outb (void *port, HPT_U8 value);
13181966bceSXin LI void     os_outw (void *port, HPT_U16 value);
13281966bceSXin LI void     os_outl (void *port, HPT_U32 value);
13381966bceSXin LI void     os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
13481966bceSXin LI void     os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
13581966bceSXin LI 
13681966bceSXin LI extern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
13781966bceSXin LI 
13881966bceSXin LI #define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
13981966bceSXin LI #define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
14081966bceSXin LI #define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
14181966bceSXin LI 
14281966bceSXin LI #define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
14381966bceSXin LI #define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
14481966bceSXin LI #define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
14581966bceSXin LI 
14681966bceSXin LI /* PCI configuration space for specified device*/
14781966bceSXin LI HPT_U8   os_pci_readb (void *osext, HPT_U8 offset);
14881966bceSXin LI HPT_U16  os_pci_readw (void *osext, HPT_U8 offset);
14981966bceSXin LI HPT_U32  os_pci_readl (void *osext, HPT_U8 offset);
15081966bceSXin LI void     os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
15181966bceSXin LI void     os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
15281966bceSXin LI void     os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
15381966bceSXin LI 
15481966bceSXin LI /* obsolute interface */
15581966bceSXin LI #define MAX_PCI_BUS_NUMBER 0xff
15681966bceSXin LI #define MAX_PCI_DEVICE_NUMBER 32
15781966bceSXin LI #define MAX_PCI_FUNC_NUMBER 1
15881966bceSXin LI HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
15981966bceSXin LI HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
16081966bceSXin LI void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
16181966bceSXin LI void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
16281966bceSXin LI 
16381966bceSXin LI 
16481966bceSXin LI void *os_map_pci_bar(
16581966bceSXin LI 	void *osext,
16681966bceSXin LI 	int index,
16781966bceSXin LI 	HPT_U32 offset,
16881966bceSXin LI 	HPT_U32 length
16981966bceSXin LI );
17081966bceSXin LI 
17181966bceSXin LI 
17281966bceSXin LI void os_unmap_pci_bar(void *osext, void *base);
17381966bceSXin LI 
17481966bceSXin LI #define os_kmap_sgptr(psg) (psg->addr._logical)
17581966bceSXin LI #define os_kunmap_sgptr(ptr)
17681966bceSXin LI #define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
17781966bceSXin LI 
17881966bceSXin LI /* timer */
17981966bceSXin LI void *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
18081966bceSXin LI void  os_del_timer(void *handle);
18181966bceSXin LI void  os_request_timer(void * osext, HPT_U32 interval);
18281966bceSXin LI HPT_TIME os_query_time(void);
18381966bceSXin LI 
18481966bceSXin LI /* task */
18581966bceSXin LI #define OS_SUPPORT_TASK
18681966bceSXin LI 
18781966bceSXin LI typedef struct _OSM_TASK {
18881966bceSXin LI 	struct _OSM_TASK *next;
18981966bceSXin LI 	void (*func)(void *vbus, void *data);
19081966bceSXin LI 	void *data;
19181966bceSXin LI }
19281966bceSXin LI OSM_TASK;
19381966bceSXin LI 
19481966bceSXin LI void os_schedule_task(void *osext, OSM_TASK *task);
19581966bceSXin LI 
19681966bceSXin LI /* misc */
19781966bceSXin LI HPT_U32 os_get_stamp(void);
19881966bceSXin LI void os_stallexec(HPT_U32 microseconds);
19981966bceSXin LI 
20081966bceSXin LI #ifndef _SYS_LIBKERN_H_
20181966bceSXin LI #define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
20281966bceSXin LI #define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
20381966bceSXin LI #define strcpy(dst, src) __builtin_strcpy((dst), (src))
memset(void * dst,int c,unsigned long size)20481966bceSXin LI static __inline void * memset(void *dst, int c, unsigned long size)
20581966bceSXin LI {
20681966bceSXin LI 	char *p;
20781966bceSXin LI 	for (p=(char*)dst; size; size--,p++) *p = c;
20881966bceSXin LI 	return dst;
20981966bceSXin LI }
21081966bceSXin LI #endif
21181966bceSXin LI 
21281966bceSXin LI #define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
21381966bceSXin LI 
21481966bceSXin LI 
21581966bceSXin LI #define os_register_device(osext, target_id)
21681966bceSXin LI #define os_unregister_device(osext, target_id)
21781966bceSXin LI int os_query_remove_device(void *osext, int target_id);
21881966bceSXin LI int os_revalidate_device(void *osext, int target_id);
21981966bceSXin LI 
22081966bceSXin LI HPT_U8 os_get_vbus_seq(void *osext);
22181966bceSXin LI 
22281966bceSXin LI /* debug support */
22381966bceSXin LI int  os_printk(char *fmt, ...);
22481966bceSXin LI 
22581966bceSXin LI #if DBG
22681966bceSXin LI extern int hpt_dbg_level;
227f29c86f1SXin LI #define dbg_printk(fmt, args...) \
228f29c86f1SXin LI 	do {\
229f29c86f1SXin LI 		char *__p = fmt;\
230f29c86f1SXin LI 		int __level;\
231f29c86f1SXin LI 		if (__p[0]=='<' && __p[2]=='>') {\
232f29c86f1SXin LI 			__level = __p[1] - '0';\
233f29c86f1SXin LI 			__p += 3;\
234f29c86f1SXin LI 		} else\
235f29c86f1SXin LI 			__level = 7;\
236f29c86f1SXin LI 		if (hpt_dbg_level >= __level)\
237f29c86f1SXin LI 			os_printk(__p, ##args);\
238f29c86f1SXin LI 	} while (0)
239f29c86f1SXin LI 
240f29c86f1SXin LI #define KdPrint(x)  do { dbg_printk x; } while (0)
24181966bceSXin LI void __os_dbgbreak(const char *file, int line);
24281966bceSXin LI #define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
24381966bceSXin LI #define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
24481966bceSXin LI void os_check_stack(const char *location, int size);
24581966bceSXin LI #define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
24681966bceSXin LI #else
24781966bceSXin LI #define KdPrint(x)
24881966bceSXin LI #define HPT_ASSERT(x)
24981966bceSXin LI #define HPT_CHECK_STACK(size)
25081966bceSXin LI #endif
25181966bceSXin LI 
25281966bceSXin LI #define OsPrint(x) do { os_printk x; } while (0)
25381966bceSXin LI 
25481966bceSXin LI #endif
255