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 #define ATPDPSIZE 256 70 typedef struct { 71 u_int32_t orig_datalen; 72 u_int32_t bytes_xfered; 73 u_int32_t last_xframt; 74 u_int32_t tag; 75 } atio_private_data_t; 76 77 typedef struct tstate { 78 struct tstate *next; 79 struct cam_path *owner; 80 struct ccb_hdr_slist atios; 81 struct ccb_hdr_slist inots; 82 lun_id_t lun; 83 int bus; 84 u_int32_t hold; 85 } tstate_t; 86 87 #define LUN_HASH_SIZE 32 88 #define LUN_HASH_FUNC(isp, port, lun) \ 89 ((IS_DUALBUS(isp)) ? \ 90 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) : \ 91 ((lun) & (LUN_HASH_SIZE - 1))) 92 #endif 93 94 struct isposinfo { 95 struct ispsoftc * next; 96 u_int64_t default_port_wwn; 97 u_int64_t default_node_wwn; 98 device_t dev; 99 struct cam_sim *sim; 100 struct cam_path *path; 101 struct cam_sim *sim2; 102 struct cam_path *path2; 103 struct intr_config_hook ehook; 104 u_int8_t mboxwaiting; 105 u_int8_t simqfrozen; 106 u_int8_t drain; 107 u_int8_t intsok; 108 struct mtx lock; 109 struct cv kthread_cv; 110 struct proc *kproc; 111 #ifdef ISP_TARGET_MODE 112 #define TM_WANTED 0x80 113 #define TM_BUSY 0x40 114 #define TM_WILDCARD_ENABLED 0x02 115 #define TM_TMODE_ENABLED 0x01 116 struct cv tgtcv0[2]; /* two busses */ 117 struct cv tgtcv1[2]; /* two busses */ 118 u_int8_t tmflags[2]; /* two busses */ 119 u_int8_t rstatus[2]; /* two bussed */ 120 u_int16_t rollinfo; 121 tstate_t tsdflt[2]; /* two busses */ 122 tstate_t *lun_hash[LUN_HASH_SIZE]; 123 atio_private_data_t atpdp[ATPDPSIZE]; 124 #endif 125 }; 126 127 #define isp_lock isp_osinfo.lock 128 129 /* 130 * Locking macros... 131 */ 132 133 #define ISP_LOCK(x) mtx_lock(&(x)->isp_lock) 134 #define ISP_UNLOCK(x) mtx_unlock(&(x)->isp_lock) 135 #define ISPLOCK_2_CAMLOCK(isp) \ 136 mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant) 137 #define CAMLOCK_2_ISPLOCK(isp) \ 138 mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock) 139 140 /* 141 * Required Macros/Defines 142 */ 143 144 #define INLINE __inline 145 146 #define ISP2100_SCRLEN 0x400 147 148 #define MEMZERO bzero 149 #define MEMCPY(dst, src, amt) bcopy((src), (dst), (amt)) 150 #define SNPRINTF snprintf 151 #define STRNCAT strncat 152 #define USEC_DELAY DELAY 153 #define USEC_SLEEP(isp, x) \ 154 if (isp->isp_osinfo.intsok) \ 155 ISP_UNLOCK(isp); \ 156 DELAY(x); \ 157 if (isp->isp_osinfo.intsok) \ 158 ISP_LOCK(isp) 159 160 #define NANOTIME_T struct timespec 161 #define GET_NANOTIME nanotime 162 #define GET_NANOSEC(x) ((x)->tv_sec * 1000000000 + (x)->tv_nsec) 163 #define NANOTIME_SUB nanotime_sub 164 165 #define MAXISPREQUEST(isp) 256 166 167 #if defined(__alpha__) 168 #define MEMORYBARRIER(isp, type, offset, size) alpha_mb() 169 #elif defined(__ia64__) 170 #define MEMORYBARRIER(isp, type, offset, size) \ 171 do { ia64_mf(); ia64_mf_a(); } while (0) 172 #else 173 #define MEMORYBARRIER(isp, type, offset, size) 174 #endif 175 176 #define MBOX_ACQUIRE(isp) 177 #define MBOX_WAIT_COMPLETE isp_mbox_wait_complete 178 #define MBOX_NOTIFY_COMPLETE(isp) \ 179 if (isp->isp_osinfo.mboxwaiting) { \ 180 isp->isp_osinfo.mboxwaiting = 0; \ 181 wakeup(&isp->isp_osinfo.mboxwaiting); \ 182 } \ 183 isp->isp_mboxbsy = 0 184 #define MBOX_RELEASE(isp) 185 186 #ifndef SCSI_GOOD 187 #define SCSI_GOOD SCSI_STATUS_OK 188 #endif 189 #ifndef SCSI_CHECK 190 #define SCSI_CHECK SCSI_STATUS_CHECK_COND 191 #endif 192 #ifndef SCSI_BUSY 193 #define SCSI_BUSY SCSI_STATUS_BUSY 194 #endif 195 #ifndef SCSI_QFULL 196 #define SCSI_QFULL SCSI_STATUS_QUEUE_FULL 197 #endif 198 199 #define XS_T struct ccb_scsiio 200 #define XS_ISP(ccb) ((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1) 201 #define XS_CHANNEL(ccb) cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)) 202 #define XS_TGT(ccb) (ccb)->ccb_h.target_id 203 #define XS_LUN(ccb) (ccb)->ccb_h.target_lun 204 205 #define XS_CDBP(ccb) \ 206 (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \ 207 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes) 208 209 #define XS_CDBLEN(ccb) (ccb)->cdb_len 210 #define XS_XFRLEN(ccb) (ccb)->dxfer_len 211 #define XS_TIME(ccb) (ccb)->ccb_h.timeout 212 #define XS_RESID(ccb) (ccb)->resid 213 #define XS_STSP(ccb) (&(ccb)->scsi_status) 214 #define XS_SNSP(ccb) (&(ccb)->sense_data) 215 216 #define XS_SNSLEN(ccb) \ 217 imin((sizeof((ccb)->sense_data)), ccb->sense_len) 218 219 #define XS_SNSKEY(ccb) ((ccb)->sense_data.flags & 0xf) 220 #define XS_TAG_P(ccb) \ 221 (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \ 222 (ccb)->tag_action != CAM_TAG_ACTION_NONE) 223 224 #define XS_TAG_TYPE(ccb) \ 225 ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \ 226 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG)) 227 228 229 #define XS_SETERR(ccb, v) (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \ 230 (ccb)->ccb_h.status |= v, \ 231 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET 232 233 # define HBA_NOERROR CAM_REQ_INPROG 234 # define HBA_BOTCH CAM_UNREC_HBA_ERROR 235 # define HBA_CMDTIMEOUT CAM_CMD_TIMEOUT 236 # define HBA_SELTIMEOUT CAM_SEL_TIMEOUT 237 # define HBA_TGTBSY CAM_SCSI_STATUS_ERROR 238 # define HBA_BUSRESET CAM_SCSI_BUS_RESET 239 # define HBA_ABORTED CAM_REQ_ABORTED 240 # define HBA_DATAOVR CAM_DATA_RUN_ERR 241 # define HBA_ARQFAIL CAM_AUTOSENSE_FAIL 242 243 244 #define XS_ERR(ccb) ((ccb)->ccb_h.status & CAM_STATUS_MASK) 245 246 #define XS_NOERR(ccb) \ 247 (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \ 248 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) 249 250 #define XS_INITERR(ccb) \ 251 XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0 252 253 #define XS_SAVE_SENSE(xs, sp) \ 254 (xs)->ccb_h.status |= CAM_AUTOSNS_VALID, \ 255 bcopy(sp->req_sense_data, &(xs)->sense_data, \ 256 imin(XS_SNSLEN(xs), sp->req_sense_len)) 257 258 #define XS_SET_STATE_STAT(a, b, c) 259 260 #define DEFAULT_IID(x) 7 261 #define DEFAULT_LOOPID(x) 109 262 #define DEFAULT_NODEWWN(isp) (isp)->isp_osinfo.default_node_wwn 263 #define DEFAULT_PORTWWN(isp) (isp)->isp_osinfo.default_port_wwn 264 #define ISP_NODEWWN(isp) FCPARAM(isp)->isp_nodewwn 265 #define ISP_PORTWWN(isp) FCPARAM(isp)->isp_portwwn 266 267 #define ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src) \ 268 if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t)) 269 #define ISP_SWIZZLE_ICB(a, b) 270 #define ISP_SWIZZLE_REQUEST(a, b) 271 #define ISP_UNSWIZZLE_RESPONSE(a, b, c) 272 #define ISP_SWIZZLE_SNS_REQ(a, b) 273 #define ISP_UNSWIZZLE_SNS_RSP(a, b, c) 274 #define ISP_SWIZZLE_NVRAM_WORD(isp, x) 275 276 /* 277 * Includes of common header files 278 */ 279 280 #include <dev/isp/ispreg.h> 281 #include <dev/isp/ispvar.h> 282 #include <dev/isp/ispmbox.h> 283 284 /* 285 * isp_osinfo definiitions && shorthand 286 */ 287 #define SIMQFRZ_RESOURCE 0x1 288 #define SIMQFRZ_LOOPDOWN 0x2 289 #define SIMQFRZ_TIMED 0x4 290 291 #define isp_sim isp_osinfo.sim 292 #define isp_path isp_osinfo.path 293 #define isp_sim2 isp_osinfo.sim2 294 #define isp_path2 isp_osinfo.path2 295 #define isp_dev isp_osinfo.dev 296 297 /* 298 * prototypes for isp_pci && isp_freebsd to share 299 */ 300 extern void isp_attach(struct ispsoftc *); 301 extern void isp_uninit(struct ispsoftc *); 302 303 /* 304 * Platform private flags 305 */ 306 #define ISP_SPRIV_ERRSET 0x1 307 #define ISP_SPRIV_INWDOG 0x2 308 #define ISP_SPRIV_GRACE 0x4 309 #define ISP_SPRIV_DONE 0x8 310 311 #define XS_CMD_S_WDOG(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG 312 #define XS_CMD_C_WDOG(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG 313 #define XS_CMD_WDOG_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG) 314 315 #define XS_CMD_S_GRACE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE 316 #define XS_CMD_C_GRACE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE 317 #define XS_CMD_GRACE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE) 318 319 #define XS_CMD_S_DONE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE 320 #define XS_CMD_C_DONE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE 321 #define XS_CMD_DONE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE) 322 323 #define XS_CMD_S_CLEAR(sccb) (sccb)->ccb_h.spriv_field0 = 0 324 /* 325 * Platform specific inline functions 326 */ 327 328 static INLINE void isp_mbox_wait_complete(struct ispsoftc *); 329 static INLINE void 330 isp_mbox_wait_complete(struct ispsoftc *isp) 331 { 332 if (isp->isp_osinfo.intsok) { 333 isp->isp_osinfo.mboxwaiting = 1; 334 (void) msleep(&isp->isp_osinfo.mboxwaiting, 335 &isp->isp_lock, PRIBIO, "isp_mboxwaiting", 10 * hz); 336 if (isp->isp_mboxbsy != 0) { 337 isp_prt(isp, ISP_LOGWARN, 338 "Interrupting Mailbox Command (0x%x) Timeout", 339 isp->isp_lastmbxcmd); 340 isp->isp_mboxbsy = 0; 341 } 342 isp->isp_osinfo.mboxwaiting = 0; 343 } else { 344 int j; 345 for (j = 0; j < 60 * 10000; j++) { 346 u_int16_t isr, sema, mbox; 347 if (isp->isp_mboxbsy == 0) { 348 break; 349 } 350 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) { 351 isp_intr(isp, isr, sema, mbox); 352 if (isp->isp_mboxbsy == 0) { 353 break; 354 } 355 } 356 USEC_DELAY(500); 357 } 358 if (isp->isp_mboxbsy != 0) { 359 isp_prt(isp, ISP_LOGWARN, 360 "Polled Mailbox Command (0x%x) Timeout", 361 isp->isp_lastmbxcmd); 362 } 363 } 364 } 365 366 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *); 367 static INLINE u_int64_t 368 nanotime_sub(struct timespec *b, struct timespec *a) 369 { 370 u_int64_t elapsed; 371 struct timespec x = *b; 372 timespecsub(&x, a); 373 elapsed = GET_NANOSEC(&x); 374 if (elapsed == 0) 375 elapsed++; 376 return (elapsed); 377 } 378 379 static INLINE char *strncat(char *, const char *, size_t); 380 static INLINE char * 381 strncat(char *d, const char *s, size_t c) 382 { 383 char *t = d; 384 385 if (c) { 386 while (*d) 387 d++; 388 while ((*d++ = *s++)) { 389 if (--c == 0) { 390 *d = '\0'; 391 break; 392 } 393 } 394 } 395 return (t); 396 } 397 398 /* 399 * Common inline functions 400 */ 401 402 #include <dev/isp/isp_inline.h> 403 #endif /* _ISP_FREEBSD_H */ 404