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;
193 isp->isp_osinfo.ehook.ich_desc = "isp";
195 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
196 * will be called right awawy, which will clear isp_osinfo.ehook_active,
197 * which would be unwise to then set again later.
199 isp->isp_osinfo.ehook_active = 1;
200 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
201 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
206 * Create the device queue for our SIM(s).
208 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
209 if (isp->isp_osinfo.devq == NULL) {
210 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
214 sysctl_ctx_init(&isp->isp_sysctl_ctx);
215 isp->isp_sysctl_tree = SYSCTL_ADD_NODE(&isp->isp_sysctl_ctx,
216 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
217 device_get_nameunit(isp->isp_dev), CTLFLAG_RD, 0, "");
218 if (isp->isp_sysctl_tree == NULL) {
219 device_printf(isp->isp_dev, "can't add sysctl node\n");
222 for (chan = 0; chan < isp->isp_nchan; chan++) {
223 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
228 callout_init(&isp->isp_osinfo.tmo);
229 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
230 isp->isp_osinfo.timer_active = 1;
232 isp->isp_osinfo.cdev = make_dev(&isp_ops, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
233 if (isp->isp_osinfo.cdev) {
234 isp->isp_osinfo.cdev->si_drv1 = isp;
239 while (--chan >= 0) {
241 struct cam_path *path;
243 sim = ISP_FC_PC(isp, chan)->sim;
244 path = ISP_FC_PC(isp, chan)->path;
246 sim = ISP_SPI_PC(isp, chan)->sim;
247 path = ISP_SPI_PC(isp, chan)->path;
251 xpt_bus_deregister(cam_sim_path(sim));
255 if (isp->isp_osinfo.ehook_active) {
256 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
257 isp->isp_osinfo.ehook_active = 0;
259 if (isp->isp_osinfo.cdev) {
260 destroy_dev(isp->isp_osinfo.cdev);
261 isp->isp_osinfo.cdev = NULL;
263 isp->isp_osinfo.devq = NULL;
268 isp_detach(ispsoftc_t *isp)
271 struct cam_path *path;
272 struct ccb_setasync csa;
276 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
278 sim = ISP_FC_PC(isp, chan)->sim;
279 path = ISP_FC_PC(isp, chan)->path;
281 sim = ISP_SPI_PC(isp, chan)->sim;
282 path = ISP_SPI_PC(isp, chan)->path;
284 if (sim->refcount > 2) {
289 if (isp->isp_osinfo.timer_active) {
290 callout_stop(&isp->isp_osinfo.tmo);
291 isp->isp_osinfo.timer_active = 0;
293 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
295 sim = ISP_FC_PC(isp, chan)->sim;
296 path = ISP_FC_PC(isp, chan)->path;
298 sim = ISP_SPI_PC(isp, chan)->sim;
299 path = ISP_SPI_PC(isp, chan)->path;
301 xpt_setup_ccb(&csa.ccb_h, path, 5);
302 csa.ccb_h.func_code = XPT_SASYNC_CB;
303 csa.event_enable = 0;
304 csa.callback = isp_cam_async;
305 csa.callback_arg = sim;
306 xpt_action((union ccb *)&csa);
308 xpt_bus_deregister(cam_sim_path(sim));
312 if (isp->isp_osinfo.cdev) {
313 destroy_dev(isp->isp_osinfo.cdev);
314 isp->isp_osinfo.cdev = NULL;
316 if (isp->isp_osinfo.ehook_active) {
317 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
318 isp->isp_osinfo.ehook_active = 0;
320 if (isp->isp_osinfo.devq != NULL) {
321 isp->isp_osinfo.devq = NULL;
323 if (isp->isp_sysctl_tree != NULL)
324 sysctl_ctx_free(&isp->isp_sysctl_ctx);
329 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
332 struct isp_fc *fc = ISP_FC_PC(isp, chan);
333 if (fc->simqfrozen == 0) {
334 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
335 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
336 xpt_freeze_simq(fc->sim, 1);
338 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
339 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
345 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
348 struct isp_fc *fc = ISP_FC_PC(isp, chan);
349 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
350 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
351 if (wasfrozen && fc->simqfrozen == 0) {
352 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
353 xpt_release_simq(fc->sim, 1);
360 ispioctl(struct dev_ioctl_args *ap)
362 cdev_t dev = ap->a_head.a_dev;
363 caddr_t addr = ap->a_data;
364 u_long c = ap->a_cmd;
366 int nr, chan, retval = ENOTTY;
373 int olddblev = isp->isp_dblev;
374 isp->isp_dblev = *(int *)addr;
375 *(int *)addr = olddblev;
381 if (chan < 0 || chan >= isp->isp_nchan) {
386 *(int *)addr = FCPARAM(isp, chan)->role;
388 *(int *)addr = SDPARAM(isp, chan)->role;
395 if (chan < 0 || chan >= isp->isp_nchan) {
400 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
406 * We don't really support dual role at present on FC cards.
408 * We should, but a bunch of things are currently broken,
411 if (nr == ISP_ROLE_BOTH) {
412 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
416 *(int *)addr = FCPARAM(isp, chan)->role;
417 #ifdef ISP_INTERNAL_TARGET
419 retval = isp_fc_change_role(isp, chan, nr);
422 FCPARAM(isp, chan)->role = nr;
425 *(int *)addr = SDPARAM(isp, chan)->role;
426 SDPARAM(isp, chan)->role = nr;
433 #ifdef ISP_TARGET_MODE
434 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
444 if (chan < 0 || chan >= isp->isp_nchan) {
449 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
461 if (chan < 0 || chan >= isp->isp_nchan) {
466 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
474 case ISP_FC_GETDINFO:
476 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
482 if (ifc->loopid >= MAX_FC_TARG) {
486 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
487 if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
488 ifc->role = lp->roles;
489 ifc->loopid = lp->handle;
490 ifc->portid = lp->portid;
491 ifc->node_wwn = lp->node_wwn;
492 ifc->port_wwn = lp->port_wwn;
501 isp_stats_t *sp = (isp_stats_t *) addr;
503 ISP_MEMZERO(sp, sizeof (*sp));
504 sp->isp_stat_version = ISP_STATS_VERSION;
505 sp->isp_type = isp->isp_type;
506 sp->isp_revision = isp->isp_revision;
508 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
509 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
510 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
511 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
512 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
513 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
514 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
515 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
523 isp->isp_intbogus = 0;
524 isp->isp_intmboxc = 0;
525 isp->isp_intoasync = 0;
526 isp->isp_rsltccmplt = 0;
527 isp->isp_fphccmplt = 0;
528 isp->isp_rscchiwater = 0;
529 isp->isp_fpcchiwater = 0;
533 case ISP_FC_GETHINFO:
535 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
536 int chan = hba->fc_channel;
538 if (chan < 0 || chan >= isp->isp_nchan) {
542 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
543 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
544 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
545 hba->fc_nchannels = isp->isp_nchan;
547 hba->fc_nports = MAX_FC_TARG;
548 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
549 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
550 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
551 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
552 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
553 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
554 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
556 hba->fc_nports = MAX_TARGETS;
558 hba->fc_topology = 0;
559 hba->nvram_node_wwn = 0ull;
560 hba->nvram_port_wwn = 0ull;
561 hba->active_node_wwn = 0ull;
562 hba->active_port_wwn = 0ull;
570 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
579 if (chan < 0 || chan >= isp->isp_nchan) {
584 needmarker = retval = 0;
585 loopid = fct->loopid;
588 uint8_t local[QENTRY_LEN];
590 isp24xx_statusreq_t *sp;
591 fcparam *fcp = FCPARAM(isp, chan);
595 for (i = 0; i < MAX_FC_TARG; i++) {
596 lp = &fcp->portdb[i];
597 if (lp->handle == loopid) {
601 if (i == MAX_FC_TARG) {
606 /* XXX VALIDATE LP XXX */
607 tmf = (isp24xx_tmf_t *) local;
608 ISP_MEMZERO(tmf, QENTRY_LEN);
609 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
610 tmf->tmf_header.rqs_entry_count = 1;
611 tmf->tmf_nphdl = lp->handle;
613 tmf->tmf_timeout = 2;
614 tmf->tmf_tidlo = lp->portid;
615 tmf->tmf_tidhi = lp->portid >> 16;
616 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
617 tmf->tmf_lun[1] = fct->lun & 0xff;
618 if (fct->lun >= 256) {
619 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
621 switch (fct->action) {
623 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
625 case IPT_TARGET_RESET:
626 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
630 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
633 case IPT_CLEAR_TASK_SET:
634 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
637 case IPT_ABORT_TASK_SET:
638 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
649 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
650 mbs.param[1] = QENTRY_LEN;
651 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
652 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
653 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
654 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
656 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
661 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
662 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
663 sp = (isp24xx_statusreq_t *) local;
664 sp->req_completion_status = 1;
665 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
666 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
667 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
668 FC_SCRATCH_RELEASE(isp, chan);
669 if (retval || sp->req_completion_status != 0) {
670 FC_SCRATCH_RELEASE(isp, chan);
679 MBSINIT(&mbs, 0, MBLOGALL, 0);
680 if (ISP_CAP_2KLOGIN(isp) == 0) {
683 switch (fct->action) {
685 mbs.param[0] = MBOX_CLEAR_ACA;
686 mbs.param[1] = loopid;
687 mbs.param[2] = fct->lun;
689 case IPT_TARGET_RESET:
690 mbs.param[0] = MBOX_TARGET_RESET;
691 mbs.param[1] = loopid;
695 mbs.param[0] = MBOX_LUN_RESET;
696 mbs.param[1] = loopid;
697 mbs.param[2] = fct->lun;
700 case IPT_CLEAR_TASK_SET:
701 mbs.param[0] = MBOX_CLEAR_TASK_SET;
702 mbs.param[1] = loopid;
703 mbs.param[2] = fct->lun;
706 case IPT_ABORT_TASK_SET:
707 mbs.param[0] = MBOX_ABORT_TASK_SET;
708 mbs.param[1] = loopid;
709 mbs.param[2] = fct->lun;
718 FCPARAM(isp, chan)->sendmarker = 1;
720 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
736 isp_intr_enable(void *arg)
739 ispsoftc_t *isp = arg;
741 for (chan = 0; chan < isp->isp_nchan; chan++) {
743 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
744 ISP_ENABLE_INTS(isp);
748 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
749 ISP_ENABLE_INTS(isp);
754 isp->isp_osinfo.ehook_active = 0;
756 /* Release our hook so that the boot can continue. */
757 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
764 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
765 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
767 static ISP_INLINE int
768 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
770 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
771 if (ISP_PCMD(ccb) == NULL) {
774 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
778 static ISP_INLINE void
779 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
781 ((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
782 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
783 ISP_PCMD(ccb) = NULL;
786 * Put the target mode functions here, because some are inlines
789 #ifdef ISP_TARGET_MODE
790 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
791 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
792 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
793 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
794 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
795 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
796 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
797 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
798 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
799 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
800 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
801 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
802 static void isp_enable_lun(ispsoftc_t *, union ccb *);
803 static void isp_enable_deferred_luns(ispsoftc_t *, int);
804 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
805 static void isp_disable_lun(ispsoftc_t *, union ccb *);
806 static int isp_enable_target_mode(ispsoftc_t *, int);
807 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
808 static timeout_t isp_refire_putback_atio;
809 static void isp_complete_ctio(union ccb *);
810 static void isp_target_putback_atio(union ccb *);
811 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
812 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
813 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
814 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
815 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
816 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
817 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
818 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
819 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
820 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
821 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
822 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
824 static ISP_INLINE int
825 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
830 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
831 SLIST_FOREACH(tptr, lhp, next) {
832 if (xpt_path_lun_id(tptr->owner) == lun) {
840 dump_tstates(ispsoftc_t *isp, int bus)
844 tstate_t *tptr = NULL;
846 if (bus >= isp->isp_nchan) {
849 for (i = 0; i < LUN_HASH_SIZE; i++) {
850 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
852 SLIST_FOREACH(tptr, lhp, next) {
853 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
859 static ISP_INLINE tstate_t *
860 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
862 tstate_t *tptr = NULL;
866 if (bus < isp->isp_nchan) {
867 for (i = 0; i < LUN_HASH_SIZE; i++) {
868 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
869 SLIST_FOREACH(tptr, lhp, next) {
870 if (xpt_path_lun_id(tptr->owner) == lun) {
880 static ISP_INLINE tstate_t *
881 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
883 tstate_t *tptr = NULL;
884 atio_private_data_t *atp;
888 if (bus < isp->isp_nchan && tagval != 0) {
889 for (i = 0; i < LUN_HASH_SIZE; i++) {
890 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
891 SLIST_FOREACH(tptr, lhp, next) {
892 atp = isp_get_atpd(isp, tptr, tagval);
893 if (atp && atp->tag == tagval) {
903 static ISP_INLINE inot_private_data_t *
904 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
906 inot_private_data_t *ntp;
911 for (bus = 0; bus < isp->isp_nchan; bus++) {
912 for (i = 0; i < LUN_HASH_SIZE; i++) {
913 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
914 SLIST_FOREACH(tptr, lhp, next) {
915 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
926 static ISP_INLINE void
927 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
929 KASSERT((tptr->hold), ("tptr not held"));
934 isp_tmcmd_restart(ispsoftc_t *isp)
936 inot_private_data_t *ntp;
941 for (bus = 0; bus < isp->isp_nchan; bus++) {
942 for (i = 0; i < LUN_HASH_SIZE; i++) {
943 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
944 SLIST_FOREACH(tptr, lhp, next) {
945 inot_private_data_t *restart_queue = tptr->restart_queue;
946 tptr->restart_queue = NULL;
947 while (restart_queue) {
949 restart_queue = ntp->rd.nt.nt_hba;
951 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
952 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
954 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
955 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
957 isp_put_ntpd(isp, tptr, ntp);
958 if (tptr->restart_queue && restart_queue != NULL) {
959 ntp = tptr->restart_queue;
960 tptr->restart_queue = restart_queue;
961 while (restart_queue->rd.nt.nt_hba) {
962 restart_queue = restart_queue->rd.nt.nt_hba;
964 restart_queue->rd.nt.nt_hba = ntp;
973 static ISP_INLINE atio_private_data_t *
974 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
976 atio_private_data_t *atp;
981 tptr->atfree = atp->next;
985 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
986 if (atp->tag == tag) {
993 static ISP_INLINE void
994 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
998 atp->next = tptr->atfree;
1003 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1005 atio_private_data_t *atp;
1006 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1008 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1009 if (atp->tag == 0) {
1012 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",
1013 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]);
1018 static ISP_INLINE inot_private_data_t *
1019 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1021 inot_private_data_t *ntp;
1024 tptr->ntfree = ntp->next;
1029 static ISP_INLINE inot_private_data_t *
1030 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1032 inot_private_data_t *ntp;
1033 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1034 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1041 static ISP_INLINE void
1042 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1044 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1045 ntp->next = tptr->ntfree;
1050 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1058 lun = xpt_path_lun_id(path);
1059 if (lun != CAM_LUN_WILDCARD) {
1060 if (lun >= ISP_MAX_LUNS(isp)) {
1061 return (CAM_LUN_INVALID);
1064 if (is_lun_enabled(isp, bus, lun)) {
1065 return (CAM_LUN_ALRDY_ENA);
1067 tptr = kmalloc(sizeof (tstate_t), M_DEVBUF, M_WAITOK | M_ZERO);
1068 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1069 if (status != CAM_REQ_CMP) {
1070 kfree(tptr, M_DEVBUF);
1073 SLIST_INIT(&tptr->atios);
1074 SLIST_INIT(&tptr->inots);
1075 for (i = 0; i < ATPDPSIZE-1; i++) {
1076 tptr->atpool[i].next = &tptr->atpool[i+1];
1077 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1079 tptr->atfree = tptr->atpool;
1080 tptr->ntfree = tptr->ntpool;
1082 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1083 SLIST_INSERT_HEAD(lhp, tptr, next);
1085 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1086 return (CAM_REQ_CMP);
1089 static ISP_INLINE void
1090 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1093 KASSERT((tptr->hold == 0), ("tptr still held"));
1094 ISP_GET_PC_ADDR(isp, xpt_path_path_id(tptr->owner), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1095 SLIST_REMOVE(lhp, tptr, tstate, next);
1096 xpt_free_path(tptr->owner);
1097 kfree(tptr, M_DEVBUF);
1104 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1106 tstate_t *tptr = NULL;
1107 int bus, tm_enabled, target_role;
1112 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1114 bus = XS_CHANNEL(ccb);
1115 target = ccb->ccb_h.target_id;
1116 lun = ccb->ccb_h.target_lun;
1117 if (target != CAM_TARGET_WILDCARD && target != 0) {
1118 ccb->ccb_h.status = CAM_TID_INVALID;
1122 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1123 ccb->ccb_h.status = CAM_LUN_INVALID;
1128 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1129 ccb->ccb_h.status = CAM_LUN_INVALID;
1133 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1134 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1138 * Wait until we're not busy with the lun enables subsystem
1140 while (isp->isp_osinfo.tmbusy) {
1141 isp->isp_osinfo.tmwanted = 1;
1142 mtx_sleep(isp, &isp->isp_lock, 0, "want_isp_enable_lun", 0);
1144 isp->isp_osinfo.tmbusy = 1;
1147 * This is as a good a place as any to check f/w capabilities.
1151 if (ISP_CAP_TMODE(isp) == 0) {
1152 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1153 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1157 * We *could* handle non-SCCLUN f/w, but we'd have to
1158 * dork with our already fragile enable/disable code.
1160 if (ISP_CAP_SCCFW(isp) == 0) {
1161 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1162 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1166 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1169 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1173 * Create the state pointer.
1174 * It should not already exist.
1176 tptr = get_lun_statep(isp, bus, lun);
1178 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1181 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1182 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1187 * We have a tricky maneuver to perform here.
1189 * If target mode isn't already enabled here,
1190 * *and* our current role includes target mode,
1191 * we enable target mode here.
1194 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1195 if (tm_enabled == 0 && target_role != 0) {
1196 if (isp_enable_target_mode(isp, bus)) {
1197 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1198 destroy_lun_state(isp, tptr);
1206 * Now check to see whether this bus is in target mode already.
1208 * If not, a later role change into target mode will finish the job.
1210 if (tm_enabled == 0) {
1211 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1212 ccb->ccb_h.status = CAM_REQ_CMP;
1213 xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
1220 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1223 if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
1224 destroy_lun_state(isp, tptr);
1228 rls_lun_statep(isp, tptr);
1230 isp->isp_osinfo.tmbusy = 0;
1231 if (isp->isp_osinfo.tmwanted) {
1232 isp->isp_osinfo.tmwanted = 0;
1239 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1242 * XXX: not entirely implemented yet
1244 (void) isp_enable_deferred(isp, bus, 0);
1248 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1252 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1253 if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
1254 status = CAM_REQ_CMP;
1256 int cmd_cnt, not_cnt;
1259 cmd_cnt = DFLT_CMND_CNT;
1260 not_cnt = DFLT_INOT_CNT;
1265 status = CAM_REQ_INPROG;
1266 isp->isp_osinfo.rptr = &status;
1267 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1268 status = CAM_RESRC_UNAVAIL;
1270 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1272 isp->isp_osinfo.rptr = NULL;
1275 if (status == CAM_REQ_CMP) {
1276 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1277 isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1283 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1285 tstate_t *tptr = NULL;
1291 bus = XS_CHANNEL(ccb);
1292 target = ccb->ccb_h.target_id;
1293 lun = ccb->ccb_h.target_lun;
1294 if (target != CAM_TARGET_WILDCARD && target != 0) {
1295 ccb->ccb_h.status = CAM_TID_INVALID;
1299 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1300 ccb->ccb_h.status = CAM_LUN_INVALID;
1305 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1306 ccb->ccb_h.status = CAM_LUN_INVALID;
1310 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1311 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1315 * See if we're busy disabling a lun now.
1317 while (isp->isp_osinfo.tmbusy) {
1318 isp->isp_osinfo.tmwanted = 1;
1319 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
1321 isp->isp_osinfo.tmbusy = 1;
1324 * Find the state pointer.
1326 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1327 ccb->ccb_h.status = CAM_PATH_INVALID;
1332 * If we're a 24XX card, we're done.
1335 status = CAM_REQ_CMP;
1340 * For SCC FW, we only deal with lun zero.
1346 isp->isp_osinfo.rptr = &status;
1347 status = CAM_REQ_INPROG;
1348 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1349 status = CAM_RESRC_UNAVAIL;
1351 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1354 if (status == CAM_REQ_CMP) {
1355 xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
1358 destroy_lun_state(isp, tptr);
1360 isp->isp_osinfo.rptr = NULL;
1361 isp->isp_osinfo.tmbusy = 0;
1362 if (isp->isp_osinfo.tmwanted) {
1363 isp->isp_osinfo.tmwanted = 0;
1370 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1374 ISP_GET_PC(isp, bus, tm_enabled, ct);
1382 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1383 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1384 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1385 mbs.param[2] = bus << 7;
1386 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1387 isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
1390 SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
1392 ISP_SET_PC(isp, bus, tm_enabled, 1);
1393 isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
1399 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1403 ISP_GET_PC(isp, bus, tm_enabled, ct);
1411 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1412 mbs.param[2] = bus << 7;
1413 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1414 isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
1417 SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
1419 ISP_SET_PC(isp, bus, tm_enabled, 0);
1420 isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
1426 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1430 rptr = isp->isp_osinfo.rptr;
1431 if (lep->le_status != LUN_OK) {
1432 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1434 *rptr = CAM_REQ_CMP_ERR;
1439 *rptr = CAM_REQ_CMP;
1446 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1450 atio_private_data_t *atp;
1451 struct ccb_scsiio *cso = &ccb->csio;
1452 uint32_t dmaresult, handle;
1453 uint8_t local[QENTRY_LEN];
1456 * Do some sanity checks.
1458 if (cso->dxfer_len == 0) {
1459 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1460 xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1461 ccb->ccb_h.status = CAM_REQ_INVALID;
1467 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1469 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1471 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1472 dump_tstates(isp, XS_CHANNEL(ccb));
1473 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1479 atp = isp_get_atpd(isp, tptr, cso->tag_id);
1481 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1482 isp_dump_atpd(isp, tptr);
1483 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1488 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1489 ccb->ccb_h.status = CAM_REQ_ABORTED;
1495 * Check to make sure we're still in target mode.
1497 if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1498 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);
1499 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1505 * Get some resources
1507 if (isp_get_pcmd(isp, ccb)) {
1508 rls_lun_statep(isp, tptr);
1509 xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1510 cam_freeze_devq(ccb->ccb_h.path);
1511 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1512 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1516 qe = isp_getrqentry(isp);
1518 xpt_print(ccb->ccb_h.path, rqo, __func__);
1519 cam_freeze_devq(ccb->ccb_h.path);
1520 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1521 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1524 memset(local, 0, QENTRY_LEN);
1527 * We're either moving data or completing a command here.
1530 ct7_entry_t *cto = (ct7_entry_t *) local;
1532 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1533 cto->ct_header.rqs_entry_count = 1;
1534 cto->ct_header.rqs_seqno = 1;
1535 cto->ct_nphdl = atp->nphdl;
1536 cto->ct_rxid = atp->tag;
1537 cto->ct_iid_lo = atp->portid;
1538 cto->ct_iid_hi = atp->portid >> 16;
1539 cto->ct_oxid = atp->oxid;
1540 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1541 cto->ct_scsi_status = cso->scsi_status;
1542 cto->ct_timeout = 120;
1543 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1544 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1545 cto->ct_flags |= CT7_SENDSTATUS;
1547 if (cso->dxfer_len == 0) {
1548 cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1549 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1550 int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1551 cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1552 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1553 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1556 cto->ct_flags |= CT7_FLAG_MODE0;
1557 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1558 cto->ct_flags |= CT7_DATA_IN;
1560 cto->ct_flags |= CT7_DATA_OUT;
1562 cto->rsp.m0.reloff = atp->bytes_xfered;
1564 * Don't overrun the limits placed on us
1566 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1567 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1569 atp->last_xframt = cso->dxfer_len;
1570 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1572 if (cto->ct_flags & CT7_SENDSTATUS) {
1573 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1574 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1575 if (cto->ct_resid < 0) {
1576 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1577 } else if (cto->ct_resid > 0) {
1578 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1580 atp->state = ATPD_STATE_LAST_CTIO;
1581 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,
1582 atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1585 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,
1586 cso->dxfer_len, atp->bytes_xfered);
1587 atp->state = ATPD_STATE_CTIO;
1589 } else if (IS_FC(isp)) {
1590 ct2_entry_t *cto = (ct2_entry_t *) local;
1592 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1593 cto->ct_header.rqs_entry_count = 1;
1594 cto->ct_header.rqs_seqno = 1;
1595 if (ISP_CAP_2KLOGIN(isp) == 0) {
1596 ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1598 cto->ct_iid = cso->init_id;
1599 if (ISP_CAP_SCCFW(isp) == 0) {
1600 cto->ct_lun = ccb->ccb_h.target_lun;
1605 cto->ct_rxid = cso->tag_id;
1606 if (cso->dxfer_len == 0) {
1607 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1608 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1609 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1610 if (cto->ct_resid < 0) {
1611 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1612 } else if (cto->ct_resid > 0) {
1613 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1615 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1616 int m = min(cso->sense_len, MAXRESPLEN);
1617 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1618 cto->rsp.m1.ct_senselen = m;
1619 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1620 } else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1624 xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1627 cto->ct_flags |= CT2_FLAG_MODE0;
1628 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1629 cto->ct_flags |= CT2_DATA_IN;
1631 cto->ct_flags |= CT2_DATA_OUT;
1633 cto->ct_reloff = atp->bytes_xfered;
1634 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1636 * Don't overrun the limits placed on us
1638 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1639 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1641 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1642 cto->ct_flags |= CT2_SENDSTATUS;
1643 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1644 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1645 if (cto->ct_resid < 0) {
1646 cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1647 } else if (cto->ct_resid > 0) {
1648 cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1651 atp->last_xframt = cso->dxfer_len;
1654 * If we're sending data and status back together,
1655 * we can't also send back sense data as well.
1657 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1660 if (cto->ct_flags & CT2_SENDSTATUS) {
1661 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1662 cto->ct_flags |= CT2_CCINCR;
1663 atp->state = ATPD_STATE_LAST_CTIO;
1664 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,
1665 atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1668 atp->state = ATPD_STATE_CTIO;
1669 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,
1670 cso->dxfer_len, atp->bytes_xfered);
1672 cto->ct_timeout = 10;
1674 ct_entry_t *cto = (ct_entry_t *) local;
1676 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1677 cto->ct_header.rqs_entry_count = 1;
1678 cto->ct_header.rqs_seqno = 1;
1679 cto->ct_iid = cso->init_id;
1680 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1681 cto->ct_tgt = ccb->ccb_h.target_id;
1682 cto->ct_lun = ccb->ccb_h.target_lun;
1683 cto->ct_fwhandle = cso->tag_id >> 16;
1684 if (AT_HAS_TAG(cso->tag_id)) {
1685 cto->ct_tag_val = cso->tag_id;
1686 cto->ct_flags |= CT_TQAE;
1688 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1689 cto->ct_flags |= CT_NODISC;
1691 if (cso->dxfer_len == 0) {
1692 cto->ct_flags |= CT_NO_DATA;
1693 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1694 cto->ct_flags |= CT_DATA_IN;
1696 cto->ct_flags |= CT_DATA_OUT;
1698 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1699 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1700 cto->ct_scsi_status = cso->scsi_status;
1701 cto->ct_resid = cso->resid;
1702 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1703 cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1705 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1706 cto->ct_timeout = 10;
1709 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1710 xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1711 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1717 * Call the dma setup routines for this entry (and any subsequent
1718 * CTIOs) if there's data to move, and then tell the f/w it's got
1719 * new things to play with. As with isp_start's usage of DMA setup,
1720 * any swizzling is done in the machine dependent layer. Because
1721 * of this, we put the request onto the queue area first in native
1726 ct7_entry_t *cto = (ct7_entry_t *) local;
1727 cto->ct_syshandle = handle;
1728 } else if (IS_FC(isp)) {
1729 ct2_entry_t *cto = (ct2_entry_t *) local;
1730 cto->ct_syshandle = handle;
1732 ct_entry_t *cto = (ct_entry_t *) local;
1733 cto->ct_syshandle = handle;
1736 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1737 if (dmaresult == CMD_QUEUED) {
1739 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1740 rls_lun_statep(isp, tptr);
1743 if (dmaresult == CMD_EAGAIN) {
1744 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1746 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1748 isp_destroy_tgt_handle(isp, handle);
1750 rls_lun_statep(isp, tptr);
1751 isp_free_pcmd(isp, ccb);
1756 isp_refire_putback_atio(void *arg)
1758 union ccb *ccb = arg;
1759 ispsoftc_t *isp = XS_ISP(ccb);
1761 isp_target_putback_atio(ccb);
1766 isp_target_putback_atio(union ccb *ccb)
1769 struct ccb_scsiio *cso;
1774 qe = isp_getrqentry(isp);
1776 xpt_print(ccb->ccb_h.path, rqo, __func__);
1777 (void) timeout(isp_refire_putback_atio, ccb, 10);
1780 memset(qe, 0, QENTRY_LEN);
1783 at2_entry_t local, *at = &local;
1784 ISP_MEMZERO(at, sizeof (at2_entry_t));
1785 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1786 at->at_header.rqs_entry_count = 1;
1787 if (ISP_CAP_SCCFW(isp)) {
1788 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1790 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1792 at->at_status = CT_OK;
1793 at->at_rxid = cso->tag_id;
1794 at->at_iid = cso->ccb_h.target_id;
1795 isp_put_atio2(isp, at, qe);
1797 at_entry_t local, *at = &local;
1798 ISP_MEMZERO(at, sizeof (at_entry_t));
1799 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1800 at->at_header.rqs_entry_count = 1;
1801 at->at_iid = cso->init_id;
1802 at->at_iid |= XS_CHANNEL(ccb) << 7;
1803 at->at_tgt = cso->ccb_h.target_id;
1804 at->at_lun = cso->ccb_h.target_lun;
1805 at->at_status = CT_OK;
1806 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1807 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1808 isp_put_atio(isp, at, qe);
1810 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1811 ISP_SYNC_REQUEST(isp);
1812 isp_complete_ctio(ccb);
1816 isp_complete_ctio(union ccb *ccb)
1818 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1819 ccb->ccb_h.status |= CAM_REQ_CMP;
1821 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1822 isp_free_pcmd(XS_ISP(ccb), ccb);
1827 * Handle ATIO stuff that the generic code can't.
1828 * This means handling CDBs.
1832 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1836 struct ccb_accept_tio *atiop;
1837 atio_private_data_t *atp;
1840 * The firmware status (except for the QLTM_SVALID bit)
1841 * indicates why this ATIO was sent to us.
1843 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1845 * If the DISCONNECTS DISABLED bit is set in the flags field,
1846 * we're still connected on the SCSI bus.
1848 status = aep->at_status;
1849 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1851 * Bus Phase Sequence error. We should have sense data
1852 * suggested by the f/w. I'm not sure quite yet what
1853 * to do about this for CAM.
1855 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1856 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1859 if ((status & ~QLTM_SVALID) != AT_CDB) {
1860 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1861 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1865 bus = GET_BUS_VAL(aep->at_iid);
1866 tptr = get_lun_statep(isp, bus, aep->at_lun);
1868 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1871 * Because we can't autofeed sense data back with
1872 * a command for parallel SCSI, we can't give back
1873 * a CHECK CONDITION. We'll give back a BUSY status
1874 * instead. This works out okay because the only
1875 * time we should, in fact, get this, is in the
1876 * case that somebody configured us without the
1877 * blackhole driver, so they get what they deserve.
1879 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1884 atp = isp_get_atpd(isp, tptr, 0);
1885 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1886 if (atiop == NULL || atp == NULL) {
1888 * Because we can't autofeed sense data back with
1889 * a command for parallel SCSI, we can't give back
1890 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1891 * instead. This works out okay because the only time we
1892 * should, in fact, get this, is in the case that we've
1895 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1896 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1897 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1899 isp_put_atpd(isp, tptr, atp);
1901 rls_lun_statep(isp, tptr);
1904 atp->tag = aep->at_tag_val;
1905 if (atp->tag == 0) {
1908 atp->state = ATPD_STATE_ATIO;
1909 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1911 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1912 atiop->ccb_h.target_id = aep->at_tgt;
1913 atiop->ccb_h.target_lun = aep->at_lun;
1914 if (aep->at_flags & AT_NODISC) {
1915 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1917 atiop->ccb_h.flags = 0;
1920 if (status & QLTM_SVALID) {
1921 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1922 atiop->sense_len = amt;
1923 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1925 atiop->sense_len = 0;
1928 atiop->init_id = GET_IID_VAL(aep->at_iid);
1929 atiop->cdb_len = aep->at_cdblen;
1930 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1931 atiop->ccb_h.status = CAM_CDB_RECVD;
1933 * Construct a tag 'id' based upon tag value (which may be 0..255)
1934 * and the handle (which we have to preserve).
1936 atiop->tag_id = atp->tag;
1937 if (aep->at_flags & AT_TQAE) {
1938 atiop->tag_action = aep->at_tag_type;
1939 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1941 atp->orig_datalen = 0;
1942 atp->bytes_xfered = 0;
1943 atp->last_xframt = 0;
1944 atp->lun = aep->at_lun;
1945 atp->nphdl = aep->at_iid;
1946 atp->portid = PORT_NONE;
1948 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1949 atp->tattr = aep->at_tag_type;
1950 atp->state = ATPD_STATE_CAM;
1951 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1952 rls_lun_statep(isp, tptr);
1956 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1961 struct ccb_accept_tio *atiop;
1963 atio_private_data_t *atp;
1964 inot_private_data_t *ntp;
1967 * The firmware status (except for the QLTM_SVALID bit)
1968 * indicates why this ATIO was sent to us.
1970 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1972 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1973 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1974 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1978 if (ISP_CAP_SCCFW(isp)) {
1979 lun = aep->at_scclun;
1983 if (ISP_CAP_2KLOGIN(isp)) {
1984 nphdl = ((at2e_entry_t *)aep)->at_iid;
1986 nphdl = aep->at_iid;
1988 tptr = get_lun_statep(isp, 0, lun);
1990 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1992 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1993 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1999 * Start any commands pending resources first.
2001 if (tptr->restart_queue) {
2002 inot_private_data_t *restart_queue = tptr->restart_queue;
2003 tptr->restart_queue = NULL;
2004 while (restart_queue) {
2005 ntp = restart_queue;
2006 restart_queue = ntp->rd.nt.nt_hba;
2007 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2008 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2009 isp_put_ntpd(isp, tptr, ntp);
2011 * If a recursion caused the restart queue to start to fill again,
2012 * stop and splice the new list on top of the old list and restore
2013 * it and go to noresrc.
2015 if (tptr->restart_queue) {
2016 ntp = tptr->restart_queue;
2017 tptr->restart_queue = restart_queue;
2018 while (restart_queue->rd.nt.nt_hba) {
2019 restart_queue = restart_queue->rd.nt.nt_hba;
2021 restart_queue->rd.nt.nt_hba = ntp;
2027 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2028 if (atiop == NULL) {
2032 atp = isp_get_atpd(isp, tptr, 0);
2037 atp->tag = aep->at_rxid;
2038 atp->state = ATPD_STATE_ATIO;
2039 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2041 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2042 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2043 atiop->ccb_h.target_lun = lun;
2046 * We don't get 'suggested' sense data as we do with SCSI cards.
2048 atiop->sense_len = 0;
2049 if (ISP_CAP_2KLOGIN(isp)) {
2051 * NB: We could not possibly have 2K logins if we
2052 * NB: also did not have SCC FW.
2054 atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2056 atiop->init_id = aep->at_iid;
2060 * If we're not in the port database, add ourselves.
2062 if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2064 (((uint64_t) aep->at_wwpn[0]) << 48) |
2065 (((uint64_t) aep->at_wwpn[1]) << 32) |
2066 (((uint64_t) aep->at_wwpn[2]) << 16) |
2067 (((uint64_t) aep->at_wwpn[3]) << 0);
2069 * However, make sure we delete ourselves if otherwise
2070 * we were there but at a different loop id.
2072 if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2073 isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2075 isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2077 atiop->cdb_len = ATIO2_CDBLEN;
2078 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2079 atiop->ccb_h.status = CAM_CDB_RECVD;
2080 atiop->tag_id = atp->tag;
2081 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2082 case ATIO2_TC_ATTR_SIMPLEQ:
2083 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2084 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2086 case ATIO2_TC_ATTR_HEADOFQ:
2087 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2088 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2090 case ATIO2_TC_ATTR_ORDERED:
2091 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2092 atiop->tag_action = MSG_ORDERED_Q_TAG;
2094 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2095 case ATIO2_TC_ATTR_UNTAGGED:
2097 atiop->tag_action = 0;
2101 atp->orig_datalen = aep->at_datalen;
2102 atp->bytes_xfered = 0;
2103 atp->last_xframt = 0;
2105 atp->nphdl = atiop->init_id;
2106 atp->sid = PORT_ANY;
2107 atp->oxid = aep->at_oxid;
2108 atp->cdb0 = aep->at_cdb[0];
2109 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2110 atp->state = ATPD_STATE_CAM;
2111 xpt_done((union ccb *)atiop);
2112 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);
2113 rls_lun_statep(isp, tptr);
2116 ntp = isp_get_ntpd(isp, tptr);
2118 rls_lun_statep(isp, tptr);
2119 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2122 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2123 ntp->rd.nt.nt_hba = tptr->restart_queue;
2124 tptr->restart_queue = ntp;
2125 rls_lun_statep(isp, tptr);
2129 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2132 uint16_t lun, chan, nphdl = NIL_HANDLE;
2134 uint64_t wwn = INI_NONE;
2137 struct ccb_accept_tio *atiop;
2138 atio_private_data_t *atp = NULL;
2139 inot_private_data_t *ntp;
2141 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2142 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2143 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2146 * Find the N-port handle, and Virtual Port Index for this command.
2148 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2149 * We also, as a matter of course, need to know the WWN of the initiator too.
2151 if (ISP_CAP_MULTI_ID(isp)) {
2153 * Find the right channel based upon D_ID
2155 isp_find_chan_by_did(isp, did, &chan);
2157 if (chan == ISP_NOCHAN) {
2161 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2162 * It's a bit tricky here as we need to stash this command *somewhere*.
2165 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2166 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2167 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2170 tptr = get_lun_statep(isp, 0, 0);
2172 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2174 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);
2175 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2179 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2182 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);
2188 * Find the PDB entry for this initiator
2190 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2192 * If we're not in the port database terminate the exchange.
2194 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",
2195 __func__, aep->at_rxid, did, chan, sid);
2196 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2203 * Get the tstate pointer
2205 tptr = get_lun_statep(isp, chan, lun);
2207 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2209 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2210 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2216 * Start any commands pending resources first.
2218 if (tptr->restart_queue) {
2219 inot_private_data_t *restart_queue = tptr->restart_queue;
2220 tptr->restart_queue = NULL;
2221 while (restart_queue) {
2222 ntp = restart_queue;
2223 restart_queue = ntp->rd.nt.nt_hba;
2224 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2225 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2226 isp_put_ntpd(isp, tptr, ntp);
2228 * If a recursion caused the restart queue to start to fill again,
2229 * stop and splice the new list on top of the old list and restore
2230 * it and go to noresrc.
2232 if (tptr->restart_queue) {
2233 if (restart_queue) {
2234 ntp = tptr->restart_queue;
2235 tptr->restart_queue = restart_queue;
2236 while (restart_queue->rd.nt.nt_hba) {
2237 restart_queue = restart_queue->rd.nt.nt_hba;
2239 restart_queue->rd.nt.nt_hba = ntp;
2247 * If the f/w is out of resources, just send a BUSY status back.
2249 if (aep->at_rxid == AT7_NORESRC_RXID) {
2250 rls_lun_statep(isp, tptr);
2251 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2256 * If we're out of resources, just send a BUSY status back.
2258 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2259 if (atiop == NULL) {
2260 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2264 atp = isp_get_atpd(isp, tptr, 0);
2266 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2269 if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2270 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",
2271 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2273 * It's not a "no resource" condition- but we can treat it like one
2278 atp->tag = aep->at_rxid;
2279 atp->state = ATPD_STATE_ATIO;
2280 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2282 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2283 atiop->init_id = nphdl;
2284 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2285 atiop->ccb_h.target_lun = lun;
2286 atiop->sense_len = 0;
2287 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2289 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2291 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2292 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2293 atiop->cdb_len = cdbxlen;
2294 atiop->ccb_h.status = CAM_CDB_RECVD;
2295 atiop->tag_id = atp->tag;
2296 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2297 case FCP_CMND_TASK_ATTR_SIMPLE:
2298 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2299 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2301 case FCP_CMND_TASK_ATTR_HEAD:
2302 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2303 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2305 case FCP_CMND_TASK_ATTR_ORDERED:
2306 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2307 atiop->tag_action = MSG_ORDERED_Q_TAG;
2311 case FCP_CMND_TASK_ATTR_ACA:
2312 case FCP_CMND_TASK_ATTR_UNTAGGED:
2313 atiop->tag_action = 0;
2316 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2317 atp->bytes_xfered = 0;
2318 atp->last_xframt = 0;
2322 atp->oxid = aep->at_hdr.ox_id;
2323 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2324 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2325 atp->state = ATPD_STATE_CAM;
2326 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);
2327 xpt_done((union ccb *)atiop);
2328 rls_lun_statep(isp, tptr);
2332 isp_put_atpd(isp, tptr, atp);
2334 ntp = isp_get_ntpd(isp, tptr);
2336 rls_lun_statep(isp, tptr);
2337 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2340 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2341 ntp->rd.nt.nt_hba = tptr->restart_queue;
2342 tptr->restart_queue = ntp;
2343 rls_lun_statep(isp, tptr);
2347 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2350 int sentstatus, ok, notify_cam, resid = 0;
2351 tstate_t *tptr = NULL;
2352 atio_private_data_t *atp = NULL;
2354 uint32_t tval, handle;
2357 * CTIO handles are 16 bits.
2358 * CTIO2 and CTIO7 are 32 bits.
2362 handle = ((ct_entry_t *)arg)->ct_syshandle;
2364 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2366 ccb = isp_find_xs_tgt(isp, handle);
2368 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2371 isp_destroy_tgt_handle(isp, handle);
2372 bus = XS_CHANNEL(ccb);
2373 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2375 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2377 KASSERT((tptr != NULL), ("cannot get state pointer"));
2378 if (isp->isp_nactive) {
2382 ct7_entry_t *ct = arg;
2384 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2386 rls_lun_statep(isp, tptr);
2387 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2391 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2392 ok = (ct->ct_nphdl == CT7_OK);
2393 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2394 ccb->ccb_h.status |= CAM_SENT_SENSE;
2396 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2397 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2398 resid = ct->ct_resid;
2399 atp->bytes_xfered += (atp->last_xframt - resid);
2400 atp->last_xframt = 0;
2402 if (ct->ct_nphdl == CT_HBA_RESET) {
2406 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2408 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2411 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2412 ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2413 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2414 } else if (IS_FC(isp)) {
2415 ct2_entry_t *ct = arg;
2417 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2419 rls_lun_statep(isp, tptr);
2420 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2423 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2424 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2425 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2426 ccb->ccb_h.status |= CAM_SENT_SENSE;
2428 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2429 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2430 resid = ct->ct_resid;
2431 atp->bytes_xfered += (atp->last_xframt - resid);
2432 atp->last_xframt = 0;
2434 if (ct->ct_status == CT_HBA_RESET) {
2438 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2440 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2442 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2443 ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2445 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2447 ct_entry_t *ct = arg;
2448 sentstatus = ct->ct_flags & CT_SENDSTATUS;
2449 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2451 * We *ought* to be able to get back to the original ATIO
2452 * here, but for some reason this gets lost. It's just as
2453 * well because it's squirrelled away as part of periph
2456 * We can live without it as long as we continue to use
2457 * the auto-replenish feature for CTIOs.
2459 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2460 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2464 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2466 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2467 } else if (ct->ct_status & QLTM_SVALID) {
2468 char *sp = (char *)ct;
2469 sp += CTIO_SENSE_OFFSET;
2470 ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2471 ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2472 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2474 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2475 resid = ct->ct_resid;
2477 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2478 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2479 tval = ct->ct_fwhandle;
2481 ccb->csio.resid += resid;
2484 * We're here either because intermediate data transfers are done
2485 * and/or the final status CTIO (which may have joined with a
2486 * Data Transfer) is done.
2488 * In any case, for this platform, the upper layers figure out
2489 * what to do next, so all we do here is collect status and
2490 * pass information along. Any DMA handles have already been
2493 if (notify_cam == 0) {
2494 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
2498 rls_lun_statep(isp, tptr);
2500 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? " FINAL " : "MIDTERM ", tval);
2502 if (!ok && !IS_24XX(isp)) {
2503 isp_target_putback_atio(ccb);
2505 isp_complete_ctio(ccb);
2510 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2512 (void) isp_notify_ack(isp, inot);
2516 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2519 switch (inp->in_status) {
2520 case IN_PORT_LOGOUT:
2522 * XXX: Need to delete this initiator's WWN from the database
2523 * XXX: Need to send this LOGOUT upstream
2525 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2527 case IN_PORT_CHANGED:
2528 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2530 case IN_GLOBAL_LOGO:
2531 isp_del_all_wwn_entries(isp, 0);
2532 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2540 atio_private_data_t *atp;
2542 struct ccb_immediate_notify *inot = NULL;
2544 if (ISP_CAP_SCCFW(isp)) {
2545 lun = inp->in_scclun;
2549 if (ISP_CAP_2KLOGIN(isp)) {
2550 loopid = ((in_fcentry_e_t *)inp)->in_iid;
2552 loopid = inp->in_iid;
2554 if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2559 tptr = get_lun_statep(isp, 0, lun);
2561 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2563 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2567 atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2570 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2571 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2574 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2575 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2577 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2580 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2583 isp_notify_t tmp, *nt = &tmp;
2584 ISP_MEMZERO(nt, sizeof (isp_notify_t));
2586 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2588 nt->nt_nphdl = loopid;
2589 nt->nt_sid = PORT_ANY;
2590 nt->nt_did = PORT_ANY;
2592 nt->nt_need_ack = 1;
2594 nt->nt_ncode = NT_ABORT_TASK;
2595 nt->nt_lreserved = inot;
2596 isp_handle_platform_target_tmf(isp, nt);
2599 rls_lun_statep(isp, tptr);
2606 (void) isp_notify_ack(isp, inp);
2611 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2616 uint8_t *ptr = NULL;
2619 nphdl = inot->in_nphdl;
2620 if (nphdl != NIL_HANDLE) {
2621 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2626 switch (inot->in_status) {
2627 case IN24XX_ELS_RCVD:
2630 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2633 * Note that we're just getting notification that an ELS was received
2634 * (possibly with some associated information sent upstream). This is
2635 * *not* the same as being given the ELS frame to accept or reject.
2637 switch (inot->in_status_subcode) {
2640 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2641 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2642 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
2643 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2644 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2645 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2646 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2647 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2648 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
2649 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2653 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2661 * Treat PRLI the same as PLOGI and make a database entry for it.
2663 if (inot->in_status_subcode == PLOGI)
2667 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2668 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2669 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
2670 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2671 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2672 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2673 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2674 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2675 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
2676 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2680 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2689 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2693 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2694 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);
2697 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,
2698 inot->in_rxid, inot->in_oxid);
2699 (void) isp_notify_ack(isp, inot);
2703 case IN24XX_PORT_LOGOUT:
2704 ptr = "PORT LOGOUT";
2705 if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2706 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2709 case IN24XX_PORT_CHANGED:
2711 ptr = "PORT CHANGED";
2714 case IN24XX_LIP_RESET:
2718 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);
2721 * All subcodes here are irrelevant. What is relevant
2722 * is that we need to terminate all active commands from
2723 * this initiator (known by N-port handle).
2725 /* XXX IMPLEMENT XXX */
2726 (void) isp_notify_ack(isp, inot);
2729 case IN24XX_LINK_RESET:
2730 case IN24XX_LINK_FAILED:
2731 case IN24XX_SRR_RCVD:
2733 (void) isp_notify_ack(isp, inot);
2739 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2742 if (isp->isp_state != ISP_RUNSTATE) {
2743 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2748 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2750 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2751 ct7_entry_t local, *cto = &local;
2752 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2757 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2758 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2763 ISP_MEMZERO(&local, sizeof (local));
2764 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2765 cto->ct_header.rqs_entry_count = 1;
2766 cto->ct_nphdl = nphdl;
2767 cto->ct_rxid = aep->at_rxid;
2768 cto->ct_vpidx = mp->nt_channel;
2769 cto->ct_iid_lo = sid;
2770 cto->ct_iid_hi = sid >> 16;
2771 cto->ct_oxid = aep->at_hdr.ox_id;
2772 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2773 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2774 return (isp_target_put_entry(isp, &local));
2778 * This case is for a responding to an ABTS frame
2780 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2783 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2785 if (mp->nt_need_ack) {
2786 uint8_t storage[QENTRY_LEN];
2787 ct7_entry_t *cto = (ct7_entry_t *) storage;
2788 abts_t *abts = (abts_t *)mp->nt_lreserved;
2790 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2791 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2792 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2793 cto->ct_header.rqs_entry_count = 1;
2794 cto->ct_nphdl = mp->nt_nphdl;
2795 cto->ct_rxid = abts->abts_rxid_task;
2796 cto->ct_iid_lo = mp->nt_sid;
2797 cto->ct_iid_hi = mp->nt_sid >> 16;
2798 cto->ct_oxid = abts->abts_ox_id;
2799 cto->ct_vpidx = mp->nt_channel;
2800 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2801 if (isp_target_put_entry(isp, cto)) {
2804 mp->nt_need_ack = 0;
2806 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2814 * Handle logout cases here
2816 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2817 isp_del_all_wwn_entries(isp, mp->nt_channel);
2820 if (mp->nt_ncode == NT_LOGOUT) {
2821 if (!IS_2100(isp) && IS_FC(isp)) {
2822 isp_del_wwn_entries(isp, mp);
2827 * General purpose acknowledgement
2829 if (mp->nt_need_ack) {
2830 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2831 return (isp_notify_ack(isp, mp->nt_lreserved));
2837 * Handle task management functions.
2839 * We show up here with a notify structure filled out.
2841 * The nt_lreserved tag points to the original queue entry
2844 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2848 struct ccb_immediate_notify *inot;
2849 inot_private_data_t *ntp = NULL;
2852 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2853 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2855 * NB: This assignment is necessary because of tricky type conversion.
2856 * XXX: This is tricky and I need to check this. If the lun isn't known
2857 * XXX: for the task management function, it does not of necessity follow
2858 * XXX: that it should go up stream to the wildcard listener.
2860 if (notify->nt_lun == LUN_ANY) {
2861 lun = CAM_LUN_WILDCARD;
2863 lun = notify->nt_lun;
2865 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2867 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2869 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2873 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2875 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2879 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2880 inot->initiator_id = CAM_TARGET_WILDCARD;
2882 inot->initiator_id = lp->handle;
2884 inot->seq_id = notify->nt_tagval;
2885 inot->tag_id = notify->nt_tagval >> 32;
2887 switch (notify->nt_ncode) {
2889 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2890 inot->arg = MSG_ABORT_TASK;
2892 case NT_ABORT_TASK_SET:
2893 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2894 inot->arg = MSG_ABORT_TASK_SET;
2897 inot->arg = MSG_CLEAR_ACA;
2899 case NT_CLEAR_TASK_SET:
2900 inot->arg = MSG_CLEAR_TASK_SET;
2903 inot->arg = MSG_LOGICAL_UNIT_RESET;
2905 case NT_TARGET_RESET:
2906 inot->arg = MSG_TARGET_RESET;
2909 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);
2913 ntp = isp_get_ntpd(isp, tptr);
2915 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2918 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2919 if (notify->nt_lreserved) {
2920 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2921 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2923 ntp->rd.seq_id = notify->nt_tagval;
2924 ntp->rd.tag_id = notify->nt_tagval >> 32;
2927 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2928 rls_lun_statep(isp, tptr);
2929 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2930 inot->ccb_h.status = CAM_MESSAGE_RECV;
2931 xpt_done((union ccb *)inot);
2935 rls_lun_statep(isp, tptr);
2937 if (notify->nt_need_ack && notify->nt_lreserved) {
2938 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2939 (void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
2941 (void) isp_notify_ack(isp, notify->nt_lreserved);
2947 * Find the associated private data and mark it as dead so
2948 * we don't try to work on it any further.
2951 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
2954 atio_private_data_t *atp;
2956 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
2958 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
2960 ccb->ccb_h.status = CAM_REQ_INVALID;
2965 atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
2967 ccb->ccb_h.status = CAM_REQ_INVALID;
2971 ccb->ccb_h.status = CAM_REQ_CMP;
2975 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
2977 atio_private_data_t *atp;
2978 inot_private_data_t *restart_queue = tptr->restart_queue;
2981 * First, clean any commands pending restart
2983 tptr->restart_queue = NULL;
2984 while (restart_queue) {
2985 uint32_t this_tag_id;
2986 inot_private_data_t *ntp = restart_queue;
2988 restart_queue = ntp->rd.nt.nt_hba;
2991 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
2993 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
2995 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2996 isp_put_ntpd(isp, tptr, ntp);
2998 ntp->rd.nt.nt_hba = tptr->restart_queue;
2999 tptr->restart_queue = ntp;
3004 * Now mark other ones dead as well.
3006 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3007 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3014 #ifdef ISP_INTERNAL_TARGET
3015 // #define ISP_FORCE_TIMEOUT 1
3016 // #define ISP_TEST_WWNS 1
3017 // #define ISP_TEST_SEPARATE_STATUS 1
3019 #define ccb_data_offset ppriv_field0
3020 #define ccb_atio ppriv_ptr1
3021 #define ccb_inot ppriv_ptr1
3023 #define MAX_ISP_TARG_TRANSFER (2 << 20)
3024 #define NISP_TARG_CMDS 1024
3025 #define NISP_TARG_NOTIFIES 1024
3026 #define DISK_SHIFT 9
3027 #define JUNK_SIZE 256
3030 #define VERIFY_10 0x2f
3033 TAILQ_HEAD(ccb_queue, ccb_hdr);
3034 extern u_int vm_kmem_size;
3036 static uint32_t disk_size;
3037 static uint8_t *disk_data = NULL;
3038 static uint8_t *junk_data;
3039 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3040 struct isptarg_softc {
3041 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3042 struct ccb_queue work_queue;
3043 struct ccb_queue rework_queue;
3044 struct ccb_queue running_queue;
3045 struct ccb_queue inot_queue;
3046 struct cam_periph *periph;
3047 struct cam_path *path;
3050 static periph_ctor_t isptargctor;
3051 static periph_dtor_t isptargdtor;
3052 static periph_start_t isptargstart;
3053 static periph_init_t isptarginit;
3054 static void isptarg_done(struct cam_periph *, union ccb *);
3055 static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3058 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3060 static struct periph_driver isptargdriver =
3062 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3071 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3073 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3075 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__,
3076 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3077 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3078 ack->ccb_h.flags = 0;
3079 ack->ccb_h.retry_count = 0;
3080 ack->ccb_h.cbfcnp = isptarg_done;
3081 ack->ccb_h.timeout = 0;
3082 ack->ccb_h.ccb_inot = inot;
3083 ack->tag_id = inot->tag_id;
3084 ack->seq_id = inot->seq_id;
3085 ack->initiator_id = inot->initiator_id;
3090 isptargstart(struct cam_periph *periph, union ccb *iccb)
3092 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3093 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3094 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3095 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3096 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3099 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3100 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3101 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3102 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3103 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3106 int i, more = 0, last;
3107 struct isptarg_softc *softc = periph->softc;
3108 struct ccb_scsiio *csio;
3109 lun_id_t return_lun;
3110 struct ccb_accept_tio *atio;
3111 uint8_t *cdb, *ptr, status;
3113 uint32_t data_len, flags;
3114 struct ccb_hdr *ccbh;
3116 KKASSERT(lockstatus(periph->sim->mtx, curthread) != 0);
3117 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,
3118 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3120 * Check for immediate notifies first
3122 ccbh = TAILQ_FIRST(&softc->inot_queue);
3124 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3125 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3126 xpt_schedule(periph, 1);
3128 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3133 * Check the rework (continuation) work queue first.
3135 ccbh = TAILQ_FIRST(&softc->rework_queue);
3137 atio = (struct ccb_accept_tio *)ccbh;
3138 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3139 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3141 ccbh = TAILQ_FIRST(&softc->work_queue);
3143 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3144 xpt_release_ccb(iccb);
3147 atio = (struct ccb_accept_tio *)ccbh;
3148 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3149 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3150 atio->ccb_h.ccb_data_offset = 0;
3153 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3160 status = SCSI_STATUS_OK;
3161 flags = CAM_SEND_STATUS;
3162 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3163 cdb = atio->cdb_io.cdb_bytes;
3164 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,
3165 cdb[0], atio->ccb_h.ccb_data_offset);
3167 return_lun = XS_LUN(atio);
3168 if (return_lun != 0) {
3169 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3170 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3171 status = SCSI_STATUS_CHECK_COND;
3172 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3173 atio->sense_data.add_sense_code = 0x25;
3174 atio->sense_data.add_sense_code_qual = 0x0;
3175 atio->sense_len = sizeof (atio->sense_data);
3177 return_lun = CAM_LUN_WILDCARD;
3182 flags |= CAM_DIR_IN;
3183 data_len = sizeof (atio->sense_data);
3184 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3185 memset(junk_data+1, 0, data_len-1);
3186 if (data_len > cdb[4]) {
3190 data_ptr = junk_data;
3197 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3198 status = SCSI_STATUS_CHECK_COND;
3199 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3200 atio->sense_data.add_sense_code = 0x5;
3201 atio->sense_data.add_sense_code_qual = 0x24;
3202 atio->sense_len = sizeof (atio->sense_data);
3204 #ifdef ISP_FORCE_TIMEOUT
3209 xpt_schedule(periph, 1);
3216 #ifdef ISP_TEST_SEPARATE_STATUS
3217 if (last && data_len) {
3222 flags &= ~CAM_SEND_STATUS;
3225 atio->ccb_h.ccb_data_offset += data_len;
3226 flags |= CAM_DIR_IN;
3228 flags |= CAM_DIR_NONE;
3236 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3237 status = SCSI_STATUS_CHECK_COND;
3238 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3239 atio->sense_data.add_sense_code = 0x5;
3240 atio->sense_data.add_sense_code_qual = 0x24;
3241 atio->sense_len = sizeof (atio->sense_data);
3243 #ifdef ISP_FORCE_TIMEOUT
3248 xpt_schedule(periph, 1);
3255 #ifdef ISP_TEST_SEPARATE_STATUS
3256 if (last && data_len) {
3261 flags &= ~CAM_SEND_STATUS;
3264 atio->ccb_h.ccb_data_offset += data_len;
3265 flags |= CAM_DIR_OUT;
3267 flags |= CAM_DIR_NONE;
3272 flags |= CAM_DIR_IN;
3273 if (cdb[1] || cdb[2] || cdb[3]) {
3274 status = SCSI_STATUS_CHECK_COND;
3275 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3276 atio->sense_data.add_sense_code = 0x5;
3277 atio->sense_data.add_sense_code_qual = 0x20;
3278 atio->sense_len = sizeof (atio->sense_data);
3281 data_len = sizeof (iqd);
3282 if (data_len > cdb[4]) {
3286 if (XS_LUN(iccb) != 0) {
3287 memcpy(junk_data, niliqd, sizeof (iqd));
3289 memcpy(junk_data, iqd, sizeof (iqd));
3291 data_ptr = junk_data;
3294 case TEST_UNIT_READY:
3295 flags |= CAM_DIR_NONE;
3298 status = SCSI_STATUS_CHECK_COND;
3299 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3300 atio->sense_data.add_sense_code = 0x28;
3301 atio->sense_data.add_sense_code_qual = 0x0;
3302 atio->sense_len = sizeof (atio->sense_data);
3305 case SYNCHRONIZE_CACHE:
3310 flags |= CAM_DIR_NONE;
3314 flags |= CAM_DIR_IN;
3315 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3316 status = SCSI_STATUS_CHECK_COND;
3317 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3318 atio->sense_data.add_sense_code = 0x5;
3319 atio->sense_data.add_sense_code_qual = 0x24;
3320 atio->sense_len = sizeof (atio->sense_data);
3323 if (cdb[8] & 0x1) { /* PMI */
3324 junk_data[0] = 0xff;
3325 junk_data[1] = 0xff;
3326 junk_data[2] = 0xff;
3327 junk_data[3] = 0xff;
3329 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3330 if (last_blk < 0xffffffffULL) {
3331 junk_data[0] = (last_blk >> 24) & 0xff;
3332 junk_data[1] = (last_blk >> 16) & 0xff;
3333 junk_data[2] = (last_blk >> 8) & 0xff;
3334 junk_data[3] = (last_blk) & 0xff;
3336 junk_data[0] = 0xff;
3337 junk_data[1] = 0xff;
3338 junk_data[2] = 0xff;
3339 junk_data[3] = 0xff;
3342 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3343 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3344 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3345 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3346 data_ptr = junk_data;
3350 flags |= CAM_DIR_IN;
3351 memset(junk_data, 0, JUNK_SIZE);
3352 junk_data[0] = (1 << 3) >> 24;
3353 junk_data[1] = (1 << 3) >> 16;
3354 junk_data[2] = (1 << 3) >> 8;
3355 junk_data[3] = (1 << 3);
3357 for (i = 0; i < 1; i++) {
3358 ptr = &junk_data[8 + (1 << 3)];
3360 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3364 data_ptr = junk_data;
3365 data_len = (ptr + 8) - junk_data;
3369 flags |= CAM_DIR_NONE;
3370 status = SCSI_STATUS_CHECK_COND;
3371 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3372 atio->sense_data.add_sense_code = 0x5;
3373 atio->sense_data.add_sense_code_qual = 0x20;
3374 atio->sense_len = sizeof (atio->sense_data);
3379 * If we are done with the transaction, tell the
3380 * controller to send status and perform a CMD_CMPLT.
3381 * If we have associated sense data, see if we can
3384 if (status == SCSI_STATUS_CHECK_COND) {
3385 flags |= CAM_SEND_SENSE;
3386 csio->sense_len = atio->sense_len;
3387 csio->sense_data = atio->sense_data;
3388 flags &= ~CAM_DIR_MASK;
3392 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3393 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3394 iccb->ccb_h.target_lun = return_lun;
3395 iccb->ccb_h.ccb_atio = atio;
3398 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3399 cam_release_devq(periph->path, 0, 0, 0, 0);
3400 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3403 xpt_schedule(periph, 1);
3408 isptargctor(struct cam_periph *periph, void *arg)
3410 struct isptarg_softc *softc;
3412 softc = (struct isptarg_softc *)arg;
3413 periph->softc = softc;
3414 softc->periph = periph;
3415 softc->path = periph->path;
3416 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3417 return (CAM_REQ_CMP);
3421 isptargdtor(struct cam_periph *periph)
3423 struct isptarg_softc *softc;
3424 softc = (struct isptarg_softc *)periph->softc;
3425 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3426 softc->periph = NULL;
3428 periph->softc = NULL;
3432 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3434 struct isptarg_softc *softc;
3436 struct ccb_accept_tio *atio;
3437 struct ccb_immediate_notify *inot;
3440 softc = (struct isptarg_softc *)periph->softc;
3442 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3444 switch (ccb->ccb_h.func_code) {
3445 case XPT_ACCEPT_TARGET_IO:
3446 atio = (struct ccb_accept_tio *) ccb;
3447 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3448 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3449 xpt_schedule(periph, 1);
3451 case XPT_IMMEDIATE_NOTIFY:
3452 inot = (struct ccb_immediate_notify *) ccb;
3453 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__);
3454 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3455 xpt_schedule(periph, 1);
3457 case XPT_CONT_TARGET_IO:
3458 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3459 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3460 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3462 atio = ccb->ccb_h.ccb_atio;
3463 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3464 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3465 xpt_action((union ccb *)atio);
3466 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3467 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3468 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3469 xpt_schedule(periph, 1);
3471 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3472 xpt_action((union ccb *)atio);
3474 xpt_release_ccb(ccb);
3476 case XPT_NOTIFY_ACKNOWLEDGE:
3477 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3478 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3479 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3481 inot = ccb->ccb_h.ccb_inot;
3482 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);
3483 xpt_release_ccb(ccb);
3484 xpt_action((union ccb *)inot);
3487 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3493 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3495 struct ac_contract *acp = arg;
3496 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3498 if (code != AC_CONTRACT) {
3501 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3505 isp_target_thread(ispsoftc_t *isp, int chan)
3507 union ccb *ccb = NULL;
3511 struct isptarg_softc *softc = NULL;
3512 struct cam_periph *periph = NULL, *wperiph = NULL;
3513 struct cam_path *path, *wpath;
3514 struct cam_sim *sim;
3516 if (disk_data == NULL) {
3517 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3518 if (disk_size < (50 << 20)) {
3519 disk_size = 50 << 20;
3521 disk_data = kmalloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3522 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3524 junk_data = kmalloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3527 softc = kmalloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3528 TAILQ_INIT(&softc->work_queue);
3529 TAILQ_INIT(&softc->rework_queue);
3530 TAILQ_INIT(&softc->running_queue);
3531 TAILQ_INIT(&softc->inot_queue);
3534 periphdriver_register(&isptargdriver);
3535 ISP_GET_PC(isp, chan, sim, sim);
3536 ISP_GET_PC(isp, chan, path, path);
3537 status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3538 if (status != CAM_REQ_CMP) {
3539 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3542 status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3543 if (status != CAM_REQ_CMP) {
3544 xpt_free_path(wpath);
3545 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3549 ccb = xpt_alloc_ccb();
3552 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3553 if (status != CAM_REQ_CMP) {
3555 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3558 wperiph = cam_periph_find(wpath, "isptarg");
3559 if (wperiph == NULL) {
3561 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3565 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3566 if (status != CAM_REQ_CMP) {
3568 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3572 periph = cam_periph_find(path, "isptarg");
3573 if (periph == NULL) {
3575 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3579 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3580 if (status != CAM_REQ_CMP) {
3582 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3588 ccb = xpt_alloc_ccb();
3591 * Make sure role is none.
3593 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3594 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3595 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3596 #ifdef ISP_TEST_WWNS
3597 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3598 ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3599 ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3601 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3611 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3612 ccb->ccb_h.func_code = XPT_EN_LUN;
3613 ccb->cel.enable = 1;
3617 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3619 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3623 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3624 ccb->ccb_h.func_code = XPT_EN_LUN;
3625 ccb->cel.enable = 1;
3629 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3631 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3639 ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3640 for (i = 0; i < 4; i++) {
3641 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3642 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3643 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3644 ccb->ccb_h.cbfcnp = isptarg_done;
3649 for (i = 0; i < NISP_TARG_CMDS; i++) {
3650 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3651 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3652 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3653 ccb->ccb_h.cbfcnp = isptarg_done;
3658 for (i = 0; i < 4; i++) {
3659 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3660 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3661 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3662 ccb->ccb_h.cbfcnp = isptarg_done;
3667 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3668 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3669 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3670 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3671 ccb->ccb_h.cbfcnp = isptarg_done;
3678 * Now turn it all back on
3680 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3681 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3682 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3683 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3689 * Okay, while things are still active, sleep...
3693 ISP_GET_PC(isp, chan, proc_active, i);
3697 lksleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3703 cam_periph_invalidate(wperiph);
3706 cam_periph_invalidate(periph);
3709 kfree(junk_data, M_ISPTARG);
3712 kfree(disk_data, M_ISPTARG);
3715 kfree(softc, M_ISPTARG);
3717 xpt_free_path(path);
3718 xpt_free_path(wpath);
3722 isp_target_thread_pi(void *arg)
3724 struct isp_spi *pi = arg;
3725 isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3729 isp_target_thread_fc(void *arg)
3731 struct isp_fc *fc = arg;
3732 isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3736 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3738 uint32_t cnt, curcnt;
3744 cnt = (((uint32_t)cdb[10]) << 24) |
3745 (((uint32_t)cdb[11]) << 16) |
3746 (((uint32_t)cdb[12]) << 8) |
3747 ((uint32_t)cdb[13]);
3749 lba = (((uint64_t)cdb[2]) << 56) |
3750 (((uint64_t)cdb[3]) << 48) |
3751 (((uint64_t)cdb[4]) << 40) |
3752 (((uint64_t)cdb[5]) << 32) |
3753 (((uint64_t)cdb[6]) << 24) |
3754 (((uint64_t)cdb[7]) << 16) |
3755 (((uint64_t)cdb[8]) << 8) |
3760 cnt = (((uint32_t)cdb[6]) << 16) |
3761 (((uint32_t)cdb[7]) << 8) |
3762 ((u_int32_t)cdb[8]);
3764 lba = (((uint32_t)cdb[2]) << 24) |
3765 (((uint32_t)cdb[3]) << 16) |
3766 (((uint32_t)cdb[4]) << 8) |
3771 cnt = (((uint32_t)cdb[7]) << 8) |
3772 ((u_int32_t)cdb[8]);
3774 lba = (((uint32_t)cdb[2]) << 24) |
3775 (((uint32_t)cdb[3]) << 16) |
3776 (((uint32_t)cdb[4]) << 8) |
3785 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
3786 (((uint32_t)cdb[2]) << 8) |
3796 if (offset == cnt) {
3801 if (lba + cnt > dl) {
3806 curcnt = MAX_ISP_TARG_TRANSFER;
3807 if (offset + curcnt >= cnt) {
3808 curcnt = cnt - offset;
3814 *kp = &dp[lba + offset];
3822 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3824 struct cam_sim *sim;
3828 sim = (struct cam_sim *)cbarg;
3829 isp = (ispsoftc_t *) cam_sim_softc(sim);
3830 bus = cam_sim_bus(sim);
3831 tgt = xpt_path_target_id(path);
3834 case AC_LOST_DEVICE:
3836 uint16_t oflags, nflags;
3837 sdparam *sdp = SDPARAM(isp, bus);
3840 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3841 #ifndef ISP_TARGET_MODE
3842 nflags &= DPARM_SAFE_DFLT;
3843 if (isp->isp_loaded_fw) {
3844 nflags |= DPARM_NARROW | DPARM_ASYNC;
3847 nflags = DPARM_DEFAULT;
3849 oflags = sdp->isp_devparam[tgt].goal_flags;
3850 sdp->isp_devparam[tgt].goal_flags = nflags;
3851 sdp->isp_devparam[tgt].dev_update = 1;
3853 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3854 sdp->isp_devparam[tgt].goal_flags = oflags;
3859 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3865 isp_poll(struct cam_sim *sim)
3867 ispsoftc_t *isp = cam_sim_softc(sim);
3869 uint16_t sema, mbox;
3871 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3872 isp_intr(isp, isr, sema, mbox);
3878 isp_watchdog(void *arg)
3880 struct ccb_scsiio *xs = arg;
3882 uint32_t ohandle = ISP_HANDLE_FREE, handle;
3887 handle = isp_find_handle(isp, xs);
3889 if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3890 isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3891 callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3899 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3901 if (handle != ISP_HANDLE_FREE) {
3903 uint16_t sema, mbox;
3904 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3905 isp_intr(isp, isr, sema, mbox);
3908 handle = isp_find_handle(isp, xs);
3910 if (handle != ISP_HANDLE_FREE) {
3912 * Try and make sure the command is really dead before
3913 * we release the handle (and DMA resources) for reuse.
3915 * If we are successful in aborting the command then
3916 * we're done here because we'll get the command returned
3919 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3925 * Note that after calling the above, the command may in
3926 * fact have been completed.
3928 xs = isp_find_xs(isp, handle);
3931 * If the command no longer exists, then we won't
3932 * be able to find the xs again with this handle.
3940 * After this point, the command is really dead.
3942 if (XS_XFRLEN(xs)) {
3943 ISP_DMAFREE(isp, xs, handle);
3945 isp_destroy_handle(isp, handle);
3946 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3947 XS_SETERR(xs, CAM_CMD_TIMEOUT);
3948 isp_prt_endcmd(isp, xs);
3951 if (ohandle != ISP_HANDLE_FREE) {
3952 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3954 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3961 isp_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
3963 if (ccb->ccb_h.status != CAM_REQ_CMP)
3964 kprintf("cam_scan_callback: failure status = %x\n",
3967 xpt_free_path(ccb->ccb_h.path);
3972 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3975 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3977 if (isp_autoconfig == 0) {
3982 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3984 ccb = xpt_alloc_ccb();
3986 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3989 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3990 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3994 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
3995 ccb->ccb_h.func_code = XPT_SCAN_BUS;
3996 ccb->ccb_h.cbfcnp = isp_bus_scan_cb;
3997 ccb->crcn.flags = CAM_FLAG_NONE;
4002 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
4004 struct cam_path *tp;
4005 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4007 if (isp_autoconfig == 0) {
4010 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4011 xpt_async(AC_LOST_DEVICE, tp, NULL);
4017 * Gone Device Timer Function- when we have decided that a device has gone
4018 * away, we wait a specific period of time prior to telling the OS it has
4021 * This timer function fires once a second and then scans the port database
4022 * for devices that are marked dead but still have a virtual target assigned.
4023 * We decrement a counter for that port database entry, and when it hits zero,
4024 * we tell the OS the device has gone away.
4029 struct isp_fc *fc = arg;
4032 taskqueue_enqueue(taskqueue_swi, &fc->gtask);
4033 ISP_UNLOCK(fc->isp);
4037 isp_gdt_task(void *arg, int pending)
4039 struct isp_fc *fc = arg;
4040 ispsoftc_t *isp = fc->isp;
4041 int chan = fc - isp->isp_osinfo.pc.fc;
4043 int dbidx, tgt, more_to_do = 0;
4046 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4047 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4048 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4050 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4053 if (lp->dev_map_idx == 0 || lp->target_mode) {
4056 if (lp->gone_timer != 0) {
4057 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);
4058 lp->gone_timer -= 1;
4062 tgt = lp->dev_map_idx - 1;
4063 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4064 lp->dev_map_idx = 0;
4065 lp->state = FC_PORTDB_STATE_NIL;
4066 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4067 isp_make_gone(isp, chan, tgt);
4071 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4073 callout_deactivate(&fc->gdt);
4074 isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_second);
4081 * Loop Down Timer Function- when loop goes down, a timer is started and
4082 * and after it expires we come here and take all probational devices that
4083 * the OS knows about and the tell the OS that they've gone away.
4085 * We don't clear the devices out of our port database because, when loop
4086 * come back up, we have to do some actual cleanup with the chip at that
4087 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4092 struct isp_fc *fc = arg;
4095 taskqueue_enqueue(taskqueue_swi, &fc->ltask);
4096 ISP_UNLOCK(fc->isp);
4100 isp_ldt_task(void *arg, int pending)
4102 struct isp_fc *fc = arg;
4103 ispsoftc_t *isp = fc->isp;
4104 int chan = fc - isp->isp_osinfo.pc.fc;
4109 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_second);
4110 callout_deactivate(&fc->ldt);
4113 * Notify to the OS all targets who we now consider have departed.
4115 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4116 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4118 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4121 if (lp->dev_map_idx == 0 || lp->target_mode) {
4126 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4130 for (i = 0; i < isp->isp_maxcmds; i++) {
4131 struct ccb_scsiio *xs;
4133 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4136 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4139 if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4142 isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4143 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4147 * Mark that we've announced that this device is gone....
4152 * but *don't* change the state of the entry. Just clear
4153 * any target id stuff and announce to CAM that the
4154 * device is gone. This way any necessary PLOGO stuff
4155 * will happen when loop comes back up.
4158 tgt = lp->dev_map_idx - 1;
4159 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4160 lp->dev_map_idx = 0;
4161 lp->state = FC_PORTDB_STATE_NIL;
4162 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4163 isp_make_gone(isp, chan, tgt);
4166 if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4167 isp_unfreeze_loopdown(isp, chan);
4170 * The loop down timer has expired. Wake up the kthread
4171 * to notice that fact (or make it false).
4174 fc->loop_down_time = fc->loop_down_limit+1;
4180 isp_kthread(void *arg)
4182 struct isp_fc *fc = arg;
4183 ispsoftc_t *isp = fc->isp;
4184 int chan = fc - isp->isp_osinfo.pc.fc;
4187 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4192 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4193 lb = isp_fc_runstate(isp, chan, 250000);
4196 * Our action is different based upon whether we're supporting
4197 * Initiator mode or not. If we are, we might freeze the simq
4198 * when loop is down and set all sorts of different delays to
4201 * If not, we simply just wait for loop to come up.
4203 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4205 * Increment loop down time by the last sleep interval
4207 fc->loop_down_time += slp;
4210 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4212 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4216 * If we've never seen loop up and we've waited longer
4217 * than quickboot time, or we've seen loop up but we've
4218 * waited longer than loop_down_limit, give up and go
4219 * to sleep until loop comes up.
4221 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4222 lim = isp_quickboot_time;
4224 lim = fc->loop_down_limit;
4226 if (fc->loop_down_time >= lim) {
4227 isp_freeze_loopdown(isp, chan, "loop limit hit");
4229 } else if (fc->loop_down_time < 10) {
4231 } else if (fc->loop_down_time < 30) {
4233 } else if (fc->loop_down_time < 60) {
4235 } else if (fc->loop_down_time < 120) {
4242 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4243 fc->loop_down_time += slp;
4246 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4247 fc->loop_down_time = 0;
4253 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4254 * now so that CAM can start sending us commands.
4256 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4257 * or kill the commands, as appropriate.
4260 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4261 isp_unfreeze_loopdown(isp, chan);
4264 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4266 lksleep(fc, &isp->isp_osinfo.lock, 0, "ispf", slp * hz);
4269 * If slp is zero, we're waking up for the first time after
4270 * things have been okay. In this case, we set a deferral state
4271 * for all commands and delay hysteresis seconds before starting
4272 * the FC state evaluation. This gives the loop/fabric a chance
4275 if (slp == 0 && fc->hysteresis) {
4276 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4277 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4278 tsleep(isp_kthread, 0, "ispt", fc->hysteresis * hz);
4279 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4282 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4286 isp_action(struct cam_sim *sim, union ccb *ccb)
4288 int bus, tgt, ts, error, lim;
4290 struct ccb_trans_settings *cts;
4292 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4294 isp = (ispsoftc_t *)cam_sim_softc(sim);
4295 KKASSERT(lockstatus(&isp->isp_lock, curthread) != 0);
4297 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4299 if (isp->isp_state != ISP_INITSTATE) {
4301 * Lie. Say it was a selection timeout.
4303 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4304 xpt_freeze_devq(ccb->ccb_h.path, 1);
4308 isp->isp_state = ISP_RUNSTATE;
4310 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4311 ISP_PCMD(ccb) = NULL;
4313 switch (ccb->ccb_h.func_code) {
4314 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4315 bus = XS_CHANNEL(ccb);
4317 * Do a couple of preliminary checks...
4319 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4320 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4321 ccb->ccb_h.status = CAM_REQ_INVALID;
4327 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4328 xpt_print(ccb->ccb_h.path, "invalid target\n");
4329 ccb->ccb_h.status = CAM_PATH_INVALID;
4330 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4331 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4332 ccb->ccb_h.status = CAM_PATH_INVALID;
4334 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4339 ccb->csio.scsi_status = SCSI_STATUS_OK;
4340 if (isp_get_pcmd(isp, ccb)) {
4341 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4342 cam_freeze_devq(ccb->ccb_h.path);
4343 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4347 error = isp_start((XS_T *) ccb);
4350 XS_CMD_S_CLEAR(ccb);
4351 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4352 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4355 ts = ccb->ccb_h.timeout;
4356 if (ts == CAM_TIME_DEFAULT) {
4359 ts = isp_mstohz(ts);
4361 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4365 * We get this result for FC devices if the loop state isn't ready yet
4366 * or if the device in question has gone zombie on us.
4368 * If we've never seen Loop UP at all, we requeue this request and wait
4369 * for the initial loop up delay to expire.
4371 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4372 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4373 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4374 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_second);
4376 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);
4378 ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4379 xpt_freeze_devq(ccb->ccb_h.path, 1);
4380 isp_free_pcmd(isp, ccb);
4384 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4385 cam_freeze_devq(ccb->ccb_h.path);
4386 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4387 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4388 isp_free_pcmd(isp, ccb);
4392 isp_free_pcmd(isp, ccb);
4393 cam_freeze_devq(ccb->ccb_h.path);
4394 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4395 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4399 isp_done((struct ccb_scsiio *) ccb);
4402 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4403 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4404 isp_free_pcmd(isp, ccb);
4409 #ifdef ISP_TARGET_MODE
4410 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
4411 if (ccb->cel.enable) {
4412 isp_enable_lun(isp, ccb);
4414 isp_disable_lun(isp, ccb);
4417 case XPT_IMMED_NOTIFY:
4418 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
4419 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
4421 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4423 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4429 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4430 str = "XPT_IMMEDIATE_NOTIFY";
4431 tag = ccb->cin1.seq_id;
4433 tag = ccb->atio.tag_id;
4434 str = "XPT_ACCEPT_TARGET_IO";
4436 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4437 dump_tstates(isp, XS_CHANNEL(ccb));
4438 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4441 ccb->ccb_h.sim_priv.entries[0].field = 0;
4442 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4443 ccb->ccb_h.flags = 0;
4445 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4446 if (ccb->atio.tag_id) {
4447 atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4449 isp_put_atpd(isp, tptr, atp);
4453 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4454 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4455 ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
4456 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4457 if (ccb->cin1.tag_id) {
4458 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4460 isp_put_ntpd(isp, tptr, ntp);
4464 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4465 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4466 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4467 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4469 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4470 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4471 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4473 rls_lun_statep(isp, tptr);
4474 ccb->ccb_h.status = CAM_REQ_INPROG;
4477 case XPT_NOTIFY_ACK:
4478 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4480 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
4483 inot_private_data_t *ntp;
4486 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4487 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4490 * All the relevant path information is in the associated immediate notify
4492 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);
4493 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4495 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__,
4496 ccb->cna2.tag_id, ccb->cna2.seq_id);
4497 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4501 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4502 rls_lun_statep(isp, tptr);
4503 cam_freeze_devq(ccb->ccb_h.path);
4504 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4505 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4508 isp_put_ntpd(isp, tptr, ntp);
4509 rls_lun_statep(isp, tptr);
4510 ccb->ccb_h.status = CAM_REQ_CMP;
4511 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);
4515 case XPT_CONT_TARGET_IO:
4516 isp_target_start_ctio(isp, ccb);
4519 case XPT_RESET_DEV: /* BDR the specified SCSI device */
4521 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4522 tgt = ccb->ccb_h.target_id;
4525 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4527 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4529 ccb->ccb_h.status = CAM_REQ_CMP;
4533 case XPT_ABORT: /* Abort the specified CCB */
4535 union ccb *accb = ccb->cab.abort_ccb;
4536 switch (accb->ccb_h.func_code) {
4537 #ifdef ISP_TARGET_MODE
4538 case XPT_ACCEPT_TARGET_IO:
4539 isp_target_mark_aborted(isp, accb);
4543 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4545 ccb->ccb_h.status = CAM_UA_ABORT;
4547 ccb->ccb_h.status = CAM_REQ_CMP;
4551 ccb->ccb_h.status = CAM_REQ_INVALID;
4555 * This is not a queued CCB, so the caller expects it to be
4556 * complete when control is returned.
4560 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
4561 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
4563 if (!IS_CURRENT_SETTINGS(cts)) {
4564 ccb->ccb_h.status = CAM_REQ_INVALID;
4568 tgt = cts->ccb_h.target_id;
4569 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4571 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4572 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4573 sdparam *sdp = SDPARAM(isp, bus);
4576 if (spi->valid == 0 && scsi->valid == 0) {
4577 ccb->ccb_h.status = CAM_REQ_CMP;
4583 * We always update (internally) from goal_flags
4584 * so any request to change settings just gets
4585 * vectored to that location.
4587 dptr = &sdp->isp_devparam[tgt].goal_flags;
4589 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4590 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4591 *dptr |= DPARM_DISC;
4593 *dptr &= ~DPARM_DISC;
4596 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4597 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4598 *dptr |= DPARM_TQING;
4600 *dptr &= ~DPARM_TQING;
4603 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4604 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4605 *dptr |= DPARM_WIDE;
4607 *dptr &= ~DPARM_WIDE;
4613 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4614 *dptr |= DPARM_SYNC;
4616 * XXX: CHECK FOR LEGALITY
4618 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4619 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4621 *dptr &= ~DPARM_SYNC;
4623 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,
4624 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4625 sdp->isp_devparam[tgt].dev_update = 1;
4628 ccb->ccb_h.status = CAM_REQ_CMP;
4631 case XPT_GET_TRAN_SETTINGS:
4633 tgt = cts->ccb_h.target_id;
4634 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4636 fcparam *fcp = FCPARAM(isp, bus);
4637 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4638 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4639 unsigned int hdlidx;
4641 cts->protocol = PROTO_SCSI;
4642 cts->protocol_version = SCSI_REV_2;
4643 cts->transport = XPORT_FC;
4644 cts->transport_version = 0;
4646 scsi->valid = CTS_SCSI_VALID_TQ;
4647 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4648 fc->valid = CTS_FC_VALID_SPEED;
4649 fc->bitrate = 100000;
4650 fc->bitrate *= fcp->isp_gbspeed;
4651 hdlidx = fcp->isp_dev_map[tgt] - 1;
4652 if (hdlidx < MAX_FC_TARG) {
4653 fcportdb_t *lp = &fcp->portdb[hdlidx];
4654 fc->wwnn = lp->node_wwn;
4655 fc->wwpn = lp->port_wwn;
4656 fc->port = lp->portid;
4657 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4660 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4661 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4662 sdparam *sdp = SDPARAM(isp, bus);
4663 uint16_t dval, pval, oval;
4665 if (IS_CURRENT_SETTINGS(cts)) {
4666 sdp->isp_devparam[tgt].dev_refresh = 1;
4668 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4669 dval = sdp->isp_devparam[tgt].actv_flags;
4670 oval = sdp->isp_devparam[tgt].actv_offset;
4671 pval = sdp->isp_devparam[tgt].actv_period;
4673 dval = sdp->isp_devparam[tgt].nvrm_flags;
4674 oval = sdp->isp_devparam[tgt].nvrm_offset;
4675 pval = sdp->isp_devparam[tgt].nvrm_period;
4678 cts->protocol = PROTO_SCSI;
4679 cts->protocol_version = SCSI_REV_2;
4680 cts->transport = XPORT_SPI;
4681 cts->transport_version = 2;
4687 if (dval & DPARM_DISC) {
4688 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4690 if ((dval & DPARM_SYNC) && oval && pval) {
4691 spi->sync_offset = oval;
4692 spi->sync_period = pval;
4694 spi->sync_offset = 0;
4695 spi->sync_period = 0;
4697 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4698 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4699 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4700 if (dval & DPARM_WIDE) {
4701 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4703 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4705 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4706 scsi->valid = CTS_SCSI_VALID_TQ;
4707 if (dval & DPARM_TQING) {
4708 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4710 spi->valid |= CTS_SPI_VALID_DISC;
4712 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4713 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4715 ccb->ccb_h.status = CAM_REQ_CMP;
4719 case XPT_CALC_GEOMETRY:
4720 cam_calc_geometry(&ccb->ccg, 1);
4724 case XPT_RESET_BUS: /* Reset the specified bus */
4725 bus = cam_sim_bus(sim);
4726 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4728 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4733 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4736 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4738 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4740 ccb->ccb_h.status = CAM_REQ_CMP;
4744 case XPT_TERM_IO: /* Terminate the I/O process */
4745 ccb->ccb_h.status = CAM_REQ_INVALID;
4750 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
4752 struct ccb_sim_knob *kp = &ccb->knob;
4757 ccb->ccb_h.status = CAM_REQ_INVALID;
4762 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4763 fcp = FCPARAM(isp, bus);
4765 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4766 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4767 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4768 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4770 ccb->ccb_h.status = CAM_REQ_CMP;
4771 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4775 switch (kp->xport_specific.fc.role) {
4776 case KNOB_ROLE_NONE:
4777 if (fcp->role != ISP_ROLE_NONE) {
4779 newrole = ISP_ROLE_NONE;
4782 case KNOB_ROLE_TARGET:
4783 if (fcp->role != ISP_ROLE_TARGET) {
4785 newrole = ISP_ROLE_TARGET;
4788 case KNOB_ROLE_INITIATOR:
4789 if (fcp->role != ISP_ROLE_INITIATOR) {
4791 newrole = ISP_ROLE_INITIATOR;
4794 case KNOB_ROLE_BOTH:
4796 if (fcp->role != ISP_ROLE_BOTH) {
4798 newrole = ISP_ROLE_BOTH;
4802 * We don't really support dual role at present on FC cards.
4804 * We should, but a bunch of things are currently broken,
4805 * so don't allow it.
4807 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4808 ccb->ccb_h.status = CAM_REQ_INVALID;
4813 if (isp_fc_change_role(isp, bus, newrole) != 0) {
4814 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4815 #ifdef ISP_TARGET_MODE
4816 } else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4817 isp_enable_deferred_luns(isp, bus);
4825 case XPT_GET_SIM_KNOB: /* Set SIM knobs */
4827 struct ccb_sim_knob *kp = &ccb->knob;
4832 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4833 fcp = FCPARAM(isp, bus);
4835 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4836 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4837 switch (fcp->role) {
4839 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4841 case ISP_ROLE_TARGET:
4842 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4844 case ISP_ROLE_INITIATOR:
4845 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4848 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4851 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4852 ccb->ccb_h.status = CAM_REQ_CMP;
4854 ccb->ccb_h.status = CAM_REQ_INVALID;
4860 case XPT_PATH_INQ: /* Path routing inquiry */
4862 struct ccb_pathinq *cpi = &ccb->cpi;
4864 cpi->version_num = 1;
4865 #ifdef ISP_TARGET_MODE
4866 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4868 cpi->target_sprt = 0;
4870 cpi->hba_eng_cnt = 0;
4871 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4872 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4873 cpi->bus_id = cam_sim_bus(sim);
4874 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4876 fcparam *fcp = FCPARAM(isp, bus);
4878 cpi->hba_misc = PIM_NOBUSRESET;
4881 * Because our loop ID can shift from time to time,
4882 * make our initiator ID out of range of our bus.
4884 cpi->initiator_id = cpi->max_target + 1;
4887 * Set base transfer capabilities for Fibre Channel, for this HBA.
4890 cpi->base_transfer_speed = 8000000;
4891 } else if (IS_24XX(isp)) {
4892 cpi->base_transfer_speed = 4000000;
4893 } else if (IS_23XX(isp)) {
4894 cpi->base_transfer_speed = 2000000;
4896 cpi->base_transfer_speed = 1000000;
4898 cpi->hba_inquiry = PI_TAG_ABLE;
4899 cpi->transport = XPORT_FC;
4900 cpi->transport_version = 0;
4901 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4902 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4903 cpi->xport_specific.fc.port = fcp->isp_portid;
4904 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4906 sdparam *sdp = SDPARAM(isp, bus);
4907 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4909 cpi->initiator_id = sdp->isp_initiator_id;
4910 cpi->base_transfer_speed = 3300;
4911 cpi->transport = XPORT_SPI;
4912 cpi->transport_version = 2;
4914 cpi->protocol = PROTO_SCSI;
4915 cpi->protocol_version = SCSI_REV_2;
4916 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4917 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4918 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4919 cpi->unit_number = cam_sim_unit(sim);
4920 cpi->ccb_h.status = CAM_REQ_CMP;
4925 ccb->ccb_h.status = CAM_REQ_INVALID;
4931 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4934 isp_done(XS_T *sccb)
4936 ispsoftc_t *isp = XS_ISP(sccb);
4940 XS_SETERR(sccb, CAM_REQ_CMP);
4942 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4943 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4944 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4945 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4947 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4951 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4952 status = sccb->ccb_h.status & CAM_STATUS_MASK;
4953 if (status != CAM_REQ_CMP) {
4954 if (status != CAM_SEL_TIMEOUT)
4955 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);
4956 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4957 sccb->ccb_h.status |= CAM_DEV_QFRZN;
4958 xpt_freeze_devq(sccb->ccb_h.path, 1);
4962 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4963 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4966 XS_CMD_S_DONE(sccb);
4967 if (XS_TACTIVE_P(sccb))
4968 callout_stop(&PISP_PCMD(sccb)->wdog);
4969 XS_CMD_S_CLEAR(sccb);
4970 isp_free_pcmd(isp, (union ccb *) sccb);
4971 xpt_done((union ccb *) sccb);
4975 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4978 static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
4979 static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4984 struct cam_path *tmppath;
4988 case ISPASYNC_NEW_TGT_PARAMS:
4990 struct ccb_trans_settings_scsi *scsi;
4991 struct ccb_trans_settings_spi *spi;
4994 struct ccb_trans_settings cts;
4996 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4998 __va_start(ap, cmd);
4999 bus = __va_arg(ap, int);
5000 tgt = __va_arg(ap, int);
5002 sdp = SDPARAM(isp, bus);
5004 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5005 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5008 flags = sdp->isp_devparam[tgt].actv_flags;
5009 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5010 cts.protocol = PROTO_SCSI;
5011 cts.transport = XPORT_SPI;
5013 scsi = &cts.proto_specific.scsi;
5014 spi = &cts.xport_specific.spi;
5016 if (flags & DPARM_TQING) {
5017 scsi->valid |= CTS_SCSI_VALID_TQ;
5018 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5021 if (flags & DPARM_DISC) {
5022 spi->valid |= CTS_SPI_VALID_DISC;
5023 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5025 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5026 if (flags & DPARM_WIDE) {
5027 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5029 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5031 if (flags & DPARM_SYNC) {
5032 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5033 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5034 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5035 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5037 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);
5038 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5039 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5040 xpt_free_path(tmppath);
5043 case ISPASYNC_BUS_RESET:
5045 __va_start(ap, cmd);
5046 bus = __va_arg(ap, int);
5048 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5050 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5052 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5058 msg = "LIP Received";
5061 case ISPASYNC_LOOP_RESET:
5066 case ISPASYNC_LOOP_DOWN:
5071 __va_start(ap, cmd);
5072 bus = __va_arg(ap, int);
5075 FCPARAM(isp, bus)->link_active = 0;
5077 fc = ISP_FC_PC(isp, bus);
5078 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5080 * We don't do any simq freezing if we are only in target mode
5082 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5084 isp_freeze_loopdown(isp, bus, msg);
5086 if (!callout_active(&fc->ldt)) {
5087 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5088 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_second);
5092 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5095 case ISPASYNC_LOOP_UP:
5096 __va_start(ap, cmd);
5097 bus = __va_arg(ap, int);
5099 fc = ISP_FC_PC(isp, bus);
5101 * Now we just note that Loop has come up. We don't
5102 * actually do anything because we're waiting for a
5103 * Change Notify before activating the FC cleanup
5104 * thread to look at the state of the loop again.
5106 FCPARAM(isp, bus)->link_active = 1;
5108 fc->loop_down_time = 0;
5109 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5111 case ISPASYNC_DEV_ARRIVED:
5112 __va_start(ap, cmd);
5113 bus = __va_arg(ap, int);
5114 lp = __va_arg(ap, fcportdb_t *);
5116 fc = ISP_FC_PC(isp, bus);
5119 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5120 int dbidx = lp - FCPARAM(isp, bus)->portdb;
5123 for (i = 0; i < MAX_FC_TARG; i++) {
5124 if (i >= FL_ID && i <= SNS_ID) {
5127 if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5131 if (i < MAX_FC_TARG) {
5132 FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5133 lp->dev_map_idx = i + 1;
5135 isp_prt(isp, ISP_LOGWARN, "out of target ids");
5136 isp_dump_portdb(isp, bus);
5139 if (lp->dev_map_idx) {
5140 tgt = lp->dev_map_idx - 1;
5141 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);
5142 isp_make_here(isp, bus, tgt);
5144 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);
5147 case ISPASYNC_DEV_CHANGED:
5148 __va_start(ap, cmd);
5149 bus = __va_arg(ap, int);
5150 lp = __va_arg(ap, fcportdb_t *);
5152 fc = ISP_FC_PC(isp, bus);
5155 if (isp_change_is_bad) {
5156 lp->state = FC_PORTDB_STATE_NIL;
5157 if (lp->dev_map_idx) {
5158 tgt = lp->dev_map_idx - 1;
5159 FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5160 lp->dev_map_idx = 0;
5161 isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5162 isp_make_gone(isp, bus, tgt);
5164 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5165 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5168 lp->portid = lp->new_portid;
5169 lp->roles = lp->new_roles;
5170 if (lp->dev_map_idx) {
5171 int t = lp->dev_map_idx - 1;
5172 FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5173 tgt = lp->dev_map_idx - 1;
5174 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5175 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5177 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);
5181 case ISPASYNC_DEV_STAYED:
5182 __va_start(ap, cmd);
5183 bus = __va_arg(ap, int);
5184 lp = __va_arg(ap, fcportdb_t *);
5186 if (lp->dev_map_idx) {
5187 tgt = lp->dev_map_idx - 1;
5188 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5189 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5191 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5192 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5195 case ISPASYNC_DEV_GONE:
5196 __va_start(ap, cmd);
5197 bus = __va_arg(ap, int);
5198 lp = __va_arg(ap, fcportdb_t *);
5200 fc = ISP_FC_PC(isp, bus);
5202 * If this has a virtual target and we haven't marked it
5203 * that we're going to have isp_gdt tell the OS it's gone,
5204 * set the isp_gdt timer running on it.
5206 * If it isn't marked that isp_gdt is going to get rid of it,
5207 * announce that it's gone.
5210 if (lp->dev_map_idx && lp->reserved == 0) {
5212 lp->state = FC_PORTDB_STATE_ZOMBIE;
5213 lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5214 if (fc->ready && !callout_active(&fc->gdt)) {
5215 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);
5216 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5218 tgt = lp->dev_map_idx - 1;
5219 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);
5220 } else if (lp->reserved == 0) {
5221 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);
5224 case ISPASYNC_CHANGE_NOTIFY:
5227 int evt, nphdl, nlstate, reason;
5229 __va_start(ap, cmd);
5230 bus = __va_arg(ap, int);
5231 evt = __va_arg(ap, int);
5232 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5233 nphdl = __va_arg(ap, int);
5234 nlstate = __va_arg(ap, int);
5235 reason = __va_arg(ap, int);
5238 nlstate = reason = 0;
5241 fc = ISP_FC_PC(isp, bus);
5243 if (evt == ISPASYNC_CHANGE_PDB) {
5244 msg = "Chan %d Port Database Changed";
5245 } else if (evt == ISPASYNC_CHANGE_SNS) {
5246 msg = "Chan %d Name Server Database Changed";
5248 msg = "Chan %d Other Change Notify";
5252 * If the loop down timer is running, cancel it.
5254 if (fc->ready && callout_active(&fc->ldt)) {
5255 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_second);
5256 callout_stop(&fc->ldt);
5258 isp_prt(isp, ISP_LOGINFO, msg, bus);
5259 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5260 isp_freeze_loopdown(isp, bus, msg);
5265 #ifdef ISP_TARGET_MODE
5266 case ISPASYNC_TARGET_NOTIFY:
5268 isp_notify_t *notify;
5269 __va_start(ap, cmd);
5270 notify = __va_arg(ap, isp_notify_t *);
5272 switch (notify->nt_ncode) {
5274 case NT_ABORT_TASK_SET:
5276 case NT_CLEAR_TASK_SET:
5278 case NT_TARGET_RESET:
5280 * These are task management functions.
5282 isp_handle_platform_target_tmf(isp, notify);
5289 * No action need be taken here.
5293 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5297 * This is device arrival/departure notification
5299 isp_handle_platform_target_notify_ack(isp, notify);
5303 struct ac_contract ac;
5304 struct ac_device_changed *fc;
5306 ac.contract_number = AC_CONTRACT_DEV_CHG;
5307 fc = (struct ac_device_changed *) ac.contract_data;
5308 fc->wwpn = notify->nt_wwn;
5309 fc->port = notify->nt_sid;
5310 fc->target = notify->nt_nphdl;
5312 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5317 struct ac_contract ac;
5318 struct ac_device_changed *fc;
5320 ac.contract_number = AC_CONTRACT_DEV_CHG;
5321 fc = (struct ac_device_changed *) ac.contract_data;
5322 fc->wwpn = notify->nt_wwn;
5323 fc->port = notify->nt_sid;
5324 fc->target = notify->nt_nphdl;
5326 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5330 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5331 isp_handle_platform_target_notify_ack(isp, notify);
5336 case ISPASYNC_TARGET_ACTION:
5340 __va_start(ap, cmd);
5341 hp = __va_arg(ap, isphdr_t *);
5343 switch (hp->rqs_entry_type) {
5345 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5347 case RQSTYPE_NOTIFY:
5349 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5350 } else if (IS_24XX(isp)) {
5351 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5353 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5358 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5360 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5364 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5370 isp_handle_platform_ctio(isp, hp);
5372 case RQSTYPE_ABTS_RCVD:
5374 abts_t *abts = (abts_t *)hp;
5375 isp_notify_t notify, *nt = ¬ify;
5381 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5382 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5383 ISP_MEMZERO(nt, sizeof (isp_notify_t));
5387 nt->nt_nphdl = abts->abts_nphdl;
5389 isp_find_chan_by_did(isp, did, &chan);
5390 if (chan == ISP_NOCHAN) {
5391 nt->nt_tgt = TGT_ANY;
5393 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5394 if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5395 nt->nt_wwn = lp->port_wwn;
5397 nt->nt_wwn = INI_ANY;
5401 * Try hard to find the lun for this command.
5403 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5405 nt->nt_lun = xpt_path_lun_id(tptr->owner);
5406 rls_lun_statep(isp, tptr);
5408 nt->nt_lun = LUN_ANY;
5410 nt->nt_need_ack = 1;
5411 nt->nt_tagval = abts->abts_rxid_task;
5412 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5413 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5414 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)",
5415 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5417 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)",
5418 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5420 nt->nt_channel = chan;
5421 nt->nt_ncode = NT_ABORT_TASK;
5422 nt->nt_lreserved = hp;
5423 isp_handle_platform_target_tmf(isp, nt);
5426 case RQSTYPE_ENABLE_LUN:
5427 case RQSTYPE_MODIFY_LUN:
5428 isp_ledone(isp, (lun_entry_t *) hp);
5434 case ISPASYNC_FW_CRASH:
5436 uint16_t mbox1, mbox6;
5437 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5438 if (IS_DUALBUS(isp)) {
5439 mbox6 = ISP_READ(isp, OUTMAILBOX6);
5443 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5444 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5445 isp->isp_osinfo.mbox_sleep_ok = 0;
5447 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5448 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5452 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5459 * Locks are held before coming here.
5462 isp_uninit(ispsoftc_t *isp)
5465 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5467 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5469 ISP_DISABLE_INTS(isp);
5473 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5474 * up from our platform default (defww{p|n}n) and morph them based upon
5477 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5478 * based upon channel.
5482 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5485 struct isp_fc *fc = ISP_FC_PC(isp, chan);
5488 * If we're asking for a active WWN, the default overrides get
5489 * returned, otherwise the NVRAM value is picked.
5491 * If we're asking for a default WWN, we just pick the default override.
5494 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5498 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5502 return (0x400000007F000009ull);
5504 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5509 * For channel zero just return what we have. For either ACTIVE or
5510 * DEFAULT cases, we depend on default override of NVRAM values for
5518 * For other channels, we are doing one of three things:
5520 * 1. If what we have now is non-zero, return it. Otherwise we morph
5521 * values from channel 0. 2. If we're here for a WWPN we synthesize
5522 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5523 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5530 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5532 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5535 if (((seed >> 60) & 0xf) == 2) {
5537 * The type 2 NAA fields for QLogic cards appear be laid out
5540 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5541 * port (1) or node (0) WWN distinguishor bit 48
5542 * physical port on dual-port chips (23XX/24XX)
5544 * This is somewhat nutty, particularly since bit 48 is
5545 * irrelevant as they assign separate serial numbers to
5546 * different physical ports anyway.
5548 * We'll stick our channel number plus one first into bits
5549 * 57..59 and thence into bits 52..55 which allows for 8 bits
5550 * of channel which is comfortably more than our maximum
5553 seed &= ~0x0FF0000000000000ULL;
5555 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5556 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5565 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5571 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5574 ksprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5576 __va_start(ap, fmt);
5577 kvsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
5579 kprintf("%s\n", lbuf);
5583 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5586 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5589 xpt_print_path(xs->ccb_h.path);
5590 __va_start(ap, fmt);
5597 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5600 struct timespec x = *b;
5602 elapsed = GET_NANOSEC(&x);
5609 isp_mbox_acquire(ispsoftc_t *isp)
5611 if (isp->isp_osinfo.mboxbsy) {
5614 isp->isp_osinfo.mboxcmd_done = 0;
5615 isp->isp_osinfo.mboxbsy = 1;
5621 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5623 unsigned int usecs = mbp->timeout;
5624 unsigned int max, olim, ilim;
5627 usecs = MBCMD_DEFAULT_TIMEOUT;
5629 max = isp->isp_mbxwrk0 + 1;
5631 if (isp->isp_osinfo.mbox_sleep_ok) {
5632 unsigned int ms = (usecs + 999) / 1000;
5634 isp->isp_osinfo.mbox_sleep_ok = 0;
5635 isp->isp_osinfo.mbox_sleeping = 1;
5636 for (olim = 0; olim < max; olim++) {
5637 lksleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, 0, "ispmbx_sleep", isp_mstohz(ms));
5638 if (isp->isp_osinfo.mboxcmd_done) {
5642 isp->isp_osinfo.mbox_sleep_ok = 1;
5643 isp->isp_osinfo.mbox_sleeping = 0;
5645 for (olim = 0; olim < max; olim++) {
5646 for (ilim = 0; ilim < usecs; ilim += 100) {
5648 uint16_t sema, mbox;
5649 if (isp->isp_osinfo.mboxcmd_done) {
5652 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5653 isp_intr(isp, isr, sema, mbox);
5654 if (isp->isp_osinfo.mboxcmd_done) {
5660 if (isp->isp_osinfo.mboxcmd_done) {
5665 if (isp->isp_osinfo.mboxcmd_done == 0) {
5666 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5667 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5668 mbp->param[0] = MBOX_TIMEOUT;
5669 isp->isp_osinfo.mboxcmd_done = 1;
5674 isp_mbox_notify_done(ispsoftc_t *isp)
5676 if (isp->isp_osinfo.mbox_sleeping) {
5677 wakeup(&isp->isp_mbxworkp);
5679 isp->isp_osinfo.mboxcmd_done = 1;
5683 isp_mbox_release(ispsoftc_t *isp)
5685 isp->isp_osinfo.mboxbsy = 0;
5689 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5692 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5695 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5705 t.tv_sec = ms / 1000;
5706 t.tv_usec = (ms % 1000) * 1000;
5707 hz = tvtohz_high(&t);
5718 isp_platform_intr(void *arg)
5720 ispsoftc_t *isp = arg;
5722 uint16_t sema, mbox;
5726 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5727 isp->isp_intbogus++;
5729 isp_intr(isp, isr, sema, mbox);
5735 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5737 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5738 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5740 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5742 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5746 isp_timer(void *arg)
5748 ispsoftc_t *isp = arg;
5751 #ifdef ISP_TARGET_MODE
5752 isp_tmcmd_restart(isp);
5755 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);