2 * Copyright (c) 1997-2006 by Matthew Jacob
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <dev/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
35 #include <machine/stdarg.h> /* for use by isp_prt below */
37 #include <sys/module.h>
38 #include <sys/ioccom.h>
39 #include <dev/isp/isp_ioctl.h>
40 #if __FreeBSD_version >= 500000
41 #include <sys/sysctl.h>
43 #include <sys/devicestat.h>
45 #include <cam/cam_periph.h>
46 #include <cam/cam_xpt_periph.h>
48 #if !defined(CAM_NEW_TRAN_CODE) && __FreeBSD_version >= 700025
49 #define CAM_NEW_TRAN_CODE 1
53 MODULE_VERSION(isp, 1);
54 MODULE_DEPEND(isp, cam, 1, 1, 1);
55 int isp_announced = 0;
56 int isp_fabric_hysteresis = 5;
57 int isp_loop_down_limit = 300; /* default loop down limit */
58 int isp_change_is_bad = 0; /* "changed" devices are bad */
59 int isp_quickboot_time = 15; /* don't wait more than N secs for loop up */
60 int isp_gone_device_time = 30; /* grace time before reporting device lost */
61 static const char *roles[4] = {
62 "(none)", "Target", "Initiator", "Target/Initiator"
64 static const char prom3[] =
65 "PortID 0x%06x Departed from Target %u because of %s";
67 static void isp_freeze_loopdown(ispsoftc_t *, char *);
68 static d_ioctl_t ispioctl;
69 static void isp_intr_enable(void *);
70 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
71 static void isp_poll(struct cam_sim *);
72 static timeout_t isp_watchdog;
73 static timeout_t isp_ldt;
74 static void isp_kthread(void *);
75 static void isp_action(struct cam_sim *, union ccb *);
77 #if __FreeBSD_version < 700000
78 ispfwfunc *isp_get_firmware_p = NULL;
81 #if __FreeBSD_version < 500000
82 #define ISP_CDEV_MAJOR 248
83 static struct cdevsw isp_cdevsw = {
85 /* close */ nullclose,
91 /* strategy */ nostrategy,
93 /* maj */ ISP_CDEV_MAJOR,
98 #define isp_sysctl_update(x) do { ; } while (0)
100 static struct cdevsw isp_cdevsw = {
101 .d_version = D_VERSION,
102 #if __FreeBSD_version < 700037
103 .d_flags = D_NEEDGIANT,
108 static void isp_sysctl_update(ispsoftc_t *);
111 static ispsoftc_t *isplist = NULL;
114 isp_attach(ispsoftc_t *isp)
116 int primary, secondary;
117 struct ccb_setasync csa;
118 struct cam_devq *devq;
120 struct cam_path *path;
123 * Establish (in case of 12X0) which bus is the primary.
130 * Create the device queue for our SIM(s).
132 devq = cam_simq_alloc(isp->isp_maxcmds);
138 * Construct our SIM entry.
140 sim = isp_sim_alloc(isp_action, isp_poll, "isp", isp,
141 device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
147 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
148 isp->isp_osinfo.ehook.ich_arg = isp;
150 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
152 cam_sim_free(sim, TRUE);
153 isp_prt(isp, ISP_LOGERR,
154 "could not establish interrupt enable hook");
159 if (xpt_bus_register(sim, isp->isp_dev, primary) != CAM_SUCCESS) {
160 cam_sim_free(sim, TRUE);
164 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
165 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
166 xpt_bus_deregister(cam_sim_path(sim));
167 cam_sim_free(sim, TRUE);
168 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
172 xpt_setup_ccb(&csa.ccb_h, path, 5);
173 csa.ccb_h.func_code = XPT_SASYNC_CB;
174 csa.event_enable = AC_LOST_DEVICE;
175 csa.callback = isp_cam_async;
176 csa.callback_arg = sim;
177 xpt_action((union ccb *)&csa);
179 isp->isp_path = path;
182 * If we have a second channel, construct SIM entry for that.
184 if (IS_DUALBUS(isp)) {
185 sim = isp_sim_alloc(isp_action, isp_poll, "isp", isp,
186 device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
188 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
189 xpt_free_path(isp->isp_path);
191 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
194 if (xpt_bus_register(sim, isp->isp_dev, secondary) !=
196 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
197 xpt_free_path(isp->isp_path);
198 cam_sim_free(sim, TRUE);
199 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
203 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
204 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
205 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
206 xpt_free_path(isp->isp_path);
207 xpt_bus_deregister(cam_sim_path(sim));
208 cam_sim_free(sim, TRUE);
209 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
213 xpt_setup_ccb(&csa.ccb_h, path, 5);
214 csa.ccb_h.func_code = XPT_SASYNC_CB;
215 csa.event_enable = AC_LOST_DEVICE;
216 csa.callback = isp_cam_async;
217 csa.callback_arg = sim;
218 xpt_action((union ccb *)&csa);
220 isp->isp_path2 = path;
224 * Create device nodes
227 (void) make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
228 GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
229 isp_sysctl_update(isp);
232 if (isp->isp_role != ISP_ROLE_NONE) {
233 isp->isp_state = ISP_RUNSTATE;
234 ISP_ENABLE_INTS(isp);
236 if (isplist == NULL) {
239 ispsoftc_t *tmp = isplist;
240 while (tmp->isp_osinfo.next) {
241 tmp = tmp->isp_osinfo.next;
243 tmp->isp_osinfo.next = isp;
247 * Create a kernel thread for fibre channel instances.
250 isp_callout_init(&isp->isp_osinfo.ldt);
251 isp_callout_init(&isp->isp_osinfo.gdt);
253 #if __FreeBSD_version >= 500000
254 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
255 RFHIGHPID, 0, "%s: fc_thrd",
256 device_get_nameunit(isp->isp_dev)))
258 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
259 "%s: fc_thrd", device_get_nameunit(isp->isp_dev)))
263 xpt_bus_deregister(cam_sim_path(sim));
264 cam_sim_free(sim, TRUE);
265 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
266 isp_prt(isp, ISP_LOGERR, "could not create kthread");
271 * We start by being "loop down" if we have an initiator role
273 if (isp->isp_role & ISP_ROLE_INITIATOR) {
274 isp_freeze_loopdown(isp, "isp_attach");
275 isp->isp_osinfo.ldt_running = 1;
276 callout_reset(&isp->isp_osinfo.ldt,
277 isp_quickboot_time * hz, isp_ldt, isp);
278 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
279 "Starting Initial Loop Down Timer");
285 isp_freeze_loopdown(ispsoftc_t *isp, char *msg)
287 if (isp->isp_osinfo.simqfrozen == 0) {
288 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown)", msg);
289 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
290 xpt_freeze_simq(isp->isp_sim, 1);
292 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown)", msg);
293 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
298 #if __FreeBSD_version < 500000
300 #define _IOP struct proc
302 #define _IOP struct thread
303 #define _DEV struct cdev *
307 ispioctl(_DEV dev, u_long c, caddr_t addr, int flags, _IOP *td)
310 int nr, retval = ENOTTY;
314 if (minor(dev) == device_get_unit(isp->isp_dev)) {
317 isp = isp->isp_osinfo.next;
324 #ifdef ISP_FW_CRASH_DUMP
325 case ISP_GET_FW_CRASH_DUMP:
327 uint16_t *ptr = FCPARAM(isp)->isp_dump_data;
332 sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
334 sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
337 void *uaddr = *((void **) addr);
338 if (copyout(ptr, uaddr, sz)) {
348 case ISP_FORCE_CRASH_DUMP:
351 isp_freeze_loopdown(isp,
352 "ispioctl(ISP_FORCE_CRASH_DUMP)");
362 int olddblev = isp->isp_dblev;
363 isp->isp_dblev = *(int *)addr;
364 *(int *)addr = olddblev;
369 *(int *)addr = isp->isp_role;
374 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
378 *(int *)addr = isp->isp_role;
390 if (isp_fc_runstate(isp, 5 * 1000000)) {
401 if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
409 case ISP_FC_GETDINFO:
411 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
417 if (ifc->loopid >= MAX_FC_TARG) {
421 lp = &FCPARAM(isp)->portdb[ifc->loopid];
422 if (lp->state == FC_PORTDB_STATE_VALID) {
423 ifc->role = lp->roles;
424 ifc->loopid = lp->handle;
425 ifc->portid = lp->portid;
426 ifc->node_wwn = lp->node_wwn;
427 ifc->port_wwn = lp->port_wwn;
436 isp_stats_t *sp = (isp_stats_t *) addr;
438 MEMZERO(sp, sizeof (*sp));
439 sp->isp_stat_version = ISP_STATS_VERSION;
440 sp->isp_type = isp->isp_type;
441 sp->isp_revision = isp->isp_revision;
443 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
444 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
445 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
446 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
447 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
448 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
449 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
450 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
458 isp->isp_intbogus = 0;
459 isp->isp_intmboxc = 0;
460 isp->isp_intoasync = 0;
461 isp->isp_rsltccmplt = 0;
462 isp->isp_fphccmplt = 0;
463 isp->isp_rscchiwater = 0;
464 isp->isp_fpcchiwater = 0;
468 case ISP_FC_GETHINFO:
470 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
471 MEMZERO(hba, sizeof (*hba));
473 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
474 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
475 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
477 hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
478 hba->fc_scsi_supported = 1;
479 hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
480 hba->fc_loopid = FCPARAM(isp)->isp_loopid;
481 hba->nvram_node_wwn = FCPARAM(isp)->isp_wwnn_nvram;
482 hba->nvram_port_wwn = FCPARAM(isp)->isp_wwpn_nvram;
483 hba->active_node_wwn = ISP_NODEWWN(isp);
484 hba->active_port_wwn = ISP_PORTWWN(isp);
492 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
500 memset(&mbs, 0, sizeof (mbs));
501 needmarker = retval = 0;
502 loopid = fct->loopid;
503 if (FCPARAM(isp)->isp_2klogin == 0) {
506 switch (fct->action) {
508 mbs.param[0] = MBOX_CLEAR_ACA;
509 mbs.param[1] = loopid;
510 mbs.param[2] = fct->lun;
512 case IPT_TARGET_RESET:
513 mbs.param[0] = MBOX_TARGET_RESET;
514 mbs.param[1] = loopid;
518 mbs.param[0] = MBOX_LUN_RESET;
519 mbs.param[1] = loopid;
520 mbs.param[2] = fct->lun;
523 case IPT_CLEAR_TASK_SET:
524 mbs.param[0] = MBOX_CLEAR_TASK_SET;
525 mbs.param[1] = loopid;
526 mbs.param[2] = fct->lun;
529 case IPT_ABORT_TASK_SET:
530 mbs.param[0] = MBOX_ABORT_TASK_SET;
531 mbs.param[1] = loopid;
532 mbs.param[2] = fct->lun;
541 isp->isp_sendmarker |= 1;
544 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
557 #if __FreeBSD_version >= 500000
559 isp_sysctl_update(ispsoftc_t *isp)
561 struct sysctl_ctx_list *ctx =
562 device_get_sysctl_ctx(isp->isp_osinfo.dev);
563 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
569 snprintf(isp->isp_osinfo.sysctl_info.fc.wwnn,
570 sizeof (isp->isp_osinfo.sysctl_info.fc.wwnn), "0x%08x%08x",
571 (uint32_t) (ISP_NODEWWN(isp) >> 32), (uint32_t) ISP_NODEWWN(isp));
573 snprintf(isp->isp_osinfo.sysctl_info.fc.wwpn,
574 sizeof (isp->isp_osinfo.sysctl_info.fc.wwpn), "0x%08x%08x",
575 (uint32_t) (ISP_PORTWWN(isp) >> 32), (uint32_t) ISP_PORTWWN(isp));
577 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
578 "wwnn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwnn, 0,
579 "World Wide Node Name");
581 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
582 "wwpn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwpn, 0,
583 "World Wide Port Name");
585 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
587 CTLFLAG_RW, &isp->isp_osinfo.loop_down_limit, 0,
588 "How long to wait for loop to come back up");
590 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
592 CTLFLAG_RW, &isp->isp_osinfo.gone_device_time, 0,
593 "How long to wait for a device to reappear");
598 isp_intr_enable(void *arg)
600 ispsoftc_t *isp = arg;
602 if (isp->isp_role != ISP_ROLE_NONE) {
603 ISP_ENABLE_INTS(isp);
606 /* Release our hook so that the boot can continue. */
607 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
611 * Put the target mode functions here, because some are inlines
614 #ifdef ISP_TARGET_MODE
616 static __inline int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
617 static __inline int are_any_luns_enabled(ispsoftc_t *, int);
618 static __inline tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
619 static __inline void rls_lun_statep(ispsoftc_t *, tstate_t *);
620 static __inline atio_private_data_t *isp_get_atpd(ispsoftc_t *, int);
622 create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
623 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
624 static int isp_en_lun(ispsoftc_t *, union ccb *);
625 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
626 static cam_status isp_abort_tgt_ccb(ispsoftc_t *, union ccb *);
627 static timeout_t isp_refire_putback_atio;
628 static void isp_complete_ctio(union ccb *);
629 static void isp_target_putback_atio(union ccb *);
630 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
631 static int isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
632 static int isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
633 static int isp_handle_platform_ctio(ispsoftc_t *, void *);
634 static int isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
635 static int isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
638 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
641 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
646 if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
649 } while ((tptr = tptr->next) != NULL);
654 are_any_luns_enabled(ispsoftc_t *isp, int port)
657 if (IS_DUALBUS(isp)) {
658 lo = (port * (LUN_HASH_SIZE >> 1));
659 hi = lo + (LUN_HASH_SIZE >> 1);
664 for (lo = 0; lo < hi; lo++) {
665 if (isp->isp_osinfo.lun_hash[lo]) {
672 static __inline tstate_t *
673 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
675 tstate_t *tptr = NULL;
677 if (lun == CAM_LUN_WILDCARD) {
678 if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
679 tptr = &isp->isp_osinfo.tsdflt[bus];
685 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
692 if (tptr->lun == lun && tptr->bus == bus) {
696 } while ((tptr = tptr->next) != NULL);
701 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
707 static __inline atio_private_data_t *
708 isp_get_atpd(ispsoftc_t *isp, int tag)
710 atio_private_data_t *atp;
711 for (atp = isp->isp_osinfo.atpdp;
712 atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
720 create_lun_state(ispsoftc_t *isp, int bus,
721 struct cam_path *path, tstate_t **rslt)
726 tstate_t *tptr, *new;
728 lun = xpt_path_lun_id(path);
729 if (lun >= ISP_MAX_LUNS(isp)) {
730 return (CAM_LUN_INVALID);
732 if (is_lun_enabled(isp, bus, lun)) {
733 return (CAM_LUN_ALRDY_ENA);
735 new = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
737 return (CAM_RESRC_UNAVAIL);
740 status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
741 xpt_path_target_id(path), xpt_path_lun_id(path));
742 if (status != CAM_REQ_CMP) {
748 SLIST_INIT(&new->atios);
749 SLIST_INIT(&new->inots);
752 hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
753 tptr = isp->isp_osinfo.lun_hash[hfx];
755 isp->isp_osinfo.lun_hash[hfx] = new;
762 return (CAM_REQ_CMP);
766 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
774 hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
775 pw = isp->isp_osinfo.lun_hash[hfx];
778 } else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
779 isp->isp_osinfo.lun_hash[hfx] = pw->next;
784 if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
795 free(tptr, M_DEVBUF);
802 isp_en_lun(ispsoftc_t *isp, union ccb *ccb)
804 struct ccb_en_lun *cel = &ccb->cel;
805 tstate_t *tptr = NULL;
807 int bus, cmd, av, wildcard, tm_on;
811 bus = XS_CHANNEL(ccb);
813 xpt_print(ccb->ccb_h.path, "illegal bus %d\n", bus);
814 ccb->ccb_h.status = CAM_PATH_INVALID;
817 tgt = ccb->ccb_h.target_id;
818 lun = ccb->ccb_h.target_lun;
820 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
821 xpt_print(ccb->ccb_h.path, "%sabling lun 0x%x on channel %d\n",
822 cel->enable? "en" : "dis", lun, bus);
825 if ((lun != CAM_LUN_WILDCARD) &&
826 (lun >= (lun_id_t) isp->isp_maxluns)) {
827 ccb->ccb_h.status = CAM_LUN_INVALID;
832 sdparam *sdp = isp->isp_param;
834 if (tgt != CAM_TARGET_WILDCARD &&
835 tgt != sdp->isp_initiator_id) {
836 ccb->ccb_h.status = CAM_TID_INVALID;
841 * There's really no point in doing this yet w/o multi-tid
842 * capability. Even then, it's problematic.
845 if (tgt != CAM_TARGET_WILDCARD &&
846 tgt != FCPARAM(isp)->isp_iid) {
847 ccb->ccb_h.status = CAM_TID_INVALID;
852 * This is as a good a place as any to check f/w capabilities.
854 if (FCPARAM(isp)->isp_tmode == 0) {
855 xpt_print(ccb->ccb_h.path,
856 "firmware does not support target mode\n");
857 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
861 * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
862 * XXX: dork with our already fragile enable/disable code.
864 if (FCPARAM(isp)->isp_sccfw == 0) {
865 xpt_print(ccb->ccb_h.path,
866 "firmware not SCCLUN capable\n");
867 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
872 if (tgt == CAM_TARGET_WILDCARD) {
873 if (lun == CAM_LUN_WILDCARD) {
876 ccb->ccb_h.status = CAM_LUN_INVALID;
883 tm_on = (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) != 0;
886 * Next check to see whether this is a target/lun wildcard action.
888 * If so, we know that we can accept commands for luns that haven't
889 * been enabled yet and send them upstream. Otherwise, we have to
890 * handle them locally (if we see them at all).
894 tptr = &isp->isp_osinfo.tsdflt[bus];
897 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
901 xpt_create_path(&tptr->owner, NULL,
902 xpt_path_path_id(ccb->ccb_h.path),
903 xpt_path_target_id(ccb->ccb_h.path),
904 xpt_path_lun_id(ccb->ccb_h.path));
905 if (ccb->ccb_h.status != CAM_REQ_CMP) {
908 SLIST_INIT(&tptr->atios);
909 SLIST_INIT(&tptr->inots);
910 isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
913 ccb->ccb_h.status = CAM_REQ_CMP;
917 ccb->ccb_h.status = CAM_SCSI_BUSY;
920 xpt_free_path(tptr->owner);
921 isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
926 * Now check to see whether this bus needs to be
927 * enabled/disabled with respect to target mode.
930 if (cel->enable && tm_on == 0) {
931 av |= ENABLE_TARGET_FLAG;
932 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
934 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
936 isp->isp_osinfo.tmflags[bus] &=
937 ~TM_WILDCARD_ENABLED;
938 xpt_free_path(tptr->owner);
942 isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
943 xpt_print(ccb->ccb_h.path, "Target Mode Enabled\n");
944 } else if (cel->enable == 0 && tm_on && wildcard) {
945 if (are_any_luns_enabled(isp, bus)) {
946 ccb->ccb_h.status = CAM_SCSI_BUSY;
949 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
951 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
954 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
955 xpt_print(ccb->ccb_h.path, "Target Mode Disabled\n");
959 ccb->ccb_h.status = CAM_REQ_CMP;
966 for (seq = 0; seq < NLEACT; seq++) {
967 if (isp->isp_osinfo.leact[seq] == 0) {
972 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
976 isp->isp_osinfo.leact[seq] = ccb;
980 create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
981 if (ccb->ccb_h.status != CAM_REQ_CMP) {
982 isp->isp_osinfo.leact[seq] = 0;
986 tptr = get_lun_statep(isp, bus, lun);
988 ccb->ccb_h.status = CAM_LUN_INVALID;
994 int c, n, ulun = lun;
996 cmd = RQSTYPE_ENABLE_LUN;
999 if (IS_FC(isp) && lun != 0) {
1000 cmd = RQSTYPE_MODIFY_LUN;
1003 * For SCC firmware, we only deal with setting
1004 * (enabling or modifying) lun 0.
1008 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1009 rls_lun_statep(isp, tptr);
1010 ccb->ccb_h.status = CAM_REQ_INPROG;
1014 int c, n, ulun = lun;
1016 cmd = -RQSTYPE_MODIFY_LUN;
1019 if (IS_FC(isp) && lun != 0) {
1022 * For SCC firmware, we only deal with setting
1023 * (enabling or modifying) lun 0.
1027 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1028 rls_lun_statep(isp, tptr);
1029 ccb->ccb_h.status = CAM_REQ_INPROG;
1033 rls_lun_statep(isp, tptr);
1034 xpt_print(ccb->ccb_h.path, "isp_lun_cmd failed\n");
1035 isp->isp_osinfo.leact[seq] = 0;
1036 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1041 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1043 const char lfmt[] = "now %sabled for target mode\n";
1048 struct ccb_en_lun *cel;
1050 seq = lep->le_reserved - 1;
1051 if (seq >= NLEACT) {
1052 isp_prt(isp, ISP_LOGERR,
1053 "seq out of range (%u) in isp_ledone", seq);
1056 ccb = isp->isp_osinfo.leact[seq];
1058 isp_prt(isp, ISP_LOGERR,
1059 "no ccb for seq %u in isp_ledone", seq);
1063 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1065 xpt_print(ccb->ccb_h.path, "null tptr in isp_ledone\n");
1066 isp->isp_osinfo.leact[seq] = 0;
1070 if (lep->le_status != LUN_OK) {
1071 xpt_print(ccb->ccb_h.path,
1072 "ENABLE/MODIFY LUN returned 0x%x\n", lep->le_status);
1074 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1075 rls_lun_statep(isp, tptr);
1076 isp->isp_osinfo.leact[seq] = 0;
1080 isp_prt(isp, ISP_LOGTDEBUG0,
1081 "isp_ledone: ENABLE/MODIFY done okay");
1086 ccb->ccb_h.status = CAM_REQ_CMP;
1087 xpt_print(ccb->ccb_h.path, lfmt, "en");
1088 rls_lun_statep(isp, tptr);
1089 isp->isp_osinfo.leact[seq] = 0;
1094 if (lep->le_header.rqs_entry_type == RQSTYPE_MODIFY_LUN) {
1095 if (isp_lun_cmd(isp, -RQSTYPE_ENABLE_LUN, XS_CHANNEL(ccb),
1096 XS_TGT(ccb), XS_LUN(ccb), 0, 0, seq+1)) {
1097 xpt_print(ccb->ccb_h.path,
1098 "isp_ledone: isp_lun_cmd failed\n");
1101 rls_lun_statep(isp, tptr);
1105 xpt_print(ccb->ccb_h.path, lfmt, "dis");
1106 rls_lun_statep(isp, tptr);
1107 destroy_lun_state(isp, tptr);
1108 ccb->ccb_h.status = CAM_REQ_CMP;
1109 isp->isp_osinfo.leact[seq] = 0;
1111 if (are_any_luns_enabled(isp, XS_CHANNEL(ccb)) == 0) {
1112 int bus = XS_CHANNEL(ccb);
1114 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1116 isp_prt(isp, ISP_LOGWARN,
1117 "disable target mode on channel %d failed", bus);
1119 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1125 isp_abort_tgt_ccb(ispsoftc_t *isp, union ccb *ccb)
1128 struct ccb_hdr_slist *lp;
1129 struct ccb_hdr *curelm;
1131 union ccb *accb = ccb->cab.abort_ccb;
1133 xpt_print(ccb->ccb_h.path, "aborting ccb %p\n", accb);
1134 if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1136 if (IS_FC(isp) && (accb->ccb_h.target_id !=
1137 ((fcparam *) isp->isp_param)->isp_loopid)) {
1139 } else if (IS_SCSI(isp) && (accb->ccb_h.target_id !=
1140 ((sdparam *) isp->isp_param)->isp_initiator_id)) {
1145 * Being restrictive about target ids is really about
1146 * making sure we're aborting for the right multi-tid
1147 * path. This doesn't really make much sense at present.
1150 return (CAM_PATH_INVALID);
1154 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
1156 xpt_print(ccb->ccb_h.path, "can't get statep\n");
1157 return (CAM_PATH_INVALID);
1159 if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1161 ctr = &tptr->atio_count;
1162 } else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
1164 ctr = &tptr->inot_count;
1166 rls_lun_statep(isp, tptr);
1167 xpt_print(ccb->ccb_h.path, "bad function code %d\n",
1168 accb->ccb_h.func_code);
1169 return (CAM_UA_ABORT);
1171 curelm = SLIST_FIRST(lp);
1173 if (curelm == &accb->ccb_h) {
1175 SLIST_REMOVE_HEAD(lp, sim_links.sle);
1177 while(curelm != NULL) {
1178 struct ccb_hdr *nextelm;
1180 nextelm = SLIST_NEXT(curelm, sim_links.sle);
1181 if (nextelm == &accb->ccb_h) {
1183 SLIST_NEXT(curelm, sim_links.sle) =
1184 SLIST_NEXT(nextelm, sim_links.sle);
1190 rls_lun_statep(isp, tptr);
1193 accb->ccb_h.status = CAM_REQ_ABORTED;
1195 return (CAM_REQ_CMP);
1197 xpt_print(ccb->ccb_h.path, "ccb %p not found\n", accb);
1198 return (CAM_PATH_INVALID);
1202 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1205 struct ccb_scsiio *cso = &ccb->csio;
1206 uint32_t nxti, optr, handle;
1207 uint8_t local[QENTRY_LEN];
1210 if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1211 xpt_print(ccb->ccb_h.path,
1212 "Request Queue Overflow in isp_target_start_ctio\n");
1213 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1216 memset(local, 0, QENTRY_LEN);
1219 * We're either moving data or completing a command here.
1223 atio_private_data_t *atp;
1224 ct2_entry_t *cto = (ct2_entry_t *) local;
1226 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1227 cto->ct_header.rqs_entry_count = 1;
1228 if (FCPARAM(isp)->isp_2klogin) {
1229 ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1231 cto->ct_iid = cso->init_id;
1232 if (FCPARAM(isp)->isp_sccfw == 0) {
1233 cto->ct_lun = ccb->ccb_h.target_lun;
1237 atp = isp_get_atpd(isp, cso->tag_id);
1239 xpt_print(ccb->ccb_h.path,
1240 "cannot find private data adjunct for tag %x\n",
1242 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
1246 cto->ct_rxid = cso->tag_id;
1247 if (cso->dxfer_len == 0) {
1248 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
1249 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1250 cto->ct_flags |= CT2_SENDSTATUS;
1251 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1253 atp->orig_datalen - atp->bytes_xfered;
1254 if (cto->ct_resid < 0) {
1255 cto->rsp.m1.ct_scsi_status |=
1257 } else if (cto->ct_resid > 0) {
1258 cto->rsp.m1.ct_scsi_status |=
1262 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1263 int m = min(cso->sense_len, MAXRESPLEN);
1264 memcpy(cto->rsp.m1.ct_resp,
1265 &cso->sense_data, m);
1266 cto->rsp.m1.ct_senselen = m;
1267 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1270 cto->ct_flags |= CT2_FLAG_MODE0;
1271 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1272 cto->ct_flags |= CT2_DATA_IN;
1274 cto->ct_flags |= CT2_DATA_OUT;
1276 cto->ct_reloff = atp->bytes_xfered;
1277 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1278 cto->ct_flags |= CT2_SENDSTATUS;
1279 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1282 (atp->bytes_xfered + cso->dxfer_len);
1283 if (cto->ct_resid < 0) {
1284 cto->rsp.m0.ct_scsi_status |=
1286 } else if (cto->ct_resid > 0) {
1287 cto->rsp.m0.ct_scsi_status |=
1291 atp->last_xframt = cso->dxfer_len;
1294 * If we're sending data and status back together,
1295 * we can't also send back sense data as well.
1297 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1300 if (cto->ct_flags & CT2_SENDSTATUS) {
1301 isp_prt(isp, ISP_LOGTDEBUG0,
1302 "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1303 cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1304 cso->dxfer_len, cto->ct_resid);
1305 cto->ct_flags |= CT2_CCINCR;
1306 atp->state = ATPD_STATE_LAST_CTIO;
1308 atp->state = ATPD_STATE_CTIO;
1310 cto->ct_timeout = 10;
1312 ct_entry_t *cto = (ct_entry_t *) local;
1314 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1315 cto->ct_header.rqs_entry_count = 1;
1316 cto->ct_iid = cso->init_id;
1317 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1318 cto->ct_tgt = ccb->ccb_h.target_id;
1319 cto->ct_lun = ccb->ccb_h.target_lun;
1320 cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1321 if (AT_HAS_TAG(cso->tag_id)) {
1322 cto->ct_tag_val = (uint8_t) AT_GET_TAG(cso->tag_id);
1323 cto->ct_flags |= CT_TQAE;
1325 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1326 cto->ct_flags |= CT_NODISC;
1328 if (cso->dxfer_len == 0) {
1329 cto->ct_flags |= CT_NO_DATA;
1330 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1331 cto->ct_flags |= CT_DATA_IN;
1333 cto->ct_flags |= CT_DATA_OUT;
1335 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1336 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1337 cto->ct_scsi_status = cso->scsi_status;
1338 cto->ct_resid = cso->resid;
1339 isp_prt(isp, ISP_LOGTDEBUG0,
1340 "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1341 cto->ct_fwhandle, cso->scsi_status, cso->resid,
1344 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1345 cto->ct_timeout = 10;
1348 if (isp_save_xs_tgt(isp, ccb, &handle)) {
1349 xpt_print(ccb->ccb_h.path,
1350 "No XFLIST pointers for isp_target_start_ctio\n");
1351 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1357 * Call the dma setup routines for this entry (and any subsequent
1358 * CTIOs) if there's data to move, and then tell the f/w it's got
1359 * new things to play with. As with isp_start's usage of DMA setup,
1360 * any swizzling is done in the machine dependent layer. Because
1361 * of this, we put the request onto the queue area first in native
1366 ct2_entry_t *cto = (ct2_entry_t *) local;
1367 cto->ct_syshandle = handle;
1369 ct_entry_t *cto = (ct_entry_t *) local;
1370 cto->ct_syshandle = handle;
1373 switch (ISP_DMASETUP(isp, cso, (ispreq_t *) local, &nxti, optr)) {
1375 ISP_ADD_REQUEST(isp, nxti);
1376 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1380 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1386 isp_destroy_tgt_handle(isp, handle);
1393 isp_refire_putback_atio(void *arg)
1396 isp_target_putback_atio(arg);
1401 isp_target_putback_atio(union ccb *ccb)
1404 struct ccb_scsiio *cso;
1405 uint32_t nxti, optr;
1410 if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1411 xpt_print(ccb->ccb_h.path,
1412 "isp_target_putback_atio: Request Queue Overflow\n");
1413 (void) timeout(isp_refire_putback_atio, ccb, 10);
1416 memset(qe, 0, QENTRY_LEN);
1419 at2_entry_t local, *at = &local;
1420 MEMZERO(at, sizeof (at2_entry_t));
1421 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1422 at->at_header.rqs_entry_count = 1;
1423 if (FCPARAM(isp)->isp_sccfw) {
1424 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1426 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1428 at->at_status = CT_OK;
1429 at->at_rxid = cso->tag_id;
1430 at->at_iid = cso->ccb_h.target_id;
1431 isp_put_atio2(isp, at, qe);
1433 at_entry_t local, *at = &local;
1434 MEMZERO(at, sizeof (at_entry_t));
1435 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1436 at->at_header.rqs_entry_count = 1;
1437 at->at_iid = cso->init_id;
1438 at->at_iid |= XS_CHANNEL(ccb) << 7;
1439 at->at_tgt = cso->ccb_h.target_id;
1440 at->at_lun = cso->ccb_h.target_lun;
1441 at->at_status = CT_OK;
1442 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1443 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1444 isp_put_atio(isp, at, qe);
1446 ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1447 ISP_ADD_REQUEST(isp, nxti);
1448 isp_complete_ctio(ccb);
1452 isp_complete_ctio(union ccb *ccb)
1454 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1455 ccb->ccb_h.status |= CAM_REQ_CMP;
1457 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1462 * Handle ATIO stuff that the generic code can't.
1463 * This means handling CDBs.
1467 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1470 int status, bus, iswildcard;
1471 struct ccb_accept_tio *atiop;
1474 * The firmware status (except for the QLTM_SVALID bit)
1475 * indicates why this ATIO was sent to us.
1477 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1479 * If the DISCONNECTS DISABLED bit is set in the flags field,
1480 * we're still connected on the SCSI bus.
1482 status = aep->at_status;
1483 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1485 * Bus Phase Sequence error. We should have sense data
1486 * suggested by the f/w. I'm not sure quite yet what
1487 * to do about this for CAM.
1489 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1490 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1493 if ((status & ~QLTM_SVALID) != AT_CDB) {
1494 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1496 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1500 bus = GET_BUS_VAL(aep->at_iid);
1501 tptr = get_lun_statep(isp, bus, aep->at_lun);
1503 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1506 * Because we can't autofeed sense data back with
1507 * a command for parallel SCSI, we can't give back
1508 * a CHECK CONDITION. We'll give back a BUSY status
1509 * instead. This works out okay because the only
1510 * time we should, in fact, get this, is in the
1511 * case that somebody configured us without the
1512 * blackhole driver, so they get what they deserve.
1514 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1522 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1523 if (atiop == NULL) {
1525 * Because we can't autofeed sense data back with
1526 * a command for parallel SCSI, we can't give back
1527 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1528 * instead. This works out okay because the only time we
1529 * should, in fact, get this, is in the case that we've
1532 xpt_print(tptr->owner,
1533 "no ATIOS for lun %d from initiator %d on channel %d\n",
1534 aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1535 if (aep->at_flags & AT_TQAE)
1536 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1538 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1539 rls_lun_statep(isp, tptr);
1542 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1544 isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1545 aep->at_lun, tptr->atio_count);
1547 atiop->ccb_h.target_id = aep->at_tgt;
1548 atiop->ccb_h.target_lun = aep->at_lun;
1550 if (aep->at_flags & AT_NODISC) {
1551 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1553 atiop->ccb_h.flags = 0;
1556 if (status & QLTM_SVALID) {
1557 size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1558 atiop->sense_len = amt;
1559 MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1561 atiop->sense_len = 0;
1564 atiop->init_id = GET_IID_VAL(aep->at_iid);
1565 atiop->cdb_len = aep->at_cdblen;
1566 MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1567 atiop->ccb_h.status = CAM_CDB_RECVD;
1569 * Construct a tag 'id' based upon tag value (which may be 0..255)
1570 * and the handle (which we have to preserve).
1572 AT_MAKE_TAGID(atiop->tag_id, bus, device_get_unit(isp->isp_dev), aep);
1573 if (aep->at_flags & AT_TQAE) {
1574 atiop->tag_action = aep->at_tag_type;
1575 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1577 xpt_done((union ccb*)atiop);
1578 isp_prt(isp, ISP_LOGTDEBUG0,
1579 "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1580 aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1581 GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1582 aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1583 "nondisc" : "disconnecting");
1584 rls_lun_statep(isp, tptr);
1589 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1593 struct ccb_accept_tio *atiop;
1594 atio_private_data_t *atp;
1597 * The firmware status (except for the QLTM_SVALID bit)
1598 * indicates why this ATIO was sent to us.
1600 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1602 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1603 isp_prt(isp, ISP_LOGWARN,
1604 "bogus atio (0x%x) leaked to platform", aep->at_status);
1605 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1609 if (FCPARAM(isp)->isp_sccfw) {
1610 lun = aep->at_scclun;
1614 tptr = get_lun_statep(isp, 0, lun);
1616 isp_prt(isp, ISP_LOGTDEBUG0,
1617 "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1618 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1620 isp_endcmd(isp, aep,
1621 SCSI_STATUS_CHECK_COND | ECMD_SVALID |
1622 (0x5 << 12) | (0x25 << 16), 0);
1627 atp = isp_get_atpd(isp, 0);
1628 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1629 if (atiop == NULL || atp == NULL) {
1632 * Because we can't autofeed sense data back with
1633 * a command for parallel SCSI, we can't give back
1634 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1635 * instead. This works out okay because the only time we
1636 * should, in fact, get this, is in the case that we've
1639 xpt_print(tptr->owner,
1640 "no %s for lun %d from initiator %d\n",
1641 (atp == NULL && atiop == NULL)? "ATIO2s *or* ATPS" :
1642 ((atp == NULL)? "ATPs" : "ATIO2s"), lun, aep->at_iid);
1643 rls_lun_statep(isp, tptr);
1644 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1647 atp->state = ATPD_STATE_ATIO;
1648 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1650 isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1651 lun, tptr->atio_count);
1653 if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1654 atiop->ccb_h.target_id = FCPARAM(isp)->isp_loopid;
1655 atiop->ccb_h.target_lun = lun;
1658 * We don't get 'suggested' sense data as we do with SCSI cards.
1660 atiop->sense_len = 0;
1662 atiop->init_id = aep->at_iid;
1663 atiop->cdb_len = ATIO2_CDBLEN;
1664 MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1665 atiop->ccb_h.status = CAM_CDB_RECVD;
1666 atiop->tag_id = aep->at_rxid;
1667 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1668 case ATIO2_TC_ATTR_SIMPLEQ:
1669 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1671 case ATIO2_TC_ATTR_HEADOFQ:
1672 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1674 case ATIO2_TC_ATTR_ORDERED:
1675 atiop->tag_action = MSG_ORDERED_Q_TAG;
1677 case ATIO2_TC_ATTR_ACAQ: /* ?? */
1678 case ATIO2_TC_ATTR_UNTAGGED:
1680 atiop->tag_action = 0;
1683 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
1685 atp->tag = atiop->tag_id;
1687 atp->orig_datalen = aep->at_datalen;
1688 atp->last_xframt = 0;
1689 atp->bytes_xfered = 0;
1690 atp->state = ATPD_STATE_CAM;
1691 xpt_done((union ccb*)atiop);
1693 isp_prt(isp, ISP_LOGTDEBUG0,
1694 "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1695 aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1696 lun, aep->at_taskflags, aep->at_datalen);
1697 rls_lun_statep(isp, tptr);
1702 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
1705 int sentstatus, ok, notify_cam, resid = 0;
1709 * CTIO and CTIO2 are close enough....
1712 ccb = isp_find_xs_tgt(isp, ((ct_entry_t *)arg)->ct_syshandle);
1713 KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1714 isp_destroy_tgt_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1717 ct2_entry_t *ct = arg;
1718 atio_private_data_t *atp = isp_get_atpd(isp, ct->ct_rxid);
1720 isp_prt(isp, ISP_LOGERR,
1721 "cannot find adjunct for %x after I/O",
1725 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1726 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1727 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1728 ccb->ccb_h.status |= CAM_SENT_SENSE;
1730 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1731 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1732 resid = ct->ct_resid;
1733 atp->bytes_xfered += (atp->last_xframt - resid);
1734 atp->last_xframt = 0;
1736 if (sentstatus || !ok) {
1739 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN,
1740 "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1741 ct->ct_rxid, ct->ct_status, ct->ct_flags,
1742 (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1743 resid, sentstatus? "FIN" : "MID");
1746 /* XXX: should really come after isp_complete_ctio */
1747 atp->state = ATPD_STATE_PDON;
1749 ct_entry_t *ct = arg;
1750 sentstatus = ct->ct_flags & CT_SENDSTATUS;
1751 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1753 * We *ought* to be able to get back to the original ATIO
1754 * here, but for some reason this gets lost. It's just as
1755 * well because it's squirrelled away as part of periph
1758 * We can live without it as long as we continue to use
1759 * the auto-replenish feature for CTIOs.
1761 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1762 if (ct->ct_status & QLTM_SVALID) {
1763 char *sp = (char *)ct;
1764 sp += CTIO_SENSE_OFFSET;
1765 ccb->csio.sense_len =
1766 min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1767 MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1768 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1770 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1771 resid = ct->ct_resid;
1773 isp_prt(isp, ISP_LOGTDEBUG0,
1774 "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1775 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1776 ct->ct_status, ct->ct_flags, resid,
1777 sentstatus? "FIN" : "MID");
1778 tval = ct->ct_fwhandle;
1780 ccb->csio.resid += resid;
1783 * We're here either because intermediate data transfers are done
1784 * and/or the final status CTIO (which may have joined with a
1785 * Data Transfer) is done.
1787 * In any case, for this platform, the upper layers figure out
1788 * what to do next, so all we do here is collect status and
1789 * pass information along. Any DMA handles have already been
1792 if (notify_cam == 0) {
1793 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
1797 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1798 (sentstatus)? " FINAL " : "MIDTERM ", tval);
1801 isp_target_putback_atio(ccb);
1803 isp_complete_ctio(ccb);
1810 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inp)
1812 return (0); /* XXXX */
1816 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
1819 switch (inp->in_status) {
1820 case IN_PORT_LOGOUT:
1821 isp_prt(isp, ISP_LOGWARN, "port logout of iid %d",
1824 case IN_PORT_CHANGED:
1825 isp_prt(isp, ISP_LOGWARN, "port changed for iid %d",
1828 case IN_GLOBAL_LOGO:
1829 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
1833 atio_private_data_t *atp = isp_get_atpd(isp, inp->in_seqid);
1834 struct ccb_immed_notify *inot = NULL;
1837 tstate_t *tptr = get_lun_statep(isp, 0, atp->lun);
1839 inot = (struct ccb_immed_notify *)
1840 SLIST_FIRST(&tptr->inots);
1843 SLIST_REMOVE_HEAD(&tptr->inots,
1845 isp_prt(isp, ISP_LOGTDEBUG0,
1846 "Take FREE INOT count now %d",
1850 isp_prt(isp, ISP_LOGWARN,
1851 "abort task RX_ID %x IID %d state %d",
1852 inp->in_seqid, inp->in_iid, atp->state);
1854 isp_prt(isp, ISP_LOGWARN,
1855 "abort task RX_ID %x from iid %d, state unknown",
1856 inp->in_seqid, inp->in_iid);
1859 inot->initiator_id = inp->in_iid;
1860 inot->sense_len = 0;
1861 inot->message_args[0] = MSG_ABORT_TAG;
1862 inot->message_args[1] = inp->in_seqid & 0xff;
1863 inot->message_args[2] = (inp->in_seqid >> 8) & 0xff;
1864 inot->ccb_h.status = CAM_MESSAGE_RECV;
1865 xpt_done((union ccb *)inot);
1877 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
1879 struct cam_sim *sim;
1882 sim = (struct cam_sim *)cbarg;
1883 isp = (ispsoftc_t *) cam_sim_softc(sim);
1885 case AC_LOST_DEVICE:
1887 uint16_t oflags, nflags;
1888 sdparam *sdp = isp->isp_param;
1891 tgt = xpt_path_target_id(path);
1893 sdp += cam_sim_bus(sim);
1894 nflags = sdp->isp_devparam[tgt].nvrm_flags;
1895 #ifndef ISP_TARGET_MODE
1896 nflags &= DPARM_SAFE_DFLT;
1897 if (isp->isp_loaded_fw) {
1898 nflags |= DPARM_NARROW | DPARM_ASYNC;
1901 nflags = DPARM_DEFAULT;
1903 oflags = sdp->isp_devparam[tgt].goal_flags;
1904 sdp->isp_devparam[tgt].goal_flags = nflags;
1905 sdp->isp_devparam[tgt].dev_update = 1;
1906 isp->isp_update |= (1 << cam_sim_bus(sim));
1907 (void) isp_control(isp,
1908 ISPCTL_UPDATE_PARAMS, NULL);
1909 sdp->isp_devparam[tgt].goal_flags = oflags;
1914 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
1920 isp_poll(struct cam_sim *sim)
1922 ispsoftc_t *isp = cam_sim_softc(sim);
1924 uint16_t sema, mbox;
1926 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1927 isp_intr(isp, isr, sema, mbox);
1932 static int isp_watchdog_work(ispsoftc_t *, XS_T *);
1935 isp_watchdog_work(ispsoftc_t *isp, XS_T *xs)
1940 * We've decided this command is dead. Make sure we're not trying
1941 * to kill a command that's already dead by getting it's handle and
1942 * and seeing whether it's still alive.
1944 handle = isp_find_handle(isp, xs);
1947 uint16_t sema, mbox;
1949 if (XS_CMD_DONE_P(xs)) {
1950 isp_prt(isp, ISP_LOGDEBUG1,
1951 "watchdog found done cmd (handle 0x%x)", handle);
1955 if (XS_CMD_WDOG_P(xs)) {
1956 isp_prt(isp, ISP_LOGDEBUG2,
1957 "recursive watchdog (handle 0x%x)", handle);
1962 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1963 isp_intr(isp, isr, sema, mbox);
1965 if (XS_CMD_DONE_P(xs)) {
1966 isp_prt(isp, ISP_LOGDEBUG2,
1967 "watchdog cleanup for handle 0x%x", handle);
1968 isp_free_pcmd(isp, (union ccb *)xs);
1969 xpt_done((union ccb *) xs);
1970 } else if (XS_CMD_GRACE_P(xs)) {
1972 * Make sure the command is *really* dead before we
1973 * release the handle (and DMA resources) for reuse.
1975 (void) isp_control(isp, ISPCTL_ABORT_CMD, xs);
1978 * After this point, the comamnd is really dead.
1980 if (XS_XFRLEN(xs)) {
1981 ISP_DMAFREE(isp, xs, handle);
1983 isp_destroy_handle(isp, handle);
1984 xpt_print(xs->ccb_h.path,
1985 "watchdog timeout for handle 0x%x\n", handle);
1986 XS_SETERR(xs, CAM_CMD_TIMEOUT);
1991 xs->ccb_h.timeout_ch = timeout(isp_watchdog, xs, hz);
1993 isp->isp_sendmarker |= 1 << XS_CHANNEL(xs);
2001 isp_watchdog(void *arg)
2007 for (r = 0, isp = isplist; r && isp; isp = isp->isp_osinfo.next) {
2009 r = isp_watchdog_work(isp, xs);
2013 printf("isp_watchdog: nobody had %p active\n", arg);
2018 #if __FreeBSD_version >= 600000
2020 isp_make_here(ispsoftc_t *isp, int tgt)
2024 * Allocate a CCB, create a wildcard path for this bus,
2025 * and schedule a rescan.
2027 ccb = xpt_alloc_ccb_nowait();
2029 isp_prt(isp, ISP_LOGWARN, "unable to alloc CCB for rescan");
2032 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
2033 cam_sim_path(isp->isp_sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2034 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2042 isp_make_gone(ispsoftc_t *isp, int tgt)
2044 struct cam_path *tp;
2045 if (xpt_create_path(&tp, NULL, cam_sim_path(isp->isp_sim), tgt,
2046 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2047 xpt_async(AC_LOST_DEVICE, tp, NULL);
2052 #define isp_make_here(isp, tgt) do { ; } while (0)
2053 #define isp_make_gone(isp, tgt) do { ; } while (0)
2058 * Gone Device Timer Function- when we have decided that a device has gone
2059 * away, we wait a specific period of time prior to telling the OS it has
2062 * This timer function fires once a second and then scans the port database
2063 * for devices that are marked dead but still have a virtual target assigned.
2064 * We decrement a counter for that port database entry, and when it hits zero,
2065 * we tell the OS the device has gone away.
2070 ispsoftc_t *isp = arg;
2072 int dbidx, tgt, more_to_do = 0;
2075 isp_prt(isp, ISP_LOGDEBUG0, "GDT timer expired");
2076 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2077 lp = &FCPARAM(isp)->portdb[dbidx];
2079 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2082 if (lp->ini_map_idx == 0) {
2085 if (lp->new_reserved == 0) {
2088 lp->new_reserved -= 1;
2089 if (lp->new_reserved != 0) {
2093 tgt = lp->ini_map_idx - 1;
2094 FCPARAM(isp)->isp_ini_map[tgt] = 0;
2095 lp->ini_map_idx = 0;
2096 lp->state = FC_PORTDB_STATE_NIL;
2097 isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2098 "Gone Device Timeout");
2099 isp_make_gone(isp, tgt);
2102 isp->isp_osinfo.gdt_running = 1;
2103 callout_reset(&isp->isp_osinfo.gdt, hz, isp_gdt, isp);
2105 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2106 "stopping Gone Device Timer");
2107 isp->isp_osinfo.gdt_running = 0;
2113 * Loop Down Timer Function- when loop goes down, a timer is started and
2114 * and after it expires we come here and take all probational devices that
2115 * the OS knows about and the tell the OS that they've gone away.
2117 * We don't clear the devices out of our port database because, when loop
2118 * come back up, we have to do some actual cleanup with the chip at that
2119 * point (implicit PLOGO, e.g., to get the chip's port database state right).
2124 ispsoftc_t *isp = arg;
2130 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Loop Down Timer expired");
2133 * Notify to the OS all targets who we now consider have departed.
2135 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2136 lp = &FCPARAM(isp)->portdb[dbidx];
2138 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
2141 if (lp->ini_map_idx == 0) {
2146 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
2150 * Mark that we've announced that this device is gone....
2155 * but *don't* change the state of the entry. Just clear
2156 * any target id stuff and announce to CAM that the
2157 * device is gone. This way any necessary PLOGO stuff
2158 * will happen when loop comes back up.
2161 tgt = lp->ini_map_idx - 1;
2162 FCPARAM(isp)->isp_ini_map[tgt] = 0;
2163 lp->ini_map_idx = 0;
2164 isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2165 "Loop Down Timeout");
2166 isp_make_gone(isp, tgt);
2170 * The loop down timer has expired. Wake up the kthread
2171 * to notice that fact (or make it false).
2173 isp->isp_osinfo.loop_down_time = isp->isp_osinfo.loop_down_limit+1;
2174 wakeup(ISP_KT_WCHAN(isp));
2179 isp_kthread(void *arg)
2181 ispsoftc_t *isp = arg;
2183 #if __FreeBSD_version < 500000
2185 #elif __FreeBSD_version < 700037
2188 mtx_lock(&isp->isp_osinfo.lock);
2191 * The first loop is for our usage where we have yet to have
2192 * gotten good fibre channel state.
2195 int wasfrozen, lb, lim;
2197 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2198 "isp_kthread: checking FC state");
2199 isp->isp_osinfo.mbox_sleep_ok = 1;
2200 lb = isp_fc_runstate(isp, 250000);
2201 isp->isp_osinfo.mbox_sleep_ok = 0;
2204 * Increment loop down time by the last sleep interval
2206 isp->isp_osinfo.loop_down_time += slp;
2209 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2210 "kthread: FC loop not up (down count %d)",
2211 isp->isp_osinfo.loop_down_time);
2213 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2214 "kthread: FC got to %d (down count %d)",
2215 lb, isp->isp_osinfo.loop_down_time);
2220 * If we've never seen loop up and we've waited longer
2221 * than quickboot time, or we've seen loop up but we've
2222 * waited longer than loop_down_limit, give up and go
2223 * to sleep until loop comes up.
2225 if (FCPARAM(isp)->loop_seen_once == 0) {
2226 lim = isp_quickboot_time;
2228 lim = isp->isp_osinfo.loop_down_limit;
2230 if (isp->isp_osinfo.loop_down_time >= lim) {
2231 isp_freeze_loopdown(isp, "loop limit hit");
2233 } else if (isp->isp_osinfo.loop_down_time < 10) {
2235 } else if (isp->isp_osinfo.loop_down_time < 30) {
2237 } else if (isp->isp_osinfo.loop_down_time < 60) {
2239 } else if (isp->isp_osinfo.loop_down_time < 120) {
2246 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2247 "isp_kthread: FC state OK");
2248 isp->isp_osinfo.loop_down_time = 0;
2253 * If we'd frozen the simq, unfreeze it now so that CAM
2254 * can start sending us commands. If the FC state isn't
2255 * okay yet, they'll hit that in isp_start which will
2256 * freeze the queue again.
2258 wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
2259 isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
2260 if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
2261 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2262 "isp_kthread: releasing simq");
2263 xpt_release_simq(isp->isp_sim, 1);
2265 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2266 "isp_kthread: sleep time %d", slp);
2267 #if __FreeBSD_version < 700037
2268 tsleep(ISP_KT_WCHAN(isp), PRIBIO, "ispf", slp * hz);
2270 msleep(ISP_KT_WCHAN(isp), &isp->isp_osinfo.lock,
2271 PRIBIO, "ispf", slp * hz);
2274 * If slp is zero, we're waking up for the first time after
2275 * things have been okay. In this case, we set a deferral state
2276 * for all commands and delay hysteresis seconds before starting
2277 * the FC state evaluation. This gives the loop/fabric a chance
2280 if (slp == 0 && isp->isp_osinfo.hysteresis) {
2281 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2282 "isp_kthread: sleep hysteresis tick time %d",
2283 isp->isp_osinfo.hysteresis * hz);
2284 #if __FreeBSD_version < 700037
2285 (void) tsleep(&isp_fabric_hysteresis, PRIBIO, "ispT",
2286 (isp->isp_osinfo.hysteresis * hz));
2288 (void) msleep(&isp_fabric_hysteresis,
2289 &isp->isp_osinfo.lock, PRIBIO, "ispT",
2290 (isp->isp_osinfo.hysteresis * hz));
2294 #if __FreeBSD_version < 500000
2296 #elif __FreeBSD_version < 700037
2299 mtx_unlock(&isp->isp_osinfo.lock);
2303 #if __FreeBSD_version < 500000
2304 static void isp_action_wrk(struct cam_sim *, union ccb *);
2306 isp_action(struct cam_sim *sim, union ccb *ccb)
2308 ispsoftc_t *isp = (ispsoftc_t *)cam_sim_softc(sim);
2310 isp_action_wrk(sim, ccb);
2313 #define isp_action isp_action_wrk
2317 isp_action(struct cam_sim *sim, union ccb *ccb)
2319 int bus, tgt, ts, error, lim;
2321 struct ccb_trans_settings *cts;
2323 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2325 isp = (ispsoftc_t *)cam_sim_softc(sim);
2326 if (isp->isp_state != ISP_RUNSTATE &&
2327 ccb->ccb_h.func_code == XPT_SCSI_IO) {
2329 if (isp->isp_state != ISP_INITSTATE) {
2331 * Lie. Say it was a selection timeout.
2333 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
2334 xpt_freeze_devq(ccb->ccb_h.path, 1);
2338 isp->isp_state = ISP_RUNSTATE;
2340 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2341 ISP_PCMD(ccb) = NULL;
2343 switch (ccb->ccb_h.func_code) {
2344 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2346 * Do a couple of preliminary checks...
2348 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2349 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2350 ccb->ccb_h.status = CAM_REQ_INVALID;
2356 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
2357 xpt_print(ccb->ccb_h.path, "invalid target\n");
2358 ccb->ccb_h.status = CAM_PATH_INVALID;
2359 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
2360 xpt_print(ccb->ccb_h.path, "invalid lun\n");
2361 ccb->ccb_h.status = CAM_PATH_INVALID;
2363 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2368 ccb->csio.scsi_status = SCSI_STATUS_OK;
2369 if (isp_get_pcmd(isp, ccb)) {
2370 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
2371 cam_freeze_devq(ccb->ccb_h.path);
2372 cam_release_devq(ccb->ccb_h.path,
2373 RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
2377 error = isp_start((XS_T *) ccb);
2380 XS_CMD_S_CLEAR(ccb);
2381 ccb->ccb_h.status |= CAM_SIM_QUEUED;
2382 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
2385 ts = ccb->ccb_h.timeout;
2386 if (ts == CAM_TIME_DEFAULT) {
2389 ts = isp_mstohz(ts);
2390 callout_reset(&PISP_PCMD(ccb)->wdog, ts,
2395 * Handle initial and subsequent loop down cases
2397 if (FCPARAM(isp)->loop_seen_once == 0) {
2398 lim = isp_quickboot_time;
2400 lim = isp->isp_osinfo.loop_down_limit;
2402 if (isp->isp_osinfo.loop_down_time >= lim) {
2403 isp_prt(isp, ISP_LOGDEBUG0,
2404 "%d.%d downtime (%d) > lim (%d)",
2405 XS_TGT(ccb), XS_LUN(ccb),
2406 isp->isp_osinfo.loop_down_time, lim);
2408 CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
2409 xpt_freeze_devq(ccb->ccb_h.path, 1);
2410 isp_free_pcmd(isp, ccb);
2414 isp_prt(isp, ISP_LOGDEBUG0,
2415 "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
2417 * Otherwise, retry in a while.
2419 cam_freeze_devq(ccb->ccb_h.path);
2420 cam_release_devq(ccb->ccb_h.path,
2421 RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
2422 XS_SETERR(ccb, CAM_REQUEUE_REQ);
2423 isp_free_pcmd(isp, ccb);
2427 XS_SETERR(ccb, CAM_REQUEUE_REQ);
2428 isp_free_pcmd(isp, ccb);
2432 isp_done((struct ccb_scsiio *) ccb);
2435 isp_prt(isp, ISP_LOGERR,
2436 "What's this? 0x%x at %d in file %s",
2437 error, __LINE__, __FILE__);
2438 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
2439 isp_free_pcmd(isp, ccb);
2444 #ifdef ISP_TARGET_MODE
2445 case XPT_EN_LUN: /* Enable LUN as a target */
2448 seq = isp_en_lun(isp, ccb);
2453 for (i = 0; isp->isp_osinfo.leact[seq] && i < 30 * 1000; i++) {
2455 uint16_t sema, mbox;
2456 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2457 isp_intr(isp, isr, sema, mbox);
2463 case XPT_NOTIFY_ACK: /* recycle notify ack */
2464 case XPT_IMMED_NOTIFY: /* Add Immediate Notify Resource */
2465 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
2468 get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2470 ccb->ccb_h.status = CAM_LUN_INVALID;
2474 ccb->ccb_h.sim_priv.entries[0].field = 0;
2475 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2476 ccb->ccb_h.flags = 0;
2478 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
2480 * Note that the command itself may not be done-
2481 * it may not even have had the first CTIO sent.
2484 isp_prt(isp, ISP_LOGTDEBUG0,
2485 "Put FREE ATIO, lun %d, count now %d",
2486 ccb->ccb_h.target_lun, tptr->atio_count);
2487 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h,
2489 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
2491 isp_prt(isp, ISP_LOGTDEBUG0,
2492 "Put FREE INOT, lun %d, count now %d",
2493 ccb->ccb_h.target_lun, tptr->inot_count);
2494 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
2497 isp_prt(isp, ISP_LOGWARN, "Got Notify ACK");;
2499 rls_lun_statep(isp, tptr);
2500 ccb->ccb_h.status = CAM_REQ_INPROG;
2503 case XPT_CONT_TARGET_IO:
2505 isp_target_start_ctio(isp, ccb);
2509 case XPT_RESET_DEV: /* BDR the specified SCSI device */
2511 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
2512 tgt = ccb->ccb_h.target_id;
2515 error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
2517 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2519 ccb->ccb_h.status = CAM_REQ_CMP;
2523 case XPT_ABORT: /* Abort the specified CCB */
2525 union ccb *accb = ccb->cab.abort_ccb;
2526 switch (accb->ccb_h.func_code) {
2527 #ifdef ISP_TARGET_MODE
2528 case XPT_ACCEPT_TARGET_IO:
2529 case XPT_IMMED_NOTIFY:
2530 ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
2532 case XPT_CONT_TARGET_IO:
2533 isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
2534 ccb->ccb_h.status = CAM_UA_ABORT;
2538 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
2540 ccb->ccb_h.status = CAM_UA_ABORT;
2542 ccb->ccb_h.status = CAM_REQ_CMP;
2546 ccb->ccb_h.status = CAM_REQ_INVALID;
2552 #ifdef CAM_NEW_TRAN_CODE
2553 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
2555 #define IS_CURRENT_SETTINGS(c) (c->flags & CCB_TRANS_CURRENT_SETTINGS)
2557 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
2559 if (!IS_CURRENT_SETTINGS(cts)) {
2560 ccb->ccb_h.status = CAM_REQ_INVALID;
2564 tgt = cts->ccb_h.target_id;
2566 #ifndef CAM_NEW_TRAN_CODE
2567 sdparam *sdp = isp->isp_param;
2570 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2574 * We always update (internally) from goal_flags
2575 * so any request to change settings just gets
2576 * vectored to that location.
2578 dptr = &sdp->isp_devparam[tgt].goal_flags;
2581 * Note that these operations affect the
2582 * the goal flags (goal_flags)- not
2583 * the current state flags. Then we mark
2584 * things so that the next operation to
2585 * this HBA will cause the update to occur.
2587 if (cts->valid & CCB_TRANS_DISC_VALID) {
2588 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
2589 *dptr |= DPARM_DISC;
2591 *dptr &= ~DPARM_DISC;
2594 if (cts->valid & CCB_TRANS_TQ_VALID) {
2595 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
2596 *dptr |= DPARM_TQING;
2598 *dptr &= ~DPARM_TQING;
2601 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) {
2602 switch (cts->bus_width) {
2603 case MSG_EXT_WDTR_BUS_16_BIT:
2604 *dptr |= DPARM_WIDE;
2607 *dptr &= ~DPARM_WIDE;
2611 * Any SYNC RATE of nonzero and SYNC_OFFSET
2612 * of nonzero will cause us to go to the
2613 * selected (from NVRAM) maximum value for
2614 * this device. At a later point, we'll
2615 * allow finer control.
2617 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
2618 (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) &&
2619 (cts->sync_offset > 0)) {
2620 *dptr |= DPARM_SYNC;
2622 *dptr &= ~DPARM_SYNC;
2624 *dptr |= DPARM_SAFE_DFLT;
2626 struct ccb_trans_settings_scsi *scsi =
2627 &cts->proto_specific.scsi;
2628 struct ccb_trans_settings_spi *spi =
2629 &cts->xport_specific.spi;
2630 sdparam *sdp = isp->isp_param;
2633 if (spi->valid == 0 && scsi->valid == 0) {
2634 ccb->ccb_h.status = CAM_REQ_CMP;
2639 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2642 * We always update (internally) from goal_flags
2643 * so any request to change settings just gets
2644 * vectored to that location.
2646 dptr = &sdp->isp_devparam[tgt].goal_flags;
2648 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
2649 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
2650 *dptr |= DPARM_DISC;
2652 *dptr &= ~DPARM_DISC;
2655 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2656 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
2657 *dptr |= DPARM_TQING;
2659 *dptr &= ~DPARM_TQING;
2662 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
2663 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
2664 *dptr |= DPARM_WIDE;
2666 *dptr &= ~DPARM_WIDE;
2672 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) &&
2673 (spi->valid & CTS_SPI_VALID_SYNC_RATE) &&
2674 (spi->sync_period && spi->sync_offset)) {
2675 *dptr |= DPARM_SYNC;
2677 * XXX: CHECK FOR LEGALITY
2679 sdp->isp_devparam[tgt].goal_period =
2681 sdp->isp_devparam[tgt].goal_offset =
2684 *dptr &= ~DPARM_SYNC;
2687 isp_prt(isp, ISP_LOGDEBUG0,
2688 "SET (%d.%d.%d) to flags %x off %x per %x",
2689 bus, tgt, cts->ccb_h.target_lun,
2690 sdp->isp_devparam[tgt].goal_flags,
2691 sdp->isp_devparam[tgt].goal_offset,
2692 sdp->isp_devparam[tgt].goal_period);
2693 sdp->isp_devparam[tgt].dev_update = 1;
2694 isp->isp_update |= (1 << bus);
2696 ccb->ccb_h.status = CAM_REQ_CMP;
2699 case XPT_GET_TRAN_SETTINGS:
2701 tgt = cts->ccb_h.target_id;
2703 #ifndef CAM_NEW_TRAN_CODE
2705 * a lot of normal SCSI things don't make sense.
2707 cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
2708 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2710 * How do you measure the width of a high
2711 * speed serial bus? Well, in bytes.
2713 * Offset and period make no sense, though, so we set
2714 * (above) a 'base' transfer speed to be gigabit.
2716 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2718 fcparam *fcp = isp->isp_param;
2719 struct ccb_trans_settings_scsi *scsi =
2720 &cts->proto_specific.scsi;
2721 struct ccb_trans_settings_fc *fc =
2722 &cts->xport_specific.fc;
2724 cts->protocol = PROTO_SCSI;
2725 cts->protocol_version = SCSI_REV_2;
2726 cts->transport = XPORT_FC;
2727 cts->transport_version = 0;
2729 scsi->valid = CTS_SCSI_VALID_TQ;
2730 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2731 fc->valid = CTS_FC_VALID_SPEED;
2732 fc->bitrate = 100000;
2733 if (fcp->isp_gbspeed == 4 || fcp->isp_gbspeed == 2)
2734 fc->bitrate *= fcp->isp_gbspeed;
2735 if (tgt > 0 && tgt < MAX_FC_TARG) {
2736 fcportdb_t *lp = &fcp->portdb[tgt];
2737 fc->wwnn = lp->node_wwn;
2738 fc->wwpn = lp->port_wwn;
2739 fc->port = lp->portid;
2740 fc->valid |= CTS_FC_VALID_WWNN |
2741 CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
2745 #ifdef CAM_NEW_TRAN_CODE
2746 struct ccb_trans_settings_scsi *scsi =
2747 &cts->proto_specific.scsi;
2748 struct ccb_trans_settings_spi *spi =
2749 &cts->xport_specific.spi;
2751 sdparam *sdp = isp->isp_param;
2752 int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2753 uint16_t dval, pval, oval;
2757 if (IS_CURRENT_SETTINGS(cts)) {
2758 sdp->isp_devparam[tgt].dev_refresh = 1;
2759 isp->isp_update |= (1 << bus);
2760 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2762 dval = sdp->isp_devparam[tgt].actv_flags;
2763 oval = sdp->isp_devparam[tgt].actv_offset;
2764 pval = sdp->isp_devparam[tgt].actv_period;
2766 dval = sdp->isp_devparam[tgt].nvrm_flags;
2767 oval = sdp->isp_devparam[tgt].nvrm_offset;
2768 pval = sdp->isp_devparam[tgt].nvrm_period;
2771 #ifndef CAM_NEW_TRAN_CODE
2772 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
2774 if (dval & DPARM_DISC) {
2775 cts->flags |= CCB_TRANS_DISC_ENB;
2777 if (dval & DPARM_TQING) {
2778 cts->flags |= CCB_TRANS_TAG_ENB;
2780 if (dval & DPARM_WIDE) {
2781 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2783 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2785 cts->valid = CCB_TRANS_BUS_WIDTH_VALID |
2786 CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2788 if ((dval & DPARM_SYNC) && oval != 0) {
2789 cts->sync_period = pval;
2790 cts->sync_offset = oval;
2792 CCB_TRANS_SYNC_RATE_VALID |
2793 CCB_TRANS_SYNC_OFFSET_VALID;
2796 cts->protocol = PROTO_SCSI;
2797 cts->protocol_version = SCSI_REV_2;
2798 cts->transport = XPORT_SPI;
2799 cts->transport_version = 2;
2805 if (dval & DPARM_DISC) {
2806 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2808 if ((dval & DPARM_SYNC) && oval && pval) {
2809 spi->sync_offset = oval;
2810 spi->sync_period = pval;
2812 spi->sync_offset = 0;
2813 spi->sync_period = 0;
2815 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2816 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2817 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2818 if (dval & DPARM_WIDE) {
2819 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2821 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2823 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2824 scsi->valid = CTS_SCSI_VALID_TQ;
2825 if (dval & DPARM_TQING) {
2826 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2828 spi->valid |= CTS_SPI_VALID_DISC;
2831 isp_prt(isp, ISP_LOGDEBUG0,
2832 "GET %s (%d.%d.%d) to flags %x off %x per %x",
2833 IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2834 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
2836 ccb->ccb_h.status = CAM_REQ_CMP;
2840 case XPT_CALC_GEOMETRY:
2841 #if __FreeBSD_version < 500000
2843 struct ccb_calc_geometry *ccg;
2844 u_int32_t secs_per_cylinder;
2848 if (ccg->block_size == 0) {
2849 ccb->ccb_h.status = CAM_REQ_INVALID;
2853 size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2854 if (size_mb > 1024) {
2856 ccg->secs_per_track = 63;
2859 ccg->secs_per_track = 32;
2861 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2862 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2863 ccb->ccb_h.status = CAM_REQ_CMP;
2869 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2874 case XPT_RESET_BUS: /* Reset the specified bus */
2875 bus = cam_sim_bus(sim);
2876 error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2878 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2881 xpt_print(ccb->ccb_h.path, "reset bus\n");
2883 if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2884 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2885 else if (isp->isp_path != NULL)
2886 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2887 ccb->ccb_h.status = CAM_REQ_CMP;
2892 case XPT_TERM_IO: /* Terminate the I/O process */
2893 ccb->ccb_h.status = CAM_REQ_INVALID;
2897 case XPT_PATH_INQ: /* Path routing inquiry */
2899 struct ccb_pathinq *cpi = &ccb->cpi;
2901 cpi->version_num = 1;
2902 #ifdef ISP_TARGET_MODE
2903 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2905 cpi->target_sprt = 0;
2907 cpi->hba_eng_cnt = 0;
2908 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
2909 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
2910 cpi->bus_id = cam_sim_bus(sim);
2912 cpi->hba_misc = PIM_NOBUSRESET;
2914 * Because our loop ID can shift from time to time,
2915 * make our initiator ID out of range of our bus.
2917 cpi->initiator_id = cpi->max_target + 1;
2920 * Set base transfer capabilities for Fibre Channel.
2921 * Technically not correct because we don't know
2922 * what media we're running on top of- but we'll
2923 * look good if we always say 100MB/s.
2925 cpi->base_transfer_speed = 100000;
2926 if (FCPARAM(isp)->isp_gbspeed == 4 ||
2927 FCPARAM(isp)->isp_gbspeed == 2)
2928 cpi->base_transfer_speed *=
2929 FCPARAM(isp)->isp_gbspeed;
2930 cpi->hba_inquiry = PI_TAG_ABLE;
2931 #ifdef CAM_NEW_TRAN_CODE
2932 cpi->transport = XPORT_FC;
2933 cpi->transport_version = 0;
2936 sdparam *sdp = isp->isp_param;
2937 sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
2938 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
2940 cpi->initiator_id = sdp->isp_initiator_id;
2941 cpi->base_transfer_speed = 3300;
2942 #ifdef CAM_NEW_TRAN_CODE
2943 cpi->transport = XPORT_SPI;
2944 cpi->transport_version = 2;
2947 #ifdef CAM_NEW_TRAN_CODE
2948 cpi->protocol = PROTO_SCSI;
2949 cpi->protocol_version = SCSI_REV_2;
2951 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2952 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
2953 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2954 cpi->unit_number = cam_sim_unit(sim);
2955 cpi->ccb_h.status = CAM_REQ_CMP;
2960 ccb->ccb_h.status = CAM_REQ_INVALID;
2966 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
2969 isp_done(struct ccb_scsiio *sccb)
2971 ispsoftc_t *isp = XS_ISP(sccb);
2974 XS_SETERR(sccb, CAM_REQ_CMP);
2976 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
2977 (sccb->scsi_status != SCSI_STATUS_OK)) {
2978 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
2979 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) &&
2980 (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
2981 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
2983 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2987 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2988 if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2989 isp_prt(isp, ISP_LOGDEBUG0,
2990 "target %d lun %d CAM status 0x%x SCSI status 0x%x",
2991 XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status,
2993 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2994 sccb->ccb_h.status |= CAM_DEV_QFRZN;
2995 xpt_freeze_devq(sccb->ccb_h.path, 1);
2999 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
3000 (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3001 xpt_print(sccb->ccb_h.path,
3002 "cam completion status 0x%x\n", sccb->ccb_h.status);
3005 XS_CMD_S_DONE(sccb);
3006 if (XS_CMD_WDOG_P(sccb) == 0) {
3007 untimeout(isp_watchdog, sccb, sccb->ccb_h.timeout_ch);
3008 if (XS_CMD_GRACE_P(sccb)) {
3009 isp_prt(isp, ISP_LOGDEBUG2,
3010 "finished command on borrowed time");
3012 XS_CMD_S_CLEAR(sccb);
3013 isp_free_pcmd(isp, (union ccb *) sccb);
3014 xpt_done((union ccb *) sccb);
3019 isp_async(ispsoftc_t *isp, ispasync_t cmd, void *arg)
3022 static const char prom[] =
3023 "PortID 0x%06x handle 0x%x role %s %s\n"
3024 " WWNN 0x%08x%08x WWPN 0x%08x%08x";
3025 static const char prom2[] =
3026 "PortID 0x%06x handle 0x%x role %s %s tgt %u\n"
3027 " WWNN 0x%08x%08x WWPN 0x%08x%08x";
3031 struct cam_path *tmppath;
3034 case ISPASYNC_NEW_TGT_PARAMS:
3036 #ifdef CAM_NEW_TRAN_CODE
3037 struct ccb_trans_settings_scsi *scsi;
3038 struct ccb_trans_settings_spi *spi;
3041 sdparam *sdp = isp->isp_param;
3042 struct ccb_trans_settings cts;
3044 memset(&cts, 0, sizeof (struct ccb_trans_settings));
3046 tgt = *((int *)arg);
3047 bus = (tgt >> 16) & 0xffff;
3050 if (xpt_create_path(&tmppath, NULL,
3051 cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
3052 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3053 isp_prt(isp, ISP_LOGWARN,
3054 "isp_async cannot make temp path for %d.%d",
3059 flags = sdp->isp_devparam[tgt].actv_flags;
3060 #ifdef CAM_NEW_TRAN_CODE
3061 cts.type = CTS_TYPE_CURRENT_SETTINGS;
3062 cts.protocol = PROTO_SCSI;
3063 cts.transport = XPORT_SPI;
3065 scsi = &cts.proto_specific.scsi;
3066 spi = &cts.xport_specific.spi;
3068 if (flags & DPARM_TQING) {
3069 scsi->valid |= CTS_SCSI_VALID_TQ;
3070 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3073 if (flags & DPARM_DISC) {
3074 spi->valid |= CTS_SPI_VALID_DISC;
3075 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3077 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3078 if (flags & DPARM_WIDE) {
3079 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3081 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3083 if (flags & DPARM_SYNC) {
3084 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3085 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3086 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3087 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3090 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
3091 cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
3092 if (flags & DPARM_DISC) {
3093 cts.flags |= CCB_TRANS_DISC_ENB;
3095 if (flags & DPARM_TQING) {
3096 cts.flags |= CCB_TRANS_TAG_ENB;
3098 cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
3099 cts.bus_width = (flags & DPARM_WIDE)?
3100 MSG_EXT_WDTR_BUS_8_BIT : MSG_EXT_WDTR_BUS_16_BIT;
3101 cts.sync_period = sdp->isp_devparam[tgt].actv_period;
3102 cts.sync_offset = sdp->isp_devparam[tgt].actv_offset;
3103 if (flags & DPARM_SYNC) {
3105 CCB_TRANS_SYNC_RATE_VALID |
3106 CCB_TRANS_SYNC_OFFSET_VALID;
3109 isp_prt(isp, ISP_LOGDEBUG2,
3110 "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
3111 bus, tgt, sdp->isp_devparam[tgt].actv_period,
3112 sdp->isp_devparam[tgt].actv_offset, flags);
3113 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3114 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3115 xpt_free_path(tmppath);
3118 case ISPASYNC_BUS_RESET:
3119 bus = *((int *)arg);
3120 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
3122 if (bus > 0 && isp->isp_path2) {
3123 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
3124 } else if (isp->isp_path) {
3125 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
3130 msg = "LIP Received";
3133 case ISPASYNC_LOOP_RESET:
3138 case ISPASYNC_LOOP_DOWN:
3142 if (isp->isp_path) {
3143 isp_freeze_loopdown(isp, msg);
3145 if (isp->isp_osinfo.ldt_running == 0) {
3146 isp->isp_osinfo.ldt_running = 1;
3147 callout_reset(&isp->isp_osinfo.ldt,
3148 isp->isp_osinfo.loop_down_limit * hz, isp_ldt, isp);
3149 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3150 "starting Loop Down Timer");
3152 isp_prt(isp, ISP_LOGINFO, msg);
3154 case ISPASYNC_LOOP_UP:
3156 * Now we just note that Loop has come up. We don't
3157 * actually do anything because we're waiting for a
3158 * Change Notify before activating the FC cleanup
3159 * thread to look at the state of the loop again.
3161 isp_prt(isp, ISP_LOGINFO, "Loop UP");
3163 case ISPASYNC_DEV_ARRIVED:
3166 if ((isp->isp_role & ISP_ROLE_INITIATOR) &&
3167 (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
3168 int dbidx = lp - FCPARAM(isp)->portdb;
3171 for (i = 0; i < MAX_FC_TARG; i++) {
3172 if (i >= FL_ID && i <= SNS_ID) {
3175 if (FCPARAM(isp)->isp_ini_map[i] == 0) {
3179 if (i < MAX_FC_TARG) {
3180 FCPARAM(isp)->isp_ini_map[i] = dbidx + 1;
3181 lp->ini_map_idx = i + 1;
3183 isp_prt(isp, ISP_LOGWARN, "out of target ids");
3184 isp_dump_portdb(isp);
3187 if (lp->ini_map_idx) {
3188 tgt = lp->ini_map_idx - 1;
3189 isp_prt(isp, ISP_LOGCONFIG, prom2,
3190 lp->portid, lp->handle,
3191 roles[lp->roles], "arrived at", tgt,
3192 (uint32_t) (lp->node_wwn >> 32),
3193 (uint32_t) lp->node_wwn,
3194 (uint32_t) (lp->port_wwn >> 32),
3195 (uint32_t) lp->port_wwn);
3196 isp_make_here(isp, tgt);
3198 isp_prt(isp, ISP_LOGCONFIG, prom,
3199 lp->portid, lp->handle,
3200 roles[lp->roles], "arrived",
3201 (uint32_t) (lp->node_wwn >> 32),
3202 (uint32_t) lp->node_wwn,
3203 (uint32_t) (lp->port_wwn >> 32),
3204 (uint32_t) lp->port_wwn);
3207 case ISPASYNC_DEV_CHANGED:
3209 if (isp_change_is_bad) {
3210 lp->state = FC_PORTDB_STATE_NIL;
3211 if (lp->ini_map_idx) {
3212 tgt = lp->ini_map_idx - 1;
3213 FCPARAM(isp)->isp_ini_map[tgt] = 0;
3214 lp->ini_map_idx = 0;
3215 isp_prt(isp, ISP_LOGCONFIG, prom3,
3216 lp->portid, tgt, "change is bad");
3217 isp_make_gone(isp, tgt);
3219 isp_prt(isp, ISP_LOGCONFIG, prom,
3220 lp->portid, lp->handle,
3222 "changed and departed",
3223 (uint32_t) (lp->node_wwn >> 32),
3224 (uint32_t) lp->node_wwn,
3225 (uint32_t) (lp->port_wwn >> 32),
3226 (uint32_t) lp->port_wwn);
3229 lp->portid = lp->new_portid;
3230 lp->roles = lp->new_roles;
3231 if (lp->ini_map_idx) {
3232 int t = lp->ini_map_idx - 1;
3233 FCPARAM(isp)->isp_ini_map[t] =
3234 (lp - FCPARAM(isp)->portdb) + 1;
3235 tgt = lp->ini_map_idx - 1;
3236 isp_prt(isp, ISP_LOGCONFIG, prom2,
3237 lp->portid, lp->handle,
3238 roles[lp->roles], "changed at", tgt,
3239 (uint32_t) (lp->node_wwn >> 32),
3240 (uint32_t) lp->node_wwn,
3241 (uint32_t) (lp->port_wwn >> 32),
3242 (uint32_t) lp->port_wwn);
3244 isp_prt(isp, ISP_LOGCONFIG, prom,
3245 lp->portid, lp->handle,
3246 roles[lp->roles], "changed",
3247 (uint32_t) (lp->node_wwn >> 32),
3248 (uint32_t) lp->node_wwn,
3249 (uint32_t) (lp->port_wwn >> 32),
3250 (uint32_t) lp->port_wwn);
3254 case ISPASYNC_DEV_STAYED:
3256 if (lp->ini_map_idx) {
3257 tgt = lp->ini_map_idx - 1;
3258 isp_prt(isp, ISP_LOGCONFIG, prom2,
3259 lp->portid, lp->handle,
3260 roles[lp->roles], "stayed at", tgt,
3261 (uint32_t) (lp->node_wwn >> 32),
3262 (uint32_t) lp->node_wwn,
3263 (uint32_t) (lp->port_wwn >> 32),
3264 (uint32_t) lp->port_wwn);
3266 isp_prt(isp, ISP_LOGCONFIG, prom,
3267 lp->portid, lp->handle,
3268 roles[lp->roles], "stayed",
3269 (uint32_t) (lp->node_wwn >> 32),
3270 (uint32_t) lp->node_wwn,
3271 (uint32_t) (lp->port_wwn >> 32),
3272 (uint32_t) lp->port_wwn);
3275 case ISPASYNC_DEV_GONE:
3278 * If this has a virtual target and we haven't marked it
3279 * that we're going to have isp_gdt tell the OS it's gone,
3280 * set the isp_gdt timer running on it.
3282 * If it isn't marked that isp_gdt is going to get rid of it,
3283 * announce that it's gone.
3285 if (lp->ini_map_idx && lp->reserved == 0) {
3287 lp->new_reserved = isp->isp_osinfo.gone_device_time;
3288 lp->state = FC_PORTDB_STATE_ZOMBIE;
3289 if (isp->isp_osinfo.gdt_running == 0) {
3290 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3291 "starting Gone Device Timer");
3292 isp->isp_osinfo.gdt_running = 1;
3293 callout_reset(&isp->isp_osinfo.gdt, hz,
3296 tgt = lp->ini_map_idx - 1;
3297 isp_prt(isp, ISP_LOGCONFIG, prom2,
3298 lp->portid, lp->handle,
3299 roles[lp->roles], "gone zombie at", tgt,
3300 (uint32_t) (lp->node_wwn >> 32),
3301 (uint32_t) lp->node_wwn,
3302 (uint32_t) (lp->port_wwn >> 32),
3303 (uint32_t) lp->port_wwn);
3304 } else if (lp->reserved == 0) {
3305 isp_prt(isp, ISP_LOGCONFIG, prom,
3306 lp->portid, lp->handle,
3307 roles[lp->roles], "departed",
3308 (uint32_t) (lp->node_wwn >> 32),
3309 (uint32_t) lp->node_wwn,
3310 (uint32_t) (lp->port_wwn >> 32),
3311 (uint32_t) lp->port_wwn);
3314 case ISPASYNC_CHANGE_NOTIFY:
3317 if (arg == ISPASYNC_CHANGE_PDB) {
3318 msg = "Port Database Changed";
3319 } else if (arg == ISPASYNC_CHANGE_SNS) {
3320 msg = "Name Server Database Changed";
3322 msg = "Other Change Notify";
3325 * If the loop down timer is running, cancel it.
3327 if (isp->isp_osinfo.ldt_running) {
3328 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3329 "Stopping Loop Down Timer");
3330 isp->isp_osinfo.ldt_running = 0;
3331 callout_stop(&isp->isp_osinfo.ldt);
3333 isp_prt(isp, ISP_LOGINFO, msg);
3334 isp_freeze_loopdown(isp, msg);
3335 wakeup(ISP_KT_WCHAN(isp));
3338 #ifdef ISP_TARGET_MODE
3339 case ISPASYNC_TARGET_NOTIFY:
3341 tmd_notify_t *nt = arg;
3342 isp_prt(isp, ISP_LOGALL,
3343 "target notify code 0x%x", nt->nt_ncode);
3346 case ISPASYNC_TARGET_ACTION:
3347 switch (((isphdr_t *)arg)->rqs_entry_type) {
3349 isp_prt(isp, ISP_LOGWARN,
3350 "event 0x%x for unhandled target action",
3351 ((isphdr_t *)arg)->rqs_entry_type);
3353 case RQSTYPE_NOTIFY:
3355 rv = isp_handle_platform_notify_scsi(isp,
3356 (in_entry_t *) arg);
3358 rv = isp_handle_platform_notify_fc(isp,
3359 (in_fcentry_t *) arg);
3363 rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
3366 rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
3371 rv = isp_handle_platform_ctio(isp, arg);
3373 case RQSTYPE_ENABLE_LUN:
3374 case RQSTYPE_MODIFY_LUN:
3375 isp_ledone(isp, (lun_entry_t *) arg);
3380 case ISPASYNC_FW_CRASH:
3382 uint16_t mbox1, mbox6;
3383 mbox1 = ISP_READ(isp, OUTMAILBOX1);
3384 if (IS_DUALBUS(isp)) {
3385 mbox6 = ISP_READ(isp, OUTMAILBOX6);
3389 isp_prt(isp, ISP_LOGERR,
3390 "Internal Firmware Error on bus %d @ RISC Address 0x%x",
3392 #ifdef ISP_FW_CRASH_DUMP
3393 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
3394 isp->isp_osinfo.mbox_sleep_ok = 0;
3396 FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3397 FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3398 isp_freeze_loopdown(isp, "f/w crash");
3402 isp->isp_osinfo.mbox_sleep_ok = mbox1;
3404 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
3405 isp->isp_osinfo.mbox_sleep_ok = 0;
3407 isp->isp_osinfo.mbox_sleep_ok = mbox1;
3409 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3412 case ISPASYNC_UNHANDLED_RESPONSE:
3415 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3423 * Locks are held before coming here.
3426 isp_uninit(ispsoftc_t *isp)
3429 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
3431 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
3433 ISP_DISABLE_INTS(isp);
3437 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3440 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3443 printf("%s: ", device_get_nameunit(isp->isp_dev));
3451 isp_nanotime_sub(struct timespec *b, struct timespec *a)
3454 struct timespec x = *b;
3456 elapsed = GET_NANOSEC(&x);
3463 isp_mbox_acquire(ispsoftc_t *isp)
3465 if (isp->isp_osinfo.mboxbsy) {
3468 isp->isp_osinfo.mboxcmd_done = 0;
3469 isp->isp_osinfo.mboxbsy = 1;
3475 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
3477 unsigned int usecs = mbp->timeout;
3478 unsigned int max, olim, ilim;
3481 usecs = MBCMD_DEFAULT_TIMEOUT;
3483 max = isp->isp_mbxwrk0 + 1;
3485 if (isp->isp_osinfo.mbox_sleep_ok) {
3486 unsigned int ms = (usecs + 999) / 1000;
3488 isp->isp_osinfo.mbox_sleep_ok = 0;
3489 isp->isp_osinfo.mbox_sleeping = 1;
3490 for (olim = 0; olim < max; olim++) {
3491 #if __FreeBSD_version < 700037
3492 tsleep(&isp->isp_mbxworkp, PRIBIO, "ispmbx_sleep",
3495 msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock,
3496 PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
3498 if (isp->isp_osinfo.mboxcmd_done) {
3502 isp->isp_osinfo.mbox_sleep_ok = 1;
3503 isp->isp_osinfo.mbox_sleeping = 0;
3505 for (olim = 0; olim < max; olim++) {
3506 for (ilim = 0; ilim < usecs; ilim += 100) {
3508 uint16_t sema, mbox;
3509 if (isp->isp_osinfo.mboxcmd_done) {
3512 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3513 isp_intr(isp, isr, sema, mbox);
3514 if (isp->isp_osinfo.mboxcmd_done) {
3520 if (isp->isp_osinfo.mboxcmd_done) {
3525 if (isp->isp_osinfo.mboxcmd_done == 0) {
3526 isp_prt(isp, ISP_LOGWARN,
3527 "%s Mailbox Command (0x%x) Timeout (%uus)",
3528 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
3529 isp->isp_lastmbxcmd, usecs);
3530 mbp->param[0] = MBOX_TIMEOUT;
3531 isp->isp_osinfo.mboxcmd_done = 1;
3536 isp_mbox_notify_done(ispsoftc_t *isp)
3538 if (isp->isp_osinfo.mbox_sleeping) {
3539 wakeup(&isp->isp_mbxworkp);
3541 isp->isp_osinfo.mboxcmd_done = 1;
3545 isp_mbox_release(ispsoftc_t *isp)
3547 isp->isp_osinfo.mboxbsy = 0;
3555 t.tv_sec = ms / 1000;
3556 t.tv_usec = (ms % 1000) * 1000;
3568 isp_platform_intr(void *arg)
3570 ispsoftc_t *isp = arg;
3572 uint16_t sema, mbox;
3576 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
3577 isp->isp_intbogus++;
3579 isp_intr(isp, isr, sema, mbox);
3585 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
3587 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3588 bus_dmamap_sync(isp->isp_osinfo.dmat,
3589 PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
3591 bus_dmamap_sync(isp->isp_osinfo.dmat,
3592 PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
3594 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);