1 /* $FreeBSD$ */ 2 /* 3 * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions 4 * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice immediately at the beginning of the file, without modification, 11 * this list of conditions, and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #ifndef _ISP_FREEBSD_H 28 #define _ISP_FREEBSD_H 29 30 #define ISP_PLATFORM_VERSION_MAJOR 5 31 #define ISP_PLATFORM_VERSION_MINOR 9 32 33 #include <sys/param.h> 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/queue.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/condvar.h> 42 #include <sys/proc.h> 43 #include <sys/bus.h> 44 45 #include <machine/bus_memio.h> 46 #include <machine/bus_pio.h> 47 #include <machine/bus.h> 48 #include <machine/clock.h> 49 #include <machine/cpu.h> 50 51 #include <cam/cam.h> 52 #include <cam/cam_debug.h> 53 #include <cam/cam_ccb.h> 54 #include <cam/cam_sim.h> 55 #include <cam/cam_xpt.h> 56 #include <cam/cam_xpt_sim.h> 57 #include <cam/cam_debug.h> 58 #include <cam/scsi/scsi_all.h> 59 #include <cam/scsi/scsi_message.h> 60 61 #include "opt_ddb.h" 62 #include "opt_isp.h" 63 64 #define HANDLE_LOOPSTATE_IN_OUTER_LAYERS 1 65 66 typedef void ispfwfunc __P((int, int, int, const u_int16_t **)); 67 68 #ifdef ISP_TARGET_MODE 69 typedef struct tstate { 70 struct tstate *next; 71 struct cam_path *owner; 72 struct ccb_hdr_slist atios; 73 struct ccb_hdr_slist inots; 74 lun_id_t lun; 75 int bus; 76 u_int32_t hold; 77 } tstate_t; 78 79 #define LUN_HASH_SIZE 32 80 #define LUN_HASH_FUNC(isp, port, lun) \ 81 ((IS_DUALBUS(isp)) ? \ 82 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) : \ 83 ((lun) & (LUN_HASH_SIZE - 1))) 84 #endif 85 86 struct isposinfo { 87 struct ispsoftc * next; 88 u_int64_t default_port_wwn; 89 u_int64_t default_node_wwn; 90 device_t dev; 91 struct cam_sim *sim; 92 struct cam_path *path; 93 struct cam_sim *sim2; 94 struct cam_path *path2; 95 struct intr_config_hook ehook; 96 u_int8_t mboxwaiting; 97 u_int8_t simqfrozen; 98 u_int8_t drain; 99 u_int8_t intsok; 100 struct mtx lock; 101 struct cv kthread_cv; 102 struct proc *kproc; 103 #ifdef ISP_TARGET_MODE 104 #define TM_WANTED 0x80 105 #define TM_BUSY 0x40 106 #define TM_WILDCARD_ENABLED 0x02 107 #define TM_TMODE_ENABLED 0x01 108 struct cv tgtcv0[2]; /* two busses */ 109 struct cv tgtcv1[2]; /* two busses */ 110 u_int8_t tmflags[2]; /* two busses */ 111 u_int8_t rstatus[2]; /* two bussed */ 112 u_int16_t rollinfo; 113 tstate_t tsdflt[2]; /* two busses */ 114 tstate_t *lun_hash[LUN_HASH_SIZE]; 115 #endif 116 }; 117 118 #define isp_lock isp_osinfo.lock 119 120 /* 121 * Locking macros... 122 */ 123 124 #define ISP_LOCK(x) mtx_lock(&(x)->isp_lock) 125 #define ISP_UNLOCK(x) mtx_unlock(&(x)->isp_lock) 126 #define ISPLOCK_2_CAMLOCK(isp) \ 127 mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant) 128 #define CAMLOCK_2_ISPLOCK(isp) \ 129 mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock) 130 131 /* 132 * Required Macros/Defines 133 */ 134 135 #define INLINE __inline 136 137 #define ISP2100_SCRLEN 0x400 138 139 #define MEMZERO bzero 140 #define MEMCPY(dst, src, amt) bcopy((src), (dst), (amt)) 141 #define SNPRINTF snprintf 142 #define STRNCAT strncat 143 #define USEC_DELAY DELAY 144 #define USEC_SLEEP(isp, x) \ 145 if (isp->isp_osinfo.intsok) \ 146 ISP_UNLOCK(isp); \ 147 DELAY(x); \ 148 if (isp->isp_osinfo.intsok) \ 149 ISP_LOCK(isp) 150 151 #define NANOTIME_T struct timespec 152 #define GET_NANOTIME nanotime 153 #define GET_NANOSEC(x) ((x)->tv_sec * 1000000000 + (x)->tv_nsec) 154 #define NANOTIME_SUB nanotime_sub 155 156 #define MAXISPREQUEST(isp) 256 157 158 #ifdef __alpha__ 159 #define MEMORYBARRIER(isp, type, offset, size) alpha_mb() 160 #else 161 #define MEMORYBARRIER(isp, type, offset, size) 162 #endif 163 164 #define MBOX_ACQUIRE(isp) 165 #define MBOX_WAIT_COMPLETE isp_mbox_wait_complete 166 #define MBOX_NOTIFY_COMPLETE(isp) \ 167 if (isp->isp_osinfo.mboxwaiting) { \ 168 isp->isp_osinfo.mboxwaiting = 0; \ 169 wakeup(&isp->isp_osinfo.mboxwaiting); \ 170 } \ 171 isp->isp_mboxbsy = 0 172 #define MBOX_RELEASE(isp) 173 174 #ifndef SCSI_GOOD 175 #define SCSI_GOOD SCSI_STATUS_OK 176 #endif 177 #ifndef SCSI_CHECK 178 #define SCSI_CHECK SCSI_STATUS_CHECK_COND 179 #endif 180 #ifndef SCSI_BUSY 181 #define SCSI_BUSY SCSI_STATUS_BUSY 182 #endif 183 #ifndef SCSI_QFULL 184 #define SCSI_QFULL SCSI_STATUS_QUEUE_FULL 185 #endif 186 187 #define XS_T struct ccb_scsiio 188 #define XS_ISP(ccb) ((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1) 189 #define XS_CHANNEL(ccb) cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)) 190 #define XS_TGT(ccb) (ccb)->ccb_h.target_id 191 #define XS_LUN(ccb) (ccb)->ccb_h.target_lun 192 193 #define XS_CDBP(ccb) \ 194 (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \ 195 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes) 196 197 #define XS_CDBLEN(ccb) (ccb)->cdb_len 198 #define XS_XFRLEN(ccb) (ccb)->dxfer_len 199 #define XS_TIME(ccb) (ccb)->ccb_h.timeout 200 #define XS_RESID(ccb) (ccb)->resid 201 #define XS_STSP(ccb) (&(ccb)->scsi_status) 202 #define XS_SNSP(ccb) (&(ccb)->sense_data) 203 204 #define XS_SNSLEN(ccb) \ 205 imin((sizeof((ccb)->sense_data)), ccb->sense_len) 206 207 #define XS_SNSKEY(ccb) ((ccb)->sense_data.flags & 0xf) 208 #define XS_TAG_P(ccb) \ 209 (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \ 210 (ccb)->tag_action != CAM_TAG_ACTION_NONE) 211 212 #define XS_TAG_TYPE(ccb) \ 213 ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \ 214 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG)) 215 216 217 #define XS_SETERR(ccb, v) (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \ 218 (ccb)->ccb_h.status |= v, \ 219 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET 220 221 # define HBA_NOERROR CAM_REQ_INPROG 222 # define HBA_BOTCH CAM_UNREC_HBA_ERROR 223 # define HBA_CMDTIMEOUT CAM_CMD_TIMEOUT 224 # define HBA_SELTIMEOUT CAM_SEL_TIMEOUT 225 # define HBA_TGTBSY CAM_SCSI_STATUS_ERROR 226 # define HBA_BUSRESET CAM_SCSI_BUS_RESET 227 # define HBA_ABORTED CAM_REQ_ABORTED 228 # define HBA_DATAOVR CAM_DATA_RUN_ERR 229 # define HBA_ARQFAIL CAM_AUTOSENSE_FAIL 230 231 232 #define XS_ERR(ccb) ((ccb)->ccb_h.status & CAM_STATUS_MASK) 233 234 #define XS_NOERR(ccb) \ 235 (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \ 236 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) 237 238 #define XS_INITERR(ccb) \ 239 XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0 240 241 #define XS_SAVE_SENSE(xs, sp) \ 242 (xs)->ccb_h.status |= CAM_AUTOSNS_VALID, \ 243 bcopy(sp->req_sense_data, &(xs)->sense_data, \ 244 imin(XS_SNSLEN(xs), sp->req_sense_len)) 245 246 #define XS_SET_STATE_STAT(a, b, c) 247 248 #define DEFAULT_IID(x) 7 249 #define DEFAULT_LOOPID(x) 109 250 #define DEFAULT_NODEWWN(isp) (isp)->isp_osinfo.default_node_wwn 251 #define DEFAULT_PORTWWN(isp) (isp)->isp_osinfo.default_port_wwn 252 #define ISP_NODEWWN(isp) FCPARAM(isp)->isp_nodewwn 253 #define ISP_PORTWWN(isp) FCPARAM(isp)->isp_portwwn 254 255 #define ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src) \ 256 if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t)) 257 #define ISP_SWIZZLE_ICB(a, b) 258 #define ISP_SWIZZLE_REQUEST(a, b) 259 #define ISP_UNSWIZZLE_RESPONSE(a, b, c) 260 #define ISP_SWIZZLE_SNS_REQ(a, b) 261 #define ISP_UNSWIZZLE_SNS_RSP(a, b, c) 262 #define ISP_SWIZZLE_NVRAM_WORD(isp, x) 263 264 /* 265 * Includes of common header files 266 */ 267 268 #include <dev/isp/ispreg.h> 269 #include <dev/isp/ispvar.h> 270 #include <dev/isp/ispmbox.h> 271 272 /* 273 * isp_osinfo definiitions && shorthand 274 */ 275 #define SIMQFRZ_RESOURCE 0x1 276 #define SIMQFRZ_LOOPDOWN 0x2 277 #define SIMQFRZ_TIMED 0x4 278 279 #define isp_sim isp_osinfo.sim 280 #define isp_path isp_osinfo.path 281 #define isp_sim2 isp_osinfo.sim2 282 #define isp_path2 isp_osinfo.path2 283 #define isp_dev isp_osinfo.dev 284 285 /* 286 * prototypes for isp_pci && isp_freebsd to share 287 */ 288 extern void isp_attach(struct ispsoftc *); 289 extern void isp_uninit(struct ispsoftc *); 290 291 /* 292 * Platform private flags 293 */ 294 #define ISP_SPRIV_ERRSET 0x1 295 #define ISP_SPRIV_INWDOG 0x2 296 #define ISP_SPRIV_GRACE 0x4 297 #define ISP_SPRIV_DONE 0x8 298 299 #define XS_CMD_S_WDOG(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG 300 #define XS_CMD_C_WDOG(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG 301 #define XS_CMD_WDOG_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG) 302 303 #define XS_CMD_S_GRACE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE 304 #define XS_CMD_C_GRACE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE 305 #define XS_CMD_GRACE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE) 306 307 #define XS_CMD_S_DONE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE 308 #define XS_CMD_C_DONE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE 309 #define XS_CMD_DONE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE) 310 311 #define XS_CMD_S_CLEAR(sccb) (sccb)->ccb_h.spriv_field0 = 0 312 /* 313 * Platform specific inline functions 314 */ 315 316 static INLINE void isp_mbox_wait_complete(struct ispsoftc *); 317 static INLINE void 318 isp_mbox_wait_complete(struct ispsoftc *isp) 319 { 320 if (isp->isp_osinfo.intsok) { 321 isp->isp_osinfo.mboxwaiting = 1; 322 (void) msleep(&isp->isp_osinfo.mboxwaiting, 323 &isp->isp_lock, PRIBIO, "isp_mboxwaiting", 10 * hz); 324 if (isp->isp_mboxbsy != 0) { 325 isp_prt(isp, ISP_LOGWARN, 326 "Interrupting Mailbox Command (0x%x) Timeout", 327 isp->isp_lastmbxcmd); 328 isp->isp_mboxbsy = 0; 329 } 330 isp->isp_osinfo.mboxwaiting = 0; 331 } else { 332 int j; 333 for (j = 0; j < 60 * 10000; j++) { 334 u_int16_t isr, sema, mbox; 335 if (isp->isp_mboxbsy == 0) { 336 break; 337 } 338 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) { 339 isp_intr(isp, isr, sema, mbox); 340 if (isp->isp_mboxbsy == 0) { 341 break; 342 } 343 } 344 USEC_DELAY(500); 345 } 346 if (isp->isp_mboxbsy != 0) { 347 isp_prt(isp, ISP_LOGWARN, 348 "Polled Mailbox Command (0x%x) Timeout", 349 isp->isp_lastmbxcmd); 350 } 351 } 352 } 353 354 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *); 355 static INLINE u_int64_t 356 nanotime_sub(struct timespec *b, struct timespec *a) 357 { 358 u_int64_t elapsed; 359 struct timespec x = *b; 360 timespecsub(&x, a); 361 elapsed = GET_NANOSEC(&x); 362 if (elapsed == 0) 363 elapsed++; 364 return (elapsed); 365 } 366 367 static INLINE char *strncat(char *, const char *, size_t); 368 static INLINE char * 369 strncat(char *d, const char *s, size_t c) 370 { 371 char *t = d; 372 373 if (c) { 374 while (*d) 375 d++; 376 while ((*d++ = *s++)) { 377 if (--c == 0) { 378 *d = '\0'; 379 break; 380 } 381 } 382 } 383 return (t); 384 } 385 386 /* 387 * Common inline functions 388 */ 389 390 #include <dev/isp/isp_inline.h> 391 #endif /* _ISP_FREEBSD_H */ 392