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_intr_enable(void *);
56 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
57 static void isp_poll(struct cam_sim *);
58 static timeout_t isp_watchdog;
59 static timeout_t isp_gdt;
60 static task_fn_t isp_gdt_task;
61 static void isp_kthread(void *);
62 static void isp_action(struct cam_sim *, union ccb *);
63 static int isp_timer_count;
64 static void isp_timer(void *);
66 static struct cdevsw isp_cdevsw = {
67 .d_version = D_VERSION,
73 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
75 ispsoftc_t *isp = (ispsoftc_t *)arg1;
77 int error, old, value;
79 value = FCPARAM(isp, chan)->role;
81 error = sysctl_handle_int(oidp, &value, 0, req);
82 if ((error != 0) || (req->newptr == NULL))
85 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
89 old = FCPARAM(isp, chan)->role;
91 /* We don't allow target mode switch from here. */
92 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
94 /* If nothing has changed -- we are done. */
100 /* Actually change the role. */
101 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
107 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
109 struct ccb_setasync csa;
111 struct cam_path *path;
112 #ifdef ISP_TARGET_MODE
117 * Construct our SIM entry.
119 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
126 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
128 cam_sim_free(sim, FALSE);
132 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
134 xpt_bus_deregister(cam_sim_path(sim));
136 cam_sim_free(sim, FALSE);
139 xpt_setup_ccb(&csa.ccb_h, path, 5);
140 csa.ccb_h.func_code = XPT_SASYNC_CB;
141 csa.event_enable = AC_LOST_DEVICE;
142 csa.callback = isp_cam_async;
143 csa.callback_arg = sim;
146 xpt_action((union ccb *)&csa);
150 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
153 #ifdef ISP_TARGET_MODE
154 TAILQ_INIT(&spi->waitq);
155 STAILQ_INIT(&spi->ntfree);
156 for (i = 0; i < ATPDPSIZE; i++)
157 STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
158 LIST_INIT(&spi->atfree);
159 for (i = ATPDPSIZE-1; i >= 0; i--)
160 LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
161 for (i = 0; i < ATPDPHASHSIZE; i++)
162 LIST_INIT(&spi->atused[i]);
165 fcparam *fcp = FCPARAM(isp, chan);
166 struct isp_fc *fc = ISP_FC_PC(isp, chan);
167 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
168 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
177 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
178 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
179 #ifdef ISP_TARGET_MODE
180 TAILQ_INIT(&fc->waitq);
181 STAILQ_INIT(&fc->ntfree);
182 for (i = 0; i < ATPDPSIZE; i++)
183 STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
184 LIST_INIT(&fc->atfree);
185 for (i = ATPDPSIZE-1; i >= 0; i--)
186 LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
187 for (i = 0; i < ATPDPHASHSIZE; i++)
188 LIST_INIT(&fc->atused[i]);
190 isp_loop_changed(isp, chan);
192 if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
193 "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
194 xpt_free_path(fc->path);
196 xpt_bus_deregister(cam_sim_path(fc->sim));
198 cam_sim_free(fc->sim, FALSE);
201 fc->num_threads += 1;
203 snprintf(name, sizeof(name), "chan%d", chan);
204 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
205 OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
207 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
208 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
209 "World Wide Node Name");
210 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
211 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
212 "World Wide Port Name");
213 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
214 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
216 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
217 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
219 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
220 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
221 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
222 "Cause a Lost Frame on a Read");
224 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
225 "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
226 isp_role_sysctl, "I", "Current role");
227 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
228 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
229 "Connection speed in gigabits");
230 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
231 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
233 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
234 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
236 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
237 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
239 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
240 "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
241 "Connection topology");
247 isp_detach_chan(ispsoftc_t *isp, int chan)
250 struct cam_path *path;
251 struct ccb_setasync csa;
254 ISP_GET_PC(isp, chan, sim, sim);
255 ISP_GET_PC(isp, chan, path, path);
256 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
258 xpt_setup_ccb(&csa.ccb_h, path, 5);
259 csa.ccb_h.func_code = XPT_SASYNC_CB;
260 csa.event_enable = 0;
261 csa.callback = isp_cam_async;
262 csa.callback_arg = sim;
263 xpt_action((union ccb *)&csa);
265 xpt_bus_deregister(cam_sim_path(sim));
266 cam_sim_free(sim, FALSE);
268 /* Wait for the channel's spawned threads to exit. */
269 wakeup(isp->isp_osinfo.pc.ptr);
270 while (*num_threads != 0)
271 mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
275 isp_attach(ispsoftc_t *isp)
277 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
278 int du = device_get_unit(isp->isp_dev);
281 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
282 isp->isp_osinfo.ehook.ich_arg = isp;
284 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
285 * will be called right awawy, which will clear isp_osinfo.ehook_active,
286 * which would be unwise to then set again later.
288 isp->isp_osinfo.ehook_active = 1;
289 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
290 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
295 * Create the device queue for our SIM(s).
297 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
298 if (isp->isp_osinfo.devq == NULL) {
299 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
303 for (chan = 0; chan < isp->isp_nchan; chan++) {
304 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
309 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
310 isp_timer_count = hz >> 2;
311 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
312 isp->isp_osinfo.timer_active = 1;
314 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
315 if (isp->isp_osinfo.cdev) {
316 isp->isp_osinfo.cdev->si_drv1 = isp;
321 while (--chan >= 0) {
323 struct cam_path *path;
325 ISP_GET_PC(isp, chan, sim, sim);
326 ISP_GET_PC(isp, chan, path, path);
329 xpt_bus_deregister(cam_sim_path(sim));
331 cam_sim_free(sim, FALSE);
333 if (isp->isp_osinfo.ehook_active) {
334 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
335 isp->isp_osinfo.ehook_active = 0;
337 if (isp->isp_osinfo.cdev) {
338 destroy_dev(isp->isp_osinfo.cdev);
339 isp->isp_osinfo.cdev = NULL;
341 cam_simq_free(isp->isp_osinfo.devq);
342 isp->isp_osinfo.devq = NULL;
347 isp_detach(ispsoftc_t *isp)
353 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
354 ISP_GET_PC(isp, chan, sim, sim);
355 if (sim->refcount > 2) {
360 /* Tell spawned threads that we're exiting. */
361 isp->isp_osinfo.is_exiting = 1;
362 if (isp->isp_osinfo.timer_active) {
363 callout_stop(&isp->isp_osinfo.tmo);
364 isp->isp_osinfo.timer_active = 0;
366 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
367 isp_detach_chan(isp, chan);
370 if (isp->isp_osinfo.cdev) {
371 destroy_dev(isp->isp_osinfo.cdev);
372 isp->isp_osinfo.cdev = NULL;
374 if (isp->isp_osinfo.ehook_active) {
375 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
376 isp->isp_osinfo.ehook_active = 0;
378 if (isp->isp_osinfo.devq != NULL) {
379 cam_simq_free(isp->isp_osinfo.devq);
380 isp->isp_osinfo.devq = NULL;
386 isp_freeze_loopdown(ispsoftc_t *isp, int chan)
389 struct isp_fc *fc = ISP_FC_PC(isp, chan);
390 if (fc->simqfrozen == 0) {
391 isp_prt(isp, ISP_LOGDEBUG0,
392 "Chan %d Freeze simq (loopdown)", chan);
393 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
394 #if __FreeBSD_version >= 1000039
397 xpt_freeze_simq(fc->sim, 1);
399 isp_prt(isp, ISP_LOGDEBUG0,
400 "Chan %d Mark simq frozen (loopdown)", chan);
401 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
407 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
410 struct isp_fc *fc = ISP_FC_PC(isp, chan);
411 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
412 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
413 if (wasfrozen && fc->simqfrozen == 0) {
414 isp_prt(isp, ISP_LOGDEBUG0,
415 "Chan %d Release simq", chan);
416 xpt_release_simq(fc->sim, 1);
417 #if __FreeBSD_version >= 1000039
426 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
429 int nr, chan, retval = ENOTTY;
436 int olddblev = isp->isp_dblev;
437 isp->isp_dblev = *(int *)addr;
438 *(int *)addr = olddblev;
444 if (chan < 0 || chan >= isp->isp_nchan) {
449 *(int *)addr = FCPARAM(isp, chan)->role;
451 *(int *)addr = ISP_ROLE_INITIATOR;
460 if (chan < 0 || chan >= isp->isp_nchan) {
465 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
470 *(int *)addr = FCPARAM(isp, chan)->role;
471 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
486 if (chan < 0 || chan >= isp->isp_nchan) {
491 if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
503 if (chan < 0 || chan >= isp->isp_nchan) {
508 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
516 case ISP_FC_GETDINFO:
518 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
524 if (ifc->loopid >= MAX_FC_TARG) {
528 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
529 if (lp->state != FC_PORTDB_STATE_NIL) {
530 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
531 ifc->loopid = lp->handle;
532 ifc->portid = lp->portid;
533 ifc->node_wwn = lp->node_wwn;
534 ifc->port_wwn = lp->port_wwn;
543 isp_stats_t *sp = (isp_stats_t *) addr;
545 ISP_MEMZERO(sp, sizeof (*sp));
546 sp->isp_stat_version = ISP_STATS_VERSION;
547 sp->isp_type = isp->isp_type;
548 sp->isp_revision = isp->isp_revision;
550 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
551 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
552 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
553 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
554 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
555 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
556 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
557 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
565 isp->isp_intbogus = 0;
566 isp->isp_intmboxc = 0;
567 isp->isp_intoasync = 0;
568 isp->isp_rsltccmplt = 0;
569 isp->isp_fphccmplt = 0;
570 isp->isp_rscchiwater = 0;
571 isp->isp_fpcchiwater = 0;
575 case ISP_FC_GETHINFO:
577 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
578 int chan = hba->fc_channel;
580 if (chan < 0 || chan >= isp->isp_nchan) {
584 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
585 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
586 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
587 hba->fc_nchannels = isp->isp_nchan;
589 hba->fc_nports = MAX_FC_TARG;
590 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
591 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
592 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
593 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
594 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
595 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
596 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
598 hba->fc_nports = MAX_TARGETS;
600 hba->fc_topology = 0;
601 hba->nvram_node_wwn = 0ull;
602 hba->nvram_port_wwn = 0ull;
603 hba->active_node_wwn = 0ull;
604 hba->active_port_wwn = 0ull;
612 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
621 if (chan < 0 || chan >= isp->isp_nchan) {
626 needmarker = retval = 0;
631 uint8_t resp[QENTRY_LEN];
633 isp24xx_statusreq_t sp;
634 fcparam *fcp = FCPARAM(isp, chan);
638 for (i = 0; i < MAX_FC_TARG; i++) {
639 lp = &fcp->portdb[i];
640 if (lp->handle == nphdl) {
644 if (i == MAX_FC_TARG) {
649 ISP_MEMZERO(&tmf, sizeof(tmf));
650 tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
651 tmf.tmf_header.rqs_entry_count = 1;
652 tmf.tmf_nphdl = lp->handle;
655 tmf.tmf_tidlo = lp->portid;
656 tmf.tmf_tidhi = lp->portid >> 16;
657 tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
658 tmf.tmf_lun[1] = fct->lun & 0xff;
659 if (fct->lun >= 256) {
660 tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
662 switch (fct->action) {
664 tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
666 case IPT_TARGET_RESET:
667 tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
671 tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
674 case IPT_CLEAR_TASK_SET:
675 tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
678 case IPT_ABORT_TASK_SET:
679 tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
691 /* Prepare space for response in memory */
692 memset(resp, 0xff, sizeof(resp));
693 tmf.tmf_handle = isp_allocate_handle(isp, resp,
695 if (tmf.tmf_handle == 0) {
696 isp_prt(isp, ISP_LOGERR,
697 "%s: TMF of Chan %d out of handles",
704 /* Send request and wait for response. */
705 reqp = isp_getrqentry(isp);
707 isp_prt(isp, ISP_LOGERR,
708 "%s: TMF of Chan %d out of rqent",
710 isp_destroy_handle(isp, tmf.tmf_handle);
715 isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
716 if (isp->isp_dblev & ISP_LOGDEBUG1)
717 isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
718 ISP_SYNC_REQUEST(isp);
719 if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
720 isp_prt(isp, ISP_LOGERR,
721 "%s: TMF of Chan %d timed out",
723 isp_destroy_handle(isp, tmf.tmf_handle);
728 if (isp->isp_dblev & ISP_LOGDEBUG1)
729 isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
730 isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
732 if (sp.req_completion_status != 0)
737 MBSINIT(&mbs, 0, MBLOGALL, 0);
738 if (ISP_CAP_2KLOGIN(isp) == 0) {
741 switch (fct->action) {
743 mbs.param[0] = MBOX_CLEAR_ACA;
744 mbs.param[1] = nphdl;
745 mbs.param[2] = fct->lun;
747 case IPT_TARGET_RESET:
748 mbs.param[0] = MBOX_TARGET_RESET;
749 mbs.param[1] = nphdl;
753 mbs.param[0] = MBOX_LUN_RESET;
754 mbs.param[1] = nphdl;
755 mbs.param[2] = fct->lun;
758 case IPT_CLEAR_TASK_SET:
759 mbs.param[0] = MBOX_CLEAR_TASK_SET;
760 mbs.param[1] = nphdl;
761 mbs.param[2] = fct->lun;
764 case IPT_ABORT_TASK_SET:
765 mbs.param[0] = MBOX_ABORT_TASK_SET;
766 mbs.param[1] = nphdl;
767 mbs.param[2] = fct->lun;
776 FCPARAM(isp, chan)->sendmarker = 1;
778 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
794 isp_intr_enable(void *arg)
797 ispsoftc_t *isp = arg;
800 for (chan = 0; chan < isp->isp_nchan; chan++) {
801 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
802 ISP_ENABLE_INTS(isp);
807 ISP_ENABLE_INTS(isp);
809 isp->isp_osinfo.ehook_active = 0;
811 /* Release our hook so that the boot can continue. */
812 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
819 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
820 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
822 static ISP_INLINE int
823 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
825 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
826 if (ISP_PCMD(ccb) == NULL) {
829 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
833 static ISP_INLINE void
834 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
837 #ifdef ISP_TARGET_MODE
838 PISP_PCMD(ccb)->datalen = 0;
839 PISP_PCMD(ccb)->totslen = 0;
840 PISP_PCMD(ccb)->cumslen = 0;
841 PISP_PCMD(ccb)->crn = 0;
843 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
844 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
845 ISP_PCMD(ccb) = NULL;
850 * Put the target mode functions here, because some are inlines
852 #ifdef ISP_TARGET_MODE
853 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
854 static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
855 static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
856 static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
857 static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
858 static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
859 static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
860 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
861 static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
862 static void isp_enable_lun(ispsoftc_t *, union ccb *);
863 static void isp_disable_lun(ispsoftc_t *, union ccb *);
864 static timeout_t isp_refire_putback_atio;
865 static timeout_t isp_refire_notify_ack;
866 static void isp_complete_ctio(union ccb *);
867 static void isp_target_putback_atio(union ccb *);
868 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
869 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
870 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
871 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
872 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
873 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
874 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
875 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
876 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
877 static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
879 static ISP_INLINE tstate_t *
880 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
882 tstate_t *tptr = NULL;
885 if (bus < isp->isp_nchan) {
886 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
887 SLIST_FOREACH(tptr, lhp, next) {
888 if (tptr->ts_lun == lun)
896 isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
898 inot_private_data_t *ntp;
901 if (STAILQ_EMPTY(&tptr->restart_queue))
904 STAILQ_CONCAT(&rq, &tptr->restart_queue);
905 while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
906 STAILQ_REMOVE_HEAD(&rq, next);
908 isp_prt(isp, ISP_LOGTDEBUG0,
909 "%s: restarting resrc deprived %x", __func__,
910 ((at7_entry_t *)ntp->data)->at_rxid);
911 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
913 isp_prt(isp, ISP_LOGTDEBUG0,
914 "%s: restarting resrc deprived %x", __func__,
915 ((at2_entry_t *)ntp->data)->at_rxid);
916 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
918 isp_put_ntpd(isp, bus, ntp);
919 if (!STAILQ_EMPTY(&tptr->restart_queue))
922 if (!STAILQ_EMPTY(&rq)) {
923 STAILQ_CONCAT(&rq, &tptr->restart_queue);
924 STAILQ_CONCAT(&tptr->restart_queue, &rq);
926 return (!STAILQ_EMPTY(&tptr->restart_queue));
930 isp_tmcmd_restart(ispsoftc_t *isp)
935 struct isp_ccbq *waitq;
938 for (bus = 0; bus < isp->isp_nchan; bus++) {
939 for (i = 0; i < LUN_HASH_SIZE; i++) {
940 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
941 SLIST_FOREACH(tptr, lhp, next)
942 isp_atio_restart(isp, bus, tptr);
946 * We only need to do this once per channel.
948 ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
949 ccb = (union ccb *)TAILQ_FIRST(waitq);
951 TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
952 isp_target_start_ctio(isp, ccb, FROM_TIMER);
957 static atio_private_data_t *
958 isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
960 struct atpdlist *atfree;
961 struct atpdlist *atused;
962 atio_private_data_t *atp;
964 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
965 atp = LIST_FIRST(atfree);
967 LIST_REMOVE(atp, next);
969 ISP_GET_PC(isp, chan, atused, atused);
970 LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
975 static atio_private_data_t *
976 isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
978 struct atpdlist *atused;
979 atio_private_data_t *atp;
981 ISP_GET_PC(isp, chan, atused, atused);
982 LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
990 isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
992 struct atpdlist *atfree;
995 isp_put_ecmd(isp, atp->ests);
997 LIST_REMOVE(atp, next);
998 memset(atp, 0, sizeof (*atp));
999 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
1000 LIST_INSERT_HEAD(atfree, atp, next);
1004 isp_dump_atpd(ispsoftc_t *isp, int chan)
1006 atio_private_data_t *atp, *atpool;
1007 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1009 ISP_GET_PC(isp, chan, atpool, atpool);
1010 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
1011 if (atp->state == ATPD_STATE_FREE)
1013 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",
1014 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]);
1018 static inot_private_data_t *
1019 isp_get_ntpd(ispsoftc_t *isp, int chan)
1021 struct ntpdlist *ntfree;
1022 inot_private_data_t *ntp;
1024 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
1025 ntp = STAILQ_FIRST(ntfree);
1027 STAILQ_REMOVE_HEAD(ntfree, next);
1031 static inot_private_data_t *
1032 isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
1034 inot_private_data_t *ntp, *ntp2;
1036 ISP_GET_PC(isp, chan, ntpool, ntp);
1037 ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
1038 for (; ntp < ntp2; ntp++) {
1039 if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
1046 isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
1048 struct ntpdlist *ntfree;
1050 ntp->tag_id = ntp->seq_id = 0;
1051 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
1052 STAILQ_INSERT_HEAD(ntfree, ntp, next);
1056 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1062 lun = xpt_path_lun_id(path);
1063 if (lun != CAM_LUN_WILDCARD) {
1064 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1065 return (CAM_LUN_INVALID);
1068 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1070 return (CAM_RESRC_UNAVAIL);
1073 SLIST_INIT(&tptr->atios);
1074 SLIST_INIT(&tptr->inots);
1075 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
1076 SLIST_INSERT_HEAD(lhp, tptr, next);
1078 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1079 return (CAM_REQ_CMP);
1083 destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
1087 inot_private_data_t *ntp;
1089 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
1090 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1091 ccb->ccb_h.status = CAM_REQ_ABORTED;
1094 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
1095 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1096 ccb->ccb_h.status = CAM_REQ_ABORTED;
1099 while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
1100 isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
1101 STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
1102 isp_put_ntpd(isp, bus, ntp);
1104 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1105 SLIST_REMOVE(lhp, tptr, tstate, next);
1106 free(tptr, M_DEVBUF);
1110 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1117 if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
1118 xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
1119 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1125 * We only support either target and lun both wildcard
1126 * or target and lun both non-wildcard.
1128 bus = XS_CHANNEL(ccb);
1129 target = ccb->ccb_h.target_id;
1130 lun = ccb->ccb_h.target_lun;
1131 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1132 "enabling lun %jx\n", (uintmax_t)lun);
1133 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1134 ccb->ccb_h.status = CAM_LUN_INVALID;
1139 /* Create the state pointer. It should not already exist. */
1140 tptr = get_lun_statep(isp, bus, lun);
1142 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1146 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1147 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1152 ccb->ccb_h.status = CAM_REQ_CMP;
1157 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1159 tstate_t *tptr = NULL;
1164 bus = XS_CHANNEL(ccb);
1165 target = ccb->ccb_h.target_id;
1166 lun = ccb->ccb_h.target_lun;
1167 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1168 "disabling lun %jx\n", (uintmax_t)lun);
1169 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1170 ccb->ccb_h.status = CAM_LUN_INVALID;
1175 /* Find the state pointer. */
1176 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1177 ccb->ccb_h.status = CAM_PATH_INVALID;
1182 destroy_lun_state(isp, bus, tptr);
1183 ccb->ccb_h.status = CAM_REQ_CMP;
1188 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1190 int fctape, sendstatus, resid;
1192 atio_private_data_t *atp;
1193 struct ccb_scsiio *cso;
1194 struct isp_ccbq *waitq;
1195 uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1196 uint8_t local[QENTRY_LEN];
1198 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,
1199 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1201 ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
1205 * Insert at the tail of the list, if any, waiting CTIO CCBs
1207 TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
1211 case FROM_CTIO_DONE:
1212 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1216 while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1217 TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
1220 xfrlen = cso->dxfer_len;
1222 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1223 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1224 ccb->ccb_h.status = CAM_REQ_INVALID;
1230 atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
1232 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1233 isp_dump_atpd(isp, XS_CHANNEL(ccb));
1234 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1240 * Is this command a dead duck?
1243 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1244 ccb->ccb_h.status = CAM_REQ_ABORTED;
1250 * Check to make sure we're still in target mode.
1252 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1253 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1254 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1255 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1261 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1262 * could be split into two CTIOs to split data and status).
1264 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1265 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);
1266 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1271 * Does the initiator expect FC-Tape style responses?
1273 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1280 * If we already did the data xfer portion of a CTIO that sends data
1281 * and status, don't do it again and do the status portion now.
1284 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1285 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1286 xfrlen = 0; /* we already did the data transfer */
1289 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1295 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1296 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1298 * Sense length is not the entire sense data structure size. Periph
1299 * drivers don't seem to be setting sense_len to reflect the actual
1300 * size. We'll peek inside to get the right amount.
1302 sense_length = cso->sense_len;
1305 * This 'cannot' happen
1307 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1308 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1314 memset(local, 0, QENTRY_LEN);
1317 * Check for overflow
1319 tmp = atp->bytes_xfered + atp->bytes_in_transit;
1320 if (xfrlen > 0 && tmp > atp->orig_datalen) {
1321 isp_prt(isp, ISP_LOGERR,
1322 "%s: [0x%x] data overflow by %u bytes", __func__,
1323 cso->tag_id, tmp + xfrlen - atp->orig_datalen);
1324 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1328 if (xfrlen > atp->orig_datalen - tmp) {
1329 xfrlen = atp->orig_datalen - tmp;
1330 if (xfrlen == 0 && !sendstatus) {
1331 cso->resid = cso->dxfer_len;
1332 ccb->ccb_h.status = CAM_REQ_CMP;
1339 ct7_entry_t *cto = (ct7_entry_t *) local;
1341 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1342 cto->ct_header.rqs_entry_count = 1;
1343 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1344 ATPD_SET_SEQNO(cto, atp);
1345 cto->ct_nphdl = atp->nphdl;
1346 cto->ct_rxid = atp->tag;
1347 cto->ct_iid_lo = atp->sid;
1348 cto->ct_iid_hi = atp->sid >> 16;
1349 cto->ct_oxid = atp->oxid;
1350 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1351 cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1352 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1355 * Mode 1, status, no data. Only possible when we are sending status, have
1356 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1358 * Mode 2, status, no data. We have to use this in the case that
1359 * the sense data won't fit into a ct7_entry_t.
1362 if (sendstatus && xfrlen == 0) {
1363 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1364 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1365 if (sense_length <= MAXRESPLEN_24XX) {
1366 cto->ct_flags |= CT7_FLAG_MODE1;
1367 cto->ct_scsi_status = cso->scsi_status;
1369 cto->ct_resid = -resid;
1370 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1371 } else if (resid > 0) {
1372 cto->ct_resid = resid;
1373 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1376 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1379 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1380 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1381 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1385 char buf[XCMD_SIZE];
1388 if (atp->ests == NULL) {
1389 atp->ests = isp_get_ecmd(isp);
1390 if (atp->ests == NULL) {
1391 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1395 memset(buf, 0, sizeof (buf));
1396 rp = (fcp_rsp_iu_t *)buf;
1398 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1399 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1401 cto->ct_flags |= CT7_FLAG_MODE2;
1402 rp->fcp_rsp_scsi_status = cso->scsi_status;
1404 rp->fcp_rsp_resid = -resid;
1405 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1406 } else if (resid > 0) {
1407 rp->fcp_rsp_resid = resid;
1408 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1411 rp->fcp_rsp_snslen = sense_length;
1412 cto->ct_senselen = sense_length;
1413 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1414 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1415 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1417 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1419 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1420 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1422 addr = isp->isp_osinfo.ecmd_dma;
1423 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1424 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,
1425 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1426 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1427 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1428 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1429 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1432 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__,
1433 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,
1434 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1436 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1437 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1439 atp->state = ATPD_STATE_LAST_CTIO;
1443 * Mode 0 data transfers, *possibly* with status.
1446 cto->ct_flags |= CT7_FLAG_MODE0;
1447 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1448 cto->ct_flags |= CT7_DATA_IN;
1450 cto->ct_flags |= CT7_DATA_OUT;
1453 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1454 cto->rsp.m0.ct_xfrlen = xfrlen;
1457 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1458 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1459 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1460 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1464 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1465 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1466 cto->ct_flags |= CT7_SENDSTATUS;
1467 atp->state = ATPD_STATE_LAST_CTIO;
1469 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1472 atp->sendst = 1; /* send status later */
1473 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1474 atp->state = ATPD_STATE_CTIO;
1477 atp->state = ATPD_STATE_CTIO;
1479 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__,
1480 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1483 ct2_entry_t *cto = (ct2_entry_t *) local;
1485 if (isp->isp_osinfo.sixtyfourbit)
1486 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1488 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1489 cto->ct_header.rqs_entry_count = 1;
1490 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1491 ATPD_SET_SEQNO(cto, atp);
1492 if (ISP_CAP_2KLOGIN(isp)) {
1493 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1495 cto->ct_iid = atp->nphdl;
1496 if (ISP_CAP_SCCFW(isp) == 0) {
1497 cto->ct_lun = ccb->ccb_h.target_lun;
1500 cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1501 cto->ct_rxid = cso->tag_id;
1504 * Mode 1, status, no data. Only possible when we are sending status, have
1505 * no data to transfer, and the sense length can fit in the ct7_entry.
1507 * Mode 2, status, no data. We have to use this in the case the response
1508 * length won't fit into a ct2_entry_t.
1510 * We'll fill out this structure with information as if this were a
1511 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1512 * needed based upon this.
1514 if (sendstatus && xfrlen == 0) {
1515 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1516 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1517 if (sense_length <= MAXRESPLEN) {
1519 cto->ct_resid = -resid;
1520 } else if (resid > 0) {
1521 cto->ct_resid = resid;
1523 cto->ct_flags |= CT2_FLAG_MODE1;
1524 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1526 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1527 } else if (resid > 0) {
1528 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1531 cto->ct_flags |= CT2_CONFIRM;
1534 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1535 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1536 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1540 char buf[XCMD_SIZE];
1543 if (atp->ests == NULL) {
1544 atp->ests = isp_get_ecmd(isp);
1545 if (atp->ests == NULL) {
1546 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1550 memset(buf, 0, sizeof (buf));
1551 rp = (fcp_rsp_iu_t *)buf;
1553 cto->ct_flags |= CT2_CONFIRM;
1554 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1556 cto->ct_flags |= CT2_FLAG_MODE2;
1557 rp->fcp_rsp_scsi_status = cso->scsi_status;
1559 rp->fcp_rsp_resid = -resid;
1560 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1561 } else if (resid > 0) {
1562 rp->fcp_rsp_resid = resid;
1563 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1566 rp->fcp_rsp_snslen = sense_length;
1567 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1568 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1569 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1571 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1573 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1574 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1576 addr = isp->isp_osinfo.ecmd_dma;
1577 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1578 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,
1579 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1580 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1581 if (isp->isp_osinfo.sixtyfourbit) {
1582 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1583 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1584 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1586 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1587 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1591 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__,
1592 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
1593 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1595 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,
1596 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1598 atp->state = ATPD_STATE_LAST_CTIO;
1602 cto->ct_flags |= CT2_FLAG_MODE0;
1603 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1604 cto->ct_flags |= CT2_DATA_IN;
1606 cto->ct_flags |= CT2_DATA_OUT;
1609 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1610 cto->rsp.m0.ct_xfrlen = xfrlen;
1613 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1614 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1615 cto->ct_flags |= CT2_SENDSTATUS;
1616 atp->state = ATPD_STATE_LAST_CTIO;
1618 cto->ct_flags |= CT2_CONFIRM;
1621 atp->sendst = 1; /* send status later */
1622 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1623 atp->state = ATPD_STATE_CTIO;
1626 atp->state = ATPD_STATE_CTIO;
1629 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,
1630 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);
1633 if (isp_get_pcmd(isp, ccb)) {
1634 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1635 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1638 handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1640 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1641 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1642 isp_free_pcmd(isp, ccb);
1645 atp->bytes_in_transit += xfrlen;
1646 PISP_PCMD(ccb)->datalen = xfrlen;
1650 * Call the dma setup routines for this entry (and any subsequent
1651 * CTIOs) if there's data to move, and then tell the f/w it's got
1652 * new things to play with. As with isp_start's usage of DMA setup,
1653 * any swizzling is done in the machine dependent layer. Because
1654 * of this, we put the request onto the queue area first in native
1659 ct7_entry_t *cto = (ct7_entry_t *) local;
1660 cto->ct_syshandle = handle;
1662 ct2_entry_t *cto = (ct2_entry_t *) local;
1663 cto->ct_syshandle = handle;
1666 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1667 if (dmaresult != CMD_QUEUED) {
1668 isp_destroy_handle(isp, handle);
1669 isp_free_pcmd(isp, ccb);
1670 if (dmaresult == CMD_EAGAIN) {
1671 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1674 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1679 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1681 ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1683 ccb->ccb_h.spriv_field0 = ~0;
1691 isp_refire_putback_atio(void *arg)
1693 union ccb *ccb = arg;
1695 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
1696 isp_target_putback_atio(ccb);
1700 isp_refire_notify_ack(void *arg)
1702 isp_tna_t *tp = arg;
1703 ispsoftc_t *isp = tp->isp;
1705 ISP_ASSERT_LOCKED(isp);
1706 if (isp_notify_ack(isp, tp->not)) {
1707 callout_schedule(&tp->timer, 5);
1715 isp_target_putback_atio(union ccb *ccb)
1718 struct ccb_scsiio *cso;
1720 at2_entry_t local, *at = &local;
1724 qe = isp_getrqentry(isp);
1726 xpt_print(ccb->ccb_h.path,
1727 "%s: Request Queue Overflow\n", __func__);
1728 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1729 isp_refire_putback_atio, ccb);
1732 memset(qe, 0, QENTRY_LEN);
1734 ISP_MEMZERO(at, sizeof (at2_entry_t));
1735 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1736 at->at_header.rqs_entry_count = 1;
1737 if (ISP_CAP_SCCFW(isp)) {
1738 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1739 #if __FreeBSD_version < 1000700
1740 if (at->at_scclun >= 256)
1741 at->at_scclun |= 0x4000;
1744 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1746 at->at_status = CT_OK;
1747 at->at_rxid = cso->tag_id;
1748 at->at_iid = cso->ccb_h.target_id;
1749 isp_put_atio2(isp, at, qe);
1750 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1751 ISP_SYNC_REQUEST(isp);
1752 isp_complete_ctio(ccb);
1756 isp_complete_ctio(union ccb *ccb)
1758 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1759 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1765 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1771 struct ccb_accept_tio *atiop;
1773 atio_private_data_t *atp;
1774 inot_private_data_t *ntp;
1777 * The firmware status (except for the QLTM_SVALID bit)
1778 * indicates why this ATIO was sent to us.
1780 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1782 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1783 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1784 isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
1788 fcp = FCPARAM(isp, 0);
1789 if (ISP_CAP_SCCFW(isp)) {
1790 lun = aep->at_scclun;
1791 #if __FreeBSD_version < 1000700
1797 if (ISP_CAP_2KLOGIN(isp)) {
1798 nphdl = ((at2e_entry_t *)aep)->at_iid;
1800 nphdl = aep->at_iid;
1802 tptr = get_lun_statep(isp, 0, lun);
1804 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1806 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1808 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1810 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1817 * Start any commands pending resources first.
1819 if (isp_atio_restart(isp, 0, tptr))
1822 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1823 if (atiop == NULL) {
1827 atp = isp_get_atpd(isp, 0, aep->at_rxid);
1832 atp->state = ATPD_STATE_ATIO;
1833 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1835 isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
1836 atiop->ccb_h.target_id = fcp->isp_loopid;
1837 atiop->ccb_h.target_lun = lun;
1840 * We don't get 'suggested' sense data as we do with SCSI cards.
1842 atiop->sense_len = 0;
1845 * If we're not in the port database, add ourselves.
1848 atiop->init_id = nphdl;
1850 if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
1851 lp->state == FC_PORTDB_STATE_ZOMBIE)) {
1853 (((uint64_t) aep->at_wwpn[0]) << 48) |
1854 (((uint64_t) aep->at_wwpn[1]) << 32) |
1855 (((uint64_t) aep->at_wwpn[2]) << 16) |
1856 (((uint64_t) aep->at_wwpn[3]) << 0);
1857 isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
1858 nphdl, PORT_ANY, 0);
1859 if (fcp->isp_loopstate > LOOP_LTEST_DONE)
1860 fcp->isp_loopstate = LOOP_LTEST_DONE;
1861 isp_async(isp, ISPASYNC_CHANGE_NOTIFY, 0,
1862 ISPASYNC_CHANGE_PDB, nphdl, 0x06, 0xff);
1863 isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
1865 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1867 atiop->cdb_len = ATIO2_CDBLEN;
1868 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1869 atiop->ccb_h.status = CAM_CDB_RECVD;
1870 atiop->tag_id = atp->tag;
1871 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1872 case ATIO2_TC_ATTR_SIMPLEQ:
1873 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1874 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1876 case ATIO2_TC_ATTR_HEADOFQ:
1877 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1878 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1880 case ATIO2_TC_ATTR_ORDERED:
1881 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1882 atiop->tag_action = MSG_ORDERED_Q_TAG;
1884 case ATIO2_TC_ATTR_ACAQ: /* ?? */
1885 case ATIO2_TC_ATTR_UNTAGGED:
1887 atiop->tag_action = 0;
1891 atp->orig_datalen = aep->at_datalen;
1892 atp->bytes_xfered = 0;
1895 atp->sid = PORT_ANY;
1896 atp->oxid = aep->at_oxid;
1897 atp->cdb0 = aep->at_cdb[0];
1898 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1899 atp->state = ATPD_STATE_CAM;
1900 xpt_done((union ccb *)atiop);
1901 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);
1904 ntp = isp_get_ntpd(isp, 0);
1906 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1909 memcpy(ntp->data, aep, QENTRY_LEN);
1910 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1914 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
1918 uint16_t chan, nphdl = NIL_HANDLE;
1922 struct ccb_accept_tio *atiop;
1923 atio_private_data_t *atp = NULL;
1924 atio_private_data_t *oatp;
1925 inot_private_data_t *ntp;
1927 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
1928 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
1929 #if __FreeBSD_version >= 1000700
1930 lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
1932 lun = (aep->at_cmnd.fcp_cmnd_lun[0] & 0x3f << 8) |
1933 aep->at_cmnd.fcp_cmnd_lun[1];
1937 * Find the N-port handle, and Virtual Port Index for this command.
1939 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
1940 * We also, as a matter of course, need to know the WWN of the initiator too.
1942 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
1944 * Find the right channel based upon D_ID
1946 isp_find_chan_by_did(isp, did, &chan);
1948 if (chan == ISP_NOCHAN) {
1952 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
1953 * It's a bit tricky here as we need to stash this command *somewhere*.
1956 if (NANOTIME_SUB(&now, &isp->isp_init_time) > 2000000000ULL) {
1957 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
1958 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
1961 tptr = get_lun_statep(isp, 0, 0);
1963 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1965 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
1966 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
1970 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
1973 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
1979 * Find the PDB entry for this initiator
1981 if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
1983 * If we're not in the port database terminate the exchange.
1985 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",
1986 __func__, aep->at_rxid, did, chan, sid);
1987 isp_dump_portdb(isp, chan);
1988 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
1994 * Get the tstate pointer
1996 tptr = get_lun_statep(isp, chan, lun);
1998 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2000 isp_prt(isp, ISP_LOGWARN,
2001 "%s: [0x%x] no state pointer for lun %jx or wildcard",
2002 __func__, aep->at_rxid, (uintmax_t)lun);
2004 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2006 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2013 * Start any commands pending resources first.
2015 if (isp_atio_restart(isp, chan, tptr))
2019 * If the f/w is out of resources, just send a BUSY status back.
2021 if (aep->at_rxid == AT7_NORESRC_RXID) {
2022 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2027 * If we're out of resources, just send a BUSY status back.
2029 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2030 if (atiop == NULL) {
2031 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2035 oatp = isp_find_atpd(isp, chan, aep->at_rxid);
2037 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",
2038 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2040 * It's not a "no resource" condition- but we can treat it like one
2044 atp = isp_get_atpd(isp, chan, aep->at_rxid);
2046 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2049 atp->word3 = lp->prli_word3;
2050 atp->state = ATPD_STATE_ATIO;
2051 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2053 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2054 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
2055 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2056 atiop->ccb_h.target_lun = lun;
2057 atiop->sense_len = 0;
2058 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2060 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2062 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2063 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2064 atiop->cdb_len = cdbxlen;
2065 atiop->ccb_h.status = CAM_CDB_RECVD;
2066 atiop->tag_id = atp->tag;
2067 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2068 case FCP_CMND_TASK_ATTR_SIMPLE:
2069 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2070 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2072 case FCP_CMND_TASK_ATTR_HEAD:
2073 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2074 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2076 case FCP_CMND_TASK_ATTR_ORDERED:
2077 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2078 atiop->tag_action = MSG_ORDERED_Q_TAG;
2082 case FCP_CMND_TASK_ATTR_ACA:
2083 case FCP_CMND_TASK_ATTR_UNTAGGED:
2084 atiop->tag_action = 0;
2087 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2088 atp->bytes_xfered = 0;
2093 atp->oxid = aep->at_hdr.ox_id;
2094 atp->rxid = aep->at_hdr.rx_id;
2095 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2096 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2097 atp->state = ATPD_STATE_CAM;
2098 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
2099 aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
2100 xpt_done((union ccb *)atiop);
2104 isp_put_atpd(isp, chan, atp);
2105 ntp = isp_get_ntpd(isp, chan);
2107 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2110 memcpy(ntp->data, aep, QENTRY_LEN);
2111 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
2116 * Handle starting an SRR (sequence retransmit request)
2117 * We get here when we've gotten the immediate notify
2118 * and the return of all outstanding CTIOs for this
2122 isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
2124 in_fcentry_24xx_t *inot;
2125 uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2128 inot = (in_fcentry_24xx_t *)atp->srr;
2129 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2131 atp->srr_ccb = NULL;
2134 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2138 ccb_off = ccb->ccb_h.spriv_field0;
2139 ccb_len = ccb->csio.dxfer_len;
2140 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2142 switch (inot->in_srr_iu) {
2143 case R_CTL_INFO_SOLICITED_DATA:
2145 * We have to restart a FCP_DATA data out transaction
2148 atp->bytes_xfered = srr_off;
2150 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2153 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
2154 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);
2157 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);
2159 case R_CTL_INFO_COMMAND_STATUS:
2160 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2163 * We have to restart a FCP_RSP IU transaction
2166 case R_CTL_INFO_DATA_DESCRIPTOR:
2168 * We have to restart an FCP DATA in transaction
2170 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2174 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2179 * We can't do anything until this is acked, so we might as well start it now.
2180 * We aren't going to do the usual asynchronous ack issue because we need
2181 * to make sure this gets on the wire first.
2183 if (isp_notify_ack(isp, inot)) {
2184 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2187 isp_target_start_ctio(isp, ccb, FROM_SRR);
2190 inot->in_reserved = 1;
2191 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2192 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2193 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2194 isp_complete_ctio(ccb);
2197 if (isp_notify_ack(isp, inot)) {
2198 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2201 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2202 ccb->ccb_h.status = CAM_MESSAGE_RECV;
2204 * This is not a strict interpretation of MDP, but it's close
2206 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2207 ccb->csio.msg_len = 7;
2208 ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2209 ccb->csio.msg_ptr[1] = 5;
2210 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */
2211 ccb->csio.msg_ptr[3] = srr_off >> 24;
2212 ccb->csio.msg_ptr[4] = srr_off >> 16;
2213 ccb->csio.msg_ptr[5] = srr_off >> 8;
2214 ccb->csio.msg_ptr[6] = srr_off;
2215 isp_complete_ctio(ccb);
2220 isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2222 in_fcentry_24xx_t *inot = inot_raw;
2223 atio_private_data_t *atp;
2224 uint32_t tag = inot->in_rxid;
2225 uint32_t bus = inot->in_vpidx;
2227 if (!IS_24XX(isp)) {
2228 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2232 atp = isp_find_atpd(isp, bus, tag);
2234 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2235 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2238 atp->srr_notify_rcvd = 1;
2239 memcpy(atp->srr, inot, sizeof (atp->srr));
2240 isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2241 inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2243 isp_handle_srr_start(isp, atp);
2247 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2250 int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
2251 atio_private_data_t *atp = NULL;
2253 uint32_t handle, data_requested, resid;
2255 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2256 ccb = isp_find_xs(isp, handle);
2258 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2261 isp_destroy_handle(isp, handle);
2262 resid = data_requested = PISP_PCMD(ccb)->datalen;
2263 isp_free_pcmd(isp, ccb);
2264 if (isp->isp_nactive) {
2268 bus = XS_CHANNEL(ccb);
2270 atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2272 atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2276 * XXX: isp_clear_commands() generates fake CTIO with zero
2277 * ct_rxid value, filling only ct_syshandle. Workaround
2278 * that using tag_id from the CCB, pointed by ct_syshandle.
2280 atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2283 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2286 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2287 atp->bytes_in_transit -= data_requested;
2289 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2292 ct7_entry_t *ct = arg;
2294 if (ct->ct_nphdl == CT7_SRR) {
2296 if (atp->srr_notify_rcvd)
2297 isp_handle_srr_start(isp, atp);
2300 if (ct->ct_nphdl == CT_HBA_RESET) {
2301 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2303 failure = CAM_UNREC_HBA_ERROR;
2305 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2306 ok = (ct->ct_nphdl == CT7_OK);
2307 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2308 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
2309 resid = ct->ct_resid;
2311 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),
2312 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2314 ct2_entry_t *ct = arg;
2315 if (ct->ct_status == CT_SRR) {
2317 if (atp->srr_notify_rcvd)
2318 isp_handle_srr_start(isp, atp);
2319 isp_target_putback_atio(ccb);
2322 if (ct->ct_status == CT_HBA_RESET) {
2323 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2325 failure = CAM_UNREC_HBA_ERROR;
2327 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2328 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2329 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2330 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2331 resid = ct->ct_resid;
2333 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),
2334 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2337 if (data_requested > 0) {
2338 atp->bytes_xfered += data_requested - resid;
2339 ccb->csio.resid = ccb->csio.dxfer_len -
2340 (data_requested - resid);
2342 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2343 ccb->ccb_h.status |= CAM_SENT_SENSE;
2344 ccb->ccb_h.status |= CAM_REQ_CMP;
2347 if (failure == CAM_UNREC_HBA_ERROR)
2348 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2350 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2352 atp->state = ATPD_STATE_PDON;
2355 * We never *not* notify CAM when there has been any error (ok == 0),
2356 * so we never need to do an ATIO putback if we're not notifying CAM.
2358 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
2359 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2360 if (notify_cam == 0) {
2362 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2368 * We are done with this ATIO if we successfully sent status.
2369 * In all other cases expect either another CTIO or XPT_ABORT.
2371 if (ok && sentstatus)
2372 isp_put_atpd(isp, bus, atp);
2375 * We're telling CAM we're done with this CTIO transaction.
2377 * 24XX cards never need an ATIO put back.
2379 * Other cards need one put back only on error.
2380 * In the latter case, a timeout will re-fire
2381 * and try again in case we didn't have
2382 * queue resources to do so at first. In any case,
2383 * once the putback is done we do the completion
2386 if (ok || IS_24XX(isp)) {
2387 isp_complete_ctio(ccb);
2389 isp_target_putback_atio(ccb);
2394 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2397 switch (inp->in_status) {
2398 case IN_PORT_LOGOUT:
2400 * XXX: Need to delete this initiator's WWN from the database
2401 * XXX: Need to send this LOGOUT upstream
2403 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2405 case IN_PORT_CHANGED:
2406 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2408 case IN_GLOBAL_LOGO:
2409 isp_del_all_wwn_entries(isp, 0);
2410 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2414 uint16_t nphdl, lun;
2418 isp_notify_t tmp, *nt = &tmp;
2420 if (ISP_CAP_SCCFW(isp)) {
2421 lun = inp->in_scclun;
2422 #if __FreeBSD_version < 1000700
2428 if (ISP_CAP_2KLOGIN(isp)) {
2429 nphdl = ((in_fcentry_e_t *)inp)->in_iid;
2431 nphdl = inp->in_iid;
2433 if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
2440 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx",
2441 inp->in_seqid, (unsigned long long) wwn);
2443 ISP_MEMZERO(nt, sizeof (isp_notify_t));
2445 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2447 nt->nt_nphdl = nphdl;
2449 nt->nt_did = PORT_ANY;
2451 nt->nt_tagval = inp->in_seqid;
2452 nt->nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
2453 nt->nt_need_ack = 1;
2455 nt->nt_ncode = NT_ABORT_TASK;
2456 nt->nt_lreserved = inp;
2457 isp_handle_platform_target_tmf(isp, nt);
2465 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
2470 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2473 uint16_t prli_options = 0;
2477 uint8_t *ptr = (uint8_t *)inot;
2478 uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
2480 nphdl = inot->in_nphdl;
2481 if (nphdl != NIL_HANDLE) {
2482 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2487 switch (inot->in_status) {
2488 case IN24XX_ELS_RCVD:
2491 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2494 * Note that we're just getting notification that an ELS was received
2495 * (possibly with some associated information sent upstream). This is
2496 * *not* the same as being given the ELS frame to accept or reject.
2498 switch (inot->in_status_subcode) {
2501 wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
2502 isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
2509 wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
2510 wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
2511 isp_add_wwn_entry(isp, chan, wwpn, wwnn,
2512 nphdl, portid, prli_options);
2516 prli_options = inot->in_prli_options;
2517 if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
2518 wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
2519 wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
2520 isp_add_wwn_entry(isp, chan, wwpn, wwnn,
2521 nphdl, portid, prli_options);
2530 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2534 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2535 isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
2538 isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
2539 inot->in_rxid, inot->in_oxid);
2540 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2544 case IN24XX_PORT_LOGOUT:
2545 msg = "PORT LOGOUT";
2546 if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2547 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2550 case IN24XX_PORT_CHANGED:
2552 msg = "PORT CHANGED";
2554 case IN24XX_LIP_RESET:
2557 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), msg, inot->in_status_subcode, nphdl);
2560 * All subcodes here are irrelevant. What is relevant
2561 * is that we need to terminate all active commands from
2562 * this initiator (known by N-port handle).
2564 /* XXX IMPLEMENT XXX */
2565 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2568 case IN24XX_SRR_RCVD:
2569 #ifdef ISP_TARGET_MODE
2570 isp_handle_srr_notify(isp, inot);
2577 case IN24XX_LINK_RESET:
2580 case IN24XX_LINK_FAILED:
2582 msg = "LINK FAILED";
2584 isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
2585 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2591 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
2594 if (isp->isp_state != ISP_RUNSTATE) {
2595 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2600 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2602 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2603 ct7_entry_t local, *cto = &local;
2604 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2609 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2610 if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
2615 ISP_MEMZERO(&local, sizeof (local));
2616 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2617 cto->ct_header.rqs_entry_count = 1;
2618 cto->ct_nphdl = nphdl;
2619 cto->ct_rxid = aep->at_rxid;
2620 cto->ct_vpidx = mp->nt_channel;
2621 cto->ct_iid_lo = sid;
2622 cto->ct_iid_hi = sid >> 16;
2623 cto->ct_oxid = aep->at_hdr.ox_id;
2624 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2625 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2627 cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
2628 cto->rsp.m1.ct_resplen = 4;
2629 ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
2630 cto->rsp.m1.ct_resp[0] = rsp & 0xff;
2631 cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
2632 cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
2633 cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
2635 return (isp_target_put_entry(isp, &local));
2639 * This case is for a responding to an ABTS frame
2641 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2644 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2646 if (mp->nt_need_ack) {
2647 uint8_t storage[QENTRY_LEN];
2648 ct7_entry_t *cto = (ct7_entry_t *) storage;
2649 abts_t *abts = (abts_t *)mp->nt_lreserved;
2651 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2652 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2653 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2654 cto->ct_header.rqs_entry_count = 1;
2655 cto->ct_nphdl = mp->nt_nphdl;
2656 cto->ct_rxid = abts->abts_rxid_task;
2657 cto->ct_iid_lo = mp->nt_sid;
2658 cto->ct_iid_hi = mp->nt_sid >> 16;
2659 cto->ct_oxid = abts->abts_ox_id;
2660 cto->ct_vpidx = mp->nt_channel;
2661 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2662 if (isp_target_put_entry(isp, cto)) {
2665 mp->nt_need_ack = 0;
2667 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2675 * Handle logout cases here
2677 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2678 isp_del_all_wwn_entries(isp, mp->nt_channel);
2681 if (mp->nt_ncode == NT_LOGOUT) {
2682 if (!IS_2100(isp) && IS_FC(isp)) {
2683 isp_del_wwn_entries(isp, mp);
2688 * General purpose acknowledgement
2690 if (mp->nt_need_ack) {
2691 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2693 * Don't need to use the guaranteed send because the caller can retry
2695 return (isp_notify_ack(isp, mp->nt_lreserved));
2701 * Handle task management functions.
2703 * We show up here with a notify structure filled out.
2705 * The nt_lreserved tag points to the original queue entry
2708 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2712 struct ccb_immediate_notify *inot;
2713 inot_private_data_t *ntp = NULL;
2716 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2717 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2719 * NB: This assignment is necessary because of tricky type conversion.
2720 * XXX: This is tricky and I need to check this. If the lun isn't known
2721 * XXX: for the task management function, it does not of necessity follow
2722 * XXX: that it should go up stream to the wildcard listener.
2724 if (notify->nt_lun == LUN_ANY) {
2725 lun = CAM_LUN_WILDCARD;
2727 lun = notify->nt_lun;
2729 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2731 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2733 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2737 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2739 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2743 if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2744 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
2745 inot->initiator_id = CAM_TARGET_WILDCARD;
2747 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
2749 inot->seq_id = notify->nt_tagval;
2750 inot->tag_id = notify->nt_tagval >> 32;
2752 switch (notify->nt_ncode) {
2754 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
2755 inot->arg = MSG_ABORT_TASK;
2757 case NT_ABORT_TASK_SET:
2758 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
2759 inot->arg = MSG_ABORT_TASK_SET;
2762 inot->arg = MSG_CLEAR_ACA;
2764 case NT_CLEAR_TASK_SET:
2765 inot->arg = MSG_CLEAR_TASK_SET;
2768 inot->arg = MSG_LOGICAL_UNIT_RESET;
2770 case NT_TARGET_RESET:
2771 inot->arg = MSG_TARGET_RESET;
2773 case NT_QUERY_TASK_SET:
2774 inot->arg = MSG_QUERY_TASK_SET;
2776 case NT_QUERY_ASYNC_EVENT:
2777 inot->arg = MSG_QUERY_ASYNC_EVENT;
2780 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);
2784 ntp = isp_get_ntpd(isp, notify->nt_channel);
2786 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2789 ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
2790 if (notify->nt_lreserved) {
2791 ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
2792 ntp->nt.nt_lreserved = &ntp->data;
2794 ntp->seq_id = notify->nt_tagval;
2795 ntp->tag_id = notify->nt_tagval >> 32;
2798 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2799 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2800 inot->ccb_h.status = CAM_MESSAGE_RECV;
2801 xpt_done((union ccb *)inot);
2804 if (notify->nt_need_ack && notify->nt_lreserved) {
2805 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2806 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2807 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2810 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
2816 isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
2818 atio_private_data_t *atp, *atpool;
2819 inot_private_data_t *ntp, *tmp;
2820 uint32_t this_tag_id;
2823 * First, clean any commands pending restart
2825 STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
2827 this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
2829 this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
2830 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2831 isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
2833 isp_put_ntpd(isp, chan, ntp);
2834 STAILQ_REMOVE(&tptr->restart_queue, ntp,
2835 inot_private_data, next);
2840 * Now mark other ones dead as well.
2842 ISP_GET_PC(isp, chan, atpool, atpool);
2843 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
2844 if (atp->lun != tptr->ts_lun)
2846 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
2853 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2855 struct cam_sim *sim;
2859 sim = (struct cam_sim *)cbarg;
2860 isp = (ispsoftc_t *) cam_sim_softc(sim);
2861 bus = cam_sim_bus(sim);
2862 tgt = xpt_path_target_id(path);
2865 case AC_LOST_DEVICE:
2867 uint16_t oflags, nflags;
2868 sdparam *sdp = SDPARAM(isp, bus);
2871 nflags = sdp->isp_devparam[tgt].nvrm_flags;
2872 nflags &= DPARM_SAFE_DFLT;
2873 if (isp->isp_loaded_fw) {
2874 nflags |= DPARM_NARROW | DPARM_ASYNC;
2876 oflags = sdp->isp_devparam[tgt].goal_flags;
2877 sdp->isp_devparam[tgt].goal_flags = nflags;
2878 sdp->isp_devparam[tgt].dev_update = 1;
2880 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
2881 sdp->isp_devparam[tgt].goal_flags = oflags;
2886 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2892 isp_poll(struct cam_sim *sim)
2894 ispsoftc_t *isp = cam_sim_softc(sim);
2895 uint16_t isr, sema, info;
2897 if (ISP_READ_ISR(isp, &isr, &sema, &info))
2898 isp_intr(isp, isr, sema, info);
2903 isp_watchdog(void *arg)
2905 struct ccb_scsiio *xs = arg;
2907 uint32_t ohandle = ISP_HANDLE_FREE, handle;
2911 handle = isp_find_handle(isp, xs);
2914 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2916 if (handle != ISP_HANDLE_FREE) {
2917 uint16_t isr, sema, info;
2918 if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
2919 isp_intr(isp, isr, sema, info);
2921 handle = isp_find_handle(isp, xs);
2923 if (handle != ISP_HANDLE_FREE) {
2925 * Try and make sure the command is really dead before
2926 * we release the handle (and DMA resources) for reuse.
2928 * If we are successful in aborting the command then
2929 * we're done here because we'll get the command returned
2932 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2937 * Note that after calling the above, the command may in
2938 * fact have been completed.
2940 xs = isp_find_xs(isp, handle);
2943 * If the command no longer exists, then we won't
2944 * be able to find the xs again with this handle.
2951 * After this point, the command is really dead.
2953 if (XS_XFRLEN(xs)) {
2954 ISP_DMAFREE(isp, xs, handle);
2956 isp_destroy_handle(isp, handle);
2957 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2958 xs->ccb_h.status &= ~CAM_STATUS_MASK;
2959 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
2960 isp_prt_endcmd(isp, xs);
2963 if (ohandle != ISP_HANDLE_FREE) {
2964 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2966 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2972 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2975 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2978 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2980 ccb = xpt_alloc_ccb_nowait();
2982 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2985 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2986 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2987 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2995 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2997 struct cam_path *tp;
2998 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3000 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3001 xpt_async(AC_LOST_DEVICE, tp, NULL);
3007 * Gone Device Timer Function- when we have decided that a device has gone
3008 * away, we wait a specific period of time prior to telling the OS it has
3011 * This timer function fires once a second and then scans the port database
3012 * for devices that are marked dead but still have a virtual target assigned.
3013 * We decrement a counter for that port database entry, and when it hits zero,
3014 * we tell the OS the device has gone away.
3019 struct isp_fc *fc = arg;
3020 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3024 isp_gdt_task(void *arg, int pending)
3026 struct isp_fc *fc = arg;
3027 ispsoftc_t *isp = fc->isp;
3028 int chan = fc - isp->isp_osinfo.pc.fc;
3030 struct ac_contract ac;
3031 struct ac_device_changed *adc;
3032 int dbidx, more_to_do = 0;
3035 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3036 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3037 lp = &FCPARAM(isp, chan)->portdb[dbidx];
3039 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3042 if (lp->gone_timer != 0) {
3043 lp->gone_timer -= 1;
3047 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3048 if (lp->is_target) {
3050 isp_make_gone(isp, lp, chan, dbidx);
3052 if (lp->is_initiator) {
3053 lp->is_initiator = 0;
3054 ac.contract_number = AC_CONTRACT_DEV_CHG;
3055 adc = (struct ac_device_changed *) ac.contract_data;
3056 adc->wwpn = lp->port_wwn;
3057 adc->port = lp->portid;
3058 adc->target = dbidx;
3060 xpt_async(AC_CONTRACT, fc->path, &ac);
3062 lp->state = FC_PORTDB_STATE_NIL;
3066 callout_reset(&fc->gdt, hz, isp_gdt, fc);
3068 callout_deactivate(&fc->gdt);
3069 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3076 * When loop goes down we remember the time and freeze CAM command queue.
3077 * During some time period we are trying to reprobe the loop. But if we
3078 * fail, we tell the OS that devices have gone away and drop the freeze.
3080 * We don't clear the devices out of our port database because, when loop
3081 * come back up, we have to do some actual cleanup with the chip at that
3082 * point (implicit PLOGO, e.g., to get the chip's port database state right).
3085 isp_loop_changed(ispsoftc_t *isp, int chan)
3087 fcparam *fcp = FCPARAM(isp, chan);
3088 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3090 if (fc->loop_down_time)
3092 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
3093 if (fcp->role & ISP_ROLE_INITIATOR)
3094 isp_freeze_loopdown(isp, chan);
3096 fc->loop_down_time = time_uptime;
3101 isp_loop_up(ispsoftc_t *isp, int chan)
3103 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3105 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
3106 fc->loop_seen_once = 1;
3108 fc->loop_down_time = 0;
3109 isp_unfreeze_loopdown(isp, chan);
3113 isp_loop_dead(ispsoftc_t *isp, int chan)
3115 fcparam *fcp = FCPARAM(isp, chan);
3116 struct isp_fc *fc = ISP_FC_PC(isp, chan);
3118 struct ac_contract ac;
3119 struct ac_device_changed *adc;
3122 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
3125 * Notify to the OS all targets who we now consider have departed.
3127 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3128 lp = &fcp->portdb[dbidx];
3130 if (lp->state == FC_PORTDB_STATE_NIL)
3134 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3136 for (i = 0; i < isp->isp_maxcmds; i++) {
3137 struct ccb_scsiio *xs;
3139 if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
3142 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3145 if (dbidx != XS_TGT(xs)) {
3148 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
3149 isp->isp_xflist[i].handle, chan, XS_TGT(xs),
3150 (uintmax_t)XS_LUN(xs));
3153 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3154 if (lp->is_target) {
3156 isp_make_gone(isp, lp, chan, dbidx);
3158 if (lp->is_initiator) {
3159 lp->is_initiator = 0;
3160 ac.contract_number = AC_CONTRACT_DEV_CHG;
3161 adc = (struct ac_device_changed *) ac.contract_data;
3162 adc->wwpn = lp->port_wwn;
3163 adc->port = lp->portid;
3164 adc->target = dbidx;
3166 xpt_async(AC_CONTRACT, fc->path, &ac);
3170 isp_unfreeze_loopdown(isp, chan);
3172 fc->loop_down_time = 0;
3176 isp_kthread(void *arg)
3178 struct isp_fc *fc = arg;
3179 ispsoftc_t *isp = fc->isp;
3180 int chan = fc - isp->isp_osinfo.pc.fc;
3184 mtx_lock(&isp->isp_osinfo.lock);
3186 while (isp->isp_osinfo.is_exiting == 0) {
3187 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3188 "Chan %d Checking FC state", chan);
3189 lb = isp_fc_runstate(isp, chan, 250000);
3190 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3191 "Chan %d FC got to %s state", chan,
3192 isp_fc_loop_statename(lb));
3195 * Our action is different based upon whether we're supporting
3196 * Initiator mode or not. If we are, we might freeze the simq
3197 * when loop is down and set all sorts of different delays to
3200 * If not, we simply just wait for loop to come up.
3202 if (lb == LOOP_READY || lb < 0) {
3206 * If we've never seen loop up and we've waited longer
3207 * than quickboot time, or we've seen loop up but we've
3208 * waited longer than loop_down_limit, give up and go
3209 * to sleep until loop comes up.
3211 if (fc->loop_seen_once == 0)
3212 lim = isp_quickboot_time;
3214 lim = fc->loop_down_limit;
3215 d = time_uptime - fc->loop_down_time;
3231 if (lb == LOOP_READY)
3232 isp_loop_up(isp, chan);
3234 isp_loop_dead(isp, chan);
3237 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
3238 "Chan %d sleep for %d seconds", chan, slp);
3239 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
3241 fc->num_threads -= 1;
3242 mtx_unlock(&isp->isp_osinfo.lock);
3246 #ifdef ISP_TARGET_MODE
3248 isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
3250 atio_private_data_t *atp;
3251 union ccb *accb = ccb->cab.abort_ccb;
3252 struct ccb_hdr *sccb;
3255 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3257 /* Search for the ATIO among queueued. */
3258 SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
3259 if (sccb != &accb->ccb_h)
3261 SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
3263 accb->ccb_h.status = CAM_REQ_ABORTED;
3265 ccb->ccb_h.status = CAM_REQ_CMP;
3270 /* Search for the ATIO among running. */
3271 atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
3273 /* Send TERMINATE to firmware. */
3274 if (!atp->dead && IS_24XX(isp)) {
3275 uint8_t storage[QENTRY_LEN];
3276 ct7_entry_t *cto = (ct7_entry_t *) storage;
3278 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
3279 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3280 cto->ct_header.rqs_entry_count = 1;
3281 cto->ct_nphdl = atp->nphdl;
3282 cto->ct_rxid = atp->tag;
3283 cto->ct_iid_lo = atp->sid;
3284 cto->ct_iid_hi = atp->sid >> 16;
3285 cto->ct_oxid = atp->oxid;
3286 cto->ct_vpidx = XS_CHANNEL(accb);
3287 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
3288 isp_target_put_entry(isp, cto);
3290 isp_put_atpd(isp, XS_CHANNEL(accb), atp);
3291 ccb->ccb_h.status = CAM_REQ_CMP;
3293 ccb->ccb_h.status = CAM_UA_ABORT;
3298 isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
3300 inot_private_data_t *ntp;
3301 union ccb *accb = ccb->cab.abort_ccb;
3302 struct ccb_hdr *sccb;
3305 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3307 /* Search for the INOT among queueued. */
3308 SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
3309 if (sccb != &accb->ccb_h)
3311 SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
3313 accb->ccb_h.status = CAM_REQ_ABORTED;
3315 ccb->ccb_h.status = CAM_REQ_CMP;
3320 /* Search for the INOT among running. */
3321 ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
3323 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, ntp->data);
3324 isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
3325 ccb->ccb_h.status = CAM_REQ_CMP;
3327 ccb->ccb_h.status = CAM_UA_ABORT;
3334 isp_action(struct cam_sim *sim, union ccb *ccb)
3336 int bus, tgt, ts, error;
3338 struct ccb_trans_settings *cts;
3340 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
3342 isp = (ispsoftc_t *)cam_sim_softc(sim);
3343 mtx_assert(&isp->isp_lock, MA_OWNED);
3344 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
3345 ISP_PCMD(ccb) = NULL;
3347 switch (ccb->ccb_h.func_code) {
3348 case XPT_SCSI_IO: /* Execute the requested I/O operation */
3349 bus = XS_CHANNEL(ccb);
3351 * Do a couple of preliminary checks...
3353 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3354 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3355 ccb->ccb_h.status = CAM_REQ_INVALID;
3356 isp_done((struct ccb_scsiio *) ccb);
3360 ccb->csio.req_map = NULL;
3362 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3363 xpt_print(ccb->ccb_h.path, "invalid target\n");
3364 ccb->ccb_h.status = CAM_PATH_INVALID;
3365 } else if (ISP_MAX_LUNS(isp) > 0 &&
3366 ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3367 xpt_print(ccb->ccb_h.path, "invalid lun\n");
3368 ccb->ccb_h.status = CAM_PATH_INVALID;
3370 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3375 ccb->csio.scsi_status = SCSI_STATUS_OK;
3376 if (isp_get_pcmd(isp, ccb)) {
3377 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
3378 cam_freeze_devq(ccb->ccb_h.path);
3379 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
3380 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3384 error = isp_start((XS_T *) ccb);
3387 ccb->ccb_h.status |= CAM_SIM_QUEUED;
3388 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
3391 ts = ccb->ccb_h.timeout;
3392 if (ts == CAM_TIME_DEFAULT) {
3395 ts = isp_mstohz(ts);
3396 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3400 * We get this result if the loop isn't ready
3401 * or if the device in question has gone zombie.
3403 if (ISP_FC_PC(isp, bus)->loop_dead) {
3404 isp_prt(isp, ISP_LOGDEBUG0,
3405 "%d.%jx loop is dead",
3406 XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3407 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
3408 isp_done((struct ccb_scsiio *) ccb);
3411 isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
3412 XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3413 cam_freeze_devq(ccb->ccb_h.path);
3414 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3415 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3416 isp_free_pcmd(isp, ccb);
3420 isp_free_pcmd(isp, ccb);
3421 cam_freeze_devq(ccb->ccb_h.path);
3422 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3423 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3427 isp_done((struct ccb_scsiio *) ccb);
3430 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3431 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3432 isp_free_pcmd(isp, ccb);
3437 #ifdef ISP_TARGET_MODE
3438 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
3439 if (ccb->cel.enable) {
3440 isp_enable_lun(isp, ccb);
3442 isp_disable_lun(isp, ccb);
3445 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
3446 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
3448 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3452 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
3453 str = "XPT_IMMEDIATE_NOTIFY";
3455 str = "XPT_ACCEPT_TARGET_IO";
3456 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
3457 "%s: no state pointer found for %s\n",
3459 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3463 ccb->ccb_h.spriv_field0 = 0;
3464 ccb->ccb_h.spriv_ptr1 = isp;
3466 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3467 ccb->atio.tag_id = 0;
3469 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3470 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3471 "Put FREE ATIO, count now %d\n", tptr->atio_count);
3472 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3473 ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3475 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3476 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3477 "Put FREE INOT, count now %d\n", tptr->inot_count);
3479 ccb->ccb_h.status = CAM_REQ_INPROG;
3482 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
3484 inot_private_data_t *ntp;
3487 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
3488 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
3491 * All the relevant path information is in the associated immediate notify
3493 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);
3494 ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
3496 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__,
3497 ccb->cna2.tag_id, ccb->cna2.seq_id);
3498 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3502 if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
3503 (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
3504 cam_freeze_devq(ccb->ccb_h.path);
3505 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3506 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3507 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
3510 isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
3511 ccb->ccb_h.status = CAM_REQ_CMP;
3512 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);
3516 case XPT_CONT_TARGET_IO:
3517 isp_target_start_ctio(isp, ccb, FROM_CAM);
3520 case XPT_RESET_DEV: /* BDR the specified SCSI device */
3521 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3522 tgt = ccb->ccb_h.target_id;
3525 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3527 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3530 * If we have a FC device, reset the Command
3531 * Reference Number, because the target will expect
3532 * that we re-start the CRN at 1 after a reset.
3535 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3537 ccb->ccb_h.status = CAM_REQ_CMP;
3541 case XPT_ABORT: /* Abort the specified CCB */
3543 union ccb *accb = ccb->cab.abort_ccb;
3544 switch (accb->ccb_h.func_code) {
3545 #ifdef ISP_TARGET_MODE
3546 case XPT_ACCEPT_TARGET_IO:
3547 isp_abort_atio(isp, ccb);
3549 case XPT_IMMEDIATE_NOTIFY:
3550 isp_abort_inot(isp, ccb);
3554 error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3556 ccb->ccb_h.status = CAM_UA_ABORT;
3558 ccb->ccb_h.status = CAM_REQ_CMP;
3562 ccb->ccb_h.status = CAM_REQ_INVALID;
3566 * This is not a queued CCB, so the caller expects it to be
3567 * complete when control is returned.
3571 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
3572 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
3574 if (!IS_CURRENT_SETTINGS(cts)) {
3575 ccb->ccb_h.status = CAM_REQ_INVALID;
3579 tgt = cts->ccb_h.target_id;
3580 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3582 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3583 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3584 sdparam *sdp = SDPARAM(isp, bus);
3587 if (spi->valid == 0 && scsi->valid == 0) {
3588 ccb->ccb_h.status = CAM_REQ_CMP;
3594 * We always update (internally) from goal_flags
3595 * so any request to change settings just gets
3596 * vectored to that location.
3598 dptr = &sdp->isp_devparam[tgt].goal_flags;
3600 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3601 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3602 *dptr |= DPARM_DISC;
3604 *dptr &= ~DPARM_DISC;
3607 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3608 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3609 *dptr |= DPARM_TQING;
3611 *dptr &= ~DPARM_TQING;
3614 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3615 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3616 *dptr |= DPARM_WIDE;
3618 *dptr &= ~DPARM_WIDE;
3624 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3625 *dptr |= DPARM_SYNC;
3627 * XXX: CHECK FOR LEGALITY
3629 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
3630 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3632 *dptr &= ~DPARM_SYNC;
3634 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,
3635 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3636 sdp->isp_devparam[tgt].dev_update = 1;
3639 ccb->ccb_h.status = CAM_REQ_CMP;
3642 case XPT_GET_TRAN_SETTINGS:
3644 tgt = cts->ccb_h.target_id;
3645 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3647 fcparam *fcp = FCPARAM(isp, bus);
3648 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3649 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3651 cts->protocol = PROTO_SCSI;
3652 cts->protocol_version = SCSI_REV_2;
3653 cts->transport = XPORT_FC;
3654 cts->transport_version = 0;
3656 scsi->valid = CTS_SCSI_VALID_TQ;
3657 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3658 fc->valid = CTS_FC_VALID_SPEED;
3659 fc->bitrate = 100000;
3660 fc->bitrate *= fcp->isp_gbspeed;
3661 if (tgt < MAX_FC_TARG) {
3662 fcportdb_t *lp = &fcp->portdb[tgt];
3663 fc->wwnn = lp->node_wwn;
3664 fc->wwpn = lp->port_wwn;
3665 fc->port = lp->portid;
3666 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3669 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3670 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3671 sdparam *sdp = SDPARAM(isp, bus);
3672 uint16_t dval, pval, oval;
3674 if (IS_CURRENT_SETTINGS(cts)) {
3675 sdp->isp_devparam[tgt].dev_refresh = 1;
3677 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3678 dval = sdp->isp_devparam[tgt].actv_flags;
3679 oval = sdp->isp_devparam[tgt].actv_offset;
3680 pval = sdp->isp_devparam[tgt].actv_period;
3682 dval = sdp->isp_devparam[tgt].nvrm_flags;
3683 oval = sdp->isp_devparam[tgt].nvrm_offset;
3684 pval = sdp->isp_devparam[tgt].nvrm_period;
3687 cts->protocol = PROTO_SCSI;
3688 cts->protocol_version = SCSI_REV_2;
3689 cts->transport = XPORT_SPI;
3690 cts->transport_version = 2;
3696 if (dval & DPARM_DISC) {
3697 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3699 if ((dval & DPARM_SYNC) && oval && pval) {
3700 spi->sync_offset = oval;
3701 spi->sync_period = pval;
3703 spi->sync_offset = 0;
3704 spi->sync_period = 0;
3706 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3707 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3708 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3709 if (dval & DPARM_WIDE) {
3710 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3712 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3714 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3715 scsi->valid = CTS_SCSI_VALID_TQ;
3716 if (dval & DPARM_TQING) {
3717 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3719 spi->valid |= CTS_SPI_VALID_DISC;
3721 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3722 bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3724 ccb->ccb_h.status = CAM_REQ_CMP;
3728 case XPT_CALC_GEOMETRY:
3729 cam_calc_geometry(&ccb->ccg, 1);
3733 case XPT_RESET_BUS: /* Reset the specified bus */
3734 bus = cam_sim_bus(sim);
3735 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
3737 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3742 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
3745 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
3747 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
3749 ccb->ccb_h.status = CAM_REQ_CMP;
3753 case XPT_TERM_IO: /* Terminate the I/O process */
3754 ccb->ccb_h.status = CAM_REQ_INVALID;
3758 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
3760 struct ccb_sim_knob *kp = &ccb->knob;
3764 ccb->ccb_h.status = CAM_REQ_INVALID;
3769 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3770 fcp = FCPARAM(isp, bus);
3772 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
3773 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
3774 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
3775 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
3777 ccb->ccb_h.status = CAM_REQ_CMP;
3778 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
3782 switch (kp->xport_specific.fc.role) {
3783 case KNOB_ROLE_NONE:
3784 if (fcp->role != ISP_ROLE_NONE) {
3786 newrole = ISP_ROLE_NONE;
3789 case KNOB_ROLE_TARGET:
3790 if (fcp->role != ISP_ROLE_TARGET) {
3792 newrole = ISP_ROLE_TARGET;
3795 case KNOB_ROLE_INITIATOR:
3796 if (fcp->role != ISP_ROLE_INITIATOR) {
3798 newrole = ISP_ROLE_INITIATOR;
3801 case KNOB_ROLE_BOTH:
3802 if (fcp->role != ISP_ROLE_BOTH) {
3804 newrole = ISP_ROLE_BOTH;
3809 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
3810 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
3811 bus, newrole) != 0) {
3812 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3821 case XPT_GET_SIM_KNOB_OLD: /* Get SIM knobs -- compat value */
3822 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
3824 struct ccb_sim_knob *kp = &ccb->knob;
3829 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3830 fcp = FCPARAM(isp, bus);
3832 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
3833 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
3834 switch (fcp->role) {
3836 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3838 case ISP_ROLE_TARGET:
3839 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3841 case ISP_ROLE_INITIATOR:
3842 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3845 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3848 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3849 ccb->ccb_h.status = CAM_REQ_CMP;
3851 ccb->ccb_h.status = CAM_REQ_INVALID;
3856 case XPT_PATH_INQ: /* Path routing inquiry */
3858 struct ccb_pathinq *cpi = &ccb->cpi;
3860 cpi->version_num = 1;
3861 #ifdef ISP_TARGET_MODE
3862 if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3863 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3866 cpi->target_sprt = 0;
3867 cpi->hba_eng_cnt = 0;
3868 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
3869 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
3870 255 : ISP_MAX_LUNS(isp) - 1;
3871 cpi->bus_id = cam_sim_bus(sim);
3872 if (isp->isp_osinfo.sixtyfourbit)
3873 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
3875 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
3877 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
3879 fcparam *fcp = FCPARAM(isp, bus);
3881 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3882 #if __FreeBSD_version >= 1000700
3883 cpi->hba_misc |= PIM_EXTLUNS;
3885 #if __FreeBSD_version >= 1000039
3886 cpi->hba_misc |= PIM_NOSCAN;
3890 * Because our loop ID can shift from time to time,
3891 * make our initiator ID out of range of our bus.
3893 cpi->initiator_id = cpi->max_target + 1;
3896 * Set base transfer capabilities for Fibre Channel, for this HBA.
3899 cpi->base_transfer_speed = 8000000;
3900 } else if (IS_24XX(isp)) {
3901 cpi->base_transfer_speed = 4000000;
3902 } else if (IS_23XX(isp)) {
3903 cpi->base_transfer_speed = 2000000;
3905 cpi->base_transfer_speed = 1000000;
3907 cpi->hba_inquiry = PI_TAG_ABLE;
3908 cpi->transport = XPORT_FC;
3909 cpi->transport_version = 0;
3910 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
3911 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
3912 cpi->xport_specific.fc.port = fcp->isp_portid;
3913 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3915 sdparam *sdp = SDPARAM(isp, bus);
3916 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3917 cpi->hba_misc = PIM_UNMAPPED;
3918 cpi->initiator_id = sdp->isp_initiator_id;
3919 cpi->base_transfer_speed = 3300;
3920 cpi->transport = XPORT_SPI;
3921 cpi->transport_version = 2;
3923 cpi->protocol = PROTO_SCSI;
3924 cpi->protocol_version = SCSI_REV_2;
3925 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3926 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
3927 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3928 cpi->unit_number = cam_sim_unit(sim);
3929 cpi->ccb_h.status = CAM_REQ_CMP;
3934 ccb->ccb_h.status = CAM_REQ_INVALID;
3940 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
3943 isp_done(XS_T *sccb)
3945 ispsoftc_t *isp = XS_ISP(sccb);
3949 XS_SETERR(sccb, CAM_REQ_CMP);
3951 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3952 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
3953 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
3954 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
3956 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3960 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3961 status = sccb->ccb_h.status & CAM_STATUS_MASK;
3962 if (status != CAM_REQ_CMP) {
3963 if (status != CAM_SEL_TIMEOUT)
3964 isp_prt(isp, ISP_LOGDEBUG0,
3965 "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
3966 XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
3967 sccb->ccb_h.status, sccb->scsi_status);
3968 else if ((IS_FC(isp))
3969 && (XS_TGT(sccb) < MAX_FC_TARG)) {
3972 fcp = FCPARAM(isp, XS_CHANNEL(sccb));
3973 fcp->portdb[XS_TGT(sccb)].is_target = 0;
3975 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3976 sccb->ccb_h.status |= CAM_DEV_QFRZN;
3977 xpt_freeze_devq(sccb->ccb_h.path, 1);
3981 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3982 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
3985 if (ISP_PCMD(sccb)) {
3986 if (callout_active(&PISP_PCMD(sccb)->wdog))
3987 callout_stop(&PISP_PCMD(sccb)->wdog);
3988 isp_free_pcmd(isp, (union ccb *) sccb);
3990 xpt_done((union ccb *) sccb);
3994 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3997 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
4003 struct cam_path *tmppath;
4004 struct ac_contract ac;
4005 struct ac_device_changed *adc;
4009 case ISPASYNC_NEW_TGT_PARAMS:
4011 struct ccb_trans_settings_scsi *scsi;
4012 struct ccb_trans_settings_spi *spi;
4015 struct ccb_trans_settings cts;
4017 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4020 bus = va_arg(ap, int);
4021 tgt = va_arg(ap, int);
4023 sdp = SDPARAM(isp, bus);
4025 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4026 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4029 flags = sdp->isp_devparam[tgt].actv_flags;
4030 cts.type = CTS_TYPE_CURRENT_SETTINGS;
4031 cts.protocol = PROTO_SCSI;
4032 cts.transport = XPORT_SPI;
4034 scsi = &cts.proto_specific.scsi;
4035 spi = &cts.xport_specific.spi;
4037 if (flags & DPARM_TQING) {
4038 scsi->valid |= CTS_SCSI_VALID_TQ;
4039 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4042 if (flags & DPARM_DISC) {
4043 spi->valid |= CTS_SPI_VALID_DISC;
4044 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4046 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4047 if (flags & DPARM_WIDE) {
4048 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4050 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4052 if (flags & DPARM_SYNC) {
4053 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4054 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4055 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
4056 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4058 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);
4059 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4060 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4061 xpt_free_path(tmppath);
4064 case ISPASYNC_BUS_RESET:
4067 bus = va_arg(ap, int);
4069 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
4071 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
4073 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
4079 msg = "LIP Received";
4081 case ISPASYNC_LOOP_RESET:
4085 case ISPASYNC_LOOP_DOWN:
4089 bus = va_arg(ap, int);
4091 isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
4092 isp_loop_changed(isp, bus);
4093 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
4095 case ISPASYNC_LOOP_UP:
4097 bus = va_arg(ap, int);
4099 isp_loop_changed(isp, bus);
4100 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
4102 case ISPASYNC_DEV_ARRIVED:
4104 bus = va_arg(ap, int);
4105 lp = va_arg(ap, fcportdb_t *);
4107 fc = ISP_FC_PC(isp, bus);
4108 tgt = FC_PORTDB_TGT(isp, bus, lp);
4109 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4110 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4111 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4112 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4114 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4115 isp_make_here(isp, lp, bus, tgt);
4117 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4118 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4119 lp->is_initiator = 1;
4120 ac.contract_number = AC_CONTRACT_DEV_CHG;
4121 adc = (struct ac_device_changed *) ac.contract_data;
4122 adc->wwpn = lp->port_wwn;
4123 adc->port = lp->portid;
4126 xpt_async(AC_CONTRACT, fc->path, &ac);
4129 case ISPASYNC_DEV_CHANGED:
4131 bus = va_arg(ap, int);
4132 lp = va_arg(ap, fcportdb_t *);
4134 fc = ISP_FC_PC(isp, bus);
4135 tgt = FC_PORTDB_TGT(isp, bus, lp);
4136 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4137 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4139 if (lp->is_target !=
4140 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4141 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4142 lp->is_target = !lp->is_target;
4143 if (lp->is_target) {
4144 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4145 isp_make_here(isp, lp, bus, tgt);
4147 isp_make_gone(isp, lp, bus, tgt);
4148 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
4151 if (lp->is_initiator !=
4152 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4153 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4154 lp->is_initiator = !lp->is_initiator;
4155 ac.contract_number = AC_CONTRACT_DEV_CHG;
4156 adc = (struct ac_device_changed *) ac.contract_data;
4157 adc->wwpn = lp->port_wwn;
4158 adc->port = lp->portid;
4160 adc->arrived = lp->is_initiator;
4161 xpt_async(AC_CONTRACT, fc->path, &ac);
4164 case ISPASYNC_DEV_STAYED:
4166 bus = va_arg(ap, int);
4167 lp = va_arg(ap, fcportdb_t *);
4169 fc = ISP_FC_PC(isp, bus);
4170 tgt = FC_PORTDB_TGT(isp, bus, lp);
4171 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4172 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4174 case ISPASYNC_DEV_GONE:
4176 bus = va_arg(ap, int);
4177 lp = va_arg(ap, fcportdb_t *);
4179 fc = ISP_FC_PC(isp, bus);
4180 tgt = FC_PORTDB_TGT(isp, bus, lp);
4182 * If this has a virtual target or initiator set the isp_gdt
4183 * timer running on it to delay its departure.
4185 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4186 if (lp->is_target || lp->is_initiator) {
4187 lp->state = FC_PORTDB_STATE_ZOMBIE;
4188 lp->gone_timer = fc->gone_device_time;
4189 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4190 if (fc->ready && !callout_active(&fc->gdt)) {
4191 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);
4192 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4196 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
4198 case ISPASYNC_CHANGE_NOTIFY:
4201 int evt, nphdl, nlstate, portid, reason;
4204 bus = va_arg(ap, int);
4205 evt = va_arg(ap, int);
4206 if (evt == ISPASYNC_CHANGE_PDB) {
4207 nphdl = va_arg(ap, int);
4208 nlstate = va_arg(ap, int);
4209 reason = va_arg(ap, int);
4210 } else if (evt == ISPASYNC_CHANGE_SNS) {
4211 portid = va_arg(ap, int);
4214 nlstate = reason = 0;
4217 fc = ISP_FC_PC(isp, bus);
4219 if (evt == ISPASYNC_CHANGE_PDB) {
4220 msg = "Port Database Changed";
4221 isp_prt(isp, ISP_LOGINFO,
4222 "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
4223 bus, msg, nphdl, nlstate, reason);
4224 } else if (evt == ISPASYNC_CHANGE_SNS) {
4225 msg = "Name Server Database Changed";
4226 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
4229 msg = "Other Change Notify";
4230 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
4232 isp_loop_changed(isp, bus);
4235 #ifdef ISP_TARGET_MODE
4236 case ISPASYNC_TARGET_NOTIFY:
4238 isp_notify_t *notify;
4240 notify = va_arg(ap, isp_notify_t *);
4242 switch (notify->nt_ncode) {
4244 case NT_ABORT_TASK_SET:
4246 case NT_CLEAR_TASK_SET:
4248 case NT_TARGET_RESET:
4249 case NT_QUERY_TASK_SET:
4250 case NT_QUERY_ASYNC_EVENT:
4252 * These are task management functions.
4254 isp_handle_platform_target_tmf(isp, notify);
4262 * No action need be taken here.
4265 case NT_GLOBAL_LOGOUT:
4268 * This is device arrival/departure notification
4270 isp_handle_platform_target_notify_ack(isp, notify, 0);
4273 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
4274 isp_handle_platform_target_notify_ack(isp, notify, 0);
4279 case ISPASYNC_TARGET_NOTIFY_ACK:
4283 inot = va_arg(ap, void *);
4285 if (isp_notify_ack(isp, inot)) {
4286 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
4290 memcpy(tp->data, inot, sizeof (tp->data));
4295 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
4296 callout_reset(&tp->timer, 5,
4297 isp_refire_notify_ack, tp);
4299 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
4304 case ISPASYNC_TARGET_ACTION:
4309 hp = va_arg(ap, isphdr_t *);
4311 switch (hp->rqs_entry_type) {
4313 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
4315 case RQSTYPE_NOTIFY:
4317 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
4319 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
4323 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
4326 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
4332 isp_handle_platform_ctio(isp, hp);
4334 case RQSTYPE_ABTS_RCVD:
4336 abts_t *abts = (abts_t *)hp;
4337 isp_notify_t notify, *nt = ¬ify;
4338 atio_private_data_t *atp;
4343 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
4344 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
4345 ISP_MEMZERO(nt, sizeof (isp_notify_t));
4349 nt->nt_nphdl = abts->abts_nphdl;
4351 isp_find_chan_by_did(isp, did, &chan);
4352 if (chan == ISP_NOCHAN) {
4353 nt->nt_tgt = TGT_ANY;
4355 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
4356 if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
4357 nt->nt_wwn = lp->port_wwn;
4359 nt->nt_wwn = INI_ANY;
4363 * Try hard to find the lun for this command.
4365 atp = isp_find_atpd(isp, chan, abts->abts_rxid_task);
4366 nt->nt_lun = atp ? atp->lun : LUN_ANY;
4367 nt->nt_need_ack = 1;
4368 nt->nt_tagval = abts->abts_rxid_task;
4369 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
4370 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
4371 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
4372 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
4374 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
4375 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
4377 nt->nt_channel = chan;
4378 nt->nt_ncode = NT_ABORT_TASK;
4379 nt->nt_lreserved = hp;
4380 isp_handle_platform_target_tmf(isp, nt);
4387 case ISPASYNC_FW_CRASH:
4389 uint16_t mbox1, mbox6;
4390 mbox1 = ISP_READ(isp, OUTMAILBOX1);
4391 if (IS_DUALBUS(isp)) {
4392 mbox6 = ISP_READ(isp, OUTMAILBOX6);
4396 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
4397 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
4398 isp->isp_osinfo.mbox_sleep_ok = 0;
4400 isp->isp_osinfo.mbox_sleep_ok = mbox1;
4401 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4405 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4412 * Locks are held before coming here.
4415 isp_uninit(ispsoftc_t *isp)
4418 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
4420 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
4422 ISP_DISABLE_INTS(isp);
4426 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
4429 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4431 /* First try to use explicitly configured WWNs. */
4432 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4436 /* Otherwise try to use WWNs from NVRAM. */
4438 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4439 FCPARAM(isp, chan)->isp_wwpn_nvram;
4444 /* If still no WWNs, try to steal them from the first channel. */
4446 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4447 ISP_FC_PC(isp, 0)->def_wwpn;
4449 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4450 FCPARAM(isp, 0)->isp_wwpn_nvram;
4454 /* If still nothing -- improvise. */
4456 seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4458 seed ^= 0x0100000000000000ULL;
4461 /* For additional channels we have to improvise even more. */
4462 if (!iswwnn && chan > 0) {
4464 * We'll stick our channel number plus one first into bits
4465 * 57..59 and thence into bits 52..55 which allows for 8 bits
4466 * of channel which is enough for our maximum of 255 channels.
4468 seed ^= 0x0100000000000000ULL;
4469 seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4470 seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
4476 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4482 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4485 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4488 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4490 printf("%s\n", lbuf);
4494 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4497 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4500 xpt_print_path(xs->ccb_h.path);
4508 isp_nanotime_sub(struct timespec *b, struct timespec *a)
4511 struct timespec x = *b;
4513 elapsed = GET_NANOSEC(&x);
4520 isp_mbox_acquire(ispsoftc_t *isp)
4522 if (isp->isp_osinfo.mboxbsy) {
4525 isp->isp_osinfo.mboxcmd_done = 0;
4526 isp->isp_osinfo.mboxbsy = 1;
4532 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4534 unsigned int usecs = mbp->timeout;
4535 unsigned int max, olim, ilim;
4538 usecs = MBCMD_DEFAULT_TIMEOUT;
4540 max = isp->isp_mbxwrk0 + 1;
4542 if (isp->isp_osinfo.mbox_sleep_ok) {
4543 unsigned int ms = (usecs + 999) / 1000;
4545 isp->isp_osinfo.mbox_sleep_ok = 0;
4546 isp->isp_osinfo.mbox_sleeping = 1;
4547 for (olim = 0; olim < max; olim++) {
4548 msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
4549 if (isp->isp_osinfo.mboxcmd_done) {
4553 isp->isp_osinfo.mbox_sleep_ok = 1;
4554 isp->isp_osinfo.mbox_sleeping = 0;
4556 for (olim = 0; olim < max; olim++) {
4557 for (ilim = 0; ilim < usecs; ilim += 100) {
4558 uint16_t isr, sema, info;
4559 if (isp->isp_osinfo.mboxcmd_done) {
4562 if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
4563 isp_intr(isp, isr, sema, info);
4564 if (isp->isp_osinfo.mboxcmd_done) {
4570 if (isp->isp_osinfo.mboxcmd_done) {
4575 if (isp->isp_osinfo.mboxcmd_done == 0) {
4576 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
4577 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
4578 mbp->param[0] = MBOX_TIMEOUT;
4579 isp->isp_osinfo.mboxcmd_done = 1;
4584 isp_mbox_notify_done(ispsoftc_t *isp)
4586 if (isp->isp_osinfo.mbox_sleeping) {
4587 wakeup(&isp->isp_mbxworkp);
4589 isp->isp_osinfo.mboxcmd_done = 1;
4593 isp_mbox_release(ispsoftc_t *isp)
4595 isp->isp_osinfo.mboxbsy = 0;
4599 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
4602 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
4605 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
4615 t.tv_sec = ms / 1000;
4616 t.tv_usec = (ms % 1000) * 1000;
4628 isp_platform_intr(void *arg)
4630 ispsoftc_t *isp = arg;
4631 uint16_t isr, sema, info;
4635 if (ISP_READ_ISR(isp, &isr, &sema, &info))
4636 isp_intr(isp, isr, sema, info);
4638 isp->isp_intbogus++;
4643 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
4645 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4646 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
4648 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
4650 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
4654 * Reset the command reference number for all LUNs on a specific target
4655 * (needed when a target arrives again) or for all targets on a port
4656 * (needed for events like a LIP).
4659 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4661 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4662 struct isp_nexus *nxp;
4666 isp_prt(isp, ISP_LOGDEBUG0,
4667 "Chan %d resetting CRN on all targets", chan);
4669 isp_prt(isp, ISP_LOGDEBUG0,
4670 "Chan %d resetting CRN on target %u", chan, tgt);
4672 for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
4673 for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
4674 if (tgt_set == 0 || tgt == nxp->tgt)
4681 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4686 struct isp_nexus *nxp;
4692 chan = XS_CHANNEL(cmd);
4695 fc = &isp->isp_osinfo.pc.fc[chan];
4696 idx = NEXUS_HASH(tgt, lun);
4697 nxp = fc->nexus_hash[idx];
4700 if (nxp->tgt == tgt && nxp->lun == lun)
4705 nxp = fc->nexus_free_list;
4707 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4712 fc->nexus_free_list = nxp->next;
4716 nxp->next = fc->nexus_hash[idx];
4717 fc->nexus_hash[idx] = nxp;
4719 if (nxp->crnseed == 0)
4721 PISP_PCMD(cmd)->crn = nxp->crnseed;
4722 *crnp = nxp->crnseed++;
4727 * We enter with the lock held
4730 isp_timer(void *arg)
4732 ispsoftc_t *isp = arg;
4733 #ifdef ISP_TARGET_MODE
4734 isp_tmcmd_restart(isp);
4736 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
4740 isp_get_ecmd(ispsoftc_t *isp)
4742 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4744 isp->isp_osinfo.ecmd_free = ecmd->next;
4750 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4752 ecmd->next = isp->isp_osinfo.ecmd_free;
4753 isp->isp_osinfo.ecmd_free = ecmd;