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