1 /* $Id: os_bsd.h,v 1.20 2010/05/11 03:12:11 lcn Exp $ */ 2 /*- 3 * HighPoint RAID Driver for FreeBSD 4 * Copyright (C) 2005-2011 HighPoint Technologies, Inc. All Rights Reserved. 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/hptnr/hptnr_config.h> 32 33 #ifndef _OS_BSD_H 34 #define _OS_BSD_H 35 36 #ifndef DBG 37 #define DBG 0 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/cons.h> 43 #if (__FreeBSD_version >= 500000) 44 #include <sys/time.h> 45 #include <sys/systm.h> 46 #else 47 #include <machine/clock.h> /*to support DELAY function under 4.x BSD versions*/ 48 #endif 49 50 #include <sys/stat.h> 51 #include <sys/malloc.h> 52 #include <sys/conf.h> 53 #include <sys/libkern.h> 54 #include <sys/kernel.h> 55 56 #if (__FreeBSD_version >= 500000) 57 #include <sys/kthread.h> 58 #include <sys/mutex.h> 59 #include <sys/module.h> 60 #endif 61 62 #include <sys/eventhandler.h> 63 #include <sys/bus.h> 64 #include <sys/taskqueue.h> 65 #include <sys/ioccom.h> 66 67 #include <machine/resource.h> 68 #if __FreeBSD_version >= 500043 69 #include <machine/pci_cfgreg.h> 70 #endif 71 #include <machine/bus.h> 72 #include <machine/stdarg.h> 73 #include <sys/rman.h> 74 75 #include <vm/vm.h> 76 #include <vm/pmap.h> 77 78 #if (__FreeBSD_version >= 500000) 79 #include <dev/pci/pcireg.h> 80 #include <dev/pci/pcivar.h> 81 #else 82 #include <pci/pcivar.h> 83 #include <pci/pcireg.h> 84 #endif 85 86 #if (__FreeBSD_version <= 500043) 87 #include <sys/devicestat.h> 88 #endif 89 90 #include <cam/cam.h> 91 #include <cam/cam_ccb.h> 92 #include <cam/cam_sim.h> 93 #include <cam/cam_xpt_sim.h> 94 #include <cam/cam_debug.h> 95 #include <cam/cam_xpt_periph.h> 96 #include <cam/cam_periph.h> 97 #include <cam/scsi/scsi_all.h> 98 #include <cam/scsi/scsi_message.h> 99 100 #if (__FreeBSD_version < 500043) 101 #include <sys/bus_private.h> 102 #endif 103 104 105 typedef struct _INQUIRYDATA { 106 u_char DeviceType : 5; 107 u_char DeviceTypeQualifier : 3; 108 u_char DeviceTypeModifier : 7; 109 u_char RemovableMedia : 1; 110 u_char Versions; 111 u_char ResponseDataFormat; 112 u_char AdditionalLength; 113 u_char Reserved[2]; 114 u_char SoftReset : 1; 115 u_char CommandQueue : 1; 116 u_char Reserved2 : 1; 117 u_char LinkedCommands : 1; 118 u_char Synchronous : 1; 119 u_char Wide16Bit : 1; 120 u_char Wide32Bit : 1; 121 u_char RelativeAddressing : 1; 122 u_char VendorId[8]; 123 u_char ProductId[16]; 124 u_char ProductRevisionLevel[4]; 125 u_char VendorSpecific[20]; 126 u_char Reserved3[40]; 127 } 128 __attribute__((packed)) 129 INQUIRYDATA, *PINQUIRYDATA; 130 131 #endif 132 133 /* private headers */ 134 135 #include <dev/hptnr/osm.h> 136 #include <dev/hptnr/him.h> 137 #include <dev/hptnr/ldm.h> 138 139 /* driver parameters */ 140 extern const char driver_name[]; 141 extern const char driver_name_long[]; 142 extern const char driver_ver[]; 143 extern int osm_max_targets; 144 145 /* 146 * adapter/vbus extensions: 147 * each physical controller has an adapter_ext, passed to him.create_adapter() 148 * each vbus has a vbus_ext passed to ldm_create_vbus(). 149 */ 150 #define EXT_TYPE_HBA 1 151 #define EXT_TYPE_VBUS 2 152 153 typedef struct _hba { 154 int ext_type; 155 LDM_ADAPTER ldm_adapter; 156 device_t pcidev; 157 PCI_ADDRESS pciaddr; 158 struct _vbus_ext *vbus_ext; 159 struct _hba *next; 160 161 struct { 162 struct resource *res; 163 int type; 164 int rid; 165 void *base; 166 } 167 pcibar[6]; 168 169 struct resource *irq_res; 170 void *irq_handle; 171 } 172 HBA, *PHBA; 173 174 typedef struct _os_cmdext { 175 struct _vbus_ext *vbus_ext; 176 struct _os_cmdext *next; 177 union ccb *ccb; 178 bus_dmamap_t dma_map; 179 struct callout_handle timeout_ch; 180 SG psg[os_max_sg_descriptors]; 181 } 182 OS_CMDEXT, *POS_CMDEXT; 183 184 typedef struct _vbus_ext { 185 int ext_type; 186 struct _vbus_ext *next; 187 PHBA hba_list; 188 struct freelist *freelist_head; 189 struct freelist *freelist_dma_head; 190 191 struct cam_sim *sim; /* sim for this vbus */ 192 struct cam_path *path; /* peripheral, path, tgt, lun with this vbus */ 193 #if (__FreeBSD_version >= 500000) 194 struct mtx lock; /* general purpose lock */ 195 #else 196 int hpt_splx; 197 #endif 198 bus_dma_tag_t io_dmat; /* I/O buffer DMA tag */ 199 200 POS_CMDEXT cmdext_list; 201 202 OSM_TASK *tasks; 203 struct task worker; 204 205 struct callout_handle timer; 206 207 eventhandler_tag shutdown_eh; 208 209 /* the LDM vbus instance continues */ 210 unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long)))); 211 } 212 VBUS_EXT, *PVBUS_EXT; 213 214 #if __FreeBSD_version >= 500000 215 #define hpt_lock_vbus(vbus_ext) mtx_lock(&(vbus_ext)->lock) 216 #define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock) 217 #else 218 static __inline void hpt_lock_vbus(PVBUS_EXT vbus_ext) 219 { 220 vbus_ext->hpt_splx = splcam(); 221 } 222 static __inline void hpt_unlock_vbus(PVBUS_EXT vbus_ext) 223 { 224 splx(vbus_ext->hpt_splx); 225 } 226 #endif 227 228 229 #define HPT_OSM_TIMEOUT (20*hz) /* timeout value for OS commands */ 230 231 #define HPT_DO_IOCONTROL _IOW('H', 0, HPT_IOCTL_PARAM) 232 233 #define HPT_SCAN_BUS _IO('H', 1) 234 235 #if __FreeBSD_version >= 501000 236 #define TASK_ENQUEUE(task) taskqueue_enqueue(taskqueue_swi_giant,(task)); 237 #else 238 #define TASK_ENQUEUE(task) taskqueue_enqueue(taskqueue_swi,(task)); 239 #endif 240 241 #if __FreeBSD_version >= 500000 242 static __inline int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo) 243 { 244 return msleep(ident, &vbus_ext->lock, priority, wmesg, timo); 245 } 246 #else 247 static __inline int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo) 248 { 249 int retval = 0; 250 251 asleep(ident, priority, wmesg, timo); 252 hpt_unlock_vbus(vbus_ext); 253 retval = await(priority, timo); 254 hpt_lock_vbus(vbus_ext); 255 256 return retval; 257 } 258 #endif 259 260 #if __FreeBSD_version < 501000 261 #define READ_16 0x88 262 #define WRITE_16 0x8a 263 #define SERVICE_ACTION_IN 0x9e 264 #endif 265 266 #define HPT_DEV_MAJOR 200 267 268