Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / disk / isp / isp_freebsd.c
1 /* $FreeBSD: src/sys/dev/isp/isp_freebsd.c,v 1.32.2.20 2002/10/11 18:49:25 mjacob Exp $ */
2 /* $DragonFly: src/sys/dev/disk/isp/isp_freebsd.c,v 1.10 2004/05/19 22:52:41 dillon Exp $ */
3 /*
4  * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
5  *
6  * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include "isp_freebsd.h"
30 #include <sys/unistd.h>
31 #include <sys/kthread.h>
32 #include <machine/stdarg.h>     /* for use by isp_prt below */
33 #include <sys/conf.h>
34 #include <sys/ioccom.h>
35 #include "isp_ioctl.h"
36
37
38 static d_ioctl_t ispioctl;
39 static void isp_intr_enable(void *);
40 static void isp_cam_async(void *, u_int32_t, struct cam_path *, void *);
41 static void isp_poll(struct cam_sim *);
42 static timeout_t isp_watchdog;
43 static void isp_kthread(void *);
44 static void isp_action(struct cam_sim *, union ccb *);
45
46
47 #define ISP_CDEV_MAJOR  248
48 static struct cdevsw isp_cdevsw = {
49         /* name */      "isp",
50         /* maj */       ISP_CDEV_MAJOR,
51         /* flags */     D_TAPE,
52         /* port */      NULL,
53         /* clone */     NULL,
54
55         /* open */      nullopen,
56         /* close */     nullclose,
57         /* read */      noread,
58         /* write */     nowrite,
59         /* ioctl */     ispioctl,
60         /* poll */      nopoll,
61         /* mmap */      nommap,
62         /* strategy */  nostrategy,
63         /* dump */      nodump,
64         /* psize */     nopsize
65 };
66
67 static struct ispsoftc *isplist = NULL;
68
69 void
70 isp_attach(struct ispsoftc *isp)
71 {
72         int primary, secondary;
73         struct ccb_setasync csa;
74         struct cam_devq *devq;
75         struct cam_sim *sim;
76         struct cam_path *path;
77
78         /*
79          * Establish (in case of 12X0) which bus is the primary.
80          */
81
82         primary = 0;
83         secondary = 1;
84
85         /*
86          * Create the device queue for our SIM(s).
87          */
88         devq = cam_simq_alloc(isp->isp_maxcmds);
89         if (devq == NULL) {
90                 return;
91         }
92
93         /*
94          * Construct our SIM entry.
95          */
96         ISPLOCK_2_CAMLOCK(isp);
97         sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
98             device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
99         cam_simq_release(devq);         /* leaves 1 ref due to cam_sim_alloc */
100         if (sim == NULL) {
101                 CAMLOCK_2_ISPLOCK(isp);
102                 return;
103         }
104         CAMLOCK_2_ISPLOCK(isp);
105
106         isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
107         isp->isp_osinfo.ehook.ich_arg = isp;
108         ISPLOCK_2_CAMLOCK(isp);
109         if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
110                 cam_sim_free(sim);
111                 CAMLOCK_2_ISPLOCK(isp);
112                 isp_prt(isp, ISP_LOGERR,
113                     "could not establish interrupt enable hook");
114                 return;
115         }
116
117         if (xpt_bus_register(sim, primary) != CAM_SUCCESS) {
118                 cam_sim_free(sim);
119                 CAMLOCK_2_ISPLOCK(isp);
120                 return;
121         }
122
123         if (xpt_create_path(&path, NULL, cam_sim_path(sim),
124             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
125                 xpt_bus_deregister(cam_sim_path(sim));
126                 cam_sim_free(sim);
127                 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
128                 CAMLOCK_2_ISPLOCK(isp);
129                 return;
130         }
131
132         xpt_setup_ccb(&csa.ccb_h, path, 5);
133         csa.ccb_h.func_code = XPT_SASYNC_CB;
134         csa.event_enable = AC_LOST_DEVICE;
135         csa.callback = isp_cam_async;
136         csa.callback_arg = sim;
137         xpt_action((union ccb *)&csa);
138         CAMLOCK_2_ISPLOCK(isp);
139         isp->isp_sim = sim;
140         isp->isp_path = path;
141         /*
142          * Create a kernel thread for fibre channel instances. We
143          * don't have dual channel FC cards.
144          */
145         if (IS_FC(isp)) {
146                 ISPLOCK_2_CAMLOCK(isp);
147                 if (kthread_create(isp_kthread, isp, &isp->isp_osinfo.kthread,
148                     "%s: fc_thrd", device_get_nameunit(isp->isp_dev))) {
149                         xpt_bus_deregister(cam_sim_path(sim));
150                         cam_sim_free(sim);
151                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
152                         CAMLOCK_2_ISPLOCK(isp);
153                         isp_prt(isp, ISP_LOGERR, "could not create kthread");
154                         return;
155                 }
156                 CAMLOCK_2_ISPLOCK(isp);
157         }
158
159
160         /*
161          * If we have a second channel, construct SIM entry for that.
162          */
163         if (IS_DUALBUS(isp)) {
164                 ISPLOCK_2_CAMLOCK(isp);
165                 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp,
166                     device_get_unit(isp->isp_dev), 1, isp->isp_maxcmds, devq);
167                 if (sim == NULL) {
168                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
169                         xpt_free_path(isp->isp_path);
170                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
171                         return;
172                 }
173                 if (xpt_bus_register(sim, secondary) != CAM_SUCCESS) {
174                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
175                         xpt_free_path(isp->isp_path);
176                         cam_sim_free(sim);
177                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
178                         CAMLOCK_2_ISPLOCK(isp);
179                         return;
180                 }
181
182                 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
183                     CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
184                         xpt_bus_deregister(cam_sim_path(isp->isp_sim));
185                         xpt_free_path(isp->isp_path);
186                         xpt_bus_deregister(cam_sim_path(sim));
187                         cam_sim_free(sim);
188                         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
189                         CAMLOCK_2_ISPLOCK(isp);
190                         return;
191                 }
192
193                 xpt_setup_ccb(&csa.ccb_h, path, 5);
194                 csa.ccb_h.func_code = XPT_SASYNC_CB;
195                 csa.event_enable = AC_LOST_DEVICE;
196                 csa.callback = isp_cam_async;
197                 csa.callback_arg = sim;
198                 xpt_action((union ccb *)&csa);
199                 CAMLOCK_2_ISPLOCK(isp);
200                 isp->isp_sim2 = sim;
201                 isp->isp_path2 = path;
202         }
203         /*
204          * Create device nodes
205          */
206         cdevsw_add(&isp_cdevsw, -1, device_get_unit(isp->isp_dev));
207         make_dev(&isp_cdevsw, device_get_unit(isp->isp_dev), UID_ROOT,
208             GID_OPERATOR, 0600, "%s", device_get_nameunit(isp->isp_dev));
209
210         if (isp->isp_role != ISP_ROLE_NONE) {
211                 isp->isp_state = ISP_RUNSTATE;
212         }
213         if (isplist == NULL) {
214                 isplist = isp;
215         } else {
216                 struct ispsoftc *tmp = isplist;
217                 while (tmp->isp_osinfo.next) {
218                         tmp = tmp->isp_osinfo.next;
219                 }
220                 tmp->isp_osinfo.next = isp;
221         }
222
223 }
224
225 static INLINE void
226 isp_freeze_loopdown(struct ispsoftc *isp, char *msg)
227 {
228         if (isp->isp_osinfo.simqfrozen == 0) {
229                 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown)", msg);
230                 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
231                 ISPLOCK_2_CAMLOCK(isp);
232                 xpt_freeze_simq(isp->isp_sim, 1);
233                 CAMLOCK_2_ISPLOCK(isp);
234         } else {
235                 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown)", msg);
236                 isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
237         }
238 }
239
240 static int
241 ispioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, d_thread_t *td)
242 {
243         struct ispsoftc *isp;
244         int retval = ENOTTY;
245
246         isp = isplist;
247         while (isp) {
248                 if (minor(dev) == device_get_unit(isp->isp_dev)) {
249                         break;
250                 }
251                 isp = isp->isp_osinfo.next;
252         }
253         if (isp == NULL)
254                 return (ENXIO);
255         
256         switch (cmd) {
257 #ifdef  ISP_FW_CRASH_DUMP
258         case ISP_GET_FW_CRASH_DUMP:
259         {
260                 u_int16_t *ptr = FCPARAM(isp)->isp_dump_data;
261                 size_t sz;
262
263                 retval = 0;
264                 if (IS_2200(isp))
265                         sz = QLA2200_RISC_IMAGE_DUMP_SIZE;
266                 else
267                         sz = QLA2300_RISC_IMAGE_DUMP_SIZE;
268                 ISP_LOCK(isp);
269                 if (ptr && *ptr) {
270                         void *uaddr = *((void **) addr);
271                         if (copyout(ptr, uaddr, sz)) {
272                                 retval = EFAULT;
273                         } else {
274                                 *ptr = 0;
275                         }
276                 } else {
277                         retval = ENXIO;
278                 }
279                 ISP_UNLOCK(isp);
280                 break;
281         }
282
283         case ISP_FORCE_CRASH_DUMP:
284                 ISP_LOCK(isp);
285                 isp_freeze_loopdown(isp, "ispioctl(ISP_FORCE_CRASH_DUMP)");
286                 isp_fw_dump(isp);
287                 isp_reinit(isp);
288                 ISP_UNLOCK(isp);
289                 retval = 0;
290                 break;
291 #endif
292         case ISP_SDBLEV:
293         {
294                 int olddblev = isp->isp_dblev;
295                 isp->isp_dblev = *(int *)addr;
296                 *(int *)addr = olddblev;
297                 retval = 0;
298                 break;
299         }
300         case ISP_RESETHBA:
301                 ISP_LOCK(isp);
302                 isp_reinit(isp);
303                 ISP_UNLOCK(isp);
304                 retval = 0;
305                 break;
306         case ISP_RESCAN:
307                 if (IS_FC(isp)) {
308                         ISP_LOCK(isp);
309                         if (isp_fc_runstate(isp, 5 * 1000000)) {
310                                 retval = EIO;
311                         } else {
312                                 retval = 0;
313                         }
314                         ISP_UNLOCK(isp);
315                 }
316                 break;
317         case ISP_FC_LIP:
318                 if (IS_FC(isp)) {
319                         ISP_LOCK(isp);
320                         if (isp_control(isp, ISPCTL_SEND_LIP, 0)) {
321                                 retval = EIO;
322                         } else {
323                                 retval = 0;
324                         }
325                         ISP_UNLOCK(isp);
326                 }
327                 break;
328         case ISP_FC_GETDINFO:
329         {
330                 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
331                 struct lportdb *lp;
332
333                 if (ifc->loopid < 0 || ifc->loopid >= MAX_FC_TARG) {
334                         retval = EINVAL;
335                         break;
336                 }
337                 ISP_LOCK(isp);
338                 lp = &FCPARAM(isp)->portdb[ifc->loopid];
339                 if (lp->valid) {
340                         ifc->loopid = lp->loopid;
341                         ifc->portid = lp->portid;
342                         ifc->node_wwn = lp->node_wwn;
343                         ifc->port_wwn = lp->port_wwn;
344                         retval = 0;
345                 } else {
346                         retval = ENODEV;
347                 }
348                 ISP_UNLOCK(isp);
349                 break;
350         }
351         case ISP_GET_STATS:
352         {
353                 isp_stats_t *sp = (isp_stats_t *) addr;
354
355                 MEMZERO(sp, sizeof (*sp));
356                 sp->isp_stat_version = ISP_STATS_VERSION;
357                 sp->isp_type = isp->isp_type;
358                 sp->isp_revision = isp->isp_revision;
359                 ISP_LOCK(isp);
360                 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
361                 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
362                 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
363                 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
364                 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
365                 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
366                 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
367                 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
368                 ISP_UNLOCK(isp);
369                 retval = 0;
370                 break;
371         }
372         case ISP_CLR_STATS:
373                 ISP_LOCK(isp);
374                 isp->isp_intcnt = 0;
375                 isp->isp_intbogus = 0;
376                 isp->isp_intmboxc = 0;
377                 isp->isp_intoasync = 0;
378                 isp->isp_rsltccmplt = 0;
379                 isp->isp_fphccmplt = 0;
380                 isp->isp_rscchiwater = 0;
381                 isp->isp_fpcchiwater = 0;
382                 ISP_UNLOCK(isp);
383                 retval = 0;
384                 break;
385         case ISP_FC_GETHINFO:
386         {
387                 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
388                 MEMZERO(hba, sizeof (*hba));
389                 ISP_LOCK(isp);
390                 hba->fc_speed = FCPARAM(isp)->isp_gbspeed;
391                 hba->fc_scsi_supported = 1;
392                 hba->fc_topology = FCPARAM(isp)->isp_topo + 1;
393                 hba->fc_loopid = FCPARAM(isp)->isp_loopid;
394                 hba->active_node_wwn = FCPARAM(isp)->isp_nodewwn;
395                 hba->active_port_wwn = FCPARAM(isp)->isp_portwwn;
396                 ISP_UNLOCK(isp);
397                 retval = 0;
398                 break;
399         }
400         case ISP_GET_FC_PARAM:
401         {
402                 struct isp_fc_param *f = (struct isp_fc_param *) addr;
403
404                 if (!IS_FC(isp)) {
405                         retval = EINVAL;
406                         break;
407                 }
408                 f->parameter = 0;
409                 if (strcmp(f->param_name, "framelength") == 0) {
410                         f->parameter = FCPARAM(isp)->isp_maxfrmlen;
411                         retval = 0;
412                         break;
413                 }
414                 if (strcmp(f->param_name, "exec_throttle") == 0) {
415                         f->parameter = FCPARAM(isp)->isp_execthrottle;
416                         retval = 0;
417                         break;
418                 }
419                 if (strcmp(f->param_name, "fullduplex") == 0) {
420                         if (FCPARAM(isp)->isp_fwoptions & ICBOPT_FULL_DUPLEX)
421                                 f->parameter = 1;
422                         retval = 0;
423                         break;
424                 }
425                 if (strcmp(f->param_name, "loopid") == 0) {
426                         f->parameter = FCPARAM(isp)->isp_loopid;
427                         retval = 0;
428                         break;
429                 }
430                 retval = EINVAL;
431                 break;
432         }
433         case ISP_SET_FC_PARAM:
434         {
435                 struct isp_fc_param *f = (struct isp_fc_param *) addr;
436                 u_int32_t param = f->parameter;
437
438                 if (!IS_FC(isp)) {
439                         retval = EINVAL;
440                         break;
441                 }
442                 f->parameter = 0;
443                 if (strcmp(f->param_name, "framelength") == 0) {
444                         if (param != 512 && param != 1024 && param != 1024) {
445                                 retval = EINVAL;
446                                 break;
447                         }
448                         FCPARAM(isp)->isp_maxfrmlen = param;
449                         retval = 0;
450                         break;
451                 }
452                 if (strcmp(f->param_name, "exec_throttle") == 0) {
453                         if (param < 16 || param > 255) {
454                                 retval = EINVAL;
455                                 break;
456                         }
457                         FCPARAM(isp)->isp_execthrottle = param;
458                         retval = 0;
459                         break;
460                 }
461                 if (strcmp(f->param_name, "fullduplex") == 0) {
462                         if (param != 0 && param != 1) {
463                                 retval = EINVAL;
464                                 break;
465                         }
466                         if (param) {
467                                 FCPARAM(isp)->isp_fwoptions |=
468                                     ICBOPT_FULL_DUPLEX;
469                         } else {
470                                 FCPARAM(isp)->isp_fwoptions &=
471                                     ~ICBOPT_FULL_DUPLEX;
472                         }
473                         retval = 0;
474                         break;
475                 }
476                 if (strcmp(f->param_name, "loopid") == 0) {
477                         if (param < 0 || param > 125) {
478                                 retval = EINVAL;
479                                 break;
480                         }
481                         FCPARAM(isp)->isp_loopid = param;
482                         retval = 0;
483                         break;
484                 }
485                 retval = EINVAL;
486                 break;
487         }
488         default:
489                 break;
490         }
491         return (retval);
492 }
493
494 static void
495 isp_intr_enable(void *arg)
496 {
497         struct ispsoftc *isp = arg;
498         if (isp->isp_role != ISP_ROLE_NONE) {
499                 ENABLE_INTS(isp);
500         }
501         /* Release our hook so that the boot can continue. */
502         config_intrhook_disestablish(&isp->isp_osinfo.ehook);
503 }
504
505 /*
506  * Put the target mode functions here, because some are inlines
507  */
508
509 #ifdef  ISP_TARGET_MODE
510
511 static INLINE int is_lun_enabled(struct ispsoftc *, int, lun_id_t);
512 static INLINE int are_any_luns_enabled(struct ispsoftc *, int);
513 static INLINE tstate_t *get_lun_statep(struct ispsoftc *, int, lun_id_t);
514 static INLINE void rls_lun_statep(struct ispsoftc *, tstate_t *);
515 static INLINE int isp_psema_sig_rqe(struct ispsoftc *, int);
516 static INLINE int isp_cv_wait_timed_rqe(struct ispsoftc *, int, int);
517 static INLINE void isp_cv_signal_rqe(struct ispsoftc *, int, int);
518 static INLINE void isp_vsema_rqe(struct ispsoftc *, int);
519 static INLINE atio_private_data_t *isp_get_atpd(struct ispsoftc *, int);
520 static cam_status
521 create_lun_state(struct ispsoftc *, int, struct cam_path *, tstate_t **);
522 static void destroy_lun_state(struct ispsoftc *, tstate_t *);
523 static void isp_en_lun(struct ispsoftc *, union ccb *);
524 static cam_status isp_abort_tgt_ccb(struct ispsoftc *, union ccb *);
525 static timeout_t isp_refire_putback_atio;
526 static void isp_complete_ctio(union ccb *);
527 static void isp_target_putback_atio(union ccb *);
528 static cam_status isp_target_start_ctio(struct ispsoftc *, union ccb *);
529 static int isp_handle_platform_atio(struct ispsoftc *, at_entry_t *);
530 static int isp_handle_platform_atio2(struct ispsoftc *, at2_entry_t *);
531 static int isp_handle_platform_ctio(struct ispsoftc *, void *);
532 static int isp_handle_platform_notify_scsi(struct ispsoftc *, in_entry_t *);
533 static int isp_handle_platform_notify_fc(struct ispsoftc *, in_fcentry_t *);
534
535 static INLINE int
536 is_lun_enabled(struct ispsoftc *isp, int bus, lun_id_t lun)
537 {
538         tstate_t *tptr;
539         tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
540         if (tptr == NULL) {
541                 return (0);
542         }
543         do {
544                 if (tptr->lun == (lun_id_t) lun && tptr->bus == bus) {
545                         return (1);
546                 }
547         } while ((tptr = tptr->next) != NULL);
548         return (0);
549 }
550
551 static INLINE int
552 are_any_luns_enabled(struct ispsoftc *isp, int port)
553 {
554         int lo, hi;
555         if (IS_DUALBUS(isp)) {
556                 lo = (port * (LUN_HASH_SIZE >> 1));
557                 hi = lo + (LUN_HASH_SIZE >> 1);
558         } else {
559                 lo = 0;
560                 hi = LUN_HASH_SIZE;
561         }
562         for (lo = 0; lo < hi; lo++) {
563                 if (isp->isp_osinfo.lun_hash[lo]) {
564                         return (1);
565                 }
566         }
567         return (0);
568 }
569
570 static INLINE tstate_t *
571 get_lun_statep(struct ispsoftc *isp, int bus, lun_id_t lun)
572 {
573         tstate_t *tptr = NULL;
574
575         if (lun == CAM_LUN_WILDCARD) {
576                 if (isp->isp_osinfo.tmflags[bus] & TM_WILDCARD_ENABLED) {
577                         tptr = &isp->isp_osinfo.tsdflt[bus];
578                         tptr->hold++;
579                         return (tptr);
580                 }
581         } else {
582                 tptr = isp->isp_osinfo.lun_hash[LUN_HASH_FUNC(isp, bus, lun)];
583                 if (tptr == NULL) {
584                         return (NULL);
585                 }
586         }
587
588         do {
589                 if (tptr->lun == lun && tptr->bus == bus) {
590                         tptr->hold++;
591                         return (tptr);
592                 }
593         } while ((tptr = tptr->next) != NULL);
594         return (tptr);
595 }
596
597 static __inline void
598 rls_lun_statep(struct ispsoftc *isp, tstate_t *tptr)
599 {
600         if (tptr->hold)
601                 tptr->hold--;
602 }
603
604 static __inline int
605 isp_psema_sig_rqe(struct ispsoftc *isp, int bus)
606 {
607         while (isp->isp_osinfo.tmflags[bus] & TM_BUSY) {
608                 isp->isp_osinfo.tmflags[bus] |= TM_WANTED;
609                 if (tsleep(&isp->isp_osinfo.tmflags[bus], PCATCH, "i0", 0)) {
610                         return (-1);
611                 }
612                 isp->isp_osinfo.tmflags[bus] |= TM_BUSY;
613         }
614         return (0);
615 }
616
617 static __inline int
618 isp_cv_wait_timed_rqe(struct ispsoftc *isp, int bus, int timo)
619 {
620         if (tsleep(&isp->isp_osinfo.rstatus[bus], 0, "qt1", timo)) {
621                 return (-1);
622         }
623         return (0);
624 }
625
626 static __inline void
627 isp_cv_signal_rqe(struct ispsoftc *isp, int bus, int status)
628 {
629         isp->isp_osinfo.rstatus[bus] = status;
630         wakeup(&isp->isp_osinfo.rstatus[bus]);
631 }
632
633 static __inline void
634 isp_vsema_rqe(struct ispsoftc *isp, int bus)
635 {
636         if (isp->isp_osinfo.tmflags[bus] & TM_WANTED) {
637                 isp->isp_osinfo.tmflags[bus] &= ~TM_WANTED;
638                 wakeup(&isp->isp_osinfo.tmflags[bus]);
639         }
640         isp->isp_osinfo.tmflags[bus] &= ~TM_BUSY;
641 }
642
643 static __inline atio_private_data_t *
644 isp_get_atpd(struct ispsoftc *isp, int tag)
645 {
646         atio_private_data_t *atp;
647         for (atp = isp->isp_osinfo.atpdp;
648             atp < &isp->isp_osinfo.atpdp[ATPDPSIZE]; atp++) {
649                 if (atp->tag == tag)
650                         return (atp);
651         }
652         return (NULL);
653 }
654
655 static cam_status
656 create_lun_state(struct ispsoftc *isp, int bus,
657     struct cam_path *path, tstate_t **rslt)
658 {
659         cam_status status;
660         lun_id_t lun;
661         int hfx;
662         tstate_t *tptr, *new;
663
664         lun = xpt_path_lun_id(path);
665         if (lun < 0) {
666                 return (CAM_LUN_INVALID);
667         }
668         if (is_lun_enabled(isp, bus, lun)) {
669                 return (CAM_LUN_ALRDY_ENA);
670         }
671         new = malloc(sizeof (tstate_t), M_DEVBUF, M_WAITOK | M_ZERO);
672         status = xpt_create_path(&new->owner, NULL, xpt_path_path_id(path),
673             xpt_path_target_id(path), xpt_path_lun_id(path));
674         if (status != CAM_REQ_CMP) {
675                 free(new, M_DEVBUF);
676                 return (status);
677         }
678         new->bus = bus;
679         new->lun = lun;
680         SLIST_INIT(&new->atios);
681         SLIST_INIT(&new->inots);
682         new->hold = 1;
683
684         hfx = LUN_HASH_FUNC(isp, new->bus, new->lun);
685         tptr = isp->isp_osinfo.lun_hash[hfx];
686         if (tptr == NULL) {
687                 isp->isp_osinfo.lun_hash[hfx] = new;
688         } else {
689                 while (tptr->next)
690                         tptr = tptr->next;
691                 tptr->next = new;
692         }
693         *rslt = new;
694         return (CAM_REQ_CMP);
695 }
696
697 static INLINE void
698 destroy_lun_state(struct ispsoftc *isp, tstate_t *tptr)
699 {
700         int hfx;
701         tstate_t *lw, *pw;
702
703         hfx = LUN_HASH_FUNC(isp, tptr->bus, tptr->lun);
704         if (tptr->hold) {
705                 return;
706         }
707         pw = isp->isp_osinfo.lun_hash[hfx];
708         if (pw == NULL) {
709                 return;
710         } else if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
711                 isp->isp_osinfo.lun_hash[hfx] = pw->next;
712         } else {
713                 lw = pw;
714                 pw = lw->next;
715                 while (pw) {
716                         if (pw->lun == tptr->lun && pw->bus == tptr->bus) {
717                                 lw->next = pw->next;
718                                 break;
719                         }
720                         lw = pw;
721                         pw = pw->next;
722                 }
723                 if (pw == NULL) {
724                         return;
725                 }
726         }
727         free(tptr, M_DEVBUF);
728 }
729
730 /*
731  * we enter with our locks held.
732  */
733 static void
734 isp_en_lun(struct ispsoftc *isp, union ccb *ccb)
735 {
736         const char lfmt[] = "Lun now %sabled for target mode on channel %d";
737         struct ccb_en_lun *cel = &ccb->cel;
738         tstate_t *tptr;
739         u_int16_t rstat;
740         int bus, cmd, av, wildcard;
741         lun_id_t lun;
742         target_id_t tgt;
743
744
745         bus = XS_CHANNEL(ccb) & 0x1;
746         tgt = ccb->ccb_h.target_id;
747         lun = ccb->ccb_h.target_lun;
748
749         /*
750          * Do some sanity checking first.
751          */
752
753         if ((lun != CAM_LUN_WILDCARD) &&
754             (lun < 0 || lun >= (lun_id_t) isp->isp_maxluns)) {
755                 ccb->ccb_h.status = CAM_LUN_INVALID;
756                 return;
757         }
758
759         if (IS_SCSI(isp)) {
760                 sdparam *sdp = isp->isp_param;
761                 sdp += bus;
762                 if (tgt != CAM_TARGET_WILDCARD &&
763                     tgt != sdp->isp_initiator_id) {
764                         ccb->ccb_h.status = CAM_TID_INVALID;
765                         return;
766                 }
767         } else {
768                 if (tgt != CAM_TARGET_WILDCARD &&
769                     tgt != FCPARAM(isp)->isp_iid) {
770                         ccb->ccb_h.status = CAM_TID_INVALID;
771                         return;
772                 }
773                 /*
774                  * This is as a good a place as any to check f/w capabilities.
775                  */
776                 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_TMODE) == 0) {
777                         isp_prt(isp, ISP_LOGERR,
778                             "firmware does not support target mode");
779                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
780                         return;
781                 }
782                 /*
783                  * XXX: We *could* handle non-SCCLUN f/w, but we'd have to
784                  * XXX: dorks with our already fragile enable/disable code.
785                  */
786                 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
787                         isp_prt(isp, ISP_LOGERR,
788                             "firmware not SCCLUN capable");
789                 }
790         }
791
792         if (tgt == CAM_TARGET_WILDCARD) {
793                 if (lun == CAM_LUN_WILDCARD) {
794                         wildcard = 1;
795                 } else {
796                         ccb->ccb_h.status = CAM_LUN_INVALID;
797                         return;
798                 }
799         } else {
800                 wildcard = 0;
801         }
802
803         /*
804          * Next check to see whether this is a target/lun wildcard action.
805          *
806          * If so, we know that we can accept commands for luns that haven't
807          * been enabled yet and send them upstream. Otherwise, we have to
808          * handle them locally (if we see them at all).
809          */
810
811         if (wildcard) {
812                 tptr = &isp->isp_osinfo.tsdflt[bus];
813                 if (cel->enable) {
814                         if (isp->isp_osinfo.tmflags[bus] &
815                             TM_WILDCARD_ENABLED) {
816                                 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
817                                 return;
818                         }
819                         ccb->ccb_h.status =
820                             xpt_create_path(&tptr->owner, NULL,
821                             xpt_path_path_id(ccb->ccb_h.path),
822                             xpt_path_target_id(ccb->ccb_h.path),
823                             xpt_path_lun_id(ccb->ccb_h.path));
824                         if (ccb->ccb_h.status != CAM_REQ_CMP) {
825                                 return;
826                         }
827                         SLIST_INIT(&tptr->atios);
828                         SLIST_INIT(&tptr->inots);
829                         isp->isp_osinfo.tmflags[bus] |= TM_WILDCARD_ENABLED;
830                 } else {
831                         if ((isp->isp_osinfo.tmflags[bus] &
832                             TM_WILDCARD_ENABLED) == 0) {
833                                 ccb->ccb_h.status = CAM_REQ_CMP;
834                                 return;
835                         }
836                         if (tptr->hold) {
837                                 ccb->ccb_h.status = CAM_SCSI_BUSY;
838                                 return;
839                         }
840                         xpt_free_path(tptr->owner);
841                         isp->isp_osinfo.tmflags[bus] &= ~TM_WILDCARD_ENABLED;
842                 }
843         }
844
845         /*
846          * Now check to see whether this bus needs to be
847          * enabled/disabled with respect to target mode.
848          */
849         av = bus << 31;
850         if (cel->enable && !(isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED)) {
851                 av |= ENABLE_TARGET_FLAG;
852                 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
853                 if (av) {
854                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
855                         if (wildcard) {
856                                 isp->isp_osinfo.tmflags[bus] &=
857                                     ~TM_WILDCARD_ENABLED;
858                                 xpt_free_path(tptr->owner);
859                         }
860                         return;
861                 }
862                 isp->isp_osinfo.tmflags[bus] |= TM_TMODE_ENABLED;
863                 isp_prt(isp, ISP_LOGINFO,
864                     "Target Mode enabled on channel %d", bus);
865         } else if (cel->enable == 0 &&
866             (isp->isp_osinfo.tmflags[bus] & TM_TMODE_ENABLED) && wildcard) {
867                 if (are_any_luns_enabled(isp, bus)) {
868                         ccb->ccb_h.status = CAM_SCSI_BUSY;
869                         return;
870                 }
871                 av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
872                 if (av) {
873                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
874                         return;
875                 }
876                 isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
877                 isp_prt(isp, ISP_LOGINFO,
878                     "Target Mode disabled on channel %d", bus);
879         }
880
881         if (wildcard) {
882                 ccb->ccb_h.status = CAM_REQ_CMP;
883                 return;
884         }
885
886         if (cel->enable) {
887                 ccb->ccb_h.status =
888                     create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
889                 if (ccb->ccb_h.status != CAM_REQ_CMP) {
890                         return;
891                 }
892         } else {
893                 tptr = get_lun_statep(isp, bus, lun);
894                 if (tptr == NULL) {
895                         ccb->ccb_h.status = CAM_LUN_INVALID;
896                         return;
897                 }
898         }
899
900         if (isp_psema_sig_rqe(isp, bus)) {
901                 rls_lun_statep(isp, tptr);
902                 if (cel->enable)
903                         destroy_lun_state(isp, tptr);
904                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
905                 return;
906         }
907
908         if (cel->enable) {
909                 u_int32_t seq = isp->isp_osinfo.rollinfo++;
910                 int c, n, ulun = lun;
911
912                 cmd = RQSTYPE_ENABLE_LUN;
913                 c = DFLT_CMND_CNT;
914                 n = DFLT_INOT_CNT;
915                 if (IS_FC(isp) && lun != 0) {
916                         cmd = RQSTYPE_MODIFY_LUN;
917                         n = 0;
918                         /*
919                          * For SCC firmware, we only deal with setting
920                          * (enabling or modifying) lun 0.
921                          */
922                         ulun = 0;
923                 }
924                 rstat = LUN_ERR;
925                 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
926                         xpt_print_path(ccb->ccb_h.path);
927                         isp_prt(isp, ISP_LOGWARN, "isp_lun_cmd failed");
928                         goto out;
929                 }
930                 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
931                         xpt_print_path(ccb->ccb_h.path);
932                         isp_prt(isp, ISP_LOGERR,
933                             "wait for ENABLE/MODIFY LUN timed out");
934                         goto out;
935                 }
936                 rstat = isp->isp_osinfo.rstatus[bus];
937                 if (rstat != LUN_OK) {
938                         xpt_print_path(ccb->ccb_h.path);
939                         isp_prt(isp, ISP_LOGERR,
940                             "ENABLE/MODIFY LUN returned 0x%x", rstat);
941                         goto out;
942                 }
943         } else {
944                 int c, n, ulun = lun;
945                 u_int32_t seq;
946
947                 rstat = LUN_ERR;
948                 seq = isp->isp_osinfo.rollinfo++;
949                 cmd = -RQSTYPE_MODIFY_LUN;
950
951                 c = DFLT_CMND_CNT;
952                 n = DFLT_INOT_CNT;
953                 if (IS_FC(isp) && lun != 0) {
954                         n = 0;
955                         /*
956                          * For SCC firmware, we only deal with setting
957                          * (enabling or modifying) lun 0.
958                          */
959                         ulun = 0;
960                 }
961                 if (isp_lun_cmd(isp, cmd, bus, tgt, ulun, c, n, seq)) {
962                         xpt_print_path(ccb->ccb_h.path);
963                         isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
964                         goto out;
965                 }
966                 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
967                         xpt_print_path(ccb->ccb_h.path);
968                         isp_prt(isp, ISP_LOGERR,
969                             "wait for MODIFY LUN timed out");
970                         goto out;
971                 }
972                 rstat = isp->isp_osinfo.rstatus[bus];
973                 if (rstat != LUN_OK) {
974                         xpt_print_path(ccb->ccb_h.path);
975                         isp_prt(isp, ISP_LOGERR,
976                             "MODIFY LUN returned 0x%x", rstat);
977                         goto out;
978                 }
979                 if (IS_FC(isp) && lun) {
980                         goto out;
981                 }
982
983                 seq = isp->isp_osinfo.rollinfo++;
984
985                 rstat = LUN_ERR;
986                 cmd = -RQSTYPE_ENABLE_LUN;
987                 if (isp_lun_cmd(isp, cmd, bus, tgt, lun, 0, 0, seq)) {
988                         xpt_print_path(ccb->ccb_h.path);
989                         isp_prt(isp, ISP_LOGERR, "isp_lun_cmd failed");
990                         goto out;
991                 }
992                 if (isp_cv_wait_timed_rqe(isp, bus, 30 * hz)) {
993                         xpt_print_path(ccb->ccb_h.path);
994                         isp_prt(isp, ISP_LOGERR,
995                              "wait for DISABLE LUN timed out");
996                         goto out;
997                 }
998                 rstat = isp->isp_osinfo.rstatus[bus];
999                 if (rstat != LUN_OK) {
1000                         xpt_print_path(ccb->ccb_h.path);
1001                         isp_prt(isp, ISP_LOGWARN,
1002                             "DISABLE LUN returned 0x%x", rstat);
1003                         goto out;
1004                 }
1005                 if (are_any_luns_enabled(isp, bus) == 0) {
1006                         av = isp_control(isp, ISPCTL_TOGGLE_TMODE, &av);
1007                         if (av) {
1008                                 isp_prt(isp, ISP_LOGWARN,
1009                                     "disable target mode on channel %d failed",
1010                                     bus);
1011                                 goto out;
1012                         }
1013                         isp->isp_osinfo.tmflags[bus] &= ~TM_TMODE_ENABLED;
1014                         xpt_print_path(ccb->ccb_h.path);
1015                         isp_prt(isp, ISP_LOGINFO,
1016                             "Target Mode disabled on channel %d", bus);
1017                 }
1018         }
1019
1020 out:
1021         isp_vsema_rqe(isp, bus);
1022
1023         if (rstat != LUN_OK) {
1024                 xpt_print_path(ccb->ccb_h.path);
1025                 isp_prt(isp, ISP_LOGWARN,
1026                     "lun %sable failed", (cel->enable) ? "en" : "dis");
1027                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1028                 rls_lun_statep(isp, tptr);
1029                 if (cel->enable)
1030                         destroy_lun_state(isp, tptr);
1031         } else {
1032                 xpt_print_path(ccb->ccb_h.path);
1033                 isp_prt(isp, ISP_LOGINFO, lfmt,
1034                     (cel->enable) ? "en" : "dis", bus);
1035                 rls_lun_statep(isp, tptr);
1036                 if (cel->enable == 0) {
1037                         destroy_lun_state(isp, tptr);
1038                 }
1039                 ccb->ccb_h.status = CAM_REQ_CMP;
1040         }
1041 }
1042
1043 static cam_status
1044 isp_abort_tgt_ccb(struct ispsoftc *isp, union ccb *ccb)
1045 {
1046         tstate_t *tptr;
1047         struct ccb_hdr_slist *lp;
1048         struct ccb_hdr *curelm;
1049         int found;
1050         union ccb *accb = ccb->cab.abort_ccb;
1051
1052         if (accb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1053                 if (IS_FC(isp) && (accb->ccb_h.target_id != 
1054                     ((fcparam *) isp->isp_param)->isp_loopid)) {
1055                         return (CAM_PATH_INVALID);
1056                 } else if (IS_SCSI(isp) && (accb->ccb_h.target_id != 
1057                     ((sdparam *) isp->isp_param)->isp_initiator_id)) {
1058                         return (CAM_PATH_INVALID);
1059                 }
1060         }
1061         tptr = get_lun_statep(isp, XS_CHANNEL(ccb), accb->ccb_h.target_lun);
1062         if (tptr == NULL) {
1063                 return (CAM_PATH_INVALID);
1064         }
1065         if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
1066                 lp = &tptr->atios;
1067         } else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
1068                 lp = &tptr->inots;
1069         } else {
1070                 rls_lun_statep(isp, tptr);
1071                 return (CAM_UA_ABORT);
1072         }
1073         curelm = SLIST_FIRST(lp);
1074         found = 0;
1075         if (curelm == &accb->ccb_h) {
1076                 found = 1;
1077                 SLIST_REMOVE_HEAD(lp, sim_links.sle);
1078         } else {
1079                 while(curelm != NULL) {
1080                         struct ccb_hdr *nextelm;
1081
1082                         nextelm = SLIST_NEXT(curelm, sim_links.sle);
1083                         if (nextelm == &accb->ccb_h) {
1084                                 found = 1;
1085                                 SLIST_NEXT(curelm, sim_links.sle) =
1086                                     SLIST_NEXT(nextelm, sim_links.sle);
1087                                 break;
1088                         }
1089                         curelm = nextelm;
1090                 }
1091         }
1092         rls_lun_statep(isp, tptr);
1093         if (found) {
1094                 accb->ccb_h.status = CAM_REQ_ABORTED;
1095                 return (CAM_REQ_CMP);
1096         }
1097         return(CAM_PATH_INVALID);
1098 }
1099
1100 static cam_status
1101 isp_target_start_ctio(struct ispsoftc *isp, union ccb *ccb)
1102 {
1103         void *qe;
1104         struct ccb_scsiio *cso = &ccb->csio;
1105         u_int16_t *hp, save_handle;
1106         u_int16_t nxti, optr;
1107         u_int8_t local[QENTRY_LEN];
1108
1109
1110         if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1111                 xpt_print_path(ccb->ccb_h.path);
1112                 printf("Request Queue Overflow in isp_target_start_ctio\n");
1113                 return (CAM_RESRC_UNAVAIL);
1114         }
1115         bzero(local, QENTRY_LEN);
1116
1117         /*
1118          * We're either moving data or completing a command here.
1119          */
1120
1121         if (IS_FC(isp)) {
1122                 atio_private_data_t *atp;
1123                 ct2_entry_t *cto = (ct2_entry_t *) local;
1124
1125                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1126                 cto->ct_header.rqs_entry_count = 1;
1127                 cto->ct_iid = cso->init_id;
1128                 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
1129                         cto->ct_lun = ccb->ccb_h.target_lun;
1130                 }
1131
1132                 atp = isp_get_atpd(isp, cso->tag_id);
1133                 if (atp == NULL) {
1134                         isp_prt(isp, ISP_LOGERR,
1135                             "cannot find private data adjunct for tag %x",
1136                             cso->tag_id);
1137                         return (-1);
1138                 }
1139
1140                 cto->ct_rxid = cso->tag_id;
1141                 if (cso->dxfer_len == 0) {
1142                         cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA;
1143                         if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1144                                 cto->ct_flags |= CT2_SENDSTATUS;
1145                                 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1146                                 cto->ct_resid =
1147                                     atp->orig_datalen - atp->bytes_xfered;
1148                                 if (cto->ct_resid < 0) {
1149                                         cto->rsp.m1.ct_scsi_status |=
1150                                             CT2_DATA_OVER;
1151                                 } else if (cto->ct_resid > 0) {
1152                                         cto->rsp.m1.ct_scsi_status |=
1153                                             CT2_DATA_UNDER;
1154                                 }
1155                         }
1156                         if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
1157                                 int m = min(cso->sense_len, MAXRESPLEN);
1158                                 bcopy(&cso->sense_data, cto->rsp.m1.ct_resp, m);
1159                                 cto->rsp.m1.ct_senselen = m;
1160                                 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1161                         }
1162                 } else {
1163                         cto->ct_flags |= CT2_FLAG_MODE0;
1164                         if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1165                                 cto->ct_flags |= CT2_DATA_IN;
1166                         } else {
1167                                 cto->ct_flags |= CT2_DATA_OUT;
1168                         }
1169                         cto->ct_reloff = atp->bytes_xfered;
1170                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
1171                                 cto->ct_flags |= CT2_SENDSTATUS;
1172                                 cto->rsp.m0.ct_scsi_status = cso->scsi_status;
1173                                 cto->ct_resid =
1174                                     atp->orig_datalen -
1175                                     (atp->bytes_xfered + cso->dxfer_len);
1176                                 if (cto->ct_resid < 0) {
1177                                         cto->rsp.m0.ct_scsi_status |=
1178                                             CT2_DATA_OVER;
1179                                 } else if (cto->ct_resid > 0) {
1180                                         cto->rsp.m0.ct_scsi_status |=
1181                                             CT2_DATA_UNDER;
1182                                 }
1183                         } else {
1184                                 atp->last_xframt = cso->dxfer_len;
1185                         }
1186                         /*
1187                          * If we're sending data and status back together,
1188                          * we can't also send back sense data as well.
1189                          */
1190                         ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1191                 }
1192
1193                 if (cto->ct_flags & CT2_SENDSTATUS) {
1194                         isp_prt(isp, ISP_LOGTDEBUG0,
1195                             "CTIO2[%x] STATUS %x origd %u curd %u resid %u",
1196                             cto->ct_rxid, cso->scsi_status, atp->orig_datalen,
1197                             cso->dxfer_len, cto->ct_resid);
1198                         cto->ct_flags |= CT2_CCINCR;
1199                         atp->state = ATPD_STATE_LAST_CTIO;
1200                 } else
1201                         atp->state = ATPD_STATE_CTIO;
1202                 cto->ct_timeout = 10;
1203                 hp = &cto->ct_syshandle;
1204         } else {
1205                 ct_entry_t *cto = (ct_entry_t *) local;
1206
1207                 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
1208                 cto->ct_header.rqs_entry_count = 1;
1209                 cto->ct_iid = cso->init_id;
1210                 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
1211                 cto->ct_tgt = ccb->ccb_h.target_id;
1212                 cto->ct_lun = ccb->ccb_h.target_lun;
1213                 cto->ct_fwhandle = AT_GET_HANDLE(cso->tag_id);
1214                 if (AT_HAS_TAG(cso->tag_id)) {
1215                         cto->ct_tag_val = (u_int8_t) AT_GET_TAG(cso->tag_id);
1216                         cto->ct_flags |= CT_TQAE;
1217                 }
1218                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
1219                         cto->ct_flags |= CT_NODISC;
1220                 }
1221                 if (cso->dxfer_len == 0) {
1222                         cto->ct_flags |= CT_NO_DATA;
1223                 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1224                         cto->ct_flags |= CT_DATA_IN;
1225                 } else {
1226                         cto->ct_flags |= CT_DATA_OUT;
1227                 }
1228                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1229                         cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
1230                         cto->ct_scsi_status = cso->scsi_status;
1231                         cto->ct_resid = cso->resid;
1232                         isp_prt(isp, ISP_LOGTDEBUG0,
1233                             "CTIO[%x] SCSI STATUS 0x%x resid %d tag_id %x",
1234                             cto->ct_fwhandle, cso->scsi_status, cso->resid,
1235                             cso->tag_id);
1236                 }
1237                 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
1238                 cto->ct_timeout = 10;
1239                 hp = &cto->ct_syshandle;
1240         }
1241
1242         if (isp_save_xs(isp, (XS_T *)ccb, hp)) {
1243                 xpt_print_path(ccb->ccb_h.path);
1244                 printf("No XFLIST pointers for isp_target_start_ctio\n");
1245                 return (CAM_RESRC_UNAVAIL);
1246         }
1247
1248
1249         /*
1250          * Call the dma setup routines for this entry (and any subsequent
1251          * CTIOs) if there's data to move, and then tell the f/w it's got
1252          * new things to play with. As with isp_start's usage of DMA setup,
1253          * any swizzling is done in the machine dependent layer. Because
1254          * of this, we put the request onto the queue area first in native
1255          * format.
1256          */
1257
1258         save_handle = *hp;
1259
1260         switch (ISP_DMASETUP(isp, cso, (ispreq_t *) local, &nxti, optr)) {
1261         case CMD_QUEUED:
1262                 ISP_ADD_REQUEST(isp, nxti);
1263                 return (CAM_REQ_INPROG);
1264
1265         case CMD_EAGAIN:
1266                 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1267                 isp_destroy_handle(isp, save_handle);
1268                 return (CAM_RESRC_UNAVAIL);
1269
1270         default:
1271                 isp_destroy_handle(isp, save_handle);
1272                 return (XS_ERR(ccb));
1273         }
1274 }
1275
1276 static void
1277 isp_refire_putback_atio(void *arg)
1278 {
1279         int s = splcam();
1280         isp_target_putback_atio(arg);
1281         splx(s);
1282 }
1283
1284 static void
1285 isp_target_putback_atio(union ccb *ccb)
1286 {
1287         struct ispsoftc *isp;
1288         struct ccb_scsiio *cso;
1289         u_int16_t nxti, optr;
1290         void *qe;
1291
1292         isp = XS_ISP(ccb);
1293
1294         if (isp_getrqentry(isp, &nxti, &optr, &qe)) {
1295                 (void) timeout(isp_refire_putback_atio, ccb, 10);
1296                 isp_prt(isp, ISP_LOGWARN,
1297                     "isp_target_putback_atio: Request Queue Overflow"); 
1298                 return;
1299         }
1300         bzero(qe, QENTRY_LEN);
1301         cso = &ccb->csio;
1302         if (IS_FC(isp)) {
1303                 at2_entry_t local, *at = &local;
1304                 MEMZERO(at, sizeof (at2_entry_t));
1305                 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
1306                 at->at_header.rqs_entry_count = 1;
1307                 if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1308                         at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
1309                 } else {
1310                         at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
1311                 }
1312                 at->at_status = CT_OK;
1313                 at->at_rxid = cso->tag_id;
1314                 at->at_iid = cso->ccb_h.target_id;
1315                 isp_put_atio2(isp, at, qe);
1316         } else {
1317                 at_entry_t local, *at = &local;
1318                 MEMZERO(at, sizeof (at_entry_t));
1319                 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
1320                 at->at_header.rqs_entry_count = 1;
1321                 at->at_iid = cso->init_id;
1322                 at->at_iid |= XS_CHANNEL(ccb) << 7;
1323                 at->at_tgt = cso->ccb_h.target_id;
1324                 at->at_lun = cso->ccb_h.target_lun;
1325                 at->at_status = CT_OK;
1326                 at->at_tag_val = AT_GET_TAG(cso->tag_id);
1327                 at->at_handle = AT_GET_HANDLE(cso->tag_id);
1328                 isp_put_atio(isp, at, qe);
1329         }
1330         ISP_TDQE(isp, "isp_target_putback_atio", (int) optr, qe);
1331         ISP_ADD_REQUEST(isp, nxti);
1332         isp_complete_ctio(ccb);
1333 }
1334
1335 static void
1336 isp_complete_ctio(union ccb *ccb)
1337 {
1338         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1339                 ccb->ccb_h.status |= CAM_REQ_CMP;
1340         }
1341         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1342         xpt_done(ccb);
1343 }
1344
1345 /*
1346  * Handle ATIO stuff that the generic code can't.
1347  * This means handling CDBs.
1348  */
1349
1350 static int
1351 isp_handle_platform_atio(struct ispsoftc *isp, at_entry_t *aep)
1352 {
1353         tstate_t *tptr;
1354         int status, bus, iswildcard;
1355         struct ccb_accept_tio *atiop;
1356
1357         /*
1358          * The firmware status (except for the QLTM_SVALID bit)
1359          * indicates why this ATIO was sent to us.
1360          *
1361          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1362          *
1363          * If the DISCONNECTS DISABLED bit is set in the flags field,
1364          * we're still connected on the SCSI bus.
1365          */
1366         status = aep->at_status;
1367         if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
1368                 /*
1369                  * Bus Phase Sequence error. We should have sense data
1370                  * suggested by the f/w. I'm not sure quite yet what
1371                  * to do about this for CAM.
1372                  */
1373                 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
1374                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1375                 return (0);
1376         }
1377         if ((status & ~QLTM_SVALID) != AT_CDB) {
1378                 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform",
1379                     status);
1380                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1381                 return (0);
1382         }
1383
1384         bus = GET_BUS_VAL(aep->at_iid);
1385         tptr = get_lun_statep(isp, bus, aep->at_lun);
1386         if (tptr == NULL) {
1387                 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
1388                 iswildcard = 1;
1389         } else {
1390                 iswildcard = 0;
1391         }
1392
1393         if (tptr == NULL) {
1394                 /*
1395                  * Because we can't autofeed sense data back with
1396                  * a command for parallel SCSI, we can't give back
1397                  * a CHECK CONDITION. We'll give back a BUSY status
1398                  * instead. This works out okay because the only
1399                  * time we should, in fact, get this, is in the
1400                  * case that somebody configured us without the
1401                  * blackhole driver, so they get what they deserve.
1402                  */
1403                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1404                 return (0);
1405         }
1406
1407         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1408         if (atiop == NULL) {
1409                 /*
1410                  * Because we can't autofeed sense data back with
1411                  * a command for parallel SCSI, we can't give back
1412                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
1413                  * instead. This works out okay because the only time we
1414                  * should, in fact, get this, is in the case that we've
1415                  * run out of ATIOS.
1416                  */
1417                 xpt_print_path(tptr->owner);
1418                 isp_prt(isp, ISP_LOGWARN,
1419                     "no ATIOS for lun %d from initiator %d on channel %d",
1420                     aep->at_lun, GET_IID_VAL(aep->at_iid), bus);
1421                 if (aep->at_flags & AT_TQAE)
1422                         isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1423                 else
1424                         isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1425                 rls_lun_statep(isp, tptr);
1426                 return (0);
1427         }
1428         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1429         if (iswildcard) {
1430                 atiop->ccb_h.target_id = aep->at_tgt;
1431                 atiop->ccb_h.target_lun = aep->at_lun;
1432         }
1433         if (aep->at_flags & AT_NODISC) {
1434                 atiop->ccb_h.flags = CAM_DIS_DISCONNECT;
1435         } else {
1436                 atiop->ccb_h.flags = 0;
1437         }
1438
1439         if (status & QLTM_SVALID) {
1440                 size_t amt = imin(QLTM_SENSELEN, sizeof (atiop->sense_data));
1441                 atiop->sense_len = amt;
1442                 MEMCPY(&atiop->sense_data, aep->at_sense, amt);
1443         } else {
1444                 atiop->sense_len = 0;
1445         }
1446
1447         atiop->init_id = GET_IID_VAL(aep->at_iid);
1448         atiop->cdb_len = aep->at_cdblen;
1449         MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
1450         atiop->ccb_h.status = CAM_CDB_RECVD;
1451         /*
1452          * Construct a tag 'id' based upon tag value (which may be 0..255)
1453          * and the handle (which we have to preserve).
1454          */
1455         AT_MAKE_TAGID(atiop->tag_id, aep);
1456         if (aep->at_flags & AT_TQAE) {
1457                 atiop->tag_action = aep->at_tag_type;
1458                 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
1459         }
1460         xpt_done((union ccb*)atiop);
1461         isp_prt(isp, ISP_LOGTDEBUG0,
1462             "ATIO[%x] CDB=0x%x bus %d iid%d->lun%d tag 0x%x ttype 0x%x %s",
1463             aep->at_handle, aep->at_cdb[0] & 0xff, GET_BUS_VAL(aep->at_iid),
1464             GET_IID_VAL(aep->at_iid), aep->at_lun, aep->at_tag_val & 0xff,
1465             aep->at_tag_type, (aep->at_flags & AT_NODISC)?
1466             "nondisc" : "disconnecting");
1467         rls_lun_statep(isp, tptr);
1468         return (0);
1469 }
1470
1471 static int
1472 isp_handle_platform_atio2(struct ispsoftc *isp, at2_entry_t *aep)
1473 {
1474         lun_id_t lun;
1475         tstate_t *tptr;
1476         struct ccb_accept_tio *atiop;
1477         atio_private_data_t *atp;
1478
1479         /*
1480          * The firmware status (except for the QLTM_SVALID bit)
1481          * indicates why this ATIO was sent to us.
1482          *
1483          * If QLTM_SVALID is set, the firware has recommended Sense Data.
1484          */
1485         if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
1486                 isp_prt(isp, ISP_LOGWARN,
1487                     "bogus atio (0x%x) leaked to platform", aep->at_status);
1488                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1489                 return (0);
1490         }
1491
1492         if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) != 0) {
1493                 lun = aep->at_scclun;
1494         } else {
1495                 lun = aep->at_lun;
1496         }
1497         tptr = get_lun_statep(isp, 0, lun);
1498         if (tptr == NULL) {
1499                 isp_prt(isp, ISP_LOGWARN, "no state pointer for lun %d", lun);
1500                 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
1501         }
1502
1503         if (tptr == NULL) {
1504                 /*
1505                  * What we'd like to know is whether or not we have a listener
1506                  * upstream that really hasn't configured yet. If we do, then
1507                  * we can give a more sensible reply here. If not, then we can
1508                  * reject this out of hand.
1509                  *
1510                  * Choices for what to send were
1511                  *
1512                  *      Not Ready, Unit Not Self-Configured Yet
1513                  *      (0x2,0x3e,0x00)
1514                  *
1515                  * for the former and
1516                  *
1517                  *      Illegal Request, Logical Unit Not Supported
1518                  *      (0x5,0x25,0x00)
1519                  *
1520                  * for the latter.
1521                  *
1522                  * We used to decide whether there was at least one listener
1523                  * based upon whether the black hole driver was configured.
1524                  * However, recent config(8) changes have made this hard to do
1525                  * at this time.
1526                  *
1527                  */
1528                 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
1529                 return (0);
1530         }
1531
1532         atp = isp_get_atpd(isp, 0);
1533         atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
1534         if (atiop == NULL || atp == NULL) {
1535                 /*
1536                  * Because we can't autofeed sense data back with
1537                  * a command for parallel SCSI, we can't give back
1538                  * a CHECK CONDITION. We'll give back a QUEUE FULL status
1539                  * instead. This works out okay because the only time we
1540                  * should, in fact, get this, is in the case that we've
1541                  * run out of ATIOS.
1542                  */
1543                 xpt_print_path(tptr->owner);
1544                 isp_prt(isp, ISP_LOGWARN,
1545                     "no %s for lun %d from initiator %d",
1546                     (atp == NULL && atiop == NULL)? "ATIO2s *or* ATPS" :
1547                     ((atp == NULL)? "ATPs" : "ATIO2s"), lun, aep->at_iid);
1548                 rls_lun_statep(isp, tptr);
1549                 isp_endcmd(isp, aep, SCSI_STATUS_QUEUE_FULL, 0);
1550                 return (0);
1551         }
1552         atp->state = ATPD_STATE_ATIO;
1553         SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1554         tptr->atio_count--;
1555         isp_prt(isp, ISP_LOGTDEBUG0, "Take FREE ATIO2 lun %d, count now %d",
1556             lun, tptr->atio_count);
1557
1558         if (tptr == &isp->isp_osinfo.tsdflt[0]) {
1559                 atiop->ccb_h.target_id =
1560                     ((fcparam *)isp->isp_param)->isp_loopid;
1561                 atiop->ccb_h.target_lun = lun;
1562         }
1563         /*
1564          * We don't get 'suggested' sense data as we do with SCSI cards.
1565          */
1566         atiop->sense_len = 0;
1567
1568         atiop->init_id = aep->at_iid;
1569         atiop->cdb_len = ATIO2_CDBLEN;
1570         MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
1571         atiop->ccb_h.status = CAM_CDB_RECVD;
1572         atiop->tag_id = aep->at_rxid;
1573         switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
1574         case ATIO2_TC_ATTR_SIMPLEQ:
1575                 atiop->tag_action = MSG_SIMPLE_Q_TAG;
1576                 break;
1577         case ATIO2_TC_ATTR_HEADOFQ:
1578                 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
1579                 break;
1580         case ATIO2_TC_ATTR_ORDERED:
1581                 atiop->tag_action = MSG_ORDERED_Q_TAG;
1582                 break;
1583         case ATIO2_TC_ATTR_ACAQ:                /* ?? */
1584         case ATIO2_TC_ATTR_UNTAGGED:
1585         default:
1586                 atiop->tag_action = 0;
1587                 break;
1588         }
1589         atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
1590
1591         atp->tag = atiop->tag_id;
1592         atp->lun = lun;
1593         atp->orig_datalen = aep->at_datalen;
1594         atp->last_xframt = 0;
1595         atp->bytes_xfered = 0;
1596         atp->state = ATPD_STATE_CAM;
1597         xpt_done((union ccb*)atiop);
1598
1599         isp_prt(isp, ISP_LOGTDEBUG0,
1600             "ATIO2[%x] CDB=0x%x iid%d->lun%d tattr 0x%x datalen %u",
1601             aep->at_rxid, aep->at_cdb[0] & 0xff, aep->at_iid,
1602             lun, aep->at_taskflags, aep->at_datalen);
1603         rls_lun_statep(isp, tptr);
1604         return (0);
1605 }
1606
1607 static int
1608 isp_handle_platform_ctio(struct ispsoftc *isp, void *arg)
1609 {
1610         union ccb *ccb;
1611         int sentstatus, ok, notify_cam, resid = 0;
1612         u_int16_t tval;
1613
1614         /*
1615          * CTIO and CTIO2 are close enough....
1616          */
1617
1618         ccb = (union ccb *) isp_find_xs(isp, ((ct_entry_t *)arg)->ct_syshandle);
1619         KASSERT((ccb != NULL), ("null ccb in isp_handle_platform_ctio"));
1620         isp_destroy_handle(isp, ((ct_entry_t *)arg)->ct_syshandle);
1621
1622         if (IS_FC(isp)) {
1623                 ct2_entry_t *ct = arg;
1624                 atio_private_data_t *atp = isp_get_atpd(isp, ct->ct_rxid);
1625                 if (atp == NULL) {
1626                         isp_prt(isp, ISP_LOGERR,
1627                             "cannot find adjunct for %x after I/O",
1628                             ct->ct_rxid);
1629                         return (0);
1630                 }
1631                 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
1632                 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
1633                 if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
1634                         ccb->ccb_h.status |= CAM_SENT_SENSE;
1635                 }
1636                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1637                 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1638                         resid = ct->ct_resid;
1639                         atp->bytes_xfered += (atp->last_xframt - resid);
1640                         atp->last_xframt = 0;
1641                 }
1642                 if (sentstatus || !ok) {
1643                         atp->tag = 0;
1644                 }
1645                 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN,
1646                     "CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s",
1647                     ct->ct_rxid, ct->ct_status, ct->ct_flags,
1648                     (ccb->ccb_h.status & CAM_SENT_SENSE) != 0,
1649                     resid, sentstatus? "FIN" : "MID");
1650                 tval = ct->ct_rxid;
1651
1652                 /* XXX: should really come after isp_complete_ctio */
1653                 atp->state = ATPD_STATE_PDON;
1654         } else {
1655                 ct_entry_t *ct = arg;
1656                 sentstatus = ct->ct_flags & CT_SENDSTATUS;
1657                 ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
1658                 /*
1659                  * We *ought* to be able to get back to the original ATIO
1660                  * here, but for some reason this gets lost. It's just as
1661                  * well because it's squirrelled away as part of periph
1662                  * private data.
1663                  *
1664                  * We can live without it as long as we continue to use
1665                  * the auto-replenish feature for CTIOs.
1666                  */
1667                 notify_cam = ct->ct_header.rqs_seqno & 0x1;
1668                 if (ct->ct_status & QLTM_SVALID) {
1669                         char *sp = (char *)ct;
1670                         sp += CTIO_SENSE_OFFSET;
1671                         ccb->csio.sense_len =
1672                             min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
1673                         MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
1674                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1675                 }
1676                 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1677                         resid = ct->ct_resid;
1678                 }
1679                 isp_prt(isp, ISP_LOGTDEBUG0,
1680                     "CTIO[%x] tag %x iid %d lun %d sts %x flg %x resid %d %s",
1681                     ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun,
1682                     ct->ct_status, ct->ct_flags, resid,
1683                     sentstatus? "FIN" : "MID");
1684                 tval = ct->ct_fwhandle;
1685         }
1686         ccb->csio.resid += resid;
1687
1688         /*
1689          * We're here either because intermediate data transfers are done
1690          * and/or the final status CTIO (which may have joined with a
1691          * Data Transfer) is done.
1692          *
1693          * In any case, for this platform, the upper layers figure out
1694          * what to do next, so all we do here is collect status and
1695          * pass information along. Any DMA handles have already been
1696          * freed.
1697          */
1698         if (notify_cam == 0) {
1699                 isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
1700                 return (0);
1701         }
1702
1703         isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done",
1704             (sentstatus)? "  FINAL " : "MIDTERM ", tval);
1705
1706         if (!ok) {
1707                 isp_target_putback_atio(ccb);
1708         } else {
1709                 isp_complete_ctio(ccb);
1710
1711         }
1712         return (0);
1713 }
1714
1715 static int
1716 isp_handle_platform_notify_scsi(struct ispsoftc *isp, in_entry_t *inp)
1717 {
1718         return (0);     /* XXXX */
1719 }
1720
1721 static int
1722 isp_handle_platform_notify_fc(struct ispsoftc *isp, in_fcentry_t *inp)
1723 {
1724
1725         switch (inp->in_status) {
1726         case IN_PORT_LOGOUT:
1727                 isp_prt(isp, ISP_LOGWARN, "port logout of iid %d",
1728                    inp->in_iid);
1729                 break;
1730         case IN_PORT_CHANGED:
1731                 isp_prt(isp, ISP_LOGWARN, "port changed for iid %d",
1732                    inp->in_iid);
1733                 break;
1734         case IN_GLOBAL_LOGO:
1735                 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
1736                 break;
1737         case IN_ABORT_TASK:
1738         {
1739                 atio_private_data_t *atp = isp_get_atpd(isp, inp->in_seqid);
1740                 struct ccb_immed_notify *inot = NULL;
1741
1742                 if (atp) {
1743                         tstate_t *tptr = get_lun_statep(isp, 0, atp->lun);
1744                         if (tptr) {
1745                                 inot = (struct ccb_immed_notify *)
1746                                     SLIST_FIRST(&tptr->inots);
1747                                 if (inot) {
1748                                         SLIST_REMOVE_HEAD(&tptr->inots,
1749                                             sim_links.sle);
1750                                 }
1751                         }
1752                         isp_prt(isp, ISP_LOGWARN,
1753                            "abort task RX_ID %x IID %d state %d",
1754                            inp->in_seqid, inp->in_iid, atp->state);
1755                 } else {
1756                         isp_prt(isp, ISP_LOGWARN,
1757                            "abort task RX_ID %x from iid %d, state unknown",
1758                            inp->in_seqid, inp->in_iid);
1759                 }
1760                 if (inot) {
1761                         inot->initiator_id = inp->in_iid;
1762                         inot->sense_len = 0;
1763                         inot->message_args[0] = MSG_ABORT_TAG;
1764                         inot->message_args[1] = inp->in_seqid & 0xff;
1765                         inot->message_args[2] = (inp->in_seqid >> 8) & 0xff;
1766                         inot->ccb_h.status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
1767                         xpt_done((union ccb *)inot);
1768                 }
1769                 break;
1770         }
1771         default:
1772                 break;
1773         }
1774         return (0);
1775 }
1776 #endif
1777
1778 static void
1779 isp_cam_async(void *cbarg, u_int32_t code, struct cam_path *path, void *arg)
1780 {
1781         struct cam_sim *sim;
1782         struct ispsoftc *isp;
1783
1784         sim = (struct cam_sim *)cbarg;
1785         isp = (struct ispsoftc *) cam_sim_softc(sim);
1786         switch (code) {
1787         case AC_LOST_DEVICE:
1788                 if (IS_SCSI(isp)) {
1789                         u_int16_t oflags, nflags;
1790                         sdparam *sdp = isp->isp_param;
1791                         int tgt;
1792
1793                         tgt = xpt_path_target_id(path);
1794                         if (tgt >= 0) {
1795                                 sdp += cam_sim_bus(sim);
1796                                 ISP_LOCK(isp);
1797                                 nflags = sdp->isp_devparam[tgt].nvrm_flags;
1798 #ifndef ISP_TARGET_MODE
1799                                 nflags &= DPARM_SAFE_DFLT;
1800                                 if (isp->isp_loaded_fw) {
1801                                         nflags |= DPARM_NARROW | DPARM_ASYNC;
1802                                 }
1803 #else
1804                                 nflags = DPARM_DEFAULT;
1805 #endif
1806                                 oflags = sdp->isp_devparam[tgt].goal_flags;
1807                                 sdp->isp_devparam[tgt].goal_flags = nflags;
1808                                 sdp->isp_devparam[tgt].dev_update = 1;
1809                                 isp->isp_update |= (1 << cam_sim_bus(sim));
1810                                 (void) isp_control(isp,
1811                                     ISPCTL_UPDATE_PARAMS, NULL);
1812                                 sdp->isp_devparam[tgt].goal_flags = oflags;
1813                                 ISP_UNLOCK(isp);
1814                         }
1815                 }
1816                 break;
1817         default:
1818                 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
1819                 break;
1820         }
1821 }
1822
1823 static void
1824 isp_poll(struct cam_sim *sim)
1825 {
1826         struct ispsoftc *isp = cam_sim_softc(sim);
1827         u_int16_t isr, sema, mbox;
1828
1829         ISP_LOCK(isp);
1830         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1831                 isp_intr(isp, isr, sema, mbox);
1832         }
1833         ISP_UNLOCK(isp);
1834 }
1835
1836
1837 static void
1838 isp_watchdog(void *arg)
1839 {
1840         XS_T *xs = arg;
1841         struct ispsoftc *isp = XS_ISP(xs);
1842         u_int32_t handle;
1843         int iok;
1844
1845         /*
1846          * We've decided this command is dead. Make sure we're not trying
1847          * to kill a command that's already dead by getting it's handle and
1848          * and seeing whether it's still alive.
1849          */
1850         ISP_LOCK(isp);
1851         iok = isp->isp_osinfo.intsok;
1852         isp->isp_osinfo.intsok = 0;
1853         handle = isp_find_handle(isp, xs);
1854         if (handle) {
1855                 u_int16_t isr, sema, mbox;
1856
1857                 if (XS_CMD_DONE_P(xs)) {
1858                         isp_prt(isp, ISP_LOGDEBUG1,
1859                             "watchdog found done cmd (handle 0x%x)", handle);
1860                         ISP_UNLOCK(isp);
1861                         return;
1862                 }
1863
1864                 if (XS_CMD_WDOG_P(xs)) {
1865                         isp_prt(isp, ISP_LOGDEBUG2,
1866                             "recursive watchdog (handle 0x%x)", handle);
1867                         ISP_UNLOCK(isp);
1868                         return;
1869                 }
1870
1871                 XS_CMD_S_WDOG(xs);
1872                 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
1873                         isp_intr(isp, isr, sema, mbox);
1874                 }
1875                 if (XS_CMD_DONE_P(xs)) {
1876                         isp_prt(isp, ISP_LOGDEBUG2,
1877                             "watchdog cleanup for handle 0x%x", handle);
1878                         xpt_done((union ccb *) xs);
1879                 } else if (XS_CMD_GRACE_P(xs)) {
1880                         /*
1881                          * Make sure the command is *really* dead before we
1882                          * release the handle (and DMA resources) for reuse.
1883                          */
1884                         (void) isp_control(isp, ISPCTL_ABORT_CMD, arg);
1885
1886                         /*
1887                          * After this point, the comamnd is really dead.
1888                          */
1889                         if (XS_XFRLEN(xs)) {
1890                                 ISP_DMAFREE(isp, xs, handle);
1891                         } 
1892                         isp_destroy_handle(isp, handle);
1893                         xpt_print_path(xs->ccb_h.path);
1894                         isp_prt(isp, ISP_LOGWARN,
1895                             "watchdog timeout for handle 0x%x", handle);
1896                         XS_SETERR(xs, CAM_CMD_TIMEOUT);
1897                         XS_CMD_C_WDOG(xs);
1898                         isp_done(xs);
1899                 } else {
1900                         u_int16_t nxti, optr;
1901                         ispreq_t local, *mp= &local, *qe;
1902
1903                         XS_CMD_C_WDOG(xs);
1904                         xs->ccb_h.timeout_ch = timeout(isp_watchdog, xs, hz);
1905                         if (isp_getrqentry(isp, &nxti, &optr, (void **) &qe)) {
1906                                 ISP_UNLOCK(isp);
1907                                 return;
1908                         }
1909                         XS_CMD_S_GRACE(xs);
1910                         MEMZERO((void *) mp, sizeof (*mp));
1911                         mp->req_header.rqs_entry_count = 1;
1912                         mp->req_header.rqs_entry_type = RQSTYPE_MARKER;
1913                         mp->req_modifier = SYNC_ALL;
1914                         mp->req_target = XS_CHANNEL(xs) << 7;
1915                         isp_put_request(isp, mp, qe);
1916                         ISP_ADD_REQUEST(isp, nxti);
1917                 }
1918         } else {
1919                 isp_prt(isp, ISP_LOGDEBUG2, "watchdog with no command");
1920         }
1921         isp->isp_osinfo.intsok = iok;
1922         ISP_UNLOCK(isp);
1923 }
1924
1925 static void
1926 isp_kthread(void *arg)
1927 {
1928         struct ispsoftc *isp = arg;
1929         int s;
1930
1931         s = splcam();
1932         isp->isp_osinfo.intsok = 1;
1933
1934         /*
1935          * The first loop is for our usage where we have yet to have
1936          * gotten good fibre channel state.
1937          */
1938         for (;;) {
1939                 int wasfrozen;
1940
1941                 isp_prt(isp, ISP_LOGDEBUG0, "kthread: checking FC state");
1942                 while (isp_fc_runstate(isp, 2 * 1000000) != 0) {
1943                         isp_prt(isp, ISP_LOGDEBUG0, "kthread: FC state ungood");
1944                         if (FCPARAM(isp)->isp_fwstate != FW_READY ||
1945                             FCPARAM(isp)->isp_loopstate < LOOP_PDB_RCVD) {
1946                                 if (FCPARAM(isp)->loop_seen_once == 0 ||
1947                                     isp->isp_osinfo.ktmature == 0) {
1948                                         break;
1949                                 }
1950                         }
1951                         tsleep(isp_kthread, 0, "isp_fcthrd", hz);
1952
1953                 }
1954
1955                 /*
1956                  * Even if we didn't get good loop state we may be
1957                  * unfreezing the SIMQ so that we can kill off
1958                  * commands (if we've never seen loop before, for example).
1959                  */
1960                 isp->isp_osinfo.ktmature = 1;
1961                 wasfrozen = isp->isp_osinfo.simqfrozen & SIMQFRZ_LOOPDOWN;
1962                 isp->isp_osinfo.simqfrozen &= ~SIMQFRZ_LOOPDOWN;
1963                 if (wasfrozen && isp->isp_osinfo.simqfrozen == 0) {
1964                         isp_prt(isp, ISP_LOGDEBUG0, "kthread: releasing simq");
1965                         ISPLOCK_2_CAMLOCK(isp);
1966                         xpt_release_simq(isp->isp_sim, 1);
1967                         CAMLOCK_2_ISPLOCK(isp);
1968                 }
1969                 tsleep(&isp->isp_osinfo.kthread, 0, "isp_fc_worker", 0);
1970                 isp_prt(isp, ISP_LOGDEBUG0, "kthread: waiting until called");
1971         }
1972 }
1973
1974 static void
1975 isp_action(struct cam_sim *sim, union ccb *ccb)
1976 {
1977         int bus, tgt, error;
1978         struct ispsoftc *isp;
1979         struct ccb_trans_settings *cts;
1980
1981         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
1982         
1983         isp = (struct ispsoftc *)cam_sim_softc(sim);
1984         ccb->ccb_h.sim_priv.entries[0].field = 0;
1985         ccb->ccb_h.sim_priv.entries[1].ptr = isp;
1986         if (isp->isp_state != ISP_RUNSTATE &&
1987             ccb->ccb_h.func_code == XPT_SCSI_IO) {
1988                 CAMLOCK_2_ISPLOCK(isp);
1989                 isp_init(isp);
1990                 if (isp->isp_state != ISP_INITSTATE) {
1991                         ISP_UNLOCK(isp);
1992                         /*
1993                          * Lie. Say it was a selection timeout.
1994                          */
1995                         ccb->ccb_h.status = CAM_SEL_TIMEOUT | CAM_DEV_QFRZN;
1996                         xpt_freeze_devq(ccb->ccb_h.path, 1);
1997                         xpt_done(ccb);
1998                         return;
1999                 }
2000                 isp->isp_state = ISP_RUNSTATE;
2001                 ISPLOCK_2_CAMLOCK(isp);
2002         }
2003         isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
2004
2005
2006         switch (ccb->ccb_h.func_code) {
2007         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
2008                 /*
2009                  * Do a couple of preliminary checks...
2010                  */
2011                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2012                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
2013                                 ccb->ccb_h.status = CAM_REQ_INVALID;
2014                                 xpt_done(ccb);
2015                                 break;
2016                         }
2017                 }
2018 #ifdef  DIAGNOSTIC
2019                 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
2020                         ccb->ccb_h.status = CAM_PATH_INVALID;
2021                 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
2022                         ccb->ccb_h.status = CAM_PATH_INVALID;
2023                 }
2024                 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
2025                         isp_prt(isp, ISP_LOGERR,
2026                             "invalid tgt/lun (%d.%d) in XPT_SCSI_IO",
2027                             ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2028                         xpt_done(ccb);
2029                         break;
2030                 }
2031 #endif
2032                 ((struct ccb_scsiio *) ccb)->scsi_status = SCSI_STATUS_OK;
2033                 CAMLOCK_2_ISPLOCK(isp);
2034                 error = isp_start((XS_T *) ccb);
2035                 switch (error) {
2036                 case CMD_QUEUED:
2037                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
2038                         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2039                                 u_int64_t ticks = (u_int64_t) hz;
2040                                 if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT)
2041                                         ticks = 60 * 1000 * ticks;
2042                                 else
2043                                         ticks = ccb->ccb_h.timeout * hz;
2044                                 ticks = ((ticks + 999) / 1000) + hz + hz;
2045                                 if (ticks >= 0x80000000) {
2046                                         isp_prt(isp, ISP_LOGERR,
2047                                             "timeout overflow");
2048                                         ticks = 0x7fffffff;
2049                                 }
2050                                 ccb->ccb_h.timeout_ch = timeout(isp_watchdog,
2051                                     (caddr_t)ccb, (int)ticks);
2052                         } else {
2053                                 callout_handle_init(&ccb->ccb_h.timeout_ch);
2054                         }
2055                         ISPLOCK_2_CAMLOCK(isp);
2056                         break;
2057                 case CMD_RQLATER:
2058                         /*
2059                          * This can only happen for Fibre Channel
2060                          */
2061                         KASSERT((IS_FC(isp)), ("CMD_RQLATER for FC only"));
2062                         if (FCPARAM(isp)->loop_seen_once == 0 &&
2063                             isp->isp_osinfo.ktmature) {
2064                                 ISPLOCK_2_CAMLOCK(isp);
2065                                 XS_SETERR(ccb, CAM_SEL_TIMEOUT);
2066                                 xpt_done(ccb);
2067                                 break;
2068                         }
2069                         wakeup(&isp->isp_osinfo.kthread);
2070                         isp_freeze_loopdown(isp, "isp_action(RQLATER)");
2071                         isp->isp_osinfo.simqfrozen |= SIMQFRZ_LOOPDOWN;
2072                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
2073                         ISPLOCK_2_CAMLOCK(isp);
2074                         xpt_done(ccb);
2075                         break;
2076                 case CMD_EAGAIN:
2077                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
2078                         ISPLOCK_2_CAMLOCK(isp);
2079                         xpt_done(ccb);
2080                         break;
2081                 case CMD_COMPLETE:
2082                         isp_done((struct ccb_scsiio *) ccb);
2083                         ISPLOCK_2_CAMLOCK(isp);
2084                         break;
2085                 default:
2086                         isp_prt(isp, ISP_LOGERR,
2087                             "What's this? 0x%x at %d in file %s",
2088                             error, __LINE__, __FILE__);
2089                         XS_SETERR(ccb, CAM_REQ_CMP_ERR);
2090                         xpt_done(ccb);
2091                         ISPLOCK_2_CAMLOCK(isp);
2092                 }
2093                 break;
2094
2095 #ifdef  ISP_TARGET_MODE
2096         case XPT_EN_LUN:                /* Enable LUN as a target */
2097         {
2098                 int iok;
2099                 CAMLOCK_2_ISPLOCK(isp);
2100                 iok = isp->isp_osinfo.intsok;
2101                 isp->isp_osinfo.intsok = 0;
2102                 isp_en_lun(isp, ccb);
2103                 isp->isp_osinfo.intsok = iok;
2104                 ISPLOCK_2_CAMLOCK(isp);
2105                 xpt_done(ccb);
2106                 break;
2107         }
2108         case XPT_NOTIFY_ACK:            /* recycle notify ack */
2109         case XPT_IMMED_NOTIFY:          /* Add Immediate Notify Resource */
2110         case XPT_ACCEPT_TARGET_IO:      /* Add Accept Target IO Resource */
2111         {
2112                 tstate_t *tptr =
2113                     get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
2114                 if (tptr == NULL) {
2115                         ccb->ccb_h.status = CAM_LUN_INVALID;
2116                         xpt_done(ccb);
2117                         break;
2118                 }
2119                 ccb->ccb_h.sim_priv.entries[0].field = 0;
2120                 ccb->ccb_h.sim_priv.entries[1].ptr = isp;
2121                 ccb->ccb_h.flags = 0;
2122
2123                 CAMLOCK_2_ISPLOCK(isp);
2124                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
2125                         /*
2126                          * Note that the command itself may not be done-
2127                          * it may not even have had the first CTIO sent.
2128                          */
2129                         tptr->atio_count++;
2130                         isp_prt(isp, ISP_LOGTDEBUG0,
2131                             "Put FREE ATIO2, lun %d, count now %d",
2132                             ccb->ccb_h.target_lun, tptr->atio_count);
2133                         SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h,
2134                             sim_links.sle);
2135                 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
2136                         SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h,
2137                             sim_links.sle);
2138                 } else {
2139                         ;
2140                 }
2141                 rls_lun_statep(isp, tptr);
2142                 ccb->ccb_h.status = CAM_REQ_INPROG;
2143                 ISPLOCK_2_CAMLOCK(isp);
2144                 break;
2145         }
2146         case XPT_CONT_TARGET_IO:
2147         {
2148                 CAMLOCK_2_ISPLOCK(isp);
2149                 ccb->ccb_h.status = isp_target_start_ctio(isp, ccb);
2150                 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
2151                         isp_prt(isp, ISP_LOGWARN,
2152                             "XPT_CONT_TARGET_IO: status 0x%x",
2153                             ccb->ccb_h.status);
2154                         XS_SETERR(ccb, CAM_REQUEUE_REQ);
2155                         ISPLOCK_2_CAMLOCK(isp);
2156                         xpt_done(ccb);
2157                 } else {
2158                         ISPLOCK_2_CAMLOCK(isp);
2159                         ccb->ccb_h.status |= CAM_SIM_QUEUED;
2160                 }
2161                 break;
2162         }
2163 #endif
2164         case XPT_RESET_DEV:             /* BDR the specified SCSI device */
2165
2166                 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
2167                 tgt = ccb->ccb_h.target_id;
2168                 tgt |= (bus << 16);
2169
2170                 CAMLOCK_2_ISPLOCK(isp);
2171                 error = isp_control(isp, ISPCTL_RESET_DEV, &tgt);
2172                 ISPLOCK_2_CAMLOCK(isp);
2173                 if (error) {
2174                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2175                 } else {
2176                         ccb->ccb_h.status = CAM_REQ_CMP;
2177                 }
2178                 xpt_done(ccb);
2179                 break;
2180         case XPT_ABORT:                 /* Abort the specified CCB */
2181         {
2182                 union ccb *accb = ccb->cab.abort_ccb;
2183                 CAMLOCK_2_ISPLOCK(isp);
2184                 switch (accb->ccb_h.func_code) {
2185 #ifdef  ISP_TARGET_MODE
2186                 case XPT_ACCEPT_TARGET_IO:
2187                 case XPT_IMMED_NOTIFY:
2188                         ccb->ccb_h.status = isp_abort_tgt_ccb(isp, ccb);
2189                         break;
2190                 case XPT_CONT_TARGET_IO:
2191                         isp_prt(isp, ISP_LOGERR, "cannot abort CTIOs yet");
2192                         ccb->ccb_h.status = CAM_UA_ABORT;
2193                         break;
2194 #endif
2195                 case XPT_SCSI_IO:
2196                         error = isp_control(isp, ISPCTL_ABORT_CMD, ccb);
2197                         if (error) {
2198                                 ccb->ccb_h.status = CAM_UA_ABORT;
2199                         } else {
2200                                 ccb->ccb_h.status = CAM_REQ_CMP;
2201                         }
2202                         break;
2203                 default:
2204                         ccb->ccb_h.status = CAM_REQ_INVALID;
2205                         break;
2206                 }
2207                 ISPLOCK_2_CAMLOCK(isp);
2208                 xpt_done(ccb);
2209                 break;
2210         }
2211 #define IS_CURRENT_SETTINGS(c)  (c->flags & CCB_TRANS_CURRENT_SETTINGS)
2212         case XPT_SET_TRAN_SETTINGS:     /* Nexus Settings */
2213                 cts = &ccb->cts;
2214                 if (!IS_CURRENT_SETTINGS(cts)) {
2215                         ccb->ccb_h.status = CAM_REQ_INVALID;
2216                         xpt_done(ccb);
2217                         break;
2218                 }
2219                 tgt = cts->ccb_h.target_id;
2220                 CAMLOCK_2_ISPLOCK(isp);
2221                 if (IS_SCSI(isp)) {
2222                         sdparam *sdp = isp->isp_param;
2223                         u_int16_t *dptr;
2224
2225                         bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2226
2227                         sdp += bus;
2228                         /*
2229                          * We always update (internally) from goal_flags
2230                          * so any request to change settings just gets
2231                          * vectored to that location.
2232                          */
2233                         dptr = &sdp->isp_devparam[tgt].goal_flags;
2234
2235                         /*
2236                          * Note that these operations affect the
2237                          * the goal flags (goal_flags)- not
2238                          * the current state flags. Then we mark
2239                          * things so that the next operation to
2240                          * this HBA will cause the update to occur.
2241                          */
2242                         if (cts->valid & CCB_TRANS_DISC_VALID) {
2243                                 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
2244                                         *dptr |= DPARM_DISC;
2245                                 } else {
2246                                         *dptr &= ~DPARM_DISC;
2247                                 }
2248                         }
2249                         if (cts->valid & CCB_TRANS_TQ_VALID) {
2250                                 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
2251                                         *dptr |= DPARM_TQING;
2252                                 } else {
2253                                         *dptr &= ~DPARM_TQING;
2254                                 }
2255                         }
2256                         if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) {
2257                                 switch (cts->bus_width) {
2258                                 case MSG_EXT_WDTR_BUS_16_BIT:
2259                                         *dptr |= DPARM_WIDE;
2260                                         break;
2261                                 default:
2262                                         *dptr &= ~DPARM_WIDE;
2263                                 }
2264                         }
2265                         /*
2266                          * Any SYNC RATE of nonzero and SYNC_OFFSET
2267                          * of nonzero will cause us to go to the
2268                          * selected (from NVRAM) maximum value for
2269                          * this device. At a later point, we'll
2270                          * allow finer control.
2271                          */
2272                         if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
2273                             (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) &&
2274                             (cts->sync_offset > 0)) {
2275                                 *dptr |= DPARM_SYNC;
2276                         } else {
2277                                 *dptr &= ~DPARM_SYNC;
2278                         }
2279                         *dptr |= DPARM_SAFE_DFLT;
2280                         isp_prt(isp, ISP_LOGDEBUG0,
2281                             "SET bus %d targ %d to flags %x off %x per %x",
2282                             bus, tgt, sdp->isp_devparam[tgt].goal_flags,
2283                             sdp->isp_devparam[tgt].goal_offset,
2284                             sdp->isp_devparam[tgt].goal_period);
2285                         sdp->isp_devparam[tgt].dev_update = 1;
2286                         isp->isp_update |= (1 << bus);
2287                 }
2288                 ISPLOCK_2_CAMLOCK(isp);
2289                 ccb->ccb_h.status = CAM_REQ_CMP;
2290                 xpt_done(ccb);
2291                 break;
2292         case XPT_GET_TRAN_SETTINGS:
2293                 cts = &ccb->cts;
2294                 tgt = cts->ccb_h.target_id;
2295                 CAMLOCK_2_ISPLOCK(isp);
2296                 if (IS_FC(isp)) {
2297                         /*
2298                          * a lot of normal SCSI things don't make sense.
2299                          */
2300                         cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
2301                         cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2302                         /*
2303                          * How do you measure the width of a high
2304                          * speed serial bus? Well, in bytes.
2305                          *
2306                          * Offset and period make no sense, though, so we set
2307                          * (above) a 'base' transfer speed to be gigabit.
2308                          */
2309                         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2310                 } else {
2311                         sdparam *sdp = isp->isp_param;
2312                         int bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
2313                         u_int16_t dval, pval, oval;
2314
2315                         sdp += bus;
2316
2317                         if (IS_CURRENT_SETTINGS(cts)) {
2318                                 sdp->isp_devparam[tgt].dev_refresh = 1;
2319                                 isp->isp_update |= (1 << bus);
2320                                 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS,
2321                                     NULL);
2322                                 dval = sdp->isp_devparam[tgt].actv_flags;
2323                                 oval = sdp->isp_devparam[tgt].actv_offset;
2324                                 pval = sdp->isp_devparam[tgt].actv_period;
2325                         } else {
2326                                 dval = sdp->isp_devparam[tgt].nvrm_flags;
2327                                 oval = sdp->isp_devparam[tgt].nvrm_offset;
2328                                 pval = sdp->isp_devparam[tgt].nvrm_period;
2329                         }
2330
2331                         cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
2332
2333                         if (dval & DPARM_DISC) {
2334                                 cts->flags |= CCB_TRANS_DISC_ENB;
2335                         }
2336                         if (dval & DPARM_TQING) {
2337                                 cts->flags |= CCB_TRANS_TAG_ENB;
2338                         }
2339                         if (dval & DPARM_WIDE) {
2340                                 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2341                         } else {
2342                                 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2343                         }
2344                         cts->valid = CCB_TRANS_BUS_WIDTH_VALID |
2345                             CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2346
2347                         if ((dval & DPARM_SYNC) && oval != 0) {
2348                                 cts->sync_period = pval;
2349                                 cts->sync_offset = oval;
2350                                 cts->valid |=
2351                                     CCB_TRANS_SYNC_RATE_VALID |
2352                                     CCB_TRANS_SYNC_OFFSET_VALID;
2353                         }
2354                         isp_prt(isp, ISP_LOGDEBUG0,
2355                             "GET %s bus %d targ %d to flags %x off %x per %x",
2356                             IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
2357                             bus, tgt, dval, oval, pval);
2358                 }
2359                 ISPLOCK_2_CAMLOCK(isp);
2360                 ccb->ccb_h.status = CAM_REQ_CMP;
2361                 xpt_done(ccb);
2362                 break;
2363
2364         case XPT_CALC_GEOMETRY:
2365         {
2366                 struct ccb_calc_geometry *ccg;
2367                 u_int32_t secs_per_cylinder;
2368                 u_int32_t size_mb;
2369
2370                 ccg = &ccb->ccg;
2371                 if (ccg->block_size == 0) {
2372                         isp_prt(isp, ISP_LOGERR,
2373                             "%d.%d XPT_CALC_GEOMETRY block size 0?",
2374                             ccg->ccb_h.target_id, ccg->ccb_h.target_lun);
2375                         ccb->ccb_h.status = CAM_REQ_INVALID;
2376                         xpt_done(ccb);
2377                         break;
2378                 }
2379                 size_mb = ccg->volume_size /((1024L * 1024L) / ccg->block_size);
2380                 if (size_mb > 1024) {
2381                         ccg->heads = 255;
2382                         ccg->secs_per_track = 63;
2383                 } else {
2384                         ccg->heads = 64;
2385                         ccg->secs_per_track = 32;
2386                 }
2387                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2388                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2389                 ccb->ccb_h.status = CAM_REQ_CMP;
2390                 xpt_done(ccb);
2391                 break;
2392         }
2393         case XPT_RESET_BUS:             /* Reset the specified bus */
2394                 bus = cam_sim_bus(sim);
2395                 CAMLOCK_2_ISPLOCK(isp);
2396                 error = isp_control(isp, ISPCTL_RESET_BUS, &bus);
2397                 ISPLOCK_2_CAMLOCK(isp);
2398                 if (error)
2399                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2400                 else {
2401                         if (cam_sim_bus(sim) && isp->isp_path2 != NULL)
2402                                 xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2403                         else if (isp->isp_path != NULL)
2404                                 xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2405                         ccb->ccb_h.status = CAM_REQ_CMP;
2406                 }
2407                 xpt_done(ccb);
2408                 break;
2409
2410         case XPT_TERM_IO:               /* Terminate the I/O process */
2411                 ccb->ccb_h.status = CAM_REQ_INVALID;
2412                 xpt_done(ccb);
2413                 break;
2414
2415         case XPT_PATH_INQ:              /* Path routing inquiry */
2416         {
2417                 struct ccb_pathinq *cpi = &ccb->cpi;
2418
2419                 cpi->version_num = 1;
2420 #ifdef  ISP_TARGET_MODE
2421                 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2422 #else
2423                 cpi->target_sprt = 0;
2424 #endif
2425                 cpi->hba_eng_cnt = 0;
2426                 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
2427                 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
2428                 cpi->bus_id = cam_sim_bus(sim);
2429                 if (IS_FC(isp)) {
2430                         cpi->hba_misc = PIM_NOBUSRESET;
2431                         /*
2432                          * Because our loop ID can shift from time to time,
2433                          * make our initiator ID out of range of our bus.
2434                          */
2435                         cpi->initiator_id = cpi->max_target + 1;
2436
2437                         /*
2438                          * Set base transfer capabilities for Fibre Channel.
2439                          * Technically not correct because we don't know
2440                          * what media we're running on top of- but we'll
2441                          * look good if we always say 100MB/s.
2442                          */
2443                         if (FCPARAM(isp)->isp_gbspeed == 2)
2444                                 cpi->base_transfer_speed = 200000;
2445                         else
2446                                 cpi->base_transfer_speed = 100000;
2447                         cpi->hba_inquiry = PI_TAG_ABLE;
2448                 } else {
2449                         sdparam *sdp = isp->isp_param;
2450                         sdp += cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
2451                         cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
2452                         cpi->hba_misc = 0;
2453                         cpi->initiator_id = sdp->isp_initiator_id;
2454                         cpi->base_transfer_speed = 3300;
2455                 }
2456                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2457                 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
2458                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2459                 cpi->unit_number = cam_sim_unit(sim);
2460                 cpi->ccb_h.status = CAM_REQ_CMP;
2461                 xpt_done(ccb);
2462                 break;
2463         }
2464         default:
2465                 ccb->ccb_h.status = CAM_REQ_INVALID;
2466                 xpt_done(ccb);
2467                 break;
2468         }
2469 }
2470
2471 #define ISPDDB  (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
2472 void
2473 isp_done(struct ccb_scsiio *sccb)
2474 {
2475         struct ispsoftc *isp = XS_ISP(sccb);
2476
2477         if (XS_NOERR(sccb))
2478                 XS_SETERR(sccb, CAM_REQ_CMP);
2479
2480         if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
2481             (sccb->scsi_status != SCSI_STATUS_OK)) {
2482                 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
2483                 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && 
2484                     (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
2485                         sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
2486                 } else {
2487                         sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
2488                 }
2489         }
2490
2491         sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2492         if ((sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2493                 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
2494                         sccb->ccb_h.status |= CAM_DEV_QFRZN;
2495                         xpt_freeze_devq(sccb->ccb_h.path, 1);
2496                         isp_prt(isp, ISP_LOGDEBUG0,
2497                             "freeze devq %d.%d cam sts %x scsi sts %x",
2498                             sccb->ccb_h.target_id, sccb->ccb_h.target_lun,
2499                             sccb->ccb_h.status, sccb->scsi_status);
2500                 }
2501         }
2502
2503         if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) &&
2504             (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2505                 xpt_print_path(sccb->ccb_h.path);
2506                 isp_prt(isp, ISP_LOGINFO, 
2507                     "cam completion status 0x%x", sccb->ccb_h.status);
2508         }
2509
2510         XS_CMD_S_DONE(sccb);
2511         if (XS_CMD_WDOG_P(sccb) == 0) {
2512                 untimeout(isp_watchdog, (caddr_t)sccb, sccb->ccb_h.timeout_ch);
2513                 if (XS_CMD_GRACE_P(sccb)) {
2514                         isp_prt(isp, ISP_LOGDEBUG2,
2515                             "finished command on borrowed time");
2516                 }
2517                 XS_CMD_S_CLEAR(sccb);
2518                 ISPLOCK_2_CAMLOCK(isp);
2519                 xpt_done((union ccb *) sccb);
2520                 CAMLOCK_2_ISPLOCK(isp);
2521         }
2522 }
2523
2524 int
2525 isp_async(struct ispsoftc *isp, ispasync_t cmd, void *arg)
2526 {
2527         int bus, rv = 0;
2528         switch (cmd) {
2529         case ISPASYNC_NEW_TGT_PARAMS:
2530         {
2531                 int flags, tgt;
2532                 sdparam *sdp = isp->isp_param;
2533                 struct ccb_trans_settings cts;
2534                 struct cam_path *tmppath;
2535
2536                 bzero(&cts, sizeof (struct ccb_trans_settings));
2537
2538                 tgt = *((int *)arg);
2539                 bus = (tgt >> 16) & 0xffff;
2540                 tgt &= 0xffff;
2541                 sdp += bus;
2542                 ISPLOCK_2_CAMLOCK(isp);
2543                 if (xpt_create_path(&tmppath, NULL,
2544                     cam_sim_path(bus? isp->isp_sim2 : isp->isp_sim),
2545                     tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2546                         CAMLOCK_2_ISPLOCK(isp);
2547                         isp_prt(isp, ISP_LOGWARN,
2548                             "isp_async cannot make temp path for %d.%d",
2549                             tgt, bus);
2550                         rv = -1;
2551                         break;
2552                 }
2553                 CAMLOCK_2_ISPLOCK(isp);
2554                 flags = sdp->isp_devparam[tgt].actv_flags;
2555                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2556                 cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2557                 if (flags & DPARM_DISC) {
2558                         cts.flags |= CCB_TRANS_DISC_ENB;
2559                 }
2560                 if (flags & DPARM_TQING) {
2561                         cts.flags |= CCB_TRANS_TAG_ENB;
2562                 }
2563                 cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2564                 cts.bus_width = (flags & DPARM_WIDE)?
2565                     MSG_EXT_WDTR_BUS_8_BIT : MSG_EXT_WDTR_BUS_16_BIT;
2566                 cts.sync_period = sdp->isp_devparam[tgt].actv_period;
2567                 cts.sync_offset = sdp->isp_devparam[tgt].actv_offset;
2568                 if (flags & DPARM_SYNC) {
2569                         cts.valid |=
2570                             CCB_TRANS_SYNC_RATE_VALID |
2571                             CCB_TRANS_SYNC_OFFSET_VALID;
2572                 }
2573                 isp_prt(isp, ISP_LOGDEBUG2,
2574                     "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x",
2575                     bus, tgt, sdp->isp_devparam[tgt].actv_period,
2576                     sdp->isp_devparam[tgt].actv_offset, flags);
2577                 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
2578                 ISPLOCK_2_CAMLOCK(isp);
2579                 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
2580                 xpt_free_path(tmppath);
2581                 CAMLOCK_2_ISPLOCK(isp);
2582                 break;
2583         }
2584         case ISPASYNC_BUS_RESET:
2585                 bus = *((int *)arg);
2586                 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected",
2587                     bus);
2588                 if (bus > 0 && isp->isp_path2) {
2589                         ISPLOCK_2_CAMLOCK(isp);
2590                         xpt_async(AC_BUS_RESET, isp->isp_path2, NULL);
2591                         CAMLOCK_2_ISPLOCK(isp);
2592                 } else if (isp->isp_path) {
2593                         ISPLOCK_2_CAMLOCK(isp);
2594                         xpt_async(AC_BUS_RESET, isp->isp_path, NULL);
2595                         CAMLOCK_2_ISPLOCK(isp);
2596                 }
2597                 break;
2598         case ISPASYNC_LIP:
2599                 if (isp->isp_path) {
2600                         isp_freeze_loopdown(isp, "ISPASYNC_LIP");
2601                 }
2602                 isp_prt(isp, ISP_LOGINFO, "LIP Received");
2603                 break;
2604         case ISPASYNC_LOOP_RESET:
2605                 if (isp->isp_path) {
2606                         isp_freeze_loopdown(isp, "ISPASYNC_LOOP_RESET");
2607                 }
2608                 isp_prt(isp, ISP_LOGINFO, "Loop Reset Received");
2609                 break;
2610         case ISPASYNC_LOOP_DOWN:
2611                 if (isp->isp_path) {
2612                         isp_freeze_loopdown(isp, "ISPASYNC_LOOP_DOWN");
2613                 }
2614                 isp_prt(isp, ISP_LOGINFO, "Loop DOWN");
2615                 break;
2616         case ISPASYNC_LOOP_UP:
2617                 /*
2618                  * Now we just note that Loop has come up. We don't
2619                  * actually do anything because we're waiting for a
2620                  * Change Notify before activating the FC cleanup
2621                  * thread to look at the state of the loop again.
2622                  */
2623                 isp_prt(isp, ISP_LOGINFO, "Loop UP");
2624                 break;
2625         case ISPASYNC_PROMENADE:
2626         {
2627                 const char *fmt = "Target %d (Loop 0x%x) Port ID 0x%x "
2628                     "(role %s) %s\n Port WWN 0x%08x%08x\n Node WWN 0x%08x%08x";
2629                 static const char *roles[4] = {
2630                     "(none)", "Target", "Initiator", "Target/Initiator"
2631                 };
2632                 fcparam *fcp = isp->isp_param;
2633                 int tgt = *((int *) arg);
2634                 struct lportdb *lp = &fcp->portdb[tgt]; 
2635
2636                 isp_prt(isp, ISP_LOGINFO, fmt, tgt, lp->loopid, lp->portid,
2637                     roles[lp->roles & 0x3],
2638                     (lp->valid)? "Arrived" : "Departed",
2639                     (u_int32_t) (lp->port_wwn >> 32),
2640                     (u_int32_t) (lp->port_wwn & 0xffffffffLL),
2641                     (u_int32_t) (lp->node_wwn >> 32),
2642                     (u_int32_t) (lp->node_wwn & 0xffffffffLL));
2643
2644                 break;
2645         }
2646         case ISPASYNC_CHANGE_NOTIFY:
2647                 if (arg == ISPASYNC_CHANGE_PDB) {
2648                         isp_prt(isp, ISP_LOGINFO,
2649                             "Port Database Changed");
2650                 } else if (arg == ISPASYNC_CHANGE_SNS) {
2651                         isp_prt(isp, ISP_LOGINFO,
2652                             "Name Server Database Changed");
2653                 }
2654                 wakeup(&isp->isp_osinfo.kthread);
2655                 break;
2656         case ISPASYNC_FABRIC_DEV:
2657         {
2658                 int target, base, lim;
2659                 fcparam *fcp = isp->isp_param;
2660                 struct lportdb *lp = NULL;
2661                 struct lportdb *clp = (struct lportdb *) arg;
2662                 char *pt;
2663
2664                 switch (clp->port_type) {
2665                 case 1:
2666                         pt = "   N_Port";
2667                         break;
2668                 case 2:
2669                         pt = "  NL_Port";
2670                         break;
2671                 case 3:
2672                         pt = "F/NL_Port";
2673                         break;
2674                 case 0x7f:
2675                         pt = "  Nx_Port";
2676                         break;
2677                 case 0x81:
2678                         pt = "  F_port";
2679                         break;
2680                 case 0x82:
2681                         pt = "  FL_Port";
2682                         break;
2683                 case 0x84:
2684                         pt = "   E_port";
2685                         break;
2686                 default:
2687                         pt = " ";
2688                         break;
2689                 }
2690
2691                 isp_prt(isp, ISP_LOGINFO,
2692                     "%s Fabric Device @ PortID 0x%x", pt, clp->portid);
2693
2694                 /*
2695                  * If we don't have an initiator role we bail.
2696                  *
2697                  * We just use ISPASYNC_FABRIC_DEV for announcement purposes.
2698                  */
2699
2700                 if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
2701                         break;
2702                 }
2703
2704                 /*
2705                  * Is this entry for us? If so, we bail.
2706                  */
2707
2708                 if (fcp->isp_portid == clp->portid) {
2709                         break;
2710                 }
2711
2712                 /*
2713                  * Else, the default policy is to find room for it in
2714                  * our local port database. Later, when we execute
2715                  * the call to isp_pdb_sync either this newly arrived
2716                  * or already logged in device will be (re)announced.
2717                  */
2718
2719                 if (fcp->isp_topo == TOPO_FL_PORT)
2720                         base = FC_SNS_ID+1;
2721                 else
2722                         base = 0;
2723
2724                 if (fcp->isp_topo == TOPO_N_PORT)
2725                         lim = 1;
2726                 else
2727                         lim = MAX_FC_TARG;
2728
2729                 /*
2730                  * Is it already in our list?
2731                  */
2732                 for (target = base; target < lim; target++) {
2733                         if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2734                                 continue;
2735                         }
2736                         lp = &fcp->portdb[target];
2737                         if (lp->port_wwn == clp->port_wwn &&
2738                             lp->node_wwn == clp->node_wwn) {
2739                                 lp->fabric_dev = 1;
2740                                 break;
2741                         }
2742                 }
2743                 if (target < lim) {
2744                         break;
2745                 }
2746                 for (target = base; target < lim; target++) {
2747                         if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2748                                 continue;
2749                         }
2750                         lp = &fcp->portdb[target];
2751                         if (lp->port_wwn == 0) {
2752                                 break;
2753                         }
2754                 }
2755                 if (target == lim) {
2756                         isp_prt(isp, ISP_LOGWARN,
2757                             "out of space for fabric devices");
2758                         break;
2759                 }
2760                 lp->port_type = clp->port_type;
2761                 lp->fc4_type = clp->fc4_type;
2762                 lp->node_wwn = clp->node_wwn;
2763                 lp->port_wwn = clp->port_wwn;
2764                 lp->portid = clp->portid;
2765                 lp->fabric_dev = 1;
2766                 break;
2767         }
2768 #ifdef  ISP_TARGET_MODE
2769         case ISPASYNC_TARGET_MESSAGE:
2770         {
2771                 tmd_msg_t *mp = arg;
2772                 isp_prt(isp, ISP_LOGALL,
2773                     "bus %d iid %d tgt %d lun %d ttype %x tval %x msg[0]=%x",
2774                     mp->nt_bus, (int) mp->nt_iid, (int) mp->nt_tgt,
2775                     (int) mp->nt_lun, mp->nt_tagtype, mp->nt_tagval,
2776                     mp->nt_msg[0]);
2777                 break;
2778         }
2779         case ISPASYNC_TARGET_EVENT:
2780         {
2781                 tmd_event_t *ep = arg;
2782                 isp_prt(isp, ISP_LOGALL,
2783                     "bus %d event code 0x%x", ep->ev_bus, ep->ev_event);
2784                 break;
2785         }
2786         case ISPASYNC_TARGET_ACTION:
2787                 switch (((isphdr_t *)arg)->rqs_entry_type) {
2788                 default:
2789                         isp_prt(isp, ISP_LOGWARN,
2790                            "event 0x%x for unhandled target action",
2791                             ((isphdr_t *)arg)->rqs_entry_type);
2792                         break;
2793                 case RQSTYPE_NOTIFY:
2794                         if (IS_SCSI(isp)) {
2795                                 rv = isp_handle_platform_notify_scsi(isp,
2796                                     (in_entry_t *) arg);
2797                         } else {
2798                                 rv = isp_handle_platform_notify_fc(isp,
2799                                     (in_fcentry_t *) arg);
2800                         }
2801                         break;
2802                 case RQSTYPE_ATIO:
2803                         rv = isp_handle_platform_atio(isp, (at_entry_t *) arg);
2804                         break;
2805                 case RQSTYPE_ATIO2:
2806                         rv = isp_handle_platform_atio2(isp, (at2_entry_t *)arg);
2807                         break;
2808                 case RQSTYPE_CTIO2:
2809                 case RQSTYPE_CTIO:
2810                         rv = isp_handle_platform_ctio(isp, arg);
2811                         break;
2812                 case RQSTYPE_ENABLE_LUN:
2813                 case RQSTYPE_MODIFY_LUN:
2814                         if (IS_DUALBUS(isp)) {
2815                                 bus =
2816                                     GET_BUS_VAL(((lun_entry_t *)arg)->le_rsvd);
2817                         } else {
2818                                 bus = 0;
2819                         }
2820                         isp_cv_signal_rqe(isp, bus,
2821                             ((lun_entry_t *)arg)->le_status);
2822                         break;
2823                 }
2824                 break;
2825 #endif
2826         case ISPASYNC_FW_CRASH:
2827         {
2828                 u_int16_t mbox1, mbox6;
2829                 mbox1 = ISP_READ(isp, OUTMAILBOX1);
2830                 if (IS_DUALBUS(isp)) { 
2831                         mbox6 = ISP_READ(isp, OUTMAILBOX6);
2832                 } else {
2833                         mbox6 = 0;
2834                 }
2835                 isp_prt(isp, ISP_LOGERR,
2836                     "Internal Firmware Error on bus %d @ RISC Address 0x%x",
2837                     mbox6, mbox1);
2838 #ifdef  ISP_FW_CRASH_DUMP
2839                 /*
2840                  * XXX: really need a thread to do this right.
2841                  */
2842                 if (IS_FC(isp)) {
2843                         FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
2844                         FCPARAM(isp)->isp_loopstate = LOOP_NIL;
2845                         isp_freeze_loopdown(isp, "f/w crash");
2846                         isp_fw_dump(isp);
2847                 }
2848                 isp_reinit(isp);
2849                 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
2850 #endif
2851                 break;
2852         }
2853         case ISPASYNC_UNHANDLED_RESPONSE:
2854                 break;
2855         default:
2856                 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
2857                 break;
2858         }
2859         return (rv);
2860 }
2861
2862
2863 /*
2864  * Locks are held before coming here.
2865  */
2866 void
2867 isp_uninit(struct ispsoftc *isp)
2868 {
2869         ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
2870         DISABLE_INTS(isp);
2871 }
2872
2873 void
2874 isp_prt(struct ispsoftc *isp, int level, const char *fmt, ...)
2875 {
2876         __va_list ap;
2877         if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
2878                 return;
2879         }
2880         printf("%s: ", device_get_nameunit(isp->isp_dev));
2881         __va_start(ap, fmt);
2882         vprintf(fmt, ap);
2883         __va_end(ap);
2884         printf("\n");
2885 }