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
28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <dev/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
36 #include <sys/module.h>
37 #include <sys/ioccom.h>
38 #include <dev/isp/isp_ioctl.h>
39 #include <sys/devicestat.h>
40 #include <cam/cam_periph.h>
41 #include <cam/cam_xpt_periph.h>
43 #if __FreeBSD_version < 800002
44 #define THREAD_CREATE kthread_create
46 #define THREAD_CREATE kproc_create
49 MODULE_VERSION(isp, 1);
50 MODULE_DEPEND(isp, cam, 1, 1, 1);
51 int isp_announced = 0;
52 int isp_fabric_hysteresis = 5;
53 int isp_loop_down_limit = 60; /* default loop down limit */
54 int isp_change_is_bad = 0; /* "changed" devices are bad */
55 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
56 int isp_gone_device_time = 30; /* grace time before reporting device lost */
57 int isp_autoconfig = 1; /* automatically attach/detach devices */
58 static const char *roles[4] = {
59 "(none)", "Target", "Initiator", "Target/Initiator"
61 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
62 static const char rqo[] = "%s: Request Queue Overflow\n";
64 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
65 static d_ioctl_t ispioctl;
66 static void isp_intr_enable(void *);
67 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
68 static void isp_poll(struct cam_sim *);
69 static timeout_t isp_watchdog;
70 static timeout_t isp_gdt;
71 static task_fn_t isp_gdt_task;
72 static timeout_t isp_ldt;
73 static task_fn_t isp_ldt_task;
74 static void isp_kthread(void *);
75 static void isp_action(struct cam_sim *, union ccb *);
76 #ifdef ISP_INTERNAL_TARGET
77 static void isp_target_thread_pi(void *);
78 static void isp_target_thread_fc(void *);
80 static void isp_timer(void *);
82 static struct cdevsw isp_cdevsw = {
83 .d_version = D_VERSION,
89 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
91 struct ccb_setasync csa;
93 struct cam_path *path;
96 * Construct our SIM entry.
98 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);
105 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
107 cam_sim_free(sim, FALSE);
111 if (xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
113 xpt_bus_deregister(cam_sim_path(sim));
115 cam_sim_free(sim, FALSE);
118 xpt_setup_ccb(&csa.ccb_h, path, 5);
119 csa.ccb_h.func_code = XPT_SASYNC_CB;
120 csa.event_enable = AC_LOST_DEVICE;
121 csa.callback = isp_cam_async;
122 csa.callback_arg = sim;
123 xpt_action((union ccb *)&csa);
126 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
129 #ifdef ISP_INTERNAL_TARGET
130 ISP_SET_PC(isp, chan, proc_active, 1);
131 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
132 ISP_SET_PC(isp, chan, proc_active, 0);
133 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
137 fcparam *fcp = FCPARAM(isp, chan);
138 struct isp_fc *fc = ISP_FC_PC(isp, chan);
146 callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
147 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
148 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
149 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
152 * We start by being "loop down" if we have an initiator role
154 if (fcp->role & ISP_ROLE_INITIATOR) {
155 isp_freeze_loopdown(isp, chan, "isp_attach");
156 callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
157 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
160 if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
161 xpt_free_path(fc->path);
163 if (callout_active(&fc->ldt)) {
164 callout_stop(&fc->ldt);
166 xpt_bus_deregister(cam_sim_path(fc->sim));
168 cam_sim_free(fc->sim, FALSE);
171 #ifdef ISP_INTERNAL_TARGET
172 ISP_SET_PC(isp, chan, proc_active, 1);
173 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
174 ISP_SET_PC(isp, chan, proc_active, 0);
175 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
179 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
180 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
181 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwnn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwnn, "World Wide Node Name");
182 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, "World Wide Port Name");
183 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "loop_down_limit", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->loop_down_limit, 0, "Loop Down Limit");
184 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "gone_device_time", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->gone_device_time, 0, "Gone Device Time");
191 isp_attach(ispsoftc_t *isp)
193 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
194 int du = device_get_unit(isp->isp_dev);
197 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
198 isp->isp_osinfo.ehook.ich_arg = isp;
200 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
201 * will be called right awawy, which will clear isp_osinfo.ehook_active,
202 * which would be unwise to then set again later.
204 isp->isp_osinfo.ehook_active = 1;
205 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
206 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
211 * Create the device queue for our SIM(s).
213 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
214 if (isp->isp_osinfo.devq == NULL) {
215 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
219 for (chan = 0; chan < isp->isp_nchan; chan++) {
220 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
225 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
226 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
227 isp->isp_osinfo.timer_active = 1;
229 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
230 if (isp->isp_osinfo.cdev) {
231 isp->isp_osinfo.cdev->si_drv1 = isp;
236 while (--chan >= 0) {
238 struct cam_path *path;
240 sim = ISP_FC_PC(isp, chan)->sim;
241 path = ISP_FC_PC(isp, chan)->path;
243 sim = ISP_SPI_PC(isp, chan)->sim;
244 path = ISP_SPI_PC(isp, chan)->path;
248 xpt_bus_deregister(cam_sim_path(sim));
250 cam_sim_free(sim, FALSE);
252 if (isp->isp_osinfo.ehook_active) {
253 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
254 isp->isp_osinfo.ehook_active = 0;
256 if (isp->isp_osinfo.cdev) {
257 destroy_dev(isp->isp_osinfo.cdev);
258 isp->isp_osinfo.cdev = NULL;
260 cam_simq_free(isp->isp_osinfo.devq);
261 isp->isp_osinfo.devq = NULL;
266 isp_detach(ispsoftc_t *isp)
269 struct cam_path *path;
270 struct ccb_setasync csa;
274 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
276 sim = ISP_FC_PC(isp, chan)->sim;
277 path = ISP_FC_PC(isp, chan)->path;
279 sim = ISP_SPI_PC(isp, chan)->sim;
280 path = ISP_SPI_PC(isp, chan)->path;
282 if (sim->refcount > 2) {
287 if (isp->isp_osinfo.timer_active) {
288 callout_stop(&isp->isp_osinfo.tmo);
289 isp->isp_osinfo.timer_active = 0;
291 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
293 sim = ISP_FC_PC(isp, chan)->sim;
294 path = ISP_FC_PC(isp, chan)->path;
296 sim = ISP_SPI_PC(isp, chan)->sim;
297 path = ISP_SPI_PC(isp, chan)->path;
299 xpt_setup_ccb(&csa.ccb_h, path, 5);
300 csa.ccb_h.func_code = XPT_SASYNC_CB;
301 csa.event_enable = 0;
302 csa.callback = isp_cam_async;
303 csa.callback_arg = sim;
304 xpt_action((union ccb *)&csa);
306 xpt_bus_deregister(cam_sim_path(sim));
307 cam_sim_free(sim, FALSE);
310 if (isp->isp_osinfo.cdev) {
311 destroy_dev(isp->isp_osinfo.cdev);
312 isp->isp_osinfo.cdev = NULL;
314 if (isp->isp_osinfo.ehook_active) {
315 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
316 isp->isp_osinfo.ehook_active = 0;
318 if (isp->isp_osinfo.devq != NULL) {
319 cam_simq_free(isp->isp_osinfo.devq);
320 isp->isp_osinfo.devq = NULL;
326 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
329 struct isp_fc *fc = ISP_FC_PC(isp, chan);
330 if (fc->simqfrozen == 0) {
331 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
332 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
333 xpt_freeze_simq(fc->sim, 1);
335 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
336 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
342 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
345 struct isp_fc *fc = ISP_FC_PC(isp, chan);
346 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
347 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
348 if (wasfrozen && fc->simqfrozen == 0) {
349 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
350 xpt_release_simq(fc->sim, 1);
357 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
360 int nr, chan, retval = ENOTTY;
367 int olddblev = isp->isp_dblev;
368 isp->isp_dblev = *(int *)addr;
369 *(int *)addr = olddblev;
375 if (chan < 0 || chan >= isp->isp_nchan) {
380 *(int *)addr = FCPARAM(isp, chan)->role;
382 *(int *)addr = SDPARAM(isp, chan)->role;
389 if (chan < 0 || chan >= isp->isp_nchan) {
394 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
400 * We don't really support dual role at present on FC cards.
402 * We should, but a bunch of things are currently broken,
405 if (nr == ISP_ROLE_BOTH) {
406 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
410 *(int *)addr = FCPARAM(isp, chan)->role;
411 #ifdef ISP_INTERNAL_TARGET
413 retval = isp_fc_change_role(isp, chan, nr);
416 FCPARAM(isp, chan)->role = nr;
419 *(int *)addr = SDPARAM(isp, chan)->role;
420 SDPARAM(isp, chan)->role = nr;
427 #ifdef ISP_TARGET_MODE
428 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
438 if (chan < 0 || chan >= isp->isp_nchan) {
443 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
455 if (chan < 0 || chan >= isp->isp_nchan) {
460 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
468 case ISP_FC_GETDINFO:
470 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
476 if (ifc->loopid >= MAX_FC_TARG) {
480 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
481 if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
482 ifc->role = lp->roles;
483 ifc->loopid = lp->handle;
484 ifc->portid = lp->portid;
485 ifc->node_wwn = lp->node_wwn;
486 ifc->port_wwn = lp->port_wwn;
495 isp_stats_t *sp = (isp_stats_t *) addr;
497 ISP_MEMZERO(sp, sizeof (*sp));
498 sp->isp_stat_version = ISP_STATS_VERSION;
499 sp->isp_type = isp->isp_type;
500 sp->isp_revision = isp->isp_revision;
502 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
503 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
504 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
505 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
506 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
507 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
508 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
509 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
517 isp->isp_intbogus = 0;
518 isp->isp_intmboxc = 0;
519 isp->isp_intoasync = 0;
520 isp->isp_rsltccmplt = 0;
521 isp->isp_fphccmplt = 0;
522 isp->isp_rscchiwater = 0;
523 isp->isp_fpcchiwater = 0;
527 case ISP_FC_GETHINFO:
529 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
530 int chan = hba->fc_channel;
532 if (chan < 0 || chan >= isp->isp_nchan) {
536 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
537 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
538 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
539 hba->fc_nchannels = isp->isp_nchan;
541 hba->fc_nports = MAX_FC_TARG;
542 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
543 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
544 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
545 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
546 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
547 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
548 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
550 hba->fc_nports = MAX_TARGETS;
552 hba->fc_topology = 0;
553 hba->nvram_node_wwn = 0ull;
554 hba->nvram_port_wwn = 0ull;
555 hba->active_node_wwn = 0ull;
556 hba->active_port_wwn = 0ull;
564 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
573 if (chan < 0 || chan >= isp->isp_nchan) {
578 needmarker = retval = 0;
579 loopid = fct->loopid;
582 uint8_t local[QENTRY_LEN];
584 isp24xx_statusreq_t *sp;
585 fcparam *fcp = FCPARAM(isp, chan);
589 for (i = 0; i < MAX_FC_TARG; i++) {
590 lp = &fcp->portdb[i];
591 if (lp->handle == loopid) {
595 if (i == MAX_FC_TARG) {
600 /* XXX VALIDATE LP XXX */
601 tmf = (isp24xx_tmf_t *) local;
602 ISP_MEMZERO(tmf, QENTRY_LEN);
603 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
604 tmf->tmf_header.rqs_entry_count = 1;
605 tmf->tmf_nphdl = lp->handle;
607 tmf->tmf_timeout = 2;
608 tmf->tmf_tidlo = lp->portid;
609 tmf->tmf_tidhi = lp->portid >> 16;
610 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
611 tmf->tmf_lun[1] = fct->lun & 0xff;
612 if (fct->lun >= 256) {
613 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
615 switch (fct->action) {
617 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
619 case IPT_TARGET_RESET:
620 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
624 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
627 case IPT_CLEAR_TASK_SET:
628 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
631 case IPT_ABORT_TASK_SET:
632 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
643 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
644 mbs.param[1] = QENTRY_LEN;
645 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
646 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
647 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
648 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
650 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
655 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
656 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
657 sp = (isp24xx_statusreq_t *) local;
658 sp->req_completion_status = 1;
659 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
660 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
661 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
662 FC_SCRATCH_RELEASE(isp, chan);
663 if (retval || sp->req_completion_status != 0) {
664 FC_SCRATCH_RELEASE(isp, chan);
673 MBSINIT(&mbs, 0, MBLOGALL, 0);
674 if (ISP_CAP_2KLOGIN(isp) == 0) {
677 switch (fct->action) {
679 mbs.param[0] = MBOX_CLEAR_ACA;
680 mbs.param[1] = loopid;
681 mbs.param[2] = fct->lun;
683 case IPT_TARGET_RESET:
684 mbs.param[0] = MBOX_TARGET_RESET;
685 mbs.param[1] = loopid;
689 mbs.param[0] = MBOX_LUN_RESET;
690 mbs.param[1] = loopid;
691 mbs.param[2] = fct->lun;
694 case IPT_CLEAR_TASK_SET:
695 mbs.param[0] = MBOX_CLEAR_TASK_SET;
696 mbs.param[1] = loopid;
697 mbs.param[2] = fct->lun;
700 case IPT_ABORT_TASK_SET:
701 mbs.param[0] = MBOX_ABORT_TASK_SET;
702 mbs.param[1] = loopid;
703 mbs.param[2] = fct->lun;
712 FCPARAM(isp, chan)->sendmarker = 1;
714 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
730 isp_intr_enable(void *arg)
733 ispsoftc_t *isp = arg;
735 for (chan = 0; chan < isp->isp_nchan; chan++) {
737 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
738 ISP_ENABLE_INTS(isp);
742 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
743 ISP_ENABLE_INTS(isp);
748 isp->isp_osinfo.ehook_active = 0;
750 /* Release our hook so that the boot can continue. */
751 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
758 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
759 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
761 static ISP_INLINE int
762 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
764 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
765 if (ISP_PCMD(ccb) == NULL) {
768 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
772 static ISP_INLINE void
773 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
775 ((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
776 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
777 ISP_PCMD(ccb) = NULL;
780 * Put the target mode functions here, because some are inlines
783 #ifdef ISP_TARGET_MODE
784 static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
785 static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
786 static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
787 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
788 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
789 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
790 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
791 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
792 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
793 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
794 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
795 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
796 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
797 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
798 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
799 static void isp_enable_lun(ispsoftc_t *, union ccb *);
800 static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
801 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
802 static void isp_disable_lun(ispsoftc_t *, union ccb *);
803 static int isp_enable_target_mode(ispsoftc_t *, int);
804 static int isp_disable_target_mode(ispsoftc_t *, int);
805 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
806 static timeout_t isp_refire_putback_atio;
807 static void isp_complete_ctio(union ccb *);
808 static void isp_target_putback_atio(union ccb *);
809 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
810 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
811 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
812 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
813 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
814 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
815 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
816 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
817 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
818 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
819 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
820 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
822 static ISP_INLINE void
823 isp_tmlock(ispsoftc_t *isp, const char *msg)
825 while (isp->isp_osinfo.tmbusy) {
826 isp->isp_osinfo.tmwanted = 1;
827 mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
829 isp->isp_osinfo.tmbusy = 1;
832 static ISP_INLINE void
833 isp_tmunlk(ispsoftc_t *isp)
835 isp->isp_osinfo.tmbusy = 0;
836 if (isp->isp_osinfo.tmwanted) {
837 isp->isp_osinfo.tmwanted = 0;
842 static ISP_INLINE int
843 is_any_lun_enabled(ispsoftc_t *isp, int bus)
848 for (i = 0; i < LUN_HASH_SIZE; i++) {
849 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
850 if (SLIST_FIRST(lhp))
856 static ISP_INLINE int
857 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
862 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
863 SLIST_FOREACH(tptr, lhp, next) {
864 if (xpt_path_lun_id(tptr->owner) == lun) {
872 dump_tstates(ispsoftc_t *isp, int bus)
876 tstate_t *tptr = NULL;
878 if (bus >= isp->isp_nchan) {
881 for (i = 0; i < LUN_HASH_SIZE; i++) {
882 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
884 SLIST_FOREACH(tptr, lhp, next) {
885 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
891 static ISP_INLINE tstate_t *
892 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
894 tstate_t *tptr = NULL;
898 if (bus < isp->isp_nchan) {
899 for (i = 0; i < LUN_HASH_SIZE; i++) {
900 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
901 SLIST_FOREACH(tptr, lhp, next) {
902 if (xpt_path_lun_id(tptr->owner) == lun) {
912 static ISP_INLINE tstate_t *
913 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
915 tstate_t *tptr = NULL;
916 atio_private_data_t *atp;
920 if (bus < isp->isp_nchan && tagval != 0) {
921 for (i = 0; i < LUN_HASH_SIZE; i++) {
922 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
923 SLIST_FOREACH(tptr, lhp, next) {
924 atp = isp_get_atpd(isp, tptr, tagval);
925 if (atp && atp->tag == tagval) {
935 static ISP_INLINE inot_private_data_t *
936 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
938 inot_private_data_t *ntp;
943 for (bus = 0; bus < isp->isp_nchan; bus++) {
944 for (i = 0; i < LUN_HASH_SIZE; i++) {
945 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
946 SLIST_FOREACH(tptr, lhp, next) {
947 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
959 static ISP_INLINE void
960 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
962 KASSERT((tptr->hold), ("tptr not held"));
967 isp_tmcmd_restart(ispsoftc_t *isp)
969 inot_private_data_t *ntp;
974 for (bus = 0; bus < isp->isp_nchan; bus++) {
975 for (i = 0; i < LUN_HASH_SIZE; i++) {
976 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
977 SLIST_FOREACH(tptr, lhp, next) {
978 inot_private_data_t *restart_queue = tptr->restart_queue;
979 tptr->restart_queue = NULL;
980 while (restart_queue) {
982 restart_queue = ntp->rd.nt.nt_hba;
984 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
985 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
987 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
988 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
990 isp_put_ntpd(isp, tptr, ntp);
991 if (tptr->restart_queue && restart_queue != NULL) {
992 ntp = tptr->restart_queue;
993 tptr->restart_queue = restart_queue;
994 while (restart_queue->rd.nt.nt_hba) {
995 restart_queue = restart_queue->rd.nt.nt_hba;
997 restart_queue->rd.nt.nt_hba = ntp;
1006 static ISP_INLINE atio_private_data_t *
1007 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1009 atio_private_data_t *atp;
1014 tptr->atfree = atp->next;
1018 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1019 if (atp->tag == tag) {
1026 static ISP_INLINE void
1027 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1031 atp->next = tptr->atfree;
1036 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1038 atio_private_data_t *atp;
1039 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1041 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1042 if (atp->tag == 0) {
1045 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",
1046 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]);
1051 static ISP_INLINE inot_private_data_t *
1052 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1054 inot_private_data_t *ntp;
1057 tptr->ntfree = ntp->next;
1062 static ISP_INLINE inot_private_data_t *
1063 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1065 inot_private_data_t *ntp;
1066 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1067 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1074 static ISP_INLINE void
1075 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1077 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1078 ntp->next = tptr->ntfree;
1083 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1091 lun = xpt_path_lun_id(path);
1092 if (lun != CAM_LUN_WILDCARD) {
1093 if (lun >= ISP_MAX_LUNS(isp)) {
1094 return (CAM_LUN_INVALID);
1097 if (is_lun_enabled(isp, bus, lun)) {
1098 return (CAM_LUN_ALRDY_ENA);
1100 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1102 return (CAM_RESRC_UNAVAIL);
1104 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1105 if (status != CAM_REQ_CMP) {
1106 free(tptr, M_DEVBUF);
1109 SLIST_INIT(&tptr->atios);
1110 SLIST_INIT(&tptr->inots);
1111 for (i = 0; i < ATPDPSIZE-1; i++) {
1112 tptr->atpool[i].next = &tptr->atpool[i+1];
1113 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1115 tptr->atfree = tptr->atpool;
1116 tptr->ntfree = tptr->ntpool;
1118 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1119 SLIST_INSERT_HEAD(lhp, tptr, next);
1121 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1122 return (CAM_REQ_CMP);
1125 static ISP_INLINE void
1126 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1130 KASSERT((tptr->hold != 0), ("tptr is not held"));
1131 KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1132 ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1133 SLIST_REMOVE(lhp, tptr, tstate, next);
1134 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1135 xpt_free_path(tptr->owner);
1136 free(tptr, M_DEVBUF);
1143 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1145 tstate_t *tptr = NULL;
1146 int bus, tm_enabled, target_role;
1152 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1154 bus = XS_CHANNEL(ccb);
1155 target = ccb->ccb_h.target_id;
1156 lun = ccb->ccb_h.target_lun;
1157 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "enabling lun %u\n", lun);
1158 if (target != CAM_TARGET_WILDCARD && target != 0) {
1159 ccb->ccb_h.status = CAM_TID_INVALID;
1163 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1164 ccb->ccb_h.status = CAM_LUN_INVALID;
1169 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1170 ccb->ccb_h.status = CAM_LUN_INVALID;
1174 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1175 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1179 * Wait until we're not busy with the lun enables subsystem
1181 isp_tmlock(isp, "isp_enable_lun");
1184 * This is as a good a place as any to check f/w capabilities.
1188 if (ISP_CAP_TMODE(isp) == 0) {
1189 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1190 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1194 * We *could* handle non-SCCLUN f/w, but we'd have to
1195 * dork with our already fragile enable/disable code.
1197 if (ISP_CAP_SCCFW(isp) == 0) {
1198 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1199 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1203 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1206 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1210 * Create the state pointer.
1211 * It should not already exist.
1213 tptr = get_lun_statep(isp, bus, lun);
1215 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1218 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1219 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1224 * We have a tricky maneuver to perform here.
1226 * If target mode isn't already enabled here,
1227 * *and* our current role includes target mode,
1228 * we enable target mode here.
1231 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1232 if (tm_enabled == 0 && target_role != 0) {
1233 if (isp_enable_target_mode(isp, bus)) {
1234 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1235 destroy_lun_state(isp, tptr);
1243 * Now check to see whether this bus is in target mode already.
1245 * If not, a later role change into target mode will finish the job.
1247 if (tm_enabled == 0) {
1248 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1249 ccb->ccb_h.status = CAM_REQ_CMP;
1250 xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1257 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1260 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1262 destroy_lun_state(isp, tptr);
1269 rls_lun_statep(isp, tptr);
1273 * And we're outta here....
1280 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1282 tstate_t *tptr = NULL;
1286 ISP_GET_PC(isp, bus, tm_enabled, i);
1288 return (CAM_REQ_CMP);
1290 ISP_GET_PC(isp, bus, tm_enable_defer, i);
1292 return (CAM_REQ_CMP);
1295 * If this succeeds, it will set tm_enable
1297 if (isp_enable_target_mode(isp, bus)) {
1298 return (CAM_REQ_CMP_ERR);
1300 isp_tmlock(isp, "isp_enable_deferred_luns");
1301 for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1302 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1303 SLIST_FOREACH(tptr, lhp, next) {
1305 if (tptr->enabled == 0) {
1306 if (isp_enable_deferred(isp, bus, xpt_path_lun_id(tptr->owner)) == 0) {
1318 return (CAM_REQ_CMP_ERR);
1320 ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1321 return (CAM_REQ_CMP);
1325 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1328 int luns_already_enabled = ISP_FC_PC(isp, bus)->tm_luns_enabled;
1330 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1331 if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
1332 status = CAM_REQ_CMP;
1334 int cmd_cnt, not_cnt;
1337 cmd_cnt = DFLT_CMND_CNT;
1338 not_cnt = DFLT_INOT_CNT;
1343 status = CAM_REQ_INPROG;
1344 isp->isp_osinfo.rptr = &status;
1345 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1346 status = CAM_RESRC_UNAVAIL;
1348 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1350 isp->isp_osinfo.rptr = NULL;
1352 if (status == CAM_REQ_CMP) {
1353 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1354 isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1360 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1362 tstate_t *tptr = NULL;
1368 bus = XS_CHANNEL(ccb);
1369 target = ccb->ccb_h.target_id;
1370 lun = ccb->ccb_h.target_lun;
1371 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "disabling lun %u\n", lun);
1372 if (target != CAM_TARGET_WILDCARD && target != 0) {
1373 ccb->ccb_h.status = CAM_TID_INVALID;
1378 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1379 ccb->ccb_h.status = CAM_LUN_INVALID;
1384 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1385 ccb->ccb_h.status = CAM_LUN_INVALID;
1391 * See if we're busy disabling a lun now.
1393 isp_tmlock(isp, "isp_disable_lun");
1394 status = CAM_REQ_INPROG;
1397 * Find the state pointer.
1399 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1400 status = CAM_PATH_INVALID;
1405 * If we're a 24XX card, we're done.
1408 status = CAM_REQ_CMP;
1413 * For SCC FW, we only deal with lun zero.
1418 isp->isp_osinfo.rptr = &status;
1419 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1420 status = CAM_RESRC_UNAVAIL;
1422 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1424 isp->isp_osinfo.rptr = NULL;
1426 if (status == CAM_REQ_CMP) {
1429 * If we have no more luns enabled for this bus, delete all tracked wwns for it (if we are FC)
1430 * and disable target mode.
1432 if (is_any_lun_enabled(isp, bus) == 0) {
1433 isp_del_all_wwn_entries(isp, bus);
1434 if (isp_disable_target_mode(isp, bus)) {
1435 status = CAM_REQ_CMP_ERR;
1439 ccb->ccb_h.status = status;
1440 if (status == CAM_REQ_CMP) {
1441 xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1442 destroy_lun_state(isp, tptr);
1445 rls_lun_statep(isp, tptr);
1452 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1456 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1457 if (tm_enabled != 0) {
1462 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1463 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1464 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1465 mbs.param[2] = bus << 7;
1466 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1467 isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
1471 ISP_SET_PC(isp, bus, tm_enabled, 1);
1472 isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
1477 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1481 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1482 if (tm_enabled == 0) {
1487 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1488 mbs.param[2] = bus << 7;
1489 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1490 isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
1494 ISP_SET_PC(isp, bus, tm_enabled, 0);
1495 isp_prt(isp, ISP_LOGINFO, "Target Role disabled onon Bus %d", bus);
1500 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1504 rptr = isp->isp_osinfo.rptr;
1505 if (lep->le_status != LUN_OK) {
1506 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1508 *rptr = CAM_REQ_CMP_ERR;
1513 *rptr = CAM_REQ_CMP;
1520 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1524 atio_private_data_t *atp;
1525 struct ccb_scsiio *cso = &ccb->csio;
1526 uint32_t dmaresult, handle;
1527 uint8_t local[QENTRY_LEN];
1530 * Do some sanity checks.
1532 if (cso->dxfer_len == 0) {
1533 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1534 xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1535 ccb->ccb_h.status = CAM_REQ_INVALID;
1541 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1543 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1545 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1546 dump_tstates(isp, XS_CHANNEL(ccb));
1547 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1553 atp = isp_get_atpd(isp, tptr, cso->tag_id);
1555 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1556 isp_dump_atpd(isp, tptr);
1557 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1562 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1563 ccb->ccb_h.status = CAM_REQ_ABORTED;
1569 * Check to make sure we're still in target mode.
1571 if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1572 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);
1573 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1579 * Get some resources
1581 if (isp_get_pcmd(isp, ccb)) {
1582 rls_lun_statep(isp, tptr);
1583 xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1584 cam_freeze_devq(ccb->ccb_h.path);
1585 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1586 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1590 qe = isp_getrqentry(isp);
1592 xpt_print(ccb->ccb_h.path, rqo, __func__);
1593 cam_freeze_devq(ccb->ccb_h.path);
1594 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1595 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1598 memset(local, 0, QENTRY_LEN);
1601 * We're either moving data or completing a command here.
1604 ct7_entry_t *cto = (ct7_entry_t *) local;
1606 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1607 cto->ct_header.rqs_entry_count = 1;
1608 cto->ct_header.rqs_seqno = 1;
1609 cto->ct_nphdl = atp->nphdl;
1610 cto->ct_rxid = atp->tag;
1611 cto->ct_iid_lo = atp->portid;
1612 cto->ct_iid_hi = atp->portid >> 16;
1613 cto->ct_oxid = atp->oxid;
1614 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1615 cto->ct_scsi_status = cso->scsi_status;
1616 cto->ct_timeout = 120;
1617 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1618 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1619 cto->ct_flags |= CT7_SENDSTATUS;
1621 if (cso->dxfer_len == 0) {
1622 cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1623 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1624 int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1625 cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1626 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1627 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1630 cto->ct_flags |= CT7_FLAG_MODE0;
1631 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1632 cto->ct_flags |= CT7_DATA_IN;
1634 cto->ct_flags |= CT7_DATA_OUT;
1636 cto->rsp.m0.reloff = atp->bytes_xfered;
1638 * Don't overrun the limits placed on us
1640 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1641 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1643 atp->last_xframt = cso->dxfer_len;
1644 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1646 if (cto->ct_flags & CT7_SENDSTATUS) {
1647 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1648 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1649 if (cto->ct_resid < 0) {
1650 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1651 } else if (cto->ct_resid > 0) {
1652 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1654 atp->state = ATPD_STATE_LAST_CTIO;
1655 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,
1656 atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1659 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,
1660 cso->dxfer_len, atp->bytes_xfered);
1661 atp->state = ATPD_STATE_CTIO;
1663 } else if (IS_FC(isp)) {
1664 ct2_entry_t *cto = (ct2_entry_t *) local;
1666 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1667 cto->ct_header.rqs_entry_count = 1;
1668 cto->ct_header.rqs_seqno = 1;
1669 if (ISP_CAP_2KLOGIN(isp) == 0) {
1670 ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1672 cto->ct_iid = cso->init_id;
1673 if (ISP_CAP_SCCFW(isp) == 0) {
1674 cto->ct_lun = ccb->ccb_h.target_lun;
1679 cto->ct_rxid = cso->tag_id;
1680 if (cso->dxfer_len == 0) {
1681 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1682 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1683 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1684 if (cto->ct_resid < 0) {
1685 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1686 } else if (cto->ct_resid > 0) {
1687 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1689 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1690 int m = min(cso->sense_len, MAXRESPLEN);
1691 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1692 cto->rsp.m1.ct_senselen = m;
1693 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1694 } else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1698 xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1701 cto->ct_flags |= CT2_FLAG_MODE0;
1702 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1703 cto->ct_flags |= CT2_DATA_IN;
1705 cto->ct_flags |= CT2_DATA_OUT;
1707 cto->ct_reloff = atp->bytes_xfered;
1708 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1710 * Don't overrun the limits placed on us
1712 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1713 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1715 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1716 cto->ct_flags |= CT2_SENDSTATUS;
1717 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1718 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1719 if (cto->ct_resid < 0) {
1720 cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1721 } else if (cto->ct_resid > 0) {
1722 cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1725 atp->last_xframt = cso->dxfer_len;
1728 * If we're sending data and status back together,
1729 * we can't also send back sense data as well.
1731 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1734 if (cto->ct_flags & CT2_SENDSTATUS) {
1735 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1736 cto->ct_flags |= CT2_CCINCR;
1737 atp->state = ATPD_STATE_LAST_CTIO;
1738 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,
1739 atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1742 atp->state = ATPD_STATE_CTIO;
1743 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,
1744 cso->dxfer_len, atp->bytes_xfered);
1746 cto->ct_timeout = 10;
1748 ct_entry_t *cto = (ct_entry_t *) local;
1750 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1751 cto->ct_header.rqs_entry_count = 1;
1752 cto->ct_header.rqs_seqno = 1;
1753 cto->ct_iid = cso->init_id;
1754 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1755 cto->ct_tgt = ccb->ccb_h.target_id;
1756 cto->ct_lun = ccb->ccb_h.target_lun;
1757 cto->ct_fwhandle = cso->tag_id >> 16;
1758 if (AT_HAS_TAG(cso->tag_id)) {
1759 cto->ct_tag_val = cso->tag_id;
1760 cto->ct_flags |= CT_TQAE;
1762 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1763 cto->ct_flags |= CT_NODISC;
1765 if (cso->dxfer_len == 0) {
1766 cto->ct_flags |= CT_NO_DATA;
1767 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1768 cto->ct_flags |= CT_DATA_IN;
1770 cto->ct_flags |= CT_DATA_OUT;
1772 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1773 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1774 cto->ct_scsi_status = cso->scsi_status;
1775 cto->ct_resid = cso->resid;
1776 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1777 cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1779 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1780 cto->ct_timeout = 10;
1783 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1784 xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1785 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1791 * Call the dma setup routines for this entry (and any subsequent
1792 * CTIOs) if there's data to move, and then tell the f/w it's got
1793 * new things to play with. As with isp_start's usage of DMA setup,
1794 * any swizzling is done in the machine dependent layer. Because
1795 * of this, we put the request onto the queue area first in native
1800 ct7_entry_t *cto = (ct7_entry_t *) local;
1801 cto->ct_syshandle = handle;
1802 } else if (IS_FC(isp)) {
1803 ct2_entry_t *cto = (ct2_entry_t *) local;
1804 cto->ct_syshandle = handle;
1806 ct_entry_t *cto = (ct_entry_t *) local;
1807 cto->ct_syshandle = handle;
1810 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1811 if (dmaresult == CMD_QUEUED) {
1813 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1814 rls_lun_statep(isp, tptr);
1817 if (dmaresult == CMD_EAGAIN) {
1818 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1820 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1822 isp_destroy_tgt_handle(isp, handle);
1824 rls_lun_statep(isp, tptr);
1825 isp_free_pcmd(isp, ccb);
1830 isp_refire_putback_atio(void *arg)
1832 union ccb *ccb = arg;
1833 ispsoftc_t *isp = XS_ISP(ccb);
1835 isp_target_putback_atio(ccb);
1840 isp_target_putback_atio(union ccb *ccb)
1843 struct ccb_scsiio *cso;
1848 qe = isp_getrqentry(isp);
1850 xpt_print(ccb->ccb_h.path, rqo, __func__);
1851 (void) timeout(isp_refire_putback_atio, ccb, 10);
1854 memset(qe, 0, QENTRY_LEN);
1857 at2_entry_t local, *at = &local;
1858 ISP_MEMZERO(at, sizeof (at2_entry_t));
1859 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1860 at->at_header.rqs_entry_count = 1;
1861 if (ISP_CAP_SCCFW(isp)) {
1862 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1864 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1866 at->at_status = CT_OK;
1867 at->at_rxid = cso->tag_id;
1868 at->at_iid = cso->ccb_h.target_id;
1869 isp_put_atio2(isp, at, qe);
1871 at_entry_t local, *at = &local;
1872 ISP_MEMZERO(at, sizeof (at_entry_t));
1873 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1874 at->at_header.rqs_entry_count = 1;
1875 at->at_iid = cso->init_id;
1876 at->at_iid |= XS_CHANNEL(ccb) << 7;
1877 at->at_tgt = cso->ccb_h.target_id;
1878 at->at_lun = cso->ccb_h.target_lun;
1879 at->at_status = CT_OK;
1880 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1881 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1882 isp_put_atio(isp, at, qe);
1884 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1885 ISP_SYNC_REQUEST(isp);
1886 isp_complete_ctio(ccb);
1890 isp_complete_ctio(union ccb *ccb)
1892 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1893 ccb->ccb_h.status |= CAM_REQ_CMP;
1895 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1896 isp_free_pcmd(XS_ISP(ccb), ccb);
1901 * Handle ATIO stuff that the generic code can't.
1902 * This means handling CDBs.
1906 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1910 struct ccb_accept_tio *atiop;
1911 atio_private_data_t *atp;
1914 * The firmware status (except for the QLTM_SVALID bit)
1915 * indicates why this ATIO was sent to us.
1917 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1919 * If the DISCONNECTS DISABLED bit is set in the flags field,
1920 * we're still connected on the SCSI bus.
1922 status = aep->at_status;
1923 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1925 * Bus Phase Sequence error. We should have sense data
1926 * suggested by the f/w. I'm not sure quite yet what
1927 * to do about this for CAM.
1929 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1930 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1933 if ((status & ~QLTM_SVALID) != AT_CDB) {
1934 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1935 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1939 bus = GET_BUS_VAL(aep->at_iid);
1940 tptr = get_lun_statep(isp, bus, aep->at_lun);
1942 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1945 * Because we can't autofeed sense data back with
1946 * a command for parallel SCSI, we can't give back
1947 * a CHECK CONDITION. We'll give back a BUSY status
1948 * instead. This works out okay because the only
1949 * time we should, in fact, get this, is in the
1950 * case that somebody configured us without the
1951 * blackhole driver, so they get what they deserve.
1953 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1958 atp = isp_get_atpd(isp, tptr, 0);
1959 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1960 if (atiop == NULL || atp == NULL) {
1962 * Because we can't autofeed sense data back with
1963 * a command for parallel SCSI, we can't give back
1964 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1965 * instead. This works out okay because the only time we
1966 * should, in fact, get this, is in the case that we've
1969 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1970 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1971 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1973 isp_put_atpd(isp, tptr, atp);
1975 rls_lun_statep(isp, tptr);
1978 atp->tag = aep->at_tag_val;
1979 if (atp->tag == 0) {
1982 atp->state = ATPD_STATE_ATIO;
1983 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1985 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1986 atiop->ccb_h.target_id = aep->at_tgt;
1987 atiop->ccb_h.target_lun = aep->at_lun;
1988 if (aep->at_flags & AT_NODISC) {
1989 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1991 atiop->ccb_h.flags = 0;
1994 if (status & QLTM_SVALID) {
1995 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1996 atiop->sense_len = amt;
1997 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1999 atiop->sense_len = 0;
2002 atiop->init_id = GET_IID_VAL(aep->at_iid);
2003 atiop->cdb_len = aep->at_cdblen;
2004 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2005 atiop->ccb_h.status = CAM_CDB_RECVD;
2007 * Construct a tag 'id' based upon tag value (which may be 0..255)
2008 * and the handle (which we have to preserve).
2010 atiop->tag_id = atp->tag;
2011 if (aep->at_flags & AT_TQAE) {
2012 atiop->tag_action = aep->at_tag_type;
2013 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2015 atp->orig_datalen = 0;
2016 atp->bytes_xfered = 0;
2017 atp->last_xframt = 0;
2018 atp->lun = aep->at_lun;
2019 atp->nphdl = aep->at_iid;
2020 atp->portid = PORT_NONE;
2022 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2023 atp->tattr = aep->at_tag_type;
2024 atp->state = ATPD_STATE_CAM;
2025 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
2026 rls_lun_statep(isp, tptr);
2030 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2035 struct ccb_accept_tio *atiop;
2037 atio_private_data_t *atp;
2038 inot_private_data_t *ntp;
2041 * The firmware status (except for the QLTM_SVALID bit)
2042 * indicates why this ATIO was sent to us.
2044 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2046 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
2047 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2048 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2052 if (ISP_CAP_SCCFW(isp)) {
2053 lun = aep->at_scclun;
2057 if (ISP_CAP_2KLOGIN(isp)) {
2058 nphdl = ((at2e_entry_t *)aep)->at_iid;
2060 nphdl = aep->at_iid;
2062 tptr = get_lun_statep(isp, 0, lun);
2064 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2066 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
2067 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2073 * Start any commands pending resources first.
2075 if (tptr->restart_queue) {
2076 inot_private_data_t *restart_queue = tptr->restart_queue;
2077 tptr->restart_queue = NULL;
2078 while (restart_queue) {
2079 ntp = restart_queue;
2080 restart_queue = ntp->rd.nt.nt_hba;
2081 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2082 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2083 isp_put_ntpd(isp, tptr, ntp);
2085 * If a recursion caused the restart queue to start to fill again,
2086 * stop and splice the new list on top of the old list and restore
2087 * it and go to noresrc.
2089 if (tptr->restart_queue) {
2090 ntp = tptr->restart_queue;
2091 tptr->restart_queue = restart_queue;
2092 while (restart_queue->rd.nt.nt_hba) {
2093 restart_queue = restart_queue->rd.nt.nt_hba;
2095 restart_queue->rd.nt.nt_hba = ntp;
2101 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2102 if (atiop == NULL) {
2106 atp = isp_get_atpd(isp, tptr, 0);
2111 atp->tag = aep->at_rxid;
2112 atp->state = ATPD_STATE_ATIO;
2113 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2115 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2116 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2117 atiop->ccb_h.target_lun = lun;
2120 * We don't get 'suggested' sense data as we do with SCSI cards.
2122 atiop->sense_len = 0;
2123 if (ISP_CAP_2KLOGIN(isp)) {
2125 * NB: We could not possibly have 2K logins if we
2126 * NB: also did not have SCC FW.
2128 atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2130 atiop->init_id = aep->at_iid;
2134 * If we're not in the port database, add ourselves.
2136 if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2138 (((uint64_t) aep->at_wwpn[0]) << 48) |
2139 (((uint64_t) aep->at_wwpn[1]) << 32) |
2140 (((uint64_t) aep->at_wwpn[2]) << 16) |
2141 (((uint64_t) aep->at_wwpn[3]) << 0);
2143 * However, make sure we delete ourselves if otherwise
2144 * we were there but at a different loop id.
2146 if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2147 isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2149 isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2151 atiop->cdb_len = ATIO2_CDBLEN;
2152 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2153 atiop->ccb_h.status = CAM_CDB_RECVD;
2154 atiop->tag_id = atp->tag;
2155 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2156 case ATIO2_TC_ATTR_SIMPLEQ:
2157 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2158 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2160 case ATIO2_TC_ATTR_HEADOFQ:
2161 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2162 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2164 case ATIO2_TC_ATTR_ORDERED:
2165 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2166 atiop->tag_action = MSG_ORDERED_Q_TAG;
2168 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2169 case ATIO2_TC_ATTR_UNTAGGED:
2171 atiop->tag_action = 0;
2175 atp->orig_datalen = aep->at_datalen;
2176 atp->bytes_xfered = 0;
2177 atp->last_xframt = 0;
2179 atp->nphdl = atiop->init_id;
2180 atp->sid = PORT_ANY;
2181 atp->oxid = aep->at_oxid;
2182 atp->cdb0 = aep->at_cdb[0];
2183 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2184 atp->state = ATPD_STATE_CAM;
2185 xpt_done((union ccb *)atiop);
2186 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);
2187 rls_lun_statep(isp, tptr);
2190 ntp = isp_get_ntpd(isp, tptr);
2192 rls_lun_statep(isp, tptr);
2193 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2196 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2197 ntp->rd.nt.nt_hba = tptr->restart_queue;
2198 tptr->restart_queue = ntp;
2199 rls_lun_statep(isp, tptr);
2203 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2206 uint16_t lun, chan, nphdl = NIL_HANDLE;
2208 uint64_t wwn = INI_NONE;
2211 struct ccb_accept_tio *atiop;
2212 atio_private_data_t *atp = NULL;
2213 atio_private_data_t *oatp;
2214 inot_private_data_t *ntp;
2216 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2217 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2218 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2221 * Find the N-port handle, and Virtual Port Index for this command.
2223 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2224 * We also, as a matter of course, need to know the WWN of the initiator too.
2226 if (ISP_CAP_MULTI_ID(isp)) {
2228 * Find the right channel based upon D_ID
2230 isp_find_chan_by_did(isp, did, &chan);
2232 if (chan == ISP_NOCHAN) {
2236 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2237 * It's a bit tricky here as we need to stash this command *somewhere*.
2240 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2241 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2242 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2245 tptr = get_lun_statep(isp, 0, 0);
2247 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2249 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);
2250 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2254 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2257 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);
2263 * Find the PDB entry for this initiator
2265 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2267 * If we're not in the port database terminate the exchange.
2269 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",
2270 __func__, aep->at_rxid, did, chan, sid);
2271 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2278 * Get the tstate pointer
2280 tptr = get_lun_statep(isp, chan, lun);
2282 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2284 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2285 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2291 * Start any commands pending resources first.
2293 if (tptr->restart_queue) {
2294 inot_private_data_t *restart_queue = tptr->restart_queue;
2295 tptr->restart_queue = NULL;
2296 while (restart_queue) {
2297 ntp = restart_queue;
2298 restart_queue = ntp->rd.nt.nt_hba;
2299 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2300 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2301 isp_put_ntpd(isp, tptr, ntp);
2303 * If a recursion caused the restart queue to start to fill again,
2304 * stop and splice the new list on top of the old list and restore
2305 * it and go to noresrc.
2307 if (tptr->restart_queue) {
2308 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
2309 if (restart_queue) {
2310 ntp = tptr->restart_queue;
2311 tptr->restart_queue = restart_queue;
2312 while (restart_queue->rd.nt.nt_hba) {
2313 restart_queue = restart_queue->rd.nt.nt_hba;
2315 restart_queue->rd.nt.nt_hba = ntp;
2323 * If the f/w is out of resources, just send a BUSY status back.
2325 if (aep->at_rxid == AT7_NORESRC_RXID) {
2326 rls_lun_statep(isp, tptr);
2327 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2332 * If we're out of resources, just send a BUSY status back.
2334 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2335 if (atiop == NULL) {
2336 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2340 atp = isp_get_atpd(isp, tptr, 0);
2342 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2345 oatp = isp_get_atpd(isp, tptr, aep->at_rxid);
2347 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d\n",
2348 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2350 * It's not a "no resource" condition- but we can treat it like one
2354 atp->tag = aep->at_rxid;
2355 atp->state = ATPD_STATE_ATIO;
2356 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2358 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2359 atiop->init_id = nphdl;
2360 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2361 atiop->ccb_h.target_lun = lun;
2362 atiop->sense_len = 0;
2363 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2365 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2367 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2368 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2369 atiop->cdb_len = cdbxlen;
2370 atiop->ccb_h.status = CAM_CDB_RECVD;
2371 atiop->tag_id = atp->tag;
2372 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2373 case FCP_CMND_TASK_ATTR_SIMPLE:
2374 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2375 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2377 case FCP_CMND_TASK_ATTR_HEAD:
2378 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2379 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2381 case FCP_CMND_TASK_ATTR_ORDERED:
2382 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2383 atiop->tag_action = MSG_ORDERED_Q_TAG;
2387 case FCP_CMND_TASK_ATTR_ACA:
2388 case FCP_CMND_TASK_ATTR_UNTAGGED:
2389 atiop->tag_action = 0;
2392 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2393 atp->bytes_xfered = 0;
2394 atp->last_xframt = 0;
2398 atp->oxid = aep->at_hdr.ox_id;
2399 atp->rxid = aep->at_hdr.rx_id;
2400 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2401 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2402 atp->state = ATPD_STATE_CAM;
2403 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);
2404 xpt_done((union ccb *)atiop);
2405 rls_lun_statep(isp, tptr);
2409 isp_put_atpd(isp, tptr, atp);
2411 ntp = isp_get_ntpd(isp, tptr);
2413 rls_lun_statep(isp, tptr);
2414 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2417 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2418 ntp->rd.nt.nt_hba = tptr->restart_queue;
2419 tptr->restart_queue = ntp;
2420 rls_lun_statep(isp, tptr);
2424 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2427 int sentstatus, ok, notify_cam, resid = 0;
2428 tstate_t *tptr = NULL;
2429 atio_private_data_t *atp = NULL;
2431 uint32_t tval, handle;
2434 * CTIO handles are 16 bits.
2435 * CTIO2 and CTIO7 are 32 bits.
2439 handle = ((ct_entry_t *)arg)->ct_syshandle;
2441 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2443 ccb = isp_find_xs_tgt(isp, handle);
2445 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2448 isp_destroy_tgt_handle(isp, handle);
2449 bus = XS_CHANNEL(ccb);
2450 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2452 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2454 KASSERT((tptr != NULL), ("cannot get state pointer"));
2455 if (isp->isp_nactive) {
2459 ct7_entry_t *ct = arg;
2461 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2463 rls_lun_statep(isp, tptr);
2464 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2468 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2469 ok = (ct->ct_nphdl == CT7_OK);
2470 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2471 ccb->ccb_h.status |= CAM_SENT_SENSE;
2473 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2474 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2475 resid = ct->ct_resid;
2476 atp->bytes_xfered += (atp->last_xframt - resid);
2477 atp->last_xframt = 0;
2479 if (ct->ct_nphdl == CT_HBA_RESET) {
2483 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2485 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2488 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2489 ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2490 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2491 } else if (IS_FC(isp)) {
2492 ct2_entry_t *ct = arg;
2494 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2496 rls_lun_statep(isp, tptr);
2497 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2500 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2501 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2502 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2503 ccb->ccb_h.status |= CAM_SENT_SENSE;
2505 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2506 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2507 resid = ct->ct_resid;
2508 atp->bytes_xfered += (atp->last_xframt - resid);
2509 atp->last_xframt = 0;
2511 if (ct->ct_status == CT_HBA_RESET) {
2515 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2517 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2519 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2520 ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2522 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2524 ct_entry_t *ct = arg;
2525 sentstatus = ct->ct_flags & CT_SENDSTATUS;
2526 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2528 * We *ought* to be able to get back to the original ATIO
2529 * here, but for some reason this gets lost. It's just as
2530 * well because it's squirrelled away as part of periph
2533 * We can live without it as long as we continue to use
2534 * the auto-replenish feature for CTIOs.
2536 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2537 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2541 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2543 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2544 } else if (ct->ct_status & QLTM_SVALID) {
2545 char *sp = (char *)ct;
2546 sp += CTIO_SENSE_OFFSET;
2547 ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2548 ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2549 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2551 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2552 resid = ct->ct_resid;
2554 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2555 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2556 tval = ct->ct_fwhandle;
2558 ccb->csio.resid += resid;
2561 * We're here either because intermediate data transfers are done
2562 * and/or the final status CTIO (which may have joined with a
2563 * Data Transfer) is done.
2565 * In any case, for this platform, the upper layers figure out
2566 * what to do next, so all we do here is collect status and
2567 * pass information along. Any DMA handles have already been
2570 if (notify_cam == 0) {
2571 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
2575 rls_lun_statep(isp, tptr);
2577 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? " FINAL " : "MIDTERM ", tval);
2579 if (!ok && !IS_24XX(isp)) {
2580 isp_target_putback_atio(ccb);
2582 isp_complete_ctio(ccb);
2587 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2589 (void) isp_notify_ack(isp, inot);
2593 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2596 switch (inp->in_status) {
2597 case IN_PORT_LOGOUT:
2599 * XXX: Need to delete this initiator's WWN from the database
2600 * XXX: Need to send this LOGOUT upstream
2602 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2604 case IN_PORT_CHANGED:
2605 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2607 case IN_GLOBAL_LOGO:
2608 isp_del_all_wwn_entries(isp, 0);
2609 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2617 atio_private_data_t *atp;
2619 struct ccb_immediate_notify *inot = NULL;
2621 if (ISP_CAP_SCCFW(isp)) {
2622 lun = inp->in_scclun;
2626 if (ISP_CAP_2KLOGIN(isp)) {
2627 loopid = ((in_fcentry_e_t *)inp)->in_iid;
2629 loopid = inp->in_iid;
2631 if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2636 tptr = get_lun_statep(isp, 0, lun);
2638 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2640 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2644 atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2647 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2648 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2651 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2652 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2654 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2657 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2660 isp_notify_t tmp, *nt = &tmp;
2661 ISP_MEMZERO(nt, sizeof (isp_notify_t));
2663 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2665 nt->nt_nphdl = loopid;
2666 nt->nt_sid = PORT_ANY;
2667 nt->nt_did = PORT_ANY;
2669 nt->nt_need_ack = 1;
2671 nt->nt_ncode = NT_ABORT_TASK;
2672 nt->nt_lreserved = inot;
2673 isp_handle_platform_target_tmf(isp, nt);
2676 rls_lun_statep(isp, tptr);
2683 (void) isp_notify_ack(isp, inp);
2688 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2693 uint8_t *ptr = NULL;
2696 nphdl = inot->in_nphdl;
2697 if (nphdl != NIL_HANDLE) {
2698 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2703 switch (inot->in_status) {
2704 case IN24XX_ELS_RCVD:
2707 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2710 * Note that we're just getting notification that an ELS was received
2711 * (possibly with some associated information sent upstream). This is
2712 * *not* the same as being given the ELS frame to accept or reject.
2714 switch (inot->in_status_subcode) {
2717 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2718 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2719 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
2720 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2721 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2722 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2723 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2724 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2725 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
2726 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2730 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2738 * Treat PRLI the same as PLOGI and make a database entry for it.
2740 if (inot->in_status_subcode == PLOGI)
2744 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2745 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2746 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
2747 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2748 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2749 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2750 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2751 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2752 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
2753 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2757 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2766 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2770 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2771 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);
2774 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,
2775 inot->in_rxid, inot->in_oxid);
2776 (void) isp_notify_ack(isp, inot);
2780 case IN24XX_PORT_LOGOUT:
2781 ptr = "PORT LOGOUT";
2782 if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2783 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2786 case IN24XX_PORT_CHANGED:
2788 ptr = "PORT CHANGED";
2791 case IN24XX_LIP_RESET:
2795 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);
2798 * All subcodes here are irrelevant. What is relevant
2799 * is that we need to terminate all active commands from
2800 * this initiator (known by N-port handle).
2802 /* XXX IMPLEMENT XXX */
2803 (void) isp_notify_ack(isp, inot);
2806 case IN24XX_LINK_RESET:
2807 case IN24XX_LINK_FAILED:
2808 case IN24XX_SRR_RCVD:
2810 (void) isp_notify_ack(isp, inot);
2816 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2819 if (isp->isp_state != ISP_RUNSTATE) {
2820 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2825 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2827 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2828 ct7_entry_t local, *cto = &local;
2829 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2834 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2835 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2840 ISP_MEMZERO(&local, sizeof (local));
2841 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2842 cto->ct_header.rqs_entry_count = 1;
2843 cto->ct_nphdl = nphdl;
2844 cto->ct_rxid = aep->at_rxid;
2845 cto->ct_vpidx = mp->nt_channel;
2846 cto->ct_iid_lo = sid;
2847 cto->ct_iid_hi = sid >> 16;
2848 cto->ct_oxid = aep->at_hdr.ox_id;
2849 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2850 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2851 return (isp_target_put_entry(isp, &local));
2855 * This case is for a responding to an ABTS frame
2857 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2860 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2862 if (mp->nt_need_ack) {
2863 uint8_t storage[QENTRY_LEN];
2864 ct7_entry_t *cto = (ct7_entry_t *) storage;
2865 abts_t *abts = (abts_t *)mp->nt_lreserved;
2867 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2868 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2869 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2870 cto->ct_header.rqs_entry_count = 1;
2871 cto->ct_nphdl = mp->nt_nphdl;
2872 cto->ct_rxid = abts->abts_rxid_task;
2873 cto->ct_iid_lo = mp->nt_sid;
2874 cto->ct_iid_hi = mp->nt_sid >> 16;
2875 cto->ct_oxid = abts->abts_ox_id;
2876 cto->ct_vpidx = mp->nt_channel;
2877 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2878 if (isp_target_put_entry(isp, cto)) {
2881 mp->nt_need_ack = 0;
2883 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2891 * Handle logout cases here
2893 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2894 isp_del_all_wwn_entries(isp, mp->nt_channel);
2897 if (mp->nt_ncode == NT_LOGOUT) {
2898 if (!IS_2100(isp) && IS_FC(isp)) {
2899 isp_del_wwn_entries(isp, mp);
2904 * General purpose acknowledgement
2906 if (mp->nt_need_ack) {
2907 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2908 return (isp_notify_ack(isp, mp->nt_lreserved));
2914 * Handle task management functions.
2916 * We show up here with a notify structure filled out.
2918 * The nt_lreserved tag points to the original queue entry
2921 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2925 struct ccb_immediate_notify *inot;
2926 inot_private_data_t *ntp = NULL;
2929 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2930 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2932 * NB: This assignment is necessary because of tricky type conversion.
2933 * XXX: This is tricky and I need to check this. If the lun isn't known
2934 * XXX: for the task management function, it does not of necessity follow
2935 * XXX: that it should go up stream to the wildcard listener.
2937 if (notify->nt_lun == LUN_ANY) {
2938 lun = CAM_LUN_WILDCARD;
2940 lun = notify->nt_lun;
2942 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2944 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2946 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2950 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2952 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2956 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2957 inot->initiator_id = CAM_TARGET_WILDCARD;
2959 inot->initiator_id = lp->handle;
2961 inot->seq_id = notify->nt_tagval;
2962 inot->tag_id = notify->nt_tagval >> 32;
2964 switch (notify->nt_ncode) {
2966 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2967 inot->arg = MSG_ABORT_TASK;
2969 case NT_ABORT_TASK_SET:
2970 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2971 inot->arg = MSG_ABORT_TASK_SET;
2974 inot->arg = MSG_CLEAR_ACA;
2976 case NT_CLEAR_TASK_SET:
2977 inot->arg = MSG_CLEAR_TASK_SET;
2980 inot->arg = MSG_LOGICAL_UNIT_RESET;
2982 case NT_TARGET_RESET:
2983 inot->arg = MSG_TARGET_RESET;
2986 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);
2990 ntp = isp_get_ntpd(isp, tptr);
2992 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2995 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2996 if (notify->nt_lreserved) {
2997 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2998 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
3000 ntp->rd.seq_id = notify->nt_tagval;
3001 ntp->rd.tag_id = notify->nt_tagval >> 32;
3004 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3005 rls_lun_statep(isp, tptr);
3006 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3007 inot->ccb_h.status = CAM_MESSAGE_RECV;
3008 xpt_done((union ccb *)inot);
3012 rls_lun_statep(isp, tptr);
3014 if (notify->nt_need_ack && notify->nt_lreserved) {
3015 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3016 (void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
3018 (void) isp_notify_ack(isp, notify->nt_lreserved);
3024 * Find the associated private data and mark it as dead so
3025 * we don't try to work on it any further.
3028 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3031 atio_private_data_t *atp;
3032 union ccb *accb = ccb->cab.abort_ccb;
3034 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3036 tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3038 ccb->ccb_h.status = CAM_REQ_INVALID;
3043 atp = isp_get_atpd(isp, tptr, accb->atio.tag_id);
3045 ccb->ccb_h.status = CAM_REQ_INVALID;
3048 ccb->ccb_h.status = CAM_REQ_CMP;
3050 rls_lun_statep(isp, tptr);
3054 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3056 atio_private_data_t *atp;
3057 inot_private_data_t *restart_queue = tptr->restart_queue;
3060 * First, clean any commands pending restart
3062 tptr->restart_queue = NULL;
3063 while (restart_queue) {
3064 uint32_t this_tag_id;
3065 inot_private_data_t *ntp = restart_queue;
3067 restart_queue = ntp->rd.nt.nt_hba;
3070 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3072 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3074 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3075 isp_put_ntpd(isp, tptr, ntp);
3077 ntp->rd.nt.nt_hba = tptr->restart_queue;
3078 tptr->restart_queue = ntp;
3083 * Now mark other ones dead as well.
3085 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3086 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3093 #ifdef ISP_INTERNAL_TARGET
3094 // #define ISP_FORCE_TIMEOUT 1
3095 // #define ISP_TEST_WWNS 1
3096 // #define ISP_TEST_SEPARATE_STATUS 1
3098 #define ccb_data_offset ppriv_field0
3099 #define ccb_atio ppriv_ptr1
3100 #define ccb_inot ppriv_ptr1
3102 #define MAX_ISP_TARG_TRANSFER (2 << 20)
3103 #define NISP_TARG_CMDS 1024
3104 #define NISP_TARG_NOTIFIES 1024
3105 #define DISK_SHIFT 9
3106 #define JUNK_SIZE 256
3109 #define VERIFY_10 0x2f
3112 TAILQ_HEAD(ccb_queue, ccb_hdr);
3113 extern u_int vm_kmem_size;
3115 static uint32_t disk_size;
3116 static uint8_t *disk_data = NULL;
3117 static uint8_t *junk_data;
3118 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3119 struct isptarg_softc {
3120 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3121 struct ccb_queue work_queue;
3122 struct ccb_queue rework_queue;
3123 struct ccb_queue running_queue;
3124 struct ccb_queue inot_queue;
3125 struct cam_periph *periph;
3126 struct cam_path *path;
3129 static periph_ctor_t isptargctor;
3130 static periph_dtor_t isptargdtor;
3131 static periph_start_t isptargstart;
3132 static periph_init_t isptarginit;
3133 static void isptarg_done(struct cam_periph *, union ccb *);
3134 static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3137 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3139 static struct periph_driver isptargdriver =
3141 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3150 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3152 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3154 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__,
3155 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3156 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3157 ack->ccb_h.flags = 0;
3158 ack->ccb_h.retry_count = 0;
3159 ack->ccb_h.cbfcnp = isptarg_done;
3160 ack->ccb_h.timeout = 0;
3161 ack->ccb_h.ccb_inot = inot;
3162 ack->tag_id = inot->tag_id;
3163 ack->seq_id = inot->seq_id;
3164 ack->initiator_id = inot->initiator_id;
3169 isptargstart(struct cam_periph *periph, union ccb *iccb)
3171 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3172 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3173 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3174 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3175 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3178 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3179 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3180 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3181 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3182 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3185 int i, more = 0, last;
3186 struct isptarg_softc *softc = periph->softc;
3187 struct ccb_scsiio *csio;
3188 lun_id_t return_lun;
3189 struct ccb_accept_tio *atio;
3190 uint8_t *cdb, *ptr, status;
3192 uint32_t data_len, flags;
3193 struct ccb_hdr *ccbh;
3195 mtx_assert(periph->sim->mtx, MA_OWNED);
3196 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,
3197 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3199 * Check for immediate notifies first
3201 ccbh = TAILQ_FIRST(&softc->inot_queue);
3203 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3204 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3205 xpt_schedule(periph, 1);
3207 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3212 * Check the rework (continuation) work queue first.
3214 ccbh = TAILQ_FIRST(&softc->rework_queue);
3216 atio = (struct ccb_accept_tio *)ccbh;
3217 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3218 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3220 ccbh = TAILQ_FIRST(&softc->work_queue);
3222 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3223 xpt_release_ccb(iccb);
3226 atio = (struct ccb_accept_tio *)ccbh;
3227 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3228 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3229 atio->ccb_h.ccb_data_offset = 0;
3232 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3239 status = SCSI_STATUS_OK;
3240 flags = CAM_SEND_STATUS;
3241 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3242 cdb = atio->cdb_io.cdb_bytes;
3243 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,
3244 cdb[0], atio->ccb_h.ccb_data_offset);
3246 return_lun = XS_LUN(atio);
3247 if (return_lun != 0) {
3248 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3249 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3250 status = SCSI_STATUS_CHECK_COND;
3251 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3252 atio->sense_data.add_sense_code = 0x25;
3253 atio->sense_data.add_sense_code_qual = 0x0;
3254 atio->sense_len = sizeof (atio->sense_data);
3256 return_lun = CAM_LUN_WILDCARD;
3261 flags |= CAM_DIR_IN;
3262 data_len = sizeof (atio->sense_data);
3263 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3264 memset(junk_data+1, 0, data_len-1);
3265 if (data_len > cdb[4]) {
3269 data_ptr = junk_data;
3276 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3277 status = SCSI_STATUS_CHECK_COND;
3278 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3279 atio->sense_data.add_sense_code = 0x5;
3280 atio->sense_data.add_sense_code_qual = 0x24;
3281 atio->sense_len = sizeof (atio->sense_data);
3283 #ifdef ISP_FORCE_TIMEOUT
3288 xpt_schedule(periph, 1);
3295 #ifdef ISP_TEST_SEPARATE_STATUS
3296 if (last && data_len) {
3301 flags &= ~CAM_SEND_STATUS;
3304 atio->ccb_h.ccb_data_offset += data_len;
3305 flags |= CAM_DIR_IN;
3307 flags |= CAM_DIR_NONE;
3315 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
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);
3322 #ifdef ISP_FORCE_TIMEOUT
3327 xpt_schedule(periph, 1);
3334 #ifdef ISP_TEST_SEPARATE_STATUS
3335 if (last && data_len) {
3340 flags &= ~CAM_SEND_STATUS;
3343 atio->ccb_h.ccb_data_offset += data_len;
3344 flags |= CAM_DIR_OUT;
3346 flags |= CAM_DIR_NONE;
3351 flags |= CAM_DIR_IN;
3352 if (cdb[1] || cdb[2] || cdb[3]) {
3353 status = SCSI_STATUS_CHECK_COND;
3354 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3355 atio->sense_data.add_sense_code = 0x5;
3356 atio->sense_data.add_sense_code_qual = 0x20;
3357 atio->sense_len = sizeof (atio->sense_data);
3360 data_len = sizeof (iqd);
3361 if (data_len > cdb[4]) {
3365 if (XS_LUN(iccb) != 0) {
3366 memcpy(junk_data, niliqd, sizeof (iqd));
3368 memcpy(junk_data, iqd, sizeof (iqd));
3370 data_ptr = junk_data;
3373 case TEST_UNIT_READY:
3374 flags |= CAM_DIR_NONE;
3377 status = SCSI_STATUS_CHECK_COND;
3378 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3379 atio->sense_data.add_sense_code = 0x28;
3380 atio->sense_data.add_sense_code_qual = 0x0;
3381 atio->sense_len = sizeof (atio->sense_data);
3384 case SYNCHRONIZE_CACHE:
3389 flags |= CAM_DIR_NONE;
3393 flags |= CAM_DIR_IN;
3394 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3395 status = SCSI_STATUS_CHECK_COND;
3396 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3397 atio->sense_data.add_sense_code = 0x5;
3398 atio->sense_data.add_sense_code_qual = 0x24;
3399 atio->sense_len = sizeof (atio->sense_data);
3402 if (cdb[8] & 0x1) { /* PMI */
3403 junk_data[0] = 0xff;
3404 junk_data[1] = 0xff;
3405 junk_data[2] = 0xff;
3406 junk_data[3] = 0xff;
3408 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3409 if (last_blk < 0xffffffffULL) {
3410 junk_data[0] = (last_blk >> 24) & 0xff;
3411 junk_data[1] = (last_blk >> 16) & 0xff;
3412 junk_data[2] = (last_blk >> 8) & 0xff;
3413 junk_data[3] = (last_blk) & 0xff;
3415 junk_data[0] = 0xff;
3416 junk_data[1] = 0xff;
3417 junk_data[2] = 0xff;
3418 junk_data[3] = 0xff;
3421 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3422 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3423 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3424 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3425 data_ptr = junk_data;
3429 flags |= CAM_DIR_IN;
3430 memset(junk_data, 0, JUNK_SIZE);
3431 junk_data[0] = (1 << 3) >> 24;
3432 junk_data[1] = (1 << 3) >> 16;
3433 junk_data[2] = (1 << 3) >> 8;
3434 junk_data[3] = (1 << 3);
3436 for (i = 0; i < 1; i++) {
3437 ptr = &junk_data[8 + (1 << 3)];
3439 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3443 data_ptr = junk_data;
3444 data_len = (ptr + 8) - junk_data;
3448 flags |= CAM_DIR_NONE;
3449 status = SCSI_STATUS_CHECK_COND;
3450 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3451 atio->sense_data.add_sense_code = 0x5;
3452 atio->sense_data.add_sense_code_qual = 0x20;
3453 atio->sense_len = sizeof (atio->sense_data);
3458 * If we are done with the transaction, tell the
3459 * controller to send status and perform a CMD_CMPLT.
3460 * If we have associated sense data, see if we can
3463 if (status == SCSI_STATUS_CHECK_COND) {
3464 flags |= CAM_SEND_SENSE;
3465 csio->sense_len = atio->sense_len;
3466 csio->sense_data = atio->sense_data;
3467 flags &= ~CAM_DIR_MASK;
3471 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3472 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3473 iccb->ccb_h.target_lun = return_lun;
3474 iccb->ccb_h.ccb_atio = atio;
3477 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3478 cam_release_devq(periph->path, 0, 0, 0, 0);
3479 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3482 xpt_schedule(periph, 1);
3487 isptargctor(struct cam_periph *periph, void *arg)
3489 struct isptarg_softc *softc;
3491 softc = (struct isptarg_softc *)arg;
3492 periph->softc = softc;
3493 softc->periph = periph;
3494 softc->path = periph->path;
3495 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3496 return (CAM_REQ_CMP);
3500 isptargdtor(struct cam_periph *periph)
3502 struct isptarg_softc *softc;
3503 softc = (struct isptarg_softc *)periph->softc;
3504 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3505 softc->periph = NULL;
3507 periph->softc = NULL;
3511 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3513 struct isptarg_softc *softc;
3515 struct ccb_accept_tio *atio;
3516 struct ccb_immediate_notify *inot;
3519 softc = (struct isptarg_softc *)periph->softc;
3521 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3523 switch (ccb->ccb_h.func_code) {
3524 case XPT_ACCEPT_TARGET_IO:
3525 atio = (struct ccb_accept_tio *) ccb;
3526 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3527 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3528 xpt_schedule(periph, 1);
3530 case XPT_IMMEDIATE_NOTIFY:
3531 inot = (struct ccb_immediate_notify *) ccb;
3532 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__);
3533 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3534 xpt_schedule(periph, 1);
3536 case XPT_CONT_TARGET_IO:
3537 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3538 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3539 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3541 atio = ccb->ccb_h.ccb_atio;
3542 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3543 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3544 xpt_action((union ccb *)atio);
3545 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3546 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3547 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3548 xpt_schedule(periph, 1);
3550 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3551 xpt_action((union ccb *)atio);
3553 xpt_release_ccb(ccb);
3555 case XPT_NOTIFY_ACKNOWLEDGE:
3556 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3557 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3558 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3560 inot = ccb->ccb_h.ccb_inot;
3561 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);
3562 xpt_release_ccb(ccb);
3563 xpt_action((union ccb *)inot);
3566 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3572 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3574 struct ac_contract *acp = arg;
3575 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3577 if (code != AC_CONTRACT) {
3580 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3584 isp_target_thread(ispsoftc_t *isp, int chan)
3586 union ccb *ccb = NULL;
3590 struct isptarg_softc *softc = NULL;
3591 struct cam_periph *periph = NULL, *wperiph = NULL;
3592 struct cam_path *path, *wpath;
3593 struct cam_sim *sim;
3595 if (disk_data == NULL) {
3596 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3597 if (disk_size < (50 << 20)) {
3598 disk_size = 50 << 20;
3600 disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3601 if (disk_data == NULL) {
3602 isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
3605 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3607 junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3608 if (junk_data == NULL) {
3609 isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
3614 softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3615 if (softc == NULL) {
3616 isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
3619 TAILQ_INIT(&softc->work_queue);
3620 TAILQ_INIT(&softc->rework_queue);
3621 TAILQ_INIT(&softc->running_queue);
3622 TAILQ_INIT(&softc->inot_queue);
3625 periphdriver_register(&isptargdriver);
3626 ISP_GET_PC(isp, chan, sim, sim);
3627 ISP_GET_PC(isp, chan, path, path);
3628 status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3629 if (status != CAM_REQ_CMP) {
3630 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3633 status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3634 if (status != CAM_REQ_CMP) {
3635 xpt_free_path(wpath);
3636 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3640 ccb = xpt_alloc_ccb();
3643 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3644 if (status != CAM_REQ_CMP) {
3646 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3649 wperiph = cam_periph_find(wpath, "isptarg");
3650 if (wperiph == NULL) {
3652 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3656 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3657 if (status != CAM_REQ_CMP) {
3659 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3663 periph = cam_periph_find(path, "isptarg");
3664 if (periph == NULL) {
3666 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3670 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3671 if (status != CAM_REQ_CMP) {
3673 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3679 ccb = xpt_alloc_ccb();
3682 * Make sure role is none.
3684 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3685 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3686 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3687 #ifdef ISP_TEST_WWNS
3688 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3689 ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3690 ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3692 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3702 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3703 ccb->ccb_h.func_code = XPT_EN_LUN;
3704 ccb->cel.enable = 1;
3708 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3710 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3714 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3715 ccb->ccb_h.func_code = XPT_EN_LUN;
3716 ccb->cel.enable = 1;
3720 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3722 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3730 ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3731 for (i = 0; i < 4; i++) {
3732 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3733 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3734 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3735 ccb->ccb_h.cbfcnp = isptarg_done;
3740 for (i = 0; i < NISP_TARG_CMDS; i++) {
3741 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3742 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3743 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3744 ccb->ccb_h.cbfcnp = isptarg_done;
3749 for (i = 0; i < 4; i++) {
3750 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3751 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3752 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3753 ccb->ccb_h.cbfcnp = isptarg_done;
3758 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3759 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3760 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3761 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3762 ccb->ccb_h.cbfcnp = isptarg_done;
3769 * Now turn it all back on
3771 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3772 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3773 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3774 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3780 * Okay, while things are still active, sleep...
3784 ISP_GET_PC(isp, chan, proc_active, i);
3788 msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3794 cam_periph_invalidate(wperiph);
3797 cam_periph_invalidate(periph);
3800 free(junk_data, M_ISPTARG);
3803 free(disk_data, M_ISPTARG);
3806 free(softc, M_ISPTARG);
3808 xpt_free_path(path);
3809 xpt_free_path(wpath);
3813 isp_target_thread_pi(void *arg)
3815 struct isp_spi *pi = arg;
3816 isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3820 isp_target_thread_fc(void *arg)
3822 struct isp_fc *fc = arg;
3823 isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3827 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3829 uint32_t cnt, curcnt;
3835 cnt = (((uint32_t)cdb[10]) << 24) |
3836 (((uint32_t)cdb[11]) << 16) |
3837 (((uint32_t)cdb[12]) << 8) |
3838 ((uint32_t)cdb[13]);
3840 lba = (((uint64_t)cdb[2]) << 56) |
3841 (((uint64_t)cdb[3]) << 48) |
3842 (((uint64_t)cdb[4]) << 40) |
3843 (((uint64_t)cdb[5]) << 32) |
3844 (((uint64_t)cdb[6]) << 24) |
3845 (((uint64_t)cdb[7]) << 16) |
3846 (((uint64_t)cdb[8]) << 8) |
3851 cnt = (((uint32_t)cdb[6]) << 16) |
3852 (((uint32_t)cdb[7]) << 8) |
3853 ((u_int32_t)cdb[8]);
3855 lba = (((uint32_t)cdb[2]) << 24) |
3856 (((uint32_t)cdb[3]) << 16) |
3857 (((uint32_t)cdb[4]) << 8) |
3862 cnt = (((uint32_t)cdb[7]) << 8) |
3863 ((u_int32_t)cdb[8]);
3865 lba = (((uint32_t)cdb[2]) << 24) |
3866 (((uint32_t)cdb[3]) << 16) |
3867 (((uint32_t)cdb[4]) << 8) |
3876 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
3877 (((uint32_t)cdb[2]) << 8) |
3887 if (offset == cnt) {
3892 if (lba + cnt > dl) {
3897 curcnt = MAX_ISP_TARG_TRANSFER;
3898 if (offset + curcnt >= cnt) {
3899 curcnt = cnt - offset;
3905 *kp = &dp[lba + offset];
3913 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3915 struct cam_sim *sim;
3919 sim = (struct cam_sim *)cbarg;
3920 isp = (ispsoftc_t *) cam_sim_softc(sim);
3921 bus = cam_sim_bus(sim);
3922 tgt = xpt_path_target_id(path);
3925 case AC_LOST_DEVICE:
3927 uint16_t oflags, nflags;
3928 sdparam *sdp = SDPARAM(isp, bus);
3931 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3932 #ifndef ISP_TARGET_MODE
3933 nflags &= DPARM_SAFE_DFLT;
3934 if (isp->isp_loaded_fw) {
3935 nflags |= DPARM_NARROW | DPARM_ASYNC;
3938 nflags = DPARM_DEFAULT;
3940 oflags = sdp->isp_devparam[tgt].goal_flags;
3941 sdp->isp_devparam[tgt].goal_flags = nflags;
3942 sdp->isp_devparam[tgt].dev_update = 1;
3944 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3945 sdp->isp_devparam[tgt].goal_flags = oflags;
3950 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3956 isp_poll(struct cam_sim *sim)
3958 ispsoftc_t *isp = cam_sim_softc(sim);
3960 uint16_t sema, mbox;
3962 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3963 isp_intr(isp, isr, sema, mbox);
3969 isp_watchdog(void *arg)
3971 struct ccb_scsiio *xs = arg;
3973 uint32_t ohandle = ISP_HANDLE_FREE, handle;
3977 handle = isp_find_handle(isp, xs);
3979 if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3980 isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3981 callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3988 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3990 if (handle != ISP_HANDLE_FREE) {
3992 uint16_t sema, mbox;
3993 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3994 isp_intr(isp, isr, sema, mbox);
3997 handle = isp_find_handle(isp, xs);
3999 if (handle != ISP_HANDLE_FREE) {
4001 * Try and make sure the command is really dead before
4002 * we release the handle (and DMA resources) for reuse.
4004 * If we are successful in aborting the command then
4005 * we're done here because we'll get the command returned
4008 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
4013 * Note that after calling the above, the command may in
4014 * fact have been completed.
4016 xs = isp_find_xs(isp, handle);
4019 * If the command no longer exists, then we won't
4020 * be able to find the xs again with this handle.
4027 * After this point, the command is really dead.
4029 if (XS_XFRLEN(xs)) {
4030 ISP_DMAFREE(isp, xs, handle);
4032 isp_destroy_handle(isp, handle);
4033 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
4034 XS_SETERR(xs, CAM_CMD_TIMEOUT);
4035 isp_prt_endcmd(isp, xs);
4038 if (ohandle != ISP_HANDLE_FREE) {
4039 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
4041 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
4047 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
4050 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4052 if (isp_autoconfig == 0) {
4057 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
4059 ccb = xpt_alloc_ccb_nowait();
4061 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
4064 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4065 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
4073 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
4075 struct cam_path *tp;
4076 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4078 if (isp_autoconfig == 0) {
4081 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4082 xpt_async(AC_LOST_DEVICE, tp, NULL);
4088 * Gone Device Timer Function- when we have decided that a device has gone
4089 * away, we wait a specific period of time prior to telling the OS it has
4092 * This timer function fires once a second and then scans the port database
4093 * for devices that are marked dead but still have a virtual target assigned.
4094 * We decrement a counter for that port database entry, and when it hits zero,
4095 * we tell the OS the device has gone away.
4100 struct isp_fc *fc = arg;
4101 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4105 isp_gdt_task(void *arg, int pending)
4107 struct isp_fc *fc = arg;
4108 ispsoftc_t *isp = fc->isp;
4109 int chan = fc - isp->isp_osinfo.pc.fc;
4111 int dbidx, tgt, more_to_do = 0;
4114 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4115 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4116 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4118 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4121 if (lp->dev_map_idx == 0 || lp->target_mode) {
4124 if (lp->gone_timer != 0) {
4125 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);
4126 lp->gone_timer -= 1;
4130 tgt = lp->dev_map_idx - 1;
4131 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4132 lp->dev_map_idx = 0;
4133 lp->state = FC_PORTDB_STATE_NIL;
4134 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4135 isp_make_gone(isp, chan, tgt);
4139 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4141 callout_deactivate(&fc->gdt);
4142 isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4149 * Loop Down Timer Function- when loop goes down, a timer is started and
4150 * and after it expires we come here and take all probational devices that
4151 * the OS knows about and the tell the OS that they've gone away.
4153 * We don't clear the devices out of our port database because, when loop
4154 * come back up, we have to do some actual cleanup with the chip at that
4155 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4160 struct isp_fc *fc = arg;
4161 taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4165 isp_ldt_task(void *arg, int pending)
4167 struct isp_fc *fc = arg;
4168 ispsoftc_t *isp = fc->isp;
4169 int chan = fc - isp->isp_osinfo.pc.fc;
4174 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4175 callout_deactivate(&fc->ldt);
4178 * Notify to the OS all targets who we now consider have departed.
4180 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4181 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4183 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4186 if (lp->dev_map_idx == 0 || lp->target_mode) {
4191 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4195 for (i = 0; i < isp->isp_maxcmds; i++) {
4196 struct ccb_scsiio *xs;
4198 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4201 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4204 if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4207 isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4208 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4212 * Mark that we've announced that this device is gone....
4217 * but *don't* change the state of the entry. Just clear
4218 * any target id stuff and announce to CAM that the
4219 * device is gone. This way any necessary PLOGO stuff
4220 * will happen when loop comes back up.
4223 tgt = lp->dev_map_idx - 1;
4224 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4225 lp->dev_map_idx = 0;
4226 lp->state = FC_PORTDB_STATE_NIL;
4227 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4228 isp_make_gone(isp, chan, tgt);
4231 if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4232 isp_unfreeze_loopdown(isp, chan);
4235 * The loop down timer has expired. Wake up the kthread
4236 * to notice that fact (or make it false).
4239 fc->loop_down_time = fc->loop_down_limit+1;
4245 isp_kthread(void *arg)
4247 struct isp_fc *fc = arg;
4248 ispsoftc_t *isp = fc->isp;
4249 int chan = fc - isp->isp_osinfo.pc.fc;
4252 mtx_lock(&isp->isp_osinfo.lock);
4257 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4258 lb = isp_fc_runstate(isp, chan, 250000);
4261 * Our action is different based upon whether we're supporting
4262 * Initiator mode or not. If we are, we might freeze the simq
4263 * when loop is down and set all sorts of different delays to
4266 * If not, we simply just wait for loop to come up.
4268 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4270 * Increment loop down time by the last sleep interval
4272 fc->loop_down_time += slp;
4275 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4277 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4281 * If we've never seen loop up and we've waited longer
4282 * than quickboot time, or we've seen loop up but we've
4283 * waited longer than loop_down_limit, give up and go
4284 * to sleep until loop comes up.
4286 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4287 lim = isp_quickboot_time;
4289 lim = fc->loop_down_limit;
4291 if (fc->loop_down_time >= lim) {
4292 isp_freeze_loopdown(isp, chan, "loop limit hit");
4294 } else if (fc->loop_down_time < 10) {
4296 } else if (fc->loop_down_time < 30) {
4298 } else if (fc->loop_down_time < 60) {
4300 } else if (fc->loop_down_time < 120) {
4307 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4308 fc->loop_down_time += slp;
4311 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4312 fc->loop_down_time = 0;
4318 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4319 * now so that CAM can start sending us commands.
4321 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4322 * or kill the commands, as appropriate.
4325 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4326 isp_unfreeze_loopdown(isp, chan);
4329 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4331 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4334 * If slp is zero, we're waking up for the first time after
4335 * things have been okay. In this case, we set a deferral state
4336 * for all commands and delay hysteresis seconds before starting
4337 * the FC state evaluation. This gives the loop/fabric a chance
4340 if (slp == 0 && fc->hysteresis) {
4341 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4342 mtx_unlock(&isp->isp_osinfo.lock);
4343 pause("ispt", fc->hysteresis * hz);
4344 mtx_lock(&isp->isp_osinfo.lock);
4347 mtx_unlock(&isp->isp_osinfo.lock);
4351 isp_action(struct cam_sim *sim, union ccb *ccb)
4353 int bus, tgt, ts, error, lim;
4355 struct ccb_trans_settings *cts;
4357 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4359 isp = (ispsoftc_t *)cam_sim_softc(sim);
4360 mtx_assert(&isp->isp_lock, MA_OWNED);
4362 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4364 if (isp->isp_state != ISP_INITSTATE) {
4366 * Lie. Say it was a selection timeout.
4368 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4369 xpt_freeze_devq(ccb->ccb_h.path, 1);
4373 isp->isp_state = ISP_RUNSTATE;
4375 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4376 ISP_PCMD(ccb) = NULL;
4378 switch (ccb->ccb_h.func_code) {
4379 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4380 bus = XS_CHANNEL(ccb);
4382 * Do a couple of preliminary checks...
4384 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4385 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4386 ccb->ccb_h.status = CAM_REQ_INVALID;
4392 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4393 xpt_print(ccb->ccb_h.path, "invalid target\n");
4394 ccb->ccb_h.status = CAM_PATH_INVALID;
4395 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4396 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4397 ccb->ccb_h.status = CAM_PATH_INVALID;
4399 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4404 ccb->csio.scsi_status = SCSI_STATUS_OK;
4405 if (isp_get_pcmd(isp, ccb)) {
4406 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4407 cam_freeze_devq(ccb->ccb_h.path);
4408 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4412 error = isp_start((XS_T *) ccb);
4415 XS_CMD_S_CLEAR(ccb);
4416 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4417 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4420 ts = ccb->ccb_h.timeout;
4421 if (ts == CAM_TIME_DEFAULT) {
4424 ts = isp_mstohz(ts);
4426 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4430 * We get this result for FC devices if the loop state isn't ready yet
4431 * or if the device in question has gone zombie on us.
4433 * If we've never seen Loop UP at all, we requeue this request and wait
4434 * for the initial loop up delay to expire.
4436 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4437 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4438 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4439 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4441 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);
4443 ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4444 xpt_freeze_devq(ccb->ccb_h.path, 1);
4445 isp_free_pcmd(isp, ccb);
4449 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4450 cam_freeze_devq(ccb->ccb_h.path);
4451 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4452 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4453 isp_free_pcmd(isp, ccb);
4457 isp_free_pcmd(isp, ccb);
4458 cam_freeze_devq(ccb->ccb_h.path);
4459 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4460 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4464 isp_done((struct ccb_scsiio *) ccb);
4467 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4468 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4469 isp_free_pcmd(isp, ccb);
4474 #ifdef ISP_TARGET_MODE
4475 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
4476 if (ccb->cel.enable) {
4477 isp_enable_lun(isp, ccb);
4479 isp_disable_lun(isp, ccb);
4482 case XPT_IMMED_NOTIFY:
4483 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
4484 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
4486 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4488 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4494 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4495 str = "XPT_IMMEDIATE_NOTIFY";
4496 tag = ccb->cin1.seq_id;
4498 tag = ccb->atio.tag_id;
4499 str = "XPT_ACCEPT_TARGET_IO";
4501 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4502 dump_tstates(isp, XS_CHANNEL(ccb));
4503 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4506 ccb->ccb_h.sim_priv.entries[0].field = 0;
4507 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4508 ccb->ccb_h.flags = 0;
4510 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4511 if (ccb->atio.tag_id) {
4512 atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4514 isp_put_atpd(isp, tptr, atp);
4518 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4519 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4520 ccb->atio.tag_id, tptr->atio_count);
4521 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4522 if (ccb->cin1.tag_id) {
4523 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4525 isp_put_ntpd(isp, tptr, ntp);
4529 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4530 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4531 ccb->cin1.seq_id, tptr->inot_count);
4532 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4534 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4535 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4536 ccb->cin1.seq_id, tptr->inot_count);
4538 rls_lun_statep(isp, tptr);
4539 ccb->ccb_h.status = CAM_REQ_INPROG;
4542 case XPT_NOTIFY_ACK:
4543 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4545 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
4548 inot_private_data_t *ntp;
4551 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4552 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4555 * All the relevant path information is in the associated immediate notify
4557 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);
4558 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4560 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__,
4561 ccb->cna2.tag_id, ccb->cna2.seq_id);
4562 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4566 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4567 rls_lun_statep(isp, tptr);
4568 cam_freeze_devq(ccb->ccb_h.path);
4569 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4570 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4573 isp_put_ntpd(isp, tptr, ntp);
4574 rls_lun_statep(isp, tptr);
4575 ccb->ccb_h.status = CAM_REQ_CMP;
4576 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);
4580 case XPT_CONT_TARGET_IO:
4581 isp_target_start_ctio(isp, ccb);
4584 case XPT_RESET_DEV: /* BDR the specified SCSI device */
4586 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4587 tgt = ccb->ccb_h.target_id;
4590 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4592 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4594 ccb->ccb_h.status = CAM_REQ_CMP;
4598 case XPT_ABORT: /* Abort the specified CCB */
4600 union ccb *accb = ccb->cab.abort_ccb;
4601 switch (accb->ccb_h.func_code) {
4602 #ifdef ISP_TARGET_MODE
4603 case XPT_ACCEPT_TARGET_IO:
4604 isp_target_mark_aborted(isp, ccb);
4608 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4610 ccb->ccb_h.status = CAM_UA_ABORT;
4612 ccb->ccb_h.status = CAM_REQ_CMP;
4616 ccb->ccb_h.status = CAM_REQ_INVALID;
4620 * This is not a queued CCB, so the caller expects it to be
4621 * complete when control is returned.
4625 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
4626 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
4628 if (!IS_CURRENT_SETTINGS(cts)) {
4629 ccb->ccb_h.status = CAM_REQ_INVALID;
4633 tgt = cts->ccb_h.target_id;
4634 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4636 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4637 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4638 sdparam *sdp = SDPARAM(isp, bus);
4641 if (spi->valid == 0 && scsi->valid == 0) {
4642 ccb->ccb_h.status = CAM_REQ_CMP;
4648 * We always update (internally) from goal_flags
4649 * so any request to change settings just gets
4650 * vectored to that location.
4652 dptr = &sdp->isp_devparam[tgt].goal_flags;
4654 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4655 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4656 *dptr |= DPARM_DISC;
4658 *dptr &= ~DPARM_DISC;
4661 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4662 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4663 *dptr |= DPARM_TQING;
4665 *dptr &= ~DPARM_TQING;
4668 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4669 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4670 *dptr |= DPARM_WIDE;
4672 *dptr &= ~DPARM_WIDE;
4678 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4679 *dptr |= DPARM_SYNC;
4681 * XXX: CHECK FOR LEGALITY
4683 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4684 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4686 *dptr &= ~DPARM_SYNC;
4688 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,
4689 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4690 sdp->isp_devparam[tgt].dev_update = 1;
4693 ccb->ccb_h.status = CAM_REQ_CMP;
4696 case XPT_GET_TRAN_SETTINGS:
4698 tgt = cts->ccb_h.target_id;
4699 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4701 fcparam *fcp = FCPARAM(isp, bus);
4702 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4703 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4704 unsigned int hdlidx;
4706 cts->protocol = PROTO_SCSI;
4707 cts->protocol_version = SCSI_REV_2;
4708 cts->transport = XPORT_FC;
4709 cts->transport_version = 0;
4711 scsi->valid = CTS_SCSI_VALID_TQ;
4712 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4713 fc->valid = CTS_FC_VALID_SPEED;
4714 fc->bitrate = 100000;
4715 fc->bitrate *= fcp->isp_gbspeed;
4716 hdlidx = fcp->isp_dev_map[tgt] - 1;
4717 if (hdlidx < MAX_FC_TARG) {
4718 fcportdb_t *lp = &fcp->portdb[hdlidx];
4719 fc->wwnn = lp->node_wwn;
4720 fc->wwpn = lp->port_wwn;
4721 fc->port = lp->portid;
4722 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4725 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4726 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4727 sdparam *sdp = SDPARAM(isp, bus);
4728 uint16_t dval, pval, oval;
4730 if (IS_CURRENT_SETTINGS(cts)) {
4731 sdp->isp_devparam[tgt].dev_refresh = 1;
4733 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4734 dval = sdp->isp_devparam[tgt].actv_flags;
4735 oval = sdp->isp_devparam[tgt].actv_offset;
4736 pval = sdp->isp_devparam[tgt].actv_period;
4738 dval = sdp->isp_devparam[tgt].nvrm_flags;
4739 oval = sdp->isp_devparam[tgt].nvrm_offset;
4740 pval = sdp->isp_devparam[tgt].nvrm_period;
4743 cts->protocol = PROTO_SCSI;
4744 cts->protocol_version = SCSI_REV_2;
4745 cts->transport = XPORT_SPI;
4746 cts->transport_version = 2;
4752 if (dval & DPARM_DISC) {
4753 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4755 if ((dval & DPARM_SYNC) && oval && pval) {
4756 spi->sync_offset = oval;
4757 spi->sync_period = pval;
4759 spi->sync_offset = 0;
4760 spi->sync_period = 0;
4762 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4763 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4764 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4765 if (dval & DPARM_WIDE) {
4766 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4768 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4770 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4771 scsi->valid = CTS_SCSI_VALID_TQ;
4772 if (dval & DPARM_TQING) {
4773 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4775 spi->valid |= CTS_SPI_VALID_DISC;
4777 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4778 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4780 ccb->ccb_h.status = CAM_REQ_CMP;
4784 case XPT_CALC_GEOMETRY:
4785 cam_calc_geometry(&ccb->ccg, 1);
4789 case XPT_RESET_BUS: /* Reset the specified bus */
4790 bus = cam_sim_bus(sim);
4791 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4793 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4798 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4801 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4803 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4805 ccb->ccb_h.status = CAM_REQ_CMP;
4809 case XPT_TERM_IO: /* Terminate the I/O process */
4810 ccb->ccb_h.status = CAM_REQ_INVALID;
4814 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
4816 struct ccb_sim_knob *kp = &ccb->knob;
4820 ccb->ccb_h.status = CAM_REQ_INVALID;
4825 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4826 fcp = FCPARAM(isp, bus);
4828 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4829 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4830 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4831 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4833 ccb->ccb_h.status = CAM_REQ_CMP;
4834 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4838 switch (kp->xport_specific.fc.role) {
4839 case KNOB_ROLE_NONE:
4840 if (fcp->role != ISP_ROLE_NONE) {
4842 newrole = ISP_ROLE_NONE;
4845 case KNOB_ROLE_TARGET:
4846 if (fcp->role != ISP_ROLE_TARGET) {
4848 newrole = ISP_ROLE_TARGET;
4851 case KNOB_ROLE_INITIATOR:
4852 if (fcp->role != ISP_ROLE_INITIATOR) {
4854 newrole = ISP_ROLE_INITIATOR;
4857 case KNOB_ROLE_BOTH:
4859 if (fcp->role != ISP_ROLE_BOTH) {
4861 newrole = ISP_ROLE_BOTH;
4865 * We don't really support dual role at present on FC cards.
4867 * We should, but a bunch of things are currently broken,
4868 * so don't allow it.
4870 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4871 ccb->ccb_h.status = CAM_REQ_INVALID;
4876 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
4877 if (isp_fc_change_role(isp, bus, newrole) != 0) {
4878 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4879 #ifdef ISP_TARGET_MODE
4880 } else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4881 ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
4889 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
4891 struct ccb_sim_knob *kp = &ccb->knob;
4896 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4897 fcp = FCPARAM(isp, bus);
4899 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4900 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4901 switch (fcp->role) {
4903 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4905 case ISP_ROLE_TARGET:
4906 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4908 case ISP_ROLE_INITIATOR:
4909 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4912 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4915 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4916 ccb->ccb_h.status = CAM_REQ_CMP;
4918 ccb->ccb_h.status = CAM_REQ_INVALID;
4923 case XPT_PATH_INQ: /* Path routing inquiry */
4925 struct ccb_pathinq *cpi = &ccb->cpi;
4927 cpi->version_num = 1;
4928 #ifdef ISP_TARGET_MODE
4929 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4931 cpi->target_sprt = 0;
4933 cpi->hba_eng_cnt = 0;
4934 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4935 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4936 cpi->bus_id = cam_sim_bus(sim);
4937 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4939 fcparam *fcp = FCPARAM(isp, bus);
4941 cpi->hba_misc = PIM_NOBUSRESET;
4944 * Because our loop ID can shift from time to time,
4945 * make our initiator ID out of range of our bus.
4947 cpi->initiator_id = cpi->max_target + 1;
4950 * Set base transfer capabilities for Fibre Channel, for this HBA.
4953 cpi->base_transfer_speed = 8000000;
4954 } else if (IS_24XX(isp)) {
4955 cpi->base_transfer_speed = 4000000;
4956 } else if (IS_23XX(isp)) {
4957 cpi->base_transfer_speed = 2000000;
4959 cpi->base_transfer_speed = 1000000;
4961 cpi->hba_inquiry = PI_TAG_ABLE;
4962 cpi->transport = XPORT_FC;
4963 cpi->transport_version = 0;
4964 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4965 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4966 cpi->xport_specific.fc.port = fcp->isp_portid;
4967 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4969 sdparam *sdp = SDPARAM(isp, bus);
4970 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4972 cpi->initiator_id = sdp->isp_initiator_id;
4973 cpi->base_transfer_speed = 3300;
4974 cpi->transport = XPORT_SPI;
4975 cpi->transport_version = 2;
4977 cpi->protocol = PROTO_SCSI;
4978 cpi->protocol_version = SCSI_REV_2;
4979 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4980 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4981 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4982 cpi->unit_number = cam_sim_unit(sim);
4983 cpi->ccb_h.status = CAM_REQ_CMP;
4988 ccb->ccb_h.status = CAM_REQ_INVALID;
4994 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4997 isp_done(XS_T *sccb)
4999 ispsoftc_t *isp = XS_ISP(sccb);
5003 XS_SETERR(sccb, CAM_REQ_CMP);
5005 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
5006 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
5007 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
5008 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
5010 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
5014 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5015 status = sccb->ccb_h.status & CAM_STATUS_MASK;
5016 if (status != CAM_REQ_CMP) {
5017 if (status != CAM_SEL_TIMEOUT)
5018 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);
5019 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
5020 sccb->ccb_h.status |= CAM_DEV_QFRZN;
5021 xpt_freeze_devq(sccb->ccb_h.path, 1);
5025 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5026 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
5029 XS_CMD_S_DONE(sccb);
5030 if (XS_TACTIVE_P(sccb))
5031 callout_stop(&PISP_PCMD(sccb)->wdog);
5032 XS_CMD_S_CLEAR(sccb);
5033 isp_free_pcmd(isp, (union ccb *) sccb);
5034 xpt_done((union ccb *) sccb);
5038 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
5041 static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
5042 static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
5047 struct cam_path *tmppath;
5051 case ISPASYNC_NEW_TGT_PARAMS:
5053 struct ccb_trans_settings_scsi *scsi;
5054 struct ccb_trans_settings_spi *spi;
5057 struct ccb_trans_settings cts;
5059 memset(&cts, 0, sizeof (struct ccb_trans_settings));
5062 bus = va_arg(ap, int);
5063 tgt = va_arg(ap, int);
5065 sdp = SDPARAM(isp, bus);
5067 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5068 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5071 flags = sdp->isp_devparam[tgt].actv_flags;
5072 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5073 cts.protocol = PROTO_SCSI;
5074 cts.transport = XPORT_SPI;
5076 scsi = &cts.proto_specific.scsi;
5077 spi = &cts.xport_specific.spi;
5079 if (flags & DPARM_TQING) {
5080 scsi->valid |= CTS_SCSI_VALID_TQ;
5081 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5084 if (flags & DPARM_DISC) {
5085 spi->valid |= CTS_SPI_VALID_DISC;
5086 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5088 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5089 if (flags & DPARM_WIDE) {
5090 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5092 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5094 if (flags & DPARM_SYNC) {
5095 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5096 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5097 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5098 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5100 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);
5101 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5102 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5103 xpt_free_path(tmppath);
5106 case ISPASYNC_BUS_RESET:
5109 bus = va_arg(ap, int);
5111 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5113 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5115 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5121 msg = "LIP Received";
5124 case ISPASYNC_LOOP_RESET:
5129 case ISPASYNC_LOOP_DOWN:
5135 bus = va_arg(ap, int);
5138 FCPARAM(isp, bus)->link_active = 0;
5140 fc = ISP_FC_PC(isp, bus);
5141 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5143 * We don't do any simq freezing if we are only in target mode
5145 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5147 isp_freeze_loopdown(isp, bus, msg);
5149 if (!callout_active(&fc->ldt)) {
5150 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5151 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5155 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5158 case ISPASYNC_LOOP_UP:
5160 bus = va_arg(ap, int);
5162 fc = ISP_FC_PC(isp, bus);
5164 * Now we just note that Loop has come up. We don't
5165 * actually do anything because we're waiting for a
5166 * Change Notify before activating the FC cleanup
5167 * thread to look at the state of the loop again.
5169 FCPARAM(isp, bus)->link_active = 1;
5171 fc->loop_down_time = 0;
5172 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5174 case ISPASYNC_DEV_ARRIVED:
5176 bus = va_arg(ap, int);
5177 lp = va_arg(ap, fcportdb_t *);
5179 fc = ISP_FC_PC(isp, bus);
5182 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5183 int dbidx = lp - FCPARAM(isp, bus)->portdb;
5186 for (i = 0; i < MAX_FC_TARG; i++) {
5187 if (i >= FL_ID && i <= SNS_ID) {
5190 if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5194 if (i < MAX_FC_TARG) {
5195 FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5196 lp->dev_map_idx = i + 1;
5198 isp_prt(isp, ISP_LOGWARN, "out of target ids");
5199 isp_dump_portdb(isp, bus);
5202 if (lp->dev_map_idx) {
5203 tgt = lp->dev_map_idx - 1;
5204 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);
5205 isp_make_here(isp, bus, tgt);
5207 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);
5210 case ISPASYNC_DEV_CHANGED:
5212 bus = va_arg(ap, int);
5213 lp = va_arg(ap, fcportdb_t *);
5215 fc = ISP_FC_PC(isp, bus);
5218 if (isp_change_is_bad) {
5219 lp->state = FC_PORTDB_STATE_NIL;
5220 if (lp->dev_map_idx) {
5221 tgt = lp->dev_map_idx - 1;
5222 FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5223 lp->dev_map_idx = 0;
5224 isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5225 isp_make_gone(isp, bus, tgt);
5227 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5228 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5231 lp->portid = lp->new_portid;
5232 lp->roles = lp->new_roles;
5233 if (lp->dev_map_idx) {
5234 int t = lp->dev_map_idx - 1;
5235 FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5236 tgt = lp->dev_map_idx - 1;
5237 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5238 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5240 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);
5244 case ISPASYNC_DEV_STAYED:
5246 bus = va_arg(ap, int);
5247 lp = va_arg(ap, fcportdb_t *);
5249 if (lp->dev_map_idx) {
5250 tgt = lp->dev_map_idx - 1;
5251 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5252 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5254 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5255 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5258 case ISPASYNC_DEV_GONE:
5260 bus = va_arg(ap, int);
5261 lp = va_arg(ap, fcportdb_t *);
5263 fc = ISP_FC_PC(isp, bus);
5265 * If this has a virtual target and we haven't marked it
5266 * that we're going to have isp_gdt tell the OS it's gone,
5267 * set the isp_gdt timer running on it.
5269 * If it isn't marked that isp_gdt is going to get rid of it,
5270 * announce that it's gone.
5273 if (lp->dev_map_idx && lp->reserved == 0) {
5275 lp->state = FC_PORTDB_STATE_ZOMBIE;
5276 lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5277 if (fc->ready && !callout_active(&fc->gdt)) {
5278 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_uptime);
5279 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5281 tgt = lp->dev_map_idx - 1;
5282 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);
5283 } else if (lp->reserved == 0) {
5284 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);
5287 case ISPASYNC_CHANGE_NOTIFY:
5290 int evt, nphdl, nlstate, reason;
5293 bus = va_arg(ap, int);
5294 evt = va_arg(ap, int);
5295 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5296 nphdl = va_arg(ap, int);
5297 nlstate = va_arg(ap, int);
5298 reason = va_arg(ap, int);
5301 nlstate = reason = 0;
5304 fc = ISP_FC_PC(isp, bus);
5306 if (evt == ISPASYNC_CHANGE_PDB) {
5307 msg = "Chan %d Port Database Changed";
5308 } else if (evt == ISPASYNC_CHANGE_SNS) {
5309 msg = "Chan %d Name Server Database Changed";
5311 msg = "Chan %d Other Change Notify";
5315 * If the loop down timer is running, cancel it.
5317 if (fc->ready && callout_active(&fc->ldt)) {
5318 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5319 callout_stop(&fc->ldt);
5321 isp_prt(isp, ISP_LOGINFO, msg, bus);
5322 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5323 isp_freeze_loopdown(isp, bus, msg);
5328 #ifdef ISP_TARGET_MODE
5329 case ISPASYNC_TARGET_NOTIFY:
5331 isp_notify_t *notify;
5333 notify = va_arg(ap, isp_notify_t *);
5335 switch (notify->nt_ncode) {
5337 case NT_ABORT_TASK_SET:
5339 case NT_CLEAR_TASK_SET:
5341 case NT_TARGET_RESET:
5343 * These are task management functions.
5345 isp_handle_platform_target_tmf(isp, notify);
5352 * No action need be taken here.
5356 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5360 * This is device arrival/departure notification
5362 isp_handle_platform_target_notify_ack(isp, notify);
5366 struct ac_contract ac;
5367 struct ac_device_changed *fc;
5369 ac.contract_number = AC_CONTRACT_DEV_CHG;
5370 fc = (struct ac_device_changed *) ac.contract_data;
5371 fc->wwpn = notify->nt_wwn;
5372 fc->port = notify->nt_sid;
5373 fc->target = notify->nt_nphdl;
5375 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5380 struct ac_contract ac;
5381 struct ac_device_changed *fc;
5383 ac.contract_number = AC_CONTRACT_DEV_CHG;
5384 fc = (struct ac_device_changed *) ac.contract_data;
5385 fc->wwpn = notify->nt_wwn;
5386 fc->port = notify->nt_sid;
5387 fc->target = notify->nt_nphdl;
5389 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5393 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5394 isp_handle_platform_target_notify_ack(isp, notify);
5399 case ISPASYNC_TARGET_ACTION:
5404 hp = va_arg(ap, isphdr_t *);
5406 switch (hp->rqs_entry_type) {
5408 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5410 case RQSTYPE_NOTIFY:
5412 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5413 } else if (IS_24XX(isp)) {
5414 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5416 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5421 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5423 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5427 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5433 isp_handle_platform_ctio(isp, hp);
5435 case RQSTYPE_ABTS_RCVD:
5437 abts_t *abts = (abts_t *)hp;
5438 isp_notify_t notify, *nt = ¬ify;
5444 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5445 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5446 ISP_MEMZERO(nt, sizeof (isp_notify_t));
5450 nt->nt_nphdl = abts->abts_nphdl;
5452 isp_find_chan_by_did(isp, did, &chan);
5453 if (chan == ISP_NOCHAN) {
5454 nt->nt_tgt = TGT_ANY;
5456 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5457 if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5458 nt->nt_wwn = lp->port_wwn;
5460 nt->nt_wwn = INI_ANY;
5464 * Try hard to find the lun for this command.
5466 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5468 nt->nt_lun = xpt_path_lun_id(tptr->owner);
5469 rls_lun_statep(isp, tptr);
5471 nt->nt_lun = LUN_ANY;
5473 nt->nt_need_ack = 1;
5474 nt->nt_tagval = abts->abts_rxid_task;
5475 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5476 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5477 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)",
5478 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5480 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)",
5481 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5483 nt->nt_channel = chan;
5484 nt->nt_ncode = NT_ABORT_TASK;
5485 nt->nt_lreserved = hp;
5486 isp_handle_platform_target_tmf(isp, nt);
5489 case RQSTYPE_ENABLE_LUN:
5490 case RQSTYPE_MODIFY_LUN:
5491 isp_ledone(isp, (lun_entry_t *) hp);
5497 case ISPASYNC_FW_CRASH:
5499 uint16_t mbox1, mbox6;
5500 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5501 if (IS_DUALBUS(isp)) {
5502 mbox6 = ISP_READ(isp, OUTMAILBOX6);
5506 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5507 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5508 isp->isp_osinfo.mbox_sleep_ok = 0;
5510 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5511 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5515 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5522 * Locks are held before coming here.
5525 isp_uninit(ispsoftc_t *isp)
5528 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5530 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5532 ISP_DISABLE_INTS(isp);
5536 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5537 * up from our platform default (defww{p|n}n) and morph them based upon
5540 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5541 * based upon channel.
5545 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5548 struct isp_fc *fc = ISP_FC_PC(isp, chan);
5551 * If we're asking for a active WWN, the default overrides get
5552 * returned, otherwise the NVRAM value is picked.
5554 * If we're asking for a default WWN, we just pick the default override.
5557 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5561 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5565 return (0x400000007F000009ull);
5567 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5572 * For channel zero just return what we have. For either ACTIVE or
5573 * DEFAULT cases, we depend on default override of NVRAM values for
5581 * For other channels, we are doing one of three things:
5583 * 1. If what we have now is non-zero, return it. Otherwise we morph
5584 * values from channel 0. 2. If we're here for a WWPN we synthesize
5585 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5586 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5593 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5595 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5598 if (((seed >> 60) & 0xf) == 2) {
5600 * The type 2 NAA fields for QLogic cards appear be laid out
5603 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5604 * port (1) or node (0) WWN distinguishor bit 48
5605 * physical port on dual-port chips (23XX/24XX)
5607 * This is somewhat nutty, particularly since bit 48 is
5608 * irrelevant as they assign separate serial numbers to
5609 * different physical ports anyway.
5611 * We'll stick our channel number plus one first into bits
5612 * 57..59 and thence into bits 52..55 which allows for 8 bits
5613 * of channel which is comfortably more than our maximum
5616 seed &= ~0x0FF0000000000000ULL;
5618 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5619 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5628 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5634 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5637 sprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5640 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
5642 printf("%s\n", lbuf);
5646 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5649 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5652 xpt_print_path(xs->ccb_h.path);
5660 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5663 struct timespec x = *b;
5665 elapsed = GET_NANOSEC(&x);
5672 isp_mbox_acquire(ispsoftc_t *isp)
5674 if (isp->isp_osinfo.mboxbsy) {
5677 isp->isp_osinfo.mboxcmd_done = 0;
5678 isp->isp_osinfo.mboxbsy = 1;
5684 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5686 unsigned int usecs = mbp->timeout;
5687 unsigned int max, olim, ilim;
5690 usecs = MBCMD_DEFAULT_TIMEOUT;
5692 max = isp->isp_mbxwrk0 + 1;
5694 if (isp->isp_osinfo.mbox_sleep_ok) {
5695 unsigned int ms = (usecs + 999) / 1000;
5697 isp->isp_osinfo.mbox_sleep_ok = 0;
5698 isp->isp_osinfo.mbox_sleeping = 1;
5699 for (olim = 0; olim < max; olim++) {
5700 msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5701 if (isp->isp_osinfo.mboxcmd_done) {
5705 isp->isp_osinfo.mbox_sleep_ok = 1;
5706 isp->isp_osinfo.mbox_sleeping = 0;
5708 for (olim = 0; olim < max; olim++) {
5709 for (ilim = 0; ilim < usecs; ilim += 100) {
5711 uint16_t sema, mbox;
5712 if (isp->isp_osinfo.mboxcmd_done) {
5715 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5716 isp_intr(isp, isr, sema, mbox);
5717 if (isp->isp_osinfo.mboxcmd_done) {
5723 if (isp->isp_osinfo.mboxcmd_done) {
5728 if (isp->isp_osinfo.mboxcmd_done == 0) {
5729 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5730 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5731 mbp->param[0] = MBOX_TIMEOUT;
5732 isp->isp_osinfo.mboxcmd_done = 1;
5737 isp_mbox_notify_done(ispsoftc_t *isp)
5739 if (isp->isp_osinfo.mbox_sleeping) {
5740 wakeup(&isp->isp_mbxworkp);
5742 isp->isp_osinfo.mboxcmd_done = 1;
5746 isp_mbox_release(ispsoftc_t *isp)
5748 isp->isp_osinfo.mboxbsy = 0;
5752 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5755 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5758 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5768 t.tv_sec = ms / 1000;
5769 t.tv_usec = (ms % 1000) * 1000;
5781 isp_platform_intr(void *arg)
5783 ispsoftc_t *isp = arg;
5785 uint16_t sema, mbox;
5789 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5790 isp->isp_intbogus++;
5792 isp_intr(isp, isr, sema, mbox);
5798 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5800 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5801 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5803 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5805 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5809 isp_timer(void *arg)
5811 ispsoftc_t *isp = arg;
5812 #ifdef ISP_TARGET_MODE
5813 isp_tmcmd_restart(isp);
5815 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);