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