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