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