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