1 /* $FreeBSD: src/sys/dev/isp/isp_freebsd.c,v 1.32.2.20 2002/10/11 18:49:25 mjacob Exp $ */
2 /* $DragonFly: src/sys/dev/disk/isp/isp_freebsd.c,v 1.12 2005/02/04 02:55:43 dillon Exp $ */
4 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
6 * Copyright (c) 1997, 1998, 1999, 2000, 2001 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
29 #include "isp_freebsd.h"
30 #include <sys/unistd.h>
31 #include <sys/kthread.h>
32 #include <machine/stdarg.h> /* for use by isp_prt below */
34 #include <sys/ioccom.h>
35 #include "isp_ioctl.h"
38 static d_ioctl_t ispioctl;
39 static void isp_intr_enable(void *);
40 static void isp_cam_async(void *, u_int32_t, struct cam_path *, void *);
41 static void isp_poll(struct cam_sim *);
42 static timeout_t isp_watchdog;
43 static void isp_kthread(void *);
44 static void isp_action(struct cam_sim *, union ccb *);
47 #define ISP_CDEV_MAJOR 248
48 static struct cdevsw isp_cdevsw = {
50 /* maj */ ISP_CDEV_MAJOR,
56 /* close */ nullclose,
62 /* strategy */ nostrategy,
67 static struct ispsoftc *isplist = NULL;
70 isp_attach(struct ispsoftc *isp)
72 int primary, secondary;
73 struct ccb_setasync csa;
74 struct cam_devq *devq;
76 struct cam_path *path;
79 * Establish (in case of 12X0) which bus is the primary.
86 * Create the device queue for our SIM(s).
88 devq = cam_simq_alloc(isp->isp_maxcmds);
94 * Construct our SIM entry.
96 ISPLOCK_2_CAMLOCK(isp);
97 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
98 device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
99 cam_simq_release(devq); /* leaves 1 ref due to cam_sim_alloc */
101 CAMLOCK_2_ISPLOCK(isp);
104 CAMLOCK_2_ISPLOCK(isp);
106 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
107 isp->isp_osinfo.ehook.ich_arg = isp;
108 isp->isp_osinfo.ehook.ich_desc = "isp";
109 ISPLOCK_2_CAMLOCK(isp);
110 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
112 CAMLOCK_2_ISPLOCK(isp);
113 isp_prt(isp, ISP_LOGERR,
114 "could not establish interrupt enable hook");
118 if (xpt_bus_register(sim, primary) != CAM_SUCCESS) {
120 CAMLOCK_2_ISPLOCK(isp);
124 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
125 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
126 xpt_bus_deregister(cam_sim_path(sim));
128 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
129 CAMLOCK_2_ISPLOCK(isp);
133 xpt_setup_ccb(&csa.ccb_h, path, 5);
134 csa.ccb_h.func_code = XPT_SASYNC_CB;
135 csa.event_enable = AC_LOST_DEVICE;
136 csa.callback = isp_cam_async;
137 csa.callback_arg = sim;
138 xpt_action((union ccb *)&csa);
139 CAMLOCK_2_ISPLOCK(isp);
141 isp->isp_path = path;
143 * Create a kernel thread for fibre channel instances. We
144 * don't have dual channel FC cards.
147 ISPLOCK_2_CAMLOCK(isp);
148 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kthread,
149 "%s: fc_thrd", device_get_nameunit(isp->isp_dev))) {
150 xpt_bus_deregister(cam_sim_path(sim));
152 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
153 CAMLOCK_2_ISPLOCK(isp);
154 isp_prt(isp, ISP_LOGERR, "could not create kthread");
157 CAMLOCK_2_ISPLOCK(isp);
162 * If we have a second channel, construct SIM entry for that.
164 if (IS_DUALBUS(isp)) {
165 ISPLOCK_2_CAMLOCK(isp);
166 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
167 device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
169 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
170 xpt_free_path(isp->isp_path);
171 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
174 if (xpt_bus_register(sim, secondary) != CAM_SUCCESS) {
175 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
176 xpt_free_path(isp->isp_path);
178 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
179 CAMLOCK_2_ISPLOCK(isp);
183 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
184 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
185 xpt_bus_deregister(cam_sim_path(isp->isp_sim));
186 xpt_free_path(isp->isp_path);
187 xpt_bus_deregister(cam_sim_path(sim));
189 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
190 CAMLOCK_2_ISPLOCK(isp);
194 xpt_setup_ccb(&csa.ccb_h, path, 5);
195 csa.ccb_h.func_code = XPT_SASYNC_CB;
196 csa.event_enable = AC_LOST_DEVICE;
197 csa.callback = isp_cam_async;
198 csa.callback_arg = sim;
199 xpt_action((union ccb *)&csa);
200 CAMLOCK_2_ISPLOCK(isp);
202 isp->isp_path2 = path;
205 * Create device nodes
207 cdevsw_add(&isp_cdevsw, -1, device_get_unit(isp->isp_dev));
208 make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
209 GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
211 if (isp->isp_role != ISP_ROLE_NONE) {
212 isp->isp_state = ISP_RUNSTATE;
214 if (isplist == NULL) {
217 struct ispsoftc *tmp = isplist;
218 while (tmp->isp_osinfo.next) {
219 tmp = tmp->isp_osinfo.next;
221 tmp->isp_osinfo.next = isp;
227 isp_freeze_loopdown(struct ispsoftc *isp, char *msg)
229 if (isp->isp_osinfo.simqfrozen == 0) {
230 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown)", msg);
231 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
232 ISPLOCK_2_CAMLOCK(isp);
233 xpt_freeze_simq(isp->isp_sim, 1);
234 CAMLOCK_2_ISPLOCK(isp);
236 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown)", msg);
237 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
242 ispioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, d_thread_t *td)
244 struct ispsoftc *isp;
249 if (minor(dev) == device_get_unit(isp->isp_dev)) {
252 isp = isp->isp_osinfo.next;
258 #ifdef ISP_FW_CRASH_DUMP
259 case ISP_GET_FW_CRASH_DUMP:
261 u_int16_t *ptr = FCPARAM(isp)->isp_dump_data;
266 sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
268 sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
271 void *uaddr = *((void **) addr);
272 if (copyout(ptr, uaddr, sz)) {
284 case ISP_FORCE_CRASH_DUMP:
286 isp_freeze_loopdown(isp, "ispioctl(ISP_FORCE_CRASH_DUMP)");
295 int olddblev = isp->isp_dblev;
296 isp->isp_dblev = *(int *)addr;
297 *(int *)addr = olddblev;
310 if (isp_fc_runstate(isp, 5 * 1000000)) {
321 if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
329 case ISP_FC_GETDINFO:
331 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
334 if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
339 lp = &FCPARAM(isp)->portdb[ifc->loopid];
341 ifc->loopid = lp->loopid;
342 ifc->portid = lp->portid;
343 ifc->node_wwn = lp->node_wwn;
344 ifc->port_wwn = lp->port_wwn;
354 isp_stats_t *sp = (isp_stats_t *) addr;
356 MEMZERO(sp, sizeof (*sp));
357 sp->isp_stat_version = ISP_STATS_VERSION;
358 sp->isp_type = isp->isp_type;
359 sp->isp_revision = isp->isp_revision;
361 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
362 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
363 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
364 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
365 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
366 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
367 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
368 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
376 isp->isp_intbogus = 0;
377 isp->isp_intmboxc = 0;
378 isp->isp_intoasync = 0;
379 isp->isp_rsltccmplt = 0;
380 isp->isp_fphccmplt = 0;
381 isp->isp_rscchiwater = 0;
382 isp->isp_fpcchiwater = 0;
386 case ISP_FC_GETHINFO:
388 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
389 MEMZERO(hba, sizeof (*hba));
391 hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
392 hba->fc_scsi_supported = 1;
393 hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
394 hba->fc_loopid = FCPARAM(isp)->isp_loopid;
395 hba->active_node_wwn = FCPARAM(isp)->isp_nodewwn;
396 hba->active_port_wwn = FCPARAM(isp)->isp_portwwn;
401 case ISP_GET_FC_PARAM:
403 struct isp_fc_param *f = (struct isp_fc_param *) addr;
410 if (strcmp(f->param_name, "framelength") == 0) {
411 f->parameter = FCPARAM(isp)->isp_maxfrmlen;
415 if (strcmp(f->param_name, "exec_throttle") == 0) {
416 f->parameter = FCPARAM(isp)->isp_execthrottle;
420 if (strcmp(f->param_name, "fullduplex") == 0) {
421 if (FCPARAM(isp)->isp_fwoptions & ICBOPT_FULL_DUPLEX)
426 if (strcmp(f->param_name, "loopid") == 0) {
427 f->parameter = FCPARAM(isp)->isp_loopid;
434 case ISP_SET_FC_PARAM:
436 struct isp_fc_param *f = (struct isp_fc_param *) addr;
437 u_int32_t param = f->parameter;
444 if (strcmp(f->param_name, "framelength") == 0) {
445 if (param != 512 && param != 1024 && param != 1024) {
449 FCPARAM(isp)->isp_maxfrmlen = param;
453 if (strcmp(f->param_name, "exec_throttle") == 0) {
454 if (param < 16 || param > 255) {
458 FCPARAM(isp)->isp_execthrottle = param;
462 if (strcmp(f->param_name, "fullduplex") == 0) {
463 if (param != 0 && param != 1) {
468 FCPARAM(isp)->isp_fwoptions |=
471 FCPARAM(isp)->isp_fwoptions &=
477 if (strcmp(f->param_name, "loopid") == 0) {
478 if (param < 0 || param > 125) {
482 FCPARAM(isp)->isp_loopid = param;
496 isp_intr_enable(void *arg)
498 struct ispsoftc *isp = arg;
499 if (isp->isp_role != ISP_ROLE_NONE) {
502 /* Release our hook so that the boot can continue. */
503 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
507 * Put the target mode functions here, because some are inlines
510 #ifdef ISP_TARGET_MODE
512 static INLINE int is_lun_enabled(struct ispsoftc *, int, lun_id_t);
513 static INLINE int are_any_luns_enabled(struct ispsoftc *, int);
514 static INLINE tstate_t *get_lun_statep(struct ispsoftc *, int, lun_id_t);
515 static INLINE void rls_lun_statep(struct ispsoftc *, tstate_t *);
516 static INLINE int isp_psema_sig_rqe(struct ispsoftc *, int);
517 static INLINE int isp_cv_wait_timed_rqe(struct ispsoftc *, int, int);
518 static INLINE void isp_cv_signal_rqe(struct ispsoftc *, int, int);
519 static INLINE void isp_vsema_rqe(struct ispsoftc *, int);
520 static INLINE atio_private_data_t *isp_get_atpd(struct ispsoftc *, int);
522 create_lun_state(struct ispsoftc *, int, struct cam_path *, tstate_t **);
523 static void destroy_lun_state(struct ispsoftc *, tstate_t *);
524 static void isp_en_lun(struct ispsoftc *, union ccb *);
525 static cam_status isp_abort_tgt_ccb(struct ispsoftc *, union ccb *);
526 static timeout_t isp_refire_putback_atio;
527 static void isp_complete_ctio(union ccb *);
528 static void isp_target_putback_atio(union ccb *);
529 static cam_status isp_target_start_ctio(struct ispsoftc *, union ccb *);
530 static int isp_handle_platform_atio(struct ispsoftc *, at_entry_t *);
531 static int isp_handle_platform_atio2(struct ispsoftc *, at2_entry_t *);
532 static int isp_handle_platform_ctio(struct ispsoftc *, void *);
533 static int isp_handle_platform_notify_scsi(struct ispsoftc *, in_entry_t *);
534 static int isp_handle_platform_notify_fc(struct ispsoftc *, in_fcentry_t *);
537 is_lun_enabled(struct ispsoftc *isp, int bus, lun_id_t lun)
540 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
545 if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
548 } while ((tptr = tptr->next) != NULL);
553 are_any_luns_enabled(struct ispsoftc *isp, int port)
556 if (IS_DUALBUS(isp)) {
557 lo = (port * (LUN_HASH_SIZE >> 1));
558 hi = lo + (LUN_HASH_SIZE >> 1);
563 for (lo = 0; lo < hi; lo++) {
564 if (isp->isp_osinfo.lun_hash[lo]) {
571 static INLINE tstate_t *
572 get_lun_statep(struct ispsoftc *isp, int bus, lun_id_t lun)
574 tstate_t *tptr = NULL;
576 if (lun == CAM_LUN_WILDCARD) {
577 if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
578 tptr = &isp->isp_osinfo.tsdflt[bus];
583 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
590 if (tptr->lun == lun && tptr->bus == bus) {
594 } while ((tptr = tptr->next) != NULL);
599 rls_lun_statep(struct ispsoftc *isp, tstate_t *tptr)
606 isp_psema_sig_rqe(struct ispsoftc *isp, int bus)
608 while (isp->isp_osinfo.tmflags[bus] & TM_BUSY) {
609 isp->isp_osinfo.tmflags[bus] |= TM_WANTED;
610 if (tsleep(&isp->isp_osinfo.tmflags[bus], PCATCH, "i0", 0)) {
613 isp->isp_osinfo.tmflags[bus] |= TM_BUSY;
619 isp_cv_wait_timed_rqe(struct ispsoftc *isp, int bus, int timo)
621 if (tsleep(&isp->isp_osinfo.rstatus[bus], 0, "qt1", timo)) {
628 isp_cv_signal_rqe(struct ispsoftc *isp, int bus, int status)
630 isp->isp_osinfo.rstatus[bus] = status;
631 wakeup(&isp->isp_osinfo.rstatus[bus]);
635 isp_vsema_rqe(struct ispsoftc *isp, int bus)
637 if (isp->isp_osinfo.tmflags[bus] & TM_WANTED) {
638 isp->isp_osinfo.tmflags[bus] &= ~TM_WANTED;
639 wakeup(&isp->isp_osinfo.tmflags[bus]);
641 isp->isp_osinfo.tmflags[bus] &= ~TM_BUSY;
644 static __inline atio_private_data_t *
645 isp_get_atpd(struct ispsoftc *isp, int tag)
647 atio_private_data_t *atp;
648 for (atp = isp->isp_osinfo.atpdp;
649 atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
657 create_lun_state(struct ispsoftc *isp, int bus,
658 struct cam_path *path, tstate_t **rslt)
663 tstate_t *tptr, *new;
665 lun = xpt_path_lun_id(path);
667 return (CAM_LUN_INVALID);
669 if (is_lun_enabled(isp, bus, lun)) {
670 return (CAM_LUN_ALRDY_ENA);
672 new = malloc(sizeof (tstate_t), M_DEVBUF, M_WAITOK | M_ZERO);
673 status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
674 xpt_path_target_id(path), xpt_path_lun_id(path));
675 if (status != CAM_REQ_CMP) {
681 SLIST_INIT(&new->atios);
682 SLIST_INIT(&new->inots);
685 hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
686 tptr = isp->isp_osinfo.lun_hash[hfx];
688 isp->isp_osinfo.lun_hash[hfx] = new;
695 return (CAM_REQ_CMP);
699 destroy_lun_state(struct ispsoftc *isp, tstate_t *tptr)
704 hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
708 pw = isp->isp_osinfo.lun_hash[hfx];
711 } else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
712 isp->isp_osinfo.lun_hash[hfx] = pw->next;
717 if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
728 free(tptr, M_DEVBUF);
732 * we enter with our locks held.
735 isp_en_lun(struct ispsoftc *isp, union ccb *ccb)
737 const char lfmt[] = "Lun now %sabled for target mode on channel %d";
738 struct ccb_en_lun *cel = &ccb->cel;
741 int bus, cmd, av, wildcard;
746 bus = XS_CHANNEL(ccb) & 0x1;
747 tgt = ccb->ccb_h.target_id;
748 lun = ccb->ccb_h.target_lun;
751 * Do some sanity checking first.
754 if ((lun != CAM_LUN_WILDCARD) &&
755 (lun < 0 || lun >= (lun_id_t) isp->isp_maxluns)) {
756 ccb->ccb_h.status = CAM_LUN_INVALID;
761 sdparam *sdp = isp->isp_param;
763 if (tgt != CAM_TARGET_WILDCARD &&
764 tgt != sdp->isp_initiator_id) {
765 ccb->ccb_h.status = CAM_TID_INVALID;
769 if (tgt != CAM_TARGET_WILDCARD &&
770 tgt != FCPARAM(isp)->isp_iid) {
771 ccb->ccb_h.status = CAM_TID_INVALID;
775 * This is as a good a place as any to check f/w capabilities.
777 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_TMODE) == 0) {
778 isp_prt(isp, ISP_LOGERR,
779 "firmware does not support target mode");
780 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
784 * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
785 * XXX: dorks with our already fragile enable/disable code.
787 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
788 isp_prt(isp, ISP_LOGERR,
789 "firmware not SCCLUN capable");
793 if (tgt == CAM_TARGET_WILDCARD) {
794 if (lun == CAM_LUN_WILDCARD) {
797 ccb->ccb_h.status = CAM_LUN_INVALID;
805 * Next check to see whether this is a target/lun wildcard action.
807 * If so, we know that we can accept commands for luns that haven't
808 * been enabled yet and send them upstream. Otherwise, we have to
809 * handle them locally (if we see them at all).
813 tptr = &isp->isp_osinfo.tsdflt[bus];
815 if (isp->isp_osinfo.tmflags[bus] &
816 TM_WILDCARD_ENABLED) {
817 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
821 xpt_create_path(&tptr->owner, NULL,
822 xpt_path_path_id(ccb->ccb_h.path),
823 xpt_path_target_id(ccb->ccb_h.path),
824 xpt_path_lun_id(ccb->ccb_h.path));
825 if (ccb->ccb_h.status != CAM_REQ_CMP) {
828 SLIST_INIT(&tptr->atios);
829 SLIST_INIT(&tptr->inots);
830 isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
832 if ((isp->isp_osinfo.tmflags[bus] &
833 TM_WILDCARD_ENABLED) == 0) {
834 ccb->ccb_h.status = CAM_REQ_CMP;
838 ccb->ccb_h.status = CAM_SCSI_BUSY;
841 xpt_free_path(tptr->owner);
842 isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
847 * Now check to see whether this bus needs to be
848 * enabled/disabled with respect to target mode.
851 if (cel->enable && !(isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED)) {
852 av |= ENABLE_TARGET_FLAG;
853 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
855 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
857 isp->isp_osinfo.tmflags[bus] &=
858 ~TM_WILDCARD_ENABLED;
859 xpt_free_path(tptr->owner);
863 isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
864 isp_prt(isp, ISP_LOGINFO,
865 "Target Mode enabled on channel %d", bus);
866 } else if (cel->enable == 0 &&
867 (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) && wildcard) {
868 if (are_any_luns_enabled(isp, bus)) {
869 ccb->ccb_h.status = CAM_SCSI_BUSY;
872 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
874 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
877 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
878 isp_prt(isp, ISP_LOGINFO,
879 "Target Mode disabled on channel %d", bus);
883 ccb->ccb_h.status = CAM_REQ_CMP;
889 create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
890 if (ccb->ccb_h.status != CAM_REQ_CMP) {
894 tptr = get_lun_statep(isp, bus, lun);
896 ccb->ccb_h.status = CAM_LUN_INVALID;
901 if (isp_psema_sig_rqe(isp, bus)) {
902 rls_lun_statep(isp, tptr);
904 destroy_lun_state(isp, tptr);
905 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
910 u_int32_t seq = isp->isp_osinfo.rollinfo++;
911 int c, n, ulun = lun;
913 cmd = RQSTYPE_ENABLE_LUN;
916 if (IS_FC(isp) && lun != 0) {
917 cmd = RQSTYPE_MODIFY_LUN;
920 * For SCC firmware, we only deal with setting
921 * (enabling or modifying) lun 0.
926 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
927 xpt_print_path(ccb->ccb_h.path);
928 isp_prt(isp, ISP_LOGWARN, "isp_lun_cmd failed");
931 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
932 xpt_print_path(ccb->ccb_h.path);
933 isp_prt(isp, ISP_LOGERR,
934 "wait for ENABLE/MODIFY LUN timed out");
937 rstat = isp->isp_osinfo.rstatus[bus];
938 if (rstat != LUN_OK) {
939 xpt_print_path(ccb->ccb_h.path);
940 isp_prt(isp, ISP_LOGERR,
941 "ENABLE/MODIFY LUN returned 0x%x", rstat);
945 int c, n, ulun = lun;
949 seq = isp->isp_osinfo.rollinfo++;
950 cmd = -RQSTYPE_MODIFY_LUN;
954 if (IS_FC(isp) && lun != 0) {
957 * For SCC firmware, we only deal with setting
958 * (enabling or modifying) lun 0.
962 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
963 xpt_print_path(ccb->ccb_h.path);
964 isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
967 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
968 xpt_print_path(ccb->ccb_h.path);
969 isp_prt(isp, ISP_LOGERR,
970 "wait for MODIFY LUN timed out");
973 rstat = isp->isp_osinfo.rstatus[bus];
974 if (rstat != LUN_OK) {
975 xpt_print_path(ccb->ccb_h.path);
976 isp_prt(isp, ISP_LOGERR,
977 "MODIFY LUN returned 0x%x", rstat);
980 if (IS_FC(isp) && lun) {
984 seq = isp->isp_osinfo.rollinfo++;
987 cmd = -RQSTYPE_ENABLE_LUN;
988 if (isp_lun_cmd(isp, cmd, bus, tgt, lun, 0, 0, seq)) {
989 xpt_print_path(ccb->ccb_h.path);
990 isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
993 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
994 xpt_print_path(ccb->ccb_h.path);
995 isp_prt(isp, ISP_LOGERR,
996 "wait for DISABLE LUN timed out");
999 rstat = isp->isp_osinfo.rstatus[bus];
1000 if (rstat != LUN_OK) {
1001 xpt_print_path(ccb->ccb_h.path);
1002 isp_prt(isp, ISP_LOGWARN,
1003 "DISABLE LUN returned 0x%x", rstat);
1006 if (are_any_luns_enabled(isp, bus) == 0) {
1007 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1009 isp_prt(isp, ISP_LOGWARN,
1010 "disable target mode on channel %d failed",
1014 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1015 xpt_print_path(ccb->ccb_h.path);
1016 isp_prt(isp, ISP_LOGINFO,
1017 "Target Mode disabled on channel %d", bus);
1022 isp_vsema_rqe(isp, bus);
1024 if (rstat != LUN_OK) {
1025 xpt_print_path(ccb->ccb_h.path);
1026 isp_prt(isp, ISP_LOGWARN,
1027 "lun %sable failed", (cel->enable) ? "en" : "dis");
1028 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1029 rls_lun_statep(isp, tptr);
1031 destroy_lun_state(isp, tptr);
1033 xpt_print_path(ccb->ccb_h.path);
1034 isp_prt(isp, ISP_LOGINFO, lfmt,
1035 (cel->enable) ? "en" : "dis", bus);
1036 rls_lun_statep(isp, tptr);
1037 if (cel->enable == 0) {
1038 destroy_lun_state(isp, tptr);
1040 ccb->ccb_h.status = CAM_REQ_CMP;
1045 isp_abort_tgt_ccb(struct ispsoftc *isp, union ccb *ccb)
1048 struct ccb_hdr_slist *lp;
1049 struct ccb_hdr *curelm;
1051 union ccb *accb = ccb->cab.abort_ccb;
1053 if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1054 if (IS_FC(isp) && (accb->ccb_h.target_id !=
1055 ((fcparam *) isp->isp_param)->isp_loopid)) {
1056 return (CAM_PATH_INVALID);
1057 } else if (IS_SCSI(isp) && (accb->ccb_h.target_id !=
1058 ((sdparam *) isp->isp_param)->isp_initiator_id)) {
1059 return (CAM_PATH_INVALID);
1062 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
1064 return (CAM_PATH_INVALID);
1066 if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1068 } else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
1071 rls_lun_statep(isp, tptr);
1072 return (CAM_UA_ABORT);
1074 curelm = SLIST_FIRST(lp);
1076 if (curelm == &accb->ccb_h) {
1078 SLIST_REMOVE_HEAD(lp, sim_links.sle);
1080 while(curelm != NULL) {
1081 struct ccb_hdr *nextelm;
1083 nextelm = SLIST_NEXT(curelm, sim_links.sle);
1084 if (nextelm == &accb->ccb_h) {
1086 SLIST_NEXT(curelm, sim_links.sle) =
1087 SLIST_NEXT(nextelm, sim_links.sle);
1093 rls_lun_statep(isp, tptr);
1095 accb->ccb_h.status = CAM_REQ_ABORTED;
1096 return (CAM_REQ_CMP);
1098 return(CAM_PATH_INVALID);
1102 isp_target_start_ctio(struct ispsoftc *isp, union ccb *ccb)
1105 struct ccb_scsiio *cso = &ccb->csio;
1106 u_int16_t *hp, save_handle;
1107 u_int16_t nxti, optr;
1108 u_int8_t local[QENTRY_LEN];
1111 if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1112 xpt_print_path(ccb->ccb_h.path);
1113 printf("Request Queue Overflow in isp_target_start_ctio\n");
1114 return (CAM_RESRC_UNAVAIL);
1116 bzero(local, QENTRY_LEN);
1119 * We're either moving data or completing a command here.
1123 atio_private_data_t *atp;
1124 ct2_entry_t *cto = (ct2_entry_t *) local;
1126 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1127 cto->ct_header.rqs_entry_count = 1;
1128 cto->ct_iid = cso->init_id;
1129 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
1130 cto->ct_lun = ccb->ccb_h.target_lun;
1133 atp = isp_get_atpd(isp, cso->tag_id);
1135 isp_prt(isp, ISP_LOGERR,
1136 "cannot find private data adjunct for tag %x",
1141 cto->ct_rxid = cso->tag_id;
1142 if (cso->dxfer_len == 0) {
1143 cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
1144 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1145 cto->ct_flags |= CT2_SENDSTATUS;
1146 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1148 atp->orig_datalen - atp->bytes_xfered;
1149 if (cto->ct_resid < 0) {
1150 cto->rsp.m1.ct_scsi_status |=
1152 } else if (cto->ct_resid > 0) {
1153 cto->rsp.m1.ct_scsi_status |=
1157 if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1158 int m = min(cso->sense_len, MAXRESPLEN);
1159 bcopy(&cso->sense_data, cto->rsp.m1.ct_resp, m);
1160 cto->rsp.m1.ct_senselen = m;
1161 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1164 cto->ct_flags |= CT2_FLAG_MODE0;
1165 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1166 cto->ct_flags |= CT2_DATA_IN;
1168 cto->ct_flags |= CT2_DATA_OUT;
1170 cto->ct_reloff = atp->bytes_xfered;
1171 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1172 cto->ct_flags |= CT2_SENDSTATUS;
1173 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1176 (atp->bytes_xfered + cso->dxfer_len);
1177 if (cto->ct_resid < 0) {
1178 cto->rsp.m0.ct_scsi_status |=
1180 } else if (cto->ct_resid > 0) {
1181 cto->rsp.m0.ct_scsi_status |=
1185 atp->last_xframt = cso->dxfer_len;
1188 * If we're sending data and status back together,
1189 * we can't also send back sense data as well.
1191 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1194 if (cto->ct_flags & CT2_SENDSTATUS) {
1195 isp_prt(isp, ISP_LOGTDEBUG0,
1196 "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1197 cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1198 cso->dxfer_len, cto->ct_resid);
1199 cto->ct_flags |= CT2_CCINCR;
1200 atp->state = ATPD_STATE_LAST_CTIO;
1202 atp->state = ATPD_STATE_CTIO;
1203 cto->ct_timeout = 10;
1204 hp = &cto->ct_syshandle;
1206 ct_entry_t *cto = (ct_entry_t *) local;
1208 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1209 cto->ct_header.rqs_entry_count = 1;
1210 cto->ct_iid = cso->init_id;
1211 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1212 cto->ct_tgt = ccb->ccb_h.target_id;
1213 cto->ct_lun = ccb->ccb_h.target_lun;
1214 cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1215 if (AT_HAS_TAG(cso->tag_id)) {
1216 cto->ct_tag_val = (u_int8_t) AT_GET_TAG(cso->tag_id);
1217 cto->ct_flags |= CT_TQAE;
1219 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1220 cto->ct_flags |= CT_NODISC;
1222 if (cso->dxfer_len == 0) {
1223 cto->ct_flags |= CT_NO_DATA;
1224 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1225 cto->ct_flags |= CT_DATA_IN;
1227 cto->ct_flags |= CT_DATA_OUT;
1229 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1230 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1231 cto->ct_scsi_status = cso->scsi_status;
1232 cto->ct_resid = cso->resid;
1233 isp_prt(isp, ISP_LOGTDEBUG0,
1234 "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1235 cto->ct_fwhandle, cso->scsi_status, cso->resid,
1238 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1239 cto->ct_timeout = 10;
1240 hp = &cto->ct_syshandle;
1243 if (isp_save_xs(isp, (XS_T *)ccb, hp)) {
1244 xpt_print_path(ccb->ccb_h.path);
1245 printf("No XFLIST pointers for isp_target_start_ctio\n");
1246 return (CAM_RESRC_UNAVAIL);
1251 * Call the dma setup routines for this entry (and any subsequent
1252 * CTIOs) if there's data to move, and then tell the f/w it's got
1253 * new things to play with. As with isp_start's usage of DMA setup,
1254 * any swizzling is done in the machine dependent layer. Because
1255 * of this, we put the request onto the queue area first in native
1261 switch (ISP_DMASETUP(isp, cso, (ispreq_t *) local, &nxti, optr)) {
1263 ISP_ADD_REQUEST(isp, nxti);
1264 return (CAM_REQ_INPROG);
1267 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1268 isp_destroy_handle(isp, save_handle);
1269 return (CAM_RESRC_UNAVAIL);
1272 isp_destroy_handle(isp, save_handle);
1273 return (XS_ERR(ccb));
1278 isp_refire_putback_atio(void *arg)
1281 isp_target_putback_atio(arg);
1286 isp_target_putback_atio(union ccb *ccb)
1288 struct ispsoftc *isp;
1289 struct ccb_scsiio *cso;
1290 u_int16_t nxti, optr;
1295 if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1296 (void) timeout(isp_refire_putback_atio, ccb, 10);
1297 isp_prt(isp, ISP_LOGWARN,
1298 "isp_target_putback_atio: Request Queue Overflow");
1301 bzero(qe, QENTRY_LEN);
1304 at2_entry_t local, *at = &local;
1305 MEMZERO(at, sizeof (at2_entry_t));
1306 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1307 at->at_header.rqs_entry_count = 1;
1308 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1309 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1311 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1313 at->at_status = CT_OK;
1314 at->at_rxid = cso->tag_id;
1315 at->at_iid = cso->ccb_h.target_id;
1316 isp_put_atio2(isp, at, qe);
1318 at_entry_t local, *at = &local;
1319 MEMZERO(at, sizeof (at_entry_t));
1320 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1321 at->at_header.rqs_entry_count = 1;
1322 at->at_iid = cso->init_id;
1323 at->at_iid |= XS_CHANNEL(ccb) << 7;
1324 at->at_tgt = cso->ccb_h.target_id;
1325 at->at_lun = cso->ccb_h.target_lun;
1326 at->at_status = CT_OK;
1327 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1328 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1329 isp_put_atio(isp, at, qe);
1331 ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1332 ISP_ADD_REQUEST(isp, nxti);
1333 isp_complete_ctio(ccb);
1337 isp_complete_ctio(union ccb *ccb)
1339 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1340 ccb->ccb_h.status |= CAM_REQ_CMP;
1342 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1347 * Handle ATIO stuff that the generic code can't.
1348 * This means handling CDBs.
1352 isp_handle_platform_atio(struct ispsoftc *isp, at_entry_t *aep)
1355 int status, bus, iswildcard;
1356 struct ccb_accept_tio *atiop;
1359 * The firmware status (except for the QLTM_SVALID bit)
1360 * indicates why this ATIO was sent to us.
1362 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1364 * If the DISCONNECTS DISABLED bit is set in the flags field,
1365 * we're still connected on the SCSI bus.
1367 status = aep->at_status;
1368 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1370 * Bus Phase Sequence error. We should have sense data
1371 * suggested by the f/w. I'm not sure quite yet what
1372 * to do about this for CAM.
1374 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1375 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1378 if ((status & ~QLTM_SVALID) != AT_CDB) {
1379 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1381 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1385 bus = GET_BUS_VAL(aep->at_iid);
1386 tptr = get_lun_statep(isp, bus, aep->at_lun);
1388 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1396 * Because we can't autofeed sense data back with
1397 * a command for parallel SCSI, we can't give back
1398 * a CHECK CONDITION. We'll give back a BUSY status
1399 * instead. This works out okay because the only
1400 * time we should, in fact, get this, is in the
1401 * case that somebody configured us without the
1402 * blackhole driver, so they get what they deserve.
1404 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1408 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1409 if (atiop == NULL) {
1411 * Because we can't autofeed sense data back with
1412 * a command for parallel SCSI, we can't give back
1413 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1414 * instead. This works out okay because the only time we
1415 * should, in fact, get this, is in the case that we've
1418 xpt_print_path(tptr->owner);
1419 isp_prt(isp, ISP_LOGWARN,
1420 "no ATIOS for lun %d from initiator %d on channel %d",
1421 aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1422 if (aep->at_flags & AT_TQAE)
1423 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1425 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1426 rls_lun_statep(isp, tptr);
1429 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1431 atiop->ccb_h.target_id = aep->at_tgt;
1432 atiop->ccb_h.target_lun = aep->at_lun;
1434 if (aep->at_flags & AT_NODISC) {
1435 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1437 atiop->ccb_h.flags = 0;
1440 if (status & QLTM_SVALID) {
1441 size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1442 atiop->sense_len = amt;
1443 MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1445 atiop->sense_len = 0;
1448 atiop->init_id = GET_IID_VAL(aep->at_iid);
1449 atiop->cdb_len = aep->at_cdblen;
1450 MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1451 atiop->ccb_h.status = CAM_CDB_RECVD;
1453 * Construct a tag 'id' based upon tag value (which may be 0..255)
1454 * and the handle (which we have to preserve).
1456 AT_MAKE_TAGID(atiop->tag_id, aep);
1457 if (aep->at_flags & AT_TQAE) {
1458 atiop->tag_action = aep->at_tag_type;
1459 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1461 xpt_done((union ccb*)atiop);
1462 isp_prt(isp, ISP_LOGTDEBUG0,
1463 "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1464 aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1465 GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1466 aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1467 "nondisc" : "disconnecting");
1468 rls_lun_statep(isp, tptr);
1473 isp_handle_platform_atio2(struct ispsoftc *isp, at2_entry_t *aep)
1477 struct ccb_accept_tio *atiop;
1478 atio_private_data_t *atp;
1481 * The firmware status (except for the QLTM_SVALID bit)
1482 * indicates why this ATIO was sent to us.
1484 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1486 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1487 isp_prt(isp, ISP_LOGWARN,
1488 "bogus atio (0x%x) leaked to platform", aep->at_status);
1489 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1493 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1494 lun = aep->at_scclun;
1498 tptr = get_lun_statep(isp, 0, lun);
1500 isp_prt(isp, ISP_LOGWARN, "no state pointer for lun %d", lun);
1501 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1506 * What we'd like to know is whether or not we have a listener
1507 * upstream that really hasn't configured yet. If we do, then
1508 * we can give a more sensible reply here. If not, then we can
1509 * reject this out of hand.
1511 * Choices for what to send were
1513 * Not Ready, Unit Not Self-Configured Yet
1516 * for the former and
1518 * Illegal Request, Logical Unit Not Supported
1523 * We used to decide whether there was at least one listener
1524 * based upon whether the black hole driver was configured.
1525 * However, recent config(8) changes have made this hard to do
1529 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1533 atp = isp_get_atpd(isp, 0);
1534 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1535 if (atiop == NULL || atp == NULL) {
1537 * Because we can't autofeed sense data back with
1538 * a command for parallel SCSI, we can't give back
1539 * a CHECK CONDITION. We'll give back a QUEUE FULL status
1540 * instead. This works out okay because the only time we
1541 * should, in fact, get this, is in the case that we've
1544 xpt_print_path(tptr->owner);
1545 isp_prt(isp, ISP_LOGWARN,
1546 "no %s for lun %d from initiator %d",
1547 (atp == NULL && atiop == NULL)? "ATIO2s *or* ATPS" :
1548 ((atp == NULL)? "ATPs" : "ATIO2s"), lun, aep->at_iid);
1549 rls_lun_statep(isp, tptr);
1550 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1553 atp->state = ATPD_STATE_ATIO;
1554 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1556 isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO2 lun %d, count now %d",
1557 lun, tptr->atio_count);
1559 if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1560 atiop->ccb_h.target_id =
1561 ((fcparam *)isp->isp_param)->isp_loopid;
1562 atiop->ccb_h.target_lun = lun;
1565 * We don't get 'suggested' sense data as we do with SCSI cards.
1567 atiop->sense_len = 0;
1569 atiop->init_id = aep->at_iid;
1570 atiop->cdb_len = ATIO2_CDBLEN;
1571 MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1572 atiop->ccb_h.status = CAM_CDB_RECVD;
1573 atiop->tag_id = aep->at_rxid;
1574 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1575 case ATIO2_TC_ATTR_SIMPLEQ:
1576 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1578 case ATIO2_TC_ATTR_HEADOFQ:
1579 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1581 case ATIO2_TC_ATTR_ORDERED:
1582 atiop->tag_action = MSG_ORDERED_Q_TAG;
1584 case ATIO2_TC_ATTR_ACAQ: /* ?? */
1585 case ATIO2_TC_ATTR_UNTAGGED:
1587 atiop->tag_action = 0;
1590 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
1592 atp->tag = atiop->tag_id;
1594 atp->orig_datalen = aep->at_datalen;
1595 atp->last_xframt = 0;
1596 atp->bytes_xfered = 0;
1597 atp->state = ATPD_STATE_CAM;
1598 xpt_done((union ccb*)atiop);
1600 isp_prt(isp, ISP_LOGTDEBUG0,
1601 "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1602 aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1603 lun, aep->at_taskflags, aep->at_datalen);
1604 rls_lun_statep(isp, tptr);
1609 isp_handle_platform_ctio(struct ispsoftc *isp, void *arg)
1612 int sentstatus, ok, notify_cam, resid = 0;
1616 * CTIO and CTIO2 are close enough....
1619 ccb = (union ccb *) isp_find_xs(isp, ((ct_entry_t *)arg)->ct_syshandle);
1620 KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1621 isp_destroy_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1624 ct2_entry_t *ct = arg;
1625 atio_private_data_t *atp = isp_get_atpd(isp, ct->ct_rxid);
1627 isp_prt(isp, ISP_LOGERR,
1628 "cannot find adjunct for %x after I/O",
1632 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1633 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1634 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1635 ccb->ccb_h.status |= CAM_SENT_SENSE;
1637 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1638 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1639 resid = ct->ct_resid;
1640 atp->bytes_xfered += (atp->last_xframt - resid);
1641 atp->last_xframt = 0;
1643 if (sentstatus || !ok) {
1646 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN,
1647 "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1648 ct->ct_rxid, ct->ct_status, ct->ct_flags,
1649 (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1650 resid, sentstatus? "FIN" : "MID");
1653 /* XXX: should really come after isp_complete_ctio */
1654 atp->state = ATPD_STATE_PDON;
1656 ct_entry_t *ct = arg;
1657 sentstatus = ct->ct_flags & CT_SENDSTATUS;
1658 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1660 * We *ought* to be able to get back to the original ATIO
1661 * here, but for some reason this gets lost. It's just as
1662 * well because it's squirrelled away as part of periph
1665 * We can live without it as long as we continue to use
1666 * the auto-replenish feature for CTIOs.
1668 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1669 if (ct->ct_status & QLTM_SVALID) {
1670 char *sp = (char *)ct;
1671 sp += CTIO_SENSE_OFFSET;
1672 ccb->csio.sense_len =
1673 min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1674 MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1675 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1677 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1678 resid = ct->ct_resid;
1680 isp_prt(isp, ISP_LOGTDEBUG0,
1681 "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1682 ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1683 ct->ct_status, ct->ct_flags, resid,
1684 sentstatus? "FIN" : "MID");
1685 tval = ct->ct_fwhandle;
1687 ccb->csio.resid += resid;
1690 * We're here either because intermediate data transfers are done
1691 * and/or the final status CTIO (which may have joined with a
1692 * Data Transfer) is done.
1694 * In any case, for this platform, the upper layers figure out
1695 * what to do next, so all we do here is collect status and
1696 * pass information along. Any DMA handles have already been
1699 if (notify_cam == 0) {
1700 isp_prt(isp, ISP_LOGTDEBUG0, " INTER CTIO[0x%x] done", tval);
1704 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1705 (sentstatus)? " FINAL " : "MIDTERM ", tval);
1708 isp_target_putback_atio(ccb);
1710 isp_complete_ctio(ccb);
1717 isp_handle_platform_notify_scsi(struct ispsoftc *isp, in_entry_t *inp)
1719 return (0); /* XXXX */
1723 isp_handle_platform_notify_fc(struct ispsoftc *isp, in_fcentry_t *inp)
1726 switch (inp->in_status) {
1727 case IN_PORT_LOGOUT:
1728 isp_prt(isp, ISP_LOGWARN, "port logout of iid %d",
1731 case IN_PORT_CHANGED:
1732 isp_prt(isp, ISP_LOGWARN, "port changed for iid %d",
1735 case IN_GLOBAL_LOGO:
1736 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
1740 atio_private_data_t *atp = isp_get_atpd(isp, inp->in_seqid);
1741 struct ccb_immed_notify *inot = NULL;
1744 tstate_t *tptr = get_lun_statep(isp, 0, atp->lun);
1746 inot = (struct ccb_immed_notify *)
1747 SLIST_FIRST(&tptr->inots);
1749 SLIST_REMOVE_HEAD(&tptr->inots,
1753 isp_prt(isp, ISP_LOGWARN,
1754 "abort task RX_ID %x IID %d state %d",
1755 inp->in_seqid, inp->in_iid, atp->state);
1757 isp_prt(isp, ISP_LOGWARN,
1758 "abort task RX_ID %x from iid %d, state unknown",
1759 inp->in_seqid, inp->in_iid);
1762 inot->initiator_id = inp->in_iid;
1763 inot->sense_len = 0;
1764 inot->message_args[0] = MSG_ABORT_TAG;
1765 inot->message_args[1] = inp->in_seqid & 0xff;
1766 inot->message_args[2] = (inp->in_seqid >> 8) & 0xff;
1767 inot->ccb_h.status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
1768 xpt_done((union ccb *)inot);
1780 isp_cam_async(void *cbarg, u_int32_t code, struct cam_path *path, void *arg)
1782 struct cam_sim *sim;
1783 struct ispsoftc *isp;
1785 sim = (struct cam_sim *)cbarg;
1786 isp = (struct ispsoftc *) cam_sim_softc(sim);
1788 case AC_LOST_DEVICE:
1790 u_int16_t oflags, nflags;
1791 sdparam *sdp = isp->isp_param;
1794 tgt = xpt_path_target_id(path);
1796 sdp += cam_sim_bus(sim);
1798 nflags = sdp->isp_devparam[tgt].nvrm_flags;
1799 #ifndef ISP_TARGET_MODE
1800 nflags &= DPARM_SAFE_DFLT;
1801 if (isp->isp_loaded_fw) {
1802 nflags |= DPARM_NARROW | DPARM_ASYNC;
1805 nflags = DPARM_DEFAULT;
1807 oflags = sdp->isp_devparam[tgt].goal_flags;
1808 sdp->isp_devparam[tgt].goal_flags = nflags;
1809 sdp->isp_devparam[tgt].dev_update = 1;
1810 isp->isp_update |= (1 << cam_sim_bus(sim));
1811 (void) isp_control(isp,
1812 ISPCTL_UPDATE_PARAMS, NULL);
1813 sdp->isp_devparam[tgt].goal_flags = oflags;
1819 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
1825 isp_poll(struct cam_sim *sim)
1827 struct ispsoftc *isp = cam_sim_softc(sim);
1828 u_int16_t isr, sema, mbox;
1831 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1832 isp_intr(isp, isr, sema, mbox);
1839 isp_watchdog(void *arg)
1842 struct ispsoftc *isp = XS_ISP(xs);
1847 * We've decided this command is dead. Make sure we're not trying
1848 * to kill a command that's already dead by getting it's handle and
1849 * and seeing whether it's still alive.
1852 iok = isp->isp_osinfo.intsok;
1853 isp->isp_osinfo.intsok = 0;
1854 handle = isp_find_handle(isp, xs);
1856 u_int16_t isr, sema, mbox;
1858 if (XS_CMD_DONE_P(xs)) {
1859 isp_prt(isp, ISP_LOGDEBUG1,
1860 "watchdog found done cmd (handle 0x%x)", handle);
1865 if (XS_CMD_WDOG_P(xs)) {
1866 isp_prt(isp, ISP_LOGDEBUG2,
1867 "recursive watchdog (handle 0x%x)", handle);
1873 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1874 isp_intr(isp, isr, sema, mbox);
1876 if (XS_CMD_DONE_P(xs)) {
1877 isp_prt(isp, ISP_LOGDEBUG2,
1878 "watchdog cleanup for handle 0x%x", handle);
1879 xpt_done((union ccb *) xs);
1880 } else if (XS_CMD_GRACE_P(xs)) {
1882 * Make sure the command is *really* dead before we
1883 * release the handle (and DMA resources) for reuse.
1885 (void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
1888 * After this point, the comamnd is really dead.
1890 if (XS_XFRLEN(xs)) {
1891 ISP_DMAFREE(isp, xs, handle);
1893 isp_destroy_handle(isp, handle);
1894 xpt_print_path(xs->ccb_h.path);
1895 isp_prt(isp, ISP_LOGWARN,
1896 "watchdog timeout for handle 0x%x", handle);
1897 XS_SETERR(xs, CAM_CMD_TIMEOUT);
1901 u_int16_t nxti, optr;
1902 ispreq_t local, *mp= &local, *qe;
1905 callout_reset(&xs->ccb_h.timeout_ch, hz,
1907 if (isp_getrqentry(isp, &nxti, &optr, (void **) &qe)) {
1912 MEMZERO((void *) mp, sizeof (*mp));
1913 mp->req_header.rqs_entry_count = 1;
1914 mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
1915 mp->req_modifier = SYNC_ALL;
1916 mp->req_target = XS_CHANNEL(xs) << 7;
1917 isp_put_request(isp, mp, qe);
1918 ISP_ADD_REQUEST(isp, nxti);
1921 isp_prt(isp, ISP_LOGDEBUG2, "watchdog with no command");
1923 isp->isp_osinfo.intsok = iok;
1928 isp_kthread(void *arg)
1930 struct ispsoftc *isp = arg;
1934 isp->isp_osinfo.intsok = 1;
1937 * The first loop is for our usage where we have yet to have
1938 * gotten good fibre channel state.
1943 isp_prt(isp, ISP_LOGDEBUG0, "kthread: checking FC state");
1944 while (isp_fc_runstate(isp, 2 * 1000000) != 0) {
1945 isp_prt(isp, ISP_LOGDEBUG0, "kthread: FC state ungood");
1946 if (FCPARAM(isp)->isp_fwstate != FW_READY ||
1947 FCPARAM(isp)->isp_loopstate < LOOP_PDB_RCVD) {
1948 if (FCPARAM(isp)->loop_seen_once == 0 ||
1949 isp->isp_osinfo.ktmature == 0) {
1953 tsleep(isp_kthread, 0, "isp_fcthrd", hz);
1958 * Even if we didn't get good loop state we may be
1959 * unfreezing the SIMQ so that we can kill off
1960 * commands (if we've never seen loop before, for example).
1962 isp->isp_osinfo.ktmature = 1;
1963 wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
1964 isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
1965 if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
1966 isp_prt(isp, ISP_LOGDEBUG0, "kthread: releasing simq");
1967 ISPLOCK_2_CAMLOCK(isp);
1968 xpt_release_simq(isp->isp_sim, 1);
1969 CAMLOCK_2_ISPLOCK(isp);
1971 tsleep(&isp->isp_osinfo.kthread, 0, "isp_fc_worker", 0);
1972 isp_prt(isp, ISP_LOGDEBUG0, "kthread: waiting until called");
1977 isp_action(struct cam_sim *sim, union ccb *ccb)
1979 int bus, tgt, error;
1980 struct ispsoftc *isp;
1981 struct ccb_trans_settings *cts;
1983 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
1985 isp = (struct ispsoftc *)cam_sim_softc(sim);
1986 ccb->ccb_h.sim_priv.entries[0].field = 0;
1987 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
1988 if (isp->isp_state != ISP_RUNSTATE &&
1989 ccb->ccb_h.func_code == XPT_SCSI_IO) {
1990 CAMLOCK_2_ISPLOCK(isp);
1992 if (isp->isp_state != ISP_INITSTATE) {
1995 * Lie. Say it was a selection timeout.
1997 ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
1998 xpt_freeze_devq(ccb->ccb_h.path, 1);
2002 isp->isp_state = ISP_RUNSTATE;
2003 ISPLOCK_2_CAMLOCK(isp);
2005 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2008 switch (ccb->ccb_h.func_code) {
2009 case XPT_SCSI_IO: /* Execute the requested I/O operation */
2011 * Do a couple of preliminary checks...
2013 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2014 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2015 ccb->ccb_h.status = CAM_REQ_INVALID;
2021 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
2022 ccb->ccb_h.status = CAM_PATH_INVALID;
2023 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
2024 ccb->ccb_h.status = CAM_PATH_INVALID;
2026 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2027 isp_prt(isp, ISP_LOGERR,
2028 "invalid tgt/lun (%d.%d) in XPT_SCSI_IO",
2029 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2034 ((struct ccb_scsiio *) ccb)->scsi_status = SCSI_STATUS_OK;
2035 CAMLOCK_2_ISPLOCK(isp);
2036 error = isp_start((XS_T *) ccb);
2039 ccb->ccb_h.status |= CAM_SIM_QUEUED;
2040 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2041 u_int64_t ticks = (u_int64_t) hz;
2042 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
2043 ticks = 60 * 1000 * ticks;
2045 ticks = ccb->ccb_h.timeout * hz;
2046 ticks = ((ticks + 999) / 1000) + hz + hz;
2047 if (ticks >= 0x80000000) {
2048 isp_prt(isp, ISP_LOGERR,
2049 "timeout overflow");
2052 callout_reset(&ccb->ccb_h.timeout_ch, ticks,
2055 ISPLOCK_2_CAMLOCK(isp);
2059 * This can only happen for Fibre Channel
2061 KASSERT((IS_FC(isp)), ("CMD_RQLATER for FC only"));
2062 if (FCPARAM(isp)->loop_seen_once == 0 &&
2063 isp->isp_osinfo.ktmature) {
2064 ISPLOCK_2_CAMLOCK(isp);
2065 XS_SETERR(ccb, CAM_SEL_TIMEOUT);
2069 wakeup(&isp->isp_osinfo.kthread);
2070 isp_freeze_loopdown(isp, "isp_action(RQLATER)");
2071 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
2072 XS_SETERR(ccb, CAM_REQUEUE_REQ);
2073 ISPLOCK_2_CAMLOCK(isp);
2077 XS_SETERR(ccb, CAM_REQUEUE_REQ);
2078 ISPLOCK_2_CAMLOCK(isp);
2082 isp_done((struct ccb_scsiio *) ccb);
2083 ISPLOCK_2_CAMLOCK(isp);
2086 isp_prt(isp, ISP_LOGERR,
2087 "What's this? 0x%x at %d in file %s",
2088 error, __LINE__, __FILE__);
2089 XS_SETERR(ccb, CAM_REQ_CMP_ERR);
2091 ISPLOCK_2_CAMLOCK(isp);
2095 #ifdef ISP_TARGET_MODE
2096 case XPT_EN_LUN: /* Enable LUN as a target */
2099 CAMLOCK_2_ISPLOCK(isp);
2100 iok = isp->isp_osinfo.intsok;
2101 isp->isp_osinfo.intsok = 0;
2102 isp_en_lun(isp, ccb);
2103 isp->isp_osinfo.intsok = iok;
2104 ISPLOCK_2_CAMLOCK(isp);
2108 case XPT_NOTIFY_ACK: /* recycle notify ack */
2109 case XPT_IMMED_NOTIFY: /* Add Immediate Notify Resource */
2110 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
2113 get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2115 ccb->ccb_h.status = CAM_LUN_INVALID;
2119 ccb->ccb_h.sim_priv.entries[0].field = 0;
2120 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2121 ccb->ccb_h.flags = 0;
2123 CAMLOCK_2_ISPLOCK(isp);
2124 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
2126 * Note that the command itself may not be done-
2127 * it may not even have had the first CTIO sent.
2130 isp_prt(isp, ISP_LOGTDEBUG0,
2131 "Put FREE ATIO2, lun %d, count now %d",
2132 ccb->ccb_h.target_lun, tptr->atio_count);
2133 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h,
2135 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
2136 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
2141 rls_lun_statep(isp, tptr);
2142 ccb->ccb_h.status = CAM_REQ_INPROG;
2143 ISPLOCK_2_CAMLOCK(isp);
2146 case XPT_CONT_TARGET_IO:
2148 CAMLOCK_2_ISPLOCK(isp);
2149 ccb->ccb_h.status = isp_target_start_ctio(isp, ccb);
2150 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
2151 isp_prt(isp, ISP_LOGWARN,
2152 "XPT_CONT_TARGET_IO: status 0x%x",
2154 XS_SETERR(ccb, CAM_REQUEUE_REQ);
2155 ISPLOCK_2_CAMLOCK(isp);
2158 ISPLOCK_2_CAMLOCK(isp);
2159 ccb->ccb_h.status |= CAM_SIM_QUEUED;
2164 case XPT_RESET_DEV: /* BDR the specified SCSI device */
2166 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
2167 tgt = ccb->ccb_h.target_id;
2170 CAMLOCK_2_ISPLOCK(isp);
2171 error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
2172 ISPLOCK_2_CAMLOCK(isp);
2174 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2176 ccb->ccb_h.status = CAM_REQ_CMP;
2180 case XPT_ABORT: /* Abort the specified CCB */
2182 union ccb *accb = ccb->cab.abort_ccb;
2183 CAMLOCK_2_ISPLOCK(isp);
2184 switch (accb->ccb_h.func_code) {
2185 #ifdef ISP_TARGET_MODE
2186 case XPT_ACCEPT_TARGET_IO:
2187 case XPT_IMMED_NOTIFY:
2188 ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
2190 case XPT_CONT_TARGET_IO:
2191 isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
2192 ccb->ccb_h.status = CAM_UA_ABORT;
2196 error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
2198 ccb->ccb_h.status = CAM_UA_ABORT;
2200 ccb->ccb_h.status = CAM_REQ_CMP;
2204 ccb->ccb_h.status = CAM_REQ_INVALID;
2207 ISPLOCK_2_CAMLOCK(isp);
2211 #define IS_CURRENT_SETTINGS(c) (c->flags & CCB_TRANS_CURRENT_SETTINGS)
2212 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
2214 if (!IS_CURRENT_SETTINGS(cts)) {
2215 ccb->ccb_h.status = CAM_REQ_INVALID;
2219 tgt = cts->ccb_h.target_id;
2220 CAMLOCK_2_ISPLOCK(isp);
2222 sdparam *sdp = isp->isp_param;
2225 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2229 * We always update (internally) from goal_flags
2230 * so any request to change settings just gets
2231 * vectored to that location.
2233 dptr = &sdp->isp_devparam[tgt].goal_flags;
2236 * Note that these operations affect the
2237 * the goal flags (goal_flags)- not
2238 * the current state flags. Then we mark
2239 * things so that the next operation to
2240 * this HBA will cause the update to occur.
2242 if (cts->valid & CCB_TRANS_DISC_VALID) {
2243 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
2244 *dptr |= DPARM_DISC;
2246 *dptr &= ~DPARM_DISC;
2249 if (cts->valid & CCB_TRANS_TQ_VALID) {
2250 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
2251 *dptr |= DPARM_TQING;
2253 *dptr &= ~DPARM_TQING;
2256 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) {
2257 switch (cts->bus_width) {
2258 case MSG_EXT_WDTR_BUS_16_BIT:
2259 *dptr |= DPARM_WIDE;
2262 *dptr &= ~DPARM_WIDE;
2266 * Any SYNC RATE of nonzero and SYNC_OFFSET
2267 * of nonzero will cause us to go to the
2268 * selected (from NVRAM) maximum value for
2269 * this device. At a later point, we'll
2270 * allow finer control.
2272 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
2273 (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) &&
2274 (cts->sync_offset > 0)) {
2275 *dptr |= DPARM_SYNC;
2277 *dptr &= ~DPARM_SYNC;
2279 *dptr |= DPARM_SAFE_DFLT;
2280 isp_prt(isp, ISP_LOGDEBUG0,
2281 "SET bus %d targ %d to flags %x off %x per %x",
2282 bus, tgt, sdp->isp_devparam[tgt].goal_flags,
2283 sdp->isp_devparam[tgt].goal_offset,
2284 sdp->isp_devparam[tgt].goal_period);
2285 sdp->isp_devparam[tgt].dev_update = 1;
2286 isp->isp_update |= (1 << bus);
2288 ISPLOCK_2_CAMLOCK(isp);
2289 ccb->ccb_h.status = CAM_REQ_CMP;
2292 case XPT_GET_TRAN_SETTINGS:
2294 tgt = cts->ccb_h.target_id;
2295 CAMLOCK_2_ISPLOCK(isp);
2298 * a lot of normal SCSI things don't make sense.
2300 cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
2301 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2303 * How do you measure the width of a high
2304 * speed serial bus? Well, in bytes.
2306 * Offset and period make no sense, though, so we set
2307 * (above) a 'base' transfer speed to be gigabit.
2309 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2311 sdparam *sdp = isp->isp_param;
2312 int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2313 u_int16_t dval, pval, oval;
2317 if (IS_CURRENT_SETTINGS(cts)) {
2318 sdp->isp_devparam[tgt].dev_refresh = 1;
2319 isp->isp_update |= (1 << bus);
2320 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2322 dval = sdp->isp_devparam[tgt].actv_flags;
2323 oval = sdp->isp_devparam[tgt].actv_offset;
2324 pval = sdp->isp_devparam[tgt].actv_period;
2326 dval = sdp->isp_devparam[tgt].nvrm_flags;
2327 oval = sdp->isp_devparam[tgt].nvrm_offset;
2328 pval = sdp->isp_devparam[tgt].nvrm_period;
2331 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
2333 if (dval & DPARM_DISC) {
2334 cts->flags |= CCB_TRANS_DISC_ENB;
2336 if (dval & DPARM_TQING) {
2337 cts->flags |= CCB_TRANS_TAG_ENB;
2339 if (dval & DPARM_WIDE) {
2340 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2342 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2344 cts->valid = CCB_TRANS_BUS_WIDTH_VALID |
2345 CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2347 if ((dval & DPARM_SYNC) && oval != 0) {
2348 cts->sync_period = pval;
2349 cts->sync_offset = oval;
2351 CCB_TRANS_SYNC_RATE_VALID |
2352 CCB_TRANS_SYNC_OFFSET_VALID;
2354 isp_prt(isp, ISP_LOGDEBUG0,
2355 "GET %s bus %d targ %d to flags %x off %x per %x",
2356 IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2357 bus, tgt, dval, oval, pval);
2359 ISPLOCK_2_CAMLOCK(isp);
2360 ccb->ccb_h.status = CAM_REQ_CMP;
2364 case XPT_CALC_GEOMETRY:
2366 struct ccb_calc_geometry *ccg;
2367 u_int32_t secs_per_cylinder;
2371 if (ccg->block_size == 0) {
2372 isp_prt(isp, ISP_LOGERR,
2373 "%d.%d XPT_CALC_GEOMETRY block size 0?",
2374 ccg->ccb_h.target_id, ccg->ccb_h.target_lun);
2375 ccb->ccb_h.status = CAM_REQ_INVALID;
2379 size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2380 if (size_mb > 1024) {
2382 ccg->secs_per_track = 63;
2385 ccg->secs_per_track = 32;
2387 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2388 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2389 ccb->ccb_h.status = CAM_REQ_CMP;
2393 case XPT_RESET_BUS: /* Reset the specified bus */
2394 bus = cam_sim_bus(sim);
2395 CAMLOCK_2_ISPLOCK(isp);
2396 error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2397 ISPLOCK_2_CAMLOCK(isp);
2399 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2401 if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2402 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2403 else if (isp->isp_path != NULL)
2404 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2405 ccb->ccb_h.status = CAM_REQ_CMP;
2410 case XPT_TERM_IO: /* Terminate the I/O process */
2411 ccb->ccb_h.status = CAM_REQ_INVALID;
2415 case XPT_PATH_INQ: /* Path routing inquiry */
2417 struct ccb_pathinq *cpi = &ccb->cpi;
2419 cpi->version_num = 1;
2420 #ifdef ISP_TARGET_MODE
2421 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2423 cpi->target_sprt = 0;
2425 cpi->hba_eng_cnt = 0;
2426 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
2427 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
2428 cpi->bus_id = cam_sim_bus(sim);
2430 cpi->hba_misc = PIM_NOBUSRESET;
2432 * Because our loop ID can shift from time to time,
2433 * make our initiator ID out of range of our bus.
2435 cpi->initiator_id = cpi->max_target + 1;
2438 * Set base transfer capabilities for Fibre Channel.
2439 * Technically not correct because we don't know
2440 * what media we're running on top of- but we'll
2441 * look good if we always say 100MB/s.
2443 if (FCPARAM(isp)->isp_gbspeed == 2)
2444 cpi->base_transfer_speed = 200000;
2446 cpi->base_transfer_speed = 100000;
2447 cpi->hba_inquiry = PI_TAG_ABLE;
2449 sdparam *sdp = isp->isp_param;
2450 sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
2451 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
2453 cpi->initiator_id = sdp->isp_initiator_id;
2454 cpi->base_transfer_speed = 3300;
2456 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2457 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
2458 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2459 cpi->unit_number = cam_sim_unit(sim);
2460 cpi->ccb_h.status = CAM_REQ_CMP;
2465 ccb->ccb_h.status = CAM_REQ_INVALID;
2471 #define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
2473 isp_done(struct ccb_scsiio *sccb)
2475 struct ispsoftc *isp = XS_ISP(sccb);
2478 XS_SETERR(sccb, CAM_REQ_CMP);
2480 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
2481 (sccb->scsi_status != SCSI_STATUS_OK)) {
2482 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
2483 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) &&
2484 (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
2485 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
2487 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2491 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2492 if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2493 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2494 sccb->ccb_h.status |= CAM_DEV_QFRZN;
2495 xpt_freeze_devq(sccb->ccb_h.path, 1);
2496 isp_prt(isp, ISP_LOGDEBUG0,
2497 "freeze devq %d.%d cam sts %x scsi sts %x",
2498 sccb->ccb_h.target_id, sccb->ccb_h.target_lun,
2499 sccb->ccb_h.status, sccb->scsi_status);
2503 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
2504 (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2505 xpt_print_path(sccb->ccb_h.path);
2506 isp_prt(isp, ISP_LOGINFO,
2507 "cam completion status 0x%x", sccb->ccb_h.status);
2510 XS_CMD_S_DONE(sccb);
2511 if (XS_CMD_WDOG_P(sccb) == 0) {
2512 callout_stop(&sccb->ccb_h.timeout_ch);
2513 if (XS_CMD_GRACE_P(sccb)) {
2514 isp_prt(isp, ISP_LOGDEBUG2,
2515 "finished command on borrowed time");
2517 XS_CMD_S_CLEAR(sccb);
2518 ISPLOCK_2_CAMLOCK(isp);
2519 xpt_done((union ccb *) sccb);
2520 CAMLOCK_2_ISPLOCK(isp);
2525 isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
2529 case ISPASYNC_NEW_TGT_PARAMS:
2532 sdparam *sdp = isp->isp_param;
2533 struct ccb_trans_settings cts;
2534 struct cam_path *tmppath;
2536 bzero(&cts, sizeof (struct ccb_trans_settings));
2538 tgt = *((int *)arg);
2539 bus = (tgt >> 16) & 0xffff;
2542 ISPLOCK_2_CAMLOCK(isp);
2543 if (xpt_create_path(&tmppath, NULL,
2544 cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
2545 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2546 CAMLOCK_2_ISPLOCK(isp);
2547 isp_prt(isp, ISP_LOGWARN,
2548 "isp_async cannot make temp path for %d.%d",
2553 CAMLOCK_2_ISPLOCK(isp);
2554 flags = sdp->isp_devparam[tgt].actv_flags;
2555 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2556 cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2557 if (flags & DPARM_DISC) {
2558 cts.flags |= CCB_TRANS_DISC_ENB;
2560 if (flags & DPARM_TQING) {
2561 cts.flags |= CCB_TRANS_TAG_ENB;
2563 cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2564 cts.bus_width = (flags & DPARM_WIDE)?
2565 MSG_EXT_WDTR_BUS_8_BIT : MSG_EXT_WDTR_BUS_16_BIT;
2566 cts.sync_period = sdp->isp_devparam[tgt].actv_period;
2567 cts.sync_offset = sdp->isp_devparam[tgt].actv_offset;
2568 if (flags & DPARM_SYNC) {
2570 CCB_TRANS_SYNC_RATE_VALID |
2571 CCB_TRANS_SYNC_OFFSET_VALID;
2573 isp_prt(isp, ISP_LOGDEBUG2,
2574 "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
2575 bus, tgt, sdp->isp_devparam[tgt].actv_period,
2576 sdp->isp_devparam[tgt].actv_offset, flags);
2577 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
2578 ISPLOCK_2_CAMLOCK(isp);
2579 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
2580 xpt_free_path(tmppath);
2581 CAMLOCK_2_ISPLOCK(isp);
2584 case ISPASYNC_BUS_RESET:
2585 bus = *((int *)arg);
2586 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
2588 if (bus > 0 && isp->isp_path2) {
2589 ISPLOCK_2_CAMLOCK(isp);
2590 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2591 CAMLOCK_2_ISPLOCK(isp);
2592 } else if (isp->isp_path) {
2593 ISPLOCK_2_CAMLOCK(isp);
2594 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2595 CAMLOCK_2_ISPLOCK(isp);
2599 if (isp->isp_path) {
2600 isp_freeze_loopdown(isp, "ISPASYNC_LIP");
2602 isp_prt(isp, ISP_LOGINFO, "LIP Received");
2604 case ISPASYNC_LOOP_RESET:
2605 if (isp->isp_path) {
2606 isp_freeze_loopdown(isp, "ISPASYNC_LOOP_RESET");
2608 isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
2610 case ISPASYNC_LOOP_DOWN:
2611 if (isp->isp_path) {
2612 isp_freeze_loopdown(isp, "ISPASYNC_LOOP_DOWN");
2614 isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
2616 case ISPASYNC_LOOP_UP:
2618 * Now we just note that Loop has come up. We don't
2619 * actually do anything because we're waiting for a
2620 * Change Notify before activating the FC cleanup
2621 * thread to look at the state of the loop again.
2623 isp_prt(isp, ISP_LOGINFO, "Loop UP");
2625 case ISPASYNC_PROMENADE:
2627 const char *fmt = "Target %d (Loop 0x%x) Port ID 0x%x "
2628 "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
2629 static const char *roles[4] = {
2630 "(none)", "Target", "Initiator", "Target/Initiator"
2632 fcparam *fcp = isp->isp_param;
2633 int tgt = *((int *) arg);
2634 struct lportdb *lp = &fcp->portdb[tgt];
2636 isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
2637 roles[lp->roles & 0x3],
2638 (lp->valid)? "Arrived" : "Departed",
2639 (u_int32_t) (lp->port_wwn >> 32),
2640 (u_int32_t) (lp->port_wwn & 0xffffffffLL),
2641 (u_int32_t) (lp->node_wwn >> 32),
2642 (u_int32_t) (lp->node_wwn & 0xffffffffLL));
2646 case ISPASYNC_CHANGE_NOTIFY:
2647 if (arg == ISPASYNC_CHANGE_PDB) {
2648 isp_prt(isp, ISP_LOGINFO,
2649 "Port Database Changed");
2650 } else if (arg == ISPASYNC_CHANGE_SNS) {
2651 isp_prt(isp, ISP_LOGINFO,
2652 "Name Server Database Changed");
2654 wakeup(&isp->isp_osinfo.kthread);
2656 case ISPASYNC_FABRIC_DEV:
2658 int target, base, lim;
2659 fcparam *fcp = isp->isp_param;
2660 struct lportdb *lp = NULL;
2661 struct lportdb *clp = (struct lportdb *) arg;
2664 switch (clp->port_type) {
2691 isp_prt(isp, ISP_LOGINFO,
2692 "%s Fabric Device @ PortID 0x%x", pt, clp->portid);
2695 * If we don't have an initiator role we bail.
2697 * We just use ISPASYNC_FABRIC_DEV for announcement purposes.
2700 if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
2705 * Is this entry for us? If so, we bail.
2708 if (fcp->isp_portid == clp->portid) {
2713 * Else, the default policy is to find room for it in
2714 * our local port database. Later, when we execute
2715 * the call to isp_pdb_sync either this newly arrived
2716 * or already logged in device will be (re)announced.
2719 if (fcp->isp_topo == TOPO_FL_PORT)
2724 if (fcp->isp_topo == TOPO_N_PORT)
2730 * Is it already in our list?
2732 for (target = base; target < lim; target++) {
2733 if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2736 lp = &fcp->portdb[target];
2737 if (lp->port_wwn == clp->port_wwn &&
2738 lp->node_wwn == clp->node_wwn) {
2746 for (target = base; target < lim; target++) {
2747 if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2750 lp = &fcp->portdb[target];
2751 if (lp->port_wwn == 0) {
2755 if (target == lim) {
2756 isp_prt(isp, ISP_LOGWARN,
2757 "out of space for fabric devices");
2760 lp->port_type = clp->port_type;
2761 lp->fc4_type = clp->fc4_type;
2762 lp->node_wwn = clp->node_wwn;
2763 lp->port_wwn = clp->port_wwn;
2764 lp->portid = clp->portid;
2768 #ifdef ISP_TARGET_MODE
2769 case ISPASYNC_TARGET_MESSAGE:
2771 tmd_msg_t *mp = arg;
2772 isp_prt(isp, ISP_LOGALL,
2773 "bus %d iid %d tgt %d lun %d ttype %x tval %x msg[0]=%x",
2774 mp->nt_bus, (int) mp->nt_iid, (int) mp->nt_tgt,
2775 (int) mp->nt_lun, mp->nt_tagtype, mp->nt_tagval,
2779 case ISPASYNC_TARGET_EVENT:
2781 tmd_event_t *ep = arg;
2782 isp_prt(isp, ISP_LOGALL,
2783 "bus %d event code 0x%x", ep->ev_bus, ep->ev_event);
2786 case ISPASYNC_TARGET_ACTION:
2787 switch (((isphdr_t *)arg)->rqs_entry_type) {
2789 isp_prt(isp, ISP_LOGWARN,
2790 "event 0x%x for unhandled target action",
2791 ((isphdr_t *)arg)->rqs_entry_type);
2793 case RQSTYPE_NOTIFY:
2795 rv = isp_handle_platform_notify_scsi(isp,
2796 (in_entry_t *) arg);
2798 rv = isp_handle_platform_notify_fc(isp,
2799 (in_fcentry_t *) arg);
2803 rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
2806 rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
2810 rv = isp_handle_platform_ctio(isp, arg);
2812 case RQSTYPE_ENABLE_LUN:
2813 case RQSTYPE_MODIFY_LUN:
2814 if (IS_DUALBUS(isp)) {
2816 GET_BUS_VAL(((lun_entry_t *)arg)->le_rsvd);
2820 isp_cv_signal_rqe(isp, bus,
2821 ((lun_entry_t *)arg)->le_status);
2826 case ISPASYNC_FW_CRASH:
2828 u_int16_t mbox1, mbox6;
2829 mbox1 = ISP_READ(isp, OUTMAILBOX1);
2830 if (IS_DUALBUS(isp)) {
2831 mbox6 = ISP_READ(isp, OUTMAILBOX6);
2835 isp_prt(isp, ISP_LOGERR,
2836 "Internal Firmware Error on bus %d @ RISC Address 0x%x",
2838 #ifdef ISP_FW_CRASH_DUMP
2840 * XXX: really need a thread to do this right.
2843 FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
2844 FCPARAM(isp)->isp_loopstate = LOOP_NIL;
2845 isp_freeze_loopdown(isp, "f/w crash");
2849 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
2853 case ISPASYNC_UNHANDLED_RESPONSE:
2856 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
2864 * Locks are held before coming here.
2867 isp_uninit(struct ispsoftc *isp)
2869 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
2874 isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
2877 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
2880 printf("%s: ", device_get_nameunit(isp->isp_dev));
2881 __va_start(ap, fmt);