1 /* $FreeBSD$ */ 2 /*- 3 * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions 4 * 5 * Copyright (c) 1997-2006 by Matthew Jacob 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice immediately at the beginning of the file, without modification, 13 * this list of conditions, and the following disclaimer. 14 * 2. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #ifndef _ISP_FREEBSD_H 30 #define _ISP_FREEBSD_H 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/endian.h> 35 #if __FreeBSD_version < 500000 36 #include <sys/kernel.h> 37 #include <sys/queue.h> 38 #include <sys/malloc.h> 39 #else 40 #include <sys/lock.h> 41 #include <sys/kernel.h> 42 #include <sys/queue.h> 43 #include <sys/malloc.h> 44 #include <sys/mutex.h> 45 #include <sys/condvar.h> 46 #endif 47 48 #include <sys/proc.h> 49 #include <sys/bus.h> 50 51 #include <machine/bus.h> 52 #if __FreeBSD_version < 500000 53 #include <machine/clock.h> 54 #endif 55 #include <machine/cpu.h> 56 57 #include <cam/cam.h> 58 #include <cam/cam_debug.h> 59 #include <cam/cam_ccb.h> 60 #include <cam/cam_sim.h> 61 #include <cam/cam_xpt.h> 62 #include <cam/cam_xpt_sim.h> 63 #include <cam/cam_debug.h> 64 #include <cam/scsi/scsi_all.h> 65 #include <cam/scsi/scsi_message.h> 66 67 #include "opt_ddb.h" 68 #include "opt_isp.h" 69 70 #if __FreeBSD_version < 500000 71 #define ISP_PLATFORM_VERSION_MAJOR 4 72 #define ISP_PLATFORM_VERSION_MINOR 17 73 #else 74 #define ISP_PLATFORM_VERSION_MAJOR 5 75 #define ISP_PLATFORM_VERSION_MINOR 9 76 #endif 77 78 /* 79 * Efficiency- get rid of SBus code && tests unless we need them. 80 */ 81 #ifdef __sparc64__ 82 #define ISP_SBUS_SUPPORTED 1 83 #else 84 #define ISP_SBUS_SUPPORTED 0 85 #endif 86 87 #define HANDLE_LOOPSTATE_IN_OUTER_LAYERS 1 88 /* #define ISP_SMPLOCK 1 */ 89 90 #if __FreeBSD_version < 500000 91 #define ISP_IFLAGS INTR_TYPE_CAM 92 #else 93 #ifdef ISP_SMPLOCK 94 #define ISP_IFLAGS INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE 95 #else 96 #define ISP_IFLAGS INTR_TYPE_CAM | INTR_ENTROPY 97 #endif 98 #endif 99 100 #if __FreeBSD_version < 700000 101 typedef void ispfwfunc(int, int, int, uint16_t **); 102 #endif 103 104 #ifdef ISP_TARGET_MODE 105 #define ISP_TARGET_FUNCTIONS 1 106 #define ATPDPSIZE 256 107 typedef struct { 108 uint32_t orig_datalen; 109 uint32_t bytes_xfered; 110 uint32_t last_xframt; 111 uint32_t tag : 16, 112 lun : 13, /* not enough */ 113 state : 3; 114 } atio_private_data_t; 115 #define ATPD_STATE_FREE 0 116 #define ATPD_STATE_ATIO 1 117 #define ATPD_STATE_CAM 2 118 #define ATPD_STATE_CTIO 3 119 #define ATPD_STATE_LAST_CTIO 4 120 #define ATPD_STATE_PDON 5 121 122 typedef struct tstate { 123 struct tstate *next; 124 struct cam_path *owner; 125 struct ccb_hdr_slist atios; 126 struct ccb_hdr_slist inots; 127 lun_id_t lun; 128 int bus; 129 uint32_t hold; 130 int atio_count; 131 int inot_count; 132 } tstate_t; 133 134 #define LUN_HASH_SIZE 32 135 #define LUN_HASH_FUNC(isp, port, lun) \ 136 ((IS_DUALBUS(isp)) ? \ 137 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) : \ 138 ((lun) & (LUN_HASH_SIZE - 1))) 139 #endif 140 141 struct isposinfo { 142 struct ispsoftc * next; 143 uint64_t default_port_wwn; 144 uint64_t default_node_wwn; 145 uint32_t default_id; 146 device_t dev; 147 struct cam_sim *sim; 148 struct cam_path *path; 149 struct cam_sim *sim2; 150 struct cam_path *path2; 151 struct intr_config_hook ehook; 152 uint8_t 153 disabled : 1, 154 fcbsy : 1, 155 ktmature : 1, 156 mboxwaiting : 1, 157 intsok : 1, 158 simqfrozen : 3; 159 #if __FreeBSD_version >= 500000 160 struct firmware * fw; 161 struct mtx lock; 162 struct cv kthread_cv; 163 union { 164 struct { 165 char wwnn[17]; 166 char wwpn[17]; 167 } fc; 168 struct { 169 int iid; 170 } spi; 171 } sysctl_info; 172 #endif 173 struct proc *kproc; 174 bus_dma_tag_t cdmat; 175 bus_dmamap_t cdmap; 176 #define isp_cdmat isp_osinfo.cdmat 177 #define isp_cdmap isp_osinfo.cdmap 178 #ifdef ISP_TARGET_MODE 179 #define TM_WILDCARD_ENABLED 0x02 180 #define TM_TMODE_ENABLED 0x01 181 uint8_t tmflags[2]; /* two busses */ 182 #define NLEACT 4 183 union ccb * leact[NLEACT]; 184 tstate_t tsdflt[2]; /* two busses */ 185 tstate_t *lun_hash[LUN_HASH_SIZE]; 186 atio_private_data_t atpdp[ATPDPSIZE]; 187 #endif 188 }; 189 190 #define isp_lock isp_osinfo.lock 191 192 /* 193 * Locking macros... 194 */ 195 196 #ifdef ISP_SMPLOCK 197 #define ISP_LOCK(x) mtx_lock(&(x)->isp_lock) 198 #define ISP_UNLOCK(x) mtx_unlock(&(x)->isp_lock) 199 #define ISPLOCK_2_CAMLOCK(isp) \ 200 mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant) 201 #define CAMLOCK_2_ISPLOCK(isp) \ 202 mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock) 203 #else 204 #define ISP_LOCK(x) do { } while (0) 205 #define ISP_UNLOCK(x) do { } while (0) 206 #define ISPLOCK_2_CAMLOCK(isp) do { } while (0) 207 #define CAMLOCK_2_ISPLOCK(isp) do { } while (0) 208 #endif 209 210 /* 211 * Required Macros/Defines 212 */ 213 214 #define ISP2100_SCRLEN 0x800 215 216 #define MEMZERO(a, b) memset(a, 0, b) 217 #define MEMCPY memcpy 218 #define SNPRINTF snprintf 219 #define USEC_DELAY DELAY 220 #define USEC_SLEEP(isp, x) \ 221 if (isp->isp_osinfo.intsok) \ 222 ISP_UNLOCK(isp); \ 223 DELAY(x); \ 224 if (isp->isp_osinfo.intsok) \ 225 ISP_LOCK(isp) 226 227 #define NANOTIME_T struct timespec 228 #define GET_NANOTIME nanotime 229 #define GET_NANOSEC(x) ((x)->tv_sec * 1000000000 + (x)->tv_nsec) 230 #define NANOTIME_SUB nanotime_sub 231 232 #define MAXISPREQUEST(isp) ((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256) 233 234 #define MEMORYBARRIER(isp, type, offset, size) \ 235 switch (type) { \ 236 case SYNC_SFORDEV: \ 237 case SYNC_REQUEST: \ 238 bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, \ 239 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \ 240 break; \ 241 case SYNC_SFORCPU: \ 242 case SYNC_RESULT: \ 243 bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, \ 244 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); \ 245 break; \ 246 default: \ 247 break; \ 248 } 249 250 #define MBOX_ACQUIRE(isp) 251 #define MBOX_WAIT_COMPLETE isp_mbox_wait_complete 252 #define MBOX_NOTIFY_COMPLETE(isp) \ 253 if (isp->isp_osinfo.mboxwaiting) { \ 254 isp->isp_osinfo.mboxwaiting = 0; \ 255 wakeup(&isp->isp_mbxworkp); \ 256 } \ 257 isp->isp_mboxbsy = 0 258 #define MBOX_RELEASE(isp) 259 260 #define FC_SCRATCH_ACQUIRE(isp) \ 261 if (isp->isp_osinfo.fcbsy) { \ 262 isp_prt(isp, ISP_LOGWARN, \ 263 "FC scratch area busy (line %d)!", __LINE__); \ 264 } else \ 265 isp->isp_osinfo.fcbsy = 1 266 #define FC_SCRATCH_RELEASE(isp) isp->isp_osinfo.fcbsy = 0 267 268 #ifndef SCSI_GOOD 269 #define SCSI_GOOD SCSI_STATUS_OK 270 #endif 271 #ifndef SCSI_CHECK 272 #define SCSI_CHECK SCSI_STATUS_CHECK_COND 273 #endif 274 #ifndef SCSI_BUSY 275 #define SCSI_BUSY SCSI_STATUS_BUSY 276 #endif 277 #ifndef SCSI_QFULL 278 #define SCSI_QFULL SCSI_STATUS_QUEUE_FULL 279 #endif 280 281 #define XS_T struct ccb_scsiio 282 #define XS_DMA_ADDR_T bus_addr_t 283 #define XS_ISP(ccb) ((ispsoftc_t *) (ccb)->ccb_h.spriv_ptr1) 284 #define XS_CHANNEL(ccb) cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)) 285 #define XS_TGT(ccb) (ccb)->ccb_h.target_id 286 #define XS_LUN(ccb) (ccb)->ccb_h.target_lun 287 288 #define XS_CDBP(ccb) \ 289 (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \ 290 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes) 291 292 #define XS_CDBLEN(ccb) (ccb)->cdb_len 293 #define XS_XFRLEN(ccb) (ccb)->dxfer_len 294 #define XS_TIME(ccb) (ccb)->ccb_h.timeout 295 #define XS_RESID(ccb) (ccb)->resid 296 #define XS_STSP(ccb) (&(ccb)->scsi_status) 297 #define XS_SNSP(ccb) (&(ccb)->sense_data) 298 299 #define XS_SNSLEN(ccb) \ 300 imin((sizeof((ccb)->sense_data)), ccb->sense_len) 301 302 #define XS_SNSKEY(ccb) ((ccb)->sense_data.flags & 0xf) 303 #define XS_TAG_P(ccb) \ 304 (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \ 305 (ccb)->tag_action != CAM_TAG_ACTION_NONE) 306 307 #define XS_TAG_TYPE(ccb) \ 308 ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \ 309 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG)) 310 311 312 #define XS_SETERR(ccb, v) (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \ 313 (ccb)->ccb_h.status |= v, \ 314 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET 315 316 # define HBA_NOERROR CAM_REQ_INPROG 317 # define HBA_BOTCH CAM_UNREC_HBA_ERROR 318 # define HBA_CMDTIMEOUT CAM_CMD_TIMEOUT 319 # define HBA_SELTIMEOUT CAM_SEL_TIMEOUT 320 # define HBA_TGTBSY CAM_SCSI_STATUS_ERROR 321 # define HBA_BUSRESET CAM_SCSI_BUS_RESET 322 # define HBA_ABORTED CAM_REQ_ABORTED 323 # define HBA_DATAOVR CAM_DATA_RUN_ERR 324 # define HBA_ARQFAIL CAM_AUTOSENSE_FAIL 325 326 327 #define XS_ERR(ccb) ((ccb)->ccb_h.status & CAM_STATUS_MASK) 328 329 #define XS_NOERR(ccb) \ 330 (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \ 331 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) 332 333 #define XS_INITERR(ccb) \ 334 XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0 335 336 #define XS_SAVE_SENSE(xs, sp) \ 337 (xs)->ccb_h.status |= CAM_AUTOSNS_VALID, \ 338 memcpy(&(xs)->sense_data, sp->req_sense_data, \ 339 imin(XS_SNSLEN(xs), sp->req_sense_len)) 340 341 #define XS_SET_STATE_STAT(a, b, c) 342 343 #define DEFAULT_IID(x) (isp)->isp_osinfo.default_id 344 #define DEFAULT_LOOPID(x) (isp)->isp_osinfo.default_id 345 #define DEFAULT_NODEWWN(isp) (isp)->isp_osinfo.default_node_wwn 346 #define DEFAULT_PORTWWN(isp) (isp)->isp_osinfo.default_port_wwn 347 #define ISP_NODEWWN(isp) FCPARAM(isp)->isp_nodewwn 348 #define ISP_PORTWWN(isp) FCPARAM(isp)->isp_portwwn 349 350 #if BYTE_ORDER == BIG_ENDIAN 351 #ifdef ISP_SBUS_SUPPORTED 352 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 353 #define ISP_IOXPUT_16(isp, s, d) \ 354 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s) 355 #define ISP_IOXPUT_32(isp, s, d) \ 356 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s) 357 #define ISP_IOXGET_8(isp, s, d) d = (*((uint8_t *)s)) 358 #define ISP_IOXGET_16(isp, s, d) \ 359 d = (isp->isp_bustype == ISP_BT_SBUS)? \ 360 *((uint16_t *)s) : bswap16(*((uint16_t *)s)) 361 #define ISP_IOXGET_32(isp, s, d) \ 362 d = (isp->isp_bustype == ISP_BT_SBUS)? \ 363 *((uint32_t *)s) : bswap32(*((uint32_t *)s)) 364 #else 365 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 366 #define ISP_IOXPUT_16(isp, s, d) *(d) = bswap16(s) 367 #define ISP_IOXPUT_32(isp, s, d) *(d) = bswap32(s) 368 #define ISP_IOXGET_8(isp, s, d) d = (*((uint8_t *)s)) 369 #define ISP_IOXGET_16(isp, s, d) d = bswap16(*((uint16_t *)s)) 370 #define ISP_IOXGET_32(isp, s, d) d = bswap32(*((uint32_t *)s)) 371 #endif 372 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp) 373 #else 374 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 375 #define ISP_IOXPUT_16(isp, s, d) *(d) = s 376 #define ISP_IOXPUT_32(isp, s, d) *(d) = s 377 #define ISP_IOXGET_8(isp, s, d) d = *(s) 378 #define ISP_IOXGET_16(isp, s, d) d = *(s) 379 #define ISP_IOXGET_32(isp, s, d) d = *(s) 380 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) 381 #endif 382 383 /* 384 * Includes of common header files 385 */ 386 387 #include <dev/isp/ispreg.h> 388 #include <dev/isp/ispvar.h> 389 #include <dev/isp/ispmbox.h> 390 391 #ifdef ISP_TARGET_MODE 392 #include <dev/isp/isp_tpublic.h> 393 #endif 394 395 void isp_prt(ispsoftc_t *, int level, const char *, ...) 396 __printflike(3, 4); 397 /* 398 * isp_osinfo definiitions && shorthand 399 */ 400 #define SIMQFRZ_RESOURCE 0x1 401 #define SIMQFRZ_LOOPDOWN 0x2 402 #define SIMQFRZ_TIMED 0x4 403 404 #define isp_sim isp_osinfo.sim 405 #define isp_path isp_osinfo.path 406 #define isp_sim2 isp_osinfo.sim2 407 #define isp_path2 isp_osinfo.path2 408 #define isp_dev isp_osinfo.dev 409 410 /* 411 * prototypes for isp_pci && isp_freebsd to share 412 */ 413 extern void isp_attach(ispsoftc_t *); 414 extern void isp_uninit(ispsoftc_t *); 415 416 /* 417 * driver global data 418 */ 419 extern int isp_announced; 420 421 /* 422 * Platform private flags 423 */ 424 #define ISP_SPRIV_ERRSET 0x1 425 #define ISP_SPRIV_INWDOG 0x2 426 #define ISP_SPRIV_GRACE 0x4 427 #define ISP_SPRIV_DONE 0x8 428 429 #define XS_CMD_S_WDOG(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG 430 #define XS_CMD_C_WDOG(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG 431 #define XS_CMD_WDOG_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG) 432 433 #define XS_CMD_S_GRACE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE 434 #define XS_CMD_C_GRACE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE 435 #define XS_CMD_GRACE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE) 436 437 #define XS_CMD_S_DONE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE 438 #define XS_CMD_C_DONE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE 439 #define XS_CMD_DONE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE) 440 441 #define XS_CMD_S_CLEAR(sccb) (sccb)->ccb_h.spriv_field0 = 0 442 443 /* 444 * Platform specific inline functions 445 */ 446 447 static __inline void isp_mbox_wait_complete(ispsoftc_t *); 448 static __inline void 449 isp_mbox_wait_complete(ispsoftc_t *isp) 450 { 451 if (isp->isp_osinfo.intsok) { 452 int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz; 453 isp->isp_osinfo.mboxwaiting = 1; 454 #ifdef ISP_SMPLOCK 455 (void) msleep(&isp->isp_mbxworkp, 456 &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim); 457 #else 458 (void) tsleep(&isp->isp_mbxworkp, 459 PRIBIO, "isp_mboxwaiting", lim); 460 #endif 461 if (isp->isp_mboxbsy != 0) { 462 isp_prt(isp, ISP_LOGWARN, 463 "Interrupting Mailbox Command (0x%x) Timeout", 464 isp->isp_lastmbxcmd); 465 isp->isp_mboxbsy = 0; 466 } 467 isp->isp_osinfo.mboxwaiting = 0; 468 } else { 469 int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000; 470 int j; 471 for (j = 0; j < lim; j++) { 472 uint16_t isr, sema, mbox; 473 if (isp->isp_mboxbsy == 0) { 474 break; 475 } 476 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) { 477 isp_intr(isp, isr, sema, mbox); 478 if (isp->isp_mboxbsy == 0) { 479 break; 480 } 481 } 482 USEC_DELAY(500); 483 } 484 if (isp->isp_mboxbsy != 0) { 485 isp_prt(isp, ISP_LOGWARN, 486 "Polled Mailbox Command (0x%x) Timeout", 487 isp->isp_lastmbxcmd); 488 } 489 } 490 } 491 492 static __inline uint64_t nanotime_sub(struct timespec *, struct timespec *); 493 static __inline uint64_t 494 nanotime_sub(struct timespec *b, struct timespec *a) 495 { 496 uint64_t elapsed; 497 struct timespec x = *b; 498 timespecsub(&x, a); 499 elapsed = GET_NANOSEC(&x); 500 if (elapsed == 0) 501 elapsed++; 502 return (elapsed); 503 } 504 505 static __inline char *strncat(char *, const char *, size_t); 506 static __inline char * 507 strncat(char *d, const char *s, size_t c) 508 { 509 char *t = d; 510 511 if (c) { 512 while (*d) 513 d++; 514 while ((*d++ = *s++)) { 515 if (--c == 0) { 516 *d = '\0'; 517 break; 518 } 519 } 520 } 521 return (t); 522 } 523 524 /* 525 * ISP Library functions 526 */ 527 528 #include <dev/isp/isp_library.h> 529 530 #endif /* _ISP_FREEBSD_H */ 531