1228940Sdelphij/*- 2228940Sdelphij * Copyright (c) 2011 HighPoint Technologies, Inc. 3228940Sdelphij * All rights reserved. 4228940Sdelphij * 5228940Sdelphij * Redistribution and use in source and binary forms, with or without 6228940Sdelphij * modification, are permitted provided that the following conditions 7228940Sdelphij * are met: 8228940Sdelphij * 1. Redistributions of source code must retain the above copyright 9228940Sdelphij * notice, this list of conditions and the following disclaimer. 10228940Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 11228940Sdelphij * notice, this list of conditions and the following disclaimer in the 12228940Sdelphij * documentation and/or other materials provided with the distribution. 13228940Sdelphij * 14228940Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15228940Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16228940Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17228940Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18228940Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19228940Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20228940Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21228940Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22228940Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23228940Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24228940Sdelphij * SUCH DAMAGE. 25228940Sdelphij * 26228940Sdelphij * $FreeBSD$ 27228940Sdelphij */ 28228940Sdelphij 29228940Sdelphij#include <dev/hpt27xx/hpt27xx_config.h> 30228940Sdelphij 31228940Sdelphij#ifndef _HPT_OSM_H_ 32228940Sdelphij#define _HPT_OSM_H_ 33228940Sdelphij 34228940Sdelphij#define VERMAGIC_OSM 6 35228940Sdelphij 36228940Sdelphij 37228940Sdelphij#define MAX_MEMBERS 64 38228940Sdelphij 39228940Sdelphij#define os_max_queue_comm 32 40228940Sdelphij#define os_max_sg_descriptors 18 41228940Sdelphij 42228940Sdelphij 43228940Sdelphijextern int os_max_cache_size; 44228940Sdelphij 45228940Sdelphij 46228940Sdelphij#define DMAPOOL_PAGE_SIZE 0x1000 /* PAGE_SIZE (i386/x86_64) */ 47228940Sdelphij#define os_max_cache_pages (os_max_cache_size/DMAPOOL_PAGE_SIZE) 48228940Sdelphij 49228940Sdelphij/* data types */ 50228940Sdelphijtypedef unsigned int HPT_UINT, HPT_U32; 51228940Sdelphijtypedef unsigned long HPT_UPTR; 52228940Sdelphijtypedef unsigned short HPT_U16; 53228940Sdelphijtypedef unsigned char HPT_U8; 54228940Sdelphijtypedef unsigned long HPT_TIME; 55228940Sdelphijtypedef unsigned long long HPT_U64; 56228940Sdelphij 57228940Sdelphij#define CPU_TO_LE64(x) (x) 58228940Sdelphij#define CPU_TO_LE32(x) (x) 59228940Sdelphij#define CPU_TO_LE16(x) (x) 60228940Sdelphij#define LE32_TO_CPU(x) (x) 61228940Sdelphij#define LE16_TO_CPU(x) (x) 62228940Sdelphij#define LE64_TO_CPU(x) (x) 63228940Sdelphij 64228940Sdelphijstatic __inline HPT_U64 CPU_TO_BE64(HPT_U64 x) 65228940Sdelphij{ 66228940Sdelphij HPT_U8 *p = (HPT_U8 *)&x; 67228940Sdelphij return ((HPT_U64)p[0] << 56) | 68228940Sdelphij ((HPT_U64)p[1] << 48) | 69228940Sdelphij ((HPT_U64)p[2] << 40) | 70228940Sdelphij ((HPT_U64)p[3] << 32) | 71228940Sdelphij ((HPT_U64)p[4] << 24) | 72228940Sdelphij ((HPT_U64)p[5] << 16) | 73228940Sdelphij ((HPT_U64)p[6] << 8) | 74228940Sdelphij p[7]; 75228940Sdelphij} 76228940Sdelphij 77228940Sdelphijstatic __inline HPT_U32 CPU_TO_BE32(HPT_U32 x) 78228940Sdelphij{ 79228940Sdelphij HPT_U8 *p = (HPT_U8 *)&x; 80228940Sdelphij return ((HPT_U32)p[0] << 24) | 81228940Sdelphij ((HPT_U32)p[1] << 16) | 82228940Sdelphij ((HPT_U32)p[2] << 8) | p[3]; 83228940Sdelphij} 84228940Sdelphij 85228940Sdelphijstatic __inline HPT_U16 CPU_TO_BE16(HPT_U16 x) 86228940Sdelphij{ 87228940Sdelphij return ((HPT_U8)x << 8) | (x>>8); 88228940Sdelphij} 89228940Sdelphij 90228940Sdelphij#define BE16_TO_CPU(x) CPU_TO_BE16(x) 91228940Sdelphij#define BE32_TO_CPU(x) CPU_TO_BE32(x) 92228940Sdelphij#define BE64_TO_CPU(x) CPU_TO_BE64(x) 93228940Sdelphij 94228940Sdelphij#define FAR 95228940Sdelphij#define EXTERN_C 96228940Sdelphij 97228940Sdelphijtypedef void * HPT_PTR; 98228940Sdelphij 99228940Sdelphijtypedef HPT_U64 HPT_LBA; 100228940Sdelphijtypedef HPT_U64 HPT_RAW_LBA; 101228940Sdelphij#define MAX_LBA_VALUE 0xffffffffffffffffull 102228940Sdelphij#define MAX_RAW_LBA_VALUE MAX_LBA_VALUE 103228940Sdelphij#define RAW_LBA(x) (x) 104228940Sdelphij#define LO_LBA(x) ((HPT_U32)(x)) 105228940Sdelphij#define HI_LBA(x) (sizeof(HPT_LBA)>4? (HPT_U32)((x)>>32) : 0) 106228940Sdelphij#define LBA_FORMAT_STR "0x%llX" 107228940Sdelphij 108228940Sdelphijtypedef HPT_U64 BUS_ADDRESS; 109228940Sdelphij#define LO_BUSADDR(x) ((HPT_U32)(x)) 110228940Sdelphij#define HI_BUSADDR(x) (sizeof(BUS_ADDRESS)>4? (x)>>32 : 0) 111228940Sdelphij 112228940Sdelphijtypedef unsigned char HPT_BOOL; 113228940Sdelphij#define HPT_TRUE 1 114228940Sdelphij#define HPT_FALSE 0 115228940Sdelphij 116228940Sdelphijtypedef struct _TIME_RECORD { 117228940Sdelphij HPT_U32 seconds:6; /* 0 - 59 */ 118228940Sdelphij HPT_U32 minutes:6; /* 0 - 59 */ 119228940Sdelphij HPT_U32 month:4; /* 1 - 12 */ 120228940Sdelphij HPT_U32 hours:6; /* 0 - 59 */ 121228940Sdelphij HPT_U32 day:5; /* 1 - 31 */ 122228940Sdelphij HPT_U32 year:5; /* 0=2000, 31=2031 */ 123228940Sdelphij} TIME_RECORD; 124228940Sdelphij 125228940Sdelphij/* hardware access */ 126228940SdelphijHPT_U8 os_inb (void *port); 127228940SdelphijHPT_U16 os_inw (void *port); 128228940SdelphijHPT_U32 os_inl (void *port); 129228940Sdelphijvoid os_outb (void *port, HPT_U8 value); 130228940Sdelphijvoid os_outw (void *port, HPT_U16 value); 131228940Sdelphijvoid os_outl (void *port, HPT_U32 value); 132228940Sdelphijvoid os_insw (void *port, HPT_U16 *buffer, HPT_U32 count); 133228940Sdelphijvoid os_outsw(void *port, HPT_U16 *buffer, HPT_U32 count); 134228940Sdelphij 135228940Sdelphijextern HPT_U32 __dummy_reg; /* to avoid the compiler warning */ 136228940Sdelphij 137228940Sdelphij#define os_readb(addr) (*(HPT_U8 *)&__dummy_reg = *(volatile HPT_U8 *)(addr)) 138228940Sdelphij#define os_readw(addr) (*(HPT_U16 *)&__dummy_reg = *(volatile HPT_U16 *)(addr)) 139228940Sdelphij#define os_readl(addr) (*(HPT_U32 *)&__dummy_reg = *(volatile HPT_U32 *)(addr)) 140228940Sdelphij 141228940Sdelphij#define os_writeb(addr, val) *(volatile HPT_U8 *)(addr) = (HPT_U8)(val) 142228940Sdelphij#define os_writew(addr, val) *(volatile HPT_U16 *)(addr) = (HPT_U16)(val) 143228940Sdelphij#define os_writel(addr, val) *(volatile HPT_U32 *)(addr) = (HPT_U32)(val) 144228940Sdelphij 145228940Sdelphij/* PCI configuration space for specified device*/ 146228940SdelphijHPT_U8 os_pci_readb (void *osext, HPT_U8 offset); 147228940SdelphijHPT_U16 os_pci_readw (void *osext, HPT_U8 offset); 148228940SdelphijHPT_U32 os_pci_readl (void *osext, HPT_U8 offset); 149228940Sdelphijvoid os_pci_writeb(void *osext, HPT_U8 offset, HPT_U8 value); 150228940Sdelphijvoid os_pci_writew(void *osext, HPT_U8 offset, HPT_U16 value); 151228940Sdelphijvoid os_pci_writel(void *osext, HPT_U8 offset, HPT_U32 value); 152228940Sdelphij 153228940Sdelphij/* obsolute interface */ 154228940Sdelphij#define MAX_PCI_BUS_NUMBER 0xff 155228940Sdelphij#define MAX_PCI_DEVICE_NUMBER 32 156228940Sdelphij#define MAX_PCI_FUNC_NUMBER 1 157228940SdelphijHPT_U8 pcicfg_read_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg); 158228940SdelphijHPT_U32 pcicfg_read_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg); 159228940Sdelphijvoid pcicfg_write_byte (HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U8 v); 160228940Sdelphijvoid pcicfg_write_dword(HPT_U8 bus, HPT_U8 dev, HPT_U8 func, HPT_U8 reg, HPT_U32 v); 161228940Sdelphij 162228940Sdelphij 163228940Sdelphijvoid *os_map_pci_bar( 164228940Sdelphij void *osext, 165228940Sdelphij int index, 166228940Sdelphij HPT_U32 offset, 167228940Sdelphij HPT_U32 length 168228940Sdelphij); 169228940Sdelphij 170228940Sdelphij 171228940Sdelphijvoid os_unmap_pci_bar(void *osext, void *base); 172228940Sdelphij 173228940Sdelphij#define os_kmap_sgptr(psg) (psg->addr._logical) 174228940Sdelphij#define os_kunmap_sgptr(ptr) 175228940Sdelphij#define os_set_sgptr(psg, ptr) (psg)->addr._logical = (ptr) 176228940Sdelphij 177228940Sdelphij/* timer */ 178228940Sdelphijvoid *os_add_timer(void *osext, HPT_U32 microseconds, void (*proc)(void *), void *arg); 179228940Sdelphijvoid os_del_timer(void *handle); 180228940Sdelphijvoid os_request_timer(void * osext, HPT_U32 interval); 181228940SdelphijHPT_TIME os_query_time(void); 182228940Sdelphij 183228940Sdelphij/* task */ 184228940Sdelphij#define OS_SUPPORT_TASK 185228940Sdelphij 186228940Sdelphijtypedef struct _OSM_TASK { 187228940Sdelphij struct _OSM_TASK *next; 188228940Sdelphij void (*func)(void *vbus, void *data); 189228940Sdelphij void *data; 190228940Sdelphij} 191228940SdelphijOSM_TASK; 192228940Sdelphij 193228940Sdelphijvoid os_schedule_task(void *osext, OSM_TASK *task); 194228940Sdelphij 195228940Sdelphij/* misc */ 196228940SdelphijHPT_U32 os_get_stamp(void); 197228940Sdelphijvoid os_stallexec(HPT_U32 microseconds); 198228940Sdelphij 199228940Sdelphij#ifndef _SYS_LIBKERN_H_ 200228940Sdelphij#define memcpy(dst, src, size) __builtin_memcpy((dst), (src), (size)) 201228940Sdelphij#define memcmp(dst, src, size) __builtin_memcmp((dst), (src), (size)) 202228940Sdelphij#define strcpy(dst, src) __builtin_strcpy((dst), (src)) 203228940Sdelphijstatic __inline void * memset(void *dst, int c, unsigned long size) 204228940Sdelphij{ 205228940Sdelphij char *p; 206228940Sdelphij for (p=(char*)dst; size; size--,p++) *p = c; 207228940Sdelphij return dst; 208228940Sdelphij} 209228940Sdelphij#endif 210228940Sdelphij 211228940Sdelphij#define farMemoryCopy(a,b,c) memcpy((char *)(a), (char *)(b), (HPT_U32)c) 212228940Sdelphij 213228940Sdelphij 214228940Sdelphij#define os_register_device(osext, target_id) 215228940Sdelphij#define os_unregister_device(osext, target_id) 216228940Sdelphijint os_query_remove_device(void *osext, int target_id); 217228940Sdelphijint os_revalidate_device(void *osext, int target_id); 218228940Sdelphij 219228940SdelphijHPT_U8 os_get_vbus_seq(void *osext); 220228940Sdelphij 221228940Sdelphij/* debug support */ 222228940Sdelphijint os_printk(char *fmt, ...); 223228940Sdelphij 224228940Sdelphij#if DBG 225228940Sdelphijextern int hpt_dbg_level; 226228940Sdelphij#define KdPrint(x) do { if (hpt_dbg_level) os_printk x; } while (0) 227228940Sdelphijvoid __os_dbgbreak(const char *file, int line); 228228940Sdelphij#define os_dbgbreak() __os_dbgbreak(__FILE__, __LINE__) 229228940Sdelphij#define HPT_ASSERT(x) do { if (!(x)) os_dbgbreak(); } while (0) 230228940Sdelphijvoid os_check_stack(const char *location, int size); 231228940Sdelphij#define HPT_CHECK_STACK(size) os_check_stack(__FUNCTION__, (size)) 232228940Sdelphij#else 233228940Sdelphij#define KdPrint(x) 234228940Sdelphij#define HPT_ASSERT(x) 235228940Sdelphij#define HPT_CHECK_STACK(size) 236228940Sdelphij#endif 237228940Sdelphij 238228940Sdelphij#define OsPrint(x) do { os_printk x; } while (0) 239228940Sdelphij 240228940Sdelphij#endif 241