2 * Copyright (c) 1997-2009 by Matthew Jacob
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/isp/isp_freebsd.c,v 1.176 2011/11/06 00:44:40 mjacob Exp $
30 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
32 #include <dev/disk/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
36 #include <sys/module.h>
37 #include <dev/disk/isp/isp_ioctl.h>
38 #include <bus/cam/cam_periph.h>
39 #include <bus/cam/cam_xpt_periph.h>
40 #include <sys/device.h>
42 #define THREAD_CREATE kthread_create
44 MODULE_VERSION(isp, 1);
45 MODULE_DEPEND(isp, cam, 1, 1, 1);
46 int isp_announced = 0;
47 int isp_fabric_hysteresis = 5;
48 int isp_loop_down_limit = 60; /* default loop down limit */
49 int isp_change_is_bad = 0; /* "changed" devices are bad */
50 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
51 int isp_gone_device_time = 30; /* grace time before reporting device lost */
52 int isp_autoconfig = 1; /* automatically attach/detach devices */
53 static const char *roles[4] = {
54 "(none)", "Target", "Initiator", "Target/Initiator"
56 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
57 static const char rqo[] = "%s: Request Queue Overflow\n";
59 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
60 static d_ioctl_t ispioctl;
61 static void isp_intr_enable(void *);
62 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
63 static void isp_poll(struct cam_sim *);
64 static timeout_t isp_watchdog;
65 static timeout_t isp_gdt;
66 static task_fn_t isp_gdt_task;
67 static timeout_t isp_ldt;
68 static task_fn_t isp_ldt_task;
69 static void isp_kthread(void *);
70 static void isp_action(struct cam_sim *, union ccb *);
71 #ifdef ISP_INTERNAL_TARGET
72 static void isp_target_thread_pi(void *);
73 static void isp_target_thread_fc(void *);
75 static void isp_timer(void *);
77 static struct dev_ops isp_ops = {
78 { "isp", 0, D_MPSAFE },
83 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
85 struct ccb_setasync csa;
87 struct cam_path *path;
90 * Construct our SIM entry.
92 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);
93 cam_simq_release(devq);
100 if (xpt_bus_register(sim, chan) != CAM_SUCCESS) {
106 if (xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
108 xpt_bus_deregister(cam_sim_path(sim));
113 xpt_setup_ccb(&csa.ccb_h, path, 5);
114 csa.ccb_h.func_code = XPT_SASYNC_CB;
115 csa.event_enable = AC_LOST_DEVICE;
116 csa.callback = isp_cam_async;
117 csa.callback_arg = sim;
118 xpt_action((union ccb *)&csa);
121 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
124 #ifdef ISP_INTERNAL_TARGET
125 ISP_SET_PC(isp, chan, proc_active, 1);
126 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
127 ISP_SET_PC(isp, chan, proc_active, 0);
128 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
132 fcparam *fcp = FCPARAM(isp, chan);
133 struct isp_fc *fc = ISP_FC_PC(isp, chan);
141 callout_init(&fc->ldt);
142 callout_init(&fc->gdt);
143 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
144 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
147 * We start by being "loop down" if we have an initiator role
149 if (fcp->role & ISP_ROLE_INITIATOR) {
150 isp_freeze_loopdown(isp, chan, "isp_attach");
151 callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
152 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_second);
155 if (THREAD_CREATE(isp_kthread, fc, &fc->kthread, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
156 xpt_free_path(fc->path);
158 if (callout_active(&fc->ldt)) {
159 callout_stop(&fc->ldt);
161 xpt_bus_deregister(cam_sim_path(fc->sim));
163 cam_sim_free(fc->sim);
166 #ifdef ISP_INTERNAL_TARGET
167 ISP_SET_PC(isp, chan, proc_active, 1);
168 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
169 ISP_SET_PC(isp, chan, proc_active, 0);
170 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
174 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
175 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
176 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwnn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwnn, 0, "World Wide Node Name");
177 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, 0, "World Wide Port Name");
178 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");
179 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");
186 isp_attach(ispsoftc_t *isp)
188 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
189 int du = device_get_unit(isp->isp_dev);
192 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
193 isp->isp_osinfo.ehook.ich_arg = isp;
194 isp->isp_osinfo.ehook.ich_desc = "isp";
196 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
197 * will be called right awawy, which will clear isp_osinfo.ehook_active,
198 * which would be unwise to then set again later.
200 isp->isp_osinfo.ehook_active = 1;
201 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
202 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
207 * Create the device queue for our SIM(s).
209 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
210 if (isp->isp_osinfo.devq == NULL) {
211 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
215 for (chan = 0; chan < isp->isp_nchan; chan++) {
216 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
221 callout_init(&isp->isp_osinfo.tmo);
222 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
223 isp->isp_osinfo.timer_active = 1;
225 isp->isp_osinfo.cdev = make_dev(&isp_ops, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
226 if (isp->isp_osinfo.cdev) {
227 isp->isp_osinfo.cdev->si_drv1 = isp;
232 while (--chan >= 0) {
234 struct cam_path *path;
236 sim = ISP_FC_PC(isp, chan)->sim;
237 path = ISP_FC_PC(isp, chan)->path;
239 sim = ISP_SPI_PC(isp, chan)->sim;
240 path = ISP_SPI_PC(isp, chan)->path;
244 xpt_bus_deregister(cam_sim_path(sim));
248 if (isp->isp_osinfo.ehook_active) {
249 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
250 isp->isp_osinfo.ehook_active = 0;
252 if (isp->isp_osinfo.cdev) {
253 destroy_dev(isp->isp_osinfo.cdev);
254 isp->isp_osinfo.cdev = NULL;
256 isp->isp_osinfo.devq = NULL;
261 isp_detach(ispsoftc_t *isp)
264 struct cam_path *path;
265 struct ccb_setasync csa;
269 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
271 sim = ISP_FC_PC(isp, chan)->sim;
272 path = ISP_FC_PC(isp, chan)->path;
274 sim = ISP_SPI_PC(isp, chan)->sim;
275 path = ISP_SPI_PC(isp, chan)->path;
277 if (sim->refcount > 2) {
282 if (isp->isp_osinfo.timer_active) {
283 callout_stop(&isp->isp_osinfo.tmo);
284 isp->isp_osinfo.timer_active = 0;
286 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
288 sim = ISP_FC_PC(isp, chan)->sim;
289 path = ISP_FC_PC(isp, chan)->path;
291 sim = ISP_SPI_PC(isp, chan)->sim;
292 path = ISP_SPI_PC(isp, chan)->path;
294 xpt_setup_ccb(&csa.ccb_h, path, 5);
295 csa.ccb_h.func_code = XPT_SASYNC_CB;
296 csa.event_enable = 0;
297 csa.callback = isp_cam_async;
298 csa.callback_arg = sim;
299 xpt_action((union ccb *)&csa);
301 xpt_bus_deregister(cam_sim_path(sim));
305 if (isp->isp_osinfo.cdev) {
306 destroy_dev(isp->isp_osinfo.cdev);
307 isp->isp_osinfo.cdev = NULL;
309 if (isp->isp_osinfo.ehook_active) {
310 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
311 isp->isp_osinfo.ehook_active = 0;
313 if (isp->isp_osinfo.devq != NULL) {
314 isp->isp_osinfo.devq = NULL;
320 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
323 struct isp_fc *fc = ISP_FC_PC(isp, chan);
324 if (fc->simqfrozen == 0) {
325 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
326 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
327 xpt_freeze_simq(fc->sim, 1);
329 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
330 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
336 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
339 struct isp_fc *fc = ISP_FC_PC(isp, chan);
340 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
341 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
342 if (wasfrozen && fc->simqfrozen == 0) {
343 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
344 xpt_release_simq(fc->sim, 1);
351 ispioctl(struct dev_ioctl_args *ap)
353 cdev_t dev = ap->a_head.a_dev;
354 caddr_t addr = ap->a_data;
355 u_long c = ap->a_cmd;
357 int nr, chan, retval = ENOTTY;
364 int olddblev = isp->isp_dblev;
365 isp->isp_dblev = *(int *)addr;
366 *(int *)addr = olddblev;
372 if (chan < 0 || chan >= isp->isp_nchan) {
377 *(int *)addr = FCPARAM(isp, chan)->role;
379 *(int *)addr = SDPARAM(isp, chan)->role;
386 if (chan < 0 || chan >= isp->isp_nchan) {
391 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
397 * We don't really support dual role at present on FC cards.
399 * We should, but a bunch of things are currently broken,
402 if (nr == ISP_ROLE_BOTH) {
403 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
407 *(int *)addr = FCPARAM(isp, chan)->role;
408 #ifdef ISP_INTERNAL_TARGET
410 retval = isp_fc_change_role(isp, chan, nr);
413 FCPARAM(isp, chan)->role = nr;
416 *(int *)addr = SDPARAM(isp, chan)->role;
417 SDPARAM(isp, chan)->role = nr;
424 #ifdef ISP_TARGET_MODE
425 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
435 if (chan < 0 || chan >= isp->isp_nchan) {
440 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
452 if (chan < 0 || chan >= isp->isp_nchan) {
457 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
465 case ISP_FC_GETDINFO:
467 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
473 if (ifc->loopid >= MAX_FC_TARG) {
477 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
478 if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
479 ifc->role = lp->roles;
480 ifc->loopid = lp->handle;
481 ifc->portid = lp->portid;
482 ifc->node_wwn = lp->node_wwn;
483 ifc->port_wwn = lp->port_wwn;
492 isp_stats_t *sp = (isp_stats_t *) addr;
494 ISP_MEMZERO(sp, sizeof (*sp));
495 sp->isp_stat_version = ISP_STATS_VERSION;
496 sp->isp_type = isp->isp_type;
497 sp->isp_revision = isp->isp_revision;
499 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
500 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
501 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
502 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
503 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
504 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
505 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
506 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
514 isp->isp_intbogus = 0;
515 isp->isp_intmboxc = 0;
516 isp->isp_intoasync = 0;
517 isp->isp_rsltccmplt = 0;
518 isp->isp_fphccmplt = 0;
519 isp->isp_rscchiwater = 0;
520 isp->isp_fpcchiwater = 0;
524 case ISP_FC_GETHINFO:
526 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
527 int chan = hba->fc_channel;
529 if (chan < 0 || chan >= isp->isp_nchan) {
533 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
534 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
535 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
536 hba->fc_nchannels = isp->isp_nchan;
538 hba->fc_nports = MAX_FC_TARG;
539 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
540 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
541 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
542 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
543 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
544 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
545 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
547 hba->fc_nports = MAX_TARGETS;
549 hba->fc_topology = 0;
550 hba->nvram_node_wwn = 0ull;
551 hba->nvram_port_wwn = 0ull;
552 hba->active_node_wwn = 0ull;
553 hba->active_port_wwn = 0ull;
561 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
570 if (chan < 0 || chan >= isp->isp_nchan) {
575 needmarker = retval = 0;
576 loopid = fct->loopid;
579 uint8_t local[QENTRY_LEN];
581 isp24xx_statusreq_t *sp;
582 fcparam *fcp = FCPARAM(isp, chan);
586 for (i = 0; i < MAX_FC_TARG; i++) {
587 lp = &fcp->portdb[i];
588 if (lp->handle == loopid) {
592 if (i == MAX_FC_TARG) {
597 /* XXX VALIDATE LP XXX */
598 tmf = (isp24xx_tmf_t *) local;
599 ISP_MEMZERO(tmf, QENTRY_LEN);
600 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
601 tmf->tmf_header.rqs_entry_count = 1;
602 tmf->tmf_nphdl = lp->handle;
604 tmf->tmf_timeout = 2;
605 tmf->tmf_tidlo = lp->portid;
606 tmf->tmf_tidhi = lp->portid >> 16;
607 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
608 tmf->tmf_lun[1] = fct->lun & 0xff;
609 if (fct->lun >= 256) {
610 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
612 switch (fct->action) {
614 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
616 case IPT_TARGET_RESET:
617 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
621 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
624 case IPT_CLEAR_TASK_SET:
625 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
628 case IPT_ABORT_TASK_SET:
629 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
640 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
641 mbs.param[1] = QENTRY_LEN;
642 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
643 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
644 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
645 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
647 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
652 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
653 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
654 sp = (isp24xx_statusreq_t *) local;
655 sp->req_completion_status = 1;
656 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
657 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
658 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
659 FC_SCRATCH_RELEASE(isp, chan);
660 if (retval || sp->req_completion_status != 0) {
661 FC_SCRATCH_RELEASE(isp, chan);
670 MBSINIT(&mbs, 0, MBLOGALL, 0);
671 if (ISP_CAP_2KLOGIN(isp) == 0) {
674 switch (fct->action) {
676 mbs.param[0] = MBOX_CLEAR_ACA;
677 mbs.param[1] = loopid;
678 mbs.param[2] = fct->lun;
680 case IPT_TARGET_RESET:
681 mbs.param[0] = MBOX_TARGET_RESET;
682 mbs.param[1] = loopid;
686 mbs.param[0] = MBOX_LUN_RESET;
687 mbs.param[1] = loopid;
688 mbs.param[2] = fct->lun;
691 case IPT_CLEAR_TASK_SET:
692 mbs.param[0] = MBOX_CLEAR_TASK_SET;
693 mbs.param[1] = loopid;
694 mbs.param[2] = fct->lun;
697 case IPT_ABORT_TASK_SET:
698 mbs.param[0] = MBOX_ABORT_TASK_SET;
699 mbs.param[1] = loopid;
700 mbs.param[2] = fct->lun;
709 FCPARAM(isp, chan)->sendmarker = 1;
711 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
727 isp_intr_enable(void *arg)
730 ispsoftc_t *isp = arg;
732 for (chan = 0; chan < isp->isp_nchan; chan++) {
734 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
735 ISP_ENABLE_INTS(isp);
739 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
740 ISP_ENABLE_INTS(isp);
745 isp->isp_osinfo.ehook_active = 0;
747 /* Release our hook so that the boot can continue. */
748 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
755 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
756 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
758 static ISP_INLINE int
759 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
761 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
762 if (ISP_PCMD(ccb) == NULL) {
765 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
769 static ISP_INLINE void
770 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
772 ((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
773 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
774 ISP_PCMD(ccb) = NULL;
777 * Put the target mode functions here, because some are inlines
780 #ifdef ISP_TARGET_MODE
781 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
782 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
783 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
784 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
785 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
786 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
787 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
788 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
789 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
790 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
791 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
792 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
793 static void isp_enable_lun(ispsoftc_t *, union ccb *);
794 static void isp_enable_deferred_luns(ispsoftc_t *, int);
795 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
796 static void isp_disable_lun(ispsoftc_t *, union ccb *);
797 static int isp_enable_target_mode(ispsoftc_t *, int);
798 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
799 static timeout_t isp_refire_putback_atio;
800 static void isp_complete_ctio(union ccb *);
801 static void isp_target_putback_atio(union ccb *);
802 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
803 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
804 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
805 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
806 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
807 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
808 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
809 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
810 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
811 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
812 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
813 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
815 static ISP_INLINE int
816 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
821 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
822 SLIST_FOREACH(tptr, lhp, next) {
823 if (xpt_path_lun_id(tptr->owner) == lun) {
831 dump_tstates(ispsoftc_t *isp, int bus)
835 tstate_t *tptr = NULL;
837 if (bus >= isp->isp_nchan) {
840 for (i = 0; i < LUN_HASH_SIZE; i++) {
841 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
843 SLIST_FOREACH(tptr, lhp, next) {
844 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
850 static ISP_INLINE tstate_t *
851 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
853 tstate_t *tptr = NULL;
857 if (bus < isp->isp_nchan) {
858 for (i = 0; i < LUN_HASH_SIZE; i++) {
859 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
860 SLIST_FOREACH(tptr, lhp, next) {
861 if (xpt_path_lun_id(tptr->owner) == lun) {
871 static ISP_INLINE tstate_t *
872 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
874 tstate_t *tptr = NULL;
875 atio_private_data_t *atp;
879 if (bus < isp->isp_nchan && tagval != 0) {
880 for (i = 0; i < LUN_HASH_SIZE; i++) {
881 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
882 SLIST_FOREACH(tptr, lhp, next) {
883 atp = isp_get_atpd(isp, tptr, tagval);
884 if (atp && atp->tag == tagval) {
894 static ISP_INLINE inot_private_data_t *
895 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
897 inot_private_data_t *ntp;
902 for (bus = 0; bus < isp->isp_nchan; bus++) {
903 for (i = 0; i < LUN_HASH_SIZE; i++) {
904 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
905 SLIST_FOREACH(tptr, lhp, next) {
906 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
917 static ISP_INLINE void
918 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
920 KASSERT((tptr->hold), ("tptr not held"));
925 isp_tmcmd_restart(ispsoftc_t *isp)
927 inot_private_data_t *ntp;
932 for (bus = 0; bus < isp->isp_nchan; bus++) {
933 for (i = 0; i < LUN_HASH_SIZE; i++) {
934 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
935 SLIST_FOREACH(tptr, lhp, next) {
936 inot_private_data_t *restart_queue = tptr->restart_queue;
937 tptr->restart_queue = NULL;
938 while (restart_queue) {
940 restart_queue = ntp->rd.nt.nt_hba;
942 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
943 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
945 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
946 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
948 isp_put_ntpd(isp, tptr, ntp);
949 if (tptr->restart_queue && restart_queue != NULL) {
950 ntp = tptr->restart_queue;
951 tptr->restart_queue = restart_queue;
952 while (restart_queue->rd.nt.nt_hba) {
953 restart_queue = restart_queue->rd.nt.nt_hba;
955 restart_queue->rd.nt.nt_hba = ntp;
964 static ISP_INLINE atio_private_data_t *
965 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
967 atio_private_data_t *atp;
972 tptr->atfree = atp->next;
976 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
977 if (atp->tag == tag) {
984 static ISP_INLINE void
985 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
989 atp->next = tptr->atfree;
994 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
996 atio_private_data_t *atp;
997 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
999 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1000 if (atp->tag == 0) {
1003 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",
1004 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]);
1009 static ISP_INLINE inot_private_data_t *
1010 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1012 inot_private_data_t *ntp;
1015 tptr->ntfree = ntp->next;
1020 static ISP_INLINE inot_private_data_t *
1021 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1023 inot_private_data_t *ntp;
1024 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1025 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1032 static ISP_INLINE void
1033 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1035 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1036 ntp->next = tptr->ntfree;
1041 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1049 lun = xpt_path_lun_id(path);
1050 if (lun != CAM_LUN_WILDCARD) {
1051 if (lun >= ISP_MAX_LUNS(isp)) {
1052 return (CAM_LUN_INVALID);
1055 if (is_lun_enabled(isp, bus, lun)) {
1056 return (CAM_LUN_ALRDY_ENA);
1058 tptr = kmalloc(sizeof (tstate_t), M_DEVBUF, M_WAITOK | M_ZERO);
1059 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1060 if (status != CAM_REQ_CMP) {
1061 kfree(tptr, M_DEVBUF);
1064 SLIST_INIT(&tptr->atios);
1065 SLIST_INIT(&tptr->inots);
1066 for (i = 0; i < ATPDPSIZE-1; i++) {
1067 tptr->atpool[i].next = &tptr->atpool[i+1];
1068 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1070 tptr->atfree = tptr->atpool;
1071 tptr->ntfree = tptr->ntpool;
1073 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1074 SLIST_INSERT_HEAD(lhp, tptr, next);
1076 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1077 return (CAM_REQ_CMP);
1080 static ISP_INLINE void
1081 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1084 KASSERT((tptr->hold == 0), ("tptr still held"));
1085 ISP_GET_PC_ADDR(isp, xpt_path_path_id(tptr->owner), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1086 SLIST_REMOVE(lhp, tptr, tstate, next);
1087 xpt_free_path(tptr->owner);
1088 kfree(tptr, M_DEVBUF);
1095 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1097 tstate_t *tptr = NULL;
1098 int bus, tm_enabled, target_role;
1103 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1105 bus = XS_CHANNEL(ccb);
1106 target = ccb->ccb_h.target_id;
1107 lun = ccb->ccb_h.target_lun;
1108 if (target != CAM_TARGET_WILDCARD && target != 0) {
1109 ccb->ccb_h.status = CAM_TID_INVALID;
1113 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1114 ccb->ccb_h.status = CAM_LUN_INVALID;
1119 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1120 ccb->ccb_h.status = CAM_LUN_INVALID;
1124 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1125 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1129 * Wait until we're not busy with the lun enables subsystem
1131 while (isp->isp_osinfo.tmbusy) {
1132 isp->isp_osinfo.tmwanted = 1;
1133 mtx_sleep(isp, &isp->isp_lock, 0, "want_isp_enable_lun", 0);
1135 isp->isp_osinfo.tmbusy = 1;
1138 * This is as a good a place as any to check f/w capabilities.
1142 if (ISP_CAP_TMODE(isp) == 0) {
1143 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1144 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1148 * We *could* handle non-SCCLUN f/w, but we'd have to
1149 * dork with our already fragile enable/disable code.
1151 if (ISP_CAP_SCCFW(isp) == 0) {
1152 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1153 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1157 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1160 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1164 * Create the state pointer.
1165 * It should not already exist.
1167 tptr = get_lun_statep(isp, bus, lun);
1169 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1172 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1173 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1178 * We have a tricky maneuver to perform here.
1180 * If target mode isn't already enabled here,
1181 * *and* our current role includes target mode,
1182 * we enable target mode here.
1185 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1186 if (tm_enabled == 0 && target_role != 0) {
1187 if (isp_enable_target_mode(isp, bus)) {
1188 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1189 destroy_lun_state(isp, tptr);
1197 * Now check to see whether this bus is in target mode already.
1199 * If not, a later role change into target mode will finish the job.
1201 if (tm_enabled == 0) {
1202 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1203 ccb->ccb_h.status = CAM_REQ_CMP;
1204 xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
1211 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1214 if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
1215 destroy_lun_state(isp, tptr);
1219 rls_lun_statep(isp, tptr);
1221 isp->isp_osinfo.tmbusy = 0;
1222 if (isp->isp_osinfo.tmwanted) {
1223 isp->isp_osinfo.tmwanted = 0;
1230 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1233 * XXX: not entirely implemented yet
1235 (void) isp_enable_deferred(isp, bus, 0);
1239 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1243 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1244 if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
1245 status = CAM_REQ_CMP;
1247 int cmd_cnt, not_cnt;
1250 cmd_cnt = DFLT_CMND_CNT;
1251 not_cnt = DFLT_INOT_CNT;
1256 status = CAM_REQ_INPROG;
1257 isp->isp_osinfo.rptr = &status;
1258 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1259 status = CAM_RESRC_UNAVAIL;
1261 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1263 isp->isp_osinfo.rptr = NULL;
1266 if (status == CAM_REQ_CMP) {
1267 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1268 isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1274 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1276 tstate_t *tptr = NULL;
1282 bus = XS_CHANNEL(ccb);
1283 target = ccb->ccb_h.target_id;
1284 lun = ccb->ccb_h.target_lun;
1285 if (target != CAM_TARGET_WILDCARD && target != 0) {
1286 ccb->ccb_h.status = CAM_TID_INVALID;
1290 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1291 ccb->ccb_h.status = CAM_LUN_INVALID;
1296 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1297 ccb->ccb_h.status = CAM_LUN_INVALID;
1301 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1302 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1306 * See if we're busy disabling a lun now.
1308 while (isp->isp_osinfo.tmbusy) {
1309 isp->isp_osinfo.tmwanted = 1;
1310 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
1312 isp->isp_osinfo.tmbusy = 1;
1315 * Find the state pointer.
1317 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1318 ccb->ccb_h.status = CAM_PATH_INVALID;
1323 * If we're a 24XX card, we're done.
1326 status = CAM_REQ_CMP;
1331 * For SCC FW, we only deal with lun zero.
1337 isp->isp_osinfo.rptr = &status;
1338 status = CAM_REQ_INPROG;
1339 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1340 status = CAM_RESRC_UNAVAIL;
1342 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1345 if (status == CAM_REQ_CMP) {
1346 xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
1349 destroy_lun_state(isp, tptr);
1351 isp->isp_osinfo.rptr = NULL;
1352 isp->isp_osinfo.tmbusy = 0;
1353 if (isp->isp_osinfo.tmwanted) {
1354 isp->isp_osinfo.tmwanted = 0;
1361 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1365 ISP_GET_PC(isp, bus, tm_enabled, ct);
1373 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1374 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1375 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1376 mbs.param[2] = bus << 7;
1377 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1378 isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
1381 SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
1383 ISP_SET_PC(isp, bus, tm_enabled, 1);
1384 isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
1390 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1394 ISP_GET_PC(isp, bus, tm_enabled, ct);
1402 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1403 mbs.param[2] = bus << 7;
1404 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1405 isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
1408 SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
1410 ISP_SET_PC(isp, bus, tm_enabled, 0);
1411 isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
1417 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1421 rptr = isp->isp_osinfo.rptr;
1422 if (lep->le_status != LUN_OK) {
1423 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1425 *rptr = CAM_REQ_CMP_ERR;
1430 *rptr = CAM_REQ_CMP;
1437 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1441 atio_private_data_t *atp;
1442 struct ccb_scsiio *cso = &ccb->csio;
1443 uint32_t dmaresult, handle;
1444 uint8_t local[QENTRY_LEN];
1447 * Do some sanity checks.
1449 if (cso->dxfer_len == 0) {
1450 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1451 xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1452 ccb->ccb_h.status = CAM_REQ_INVALID;
1458 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1460 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1462 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1463 dump_tstates(isp, XS_CHANNEL(ccb));
1464 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1470 atp = isp_get_atpd(isp, tptr, cso->tag_id);
1472 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1473 isp_dump_atpd(isp, tptr);
1474 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1479 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1480 ccb->ccb_h.status = CAM_REQ_ABORTED;
1486 * Check to make sure we're still in target mode.
1488 if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1489 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);
1490 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1496 * Get some resources
1498 if (isp_get_pcmd(isp, ccb)) {
1499 rls_lun_statep(isp, tptr);
1500 xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1501 cam_freeze_devq(ccb->ccb_h.path);
1502 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1503 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1507 qe = isp_getrqentry(isp);
1509 xpt_print(ccb->ccb_h.path, rqo, __func__);
1510 cam_freeze_devq(ccb->ccb_h.path);
1511 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1512 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1515 memset(local, 0, QENTRY_LEN);
1518 * We're either moving data or completing a command here.
1521 ct7_entry_t *cto = (ct7_entry_t *) local;
1523 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1524 cto->ct_header.rqs_entry_count = 1;
1525 cto->ct_header.rqs_seqno = 1;
1526 cto->ct_nphdl = atp->nphdl;
1527 cto->ct_rxid = atp->tag;
1528 cto->ct_iid_lo = atp->portid;
1529 cto->ct_iid_hi = atp->portid >> 16;
1530 cto->ct_oxid = atp->oxid;
1531 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1532 cto->ct_scsi_status = cso->scsi_status;
1533 cto->ct_timeout = 120;
1534 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1535 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1536 cto->ct_flags |= CT7_SENDSTATUS;
1538 if (cso->dxfer_len == 0) {
1539 cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1540 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1541 int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1542 cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1543 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1544 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1547 cto->ct_flags |= CT7_FLAG_MODE0;
1548 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1549 cto->ct_flags |= CT7_DATA_IN;
1551 cto->ct_flags |= CT7_DATA_OUT;
1553 cto->rsp.m0.reloff = atp->bytes_xfered;
1555 * Don't overrun the limits placed on us
1557 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1558 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1560 atp->last_xframt = cso->dxfer_len;
1561 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1563 if (cto->ct_flags & CT7_SENDSTATUS) {
1564 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1565 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1566 if (cto->ct_resid < 0) {
1567 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1568 } else if (cto->ct_resid > 0) {
1569 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1571 atp->state = ATPD_STATE_LAST_CTIO;
1572 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,
1573 atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1576 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,
1577 cso->dxfer_len, atp->bytes_xfered);
1578 atp->state = ATPD_STATE_CTIO;
1580 } else if (IS_FC(isp)) {
1581 ct2_entry_t *cto = (ct2_entry_t *) local;
1583 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1584 cto->ct_header.rqs_entry_count = 1;
1585 cto->ct_header.rqs_seqno = 1;
1586 if (ISP_CAP_2KLOGIN(isp) == 0) {
1587 ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1589 cto->ct_iid = cso->init_id;
1590 if (ISP_CAP_SCCFW(isp) == 0) {
1591 cto->ct_lun = ccb->ccb_h.target_lun;
1596 cto->ct_rxid = cso->tag_id;
1597 if (cso->dxfer_len == 0) {
1598 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1599 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1600 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1601 if (cto->ct_resid < 0) {
1602 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1603 } else if (cto->ct_resid > 0) {
1604 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1606 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1607 int m = min(cso->sense_len, MAXRESPLEN);
1608 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1609 cto->rsp.m1.ct_senselen = m;
1610 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1611 } else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1615 xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1618 cto->ct_flags |= CT2_FLAG_MODE0;
1619 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1620 cto->ct_flags |= CT2_DATA_IN;
1622 cto->ct_flags |= CT2_DATA_OUT;
1624 cto->ct_reloff = atp->bytes_xfered;
1625 cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1627 * Don't overrun the limits placed on us
1629 if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1630 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1632 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1633 cto->ct_flags |= CT2_SENDSTATUS;
1634 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1635 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1636 if (cto->ct_resid < 0) {
1637 cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1638 } else if (cto->ct_resid > 0) {
1639 cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1642 atp->last_xframt = cso->dxfer_len;
1645 * If we're sending data and status back together,
1646 * we can't also send back sense data as well.
1648 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1651 if (cto->ct_flags & CT2_SENDSTATUS) {
1652 int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1653 cto->ct_flags |= CT2_CCINCR;
1654 atp->state = ATPD_STATE_LAST_CTIO;
1655 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,
1656 atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1659 atp->state = ATPD_STATE_CTIO;
1660 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,
1661 cso->dxfer_len, atp->bytes_xfered);
1663 cto->ct_timeout = 10;
1665 ct_entry_t *cto = (ct_entry_t *) local;
1667 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1668 cto->ct_header.rqs_entry_count = 1;
1669 cto->ct_header.rqs_seqno = 1;
1670 cto->ct_iid = cso->init_id;
1671 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1672 cto->ct_tgt = ccb->ccb_h.target_id;
1673 cto->ct_lun = ccb->ccb_h.target_lun;
1674 cto->ct_fwhandle = cso->tag_id >> 16;
1675 if (AT_HAS_TAG(cso->tag_id)) {
1676 cto->ct_tag_val = cso->tag_id;
1677 cto->ct_flags |= CT_TQAE;
1679 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1680 cto->ct_flags |= CT_NODISC;
1682 if (cso->dxfer_len == 0) {
1683 cto->ct_flags |= CT_NO_DATA;
1684 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1685 cto->ct_flags |= CT_DATA_IN;
1687 cto->ct_flags |= CT_DATA_OUT;
1689 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1690 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1691 cto->ct_scsi_status = cso->scsi_status;
1692 cto->ct_resid = cso->resid;
1693 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1694 cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1696 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1697 cto->ct_timeout = 10;
1700 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1701 xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1702 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1708 * Call the dma setup routines for this entry (and any subsequent
1709 * CTIOs) if there's data to move, and then tell the f/w it's got
1710 * new things to play with. As with isp_start's usage of DMA setup,
1711 * any swizzling is done in the machine dependent layer. Because
1712 * of this, we put the request onto the queue area first in native
1717 ct7_entry_t *cto = (ct7_entry_t *) local;
1718 cto->ct_syshandle = handle;
1719 } else if (IS_FC(isp)) {
1720 ct2_entry_t *cto = (ct2_entry_t *) local;
1721 cto->ct_syshandle = handle;
1723 ct_entry_t *cto = (ct_entry_t *) local;
1724 cto->ct_syshandle = handle;
1727 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1728 if (dmaresult == CMD_QUEUED) {
1730 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1731 rls_lun_statep(isp, tptr);
1734 if (dmaresult == CMD_EAGAIN) {
1735 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1737 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1739 isp_destroy_tgt_handle(isp, handle);
1741 rls_lun_statep(isp, tptr);
1742 isp_free_pcmd(isp, ccb);
1747 isp_refire_putback_atio(void *arg)
1749 union ccb *ccb = arg;
1750 ispsoftc_t *isp = XS_ISP(ccb);
1752 isp_target_putback_atio(ccb);
1757 isp_target_putback_atio(union ccb *ccb)
1760 struct ccb_scsiio *cso;
1765 qe = isp_getrqentry(isp);
1767 xpt_print(ccb->ccb_h.path, rqo, __func__);
1768 (void) timeout(isp_refire_putback_atio, ccb, 10);
1771 memset(qe, 0, QENTRY_LEN);
1774 at2_entry_t local, *at = &local;
1775 ISP_MEMZERO(at, sizeof (at2_entry_t));
1776 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1777 at->at_header.rqs_entry_count = 1;
1778 if (ISP_CAP_SCCFW(isp)) {
1779 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1781 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1783 at->at_status = CT_OK;
1784 at->at_rxid = cso->tag_id;
1785 at->at_iid = cso->ccb_h.target_id;
1786 isp_put_atio2(isp, at, qe);
1788 at_entry_t local, *at = &local;
1789 ISP_MEMZERO(at, sizeof (at_entry_t));
1790 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1791 at->at_header.rqs_entry_count = 1;
1792 at->at_iid = cso->init_id;
1793 at->at_iid |= XS_CHANNEL(ccb) << 7;
1794 at->at_tgt = cso->ccb_h.target_id;
1795 at->at_lun = cso->ccb_h.target_lun;
1796 at->at_status = CT_OK;
1797 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1798 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1799 isp_put_atio(isp, at, qe);
1801 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1802 ISP_SYNC_REQUEST(isp);
1803 isp_complete_ctio(ccb);
1807 isp_complete_ctio(union ccb *ccb)
1809 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1810 ccb->ccb_h.status |= CAM_REQ_CMP;
1812 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1813 isp_free_pcmd(XS_ISP(ccb), ccb);
1818 * Handle ATIO stuff that the generic code can't.
1819 * This means handling CDBs.
1823 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1827 struct ccb_accept_tio *atiop;
1828 atio_private_data_t *atp;
1831 * The firmware status (except for the QLTM_SVALID bit)
1832 * indicates why this ATIO was sent to us.
1834 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1836 * If the DISCONNECTS DISABLED bit is set in the flags field,
1837 * we're still connected on the SCSI bus.
1839 status = aep->at_status;
1840 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1842 * Bus Phase Sequence error. We should have sense data
1843 * suggested by the f/w. I'm not sure quite yet what
1844 * to do about this for CAM.
1846 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1847 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1850 if ((status & ~QLTM_SVALID) != AT_CDB) {
1851 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1852 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1856 bus = GET_BUS_VAL(aep->at_iid);
1857 tptr = get_lun_statep(isp, bus, aep->at_lun);
1859 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1862 * Because we can't autofeed sense data back with
1863 * a command for parallel SCSI, we can't give back
1864 * a CHECK CONDITION. We'll give back a BUSY status
1865 * instead. This works out okay because the only
1866 * time we should, in fact, get this, is in the
1867 * case that somebody configured us without the
1868 * blackhole driver, so they get what they deserve.
1870 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1875 atp = isp_get_atpd(isp, tptr, 0);
1876 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1877 if (atiop == NULL || atp == NULL) {
1879 * Because we can't autofeed sense data back with
1880 * a command for parallel SCSI, we can't give back
1881 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1882 * instead. This works out okay because the only time we
1883 * should, in fact, get this, is in the case that we've
1886 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1887 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1888 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1890 isp_put_atpd(isp, tptr, atp);
1892 rls_lun_statep(isp, tptr);
1895 atp->tag = aep->at_tag_val;
1896 if (atp->tag == 0) {
1899 atp->state = ATPD_STATE_ATIO;
1900 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1902 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1903 atiop->ccb_h.target_id = aep->at_tgt;
1904 atiop->ccb_h.target_lun = aep->at_lun;
1905 if (aep->at_flags & AT_NODISC) {
1906 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1908 atiop->ccb_h.flags = 0;
1911 if (status & QLTM_SVALID) {
1912 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1913 atiop->sense_len = amt;
1914 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1916 atiop->sense_len = 0;
1919 atiop->init_id = GET_IID_VAL(aep->at_iid);
1920 atiop->cdb_len = aep->at_cdblen;
1921 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1922 atiop->ccb_h.status = CAM_CDB_RECVD;
1924 * Construct a tag 'id' based upon tag value (which may be 0..255)
1925 * and the handle (which we have to preserve).
1927 atiop->tag_id = atp->tag;
1928 if (aep->at_flags & AT_TQAE) {
1929 atiop->tag_action = aep->at_tag_type;
1930 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1932 atp->orig_datalen = 0;
1933 atp->bytes_xfered = 0;
1934 atp->last_xframt = 0;
1935 atp->lun = aep->at_lun;
1936 atp->nphdl = aep->at_iid;
1937 atp->portid = PORT_NONE;
1939 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1940 atp->tattr = aep->at_tag_type;
1941 atp->state = ATPD_STATE_CAM;
1942 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1943 rls_lun_statep(isp, tptr);
1947 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1952 struct ccb_accept_tio *atiop;
1954 atio_private_data_t *atp;
1955 inot_private_data_t *ntp;
1958 * The firmware status (except for the QLTM_SVALID bit)
1959 * indicates why this ATIO was sent to us.
1961 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1963 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1964 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1965 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1969 if (ISP_CAP_SCCFW(isp)) {
1970 lun = aep->at_scclun;
1974 if (ISP_CAP_2KLOGIN(isp)) {
1975 nphdl = ((at2e_entry_t *)aep)->at_iid;
1977 nphdl = aep->at_iid;
1979 tptr = get_lun_statep(isp, 0, lun);
1981 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1983 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1984 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1990 * Start any commands pending resources first.
1992 if (tptr->restart_queue) {
1993 inot_private_data_t *restart_queue = tptr->restart_queue;
1994 tptr->restart_queue = NULL;
1995 while (restart_queue) {
1996 ntp = restart_queue;
1997 restart_queue = ntp->rd.nt.nt_hba;
1998 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1999 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2000 isp_put_ntpd(isp, tptr, ntp);
2002 * If a recursion caused the restart queue to start to fill again,
2003 * stop and splice the new list on top of the old list and restore
2004 * it and go to noresrc.
2006 if (tptr->restart_queue) {
2007 ntp = tptr->restart_queue;
2008 tptr->restart_queue = restart_queue;
2009 while (restart_queue->rd.nt.nt_hba) {
2010 restart_queue = restart_queue->rd.nt.nt_hba;
2012 restart_queue->rd.nt.nt_hba = ntp;
2018 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2019 if (atiop == NULL) {
2023 atp = isp_get_atpd(isp, tptr, 0);
2028 atp->tag = aep->at_rxid;
2029 atp->state = ATPD_STATE_ATIO;
2030 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2032 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2033 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2034 atiop->ccb_h.target_lun = lun;
2037 * We don't get 'suggested' sense data as we do with SCSI cards.
2039 atiop->sense_len = 0;
2040 if (ISP_CAP_2KLOGIN(isp)) {
2042 * NB: We could not possibly have 2K logins if we
2043 * NB: also did not have SCC FW.
2045 atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2047 atiop->init_id = aep->at_iid;
2051 * If we're not in the port database, add ourselves.
2053 if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2055 (((uint64_t) aep->at_wwpn[0]) << 48) |
2056 (((uint64_t) aep->at_wwpn[1]) << 32) |
2057 (((uint64_t) aep->at_wwpn[2]) << 16) |
2058 (((uint64_t) aep->at_wwpn[3]) << 0);
2060 * However, make sure we delete ourselves if otherwise
2061 * we were there but at a different loop id.
2063 if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2064 isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2066 isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2068 atiop->cdb_len = ATIO2_CDBLEN;
2069 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2070 atiop->ccb_h.status = CAM_CDB_RECVD;
2071 atiop->tag_id = atp->tag;
2072 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2073 case ATIO2_TC_ATTR_SIMPLEQ:
2074 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2075 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2077 case ATIO2_TC_ATTR_HEADOFQ:
2078 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2079 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2081 case ATIO2_TC_ATTR_ORDERED:
2082 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2083 atiop->tag_action = MSG_ORDERED_Q_TAG;
2085 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2086 case ATIO2_TC_ATTR_UNTAGGED:
2088 atiop->tag_action = 0;
2092 atp->orig_datalen = aep->at_datalen;
2093 atp->bytes_xfered = 0;
2094 atp->last_xframt = 0;
2096 atp->nphdl = atiop->init_id;
2097 atp->sid = PORT_ANY;
2098 atp->oxid = aep->at_oxid;
2099 atp->cdb0 = aep->at_cdb[0];
2100 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2101 atp->state = ATPD_STATE_CAM;
2102 xpt_done((union ccb *)atiop);
2103 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);
2104 rls_lun_statep(isp, tptr);
2107 ntp = isp_get_ntpd(isp, tptr);
2109 rls_lun_statep(isp, tptr);
2110 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2113 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2114 ntp->rd.nt.nt_hba = tptr->restart_queue;
2115 tptr->restart_queue = ntp;
2116 rls_lun_statep(isp, tptr);
2120 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2123 uint16_t lun, chan, nphdl = NIL_HANDLE;
2125 uint64_t wwn = INI_NONE;
2128 struct ccb_accept_tio *atiop;
2129 atio_private_data_t *atp = NULL;
2130 inot_private_data_t *ntp;
2132 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2133 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2134 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2137 * Find the N-port handle, and Virtual Port Index for this command.
2139 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2140 * We also, as a matter of course, need to know the WWN of the initiator too.
2142 if (ISP_CAP_MULTI_ID(isp)) {
2144 * Find the right channel based upon D_ID
2146 isp_find_chan_by_did(isp, did, &chan);
2148 if (chan == ISP_NOCHAN) {
2152 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2153 * It's a bit tricky here as we need to stash this command *somewhere*.
2156 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2157 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2158 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2161 tptr = get_lun_statep(isp, 0, 0);
2163 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2165 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);
2166 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2170 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2173 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);
2179 * Find the PDB entry for this initiator
2181 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2183 * If we're not in the port database terminate the exchange.
2185 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",
2186 __func__, aep->at_rxid, did, chan, sid);
2187 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2194 * Get the tstate pointer
2196 tptr = get_lun_statep(isp, chan, lun);
2198 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2200 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2201 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2207 * Start any commands pending resources first.
2209 if (tptr->restart_queue) {
2210 inot_private_data_t *restart_queue = tptr->restart_queue;
2211 tptr->restart_queue = NULL;
2212 while (restart_queue) {
2213 ntp = restart_queue;
2214 restart_queue = ntp->rd.nt.nt_hba;
2215 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2216 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2217 isp_put_ntpd(isp, tptr, ntp);
2219 * If a recursion caused the restart queue to start to fill again,
2220 * stop and splice the new list on top of the old list and restore
2221 * it and go to noresrc.
2223 if (tptr->restart_queue) {
2224 if (restart_queue) {
2225 ntp = tptr->restart_queue;
2226 tptr->restart_queue = restart_queue;
2227 while (restart_queue->rd.nt.nt_hba) {
2228 restart_queue = restart_queue->rd.nt.nt_hba;
2230 restart_queue->rd.nt.nt_hba = ntp;
2238 * If the f/w is out of resources, just send a BUSY status back.
2240 if (aep->at_rxid == AT7_NORESRC_RXID) {
2241 rls_lun_statep(isp, tptr);
2242 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2247 * If we're out of resources, just send a BUSY status back.
2249 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2250 if (atiop == NULL) {
2251 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2255 atp = isp_get_atpd(isp, tptr, 0);
2257 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2260 if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2261 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x)\n",
2262 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2264 * It's not a "no resource" condition- but we can treat it like one
2269 atp->tag = aep->at_rxid;
2270 atp->state = ATPD_STATE_ATIO;
2271 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2273 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2274 atiop->init_id = nphdl;
2275 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2276 atiop->ccb_h.target_lun = lun;
2277 atiop->sense_len = 0;
2278 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2280 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2282 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2283 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2284 atiop->cdb_len = cdbxlen;
2285 atiop->ccb_h.status = CAM_CDB_RECVD;
2286 atiop->tag_id = atp->tag;
2287 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2288 case FCP_CMND_TASK_ATTR_SIMPLE:
2289 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2290 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2292 case FCP_CMND_TASK_ATTR_HEAD:
2293 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2294 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2296 case FCP_CMND_TASK_ATTR_ORDERED:
2297 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2298 atiop->tag_action = MSG_ORDERED_Q_TAG;
2302 case FCP_CMND_TASK_ATTR_ACA:
2303 case FCP_CMND_TASK_ATTR_UNTAGGED:
2304 atiop->tag_action = 0;
2307 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2308 atp->bytes_xfered = 0;
2309 atp->last_xframt = 0;
2313 atp->oxid = aep->at_hdr.ox_id;
2314 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2315 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2316 atp->state = ATPD_STATE_CAM;
2317 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);
2318 xpt_done((union ccb *)atiop);
2319 rls_lun_statep(isp, tptr);
2323 isp_put_atpd(isp, tptr, atp);
2325 ntp = isp_get_ntpd(isp, tptr);
2327 rls_lun_statep(isp, tptr);
2328 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2331 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2332 ntp->rd.nt.nt_hba = tptr->restart_queue;
2333 tptr->restart_queue = ntp;
2334 rls_lun_statep(isp, tptr);
2338 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2341 int sentstatus, ok, notify_cam, resid = 0;
2342 tstate_t *tptr = NULL;
2343 atio_private_data_t *atp = NULL;
2345 uint32_t tval, handle;
2348 * CTIO handles are 16 bits.
2349 * CTIO2 and CTIO7 are 32 bits.
2353 handle = ((ct_entry_t *)arg)->ct_syshandle;
2355 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2357 ccb = isp_find_xs_tgt(isp, handle);
2359 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2362 isp_destroy_tgt_handle(isp, handle);
2363 bus = XS_CHANNEL(ccb);
2364 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2366 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2368 KASSERT((tptr != NULL), ("cannot get state pointer"));
2369 if (isp->isp_nactive) {
2373 ct7_entry_t *ct = arg;
2375 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2377 rls_lun_statep(isp, tptr);
2378 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2382 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2383 ok = (ct->ct_nphdl == CT7_OK);
2384 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2385 ccb->ccb_h.status |= CAM_SENT_SENSE;
2387 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2388 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2389 resid = ct->ct_resid;
2390 atp->bytes_xfered += (atp->last_xframt - resid);
2391 atp->last_xframt = 0;
2393 if (ct->ct_nphdl == CT_HBA_RESET) {
2397 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2399 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2402 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2403 ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2404 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2405 } else if (IS_FC(isp)) {
2406 ct2_entry_t *ct = arg;
2408 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2410 rls_lun_statep(isp, tptr);
2411 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2414 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2415 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2416 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2417 ccb->ccb_h.status |= CAM_SENT_SENSE;
2419 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2420 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2421 resid = ct->ct_resid;
2422 atp->bytes_xfered += (atp->last_xframt - resid);
2423 atp->last_xframt = 0;
2425 if (ct->ct_status == CT_HBA_RESET) {
2429 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2431 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2433 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2434 ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2436 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2438 ct_entry_t *ct = arg;
2439 sentstatus = ct->ct_flags & CT_SENDSTATUS;
2440 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2442 * We *ought* to be able to get back to the original ATIO
2443 * here, but for some reason this gets lost. It's just as
2444 * well because it's squirrelled away as part of periph
2447 * We can live without it as long as we continue to use
2448 * the auto-replenish feature for CTIOs.
2450 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2451 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2455 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2457 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2458 } else if (ct->ct_status & QLTM_SVALID) {
2459 char *sp = (char *)ct;
2460 sp += CTIO_SENSE_OFFSET;
2461 ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2462 ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2463 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2465 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2466 resid = ct->ct_resid;
2468 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2469 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2470 tval = ct->ct_fwhandle;
2472 ccb->csio.resid += resid;
2475 * We're here either because intermediate data transfers are done
2476 * and/or the final status CTIO (which may have joined with a
2477 * Data Transfer) is done.
2479 * In any case, for this platform, the upper layers figure out
2480 * what to do next, so all we do here is collect status and
2481 * pass information along. Any DMA handles have already been
2484 if (notify_cam == 0) {
2485 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
2489 rls_lun_statep(isp, tptr);
2491 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? " FINAL " : "MIDTERM ", tval);
2493 if (!ok && !IS_24XX(isp)) {
2494 isp_target_putback_atio(ccb);
2496 isp_complete_ctio(ccb);
2501 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2503 (void) isp_notify_ack(isp, inot);
2507 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2510 switch (inp->in_status) {
2511 case IN_PORT_LOGOUT:
2513 * XXX: Need to delete this initiator's WWN from the database
2514 * XXX: Need to send this LOGOUT upstream
2516 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2518 case IN_PORT_CHANGED:
2519 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2521 case IN_GLOBAL_LOGO:
2522 isp_del_all_wwn_entries(isp, 0);
2523 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2531 atio_private_data_t *atp;
2533 struct ccb_immediate_notify *inot = NULL;
2535 if (ISP_CAP_SCCFW(isp)) {
2536 lun = inp->in_scclun;
2540 if (ISP_CAP_2KLOGIN(isp)) {
2541 loopid = ((in_fcentry_e_t *)inp)->in_iid;
2543 loopid = inp->in_iid;
2545 if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2550 tptr = get_lun_statep(isp, 0, lun);
2552 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2554 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2558 atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2561 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2562 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2565 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2566 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2568 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2571 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2574 isp_notify_t tmp, *nt = &tmp;
2575 ISP_MEMZERO(nt, sizeof (isp_notify_t));
2577 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2579 nt->nt_nphdl = loopid;
2580 nt->nt_sid = PORT_ANY;
2581 nt->nt_did = PORT_ANY;
2583 nt->nt_need_ack = 1;
2585 nt->nt_ncode = NT_ABORT_TASK;
2586 nt->nt_lreserved = inot;
2587 isp_handle_platform_target_tmf(isp, nt);
2590 rls_lun_statep(isp, tptr);
2597 (void) isp_notify_ack(isp, inp);
2602 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2607 uint8_t *ptr = NULL;
2610 nphdl = inot->in_nphdl;
2611 if (nphdl != NIL_HANDLE) {
2612 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2617 switch (inot->in_status) {
2618 case IN24XX_ELS_RCVD:
2621 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2624 * Note that we're just getting notification that an ELS was received
2625 * (possibly with some associated information sent upstream). This is
2626 * *not* the same as being given the ELS frame to accept or reject.
2628 switch (inot->in_status_subcode) {
2631 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2632 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2633 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
2634 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2635 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2636 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2637 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2638 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2639 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
2640 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2644 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2652 * Treat PRLI the same as PLOGI and make a database entry for it.
2654 if (inot->in_status_subcode == PLOGI)
2658 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2659 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
2660 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
2661 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2662 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2663 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2664 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2665 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2666 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
2667 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2671 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2680 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2684 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2685 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);
2688 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,
2689 inot->in_rxid, inot->in_oxid);
2690 (void) isp_notify_ack(isp, inot);
2694 case IN24XX_PORT_LOGOUT:
2695 ptr = "PORT LOGOUT";
2696 if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2697 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2700 case IN24XX_PORT_CHANGED:
2702 ptr = "PORT CHANGED";
2705 case IN24XX_LIP_RESET:
2709 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);
2712 * All subcodes here are irrelevant. What is relevant
2713 * is that we need to terminate all active commands from
2714 * this initiator (known by N-port handle).
2716 /* XXX IMPLEMENT XXX */
2717 (void) isp_notify_ack(isp, inot);
2720 case IN24XX_LINK_RESET:
2721 case IN24XX_LINK_FAILED:
2722 case IN24XX_SRR_RCVD:
2724 (void) isp_notify_ack(isp, inot);
2730 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2733 if (isp->isp_state != ISP_RUNSTATE) {
2734 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2739 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2741 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2742 ct7_entry_t local, *cto = &local;
2743 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2748 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2749 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2754 ISP_MEMZERO(&local, sizeof (local));
2755 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2756 cto->ct_header.rqs_entry_count = 1;
2757 cto->ct_nphdl = nphdl;
2758 cto->ct_rxid = aep->at_rxid;
2759 cto->ct_vpidx = mp->nt_channel;
2760 cto->ct_iid_lo = sid;
2761 cto->ct_iid_hi = sid >> 16;
2762 cto->ct_oxid = aep->at_hdr.ox_id;
2763 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2764 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2765 return (isp_target_put_entry(isp, &local));
2769 * This case is for a responding to an ABTS frame
2771 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2774 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2776 if (mp->nt_need_ack) {
2777 uint8_t storage[QENTRY_LEN];
2778 ct7_entry_t *cto = (ct7_entry_t *) storage;
2779 abts_t *abts = (abts_t *)mp->nt_lreserved;
2781 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2782 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2783 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2784 cto->ct_header.rqs_entry_count = 1;
2785 cto->ct_nphdl = mp->nt_nphdl;
2786 cto->ct_rxid = abts->abts_rxid_task;
2787 cto->ct_iid_lo = mp->nt_sid;
2788 cto->ct_iid_hi = mp->nt_sid >> 16;
2789 cto->ct_oxid = abts->abts_ox_id;
2790 cto->ct_vpidx = mp->nt_channel;
2791 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2792 if (isp_target_put_entry(isp, cto)) {
2795 mp->nt_need_ack = 0;
2797 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2805 * Handle logout cases here
2807 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2808 isp_del_all_wwn_entries(isp, mp->nt_channel);
2811 if (mp->nt_ncode == NT_LOGOUT) {
2812 if (!IS_2100(isp) && IS_FC(isp)) {
2813 isp_del_wwn_entries(isp, mp);
2818 * General purpose acknowledgement
2820 if (mp->nt_need_ack) {
2821 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2822 return (isp_notify_ack(isp, mp->nt_lreserved));
2828 * Handle task management functions.
2830 * We show up here with a notify structure filled out.
2832 * The nt_lreserved tag points to the original queue entry
2835 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2839 struct ccb_immediate_notify *inot;
2840 inot_private_data_t *ntp = NULL;
2843 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2844 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2846 * NB: This assignment is necessary because of tricky type conversion.
2847 * XXX: This is tricky and I need to check this. If the lun isn't known
2848 * XXX: for the task management function, it does not of necessity follow
2849 * XXX: that it should go up stream to the wildcard listener.
2851 if (notify->nt_lun == LUN_ANY) {
2852 lun = CAM_LUN_WILDCARD;
2854 lun = notify->nt_lun;
2856 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2858 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2860 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2864 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2866 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2870 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2871 inot->initiator_id = CAM_TARGET_WILDCARD;
2873 inot->initiator_id = lp->handle;
2875 inot->seq_id = notify->nt_tagval;
2876 inot->tag_id = notify->nt_tagval >> 32;
2878 switch (notify->nt_ncode) {
2880 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2881 inot->arg = MSG_ABORT_TASK;
2883 case NT_ABORT_TASK_SET:
2884 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2885 inot->arg = MSG_ABORT_TASK_SET;
2888 inot->arg = MSG_CLEAR_ACA;
2890 case NT_CLEAR_TASK_SET:
2891 inot->arg = MSG_CLEAR_TASK_SET;
2894 inot->arg = MSG_LOGICAL_UNIT_RESET;
2896 case NT_TARGET_RESET:
2897 inot->arg = MSG_TARGET_RESET;
2900 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);
2904 ntp = isp_get_ntpd(isp, tptr);
2906 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2909 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2910 if (notify->nt_lreserved) {
2911 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2912 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2914 ntp->rd.seq_id = notify->nt_tagval;
2915 ntp->rd.tag_id = notify->nt_tagval >> 32;
2918 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2919 rls_lun_statep(isp, tptr);
2920 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2921 inot->ccb_h.status = CAM_MESSAGE_RECV;
2922 xpt_done((union ccb *)inot);
2926 rls_lun_statep(isp, tptr);
2928 if (notify->nt_need_ack && notify->nt_lreserved) {
2929 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2930 (void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
2932 (void) isp_notify_ack(isp, notify->nt_lreserved);
2938 * Find the associated private data and mark it as dead so
2939 * we don't try to work on it any further.
2942 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
2945 atio_private_data_t *atp;
2947 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
2949 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
2951 ccb->ccb_h.status = CAM_REQ_INVALID;
2956 atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
2958 ccb->ccb_h.status = CAM_REQ_INVALID;
2962 ccb->ccb_h.status = CAM_REQ_CMP;
2966 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
2968 atio_private_data_t *atp;
2969 inot_private_data_t *restart_queue = tptr->restart_queue;
2972 * First, clean any commands pending restart
2974 tptr->restart_queue = NULL;
2975 while (restart_queue) {
2976 uint32_t this_tag_id;
2977 inot_private_data_t *ntp = restart_queue;
2979 restart_queue = ntp->rd.nt.nt_hba;
2982 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
2984 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
2986 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2987 isp_put_ntpd(isp, tptr, ntp);
2989 ntp->rd.nt.nt_hba = tptr->restart_queue;
2990 tptr->restart_queue = ntp;
2995 * Now mark other ones dead as well.
2997 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
2998 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3005 #ifdef ISP_INTERNAL_TARGET
3006 // #define ISP_FORCE_TIMEOUT 1
3007 // #define ISP_TEST_WWNS 1
3008 // #define ISP_TEST_SEPARATE_STATUS 1
3010 #define ccb_data_offset ppriv_field0
3011 #define ccb_atio ppriv_ptr1
3012 #define ccb_inot ppriv_ptr1
3014 #define MAX_ISP_TARG_TRANSFER (2 << 20)
3015 #define NISP_TARG_CMDS 1024
3016 #define NISP_TARG_NOTIFIES 1024
3017 #define DISK_SHIFT 9
3018 #define JUNK_SIZE 256
3021 #define VERIFY_10 0x2f
3024 TAILQ_HEAD(ccb_queue, ccb_hdr);
3025 extern u_int vm_kmem_size;
3027 static uint32_t disk_size;
3028 static uint8_t *disk_data = NULL;
3029 static uint8_t *junk_data;
3030 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3031 struct isptarg_softc {
3032 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3033 struct ccb_queue work_queue;
3034 struct ccb_queue rework_queue;
3035 struct ccb_queue running_queue;
3036 struct ccb_queue inot_queue;
3037 struct cam_periph *periph;
3038 struct cam_path *path;
3041 static periph_ctor_t isptargctor;
3042 static periph_dtor_t isptargdtor;
3043 static periph_start_t isptargstart;
3044 static periph_init_t isptarginit;
3045 static void isptarg_done(struct cam_periph *, union ccb *);
3046 static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3049 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3051 static struct periph_driver isptargdriver =
3053 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3062 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3064 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3066 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__,
3067 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3068 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3069 ack->ccb_h.flags = 0;
3070 ack->ccb_h.retry_count = 0;
3071 ack->ccb_h.cbfcnp = isptarg_done;
3072 ack->ccb_h.timeout = 0;
3073 ack->ccb_h.ccb_inot = inot;
3074 ack->tag_id = inot->tag_id;
3075 ack->seq_id = inot->seq_id;
3076 ack->initiator_id = inot->initiator_id;
3081 isptargstart(struct cam_periph *periph, union ccb *iccb)
3083 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3084 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3085 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3086 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3087 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3090 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3091 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3092 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3093 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3094 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3097 int i, more = 0, last;
3098 struct isptarg_softc *softc = periph->softc;
3099 struct ccb_scsiio *csio;
3100 lun_id_t return_lun;
3101 struct ccb_accept_tio *atio;
3102 uint8_t *cdb, *ptr, status;
3104 uint32_t data_len, flags;
3105 struct ccb_hdr *ccbh;
3107 KKASSERT(lockstatus(periph->sim->mtx, curthread) != 0);
3108 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,
3109 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3111 * Check for immediate notifies first
3113 ccbh = TAILQ_FIRST(&softc->inot_queue);
3115 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3116 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3117 xpt_schedule(periph, 1);
3119 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3124 * Check the rework (continuation) work queue first.
3126 ccbh = TAILQ_FIRST(&softc->rework_queue);
3128 atio = (struct ccb_accept_tio *)ccbh;
3129 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3130 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3132 ccbh = TAILQ_FIRST(&softc->work_queue);
3134 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3135 xpt_release_ccb(iccb);
3138 atio = (struct ccb_accept_tio *)ccbh;
3139 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3140 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3141 atio->ccb_h.ccb_data_offset = 0;
3144 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3151 status = SCSI_STATUS_OK;
3152 flags = CAM_SEND_STATUS;
3153 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3154 cdb = atio->cdb_io.cdb_bytes;
3155 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,
3156 cdb[0], atio->ccb_h.ccb_data_offset);
3158 return_lun = XS_LUN(atio);
3159 if (return_lun != 0) {
3160 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3161 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3162 status = SCSI_STATUS_CHECK_COND;
3163 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3164 atio->sense_data.add_sense_code = 0x25;
3165 atio->sense_data.add_sense_code_qual = 0x0;
3166 atio->sense_len = sizeof (atio->sense_data);
3168 return_lun = CAM_LUN_WILDCARD;
3173 flags |= CAM_DIR_IN;
3174 data_len = sizeof (atio->sense_data);
3175 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3176 memset(junk_data+1, 0, data_len-1);
3177 if (data_len > cdb[4]) {
3181 data_ptr = junk_data;
3188 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3189 status = SCSI_STATUS_CHECK_COND;
3190 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3191 atio->sense_data.add_sense_code = 0x5;
3192 atio->sense_data.add_sense_code_qual = 0x24;
3193 atio->sense_len = sizeof (atio->sense_data);
3195 #ifdef ISP_FORCE_TIMEOUT
3200 xpt_schedule(periph, 1);
3207 #ifdef ISP_TEST_SEPARATE_STATUS
3208 if (last && data_len) {
3213 flags &= ~CAM_SEND_STATUS;
3216 atio->ccb_h.ccb_data_offset += data_len;
3217 flags |= CAM_DIR_IN;
3219 flags |= CAM_DIR_NONE;
3227 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3228 status = SCSI_STATUS_CHECK_COND;
3229 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3230 atio->sense_data.add_sense_code = 0x5;
3231 atio->sense_data.add_sense_code_qual = 0x24;
3232 atio->sense_len = sizeof (atio->sense_data);
3234 #ifdef ISP_FORCE_TIMEOUT
3239 xpt_schedule(periph, 1);
3246 #ifdef ISP_TEST_SEPARATE_STATUS
3247 if (last && data_len) {
3252 flags &= ~CAM_SEND_STATUS;
3255 atio->ccb_h.ccb_data_offset += data_len;
3256 flags |= CAM_DIR_OUT;
3258 flags |= CAM_DIR_NONE;
3263 flags |= CAM_DIR_IN;
3264 if (cdb[1] || cdb[2] || cdb[3]) {
3265 status = SCSI_STATUS_CHECK_COND;
3266 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3267 atio->sense_data.add_sense_code = 0x5;
3268 atio->sense_data.add_sense_code_qual = 0x20;
3269 atio->sense_len = sizeof (atio->sense_data);
3272 data_len = sizeof (iqd);
3273 if (data_len > cdb[4]) {
3277 if (XS_LUN(iccb) != 0) {
3278 memcpy(junk_data, niliqd, sizeof (iqd));
3280 memcpy(junk_data, iqd, sizeof (iqd));
3282 data_ptr = junk_data;
3285 case TEST_UNIT_READY:
3286 flags |= CAM_DIR_NONE;
3289 status = SCSI_STATUS_CHECK_COND;
3290 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3291 atio->sense_data.add_sense_code = 0x28;
3292 atio->sense_data.add_sense_code_qual = 0x0;
3293 atio->sense_len = sizeof (atio->sense_data);
3296 case SYNCHRONIZE_CACHE:
3301 flags |= CAM_DIR_NONE;
3305 flags |= CAM_DIR_IN;
3306 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3307 status = SCSI_STATUS_CHECK_COND;
3308 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3309 atio->sense_data.add_sense_code = 0x5;
3310 atio->sense_data.add_sense_code_qual = 0x24;
3311 atio->sense_len = sizeof (atio->sense_data);
3314 if (cdb[8] & 0x1) { /* PMI */
3315 junk_data[0] = 0xff;
3316 junk_data[1] = 0xff;
3317 junk_data[2] = 0xff;
3318 junk_data[3] = 0xff;
3320 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3321 if (last_blk < 0xffffffffULL) {
3322 junk_data[0] = (last_blk >> 24) & 0xff;
3323 junk_data[1] = (last_blk >> 16) & 0xff;
3324 junk_data[2] = (last_blk >> 8) & 0xff;
3325 junk_data[3] = (last_blk) & 0xff;
3327 junk_data[0] = 0xff;
3328 junk_data[1] = 0xff;
3329 junk_data[2] = 0xff;
3330 junk_data[3] = 0xff;
3333 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3334 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3335 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3336 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3337 data_ptr = junk_data;
3341 flags |= CAM_DIR_IN;
3342 memset(junk_data, 0, JUNK_SIZE);
3343 junk_data[0] = (1 << 3) >> 24;
3344 junk_data[1] = (1 << 3) >> 16;
3345 junk_data[2] = (1 << 3) >> 8;
3346 junk_data[3] = (1 << 3);
3348 for (i = 0; i < 1; i++) {
3349 ptr = &junk_data[8 + (1 << 3)];
3351 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3355 data_ptr = junk_data;
3356 data_len = (ptr + 8) - junk_data;
3360 flags |= CAM_DIR_NONE;
3361 status = SCSI_STATUS_CHECK_COND;
3362 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3363 atio->sense_data.add_sense_code = 0x5;
3364 atio->sense_data.add_sense_code_qual = 0x20;
3365 atio->sense_len = sizeof (atio->sense_data);
3370 * If we are done with the transaction, tell the
3371 * controller to send status and perform a CMD_CMPLT.
3372 * If we have associated sense data, see if we can
3375 if (status == SCSI_STATUS_CHECK_COND) {
3376 flags |= CAM_SEND_SENSE;
3377 csio->sense_len = atio->sense_len;
3378 csio->sense_data = atio->sense_data;
3379 flags &= ~CAM_DIR_MASK;
3383 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3384 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3385 iccb->ccb_h.target_lun = return_lun;
3386 iccb->ccb_h.ccb_atio = atio;
3389 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3390 cam_release_devq(periph->path, 0, 0, 0, 0);
3391 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3394 xpt_schedule(periph, 1);
3399 isptargctor(struct cam_periph *periph, void *arg)
3401 struct isptarg_softc *softc;
3403 softc = (struct isptarg_softc *)arg;
3404 periph->softc = softc;
3405 softc->periph = periph;
3406 softc->path = periph->path;
3407 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3408 return (CAM_REQ_CMP);
3412 isptargdtor(struct cam_periph *periph)
3414 struct isptarg_softc *softc;
3415 softc = (struct isptarg_softc *)periph->softc;
3416 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3417 softc->periph = NULL;
3419 periph->softc = NULL;
3423 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3425 struct isptarg_softc *softc;
3427 struct ccb_accept_tio *atio;
3428 struct ccb_immediate_notify *inot;
3431 softc = (struct isptarg_softc *)periph->softc;
3433 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3435 switch (ccb->ccb_h.func_code) {
3436 case XPT_ACCEPT_TARGET_IO:
3437 atio = (struct ccb_accept_tio *) ccb;
3438 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3439 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
3440 xpt_schedule(periph, 1);
3442 case XPT_IMMEDIATE_NOTIFY:
3443 inot = (struct ccb_immediate_notify *) ccb;
3444 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__);
3445 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
3446 xpt_schedule(periph, 1);
3448 case XPT_CONT_TARGET_IO:
3449 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3450 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3451 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3453 atio = ccb->ccb_h.ccb_atio;
3454 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3455 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3456 xpt_action((union ccb *)atio);
3457 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3458 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3459 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
3460 xpt_schedule(periph, 1);
3462 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3463 xpt_action((union ccb *)atio);
3465 xpt_release_ccb(ccb);
3467 case XPT_NOTIFY_ACKNOWLEDGE:
3468 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3469 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3470 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3472 inot = ccb->ccb_h.ccb_inot;
3473 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);
3474 xpt_release_ccb(ccb);
3475 xpt_action((union ccb *)inot);
3478 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3484 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3486 struct ac_contract *acp = arg;
3487 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3489 if (code != AC_CONTRACT) {
3492 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3496 isp_target_thread(ispsoftc_t *isp, int chan)
3498 union ccb *ccb = NULL;
3502 struct isptarg_softc *softc = NULL;
3503 struct cam_periph *periph = NULL, *wperiph = NULL;
3504 struct cam_path *path, *wpath;
3505 struct cam_sim *sim;
3507 if (disk_data == NULL) {
3508 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3509 if (disk_size < (50 << 20)) {
3510 disk_size = 50 << 20;
3512 disk_data = kmalloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3513 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3515 junk_data = kmalloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3518 softc = kmalloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3519 TAILQ_INIT(&softc->work_queue);
3520 TAILQ_INIT(&softc->rework_queue);
3521 TAILQ_INIT(&softc->running_queue);
3522 TAILQ_INIT(&softc->inot_queue);
3525 periphdriver_register(&isptargdriver);
3526 ISP_GET_PC(isp, chan, sim, sim);
3527 ISP_GET_PC(isp, chan, path, path);
3528 status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3529 if (status != CAM_REQ_CMP) {
3530 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3533 status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3534 if (status != CAM_REQ_CMP) {
3535 xpt_free_path(wpath);
3536 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3541 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3542 if (status != CAM_REQ_CMP) {
3544 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3547 wperiph = cam_periph_find(wpath, "isptarg");
3548 if (wperiph == NULL) {
3550 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3554 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3555 if (status != CAM_REQ_CMP) {
3557 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3561 periph = cam_periph_find(path, "isptarg");
3562 if (periph == NULL) {
3564 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3568 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3569 if (status != CAM_REQ_CMP) {
3571 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3577 ccb = xpt_alloc_ccb();
3580 * Make sure role is none.
3582 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3583 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3584 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3585 #ifdef ISP_TEST_WWNS
3586 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3587 ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3588 ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3590 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3600 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3601 ccb->ccb_h.func_code = XPT_EN_LUN;
3602 ccb->cel.enable = 1;
3606 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3608 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3612 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3613 ccb->ccb_h.func_code = XPT_EN_LUN;
3614 ccb->cel.enable = 1;
3618 if (ccb->ccb_h.status != CAM_REQ_CMP) {
3620 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3628 ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3629 for (i = 0; i < 4; i++) {
3630 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3631 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3632 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3633 ccb->ccb_h.cbfcnp = isptarg_done;
3638 for (i = 0; i < NISP_TARG_CMDS; i++) {
3639 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3640 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3641 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3642 ccb->ccb_h.cbfcnp = isptarg_done;
3647 for (i = 0; i < 4; i++) {
3648 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3649 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3650 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3651 ccb->ccb_h.cbfcnp = isptarg_done;
3656 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3657 ccb = kmalloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3658 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3659 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3660 ccb->ccb_h.cbfcnp = isptarg_done;
3667 * Now turn it all back on
3669 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3670 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3671 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3672 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3678 * Okay, while things are still active, sleep...
3682 ISP_GET_PC(isp, chan, proc_active, i);
3686 lksleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3692 cam_periph_invalidate(wperiph);
3695 cam_periph_invalidate(periph);
3698 kfree(junk_data, M_ISPTARG);
3701 kfree(disk_data, M_ISPTARG);
3704 kfree(softc, M_ISPTARG);
3706 xpt_free_path(path);
3707 xpt_free_path(wpath);
3711 isp_target_thread_pi(void *arg)
3713 struct isp_spi *pi = arg;
3714 isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3718 isp_target_thread_fc(void *arg)
3720 struct isp_fc *fc = arg;
3721 isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3725 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3727 uint32_t cnt, curcnt;
3733 cnt = (((uint32_t)cdb[10]) << 24) |
3734 (((uint32_t)cdb[11]) << 16) |
3735 (((uint32_t)cdb[12]) << 8) |
3736 ((uint32_t)cdb[13]);
3738 lba = (((uint64_t)cdb[2]) << 56) |
3739 (((uint64_t)cdb[3]) << 48) |
3740 (((uint64_t)cdb[4]) << 40) |
3741 (((uint64_t)cdb[5]) << 32) |
3742 (((uint64_t)cdb[6]) << 24) |
3743 (((uint64_t)cdb[7]) << 16) |
3744 (((uint64_t)cdb[8]) << 8) |
3749 cnt = (((uint32_t)cdb[6]) << 16) |
3750 (((uint32_t)cdb[7]) << 8) |
3751 ((u_int32_t)cdb[8]);
3753 lba = (((uint32_t)cdb[2]) << 24) |
3754 (((uint32_t)cdb[3]) << 16) |
3755 (((uint32_t)cdb[4]) << 8) |
3760 cnt = (((uint32_t)cdb[7]) << 8) |
3761 ((u_int32_t)cdb[8]);
3763 lba = (((uint32_t)cdb[2]) << 24) |
3764 (((uint32_t)cdb[3]) << 16) |
3765 (((uint32_t)cdb[4]) << 8) |
3774 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
3775 (((uint32_t)cdb[2]) << 8) |
3785 if (offset == cnt) {
3790 if (lba + cnt > dl) {
3795 curcnt = MAX_ISP_TARG_TRANSFER;
3796 if (offset + curcnt >= cnt) {
3797 curcnt = cnt - offset;
3803 *kp = &dp[lba + offset];
3811 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3813 struct cam_sim *sim;
3817 sim = (struct cam_sim *)cbarg;
3818 isp = (ispsoftc_t *) cam_sim_softc(sim);
3819 bus = cam_sim_bus(sim);
3820 tgt = xpt_path_target_id(path);
3823 case AC_LOST_DEVICE:
3825 uint16_t oflags, nflags;
3826 sdparam *sdp = SDPARAM(isp, bus);
3829 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3830 #ifndef ISP_TARGET_MODE
3831 nflags &= DPARM_SAFE_DFLT;
3832 if (isp->isp_loaded_fw) {
3833 nflags |= DPARM_NARROW | DPARM_ASYNC;
3836 nflags = DPARM_DEFAULT;
3838 oflags = sdp->isp_devparam[tgt].goal_flags;
3839 sdp->isp_devparam[tgt].goal_flags = nflags;
3840 sdp->isp_devparam[tgt].dev_update = 1;
3842 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3843 sdp->isp_devparam[tgt].goal_flags = oflags;
3848 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3854 isp_poll(struct cam_sim *sim)
3856 ispsoftc_t *isp = cam_sim_softc(sim);
3858 uint16_t sema, mbox;
3860 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3861 isp_intr(isp, isr, sema, mbox);
3867 isp_watchdog(void *arg)
3869 struct ccb_scsiio *xs = arg;
3871 uint32_t ohandle = ISP_HANDLE_FREE, handle;
3876 handle = isp_find_handle(isp, xs);
3878 if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3879 isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3880 callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3888 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3890 if (handle != ISP_HANDLE_FREE) {
3892 uint16_t sema, mbox;
3893 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3894 isp_intr(isp, isr, sema, mbox);
3897 handle = isp_find_handle(isp, xs);
3899 if (handle != ISP_HANDLE_FREE) {
3901 * Try and make sure the command is really dead before
3902 * we release the handle (and DMA resources) for reuse.
3904 * If we are successful in aborting the command then
3905 * we're done here because we'll get the command returned
3908 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3914 * Note that after calling the above, the command may in
3915 * fact have been completed.
3917 xs = isp_find_xs(isp, handle);
3920 * If the command no longer exists, then we won't
3921 * be able to find the xs again with this handle.
3929 * After this point, the command is really dead.
3931 if (XS_XFRLEN(xs)) {
3932 ISP_DMAFREE(isp, xs, handle);
3934 isp_destroy_handle(isp, handle);
3935 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3936 XS_SETERR(xs, CAM_CMD_TIMEOUT);
3937 isp_prt_endcmd(isp, xs);
3940 if (ohandle != ISP_HANDLE_FREE) {
3941 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3943 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3950 isp_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
3952 if (ccb->ccb_h.status != CAM_REQ_CMP)
3953 kprintf("cam_scan_callback: failure status = %x\n",
3956 xpt_free_path(ccb->ccb_h.path);
3961 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3964 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3966 if (isp_autoconfig == 0) {
3971 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3973 ccb = xpt_alloc_ccb();
3975 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3978 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3979 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3983 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
3984 ccb->ccb_h.func_code = XPT_SCAN_BUS;
3985 ccb->ccb_h.cbfcnp = isp_bus_scan_cb;
3986 ccb->crcn.flags = CAM_FLAG_NONE;
3991 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
3993 struct cam_path *tp;
3994 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3996 if (isp_autoconfig == 0) {
3999 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4000 xpt_async(AC_LOST_DEVICE, tp, NULL);
4006 * Gone Device Timer Function- when we have decided that a device has gone
4007 * away, we wait a specific period of time prior to telling the OS it has
4010 * This timer function fires once a second and then scans the port database
4011 * for devices that are marked dead but still have a virtual target assigned.
4012 * We decrement a counter for that port database entry, and when it hits zero,
4013 * we tell the OS the device has gone away.
4018 struct isp_fc *fc = arg;
4021 taskqueue_enqueue(taskqueue_swi, &fc->gtask);
4022 ISP_UNLOCK(fc->isp);
4026 isp_gdt_task(void *arg, int pending)
4028 struct isp_fc *fc = arg;
4029 ispsoftc_t *isp = fc->isp;
4030 int chan = fc - isp->isp_osinfo.pc.fc;
4032 int dbidx, tgt, more_to_do = 0;
4035 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4036 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4037 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4039 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4042 if (lp->dev_map_idx == 0 || lp->target_mode) {
4045 if (lp->gone_timer != 0) {
4046 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);
4047 lp->gone_timer -= 1;
4051 tgt = lp->dev_map_idx - 1;
4052 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4053 lp->dev_map_idx = 0;
4054 lp->state = FC_PORTDB_STATE_NIL;
4055 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4056 isp_make_gone(isp, chan, tgt);
4060 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4062 callout_deactivate(&fc->gdt);
4063 isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_second);
4070 * Loop Down Timer Function- when loop goes down, a timer is started and
4071 * and after it expires we come here and take all probational devices that
4072 * the OS knows about and the tell the OS that they've gone away.
4074 * We don't clear the devices out of our port database because, when loop
4075 * come back up, we have to do some actual cleanup with the chip at that
4076 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4081 struct isp_fc *fc = arg;
4084 taskqueue_enqueue(taskqueue_swi, &fc->ltask);
4085 ISP_UNLOCK(fc->isp);
4089 isp_ldt_task(void *arg, int pending)
4091 struct isp_fc *fc = arg;
4092 ispsoftc_t *isp = fc->isp;
4093 int chan = fc - isp->isp_osinfo.pc.fc;
4098 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_second);
4099 callout_deactivate(&fc->ldt);
4102 * Notify to the OS all targets who we now consider have departed.
4104 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4105 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4107 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4110 if (lp->dev_map_idx == 0 || lp->target_mode) {
4115 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4119 for (i = 0; i < isp->isp_maxcmds; i++) {
4120 struct ccb_scsiio *xs;
4122 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4125 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4128 if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4131 isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4132 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4136 * Mark that we've announced that this device is gone....
4141 * but *don't* change the state of the entry. Just clear
4142 * any target id stuff and announce to CAM that the
4143 * device is gone. This way any necessary PLOGO stuff
4144 * will happen when loop comes back up.
4147 tgt = lp->dev_map_idx - 1;
4148 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4149 lp->dev_map_idx = 0;
4150 lp->state = FC_PORTDB_STATE_NIL;
4151 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4152 isp_make_gone(isp, chan, tgt);
4155 if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4156 isp_unfreeze_loopdown(isp, chan);
4159 * The loop down timer has expired. Wake up the kthread
4160 * to notice that fact (or make it false).
4163 fc->loop_down_time = fc->loop_down_limit+1;
4169 isp_kthread(void *arg)
4171 struct isp_fc *fc = arg;
4172 ispsoftc_t *isp = fc->isp;
4173 int chan = fc - isp->isp_osinfo.pc.fc;
4176 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4181 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4182 lb = isp_fc_runstate(isp, chan, 250000);
4185 * Our action is different based upon whether we're supporting
4186 * Initiator mode or not. If we are, we might freeze the simq
4187 * when loop is down and set all sorts of different delays to
4190 * If not, we simply just wait for loop to come up.
4192 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4194 * Increment loop down time by the last sleep interval
4196 fc->loop_down_time += slp;
4199 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4201 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4205 * If we've never seen loop up and we've waited longer
4206 * than quickboot time, or we've seen loop up but we've
4207 * waited longer than loop_down_limit, give up and go
4208 * to sleep until loop comes up.
4210 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4211 lim = isp_quickboot_time;
4213 lim = fc->loop_down_limit;
4215 if (fc->loop_down_time >= lim) {
4216 isp_freeze_loopdown(isp, chan, "loop limit hit");
4218 } else if (fc->loop_down_time < 10) {
4220 } else if (fc->loop_down_time < 30) {
4222 } else if (fc->loop_down_time < 60) {
4224 } else if (fc->loop_down_time < 120) {
4231 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4232 fc->loop_down_time += slp;
4235 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4236 fc->loop_down_time = 0;
4242 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4243 * now so that CAM can start sending us commands.
4245 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4246 * or kill the commands, as appropriate.
4249 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4250 isp_unfreeze_loopdown(isp, chan);
4253 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4255 lksleep(fc, &isp->isp_osinfo.lock, 0, "ispf", slp * hz);
4258 * If slp is zero, we're waking up for the first time after
4259 * things have been okay. In this case, we set a deferral state
4260 * for all commands and delay hysteresis seconds before starting
4261 * the FC state evaluation. This gives the loop/fabric a chance
4264 if (slp == 0 && fc->hysteresis) {
4265 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4266 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4267 tsleep(isp_kthread, 0, "ispt", fc->hysteresis * hz);
4268 lockmgr(&isp->isp_osinfo.lock, LK_EXCLUSIVE);
4271 lockmgr(&isp->isp_osinfo.lock, LK_RELEASE);
4275 isp_action(struct cam_sim *sim, union ccb *ccb)
4277 int bus, tgt, ts, error, lim;
4279 struct ccb_trans_settings *cts;
4281 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4283 isp = (ispsoftc_t *)cam_sim_softc(sim);
4284 KKASSERT(lockstatus(&isp->isp_lock, curthread) != 0);
4286 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4288 if (isp->isp_state != ISP_INITSTATE) {
4290 * Lie. Say it was a selection timeout.
4292 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4293 xpt_freeze_devq(ccb->ccb_h.path, 1);
4297 isp->isp_state = ISP_RUNSTATE;
4299 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4300 ISP_PCMD(ccb) = NULL;
4302 switch (ccb->ccb_h.func_code) {
4303 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4304 bus = XS_CHANNEL(ccb);
4306 * Do a couple of preliminary checks...
4308 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4309 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4310 ccb->ccb_h.status = CAM_REQ_INVALID;
4316 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4317 xpt_print(ccb->ccb_h.path, "invalid target\n");
4318 ccb->ccb_h.status = CAM_PATH_INVALID;
4319 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4320 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4321 ccb->ccb_h.status = CAM_PATH_INVALID;
4323 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4328 ccb->csio.scsi_status = SCSI_STATUS_OK;
4329 if (isp_get_pcmd(isp, ccb)) {
4330 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4331 cam_freeze_devq(ccb->ccb_h.path);
4332 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4336 error = isp_start((XS_T *) ccb);
4339 XS_CMD_S_CLEAR(ccb);
4340 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4341 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4344 ts = ccb->ccb_h.timeout;
4345 if (ts == CAM_TIME_DEFAULT) {
4348 ts = isp_mstohz(ts);
4350 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4354 * We get this result for FC devices if the loop state isn't ready yet
4355 * or if the device in question has gone zombie on us.
4357 * If we've never seen Loop UP at all, we requeue this request and wait
4358 * for the initial loop up delay to expire.
4360 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4361 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4362 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4363 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_second);
4365 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);
4367 ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4368 xpt_freeze_devq(ccb->ccb_h.path, 1);
4369 isp_free_pcmd(isp, ccb);
4373 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4374 cam_freeze_devq(ccb->ccb_h.path);
4375 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4376 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4377 isp_free_pcmd(isp, ccb);
4381 isp_free_pcmd(isp, ccb);
4382 cam_freeze_devq(ccb->ccb_h.path);
4383 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4384 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4388 isp_done((struct ccb_scsiio *) ccb);
4391 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4392 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4393 isp_free_pcmd(isp, ccb);
4398 #ifdef ISP_TARGET_MODE
4399 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
4400 if (ccb->cel.enable) {
4401 isp_enable_lun(isp, ccb);
4403 isp_disable_lun(isp, ccb);
4406 case XPT_IMMED_NOTIFY:
4407 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
4408 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
4410 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4412 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4418 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4419 str = "XPT_IMMEDIATE_NOTIFY";
4420 tag = ccb->cin1.seq_id;
4422 tag = ccb->atio.tag_id;
4423 str = "XPT_ACCEPT_TARGET_IO";
4425 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4426 dump_tstates(isp, XS_CHANNEL(ccb));
4427 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4430 ccb->ccb_h.sim_priv.entries[0].field = 0;
4431 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4432 ccb->ccb_h.flags = 0;
4434 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4435 if (ccb->atio.tag_id) {
4436 atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4438 isp_put_atpd(isp, tptr, atp);
4442 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4443 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4444 ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
4445 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4446 if (ccb->cin1.tag_id) {
4447 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4449 isp_put_ntpd(isp, tptr, ntp);
4453 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4454 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4455 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4456 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4458 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4459 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4460 ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4462 rls_lun_statep(isp, tptr);
4463 ccb->ccb_h.status = CAM_REQ_INPROG;
4466 case XPT_NOTIFY_ACK:
4467 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4469 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
4472 inot_private_data_t *ntp;
4475 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4476 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4479 * All the relevant path information is in the associated immediate notify
4481 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);
4482 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4484 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__,
4485 ccb->cna2.tag_id, ccb->cna2.seq_id);
4486 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4490 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4491 rls_lun_statep(isp, tptr);
4492 cam_freeze_devq(ccb->ccb_h.path);
4493 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4494 XS_SETERR(ccb, CAM_REQUEUE_REQ);
4497 isp_put_ntpd(isp, tptr, ntp);
4498 rls_lun_statep(isp, tptr);
4499 ccb->ccb_h.status = CAM_REQ_CMP;
4500 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);
4504 case XPT_CONT_TARGET_IO:
4505 isp_target_start_ctio(isp, ccb);
4508 case XPT_RESET_DEV: /* BDR the specified SCSI device */
4510 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4511 tgt = ccb->ccb_h.target_id;
4514 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4516 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4518 ccb->ccb_h.status = CAM_REQ_CMP;
4522 case XPT_ABORT: /* Abort the specified CCB */
4524 union ccb *accb = ccb->cab.abort_ccb;
4525 switch (accb->ccb_h.func_code) {
4526 #ifdef ISP_TARGET_MODE
4527 case XPT_ACCEPT_TARGET_IO:
4528 isp_target_mark_aborted(isp, accb);
4532 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4534 ccb->ccb_h.status = CAM_UA_ABORT;
4536 ccb->ccb_h.status = CAM_REQ_CMP;
4540 ccb->ccb_h.status = CAM_REQ_INVALID;
4544 * This is not a queued CCB, so the caller expects it to be
4545 * complete when control is returned.
4549 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
4550 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
4552 if (!IS_CURRENT_SETTINGS(cts)) {
4553 ccb->ccb_h.status = CAM_REQ_INVALID;
4557 tgt = cts->ccb_h.target_id;
4558 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4560 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4561 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4562 sdparam *sdp = SDPARAM(isp, bus);
4565 if (spi->valid == 0 && scsi->valid == 0) {
4566 ccb->ccb_h.status = CAM_REQ_CMP;
4572 * We always update (internally) from goal_flags
4573 * so any request to change settings just gets
4574 * vectored to that location.
4576 dptr = &sdp->isp_devparam[tgt].goal_flags;
4578 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4579 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4580 *dptr |= DPARM_DISC;
4582 *dptr &= ~DPARM_DISC;
4585 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4586 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4587 *dptr |= DPARM_TQING;
4589 *dptr &= ~DPARM_TQING;
4592 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4593 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4594 *dptr |= DPARM_WIDE;
4596 *dptr &= ~DPARM_WIDE;
4602 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4603 *dptr |= DPARM_SYNC;
4605 * XXX: CHECK FOR LEGALITY
4607 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4608 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4610 *dptr &= ~DPARM_SYNC;
4612 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,
4613 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4614 sdp->isp_devparam[tgt].dev_update = 1;
4617 ccb->ccb_h.status = CAM_REQ_CMP;
4620 case XPT_GET_TRAN_SETTINGS:
4622 tgt = cts->ccb_h.target_id;
4623 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4625 fcparam *fcp = FCPARAM(isp, bus);
4626 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4627 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4628 unsigned int hdlidx;
4630 cts->protocol = PROTO_SCSI;
4631 cts->protocol_version = SCSI_REV_2;
4632 cts->transport = XPORT_FC;
4633 cts->transport_version = 0;
4635 scsi->valid = CTS_SCSI_VALID_TQ;
4636 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4637 fc->valid = CTS_FC_VALID_SPEED;
4638 fc->bitrate = 100000;
4639 fc->bitrate *= fcp->isp_gbspeed;
4640 hdlidx = fcp->isp_dev_map[tgt] - 1;
4641 if (hdlidx < MAX_FC_TARG) {
4642 fcportdb_t *lp = &fcp->portdb[hdlidx];
4643 fc->wwnn = lp->node_wwn;
4644 fc->wwpn = lp->port_wwn;
4645 fc->port = lp->portid;
4646 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4649 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4650 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4651 sdparam *sdp = SDPARAM(isp, bus);
4652 uint16_t dval, pval, oval;
4654 if (IS_CURRENT_SETTINGS(cts)) {
4655 sdp->isp_devparam[tgt].dev_refresh = 1;
4657 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4658 dval = sdp->isp_devparam[tgt].actv_flags;
4659 oval = sdp->isp_devparam[tgt].actv_offset;
4660 pval = sdp->isp_devparam[tgt].actv_period;
4662 dval = sdp->isp_devparam[tgt].nvrm_flags;
4663 oval = sdp->isp_devparam[tgt].nvrm_offset;
4664 pval = sdp->isp_devparam[tgt].nvrm_period;
4667 cts->protocol = PROTO_SCSI;
4668 cts->protocol_version = SCSI_REV_2;
4669 cts->transport = XPORT_SPI;
4670 cts->transport_version = 2;
4676 if (dval & DPARM_DISC) {
4677 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4679 if ((dval & DPARM_SYNC) && oval && pval) {
4680 spi->sync_offset = oval;
4681 spi->sync_period = pval;
4683 spi->sync_offset = 0;
4684 spi->sync_period = 0;
4686 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4687 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4688 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4689 if (dval & DPARM_WIDE) {
4690 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4692 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4694 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4695 scsi->valid = CTS_SCSI_VALID_TQ;
4696 if (dval & DPARM_TQING) {
4697 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4699 spi->valid |= CTS_SPI_VALID_DISC;
4701 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4702 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4704 ccb->ccb_h.status = CAM_REQ_CMP;
4708 case XPT_CALC_GEOMETRY:
4709 cam_calc_geometry(&ccb->ccg, 1);
4713 case XPT_RESET_BUS: /* Reset the specified bus */
4714 bus = cam_sim_bus(sim);
4715 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4717 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4722 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4725 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4727 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4729 ccb->ccb_h.status = CAM_REQ_CMP;
4733 case XPT_TERM_IO: /* Terminate the I/O process */
4734 ccb->ccb_h.status = CAM_REQ_INVALID;
4739 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
4741 struct ccb_sim_knob *kp = &ccb->knob;
4746 ccb->ccb_h.status = CAM_REQ_INVALID;
4751 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4752 fcp = FCPARAM(isp, bus);
4754 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4755 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4756 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4757 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4759 ccb->ccb_h.status = CAM_REQ_CMP;
4760 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4764 switch (kp->xport_specific.fc.role) {
4765 case KNOB_ROLE_NONE:
4766 if (fcp->role != ISP_ROLE_NONE) {
4768 newrole = ISP_ROLE_NONE;
4771 case KNOB_ROLE_TARGET:
4772 if (fcp->role != ISP_ROLE_TARGET) {
4774 newrole = ISP_ROLE_TARGET;
4777 case KNOB_ROLE_INITIATOR:
4778 if (fcp->role != ISP_ROLE_INITIATOR) {
4780 newrole = ISP_ROLE_INITIATOR;
4783 case KNOB_ROLE_BOTH:
4785 if (fcp->role != ISP_ROLE_BOTH) {
4787 newrole = ISP_ROLE_BOTH;
4791 * We don't really support dual role at present on FC cards.
4793 * We should, but a bunch of things are currently broken,
4794 * so don't allow it.
4796 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4797 ccb->ccb_h.status = CAM_REQ_INVALID;
4802 if (isp_fc_change_role(isp, bus, newrole) != 0) {
4803 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4804 #ifdef ISP_TARGET_MODE
4805 } else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4806 isp_enable_deferred_luns(isp, bus);
4814 case XPT_GET_SIM_KNOB: /* Set SIM knobs */
4816 struct ccb_sim_knob *kp = &ccb->knob;
4821 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4822 fcp = FCPARAM(isp, bus);
4824 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4825 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4826 switch (fcp->role) {
4828 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4830 case ISP_ROLE_TARGET:
4831 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4833 case ISP_ROLE_INITIATOR:
4834 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4837 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4840 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4841 ccb->ccb_h.status = CAM_REQ_CMP;
4843 ccb->ccb_h.status = CAM_REQ_INVALID;
4849 case XPT_PATH_INQ: /* Path routing inquiry */
4851 struct ccb_pathinq *cpi = &ccb->cpi;
4853 cpi->version_num = 1;
4854 #ifdef ISP_TARGET_MODE
4855 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4857 cpi->target_sprt = 0;
4859 cpi->hba_eng_cnt = 0;
4860 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4861 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4862 cpi->bus_id = cam_sim_bus(sim);
4863 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4865 fcparam *fcp = FCPARAM(isp, bus);
4867 cpi->hba_misc = PIM_NOBUSRESET;
4870 * Because our loop ID can shift from time to time,
4871 * make our initiator ID out of range of our bus.
4873 cpi->initiator_id = cpi->max_target + 1;
4876 * Set base transfer capabilities for Fibre Channel, for this HBA.
4879 cpi->base_transfer_speed = 8000000;
4880 } else if (IS_24XX(isp)) {
4881 cpi->base_transfer_speed = 4000000;
4882 } else if (IS_23XX(isp)) {
4883 cpi->base_transfer_speed = 2000000;
4885 cpi->base_transfer_speed = 1000000;
4887 cpi->hba_inquiry = PI_TAG_ABLE;
4888 cpi->transport = XPORT_FC;
4889 cpi->transport_version = 0;
4890 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4891 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4892 cpi->xport_specific.fc.port = fcp->isp_portid;
4893 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4895 sdparam *sdp = SDPARAM(isp, bus);
4896 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4898 cpi->initiator_id = sdp->isp_initiator_id;
4899 cpi->base_transfer_speed = 3300;
4900 cpi->transport = XPORT_SPI;
4901 cpi->transport_version = 2;
4903 cpi->protocol = PROTO_SCSI;
4904 cpi->protocol_version = SCSI_REV_2;
4905 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4906 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4907 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4908 cpi->unit_number = cam_sim_unit(sim);
4909 cpi->ccb_h.status = CAM_REQ_CMP;
4914 ccb->ccb_h.status = CAM_REQ_INVALID;
4920 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4923 isp_done(XS_T *sccb)
4925 ispsoftc_t *isp = XS_ISP(sccb);
4929 XS_SETERR(sccb, CAM_REQ_CMP);
4931 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4932 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4933 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4934 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4936 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4940 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4941 status = sccb->ccb_h.status & CAM_STATUS_MASK;
4942 if (status != CAM_REQ_CMP) {
4943 if (status != CAM_SEL_TIMEOUT)
4944 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);
4945 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4946 sccb->ccb_h.status |= CAM_DEV_QFRZN;
4947 xpt_freeze_devq(sccb->ccb_h.path, 1);
4951 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4952 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4955 XS_CMD_S_DONE(sccb);
4956 if (XS_TACTIVE_P(sccb))
4957 callout_stop(&PISP_PCMD(sccb)->wdog);
4958 XS_CMD_S_CLEAR(sccb);
4959 isp_free_pcmd(isp, (union ccb *) sccb);
4960 xpt_done((union ccb *) sccb);
4964 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4967 static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
4968 static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4973 struct cam_path *tmppath;
4977 case ISPASYNC_NEW_TGT_PARAMS:
4979 struct ccb_trans_settings_scsi *scsi;
4980 struct ccb_trans_settings_spi *spi;
4983 struct ccb_trans_settings cts;
4985 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4987 __va_start(ap, cmd);
4988 bus = __va_arg(ap, int);
4989 tgt = __va_arg(ap, int);
4991 sdp = SDPARAM(isp, bus);
4993 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4994 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4997 flags = sdp->isp_devparam[tgt].actv_flags;
4998 cts.type = CTS_TYPE_CURRENT_SETTINGS;
4999 cts.protocol = PROTO_SCSI;
5000 cts.transport = XPORT_SPI;
5002 scsi = &cts.proto_specific.scsi;
5003 spi = &cts.xport_specific.spi;
5005 if (flags & DPARM_TQING) {
5006 scsi->valid |= CTS_SCSI_VALID_TQ;
5007 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5010 if (flags & DPARM_DISC) {
5011 spi->valid |= CTS_SPI_VALID_DISC;
5012 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5014 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5015 if (flags & DPARM_WIDE) {
5016 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5018 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5020 if (flags & DPARM_SYNC) {
5021 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5022 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5023 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5024 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5026 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);
5027 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5028 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5029 xpt_free_path(tmppath);
5032 case ISPASYNC_BUS_RESET:
5034 __va_start(ap, cmd);
5035 bus = __va_arg(ap, int);
5037 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5039 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5041 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5047 msg = "LIP Received";
5050 case ISPASYNC_LOOP_RESET:
5055 case ISPASYNC_LOOP_DOWN:
5060 __va_start(ap, cmd);
5061 bus = __va_arg(ap, int);
5064 FCPARAM(isp, bus)->link_active = 0;
5066 fc = ISP_FC_PC(isp, bus);
5067 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5069 * We don't do any simq freezing if we are only in target mode
5071 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5073 isp_freeze_loopdown(isp, bus, msg);
5075 if (!callout_active(&fc->ldt)) {
5076 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5077 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_second);
5081 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5084 case ISPASYNC_LOOP_UP:
5085 __va_start(ap, cmd);
5086 bus = __va_arg(ap, int);
5088 fc = ISP_FC_PC(isp, bus);
5090 * Now we just note that Loop has come up. We don't
5091 * actually do anything because we're waiting for a
5092 * Change Notify before activating the FC cleanup
5093 * thread to look at the state of the loop again.
5095 FCPARAM(isp, bus)->link_active = 1;
5097 fc->loop_down_time = 0;
5098 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5100 case ISPASYNC_DEV_ARRIVED:
5101 __va_start(ap, cmd);
5102 bus = __va_arg(ap, int);
5103 lp = __va_arg(ap, fcportdb_t *);
5105 fc = ISP_FC_PC(isp, bus);
5108 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5109 int dbidx = lp - FCPARAM(isp, bus)->portdb;
5112 for (i = 0; i < MAX_FC_TARG; i++) {
5113 if (i >= FL_ID && i <= SNS_ID) {
5116 if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5120 if (i < MAX_FC_TARG) {
5121 FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5122 lp->dev_map_idx = i + 1;
5124 isp_prt(isp, ISP_LOGWARN, "out of target ids");
5125 isp_dump_portdb(isp, bus);
5128 if (lp->dev_map_idx) {
5129 tgt = lp->dev_map_idx - 1;
5130 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);
5131 isp_make_here(isp, bus, tgt);
5133 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);
5136 case ISPASYNC_DEV_CHANGED:
5137 __va_start(ap, cmd);
5138 bus = __va_arg(ap, int);
5139 lp = __va_arg(ap, fcportdb_t *);
5141 fc = ISP_FC_PC(isp, bus);
5144 if (isp_change_is_bad) {
5145 lp->state = FC_PORTDB_STATE_NIL;
5146 if (lp->dev_map_idx) {
5147 tgt = lp->dev_map_idx - 1;
5148 FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5149 lp->dev_map_idx = 0;
5150 isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5151 isp_make_gone(isp, bus, tgt);
5153 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5154 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5157 lp->portid = lp->new_portid;
5158 lp->roles = lp->new_roles;
5159 if (lp->dev_map_idx) {
5160 int t = lp->dev_map_idx - 1;
5161 FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5162 tgt = lp->dev_map_idx - 1;
5163 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5164 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5166 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);
5170 case ISPASYNC_DEV_STAYED:
5171 __va_start(ap, cmd);
5172 bus = __va_arg(ap, int);
5173 lp = __va_arg(ap, fcportdb_t *);
5175 if (lp->dev_map_idx) {
5176 tgt = lp->dev_map_idx - 1;
5177 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5178 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5180 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5181 (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5184 case ISPASYNC_DEV_GONE:
5185 __va_start(ap, cmd);
5186 bus = __va_arg(ap, int);
5187 lp = __va_arg(ap, fcportdb_t *);
5189 fc = ISP_FC_PC(isp, bus);
5191 * If this has a virtual target and we haven't marked it
5192 * that we're going to have isp_gdt tell the OS it's gone,
5193 * set the isp_gdt timer running on it.
5195 * If it isn't marked that isp_gdt is going to get rid of it,
5196 * announce that it's gone.
5199 if (lp->dev_map_idx && lp->reserved == 0) {
5201 lp->state = FC_PORTDB_STATE_ZOMBIE;
5202 lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5203 if (fc->ready && !callout_active(&fc->gdt)) {
5204 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_second);
5205 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5207 tgt = lp->dev_map_idx - 1;
5208 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);
5209 } else if (lp->reserved == 0) {
5210 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);
5213 case ISPASYNC_CHANGE_NOTIFY:
5216 int evt, nphdl, nlstate, reason;
5218 __va_start(ap, cmd);
5219 bus = __va_arg(ap, int);
5220 evt = __va_arg(ap, int);
5221 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5222 nphdl = __va_arg(ap, int);
5223 nlstate = __va_arg(ap, int);
5224 reason = __va_arg(ap, int);
5227 nlstate = reason = 0;
5230 fc = ISP_FC_PC(isp, bus);
5232 if (evt == ISPASYNC_CHANGE_PDB) {
5233 msg = "Chan %d Port Database Changed";
5234 } else if (evt == ISPASYNC_CHANGE_SNS) {
5235 msg = "Chan %d Name Server Database Changed";
5237 msg = "Chan %d Other Change Notify";
5241 * If the loop down timer is running, cancel it.
5243 if (fc->ready && callout_active(&fc->ldt)) {
5244 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_second);
5245 callout_stop(&fc->ldt);
5247 isp_prt(isp, ISP_LOGINFO, msg, bus);
5248 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5249 isp_freeze_loopdown(isp, bus, msg);
5254 #ifdef ISP_TARGET_MODE
5255 case ISPASYNC_TARGET_NOTIFY:
5257 isp_notify_t *notify;
5258 __va_start(ap, cmd);
5259 notify = __va_arg(ap, isp_notify_t *);
5261 switch (notify->nt_ncode) {
5263 case NT_ABORT_TASK_SET:
5265 case NT_CLEAR_TASK_SET:
5267 case NT_TARGET_RESET:
5269 * These are task management functions.
5271 isp_handle_platform_target_tmf(isp, notify);
5278 * No action need be taken here.
5282 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5286 * This is device arrival/departure notification
5288 isp_handle_platform_target_notify_ack(isp, notify);
5292 struct ac_contract ac;
5293 struct ac_device_changed *fc;
5295 ac.contract_number = AC_CONTRACT_DEV_CHG;
5296 fc = (struct ac_device_changed *) ac.contract_data;
5297 fc->wwpn = notify->nt_wwn;
5298 fc->port = notify->nt_sid;
5299 fc->target = notify->nt_nphdl;
5301 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5306 struct ac_contract ac;
5307 struct ac_device_changed *fc;
5309 ac.contract_number = AC_CONTRACT_DEV_CHG;
5310 fc = (struct ac_device_changed *) ac.contract_data;
5311 fc->wwpn = notify->nt_wwn;
5312 fc->port = notify->nt_sid;
5313 fc->target = notify->nt_nphdl;
5315 xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5319 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5320 isp_handle_platform_target_notify_ack(isp, notify);
5325 case ISPASYNC_TARGET_ACTION:
5329 __va_start(ap, cmd);
5330 hp = __va_arg(ap, isphdr_t *);
5332 switch (hp->rqs_entry_type) {
5334 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5336 case RQSTYPE_NOTIFY:
5338 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5339 } else if (IS_24XX(isp)) {
5340 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5342 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5347 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5349 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5353 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5359 isp_handle_platform_ctio(isp, hp);
5361 case RQSTYPE_ABTS_RCVD:
5363 abts_t *abts = (abts_t *)hp;
5364 isp_notify_t notify, *nt = ¬ify;
5370 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5371 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5372 ISP_MEMZERO(nt, sizeof (isp_notify_t));
5376 nt->nt_nphdl = abts->abts_nphdl;
5378 isp_find_chan_by_did(isp, did, &chan);
5379 if (chan == ISP_NOCHAN) {
5380 nt->nt_tgt = TGT_ANY;
5382 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5383 if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5384 nt->nt_wwn = lp->port_wwn;
5386 nt->nt_wwn = INI_ANY;
5390 * Try hard to find the lun for this command.
5392 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5394 nt->nt_lun = xpt_path_lun_id(tptr->owner);
5395 rls_lun_statep(isp, tptr);
5397 nt->nt_lun = LUN_ANY;
5399 nt->nt_need_ack = 1;
5400 nt->nt_tagval = abts->abts_rxid_task;
5401 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5402 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5403 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)",
5404 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5406 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)",
5407 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5409 nt->nt_channel = chan;
5410 nt->nt_ncode = NT_ABORT_TASK;
5411 nt->nt_lreserved = hp;
5412 isp_handle_platform_target_tmf(isp, nt);
5415 case RQSTYPE_ENABLE_LUN:
5416 case RQSTYPE_MODIFY_LUN:
5417 isp_ledone(isp, (lun_entry_t *) hp);
5423 case ISPASYNC_FW_CRASH:
5425 uint16_t mbox1, mbox6;
5426 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5427 if (IS_DUALBUS(isp)) {
5428 mbox6 = ISP_READ(isp, OUTMAILBOX6);
5432 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5433 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5434 isp->isp_osinfo.mbox_sleep_ok = 0;
5436 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5437 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5441 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5448 * Locks are held before coming here.
5451 isp_uninit(ispsoftc_t *isp)
5454 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5456 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5458 ISP_DISABLE_INTS(isp);
5462 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5463 * up from our platform default (defww{p|n}n) and morph them based upon
5466 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5467 * based upon channel.
5471 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5474 struct isp_fc *fc = ISP_FC_PC(isp, chan);
5477 * If we're asking for a active WWN, the default overrides get
5478 * returned, otherwise the NVRAM value is picked.
5480 * If we're asking for a default WWN, we just pick the default override.
5483 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5487 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5491 return (0x400000007F000009ull);
5493 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5498 * For channel zero just return what we have. For either ACTIVE or
5499 * DEFAULT cases, we depend on default override of NVRAM values for
5507 * For other channels, we are doing one of three things:
5509 * 1. If what we have now is non-zero, return it. Otherwise we morph
5510 * values from channel 0. 2. If we're here for a WWPN we synthesize
5511 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5512 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5519 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5521 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5524 if (((seed >> 60) & 0xf) == 2) {
5526 * The type 2 NAA fields for QLogic cards appear be laid out
5529 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5530 * port (1) or node (0) WWN distinguishor bit 48
5531 * physical port on dual-port chips (23XX/24XX)
5533 * This is somewhat nutty, particularly since bit 48 is
5534 * irrelevant as they assign separate serial numbers to
5535 * different physical ports anyway.
5537 * We'll stick our channel number plus one first into bits
5538 * 57..59 and thence into bits 52..55 which allows for 8 bits
5539 * of channel which is comfortably more than our maximum
5542 seed &= ~0x0FF0000000000000ULL;
5544 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5545 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5554 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5560 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5563 ksprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5565 __va_start(ap, fmt);
5566 kvsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
5568 kprintf("%s\n", lbuf);
5572 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5575 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5578 xpt_print_path(xs->ccb_h.path);
5579 __va_start(ap, fmt);
5586 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5589 struct timespec x = *b;
5591 elapsed = GET_NANOSEC(&x);
5598 isp_mbox_acquire(ispsoftc_t *isp)
5600 if (isp->isp_osinfo.mboxbsy) {
5603 isp->isp_osinfo.mboxcmd_done = 0;
5604 isp->isp_osinfo.mboxbsy = 1;
5610 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5612 unsigned int usecs = mbp->timeout;
5613 unsigned int max, olim, ilim;
5616 usecs = MBCMD_DEFAULT_TIMEOUT;
5618 max = isp->isp_mbxwrk0 + 1;
5620 if (isp->isp_osinfo.mbox_sleep_ok) {
5621 unsigned int ms = (usecs + 999) / 1000;
5623 isp->isp_osinfo.mbox_sleep_ok = 0;
5624 isp->isp_osinfo.mbox_sleeping = 1;
5625 for (olim = 0; olim < max; olim++) {
5626 lksleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, 0, "ispmbx_sleep", isp_mstohz(ms));
5627 if (isp->isp_osinfo.mboxcmd_done) {
5631 isp->isp_osinfo.mbox_sleep_ok = 1;
5632 isp->isp_osinfo.mbox_sleeping = 0;
5634 for (olim = 0; olim < max; olim++) {
5635 for (ilim = 0; ilim < usecs; ilim += 100) {
5637 uint16_t sema, mbox;
5638 if (isp->isp_osinfo.mboxcmd_done) {
5641 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5642 isp_intr(isp, isr, sema, mbox);
5643 if (isp->isp_osinfo.mboxcmd_done) {
5649 if (isp->isp_osinfo.mboxcmd_done) {
5654 if (isp->isp_osinfo.mboxcmd_done == 0) {
5655 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5656 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5657 mbp->param[0] = MBOX_TIMEOUT;
5658 isp->isp_osinfo.mboxcmd_done = 1;
5663 isp_mbox_notify_done(ispsoftc_t *isp)
5665 if (isp->isp_osinfo.mbox_sleeping) {
5666 wakeup(&isp->isp_mbxworkp);
5668 isp->isp_osinfo.mboxcmd_done = 1;
5672 isp_mbox_release(ispsoftc_t *isp)
5674 isp->isp_osinfo.mboxbsy = 0;
5678 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5681 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5684 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5694 t.tv_sec = ms / 1000;
5695 t.tv_usec = (ms % 1000) * 1000;
5696 hz = tvtohz_high(&t);
5707 isp_platform_intr(void *arg)
5709 ispsoftc_t *isp = arg;
5711 uint16_t sema, mbox;
5715 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5716 isp->isp_intbogus++;
5718 isp_intr(isp, isr, sema, mbox);
5724 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5726 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5727 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5729 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5731 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5735 isp_timer(void *arg)
5737 ispsoftc_t *isp = arg;
5740 #ifdef ISP_TARGET_MODE
5741 isp_tmcmd_restart(isp);
5744 callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);