1 /*- 2 * Copyright (c) 1997-2009 by Matthew Jacob 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. The name of the author may not be used to endorse or promote products 12 * derived from this software without specific prior written permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters. 29 */ 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <dev/isp/isp_freebsd.h> 34 #include <sys/unistd.h> 35 #include <sys/kthread.h> 36 #include <sys/conf.h> 37 #include <sys/module.h> 38 #include <sys/ioccom.h> 39 #include <dev/isp/isp_ioctl.h> 40 #include <sys/devicestat.h> 41 #include <cam/cam_periph.h> 42 #include <cam/cam_xpt_periph.h> 43 44 MODULE_VERSION(isp, 1); 45 MODULE_DEPEND(isp, cam, 1, 1, 1); 46 int isp_announced = 0; 47 int isp_loop_down_limit = 60; /* default loop down limit */ 48 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */ 49 int isp_gone_device_time = 30; /* grace time before reporting device lost */ 50 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s"; 51 52 static void isp_freeze_loopdown(ispsoftc_t *, int); 53 static void isp_loop_changed(ispsoftc_t *isp, int chan); 54 static d_ioctl_t ispioctl; 55 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *); 56 static void isp_poll(struct cam_sim *); 57 static timeout_t isp_watchdog; 58 static timeout_t isp_gdt; 59 static task_fn_t isp_gdt_task; 60 static void isp_kthread(void *); 61 static void isp_action(struct cam_sim *, union ccb *); 62 static int isp_timer_count; 63 static void isp_timer(void *); 64 65 static struct cdevsw isp_cdevsw = { 66 .d_version = D_VERSION, 67 .d_ioctl = ispioctl, 68 .d_name = "isp", 69 }; 70 71 static int 72 isp_role_sysctl(SYSCTL_HANDLER_ARGS) 73 { 74 ispsoftc_t *isp = (ispsoftc_t *)arg1; 75 int chan = arg2; 76 int error, old, value; 77 78 value = FCPARAM(isp, chan)->role; 79 80 error = sysctl_handle_int(oidp, &value, 0, req); 81 if ((error != 0) || (req->newptr == NULL)) 82 return (error); 83 84 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH) 85 return (EINVAL); 86 87 ISP_LOCK(isp); 88 old = FCPARAM(isp, chan)->role; 89 90 /* We don't allow target mode switch from here. */ 91 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR); 92 93 /* If nothing has changed -- we are done. */ 94 if (value == old) { 95 ISP_UNLOCK(isp); 96 return (0); 97 } 98 99 /* Actually change the role. */ 100 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value); 101 ISP_UNLOCK(isp); 102 return (error); 103 } 104 105 static int 106 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan) 107 { 108 struct ccb_setasync csa; 109 struct cam_sim *sim; 110 struct cam_path *path; 111 #ifdef ISP_TARGET_MODE 112 int i; 113 #endif 114 115 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, 116 device_get_unit(isp->isp_dev), &isp->isp_lock, 117 isp->isp_maxcmds, isp->isp_maxcmds, devq); 118 if (sim == NULL) 119 return (ENOMEM); 120 121 ISP_LOCK(isp); 122 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) { 123 ISP_UNLOCK(isp); 124 cam_sim_free(sim, FALSE); 125 return (EIO); 126 } 127 ISP_UNLOCK(isp); 128 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 129 ISP_LOCK(isp); 130 xpt_bus_deregister(cam_sim_path(sim)); 131 ISP_UNLOCK(isp); 132 cam_sim_free(sim, FALSE); 133 return (ENXIO); 134 } 135 xpt_setup_ccb(&csa.ccb_h, path, 5); 136 csa.ccb_h.func_code = XPT_SASYNC_CB; 137 csa.event_enable = AC_LOST_DEVICE; 138 csa.callback = isp_cam_async; 139 csa.callback_arg = sim; 140 141 ISP_LOCK(isp); 142 xpt_action((union ccb *)&csa); 143 ISP_UNLOCK(isp); 144 145 if (IS_SCSI(isp)) { 146 struct isp_spi *spi = ISP_SPI_PC(isp, chan); 147 spi->sim = sim; 148 spi->path = path; 149 #ifdef ISP_TARGET_MODE 150 TAILQ_INIT(&spi->waitq); 151 STAILQ_INIT(&spi->ntfree); 152 for (i = 0; i < ATPDPSIZE; i++) 153 STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next); 154 LIST_INIT(&spi->atfree); 155 for (i = ATPDPSIZE-1; i >= 0; i--) 156 LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next); 157 for (i = 0; i < ATPDPHASHSIZE; i++) 158 LIST_INIT(&spi->atused[i]); 159 #endif 160 } else { 161 fcparam *fcp = FCPARAM(isp, chan); 162 struct isp_fc *fc = ISP_FC_PC(isp, chan); 163 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev); 164 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev); 165 char name[16]; 166 167 ISP_LOCK(isp); 168 fc->sim = sim; 169 fc->path = path; 170 fc->isp = isp; 171 fc->ready = 1; 172 173 callout_init_mtx(&fc->gdt, &isp->isp_lock, 0); 174 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc); 175 #ifdef ISP_TARGET_MODE 176 TAILQ_INIT(&fc->waitq); 177 STAILQ_INIT(&fc->ntfree); 178 for (i = 0; i < ATPDPSIZE; i++) 179 STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next); 180 LIST_INIT(&fc->atfree); 181 for (i = ATPDPSIZE-1; i >= 0; i--) 182 LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next); 183 for (i = 0; i < ATPDPHASHSIZE; i++) 184 LIST_INIT(&fc->atused[i]); 185 #endif 186 isp_loop_changed(isp, chan); 187 ISP_UNLOCK(isp); 188 if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0, 189 "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) { 190 xpt_free_path(fc->path); 191 ISP_LOCK(isp); 192 xpt_bus_deregister(cam_sim_path(fc->sim)); 193 ISP_UNLOCK(isp); 194 cam_sim_free(fc->sim, FALSE); 195 return (ENOMEM); 196 } 197 fc->num_threads += 1; 198 if (chan > 0) { 199 snprintf(name, sizeof(name), "chan%d", chan); 200 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), 201 OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel"); 202 } 203 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 204 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn, 205 "World Wide Node Name"); 206 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 207 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn, 208 "World Wide Port Name"); 209 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 210 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0, 211 "Loop Down Limit"); 212 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 213 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0, 214 "Gone Device Time"); 215 #if defined(ISP_TARGET_MODE) && defined(DEBUG) 216 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 217 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0, 218 "Cause a Lost Frame on a Read"); 219 #endif 220 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 221 "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan, 222 isp_role_sysctl, "I", "Current role"); 223 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 224 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0, 225 "Connection speed in gigabits"); 226 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 227 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0, 228 "Link state"); 229 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 230 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0, 231 "Firmware state"); 232 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 233 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0, 234 "Loop state"); 235 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 236 "topo", CTLFLAG_RD, &fcp->isp_topo, 0, 237 "Connection topology"); 238 } 239 return (0); 240 } 241 242 static void 243 isp_detach_chan(ispsoftc_t *isp, int chan) 244 { 245 struct cam_sim *sim; 246 struct cam_path *path; 247 struct ccb_setasync csa; 248 int *num_threads; 249 250 ISP_GET_PC(isp, chan, sim, sim); 251 ISP_GET_PC(isp, chan, path, path); 252 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads); 253 254 xpt_setup_ccb(&csa.ccb_h, path, 5); 255 csa.ccb_h.func_code = XPT_SASYNC_CB; 256 csa.event_enable = 0; 257 csa.callback = isp_cam_async; 258 csa.callback_arg = sim; 259 xpt_action((union ccb *)&csa); 260 xpt_free_path(path); 261 xpt_bus_deregister(cam_sim_path(sim)); 262 cam_sim_free(sim, FALSE); 263 264 /* Wait for the channel's spawned threads to exit. */ 265 wakeup(isp->isp_osinfo.pc.ptr); 266 while (*num_threads != 0) 267 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "isp_reap", 100); 268 } 269 270 int 271 isp_attach(ispsoftc_t *isp) 272 { 273 const char *nu = device_get_nameunit(isp->isp_osinfo.dev); 274 int du = device_get_unit(isp->isp_dev); 275 int chan; 276 277 /* 278 * Create the device queue for our SIM(s). 279 */ 280 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds); 281 if (isp->isp_osinfo.devq == NULL) { 282 return (EIO); 283 } 284 285 for (chan = 0; chan < isp->isp_nchan; chan++) { 286 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) { 287 goto unwind; 288 } 289 } 290 291 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0); 292 isp_timer_count = hz >> 2; 293 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp); 294 295 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu); 296 if (isp->isp_osinfo.cdev) { 297 isp->isp_osinfo.cdev->si_drv1 = isp; 298 } 299 return (0); 300 301 unwind: 302 while (--chan >= 0) { 303 struct cam_sim *sim; 304 struct cam_path *path; 305 306 ISP_GET_PC(isp, chan, sim, sim); 307 ISP_GET_PC(isp, chan, path, path); 308 xpt_free_path(path); 309 ISP_LOCK(isp); 310 xpt_bus_deregister(cam_sim_path(sim)); 311 ISP_UNLOCK(isp); 312 cam_sim_free(sim, FALSE); 313 } 314 cam_simq_free(isp->isp_osinfo.devq); 315 isp->isp_osinfo.devq = NULL; 316 return (-1); 317 } 318 319 int 320 isp_detach(ispsoftc_t *isp) 321 { 322 int chan; 323 324 if (isp->isp_osinfo.cdev) { 325 destroy_dev(isp->isp_osinfo.cdev); 326 isp->isp_osinfo.cdev = NULL; 327 } 328 ISP_LOCK(isp); 329 /* Tell spawned threads that we're exiting. */ 330 isp->isp_osinfo.is_exiting = 1; 331 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) 332 isp_detach_chan(isp, chan); 333 ISP_UNLOCK(isp); 334 callout_drain(&isp->isp_osinfo.tmo); 335 cam_simq_free(isp->isp_osinfo.devq); 336 return (0); 337 } 338 339 static void 340 isp_freeze_loopdown(ispsoftc_t *isp, int chan) 341 { 342 struct isp_fc *fc = ISP_FC_PC(isp, chan); 343 344 if (fc->sim == NULL) 345 return; 346 if (fc->simqfrozen == 0) { 347 isp_prt(isp, ISP_LOGDEBUG0, 348 "Chan %d Freeze simq (loopdown)", chan); 349 fc->simqfrozen = SIMQFRZ_LOOPDOWN; 350 xpt_hold_boot(); 351 xpt_freeze_simq(fc->sim, 1); 352 } else { 353 isp_prt(isp, ISP_LOGDEBUG0, 354 "Chan %d Mark simq frozen (loopdown)", chan); 355 fc->simqfrozen |= SIMQFRZ_LOOPDOWN; 356 } 357 } 358 359 static void 360 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan) 361 { 362 struct isp_fc *fc = ISP_FC_PC(isp, chan); 363 364 if (fc->sim == NULL) 365 return; 366 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN; 367 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN; 368 if (wasfrozen && fc->simqfrozen == 0) { 369 isp_prt(isp, ISP_LOGDEBUG0, 370 "Chan %d Release simq", chan); 371 xpt_release_simq(fc->sim, 1); 372 xpt_release_boot(); 373 } 374 } 375 376 static int 377 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td) 378 { 379 ispsoftc_t *isp; 380 int nr, chan, retval = ENOTTY; 381 382 isp = dev->si_drv1; 383 384 switch (c) { 385 case ISP_SDBLEV: 386 { 387 int olddblev = isp->isp_dblev; 388 isp->isp_dblev = *(int *)addr; 389 *(int *)addr = olddblev; 390 retval = 0; 391 break; 392 } 393 case ISP_GETROLE: 394 chan = *(int *)addr; 395 if (chan < 0 || chan >= isp->isp_nchan) { 396 retval = -ENXIO; 397 break; 398 } 399 if (IS_FC(isp)) { 400 *(int *)addr = FCPARAM(isp, chan)->role; 401 } else { 402 *(int *)addr = ISP_ROLE_INITIATOR; 403 } 404 retval = 0; 405 break; 406 case ISP_SETROLE: 407 if (IS_SCSI(isp)) 408 break; 409 nr = *(int *)addr; 410 chan = nr >> 8; 411 if (chan < 0 || chan >= isp->isp_nchan) { 412 retval = -ENXIO; 413 break; 414 } 415 nr &= 0xff; 416 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) { 417 retval = EINVAL; 418 break; 419 } 420 ISP_LOCK(isp); 421 *(int *)addr = FCPARAM(isp, chan)->role; 422 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr); 423 ISP_UNLOCK(isp); 424 retval = 0; 425 break; 426 427 case ISP_RESETHBA: 428 ISP_LOCK(isp); 429 isp_reinit(isp, 0); 430 ISP_UNLOCK(isp); 431 retval = 0; 432 break; 433 434 case ISP_RESCAN: 435 if (IS_FC(isp)) { 436 chan = *(int *)addr; 437 if (chan < 0 || chan >= isp->isp_nchan) { 438 retval = -ENXIO; 439 break; 440 } 441 ISP_LOCK(isp); 442 if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) { 443 retval = EIO; 444 } else { 445 retval = 0; 446 } 447 ISP_UNLOCK(isp); 448 } 449 break; 450 451 case ISP_FC_LIP: 452 if (IS_FC(isp)) { 453 chan = *(int *)addr; 454 if (chan < 0 || chan >= isp->isp_nchan) { 455 retval = -ENXIO; 456 break; 457 } 458 ISP_LOCK(isp); 459 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) { 460 retval = EIO; 461 } else { 462 retval = 0; 463 } 464 ISP_UNLOCK(isp); 465 } 466 break; 467 case ISP_FC_GETDINFO: 468 { 469 struct isp_fc_device *ifc = (struct isp_fc_device *) addr; 470 fcportdb_t *lp; 471 472 if (IS_SCSI(isp)) { 473 break; 474 } 475 if (ifc->loopid >= MAX_FC_TARG) { 476 retval = EINVAL; 477 break; 478 } 479 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid]; 480 if (lp->state != FC_PORTDB_STATE_NIL) { 481 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT; 482 ifc->loopid = lp->handle; 483 ifc->portid = lp->portid; 484 ifc->node_wwn = lp->node_wwn; 485 ifc->port_wwn = lp->port_wwn; 486 retval = 0; 487 } else { 488 retval = ENODEV; 489 } 490 break; 491 } 492 case ISP_FC_GETHINFO: 493 { 494 struct isp_hba_device *hba = (struct isp_hba_device *) addr; 495 int chan = hba->fc_channel; 496 497 if (chan < 0 || chan >= isp->isp_nchan) { 498 retval = ENXIO; 499 break; 500 } 501 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev); 502 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev); 503 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev); 504 hba->fc_nchannels = isp->isp_nchan; 505 if (IS_FC(isp)) { 506 hba->fc_nports = MAX_FC_TARG; 507 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed; 508 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1; 509 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid; 510 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram; 511 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram; 512 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn; 513 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn; 514 } else { 515 hba->fc_nports = MAX_TARGETS; 516 hba->fc_speed = 0; 517 hba->fc_topology = 0; 518 hba->nvram_node_wwn = 0ull; 519 hba->nvram_port_wwn = 0ull; 520 hba->active_node_wwn = 0ull; 521 hba->active_port_wwn = 0ull; 522 } 523 retval = 0; 524 break; 525 } 526 case ISP_TSK_MGMT: 527 { 528 int needmarker; 529 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr; 530 uint16_t nphdl; 531 mbreg_t mbs; 532 533 if (IS_SCSI(isp)) { 534 break; 535 } 536 537 chan = fct->chan; 538 if (chan < 0 || chan >= isp->isp_nchan) { 539 retval = -ENXIO; 540 break; 541 } 542 543 needmarker = retval = 0; 544 nphdl = fct->loopid; 545 ISP_LOCK(isp); 546 if (IS_24XX(isp)) { 547 void *reqp; 548 uint8_t resp[QENTRY_LEN]; 549 isp24xx_tmf_t tmf; 550 isp24xx_statusreq_t sp; 551 fcparam *fcp = FCPARAM(isp, chan); 552 fcportdb_t *lp; 553 int i; 554 555 for (i = 0; i < MAX_FC_TARG; i++) { 556 lp = &fcp->portdb[i]; 557 if (lp->handle == nphdl) { 558 break; 559 } 560 } 561 if (i == MAX_FC_TARG) { 562 retval = ENXIO; 563 ISP_UNLOCK(isp); 564 break; 565 } 566 ISP_MEMZERO(&tmf, sizeof(tmf)); 567 tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT; 568 tmf.tmf_header.rqs_entry_count = 1; 569 tmf.tmf_nphdl = lp->handle; 570 tmf.tmf_delay = 2; 571 tmf.tmf_timeout = 4; 572 tmf.tmf_tidlo = lp->portid; 573 tmf.tmf_tidhi = lp->portid >> 16; 574 tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan); 575 tmf.tmf_lun[1] = fct->lun & 0xff; 576 if (fct->lun >= 256) { 577 tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8); 578 } 579 switch (fct->action) { 580 case IPT_CLEAR_ACA: 581 tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA; 582 break; 583 case IPT_TARGET_RESET: 584 tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET; 585 needmarker = 1; 586 break; 587 case IPT_LUN_RESET: 588 tmf.tmf_flags = ISP24XX_TMF_LUN_RESET; 589 needmarker = 1; 590 break; 591 case IPT_CLEAR_TASK_SET: 592 tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET; 593 needmarker = 1; 594 break; 595 case IPT_ABORT_TASK_SET: 596 tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET; 597 needmarker = 1; 598 break; 599 default: 600 retval = EINVAL; 601 break; 602 } 603 if (retval) { 604 ISP_UNLOCK(isp); 605 break; 606 } 607 608 /* Prepare space for response in memory */ 609 memset(resp, 0xff, sizeof(resp)); 610 tmf.tmf_handle = isp_allocate_handle(isp, resp, 611 ISP_HANDLE_CTRL); 612 if (tmf.tmf_handle == 0) { 613 isp_prt(isp, ISP_LOGERR, 614 "%s: TMF of Chan %d out of handles", 615 __func__, chan); 616 ISP_UNLOCK(isp); 617 retval = ENOMEM; 618 break; 619 } 620 621 /* Send request and wait for response. */ 622 reqp = isp_getrqentry(isp); 623 if (reqp == NULL) { 624 isp_prt(isp, ISP_LOGERR, 625 "%s: TMF of Chan %d out of rqent", 626 __func__, chan); 627 isp_destroy_handle(isp, tmf.tmf_handle); 628 ISP_UNLOCK(isp); 629 retval = EIO; 630 break; 631 } 632 isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp); 633 if (isp->isp_dblev & ISP_LOGDEBUG1) 634 isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp); 635 ISP_SYNC_REQUEST(isp); 636 if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) { 637 isp_prt(isp, ISP_LOGERR, 638 "%s: TMF of Chan %d timed out", 639 __func__, chan); 640 isp_destroy_handle(isp, tmf.tmf_handle); 641 ISP_UNLOCK(isp); 642 retval = EIO; 643 break; 644 } 645 if (isp->isp_dblev & ISP_LOGDEBUG1) 646 isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp); 647 isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp); 648 649 if (sp.req_completion_status != 0) 650 retval = EIO; 651 else if (needmarker) 652 fcp->sendmarker = 1; 653 } else { 654 MBSINIT(&mbs, 0, MBLOGALL, 0); 655 if (ISP_CAP_2KLOGIN(isp) == 0) { 656 nphdl <<= 8; 657 } 658 switch (fct->action) { 659 case IPT_CLEAR_ACA: 660 mbs.param[0] = MBOX_CLEAR_ACA; 661 mbs.param[1] = nphdl; 662 mbs.param[2] = fct->lun; 663 break; 664 case IPT_TARGET_RESET: 665 mbs.param[0] = MBOX_TARGET_RESET; 666 mbs.param[1] = nphdl; 667 needmarker = 1; 668 break; 669 case IPT_LUN_RESET: 670 mbs.param[0] = MBOX_LUN_RESET; 671 mbs.param[1] = nphdl; 672 mbs.param[2] = fct->lun; 673 needmarker = 1; 674 break; 675 case IPT_CLEAR_TASK_SET: 676 mbs.param[0] = MBOX_CLEAR_TASK_SET; 677 mbs.param[1] = nphdl; 678 mbs.param[2] = fct->lun; 679 needmarker = 1; 680 break; 681 case IPT_ABORT_TASK_SET: 682 mbs.param[0] = MBOX_ABORT_TASK_SET; 683 mbs.param[1] = nphdl; 684 mbs.param[2] = fct->lun; 685 needmarker = 1; 686 break; 687 default: 688 retval = EINVAL; 689 break; 690 } 691 if (retval == 0) { 692 if (needmarker) { 693 FCPARAM(isp, chan)->sendmarker = 1; 694 } 695 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs); 696 if (retval) { 697 retval = EIO; 698 } 699 } 700 } 701 ISP_UNLOCK(isp); 702 break; 703 } 704 default: 705 break; 706 } 707 return (retval); 708 } 709 710 /* 711 * Local Inlines 712 */ 713 714 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *); 715 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *); 716 717 static ISP_INLINE int 718 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb) 719 { 720 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free; 721 if (ISP_PCMD(ccb) == NULL) { 722 return (-1); 723 } 724 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next; 725 return (0); 726 } 727 728 static ISP_INLINE void 729 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb) 730 { 731 if (ISP_PCMD(ccb)) { 732 #ifdef ISP_TARGET_MODE 733 PISP_PCMD(ccb)->datalen = 0; 734 #endif 735 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free; 736 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb); 737 ISP_PCMD(ccb) = NULL; 738 } 739 } 740 741 /* 742 * Put the target mode functions here, because some are inlines 743 */ 744 #ifdef ISP_TARGET_MODE 745 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t); 746 static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t); 747 static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t); 748 static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *); 749 static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int); 750 static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t); 751 static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *); 752 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **); 753 static void destroy_lun_state(ispsoftc_t *, int, tstate_t *); 754 static void isp_enable_lun(ispsoftc_t *, union ccb *); 755 static void isp_disable_lun(ispsoftc_t *, union ccb *); 756 static timeout_t isp_refire_putback_atio; 757 static timeout_t isp_refire_notify_ack; 758 static void isp_complete_ctio(union ccb *); 759 static void isp_target_putback_atio(union ccb *); 760 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE }; 761 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How); 762 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *); 763 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *); 764 static void isp_handle_platform_ctio(ispsoftc_t *, void *); 765 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp); 766 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *); 767 static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t); 768 769 static ISP_INLINE tstate_t * 770 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun) 771 { 772 tstate_t *tptr = NULL; 773 struct tslist *lhp; 774 775 if (bus < isp->isp_nchan) { 776 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp); 777 SLIST_FOREACH(tptr, lhp, next) { 778 if (tptr->ts_lun == lun) 779 return (tptr); 780 } 781 } 782 return (NULL); 783 } 784 785 static int 786 isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr) 787 { 788 inot_private_data_t *ntp; 789 struct ntpdlist rq; 790 791 if (STAILQ_EMPTY(&tptr->restart_queue)) 792 return (0); 793 STAILQ_INIT(&rq); 794 STAILQ_CONCAT(&rq, &tptr->restart_queue); 795 while ((ntp = STAILQ_FIRST(&rq)) != NULL) { 796 STAILQ_REMOVE_HEAD(&rq, next); 797 if (IS_24XX(isp)) { 798 isp_prt(isp, ISP_LOGTDEBUG0, 799 "%s: restarting resrc deprived %x", __func__, 800 ((at7_entry_t *)ntp->data)->at_rxid); 801 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data); 802 } else { 803 isp_prt(isp, ISP_LOGTDEBUG0, 804 "%s: restarting resrc deprived %x", __func__, 805 ((at2_entry_t *)ntp->data)->at_rxid); 806 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data); 807 } 808 isp_put_ntpd(isp, bus, ntp); 809 if (!STAILQ_EMPTY(&tptr->restart_queue)) 810 break; 811 } 812 if (!STAILQ_EMPTY(&rq)) { 813 STAILQ_CONCAT(&rq, &tptr->restart_queue); 814 STAILQ_CONCAT(&tptr->restart_queue, &rq); 815 } 816 return (!STAILQ_EMPTY(&tptr->restart_queue)); 817 } 818 819 static void 820 isp_tmcmd_restart(ispsoftc_t *isp) 821 { 822 tstate_t *tptr; 823 union ccb *ccb; 824 struct tslist *lhp; 825 struct isp_ccbq *waitq; 826 int bus, i; 827 828 for (bus = 0; bus < isp->isp_nchan; bus++) { 829 for (i = 0; i < LUN_HASH_SIZE; i++) { 830 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp); 831 SLIST_FOREACH(tptr, lhp, next) 832 isp_atio_restart(isp, bus, tptr); 833 } 834 835 /* 836 * We only need to do this once per channel. 837 */ 838 ISP_GET_PC_ADDR(isp, bus, waitq, waitq); 839 ccb = (union ccb *)TAILQ_FIRST(waitq); 840 if (ccb != NULL) { 841 TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe); 842 isp_target_start_ctio(isp, ccb, FROM_TIMER); 843 } 844 } 845 } 846 847 static atio_private_data_t * 848 isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag) 849 { 850 struct atpdlist *atfree; 851 struct atpdlist *atused; 852 atio_private_data_t *atp; 853 854 ISP_GET_PC_ADDR(isp, chan, atfree, atfree); 855 atp = LIST_FIRST(atfree); 856 if (atp) { 857 LIST_REMOVE(atp, next); 858 atp->tag = tag; 859 ISP_GET_PC(isp, chan, atused, atused); 860 LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next); 861 } 862 return (atp); 863 } 864 865 static atio_private_data_t * 866 isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag) 867 { 868 struct atpdlist *atused; 869 atio_private_data_t *atp; 870 871 ISP_GET_PC(isp, chan, atused, atused); 872 LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) { 873 if (atp->tag == tag) 874 return (atp); 875 } 876 return (NULL); 877 } 878 879 static void 880 isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp) 881 { 882 struct atpdlist *atfree; 883 884 if (atp->ests) { 885 isp_put_ecmd(isp, atp->ests); 886 } 887 LIST_REMOVE(atp, next); 888 memset(atp, 0, sizeof (*atp)); 889 ISP_GET_PC_ADDR(isp, chan, atfree, atfree); 890 LIST_INSERT_HEAD(atfree, atp, next); 891 } 892 893 static void 894 isp_dump_atpd(ispsoftc_t *isp, int chan) 895 { 896 atio_private_data_t *atp, *atpool; 897 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" }; 898 899 ISP_GET_PC(isp, chan, atpool, atpool); 900 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) { 901 if (atp->state == ATPD_STATE_FREE) 902 continue; 903 isp_prt(isp, ISP_LOGALL, "Chan %d ATP [0x%x] origdlen %u bytes_xfrd %u lun %jx nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s", 904 chan, atp->tag, atp->orig_datalen, atp->bytes_xfered, (uintmax_t)atp->lun, atp->nphdl, atp->sid, atp->did, atp->oxid, states[atp->state & 0x7]); 905 } 906 } 907 908 static inot_private_data_t * 909 isp_get_ntpd(ispsoftc_t *isp, int chan) 910 { 911 struct ntpdlist *ntfree; 912 inot_private_data_t *ntp; 913 914 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree); 915 ntp = STAILQ_FIRST(ntfree); 916 if (ntp) 917 STAILQ_REMOVE_HEAD(ntfree, next); 918 return (ntp); 919 } 920 921 static inot_private_data_t * 922 isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id) 923 { 924 inot_private_data_t *ntp, *ntp2; 925 926 ISP_GET_PC(isp, chan, ntpool, ntp); 927 ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2); 928 for (; ntp < ntp2; ntp++) { 929 if (ntp->tag_id == tag_id && ntp->seq_id == seq_id) 930 return (ntp); 931 } 932 return (NULL); 933 } 934 935 static void 936 isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp) 937 { 938 struct ntpdlist *ntfree; 939 940 ntp->tag_id = ntp->seq_id = 0; 941 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree); 942 STAILQ_INSERT_HEAD(ntfree, ntp, next); 943 } 944 945 static cam_status 946 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt) 947 { 948 lun_id_t lun; 949 struct tslist *lhp; 950 tstate_t *tptr; 951 952 lun = xpt_path_lun_id(path); 953 if (lun != CAM_LUN_WILDCARD) { 954 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) { 955 return (CAM_LUN_INVALID); 956 } 957 } 958 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO); 959 if (tptr == NULL) { 960 return (CAM_RESRC_UNAVAIL); 961 } 962 tptr->ts_lun = lun; 963 SLIST_INIT(&tptr->atios); 964 SLIST_INIT(&tptr->inots); 965 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp); 966 SLIST_INSERT_HEAD(lhp, tptr, next); 967 *rslt = tptr; 968 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n"); 969 return (CAM_REQ_CMP); 970 } 971 972 static void 973 destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr) 974 { 975 union ccb *ccb; 976 struct tslist *lhp; 977 inot_private_data_t *ntp; 978 979 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) { 980 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle); 981 ccb->ccb_h.status = CAM_REQ_ABORTED; 982 xpt_done(ccb); 983 }; 984 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) { 985 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle); 986 ccb->ccb_h.status = CAM_REQ_ABORTED; 987 xpt_done(ccb); 988 } 989 while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) { 990 isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0); 991 STAILQ_REMOVE_HEAD(&tptr->restart_queue, next); 992 isp_put_ntpd(isp, bus, ntp); 993 } 994 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp); 995 SLIST_REMOVE(lhp, tptr, tstate, next); 996 free(tptr, M_DEVBUF); 997 } 998 999 static void 1000 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb) 1001 { 1002 tstate_t *tptr; 1003 int bus; 1004 target_id_t target; 1005 lun_id_t lun; 1006 1007 if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) { 1008 xpt_print(ccb->ccb_h.path, "Target mode is not supported\n"); 1009 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 1010 xpt_done(ccb); 1011 return; 1012 } 1013 1014 /* 1015 * We only support either target and lun both wildcard 1016 * or target and lun both non-wildcard. 1017 */ 1018 bus = XS_CHANNEL(ccb); 1019 target = ccb->ccb_h.target_id; 1020 lun = ccb->ccb_h.target_lun; 1021 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, 1022 "enabling lun %jx\n", (uintmax_t)lun); 1023 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) { 1024 ccb->ccb_h.status = CAM_LUN_INVALID; 1025 xpt_done(ccb); 1026 return; 1027 } 1028 1029 /* Create the state pointer. It should not already exist. */ 1030 tptr = get_lun_statep(isp, bus, lun); 1031 if (tptr) { 1032 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA; 1033 xpt_done(ccb); 1034 return; 1035 } 1036 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr); 1037 if (ccb->ccb_h.status != CAM_REQ_CMP) { 1038 xpt_done(ccb); 1039 return; 1040 } 1041 1042 ccb->ccb_h.status = CAM_REQ_CMP; 1043 xpt_done(ccb); 1044 } 1045 1046 static void 1047 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb) 1048 { 1049 tstate_t *tptr = NULL; 1050 int bus; 1051 target_id_t target; 1052 lun_id_t lun; 1053 1054 bus = XS_CHANNEL(ccb); 1055 target = ccb->ccb_h.target_id; 1056 lun = ccb->ccb_h.target_lun; 1057 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, 1058 "disabling lun %jx\n", (uintmax_t)lun); 1059 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) { 1060 ccb->ccb_h.status = CAM_LUN_INVALID; 1061 xpt_done(ccb); 1062 return; 1063 } 1064 1065 /* Find the state pointer. */ 1066 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) { 1067 ccb->ccb_h.status = CAM_PATH_INVALID; 1068 xpt_done(ccb); 1069 return; 1070 } 1071 1072 destroy_lun_state(isp, bus, tptr); 1073 ccb->ccb_h.status = CAM_REQ_CMP; 1074 xpt_done(ccb); 1075 } 1076 1077 static void 1078 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how) 1079 { 1080 int fctape, sendstatus, resid; 1081 fcparam *fcp; 1082 atio_private_data_t *atp; 1083 struct ccb_scsiio *cso; 1084 struct isp_ccbq *waitq; 1085 uint32_t dmaresult, handle, xfrlen, sense_length, tmp; 1086 uint8_t local[QENTRY_LEN]; 1087 1088 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len, 1089 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0)); 1090 1091 ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq); 1092 switch (how) { 1093 case FROM_CAM: 1094 /* 1095 * Insert at the tail of the list, if any, waiting CTIO CCBs 1096 */ 1097 TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, sim_links.tqe); 1098 break; 1099 case FROM_TIMER: 1100 case FROM_SRR: 1101 case FROM_CTIO_DONE: 1102 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1103 break; 1104 } 1105 1106 while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) { 1107 TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe); 1108 1109 cso = &ccb->csio; 1110 xfrlen = cso->dxfer_len; 1111 if (xfrlen == 0) { 1112 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) { 1113 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n"); 1114 ccb->ccb_h.status = CAM_REQ_INVALID; 1115 xpt_done(ccb); 1116 continue; 1117 } 1118 } 1119 1120 atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id); 1121 if (atp == NULL) { 1122 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__); 1123 isp_dump_atpd(isp, XS_CHANNEL(ccb)); 1124 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1125 xpt_done(ccb); 1126 continue; 1127 } 1128 1129 /* 1130 * Is this command a dead duck? 1131 */ 1132 if (atp->dead) { 1133 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id); 1134 ccb->ccb_h.status = CAM_REQ_ABORTED; 1135 xpt_done(ccb); 1136 continue; 1137 } 1138 1139 /* 1140 * Check to make sure we're still in target mode. 1141 */ 1142 fcp = FCPARAM(isp, XS_CHANNEL(ccb)); 1143 if ((fcp->role & ISP_ROLE_TARGET) == 0) { 1144 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id); 1145 ccb->ccb_h.status = CAM_PROVIDE_FAIL; 1146 xpt_done(ccb); 1147 continue; 1148 } 1149 1150 /* 1151 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which 1152 * could be split into two CTIOs to split data and status). 1153 */ 1154 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) { 1155 isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags); 1156 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1157 break; 1158 } 1159 1160 /* 1161 * Does the initiator expect FC-Tape style responses? 1162 */ 1163 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) { 1164 fctape = 1; 1165 } else { 1166 fctape = 0; 1167 } 1168 1169 /* 1170 * If we already did the data xfer portion of a CTIO that sends data 1171 * and status, don't do it again and do the status portion now. 1172 */ 1173 if (atp->sendst) { 1174 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u", 1175 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit); 1176 xfrlen = 0; /* we already did the data transfer */ 1177 atp->sendst = 0; 1178 } 1179 if (ccb->ccb_h.flags & CAM_SEND_STATUS) { 1180 sendstatus = 1; 1181 } else { 1182 sendstatus = 0; 1183 } 1184 1185 if (ccb->ccb_h.flags & CAM_SEND_SENSE) { 1186 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?")); 1187 /* 1188 * Sense length is not the entire sense data structure size. Periph 1189 * drivers don't seem to be setting sense_len to reflect the actual 1190 * size. We'll peek inside to get the right amount. 1191 */ 1192 sense_length = cso->sense_len; 1193 1194 /* 1195 * This 'cannot' happen 1196 */ 1197 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) { 1198 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE; 1199 } 1200 } else { 1201 sense_length = 0; 1202 } 1203 1204 memset(local, 0, QENTRY_LEN); 1205 1206 /* 1207 * Check for overflow 1208 */ 1209 tmp = atp->bytes_xfered + atp->bytes_in_transit; 1210 if (xfrlen > 0 && tmp > atp->orig_datalen) { 1211 isp_prt(isp, ISP_LOGERR, 1212 "%s: [0x%x] data overflow by %u bytes", __func__, 1213 cso->tag_id, tmp + xfrlen - atp->orig_datalen); 1214 ccb->ccb_h.status = CAM_DATA_RUN_ERR; 1215 xpt_done(ccb); 1216 continue; 1217 } 1218 if (xfrlen > atp->orig_datalen - tmp) { 1219 xfrlen = atp->orig_datalen - tmp; 1220 if (xfrlen == 0 && !sendstatus) { 1221 cso->resid = cso->dxfer_len; 1222 ccb->ccb_h.status = CAM_REQ_CMP; 1223 xpt_done(ccb); 1224 continue; 1225 } 1226 } 1227 1228 if (IS_24XX(isp)) { 1229 ct7_entry_t *cto = (ct7_entry_t *) local; 1230 1231 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7; 1232 cto->ct_header.rqs_entry_count = 1; 1233 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM; 1234 ATPD_SET_SEQNO(cto, atp); 1235 cto->ct_nphdl = atp->nphdl; 1236 cto->ct_rxid = atp->tag; 1237 cto->ct_iid_lo = atp->sid; 1238 cto->ct_iid_hi = atp->sid >> 16; 1239 cto->ct_oxid = atp->oxid; 1240 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb)); 1241 cto->ct_timeout = XS_TIME(ccb); 1242 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT; 1243 1244 /* 1245 * Mode 1, status, no data. Only possible when we are sending status, have 1246 * no data to transfer, and any sense data can fit into a ct7_entry_t. 1247 * 1248 * Mode 2, status, no data. We have to use this in the case that 1249 * the sense data won't fit into a ct7_entry_t. 1250 * 1251 */ 1252 if (sendstatus && xfrlen == 0) { 1253 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA; 1254 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit; 1255 if (sense_length <= MAXRESPLEN_24XX) { 1256 cto->ct_flags |= CT7_FLAG_MODE1; 1257 cto->ct_scsi_status = cso->scsi_status; 1258 if (resid < 0) { 1259 cto->ct_resid = -resid; 1260 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8); 1261 } else if (resid > 0) { 1262 cto->ct_resid = resid; 1263 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8); 1264 } 1265 if (fctape) { 1266 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF; 1267 } 1268 if (sense_length) { 1269 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8); 1270 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length; 1271 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length); 1272 } 1273 } else { 1274 bus_addr_t addr; 1275 char buf[XCMD_SIZE]; 1276 fcp_rsp_iu_t *rp; 1277 1278 if (atp->ests == NULL) { 1279 atp->ests = isp_get_ecmd(isp); 1280 if (atp->ests == NULL) { 1281 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1282 break; 1283 } 1284 } 1285 memset(buf, 0, sizeof (buf)); 1286 rp = (fcp_rsp_iu_t *)buf; 1287 if (fctape) { 1288 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF; 1289 rp->fcp_rsp_bits |= FCP_CONF_REQ; 1290 } 1291 cto->ct_flags |= CT7_FLAG_MODE2; 1292 rp->fcp_rsp_scsi_status = cso->scsi_status; 1293 if (resid < 0) { 1294 rp->fcp_rsp_resid = -resid; 1295 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW; 1296 } else if (resid > 0) { 1297 rp->fcp_rsp_resid = resid; 1298 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW; 1299 } 1300 if (sense_length) { 1301 rp->fcp_rsp_snslen = sense_length; 1302 cto->ct_senselen = sense_length; 1303 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID; 1304 isp_put_fcp_rsp_iu(isp, rp, atp->ests); 1305 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length); 1306 } else { 1307 isp_put_fcp_rsp_iu(isp, rp, atp->ests); 1308 } 1309 if (isp->isp_dblev & ISP_LOGTDEBUG1) { 1310 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests); 1311 } 1312 addr = isp->isp_osinfo.ecmd_dma; 1313 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE); 1314 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests, 1315 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length); 1316 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length; 1317 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr); 1318 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr); 1319 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length; 1320 } 1321 if (sense_length) { 1322 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__, 1323 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length, 1324 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]); 1325 } else { 1326 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, 1327 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid); 1328 } 1329 atp->state = ATPD_STATE_LAST_CTIO; 1330 } 1331 1332 /* 1333 * Mode 0 data transfers, *possibly* with status. 1334 */ 1335 if (xfrlen != 0) { 1336 cto->ct_flags |= CT7_FLAG_MODE0; 1337 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1338 cto->ct_flags |= CT7_DATA_IN; 1339 } else { 1340 cto->ct_flags |= CT7_DATA_OUT; 1341 } 1342 1343 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit; 1344 cto->rsp.m0.ct_xfrlen = xfrlen; 1345 1346 #ifdef DEBUG 1347 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) { 1348 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2)); 1349 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0; 1350 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2; 1351 } 1352 #endif 1353 if (sendstatus) { 1354 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen; 1355 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) { 1356 cto->ct_flags |= CT7_SENDSTATUS; 1357 atp->state = ATPD_STATE_LAST_CTIO; 1358 if (fctape) { 1359 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF; 1360 } 1361 } else { 1362 atp->sendst = 1; /* send status later */ 1363 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM; 1364 atp->state = ATPD_STATE_CTIO; 1365 } 1366 } else { 1367 atp->state = ATPD_STATE_CTIO; 1368 } 1369 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__, 1370 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered); 1371 } 1372 } else { 1373 ct2_entry_t *cto = (ct2_entry_t *) local; 1374 1375 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2; 1376 cto->ct_header.rqs_entry_count = 1; 1377 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM; 1378 ATPD_SET_SEQNO(cto, atp); 1379 if (ISP_CAP_2KLOGIN(isp)) { 1380 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl; 1381 } else { 1382 cto->ct_iid = atp->nphdl; 1383 if (ISP_CAP_SCCFW(isp) == 0) { 1384 cto->ct_lun = ccb->ccb_h.target_lun; 1385 } 1386 } 1387 cto->ct_timeout = XS_TIME(ccb); 1388 cto->ct_rxid = cso->tag_id; 1389 1390 /* 1391 * Mode 1, status, no data. Only possible when we are sending status, have 1392 * no data to transfer, and the sense length can fit in the ct7_entry. 1393 * 1394 * Mode 2, status, no data. We have to use this in the case the response 1395 * length won't fit into a ct2_entry_t. 1396 * 1397 * We'll fill out this structure with information as if this were a 1398 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as 1399 * needed based upon this. 1400 */ 1401 if (sendstatus && xfrlen == 0) { 1402 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA; 1403 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit; 1404 if (sense_length <= MAXRESPLEN) { 1405 if (resid < 0) { 1406 cto->ct_resid = -resid; 1407 } else if (resid > 0) { 1408 cto->ct_resid = resid; 1409 } 1410 cto->ct_flags |= CT2_FLAG_MODE1; 1411 cto->rsp.m1.ct_scsi_status = cso->scsi_status; 1412 if (resid < 0) { 1413 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER; 1414 } else if (resid > 0) { 1415 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER; 1416 } 1417 if (fctape) { 1418 cto->ct_flags |= CT2_CONFIRM; 1419 } 1420 if (sense_length) { 1421 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID; 1422 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length; 1423 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length); 1424 } 1425 } else { 1426 bus_addr_t addr; 1427 char buf[XCMD_SIZE]; 1428 fcp_rsp_iu_t *rp; 1429 1430 if (atp->ests == NULL) { 1431 atp->ests = isp_get_ecmd(isp); 1432 if (atp->ests == NULL) { 1433 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1434 break; 1435 } 1436 } 1437 memset(buf, 0, sizeof (buf)); 1438 rp = (fcp_rsp_iu_t *)buf; 1439 if (fctape) { 1440 cto->ct_flags |= CT2_CONFIRM; 1441 rp->fcp_rsp_bits |= FCP_CONF_REQ; 1442 } 1443 cto->ct_flags |= CT2_FLAG_MODE2; 1444 rp->fcp_rsp_scsi_status = cso->scsi_status; 1445 if (resid < 0) { 1446 rp->fcp_rsp_resid = -resid; 1447 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW; 1448 } else if (resid > 0) { 1449 rp->fcp_rsp_resid = resid; 1450 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW; 1451 } 1452 if (sense_length) { 1453 rp->fcp_rsp_snslen = sense_length; 1454 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID; 1455 isp_put_fcp_rsp_iu(isp, rp, atp->ests); 1456 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length); 1457 } else { 1458 isp_put_fcp_rsp_iu(isp, rp, atp->ests); 1459 } 1460 if (isp->isp_dblev & ISP_LOGTDEBUG1) { 1461 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests); 1462 } 1463 addr = isp->isp_osinfo.ecmd_dma; 1464 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE); 1465 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests, 1466 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length); 1467 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length; 1468 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr); 1469 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length; 1470 } 1471 if (sense_length) { 1472 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__, 1473 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, 1474 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]); 1475 } else { 1476 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid, 1477 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid); 1478 } 1479 atp->state = ATPD_STATE_LAST_CTIO; 1480 } 1481 1482 if (xfrlen != 0) { 1483 cto->ct_flags |= CT2_FLAG_MODE0; 1484 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 1485 cto->ct_flags |= CT2_DATA_IN; 1486 } else { 1487 cto->ct_flags |= CT2_DATA_OUT; 1488 } 1489 1490 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit; 1491 cto->rsp.m0.ct_xfrlen = xfrlen; 1492 1493 if (sendstatus) { 1494 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen; 1495 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) { 1496 cto->ct_flags |= CT2_SENDSTATUS; 1497 atp->state = ATPD_STATE_LAST_CTIO; 1498 if (fctape) { 1499 cto->ct_flags |= CT2_CONFIRM; 1500 } 1501 } else { 1502 atp->sendst = 1; /* send status later */ 1503 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM; 1504 atp->state = ATPD_STATE_CTIO; 1505 } 1506 } else { 1507 atp->state = ATPD_STATE_CTIO; 1508 } 1509 } 1510 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid, 1511 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered); 1512 } 1513 1514 if (isp_get_pcmd(isp, ccb)) { 1515 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n"); 1516 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1517 break; 1518 } 1519 handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET); 1520 if (handle == 0) { 1521 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__); 1522 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1523 isp_free_pcmd(isp, ccb); 1524 break; 1525 } 1526 atp->bytes_in_transit += xfrlen; 1527 PISP_PCMD(ccb)->datalen = xfrlen; 1528 1529 1530 /* 1531 * Call the dma setup routines for this entry (and any subsequent 1532 * CTIOs) if there's data to move, and then tell the f/w it's got 1533 * new things to play with. As with isp_start's usage of DMA setup, 1534 * any swizzling is done in the machine dependent layer. Because 1535 * of this, we put the request onto the queue area first in native 1536 * format. 1537 */ 1538 1539 if (IS_24XX(isp)) { 1540 ct7_entry_t *cto = (ct7_entry_t *) local; 1541 cto->ct_syshandle = handle; 1542 } else { 1543 ct2_entry_t *cto = (ct2_entry_t *) local; 1544 cto->ct_syshandle = handle; 1545 } 1546 1547 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local); 1548 if (dmaresult != CMD_QUEUED) { 1549 isp_destroy_handle(isp, handle); 1550 isp_free_pcmd(isp, ccb); 1551 if (dmaresult == CMD_EAGAIN) { 1552 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe); 1553 break; 1554 } 1555 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1556 xpt_done(ccb); 1557 continue; 1558 } 1559 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED; 1560 if (xfrlen) { 1561 ccb->ccb_h.spriv_field0 = atp->bytes_xfered; 1562 } else { 1563 ccb->ccb_h.spriv_field0 = ~0; 1564 } 1565 atp->ctcnt++; 1566 atp->seqno++; 1567 } 1568 } 1569 1570 static void 1571 isp_refire_putback_atio(void *arg) 1572 { 1573 union ccb *ccb = arg; 1574 1575 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb)); 1576 isp_target_putback_atio(ccb); 1577 } 1578 1579 static void 1580 isp_refire_notify_ack(void *arg) 1581 { 1582 isp_tna_t *tp = arg; 1583 ispsoftc_t *isp = tp->isp; 1584 1585 ISP_ASSERT_LOCKED(isp); 1586 if (isp_notify_ack(isp, tp->not)) { 1587 callout_schedule(&tp->timer, 5); 1588 } else { 1589 free(tp, M_DEVBUF); 1590 } 1591 } 1592 1593 1594 static void 1595 isp_target_putback_atio(union ccb *ccb) 1596 { 1597 ispsoftc_t *isp = XS_ISP(ccb); 1598 struct ccb_scsiio *cso = &ccb->csio; 1599 at2_entry_t local, *at = &local; 1600 1601 ISP_MEMZERO(at, sizeof (at2_entry_t)); 1602 at->at_header.rqs_entry_type = RQSTYPE_ATIO2; 1603 at->at_header.rqs_entry_count = 1; 1604 if (ISP_CAP_SCCFW(isp)) { 1605 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun; 1606 } else { 1607 at->at_lun = (uint8_t) ccb->ccb_h.target_lun; 1608 } 1609 at->at_status = CT_OK; 1610 at->at_rxid = cso->tag_id; 1611 at->at_iid = cso->ccb_h.target_id; 1612 if (isp_target_put_entry(isp, at)) { 1613 callout_reset(&PISP_PCMD(ccb)->wdog, 10, 1614 isp_refire_putback_atio, ccb); 1615 } else 1616 isp_complete_ctio(ccb); 1617 } 1618 1619 static void 1620 isp_complete_ctio(union ccb *ccb) 1621 { 1622 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) { 1623 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 1624 xpt_done(ccb); 1625 } 1626 } 1627 1628 static void 1629 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep) 1630 { 1631 fcparam *fcp; 1632 lun_id_t lun; 1633 fcportdb_t *lp; 1634 tstate_t *tptr; 1635 struct ccb_accept_tio *atiop; 1636 uint16_t nphdl; 1637 atio_private_data_t *atp; 1638 inot_private_data_t *ntp; 1639 1640 /* 1641 * The firmware status (except for the QLTM_SVALID bit) 1642 * indicates why this ATIO was sent to us. 1643 * 1644 * If QLTM_SVALID is set, the firmware has recommended Sense Data. 1645 */ 1646 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) { 1647 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status); 1648 isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0); 1649 return; 1650 } 1651 1652 fcp = FCPARAM(isp, 0); 1653 if (ISP_CAP_SCCFW(isp)) { 1654 lun = aep->at_scclun; 1655 } else { 1656 lun = aep->at_lun; 1657 } 1658 if (ISP_CAP_2KLOGIN(isp)) { 1659 nphdl = ((at2e_entry_t *)aep)->at_iid; 1660 } else { 1661 nphdl = aep->at_iid; 1662 } 1663 tptr = get_lun_statep(isp, 0, lun); 1664 if (tptr == NULL) { 1665 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD); 1666 if (tptr == NULL) { 1667 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun); 1668 if (lun == 0) { 1669 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0); 1670 } else { 1671 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0); 1672 } 1673 return; 1674 } 1675 } 1676 1677 /* 1678 * Start any commands pending resources first. 1679 */ 1680 if (isp_atio_restart(isp, 0, tptr)) 1681 goto noresrc; 1682 1683 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios); 1684 if (atiop == NULL) { 1685 goto noresrc; 1686 } 1687 1688 atp = isp_get_atpd(isp, 0, aep->at_rxid); 1689 if (atp == NULL) { 1690 goto noresrc; 1691 } 1692 1693 atp->state = ATPD_STATE_ATIO; 1694 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle); 1695 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n"); 1696 atiop->ccb_h.target_id = fcp->isp_loopid; 1697 atiop->ccb_h.target_lun = lun; 1698 1699 /* 1700 * We don't get 'suggested' sense data as we do with SCSI cards. 1701 */ 1702 atiop->sense_len = 0; 1703 1704 /* 1705 * If we're not in the port database, add ourselves. 1706 */ 1707 if (IS_2100(isp)) 1708 atiop->init_id = nphdl; 1709 else { 1710 if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) { 1711 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp); 1712 } else { 1713 isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB", 1714 __func__, nphdl); 1715 isp_dump_portdb(isp, 0); 1716 isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0); 1717 return; 1718 } 1719 } 1720 atiop->cdb_len = ATIO2_CDBLEN; 1721 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN); 1722 atiop->ccb_h.status = CAM_CDB_RECVD; 1723 atiop->tag_id = atp->tag; 1724 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) { 1725 case ATIO2_TC_ATTR_SIMPLEQ: 1726 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1727 atiop->tag_action = MSG_SIMPLE_Q_TAG; 1728 break; 1729 case ATIO2_TC_ATTR_HEADOFQ: 1730 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1731 atiop->tag_action = MSG_HEAD_OF_Q_TAG; 1732 break; 1733 case ATIO2_TC_ATTR_ORDERED: 1734 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1735 atiop->tag_action = MSG_ORDERED_Q_TAG; 1736 break; 1737 case ATIO2_TC_ATTR_ACAQ: /* ?? */ 1738 case ATIO2_TC_ATTR_UNTAGGED: 1739 default: 1740 atiop->tag_action = 0; 1741 break; 1742 } 1743 1744 atp->orig_datalen = aep->at_datalen; 1745 atp->bytes_xfered = 0; 1746 atp->lun = lun; 1747 atp->nphdl = nphdl; 1748 atp->sid = PORT_ANY; 1749 atp->oxid = aep->at_oxid; 1750 atp->cdb0 = aep->at_cdb[0]; 1751 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK; 1752 atp->state = ATPD_STATE_CAM; 1753 xpt_done((union ccb *)atiop); 1754 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %jx datalen %u", aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen); 1755 return; 1756 noresrc: 1757 ntp = isp_get_ntpd(isp, 0); 1758 if (ntp == NULL) { 1759 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0); 1760 return; 1761 } 1762 memcpy(ntp->data, aep, QENTRY_LEN); 1763 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next); 1764 } 1765 1766 static void 1767 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep) 1768 { 1769 int cdbxlen; 1770 lun_id_t lun; 1771 uint16_t chan, nphdl = NIL_HANDLE; 1772 uint32_t did, sid; 1773 fcportdb_t *lp; 1774 tstate_t *tptr; 1775 struct ccb_accept_tio *atiop; 1776 atio_private_data_t *atp = NULL; 1777 atio_private_data_t *oatp; 1778 inot_private_data_t *ntp; 1779 1780 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2]; 1781 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2]; 1782 lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun)); 1783 1784 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) { 1785 /* Channel has to be derived from D_ID */ 1786 isp_find_chan_by_did(isp, did, &chan); 1787 if (chan == ISP_NOCHAN) { 1788 isp_prt(isp, ISP_LOGWARN, 1789 "%s: [RX_ID 0x%x] D_ID %x not found on any channel", 1790 __func__, aep->at_rxid, did); 1791 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, 1792 ECMD_TERMINATE, 0); 1793 return; 1794 } 1795 } else { 1796 chan = 0; 1797 } 1798 1799 /* 1800 * Find the PDB entry for this initiator 1801 */ 1802 if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) { 1803 /* 1804 * If we're not in the port database terminate the exchange. 1805 */ 1806 isp_prt(isp, ISP_LOGTINFO, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x wasn't in PDB already", 1807 __func__, aep->at_rxid, did, chan, sid); 1808 isp_dump_portdb(isp, chan); 1809 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0); 1810 return; 1811 } 1812 nphdl = lp->handle; 1813 1814 /* 1815 * Get the tstate pointer 1816 */ 1817 tptr = get_lun_statep(isp, chan, lun); 1818 if (tptr == NULL) { 1819 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD); 1820 if (tptr == NULL) { 1821 isp_prt(isp, ISP_LOGWARN, 1822 "%s: [0x%x] no state pointer for lun %jx or wildcard", 1823 __func__, aep->at_rxid, (uintmax_t)lun); 1824 if (lun == 0) { 1825 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0); 1826 } else { 1827 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0); 1828 } 1829 return; 1830 } 1831 } 1832 1833 /* 1834 * Start any commands pending resources first. 1835 */ 1836 if (isp_atio_restart(isp, chan, tptr)) 1837 goto noresrc; 1838 1839 /* 1840 * If the f/w is out of resources, just send a BUSY status back. 1841 */ 1842 if (aep->at_rxid == AT7_NORESRC_RXID) { 1843 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0); 1844 return; 1845 } 1846 1847 /* 1848 * If we're out of resources, just send a BUSY status back. 1849 */ 1850 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios); 1851 if (atiop == NULL) { 1852 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid); 1853 goto noresrc; 1854 } 1855 1856 oatp = isp_find_atpd(isp, chan, aep->at_rxid); 1857 if (oatp) { 1858 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d", 1859 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state); 1860 /* 1861 * It's not a "no resource" condition- but we can treat it like one 1862 */ 1863 goto noresrc; 1864 } 1865 atp = isp_get_atpd(isp, chan, aep->at_rxid); 1866 if (atp == NULL) { 1867 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid); 1868 goto noresrc; 1869 } 1870 atp->word3 = lp->prli_word3; 1871 atp->state = ATPD_STATE_ATIO; 1872 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle); 1873 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n"); 1874 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp); 1875 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid; 1876 atiop->ccb_h.target_lun = lun; 1877 atiop->sense_len = 0; 1878 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT; 1879 if (cdbxlen) { 1880 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored"); 1881 } 1882 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb); 1883 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen); 1884 atiop->cdb_len = cdbxlen; 1885 atiop->ccb_h.status = CAM_CDB_RECVD; 1886 atiop->tag_id = atp->tag; 1887 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) { 1888 case FCP_CMND_TASK_ATTR_SIMPLE: 1889 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1890 atiop->tag_action = MSG_SIMPLE_Q_TAG; 1891 break; 1892 case FCP_CMND_TASK_ATTR_HEAD: 1893 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1894 atiop->tag_action = MSG_HEAD_OF_Q_TAG; 1895 break; 1896 case FCP_CMND_TASK_ATTR_ORDERED: 1897 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID; 1898 atiop->tag_action = MSG_ORDERED_Q_TAG; 1899 break; 1900 default: 1901 /* FALLTHROUGH */ 1902 case FCP_CMND_TASK_ATTR_ACA: 1903 case FCP_CMND_TASK_ATTR_UNTAGGED: 1904 atiop->tag_action = 0; 1905 break; 1906 } 1907 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl; 1908 atp->bytes_xfered = 0; 1909 atp->lun = lun; 1910 atp->nphdl = nphdl; 1911 atp->sid = sid; 1912 atp->did = did; 1913 atp->oxid = aep->at_hdr.ox_id; 1914 atp->rxid = aep->at_hdr.rx_id; 1915 atp->cdb0 = atiop->cdb_io.cdb_bytes[0]; 1916 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK; 1917 atp->state = ATPD_STATE_CAM; 1918 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u", 1919 aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen); 1920 xpt_done((union ccb *)atiop); 1921 return; 1922 noresrc: 1923 if (atp) 1924 isp_put_atpd(isp, chan, atp); 1925 ntp = isp_get_ntpd(isp, chan); 1926 if (ntp == NULL) { 1927 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0); 1928 return; 1929 } 1930 memcpy(ntp->data, aep, QENTRY_LEN); 1931 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next); 1932 } 1933 1934 1935 /* 1936 * Handle starting an SRR (sequence retransmit request) 1937 * We get here when we've gotten the immediate notify 1938 * and the return of all outstanding CTIOs for this 1939 * transaction. 1940 */ 1941 static void 1942 isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp) 1943 { 1944 in_fcentry_24xx_t *inot; 1945 uint32_t srr_off, ccb_off, ccb_len, ccb_end; 1946 union ccb *ccb; 1947 1948 inot = (in_fcentry_24xx_t *)atp->srr; 1949 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16); 1950 ccb = atp->srr_ccb; 1951 atp->srr_ccb = NULL; 1952 atp->nsrr++; 1953 if (ccb == NULL) { 1954 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag); 1955 goto fail; 1956 } 1957 1958 ccb_off = ccb->ccb_h.spriv_field0; 1959 ccb_len = ccb->csio.dxfer_len; 1960 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len; 1961 1962 switch (inot->in_srr_iu) { 1963 case R_CTL_INFO_SOLICITED_DATA: 1964 /* 1965 * We have to restart a FCP_DATA data out transaction 1966 */ 1967 atp->sendst = 0; 1968 atp->bytes_xfered = srr_off; 1969 if (ccb_len == 0) { 1970 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off); 1971 goto mdp; 1972 } 1973 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) { 1974 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end); 1975 goto mdp; 1976 } 1977 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end); 1978 break; 1979 case R_CTL_INFO_COMMAND_STATUS: 1980 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag); 1981 atp->sendst = 1; 1982 /* 1983 * We have to restart a FCP_RSP IU transaction 1984 */ 1985 break; 1986 case R_CTL_INFO_DATA_DESCRIPTOR: 1987 /* 1988 * We have to restart an FCP DATA in transaction 1989 */ 1990 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping"); 1991 goto fail; 1992 1993 default: 1994 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu); 1995 goto fail; 1996 } 1997 1998 /* 1999 * We can't do anything until this is acked, so we might as well start it now. 2000 * We aren't going to do the usual asynchronous ack issue because we need 2001 * to make sure this gets on the wire first. 2002 */ 2003 if (isp_notify_ack(isp, inot)) { 2004 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose"); 2005 goto fail; 2006 } 2007 isp_target_start_ctio(isp, ccb, FROM_SRR); 2008 return; 2009 fail: 2010 inot->in_reserved = 1; 2011 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot); 2012 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2013 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 2014 isp_complete_ctio(ccb); 2015 return; 2016 mdp: 2017 if (isp_notify_ack(isp, inot)) { 2018 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose"); 2019 goto fail; 2020 } 2021 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2022 ccb->ccb_h.status = CAM_MESSAGE_RECV; 2023 /* 2024 * This is not a strict interpretation of MDP, but it's close 2025 */ 2026 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16]; 2027 ccb->csio.msg_len = 7; 2028 ccb->csio.msg_ptr[0] = MSG_EXTENDED; 2029 ccb->csio.msg_ptr[1] = 5; 2030 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */ 2031 ccb->csio.msg_ptr[3] = srr_off >> 24; 2032 ccb->csio.msg_ptr[4] = srr_off >> 16; 2033 ccb->csio.msg_ptr[5] = srr_off >> 8; 2034 ccb->csio.msg_ptr[6] = srr_off; 2035 isp_complete_ctio(ccb); 2036 } 2037 2038 2039 static void 2040 isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify) 2041 { 2042 in_fcentry_24xx_t *inot = notify->nt_lreserved; 2043 atio_private_data_t *atp; 2044 uint32_t tag = notify->nt_tagval & 0xffffffff; 2045 2046 atp = isp_find_atpd(isp, notify->nt_channel, tag); 2047 if (atp == NULL) { 2048 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", 2049 __func__, tag); 2050 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot); 2051 return; 2052 } 2053 atp->srr_notify_rcvd = 1; 2054 memcpy(atp->srr, inot, sizeof (atp->srr)); 2055 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x", 2056 inot->in_rxid, inot->in_flags, inot->in_srr_iu, 2057 ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo); 2058 if (atp->srr_ccb) 2059 isp_handle_srr_start(isp, atp); 2060 } 2061 2062 static void 2063 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg) 2064 { 2065 union ccb *ccb; 2066 int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0; 2067 atio_private_data_t *atp = NULL; 2068 int bus; 2069 uint32_t handle, data_requested, resid; 2070 2071 handle = ((ct2_entry_t *)arg)->ct_syshandle; 2072 ccb = isp_find_xs(isp, handle); 2073 if (ccb == NULL) { 2074 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg); 2075 return; 2076 } 2077 isp_destroy_handle(isp, handle); 2078 resid = data_requested = PISP_PCMD(ccb)->datalen; 2079 isp_free_pcmd(isp, ccb); 2080 2081 bus = XS_CHANNEL(ccb); 2082 if (IS_24XX(isp)) { 2083 atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid); 2084 } else { 2085 atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid); 2086 } 2087 if (atp == NULL) { 2088 /* 2089 * XXX: isp_clear_commands() generates fake CTIO with zero 2090 * ct_rxid value, filling only ct_syshandle. Workaround 2091 * that using tag_id from the CCB, pointed by ct_syshandle. 2092 */ 2093 atp = isp_find_atpd(isp, bus, ccb->csio.tag_id); 2094 } 2095 if (atp == NULL) { 2096 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id); 2097 return; 2098 } 2099 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero")); 2100 atp->bytes_in_transit -= data_requested; 2101 atp->ctcnt -= 1; 2102 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 2103 2104 if (IS_24XX(isp)) { 2105 ct7_entry_t *ct = arg; 2106 2107 if (ct->ct_nphdl == CT7_SRR) { 2108 atp->srr_ccb = ccb; 2109 if (atp->srr_notify_rcvd) 2110 isp_handle_srr_start(isp, atp); 2111 return; 2112 } 2113 if (ct->ct_nphdl == CT_HBA_RESET) { 2114 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) && 2115 (atp->sendst == 0); 2116 failure = CAM_UNREC_HBA_ERROR; 2117 } else { 2118 sentstatus = ct->ct_flags & CT7_SENDSTATUS; 2119 ok = (ct->ct_nphdl == CT7_OK); 2120 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0; 2121 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) 2122 resid = ct->ct_resid; 2123 } 2124 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct), 2125 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID"); 2126 } else { 2127 ct2_entry_t *ct = arg; 2128 if (ct->ct_status == CT_SRR) { 2129 atp->srr_ccb = ccb; 2130 if (atp->srr_notify_rcvd) 2131 isp_handle_srr_start(isp, atp); 2132 isp_target_putback_atio(ccb); 2133 return; 2134 } 2135 if (ct->ct_status == CT_HBA_RESET) { 2136 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) && 2137 (atp->sendst == 0); 2138 failure = CAM_UNREC_HBA_ERROR; 2139 } else { 2140 sentstatus = ct->ct_flags & CT2_SENDSTATUS; 2141 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK; 2142 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0; 2143 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) 2144 resid = ct->ct_resid; 2145 } 2146 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct), 2147 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID"); 2148 } 2149 if (ok) { 2150 if (data_requested > 0) { 2151 atp->bytes_xfered += data_requested - resid; 2152 ccb->csio.resid = ccb->csio.dxfer_len - 2153 (data_requested - resid); 2154 } 2155 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) 2156 ccb->ccb_h.status |= CAM_SENT_SENSE; 2157 ccb->ccb_h.status |= CAM_REQ_CMP; 2158 } else { 2159 notify_cam = 1; 2160 if (failure == CAM_UNREC_HBA_ERROR) 2161 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR; 2162 else 2163 ccb->ccb_h.status |= CAM_REQ_CMP_ERR; 2164 } 2165 atp->state = ATPD_STATE_PDON; 2166 2167 /* 2168 * We never *not* notify CAM when there has been any error (ok == 0), 2169 * so we never need to do an ATIO putback if we're not notifying CAM. 2170 */ 2171 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)", 2172 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0); 2173 if (notify_cam == 0) { 2174 if (atp->sendst) { 2175 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE); 2176 } 2177 return; 2178 } 2179 2180 /* 2181 * We are done with this ATIO if we successfully sent status. 2182 * In all other cases expect either another CTIO or XPT_ABORT. 2183 */ 2184 if (ok && sentstatus) 2185 isp_put_atpd(isp, bus, atp); 2186 2187 /* 2188 * We're telling CAM we're done with this CTIO transaction. 2189 * 2190 * 24XX cards never need an ATIO put back. 2191 * 2192 * Other cards need one put back only on error. 2193 * In the latter case, a timeout will re-fire 2194 * and try again in case we didn't have 2195 * queue resources to do so at first. In any case, 2196 * once the putback is done we do the completion 2197 * call. 2198 */ 2199 if (ok || IS_24XX(isp)) { 2200 isp_complete_ctio(ccb); 2201 } else { 2202 isp_target_putback_atio(ccb); 2203 } 2204 } 2205 2206 static int 2207 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp) 2208 { 2209 2210 if (isp->isp_state != ISP_RUNSTATE) { 2211 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL); 2212 return (0); 2213 } 2214 2215 /* 2216 * This case is for a Task Management Function, which shows up as an ATIO7 entry. 2217 */ 2218 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) { 2219 ct7_entry_t local, *cto = &local; 2220 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved; 2221 fcportdb_t *lp; 2222 uint32_t sid; 2223 uint16_t nphdl; 2224 2225 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2]; 2226 if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) { 2227 nphdl = lp->handle; 2228 } else { 2229 nphdl = NIL_HANDLE; 2230 } 2231 ISP_MEMZERO(&local, sizeof (local)); 2232 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7; 2233 cto->ct_header.rqs_entry_count = 1; 2234 cto->ct_nphdl = nphdl; 2235 cto->ct_rxid = aep->at_rxid; 2236 cto->ct_vpidx = mp->nt_channel; 2237 cto->ct_iid_lo = sid; 2238 cto->ct_iid_hi = sid >> 16; 2239 cto->ct_oxid = aep->at_hdr.ox_id; 2240 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1; 2241 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT; 2242 if (rsp != 0) { 2243 cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8); 2244 cto->rsp.m1.ct_resplen = 4; 2245 ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp)); 2246 cto->rsp.m1.ct_resp[0] = rsp & 0xff; 2247 cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff; 2248 cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff; 2249 cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff; 2250 } 2251 return (isp_target_put_entry(isp, &local)); 2252 } 2253 2254 /* 2255 * This case is for a responding to an ABTS frame 2256 */ 2257 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) { 2258 2259 /* 2260 * Overload nt_need_ack here to mark whether we've terminated the associated command. 2261 */ 2262 if (mp->nt_need_ack) { 2263 uint8_t storage[QENTRY_LEN]; 2264 ct7_entry_t *cto = (ct7_entry_t *) storage; 2265 abts_t *abts = (abts_t *)mp->nt_lreserved; 2266 2267 ISP_MEMZERO(cto, sizeof (ct7_entry_t)); 2268 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task); 2269 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7; 2270 cto->ct_header.rqs_entry_count = 1; 2271 cto->ct_nphdl = mp->nt_nphdl; 2272 cto->ct_rxid = abts->abts_rxid_task; 2273 cto->ct_iid_lo = mp->nt_sid; 2274 cto->ct_iid_hi = mp->nt_sid >> 16; 2275 cto->ct_oxid = abts->abts_ox_id; 2276 cto->ct_vpidx = mp->nt_channel; 2277 cto->ct_flags = CT7_NOACK|CT7_TERMINATE; 2278 if (isp_target_put_entry(isp, cto)) { 2279 return (ENOMEM); 2280 } 2281 mp->nt_need_ack = 0; 2282 } 2283 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) { 2284 return (ENOMEM); 2285 } else { 2286 return (0); 2287 } 2288 } 2289 2290 /* 2291 * Handle logout cases here 2292 */ 2293 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) { 2294 isp_del_all_wwn_entries(isp, mp->nt_channel); 2295 } 2296 2297 if (mp->nt_ncode == NT_LOGOUT) { 2298 if (!IS_2100(isp) && IS_FC(isp)) { 2299 isp_del_wwn_entries(isp, mp); 2300 } 2301 } 2302 2303 /* 2304 * General purpose acknowledgement 2305 */ 2306 if (mp->nt_need_ack) { 2307 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL); 2308 /* 2309 * Don't need to use the guaranteed send because the caller can retry 2310 */ 2311 return (isp_notify_ack(isp, mp->nt_lreserved)); 2312 } 2313 return (0); 2314 } 2315 2316 /* 2317 * Handle task management functions. 2318 * 2319 * We show up here with a notify structure filled out. 2320 * 2321 * The nt_lreserved tag points to the original queue entry 2322 */ 2323 static void 2324 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify) 2325 { 2326 tstate_t *tptr; 2327 fcportdb_t *lp; 2328 struct ccb_immediate_notify *inot; 2329 inot_private_data_t *ntp = NULL; 2330 atio_private_data_t *atp; 2331 lun_id_t lun; 2332 2333 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode, 2334 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun); 2335 if (notify->nt_lun == LUN_ANY) { 2336 if (notify->nt_tagval == TAG_ANY) { 2337 lun = CAM_LUN_WILDCARD; 2338 } else { 2339 atp = isp_find_atpd(isp, notify->nt_channel, 2340 notify->nt_tagval & 0xffffffff); 2341 lun = atp ? atp->lun : CAM_LUN_WILDCARD; 2342 } 2343 } else { 2344 lun = notify->nt_lun; 2345 } 2346 tptr = get_lun_statep(isp, notify->nt_channel, lun); 2347 if (tptr == NULL) { 2348 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD); 2349 if (tptr == NULL) { 2350 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun); 2351 goto bad; 2352 } 2353 } 2354 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots); 2355 if (inot == NULL) { 2356 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun); 2357 goto bad; 2358 } 2359 2360 if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 && 2361 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) { 2362 inot->initiator_id = CAM_TARGET_WILDCARD; 2363 } else { 2364 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp); 2365 } 2366 inot->seq_id = notify->nt_tagval; 2367 inot->tag_id = notify->nt_tagval >> 32; 2368 2369 switch (notify->nt_ncode) { 2370 case NT_ABORT_TASK: 2371 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id); 2372 inot->arg = MSG_ABORT_TASK; 2373 break; 2374 case NT_ABORT_TASK_SET: 2375 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY); 2376 inot->arg = MSG_ABORT_TASK_SET; 2377 break; 2378 case NT_CLEAR_ACA: 2379 inot->arg = MSG_CLEAR_ACA; 2380 break; 2381 case NT_CLEAR_TASK_SET: 2382 inot->arg = MSG_CLEAR_TASK_SET; 2383 break; 2384 case NT_LUN_RESET: 2385 inot->arg = MSG_LOGICAL_UNIT_RESET; 2386 break; 2387 case NT_TARGET_RESET: 2388 inot->arg = MSG_TARGET_RESET; 2389 break; 2390 case NT_QUERY_TASK_SET: 2391 inot->arg = MSG_QUERY_TASK_SET; 2392 break; 2393 case NT_QUERY_ASYNC_EVENT: 2394 inot->arg = MSG_QUERY_ASYNC_EVENT; 2395 break; 2396 default: 2397 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun %#jx", __func__, notify->nt_ncode, notify->nt_channel, (uintmax_t)lun); 2398 goto bad; 2399 } 2400 2401 ntp = isp_get_ntpd(isp, notify->nt_channel); 2402 if (ntp == NULL) { 2403 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__); 2404 goto bad; 2405 } 2406 ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t)); 2407 if (notify->nt_lreserved) { 2408 ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN); 2409 ntp->nt.nt_lreserved = &ntp->data; 2410 } 2411 ntp->seq_id = notify->nt_tagval; 2412 ntp->tag_id = notify->nt_tagval >> 32; 2413 2414 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle); 2415 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n"); 2416 inot->ccb_h.status = CAM_MESSAGE_RECV; 2417 xpt_done((union ccb *)inot); 2418 return; 2419 bad: 2420 if (notify->nt_need_ack) { 2421 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) { 2422 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) { 2423 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK"); 2424 } 2425 } else { 2426 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved); 2427 } 2428 } 2429 } 2430 2431 static void 2432 isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id) 2433 { 2434 atio_private_data_t *atp, *atpool; 2435 inot_private_data_t *ntp, *tmp; 2436 uint32_t this_tag_id; 2437 2438 /* 2439 * First, clean any commands pending restart 2440 */ 2441 STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) { 2442 if (IS_24XX(isp)) 2443 this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid; 2444 else 2445 this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid; 2446 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) { 2447 isp_endcmd(isp, ntp->data, NIL_HANDLE, chan, 2448 ECMD_TERMINATE, 0); 2449 isp_put_ntpd(isp, chan, ntp); 2450 STAILQ_REMOVE(&tptr->restart_queue, ntp, 2451 inot_private_data, next); 2452 } 2453 } 2454 2455 /* 2456 * Now mark other ones dead as well. 2457 */ 2458 ISP_GET_PC(isp, chan, atpool, atpool); 2459 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) { 2460 if (atp->lun != tptr->ts_lun) 2461 continue; 2462 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) 2463 atp->dead = 1; 2464 } 2465 } 2466 #endif 2467 2468 static void 2469 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg) 2470 { 2471 struct cam_sim *sim; 2472 int bus, tgt; 2473 ispsoftc_t *isp; 2474 2475 sim = (struct cam_sim *)cbarg; 2476 isp = (ispsoftc_t *) cam_sim_softc(sim); 2477 bus = cam_sim_bus(sim); 2478 tgt = xpt_path_target_id(path); 2479 2480 switch (code) { 2481 case AC_LOST_DEVICE: 2482 if (IS_SCSI(isp)) { 2483 uint16_t oflags, nflags; 2484 sdparam *sdp = SDPARAM(isp, bus); 2485 2486 if (tgt >= 0) { 2487 nflags = sdp->isp_devparam[tgt].nvrm_flags; 2488 nflags &= DPARM_SAFE_DFLT; 2489 if (isp->isp_loaded_fw) { 2490 nflags |= DPARM_NARROW | DPARM_ASYNC; 2491 } 2492 oflags = sdp->isp_devparam[tgt].goal_flags; 2493 sdp->isp_devparam[tgt].goal_flags = nflags; 2494 sdp->isp_devparam[tgt].dev_update = 1; 2495 sdp->update = 1; 2496 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus); 2497 sdp->isp_devparam[tgt].goal_flags = oflags; 2498 } 2499 } 2500 break; 2501 default: 2502 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code); 2503 break; 2504 } 2505 } 2506 2507 static void 2508 isp_poll(struct cam_sim *sim) 2509 { 2510 ispsoftc_t *isp = cam_sim_softc(sim); 2511 2512 ISP_RUN_ISR(isp); 2513 } 2514 2515 2516 static void 2517 isp_watchdog(void *arg) 2518 { 2519 struct ccb_scsiio *xs = arg; 2520 ispsoftc_t *isp; 2521 uint32_t ohandle = ISP_HANDLE_FREE, handle; 2522 2523 isp = XS_ISP(xs); 2524 2525 handle = isp_find_handle(isp, xs); 2526 2527 /* 2528 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere. 2529 */ 2530 if (handle != ISP_HANDLE_FREE) { 2531 ISP_RUN_ISR(isp); 2532 ohandle = handle; 2533 handle = isp_find_handle(isp, xs); 2534 } 2535 if (handle != ISP_HANDLE_FREE) { 2536 /* 2537 * Try and make sure the command is really dead before 2538 * we release the handle (and DMA resources) for reuse. 2539 * 2540 * If we are successful in aborting the command then 2541 * we're done here because we'll get the command returned 2542 * back separately. 2543 */ 2544 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) { 2545 return; 2546 } 2547 2548 /* 2549 * Note that after calling the above, the command may in 2550 * fact have been completed. 2551 */ 2552 xs = isp_find_xs(isp, handle); 2553 2554 /* 2555 * If the command no longer exists, then we won't 2556 * be able to find the xs again with this handle. 2557 */ 2558 if (xs == NULL) { 2559 return; 2560 } 2561 2562 /* 2563 * After this point, the command is really dead. 2564 */ 2565 if (XS_XFRLEN(xs)) { 2566 ISP_DMAFREE(isp, xs, handle); 2567 } 2568 isp_destroy_handle(isp, handle); 2569 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle); 2570 xs->ccb_h.status &= ~CAM_STATUS_MASK; 2571 xs->ccb_h.status |= CAM_CMD_TIMEOUT; 2572 isp_done(xs); 2573 } else { 2574 if (ohandle != ISP_HANDLE_FREE) { 2575 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle); 2576 } else { 2577 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__); 2578 } 2579 } 2580 } 2581 2582 static void 2583 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt) 2584 { 2585 union ccb *ccb; 2586 struct isp_fc *fc = ISP_FC_PC(isp, chan); 2587 2588 /* 2589 * Allocate a CCB, create a wildcard path for this target and schedule a rescan. 2590 */ 2591 ccb = xpt_alloc_ccb_nowait(); 2592 if (ccb == NULL) { 2593 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan); 2594 return; 2595 } 2596 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim), 2597 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 2598 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan"); 2599 xpt_free_ccb(ccb); 2600 return; 2601 } 2602 xpt_rescan(ccb); 2603 } 2604 2605 static void 2606 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt) 2607 { 2608 struct cam_path *tp; 2609 struct isp_fc *fc = ISP_FC_PC(isp, chan); 2610 2611 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) { 2612 xpt_async(AC_LOST_DEVICE, tp, NULL); 2613 xpt_free_path(tp); 2614 } 2615 } 2616 2617 /* 2618 * Gone Device Timer Function- when we have decided that a device has gone 2619 * away, we wait a specific period of time prior to telling the OS it has 2620 * gone away. 2621 * 2622 * This timer function fires once a second and then scans the port database 2623 * for devices that are marked dead but still have a virtual target assigned. 2624 * We decrement a counter for that port database entry, and when it hits zero, 2625 * we tell the OS the device has gone away. 2626 */ 2627 static void 2628 isp_gdt(void *arg) 2629 { 2630 struct isp_fc *fc = arg; 2631 taskqueue_enqueue(taskqueue_thread, &fc->gtask); 2632 } 2633 2634 static void 2635 isp_gdt_task(void *arg, int pending) 2636 { 2637 struct isp_fc *fc = arg; 2638 ispsoftc_t *isp = fc->isp; 2639 int chan = fc - isp->isp_osinfo.pc.fc; 2640 fcportdb_t *lp; 2641 struct ac_contract ac; 2642 struct ac_device_changed *adc; 2643 int dbidx, more_to_do = 0; 2644 2645 ISP_LOCK(isp); 2646 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan); 2647 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) { 2648 lp = &FCPARAM(isp, chan)->portdb[dbidx]; 2649 2650 if (lp->state != FC_PORTDB_STATE_ZOMBIE) { 2651 continue; 2652 } 2653 if (lp->gone_timer != 0) { 2654 lp->gone_timer -= 1; 2655 more_to_do++; 2656 continue; 2657 } 2658 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout"); 2659 if (lp->is_target) { 2660 lp->is_target = 0; 2661 isp_make_gone(isp, lp, chan, dbidx); 2662 } 2663 if (lp->is_initiator) { 2664 lp->is_initiator = 0; 2665 ac.contract_number = AC_CONTRACT_DEV_CHG; 2666 adc = (struct ac_device_changed *) ac.contract_data; 2667 adc->wwpn = lp->port_wwn; 2668 adc->port = lp->portid; 2669 adc->target = dbidx; 2670 adc->arrived = 0; 2671 xpt_async(AC_CONTRACT, fc->path, &ac); 2672 } 2673 lp->state = FC_PORTDB_STATE_NIL; 2674 } 2675 if (fc->ready) { 2676 if (more_to_do) { 2677 callout_reset(&fc->gdt, hz, isp_gdt, fc); 2678 } else { 2679 callout_deactivate(&fc->gdt); 2680 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime); 2681 } 2682 } 2683 ISP_UNLOCK(isp); 2684 } 2685 2686 /* 2687 * When loop goes down we remember the time and freeze CAM command queue. 2688 * During some time period we are trying to reprobe the loop. But if we 2689 * fail, we tell the OS that devices have gone away and drop the freeze. 2690 * 2691 * We don't clear the devices out of our port database because, when loop 2692 * come back up, we have to do some actual cleanup with the chip at that 2693 * point (implicit PLOGO, e.g., to get the chip's port database state right). 2694 */ 2695 static void 2696 isp_loop_changed(ispsoftc_t *isp, int chan) 2697 { 2698 fcparam *fcp = FCPARAM(isp, chan); 2699 struct isp_fc *fc = ISP_FC_PC(isp, chan); 2700 2701 if (fc->loop_down_time) 2702 return; 2703 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan); 2704 if (fcp->role & ISP_ROLE_INITIATOR) 2705 isp_freeze_loopdown(isp, chan); 2706 fc->loop_down_time = time_uptime; 2707 wakeup(fc); 2708 } 2709 2710 static void 2711 isp_loop_up(ispsoftc_t *isp, int chan) 2712 { 2713 struct isp_fc *fc = ISP_FC_PC(isp, chan); 2714 2715 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan); 2716 fc->loop_seen_once = 1; 2717 fc->loop_down_time = 0; 2718 isp_unfreeze_loopdown(isp, chan); 2719 } 2720 2721 static void 2722 isp_loop_dead(ispsoftc_t *isp, int chan) 2723 { 2724 fcparam *fcp = FCPARAM(isp, chan); 2725 struct isp_fc *fc = ISP_FC_PC(isp, chan); 2726 fcportdb_t *lp; 2727 struct ac_contract ac; 2728 struct ac_device_changed *adc; 2729 int dbidx, i; 2730 2731 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan); 2732 2733 /* 2734 * Notify to the OS all targets who we now consider have departed. 2735 */ 2736 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) { 2737 lp = &fcp->portdb[dbidx]; 2738 2739 if (lp->state == FC_PORTDB_STATE_NIL) 2740 continue; 2741 2742 /* 2743 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST! 2744 */ 2745 for (i = 0; i < isp->isp_maxcmds; i++) { 2746 struct ccb_scsiio *xs; 2747 2748 if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) { 2749 continue; 2750 } 2751 if ((xs = isp->isp_xflist[i].cmd) == NULL) { 2752 continue; 2753 } 2754 if (dbidx != XS_TGT(xs)) { 2755 continue; 2756 } 2757 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout", 2758 isp->isp_xflist[i].handle, chan, XS_TGT(xs), 2759 (uintmax_t)XS_LUN(xs)); 2760 } 2761 2762 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout"); 2763 if (lp->is_target) { 2764 lp->is_target = 0; 2765 isp_make_gone(isp, lp, chan, dbidx); 2766 } 2767 if (lp->is_initiator) { 2768 lp->is_initiator = 0; 2769 ac.contract_number = AC_CONTRACT_DEV_CHG; 2770 adc = (struct ac_device_changed *) ac.contract_data; 2771 adc->wwpn = lp->port_wwn; 2772 adc->port = lp->portid; 2773 adc->target = dbidx; 2774 adc->arrived = 0; 2775 xpt_async(AC_CONTRACT, fc->path, &ac); 2776 } 2777 } 2778 2779 isp_unfreeze_loopdown(isp, chan); 2780 fc->loop_down_time = 0; 2781 } 2782 2783 static void 2784 isp_kthread(void *arg) 2785 { 2786 struct isp_fc *fc = arg; 2787 ispsoftc_t *isp = fc->isp; 2788 int chan = fc - isp->isp_osinfo.pc.fc; 2789 int slp = 0, d; 2790 int lb, lim; 2791 2792 ISP_LOCK(isp); 2793 while (isp->isp_osinfo.is_exiting == 0) { 2794 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, 2795 "Chan %d Checking FC state", chan); 2796 lb = isp_fc_runstate(isp, chan, 250000); 2797 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, 2798 "Chan %d FC got to %s state", chan, 2799 isp_fc_loop_statename(lb)); 2800 2801 /* 2802 * Our action is different based upon whether we're supporting 2803 * Initiator mode or not. If we are, we might freeze the simq 2804 * when loop is down and set all sorts of different delays to 2805 * check again. 2806 * 2807 * If not, we simply just wait for loop to come up. 2808 */ 2809 if (lb == LOOP_READY || lb < 0) { 2810 slp = 0; 2811 } else { 2812 /* 2813 * If we've never seen loop up and we've waited longer 2814 * than quickboot time, or we've seen loop up but we've 2815 * waited longer than loop_down_limit, give up and go 2816 * to sleep until loop comes up. 2817 */ 2818 if (fc->loop_seen_once == 0) 2819 lim = isp_quickboot_time; 2820 else 2821 lim = fc->loop_down_limit; 2822 d = time_uptime - fc->loop_down_time; 2823 if (d >= lim) 2824 slp = 0; 2825 else if (d < 10) 2826 slp = 1; 2827 else if (d < 30) 2828 slp = 5; 2829 else if (d < 60) 2830 slp = 10; 2831 else if (d < 120) 2832 slp = 20; 2833 else 2834 slp = 30; 2835 } 2836 2837 if (slp == 0) { 2838 if (lb == LOOP_READY) 2839 isp_loop_up(isp, chan); 2840 else 2841 isp_loop_dead(isp, chan); 2842 } 2843 2844 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, 2845 "Chan %d sleep for %d seconds", chan, slp); 2846 msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz); 2847 } 2848 fc->num_threads -= 1; 2849 ISP_UNLOCK(isp); 2850 kthread_exit(); 2851 } 2852 2853 #ifdef ISP_TARGET_MODE 2854 static void 2855 isp_abort_atio(ispsoftc_t *isp, union ccb *ccb) 2856 { 2857 atio_private_data_t *atp; 2858 union ccb *accb = ccb->cab.abort_ccb; 2859 struct ccb_hdr *sccb; 2860 tstate_t *tptr; 2861 2862 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb)); 2863 if (tptr != NULL) { 2864 /* Search for the ATIO among queueued. */ 2865 SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) { 2866 if (sccb != &accb->ccb_h) 2867 continue; 2868 SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle); 2869 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path, 2870 "Abort FREE ATIO\n"); 2871 accb->ccb_h.status = CAM_REQ_ABORTED; 2872 xpt_done(accb); 2873 ccb->ccb_h.status = CAM_REQ_CMP; 2874 return; 2875 } 2876 } 2877 2878 /* Search for the ATIO among running. */ 2879 atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id); 2880 if (atp != NULL) { 2881 /* Send TERMINATE to firmware. */ 2882 if (!atp->dead && IS_24XX(isp)) { 2883 uint8_t storage[QENTRY_LEN]; 2884 ct7_entry_t *cto = (ct7_entry_t *) storage; 2885 2886 ISP_MEMZERO(cto, sizeof (ct7_entry_t)); 2887 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7; 2888 cto->ct_header.rqs_entry_count = 1; 2889 cto->ct_nphdl = atp->nphdl; 2890 cto->ct_rxid = atp->tag; 2891 cto->ct_iid_lo = atp->sid; 2892 cto->ct_iid_hi = atp->sid >> 16; 2893 cto->ct_oxid = atp->oxid; 2894 cto->ct_vpidx = XS_CHANNEL(accb); 2895 cto->ct_flags = CT7_NOACK|CT7_TERMINATE; 2896 isp_target_put_entry(isp, cto); 2897 } 2898 isp_put_atpd(isp, XS_CHANNEL(accb), atp); 2899 ccb->ccb_h.status = CAM_REQ_CMP; 2900 } else { 2901 ccb->ccb_h.status = CAM_UA_ABORT; 2902 } 2903 } 2904 2905 static void 2906 isp_abort_inot(ispsoftc_t *isp, union ccb *ccb) 2907 { 2908 inot_private_data_t *ntp; 2909 union ccb *accb = ccb->cab.abort_ccb; 2910 struct ccb_hdr *sccb; 2911 tstate_t *tptr; 2912 2913 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb)); 2914 if (tptr != NULL) { 2915 /* Search for the INOT among queueued. */ 2916 SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) { 2917 if (sccb != &accb->ccb_h) 2918 continue; 2919 SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle); 2920 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path, 2921 "Abort FREE INOT\n"); 2922 accb->ccb_h.status = CAM_REQ_ABORTED; 2923 xpt_done(accb); 2924 ccb->ccb_h.status = CAM_REQ_CMP; 2925 return; 2926 } 2927 } 2928 2929 /* Search for the INOT among running. */ 2930 ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id); 2931 if (ntp != NULL) { 2932 if (ntp->nt.nt_need_ack) { 2933 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, 2934 ntp->nt.nt_lreserved); 2935 } 2936 isp_put_ntpd(isp, XS_CHANNEL(accb), ntp); 2937 ccb->ccb_h.status = CAM_REQ_CMP; 2938 } else { 2939 ccb->ccb_h.status = CAM_UA_ABORT; 2940 return; 2941 } 2942 } 2943 #endif 2944 2945 static void 2946 isp_action(struct cam_sim *sim, union ccb *ccb) 2947 { 2948 int bus, tgt, error; 2949 ispsoftc_t *isp; 2950 struct ccb_trans_settings *cts; 2951 sbintime_t ts; 2952 2953 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n")); 2954 2955 isp = (ispsoftc_t *)cam_sim_softc(sim); 2956 ISP_ASSERT_LOCKED(isp); 2957 bus = cam_sim_bus(sim); 2958 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code); 2959 ISP_PCMD(ccb) = NULL; 2960 2961 switch (ccb->ccb_h.func_code) { 2962 case XPT_SCSI_IO: /* Execute the requested I/O operation */ 2963 /* 2964 * Do a couple of preliminary checks... 2965 */ 2966 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) { 2967 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) { 2968 ccb->ccb_h.status = CAM_REQ_INVALID; 2969 isp_done((struct ccb_scsiio *) ccb); 2970 break; 2971 } 2972 } 2973 ccb->csio.req_map = NULL; 2974 #ifdef DIAGNOSTIC 2975 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) { 2976 xpt_print(ccb->ccb_h.path, "invalid target\n"); 2977 ccb->ccb_h.status = CAM_PATH_INVALID; 2978 } else if (ISP_MAX_LUNS(isp) > 0 && 2979 ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) { 2980 xpt_print(ccb->ccb_h.path, "invalid lun\n"); 2981 ccb->ccb_h.status = CAM_PATH_INVALID; 2982 } 2983 if (ccb->ccb_h.status == CAM_PATH_INVALID) { 2984 xpt_done(ccb); 2985 break; 2986 } 2987 #endif 2988 ccb->csio.scsi_status = SCSI_STATUS_OK; 2989 if (isp_get_pcmd(isp, ccb)) { 2990 isp_prt(isp, ISP_LOGWARN, "out of PCMDs"); 2991 cam_freeze_devq(ccb->ccb_h.path); 2992 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0); 2993 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2994 xpt_done(ccb); 2995 break; 2996 } 2997 error = isp_start((XS_T *) ccb); 2998 switch (error) { 2999 case CMD_QUEUED: 3000 ccb->ccb_h.status |= CAM_SIM_QUEUED; 3001 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) 3002 break; 3003 /* Give firmware extra 10s to handle timeout. */ 3004 ts = SBT_1MS * ccb->ccb_h.timeout + 10 * SBT_1S; 3005 callout_reset_sbt(&PISP_PCMD(ccb)->wdog, ts, 0, 3006 isp_watchdog, ccb, 0); 3007 break; 3008 case CMD_RQLATER: 3009 isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later", 3010 XS_TGT(ccb), (uintmax_t)XS_LUN(ccb)); 3011 cam_freeze_devq(ccb->ccb_h.path); 3012 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0); 3013 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3014 isp_free_pcmd(isp, ccb); 3015 xpt_done(ccb); 3016 break; 3017 case CMD_EAGAIN: 3018 isp_free_pcmd(isp, ccb); 3019 cam_freeze_devq(ccb->ccb_h.path); 3020 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0); 3021 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3022 xpt_done(ccb); 3023 break; 3024 case CMD_COMPLETE: 3025 isp_done((struct ccb_scsiio *) ccb); 3026 break; 3027 default: 3028 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__); 3029 ccb->ccb_h.status = CAM_REQUEUE_REQ; 3030 isp_free_pcmd(isp, ccb); 3031 xpt_done(ccb); 3032 } 3033 break; 3034 3035 #ifdef ISP_TARGET_MODE 3036 case XPT_EN_LUN: /* Enable/Disable LUN as a target */ 3037 if (ccb->cel.enable) { 3038 isp_enable_lun(isp, ccb); 3039 } else { 3040 isp_disable_lun(isp, ccb); 3041 } 3042 break; 3043 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */ 3044 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */ 3045 { 3046 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun); 3047 if (tptr == NULL) { 3048 const char *str; 3049 3050 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) 3051 str = "XPT_IMMEDIATE_NOTIFY"; 3052 else 3053 str = "XPT_ACCEPT_TARGET_IO"; 3054 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, 3055 "%s: no state pointer found for %s\n", 3056 __func__, str); 3057 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 3058 xpt_done(ccb); 3059 break; 3060 } 3061 ccb->ccb_h.spriv_field0 = 0; 3062 ccb->ccb_h.spriv_ptr1 = isp; 3063 3064 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) { 3065 ccb->atio.tag_id = 0; 3066 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle); 3067 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, 3068 "Put FREE ATIO\n"); 3069 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) { 3070 ccb->cin1.seq_id = ccb->cin1.tag_id = 0; 3071 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle); 3072 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, 3073 "Put FREE INOT\n"); 3074 } 3075 ccb->ccb_h.status = CAM_REQ_INPROG; 3076 break; 3077 } 3078 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */ 3079 { 3080 inot_private_data_t *ntp; 3081 3082 /* 3083 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb 3084 * XXX: matches that for the immediate notify, we have to *search* for the notify structure 3085 */ 3086 /* 3087 * All the relevant path information is in the associated immediate notify 3088 */ 3089 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] NOTIFY ACKNOWLEDGE for 0x%x seen\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id); 3090 ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id); 3091 if (ntp == NULL) { 3092 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] XPT_NOTIFY_ACKNOWLEDGE of 0x%x cannot find ntp private data\n", __func__, 3093 ccb->cna2.tag_id, ccb->cna2.seq_id); 3094 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 3095 xpt_done(ccb); 3096 break; 3097 } 3098 if (isp_handle_platform_target_notify_ack(isp, &ntp->nt, 3099 (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) { 3100 cam_freeze_devq(ccb->ccb_h.path); 3101 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0); 3102 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 3103 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 3104 break; 3105 } 3106 isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp); 3107 ccb->ccb_h.status = CAM_REQ_CMP; 3108 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] calling xpt_done for tag 0x%x\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id); 3109 xpt_done(ccb); 3110 break; 3111 } 3112 case XPT_CONT_TARGET_IO: 3113 isp_target_start_ctio(isp, ccb, FROM_CAM); 3114 break; 3115 #endif 3116 case XPT_RESET_DEV: /* BDR the specified SCSI device */ 3117 tgt = ccb->ccb_h.target_id; 3118 tgt |= (bus << 16); 3119 3120 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt); 3121 if (error) { 3122 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 3123 } else { 3124 /* 3125 * If we have a FC device, reset the Command 3126 * Reference Number, because the target will expect 3127 * that we re-start the CRN at 1 after a reset. 3128 */ 3129 if (IS_FC(isp)) 3130 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1); 3131 3132 ccb->ccb_h.status = CAM_REQ_CMP; 3133 } 3134 xpt_done(ccb); 3135 break; 3136 case XPT_ABORT: /* Abort the specified CCB */ 3137 { 3138 union ccb *accb = ccb->cab.abort_ccb; 3139 switch (accb->ccb_h.func_code) { 3140 #ifdef ISP_TARGET_MODE 3141 case XPT_ACCEPT_TARGET_IO: 3142 isp_abort_atio(isp, ccb); 3143 break; 3144 case XPT_IMMEDIATE_NOTIFY: 3145 isp_abort_inot(isp, ccb); 3146 break; 3147 #endif 3148 case XPT_SCSI_IO: 3149 error = isp_control(isp, ISPCTL_ABORT_CMD, accb); 3150 if (error) { 3151 ccb->ccb_h.status = CAM_UA_ABORT; 3152 } else { 3153 ccb->ccb_h.status = CAM_REQ_CMP; 3154 } 3155 break; 3156 default: 3157 ccb->ccb_h.status = CAM_REQ_INVALID; 3158 break; 3159 } 3160 /* 3161 * This is not a queued CCB, so the caller expects it to be 3162 * complete when control is returned. 3163 */ 3164 break; 3165 } 3166 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS) 3167 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */ 3168 cts = &ccb->cts; 3169 if (!IS_CURRENT_SETTINGS(cts)) { 3170 ccb->ccb_h.status = CAM_REQ_INVALID; 3171 xpt_done(ccb); 3172 break; 3173 } 3174 tgt = cts->ccb_h.target_id; 3175 if (IS_SCSI(isp)) { 3176 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi; 3177 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi; 3178 sdparam *sdp = SDPARAM(isp, bus); 3179 uint16_t *dptr; 3180 3181 if (spi->valid == 0 && scsi->valid == 0) { 3182 ccb->ccb_h.status = CAM_REQ_CMP; 3183 xpt_done(ccb); 3184 break; 3185 } 3186 3187 /* 3188 * We always update (internally) from goal_flags 3189 * so any request to change settings just gets 3190 * vectored to that location. 3191 */ 3192 dptr = &sdp->isp_devparam[tgt].goal_flags; 3193 3194 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) { 3195 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0) 3196 *dptr |= DPARM_DISC; 3197 else 3198 *dptr &= ~DPARM_DISC; 3199 } 3200 3201 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) { 3202 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) 3203 *dptr |= DPARM_TQING; 3204 else 3205 *dptr &= ~DPARM_TQING; 3206 } 3207 3208 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) { 3209 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT) 3210 *dptr |= DPARM_WIDE; 3211 else 3212 *dptr &= ~DPARM_WIDE; 3213 } 3214 3215 /* 3216 * XXX: FIX ME 3217 */ 3218 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) { 3219 *dptr |= DPARM_SYNC; 3220 /* 3221 * XXX: CHECK FOR LEGALITY 3222 */ 3223 sdp->isp_devparam[tgt].goal_period = spi->sync_period; 3224 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset; 3225 } else { 3226 *dptr &= ~DPARM_SYNC; 3227 } 3228 isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%jx) to flags %x off %x per %x", bus, tgt, (uintmax_t)cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags, 3229 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period); 3230 sdp->isp_devparam[tgt].dev_update = 1; 3231 sdp->update = 1; 3232 } 3233 ccb->ccb_h.status = CAM_REQ_CMP; 3234 xpt_done(ccb); 3235 break; 3236 case XPT_GET_TRAN_SETTINGS: 3237 cts = &ccb->cts; 3238 tgt = cts->ccb_h.target_id; 3239 if (IS_FC(isp)) { 3240 fcparam *fcp = FCPARAM(isp, bus); 3241 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi; 3242 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc; 3243 3244 cts->protocol = PROTO_SCSI; 3245 cts->protocol_version = SCSI_REV_2; 3246 cts->transport = XPORT_FC; 3247 cts->transport_version = 0; 3248 3249 scsi->valid = CTS_SCSI_VALID_TQ; 3250 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 3251 fc->valid = CTS_FC_VALID_SPEED; 3252 fc->bitrate = 100000; 3253 fc->bitrate *= fcp->isp_gbspeed; 3254 if (tgt < MAX_FC_TARG) { 3255 fcportdb_t *lp = &fcp->portdb[tgt]; 3256 fc->wwnn = lp->node_wwn; 3257 fc->wwpn = lp->port_wwn; 3258 fc->port = lp->portid; 3259 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT; 3260 } 3261 } else { 3262 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi; 3263 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi; 3264 sdparam *sdp = SDPARAM(isp, bus); 3265 uint16_t dval, pval, oval; 3266 3267 if (IS_CURRENT_SETTINGS(cts)) { 3268 sdp->isp_devparam[tgt].dev_refresh = 1; 3269 sdp->update = 1; 3270 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus); 3271 dval = sdp->isp_devparam[tgt].actv_flags; 3272 oval = sdp->isp_devparam[tgt].actv_offset; 3273 pval = sdp->isp_devparam[tgt].actv_period; 3274 } else { 3275 dval = sdp->isp_devparam[tgt].nvrm_flags; 3276 oval = sdp->isp_devparam[tgt].nvrm_offset; 3277 pval = sdp->isp_devparam[tgt].nvrm_period; 3278 } 3279 3280 cts->protocol = PROTO_SCSI; 3281 cts->protocol_version = SCSI_REV_2; 3282 cts->transport = XPORT_SPI; 3283 cts->transport_version = 2; 3284 3285 spi->valid = 0; 3286 scsi->valid = 0; 3287 spi->flags = 0; 3288 scsi->flags = 0; 3289 if (dval & DPARM_DISC) { 3290 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 3291 } 3292 if ((dval & DPARM_SYNC) && oval && pval) { 3293 spi->sync_offset = oval; 3294 spi->sync_period = pval; 3295 } else { 3296 spi->sync_offset = 0; 3297 spi->sync_period = 0; 3298 } 3299 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET; 3300 spi->valid |= CTS_SPI_VALID_SYNC_RATE; 3301 spi->valid |= CTS_SPI_VALID_BUS_WIDTH; 3302 if (dval & DPARM_WIDE) { 3303 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT; 3304 } else { 3305 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 3306 } 3307 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) { 3308 scsi->valid = CTS_SCSI_VALID_TQ; 3309 if (dval & DPARM_TQING) { 3310 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 3311 } 3312 spi->valid |= CTS_SPI_VALID_DISC; 3313 } 3314 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM", 3315 bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval); 3316 } 3317 ccb->ccb_h.status = CAM_REQ_CMP; 3318 xpt_done(ccb); 3319 break; 3320 3321 case XPT_CALC_GEOMETRY: 3322 cam_calc_geometry(&ccb->ccg, 1); 3323 xpt_done(ccb); 3324 break; 3325 3326 case XPT_RESET_BUS: /* Reset the specified bus */ 3327 error = isp_control(isp, ISPCTL_RESET_BUS, bus); 3328 if (error) { 3329 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 3330 xpt_done(ccb); 3331 break; 3332 } 3333 if (bootverbose) { 3334 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus); 3335 } 3336 if (IS_FC(isp)) { 3337 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0); 3338 } else { 3339 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0); 3340 } 3341 ccb->ccb_h.status = CAM_REQ_CMP; 3342 xpt_done(ccb); 3343 break; 3344 3345 case XPT_TERM_IO: /* Terminate the I/O process */ 3346 ccb->ccb_h.status = CAM_REQ_INVALID; 3347 xpt_done(ccb); 3348 break; 3349 3350 case XPT_SET_SIM_KNOB: /* Set SIM knobs */ 3351 { 3352 struct ccb_sim_knob *kp = &ccb->knob; 3353 fcparam *fcp; 3354 3355 if (!IS_FC(isp)) { 3356 ccb->ccb_h.status = CAM_REQ_INVALID; 3357 xpt_done(ccb); 3358 break; 3359 } 3360 3361 fcp = FCPARAM(isp, bus); 3362 3363 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) { 3364 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn; 3365 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn; 3366 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn); 3367 } 3368 ccb->ccb_h.status = CAM_REQ_CMP; 3369 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) { 3370 int rchange = 0; 3371 int newrole = 0; 3372 3373 switch (kp->xport_specific.fc.role) { 3374 case KNOB_ROLE_NONE: 3375 if (fcp->role != ISP_ROLE_NONE) { 3376 rchange = 1; 3377 newrole = ISP_ROLE_NONE; 3378 } 3379 break; 3380 case KNOB_ROLE_TARGET: 3381 if (fcp->role != ISP_ROLE_TARGET) { 3382 rchange = 1; 3383 newrole = ISP_ROLE_TARGET; 3384 } 3385 break; 3386 case KNOB_ROLE_INITIATOR: 3387 if (fcp->role != ISP_ROLE_INITIATOR) { 3388 rchange = 1; 3389 newrole = ISP_ROLE_INITIATOR; 3390 } 3391 break; 3392 case KNOB_ROLE_BOTH: 3393 if (fcp->role != ISP_ROLE_BOTH) { 3394 rchange = 1; 3395 newrole = ISP_ROLE_BOTH; 3396 } 3397 break; 3398 } 3399 if (rchange) { 3400 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole); 3401 if (isp_control(isp, ISPCTL_CHANGE_ROLE, 3402 bus, newrole) != 0) { 3403 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 3404 xpt_done(ccb); 3405 break; 3406 } 3407 } 3408 } 3409 xpt_done(ccb); 3410 break; 3411 } 3412 case XPT_GET_SIM_KNOB_OLD: /* Get SIM knobs -- compat value */ 3413 case XPT_GET_SIM_KNOB: /* Get SIM knobs */ 3414 { 3415 struct ccb_sim_knob *kp = &ccb->knob; 3416 3417 if (IS_FC(isp)) { 3418 fcparam *fcp; 3419 3420 fcp = FCPARAM(isp, bus); 3421 3422 kp->xport_specific.fc.wwnn = fcp->isp_wwnn; 3423 kp->xport_specific.fc.wwpn = fcp->isp_wwpn; 3424 switch (fcp->role) { 3425 case ISP_ROLE_NONE: 3426 kp->xport_specific.fc.role = KNOB_ROLE_NONE; 3427 break; 3428 case ISP_ROLE_TARGET: 3429 kp->xport_specific.fc.role = KNOB_ROLE_TARGET; 3430 break; 3431 case ISP_ROLE_INITIATOR: 3432 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR; 3433 break; 3434 case ISP_ROLE_BOTH: 3435 kp->xport_specific.fc.role = KNOB_ROLE_BOTH; 3436 break; 3437 } 3438 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE; 3439 ccb->ccb_h.status = CAM_REQ_CMP; 3440 } else { 3441 ccb->ccb_h.status = CAM_REQ_INVALID; 3442 } 3443 xpt_done(ccb); 3444 break; 3445 } 3446 case XPT_PATH_INQ: /* Path routing inquiry */ 3447 { 3448 struct ccb_pathinq *cpi = &ccb->cpi; 3449 3450 cpi->version_num = 1; 3451 #ifdef ISP_TARGET_MODE 3452 if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp)) 3453 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO; 3454 else 3455 #endif 3456 cpi->target_sprt = 0; 3457 cpi->hba_eng_cnt = 0; 3458 cpi->max_target = ISP_MAX_TARGETS(isp) - 1; 3459 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ? 3460 255 : ISP_MAX_LUNS(isp) - 1; 3461 cpi->bus_id = cam_sim_bus(sim); 3462 if (sizeof (bus_size_t) > 4) 3463 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE; 3464 else 3465 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE; 3466 3467 if (IS_FC(isp)) { 3468 fcparam *fcp = FCPARAM(isp, bus); 3469 3470 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED; 3471 cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN; 3472 3473 /* 3474 * Because our loop ID can shift from time to time, 3475 * make our initiator ID out of range of our bus. 3476 */ 3477 cpi->initiator_id = cpi->max_target + 1; 3478 3479 /* 3480 * Set base transfer capabilities for Fibre Channel, for this HBA. 3481 */ 3482 if (IS_25XX(isp)) { 3483 cpi->base_transfer_speed = 8000000; 3484 } else if (IS_24XX(isp)) { 3485 cpi->base_transfer_speed = 4000000; 3486 } else if (IS_23XX(isp)) { 3487 cpi->base_transfer_speed = 2000000; 3488 } else { 3489 cpi->base_transfer_speed = 1000000; 3490 } 3491 cpi->hba_inquiry = PI_TAG_ABLE; 3492 cpi->transport = XPORT_FC; 3493 cpi->transport_version = 0; 3494 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn; 3495 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn; 3496 cpi->xport_specific.fc.port = fcp->isp_portid; 3497 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000; 3498 } else { 3499 sdparam *sdp = SDPARAM(isp, bus); 3500 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 3501 cpi->hba_misc = PIM_UNMAPPED; 3502 cpi->initiator_id = sdp->isp_initiator_id; 3503 cpi->base_transfer_speed = 3300; 3504 cpi->transport = XPORT_SPI; 3505 cpi->transport_version = 2; 3506 } 3507 cpi->protocol = PROTO_SCSI; 3508 cpi->protocol_version = SCSI_REV_2; 3509 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 3510 strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN); 3511 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 3512 cpi->unit_number = cam_sim_unit(sim); 3513 cpi->ccb_h.status = CAM_REQ_CMP; 3514 xpt_done(ccb); 3515 break; 3516 } 3517 default: 3518 ccb->ccb_h.status = CAM_REQ_INVALID; 3519 xpt_done(ccb); 3520 break; 3521 } 3522 } 3523 3524 void 3525 isp_done(XS_T *sccb) 3526 { 3527 ispsoftc_t *isp = XS_ISP(sccb); 3528 uint32_t status; 3529 3530 if (XS_NOERR(sccb)) 3531 XS_SETERR(sccb, CAM_REQ_CMP); 3532 3533 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) { 3534 sccb->ccb_h.status &= ~CAM_STATUS_MASK; 3535 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) { 3536 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL; 3537 } else { 3538 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR; 3539 } 3540 } 3541 3542 sccb->ccb_h.status &= ~CAM_SIM_QUEUED; 3543 status = sccb->ccb_h.status & CAM_STATUS_MASK; 3544 if (status != CAM_REQ_CMP && 3545 (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 3546 sccb->ccb_h.status |= CAM_DEV_QFRZN; 3547 xpt_freeze_devq(sccb->ccb_h.path, 1); 3548 } 3549 3550 if (ISP_PCMD(sccb)) { 3551 if (callout_active(&PISP_PCMD(sccb)->wdog)) 3552 callout_stop(&PISP_PCMD(sccb)->wdog); 3553 isp_free_pcmd(isp, (union ccb *) sccb); 3554 } 3555 xpt_done((union ccb *) sccb); 3556 } 3557 3558 void 3559 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...) 3560 { 3561 int bus; 3562 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s"; 3563 char buf[64]; 3564 char *msg = NULL; 3565 target_id_t tgt; 3566 fcportdb_t *lp; 3567 struct isp_fc *fc; 3568 struct cam_path *tmppath; 3569 struct ac_contract ac; 3570 struct ac_device_changed *adc; 3571 va_list ap; 3572 3573 switch (cmd) { 3574 case ISPASYNC_NEW_TGT_PARAMS: 3575 { 3576 struct ccb_trans_settings_scsi *scsi; 3577 struct ccb_trans_settings_spi *spi; 3578 int flags, tgt; 3579 sdparam *sdp; 3580 struct ccb_trans_settings cts; 3581 3582 memset(&cts, 0, sizeof (struct ccb_trans_settings)); 3583 3584 va_start(ap, cmd); 3585 bus = va_arg(ap, int); 3586 tgt = va_arg(ap, int); 3587 va_end(ap); 3588 sdp = SDPARAM(isp, bus); 3589 3590 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 3591 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus); 3592 break; 3593 } 3594 flags = sdp->isp_devparam[tgt].actv_flags; 3595 cts.type = CTS_TYPE_CURRENT_SETTINGS; 3596 cts.protocol = PROTO_SCSI; 3597 cts.transport = XPORT_SPI; 3598 3599 scsi = &cts.proto_specific.scsi; 3600 spi = &cts.xport_specific.spi; 3601 3602 if (flags & DPARM_TQING) { 3603 scsi->valid |= CTS_SCSI_VALID_TQ; 3604 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 3605 } 3606 3607 if (flags & DPARM_DISC) { 3608 spi->valid |= CTS_SPI_VALID_DISC; 3609 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 3610 } 3611 spi->flags |= CTS_SPI_VALID_BUS_WIDTH; 3612 if (flags & DPARM_WIDE) { 3613 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT; 3614 } else { 3615 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 3616 } 3617 if (flags & DPARM_SYNC) { 3618 spi->valid |= CTS_SPI_VALID_SYNC_RATE; 3619 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET; 3620 spi->sync_period = sdp->isp_devparam[tgt].actv_period; 3621 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset; 3622 } 3623 isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags); 3624 xpt_setup_ccb(&cts.ccb_h, tmppath, 1); 3625 xpt_async(AC_TRANSFER_NEG, tmppath, &cts); 3626 xpt_free_path(tmppath); 3627 break; 3628 } 3629 case ISPASYNC_BUS_RESET: 3630 { 3631 va_start(ap, cmd); 3632 bus = va_arg(ap, int); 3633 va_end(ap); 3634 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus); 3635 if (IS_FC(isp)) { 3636 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL); 3637 } else { 3638 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL); 3639 } 3640 break; 3641 } 3642 case ISPASYNC_LIP: 3643 if (msg == NULL) 3644 msg = "LIP Received"; 3645 /* FALLTHROUGH */ 3646 case ISPASYNC_LOOP_RESET: 3647 if (msg == NULL) 3648 msg = "LOOP Reset"; 3649 /* FALLTHROUGH */ 3650 case ISPASYNC_LOOP_DOWN: 3651 if (msg == NULL) 3652 msg = "LOOP Down"; 3653 va_start(ap, cmd); 3654 bus = va_arg(ap, int); 3655 va_end(ap); 3656 isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0); 3657 isp_loop_changed(isp, bus); 3658 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg); 3659 break; 3660 case ISPASYNC_LOOP_UP: 3661 va_start(ap, cmd); 3662 bus = va_arg(ap, int); 3663 va_end(ap); 3664 isp_loop_changed(isp, bus); 3665 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus); 3666 break; 3667 case ISPASYNC_DEV_ARRIVED: 3668 va_start(ap, cmd); 3669 bus = va_arg(ap, int); 3670 lp = va_arg(ap, fcportdb_t *); 3671 va_end(ap); 3672 fc = ISP_FC_PC(isp, bus); 3673 tgt = FC_PORTDB_TGT(isp, bus, lp); 3674 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3); 3675 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived"); 3676 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && 3677 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) { 3678 lp->is_target = 1; 3679 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1); 3680 isp_make_here(isp, lp, bus, tgt); 3681 } 3682 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) && 3683 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) { 3684 lp->is_initiator = 1; 3685 ac.contract_number = AC_CONTRACT_DEV_CHG; 3686 adc = (struct ac_device_changed *) ac.contract_data; 3687 adc->wwpn = lp->port_wwn; 3688 adc->port = lp->portid; 3689 adc->target = tgt; 3690 adc->arrived = 1; 3691 xpt_async(AC_CONTRACT, fc->path, &ac); 3692 } 3693 break; 3694 case ISPASYNC_DEV_CHANGED: 3695 va_start(ap, cmd); 3696 bus = va_arg(ap, int); 3697 lp = va_arg(ap, fcportdb_t *); 3698 va_end(ap); 3699 fc = ISP_FC_PC(isp, bus); 3700 tgt = FC_PORTDB_TGT(isp, bus, lp); 3701 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3); 3702 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed"); 3703 changed: 3704 if (lp->is_target != 3705 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && 3706 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) { 3707 lp->is_target = !lp->is_target; 3708 if (lp->is_target) { 3709 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1); 3710 isp_make_here(isp, lp, bus, tgt); 3711 } else { 3712 isp_make_gone(isp, lp, bus, tgt); 3713 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1); 3714 } 3715 } 3716 if (lp->is_initiator != 3717 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) && 3718 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) { 3719 lp->is_initiator = !lp->is_initiator; 3720 ac.contract_number = AC_CONTRACT_DEV_CHG; 3721 adc = (struct ac_device_changed *) ac.contract_data; 3722 adc->wwpn = lp->port_wwn; 3723 adc->port = lp->portid; 3724 adc->target = tgt; 3725 adc->arrived = lp->is_initiator; 3726 xpt_async(AC_CONTRACT, fc->path, &ac); 3727 } 3728 break; 3729 case ISPASYNC_DEV_STAYED: 3730 va_start(ap, cmd); 3731 bus = va_arg(ap, int); 3732 lp = va_arg(ap, fcportdb_t *); 3733 va_end(ap); 3734 fc = ISP_FC_PC(isp, bus); 3735 tgt = FC_PORTDB_TGT(isp, bus, lp); 3736 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3); 3737 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed"); 3738 goto changed; 3739 case ISPASYNC_DEV_GONE: 3740 va_start(ap, cmd); 3741 bus = va_arg(ap, int); 3742 lp = va_arg(ap, fcportdb_t *); 3743 va_end(ap); 3744 fc = ISP_FC_PC(isp, bus); 3745 tgt = FC_PORTDB_TGT(isp, bus, lp); 3746 /* 3747 * If this has a virtual target or initiator set the isp_gdt 3748 * timer running on it to delay its departure. 3749 */ 3750 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3); 3751 if (lp->is_target || lp->is_initiator) { 3752 lp->state = FC_PORTDB_STATE_ZOMBIE; 3753 lp->gone_timer = fc->gone_device_time; 3754 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie"); 3755 if (fc->ready && !callout_active(&fc->gdt)) { 3756 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime); 3757 callout_reset(&fc->gdt, hz, isp_gdt, fc); 3758 } 3759 break; 3760 } 3761 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone"); 3762 break; 3763 case ISPASYNC_CHANGE_NOTIFY: 3764 { 3765 char *msg; 3766 int evt, nphdl, nlstate, portid, reason; 3767 3768 va_start(ap, cmd); 3769 bus = va_arg(ap, int); 3770 evt = va_arg(ap, int); 3771 if (evt == ISPASYNC_CHANGE_PDB) { 3772 nphdl = va_arg(ap, int); 3773 nlstate = va_arg(ap, int); 3774 reason = va_arg(ap, int); 3775 } else if (evt == ISPASYNC_CHANGE_SNS) { 3776 portid = va_arg(ap, int); 3777 } else { 3778 nphdl = NIL_HANDLE; 3779 nlstate = reason = 0; 3780 } 3781 va_end(ap); 3782 3783 if (evt == ISPASYNC_CHANGE_PDB) { 3784 msg = "Port Database Changed"; 3785 isp_prt(isp, ISP_LOGINFO, 3786 "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)", 3787 bus, msg, nphdl, nlstate, reason); 3788 } else if (evt == ISPASYNC_CHANGE_SNS) { 3789 msg = "Name Server Database Changed"; 3790 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)", 3791 bus, msg, portid); 3792 } else { 3793 msg = "Other Change Notify"; 3794 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg); 3795 } 3796 isp_loop_changed(isp, bus); 3797 break; 3798 } 3799 #ifdef ISP_TARGET_MODE 3800 case ISPASYNC_TARGET_NOTIFY: 3801 { 3802 isp_notify_t *notify; 3803 va_start(ap, cmd); 3804 notify = va_arg(ap, isp_notify_t *); 3805 va_end(ap); 3806 switch (notify->nt_ncode) { 3807 case NT_ABORT_TASK: 3808 case NT_ABORT_TASK_SET: 3809 case NT_CLEAR_ACA: 3810 case NT_CLEAR_TASK_SET: 3811 case NT_LUN_RESET: 3812 case NT_TARGET_RESET: 3813 case NT_QUERY_TASK_SET: 3814 case NT_QUERY_ASYNC_EVENT: 3815 /* 3816 * These are task management functions. 3817 */ 3818 isp_handle_platform_target_tmf(isp, notify); 3819 break; 3820 case NT_BUS_RESET: 3821 case NT_LIP_RESET: 3822 case NT_LINK_UP: 3823 case NT_LINK_DOWN: 3824 case NT_HBA_RESET: 3825 /* 3826 * No action need be taken here. 3827 */ 3828 break; 3829 case NT_GLOBAL_LOGOUT: 3830 case NT_LOGOUT: 3831 /* 3832 * This is device arrival/departure notification 3833 */ 3834 isp_handle_platform_target_notify_ack(isp, notify, 0); 3835 break; 3836 case NT_SRR: 3837 isp_handle_platform_srr(isp, notify); 3838 break; 3839 default: 3840 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode); 3841 isp_handle_platform_target_notify_ack(isp, notify, 0); 3842 break; 3843 } 3844 break; 3845 } 3846 case ISPASYNC_TARGET_NOTIFY_ACK: 3847 { 3848 void *inot; 3849 va_start(ap, cmd); 3850 inot = va_arg(ap, void *); 3851 va_end(ap); 3852 if (isp_notify_ack(isp, inot)) { 3853 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT); 3854 if (tp) { 3855 tp->isp = isp; 3856 memcpy(tp->data, inot, sizeof (tp->data)); 3857 tp->not = tp->data; 3858 callout_init_mtx(&tp->timer, &isp->isp_lock, 0); 3859 callout_reset(&tp->timer, 5, 3860 isp_refire_notify_ack, tp); 3861 } else { 3862 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire"); 3863 } 3864 } 3865 break; 3866 } 3867 case ISPASYNC_TARGET_ACTION: 3868 { 3869 isphdr_t *hp; 3870 3871 va_start(ap, cmd); 3872 hp = va_arg(ap, isphdr_t *); 3873 va_end(ap); 3874 switch (hp->rqs_entry_type) { 3875 case RQSTYPE_ATIO: 3876 isp_handle_platform_atio7(isp, (at7_entry_t *) hp); 3877 break; 3878 case RQSTYPE_ATIO2: 3879 isp_handle_platform_atio2(isp, (at2_entry_t *) hp); 3880 break; 3881 case RQSTYPE_CTIO7: 3882 case RQSTYPE_CTIO3: 3883 case RQSTYPE_CTIO2: 3884 case RQSTYPE_CTIO: 3885 isp_handle_platform_ctio(isp, hp); 3886 break; 3887 default: 3888 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", 3889 __func__, hp->rqs_entry_type); 3890 break; 3891 } 3892 break; 3893 } 3894 #endif 3895 case ISPASYNC_FW_CRASH: 3896 { 3897 uint16_t mbox1, mbox6; 3898 mbox1 = ISP_READ(isp, OUTMAILBOX1); 3899 if (IS_DUALBUS(isp)) { 3900 mbox6 = ISP_READ(isp, OUTMAILBOX6); 3901 } else { 3902 mbox6 = 0; 3903 } 3904 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1); 3905 #if 0 3906 mbox1 = isp->isp_osinfo.mbox_sleep_ok; 3907 isp->isp_osinfo.mbox_sleep_ok = 0; 3908 isp_reinit(isp, 1); 3909 isp->isp_osinfo.mbox_sleep_ok = mbox1; 3910 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL); 3911 #endif 3912 break; 3913 } 3914 default: 3915 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd); 3916 break; 3917 } 3918 } 3919 3920 uint64_t 3921 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn) 3922 { 3923 uint64_t seed; 3924 struct isp_fc *fc = ISP_FC_PC(isp, chan); 3925 3926 /* First try to use explicitly configured WWNs. */ 3927 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn; 3928 if (seed) 3929 return (seed); 3930 3931 /* Otherwise try to use WWNs from NVRAM. */ 3932 if (isactive) { 3933 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : 3934 FCPARAM(isp, chan)->isp_wwpn_nvram; 3935 if (seed) 3936 return (seed); 3937 } 3938 3939 /* If still no WWNs, try to steal them from the first channel. */ 3940 if (chan > 0) { 3941 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : 3942 ISP_FC_PC(isp, 0)->def_wwpn; 3943 if (seed == 0) { 3944 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : 3945 FCPARAM(isp, 0)->isp_wwpn_nvram; 3946 } 3947 } 3948 3949 /* If still nothing -- improvise. */ 3950 if (seed == 0) { 3951 seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev); 3952 if (!iswwnn) 3953 seed ^= 0x0100000000000000ULL; 3954 } 3955 3956 /* For additional channels we have to improvise even more. */ 3957 if (!iswwnn && chan > 0) { 3958 /* 3959 * We'll stick our channel number plus one first into bits 3960 * 57..59 and thence into bits 52..55 which allows for 8 bits 3961 * of channel which is enough for our maximum of 255 channels. 3962 */ 3963 seed ^= 0x0100000000000000ULL; 3964 seed ^= ((uint64_t) (chan + 1) & 0xf) << 56; 3965 seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52; 3966 } 3967 return (seed); 3968 } 3969 3970 void 3971 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...) 3972 { 3973 int loc; 3974 char lbuf[200]; 3975 va_list ap; 3976 3977 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) { 3978 return; 3979 } 3980 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev)); 3981 loc = strlen(lbuf); 3982 va_start(ap, fmt); 3983 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap); 3984 va_end(ap); 3985 printf("%s\n", lbuf); 3986 } 3987 3988 void 3989 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...) 3990 { 3991 va_list ap; 3992 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) { 3993 return; 3994 } 3995 xpt_print_path(xs->ccb_h.path); 3996 va_start(ap, fmt); 3997 vprintf(fmt, ap); 3998 va_end(ap); 3999 printf("\n"); 4000 } 4001 4002 uint64_t 4003 isp_nanotime_sub(struct timespec *b, struct timespec *a) 4004 { 4005 uint64_t elapsed; 4006 struct timespec x = *b; 4007 timespecsub(&x, a); 4008 elapsed = GET_NANOSEC(&x); 4009 if (elapsed == 0) 4010 elapsed++; 4011 return (elapsed); 4012 } 4013 4014 int 4015 isp_mbox_acquire(ispsoftc_t *isp) 4016 { 4017 if (isp->isp_osinfo.mboxbsy) { 4018 return (1); 4019 } else { 4020 isp->isp_osinfo.mboxcmd_done = 0; 4021 isp->isp_osinfo.mboxbsy = 1; 4022 return (0); 4023 } 4024 } 4025 4026 void 4027 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp) 4028 { 4029 u_int t, to; 4030 4031 to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout; 4032 if (isp->isp_osinfo.mbox_sleep_ok) { 4033 isp->isp_osinfo.mbox_sleep_ok = 0; 4034 isp->isp_osinfo.mbox_sleeping = 1; 4035 msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_lock, 4036 PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0); 4037 isp->isp_osinfo.mbox_sleep_ok = 1; 4038 isp->isp_osinfo.mbox_sleeping = 0; 4039 } else { 4040 for (t = 0; t < to; t += 100) { 4041 if (isp->isp_osinfo.mboxcmd_done) 4042 break; 4043 ISP_RUN_ISR(isp); 4044 if (isp->isp_osinfo.mboxcmd_done) 4045 break; 4046 ISP_DELAY(100); 4047 } 4048 } 4049 if (isp->isp_osinfo.mboxcmd_done == 0) { 4050 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)", 4051 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", 4052 isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno); 4053 mbp->param[0] = MBOX_TIMEOUT; 4054 isp->isp_osinfo.mboxcmd_done = 1; 4055 } 4056 } 4057 4058 void 4059 isp_mbox_notify_done(ispsoftc_t *isp) 4060 { 4061 isp->isp_osinfo.mboxcmd_done = 1; 4062 if (isp->isp_osinfo.mbox_sleeping) 4063 wakeup(&isp->isp_osinfo.mboxcmd_done); 4064 } 4065 4066 void 4067 isp_mbox_release(ispsoftc_t *isp) 4068 { 4069 isp->isp_osinfo.mboxbsy = 0; 4070 } 4071 4072 int 4073 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan) 4074 { 4075 int ret = 0; 4076 if (isp->isp_osinfo.pc.fc[chan].fcbsy) { 4077 ret = -1; 4078 } else { 4079 isp->isp_osinfo.pc.fc[chan].fcbsy = 1; 4080 } 4081 return (ret); 4082 } 4083 4084 void 4085 isp_platform_intr(void *arg) 4086 { 4087 ispsoftc_t *isp = arg; 4088 4089 ISP_LOCK(isp); 4090 ISP_RUN_ISR(isp); 4091 ISP_UNLOCK(isp); 4092 } 4093 4094 void 4095 isp_platform_intr_resp(void *arg) 4096 { 4097 ispsoftc_t *isp = arg; 4098 4099 ISP_LOCK(isp); 4100 isp_intr_respq(isp); 4101 ISP_UNLOCK(isp); 4102 4103 /* We have handshake enabled, so explicitly complete interrupt */ 4104 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT); 4105 } 4106 4107 void 4108 isp_platform_intr_atio(void *arg) 4109 { 4110 ispsoftc_t *isp = arg; 4111 4112 ISP_LOCK(isp); 4113 #ifdef ISP_TARGET_MODE 4114 isp_intr_atioq(isp); 4115 #endif 4116 ISP_UNLOCK(isp); 4117 4118 /* We have handshake enabled, so explicitly complete interrupt */ 4119 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT); 4120 } 4121 4122 void 4123 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl) 4124 { 4125 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) { 4126 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD); 4127 } else { 4128 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE); 4129 } 4130 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap); 4131 } 4132 4133 /* 4134 * Reset the command reference number for all LUNs on a specific target 4135 * (needed when a target arrives again) or for all targets on a port 4136 * (needed for events like a LIP). 4137 */ 4138 void 4139 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set) 4140 { 4141 struct isp_fc *fc = ISP_FC_PC(isp, chan); 4142 struct isp_nexus *nxp; 4143 int i; 4144 4145 if (tgt_set == 0) 4146 isp_prt(isp, ISP_LOGDEBUG0, 4147 "Chan %d resetting CRN on all targets", chan); 4148 else 4149 isp_prt(isp, ISP_LOGDEBUG0, 4150 "Chan %d resetting CRN on target %u", chan, tgt); 4151 4152 for (i = 0; i < NEXUS_HASH_WIDTH; i++) { 4153 for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) { 4154 if (tgt_set == 0 || tgt == nxp->tgt) 4155 nxp->crnseed = 0; 4156 } 4157 } 4158 } 4159 4160 int 4161 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd) 4162 { 4163 lun_id_t lun; 4164 uint32_t chan, tgt; 4165 struct isp_fc *fc; 4166 struct isp_nexus *nxp; 4167 int idx; 4168 4169 if (IS_2100(isp)) 4170 return (0); 4171 4172 chan = XS_CHANNEL(cmd); 4173 tgt = XS_TGT(cmd); 4174 lun = XS_LUN(cmd); 4175 fc = &isp->isp_osinfo.pc.fc[chan]; 4176 idx = NEXUS_HASH(tgt, lun); 4177 nxp = fc->nexus_hash[idx]; 4178 4179 while (nxp) { 4180 if (nxp->tgt == tgt && nxp->lun == lun) 4181 break; 4182 nxp = nxp->next; 4183 } 4184 if (nxp == NULL) { 4185 nxp = fc->nexus_free_list; 4186 if (nxp == NULL) { 4187 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT); 4188 if (nxp == NULL) { 4189 return (-1); 4190 } 4191 } else { 4192 fc->nexus_free_list = nxp->next; 4193 } 4194 nxp->tgt = tgt; 4195 nxp->lun = lun; 4196 nxp->next = fc->nexus_hash[idx]; 4197 fc->nexus_hash[idx] = nxp; 4198 } 4199 if (nxp->crnseed == 0) 4200 nxp->crnseed = 1; 4201 *crnp = nxp->crnseed++; 4202 return (0); 4203 } 4204 4205 /* 4206 * We enter with the lock held 4207 */ 4208 void 4209 isp_timer(void *arg) 4210 { 4211 ispsoftc_t *isp = arg; 4212 #ifdef ISP_TARGET_MODE 4213 isp_tmcmd_restart(isp); 4214 #endif 4215 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp); 4216 } 4217 4218 isp_ecmd_t * 4219 isp_get_ecmd(ispsoftc_t *isp) 4220 { 4221 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free; 4222 if (ecmd) { 4223 isp->isp_osinfo.ecmd_free = ecmd->next; 4224 } 4225 return (ecmd); 4226 } 4227 4228 void 4229 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd) 4230 { 4231 ecmd->next = isp->isp_osinfo.ecmd_free; 4232 isp->isp_osinfo.ecmd_free = ecmd; 4233 } 4234