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