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