]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/isp/isp_freebsd.c
MFC r228461:
[FreeBSD/stable/9.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 #include <dev/isp/isp_freebsd.h>
33 #include <sys/unistd.h>
34 #include <sys/kthread.h>
35 #include <sys/conf.h>
36 #include <sys/module.h>
37 #include <sys/ioccom.h>
38 #include <dev/isp/isp_ioctl.h>
39 #include <sys/devicestat.h>
40 #include <cam/cam_periph.h>
41 #include <cam/cam_xpt_periph.h>
42
43 #if     __FreeBSD_version < 800002 
44 #define THREAD_CREATE   kthread_create
45 #else
46 #define THREAD_CREATE   kproc_create
47 #endif
48
49 MODULE_VERSION(isp, 1);
50 MODULE_DEPEND(isp, cam, 1, 1, 1);
51 int isp_announced = 0;
52 int isp_fabric_hysteresis = 5;
53 int isp_loop_down_limit = 60;   /* default loop down limit */
54 int isp_change_is_bad = 0;      /* "changed" devices are bad */
55 int isp_quickboot_time = 7;     /* don't wait more than N secs for loop up */
56 int isp_gone_device_time = 30;  /* grace time before reporting device lost */
57 int isp_autoconfig = 1;         /* automatically attach/detach devices */
58 static const char *roles[4] = {
59     "(none)", "Target", "Initiator", "Target/Initiator"
60 };
61 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
62 static const char rqo[] = "%s: Request Queue Overflow\n";
63
64 static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
65 static d_ioctl_t ispioctl;
66 static void isp_intr_enable(void *);
67 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
68 static void isp_poll(struct cam_sim *);
69 static timeout_t isp_watchdog;
70 static timeout_t isp_gdt;
71 static task_fn_t isp_gdt_task;
72 static timeout_t isp_ldt;
73 static task_fn_t isp_ldt_task;
74 static void isp_kthread(void *);
75 static void isp_action(struct cam_sim *, union ccb *);
76 #ifdef  ISP_INTERNAL_TARGET
77 static void isp_target_thread_pi(void *);
78 static void isp_target_thread_fc(void *);
79 #endif
80 static void isp_timer(void *);
81
82 static struct cdevsw isp_cdevsw = {
83         .d_version =    D_VERSION,
84         .d_ioctl =      ispioctl,
85         .d_name =       "isp",
86 };
87
88 static int
89 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
90 {
91         struct ccb_setasync csa;
92         struct cam_sim *sim;
93         struct cam_path *path;
94
95         /*
96          * Construct our SIM entry.
97          */
98         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);
99
100         if (sim == NULL) {
101                 return (ENOMEM);
102         }
103
104         ISP_LOCK(isp);
105         if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
106                 ISP_UNLOCK(isp);
107                 cam_sim_free(sim, FALSE);
108                 return (EIO);
109         }
110         ISP_UNLOCK(isp);
111         if (xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
112                 ISP_LOCK(isp);
113                 xpt_bus_deregister(cam_sim_path(sim));
114                 ISP_UNLOCK(isp);
115                 cam_sim_free(sim, FALSE);
116                 return (ENXIO);
117         }
118         xpt_setup_ccb(&csa.ccb_h, path, 5);
119         csa.ccb_h.func_code = XPT_SASYNC_CB;
120         csa.event_enable = AC_LOST_DEVICE;
121         csa.callback = isp_cam_async;
122         csa.callback_arg = sim;
123         xpt_action((union ccb *)&csa);
124
125         if (IS_SCSI(isp)) {
126                 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
127                 spi->sim = sim;
128                 spi->path = path;
129 #ifdef  ISP_INTERNAL_TARGET
130                 ISP_SET_PC(isp, chan, proc_active, 1);
131                 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
132                         ISP_SET_PC(isp, chan, proc_active, 0);
133                         isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
134                 }
135 #endif
136         } else {
137                 fcparam *fcp = FCPARAM(isp, chan);
138                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
139
140                 ISP_LOCK(isp);
141                 fc->sim = sim;
142                 fc->path = path;
143                 fc->isp = isp;
144                 fc->ready = 1;
145
146                 callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
147                 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
148                 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
149                 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
150
151                 /*
152                  * We start by being "loop down" if we have an initiator role
153                  */
154                 if (fcp->role & ISP_ROLE_INITIATOR) {
155                         isp_freeze_loopdown(isp, chan, "isp_attach");
156                         callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
157                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
158                 }
159                 ISP_UNLOCK(isp);
160                 if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
161                         xpt_free_path(fc->path);
162                         ISP_LOCK(isp);
163                         if (callout_active(&fc->ldt)) {
164                                 callout_stop(&fc->ldt);
165                         }
166                         xpt_bus_deregister(cam_sim_path(fc->sim));
167                         ISP_UNLOCK(isp);
168                         cam_sim_free(fc->sim, FALSE);
169                         return (ENOMEM);
170                 }
171 #ifdef  ISP_INTERNAL_TARGET
172                 ISP_SET_PC(isp, chan, proc_active, 1);
173                 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
174                         ISP_SET_PC(isp, chan, proc_active, 0);
175                         isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
176                 }
177 #endif
178         }
179         return (0);
180 }
181
182 int
183 isp_attach(ispsoftc_t *isp)
184 {
185         const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
186         int du = device_get_unit(isp->isp_dev);
187         int chan;
188
189         isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
190         isp->isp_osinfo.ehook.ich_arg = isp;
191         /*
192          * Haha. Set this first, because if we're loaded as a module isp_intr_enable
193          * will be called right awawy, which will clear isp_osinfo.ehook_active,
194          * which would be unwise to then set again later.
195          */
196         isp->isp_osinfo.ehook_active = 1;
197         if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
198                 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
199                 return (-EIO);
200         }
201
202         /*
203          * Create the device queue for our SIM(s).
204          */
205         isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
206         if (isp->isp_osinfo.devq == NULL) {
207                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
208                 return (EIO);
209         }
210
211         for (chan = 0; chan < isp->isp_nchan; chan++) {
212                 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
213                         goto unwind;
214                 }
215         }
216
217         callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
218         callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
219         isp->isp_osinfo.timer_active = 1;
220
221         isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
222         if (isp->isp_osinfo.cdev) {
223                 isp->isp_osinfo.cdev->si_drv1 = isp;
224         }
225         return (0);
226
227 unwind:
228         while (--chan >= 0) {
229                 struct cam_sim *sim;
230                 struct cam_path *path;
231                 if (IS_FC(isp)) {
232                         sim = ISP_FC_PC(isp, chan)->sim;
233                         path = ISP_FC_PC(isp, chan)->path;
234                 } else {
235                         sim = ISP_SPI_PC(isp, chan)->sim;
236                         path = ISP_SPI_PC(isp, chan)->path;
237                 }
238                 xpt_free_path(path);
239                 ISP_LOCK(isp);
240                 xpt_bus_deregister(cam_sim_path(sim));
241                 ISP_UNLOCK(isp);
242                 cam_sim_free(sim, FALSE);
243         }
244         if (isp->isp_osinfo.ehook_active) {
245                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
246                 isp->isp_osinfo.ehook_active = 0;
247         }
248         if (isp->isp_osinfo.cdev) {
249                 destroy_dev(isp->isp_osinfo.cdev);
250                 isp->isp_osinfo.cdev = NULL;
251         }
252         cam_simq_free(isp->isp_osinfo.devq);
253         isp->isp_osinfo.devq = NULL;
254         return (-1);
255 }
256
257 int
258 isp_detach(ispsoftc_t *isp)
259 {
260         struct cam_sim *sim;
261         struct cam_path *path;
262         struct ccb_setasync csa;
263         int chan;
264
265         ISP_LOCK(isp);
266         for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
267                 if (IS_FC(isp)) {
268                         sim = ISP_FC_PC(isp, chan)->sim;
269                         path = ISP_FC_PC(isp, chan)->path;
270                 } else {
271                         sim = ISP_SPI_PC(isp, chan)->sim;
272                         path = ISP_SPI_PC(isp, chan)->path;
273                 }
274                 if (sim->refcount > 2) {
275                         ISP_UNLOCK(isp);
276                         return (EBUSY);
277                 }
278         }
279         if (isp->isp_osinfo.timer_active) {
280                 callout_stop(&isp->isp_osinfo.tmo);
281                 isp->isp_osinfo.timer_active = 0;
282         }
283         for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
284                 if (IS_FC(isp)) {
285                         sim = ISP_FC_PC(isp, chan)->sim;
286                         path = ISP_FC_PC(isp, chan)->path;
287                 } else {
288                         sim = ISP_SPI_PC(isp, chan)->sim;
289                         path = ISP_SPI_PC(isp, chan)->path;
290                 }
291                 xpt_setup_ccb(&csa.ccb_h, path, 5);
292                 csa.ccb_h.func_code = XPT_SASYNC_CB;
293                 csa.event_enable = 0;
294                 csa.callback = isp_cam_async;
295                 csa.callback_arg = sim;
296                 xpt_action((union ccb *)&csa);
297                 xpt_free_path(path);
298                 xpt_bus_deregister(cam_sim_path(sim));
299                 cam_sim_free(sim, FALSE);
300         }
301         ISP_UNLOCK(isp);
302         if (isp->isp_osinfo.cdev) {
303                 destroy_dev(isp->isp_osinfo.cdev);
304                 isp->isp_osinfo.cdev = NULL;
305         }
306         if (isp->isp_osinfo.ehook_active) {
307                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
308                 isp->isp_osinfo.ehook_active = 0;
309         }
310         if (isp->isp_osinfo.devq != NULL) {
311                 cam_simq_free(isp->isp_osinfo.devq);
312                 isp->isp_osinfo.devq = NULL;
313         }
314         return (0);
315 }
316
317 static void
318 isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
319 {
320         if (IS_FC(isp)) {
321                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
322                 if (fc->simqfrozen == 0) {
323                         isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
324                         fc->simqfrozen = SIMQFRZ_LOOPDOWN;
325                         xpt_freeze_simq(fc->sim, 1);
326                 } else {
327                         isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
328                         fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
329                 }
330         }
331 }
332
333 static void
334 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
335 {
336         if (IS_FC(isp)) {
337                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
338                 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
339                 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
340                 if (wasfrozen && fc->simqfrozen == 0) {
341                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
342                         xpt_release_simq(fc->sim, 1);
343                 }
344         }
345 }
346
347
348 static int
349 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
350 {
351         ispsoftc_t *isp;
352         int nr, chan, retval = ENOTTY;
353
354         isp = dev->si_drv1;
355         
356         switch (c) {
357         case ISP_SDBLEV:
358         {
359                 int olddblev = isp->isp_dblev;
360                 isp->isp_dblev = *(int *)addr;
361                 *(int *)addr = olddblev;
362                 retval = 0;
363                 break;
364         }
365         case ISP_GETROLE:
366                 chan = *(int *)addr;
367                 if (chan < 0 || chan >= isp->isp_nchan) {
368                         retval = -ENXIO;
369                         break;
370                 }
371                 if (IS_FC(isp)) {
372                         *(int *)addr = FCPARAM(isp, chan)->role;
373                 } else {
374                         *(int *)addr = SDPARAM(isp, chan)->role;
375                 }
376                 retval = 0;
377                 break;
378         case ISP_SETROLE:
379                 nr = *(int *)addr;
380                 chan = nr >> 8;
381                 if (chan < 0 || chan >= isp->isp_nchan) {
382                         retval = -ENXIO;
383                         break;
384                 }
385                 nr &= 0xff;
386                 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
387                         retval = EINVAL;
388                         break;
389                 }
390                 if (IS_FC(isp)) {
391                         /*
392                          * We don't really support dual role at present on FC cards.
393                          *
394                          * We should, but a bunch of things are currently broken,
395                          * so don't allow it.
396                          */
397                         if (nr == ISP_ROLE_BOTH) {
398                                 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
399                                 retval = EINVAL;
400                                 break;
401                         }
402                         *(int *)addr = FCPARAM(isp, chan)->role;
403 #ifdef  ISP_INTERNAL_TARGET
404                         ISP_LOCK(isp);
405                         retval = isp_fc_change_role(isp, chan, nr);
406                         ISP_UNLOCK(isp);
407 #else
408                         FCPARAM(isp, chan)->role = nr;
409 #endif
410                 } else {
411                         *(int *)addr = SDPARAM(isp, chan)->role;
412                         SDPARAM(isp, chan)->role = nr;
413                 }
414                 retval = 0;
415                 break;
416
417         case ISP_RESETHBA:
418                 ISP_LOCK(isp);
419 #ifdef  ISP_TARGET_MODE
420                 isp_del_all_wwn_entries(isp, ISP_NOCHAN);
421 #endif
422                 isp_reinit(isp, 0);
423                 ISP_UNLOCK(isp);
424                 retval = 0;
425                 break;
426
427         case ISP_RESCAN:
428                 if (IS_FC(isp)) {
429                         chan = *(int *)addr;
430                         if (chan < 0 || chan >= isp->isp_nchan) {
431                                 retval = -ENXIO;
432                                 break;
433                         }
434                         ISP_LOCK(isp);
435                         if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
436                                 retval = EIO;
437                         } else {
438                                 retval = 0;
439                         }
440                         ISP_UNLOCK(isp);
441                 }
442                 break;
443
444         case ISP_FC_LIP:
445                 if (IS_FC(isp)) {
446                         chan = *(int *)addr;
447                         if (chan < 0 || chan >= isp->isp_nchan) {
448                                 retval = -ENXIO;
449                                 break;
450                         }
451                         ISP_LOCK(isp);
452                         if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
453                                 retval = EIO;
454                         } else {
455                                 retval = 0;
456                         }
457                         ISP_UNLOCK(isp);
458                 }
459                 break;
460         case ISP_FC_GETDINFO:
461         {
462                 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
463                 fcportdb_t *lp;
464
465                 if (IS_SCSI(isp)) {
466                         break;
467                 }
468                 if (ifc->loopid >= MAX_FC_TARG) {
469                         retval = EINVAL;
470                         break;
471                 }
472                 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
473                 if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
474                         ifc->role = lp->roles;
475                         ifc->loopid = lp->handle;
476                         ifc->portid = lp->portid;
477                         ifc->node_wwn = lp->node_wwn;
478                         ifc->port_wwn = lp->port_wwn;
479                         retval = 0;
480                 } else {
481                         retval = ENODEV;
482                 }
483                 break;
484         }
485         case ISP_GET_STATS:
486         {
487                 isp_stats_t *sp = (isp_stats_t *) addr;
488
489                 ISP_MEMZERO(sp, sizeof (*sp));
490                 sp->isp_stat_version = ISP_STATS_VERSION;
491                 sp->isp_type = isp->isp_type;
492                 sp->isp_revision = isp->isp_revision;
493                 ISP_LOCK(isp);
494                 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
495                 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
496                 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
497                 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
498                 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
499                 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
500                 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
501                 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
502                 ISP_UNLOCK(isp);
503                 retval = 0;
504                 break;
505         }
506         case ISP_CLR_STATS:
507                 ISP_LOCK(isp);
508                 isp->isp_intcnt = 0;
509                 isp->isp_intbogus = 0;
510                 isp->isp_intmboxc = 0;
511                 isp->isp_intoasync = 0;
512                 isp->isp_rsltccmplt = 0;
513                 isp->isp_fphccmplt = 0;
514                 isp->isp_rscchiwater = 0;
515                 isp->isp_fpcchiwater = 0;
516                 ISP_UNLOCK(isp);
517                 retval = 0;
518                 break;
519         case ISP_FC_GETHINFO:
520         {
521                 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
522                 int chan = hba->fc_channel;
523
524                 if (chan < 0 || chan >= isp->isp_nchan) {
525                         retval = ENXIO;
526                         break;
527                 }
528                 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
529                 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
530                 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
531                 hba->fc_nchannels = isp->isp_nchan;
532                 if (IS_FC(isp)) {
533                         hba->fc_nports = MAX_FC_TARG;
534                         hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
535                         hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
536                         hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
537                         hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
538                         hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
539                         hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
540                         hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
541                 } else {
542                         hba->fc_nports = MAX_TARGETS;
543                         hba->fc_speed = 0;
544                         hba->fc_topology = 0;
545                         hba->nvram_node_wwn = 0ull;
546                         hba->nvram_port_wwn = 0ull;
547                         hba->active_node_wwn = 0ull;
548                         hba->active_port_wwn = 0ull;
549                 }
550                 retval = 0;
551                 break;
552         }
553         case ISP_TSK_MGMT:
554         {
555                 int needmarker;
556                 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
557                 uint16_t loopid;
558                 mbreg_t mbs;
559
560                 if (IS_SCSI(isp)) {
561                         break;
562                 }
563
564                 chan = fct->chan;
565                 if (chan < 0 || chan >= isp->isp_nchan) {
566                         retval = -ENXIO;
567                         break;
568                 }
569
570                 needmarker = retval = 0;
571                 loopid = fct->loopid;
572                 ISP_LOCK(isp);
573                 if (IS_24XX(isp)) {
574                         uint8_t local[QENTRY_LEN];
575                         isp24xx_tmf_t *tmf;
576                         isp24xx_statusreq_t *sp;
577                         fcparam *fcp = FCPARAM(isp, chan);
578                         fcportdb_t *lp;
579                         int i;
580
581                         for (i = 0; i < MAX_FC_TARG; i++) {
582                                 lp = &fcp->portdb[i];
583                                 if (lp->handle == loopid) {
584                                         break;
585                                 }
586                         }
587                         if (i == MAX_FC_TARG) {
588                                 retval = ENXIO;
589                                 ISP_UNLOCK(isp);
590                                 break;
591                         }
592                         /* XXX VALIDATE LP XXX */
593                         tmf = (isp24xx_tmf_t *) local;
594                         ISP_MEMZERO(tmf, QENTRY_LEN);
595                         tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
596                         tmf->tmf_header.rqs_entry_count = 1;
597                         tmf->tmf_nphdl = lp->handle;
598                         tmf->tmf_delay = 2;
599                         tmf->tmf_timeout = 2;
600                         tmf->tmf_tidlo = lp->portid;
601                         tmf->tmf_tidhi = lp->portid >> 16;
602                         tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
603                         tmf->tmf_lun[1] = fct->lun & 0xff;
604                         if (fct->lun >= 256) {
605                                 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
606                         }
607                         switch (fct->action) {
608                         case IPT_CLEAR_ACA:
609                                 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
610                                 break;
611                         case IPT_TARGET_RESET:
612                                 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
613                                 needmarker = 1;
614                                 break;
615                         case IPT_LUN_RESET:
616                                 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
617                                 needmarker = 1;
618                                 break;
619                         case IPT_CLEAR_TASK_SET:
620                                 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
621                                 needmarker = 1;
622                                 break;
623                         case IPT_ABORT_TASK_SET:
624                                 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
625                                 needmarker = 1;
626                                 break;
627                         default:
628                                 retval = EINVAL;
629                                 break;
630                         }
631                         if (retval) {
632                                 ISP_UNLOCK(isp);
633                                 break;
634                         }
635                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
636                         mbs.param[1] = QENTRY_LEN;
637                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
638                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
639                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
640                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
641
642                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
643                                 ISP_UNLOCK(isp);
644                                 retval = ENOMEM;
645                                 break;
646                         }
647                         isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
648                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
649                         sp = (isp24xx_statusreq_t *) local;
650                         sp->req_completion_status = 1;
651                         retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
652                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
653                         isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
654                         FC_SCRATCH_RELEASE(isp, chan);
655                         if (retval || sp->req_completion_status != 0) {
656                                 FC_SCRATCH_RELEASE(isp, chan);
657                                 retval = EIO;
658                         }
659                         if (retval == 0) {
660                                 if (needmarker) {
661                                         fcp->sendmarker = 1;
662                                 }
663                         }
664                 } else {
665                         MBSINIT(&mbs, 0, MBLOGALL, 0);
666                         if (ISP_CAP_2KLOGIN(isp) == 0) {
667                                 loopid <<= 8;
668                         }
669                         switch (fct->action) {
670                         case IPT_CLEAR_ACA:
671                                 mbs.param[0] = MBOX_CLEAR_ACA;
672                                 mbs.param[1] = loopid;
673                                 mbs.param[2] = fct->lun;
674                                 break;
675                         case IPT_TARGET_RESET:
676                                 mbs.param[0] = MBOX_TARGET_RESET;
677                                 mbs.param[1] = loopid;
678                                 needmarker = 1;
679                                 break;
680                         case IPT_LUN_RESET:
681                                 mbs.param[0] = MBOX_LUN_RESET;
682                                 mbs.param[1] = loopid;
683                                 mbs.param[2] = fct->lun;
684                                 needmarker = 1;
685                                 break;
686                         case IPT_CLEAR_TASK_SET:
687                                 mbs.param[0] = MBOX_CLEAR_TASK_SET;
688                                 mbs.param[1] = loopid;
689                                 mbs.param[2] = fct->lun;
690                                 needmarker = 1;
691                                 break;
692                         case IPT_ABORT_TASK_SET:
693                                 mbs.param[0] = MBOX_ABORT_TASK_SET;
694                                 mbs.param[1] = loopid;
695                                 mbs.param[2] = fct->lun;
696                                 needmarker = 1;
697                                 break;
698                         default:
699                                 retval = EINVAL;
700                                 break;
701                         }
702                         if (retval == 0) {
703                                 if (needmarker) {
704                                         FCPARAM(isp, chan)->sendmarker = 1;
705                                 }
706                                 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
707                                 if (retval) {
708                                         retval = EIO;
709                                 }
710                         }
711                 }
712                 ISP_UNLOCK(isp);
713                 break;
714         }
715         default:
716                 break;
717         }
718         return (retval);
719 }
720
721 static void
722 isp_intr_enable(void *arg)
723 {
724         int chan;
725         ispsoftc_t *isp = arg;
726         ISP_LOCK(isp);
727         for (chan = 0; chan < isp->isp_nchan; chan++) {
728                 if (IS_FC(isp)) {
729                         if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
730                                 ISP_ENABLE_INTS(isp);
731                                 break;
732                         }
733                 } else {
734                         if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
735                                 ISP_ENABLE_INTS(isp);
736                                 break;
737                         }
738                 }
739         }
740         isp->isp_osinfo.ehook_active = 0;
741         ISP_UNLOCK(isp);
742         /* Release our hook so that the boot can continue. */
743         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
744 }
745
746 /*
747  * Local Inlines
748  */
749
750 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
751 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
752
753 static ISP_INLINE int
754 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
755 {
756         ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
757         if (ISP_PCMD(ccb) == NULL) {
758                 return (-1);
759         }
760         isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
761         return (0);
762 }
763
764 static ISP_INLINE void
765 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
766 {
767         ((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
768         isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
769         ISP_PCMD(ccb) = NULL;
770 }
771 /*
772  * Put the target mode functions here, because some are inlines
773  */
774
775 #ifdef  ISP_TARGET_MODE
776 static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
777 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
778 static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
779 static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
780 static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
781 static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
782 static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
783 static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
784 static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
785 static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
786 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
787 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
788 static void isp_enable_lun(ispsoftc_t *, union ccb *);
789 static void isp_enable_deferred_luns(ispsoftc_t *, int);
790 static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
791 static void isp_disable_lun(ispsoftc_t *, union ccb *);
792 static int isp_enable_target_mode(ispsoftc_t *, int);
793 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
794 static timeout_t isp_refire_putback_atio;
795 static void isp_complete_ctio(union ccb *);
796 static void isp_target_putback_atio(union ccb *);
797 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
798 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
799 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
800 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
801 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
802 static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
803 static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
804 static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
805 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
806 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
807 static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
808 static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
809
810 static ISP_INLINE int
811 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
812 {
813         tstate_t *tptr;
814         struct tslist *lhp;
815
816         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
817         SLIST_FOREACH(tptr, lhp, next) {
818                 if (xpt_path_lun_id(tptr->owner) == lun) {
819                         return (1);
820                 }
821         }
822         return (0);
823 }
824
825 static void
826 dump_tstates(ispsoftc_t *isp, int bus)
827 {
828         int i, j;
829         struct tslist *lhp;
830         tstate_t *tptr = NULL;
831
832         if (bus >= isp->isp_nchan) {
833                 return;
834         }
835         for (i = 0; i < LUN_HASH_SIZE; i++) {
836                 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
837                 j = 0;
838                 SLIST_FOREACH(tptr, lhp, next) {
839                         xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
840                         j++;
841                 }
842         }
843 }
844
845 static ISP_INLINE tstate_t *
846 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
847 {
848         tstate_t *tptr = NULL;
849         struct tslist *lhp;
850         int i;
851
852         if (bus < isp->isp_nchan) {
853                 for (i = 0; i < LUN_HASH_SIZE; i++) {
854                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
855                         SLIST_FOREACH(tptr, lhp, next) {
856                                 if (xpt_path_lun_id(tptr->owner) == lun) {
857                                         tptr->hold++;
858                                         return (tptr);
859                                 }
860                         }
861                 }
862         }
863         return (NULL);
864 }
865
866 static ISP_INLINE tstate_t *
867 get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
868 {
869         tstate_t *tptr = NULL;
870         atio_private_data_t *atp;
871         struct tslist *lhp;
872         int i;
873
874         if (bus < isp->isp_nchan && tagval != 0) {
875                 for (i = 0; i < LUN_HASH_SIZE; i++) {
876                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
877                         SLIST_FOREACH(tptr, lhp, next) {
878                                 atp = isp_get_atpd(isp, tptr, tagval);
879                                 if (atp && atp->tag == tagval) {
880                                         tptr->hold++;
881                                         return (tptr);
882                                 }
883                         }
884                 }
885         }
886         return (NULL);
887 }
888
889 static ISP_INLINE inot_private_data_t *
890 get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
891 {
892         inot_private_data_t *ntp;
893         tstate_t *tptr;
894         struct tslist *lhp;
895         int bus, i;
896
897         for (bus = 0; bus < isp->isp_nchan; bus++) {
898                 for (i = 0; i < LUN_HASH_SIZE; i++) {
899                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
900                         SLIST_FOREACH(tptr, lhp, next) {
901                                 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
902                                 if (ntp) {
903                                         *rslt = tptr;
904                                         tptr->hold++;
905                                         return (ntp);
906                                 }
907                         }
908                 }
909         }
910         return (NULL);
911 }
912 static ISP_INLINE void
913 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
914 {
915         KASSERT((tptr->hold), ("tptr not held"));
916         tptr->hold--;
917 }
918
919 static void
920 isp_tmcmd_restart(ispsoftc_t *isp)
921 {
922         inot_private_data_t *ntp;
923         tstate_t *tptr;
924         struct tslist *lhp;
925         int bus, i;
926
927         for (bus = 0; bus < isp->isp_nchan; bus++) {
928                 for (i = 0; i < LUN_HASH_SIZE; i++) {
929                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
930                         SLIST_FOREACH(tptr, lhp, next) {
931                                 inot_private_data_t *restart_queue = tptr->restart_queue;
932                                 tptr->restart_queue = NULL;
933                                 while (restart_queue) {
934                                         ntp = restart_queue;
935                                         restart_queue = ntp->rd.nt.nt_hba;
936                                         if (IS_24XX(isp)) {
937                                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
938                                                 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
939                                         } else {
940                                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
941                                                 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
942                                         }
943                                         isp_put_ntpd(isp, tptr, ntp);
944                                         if (tptr->restart_queue && restart_queue != NULL) {
945                                                 ntp = tptr->restart_queue;
946                                                 tptr->restart_queue = restart_queue;
947                                                 while (restart_queue->rd.nt.nt_hba) {
948                                                         restart_queue = restart_queue->rd.nt.nt_hba;
949                                                 }
950                                                 restart_queue->rd.nt.nt_hba = ntp;
951                                                 break;
952                                         }
953                                 }
954                         }
955                 }
956         }
957 }
958
959 static ISP_INLINE atio_private_data_t *
960 isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
961 {
962         atio_private_data_t *atp;
963
964         if (tag == 0) {
965                 atp = tptr->atfree;
966                 if (atp) {
967                         tptr->atfree = atp->next;
968                 }
969                 return (atp);
970         }
971         for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
972                 if (atp->tag == tag) {
973                         return (atp);
974                 }
975         }
976         return (NULL);
977 }
978
979 static ISP_INLINE void
980 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
981 {
982         atp->tag = 0;
983         atp->dead = 0;
984         atp->next = tptr->atfree;
985         tptr->atfree = atp;
986 }
987
988 static void
989 isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
990 {
991         atio_private_data_t *atp;
992         const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
993
994         for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
995                 if (atp->tag == 0) {
996                         continue;
997                 }
998                 xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u last_xfr %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
999                     atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->last_xframt, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1000         }
1001 }
1002
1003
1004 static ISP_INLINE inot_private_data_t *
1005 isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1006 {
1007         inot_private_data_t *ntp;
1008         ntp = tptr->ntfree;
1009         if (ntp) {
1010                 tptr->ntfree = ntp->next;
1011         }
1012         return (ntp);
1013 }
1014
1015 static ISP_INLINE inot_private_data_t *
1016 isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1017 {
1018         inot_private_data_t *ntp;
1019         for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1020                 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1021                         return (ntp);
1022                 }
1023         }
1024         return (NULL);
1025 }
1026
1027 static ISP_INLINE void
1028 isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1029 {
1030         ntp->rd.tag_id = ntp->rd.seq_id = 0;
1031         ntp->next = tptr->ntfree;
1032         tptr->ntfree = ntp;
1033 }
1034
1035 static cam_status
1036 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1037 {
1038         cam_status status;
1039         lun_id_t lun;
1040         struct tslist *lhp;
1041         tstate_t *tptr;
1042         int i;
1043
1044         lun = xpt_path_lun_id(path);
1045         if (lun != CAM_LUN_WILDCARD) {
1046                 if (lun >= ISP_MAX_LUNS(isp)) {
1047                         return (CAM_LUN_INVALID);
1048                 }
1049         }
1050         if (is_lun_enabled(isp, bus, lun)) {
1051                 return (CAM_LUN_ALRDY_ENA);
1052         }
1053         tptr = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1054         if (tptr == NULL) {
1055                 return (CAM_RESRC_UNAVAIL);
1056         }
1057         status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1058         if (status != CAM_REQ_CMP) {
1059                 free(tptr, M_DEVBUF);
1060                 return (status);
1061         }
1062         SLIST_INIT(&tptr->atios);
1063         SLIST_INIT(&tptr->inots);
1064         for (i = 0; i < ATPDPSIZE-1; i++) {
1065                 tptr->atpool[i].next = &tptr->atpool[i+1];
1066                 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1067         }
1068         tptr->atfree = tptr->atpool;
1069         tptr->ntfree = tptr->ntpool;
1070         tptr->hold = 1;
1071         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1072         SLIST_INSERT_HEAD(lhp, tptr, next);
1073         *rslt = tptr;
1074         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1075         return (CAM_REQ_CMP);
1076 }
1077
1078 static ISP_INLINE void
1079 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1080 {
1081         struct tslist *lhp;
1082         KASSERT((tptr->hold != 0), ("tptr is not held"));
1083         KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1084         ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(xpt_path_lun_id(tptr->owner))], lhp);
1085         SLIST_REMOVE(lhp, tptr, tstate, next);
1086         xpt_free_path(tptr->owner);
1087         free(tptr, M_DEVBUF);
1088 }
1089
1090 /*
1091  * Enable a lun.
1092  */
1093 static void
1094 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1095 {
1096         tstate_t *tptr = NULL;
1097         int bus, tm_enabled, target_role;
1098         target_id_t target;
1099         lun_id_t lun;
1100
1101         /*
1102          * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1103          */
1104         bus = XS_CHANNEL(ccb);
1105         target = ccb->ccb_h.target_id;
1106         lun = ccb->ccb_h.target_lun;
1107         if (target != CAM_TARGET_WILDCARD && target != 0) {
1108                 ccb->ccb_h.status = CAM_TID_INVALID;
1109                 xpt_done(ccb);
1110                 return;
1111         }
1112         if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1113                 ccb->ccb_h.status = CAM_LUN_INVALID;
1114                 xpt_done(ccb);
1115                 return;
1116         }
1117
1118         if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1119                 ccb->ccb_h.status = CAM_LUN_INVALID;
1120                 xpt_done(ccb);
1121                 return;
1122         }
1123         if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1124                 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1125         }
1126
1127         /*
1128          * Wait until we're not busy with the lun enables subsystem
1129          */
1130         while (isp->isp_osinfo.tmbusy) {
1131                 isp->isp_osinfo.tmwanted = 1;
1132                 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_enable_lun", 0);
1133         }
1134         isp->isp_osinfo.tmbusy = 1;
1135
1136         /*
1137          * This is as a good a place as any to check f/w capabilities.
1138          */
1139
1140         if (IS_FC(isp)) {
1141                 if (ISP_CAP_TMODE(isp) == 0) {
1142                         xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1143                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1144                         goto done;
1145                 }
1146                 /*
1147                  * We *could* handle non-SCCLUN f/w, but we'd have to
1148                  * dork with our already fragile enable/disable code.
1149                  */
1150                 if (ISP_CAP_SCCFW(isp) == 0) {
1151                         xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1152                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1153                         goto done;
1154                 }
1155
1156                 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1157
1158         } else {
1159                 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1160         }
1161
1162         /*
1163          * Create the state pointer.
1164          * It should not already exist.
1165          */
1166         tptr = get_lun_statep(isp, bus, lun);
1167         if (tptr) {
1168                 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1169                 goto done;
1170         }
1171         ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1172         if (ccb->ccb_h.status != CAM_REQ_CMP) {
1173                 goto done;
1174         }
1175
1176         /*
1177          * We have a tricky maneuver to perform here.
1178          *
1179          * If target mode isn't already enabled here,
1180          * *and* our current role includes target mode,
1181          * we enable target mode here.
1182          *
1183          */
1184         ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1185         if (tm_enabled == 0 && target_role != 0) {
1186                 if (isp_enable_target_mode(isp, bus)) {
1187                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1188                         destroy_lun_state(isp, tptr);
1189                         tptr = NULL;
1190                         goto done;
1191                 }
1192                 tm_enabled = 1;
1193         }
1194
1195         /*
1196          * Now check to see whether this bus is in target mode already.
1197          *
1198          * If not, a later role change into target mode will finish the job.
1199          */
1200         if (tm_enabled == 0) {
1201                 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1202                 ccb->ccb_h.status = CAM_REQ_CMP;
1203                 xpt_print(ccb->ccb_h.path, "Target Mode Not Enabled Yet- Lun Enables Deferred\n");
1204                 goto done;
1205         }
1206
1207         /*
1208          * Enable the lun.
1209          */
1210         ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1211
1212 done:
1213         if (ccb->ccb_h.status != CAM_REQ_CMP && tptr) {
1214                 destroy_lun_state(isp, tptr);
1215                 tptr = NULL;
1216         }
1217         if (tptr) {
1218                 rls_lun_statep(isp, tptr);
1219         }
1220         isp->isp_osinfo.tmbusy = 0;
1221         if (isp->isp_osinfo.tmwanted) {
1222                 isp->isp_osinfo.tmwanted = 0;
1223                 wakeup(isp);
1224         }
1225         xpt_done(ccb);
1226 }
1227
1228 static void
1229 isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1230 {
1231         /*
1232          * XXX: not entirely implemented yet
1233          */
1234         (void) isp_enable_deferred(isp, bus, 0);
1235 }
1236
1237 static uint32_t
1238 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1239 {
1240         cam_status status;
1241
1242         isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
1243         if (IS_24XX(isp) || (IS_FC(isp) && ISP_FC_PC(isp, bus)->tm_luns_enabled)) {
1244                 status = CAM_REQ_CMP;
1245         } else {
1246                 int cmd_cnt, not_cnt;
1247
1248                 if (IS_23XX(isp)) {
1249                         cmd_cnt = DFLT_CMND_CNT;
1250                         not_cnt = DFLT_INOT_CNT;
1251                 } else {
1252                         cmd_cnt = 64;
1253                         not_cnt = 8;
1254                 }
1255                 status = CAM_REQ_INPROG;
1256                 isp->isp_osinfo.rptr = &status;
1257                 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
1258                         status = CAM_RESRC_UNAVAIL;
1259                 } else {
1260                         mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1261                 }
1262                 isp->isp_osinfo.rptr = NULL;
1263         }
1264
1265         if (status == CAM_REQ_CMP) {
1266                 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1267                 isp_prt(isp, ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1268         }
1269         return (status);
1270 }
1271
1272 static void
1273 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1274 {
1275         tstate_t *tptr = NULL;
1276         int bus;
1277         cam_status status;
1278         target_id_t target;
1279         lun_id_t lun;
1280
1281         bus = XS_CHANNEL(ccb);
1282         target = ccb->ccb_h.target_id;
1283         lun = ccb->ccb_h.target_lun;
1284         if (target != CAM_TARGET_WILDCARD && target != 0) {
1285                 ccb->ccb_h.status = CAM_TID_INVALID;
1286                 xpt_done(ccb);
1287                 return;
1288         }
1289         if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1290                 ccb->ccb_h.status = CAM_LUN_INVALID;
1291                 xpt_done(ccb);
1292                 return;
1293         }
1294
1295         if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1296                 ccb->ccb_h.status = CAM_LUN_INVALID;
1297                 xpt_done(ccb);
1298                 return;
1299         }
1300         if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1301                 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1302         }
1303
1304         /*
1305          * See if we're busy disabling a lun now.
1306          */
1307         while (isp->isp_osinfo.tmbusy) {
1308                 isp->isp_osinfo.tmwanted = 1;
1309                 mtx_sleep(isp, &isp->isp_lock, PRIBIO, "want_isp_disable_lun", 0);
1310         }
1311         isp->isp_osinfo.tmbusy = 1;
1312         status = CAM_REQ_INPROG;
1313
1314         /*
1315          * Find the state pointer.
1316          */
1317         if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1318                 status = CAM_PATH_INVALID;
1319                 goto done;
1320         }
1321
1322         /*
1323          * If we're a 24XX card, we're done.
1324          */
1325         if (IS_24XX(isp)) {
1326                 status = CAM_REQ_CMP;
1327                 goto done;
1328         }
1329
1330         /*
1331          * For SCC FW, we only deal with lun zero.
1332          */
1333         if (IS_FC(isp)) {
1334                 lun = 0;
1335         }
1336
1337         isp->isp_osinfo.rptr = &status;
1338         if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1339                 status = CAM_RESRC_UNAVAIL;
1340         } else {
1341                 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1342         }
1343 done:
1344         ccb->ccb_h.status = status;
1345         if (status == CAM_REQ_CMP) {
1346                 xpt_print(ccb->ccb_h.path, "now disabled for target mode\n");
1347         }
1348         if (tptr) {
1349                 destroy_lun_state(isp, tptr);
1350         }
1351         isp->isp_osinfo.rptr = NULL;
1352         isp->isp_osinfo.tmbusy = 0;
1353         if (isp->isp_osinfo.tmwanted) {
1354                 isp->isp_osinfo.tmwanted = 0;
1355                 wakeup(isp);
1356         }
1357         xpt_done(ccb);
1358 }
1359
1360 static int
1361 isp_enable_target_mode(ispsoftc_t *isp, int bus)
1362 {
1363         int ct;
1364
1365         ISP_GET_PC(isp, bus, tm_enabled, ct);
1366         if (ct != 0) {
1367                 return (0);
1368         }
1369
1370         if (IS_SCSI(isp)) {
1371                 mbreg_t mbs;
1372
1373                 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1374                 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1375                 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1376                 mbs.param[2] = bus << 7;
1377                 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1378                         isp_prt(isp, ISP_LOGERR, "Unable to add Target Role to Bus %d", bus);
1379                         return (EIO);
1380                 }
1381                 SDPARAM(isp, bus)->role |= ISP_ROLE_TARGET;
1382         }
1383         ISP_SET_PC(isp, bus, tm_enabled, 1);
1384         isp_prt(isp, ISP_LOGINFO, "Target Role added to Bus %d", bus);
1385         return (0);
1386 }
1387
1388 #ifdef  NEEDED
1389 static int
1390 isp_disable_target_mode(ispsoftc_t *isp, int bus)
1391 {
1392         int ct;
1393
1394         ISP_GET_PC(isp, bus, tm_enabled, ct);
1395         if (ct == 0) {
1396                 return (0);
1397         }
1398
1399         if (IS_SCSI(isp)) {
1400                 mbreg_t mbs;
1401
1402                 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1403                 mbs.param[2] = bus << 7;
1404                 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1405                         isp_prt(isp, ISP_LOGERR, "Unable to subtract Target Role to Bus %d", bus);
1406                         return (EIO);
1407                 }
1408                 SDPARAM(isp, bus)->role &= ~ISP_ROLE_TARGET;
1409         }
1410         ISP_SET_PC(isp, bus, tm_enabled, 0);
1411         isp_prt(isp, ISP_LOGINFO, "Target Role subtracted from Bus %d", bus);
1412         return (0);
1413 }
1414 #endif
1415
1416 static void
1417 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1418 {
1419         uint32_t *rptr;
1420
1421         rptr = isp->isp_osinfo.rptr;
1422         if (lep->le_status != LUN_OK) {
1423                 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1424                 if (rptr) {
1425                         *rptr = CAM_REQ_CMP_ERR;
1426                         wakeup_one(rptr);
1427                 }
1428         } else {
1429                 if (rptr) {
1430                         *rptr = CAM_REQ_CMP;
1431                         wakeup_one(rptr);
1432                 }
1433         }
1434 }
1435
1436 static void
1437 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1438 {
1439         void *qe;
1440         tstate_t *tptr;
1441         atio_private_data_t *atp;
1442         struct ccb_scsiio *cso = &ccb->csio;
1443         uint32_t dmaresult, handle;
1444         uint8_t local[QENTRY_LEN];
1445
1446         /*
1447          * Do some sanity checks.
1448          */
1449         if (cso->dxfer_len == 0) {
1450                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1451                         xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1452                         ccb->ccb_h.status = CAM_REQ_INVALID;
1453                         xpt_done(ccb);
1454                         return;
1455                 }
1456         }
1457
1458         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1459         if (tptr == NULL) {
1460                 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1461                 if (tptr == NULL) {
1462                         xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
1463                         dump_tstates(isp, XS_CHANNEL(ccb));
1464                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1465                         xpt_done(ccb);
1466                         return;
1467                 }
1468         }
1469
1470         atp = isp_get_atpd(isp, tptr, cso->tag_id);
1471         if (atp == NULL) {
1472                 xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
1473                 isp_dump_atpd(isp, tptr);
1474                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1475                 xpt_done(ccb);
1476                 return;
1477         }
1478         if (atp->dead) {
1479                 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
1480                 ccb->ccb_h.status = CAM_REQ_ABORTED;
1481                 xpt_done(ccb);
1482                 return;
1483         }
1484
1485         /*
1486          * Check to make sure we're still in target mode.
1487          */
1488         if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
1489                 xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
1490                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1491                 xpt_done(ccb);
1492                 return;
1493         }
1494
1495         /*
1496          * Get some resources
1497          */
1498         if (isp_get_pcmd(isp, ccb)) {
1499                 rls_lun_statep(isp, tptr);
1500                 xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
1501                 cam_freeze_devq(ccb->ccb_h.path);
1502                 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1503                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1504                 xpt_done(ccb);
1505                 return;
1506         }
1507         qe = isp_getrqentry(isp);
1508         if (qe == NULL) {
1509                 xpt_print(ccb->ccb_h.path, rqo, __func__);
1510                 cam_freeze_devq(ccb->ccb_h.path);
1511                 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
1512                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1513                 goto out;
1514         }
1515         memset(local, 0, QENTRY_LEN);
1516
1517         /*
1518          * We're either moving data or completing a command here.
1519          */
1520         if (IS_24XX(isp)) {
1521                 ct7_entry_t *cto = (ct7_entry_t *) local;
1522
1523                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1524                 cto->ct_header.rqs_entry_count = 1;
1525                 cto->ct_header.rqs_seqno = 1;
1526                 cto->ct_nphdl = atp->nphdl;
1527                 cto->ct_rxid = atp->tag;
1528                 cto->ct_iid_lo = atp->portid;
1529                 cto->ct_iid_hi = atp->portid >> 16;
1530                 cto->ct_oxid = atp->oxid;
1531                 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1532                 cto->ct_scsi_status = cso->scsi_status;
1533                 cto->ct_timeout = 120;
1534                 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1535                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1536                         cto->ct_flags |= CT7_SENDSTATUS;
1537                 }
1538                 if (cso->dxfer_len == 0) {
1539                         cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
1540                         if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1541                                 int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
1542                                 cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
1543                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
1544                                 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1545                         }
1546                 } else {
1547                         cto->ct_flags |= CT7_FLAG_MODE0;
1548                         if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1549                                 cto->ct_flags |= CT7_DATA_IN;
1550                         } else {
1551                                 cto->ct_flags |= CT7_DATA_OUT;
1552                         }
1553                         cto->rsp.m0.reloff = atp->bytes_xfered;
1554                         /*
1555                          * Don't overrun the limits placed on us
1556                          */
1557                         if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1558                                 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1559                         }
1560                         atp->last_xframt = cso->dxfer_len;
1561                         cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1562                 }
1563                 if (cto->ct_flags & CT7_SENDSTATUS) {
1564                         int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1565                         cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1566                         if (cto->ct_resid < 0) {
1567                                 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1568                         } else if (cto->ct_resid > 0) {
1569                                 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1570                         }
1571                         atp->state = ATPD_STATE_LAST_CTIO;
1572                         ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1573                             atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1574                 } else {
1575                         cto->ct_resid = 0;
1576                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1577                             cso->dxfer_len, atp->bytes_xfered);
1578                         atp->state = ATPD_STATE_CTIO;
1579                 }
1580         } else if (IS_FC(isp)) {
1581                 ct2_entry_t *cto = (ct2_entry_t *) local;
1582
1583                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1584                 cto->ct_header.rqs_entry_count = 1;
1585                 cto->ct_header.rqs_seqno = 1;
1586                 if (ISP_CAP_2KLOGIN(isp) == 0) {
1587                         ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1588                 } else {
1589                         cto->ct_iid = cso->init_id;
1590                         if (ISP_CAP_SCCFW(isp) == 0) {
1591                                 cto->ct_lun = ccb->ccb_h.target_lun;
1592                         }
1593                 }
1594
1595
1596                 cto->ct_rxid = cso->tag_id;
1597                 if (cso->dxfer_len == 0) {
1598                         cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
1599                         cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1600                         cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
1601                         if (cto->ct_resid < 0) {
1602                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1603                         } else if (cto->ct_resid > 0) {
1604                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1605                         }
1606                         if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1607                                 int m = min(cso->sense_len, MAXRESPLEN);
1608                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
1609                                 cto->rsp.m1.ct_senselen = m;
1610                                 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1611                         } else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
1612                                 /*
1613                                  * XXX: DEBUG
1614                                  */
1615                                 xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
1616                         }
1617                 } else {
1618                         cto->ct_flags |= CT2_FLAG_MODE0;
1619                         if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1620                                 cto->ct_flags |= CT2_DATA_IN;
1621                         } else {
1622                                 cto->ct_flags |= CT2_DATA_OUT;
1623                         }
1624                         cto->ct_reloff = atp->bytes_xfered;
1625                         cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
1626                         /*
1627                          * Don't overrun the limits placed on us
1628                          */
1629                         if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
1630                                 cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
1631                         }
1632                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1633                                 cto->ct_flags |= CT2_SENDSTATUS;
1634                                 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1635                                 cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
1636                                 if (cto->ct_resid < 0) {
1637                                         cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
1638                                 } else if (cto->ct_resid > 0) {
1639                                         cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
1640                                 }
1641                         } else {
1642                                 atp->last_xframt = cso->dxfer_len;
1643                         }
1644                         /*
1645                          * If we're sending data and status back together,
1646                          * we can't also send back sense data as well.
1647                          */
1648                         ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1649                 }
1650
1651                 if (cto->ct_flags & CT2_SENDSTATUS) {
1652                         int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
1653                         cto->ct_flags |= CT2_CCINCR;
1654                         atp->state = ATPD_STATE_LAST_CTIO;
1655                         ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
1656                             atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1657                 } else {
1658                         cto->ct_resid = 0;
1659                         atp->state = ATPD_STATE_CTIO;
1660                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
1661                             cso->dxfer_len, atp->bytes_xfered);
1662                 }
1663                 cto->ct_timeout = 10;
1664         } else {
1665                 ct_entry_t *cto = (ct_entry_t *) local;
1666
1667                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1668                 cto->ct_header.rqs_entry_count = 1;
1669                 cto->ct_header.rqs_seqno = 1;
1670                 cto->ct_iid = cso->init_id;
1671                 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1672                 cto->ct_tgt = ccb->ccb_h.target_id;
1673                 cto->ct_lun = ccb->ccb_h.target_lun;
1674                 cto->ct_fwhandle = cso->tag_id >> 16;
1675                 if (AT_HAS_TAG(cso->tag_id)) {
1676                         cto->ct_tag_val = cso->tag_id;
1677                         cto->ct_flags |= CT_TQAE;
1678                 }
1679                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1680                         cto->ct_flags |= CT_NODISC;
1681                 }
1682                 if (cso->dxfer_len == 0) {
1683                         cto->ct_flags |= CT_NO_DATA;
1684                 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1685                         cto->ct_flags |= CT_DATA_IN;
1686                 } else {
1687                         cto->ct_flags |= CT_DATA_OUT;
1688                 }
1689                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1690                         cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1691                         cto->ct_scsi_status = cso->scsi_status;
1692                         cto->ct_resid = cso->resid;
1693                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
1694                             cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
1695                 }
1696                 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1697                 cto->ct_timeout = 10;
1698         }
1699
1700         if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
1701                 xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1702                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1703                 goto out;
1704         }
1705
1706
1707         /*
1708          * Call the dma setup routines for this entry (and any subsequent
1709          * CTIOs) if there's data to move, and then tell the f/w it's got
1710          * new things to play with. As with isp_start's usage of DMA setup,
1711          * any swizzling is done in the machine dependent layer. Because
1712          * of this, we put the request onto the queue area first in native
1713          * format.
1714          */
1715
1716         if (IS_24XX(isp)) {
1717                 ct7_entry_t *cto = (ct7_entry_t *) local;
1718                 cto->ct_syshandle = handle;
1719         } else if (IS_FC(isp)) {
1720                 ct2_entry_t *cto = (ct2_entry_t *) local;
1721                 cto->ct_syshandle = handle;
1722         } else {
1723                 ct_entry_t *cto = (ct_entry_t *) local;
1724                 cto->ct_syshandle = handle;
1725         }
1726
1727         dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1728         if (dmaresult == CMD_QUEUED) {
1729                 isp->isp_nactive++;
1730                 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1731                 rls_lun_statep(isp, tptr);
1732                 return;
1733         }
1734         if (dmaresult == CMD_EAGAIN) {
1735                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1736         } else {
1737                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1738         }
1739         isp_destroy_tgt_handle(isp, handle);
1740 out:
1741         rls_lun_statep(isp, tptr);
1742         isp_free_pcmd(isp, ccb);
1743         xpt_done(ccb);
1744 }
1745
1746 static void
1747 isp_refire_putback_atio(void *arg)
1748 {
1749         union ccb *ccb = arg;
1750         ispsoftc_t *isp = XS_ISP(ccb);
1751         ISP_LOCK(isp);
1752         isp_target_putback_atio(ccb);
1753         ISP_UNLOCK(isp);
1754 }
1755
1756 static void
1757 isp_target_putback_atio(union ccb *ccb)
1758 {
1759         ispsoftc_t *isp;
1760         struct ccb_scsiio *cso;
1761         void *qe;
1762
1763         isp = XS_ISP(ccb);
1764
1765         qe = isp_getrqentry(isp);
1766         if (qe == NULL) {
1767                 xpt_print(ccb->ccb_h.path, rqo, __func__);
1768                 (void) timeout(isp_refire_putback_atio, ccb, 10);
1769                 return;
1770         }
1771         memset(qe, 0, QENTRY_LEN);
1772         cso = &ccb->csio;
1773         if (IS_FC(isp)) {
1774                 at2_entry_t local, *at = &local;
1775                 ISP_MEMZERO(at, sizeof (at2_entry_t));
1776                 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1777                 at->at_header.rqs_entry_count = 1;
1778                 if (ISP_CAP_SCCFW(isp)) {
1779                         at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1780                 } else {
1781                         at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1782                 }
1783                 at->at_status = CT_OK;
1784                 at->at_rxid = cso->tag_id;
1785                 at->at_iid = cso->ccb_h.target_id;
1786                 isp_put_atio2(isp, at, qe);
1787         } else {
1788                 at_entry_t local, *at = &local;
1789                 ISP_MEMZERO(at, sizeof (at_entry_t));
1790                 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1791                 at->at_header.rqs_entry_count = 1;
1792                 at->at_iid = cso->init_id;
1793                 at->at_iid |= XS_CHANNEL(ccb) << 7;
1794                 at->at_tgt = cso->ccb_h.target_id;
1795                 at->at_lun = cso->ccb_h.target_lun;
1796                 at->at_status = CT_OK;
1797                 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1798                 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1799                 isp_put_atio(isp, at, qe);
1800         }
1801         ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
1802         ISP_SYNC_REQUEST(isp);
1803         isp_complete_ctio(ccb);
1804 }
1805
1806 static void
1807 isp_complete_ctio(union ccb *ccb)
1808 {
1809         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1810                 ccb->ccb_h.status |= CAM_REQ_CMP;
1811         }
1812         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1813         isp_free_pcmd(XS_ISP(ccb), ccb);
1814         xpt_done(ccb);
1815 }
1816
1817 /*
1818  * Handle ATIO stuff that the generic code can't.
1819  * This means handling CDBs.
1820  */
1821
1822 static void
1823 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1824 {
1825         tstate_t *tptr;
1826         int status, bus;
1827         struct ccb_accept_tio *atiop;
1828         atio_private_data_t *atp;
1829
1830         /*
1831          * The firmware status (except for the QLTM_SVALID bit)
1832          * indicates why this ATIO was sent to us.
1833          *
1834          * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1835          *
1836          * If the DISCONNECTS DISABLED bit is set in the flags field,
1837          * we're still connected on the SCSI bus.
1838          */
1839         status = aep->at_status;
1840         if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1841                 /*
1842                  * Bus Phase Sequence error. We should have sense data
1843                  * suggested by the f/w. I'm not sure quite yet what
1844                  * to do about this for CAM.
1845                  */
1846                 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1847                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1848                 return;
1849         }
1850         if ((status & ~QLTM_SVALID) != AT_CDB) {
1851                 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
1852                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1853                 return;
1854         }
1855
1856         bus = GET_BUS_VAL(aep->at_iid);
1857         tptr = get_lun_statep(isp, bus, aep->at_lun);
1858         if (tptr == NULL) {
1859                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1860                 if (tptr == NULL) {
1861                         /*
1862                          * Because we can't autofeed sense data back with
1863                          * a command for parallel SCSI, we can't give back
1864                          * a CHECK CONDITION. We'll give back a BUSY status
1865                          * instead. This works out okay because the only
1866                          * time we should, in fact, get this, is in the
1867                          * case that somebody configured us without the
1868                          * blackhole driver, so they get what they deserve.
1869                          */
1870                         isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1871                         return;
1872                 }
1873         }
1874
1875         atp = isp_get_atpd(isp, tptr, 0);
1876         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1877         if (atiop == NULL || atp == NULL) {
1878                 /*
1879                  * Because we can't autofeed sense data back with
1880                  * a command for parallel SCSI, we can't give back
1881                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
1882                  * instead. This works out okay because the only time we
1883                  * should, in fact, get this, is in the case that we've
1884                  * run out of ATIOS.
1885                  */
1886                 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
1887                     ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
1888                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1889                 if (atp) {
1890                         isp_put_atpd(isp, tptr, atp);
1891                 }
1892                 rls_lun_statep(isp, tptr);
1893                 return;
1894         }
1895         atp->tag = aep->at_tag_val;
1896         if (atp->tag == 0) {
1897                 atp->tag = ~0;
1898         }
1899         atp->state = ATPD_STATE_ATIO;
1900         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1901         tptr->atio_count--;
1902         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
1903         atiop->ccb_h.target_id = aep->at_tgt;
1904         atiop->ccb_h.target_lun = aep->at_lun;
1905         if (aep->at_flags & AT_NODISC) {
1906                 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1907         } else {
1908                 atiop->ccb_h.flags = 0;
1909         }
1910
1911         if (status & QLTM_SVALID) {
1912                 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
1913                 atiop->sense_len = amt;
1914                 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1915         } else {
1916                 atiop->sense_len = 0;
1917         }
1918
1919         atiop->init_id = GET_IID_VAL(aep->at_iid);
1920         atiop->cdb_len = aep->at_cdblen;
1921         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1922         atiop->ccb_h.status = CAM_CDB_RECVD;
1923         /*
1924          * Construct a tag 'id' based upon tag value (which may be 0..255)
1925          * and the handle (which we have to preserve).
1926          */
1927         atiop->tag_id = atp->tag;
1928         if (aep->at_flags & AT_TQAE) {
1929                 atiop->tag_action = aep->at_tag_type;
1930                 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1931         }
1932         atp->orig_datalen = 0;
1933         atp->bytes_xfered = 0;
1934         atp->last_xframt = 0;
1935         atp->lun = aep->at_lun;
1936         atp->nphdl = aep->at_iid;
1937         atp->portid = PORT_NONE;
1938         atp->oxid = 0;
1939         atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1940         atp->tattr = aep->at_tag_type;
1941         atp->state = ATPD_STATE_CAM;
1942         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
1943         rls_lun_statep(isp, tptr);
1944 }
1945
1946 static void
1947 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1948 {
1949         lun_id_t lun;
1950         fcportdb_t *lp;
1951         tstate_t *tptr;
1952         struct ccb_accept_tio *atiop;
1953         uint16_t nphdl;
1954         atio_private_data_t *atp;
1955         inot_private_data_t *ntp;
1956
1957         /*
1958          * The firmware status (except for the QLTM_SVALID bit)
1959          * indicates why this ATIO was sent to us.
1960          *
1961          * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1962          */
1963         if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1964                 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1965                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1966                 return;
1967         }
1968
1969         if (ISP_CAP_SCCFW(isp)) {
1970                 lun = aep->at_scclun;
1971         } else {
1972                 lun = aep->at_lun;
1973         }
1974         if (ISP_CAP_2KLOGIN(isp)) {
1975                 nphdl = ((at2e_entry_t *)aep)->at_iid;
1976         } else {
1977                 nphdl = aep->at_iid;
1978         }
1979         tptr = get_lun_statep(isp, 0, lun);
1980         if (tptr == NULL) {
1981                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1982                 if (tptr == NULL) {
1983                         isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1984                         isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1985                         return;
1986                 }
1987         }
1988
1989         /*
1990          * Start any commands pending resources first.
1991          */
1992         if (tptr->restart_queue) {
1993                 inot_private_data_t *restart_queue = tptr->restart_queue;
1994                 tptr->restart_queue = NULL;
1995                 while (restart_queue) {
1996                         ntp = restart_queue;
1997                         restart_queue = ntp->rd.nt.nt_hba;
1998                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1999                         isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2000                         isp_put_ntpd(isp, tptr, ntp);
2001                         /*
2002                          * If a recursion caused the restart queue to start to fill again,
2003                          * stop and splice the new list on top of the old list and restore
2004                          * it and go to noresrc.
2005                          */
2006                         if (tptr->restart_queue) {
2007                                 ntp = tptr->restart_queue;
2008                                 tptr->restart_queue = restart_queue;
2009                                 while (restart_queue->rd.nt.nt_hba) {
2010                                         restart_queue = restart_queue->rd.nt.nt_hba;
2011                                 }
2012                                 restart_queue->rd.nt.nt_hba = ntp;
2013                                 goto noresrc;
2014                         }
2015                 }
2016         }
2017
2018         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2019         if (atiop == NULL) {
2020                 goto noresrc;
2021         }
2022
2023         atp = isp_get_atpd(isp, tptr, 0);
2024         if (atp == NULL) {
2025                 goto noresrc;
2026         }
2027
2028         atp->tag = aep->at_rxid;
2029         atp->state = ATPD_STATE_ATIO;
2030         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2031         tptr->atio_count--;
2032         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2033         atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2034         atiop->ccb_h.target_lun = lun;
2035
2036         /*
2037          * We don't get 'suggested' sense data as we do with SCSI cards.
2038          */
2039         atiop->sense_len = 0;
2040         if (ISP_CAP_2KLOGIN(isp)) {
2041                 /*
2042                  * NB: We could not possibly have 2K logins if we
2043                  * NB: also did not have SCC FW.
2044                  */
2045                 atiop->init_id = ((at2e_entry_t *)aep)->at_iid;
2046         } else {
2047                 atiop->init_id = aep->at_iid;
2048         }
2049
2050         /*
2051          * If we're not in the port database, add ourselves.
2052          */
2053         if (!IS_2100(isp) && isp_find_pdb_by_loopid(isp, 0, atiop->init_id, &lp) == 0) {
2054                 uint64_t iid =
2055                         (((uint64_t) aep->at_wwpn[0]) << 48) |
2056                         (((uint64_t) aep->at_wwpn[1]) << 32) |
2057                         (((uint64_t) aep->at_wwpn[2]) << 16) |
2058                         (((uint64_t) aep->at_wwpn[3]) <<  0);
2059                 /*
2060                  * However, make sure we delete ourselves if otherwise
2061                  * we were there but at a different loop id.
2062                  */
2063                 if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
2064                         isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
2065                 }
2066                 isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
2067         }
2068         atiop->cdb_len = ATIO2_CDBLEN;
2069         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2070         atiop->ccb_h.status = CAM_CDB_RECVD;
2071         atiop->tag_id = atp->tag;
2072         switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2073         case ATIO2_TC_ATTR_SIMPLEQ:
2074                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2075                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2076                 break;
2077         case ATIO2_TC_ATTR_HEADOFQ:
2078                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2079                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2080                 break;
2081         case ATIO2_TC_ATTR_ORDERED:
2082                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2083                 atiop->tag_action = MSG_ORDERED_Q_TAG;
2084                 break;
2085         case ATIO2_TC_ATTR_ACAQ:                /* ?? */
2086         case ATIO2_TC_ATTR_UNTAGGED:
2087         default:
2088                 atiop->tag_action = 0;
2089                 break;
2090         }
2091
2092         atp->orig_datalen = aep->at_datalen;
2093         atp->bytes_xfered = 0;
2094         atp->last_xframt = 0;
2095         atp->lun = lun;
2096         atp->nphdl = atiop->init_id;
2097         atp->sid = PORT_ANY;
2098         atp->oxid = aep->at_oxid;
2099         atp->cdb0 = aep->at_cdb[0];
2100         atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2101         atp->state = ATPD_STATE_CAM;
2102         xpt_done((union ccb *)atiop);
2103         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2104         rls_lun_statep(isp, tptr);
2105         return;
2106 noresrc:
2107         ntp = isp_get_ntpd(isp, tptr);
2108         if (ntp == NULL) {
2109                 rls_lun_statep(isp, tptr);
2110                 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2111                 return;
2112         }
2113         memcpy(ntp->rd.data, aep, QENTRY_LEN);
2114         ntp->rd.nt.nt_hba = tptr->restart_queue;
2115         tptr->restart_queue = ntp;
2116         rls_lun_statep(isp, tptr);
2117 }
2118
2119 static void
2120 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2121 {
2122         int cdbxlen;
2123         uint16_t lun, chan, nphdl = NIL_HANDLE;
2124         uint32_t did, sid;
2125         uint64_t wwn = INI_NONE;
2126         fcportdb_t *lp;
2127         tstate_t *tptr;
2128         struct ccb_accept_tio *atiop;
2129         atio_private_data_t *atp = NULL;
2130         inot_private_data_t *ntp;
2131
2132         did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2133         sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2134         lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2135
2136         /*
2137          * Find the N-port handle, and Virtual Port Index for this command.
2138          *
2139          * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2140          * We also, as a matter of course, need to know the WWN of the initiator too.
2141          */
2142         if (ISP_CAP_MULTI_ID(isp)) {
2143                 /*
2144                  * Find the right channel based upon D_ID
2145                  */
2146                 isp_find_chan_by_did(isp, did, &chan);
2147
2148                 if (chan == ISP_NOCHAN) {
2149                         NANOTIME_T now;
2150
2151                         /*
2152                          * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2153                          * It's a bit tricky here as we need to stash this command *somewhere*.
2154                          */
2155                         GET_NANOTIME(&now);
2156                         if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2157                                 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2158                                 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2159                                 return;
2160                         }
2161                         tptr = get_lun_statep(isp, 0, 0);
2162                         if (tptr == NULL) {
2163                                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2164                                 if (tptr == NULL) {
2165                                         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);
2166                                         isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2167                                         return;
2168                                 }
2169                         }
2170                         isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2171                         goto noresrc;
2172                 }
2173                 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);
2174         } else {
2175                 chan = 0;
2176         }
2177
2178         /*
2179          * Find the PDB entry for this initiator
2180          */
2181         if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2182                 /*
2183                  * If we're not in the port database terminate the exchange.
2184                  */
2185                 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",
2186                     __func__, aep->at_rxid, did, chan, sid);
2187                 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2188                 return;
2189         }
2190         nphdl = lp->handle;
2191         wwn = lp->port_wwn;
2192
2193         /*
2194          * Get the tstate pointer
2195          */
2196         tptr = get_lun_statep(isp, chan, lun);
2197         if (tptr == NULL) {
2198                 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2199                 if (tptr == NULL) {
2200                         isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
2201                         isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2202                         return;
2203                 }
2204         }
2205
2206         /*
2207          * Start any commands pending resources first.
2208          */
2209         if (tptr->restart_queue) {
2210                 inot_private_data_t *restart_queue = tptr->restart_queue;
2211                 tptr->restart_queue = NULL;
2212                 while (restart_queue) {
2213                         ntp = restart_queue;
2214                         restart_queue = ntp->rd.nt.nt_hba;
2215                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2216                         isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2217                         isp_put_ntpd(isp, tptr, ntp);
2218                         /*
2219                          * If a recursion caused the restart queue to start to fill again,
2220                          * stop and splice the new list on top of the old list and restore
2221                          * it and go to noresrc.
2222                          */
2223                         if (tptr->restart_queue) {
2224                                 if (restart_queue) {
2225                                         ntp = tptr->restart_queue;
2226                                         tptr->restart_queue = restart_queue;
2227                                         while (restart_queue->rd.nt.nt_hba) {
2228                                                 restart_queue = restart_queue->rd.nt.nt_hba;
2229                                         }
2230                                         restart_queue->rd.nt.nt_hba = ntp;
2231                                 }
2232                                 goto noresrc;
2233                         }
2234                 }
2235         }
2236
2237         /*
2238          * If the f/w is out of resources, just send a BUSY status back.
2239          */
2240         if (aep->at_rxid == AT7_NORESRC_RXID) {
2241                 rls_lun_statep(isp, tptr);
2242                 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2243                 return;
2244         }
2245
2246         /*
2247          * If we're out of resources, just send a BUSY status back.
2248          */
2249         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2250         if (atiop == NULL) {
2251                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2252                 goto noresrc;
2253         }
2254
2255         atp = isp_get_atpd(isp, tptr, 0);
2256         if (atp == NULL) {
2257                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2258                 goto noresrc;
2259         }
2260         if (isp_get_atpd(isp, tptr, aep->at_rxid)) {
2261                 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)\n",
2262                     aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id);
2263                 /*
2264                  * It's not a "no resource" condition- but we can treat it like one
2265                  */
2266                 goto noresrc;
2267         }
2268
2269         atp->tag = aep->at_rxid;
2270         atp->state = ATPD_STATE_ATIO;
2271         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2272         tptr->atio_count--;
2273         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2274         atiop->init_id = nphdl;
2275         atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2276         atiop->ccb_h.target_lun = lun;
2277         atiop->sense_len = 0;
2278         cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2279         if (cdbxlen) {
2280                 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2281         }
2282         cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2283         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2284         atiop->cdb_len = cdbxlen;
2285         atiop->ccb_h.status = CAM_CDB_RECVD;
2286         atiop->tag_id = atp->tag;
2287         switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2288         case FCP_CMND_TASK_ATTR_SIMPLE:
2289                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2290                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2291                 break;
2292         case FCP_CMND_TASK_ATTR_HEAD:
2293                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2294                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2295                 break;
2296         case FCP_CMND_TASK_ATTR_ORDERED:
2297                 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
2298                 atiop->tag_action = MSG_ORDERED_Q_TAG;
2299                 break;
2300         default:
2301                 /* FALLTHROUGH */
2302         case FCP_CMND_TASK_ATTR_ACA:
2303         case FCP_CMND_TASK_ATTR_UNTAGGED:
2304                 atiop->tag_action = 0;
2305                 break;
2306         }
2307         atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2308         atp->bytes_xfered = 0;
2309         atp->last_xframt = 0;
2310         atp->lun = lun;
2311         atp->nphdl = nphdl;
2312         atp->portid = sid;
2313         atp->oxid = aep->at_hdr.ox_id;
2314         atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2315         atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2316         atp->state = ATPD_STATE_CAM;
2317         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2318         xpt_done((union ccb *)atiop);
2319         rls_lun_statep(isp, tptr);
2320         return;
2321 noresrc:
2322         if (atp) {
2323                 isp_put_atpd(isp, tptr, atp);
2324         }
2325         ntp = isp_get_ntpd(isp, tptr);
2326         if (ntp == NULL) {
2327                 rls_lun_statep(isp, tptr);
2328                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2329                 return;
2330         }
2331         memcpy(ntp->rd.data, aep, QENTRY_LEN);
2332         ntp->rd.nt.nt_hba = tptr->restart_queue;
2333         tptr->restart_queue = ntp;
2334         rls_lun_statep(isp, tptr);
2335 }
2336
2337 static void
2338 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2339 {
2340         union ccb *ccb;
2341         int sentstatus, ok, notify_cam, resid = 0;
2342         tstate_t *tptr = NULL;
2343         atio_private_data_t *atp = NULL;
2344         int bus;
2345         uint32_t tval, handle;
2346
2347         /*
2348          * CTIO handles are 16 bits.
2349          * CTIO2 and CTIO7 are 32 bits.
2350          */
2351
2352         if (IS_SCSI(isp)) {
2353                 handle = ((ct_entry_t *)arg)->ct_syshandle;
2354         } else {
2355                 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2356         }
2357         ccb = isp_find_xs_tgt(isp, handle);
2358         if (ccb == NULL) {
2359                 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2360                 return;
2361         }
2362         isp_destroy_tgt_handle(isp, handle);
2363         bus = XS_CHANNEL(ccb);
2364         tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2365         if (tptr == NULL) {
2366                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2367         }
2368         KASSERT((tptr != NULL), ("cannot get state pointer"));
2369         if (isp->isp_nactive) {
2370                 isp->isp_nactive++;
2371         }
2372         if (IS_24XX(isp)) {
2373                 ct7_entry_t *ct = arg;
2374
2375                 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2376                 if (atp == NULL) {
2377                         rls_lun_statep(isp, tptr);
2378                         isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2379                         return;
2380                 }
2381
2382                 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2383                 ok = (ct->ct_nphdl == CT7_OK);
2384                 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2385                         ccb->ccb_h.status |= CAM_SENT_SENSE;
2386                 }
2387                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2388                 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2389                         resid = ct->ct_resid;
2390                         atp->bytes_xfered += (atp->last_xframt - resid);
2391                         atp->last_xframt = 0;
2392                 }
2393                 if (ct->ct_nphdl == CT_HBA_RESET) {
2394                         ok = 0;
2395                         notify_cam = 1;
2396                         sentstatus = 1;
2397                         ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2398                 } else if (!ok) {
2399                         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2400                 }
2401                 tval = atp->tag;
2402                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2403                     ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2404                 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2405         } else if (IS_FC(isp)) {
2406                 ct2_entry_t *ct = arg;
2407
2408                 atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
2409                 if (atp == NULL) {
2410                         rls_lun_statep(isp, tptr);
2411                         isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
2412                         return;
2413                 }
2414                 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2415                 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2416                 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
2417                         ccb->ccb_h.status |= CAM_SENT_SENSE;
2418                 }
2419                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2420                 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
2421                         resid = ct->ct_resid;
2422                         atp->bytes_xfered += (atp->last_xframt - resid);
2423                         atp->last_xframt = 0;
2424                 }
2425                 if (ct->ct_status == CT_HBA_RESET) {
2426                         ok = 0;
2427                         notify_cam = 1;
2428                         sentstatus = 1;
2429                         ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2430                 } else if (!ok) {
2431                         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2432                 }
2433                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
2434                     ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2435                 tval = atp->tag;
2436                 atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
2437         } else {
2438                 ct_entry_t *ct = arg;
2439                 sentstatus = ct->ct_flags & CT_SENDSTATUS;
2440                 ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
2441                 /*
2442                  * We *ought* to be able to get back to the original ATIO
2443                  * here, but for some reason this gets lost. It's just as
2444                  * well because it's squirrelled away as part of periph
2445                  * private data.
2446                  *
2447                  * We can live without it as long as we continue to use
2448                  * the auto-replenish feature for CTIOs.
2449                  */
2450                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
2451                 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
2452                         ok = 0;
2453                         notify_cam = 1;
2454                         sentstatus = 1;
2455                         ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2456                 } else if (!ok) {
2457                         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2458                 } else if (ct->ct_status & QLTM_SVALID) {
2459                         char *sp = (char *)ct;
2460                         sp += CTIO_SENSE_OFFSET;
2461                         ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
2462                         ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
2463                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2464                 }
2465                 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
2466                         resid = ct->ct_resid;
2467                 }
2468                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
2469                     ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
2470                 tval = ct->ct_fwhandle;
2471         }
2472         ccb->csio.resid += resid;
2473
2474         /*
2475          * We're here either because intermediate data transfers are done
2476          * and/or the final status CTIO (which may have joined with a
2477          * Data Transfer) is done.
2478          *
2479          * In any case, for this platform, the upper layers figure out
2480          * what to do next, so all we do here is collect status and
2481          * pass information along. Any DMA handles have already been
2482          * freed.
2483          */
2484         if (notify_cam == 0) {
2485                 isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
2486                 return;
2487         }
2488         if (tptr) {
2489                 rls_lun_statep(isp, tptr);
2490         }
2491         isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? "  FINAL " : "MIDTERM ", tval);
2492
2493         if (!ok && !IS_24XX(isp)) {
2494                 isp_target_putback_atio(ccb);
2495         } else {
2496                 isp_complete_ctio(ccb);
2497         }
2498 }
2499
2500 static void
2501 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
2502 {
2503         (void) isp_notify_ack(isp, inot);
2504 }
2505
2506 static void
2507 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
2508 {
2509         int needack = 1;
2510         switch (inp->in_status) {
2511         case IN_PORT_LOGOUT:
2512                 /*
2513                  * XXX: Need to delete this initiator's WWN from the database
2514                  * XXX: Need to send this LOGOUT upstream
2515                  */
2516                 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
2517                 break;
2518         case IN_PORT_CHANGED:
2519                 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
2520                 break;
2521         case IN_GLOBAL_LOGO:
2522                 isp_del_all_wwn_entries(isp, 0);
2523                 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
2524                 break;
2525         case IN_ABORT_TASK:
2526         {
2527                 tstate_t *tptr;
2528                 uint16_t lun;
2529                 uint32_t loopid;
2530                 uint64_t wwn;
2531                 atio_private_data_t *atp;
2532                 fcportdb_t *lp;
2533                 struct ccb_immediate_notify *inot = NULL;
2534
2535                 if (ISP_CAP_SCCFW(isp)) {
2536                         lun = inp->in_scclun;
2537                 } else {
2538                         lun = inp->in_lun;
2539                 }
2540                 if (ISP_CAP_2KLOGIN(isp)) {
2541                         loopid = ((in_fcentry_e_t *)inp)->in_iid;
2542                 } else {
2543                         loopid = inp->in_iid;
2544                 }
2545                 if (isp_find_pdb_by_loopid(isp, 0, loopid, &lp)) {
2546                         wwn = lp->port_wwn;
2547                 } else {
2548                         wwn = INI_ANY;
2549                 }
2550                 tptr = get_lun_statep(isp, 0, lun);
2551                 if (tptr == NULL) {
2552                         tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2553                         if (tptr == NULL) {
2554                                 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
2555                                 return;
2556                         }
2557                 }
2558                 atp = isp_get_atpd(isp, tptr, inp->in_seqid);
2559
2560                 if (atp) {
2561                         inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2562                         isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
2563                         if (inot) {
2564                                 tptr->inot_count--;
2565                                 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2566                                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2567                         } else {
2568                                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
2569                         }
2570                 } else {
2571                         ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
2572                 }
2573                 if (inot) {
2574                         isp_notify_t tmp, *nt = &tmp;
2575                         ISP_MEMZERO(nt, sizeof (isp_notify_t));
2576                         nt->nt_hba = isp;
2577                         nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
2578                         nt->nt_wwn = wwn;
2579                         nt->nt_nphdl = loopid;
2580                         nt->nt_sid = PORT_ANY;
2581                         nt->nt_did = PORT_ANY;
2582                         nt->nt_lun = lun;
2583                         nt->nt_need_ack = 1;
2584                         nt->nt_channel = 0;
2585                         nt->nt_ncode = NT_ABORT_TASK;
2586                         nt->nt_lreserved = inot;
2587                         isp_handle_platform_target_tmf(isp, nt);
2588                         needack = 0;
2589                 }
2590                 rls_lun_statep(isp, tptr);
2591                 break;
2592         }
2593         default:
2594                 break;
2595         }
2596         if (needack) {
2597                 (void) isp_notify_ack(isp, inp);
2598         }
2599 }
2600
2601 static void
2602 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
2603 {
2604         uint16_t nphdl;
2605         uint32_t portid;
2606         fcportdb_t *lp;
2607         uint8_t *ptr = NULL;
2608         uint64_t wwn;
2609
2610         nphdl = inot->in_nphdl;
2611         if (nphdl != NIL_HANDLE) {
2612                 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
2613         } else {
2614                 portid = PORT_ANY;
2615         }
2616
2617         switch (inot->in_status) {
2618         case IN24XX_ELS_RCVD:
2619         {
2620                 char buf[16], *msg;
2621                 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
2622
2623                 /*
2624                  * Note that we're just getting notification that an ELS was received
2625                  * (possibly with some associated information sent upstream). This is
2626                  * *not* the same as being given the ELS frame to accept or reject.
2627                  */
2628                 switch (inot->in_status_subcode) {
2629                 case LOGO:
2630                         msg = "LOGO";
2631                         if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2632                                 ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2633                                 wwn =   (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF])   << 56) |
2634                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
2635                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
2636                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
2637                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
2638                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
2639                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) <<  8) |
2640                                         (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
2641                         } else {
2642                                 wwn = INI_ANY;
2643                         }
2644                         isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
2645                         break;
2646                 case PRLO:
2647                         msg = "PRLO";
2648                         break;
2649                 case PLOGI:
2650                 case PRLI:
2651                         /*
2652                          * Treat PRLI the same as PLOGI and make a database entry for it.
2653                          */
2654                         if (inot->in_status_subcode == PLOGI)
2655                                 msg = "PLOGI";
2656                         else
2657                                 msg = "PRLI";
2658                         if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
2659                                 ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
2660                                 wwn =   (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
2661                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
2662                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
2663                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
2664                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
2665                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
2666                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) <<  8) |
2667                                         (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
2668                         } else {
2669                                 wwn = INI_NONE;
2670                         }
2671                         isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
2672                         break;
2673                 case PDISC:
2674                         msg = "PDISC";
2675                         break;
2676                 case ADISC:
2677                         msg = "ADISC";
2678                         break;
2679                 default:
2680                         ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
2681                         msg = buf;
2682                         break;
2683                 }
2684                 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
2685                         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);
2686                         break;
2687                 }
2688                 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,
2689                     inot->in_rxid, inot->in_oxid);
2690                 (void) isp_notify_ack(isp, inot);
2691                 break;
2692         }
2693
2694         case IN24XX_PORT_LOGOUT:
2695                 ptr = "PORT LOGOUT";
2696                 if (isp_find_pdb_by_loopid(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
2697                         isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
2698                 }
2699                 /* FALLTHROUGH */
2700         case IN24XX_PORT_CHANGED:
2701                 if (ptr == NULL) {
2702                         ptr = "PORT CHANGED";
2703                 }
2704                 /* FALLTHROUGH */
2705         case IN24XX_LIP_RESET: 
2706                 if (ptr == NULL) {
2707                         ptr = "LIP RESET";
2708                 }
2709                 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), ptr, inot->in_status_subcode, nphdl);
2710
2711                 /*
2712                  * All subcodes here are irrelevant. What is relevant
2713                  * is that we need to terminate all active commands from
2714                  * this initiator (known by N-port handle).
2715                  */
2716                 /* XXX IMPLEMENT XXX */
2717                 (void) isp_notify_ack(isp, inot);
2718                 break;
2719
2720         case IN24XX_LINK_RESET:
2721         case IN24XX_LINK_FAILED:
2722         case IN24XX_SRR_RCVD:
2723         default:
2724                 (void) isp_notify_ack(isp, inot);
2725                 break;
2726         }
2727 }
2728
2729 static int
2730 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
2731 {
2732
2733         if (isp->isp_state != ISP_RUNSTATE) {
2734                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2735                 return (0);
2736         }
2737
2738         /*
2739          * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2740          */
2741         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2742                 ct7_entry_t local, *cto = &local;
2743                 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2744                 fcportdb_t *lp;
2745                 uint32_t sid;
2746                 uint16_t nphdl;
2747
2748                 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2749                 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
2750                         nphdl = lp->handle;
2751                 } else {
2752                         nphdl = NIL_HANDLE;
2753                 }
2754                 ISP_MEMZERO(&local, sizeof (local));
2755                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2756                 cto->ct_header.rqs_entry_count = 1;
2757                 cto->ct_nphdl = nphdl;
2758                 cto->ct_rxid = aep->at_rxid;
2759                 cto->ct_vpidx = mp->nt_channel;
2760                 cto->ct_iid_lo = sid;
2761                 cto->ct_iid_hi = sid >> 16;
2762                 cto->ct_oxid = aep->at_hdr.ox_id;
2763                 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2764                 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2765                 return (isp_target_put_entry(isp, &local));
2766         }
2767
2768         /*
2769          * This case is for a responding to an ABTS frame
2770          */
2771         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2772
2773                 /*
2774                  * Overload nt_need_ack here to mark whether we've terminated the associated command.
2775                  */
2776                 if (mp->nt_need_ack) {
2777                         uint8_t storage[QENTRY_LEN];
2778                         ct7_entry_t *cto = (ct7_entry_t *) storage;
2779                         abts_t *abts = (abts_t *)mp->nt_lreserved;
2780
2781                         ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2782                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2783                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2784                         cto->ct_header.rqs_entry_count = 1;
2785                         cto->ct_nphdl = mp->nt_nphdl;
2786                         cto->ct_rxid = abts->abts_rxid_task;
2787                         cto->ct_iid_lo = mp->nt_sid;
2788                         cto->ct_iid_hi = mp->nt_sid >> 16;
2789                         cto->ct_oxid = abts->abts_ox_id;
2790                         cto->ct_vpidx = mp->nt_channel;
2791                         cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2792                         if (isp_target_put_entry(isp, cto)) {
2793                                 return (ENOMEM);
2794                         }
2795                         mp->nt_need_ack = 0;
2796                 }
2797                 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2798                         return (ENOMEM);
2799                 } else {
2800                         return (0);
2801                 }
2802         }
2803
2804         /*
2805          * Handle logout cases here
2806          */
2807         if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2808                 isp_del_all_wwn_entries(isp, mp->nt_channel);
2809         }
2810
2811         if (mp->nt_ncode == NT_LOGOUT) {
2812                 if (!IS_2100(isp) && IS_FC(isp)) {
2813                         isp_del_wwn_entries(isp, mp);
2814                 }
2815         }
2816
2817         /*
2818          * General purpose acknowledgement
2819          */
2820         if (mp->nt_need_ack) {
2821                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2822                 return (isp_notify_ack(isp, mp->nt_lreserved));
2823         }
2824         return (0);
2825 }
2826
2827 /*
2828  * Handle task management functions.
2829  *
2830  * We show up here with a notify structure filled out.
2831  *
2832  * The nt_lreserved tag points to the original queue entry
2833  */
2834 static void
2835 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2836 {
2837         tstate_t *tptr;
2838         fcportdb_t *lp;
2839         struct ccb_immediate_notify *inot;
2840         inot_private_data_t *ntp = NULL;
2841         lun_id_t lun;
2842
2843         isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
2844             notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2845         /*
2846          * NB: This assignment is necessary because of tricky type conversion.
2847          * XXX: This is tricky and I need to check this. If the lun isn't known
2848          * XXX: for the task management function, it does not of necessity follow
2849          * XXX: that it should go up stream to the wildcard listener.
2850          */
2851         if (notify->nt_lun == LUN_ANY) {
2852                 lun = CAM_LUN_WILDCARD;
2853         } else {
2854                 lun = notify->nt_lun;
2855         }
2856         tptr = get_lun_statep(isp, notify->nt_channel, lun);
2857         if (tptr == NULL) {
2858                 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2859                 if (tptr == NULL) {
2860                         isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2861                         goto bad;
2862                 }
2863         }
2864         inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2865         if (inot == NULL) {
2866                 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
2867                 goto bad;
2868         }
2869
2870         if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0) {
2871                 inot->initiator_id = CAM_TARGET_WILDCARD;
2872         } else {
2873                 inot->initiator_id = lp->handle;
2874         }
2875         inot->seq_id = notify->nt_tagval;
2876         inot->tag_id = notify->nt_tagval >> 32;
2877
2878         switch (notify->nt_ncode) {
2879         case NT_ABORT_TASK:
2880                 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
2881                 inot->arg = MSG_ABORT_TASK;
2882                 break;
2883         case NT_ABORT_TASK_SET:
2884                 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
2885                 inot->arg = MSG_ABORT_TASK_SET;
2886                 break;
2887         case NT_CLEAR_ACA:
2888                 inot->arg = MSG_CLEAR_ACA;
2889                 break;
2890         case NT_CLEAR_TASK_SET:
2891                 inot->arg = MSG_CLEAR_TASK_SET;
2892                 break;
2893         case NT_LUN_RESET:
2894                 inot->arg = MSG_LOGICAL_UNIT_RESET;
2895                 break;
2896         case NT_TARGET_RESET:
2897                 inot->arg = MSG_TARGET_RESET;
2898                 break;
2899         default:
2900                 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);
2901                 goto bad;
2902         }
2903
2904         ntp = isp_get_ntpd(isp, tptr);
2905         if (ntp == NULL) {
2906                 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2907                 goto bad;
2908         }
2909         ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
2910         if (notify->nt_lreserved) {
2911                 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
2912                 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
2913         }
2914         ntp->rd.seq_id = notify->nt_tagval;
2915         ntp->rd.tag_id = notify->nt_tagval >> 32;
2916
2917         tptr->inot_count--;
2918         SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2919         rls_lun_statep(isp, tptr);
2920         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
2921         inot->ccb_h.status = CAM_MESSAGE_RECV;
2922         xpt_done((union ccb *)inot);
2923         return;
2924 bad:
2925         if (tptr) {
2926                 rls_lun_statep(isp, tptr);
2927         }
2928         if (notify->nt_need_ack && notify->nt_lreserved) {
2929                 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2930                         (void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
2931                 } else {
2932                         (void) isp_notify_ack(isp, notify->nt_lreserved);
2933                 }
2934         }
2935 }
2936
2937 /*
2938  * Find the associated private data and mark it as dead so
2939  * we don't try to work on it any further.
2940  */
2941 static void
2942 isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
2943 {
2944         tstate_t *tptr;
2945         atio_private_data_t *atp;
2946         union ccb *accb = ccb->cab.abort_ccb;
2947
2948         tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2949         if (tptr == NULL) {
2950                 tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
2951                 if (tptr == NULL) {
2952                         ccb->ccb_h.status = CAM_REQ_INVALID;
2953                         return;
2954                 }
2955         }
2956
2957         atp = isp_get_atpd(isp, tptr, accb->atio.tag_id);
2958         if (atp == NULL) {
2959                 ccb->ccb_h.status = CAM_REQ_INVALID;
2960         } else {
2961                 atp->dead = 1;
2962                 ccb->ccb_h.status = CAM_REQ_CMP;
2963         }
2964         rls_lun_statep(isp, tptr);
2965 }
2966
2967 static void
2968 isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
2969 {
2970         atio_private_data_t *atp;
2971         inot_private_data_t *restart_queue = tptr->restart_queue;
2972
2973         /*
2974          * First, clean any commands pending restart
2975          */
2976         tptr->restart_queue = NULL;
2977         while (restart_queue) {
2978                 uint32_t this_tag_id;
2979                 inot_private_data_t *ntp = restart_queue;
2980
2981                 restart_queue = ntp->rd.nt.nt_hba;
2982
2983                 if (IS_24XX(isp)) {
2984                         this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
2985                 } else {
2986                         this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
2987                 }
2988                 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2989                         isp_put_ntpd(isp, tptr, ntp);
2990                 } else {
2991                         ntp->rd.nt.nt_hba = tptr->restart_queue;
2992                         tptr->restart_queue = ntp;
2993                 }
2994         }
2995
2996         /*
2997          * Now mark other ones dead as well.
2998          */
2999         for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3000                 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3001                         atp->dead = 1;
3002                 }
3003         }
3004 }
3005
3006
3007 #ifdef  ISP_INTERNAL_TARGET
3008 // #define      ISP_FORCE_TIMEOUT               1
3009 // #define      ISP_TEST_WWNS                   1
3010 // #define      ISP_TEST_SEPARATE_STATUS        1
3011
3012 #define ccb_data_offset         ppriv_field0
3013 #define ccb_atio                ppriv_ptr1
3014 #define ccb_inot                ppriv_ptr1
3015
3016 #define MAX_ISP_TARG_TRANSFER   (2 << 20)
3017 #define NISP_TARG_CMDS          1024
3018 #define NISP_TARG_NOTIFIES      1024
3019 #define DISK_SHIFT              9
3020 #define JUNK_SIZE               256
3021
3022 #ifndef VERIFY_10
3023 #define VERIFY_10       0x2f
3024 #endif
3025
3026 TAILQ_HEAD(ccb_queue, ccb_hdr);
3027 extern u_int vm_kmem_size;
3028 static int ca;
3029 static uint32_t disk_size;
3030 static uint8_t *disk_data = NULL;
3031 static uint8_t *junk_data;
3032 static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3033 struct isptarg_softc {
3034         /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3035         struct ccb_queue        work_queue;
3036         struct ccb_queue        rework_queue;
3037         struct ccb_queue        running_queue;
3038         struct ccb_queue        inot_queue;
3039         struct cam_periph       *periph;
3040         struct cam_path         *path;
3041         ispsoftc_t              *isp;
3042 };
3043 static periph_ctor_t    isptargctor;
3044 static periph_dtor_t    isptargdtor;
3045 static periph_start_t   isptargstart;
3046 static periph_init_t    isptarginit;
3047 static void             isptarg_done(struct cam_periph *, union ccb *);
3048 static void             isptargasync(void *, u_int32_t, struct cam_path *, void *);
3049
3050
3051 static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3052
3053 static struct periph_driver isptargdriver =
3054 {
3055         isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), /* generation */ 0
3056 };
3057
3058 static void
3059 isptarginit(void)
3060 {
3061 }
3062
3063 static void
3064 isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3065 {
3066         struct ccb_notify_acknowledge *ack = &iccb->cna2;
3067
3068         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3069             inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3070         ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3071         ack->ccb_h.flags = 0;
3072         ack->ccb_h.retry_count = 0;
3073         ack->ccb_h.cbfcnp = isptarg_done;
3074         ack->ccb_h.timeout = 0;
3075         ack->ccb_h.ccb_inot = inot;
3076         ack->tag_id = inot->tag_id;
3077         ack->seq_id = inot->seq_id;
3078         ack->initiator_id = inot->initiator_id;
3079         xpt_action(iccb);
3080 }
3081
3082 static void
3083 isptargstart(struct cam_periph *periph, union ccb *iccb)
3084 {
3085         const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3086                 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3087                 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3088                 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3089                 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3090                 '0', '0', '0', '1'
3091         };
3092         const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3093                 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3094                 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3095                 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3096                 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3097                 '0', '0', '0', '1'
3098         };
3099         int i, more = 0, last;
3100         struct isptarg_softc *softc = periph->softc;
3101         struct ccb_scsiio *csio;
3102         lun_id_t return_lun;
3103         struct ccb_accept_tio *atio;
3104         uint8_t *cdb, *ptr, status;
3105         uint8_t *data_ptr;
3106         uint32_t data_len, flags;
3107         struct ccb_hdr *ccbh;
3108             
3109         mtx_assert(periph->sim->mtx, MA_OWNED);
3110         ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3111             TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3112         /*
3113          * Check for immediate notifies first
3114          */
3115         ccbh = TAILQ_FIRST(&softc->inot_queue);
3116         if (ccbh) {
3117                 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3118                 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3119                         xpt_schedule(periph, 1);
3120                 }
3121                 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3122                 return;
3123         }
3124
3125         /*
3126          * Check the rework (continuation) work queue first.
3127          */
3128         ccbh = TAILQ_FIRST(&softc->rework_queue);
3129         if (ccbh) {
3130                 atio = (struct ccb_accept_tio *)ccbh;
3131                 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3132                 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3133         } else {
3134                 ccbh = TAILQ_FIRST(&softc->work_queue);
3135                 if (ccbh == NULL) {
3136                         ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
3137                         xpt_release_ccb(iccb);
3138                         return;
3139                 }
3140                 atio = (struct ccb_accept_tio *)ccbh;
3141                 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3142                 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3143                 atio->ccb_h.ccb_data_offset = 0;
3144         }
3145
3146         if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3147                 panic("BAD ATIO");
3148         }
3149
3150         data_ptr = NULL;
3151         data_len = 0;
3152         csio = &iccb->csio;
3153         status = SCSI_STATUS_OK;
3154         flags = CAM_SEND_STATUS;
3155         memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3156         cdb = atio->cdb_io.cdb_bytes;
3157         ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
3158             cdb[0], atio->ccb_h.ccb_data_offset);
3159
3160         return_lun = XS_LUN(atio);
3161         if (return_lun != 0) {
3162                 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3163                 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3164                         status = SCSI_STATUS_CHECK_COND;
3165                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
3166                         atio->sense_data.add_sense_code = 0x25;
3167                         atio->sense_data.add_sense_code_qual = 0x0;
3168                         atio->sense_len = sizeof (atio->sense_data);
3169                 }
3170                 return_lun = CAM_LUN_WILDCARD;
3171         }
3172
3173         switch (cdb[0]) {
3174         case REQUEST_SENSE:
3175                 flags |= CAM_DIR_IN;
3176                 data_len = sizeof (atio->sense_data);
3177                 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3178                 memset(junk_data+1, 0, data_len-1);
3179                 if (data_len > cdb[4]) {
3180                         data_len = cdb[4];
3181                 }
3182                 if (data_len) {
3183                         data_ptr = junk_data;
3184                 }
3185                 break;
3186         case READ_6:
3187         case READ_10:
3188         case READ_12:
3189         case READ_16:
3190                 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3191                         status = SCSI_STATUS_CHECK_COND;
3192                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3193                         atio->sense_data.add_sense_code = 0x5;
3194                         atio->sense_data.add_sense_code_qual = 0x24;
3195                         atio->sense_len = sizeof (atio->sense_data);
3196                 } else {
3197 #ifdef  ISP_FORCE_TIMEOUT
3198                         {
3199                                 static int foo;
3200                                 if (foo++ == 500) {
3201                                         if (more) {
3202                                                 xpt_schedule(periph, 1);
3203                                         }
3204                                         foo = 0;
3205                                         return;
3206                                 }
3207                         }
3208 #endif
3209 #ifdef  ISP_TEST_SEPARATE_STATUS
3210                         if (last && data_len) {
3211                                 last = 0;
3212                         }
3213 #endif
3214                         if (last == 0) {
3215                                 flags &= ~CAM_SEND_STATUS;
3216                         }
3217                         if (data_len) {
3218                                 atio->ccb_h.ccb_data_offset += data_len;
3219                                 flags |= CAM_DIR_IN;
3220                         } else {
3221                                 flags |= CAM_DIR_NONE;
3222                         }
3223                 }
3224                 break;
3225         case WRITE_6:
3226         case WRITE_10:
3227         case WRITE_12:
3228         case WRITE_16:
3229                 if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
3230                         status = SCSI_STATUS_CHECK_COND;
3231                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3232                         atio->sense_data.add_sense_code = 0x5;
3233                         atio->sense_data.add_sense_code_qual = 0x24;
3234                         atio->sense_len = sizeof (atio->sense_data);
3235                 } else {
3236 #ifdef  ISP_FORCE_TIMEOUT
3237                         {
3238                                 static int foo;
3239                                 if (foo++ == 500) {
3240                                         if (more) {
3241                                                 xpt_schedule(periph, 1);
3242                                         }
3243                                         foo = 0;
3244                                         return;
3245                                 }
3246                         }
3247 #endif
3248 #ifdef  ISP_TEST_SEPARATE_STATUS
3249                         if (last && data_len) {
3250                                 last = 0;
3251                         }
3252 #endif
3253                         if (last == 0) {
3254                                 flags &= ~CAM_SEND_STATUS;
3255                         }
3256                         if (data_len) {
3257                                 atio->ccb_h.ccb_data_offset += data_len;
3258                                 flags |= CAM_DIR_OUT;
3259                         } else {
3260                                 flags |= CAM_DIR_NONE;
3261                         }
3262                 }
3263                 break;
3264         case INQUIRY:
3265                 flags |= CAM_DIR_IN;
3266                 if (cdb[1] || cdb[2] || cdb[3]) {
3267                         status = SCSI_STATUS_CHECK_COND;
3268                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3269                         atio->sense_data.add_sense_code = 0x5;
3270                         atio->sense_data.add_sense_code_qual = 0x20;
3271                         atio->sense_len = sizeof (atio->sense_data);
3272                         break;
3273                 }
3274                 data_len = sizeof (iqd);
3275                 if (data_len > cdb[4]) {
3276                         data_len = cdb[4];
3277                 }
3278                 if (data_len) {
3279                         if (XS_LUN(iccb) != 0) {
3280                                 memcpy(junk_data, niliqd, sizeof (iqd));
3281                         } else {
3282                                 memcpy(junk_data, iqd, sizeof (iqd));
3283                         }
3284                         data_ptr = junk_data;
3285                 }
3286                 break;
3287         case TEST_UNIT_READY:
3288                 flags |= CAM_DIR_NONE;
3289                 if (ca) {
3290                         ca = 0;
3291                         status = SCSI_STATUS_CHECK_COND;
3292                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3293                         atio->sense_data.add_sense_code = 0x28;
3294                         atio->sense_data.add_sense_code_qual = 0x0;
3295                         atio->sense_len = sizeof (atio->sense_data);
3296                 }
3297                 break;
3298         case SYNCHRONIZE_CACHE:
3299         case START_STOP:
3300         case RESERVE:
3301         case RELEASE:
3302         case VERIFY_10:
3303                 flags |= CAM_DIR_NONE;
3304                 break;
3305
3306         case READ_CAPACITY:
3307                 flags |= CAM_DIR_IN;
3308                 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3309                         status = SCSI_STATUS_CHECK_COND;
3310                         atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3311                         atio->sense_data.add_sense_code = 0x5;
3312                         atio->sense_data.add_sense_code_qual = 0x24;
3313                         atio->sense_len = sizeof (atio->sense_data);
3314                         break;
3315                 }
3316                 if (cdb[8] & 0x1) { /* PMI */
3317                         junk_data[0] = 0xff;
3318                         junk_data[1] = 0xff;
3319                         junk_data[2] = 0xff;
3320                         junk_data[3] = 0xff;
3321                 } else {
3322                         uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3323                         if (last_blk < 0xffffffffULL) {
3324                             junk_data[0] = (last_blk >> 24) & 0xff;
3325                             junk_data[1] = (last_blk >> 16) & 0xff;
3326                             junk_data[2] = (last_blk >>  8) & 0xff;
3327                             junk_data[3] = (last_blk) & 0xff;
3328                         } else {
3329                             junk_data[0] = 0xff;
3330                             junk_data[1] = 0xff;
3331                             junk_data[2] = 0xff;
3332                             junk_data[3] = 0xff;
3333                         }
3334                 }
3335                 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3336                 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3337                 junk_data[6] = ((1 << DISK_SHIFT) >>  8) & 0xff;
3338                 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3339                 data_ptr = junk_data;
3340                 data_len = 8;
3341                 break;
3342         case REPORT_LUNS:
3343                 flags |= CAM_DIR_IN;
3344                 memset(junk_data, 0, JUNK_SIZE);
3345                 junk_data[0] = (1 << 3) >> 24;
3346                 junk_data[1] = (1 << 3) >> 16;
3347                 junk_data[2] = (1 << 3) >> 8;
3348                 junk_data[3] = (1 << 3);
3349                 ptr = NULL;
3350                 for (i = 0; i < 1; i++) {
3351                         ptr = &junk_data[8 + (1 << 3)];
3352                         if (i >= 256) {
3353                                 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3354                         }
3355                         ptr[1] = i;
3356                 }
3357                 data_ptr = junk_data;
3358                 data_len = (ptr + 8) - junk_data;
3359                 break;
3360
3361         default:
3362                 flags |= CAM_DIR_NONE;
3363                 status = SCSI_STATUS_CHECK_COND;
3364                 atio->sense_data.error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
3365                 atio->sense_data.add_sense_code = 0x5;
3366                 atio->sense_data.add_sense_code_qual = 0x20;
3367                 atio->sense_len = sizeof (atio->sense_data);
3368                 break;
3369         }
3370
3371         /*
3372          * If we are done with the transaction, tell the
3373          * controller to send status and perform a CMD_CMPLT.
3374          * If we have associated sense data, see if we can
3375          * send that too.
3376          */
3377         if (status == SCSI_STATUS_CHECK_COND) {
3378                 flags |= CAM_SEND_SENSE;
3379                 csio->sense_len = atio->sense_len;
3380                 csio->sense_data = atio->sense_data;
3381                 flags &= ~CAM_DIR_MASK;
3382                 data_len = 0;
3383                 data_ptr = NULL;
3384         }
3385         cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 0);
3386         iccb->ccb_h.target_id = atio->ccb_h.target_id;
3387         iccb->ccb_h.target_lun = return_lun;
3388         iccb->ccb_h.ccb_atio = atio;
3389         xpt_action(iccb);
3390
3391         if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3392                 cam_release_devq(periph->path, 0, 0, 0, 0); 
3393                 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
3394         }
3395         if (more) {
3396                 xpt_schedule(periph, 1);
3397         }
3398 }
3399
3400 static cam_status
3401 isptargctor(struct cam_periph *periph, void *arg)
3402 {
3403         struct isptarg_softc *softc;
3404
3405         softc = (struct isptarg_softc *)arg;
3406         periph->softc = softc;
3407         softc->periph = periph;
3408         softc->path = periph->path;
3409         ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3410         return (CAM_REQ_CMP);
3411 }
3412
3413 static void
3414 isptargdtor(struct cam_periph *periph)
3415 {
3416         struct isptarg_softc *softc;
3417         softc = (struct isptarg_softc *)periph->softc;
3418         ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
3419         softc->periph = NULL;
3420         softc->path = NULL;
3421         periph->softc = NULL;
3422 }
3423
3424 static void
3425 isptarg_done(struct cam_periph *periph, union ccb *ccb)
3426 {
3427         struct isptarg_softc *softc;
3428         ispsoftc_t *isp;
3429         struct ccb_accept_tio *atio;
3430         struct ccb_immediate_notify *inot;
3431         cam_status status;
3432
3433         softc = (struct isptarg_softc *)periph->softc;
3434         isp = softc->isp;
3435         status = ccb->ccb_h.status & CAM_STATUS_MASK;
3436
3437         switch (ccb->ccb_h.func_code) {
3438         case XPT_ACCEPT_TARGET_IO:
3439                 atio = (struct ccb_accept_tio *) ccb;
3440                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
3441                 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe); 
3442                 xpt_schedule(periph, 1);
3443                 break;
3444         case XPT_IMMEDIATE_NOTIFY:
3445                 inot = (struct ccb_immediate_notify *) ccb;
3446                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
3447                 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe); 
3448                 xpt_schedule(periph, 1);
3449                 break;
3450         case XPT_CONT_TARGET_IO:
3451                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3452                         cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0); 
3453                         ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3454                 }
3455                 atio = ccb->ccb_h.ccb_atio;
3456                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3457                         cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
3458                         xpt_action((union ccb *)atio);
3459                 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
3460                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
3461                         TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe); 
3462                         xpt_schedule(periph, 1);
3463                 } else {
3464                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO seen in %s\n", atio->tag_id, __func__);
3465                         xpt_action((union ccb *)atio);
3466                 }
3467                 xpt_release_ccb(ccb);
3468                 break;
3469         case XPT_NOTIFY_ACKNOWLEDGE:
3470                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
3471                         cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0); 
3472                         ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3473                 }
3474                 inot = ccb->ccb_h.ccb_inot;
3475                 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
3476                 xpt_release_ccb(ccb);
3477                 xpt_action((union ccb *)inot);
3478                 break;
3479         default:
3480                 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
3481                 break;
3482         }
3483 }
3484
3485 static void
3486 isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
3487 {
3488         struct ac_contract *acp = arg;
3489         struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
3490
3491         if (code != AC_CONTRACT) {
3492                 return;
3493         }
3494         xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
3495 }
3496
3497 static void
3498 isp_target_thread(ispsoftc_t *isp, int chan)
3499 {
3500         union ccb *ccb = NULL;
3501         int i;
3502         void *wchan;
3503         cam_status status;
3504         struct isptarg_softc *softc = NULL;
3505         struct cam_periph *periph = NULL, *wperiph = NULL;
3506         struct cam_path *path, *wpath;
3507         struct cam_sim *sim;
3508
3509         if (disk_data == NULL) {
3510                 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
3511                 if (disk_size < (50 << 20)) {
3512                         disk_size = 50 << 20;
3513                 }
3514                 disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
3515                 if (disk_data == NULL) {
3516                         isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
3517                         goto out;
3518                 }
3519                 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
3520         }
3521         junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
3522         if (junk_data == NULL) {
3523                 isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
3524                 goto out;
3525         }
3526
3527
3528         softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
3529         if (softc == NULL) {
3530                 isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
3531                 goto out;
3532         }
3533         TAILQ_INIT(&softc->work_queue);
3534         TAILQ_INIT(&softc->rework_queue);
3535         TAILQ_INIT(&softc->running_queue);
3536         TAILQ_INIT(&softc->inot_queue);
3537         softc->isp = isp;
3538
3539         periphdriver_register(&isptargdriver);
3540         ISP_GET_PC(isp, chan, sim, sim);
3541         ISP_GET_PC(isp, chan, path,  path);
3542         status = xpt_create_path_unlocked(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3543         if (status != CAM_REQ_CMP) {
3544                 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
3545                 return;
3546         }
3547         status = xpt_create_path_unlocked(&path, NULL, cam_sim_path(sim), 0, 0);
3548         if (status != CAM_REQ_CMP) {
3549                 xpt_free_path(wpath);
3550                 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
3551                 return;
3552         }
3553
3554         ccb = xpt_alloc_ccb();
3555
3556         ISP_LOCK(isp);
3557         status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
3558         if (status != CAM_REQ_CMP) {
3559                 ISP_UNLOCK(isp);
3560                 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
3561                 goto out;
3562         }
3563         wperiph = cam_periph_find(wpath, "isptarg");
3564         if (wperiph == NULL) {
3565                 ISP_UNLOCK(isp);
3566                 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
3567                 goto out;
3568         }
3569
3570         status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
3571         if (status != CAM_REQ_CMP) {
3572                 ISP_UNLOCK(isp);
3573                 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
3574                 goto out;
3575         }
3576
3577         periph = cam_periph_find(path, "isptarg");
3578         if (periph == NULL) {
3579                 ISP_UNLOCK(isp);
3580                 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
3581                 goto out;
3582         }
3583
3584         status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
3585         if (status != CAM_REQ_CMP) {
3586                 ISP_UNLOCK(isp);
3587                 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
3588                 goto out;
3589         }
3590
3591         ISP_UNLOCK(isp);
3592
3593         ccb = xpt_alloc_ccb();
3594
3595         /*
3596          * Make sure role is none.
3597          */
3598         xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3599         ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3600         ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
3601 #ifdef  ISP_TEST_WWNS
3602         ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE | KNOB_VALID_ADDRESS;
3603         ccb->knob.xport_specific.fc.wwnn = 0x508004d000000000ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3604         ccb->knob.xport_specific.fc.wwpn = 0x508004d000000001ULL | (device_get_unit(isp->isp_osinfo.dev) << 8) | (chan << 16);
3605 #else
3606         ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3607 #endif
3608
3609         ISP_LOCK(isp);
3610         xpt_action(ccb);
3611         ISP_UNLOCK(isp);
3612
3613         /*
3614          * Now enable luns
3615          */
3616         xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3617         ccb->ccb_h.func_code = XPT_EN_LUN;
3618         ccb->cel.enable = 1;
3619         ISP_LOCK(isp);
3620         xpt_action(ccb);
3621         ISP_UNLOCK(isp);
3622         if (ccb->ccb_h.status != CAM_REQ_CMP) {
3623                 xpt_free_ccb(ccb);
3624                 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3625                 goto out;
3626         }
3627
3628         xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
3629         ccb->ccb_h.func_code = XPT_EN_LUN;
3630         ccb->cel.enable = 1;
3631         ISP_LOCK(isp);
3632         xpt_action(ccb);
3633         ISP_UNLOCK(isp);
3634         if (ccb->ccb_h.status != CAM_REQ_CMP) {
3635                 xpt_free_ccb(ccb);
3636                 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
3637                 goto out;
3638         }
3639         xpt_free_ccb(ccb);
3640
3641         /*
3642          * Add resources
3643          */
3644         ISP_GET_PC_ADDR(isp, chan, target_proc, wchan);
3645         for (i = 0; i < 4; i++) {
3646                 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3647                 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3648                 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3649                 ccb->ccb_h.cbfcnp = isptarg_done;
3650                 ISP_LOCK(isp);
3651                 xpt_action(ccb);
3652                 ISP_UNLOCK(isp);
3653         }
3654         for (i = 0; i < NISP_TARG_CMDS; i++) {
3655                 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3656                 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3657                 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
3658                 ccb->ccb_h.cbfcnp = isptarg_done;
3659                 ISP_LOCK(isp);
3660                 xpt_action(ccb);
3661                 ISP_UNLOCK(isp);
3662         }
3663         for (i = 0; i < 4; i++) {
3664                 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3665                 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
3666                 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3667                 ccb->ccb_h.cbfcnp = isptarg_done;
3668                 ISP_LOCK(isp);
3669                 xpt_action(ccb);
3670                 ISP_UNLOCK(isp);
3671         }
3672         for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
3673                 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
3674                 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
3675                 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
3676                 ccb->ccb_h.cbfcnp = isptarg_done;
3677                 ISP_LOCK(isp);
3678                 xpt_action(ccb);
3679                 ISP_UNLOCK(isp);
3680         }
3681
3682         /*
3683          * Now turn it all back on
3684          */
3685         xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
3686         ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
3687         ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
3688         ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
3689         ISP_LOCK(isp);
3690         xpt_action(ccb);
3691         ISP_UNLOCK(isp);
3692
3693         /*
3694          * Okay, while things are still active, sleep...
3695          */
3696         ISP_LOCK(isp);
3697         for (;;) {
3698                 ISP_GET_PC(isp, chan, proc_active, i);
3699                 if (i == 0) {
3700                         break;
3701                 }
3702                 msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
3703         }
3704         ISP_UNLOCK(isp);
3705
3706 out:
3707         if (wperiph) {
3708                 cam_periph_invalidate(wperiph);
3709         }
3710         if (periph) {
3711                 cam_periph_invalidate(periph);
3712         }
3713         if (junk_data) {
3714                 free(junk_data, M_ISPTARG);
3715         }
3716         if (disk_data) {
3717                 free(disk_data, M_ISPTARG);
3718         }
3719         if (softc) {
3720                 free(softc, M_ISPTARG);
3721         }
3722         xpt_free_path(path);
3723         xpt_free_path(wpath);
3724 }
3725
3726 static void
3727 isp_target_thread_pi(void *arg)
3728 {
3729         struct isp_spi *pi = arg;
3730         isp_target_thread(cam_sim_softc(pi->sim), cam_sim_bus(pi->sim));
3731 }
3732
3733 static void
3734 isp_target_thread_fc(void *arg)
3735 {
3736         struct isp_fc *fc = arg;
3737         isp_target_thread(cam_sim_softc(fc->sim), cam_sim_bus(fc->sim));
3738 }
3739
3740 static int
3741 isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
3742 {
3743         uint32_t cnt, curcnt;
3744         uint64_t lba;
3745
3746         switch (cdb[0]) {
3747         case WRITE_16:
3748         case READ_16:
3749                 cnt =   (((uint32_t)cdb[10]) <<  24) |
3750                         (((uint32_t)cdb[11]) <<  16) |
3751                         (((uint32_t)cdb[12]) <<   8) |
3752                         ((uint32_t)cdb[13]);
3753
3754                 lba =   (((uint64_t)cdb[2]) << 56) |
3755                         (((uint64_t)cdb[3]) << 48) |
3756                         (((uint64_t)cdb[4]) << 40) |
3757                         (((uint64_t)cdb[5]) << 32) |
3758                         (((uint64_t)cdb[6]) << 24) |
3759                         (((uint64_t)cdb[7]) << 16) |
3760                         (((uint64_t)cdb[8]) <<  8) |
3761                         ((uint64_t)cdb[9]);
3762                 break;
3763         case WRITE_12:
3764         case READ_12:
3765                 cnt =   (((uint32_t)cdb[6]) <<  16) |
3766                         (((uint32_t)cdb[7]) <<   8) |
3767                         ((u_int32_t)cdb[8]);
3768
3769                 lba =   (((uint32_t)cdb[2]) << 24) |
3770                         (((uint32_t)cdb[3]) << 16) |
3771                         (((uint32_t)cdb[4]) <<  8) |
3772                         ((uint32_t)cdb[5]);
3773                 break;
3774         case WRITE_10:
3775         case READ_10:
3776                 cnt =   (((uint32_t)cdb[7]) <<  8) |
3777                         ((u_int32_t)cdb[8]);
3778
3779                 lba =   (((uint32_t)cdb[2]) << 24) |
3780                         (((uint32_t)cdb[3]) << 16) |
3781                         (((uint32_t)cdb[4]) <<  8) |
3782                         ((uint32_t)cdb[5]);
3783                 break;
3784         case WRITE_6:
3785         case READ_6:
3786                 cnt = cdb[4];
3787                 if (cnt == 0) {
3788                         cnt = 256;
3789                 }
3790                 lba =   (((uint32_t)cdb[1] & 0x1f) << 16) |
3791                         (((uint32_t)cdb[2]) << 8) |
3792                         ((uint32_t)cdb[3]);
3793                 break;
3794         default:
3795                 return (-1);
3796         }
3797
3798         cnt <<= DISK_SHIFT;
3799         lba <<= DISK_SHIFT;
3800
3801         if (offset == cnt) {
3802                 *lp = 1;
3803                 return (0);
3804         }
3805
3806         if (lba + cnt > dl) {
3807                 return (-1);
3808         }
3809
3810
3811         curcnt = MAX_ISP_TARG_TRANSFER;
3812         if (offset + curcnt >= cnt) {
3813                 curcnt = cnt - offset;
3814                 *lp = 1;
3815         } else {
3816                 *lp = 0;
3817         }
3818         *tl = curcnt;
3819         *kp = &dp[lba + offset];
3820         return (0);
3821 }
3822
3823 #endif
3824 #endif
3825
3826 static void
3827 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
3828 {
3829         struct cam_sim *sim;
3830         int bus, tgt;
3831         ispsoftc_t *isp;
3832
3833         sim = (struct cam_sim *)cbarg;
3834         isp = (ispsoftc_t *) cam_sim_softc(sim);
3835         bus = cam_sim_bus(sim);
3836         tgt = xpt_path_target_id(path);
3837
3838         switch (code) {
3839         case AC_LOST_DEVICE:
3840                 if (IS_SCSI(isp)) {
3841                         uint16_t oflags, nflags;
3842                         sdparam *sdp = SDPARAM(isp, bus);
3843
3844                         if (tgt >= 0) {
3845                                 nflags = sdp->isp_devparam[tgt].nvrm_flags;
3846 #ifndef ISP_TARGET_MODE
3847                                 nflags &= DPARM_SAFE_DFLT;
3848                                 if (isp->isp_loaded_fw) {
3849                                         nflags |= DPARM_NARROW | DPARM_ASYNC;
3850                                 }
3851 #else
3852                                 nflags = DPARM_DEFAULT;
3853 #endif
3854                                 oflags = sdp->isp_devparam[tgt].goal_flags;
3855                                 sdp->isp_devparam[tgt].goal_flags = nflags;
3856                                 sdp->isp_devparam[tgt].dev_update = 1;
3857                                 sdp->update = 1;
3858                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3859                                 sdp->isp_devparam[tgt].goal_flags = oflags;
3860                         }
3861                 }
3862                 break;
3863         default:
3864                 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
3865                 break;
3866         }
3867 }
3868
3869 static void
3870 isp_poll(struct cam_sim *sim)
3871 {
3872         ispsoftc_t *isp = cam_sim_softc(sim);
3873         uint32_t isr;
3874         uint16_t sema, mbox;
3875
3876         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3877                 isp_intr(isp, isr, sema, mbox);
3878         }
3879 }
3880
3881
3882 static void
3883 isp_watchdog(void *arg)
3884 {
3885         struct ccb_scsiio *xs = arg;
3886         ispsoftc_t *isp;
3887         uint32_t ohandle = ISP_HANDLE_FREE, handle;
3888
3889         isp = XS_ISP(xs);
3890
3891         handle = isp_find_handle(isp, xs);
3892
3893         if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
3894                 isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
3895                 callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
3896                 XS_CMD_S_WPEND(xs);
3897                 return;
3898         }
3899         XS_C_TACTIVE(xs);
3900
3901         /*
3902          * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
3903          */
3904         if (handle != ISP_HANDLE_FREE) {
3905                 uint32_t isr;
3906                 uint16_t sema, mbox;
3907                 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
3908                         isp_intr(isp, isr, sema, mbox);
3909                 }
3910                 ohandle = handle;
3911                 handle = isp_find_handle(isp, xs);
3912         }
3913         if (handle != ISP_HANDLE_FREE) {
3914                 /*
3915                  * Try and make sure the command is really dead before
3916                  * we release the handle (and DMA resources) for reuse.
3917                  *
3918                  * If we are successful in aborting the command then
3919                  * we're done here because we'll get the command returned
3920                  * back separately.
3921                  */
3922                 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
3923                         return;
3924                 }
3925
3926                 /*
3927                  * Note that after calling the above, the command may in
3928                  * fact have been completed.
3929                  */
3930                 xs = isp_find_xs(isp, handle);
3931
3932                 /*
3933                  * If the command no longer exists, then we won't
3934                  * be able to find the xs again with this handle.
3935                  */
3936                 if (xs == NULL) {
3937                         return;
3938                 }
3939
3940                 /*
3941                  * After this point, the command is really dead.
3942                  */
3943                 if (XS_XFRLEN(xs)) {
3944                         ISP_DMAFREE(isp, xs, handle);
3945                 } 
3946                 isp_destroy_handle(isp, handle);
3947                 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
3948                 XS_SETERR(xs, CAM_CMD_TIMEOUT);
3949                 isp_prt_endcmd(isp, xs);
3950                 isp_done(xs);
3951         } else {
3952                 if (ohandle != ISP_HANDLE_FREE) {
3953                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
3954                 } else {
3955                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
3956                 }
3957         }
3958 }
3959
3960 static void
3961 isp_make_here(ispsoftc_t *isp, int chan, int tgt)
3962 {
3963         union ccb *ccb;
3964         struct isp_fc *fc = ISP_FC_PC(isp, chan);
3965
3966         if (isp_autoconfig == 0) {
3967                 return;
3968         }
3969
3970         /*
3971          * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
3972          */
3973         ccb = xpt_alloc_ccb_nowait();
3974         if (ccb == NULL) {
3975                 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
3976                 return;
3977         }
3978         if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3979                 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
3980                 xpt_free_ccb(ccb);
3981                 return;
3982         }
3983         xpt_rescan(ccb);
3984 }
3985
3986 static void
3987 isp_make_gone(ispsoftc_t *isp, int chan, int tgt)
3988 {
3989         struct cam_path *tp;
3990         struct isp_fc *fc = ISP_FC_PC(isp, chan);
3991
3992         if (isp_autoconfig == 0) {
3993                 return;
3994         }
3995         if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
3996                 xpt_async(AC_LOST_DEVICE, tp, NULL);
3997                 xpt_free_path(tp);
3998         }
3999 }
4000
4001 /*
4002  * Gone Device Timer Function- when we have decided that a device has gone
4003  * away, we wait a specific period of time prior to telling the OS it has
4004  * gone away.
4005  *
4006  * This timer function fires once a second and then scans the port database
4007  * for devices that are marked dead but still have a virtual target assigned.
4008  * We decrement a counter for that port database entry, and when it hits zero,
4009  * we tell the OS the device has gone away.
4010  */
4011 static void
4012 isp_gdt(void *arg)
4013 {
4014         struct isp_fc *fc = arg;
4015         taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4016 }
4017
4018 static void
4019 isp_gdt_task(void *arg, int pending)
4020 {
4021         struct isp_fc *fc = arg;
4022         ispsoftc_t *isp = fc->isp;
4023         int chan = fc - isp->isp_osinfo.pc.fc;
4024         fcportdb_t *lp;
4025         int dbidx, tgt, more_to_do = 0;
4026
4027         ISP_LOCK(isp);
4028         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4029         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4030                 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4031
4032                 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4033                         continue;
4034                 }
4035                 if (lp->dev_map_idx == 0 || lp->target_mode) {
4036                         continue;
4037                 }
4038                 if (lp->gone_timer != 0) {
4039                         isp_prt(isp, ISP_LOGSANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
4040                         lp->gone_timer -= 1;
4041                         more_to_do++;
4042                         continue;
4043                 }
4044                 tgt = lp->dev_map_idx - 1;
4045                 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4046                 lp->dev_map_idx = 0;
4047                 lp->state = FC_PORTDB_STATE_NIL;
4048                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Gone Device Timeout");
4049                 isp_make_gone(isp, chan, tgt);
4050         }
4051         if (fc->ready) {
4052                 if (more_to_do) {
4053                         callout_reset(&fc->gdt, hz, isp_gdt, fc);
4054                 } else {
4055                         callout_deactivate(&fc->gdt);
4056                         isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4057                 }
4058         }
4059         ISP_UNLOCK(isp);
4060 }
4061
4062 /*
4063  * Loop Down Timer Function- when loop goes down, a timer is started and
4064  * and after it expires we come here and take all probational devices that
4065  * the OS knows about and the tell the OS that they've gone away.
4066  * 
4067  * We don't clear the devices out of our port database because, when loop
4068  * come back up, we have to do some actual cleanup with the chip at that
4069  * point (implicit PLOGO, e.g., to get the chip's port database state right).
4070  */
4071 static void
4072 isp_ldt(void *arg)
4073 {
4074         struct isp_fc *fc = arg;
4075         taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4076 }
4077
4078 static void
4079 isp_ldt_task(void *arg, int pending)
4080 {
4081         struct isp_fc *fc = arg;
4082         ispsoftc_t *isp = fc->isp;
4083         int chan = fc - isp->isp_osinfo.pc.fc;
4084         fcportdb_t *lp;
4085         int dbidx, tgt, i;
4086
4087         ISP_LOCK(isp);
4088         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4089         callout_deactivate(&fc->ldt);
4090
4091         /*
4092          * Notify to the OS all targets who we now consider have departed.
4093          */
4094         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4095                 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4096
4097                 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
4098                         continue;
4099                 }
4100                 if (lp->dev_map_idx == 0 || lp->target_mode) {
4101                         continue;
4102                 }
4103
4104                 /*
4105                  * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4106                  */
4107                 
4108
4109                 for (i = 0; i < isp->isp_maxcmds; i++) {
4110                         struct ccb_scsiio *xs;
4111
4112                         if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4113                                 continue;
4114                         }
4115                         if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4116                                 continue;
4117                         }
4118                         if (dbidx != (FCPARAM(isp, chan)->isp_dev_map[XS_TGT(xs)] - 1)) {
4119                                 continue;
4120                         }
4121                         isp_prt(isp, ISP_LOGWARN, "command handle 0x%08x for %d.%d.%d orphaned by loop down timeout",
4122                             isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4123                 }
4124
4125                 /*
4126                  * Mark that we've announced that this device is gone....
4127                  */
4128                 lp->reserved = 1;
4129
4130                 /*
4131                  * but *don't* change the state of the entry. Just clear
4132                  * any target id stuff and announce to CAM that the
4133                  * device is gone. This way any necessary PLOGO stuff
4134                  * will happen when loop comes back up.
4135                  */
4136
4137                 tgt = lp->dev_map_idx - 1;
4138                 FCPARAM(isp, chan)->isp_dev_map[tgt] = 0;
4139                 lp->dev_map_idx = 0;
4140                 lp->state = FC_PORTDB_STATE_NIL;
4141                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, lp->portid, tgt, "Loop Down Timeout");
4142                 isp_make_gone(isp, chan, tgt);
4143         }
4144
4145         if (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR) {
4146                 isp_unfreeze_loopdown(isp, chan);
4147         }
4148         /*
4149          * The loop down timer has expired. Wake up the kthread
4150          * to notice that fact (or make it false).
4151          */
4152         fc->loop_dead = 1;
4153         fc->loop_down_time = fc->loop_down_limit+1;
4154         wakeup(fc);
4155         ISP_UNLOCK(isp);
4156 }
4157
4158 static void
4159 isp_kthread(void *arg)
4160 {
4161         struct isp_fc *fc = arg;
4162         ispsoftc_t *isp = fc->isp;
4163         int chan = fc - isp->isp_osinfo.pc.fc;
4164         int slp = 0;
4165
4166         mtx_lock(&isp->isp_osinfo.lock);
4167
4168         for (;;) {
4169                 int lb, lim;
4170
4171                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4172                 lb = isp_fc_runstate(isp, chan, 250000);
4173
4174                 /*
4175                  * Our action is different based upon whether we're supporting
4176                  * Initiator mode or not. If we are, we might freeze the simq
4177                  * when loop is down and set all sorts of different delays to
4178                  * check again.
4179                  *
4180                  * If not, we simply just wait for loop to come up.
4181                  */
4182                 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4183                         /*
4184                          * Increment loop down time by the last sleep interval
4185                          */
4186                         fc->loop_down_time += slp;
4187
4188                         if (lb < 0) {
4189                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4190                         } else {
4191                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4192                         }
4193
4194                         /*
4195                          * If we've never seen loop up and we've waited longer
4196                          * than quickboot time, or we've seen loop up but we've
4197                          * waited longer than loop_down_limit, give up and go
4198                          * to sleep until loop comes up.
4199                          */
4200                         if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4201                                 lim = isp_quickboot_time;
4202                         } else {
4203                                 lim = fc->loop_down_limit;
4204                         }
4205                         if (fc->loop_down_time >= lim) {
4206                                 isp_freeze_loopdown(isp, chan, "loop limit hit");
4207                                 slp = 0;
4208                         } else if (fc->loop_down_time < 10) {
4209                                 slp = 1;
4210                         } else if (fc->loop_down_time < 30) {
4211                                 slp = 5;
4212                         } else if (fc->loop_down_time < 60) {
4213                                 slp = 10;
4214                         } else if (fc->loop_down_time < 120) {
4215                                 slp = 20;
4216                         } else {
4217                                 slp = 30;
4218                         }
4219
4220                 } else if (lb) {
4221                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4222                         fc->loop_down_time += slp;
4223                         slp = 60;
4224                 } else {
4225                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4226                         fc->loop_down_time = 0;
4227                         slp = 0;
4228                 }
4229
4230
4231                 /*
4232                  * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4233                  * now so that CAM can start sending us commands.
4234                  *
4235                  * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4236                  * or kill the commands, as appropriate.
4237                  */
4238
4239                 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4240                         isp_unfreeze_loopdown(isp, chan);
4241                 }
4242
4243                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4244
4245                 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4246
4247                 /*
4248                  * If slp is zero, we're waking up for the first time after
4249                  * things have been okay. In this case, we set a deferral state
4250                  * for all commands and delay hysteresis seconds before starting
4251                  * the FC state evaluation. This gives the loop/fabric a chance
4252                  * to settle.
4253                  */
4254                 if (slp == 0 && fc->hysteresis) {
4255                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4256                         mtx_unlock(&isp->isp_osinfo.lock);
4257                         pause("ispt", fc->hysteresis * hz);
4258                         mtx_lock(&isp->isp_osinfo.lock);
4259                 }
4260         }
4261         mtx_unlock(&isp->isp_osinfo.lock);
4262 }
4263
4264 static void
4265 isp_action(struct cam_sim *sim, union ccb *ccb)
4266 {
4267         int bus, tgt, ts, error, lim;
4268         ispsoftc_t *isp;
4269         struct ccb_trans_settings *cts;
4270
4271         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4272         
4273         isp = (ispsoftc_t *)cam_sim_softc(sim);
4274         mtx_assert(&isp->isp_lock, MA_OWNED);
4275
4276         if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4277                 isp_init(isp);
4278                 if (isp->isp_state != ISP_INITSTATE) {
4279                         /*
4280                          * Lie. Say it was a selection timeout.
4281                          */
4282                         ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
4283                         xpt_freeze_devq(ccb->ccb_h.path, 1);
4284                         xpt_done(ccb);
4285                         return;
4286                 }
4287                 isp->isp_state = ISP_RUNSTATE;
4288         }
4289         isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4290         ISP_PCMD(ccb) = NULL;
4291
4292         switch (ccb->ccb_h.func_code) {
4293         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
4294                 bus = XS_CHANNEL(ccb);
4295                 /*
4296                  * Do a couple of preliminary checks...
4297                  */
4298                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4299                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4300                                 ccb->ccb_h.status = CAM_REQ_INVALID;
4301                                 xpt_done(ccb);
4302                                 break;
4303                         }
4304                 }
4305 #ifdef  DIAGNOSTIC
4306                 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4307                         xpt_print(ccb->ccb_h.path, "invalid target\n");
4308                         ccb->ccb_h.status = CAM_PATH_INVALID;
4309                 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4310                         xpt_print(ccb->ccb_h.path, "invalid lun\n");
4311                         ccb->ccb_h.status = CAM_PATH_INVALID;
4312                 }
4313                 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4314                         xpt_done(ccb);
4315                         break;
4316                 }
4317 #endif
4318                 ccb->csio.scsi_status = SCSI_STATUS_OK;
4319                 if (isp_get_pcmd(isp, ccb)) {
4320                         isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4321                         cam_freeze_devq(ccb->ccb_h.path);
4322                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4323                         xpt_done(ccb);
4324                         break;
4325                 }
4326                 error = isp_start((XS_T *) ccb);
4327                 switch (error) {
4328                 case CMD_QUEUED:
4329                         XS_CMD_S_CLEAR(ccb);
4330                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
4331                         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4332                                 break;
4333                         }
4334                         ts = ccb->ccb_h.timeout;
4335                         if (ts == CAM_TIME_DEFAULT) {
4336                                 ts = 60*1000;
4337                         }
4338                         ts = isp_mstohz(ts);
4339                         XS_S_TACTIVE(ccb);
4340                         callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4341                         break;
4342                 case CMD_RQLATER:
4343                         /*
4344                          * We get this result for FC devices if the loop state isn't ready yet
4345                          * or if the device in question has gone zombie on us.
4346                          *
4347                          * If we've never seen Loop UP at all, we requeue this request and wait
4348                          * for the initial loop up delay to expire.
4349                          */
4350                         lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4351                         if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4352                                 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4353                                         isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4354                                 } else {
4355                                         isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4356                                 }
4357                                 ccb->ccb_h.status = CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
4358                                 xpt_freeze_devq(ccb->ccb_h.path, 1);
4359                                 isp_free_pcmd(isp, ccb);
4360                                 xpt_done(ccb);
4361                                 break;
4362                         }
4363                         isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
4364                         cam_freeze_devq(ccb->ccb_h.path);
4365                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4366                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
4367                         isp_free_pcmd(isp, ccb);
4368                         xpt_done(ccb);
4369                         break;
4370                 case CMD_EAGAIN:
4371                         isp_free_pcmd(isp, ccb);
4372                         cam_freeze_devq(ccb->ccb_h.path);
4373                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
4374                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
4375                         xpt_done(ccb);
4376                         break;
4377                 case CMD_COMPLETE:
4378                         isp_done((struct ccb_scsiio *) ccb);
4379                         break;
4380                 default:
4381                         isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
4382                         XS_SETERR(ccb, CAM_REQ_CMP_ERR);
4383                         isp_free_pcmd(isp, ccb);
4384                         xpt_done(ccb);
4385                 }
4386                 break;
4387
4388 #ifdef  ISP_TARGET_MODE
4389         case XPT_EN_LUN:                /* Enable/Disable LUN as a target */
4390                 if (ccb->cel.enable) {
4391                         isp_enable_lun(isp, ccb);
4392                 } else {
4393                         isp_disable_lun(isp, ccb);
4394                 }
4395                 break;
4396         case XPT_IMMED_NOTIFY:
4397         case XPT_IMMEDIATE_NOTIFY:      /* Add Immediate Notify Resource */
4398         case XPT_ACCEPT_TARGET_IO:      /* Add Accept Target IO Resource */
4399         {
4400                 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
4401                 if (tptr == NULL) {
4402                         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
4403                 }
4404                 if (tptr == NULL) {
4405                         const char *str;
4406                         uint32_t tag;
4407
4408                         if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4409                                 str = "XPT_IMMEDIATE_NOTIFY";
4410                                 tag = ccb->cin1.seq_id;
4411                         } else {
4412                                 tag = ccb->atio.tag_id;
4413                                 str = "XPT_ACCEPT_TARGET_IO";
4414                         }
4415                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
4416                         dump_tstates(isp, XS_CHANNEL(ccb));
4417                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4418                         break;
4419                 }
4420                 ccb->ccb_h.sim_priv.entries[0].field = 0;
4421                 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
4422                 ccb->ccb_h.flags = 0;
4423
4424                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4425                         if (ccb->atio.tag_id) {
4426                                 atio_private_data_t *atp = isp_get_atpd(isp, tptr, ccb->atio.tag_id);
4427                                 if (atp) {
4428                                         isp_put_atpd(isp, tptr, atp);
4429                                 }
4430                         }
4431                         tptr->atio_count++;
4432                         SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
4433                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
4434                             ((struct ccb_accept_tio *)ccb)->tag_id, tptr->atio_count);
4435                 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
4436                         if (ccb->cin1.tag_id) {
4437                                 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
4438                                 if (ntp) {
4439                                         isp_put_ntpd(isp, tptr, ntp);
4440                                 }
4441                         }
4442                         tptr->inot_count++;
4443                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4444                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4445                             ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4446                 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4447                         tptr->inot_count++;
4448                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
4449                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
4450                             ((struct ccb_immediate_notify *)ccb)->seq_id, tptr->inot_count);
4451                 }
4452                 rls_lun_statep(isp, tptr);
4453                 ccb->ccb_h.status = CAM_REQ_INPROG;
4454                 break;
4455         }
4456         case XPT_NOTIFY_ACK:
4457                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4458                 break;
4459         case XPT_NOTIFY_ACKNOWLEDGE:            /* notify ack */
4460         {
4461                 tstate_t *tptr;
4462                 inot_private_data_t *ntp;
4463
4464                 /*
4465                  * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
4466                  * XXX: matches that for the immediate notify, we have to *search* for the notify structure
4467                  */
4468                 /*
4469                  * All the relevant path information is in the associated immediate notify
4470                  */
4471                 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);
4472                 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
4473                 if (ntp == NULL) {
4474                         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__,
4475                              ccb->cna2.tag_id, ccb->cna2.seq_id);
4476                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
4477                         xpt_done(ccb);
4478                         break;
4479                 }
4480                 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
4481                         rls_lun_statep(isp, tptr);
4482                         cam_freeze_devq(ccb->ccb_h.path);
4483                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
4484                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
4485                         break;
4486                 }
4487                 isp_put_ntpd(isp, tptr, ntp);
4488                 rls_lun_statep(isp, tptr);
4489                 ccb->ccb_h.status = CAM_REQ_CMP;
4490                 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);
4491                 xpt_done(ccb);
4492                 break;
4493         }
4494         case XPT_CONT_TARGET_IO:
4495                 isp_target_start_ctio(isp, ccb);
4496                 break;
4497 #endif
4498         case XPT_RESET_DEV:             /* BDR the specified SCSI device */
4499
4500                 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
4501                 tgt = ccb->ccb_h.target_id;
4502                 tgt |= (bus << 16);
4503
4504                 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
4505                 if (error) {
4506                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4507                 } else {
4508                         ccb->ccb_h.status = CAM_REQ_CMP;
4509                 }
4510                 xpt_done(ccb);
4511                 break;
4512         case XPT_ABORT:                 /* Abort the specified CCB */
4513         {
4514                 union ccb *accb = ccb->cab.abort_ccb;
4515                 switch (accb->ccb_h.func_code) {
4516 #ifdef  ISP_TARGET_MODE
4517                 case XPT_ACCEPT_TARGET_IO:
4518                         isp_target_mark_aborted(isp, ccb);
4519                         break;
4520 #endif
4521                 case XPT_SCSI_IO:
4522                         error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
4523                         if (error) {
4524                                 ccb->ccb_h.status = CAM_UA_ABORT;
4525                         } else {
4526                                 ccb->ccb_h.status = CAM_REQ_CMP;
4527                         }
4528                         break;
4529                 default:
4530                         ccb->ccb_h.status = CAM_REQ_INVALID;
4531                         break;
4532                 }
4533                 /*
4534                  * This is not a queued CCB, so the caller expects it to be
4535                  * complete when control is returned.
4536                  */
4537                 break;
4538         }
4539 #define IS_CURRENT_SETTINGS(c)  (c->type == CTS_TYPE_CURRENT_SETTINGS)
4540         case XPT_SET_TRAN_SETTINGS:     /* Nexus Settings */
4541                 cts = &ccb->cts;
4542                 if (!IS_CURRENT_SETTINGS(cts)) {
4543                         ccb->ccb_h.status = CAM_REQ_INVALID;
4544                         xpt_done(ccb);
4545                         break;
4546                 }
4547                 tgt = cts->ccb_h.target_id;
4548                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4549                 if (IS_SCSI(isp)) {
4550                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4551                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4552                         sdparam *sdp = SDPARAM(isp, bus);
4553                         uint16_t *dptr;
4554
4555                         if (spi->valid == 0 && scsi->valid == 0) {
4556                                 ccb->ccb_h.status = CAM_REQ_CMP;
4557                                 xpt_done(ccb);
4558                                 break;
4559                         }
4560
4561                         /*
4562                          * We always update (internally) from goal_flags
4563                          * so any request to change settings just gets
4564                          * vectored to that location.
4565                          */
4566                         dptr = &sdp->isp_devparam[tgt].goal_flags;
4567
4568                         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
4569                                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
4570                                         *dptr |= DPARM_DISC;
4571                                 else
4572                                         *dptr &= ~DPARM_DISC;
4573                         }
4574
4575                         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
4576                                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
4577                                         *dptr |= DPARM_TQING;
4578                                 else
4579                                         *dptr &= ~DPARM_TQING;
4580                         }
4581
4582                         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
4583                                 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
4584                                         *dptr |= DPARM_WIDE;
4585                                 else
4586                                         *dptr &= ~DPARM_WIDE;
4587                         }
4588
4589                         /*
4590                          * XXX: FIX ME
4591                          */
4592                         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
4593                                 *dptr |= DPARM_SYNC;
4594                                 /*
4595                                  * XXX: CHECK FOR LEGALITY
4596                                  */
4597                                 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
4598                                 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
4599                         } else {
4600                                 *dptr &= ~DPARM_SYNC;
4601                         }
4602                         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,
4603                             sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
4604                         sdp->isp_devparam[tgt].dev_update = 1;
4605                         sdp->update = 1;
4606                 }
4607                 ccb->ccb_h.status = CAM_REQ_CMP;
4608                 xpt_done(ccb);
4609                 break;
4610         case XPT_GET_TRAN_SETTINGS:
4611                 cts = &ccb->cts;
4612                 tgt = cts->ccb_h.target_id;
4613                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
4614                 if (IS_FC(isp)) {
4615                         fcparam *fcp = FCPARAM(isp, bus);
4616                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4617                         struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
4618                         unsigned int hdlidx;
4619
4620                         cts->protocol = PROTO_SCSI;
4621                         cts->protocol_version = SCSI_REV_2;
4622                         cts->transport = XPORT_FC;
4623                         cts->transport_version = 0;
4624
4625                         scsi->valid = CTS_SCSI_VALID_TQ;
4626                         scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
4627                         fc->valid = CTS_FC_VALID_SPEED;
4628                         fc->bitrate = 100000;
4629                         fc->bitrate *= fcp->isp_gbspeed;
4630                         hdlidx = fcp->isp_dev_map[tgt] - 1;
4631                         if (hdlidx < MAX_FC_TARG) {
4632                                 fcportdb_t *lp = &fcp->portdb[hdlidx];
4633                                 fc->wwnn = lp->node_wwn;
4634                                 fc->wwpn = lp->port_wwn;
4635                                 fc->port = lp->portid;
4636                                 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
4637                         }
4638                 } else {
4639                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
4640                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
4641                         sdparam *sdp = SDPARAM(isp, bus);
4642                         uint16_t dval, pval, oval;
4643
4644                         if (IS_CURRENT_SETTINGS(cts)) {
4645                                 sdp->isp_devparam[tgt].dev_refresh = 1;
4646                                 sdp->update = 1;
4647                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4648                                 dval = sdp->isp_devparam[tgt].actv_flags;
4649                                 oval = sdp->isp_devparam[tgt].actv_offset;
4650                                 pval = sdp->isp_devparam[tgt].actv_period;
4651                         } else {
4652                                 dval = sdp->isp_devparam[tgt].nvrm_flags;
4653                                 oval = sdp->isp_devparam[tgt].nvrm_offset;
4654                                 pval = sdp->isp_devparam[tgt].nvrm_period;
4655                         }
4656
4657                         cts->protocol = PROTO_SCSI;
4658                         cts->protocol_version = SCSI_REV_2;
4659                         cts->transport = XPORT_SPI;
4660                         cts->transport_version = 2;
4661
4662                         spi->valid = 0;
4663                         scsi->valid = 0;
4664                         spi->flags = 0;
4665                         scsi->flags = 0;
4666                         if (dval & DPARM_DISC) {
4667                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
4668                         }
4669                         if ((dval & DPARM_SYNC) && oval && pval) {
4670                                 spi->sync_offset = oval;
4671                                 spi->sync_period = pval;
4672                         } else {
4673                                 spi->sync_offset = 0;
4674                                 spi->sync_period = 0;
4675                         }
4676                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
4677                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
4678                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
4679                         if (dval & DPARM_WIDE) {
4680                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
4681                         } else {
4682                                 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
4683                         }
4684                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4685                                 scsi->valid = CTS_SCSI_VALID_TQ;
4686                                 if (dval & DPARM_TQING) {
4687                                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4688                                 }
4689                                 spi->valid |= CTS_SPI_VALID_DISC;
4690                         }
4691                         isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
4692                             bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
4693                 }
4694                 ccb->ccb_h.status = CAM_REQ_CMP;
4695                 xpt_done(ccb);
4696                 break;
4697
4698         case XPT_CALC_GEOMETRY:
4699                 cam_calc_geometry(&ccb->ccg, 1);
4700                 xpt_done(ccb);
4701                 break;
4702
4703         case XPT_RESET_BUS:             /* Reset the specified bus */
4704                 bus = cam_sim_bus(sim);
4705                 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
4706                 if (error) {
4707                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4708                         xpt_done(ccb);
4709                         break;
4710                 }
4711                 if (bootverbose) {
4712                         xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
4713                 }
4714                 if (IS_FC(isp)) {
4715                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
4716                 } else {
4717                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
4718                 }
4719                 ccb->ccb_h.status = CAM_REQ_CMP;
4720                 xpt_done(ccb);
4721                 break;
4722
4723         case XPT_TERM_IO:               /* Terminate the I/O process */
4724                 ccb->ccb_h.status = CAM_REQ_INVALID;
4725                 xpt_done(ccb);
4726                 break;
4727
4728         case XPT_SET_SIM_KNOB:          /* Set SIM knobs */
4729         {
4730                 struct ccb_sim_knob *kp = &ccb->knob;
4731                 fcparam *fcp;
4732
4733
4734                 if (!IS_FC(isp)) {
4735                         ccb->ccb_h.status = CAM_REQ_INVALID;
4736                         xpt_done(ccb);
4737                         break;
4738                 }
4739
4740                 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4741                 fcp = FCPARAM(isp, bus);
4742
4743                 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
4744                         fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
4745                         fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
4746                         isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
4747                 }
4748                 ccb->ccb_h.status = CAM_REQ_CMP;
4749                 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
4750                         int rchange = 0;
4751                         int newrole = 0;
4752
4753                         switch (kp->xport_specific.fc.role) {
4754                         case KNOB_ROLE_NONE:
4755                                 if (fcp->role != ISP_ROLE_NONE) {
4756                                         rchange = 1;
4757                                         newrole = ISP_ROLE_NONE;
4758                                 }
4759                                 break;
4760                         case KNOB_ROLE_TARGET:
4761                                 if (fcp->role != ISP_ROLE_TARGET) {
4762                                         rchange = 1;
4763                                         newrole = ISP_ROLE_TARGET;
4764                                 }
4765                                 break;
4766                         case KNOB_ROLE_INITIATOR:
4767                                 if (fcp->role != ISP_ROLE_INITIATOR) {
4768                                         rchange = 1;
4769                                         newrole = ISP_ROLE_INITIATOR;
4770                                 }
4771                                 break;
4772                         case KNOB_ROLE_BOTH:
4773 #if 0
4774                                 if (fcp->role != ISP_ROLE_BOTH) {
4775                                         rchange = 1;
4776                                         newrole = ISP_ROLE_BOTH;
4777                                 }
4778 #else
4779                                 /*
4780                                  * We don't really support dual role at present on FC cards.
4781                                  *
4782                                  * We should, but a bunch of things are currently broken,
4783                                  * so don't allow it.
4784                                  */
4785                                 isp_prt(isp, ISP_LOGERR, "cannot support dual role at present");
4786                                 ccb->ccb_h.status = CAM_REQ_INVALID;
4787 #endif
4788                                 break;
4789                         }
4790                         if (rchange) {
4791                                 if (isp_fc_change_role(isp, bus, newrole) != 0) {
4792                                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
4793 #ifdef  ISP_TARGET_MODE
4794                                 } else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
4795                                         isp_enable_deferred_luns(isp, bus);
4796 #endif
4797                                 }
4798                         }
4799                 }
4800                 xpt_done(ccb);
4801                 break;
4802         }
4803         case XPT_GET_SIM_KNOB:          /* Set SIM knobs */
4804         {
4805                 struct ccb_sim_knob *kp = &ccb->knob;
4806
4807                 if (IS_FC(isp)) {
4808                         fcparam *fcp;
4809
4810                         bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
4811                         fcp = FCPARAM(isp, bus);
4812
4813                         kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
4814                         kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
4815                         switch (fcp->role) {
4816                         case ISP_ROLE_NONE:
4817                                 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
4818                                 break;
4819                         case ISP_ROLE_TARGET:
4820                                 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
4821                                 break;
4822                         case ISP_ROLE_INITIATOR:
4823                                 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
4824                                 break;
4825                         case ISP_ROLE_BOTH:
4826                                 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
4827                                 break;
4828                         }
4829                         kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
4830                         ccb->ccb_h.status = CAM_REQ_CMP;
4831                 } else {
4832                         ccb->ccb_h.status = CAM_REQ_INVALID;
4833                 }
4834                 xpt_done(ccb);
4835                 break;
4836         }
4837         case XPT_PATH_INQ:              /* Path routing inquiry */
4838         {
4839                 struct ccb_pathinq *cpi = &ccb->cpi;
4840
4841                 cpi->version_num = 1;
4842 #ifdef  ISP_TARGET_MODE
4843                 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
4844 #else
4845                 cpi->target_sprt = 0;
4846 #endif
4847                 cpi->hba_eng_cnt = 0;
4848                 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
4849                 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
4850                 cpi->bus_id = cam_sim_bus(sim);
4851                 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
4852                 if (IS_FC(isp)) {
4853                         fcparam *fcp = FCPARAM(isp, bus);
4854
4855                         cpi->hba_misc = PIM_NOBUSRESET;
4856
4857                         /*
4858                          * Because our loop ID can shift from time to time,
4859                          * make our initiator ID out of range of our bus.
4860                          */
4861                         cpi->initiator_id = cpi->max_target + 1;
4862
4863                         /*
4864                          * Set base transfer capabilities for Fibre Channel, for this HBA.
4865                          */
4866                         if (IS_25XX(isp)) {
4867                                 cpi->base_transfer_speed = 8000000;
4868                         } else if (IS_24XX(isp)) {
4869                                 cpi->base_transfer_speed = 4000000;
4870                         } else if (IS_23XX(isp)) {
4871                                 cpi->base_transfer_speed = 2000000;
4872                         } else {
4873                                 cpi->base_transfer_speed = 1000000;
4874                         }
4875                         cpi->hba_inquiry = PI_TAG_ABLE;
4876                         cpi->transport = XPORT_FC;
4877                         cpi->transport_version = 0;
4878                         cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
4879                         cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
4880                         cpi->xport_specific.fc.port = fcp->isp_portid;
4881                         cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
4882                 } else {
4883                         sdparam *sdp = SDPARAM(isp, bus);
4884                         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
4885                         cpi->hba_misc = 0;
4886                         cpi->initiator_id = sdp->isp_initiator_id;
4887                         cpi->base_transfer_speed = 3300;
4888                         cpi->transport = XPORT_SPI;
4889                         cpi->transport_version = 2;
4890                 }
4891                 cpi->protocol = PROTO_SCSI;
4892                 cpi->protocol_version = SCSI_REV_2;
4893                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4894                 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
4895                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
4896                 cpi->unit_number = cam_sim_unit(sim);
4897                 cpi->ccb_h.status = CAM_REQ_CMP;
4898                 xpt_done(ccb);
4899                 break;
4900         }
4901         default:
4902                 ccb->ccb_h.status = CAM_REQ_INVALID;
4903                 xpt_done(ccb);
4904                 break;
4905         }
4906 }
4907
4908 #define ISPDDB  (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
4909
4910 void
4911 isp_done(XS_T *sccb)
4912 {
4913         ispsoftc_t *isp = XS_ISP(sccb);
4914         uint32_t status;
4915
4916         if (XS_NOERR(sccb))
4917                 XS_SETERR(sccb, CAM_REQ_CMP);
4918
4919         if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
4920                 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
4921                 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
4922                         sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
4923                 } else {
4924                         sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
4925                 }
4926         }
4927
4928         sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4929         status = sccb->ccb_h.status & CAM_STATUS_MASK;
4930         if (status != CAM_REQ_CMP) {
4931                 if (status != CAM_SEL_TIMEOUT)
4932                         isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
4933                 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
4934                         sccb->ccb_h.status |= CAM_DEV_QFRZN;
4935                         xpt_freeze_devq(sccb->ccb_h.path, 1);
4936                 }
4937         }
4938
4939         if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4940                 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
4941         }
4942
4943         XS_CMD_S_DONE(sccb);
4944         if (XS_TACTIVE_P(sccb))
4945                 callout_stop(&PISP_PCMD(sccb)->wdog);
4946         XS_CMD_S_CLEAR(sccb);
4947         isp_free_pcmd(isp, (union ccb *) sccb);
4948         xpt_done((union ccb *) sccb);
4949 }
4950
4951 void
4952 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
4953 {
4954         int bus;
4955         static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
4956         static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
4957         char *msg = NULL;
4958         target_id_t tgt;
4959         fcportdb_t *lp;
4960         struct isp_fc *fc;
4961         struct cam_path *tmppath;
4962         va_list ap;
4963
4964         switch (cmd) {
4965         case ISPASYNC_NEW_TGT_PARAMS:
4966         {
4967                 struct ccb_trans_settings_scsi *scsi;
4968                 struct ccb_trans_settings_spi *spi;
4969                 int flags, tgt;
4970                 sdparam *sdp;
4971                 struct ccb_trans_settings cts;
4972
4973                 memset(&cts, 0, sizeof (struct ccb_trans_settings));
4974
4975                 va_start(ap, cmd);
4976                 bus = va_arg(ap, int);
4977                 tgt = va_arg(ap, int);
4978                 va_end(ap);
4979                 sdp = SDPARAM(isp, bus);
4980
4981                 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4982                         isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
4983                         break;
4984                 }
4985                 flags = sdp->isp_devparam[tgt].actv_flags;
4986                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
4987                 cts.protocol = PROTO_SCSI;
4988                 cts.transport = XPORT_SPI;
4989
4990                 scsi = &cts.proto_specific.scsi;
4991                 spi = &cts.xport_specific.spi;
4992
4993                 if (flags & DPARM_TQING) {
4994                         scsi->valid |= CTS_SCSI_VALID_TQ;
4995                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
4996                 }
4997
4998                 if (flags & DPARM_DISC) {
4999                         spi->valid |= CTS_SPI_VALID_DISC;
5000                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5001                 }
5002                 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5003                 if (flags & DPARM_WIDE) {
5004                         spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5005                 } else {
5006                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5007                 }
5008                 if (flags & DPARM_SYNC) {
5009                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5010                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5011                         spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5012                         spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5013                 }
5014                 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);
5015                 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5016                 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5017                 xpt_free_path(tmppath);
5018                 break;
5019         }
5020         case ISPASYNC_BUS_RESET:
5021         {
5022                 va_start(ap, cmd);
5023                 bus = va_arg(ap, int);
5024                 va_end(ap);
5025                 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5026                 if (IS_FC(isp)) {
5027                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5028                 } else {
5029                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5030                 }
5031                 break;
5032         }
5033         case ISPASYNC_LIP:
5034                 if (msg == NULL) {
5035                         msg = "LIP Received";
5036                 }
5037                 /* FALLTHROUGH */
5038         case ISPASYNC_LOOP_RESET:
5039                 if (msg == NULL) {
5040                         msg = "LOOP Reset";
5041                 }
5042                 /* FALLTHROUGH */
5043         case ISPASYNC_LOOP_DOWN:
5044         {
5045                 if (msg == NULL) {
5046                         msg = "LOOP Down";
5047                 }
5048                 va_start(ap, cmd);
5049                 bus = va_arg(ap, int);
5050                 va_end(ap);
5051
5052                 FCPARAM(isp, bus)->link_active = 0;
5053
5054                 fc = ISP_FC_PC(isp, bus);
5055                 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5056                         /*
5057                          * We don't do any simq freezing if we are only in target mode
5058                          */
5059                         if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5060                                 if (fc->path) {
5061                                         isp_freeze_loopdown(isp, bus, msg);
5062                                 }
5063                                 if (!callout_active(&fc->ldt)) {
5064                                         callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5065                                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5066                                 }
5067                         }
5068                 }
5069                 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5070                 break;
5071         }
5072         case ISPASYNC_LOOP_UP:
5073                 va_start(ap, cmd);
5074                 bus = va_arg(ap, int);
5075                 va_end(ap);
5076                 fc = ISP_FC_PC(isp, bus);
5077                 /*
5078                  * Now we just note that Loop has come up. We don't
5079                  * actually do anything because we're waiting for a
5080                  * Change Notify before activating the FC cleanup
5081                  * thread to look at the state of the loop again.
5082                  */
5083                 FCPARAM(isp, bus)->link_active = 1;
5084                 fc->loop_dead = 0;
5085                 fc->loop_down_time = 0;
5086                 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5087                 break;
5088         case ISPASYNC_DEV_ARRIVED:
5089                 va_start(ap, cmd);
5090                 bus = va_arg(ap, int);
5091                 lp = va_arg(ap, fcportdb_t *);
5092                 va_end(ap);
5093                 fc = ISP_FC_PC(isp, bus);
5094                 lp->reserved = 0;
5095                 lp->gone_timer = 0;
5096                 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
5097                         int dbidx = lp - FCPARAM(isp, bus)->portdb;
5098                         int i;
5099
5100                         for (i = 0; i < MAX_FC_TARG; i++) {
5101                                 if (i >= FL_ID && i <= SNS_ID) {
5102                                         continue;
5103                                 }
5104                                 if (FCPARAM(isp, bus)->isp_dev_map[i] == 0) {
5105                                         break;
5106                                 }
5107                         }
5108                         if (i < MAX_FC_TARG) {
5109                                 FCPARAM(isp, bus)->isp_dev_map[i] = dbidx + 1;
5110                                 lp->dev_map_idx = i + 1;
5111                         } else {
5112                                 isp_prt(isp, ISP_LOGWARN, "out of target ids");
5113                                 isp_dump_portdb(isp, bus);
5114                         }
5115                 }
5116                 if (lp->dev_map_idx) {
5117                         tgt = lp->dev_map_idx - 1;
5118                         isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5119                         isp_make_here(isp, bus, tgt);
5120                 } else {
5121                         isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5122                 }
5123                 break;
5124         case ISPASYNC_DEV_CHANGED:
5125                 va_start(ap, cmd);
5126                 bus = va_arg(ap, int);
5127                 lp = va_arg(ap, fcportdb_t *);
5128                 va_end(ap);
5129                 fc = ISP_FC_PC(isp, bus);
5130                 lp->reserved = 0;
5131                 lp->gone_timer = 0;
5132                 if (isp_change_is_bad) {
5133                         lp->state = FC_PORTDB_STATE_NIL;
5134                         if (lp->dev_map_idx) {
5135                                 tgt = lp->dev_map_idx - 1;
5136                                 FCPARAM(isp, bus)->isp_dev_map[tgt] = 0;
5137                                 lp->dev_map_idx = 0;
5138                                 isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
5139                                 isp_make_gone(isp, bus, tgt);
5140                         } else {
5141                                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
5142                                     (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5143                         }
5144                 } else {
5145                         lp->portid = lp->new_portid;
5146                         lp->roles = lp->new_roles;
5147                         if (lp->dev_map_idx) {
5148                                 int t = lp->dev_map_idx - 1;
5149                                 FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
5150                                 tgt = lp->dev_map_idx - 1;
5151                                 isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
5152                                     (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5153                         } else {
5154                                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5155                         }
5156                 }
5157                 break;
5158         case ISPASYNC_DEV_STAYED:
5159                 va_start(ap, cmd);
5160                 bus = va_arg(ap, int);
5161                 lp = va_arg(ap, fcportdb_t *);
5162                 va_end(ap);
5163                 if (lp->dev_map_idx) {
5164                         tgt = lp->dev_map_idx - 1;
5165                         isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
5166                             (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5167                 } else {
5168                         isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
5169                             (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5170                 }
5171                 break;
5172         case ISPASYNC_DEV_GONE:
5173                 va_start(ap, cmd);
5174                 bus = va_arg(ap, int);
5175                 lp = va_arg(ap, fcportdb_t *);
5176                 va_end(ap);
5177                 fc = ISP_FC_PC(isp, bus);
5178                 /*
5179                  * If this has a virtual target and we haven't marked it
5180                  * that we're going to have isp_gdt tell the OS it's gone,
5181                  * set the isp_gdt timer running on it.
5182                  *
5183                  * If it isn't marked that isp_gdt is going to get rid of it,
5184                  * announce that it's gone.
5185                  *
5186                  */
5187                 if (lp->dev_map_idx && lp->reserved == 0) {
5188                         lp->reserved = 1;
5189                         lp->state = FC_PORTDB_STATE_ZOMBIE;
5190                         lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
5191                         if (fc->ready && !callout_active(&fc->gdt)) {
5192                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
5193                                 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5194                         }
5195                         tgt = lp->dev_map_idx - 1;
5196                         isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5197                 } else if (lp->reserved == 0) {
5198                         isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
5199                 }
5200                 break;
5201         case ISPASYNC_CHANGE_NOTIFY:
5202         {
5203                 char *msg;
5204                 int evt, nphdl, nlstate, reason;
5205
5206                 va_start(ap, cmd);
5207                 bus = va_arg(ap, int);
5208                 evt = va_arg(ap, int);
5209                 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5210                         nphdl = va_arg(ap, int);
5211                         nlstate = va_arg(ap, int);
5212                         reason = va_arg(ap, int);
5213                 } else {
5214                         nphdl = NIL_HANDLE;
5215                         nlstate = reason = 0;
5216                 }
5217                 va_end(ap);
5218                 fc = ISP_FC_PC(isp, bus);
5219
5220                 if (evt == ISPASYNC_CHANGE_PDB) {
5221                         msg = "Chan %d Port Database Changed";
5222                 } else if (evt == ISPASYNC_CHANGE_SNS) {
5223                         msg = "Chan %d Name Server Database Changed";
5224                 } else {
5225                         msg = "Chan %d Other Change Notify";
5226                 }
5227
5228                 /*
5229                  * If the loop down timer is running, cancel it.
5230                  */
5231                 if (fc->ready && callout_active(&fc->ldt)) {
5232                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5233                         callout_stop(&fc->ldt);
5234                 }
5235                 isp_prt(isp, ISP_LOGINFO, msg, bus);
5236                 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5237                         isp_freeze_loopdown(isp, bus, msg);
5238                 }
5239                 wakeup(fc);
5240                 break;
5241         }
5242 #ifdef  ISP_TARGET_MODE
5243         case ISPASYNC_TARGET_NOTIFY:
5244         {
5245                 isp_notify_t *notify;
5246                 va_start(ap, cmd);
5247                 notify = va_arg(ap, isp_notify_t *);
5248                 va_end(ap);
5249                 switch (notify->nt_ncode) {
5250                 case NT_ABORT_TASK:
5251                 case NT_ABORT_TASK_SET:
5252                 case NT_CLEAR_ACA:
5253                 case NT_CLEAR_TASK_SET:
5254                 case NT_LUN_RESET:
5255                 case NT_TARGET_RESET:
5256                         /*
5257                          * These are task management functions.
5258                          */
5259                         isp_handle_platform_target_tmf(isp, notify);
5260                         break;
5261                 case NT_BUS_RESET:
5262                 case NT_LIP_RESET:
5263                 case NT_LINK_UP:
5264                 case NT_LINK_DOWN:
5265                         /*
5266                          * No action need be taken here.
5267                          */
5268                         break;
5269                 case NT_HBA_RESET:
5270                         isp_del_all_wwn_entries(isp, ISP_NOCHAN);
5271                         break;
5272                 case NT_LOGOUT:
5273                         /*
5274                          * This is device arrival/departure notification
5275                          */
5276                         isp_handle_platform_target_notify_ack(isp, notify);
5277                         break;
5278                 case NT_ARRIVED:
5279                 {
5280                         struct ac_contract ac;
5281                         struct ac_device_changed *fc;
5282
5283                         ac.contract_number = AC_CONTRACT_DEV_CHG;
5284                         fc = (struct ac_device_changed *) ac.contract_data;
5285                         fc->wwpn = notify->nt_wwn;
5286                         fc->port = notify->nt_sid;
5287                         fc->target = notify->nt_nphdl;
5288                         fc->arrived = 1;
5289                         xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5290                         break;
5291                 }
5292                 case NT_DEPARTED:
5293                 {
5294                         struct ac_contract ac;
5295                         struct ac_device_changed *fc;
5296
5297                         ac.contract_number = AC_CONTRACT_DEV_CHG;
5298                         fc = (struct ac_device_changed *) ac.contract_data;
5299                         fc->wwpn = notify->nt_wwn;
5300                         fc->port = notify->nt_sid;
5301                         fc->target = notify->nt_nphdl;
5302                         fc->arrived = 0;
5303                         xpt_async(AC_CONTRACT, ISP_FC_PC(isp, notify->nt_channel)->path, &ac);
5304                         break;
5305                 }
5306                 default:
5307                         isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5308                         isp_handle_platform_target_notify_ack(isp, notify);
5309                         break;
5310                 }
5311                 break;
5312         }
5313         case ISPASYNC_TARGET_ACTION:
5314         {
5315                 isphdr_t *hp;
5316
5317                 va_start(ap, cmd);
5318                 hp = va_arg(ap, isphdr_t *);
5319                 va_end(ap);
5320                 switch (hp->rqs_entry_type) {
5321                 default:
5322                         isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5323                         break;
5324                 case RQSTYPE_NOTIFY:
5325                         if (IS_SCSI(isp)) {
5326                                 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5327                         } else if (IS_24XX(isp)) {
5328                                 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5329                         } else {
5330                                 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5331                         }
5332                         break;
5333                 case RQSTYPE_ATIO:
5334                         if (IS_24XX(isp)) {
5335                                 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5336                         } else {
5337                                 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5338                         }
5339                         break;
5340                 case RQSTYPE_ATIO2:
5341                         isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5342                         break;
5343                 case RQSTYPE_CTIO7:
5344                 case RQSTYPE_CTIO3:
5345                 case RQSTYPE_CTIO2:
5346                 case RQSTYPE_CTIO:
5347                         isp_handle_platform_ctio(isp, hp);
5348                         break;
5349                 case RQSTYPE_ABTS_RCVD:
5350                 {
5351                         abts_t *abts = (abts_t *)hp;
5352                         isp_notify_t notify, *nt = &notify;
5353                         tstate_t *tptr;
5354                         fcportdb_t *lp;
5355                         uint16_t chan;
5356                         uint32_t sid, did;
5357
5358                         did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
5359                         sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
5360                         ISP_MEMZERO(nt, sizeof (isp_notify_t));
5361
5362                         nt->nt_hba = isp;
5363                         nt->nt_did = did;
5364                         nt->nt_nphdl = abts->abts_nphdl;
5365                         nt->nt_sid = sid;
5366                         isp_find_chan_by_did(isp, did, &chan);
5367                         if (chan == ISP_NOCHAN) {
5368                                 nt->nt_tgt = TGT_ANY;
5369                         } else {
5370                                 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
5371                                 if (isp_find_pdb_by_loopid(isp, chan, abts->abts_nphdl, &lp)) {
5372                                         nt->nt_wwn = lp->port_wwn;
5373                                 } else {
5374                                         nt->nt_wwn = INI_ANY;
5375                                 }
5376                         }
5377                         /*
5378                          * Try hard to find the lun for this command.
5379                          */
5380                         tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
5381                         if (tptr) {
5382                                 nt->nt_lun = xpt_path_lun_id(tptr->owner);
5383                                 rls_lun_statep(isp, tptr);
5384                         } else {
5385                                 nt->nt_lun = LUN_ANY;
5386                         }
5387                         nt->nt_need_ack = 1;
5388                         nt->nt_tagval = abts->abts_rxid_task;
5389                         nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
5390                         if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
5391                                 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)",
5392                                     abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
5393                         } else {
5394                                 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)",
5395                                     abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
5396                         }
5397                         nt->nt_channel = chan;
5398                         nt->nt_ncode = NT_ABORT_TASK;
5399                         nt->nt_lreserved = hp;
5400                         isp_handle_platform_target_tmf(isp, nt);
5401                         break;
5402                 }
5403                 case RQSTYPE_ENABLE_LUN:
5404                 case RQSTYPE_MODIFY_LUN:
5405                         isp_ledone(isp, (lun_entry_t *) hp);
5406                         break;
5407                 }
5408                 break;
5409         }
5410 #endif
5411         case ISPASYNC_FW_CRASH:
5412         {
5413                 uint16_t mbox1, mbox6;
5414                 mbox1 = ISP_READ(isp, OUTMAILBOX1);
5415                 if (IS_DUALBUS(isp)) { 
5416                         mbox6 = ISP_READ(isp, OUTMAILBOX6);
5417                 } else {
5418                         mbox6 = 0;
5419                 }
5420                 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
5421                 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
5422                 isp->isp_osinfo.mbox_sleep_ok = 0;
5423                 isp_reinit(isp, 1);
5424                 isp->isp_osinfo.mbox_sleep_ok = mbox1;
5425                 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
5426                 break;
5427         }
5428         default:
5429                 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
5430                 break;
5431         }
5432 }
5433
5434
5435 /*
5436  * Locks are held before coming here.
5437  */
5438 void
5439 isp_uninit(ispsoftc_t *isp)
5440 {
5441         if (IS_24XX(isp)) {
5442                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
5443         } else {
5444                 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
5445         }
5446         ISP_DISABLE_INTS(isp);
5447 }
5448
5449 /*
5450  * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
5451  * up from our platform default (defww{p|n}n) and morph them based upon
5452  * channel.
5453  * 
5454  * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
5455  * based upon channel.
5456  */
5457
5458 uint64_t
5459 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
5460 {
5461         uint64_t seed;
5462         struct isp_fc *fc = ISP_FC_PC(isp, chan);
5463
5464         /*
5465          * If we're asking for a active WWN, the default overrides get
5466          * returned, otherwise the NVRAM value is picked.
5467          * 
5468          * If we're asking for a default WWN, we just pick the default override.
5469          */
5470         if (isactive) {
5471                 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5472                 if (seed) {
5473                         return (seed);
5474                 }
5475                 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
5476                 if (seed) {
5477                         return (seed);
5478                 }
5479                 return (0x400000007F000009ull);
5480         } else {
5481                 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
5482         }
5483
5484
5485         /*
5486          * For channel zero just return what we have. For either ACTIVE or
5487          * DEFAULT cases, we depend on default override of NVRAM values for
5488          * channel zero.
5489          */
5490         if (chan == 0) {
5491                 return (seed);
5492         }
5493
5494         /*
5495          * For other channels, we are doing one of three things:
5496          * 
5497          * 1. If what we have now is non-zero, return it. Otherwise we morph
5498          * values from channel 0. 2. If we're here for a WWPN we synthesize
5499          * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
5500          * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
5501          */
5502
5503         if (seed) {
5504                 return (seed);
5505         }
5506         if (isactive) {
5507                 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
5508         } else {
5509                 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
5510         }
5511
5512         if (((seed >> 60) & 0xf) == 2) {
5513                 /*
5514                  * The type 2 NAA fields for QLogic cards appear be laid out
5515                  * thusly:
5516                  * 
5517                  * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
5518                  * port (1) or node (0) WWN distinguishor bit 48
5519                  * physical port on dual-port chips (23XX/24XX)
5520                  * 
5521                  * This is somewhat nutty, particularly since bit 48 is
5522                  * irrelevant as they assign separate serial numbers to
5523                  * different physical ports anyway.
5524                  * 
5525                  * We'll stick our channel number plus one first into bits
5526                  * 57..59 and thence into bits 52..55 which allows for 8 bits
5527                  * of channel which is comfortably more than our maximum
5528                  * (126) now.
5529                  */
5530                 seed &= ~0x0FF0000000000000ULL;
5531                 if (iswwnn == 0) {
5532                         seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
5533                         seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
5534                 }
5535         } else {
5536                 seed = 0;
5537         }
5538         return (seed);
5539 }
5540
5541 void
5542 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
5543 {
5544         int loc;
5545         char lbuf[128];
5546         va_list ap;
5547
5548         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5549                 return;
5550         }
5551         sprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
5552         loc = strlen(lbuf);
5553         va_start(ap, fmt);
5554         vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap); 
5555         va_end(ap);
5556         printf("%s\n", lbuf);
5557 }
5558
5559 void
5560 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
5561 {
5562         va_list ap;
5563         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
5564                 return;
5565         }
5566         xpt_print_path(xs->ccb_h.path);
5567         va_start(ap, fmt);
5568         vprintf(fmt, ap);
5569         va_end(ap);
5570         printf("\n");
5571 }
5572
5573 uint64_t
5574 isp_nanotime_sub(struct timespec *b, struct timespec *a)
5575 {
5576         uint64_t elapsed;
5577         struct timespec x = *b;
5578         timespecsub(&x, a);
5579         elapsed = GET_NANOSEC(&x);
5580         if (elapsed == 0)
5581                 elapsed++;
5582         return (elapsed);
5583 }
5584
5585 int
5586 isp_mbox_acquire(ispsoftc_t *isp)
5587 {
5588         if (isp->isp_osinfo.mboxbsy) {
5589                 return (1);
5590         } else {
5591                 isp->isp_osinfo.mboxcmd_done = 0;
5592                 isp->isp_osinfo.mboxbsy = 1;
5593                 return (0);
5594         }
5595 }
5596
5597 void
5598 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
5599 {
5600         unsigned int usecs = mbp->timeout;
5601         unsigned int max, olim, ilim;
5602
5603         if (usecs == 0) {
5604                 usecs = MBCMD_DEFAULT_TIMEOUT;
5605         }
5606         max = isp->isp_mbxwrk0 + 1;
5607
5608         if (isp->isp_osinfo.mbox_sleep_ok) {
5609                 unsigned int ms = (usecs + 999) / 1000;
5610
5611                 isp->isp_osinfo.mbox_sleep_ok = 0;
5612                 isp->isp_osinfo.mbox_sleeping = 1;
5613                 for (olim = 0; olim < max; olim++) {
5614                         msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
5615                         if (isp->isp_osinfo.mboxcmd_done) {
5616                                 break;
5617                         }
5618                 }
5619                 isp->isp_osinfo.mbox_sleep_ok = 1;
5620                 isp->isp_osinfo.mbox_sleeping = 0;
5621         } else {
5622                 for (olim = 0; olim < max; olim++) {
5623                         for (ilim = 0; ilim < usecs; ilim += 100) {
5624                                 uint32_t isr;
5625                                 uint16_t sema, mbox;
5626                                 if (isp->isp_osinfo.mboxcmd_done) {
5627                                         break;
5628                                 }
5629                                 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
5630                                         isp_intr(isp, isr, sema, mbox);
5631                                         if (isp->isp_osinfo.mboxcmd_done) {
5632                                                 break;
5633                                         }
5634                                 }
5635                                 ISP_DELAY(100);
5636                         }
5637                         if (isp->isp_osinfo.mboxcmd_done) {
5638                                 break;
5639                         }
5640                 }
5641         }
5642         if (isp->isp_osinfo.mboxcmd_done == 0) {
5643                 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
5644                     isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
5645                 mbp->param[0] = MBOX_TIMEOUT;
5646                 isp->isp_osinfo.mboxcmd_done = 1;
5647         }
5648 }
5649
5650 void
5651 isp_mbox_notify_done(ispsoftc_t *isp)
5652 {
5653         if (isp->isp_osinfo.mbox_sleeping) {
5654                 wakeup(&isp->isp_mbxworkp);
5655         }
5656         isp->isp_osinfo.mboxcmd_done = 1;
5657 }
5658
5659 void
5660 isp_mbox_release(ispsoftc_t *isp)
5661 {
5662         isp->isp_osinfo.mboxbsy = 0;
5663 }
5664
5665 int
5666 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
5667 {
5668         int ret = 0;
5669         if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
5670                 ret = -1;
5671         } else {
5672                 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
5673         }
5674         return (ret);
5675 }
5676
5677 int
5678 isp_mstohz(int ms)
5679 {
5680         int hz;
5681         struct timeval t;
5682         t.tv_sec = ms / 1000;
5683         t.tv_usec = (ms % 1000) * 1000;
5684         hz = tvtohz(&t);
5685         if (hz < 0) {
5686                 hz = 0x7fffffff;
5687         }
5688         if (hz == 0) {
5689                 hz = 1;
5690         }
5691         return (hz);
5692 }
5693
5694 void
5695 isp_platform_intr(void *arg)
5696 {
5697         ispsoftc_t *isp = arg;
5698         uint32_t isr;
5699         uint16_t sema, mbox;
5700
5701         ISP_LOCK(isp);
5702         isp->isp_intcnt++;
5703         if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
5704                 isp->isp_intbogus++;
5705         } else {
5706                 isp_intr(isp, isr, sema, mbox);
5707         }
5708         ISP_UNLOCK(isp);
5709 }
5710
5711 void
5712 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
5713 {
5714         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
5715                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
5716         } else {
5717                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
5718         }
5719         bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
5720 }
5721
5722 void
5723 isp_timer(void *arg)
5724 {
5725         ispsoftc_t *isp = arg;
5726 #ifdef  ISP_TARGET_MODE
5727         isp_tmcmd_restart(isp);
5728 #endif
5729         callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
5730 }