11fdeb165SXin LI /* $Id: osm.h,v 1.10 2010/05/11 10:30:33 lcn Exp $ */
21fdeb165SXin LI /*-
3*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
4718cf2ccSPedro F. Giffuni *
51fdeb165SXin LI * Copyright (C) 2005-2011 HighPoint Technologies, Inc.
61fdeb165SXin LI * All rights reserved.
71fdeb165SXin LI *
81fdeb165SXin LI * Redistribution and use in source and binary forms, with or without
91fdeb165SXin LI * modification, are permitted provided that the following conditions
101fdeb165SXin LI * are met:
111fdeb165SXin LI * 1. Redistributions of source code must retain the above copyright
121fdeb165SXin LI * notice, this list of conditions and the following disclaimer.
131fdeb165SXin LI * 2. Redistributions in binary form must reproduce the above copyright
141fdeb165SXin LI * notice, this list of conditions and the following disclaimer in the
151fdeb165SXin LI * documentation and/or other materials provided with the distribution.
161fdeb165SXin LI *
171fdeb165SXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181fdeb165SXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191fdeb165SXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201fdeb165SXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211fdeb165SXin LI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221fdeb165SXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231fdeb165SXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241fdeb165SXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251fdeb165SXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261fdeb165SXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271fdeb165SXin LI * SUCH DAMAGE.
281fdeb165SXin LI */
291fdeb165SXin LI #include <dev/hptnr/hptnr_config.h>
301fdeb165SXin LI #ifndef _HPT_OSM_H_
311fdeb165SXin LI #define _HPT_OSM_H_
321fdeb165SXin LI
331fdeb165SXin LI #define VERMAGIC_OSM 6
341fdeb165SXin LI
351fdeb165SXin LI
361fdeb165SXin LI #define MAX_MEMBERS 64
371fdeb165SXin LI
381fdeb165SXin LI #define os_max_queue_comm 32
391fdeb165SXin LI #define os_max_sg_descriptors 18
401fdeb165SXin LI
411fdeb165SXin LI
421fdeb165SXin LI extern int os_max_cache_size;
431fdeb165SXin LI
441fdeb165SXin LI
451fdeb165SXin LI #define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */
461fdeb165SXin LI #define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE)
471fdeb165SXin LI
481fdeb165SXin LI /* data types */
491fdeb165SXin LI typedef unsigned int HPT_UINT, HPT_U32;
501fdeb165SXin LI typedef unsigned long HPT_UPTR;
511fdeb165SXin LI typedef unsigned short HPT_U16;
521fdeb165SXin LI typedef unsigned char HPT_U8;
531fdeb165SXin LI typedef unsigned long HPT_TIME;
541fdeb165SXin LI typedef unsigned long long HPT_U64;
551fdeb165SXin LI
561fdeb165SXin LI #define CPU_TO_LE64(x) (x)
571fdeb165SXin LI #define CPU_TO_LE32(x) (x)
581fdeb165SXin LI #define CPU_TO_LE16(x) (x)
591fdeb165SXin LI #define LE32_TO_CPU(x) (x)
601fdeb165SXin LI #define LE16_TO_CPU(x) (x)
611fdeb165SXin LI #define LE64_TO_CPU(x) (x)
621fdeb165SXin LI
CPU_TO_BE64(HPT_U64 x)631fdeb165SXin LI static __inline HPT_U64 CPU_TO_BE64(HPT_U64 x)
641fdeb165SXin LI {
651fdeb165SXin LI HPT_U8 *p = (HPT_U8 *)&x;
661fdeb165SXin LI return ((HPT_U64)p[0] << 56) |
671fdeb165SXin LI ((HPT_U64)p[1] << 48) |
681fdeb165SXin LI ((HPT_U64)p[2] << 40) |
691fdeb165SXin LI ((HPT_U64)p[3] << 32) |
701fdeb165SXin LI ((HPT_U64)p[4] << 24) |
711fdeb165SXin LI ((HPT_U64)p[5] << 16) |
721fdeb165SXin LI ((HPT_U64)p[6] << 8) |
731fdeb165SXin LI p[7];
741fdeb165SXin LI }
751fdeb165SXin LI
CPU_TO_BE32(HPT_U32 x)761fdeb165SXin LI static __inline HPT_U32 CPU_TO_BE32(HPT_U32 x)
771fdeb165SXin LI {
781fdeb165SXin LI HPT_U8 *p = (HPT_U8 *)&x;
791fdeb165SXin LI return ((HPT_U32)p[0] << 24) |
801fdeb165SXin LI ((HPT_U32)p[1] << 16) |
811fdeb165SXin LI ((HPT_U32)p[2] << 8) | p[3];
821fdeb165SXin LI }
831fdeb165SXin LI
CPU_TO_BE16(HPT_U16 x)841fdeb165SXin LI static __inline HPT_U16 CPU_TO_BE16(HPT_U16 x)
851fdeb165SXin LI {
861fdeb165SXin LI return ((HPT_U8)x << 8) | (x>>8);
871fdeb165SXin LI }
881fdeb165SXin LI
891fdeb165SXin LI #define BE16_TO_CPU(x) CPU_TO_BE16(x)
901fdeb165SXin LI #define BE32_TO_CPU(x) CPU_TO_BE32(x)
911fdeb165SXin LI #define BE64_TO_CPU(x) CPU_TO_BE64(x)
921fdeb165SXin LI
931fdeb165SXin LI #define FAR
941fdeb165SXin LI #define EXTERN_C
951fdeb165SXin LI
961fdeb165SXin LI typedef void * HPT_PTR;
971fdeb165SXin LI
981fdeb165SXin LI typedef HPT_U64 HPT_LBA;
991fdeb165SXin LI typedef HPT_U64 HPT_RAW_LBA;
1001fdeb165SXin LI #define MAX_LBA_VALUE 0xffffffffffffffffull
1011fdeb165SXin LI #define MAX_RAW_LBA_VALUE MAX_LBA_VALUE
1021fdeb165SXin LI #define RAW_LBA(x) (x)
1031fdeb165SXin LI #define LO_LBA(x) ((HPT_U32)(x))
1041fdeb165SXin LI #define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0)
1051fdeb165SXin LI #define LBA_FORMAT_STR "0x%llX"
1061fdeb165SXin LI
1071fdeb165SXin LI typedef HPT_U64 BUS_ADDRESS;
1081fdeb165SXin LI #define LO_BUSADDR(x) ((HPT_U32)(x))
1091fdeb165SXin LI #define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0)
1101fdeb165SXin LI
1111fdeb165SXin LI typedef unsigned char HPT_BOOL;
1121fdeb165SXin LI #define HPT_TRUE 1
1131fdeb165SXin LI #define HPT_FALSE 0
1141fdeb165SXin LI
1151fdeb165SXin LI typedef struct _TIME_RECORD {
1161fdeb165SXin LI HPT_U32 seconds:6; /* 0 - 59 */
1171fdeb165SXin LI HPT_U32 minutes:6; /* 0 - 59 */
1181fdeb165SXin LI HPT_U32 month:4; /* 1 - 12 */
1191fdeb165SXin LI HPT_U32 hours:6; /* 0 - 59 */
1201fdeb165SXin LI HPT_U32 day:5; /* 1 - 31 */
1211fdeb165SXin LI HPT_U32 year:5; /* 0=2000, 31=2031 */
1221fdeb165SXin LI } TIME_RECORD;
1231fdeb165SXin LI
1241fdeb165SXin LI /* hardware access */
1251fdeb165SXin LI HPT_U8 os_inb (void *port);
1261fdeb165SXin LI HPT_U16 os_inw (void *port);
1271fdeb165SXin LI HPT_U32 os_inl (void *port);
1281fdeb165SXin LI void os_outb (void *port, HPT_U8 value);
1291fdeb165SXin LI void os_outw (void *port, HPT_U16 value);
1301fdeb165SXin LI void os_outl (void *port, HPT_U32 value);
1311fdeb165SXin LI void os_insw (void *port, HPT_U16 *buffer, HPT_U32 count);
1321fdeb165SXin LI void os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count);
1331fdeb165SXin LI
1341fdeb165SXin LI extern HPT_U32 __dummy_reg; /* to avoid the compiler warning */
1351fdeb165SXin LI
1361fdeb165SXin LI #define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr))
1371fdeb165SXin LI #define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr))
1381fdeb165SXin LI #define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr))
1391fdeb165SXin LI
1401fdeb165SXin LI #define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val)
1411fdeb165SXin LI #define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val)
1421fdeb165SXin LI #define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val)
1431fdeb165SXin LI
1441fdeb165SXin LI /* PCI configuration space for specified device*/
1451fdeb165SXin LI HPT_U8 os_pci_readb (void *osext, HPT_U8 offset);
1461fdeb165SXin LI HPT_U16 os_pci_readw (void *osext, HPT_U8 offset);
1471fdeb165SXin LI HPT_U32 os_pci_readl (void *osext, HPT_U8 offset);
1481fdeb165SXin LI void os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value);
1491fdeb165SXin LI void os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value);
1501fdeb165SXin LI void os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value);
1511fdeb165SXin LI
1521fdeb165SXin LI /* obsolute interface */
1531fdeb165SXin LI #define MAX_PCI_BUS_NUMBER 0xff
1541fdeb165SXin LI #define MAX_PCI_DEVICE_NUMBER 32
1551fdeb165SXin LI #define MAX_PCI_FUNC_NUMBER 1
1567d379626SXin LI HPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
1571fdeb165SXin LI HPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg);
1587d379626SXin LI void pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v);
1597d379626SXin LI void pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v);
1601fdeb165SXin LI
1611fdeb165SXin LI
1621fdeb165SXin LI void *os_map_pci_bar(
1631fdeb165SXin LI void *osext,
1641fdeb165SXin LI int index,
1651fdeb165SXin LI HPT_U32 offset,
1661fdeb165SXin LI HPT_U32 length
1671fdeb165SXin LI );
1681fdeb165SXin LI
1691fdeb165SXin LI
1701fdeb165SXin LI void os_unmap_pci_bar(void *osext, void *base);
1711fdeb165SXin LI
1721fdeb165SXin LI #define os_kmap_sgptr(psg) (psg->addr._logical)
1731fdeb165SXin LI #define os_kunmap_sgptr(ptr)
1741fdeb165SXin LI #define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr)
1751fdeb165SXin LI
1761fdeb165SXin LI /* timer */
1771fdeb165SXin LI void *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg);
1781fdeb165SXin LI void os_del_timer(void *handle);
1791fdeb165SXin LI void os_request_timer(void * osext, HPT_U32 interval);
1801fdeb165SXin LI HPT_TIME os_query_time(void);
1811fdeb165SXin LI
1821fdeb165SXin LI /* task */
1831fdeb165SXin LI #define OS_SUPPORT_TASK
1841fdeb165SXin LI
1851fdeb165SXin LI typedef struct _OSM_TASK {
1861fdeb165SXin LI struct _OSM_TASK *next;
1871fdeb165SXin LI void (*func)(void *vbus, void *data);
1881fdeb165SXin LI void *data;
1891fdeb165SXin LI }
1901fdeb165SXin LI OSM_TASK;
1911fdeb165SXin LI
1921fdeb165SXin LI void os_schedule_task(void *osext, OSM_TASK *task);
1931fdeb165SXin LI
1941fdeb165SXin LI /* misc */
1951fdeb165SXin LI HPT_U32 os_get_stamp(void);
1961fdeb165SXin LI void os_stallexec(HPT_U32 microseconds);
1971fdeb165SXin LI
1981fdeb165SXin LI #ifndef _SYS_LIBKERN_H_
1991fdeb165SXin LI #define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size))
2001fdeb165SXin LI #define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size))
2011fdeb165SXin LI #define strcpy(dst, src) __builtin_strcpy((dst), (src))
memset(void * dst,int c,unsigned long size)2021fdeb165SXin LI static __inline void * memset(void *dst, int c, unsigned long size)
2031fdeb165SXin LI {
2041fdeb165SXin LI char *p;
2051fdeb165SXin LI for (p=(char*)dst; size; size--,p++) *p = c;
2061fdeb165SXin LI return dst;
2071fdeb165SXin LI }
2081fdeb165SXin LI #endif
2091fdeb165SXin LI
2101fdeb165SXin LI #define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c)
2111fdeb165SXin LI
2121fdeb165SXin LI
2131fdeb165SXin LI #define os_register_device(osext, target_id)
2141fdeb165SXin LI #define os_unregister_device(osext, target_id)
2151fdeb165SXin LI int os_query_remove_device(void *osext, int target_id);
2161fdeb165SXin LI int os_revalidate_device(void *osext, int target_id);
2171fdeb165SXin LI
2181fdeb165SXin LI HPT_U8 os_get_vbus_seq(void *osext);
2191fdeb165SXin LI
2201fdeb165SXin LI /* debug support */
2211fdeb165SXin LI int os_printk(char *fmt, ...);
2221fdeb165SXin LI
2231fdeb165SXin LI #if DBG
2241fdeb165SXin LI extern int hpt_dbg_level;
2251fdeb165SXin LI #define KdPrint(x) do { if (hpt_dbg_level) os_printk x; } while (0)
2261fdeb165SXin LI void __os_dbgbreak(const char *file, int line);
2271fdeb165SXin LI #define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__)
2281fdeb165SXin LI #define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0)
2291fdeb165SXin LI void os_check_stack(const char *location, int size);
2301fdeb165SXin LI #define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size))
2311fdeb165SXin LI #else
2321fdeb165SXin LI #define KdPrint(x)
2331fdeb165SXin LI #define HPT_ASSERT(x)
2341fdeb165SXin LI #define HPT_CHECK_STACK(size)
2351fdeb165SXin LI #endif
2361fdeb165SXin LI
2371fdeb165SXin LI #define OsPrint(x) do { os_printk x; } while (0)
2381fdeb165SXin LI
2391fdeb165SXin LI #endif
240