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