]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/isp/isp_freebsd.c
This commit was generated by cvs2svn to compensate for changes in r165182,
[FreeBSD/FreeBSD.git] / sys / dev / isp / isp_freebsd.c
1 /*-
2  *
3  * Copyright (c) 1997-2006 by Matthew Jacob
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
30  */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <dev/isp/isp_freebsd.h>
34 #include <sys/unistd.h>
35 #include <sys/kthread.h>
36 #include <machine/stdarg.h>     /* for use by isp_prt below */
37 #include <sys/conf.h>
38 #include <sys/module.h>
39 #include <sys/ioccom.h>
40 #include <dev/isp/isp_ioctl.h>
41 #if     __FreeBSD_version >= 500000
42 #include <sys/sysctl.h>
43 #endif
44 #include <cam/cam_periph.h>
45
46
47 MODULE_VERSION(isp, 1);
48 MODULE_DEPEND(isp, cam, 1, 1, 1);
49 int isp_announced = 0;
50 int isp_fabric_hysteresis = 5;
51 int isp_loop_down_limit = 300;  /* default loop down limit */
52 int isp_change_is_bad = 0;      /* "changed" devices are bad */
53 int isp_quickboot_time = 15;    /* don't wait more than N secs for loop up */
54 int isp_gone_device_time = 30;  /* grace time before reporting device lost */
55 static const char *roles[4] = {
56     "(none)", "Target", "Initiator", "Target/Initiator"
57 };
58 static const char prom3[] =
59     "PortID 0x%06x Departed from Target %u because of %s";
60
61 static void isp_freeze_loopdown(ispsoftc_t *, char *);
62 static d_ioctl_t ispioctl;
63 static void isp_intr_enable(void *);
64 static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
65 static void isp_poll(struct cam_sim *);
66 static timeout_t isp_watchdog;
67 static timeout_t isp_ldt;
68 static void isp_kthread(void *);
69 static void isp_action(struct cam_sim *, union ccb *);
70
71 #if __FreeBSD_version < 700000
72 ispfwfunc *isp_get_firmware_p = NULL;
73 #endif
74
75 #if __FreeBSD_version < 500000  
76 #define ISP_CDEV_MAJOR  248
77 static struct cdevsw isp_cdevsw = {
78         /* open */      nullopen,
79         /* close */     nullclose,
80         /* read */      noread,
81         /* write */     nowrite,
82         /* ioctl */     ispioctl,
83         /* poll */      nopoll,
84         /* mmap */      nommap,
85         /* strategy */  nostrategy,
86         /* name */      "isp",
87         /* maj */       ISP_CDEV_MAJOR,
88         /* dump */      nodump,
89         /* psize */     nopsize,
90         /* flags */     D_TAPE,
91 };
92 #define isp_sysctl_update(x)    do { ; } while (0)
93 #else
94 static struct cdevsw isp_cdevsw = {
95         .d_version =    D_VERSION,
96         .d_flags =      D_NEEDGIANT,
97         .d_ioctl =      ispioctl,
98         .d_name =       "isp",
99 };
100 static void isp_sysctl_update(ispsoftc_t *);
101 #endif
102
103 static ispsoftc_t *isplist = NULL;
104
105 void
106 isp_attach(ispsoftc_t *isp)
107 {
108         int primary, secondary;
109         struct ccb_setasync csa;
110         struct cam_devq *devq;
111         struct cam_sim *sim;
112         struct cam_path *path;
113
114         /*
115          * Establish (in case of 12X0) which bus is the primary.
116          */
117
118         primary = 0;
119         secondary = 1;
120
121         /*
122          * Create the device queue for our SIM(s).
123          */
124         devq = cam_simq_alloc(isp->isp_maxcmds);
125         if (devq == NULL) {
126                 return;
127         }
128
129         /*
130          * Construct our SIM entry.
131          */
132         ISPLOCK_2_CAMLOCK(isp);
133         sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
134             device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
135         if (sim == NULL) {
136                 cam_simq_free(devq);
137                 CAMLOCK_2_ISPLOCK(isp);
138                 return;
139         }
140         CAMLOCK_2_ISPLOCK(isp);
141
142         isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
143         isp->isp_osinfo.ehook.ich_arg = isp;
144         ISPLOCK_2_CAMLOCK(isp);
145         if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
146                 cam_sim_free(sim, TRUE);
147                 CAMLOCK_2_ISPLOCK(isp);
148                 isp_prt(isp, ISP_LOGERR,
149                     "could not establish interrupt enable hook");
150                 return;
151         }
152
153         if (xpt_bus_register(sim, primary) != CAM_SUCCESS) {
154                 cam_sim_free(sim, TRUE);
155                 CAMLOCK_2_ISPLOCK(isp);
156                 return;
157         }
158
159         if (xpt_create_path(&path, NULL, cam_sim_path(sim),
160             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
161                 xpt_bus_deregister(cam_sim_path(sim));
162                 cam_sim_free(sim, TRUE);
163                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
164                 CAMLOCK_2_ISPLOCK(isp);
165                 return;
166         }
167
168         xpt_setup_ccb(&csa.ccb_h, path, 5);
169         csa.ccb_h.func_code = XPT_SASYNC_CB;
170         csa.event_enable = AC_LOST_DEVICE;
171         csa.callback = isp_cam_async;
172         csa.callback_arg = sim;
173         xpt_action((union ccb *)&csa);
174         CAMLOCK_2_ISPLOCK(isp);
175         isp->isp_sim = sim;
176         isp->isp_path = path;
177         /*
178          * Create a kernel thread for fibre channel instances. We
179          * don't have dual channel FC cards.
180          */
181         if (IS_FC(isp)) {
182                 ISPLOCK_2_CAMLOCK(isp);
183 #if __FreeBSD_version >= 500000  
184                 cv_init(&isp->isp_osinfo.kthread_cv, "isp_kthread_cv");
185                 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
186                     RFHIGHPID, 0, "%s: fc_thrd",
187                     device_get_nameunit(isp->isp_dev)))
188 #else
189                 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kproc,
190                     "%s: fc_thrd", device_get_nameunit(isp->isp_dev)))
191 #endif
192                 {
193                         xpt_bus_deregister(cam_sim_path(sim));
194                         cam_sim_free(sim, TRUE);
195                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
196                         CAMLOCK_2_ISPLOCK(isp);
197                         isp_prt(isp, ISP_LOGERR, "could not create kthread");
198                         return;
199                 }
200                 CAMLOCK_2_ISPLOCK(isp);
201                 /*
202                  * We start by being "loop down" if we have an initiator role
203                  */
204                 if (isp->isp_role & ISP_ROLE_INITIATOR) {
205                         isp_freeze_loopdown(isp, "isp_attach");
206                         isp->isp_osinfo.ldt =
207                             timeout(isp_ldt, isp, isp_quickboot_time * hz);
208                         isp->isp_osinfo.ldt_running = 1;
209                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
210                            "Starting Initial Loop Down Timer");
211                 }
212         }
213
214
215         /*
216          * If we have a second channel, construct SIM entry for that.
217          */
218         if (IS_DUALBUS(isp)) {
219                 ISPLOCK_2_CAMLOCK(isp);
220                 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
221                     device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
222                 if (sim == NULL) {
223                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
224                         xpt_free_path(isp->isp_path);
225                         cam_simq_free(devq);
226                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
227                         return;
228                 }
229                 if (xpt_bus_register(sim, secondary) != CAM_SUCCESS) {
230                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
231                         xpt_free_path(isp->isp_path);
232                         cam_sim_free(sim, TRUE);
233                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
234                         CAMLOCK_2_ISPLOCK(isp);
235                         return;
236                 }
237
238                 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
239                     CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
240                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
241                         xpt_free_path(isp->isp_path);
242                         xpt_bus_deregister(cam_sim_path(sim));
243                         cam_sim_free(sim, TRUE);
244                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
245                         CAMLOCK_2_ISPLOCK(isp);
246                         return;
247                 }
248
249                 xpt_setup_ccb(&csa.ccb_h, path, 5);
250                 csa.ccb_h.func_code = XPT_SASYNC_CB;
251                 csa.event_enable = AC_LOST_DEVICE;
252                 csa.callback = isp_cam_async;
253                 csa.callback_arg = sim;
254                 xpt_action((union ccb *)&csa);
255                 CAMLOCK_2_ISPLOCK(isp);
256                 isp->isp_sim2 = sim;
257                 isp->isp_path2 = path;
258         }
259
260         /*
261          * Create device nodes
262          */
263         (void) make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
264             GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
265
266         if (isp->isp_role != ISP_ROLE_NONE) {
267                 isp->isp_state = ISP_RUNSTATE;
268                 ISP_ENABLE_INTS(isp);
269         }
270         if (isplist == NULL) {
271                 isplist = isp;
272         } else {
273                 ispsoftc_t *tmp = isplist;
274                 while (tmp->isp_osinfo.next) {
275                         tmp = tmp->isp_osinfo.next;
276                 }
277                 tmp->isp_osinfo.next = isp;
278         }
279         isp_sysctl_update(isp);
280 }
281
282 static void
283 isp_freeze_loopdown(ispsoftc_t *isp, char *msg)
284 {
285         if (isp->isp_osinfo.simqfrozen == 0) {
286                 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown)", msg);
287                 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
288                 ISPLOCK_2_CAMLOCK(isp);
289                 xpt_freeze_simq(isp->isp_sim, 1);
290                 CAMLOCK_2_ISPLOCK(isp);
291         } else {
292                 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown)", msg);
293                 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
294         }
295 }
296
297
298 #if __FreeBSD_version < 500000  
299 #define _DEV    dev_t
300 #define _IOP    struct proc
301 #else
302 #define _IOP    struct thread
303 #define _DEV    struct cdev *
304 #endif
305
306 static int
307 ispioctl(_DEV dev, u_long c, caddr_t addr, int flags, _IOP *td)
308 {
309         ispsoftc_t *isp;
310         int nr, retval = ENOTTY;
311
312         isp = isplist;
313         while (isp) {
314                 if (minor(dev) == device_get_unit(isp->isp_dev)) {
315                         break;
316                 }
317                 isp = isp->isp_osinfo.next;
318         }
319         if (isp == NULL)
320                 return (ENXIO);
321         
322         switch (c) {
323 #ifdef  ISP_FW_CRASH_DUMP
324         case ISP_GET_FW_CRASH_DUMP:
325                 if (IS_FC(isp)) {
326                         uint16_t *ptr = FCPARAM(isp)->isp_dump_data;
327                         size_t sz;
328
329                         retval = 0;
330                         if (IS_2200(isp)) {
331                                 sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
332                         } else {
333                                 sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
334                         }
335                         ISP_LOCK(isp);
336                         if (ptr && *ptr) {
337                                 void *uaddr = *((void **) addr);
338                                 if (copyout(ptr, uaddr, sz)) {
339                                         retval = EFAULT;
340                                 } else {
341                                         *ptr = 0;
342                                 }
343                         } else {
344                                 retval = ENXIO;
345                         }
346                         ISP_UNLOCK(isp);
347                 }
348                 break;
349         case ISP_FORCE_CRASH_DUMP:
350                 if (IS_FC(isp)) {
351                         ISP_LOCK(isp);
352                         isp_freeze_loopdown(isp,
353                             "ispioctl(ISP_FORCE_CRASH_DUMP)");
354                         isp_fw_dump(isp);
355                         isp_reinit(isp);
356                         ISP_UNLOCK(isp);
357                         retval = 0;
358                 }
359                 break;
360 #endif
361         case ISP_SDBLEV:
362         {
363                 int olddblev = isp->isp_dblev;
364                 isp->isp_dblev = *(int *)addr;
365                 *(int *)addr = olddblev;
366                 retval = 0;
367                 break;
368         }
369         case ISP_GETROLE:
370                 *(int *)addr = isp->isp_role;
371                 retval = 0;
372                 break;
373         case ISP_SETROLE:
374                 nr = *(int *)addr;
375                 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
376                         retval = EINVAL;
377                         break;
378                 }
379                 *(int *)addr = isp->isp_role;
380                 isp->isp_role = nr;
381                 /* FALLTHROUGH */
382         case ISP_RESETHBA:
383                 ISP_LOCK(isp);
384                 isp_reinit(isp);
385                 ISP_UNLOCK(isp);
386                 retval = 0;
387                 break;
388         case ISP_RESCAN:
389                 if (IS_FC(isp)) {
390                         ISP_LOCK(isp);
391                         if (isp_fc_runstate(isp, 5 * 1000000)) {
392                                 retval = EIO;
393                         } else {
394                                 retval = 0;
395                         }
396                         ISP_UNLOCK(isp);
397                 }
398                 break;
399         case ISP_FC_LIP:
400                 if (IS_FC(isp)) {
401                         ISP_LOCK(isp);
402                         if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
403                                 retval = EIO;
404                         } else {
405                                 retval = 0;
406                         }
407                         ISP_UNLOCK(isp);
408                 }
409                 break;
410         case ISP_FC_GETDINFO:
411         {
412                 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
413                 fcportdb_t *lp;
414
415                 if (IS_SCSI(isp)) {
416                         break;
417                 }
418                 if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
419                         retval = EINVAL;
420                         break;
421                 }
422                 ISP_LOCK(isp);
423                 lp = &FCPARAM(isp)->portdb[ifc->loopid];
424                 if (lp->state == FC_PORTDB_STATE_VALID) {
425                         ifc->role = lp->roles;
426                         ifc->loopid = lp->handle;
427                         ifc->portid = lp->portid;
428                         ifc->node_wwn = lp->node_wwn;
429                         ifc->port_wwn = lp->port_wwn;
430                         retval = 0;
431                 } else {
432                         retval = ENODEV;
433                 }
434                 ISP_UNLOCK(isp);
435                 break;
436         }
437         case ISP_GET_STATS:
438         {
439                 isp_stats_t *sp = (isp_stats_t *) addr;
440
441                 MEMZERO(sp, sizeof (*sp));
442                 sp->isp_stat_version = ISP_STATS_VERSION;
443                 sp->isp_type = isp->isp_type;
444                 sp->isp_revision = isp->isp_revision;
445                 ISP_LOCK(isp);
446                 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
447                 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
448                 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
449                 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
450                 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
451                 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
452                 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
453                 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
454                 ISP_UNLOCK(isp);
455                 retval = 0;
456                 break;
457         }
458         case ISP_CLR_STATS:
459                 ISP_LOCK(isp);
460                 isp->isp_intcnt = 0;
461                 isp->isp_intbogus = 0;
462                 isp->isp_intmboxc = 0;
463                 isp->isp_intoasync = 0;
464                 isp->isp_rsltccmplt = 0;
465                 isp->isp_fphccmplt = 0;
466                 isp->isp_rscchiwater = 0;
467                 isp->isp_fpcchiwater = 0;
468                 ISP_UNLOCK(isp);
469                 retval = 0;
470                 break;
471         case ISP_FC_GETHINFO:
472         {
473                 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
474                 MEMZERO(hba, sizeof (*hba));
475
476                 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
477                 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
478                 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
479                 if (IS_FC(isp)) {
480                         hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
481                         hba->fc_scsi_supported = 1;
482                         hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
483                         hba->fc_loopid = FCPARAM(isp)->isp_loopid;
484                         hba->nvram_node_wwn = FCPARAM(isp)->isp_nodewwn;
485                         hba->nvram_port_wwn = FCPARAM(isp)->isp_portwwn;
486                         hba->active_node_wwn = ISP_NODEWWN(isp);
487                         hba->active_port_wwn = ISP_PORTWWN(isp);
488                 }
489                 retval = 0;
490                 break;
491         }
492         case ISP_GET_FC_PARAM:
493         {
494                 struct isp_fc_param *f = (struct isp_fc_param *) addr;
495
496                 if (IS_SCSI(isp)) {
497                         break;
498                 }
499                 f->parameter = 0;
500                 if (strcmp(f->param_name, "framelength") == 0) {
501                         f->parameter = FCPARAM(isp)->isp_maxfrmlen;
502                         retval = 0;
503                         break;
504                 }
505                 if (strcmp(f->param_name, "exec_throttle") == 0) {
506                         f->parameter = FCPARAM(isp)->isp_execthrottle;
507                         retval = 0;
508                         break;
509                 }
510                 if (strcmp(f->param_name, "fullduplex") == 0) {
511                         if (FCPARAM(isp)->isp_fwoptions & ICBOPT_FULL_DUPLEX)
512                                 f->parameter = 1;
513                         retval = 0;
514                         break;
515                 }
516                 if (strcmp(f->param_name, "loopid") == 0) {
517                         f->parameter = FCPARAM(isp)->isp_loopid;
518                         retval = 0;
519                         break;
520                 }
521                 retval = EINVAL;
522                 break;
523         }
524         case ISP_SET_FC_PARAM:
525         {
526                 struct isp_fc_param *f = (struct isp_fc_param *) addr;
527                 uint32_t param = f->parameter;
528
529                 if (IS_SCSI(isp)) {
530                         break;
531                 }
532                 f->parameter = 0;
533                 if (strcmp(f->param_name, "framelength") == 0) {
534                         if (param != 512 && param != 1024 && param != 1024) {
535                                 retval = EINVAL;
536                                 break;
537                         }
538                         FCPARAM(isp)->isp_maxfrmlen = param;
539                         retval = 0;
540                         break;
541                 }
542                 if (strcmp(f->param_name, "exec_throttle") == 0) {
543                         if (param < 16 || param > 255) {
544                                 retval = EINVAL;
545                                 break;
546                         }
547                         FCPARAM(isp)->isp_execthrottle = param;
548                         retval = 0;
549                         break;
550                 }
551                 if (strcmp(f->param_name, "fullduplex") == 0) {
552                         if (param != 0 && param != 1) {
553                                 retval = EINVAL;
554                                 break;
555                         }
556                         if (param) {
557                                 FCPARAM(isp)->isp_fwoptions |=
558                                     ICBOPT_FULL_DUPLEX;
559                         } else {
560                                 FCPARAM(isp)->isp_fwoptions &=
561                                     ~ICBOPT_FULL_DUPLEX;
562                         }
563                         retval = 0;
564                         break;
565                 }
566                 if (strcmp(f->param_name, "loopid") == 0) {
567                         if (param < 0 || param > 125) {
568                                 retval = EINVAL;
569                                 break;
570                         }
571                         FCPARAM(isp)->isp_loopid = param;
572                         retval = 0;
573                         break;
574                 }
575                 retval = EINVAL;
576                 break;
577         }
578         case ISP_TSK_MGMT:
579         {
580                 int needmarker;
581                 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
582                 uint16_t loopid;
583                 mbreg_t mbs;
584
585                 if (IS_SCSI(isp)) {
586                         break;
587                 }
588
589                 memset(&mbs, 0, sizeof (mbs));
590                 needmarker = retval = 0;
591                 loopid = fct->loopid;
592                 if (FCPARAM(isp)->isp_2klogin == 0) {
593                         loopid <<= 8;
594                 }
595                 switch (fct->action) {
596                 case IPT_CLEAR_ACA:
597                         mbs.param[0] = MBOX_CLEAR_ACA;
598                         mbs.param[1] = loopid;
599                         mbs.param[2] = fct->lun;
600                         break;
601                 case IPT_TARGET_RESET:
602                         mbs.param[0] = MBOX_TARGET_RESET;
603                         mbs.param[1] = loopid;
604                         needmarker = 1;
605                         break;
606                 case IPT_LUN_RESET:
607                         mbs.param[0] = MBOX_LUN_RESET;
608                         mbs.param[1] = loopid;
609                         mbs.param[2] = fct->lun;
610                         needmarker = 1;
611                         break;
612                 case IPT_CLEAR_TASK_SET:
613                         mbs.param[0] = MBOX_CLEAR_TASK_SET;
614                         mbs.param[1] = loopid;
615                         mbs.param[2] = fct->lun;
616                         needmarker = 1;
617                         break;
618                 case IPT_ABORT_TASK_SET:
619                         mbs.param[0] = MBOX_ABORT_TASK_SET;
620                         mbs.param[1] = loopid;
621                         mbs.param[2] = fct->lun;
622                         needmarker = 1;
623                         break;
624                 default:
625                         retval = EINVAL;
626                         break;
627                 }
628                 if (retval == 0) {
629                         ISP_LOCK(isp);
630                         if (needmarker) {
631                                 isp->isp_sendmarker |= 1;
632                         }
633                         retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
634                         ISP_UNLOCK(isp);
635                         if (retval)
636                                 retval = EIO;
637                 }
638                 break;
639         }
640         default:
641                 break;
642         }
643         return (retval);
644 }
645
646 #if __FreeBSD_version >= 500000
647 static void
648 isp_sysctl_update(ispsoftc_t *isp)
649 {
650         struct sysctl_ctx_list *ctx =
651             device_get_sysctl_ctx(isp->isp_osinfo.dev);
652         struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
653
654         if (IS_SCSI(isp)) {
655                 return;
656         }
657
658         snprintf(isp->isp_osinfo.sysctl_info.fc.wwnn,
659             sizeof (isp->isp_osinfo.sysctl_info.fc.wwnn), "0x%08x%08x",
660             (uint32_t) (ISP_NODEWWN(isp) >> 32), (uint32_t) ISP_NODEWWN(isp));
661
662         snprintf(isp->isp_osinfo.sysctl_info.fc.wwpn,
663             sizeof (isp->isp_osinfo.sysctl_info.fc.wwpn), "0x%08x%08x",
664             (uint32_t) (ISP_PORTWWN(isp) >> 32), (uint32_t) ISP_PORTWWN(isp));
665
666         SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
667                "wwnn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwnn, 0,
668                "World Wide Node Name");
669
670         SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
671                "wwpn", CTLFLAG_RD, isp->isp_osinfo.sysctl_info.fc.wwpn, 0,
672                "World Wide Port Name");
673
674         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
675             "loop_down_limit",
676             CTLFLAG_RW, &isp->isp_osinfo.loop_down_limit, 0,
677             "How long to wait for loop to come back up");
678
679         SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
680             "gone_device_time",
681             CTLFLAG_RW, &isp->isp_osinfo.gone_device_time, 0,
682             "How long to wait for a device to reappear");
683 }
684 #endif
685
686 static void
687 isp_intr_enable(void *arg)
688 {
689         ispsoftc_t *isp = arg;
690         if (isp->isp_role != ISP_ROLE_NONE) {
691                 ISP_ENABLE_INTS(isp);
692         }
693         /* Release our hook so that the boot can continue. */
694         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
695 }
696
697 /*
698  * Put the target mode functions here, because some are inlines
699  */
700
701 #ifdef  ISP_TARGET_MODE
702
703 static __inline int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
704 static __inline int are_any_luns_enabled(ispsoftc_t *, int);
705 static __inline tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
706 static __inline void rls_lun_statep(ispsoftc_t *, tstate_t *);
707 static __inline atio_private_data_t *isp_get_atpd(ispsoftc_t *, int);
708 static cam_status
709 create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
710 static void destroy_lun_state(ispsoftc_t *, tstate_t *);
711 static int isp_en_lun(ispsoftc_t *, union ccb *);
712 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
713 static cam_status isp_abort_tgt_ccb(ispsoftc_t *, union ccb *);
714 static timeout_t isp_refire_putback_atio;
715 static void isp_complete_ctio(union ccb *);
716 static void isp_target_putback_atio(union ccb *);
717 static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
718 static int isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
719 static int isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
720 static int isp_handle_platform_ctio(ispsoftc_t *, void *);
721 static int isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
722 static int isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
723
724 static __inline int
725 is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
726 {
727         tstate_t *tptr;
728         tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
729         if (tptr == NULL) {
730                 return (0);
731         }
732         do {
733                 if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
734                         return (1);
735                 }
736         } while ((tptr = tptr->next) != NULL);
737         return (0);
738 }
739
740 static __inline int
741 are_any_luns_enabled(ispsoftc_t *isp, int port)
742 {
743         int lo, hi;
744         if (IS_DUALBUS(isp)) {
745                 lo = (port * (LUN_HASH_SIZE >> 1));
746                 hi = lo + (LUN_HASH_SIZE >> 1);
747         } else {
748                 lo = 0;
749                 hi = LUN_HASH_SIZE;
750         }
751         for (lo = 0; lo < hi; lo++) {
752                 if (isp->isp_osinfo.lun_hash[lo]) {
753                         return (1);
754                 }
755         }
756         return (0);
757 }
758
759 static __inline tstate_t *
760 get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
761 {
762         tstate_t *tptr = NULL;
763
764         if (lun == CAM_LUN_WILDCARD) {
765                 if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
766                         tptr = &isp->isp_osinfo.tsdflt[bus];
767                         tptr->hold++;
768                         return (tptr);
769                 }
770                 return (NULL);
771         } else {
772                 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
773                 if (tptr == NULL) {
774                         return (NULL);
775                 }
776         }
777
778         do {
779                 if (tptr->lun == lun && tptr->bus == bus) {
780                         tptr->hold++;
781                         return (tptr);
782                 }
783         } while ((tptr = tptr->next) != NULL);
784         return (tptr);
785 }
786
787 static __inline void
788 rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
789 {
790         if (tptr->hold)
791                 tptr->hold--;
792 }
793
794 static __inline atio_private_data_t *
795 isp_get_atpd(ispsoftc_t *isp, int tag)
796 {
797         atio_private_data_t *atp;
798         for (atp = isp->isp_osinfo.atpdp;
799             atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
800                 if (atp->tag == tag)
801                         return (atp);
802         }
803         return (NULL);
804 }
805
806 static cam_status
807 create_lun_state(ispsoftc_t *isp, int bus,
808     struct cam_path *path, tstate_t **rslt)
809 {
810         cam_status status;
811         lun_id_t lun;
812         int hfx;
813         tstate_t *tptr, *new;
814
815         lun = xpt_path_lun_id(path);
816         if (lun < 0) {
817                 return (CAM_LUN_INVALID);
818         }
819         if (is_lun_enabled(isp, bus, lun)) {
820                 return (CAM_LUN_ALRDY_ENA);
821         }
822         new = (tstate_t *) malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
823         if (new == NULL) {
824                 return (CAM_RESRC_UNAVAIL);
825         }
826
827         status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
828             xpt_path_target_id(path), xpt_path_lun_id(path));
829         if (status != CAM_REQ_CMP) {
830                 free(new, M_DEVBUF);
831                 return (status);
832         }
833         new->bus = bus;
834         new->lun = lun;
835         SLIST_INIT(&new->atios);
836         SLIST_INIT(&new->inots);
837         new->hold = 1;
838
839         hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
840         tptr = isp->isp_osinfo.lun_hash[hfx];
841         if (tptr == NULL) {
842                 isp->isp_osinfo.lun_hash[hfx] = new;
843         } else {
844                 while (tptr->next)
845                         tptr = tptr->next;
846                 tptr->next = new;
847         }
848         *rslt = new;
849         return (CAM_REQ_CMP);
850 }
851
852 static __inline void
853 destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
854 {
855         int hfx;
856         tstate_t *lw, *pw;
857
858         if (tptr->hold) {
859                 return;
860         }
861         hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
862         pw = isp->isp_osinfo.lun_hash[hfx];
863         if (pw == NULL) {
864                 return;
865         } else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
866                 isp->isp_osinfo.lun_hash[hfx] = pw->next;
867         } else {
868                 lw = pw;
869                 pw = lw->next;
870                 while (pw) {
871                         if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
872                                 lw->next = pw->next;
873                                 break;
874                         }
875                         lw = pw;
876                         pw = pw->next;
877                 }
878                 if (pw == NULL) {
879                         return;
880                 }
881         }
882         free(tptr, M_DEVBUF);
883 }
884
885 /*
886  * Enable luns.
887  */
888 static int
889 isp_en_lun(ispsoftc_t *isp, union ccb *ccb)
890 {
891         struct ccb_en_lun *cel = &ccb->cel;
892         tstate_t *tptr;
893         uint32_t seq;
894         int bus, cmd, av, wildcard, tm_on;
895         lun_id_t lun;
896         target_id_t tgt;
897
898         bus = XS_CHANNEL(ccb);
899         if (bus > 1) {
900                 xpt_print(ccb->ccb_h.path, "illegal bus %d\n", bus);
901                 ccb->ccb_h.status = CAM_PATH_INVALID;
902                 return (-1);
903         }
904         tgt = ccb->ccb_h.target_id;
905         lun = ccb->ccb_h.target_lun;
906
907         if (isp->isp_dblev & ISP_LOGTDEBUG0) {
908                 xpt_print(ccb->ccb_h.path, "%sabling lun 0x%x on channel %d\n",
909                     cel->enable? "en" : "dis", lun, bus);
910         }
911
912         if ((lun != CAM_LUN_WILDCARD) &&
913             (lun < 0 || lun >= (lun_id_t) isp->isp_maxluns)) {
914                 ccb->ccb_h.status = CAM_LUN_INVALID;
915                 return (-1);
916         }
917
918         if (IS_SCSI(isp)) {
919                 sdparam *sdp = isp->isp_param;
920                 sdp += bus;
921                 if (tgt != CAM_TARGET_WILDCARD &&
922                     tgt != sdp->isp_initiator_id) {
923                         ccb->ccb_h.status = CAM_TID_INVALID;
924                         return (-1);
925                 }
926         } else {
927                 /*
928                  * There's really no point in doing this yet w/o multi-tid
929                  * capability. Even then, it's problematic.
930                  */
931 #if     0
932                 if (tgt != CAM_TARGET_WILDCARD &&
933                     tgt != FCPARAM(isp)->isp_iid) {
934                         ccb->ccb_h.status = CAM_TID_INVALID;
935                         return (-1);
936                 }
937 #endif
938                 /*
939                  * This is as a good a place as any to check f/w capabilities.
940                  */
941                 if (FCPARAM(isp)->isp_tmode == 0) {
942                         xpt_print(ccb->ccb_h.path,
943                             "firmware does not support target mode\n");
944                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
945                         return (-1);
946                 }
947                 /*
948                  * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
949                  * XXX: dork with our already fragile enable/disable code.
950                  */
951                 if (FCPARAM(isp)->isp_sccfw == 0) {
952                         xpt_print(ccb->ccb_h.path,
953                             "firmware not SCCLUN capable\n");
954                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
955                         return (-1);
956                 }
957         }
958
959         if (tgt == CAM_TARGET_WILDCARD) {
960                 if (lun == CAM_LUN_WILDCARD) {
961                         wildcard = 1;
962                 } else {
963                         ccb->ccb_h.status = CAM_LUN_INVALID;
964                         return (-1);
965                 }
966         } else {
967                 wildcard = 0;
968         }
969
970         tm_on = (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) != 0;
971
972         /*
973          * Next check to see whether this is a target/lun wildcard action.
974          *
975          * If so, we know that we can accept commands for luns that haven't
976          * been enabled yet and send them upstream. Otherwise, we have to
977          * handle them locally (if we see them at all).
978          */
979
980         if (wildcard) {
981                 tptr = &isp->isp_osinfo.tsdflt[bus];
982                 if (cel->enable) {
983                         if (tm_on) {
984                                 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
985                                 return (-1);
986                         }
987                         ccb->ccb_h.status =
988                             xpt_create_path(&tptr->owner, NULL,
989                             xpt_path_path_id(ccb->ccb_h.path),
990                             xpt_path_target_id(ccb->ccb_h.path),
991                             xpt_path_lun_id(ccb->ccb_h.path));
992                         if (ccb->ccb_h.status != CAM_REQ_CMP) {
993                                 return (-1);
994                         }
995                         SLIST_INIT(&tptr->atios);
996                         SLIST_INIT(&tptr->inots);
997                         isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
998                 } else {
999                         if (tm_on == 0) {
1000                                 ccb->ccb_h.status = CAM_REQ_CMP;
1001                                 return (-1);
1002                         }
1003                         if (tptr->hold) {
1004                                 ccb->ccb_h.status = CAM_SCSI_BUSY;
1005                                 return (-1);
1006                         }
1007                         xpt_free_path(tptr->owner);
1008                         isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
1009                 }
1010         }
1011
1012         /*
1013          * Now check to see whether this bus needs to be
1014          * enabled/disabled with respect to target mode.
1015          */
1016         av = bus << 31;
1017         if (cel->enable && tm_on == 0) {
1018                 av |= ENABLE_TARGET_FLAG;
1019                 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1020                 if (av) {
1021                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1022                         if (wildcard) {
1023                                 isp->isp_osinfo.tmflags[bus] &=
1024                                     ~TM_WILDCARD_ENABLED;
1025                                 xpt_free_path(tptr->owner);
1026                         }
1027                         return (-1);
1028                 }
1029                 isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
1030                 xpt_print(ccb->ccb_h.path, "Target Mode Enabled\n");
1031         } else if (cel->enable == 0 && tm_on && wildcard) {
1032                 if (are_any_luns_enabled(isp, bus)) {
1033                         ccb->ccb_h.status = CAM_SCSI_BUSY;
1034                         return (-1);
1035                 }
1036                 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1037                 if (av) {
1038                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1039                         return (-1);
1040                 }
1041                 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1042                 xpt_print(ccb->ccb_h.path, "Target Mode Disabled\n");
1043         }
1044
1045         if (wildcard) {
1046                 ccb->ccb_h.status = CAM_REQ_CMP;
1047                 return (-1);
1048         }
1049
1050         /*
1051          * Find an empty slot
1052          */
1053         for (seq = 0; seq < NLEACT; seq++) {
1054                 if (isp->isp_osinfo.leact[seq] == 0) {
1055                         break;
1056                 }
1057         }
1058         if (seq >= NLEACT) {
1059                 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1060                 return (-1);
1061                 
1062         }
1063         isp->isp_osinfo.leact[seq] = ccb;
1064
1065         if (cel->enable) {
1066                 ccb->ccb_h.status =
1067                     create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1068                 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1069                         isp->isp_osinfo.leact[seq] = 0;
1070                         return (-1);
1071                 }
1072         } else {
1073                 tptr = get_lun_statep(isp, bus, lun);
1074                 if (tptr == NULL) {
1075                         ccb->ccb_h.status = CAM_LUN_INVALID;
1076                         return (-1);
1077                 }
1078         }
1079
1080         if (cel->enable) {
1081                 int c, n, ulun = lun;
1082
1083                 cmd = RQSTYPE_ENABLE_LUN;
1084                 c = DFLT_CMND_CNT;
1085                 n = DFLT_INOT_CNT;
1086                 if (IS_FC(isp) && lun != 0) {
1087                         cmd = RQSTYPE_MODIFY_LUN;
1088                         n = 0;
1089                         /*
1090                          * For SCC firmware, we only deal with setting
1091                          * (enabling or modifying) lun 0.
1092                          */
1093                         ulun = 0;
1094                 }
1095                 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1096                         rls_lun_statep(isp, tptr);
1097                         ccb->ccb_h.status = CAM_REQ_INPROG;
1098                         return (seq);
1099                 }
1100         } else {
1101                 int c, n, ulun = lun;
1102
1103                 cmd = -RQSTYPE_MODIFY_LUN;
1104                 c = DFLT_CMND_CNT;
1105                 n = DFLT_INOT_CNT;
1106                 if (IS_FC(isp) && lun != 0) {
1107                         n = 0;
1108                         /*
1109                          * For SCC firmware, we only deal with setting
1110                          * (enabling or modifying) lun 0.
1111                          */
1112                         ulun = 0;
1113                 }
1114                 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq+1) == 0) {
1115                         rls_lun_statep(isp, tptr);
1116                         ccb->ccb_h.status = CAM_REQ_INPROG;
1117                         return (seq);
1118                 }
1119         }
1120         rls_lun_statep(isp, tptr);
1121         xpt_print(ccb->ccb_h.path, "isp_lun_cmd failed\n");
1122         isp->isp_osinfo.leact[seq] = 0;
1123         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1124         return (-1);
1125 }
1126
1127 static void
1128 isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1129 {
1130         const char lfmt[] = "now %sabled for target mode";
1131         union ccb *ccb;
1132         uint32_t seq;
1133         tstate_t *tptr;
1134         int av;
1135         struct ccb_en_lun *cel;
1136
1137         seq = lep->le_reserved - 1;
1138         if (seq >= NLEACT) {
1139                 isp_prt(isp, ISP_LOGERR,
1140                     "seq out of range (%u) in isp_ledone", seq);
1141                 return;
1142         }
1143         ccb = isp->isp_osinfo.leact[seq];
1144         if (ccb == 0) {
1145                 isp_prt(isp, ISP_LOGERR,
1146                     "no ccb for seq %u in isp_ledone", seq);
1147                 return;
1148         }
1149         cel = &ccb->cel;
1150         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1151         if (tptr == NULL) {
1152                 xpt_print(ccb->ccb_h.path, "null tptr in isp_ledone\n");
1153                 isp->isp_osinfo.leact[seq] = 0;
1154                 return;
1155         }
1156
1157         if (lep->le_status != LUN_OK) {
1158                 xpt_print(ccb->ccb_h.path,
1159                     "ENABLE/MODIFY LUN returned 0x%x\n", lep->le_status);
1160 err:
1161                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1162                 rls_lun_statep(isp, tptr);
1163                 isp->isp_osinfo.leact[seq] = 0;
1164                 ISPLOCK_2_CAMLOCK(isp);
1165                 xpt_done(ccb);
1166                 CAMLOCK_2_ISPLOCK(isp);
1167                 return;
1168         } else {
1169                 isp_prt(isp, ISP_LOGTDEBUG0,
1170                     "isp_ledone: ENABLE/MODIFY done okay");
1171         }
1172
1173
1174         if (cel->enable) {
1175                 ccb->ccb_h.status = CAM_REQ_CMP;
1176                 xpt_print(ccb->ccb_h.path, lfmt, "en");
1177                 rls_lun_statep(isp, tptr);
1178                 isp->isp_osinfo.leact[seq] = 0;
1179                 ISPLOCK_2_CAMLOCK(isp);
1180                 xpt_done(ccb);
1181                 CAMLOCK_2_ISPLOCK(isp);
1182                 return;
1183         }
1184
1185         if (lep->le_header.rqs_entry_type == RQSTYPE_MODIFY_LUN) {
1186                 if (isp_lun_cmd(isp, -RQSTYPE_ENABLE_LUN, XS_CHANNEL(ccb),
1187                     XS_TGT(ccb), XS_LUN(ccb), 0, 0, seq+1)) {
1188                         xpt_print(ccb->ccb_h.path,
1189                             "isp_ledone: isp_lun_cmd failed\n");
1190                         goto err;
1191                 }
1192                 rls_lun_statep(isp, tptr);
1193                 return;
1194         }
1195
1196         xpt_print(ccb->ccb_h.path, lfmt, "dis");
1197         rls_lun_statep(isp, tptr);
1198         destroy_lun_state(isp, tptr);
1199         ccb->ccb_h.status = CAM_REQ_CMP;
1200         isp->isp_osinfo.leact[seq] = 0;
1201         ISPLOCK_2_CAMLOCK(isp);
1202         xpt_done(ccb);
1203         CAMLOCK_2_ISPLOCK(isp);
1204         if (are_any_luns_enabled(isp, XS_CHANNEL(ccb)) == 0) {
1205                 int bus = XS_CHANNEL(ccb);
1206                 av = bus << 31;
1207                 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1208                 if (av) {
1209                         isp_prt(isp, ISP_LOGWARN,
1210                             "disable target mode on channel %d failed", bus);
1211                 }
1212                 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1213         }
1214 }
1215
1216
1217 static cam_status
1218 isp_abort_tgt_ccb(ispsoftc_t *isp, union ccb *ccb)
1219 {
1220         tstate_t *tptr;
1221         struct ccb_hdr_slist *lp;
1222         struct ccb_hdr *curelm;
1223         int found, *ctr;
1224         union ccb *accb = ccb->cab.abort_ccb;
1225
1226         xpt_print(ccb->ccb_h.path, "aborting ccb %p\n", accb);
1227         if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1228                 int badpath = 0;
1229                 if (IS_FC(isp) && (accb->ccb_h.target_id != 
1230                     ((fcparam *) isp->isp_param)->isp_loopid)) {
1231                         badpath = 1;
1232                 } else if (IS_SCSI(isp) && (accb->ccb_h.target_id != 
1233                     ((sdparam *) isp->isp_param)->isp_initiator_id)) {
1234                         badpath = 1;
1235                 }
1236                 if (badpath) {
1237                         /*
1238                          * Being restrictive about target ids is really about
1239                          * making sure we're aborting for the right multi-tid
1240                          * path. This doesn't really make much sense at present.
1241                          */
1242 #if     0
1243                         return (CAM_PATH_INVALID);
1244 #endif
1245                 }
1246         }
1247         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
1248         if (tptr == NULL) {
1249                 xpt_print(ccb->ccb_h.path, "can't get statep\n");
1250                 return (CAM_PATH_INVALID);
1251         }
1252         if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1253                 lp = &tptr->atios;
1254                 ctr = &tptr->atio_count;
1255         } else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
1256                 lp = &tptr->inots;
1257                 ctr = &tptr->inot_count;
1258         } else {
1259                 rls_lun_statep(isp, tptr);
1260                 xpt_print(ccb->ccb_h.path, "bad function code %d\n",
1261                     accb->ccb_h.func_code);
1262                 return (CAM_UA_ABORT);
1263         }
1264         curelm = SLIST_FIRST(lp);
1265         found = 0;
1266         if (curelm == &accb->ccb_h) {
1267                 found = 1;
1268                 SLIST_REMOVE_HEAD(lp, sim_links.sle);
1269         } else {
1270                 while(curelm != NULL) {
1271                         struct ccb_hdr *nextelm;
1272
1273                         nextelm = SLIST_NEXT(curelm, sim_links.sle);
1274                         if (nextelm == &accb->ccb_h) {
1275                                 found = 1;
1276                                 SLIST_NEXT(curelm, sim_links.sle) =
1277                                     SLIST_NEXT(nextelm, sim_links.sle);
1278                                 break;
1279                         }
1280                         curelm = nextelm;
1281                 }
1282         }
1283         rls_lun_statep(isp, tptr);
1284         if (found) {
1285                 (*ctr)--;
1286                 accb->ccb_h.status = CAM_REQ_ABORTED;
1287                 xpt_done(accb);
1288                 return (CAM_REQ_CMP);
1289         }
1290         xpt_print(ccb->ccb_h.path, "ccb %p not found\n", accb);
1291         return (CAM_PATH_INVALID);
1292 }
1293
1294 static void
1295 isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
1296 {
1297         void *qe;
1298         struct ccb_scsiio *cso = &ccb->csio;
1299         uint32_t nxti, optr, handle;
1300         uint8_t local[QENTRY_LEN];
1301
1302
1303         if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1304                 xpt_print(ccb->ccb_h.path,
1305                     "Request Queue Overflow in isp_target_start_ctio\n");
1306                 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1307                 goto out;
1308         }
1309         memset(local, 0, QENTRY_LEN);
1310
1311         /*
1312          * We're either moving data or completing a command here.
1313          */
1314
1315         if (IS_FC(isp)) {
1316                 atio_private_data_t *atp;
1317                 ct2_entry_t *cto = (ct2_entry_t *) local;
1318
1319                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1320                 cto->ct_header.rqs_entry_count = 1;
1321                 if (FCPARAM(isp)->isp_2klogin) {
1322                         ((ct2e_entry_t *)cto)->ct_iid = cso->init_id;
1323                 } else {
1324                         cto->ct_iid = cso->init_id;
1325                         if (FCPARAM(isp)->isp_sccfw == 0) {
1326                                 cto->ct_lun = ccb->ccb_h.target_lun;
1327                         }
1328                 }
1329
1330                 atp = isp_get_atpd(isp, cso->tag_id);
1331                 if (atp == NULL) {
1332                         xpt_print(ccb->ccb_h.path,
1333                             "cannot find private data adjunct for tag %x\n",
1334                             cso->tag_id);
1335                         XS_SETERR(ccb, CAM_REQ_CMP_ERR);
1336                         goto out;
1337                 }
1338
1339                 cto->ct_rxid = cso->tag_id;
1340                 if (cso->dxfer_len == 0) {
1341                         cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
1342                         if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1343                                 cto->ct_flags |= CT2_SENDSTATUS;
1344                                 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1345                                 cto->ct_resid =
1346                                     atp->orig_datalen - atp->bytes_xfered;
1347                                 if (cto->ct_resid < 0) {
1348                                         cto->rsp.m1.ct_scsi_status |=
1349                                             CT2_DATA_OVER;
1350                                 } else if (cto->ct_resid > 0) {
1351                                         cto->rsp.m1.ct_scsi_status |=
1352                                             CT2_DATA_UNDER;
1353                                 }
1354                         }
1355                         if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1356                                 int m = min(cso->sense_len, MAXRESPLEN);
1357                                 memcpy(cto->rsp.m1.ct_resp,
1358                                     &cso->sense_data, m);
1359                                 cto->rsp.m1.ct_senselen = m;
1360                                 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1361                         }
1362                 } else {
1363                         cto->ct_flags |= CT2_FLAG_MODE0;
1364                         if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1365                                 cto->ct_flags |= CT2_DATA_IN;
1366                         } else {
1367                                 cto->ct_flags |= CT2_DATA_OUT;
1368                         }
1369                         cto->ct_reloff = atp->bytes_xfered;
1370                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1371                                 cto->ct_flags |= CT2_SENDSTATUS;
1372                                 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1373                                 cto->ct_resid =
1374                                     atp->orig_datalen -
1375                                     (atp->bytes_xfered + cso->dxfer_len);
1376                                 if (cto->ct_resid < 0) {
1377                                         cto->rsp.m0.ct_scsi_status |=
1378                                             CT2_DATA_OVER;
1379                                 } else if (cto->ct_resid > 0) {
1380                                         cto->rsp.m0.ct_scsi_status |=
1381                                             CT2_DATA_UNDER;
1382                                 }
1383                         } else {
1384                                 atp->last_xframt = cso->dxfer_len;
1385                         }
1386                         /*
1387                          * If we're sending data and status back together,
1388                          * we can't also send back sense data as well.
1389                          */
1390                         ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1391                 }
1392
1393                 if (cto->ct_flags & CT2_SENDSTATUS) {
1394                         isp_prt(isp, ISP_LOGTDEBUG0,
1395                             "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1396                             cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1397                             cso->dxfer_len, cto->ct_resid);
1398                         cto->ct_flags |= CT2_CCINCR;
1399                         atp->state = ATPD_STATE_LAST_CTIO;
1400                 } else {
1401                         atp->state = ATPD_STATE_CTIO;
1402                 }
1403                 cto->ct_timeout = 10;
1404         } else {
1405                 ct_entry_t *cto = (ct_entry_t *) local;
1406
1407                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1408                 cto->ct_header.rqs_entry_count = 1;
1409                 cto->ct_iid = cso->init_id;
1410                 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1411                 cto->ct_tgt = ccb->ccb_h.target_id;
1412                 cto->ct_lun = ccb->ccb_h.target_lun;
1413                 cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1414                 if (AT_HAS_TAG(cso->tag_id)) {
1415                         cto->ct_tag_val = (uint8_t) AT_GET_TAG(cso->tag_id);
1416                         cto->ct_flags |= CT_TQAE;
1417                 }
1418                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1419                         cto->ct_flags |= CT_NODISC;
1420                 }
1421                 if (cso->dxfer_len == 0) {
1422                         cto->ct_flags |= CT_NO_DATA;
1423                 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1424                         cto->ct_flags |= CT_DATA_IN;
1425                 } else {
1426                         cto->ct_flags |= CT_DATA_OUT;
1427                 }
1428                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1429                         cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1430                         cto->ct_scsi_status = cso->scsi_status;
1431                         cto->ct_resid = cso->resid;
1432                         isp_prt(isp, ISP_LOGTDEBUG0,
1433                             "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1434                             cto->ct_fwhandle, cso->scsi_status, cso->resid,
1435                             cso->tag_id);
1436                 }
1437                 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1438                 cto->ct_timeout = 10;
1439         }
1440
1441         if (isp_save_xs_tgt(isp, ccb, &handle)) {
1442                 xpt_print(ccb->ccb_h.path,
1443                     "No XFLIST pointers for isp_target_start_ctio\n");
1444                 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1445                 goto out;
1446         }
1447
1448
1449         /*
1450          * Call the dma setup routines for this entry (and any subsequent
1451          * CTIOs) if there's data to move, and then tell the f/w it's got
1452          * new things to play with. As with isp_start's usage of DMA setup,
1453          * any swizzling is done in the machine dependent layer. Because
1454          * of this, we put the request onto the queue area first in native
1455          * format.
1456          */
1457
1458         if (IS_FC(isp)) {
1459                 ct2_entry_t *cto = (ct2_entry_t *) local;
1460                 cto->ct_syshandle = handle;
1461         } else {
1462                 ct_entry_t *cto = (ct_entry_t *) local;
1463                 cto->ct_syshandle = handle;
1464         }
1465
1466         switch (ISP_DMASETUP(isp, cso, (ispreq_t *) local, &nxti, optr)) {
1467         case CMD_QUEUED:
1468                 ISP_ADD_REQUEST(isp, nxti);
1469                 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1470                 return;
1471
1472         case CMD_EAGAIN:
1473                 XS_SETERR(ccb, CAM_REQUEUE_REQ);
1474                 break;
1475
1476         default:
1477                 break;
1478         }
1479         isp_destroy_tgt_handle(isp, handle);
1480
1481 out:
1482         ISPLOCK_2_CAMLOCK(isp);
1483         xpt_done(ccb);
1484         CAMLOCK_2_ISPLOCK(isp);
1485 }
1486
1487 static void
1488 isp_refire_putback_atio(void *arg)
1489 {
1490         int s = splcam();
1491         isp_target_putback_atio(arg);
1492         splx(s);
1493 }
1494
1495 static void
1496 isp_target_putback_atio(union ccb *ccb)
1497 {
1498         ispsoftc_t *isp;
1499         struct ccb_scsiio *cso;
1500         uint32_t nxti, optr;
1501         void *qe;
1502
1503         isp = XS_ISP(ccb);
1504
1505         if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1506                 xpt_print(ccb->ccb_h.path,
1507                     "isp_target_putback_atio: Request Queue Overflow\n"); 
1508                 (void) timeout(isp_refire_putback_atio, ccb, 10);
1509                 return;
1510         }
1511         memset(qe, 0, QENTRY_LEN);
1512         cso = &ccb->csio;
1513         if (IS_FC(isp)) {
1514                 at2_entry_t local, *at = &local;
1515                 MEMZERO(at, sizeof (at2_entry_t));
1516                 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1517                 at->at_header.rqs_entry_count = 1;
1518                 if (FCPARAM(isp)->isp_sccfw) {
1519                         at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1520                 } else {
1521                         at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1522                 }
1523                 at->at_status = CT_OK;
1524                 at->at_rxid = cso->tag_id;
1525                 at->at_iid = cso->ccb_h.target_id;
1526                 isp_put_atio2(isp, at, qe);
1527         } else {
1528                 at_entry_t local, *at = &local;
1529                 MEMZERO(at, sizeof (at_entry_t));
1530                 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1531                 at->at_header.rqs_entry_count = 1;
1532                 at->at_iid = cso->init_id;
1533                 at->at_iid |= XS_CHANNEL(ccb) << 7;
1534                 at->at_tgt = cso->ccb_h.target_id;
1535                 at->at_lun = cso->ccb_h.target_lun;
1536                 at->at_status = CT_OK;
1537                 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1538                 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1539                 isp_put_atio(isp, at, qe);
1540         }
1541         ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1542         ISP_ADD_REQUEST(isp, nxti);
1543         isp_complete_ctio(ccb);
1544 }
1545
1546 static void
1547 isp_complete_ctio(union ccb *ccb)
1548 {
1549         ISPLOCK_2_CAMLOCK(isp);
1550         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1551                 ccb->ccb_h.status |= CAM_REQ_CMP;
1552         }
1553         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1554         xpt_done(ccb);
1555         CAMLOCK_2_ISPLOCK(isp);
1556 }
1557
1558 /*
1559  * Handle ATIO stuff that the generic code can't.
1560  * This means handling CDBs.
1561  */
1562
1563 static int
1564 isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
1565 {
1566         tstate_t *tptr;
1567         int status, bus, iswildcard;
1568         struct ccb_accept_tio *atiop;
1569
1570         /*
1571          * The firmware status (except for the QLTM_SVALID bit)
1572          * indicates why this ATIO was sent to us.
1573          *
1574          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1575          *
1576          * If the DISCONNECTS DISABLED bit is set in the flags field,
1577          * we're still connected on the SCSI bus.
1578          */
1579         status = aep->at_status;
1580         if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1581                 /*
1582                  * Bus Phase Sequence error. We should have sense data
1583                  * suggested by the f/w. I'm not sure quite yet what
1584                  * to do about this for CAM.
1585                  */
1586                 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1587                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1588                 return (0);
1589         }
1590         if ((status & ~QLTM_SVALID) != AT_CDB) {
1591                 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1592                     status);
1593                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1594                 return (0);
1595         }
1596
1597         bus = GET_BUS_VAL(aep->at_iid);
1598         tptr = get_lun_statep(isp, bus, aep->at_lun);
1599         if (tptr == NULL) {
1600                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1601                 if (tptr == NULL) {
1602                         /*
1603                          * Because we can't autofeed sense data back with
1604                          * a command for parallel SCSI, we can't give back
1605                          * a CHECK CONDITION. We'll give back a BUSY status
1606                          * instead. This works out okay because the only
1607                          * time we should, in fact, get this, is in the
1608                          * case that somebody configured us without the
1609                          * blackhole driver, so they get what they deserve.
1610                          */
1611                         isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1612                         return (0);
1613                 }
1614                 iswildcard = 1;
1615         } else {
1616                 iswildcard = 0;
1617         }
1618
1619         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1620         if (atiop == NULL) {
1621                 /*
1622                  * Because we can't autofeed sense data back with
1623                  * a command for parallel SCSI, we can't give back
1624                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
1625                  * instead. This works out okay because the only time we
1626                  * should, in fact, get this, is in the case that we've
1627                  * run out of ATIOS.
1628                  */
1629                 xpt_print(tptr->owner,
1630                     "no ATIOS for lun %d from initiator %d on channel %d\n",
1631                     aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1632                 if (aep->at_flags & AT_TQAE)
1633                         isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1634                 else
1635                         isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1636                 rls_lun_statep(isp, tptr);
1637                 return (0);
1638         }
1639         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1640         tptr->atio_count--;
1641         isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1642             aep->at_lun, tptr->atio_count);
1643         if (iswildcard) {
1644                 atiop->ccb_h.target_id = aep->at_tgt;
1645                 atiop->ccb_h.target_lun = aep->at_lun;
1646         }
1647         if (aep->at_flags & AT_NODISC) {
1648                 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1649         } else {
1650                 atiop->ccb_h.flags = 0;
1651         }
1652
1653         if (status & QLTM_SVALID) {
1654                 size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1655                 atiop->sense_len = amt;
1656                 MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1657         } else {
1658                 atiop->sense_len = 0;
1659         }
1660
1661         atiop->init_id = GET_IID_VAL(aep->at_iid);
1662         atiop->cdb_len = aep->at_cdblen;
1663         MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1664         atiop->ccb_h.status = CAM_CDB_RECVD;
1665         /*
1666          * Construct a tag 'id' based upon tag value (which may be 0..255)
1667          * and the handle (which we have to preserve).
1668          */
1669         AT_MAKE_TAGID(atiop->tag_id,  device_get_unit(isp->isp_dev), aep);
1670         if (aep->at_flags & AT_TQAE) {
1671                 atiop->tag_action = aep->at_tag_type;
1672                 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1673         }
1674         xpt_done((union ccb*)atiop);
1675         isp_prt(isp, ISP_LOGTDEBUG0,
1676             "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1677             aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1678             GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1679             aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1680             "nondisc" : "disconnecting");
1681         rls_lun_statep(isp, tptr);
1682         return (0);
1683 }
1684
1685 static int
1686 isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1687 {
1688         lun_id_t lun;
1689         tstate_t *tptr;
1690         struct ccb_accept_tio *atiop;
1691         atio_private_data_t *atp;
1692
1693         /*
1694          * The firmware status (except for the QLTM_SVALID bit)
1695          * indicates why this ATIO was sent to us.
1696          *
1697          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1698          */
1699         if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1700                 isp_prt(isp, ISP_LOGWARN,
1701                     "bogus atio (0x%x) leaked to platform", aep->at_status);
1702                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1703                 return (0);
1704         }
1705
1706         if (FCPARAM(isp)->isp_sccfw) {
1707                 lun = aep->at_scclun;
1708         } else {
1709                 lun = aep->at_lun;
1710         }
1711         tptr = get_lun_statep(isp, 0, lun);
1712         if (tptr == NULL) {
1713                 isp_prt(isp, ISP_LOGTDEBUG0,
1714                     "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
1715                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1716                 if (tptr == NULL) {
1717                         isp_endcmd(isp, aep,
1718                             SCSI_STATUS_CHECK_COND | ECMD_SVALID |
1719                             (0x5 << 12) | (0x25 << 16), 0);
1720                         return (0);
1721                 }
1722         }
1723
1724         atp = isp_get_atpd(isp, 0);
1725         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1726         if (atiop == NULL || atp == NULL) {
1727
1728                 /*
1729                  * Because we can't autofeed sense data back with
1730                  * a command for parallel SCSI, we can't give back
1731                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
1732                  * instead. This works out okay because the only time we
1733                  * should, in fact, get this, is in the case that we've
1734                  * run out of ATIOS.
1735                  */
1736                 xpt_print(tptr->owner,
1737                     "no %s for lun %d from initiator %d\n",
1738                     (atp == NULL && atiop == NULL)? "ATIO2s *or* ATPS" :
1739                     ((atp == NULL)? "ATPs" : "ATIO2s"), lun, aep->at_iid);
1740                 rls_lun_statep(isp, tptr);
1741                 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1742                 return (0);
1743         }
1744         atp->state = ATPD_STATE_ATIO;
1745         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1746         tptr->atio_count--;
1747         isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO lun %d, count now %d",
1748             lun, tptr->atio_count);
1749
1750         if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1751                 atiop->ccb_h.target_id = FCPARAM(isp)->isp_loopid;
1752                 atiop->ccb_h.target_lun = lun;
1753         }
1754         /*
1755          * We don't get 'suggested' sense data as we do with SCSI cards.
1756          */
1757         atiop->sense_len = 0;
1758
1759         atiop->init_id = aep->at_iid;
1760         atiop->cdb_len = ATIO2_CDBLEN;
1761         MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1762         atiop->ccb_h.status = CAM_CDB_RECVD;
1763         atiop->tag_id = aep->at_rxid;
1764         switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1765         case ATIO2_TC_ATTR_SIMPLEQ:
1766                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1767                 break;
1768         case ATIO2_TC_ATTR_HEADOFQ:
1769                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1770                 break;
1771         case ATIO2_TC_ATTR_ORDERED:
1772                 atiop->tag_action = MSG_ORDERED_Q_TAG;
1773                 break;
1774         case ATIO2_TC_ATTR_ACAQ:                /* ?? */
1775         case ATIO2_TC_ATTR_UNTAGGED:
1776         default:
1777                 atiop->tag_action = 0;
1778                 break;
1779         }
1780         atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
1781
1782         atp->tag = atiop->tag_id;
1783         atp->lun = lun;
1784         atp->orig_datalen = aep->at_datalen;
1785         atp->last_xframt = 0;
1786         atp->bytes_xfered = 0;
1787         atp->state = ATPD_STATE_CAM;
1788         ISPLOCK_2_CAMLOCK(siP);
1789         xpt_done((union ccb*)atiop);
1790
1791         isp_prt(isp, ISP_LOGTDEBUG0,
1792             "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1793             aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1794             lun, aep->at_taskflags, aep->at_datalen);
1795         rls_lun_statep(isp, tptr);
1796         return (0);
1797 }
1798
1799 static int
1800 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
1801 {
1802         union ccb *ccb;
1803         int sentstatus, ok, notify_cam, resid = 0;
1804         uint16_t tval;
1805
1806         /*
1807          * CTIO and CTIO2 are close enough....
1808          */
1809
1810         ccb = isp_find_xs_tgt(isp, ((ct_entry_t *)arg)->ct_syshandle);
1811         KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1812         isp_destroy_tgt_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1813
1814         if (IS_FC(isp)) {
1815                 ct2_entry_t *ct = arg;
1816                 atio_private_data_t *atp = isp_get_atpd(isp, ct->ct_rxid);
1817                 if (atp == NULL) {
1818                         isp_prt(isp, ISP_LOGERR,
1819                             "cannot find adjunct for %x after I/O",
1820                             ct->ct_rxid);
1821                         return (0);
1822                 }
1823                 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1824                 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1825                 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1826                         ccb->ccb_h.status |= CAM_SENT_SENSE;
1827                 }
1828                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1829                 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1830                         resid = ct->ct_resid;
1831                         atp->bytes_xfered += (atp->last_xframt - resid);
1832                         atp->last_xframt = 0;
1833                 }
1834                 if (sentstatus || !ok) {
1835                         atp->tag = 0;
1836                 }
1837                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN,
1838                     "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1839                     ct->ct_rxid, ct->ct_status, ct->ct_flags,
1840                     (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1841                     resid, sentstatus? "FIN" : "MID");
1842                 tval = ct->ct_rxid;
1843
1844                 /* XXX: should really come after isp_complete_ctio */
1845                 atp->state = ATPD_STATE_PDON;
1846         } else {
1847                 ct_entry_t *ct = arg;
1848                 sentstatus = ct->ct_flags & CT_SENDSTATUS;
1849                 ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
1850                 /*
1851                  * We *ought* to be able to get back to the original ATIO
1852                  * here, but for some reason this gets lost. It's just as
1853                  * well because it's squirrelled away as part of periph
1854                  * private data.
1855                  *
1856                  * We can live without it as long as we continue to use
1857                  * the auto-replenish feature for CTIOs.
1858                  */
1859                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1860                 if (ct->ct_status & QLTM_SVALID) {
1861                         char *sp = (char *)ct;
1862                         sp += CTIO_SENSE_OFFSET;
1863                         ccb->csio.sense_len =
1864                             min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1865                         MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1866                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1867                 }
1868                 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1869                         resid = ct->ct_resid;
1870                 }
1871                 isp_prt(isp, ISP_LOGTDEBUG0,
1872                     "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1873                     ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1874                     ct->ct_status, ct->ct_flags, resid,
1875                     sentstatus? "FIN" : "MID");
1876                 tval = ct->ct_fwhandle;
1877         }
1878         ccb->csio.resid += resid;
1879
1880         /*
1881          * We're here either because intermediate data transfers are done
1882          * and/or the final status CTIO (which may have joined with a
1883          * Data Transfer) is done.
1884          *
1885          * In any case, for this platform, the upper layers figure out
1886          * what to do next, so all we do here is collect status and
1887          * pass information along. Any DMA handles have already been
1888          * freed.
1889          */
1890         if (notify_cam == 0) {
1891                 isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
1892                 return (0);
1893         }
1894
1895         isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1896             (sentstatus)? "  FINAL " : "MIDTERM ", tval);
1897
1898         if (!ok) {
1899                 isp_target_putback_atio(ccb);
1900         } else {
1901                 isp_complete_ctio(ccb);
1902
1903         }
1904         return (0);
1905 }
1906
1907 static int
1908 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inp)
1909 {
1910         return (0);     /* XXXX */
1911 }
1912
1913 static int
1914 isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
1915 {
1916
1917         switch (inp->in_status) {
1918         case IN_PORT_LOGOUT:
1919                 isp_prt(isp, ISP_LOGWARN, "port logout of iid %d",
1920                    inp->in_iid);
1921                 break;
1922         case IN_PORT_CHANGED:
1923                 isp_prt(isp, ISP_LOGWARN, "port changed for iid %d",
1924                    inp->in_iid);
1925                 break;
1926         case IN_GLOBAL_LOGO:
1927                 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
1928                 break;
1929         case IN_ABORT_TASK:
1930         {
1931                 atio_private_data_t *atp = isp_get_atpd(isp, inp->in_seqid);
1932                 struct ccb_immed_notify *inot = NULL;
1933
1934                 if (atp) {
1935                         tstate_t *tptr = get_lun_statep(isp, 0, atp->lun);
1936                         if (tptr) {
1937                                 inot = (struct ccb_immed_notify *)
1938                                     SLIST_FIRST(&tptr->inots);
1939                                 if (inot) {
1940                                         tptr->inot_count--;
1941                                         SLIST_REMOVE_HEAD(&tptr->inots,
1942                                             sim_links.sle);
1943                                         isp_prt(isp, ISP_LOGTDEBUG0,
1944                                             "Take FREE INOT count now %d",
1945                                             tptr->inot_count);
1946                                 }
1947                         }
1948                         isp_prt(isp, ISP_LOGWARN,
1949                            "abort task RX_ID %x IID %d state %d",
1950                            inp->in_seqid, inp->in_iid, atp->state);
1951                 } else {
1952                         isp_prt(isp, ISP_LOGWARN,
1953                            "abort task RX_ID %x from iid %d, state unknown",
1954                            inp->in_seqid, inp->in_iid);
1955                 }
1956                 if (inot) {
1957                         inot->initiator_id = inp->in_iid;
1958                         inot->sense_len = 0;
1959                         inot->message_args[0] = MSG_ABORT_TAG;
1960                         inot->message_args[1] = inp->in_seqid & 0xff;
1961                         inot->message_args[2] = (inp->in_seqid >> 8) & 0xff;
1962                         inot->ccb_h.status = CAM_MESSAGE_RECV;
1963                         xpt_done((union ccb *)inot);
1964                 }
1965                 break;
1966         }
1967         default:
1968                 break;
1969         }
1970         return (0);
1971 }
1972 #endif
1973
1974 static void
1975 isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
1976 {
1977         struct cam_sim *sim;
1978         ispsoftc_t *isp;
1979
1980         sim = (struct cam_sim *)cbarg;
1981         isp = (ispsoftc_t *) cam_sim_softc(sim);
1982         switch (code) {
1983         case AC_LOST_DEVICE:
1984                 if (IS_SCSI(isp)) {
1985                         uint16_t oflags, nflags;
1986                         sdparam *sdp = isp->isp_param;
1987                         int tgt;
1988
1989                         tgt = xpt_path_target_id(path);
1990                         if (tgt >= 0) {
1991                                 sdp += cam_sim_bus(sim);
1992                                 ISP_LOCK(isp);
1993                                 nflags = sdp->isp_devparam[tgt].nvrm_flags;
1994 #ifndef ISP_TARGET_MODE
1995                                 nflags &= DPARM_SAFE_DFLT;
1996                                 if (isp->isp_loaded_fw) {
1997                                         nflags |= DPARM_NARROW | DPARM_ASYNC;
1998                                 }
1999 #else
2000                                 nflags = DPARM_DEFAULT;
2001 #endif
2002                                 oflags = sdp->isp_devparam[tgt].goal_flags;
2003                                 sdp->isp_devparam[tgt].goal_flags = nflags;
2004                                 sdp->isp_devparam[tgt].dev_update = 1;
2005                                 isp->isp_update |= (1 << cam_sim_bus(sim));
2006                                 (void) isp_control(isp,
2007                                     ISPCTL_UPDATE_PARAMS, NULL);
2008                                 sdp->isp_devparam[tgt].goal_flags = oflags;
2009                                 ISP_UNLOCK(isp);
2010                         }
2011                 }
2012                 break;
2013         default:
2014                 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
2015                 break;
2016         }
2017 }
2018
2019 static void
2020 isp_poll(struct cam_sim *sim)
2021 {
2022         ispsoftc_t *isp = cam_sim_softc(sim);
2023         uint32_t isr;
2024         uint16_t sema, mbox;
2025
2026         ISP_LOCK(isp);
2027         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2028                 isp_intr(isp, isr, sema, mbox);
2029         }
2030         ISP_UNLOCK(isp);
2031 }
2032
2033
2034 static int isp_watchdog_work(ispsoftc_t *, XS_T *);
2035
2036 static int
2037 isp_watchdog_work(ispsoftc_t *isp, XS_T *xs)
2038 {
2039         uint32_t handle;
2040
2041         /*
2042          * We've decided this command is dead. Make sure we're not trying
2043          * to kill a command that's already dead by getting it's handle and
2044          * and seeing whether it's still alive.
2045          */
2046         ISP_LOCK(isp);
2047         handle = isp_find_handle(isp, xs);
2048         if (handle) {
2049                 uint32_t isr;
2050                 uint16_t sema, mbox;
2051
2052                 if (XS_CMD_DONE_P(xs)) {
2053                         isp_prt(isp, ISP_LOGDEBUG1,
2054                             "watchdog found done cmd (handle 0x%x)", handle);
2055                         ISP_UNLOCK(isp);
2056                         return (1);;
2057                 }
2058
2059                 if (XS_CMD_WDOG_P(xs)) {
2060                         isp_prt(isp, ISP_LOGDEBUG2,
2061                             "recursive watchdog (handle 0x%x)", handle);
2062                         ISP_UNLOCK(isp);
2063                         return (1);
2064                 }
2065
2066                 XS_CMD_S_WDOG(xs);
2067                 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2068                         isp_intr(isp, isr, sema, mbox);
2069                 }
2070                 if (XS_CMD_DONE_P(xs)) {
2071                         isp_prt(isp, ISP_LOGDEBUG2,
2072                             "watchdog cleanup for handle 0x%x", handle);
2073                         ISPLOCK_2_CAMLOCK(isp);
2074                         xpt_done((union ccb *) xs);
2075                         CAMLOCK_2_ISPLOCK(isp);
2076                 } else if (XS_CMD_GRACE_P(xs)) {
2077                         /*
2078                          * Make sure the command is *really* dead before we
2079                          * release the handle (and DMA resources) for reuse.
2080                          */
2081                         (void) isp_control(isp, ISPCTL_ABORT_CMD, xs);
2082
2083                         /*
2084                          * After this point, the comamnd is really dead.
2085                          */
2086                         if (XS_XFRLEN(xs)) {
2087                                 ISP_DMAFREE(isp, xs, handle);
2088                         } 
2089                         isp_destroy_handle(isp, handle);
2090                         xpt_print(xs->ccb_h.path,
2091                             "watchdog timeout for handle 0x%x\n", handle);
2092                         XS_SETERR(xs, CAM_CMD_TIMEOUT);
2093                         XS_CMD_C_WDOG(xs);
2094                         ISPLOCK_2_CAMLOCK(isp);
2095                         isp_done(xs);
2096                         CAMLOCK_2_ISPLOCK(isp);
2097                 } else {
2098                         XS_CMD_C_WDOG(xs);
2099                         xs->ccb_h.timeout_ch = timeout(isp_watchdog, xs, hz);
2100                         XS_CMD_S_GRACE(xs);
2101                         isp->isp_sendmarker |= 1 << XS_CHANNEL(xs);
2102                 }
2103                 ISP_UNLOCK(isp);
2104                 return (1);
2105         }
2106         ISP_UNLOCK(isp);
2107         return (0);
2108 }
2109
2110 static void
2111 isp_watchdog(void *arg)
2112 {
2113         ispsoftc_t *isp;
2114         XS_T *xs = arg;
2115         for (isp = isplist; isp != NULL; isp = isp->isp_osinfo.next) {
2116                 if (isp_watchdog_work(isp, xs)) {
2117                         break;
2118                 }
2119         }
2120         if (isp == NULL) {
2121                 printf("isp_watchdog: nobody had %p active\n", arg);
2122         }
2123 }
2124
2125
2126 #if __FreeBSD_version >= 500000  
2127 #define isp_make_here(isp, tgt) isp_announce(isp, tgt, AC_FOUND_DEVICE)
2128 #define isp_make_gone(isp, tgt) isp_announce(isp, tgt, AC_LOST_DEVICE)
2129
2130 /*
2131  * Support function for Announcement
2132  */
2133 static void
2134 isp_announce(ispsoftc_t *isp, int tgt, int action)
2135 {
2136         struct cam_path *tmppath;
2137         ISPLOCK_2_CAMLOCK(isp);
2138         if (xpt_create_path(&tmppath, NULL, cam_sim_path(isp->isp_sim), tgt,
2139             CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
2140                 xpt_async(action, tmppath, NULL);
2141                 xpt_free_path(tmppath);
2142         }
2143         CAMLOCK_2_ISPLOCK(isp);
2144 }
2145 #else
2146 #define isp_make_here(isp, tgt) do { ; } while (0)
2147 #define isp_make_gone(isp, tgt) do { ; } while (0)
2148 #endif
2149
2150
2151 /*
2152  * Gone Device Timer Function- when we have decided that a device has gone
2153  * away, we wait a specific period of time prior to telling the OS it has
2154  * gone away.
2155  *
2156  * This timer function fires once a second and then scans the port database
2157  * for devices that are marked dead but still have a virtual target assigned.
2158  * We decrement a counter for that port database entry, and when it hits zero,
2159  * we tell the OS the device has gone away.
2160  */
2161 static void
2162 isp_gdt(void *arg)
2163 {
2164         ispsoftc_t *isp = arg;
2165         fcportdb_t *lp;
2166         int dbidx, tgt, more_to_do = 0;
2167
2168         isp_prt(isp, ISP_LOGDEBUG0, "GDT timer expired");
2169         ISP_LOCK(isp);
2170         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2171                 lp = &FCPARAM(isp)->portdb[dbidx];
2172
2173                 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
2174                         continue;
2175                 }
2176                 if (lp->ini_map_idx == 0) {
2177                         continue;
2178                 }
2179                 if (lp->new_reserved == 0) {
2180                         continue;
2181                 }
2182                 lp->new_reserved -= 1;
2183                 if (lp->new_reserved != 0) {
2184                         more_to_do++;
2185                         continue;
2186                 }
2187                 tgt = lp->ini_map_idx - 1;
2188                 FCPARAM(isp)->isp_ini_map[tgt] = 0;
2189                 lp->ini_map_idx = 0;
2190                 lp->state = FC_PORTDB_STATE_NIL;
2191                 isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2192                     "Gone Device Timeout");
2193                 isp_make_gone(isp, tgt);
2194         }
2195         if (more_to_do) {
2196                 isp->isp_osinfo.gdt = timeout(isp_gdt, isp, hz);
2197         } else {
2198                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2199                     "stopping Gone Device Timer");
2200                 isp->isp_osinfo.gdt_running = 0;
2201         }
2202         ISP_UNLOCK(isp);
2203 }
2204
2205 /*
2206  * Loop Down Timer Function- when loop goes down, a timer is started and
2207  * and after it expires we come here and take all probational devices that
2208  * the OS knows about and the tell the OS that they've gone away.
2209  * 
2210  * We don't clear the devices out of our port database because, when loop
2211  * come back up, we have to do some actual cleanup with the chip at that
2212  * point (implicit PLOGO, e.g., to get the chip's port database state right).
2213  */
2214 static void
2215 isp_ldt(void *arg)
2216 {
2217         ispsoftc_t *isp = arg;
2218         fcportdb_t *lp;
2219         int dbidx, tgt;
2220
2221         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Loop Down Timer expired");
2222         ISP_LOCK(isp);
2223
2224         /*
2225          * Notify to the OS all targets who we now consider have departed.
2226          */
2227         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2228                 lp = &FCPARAM(isp)->portdb[dbidx];
2229
2230                 if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
2231                         continue;
2232                 }
2233                 if (lp->ini_map_idx == 0) {
2234                         continue;
2235                 }
2236
2237                 /*
2238                  * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
2239                  */
2240
2241                 /*
2242                  * Mark that we've announced that this device is gone....
2243                  */
2244                 lp->reserved = 1;
2245
2246                 /*
2247                  * but *don't* change the state of the entry. Just clear
2248                  * any target id stuff and announce to CAM that the
2249                  * device is gone. This way any necessary PLOGO stuff
2250                  * will happen when loop comes back up.
2251                  */
2252
2253                 tgt = lp->ini_map_idx - 1;
2254                 FCPARAM(isp)->isp_ini_map[tgt] = 0;
2255                 lp->ini_map_idx = 0;
2256                 isp_prt(isp, ISP_LOGCONFIG, prom3, lp->portid, tgt,
2257                     "Loop Down Timeout");
2258                 isp_make_gone(isp, tgt);
2259         }
2260
2261         /*
2262          * The loop down timer has expired. Wake up the kthread
2263          * to notice that fact (or make it false).
2264          */
2265         isp->isp_osinfo.loop_down_time = isp->isp_osinfo.loop_down_limit+1;
2266 #if __FreeBSD_version < 500000  
2267         wakeup(&isp->isp_osinfo.kproc);
2268 #else
2269 #ifdef  ISP_SMPLOCK
2270         cv_signal(&isp->isp_osinfo.kthread_cv);
2271 #else
2272         wakeup(&isp->isp_osinfo.kthread_cv);
2273 #endif
2274 #endif
2275         ISP_UNLOCK(isp);
2276 }
2277
2278 static void
2279 isp_kthread(void *arg)
2280 {
2281         ispsoftc_t *isp = arg;
2282         int slp = 0;
2283 #if __FreeBSD_version < 500000  
2284         int s;
2285
2286         s = splcam();
2287 #else
2288 #ifdef  ISP_SMPLOCK
2289         mtx_lock(&isp->isp_lock);
2290 #else
2291         mtx_lock(&Giant);
2292 #endif
2293 #endif
2294         /*
2295          * The first loop is for our usage where we have yet to have
2296          * gotten good fibre channel state.
2297          */
2298         for (;;) {
2299                 int wasfrozen, lb, lim;
2300
2301                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2302                     "isp_kthread: checking FC state");
2303                 isp->isp_osinfo.mbox_sleep_ok = 1;
2304                 lb = isp_fc_runstate(isp, 250000);
2305                 isp->isp_osinfo.mbox_sleep_ok = 0;
2306                 if (lb) {
2307                         /*
2308                          * Increment loop down time by the last sleep interval
2309                          */
2310                         isp->isp_osinfo.loop_down_time += slp;
2311
2312                         if (lb < 0) {
2313                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2314                                     "kthread: FC loop not up (down count %d)",
2315                                     isp->isp_osinfo.loop_down_time);
2316                         } else {
2317                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2318                                     "kthread: FC got to %d (down count %d)",
2319                                     lb, isp->isp_osinfo.loop_down_time);
2320                         }
2321
2322
2323                         /*
2324                          * If we've never seen loop up and we've waited longer
2325                          * than quickboot time, or we've seen loop up but we've
2326                          * waited longer than loop_down_limit, give up and go
2327                          * to sleep until loop comes up.
2328                          */
2329                         if (FCPARAM(isp)->loop_seen_once == 0) {
2330                                 lim = isp_quickboot_time;
2331                         } else {
2332                                 lim = isp->isp_osinfo.loop_down_limit;
2333                         }
2334                         if (isp->isp_osinfo.loop_down_time >= lim) {
2335                                 isp_freeze_loopdown(isp, "loop limit hit");
2336                                 slp = 0;
2337                         } else if (isp->isp_osinfo.loop_down_time < 10) {
2338                                 slp = 1;
2339                         } else if (isp->isp_osinfo.loop_down_time < 30) {
2340                                 slp = 5;
2341                         } else if (isp->isp_osinfo.loop_down_time < 60) {
2342                                 slp = 10;
2343                         } else if (isp->isp_osinfo.loop_down_time < 120) {
2344                                 slp = 20;
2345                         } else {
2346                                 slp = 30;
2347                         }
2348
2349                 } else {
2350                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2351                             "isp_kthread: FC state OK");
2352                         isp->isp_osinfo.loop_down_time = 0;
2353                         slp = 0;
2354                 }
2355
2356                 /*
2357                  * If we'd frozen the simq, unfreeze it now so that CAM
2358                  * can start sending us commands. If the FC state isn't
2359                  * okay yet, they'll hit that in isp_start which will
2360                  * freeze the queue again.
2361                  */
2362                 wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
2363                 isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
2364                 if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
2365                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2366                             "isp_kthread: releasing simq");
2367                         ISPLOCK_2_CAMLOCK(isp);
2368                         xpt_release_simq(isp->isp_sim, 1);
2369                         CAMLOCK_2_ISPLOCK(isp);
2370                 }
2371                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2372                     "isp_kthread: sleep time %d", slp);
2373 #if __FreeBSD_version < 500000  
2374                 tsleep(&isp->isp_osinfo.kproc, PRIBIO, "ispf",
2375                     slp * hz);
2376 #else
2377 #ifdef  ISP_SMPLOCK
2378                 cv_timed_wait(&isp->isp_osinfo.kthread_cv, &isp->isp_lock,
2379                     slp * hz);
2380 #else
2381                 (void) tsleep(&isp->isp_osinfo.kthread_cv, PRIBIO, "ispf",
2382                     slp * hz);
2383 #endif
2384 #endif
2385                 /*
2386                  * If slp is zero, we're waking up for the first time after
2387                  * things have been okay. In this case, we set a deferral state
2388                  * for all commands and delay hysteresis seconds before starting
2389                  * the FC state evaluation. This gives the loop/fabric a chance
2390                  * to settle.
2391                  */
2392                 if (slp == 0 && isp->isp_osinfo.hysteresis) {
2393                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2394                             "isp_kthread: sleep hysteresis tick time %d",
2395                             isp->isp_osinfo.hysteresis * hz);
2396                         (void) tsleep(&isp_fabric_hysteresis, PRIBIO, "ispT",
2397                             (isp->isp_osinfo.hysteresis * hz));
2398                 }
2399         }
2400 }
2401
2402 static void
2403 isp_action(struct cam_sim *sim, union ccb *ccb)
2404 {
2405         int bus, tgt, error, lim;
2406         ispsoftc_t *isp;
2407         struct ccb_trans_settings *cts;
2408
2409         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
2410         
2411         isp = (ispsoftc_t *)cam_sim_softc(sim);
2412         ccb->ccb_h.sim_priv.entries[0].field = 0;
2413         ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2414         if (isp->isp_state != ISP_RUNSTATE &&
2415             ccb->ccb_h.func_code == XPT_SCSI_IO) {
2416                 CAMLOCK_2_ISPLOCK(isp);
2417                 isp_init(isp);
2418                 if (isp->isp_state != ISP_INITSTATE) {
2419                         ISP_UNLOCK(isp);
2420                         /*
2421                          * Lie. Say it was a selection timeout.
2422                          */
2423                         ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
2424                         xpt_freeze_devq(ccb->ccb_h.path, 1);
2425                         xpt_done(ccb);
2426                         return;
2427                 }
2428                 isp->isp_state = ISP_RUNSTATE;
2429                 ISPLOCK_2_CAMLOCK(isp);
2430         }
2431         isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2432
2433
2434         switch (ccb->ccb_h.func_code) {
2435         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
2436                 /*
2437                  * Do a couple of preliminary checks...
2438                  */
2439                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2440                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2441                                 ccb->ccb_h.status = CAM_REQ_INVALID;
2442                                 xpt_done(ccb);
2443                                 break;
2444                         }
2445                 }
2446 #ifdef  DIAGNOSTIC
2447                 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
2448                         xpt_print(ccb->ccb_h.path, "invalid target\n");
2449                         ccb->ccb_h.status = CAM_PATH_INVALID;
2450                 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
2451                         xpt_print(ccb->ccb_h.path, "invalid lun\n");
2452                         ccb->ccb_h.status = CAM_PATH_INVALID;
2453                 }
2454                 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2455                         xpt_done(ccb);
2456                         break;
2457                 }
2458 #endif
2459                 ((struct ccb_scsiio *) ccb)->scsi_status = SCSI_STATUS_OK;
2460                 CAMLOCK_2_ISPLOCK(isp);
2461                 error = isp_start((XS_T *) ccb);
2462                 switch (error) {
2463                 case CMD_QUEUED:
2464                         XS_CMD_S_CLEAR(ccb);
2465                         ISPLOCK_2_CAMLOCK(isp);
2466                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
2467                         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2468                                 int ms = ccb->ccb_h.timeout;
2469                                 if (ms == CAM_TIME_DEFAULT) {
2470                                         ms = 60*1000;
2471                                 }
2472                                 ccb->ccb_h.timeout_ch =
2473                                     timeout(isp_watchdog, ccb, isp_mstohz(ms));
2474                         } else {
2475                                 callout_handle_init(&ccb->ccb_h.timeout_ch);
2476                         }
2477                         break;
2478                 case CMD_RQLATER:
2479                         /*
2480                          * This can only happen for Fibre Channel
2481                          */
2482                         KASSERT((IS_FC(isp)), ("CMD_RQLATER for FC only"));
2483
2484                         /*
2485                          * Handle initial and subsequent loop down cases
2486                          */
2487                         if (FCPARAM(isp)->loop_seen_once == 0) {
2488                                 lim = isp_quickboot_time;
2489                         } else {
2490                                 lim = isp->isp_osinfo.loop_down_limit;
2491                         }
2492                         if (isp->isp_osinfo.loop_down_time >= lim) {
2493                                 isp_prt(isp, ISP_LOGDEBUG0,
2494                                     "%d.%d downtime (%d) > lim (%d)",
2495                                     XS_TGT(ccb), XS_LUN(ccb),
2496                                     isp->isp_osinfo.loop_down_time, lim);
2497                                 ccb->ccb_h.status =
2498                                     CAM_SEL_TIMEOUT|CAM_DEV_QFRZN;
2499                                 xpt_freeze_devq(ccb->ccb_h.path, 1);
2500                                 ISPLOCK_2_CAMLOCK(isp);
2501                                 xpt_done(ccb);
2502                                 break;
2503                         }
2504                         isp_prt(isp, ISP_LOGDEBUG0,
2505                             "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
2506                         /*
2507                          * Otherwise, retry in a while.
2508                          */
2509                         ISPLOCK_2_CAMLOCK(isp);
2510                         cam_freeze_devq(ccb->ccb_h.path);
2511                         cam_release_devq(ccb->ccb_h.path,
2512                             RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
2513                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
2514                         xpt_done(ccb);
2515                         break;
2516                 case CMD_EAGAIN:
2517                         ISPLOCK_2_CAMLOCK(isp);
2518                         cam_freeze_devq(ccb->ccb_h.path);
2519                         cam_release_devq(ccb->ccb_h.path,
2520                             RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
2521                         xpt_done(ccb);
2522                         break;
2523                 case CMD_COMPLETE:
2524                         isp_done((struct ccb_scsiio *) ccb);
2525                         ISPLOCK_2_CAMLOCK(isp);
2526                         break;
2527                 default:
2528                         ISPLOCK_2_CAMLOCK(isp);
2529                         isp_prt(isp, ISP_LOGERR,
2530                             "What's this? 0x%x at %d in file %s",
2531                             error, __LINE__, __FILE__);
2532                         XS_SETERR(ccb, CAM_REQ_CMP_ERR);
2533                         xpt_done(ccb);
2534                 }
2535                 break;
2536
2537 #ifdef  ISP_TARGET_MODE
2538         case XPT_EN_LUN:                /* Enable LUN as a target */
2539         {
2540                 int seq, i;
2541                 CAMLOCK_2_ISPLOCK(isp);
2542                 seq = isp_en_lun(isp, ccb);
2543                 if (seq < 0) {
2544                         ISPLOCK_2_CAMLOCK(isp);
2545                         xpt_done(ccb);
2546                         break;
2547                 }
2548                 for (i = 0; isp->isp_osinfo.leact[seq] && i < 30 * 1000; i++) {
2549                         uint32_t isr;
2550                         uint16_t sema, mbox;
2551                         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
2552                                 isp_intr(isp, isr, sema, mbox);
2553                         }
2554                         DELAY(1000);
2555                 }
2556                 ISPLOCK_2_CAMLOCK(isp);
2557                 break;
2558         }
2559         case XPT_NOTIFY_ACK:            /* recycle notify ack */
2560         case XPT_IMMED_NOTIFY:          /* Add Immediate Notify Resource */
2561         case XPT_ACCEPT_TARGET_IO:      /* Add Accept Target IO Resource */
2562         {
2563                 tstate_t *tptr =
2564                     get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2565                 if (tptr == NULL) {
2566                         ccb->ccb_h.status = CAM_LUN_INVALID;
2567                         xpt_done(ccb);
2568                         break;
2569                 }
2570                 ccb->ccb_h.sim_priv.entries[0].field = 0;
2571                 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2572                 ccb->ccb_h.flags = 0;
2573
2574                 CAMLOCK_2_ISPLOCK(isp);
2575                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
2576                         /*
2577                          * Note that the command itself may not be done-
2578                          * it may not even have had the first CTIO sent.
2579                          */
2580                         tptr->atio_count++;
2581                         isp_prt(isp, ISP_LOGTDEBUG0,
2582                             "Put FREE ATIO, lun %d, count now %d",
2583                             ccb->ccb_h.target_lun, tptr->atio_count);
2584                         SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h,
2585                             sim_links.sle);
2586                 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
2587                         tptr->inot_count++;
2588                         isp_prt(isp, ISP_LOGTDEBUG0,
2589                             "Put FREE INOT, lun %d, count now %d",
2590                             ccb->ccb_h.target_lun, tptr->inot_count);
2591                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
2592                             sim_links.sle);
2593                 } else {
2594                         isp_prt(isp, ISP_LOGWARN, "Got Notify ACK");;
2595                 }
2596                 rls_lun_statep(isp, tptr);
2597                 ccb->ccb_h.status = CAM_REQ_INPROG;
2598                 ISPLOCK_2_CAMLOCK(isp);
2599                 break;
2600         }
2601         case XPT_CONT_TARGET_IO:
2602         {
2603                 CAMLOCK_2_ISPLOCK(isp);
2604                 isp_target_start_ctio(isp, ccb);
2605                 ISPLOCK_2_CAMLOCK(isp);
2606                 break;
2607         }
2608 #endif
2609         case XPT_RESET_DEV:             /* BDR the specified SCSI device */
2610
2611                 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
2612                 tgt = ccb->ccb_h.target_id;
2613                 tgt |= (bus << 16);
2614
2615                 CAMLOCK_2_ISPLOCK(isp);
2616                 error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
2617                 ISPLOCK_2_CAMLOCK(isp);
2618                 if (error) {
2619                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2620                 } else {
2621                         ccb->ccb_h.status = CAM_REQ_CMP;
2622                 }
2623                 xpt_done(ccb);
2624                 break;
2625         case XPT_ABORT:                 /* Abort the specified CCB */
2626         {
2627                 union ccb *accb = ccb->cab.abort_ccb;
2628                 CAMLOCK_2_ISPLOCK(isp);
2629                 switch (accb->ccb_h.func_code) {
2630 #ifdef  ISP_TARGET_MODE
2631                 case XPT_ACCEPT_TARGET_IO:
2632                 case XPT_IMMED_NOTIFY:
2633                         ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
2634                         break;
2635                 case XPT_CONT_TARGET_IO:
2636                         isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
2637                         ccb->ccb_h.status = CAM_UA_ABORT;
2638                         break;
2639 #endif
2640                 case XPT_SCSI_IO:
2641                         error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
2642                         if (error) {
2643                                 ccb->ccb_h.status = CAM_UA_ABORT;
2644                         } else {
2645                                 ccb->ccb_h.status = CAM_REQ_CMP;
2646                         }
2647                         break;
2648                 default:
2649                         ccb->ccb_h.status = CAM_REQ_INVALID;
2650                         break;
2651                 }
2652                 ISPLOCK_2_CAMLOCK(isp);
2653                 xpt_done(ccb);
2654                 break;
2655         }
2656 #define IS_CURRENT_SETTINGS(c)  (c->type == CTS_TYPE_CURRENT_SETTINGS)
2657         case XPT_SET_TRAN_SETTINGS:     /* Nexus Settings */
2658                 cts = &ccb->cts;
2659                 if (!IS_CURRENT_SETTINGS(cts)) {
2660                         ccb->ccb_h.status = CAM_REQ_INVALID;
2661                         xpt_done(ccb);
2662                         break;
2663                 }
2664                 tgt = cts->ccb_h.target_id;
2665                 CAMLOCK_2_ISPLOCK(isp);
2666                 if (IS_SCSI(isp)) {
2667                         struct ccb_trans_settings_scsi *scsi =
2668                             &cts->proto_specific.scsi;
2669                         struct ccb_trans_settings_spi *spi =
2670                             &cts->xport_specific.spi;
2671                         sdparam *sdp = isp->isp_param;
2672                         uint16_t *dptr;
2673
2674                         if (spi->valid == 0 && scsi->valid == 0) {
2675                                 ISPLOCK_2_CAMLOCK(isp);
2676                                 ccb->ccb_h.status = CAM_REQ_CMP;
2677                                 xpt_done(ccb);
2678                                 break;
2679                         }
2680                                 
2681                         bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2682                         sdp += bus;
2683                         /*
2684                          * We always update (internally) from goal_flags
2685                          * so any request to change settings just gets
2686                          * vectored to that location.
2687                          */
2688                         dptr = &sdp->isp_devparam[tgt].goal_flags;
2689
2690                         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
2691                                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
2692                                         *dptr |= DPARM_DISC;
2693                                 else
2694                                         *dptr &= ~DPARM_DISC;
2695                         }
2696
2697                         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2698                                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
2699                                         *dptr |= DPARM_TQING;
2700                                 else
2701                                         *dptr &= ~DPARM_TQING;
2702                         }
2703
2704                         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
2705                                 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
2706                                         *dptr |= DPARM_WIDE;
2707                                 else
2708                                         *dptr &= ~DPARM_WIDE;
2709                         }
2710
2711                         /*
2712                          * XXX: FIX ME
2713                          */
2714                         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) &&
2715                             (spi->valid & CTS_SPI_VALID_SYNC_RATE) &&
2716                             (spi->sync_period && spi->sync_offset)) {
2717                                 *dptr |= DPARM_SYNC;
2718                                 /*
2719                                  * XXX: CHECK FOR LEGALITY
2720                                  */
2721                                 sdp->isp_devparam[tgt].goal_period =
2722                                     spi->sync_period;
2723                                 sdp->isp_devparam[tgt].goal_offset =
2724                                     spi->sync_offset;
2725                         } else {
2726                                 *dptr &= ~DPARM_SYNC;
2727                         }
2728                         isp_prt(isp, ISP_LOGDEBUG0,
2729                             "SET (%d.%d.%d) to flags %x off %x per %x",
2730                             bus, tgt, cts->ccb_h.target_lun,
2731                             sdp->isp_devparam[tgt].goal_flags,
2732                             sdp->isp_devparam[tgt].goal_offset,
2733                             sdp->isp_devparam[tgt].goal_period);
2734                         sdp->isp_devparam[tgt].dev_update = 1;
2735                         isp->isp_update |= (1 << bus);
2736                 }
2737                 ISPLOCK_2_CAMLOCK(isp);
2738                 ccb->ccb_h.status = CAM_REQ_CMP;
2739                 xpt_done(ccb);
2740                 break;
2741         case XPT_GET_TRAN_SETTINGS:
2742                 cts = &ccb->cts;
2743                 tgt = cts->ccb_h.target_id;
2744                 CAMLOCK_2_ISPLOCK(isp);
2745                 if (IS_FC(isp)) {
2746                         fcparam *fcp = isp->isp_param;
2747                         struct ccb_trans_settings_scsi *scsi =
2748                             &cts->proto_specific.scsi;
2749                         struct ccb_trans_settings_fc *fc =
2750                             &cts->xport_specific.fc;
2751
2752                         cts->protocol = PROTO_SCSI;
2753                         cts->protocol_version = SCSI_REV_2;
2754                         cts->transport = XPORT_FC;
2755                         cts->transport_version = 0;
2756
2757                         scsi->valid = CTS_SCSI_VALID_TQ;
2758                         scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2759                         fc->valid = CTS_FC_VALID_SPEED;
2760                         if (fcp->isp_gbspeed == 2) {
2761                                 fc->bitrate = 200000;
2762                         } else {
2763                                 fc->bitrate = 100000;
2764                         }
2765                         if (tgt > 0 && tgt < MAX_FC_TARG) {
2766                                 fcportdb_t *lp = &fcp->portdb[tgt];
2767                                 fc->wwnn = lp->node_wwn;
2768                                 fc->wwpn = lp->port_wwn;
2769                                 fc->port = lp->portid;
2770                                 fc->valid |= CTS_FC_VALID_WWNN |
2771                                     CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
2772                         }
2773                 } else {
2774                         struct ccb_trans_settings_scsi *scsi =
2775                             &cts->proto_specific.scsi;
2776                         struct ccb_trans_settings_spi *spi =
2777                             &cts->xport_specific.spi;
2778                         sdparam *sdp = isp->isp_param;
2779                         int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2780                         uint16_t dval, pval, oval;
2781
2782                         sdp += bus;
2783
2784                         if (IS_CURRENT_SETTINGS(cts)) {
2785                                 sdp->isp_devparam[tgt].dev_refresh = 1;
2786                                 isp->isp_update |= (1 << bus);
2787                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2788                                     NULL);
2789                                 dval = sdp->isp_devparam[tgt].actv_flags;
2790                                 oval = sdp->isp_devparam[tgt].actv_offset;
2791                                 pval = sdp->isp_devparam[tgt].actv_period;
2792                         } else {
2793                                 dval = sdp->isp_devparam[tgt].nvrm_flags;
2794                                 oval = sdp->isp_devparam[tgt].nvrm_offset;
2795                                 pval = sdp->isp_devparam[tgt].nvrm_period;
2796                         }
2797
2798                         cts->protocol = PROTO_SCSI;
2799                         cts->protocol_version = SCSI_REV_2;
2800                         cts->transport = XPORT_SPI;
2801                         cts->transport_version = 2;
2802
2803                         spi->valid = 0;
2804                         scsi->valid = 0;
2805                         spi->flags = 0;
2806                         scsi->flags = 0;
2807                         if (dval & DPARM_DISC) {
2808                                 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2809                         }
2810                         if ((dval & DPARM_SYNC) && oval && pval) {
2811                                 spi->sync_offset = oval;
2812                                 spi->sync_period = pval;
2813                         } else {
2814                                 spi->sync_offset = 0;
2815                                 spi->sync_period = 0;
2816                         }
2817                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2818                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2819                         spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2820                         if (dval & DPARM_WIDE) {
2821                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2822                         } else {
2823                                 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2824                         }
2825                         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2826                                 scsi->valid = CTS_SCSI_VALID_TQ;
2827                                 if (dval & DPARM_TQING) {
2828                                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2829                                 }
2830                                 spi->valid |= CTS_SPI_VALID_DISC;
2831                         }
2832                         isp_prt(isp, ISP_LOGDEBUG0,
2833                             "GET %s (%d.%d.%d) to flags %x off %x per %x",
2834                             IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2835                             bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
2836                 }
2837                 ISPLOCK_2_CAMLOCK(isp);
2838                 ccb->ccb_h.status = CAM_REQ_CMP;
2839                 xpt_done(ccb);
2840                 break;
2841
2842         case XPT_CALC_GEOMETRY:
2843 #if __FreeBSD_version < 500000  
2844         {
2845                 struct ccb_calc_geometry *ccg;
2846                 u_int32_t secs_per_cylinder;
2847                 u_int32_t size_mb;
2848
2849                 ccg = &ccb->ccg;
2850                 if (ccg->block_size == 0) {
2851                         ccb->ccb_h.status = CAM_REQ_INVALID;
2852                         xpt_done(ccb);
2853                         break;
2854                 }
2855                 size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2856                 if (size_mb > 1024) {
2857                         ccg->heads = 255;
2858                         ccg->secs_per_track = 63;
2859                 } else {
2860                         ccg->heads = 64;
2861                         ccg->secs_per_track = 32;
2862                 }
2863                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2864                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2865                 ccb->ccb_h.status = CAM_REQ_CMP;
2866                 xpt_done(ccb);
2867                 break;
2868         }
2869 #else
2870         {
2871                 cam_calc_geometry(&ccb->ccg, /*extended*/1);
2872                 xpt_done(ccb);
2873                 break;
2874         }
2875 #endif
2876         case XPT_RESET_BUS:             /* Reset the specified bus */
2877                 bus = cam_sim_bus(sim);
2878                 CAMLOCK_2_ISPLOCK(isp);
2879                 error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2880                 ISPLOCK_2_CAMLOCK(isp);
2881                 if (error)
2882                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2883                 else {
2884                         if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2885                                 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2886                         else if (isp->isp_path != NULL)
2887                                 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2888                         ccb->ccb_h.status = CAM_REQ_CMP;
2889                 }
2890                 xpt_done(ccb);
2891                 break;
2892
2893         case XPT_TERM_IO:               /* Terminate the I/O process */
2894                 ccb->ccb_h.status = CAM_REQ_INVALID;
2895                 xpt_done(ccb);
2896                 break;
2897
2898         case XPT_PATH_INQ:              /* Path routing inquiry */
2899         {
2900                 struct ccb_pathinq *cpi = &ccb->cpi;
2901
2902                 cpi->version_num = 1;
2903 #ifdef  ISP_TARGET_MODE
2904                 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2905 #else
2906                 cpi->target_sprt = 0;
2907 #endif
2908                 cpi->hba_eng_cnt = 0;
2909                 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
2910                 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
2911                 cpi->bus_id = cam_sim_bus(sim);
2912                 if (IS_FC(isp)) {
2913                         cpi->hba_misc = PIM_NOBUSRESET;
2914                         /*
2915                          * Because our loop ID can shift from time to time,
2916                          * make our initiator ID out of range of our bus.
2917                          */
2918                         cpi->initiator_id = cpi->max_target + 1;
2919
2920                         /*
2921                          * Set base transfer capabilities for Fibre Channel.
2922                          * Technically not correct because we don't know
2923                          * what media we're running on top of- but we'll
2924                          * look good if we always say 100MB/s.
2925                          */
2926                         if (FCPARAM(isp)->isp_gbspeed == 2)
2927                                 cpi->base_transfer_speed = 200000;
2928                         else
2929                                 cpi->base_transfer_speed = 100000;
2930                         cpi->hba_inquiry = PI_TAG_ABLE;
2931                         cpi->transport = XPORT_FC;
2932                         cpi->transport_version = 0;     /* WHAT'S THIS FOR? */
2933                 } else {
2934                         sdparam *sdp = isp->isp_param;
2935                         sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
2936                         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
2937                         cpi->hba_misc = 0;
2938                         cpi->initiator_id = sdp->isp_initiator_id;
2939                         cpi->base_transfer_speed = 3300;
2940                         cpi->transport = XPORT_SPI;
2941                         cpi->transport_version = 2;     /* WHAT'S THIS FOR? */
2942                 }
2943                 cpi->protocol = PROTO_SCSI;
2944                 cpi->protocol_version = SCSI_REV_2;
2945                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2946                 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
2947                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2948                 cpi->unit_number = cam_sim_unit(sim);
2949                 cpi->ccb_h.status = CAM_REQ_CMP;
2950                 xpt_done(ccb);
2951                 break;
2952         }
2953         default:
2954                 ccb->ccb_h.status = CAM_REQ_INVALID;
2955                 xpt_done(ccb);
2956                 break;
2957         }
2958 }
2959
2960 #define ISPDDB  (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
2961
2962 void
2963 isp_done(struct ccb_scsiio *sccb)
2964 {
2965         ispsoftc_t *isp = XS_ISP(sccb);
2966
2967         if (XS_NOERR(sccb))
2968                 XS_SETERR(sccb, CAM_REQ_CMP);
2969
2970         if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
2971             (sccb->scsi_status != SCSI_STATUS_OK)) {
2972                 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
2973                 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && 
2974                     (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
2975                         sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
2976                 } else {
2977                         sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2978                 }
2979         }
2980
2981         sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2982         if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2983                 isp_prt(isp, ISP_LOGDEBUG0,
2984                     "target %d lun %d CAM status 0x%x SCSI status 0x%x",
2985                     XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status,
2986                     sccb->scsi_status);
2987                 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2988                         sccb->ccb_h.status |= CAM_DEV_QFRZN;
2989                         xpt_freeze_devq(sccb->ccb_h.path, 1);
2990                 }
2991         }
2992
2993         if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
2994             (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2995                 xpt_print(sccb->ccb_h.path,
2996                     "cam completion status 0x%x\n", sccb->ccb_h.status);
2997         }
2998
2999         XS_CMD_S_DONE(sccb);
3000         if (XS_CMD_WDOG_P(sccb) == 0) {
3001                 untimeout(isp_watchdog, sccb, sccb->ccb_h.timeout_ch);
3002                 if (XS_CMD_GRACE_P(sccb)) {
3003                         isp_prt(isp, ISP_LOGDEBUG2,
3004                             "finished command on borrowed time");
3005                 }
3006                 XS_CMD_S_CLEAR(sccb);
3007                 ISPLOCK_2_CAMLOCK(isp);
3008                 xpt_done((union ccb *) sccb);
3009                 CAMLOCK_2_ISPLOCK(isp);
3010         }
3011 }
3012
3013 int
3014 isp_async(ispsoftc_t *isp, ispasync_t cmd, void *arg)
3015 {
3016         int bus, rv = 0;
3017         static const char prom[] =
3018             "PortID 0x%06x handle 0x%x role %s %s\n"
3019             "      WWNN 0x%08x%08x WWPN 0x%08x%08x";
3020         static const char prom2[] =
3021             "PortID 0x%06x handle 0x%x role %s %s tgt %u\n"
3022             "      WWNN 0x%08x%08x WWPN 0x%08x%08x";
3023         char *msg = NULL;
3024         target_id_t tgt;
3025         fcportdb_t *lp;
3026         struct cam_path *tmppath;
3027
3028         switch (cmd) {
3029         case ISPASYNC_NEW_TGT_PARAMS:
3030         {
3031                 struct ccb_trans_settings_scsi *scsi;
3032                 struct ccb_trans_settings_spi *spi;
3033                 int flags, tgt;
3034                 sdparam *sdp = isp->isp_param;
3035                 struct ccb_trans_settings cts;
3036
3037                 memset(&cts, 0, sizeof (struct ccb_trans_settings));
3038
3039                 tgt = *((int *)arg);
3040                 bus = (tgt >> 16) & 0xffff;
3041                 tgt &= 0xffff;
3042                 sdp += bus;
3043                 ISPLOCK_2_CAMLOCK(isp);
3044                 if (xpt_create_path(&tmppath, NULL,
3045                     cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
3046                     tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
3047                         CAMLOCK_2_ISPLOCK(isp);
3048                         isp_prt(isp, ISP_LOGWARN,
3049                             "isp_async cannot make temp path for %d.%d",
3050                             tgt, bus);
3051                         rv = -1;
3052                         break;
3053                 }
3054                 CAMLOCK_2_ISPLOCK(isp);
3055                 flags = sdp->isp_devparam[tgt].actv_flags;
3056                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
3057                 cts.protocol = PROTO_SCSI;
3058                 cts.transport = XPORT_SPI;
3059
3060                 scsi = &cts.proto_specific.scsi;
3061                 spi = &cts.xport_specific.spi;
3062
3063                 if (flags & DPARM_TQING) {
3064                         scsi->valid |= CTS_SCSI_VALID_TQ;
3065                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3066                 }
3067
3068                 if (flags & DPARM_DISC) {
3069                         spi->valid |= CTS_SPI_VALID_DISC;
3070                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3071                 }
3072                 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
3073                 if (flags & DPARM_WIDE) {
3074                         spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3075                 } else {
3076                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3077                 }
3078                 if (flags & DPARM_SYNC) {
3079                         spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3080                         spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3081                         spi->sync_period = sdp->isp_devparam[tgt].actv_period;
3082                         spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
3083                 }
3084                 isp_prt(isp, ISP_LOGDEBUG2,
3085                     "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
3086                     bus, tgt, sdp->isp_devparam[tgt].actv_period,
3087                     sdp->isp_devparam[tgt].actv_offset, flags);
3088                 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
3089                 ISPLOCK_2_CAMLOCK(isp);
3090                 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
3091                 xpt_free_path(tmppath);
3092                 CAMLOCK_2_ISPLOCK(isp);
3093                 break;
3094         }
3095         case ISPASYNC_BUS_RESET:
3096                 bus = *((int *)arg);
3097                 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
3098                     bus);
3099                 if (bus > 0 && isp->isp_path2) {
3100                         ISPLOCK_2_CAMLOCK(isp);
3101                         xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
3102                         CAMLOCK_2_ISPLOCK(isp);
3103                 } else if (isp->isp_path) {
3104                         ISPLOCK_2_CAMLOCK(isp);
3105                         xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
3106                         CAMLOCK_2_ISPLOCK(isp);
3107                 }
3108                 break;
3109         case ISPASYNC_LIP:
3110                 if (msg == NULL) {
3111                         msg = "LIP Received";
3112                 }
3113                 /* FALLTHROUGH */
3114         case ISPASYNC_LOOP_RESET:
3115                 if (msg == NULL) {
3116                         msg = "LOOP Reset";
3117                 }
3118                 /* FALLTHROUGH */
3119         case ISPASYNC_LOOP_DOWN:
3120                 if (msg == NULL) {
3121                         msg = "LOOP Down";
3122                 }
3123                 if (isp->isp_path) {
3124                         isp_freeze_loopdown(isp, msg);
3125                 }
3126                 if (isp->isp_osinfo.ldt_running == 0) {
3127                         isp->isp_osinfo.ldt = timeout(isp_ldt, isp,
3128                             isp->isp_osinfo.loop_down_limit * hz);
3129                         isp->isp_osinfo.ldt_running = 1;
3130                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3131                            "starting Loop Down Timer");
3132                 }
3133                 isp_prt(isp, ISP_LOGINFO, msg);
3134                 break;
3135         case ISPASYNC_LOOP_UP:
3136                 /*
3137                  * Now we just note that Loop has come up. We don't
3138                  * actually do anything because we're waiting for a
3139                  * Change Notify before activating the FC cleanup
3140                  * thread to look at the state of the loop again.
3141                  */
3142                 isp_prt(isp, ISP_LOGINFO, "Loop UP");
3143                 break;
3144         case ISPASYNC_DEV_ARRIVED:
3145                 lp = arg;
3146                 lp->reserved = 0;
3147                 if ((isp->isp_role & ISP_ROLE_INITIATOR) &&
3148                     (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
3149                         int dbidx = lp - FCPARAM(isp)->portdb;
3150                         int i;
3151
3152                         for (i = 0; i < MAX_FC_TARG; i++) {
3153                                 if (i >= FL_ID && i <= SNS_ID) {
3154                                         continue;
3155                                 }
3156                                 if (FCPARAM(isp)->isp_ini_map[i] == 0) {
3157                                         break;
3158                                 }
3159                         }
3160                         if (i < MAX_FC_TARG) {
3161                                 FCPARAM(isp)->isp_ini_map[i] = dbidx + 1;
3162                                 lp->ini_map_idx = i + 1;
3163                         } else {
3164                                 isp_prt(isp, ISP_LOGWARN, "out of target ids");
3165                                 isp_dump_portdb(isp);
3166                         }
3167                 }
3168                 if (lp->ini_map_idx) {
3169                         tgt = lp->ini_map_idx - 1;
3170                         isp_prt(isp, ISP_LOGCONFIG, prom2,
3171                             lp->portid, lp->handle,
3172                             roles[lp->roles], "arrived at", tgt,
3173                             (uint32_t) (lp->node_wwn >> 32),
3174                             (uint32_t) lp->node_wwn,
3175                             (uint32_t) (lp->port_wwn >> 32),
3176                             (uint32_t) lp->port_wwn);
3177                         isp_make_here(isp, tgt);
3178                 } else {
3179                         isp_prt(isp, ISP_LOGCONFIG, prom,
3180                             lp->portid, lp->handle,
3181                             roles[lp->roles], "arrived",
3182                             (uint32_t) (lp->node_wwn >> 32),
3183                             (uint32_t) lp->node_wwn,
3184                             (uint32_t) (lp->port_wwn >> 32),
3185                             (uint32_t) lp->port_wwn);
3186                 }
3187                 break;
3188         case ISPASYNC_DEV_CHANGED:
3189                 lp = arg;
3190                 if (isp_change_is_bad) {
3191                         lp->state = FC_PORTDB_STATE_NIL;
3192                         if (lp->ini_map_idx) {
3193                                 tgt = lp->ini_map_idx - 1;
3194                                 FCPARAM(isp)->isp_ini_map[tgt] = 0;
3195                                 lp->ini_map_idx = 0;
3196                                 isp_prt(isp, ISP_LOGCONFIG, prom3,
3197                                     lp->portid, tgt, "change is bad");
3198                                 isp_make_gone(isp, tgt);
3199                         } else {
3200                                 isp_prt(isp, ISP_LOGCONFIG, prom,
3201                                     lp->portid, lp->handle,
3202                                     roles[lp->roles],
3203                                     "changed and departed",
3204                                     (uint32_t) (lp->node_wwn >> 32),
3205                                     (uint32_t) lp->node_wwn,
3206                                     (uint32_t) (lp->port_wwn >> 32),
3207                                     (uint32_t) lp->port_wwn);
3208                         }
3209                 } else {
3210                         lp->portid = lp->new_portid;
3211                         lp->roles = lp->new_roles;
3212                         if (lp->ini_map_idx) {
3213                                 int t = lp->ini_map_idx - 1;
3214                                 FCPARAM(isp)->isp_ini_map[t] =
3215                                     (lp - FCPARAM(isp)->portdb) + 1;
3216                                 tgt = lp->ini_map_idx - 1;
3217                                 isp_prt(isp, ISP_LOGCONFIG, prom2,
3218                                     lp->portid, lp->handle,
3219                                     roles[lp->roles], "changed at", tgt,
3220                                     (uint32_t) (lp->node_wwn >> 32),
3221                                     (uint32_t) lp->node_wwn,
3222                                     (uint32_t) (lp->port_wwn >> 32),
3223                                     (uint32_t) lp->port_wwn);
3224                         } else {
3225                                 isp_prt(isp, ISP_LOGCONFIG, prom,
3226                                     lp->portid, lp->handle,
3227                                     roles[lp->roles], "changed",
3228                                     (uint32_t) (lp->node_wwn >> 32),
3229                                     (uint32_t) lp->node_wwn,
3230                                     (uint32_t) (lp->port_wwn >> 32),
3231                                     (uint32_t) lp->port_wwn);
3232                         }
3233                 }
3234                 break;
3235         case ISPASYNC_DEV_STAYED:
3236                 lp = arg;
3237                 if (lp->ini_map_idx) {
3238                         tgt = lp->ini_map_idx - 1;
3239                         isp_prt(isp, ISP_LOGCONFIG, prom2,
3240                             lp->portid, lp->handle,
3241                             roles[lp->roles], "stayed at", tgt,
3242                             (uint32_t) (lp->node_wwn >> 32),
3243                             (uint32_t) lp->node_wwn,
3244                             (uint32_t) (lp->port_wwn >> 32),
3245                             (uint32_t) lp->port_wwn);
3246                 } else {
3247                         isp_prt(isp, ISP_LOGCONFIG, prom,
3248                             lp->portid, lp->handle,
3249                             roles[lp->roles], "stayed",
3250                             (uint32_t) (lp->node_wwn >> 32),
3251                             (uint32_t) lp->node_wwn,
3252                             (uint32_t) (lp->port_wwn >> 32),
3253                             (uint32_t) lp->port_wwn);
3254                 }
3255                 break;
3256         case ISPASYNC_DEV_GONE:
3257                 lp = arg;
3258                 /*
3259                  * If this has a virtual target and we haven't marked it
3260                  * that we're going to have isp_gdt tell the OS it's gone,
3261                  * set the isp_gdt timer running on it.
3262                  *
3263                  * If it isn't marked that isp_gdt is going to get rid of it,
3264                  * announce that it's gone.
3265                  */
3266                 if (lp->ini_map_idx && lp->reserved == 0) {
3267                         lp->reserved = 1;
3268                         lp->new_reserved = isp->isp_osinfo.gone_device_time;
3269                         lp->state = FC_PORTDB_STATE_ZOMBIE;
3270                         if (isp->isp_osinfo.gdt_running == 0) {
3271                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3272                                     "starting Gone Device Timer");
3273                                 isp->isp_osinfo.gdt = timeout(isp_gdt, isp, hz);
3274                                 isp->isp_osinfo.gdt_running = 1;
3275                         }
3276                         tgt = lp->ini_map_idx - 1;
3277                         isp_prt(isp, ISP_LOGCONFIG, prom2,
3278                             lp->portid, lp->handle,
3279                             roles[lp->roles], "gone zombie at", tgt,
3280                             (uint32_t) (lp->node_wwn >> 32),
3281                             (uint32_t) lp->node_wwn,
3282                             (uint32_t) (lp->port_wwn >> 32),
3283                             (uint32_t) lp->port_wwn);
3284                 } else if (lp->reserved == 0) {
3285                         isp_prt(isp, ISP_LOGCONFIG, prom,
3286                             lp->portid, lp->handle,
3287                             roles[lp->roles], "departed",
3288                             (uint32_t) (lp->node_wwn >> 32),
3289                             (uint32_t) lp->node_wwn,
3290                             (uint32_t) (lp->port_wwn >> 32),
3291                             (uint32_t) lp->port_wwn);
3292                 }
3293                 break;
3294         case ISPASYNC_CHANGE_NOTIFY:
3295         {
3296                 char *msg;
3297                 if (arg == ISPASYNC_CHANGE_PDB) {
3298                         msg = "Port Database Changed";
3299                 } else if (arg == ISPASYNC_CHANGE_SNS) {
3300                         msg = "Name Server Database Changed";
3301                 } else {
3302                         msg = "Other Change Notify";
3303                 }
3304                 /*
3305                  * If the loop down timer is running, cancel it.
3306                  */
3307                 if (isp->isp_osinfo.ldt_running) {
3308                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3309                            "Stopping Loop Down Timer");
3310                         isp->isp_osinfo.ldt_running = 0;
3311                         untimeout(isp_ldt, isp, isp->isp_osinfo.ldt);
3312                         callout_handle_init(&isp->isp_osinfo.ldt);
3313                 }
3314                 isp_prt(isp, ISP_LOGINFO, msg);
3315                 isp_freeze_loopdown(isp, msg);
3316 #if __FreeBSD_version < 500000  
3317                 wakeup(&isp->isp_osinfo.kproc);
3318 #else
3319 #ifdef  ISP_SMPLOCK
3320                 cv_signal(&isp->isp_osinfo.kthread_cv);
3321 #else
3322                 wakeup(&isp->isp_osinfo.kthread_cv);
3323 #endif
3324 #endif
3325                 break;
3326         }
3327 #ifdef  ISP_TARGET_MODE
3328         case ISPASYNC_TARGET_NOTIFY:
3329         {
3330                 tmd_notify_t *nt = arg;
3331                 isp_prt(isp, ISP_LOGALL,
3332                     "target notify code 0x%x", nt->nt_ncode);
3333                 break;
3334         }
3335         case ISPASYNC_TARGET_ACTION:
3336                 switch (((isphdr_t *)arg)->rqs_entry_type) {
3337                 default:
3338                         isp_prt(isp, ISP_LOGWARN,
3339                            "event 0x%x for unhandled target action",
3340                             ((isphdr_t *)arg)->rqs_entry_type);
3341                         break;
3342                 case RQSTYPE_NOTIFY:
3343                         if (IS_SCSI(isp)) {
3344                                 rv = isp_handle_platform_notify_scsi(isp,
3345                                     (in_entry_t *) arg);
3346                         } else {
3347                                 rv = isp_handle_platform_notify_fc(isp,
3348                                     (in_fcentry_t *) arg);
3349                         }
3350                         break;
3351                 case RQSTYPE_ATIO:
3352                         rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
3353                         break;
3354                 case RQSTYPE_ATIO2:
3355                         rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
3356                         break;
3357                 case RQSTYPE_CTIO3:
3358                 case RQSTYPE_CTIO2:
3359                 case RQSTYPE_CTIO:
3360                         rv = isp_handle_platform_ctio(isp, arg);
3361                         break;
3362                 case RQSTYPE_ENABLE_LUN:
3363                 case RQSTYPE_MODIFY_LUN:
3364                         isp_ledone(isp, (lun_entry_t *) arg);
3365                         break;
3366                 }
3367                 break;
3368 #endif
3369         case ISPASYNC_FW_CRASH:
3370         {
3371                 uint16_t mbox1, mbox6;
3372                 mbox1 = ISP_READ(isp, OUTMAILBOX1);
3373                 if (IS_DUALBUS(isp)) { 
3374                         mbox6 = ISP_READ(isp, OUTMAILBOX6);
3375                 } else {
3376                         mbox6 = 0;
3377                 }
3378                 isp_prt(isp, ISP_LOGERR,
3379                     "Internal Firmware Error on bus %d @ RISC Address 0x%x",
3380                     mbox6, mbox1);
3381 #ifdef  ISP_FW_CRASH_DUMP
3382                 /*
3383                  * XXX: really need a thread to do this right.
3384                  */
3385                 if (IS_FC(isp)) {
3386                         FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3387                         FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3388                         isp_freeze_loopdown(isp, "f/w crash");
3389                         isp_fw_dump(isp);
3390                 }
3391                 isp_reinit(isp);
3392                 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3393 #endif
3394                 break;
3395         }
3396         case ISPASYNC_UNHANDLED_RESPONSE:
3397                 break;
3398         default:
3399                 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
3400                 break;
3401         }
3402         return (rv);
3403 }
3404
3405
3406 /*
3407  * Locks are held before coming here.
3408  */
3409 void
3410 isp_uninit(ispsoftc_t *isp)
3411 {
3412         if (IS_24XX(isp)) {
3413                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
3414         } else {
3415                 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
3416         }
3417         ISP_DISABLE_INTS(isp);
3418 }
3419
3420 void
3421 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
3422 {
3423         va_list ap;
3424         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
3425                 return;
3426         }
3427         printf("%s: ", device_get_nameunit(isp->isp_dev));
3428         va_start(ap, fmt);
3429         vprintf(fmt, ap);
3430         va_end(ap);
3431         printf("\n");
3432 }
3433
3434 uint64_t
3435 isp_nanotime_sub(struct timespec *b, struct timespec *a)
3436 {
3437         uint64_t elapsed;
3438         struct timespec x = *b;
3439         timespecsub(&x, a);
3440         elapsed = GET_NANOSEC(&x);
3441         if (elapsed == 0)
3442                 elapsed++;
3443         return (elapsed);
3444 }
3445
3446 int
3447 isp_mbox_acquire(ispsoftc_t *isp)
3448 {
3449         if (isp->isp_osinfo.mboxbsy) {
3450                 return (1);
3451         } else {
3452                 isp->isp_osinfo.mboxcmd_done = 0;
3453                 isp->isp_osinfo.mboxbsy = 1;
3454                 return (0);
3455         }
3456 }
3457
3458 void
3459 isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
3460 {
3461         int usecs = mbp->timeout;
3462         int j;
3463
3464         if (usecs == 0) {
3465                 usecs = MBCMD_DEFAULT_TIMEOUT;
3466         }
3467         if (isp->isp_mbxwrk0) {
3468                 usecs *= isp->isp_mbxwrk0;
3469         }
3470         if (isp->isp_osinfo.mbox_sleep_ok) {
3471                 int ms = usecs / 1000;
3472                 isp->isp_osinfo.mbox_sleep_ok = 0;
3473                 isp->isp_osinfo.mbox_sleeping = 1;
3474 #if __FreeBSD_version < 500000  || !defined(ISP_SMPLOCK)
3475                 tsleep(&isp->isp_mbxworkp, PRIBIO, "ispmbx_sleep",
3476                     isp_mstohz(ms));
3477 #else
3478                 msleep(&isp->isp_mbxworkp, &isp->isp_mtx, PRIBIO,
3479                     "ispmbx_sleep", isp_mstohz(ms));
3480 #endif
3481                 isp->isp_osinfo.mbox_sleep_ok = 1;
3482                 isp->isp_osinfo.mbox_sleeping = 0;
3483         } else {
3484                 for (j = 0; j < usecs; j += 100) {
3485                         uint32_t isr;
3486                         uint16_t sema, mbox;
3487                         if (isp->isp_osinfo.mboxcmd_done) {
3488                                 break;
3489                         }
3490                         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
3491                                 isp_intr(isp, isr, sema, mbox);
3492                                 if (isp->isp_osinfo.mboxcmd_done) {
3493                                         break;
3494                                 }
3495                         }
3496                         USEC_DELAY(100);
3497                 }
3498         }
3499         if (isp->isp_osinfo.mboxcmd_done == 0) {
3500                 isp_prt(isp, ISP_LOGWARN,
3501                     "%s Mailbox Command (0x%x) Timeout",
3502                     isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled",
3503                     isp->isp_lastmbxcmd);
3504                 mbp->param[0] = MBOX_TIMEOUT;
3505                 isp->isp_osinfo.mboxcmd_done = 1;
3506         }
3507 }
3508
3509 void
3510 isp_mbox_notify_done(ispsoftc_t *isp)
3511 {
3512         if (isp->isp_osinfo.mbox_sleeping) {
3513                 wakeup(&isp->isp_mbxworkp);
3514         }
3515         isp->isp_osinfo.mboxcmd_done = 1;
3516 }
3517
3518 void
3519 isp_mbox_release(ispsoftc_t *isp)
3520 {
3521         isp->isp_osinfo.mboxbsy = 0;
3522 }
3523
3524 int
3525 isp_mstohz(int ms)
3526 {
3527         struct timeval t;
3528         t.tv_sec = ms / 1000;
3529         t.tv_usec = (ms % 1000) * 1000;
3530         ms = tvtohz(&t);
3531         if (ms < 0) {
3532                 ms = 0x7fffffff;
3533         }
3534         return (ms);
3535 }