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