2 * Copyright (c) 1997-2009 by Matthew Jacob
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/isp/isp_freebsd.c,v 1.176 2011/11/06 00:44:40 mjacob Exp $
30 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
32 #include <dev/disk/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
36 #include <sys/module.h>
37 #include <dev/disk/isp/isp_ioctl.h>
38 #include <sys/devicestat.h>
39 #include <bus/cam/cam_periph.h>
40 #include <bus/cam/cam_xpt_periph.h>
41 #include <sys/device.h>
43 #define THREAD_CREATE kthread_create
45 MODULE_VERSION(isp, 1);
46 MODULE_DEPEND(isp, cam, 1, 1, 1);
47 int isp_announced = 0;
48 int isp_fabric_hysteresis = 5;
49 int isp_loop_down_limit = 60; /* default loop down limit */
50 int isp_change_is_bad = 0; /* "changed" devices are bad */
51 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
52 int isp_gone_device_time = 30; /* grace time before reporting device lost */
53 int isp_autoconfig = 1; /* automatically attach/detach devices */
54 static const char *roles[4] = {
55 "(none)", "Target", "Initiator", "Target/Initiator"
57 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
58 static const char rqo[] = "%s: Request Queue Overflow\n";
60 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
61 static d_ioctl_t ispioctl;
62 static void isp_intr_enable(void *);
63 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
64 static void isp_poll(struct cam_sim *);
65 static timeout_t isp_watchdog;
66 static timeout_t isp_gdt;
67 static task_fn_t isp_gdt_task;
68 static timeout_t isp_ldt;
69 static task_fn_t isp_ldt_task;
70 static void isp_kthread(void *);
71 static void isp_action(struct cam_sim *, union ccb *);
72 #ifdef ISP_INTERNAL_TARGET
73 static void isp_target_thread_pi(void *);
74 static void isp_target_thread_fc(void *);
76 static void isp_timer(void *);
78 static struct dev_ops isp_ops = {
84 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
86 struct ccb_setasync csa;
88 struct cam_path *path;
91 * Construct our SIM entry.
93 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
94 cam_simq_release(devq);
101 if (xpt_bus_register(sim, chan) != CAM_SUCCESS) {
107 if (xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
109 xpt_bus_deregister(cam_sim_path(sim));
114 xpt_setup_ccb(&csa.ccb_h, path, 5);
115 csa.ccb_h.func_code = XPT_SASYNC_CB;
116 csa.event_enable = AC_LOST_DEVICE;
117 csa.callback = isp_cam_async;
118 csa.callback_arg = sim;
119 xpt_action((union ccb *)&csa);
122 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
125 #ifdef ISP_INTERNAL_TARGET
126 ISP_SET_PC(isp, chan, proc_active, 1);
127 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
128 ISP_SET_PC(isp, chan, proc_active, 0);
129 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
133 fcparam *fcp = FCPARAM(isp, chan);
134 struct isp_fc *fc = ISP_FC_PC(isp, chan);
142 callout_init(&fc->ldt);
143 callout_init(&fc->gdt);
144 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
145 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
148 * We start by being "loop down" if we have an initiator role
150 if (fcp->role & ISP_ROLE_INITIATOR) {
151 isp_freeze_loopdown(isp, chan, "isp_attach");
152 callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
153 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_second);
156 if (THREAD_CREATE(isp_kthread, fc, &fc->kthread, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
157 xpt_free_path(fc->path);
159 if (callout_active(&fc->ldt)) {
160 callout_stop(&fc->ldt);
162 xpt_bus_deregister(cam_sim_path(fc->sim));
164 cam_sim_free(fc->sim);
167 #ifdef ISP_INTERNAL_TARGET
168 ISP_SET_PC(isp, chan, proc_active, 1);
169 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
170 ISP_SET_PC(isp, chan, proc_active, 0);
171 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
175 SYSCTL_ADD_QUAD(&isp->isp_sysctl_ctx, SYSCTL_CHILDREN(isp->isp_sysctl_tree), OID_AUTO, "wwnn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwnn, 0, "World Wide Node Name");
176 SYSCTL_ADD_QUAD(&isp->isp_sysctl_ctx, SYSCTL_CHILDREN(isp->isp_sysctl_tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, 0, "World Wide Port Name");
177 SYSCTL_ADD_UINT(&isp->isp_sysctl_ctx, SYSCTL_CHILDREN(isp->isp_sysctl_tree), OID_AUTO, "loop_down_limit", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->loop_down_limit, 0, "Loop Down Limit");
178 SYSCTL_ADD_UINT(&isp->isp_sysctl_ctx, SYSCTL_CHILDREN(isp->isp_sysctl_tree), OID_AUTO, "gone_device_time", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->gone_device_time, 0, "Gone Device Time");
185 isp_attach(ispsoftc_t *isp)
187 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
188 int du = device_get_unit(isp->isp_dev);
191 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
192 isp->isp_osinfo.ehook.ich_arg = isp;
194 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
195 * will be called right awawy, which will clear isp_osinfo.ehook_active,
196 * which would be unwise to then set again later.
198 isp->isp_osinfo.ehook_active = 1;
199 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
200 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
205 * Create the device queue for our SIM(s).
207 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
208 if (isp->isp_osinfo.devq == NULL) {
209 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
213 sysctl_ctx_init(&isp->isp_sysctl_ctx);
214 isp->isp_sysctl_tree = SYSCTL_ADD_NODE(&isp->isp_sysctl_ctx,
215 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
216 device_get_nameunit(isp->isp_dev), CTLFLAG_RD, 0, "");
217 if (isp->isp_sysctl_tree == NULL) {
218 device_printf(isp->isp_dev, "can't add sysctl node\n");
221 for (chan = 0; chan < isp->isp_nchan; chan++) {
222 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
227 callout_init(&isp->isp_osinfo.tmo);
228 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
229 isp->isp_osinfo.timer_active = 1;
231 isp->isp_osinfo.cdev = make_dev(&isp_ops, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
232 if (isp->isp_osinfo.cdev) {
233 isp->isp_osinfo.cdev->si_drv1 = isp;
238 while (--chan >= 0) {
240 struct cam_path *path;
242 sim = ISP_FC_PC(isp, chan)->sim;
243 path = ISP_FC_PC(isp, chan)->path;
245 sim = ISP_SPI_PC(isp, chan)->sim;
246 path = ISP_SPI_PC(isp, chan)->path;
250 xpt_bus_deregister(cam_sim_path(sim));
254 if (isp->isp_osinfo.ehook_active) {
255 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
256 isp->isp_osinfo.ehook_active = 0;
258 if (isp->isp_osinfo.cdev) {
259 destroy_dev(isp->isp_osinfo.cdev);
260 isp->isp_osinfo.cdev = NULL;
262 isp->isp_osinfo.devq = NULL;
267 isp_detach(ispsoftc_t *isp)
270 struct cam_path *path;
271 struct ccb_setasync csa;
275 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
277 sim = ISP_FC_PC(isp, chan)->sim;
278 path = ISP_FC_PC(isp, chan)->path;
280 sim = ISP_SPI_PC(isp, chan)->sim;
281 path = ISP_SPI_PC(isp, chan)->path;
283 if (sim->refcount > 2) {
288 if (isp->isp_osinfo.timer_active) {
289 callout_stop(&isp->isp_osinfo.tmo);
290 isp->isp_osinfo.timer_active = 0;
292 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
294 sim = ISP_FC_PC(isp, chan)->sim;
295 path = ISP_FC_PC(isp, chan)->path;
297 sim = ISP_SPI_PC(isp, chan)->sim;
298 path = ISP_SPI_PC(isp, chan)->path;
300 xpt_setup_ccb(&csa.ccb_h, path, 5);
301 csa.ccb_h.func_code = XPT_SASYNC_CB;
302 csa.event_enable = 0;
303 csa.callback = isp_cam_async;
304 csa.callback_arg = sim;
305 xpt_action((union ccb *)&csa);
307 xpt_bus_deregister(cam_sim_path(sim));
311 if (isp->isp_osinfo.cdev) {
312 destroy_dev(isp->isp_osinfo.cdev);
313 isp->isp_osinfo.cdev = NULL;
315 if (isp->isp_osinfo.ehook_active) {
316 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
317 isp->isp_osinfo.ehook_active = 0;
319 if (isp->isp_osinfo.devq != NULL) {
320 isp->isp_osinfo.devq = NULL;
322 if (isp->isp_sysctl_tree != NULL)
323 sysctl_ctx_free(&isp->isp_sysctl_ctx);
328 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
331 struct isp_fc *fc = ISP_FC_PC(isp, chan);
332 if (fc->simqfrozen == 0) {
333 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
334 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
335 xpt_freeze_simq(fc->sim, 1);
337 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
338 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
344 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
347 struct isp_fc *fc = ISP_FC_PC(isp, chan);
348 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
349 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
350 if (wasfrozen && fc->simqfrozen == 0) {
351 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
352 xpt_release_simq(fc->sim, 1);
359 ispioctl(struct dev_ioctl_args *ap)
361 cdev_t dev = ap->a_head.a_dev;
362 caddr_t addr = ap->a_data;
363 u_long c = ap->a_cmd;
365 int nr, chan, retval = ENOTTY;
372 int olddblev = isp->isp_dblev;
373 isp->isp_dblev = *(int *)addr;
374 *(int *)addr = olddblev;
380 if (chan < 0 || chan >= isp->isp_nchan) {
385 *(int *)addr = FCPARAM(isp, chan)->role;
387 *(int *)addr = SDPARAM(isp, chan)->role;
394 if (chan < 0 || chan >= isp->isp_nchan) {
399 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
405 * We don't really support dual role at present on FC cards.
407 * We should, but a bunch of things are currently broken,
410 if (nr == ISP_ROLE_BOTH) {
411 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
415 *(int *)addr = FCPARAM(isp, chan)->role;
416 #ifdef ISP_INTERNAL_TARGET
418 retval = isp_fc_change_role(isp, chan, nr);
421 FCPARAM(isp, chan)->role = nr;
424 *(int *)addr = SDPARAM(isp, chan)->role;
425 SDPARAM(isp, chan)->role = nr;
432 #ifdef ISP_TARGET_MODE
433 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
443 if (chan < 0 || chan >= isp->isp_nchan) {
448 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
460 if (chan < 0 || chan >= isp->isp_nchan) {
465 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
473 case ISP_FC_GETDINFO:
475 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
481 if (ifc->loopid >= MAX_FC_TARG) {
485 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
486 if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
487 ifc->role = lp->roles;
488 ifc->loopid = lp->handle;
489 ifc->portid = lp->portid;
490 ifc->node_wwn = lp->node_wwn;
491 ifc->port_wwn = lp->port_wwn;
500 isp_stats_t *sp = (isp_stats_t *) addr;
502 ISP_MEMZERO(sp, sizeof (*sp));
503 sp->isp_stat_version = ISP_STATS_VERSION;
504 sp->isp_type = isp->isp_type;
505 sp->isp_revision = isp->isp_revision;
507 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
508 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
509 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
510 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
511 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
512 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
513 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
514 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
522 isp->isp_intbogus = 0;
523 isp->isp_intmboxc = 0;
524 isp->isp_intoasync = 0;
525 isp->isp_rsltccmplt = 0;
526 isp->isp_fphccmplt = 0;
527 isp->isp_rscchiwater = 0;
528 isp->isp_fpcchiwater = 0;
532 case ISP_FC_GETHINFO:
534 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
535 int chan = hba->fc_channel;
537 if (chan < 0 || chan >= isp->isp_nchan) {
541 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
542 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
543 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
544 hba->fc_nchannels = isp->isp_nchan;
546 hba->fc_nports = MAX_FC_TARG;
547 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
548 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
549 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
550 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
551 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
552 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
553 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
555 hba->fc_nports = MAX_TARGETS;
557 hba->fc_topology = 0;
558 hba->nvram_node_wwn = 0ull;
559 hba->nvram_port_wwn = 0ull;
560 hba->active_node_wwn = 0ull;
561 hba->active_port_wwn = 0ull;
569 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
578 if (chan < 0 || chan >= isp->isp_nchan) {
583 needmarker = retval = 0;
584 loopid = fct->loopid;
587 uint8_t local[QENTRY_LEN];
589 isp24xx_statusreq_t *sp;
590 fcparam *fcp = FCPARAM(isp, chan);
594 for (i = 0; i < MAX_FC_TARG; i++) {
595 lp = &fcp->portdb[i];
596 if (lp->handle == loopid) {
600 if (i == MAX_FC_TARG) {
605 /* XXX VALIDATE LP XXX */
606 tmf = (isp24xx_tmf_t *) local;
607 ISP_MEMZERO(tmf, QENTRY_LEN);
608 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
609 tmf->tmf_header.rqs_entry_count = 1;
610 tmf->tmf_nphdl = lp->handle;
612 tmf->tmf_timeout = 2;
613 tmf->tmf_tidlo = lp->portid;
614 tmf->tmf_tidhi = lp->portid >> 16;
615 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
616 tmf->tmf_lun[1] = fct->lun & 0xff;
617 if (fct->lun >= 256) {
618 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
620 switch (fct->action) {
622 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
624 case IPT_TARGET_RESET:
625 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
629 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
632 case IPT_CLEAR_TASK_SET:
633 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
636 case IPT_ABORT_TASK_SET:
637 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
648 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
649 mbs.param[1] = QENTRY_LEN;
650 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
651 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
652 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
653 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
655 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
660 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
661 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
662 sp = (isp24xx_statusreq_t *) local;
663 sp->req_completion_status = 1;
664 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
665 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
666 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
667 FC_SCRATCH_RELEASE(isp, chan);
668 if (retval || sp->req_completion_status != 0) {
669 FC_SCRATCH_RELEASE(isp, chan);
678 MBSINIT(&mbs, 0, MBLOGALL, 0);
679 if (ISP_CAP_2KLOGIN(isp) == 0) {
682 switch (fct->action) {
684 mbs.param[0] = MBOX_CLEAR_ACA;
685 mbs.param[1] = loopid;
686 mbs.param[2] = fct->lun;
688 case IPT_TARGET_RESET:
689 mbs.param[0] = MBOX_TARGET_RESET;
690 mbs.param[1] = loopid;
694 mbs.param[0] = MBOX_LUN_RESET;
695 mbs.param[1] = loopid;
696 mbs.param[2] = fct->lun;
699 case IPT_CLEAR_TASK_SET:
700 mbs.param[0] = MBOX_CLEAR_TASK_SET;
701 mbs.param[1] = loopid;
702 mbs.param[2] = fct->lun;
705 case IPT_ABORT_TASK_SET:
706 mbs.param[0] = MBOX_ABORT_TASK_SET;
707 mbs.param[1] = loopid;
708 mbs.param[2] = fct->lun;
717 FCPARAM(isp, chan)->sendmarker = 1;
719 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
735 isp_intr_enable(void *arg)
738 ispsoftc_t *isp = arg;
740 for (chan = 0; chan < isp->isp_nchan; chan++) {
742 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
743 ISP_ENABLE_INTS(isp);
747 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
748 ISP_ENABLE_INTS(isp);
753 isp->isp_osinfo.ehook_active = 0;
755 /* Release our hook so that the boot can continue. */
756 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
763 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
764 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
766 static ISP_INLINE int
767 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
769 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
770 if (ISP_PCMD(ccb) == NULL) {
773 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
777 static ISP_INLINE void
778 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
780 ((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
781 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
782 ISP_PCMD(ccb) = NULL;
785 * Put the target mode functions here, because some are inlines
788 #ifdef ISP_TARGET_MODE
789 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
790 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
791 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
792 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
793 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
794 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
795 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
796 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
797 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
798 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
799 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
800 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
801 static void isp_enable_lun(ispsoftc_t *, union ccb *);
802 static void isp_enable_deferred_luns(ispsoftc_t *, int);
803 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
804 static void isp_disable_lun(ispsoftc_t *, union ccb *);
805 static int isp_enable_target_mode(ispsoftc_t *, int);
806 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
807 static timeout_t isp_refire_putback_atio;
808 static void isp_complete_ctio(union ccb *);
809 static void isp_target_putback_atio(union ccb *);
810 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
811 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
812 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
813 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
814 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
815 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
816 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
817 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
818 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
819 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
820 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
821 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
823 static ISP_INLINE int
824 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
829 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
830 SLIST_FOREACH(tptr, lhp, next) {
831 if (xpt_path_lun_id(tptr->owner) == lun) {
839 dump_tstates(ispsoftc_t *isp, int bus)
843 tstate_t *tptr = NULL;
845 if (bus >= isp->isp_nchan) {
848 for (i = 0; i < LUN_HASH_SIZE; i++) {
849 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
851 SLIST_FOREACH(tptr, lhp, next) {
852 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
858 static ISP_INLINE tstate_t *
859 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
861 tstate_t *tptr = NULL;
865 if (bus < isp->isp_nchan) {
866 for (i = 0; i < LUN_HASH_SIZE; i++) {
867 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
868 SLIST_FOREACH(tptr, lhp, next) {
869 if (xpt_path_lun_id(tptr->owner) == lun) {
879 static ISP_INLINE tstate_t *
880 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
882 tstate_t *tptr = NULL;
883 atio_private_data_t *atp;
887 if (bus < isp->isp_nchan && tagval != 0) {
888 for (i = 0; i < LUN_HASH_SIZE; i++) {
889 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
890 SLIST_FOREACH(tptr, lhp, next) {
891 atp = isp_get_atpd(isp, tptr, tagval);
892 if (atp && atp->tag == tagval) {
902 static ISP_INLINE inot_private_data_t *
903 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
905 inot_private_data_t *ntp;
910 for (bus = 0; bus < isp->isp_nchan; bus++) {
911 for (i = 0; i < LUN_HASH_SIZE; i++) {
912 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
913 SLIST_FOREACH(tptr, lhp, next) {
914 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
925 static ISP_INLINE void
926 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
928 KASSERT((tptr->hold), ("tptr not held"));
933 isp_tmcmd_restart(ispsoftc_t *isp)
935 inot_private_data_t *ntp;
940 for (bus = 0; bus < isp->isp_nchan; bus++) {
941 for (i = 0; i < LUN_HASH_SIZE; i++) {
942 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
943 SLIST_FOREACH(tptr, lhp, next) {
944 inot_private_data_t *restart_queue = tptr->restart_queue;
945 tptr->restart_queue = NULL;
946 while (restart_queue) {
948 restart_queue = ntp->rd.nt.nt_hba;
950 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
951 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
953 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
954 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
956 isp_put_ntpd(isp, tptr, ntp);
957 if (tptr->restart_queue && restart_queue != NULL) {
958 ntp = tptr->restart_queue;
959 tptr->restart_queue = restart_queue;
960 while (restart_queue->rd.nt.nt_hba) {
961 restart_queue = restart_queue->rd.nt.nt_hba;
963 restart_queue->rd.nt.nt_hba = ntp;
972 static ISP_INLINE atio_private_data_t *
973 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
975 atio_private_data_t *atp;
980 tptr->atfree = atp->next;
984 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
985 if (atp->tag == tag) {
992 static ISP_INLINE void
993 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
997 atp->next = tptr->atfree;
1002 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1004 atio_private_data_t *atp;
1005 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1007 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1008 if (atp->tag == 0) {
1011 xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u last_xfr %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1012 atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->last_xframt, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1017 static ISP_INLINE inot_private_data_t *
1018 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1020 inot_private_data_t *ntp;
1023 tptr->ntfree = ntp->next;
1028 static ISP_INLINE inot_private_data_t *
1029 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1031 inot_private_data_t *ntp;
1032 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1033 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1040 static ISP_INLINE void
1041 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1043 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1044 ntp->next = tptr->ntfree;
1049 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1057 lun = xpt_path_lun_id(path);
1058 if (lun != CAM_LUN_WILDCARD) {
1059 if (lun >= ISP_MAX_LUNS(isp)) {
1060 return (CAM_LUN_INVALID);
1063 if (is_lun_enabled(isp, bus, lun)) {
1064 return (CAM_LUN_ALRDY_ENA);
1066 tptr = kmalloc(sizeof (tstate_t), M_DEVBUF, M_WAITOK | M_ZERO);
1067 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1068 if (status != CAM_REQ_CMP) {
1069 kfree(tptr, M_DEVBUF);
1072 SLIST_INIT(&tptr->atios);
1073 SLIST_INIT(&tptr->inots);
1074 for (i = 0; i < ATPDPSIZE-1; i++) {
1075 tptr->atpool[i].next = &tptr->atpool[i+1];
1076 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1078 tptr->atfree = tptr->atpool;
1079 tptr->ntfree = tptr->ntpool;
1081 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1082 SLIST_INSERT_HEAD(lhp, tptr, next);
1084 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1085 return (CAM_REQ_CMP);
1088 static ISP_INLINE void
1089 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1092 KASSERT((tptr->hold == 0), ("tptr still held"));
1093 ISP_GET_PC_ADDR(isp, xpt_path_path_id(tptr->owner), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1094 SLIST_REMOVE(lhp, tptr, tstate, next);
1095 xpt_free_path(tptr->owner);
1096 kfree(tptr, M_DEVBUF);
1103 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1105 tstate_t *tptr = NULL;
1106 int bus, tm_enabled, target_role;
1111 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1113 bus = XS_CHANNEL(ccb);
1114 target = ccb->ccb_h.target_id;
1115 lun = ccb->ccb_h.target_lun;
1116 if (target != CAM_TARGET_WILDCARD && target != 0) {
1117 ccb->ccb_h.status = CAM_TID_INVALID;
1121 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1122 ccb->ccb_h.status = CAM_LUN_INVALID;
1127 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1128 ccb->ccb_h.status = CAM_LUN_INVALID;
1132 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1133 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1137 * Wait until we're not busy with the lun enables subsystem
1139 while (isp->isp_osinfo.tmbusy) {
1140 isp->isp_osinfo.tmwanted = 1;
1141 mtx_sleep(isp, &isp->isp_lock, 0, "want_isp_enable_lun", 0);
1143 isp->isp_osinfo.tmbusy = 1;
1146 * This is as a good a place as any to check f/w capabilities.
1150 if (ISP_CAP_TMODE(isp) == 0) {
1151 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1152 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1156 * We *could* handle non-SCCLUN f/w, but we'd have to
1157 * dork with our already fragile enable/disable code.
1159 if (ISP_CAP_SCCFW(isp) == 0) {
1160 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1161 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1165 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1168 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1172 * Create the state pointer.
1173 * It should not already exist.
1175 tptr = get_lun_statep(isp, bus, lun);
1177 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1180 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1181 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1186 * We have a tricky maneuver to perform here.
1188 * If target mode isn't already enabled here,
1189 * *and* our current role includes target mode,
1190 * we enable target mode here.
1193 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1194 if (tm_enabled == 0 && target_role != 0) {
1195 if (isp_enable_target_mode(isp, bus)) {
1196 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1197 destroy_lun_state(isp, tptr);
1205 * Now check to see whether this bus is in target mode already.
1207 * If not, a later role change into target mode will finish the job.
1209 if (tm_enabled == 0) {
1210 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1211 ccb->ccb_h.status = CAM_REQ_CMP;
1212 xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
1219 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1222 if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
1223 destroy_lun_state(isp, tptr);
1227 rls_lun_statep(isp, tptr);
1229 isp->isp_osinfo.tmbusy = 0;
1230 if (isp->isp_osinfo.tmwanted) {
1231 isp->isp_osinfo.tmwanted = 0;
1238 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1241 * XXX: not entirely implemented yet
1243 (void) isp_enable_deferred(isp, bus, 0);
1247 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1251 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1252 if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
1253 status = CAM_REQ_CMP;
1255 int cmd_cnt, not_cnt;
1258 cmd_cnt = DFLT_CMND_CNT;
1259 not_cnt = DFLT_INOT_CNT;
1264 status = CAM_REQ_INPROG;
1265 isp->isp_osinfo.rptr = &status;
1266 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1267 status = CAM_RESRC_UNAVAIL;
1269 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1271 isp->isp_osinfo.rptr = NULL;
1274 if (status == CAM_REQ_CMP) {
1275 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1276 isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1282 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1284 tstate_t *tptr = NULL;
1290 bus = XS_CHANNEL(ccb);
1291 target = ccb->ccb_h.target_id;
1292 lun = ccb->ccb_h.target_lun;
1293 if (target != CAM_TARGET_WILDCARD && target != 0) {
1294 ccb->ccb_h.status = CAM_TID_INVALID;
1298 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1299 ccb->ccb_h.status = CAM_LUN_INVALID;
1304 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1305 ccb->ccb_h.status = CAM_LUN_INVALID;
1309 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1310 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1314 * See if we're busy disabling a lun now.
1316 while (isp->isp_osinfo.tmbusy) {
1317 isp->isp_osinfo.tmwanted = 1;
1318 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
1320 isp->isp_osinfo.tmbusy = 1;
1323 * Find the state pointer.
1325 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1326 ccb->ccb_h.status = CAM_PATH_INVALID;
1331 * If we're a 24XX card, we're done.
1334 status = CAM_REQ_CMP;
1339 * For SCC FW, we only deal with lun zero.
1345 isp->isp_osinfo.rptr = &status;
1346 status = CAM_REQ_INPROG;
1347 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1348 status = CAM_RESRC_UNAVAIL;
1350 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1353 if (status == CAM_REQ_CMP) {
1354 xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
1357 destroy_lun_state(isp, tptr);
1359 isp->isp_osinfo.rptr = NULL;
1360 isp->isp_osinfo.tmbusy = 0;
1361 if (isp->isp_osinfo.tmwanted) {
1362 isp->isp_osinfo.tmwanted = 0;
1369 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1373 ISP_GET_PC(isp, bus, tm_enabled, ct);
1381 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1382 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1383 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1384 mbs.param[2] = bus << 7;
1385 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1386 isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
1389 SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
1391 ISP_SET_PC(isp, bus, tm_enabled, 1);
1392 isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
1398 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1402 ISP_GET_PC(isp, bus, tm_enabled, ct);
1410 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1411 mbs.param[2] = bus << 7;
1412 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1413 isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
1416 SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
1418 ISP_SET_PC(isp, bus, tm_enabled, 0);
1419 isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
1425 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1429 rptr = isp->isp_osinfo.rptr;
1430 if (lep->le_status != LUN_OK) {
1431 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1433 *rptr = CAM_REQ_CMP_ERR;
1438 *rptr = CAM_REQ_CMP;
1445 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1449 atio_private_data_t *atp;
1450 struct ccb_scsiio *cso = &ccb->csio;
1451 uint32_t dmaresult, handle;
1452 uint8_t local[QENTRY_LEN];
1455 * Do some sanity checks.
1457 if (cso->dxfer_len == 0) {
1458 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1459 xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1460 ccb->ccb_h.status = CAM_REQ_INVALID;
1466 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1468 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1470 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1471 dump_tstates(isp, XS_CHANNEL(ccb));
1472 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1478 atp = isp_get_atpd(isp, tptr, cso->tag_id);
1480 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1481 isp_dump_atpd(isp, tptr);
1482 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1487 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1488 ccb->ccb_h.status = CAM_REQ_ABORTED;
1494 * Check to make sure we're still in target mode.
1496 if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1497 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
1498 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1504 * Get some resources
1506 if (isp_get_pcmd(isp, ccb)) {
1507 rls_lun_statep(isp, tptr);
1508 xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1509 cam_freeze_devq(ccb->ccb_h.path);
1510 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1511 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1515 qe = isp_getrqentry(isp);
1517 xpt_print(ccb->ccb_h.path, rqo, __func__);
1518 cam_freeze_devq(ccb->ccb_h.path);
1519 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1520 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1523 memset(local, 0, QENTRY_LEN);
1526 * We're either moving data or completing a command here.
1529 ct7_entry_t *cto = (ct7_entry_t *) local;
1531 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1532 cto->ct_header.rqs_entry_count = 1;
1533 cto->ct_header.rqs_seqno = 1;
1534 cto->ct_nphdl = atp->nphdl;
1535 cto->ct_rxid = atp->tag;
1536 cto->ct_iid_lo = atp->portid;
1537 cto->ct_iid_hi = atp->portid >> 16;
1538 cto->ct_oxid = atp->oxid;
1539 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1540 cto->ct_scsi_status = cso->scsi_status;
1541 cto->ct_timeout = 120;
1542 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1543 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1544 cto->ct_flags |= CT7_SENDSTATUS;
1546 if (cso->dxfer_len == 0) {
1547 cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1548 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1549 int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1550 cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1551 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1552 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1555 cto->ct_flags |= CT7_FLAG_MODE0;
1556 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1557 cto->ct_flags |= CT7_DATA_IN;
1559 cto->ct_flags |= CT7_DATA_OUT;
1561 cto->rsp.m0.reloff = atp->bytes_xfered;
1563 * Don't overrun the limits placed on us
1565 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1566 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1568 atp->last_xframt = cso->dxfer_len;
1569 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1571 if (cto->ct_flags & CT7_SENDSTATUS) {
1572 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1573 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1574 if (cto->ct_resid < 0) {
1575 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1576 } else if (cto->ct_resid > 0) {
1577 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1579 atp->state = ATPD_STATE_LAST_CTIO;
1580 ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1581 atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1584 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1585 cso->dxfer_len, atp->bytes_xfered);
1586 atp->state = ATPD_STATE_CTIO;
1588 } else if (IS_FC(isp)) {
1589 ct2_entry_t *cto = (ct2_entry_t *) local;
1591 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1592 cto->ct_header.rqs_entry_count = 1;
1593 cto->ct_header.rqs_seqno = 1;
1594 if (ISP_CAP_2KLOGIN(isp) == 0) {
1595 ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1597 cto->ct_iid = cso->init_id;
1598 if (ISP_CAP_SCCFW(isp) == 0) {
1599 cto->ct_lun = ccb->ccb_h.target_lun;
1604 cto->ct_rxid = cso->tag_id;
1605 if (cso->dxfer_len == 0) {
1606 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1607 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1608 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1609 if (cto->ct_resid < 0) {
1610 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1611 } else if (cto->ct_resid > 0) {
1612 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1614 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1615 int m = min(cso->sense_len, MAXRESPLEN);
1616 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1617 cto->rsp.m1.ct_senselen = m;
1618 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1619 } else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1623 xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1626 cto->ct_flags |= CT2_FLAG_MODE0;
1627 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1628 cto->ct_flags |= CT2_DATA_IN;
1630 cto->ct_flags |= CT2_DATA_OUT;
1632 cto->ct_reloff = atp->bytes_xfered;
1633 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1635 * Don't overrun the limits placed on us
1637 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1638 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1640 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1641 cto->ct_flags |= CT2_SENDSTATUS;
1642 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1643 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1644 if (cto->ct_resid < 0) {
1645 cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1646 } else if (cto->ct_resid > 0) {
1647 cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1650 atp->last_xframt = cso->dxfer_len;
1653 * If we're sending data and status back together,
1654 * we can't also send back sense data as well.
1656 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1659 if (cto->ct_flags & CT2_SENDSTATUS) {
1660 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1661 cto->ct_flags |= CT2_CCINCR;
1662 atp->state = ATPD_STATE_LAST_CTIO;
1663 ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1664 atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1667 atp->state = ATPD_STATE_CTIO;
1668 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1669 cso->dxfer_len, atp->bytes_xfered);
1671 cto->ct_timeout = 10;
1673 ct_entry_t *cto = (ct_entry_t *) local;
1675 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1676 cto->ct_header.rqs_entry_count = 1;
1677 cto->ct_header.rqs_seqno = 1;
1678 cto->ct_iid = cso->init_id;
1679 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1680 cto->ct_tgt = ccb->ccb_h.target_id;
1681 cto->ct_lun = ccb->ccb_h.target_lun;
1682 cto->ct_fwhandle = cso->tag_id >> 16;
1683 if (AT_HAS_TAG(cso->tag_id)) {
1684 cto->ct_tag_val = cso->tag_id;
1685 cto->ct_flags |= CT_TQAE;
1687 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1688 cto->ct_flags |= CT_NODISC;
1690 if (cso->dxfer_len == 0) {
1691 cto->ct_flags |= CT_NO_DATA;
1692 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1693 cto->ct_flags |= CT_DATA_IN;
1695 cto->ct_flags |= CT_DATA_OUT;
1697 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1698 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1699 cto->ct_scsi_status = cso->scsi_status;
1700 cto->ct_resid = cso->resid;
1701 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1702 cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1704 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1705 cto->ct_timeout = 10;
1708 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1709 xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1710 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1716 * Call the dma setup routines for this entry (and any subsequent
1717 * CTIOs) if there's data to move, and then tell the f/w it's got
1718 * new things to play with. As with isp_start's usage of DMA setup,
1719 * any swizzling is done in the machine dependent layer. Because
1720 * of this, we put the request onto the queue area first in native
1725 ct7_entry_t *cto = (ct7_entry_t *) local;
1726 cto->ct_syshandle = handle;
1727 } else if (IS_FC(isp)) {
1728 ct2_entry_t *cto = (ct2_entry_t *) local;
1729 cto->ct_syshandle = handle;
1731 ct_entry_t *cto = (ct_entry_t *) local;
1732 cto->ct_syshandle = handle;
1735 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1736 if (dmaresult == CMD_QUEUED) {
1738 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1739 rls_lun_statep(isp, tptr);
1742 if (dmaresult == CMD_EAGAIN) {
1743 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1745 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1747 isp_destroy_tgt_handle(isp, handle);
1749 rls_lun_statep(isp, tptr);
1750 isp_free_pcmd(isp, ccb);
1755 isp_refire_putback_atio(void *arg)
1757 union ccb *ccb = arg;
1758 ispsoftc_t *isp = XS_ISP(ccb);
1760 isp_target_putback_atio(ccb);
1765 isp_target_putback_atio(union ccb *ccb)
1768 struct ccb_scsiio *cso;
1773 qe = isp_getrqentry(isp);
1775 xpt_print(ccb->ccb_h.path, rqo, __func__);
1776 (void) timeout(isp_refire_putback_atio, ccb, 10);
1779 memset(qe, 0, QENTRY_LEN);
1782 at2_entry_t local, *at = &local;
1783 ISP_MEMZERO(at, sizeof (at2_entry_t));
1784 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1785 at->at_header.rqs_entry_count = 1;
1786 if (ISP_CAP_SCCFW(isp)) {
1787 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1789 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1791 at->at_status = CT_OK;
1792 at->at_rxid = cso->tag_id;
1793 at->at_iid = cso->ccb_h.target_id;
1794 isp_put_atio2(isp, at, qe);
1796 at_entry_t local, *at = &local;
1797 ISP_MEMZERO(at, sizeof (at_entry_t));
1798 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1799 at->at_header.rqs_entry_count = 1;
1800 at->at_iid = cso->init_id;
1801 at->at_iid |= XS_CHANNEL(ccb) << 7;
1802 at->at_tgt = cso->ccb_h.target_id;
1803 at->at_lun = cso->ccb_h.target_lun;
1804 at->at_status = CT_OK;
1805 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1806 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1807 isp_put_atio(isp, at, qe);
1809 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1810 ISP_SYNC_REQUEST(isp);
1811 isp_complete_ctio(ccb);
1815 isp_complete_ctio(union ccb *ccb)
1817 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1818 ccb->ccb_h.status |= CAM_REQ_CMP;
1820 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1821 isp_free_pcmd(XS_ISP(ccb), ccb);
1826 * Handle ATIO stuff that the generic code can't.
1827 * This means handling CDBs.
1831 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1835 struct ccb_accept_tio *atiop;
1836 atio_private_data_t *atp;
1839 * The firmware status (except for the QLTM_SVALID bit)
1840 * indicates why this ATIO was sent to us.
1842 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1844 * If the DISCONNECTS DISABLED bit is set in the flags field,
1845 * we're still connected on the SCSI bus.
1847 status = aep->at_status;
1848 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1850 * Bus Phase Sequence error. We should have sense data
1851 * suggested by the f/w. I'm not sure quite yet what
1852 * to do about this for CAM.
1854 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1855 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1858 if ((status & ~QLTM_SVALID) != AT_CDB) {
1859 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1860 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1864 bus = GET_BUS_VAL(aep->at_iid);
1865 tptr = get_lun_statep(isp, bus, aep->at_lun);
1867 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1870 * Because we can't autofeed sense data back with
1871 * a command for parallel SCSI, we can't give back
1872 * a CHECK CONDITION. We'll give back a BUSY status
1873 * instead. This works out okay because the only
1874 * time we should, in fact, get this, is in the
1875 * case that somebody configured us without the
1876 * blackhole driver, so they get what they deserve.
1878 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1883 atp = isp_get_atpd(isp, tptr, 0);
1884 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1885 if (atiop == NULL || atp == NULL) {
1887 * Because we can't autofeed sense data back with
1888 * a command for parallel SCSI, we can't give back
1889 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1890 * instead. This works out okay because the only time we
1891 * should, in fact, get this, is in the case that we've
1894 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1895 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1896 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1898 isp_put_atpd(isp, tptr, atp);
1900 rls_lun_statep(isp, tptr);
1903 atp->tag = aep->at_tag_val;
1904 if (atp->tag == 0) {
1907 atp->state = ATPD_STATE_ATIO;
1908 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1910 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1911 atiop->ccb_h.target_id = aep->at_tgt;
1912 atiop->ccb_h.target_lun = aep->at_lun;
1913 if (aep->at_flags & AT_NODISC) {
1914 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1916 atiop->ccb_h.flags = 0;
1919 if (status & QLTM_SVALID) {
1920 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1921 atiop->sense_len = amt;
1922 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1924 atiop->sense_len = 0;
1927 atiop->init_id = GET_IID_VAL(aep->at_iid);
1928 atiop->cdb_len = aep->at_cdblen;
1929 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1930 atiop->ccb_h.status = CAM_CDB_RECVD;
1932 * Construct a tag 'id' based upon tag value (which may be 0..255)
1933 * and the handle (which we have to preserve).
1935 atiop->tag_id = atp->tag;
1936 if (aep->at_flags & AT_TQAE) {
1937 atiop->tag_action = aep->at_tag_type;
1938 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1940 atp->orig_datalen = 0;
1941 atp->bytes_xfered = 0;
1942 atp->last_xframt = 0;
1943 atp->lun = aep->at_lun;
1944 atp->nphdl = aep->at_iid;
1945 atp->portid = PORT_NONE;
1947 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1948 atp->tattr = aep->at_tag_type;
1949 atp->state = ATPD_STATE_CAM;
1950 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1951 rls_lun_statep(isp, tptr);
1955 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1960 struct ccb_accept_tio *atiop;
1962 atio_private_data_t *atp;
1963 inot_private_data_t *ntp;
1966 * The firmware status (except for the QLTM_SVALID bit)
1967 * indicates why this ATIO was sent to us.
1969 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1971 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1972 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1973 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1977 if (ISP_CAP_SCCFW(isp)) {
1978 lun = aep->at_scclun;
1982 if (ISP_CAP_2KLOGIN(isp)) {
1983 nphdl = ((at2e_entry_t *)aep)->at_iid;
1985 nphdl = aep->at_iid;
1987 tptr = get_lun_statep(isp, 0, lun);
1989 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1991 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1992 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1998 * Start any commands pending resources first.
2000 if (tptr->restart_queue) {
2001 inot_private_data_t *restart_queue = tptr->restart_queue;
2002 tptr->restart_queue = NULL;
2003 while (restart_queue) {
2004 ntp = restart_queue;
2005 restart_queue = ntp->rd.nt.nt_hba;
2006 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2007 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2008 isp_put_ntpd(isp, tptr, ntp);
2010 * If a recursion caused the restart queue to start to fill again,
2011 * stop and splice the new list on top of the old list and restore
2012 * it and go to noresrc.
2014 if (tptr->restart_queue) {
2015 ntp = tptr->restart_queue;
2016 tptr->restart_queue = restart_queue;
2017 while (restart_queue->rd.nt.nt_hba) {
2018 restart_queue = restart_queue->rd.nt.nt_hba;
2020 restart_queue->rd.nt.nt_hba = ntp;
2026 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2027 if (atiop == NULL) {
2031 atp = isp_get_atpd(isp, tptr, 0);
2036 atp->tag = aep->at_rxid;
2037 atp->state = ATPD_STATE_ATIO;
2038 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2040 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2041 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2042 atiop->ccb_h.target_lun = lun;
2045 * We don't get 'suggested' sense data as we do with SCSI cards.
2047 atiop->sense_len = 0;
2048 if (ISP_CAP_2KLOGIN(isp)) {
2050 * NB: We could not possibly have 2K logins if we
2051 * NB: also did not have SCC FW.
2053 atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2055 atiop->init_id = aep->at_iid;
2059 * If we're not in the port database, add ourselves.
2061 if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2063 (((uint64_t) aep->at_wwpn[0]) << 48) |
2064 (((uint64_t) aep->at_wwpn[1]) << 32) |
2065 (((uint64_t) aep->at_wwpn[2]) << 16) |
2066 (((uint64_t) aep->at_wwpn[3]) << 0);
2068 * However, make sure we delete ourselves if otherwise
2069 * we were there but at a different loop id.
2071 if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2072 isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2074 isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2076 atiop->cdb_len = ATIO2_CDBLEN;
2077 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2078 atiop->ccb_h.status = CAM_CDB_RECVD;
2079 atiop->tag_id = atp->tag;
2080 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2081 case ATIO2_TC_ATTR_SIMPLEQ:
2082 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2083 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2085 case ATIO2_TC_ATTR_HEADOFQ:
2086 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2087 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2089 case ATIO2_TC_ATTR_ORDERED:
2090 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2091 atiop->tag_action = MSG_ORDERED_Q_TAG;
2093 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2094 case ATIO2_TC_ATTR_UNTAGGED:
2096 atiop->tag_action = 0;
2100 atp->orig_datalen = aep->at_datalen;
2101 atp->bytes_xfered = 0;
2102 atp->last_xframt = 0;
2104 atp->nphdl = atiop->init_id;
2105 atp->sid = PORT_ANY;
2106 atp->oxid = aep->at_oxid;
2107 atp->cdb0 = aep->at_cdb[0];
2108 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2109 atp->state = ATPD_STATE_CAM;
2110 xpt_done((union ccb *)atiop);
2111 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2112 rls_lun_statep(isp, tptr);
2115 ntp = isp_get_ntpd(isp, tptr);
2117 rls_lun_statep(isp, tptr);
2118 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2121 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2122 ntp->rd.nt.nt_hba = tptr->restart_queue;
2123 tptr->restart_queue = ntp;
2124 rls_lun_statep(isp, tptr);
2128 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2131 uint16_t lun, chan, nphdl = NIL_HANDLE;
2133 uint64_t wwn = INI_NONE;
2136 struct ccb_accept_tio *atiop;
2137 atio_private_data_t *atp = NULL;
2138 inot_private_data_t *ntp;
2140 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2141 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2142 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2145 * Find the N-port handle, and Virtual Port Index for this command.
2147 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2148 * We also, as a matter of course, need to know the WWN of the initiator too.
2150 if (ISP_CAP_MULTI_ID(isp)) {
2152 * Find the right channel based upon D_ID
2154 isp_find_chan_by_did(isp, did, &chan);
2156 if (chan == ISP_NOCHAN) {
2160 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2161 * It's a bit tricky here as we need to stash this command *somewhere*.
2164 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2165 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2166 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2169 tptr = get_lun_statep(isp, 0, 0);
2171 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2173 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2174 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2178 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2181 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2187 * Find the PDB entry for this initiator
2189 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2191 * If we're not in the port database terminate the exchange.
2193 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",
2194 __func__, aep->at_rxid, did, chan, sid);
2195 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2202 * Get the tstate pointer
2204 tptr = get_lun_statep(isp, chan, lun);
2206 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2208 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2209 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2215 * Start any commands pending resources first.
2217 if (tptr->restart_queue) {
2218 inot_private_data_t *restart_queue = tptr->restart_queue;
2219 tptr->restart_queue = NULL;
2220 while (restart_queue) {
2221 ntp = restart_queue;
2222 restart_queue = ntp->rd.nt.nt_hba;
2223 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2224 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2225 isp_put_ntpd(isp, tptr, ntp);
2227 * If a recursion caused the restart queue to start to fill again,
2228 * stop and splice the new list on top of the old list and restore
2229 * it and go to noresrc.
2231 if (tptr->restart_queue) {
2232 if (restart_queue) {
2233 ntp = tptr->restart_queue;
2234 tptr->restart_queue = restart_queue;
2235 while (restart_queue->rd.nt.nt_hba) {
2236 restart_queue = restart_queue->rd.nt.nt_hba;
2238 restart_queue->rd.nt.nt_hba = ntp;
2246 * If the f/w is out of resources, just send a BUSY status back.
2248 if (aep->at_rxid == AT7_NORESRC_RXID) {
2249 rls_lun_statep(isp, tptr);
2250 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2255 * If we're out of resources, just send a BUSY status back.
2257 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2258 if (atiop == NULL) {
2259 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2263 atp = isp_get_atpd(isp, tptr, 0);
2265 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2268 if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2269 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)\n",
2270 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2272 * It's not a "no resource" condition- but we can treat it like one
2277 atp->tag = aep->at_rxid;
2278 atp->state = ATPD_STATE_ATIO;
2279 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2281 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2282 atiop->init_id = nphdl;
2283 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2284 atiop->ccb_h.target_lun = lun;
2285 atiop->sense_len = 0;
2286 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2288 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2290 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2291 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2292 atiop->cdb_len = cdbxlen;
2293 atiop->ccb_h.status = CAM_CDB_RECVD;
2294 atiop->tag_id = atp->tag;
2295 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2296 case FCP_CMND_TASK_ATTR_SIMPLE:
2297 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2298 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2300 case FCP_CMND_TASK_ATTR_HEAD:
2301 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2302 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2304 case FCP_CMND_TASK_ATTR_ORDERED:
2305 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2306 atiop->tag_action = MSG_ORDERED_Q_TAG;
2310 case FCP_CMND_TASK_ATTR_ACA:
2311 case FCP_CMND_TASK_ATTR_UNTAGGED:
2312 atiop->tag_action = 0;
2315 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2316 atp->bytes_xfered = 0;
2317 atp->last_xframt = 0;
2321 atp->oxid = aep->at_hdr.ox_id;
2322 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2323 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2324 atp->state = ATPD_STATE_CAM;
2325 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2326 xpt_done((union ccb *)atiop);
2327 rls_lun_statep(isp, tptr);
2331 isp_put_atpd(isp, tptr, atp);
2333 ntp = isp_get_ntpd(isp, tptr);
2335 rls_lun_statep(isp, tptr);
2336 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2339 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2340 ntp->rd.nt.nt_hba = tptr->restart_queue;
2341 tptr->restart_queue = ntp;
2342 rls_lun_statep(isp, tptr);
2346 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2349 int sentstatus, ok, notify_cam, resid = 0;
2350 tstate_t *tptr = NULL;
2351 atio_private_data_t *atp = NULL;
2353 uint32_t tval, handle;
2356 * CTIO handles are 16 bits.
2357 * CTIO2 and CTIO7 are 32 bits.
2361 handle = ((ct_entry_t *)arg)->ct_syshandle;
2363 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2365 ccb = isp_find_xs_tgt(isp, handle);
2367 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2370 isp_destroy_tgt_handle(isp, handle);
2371 bus = XS_CHANNEL(ccb);
2372 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2374 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2376 KASSERT((tptr != NULL), ("cannot get state pointer"));
2377 if (isp->isp_nactive) {
2381 ct7_entry_t *ct = arg;
2383 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2385 rls_lun_statep(isp, tptr);
2386 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2390 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2391 ok = (ct->ct_nphdl == CT7_OK);
2392 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2393 ccb->ccb_h.status |= CAM_SENT_SENSE;
2395 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2396 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2397 resid = ct->ct_resid;
2398 atp->bytes_xfered += (atp->last_xframt - resid);
2399 atp->last_xframt = 0;
2401 if (ct->ct_nphdl == CT_HBA_RESET) {
2405 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2407 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2410 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2411 ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2412 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2413 } else if (IS_FC(isp)) {
2414 ct2_entry_t *ct = arg;
2416 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2418 rls_lun_statep(isp, tptr);
2419 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2422 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2423 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2424 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2425 ccb->ccb_h.status |= CAM_SENT_SENSE;
2427 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2428 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2429 resid = ct->ct_resid;
2430 atp->bytes_xfered += (atp->last_xframt - resid);
2431 atp->last_xframt = 0;
2433 if (ct->ct_status == CT_HBA_RESET) {
2437 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2439 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2441 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2442 ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2444 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2446 ct_entry_t *ct = arg;
2447 sentstatus = ct->ct_flags & CT_SENDSTATUS;
2448 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2450 * We *ought* to be able to get back to the original ATIO
2451 * here, but for some reason this gets lost. It's just as
2452 * well because it's squirrelled away as part of periph
2455 * We can live without it as long as we continue to use
2456 * the auto-replenish feature for CTIOs.
2458 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2459 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2463 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2465 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2466 } else if (ct->ct_status & QLTM_SVALID) {
2467 char *sp = (char *)ct;
2468 sp += CTIO_SENSE_OFFSET;
2469 ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2470 ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2471 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2473 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2474 resid = ct->ct_resid;
2476 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2477 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2478 tval = ct->ct_fwhandle;
2480 ccb->csio.resid += resid;
2483 * We're here either because intermediate data transfers are done
2484 * and/or the final status CTIO (which may have joined with a
2485 * Data Transfer) is done.
2487 * In any case, for this platform, the upper layers figure out
2488 * what to do next, so all we do here is collect status and
2489 * pass information along. Any DMA handles have already been
2492 if (notify_cam == 0) {
2493 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
2497 rls_lun_statep(isp, tptr);
2499 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? " FINAL " : "MIDTERM ", tval);
2501 if (!ok && !IS_24XX(isp)) {
2502 isp_target_putback_atio(ccb);
2504 isp_complete_ctio(ccb);
2509 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2511 (void) isp_notify_ack(isp, inot);
2515 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2518 switch (inp->in_status) {
2519 case IN_PORT_LOGOUT:
2521 * XXX: Need to delete this initiator's WWN from the database
2522 * XXX: Need to send this LOGOUT upstream
2524 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2526 case IN_PORT_CHANGED:
2527 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2529 case IN_GLOBAL_LOGO:
2530 isp_del_all_wwn_entries(isp, 0);
2531 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2539 atio_private_data_t *atp;
2541 struct ccb_immediate_notify *inot = NULL;
2543 if (ISP_CAP_SCCFW(isp)) {
2544 lun = inp->in_scclun;
2548 if (ISP_CAP_2KLOGIN(isp)) {
2549 loopid = ((in_fcentry_e_t *)inp)->in_iid;
2551 loopid = inp->in_iid;
2553 if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2558 tptr = get_lun_statep(isp, 0, lun);
2560 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2562 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2566 atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2569 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2570 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2573 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2574 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2576 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2579 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2582 isp_notify_t tmp, *nt = &tmp;
2583 ISP_MEMZERO(nt, sizeof (isp_notify_t));
2585 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2587 nt->nt_nphdl = loopid;
2588 nt->nt_sid = PORT_ANY;
2589 nt->nt_did = PORT_ANY;
2591 nt->nt_need_ack = 1;
2593 nt->nt_ncode = NT_ABORT_TASK;
2594 nt->nt_lreserved = inot;
2595 isp_handle_platform_target_tmf(isp, nt);
2598 rls_lun_statep(isp, tptr);
2605 (void) isp_notify_ack(isp, inp);
2610 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2615 uint8_t *ptr = NULL;
2618 nphdl = inot->in_nphdl;
2619 if (nphdl != NIL_HANDLE) {
2620 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2625 switch (inot->in_status) {
2626 case IN24XX_ELS_RCVD:
2629 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2632 * Note that we're just getting notification that an ELS was received
2633 * (possibly with some associated information sent upstream). This is
2634 * *not* the same as being given the ELS frame to accept or reject.
2636 switch (inot->in_status_subcode) {
2639 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2640 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2641 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
2642 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2643 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2644 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2645 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2646 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2647 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
2648 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2652 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2660 * Treat PRLI the same as PLOGI and make a database entry for it.
2662 if (inot->in_status_subcode == PLOGI)
2666 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2667 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2668 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
2669 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2670 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2671 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2672 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2673 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2674 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
2675 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2679 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2688 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2692 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2693 isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
2696 isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
2697 inot->in_rxid, inot->in_oxid);
2698 (void) isp_notify_ack(isp, inot);
2702 case IN24XX_PORT_LOGOUT:
2703 ptr = "PORT LOGOUT";
2704 if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2705 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2708 case IN24XX_PORT_CHANGED:
2710 ptr = "PORT CHANGED";
2713 case IN24XX_LIP_RESET:
2717 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr, inot->in_status_subcode, nphdl);
2720 * All subcodes here are irrelevant. What is relevant
2721 * is that we need to terminate all active commands from
2722 * this initiator (known by N-port handle).
2724 /* XXX IMPLEMENT XXX */
2725 (void) isp_notify_ack(isp, inot);
2728 case IN24XX_LINK_RESET:
2729 case IN24XX_LINK_FAILED:
2730 case IN24XX_SRR_RCVD:
2732 (void) isp_notify_ack(isp, inot);
2738 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2741 if (isp->isp_state != ISP_RUNSTATE) {
2742 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2747 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2749 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2750 ct7_entry_t local, *cto = &local;
2751 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2756 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2757 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2762 ISP_MEMZERO(&local, sizeof (local));
2763 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2764 cto->ct_header.rqs_entry_count = 1;
2765 cto->ct_nphdl = nphdl;
2766 cto->ct_rxid = aep->at_rxid;
2767 cto->ct_vpidx = mp->nt_channel;
2768 cto->ct_iid_lo = sid;
2769 cto->ct_iid_hi = sid >> 16;
2770 cto->ct_oxid = aep->at_hdr.ox_id;
2771 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2772 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2773 return (isp_target_put_entry(isp, &local));
2777 * This case is for a responding to an ABTS frame
2779 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2782 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2784 if (mp->nt_need_ack) {
2785 uint8_t storage[QENTRY_LEN];
2786 ct7_entry_t *cto = (ct7_entry_t *) storage;
2787 abts_t *abts = (abts_t *)mp->nt_lreserved;
2789 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2790 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2791 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2792 cto->ct_header.rqs_entry_count = 1;
2793 cto->ct_nphdl = mp->nt_nphdl;
2794 cto->ct_rxid = abts->abts_rxid_task;
2795 cto->ct_iid_lo = mp->nt_sid;
2796 cto->ct_iid_hi = mp->nt_sid >> 16;
2797 cto->ct_oxid = abts->abts_ox_id;
2798 cto->ct_vpidx = mp->nt_channel;
2799 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2800 if (isp_target_put_entry(isp, cto)) {
2803 mp->nt_need_ack = 0;
2805 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2813 * Handle logout cases here
2815 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2816 isp_del_all_wwn_entries(isp, mp->nt_channel);
2819 if (mp->nt_ncode == NT_LOGOUT) {
2820 if (!IS_2100(isp) && IS_FC(isp)) {
2821 isp_del_wwn_entries(isp, mp);
2826 * General purpose acknowledgement
2828 if (mp->nt_need_ack) {
2829 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2830 return (isp_notify_ack(isp, mp->nt_lreserved));
2836 * Handle task management functions.
2838 * We show up here with a notify structure filled out.
2840 * The nt_lreserved tag points to the original queue entry
2843 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2847 struct ccb_immediate_notify *inot;
2848 inot_private_data_t *ntp = NULL;
2851 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2852 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2854 * NB: This assignment is necessary because of tricky type conversion.
2855 * XXX: This is tricky and I need to check this. If the lun isn't known
2856 * XXX: for the task management function, it does not of necessity follow
2857 * XXX: that it should go up stream to the wildcard listener.
2859 if (notify->nt_lun == LUN_ANY) {
2860 lun = CAM_LUN_WILDCARD;
2862 lun = notify->nt_lun;
2864 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2866 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2868 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2872 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2874 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2878 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2879 inot->initiator_id = CAM_TARGET_WILDCARD;
2881 inot->initiator_id = lp->handle;
2883 inot->seq_id = notify->nt_tagval;
2884 inot->tag_id = notify->nt_tagval >> 32;
2886 switch (notify->nt_ncode) {
2888 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2889 inot->arg = MSG_ABORT_TASK;
2891 case NT_ABORT_TASK_SET:
2892 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2893 inot->arg = MSG_ABORT_TASK_SET;
2896 inot->arg = MSG_CLEAR_ACA;
2898 case NT_CLEAR_TASK_SET:
2899 inot->arg = MSG_CLEAR_TASK_SET;
2902 inot->arg = MSG_LOGICAL_UNIT_RESET;
2904 case NT_TARGET_RESET:
2905 inot->arg = MSG_TARGET_RESET;
2908 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
2912 ntp = isp_get_ntpd(isp, tptr);
2914 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2917 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2918 if (notify->nt_lreserved) {
2919 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2920 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2922 ntp->rd.seq_id = notify->nt_tagval;
2923 ntp->rd.tag_id = notify->nt_tagval >> 32;
2926 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2927 rls_lun_statep(isp, tptr);
2928 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2929 inot->ccb_h.status = CAM_MESSAGE_RECV;
2930 xpt_done((union ccb *)inot);
2934 rls_lun_statep(isp, tptr);
2936 if (notify->nt_need_ack && notify->nt_lreserved) {
2937 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2938 (void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
2940 (void) isp_notify_ack(isp, notify->nt_lreserved);
2946 * Find the associated private data and mark it as dead so
2947 * we don't try to work on it any further.
2950 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
2953 atio_private_data_t *atp;
2955 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
2957 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
2959 ccb->ccb_h.status = CAM_REQ_INVALID;
2964 atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
2966 ccb->ccb_h.status = CAM_REQ_INVALID;
2970 ccb->ccb_h.status = CAM_REQ_CMP;
2974 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
2976 atio_private_data_t *atp;
2977 inot_private_data_t *restart_queue = tptr->restart_queue;
2980 * First, clean any commands pending restart
2982 tptr->restart_queue = NULL;
2983 while (restart_queue) {
2984 uint32_t this_tag_id;
2985 inot_private_data_t *ntp = restart_queue;
2987 restart_queue = ntp->rd.nt.nt_hba;
2990 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
2992 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
2994 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2995 isp_put_ntpd(isp, tptr, ntp);
2997 ntp->rd.nt.nt_hba = tptr->restart_queue;
2998 tptr->restart_queue = ntp;
3003 * Now mark other ones dead as well.
3005 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3006 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3013 #ifdef ISP_INTERNAL_TARGET
3014 // #define ISP_FORCE_TIMEOUT 1
3015 // #define ISP_TEST_WWNS 1
3016 // #define ISP_TEST_SEPARATE_STATUS 1
3018 #define ccb_data_offset ppriv_field0
3019 #define ccb_atio ppriv_ptr1
3020 #define ccb_inot ppriv_ptr1
3022 #define MAX_ISP_TARG_TRANSFER (2 << 20)
3023 #define NISP_TARG_CMDS 1024
3024 #define NISP_TARG_NOTIFIES 1024
3025 #define DISK_SHIFT 9
3026 #define JUNK_SIZE 256
3029 #define VERIFY_10 0x2f
3032 TAILQ_HEAD(ccb_queue, ccb_hdr);
3033 extern u_int vm_kmem_size;
3035 static uint32_t disk_size;
3036 static uint8_t *disk_data = NULL;
3037 static uint8_t *junk_data;
3038 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3039 struct isptarg_softc {
3040 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3041 struct ccb_queue work_queue;
3042 struct ccb_queue rework_queue;
3043 struct ccb_queue running_queue;
3044 struct ccb_queue inot_queue;
3045 struct cam_periph *periph;
3046 struct cam_path *path;
3049 static periph_ctor_t isptargctor;
3050 static periph_dtor_t isptargdtor;
3051 static periph_start_t isptargstart;
3052 static periph_init_t isptarginit;
3053 static void isptarg_done(struct cam_periph *, union ccb *);
3054 static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3057 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3059 static struct periph_driver isptargdriver =
3061 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3070 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3072 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3074 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3075 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3076 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3077 ack->ccb_h.flags = 0;
3078 ack->ccb_h.retry_count = 0;
3079 ack->ccb_h.cbfcnp = isptarg_done;
3080 ack->ccb_h.timeout = 0;
3081 ack->ccb_h.ccb_inot = inot;
3082 ack->tag_id = inot->tag_id;
3083 ack->seq_id = inot->seq_id;
3084 ack->initiator_id = inot->initiator_id;
3089 isptargstart(struct cam_periph *periph, union ccb *iccb)
3091 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3092 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3093 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3094 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3095 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3098 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3099 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3100 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3101 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3102 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3105 int i, more = 0, last;
3106 struct isptarg_softc *softc = periph->softc;
3107 struct ccb_scsiio *csio;
3108 lun_id_t return_lun;
3109 struct ccb_accept_tio *atio;
3110 uint8_t *cdb, *ptr, status;
3112 uint32_t data_len, flags;
3113 struct ccb_hdr *ccbh;
3115 KKASSERT(lockstatus(periph->sim->mtx, curthread) != 0);
3116 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3117 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3119 * Check for immediate notifies first
3121 ccbh = TAILQ_FIRST(&softc->inot_queue);
3123 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3124 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3125 xpt_schedule(periph, 1);
3127 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3132 * Check the rework (continuation) work queue first.
3134 ccbh = TAILQ_FIRST(&softc->rework_queue);
3136 atio = (struct ccb_accept_tio *)ccbh;
3137 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3138 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3140 ccbh = TAILQ_FIRST(&softc->work_queue);
3142 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3143 xpt_release_ccb(iccb);
3146 atio = (struct ccb_accept_tio *)ccbh;
3147 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3148 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3149 atio->ccb_h.ccb_data_offset = 0;
3152 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3159 status = SCSI_STATUS_OK;
3160 flags = CAM_SEND_STATUS;
3161 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3162 cdb = atio->cdb_io.cdb_bytes;
3163 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
3164 cdb[0], atio->ccb_h.ccb_data_offset);
3166 return_lun = XS_LUN(atio);
3167 if (return_lun != 0) {
3168 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3169 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3170 status = SCSI_STATUS_CHECK_COND;
3171 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3172 atio->sense_data.add_sense_code = 0x25;
3173 atio->sense_data.add_sense_code_qual = 0x0;
3174 atio->sense_len = sizeof (atio->sense_data);
3176 return_lun = CAM_LUN_WILDCARD;
3181 flags |= CAM_DIR_IN;
3182 data_len = sizeof (atio->sense_data);
3183 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3184 memset(junk_data+1, 0, data_len-1);
3185 if (data_len > cdb[4]) {
3189 data_ptr = junk_data;
3196 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3197 status = SCSI_STATUS_CHECK_COND;
3198 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3199 atio->sense_data.add_sense_code = 0x5;
3200 atio->sense_data.add_sense_code_qual = 0x24;
3201 atio->sense_len = sizeof (atio->sense_data);
3203 #ifdef ISP_FORCE_TIMEOUT
3208 xpt_schedule(periph, 1);
3215 #ifdef ISP_TEST_SEPARATE_STATUS
3216 if (last && data_len) {
3221 flags &= ~CAM_SEND_STATUS;
3224 atio->ccb_h.ccb_data_offset += data_len;
3225 flags |= CAM_DIR_IN;
3227 flags |= CAM_DIR_NONE;
3235 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3236 status = SCSI_STATUS_CHECK_COND;
3237 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3238 atio->sense_data.add_sense_code = 0x5;
3239 atio->sense_data.add_sense_code_qual = 0x24;
3240 atio->sense_len = sizeof (atio->sense_data);
3242 #ifdef ISP_FORCE_TIMEOUT
3247 xpt_schedule(periph, 1);
3254 #ifdef ISP_TEST_SEPARATE_STATUS
3255 if (last && data_len) {
3260 flags &= ~CAM_SEND_STATUS;
3263 atio->ccb_h.ccb_data_offset += data_len;
3264 flags |= CAM_DIR_OUT;
3266 flags |= CAM_DIR_NONE;
3271 flags |= CAM_DIR_IN;
3272 if (cdb[1] || cdb[2] || cdb[3]) {
3273 status = SCSI_STATUS_CHECK_COND;
3274 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3275 atio->sense_data.add_sense_code = 0x5;
3276 atio->sense_data.add_sense_code_qual = 0x20;
3277 atio->sense_len = sizeof (atio->sense_data);
3280 data_len = sizeof (iqd);
3281 if (data_len > cdb[4]) {
3285 if (XS_LUN(iccb) != 0) {
3286 memcpy(junk_data, niliqd, sizeof (iqd));
3288 memcpy(junk_data, iqd, sizeof (iqd));
3290 data_ptr = junk_data;
3293 case TEST_UNIT_READY:
3294 flags |= CAM_DIR_NONE;
3297 status = SCSI_STATUS_CHECK_COND;
3298 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3299 atio->sense_data.add_sense_code = 0x28;
3300 atio->sense_data.add_sense_code_qual = 0x0;
3301 atio->sense_len = sizeof (atio->sense_data);
3304 case SYNCHRONIZE_CACHE:
3309 flags |= CAM_DIR_NONE;
3313 flags |= CAM_DIR_IN;
3314 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3315 status = SCSI_STATUS_CHECK_COND;
3316 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3317 atio->sense_data.add_sense_code = 0x5;
3318 atio->sense_data.add_sense_code_qual = 0x24;
3319 atio->sense_len = sizeof (atio->sense_data);
3322 if (cdb[8] & 0x1) { /* PMI */
3323 junk_data[0] = 0xff;
3324 junk_data[1] = 0xff;
3325 junk_data[2] = 0xff;
3326 junk_data[3] = 0xff;
3328 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3329 if (last_blk < 0xffffffffULL) {
3330 junk_data[0] = (last_blk >> 24) & 0xff;
3331 junk_data[1] = (last_blk >> 16) & 0xff;
3332 junk_data[2] = (last_blk >> 8) & 0xff;
3333 junk_data[3] = (last_blk) & 0xff;
3335 junk_data[0] = 0xff;
3336 junk_data[1] = 0xff;
3337 junk_data[2] = 0xff;
3338 junk_data[3] = 0xff;
3341 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3342 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3343 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3344 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3345 data_ptr = junk_data;
3349 flags |= CAM_DIR_IN;
3350 memset(junk_data, 0, JUNK_SIZE);
3351 junk_data[0] = (1 << 3) >> 24;
3352 junk_data[1] = (1 << 3) >> 16;
3353 junk_data[2] = (1 << 3) >> 8;
3354 junk_data[3] = (1 << 3);
3356 for (i = 0; i < 1; i++) {
3357 ptr = &junk_data[8 + (1 << 3)];
3359 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3363 data_ptr = junk_data;
3364 data_len = (ptr + 8) - junk_data;
3368 flags |= CAM_DIR_NONE;
3369 status = SCSI_STATUS_CHECK_COND;
3370 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3371 atio->sense_data.add_sense_code = 0x5;
3372 atio->sense_data.add_sense_code_qual = 0x20;
3373 atio->sense_len = sizeof (atio->sense_data);
3378 * If we are done with the transaction, tell the
3379 * controller to send status and perform a CMD_CMPLT.
3380 * If we have associated sense data, see if we can
3383 if (status == SCSI_STATUS_CHECK_COND) {
3384 flags |= CAM_SEND_SENSE;
3385 csio->sense_len = atio->sense_len;
3386 csio->sense_data = atio->sense_data;
3387 flags &= ~CAM_DIR_MASK;
3391 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3392 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3393 iccb->ccb_h.target_lun = return_lun;
3394 iccb->ccb_h.ccb_atio = atio;
3397 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3398 cam_release_devq(periph->path, 0, 0, 0, 0);
3399 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3402 xpt_schedule(periph, 1);
3407 isptargctor(struct cam_periph *periph, void *arg)
3409 struct isptarg_softc *softc;
3411 softc = (struct isptarg_softc *)arg;
3412 periph->softc = softc;
3413 softc->periph = periph;
3414 softc->path = periph->path;
3415 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3416 return (CAM_REQ_CMP);
3420 isptargdtor(struct cam_periph *periph)
3422 struct isptarg_softc *softc;
3423 softc = (struct isptarg_softc *)periph->softc;
3424 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3425 softc->periph = NULL;
3427 periph->softc = NULL;
3431 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3433 struct isptarg_softc *softc;
3435 struct ccb_accept_tio *atio;
3436 struct ccb_immediate_notify *inot;
3439 softc = (struct isptarg_softc *)periph->softc;
3441 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3443 switch (ccb->ccb_h.func_code) {
3444 case XPT_ACCEPT_TARGET_IO:
3445 atio = (struct ccb_accept_tio *) ccb;
3446 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3447 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3448 xpt_schedule(periph, 1);
3450 case XPT_IMMEDIATE_NOTIFY:
3451 inot = (struct ccb_immediate_notify *) ccb;
3452 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
3453 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3454 xpt_schedule(periph, 1);
3456 case XPT_CONT_TARGET_IO:
3457 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3458 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3459 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3461 atio = ccb->ccb_h.ccb_atio;
3462 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3463 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3464 xpt_action((union ccb *)atio);
3465 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3466 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3467 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3468 xpt_schedule(periph, 1);
3470 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3471 xpt_action((union ccb *)atio);
3473 xpt_release_ccb(ccb);
3475 case XPT_NOTIFY_ACKNOWLEDGE:
3476 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3477 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3478 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3480 inot = ccb->ccb_h.ccb_inot;
3481 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
3482 xpt_release_ccb(ccb);
3483 xpt_action((union ccb *)inot);
3486 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3492 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3494 struct ac_contract *acp = arg;
3495 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3497 if (code != AC_CONTRACT) {
3500 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3504 isp_target_thread(ispsoftc_t *isp, int chan)
3506 union ccb *ccb = NULL;
3510 struct isptarg_softc *softc = NULL;
3511 struct cam_periph *periph = NULL, *wperiph = NULL;
3512 struct cam_path *path, *wpath;
3513 struct cam_sim *sim;
3515 if (disk_data == NULL) {
3516 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3517 if (disk_size < (50 << 20)) {
3518 disk_size = 50 << 20;
3520 disk_data = kmalloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3521 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3523 junk_data = kmalloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3526 softc = kmalloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3527 TAILQ_INIT(&softc->work_queue);
3528 TAILQ_INIT(&softc->rework_queue);
3529 TAILQ_INIT(&softc->running_queue);
3530 TAILQ_INIT(&softc->inot_queue);
3533 periphdriver_register(&isptargdriver);
3534 ISP_GET_PC(isp, chan, sim, sim);
3535 ISP_GET_PC(isp, chan, path, path);
3536 status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3537 if (status != CAM_REQ_CMP) {
3538 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3541 status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3542 if (status != CAM_REQ_CMP) {
3543 xpt_free_path(wpath);
3544 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3548 ccb = xpt_alloc_ccb();
3551 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3552 if (status != CAM_REQ_CMP) {
3554 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3557 wperiph = cam_periph_find(wpath, "isptarg");
3558 if (wperiph == NULL) {
3560 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3564 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3565 if (status != CAM_REQ_CMP) {
3567 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3571 periph = cam_periph_find(path, "isptarg");
3572 if (periph == NULL) {
3574 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3578 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3579 if (status != CAM_REQ_CMP) {
3581 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3587 ccb = xpt_alloc_ccb();
3590 * Make sure role is none.
3592 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3593 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3594 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3595 #ifdef ISP_TEST_WWNS
3596 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3597 ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3598 ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3600 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3610 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3611 ccb->ccb_h.func_code = XPT_EN_LUN;
3612 ccb->cel.enable = 1;
3616 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3618 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3622 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3623 ccb->ccb_h.func_code = XPT_EN_LUN;
3624 ccb->cel.enable = 1;
3628 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3630 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3638 ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3639 for (i = 0; i < 4; i++) {
3640 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3641 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3642 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3643 ccb->ccb_h.cbfcnp = isptarg_done;
3648 for (i = 0; i < NISP_TARG_CMDS; i++) {
3649 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3650 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3651 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3652 ccb->ccb_h.cbfcnp = isptarg_done;
3657 for (i = 0; i < 4; i++) {
3658 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3659 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3660 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3661 ccb->ccb_h.cbfcnp = isptarg_done;
3666 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3667 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3668 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3669 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3670 ccb->ccb_h.cbfcnp = isptarg_done;
3677 * Now turn it all back on
3679 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3680 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3681 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3682 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3688 * Okay, while things are still active, sleep...
3692 ISP_GET_PC(isp, chan, proc_active, i);
3696 lksleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3702 cam_periph_invalidate(wperiph);
3705 cam_periph_invalidate(periph);
3708 kfree(junk_data, M_ISPTARG);
3711 kfree(disk_data, M_ISPTARG);
3714 kfree(softc, M_ISPTARG);
3716 xpt_free_path(path);
3717 xpt_free_path(wpath);
3721 isp_target_thread_pi(void *arg)
3723 struct isp_spi *pi = arg;
3724 isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3728 isp_target_thread_fc(void *arg)
3730 struct isp_fc *fc = arg;
3731 isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3735 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3737 uint32_t cnt, curcnt;
3743 cnt = (((uint32_t)cdb[10]) << 24) |
3744 (((uint32_t)cdb[11]) << 16) |
3745 (((uint32_t)cdb[12]) << 8) |
3746 ((uint32_t)cdb[13]);
3748 lba = (((uint64_t)cdb[2]) << 56) |
3749 (((uint64_t)cdb[3]) << 48) |
3750 (((uint64_t)cdb[4]) << 40) |
3751 (((uint64_t)cdb[5]) << 32) |
3752 (((uint64_t)cdb[6]) << 24) |
3753 (((uint64_t)cdb[7]) << 16) |
3754 (((uint64_t)cdb[8]) << 8) |
3759 cnt = (((uint32_t)cdb[6]) << 16) |
3760 (((uint32_t)cdb[7]) << 8) |
3761 ((u_int32_t)cdb[8]);
3763 lba = (((uint32_t)cdb[2]) << 24) |
3764 (((uint32_t)cdb[3]) << 16) |
3765 (((uint32_t)cdb[4]) << 8) |
3770 cnt = (((uint32_t)cdb[7]) << 8) |
3771 ((u_int32_t)cdb[8]);
3773 lba = (((uint32_t)cdb[2]) << 24) |
3774 (((uint32_t)cdb[3]) << 16) |
3775 (((uint32_t)cdb[4]) << 8) |
3784 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
3785 (((uint32_t)cdb[2]) << 8) |
3795 if (offset == cnt) {
3800 if (lba + cnt > dl) {
3805 curcnt = MAX_ISP_TARG_TRANSFER;
3806 if (offset + curcnt >= cnt) {
3807 curcnt = cnt - offset;
3813 *kp = &dp[lba + offset];
3821 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3823 struct cam_sim *sim;
3827 sim = (struct cam_sim *)cbarg;
3828 isp = (ispsoftc_t *) cam_sim_softc(sim);
3829 bus = cam_sim_bus(sim);
3830 tgt = xpt_path_target_id(path);
3833 case AC_LOST_DEVICE:
3835 uint16_t oflags, nflags;
3836 sdparam *sdp = SDPARAM(isp, bus);
3839 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3840 #ifndef ISP_TARGET_MODE
3841 nflags &= DPARM_SAFE_DFLT;
3842 if (isp->isp_loaded_fw) {
3843 nflags |= DPARM_NARROW | DPARM_ASYNC;
3846 nflags = DPARM_DEFAULT;
3848 oflags = sdp->isp_devparam[tgt].goal_flags;
3849 sdp->isp_devparam[tgt].goal_flags = nflags;
3850 sdp->isp_devparam[tgt].dev_update = 1;
3852 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3853 sdp->isp_devparam[tgt].goal_flags = oflags;
3858 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3864 isp_poll(struct cam_sim *sim)
3866 ispsoftc_t *isp = cam_sim_softc(sim);
3868 uint16_t sema, mbox;
3870 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3871 isp_intr(isp, isr, sema, mbox);
3877 isp_watchdog(void *arg)
3879 struct ccb_scsiio *xs = arg;
3881 uint32_t ohandle = ISP_HANDLE_FREE, handle;
3886 handle = isp_find_handle(isp, xs);
3888 if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3889 isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3890 callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3898 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3900 if (handle != ISP_HANDLE_FREE) {
3902 uint16_t sema, mbox;
3903 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3904 isp_intr(isp, isr, sema, mbox);
3907 handle = isp_find_handle(isp, xs);
3909 if (handle != ISP_HANDLE_FREE) {
3911 * Try and make sure the command is really dead before
3912 * we release the handle (and DMA resources) for reuse.
3914 * If we are successful in aborting the command then
3915 * we're done here because we'll get the command returned
3918 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3924 * Note that after calling the above, the command may in
3925 * fact have been completed.
3927 xs = isp_find_xs(isp, handle);
3930 * If the command no longer exists, then we won't
3931 * be able to find the xs again with this handle.
3939 * After this point, the command is really dead.
3941 if (XS_XFRLEN(xs)) {
3942 ISP_DMAFREE(isp, xs, handle);
3944 isp_destroy_handle(isp, handle);
3945 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3946 XS_SETERR(xs, CAM_CMD_TIMEOUT);
3947 isp_prt_endcmd(isp, xs);
3950 if (ohandle != ISP_HANDLE_FREE) {
3951 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3953 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3960 isp_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
3962 if (ccb->ccb_h.status != CAM_REQ_CMP)
3963 kprintf("cam_scan_callback: failure status = %x\n",
3966 xpt_free_path(ccb->ccb_h.path);
3971 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3974 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3976 if (isp_autoconfig == 0) {
3981 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3983 ccb = xpt_alloc_ccb();
3985 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3988 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3989 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3993 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
3994 ccb->ccb_h.func_code = XPT_SCAN_BUS;
3995 ccb->ccb_h.cbfcnp = isp_bus_scan_cb;
3996 ccb->crcn.flags = CAM_FLAG_NONE;
4001 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
4003 struct cam_path *tp;
4004 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4006 if (isp_autoconfig == 0) {
4009 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4010 xpt_async(AC_LOST_DEVICE, tp, NULL);
4016 * Gone Device Timer Function- when we have decided that a device has gone
4017 * away, we wait a specific period of time prior to telling the OS it has
4020 * This timer function fires once a second and then scans the port database
4021 * for devices that are marked dead but still have a virtual target assigned.
4022 * We decrement a counter for that port database entry, and when it hits zero,
4023 * we tell the OS the device has gone away.
4028 struct isp_fc *fc = arg;
4031 taskqueue_enqueue(taskqueue_swi, &fc->gtask);
4032 ISP_UNLOCK(fc->isp);
4036 isp_gdt_task(void *arg, int pending)
4038 struct isp_fc *fc = arg;
4039 ispsoftc_t *isp = fc->isp;
4040 int chan = fc - isp->isp_osinfo.pc.fc;
4042 int dbidx, tgt, more_to_do = 0;
4045 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4046 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4047 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4049 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4052 if (lp->dev_map_idx == 0 || lp->target_mode) {
4055 if (lp->gone_timer != 0) {
4056 isp_prt(isp, ISP_LOGSANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
4057 lp->gone_timer -= 1;
4061 tgt = lp->dev_map_idx - 1;
4062 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4063 lp->dev_map_idx = 0;
4064 lp->state = FC_PORTDB_STATE_NIL;
4065 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4066 isp_make_gone(isp, chan, tgt);
4070 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4072 callout_deactivate(&fc->gdt);
4073 isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_second);
4080 * Loop Down Timer Function- when loop goes down, a timer is started and
4081 * and after it expires we come here and take all probational devices that
4082 * the OS knows about and the tell the OS that they've gone away.
4084 * We don't clear the devices out of our port database because, when loop
4085 * come back up, we have to do some actual cleanup with the chip at that
4086 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4091 struct isp_fc *fc = arg;
4094 taskqueue_enqueue(taskqueue_swi, &fc->ltask);
4095 ISP_UNLOCK(fc->isp);
4099 isp_ldt_task(void *arg, int pending)
4101 struct isp_fc *fc = arg;
4102 ispsoftc_t *isp = fc->isp;
4103 int chan = fc - isp->isp_osinfo.pc.fc;
4108 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_second);
4109 callout_deactivate(&fc->ldt);
4112 * Notify to the OS all targets who we now consider have departed.
4114 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4115 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4117 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4120 if (lp->dev_map_idx == 0 || lp->target_mode) {
4125 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4129 for (i = 0; i < isp->isp_maxcmds; i++) {
4130 struct ccb_scsiio *xs;
4132 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4135 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4138 if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4141 isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4142 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4146 * Mark that we've announced that this device is gone....
4151 * but *don't* change the state of the entry. Just clear
4152 * any target id stuff and announce to CAM that the
4153 * device is gone. This way any necessary PLOGO stuff
4154 * will happen when loop comes back up.
4157 tgt = lp->dev_map_idx - 1;
4158 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4159 lp->dev_map_idx = 0;
4160 lp->state = FC_PORTDB_STATE_NIL;
4161 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4162 isp_make_gone(isp, chan, tgt);
4165 if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4166 isp_unfreeze_loopdown(isp, chan);
4169 * The loop down timer has expired. Wake up the kthread
4170 * to notice that fact (or make it false).
4173 fc->loop_down_time = fc->loop_down_limit+1;
4179 isp_kthread(void *arg)
4181 struct isp_fc *fc = arg;
4182 ispsoftc_t *isp = fc->isp;
4183 int chan = fc - isp->isp_osinfo.pc.fc;
4186 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4191 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4192 lb = isp_fc_runstate(isp, chan, 250000);
4195 * Our action is different based upon whether we're supporting
4196 * Initiator mode or not. If we are, we might freeze the simq
4197 * when loop is down and set all sorts of different delays to
4200 * If not, we simply just wait for loop to come up.
4202 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4204 * Increment loop down time by the last sleep interval
4206 fc->loop_down_time += slp;
4209 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4211 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4215 * If we've never seen loop up and we've waited longer
4216 * than quickboot time, or we've seen loop up but we've
4217 * waited longer than loop_down_limit, give up and go
4218 * to sleep until loop comes up.
4220 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4221 lim = isp_quickboot_time;
4223 lim = fc->loop_down_limit;
4225 if (fc->loop_down_time >= lim) {
4226 isp_freeze_loopdown(isp, chan, "loop limit hit");
4228 } else if (fc->loop_down_time < 10) {
4230 } else if (fc->loop_down_time < 30) {
4232 } else if (fc->loop_down_time < 60) {
4234 } else if (fc->loop_down_time < 120) {
4241 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4242 fc->loop_down_time += slp;
4245 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4246 fc->loop_down_time = 0;
4252 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4253 * now so that CAM can start sending us commands.
4255 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4256 * or kill the commands, as appropriate.
4259 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4260 isp_unfreeze_loopdown(isp, chan);
4263 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4265 lksleep(fc, &isp->isp_osinfo.lock, 0, "ispf", slp * hz);
4268 * If slp is zero, we're waking up for the first time after
4269 * things have been okay. In this case, we set a deferral state
4270 * for all commands and delay hysteresis seconds before starting
4271 * the FC state evaluation. This gives the loop/fabric a chance
4274 if (slp == 0 && fc->hysteresis) {
4275 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4276 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4277 tsleep(isp_kthread, 0, "ispt", fc->hysteresis * hz);
4278 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4281 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4285 isp_action(struct cam_sim *sim, union ccb *ccb)
4287 int bus, tgt, ts, error, lim;
4289 struct ccb_trans_settings *cts;
4291 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4293 isp = (ispsoftc_t *)cam_sim_softc(sim);
4294 KKASSERT(lockstatus(&isp->isp_lock, curthread) != 0);
4296 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4298 if (isp->isp_state != ISP_INITSTATE) {
4300 * Lie. Say it was a selection timeout.
4302 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4303 xpt_freeze_devq(ccb->ccb_h.path, 1);
4307 isp->isp_state = ISP_RUNSTATE;
4309 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4310 ISP_PCMD(ccb) = NULL;
4312 switch (ccb->ccb_h.func_code) {
4313 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4314 bus = XS_CHANNEL(ccb);
4316 * Do a couple of preliminary checks...
4318 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4319 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4320 ccb->ccb_h.status = CAM_REQ_INVALID;
4326 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4327 xpt_print(ccb->ccb_h.path, "invalid target\n");
4328 ccb->ccb_h.status = CAM_PATH_INVALID;
4329 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4330 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4331 ccb->ccb_h.status = CAM_PATH_INVALID;
4333 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4338 ccb->csio.scsi_status = SCSI_STATUS_OK;
4339 if (isp_get_pcmd(isp, ccb)) {
4340 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4341 cam_freeze_devq(ccb->ccb_h.path);
4342 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4346 error = isp_start((XS_T *) ccb);
4349 XS_CMD_S_CLEAR(ccb);
4350 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4351 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4354 ts = ccb->ccb_h.timeout;
4355 if (ts == CAM_TIME_DEFAULT) {
4358 ts = isp_mstohz(ts);
4360 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4364 * We get this result for FC devices if the loop state isn't ready yet
4365 * or if the device in question has gone zombie on us.
4367 * If we've never seen Loop UP at all, we requeue this request and wait
4368 * for the initial loop up delay to expire.
4370 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4371 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4372 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4373 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_second);
4375 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4377 ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4378 xpt_freeze_devq(ccb->ccb_h.path, 1);
4379 isp_free_pcmd(isp, ccb);
4383 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4384 cam_freeze_devq(ccb->ccb_h.path);
4385 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4386 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4387 isp_free_pcmd(isp, ccb);
4391 isp_free_pcmd(isp, ccb);
4392 cam_freeze_devq(ccb->ccb_h.path);
4393 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4394 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4398 isp_done((struct ccb_scsiio *) ccb);
4401 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4402 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4403 isp_free_pcmd(isp, ccb);
4408 #ifdef ISP_TARGET_MODE
4409 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
4410 if (ccb->cel.enable) {
4411 isp_enable_lun(isp, ccb);
4413 isp_disable_lun(isp, ccb);
4416 case XPT_IMMED_NOTIFY:
4417 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
4418 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
4420 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4422 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4428 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4429 str = "XPT_IMMEDIATE_NOTIFY";
4430 tag = ccb->cin1.seq_id;
4432 tag = ccb->atio.tag_id;
4433 str = "XPT_ACCEPT_TARGET_IO";
4435 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4436 dump_tstates(isp, XS_CHANNEL(ccb));
4437 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4440 ccb->ccb_h.sim_priv.entries[0].field = 0;
4441 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4442 ccb->ccb_h.flags = 0;
4444 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4445 if (ccb->atio.tag_id) {
4446 atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4448 isp_put_atpd(isp, tptr, atp);
4452 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4453 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4454 ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
4455 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4456 if (ccb->cin1.tag_id) {
4457 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4459 isp_put_ntpd(isp, tptr, ntp);
4463 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4464 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4465 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4466 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4468 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4469 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4470 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4472 rls_lun_statep(isp, tptr);
4473 ccb->ccb_h.status = CAM_REQ_INPROG;
4476 case XPT_NOTIFY_ACK:
4477 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4479 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
4482 inot_private_data_t *ntp;
4485 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4486 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4489 * All the relevant path information is in the associated immediate notify
4491 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);
4492 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4494 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__,
4495 ccb->cna2.tag_id, ccb->cna2.seq_id);
4496 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4500 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4501 rls_lun_statep(isp, tptr);
4502 cam_freeze_devq(ccb->ccb_h.path);
4503 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4504 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4507 isp_put_ntpd(isp, tptr, ntp);
4508 rls_lun_statep(isp, tptr);
4509 ccb->ccb_h.status = CAM_REQ_CMP;
4510 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);
4514 case XPT_CONT_TARGET_IO:
4515 isp_target_start_ctio(isp, ccb);
4518 case XPT_RESET_DEV: /* BDR the specified SCSI device */
4520 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4521 tgt = ccb->ccb_h.target_id;
4524 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4526 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4528 ccb->ccb_h.status = CAM_REQ_CMP;
4532 case XPT_ABORT: /* Abort the specified CCB */
4534 union ccb *accb = ccb->cab.abort_ccb;
4535 switch (accb->ccb_h.func_code) {
4536 #ifdef ISP_TARGET_MODE
4537 case XPT_ACCEPT_TARGET_IO:
4538 isp_target_mark_aborted(isp, accb);
4542 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4544 ccb->ccb_h.status = CAM_UA_ABORT;
4546 ccb->ccb_h.status = CAM_REQ_CMP;
4550 ccb->ccb_h.status = CAM_REQ_INVALID;
4554 * This is not a queued CCB, so the caller expects it to be
4555 * complete when control is returned.
4559 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
4560 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
4562 if (!IS_CURRENT_SETTINGS(cts)) {
4563 ccb->ccb_h.status = CAM_REQ_INVALID;
4567 tgt = cts->ccb_h.target_id;
4568 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4570 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4571 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4572 sdparam *sdp = SDPARAM(isp, bus);
4575 if (spi->valid == 0 && scsi->valid == 0) {
4576 ccb->ccb_h.status = CAM_REQ_CMP;
4582 * We always update (internally) from goal_flags
4583 * so any request to change settings just gets
4584 * vectored to that location.
4586 dptr = &sdp->isp_devparam[tgt].goal_flags;
4588 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4589 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4590 *dptr |= DPARM_DISC;
4592 *dptr &= ~DPARM_DISC;
4595 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4596 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4597 *dptr |= DPARM_TQING;
4599 *dptr &= ~DPARM_TQING;
4602 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4603 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4604 *dptr |= DPARM_WIDE;
4606 *dptr &= ~DPARM_WIDE;
4612 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4613 *dptr |= DPARM_SYNC;
4615 * XXX: CHECK FOR LEGALITY
4617 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4618 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4620 *dptr &= ~DPARM_SYNC;
4622 isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
4623 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4624 sdp->isp_devparam[tgt].dev_update = 1;
4627 ccb->ccb_h.status = CAM_REQ_CMP;
4630 case XPT_GET_TRAN_SETTINGS:
4632 tgt = cts->ccb_h.target_id;
4633 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4635 fcparam *fcp = FCPARAM(isp, bus);
4636 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4637 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4638 unsigned int hdlidx;
4640 cts->protocol = PROTO_SCSI;
4641 cts->protocol_version = SCSI_REV_2;
4642 cts->transport = XPORT_FC;
4643 cts->transport_version = 0;
4645 scsi->valid = CTS_SCSI_VALID_TQ;
4646 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4647 fc->valid = CTS_FC_VALID_SPEED;
4648 fc->bitrate = 100000;
4649 fc->bitrate *= fcp->isp_gbspeed;
4650 hdlidx = fcp->isp_dev_map[tgt] - 1;
4651 if (hdlidx < MAX_FC_TARG) {
4652 fcportdb_t *lp = &fcp->portdb[hdlidx];
4653 fc->wwnn = lp->node_wwn;
4654 fc->wwpn = lp->port_wwn;
4655 fc->port = lp->portid;
4656 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4659 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4660 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4661 sdparam *sdp = SDPARAM(isp, bus);
4662 uint16_t dval, pval, oval;
4664 if (IS_CURRENT_SETTINGS(cts)) {
4665 sdp->isp_devparam[tgt].dev_refresh = 1;
4667 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4668 dval = sdp->isp_devparam[tgt].actv_flags;
4669 oval = sdp->isp_devparam[tgt].actv_offset;
4670 pval = sdp->isp_devparam[tgt].actv_period;
4672 dval = sdp->isp_devparam[tgt].nvrm_flags;
4673 oval = sdp->isp_devparam[tgt].nvrm_offset;
4674 pval = sdp->isp_devparam[tgt].nvrm_period;
4677 cts->protocol = PROTO_SCSI;
4678 cts->protocol_version = SCSI_REV_2;
4679 cts->transport = XPORT_SPI;
4680 cts->transport_version = 2;
4686 if (dval & DPARM_DISC) {
4687 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4689 if ((dval & DPARM_SYNC) && oval && pval) {
4690 spi->sync_offset = oval;
4691 spi->sync_period = pval;
4693 spi->sync_offset = 0;
4694 spi->sync_period = 0;
4696 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4697 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4698 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4699 if (dval & DPARM_WIDE) {
4700 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4702 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4704 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4705 scsi->valid = CTS_SCSI_VALID_TQ;
4706 if (dval & DPARM_TQING) {
4707 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4709 spi->valid |= CTS_SPI_VALID_DISC;
4711 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4712 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4714 ccb->ccb_h.status = CAM_REQ_CMP;
4718 case XPT_CALC_GEOMETRY:
4719 cam_calc_geometry(&ccb->ccg, 1);
4723 case XPT_RESET_BUS: /* Reset the specified bus */
4724 bus = cam_sim_bus(sim);
4725 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4727 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4732 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4735 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4737 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4739 ccb->ccb_h.status = CAM_REQ_CMP;
4743 case XPT_TERM_IO: /* Terminate the I/O process */
4744 ccb->ccb_h.status = CAM_REQ_INVALID;
4749 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
4751 struct ccb_sim_knob *kp = &ccb->knob;
4756 ccb->ccb_h.status = CAM_REQ_INVALID;
4761 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4762 fcp = FCPARAM(isp, bus);
4764 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4765 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4766 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4767 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4769 ccb->ccb_h.status = CAM_REQ_CMP;
4770 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4774 switch (kp->xport_specific.fc.role) {
4775 case KNOB_ROLE_NONE:
4776 if (fcp->role != ISP_ROLE_NONE) {
4778 newrole = ISP_ROLE_NONE;
4781 case KNOB_ROLE_TARGET:
4782 if (fcp->role != ISP_ROLE_TARGET) {
4784 newrole = ISP_ROLE_TARGET;
4787 case KNOB_ROLE_INITIATOR:
4788 if (fcp->role != ISP_ROLE_INITIATOR) {
4790 newrole = ISP_ROLE_INITIATOR;
4793 case KNOB_ROLE_BOTH:
4795 if (fcp->role != ISP_ROLE_BOTH) {
4797 newrole = ISP_ROLE_BOTH;
4801 * We don't really support dual role at present on FC cards.
4803 * We should, but a bunch of things are currently broken,
4804 * so don't allow it.
4806 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4807 ccb->ccb_h.status = CAM_REQ_INVALID;
4812 if (isp_fc_change_role(isp, bus, newrole) != 0) {
4813 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4814 #ifdef ISP_TARGET_MODE
4815 } else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4816 isp_enable_deferred_luns(isp, bus);
4824 case XPT_GET_SIM_KNOB: /* Set SIM knobs */
4826 struct ccb_sim_knob *kp = &ccb->knob;
4831 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4832 fcp = FCPARAM(isp, bus);
4834 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4835 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4836 switch (fcp->role) {
4838 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4840 case ISP_ROLE_TARGET:
4841 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4843 case ISP_ROLE_INITIATOR:
4844 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4847 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4850 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4851 ccb->ccb_h.status = CAM_REQ_CMP;
4853 ccb->ccb_h.status = CAM_REQ_INVALID;
4859 case XPT_PATH_INQ: /* Path routing inquiry */
4861 struct ccb_pathinq *cpi = &ccb->cpi;
4863 cpi->version_num = 1;
4864 #ifdef ISP_TARGET_MODE
4865 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4867 cpi->target_sprt = 0;
4869 cpi->hba_eng_cnt = 0;
4870 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4871 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4872 cpi->bus_id = cam_sim_bus(sim);
4873 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4875 fcparam *fcp = FCPARAM(isp, bus);
4877 cpi->hba_misc = PIM_NOBUSRESET;
4880 * Because our loop ID can shift from time to time,
4881 * make our initiator ID out of range of our bus.
4883 cpi->initiator_id = cpi->max_target + 1;
4886 * Set base transfer capabilities for Fibre Channel, for this HBA.
4889 cpi->base_transfer_speed = 8000000;
4890 } else if (IS_24XX(isp)) {
4891 cpi->base_transfer_speed = 4000000;
4892 } else if (IS_23XX(isp)) {
4893 cpi->base_transfer_speed = 2000000;
4895 cpi->base_transfer_speed = 1000000;
4897 cpi->hba_inquiry = PI_TAG_ABLE;
4898 cpi->transport = XPORT_FC;
4899 cpi->transport_version = 0;
4900 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4901 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4902 cpi->xport_specific.fc.port = fcp->isp_portid;
4903 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4905 sdparam *sdp = SDPARAM(isp, bus);
4906 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4908 cpi->initiator_id = sdp->isp_initiator_id;
4909 cpi->base_transfer_speed = 3300;
4910 cpi->transport = XPORT_SPI;
4911 cpi->transport_version = 2;
4913 cpi->protocol = PROTO_SCSI;
4914 cpi->protocol_version = SCSI_REV_2;
4915 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4916 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4917 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4918 cpi->unit_number = cam_sim_unit(sim);
4919 cpi->ccb_h.status = CAM_REQ_CMP;
4924 ccb->ccb_h.status = CAM_REQ_INVALID;
4930 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4933 isp_done(XS_T *sccb)
4935 ispsoftc_t *isp = XS_ISP(sccb);
4939 XS_SETERR(sccb, CAM_REQ_CMP);
4941 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4942 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4943 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4944 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4946 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4950 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4951 status = sccb->ccb_h.status & CAM_STATUS_MASK;
4952 if (status != CAM_REQ_CMP) {
4953 if (status != CAM_SEL_TIMEOUT)
4954 isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
4955 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4956 sccb->ccb_h.status |= CAM_DEV_QFRZN;
4957 xpt_freeze_devq(sccb->ccb_h.path, 1);
4961 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4962 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4965 XS_CMD_S_DONE(sccb);
4966 if (XS_TACTIVE_P(sccb))
4967 callout_stop(&PISP_PCMD(sccb)->wdog);
4968 XS_CMD_S_CLEAR(sccb);
4969 isp_free_pcmd(isp, (union ccb *) sccb);
4970 xpt_done((union ccb *) sccb);
4974 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4977 static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
4978 static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4983 struct cam_path *tmppath;
4987 case ISPASYNC_NEW_TGT_PARAMS:
4989 struct ccb_trans_settings_scsi *scsi;
4990 struct ccb_trans_settings_spi *spi;
4993 struct ccb_trans_settings cts;
4995 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4997 __va_start(ap, cmd);
4998 bus = __va_arg(ap, int);
4999 tgt = __va_arg(ap, int);
5001 sdp = SDPARAM(isp, bus);
5003 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5004 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5007 flags = sdp->isp_devparam[tgt].actv_flags;
5008 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5009 cts.protocol = PROTO_SCSI;
5010 cts.transport = XPORT_SPI;
5012 scsi = &cts.proto_specific.scsi;
5013 spi = &cts.xport_specific.spi;
5015 if (flags & DPARM_TQING) {
5016 scsi->valid |= CTS_SCSI_VALID_TQ;
5017 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5020 if (flags & DPARM_DISC) {
5021 spi->valid |= CTS_SPI_VALID_DISC;
5022 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5024 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5025 if (flags & DPARM_WIDE) {
5026 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5028 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5030 if (flags & DPARM_SYNC) {
5031 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5032 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5033 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5034 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5036 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);
5037 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5038 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5039 xpt_free_path(tmppath);
5042 case ISPASYNC_BUS_RESET:
5044 __va_start(ap, cmd);
5045 bus = __va_arg(ap, int);
5047 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5049 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5051 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5057 msg = "LIP Received";
5060 case ISPASYNC_LOOP_RESET:
5065 case ISPASYNC_LOOP_DOWN:
5070 __va_start(ap, cmd);
5071 bus = __va_arg(ap, int);
5074 FCPARAM(isp, bus)->link_active = 0;
5076 fc = ISP_FC_PC(isp, bus);
5077 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5079 * We don't do any simq freezing if we are only in target mode
5081 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5083 isp_freeze_loopdown(isp, bus, msg);
5085 if (!callout_active(&fc->ldt)) {
5086 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5087 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_second);
5091 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5094 case ISPASYNC_LOOP_UP:
5095 __va_start(ap, cmd);
5096 bus = __va_arg(ap, int);
5098 fc = ISP_FC_PC(isp, bus);
5100 * Now we just note that Loop has come up. We don't
5101 * actually do anything because we're waiting for a
5102 * Change Notify before activating the FC cleanup
5103 * thread to look at the state of the loop again.
5105 FCPARAM(isp, bus)->link_active = 1;
5107 fc->loop_down_time = 0;
5108 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5110 case ISPASYNC_DEV_ARRIVED:
5111 __va_start(ap, cmd);
5112 bus = __va_arg(ap, int);
5113 lp = __va_arg(ap, fcportdb_t *);
5115 fc = ISP_FC_PC(isp, bus);
5118 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5119 int dbidx = lp - FCPARAM(isp, bus)->portdb;
5122 for (i = 0; i < MAX_FC_TARG; i++) {
5123 if (i >= FL_ID && i <= SNS_ID) {
5126 if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5130 if (i < MAX_FC_TARG) {
5131 FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5132 lp->dev_map_idx = i + 1;
5134 isp_prt(isp, ISP_LOGWARN, "out of target ids");
5135 isp_dump_portdb(isp, bus);
5138 if (lp->dev_map_idx) {
5139 tgt = lp->dev_map_idx - 1;
5140 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5141 isp_make_here(isp, bus, tgt);
5143 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5146 case ISPASYNC_DEV_CHANGED:
5147 __va_start(ap, cmd);
5148 bus = __va_arg(ap, int);
5149 lp = __va_arg(ap, fcportdb_t *);
5151 fc = ISP_FC_PC(isp, bus);
5154 if (isp_change_is_bad) {
5155 lp->state = FC_PORTDB_STATE_NIL;
5156 if (lp->dev_map_idx) {
5157 tgt = lp->dev_map_idx - 1;
5158 FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5159 lp->dev_map_idx = 0;
5160 isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5161 isp_make_gone(isp, bus, tgt);
5163 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5164 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5167 lp->portid = lp->new_portid;
5168 lp->roles = lp->new_roles;
5169 if (lp->dev_map_idx) {
5170 int t = lp->dev_map_idx - 1;
5171 FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5172 tgt = lp->dev_map_idx - 1;
5173 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5174 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5176 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5180 case ISPASYNC_DEV_STAYED:
5181 __va_start(ap, cmd);
5182 bus = __va_arg(ap, int);
5183 lp = __va_arg(ap, fcportdb_t *);
5185 if (lp->dev_map_idx) {
5186 tgt = lp->dev_map_idx - 1;
5187 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5188 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5190 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5191 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5194 case ISPASYNC_DEV_GONE:
5195 __va_start(ap, cmd);
5196 bus = __va_arg(ap, int);
5197 lp = __va_arg(ap, fcportdb_t *);
5199 fc = ISP_FC_PC(isp, bus);
5201 * If this has a virtual target and we haven't marked it
5202 * that we're going to have isp_gdt tell the OS it's gone,
5203 * set the isp_gdt timer running on it.
5205 * If it isn't marked that isp_gdt is going to get rid of it,
5206 * announce that it's gone.
5209 if (lp->dev_map_idx && lp->reserved == 0) {
5211 lp->state = FC_PORTDB_STATE_ZOMBIE;
5212 lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5213 if (fc->ready && !callout_active(&fc->gdt)) {
5214 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_second);
5215 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5217 tgt = lp->dev_map_idx - 1;
5218 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5219 } else if (lp->reserved == 0) {
5220 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5223 case ISPASYNC_CHANGE_NOTIFY:
5226 int evt, nphdl, nlstate, reason;
5228 __va_start(ap, cmd);
5229 bus = __va_arg(ap, int);
5230 evt = __va_arg(ap, int);
5231 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5232 nphdl = __va_arg(ap, int);
5233 nlstate = __va_arg(ap, int);
5234 reason = __va_arg(ap, int);
5237 nlstate = reason = 0;
5240 fc = ISP_FC_PC(isp, bus);
5242 if (evt == ISPASYNC_CHANGE_PDB) {
5243 msg = "Chan %d Port Database Changed";
5244 } else if (evt == ISPASYNC_CHANGE_SNS) {
5245 msg = "Chan %d Name Server Database Changed";
5247 msg = "Chan %d Other Change Notify";
5251 * If the loop down timer is running, cancel it.
5253 if (fc->ready && callout_active(&fc->ldt)) {
5254 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_second);
5255 callout_stop(&fc->ldt);
5257 isp_prt(isp, ISP_LOGINFO, msg, bus);
5258 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5259 isp_freeze_loopdown(isp, bus, msg);
5264 #ifdef ISP_TARGET_MODE
5265 case ISPASYNC_TARGET_NOTIFY:
5267 isp_notify_t *notify;
5268 __va_start(ap, cmd);
5269 notify = __va_arg(ap, isp_notify_t *);
5271 switch (notify->nt_ncode) {
5273 case NT_ABORT_TASK_SET:
5275 case NT_CLEAR_TASK_SET:
5277 case NT_TARGET_RESET:
5279 * These are task management functions.
5281 isp_handle_platform_target_tmf(isp, notify);
5288 * No action need be taken here.
5292 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5296 * This is device arrival/departure notification
5298 isp_handle_platform_target_notify_ack(isp, notify);
5302 struct ac_contract ac;
5303 struct ac_device_changed *fc;
5305 ac.contract_number = AC_CONTRACT_DEV_CHG;
5306 fc = (struct ac_device_changed *) ac.contract_data;
5307 fc->wwpn = notify->nt_wwn;
5308 fc->port = notify->nt_sid;
5309 fc->target = notify->nt_nphdl;
5311 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5316 struct ac_contract ac;
5317 struct ac_device_changed *fc;
5319 ac.contract_number = AC_CONTRACT_DEV_CHG;
5320 fc = (struct ac_device_changed *) ac.contract_data;
5321 fc->wwpn = notify->nt_wwn;
5322 fc->port = notify->nt_sid;
5323 fc->target = notify->nt_nphdl;
5325 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5329 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5330 isp_handle_platform_target_notify_ack(isp, notify);
5335 case ISPASYNC_TARGET_ACTION:
5339 __va_start(ap, cmd);
5340 hp = __va_arg(ap, isphdr_t *);
5342 switch (hp->rqs_entry_type) {
5344 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5346 case RQSTYPE_NOTIFY:
5348 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5349 } else if (IS_24XX(isp)) {
5350 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5352 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5357 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5359 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5363 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5369 isp_handle_platform_ctio(isp, hp);
5371 case RQSTYPE_ABTS_RCVD:
5373 abts_t *abts = (abts_t *)hp;
5374 isp_notify_t notify, *nt = ¬ify;
5380 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5381 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5382 ISP_MEMZERO(nt, sizeof (isp_notify_t));
5386 nt->nt_nphdl = abts->abts_nphdl;
5388 isp_find_chan_by_did(isp, did, &chan);
5389 if (chan == ISP_NOCHAN) {
5390 nt->nt_tgt = TGT_ANY;
5392 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5393 if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5394 nt->nt_wwn = lp->port_wwn;
5396 nt->nt_wwn = INI_ANY;
5400 * Try hard to find the lun for this command.
5402 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5404 nt->nt_lun = xpt_path_lun_id(tptr->owner);
5405 rls_lun_statep(isp, tptr);
5407 nt->nt_lun = LUN_ANY;
5409 nt->nt_need_ack = 1;
5410 nt->nt_tagval = abts->abts_rxid_task;
5411 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5412 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5413 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
5414 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5416 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
5417 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5419 nt->nt_channel = chan;
5420 nt->nt_ncode = NT_ABORT_TASK;
5421 nt->nt_lreserved = hp;
5422 isp_handle_platform_target_tmf(isp, nt);
5425 case RQSTYPE_ENABLE_LUN:
5426 case RQSTYPE_MODIFY_LUN:
5427 isp_ledone(isp, (lun_entry_t *) hp);
5433 case ISPASYNC_FW_CRASH:
5435 uint16_t mbox1, mbox6;
5436 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5437 if (IS_DUALBUS(isp)) {
5438 mbox6 = ISP_READ(isp, OUTMAILBOX6);
5442 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5443 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5444 isp->isp_osinfo.mbox_sleep_ok = 0;
5446 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5447 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5451 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5458 * Locks are held before coming here.
5461 isp_uninit(ispsoftc_t *isp)
5464 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5466 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5468 ISP_DISABLE_INTS(isp);
5472 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5473 * up from our platform default (defww{p|n}n) and morph them based upon
5476 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5477 * based upon channel.
5481 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5484 struct isp_fc *fc = ISP_FC_PC(isp, chan);
5487 * If we're asking for a active WWN, the default overrides get
5488 * returned, otherwise the NVRAM value is picked.
5490 * If we're asking for a default WWN, we just pick the default override.
5493 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5497 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5501 return (0x400000007F000009ull);
5503 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5508 * For channel zero just return what we have. For either ACTIVE or
5509 * DEFAULT cases, we depend on default override of NVRAM values for
5517 * For other channels, we are doing one of three things:
5519 * 1. If what we have now is non-zero, return it. Otherwise we morph
5520 * values from channel 0. 2. If we're here for a WWPN we synthesize
5521 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5522 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5529 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5531 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5534 if (((seed >> 60) & 0xf) == 2) {
5536 * The type 2 NAA fields for QLogic cards appear be laid out
5539 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5540 * port (1) or node (0) WWN distinguishor bit 48
5541 * physical port on dual-port chips (23XX/24XX)
5543 * This is somewhat nutty, particularly since bit 48 is
5544 * irrelevant as they assign separate serial numbers to
5545 * different physical ports anyway.
5547 * We'll stick our channel number plus one first into bits
5548 * 57..59 and thence into bits 52..55 which allows for 8 bits
5549 * of channel which is comfortably more than our maximum
5552 seed &= ~0x0FF0000000000000ULL;
5554 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5555 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5564 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5570 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5573 ksprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5575 __va_start(ap, fmt);
5576 kvsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
5578 kprintf("%s\n", lbuf);
5582 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5585 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5588 xpt_print_path(xs->ccb_h.path);
5589 __va_start(ap, fmt);
5596 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5599 struct timespec x = *b;
5601 elapsed = GET_NANOSEC(&x);
5608 isp_mbox_acquire(ispsoftc_t *isp)
5610 if (isp->isp_osinfo.mboxbsy) {
5613 isp->isp_osinfo.mboxcmd_done = 0;
5614 isp->isp_osinfo.mboxbsy = 1;
5620 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5622 unsigned int usecs = mbp->timeout;
5623 unsigned int max, olim, ilim;
5626 usecs = MBCMD_DEFAULT_TIMEOUT;
5628 max = isp->isp_mbxwrk0 + 1;
5630 if (isp->isp_osinfo.mbox_sleep_ok) {
5631 unsigned int ms = (usecs + 999) / 1000;
5633 isp->isp_osinfo.mbox_sleep_ok = 0;
5634 isp->isp_osinfo.mbox_sleeping = 1;
5635 for (olim = 0; olim < max; olim++) {
5636 lksleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, 0, "ispmbx_sleep", isp_mstohz(ms));
5637 if (isp->isp_osinfo.mboxcmd_done) {
5641 isp->isp_osinfo.mbox_sleep_ok = 1;
5642 isp->isp_osinfo.mbox_sleeping = 0;
5644 for (olim = 0; olim < max; olim++) {
5645 for (ilim = 0; ilim < usecs; ilim += 100) {
5647 uint16_t sema, mbox;
5648 if (isp->isp_osinfo.mboxcmd_done) {
5651 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5652 isp_intr(isp, isr, sema, mbox);
5653 if (isp->isp_osinfo.mboxcmd_done) {
5659 if (isp->isp_osinfo.mboxcmd_done) {
5664 if (isp->isp_osinfo.mboxcmd_done == 0) {
5665 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5666 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5667 mbp->param[0] = MBOX_TIMEOUT;
5668 isp->isp_osinfo.mboxcmd_done = 1;
5673 isp_mbox_notify_done(ispsoftc_t *isp)
5675 if (isp->isp_osinfo.mbox_sleeping) {
5676 wakeup(&isp->isp_mbxworkp);
5678 isp->isp_osinfo.mboxcmd_done = 1;
5682 isp_mbox_release(ispsoftc_t *isp)
5684 isp->isp_osinfo.mboxbsy = 0;
5688 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5691 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5694 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5704 t.tv_sec = ms / 1000;
5705 t.tv_usec = (ms % 1000) * 1000;
5706 hz = tvtohz_high(&t);
5717 isp_platform_intr(void *arg)
5719 ispsoftc_t *isp = arg;
5721 uint16_t sema, mbox;
5725 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5726 isp->isp_intbogus++;
5728 isp_intr(isp, isr, sema, mbox);
5734 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5736 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5737 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5739 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5741 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5745 isp_timer(void *arg)
5747 ispsoftc_t *isp = arg;
5750 #ifdef ISP_TARGET_MODE
5751 isp_tmcmd_restart(isp);
5754 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);