]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/isp/isp_freebsd.c
MFC r289930: Formalize/unify chip (re-)inits.
[FreeBSD/stable/10.git] / sys / dev / isp / isp_freebsd.c
1 /*-
2  * Copyright (c) 1997-2009 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <dev/isp/isp_freebsd.h>
34 #include <sys/unistd.h>
35 #include <sys/kthread.h>
36 #include <sys/conf.h>
37 #include <sys/module.h>
38 #include <sys/ioccom.h>
39 #include <dev/isp/isp_ioctl.h>
40 #include <sys/devicestat.h>
41 #include <cam/cam_periph.h>
42 #include <cam/cam_xpt_periph.h>
43
44 #if     __FreeBSD_version < 800002 
45 #define THREAD_CREATE   kthread_create
46 #else
47 #define THREAD_CREATE   kproc_create
48 #endif
49
50 MODULE_VERSION(isp, 1);
51 MODULE_DEPEND(isp, cam, 1, 1, 1);
52 int isp_announced = 0;
53 int isp_fabric_hysteresis = 5;
54 int isp_loop_down_limit = 60;   /* default loop down limit */
55 int isp_quickboot_time = 7;     /* don't wait more than N secs for loop up */
56 int isp_gone_device_time = 30;  /* grace time before reporting device lost */
57 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
58
59 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
60 static d_ioctl_t ispioctl;
61 static void isp_intr_enable(void *);
62 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
63 static void isp_poll(struct cam_sim *);
64 static timeout_t isp_watchdog;
65 static timeout_t isp_gdt;
66 static task_fn_t isp_gdt_task;
67 static timeout_t isp_ldt;
68 static task_fn_t isp_ldt_task;
69 static void isp_kthread(void *);
70 static void isp_action(struct cam_sim *, union ccb *);
71 static int isp_timer_count;
72 static void isp_timer(void *);
73
74 static struct cdevsw isp_cdevsw = {
75         .d_version =    D_VERSION,
76         .d_ioctl =      ispioctl,
77         .d_name =       "isp",
78 };
79
80 static int
81 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
82 {
83         ispsoftc_t *isp = (ispsoftc_t *)arg1;
84         int chan = arg2;
85         int error, old, value;
86
87         value = FCPARAM(isp, chan)->role;
88
89         error = sysctl_handle_int(oidp, &value, 0, req);
90         if ((error != 0) || (req->newptr == NULL))
91                 return (error);
92
93         if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
94                 return (EINVAL);
95
96         ISP_LOCK(isp);
97         old = FCPARAM(isp, chan)->role;
98
99         /* We don't allow target mode switch from here. */
100         value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
101
102         /* If nothing has changed -- we are done. */
103         if (value == old) {
104                 ISP_UNLOCK(isp);
105                 return (0);
106         }
107
108         /* Actually change the role. */
109         error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
110         ISP_UNLOCK(isp);
111         return (error);
112 }
113
114 static int
115 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
116 {
117         struct ccb_setasync csa;
118         struct cam_sim *sim;
119         struct cam_path *path;
120
121         /*
122          * Construct our SIM entry.
123          */
124         sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
125
126         if (sim == NULL) {
127                 return (ENOMEM);
128         }
129
130         ISP_LOCK(isp);
131         if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
132                 ISP_UNLOCK(isp);
133                 cam_sim_free(sim, FALSE);
134                 return (EIO);
135         }
136         ISP_UNLOCK(isp);
137         if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
138                 ISP_LOCK(isp);
139                 xpt_bus_deregister(cam_sim_path(sim));
140                 ISP_UNLOCK(isp);
141                 cam_sim_free(sim, FALSE);
142                 return (ENXIO);
143         }
144         xpt_setup_ccb(&csa.ccb_h, path, 5);
145         csa.ccb_h.func_code = XPT_SASYNC_CB;
146         csa.event_enable = AC_LOST_DEVICE;
147         csa.callback = isp_cam_async;
148         csa.callback_arg = sim;
149
150         ISP_LOCK(isp);
151         xpt_action((union ccb *)&csa);
152         ISP_UNLOCK(isp);
153
154         if (IS_SCSI(isp)) {
155                 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
156                 spi->sim = sim;
157                 spi->path = path;
158         } else {
159                 fcparam *fcp = FCPARAM(isp, chan);
160                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
161                 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
162                 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
163                 char name[16];
164
165                 ISP_LOCK(isp);
166                 fc->sim = sim;
167                 fc->path = path;
168                 fc->isp = isp;
169                 fc->ready = 1;
170
171                 callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
172                 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
173                 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
174                 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
175
176                 /*
177                  * We start by being "loop down" if we have an initiator role
178                  */
179                 if (fcp->role & ISP_ROLE_INITIATOR) {
180                         isp_freeze_loopdown(isp, chan, "isp_attach");
181                         callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
182                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
183                 }
184                 ISP_UNLOCK(isp);
185                 if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
186                         xpt_free_path(fc->path);
187                         ISP_LOCK(isp);
188                         if (callout_active(&fc->ldt))
189                                 callout_stop(&fc->ldt);
190                         xpt_bus_deregister(cam_sim_path(fc->sim));
191                         ISP_UNLOCK(isp);
192                         cam_sim_free(fc->sim, FALSE);
193                         return (ENOMEM);
194                 }
195                 fc->num_threads += 1;
196                 if (chan > 0) {
197                         snprintf(name, sizeof(name), "chan%d", chan);
198                         tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
199                             OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
200                 }
201                 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
202                     "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
203                     "World Wide Node Name");
204                 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
205                     "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
206                     "World Wide Port Name");
207                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
208                     "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
209                     "Loop Down Limit");
210                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
211                     "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
212                     "Gone Device Time");
213 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
214                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
215                     "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
216                     "Cause a Lost Frame on a Read");
217 #endif
218                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
219                     "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
220                     isp_role_sysctl, "I", "Current role");
221                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
222                     "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
223                     "Connection speed in gigabits");
224                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
225                     "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
226                     "Link state");
227                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
228                     "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
229                     "Firmware state");
230                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
231                     "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
232                     "Loop state");
233                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
234                     "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
235                     "Connection topology");
236         }
237         return (0);
238 }
239
240 static void
241 isp_detach_chan(ispsoftc_t *isp, int chan)
242 {
243         struct cam_sim *sim;
244         struct cam_path *path;
245         struct ccb_setasync csa;
246         int *num_threads;
247
248         ISP_GET_PC(isp, chan, sim, sim);
249         ISP_GET_PC(isp, chan, path, path);
250         ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
251
252         xpt_setup_ccb(&csa.ccb_h, path, 5);
253         csa.ccb_h.func_code = XPT_SASYNC_CB;
254         csa.event_enable = 0;
255         csa.callback = isp_cam_async;
256         csa.callback_arg = sim;
257         xpt_action((union ccb *)&csa);
258         xpt_free_path(path);
259         xpt_bus_deregister(cam_sim_path(sim));
260         cam_sim_free(sim, FALSE);
261
262         /* Wait for the channel's spawned threads to exit. */
263         wakeup(isp->isp_osinfo.pc.ptr);
264         while (*num_threads != 0)
265                 mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
266 }
267
268 int
269 isp_attach(ispsoftc_t *isp)
270 {
271         const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
272         int du = device_get_unit(isp->isp_dev);
273         int chan;
274
275         isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
276         isp->isp_osinfo.ehook.ich_arg = isp;
277         /*
278          * Haha. Set this first, because if we're loaded as a module isp_intr_enable
279          * will be called right awawy, which will clear isp_osinfo.ehook_active,
280          * which would be unwise to then set again later.
281          */
282         isp->isp_osinfo.ehook_active = 1;
283         if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
284                 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
285                 return (-EIO);
286         }
287
288         /*
289          * Create the device queue for our SIM(s).
290          */
291         isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
292         if (isp->isp_osinfo.devq == NULL) {
293                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
294                 return (EIO);
295         }
296
297         for (chan = 0; chan < isp->isp_nchan; chan++) {
298                 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
299                         goto unwind;
300                 }
301         }
302
303         callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
304         isp_timer_count = hz >> 2;
305         callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
306         isp->isp_osinfo.timer_active = 1;
307
308         isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
309         if (isp->isp_osinfo.cdev) {
310                 isp->isp_osinfo.cdev->si_drv1 = isp;
311         }
312         return (0);
313
314 unwind:
315         while (--chan >= 0) {
316                 struct cam_sim *sim;
317                 struct cam_path *path;
318
319                 ISP_GET_PC(isp, chan, sim, sim);
320                 ISP_GET_PC(isp, chan, path, path);
321                 xpt_free_path(path);
322                 ISP_LOCK(isp);
323                 xpt_bus_deregister(cam_sim_path(sim));
324                 ISP_UNLOCK(isp);
325                 cam_sim_free(sim, FALSE);
326         }
327         if (isp->isp_osinfo.ehook_active) {
328                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
329                 isp->isp_osinfo.ehook_active = 0;
330         }
331         if (isp->isp_osinfo.cdev) {
332                 destroy_dev(isp->isp_osinfo.cdev);
333                 isp->isp_osinfo.cdev = NULL;
334         }
335         cam_simq_free(isp->isp_osinfo.devq);
336         isp->isp_osinfo.devq = NULL;
337         return (-1);
338 }
339
340 int
341 isp_detach(ispsoftc_t *isp)
342 {
343         struct cam_sim *sim;
344         int chan;
345
346         ISP_LOCK(isp);
347         for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
348                 ISP_GET_PC(isp, chan, sim, sim);
349                 if (sim->refcount > 2) {
350                         ISP_UNLOCK(isp);
351                         return (EBUSY);
352                 }
353         }
354         /* Tell spawned threads that we're exiting. */
355         isp->isp_osinfo.is_exiting = 1;
356         if (isp->isp_osinfo.timer_active) {
357                 callout_stop(&isp->isp_osinfo.tmo);
358                 isp->isp_osinfo.timer_active = 0;
359         }
360         for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
361                 isp_detach_chan(isp, chan);
362         ISP_UNLOCK(isp);
363
364         if (isp->isp_osinfo.cdev) {
365                 destroy_dev(isp->isp_osinfo.cdev);
366                 isp->isp_osinfo.cdev = NULL;
367         }
368         if (isp->isp_osinfo.ehook_active) {
369                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
370                 isp->isp_osinfo.ehook_active = 0;
371         }
372         if (isp->isp_osinfo.devq != NULL) {
373                 cam_simq_free(isp->isp_osinfo.devq);
374                 isp->isp_osinfo.devq = NULL;
375         }
376         return (0);
377 }
378
379 static void
380 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
381 {
382         if (IS_FC(isp)) {
383                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
384                 if (fc->simqfrozen == 0) {
385                         isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
386                         fc->simqfrozen = SIMQFRZ_LOOPDOWN;
387 #if __FreeBSD_version >= 1000039
388                         xpt_hold_boot();
389 #endif
390                         xpt_freeze_simq(fc->sim, 1);
391                 } else {
392                         isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
393                         fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
394                 }
395         }
396 }
397
398 static void
399 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
400 {
401         if (IS_FC(isp)) {
402                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
403                 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
404                 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
405                 if (wasfrozen && fc->simqfrozen == 0) {
406                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
407                         xpt_release_simq(fc->sim, 1);
408 #if __FreeBSD_version >= 1000039
409                         xpt_release_boot();
410 #endif
411                 }
412         }
413 }
414
415
416 static int
417 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
418 {
419         ispsoftc_t *isp;
420         int nr, chan, retval = ENOTTY;
421
422         isp = dev->si_drv1;
423
424         switch (c) {
425         case ISP_SDBLEV:
426         {
427                 int olddblev = isp->isp_dblev;
428                 isp->isp_dblev = *(int *)addr;
429                 *(int *)addr = olddblev;
430                 retval = 0;
431                 break;
432         }
433         case ISP_GETROLE:
434                 chan = *(int *)addr;
435                 if (chan < 0 || chan >= isp->isp_nchan) {
436                         retval = -ENXIO;
437                         break;
438                 }
439                 if (IS_FC(isp)) {
440                         *(int *)addr = FCPARAM(isp, chan)->role;
441                 } else {
442                         *(int *)addr = SDPARAM(isp, chan)->role;
443                 }
444                 retval = 0;
445                 break;
446         case ISP_SETROLE:
447                 nr = *(int *)addr;
448                 chan = nr >> 8;
449                 if (chan < 0 || chan >= isp->isp_nchan) {
450                         retval = -ENXIO;
451                         break;
452                 }
453                 nr &= 0xff;
454                 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
455                         retval = EINVAL;
456                         break;
457                 }
458                 ISP_LOCK(isp);
459                 if (IS_FC(isp))
460                         *(int *)addr = FCPARAM(isp, chan)->role;
461                 else
462                         *(int *)addr = SDPARAM(isp, chan)->role;
463                 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
464                 ISP_UNLOCK(isp);
465                 retval = 0;
466                 break;
467
468         case ISP_RESETHBA:
469                 ISP_LOCK(isp);
470                 isp_reinit(isp, 0);
471                 ISP_UNLOCK(isp);
472                 retval = 0;
473                 break;
474
475         case ISP_RESCAN:
476                 if (IS_FC(isp)) {
477                         chan = *(int *)addr;
478                         if (chan < 0 || chan >= isp->isp_nchan) {
479                                 retval = -ENXIO;
480                                 break;
481                         }
482                         ISP_LOCK(isp);
483                         if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
484                                 retval = EIO;
485                         } else {
486                                 retval = 0;
487                         }
488                         ISP_UNLOCK(isp);
489                 }
490                 break;
491
492         case ISP_FC_LIP:
493                 if (IS_FC(isp)) {
494                         chan = *(int *)addr;
495                         if (chan < 0 || chan >= isp->isp_nchan) {
496                                 retval = -ENXIO;
497                                 break;
498                         }
499                         ISP_LOCK(isp);
500                         if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
501                                 retval = EIO;
502                         } else {
503                                 retval = 0;
504                         }
505                         ISP_UNLOCK(isp);
506                 }
507                 break;
508         case ISP_FC_GETDINFO:
509         {
510                 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
511                 fcportdb_t *lp;
512
513                 if (IS_SCSI(isp)) {
514                         break;
515                 }
516                 if (ifc->loopid >= MAX_FC_TARG) {
517                         retval = EINVAL;
518                         break;
519                 }
520                 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
521                 if (lp->state != FC_PORTDB_STATE_NIL) {
522                         ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
523                         ifc->loopid = lp->handle;
524                         ifc->portid = lp->portid;
525                         ifc->node_wwn = lp->node_wwn;
526                         ifc->port_wwn = lp->port_wwn;
527                         retval = 0;
528                 } else {
529                         retval = ENODEV;
530                 }
531                 break;
532         }
533         case ISP_GET_STATS:
534         {
535                 isp_stats_t *sp = (isp_stats_t *) addr;
536
537                 ISP_MEMZERO(sp, sizeof (*sp));
538                 sp->isp_stat_version = ISP_STATS_VERSION;
539                 sp->isp_type = isp->isp_type;
540                 sp->isp_revision = isp->isp_revision;
541                 ISP_LOCK(isp);
542                 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
543                 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
544                 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
545                 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
546                 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
547                 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
548                 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
549                 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
550                 ISP_UNLOCK(isp);
551                 retval = 0;
552                 break;
553         }
554         case ISP_CLR_STATS:
555                 ISP_LOCK(isp);
556                 isp->isp_intcnt = 0;
557                 isp->isp_intbogus = 0;
558                 isp->isp_intmboxc = 0;
559                 isp->isp_intoasync = 0;
560                 isp->isp_rsltccmplt = 0;
561                 isp->isp_fphccmplt = 0;
562                 isp->isp_rscchiwater = 0;
563                 isp->isp_fpcchiwater = 0;
564                 ISP_UNLOCK(isp);
565                 retval = 0;
566                 break;
567         case ISP_FC_GETHINFO:
568         {
569                 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
570                 int chan = hba->fc_channel;
571
572                 if (chan < 0 || chan >= isp->isp_nchan) {
573                         retval = ENXIO;
574                         break;
575                 }
576                 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
577                 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
578                 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
579                 hba->fc_nchannels = isp->isp_nchan;
580                 if (IS_FC(isp)) {
581                         hba->fc_nports = MAX_FC_TARG;
582                         hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
583                         hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
584                         hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
585                         hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
586                         hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
587                         hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
588                         hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
589                 } else {
590                         hba->fc_nports = MAX_TARGETS;
591                         hba->fc_speed = 0;
592                         hba->fc_topology = 0;
593                         hba->nvram_node_wwn = 0ull;
594                         hba->nvram_port_wwn = 0ull;
595                         hba->active_node_wwn = 0ull;
596                         hba->active_port_wwn = 0ull;
597                 }
598                 retval = 0;
599                 break;
600         }
601         case ISP_TSK_MGMT:
602         {
603                 int needmarker;
604                 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
605                 uint16_t loopid;
606                 mbreg_t mbs;
607
608                 if (IS_SCSI(isp)) {
609                         break;
610                 }
611
612                 chan = fct->chan;
613                 if (chan < 0 || chan >= isp->isp_nchan) {
614                         retval = -ENXIO;
615                         break;
616                 }
617
618                 needmarker = retval = 0;
619                 loopid = fct->loopid;
620                 ISP_LOCK(isp);
621                 if (IS_24XX(isp)) {
622                         uint8_t local[QENTRY_LEN];
623                         isp24xx_tmf_t *tmf;
624                         isp24xx_statusreq_t *sp;
625                         fcparam *fcp = FCPARAM(isp, chan);
626                         fcportdb_t *lp;
627                         int i;
628
629                         for (i = 0; i < MAX_FC_TARG; i++) {
630                                 lp = &fcp->portdb[i];
631                                 if (lp->handle == loopid) {
632                                         break;
633                                 }
634                         }
635                         if (i == MAX_FC_TARG) {
636                                 retval = ENXIO;
637                                 ISP_UNLOCK(isp);
638                                 break;
639                         }
640                         /* XXX VALIDATE LP XXX */
641                         tmf = (isp24xx_tmf_t *) local;
642                         ISP_MEMZERO(tmf, QENTRY_LEN);
643                         tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
644                         tmf->tmf_header.rqs_entry_count = 1;
645                         tmf->tmf_nphdl = lp->handle;
646                         tmf->tmf_delay = 2;
647                         tmf->tmf_timeout = 2;
648                         tmf->tmf_tidlo = lp->portid;
649                         tmf->tmf_tidhi = lp->portid >> 16;
650                         tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
651                         tmf->tmf_lun[1] = fct->lun & 0xff;
652                         if (fct->lun >= 256) {
653                                 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
654                         }
655                         switch (fct->action) {
656                         case IPT_CLEAR_ACA:
657                                 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
658                                 break;
659                         case IPT_TARGET_RESET:
660                                 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
661                                 needmarker = 1;
662                                 break;
663                         case IPT_LUN_RESET:
664                                 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
665                                 needmarker = 1;
666                                 break;
667                         case IPT_CLEAR_TASK_SET:
668                                 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
669                                 needmarker = 1;
670                                 break;
671                         case IPT_ABORT_TASK_SET:
672                                 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
673                                 needmarker = 1;
674                                 break;
675                         default:
676                                 retval = EINVAL;
677                                 break;
678                         }
679                         if (retval) {
680                                 ISP_UNLOCK(isp);
681                                 break;
682                         }
683                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
684                         mbs.param[1] = QENTRY_LEN;
685                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
686                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
687                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
688                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
689
690                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
691                                 ISP_UNLOCK(isp);
692                                 retval = ENOMEM;
693                                 break;
694                         }
695                         isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
696                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
697                         sp = (isp24xx_statusreq_t *) local;
698                         sp->req_completion_status = 1;
699                         retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
700                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
701                         isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
702                         FC_SCRATCH_RELEASE(isp, chan);
703                         if (retval || sp->req_completion_status != 0) {
704                                 FC_SCRATCH_RELEASE(isp, chan);
705                                 retval = EIO;
706                         }
707                         if (retval == 0) {
708                                 if (needmarker) {
709                                         fcp->sendmarker = 1;
710                                 }
711                         }
712                 } else {
713                         MBSINIT(&mbs, 0, MBLOGALL, 0);
714                         if (ISP_CAP_2KLOGIN(isp) == 0) {
715                                 loopid <<= 8;
716                         }
717                         switch (fct->action) {
718                         case IPT_CLEAR_ACA:
719                                 mbs.param[0] = MBOX_CLEAR_ACA;
720                                 mbs.param[1] = loopid;
721                                 mbs.param[2] = fct->lun;
722                                 break;
723                         case IPT_TARGET_RESET:
724                                 mbs.param[0] = MBOX_TARGET_RESET;
725                                 mbs.param[1] = loopid;
726                                 needmarker = 1;
727                                 break;
728                         case IPT_LUN_RESET:
729                                 mbs.param[0] = MBOX_LUN_RESET;
730                                 mbs.param[1] = loopid;
731                                 mbs.param[2] = fct->lun;
732                                 needmarker = 1;
733                                 break;
734                         case IPT_CLEAR_TASK_SET:
735                                 mbs.param[0] = MBOX_CLEAR_TASK_SET;
736                                 mbs.param[1] = loopid;
737                                 mbs.param[2] = fct->lun;
738                                 needmarker = 1;
739                                 break;
740                         case IPT_ABORT_TASK_SET:
741                                 mbs.param[0] = MBOX_ABORT_TASK_SET;
742                                 mbs.param[1] = loopid;
743                                 mbs.param[2] = fct->lun;
744                                 needmarker = 1;
745                                 break;
746                         default:
747                                 retval = EINVAL;
748                                 break;
749                         }
750                         if (retval == 0) {
751                                 if (needmarker) {
752                                         FCPARAM(isp, chan)->sendmarker = 1;
753                                 }
754                                 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
755                                 if (retval) {
756                                         retval = EIO;
757                                 }
758                         }
759                 }
760                 ISP_UNLOCK(isp);
761                 break;
762         }
763         default:
764                 break;
765         }
766         return (retval);
767 }
768
769 static void
770 isp_intr_enable(void *arg)
771 {
772         int chan;
773         ispsoftc_t *isp = arg;
774         ISP_LOCK(isp);
775         for (chan = 0; chan < isp->isp_nchan; chan++) {
776                 if (IS_FC(isp)) {
777                         if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
778                                 ISP_ENABLE_INTS(isp);
779                                 break;
780                         }
781                 } else {
782                         if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
783                                 ISP_ENABLE_INTS(isp);
784                                 break;
785                         }
786                 }
787         }
788         isp->isp_osinfo.ehook_active = 0;
789         ISP_UNLOCK(isp);
790         /* Release our hook so that the boot can continue. */
791         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
792 }
793
794 /*
795  * Local Inlines
796  */
797
798 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
799 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
800
801 static ISP_INLINE int
802 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
803 {
804         ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
805         if (ISP_PCMD(ccb) == NULL) {
806                 return (-1);
807         }
808         isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
809         return (0);
810 }
811
812 static ISP_INLINE void
813 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
814 {
815         if (ISP_PCMD(ccb)) {
816 #ifdef  ISP_TARGET_MODE
817                 PISP_PCMD(ccb)->datalen = 0;
818                 PISP_PCMD(ccb)->totslen = 0;
819                 PISP_PCMD(ccb)->cumslen = 0;
820                 PISP_PCMD(ccb)->crn = 0;
821 #endif
822                 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
823                 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
824                 ISP_PCMD(ccb) = NULL;
825         }
826 }
827
828 /*
829  * Put the target mode functions here, because some are inlines
830  */
831 #ifdef  ISP_TARGET_MODE
832 static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
833 static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
834 static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
835 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
836 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
837 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
838 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
839 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
840 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
841 static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
842 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
843 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
844 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
845 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
846 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
847 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
848 static void isp_enable_lun(ispsoftc_t *, union ccb *);
849 static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
850 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
851 static void isp_disable_lun(ispsoftc_t *, union ccb *);
852 static int isp_enable_target_mode(ispsoftc_t *, int);
853 static int isp_disable_target_mode(ispsoftc_t *, int);
854 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
855 static timeout_t isp_refire_putback_atio;
856 static timeout_t isp_refire_notify_ack;
857 static void isp_complete_ctio(union ccb *);
858 static void isp_target_putback_atio(union ccb *);
859 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
860 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
861 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
862 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
863 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
864 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
865 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
866 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
867 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
868 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
869 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
870 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
871 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
872
873 static ISP_INLINE void
874 isp_tmlock(ispsoftc_t *isp, const char *msg)
875 {
876         while (isp->isp_osinfo.tmbusy) {
877                 isp->isp_osinfo.tmwanted = 1;
878                 mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
879         }
880         isp->isp_osinfo.tmbusy = 1;
881 }
882
883 static ISP_INLINE void
884 isp_tmunlk(ispsoftc_t *isp)
885 {
886         isp->isp_osinfo.tmbusy = 0;
887         if (isp->isp_osinfo.tmwanted) {
888                 isp->isp_osinfo.tmwanted = 0;
889                 wakeup(isp);
890         }
891 }
892
893 static ISP_INLINE int
894 is_any_lun_enabled(ispsoftc_t *isp, int bus)
895 {
896         struct tslist *lhp;
897         int i;
898
899         for (i = 0; i < LUN_HASH_SIZE; i++) {
900                 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
901                 if (SLIST_FIRST(lhp))
902                         return (1);
903         }
904         return (0);
905 }
906
907 static ISP_INLINE int
908 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
909 {
910         tstate_t *tptr;
911         struct tslist *lhp;
912
913         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
914         SLIST_FOREACH(tptr, lhp, next) {
915                 if (tptr->ts_lun == lun) {
916                         return (1);
917                 }
918         }
919         return (0);
920 }
921
922 static void
923 dump_tstates(ispsoftc_t *isp, int bus)
924 {
925         int i, j;
926         struct tslist *lhp;
927         tstate_t *tptr = NULL;
928
929         if (bus >= isp->isp_nchan) {
930                 return;
931         }
932         for (i = 0; i < LUN_HASH_SIZE; i++) {
933                 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
934                 j = 0;
935                 SLIST_FOREACH(tptr, lhp, next) {
936                         xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
937                         j++;
938                 }
939         }
940 }
941
942 static ISP_INLINE tstate_t *
943 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
944 {
945         tstate_t *tptr = NULL;
946         struct tslist *lhp;
947
948         if (bus < isp->isp_nchan) {
949                 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
950                 SLIST_FOREACH(tptr, lhp, next) {
951                         if (tptr->ts_lun == lun) {
952                                 tptr->hold++;
953                                 return (tptr);
954                         }
955                 }
956         }
957         return (NULL);
958 }
959
960 static ISP_INLINE tstate_t *
961 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
962 {
963         tstate_t *tptr = NULL;
964         atio_private_data_t *atp;
965         struct tslist *lhp;
966         int i;
967
968         if (bus < isp->isp_nchan && tagval != 0) {
969                 for (i = 0; i < LUN_HASH_SIZE; i++) {
970                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
971                         SLIST_FOREACH(tptr, lhp, next) {
972                                 atp = isp_find_atpd(isp, tptr, tagval);
973                                 if (atp) {
974                                         tptr->hold++;
975                                         return (tptr);
976                                 }
977                         }
978                 }
979         }
980         return (NULL);
981 }
982
983 static ISP_INLINE inot_private_data_t *
984 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
985 {
986         inot_private_data_t *ntp;
987         tstate_t *tptr;
988         struct tslist *lhp;
989         int bus, i;
990
991         for (bus = 0; bus < isp->isp_nchan; bus++) {
992                 for (i = 0; i < LUN_HASH_SIZE; i++) {
993                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
994                         SLIST_FOREACH(tptr, lhp, next) {
995                                 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
996                                 if (ntp) {
997                                         *rslt = tptr;
998                                         tptr->hold++;
999                                         return (ntp);
1000                                 }
1001                         }
1002                 }
1003         }
1004         return (NULL);
1005 }
1006
1007 static ISP_INLINE void
1008 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
1009 {
1010         KASSERT((tptr->hold), ("tptr not held"));
1011         tptr->hold--;
1012 }
1013
1014 static void
1015 isp_tmcmd_restart(ispsoftc_t *isp)
1016 {
1017         inot_private_data_t *ntp;
1018         inot_private_data_t *restart_queue;
1019         tstate_t *tptr;
1020         union ccb *ccb;
1021         struct tslist *lhp;
1022         int bus, i;
1023
1024         for (bus = 0; bus < isp->isp_nchan; bus++) {
1025                 for (i = 0; i < LUN_HASH_SIZE; i++) {
1026                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1027                         SLIST_FOREACH(tptr, lhp, next) {
1028                                 if ((restart_queue = tptr->restart_queue) != NULL)
1029                                         tptr->restart_queue = NULL;
1030                                 while (restart_queue) {
1031                                         ntp = restart_queue;
1032                                         restart_queue = ntp->rd.nt.nt_hba;
1033                                         if (IS_24XX(isp)) {
1034                                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
1035                                                 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
1036                                         } else {
1037                                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1038                                                 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1039                                         }
1040                                         isp_put_ntpd(isp, tptr, ntp);
1041                                         if (tptr->restart_queue && restart_queue != NULL) {
1042                                                 ntp = tptr->restart_queue;
1043                                                 tptr->restart_queue = restart_queue;
1044                                                 while (restart_queue->rd.nt.nt_hba) {
1045                                                         restart_queue = restart_queue->rd.nt.nt_hba;
1046                                                 }
1047                                                 restart_queue->rd.nt.nt_hba = ntp;
1048                                                 break;
1049                                         }
1050                                 }
1051                                 /*
1052                                  * We only need to do this once per tptr
1053                                  */
1054                                 if (!TAILQ_EMPTY(&tptr->waitq)) {
1055                                         ccb = (union ccb *)TAILQ_LAST(&tptr->waitq, isp_ccbq);
1056                                         TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1057                                         isp_target_start_ctio(isp, ccb, FROM_TIMER);
1058                                 }
1059                         }
1060                 }
1061         }
1062 }
1063
1064 static ISP_INLINE atio_private_data_t *
1065 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1066 {
1067         atio_private_data_t *atp;
1068
1069         atp = LIST_FIRST(&tptr->atfree);
1070         if (atp) {
1071                 LIST_REMOVE(atp, next);
1072                 atp->tag = tag;
1073                 LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
1074         }
1075         return (atp);
1076 }
1077
1078 static ISP_INLINE atio_private_data_t *
1079 isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1080 {
1081         atio_private_data_t *atp;
1082
1083         LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1084                 if (atp->tag == tag)
1085                         return (atp);
1086         }
1087         return (NULL);
1088 }
1089
1090 static ISP_INLINE void
1091 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1092 {
1093         if (atp->ests) {
1094                 isp_put_ecmd(isp, atp->ests);
1095         }
1096         LIST_REMOVE(atp, next);
1097         memset(atp, 0, sizeof (*atp));
1098         LIST_INSERT_HEAD(&tptr->atfree, atp, next);
1099 }
1100
1101 static void
1102 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1103 {
1104         atio_private_data_t *atp;
1105         const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1106
1107         for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1108                 xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1109                     atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1110         }
1111 }
1112
1113
1114 static ISP_INLINE inot_private_data_t *
1115 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1116 {
1117         inot_private_data_t *ntp;
1118         ntp = tptr->ntfree;
1119         if (ntp) {
1120                 tptr->ntfree = ntp->next;
1121         }
1122         return (ntp);
1123 }
1124
1125 static ISP_INLINE inot_private_data_t *
1126 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1127 {
1128         inot_private_data_t *ntp;
1129         for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1130                 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1131                         return (ntp);
1132                 }
1133         }
1134         return (NULL);
1135 }
1136
1137 static ISP_INLINE void
1138 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1139 {
1140         ntp->rd.tag_id = ntp->rd.seq_id = 0;
1141         ntp->next = tptr->ntfree;
1142         tptr->ntfree = ntp;
1143 }
1144
1145 static cam_status
1146 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1147 {
1148         cam_status status;
1149         lun_id_t lun;
1150         struct tslist *lhp;
1151         tstate_t *tptr;
1152         int i;
1153
1154         lun = xpt_path_lun_id(path);
1155         if (lun != CAM_LUN_WILDCARD) {
1156                 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
1157                         return (CAM_LUN_INVALID);
1158                 }
1159         }
1160         if (is_lun_enabled(isp, bus, lun)) {
1161                 return (CAM_LUN_ALRDY_ENA);
1162         }
1163         tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1164         if (tptr == NULL) {
1165                 return (CAM_RESRC_UNAVAIL);
1166         }
1167         tptr->ts_lun = lun;
1168         status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1169         if (status != CAM_REQ_CMP) {
1170                 free(tptr, M_DEVBUF);
1171                 return (status);
1172         }
1173         SLIST_INIT(&tptr->atios);
1174         SLIST_INIT(&tptr->inots);
1175         TAILQ_INIT(&tptr->waitq);
1176         LIST_INIT(&tptr->atfree);
1177         for (i = ATPDPSIZE-1; i >= 0; i--)
1178                 LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1179         for (i = 0; i < ATPDPHASHSIZE; i++)
1180                 LIST_INIT(&tptr->atused[i]);
1181         for (i = 0; i < ATPDPSIZE-1; i++)
1182                 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1183         tptr->ntfree = tptr->ntpool;
1184         tptr->hold = 1;
1185         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
1186         SLIST_INSERT_HEAD(lhp, tptr, next);
1187         *rslt = tptr;
1188         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1189         return (CAM_REQ_CMP);
1190 }
1191
1192 static ISP_INLINE void
1193 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1194 {
1195         union ccb *ccb;
1196         struct tslist *lhp;
1197
1198         KASSERT((tptr->hold != 0), ("tptr is not held"));
1199         KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1200         do {
1201                 ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1202                 if (ccb) {
1203                         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1204                         ccb->ccb_h.status = CAM_REQ_ABORTED;
1205                         xpt_done(ccb);
1206                 }
1207         } while (ccb);
1208         do {
1209                 ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1210                 if (ccb) {
1211                         SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1212                         ccb->ccb_h.status = CAM_REQ_ABORTED;
1213                         xpt_done(ccb);
1214                 }
1215         } while (ccb);
1216         ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1217         SLIST_REMOVE(lhp, tptr, tstate, next);
1218         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1219         xpt_free_path(tptr->owner);
1220         free(tptr, M_DEVBUF);
1221 }
1222
1223 /*
1224  * Enable a lun.
1225  */
1226 static void
1227 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1228 {
1229         tstate_t *tptr = NULL;
1230         int bus, tm_enabled, target_role;
1231         target_id_t target;
1232         lun_id_t lun;
1233
1234
1235         /*
1236          * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1237          */
1238         bus = XS_CHANNEL(ccb);
1239         target = ccb->ccb_h.target_id;
1240         lun = ccb->ccb_h.target_lun;
1241         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1242             "enabling lun %jx\n", (uintmax_t)lun);
1243         if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1244                 ccb->ccb_h.status = CAM_LUN_INVALID;
1245                 xpt_done(ccb);
1246                 return;
1247         }
1248
1249         if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1250                 ccb->ccb_h.status = CAM_LUN_INVALID;
1251                 xpt_done(ccb);
1252                 return;
1253         }
1254         if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1255                 xpt_print(ccb->ccb_h.path,
1256                     "enabling lun 0x%jx on channel %d\n", (uintmax_t)lun, bus);
1257         }
1258
1259         /*
1260          * Wait until we're not busy with the lun enables subsystem
1261          */
1262         isp_tmlock(isp, "isp_enable_lun");
1263
1264         /*
1265          * This is as a good a place as any to check f/w capabilities.
1266          */
1267
1268         if (IS_FC(isp)) {
1269                 if (ISP_CAP_TMODE(isp) == 0) {
1270                         xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1271                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1272                         goto done;
1273                 }
1274                 /*
1275                  * We *could* handle non-SCCLUN f/w, but we'd have to
1276                  * dork with our already fragile enable/disable code.
1277                  */
1278                 if (ISP_CAP_SCCFW(isp) == 0) {
1279                         xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1280                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1281                         goto done;
1282                 }
1283
1284                 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1285
1286         } else {
1287                 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1288         }
1289
1290         /*
1291          * Create the state pointer.
1292          * It should not already exist.
1293          */
1294         tptr = get_lun_statep(isp, bus, lun);
1295         if (tptr) {
1296                 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1297                 goto done;
1298         }
1299         ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1300         if (ccb->ccb_h.status != CAM_REQ_CMP) {
1301                 goto done;
1302         }
1303
1304         /*
1305          * We have a tricky maneuver to perform here.
1306          *
1307          * If target mode isn't already enabled here,
1308          * *and* our current role includes target mode,
1309          * we enable target mode here.
1310          *
1311          */
1312         ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1313         if (tm_enabled == 0 && target_role != 0) {
1314                 if (isp_enable_target_mode(isp, bus)) {
1315                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1316                         destroy_lun_state(isp, tptr);
1317                         tptr = NULL;
1318                         goto done;
1319                 }
1320                 tm_enabled = 1;
1321         }
1322
1323         /*
1324          * Now check to see whether this bus is in target mode already.
1325          *
1326          * If not, a later role change into target mode will finish the job.
1327          */
1328         if (tm_enabled == 0) {
1329                 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1330                 ccb->ccb_h.status = CAM_REQ_CMP;
1331                 xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1332                 goto done1;
1333         }
1334
1335         /*
1336          * Enable the lun.
1337          */
1338         ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1339
1340 done:
1341         if (ccb->ccb_h.status != CAM_REQ_CMP)  {
1342                 if (tptr) {
1343                         destroy_lun_state(isp, tptr);
1344                         tptr = NULL;
1345                 }
1346         } else {
1347                 tptr->enabled = 1;
1348         }
1349 done1:
1350         if (tptr) {
1351                 rls_lun_statep(isp, tptr);
1352         }
1353
1354         /*
1355          * And we're outta here....
1356          */
1357         isp_tmunlk(isp);
1358         xpt_done(ccb);
1359 }
1360
1361 static cam_status
1362 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1363 {
1364         tstate_t *tptr = NULL;
1365         struct tslist *lhp;
1366         int i, n;
1367
1368
1369         ISP_GET_PC(isp, bus, tm_enabled, i);
1370         if (i == 1) {
1371                 return (CAM_REQ_CMP);
1372         }
1373         ISP_GET_PC(isp, bus, tm_enable_defer, i);
1374         if (i == 0) {
1375                 return (CAM_REQ_CMP);
1376         }
1377         /*
1378          * If this succeeds, it will set tm_enable
1379          */
1380         if (isp_enable_target_mode(isp, bus)) {
1381                 return (CAM_REQ_CMP_ERR);
1382         }
1383         isp_tmlock(isp, "isp_enable_deferred_luns");
1384         for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1385                 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1386                 SLIST_FOREACH(tptr, lhp, next) {
1387                         tptr->hold++;
1388                         if (tptr->enabled == 0) {
1389                                 if (isp_enable_deferred(isp, bus, tptr->ts_lun) == CAM_REQ_CMP) {
1390                                         tptr->enabled = 1;
1391                                         n++;
1392                                 }
1393                         } else {
1394                                 n++;
1395                         }
1396                         tptr->hold--;
1397                 }
1398         }
1399         isp_tmunlk(isp);
1400         if (n == 0) {
1401                 return (CAM_REQ_CMP_ERR);
1402         }
1403         ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1404         return (CAM_REQ_CMP);
1405 }
1406
1407 static cam_status
1408 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1409 {
1410         cam_status status;
1411         int luns_already_enabled;
1412
1413         ISP_GET_PC(isp, bus, tm_luns_enabled, luns_already_enabled);
1414         isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u luns_enabled %d", __func__, bus, lun, luns_already_enabled);
1415         if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
1416                 status = CAM_REQ_CMP;
1417         } else {
1418                 int cmd_cnt, not_cnt;
1419
1420                 if (IS_23XX(isp)) {
1421                         cmd_cnt = DFLT_CMND_CNT;
1422                         not_cnt = DFLT_INOT_CNT;
1423                 } else {
1424                         cmd_cnt = 64;
1425                         not_cnt = 8;
1426                 }
1427                 status = CAM_REQ_INPROG;
1428                 isp->isp_osinfo.rptr = &status;
1429                 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun == CAM_LUN_WILDCARD? 0 : lun, cmd_cnt, not_cnt)) {
1430                         status = CAM_RESRC_UNAVAIL;
1431                 } else {
1432                         mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1433                 }
1434                 isp->isp_osinfo.rptr = NULL;
1435         }
1436         if (status == CAM_REQ_CMP) {
1437                 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1438                 isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1439         }
1440         return (status);
1441 }
1442
1443 static void
1444 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1445 {
1446         tstate_t *tptr = NULL;
1447         int bus;
1448         cam_status status;
1449         target_id_t target;
1450         lun_id_t lun;
1451
1452         bus = XS_CHANNEL(ccb);
1453         target = ccb->ccb_h.target_id;
1454         lun = ccb->ccb_h.target_lun;
1455         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1456             "disabling lun %jx\n", (uintmax_t)lun);
1457         if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1458                 ccb->ccb_h.status = CAM_LUN_INVALID;
1459                 xpt_done(ccb);
1460                 return;
1461         }
1462
1463         if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1464                 ccb->ccb_h.status = CAM_LUN_INVALID;
1465                 xpt_done(ccb);
1466                 return;
1467         }
1468
1469         /*
1470          * See if we're busy disabling a lun now.
1471          */
1472         isp_tmlock(isp, "isp_disable_lun");
1473         status = CAM_REQ_INPROG;
1474
1475         /*
1476          * Find the state pointer.
1477          */
1478         if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1479                 status = CAM_PATH_INVALID;
1480                 goto done;
1481         }
1482
1483         /*
1484          * If we're a 24XX card, we're done.
1485          */
1486         if (IS_24XX(isp)) {
1487                 status = CAM_REQ_CMP;
1488                 goto done;
1489         }
1490
1491         /*
1492          * For SCC FW, we only deal with lun zero.
1493          */
1494         if (IS_FC(isp) && lun > 0) {
1495                 status = CAM_REQ_CMP;
1496                 goto done;
1497         }
1498         isp->isp_osinfo.rptr = &status;
1499         if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1500                 status = CAM_RESRC_UNAVAIL;
1501         } else {
1502                 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1503         }
1504         isp->isp_osinfo.rptr = NULL;
1505 done:
1506         if (status == CAM_REQ_CMP) {
1507                 tptr->enabled = 0;
1508                 if (is_any_lun_enabled(isp, bus) == 0) {
1509                         if (isp_disable_target_mode(isp, bus)) {
1510                                 status = CAM_REQ_CMP_ERR;
1511                         }
1512                 }
1513         }
1514         ccb->ccb_h.status = status;
1515         if (status == CAM_REQ_CMP) {
1516                 destroy_lun_state(isp, tptr);
1517                 xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1518         } else {
1519                 if (tptr)
1520                         rls_lun_statep(isp, tptr);
1521         }
1522         isp_tmunlk(isp);
1523         xpt_done(ccb);
1524 }
1525
1526 static int
1527 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1528 {
1529         int tm_enabled;
1530
1531         ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1532         if (tm_enabled != 0) {
1533                 return (0);
1534         }
1535         if (IS_SCSI(isp)) {
1536                 mbreg_t mbs;
1537                 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1538                 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1539                 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1540                 mbs.param[2] = bus << 7;
1541                 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1542                         isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
1543                         return (EIO);
1544                 }
1545         }
1546         ISP_SET_PC(isp, bus, tm_enabled, 1);
1547         isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
1548         return (0);
1549 }
1550
1551 static int
1552 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1553 {
1554         int tm_enabled;
1555
1556         ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1557         if (tm_enabled == 0) {
1558                 return (0);
1559         }
1560         if (IS_SCSI(isp)) {
1561                 mbreg_t mbs;
1562                 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1563                 mbs.param[2] = bus << 7;
1564                 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1565                         isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
1566                         return (EIO);
1567                 }
1568         }
1569         ISP_SET_PC(isp, bus, tm_enabled, 0);
1570         isp_prt(isp, ISP_LOGINFO, "Target Role disabled on Bus %d", bus);
1571         return (0);
1572 }
1573
1574 static void
1575 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1576 {
1577         uint32_t *rptr;
1578
1579         rptr = isp->isp_osinfo.rptr;
1580         if (lep->le_status != LUN_OK) {
1581                 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1582                 if (rptr) {
1583                         *rptr = CAM_REQ_CMP_ERR;
1584                         wakeup_one(rptr);
1585                 }
1586         } else {
1587                 if (rptr) {
1588                         *rptr = CAM_REQ_CMP;
1589                         wakeup_one(rptr);
1590                 }
1591         }
1592 }
1593
1594 static void
1595 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1596 {
1597         int fctape, sendstatus, resid;
1598         tstate_t *tptr;
1599         fcparam *fcp;
1600         atio_private_data_t *atp;
1601         struct ccb_scsiio *cso;
1602         uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1603         uint8_t local[QENTRY_LEN];
1604
1605         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1606         if (tptr == NULL) {
1607                 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1608                 if (tptr == NULL) {
1609                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
1610                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1611                         xpt_done(ccb);
1612                         return;
1613                 }
1614         }
1615         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
1616             (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1617
1618         switch (how) {
1619         case FROM_TIMER:
1620         case FROM_CAM:
1621                 /*
1622                  * Insert at the tail of the list, if any, waiting CTIO CCBs
1623                  */
1624                 TAILQ_INSERT_TAIL(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
1625                 break;
1626         case FROM_SRR:
1627         case FROM_CTIO_DONE:
1628                 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
1629                 break;
1630         }
1631
1632         while (TAILQ_FIRST(&tptr->waitq) != NULL) {
1633                 ccb = (union ccb *) TAILQ_FIRST(&tptr->waitq);
1634                 TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1635
1636                 cso = &ccb->csio;
1637                 xfrlen = cso->dxfer_len;
1638                 if (xfrlen == 0) {
1639                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1640                                 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1641                                 ccb->ccb_h.status = CAM_REQ_INVALID;
1642                                 xpt_done(ccb);
1643                                 continue;
1644                         }
1645                 }
1646
1647                 atp = isp_find_atpd(isp, tptr, cso->tag_id);
1648                 if (atp == NULL) {
1649                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1650                         isp_dump_atpd(isp, tptr);
1651                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1652                         xpt_done(ccb);
1653                         continue;
1654                 }
1655
1656                 /*
1657                  * Is this command a dead duck?
1658                  */
1659                 if (atp->dead) {
1660                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1661                         ccb->ccb_h.status = CAM_REQ_ABORTED;
1662                         xpt_done(ccb);
1663                         continue;
1664                 }
1665
1666                 /*
1667                  * Check to make sure we're still in target mode.
1668                  */
1669                 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1670                 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1671                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1672                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1673                         xpt_done(ccb);
1674                         continue;
1675                 }
1676
1677                 /*
1678                  * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1679                  * could be split into two CTIOs to split data and status).
1680                  */
1681                 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1682                         isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1683                         TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
1684                         break;
1685                 }
1686
1687                 /*
1688                  * Does the initiator expect FC-Tape style responses?
1689                  */
1690                 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1691                         fctape = 1;
1692                 } else {
1693                         fctape = 0;
1694                 }
1695
1696                 /*
1697                  * If we already did the data xfer portion of a CTIO that sends data
1698                  * and status, don't do it again and do the status portion now.
1699                  */
1700                 if (atp->sendst) {
1701                         isp_prt(isp, ISP_LOGTINFO, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1702                             cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1703                         xfrlen = 0;     /* we already did the data transfer */
1704                         atp->sendst = 0;
1705                 }
1706                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1707                         sendstatus = 1;
1708                 } else {
1709                         sendstatus = 0;
1710                 }
1711
1712                 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1713                         KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1714                         /*
1715                          * Sense length is not the entire sense data structure size. Periph
1716                          * drivers don't seem to be setting sense_len to reflect the actual
1717                          * size. We'll peek inside to get the right amount.
1718                          */
1719                         sense_length = cso->sense_len;
1720
1721                         /*
1722                          * This 'cannot' happen
1723                          */
1724                         if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1725                                 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1726                         }
1727                 } else {
1728                         sense_length = 0;
1729                 }
1730
1731                 memset(local, 0, QENTRY_LEN);
1732
1733                 /*
1734                  * Check for overflow
1735                  */
1736                 tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
1737                 if (tmp > atp->orig_datalen) {
1738                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
1739                         ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1740                         xpt_done(ccb);
1741                         continue;
1742                 }
1743
1744                 if (IS_24XX(isp)) {
1745                         ct7_entry_t *cto = (ct7_entry_t *) local;
1746
1747                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1748                         cto->ct_header.rqs_entry_count = 1;
1749                         cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1750                         ATPD_SET_SEQNO(cto, atp);
1751                         cto->ct_nphdl = atp->nphdl;
1752                         cto->ct_rxid = atp->tag;
1753                         cto->ct_iid_lo = atp->portid;
1754                         cto->ct_iid_hi = atp->portid >> 16;
1755                         cto->ct_oxid = atp->oxid;
1756                         cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1757                         cto->ct_timeout = 120;
1758                         cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1759
1760                         /*
1761                          * Mode 1, status, no data. Only possible when we are sending status, have
1762                          * no data to transfer, and any sense data can fit into a ct7_entry_t.
1763                          *
1764                          * Mode 2, status, no data. We have to use this in the case that
1765                          * the sense data won't fit into a ct7_entry_t.
1766                          *
1767                          */
1768                         if (sendstatus && xfrlen == 0) {
1769                                 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1770                                 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1771                                 if (sense_length <= MAXRESPLEN_24XX) {
1772                                         if (resid < 0) {
1773                                                 cto->ct_resid = -resid;
1774                                         } else if (resid > 0) {
1775                                                 cto->ct_resid = resid;
1776                                         }
1777                                         cto->ct_flags |= CT7_FLAG_MODE1;
1778                                         cto->ct_scsi_status = cso->scsi_status;
1779                                         if (resid < 0) {
1780                                                 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1781                                         } else if (resid > 0) {
1782                                                 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1783                                         }
1784                                         if (fctape) {
1785                                                 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1786                                         }
1787                                         if (sense_length) {
1788                                                 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1789                                                 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1790                                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1791                                         }
1792                                 } else {
1793                                         bus_addr_t addr;
1794                                         char buf[XCMD_SIZE];
1795                                         fcp_rsp_iu_t *rp;
1796
1797                                         if (atp->ests == NULL) {
1798                                                 atp->ests = isp_get_ecmd(isp);
1799                                                 if (atp->ests == NULL) {
1800                                                         TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
1801                                                         break;
1802                                                 }
1803                                         }
1804                                         memset(buf, 0, sizeof (buf));
1805                                         rp = (fcp_rsp_iu_t *)buf;
1806                                         if (fctape) {
1807                                                 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1808                                                 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1809                                         }
1810                                         cto->ct_flags |= CT7_FLAG_MODE2;
1811                                         rp->fcp_rsp_scsi_status = cso->scsi_status;
1812                                         if (resid < 0) {
1813                                                 rp->fcp_rsp_resid = -resid;
1814                                                 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1815                                         } else if (resid > 0) {
1816                                                 rp->fcp_rsp_resid = resid;
1817                                                 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1818                                         }
1819                                         if (sense_length) {
1820                                                 rp->fcp_rsp_snslen = sense_length;
1821                                                 cto->ct_senselen = sense_length;
1822                                                 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1823                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1824                                                 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1825                                         } else {
1826                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1827                                         }
1828                                         if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1829                                                 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1830                                         }
1831                                         addr = isp->isp_osinfo.ecmd_dma;
1832                                         addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1833                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1834                                             (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1835                                         cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1836                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1837                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1838                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1839                                 }
1840                                 if (sense_length) {
1841                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
1842                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
1843                                             cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1844                                 } else {
1845                                         isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1846                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1847                                 }
1848                                 atp->state = ATPD_STATE_LAST_CTIO;
1849                         }
1850
1851                         /*
1852                          * Mode 0 data transfers, *possibly* with status.
1853                          */
1854                         if (xfrlen != 0) {
1855                                 cto->ct_flags |= CT7_FLAG_MODE0;
1856                                 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1857                                         cto->ct_flags |= CT7_DATA_IN;
1858                                 } else {
1859                                         cto->ct_flags |= CT7_DATA_OUT;
1860                                 }
1861
1862                                 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1863                                 cto->rsp.m0.ct_xfrlen = xfrlen;
1864
1865 #ifdef  DEBUG
1866                                 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1867                                         isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1868                                         ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1869                                         cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1870                                 }
1871 #endif
1872                                 if (sendstatus) {
1873                                         resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1874                                         if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1875                                                 cto->ct_flags |= CT7_SENDSTATUS;
1876                                                 atp->state = ATPD_STATE_LAST_CTIO;
1877                                                 if (fctape) {
1878                                                         cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1879                                                 }
1880                                         } else {
1881                                                 atp->sendst = 1;        /* send status later */
1882                                                 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1883                                                 atp->state = ATPD_STATE_CTIO;
1884                                         }
1885                                 } else {
1886                                         atp->state = ATPD_STATE_CTIO;
1887                                 }
1888                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
1889                                     cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1890                         }
1891                 } else if (IS_FC(isp)) {
1892                         ct2_entry_t *cto = (ct2_entry_t *) local;
1893
1894                         if (isp->isp_osinfo.sixtyfourbit)
1895                                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1896                         else
1897                                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1898                         cto->ct_header.rqs_entry_count = 1;
1899                         cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1900                         ATPD_SET_SEQNO(cto, atp);
1901                         if (ISP_CAP_2KLOGIN(isp)) {
1902                                 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1903                         } else {
1904                                 cto->ct_iid = atp->nphdl;
1905                                 if (ISP_CAP_SCCFW(isp) == 0) {
1906                                         cto->ct_lun = ccb->ccb_h.target_lun;
1907                                 }
1908                         }
1909                         cto->ct_timeout = 10;
1910                         cto->ct_rxid = cso->tag_id;
1911
1912                         /*
1913                          * Mode 1, status, no data. Only possible when we are sending status, have
1914                          * no data to transfer, and the sense length can fit in the ct7_entry.
1915                          *
1916                          * Mode 2, status, no data. We have to use this in the case the response
1917                          * length won't fit into a ct2_entry_t.
1918                          *
1919                          * We'll fill out this structure with information as if this were a
1920                          * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1921                          * needed based upon this.
1922                          */
1923                         if (sendstatus && xfrlen == 0) {
1924                                 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1925                                 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1926                                 if (sense_length <= MAXRESPLEN) {
1927                                         if (resid < 0) {
1928                                                 cto->ct_resid = -resid;
1929                                         } else if (resid > 0) {
1930                                                 cto->ct_resid = resid;
1931                                         }
1932                                         cto->ct_flags |= CT2_FLAG_MODE1;
1933                                         cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1934                                         if (resid < 0) {
1935                                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1936                                         } else if (resid > 0) {
1937                                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1938                                         }
1939                                         if (fctape) {
1940                                                 cto->ct_flags |= CT2_CONFIRM;
1941                                         }
1942                                         if (sense_length) {
1943                                                 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1944                                                 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1945                                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1946                                         }
1947                                 } else {
1948                                         bus_addr_t addr;
1949                                         char buf[XCMD_SIZE];
1950                                         fcp_rsp_iu_t *rp;
1951
1952                                         if (atp->ests == NULL) {
1953                                                 atp->ests = isp_get_ecmd(isp);
1954                                                 if (atp->ests == NULL) {
1955                                                         TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
1956                                                         break;
1957                                                 }
1958                                         }
1959                                         memset(buf, 0, sizeof (buf));
1960                                         rp = (fcp_rsp_iu_t *)buf;
1961                                         if (fctape) {
1962                                                 cto->ct_flags |= CT2_CONFIRM;
1963                                                 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1964                                         }
1965                                         cto->ct_flags |= CT2_FLAG_MODE2;
1966                                         rp->fcp_rsp_scsi_status = cso->scsi_status;
1967                                         if (resid < 0) {
1968                                                 rp->fcp_rsp_resid = -resid;
1969                                                 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1970                                         } else if (resid > 0) {
1971                                                 rp->fcp_rsp_resid = resid;
1972                                                 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1973                                         }
1974                                         if (sense_length) {
1975                                                 rp->fcp_rsp_snslen = sense_length;
1976                                                 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1977                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1978                                                 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1979                                         } else {
1980                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1981                                         }
1982                                         if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1983                                                 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1984                                         }
1985                                         addr = isp->isp_osinfo.ecmd_dma;
1986                                         addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1987                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1988                                             (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1989                                         cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1990                                         if (isp->isp_osinfo.sixtyfourbit) {
1991                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1992                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1993                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1994                                         } else {
1995                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1996                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1997                                         }
1998                                 }
1999                                 if (sense_length) {
2000                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
2001                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
2002                                             cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
2003                                 } else {
2004                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
2005                                             ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
2006                                 }
2007                                 atp->state = ATPD_STATE_LAST_CTIO;
2008                         }
2009
2010                         if (xfrlen != 0) {
2011                                 cto->ct_flags |= CT2_FLAG_MODE0;
2012                                 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2013                                         cto->ct_flags |= CT2_DATA_IN;
2014                                 } else {
2015                                         cto->ct_flags |= CT2_DATA_OUT;
2016                                 }
2017
2018                                 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
2019                                 cto->rsp.m0.ct_xfrlen = xfrlen;
2020
2021                                 if (sendstatus) {
2022                                         resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
2023                                         if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
2024                                                 cto->ct_flags |= CT2_SENDSTATUS;
2025                                                 atp->state = ATPD_STATE_LAST_CTIO;
2026                                                 if (fctape) {
2027                                                         cto->ct_flags |= CT2_CONFIRM;
2028                                                 }
2029                                         } else {
2030                                                 atp->sendst = 1;        /* send status later */
2031                                                 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
2032                                                 atp->state = ATPD_STATE_CTIO;
2033                                         }
2034                                 } else {
2035                                         atp->state = ATPD_STATE_CTIO;
2036                                 }
2037                         }
2038                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
2039                             ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
2040                 } else {
2041                         ct_entry_t *cto = (ct_entry_t *) local;
2042
2043                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
2044                         cto->ct_header.rqs_entry_count = 1;
2045                         cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
2046                         ATPD_SET_SEQNO(cto, atp);
2047                         cto->ct_iid = cso->init_id;
2048                         cto->ct_iid |= XS_CHANNEL(ccb) << 7;
2049                         cto->ct_tgt = ccb->ccb_h.target_id;
2050                         cto->ct_lun = ccb->ccb_h.target_lun;
2051                         cto->ct_fwhandle = cso->tag_id;
2052                         if (atp->rxid) {
2053                                 cto->ct_tag_val = atp->rxid;
2054                                 cto->ct_flags |= CT_TQAE;
2055                         }
2056                         if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2057                                 cto->ct_flags |= CT_NODISC;
2058                         }
2059                         if (cso->dxfer_len == 0) {
2060                                 cto->ct_flags |= CT_NO_DATA;
2061                         } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2062                                 cto->ct_flags |= CT_DATA_IN;
2063                         } else {
2064                                 cto->ct_flags |= CT_DATA_OUT;
2065                         }
2066                         if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
2067                                 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
2068                                 cto->ct_scsi_status = cso->scsi_status;
2069                                 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit - xfrlen;
2070                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d scsi status %x resid %d tag_id %x", __func__,
2071                                     cto->ct_fwhandle, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), cso->scsi_status, cso->resid, cso->tag_id);
2072                         }
2073                         ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
2074                         cto->ct_timeout = 10;
2075                 }
2076
2077                 if (isp_get_pcmd(isp, ccb)) {
2078                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
2079                         TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
2080                         break;
2081                 }
2082                 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
2083                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
2084                         TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
2085                         isp_free_pcmd(isp, ccb);
2086                         break;
2087                 }
2088                 atp->bytes_in_transit += xfrlen;
2089                 PISP_PCMD(ccb)->datalen = xfrlen;
2090
2091
2092                 /*
2093                  * Call the dma setup routines for this entry (and any subsequent
2094                  * CTIOs) if there's data to move, and then tell the f/w it's got
2095                  * new things to play with. As with isp_start's usage of DMA setup,
2096                  * any swizzling is done in the machine dependent layer. Because
2097                  * of this, we put the request onto the queue area first in native
2098                  * format.
2099                  */
2100
2101                 if (IS_24XX(isp)) {
2102                         ct7_entry_t *cto = (ct7_entry_t *) local;
2103                         cto->ct_syshandle = handle;
2104                 } else if (IS_FC(isp)) {
2105                         ct2_entry_t *cto = (ct2_entry_t *) local;
2106                         cto->ct_syshandle = handle;
2107                 } else {
2108                         ct_entry_t *cto = (ct_entry_t *) local;
2109                         cto->ct_syshandle = handle;
2110                 }
2111
2112                 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
2113                 if (dmaresult != CMD_QUEUED) {
2114                         isp_destroy_tgt_handle(isp, handle);
2115                         isp_free_pcmd(isp, ccb);
2116                         if (dmaresult == CMD_EAGAIN) {
2117                                 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe); 
2118                                 break;
2119                         }
2120                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2121                         xpt_done(ccb);
2122                         continue;
2123                 }
2124                 isp->isp_nactive++;
2125                 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2126                 if (xfrlen) {
2127                         ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
2128                 } else {
2129                         ccb->ccb_h.spriv_field0 = ~0;
2130                 }
2131                 atp->ctcnt++;
2132                 atp->seqno++;
2133         }
2134         rls_lun_statep(isp, tptr);
2135 }
2136
2137 static void
2138 isp_refire_putback_atio(void *arg)
2139 {
2140         union ccb *ccb = arg;
2141
2142         ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
2143         isp_target_putback_atio(ccb);
2144 }
2145
2146 static void
2147 isp_refire_notify_ack(void *arg)
2148 {
2149         isp_tna_t *tp  = arg;
2150         ispsoftc_t *isp = tp->isp;
2151
2152         ISP_ASSERT_LOCKED(isp);
2153         if (isp_notify_ack(isp, tp->not)) {
2154                 callout_schedule(&tp->timer, 5);
2155         } else {
2156                 free(tp, M_DEVBUF);
2157         }
2158 }
2159
2160
2161 static void
2162 isp_target_putback_atio(union ccb *ccb)
2163 {
2164         ispsoftc_t *isp;
2165         struct ccb_scsiio *cso;
2166         void *qe;
2167
2168         isp = XS_ISP(ccb);
2169
2170         qe = isp_getrqentry(isp);
2171         if (qe == NULL) {
2172                 xpt_print(ccb->ccb_h.path,
2173                     "%s: Request Queue Overflow\n", __func__);
2174                 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
2175                     isp_refire_putback_atio, ccb);
2176                 return;
2177         }
2178         memset(qe, 0, QENTRY_LEN);
2179         cso = &ccb->csio;
2180         if (IS_FC(isp)) {
2181                 at2_entry_t local, *at = &local;
2182                 ISP_MEMZERO(at, sizeof (at2_entry_t));
2183                 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
2184                 at->at_header.rqs_entry_count = 1;
2185                 if (ISP_CAP_SCCFW(isp)) {
2186                         at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
2187 #if __FreeBSD_version < 1000700
2188                         if (at->at_scclun >= 256)
2189                                 at->at_scclun |= 0x4000;
2190 #endif
2191                 } else {
2192                         at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
2193                 }
2194                 at->at_status = CT_OK;
2195                 at->at_rxid = cso->tag_id;
2196                 at->at_iid = cso->ccb_h.target_id;
2197                 isp_put_atio2(isp, at, qe);
2198         } else {
2199                 at_entry_t local, *at = &local;
2200                 ISP_MEMZERO(at, sizeof (at_entry_t));
2201                 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
2202                 at->at_header.rqs_entry_count = 1;
2203                 at->at_iid = cso->init_id;
2204                 at->at_iid |= XS_CHANNEL(ccb) << 7;
2205                 at->at_tgt = cso->ccb_h.target_id;
2206                 at->at_lun = cso->ccb_h.target_lun;
2207                 at->at_status = CT_OK;
2208                 at->at_tag_val = AT_GET_TAG(cso->tag_id);
2209                 at->at_handle = AT_GET_HANDLE(cso->tag_id);
2210                 isp_put_atio(isp, at, qe);
2211         }
2212         ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
2213         ISP_SYNC_REQUEST(isp);
2214         isp_complete_ctio(ccb);
2215 }
2216
2217 static void
2218 isp_complete_ctio(union ccb *ccb)
2219 {
2220         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2221                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2222                 xpt_done(ccb);
2223         }
2224 }
2225
2226 /*
2227  * Handle ATIO stuff that the generic code can't.
2228  * This means handling CDBs.
2229  */
2230
2231 static void
2232 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
2233 {
2234         tstate_t *tptr;
2235         int status, bus;
2236         struct ccb_accept_tio *atiop;
2237         atio_private_data_t *atp;
2238
2239         /*
2240          * The firmware status (except for the QLTM_SVALID bit)
2241          * indicates why this ATIO was sent to us.
2242          *
2243          * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2244          *
2245          * If the DISCONNECTS DISABLED bit is set in the flags field,
2246          * we're still connected on the SCSI bus.
2247          */
2248         status = aep->at_status;
2249         if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
2250                 /*
2251                  * Bus Phase Sequence error. We should have sense data
2252                  * suggested by the f/w. I'm not sure quite yet what
2253                  * to do about this for CAM.
2254                  */
2255                 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
2256                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2257                 return;
2258         }
2259         if ((status & ~QLTM_SVALID) != AT_CDB) {
2260                 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
2261                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2262                 return;
2263         }
2264
2265         bus = GET_BUS_VAL(aep->at_iid);
2266         tptr = get_lun_statep(isp, bus, aep->at_lun);
2267         if (tptr == NULL) {
2268                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2269                 if (tptr == NULL) {
2270                         /*
2271                          * Because we can't autofeed sense data back with
2272                          * a command for parallel SCSI, we can't give back
2273                          * a CHECK CONDITION. We'll give back a BUSY status
2274                          * instead. This works out okay because the only
2275                          * time we should, in fact, get this, is in the
2276                          * case that somebody configured us without the
2277                          * blackhole driver, so they get what they deserve.
2278                          */
2279                         isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2280                         return;
2281                 }
2282         }
2283
2284         atp = isp_get_atpd(isp, tptr, aep->at_handle);
2285         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2286         if (atiop == NULL || atp == NULL) {
2287                 /*
2288                  * Because we can't autofeed sense data back with
2289                  * a command for parallel SCSI, we can't give back
2290                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
2291                  * instead. This works out okay because the only time we
2292                  * should, in fact, get this, is in the case that we've
2293                  * run out of ATIOS.
2294                  */
2295                 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
2296                     ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
2297                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2298                 if (atp) {
2299                         isp_put_atpd(isp, tptr, atp);
2300                 }
2301                 rls_lun_statep(isp, tptr);
2302                 return;
2303         }
2304         atp->rxid = aep->at_tag_val;
2305         atp->state = ATPD_STATE_ATIO;
2306         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2307         tptr->atio_count--;
2308         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2309         atiop->ccb_h.target_id = aep->at_tgt;
2310         atiop->ccb_h.target_lun = aep->at_lun;
2311         if (aep->at_flags & AT_NODISC) {
2312                 atiop->ccb_h.flags |= CAM_DIS_DISCONNECT;
2313         } else {
2314                 atiop->ccb_h.flags &= ~CAM_DIS_DISCONNECT;
2315         }
2316
2317         if (status & QLTM_SVALID) {
2318                 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
2319                 atiop->sense_len = amt;
2320                 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
2321         } else {
2322                 atiop->sense_len = 0;
2323         }
2324
2325         atiop->init_id = GET_IID_VAL(aep->at_iid);
2326         atiop->cdb_len = aep->at_cdblen;
2327         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2328         atiop->ccb_h.status = CAM_CDB_RECVD;
2329         /*
2330          * Construct a tag 'id' based upon tag value (which may be 0..255)
2331          * and the handle (which we have to preserve).
2332          */
2333         atiop->tag_id = atp->tag;
2334         if (aep->at_flags & AT_TQAE) {
2335                 atiop->tag_action = aep->at_tag_type;
2336                 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2337         }
2338         atp->orig_datalen = 0;
2339         atp->bytes_xfered = 0;
2340         atp->lun = aep->at_lun;
2341         atp->nphdl = aep->at_iid;
2342         atp->portid = PORT_NONE;
2343         atp->oxid = 0;
2344         atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2345         atp->tattr = aep->at_tag_type;
2346         atp->state = ATPD_STATE_CAM;
2347         isp_prt(isp, ISP_LOGTDEBUG0, "ATIO[0x%x] CDB=0x%x lun %d", aep->at_tag_val, atp->cdb0, atp->lun);
2348         rls_lun_statep(isp, tptr);
2349 }
2350
2351 static void
2352 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2353 {
2354         lun_id_t lun;
2355         fcportdb_t *lp;
2356         tstate_t *tptr;
2357         struct ccb_accept_tio *atiop;
2358         uint16_t nphdl;
2359         atio_private_data_t *atp;
2360         inot_private_data_t *ntp;
2361
2362         /*
2363          * The firmware status (except for the QLTM_SVALID bit)
2364          * indicates why this ATIO was sent to us.
2365          *
2366          * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2367          */
2368         if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
2369                 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2370                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2371                 return;
2372         }
2373
2374         if (ISP_CAP_SCCFW(isp)) {
2375                 lun = aep->at_scclun;
2376 #if __FreeBSD_version < 1000700
2377                 lun &= 0x3fff;
2378 #endif
2379         } else {
2380                 lun = aep->at_lun;
2381         }
2382         if (ISP_CAP_2KLOGIN(isp)) {
2383                 nphdl = ((at2e_entry_t *)aep)->at_iid;
2384         } else {
2385                 nphdl = aep->at_iid;
2386         }
2387         tptr = get_lun_statep(isp, 0, lun);
2388         if (tptr == NULL) {
2389                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2390                 if (tptr == NULL) {
2391                         isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2392                         if (lun == 0) {
2393                                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2394                         } else {
2395                                 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2396                         }
2397                         return;
2398                 }
2399         }
2400
2401         /*
2402          * Start any commands pending resources first.
2403          */
2404         if (tptr->restart_queue) {
2405                 inot_private_data_t *restart_queue = tptr->restart_queue;
2406                 tptr->restart_queue = NULL;
2407                 while (restart_queue) {
2408                         ntp = restart_queue;
2409                         restart_queue = ntp->rd.nt.nt_hba;
2410                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2411                         isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2412                         isp_put_ntpd(isp, tptr, ntp);
2413                         /*
2414                          * If a recursion caused the restart queue to start to fill again,
2415                          * stop and splice the new list on top of the old list and restore
2416                          * it and go to noresrc.
2417                          */
2418                         if (tptr->restart_queue) {
2419                                 ntp = tptr->restart_queue;
2420                                 tptr->restart_queue = restart_queue;
2421                                 while (restart_queue->rd.nt.nt_hba) {
2422                                         restart_queue = restart_queue->rd.nt.nt_hba;
2423                                 }
2424                                 restart_queue->rd.nt.nt_hba = ntp;
2425                                 goto noresrc;
2426                         }
2427                 }
2428         }
2429
2430         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2431         if (atiop == NULL) {
2432                 goto noresrc;
2433         }
2434
2435         atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2436         if (atp == NULL) {
2437                 goto noresrc;
2438         }
2439
2440         atp->state = ATPD_STATE_ATIO;
2441         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2442         tptr->atio_count--;
2443         isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
2444         atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2445         atiop->ccb_h.target_lun = lun;
2446
2447         /*
2448          * We don't get 'suggested' sense data as we do with SCSI cards.
2449          */
2450         atiop->sense_len = 0;
2451
2452         /*
2453          * If we're not in the port database, add ourselves.
2454          */
2455         if (IS_2100(isp))
2456                 atiop->init_id = nphdl;
2457         else {
2458                 if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
2459                      lp->state == FC_PORTDB_STATE_ZOMBIE)) {
2460                         uint64_t wwpn =
2461                                 (((uint64_t) aep->at_wwpn[0]) << 48) |
2462                                 (((uint64_t) aep->at_wwpn[1]) << 32) |
2463                                 (((uint64_t) aep->at_wwpn[2]) << 16) |
2464                                 (((uint64_t) aep->at_wwpn[3]) <<  0);
2465                         isp_add_wwn_entry(isp, 0, wwpn, INI_NONE,
2466                             nphdl, PORT_ANY, 0);
2467                         isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
2468                 }
2469                 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
2470         }
2471         atiop->cdb_len = ATIO2_CDBLEN;
2472         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2473         atiop->ccb_h.status = CAM_CDB_RECVD;
2474         atiop->tag_id = atp->tag;
2475         switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2476         case ATIO2_TC_ATTR_SIMPLEQ:
2477                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2478                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2479                 break;
2480         case ATIO2_TC_ATTR_HEADOFQ:
2481                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2482                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2483                 break;
2484         case ATIO2_TC_ATTR_ORDERED:
2485                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2486                 atiop->tag_action = MSG_ORDERED_Q_TAG;
2487                 break;
2488         case ATIO2_TC_ATTR_ACAQ:                /* ?? */
2489         case ATIO2_TC_ATTR_UNTAGGED:
2490         default:
2491                 atiop->tag_action = 0;
2492                 break;
2493         }
2494
2495         atp->orig_datalen = aep->at_datalen;
2496         atp->bytes_xfered = 0;
2497         atp->lun = lun;
2498         atp->nphdl = nphdl;
2499         atp->sid = PORT_ANY;
2500         atp->oxid = aep->at_oxid;
2501         atp->cdb0 = aep->at_cdb[0];
2502         atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2503         atp->state = ATPD_STATE_CAM;
2504         xpt_done((union ccb *)atiop);
2505         isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2506         rls_lun_statep(isp, tptr);
2507         return;
2508 noresrc:
2509         ntp = isp_get_ntpd(isp, tptr);
2510         if (ntp == NULL) {
2511                 rls_lun_statep(isp, tptr);
2512                 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2513                 return;
2514         }
2515         memcpy(ntp->rd.data, aep, QENTRY_LEN);
2516         ntp->rd.nt.nt_hba = tptr->restart_queue;
2517         tptr->restart_queue = ntp;
2518         rls_lun_statep(isp, tptr);
2519 }
2520
2521 static void
2522 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2523 {
2524         int cdbxlen;
2525         lun_id_t lun;
2526         uint16_t chan, nphdl = NIL_HANDLE;
2527         uint32_t did, sid;
2528         fcportdb_t *lp;
2529         tstate_t *tptr;
2530         struct ccb_accept_tio *atiop;
2531         atio_private_data_t *atp = NULL;
2532         atio_private_data_t *oatp;
2533         inot_private_data_t *ntp;
2534
2535         did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2536         sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2537 #if __FreeBSD_version >= 1000700
2538         lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
2539 #else
2540         lun = (aep->at_cmnd.fcp_cmnd_lun[0] & 0x3f << 8) |
2541             aep->at_cmnd.fcp_cmnd_lun[1];
2542 #endif
2543
2544         /*
2545          * Find the N-port handle, and Virtual Port Index for this command.
2546          *
2547          * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2548          * We also, as a matter of course, need to know the WWN of the initiator too.
2549          */
2550         if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
2551                 /*
2552                  * Find the right channel based upon D_ID
2553                  */
2554                 isp_find_chan_by_did(isp, did, &chan);
2555
2556                 if (chan == ISP_NOCHAN) {
2557                         NANOTIME_T now;
2558
2559                         /*
2560                          * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2561                          * It's a bit tricky here as we need to stash this command *somewhere*.
2562                          */
2563                         GET_NANOTIME(&now);
2564                         if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2565                                 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2566                                 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2567                                 return;
2568                         }
2569                         tptr = get_lun_statep(isp, 0, 0);
2570                         if (tptr == NULL) {
2571                                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2572                                 if (tptr == NULL) {
2573                                         isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2574                                         isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2575                                         return;
2576                                 }
2577                         }
2578                         isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2579                         goto noresrc;
2580                 }
2581                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2582         } else {
2583                 chan = 0;
2584         }
2585
2586         /*
2587          * Find the PDB entry for this initiator
2588          */
2589         if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2590                 /*
2591                  * If we're not in the port database terminate the exchange.
2592                  */
2593                 isp_prt(isp, ISP_LOGTINFO, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x wasn't in PDB already",
2594                     __func__, aep->at_rxid, did, chan, sid);
2595                 isp_dump_portdb(isp, chan);
2596                 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2597                 return;
2598         }
2599         nphdl = lp->handle;
2600
2601         /*
2602          * Get the tstate pointer
2603          */
2604         tptr = get_lun_statep(isp, chan, lun);
2605         if (tptr == NULL) {
2606                 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2607                 if (tptr == NULL) {
2608                         isp_prt(isp, ISP_LOGWARN,
2609                             "%s: [0x%x] no state pointer for lun %jx or wildcard",
2610                             __func__, aep->at_rxid, (uintmax_t)lun);
2611                         if (lun == 0) {
2612                                 isp_endcmd(isp, aep, nphdl, SCSI_STATUS_BUSY, 0);
2613                         } else {
2614                                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2615                         }
2616                         return;
2617                 }
2618         }
2619
2620         /*
2621          * Start any commands pending resources first.
2622          */
2623         if (tptr->restart_queue) {
2624                 inot_private_data_t *restart_queue = tptr->restart_queue;
2625                 tptr->restart_queue = NULL;
2626                 while (restart_queue) {
2627                         ntp = restart_queue;
2628                         restart_queue = ntp->rd.nt.nt_hba;
2629                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2630                         isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2631                         isp_put_ntpd(isp, tptr, ntp);
2632                         /*
2633                          * If a recursion caused the restart queue to start to fill again,
2634                          * stop and splice the new list on top of the old list and restore
2635                          * it and go to noresrc.
2636                          */
2637                         if (tptr->restart_queue) {
2638                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
2639                                 if (restart_queue) {
2640                                         ntp = tptr->restart_queue;
2641                                         tptr->restart_queue = restart_queue;
2642                                         while (restart_queue->rd.nt.nt_hba) {
2643                                                 restart_queue = restart_queue->rd.nt.nt_hba;
2644                                         }
2645                                         restart_queue->rd.nt.nt_hba = ntp;
2646                                 }
2647                                 goto noresrc;
2648                         }
2649                 }
2650         }
2651
2652         /*
2653          * If the f/w is out of resources, just send a BUSY status back.
2654          */
2655         if (aep->at_rxid == AT7_NORESRC_RXID) {
2656                 rls_lun_statep(isp, tptr);
2657                 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2658                 return;
2659         }
2660
2661         /*
2662          * If we're out of resources, just send a BUSY status back.
2663          */
2664         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2665         if (atiop == NULL) {
2666                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2667                 goto noresrc;
2668         }
2669
2670         oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
2671         if (oatp) {
2672                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
2673                     aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2674                 /*
2675                  * It's not a "no resource" condition- but we can treat it like one
2676                  */
2677                 goto noresrc;
2678         }
2679         atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2680         if (atp == NULL) {
2681                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2682                 goto noresrc;
2683         }
2684         atp->word3 = lp->prli_word3;
2685         atp->state = ATPD_STATE_ATIO;
2686         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2687         tptr->atio_count--;
2688         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2689         atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
2690         atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2691         atiop->ccb_h.target_lun = lun;
2692         atiop->sense_len = 0;
2693         cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2694         if (cdbxlen) {
2695                 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2696         }
2697         cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2698         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2699         atiop->cdb_len = cdbxlen;
2700         atiop->ccb_h.status = CAM_CDB_RECVD;
2701         atiop->tag_id = atp->tag;
2702         switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2703         case FCP_CMND_TASK_ATTR_SIMPLE:
2704                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2705                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2706                 break;
2707         case FCP_CMND_TASK_ATTR_HEAD:
2708                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2709                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2710                 break;
2711         case FCP_CMND_TASK_ATTR_ORDERED:
2712                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2713                 atiop->tag_action = MSG_ORDERED_Q_TAG;
2714                 break;
2715         default:
2716                 /* FALLTHROUGH */
2717         case FCP_CMND_TASK_ATTR_ACA:
2718         case FCP_CMND_TASK_ATTR_UNTAGGED:
2719                 atiop->tag_action = 0;
2720                 break;
2721         }
2722         atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2723         atp->bytes_xfered = 0;
2724         atp->lun = lun;
2725         atp->nphdl = nphdl;
2726         atp->portid = sid;
2727         atp->oxid = aep->at_hdr.ox_id;
2728         atp->rxid = aep->at_hdr.rx_id;
2729         atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2730         atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2731         atp->state = ATPD_STATE_CAM;
2732         isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
2733             aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
2734         xpt_done((union ccb *)atiop);
2735         rls_lun_statep(isp, tptr);
2736         return;
2737 noresrc:
2738         if (atp) {
2739                 isp_put_atpd(isp, tptr, atp);
2740         }
2741         ntp = isp_get_ntpd(isp, tptr);
2742         if (ntp == NULL) {
2743                 rls_lun_statep(isp, tptr);
2744                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2745                 return;
2746         }
2747         memcpy(ntp->rd.data, aep, QENTRY_LEN);
2748         ntp->rd.nt.nt_hba = tptr->restart_queue;
2749         tptr->restart_queue = ntp;
2750         rls_lun_statep(isp, tptr);
2751 }
2752
2753
2754 /*
2755  * Handle starting an SRR (sequence retransmit request)
2756  * We get here when we've gotten the immediate notify
2757  * and the return of all outstanding CTIOs for this
2758  * transaction.
2759  */
2760 static void
2761 isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2762 {
2763         in_fcentry_24xx_t *inot;
2764         uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2765         union ccb *ccb;
2766
2767         inot = (in_fcentry_24xx_t *)atp->srr;
2768         srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2769         ccb = atp->srr_ccb;
2770         atp->srr_ccb = NULL;
2771         atp->nsrr++;
2772         if (ccb == NULL) {
2773                 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2774                 goto fail;
2775         }
2776
2777         ccb_off = ccb->ccb_h.spriv_field0;
2778         ccb_len = ccb->csio.dxfer_len;
2779         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2780
2781         switch (inot->in_srr_iu) {
2782         case R_CTL_INFO_SOLICITED_DATA:
2783                 /*
2784                  * We have to restart a FCP_DATA data out transaction
2785                  */
2786                 atp->sendst = 0;
2787                 atp->bytes_xfered = srr_off;
2788                 if (ccb_len == 0) {
2789                         isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2790                         goto mdp;
2791                 }
2792                 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
2793                         isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2794                         goto mdp;
2795                 }
2796                 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2797                 break;
2798         case R_CTL_INFO_COMMAND_STATUS:
2799                 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2800                 atp->sendst = 1;
2801                 /*
2802                  * We have to restart a FCP_RSP IU transaction
2803                  */
2804                 break;
2805         case R_CTL_INFO_DATA_DESCRIPTOR:
2806                 /*
2807                  * We have to restart an FCP DATA in transaction
2808                  */
2809                 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2810                 goto fail;
2811                 
2812         default:
2813                 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2814                 goto fail;
2815         }
2816
2817         /*
2818          * We can't do anything until this is acked, so we might as well start it now.
2819          * We aren't going to do the usual asynchronous ack issue because we need
2820          * to make sure this gets on the wire first.
2821          */
2822         if (isp_notify_ack(isp, inot)) {
2823                 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2824                 goto fail;
2825         }
2826         isp_target_start_ctio(isp, ccb, FROM_SRR);
2827         return;
2828 fail:
2829         inot->in_reserved = 1;
2830         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2831         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2832         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2833         isp_complete_ctio(ccb);
2834         return;
2835 mdp:
2836         if (isp_notify_ack(isp, inot)) {
2837                 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2838                 goto fail;
2839         }
2840         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2841         ccb->ccb_h.status = CAM_MESSAGE_RECV;
2842         /*
2843          * This is not a strict interpretation of MDP, but it's close
2844          */
2845         ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2846         ccb->csio.msg_len = 7;
2847         ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2848         ccb->csio.msg_ptr[1] = 5;
2849         ccb->csio.msg_ptr[2] = 0;       /* modify data pointer */
2850         ccb->csio.msg_ptr[3] = srr_off >> 24;
2851         ccb->csio.msg_ptr[4] = srr_off >> 16;
2852         ccb->csio.msg_ptr[5] = srr_off >> 8;
2853         ccb->csio.msg_ptr[6] = srr_off;
2854         isp_complete_ctio(ccb);
2855 }
2856
2857
2858 static void
2859 isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2860 {
2861         tstate_t *tptr;
2862         in_fcentry_24xx_t *inot = inot_raw;
2863         atio_private_data_t *atp;
2864         uint32_t tag = inot->in_rxid;
2865         uint32_t bus = inot->in_vpidx;
2866
2867         if (!IS_24XX(isp)) {
2868                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2869                 return;
2870         }
2871
2872         tptr = get_lun_statep_from_tag(isp, bus, tag);
2873         if (tptr == NULL) {
2874                 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2875                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2876                 return;
2877         }
2878         atp = isp_find_atpd(isp, tptr, tag);
2879         if (atp == NULL) {
2880                 rls_lun_statep(isp, tptr);
2881                 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2882                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2883                 return;
2884         }
2885         atp->srr_notify_rcvd = 1;
2886         memcpy(atp->srr, inot, sizeof (atp->srr));
2887         isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2888             inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2889         if (atp->srr_ccb)
2890                 isp_handle_srr_start(isp, tptr, atp);
2891         rls_lun_statep(isp, tptr);
2892 }
2893
2894 static void
2895 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2896 {
2897         union ccb *ccb;
2898         int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
2899         tstate_t *tptr = NULL;
2900         atio_private_data_t *atp = NULL;
2901         int bus;
2902         uint32_t handle, moved_data = 0, data_requested;
2903
2904         /*
2905          * CTIO handles are 16 bits.
2906          * CTIO2 and CTIO7 are 32 bits.
2907          */
2908
2909         if (IS_SCSI(isp)) {
2910                 handle = ((ct_entry_t *)arg)->ct_syshandle;
2911         } else {
2912                 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2913         }
2914         ccb = isp_find_xs_tgt(isp, handle);
2915         if (ccb == NULL) {
2916                 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2917                 return;
2918         }
2919         isp_destroy_tgt_handle(isp, handle);
2920         data_requested = PISP_PCMD(ccb)->datalen;
2921         isp_free_pcmd(isp, ccb);
2922         if (isp->isp_nactive) {
2923                 isp->isp_nactive--;
2924         }
2925
2926         bus = XS_CHANNEL(ccb);
2927         tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2928         if (tptr == NULL) {
2929                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2930         }
2931         if (tptr == NULL) {
2932                 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2933                 return;
2934         }
2935
2936         if (IS_24XX(isp)) {
2937                 atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2938         } else if (IS_FC(isp)) {
2939                 atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2940         } else {
2941                 atp = isp_find_atpd(isp, tptr, ((ct_entry_t *)arg)->ct_fwhandle);
2942         }
2943         if (atp == NULL) {
2944                 /*
2945                  * XXX: isp_clear_commands() generates fake CTIO with zero
2946                  * ct_rxid value, filling only ct_syshandle.  Workaround
2947                  * that using tag_id from the CCB, pointed by ct_syshandle.
2948                  */
2949                 atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2950         }
2951         if (atp == NULL) {
2952                 rls_lun_statep(isp, tptr);
2953                 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2954                 return;
2955         }
2956         KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2957         atp->bytes_in_transit -= data_requested;
2958         atp->ctcnt -= 1;
2959         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2960
2961         if (IS_24XX(isp)) {
2962                 ct7_entry_t *ct = arg;
2963
2964                 if (ct->ct_nphdl == CT7_SRR) {
2965                         atp->srr_ccb = ccb;
2966                         if (atp->srr_notify_rcvd)
2967                                 isp_handle_srr_start(isp, tptr, atp);
2968                         rls_lun_statep(isp, tptr);
2969                         return;
2970                 }
2971                 if (ct->ct_nphdl == CT_HBA_RESET) {
2972                         failure = CAM_UNREC_HBA_ERROR;
2973                 } else {
2974                         sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2975                         ok = (ct->ct_nphdl == CT7_OK);
2976                         notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2977                         if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2978                                 resid = ct->ct_resid;
2979                                 moved_data = data_requested - resid;
2980                         }
2981                 }
2982                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2983                    notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2984         } else if (IS_FC(isp)) {
2985                 ct2_entry_t *ct = arg;
2986                 if (ct->ct_status == CT_SRR) {
2987                         atp->srr_ccb = ccb;
2988                         if (atp->srr_notify_rcvd)
2989                                 isp_handle_srr_start(isp, tptr, atp);
2990                         rls_lun_statep(isp, tptr);
2991                         isp_target_putback_atio(ccb);
2992                         return;
2993                 }
2994                 if (ct->ct_status == CT_HBA_RESET) {
2995                         failure = CAM_UNREC_HBA_ERROR;
2996                 } else {
2997                         sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2998                         ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2999                         notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3000                         if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
3001                                 resid = ct->ct_resid;
3002                                 moved_data = data_requested - resid;
3003                         }
3004                 }
3005                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
3006                     notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
3007         } else {
3008                 ct_entry_t *ct = arg;
3009
3010                 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
3011                         failure = CAM_UNREC_HBA_ERROR;
3012                 } else {
3013                         sentstatus = ct->ct_flags & CT_SENDSTATUS;
3014                         ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
3015                         notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3016                 }
3017                 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
3018                         resid = ct->ct_resid;
3019                         moved_data = data_requested - resid;
3020                 }
3021                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d tag %x S_ID 0x%x lun %x sts %x flg %x resid %d %s", __func__, ct->ct_fwhandle, ATPD_GET_SEQNO(ct),
3022                     notify_cam, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
3023         }
3024         if (ok) {
3025                 if (moved_data) {
3026                         atp->bytes_xfered += moved_data;
3027                         ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
3028                 }
3029                 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
3030                         ccb->ccb_h.status |= CAM_SENT_SENSE;
3031                 }
3032                 ccb->ccb_h.status |= CAM_REQ_CMP;
3033         } else {
3034                 notify_cam = 1;
3035                 if (failure == CAM_UNREC_HBA_ERROR)
3036                         ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
3037                 else
3038                         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
3039         }
3040         atp->state = ATPD_STATE_PDON;
3041         rls_lun_statep(isp, tptr);
3042
3043         /*
3044          * We never *not* notify CAM when there has been any error (ok == 0),
3045          * so we never need to do an ATIO putback if we're not notifying CAM.
3046          */
3047         isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
3048             (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
3049         if (notify_cam == 0) {
3050                 if (atp->sendst) {
3051                         isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
3052                 }
3053                 return;
3054         }
3055
3056         /*
3057          * We're telling CAM we're done with this CTIO transaction.
3058          *
3059          * 24XX cards never need an ATIO put back.
3060          *
3061          * Other cards need one put back only on error.
3062          * In the latter case, a timeout will re-fire
3063          * and try again in case we didn't have
3064          * queue resources to do so at first. In any case,
3065          * once the putback is done we do the completion
3066          * call.
3067          */
3068         if (ok || IS_24XX(isp)) {
3069                 isp_complete_ctio(ccb);
3070         } else {
3071                 isp_target_putback_atio(ccb);
3072         }
3073 }
3074
3075 static void
3076 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
3077 {
3078         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3079 }
3080
3081 static void
3082 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
3083 {
3084         int needack = 1;
3085         switch (inp->in_status) {
3086         case IN_PORT_LOGOUT:
3087                 /*
3088                  * XXX: Need to delete this initiator's WWN from the database
3089                  * XXX: Need to send this LOGOUT upstream
3090                  */
3091                 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
3092                 break;
3093         case IN_PORT_CHANGED:
3094                 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
3095                 break;
3096         case IN_GLOBAL_LOGO:
3097                 isp_del_all_wwn_entries(isp, 0);
3098                 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
3099                 break;
3100         case IN_ABORT_TASK:
3101         {
3102                 tstate_t *tptr;
3103                 uint16_t lun;
3104                 uint32_t loopid, sid;
3105                 uint64_t wwn;
3106                 atio_private_data_t *atp;
3107                 fcportdb_t *lp;
3108                 struct ccb_immediate_notify *inot = NULL;
3109
3110                 if (ISP_CAP_SCCFW(isp)) {
3111                         lun = inp->in_scclun;
3112 #if __FreeBSD_version < 1000700
3113                         lun &= 0x3fff;
3114 #endif
3115                 } else {
3116                         lun = inp->in_lun;
3117                 }
3118                 if (ISP_CAP_2KLOGIN(isp)) {
3119                         loopid = ((in_fcentry_e_t *)inp)->in_iid;
3120                 } else {
3121                         loopid = inp->in_iid;
3122                 }
3123                 if (isp_find_pdb_by_handle(isp, 0, loopid, &lp)) {
3124                         wwn = lp->port_wwn;
3125                         sid = lp->portid;
3126                 } else {
3127                         wwn = INI_ANY;
3128                         sid = PORT_ANY;
3129                 }
3130                 tptr = get_lun_statep(isp, 0, lun);
3131                 if (tptr == NULL) {
3132                         tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
3133                         if (tptr == NULL) {
3134                                 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
3135                                 return;
3136                         }
3137                 }
3138                 atp = isp_find_atpd(isp, tptr, inp->in_seqid);
3139
3140                 if (atp) {
3141                         inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3142                         isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
3143                         if (inot) {
3144                                 tptr->inot_count--;
3145                                 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3146                                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3147                         } else {
3148                                 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
3149                         }
3150                 } else {
3151                         ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
3152                 }
3153                 if (inot) {
3154                         isp_notify_t tmp, *nt = &tmp;
3155                         ISP_MEMZERO(nt, sizeof (isp_notify_t));
3156                         nt->nt_hba = isp;
3157                         nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
3158                         nt->nt_wwn = wwn;
3159                         nt->nt_nphdl = loopid;
3160                         nt->nt_sid = sid;
3161                         nt->nt_did = PORT_ANY;
3162                         nt->nt_lun = lun;
3163                         nt->nt_need_ack = 1;
3164                         nt->nt_channel = 0;
3165                         nt->nt_ncode = NT_ABORT_TASK;
3166                         nt->nt_lreserved = inot;
3167                         isp_handle_platform_target_tmf(isp, nt);
3168                         needack = 0;
3169                 }
3170                 rls_lun_statep(isp, tptr);
3171                 break;
3172         }
3173         default:
3174                 break;
3175         }
3176         if (needack) {
3177                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
3178         }
3179 }
3180
3181 static void
3182 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
3183 {
3184         uint16_t nphdl;
3185         uint16_t prli_options = 0;
3186         uint32_t portid;
3187         fcportdb_t *lp;
3188         char *msg = NULL;
3189         uint8_t *ptr = (uint8_t *)inot;
3190         uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
3191
3192         nphdl = inot->in_nphdl;
3193         if (nphdl != NIL_HANDLE) {
3194                 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
3195         } else {
3196                 portid = PORT_ANY;
3197         }
3198
3199         switch (inot->in_status) {
3200         case IN24XX_ELS_RCVD:
3201         {
3202                 char buf[16];
3203                 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
3204
3205                 /*
3206                  * Note that we're just getting notification that an ELS was received
3207                  * (possibly with some associated information sent upstream). This is
3208                  * *not* the same as being given the ELS frame to accept or reject.
3209                  */
3210                 switch (inot->in_status_subcode) {
3211                 case LOGO:
3212                         msg = "LOGO";
3213                         wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
3214                         isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
3215                         break;
3216                 case PRLO:
3217                         msg = "PRLO";
3218                         break;
3219                 case PLOGI:
3220                         msg = "PLOGI";
3221                         wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
3222                         wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
3223                         isp_add_wwn_entry(isp, chan, wwpn, wwnn,
3224                             nphdl, portid, prli_options);
3225                         break;
3226                 case PRLI:
3227                         msg = "PRLI";
3228                         prli_options = inot->in_prli_options;
3229                         if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
3230                                 wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
3231                         wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
3232                         isp_add_wwn_entry(isp, chan, wwpn, wwnn,
3233                             nphdl, portid, prli_options);
3234                         break;
3235                 case PDISC:
3236                         msg = "PDISC";
3237                         break;
3238                 case ADISC:
3239                         msg = "ADISC";
3240                         break;
3241                 default:
3242                         ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
3243                         msg = buf;
3244                         break;
3245                 }
3246                 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
3247                         isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
3248                         break;
3249                 }
3250                 isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
3251                     inot->in_rxid, inot->in_oxid);
3252                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3253                 break;
3254         }
3255
3256         case IN24XX_PORT_LOGOUT:
3257                 msg = "PORT LOGOUT";
3258                 if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
3259                         isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
3260                 }
3261                 /* FALLTHROUGH */
3262         case IN24XX_PORT_CHANGED:
3263                 if (msg == NULL)
3264                         msg = "PORT CHANGED";
3265                 /* FALLTHROUGH */
3266         case IN24XX_LIP_RESET:
3267                 if (msg == NULL)
3268                         msg = "LIP RESET";
3269                 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), msg, inot->in_status_subcode, nphdl);
3270
3271                 /*
3272                  * All subcodes here are irrelevant. What is relevant
3273                  * is that we need to terminate all active commands from
3274                  * this initiator (known by N-port handle).
3275                  */
3276                 /* XXX IMPLEMENT XXX */
3277                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3278                 break;
3279
3280         case IN24XX_SRR_RCVD:
3281 #ifdef  ISP_TARGET_MODE
3282                 isp_handle_srr_notify(isp, inot);
3283                 break;
3284 #else
3285                 if (msg == NULL)
3286                         msg = "SRR RCVD";
3287                 /* FALLTHROUGH */
3288 #endif
3289         case IN24XX_LINK_RESET:
3290                 if (msg == NULL)
3291                         msg = "LINK RESET";
3292         case IN24XX_LINK_FAILED:
3293                 if (msg == NULL)
3294                         msg = "LINK FAILED";
3295         default:
3296                 isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), msg);
3297                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3298                 break;
3299         }
3300 }
3301
3302 static int
3303 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
3304 {
3305
3306         if (isp->isp_state != ISP_RUNSTATE) {
3307                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
3308                 return (0);
3309         }
3310
3311         /*
3312          * This case is for a Task Management Function, which shows up as an ATIO7 entry.
3313          */
3314         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
3315                 ct7_entry_t local, *cto = &local;
3316                 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
3317                 fcportdb_t *lp;
3318                 uint32_t sid;
3319                 uint16_t nphdl;
3320
3321                 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
3322                 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
3323                         nphdl = lp->handle;
3324                 } else {
3325                         nphdl = NIL_HANDLE;
3326                 }
3327                 ISP_MEMZERO(&local, sizeof (local));
3328                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3329                 cto->ct_header.rqs_entry_count = 1;
3330                 cto->ct_nphdl = nphdl;
3331                 cto->ct_rxid = aep->at_rxid;
3332                 cto->ct_vpidx = mp->nt_channel;
3333                 cto->ct_iid_lo = sid;
3334                 cto->ct_iid_hi = sid >> 16;
3335                 cto->ct_oxid = aep->at_hdr.ox_id;
3336                 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
3337                 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
3338                 return (isp_target_put_entry(isp, &local));
3339         }
3340
3341         /*
3342          * This case is for a responding to an ABTS frame
3343          */
3344         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3345
3346                 /*
3347                  * Overload nt_need_ack here to mark whether we've terminated the associated command.
3348                  */
3349                 if (mp->nt_need_ack) {
3350                         uint8_t storage[QENTRY_LEN];
3351                         ct7_entry_t *cto = (ct7_entry_t *) storage;
3352                         abts_t *abts = (abts_t *)mp->nt_lreserved;
3353
3354                         ISP_MEMZERO(cto, sizeof (ct7_entry_t));
3355                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
3356                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3357                         cto->ct_header.rqs_entry_count = 1;
3358                         cto->ct_nphdl = mp->nt_nphdl;
3359                         cto->ct_rxid = abts->abts_rxid_task;
3360                         cto->ct_iid_lo = mp->nt_sid;
3361                         cto->ct_iid_hi = mp->nt_sid >> 16;
3362                         cto->ct_oxid = abts->abts_ox_id;
3363                         cto->ct_vpidx = mp->nt_channel;
3364                         cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
3365                         if (isp_target_put_entry(isp, cto)) {
3366                                 return (ENOMEM);
3367                         }
3368                         mp->nt_need_ack = 0;
3369                 }
3370                 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
3371                         return (ENOMEM);
3372                 } else {
3373                         return (0);
3374                 }
3375         }
3376
3377         /*
3378          * Handle logout cases here
3379          */
3380         if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
3381                 isp_del_all_wwn_entries(isp, mp->nt_channel);
3382         }
3383
3384         if (mp->nt_ncode == NT_LOGOUT) {
3385                 if (!IS_2100(isp) && IS_FC(isp)) {
3386                         isp_del_wwn_entries(isp, mp);
3387                 }
3388         }
3389
3390         /*
3391          * General purpose acknowledgement
3392          */
3393         if (mp->nt_need_ack) {
3394                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
3395                 /*
3396                  * Don't need to use the guaranteed send because the caller can retry
3397                  */
3398                 return (isp_notify_ack(isp, mp->nt_lreserved));
3399         }
3400         return (0);
3401 }
3402
3403 /*
3404  * Handle task management functions.
3405  *
3406  * We show up here with a notify structure filled out.
3407  *
3408  * The nt_lreserved tag points to the original queue entry
3409  */
3410 static void
3411 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
3412 {
3413         tstate_t *tptr;
3414         fcportdb_t *lp;
3415         struct ccb_immediate_notify *inot;
3416         inot_private_data_t *ntp = NULL;
3417         lun_id_t lun;
3418
3419         isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
3420             notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
3421         /*
3422          * NB: This assignment is necessary because of tricky type conversion.
3423          * XXX: This is tricky and I need to check this. If the lun isn't known
3424          * XXX: for the task management function, it does not of necessity follow
3425          * XXX: that it should go up stream to the wildcard listener.
3426          */
3427         if (notify->nt_lun == LUN_ANY) {
3428                 lun = CAM_LUN_WILDCARD;
3429         } else {
3430                 lun = notify->nt_lun;
3431         }
3432         tptr = get_lun_statep(isp, notify->nt_channel, lun);
3433         if (tptr == NULL) {
3434                 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
3435                 if (tptr == NULL) {
3436                         isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3437                         goto bad;
3438                 }
3439         }
3440         inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3441         if (inot == NULL) {
3442                 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3443                 goto bad;
3444         }
3445
3446         if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
3447             isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
3448                 inot->initiator_id = CAM_TARGET_WILDCARD;
3449         } else {
3450                 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
3451         }
3452         inot->seq_id = notify->nt_tagval;
3453         inot->tag_id = notify->nt_tagval >> 32;
3454
3455         switch (notify->nt_ncode) {
3456         case NT_ABORT_TASK:
3457                 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
3458                 inot->arg = MSG_ABORT_TASK;
3459                 break;
3460         case NT_ABORT_TASK_SET:
3461                 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
3462                 inot->arg = MSG_ABORT_TASK_SET;
3463                 break;
3464         case NT_CLEAR_ACA:
3465                 inot->arg = MSG_CLEAR_ACA;
3466                 break;
3467         case NT_CLEAR_TASK_SET:
3468                 inot->arg = MSG_CLEAR_TASK_SET;
3469                 break;
3470         case NT_LUN_RESET:
3471                 inot->arg = MSG_LOGICAL_UNIT_RESET;
3472                 break;
3473         case NT_TARGET_RESET:
3474                 inot->arg = MSG_TARGET_RESET;
3475                 break;
3476         case NT_QUERY_TASK_SET:
3477                 inot->arg = MSG_QUERY_TASK_SET;
3478                 break;
3479         case NT_QUERY_ASYNC_EVENT:
3480                 inot->arg = MSG_QUERY_ASYNC_EVENT;
3481                 break;
3482         default:
3483                 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
3484                 goto bad;
3485         }
3486
3487         ntp = isp_get_ntpd(isp, tptr);
3488         if (ntp == NULL) {
3489                 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
3490                 goto bad;
3491         }
3492         ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
3493         if (notify->nt_lreserved) {
3494                 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
3495                 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
3496         }
3497         ntp->rd.seq_id = notify->nt_tagval;
3498         ntp->rd.tag_id = notify->nt_tagval >> 32;
3499
3500         tptr->inot_count--;
3501         SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3502         rls_lun_statep(isp, tptr);
3503         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3504         inot->ccb_h.status = CAM_MESSAGE_RECV;
3505         xpt_done((union ccb *)inot);
3506         return;
3507 bad:
3508         if (tptr) {
3509                 rls_lun_statep(isp, tptr);
3510         }
3511         if (notify->nt_need_ack && notify->nt_lreserved) {
3512                 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3513                         if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
3514                                 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
3515                         }
3516                 } else {
3517                         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
3518                 }
3519         }
3520 }
3521
3522 /*
3523  * Find the associated private data and mark it as dead so
3524  * we don't try to work on it any further.
3525  */
3526 static void
3527 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3528 {
3529         tstate_t *tptr;
3530         atio_private_data_t *atp;
3531         union ccb *accb = ccb->cab.abort_ccb;
3532
3533         tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3534         if (tptr == NULL) {
3535                 tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3536                 if (tptr == NULL) {
3537                         ccb->ccb_h.status = CAM_REQ_INVALID;
3538                         return;
3539                 }
3540         }
3541
3542         atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
3543         if (atp == NULL) {
3544                 ccb->ccb_h.status = CAM_REQ_INVALID;
3545         } else {
3546                 atp->dead = 1;
3547                 ccb->ccb_h.status = CAM_REQ_CMP;
3548         }
3549         rls_lun_statep(isp, tptr);
3550 }
3551
3552 static void
3553 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3554 {
3555         atio_private_data_t *atp;
3556         inot_private_data_t *restart_queue = tptr->restart_queue;
3557
3558         /*
3559          * First, clean any commands pending restart
3560          */
3561         tptr->restart_queue = NULL;
3562         while (restart_queue) {
3563                 uint32_t this_tag_id;
3564                 inot_private_data_t *ntp = restart_queue;
3565
3566                 restart_queue = ntp->rd.nt.nt_hba;
3567
3568                 if (IS_24XX(isp)) {
3569                         this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3570                 } else {
3571                         this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3572                 }
3573                 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3574                         isp_put_ntpd(isp, tptr, ntp);
3575                 } else {
3576                         ntp->rd.nt.nt_hba = tptr->restart_queue;
3577                         tptr->restart_queue = ntp;
3578                 }
3579         }
3580
3581         /*
3582          * Now mark other ones dead as well.
3583          */
3584         for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3585                 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3586                         atp->dead = 1;
3587                 }
3588         }
3589 }
3590 #endif
3591
3592 static void
3593 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3594 {
3595         struct cam_sim *sim;
3596         int bus, tgt;
3597         ispsoftc_t *isp;
3598
3599         sim = (struct cam_sim *)cbarg;
3600         isp = (ispsoftc_t *) cam_sim_softc(sim);
3601         bus = cam_sim_bus(sim);
3602         tgt = xpt_path_target_id(path);
3603
3604         switch (code) {
3605         case AC_LOST_DEVICE:
3606                 if (IS_SCSI(isp)) {
3607                         uint16_t oflags, nflags;
3608                         sdparam *sdp = SDPARAM(isp, bus);
3609
3610                         if (tgt >= 0) {
3611                                 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3612 #ifndef ISP_TARGET_MODE
3613                                 nflags &= DPARM_SAFE_DFLT;
3614                                 if (isp->isp_loaded_fw) {
3615                                         nflags |= DPARM_NARROW | DPARM_ASYNC;
3616                                 }
3617 #else
3618                                 nflags = DPARM_DEFAULT;
3619 #endif
3620                                 oflags = sdp->isp_devparam[tgt].goal_flags;
3621                                 sdp->isp_devparam[tgt].goal_flags = nflags;
3622                                 sdp->isp_devparam[tgt].dev_update = 1;
3623                                 sdp->update = 1;
3624                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3625                                 sdp->isp_devparam[tgt].goal_flags = oflags;
3626                         }
3627                 }
3628                 break;
3629         default:
3630                 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3631                 break;
3632         }
3633 }
3634
3635 static void
3636 isp_poll(struct cam_sim *sim)
3637 {
3638         ispsoftc_t *isp = cam_sim_softc(sim);
3639         uint16_t isr, sema, info;
3640
3641         if (ISP_READ_ISR(isp, &isr, &sema, &info))
3642                 isp_intr(isp, isr, sema, info);
3643 }
3644
3645
3646 static void
3647 isp_watchdog(void *arg)
3648 {
3649         struct ccb_scsiio *xs = arg;
3650         ispsoftc_t *isp;
3651         uint32_t ohandle = ISP_HANDLE_FREE, handle;
3652
3653         isp = XS_ISP(xs);
3654
3655         handle = isp_find_handle(isp, xs);
3656
3657         /*
3658          * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3659          */
3660         if (handle != ISP_HANDLE_FREE) {
3661                 uint16_t isr, sema, info;
3662                 if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
3663                         isp_intr(isp, isr, sema, info);
3664                 ohandle = handle;
3665                 handle = isp_find_handle(isp, xs);
3666         }
3667         if (handle != ISP_HANDLE_FREE) {
3668                 /*
3669                  * Try and make sure the command is really dead before
3670                  * we release the handle (and DMA resources) for reuse.
3671                  *
3672                  * If we are successful in aborting the command then
3673                  * we're done here because we'll get the command returned
3674                  * back separately.
3675                  */
3676                 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3677                         return;
3678                 }
3679
3680                 /*
3681                  * Note that after calling the above, the command may in
3682                  * fact have been completed.
3683                  */
3684                 xs = isp_find_xs(isp, handle);
3685
3686                 /*
3687                  * If the command no longer exists, then we won't
3688                  * be able to find the xs again with this handle.
3689                  */
3690                 if (xs == NULL) {
3691                         return;
3692                 }
3693
3694                 /*
3695                  * After this point, the command is really dead.
3696                  */
3697                 if (XS_XFRLEN(xs)) {
3698                         ISP_DMAFREE(isp, xs, handle);
3699                 } 
3700                 isp_destroy_handle(isp, handle);
3701                 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3702                 xs->ccb_h.status &= ~CAM_STATUS_MASK;
3703                 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
3704                 isp_prt_endcmd(isp, xs);
3705                 isp_done(xs);
3706         } else {
3707                 if (ohandle != ISP_HANDLE_FREE) {
3708                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3709                 } else {
3710                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3711                 }
3712         }
3713 }
3714
3715 static void
3716 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3717 {
3718         union ccb *ccb;
3719         struct isp_fc *fc = ISP_FC_PC(isp, chan);
3720
3721         /*
3722          * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3723          */
3724         ccb = xpt_alloc_ccb_nowait();
3725         if (ccb == NULL) {
3726                 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3727                 return;
3728         }
3729         if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
3730             tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3731                 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3732                 xpt_free_ccb(ccb);
3733                 return;
3734         }
3735         xpt_rescan(ccb);
3736 }
3737
3738 static void
3739 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
3740 {
3741         struct cam_path *tp;
3742         struct isp_fc *fc = ISP_FC_PC(isp, chan);
3743
3744         if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3745                 xpt_async(AC_LOST_DEVICE, tp, NULL);
3746                 xpt_free_path(tp);
3747         }
3748 }
3749
3750 /*
3751  * Gone Device Timer Function- when we have decided that a device has gone
3752  * away, we wait a specific period of time prior to telling the OS it has
3753  * gone away.
3754  *
3755  * This timer function fires once a second and then scans the port database
3756  * for devices that are marked dead but still have a virtual target assigned.
3757  * We decrement a counter for that port database entry, and when it hits zero,
3758  * we tell the OS the device has gone away.
3759  */
3760 static void
3761 isp_gdt(void *arg)
3762 {
3763         struct isp_fc *fc = arg;
3764         taskqueue_enqueue(taskqueue_thread, &fc->gtask);
3765 }
3766
3767 static void
3768 isp_gdt_task(void *arg, int pending)
3769 {
3770         struct isp_fc *fc = arg;
3771         ispsoftc_t *isp = fc->isp;
3772         int chan = fc - isp->isp_osinfo.pc.fc;
3773         fcportdb_t *lp;
3774         struct ac_contract ac;
3775         struct ac_device_changed *adc;
3776         int dbidx, more_to_do = 0;
3777
3778         ISP_LOCK(isp);
3779         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
3780         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3781                 lp = &FCPARAM(isp, chan)->portdb[dbidx];
3782
3783                 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
3784                         continue;
3785                 }
3786                 if (lp->gone_timer != 0) {
3787                         lp->gone_timer -= 1;
3788                         more_to_do++;
3789                         continue;
3790                 }
3791                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
3792                 if (lp->is_target) {
3793                         lp->is_target = 0;
3794                         isp_make_gone(isp, lp, chan, dbidx);
3795                 }
3796                 if (lp->is_initiator) {
3797                         lp->is_initiator = 0;
3798                         ac.contract_number = AC_CONTRACT_DEV_CHG;
3799                         adc = (struct ac_device_changed *) ac.contract_data;
3800                         adc->wwpn = lp->port_wwn;
3801                         adc->port = lp->portid;
3802                         adc->target = dbidx;
3803                         adc->arrived = 0;
3804                         xpt_async(AC_CONTRACT, fc->path, &ac);
3805                 }
3806                 lp->state = FC_PORTDB_STATE_NIL;
3807         }
3808         if (fc->ready) {
3809                 if (more_to_do) {
3810                         callout_reset(&fc->gdt, hz, isp_gdt, fc);
3811                 } else {
3812                         callout_deactivate(&fc->gdt);
3813                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
3814                 }
3815         }
3816         ISP_UNLOCK(isp);
3817 }
3818
3819 /*
3820  * Loop Down Timer Function- when loop goes down, a timer is started and
3821  * and after it expires we come here and take all probational devices that
3822  * the OS knows about and the tell the OS that they've gone away.
3823  * 
3824  * We don't clear the devices out of our port database because, when loop
3825  * come back up, we have to do some actual cleanup with the chip at that
3826  * point (implicit PLOGO, e.g., to get the chip's port database state right).
3827  */
3828 static void
3829 isp_ldt(void *arg)
3830 {
3831         struct isp_fc *fc = arg;
3832         taskqueue_enqueue(taskqueue_thread, &fc->ltask);
3833 }
3834
3835 static void
3836 isp_ldt_task(void *arg, int pending)
3837 {
3838         struct isp_fc *fc = arg;
3839         ispsoftc_t *isp = fc->isp;
3840         int chan = fc - isp->isp_osinfo.pc.fc;
3841         fcportdb_t *lp;
3842         struct ac_contract ac;
3843         struct ac_device_changed *adc;
3844         int dbidx, i;
3845
3846         ISP_LOCK(isp);
3847         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
3848         callout_deactivate(&fc->ldt);
3849
3850         /*
3851          * Notify to the OS all targets who we now consider have departed.
3852          */
3853         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3854                 lp = &FCPARAM(isp, chan)->portdb[dbidx];
3855
3856                 if (lp->state == FC_PORTDB_STATE_NIL)
3857                         continue;
3858
3859                 /*
3860                  * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
3861                  */
3862                 for (i = 0; i < isp->isp_maxcmds; i++) {
3863                         struct ccb_scsiio *xs;
3864
3865                         if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
3866                                 continue;
3867                         }
3868                         if ((xs = isp->isp_xflist[i].cmd) == NULL) {
3869                                 continue;
3870                         }
3871                         if (dbidx != XS_TGT(xs)) {
3872                                 continue;
3873                         }
3874                         isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
3875                             isp->isp_xflist[i].handle, chan, XS_TGT(xs),
3876                             (uintmax_t)XS_LUN(xs));
3877                 }
3878
3879                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
3880                 if (lp->is_target) {
3881                         lp->is_target = 0;
3882                         isp_make_gone(isp, lp, chan, dbidx);
3883                 }
3884                 if (lp->is_initiator) {
3885                         lp->is_initiator = 0;
3886                         ac.contract_number = AC_CONTRACT_DEV_CHG;
3887                         adc = (struct ac_device_changed *) ac.contract_data;
3888                         adc->wwpn = lp->port_wwn;
3889                         adc->port = lp->portid;
3890                         adc->target = dbidx;
3891                         adc->arrived = 0;
3892                         xpt_async(AC_CONTRACT, fc->path, &ac);
3893                 }
3894         }
3895
3896         isp_unfreeze_loopdown(isp, chan);
3897         /*
3898          * The loop down timer has expired. Wake up the kthread
3899          * to notice that fact (or make it false).
3900          */
3901         fc->loop_dead = 1;
3902         fc->loop_down_time = fc->loop_down_limit+1;
3903         wakeup(fc);
3904         ISP_UNLOCK(isp);
3905 }
3906
3907 static void
3908 isp_kthread(void *arg)
3909 {
3910         struct isp_fc *fc = arg;
3911         ispsoftc_t *isp = fc->isp;
3912         int chan = fc - isp->isp_osinfo.pc.fc;
3913         int slp = 0;
3914
3915         mtx_lock(&isp->isp_osinfo.lock);
3916
3917         while (isp->isp_osinfo.is_exiting == 0) {
3918                 int lb, lim;
3919
3920                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
3921                 lb = isp_fc_runstate(isp, chan, 250000);
3922
3923                 /*
3924                  * Our action is different based upon whether we're supporting
3925                  * Initiator mode or not. If we are, we might freeze the simq
3926                  * when loop is down and set all sorts of different delays to
3927                  * check again.
3928                  *
3929                  * If not, we simply just wait for loop to come up.
3930                  */
3931                 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
3932                         /*
3933                          * Increment loop down time by the last sleep interval
3934                          */
3935                         fc->loop_down_time += slp;
3936
3937                         if (lb < 0) {
3938                                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
3939                         } else {
3940                                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
3941                         }
3942
3943                         /*
3944                          * If we've never seen loop up and we've waited longer
3945                          * than quickboot time, or we've seen loop up but we've
3946                          * waited longer than loop_down_limit, give up and go
3947                          * to sleep until loop comes up.
3948                          */
3949                         if (FCPARAM(isp, chan)->loop_seen_once == 0) {
3950                                 lim = isp_quickboot_time;
3951                         } else {
3952                                 lim = fc->loop_down_limit;
3953                         }
3954                         if (fc->loop_down_time >= lim) {
3955                                 isp_freeze_loopdown(isp, chan, "loop limit hit");
3956                                 slp = 0;
3957                         } else if (fc->loop_down_time < 10) {
3958                                 slp = 1;
3959                         } else if (fc->loop_down_time < 30) {
3960                                 slp = 5;
3961                         } else if (fc->loop_down_time < 60) {
3962                                 slp = 10;
3963                         } else if (fc->loop_down_time < 120) {
3964                                 slp = 20;
3965                         } else {
3966                                 slp = 30;
3967                         }
3968
3969                 } else if (lb) {
3970                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
3971                         fc->loop_down_time += slp;
3972                         if (fc->loop_down_time > 300)
3973                                 slp = 0;
3974                         else
3975                                 slp = 60;
3976                 } else {
3977                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
3978                         fc->loop_down_time = 0;
3979                         slp = 0;
3980                 }
3981
3982
3983                 /*
3984                  * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
3985                  * now so that CAM can start sending us commands.
3986                  *
3987                  * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
3988                  * or kill the commands, as appropriate.
3989                  */
3990
3991                 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
3992                         isp_unfreeze_loopdown(isp, chan);
3993                 }
3994
3995                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
3996
3997                 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
3998
3999                 /*
4000                  * If slp is zero, we're waking up for the first time after
4001                  * things have been okay. In this case, we set a deferral state
4002                  * for all commands and delay hysteresis seconds before starting
4003                  * the FC state evaluation. This gives the loop/fabric a chance
4004                  * to settle.
4005                  */
4006                 if (slp == 0 && fc->hysteresis) {
4007                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4008                         mtx_unlock(&isp->isp_osinfo.lock);
4009                         pause("ispt", fc->hysteresis * hz);
4010                         mtx_lock(&isp->isp_osinfo.lock);
4011                 }
4012         }
4013         fc->num_threads -= 1;
4014         mtx_unlock(&isp->isp_osinfo.lock);
4015         kthread_exit();
4016 }
4017
4018 static void
4019 isp_action(struct cam_sim *sim, union ccb *ccb)
4020 {
4021         int bus, tgt, ts, error, lim;
4022         ispsoftc_t *isp;
4023         struct ccb_trans_settings *cts;
4024
4025         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4026
4027         isp = (ispsoftc_t *)cam_sim_softc(sim);
4028         mtx_assert(&isp->isp_lock, MA_OWNED);
4029         isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4030         ISP_PCMD(ccb) = NULL;
4031
4032         switch (ccb->ccb_h.func_code) {
4033         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
4034                 bus = XS_CHANNEL(ccb);
4035                 /*
4036                  * Do a couple of preliminary checks...
4037                  */
4038                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4039                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4040                                 ccb->ccb_h.status = CAM_REQ_INVALID;
4041                                 isp_done((struct ccb_scsiio *) ccb);
4042                                 break;
4043                         }
4044                 }
4045                 ccb->csio.req_map = NULL;
4046 #ifdef  DIAGNOSTIC
4047                 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
4048                         xpt_print(ccb->ccb_h.path, "invalid target\n");
4049                         ccb->ccb_h.status = CAM_PATH_INVALID;
4050                 } else if (ISP_MAX_LUNS(isp) > 0 &&
4051                     ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
4052                         xpt_print(ccb->ccb_h.path, "invalid lun\n");
4053                         ccb->ccb_h.status = CAM_PATH_INVALID;
4054                 }
4055                 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4056                         xpt_done(ccb);
4057                         break;
4058                 }
4059 #endif
4060                 ccb->csio.scsi_status = SCSI_STATUS_OK;
4061                 if (isp_get_pcmd(isp, ccb)) {
4062                         isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4063                         cam_freeze_devq(ccb->ccb_h.path);
4064                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4065                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
4066                         xpt_done(ccb);
4067                         break;
4068                 }
4069                 error = isp_start((XS_T *) ccb);
4070                 switch (error) {
4071                 case CMD_QUEUED:
4072                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
4073                         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4074                                 break;
4075                         }
4076                         ts = ccb->ccb_h.timeout;
4077                         if (ts == CAM_TIME_DEFAULT) {
4078                                 ts = 60*1000;
4079                         }
4080                         ts = isp_mstohz(ts);
4081                         callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4082                         break;
4083                 case CMD_RQLATER:
4084                         /*
4085                          * We get this result for FC devices if the loop state isn't ready yet
4086                          * or if the device in question has gone zombie on us.
4087                          *
4088                          * If we've never seen Loop UP at all, we requeue this request and wait
4089                          * for the initial loop up delay to expire.
4090                          */
4091                         lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4092                         if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4093                                 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4094                                         isp_prt(isp, ISP_LOGDEBUG0,
4095                                             "%d.%jx loop not seen yet @ %lu",
4096                                             XS_TGT(ccb), (uintmax_t)XS_LUN(ccb),
4097                                             (unsigned long) time_uptime);
4098                                 } else {
4099                                         isp_prt(isp, ISP_LOGDEBUG0,
4100                                             "%d.%jx downtime (%d) > lim (%d)",
4101                                             XS_TGT(ccb), (uintmax_t)XS_LUN(ccb),
4102                                             ISP_FC_PC(isp, bus)->loop_down_time,
4103                                             lim);
4104                                 }
4105                                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
4106                                 isp_done((struct ccb_scsiio *) ccb);
4107                                 break;
4108                         }
4109                         isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
4110                             XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
4111                         cam_freeze_devq(ccb->ccb_h.path);
4112                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4113                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
4114                         isp_free_pcmd(isp, ccb);
4115                         xpt_done(ccb);
4116                         break;
4117                 case CMD_EAGAIN:
4118                         isp_free_pcmd(isp, ccb);
4119                         cam_freeze_devq(ccb->ccb_h.path);
4120                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4121                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
4122                         xpt_done(ccb);
4123                         break;
4124                 case CMD_COMPLETE:
4125                         isp_done((struct ccb_scsiio *) ccb);
4126                         break;
4127                 default:
4128                         isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4129                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
4130                         isp_free_pcmd(isp, ccb);
4131                         xpt_done(ccb);
4132                 }
4133                 break;
4134
4135 #ifdef  ISP_TARGET_MODE
4136         case XPT_EN_LUN:                /* Enable/Disable LUN as a target */
4137                 if (ccb->cel.enable) {
4138                         isp_enable_lun(isp, ccb);
4139                 } else {
4140                         isp_disable_lun(isp, ccb);
4141                 }
4142                 break;
4143         case XPT_IMMED_NOTIFY:
4144         case XPT_IMMEDIATE_NOTIFY:      /* Add Immediate Notify Resource */
4145         case XPT_ACCEPT_TARGET_IO:      /* Add Accept Target IO Resource */
4146         {
4147                 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4148                 if (tptr == NULL) {
4149                         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4150                 }
4151                 if (tptr == NULL) {
4152                         const char *str;
4153                         uint32_t tag;
4154
4155                         if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4156                                 str = "XPT_IMMEDIATE_NOTIFY";
4157                                 tag = ccb->cin1.seq_id;
4158                         } else {
4159                                 tag = ccb->atio.tag_id;
4160                                 str = "XPT_ACCEPT_TARGET_IO";
4161                         }
4162                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4163                         dump_tstates(isp, XS_CHANNEL(ccb));
4164                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4165                         break;
4166                 }
4167                 ccb->ccb_h.spriv_field0 = 0;
4168                 ccb->ccb_h.spriv_ptr1 = isp;
4169
4170                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4171                         if (ccb->atio.tag_id) {
4172                                 atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
4173                                 if (atp) {
4174                                         isp_put_atpd(isp, tptr, atp);
4175                                 }
4176                         }
4177                         tptr->atio_count++;
4178                         SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4179                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4180                             ccb->atio.tag_id, tptr->atio_count);
4181                         ccb->atio.tag_id = 0;
4182                 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4183                         if (ccb->cin1.tag_id) {
4184                                 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4185                                 if (ntp) {
4186                                         isp_put_ntpd(isp, tptr, ntp);
4187                                 }
4188                         }
4189                         tptr->inot_count++;
4190                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4191                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4192                             ccb->cin1.seq_id, tptr->inot_count);
4193                         ccb->cin1.seq_id = 0;
4194                 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4195                         tptr->inot_count++;
4196                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4197                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4198                             ccb->cin1.seq_id, tptr->inot_count);
4199                         ccb->cin1.seq_id = 0;
4200                 }
4201                 rls_lun_statep(isp, tptr);
4202                 ccb->ccb_h.status = CAM_REQ_INPROG;
4203                 break;
4204         }
4205         case XPT_NOTIFY_ACK:
4206                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4207                 break;
4208         case XPT_NOTIFY_ACKNOWLEDGE:            /* notify ack */
4209         {
4210                 tstate_t *tptr;
4211                 inot_private_data_t *ntp;
4212
4213                 /*
4214                  * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4215                  * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4216                  */
4217                 /*
4218                  * All the relevant path information is in the associated immediate notify
4219                  */
4220                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] NOTIFY ACKNOWLEDGE for 0x%x seen\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
4221                 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4222                 if (ntp == NULL) {
4223                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] XPT_NOTIFY_ACKNOWLEDGE of 0x%x cannot find ntp private data\n", __func__,
4224                              ccb->cna2.tag_id, ccb->cna2.seq_id);
4225                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4226                         xpt_done(ccb);
4227                         break;
4228                 }
4229                 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4230                         rls_lun_statep(isp, tptr);
4231                         cam_freeze_devq(ccb->ccb_h.path);
4232                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4233                         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
4234                         ccb->ccb_h.status |= CAM_REQUEUE_REQ;
4235                         break;
4236                 }
4237                 isp_put_ntpd(isp, tptr, ntp);
4238                 rls_lun_statep(isp, tptr);
4239                 ccb->ccb_h.status = CAM_REQ_CMP;
4240                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] calling xpt_done for tag 0x%x\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
4241                 xpt_done(ccb);
4242                 break;
4243         }
4244         case XPT_CONT_TARGET_IO:
4245                 isp_target_start_ctio(isp, ccb, FROM_CAM);
4246                 break;
4247 #endif
4248         case XPT_RESET_DEV:             /* BDR the specified SCSI device */
4249         {
4250                 struct isp_fc *fc;
4251
4252                 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4253                 tgt = ccb->ccb_h.target_id;
4254                 tgt |= (bus << 16);
4255                 if (IS_FC(isp))
4256                         fc = ISP_FC_PC(isp, bus);
4257                 else
4258                         fc = NULL;
4259
4260                 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4261                 if (error) {
4262                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4263                 } else {
4264                         /*
4265                          * If we have a FC device, reset the Command
4266                          * Reference Number, because the target will expect
4267                          * that we re-start the CRN at 1 after a reset.
4268                          */
4269                         if (fc != NULL)
4270                                 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
4271
4272                         ccb->ccb_h.status = CAM_REQ_CMP;
4273                 }
4274                 xpt_done(ccb);
4275                 break;
4276         }
4277         case XPT_ABORT:                 /* Abort the specified CCB */
4278         {
4279                 union ccb *accb = ccb->cab.abort_ccb;
4280                 switch (accb->ccb_h.func_code) {
4281 #ifdef  ISP_TARGET_MODE
4282                 case XPT_ACCEPT_TARGET_IO:
4283                         isp_target_mark_aborted(isp, ccb);
4284                         break;
4285 #endif
4286                 case XPT_SCSI_IO:
4287                         error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
4288                         if (error) {
4289                                 ccb->ccb_h.status = CAM_UA_ABORT;
4290                         } else {
4291                                 ccb->ccb_h.status = CAM_REQ_CMP;
4292                         }
4293                         break;
4294                 default:
4295                         ccb->ccb_h.status = CAM_REQ_INVALID;
4296                         break;
4297                 }
4298                 /*
4299                  * This is not a queued CCB, so the caller expects it to be
4300                  * complete when control is returned.
4301                  */
4302                 break;
4303         }
4304 #define IS_CURRENT_SETTINGS(c)  (c->type == CTS_TYPE_CURRENT_SETTINGS)
4305         case XPT_SET_TRAN_SETTINGS:     /* Nexus Settings */
4306                 cts = &ccb->cts;
4307                 if (!IS_CURRENT_SETTINGS(cts)) {
4308                         ccb->ccb_h.status = CAM_REQ_INVALID;
4309                         xpt_done(ccb);
4310                         break;
4311                 }
4312                 tgt = cts->ccb_h.target_id;
4313                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4314                 if (IS_SCSI(isp)) {
4315                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4316                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4317                         sdparam *sdp = SDPARAM(isp, bus);
4318                         uint16_t *dptr;
4319
4320                         if (spi->valid == 0 && scsi->valid == 0) {
4321                                 ccb->ccb_h.status = CAM_REQ_CMP;
4322                                 xpt_done(ccb);
4323                                 break;
4324                         }
4325
4326                         /*
4327                          * We always update (internally) from goal_flags
4328                          * so any request to change settings just gets
4329                          * vectored to that location.
4330                          */
4331                         dptr = &sdp->isp_devparam[tgt].goal_flags;
4332
4333                         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4334                                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4335                                         *dptr |= DPARM_DISC;
4336                                 else
4337                                         *dptr &= ~DPARM_DISC;
4338                         }
4339
4340                         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4341                                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4342                                         *dptr |= DPARM_TQING;
4343                                 else
4344                                         *dptr &= ~DPARM_TQING;
4345                         }
4346
4347                         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4348                                 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4349                                         *dptr |= DPARM_WIDE;
4350                                 else
4351                                         *dptr &= ~DPARM_WIDE;
4352                         }
4353
4354                         /*
4355                          * XXX: FIX ME
4356                          */
4357                         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4358                                 *dptr |= DPARM_SYNC;
4359                                 /*
4360                                  * XXX: CHECK FOR LEGALITY
4361                                  */
4362                                 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4363                                 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4364                         } else {
4365                                 *dptr &= ~DPARM_SYNC;
4366                         }
4367                         isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
4368                             sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4369                         sdp->isp_devparam[tgt].dev_update = 1;
4370                         sdp->update = 1;
4371                 }
4372                 ccb->ccb_h.status = CAM_REQ_CMP;
4373                 xpt_done(ccb);
4374                 break;
4375         case XPT_GET_TRAN_SETTINGS:
4376                 cts = &ccb->cts;
4377                 tgt = cts->ccb_h.target_id;
4378                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4379                 if (IS_FC(isp)) {
4380                         fcparam *fcp = FCPARAM(isp, bus);
4381                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4382                         struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4383
4384                         cts->protocol = PROTO_SCSI;
4385                         cts->protocol_version = SCSI_REV_2;
4386                         cts->transport = XPORT_FC;
4387                         cts->transport_version = 0;
4388
4389                         scsi->valid = CTS_SCSI_VALID_TQ;
4390                         scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4391                         fc->valid = CTS_FC_VALID_SPEED;
4392                         fc->bitrate = 100000;
4393                         fc->bitrate *= fcp->isp_gbspeed;
4394                         if (tgt < MAX_FC_TARG) {
4395                                 fcportdb_t *lp = &fcp->portdb[tgt];
4396                                 fc->wwnn = lp->node_wwn;
4397                                 fc->wwpn = lp->port_wwn;
4398                                 fc->port = lp->portid;
4399                                 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4400                         }
4401                 } else {
4402                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4403                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4404                         sdparam *sdp = SDPARAM(isp, bus);
4405                         uint16_t dval, pval, oval;
4406
4407                         if (IS_CURRENT_SETTINGS(cts)) {
4408                                 sdp->isp_devparam[tgt].dev_refresh = 1;
4409                                 sdp->update = 1;
4410                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4411                                 dval = sdp->isp_devparam[tgt].actv_flags;
4412                                 oval = sdp->isp_devparam[tgt].actv_offset;
4413                                 pval = sdp->isp_devparam[tgt].actv_period;
4414                         } else {
4415                                 dval = sdp->isp_devparam[tgt].nvrm_flags;
4416                                 oval = sdp->isp_devparam[tgt].nvrm_offset;
4417                                 pval = sdp->isp_devparam[tgt].nvrm_period;
4418                         }
4419
4420                         cts->protocol = PROTO_SCSI;
4421                         cts->protocol_version = SCSI_REV_2;
4422                         cts->transport = XPORT_SPI;
4423                         cts->transport_version = 2;
4424
4425                         spi->valid = 0;
4426                         scsi->valid = 0;
4427                         spi->flags = 0;
4428                         scsi->flags = 0;
4429                         if (dval & DPARM_DISC) {
4430                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4431                         }
4432                         if ((dval & DPARM_SYNC) && oval && pval) {
4433                                 spi->sync_offset = oval;
4434                                 spi->sync_period = pval;
4435                         } else {
4436                                 spi->sync_offset = 0;
4437                                 spi->sync_period = 0;
4438                         }
4439                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4440                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4441                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4442                         if (dval & DPARM_WIDE) {
4443                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4444                         } else {
4445                                 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4446                         }
4447                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4448                                 scsi->valid = CTS_SCSI_VALID_TQ;
4449                                 if (dval & DPARM_TQING) {
4450                                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4451                                 }
4452                                 spi->valid |= CTS_SPI_VALID_DISC;
4453                         }
4454                         isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4455                             bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4456                 }
4457                 ccb->ccb_h.status = CAM_REQ_CMP;
4458                 xpt_done(ccb);
4459                 break;
4460
4461         case XPT_CALC_GEOMETRY:
4462                 cam_calc_geometry(&ccb->ccg, 1);
4463                 xpt_done(ccb);
4464                 break;
4465
4466         case XPT_RESET_BUS:             /* Reset the specified bus */
4467                 bus = cam_sim_bus(sim);
4468                 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4469                 if (error) {
4470                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4471                         xpt_done(ccb);
4472                         break;
4473                 }
4474                 if (bootverbose) {
4475                         xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4476                 }
4477                 if (IS_FC(isp)) {
4478                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4479                 } else {
4480                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4481                 }
4482                 ccb->ccb_h.status = CAM_REQ_CMP;
4483                 xpt_done(ccb);
4484                 break;
4485
4486         case XPT_TERM_IO:               /* Terminate the I/O process */
4487                 ccb->ccb_h.status = CAM_REQ_INVALID;
4488                 xpt_done(ccb);
4489                 break;
4490
4491         case XPT_SET_SIM_KNOB:          /* Set SIM knobs */
4492         {
4493                 struct ccb_sim_knob *kp = &ccb->knob;
4494                 fcparam *fcp;
4495
4496                 if (!IS_FC(isp)) {
4497                         ccb->ccb_h.status = CAM_REQ_INVALID;
4498                         xpt_done(ccb);
4499                         break;
4500                 }
4501
4502                 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4503                 fcp = FCPARAM(isp, bus);
4504
4505                 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4506                         fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4507                         fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4508                         isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4509                 }
4510                 ccb->ccb_h.status = CAM_REQ_CMP;
4511                 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4512                         int rchange = 0;
4513                         int newrole = 0;
4514
4515                         switch (kp->xport_specific.fc.role) {
4516                         case KNOB_ROLE_NONE:
4517                                 if (fcp->role != ISP_ROLE_NONE) {
4518                                         rchange = 1;
4519                                         newrole = ISP_ROLE_NONE;
4520                                 }
4521                                 break;
4522                         case KNOB_ROLE_TARGET:
4523                                 if (fcp->role != ISP_ROLE_TARGET) {
4524                                         rchange = 1;
4525                                         newrole = ISP_ROLE_TARGET;
4526                                 }
4527                                 break;
4528                         case KNOB_ROLE_INITIATOR:
4529                                 if (fcp->role != ISP_ROLE_INITIATOR) {
4530                                         rchange = 1;
4531                                         newrole = ISP_ROLE_INITIATOR;
4532                                 }
4533                                 break;
4534                         case KNOB_ROLE_BOTH:
4535                                 if (fcp->role != ISP_ROLE_BOTH) {
4536                                         rchange = 1;
4537                                         newrole = ISP_ROLE_BOTH;
4538                                 }
4539                                 break;
4540                         }
4541                         if (rchange) {
4542                                 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
4543 #ifdef  ISP_TARGET_MODE
4544                                 ISP_SET_PC(isp, bus, tm_enabled, 0);
4545                                 ISP_SET_PC(isp, bus, tm_luns_enabled, 0);
4546 #endif
4547                                 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
4548                                     bus, newrole) != 0) {
4549                                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4550                                         xpt_done(ccb);
4551                                         break;
4552                                 }
4553 #ifdef  ISP_TARGET_MODE
4554                                 if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4555                                         /*
4556                                          * Give the new role a chance to complain and settle
4557                                          */
4558                                         msleep(isp, &isp->isp_lock, PRIBIO, "taking a breather", 2);
4559                                         ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
4560                                 }
4561 #endif
4562                         }
4563                 }
4564                 xpt_done(ccb);
4565                 break;
4566         }
4567         case XPT_GET_SIM_KNOB:          /* Get SIM knobs */
4568         {
4569                 struct ccb_sim_knob *kp = &ccb->knob;
4570
4571                 if (IS_FC(isp)) {
4572                         fcparam *fcp;
4573
4574                         bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4575                         fcp = FCPARAM(isp, bus);
4576
4577                         kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4578                         kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4579                         switch (fcp->role) {
4580                         case ISP_ROLE_NONE:
4581                                 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4582                                 break;
4583                         case ISP_ROLE_TARGET:
4584                                 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4585                                 break;
4586                         case ISP_ROLE_INITIATOR:
4587                                 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4588                                 break;
4589                         case ISP_ROLE_BOTH:
4590                                 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4591                                 break;
4592                         }
4593                         kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4594                         ccb->ccb_h.status = CAM_REQ_CMP;
4595                 } else {
4596                         ccb->ccb_h.status = CAM_REQ_INVALID;
4597                 }
4598                 xpt_done(ccb);
4599                 break;
4600         }
4601         case XPT_PATH_INQ:              /* Path routing inquiry */
4602         {
4603                 struct ccb_pathinq *cpi = &ccb->cpi;
4604
4605                 cpi->version_num = 1;
4606 #ifdef  ISP_TARGET_MODE
4607                 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4608 #else
4609                 cpi->target_sprt = 0;
4610 #endif
4611                 cpi->hba_eng_cnt = 0;
4612                 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4613                 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
4614                     255 : ISP_MAX_LUNS(isp) - 1;
4615                 cpi->bus_id = cam_sim_bus(sim);
4616                 if (isp->isp_osinfo.sixtyfourbit)
4617                         cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
4618                 else
4619                         cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
4620
4621                 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4622                 if (IS_FC(isp)) {
4623                         fcparam *fcp = FCPARAM(isp, bus);
4624
4625                         cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
4626 #if __FreeBSD_version >= 1000700
4627                         cpi->hba_misc |= PIM_EXTLUNS;
4628 #endif
4629 #if __FreeBSD_version >= 1000039
4630                         cpi->hba_misc |= PIM_NOSCAN;
4631 #endif
4632
4633                         /*
4634                          * Because our loop ID can shift from time to time,
4635                          * make our initiator ID out of range of our bus.
4636                          */
4637                         cpi->initiator_id = cpi->max_target + 1;
4638
4639                         /*
4640                          * Set base transfer capabilities for Fibre Channel, for this HBA.
4641                          */
4642                         if (IS_25XX(isp)) {
4643                                 cpi->base_transfer_speed = 8000000;
4644                         } else if (IS_24XX(isp)) {
4645                                 cpi->base_transfer_speed = 4000000;
4646                         } else if (IS_23XX(isp)) {
4647                                 cpi->base_transfer_speed = 2000000;
4648                         } else {
4649                                 cpi->base_transfer_speed = 1000000;
4650                         }
4651                         cpi->hba_inquiry = PI_TAG_ABLE;
4652                         cpi->transport = XPORT_FC;
4653                         cpi->transport_version = 0;
4654                         cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4655                         cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4656                         cpi->xport_specific.fc.port = fcp->isp_portid;
4657                         cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4658                 } else {
4659                         sdparam *sdp = SDPARAM(isp, bus);
4660                         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4661                         cpi->hba_misc = PIM_UNMAPPED;
4662                         cpi->initiator_id = sdp->isp_initiator_id;
4663                         cpi->base_transfer_speed = 3300;
4664                         cpi->transport = XPORT_SPI;
4665                         cpi->transport_version = 2;
4666                 }
4667                 cpi->protocol = PROTO_SCSI;
4668                 cpi->protocol_version = SCSI_REV_2;
4669                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4670                 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4671                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4672                 cpi->unit_number = cam_sim_unit(sim);
4673                 cpi->ccb_h.status = CAM_REQ_CMP;
4674                 xpt_done(ccb);
4675                 break;
4676         }
4677         default:
4678                 ccb->ccb_h.status = CAM_REQ_INVALID;
4679                 xpt_done(ccb);
4680                 break;
4681         }
4682 }
4683
4684 #define ISPDDB  (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4685
4686 void
4687 isp_done(XS_T *sccb)
4688 {
4689         ispsoftc_t *isp = XS_ISP(sccb);
4690         uint32_t status;
4691
4692         if (XS_NOERR(sccb))
4693                 XS_SETERR(sccb, CAM_REQ_CMP);
4694
4695         if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4696                 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4697                 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4698                         sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4699                 } else {
4700                         sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4701                 }
4702         }
4703
4704         sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4705         status = sccb->ccb_h.status & CAM_STATUS_MASK;
4706         if (status != CAM_REQ_CMP) {
4707                 if (status != CAM_SEL_TIMEOUT)
4708                         isp_prt(isp, ISP_LOGDEBUG0,
4709                             "target %d lun %jx CAM status 0x%x SCSI status 0x%x",
4710                             XS_TGT(sccb), (uintmax_t)XS_LUN(sccb),
4711                             sccb->ccb_h.status, sccb->scsi_status);
4712                 else if ((IS_FC(isp))
4713                       && (XS_TGT(sccb) < MAX_FC_TARG)) {
4714                         fcparam *fcp;
4715
4716                         fcp = FCPARAM(isp, XS_CHANNEL(sccb));
4717                         fcp->portdb[XS_TGT(sccb)].is_target = 0;
4718                 }
4719                 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4720                         sccb->ccb_h.status |= CAM_DEV_QFRZN;
4721                         xpt_freeze_devq(sccb->ccb_h.path, 1);
4722                 }
4723         }
4724
4725         if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4726                 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4727         }
4728
4729         if (ISP_PCMD(sccb)) {
4730                 if (callout_active(&PISP_PCMD(sccb)->wdog))
4731                         callout_stop(&PISP_PCMD(sccb)->wdog);
4732                 isp_free_pcmd(isp, (union ccb *) sccb);
4733         }
4734         xpt_done((union ccb *) sccb);
4735 }
4736
4737 void
4738 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4739 {
4740         int bus;
4741         static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
4742         char buf[64];
4743         char *msg = NULL;
4744         target_id_t tgt;
4745         fcportdb_t *lp;
4746         struct isp_fc *fc;
4747         struct cam_path *tmppath;
4748         struct ac_contract ac;
4749         struct ac_device_changed *adc;
4750         va_list ap;
4751
4752         switch (cmd) {
4753         case ISPASYNC_NEW_TGT_PARAMS:
4754         {
4755                 struct ccb_trans_settings_scsi *scsi;
4756                 struct ccb_trans_settings_spi *spi;
4757                 int flags, tgt;
4758                 sdparam *sdp;
4759                 struct ccb_trans_settings cts;
4760
4761                 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4762
4763                 va_start(ap, cmd);
4764                 bus = va_arg(ap, int);
4765                 tgt = va_arg(ap, int);
4766                 va_end(ap);
4767                 sdp = SDPARAM(isp, bus);
4768
4769                 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4770                         isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4771                         break;
4772                 }
4773                 flags = sdp->isp_devparam[tgt].actv_flags;
4774                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
4775                 cts.protocol = PROTO_SCSI;
4776                 cts.transport = XPORT_SPI;
4777
4778                 scsi = &cts.proto_specific.scsi;
4779                 spi = &cts.xport_specific.spi;
4780
4781                 if (flags & DPARM_TQING) {
4782                         scsi->valid |= CTS_SCSI_VALID_TQ;
4783                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4784                 }
4785
4786                 if (flags & DPARM_DISC) {
4787                         spi->valid |= CTS_SPI_VALID_DISC;
4788                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4789                 }
4790                 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
4791                 if (flags & DPARM_WIDE) {
4792                         spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4793                 } else {
4794                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4795                 }
4796                 if (flags & DPARM_SYNC) {
4797                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4798                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4799                         spi->sync_period = sdp->isp_devparam[tgt].actv_period;
4800                         spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
4801                 }
4802                 isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags);
4803                 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
4804                 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
4805                 xpt_free_path(tmppath);
4806                 break;
4807         }
4808         case ISPASYNC_BUS_RESET:
4809         {
4810                 va_start(ap, cmd);
4811                 bus = va_arg(ap, int);
4812                 va_end(ap);
4813                 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
4814                 if (IS_FC(isp)) {
4815                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
4816                 } else {
4817                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
4818                 }
4819                 break;
4820         }
4821         case ISPASYNC_LIP:
4822                 if (msg == NULL)
4823                         msg = "LIP Received";
4824                 /* FALLTHROUGH */
4825         case ISPASYNC_LOOP_RESET:
4826                 if (msg == NULL)
4827                         msg = "LOOP Reset";
4828                 /* FALLTHROUGH */
4829         case ISPASYNC_LOOP_DOWN:
4830         {
4831                 if (msg == NULL)
4832                         msg = "LOOP Down";
4833                 va_start(ap, cmd);
4834                 bus = va_arg(ap, int);
4835                 va_end(ap);
4836
4837                 FCPARAM(isp, bus)->isp_linkstate = 0;
4838
4839                 fc = ISP_FC_PC(isp, bus);
4840                 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
4841                         /*
4842                          * We don't do any simq freezing if we are only in target mode
4843                          */
4844                         if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
4845                                 if (fc->path) {
4846                                         isp_freeze_loopdown(isp, bus, msg);
4847                                 }
4848                         }
4849                         if (!callout_active(&fc->ldt)) {
4850                                 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
4851                                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
4852                         }
4853                 }
4854                 isp_fcp_reset_crn(fc, /*tgt*/0, /*tgt_set*/ 0);
4855
4856                 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
4857                 break;
4858         }
4859         case ISPASYNC_LOOP_UP:
4860                 va_start(ap, cmd);
4861                 bus = va_arg(ap, int);
4862                 va_end(ap);
4863                 fc = ISP_FC_PC(isp, bus);
4864                 /*
4865                  * Now we just note that Loop has come up. We don't
4866                  * actually do anything because we're waiting for a
4867                  * Change Notify before activating the FC cleanup
4868                  * thread to look at the state of the loop again.
4869                  */
4870                 FCPARAM(isp, bus)->isp_linkstate = 1;
4871                 fc->loop_dead = 0;
4872                 fc->loop_down_time = 0;
4873                 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
4874                 break;
4875         case ISPASYNC_DEV_ARRIVED:
4876                 va_start(ap, cmd);
4877                 bus = va_arg(ap, int);
4878                 lp = va_arg(ap, fcportdb_t *);
4879                 va_end(ap);
4880                 fc = ISP_FC_PC(isp, bus);
4881                 tgt = FC_PORTDB_TGT(isp, bus, lp);
4882                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4883                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
4884                 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4885                     (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
4886                         lp->is_target = 1;
4887                         isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
4888                         isp_make_here(isp, lp, bus, tgt);
4889                 }
4890                 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4891                     (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
4892                         lp->is_initiator = 1;
4893                         ac.contract_number = AC_CONTRACT_DEV_CHG;
4894                         adc = (struct ac_device_changed *) ac.contract_data;
4895                         adc->wwpn = lp->port_wwn;
4896                         adc->port = lp->portid;
4897                         adc->target = tgt;
4898                         adc->arrived = 1;
4899                         xpt_async(AC_CONTRACT, fc->path, &ac);
4900                 }
4901                 break;
4902         case ISPASYNC_DEV_CHANGED:
4903                 va_start(ap, cmd);
4904                 bus = va_arg(ap, int);
4905                 lp = va_arg(ap, fcportdb_t *);
4906                 va_end(ap);
4907                 fc = ISP_FC_PC(isp, bus);
4908                 tgt = FC_PORTDB_TGT(isp, bus, lp);
4909                 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
4910                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
4911 changed:
4912                 if (lp->is_target !=
4913                     ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
4914                      (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
4915                         lp->is_target = !lp->is_target;
4916                         if (lp->is_target) {
4917                                 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
4918                                 isp_make_here(isp, lp, bus, tgt);
4919                         } else {
4920                                 isp_make_gone(isp, lp, bus, tgt);
4921                                 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
4922                         }
4923                 }
4924                 if (lp->is_initiator !=
4925                     ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
4926                      (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
4927                         lp->is_initiator = !lp->is_initiator;
4928                         ac.contract_number = AC_CONTRACT_DEV_CHG;
4929                         adc = (struct ac_device_changed *) ac.contract_data;
4930                         adc->wwpn = lp->port_wwn;
4931                         adc->port = lp->portid;
4932                         adc->target = tgt;
4933                         adc->arrived = lp->is_initiator;
4934                         xpt_async(AC_CONTRACT, fc->path, &ac);
4935                 }
4936                 break;
4937         case ISPASYNC_DEV_STAYED:
4938                 va_start(ap, cmd);
4939                 bus = va_arg(ap, int);
4940                 lp = va_arg(ap, fcportdb_t *);
4941                 va_end(ap);
4942                 fc = ISP_FC_PC(isp, bus);
4943                 tgt = FC_PORTDB_TGT(isp, bus, lp);
4944                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4945                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
4946                 goto changed;
4947         case ISPASYNC_DEV_GONE:
4948                 va_start(ap, cmd);
4949                 bus = va_arg(ap, int);
4950                 lp = va_arg(ap, fcportdb_t *);
4951                 va_end(ap);
4952                 fc = ISP_FC_PC(isp, bus);
4953                 tgt = FC_PORTDB_TGT(isp, bus, lp);
4954                 /*
4955                  * If this has a virtual target or initiator set the isp_gdt
4956                  * timer running on it to delay its departure.
4957                  */
4958                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
4959                 if (lp->is_target || lp->is_initiator) {
4960                         lp->state = FC_PORTDB_STATE_ZOMBIE;
4961                         lp->gone_timer = fc->gone_device_time;
4962                         isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
4963                         if (fc->ready && !callout_active(&fc->gdt)) {
4964                                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
4965                                 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4966                         }
4967                         break;
4968                 }
4969                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
4970                 break;
4971         case ISPASYNC_CHANGE_NOTIFY:
4972         {
4973                 char *msg;
4974                 int evt, nphdl, nlstate, reason;
4975
4976                 va_start(ap, cmd);
4977                 bus = va_arg(ap, int);
4978                 evt = va_arg(ap, int);
4979                 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
4980                         nphdl = va_arg(ap, int);
4981                         nlstate = va_arg(ap, int);
4982                         reason = va_arg(ap, int);
4983                 } else {
4984                         nphdl = NIL_HANDLE;
4985                         nlstate = reason = 0;
4986                 }
4987                 va_end(ap);
4988                 fc = ISP_FC_PC(isp, bus);
4989
4990                 if (evt == ISPASYNC_CHANGE_PDB) {
4991                         msg = "Chan %d Port Database Changed";
4992                 } else if (evt == ISPASYNC_CHANGE_SNS) {
4993                         msg = "Chan %d Name Server Database Changed";
4994                 } else {
4995                         msg = "Chan %d Other Change Notify";
4996                 }
4997
4998                 /*
4999                  * If the loop down timer is running, cancel it.
5000                  */
5001                 if (fc->ready && callout_active(&fc->ldt)) {
5002                         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5003                         callout_stop(&fc->ldt);
5004                 }
5005                 isp_prt(isp, ISP_LOGINFO, msg, bus);
5006                 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5007                         isp_freeze_loopdown(isp, bus, msg);
5008                 }
5009                 wakeup(fc);
5010                 break;
5011         }
5012 #ifdef  ISP_TARGET_MODE
5013         case ISPASYNC_TARGET_NOTIFY:
5014         {
5015                 isp_notify_t *notify;
5016                 va_start(ap, cmd);
5017                 notify = va_arg(ap, isp_notify_t *);
5018                 va_end(ap);
5019                 switch (notify->nt_ncode) {
5020                 case NT_ABORT_TASK:
5021                 case NT_ABORT_TASK_SET:
5022                 case NT_CLEAR_ACA:
5023                 case NT_CLEAR_TASK_SET:
5024                 case NT_LUN_RESET:
5025                 case NT_TARGET_RESET:
5026                 case NT_QUERY_TASK_SET:
5027                 case NT_QUERY_ASYNC_EVENT:
5028                         /*
5029                          * These are task management functions.
5030                          */
5031                         isp_handle_platform_target_tmf(isp, notify);
5032                         break;
5033                 case NT_BUS_RESET:
5034                 case NT_LIP_RESET:
5035                 case NT_LINK_UP:
5036                 case NT_LINK_DOWN:
5037                 case NT_HBA_RESET:
5038                         /*
5039                          * No action need be taken here.
5040                          */
5041                         break;
5042                 case NT_GLOBAL_LOGOUT:
5043                 case NT_LOGOUT:
5044                         /*
5045                          * This is device arrival/departure notification
5046                          */
5047                         isp_handle_platform_target_notify_ack(isp, notify);
5048                         break;
5049                 default:
5050                         isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5051                         isp_handle_platform_target_notify_ack(isp, notify);
5052                         break;
5053                 }
5054                 break;
5055         }
5056         case ISPASYNC_TARGET_NOTIFY_ACK:
5057         {
5058                 void *inot;
5059                 va_start(ap, cmd);
5060                 inot = va_arg(ap, void *);
5061                 va_end(ap);
5062                 if (isp_notify_ack(isp, inot)) {
5063                         isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
5064                         if (tp) {
5065                                 tp->isp = isp;
5066                                 if (inot) {
5067                                         memcpy(tp->data, inot, sizeof (tp->data));
5068                                         tp->not = tp->data;
5069                                 } else {
5070                                         tp->not = NULL;
5071                                 }
5072                                 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
5073                                 callout_reset(&tp->timer, 5,
5074                                     isp_refire_notify_ack, tp);
5075                         } else {
5076                                 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
5077                         }
5078                 }
5079                 break;
5080         }
5081         case ISPASYNC_TARGET_ACTION:
5082         {
5083                 isphdr_t *hp;
5084
5085                 va_start(ap, cmd);
5086                 hp = va_arg(ap, isphdr_t *);
5087                 va_end(ap);
5088                 switch (hp->rqs_entry_type) {
5089                 default:
5090                         isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5091                         break;
5092                 case RQSTYPE_NOTIFY:
5093                         if (IS_SCSI(isp)) {
5094                                 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5095                         } else if (IS_24XX(isp)) {
5096                                 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5097                         } else {
5098                                 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5099                         }
5100                         break;
5101                 case RQSTYPE_ATIO:
5102                         if (IS_24XX(isp)) {
5103                                 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5104                         } else {
5105                                 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5106                         }
5107                         break;
5108                 case RQSTYPE_ATIO2:
5109                         isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5110                         break;
5111                 case RQSTYPE_CTIO7:
5112                 case RQSTYPE_CTIO3:
5113                 case RQSTYPE_CTIO2:
5114                 case RQSTYPE_CTIO:
5115                         isp_handle_platform_ctio(isp, hp);
5116                         break;
5117                 case RQSTYPE_ABTS_RCVD:
5118                 {
5119                         abts_t *abts = (abts_t *)hp;
5120                         isp_notify_t notify, *nt = &notify;
5121                         tstate_t *tptr;
5122                         fcportdb_t *lp;
5123                         uint16_t chan;
5124                         uint32_t sid, did;
5125
5126                         did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5127                         sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5128                         ISP_MEMZERO(nt, sizeof (isp_notify_t));
5129
5130                         nt->nt_hba = isp;
5131                         nt->nt_did = did;
5132                         nt->nt_nphdl = abts->abts_nphdl;
5133                         nt->nt_sid = sid;
5134                         isp_find_chan_by_did(isp, did, &chan);
5135                         if (chan == ISP_NOCHAN) {
5136                                 nt->nt_tgt = TGT_ANY;
5137                         } else {
5138                                 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5139                                 if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
5140                                         nt->nt_wwn = lp->port_wwn;
5141                                 } else {
5142                                         nt->nt_wwn = INI_ANY;
5143                                 }
5144                         }
5145                         /*
5146                          * Try hard to find the lun for this command.
5147                          */
5148                         tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5149                         if (tptr) {
5150                                 nt->nt_lun = tptr->ts_lun;
5151                                 rls_lun_statep(isp, tptr);
5152                         } else {
5153                                 nt->nt_lun = LUN_ANY;
5154                         }
5155                         nt->nt_need_ack = 1;
5156                         nt->nt_tagval = abts->abts_rxid_task;
5157                         nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5158                         if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5159                                 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
5160                                     abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5161                         } else {
5162                                 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
5163                                     abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5164                         }
5165                         nt->nt_channel = chan;
5166                         nt->nt_ncode = NT_ABORT_TASK;
5167                         nt->nt_lreserved = hp;
5168                         isp_handle_platform_target_tmf(isp, nt);
5169                         break;
5170                 }
5171                 case RQSTYPE_ENABLE_LUN:
5172                 case RQSTYPE_MODIFY_LUN:
5173                         isp_ledone(isp, (lun_entry_t *) hp);
5174                         break;
5175                 }
5176                 break;
5177         }
5178 #endif
5179         case ISPASYNC_FW_CRASH:
5180         {
5181                 uint16_t mbox1, mbox6;
5182                 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5183                 if (IS_DUALBUS(isp)) { 
5184                         mbox6 = ISP_READ(isp, OUTMAILBOX6);
5185                 } else {
5186                         mbox6 = 0;
5187                 }
5188                 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5189                 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5190                 isp->isp_osinfo.mbox_sleep_ok = 0;
5191                 isp_reinit(isp, 1);
5192                 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5193                 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5194                 break;
5195         }
5196         default:
5197                 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5198                 break;
5199         }
5200 }
5201
5202
5203 /*
5204  * Locks are held before coming here.
5205  */
5206 void
5207 isp_uninit(ispsoftc_t *isp)
5208 {
5209         if (IS_24XX(isp)) {
5210                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5211         } else {
5212                 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5213         }
5214         ISP_DISABLE_INTS(isp);
5215 }
5216
5217 /*
5218  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5219  * up from our platform default (defww{p|n}n) and morph them based upon
5220  * channel.
5221  * 
5222  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5223  * based upon channel.
5224  */
5225
5226 uint64_t
5227 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5228 {
5229         uint64_t seed;
5230         struct isp_fc *fc = ISP_FC_PC(isp, chan);
5231
5232         /*
5233          * If we're asking for a active WWN, the default overrides get
5234          * returned, otherwise the NVRAM value is picked.
5235          * 
5236          * If we're asking for a default WWN, we just pick the default override.
5237          */
5238         if (isactive) {
5239                 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5240                 if (seed) {
5241                         return (seed);
5242                 }
5243                 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5244                 if (seed) {
5245                         return (seed);
5246                 }
5247                 return (0x400000007F000009ull);
5248         }
5249
5250         seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5251
5252         /*
5253          * For channel zero just return what we have. For either ACTIVE or
5254          * DEFAULT cases, we depend on default override of NVRAM values for
5255          * channel zero.
5256          */
5257         if (chan == 0) {
5258                 return (seed);
5259         }
5260
5261         /*
5262          * For other channels, we are doing one of three things:
5263          * 
5264          * 1. If what we have now is non-zero, return it. Otherwise we morph
5265          * values from channel 0. 2. If we're here for a WWPN we synthesize
5266          * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5267          * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5268          */
5269
5270         if (seed) {
5271                 return (seed);
5272         }
5273         seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5274         if (seed == 0)
5275                 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5276
5277         if (((seed >> 60) & 0xf) == 2) {
5278                 /*
5279                  * The type 2 NAA fields for QLogic cards appear be laid out
5280                  * thusly:
5281                  * 
5282                  * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5283                  * port (1) or node (0) WWN distinguishor bit 48
5284                  * physical port on dual-port chips (23XX/24XX)
5285                  * 
5286                  * This is somewhat nutty, particularly since bit 48 is
5287                  * irrelevant as they assign separate serial numbers to
5288                  * different physical ports anyway.
5289                  * 
5290                  * We'll stick our channel number plus one first into bits
5291                  * 57..59 and thence into bits 52..55 which allows for 8 bits
5292                  * of channel which is comfortably more than our maximum
5293                  * (126) now.
5294                  */
5295                 seed &= ~0x0FF0000000000000ULL;
5296                 if (iswwnn == 0) {
5297                         seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5298                         seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5299                 }
5300         } else {
5301                 seed = 0;
5302         }
5303         return (seed);
5304 }
5305
5306 void
5307 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5308 {
5309         int loc;
5310         char lbuf[200];
5311         va_list ap;
5312
5313         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5314                 return;
5315         }
5316         snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
5317         loc = strlen(lbuf);
5318         va_start(ap, fmt);
5319         vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap); 
5320         va_end(ap);
5321         printf("%s\n", lbuf);
5322 }
5323
5324 void
5325 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5326 {
5327         va_list ap;
5328         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5329                 return;
5330         }
5331         xpt_print_path(xs->ccb_h.path);
5332         va_start(ap, fmt);
5333         vprintf(fmt, ap);
5334         va_end(ap);
5335         printf("\n");
5336 }
5337
5338 uint64_t
5339 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5340 {
5341         uint64_t elapsed;
5342         struct timespec x = *b;
5343         timespecsub(&x, a);
5344         elapsed = GET_NANOSEC(&x);
5345         if (elapsed == 0)
5346                 elapsed++;
5347         return (elapsed);
5348 }
5349
5350 int
5351 isp_mbox_acquire(ispsoftc_t *isp)
5352 {
5353         if (isp->isp_osinfo.mboxbsy) {
5354                 return (1);
5355         } else {
5356                 isp->isp_osinfo.mboxcmd_done = 0;
5357                 isp->isp_osinfo.mboxbsy = 1;
5358                 return (0);
5359         }
5360 }
5361
5362 void
5363 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5364 {
5365         unsigned int usecs = mbp->timeout;
5366         unsigned int max, olim, ilim;
5367
5368         if (usecs == 0) {
5369                 usecs = MBCMD_DEFAULT_TIMEOUT;
5370         }
5371         max = isp->isp_mbxwrk0 + 1;
5372
5373         if (isp->isp_osinfo.mbox_sleep_ok) {
5374                 unsigned int ms = (usecs + 999) / 1000;
5375
5376                 isp->isp_osinfo.mbox_sleep_ok = 0;
5377                 isp->isp_osinfo.mbox_sleeping = 1;
5378                 for (olim = 0; olim < max; olim++) {
5379                         msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5380                         if (isp->isp_osinfo.mboxcmd_done) {
5381                                 break;
5382                         }
5383                 }
5384                 isp->isp_osinfo.mbox_sleep_ok = 1;
5385                 isp->isp_osinfo.mbox_sleeping = 0;
5386         } else {
5387                 for (olim = 0; olim < max; olim++) {
5388                         for (ilim = 0; ilim < usecs; ilim += 100) {
5389                                 uint16_t isr, sema, info;
5390                                 if (isp->isp_osinfo.mboxcmd_done) {
5391                                         break;
5392                                 }
5393                                 if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
5394                                         isp_intr(isp, isr, sema, info);
5395                                         if (isp->isp_osinfo.mboxcmd_done) {
5396                                                 break;
5397                                         }
5398                                 }
5399                                 ISP_DELAY(100);
5400                         }
5401                         if (isp->isp_osinfo.mboxcmd_done) {
5402                                 break;
5403                         }
5404                 }
5405         }
5406         if (isp->isp_osinfo.mboxcmd_done == 0) {
5407                 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5408                     isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5409                 mbp->param[0] = MBOX_TIMEOUT;
5410                 isp->isp_osinfo.mboxcmd_done = 1;
5411         }
5412 }
5413
5414 void
5415 isp_mbox_notify_done(ispsoftc_t *isp)
5416 {
5417         if (isp->isp_osinfo.mbox_sleeping) {
5418                 wakeup(&isp->isp_mbxworkp);
5419         }
5420         isp->isp_osinfo.mboxcmd_done = 1;
5421 }
5422
5423 void
5424 isp_mbox_release(ispsoftc_t *isp)
5425 {
5426         isp->isp_osinfo.mboxbsy = 0;
5427 }
5428
5429 int
5430 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5431 {
5432         int ret = 0;
5433         if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5434                 ret = -1;
5435         } else {
5436                 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5437         }
5438         return (ret);
5439 }
5440
5441 int
5442 isp_mstohz(int ms)
5443 {
5444         int hz;
5445         struct timeval t;
5446         t.tv_sec = ms / 1000;
5447         t.tv_usec = (ms % 1000) * 1000;
5448         hz = tvtohz(&t);
5449         if (hz < 0) {
5450                 hz = 0x7fffffff;
5451         }
5452         if (hz == 0) {
5453                 hz = 1;
5454         }
5455         return (hz);
5456 }
5457
5458 void
5459 isp_platform_intr(void *arg)
5460 {
5461         ispsoftc_t *isp = arg;
5462         uint16_t isr, sema, info;
5463
5464         ISP_LOCK(isp);
5465         isp->isp_intcnt++;
5466         if (ISP_READ_ISR(isp, &isr, &sema, &info))
5467                 isp_intr(isp, isr, sema, info);
5468         else
5469                 isp->isp_intbogus++;
5470         ISP_UNLOCK(isp);
5471 }
5472
5473 void
5474 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5475 {
5476         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5477                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5478         } else {
5479                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5480         }
5481         bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5482 }
5483
5484 /*
5485  * Reset the command reference number for all LUNs on a specific target
5486  * (needed when a target arrives again) or for all targets on a port
5487  * (needed for events like a LIP).
5488  */
5489 void
5490 isp_fcp_reset_crn(struct isp_fc *fc, uint32_t tgt, int tgt_set)
5491 {
5492         int i;
5493         struct isp_nexus *nxp;
5494
5495         if (tgt_set == 0)
5496                 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN on all targets");
5497         else
5498                 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN target %u", tgt);
5499
5500         for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
5501                 nxp = fc->nexus_hash[i];
5502                 while (nxp) {
5503                         if ((tgt_set != 0) && (tgt == nxp->tgt))
5504                                 nxp->crnseed = 0;
5505
5506                         nxp = nxp->next;
5507                 }
5508         }
5509 }
5510
5511 int
5512 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
5513 {
5514         lun_id_t lun;
5515         uint32_t chan, tgt;
5516         struct isp_fc *fc;
5517         struct isp_nexus *nxp;
5518         int idx;
5519
5520         if (isp->isp_type < ISP_HA_FC_2300)
5521                 return (0);
5522
5523         chan = XS_CHANNEL(cmd);
5524         tgt = XS_TGT(cmd);
5525         lun = XS_LUN(cmd);
5526         fc = &isp->isp_osinfo.pc.fc[chan];
5527         idx = NEXUS_HASH(tgt, lun);
5528         nxp = fc->nexus_hash[idx];
5529
5530         while (nxp) {
5531                 if (nxp->tgt == tgt && nxp->lun == lun)
5532                         break;
5533                 nxp = nxp->next;
5534         }
5535         if (nxp == NULL) {
5536                 nxp = fc->nexus_free_list;
5537                 if (nxp == NULL) {
5538                         nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
5539                         if (nxp == NULL) {
5540                                 return (-1);
5541                         }
5542                 } else {
5543                         fc->nexus_free_list = nxp->next;
5544                 }
5545                 nxp->tgt = tgt;
5546                 nxp->lun = lun;
5547                 nxp->next = fc->nexus_hash[idx];
5548                 fc->nexus_hash[idx] = nxp;
5549         }
5550         if (nxp) {
5551                 if (nxp->crnseed == 0)
5552                         nxp->crnseed = 1;
5553                 if (cmd)
5554                         PISP_PCMD(cmd)->crn = nxp->crnseed;
5555                 *crnp = nxp->crnseed++;
5556                 return (0);
5557         }
5558         return (-1);
5559 }
5560
5561 /*
5562  * We enter with the lock held
5563  */
5564 void
5565 isp_timer(void *arg)
5566 {
5567         ispsoftc_t *isp = arg;
5568 #ifdef  ISP_TARGET_MODE
5569         isp_tmcmd_restart(isp);
5570 #endif
5571         callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
5572 }
5573
5574 isp_ecmd_t *
5575 isp_get_ecmd(ispsoftc_t *isp)
5576 {
5577         isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
5578         if (ecmd) {
5579                 isp->isp_osinfo.ecmd_free = ecmd->next;
5580         }
5581         return (ecmd);
5582 }
5583
5584 void
5585 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
5586 {
5587         ecmd->next = isp->isp_osinfo.ecmd_free;
5588         isp->isp_osinfo.ecmd_free = ecmd;
5589 }