AHD: Add a DELAY() in the XPT_RESET_BUS path to avoid a race.
[dragonfly.git] / sys / dev / disk / aic7xxx / aic79xx_osm.c
1 /*
2  * Bus independent FreeBSD shim for the aic79xx based Adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2002, 2004 Justin T. Gibbs.
5  * Copyright (c) 2001-2002 Adaptec Inc.
6  * All rights reserved.
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, this list of conditions, and the following disclaimer,
13  *    without modification.
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  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.c#35 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.c,v 1.23 2005/12/04 02:12:40 ru Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.27 2008/05/18 20:30:21 pavalos Exp $
36  */
37
38 #include "aic79xx_osm.h"
39 #include "aic79xx_inline.h"
40
41 #include <sys/kthread.h>
42
43 #include "opt_ddb.h"
44 #ifdef DDB
45 #include <ddb/ddb.h>
46 #endif
47
48 #ifndef AHD_TMODE_ENABLE
49 #define AHD_TMODE_ENABLE 0
50 #endif
51
52 #include "aic_osm_lib.c"
53
54 #define ccb_scb_ptr spriv_ptr0
55
56 #if 0
57 static void     ahd_dump_targcmd(struct target_cmd *cmd);
58 #endif
59 static int      ahd_modevent(module_t mod, int type, void *data);
60 static void     ahd_action(struct cam_sim *sim, union ccb *ccb);
61 static void     ahd_set_tran_settings(struct ahd_softc *ahd,
62                                       int our_id, char channel,
63                                       struct ccb_trans_settings *cts);
64 static void     ahd_get_tran_settings(struct ahd_softc *ahd,
65                                       int our_id, char channel,
66                                       struct ccb_trans_settings *cts);
67 static void     ahd_async(void *callback_arg, uint32_t code,
68                           struct cam_path *path, void *arg);
69 static void     ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
70                                 int nsegments, int error);
71 static void     ahd_poll(struct cam_sim *sim);
72 static void     ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
73                                struct ccb_scsiio *csio, struct scb *scb);
74 static void     ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
75                               union ccb *ccb);
76 static int      ahd_create_path(struct ahd_softc *ahd,
77                                 char channel, u_int target, u_int lun,
78                                 struct cam_path **path);
79
80 static int
81 ahd_create_path(struct ahd_softc *ahd, char channel, u_int target,
82                 u_int lun, struct cam_path **path)
83 {
84         path_id_t path_id;
85
86         path_id = cam_sim_path(ahd->platform_data->sim);
87         return (xpt_create_path(path, /*periph*/NULL,
88                                 path_id, target, lun));
89 }
90
91 int
92 ahd_map_int(struct ahd_softc *ahd)
93 {
94         int error;
95
96         /* Hook up our interrupt handler */
97         error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
98                                INTR_MPSAFE, ahd_platform_intr, ahd,
99                                &ahd->platform_data->ih, NULL);
100         if (error != 0)
101                 device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
102                               error);
103         return (error);
104 }
105
106 /*
107  * Attach all the sub-devices we can find
108  */
109 int
110 ahd_attach(struct ahd_softc *ahd)
111 {
112         char   ahd_info[256];
113         struct ccb_setasync csa;
114         struct cam_sim *sim;
115         struct cam_path *path;
116         int count;
117
118         count = 0;
119         sim = NULL;
120
121         /*
122          * Create a thread to perform all recovery.
123          */
124         if (ahd_spawn_recovery_thread(ahd) != 0)
125                 goto fail;
126
127         ahd_controller_info(ahd, ahd_info);
128         kprintf("%s\n", ahd_info);
129         ahd_lock(ahd);
130
131         /*
132          * Construct our SIM entry
133          */
134         sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
135                             device_get_unit(ahd->dev_softc),
136                             &ahd->platform_data->lock, 1, AHD_MAX_QUEUE, NULL);
137         if (sim == NULL)
138                 goto fail;
139
140         if (xpt_bus_register(sim, /*bus_id*/0) != CAM_SUCCESS) {
141                 cam_sim_free(sim);
142                 sim = NULL;
143                 goto fail;
144         }
145         
146         if (xpt_create_path(&path, /*periph*/NULL,
147                             cam_sim_path(sim), CAM_TARGET_WILDCARD,
148                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
149                 xpt_bus_deregister(cam_sim_path(sim));
150                 cam_sim_free(sim);
151                 sim = NULL;
152                 goto fail;
153         }
154                 
155         xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
156         csa.ccb_h.func_code = XPT_SASYNC_CB;
157         csa.event_enable = AC_LOST_DEVICE;
158         csa.callback = ahd_async;
159         csa.callback_arg = sim;
160         xpt_action((union ccb *)&csa);
161         count++;
162
163 fail:
164         ahd->platform_data->sim = sim;
165         ahd->platform_data->path = path;
166         ahd_unlock(ahd);
167         if (count != 0) {
168                 /* We have to wait until after any system dumps... */
169                 ahd->platform_data->eh =
170                     EVENTHANDLER_REGISTER(shutdown_post_sync, ahd_shutdown,
171                                           ahd, SHUTDOWN_PRI_DRIVER);
172                 ahd_intr_enable(ahd, TRUE);
173         }
174
175         return (count);
176 }
177
178 /*
179  * Catch an interrupt from the adapter
180  */
181 void
182 ahd_platform_intr(void *arg)
183 {
184         struct  ahd_softc *ahd;
185
186         ahd = (struct ahd_softc *)arg; 
187         ahd_lock(ahd);
188         ahd_intr(ahd);
189         ahd_unlock(ahd);
190 }
191
192 /*
193  * We have an scb which has been processed by the
194  * adaptor, now we look to see how the operation
195  * went.
196  */
197 void
198 ahd_done(struct ahd_softc *ahd, struct scb *scb)
199 {
200         union ccb *ccb;
201
202         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
203                   ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
204
205         ccb = scb->io_ctx;
206         LIST_REMOVE(scb, pending_links);
207         if ((scb->flags & SCB_TIMEDOUT) != 0)
208                 LIST_REMOVE(scb, timedout_links);
209
210         callout_stop(&scb->io_timer);
211
212         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
213                 bus_dmasync_op_t op;
214
215                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
216                         op = BUS_DMASYNC_POSTREAD;
217                 else
218                         op = BUS_DMASYNC_POSTWRITE;
219                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
220                 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
221         }
222
223 #ifdef AHD_TARGET_MODE
224         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
225                 struct cam_path *ccb_path;
226
227                 /*
228                  * If we have finally disconnected, clean up our
229                  * pending device state.
230                  * XXX - There may be error states that cause where
231                  *       we will remain connected.
232                  */
233                 ccb_path = ccb->ccb_h.path;
234                 if (ahd->pending_device != NULL
235                  && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
236
237                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
238                                 ahd->pending_device = NULL;
239                         } else {
240                                 xpt_print_path(ccb->ccb_h.path);
241                                 kprintf("Still disconnected\n");
242                                 ahd_freeze_ccb(ccb);
243                         }
244                 }
245
246                 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
247                         ccb->ccb_h.status |= CAM_REQ_CMP;
248                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
249                 ahd_free_scb(ahd, scb);
250                 xpt_done(ccb);
251                 return;
252         }
253 #endif
254
255         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
256                 struct  scb *list_scb;
257
258                 ahd->scb_data.recovery_scbs--;
259
260                 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
261                  || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
262                         aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
263
264                 if (ahd->scb_data.recovery_scbs == 0) {
265                         /*
266                          * All recovery actions have completed successfully,
267                          * so reinstate the timeouts for all other pending
268                          * commands.
269                          */
270                         LIST_FOREACH(list_scb,
271                                      &ahd->pending_scbs, pending_links) {
272
273                                 aic_scb_timer_reset(list_scb,
274                                                     aic_get_timeout(scb));
275                         }
276
277                         ahd_print_path(ahd, scb);
278                         kprintf("no longer in timeout, status = %x\n",
279                                ccb->ccb_h.status);
280                 }
281         }
282
283         /* Don't clobber any existing error state */
284         if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
285                 ccb->ccb_h.status |= CAM_REQ_CMP;
286         } else if ((scb->flags & SCB_SENSE) != 0) {
287                 /*
288                  * We performed autosense retrieval.
289                  *
290                  * Zero any sense not transferred by the
291                  * device.  The SCSI spec mandates that any
292                  * untransfered data should be assumed to be
293                  * zero.  Complete the 'bounce' of sense information
294                  * through buffers accessible via bus-space by
295                  * copying it into the clients csio.
296                  */
297                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
298                 memcpy(&ccb->csio.sense_data,
299                        ahd_get_sense_buf(ahd, scb),
300 /* XXX What size do we want to use??? */
301                         sizeof(ccb->csio.sense_data)
302                        - ccb->csio.sense_resid);
303                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
304         } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
305                 struct scsi_status_iu_header *siu;
306                 u_int sense_len;
307
308                 /*
309                  * Copy only the sense data into the provided buffer.
310                  */
311                 siu = (struct scsi_status_iu_header *)scb->sense_data;
312                 sense_len = MIN(scsi_4btoul(siu->sense_length),
313                                 sizeof(ccb->csio.sense_data));
314                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
315                 memcpy(&ccb->csio.sense_data,
316                        ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
317                        sense_len);
318 #ifdef AHD_DEBUG
319                 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
320                         uint8_t *sense_data = (uint8_t *)&ccb->csio.sense_data;
321                         u_int i;
322
323                         kprintf("Copied %d bytes of sense data offset %d:",
324                                sense_len, SIU_SENSE_OFFSET(siu));
325                         for (i = 0; i < sense_len; i++)
326                                 kprintf(" 0x%x", *sense_data++);
327                         kprintf("\n");
328                 }
329 #endif
330                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
331         }
332         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
333         ahd_free_scb(ahd, scb);
334         xpt_done(ccb);
335 }
336
337 static void
338 ahd_action(struct cam_sim *sim, union ccb *ccb)
339 {
340         struct  ahd_softc *ahd;
341 #ifdef AHD_TARGET_MODE
342         struct  ahd_tmode_lstate *lstate;
343 #endif
344         u_int   target_id;
345         u_int   our_id;
346
347         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
348         
349         ahd = (struct ahd_softc *)cam_sim_softc(sim);
350
351         target_id = ccb->ccb_h.target_id;
352         our_id = SIM_SCSI_ID(ahd, sim);
353         
354         switch (ccb->ccb_h.func_code) {
355         /* Common cases first */
356 #ifdef AHD_TARGET_MODE
357         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
358         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
359         {
360                 struct     ahd_tmode_tstate *tstate;
361                 cam_status status;
362
363                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
364                                              &lstate, TRUE);
365
366                 if (status != CAM_REQ_CMP) {
367                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
368                                 /* Response from the black hole device */
369                                 tstate = NULL;
370                                 lstate = ahd->black_hole;
371                         } else {
372                                 ccb->ccb_h.status = status;
373                                 xpt_done(ccb);
374                                 break;
375                         }
376                 }
377                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
378
379                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
380                                           sim_links.sle);
381                         ccb->ccb_h.status = CAM_REQ_INPROG;
382                         if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
383                                 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
384                         break;
385                 }
386
387                 /*
388                  * The target_id represents the target we attempt to
389                  * select.  In target mode, this is the initiator of
390                  * the original command.
391                  */
392                 our_id = target_id;
393                 target_id = ccb->csio.init_id;
394                 /* FALLTHROUGH */
395         }
396 #endif
397         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
398         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
399         {
400                 struct  scb *scb;
401                 struct  hardware_scb *hscb;     
402                 struct  ahd_initiator_tinfo *tinfo;
403                 struct  ahd_tmode_tstate *tstate;
404                 u_int   col_idx;
405
406                 if ((ahd->flags & AHD_INITIATORROLE) == 0
407                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
408                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
409                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
410                         xpt_done(ccb);
411                         return;
412                 }
413
414                 /*
415                  * get an scb to use.
416                  */
417                 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
418                                             target_id, &tstate);
419                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
420                  || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
421                  || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
422                         col_idx = AHD_NEVER_COL_IDX;
423                 } else {
424                         col_idx = AHD_BUILD_COL_IDX(target_id,
425                                                     ccb->ccb_h.target_lun);
426                 }
427                 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
428         
429                         xpt_freeze_simq(sim, /*count*/1);
430                         ahd->flags |= AHD_RESOURCE_SHORTAGE;
431                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
432                         xpt_done(ccb);
433                         return;
434                 }
435                 
436                 hscb = scb->hscb;
437                 
438                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
439                           ("start scb(%p)\n", scb));
440                 scb->io_ctx = ccb;
441                 /*
442                  * So we can find the SCB when an abort is requested
443                  */
444                 ccb->ccb_h.ccb_scb_ptr = scb;
445
446                 /*
447                  * Put all the arguments for the xfer in the scb
448                  */
449                 hscb->control = 0;
450                 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
451                 hscb->lun = ccb->ccb_h.target_lun;
452                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
453                         hscb->cdb_len = 0;
454                         scb->flags |= SCB_DEVICE_RESET;
455                         hscb->control |= MK_MESSAGE;
456                         hscb->task_management = SIU_TASKMGMT_LUN_RESET;
457                         ahd_execute_scb(scb, NULL, 0, 0);
458                 } else {
459 #ifdef AHD_TARGET_MODE
460                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
461                                 struct target_data *tdata;
462
463                                 tdata = &hscb->shared_data.tdata;
464                                 if (ahd->pending_device == lstate)
465                                         scb->flags |= SCB_TARGET_IMMEDIATE;
466                                 hscb->control |= TARGET_SCB;
467                                 tdata->target_phases = 0;
468                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
469                                         tdata->target_phases |= SPHASE_PENDING;
470                                         tdata->scsi_status =
471                                             ccb->csio.scsi_status;
472                                 }
473                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
474                                         tdata->target_phases |= NO_DISCONNECT;
475
476                                 tdata->initiator_tag =
477                                     ahd_htole16(ccb->csio.tag_id);
478                         }
479 #endif
480                         hscb->task_management = 0;
481                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
482                                 hscb->control |= ccb->csio.tag_action;
483                         
484                         ahd_setup_data(ahd, sim, &ccb->csio, scb);
485                 }
486                 break;
487         }
488 #ifdef AHD_TARGET_MODE
489         case XPT_NOTIFY_ACK:
490         case XPT_IMMED_NOTIFY:
491         {
492                 struct     ahd_tmode_tstate *tstate;
493                 struct     ahd_tmode_lstate *lstate;
494                 cam_status status;
495
496                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
497                                              &lstate, TRUE);
498
499                 if (status != CAM_REQ_CMP) {
500                         ccb->ccb_h.status = status;
501                         xpt_done(ccb);
502                         break;
503                 }
504                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
505                                   sim_links.sle);
506                 ccb->ccb_h.status = CAM_REQ_INPROG;
507                 ahd_send_lstate_events(ahd, lstate);
508                 break;
509         }
510         case XPT_EN_LUN:                /* Enable LUN as a target */
511                 ahd_handle_en_lun(ahd, sim, ccb);
512                 xpt_done(ccb);
513                 break;
514 #endif
515         case XPT_ABORT:                 /* Abort the specified CCB */
516         {
517                 ahd_abort_ccb(ahd, sim, ccb);
518                 break;
519         }
520         case XPT_SET_TRAN_SETTINGS:
521         {
522                 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
523                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
524                 xpt_done(ccb);
525                 break;
526         }
527         case XPT_GET_TRAN_SETTINGS:
528         /* Get default/user set transfer settings for the target */
529         {
530                 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
531                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
532                 xpt_done(ccb);
533                 break;
534         }
535         case XPT_CALC_GEOMETRY:
536         {
537                 cam_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
538                 xpt_done(ccb);
539                 break;
540         }
541         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
542         {
543                 int  found;
544                 
545                 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
546                                           /*initiate reset*/TRUE);
547                 if (bootverbose) {
548                         xpt_print_path(SIM_PATH(ahd, sim));
549                         kprintf("SCSI bus reset delivered. "
550                                "%d SCBs aborted.\n", found);
551                 }
552                 /*
553                  * There seems to be some sort of race  between the
554                  * chipset and queueing new commands after a bus
555                  * reset.  Add a DELAY().  Note: tsleep() cannot be
556                  * used here because the ahd lock deadlocks the callout
557                  * thread.
558                  */
559                 DELAY(100000);
560                 ccb->ccb_h.status = CAM_REQ_CMP;
561                 xpt_done(ccb);
562                 break;
563         }
564         case XPT_TERM_IO:               /* Terminate the I/O process */
565                 /* XXX Implement */
566                 ccb->ccb_h.status = CAM_REQ_INVALID;
567                 xpt_done(ccb);
568                 break;
569         case XPT_PATH_INQ:              /* Path routing inquiry */
570         {
571                 struct ccb_pathinq *cpi = &ccb->cpi;
572                 
573                 cpi->version_num = 1; /* XXX??? */
574                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
575                 if ((ahd->features & AHD_WIDE) != 0)
576                         cpi->hba_inquiry |= PI_WIDE_16;
577                 if ((ahd->features & AHD_TARGETMODE) != 0) {
578                         cpi->target_sprt = PIT_PROCESSOR
579                                          | PIT_DISCONNECT
580                                          | PIT_TERM_IO;
581                 } else {
582                         cpi->target_sprt = 0;
583                 }
584                 cpi->hba_misc = 0;
585                 cpi->hba_eng_cnt = 0;
586                 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
587                 cpi->max_lun = AHD_NUM_LUNS_NONPKT - 1;
588                 cpi->initiator_id = ahd->our_id;
589                 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
590                         cpi->hba_misc |= PIM_NOBUSRESET;
591                 }
592                 cpi->bus_id = cam_sim_bus(sim);
593                 cpi->base_transfer_speed = 3300;
594                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
595                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
596                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
597                 cpi->unit_number = cam_sim_unit(sim);
598                 cpi->protocol = PROTO_SCSI;
599                 cpi->protocol_version = SCSI_REV_2;
600                 cpi->transport = XPORT_SPI;
601                 cpi->transport_version = 4;
602                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST
603                                                     | SID_SPI_IUS
604                                                     | SID_SPI_QAS;
605                 cpi->ccb_h.status = CAM_REQ_CMP;
606                 xpt_done(ccb);
607                 break;
608         }
609         default:
610                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
611                 xpt_done(ccb);
612                 break;
613         }
614 }
615
616
617 static void
618 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
619                       struct ccb_trans_settings *cts)
620 {
621         struct    ahd_devinfo devinfo;
622         struct    ccb_trans_settings_scsi *scsi;
623         struct    ccb_trans_settings_spi *spi;
624         struct    ahd_initiator_tinfo *tinfo;
625         struct    ahd_tmode_tstate *tstate;
626         uint16_t *discenable;
627         uint16_t *tagenable;
628         u_int     update_type;
629
630         scsi = &cts->proto_specific.scsi;
631         spi = &cts->xport_specific.spi;
632         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
633                             cts->ccb_h.target_id,
634                             cts->ccb_h.target_lun,
635                             SIM_CHANNEL(ahd, sim),
636                             ROLE_UNKNOWN);
637         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
638                                     devinfo.our_scsiid,
639                                     devinfo.target, &tstate);
640         update_type = 0;
641         if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
642                 update_type |= AHD_TRANS_GOAL;
643                 discenable = &tstate->discenable;
644                 tagenable = &tstate->tagenable;
645                 tinfo->curr.protocol_version = cts->protocol_version;
646                 tinfo->curr.transport_version = cts->transport_version;
647                 tinfo->goal.protocol_version = cts->protocol_version;
648                 tinfo->goal.transport_version = cts->transport_version;
649         } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
650                 update_type |= AHD_TRANS_USER;
651                 discenable = &ahd->user_discenable;
652                 tagenable = &ahd->user_tagenable;
653                 tinfo->user.protocol_version = cts->protocol_version;
654                 tinfo->user.transport_version = cts->transport_version;
655         } else {
656                 cts->ccb_h.status = CAM_REQ_INVALID;
657                 return;
658         }
659         
660         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
661                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
662                         *discenable |= devinfo.target_mask;
663                 else
664                         *discenable &= ~devinfo.target_mask;
665         }
666         
667         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
668                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
669                         *tagenable |= devinfo.target_mask;
670                 else
671                         *tagenable &= ~devinfo.target_mask;
672         }       
673
674         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
675                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
676                                    &spi->bus_width, ROLE_UNKNOWN);
677                 ahd_set_width(ahd, &devinfo, spi->bus_width,
678                               update_type, /*paused*/FALSE);
679         }
680
681         if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
682                 if (update_type == AHD_TRANS_USER)
683                         spi->ppr_options = tinfo->user.ppr_options;
684                 else
685                         spi->ppr_options = tinfo->goal.ppr_options;
686         }
687
688         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
689                 if (update_type == AHD_TRANS_USER)
690                         spi->sync_offset = tinfo->user.offset;
691                 else
692                         spi->sync_offset = tinfo->goal.offset;
693         }
694
695         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
696                 if (update_type == AHD_TRANS_USER)
697                         spi->sync_period = tinfo->user.period;
698                 else
699                         spi->sync_period = tinfo->goal.period;
700         }
701
702         if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
703          || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
704                 u_int   maxsync;
705
706                 maxsync = AHD_SYNCRATE_MAX;
707
708                 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
709                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
710
711                 if ((*discenable & devinfo.target_mask) == 0)
712                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
713
714                 ahd_find_syncrate(ahd, &spi->sync_period,
715                                   &spi->ppr_options, maxsync);
716                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
717                                     spi->sync_period, &spi->sync_offset,
718                                     spi->bus_width, ROLE_UNKNOWN);
719
720                 /* We use a period of 0 to represent async */
721                 if (spi->sync_offset == 0) {
722                         spi->sync_period = 0;
723                         spi->ppr_options = 0;
724                 }
725
726                 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
727                                  spi->sync_offset, spi->ppr_options,
728                                  update_type, /*paused*/FALSE);
729         }
730         cts->ccb_h.status = CAM_REQ_CMP;
731 }
732
733 static void
734 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
735                       struct ccb_trans_settings *cts)
736 {
737         struct  ahd_devinfo devinfo;
738         struct  ccb_trans_settings_scsi *scsi;
739         struct  ccb_trans_settings_spi *spi;
740         struct  ahd_initiator_tinfo *targ_info;
741         struct  ahd_tmode_tstate *tstate;
742         struct  ahd_transinfo *tinfo;
743
744         scsi = &cts->proto_specific.scsi;
745         spi = &cts->xport_specific.spi;
746         ahd_compile_devinfo(&devinfo, our_id,
747                             cts->ccb_h.target_id,
748                             cts->ccb_h.target_lun,
749                             channel, ROLE_UNKNOWN);
750         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
751                                         devinfo.our_scsiid,
752                                         devinfo.target, &tstate);
753         
754         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
755                 tinfo = &targ_info->curr;
756         else
757                 tinfo = &targ_info->user;
758         
759         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
760         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
761         if (cts->type == CTS_TYPE_USER_SETTINGS) {
762                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
763                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
764
765                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
766                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
767         } else {
768                 if ((tstate->discenable & devinfo.target_mask) != 0)
769                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
770
771                 if ((tstate->tagenable & devinfo.target_mask) != 0)
772                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
773         }
774         cts->protocol_version = tinfo->protocol_version;
775         cts->transport_version = tinfo->transport_version;
776
777         spi->sync_period = tinfo->period;
778         spi->sync_offset = tinfo->offset;
779         spi->bus_width = tinfo->width;
780         spi->ppr_options = tinfo->ppr_options;
781         
782         cts->protocol = PROTO_SCSI;
783         cts->transport = XPORT_SPI;
784         spi->valid = CTS_SPI_VALID_SYNC_RATE
785                    | CTS_SPI_VALID_SYNC_OFFSET
786                    | CTS_SPI_VALID_BUS_WIDTH
787                    | CTS_SPI_VALID_PPR_OPTIONS;
788
789         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
790                 scsi->valid = CTS_SCSI_VALID_TQ;
791                 spi->valid |= CTS_SPI_VALID_DISC;
792         } else {
793                 scsi->valid = 0;
794         }
795
796         cts->ccb_h.status = CAM_REQ_CMP;
797 }
798
799 static void
800 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
801 {
802         struct ahd_softc *ahd;
803         struct cam_sim *sim;
804
805         sim = (struct cam_sim *)callback_arg;
806         ahd = (struct ahd_softc *)cam_sim_softc(sim);
807         switch (code) {
808         case AC_LOST_DEVICE:
809         {
810                 struct  ahd_devinfo devinfo;
811
812                 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
813                                     xpt_path_target_id(path),
814                                     xpt_path_lun_id(path),
815                                     SIM_CHANNEL(ahd, sim),
816                                     ROLE_UNKNOWN);
817
818                 /*
819                  * Revert to async/narrow transfers
820                  * for the next device.
821                  */
822                 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
823                               AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
824                 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
825                                  /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
826                                  /*paused*/FALSE);
827                 break;
828         }
829         default:
830                 break;
831         }
832 }
833
834 static void
835 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
836                 int error)
837 {
838         struct  scb *scb;
839         union   ccb *ccb;
840         struct  ahd_softc *ahd;
841         struct  ahd_initiator_tinfo *tinfo;
842         struct  ahd_tmode_tstate *tstate;
843         u_int   mask;
844
845         scb = (struct scb *)arg;
846         ccb = scb->io_ctx;
847         ahd = scb->ahd_softc;
848
849         if (error != 0) {
850                 if (error == EFBIG)
851                         aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
852                 else
853                         aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
854                 if (nsegments != 0)
855                         bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
856                 ahd_free_scb(ahd, scb);
857                 xpt_done(ccb);
858                 return;
859         }
860         scb->sg_count = 0;
861         if (nsegments != 0) {
862                 void *sg;
863                 bus_dmasync_op_t op;
864                 u_int i;
865
866                 /* Copy the segments into our SG list */
867                 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
868
869                         sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
870                                           dm_segs->ds_len,
871                                           /*last*/i == 1);
872                         dm_segs++;
873                 }
874                 
875                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
876                         op = BUS_DMASYNC_PREREAD;
877                 else
878                         op = BUS_DMASYNC_PREWRITE;
879
880                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
881
882                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
883                         struct target_data *tdata;
884
885                         tdata = &scb->hscb->shared_data.tdata;
886                         tdata->target_phases |= DPHASE_PENDING;
887                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
888                                 tdata->data_phase = P_DATAOUT;
889                         else
890                                 tdata->data_phase = P_DATAIN;
891                 }
892         }
893
894         /*
895          * Last time we need to check if this SCB needs to
896          * be aborted.
897          */
898         if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
899                 if (nsegments != 0)
900                         bus_dmamap_unload(ahd->buffer_dmat,
901                                           scb->dmamap);
902                 ahd_free_scb(ahd, scb);
903                 xpt_done(ccb);
904                 return;
905         }
906
907         tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
908                                     SCSIID_OUR_ID(scb->hscb->scsiid),
909                                     SCSIID_TARGET(ahd, scb->hscb->scsiid),
910                                     &tstate);
911
912         mask = SCB_GET_TARGET_MASK(ahd, scb);
913
914         if ((tstate->discenable & mask) != 0
915          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
916                 scb->hscb->control |= DISCENB;
917
918         if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
919                 scb->flags |= SCB_PACKETIZED;
920                 if (scb->hscb->task_management != 0)
921                         scb->hscb->control &= ~MK_MESSAGE;
922         }
923
924         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
925          && (tinfo->goal.width != 0
926           || tinfo->goal.period != 0
927           || tinfo->goal.ppr_options != 0)) {
928                 scb->flags |= SCB_NEGOTIATE;
929                 scb->hscb->control |= MK_MESSAGE;
930         } else if ((tstate->auto_negotiate & mask) != 0) {
931                 scb->flags |= SCB_AUTO_NEGOTIATE;
932                 scb->hscb->control |= MK_MESSAGE;
933         }
934
935         LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
936
937         ccb->ccb_h.status |= CAM_SIM_QUEUED;
938
939         aic_scb_timer_start(scb);
940
941         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
942                 /* Define a mapping from our tag to the SCB. */
943                 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
944                 ahd_pause(ahd);
945                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
946                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
947                 ahd_unpause(ahd);
948         } else {
949                 ahd_queue_scb(ahd, scb);
950         }
951 }
952
953 static void
954 ahd_poll(struct cam_sim *sim)
955 {
956         ahd_intr(cam_sim_softc(sim));
957 }
958
959 static void
960 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
961                struct ccb_scsiio *csio, struct scb *scb)
962 {
963         struct hardware_scb *hscb;
964         struct ccb_hdr *ccb_h;
965         
966         hscb = scb->hscb;
967         ccb_h = &csio->ccb_h;
968         
969         csio->resid = 0;
970         csio->sense_resid = 0;
971         if (ccb_h->func_code == XPT_SCSI_IO) {
972                 hscb->cdb_len = csio->cdb_len;
973                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
974
975                         if (hscb->cdb_len > MAX_CDB_LEN
976                          && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
977
978                                 /*
979                                  * Should CAM start to support CDB sizes
980                                  * greater than 16 bytes, we could use
981                                  * the sense buffer to store the CDB.
982                                  */
983                                 aic_set_transaction_status(scb,
984                                                            CAM_REQ_INVALID);
985                                 ahd_free_scb(ahd, scb);
986                                 xpt_done((union ccb *)csio);
987                                 return;
988                         }
989                         if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
990                                 hscb->shared_data.idata.cdb_from_host.cdbptr =
991                                    aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
992                                 hscb->shared_data.idata.cdb_from_host.cdblen =
993                                    csio->cdb_len;
994                                 hscb->cdb_len |= SCB_CDB_LEN_PTR;
995                         } else {
996                                 memcpy(hscb->shared_data.idata.cdb, 
997                                        csio->cdb_io.cdb_ptr,
998                                        hscb->cdb_len);
999                         }
1000                 } else {
1001                         if (hscb->cdb_len > MAX_CDB_LEN) {
1002
1003                                 aic_set_transaction_status(scb,
1004                                                            CAM_REQ_INVALID);
1005                                 ahd_free_scb(ahd, scb);
1006                                 xpt_done((union ccb *)csio);
1007                                 return;
1008                         }
1009                         memcpy(hscb->shared_data.idata.cdb,
1010                                csio->cdb_io.cdb_bytes, hscb->cdb_len);
1011                 }
1012         }
1013                 
1014         /* Only use S/G if there is a transfer */
1015         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1016                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1017                         /* We've been given a pointer to a single buffer */
1018                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1019                                 int error;
1020
1021                                 crit_enter();
1022                                 error = bus_dmamap_load(ahd->buffer_dmat,
1023                                                         scb->dmamap,
1024                                                         csio->data_ptr,
1025                                                         csio->dxfer_len,
1026                                                         ahd_execute_scb,
1027                                                         scb, /*flags*/0);
1028                                 if (error == EINPROGRESS) {
1029                                         /*
1030                                          * So as to maintain ordering,
1031                                          * freeze the controller queue
1032                                          * until our mapping is
1033                                          * returned.
1034                                          */
1035                                         xpt_freeze_simq(sim,
1036                                                         /*count*/1);
1037                                         scb->io_ctx->ccb_h.status |=
1038                                             CAM_RELEASE_SIMQ;
1039                                 }
1040                                 crit_exit();
1041                         } else {
1042                                 struct bus_dma_segment seg;
1043
1044                                 /* Pointer to physical buffer */
1045                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1046                                         panic("ahd_setup_data - Transfer size "
1047                                               "larger than can device max");
1048
1049                                 seg.ds_addr =
1050                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1051                                 seg.ds_len = csio->dxfer_len;
1052                                 ahd_execute_scb(scb, &seg, 1, 0);
1053                         }
1054                 } else {
1055                         struct bus_dma_segment *segs;
1056
1057                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1058                                 panic("ahd_setup_data - Physical segment "
1059                                       "pointers unsupported");
1060
1061                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1062                                 panic("ahd_setup_data - Virtual segment "
1063                                       "addresses unsupported");
1064
1065                         /* Just use the segments provided */
1066                         segs = (struct bus_dma_segment *)csio->data_ptr;
1067                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1068                 }
1069         } else {
1070                 ahd_execute_scb(scb, NULL, 0, 0);
1071         }
1072 }
1073
1074 static void
1075 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1076 {
1077         union ccb *abort_ccb;
1078
1079         abort_ccb = ccb->cab.abort_ccb;
1080         switch (abort_ccb->ccb_h.func_code) {
1081 #ifdef AHD_TARGET_MODE
1082         case XPT_ACCEPT_TARGET_IO:
1083         case XPT_IMMED_NOTIFY:
1084         case XPT_CONT_TARGET_IO:
1085         {
1086                 struct ahd_tmode_tstate *tstate;
1087                 struct ahd_tmode_lstate *lstate;
1088                 struct ccb_hdr_slist *list;
1089                 cam_status status;
1090
1091                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1092                                              &lstate, TRUE);
1093
1094                 if (status != CAM_REQ_CMP) {
1095                         ccb->ccb_h.status = status;
1096                         break;
1097                 }
1098
1099                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1100                         list = &lstate->accept_tios;
1101                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1102                         list = &lstate->immed_notifies;
1103                 else
1104                         list = NULL;
1105
1106                 if (list != NULL) {
1107                         struct ccb_hdr *curelm;
1108                         int found;
1109
1110                         curelm = SLIST_FIRST(list);
1111                         found = 0;
1112                         if (curelm == &abort_ccb->ccb_h) {
1113                                 found = 1;
1114                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1115                         } else {
1116                                 while(curelm != NULL) {
1117                                         struct ccb_hdr *nextelm;
1118
1119                                         nextelm =
1120                                             SLIST_NEXT(curelm, sim_links.sle);
1121
1122                                         if (nextelm == &abort_ccb->ccb_h) {
1123                                                 found = 1;
1124                                                 SLIST_NEXT(curelm,
1125                                                            sim_links.sle) =
1126                                                     SLIST_NEXT(nextelm,
1127                                                                sim_links.sle);
1128                                                 break;
1129                                         }
1130                                         curelm = nextelm;
1131                                 }
1132                         }
1133
1134                         if (found) {
1135                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1136                                 xpt_done(abort_ccb);
1137                                 ccb->ccb_h.status = CAM_REQ_CMP;
1138                         } else {
1139                                 xpt_print_path(abort_ccb->ccb_h.path);
1140                                 kprintf("Not found\n");
1141                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1142                         }
1143                         break;
1144                 }
1145                 /* FALLTHROUGH */
1146         }
1147 #endif
1148         case XPT_SCSI_IO:
1149                 /* XXX Fully implement the hard ones */
1150                 ccb->ccb_h.status = CAM_UA_ABORT;
1151                 break;
1152         default:
1153                 ccb->ccb_h.status = CAM_REQ_INVALID;
1154                 break;
1155         }
1156         xpt_done(ccb);
1157 }
1158
1159 void
1160 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1161                 u_int lun, ac_code code, void *opt_arg)
1162 {
1163         struct  ccb_trans_settings cts;
1164         struct cam_path *path;
1165         void *arg;
1166         int error;
1167
1168         arg = NULL;
1169         error = ahd_create_path(ahd, channel, target, lun, &path);
1170
1171         if (error != CAM_REQ_CMP)
1172                 return;
1173
1174         switch (code) {
1175         case AC_TRANSFER_NEG:
1176         {
1177                 struct  ccb_trans_settings_scsi *scsi;
1178         
1179                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1180                 scsi = &cts.proto_specific.scsi;
1181                 cts.ccb_h.path = path;
1182                 cts.ccb_h.target_id = target;
1183                 cts.ccb_h.target_lun = lun;
1184                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1185                 arg = &cts;
1186                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1187                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1188                 if (opt_arg == NULL)
1189                         break;
1190                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1191                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1192                 scsi->valid |= CTS_SCSI_VALID_TQ;
1193                 break;
1194         }
1195         case AC_SENT_BDR:
1196         case AC_BUS_RESET:
1197                 break;
1198         default:
1199                 panic("ahd_send_async: Unexpected async event");
1200         }
1201         xpt_async(code, path, arg);
1202         xpt_free_path(path);
1203 }
1204
1205 void
1206 ahd_platform_set_tags(struct ahd_softc *ahd,
1207                       struct ahd_devinfo *devinfo, int enable)
1208 {
1209 }
1210
1211 int
1212 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1213 {
1214         ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1215                                     M_INTWAIT | M_ZERO);
1216         return (0);
1217 }
1218
1219 void
1220 ahd_platform_free(struct ahd_softc *ahd)
1221 {
1222         struct ahd_platform_data *pdata;
1223
1224         pdata = ahd->platform_data;
1225         if (pdata != NULL) {
1226                 if (pdata->regs[0] != NULL)
1227                         bus_release_resource(ahd->dev_softc,
1228                                              pdata->regs_res_type[0],
1229                                              pdata->regs_res_id[0],
1230                                              pdata->regs[0]);
1231
1232                 if (pdata->regs[1] != NULL)
1233                         bus_release_resource(ahd->dev_softc,
1234                                              pdata->regs_res_type[1],
1235                                              pdata->regs_res_id[1],
1236                                              pdata->regs[1]);
1237
1238                 if (pdata->irq != NULL)
1239                         bus_release_resource(ahd->dev_softc,
1240                                              pdata->irq_res_type,
1241                                              0, pdata->irq);
1242
1243                 if (pdata->sim != NULL) {
1244                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1245                         xpt_free_path(pdata->path);
1246                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1247                         cam_sim_free(pdata->sim);
1248                 }
1249                 if (pdata->eh != NULL)
1250                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1251                 kfree(ahd->platform_data, M_DEVBUF);
1252         }
1253 }
1254
1255 int
1256 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1257 {
1258         /* We don't sort softcs under FreeBSD so report equal always */
1259         return (0);
1260 }
1261
1262 int
1263 ahd_detach(device_t dev)
1264 {
1265         struct ahd_softc *ahd;
1266
1267         device_printf(dev, "detaching device\n");
1268         ahd = device_get_softc(dev);
1269         ahd_lock(ahd);
1270         TAILQ_REMOVE(&ahd_tailq, ahd, links);
1271         ahd_intr_enable(ahd, FALSE);
1272         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1273         ahd_unlock(ahd);
1274         ahd_free(ahd);
1275         return (0);
1276 }
1277
1278 #if 0
1279 static void
1280 ahd_dump_targcmd(struct target_cmd *cmd)
1281 {
1282         uint8_t *byte;
1283         uint8_t *last_byte;
1284         int i;
1285
1286         byte = &cmd->initiator_channel;
1287         /* Debugging info for received commands */
1288         last_byte = &cmd[1].initiator_channel;
1289
1290         i = 0;
1291         while (byte < last_byte) {
1292                 if (i == 0)
1293                         kprintf("\t");
1294                 kprintf("%#x", *byte++);
1295                 i++;
1296                 if (i == 8) {
1297                         kprintf("\n");
1298                         i = 0;
1299                 } else {
1300                         kprintf(", ");
1301                 }
1302         }
1303 }
1304 #endif
1305
1306 static int
1307 ahd_modevent(module_t mod, int type, void *data)
1308 {
1309         /* XXX Deal with busy status on unload. */
1310         /* XXX Deal with unknown events */
1311         return 0;
1312 }
1313   
1314 static moduledata_t ahd_mod = {
1315         "ahd",
1316         ahd_modevent,
1317         NULL
1318 };
1319
1320 /********************************** DDB Hooks *********************************/
1321 #ifdef DDB
1322 static struct ahd_softc *ahd_ddb_softc;
1323 static int ahd_ddb_paused;
1324 static int ahd_ddb_paused_on_entry;
1325 DB_COMMAND(ahd_sunit, ahd_ddb_sunit)
1326 {
1327         struct ahd_softc *list_ahd;
1328
1329         ahd_ddb_softc = NULL;
1330         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1331                 if (list_ahd->unit == addr)
1332                         ahd_ddb_softc = list_ahd;
1333         }
1334         if (ahd_ddb_softc == NULL)
1335                 db_error("No matching softc found!\n");
1336 }
1337
1338 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1339 {
1340         if (ahd_ddb_softc == NULL) {
1341                 db_error("Must set unit with ahd_sunit first!\n");
1342                 return;
1343         }
1344         if (ahd_ddb_paused == 0) {
1345                 ahd_ddb_paused++;
1346                 if (ahd_is_paused(ahd_ddb_softc)) {
1347                         ahd_ddb_paused_on_entry++;
1348                         return;
1349                 }
1350                 ahd_pause(ahd_ddb_softc);
1351         }
1352 }
1353
1354 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1355 {
1356         if (ahd_ddb_softc == NULL) {
1357                 db_error("Must set unit with ahd_sunit first!\n");
1358                 return;
1359         }
1360         if (ahd_ddb_paused != 0) {
1361                 ahd_ddb_paused = 0;
1362                 if (ahd_ddb_paused_on_entry)
1363                         return;
1364                 ahd_unpause(ahd_ddb_softc);
1365         } else if (ahd_ddb_paused_on_entry != 0) {
1366                 /* Two unpauses to clear a paused on entry. */
1367                 ahd_ddb_paused_on_entry = 0;
1368                 ahd_unpause(ahd_ddb_softc);
1369         }
1370 }
1371
1372 DB_COMMAND(ahd_in, ahd_ddb_in)
1373 {
1374         int c;
1375         int size;
1376  
1377         if (ahd_ddb_softc == NULL) {
1378                 db_error("Must set unit with ahd_sunit first!\n");
1379                 return;
1380         }
1381         if (have_addr == 0)
1382                 return;
1383
1384         size = 1;
1385         while ((c = *modif++) != '\0') {
1386                 switch (c) {
1387                 case 'b':
1388                         size = 1;
1389                         break;
1390                 case 'w':
1391                         size = 2;
1392                         break;
1393                 case 'l':
1394                         size = 4;
1395                 break;
1396                 }
1397         }
1398
1399         if (count <= 0)
1400                 count = 1;
1401         while (--count >= 0) {
1402                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1403                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1404                 switch (size) {
1405                 case 1:
1406                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1407                         break;
1408                 case 2:
1409                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1410                         break;
1411                 case 4:
1412                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1413                         break;
1414                 }
1415         }
1416 }
1417
1418 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1419 {
1420         db_expr_t old_value;
1421         db_expr_t new_value;
1422         int       size;
1423  
1424         if (ahd_ddb_softc == NULL) {
1425                 db_error("Must set unit with ahd_sunit first!\n");
1426                 return;
1427         }
1428
1429         switch (modif[0]) {
1430         case '\0':
1431         case 'b':
1432                 size = 1;
1433                 break;
1434         case 'h':
1435                 size = 2;
1436                 break;
1437         case 'l':
1438                 size = 4;
1439                 break;
1440         default:
1441                 db_error("Unknown size\n");
1442                 return;
1443         }
1444  
1445         while (db_expression(&new_value)) {
1446                 switch (size) {
1447                 default:
1448                 case 1:
1449                         old_value = ahd_inb(ahd_ddb_softc, addr);
1450                         ahd_outb(ahd_ddb_softc, addr, new_value);
1451                         break;
1452                 case 2:
1453                         old_value = ahd_inw(ahd_ddb_softc, addr);
1454                         ahd_outw(ahd_ddb_softc, addr, new_value);
1455                         break;
1456                 case 4:
1457                         old_value = ahd_inl(ahd_ddb_softc, addr);
1458                         ahd_outl(ahd_ddb_softc, addr, new_value);
1459                         break;
1460                 }
1461                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1462                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1463                           (u_long)old_value, (u_long)new_value);
1464                 addr += size;
1465         }
1466         db_skip_to_eol();
1467 }
1468
1469 DB_COMMAND(ahd_dump, ahd_ddb_dump)
1470 {
1471         if (ahd_ddb_softc == NULL) {
1472                 db_error("Must set unit with ahd_sunit first!\n");
1473                 return;
1474         }
1475         ahd_dump_card_state(ahd_ddb_softc);
1476 }
1477
1478 #endif
1479
1480
1481 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1482 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1483 MODULE_VERSION(ahd, 1);