]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/isp/isp_freebsd.c
MFC r315587, r315652: Remove some dead/useless code.
[FreeBSD/FreeBSD.git] / sys / dev / isp / isp_freebsd.c
1 /*-
2  * Copyright (c) 1997-2009 by Matthew Jacob
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <dev/isp/isp_freebsd.h>
34 #include <sys/unistd.h>
35 #include <sys/kthread.h>
36 #include <sys/conf.h>
37 #include <sys/module.h>
38 #include <sys/ioccom.h>
39 #include <dev/isp/isp_ioctl.h>
40 #include <sys/devicestat.h>
41 #include <cam/cam_periph.h>
42 #include <cam/cam_xpt_periph.h>
43
44 MODULE_VERSION(isp, 1);
45 MODULE_DEPEND(isp, cam, 1, 1, 1);
46 int isp_announced = 0;
47 int isp_loop_down_limit = 60;   /* default loop down limit */
48 int isp_quickboot_time = 7;     /* don't wait more than N secs for loop up */
49 int isp_gone_device_time = 30;  /* grace time before reporting device lost */
50 static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
51
52 static void isp_freeze_loopdown(ispsoftc_t *, int);
53 static void isp_loop_changed(ispsoftc_t *isp, int chan);
54 static d_ioctl_t ispioctl;
55 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
56 static void isp_poll(struct cam_sim *);
57 static timeout_t isp_watchdog;
58 static timeout_t isp_gdt;
59 static task_fn_t isp_gdt_task;
60 static void isp_kthread(void *);
61 static void isp_action(struct cam_sim *, union ccb *);
62 static int isp_timer_count;
63 static void isp_timer(void *);
64
65 static struct cdevsw isp_cdevsw = {
66         .d_version =    D_VERSION,
67         .d_ioctl =      ispioctl,
68         .d_name =       "isp",
69 };
70
71 static int
72 isp_role_sysctl(SYSCTL_HANDLER_ARGS)
73 {
74         ispsoftc_t *isp = (ispsoftc_t *)arg1;
75         int chan = arg2;
76         int error, old, value;
77
78         value = FCPARAM(isp, chan)->role;
79
80         error = sysctl_handle_int(oidp, &value, 0, req);
81         if ((error != 0) || (req->newptr == NULL))
82                 return (error);
83
84         if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
85                 return (EINVAL);
86
87         ISP_LOCK(isp);
88         old = FCPARAM(isp, chan)->role;
89
90         /* We don't allow target mode switch from here. */
91         value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
92
93         /* If nothing has changed -- we are done. */
94         if (value == old) {
95                 ISP_UNLOCK(isp);
96                 return (0);
97         }
98
99         /* Actually change the role. */
100         error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
101         ISP_UNLOCK(isp);
102         return (error);
103 }
104
105 static int
106 isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
107 {
108         struct ccb_setasync csa;
109         struct cam_sim *sim;
110         struct cam_path *path;
111 #ifdef  ISP_TARGET_MODE
112         int i;
113 #endif
114
115         /*
116          * Construct our SIM entry.
117          */
118         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);
119
120         if (sim == NULL) {
121                 return (ENOMEM);
122         }
123
124         ISP_LOCK(isp);
125         if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
126                 ISP_UNLOCK(isp);
127                 cam_sim_free(sim, FALSE);
128                 return (EIO);
129         }
130         ISP_UNLOCK(isp);
131         if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
132                 ISP_LOCK(isp);
133                 xpt_bus_deregister(cam_sim_path(sim));
134                 ISP_UNLOCK(isp);
135                 cam_sim_free(sim, FALSE);
136                 return (ENXIO);
137         }
138         xpt_setup_ccb(&csa.ccb_h, path, 5);
139         csa.ccb_h.func_code = XPT_SASYNC_CB;
140         csa.event_enable = AC_LOST_DEVICE;
141         csa.callback = isp_cam_async;
142         csa.callback_arg = sim;
143
144         ISP_LOCK(isp);
145         xpt_action((union ccb *)&csa);
146         ISP_UNLOCK(isp);
147
148         if (IS_SCSI(isp)) {
149                 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
150                 spi->sim = sim;
151                 spi->path = path;
152 #ifdef  ISP_TARGET_MODE
153                 TAILQ_INIT(&spi->waitq);
154                 STAILQ_INIT(&spi->ntfree);
155                 for (i = 0; i < ATPDPSIZE; i++)
156                         STAILQ_INSERT_TAIL(&spi->ntfree, &spi->ntpool[i], next);
157                 LIST_INIT(&spi->atfree);
158                 for (i = ATPDPSIZE-1; i >= 0; i--)
159                         LIST_INSERT_HEAD(&spi->atfree, &spi->atpool[i], next);
160                 for (i = 0; i < ATPDPHASHSIZE; i++)
161                         LIST_INIT(&spi->atused[i]);
162 #endif
163         } else {
164                 fcparam *fcp = FCPARAM(isp, chan);
165                 struct isp_fc *fc = ISP_FC_PC(isp, chan);
166                 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
167                 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
168                 char name[16];
169
170                 ISP_LOCK(isp);
171                 fc->sim = sim;
172                 fc->path = path;
173                 fc->isp = isp;
174                 fc->ready = 1;
175
176                 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
177                 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
178 #ifdef  ISP_TARGET_MODE
179                 TAILQ_INIT(&fc->waitq);
180                 STAILQ_INIT(&fc->ntfree);
181                 for (i = 0; i < ATPDPSIZE; i++)
182                         STAILQ_INSERT_TAIL(&fc->ntfree, &fc->ntpool[i], next);
183                 LIST_INIT(&fc->atfree);
184                 for (i = ATPDPSIZE-1; i >= 0; i--)
185                         LIST_INSERT_HEAD(&fc->atfree, &fc->atpool[i], next);
186                 for (i = 0; i < ATPDPHASHSIZE; i++)
187                         LIST_INIT(&fc->atused[i]);
188 #endif
189                 isp_loop_changed(isp, chan);
190                 ISP_UNLOCK(isp);
191                 if (kproc_create(isp_kthread, fc, &fc->kproc, 0, 0,
192                     "%s_%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
193                         xpt_free_path(fc->path);
194                         ISP_LOCK(isp);
195                         xpt_bus_deregister(cam_sim_path(fc->sim));
196                         ISP_UNLOCK(isp);
197                         cam_sim_free(fc->sim, FALSE);
198                         return (ENOMEM);
199                 }
200                 fc->num_threads += 1;
201                 if (chan > 0) {
202                         snprintf(name, sizeof(name), "chan%d", chan);
203                         tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
204                             OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
205                 }
206                 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
207                     "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
208                     "World Wide Node Name");
209                 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
210                     "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
211                     "World Wide Port Name");
212                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213                     "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
214                     "Loop Down Limit");
215                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
216                     "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
217                     "Gone Device Time");
218 #if defined(ISP_TARGET_MODE) && defined(DEBUG)
219                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
220                     "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
221                     "Cause a Lost Frame on a Read");
222 #endif
223                 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
224                     "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
225                     isp_role_sysctl, "I", "Current role");
226                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
227                     "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
228                     "Connection speed in gigabits");
229                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
230                     "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
231                     "Link state");
232                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
233                     "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
234                     "Firmware state");
235                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236                     "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
237                     "Loop state");
238                 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
239                     "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
240                     "Connection topology");
241         }
242         return (0);
243 }
244
245 static void
246 isp_detach_chan(ispsoftc_t *isp, int chan)
247 {
248         struct cam_sim *sim;
249         struct cam_path *path;
250         struct ccb_setasync csa;
251         int *num_threads;
252
253         ISP_GET_PC(isp, chan, sim, sim);
254         ISP_GET_PC(isp, chan, path, path);
255         ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
256
257         xpt_setup_ccb(&csa.ccb_h, path, 5);
258         csa.ccb_h.func_code = XPT_SASYNC_CB;
259         csa.event_enable = 0;
260         csa.callback = isp_cam_async;
261         csa.callback_arg = sim;
262         xpt_action((union ccb *)&csa);
263         xpt_free_path(path);
264         xpt_bus_deregister(cam_sim_path(sim));
265         cam_sim_free(sim, FALSE);
266
267         /* Wait for the channel's spawned threads to exit. */
268         wakeup(isp->isp_osinfo.pc.ptr);
269         while (*num_threads != 0)
270                 mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
271 }
272
273 int
274 isp_attach(ispsoftc_t *isp)
275 {
276         const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
277         int du = device_get_unit(isp->isp_dev);
278         int chan;
279
280         /*
281          * Create the device queue for our SIM(s).
282          */
283         isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
284         if (isp->isp_osinfo.devq == NULL) {
285                 return (EIO);
286         }
287
288         for (chan = 0; chan < isp->isp_nchan; chan++) {
289                 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
290                         goto unwind;
291                 }
292         }
293
294         callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
295         isp_timer_count = hz >> 2;
296         callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
297
298         isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
299         if (isp->isp_osinfo.cdev) {
300                 isp->isp_osinfo.cdev->si_drv1 = isp;
301         }
302         return (0);
303
304 unwind:
305         while (--chan >= 0) {
306                 struct cam_sim *sim;
307                 struct cam_path *path;
308
309                 ISP_GET_PC(isp, chan, sim, sim);
310                 ISP_GET_PC(isp, chan, path, path);
311                 xpt_free_path(path);
312                 ISP_LOCK(isp);
313                 xpt_bus_deregister(cam_sim_path(sim));
314                 ISP_UNLOCK(isp);
315                 cam_sim_free(sim, FALSE);
316         }
317         cam_simq_free(isp->isp_osinfo.devq);
318         isp->isp_osinfo.devq = NULL;
319         return (-1);
320 }
321
322 int
323 isp_detach(ispsoftc_t *isp)
324 {
325         int chan;
326
327         if (isp->isp_osinfo.cdev) {
328                 destroy_dev(isp->isp_osinfo.cdev);
329                 isp->isp_osinfo.cdev = NULL;
330         }
331         ISP_LOCK(isp);
332         /* Tell spawned threads that we're exiting. */
333         isp->isp_osinfo.is_exiting = 1;
334         for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
335                 isp_detach_chan(isp, chan);
336         ISP_UNLOCK(isp);
337         callout_drain(&isp->isp_osinfo.tmo);
338         cam_simq_free(isp->isp_osinfo.devq);
339         return (0);
340 }
341
342 static void
343 isp_freeze_loopdown(ispsoftc_t *isp, int chan)
344 {
345         struct isp_fc *fc = ISP_FC_PC(isp, chan);
346
347         if (fc->sim == NULL)
348                 return;
349         if (fc->simqfrozen == 0) {
350                 isp_prt(isp, ISP_LOGDEBUG0,
351                     "Chan %d Freeze simq (loopdown)", chan);
352                 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
353                 xpt_hold_boot();
354                 xpt_freeze_simq(fc->sim, 1);
355         } else {
356                 isp_prt(isp, ISP_LOGDEBUG0,
357                     "Chan %d Mark simq frozen (loopdown)", chan);
358                 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
359         }
360 }
361
362 static void
363 isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
364 {
365         struct isp_fc *fc = ISP_FC_PC(isp, chan);
366
367         if (fc->sim == NULL)
368                 return;
369         int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
370         fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
371         if (wasfrozen && fc->simqfrozen == 0) {
372                 isp_prt(isp, ISP_LOGDEBUG0,
373                     "Chan %d Release simq", chan);
374                 xpt_release_simq(fc->sim, 1);
375                 xpt_release_boot();
376         }
377 }
378
379 static int
380 ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
381 {
382         ispsoftc_t *isp;
383         int nr, chan, retval = ENOTTY;
384
385         isp = dev->si_drv1;
386
387         switch (c) {
388         case ISP_SDBLEV:
389         {
390                 int olddblev = isp->isp_dblev;
391                 isp->isp_dblev = *(int *)addr;
392                 *(int *)addr = olddblev;
393                 retval = 0;
394                 break;
395         }
396         case ISP_GETROLE:
397                 chan = *(int *)addr;
398                 if (chan < 0 || chan >= isp->isp_nchan) {
399                         retval = -ENXIO;
400                         break;
401                 }
402                 if (IS_FC(isp)) {
403                         *(int *)addr = FCPARAM(isp, chan)->role;
404                 } else {
405                         *(int *)addr = ISP_ROLE_INITIATOR;
406                 }
407                 retval = 0;
408                 break;
409         case ISP_SETROLE:
410                 if (IS_SCSI(isp))
411                         break;
412                 nr = *(int *)addr;
413                 chan = nr >> 8;
414                 if (chan < 0 || chan >= isp->isp_nchan) {
415                         retval = -ENXIO;
416                         break;
417                 }
418                 nr &= 0xff;
419                 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
420                         retval = EINVAL;
421                         break;
422                 }
423                 ISP_LOCK(isp);
424                 *(int *)addr = FCPARAM(isp, chan)->role;
425                 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
426                 ISP_UNLOCK(isp);
427                 retval = 0;
428                 break;
429
430         case ISP_RESETHBA:
431                 ISP_LOCK(isp);
432                 isp_reinit(isp, 0);
433                 ISP_UNLOCK(isp);
434                 retval = 0;
435                 break;
436
437         case ISP_RESCAN:
438                 if (IS_FC(isp)) {
439                         chan = *(int *)addr;
440                         if (chan < 0 || chan >= isp->isp_nchan) {
441                                 retval = -ENXIO;
442                                 break;
443                         }
444                         ISP_LOCK(isp);
445                         if (isp_fc_runstate(isp, chan, 5 * 1000000) != LOOP_READY) {
446                                 retval = EIO;
447                         } else {
448                                 retval = 0;
449                         }
450                         ISP_UNLOCK(isp);
451                 }
452                 break;
453
454         case ISP_FC_LIP:
455                 if (IS_FC(isp)) {
456                         chan = *(int *)addr;
457                         if (chan < 0 || chan >= isp->isp_nchan) {
458                                 retval = -ENXIO;
459                                 break;
460                         }
461                         ISP_LOCK(isp);
462                         if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
463                                 retval = EIO;
464                         } else {
465                                 retval = 0;
466                         }
467                         ISP_UNLOCK(isp);
468                 }
469                 break;
470         case ISP_FC_GETDINFO:
471         {
472                 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
473                 fcportdb_t *lp;
474
475                 if (IS_SCSI(isp)) {
476                         break;
477                 }
478                 if (ifc->loopid >= MAX_FC_TARG) {
479                         retval = EINVAL;
480                         break;
481                 }
482                 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
483                 if (lp->state != FC_PORTDB_STATE_NIL) {
484                         ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
485                         ifc->loopid = lp->handle;
486                         ifc->portid = lp->portid;
487                         ifc->node_wwn = lp->node_wwn;
488                         ifc->port_wwn = lp->port_wwn;
489                         retval = 0;
490                 } else {
491                         retval = ENODEV;
492                 }
493                 break;
494         }
495         case ISP_FC_GETHINFO:
496         {
497                 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
498                 int chan = hba->fc_channel;
499
500                 if (chan < 0 || chan >= isp->isp_nchan) {
501                         retval = ENXIO;
502                         break;
503                 }
504                 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
505                 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
506                 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
507                 hba->fc_nchannels = isp->isp_nchan;
508                 if (IS_FC(isp)) {
509                         hba->fc_nports = MAX_FC_TARG;
510                         hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
511                         hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
512                         hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
513                         hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
514                         hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
515                         hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
516                         hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
517                 } else {
518                         hba->fc_nports = MAX_TARGETS;
519                         hba->fc_speed = 0;
520                         hba->fc_topology = 0;
521                         hba->nvram_node_wwn = 0ull;
522                         hba->nvram_port_wwn = 0ull;
523                         hba->active_node_wwn = 0ull;
524                         hba->active_port_wwn = 0ull;
525                 }
526                 retval = 0;
527                 break;
528         }
529         case ISP_TSK_MGMT:
530         {
531                 int needmarker;
532                 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
533                 uint16_t nphdl;
534                 mbreg_t mbs;
535
536                 if (IS_SCSI(isp)) {
537                         break;
538                 }
539
540                 chan = fct->chan;
541                 if (chan < 0 || chan >= isp->isp_nchan) {
542                         retval = -ENXIO;
543                         break;
544                 }
545
546                 needmarker = retval = 0;
547                 nphdl = fct->loopid;
548                 ISP_LOCK(isp);
549                 if (IS_24XX(isp)) {
550                         void *reqp;
551                         uint8_t resp[QENTRY_LEN];
552                         isp24xx_tmf_t tmf;
553                         isp24xx_statusreq_t sp;
554                         fcparam *fcp = FCPARAM(isp, chan);
555                         fcportdb_t *lp;
556                         int i;
557
558                         for (i = 0; i < MAX_FC_TARG; i++) {
559                                 lp = &fcp->portdb[i];
560                                 if (lp->handle == nphdl) {
561                                         break;
562                                 }
563                         }
564                         if (i == MAX_FC_TARG) {
565                                 retval = ENXIO;
566                                 ISP_UNLOCK(isp);
567                                 break;
568                         }
569                         ISP_MEMZERO(&tmf, sizeof(tmf));
570                         tmf.tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
571                         tmf.tmf_header.rqs_entry_count = 1;
572                         tmf.tmf_nphdl = lp->handle;
573                         tmf.tmf_delay = 2;
574                         tmf.tmf_timeout = 4;
575                         tmf.tmf_tidlo = lp->portid;
576                         tmf.tmf_tidhi = lp->portid >> 16;
577                         tmf.tmf_vpidx = ISP_GET_VPIDX(isp, chan);
578                         tmf.tmf_lun[1] = fct->lun & 0xff;
579                         if (fct->lun >= 256) {
580                                 tmf.tmf_lun[0] = 0x40 | (fct->lun >> 8);
581                         }
582                         switch (fct->action) {
583                         case IPT_CLEAR_ACA:
584                                 tmf.tmf_flags = ISP24XX_TMF_CLEAR_ACA;
585                                 break;
586                         case IPT_TARGET_RESET:
587                                 tmf.tmf_flags = ISP24XX_TMF_TARGET_RESET;
588                                 needmarker = 1;
589                                 break;
590                         case IPT_LUN_RESET:
591                                 tmf.tmf_flags = ISP24XX_TMF_LUN_RESET;
592                                 needmarker = 1;
593                                 break;
594                         case IPT_CLEAR_TASK_SET:
595                                 tmf.tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
596                                 needmarker = 1;
597                                 break;
598                         case IPT_ABORT_TASK_SET:
599                                 tmf.tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
600                                 needmarker = 1;
601                                 break;
602                         default:
603                                 retval = EINVAL;
604                                 break;
605                         }
606                         if (retval) {
607                                 ISP_UNLOCK(isp);
608                                 break;
609                         }
610
611                         /* Prepare space for response in memory */
612                         memset(resp, 0xff, sizeof(resp));
613                         tmf.tmf_handle = isp_allocate_handle(isp, resp,
614                             ISP_HANDLE_CTRL);
615                         if (tmf.tmf_handle == 0) {
616                                 isp_prt(isp, ISP_LOGERR,
617                                     "%s: TMF of Chan %d out of handles",
618                                     __func__, chan);
619                                 ISP_UNLOCK(isp);
620                                 retval = ENOMEM;
621                                 break;
622                         }
623
624                         /* Send request and wait for response. */
625                         reqp = isp_getrqentry(isp);
626                         if (reqp == NULL) {
627                                 isp_prt(isp, ISP_LOGERR,
628                                     "%s: TMF of Chan %d out of rqent",
629                                     __func__, chan);
630                                 isp_destroy_handle(isp, tmf.tmf_handle);
631                                 ISP_UNLOCK(isp);
632                                 retval = EIO;
633                                 break;
634                         }
635                         isp_put_24xx_tmf(isp, &tmf, (isp24xx_tmf_t *)reqp);
636                         if (isp->isp_dblev & ISP_LOGDEBUG1)
637                                 isp_print_bytes(isp, "IOCB TMF", QENTRY_LEN, reqp);
638                         ISP_SYNC_REQUEST(isp);
639                         if (msleep(resp, &isp->isp_lock, 0, "TMF", 5*hz) == EWOULDBLOCK) {
640                                 isp_prt(isp, ISP_LOGERR,
641                                     "%s: TMF of Chan %d timed out",
642                                     __func__, chan);
643                                 isp_destroy_handle(isp, tmf.tmf_handle);
644                                 ISP_UNLOCK(isp);
645                                 retval = EIO;
646                                 break;
647                         }
648                         if (isp->isp_dblev & ISP_LOGDEBUG1)
649                                 isp_print_bytes(isp, "IOCB TMF response", QENTRY_LEN, resp);
650                         isp_get_24xx_response(isp, (isp24xx_statusreq_t *)resp, &sp);
651
652                         if (sp.req_completion_status != 0)
653                                 retval = EIO;
654                         else if (needmarker)
655                                 fcp->sendmarker = 1;
656                 } else {
657                         MBSINIT(&mbs, 0, MBLOGALL, 0);
658                         if (ISP_CAP_2KLOGIN(isp) == 0) {
659                                 nphdl <<= 8;
660                         }
661                         switch (fct->action) {
662                         case IPT_CLEAR_ACA:
663                                 mbs.param[0] = MBOX_CLEAR_ACA;
664                                 mbs.param[1] = nphdl;
665                                 mbs.param[2] = fct->lun;
666                                 break;
667                         case IPT_TARGET_RESET:
668                                 mbs.param[0] = MBOX_TARGET_RESET;
669                                 mbs.param[1] = nphdl;
670                                 needmarker = 1;
671                                 break;
672                         case IPT_LUN_RESET:
673                                 mbs.param[0] = MBOX_LUN_RESET;
674                                 mbs.param[1] = nphdl;
675                                 mbs.param[2] = fct->lun;
676                                 needmarker = 1;
677                                 break;
678                         case IPT_CLEAR_TASK_SET:
679                                 mbs.param[0] = MBOX_CLEAR_TASK_SET;
680                                 mbs.param[1] = nphdl;
681                                 mbs.param[2] = fct->lun;
682                                 needmarker = 1;
683                                 break;
684                         case IPT_ABORT_TASK_SET:
685                                 mbs.param[0] = MBOX_ABORT_TASK_SET;
686                                 mbs.param[1] = nphdl;
687                                 mbs.param[2] = fct->lun;
688                                 needmarker = 1;
689                                 break;
690                         default:
691                                 retval = EINVAL;
692                                 break;
693                         }
694                         if (retval == 0) {
695                                 if (needmarker) {
696                                         FCPARAM(isp, chan)->sendmarker = 1;
697                                 }
698                                 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
699                                 if (retval) {
700                                         retval = EIO;
701                                 }
702                         }
703                 }
704                 ISP_UNLOCK(isp);
705                 break;
706         }
707         default:
708                 break;
709         }
710         return (retval);
711 }
712
713 /*
714  * Local Inlines
715  */
716
717 static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
718 static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
719
720 static ISP_INLINE int
721 isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
722 {
723         ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
724         if (ISP_PCMD(ccb) == NULL) {
725                 return (-1);
726         }
727         isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
728         return (0);
729 }
730
731 static ISP_INLINE void
732 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
733 {
734         if (ISP_PCMD(ccb)) {
735 #ifdef  ISP_TARGET_MODE
736                 PISP_PCMD(ccb)->datalen = 0;
737                 PISP_PCMD(ccb)->totslen = 0;
738                 PISP_PCMD(ccb)->cumslen = 0;
739                 PISP_PCMD(ccb)->crn = 0;
740 #endif
741                 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
742                 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
743                 ISP_PCMD(ccb) = NULL;
744         }
745 }
746
747 /*
748  * Put the target mode functions here, because some are inlines
749  */
750 #ifdef  ISP_TARGET_MODE
751 static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
752 static atio_private_data_t *isp_get_atpd(ispsoftc_t *, int, uint32_t);
753 static atio_private_data_t *isp_find_atpd(ispsoftc_t *, int, uint32_t);
754 static void isp_put_atpd(ispsoftc_t *, int, atio_private_data_t *);
755 static inot_private_data_t *isp_get_ntpd(ispsoftc_t *, int);
756 static inot_private_data_t *isp_find_ntpd(ispsoftc_t *, int, uint32_t, uint32_t);
757 static void isp_put_ntpd(ispsoftc_t *, int, inot_private_data_t *);
758 static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
759 static void destroy_lun_state(ispsoftc_t *, int, tstate_t *);
760 static void isp_enable_lun(ispsoftc_t *, union ccb *);
761 static void isp_disable_lun(ispsoftc_t *, union ccb *);
762 static timeout_t isp_refire_putback_atio;
763 static timeout_t isp_refire_notify_ack;
764 static void isp_complete_ctio(union ccb *);
765 static void isp_target_putback_atio(union ccb *);
766 enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
767 static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
768 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
769 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
770 static void isp_handle_platform_ctio(ispsoftc_t *, void *);
771 static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *, uint32_t rsp);
772 static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
773 static void isp_target_mark_aborted_early(ispsoftc_t *, int chan, tstate_t *, uint32_t);
774
775 static ISP_INLINE tstate_t *
776 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
777 {
778         tstate_t *tptr = NULL;
779         struct tslist *lhp;
780
781         if (bus < isp->isp_nchan) {
782                 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
783                 SLIST_FOREACH(tptr, lhp, next) {
784                         if (tptr->ts_lun == lun)
785                                 return (tptr);
786                 }
787         }
788         return (NULL);
789 }
790
791 static int
792 isp_atio_restart(ispsoftc_t *isp, int bus, tstate_t *tptr)
793 {
794         inot_private_data_t *ntp;
795         struct ntpdlist rq;
796
797         if (STAILQ_EMPTY(&tptr->restart_queue))
798                 return (0);
799         STAILQ_INIT(&rq);
800         STAILQ_CONCAT(&rq, &tptr->restart_queue);
801         while ((ntp = STAILQ_FIRST(&rq)) != NULL) {
802                 STAILQ_REMOVE_HEAD(&rq, next);
803                 if (IS_24XX(isp)) {
804                         isp_prt(isp, ISP_LOGTDEBUG0,
805                             "%s: restarting resrc deprived %x", __func__,
806                             ((at7_entry_t *)ntp->data)->at_rxid);
807                         isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->data);
808                 } else {
809                         isp_prt(isp, ISP_LOGTDEBUG0,
810                             "%s: restarting resrc deprived %x", __func__,
811                             ((at2_entry_t *)ntp->data)->at_rxid);
812                         isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->data);
813                 }
814                 isp_put_ntpd(isp, bus, ntp);
815                 if (!STAILQ_EMPTY(&tptr->restart_queue))
816                         break;
817         }
818         if (!STAILQ_EMPTY(&rq)) {
819                 STAILQ_CONCAT(&rq, &tptr->restart_queue);
820                 STAILQ_CONCAT(&tptr->restart_queue, &rq);
821         }
822         return (!STAILQ_EMPTY(&tptr->restart_queue));
823 }
824
825 static void
826 isp_tmcmd_restart(ispsoftc_t *isp)
827 {
828         tstate_t *tptr;
829         union ccb *ccb;
830         struct tslist *lhp;
831         struct isp_ccbq *waitq;
832         int bus, i;
833
834         for (bus = 0; bus < isp->isp_nchan; bus++) {
835                 for (i = 0; i < LUN_HASH_SIZE; i++) {
836                         ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
837                         SLIST_FOREACH(tptr, lhp, next)
838                                 isp_atio_restart(isp, bus, tptr);
839                 }
840
841                 /*
842                  * We only need to do this once per channel.
843                  */
844                 ISP_GET_PC_ADDR(isp, bus, waitq, waitq);
845                 ccb = (union ccb *)TAILQ_FIRST(waitq);
846                 if (ccb != NULL) {
847                         TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
848                         isp_target_start_ctio(isp, ccb, FROM_TIMER);
849                 }
850         }
851 }
852
853 static atio_private_data_t *
854 isp_get_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
855 {
856         struct atpdlist *atfree;
857         struct atpdlist *atused;
858         atio_private_data_t *atp;
859
860         ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
861         atp = LIST_FIRST(atfree);
862         if (atp) {
863                 LIST_REMOVE(atp, next);
864                 atp->tag = tag;
865                 ISP_GET_PC(isp, chan, atused, atused);
866                 LIST_INSERT_HEAD(&atused[ATPDPHASH(tag)], atp, next);
867         }
868         return (atp);
869 }
870
871 static atio_private_data_t *
872 isp_find_atpd(ispsoftc_t *isp, int chan, uint32_t tag)
873 {
874         struct atpdlist *atused;
875         atio_private_data_t *atp;
876
877         ISP_GET_PC(isp, chan, atused, atused);
878         LIST_FOREACH(atp, &atused[ATPDPHASH(tag)], next) {
879                 if (atp->tag == tag)
880                         return (atp);
881         }
882         return (NULL);
883 }
884
885 static void
886 isp_put_atpd(ispsoftc_t *isp, int chan, atio_private_data_t *atp)
887 {
888         struct atpdlist *atfree;
889
890         if (atp->ests) {
891                 isp_put_ecmd(isp, atp->ests);
892         }
893         LIST_REMOVE(atp, next);
894         memset(atp, 0, sizeof (*atp));
895         ISP_GET_PC_ADDR(isp, chan, atfree, atfree);
896         LIST_INSERT_HEAD(atfree, atp, next);
897 }
898
899 static void
900 isp_dump_atpd(ispsoftc_t *isp, int chan)
901 {
902         atio_private_data_t *atp, *atpool;
903         const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
904
905         ISP_GET_PC(isp, chan, atpool, atpool);
906         for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
907                 if (atp->state == ATPD_STATE_FREE)
908                         continue;
909                 isp_prt(isp, ISP_LOGALL, "Chan %d ATP [0x%x] origdlen %u bytes_xfrd %u lun %jx nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s",
910                     chan, atp->tag, atp->orig_datalen, atp->bytes_xfered, (uintmax_t)atp->lun, atp->nphdl, atp->sid, atp->did, atp->oxid, states[atp->state & 0x7]);
911         }
912 }
913
914 static inot_private_data_t *
915 isp_get_ntpd(ispsoftc_t *isp, int chan)
916 {
917         struct ntpdlist *ntfree;
918         inot_private_data_t *ntp;
919
920         ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
921         ntp = STAILQ_FIRST(ntfree);
922         if (ntp)
923                 STAILQ_REMOVE_HEAD(ntfree, next);
924         return (ntp);
925 }
926
927 static inot_private_data_t *
928 isp_find_ntpd(ispsoftc_t *isp, int chan, uint32_t tag_id, uint32_t seq_id)
929 {
930         inot_private_data_t *ntp, *ntp2;
931
932         ISP_GET_PC(isp, chan, ntpool, ntp);
933         ISP_GET_PC_ADDR(isp, chan, ntpool[ATPDPSIZE], ntp2);
934         for (; ntp < ntp2; ntp++) {
935                 if (ntp->tag_id == tag_id && ntp->seq_id == seq_id)
936                         return (ntp);
937         }
938         return (NULL);
939 }
940
941 static void
942 isp_put_ntpd(ispsoftc_t *isp, int chan, inot_private_data_t *ntp)
943 {
944         struct ntpdlist *ntfree;
945
946         ntp->tag_id = ntp->seq_id = 0;
947         ISP_GET_PC_ADDR(isp, chan, ntfree, ntfree);
948         STAILQ_INSERT_HEAD(ntfree, ntp, next);
949 }
950
951 static cam_status
952 create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
953 {
954         lun_id_t lun;
955         struct tslist *lhp;
956         tstate_t *tptr;
957
958         lun = xpt_path_lun_id(path);
959         if (lun != CAM_LUN_WILDCARD) {
960                 if (ISP_MAX_LUNS(isp) > 0 && lun >= ISP_MAX_LUNS(isp)) {
961                         return (CAM_LUN_INVALID);
962                 }
963         }
964         tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
965         if (tptr == NULL) {
966                 return (CAM_RESRC_UNAVAIL);
967         }
968         tptr->ts_lun = lun;
969         SLIST_INIT(&tptr->atios);
970         SLIST_INIT(&tptr->inots);
971         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
972         SLIST_INSERT_HEAD(lhp, tptr, next);
973         *rslt = tptr;
974         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
975         return (CAM_REQ_CMP);
976 }
977
978 static void
979 destroy_lun_state(ispsoftc_t *isp, int bus, tstate_t *tptr)
980 {
981         union ccb *ccb;
982         struct tslist *lhp;
983         inot_private_data_t *ntp;
984
985         while ((ccb = (union ccb *)SLIST_FIRST(&tptr->atios)) != NULL) {
986                 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
987                 ccb->ccb_h.status = CAM_REQ_ABORTED;
988                 xpt_done(ccb);
989         };
990         while ((ccb = (union ccb *)SLIST_FIRST(&tptr->inots)) != NULL) {
991                 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
992                 ccb->ccb_h.status = CAM_REQ_ABORTED;
993                 xpt_done(ccb);
994         }
995         while ((ntp = STAILQ_FIRST(&tptr->restart_queue)) != NULL) {
996                 isp_endcmd(isp, ntp->data, NIL_HANDLE, bus, SCSI_STATUS_BUSY, 0);
997                 STAILQ_REMOVE_HEAD(&tptr->restart_queue, next);
998                 isp_put_ntpd(isp, bus, ntp);
999         }
1000         ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1001         SLIST_REMOVE(lhp, tptr, tstate, next);
1002         free(tptr, M_DEVBUF);
1003 }
1004
1005 static void
1006 isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1007 {
1008         tstate_t *tptr;
1009         int bus;
1010         target_id_t target;
1011         lun_id_t lun;
1012
1013         if (!IS_FC(isp) || !ISP_CAP_TMODE(isp) || !ISP_CAP_SCCFW(isp)) {
1014                 xpt_print(ccb->ccb_h.path, "Target mode is not supported\n");
1015                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1016                 xpt_done(ccb);
1017                 return;
1018         }
1019
1020         /*
1021          * We only support either target and lun both wildcard
1022          * or target and lun both non-wildcard.
1023          */
1024         bus = XS_CHANNEL(ccb);
1025         target = ccb->ccb_h.target_id;
1026         lun = ccb->ccb_h.target_lun;
1027         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1028             "enabling lun %jx\n", (uintmax_t)lun);
1029         if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1030                 ccb->ccb_h.status = CAM_LUN_INVALID;
1031                 xpt_done(ccb);
1032                 return;
1033         }
1034
1035         /* Create the state pointer. It should not already exist. */
1036         tptr = get_lun_statep(isp, bus, lun);
1037         if (tptr) {
1038                 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1039                 xpt_done(ccb);
1040                 return;
1041         }
1042         ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1043         if (ccb->ccb_h.status != CAM_REQ_CMP) {
1044                 xpt_done(ccb);
1045                 return;
1046         }
1047
1048         ccb->ccb_h.status = CAM_REQ_CMP;
1049         xpt_done(ccb);
1050 }
1051
1052 static void
1053 isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1054 {
1055         tstate_t *tptr = NULL;
1056         int bus;
1057         target_id_t target;
1058         lun_id_t lun;
1059
1060         bus = XS_CHANNEL(ccb);
1061         target = ccb->ccb_h.target_id;
1062         lun = ccb->ccb_h.target_lun;
1063         ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path,
1064             "disabling lun %jx\n", (uintmax_t)lun);
1065         if ((target == CAM_TARGET_WILDCARD) != (lun == CAM_LUN_WILDCARD)) {
1066                 ccb->ccb_h.status = CAM_LUN_INVALID;
1067                 xpt_done(ccb);
1068                 return;
1069         }
1070
1071         /* Find the state pointer. */
1072         if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1073                 ccb->ccb_h.status = CAM_PATH_INVALID;
1074                 xpt_done(ccb);
1075                 return;
1076         }
1077
1078         destroy_lun_state(isp, bus, tptr);
1079         ccb->ccb_h.status = CAM_REQ_CMP;
1080         xpt_done(ccb);
1081 }
1082
1083 static void
1084 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1085 {
1086         int fctape, sendstatus, resid;
1087         fcparam *fcp;
1088         atio_private_data_t *atp;
1089         struct ccb_scsiio *cso;
1090         struct isp_ccbq *waitq;
1091         uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1092         uint8_t local[QENTRY_LEN];
1093
1094         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
1095             (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1096
1097         ISP_GET_PC_ADDR(isp, XS_CHANNEL(ccb), waitq, waitq);
1098         switch (how) {
1099         case FROM_CAM:
1100                 /*
1101                  * Insert at the tail of the list, if any, waiting CTIO CCBs
1102                  */
1103                 TAILQ_INSERT_TAIL(waitq, &ccb->ccb_h, periph_links.tqe);
1104                 break;
1105         case FROM_TIMER:
1106         case FROM_SRR:
1107         case FROM_CTIO_DONE:
1108                 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1109                 break;
1110         }
1111
1112         while ((ccb = (union ccb *) TAILQ_FIRST(waitq)) != NULL) {
1113                 TAILQ_REMOVE(waitq, &ccb->ccb_h, periph_links.tqe);
1114
1115                 cso = &ccb->csio;
1116                 xfrlen = cso->dxfer_len;
1117                 if (xfrlen == 0) {
1118                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1119                                 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1120                                 ccb->ccb_h.status = CAM_REQ_INVALID;
1121                                 xpt_done(ccb);
1122                                 continue;
1123                         }
1124                 }
1125
1126                 atp = isp_find_atpd(isp, XS_CHANNEL(ccb), cso->tag_id);
1127                 if (atp == NULL) {
1128                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1129                         isp_dump_atpd(isp, XS_CHANNEL(ccb));
1130                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1131                         xpt_done(ccb);
1132                         continue;
1133                 }
1134
1135                 /*
1136                  * Is this command a dead duck?
1137                  */
1138                 if (atp->dead) {
1139                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1140                         ccb->ccb_h.status = CAM_REQ_ABORTED;
1141                         xpt_done(ccb);
1142                         continue;
1143                 }
1144
1145                 /*
1146                  * Check to make sure we're still in target mode.
1147                  */
1148                 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1149                 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1150                         isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1151                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1152                         xpt_done(ccb);
1153                         continue;
1154                 }
1155
1156                 /*
1157                  * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1158                  * could be split into two CTIOs to split data and status).
1159                  */
1160                 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1161                         isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1162                         TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1163                         break;
1164                 }
1165
1166                 /*
1167                  * Does the initiator expect FC-Tape style responses?
1168                  */
1169                 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1170                         fctape = 1;
1171                 } else {
1172                         fctape = 0;
1173                 }
1174
1175                 /*
1176                  * If we already did the data xfer portion of a CTIO that sends data
1177                  * and status, don't do it again and do the status portion now.
1178                  */
1179                 if (atp->sendst) {
1180                         isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1181                             cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1182                         xfrlen = 0;     /* we already did the data transfer */
1183                         atp->sendst = 0;
1184                 }
1185                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1186                         sendstatus = 1;
1187                 } else {
1188                         sendstatus = 0;
1189                 }
1190
1191                 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1192                         KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1193                         /*
1194                          * Sense length is not the entire sense data structure size. Periph
1195                          * drivers don't seem to be setting sense_len to reflect the actual
1196                          * size. We'll peek inside to get the right amount.
1197                          */
1198                         sense_length = cso->sense_len;
1199
1200                         /*
1201                          * This 'cannot' happen
1202                          */
1203                         if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1204                                 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1205                         }
1206                 } else {
1207                         sense_length = 0;
1208                 }
1209
1210                 memset(local, 0, QENTRY_LEN);
1211
1212                 /*
1213                  * Check for overflow
1214                  */
1215                 tmp = atp->bytes_xfered + atp->bytes_in_transit;
1216                 if (xfrlen > 0 && tmp > atp->orig_datalen) {
1217                         isp_prt(isp, ISP_LOGERR,
1218                             "%s: [0x%x] data overflow by %u bytes", __func__,
1219                             cso->tag_id, tmp + xfrlen - atp->orig_datalen);
1220                         ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1221                         xpt_done(ccb);
1222                         continue;
1223                 }
1224                 if (xfrlen > atp->orig_datalen - tmp) {
1225                         xfrlen = atp->orig_datalen - tmp;
1226                         if (xfrlen == 0 && !sendstatus) {
1227                                 cso->resid = cso->dxfer_len;
1228                                 ccb->ccb_h.status = CAM_REQ_CMP;
1229                                 xpt_done(ccb);
1230                                 continue;
1231                         }
1232                 }
1233
1234                 if (IS_24XX(isp)) {
1235                         ct7_entry_t *cto = (ct7_entry_t *) local;
1236
1237                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1238                         cto->ct_header.rqs_entry_count = 1;
1239                         cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1240                         ATPD_SET_SEQNO(cto, atp);
1241                         cto->ct_nphdl = atp->nphdl;
1242                         cto->ct_rxid = atp->tag;
1243                         cto->ct_iid_lo = atp->sid;
1244                         cto->ct_iid_hi = atp->sid >> 16;
1245                         cto->ct_oxid = atp->oxid;
1246                         cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1247                         cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1248                         cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1249
1250                         /*
1251                          * Mode 1, status, no data. Only possible when we are sending status, have
1252                          * no data to transfer, and any sense data can fit into a ct7_entry_t.
1253                          *
1254                          * Mode 2, status, no data. We have to use this in the case that
1255                          * the sense data won't fit into a ct7_entry_t.
1256                          *
1257                          */
1258                         if (sendstatus && xfrlen == 0) {
1259                                 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1260                                 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1261                                 if (sense_length <= MAXRESPLEN_24XX) {
1262                                         cto->ct_flags |= CT7_FLAG_MODE1;
1263                                         cto->ct_scsi_status = cso->scsi_status;
1264                                         if (resid < 0) {
1265                                                 cto->ct_resid = -resid;
1266                                                 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1267                                         } else if (resid > 0) {
1268                                                 cto->ct_resid = resid;
1269                                                 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1270                                         }
1271                                         if (fctape) {
1272                                                 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1273                                         }
1274                                         if (sense_length) {
1275                                                 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1276                                                 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1277                                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1278                                         }
1279                                 } else {
1280                                         bus_addr_t addr;
1281                                         char buf[XCMD_SIZE];
1282                                         fcp_rsp_iu_t *rp;
1283
1284                                         if (atp->ests == NULL) {
1285                                                 atp->ests = isp_get_ecmd(isp);
1286                                                 if (atp->ests == NULL) {
1287                                                         TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1288                                                         break;
1289                                                 }
1290                                         }
1291                                         memset(buf, 0, sizeof (buf));
1292                                         rp = (fcp_rsp_iu_t *)buf;
1293                                         if (fctape) {
1294                                                 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1295                                                 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1296                                         }
1297                                         cto->ct_flags |= CT7_FLAG_MODE2;
1298                                         rp->fcp_rsp_scsi_status = cso->scsi_status;
1299                                         if (resid < 0) {
1300                                                 rp->fcp_rsp_resid = -resid;
1301                                                 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1302                                         } else if (resid > 0) {
1303                                                 rp->fcp_rsp_resid = resid;
1304                                                 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1305                                         }
1306                                         if (sense_length) {
1307                                                 rp->fcp_rsp_snslen = sense_length;
1308                                                 cto->ct_senselen = sense_length;
1309                                                 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1310                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1311                                                 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1312                                         } else {
1313                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1314                                         }
1315                                         if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1316                                                 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1317                                         }
1318                                         addr = isp->isp_osinfo.ecmd_dma;
1319                                         addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1320                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1321                                             (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1322                                         cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1323                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1324                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1325                                         cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1326                                 }
1327                                 if (sense_length) {
1328                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
1329                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
1330                                             cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1331                                 } else {
1332                                         isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1333                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1334                                 }
1335                                 atp->state = ATPD_STATE_LAST_CTIO;
1336                         }
1337
1338                         /*
1339                          * Mode 0 data transfers, *possibly* with status.
1340                          */
1341                         if (xfrlen != 0) {
1342                                 cto->ct_flags |= CT7_FLAG_MODE0;
1343                                 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1344                                         cto->ct_flags |= CT7_DATA_IN;
1345                                 } else {
1346                                         cto->ct_flags |= CT7_DATA_OUT;
1347                                 }
1348
1349                                 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1350                                 cto->rsp.m0.ct_xfrlen = xfrlen;
1351
1352 #ifdef  DEBUG
1353                                 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1354                                         isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1355                                         ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1356                                         cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1357                                 }
1358 #endif
1359                                 if (sendstatus) {
1360                                         resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1361                                         if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1362                                                 cto->ct_flags |= CT7_SENDSTATUS;
1363                                                 atp->state = ATPD_STATE_LAST_CTIO;
1364                                                 if (fctape) {
1365                                                         cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1366                                                 }
1367                                         } else {
1368                                                 atp->sendst = 1;        /* send status later */
1369                                                 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1370                                                 atp->state = ATPD_STATE_CTIO;
1371                                         }
1372                                 } else {
1373                                         atp->state = ATPD_STATE_CTIO;
1374                                 }
1375                                 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
1376                                     cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1377                         }
1378                 } else {
1379                         ct2_entry_t *cto = (ct2_entry_t *) local;
1380
1381                         if (isp->isp_osinfo.sixtyfourbit)
1382                                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1383                         else
1384                                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1385                         cto->ct_header.rqs_entry_count = 1;
1386                         cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1387                         ATPD_SET_SEQNO(cto, atp);
1388                         if (ISP_CAP_2KLOGIN(isp)) {
1389                                 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1390                         } else {
1391                                 cto->ct_iid = atp->nphdl;
1392                                 if (ISP_CAP_SCCFW(isp) == 0) {
1393                                         cto->ct_lun = ccb->ccb_h.target_lun;
1394                                 }
1395                         }
1396                         cto->ct_timeout = (XS_TIME(ccb) + 999) / 1000;
1397                         cto->ct_rxid = cso->tag_id;
1398
1399                         /*
1400                          * Mode 1, status, no data. Only possible when we are sending status, have
1401                          * no data to transfer, and the sense length can fit in the ct7_entry.
1402                          *
1403                          * Mode 2, status, no data. We have to use this in the case the response
1404                          * length won't fit into a ct2_entry_t.
1405                          *
1406                          * We'll fill out this structure with information as if this were a
1407                          * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1408                          * needed based upon this.
1409                          */
1410                         if (sendstatus && xfrlen == 0) {
1411                                 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1412                                 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1413                                 if (sense_length <= MAXRESPLEN) {
1414                                         if (resid < 0) {
1415                                                 cto->ct_resid = -resid;
1416                                         } else if (resid > 0) {
1417                                                 cto->ct_resid = resid;
1418                                         }
1419                                         cto->ct_flags |= CT2_FLAG_MODE1;
1420                                         cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1421                                         if (resid < 0) {
1422                                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1423                                         } else if (resid > 0) {
1424                                                 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1425                                         }
1426                                         if (fctape) {
1427                                                 cto->ct_flags |= CT2_CONFIRM;
1428                                         }
1429                                         if (sense_length) {
1430                                                 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1431                                                 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1432                                                 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1433                                         }
1434                                 } else {
1435                                         bus_addr_t addr;
1436                                         char buf[XCMD_SIZE];
1437                                         fcp_rsp_iu_t *rp;
1438
1439                                         if (atp->ests == NULL) {
1440                                                 atp->ests = isp_get_ecmd(isp);
1441                                                 if (atp->ests == NULL) {
1442                                                         TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1443                                                         break;
1444                                                 }
1445                                         }
1446                                         memset(buf, 0, sizeof (buf));
1447                                         rp = (fcp_rsp_iu_t *)buf;
1448                                         if (fctape) {
1449                                                 cto->ct_flags |= CT2_CONFIRM;
1450                                                 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1451                                         }
1452                                         cto->ct_flags |= CT2_FLAG_MODE2;
1453                                         rp->fcp_rsp_scsi_status = cso->scsi_status;
1454                                         if (resid < 0) {
1455                                                 rp->fcp_rsp_resid = -resid;
1456                                                 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1457                                         } else if (resid > 0) {
1458                                                 rp->fcp_rsp_resid = resid;
1459                                                 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1460                                         }
1461                                         if (sense_length) {
1462                                                 rp->fcp_rsp_snslen = sense_length;
1463                                                 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1464                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1465                                                 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1466                                         } else {
1467                                                 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1468                                         }
1469                                         if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1470                                                 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1471                                         }
1472                                         addr = isp->isp_osinfo.ecmd_dma;
1473                                         addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1474                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1475                                             (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1476                                         cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1477                                         if (cto->ct_header.rqs_entry_type == RQSTYPE_CTIO3) {
1478                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
1479                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
1480                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1481                                         } else {
1482                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
1483                                                 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1484                                         }
1485                                 }
1486                                 if (sense_length) {
1487                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
1488                                             cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
1489                                             cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1490                                 } else {
1491                                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
1492                                             ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
1493                                 }
1494                                 atp->state = ATPD_STATE_LAST_CTIO;
1495                         }
1496
1497                         if (xfrlen != 0) {
1498                                 cto->ct_flags |= CT2_FLAG_MODE0;
1499                                 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1500                                         cto->ct_flags |= CT2_DATA_IN;
1501                                 } else {
1502                                         cto->ct_flags |= CT2_DATA_OUT;
1503                                 }
1504
1505                                 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
1506                                 cto->rsp.m0.ct_xfrlen = xfrlen;
1507
1508                                 if (sendstatus) {
1509                                         resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1510                                         if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
1511                                                 cto->ct_flags |= CT2_SENDSTATUS;
1512                                                 atp->state = ATPD_STATE_LAST_CTIO;
1513                                                 if (fctape) {
1514                                                         cto->ct_flags |= CT2_CONFIRM;
1515                                                 }
1516                                         } else {
1517                                                 atp->sendst = 1;        /* send status later */
1518                                                 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1519                                                 atp->state = ATPD_STATE_CTIO;
1520                                         }
1521                                 } else {
1522                                         atp->state = ATPD_STATE_CTIO;
1523                                 }
1524                         }
1525                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
1526                             ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
1527                 }
1528
1529                 if (isp_get_pcmd(isp, ccb)) {
1530                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
1531                         TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1532                         break;
1533                 }
1534                 handle = isp_allocate_handle(isp, ccb, ISP_HANDLE_TARGET);
1535                 if (handle == 0) {
1536                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
1537                         TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1538                         isp_free_pcmd(isp, ccb);
1539                         break;
1540                 }
1541                 atp->bytes_in_transit += xfrlen;
1542                 PISP_PCMD(ccb)->datalen = xfrlen;
1543
1544
1545                 /*
1546                  * Call the dma setup routines for this entry (and any subsequent
1547                  * CTIOs) if there's data to move, and then tell the f/w it's got
1548                  * new things to play with. As with isp_start's usage of DMA setup,
1549                  * any swizzling is done in the machine dependent layer. Because
1550                  * of this, we put the request onto the queue area first in native
1551                  * format.
1552                  */
1553
1554                 if (IS_24XX(isp)) {
1555                         ct7_entry_t *cto = (ct7_entry_t *) local;
1556                         cto->ct_syshandle = handle;
1557                 } else {
1558                         ct2_entry_t *cto = (ct2_entry_t *) local;
1559                         cto->ct_syshandle = handle;
1560                 }
1561
1562                 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
1563                 if (dmaresult != CMD_QUEUED) {
1564                         isp_destroy_handle(isp, handle);
1565                         isp_free_pcmd(isp, ccb);
1566                         if (dmaresult == CMD_EAGAIN) {
1567                                 TAILQ_INSERT_HEAD(waitq, &ccb->ccb_h, periph_links.tqe);
1568                                 break;
1569                         }
1570                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1571                         xpt_done(ccb);
1572                         continue;
1573                 }
1574                 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
1575                 if (xfrlen) {
1576                         ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
1577                 } else {
1578                         ccb->ccb_h.spriv_field0 = ~0;
1579                 }
1580                 atp->ctcnt++;
1581                 atp->seqno++;
1582         }
1583 }
1584
1585 static void
1586 isp_refire_putback_atio(void *arg)
1587 {
1588         union ccb *ccb = arg;
1589
1590         ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
1591         isp_target_putback_atio(ccb);
1592 }
1593
1594 static void
1595 isp_refire_notify_ack(void *arg)
1596 {
1597         isp_tna_t *tp  = arg;
1598         ispsoftc_t *isp = tp->isp;
1599
1600         ISP_ASSERT_LOCKED(isp);
1601         if (isp_notify_ack(isp, tp->not)) {
1602                 callout_schedule(&tp->timer, 5);
1603         } else {
1604                 free(tp, M_DEVBUF);
1605         }
1606 }
1607
1608
1609 static void
1610 isp_target_putback_atio(union ccb *ccb)
1611 {
1612         ispsoftc_t *isp = XS_ISP(ccb);
1613         struct ccb_scsiio *cso = &ccb->csio;
1614         at2_entry_t local, *at = &local;
1615
1616         ISP_MEMZERO(at, sizeof (at2_entry_t));
1617         at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1618         at->at_header.rqs_entry_count = 1;
1619         if (ISP_CAP_SCCFW(isp)) {
1620                 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1621         } else {
1622                 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1623         }
1624         at->at_status = CT_OK;
1625         at->at_rxid = cso->tag_id;
1626         at->at_iid = cso->ccb_h.target_id;
1627         if (isp_target_put_entry(isp, at)) {
1628                 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
1629                     isp_refire_putback_atio, ccb);
1630         } else
1631                 isp_complete_ctio(ccb);
1632 }
1633
1634 static void
1635 isp_complete_ctio(union ccb *ccb)
1636 {
1637         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1638                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1639                 xpt_done(ccb);
1640         }
1641 }
1642
1643 static void
1644 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1645 {
1646         fcparam *fcp;
1647         lun_id_t lun;
1648         fcportdb_t *lp;
1649         tstate_t *tptr;
1650         struct ccb_accept_tio *atiop;
1651         uint16_t nphdl;
1652         atio_private_data_t *atp;
1653         inot_private_data_t *ntp;
1654
1655         /*
1656          * The firmware status (except for the QLTM_SVALID bit)
1657          * indicates why this ATIO was sent to us.
1658          *
1659          * If QLTM_SVALID is set, the firmware has recommended Sense Data.
1660          */
1661         if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1662                 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
1663                 isp_endcmd(isp, aep, NIL_HANDLE, 0, SCSI_STATUS_BUSY, 0);
1664                 return;
1665         }
1666
1667         fcp = FCPARAM(isp, 0);
1668         if (ISP_CAP_SCCFW(isp)) {
1669                 lun = aep->at_scclun;
1670         } else {
1671                 lun = aep->at_lun;
1672         }
1673         if (ISP_CAP_2KLOGIN(isp)) {
1674                 nphdl = ((at2e_entry_t *)aep)->at_iid;
1675         } else {
1676                 nphdl = aep->at_iid;
1677         }
1678         tptr = get_lun_statep(isp, 0, lun);
1679         if (tptr == NULL) {
1680                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1681                 if (tptr == NULL) {
1682                         isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %jx or wildcard", __func__, aep->at_rxid, (uintmax_t)lun);
1683                         if (lun == 0) {
1684                                 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1685                         } else {
1686                                 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1687                         }
1688                         return;
1689                 }
1690         }
1691
1692         /*
1693          * Start any commands pending resources first.
1694          */
1695         if (isp_atio_restart(isp, 0, tptr))
1696                 goto noresrc;
1697
1698         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1699         if (atiop == NULL) {
1700                 goto noresrc;
1701         }
1702
1703         atp = isp_get_atpd(isp, 0, aep->at_rxid);
1704         if (atp == NULL) {
1705                 goto noresrc;
1706         }
1707
1708         atp->state = ATPD_STATE_ATIO;
1709         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1710         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1711         atiop->ccb_h.target_id = fcp->isp_loopid;
1712         atiop->ccb_h.target_lun = lun;
1713
1714         /*
1715          * We don't get 'suggested' sense data as we do with SCSI cards.
1716          */
1717         atiop->sense_len = 0;
1718
1719         /*
1720          * If we're not in the port database, add ourselves.
1721          */
1722         if (IS_2100(isp))
1723                 atiop->init_id = nphdl;
1724         else {
1725                 if (isp_find_pdb_by_handle(isp, 0, nphdl, &lp)) {
1726                         atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
1727                 } else {
1728                         isp_prt(isp, ISP_LOGTINFO, "%s: port %x isn't in PDB",
1729                             __func__, nphdl);
1730                         isp_dump_portdb(isp, 0);
1731                         isp_endcmd(isp, aep, NIL_HANDLE, 0, ECMD_TERMINATE, 0);
1732                         return;
1733                 }
1734         }
1735         atiop->cdb_len = ATIO2_CDBLEN;
1736         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1737         atiop->ccb_h.status = CAM_CDB_RECVD;
1738         atiop->tag_id = atp->tag;
1739         switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1740         case ATIO2_TC_ATTR_SIMPLEQ:
1741                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1742                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1743                 break;
1744         case ATIO2_TC_ATTR_HEADOFQ:
1745                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1746                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1747                 break;
1748         case ATIO2_TC_ATTR_ORDERED:
1749                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1750                 atiop->tag_action = MSG_ORDERED_Q_TAG;
1751                 break;
1752         case ATIO2_TC_ATTR_ACAQ:                /* ?? */
1753         case ATIO2_TC_ATTR_UNTAGGED:
1754         default:
1755                 atiop->tag_action = 0;
1756                 break;
1757         }
1758
1759         atp->orig_datalen = aep->at_datalen;
1760         atp->bytes_xfered = 0;
1761         atp->lun = lun;
1762         atp->nphdl = nphdl;
1763         atp->sid = PORT_ANY;
1764         atp->oxid = aep->at_oxid;
1765         atp->cdb0 = aep->at_cdb[0];
1766         atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
1767         atp->state = ATPD_STATE_CAM;
1768         xpt_done((union ccb *)atiop);
1769         isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %jx datalen %u", aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
1770         return;
1771 noresrc:
1772         ntp = isp_get_ntpd(isp, 0);
1773         if (ntp == NULL) {
1774                 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
1775                 return;
1776         }
1777         memcpy(ntp->data, aep, QENTRY_LEN);
1778         STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1779 }
1780
1781 static void
1782 isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
1783 {
1784         int cdbxlen;
1785         lun_id_t lun;
1786         uint16_t chan, nphdl = NIL_HANDLE;
1787         uint32_t did, sid;
1788         fcportdb_t *lp;
1789         tstate_t *tptr;
1790         struct ccb_accept_tio *atiop;
1791         atio_private_data_t *atp = NULL;
1792         atio_private_data_t *oatp;
1793         inot_private_data_t *ntp;
1794
1795         did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
1796         sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
1797         lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
1798
1799         if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
1800                 /* Channel has to be derived from D_ID */
1801                 isp_find_chan_by_did(isp, did, &chan);
1802                 if (chan == ISP_NOCHAN) {
1803                         isp_prt(isp, ISP_LOGWARN,
1804                             "%s: [RX_ID 0x%x] D_ID %x not found on any channel",
1805                             __func__, aep->at_rxid, did);
1806                         isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
1807                             ECMD_TERMINATE, 0);
1808                         return;
1809                 }
1810         } else {
1811                 chan = 0;
1812         }
1813
1814         /*
1815          * Find the PDB entry for this initiator
1816          */
1817         if (isp_find_pdb_by_portid(isp, chan, sid, &lp) == 0) {
1818                 /*
1819                  * If we're not in the port database terminate the exchange.
1820                  */
1821                 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",
1822                     __func__, aep->at_rxid, did, chan, sid);
1823                 isp_dump_portdb(isp, chan);
1824                 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
1825                 return;
1826         }
1827         nphdl = lp->handle;
1828
1829         /*
1830          * Get the tstate pointer
1831          */
1832         tptr = get_lun_statep(isp, chan, lun);
1833         if (tptr == NULL) {
1834                 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
1835                 if (tptr == NULL) {
1836                         isp_prt(isp, ISP_LOGWARN,
1837                             "%s: [0x%x] no state pointer for lun %jx or wildcard",
1838                             __func__, aep->at_rxid, (uintmax_t)lun);
1839                         if (lun == 0) {
1840                                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1841                         } else {
1842                                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
1843                         }
1844                         return;
1845                 }
1846         }
1847
1848         /*
1849          * Start any commands pending resources first.
1850          */
1851         if (isp_atio_restart(isp, chan, tptr))
1852                 goto noresrc;
1853
1854         /*
1855          * If the f/w is out of resources, just send a BUSY status back.
1856          */
1857         if (aep->at_rxid == AT7_NORESRC_RXID) {
1858                 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
1859                 return;
1860         }
1861
1862         /*
1863          * If we're out of resources, just send a BUSY status back.
1864          */
1865         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1866         if (atiop == NULL) {
1867                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
1868                 goto noresrc;
1869         }
1870
1871         oatp = isp_find_atpd(isp, chan, aep->at_rxid);
1872         if (oatp) {
1873                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
1874                     aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
1875                 /*
1876                  * It's not a "no resource" condition- but we can treat it like one
1877                  */
1878                 goto noresrc;
1879         }
1880         atp = isp_get_atpd(isp, chan, aep->at_rxid);
1881         if (atp == NULL) {
1882                 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
1883                 goto noresrc;
1884         }
1885         atp->word3 = lp->prli_word3;
1886         atp->state = ATPD_STATE_ATIO;
1887         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1888         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO\n");
1889         atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
1890         atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
1891         atiop->ccb_h.target_lun = lun;
1892         atiop->sense_len = 0;
1893         cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
1894         if (cdbxlen) {
1895                 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
1896         }
1897         cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
1898         ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
1899         atiop->cdb_len = cdbxlen;
1900         atiop->ccb_h.status = CAM_CDB_RECVD;
1901         atiop->tag_id = atp->tag;
1902         switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
1903         case FCP_CMND_TASK_ATTR_SIMPLE:
1904                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1905                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1906                 break;
1907         case FCP_CMND_TASK_ATTR_HEAD:
1908                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1909                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1910                 break;
1911         case FCP_CMND_TASK_ATTR_ORDERED:
1912                 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
1913                 atiop->tag_action = MSG_ORDERED_Q_TAG;
1914                 break;
1915         default:
1916                 /* FALLTHROUGH */
1917         case FCP_CMND_TASK_ATTR_ACA:
1918         case FCP_CMND_TASK_ATTR_UNTAGGED:
1919                 atiop->tag_action = 0;
1920                 break;
1921         }
1922         atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
1923         atp->bytes_xfered = 0;
1924         atp->lun = lun;
1925         atp->nphdl = nphdl;
1926         atp->sid = sid;
1927         atp->did = did;
1928         atp->oxid = aep->at_hdr.ox_id;
1929         atp->rxid = aep->at_hdr.rx_id;
1930         atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
1931         atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
1932         atp->state = ATPD_STATE_CAM;
1933         isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %jx datalen %u",
1934             aep->at_rxid, atp->cdb0, (uintmax_t)lun, atp->orig_datalen);
1935         xpt_done((union ccb *)atiop);
1936         return;
1937 noresrc:
1938         if (atp)
1939                 isp_put_atpd(isp, chan, atp);
1940         ntp = isp_get_ntpd(isp, chan);
1941         if (ntp == NULL) {
1942                 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
1943                 return;
1944         }
1945         memcpy(ntp->data, aep, QENTRY_LEN);
1946         STAILQ_INSERT_TAIL(&tptr->restart_queue, ntp, next);
1947 }
1948
1949
1950 /*
1951  * Handle starting an SRR (sequence retransmit request)
1952  * We get here when we've gotten the immediate notify
1953  * and the return of all outstanding CTIOs for this
1954  * transaction.
1955  */
1956 static void
1957 isp_handle_srr_start(ispsoftc_t *isp, atio_private_data_t *atp)
1958 {
1959         in_fcentry_24xx_t *inot;
1960         uint32_t srr_off, ccb_off, ccb_len, ccb_end;
1961         union ccb *ccb;
1962
1963         inot = (in_fcentry_24xx_t *)atp->srr;
1964         srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
1965         ccb = atp->srr_ccb;
1966         atp->srr_ccb = NULL;
1967         atp->nsrr++;
1968         if (ccb == NULL) {
1969                 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
1970                 goto fail;
1971         }
1972
1973         ccb_off = ccb->ccb_h.spriv_field0;
1974         ccb_len = ccb->csio.dxfer_len;
1975         ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
1976
1977         switch (inot->in_srr_iu) {
1978         case R_CTL_INFO_SOLICITED_DATA:
1979                 /*
1980                  * We have to restart a FCP_DATA data out transaction
1981                  */
1982                 atp->sendst = 0;
1983                 atp->bytes_xfered = srr_off;
1984                 if (ccb_len == 0) {
1985                         isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
1986                         goto mdp;
1987                 }
1988                 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
1989                         isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
1990                         goto mdp;
1991                 }
1992                 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
1993                 break;
1994         case R_CTL_INFO_COMMAND_STATUS:
1995                 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
1996                 atp->sendst = 1;
1997                 /*
1998                  * We have to restart a FCP_RSP IU transaction
1999                  */
2000                 break;
2001         case R_CTL_INFO_DATA_DESCRIPTOR:
2002                 /*
2003                  * We have to restart an FCP DATA in transaction
2004                  */
2005                 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2006                 goto fail;
2007                 
2008         default:
2009                 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2010                 goto fail;
2011         }
2012
2013         /*
2014          * We can't do anything until this is acked, so we might as well start it now.
2015          * We aren't going to do the usual asynchronous ack issue because we need
2016          * to make sure this gets on the wire first.
2017          */
2018         if (isp_notify_ack(isp, inot)) {
2019                 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2020                 goto fail;
2021         }
2022         isp_target_start_ctio(isp, ccb, FROM_SRR);
2023         return;
2024 fail:
2025         inot->in_reserved = 1;
2026         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2027         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2028         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2029         isp_complete_ctio(ccb);
2030         return;
2031 mdp:
2032         if (isp_notify_ack(isp, inot)) {
2033                 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2034                 goto fail;
2035         }
2036         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2037         ccb->ccb_h.status = CAM_MESSAGE_RECV;
2038         /*
2039          * This is not a strict interpretation of MDP, but it's close
2040          */
2041         ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2042         ccb->csio.msg_len = 7;
2043         ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2044         ccb->csio.msg_ptr[1] = 5;
2045         ccb->csio.msg_ptr[2] = 0;       /* modify data pointer */
2046         ccb->csio.msg_ptr[3] = srr_off >> 24;
2047         ccb->csio.msg_ptr[4] = srr_off >> 16;
2048         ccb->csio.msg_ptr[5] = srr_off >> 8;
2049         ccb->csio.msg_ptr[6] = srr_off;
2050         isp_complete_ctio(ccb);
2051 }
2052
2053
2054 static void
2055 isp_handle_platform_srr(ispsoftc_t *isp, isp_notify_t *notify)
2056 {
2057         in_fcentry_24xx_t *inot = notify->nt_lreserved;
2058         atio_private_data_t *atp;
2059         uint32_t tag = notify->nt_tagval & 0xffffffff;
2060
2061         atp = isp_find_atpd(isp, notify->nt_channel, tag);
2062         if (atp == NULL) {
2063                 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify",
2064                     __func__, tag);
2065                 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2066                 return;
2067         }
2068         atp->srr_notify_rcvd = 1;
2069         memcpy(atp->srr, inot, sizeof (atp->srr));
2070         isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] flags 0x%x srr_iu %x reloff 0x%x",
2071             inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2072             ((uint32_t)inot->in_srr_reloff_hi << 16) | inot->in_srr_reloff_lo);
2073         if (atp->srr_ccb)
2074                 isp_handle_srr_start(isp, atp);
2075 }
2076
2077 static void
2078 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2079 {
2080         union ccb *ccb;
2081         int sentstatus = 0, ok = 0, notify_cam = 0, failure = 0;
2082         atio_private_data_t *atp = NULL;
2083         int bus;
2084         uint32_t handle, data_requested, resid;
2085
2086         handle = ((ct2_entry_t *)arg)->ct_syshandle;
2087         ccb = isp_find_xs(isp, handle);
2088         if (ccb == NULL) {
2089                 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2090                 return;
2091         }
2092         isp_destroy_handle(isp, handle);
2093         resid = data_requested = PISP_PCMD(ccb)->datalen;
2094         isp_free_pcmd(isp, ccb);
2095
2096         bus = XS_CHANNEL(ccb);
2097         if (IS_24XX(isp)) {
2098                 atp = isp_find_atpd(isp, bus, ((ct7_entry_t *)arg)->ct_rxid);
2099         } else {
2100                 atp = isp_find_atpd(isp, bus, ((ct2_entry_t *)arg)->ct_rxid);
2101         }
2102         if (atp == NULL) {
2103                 /*
2104                  * XXX: isp_clear_commands() generates fake CTIO with zero
2105                  * ct_rxid value, filling only ct_syshandle.  Workaround
2106                  * that using tag_id from the CCB, pointed by ct_syshandle.
2107                  */
2108                 atp = isp_find_atpd(isp, bus, ccb->csio.tag_id);
2109         }
2110         if (atp == NULL) {
2111                 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2112                 return;
2113         }
2114         KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2115         atp->bytes_in_transit -= data_requested;
2116         atp->ctcnt -= 1;
2117         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2118
2119         if (IS_24XX(isp)) {
2120                 ct7_entry_t *ct = arg;
2121
2122                 if (ct->ct_nphdl == CT7_SRR) {
2123                         atp->srr_ccb = ccb;
2124                         if (atp->srr_notify_rcvd)
2125                                 isp_handle_srr_start(isp, atp);
2126                         return;
2127                 }
2128                 if (ct->ct_nphdl == CT_HBA_RESET) {
2129                         sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2130                             (atp->sendst == 0);
2131                         failure = CAM_UNREC_HBA_ERROR;
2132                 } else {
2133                         sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2134                         ok = (ct->ct_nphdl == CT7_OK);
2135                         notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2136                         if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA)
2137                                 resid = ct->ct_resid;
2138                 }
2139                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2140                    notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2141         } else {
2142                 ct2_entry_t *ct = arg;
2143                 if (ct->ct_status == CT_SRR) {
2144                         atp->srr_ccb = ccb;
2145                         if (atp->srr_notify_rcvd)
2146                                 isp_handle_srr_start(isp, atp);
2147                         isp_target_putback_atio(ccb);
2148                         return;
2149                 }
2150                 if (ct->ct_status == CT_HBA_RESET) {
2151                         sentstatus = (ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2152                             (atp->sendst == 0);
2153                         failure = CAM_UNREC_HBA_ERROR;
2154                 } else {
2155                         sentstatus = ct->ct_flags & CT2_SENDSTATUS;
2156                         ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
2157                         notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2158                         if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA)
2159                                 resid = ct->ct_resid;
2160                 }
2161                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2162                     notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2163         }
2164         if (ok) {
2165                 if (data_requested > 0) {
2166                         atp->bytes_xfered += data_requested - resid;
2167                         ccb->csio.resid = ccb->csio.dxfer_len -
2168                             (data_requested - resid);
2169                 }
2170                 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE))
2171                         ccb->ccb_h.status |= CAM_SENT_SENSE;
2172                 ccb->ccb_h.status |= CAM_REQ_CMP;
2173         } else {
2174                 notify_cam = 1;
2175                 if (failure == CAM_UNREC_HBA_ERROR)
2176                         ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
2177                 else
2178                         ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2179         }
2180         atp->state = ATPD_STATE_PDON;
2181
2182         /*
2183          * We never *not* notify CAM when there has been any error (ok == 0),
2184          * so we never need to do an ATIO putback if we're not notifying CAM.
2185          */
2186         isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
2187             (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
2188         if (notify_cam == 0) {
2189                 if (atp->sendst) {
2190                         isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
2191                 }
2192                 return;
2193         }
2194
2195         /*
2196          * We are done with this ATIO if we successfully sent status.
2197          * In all other cases expect either another CTIO or XPT_ABORT.
2198          */
2199         if (ok && sentstatus)
2200                 isp_put_atpd(isp, bus, atp);
2201
2202         /*
2203          * We're telling CAM we're done with this CTIO transaction.
2204          *
2205          * 24XX cards never need an ATIO put back.
2206          *
2207          * Other cards need one put back only on error.
2208          * In the latter case, a timeout will re-fire
2209          * and try again in case we didn't have
2210          * queue resources to do so at first. In any case,
2211          * once the putback is done we do the completion
2212          * call.
2213          */
2214         if (ok || IS_24XX(isp)) {
2215                 isp_complete_ctio(ccb);
2216         } else {
2217                 isp_target_putback_atio(ccb);
2218         }
2219 }
2220
2221 static int
2222 isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp, uint32_t rsp)
2223 {
2224
2225         if (isp->isp_state != ISP_RUNSTATE) {
2226                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
2227                 return (0);
2228         }
2229
2230         /*
2231          * This case is for a Task Management Function, which shows up as an ATIO7 entry.
2232          */
2233         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
2234                 ct7_entry_t local, *cto = &local;
2235                 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
2236                 fcportdb_t *lp;
2237                 uint32_t sid;
2238                 uint16_t nphdl;
2239
2240                 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2241                 if (isp_find_pdb_by_portid(isp, mp->nt_channel, sid, &lp)) {
2242                         nphdl = lp->handle;
2243                 } else {
2244                         nphdl = NIL_HANDLE;
2245                 }
2246                 ISP_MEMZERO(&local, sizeof (local));
2247                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2248                 cto->ct_header.rqs_entry_count = 1;
2249                 cto->ct_nphdl = nphdl;
2250                 cto->ct_rxid = aep->at_rxid;
2251                 cto->ct_vpidx = mp->nt_channel;
2252                 cto->ct_iid_lo = sid;
2253                 cto->ct_iid_hi = sid >> 16;
2254                 cto->ct_oxid = aep->at_hdr.ox_id;
2255                 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
2256                 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
2257                 if (rsp != 0) {
2258                         cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
2259                         cto->rsp.m1.ct_resplen = 4;
2260                         ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
2261                         cto->rsp.m1.ct_resp[0] = rsp & 0xff;
2262                         cto->rsp.m1.ct_resp[1] = (rsp >> 8) & 0xff;
2263                         cto->rsp.m1.ct_resp[2] = (rsp >> 16) & 0xff;
2264                         cto->rsp.m1.ct_resp[3] = (rsp >> 24) & 0xff;
2265                 }
2266                 return (isp_target_put_entry(isp, &local));
2267         }
2268
2269         /*
2270          * This case is for a responding to an ABTS frame
2271          */
2272         if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2273
2274                 /*
2275                  * Overload nt_need_ack here to mark whether we've terminated the associated command.
2276                  */
2277                 if (mp->nt_need_ack) {
2278                         uint8_t storage[QENTRY_LEN];
2279                         ct7_entry_t *cto = (ct7_entry_t *) storage;
2280                         abts_t *abts = (abts_t *)mp->nt_lreserved;
2281
2282                         ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2283                         isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
2284                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2285                         cto->ct_header.rqs_entry_count = 1;
2286                         cto->ct_nphdl = mp->nt_nphdl;
2287                         cto->ct_rxid = abts->abts_rxid_task;
2288                         cto->ct_iid_lo = mp->nt_sid;
2289                         cto->ct_iid_hi = mp->nt_sid >> 16;
2290                         cto->ct_oxid = abts->abts_ox_id;
2291                         cto->ct_vpidx = mp->nt_channel;
2292                         cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2293                         if (isp_target_put_entry(isp, cto)) {
2294                                 return (ENOMEM);
2295                         }
2296                         mp->nt_need_ack = 0;
2297                 }
2298                 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
2299                         return (ENOMEM);
2300                 } else {
2301                         return (0);
2302                 }
2303         }
2304
2305         /*
2306          * Handle logout cases here
2307          */
2308         if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
2309                 isp_del_all_wwn_entries(isp, mp->nt_channel);
2310         }
2311
2312         if (mp->nt_ncode == NT_LOGOUT) {
2313                 if (!IS_2100(isp) && IS_FC(isp)) {
2314                         isp_del_wwn_entries(isp, mp);
2315                 }
2316         }
2317
2318         /*
2319          * General purpose acknowledgement
2320          */
2321         if (mp->nt_need_ack) {
2322                 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
2323                 /*
2324                  * Don't need to use the guaranteed send because the caller can retry
2325                  */
2326                 return (isp_notify_ack(isp, mp->nt_lreserved));
2327         }
2328         return (0);
2329 }
2330
2331 /*
2332  * Handle task management functions.
2333  *
2334  * We show up here with a notify structure filled out.
2335  *
2336  * The nt_lreserved tag points to the original queue entry
2337  */
2338 static void
2339 isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
2340 {
2341         tstate_t *tptr;
2342         fcportdb_t *lp;
2343         struct ccb_immediate_notify *inot;
2344         inot_private_data_t *ntp = NULL;
2345         atio_private_data_t *atp;
2346         lun_id_t lun;
2347
2348         isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid  0x%x tagval 0x%016llx chan %d lun %jx", __func__, notify->nt_ncode,
2349             notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
2350         if (notify->nt_lun == LUN_ANY) {
2351                 if (notify->nt_tagval == TAG_ANY) {
2352                         lun = CAM_LUN_WILDCARD;
2353                 } else {
2354                         atp = isp_find_atpd(isp, notify->nt_channel,
2355                             notify->nt_tagval & 0xffffffff);
2356                         lun = atp ? atp->lun : CAM_LUN_WILDCARD;
2357                 }
2358         } else {
2359                 lun = notify->nt_lun;
2360         }
2361         tptr = get_lun_statep(isp, notify->nt_channel, lun);
2362         if (tptr == NULL) {
2363                 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
2364                 if (tptr == NULL) {
2365                         isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2366                         goto bad;
2367                 }
2368         }
2369         inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
2370         if (inot == NULL) {
2371                 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun %#jx", __func__, notify->nt_channel, (uintmax_t)lun);
2372                 goto bad;
2373         }
2374
2375         if (isp_find_pdb_by_portid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
2376             isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
2377                 inot->initiator_id = CAM_TARGET_WILDCARD;
2378         } else {
2379                 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
2380         }
2381         inot->seq_id = notify->nt_tagval;
2382         inot->tag_id = notify->nt_tagval >> 32;
2383
2384         switch (notify->nt_ncode) {
2385         case NT_ABORT_TASK:
2386                 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, inot->tag_id);
2387                 inot->arg = MSG_ABORT_TASK;
2388                 break;
2389         case NT_ABORT_TASK_SET:
2390                 isp_target_mark_aborted_early(isp, notify->nt_channel, tptr, TAG_ANY);
2391                 inot->arg = MSG_ABORT_TASK_SET;
2392                 break;
2393         case NT_CLEAR_ACA:
2394                 inot->arg = MSG_CLEAR_ACA;
2395                 break;
2396         case NT_CLEAR_TASK_SET:
2397                 inot->arg = MSG_CLEAR_TASK_SET;
2398                 break;
2399         case NT_LUN_RESET:
2400                 inot->arg = MSG_LOGICAL_UNIT_RESET;
2401                 break;
2402         case NT_TARGET_RESET:
2403                 inot->arg = MSG_TARGET_RESET;
2404                 break;
2405         case NT_QUERY_TASK_SET:
2406                 inot->arg = MSG_QUERY_TASK_SET;
2407                 break;
2408         case NT_QUERY_ASYNC_EVENT:
2409                 inot->arg = MSG_QUERY_ASYNC_EVENT;
2410                 break;
2411         default:
2412                 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun %#jx", __func__, notify->nt_ncode, notify->nt_channel, (uintmax_t)lun);
2413                 goto bad;
2414         }
2415
2416         ntp = isp_get_ntpd(isp, notify->nt_channel);
2417         if (ntp == NULL) {
2418                 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
2419                 goto bad;
2420         }
2421         ISP_MEMCPY(&ntp->nt, notify, sizeof (isp_notify_t));
2422         if (notify->nt_lreserved) {
2423                 ISP_MEMCPY(&ntp->data, notify->nt_lreserved, QENTRY_LEN);
2424                 ntp->nt.nt_lreserved = &ntp->data;
2425         }
2426         ntp->seq_id = notify->nt_tagval;
2427         ntp->tag_id = notify->nt_tagval >> 32;
2428
2429         SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
2430         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "Take FREE INOT\n");
2431         inot->ccb_h.status = CAM_MESSAGE_RECV;
2432         xpt_done((union ccb *)inot);
2433         return;
2434 bad:
2435         if (notify->nt_need_ack) {
2436                 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
2437                         if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
2438                                 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
2439                         }
2440                 } else {
2441                         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
2442                 }
2443         }
2444 }
2445
2446 static void
2447 isp_target_mark_aborted_early(ispsoftc_t *isp, int chan, tstate_t *tptr, uint32_t tag_id)
2448 {
2449         atio_private_data_t *atp, *atpool;
2450         inot_private_data_t *ntp, *tmp;
2451         uint32_t this_tag_id;
2452
2453         /*
2454          * First, clean any commands pending restart
2455          */
2456         STAILQ_FOREACH_SAFE(ntp, &tptr->restart_queue, next, tmp) {
2457                 if (IS_24XX(isp))
2458                         this_tag_id = ((at7_entry_t *)ntp->data)->at_rxid;
2459                 else
2460                         this_tag_id = ((at2_entry_t *)ntp->data)->at_rxid;
2461                 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
2462                         isp_endcmd(isp, ntp->data, NIL_HANDLE, chan,
2463                             ECMD_TERMINATE, 0);
2464                         isp_put_ntpd(isp, chan, ntp);
2465                         STAILQ_REMOVE(&tptr->restart_queue, ntp,
2466                             inot_private_data, next);
2467                 }
2468         }
2469
2470         /*
2471          * Now mark other ones dead as well.
2472          */
2473         ISP_GET_PC(isp, chan, atpool, atpool);
2474         for (atp = atpool; atp < &atpool[ATPDPSIZE]; atp++) {
2475                 if (atp->lun != tptr->ts_lun)
2476                         continue;
2477                 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id)
2478                         atp->dead = 1;
2479         }
2480 }
2481 #endif
2482
2483 static void
2484 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
2485 {
2486         struct cam_sim *sim;
2487         int bus, tgt;
2488         ispsoftc_t *isp;
2489
2490         sim = (struct cam_sim *)cbarg;
2491         isp = (ispsoftc_t *) cam_sim_softc(sim);
2492         bus = cam_sim_bus(sim);
2493         tgt = xpt_path_target_id(path);
2494
2495         switch (code) {
2496         case AC_LOST_DEVICE:
2497                 if (IS_SCSI(isp)) {
2498                         uint16_t oflags, nflags;
2499                         sdparam *sdp = SDPARAM(isp, bus);
2500
2501                         if (tgt >= 0) {
2502                                 nflags = sdp->isp_devparam[tgt].nvrm_flags;
2503                                 nflags &= DPARM_SAFE_DFLT;
2504                                 if (isp->isp_loaded_fw) {
2505                                         nflags |= DPARM_NARROW | DPARM_ASYNC;
2506                                 }
2507                                 oflags = sdp->isp_devparam[tgt].goal_flags;
2508                                 sdp->isp_devparam[tgt].goal_flags = nflags;
2509                                 sdp->isp_devparam[tgt].dev_update = 1;
2510                                 sdp->update = 1;
2511                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
2512                                 sdp->isp_devparam[tgt].goal_flags = oflags;
2513                         }
2514                 }
2515                 break;
2516         default:
2517                 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2518                 break;
2519         }
2520 }
2521
2522 static void
2523 isp_poll(struct cam_sim *sim)
2524 {
2525         ispsoftc_t *isp = cam_sim_softc(sim);
2526
2527         ISP_RUN_ISR(isp);
2528 }
2529
2530
2531 static void
2532 isp_watchdog(void *arg)
2533 {
2534         struct ccb_scsiio *xs = arg;
2535         ispsoftc_t *isp;
2536         uint32_t ohandle = ISP_HANDLE_FREE, handle;
2537
2538         isp = XS_ISP(xs);
2539
2540         handle = isp_find_handle(isp, xs);
2541
2542         /*
2543          * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
2544          */
2545         if (handle != ISP_HANDLE_FREE) {
2546                 ISP_RUN_ISR(isp);
2547                 ohandle = handle;
2548                 handle = isp_find_handle(isp, xs);
2549         }
2550         if (handle != ISP_HANDLE_FREE) {
2551                 /*
2552                  * Try and make sure the command is really dead before
2553                  * we release the handle (and DMA resources) for reuse.
2554                  *
2555                  * If we are successful in aborting the command then
2556                  * we're done here because we'll get the command returned
2557                  * back separately.
2558                  */
2559                 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
2560                         return;
2561                 }
2562
2563                 /*
2564                  * Note that after calling the above, the command may in
2565                  * fact have been completed.
2566                  */
2567                 xs = isp_find_xs(isp, handle);
2568
2569                 /*
2570                  * If the command no longer exists, then we won't
2571                  * be able to find the xs again with this handle.
2572                  */
2573                 if (xs == NULL) {
2574                         return;
2575                 }
2576
2577                 /*
2578                  * After this point, the command is really dead.
2579                  */
2580                 if (XS_XFRLEN(xs)) {
2581                         ISP_DMAFREE(isp, xs, handle);
2582                 } 
2583                 isp_destroy_handle(isp, handle);
2584                 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
2585                 xs->ccb_h.status &= ~CAM_STATUS_MASK;
2586                 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
2587                 isp_prt_endcmd(isp, xs);
2588                 isp_done(xs);
2589         } else {
2590                 if (ohandle != ISP_HANDLE_FREE) {
2591                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
2592                 } else {
2593                         isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
2594                 }
2595         }
2596 }
2597
2598 static void
2599 isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2600 {
2601         union ccb *ccb;
2602         struct isp_fc *fc = ISP_FC_PC(isp, chan);
2603
2604         /*
2605          * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
2606          */
2607         ccb = xpt_alloc_ccb_nowait();
2608         if (ccb == NULL) {
2609                 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
2610                 return;
2611         }
2612         if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
2613             tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2614                 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
2615                 xpt_free_ccb(ccb);
2616                 return;
2617         }
2618         xpt_rescan(ccb);
2619 }
2620
2621 static void
2622 isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
2623 {
2624         struct cam_path *tp;
2625         struct isp_fc *fc = ISP_FC_PC(isp, chan);
2626
2627         if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2628                 xpt_async(AC_LOST_DEVICE, tp, NULL);
2629                 xpt_free_path(tp);
2630         }
2631 }
2632
2633 /*
2634  * Gone Device Timer Function- when we have decided that a device has gone
2635  * away, we wait a specific period of time prior to telling the OS it has
2636  * gone away.
2637  *
2638  * This timer function fires once a second and then scans the port database
2639  * for devices that are marked dead but still have a virtual target assigned.
2640  * We decrement a counter for that port database entry, and when it hits zero,
2641  * we tell the OS the device has gone away.
2642  */
2643 static void
2644 isp_gdt(void *arg)
2645 {
2646         struct isp_fc *fc = arg;
2647         taskqueue_enqueue(taskqueue_thread, &fc->gtask);
2648 }
2649
2650 static void
2651 isp_gdt_task(void *arg, int pending)
2652 {
2653         struct isp_fc *fc = arg;
2654         ispsoftc_t *isp = fc->isp;
2655         int chan = fc - isp->isp_osinfo.pc.fc;
2656         fcportdb_t *lp;
2657         struct ac_contract ac;
2658         struct ac_device_changed *adc;
2659         int dbidx, more_to_do = 0;
2660
2661         ISP_LOCK(isp);
2662         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
2663         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2664                 lp = &FCPARAM(isp, chan)->portdb[dbidx];
2665
2666                 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2667                         continue;
2668                 }
2669                 if (lp->gone_timer != 0) {
2670                         lp->gone_timer -= 1;
2671                         more_to_do++;
2672                         continue;
2673                 }
2674                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
2675                 if (lp->is_target) {
2676                         lp->is_target = 0;
2677                         isp_make_gone(isp, lp, chan, dbidx);
2678                 }
2679                 if (lp->is_initiator) {
2680                         lp->is_initiator = 0;
2681                         ac.contract_number = AC_CONTRACT_DEV_CHG;
2682                         adc = (struct ac_device_changed *) ac.contract_data;
2683                         adc->wwpn = lp->port_wwn;
2684                         adc->port = lp->portid;
2685                         adc->target = dbidx;
2686                         adc->arrived = 0;
2687                         xpt_async(AC_CONTRACT, fc->path, &ac);
2688                 }
2689                 lp->state = FC_PORTDB_STATE_NIL;
2690         }
2691         if (fc->ready) {
2692                 if (more_to_do) {
2693                         callout_reset(&fc->gdt, hz, isp_gdt, fc);
2694                 } else {
2695                         callout_deactivate(&fc->gdt);
2696                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
2697                 }
2698         }
2699         ISP_UNLOCK(isp);
2700 }
2701
2702 /*
2703  * When loop goes down we remember the time and freeze CAM command queue.
2704  * During some time period we are trying to reprobe the loop.  But if we
2705  * fail, we tell the OS that devices have gone away and drop the freeze.
2706  *
2707  * We don't clear the devices out of our port database because, when loop
2708  * come back up, we have to do some actual cleanup with the chip at that
2709  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2710  */
2711 static void
2712 isp_loop_changed(ispsoftc_t *isp, int chan)
2713 {
2714         fcparam *fcp = FCPARAM(isp, chan);
2715         struct isp_fc *fc = ISP_FC_PC(isp, chan);
2716
2717         if (fc->loop_down_time)
2718                 return;
2719         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop changed", chan);
2720         if (fcp->role & ISP_ROLE_INITIATOR)
2721                 isp_freeze_loopdown(isp, chan);
2722         fc->loop_down_time = time_uptime;
2723         wakeup(fc);
2724 }
2725
2726 static void
2727 isp_loop_up(ispsoftc_t *isp, int chan)
2728 {
2729         struct isp_fc *fc = ISP_FC_PC(isp, chan);
2730
2731         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is up", chan);
2732         fc->loop_seen_once = 1;
2733         fc->loop_down_time = 0;
2734         isp_unfreeze_loopdown(isp, chan);
2735 }
2736
2737 static void
2738 isp_loop_dead(ispsoftc_t *isp, int chan)
2739 {
2740         fcparam *fcp = FCPARAM(isp, chan);
2741         struct isp_fc *fc = ISP_FC_PC(isp, chan);
2742         fcportdb_t *lp;
2743         struct ac_contract ac;
2744         struct ac_device_changed *adc;
2745         int dbidx, i;
2746
2747         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop is dead", chan);
2748
2749         /*
2750          * Notify to the OS all targets who we now consider have departed.
2751          */
2752         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2753                 lp = &fcp->portdb[dbidx];
2754
2755                 if (lp->state == FC_PORTDB_STATE_NIL)
2756                         continue;
2757
2758                 /*
2759                  * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
2760                  */
2761                 for (i = 0; i < isp->isp_maxcmds; i++) {
2762                         struct ccb_scsiio *xs;
2763
2764                         if (ISP_H2HT(isp->isp_xflist[i].handle) != ISP_HANDLE_INITIATOR) {
2765                                 continue;
2766                         }
2767                         if ((xs = isp->isp_xflist[i].cmd) == NULL) {
2768                                 continue;
2769                         }
2770                         if (dbidx != XS_TGT(xs)) {
2771                                 continue;
2772                         }
2773                         isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%jx orphaned by loop down timeout",
2774                             isp->isp_xflist[i].handle, chan, XS_TGT(xs),
2775                             (uintmax_t)XS_LUN(xs));
2776                 }
2777
2778                 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
2779                 if (lp->is_target) {
2780                         lp->is_target = 0;
2781                         isp_make_gone(isp, lp, chan, dbidx);
2782                 }
2783                 if (lp->is_initiator) {
2784                         lp->is_initiator = 0;
2785                         ac.contract_number = AC_CONTRACT_DEV_CHG;
2786                         adc = (struct ac_device_changed *) ac.contract_data;
2787                         adc->wwpn = lp->port_wwn;
2788                         adc->port = lp->portid;
2789                         adc->target = dbidx;
2790                         adc->arrived = 0;
2791                         xpt_async(AC_CONTRACT, fc->path, &ac);
2792                 }
2793         }
2794
2795         isp_unfreeze_loopdown(isp, chan);
2796         fc->loop_down_time = 0;
2797 }
2798
2799 static void
2800 isp_kthread(void *arg)
2801 {
2802         struct isp_fc *fc = arg;
2803         ispsoftc_t *isp = fc->isp;
2804         int chan = fc - isp->isp_osinfo.pc.fc;
2805         int slp = 0, d;
2806         int lb, lim;
2807
2808         mtx_lock(&isp->isp_osinfo.lock);
2809
2810         while (isp->isp_osinfo.is_exiting == 0) {
2811                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2812                     "Chan %d Checking FC state", chan);
2813                 lb = isp_fc_runstate(isp, chan, 250000);
2814                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2815                     "Chan %d FC got to %s state", chan,
2816                     isp_fc_loop_statename(lb));
2817
2818                 /*
2819                  * Our action is different based upon whether we're supporting
2820                  * Initiator mode or not. If we are, we might freeze the simq
2821                  * when loop is down and set all sorts of different delays to
2822                  * check again.
2823                  *
2824                  * If not, we simply just wait for loop to come up.
2825                  */
2826                 if (lb == LOOP_READY || lb < 0) {
2827                         slp = 0;
2828                 } else {
2829                         /*
2830                          * If we've never seen loop up and we've waited longer
2831                          * than quickboot time, or we've seen loop up but we've
2832                          * waited longer than loop_down_limit, give up and go
2833                          * to sleep until loop comes up.
2834                          */
2835                         if (fc->loop_seen_once == 0)
2836                                 lim = isp_quickboot_time;
2837                         else
2838                                 lim = fc->loop_down_limit;
2839                         d = time_uptime - fc->loop_down_time;
2840                         if (d >= lim)
2841                                 slp = 0;
2842                         else if (d < 10)
2843                                 slp = 1;
2844                         else if (d < 30)
2845                                 slp = 5;
2846                         else if (d < 60)
2847                                 slp = 10;
2848                         else if (d < 120)
2849                                 slp = 20;
2850                         else
2851                                 slp = 30;
2852                 }
2853
2854                 if (slp == 0) {
2855                         if (lb == LOOP_READY)
2856                                 isp_loop_up(isp, chan);
2857                         else
2858                                 isp_loop_dead(isp, chan);
2859                 }
2860
2861                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0,
2862                     "Chan %d sleep for %d seconds", chan, slp);
2863                 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
2864         }
2865         fc->num_threads -= 1;
2866         mtx_unlock(&isp->isp_osinfo.lock);
2867         kthread_exit();
2868 }
2869
2870 #ifdef  ISP_TARGET_MODE
2871 static void
2872 isp_abort_atio(ispsoftc_t *isp, union ccb *ccb)
2873 {
2874         atio_private_data_t *atp;
2875         union ccb *accb = ccb->cab.abort_ccb;
2876         struct ccb_hdr *sccb;
2877         tstate_t *tptr;
2878
2879         tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2880         if (tptr != NULL) {
2881                 /* Search for the ATIO among queueued. */
2882                 SLIST_FOREACH(sccb, &tptr->atios, sim_links.sle) {
2883                         if (sccb != &accb->ccb_h)
2884                                 continue;
2885                         SLIST_REMOVE(&tptr->atios, sccb, ccb_hdr, sim_links.sle);
2886                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2887                             "Abort FREE ATIO\n");
2888                         accb->ccb_h.status = CAM_REQ_ABORTED;
2889                         xpt_done(accb);
2890                         ccb->ccb_h.status = CAM_REQ_CMP;
2891                         return;
2892                 }
2893         }
2894
2895         /* Search for the ATIO among running. */
2896         atp = isp_find_atpd(isp, XS_CHANNEL(accb), accb->atio.tag_id);
2897         if (atp != NULL) {
2898                 /* Send TERMINATE to firmware. */
2899                 if (!atp->dead && IS_24XX(isp)) {
2900                         uint8_t storage[QENTRY_LEN];
2901                         ct7_entry_t *cto = (ct7_entry_t *) storage;
2902
2903                         ISP_MEMZERO(cto, sizeof (ct7_entry_t));
2904                         cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
2905                         cto->ct_header.rqs_entry_count = 1;
2906                         cto->ct_nphdl = atp->nphdl;
2907                         cto->ct_rxid = atp->tag;
2908                         cto->ct_iid_lo = atp->sid;
2909                         cto->ct_iid_hi = atp->sid >> 16;
2910                         cto->ct_oxid = atp->oxid;
2911                         cto->ct_vpidx = XS_CHANNEL(accb);
2912                         cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
2913                         isp_target_put_entry(isp, cto);
2914                 }
2915                 isp_put_atpd(isp, XS_CHANNEL(accb), atp);
2916                 ccb->ccb_h.status = CAM_REQ_CMP;
2917         } else {
2918                 ccb->ccb_h.status = CAM_UA_ABORT;
2919         }
2920 }
2921
2922 static void
2923 isp_abort_inot(ispsoftc_t *isp, union ccb *ccb)
2924 {
2925         inot_private_data_t *ntp;
2926         union ccb *accb = ccb->cab.abort_ccb;
2927         struct ccb_hdr *sccb;
2928         tstate_t *tptr;
2929
2930         tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
2931         if (tptr != NULL) {
2932                 /* Search for the INOT among queueued. */
2933                 SLIST_FOREACH(sccb, &tptr->inots, sim_links.sle) {
2934                         if (sccb != &accb->ccb_h)
2935                                 continue;
2936                         SLIST_REMOVE(&tptr->inots, sccb, ccb_hdr, sim_links.sle);
2937                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, sccb->path,
2938                             "Abort FREE INOT\n");
2939                         accb->ccb_h.status = CAM_REQ_ABORTED;
2940                         xpt_done(accb);
2941                         ccb->ccb_h.status = CAM_REQ_CMP;
2942                         return;
2943                 }
2944         }
2945
2946         /* Search for the INOT among running. */
2947         ntp = isp_find_ntpd(isp, XS_CHANNEL(accb), accb->cin1.tag_id, accb->cin1.seq_id);
2948         if (ntp != NULL) {
2949                 if (ntp->nt.nt_need_ack) {
2950                         isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK,
2951                             ntp->nt.nt_lreserved);
2952                 }
2953                 isp_put_ntpd(isp, XS_CHANNEL(accb), ntp);
2954                 ccb->ccb_h.status = CAM_REQ_CMP;
2955         } else {
2956                 ccb->ccb_h.status = CAM_UA_ABORT;
2957                 return;
2958         }
2959 }
2960 #endif
2961
2962 static void
2963 isp_action(struct cam_sim *sim, union ccb *ccb)
2964 {
2965         int bus, tgt, ts, error;
2966         ispsoftc_t *isp;
2967         struct ccb_trans_settings *cts;
2968
2969         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2970
2971         isp = (ispsoftc_t *)cam_sim_softc(sim);
2972         mtx_assert(&isp->isp_lock, MA_OWNED);
2973         isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2974         ISP_PCMD(ccb) = NULL;
2975
2976         switch (ccb->ccb_h.func_code) {
2977         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
2978                 bus = XS_CHANNEL(ccb);
2979                 /*
2980                  * Do a couple of preliminary checks...
2981                  */
2982                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2983                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2984                                 ccb->ccb_h.status = CAM_REQ_INVALID;
2985                                 isp_done((struct ccb_scsiio *) ccb);
2986                                 break;
2987                         }
2988                 }
2989                 ccb->csio.req_map = NULL;
2990 #ifdef  DIAGNOSTIC
2991                 if (ccb->ccb_h.target_id >= ISP_MAX_TARGETS(isp)) {
2992                         xpt_print(ccb->ccb_h.path, "invalid target\n");
2993                         ccb->ccb_h.status = CAM_PATH_INVALID;
2994                 } else if (ISP_MAX_LUNS(isp) > 0 &&
2995                     ccb->ccb_h.target_lun >= ISP_MAX_LUNS(isp)) {
2996                         xpt_print(ccb->ccb_h.path, "invalid lun\n");
2997                         ccb->ccb_h.status = CAM_PATH_INVALID;
2998                 }
2999                 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
3000                         xpt_done(ccb);
3001                         break;
3002                 }
3003 #endif
3004                 ccb->csio.scsi_status = SCSI_STATUS_OK;
3005                 if (isp_get_pcmd(isp, ccb)) {
3006                         isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
3007                         cam_freeze_devq(ccb->ccb_h.path);
3008                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
3009                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3010                         xpt_done(ccb);
3011                         break;
3012                 }
3013                 error = isp_start((XS_T *) ccb);
3014                 switch (error) {
3015                 case CMD_QUEUED:
3016                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
3017                         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
3018                                 break;
3019                         }
3020                         ts = ccb->ccb_h.timeout;
3021                         if (ts == CAM_TIME_DEFAULT) {
3022                                 ts = 60*1000;
3023                         }
3024                         ts = isp_mstohz(ts);
3025                         callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
3026                         break;
3027                 case CMD_RQLATER:
3028                         isp_prt(isp, ISP_LOGDEBUG0, "%d.%jx retry later",
3029                             XS_TGT(ccb), (uintmax_t)XS_LUN(ccb));
3030                         cam_freeze_devq(ccb->ccb_h.path);
3031                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3032                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3033                         isp_free_pcmd(isp, ccb);
3034                         xpt_done(ccb);
3035                         break;
3036                 case CMD_EAGAIN:
3037                         isp_free_pcmd(isp, ccb);
3038                         cam_freeze_devq(ccb->ccb_h.path);
3039                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
3040                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3041                         xpt_done(ccb);
3042                         break;
3043                 case CMD_COMPLETE:
3044                         isp_done((struct ccb_scsiio *) ccb);
3045                         break;
3046                 default:
3047                         isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
3048                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
3049                         isp_free_pcmd(isp, ccb);
3050                         xpt_done(ccb);
3051                 }
3052                 break;
3053
3054 #ifdef  ISP_TARGET_MODE
3055         case XPT_EN_LUN:                /* Enable/Disable LUN as a target */
3056                 if (ccb->cel.enable) {
3057                         isp_enable_lun(isp, ccb);
3058                 } else {
3059                         isp_disable_lun(isp, ccb);
3060                 }
3061                 break;
3062         case XPT_IMMEDIATE_NOTIFY:      /* Add Immediate Notify Resource */
3063         case XPT_ACCEPT_TARGET_IO:      /* Add Accept Target IO Resource */
3064         {
3065                 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
3066                 if (tptr == NULL) {
3067                         const char *str;
3068
3069                         if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
3070                                 str = "XPT_IMMEDIATE_NOTIFY";
3071                         else
3072                                 str = "XPT_ACCEPT_TARGET_IO";
3073                         ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path,
3074                             "%s: no state pointer found for %s\n",
3075                             __func__, str);
3076                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3077                         xpt_done(ccb);
3078                         break;
3079                 }
3080                 ccb->ccb_h.spriv_field0 = 0;
3081                 ccb->ccb_h.spriv_ptr1 = isp;
3082
3083                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3084                         ccb->atio.tag_id = 0;
3085                         SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
3086                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3087                             "Put FREE ATIO\n");
3088                 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
3089                         ccb->cin1.seq_id = ccb->cin1.tag_id = 0;
3090                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
3091                         ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path,
3092                             "Put FREE INOT\n");
3093                 }
3094                 ccb->ccb_h.status = CAM_REQ_INPROG;
3095                 break;
3096         }
3097         case XPT_NOTIFY_ACKNOWLEDGE:            /* notify ack */
3098         {
3099                 inot_private_data_t *ntp;
3100
3101                 /*
3102                  * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
3103                  * XXX: matches that for the immediate notify, we have to *search* for the notify structure
3104                  */
3105                 /*
3106                  * All the relevant path information is in the associated immediate notify
3107                  */
3108                 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);
3109                 ntp = isp_find_ntpd(isp, XS_CHANNEL(ccb), ccb->cna2.tag_id, ccb->cna2.seq_id);
3110                 if (ntp == NULL) {
3111                         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__,
3112                              ccb->cna2.tag_id, ccb->cna2.seq_id);
3113                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3114                         xpt_done(ccb);
3115                         break;
3116                 }
3117                 if (isp_handle_platform_target_notify_ack(isp, &ntp->nt,
3118                     (ccb->ccb_h.flags & CAM_SEND_STATUS) ? ccb->cna2.arg : 0)) {
3119                         cam_freeze_devq(ccb->ccb_h.path);
3120                         cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
3121                         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
3122                         ccb->ccb_h.status |= CAM_REQUEUE_REQ;
3123                         break;
3124                 }
3125                 isp_put_ntpd(isp, XS_CHANNEL(ccb), ntp);
3126                 ccb->ccb_h.status = CAM_REQ_CMP;
3127                 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);
3128                 xpt_done(ccb);
3129                 break;
3130         }
3131         case XPT_CONT_TARGET_IO:
3132                 isp_target_start_ctio(isp, ccb, FROM_CAM);
3133                 break;
3134 #endif
3135         case XPT_RESET_DEV:             /* BDR the specified SCSI device */
3136                 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
3137                 tgt = ccb->ccb_h.target_id;
3138                 tgt |= (bus << 16);
3139
3140                 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
3141                 if (error) {
3142                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3143                 } else {
3144                         /*
3145                          * If we have a FC device, reset the Command
3146                          * Reference Number, because the target will expect
3147                          * that we re-start the CRN at 1 after a reset.
3148                          */
3149                         if (IS_FC(isp))
3150                                 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3151
3152                         ccb->ccb_h.status = CAM_REQ_CMP;
3153                 }
3154                 xpt_done(ccb);
3155                 break;
3156         case XPT_ABORT:                 /* Abort the specified CCB */
3157         {
3158                 union ccb *accb = ccb->cab.abort_ccb;
3159                 switch (accb->ccb_h.func_code) {
3160 #ifdef  ISP_TARGET_MODE
3161                 case XPT_ACCEPT_TARGET_IO:
3162                         isp_abort_atio(isp, ccb);
3163                         break;
3164                 case XPT_IMMEDIATE_NOTIFY:
3165                         isp_abort_inot(isp, ccb);
3166                         break;
3167 #endif
3168                 case XPT_SCSI_IO:
3169                         error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
3170                         if (error) {
3171                                 ccb->ccb_h.status = CAM_UA_ABORT;
3172                         } else {
3173                                 ccb->ccb_h.status = CAM_REQ_CMP;
3174                         }
3175                         break;
3176                 default:
3177                         ccb->ccb_h.status = CAM_REQ_INVALID;
3178                         break;
3179                 }
3180                 /*
3181                  * This is not a queued CCB, so the caller expects it to be
3182                  * complete when control is returned.
3183                  */
3184                 break;
3185         }
3186 #define IS_CURRENT_SETTINGS(c)  (c->type == CTS_TYPE_CURRENT_SETTINGS)
3187         case XPT_SET_TRAN_SETTINGS:     /* Nexus Settings */
3188                 cts = &ccb->cts;
3189                 if (!IS_CURRENT_SETTINGS(cts)) {
3190                         ccb->ccb_h.status = CAM_REQ_INVALID;
3191                         xpt_done(ccb);
3192                         break;
3193                 }
3194                 tgt = cts->ccb_h.target_id;
3195                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3196                 if (IS_SCSI(isp)) {
3197                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3198                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3199                         sdparam *sdp = SDPARAM(isp, bus);
3200                         uint16_t *dptr;
3201
3202                         if (spi->valid == 0 && scsi->valid == 0) {
3203                                 ccb->ccb_h.status = CAM_REQ_CMP;
3204                                 xpt_done(ccb);
3205                                 break;
3206                         }
3207
3208                         /*
3209                          * We always update (internally) from goal_flags
3210                          * so any request to change settings just gets
3211                          * vectored to that location.
3212                          */
3213                         dptr = &sdp->isp_devparam[tgt].goal_flags;
3214
3215                         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3216                                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
3217                                         *dptr |= DPARM_DISC;
3218                                 else
3219                                         *dptr &= ~DPARM_DISC;
3220                         }
3221
3222                         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3223                                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
3224                                         *dptr |= DPARM_TQING;
3225                                 else
3226                                         *dptr &= ~DPARM_TQING;
3227                         }
3228
3229                         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3230                                 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
3231                                         *dptr |= DPARM_WIDE;
3232                                 else
3233                                         *dptr &= ~DPARM_WIDE;
3234                         }
3235
3236                         /*
3237                          * XXX: FIX ME
3238                          */
3239                         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
3240                                 *dptr |= DPARM_SYNC;
3241                                 /*
3242                                  * XXX: CHECK FOR LEGALITY
3243                                  */
3244                                 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
3245                                 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
3246                         } else {
3247                                 *dptr &= ~DPARM_SYNC;
3248                         }
3249                         isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%jx) to flags %x off %x per %x", bus, tgt, (uintmax_t)cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
3250                             sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
3251                         sdp->isp_devparam[tgt].dev_update = 1;
3252                         sdp->update = 1;
3253                 }
3254                 ccb->ccb_h.status = CAM_REQ_CMP;
3255                 xpt_done(ccb);
3256                 break;
3257         case XPT_GET_TRAN_SETTINGS:
3258                 cts = &ccb->cts;
3259                 tgt = cts->ccb_h.target_id;
3260                 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
3261                 if (IS_FC(isp)) {
3262                         fcparam *fcp = FCPARAM(isp, bus);
3263                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3264                         struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
3265
3266                         cts->protocol = PROTO_SCSI;
3267                         cts->protocol_version = SCSI_REV_2;
3268                         cts->transport = XPORT_FC;
3269                         cts->transport_version = 0;
3270
3271                         scsi->valid = CTS_SCSI_VALID_TQ;
3272                         scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3273                         fc->valid = CTS_FC_VALID_SPEED;
3274                         fc->bitrate = 100000;
3275                         fc->bitrate *= fcp->isp_gbspeed;
3276                         if (tgt < MAX_FC_TARG) {
3277                                 fcportdb_t *lp = &fcp->portdb[tgt];
3278                                 fc->wwnn = lp->node_wwn;
3279                                 fc->wwpn = lp->port_wwn;
3280                                 fc->port = lp->portid;
3281                                 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
3282                         }
3283                 } else {
3284                         struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3285                         struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3286                         sdparam *sdp = SDPARAM(isp, bus);
3287                         uint16_t dval, pval, oval;
3288
3289                         if (IS_CURRENT_SETTINGS(cts)) {
3290                                 sdp->isp_devparam[tgt].dev_refresh = 1;
3291                                 sdp->update = 1;
3292                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
3293                                 dval = sdp->isp_devparam[tgt].actv_flags;
3294                                 oval = sdp->isp_devparam[tgt].actv_offset;
3295                                 pval = sdp->isp_devparam[tgt].actv_period;
3296                         } else {
3297                                 dval = sdp->isp_devparam[tgt].nvrm_flags;
3298                                 oval = sdp->isp_devparam[tgt].nvrm_offset;
3299                                 pval = sdp->isp_devparam[tgt].nvrm_period;
3300                         }
3301
3302                         cts->protocol = PROTO_SCSI;
3303                         cts->protocol_version = SCSI_REV_2;
3304                         cts->transport = XPORT_SPI;
3305                         cts->transport_version = 2;
3306
3307                         spi->valid = 0;
3308                         scsi->valid = 0;
3309                         spi->flags = 0;
3310                         scsi->flags = 0;
3311                         if (dval & DPARM_DISC) {
3312                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3313                         }
3314                         if ((dval & DPARM_SYNC) && oval && pval) {
3315                                 spi->sync_offset = oval;
3316                                 spi->sync_period = pval;
3317                         } else {
3318                                 spi->sync_offset = 0;
3319                                 spi->sync_period = 0;
3320                         }
3321                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3322                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3323                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3324                         if (dval & DPARM_WIDE) {
3325                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3326                         } else {
3327                                 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3328                         }
3329                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3330                                 scsi->valid = CTS_SCSI_VALID_TQ;
3331                                 if (dval & DPARM_TQING) {
3332                                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3333                                 }
3334                                 spi->valid |= CTS_SPI_VALID_DISC;
3335                         }
3336                         isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%jx) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
3337                             bus, tgt, (uintmax_t)cts->ccb_h.target_lun, dval, oval, pval);
3338                 }
3339                 ccb->ccb_h.status = CAM_REQ_CMP;
3340                 xpt_done(ccb);
3341                 break;
3342
3343         case XPT_CALC_GEOMETRY:
3344                 cam_calc_geometry(&ccb->ccg, 1);
3345                 xpt_done(ccb);
3346                 break;
3347
3348         case XPT_RESET_BUS:             /* Reset the specified bus */
3349                 bus = cam_sim_bus(sim);
3350                 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
3351                 if (error) {
3352                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3353                         xpt_done(ccb);
3354                         break;
3355                 }
3356                 if (bootverbose) {
3357                         xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
3358                 }
3359                 if (IS_FC(isp)) {
3360                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
3361                 } else {
3362                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
3363                 }
3364                 ccb->ccb_h.status = CAM_REQ_CMP;
3365                 xpt_done(ccb);
3366                 break;
3367
3368         case XPT_TERM_IO:               /* Terminate the I/O process */
3369                 ccb->ccb_h.status = CAM_REQ_INVALID;
3370                 xpt_done(ccb);
3371                 break;
3372
3373         case XPT_SET_SIM_KNOB:          /* Set SIM knobs */
3374         {
3375                 struct ccb_sim_knob *kp = &ccb->knob;
3376                 fcparam *fcp;
3377
3378                 if (!IS_FC(isp)) {
3379                         ccb->ccb_h.status = CAM_REQ_INVALID;
3380                         xpt_done(ccb);
3381                         break;
3382                 }
3383
3384                 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3385                 fcp = FCPARAM(isp, bus);
3386
3387                 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
3388                         fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
3389                         fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
3390                         isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
3391                 }
3392                 ccb->ccb_h.status = CAM_REQ_CMP;
3393                 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
3394                         int rchange = 0;
3395                         int newrole = 0;
3396
3397                         switch (kp->xport_specific.fc.role) {
3398                         case KNOB_ROLE_NONE:
3399                                 if (fcp->role != ISP_ROLE_NONE) {
3400                                         rchange = 1;
3401                                         newrole = ISP_ROLE_NONE;
3402                                 }
3403                                 break;
3404                         case KNOB_ROLE_TARGET:
3405                                 if (fcp->role != ISP_ROLE_TARGET) {
3406                                         rchange = 1;
3407                                         newrole = ISP_ROLE_TARGET;
3408                                 }
3409                                 break;
3410                         case KNOB_ROLE_INITIATOR:
3411                                 if (fcp->role != ISP_ROLE_INITIATOR) {
3412                                         rchange = 1;
3413                                         newrole = ISP_ROLE_INITIATOR;
3414                                 }
3415                                 break;
3416                         case KNOB_ROLE_BOTH:
3417                                 if (fcp->role != ISP_ROLE_BOTH) {
3418                                         rchange = 1;
3419                                         newrole = ISP_ROLE_BOTH;
3420                                 }
3421                                 break;
3422                         }
3423                         if (rchange) {
3424                                 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
3425                                 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
3426                                     bus, newrole) != 0) {
3427                                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3428                                         xpt_done(ccb);
3429                                         break;
3430                                 }
3431                         }
3432                 }
3433                 xpt_done(ccb);
3434                 break;
3435         }
3436         case XPT_GET_SIM_KNOB_OLD:      /* Get SIM knobs -- compat value */
3437         case XPT_GET_SIM_KNOB:          /* Get SIM knobs */
3438         {
3439                 struct ccb_sim_knob *kp = &ccb->knob;
3440
3441                 if (IS_FC(isp)) {
3442                         fcparam *fcp;
3443
3444                         bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
3445                         fcp = FCPARAM(isp, bus);
3446
3447                         kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
3448                         kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
3449                         switch (fcp->role) {
3450                         case ISP_ROLE_NONE:
3451                                 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3452                                 break;
3453                         case ISP_ROLE_TARGET:
3454                                 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3455                                 break;
3456                         case ISP_ROLE_INITIATOR:
3457                                 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3458                                 break;
3459                         case ISP_ROLE_BOTH:
3460                                 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3461                                 break;
3462                         }
3463                         kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3464                         ccb->ccb_h.status = CAM_REQ_CMP;
3465                 } else {
3466                         ccb->ccb_h.status = CAM_REQ_INVALID;
3467                 }
3468                 xpt_done(ccb);
3469                 break;
3470         }
3471         case XPT_PATH_INQ:              /* Path routing inquiry */
3472         {
3473                 struct ccb_pathinq *cpi = &ccb->cpi;
3474
3475                 cpi->version_num = 1;
3476 #ifdef  ISP_TARGET_MODE
3477                 if (IS_FC(isp) && ISP_CAP_TMODE(isp) && ISP_CAP_SCCFW(isp))
3478                         cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3479                 else
3480 #endif
3481                         cpi->target_sprt = 0;
3482                 cpi->hba_eng_cnt = 0;
3483                 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
3484                 cpi->max_lun = ISP_MAX_LUNS(isp) == 0 ?
3485                     255 : ISP_MAX_LUNS(isp) - 1;
3486                 cpi->bus_id = cam_sim_bus(sim);
3487                 if (isp->isp_osinfo.sixtyfourbit)
3488                         cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
3489                 else
3490                         cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
3491
3492                 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
3493                 if (IS_FC(isp)) {
3494                         fcparam *fcp = FCPARAM(isp, bus);
3495
3496                         cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
3497                         cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
3498
3499                         /*
3500                          * Because our loop ID can shift from time to time,
3501                          * make our initiator ID out of range of our bus.
3502                          */
3503                         cpi->initiator_id = cpi->max_target + 1;
3504
3505                         /*
3506                          * Set base transfer capabilities for Fibre Channel, for this HBA.
3507                          */
3508                         if (IS_25XX(isp)) {
3509                                 cpi->base_transfer_speed = 8000000;
3510                         } else if (IS_24XX(isp)) {
3511                                 cpi->base_transfer_speed = 4000000;
3512                         } else if (IS_23XX(isp)) {
3513                                 cpi->base_transfer_speed = 2000000;
3514                         } else {
3515                                 cpi->base_transfer_speed = 1000000;
3516                         }
3517                         cpi->hba_inquiry = PI_TAG_ABLE;
3518                         cpi->transport = XPORT_FC;
3519                         cpi->transport_version = 0;
3520                         cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
3521                         cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
3522                         cpi->xport_specific.fc.port = fcp->isp_portid;
3523                         cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
3524                 } else {
3525                         sdparam *sdp = SDPARAM(isp, bus);
3526                         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3527                         cpi->hba_misc = PIM_UNMAPPED;
3528                         cpi->initiator_id = sdp->isp_initiator_id;
3529                         cpi->base_transfer_speed = 3300;
3530                         cpi->transport = XPORT_SPI;
3531                         cpi->transport_version = 2;
3532                 }
3533                 cpi->protocol = PROTO_SCSI;
3534                 cpi->protocol_version = SCSI_REV_2;
3535                 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3536                 strlcpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
3537                 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3538                 cpi->unit_number = cam_sim_unit(sim);
3539                 cpi->ccb_h.status = CAM_REQ_CMP;
3540                 xpt_done(ccb);
3541                 break;
3542         }
3543         default:
3544                 ccb->ccb_h.status = CAM_REQ_INVALID;
3545                 xpt_done(ccb);
3546                 break;
3547         }
3548 }
3549
3550 void
3551 isp_done(XS_T *sccb)
3552 {
3553         ispsoftc_t *isp = XS_ISP(sccb);
3554         uint32_t status;
3555
3556         if (XS_NOERR(sccb))
3557                 XS_SETERR(sccb, CAM_REQ_CMP);
3558
3559         if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
3560                 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
3561                 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
3562                         sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
3563                 } else {
3564                         sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
3565                 }
3566         }
3567
3568         sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3569         status = sccb->ccb_h.status & CAM_STATUS_MASK;
3570         if (status != CAM_REQ_CMP &&
3571             (sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
3572                 sccb->ccb_h.status |= CAM_DEV_QFRZN;
3573                 xpt_freeze_devq(sccb->ccb_h.path, 1);
3574         }
3575
3576         if (ISP_PCMD(sccb)) {
3577                 if (callout_active(&PISP_PCMD(sccb)->wdog))
3578                         callout_stop(&PISP_PCMD(sccb)->wdog);
3579                 isp_free_pcmd(isp, (union ccb *) sccb);
3580         }
3581         xpt_done((union ccb *) sccb);
3582 }
3583
3584 void
3585 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
3586 {
3587         int bus;
3588         static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
3589         char buf[64];
3590         char *msg = NULL;
3591         target_id_t tgt;
3592         fcportdb_t *lp;
3593         struct isp_fc *fc;
3594         struct cam_path *tmppath;
3595         struct ac_contract ac;
3596         struct ac_device_changed *adc;
3597         va_list ap;
3598
3599         switch (cmd) {
3600         case ISPASYNC_NEW_TGT_PARAMS:
3601         {
3602                 struct ccb_trans_settings_scsi *scsi;
3603                 struct ccb_trans_settings_spi *spi;
3604                 int flags, tgt;
3605                 sdparam *sdp;
3606                 struct ccb_trans_settings cts;
3607
3608                 memset(&cts, 0, sizeof (struct ccb_trans_settings));
3609
3610                 va_start(ap, cmd);
3611                 bus = va_arg(ap, int);
3612                 tgt = va_arg(ap, int);
3613                 va_end(ap);
3614                 sdp = SDPARAM(isp, bus);
3615
3616                 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3617                         isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
3618                         break;
3619                 }
3620                 flags = sdp->isp_devparam[tgt].actv_flags;
3621                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
3622                 cts.protocol = PROTO_SCSI;
3623                 cts.transport = XPORT_SPI;
3624
3625                 scsi = &cts.proto_specific.scsi;
3626                 spi = &cts.xport_specific.spi;
3627
3628                 if (flags & DPARM_TQING) {
3629                         scsi->valid |= CTS_SCSI_VALID_TQ;
3630                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3631                 }
3632
3633                 if (flags & DPARM_DISC) {
3634                         spi->valid |= CTS_SPI_VALID_DISC;
3635                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3636                 }
3637                 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3638                 if (flags & DPARM_WIDE) {
3639                         spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3640                 } else {
3641                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3642                 }
3643                 if (flags & DPARM_SYNC) {
3644                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3645                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3646                         spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3647                         spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3648                 }
3649                 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);
3650                 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3651                 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3652                 xpt_free_path(tmppath);
3653                 break;
3654         }
3655         case ISPASYNC_BUS_RESET:
3656         {
3657                 va_start(ap, cmd);
3658                 bus = va_arg(ap, int);
3659                 va_end(ap);
3660                 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
3661                 if (IS_FC(isp)) {
3662                         xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
3663                 } else {
3664                         xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
3665                 }
3666                 break;
3667         }
3668         case ISPASYNC_LIP:
3669                 if (msg == NULL)
3670                         msg = "LIP Received";
3671                 /* FALLTHROUGH */
3672         case ISPASYNC_LOOP_RESET:
3673                 if (msg == NULL)
3674                         msg = "LOOP Reset";
3675                 /* FALLTHROUGH */
3676         case ISPASYNC_LOOP_DOWN:
3677                 if (msg == NULL)
3678                         msg = "LOOP Down";
3679                 va_start(ap, cmd);
3680                 bus = va_arg(ap, int);
3681                 va_end(ap);
3682                 isp_fcp_reset_crn(isp, bus, /*tgt*/0, /*tgt_set*/ 0);
3683                 isp_loop_changed(isp, bus);
3684                 isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3685                 break;
3686         case ISPASYNC_LOOP_UP:
3687                 va_start(ap, cmd);
3688                 bus = va_arg(ap, int);
3689                 va_end(ap);
3690                 isp_loop_changed(isp, bus);
3691                 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
3692                 break;
3693         case ISPASYNC_DEV_ARRIVED:
3694                 va_start(ap, cmd);
3695                 bus = va_arg(ap, int);
3696                 lp = va_arg(ap, fcportdb_t *);
3697                 va_end(ap);
3698                 fc = ISP_FC_PC(isp, bus);
3699                 tgt = FC_PORTDB_TGT(isp, bus, lp);
3700                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3701                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
3702                 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3703                     (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
3704                         lp->is_target = 1;
3705                         isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3706                         isp_make_here(isp, lp, bus, tgt);
3707                 }
3708                 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3709                     (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
3710                         lp->is_initiator = 1;
3711                         ac.contract_number = AC_CONTRACT_DEV_CHG;
3712                         adc = (struct ac_device_changed *) ac.contract_data;
3713                         adc->wwpn = lp->port_wwn;
3714                         adc->port = lp->portid;
3715                         adc->target = tgt;
3716                         adc->arrived = 1;
3717                         xpt_async(AC_CONTRACT, fc->path, &ac);
3718                 }
3719                 break;
3720         case ISPASYNC_DEV_CHANGED:
3721                 va_start(ap, cmd);
3722                 bus = va_arg(ap, int);
3723                 lp = va_arg(ap, fcportdb_t *);
3724                 va_end(ap);
3725                 fc = ISP_FC_PC(isp, bus);
3726                 tgt = FC_PORTDB_TGT(isp, bus, lp);
3727                 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
3728                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
3729 changed:
3730                 if (lp->is_target !=
3731                     ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
3732                      (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
3733                         lp->is_target = !lp->is_target;
3734                         if (lp->is_target) {
3735                                 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3736                                 isp_make_here(isp, lp, bus, tgt);
3737                         } else {
3738                                 isp_make_gone(isp, lp, bus, tgt);
3739                                 isp_fcp_reset_crn(isp, bus, tgt, /*tgt_set*/ 1);
3740                         }
3741                 }
3742                 if (lp->is_initiator !=
3743                     ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
3744                      (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
3745                         lp->is_initiator = !lp->is_initiator;
3746                         ac.contract_number = AC_CONTRACT_DEV_CHG;
3747                         adc = (struct ac_device_changed *) ac.contract_data;
3748                         adc->wwpn = lp->port_wwn;
3749                         adc->port = lp->portid;
3750                         adc->target = tgt;
3751                         adc->arrived = lp->is_initiator;
3752                         xpt_async(AC_CONTRACT, fc->path, &ac);
3753                 }
3754                 break;
3755         case ISPASYNC_DEV_STAYED:
3756                 va_start(ap, cmd);
3757                 bus = va_arg(ap, int);
3758                 lp = va_arg(ap, fcportdb_t *);
3759                 va_end(ap);
3760                 fc = ISP_FC_PC(isp, bus);
3761                 tgt = FC_PORTDB_TGT(isp, bus, lp);
3762                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3763                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
3764                 goto changed;
3765         case ISPASYNC_DEV_GONE:
3766                 va_start(ap, cmd);
3767                 bus = va_arg(ap, int);
3768                 lp = va_arg(ap, fcportdb_t *);
3769                 va_end(ap);
3770                 fc = ISP_FC_PC(isp, bus);
3771                 tgt = FC_PORTDB_TGT(isp, bus, lp);
3772                 /*
3773                  * If this has a virtual target or initiator set the isp_gdt
3774                  * timer running on it to delay its departure.
3775                  */
3776                 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
3777                 if (lp->is_target || lp->is_initiator) {
3778                         lp->state = FC_PORTDB_STATE_ZOMBIE;
3779                         lp->gone_timer = fc->gone_device_time;
3780                         isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
3781                         if (fc->ready && !callout_active(&fc->gdt)) {
3782                                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
3783                                 callout_reset(&fc->gdt, hz, isp_gdt, fc);
3784                         }
3785                         break;
3786                 }
3787                 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
3788                 break;
3789         case ISPASYNC_CHANGE_NOTIFY:
3790         {
3791                 char *msg;
3792                 int evt, nphdl, nlstate, portid, reason;
3793
3794                 va_start(ap, cmd);
3795                 bus = va_arg(ap, int);
3796                 evt = va_arg(ap, int);
3797                 if (evt == ISPASYNC_CHANGE_PDB) {
3798                         nphdl = va_arg(ap, int);
3799                         nlstate = va_arg(ap, int);
3800                         reason = va_arg(ap, int);
3801                 } else if (evt == ISPASYNC_CHANGE_SNS) {
3802                         portid = va_arg(ap, int);
3803                 } else {
3804                         nphdl = NIL_HANDLE;
3805                         nlstate = reason = 0;
3806                 }
3807                 va_end(ap);
3808                 fc = ISP_FC_PC(isp, bus);
3809
3810                 if (evt == ISPASYNC_CHANGE_PDB) {
3811                         msg = "Port Database Changed";
3812                         isp_prt(isp, ISP_LOGINFO,
3813                             "Chan %d %s (nphdl 0x%x state 0x%x reason 0x%x)",
3814                             bus, msg, nphdl, nlstate, reason);
3815                 } else if (evt == ISPASYNC_CHANGE_SNS) {
3816                         msg = "Name Server Database Changed";
3817                         isp_prt(isp, ISP_LOGINFO, "Chan %d %s (PortID 0x%06x)",
3818                             bus, msg, portid);
3819                 } else {
3820                         msg = "Other Change Notify";
3821                         isp_prt(isp, ISP_LOGINFO, "Chan %d %s", bus, msg);
3822                 }
3823                 isp_loop_changed(isp, bus);
3824                 break;
3825         }
3826 #ifdef  ISP_TARGET_MODE
3827         case ISPASYNC_TARGET_NOTIFY:
3828         {
3829                 isp_notify_t *notify;
3830                 va_start(ap, cmd);
3831                 notify = va_arg(ap, isp_notify_t *);
3832                 va_end(ap);
3833                 switch (notify->nt_ncode) {
3834                 case NT_ABORT_TASK:
3835                 case NT_ABORT_TASK_SET:
3836                 case NT_CLEAR_ACA:
3837                 case NT_CLEAR_TASK_SET:
3838                 case NT_LUN_RESET:
3839                 case NT_TARGET_RESET:
3840                 case NT_QUERY_TASK_SET:
3841                 case NT_QUERY_ASYNC_EVENT:
3842                         /*
3843                          * These are task management functions.
3844                          */
3845                         isp_handle_platform_target_tmf(isp, notify);
3846                         break;
3847                 case NT_BUS_RESET:
3848                 case NT_LIP_RESET:
3849                 case NT_LINK_UP:
3850                 case NT_LINK_DOWN:
3851                 case NT_HBA_RESET:
3852                         /*
3853                          * No action need be taken here.
3854                          */
3855                         break;
3856                 case NT_GLOBAL_LOGOUT:
3857                 case NT_LOGOUT:
3858                         /*
3859                          * This is device arrival/departure notification
3860                          */
3861                         isp_handle_platform_target_notify_ack(isp, notify, 0);
3862                         break;
3863                 case NT_SRR:
3864                         isp_handle_platform_srr(isp, notify);
3865                         break;
3866                 default:
3867                         isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
3868                         isp_handle_platform_target_notify_ack(isp, notify, 0);
3869                         break;
3870                 }
3871                 break;
3872         }
3873         case ISPASYNC_TARGET_NOTIFY_ACK:
3874         {
3875                 void *inot;
3876                 va_start(ap, cmd);
3877                 inot = va_arg(ap, void *);
3878                 va_end(ap);
3879                 if (isp_notify_ack(isp, inot)) {
3880                         isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
3881                         if (tp) {
3882                                 tp->isp = isp;
3883                                 memcpy(tp->data, inot, sizeof (tp->data));
3884                                 tp->not = tp->data;
3885                                 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
3886                                 callout_reset(&tp->timer, 5,
3887                                     isp_refire_notify_ack, tp);
3888                         } else {
3889                                 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
3890                         }
3891                 }
3892                 break;
3893         }
3894         case ISPASYNC_TARGET_ACTION:
3895         {
3896                 isphdr_t *hp;
3897
3898                 va_start(ap, cmd);
3899                 hp = va_arg(ap, isphdr_t *);
3900                 va_end(ap);
3901                 switch (hp->rqs_entry_type) {
3902                 case RQSTYPE_ATIO:
3903                         isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
3904                         break;
3905                 case RQSTYPE_ATIO2:
3906                         isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
3907                         break;
3908                 case RQSTYPE_CTIO7:
3909                 case RQSTYPE_CTIO3:
3910                 case RQSTYPE_CTIO2:
3911                 case RQSTYPE_CTIO:
3912                         isp_handle_platform_ctio(isp, hp);
3913                         break;
3914                 default:
3915                         isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x",
3916                             __func__, hp->rqs_entry_type);
3917                         break;
3918                 }
3919                 break;
3920         }
3921 #endif
3922         case ISPASYNC_FW_CRASH:
3923         {
3924                 uint16_t mbox1, mbox6;
3925                 mbox1 = ISP_READ(isp, OUTMAILBOX1);
3926                 if (IS_DUALBUS(isp)) { 
3927                         mbox6 = ISP_READ(isp, OUTMAILBOX6);
3928                 } else {
3929                         mbox6 = 0;
3930                 }
3931                 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
3932 #if 0
3933                 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
3934                 isp->isp_osinfo.mbox_sleep_ok = 0;
3935                 isp_reinit(isp, 1);
3936                 isp->isp_osinfo.mbox_sleep_ok = mbox1;
3937                 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3938 #endif
3939                 break;
3940         }
3941         default:
3942                 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3943                 break;
3944         }
3945 }
3946
3947 uint64_t
3948 isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
3949 {
3950         uint64_t seed;
3951         struct isp_fc *fc = ISP_FC_PC(isp, chan);
3952
3953         /* First try to use explicitly configured WWNs. */
3954         seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
3955         if (seed)
3956                 return (seed);
3957
3958         /* Otherwise try to use WWNs from NVRAM. */
3959         if (isactive) {
3960                 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram :
3961                     FCPARAM(isp, chan)->isp_wwpn_nvram;
3962                 if (seed)
3963                         return (seed);
3964         }
3965
3966         /* If still no WWNs, try to steal them from the first channel. */
3967         if (chan > 0) {
3968                 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn :
3969                     ISP_FC_PC(isp, 0)->def_wwpn;
3970                 if (seed == 0) {
3971                         seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram :
3972                             FCPARAM(isp, 0)->isp_wwpn_nvram;
3973                 }
3974         }
3975
3976         /* If still nothing -- improvise. */
3977         if (seed == 0) {
3978                 seed = 0x400000007F000000ull + device_get_unit(isp->isp_dev);
3979                 if (!iswwnn)
3980                         seed ^= 0x0100000000000000ULL;
3981         }
3982
3983         /* For additional channels we have to improvise even more. */
3984         if (!iswwnn && chan > 0) {
3985                 /*
3986                  * We'll stick our channel number plus one first into bits
3987                  * 57..59 and thence into bits 52..55 which allows for 8 bits
3988                  * of channel which is enough for our maximum of 255 channels.
3989                  */
3990                 seed ^= 0x0100000000000000ULL;
3991                 seed ^= ((uint64_t) (chan + 1) & 0xf) << 56;
3992                 seed ^= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
3993         }
3994         return (seed);
3995 }
3996
3997 void
3998 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3999 {
4000         int loc;
4001         char lbuf[200];
4002         va_list ap;
4003
4004         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4005                 return;
4006         }
4007         snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
4008         loc = strlen(lbuf);
4009         va_start(ap, fmt);
4010         vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap); 
4011         va_end(ap);
4012         printf("%s\n", lbuf);
4013 }
4014
4015 void
4016 isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
4017 {
4018         va_list ap;
4019         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
4020                 return;
4021         }
4022         xpt_print_path(xs->ccb_h.path);
4023         va_start(ap, fmt);
4024         vprintf(fmt, ap);
4025         va_end(ap);
4026         printf("\n");
4027 }
4028
4029 uint64_t
4030 isp_nanotime_sub(struct timespec *b, struct timespec *a)
4031 {
4032         uint64_t elapsed;
4033         struct timespec x = *b;
4034         timespecsub(&x, a);
4035         elapsed = GET_NANOSEC(&x);
4036         if (elapsed == 0)
4037                 elapsed++;
4038         return (elapsed);
4039 }
4040
4041 int
4042 isp_mbox_acquire(ispsoftc_t *isp)
4043 {
4044         if (isp->isp_osinfo.mboxbsy) {
4045                 return (1);
4046         } else {
4047                 isp->isp_osinfo.mboxcmd_done = 0;
4048                 isp->isp_osinfo.mboxbsy = 1;
4049                 return (0);
4050         }
4051 }
4052
4053 void
4054 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
4055 {
4056         u_int t, to;
4057
4058         to = (mbp->timeout == 0) ? MBCMD_DEFAULT_TIMEOUT : mbp->timeout;
4059         if (isp->isp_osinfo.mbox_sleep_ok) {
4060                 isp->isp_osinfo.mbox_sleep_ok = 0;
4061                 isp->isp_osinfo.mbox_sleeping = 1;
4062                 msleep_sbt(&isp->isp_osinfo.mboxcmd_done, &isp->isp_osinfo.lock,
4063                     PRIBIO, "ispmbx_sleep", to * SBT_1US, 0, 0);
4064                 isp->isp_osinfo.mbox_sleep_ok = 1;
4065                 isp->isp_osinfo.mbox_sleeping = 0;
4066         } else {
4067                 for (t = 0; t < to; t += 100) {
4068                         if (isp->isp_osinfo.mboxcmd_done)
4069                                 break;
4070                         ISP_RUN_ISR(isp);
4071                         if (isp->isp_osinfo.mboxcmd_done)
4072                                 break;
4073                         ISP_DELAY(100);
4074                 }
4075         }
4076         if (isp->isp_osinfo.mboxcmd_done == 0) {
4077                 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (%s:%d)",
4078                     isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
4079                     isp->isp_lastmbxcmd, to, mbp->func, mbp->lineno);
4080                 mbp->param[0] = MBOX_TIMEOUT;
4081                 isp->isp_osinfo.mboxcmd_done = 1;
4082         }
4083 }
4084
4085 void
4086 isp_mbox_notify_done(ispsoftc_t *isp)
4087 {
4088         isp->isp_osinfo.mboxcmd_done = 1;
4089         if (isp->isp_osinfo.mbox_sleeping)
4090                 wakeup(&isp->isp_osinfo.mboxcmd_done);
4091 }
4092
4093 void
4094 isp_mbox_release(ispsoftc_t *isp)
4095 {
4096         isp->isp_osinfo.mboxbsy = 0;
4097 }
4098
4099 int
4100 isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
4101 {
4102         int ret = 0;
4103         if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
4104                 ret = -1;
4105         } else {
4106                 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
4107         }
4108         return (ret);
4109 }
4110
4111 int
4112 isp_mstohz(int ms)
4113 {
4114         int hz;
4115         struct timeval t;
4116         t.tv_sec = ms / 1000;
4117         t.tv_usec = (ms % 1000) * 1000;
4118         hz = tvtohz(&t);
4119         if (hz < 0) {
4120                 hz = 0x7fffffff;
4121         }
4122         if (hz == 0) {
4123                 hz = 1;
4124         }
4125         return (hz);
4126 }
4127
4128 void
4129 isp_platform_intr(void *arg)
4130 {
4131         ispsoftc_t *isp = arg;
4132
4133         ISP_LOCK(isp);
4134         ISP_RUN_ISR(isp);
4135         ISP_UNLOCK(isp);
4136 }
4137
4138 void
4139 isp_platform_intr_resp(void *arg)
4140 {
4141         ispsoftc_t *isp = arg;
4142
4143         ISP_LOCK(isp);
4144         isp_intr_respq(isp);
4145         ISP_UNLOCK(isp);
4146
4147         /* We have handshake enabled, so explicitly complete interrupt */
4148         ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4149 }
4150
4151 void
4152 isp_platform_intr_atio(void *arg)
4153 {
4154         ispsoftc_t *isp = arg;
4155
4156         ISP_LOCK(isp);
4157 #ifdef  ISP_TARGET_MODE
4158         isp_intr_atioq(isp);
4159 #endif
4160         ISP_UNLOCK(isp);
4161
4162         /* We have handshake enabled, so explicitly complete interrupt */
4163         ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
4164 }
4165
4166 void
4167 isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
4168 {
4169         if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4170                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
4171         } else {
4172                 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
4173         }
4174         bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
4175 }
4176
4177 /*
4178  * Reset the command reference number for all LUNs on a specific target
4179  * (needed when a target arrives again) or for all targets on a port
4180  * (needed for events like a LIP).
4181  */
4182 void
4183 isp_fcp_reset_crn(ispsoftc_t *isp, int chan, uint32_t tgt, int tgt_set)
4184 {
4185         struct isp_fc *fc = ISP_FC_PC(isp, chan);
4186         struct isp_nexus *nxp;
4187         int i;
4188
4189         if (tgt_set == 0)
4190                 isp_prt(isp, ISP_LOGDEBUG0,
4191                     "Chan %d resetting CRN on all targets", chan);
4192         else
4193                 isp_prt(isp, ISP_LOGDEBUG0,
4194                     "Chan %d resetting CRN on target %u", chan, tgt);
4195
4196         for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
4197                 for (nxp = fc->nexus_hash[i]; nxp != NULL; nxp = nxp->next) {
4198                         if (tgt_set == 0 || tgt == nxp->tgt)
4199                                 nxp->crnseed = 0;
4200                 }
4201         }
4202 }
4203
4204 int
4205 isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
4206 {
4207         lun_id_t lun;
4208         uint32_t chan, tgt;
4209         struct isp_fc *fc;
4210         struct isp_nexus *nxp;
4211         int idx;
4212
4213         if (IS_2100(isp))
4214                 return (0);
4215
4216         chan = XS_CHANNEL(cmd);
4217         tgt = XS_TGT(cmd);
4218         lun = XS_LUN(cmd);
4219         fc = &isp->isp_osinfo.pc.fc[chan];
4220         idx = NEXUS_HASH(tgt, lun);
4221         nxp = fc->nexus_hash[idx];
4222
4223         while (nxp) {
4224                 if (nxp->tgt == tgt && nxp->lun == lun)
4225                         break;
4226                 nxp = nxp->next;
4227         }
4228         if (nxp == NULL) {
4229                 nxp = fc->nexus_free_list;
4230                 if (nxp == NULL) {
4231                         nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
4232                         if (nxp == NULL) {
4233                                 return (-1);
4234                         }
4235                 } else {
4236                         fc->nexus_free_list = nxp->next;
4237                 }
4238                 nxp->tgt = tgt;
4239                 nxp->lun = lun;
4240                 nxp->next = fc->nexus_hash[idx];
4241                 fc->nexus_hash[idx] = nxp;
4242         }
4243         if (nxp->crnseed == 0)
4244                 nxp->crnseed = 1;
4245         PISP_PCMD(cmd)->crn = nxp->crnseed;
4246         *crnp = nxp->crnseed++;
4247         return (0);
4248 }
4249
4250 /*
4251  * We enter with the lock held
4252  */
4253 void
4254 isp_timer(void *arg)
4255 {
4256         ispsoftc_t *isp = arg;
4257 #ifdef  ISP_TARGET_MODE
4258         isp_tmcmd_restart(isp);
4259 #endif
4260         callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
4261 }
4262
4263 isp_ecmd_t *
4264 isp_get_ecmd(ispsoftc_t *isp)
4265 {
4266         isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
4267         if (ecmd) {
4268                 isp->isp_osinfo.ecmd_free = ecmd->next;
4269         }
4270         return (ecmd);
4271 }
4272
4273 void
4274 isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
4275 {
4276         ecmd->next = isp->isp_osinfo.ecmd_free;
4277         isp->isp_osinfo.ecmd_free = ecmd;
4278 }