2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2017 Alexander Motin <mav@FreeBSD.org>
5 * Copyright (c) 1997-2009 by Matthew Jacob
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <dev/isp/isp_freebsd.h>
37 #include <sys/unistd.h>
38 #include <sys/kthread.h>
40 #include <sys/module.h>
41 #include <sys/ioccom.h>
42 #include <dev/isp/isp_ioctl.h>
43 #include <sys/devicestat.h>
44 #include <cam/cam_periph.h>
45 #include <cam/cam_xpt_periph.h>
47 MODULE_VERSION(isp, 1);
48 MODULE_DEPEND(isp, cam, 1, 1, 1);
49 int isp_announced = 0;
50 int isp_loop_down_limit = 60; /* default loop down limit */
51 int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
52 int isp_gone_device_time = 30; /* grace time before reporting device lost */
53 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
55 static void isp_freeze_loopdown(ispsoftc_t *, int);
56 static void isp_loop_changed(ispsoftc_t *isp, int chan);
57 static d_ioctl_t ispioctl;
58 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
59 static void isp_poll(struct cam_sim *);
60 static callout_func_t isp_watchdog;
61 static callout_func_t isp_gdt;
62 static task_fn_t isp_gdt_task;
63 static void isp_kthread(void *);
64 static void isp_action(struct cam_sim *, union ccb *);
65 static int isp_timer_count;
66 static void isp_timer(void *);
68 static struct cdevsw isp_cdevsw = {
69 .d_version = D_VERSION,
75 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
77 ispsoftc_t *isp = (ispsoftc_t *)arg1;
79 int error, old, value;
81 value = FCPARAM(isp, chan)->role;
83 error = sysctl_handle_int(oidp, &value, 0, req);
84 if ((error != 0) || (req->newptr == NULL))
87 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
91 old = FCPARAM(isp, chan)->role;
93 /* We don't allow target mode switch from here. */
94 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
96 /* If nothing has changed -- we are done. */
102 /* Actually change the role. */
103 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
109 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
111 struct ccb_setasync csa;
113 struct cam_path *path;
114 #ifdef ISP_TARGET_MODE
118 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
119 device_get_unit(isp->isp_dev), &isp->isp_lock,
120 isp->isp_maxcmds, isp->isp_maxcmds, devq);
125 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
127 cam_sim_free(sim, FALSE);
131 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
133 xpt_bus_deregister(cam_sim_path(sim));
135 cam_sim_free(sim, FALSE);
138 xpt_setup_ccb(&csa.ccb_h, path, 5);
139 csa.ccb_h.func_code = XPT_SASYNC_CB;
140 csa.event_enable = AC_LOST_DEVICE;
141 csa.callback = isp_cam_async;
142 csa.callback_arg = sim;
145 xpt_action((union ccb *)&csa);
149 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
152 #ifdef ISP_TARGET_MODE
153 TAILQ_INIT(&spi->waitq);
154 STAILQ_INIT(&spi->ntfree);
155 for (i = 0; i < ATPDPSIZE; i++)
156 STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
157 LIST_INIT(&spi->atfree);
158 for (i = ATPDPSIZE-1; i >= 0; i--)
159 LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
160 for (i = 0; i < ATPDPHASHSIZE; i++)
161 LIST_INIT(&spi->atused[i]);
164 fcparam *fcp = FCPARAM(isp, chan);
165 struct isp_fc *fc = ISP_FC_PC(isp, chan);
166 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
167 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
175 fcp->isp_use_gft_id = 1;
176 fcp->isp_use_gff_id = 1;
178 callout_init_mtx(&fc->gdt, &isp->isp_lock, 0);
179 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
180 #ifdef ISP_TARGET_MODE
181 TAILQ_INIT(&fc->waitq);
182 STAILQ_INIT(&fc->ntfree);
183 for (i = 0; i < ATPDPSIZE; i++)
184 STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
185 LIST_INIT(&fc->atfree);
186 for (i = ATPDPSIZE-1; i >= 0; i--)
187 LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
188 for (i = 0; i < ATPDPHASHSIZE; i++)
189 LIST_INIT(&fc->atused[i]);
191 isp_loop_changed(isp, chan);
193 if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
194 "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
195 xpt_free_path(fc->path);
197 xpt_bus_deregister(cam_sim_path(fc->sim));
199 cam_sim_free(fc->sim, FALSE);
202 fc->num_threads += 1;
204 snprintf(name, sizeof(name), "chan%d", chan);
205 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
206 OID_AUTO, name, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
209 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
211 "World Wide Node Name");
212 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
214 "World Wide Port Name");
215 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
216 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
218 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
219 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
221 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
222 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
223 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
224 "Cause a Lost Frame on a Read");
226 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
227 "role", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
228 isp, chan, isp_role_sysctl, "I", "Current role");
229 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
230 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
231 "Connection speed in gigabits");
232 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
233 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
235 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
238 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
239 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
241 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
242 "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
243 "Connection topology");
244 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
245 "use_gft_id", CTLFLAG_RWTUN, &fcp->isp_use_gft_id, 0,
246 "Use GFT_ID during fabric scan");
247 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
248 "use_gff_id", CTLFLAG_RWTUN, &fcp->isp_use_gff_id, 0,
249 "Use GFF_ID during fabric scan");
255 isp_detach_chan(ispsoftc_t *isp, int chan)
258 struct cam_path *path;
259 struct ccb_setasync csa;
262 ISP_GET_PC(isp, chan, sim, sim);
263 ISP_GET_PC(isp, chan, path, path);
264 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
266 xpt_setup_ccb(&csa.ccb_h, path, 5);
267 csa.ccb_h.func_code = XPT_SASYNC_CB;
268 csa.event_enable = 0;
269 csa.callback = isp_cam_async;
270 csa.callback_arg = sim;
271 xpt_action((union ccb *)&csa);
273 xpt_bus_deregister(cam_sim_path(sim));
274 cam_sim_free(sim, FALSE);
276 /* Wait for the channel's spawned threads to exit. */
277 wakeup(isp->isp_osinfo.pc.ptr);
278 while (*num_threads != 0)
279 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "isp_reap", 100);
283 isp_attach(ispsoftc_t *isp)
285 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
286 int du = device_get_unit(isp->isp_dev);
290 * Create the device queue for our SIM(s).
292 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
293 if (isp->isp_osinfo.devq == NULL) {
297 for (chan = 0; chan < isp->isp_nchan; chan++) {
298 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
303 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_lock, 0);
304 isp_timer_count = hz >> 2;
305 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
307 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
308 if (isp->isp_osinfo.cdev) {
309 isp->isp_osinfo.cdev->si_drv1 = isp;
314 while (--chan >= 0) {
316 struct cam_path *path;
318 ISP_GET_PC(isp, chan, sim, sim);
319 ISP_GET_PC(isp, chan, path, path);
322 xpt_bus_deregister(cam_sim_path(sim));
324 cam_sim_free(sim, FALSE);
326 cam_simq_free(isp->isp_osinfo.devq);
327 isp->isp_osinfo.devq = NULL;
332 isp_detach(ispsoftc_t *isp)
336 if (isp->isp_osinfo.cdev) {
337 destroy_dev(isp->isp_osinfo.cdev);
338 isp->isp_osinfo.cdev = NULL;
341 /* Tell spawned threads that we're exiting. */
342 isp->isp_osinfo.is_exiting = 1;
343 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
344 isp_detach_chan(isp, chan);
346 callout_drain(&isp->isp_osinfo.tmo);
347 cam_simq_free(isp->isp_osinfo.devq);
352 isp_freeze_loopdown(ispsoftc_t *isp, int chan)
354 struct isp_fc *fc = ISP_FC_PC(isp, chan);
358 if (fc->simqfrozen == 0) {
359 isp_prt(isp, ISP_LOGDEBUG0,
360 "Chan %d Freeze simq (loopdown)", chan);
361 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
363 xpt_freeze_simq(fc->sim, 1);
365 isp_prt(isp, ISP_LOGDEBUG0,
366 "Chan %d Mark simq frozen (loopdown)", chan);
367 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
372 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
374 struct isp_fc *fc = ISP_FC_PC(isp, chan);
378 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
379 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
380 if (wasfrozen && fc->simqfrozen == 0) {
381 isp_prt(isp, ISP_LOGDEBUG0,
382 "Chan %d Release simq", chan);
383 xpt_release_simq(fc->sim, 1);
389 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
392 int nr, chan, retval = ENOTTY;
399 int olddblev = isp->isp_dblev;
400 isp->isp_dblev = *(int *)addr;
401 *(int *)addr = olddblev;
407 if (chan < 0 || chan >= isp->isp_nchan) {
412 *(int *)addr = FCPARAM(isp, chan)->role;
414 *(int *)addr = ISP_ROLE_INITIATOR;
423 if (chan < 0 || chan >= isp->isp_nchan) {
428 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
433 *(int *)addr = FCPARAM(isp, chan)->role;
434 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
448 chan = *(intptr_t *)addr;
449 if (chan < 0 || chan >= isp->isp_nchan) {
454 if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
465 chan = *(intptr_t *)addr;
466 if (chan < 0 || chan >= isp->isp_nchan) {
471 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
479 case ISP_FC_GETDINFO:
481 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
487 if (ifc->loopid >= MAX_FC_TARG) {
491 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
492 if (lp->state != FC_PORTDB_STATE_NIL) {
493 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
494 ifc->loopid = lp->handle;
495 ifc->portid = lp->portid;
496 ifc->node_wwn = lp->node_wwn;
497 ifc->port_wwn = lp->port_wwn;
504 case ISP_FC_GETHINFO:
506 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
507 int chan = hba->fc_channel;
509 if (chan < 0 || chan >= isp->isp_nchan) {
513 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
514 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
515 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
516 hba->fc_nchannels = isp->isp_nchan;
518 hba->fc_nports = MAX_FC_TARG;
519 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
520 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
521 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
522 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
523 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
524 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
525 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
527 hba->fc_nports = MAX_TARGETS;
529 hba->fc_topology = 0;
530 hba->nvram_node_wwn = 0ull;
531 hba->nvram_port_wwn = 0ull;
532 hba->active_node_wwn = 0ull;
533 hba->active_port_wwn = 0ull;
541 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
550 if (chan < 0 || chan >= isp->isp_nchan) {
555 needmarker = retval = 0;
560 uint8_t resp[QENTRY_LEN];
562 isp24xx_statusreq_t sp;
563 fcparam *fcp = FCPARAM(isp, chan);
567 for (i = 0; i < MAX_FC_TARG; i++) {
568 lp = &fcp->portdb[i];
569 if (lp->handle == nphdl) {
573 if (i == MAX_FC_TARG) {
578 ISP_MEMZERO(&tmf, sizeof(tmf));
579 tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
580 tmf.tmf_header.rqs_entry_count = 1;
581 tmf.tmf_nphdl = lp->handle;
584 tmf.tmf_tidlo = lp->portid;
585 tmf.tmf_tidhi = lp->portid >> 16;
586 tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
587 tmf.tmf_lun[1] = fct->lun & 0xff;
588 if (fct->lun >= 256) {
589 tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
591 switch (fct->action) {
593 tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
595 case IPT_TARGET_RESET:
596 tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
600 tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
603 case IPT_CLEAR_TASK_SET:
604 tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
607 case IPT_ABORT_TASK_SET:
608 tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
620 /* Prepare space for response in memory */
621 memset(resp, 0xff, sizeof(resp));
622 tmf.tmf_handle = isp_allocate_handle(isp, resp,
624 if (tmf.tmf_handle == 0) {
625 isp_prt(isp, ISP_LOGERR,
626 "%s: TMF of Chan %d out of handles",
633 /* Send request and wait for response. */
634 reqp = isp_getrqentry(isp);
636 isp_prt(isp, ISP_LOGERR,
637 "%s: TMF of Chan %d out of rqent",
639 isp_destroy_handle(isp, tmf.tmf_handle);
644 isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
645 if (isp->isp_dblev & ISP_LOGDEBUG1)
646 isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
647 ISP_SYNC_REQUEST(isp);
648 if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
649 isp_prt(isp, ISP_LOGERR,
650 "%s: TMF of Chan %d timed out",
652 isp_destroy_handle(isp, tmf.tmf_handle);
657 if (isp->isp_dblev & ISP_LOGDEBUG1)
658 isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
659 isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
661 if (sp.req_completion_status != 0)
666 MBSINIT(&mbs, 0, MBLOGALL, 0);
667 if (ISP_CAP_2KLOGIN(isp) == 0) {
670 switch (fct->action) {
672 mbs.param[0] = MBOX_CLEAR_ACA;
673 mbs.param[1] = nphdl;
674 mbs.param[2] = fct->lun;
676 case IPT_TARGET_RESET:
677 mbs.param[0] = MBOX_TARGET_RESET;
678 mbs.param[1] = nphdl;
682 mbs.param[0] = MBOX_LUN_RESET;
683 mbs.param[1] = nphdl;
684 mbs.param[2] = fct->lun;
687 case IPT_CLEAR_TASK_SET:
688 mbs.param[0] = MBOX_CLEAR_TASK_SET;
689 mbs.param[1] = nphdl;
690 mbs.param[2] = fct->lun;
693 case IPT_ABORT_TASK_SET:
694 mbs.param[0] = MBOX_ABORT_TASK_SET;
695 mbs.param[1] = nphdl;
696 mbs.param[2] = fct->lun;
705 FCPARAM(isp, chan)->sendmarker = 1;
707 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
726 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
727 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
729 static ISP_INLINE int
730 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
732 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
733 if (ISP_PCMD(ccb) == NULL) {
736 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
740 static ISP_INLINE void
741 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
744 #ifdef ISP_TARGET_MODE
745 PISP_PCMD(ccb)->datalen = 0;
747 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
748 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
749 ISP_PCMD(ccb) = NULL;
754 * Put the target mode functions here, because some are inlines
756 #ifdef ISP_TARGET_MODE
757 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
758 static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
759 static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
760 static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
761 static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
762 static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
763 static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
764 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
765 static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
766 static void isp_enable_lun(ispsoftc_t *, union ccb *);
767 static void isp_disable_lun(ispsoftc_t *, union ccb *);
768 static callout_func_t isp_refire_putback_atio;
769 static callout_func_t isp_refire_notify_ack;
770 static void isp_complete_ctio(union ccb *);
771 static void isp_target_putback_atio(union ccb *);
772 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
773 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
774 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
775 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
776 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
777 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
778 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
779 static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
781 static ISP_INLINE tstate_t *
782 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
784 tstate_t *tptr = NULL;
787 if (bus < isp->isp_nchan) {
788 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
789 SLIST_FOREACH(tptr, lhp, next) {
790 if (tptr->ts_lun == lun)
798 isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
800 inot_private_data_t *ntp;
803 if (STAILQ_EMPTY(&tptr->restart_queue))
806 STAILQ_CONCAT(&rq, &tptr->restart_queue);
807 while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
808 STAILQ_REMOVE_HEAD(&rq, next);
810 isp_prt(isp, ISP_LOGTDEBUG0,
811 "%s: restarting resrc deprived %x", __func__,
812 ((at7_entry_t *)ntp->data)->at_rxid);
813 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
815 isp_prt(isp, ISP_LOGTDEBUG0,
816 "%s: restarting resrc deprived %x", __func__,
817 ((at2_entry_t *)ntp->data)->at_rxid);
818 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
820 isp_put_ntpd(isp, bus, ntp);
821 if (!STAILQ_EMPTY(&tptr->restart_queue))
824 if (!STAILQ_EMPTY(&rq)) {
825 STAILQ_CONCAT(&rq, &tptr->restart_queue);
826 STAILQ_CONCAT(&tptr->restart_queue, &rq);
828 return (!STAILQ_EMPTY(&tptr->restart_queue));
832 isp_tmcmd_restart(ispsoftc_t *isp)
837 struct isp_ccbq *waitq;
840 for (bus = 0; bus < isp->isp_nchan; bus++) {
841 for (i = 0; i < LUN_HASH_SIZE; i++) {
842 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
843 SLIST_FOREACH(tptr, lhp, next)
844 isp_atio_restart(isp, bus, tptr);
848 * We only need to do this once per channel.
850 ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
851 ccb = (union ccb *)TAILQ_FIRST(waitq);
853 TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
854 isp_target_start_ctio(isp, ccb, FROM_TIMER);
859 static atio_private_data_t *
860 isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
862 struct atpdlist *atfree;
863 struct atpdlist *atused;
864 atio_private_data_t *atp;
866 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
867 atp = LIST_FIRST(atfree);
869 LIST_REMOVE(atp, next);
871 ISP_GET_PC(isp, chan, atused, atused);
872 LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
877 static atio_private_data_t *
878 isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
880 struct atpdlist *atused;
881 atio_private_data_t *atp;
883 ISP_GET_PC(isp, chan, atused, atused);
884 LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
892 isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
894 struct atpdlist *atfree;
897 isp_put_ecmd(isp, atp->ests);
899 LIST_REMOVE(atp, next);
900 memset(atp, 0, sizeof (*atp));
901 ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
902 LIST_INSERT_HEAD(atfree, atp, next);
906 isp_dump_atpd(ispsoftc_t *isp, int chan)
908 atio_private_data_t *atp, *atpool;
909 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
911 ISP_GET_PC(isp, chan, atpool, atpool);
912 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
913 if (atp->state == ATPD_STATE_FREE)
915 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",
916 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]);
920 static inot_private_data_t *
921 isp_get_ntpd(ispsoftc_t *isp, int chan)
923 struct ntpdlist *ntfree;
924 inot_private_data_t *ntp;
926 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
927 ntp = STAILQ_FIRST(ntfree);
929 STAILQ_REMOVE_HEAD(ntfree, next);
933 static inot_private_data_t *
934 isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
936 inot_private_data_t *ntp, *ntp2;
938 ISP_GET_PC(isp, chan, ntpool, ntp);
939 ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
940 for (; ntp < ntp2; ntp++) {
941 if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
948 isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
950 struct ntpdlist *ntfree;
952 ntp->tag_id = ntp->seq_id = 0;
953 ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
954 STAILQ_INSERT_HEAD(ntfree, ntp, next);
958 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
964 lun = xpt_path_lun_id(path);
965 if (lun != CAM_LUN_WILDCARD) {
966 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
967 return (CAM_LUN_INVALID);
970 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
972 return (CAM_RESRC_UNAVAIL);
975 SLIST_INIT(&tptr->atios);
976 SLIST_INIT(&tptr->inots);
977 STAILQ_INIT(&tptr->restart_queue);
978 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
979 SLIST_INSERT_HEAD(lhp, tptr, next);
981 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
982 return (CAM_REQ_CMP);
986 destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
990 inot_private_data_t *ntp;
992 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
993 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
994 ccb->ccb_h.status = CAM_REQ_ABORTED;
997 while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
998 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
999 ccb->ccb_h.status = CAM_REQ_ABORTED;
1002 while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
1003 isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
1004 STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
1005 isp_put_ntpd(isp, bus, ntp);
1007 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1008 SLIST_REMOVE(lhp, tptr, tstate, next);
1009 free(tptr, M_DEVBUF);
1013 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1020 if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
1021 xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
1022 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1028 * We only support either target and lun both wildcard
1029 * or target and lun both non-wildcard.
1031 bus = XS_CHANNEL(ccb);
1032 target = ccb->ccb_h.target_id;
1033 lun = ccb->ccb_h.target_lun;
1034 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1035 "enabling lun %jx\n", (uintmax_t)lun);
1036 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1037 ccb->ccb_h.status = CAM_LUN_INVALID;
1042 /* Create the state pointer. It should not already exist. */
1043 tptr = get_lun_statep(isp, bus, lun);
1045 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1049 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1050 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1055 ccb->ccb_h.status = CAM_REQ_CMP;
1060 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1062 tstate_t *tptr = NULL;
1067 bus = XS_CHANNEL(ccb);
1068 target = ccb->ccb_h.target_id;
1069 lun = ccb->ccb_h.target_lun;
1070 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1071 "disabling lun %jx\n", (uintmax_t)lun);
1072 if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1073 ccb->ccb_h.status = CAM_LUN_INVALID;
1078 /* Find the state pointer. */
1079 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1080 ccb->ccb_h.status = CAM_PATH_INVALID;
1085 destroy_lun_state(isp, bus, tptr);
1086 ccb->ccb_h.status = CAM_REQ_CMP;
1091 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1093 int fctape, sendstatus, resid;
1095 atio_private_data_t *atp;
1096 struct ccb_scsiio *cso;
1097 struct isp_ccbq *waitq;
1098 uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1099 uint8_t local[QENTRY_LEN];
1101 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,
1102 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1104 ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
1108 * Insert at the tail of the list, if any, waiting CTIO CCBs
1110 TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, sim_links.tqe);
1114 case FROM_CTIO_DONE:
1115 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1119 while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1120 TAILQ_REMOVE(waitq, &ccb->ccb_h, sim_links.tqe);
1123 xfrlen = cso->dxfer_len;
1125 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1126 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1127 ccb->ccb_h.status = CAM_REQ_INVALID;
1133 atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
1135 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1136 isp_dump_atpd(isp, XS_CHANNEL(ccb));
1137 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1143 * Is this command a dead duck?
1146 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1147 ccb->ccb_h.status = CAM_REQ_ABORTED;
1153 * Check to make sure we're still in target mode.
1155 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1156 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1157 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1158 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1164 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1165 * could be split into two CTIOs to split data and status).
1167 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1168 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);
1169 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1174 * Does the initiator expect FC-Tape style responses?
1176 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1183 * If we already did the data xfer portion of a CTIO that sends data
1184 * and status, don't do it again and do the status portion now.
1187 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1188 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1189 xfrlen = 0; /* we already did the data transfer */
1192 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1198 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1199 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1201 * Sense length is not the entire sense data structure size. Periph
1202 * drivers don't seem to be setting sense_len to reflect the actual
1203 * size. We'll peek inside to get the right amount.
1205 sense_length = cso->sense_len;
1208 * This 'cannot' happen
1210 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1211 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1217 memset(local, 0, QENTRY_LEN);
1220 * Check for overflow
1222 tmp = atp->bytes_xfered + atp->bytes_in_transit;
1223 if (xfrlen > 0 && tmp > atp->orig_datalen) {
1224 isp_prt(isp, ISP_LOGERR,
1225 "%s: [0x%x] data overflow by %u bytes", __func__,
1226 cso->tag_id, tmp + xfrlen - atp->orig_datalen);
1227 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1231 if (xfrlen > atp->orig_datalen - tmp) {
1232 xfrlen = atp->orig_datalen - tmp;
1233 if (xfrlen == 0 && !sendstatus) {
1234 cso->resid = cso->dxfer_len;
1235 ccb->ccb_h.status = CAM_REQ_CMP;
1242 ct7_entry_t *cto = (ct7_entry_t *) local;
1244 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1245 cto->ct_header.rqs_entry_count = 1;
1246 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1247 ATPD_SET_SEQNO(cto, atp);
1248 cto->ct_nphdl = atp->nphdl;
1249 cto->ct_rxid = atp->tag;
1250 cto->ct_iid_lo = atp->sid;
1251 cto->ct_iid_hi = atp->sid >> 16;
1252 cto->ct_oxid = atp->oxid;
1253 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1254 cto->ct_timeout = XS_TIME(ccb);
1255 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1258 * Mode 1, status, no data. Only possible when we are sending status, have
1259 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1261 * Mode 2, status, no data. We have to use this in the case that
1262 * the sense data won't fit into a ct7_entry_t.
1265 if (sendstatus && xfrlen == 0) {
1266 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1267 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1268 if (sense_length <= MAXRESPLEN_24XX) {
1269 cto->ct_flags |= CT7_FLAG_MODE1;
1270 cto->ct_scsi_status = cso->scsi_status;
1272 cto->ct_resid = -resid;
1273 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1274 } else if (resid > 0) {
1275 cto->ct_resid = resid;
1276 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1279 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1282 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1283 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1284 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1288 char buf[XCMD_SIZE];
1291 if (atp->ests == NULL) {
1292 atp->ests = isp_get_ecmd(isp);
1293 if (atp->ests == NULL) {
1294 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1298 memset(buf, 0, sizeof (buf));
1299 rp = (fcp_rsp_iu_t *)buf;
1301 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1302 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1304 cto->ct_flags |= CT7_FLAG_MODE2;
1305 rp->fcp_rsp_scsi_status = cso->scsi_status;
1307 rp->fcp_rsp_resid = -resid;
1308 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1309 } else if (resid > 0) {
1310 rp->fcp_rsp_resid = resid;
1311 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1314 rp->fcp_rsp_snslen = sense_length;
1315 cto->ct_senselen = sense_length;
1316 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1317 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1318 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1320 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1322 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1323 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1325 addr = isp->isp_osinfo.ecmd_dma;
1326 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1327 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,
1328 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1329 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1330 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1331 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1332 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1335 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__,
1336 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,
1337 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1339 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1340 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1342 atp->state = ATPD_STATE_LAST_CTIO;
1346 * Mode 0 data transfers, *possibly* with status.
1349 cto->ct_flags |= CT7_FLAG_MODE0;
1350 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1351 cto->ct_flags |= CT7_DATA_IN;
1353 cto->ct_flags |= CT7_DATA_OUT;
1356 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1357 cto->rsp.m0.ct_xfrlen = xfrlen;
1360 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1361 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1362 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1363 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1367 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1368 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1369 cto->ct_flags |= CT7_SENDSTATUS;
1370 atp->state = ATPD_STATE_LAST_CTIO;
1372 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1375 atp->sendst = 1; /* send status later */
1376 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1377 atp->state = ATPD_STATE_CTIO;
1380 atp->state = ATPD_STATE_CTIO;
1382 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__,
1383 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1386 ct2_entry_t *cto = (ct2_entry_t *) local;
1388 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1389 cto->ct_header.rqs_entry_count = 1;
1390 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1391 ATPD_SET_SEQNO(cto, atp);
1392 if (ISP_CAP_2KLOGIN(isp)) {
1393 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1395 cto->ct_iid = atp->nphdl;
1396 if (ISP_CAP_SCCFW(isp) == 0) {
1397 cto->ct_lun = ccb->ccb_h.target_lun;
1400 cto->ct_timeout = XS_TIME(ccb);
1401 cto->ct_rxid = cso->tag_id;
1404 * Mode 1, status, no data. Only possible when we are sending status, have
1405 * no data to transfer, and the sense length can fit in the ct7_entry.
1407 * Mode 2, status, no data. We have to use this in the case the response
1408 * length won't fit into a ct2_entry_t.
1410 * We'll fill out this structure with information as if this were a
1411 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1412 * needed based upon this.
1414 if (sendstatus && xfrlen == 0) {
1415 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1416 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1417 if (sense_length <= MAXRESPLEN) {
1419 cto->ct_resid = -resid;
1420 } else if (resid > 0) {
1421 cto->ct_resid = resid;
1423 cto->ct_flags |= CT2_FLAG_MODE1;
1424 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1426 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1427 } else if (resid > 0) {
1428 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1431 cto->ct_flags |= CT2_CONFIRM;
1434 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1435 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1436 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1440 char buf[XCMD_SIZE];
1443 if (atp->ests == NULL) {
1444 atp->ests = isp_get_ecmd(isp);
1445 if (atp->ests == NULL) {
1446 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1450 memset(buf, 0, sizeof (buf));
1451 rp = (fcp_rsp_iu_t *)buf;
1453 cto->ct_flags |= CT2_CONFIRM;
1454 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1456 cto->ct_flags |= CT2_FLAG_MODE2;
1457 rp->fcp_rsp_scsi_status = cso->scsi_status;
1459 rp->fcp_rsp_resid = -resid;
1460 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1461 } else if (resid > 0) {
1462 rp->fcp_rsp_resid = resid;
1463 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1466 rp->fcp_rsp_snslen = sense_length;
1467 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1468 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1469 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1471 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1473 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1474 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1476 addr = isp->isp_osinfo.ecmd_dma;
1477 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1478 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,
1479 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1480 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1481 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1482 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1485 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__,
1486 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
1487 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1489 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,
1490 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1492 atp->state = ATPD_STATE_LAST_CTIO;
1496 cto->ct_flags |= CT2_FLAG_MODE0;
1497 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1498 cto->ct_flags |= CT2_DATA_IN;
1500 cto->ct_flags |= CT2_DATA_OUT;
1503 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1504 cto->rsp.m0.ct_xfrlen = xfrlen;
1507 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1508 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1509 cto->ct_flags |= CT2_SENDSTATUS;
1510 atp->state = ATPD_STATE_LAST_CTIO;
1512 cto->ct_flags |= CT2_CONFIRM;
1515 atp->sendst = 1; /* send status later */
1516 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1517 atp->state = ATPD_STATE_CTIO;
1520 atp->state = ATPD_STATE_CTIO;
1523 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,
1524 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);
1527 if (isp_get_pcmd(isp, ccb)) {
1528 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1529 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1532 handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1534 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1535 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1536 isp_free_pcmd(isp, ccb);
1539 atp->bytes_in_transit += xfrlen;
1540 PISP_PCMD(ccb)->datalen = xfrlen;
1544 * Call the dma setup routines for this entry (and any subsequent
1545 * CTIOs) if there's data to move, and then tell the f/w it's got
1546 * new things to play with. As with isp_start's usage of DMA setup,
1547 * any swizzling is done in the machine dependent layer. Because
1548 * of this, we put the request onto the queue area first in native
1553 ct7_entry_t *cto = (ct7_entry_t *) local;
1554 cto->ct_syshandle = handle;
1556 ct2_entry_t *cto = (ct2_entry_t *) local;
1557 cto->ct_syshandle = handle;
1560 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1561 if (dmaresult != CMD_QUEUED) {
1562 isp_destroy_handle(isp, handle);
1563 isp_free_pcmd(isp, ccb);
1564 if (dmaresult == CMD_EAGAIN) {
1565 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, sim_links.tqe);
1568 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1572 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1574 ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1576 ccb->ccb_h.spriv_field0 = ~0;
1584 isp_refire_putback_atio(void *arg)
1586 union ccb *ccb = arg;
1588 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
1589 isp_target_putback_atio(ccb);
1593 isp_refire_notify_ack(void *arg)
1595 isp_tna_t *tp = arg;
1596 ispsoftc_t *isp = tp->isp;
1598 ISP_ASSERT_LOCKED(isp);
1599 if (isp_notify_ack(isp, tp->not)) {
1600 callout_schedule(&tp->timer, 5);
1608 isp_target_putback_atio(union ccb *ccb)
1610 ispsoftc_t *isp = XS_ISP(ccb);
1611 struct ccb_scsiio *cso = &ccb->csio;
1612 at2_entry_t local, *at = &local;
1614 ISP_MEMZERO(at, sizeof (at2_entry_t));
1615 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1616 at->at_header.rqs_entry_count = 1;
1617 if (ISP_CAP_SCCFW(isp)) {
1618 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1620 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1622 at->at_status = CT_OK;
1623 at->at_rxid = cso->tag_id;
1624 at->at_iid = cso->init_id;
1625 if (isp_target_put_entry(isp, at)) {
1626 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1627 isp_refire_putback_atio, ccb);
1629 isp_complete_ctio(ccb);
1633 isp_complete_ctio(union ccb *ccb)
1635 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1636 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1642 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1648 struct ccb_accept_tio *atiop;
1650 atio_private_data_t *atp;
1651 inot_private_data_t *ntp;
1654 * The firmware status (except for the QLTM_SVALID bit)
1655 * indicates why this ATIO was sent to us.
1657 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1659 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1660 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1661 isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
1665 fcp = FCPARAM(isp, 0);
1666 if (ISP_CAP_SCCFW(isp)) {
1667 lun = aep->at_scclun;
1671 if (ISP_CAP_2KLOGIN(isp)) {
1672 nphdl = ((at2e_entry_t *)aep)->at_iid;
1674 nphdl = aep->at_iid;
1676 tptr = get_lun_statep(isp, 0, lun);
1678 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1680 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1682 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1684 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1691 * Start any commands pending resources first.
1693 if (isp_atio_restart(isp, 0, tptr))
1696 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1697 if (atiop == NULL) {
1701 atp = isp_get_atpd(isp, 0, aep->at_rxid);
1706 atp->state = ATPD_STATE_ATIO;
1707 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1708 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1709 atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
1710 atiop->ccb_h.target_lun = lun;
1713 * We don't get 'suggested' sense data as we do with SCSI cards.
1715 atiop->sense_len = 0;
1718 * If we're not in the port database, add ourselves.
1721 atiop->init_id = nphdl;
1723 if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
1724 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1726 isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB",
1728 isp_dump_portdb(isp, 0);
1729 isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0);
1733 atiop->cdb_len = ATIO2_CDBLEN;
1734 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1735 atiop->ccb_h.status = CAM_CDB_RECVD;
1736 atiop->tag_id = atp->tag;
1737 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1738 case ATIO2_TC_ATTR_SIMPLEQ:
1739 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1740 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1742 case ATIO2_TC_ATTR_HEADOFQ:
1743 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1744 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1746 case ATIO2_TC_ATTR_ORDERED:
1747 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1748 atiop->tag_action = MSG_ORDERED_Q_TAG;
1750 case ATIO2_TC_ATTR_ACAQ: /* ?? */
1751 case ATIO2_TC_ATTR_UNTAGGED:
1753 atiop->tag_action = 0;
1757 atp->orig_datalen = aep->at_datalen;
1758 atp->bytes_xfered = 0;
1761 atp->sid = PORT_ANY;
1762 atp->oxid = aep->at_oxid;
1763 atp->cdb0 = aep->at_cdb[0];
1764 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1765 atp->state = ATPD_STATE_CAM;
1766 xpt_done((union ccb *)atiop);
1767 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);
1770 ntp = isp_get_ntpd(isp, 0);
1772 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1775 memcpy(ntp->data, aep, QENTRY_LEN);
1776 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1780 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
1784 uint16_t chan, nphdl = NIL_HANDLE;
1788 struct ccb_accept_tio *atiop;
1789 atio_private_data_t *atp = NULL;
1790 atio_private_data_t *oatp;
1791 inot_private_data_t *ntp;
1793 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
1794 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
1795 lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
1797 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
1798 /* Channel has to be derived from D_ID */
1799 isp_find_chan_by_did(isp, did, &chan);
1800 if (chan == ISP_NOCHAN) {
1801 isp_prt(isp, ISP_LOGWARN,
1802 "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
1803 __func__, aep->at_rxid, did);
1804 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
1813 * Find the PDB entry for this initiator
1815 if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
1817 * If we're not in the port database terminate the exchange.
1819 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",
1820 __func__, aep->at_rxid, did, chan, sid);
1821 isp_dump_portdb(isp, chan);
1822 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
1828 * Get the tstate pointer
1830 tptr = get_lun_statep(isp, chan, lun);
1832 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
1834 isp_prt(isp, ISP_LOGWARN,
1835 "%s: [0x%x] no state pointer for lun %jx or wildcard",
1836 __func__, aep->at_rxid, (uintmax_t)lun);
1838 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1840 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1847 * Start any commands pending resources first.
1849 if (isp_atio_restart(isp, chan, tptr))
1853 * If the f/w is out of resources, just send a BUSY status back.
1855 if (aep->at_rxid == AT7_NORESRC_RXID) {
1856 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
1861 * If we're out of resources, just send a BUSY status back.
1863 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1864 if (atiop == NULL) {
1865 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
1869 oatp = isp_find_atpd(isp, chan, aep->at_rxid);
1871 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",
1872 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
1874 * It's not a "no resource" condition- but we can treat it like one
1878 atp = isp_get_atpd(isp, chan, aep->at_rxid);
1880 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1883 atp->word3 = lp->prli_word3;
1884 atp->state = ATPD_STATE_ATIO;
1885 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1886 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1887 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
1888 atiop->ccb_h.target_id = ISP_MAX_TARGETS(isp);
1889 atiop->ccb_h.target_lun = lun;
1890 atiop->sense_len = 0;
1891 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
1893 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
1895 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
1896 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
1897 atiop->cdb_len = cdbxlen;
1898 atiop->ccb_h.status = CAM_CDB_RECVD;
1899 atiop->tag_id = atp->tag;
1900 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
1901 case FCP_CMND_TASK_ATTR_SIMPLE:
1902 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1903 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1905 case FCP_CMND_TASK_ATTR_HEAD:
1906 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1907 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1909 case FCP_CMND_TASK_ATTR_ORDERED:
1910 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1911 atiop->tag_action = MSG_ORDERED_Q_TAG;
1915 case FCP_CMND_TASK_ATTR_ACA:
1916 case FCP_CMND_TASK_ATTR_UNTAGGED:
1917 atiop->tag_action = 0;
1920 atiop->priority = (aep->at_cmnd.fcp_cmnd_task_attribute &
1921 FCP_CMND_PRIO_MASK) >> FCP_CMND_PRIO_SHIFT;
1922 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
1923 atp->bytes_xfered = 0;
1928 atp->oxid = aep->at_hdr.ox_id;
1929 atp->rxid = aep->at_hdr.rx_id;
1930 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1931 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
1932 atp->state = ATPD_STATE_CAM;
1933 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
1934 aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
1935 xpt_done((union ccb *)atiop);
1939 isp_put_atpd(isp, chan, atp);
1940 ntp = isp_get_ntpd(isp, chan);
1942 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1945 memcpy(ntp->data, aep, QENTRY_LEN);
1946 STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1951 * Handle starting an SRR (sequence retransmit request)
1952 * We get here when we've gotten the immediate notify
1953 * and the return of all outstanding CTIOs for this
1957 isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1959 in_fcentry_24xx_t *inot;
1960 uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1963 inot = (in_fcentry_24xx_t *)atp->srr;
1964 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1966 atp->srr_ccb = NULL;
1969 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1973 ccb_off = ccb->ccb_h.spriv_field0;
1974 ccb_len = ccb->csio.dxfer_len;
1975 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1977 switch (inot->in_srr_iu) {
1978 case R_CTL_INFO_SOLICITED_DATA:
1980 * We have to restart a FCP_DATA data out transaction
1983 atp->bytes_xfered = srr_off;
1985 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1988 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
1989 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);
1992 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);
1994 case R_CTL_INFO_COMMAND_STATUS:
1995 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1998 * We have to restart a FCP_RSP IU transaction
2001 case R_CTL_INFO_DATA_DESCRIPTOR:
2003 * We have to restart an FCP DATA in transaction
2005 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2009 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2014 * We can't do anything until this is acked, so we might as well start it now.
2015 * We aren't going to do the usual asynchronous ack issue because we need
2016 * to make sure this gets on the wire first.
2018 if (isp_notify_ack(isp, inot)) {
2019 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2022 isp_target_start_ctio(isp, ccb, FROM_SRR);
2025 inot->in_reserved = 1;
2026 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2027 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2028 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2029 isp_complete_ctio(ccb);
2032 if (isp_notify_ack(isp, inot)) {
2033 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2036 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2037 ccb->ccb_h.status = CAM_MESSAGE_RECV;
2039 * This is not a strict interpretation of MDP, but it's close
2041 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2042 ccb->csio.msg_len = 7;
2043 ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2044 ccb->csio.msg_ptr[1] = 5;
2045 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */
2046 ccb->csio.msg_ptr[3] = srr_off >> 24;
2047 ccb->csio.msg_ptr[4] = srr_off >> 16;
2048 ccb->csio.msg_ptr[5] = srr_off >> 8;
2049 ccb->csio.msg_ptr[6] = srr_off;
2050 isp_complete_ctio(ccb);
2055 isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
2057 in_fcentry_24xx_t *inot = notify->nt_lreserved;
2058 atio_private_data_t *atp;
2059 uint32_t tag = notify->nt_tagval & 0xffffffff;
2061 atp = isp_find_atpd(isp, notify->nt_channel, tag);
2063 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
2065 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2068 atp->srr_notify_rcvd = 1;
2069 memcpy(atp->srr, inot, sizeof (atp->srr));
2070 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
2071 inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2072 ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
2074 isp_handle_srr_start(isp, atp);
2078 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2081 int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
2082 atio_private_data_t *atp = NULL;
2084 uint32_t handle, data_requested, resid;
2086 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2087 ccb = isp_find_xs(isp, handle);
2089 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2092 isp_destroy_handle(isp, handle);
2093 resid = data_requested = PISP_PCMD(ccb)->datalen;
2094 isp_free_pcmd(isp, ccb);
2096 bus = XS_CHANNEL(ccb);
2098 atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2100 atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2104 * XXX: isp_clear_commands() generates fake CTIO with zero
2105 * ct_rxid value, filling only ct_syshandle. Workaround
2106 * that using tag_id from the CCB, pointed by ct_syshandle.
2108 atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2111 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2114 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2115 atp->bytes_in_transit -= data_requested;
2117 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2120 ct7_entry_t *ct = arg;
2122 if (ct->ct_nphdl == CT7_SRR) {
2124 if (atp->srr_notify_rcvd)
2125 isp_handle_srr_start(isp, atp);
2128 if (ct->ct_nphdl == CT_HBA_RESET) {
2129 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2131 failure = CAM_UNREC_HBA_ERROR;
2133 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2134 ok = (ct->ct_nphdl == CT7_OK);
2135 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2136 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
2137 resid = ct->ct_resid;
2139 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),
2140 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2142 ct2_entry_t *ct = arg;
2143 if (ct->ct_status == CT_SRR) {
2145 if (atp->srr_notify_rcvd)
2146 isp_handle_srr_start(isp, atp);
2147 isp_target_putback_atio(ccb);
2150 if (ct->ct_status == CT_HBA_RESET) {
2151 sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2153 failure = CAM_UNREC_HBA_ERROR;
2155 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2156 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2157 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2158 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2159 resid = ct->ct_resid;
2161 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),
2162 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2165 if (data_requested > 0) {
2166 atp->bytes_xfered += data_requested - resid;
2167 ccb->csio.resid = ccb->csio.dxfer_len -
2168 (data_requested - resid);
2170 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2171 ccb->ccb_h.status |= CAM_SENT_SENSE;
2172 ccb->ccb_h.status |= CAM_REQ_CMP;
2175 if (failure == CAM_UNREC_HBA_ERROR)
2176 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2178 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2180 atp->state = ATPD_STATE_PDON;
2183 * We never *not* notify CAM when there has been any error (ok == 0),
2184 * so we never need to do an ATIO putback if we're not notifying CAM.
2186 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
2187 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2188 if (notify_cam == 0) {
2190 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2196 * We are done with this ATIO if we successfully sent status.
2197 * In all other cases expect either another CTIO or XPT_ABORT.
2199 if (ok && sentstatus)
2200 isp_put_atpd(isp, bus, atp);
2203 * We're telling CAM we're done with this CTIO transaction.
2205 * 24XX cards never need an ATIO put back.
2207 * Other cards need one put back only on error.
2208 * In the latter case, a timeout will re-fire
2209 * and try again in case we didn't have
2210 * queue resources to do so at first. In any case,
2211 * once the putback is done we do the completion
2214 if (ok || IS_24XX(isp)) {
2215 isp_complete_ctio(ccb);
2217 isp_target_putback_atio(ccb);
2222 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
2225 if (isp->isp_state != ISP_RUNSTATE) {
2226 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2231 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2233 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2234 ct7_entry_t local, *cto = &local;
2235 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2240 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2241 if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
2246 ISP_MEMZERO(&local, sizeof (local));
2247 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2248 cto->ct_header.rqs_entry_count = 1;
2249 cto->ct_nphdl = nphdl;
2250 cto->ct_rxid = aep->at_rxid;
2251 cto->ct_vpidx = mp->nt_channel;
2252 cto->ct_iid_lo = sid;
2253 cto->ct_iid_hi = sid >> 16;
2254 cto->ct_oxid = aep->at_hdr.ox_id;
2255 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2256 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2258 cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
2259 cto->rsp.m1.ct_resplen = 4;
2260 ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
2261 cto->rsp.m1.ct_resp[0] = rsp & 0xff;
2262 cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
2263 cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
2264 cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
2266 return (isp_target_put_entry(isp, &local));
2270 * This case is for a responding to an ABTS frame
2272 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2275 * Overload nt_need_ack here to mark whether we've terminated the associated command.
2277 if (mp->nt_need_ack) {
2278 uint8_t storage[QENTRY_LEN];
2279 ct7_entry_t *cto = (ct7_entry_t *) storage;
2280 abts_t *abts = (abts_t *)mp->nt_lreserved;
2282 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2283 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2284 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2285 cto->ct_header.rqs_entry_count = 1;
2286 cto->ct_nphdl = mp->nt_nphdl;
2287 cto->ct_rxid = abts->abts_rxid_task;
2288 cto->ct_iid_lo = mp->nt_sid;
2289 cto->ct_iid_hi = mp->nt_sid >> 16;
2290 cto->ct_oxid = abts->abts_ox_id;
2291 cto->ct_vpidx = mp->nt_channel;
2292 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2293 if (isp_target_put_entry(isp, cto)) {
2296 mp->nt_need_ack = 0;
2298 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2306 * Handle logout cases here
2308 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2309 isp_del_all_wwn_entries(isp, mp->nt_channel);
2312 if (mp->nt_ncode == NT_LOGOUT) {
2313 if (!IS_2100(isp) && IS_FC(isp)) {
2314 isp_del_wwn_entries(isp, mp);
2319 * General purpose acknowledgement
2321 if (mp->nt_need_ack) {
2322 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2324 * Don't need to use the guaranteed send because the caller can retry
2326 return (isp_notify_ack(isp, mp->nt_lreserved));
2332 * Handle task management functions.
2334 * We show up here with a notify structure filled out.
2336 * The nt_lreserved tag points to the original queue entry
2339 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2343 struct ccb_immediate_notify *inot;
2344 inot_private_data_t *ntp = NULL;
2345 atio_private_data_t *atp;
2348 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
2349 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2350 if (notify->nt_lun == LUN_ANY) {
2351 if (notify->nt_tagval == TAG_ANY) {
2352 lun = CAM_LUN_WILDCARD;
2354 atp = isp_find_atpd(isp, notify->nt_channel,
2355 notify->nt_tagval & 0xffffffff);
2356 lun = atp ? atp->lun : CAM_LUN_WILDCARD;
2359 lun = notify->nt_lun;
2361 tptr = get_lun_statep(isp, notify->nt_channel, lun);
2363 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2365 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2369 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2371 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2375 inot->ccb_h.target_id = ISP_MAX_TARGETS(isp);
2376 inot->ccb_h.target_lun = lun;
2377 if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2378 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
2379 inot->initiator_id = CAM_TARGET_WILDCARD;
2381 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
2383 inot->seq_id = notify->nt_tagval;
2384 inot->tag_id = notify->nt_tagval >> 32;
2386 switch (notify->nt_ncode) {
2388 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
2389 inot->arg = MSG_ABORT_TASK;
2391 case NT_ABORT_TASK_SET:
2392 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
2393 inot->arg = MSG_ABORT_TASK_SET;
2396 inot->arg = MSG_CLEAR_ACA;
2398 case NT_CLEAR_TASK_SET:
2399 inot->arg = MSG_CLEAR_TASK_SET;
2402 inot->arg = MSG_LOGICAL_UNIT_RESET;
2404 case NT_TARGET_RESET:
2405 inot->arg = MSG_TARGET_RESET;
2407 case NT_QUERY_TASK_SET:
2408 inot->arg = MSG_QUERY_TASK_SET;
2410 case NT_QUERY_ASYNC_EVENT:
2411 inot->arg = MSG_QUERY_ASYNC_EVENT;
2414 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);
2418 ntp = isp_get_ntpd(isp, notify->nt_channel);
2420 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2423 ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
2424 if (notify->nt_lreserved) {
2425 ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
2426 ntp->nt.nt_lreserved = &ntp->data;
2428 ntp->seq_id = notify->nt_tagval;
2429 ntp->tag_id = notify->nt_tagval >> 32;
2431 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2432 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
2433 inot->ccb_h.status = CAM_MESSAGE_RECV;
2434 xpt_done((union ccb *)inot);
2437 if (notify->nt_need_ack) {
2438 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2439 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2440 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2443 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
2449 isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
2451 atio_private_data_t *atp, *atpool;
2452 inot_private_data_t *ntp, *tmp;
2453 uint32_t this_tag_id;
2456 * First, clean any commands pending restart
2458 STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
2460 this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
2462 this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
2463 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2464 isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
2466 isp_put_ntpd(isp, chan, ntp);
2467 STAILQ_REMOVE(&tptr->restart_queue, ntp,
2468 inot_private_data, next);
2473 * Now mark other ones dead as well.
2475 ISP_GET_PC(isp, chan, atpool, atpool);
2476 for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
2477 if (atp->lun != tptr->ts_lun)
2479 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
2486 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2488 struct cam_sim *sim;
2492 sim = (struct cam_sim *)cbarg;
2493 isp = (ispsoftc_t *) cam_sim_softc(sim);
2494 bus = cam_sim_bus(sim);
2495 tgt = xpt_path_target_id(path);
2498 case AC_LOST_DEVICE:
2500 uint16_t oflags, nflags;
2501 sdparam *sdp = SDPARAM(isp, bus);
2504 nflags = sdp->isp_devparam[tgt].nvrm_flags;
2505 nflags &= DPARM_SAFE_DFLT;
2506 if (isp->isp_loaded_fw) {
2507 nflags |= DPARM_NARROW | DPARM_ASYNC;
2509 oflags = sdp->isp_devparam[tgt].goal_flags;
2510 sdp->isp_devparam[tgt].goal_flags = nflags;
2511 sdp->isp_devparam[tgt].dev_update = 1;
2513 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
2514 sdp->isp_devparam[tgt].goal_flags = oflags;
2519 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2525 isp_poll(struct cam_sim *sim)
2527 ispsoftc_t *isp = cam_sim_softc(sim);
2534 isp_watchdog(void *arg)
2536 struct ccb_scsiio *xs = arg;
2538 uint32_t ohandle = ISP_HANDLE_FREE, handle;
2542 handle = isp_find_handle(isp, xs);
2545 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2547 if (handle != ISP_HANDLE_FREE) {
2550 handle = isp_find_handle(isp, xs);
2552 if (handle != ISP_HANDLE_FREE) {
2554 * Try and make sure the command is really dead before
2555 * we release the handle (and DMA resources) for reuse.
2557 * If we are successful in aborting the command then
2558 * we're done here because we'll get the command returned
2561 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2566 * Note that after calling the above, the command may in
2567 * fact have been completed.
2569 xs = isp_find_xs(isp, handle);
2572 * If the command no longer exists, then we won't
2573 * be able to find the xs again with this handle.
2580 * After this point, the command is really dead.
2582 if (XS_XFRLEN(xs)) {
2583 ISP_DMAFREE(isp, xs, handle);
2585 isp_destroy_handle(isp, handle);
2586 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2587 XS_SETERR(xs, CAM_CMD_TIMEOUT);
2590 if (ohandle != ISP_HANDLE_FREE) {
2591 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2593 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2599 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2602 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2605 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2607 ccb = xpt_alloc_ccb_nowait();
2609 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2612 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2613 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2614 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2622 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2624 struct cam_path *tp;
2625 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2627 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2628 xpt_async(AC_LOST_DEVICE, tp, NULL);
2634 * Gone Device Timer Function- when we have decided that a device has gone
2635 * away, we wait a specific period of time prior to telling the OS it has
2638 * This timer function fires once a second and then scans the port database
2639 * for devices that are marked dead but still have a virtual target assigned.
2640 * We decrement a counter for that port database entry, and when it hits zero,
2641 * we tell the OS the device has gone away.
2646 struct isp_fc *fc = arg;
2647 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2651 isp_gdt_task(void *arg, int pending)
2653 struct isp_fc *fc = arg;
2654 ispsoftc_t *isp = fc->isp;
2655 int chan = fc - isp->isp_osinfo.pc.fc;
2657 struct ac_contract ac;
2658 struct ac_device_changed *adc;
2659 int dbidx, more_to_do = 0;
2662 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2663 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2664 lp = &FCPARAM(isp, chan)->portdb[dbidx];
2666 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2669 if (lp->gone_timer != 0) {
2670 lp->gone_timer -= 1;
2674 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2675 if (lp->is_target) {
2677 isp_make_gone(isp, lp, chan, dbidx);
2679 if (lp->is_initiator) {
2680 lp->is_initiator = 0;
2681 ac.contract_number = AC_CONTRACT_DEV_CHG;
2682 adc = (struct ac_device_changed *) ac.contract_data;
2683 adc->wwpn = lp->port_wwn;
2684 adc->port = lp->portid;
2685 adc->target = dbidx;
2687 xpt_async(AC_CONTRACT, fc->path, &ac);
2689 lp->state = FC_PORTDB_STATE_NIL;
2693 callout_reset(&fc->gdt, hz, isp_gdt, fc);
2695 callout_deactivate(&fc->gdt);
2696 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2703 * When loop goes down we remember the time and freeze CAM command queue.
2704 * During some time period we are trying to reprobe the loop. But if we
2705 * fail, we tell the OS that devices have gone away and drop the freeze.
2707 * We don't clear the devices out of our port database because, when loop
2708 * come back up, we have to do some actual cleanup with the chip at that
2709 * point (implicit PLOGO, e.g., to get the chip's port database state right).
2712 isp_loop_changed(ispsoftc_t *isp, int chan)
2714 fcparam *fcp = FCPARAM(isp, chan);
2715 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2717 if (fc->loop_down_time)
2719 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2720 if (fcp->role & ISP_ROLE_INITIATOR)
2721 isp_freeze_loopdown(isp, chan);
2722 fc->loop_down_time = time_uptime;
2727 isp_loop_up(ispsoftc_t *isp, int chan)
2729 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2731 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2732 fc->loop_seen_once = 1;
2733 fc->loop_down_time = 0;
2734 isp_unfreeze_loopdown(isp, chan);
2738 isp_loop_dead(ispsoftc_t *isp, int chan)
2740 fcparam *fcp = FCPARAM(isp, chan);
2741 struct isp_fc *fc = ISP_FC_PC(isp, chan);
2743 struct ac_contract ac;
2744 struct ac_device_changed *adc;
2747 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
2750 * Notify to the OS all targets who we now consider have departed.
2752 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2753 lp = &fcp->portdb[dbidx];
2755 if (lp->state == FC_PORTDB_STATE_NIL)
2758 for (i = 0; i < isp->isp_maxcmds; i++) {
2759 struct ccb_scsiio *xs;
2761 if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2764 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2767 if (dbidx != XS_TGT(xs)) {
2770 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
2771 isp->isp_xflist[i].handle, chan, XS_TGT(xs),
2772 (uintmax_t)XS_LUN(xs));
2775 * Just like in isp_watchdog, abort the outstanding
2776 * command or immediately free its resources if it is
2779 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2783 if (XS_XFRLEN(xs)) {
2784 ISP_DMAFREE(isp, xs, isp->isp_xflist[i].handle);
2786 isp_destroy_handle(isp, isp->isp_xflist[i].handle);
2787 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx could not be aborted and was destroyed",
2788 isp->isp_xflist[i].handle, chan, XS_TGT(xs),
2789 (uintmax_t)XS_LUN(xs));
2790 XS_SETERR(xs, HBA_BUSRESET);
2794 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2795 if (lp->is_target) {
2797 isp_make_gone(isp, lp, chan, dbidx);
2799 if (lp->is_initiator) {
2800 lp->is_initiator = 0;
2801 ac.contract_number = AC_CONTRACT_DEV_CHG;
2802 adc = (struct ac_device_changed *) ac.contract_data;
2803 adc->wwpn = lp->port_wwn;
2804 adc->port = lp->portid;
2805 adc->target = dbidx;
2807 xpt_async(AC_CONTRACT, fc->path, &ac);
2811 isp_unfreeze_loopdown(isp, chan);
2812 fc->loop_down_time = 0;
2816 isp_kthread(void *arg)
2818 struct isp_fc *fc = arg;
2819 ispsoftc_t *isp = fc->isp;
2820 int chan = fc - isp->isp_osinfo.pc.fc;
2825 while (isp->isp_osinfo.is_exiting == 0) {
2826 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2827 "Chan %d Checking FC state", chan);
2828 lb = isp_fc_runstate(isp, chan, 250000);
2829 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2830 "Chan %d FC got to %s state", chan,
2831 isp_fc_loop_statename(lb));
2834 * Our action is different based upon whether we're supporting
2835 * Initiator mode or not. If we are, we might freeze the simq
2836 * when loop is down and set all sorts of different delays to
2839 * If not, we simply just wait for loop to come up.
2841 if (lb == LOOP_READY || lb < 0) {
2845 * If we've never seen loop up and we've waited longer
2846 * than quickboot time, or we've seen loop up but we've
2847 * waited longer than loop_down_limit, give up and go
2848 * to sleep until loop comes up.
2850 if (fc->loop_seen_once == 0)
2851 lim = isp_quickboot_time;
2853 lim = fc->loop_down_limit;
2854 d = time_uptime - fc->loop_down_time;
2870 if (lb == LOOP_READY)
2871 isp_loop_up(isp, chan);
2873 isp_loop_dead(isp, chan);
2876 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2877 "Chan %d sleep for %d seconds", chan, slp);
2878 msleep(fc, &isp->isp_lock, PRIBIO, "ispf", slp * hz);
2880 fc->num_threads -= 1;
2885 #ifdef ISP_TARGET_MODE
2887 isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
2889 atio_private_data_t *atp;
2890 union ccb *accb = ccb->cab.abort_ccb;
2891 struct ccb_hdr *sccb;
2894 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2896 /* Search for the ATIO among queueued. */
2897 SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
2898 if (sccb != &accb->ccb_h)
2900 SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2901 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2902 "Abort FREE ATIO\n");
2903 accb->ccb_h.status = CAM_REQ_ABORTED;
2905 ccb->ccb_h.status = CAM_REQ_CMP;
2910 /* Search for the ATIO among running. */
2911 atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
2913 /* Send TERMINATE to firmware. */
2914 if (!atp->dead && IS_24XX(isp)) {
2915 uint8_t storage[QENTRY_LEN];
2916 ct7_entry_t *cto = (ct7_entry_t *) storage;
2918 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2919 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2920 cto->ct_header.rqs_entry_count = 1;
2921 cto->ct_nphdl = atp->nphdl;
2922 cto->ct_rxid = atp->tag;
2923 cto->ct_iid_lo = atp->sid;
2924 cto->ct_iid_hi = atp->sid >> 16;
2925 cto->ct_oxid = atp->oxid;
2926 cto->ct_vpidx = XS_CHANNEL(accb);
2927 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2928 isp_target_put_entry(isp, cto);
2930 isp_put_atpd(isp, XS_CHANNEL(accb), atp);
2931 ccb->ccb_h.status = CAM_REQ_CMP;
2933 ccb->ccb_h.status = CAM_UA_ABORT;
2938 isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
2940 inot_private_data_t *ntp;
2941 union ccb *accb = ccb->cab.abort_ccb;
2942 struct ccb_hdr *sccb;
2945 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2947 /* Search for the INOT among queueued. */
2948 SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
2949 if (sccb != &accb->ccb_h)
2951 SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2952 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2953 "Abort FREE INOT\n");
2954 accb->ccb_h.status = CAM_REQ_ABORTED;
2956 ccb->ccb_h.status = CAM_REQ_CMP;
2961 /* Search for the INOT among running. */
2962 ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
2964 if (ntp->nt.nt_need_ack) {
2965 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
2966 ntp->nt.nt_lreserved);
2968 isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
2969 ccb->ccb_h.status = CAM_REQ_CMP;
2971 ccb->ccb_h.status = CAM_UA_ABORT;
2978 isp_action(struct cam_sim *sim, union ccb *ccb)
2980 int bus, tgt, error;
2982 struct ccb_trans_settings *cts;
2985 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2987 isp = (ispsoftc_t *)cam_sim_softc(sim);
2988 ISP_ASSERT_LOCKED(isp);
2989 bus = cam_sim_bus(sim);
2990 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2991 ISP_PCMD(ccb) = NULL;
2993 switch (ccb->ccb_h.func_code) {
2994 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2996 * Do a couple of preliminary checks...
2998 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2999 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3000 ccb->ccb_h.status = CAM_REQ_INVALID;
3001 isp_done((struct ccb_scsiio *) ccb);
3005 ccb->csio.req_map = NULL;
3007 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
3008 xpt_print(ccb->ccb_h.path, "invalid target\n");
3009 ccb->ccb_h.status = CAM_PATH_INVALID;
3010 } else if (ISP_MAX_LUNS(isp) > 0 &&
3011 ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
3012 xpt_print(ccb->ccb_h.path, "invalid lun\n");
3013 ccb->ccb_h.status = CAM_PATH_INVALID;
3015 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3020 ccb->csio.scsi_status = SCSI_STATUS_OK;
3021 if (isp_get_pcmd(isp, ccb)) {
3022 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
3023 cam_freeze_devq(ccb->ccb_h.path);
3024 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
3025 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3029 error = isp_start((XS_T *) ccb);
3032 ccb->ccb_h.status |= CAM_SIM_QUEUED;
3033 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY)
3035 /* Give firmware extra 10s to handle timeout. */
3036 ts = SBT_1MS * ccb->ccb_h.timeout + 10 * SBT_1S;
3037 callout_reset_sbt(&PISP_PCMD(ccb)->wdog, ts, 0,
3038 isp_watchdog, ccb, 0);
3041 isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
3042 XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3043 cam_freeze_devq(ccb->ccb_h.path);
3044 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3045 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3046 isp_free_pcmd(isp, ccb);
3050 isp_free_pcmd(isp, ccb);
3051 cam_freeze_devq(ccb->ccb_h.path);
3052 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3053 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3057 isp_done((struct ccb_scsiio *) ccb);
3060 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3061 ccb->ccb_h.status = CAM_REQUEUE_REQ;
3062 isp_free_pcmd(isp, ccb);
3067 #ifdef ISP_TARGET_MODE
3068 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
3069 if (ccb->cel.enable) {
3070 isp_enable_lun(isp, ccb);
3072 isp_disable_lun(isp, ccb);
3075 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
3076 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
3078 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3082 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
3083 str = "XPT_IMMEDIATE_NOTIFY";
3085 str = "XPT_ACCEPT_TARGET_IO";
3086 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
3087 "%s: no state pointer found for %s\n",
3089 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3093 ccb->ccb_h.spriv_field0 = 0;
3094 ccb->ccb_h.spriv_ptr1 = isp;
3096 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3097 ccb->atio.tag_id = 0;
3098 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3099 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3101 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3102 ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3103 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3104 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3107 ccb->ccb_h.status = CAM_REQ_INPROG;
3110 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
3112 inot_private_data_t *ntp;
3115 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
3116 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
3119 * All the relevant path information is in the associated immediate notify
3121 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);
3122 ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
3124 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__,
3125 ccb->cna2.tag_id, ccb->cna2.seq_id);
3126 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3130 if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
3131 (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
3132 cam_freeze_devq(ccb->ccb_h.path);
3133 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3134 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3135 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
3138 isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
3139 ccb->ccb_h.status = CAM_REQ_CMP;
3140 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);
3144 case XPT_CONT_TARGET_IO:
3145 isp_target_start_ctio(isp, ccb, FROM_CAM);
3148 case XPT_RESET_DEV: /* BDR the specified SCSI device */
3149 tgt = ccb->ccb_h.target_id;
3152 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3154 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3157 * If we have a FC device, reset the Command
3158 * Reference Number, because the target will expect
3159 * that we re-start the CRN at 1 after a reset.
3162 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3164 ccb->ccb_h.status = CAM_REQ_CMP;
3168 case XPT_ABORT: /* Abort the specified CCB */
3170 union ccb *accb = ccb->cab.abort_ccb;
3171 switch (accb->ccb_h.func_code) {
3172 #ifdef ISP_TARGET_MODE
3173 case XPT_ACCEPT_TARGET_IO:
3174 isp_abort_atio(isp, ccb);
3176 case XPT_IMMEDIATE_NOTIFY:
3177 isp_abort_inot(isp, ccb);
3181 error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3183 ccb->ccb_h.status = CAM_UA_ABORT;
3185 ccb->ccb_h.status = CAM_REQ_CMP;
3189 ccb->ccb_h.status = CAM_REQ_INVALID;
3193 * This is not a queued CCB, so the caller expects it to be
3194 * complete when control is returned.
3198 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
3199 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
3201 if (!IS_CURRENT_SETTINGS(cts)) {
3202 ccb->ccb_h.status = CAM_REQ_INVALID;
3206 tgt = cts->ccb_h.target_id;
3208 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3209 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3210 sdparam *sdp = SDPARAM(isp, bus);
3213 if (spi->valid == 0 && scsi->valid == 0) {
3214 ccb->ccb_h.status = CAM_REQ_CMP;
3220 * We always update (internally) from goal_flags
3221 * so any request to change settings just gets
3222 * vectored to that location.
3224 dptr = &sdp->isp_devparam[tgt].goal_flags;
3226 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3227 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3228 *dptr |= DPARM_DISC;
3230 *dptr &= ~DPARM_DISC;
3233 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3234 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3235 *dptr |= DPARM_TQING;
3237 *dptr &= ~DPARM_TQING;
3240 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3241 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3242 *dptr |= DPARM_WIDE;
3244 *dptr &= ~DPARM_WIDE;
3250 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3251 *dptr |= DPARM_SYNC;
3253 * XXX: CHECK FOR LEGALITY
3255 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
3256 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3258 *dptr &= ~DPARM_SYNC;
3260 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,
3261 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3262 sdp->isp_devparam[tgt].dev_update = 1;
3265 ccb->ccb_h.status = CAM_REQ_CMP;
3268 case XPT_GET_TRAN_SETTINGS:
3270 tgt = cts->ccb_h.target_id;
3272 fcparam *fcp = FCPARAM(isp, bus);
3273 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3274 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3276 cts->protocol = PROTO_SCSI;
3277 cts->protocol_version = SCSI_REV_2;
3278 cts->transport = XPORT_FC;
3279 cts->transport_version = 0;
3281 scsi->valid = CTS_SCSI_VALID_TQ;
3282 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3283 fc->valid = CTS_FC_VALID_SPEED;
3284 fc->bitrate = 100000;
3285 fc->bitrate *= fcp->isp_gbspeed;
3286 if (tgt < MAX_FC_TARG) {
3287 fcportdb_t *lp = &fcp->portdb[tgt];
3288 fc->wwnn = lp->node_wwn;
3289 fc->wwpn = lp->port_wwn;
3290 fc->port = lp->portid;
3291 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3294 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3295 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3296 sdparam *sdp = SDPARAM(isp, bus);
3297 uint16_t dval, pval, oval;
3299 if (IS_CURRENT_SETTINGS(cts)) {
3300 sdp->isp_devparam[tgt].dev_refresh = 1;
3302 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3303 dval = sdp->isp_devparam[tgt].actv_flags;
3304 oval = sdp->isp_devparam[tgt].actv_offset;
3305 pval = sdp->isp_devparam[tgt].actv_period;
3307 dval = sdp->isp_devparam[tgt].nvrm_flags;
3308 oval = sdp->isp_devparam[tgt].nvrm_offset;
3309 pval = sdp->isp_devparam[tgt].nvrm_period;
3312 cts->protocol = PROTO_SCSI;
3313 cts->protocol_version = SCSI_REV_2;
3314 cts->transport = XPORT_SPI;
3315 cts->transport_version = 2;
3321 if (dval & DPARM_DISC) {
3322 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3324 if ((dval & DPARM_SYNC) && oval && pval) {
3325 spi->sync_offset = oval;
3326 spi->sync_period = pval;
3328 spi->sync_offset = 0;
3329 spi->sync_period = 0;
3331 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3332 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3333 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3334 if (dval & DPARM_WIDE) {
3335 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3337 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3339 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3340 scsi->valid = CTS_SCSI_VALID_TQ;
3341 if (dval & DPARM_TQING) {
3342 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3344 spi->valid |= CTS_SPI_VALID_DISC;
3346 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3347 bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3349 ccb->ccb_h.status = CAM_REQ_CMP;
3353 case XPT_CALC_GEOMETRY:
3354 cam_calc_geometry(&ccb->ccg, 1);
3358 case XPT_RESET_BUS: /* Reset the specified bus */
3359 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
3361 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3366 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
3369 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
3371 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
3373 ccb->ccb_h.status = CAM_REQ_CMP;
3377 case XPT_TERM_IO: /* Terminate the I/O process */
3378 ccb->ccb_h.status = CAM_REQ_INVALID;
3382 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
3384 struct ccb_sim_knob *kp = &ccb->knob;
3388 ccb->ccb_h.status = CAM_REQ_INVALID;
3393 fcp = FCPARAM(isp, bus);
3395 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
3396 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
3397 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
3398 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
3400 ccb->ccb_h.status = CAM_REQ_CMP;
3401 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
3405 switch (kp->xport_specific.fc.role) {
3406 case KNOB_ROLE_NONE:
3407 if (fcp->role != ISP_ROLE_NONE) {
3409 newrole = ISP_ROLE_NONE;
3412 case KNOB_ROLE_TARGET:
3413 if (fcp->role != ISP_ROLE_TARGET) {
3415 newrole = ISP_ROLE_TARGET;
3418 case KNOB_ROLE_INITIATOR:
3419 if (fcp->role != ISP_ROLE_INITIATOR) {
3421 newrole = ISP_ROLE_INITIATOR;
3424 case KNOB_ROLE_BOTH:
3425 if (fcp->role != ISP_ROLE_BOTH) {
3427 newrole = ISP_ROLE_BOTH;
3432 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
3433 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
3434 bus, newrole) != 0) {
3435 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3444 case XPT_GET_SIM_KNOB_OLD: /* Get SIM knobs -- compat value */
3445 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
3447 struct ccb_sim_knob *kp = &ccb->knob;
3452 fcp = FCPARAM(isp, bus);
3454 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
3455 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
3456 switch (fcp->role) {
3458 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3460 case ISP_ROLE_TARGET:
3461 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3463 case ISP_ROLE_INITIATOR:
3464 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3467 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3470 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3471 ccb->ccb_h.status = CAM_REQ_CMP;
3473 ccb->ccb_h.status = CAM_REQ_INVALID;
3478 case XPT_PATH_INQ: /* Path routing inquiry */
3480 struct ccb_pathinq *cpi = &ccb->cpi;
3482 cpi->version_num = 1;
3483 #ifdef ISP_TARGET_MODE
3484 if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3485 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3488 cpi->target_sprt = 0;
3489 cpi->hba_eng_cnt = 0;
3490 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
3491 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
3492 255 : ISP_MAX_LUNS(isp) - 1;
3493 cpi->bus_id = cam_sim_bus(sim);
3494 if (sizeof (bus_size_t) > 4)
3495 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
3497 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
3500 fcparam *fcp = FCPARAM(isp, bus);
3502 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3503 cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
3506 * Because our loop ID can shift from time to time,
3507 * make our initiator ID out of range of our bus.
3509 cpi->initiator_id = cpi->max_target + 1;
3512 * Set base transfer capabilities for Fibre Channel, for this HBA.
3515 cpi->base_transfer_speed = 8000000;
3516 } else if (IS_24XX(isp)) {
3517 cpi->base_transfer_speed = 4000000;
3518 } else if (IS_23XX(isp)) {
3519 cpi->base_transfer_speed = 2000000;
3521 cpi->base_transfer_speed = 1000000;
3523 cpi->hba_inquiry = PI_TAG_ABLE;
3524 cpi->transport = XPORT_FC;
3525 cpi->transport_version = 0;
3526 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
3527 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
3528 cpi->xport_specific.fc.port = fcp->isp_portid;
3529 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3531 sdparam *sdp = SDPARAM(isp, bus);
3532 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3533 cpi->hba_misc = PIM_UNMAPPED;
3534 cpi->initiator_id = sdp->isp_initiator_id;
3535 cpi->base_transfer_speed = 3300;
3536 cpi->transport = XPORT_SPI;
3537 cpi->transport_version = 2;
3539 cpi->protocol = PROTO_SCSI;
3540 cpi->protocol_version = SCSI_REV_2;
3541 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3542 strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
3543 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3544 cpi->unit_number = cam_sim_unit(sim);
3545 cpi->ccb_h.status = CAM_REQ_CMP;
3550 ccb->ccb_h.status = CAM_REQ_INVALID;
3557 isp_done(XS_T *sccb)
3559 ispsoftc_t *isp = XS_ISP(sccb);
3563 XS_SETERR(sccb, CAM_REQ_CMP);
3565 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3566 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
3567 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
3568 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
3570 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3574 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3575 status = sccb->ccb_h.status & CAM_STATUS_MASK;
3576 if (status != CAM_REQ_CMP &&
3577 (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3578 sccb->ccb_h.status |= CAM_DEV_QFRZN;
3579 xpt_freeze_devq(sccb->ccb_h.path, 1);
3582 if (ISP_PCMD(sccb)) {
3583 if (callout_active(&PISP_PCMD(sccb)->wdog))
3584 callout_stop(&PISP_PCMD(sccb)->wdog);
3585 isp_free_pcmd(isp, (union ccb *) sccb);
3587 xpt_done((union ccb *) sccb);
3591 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3594 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3597 target_id_t tgt = 0;
3600 struct cam_path *tmppath;
3601 struct ac_contract ac;
3602 struct ac_device_changed *adc;
3606 case ISPASYNC_NEW_TGT_PARAMS:
3608 struct ccb_trans_settings_scsi *scsi;
3609 struct ccb_trans_settings_spi *spi;
3612 struct ccb_trans_settings cts;
3614 memset(&cts, 0, sizeof (struct ccb_trans_settings));
3617 bus = va_arg(ap, int);
3618 tgt = va_arg(ap, int);
3620 sdp = SDPARAM(isp, bus);
3622 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3623 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3626 flags = sdp->isp_devparam[tgt].actv_flags;
3627 cts.type = CTS_TYPE_CURRENT_SETTINGS;
3628 cts.protocol = PROTO_SCSI;
3629 cts.transport = XPORT_SPI;
3631 scsi = &cts.proto_specific.scsi;
3632 spi = &cts.xport_specific.spi;
3634 if (flags & DPARM_TQING) {
3635 scsi->valid |= CTS_SCSI_VALID_TQ;
3636 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3639 if (flags & DPARM_DISC) {
3640 spi->valid |= CTS_SPI_VALID_DISC;
3641 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3643 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3644 if (flags & DPARM_WIDE) {
3645 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3647 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3649 if (flags & DPARM_SYNC) {
3650 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3651 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3652 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3653 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3655 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);
3656 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3657 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3658 xpt_free_path(tmppath);
3661 case ISPASYNC_BUS_RESET:
3664 bus = va_arg(ap, int);
3666 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
3668 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
3670 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
3674 case ISPASYNC_LOOP_RESET:
3679 bus = va_arg(ap, int);
3682 lipp = ISP_READ(isp, OUTMAILBOX1);
3683 fcp = FCPARAM(isp, bus);
3685 isp_prt(isp, ISP_LOGINFO, "Chan %d LOOP Reset, LIP primitive %x", bus, lipp);
3687 * Per FCP-4, a Reset LIP should result in a CRN reset. Other
3688 * LIPs and loop up/down events should never reset the CRN. For
3689 * an as of yet unknown reason, 24xx series cards (and
3690 * potentially others) can interrupt with a LIP Reset status
3691 * when no LIP reset came down the wire. Additionally, the LIP
3692 * primitive accompanying this status would not be a valid LIP
3693 * Reset primitive, but some variation of an invalid AL_PA
3694 * LIP. As a result, we have to verify the AL_PD in the LIP
3695 * addresses our port before blindly resetting.
3697 if (FCP_IS_DEST_ALPD(fcp, (lipp & 0x00FF)))
3698 isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
3699 isp_loop_changed(isp, bus);
3704 msg = "LIP Received";
3706 case ISPASYNC_LOOP_DOWN:
3710 case ISPASYNC_LOOP_UP:
3714 bus = va_arg(ap, int);
3716 isp_loop_changed(isp, bus);
3717 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3719 case ISPASYNC_DEV_ARRIVED:
3721 bus = va_arg(ap, int);
3722 lp = va_arg(ap, fcportdb_t *);
3724 fc = ISP_FC_PC(isp, bus);
3725 tgt = FC_PORTDB_TGT(isp, bus, lp);
3726 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3727 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
3728 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3729 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
3731 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3732 isp_make_here(isp, lp, bus, tgt);
3734 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3735 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
3736 lp->is_initiator = 1;
3737 ac.contract_number = AC_CONTRACT_DEV_CHG;
3738 adc = (struct ac_device_changed *) ac.contract_data;
3739 adc->wwpn = lp->port_wwn;
3740 adc->port = lp->portid;
3743 xpt_async(AC_CONTRACT, fc->path, &ac);
3746 case ISPASYNC_DEV_CHANGED:
3747 case ISPASYNC_DEV_STAYED:
3753 bus = va_arg(ap, int);
3754 lp = va_arg(ap, fcportdb_t *);
3756 fc = ISP_FC_PC(isp, bus);
3757 tgt = FC_PORTDB_TGT(isp, bus, lp);
3758 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
3759 if (cmd == ISPASYNC_DEV_CHANGED)
3760 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
3762 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
3764 if (lp->is_target !=
3765 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3766 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
3767 lp->is_target = !lp->is_target;
3768 if (lp->is_target) {
3769 if (cmd == ISPASYNC_DEV_CHANGED) {
3770 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3773 isp_make_here(isp, lp, bus, tgt);
3775 isp_make_gone(isp, lp, bus, tgt);
3776 if (cmd == ISPASYNC_DEV_CHANGED) {
3777 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3782 if (lp->is_initiator !=
3783 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3784 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
3785 lp->is_initiator = !lp->is_initiator;
3786 ac.contract_number = AC_CONTRACT_DEV_CHG;
3787 adc = (struct ac_device_changed *) ac.contract_data;
3788 adc->wwpn = lp->port_wwn;
3789 adc->port = lp->portid;
3791 adc->arrived = lp->is_initiator;
3792 xpt_async(AC_CONTRACT, fc->path, &ac);
3795 if ((cmd == ISPASYNC_DEV_CHANGED) &&
3796 (crn_reset_done == 0))
3797 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3801 case ISPASYNC_DEV_GONE:
3803 bus = va_arg(ap, int);
3804 lp = va_arg(ap, fcportdb_t *);
3806 fc = ISP_FC_PC(isp, bus);
3807 tgt = FC_PORTDB_TGT(isp, bus, lp);
3809 * If this has a virtual target or initiator set the isp_gdt
3810 * timer running on it to delay its departure.
3812 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3813 if (lp->is_target || lp->is_initiator) {
3814 lp->state = FC_PORTDB_STATE_ZOMBIE;
3815 lp->gone_timer = fc->gone_device_time;
3816 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
3817 if (fc->ready && !callout_active(&fc->gdt)) {
3818 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);
3819 callout_reset(&fc->gdt, hz, isp_gdt, fc);
3823 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
3825 case ISPASYNC_CHANGE_NOTIFY:
3828 int evt, nphdl, nlstate, portid, reason;
3831 bus = va_arg(ap, int);
3832 evt = va_arg(ap, int);
3833 if (evt == ISPASYNC_CHANGE_PDB) {
3834 nphdl = va_arg(ap, int);
3835 nlstate = va_arg(ap, int);
3836 reason = va_arg(ap, int);
3837 } else if (evt == ISPASYNC_CHANGE_SNS) {
3838 portid = va_arg(ap, int);
3841 nlstate = reason = 0;
3845 if (evt == ISPASYNC_CHANGE_PDB) {
3847 msg = "Port Database Changed";
3848 isp_prt(isp, ISP_LOGINFO,
3849 "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3850 bus, msg, nphdl, nlstate, reason);
3852 * Port database syncs are not sufficient for
3853 * determining that logins or logouts are done on the
3854 * loop, but this information is directly available from
3855 * the reason code from the incoming mbox. We must reset
3856 * the fcp crn on these events according to FCP-4
3859 case PDB24XX_AE_IMPL_LOGO_1:
3860 case PDB24XX_AE_IMPL_LOGO_2:
3861 case PDB24XX_AE_IMPL_LOGO_3:
3862 case PDB24XX_AE_PLOGI_RCVD:
3863 case PDB24XX_AE_PRLI_RCVD:
3864 case PDB24XX_AE_PRLO_RCVD:
3865 case PDB24XX_AE_LOGO_RCVD:
3866 case PDB24XX_AE_PLOGI_DONE:
3867 case PDB24XX_AE_PRLI_DONE:
3869 * If the event is not global, twiddle tgt and
3870 * tgt_set to nominate only the target
3871 * associated with the nphdl.
3873 if (nphdl != PDB24XX_AE_GLOBAL) {
3874 /* Break if we don't yet have the pdb */
3875 if (!isp_find_pdb_by_handle(isp, bus, nphdl, &lp))
3877 tgt = FC_PORTDB_TGT(isp, bus, lp);
3880 isp_fcp_reset_crn(isp, bus, tgt, tgt_set);
3885 } else if (evt == ISPASYNC_CHANGE_SNS) {
3886 msg = "Name Server Database Changed";
3887 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3890 msg = "Other Change Notify";
3891 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3893 isp_loop_changed(isp, bus);
3896 #ifdef ISP_TARGET_MODE
3897 case ISPASYNC_TARGET_NOTIFY:
3899 isp_notify_t *notify;
3901 notify = va_arg(ap, isp_notify_t *);
3903 switch (notify->nt_ncode) {
3905 case NT_ABORT_TASK_SET:
3907 case NT_CLEAR_TASK_SET:
3909 case NT_TARGET_RESET:
3910 case NT_QUERY_TASK_SET:
3911 case NT_QUERY_ASYNC_EVENT:
3913 * These are task management functions.
3915 isp_handle_platform_target_tmf(isp, notify);
3923 * No action need be taken here.
3926 case NT_GLOBAL_LOGOUT:
3929 * This is device arrival/departure notification
3931 isp_handle_platform_target_notify_ack(isp, notify, 0);
3934 isp_handle_platform_srr(isp, notify);
3937 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
3938 isp_handle_platform_target_notify_ack(isp, notify, 0);
3943 case ISPASYNC_TARGET_NOTIFY_ACK:
3947 inot = va_arg(ap, void *);
3949 if (isp_notify_ack(isp, inot)) {
3950 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3953 memcpy(tp->data, inot, sizeof (tp->data));
3955 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
3956 callout_reset(&tp->timer, 5,
3957 isp_refire_notify_ack, tp);
3959 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3964 case ISPASYNC_TARGET_ACTION:
3969 hp = va_arg(ap, isphdr_t *);
3971 switch (hp->rqs_entry_type) {
3973 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
3976 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
3982 isp_handle_platform_ctio(isp, hp);
3985 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
3986 __func__, hp->rqs_entry_type);
3992 case ISPASYNC_FW_CRASH:
3994 uint16_t mbox1, mbox6;
3995 mbox1 = ISP_READ(isp, OUTMAILBOX1);
3996 if (IS_DUALBUS(isp)) {
3997 mbox6 = ISP_READ(isp, OUTMAILBOX6);
4001 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
4003 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
4004 isp->isp_osinfo.mbox_sleep_ok = 0;
4006 isp->isp_osinfo.mbox_sleep_ok = mbox1;
4007 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4012 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
4018 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
4021 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4023 /* First try to use explicitly configured WWNs. */
4024 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
4028 /* Otherwise try to use WWNs from NVRAM. */
4030 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
4031 FCPARAM(isp, chan)->isp_wwpn_nvram;
4036 /* If still no WWNs, try to steal them from the first channel. */
4038 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
4039 ISP_FC_PC(isp, 0)->def_wwpn;
4041 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
4042 FCPARAM(isp, 0)->isp_wwpn_nvram;
4046 /* If still nothing -- improvise. */
4048 seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
4050 seed ^= 0x0100000000000000ULL;
4053 /* For additional channels we have to improvise even more. */
4054 if (!iswwnn && chan > 0) {
4056 * We'll stick our channel number plus one first into bits
4057 * 57..59 and thence into bits 52..55 which allows for 8 bits
4058 * of channel which is enough for our maximum of 255 channels.
4060 seed ^= 0x0100000000000000ULL;
4061 seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
4062 seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
4068 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
4074 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4077 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4080 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
4082 printf("%s\n", lbuf);
4086 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4089 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4092 xpt_print_path(xs->ccb_h.path);
4100 isp_nanotime_sub(struct timespec *b, struct timespec *a)
4105 timespecsub(b, a, &x);
4106 elapsed = GET_NANOSEC(&x);
4113 isp_mbox_acquire(ispsoftc_t *isp)
4115 if (isp->isp_osinfo.mboxbsy) {
4118 isp->isp_osinfo.mboxcmd_done = 0;
4119 isp->isp_osinfo.mboxbsy = 1;
4125 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4129 to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4130 if (isp->isp_osinfo.mbox_sleep_ok) {
4131 isp->isp_osinfo.mbox_sleep_ok = 0;
4132 isp->isp_osinfo.mbox_sleeping = 1;
4133 msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_lock,
4134 PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4135 isp->isp_osinfo.mbox_sleep_ok = 1;
4136 isp->isp_osinfo.mbox_sleeping = 0;
4138 for (t = 0; t < to; t += 100) {
4139 if (isp->isp_osinfo.mboxcmd_done)
4142 if (isp->isp_osinfo.mboxcmd_done)
4147 if (isp->isp_osinfo.mboxcmd_done == 0) {
4148 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
4149 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
4150 isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4151 mbp->param[0] = MBOX_TIMEOUT;
4152 isp->isp_osinfo.mboxcmd_done = 1;
4157 isp_mbox_notify_done(ispsoftc_t *isp)
4159 isp->isp_osinfo.mboxcmd_done = 1;
4160 if (isp->isp_osinfo.mbox_sleeping)
4161 wakeup(&isp->isp_osinfo.mboxcmd_done);
4165 isp_mbox_release(ispsoftc_t *isp)
4167 isp->isp_osinfo.mboxbsy = 0;
4171 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
4174 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
4177 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
4183 isp_platform_intr(void *arg)
4185 ispsoftc_t *isp = arg;
4193 isp_platform_intr_resp(void *arg)
4195 ispsoftc_t *isp = arg;
4198 isp_intr_respq(isp);
4201 /* We have handshake enabled, so explicitly complete interrupt */
4202 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4206 isp_platform_intr_atio(void *arg)
4208 ispsoftc_t *isp = arg;
4211 #ifdef ISP_TARGET_MODE
4212 isp_intr_atioq(isp);
4216 /* We have handshake enabled, so explicitly complete interrupt */
4217 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4221 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
4223 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4224 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
4226 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
4228 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
4232 * Reset the command reference number for all LUNs on a specific target
4233 * (needed when a target arrives again) or for all targets on a port
4234 * (needed for events like a LIP).
4237 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4239 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4240 struct isp_nexus *nxp;
4244 isp_prt(isp, ISP_LOGDEBUG0,
4245 "Chan %d resetting CRN on all targets", chan);
4247 isp_prt(isp, ISP_LOGDEBUG0,
4248 "Chan %d resetting CRN on target %u", chan, tgt);
4250 for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
4251 for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
4252 if (tgt_set == 0 || tgt == nxp->tgt)
4259 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4264 struct isp_nexus *nxp;
4270 chan = XS_CHANNEL(cmd);
4273 fc = &isp->isp_osinfo.pc.fc[chan];
4274 idx = NEXUS_HASH(tgt, lun);
4275 nxp = fc->nexus_hash[idx];
4278 if (nxp->tgt == tgt && nxp->lun == lun)
4283 nxp = fc->nexus_free_list;
4285 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4290 fc->nexus_free_list = nxp->next;
4294 nxp->next = fc->nexus_hash[idx];
4295 fc->nexus_hash[idx] = nxp;
4297 if (nxp->crnseed == 0)
4299 *crnp = nxp->crnseed++;
4304 * We enter with the lock held
4307 isp_timer(void *arg)
4309 ispsoftc_t *isp = arg;
4310 #ifdef ISP_TARGET_MODE
4311 isp_tmcmd_restart(isp);
4313 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
4317 isp_get_ecmd(ispsoftc_t *isp)
4319 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4321 isp->isp_osinfo.ecmd_free = ecmd->next;
4327 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4329 ecmd->next = isp->isp_osinfo.ecmd_free;
4330 isp->isp_osinfo.ecmd_free = ecmd;