2 * Copyright (c) 1997-2009 by Matthew Jacob
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <dev/isp/isp_freebsd.h>
34 #include <sys/unistd.h>
35 #include <sys/kthread.h>
37 #include <sys/module.h>
38 #include <sys/ioccom.h>
39 #include <dev/isp/isp_ioctl.h>
40 #include <sys/devicestat.h>
41 #include <cam/cam_periph.h>
42 #include <cam/cam_xpt_periph.h>
44 MODULE_VERSION(isp, 1);
45 MODULE_DEPEND(isp, cam, 1, 1, 1);
46 int isp_announced = 0;
47 int isp_loop_down_limit = 60; /* default loop down limit */
48 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
49 int isp_gone_device_time = 30; /* grace time before reporting device lost */
50 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
52 static void isp_freeze_loopdown(ispsoftc_t *, int);
53 static void isp_loop_changed(ispsoftc_t *isp, int chan);
54 static d_ioctl_t ispioctl;
55 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
56 static void isp_poll(struct cam_sim *);
57 static timeout_t isp_watchdog;
58 static timeout_t isp_gdt;
59 static task_fn_t isp_gdt_task;
60 static void isp_kthread(void *);
61 static void isp_action(struct cam_sim *, union ccb *);
62 static int isp_timer_count;
63 static void isp_timer(void *);
65 static struct cdevsw isp_cdevsw = {
66 .d_version = D_VERSION,
72 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
74 ispsoftc_t *isp = (ispsoftc_t *)arg1;
76 int error, old, value;
78 value = FCPARAM(isp, chan)->role;
80 error = sysctl_handle_int(oidp, &value, 0, req);
81 if ((error != 0) || (req->newptr == NULL))
84 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
88 old = FCPARAM(isp, chan)->role;
90 /* We don't allow target mode switch from here. */
91 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
93 /* If nothing has changed -- we are done. */
99 /* Actually change the role. */
100 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
106 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
108 struct ccb_setasync csa;
110 struct cam_path *path;
111 #ifdef ISP_TARGET_MODE
115 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
116 device_get_unit(isp->isp_dev), &isp->isp_lock,
117 isp->isp_maxcmds, isp->isp_maxcmds, devq);
122 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
124 cam_sim_free(sim, FALSE);
128 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
130 xpt_bus_deregister(cam_sim_path(sim));
132 cam_sim_free(sim, FALSE);
135 xpt_setup_ccb(&csa.ccb_h, path, 5);
136 csa.ccb_h.func_code = XPT_SASYNC_CB;
137 csa.event_enable = AC_LOST_DEVICE;
138 csa.callback = isp_cam_async;
139 csa.callback_arg = sim;
142 xpt_action((union ccb *)&csa);
146 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
149 #ifdef ISP_TARGET_MODE
150 TAILQ_INIT(&spi->waitq);
151 STAILQ_INIT(&spi->ntfree);
152 for (i = 0; i < ATPDPSIZE; i++)
153 STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
154 LIST_INIT(&spi->atfree);
155 for (i = ATPDPSIZE-1; i >= 0; i--)
156 LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
157 for (i = 0; i < ATPDPHASHSIZE; i++)
158 LIST_INIT(&spi->atused[i]);
161 fcparam *fcp = FCPARAM(isp, chan);
162 struct isp_fc *fc = ISP_FC_PC(isp, chan);
163 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
164 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
173 callout_init_mtx(&fc->gdt, &isp->isp_lock, 0);
174 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
175 #ifdef ISP_TARGET_MODE
176 TAILQ_INIT(&fc->waitq);
177 STAILQ_INIT(&fc->ntfree);
178 for (i = 0; i < ATPDPSIZE; i++)
179 STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
180 LIST_INIT(&fc->atfree);
181 for (i = ATPDPSIZE-1; i >= 0; i--)
182 LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
183 for (i = 0; i < ATPDPHASHSIZE; i++)
184 LIST_INIT(&fc->atused[i]);
186 isp_loop_changed(isp, chan);
188 if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
189 "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
190 xpt_free_path(fc->path);
192 xpt_bus_deregister(cam_sim_path(fc->sim));
194 cam_sim_free(fc->sim, FALSE);
197 fc->num_threads += 1;
199 snprintf(name, sizeof(name), "chan%d", chan);
200 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
201 OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
203 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
204 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
205 "World Wide Node Name");
206 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
207 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
208 "World Wide Port Name");
209 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
212 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
215 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
216 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
217 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
218 "Cause a Lost Frame on a Read");
220 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
221 "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
222 isp_role_sysctl, "I", "Current role");
223 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
224 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
225 "Connection speed in gigabits");
226 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
227 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
229 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
230 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
232 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
233 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
235 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236 "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
237 "Connection topology");
243 isp_detach_chan(ispsoftc_t *isp, int chan)
246 struct cam_path *path;
247 struct ccb_setasync csa;
250 ISP_GET_PC(isp, chan, sim, sim);
251 ISP_GET_PC(isp, chan, path, path);
252 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
254 xpt_setup_ccb(&csa.ccb_h, path, 5);
255 csa.ccb_h.func_code = XPT_SASYNC_CB;
256 csa.event_enable = 0;
257 csa.callback = isp_cam_async;
258 csa.callback_arg = sim;
259 xpt_action((union ccb *)&csa);
261 xpt_bus_deregister(cam_sim_path(sim));
262 cam_sim_free(sim, FALSE);
264 /* Wait for the channel's spawned threads to exit. */
265 wakeup(isp->isp_osinfo.pc.ptr);
266 while (*num_threads != 0)
267 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "isp_reap", 100);
271 isp_attach(ispsoftc_t *isp)
273 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
274 int du = device_get_unit(isp->isp_dev);
278 * Create the device queue for our SIM(s).
280 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
281 if (isp->isp_osinfo.devq == NULL) {
285 for (chan = 0; chan < isp->isp_nchan; chan++) {
286 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
291 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0);
292 isp_timer_count = hz >> 2;
293 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
295 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
296 if (isp->isp_osinfo.cdev) {
297 isp->isp_osinfo.cdev->si_drv1 = isp;
302 while (--chan >= 0) {
304 struct cam_path *path;
306 ISP_GET_PC(isp, chan, sim, sim);
307 ISP_GET_PC(isp, chan, path, path);
310 xpt_bus_deregister(cam_sim_path(sim));
312 cam_sim_free(sim, FALSE);
314 cam_simq_free(isp->isp_osinfo.devq);
315 isp->isp_osinfo.devq = NULL;
320 isp_detach(ispsoftc_t *isp)
324 if (isp->isp_osinfo.cdev) {
325 destroy_dev(isp->isp_osinfo.cdev);
326 isp->isp_osinfo.cdev = NULL;
329 /* Tell spawned threads that we're exiting. */
330 isp->isp_osinfo.is_exiting = 1;
331 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
332 isp_detach_chan(isp, chan);
334 callout_drain(&isp->isp_osinfo.tmo);
335 cam_simq_free(isp->isp_osinfo.devq);
340 isp_freeze_loopdown(ispsoftc_t *isp, int chan)
342 struct isp_fc *fc = ISP_FC_PC(isp, chan);
346 if (fc->simqfrozen == 0) {
347 isp_prt(isp, ISP_LOGDEBUG0,
348 "Chan %d Freeze simq (loopdown)", chan);
349 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
351 xpt_freeze_simq(fc->sim, 1);
353 isp_prt(isp, ISP_LOGDEBUG0,
354 "Chan %d Mark simq frozen (loopdown)", chan);
355 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
360 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
362 struct isp_fc *fc = ISP_FC_PC(isp, chan);
366 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
367 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
368 if (wasfrozen && fc->simqfrozen == 0) {
369 isp_prt(isp, ISP_LOGDEBUG0,
370 "Chan %d Release simq", chan);
371 xpt_release_simq(fc->sim, 1);
377 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
380 int nr, chan, retval = ENOTTY;
387 int olddblev = isp->isp_dblev;
388 isp->isp_dblev = *(int *)addr;
389 *(int *)addr = olddblev;
395 if (chan < 0 || chan >= isp->isp_nchan) {
400 *(int *)addr = FCPARAM(isp, chan)->role;
402 *(int *)addr = ISP_ROLE_INITIATOR;
411 if (chan < 0 || chan >= isp->isp_nchan) {
416 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
421 *(int *)addr = FCPARAM(isp, chan)->role;
422 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
437 if (chan < 0 || chan >= isp->isp_nchan) {
442 if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
454 if (chan < 0 || chan >= isp->isp_nchan) {
459 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
467 case ISP_FC_GETDINFO:
469 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
475 if (ifc->loopid >= MAX_FC_TARG) {
479 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
480 if (lp->state != FC_PORTDB_STATE_NIL) {
481 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
482 ifc->loopid = lp->handle;
483 ifc->portid = lp->portid;
484 ifc->node_wwn = lp->node_wwn;
485 ifc->port_wwn = lp->port_wwn;
492 case ISP_FC_GETHINFO:
494 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
495 int chan = hba->fc_channel;
497 if (chan < 0 || chan >= isp->isp_nchan) {
501 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
502 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
503 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
504 hba->fc_nchannels = isp->isp_nchan;
506 hba->fc_nports = MAX_FC_TARG;
507 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
508 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
509 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
510 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
511 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
512 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
513 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
515 hba->fc_nports = MAX_TARGETS;
517 hba->fc_topology = 0;
518 hba->nvram_node_wwn = 0ull;
519 hba->nvram_port_wwn = 0ull;
520 hba->active_node_wwn = 0ull;
521 hba->active_port_wwn = 0ull;
529 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
538 if (chan < 0 || chan >= isp->isp_nchan) {
543 needmarker = retval = 0;
548 uint8_t resp[QENTRY_LEN];
550 isp24xx_statusreq_t sp;
551 fcparam *fcp = FCPARAM(isp, chan);
555 for (i = 0; i < MAX_FC_TARG; i++) {
556 lp = &fcp->portdb[i];
557 if (lp->handle == nphdl) {
561 if (i == MAX_FC_TARG) {
566 ISP_MEMZERO(&tmf, sizeof(tmf));
567 tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
568 tmf.tmf_header.rqs_entry_count = 1;
569 tmf.tmf_nphdl = lp->handle;
572 tmf.tmf_tidlo = lp->portid;
573 tmf.tmf_tidhi = lp->portid >> 16;
574 tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
575 tmf.tmf_lun[1] = fct->lun & 0xff;
576 if (fct->lun >= 256) {
577 tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
579 switch (fct->action) {
581 tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
583 case IPT_TARGET_RESET:
584 tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
588 tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
591 case IPT_CLEAR_TASK_SET:
592 tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
595 case IPT_ABORT_TASK_SET:
596 tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
608 /* Prepare space for response in memory */
609 memset(resp, 0xff, sizeof(resp));
610 tmf.tmf_handle = isp_allocate_handle(isp, resp,
612 if (tmf.tmf_handle == 0) {
613 isp_prt(isp, ISP_LOGERR,
614 "%s: TMF of Chan %d out of handles",
621 /* Send request and wait for response. */
622 reqp = isp_getrqentry(isp);
624 isp_prt(isp, ISP_LOGERR,
625 "%s: TMF of Chan %d out of rqent",
627 isp_destroy_handle(isp, tmf.tmf_handle);
632 isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
633 if (isp->isp_dblev & ISP_LOGDEBUG1)
634 isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
635 ISP_SYNC_REQUEST(isp);
636 if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
637 isp_prt(isp, ISP_LOGERR,
638 "%s: TMF of Chan %d timed out",
640 isp_destroy_handle(isp, tmf.tmf_handle);
645 if (isp->isp_dblev & ISP_LOGDEBUG1)
646 isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
647 isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
649 if (sp.req_completion_status != 0)
654 MBSINIT(&mbs, 0, MBLOGALL, 0);
655 if (ISP_CAP_2KLOGIN(isp) == 0) {
658 switch (fct->action) {
660 mbs.param[0] = MBOX_CLEAR_ACA;
661 mbs.param[1] = nphdl;
662 mbs.param[2] = fct->lun;
664 case IPT_TARGET_RESET:
665 mbs.param[0] = MBOX_TARGET_RESET;
666 mbs.param[1] = nphdl;
670 mbs.param[0] = MBOX_LUN_RESET;
671 mbs.param[1] = nphdl;
672 mbs.param[2] = fct->lun;
675 case IPT_CLEAR_TASK_SET:
676 mbs.param[0] = MBOX_CLEAR_TASK_SET;
677 mbs.param[1] = nphdl;
678 mbs.param[2] = fct->lun;
681 case IPT_ABORT_TASK_SET:
682 mbs.param[0] = MBOX_ABORT_TASK_SET;
683 mbs.param[1] = nphdl;
684 mbs.param[2] = fct->lun;
693 FCPARAM(isp, chan)->sendmarker = 1;
695 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
714 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
715 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
717 static ISP_INLINE int
718 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
720 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
721 if (ISP_PCMD(ccb) == NULL) {
724 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
728 static ISP_INLINE void
729 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
732 #ifdef ISP_TARGET_MODE
733 PISP_PCMD(ccb)->datalen = 0;
734 PISP_PCMD(ccb)->totslen = 0;
735 PISP_PCMD(ccb)->cumslen = 0;
736 PISP_PCMD(ccb)->crn = 0;
738 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
739 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
740 ISP_PCMD(ccb) = NULL;
745 * Put the target mode functions here, because some are inlines
747 #ifdef ISP_TARGET_MODE
748 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
749 static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
750 static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
751 static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
752 static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
753 static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
754 static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
755 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
756 static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
757 static void isp_enable_lun(ispsoftc_t *, union ccb *);
758 static void isp_disable_lun(ispsoftc_t *, union ccb *);
759 static timeout_t isp_refire_putback_atio;
760 static timeout_t isp_refire_notify_ack;
761 static void isp_complete_ctio(union ccb *);
762 static void isp_target_putback_atio(union ccb *);
763 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
764 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
765 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
766 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
767 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
768 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
769 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
770 static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
772 static ISP_INLINE tstate_t *
773 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
775 tstate_t *tptr = NULL;
778 if (bus < isp->isp_nchan) {
779 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
780 SLIST_FOREACH(tptr, lhp, next) {
781 if (tptr->ts_lun == lun)
789 isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
791 inot_private_data_t *ntp;
794 if (STAILQ_EMPTY(&tptr->restart_queue))
797 STAILQ_CONCAT(&rq, &tptr->restart_queue);
798 while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
799 STAILQ_REMOVE_HEAD(&rq, next);
801 isp_prt(isp, ISP_LOGTDEBUG0,
802 "%s: restarting resrc deprived %x", __func__,
803 ((at7_entry_t *)ntp->data)->at_rxid);
804 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
806 isp_prt(isp, ISP_LOGTDEBUG0,
807 "%s: restarting resrc deprived %x", __func__,
808 ((at2_entry_t *)ntp->data)->at_rxid);
809 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
811 isp_put_ntpd(isp, bus, ntp);
812 if (!STAILQ_EMPTY(&tptr->restart_queue))
815 if (!STAILQ_EMPTY(&rq)) {
816 STAILQ_CONCAT(&rq, &tptr->restart_queue);
817 STAILQ_CONCAT(&tptr->restart_queue, &rq);
819 return (!STAILQ_EMPTY(&tptr->restart_queue));
823 isp_tmcmd_restart(ispsoftc_t *isp)
828 struct isp_ccbq *waitq;
831 for (bus = 0; bus < isp->isp_nchan; bus++) {
832 for (i = 0; i < LUN_HASH_SIZE; i++) {
833 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
834 SLIST_FOREACH(tptr, lhp, next)
835 isp_atio_restart(isp, bus, tptr);
839 * We only need to do this once per channel.
841 ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
842 ccb = (union ccb *)TAILQ_FIRST(waitq);
844 TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
845 isp_target_start_ctio(isp, ccb, FROM_TIMER);
850 static atio_private_data_t *
851 isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
853 struct atpdlist *atfree;
854 struct atpdlist *atused;
855 atio_private_data_t *atp;
857 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
858 atp = LIST_FIRST(atfree);
860 LIST_REMOVE(atp, next);
862 ISP_GET_PC(isp, chan, atused, atused);
863 LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
868 static atio_private_data_t *
869 isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
871 struct atpdlist *atused;
872 atio_private_data_t *atp;
874 ISP_GET_PC(isp, chan, atused, atused);
875 LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
883 isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
885 struct atpdlist *atfree;
888 isp_put_ecmd(isp, atp->ests);
890 LIST_REMOVE(atp, next);
891 memset(atp, 0, sizeof (*atp));
892 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
893 LIST_INSERT_HEAD(atfree, atp, next);
897 isp_dump_atpd(ispsoftc_t *isp, int chan)
899 atio_private_data_t *atp, *atpool;
900 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
902 ISP_GET_PC(isp, chan, atpool, atpool);
903 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
904 if (atp->state == ATPD_STATE_FREE)
906 isp_prt(isp, ISP_LOGALL, "Chan %d ATP [0x%x] origdlen %u bytes_xfrd %u lun %jx nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s",
907 chan, atp->tag, atp->orig_datalen, atp->bytes_xfered, (uintmax_t)atp->lun, atp->nphdl, atp->sid, atp->did, atp->oxid, states[atp->state & 0x7]);
911 static inot_private_data_t *
912 isp_get_ntpd(ispsoftc_t *isp, int chan)
914 struct ntpdlist *ntfree;
915 inot_private_data_t *ntp;
917 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
918 ntp = STAILQ_FIRST(ntfree);
920 STAILQ_REMOVE_HEAD(ntfree, next);
924 static inot_private_data_t *
925 isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
927 inot_private_data_t *ntp, *ntp2;
929 ISP_GET_PC(isp, chan, ntpool, ntp);
930 ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
931 for (; ntp < ntp2; ntp++) {
932 if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
939 isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
941 struct ntpdlist *ntfree;
943 ntp->tag_id = ntp->seq_id = 0;
944 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
945 STAILQ_INSERT_HEAD(ntfree, ntp, next);
949 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
955 lun = xpt_path_lun_id(path);
956 if (lun != CAM_LUN_WILDCARD) {
957 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
958 return (CAM_LUN_INVALID);
961 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
963 return (CAM_RESRC_UNAVAIL);
966 SLIST_INIT(&tptr->atios);
967 SLIST_INIT(&tptr->inots);
968 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
969 SLIST_INSERT_HEAD(lhp, tptr, next);
971 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
972 return (CAM_REQ_CMP);
976 destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
980 inot_private_data_t *ntp;
982 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
983 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
984 ccb->ccb_h.status = CAM_REQ_ABORTED;
987 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
988 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
989 ccb->ccb_h.status = CAM_REQ_ABORTED;
992 while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
993 isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
994 STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
995 isp_put_ntpd(isp, bus, ntp);
997 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
998 SLIST_REMOVE(lhp, tptr, tstate, next);
999 free(tptr, M_DEVBUF);
1003 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1010 if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
1011 xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
1012 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1018 * We only support either target and lun both wildcard
1019 * or target and lun both non-wildcard.
1021 bus = XS_CHANNEL(ccb);
1022 target = ccb->ccb_h.target_id;
1023 lun = ccb->ccb_h.target_lun;
1024 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1025 "enabling lun %jx\n", (uintmax_t)lun);
1026 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1027 ccb->ccb_h.status = CAM_LUN_INVALID;
1032 /* Create the state pointer. It should not already exist. */
1033 tptr = get_lun_statep(isp, bus, lun);
1035 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1039 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1040 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1045 ccb->ccb_h.status = CAM_REQ_CMP;
1050 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1052 tstate_t *tptr = NULL;
1057 bus = XS_CHANNEL(ccb);
1058 target = ccb->ccb_h.target_id;
1059 lun = ccb->ccb_h.target_lun;
1060 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1061 "disabling lun %jx\n", (uintmax_t)lun);
1062 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1063 ccb->ccb_h.status = CAM_LUN_INVALID;
1068 /* Find the state pointer. */
1069 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1070 ccb->ccb_h.status = CAM_PATH_INVALID;
1075 destroy_lun_state(isp, bus, tptr);
1076 ccb->ccb_h.status = CAM_REQ_CMP;
1081 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1083 int fctape, sendstatus, resid;
1085 atio_private_data_t *atp;
1086 struct ccb_scsiio *cso;
1087 struct isp_ccbq *waitq;
1088 uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1089 uint8_t local[QENTRY_LEN];
1091 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
1092 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1094 ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
1098 * Insert at the tail of the list, if any, waiting CTIO CCBs
1100 TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
1104 case FROM_CTIO_DONE:
1105 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1109 while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1110 TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
1113 xfrlen = cso->dxfer_len;
1115 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1116 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1117 ccb->ccb_h.status = CAM_REQ_INVALID;
1123 atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
1125 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1126 isp_dump_atpd(isp, XS_CHANNEL(ccb));
1127 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1133 * Is this command a dead duck?
1136 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1137 ccb->ccb_h.status = CAM_REQ_ABORTED;
1143 * Check to make sure we're still in target mode.
1145 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1146 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1147 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1148 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1154 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1155 * could be split into two CTIOs to split data and status).
1157 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1158 isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1159 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1164 * Does the initiator expect FC-Tape style responses?
1166 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1173 * If we already did the data xfer portion of a CTIO that sends data
1174 * and status, don't do it again and do the status portion now.
1177 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1178 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1179 xfrlen = 0; /* we already did the data transfer */
1182 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1188 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1189 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1191 * Sense length is not the entire sense data structure size. Periph
1192 * drivers don't seem to be setting sense_len to reflect the actual
1193 * size. We'll peek inside to get the right amount.
1195 sense_length = cso->sense_len;
1198 * This 'cannot' happen
1200 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1201 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1207 memset(local, 0, QENTRY_LEN);
1210 * Check for overflow
1212 tmp = atp->bytes_xfered + atp->bytes_in_transit;
1213 if (xfrlen > 0 && tmp > atp->orig_datalen) {
1214 isp_prt(isp, ISP_LOGERR,
1215 "%s: [0x%x] data overflow by %u bytes", __func__,
1216 cso->tag_id, tmp + xfrlen - atp->orig_datalen);
1217 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1221 if (xfrlen > atp->orig_datalen - tmp) {
1222 xfrlen = atp->orig_datalen - tmp;
1223 if (xfrlen == 0 && !sendstatus) {
1224 cso->resid = cso->dxfer_len;
1225 ccb->ccb_h.status = CAM_REQ_CMP;
1232 ct7_entry_t *cto = (ct7_entry_t *) local;
1234 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1235 cto->ct_header.rqs_entry_count = 1;
1236 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1237 ATPD_SET_SEQNO(cto, atp);
1238 cto->ct_nphdl = atp->nphdl;
1239 cto->ct_rxid = atp->tag;
1240 cto->ct_iid_lo = atp->sid;
1241 cto->ct_iid_hi = atp->sid >> 16;
1242 cto->ct_oxid = atp->oxid;
1243 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1244 cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1245 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1248 * Mode 1, status, no data. Only possible when we are sending status, have
1249 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1251 * Mode 2, status, no data. We have to use this in the case that
1252 * the sense data won't fit into a ct7_entry_t.
1255 if (sendstatus && xfrlen == 0) {
1256 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1257 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1258 if (sense_length <= MAXRESPLEN_24XX) {
1259 cto->ct_flags |= CT7_FLAG_MODE1;
1260 cto->ct_scsi_status = cso->scsi_status;
1262 cto->ct_resid = -resid;
1263 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1264 } else if (resid > 0) {
1265 cto->ct_resid = resid;
1266 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1269 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1272 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1273 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1274 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1278 char buf[XCMD_SIZE];
1281 if (atp->ests == NULL) {
1282 atp->ests = isp_get_ecmd(isp);
1283 if (atp->ests == NULL) {
1284 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1288 memset(buf, 0, sizeof (buf));
1289 rp = (fcp_rsp_iu_t *)buf;
1291 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1292 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1294 cto->ct_flags |= CT7_FLAG_MODE2;
1295 rp->fcp_rsp_scsi_status = cso->scsi_status;
1297 rp->fcp_rsp_resid = -resid;
1298 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1299 } else if (resid > 0) {
1300 rp->fcp_rsp_resid = resid;
1301 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1304 rp->fcp_rsp_snslen = sense_length;
1305 cto->ct_senselen = sense_length;
1306 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1307 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1308 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1310 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1312 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1313 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1315 addr = isp->isp_osinfo.ecmd_dma;
1316 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1317 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1318 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1319 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1320 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1321 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1322 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1325 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
1326 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
1327 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1329 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1330 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1332 atp->state = ATPD_STATE_LAST_CTIO;
1336 * Mode 0 data transfers, *possibly* with status.
1339 cto->ct_flags |= CT7_FLAG_MODE0;
1340 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1341 cto->ct_flags |= CT7_DATA_IN;
1343 cto->ct_flags |= CT7_DATA_OUT;
1346 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1347 cto->rsp.m0.ct_xfrlen = xfrlen;
1350 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1351 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1352 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1353 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1357 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1358 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1359 cto->ct_flags |= CT7_SENDSTATUS;
1360 atp->state = ATPD_STATE_LAST_CTIO;
1362 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1365 atp->sendst = 1; /* send status later */
1366 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1367 atp->state = ATPD_STATE_CTIO;
1370 atp->state = ATPD_STATE_CTIO;
1372 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
1373 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1376 ct2_entry_t *cto = (ct2_entry_t *) local;
1378 if (isp->isp_osinfo.sixtyfourbit)
1379 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1381 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1382 cto->ct_header.rqs_entry_count = 1;
1383 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1384 ATPD_SET_SEQNO(cto, atp);
1385 if (ISP_CAP_2KLOGIN(isp)) {
1386 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1388 cto->ct_iid = atp->nphdl;
1389 if (ISP_CAP_SCCFW(isp) == 0) {
1390 cto->ct_lun = ccb->ccb_h.target_lun;
1393 cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1394 cto->ct_rxid = cso->tag_id;
1397 * Mode 1, status, no data. Only possible when we are sending status, have
1398 * no data to transfer, and the sense length can fit in the ct7_entry.
1400 * Mode 2, status, no data. We have to use this in the case the response
1401 * length won't fit into a ct2_entry_t.
1403 * We'll fill out this structure with information as if this were a
1404 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1405 * needed based upon this.
1407 if (sendstatus && xfrlen == 0) {
1408 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1409 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1410 if (sense_length <= MAXRESPLEN) {
1412 cto->ct_resid = -resid;
1413 } else if (resid > 0) {
1414 cto->ct_resid = resid;
1416 cto->ct_flags |= CT2_FLAG_MODE1;
1417 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1419 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1420 } else if (resid > 0) {
1421 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1424 cto->ct_flags |= CT2_CONFIRM;
1427 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1428 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1429 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1433 char buf[XCMD_SIZE];
1436 if (atp->ests == NULL) {
1437 atp->ests = isp_get_ecmd(isp);
1438 if (atp->ests == NULL) {
1439 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1443 memset(buf, 0, sizeof (buf));
1444 rp = (fcp_rsp_iu_t *)buf;
1446 cto->ct_flags |= CT2_CONFIRM;
1447 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1449 cto->ct_flags |= CT2_FLAG_MODE2;
1450 rp->fcp_rsp_scsi_status = cso->scsi_status;
1452 rp->fcp_rsp_resid = -resid;
1453 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1454 } else if (resid > 0) {
1455 rp->fcp_rsp_resid = resid;
1456 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1459 rp->fcp_rsp_snslen = sense_length;
1460 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1461 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1462 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1464 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1466 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1467 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1469 addr = isp->isp_osinfo.ecmd_dma;
1470 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1471 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1472 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1473 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1474 if (cto->ct_header.rqs_entry_type == RQSTYPE_CTIO3) {
1475 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1476 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1477 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1479 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1480 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1484 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
1485 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
1486 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1488 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
1489 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1491 atp->state = ATPD_STATE_LAST_CTIO;
1495 cto->ct_flags |= CT2_FLAG_MODE0;
1496 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1497 cto->ct_flags |= CT2_DATA_IN;
1499 cto->ct_flags |= CT2_DATA_OUT;
1502 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1503 cto->rsp.m0.ct_xfrlen = xfrlen;
1506 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1507 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1508 cto->ct_flags |= CT2_SENDSTATUS;
1509 atp->state = ATPD_STATE_LAST_CTIO;
1511 cto->ct_flags |= CT2_CONFIRM;
1514 atp->sendst = 1; /* send status later */
1515 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1516 atp->state = ATPD_STATE_CTIO;
1519 atp->state = ATPD_STATE_CTIO;
1522 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
1523 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1526 if (isp_get_pcmd(isp, ccb)) {
1527 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1528 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1531 handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1533 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1534 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1535 isp_free_pcmd(isp, ccb);
1538 atp->bytes_in_transit += xfrlen;
1539 PISP_PCMD(ccb)->datalen = xfrlen;
1543 * Call the dma setup routines for this entry (and any subsequent
1544 * CTIOs) if there's data to move, and then tell the f/w it's got
1545 * new things to play with. As with isp_start's usage of DMA setup,
1546 * any swizzling is done in the machine dependent layer. Because
1547 * of this, we put the request onto the queue area first in native
1552 ct7_entry_t *cto = (ct7_entry_t *) local;
1553 cto->ct_syshandle = handle;
1555 ct2_entry_t *cto = (ct2_entry_t *) local;
1556 cto->ct_syshandle = handle;
1559 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1560 if (dmaresult != CMD_QUEUED) {
1561 isp_destroy_handle(isp, handle);
1562 isp_free_pcmd(isp, ccb);
1563 if (dmaresult == CMD_EAGAIN) {
1564 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1567 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1571 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1573 ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1575 ccb->ccb_h.spriv_field0 = ~0;
1583 isp_refire_putback_atio(void *arg)
1585 union ccb *ccb = arg;
1587 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
1588 isp_target_putback_atio(ccb);
1592 isp_refire_notify_ack(void *arg)
1594 isp_tna_t *tp = arg;
1595 ispsoftc_t *isp = tp->isp;
1597 ISP_ASSERT_LOCKED(isp);
1598 if (isp_notify_ack(isp, tp->not)) {
1599 callout_schedule(&tp->timer, 5);
1607 isp_target_putback_atio(union ccb *ccb)
1609 ispsoftc_t *isp = XS_ISP(ccb);
1610 struct ccb_scsiio *cso = &ccb->csio;
1611 at2_entry_t local, *at = &local;
1613 ISP_MEMZERO(at, sizeof (at2_entry_t));
1614 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1615 at->at_header.rqs_entry_count = 1;
1616 if (ISP_CAP_SCCFW(isp)) {
1617 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1619 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1621 at->at_status = CT_OK;
1622 at->at_rxid = cso->tag_id;
1623 at->at_iid = cso->ccb_h.target_id;
1624 if (isp_target_put_entry(isp, at)) {
1625 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1626 isp_refire_putback_atio, ccb);
1628 isp_complete_ctio(ccb);
1632 isp_complete_ctio(union ccb *ccb)
1634 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1635 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1641 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1647 struct ccb_accept_tio *atiop;
1649 atio_private_data_t *atp;
1650 inot_private_data_t *ntp;
1653 * The firmware status (except for the QLTM_SVALID bit)
1654 * indicates why this ATIO was sent to us.
1656 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1658 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1659 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1660 isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
1664 fcp = FCPARAM(isp, 0);
1665 if (ISP_CAP_SCCFW(isp)) {
1666 lun = aep->at_scclun;
1670 if (ISP_CAP_2KLOGIN(isp)) {
1671 nphdl = ((at2e_entry_t *)aep)->at_iid;
1673 nphdl = aep->at_iid;
1675 tptr = get_lun_statep(isp, 0, lun);
1677 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1679 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1681 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1683 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1690 * Start any commands pending resources first.
1692 if (isp_atio_restart(isp, 0, tptr))
1695 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1696 if (atiop == NULL) {
1700 atp = isp_get_atpd(isp, 0, aep->at_rxid);
1705 atp->state = ATPD_STATE_ATIO;
1706 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1707 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1708 atiop->ccb_h.target_id = fcp->isp_loopid;
1709 atiop->ccb_h.target_lun = lun;
1712 * We don't get 'suggested' sense data as we do with SCSI cards.
1714 atiop->sense_len = 0;
1717 * If we're not in the port database, add ourselves.
1720 atiop->init_id = nphdl;
1722 if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
1723 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1725 isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB",
1727 isp_dump_portdb(isp, 0);
1728 isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0);
1732 atiop->cdb_len = ATIO2_CDBLEN;
1733 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1734 atiop->ccb_h.status = CAM_CDB_RECVD;
1735 atiop->tag_id = atp->tag;
1736 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1737 case ATIO2_TC_ATTR_SIMPLEQ:
1738 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1739 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1741 case ATIO2_TC_ATTR_HEADOFQ:
1742 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1743 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1745 case ATIO2_TC_ATTR_ORDERED:
1746 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1747 atiop->tag_action = MSG_ORDERED_Q_TAG;
1749 case ATIO2_TC_ATTR_ACAQ: /* ?? */
1750 case ATIO2_TC_ATTR_UNTAGGED:
1752 atiop->tag_action = 0;
1756 atp->orig_datalen = aep->at_datalen;
1757 atp->bytes_xfered = 0;
1760 atp->sid = PORT_ANY;
1761 atp->oxid = aep->at_oxid;
1762 atp->cdb0 = aep->at_cdb[0];
1763 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1764 atp->state = ATPD_STATE_CAM;
1765 xpt_done((union ccb *)atiop);
1766 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %jx datalen %u", aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
1769 ntp = isp_get_ntpd(isp, 0);
1771 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1774 memcpy(ntp->data, aep, QENTRY_LEN);
1775 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1779 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
1783 uint16_t chan, nphdl = NIL_HANDLE;
1787 struct ccb_accept_tio *atiop;
1788 atio_private_data_t *atp = NULL;
1789 atio_private_data_t *oatp;
1790 inot_private_data_t *ntp;
1792 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
1793 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
1794 lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
1796 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
1797 /* Channel has to be derived from D_ID */
1798 isp_find_chan_by_did(isp, did, &chan);
1799 if (chan == ISP_NOCHAN) {
1800 isp_prt(isp, ISP_LOGWARN,
1801 "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
1802 __func__, aep->at_rxid, did);
1803 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
1812 * Find the PDB entry for this initiator
1814 if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
1816 * If we're not in the port database terminate the exchange.
1818 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",
1819 __func__, aep->at_rxid, did, chan, sid);
1820 isp_dump_portdb(isp, chan);
1821 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
1827 * Get the tstate pointer
1829 tptr = get_lun_statep(isp, chan, lun);
1831 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
1833 isp_prt(isp, ISP_LOGWARN,
1834 "%s: [0x%x] no state pointer for lun %jx or wildcard",
1835 __func__, aep->at_rxid, (uintmax_t)lun);
1837 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1839 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1846 * Start any commands pending resources first.
1848 if (isp_atio_restart(isp, chan, tptr))
1852 * If the f/w is out of resources, just send a BUSY status back.
1854 if (aep->at_rxid == AT7_NORESRC_RXID) {
1855 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
1860 * If we're out of resources, just send a BUSY status back.
1862 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1863 if (atiop == NULL) {
1864 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
1868 oatp = isp_find_atpd(isp, chan, aep->at_rxid);
1870 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
1871 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
1873 * It's not a "no resource" condition- but we can treat it like one
1877 atp = isp_get_atpd(isp, chan, aep->at_rxid);
1879 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1882 atp->word3 = lp->prli_word3;
1883 atp->state = ATPD_STATE_ATIO;
1884 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1885 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1886 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
1887 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
1888 atiop->ccb_h.target_lun = lun;
1889 atiop->sense_len = 0;
1890 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
1892 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
1894 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
1895 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
1896 atiop->cdb_len = cdbxlen;
1897 atiop->ccb_h.status = CAM_CDB_RECVD;
1898 atiop->tag_id = atp->tag;
1899 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
1900 case FCP_CMND_TASK_ATTR_SIMPLE:
1901 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1902 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1904 case FCP_CMND_TASK_ATTR_HEAD:
1905 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1906 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1908 case FCP_CMND_TASK_ATTR_ORDERED:
1909 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1910 atiop->tag_action = MSG_ORDERED_Q_TAG;
1914 case FCP_CMND_TASK_ATTR_ACA:
1915 case FCP_CMND_TASK_ATTR_UNTAGGED:
1916 atiop->tag_action = 0;
1919 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
1920 atp->bytes_xfered = 0;
1925 atp->oxid = aep->at_hdr.ox_id;
1926 atp->rxid = aep->at_hdr.rx_id;
1927 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1928 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
1929 atp->state = ATPD_STATE_CAM;
1930 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
1931 aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
1932 xpt_done((union ccb *)atiop);
1936 isp_put_atpd(isp, chan, atp);
1937 ntp = isp_get_ntpd(isp, chan);
1939 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1942 memcpy(ntp->data, aep, QENTRY_LEN);
1943 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1948 * Handle starting an SRR (sequence retransmit request)
1949 * We get here when we've gotten the immediate notify
1950 * and the return of all outstanding CTIOs for this
1954 isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1956 in_fcentry_24xx_t *inot;
1957 uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1960 inot = (in_fcentry_24xx_t *)atp->srr;
1961 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1963 atp->srr_ccb = NULL;
1966 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1970 ccb_off = ccb->ccb_h.spriv_field0;
1971 ccb_len = ccb->csio.dxfer_len;
1972 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1974 switch (inot->in_srr_iu) {
1975 case R_CTL_INFO_SOLICITED_DATA:
1977 * We have to restart a FCP_DATA data out transaction
1980 atp->bytes_xfered = srr_off;
1982 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1985 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
1986 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
1989 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
1991 case R_CTL_INFO_COMMAND_STATUS:
1992 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1995 * We have to restart a FCP_RSP IU transaction
1998 case R_CTL_INFO_DATA_DESCRIPTOR:
2000 * We have to restart an FCP DATA in transaction
2002 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2006 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2011 * We can't do anything until this is acked, so we might as well start it now.
2012 * We aren't going to do the usual asynchronous ack issue because we need
2013 * to make sure this gets on the wire first.
2015 if (isp_notify_ack(isp, inot)) {
2016 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2019 isp_target_start_ctio(isp, ccb, FROM_SRR);
2022 inot->in_reserved = 1;
2023 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2024 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2025 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2026 isp_complete_ctio(ccb);
2029 if (isp_notify_ack(isp, inot)) {
2030 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2033 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2034 ccb->ccb_h.status = CAM_MESSAGE_RECV;
2036 * This is not a strict interpretation of MDP, but it's close
2038 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2039 ccb->csio.msg_len = 7;
2040 ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2041 ccb->csio.msg_ptr[1] = 5;
2042 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */
2043 ccb->csio.msg_ptr[3] = srr_off >> 24;
2044 ccb->csio.msg_ptr[4] = srr_off >> 16;
2045 ccb->csio.msg_ptr[5] = srr_off >> 8;
2046 ccb->csio.msg_ptr[6] = srr_off;
2047 isp_complete_ctio(ccb);
2052 isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
2054 in_fcentry_24xx_t *inot = notify->nt_lreserved;
2055 atio_private_data_t *atp;
2056 uint32_t tag = notify->nt_tagval & 0xffffffff;
2058 atp = isp_find_atpd(isp, notify->nt_channel, tag);
2060 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
2062 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2065 atp->srr_notify_rcvd = 1;
2066 memcpy(atp->srr, inot, sizeof (atp->srr));
2067 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
2068 inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2069 ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
2071 isp_handle_srr_start(isp, atp);
2075 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2078 int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
2079 atio_private_data_t *atp = NULL;
2081 uint32_t handle, data_requested, resid;
2083 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2084 ccb = isp_find_xs(isp, handle);
2086 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2089 isp_destroy_handle(isp, handle);
2090 resid = data_requested = PISP_PCMD(ccb)->datalen;
2091 isp_free_pcmd(isp, ccb);
2093 bus = XS_CHANNEL(ccb);
2095 atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2097 atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2101 * XXX: isp_clear_commands() generates fake CTIO with zero
2102 * ct_rxid value, filling only ct_syshandle. Workaround
2103 * that using tag_id from the CCB, pointed by ct_syshandle.
2105 atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2108 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2111 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2112 atp->bytes_in_transit -= data_requested;
2114 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2117 ct7_entry_t *ct = arg;
2119 if (ct->ct_nphdl == CT7_SRR) {
2121 if (atp->srr_notify_rcvd)
2122 isp_handle_srr_start(isp, atp);
2125 if (ct->ct_nphdl == CT_HBA_RESET) {
2126 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2128 failure = CAM_UNREC_HBA_ERROR;
2130 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2131 ok = (ct->ct_nphdl == CT7_OK);
2132 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2133 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
2134 resid = ct->ct_resid;
2136 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2137 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2139 ct2_entry_t *ct = arg;
2140 if (ct->ct_status == CT_SRR) {
2142 if (atp->srr_notify_rcvd)
2143 isp_handle_srr_start(isp, atp);
2144 isp_target_putback_atio(ccb);
2147 if (ct->ct_status == CT_HBA_RESET) {
2148 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2150 failure = CAM_UNREC_HBA_ERROR;
2152 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2153 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2154 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2155 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2156 resid = ct->ct_resid;
2158 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2159 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2162 if (data_requested > 0) {
2163 atp->bytes_xfered += data_requested - resid;
2164 ccb->csio.resid = ccb->csio.dxfer_len -
2165 (data_requested - resid);
2167 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2168 ccb->ccb_h.status |= CAM_SENT_SENSE;
2169 ccb->ccb_h.status |= CAM_REQ_CMP;
2172 if (failure == CAM_UNREC_HBA_ERROR)
2173 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2175 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2177 atp->state = ATPD_STATE_PDON;
2180 * We never *not* notify CAM when there has been any error (ok == 0),
2181 * so we never need to do an ATIO putback if we're not notifying CAM.
2183 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
2184 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2185 if (notify_cam == 0) {
2187 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2193 * We are done with this ATIO if we successfully sent status.
2194 * In all other cases expect either another CTIO or XPT_ABORT.
2196 if (ok && sentstatus)
2197 isp_put_atpd(isp, bus, atp);
2200 * We're telling CAM we're done with this CTIO transaction.
2202 * 24XX cards never need an ATIO put back.
2204 * Other cards need one put back only on error.
2205 * In the latter case, a timeout will re-fire
2206 * and try again in case we didn't have
2207 * queue resources to do so at first. In any case,
2208 * once the putback is done we do the completion
2211 if (ok || IS_24XX(isp)) {
2212 isp_complete_ctio(ccb);
2214 isp_target_putback_atio(ccb);
2219 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
2222 if (isp->isp_state != ISP_RUNSTATE) {
2223 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2228 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2230 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2231 ct7_entry_t local, *cto = &local;
2232 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2237 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2238 if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
2243 ISP_MEMZERO(&local, sizeof (local));
2244 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2245 cto->ct_header.rqs_entry_count = 1;
2246 cto->ct_nphdl = nphdl;
2247 cto->ct_rxid = aep->at_rxid;
2248 cto->ct_vpidx = mp->nt_channel;
2249 cto->ct_iid_lo = sid;
2250 cto->ct_iid_hi = sid >> 16;
2251 cto->ct_oxid = aep->at_hdr.ox_id;
2252 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2253 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2255 cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
2256 cto->rsp.m1.ct_resplen = 4;
2257 ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
2258 cto->rsp.m1.ct_resp[0] = rsp & 0xff;
2259 cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
2260 cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
2261 cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
2263 return (isp_target_put_entry(isp, &local));
2267 * This case is for a responding to an ABTS frame
2269 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2272 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2274 if (mp->nt_need_ack) {
2275 uint8_t storage[QENTRY_LEN];
2276 ct7_entry_t *cto = (ct7_entry_t *) storage;
2277 abts_t *abts = (abts_t *)mp->nt_lreserved;
2279 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2280 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2281 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2282 cto->ct_header.rqs_entry_count = 1;
2283 cto->ct_nphdl = mp->nt_nphdl;
2284 cto->ct_rxid = abts->abts_rxid_task;
2285 cto->ct_iid_lo = mp->nt_sid;
2286 cto->ct_iid_hi = mp->nt_sid >> 16;
2287 cto->ct_oxid = abts->abts_ox_id;
2288 cto->ct_vpidx = mp->nt_channel;
2289 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2290 if (isp_target_put_entry(isp, cto)) {
2293 mp->nt_need_ack = 0;
2295 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2303 * Handle logout cases here
2305 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2306 isp_del_all_wwn_entries(isp, mp->nt_channel);
2309 if (mp->nt_ncode == NT_LOGOUT) {
2310 if (!IS_2100(isp) && IS_FC(isp)) {
2311 isp_del_wwn_entries(isp, mp);
2316 * General purpose acknowledgement
2318 if (mp->nt_need_ack) {
2319 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2321 * Don't need to use the guaranteed send because the caller can retry
2323 return (isp_notify_ack(isp, mp->nt_lreserved));
2329 * Handle task management functions.
2331 * We show up here with a notify structure filled out.
2333 * The nt_lreserved tag points to the original queue entry
2336 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2340 struct ccb_immediate_notify *inot;
2341 inot_private_data_t *ntp = NULL;
2342 atio_private_data_t *atp;
2345 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
2346 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2347 if (notify->nt_lun == LUN_ANY) {
2348 if (notify->nt_tagval == TAG_ANY) {
2349 lun = CAM_LUN_WILDCARD;
2351 atp = isp_find_atpd(isp, notify->nt_channel,
2352 notify->nt_tagval & 0xffffffff);
2353 lun = atp ? atp->lun : CAM_LUN_WILDCARD;
2356 lun = notify->nt_lun;
2358 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2360 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2362 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2366 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2368 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2372 if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2373 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
2374 inot->initiator_id = CAM_TARGET_WILDCARD;
2376 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
2378 inot->seq_id = notify->nt_tagval;
2379 inot->tag_id = notify->nt_tagval >> 32;
2381 switch (notify->nt_ncode) {
2383 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
2384 inot->arg = MSG_ABORT_TASK;
2386 case NT_ABORT_TASK_SET:
2387 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
2388 inot->arg = MSG_ABORT_TASK_SET;
2391 inot->arg = MSG_CLEAR_ACA;
2393 case NT_CLEAR_TASK_SET:
2394 inot->arg = MSG_CLEAR_TASK_SET;
2397 inot->arg = MSG_LOGICAL_UNIT_RESET;
2399 case NT_TARGET_RESET:
2400 inot->arg = MSG_TARGET_RESET;
2402 case NT_QUERY_TASK_SET:
2403 inot->arg = MSG_QUERY_TASK_SET;
2405 case NT_QUERY_ASYNC_EVENT:
2406 inot->arg = MSG_QUERY_ASYNC_EVENT;
2409 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun %#jx", __func__, notify->nt_ncode, notify->nt_channel, (uintmax_t)lun);
2413 ntp = isp_get_ntpd(isp, notify->nt_channel);
2415 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2418 ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
2419 if (notify->nt_lreserved) {
2420 ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
2421 ntp->nt.nt_lreserved = &ntp->data;
2423 ntp->seq_id = notify->nt_tagval;
2424 ntp->tag_id = notify->nt_tagval >> 32;
2426 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2427 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
2428 inot->ccb_h.status = CAM_MESSAGE_RECV;
2429 xpt_done((union ccb *)inot);
2432 if (notify->nt_need_ack) {
2433 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2434 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2435 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2438 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
2444 isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
2446 atio_private_data_t *atp, *atpool;
2447 inot_private_data_t *ntp, *tmp;
2448 uint32_t this_tag_id;
2451 * First, clean any commands pending restart
2453 STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
2455 this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
2457 this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
2458 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2459 isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
2461 isp_put_ntpd(isp, chan, ntp);
2462 STAILQ_REMOVE(&tptr->restart_queue, ntp,
2463 inot_private_data, next);
2468 * Now mark other ones dead as well.
2470 ISP_GET_PC(isp, chan, atpool, atpool);
2471 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
2472 if (atp->lun != tptr->ts_lun)
2474 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
2481 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2483 struct cam_sim *sim;
2487 sim = (struct cam_sim *)cbarg;
2488 isp = (ispsoftc_t *) cam_sim_softc(sim);
2489 bus = cam_sim_bus(sim);
2490 tgt = xpt_path_target_id(path);
2493 case AC_LOST_DEVICE:
2495 uint16_t oflags, nflags;
2496 sdparam *sdp = SDPARAM(isp, bus);
2499 nflags = sdp->isp_devparam[tgt].nvrm_flags;
2500 nflags &= DPARM_SAFE_DFLT;
2501 if (isp->isp_loaded_fw) {
2502 nflags |= DPARM_NARROW | DPARM_ASYNC;
2504 oflags = sdp->isp_devparam[tgt].goal_flags;
2505 sdp->isp_devparam[tgt].goal_flags = nflags;
2506 sdp->isp_devparam[tgt].dev_update = 1;
2508 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
2509 sdp->isp_devparam[tgt].goal_flags = oflags;
2514 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2520 isp_poll(struct cam_sim *sim)
2522 ispsoftc_t *isp = cam_sim_softc(sim);
2529 isp_watchdog(void *arg)
2531 struct ccb_scsiio *xs = arg;
2533 uint32_t ohandle = ISP_HANDLE_FREE, handle;
2537 handle = isp_find_handle(isp, xs);
2540 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2542 if (handle != ISP_HANDLE_FREE) {
2545 handle = isp_find_handle(isp, xs);
2547 if (handle != ISP_HANDLE_FREE) {
2549 * Try and make sure the command is really dead before
2550 * we release the handle (and DMA resources) for reuse.
2552 * If we are successful in aborting the command then
2553 * we're done here because we'll get the command returned
2556 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2561 * Note that after calling the above, the command may in
2562 * fact have been completed.
2564 xs = isp_find_xs(isp, handle);
2567 * If the command no longer exists, then we won't
2568 * be able to find the xs again with this handle.
2575 * After this point, the command is really dead.
2577 if (XS_XFRLEN(xs)) {
2578 ISP_DMAFREE(isp, xs, handle);
2580 isp_destroy_handle(isp, handle);
2581 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2582 xs->ccb_h.status &= ~CAM_STATUS_MASK;
2583 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
2584 isp_prt_endcmd(isp, xs);
2587 if (ohandle != ISP_HANDLE_FREE) {
2588 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2590 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2596 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2599 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2602 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2604 ccb = xpt_alloc_ccb_nowait();
2606 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2609 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2610 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2611 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2619 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2621 struct cam_path *tp;
2622 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2624 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2625 xpt_async(AC_LOST_DEVICE, tp, NULL);
2631 * Gone Device Timer Function- when we have decided that a device has gone
2632 * away, we wait a specific period of time prior to telling the OS it has
2635 * This timer function fires once a second and then scans the port database
2636 * for devices that are marked dead but still have a virtual target assigned.
2637 * We decrement a counter for that port database entry, and when it hits zero,
2638 * we tell the OS the device has gone away.
2643 struct isp_fc *fc = arg;
2644 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2648 isp_gdt_task(void *arg, int pending)
2650 struct isp_fc *fc = arg;
2651 ispsoftc_t *isp = fc->isp;
2652 int chan = fc - isp->isp_osinfo.pc.fc;
2654 struct ac_contract ac;
2655 struct ac_device_changed *adc;
2656 int dbidx, more_to_do = 0;
2659 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2660 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2661 lp = &FCPARAM(isp, chan)->portdb[dbidx];
2663 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2666 if (lp->gone_timer != 0) {
2667 lp->gone_timer -= 1;
2671 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2672 if (lp->is_target) {
2674 isp_make_gone(isp, lp, chan, dbidx);
2676 if (lp->is_initiator) {
2677 lp->is_initiator = 0;
2678 ac.contract_number = AC_CONTRACT_DEV_CHG;
2679 adc = (struct ac_device_changed *) ac.contract_data;
2680 adc->wwpn = lp->port_wwn;
2681 adc->port = lp->portid;
2682 adc->target = dbidx;
2684 xpt_async(AC_CONTRACT, fc->path, &ac);
2686 lp->state = FC_PORTDB_STATE_NIL;
2690 callout_reset(&fc->gdt, hz, isp_gdt, fc);
2692 callout_deactivate(&fc->gdt);
2693 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2700 * When loop goes down we remember the time and freeze CAM command queue.
2701 * During some time period we are trying to reprobe the loop. But if we
2702 * fail, we tell the OS that devices have gone away and drop the freeze.
2704 * We don't clear the devices out of our port database because, when loop
2705 * come back up, we have to do some actual cleanup with the chip at that
2706 * point (implicit PLOGO, e.g., to get the chip's port database state right).
2709 isp_loop_changed(ispsoftc_t *isp, int chan)
2711 fcparam *fcp = FCPARAM(isp, chan);
2712 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2714 if (fc->loop_down_time)
2716 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2717 if (fcp->role & ISP_ROLE_INITIATOR)
2718 isp_freeze_loopdown(isp, chan);
2719 fc->loop_down_time = time_uptime;
2724 isp_loop_up(ispsoftc_t *isp, int chan)
2726 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2728 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2729 fc->loop_seen_once = 1;
2730 fc->loop_down_time = 0;
2731 isp_unfreeze_loopdown(isp, chan);
2735 isp_loop_dead(ispsoftc_t *isp, int chan)
2737 fcparam *fcp = FCPARAM(isp, chan);
2738 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2740 struct ac_contract ac;
2741 struct ac_device_changed *adc;
2744 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
2747 * Notify to the OS all targets who we now consider have departed.
2749 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2750 lp = &fcp->portdb[dbidx];
2752 if (lp->state == FC_PORTDB_STATE_NIL)
2756 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
2758 for (i = 0; i < isp->isp_maxcmds; i++) {
2759 struct ccb_scsiio *xs;
2761 if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2764 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2767 if (dbidx != XS_TGT(xs)) {
2770 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
2771 isp->isp_xflist[i].handle, chan, XS_TGT(xs),
2772 (uintmax_t)XS_LUN(xs));
2775 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2776 if (lp->is_target) {
2778 isp_make_gone(isp, lp, chan, dbidx);
2780 if (lp->is_initiator) {
2781 lp->is_initiator = 0;
2782 ac.contract_number = AC_CONTRACT_DEV_CHG;
2783 adc = (struct ac_device_changed *) ac.contract_data;
2784 adc->wwpn = lp->port_wwn;
2785 adc->port = lp->portid;
2786 adc->target = dbidx;
2788 xpt_async(AC_CONTRACT, fc->path, &ac);
2792 isp_unfreeze_loopdown(isp, chan);
2793 fc->loop_down_time = 0;
2797 isp_kthread(void *arg)
2799 struct isp_fc *fc = arg;
2800 ispsoftc_t *isp = fc->isp;
2801 int chan = fc - isp->isp_osinfo.pc.fc;
2806 while (isp->isp_osinfo.is_exiting == 0) {
2807 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2808 "Chan %d Checking FC state", chan);
2809 lb = isp_fc_runstate(isp, chan, 250000);
2810 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2811 "Chan %d FC got to %s state", chan,
2812 isp_fc_loop_statename(lb));
2815 * Our action is different based upon whether we're supporting
2816 * Initiator mode or not. If we are, we might freeze the simq
2817 * when loop is down and set all sorts of different delays to
2820 * If not, we simply just wait for loop to come up.
2822 if (lb == LOOP_READY || lb < 0) {
2826 * If we've never seen loop up and we've waited longer
2827 * than quickboot time, or we've seen loop up but we've
2828 * waited longer than loop_down_limit, give up and go
2829 * to sleep until loop comes up.
2831 if (fc->loop_seen_once == 0)
2832 lim = isp_quickboot_time;
2834 lim = fc->loop_down_limit;
2835 d = time_uptime - fc->loop_down_time;
2851 if (lb == LOOP_READY)
2852 isp_loop_up(isp, chan);
2854 isp_loop_dead(isp, chan);
2857 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2858 "Chan %d sleep for %d seconds", chan, slp);
2859 msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz);
2861 fc->num_threads -= 1;
2866 #ifdef ISP_TARGET_MODE
2868 isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
2870 atio_private_data_t *atp;
2871 union ccb *accb = ccb->cab.abort_ccb;
2872 struct ccb_hdr *sccb;
2875 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2877 /* Search for the ATIO among queueued. */
2878 SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
2879 if (sccb != &accb->ccb_h)
2881 SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2882 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2883 "Abort FREE ATIO\n");
2884 accb->ccb_h.status = CAM_REQ_ABORTED;
2886 ccb->ccb_h.status = CAM_REQ_CMP;
2891 /* Search for the ATIO among running. */
2892 atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
2894 /* Send TERMINATE to firmware. */
2895 if (!atp->dead && IS_24XX(isp)) {
2896 uint8_t storage[QENTRY_LEN];
2897 ct7_entry_t *cto = (ct7_entry_t *) storage;
2899 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2900 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2901 cto->ct_header.rqs_entry_count = 1;
2902 cto->ct_nphdl = atp->nphdl;
2903 cto->ct_rxid = atp->tag;
2904 cto->ct_iid_lo = atp->sid;
2905 cto->ct_iid_hi = atp->sid >> 16;
2906 cto->ct_oxid = atp->oxid;
2907 cto->ct_vpidx = XS_CHANNEL(accb);
2908 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2909 isp_target_put_entry(isp, cto);
2911 isp_put_atpd(isp, XS_CHANNEL(accb), atp);
2912 ccb->ccb_h.status = CAM_REQ_CMP;
2914 ccb->ccb_h.status = CAM_UA_ABORT;
2919 isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
2921 inot_private_data_t *ntp;
2922 union ccb *accb = ccb->cab.abort_ccb;
2923 struct ccb_hdr *sccb;
2926 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2928 /* Search for the INOT among queueued. */
2929 SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
2930 if (sccb != &accb->ccb_h)
2932 SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2933 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2934 "Abort FREE INOT\n");
2935 accb->ccb_h.status = CAM_REQ_ABORTED;
2937 ccb->ccb_h.status = CAM_REQ_CMP;
2942 /* Search for the INOT among running. */
2943 ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
2945 if (ntp->nt.nt_need_ack) {
2946 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
2947 ntp->nt.nt_lreserved);
2949 isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
2950 ccb->ccb_h.status = CAM_REQ_CMP;
2952 ccb->ccb_h.status = CAM_UA_ABORT;
2959 isp_action(struct cam_sim *sim, union ccb *ccb)
2961 int bus, tgt, ts, error;
2963 struct ccb_trans_settings *cts;
2965 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2967 isp = (ispsoftc_t *)cam_sim_softc(sim);
2968 ISP_ASSERT_LOCKED(isp);
2969 bus = cam_sim_bus(sim);
2970 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2971 ISP_PCMD(ccb) = NULL;
2973 switch (ccb->ccb_h.func_code) {
2974 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2976 * Do a couple of preliminary checks...
2978 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2979 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2980 ccb->ccb_h.status = CAM_REQ_INVALID;
2981 isp_done((struct ccb_scsiio *) ccb);
2985 ccb->csio.req_map = NULL;
2987 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
2988 xpt_print(ccb->ccb_h.path, "invalid target\n");
2989 ccb->ccb_h.status = CAM_PATH_INVALID;
2990 } else if (ISP_MAX_LUNS(isp) > 0 &&
2991 ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
2992 xpt_print(ccb->ccb_h.path, "invalid lun\n");
2993 ccb->ccb_h.status = CAM_PATH_INVALID;
2995 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3000 ccb->csio.scsi_status = SCSI_STATUS_OK;
3001 if (isp_get_pcmd(isp, ccb)) {
3002 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
3003 cam_freeze_devq(ccb->ccb_h.path);
3004 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
3005 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3009 error = isp_start((XS_T *) ccb);
3012 ccb->ccb_h.status |= CAM_SIM_QUEUED;
3013 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
3016 ts = ccb->ccb_h.timeout;
3017 if (ts == CAM_TIME_DEFAULT) {
3020 ts = isp_mstohz(ts);
3021 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3024 isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
3025 XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3026 cam_freeze_devq(ccb->ccb_h.path);
3027 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3028 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3029 isp_free_pcmd(isp, ccb);
3033 isp_free_pcmd(isp, ccb);
3034 cam_freeze_devq(ccb->ccb_h.path);
3035 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3036 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3040 isp_done((struct ccb_scsiio *) ccb);
3043 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3044 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3045 isp_free_pcmd(isp, ccb);
3050 #ifdef ISP_TARGET_MODE
3051 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
3052 if (ccb->cel.enable) {
3053 isp_enable_lun(isp, ccb);
3055 isp_disable_lun(isp, ccb);
3058 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
3059 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
3061 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3065 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
3066 str = "XPT_IMMEDIATE_NOTIFY";
3068 str = "XPT_ACCEPT_TARGET_IO";
3069 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
3070 "%s: no state pointer found for %s\n",
3072 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3076 ccb->ccb_h.spriv_field0 = 0;
3077 ccb->ccb_h.spriv_ptr1 = isp;
3079 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3080 ccb->atio.tag_id = 0;
3081 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3082 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3084 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3085 ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3086 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3087 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3090 ccb->ccb_h.status = CAM_REQ_INPROG;
3093 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
3095 inot_private_data_t *ntp;
3098 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
3099 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
3102 * All the relevant path information is in the associated immediate notify
3104 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);
3105 ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
3107 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__,
3108 ccb->cna2.tag_id, ccb->cna2.seq_id);
3109 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3113 if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
3114 (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
3115 cam_freeze_devq(ccb->ccb_h.path);
3116 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3117 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3118 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
3121 isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
3122 ccb->ccb_h.status = CAM_REQ_CMP;
3123 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);
3127 case XPT_CONT_TARGET_IO:
3128 isp_target_start_ctio(isp, ccb, FROM_CAM);
3131 case XPT_RESET_DEV: /* BDR the specified SCSI device */
3132 tgt = ccb->ccb_h.target_id;
3135 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3137 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3140 * If we have a FC device, reset the Command
3141 * Reference Number, because the target will expect
3142 * that we re-start the CRN at 1 after a reset.
3145 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3147 ccb->ccb_h.status = CAM_REQ_CMP;
3151 case XPT_ABORT: /* Abort the specified CCB */
3153 union ccb *accb = ccb->cab.abort_ccb;
3154 switch (accb->ccb_h.func_code) {
3155 #ifdef ISP_TARGET_MODE
3156 case XPT_ACCEPT_TARGET_IO:
3157 isp_abort_atio(isp, ccb);
3159 case XPT_IMMEDIATE_NOTIFY:
3160 isp_abort_inot(isp, ccb);
3164 error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3166 ccb->ccb_h.status = CAM_UA_ABORT;
3168 ccb->ccb_h.status = CAM_REQ_CMP;
3172 ccb->ccb_h.status = CAM_REQ_INVALID;
3176 * This is not a queued CCB, so the caller expects it to be
3177 * complete when control is returned.
3181 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
3182 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
3184 if (!IS_CURRENT_SETTINGS(cts)) {
3185 ccb->ccb_h.status = CAM_REQ_INVALID;
3189 tgt = cts->ccb_h.target_id;
3191 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3192 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3193 sdparam *sdp = SDPARAM(isp, bus);
3196 if (spi->valid == 0 && scsi->valid == 0) {
3197 ccb->ccb_h.status = CAM_REQ_CMP;
3203 * We always update (internally) from goal_flags
3204 * so any request to change settings just gets
3205 * vectored to that location.
3207 dptr = &sdp->isp_devparam[tgt].goal_flags;
3209 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3210 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3211 *dptr |= DPARM_DISC;
3213 *dptr &= ~DPARM_DISC;
3216 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3217 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3218 *dptr |= DPARM_TQING;
3220 *dptr &= ~DPARM_TQING;
3223 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3224 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3225 *dptr |= DPARM_WIDE;
3227 *dptr &= ~DPARM_WIDE;
3233 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3234 *dptr |= DPARM_SYNC;
3236 * XXX: CHECK FOR LEGALITY
3238 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
3239 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3241 *dptr &= ~DPARM_SYNC;
3243 isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%jx) to flags %x off %x per %x", bus, tgt, (uintmax_t)cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
3244 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3245 sdp->isp_devparam[tgt].dev_update = 1;
3248 ccb->ccb_h.status = CAM_REQ_CMP;
3251 case XPT_GET_TRAN_SETTINGS:
3253 tgt = cts->ccb_h.target_id;
3255 fcparam *fcp = FCPARAM(isp, bus);
3256 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3257 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3259 cts->protocol = PROTO_SCSI;
3260 cts->protocol_version = SCSI_REV_2;
3261 cts->transport = XPORT_FC;
3262 cts->transport_version = 0;
3264 scsi->valid = CTS_SCSI_VALID_TQ;
3265 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3266 fc->valid = CTS_FC_VALID_SPEED;
3267 fc->bitrate = 100000;
3268 fc->bitrate *= fcp->isp_gbspeed;
3269 if (tgt < MAX_FC_TARG) {
3270 fcportdb_t *lp = &fcp->portdb[tgt];
3271 fc->wwnn = lp->node_wwn;
3272 fc->wwpn = lp->port_wwn;
3273 fc->port = lp->portid;
3274 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3277 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3278 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3279 sdparam *sdp = SDPARAM(isp, bus);
3280 uint16_t dval, pval, oval;
3282 if (IS_CURRENT_SETTINGS(cts)) {
3283 sdp->isp_devparam[tgt].dev_refresh = 1;
3285 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3286 dval = sdp->isp_devparam[tgt].actv_flags;
3287 oval = sdp->isp_devparam[tgt].actv_offset;
3288 pval = sdp->isp_devparam[tgt].actv_period;
3290 dval = sdp->isp_devparam[tgt].nvrm_flags;
3291 oval = sdp->isp_devparam[tgt].nvrm_offset;
3292 pval = sdp->isp_devparam[tgt].nvrm_period;
3295 cts->protocol = PROTO_SCSI;
3296 cts->protocol_version = SCSI_REV_2;
3297 cts->transport = XPORT_SPI;
3298 cts->transport_version = 2;
3304 if (dval & DPARM_DISC) {
3305 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3307 if ((dval & DPARM_SYNC) && oval && pval) {
3308 spi->sync_offset = oval;
3309 spi->sync_period = pval;
3311 spi->sync_offset = 0;
3312 spi->sync_period = 0;
3314 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3315 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3316 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3317 if (dval & DPARM_WIDE) {
3318 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3320 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3322 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3323 scsi->valid = CTS_SCSI_VALID_TQ;
3324 if (dval & DPARM_TQING) {
3325 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3327 spi->valid |= CTS_SPI_VALID_DISC;
3329 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3330 bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3332 ccb->ccb_h.status = CAM_REQ_CMP;
3336 case XPT_CALC_GEOMETRY:
3337 cam_calc_geometry(&ccb->ccg, 1);
3341 case XPT_RESET_BUS: /* Reset the specified bus */
3342 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
3344 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3349 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
3352 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
3354 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
3356 ccb->ccb_h.status = CAM_REQ_CMP;
3360 case XPT_TERM_IO: /* Terminate the I/O process */
3361 ccb->ccb_h.status = CAM_REQ_INVALID;
3365 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
3367 struct ccb_sim_knob *kp = &ccb->knob;
3371 ccb->ccb_h.status = CAM_REQ_INVALID;
3376 fcp = FCPARAM(isp, bus);
3378 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
3379 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
3380 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
3381 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
3383 ccb->ccb_h.status = CAM_REQ_CMP;
3384 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
3388 switch (kp->xport_specific.fc.role) {
3389 case KNOB_ROLE_NONE:
3390 if (fcp->role != ISP_ROLE_NONE) {
3392 newrole = ISP_ROLE_NONE;
3395 case KNOB_ROLE_TARGET:
3396 if (fcp->role != ISP_ROLE_TARGET) {
3398 newrole = ISP_ROLE_TARGET;
3401 case KNOB_ROLE_INITIATOR:
3402 if (fcp->role != ISP_ROLE_INITIATOR) {
3404 newrole = ISP_ROLE_INITIATOR;
3407 case KNOB_ROLE_BOTH:
3408 if (fcp->role != ISP_ROLE_BOTH) {
3410 newrole = ISP_ROLE_BOTH;
3415 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
3416 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
3417 bus, newrole) != 0) {
3418 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3427 case XPT_GET_SIM_KNOB_OLD: /* Get SIM knobs -- compat value */
3428 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
3430 struct ccb_sim_knob *kp = &ccb->knob;
3435 fcp = FCPARAM(isp, bus);
3437 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
3438 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
3439 switch (fcp->role) {
3441 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3443 case ISP_ROLE_TARGET:
3444 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3446 case ISP_ROLE_INITIATOR:
3447 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3450 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3453 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3454 ccb->ccb_h.status = CAM_REQ_CMP;
3456 ccb->ccb_h.status = CAM_REQ_INVALID;
3461 case XPT_PATH_INQ: /* Path routing inquiry */
3463 struct ccb_pathinq *cpi = &ccb->cpi;
3465 cpi->version_num = 1;
3466 #ifdef ISP_TARGET_MODE
3467 if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3468 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3471 cpi->target_sprt = 0;
3472 cpi->hba_eng_cnt = 0;
3473 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
3474 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
3475 255 : ISP_MAX_LUNS(isp) - 1;
3476 cpi->bus_id = cam_sim_bus(sim);
3477 if (isp->isp_osinfo.sixtyfourbit)
3478 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
3480 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
3483 fcparam *fcp = FCPARAM(isp, bus);
3485 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3486 cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
3489 * Because our loop ID can shift from time to time,
3490 * make our initiator ID out of range of our bus.
3492 cpi->initiator_id = cpi->max_target + 1;
3495 * Set base transfer capabilities for Fibre Channel, for this HBA.
3498 cpi->base_transfer_speed = 8000000;
3499 } else if (IS_24XX(isp)) {
3500 cpi->base_transfer_speed = 4000000;
3501 } else if (IS_23XX(isp)) {
3502 cpi->base_transfer_speed = 2000000;
3504 cpi->base_transfer_speed = 1000000;
3506 cpi->hba_inquiry = PI_TAG_ABLE;
3507 cpi->transport = XPORT_FC;
3508 cpi->transport_version = 0;
3509 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
3510 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
3511 cpi->xport_specific.fc.port = fcp->isp_portid;
3512 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3514 sdparam *sdp = SDPARAM(isp, bus);
3515 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3516 cpi->hba_misc = PIM_UNMAPPED;
3517 cpi->initiator_id = sdp->isp_initiator_id;
3518 cpi->base_transfer_speed = 3300;
3519 cpi->transport = XPORT_SPI;
3520 cpi->transport_version = 2;
3522 cpi->protocol = PROTO_SCSI;
3523 cpi->protocol_version = SCSI_REV_2;
3524 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3525 strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
3526 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3527 cpi->unit_number = cam_sim_unit(sim);
3528 cpi->ccb_h.status = CAM_REQ_CMP;
3533 ccb->ccb_h.status = CAM_REQ_INVALID;
3540 isp_done(XS_T *sccb)
3542 ispsoftc_t *isp = XS_ISP(sccb);
3546 XS_SETERR(sccb, CAM_REQ_CMP);
3548 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3549 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
3550 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
3551 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
3553 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3557 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3558 status = sccb->ccb_h.status & CAM_STATUS_MASK;
3559 if (status != CAM_REQ_CMP &&
3560 (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3561 sccb->ccb_h.status |= CAM_DEV_QFRZN;
3562 xpt_freeze_devq(sccb->ccb_h.path, 1);
3565 if (ISP_PCMD(sccb)) {
3566 if (callout_active(&PISP_PCMD(sccb)->wdog))
3567 callout_stop(&PISP_PCMD(sccb)->wdog);
3568 isp_free_pcmd(isp, (union ccb *) sccb);
3570 xpt_done((union ccb *) sccb);
3574 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3577 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3583 struct cam_path *tmppath;
3584 struct ac_contract ac;
3585 struct ac_device_changed *adc;
3589 case ISPASYNC_NEW_TGT_PARAMS:
3591 struct ccb_trans_settings_scsi *scsi;
3592 struct ccb_trans_settings_spi *spi;
3595 struct ccb_trans_settings cts;
3597 memset(&cts, 0, sizeof (struct ccb_trans_settings));
3600 bus = va_arg(ap, int);
3601 tgt = va_arg(ap, int);
3603 sdp = SDPARAM(isp, bus);
3605 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3606 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3609 flags = sdp->isp_devparam[tgt].actv_flags;
3610 cts.type = CTS_TYPE_CURRENT_SETTINGS;
3611 cts.protocol = PROTO_SCSI;
3612 cts.transport = XPORT_SPI;
3614 scsi = &cts.proto_specific.scsi;
3615 spi = &cts.xport_specific.spi;
3617 if (flags & DPARM_TQING) {
3618 scsi->valid |= CTS_SCSI_VALID_TQ;
3619 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3622 if (flags & DPARM_DISC) {
3623 spi->valid |= CTS_SPI_VALID_DISC;
3624 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3626 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3627 if (flags & DPARM_WIDE) {
3628 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3630 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3632 if (flags & DPARM_SYNC) {
3633 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3634 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3635 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3636 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3638 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);
3639 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3640 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3641 xpt_free_path(tmppath);
3644 case ISPASYNC_BUS_RESET:
3647 bus = va_arg(ap, int);
3649 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
3651 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
3653 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
3659 msg = "LIP Received";
3661 case ISPASYNC_LOOP_RESET:
3665 case ISPASYNC_LOOP_DOWN:
3669 bus = va_arg(ap, int);
3671 isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
3672 isp_loop_changed(isp, bus);
3673 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3675 case ISPASYNC_LOOP_UP:
3677 bus = va_arg(ap, int);
3679 isp_loop_changed(isp, bus);
3680 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
3682 case ISPASYNC_DEV_ARRIVED:
3684 bus = va_arg(ap, int);
3685 lp = va_arg(ap, fcportdb_t *);
3687 fc = ISP_FC_PC(isp, bus);
3688 tgt = FC_PORTDB_TGT(isp, bus, lp);
3689 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3690 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
3691 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3692 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
3694 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3695 isp_make_here(isp, lp, bus, tgt);
3697 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3698 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
3699 lp->is_initiator = 1;
3700 ac.contract_number = AC_CONTRACT_DEV_CHG;
3701 adc = (struct ac_device_changed *) ac.contract_data;
3702 adc->wwpn = lp->port_wwn;
3703 adc->port = lp->portid;
3706 xpt_async(AC_CONTRACT, fc->path, &ac);
3709 case ISPASYNC_DEV_CHANGED:
3711 bus = va_arg(ap, int);
3712 lp = va_arg(ap, fcportdb_t *);
3714 fc = ISP_FC_PC(isp, bus);
3715 tgt = FC_PORTDB_TGT(isp, bus, lp);
3716 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
3717 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
3719 if (lp->is_target !=
3720 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3721 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
3722 lp->is_target = !lp->is_target;
3723 if (lp->is_target) {
3724 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3725 isp_make_here(isp, lp, bus, tgt);
3727 isp_make_gone(isp, lp, bus, tgt);
3728 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3731 if (lp->is_initiator !=
3732 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3733 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
3734 lp->is_initiator = !lp->is_initiator;
3735 ac.contract_number = AC_CONTRACT_DEV_CHG;
3736 adc = (struct ac_device_changed *) ac.contract_data;
3737 adc->wwpn = lp->port_wwn;
3738 adc->port = lp->portid;
3740 adc->arrived = lp->is_initiator;
3741 xpt_async(AC_CONTRACT, fc->path, &ac);
3744 case ISPASYNC_DEV_STAYED:
3746 bus = va_arg(ap, int);
3747 lp = va_arg(ap, fcportdb_t *);
3749 fc = ISP_FC_PC(isp, bus);
3750 tgt = FC_PORTDB_TGT(isp, bus, lp);
3751 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3752 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
3754 case ISPASYNC_DEV_GONE:
3756 bus = va_arg(ap, int);
3757 lp = va_arg(ap, fcportdb_t *);
3759 fc = ISP_FC_PC(isp, bus);
3760 tgt = FC_PORTDB_TGT(isp, bus, lp);
3762 * If this has a virtual target or initiator set the isp_gdt
3763 * timer running on it to delay its departure.
3765 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3766 if (lp->is_target || lp->is_initiator) {
3767 lp->state = FC_PORTDB_STATE_ZOMBIE;
3768 lp->gone_timer = fc->gone_device_time;
3769 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
3770 if (fc->ready && !callout_active(&fc->gdt)) {
3771 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
3772 callout_reset(&fc->gdt, hz, isp_gdt, fc);
3776 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
3778 case ISPASYNC_CHANGE_NOTIFY:
3781 int evt, nphdl, nlstate, portid, reason;
3784 bus = va_arg(ap, int);
3785 evt = va_arg(ap, int);
3786 if (evt == ISPASYNC_CHANGE_PDB) {
3787 nphdl = va_arg(ap, int);
3788 nlstate = va_arg(ap, int);
3789 reason = va_arg(ap, int);
3790 } else if (evt == ISPASYNC_CHANGE_SNS) {
3791 portid = va_arg(ap, int);
3794 nlstate = reason = 0;
3797 fc = ISP_FC_PC(isp, bus);
3799 if (evt == ISPASYNC_CHANGE_PDB) {
3800 msg = "Port Database Changed";
3801 isp_prt(isp, ISP_LOGINFO,
3802 "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3803 bus, msg, nphdl, nlstate, reason);
3804 } else if (evt == ISPASYNC_CHANGE_SNS) {
3805 msg = "Name Server Database Changed";
3806 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3809 msg = "Other Change Notify";
3810 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3812 isp_loop_changed(isp, bus);
3815 #ifdef ISP_TARGET_MODE
3816 case ISPASYNC_TARGET_NOTIFY:
3818 isp_notify_t *notify;
3820 notify = va_arg(ap, isp_notify_t *);
3822 switch (notify->nt_ncode) {
3824 case NT_ABORT_TASK_SET:
3826 case NT_CLEAR_TASK_SET:
3828 case NT_TARGET_RESET:
3829 case NT_QUERY_TASK_SET:
3830 case NT_QUERY_ASYNC_EVENT:
3832 * These are task management functions.
3834 isp_handle_platform_target_tmf(isp, notify);
3842 * No action need be taken here.
3845 case NT_GLOBAL_LOGOUT:
3848 * This is device arrival/departure notification
3850 isp_handle_platform_target_notify_ack(isp, notify, 0);
3853 isp_handle_platform_srr(isp, notify);
3856 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
3857 isp_handle_platform_target_notify_ack(isp, notify, 0);
3862 case ISPASYNC_TARGET_NOTIFY_ACK:
3866 inot = va_arg(ap, void *);
3868 if (isp_notify_ack(isp, inot)) {
3869 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3872 memcpy(tp->data, inot, sizeof (tp->data));
3874 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
3875 callout_reset(&tp->timer, 5,
3876 isp_refire_notify_ack, tp);
3878 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3883 case ISPASYNC_TARGET_ACTION:
3888 hp = va_arg(ap, isphdr_t *);
3890 switch (hp->rqs_entry_type) {
3892 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
3895 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
3901 isp_handle_platform_ctio(isp, hp);
3904 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
3905 __func__, hp->rqs_entry_type);
3911 case ISPASYNC_FW_CRASH:
3913 uint16_t mbox1, mbox6;
3914 mbox1 = ISP_READ(isp, OUTMAILBOX1);
3915 if (IS_DUALBUS(isp)) {
3916 mbox6 = ISP_READ(isp, OUTMAILBOX6);
3920 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
3922 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
3923 isp->isp_osinfo.mbox_sleep_ok = 0;
3925 isp->isp_osinfo.mbox_sleep_ok = mbox1;
3926 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3931 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3937 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
3940 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3942 /* First try to use explicitly configured WWNs. */
3943 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
3947 /* Otherwise try to use WWNs from NVRAM. */
3949 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
3950 FCPARAM(isp, chan)->isp_wwpn_nvram;
3955 /* If still no WWNs, try to steal them from the first channel. */
3957 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
3958 ISP_FC_PC(isp, 0)->def_wwpn;
3960 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
3961 FCPARAM(isp, 0)->isp_wwpn_nvram;
3965 /* If still nothing -- improvise. */
3967 seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
3969 seed ^= 0x0100000000000000ULL;
3972 /* For additional channels we have to improvise even more. */
3973 if (!iswwnn && chan > 0) {
3975 * We'll stick our channel number plus one first into bits
3976 * 57..59 and thence into bits 52..55 which allows for 8 bits
3977 * of channel which is enough for our maximum of 255 channels.
3979 seed ^= 0x0100000000000000ULL;
3980 seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
3981 seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
3987 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3993 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3996 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
3999 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4001 printf("%s\n", lbuf);
4005 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4008 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4011 xpt_print_path(xs->ccb_h.path);
4019 isp_nanotime_sub(struct timespec *b, struct timespec *a)
4022 struct timespec x = *b;
4024 elapsed = GET_NANOSEC(&x);
4031 isp_mbox_acquire(ispsoftc_t *isp)
4033 if (isp->isp_osinfo.mboxbsy) {
4036 isp->isp_osinfo.mboxcmd_done = 0;
4037 isp->isp_osinfo.mboxbsy = 1;
4043 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4047 to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4048 if (isp->isp_osinfo.mbox_sleep_ok) {
4049 isp->isp_osinfo.mbox_sleep_ok = 0;
4050 isp->isp_osinfo.mbox_sleeping = 1;
4051 msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_lock,
4052 PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4053 isp->isp_osinfo.mbox_sleep_ok = 1;
4054 isp->isp_osinfo.mbox_sleeping = 0;
4056 for (t = 0; t < to; t += 100) {
4057 if (isp->isp_osinfo.mboxcmd_done)
4060 if (isp->isp_osinfo.mboxcmd_done)
4065 if (isp->isp_osinfo.mboxcmd_done == 0) {
4066 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
4067 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
4068 isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4069 mbp->param[0] = MBOX_TIMEOUT;
4070 isp->isp_osinfo.mboxcmd_done = 1;
4075 isp_mbox_notify_done(ispsoftc_t *isp)
4077 isp->isp_osinfo.mboxcmd_done = 1;
4078 if (isp->isp_osinfo.mbox_sleeping)
4079 wakeup(&isp->isp_osinfo.mboxcmd_done);
4083 isp_mbox_release(ispsoftc_t *isp)
4085 isp->isp_osinfo.mboxbsy = 0;
4089 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
4092 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
4095 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
4105 t.tv_sec = ms / 1000;
4106 t.tv_usec = (ms % 1000) * 1000;
4118 isp_platform_intr(void *arg)
4120 ispsoftc_t *isp = arg;
4128 isp_platform_intr_resp(void *arg)
4130 ispsoftc_t *isp = arg;
4133 isp_intr_respq(isp);
4136 /* We have handshake enabled, so explicitly complete interrupt */
4137 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4141 isp_platform_intr_atio(void *arg)
4143 ispsoftc_t *isp = arg;
4146 #ifdef ISP_TARGET_MODE
4147 isp_intr_atioq(isp);
4151 /* We have handshake enabled, so explicitly complete interrupt */
4152 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4156 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
4158 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4159 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
4161 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
4163 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
4167 * Reset the command reference number for all LUNs on a specific target
4168 * (needed when a target arrives again) or for all targets on a port
4169 * (needed for events like a LIP).
4172 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4174 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4175 struct isp_nexus *nxp;
4179 isp_prt(isp, ISP_LOGDEBUG0,
4180 "Chan %d resetting CRN on all targets", chan);
4182 isp_prt(isp, ISP_LOGDEBUG0,
4183 "Chan %d resetting CRN on target %u", chan, tgt);
4185 for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
4186 for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
4187 if (tgt_set == 0 || tgt == nxp->tgt)
4194 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4199 struct isp_nexus *nxp;
4205 chan = XS_CHANNEL(cmd);
4208 fc = &isp->isp_osinfo.pc.fc[chan];
4209 idx = NEXUS_HASH(tgt, lun);
4210 nxp = fc->nexus_hash[idx];
4213 if (nxp->tgt == tgt && nxp->lun == lun)
4218 nxp = fc->nexus_free_list;
4220 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4225 fc->nexus_free_list = nxp->next;
4229 nxp->next = fc->nexus_hash[idx];
4230 fc->nexus_hash[idx] = nxp;
4232 if (nxp->crnseed == 0)
4234 PISP_PCMD(cmd)->crn = nxp->crnseed;
4235 *crnp = nxp->crnseed++;
4240 * We enter with the lock held
4243 isp_timer(void *arg)
4245 ispsoftc_t *isp = arg;
4246 #ifdef ISP_TARGET_MODE
4247 isp_tmcmd_restart(isp);
4249 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
4253 isp_get_ecmd(ispsoftc_t *isp)
4255 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4257 isp->isp_osinfo.ecmd_free = ecmd->next;
4263 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4265 ecmd->next = isp->isp_osinfo.ecmd_free;
4266 isp->isp_osinfo.ecmd_free = ecmd;