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