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