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