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