e627301a2c2c720123ea215847db8c873cdb3ce9
[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.21 2005/02/16 18:09:41 gibbs Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic79xx_osm.c,v 1.18 2007/07/06 06:26:59 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 UNUSED
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                                0, 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();
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                             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         if (count != 0) {
167                 /* We have to wait until after any system dumps... */
168                 ahd->platform_data->eh =
169                     EVENTHANDLER_REGISTER(shutdown_post_sync, ahd_shutdown,
170                                           ahd, SHUTDOWN_PRI_DEFAULT);
171                 ahd_intr_enable(ahd, TRUE);
172         }
173
174         ahd_unlock();
175
176         return (count);
177 }
178
179 /*
180  * Catch an interrupt from the adapter
181  */
182 void
183 ahd_platform_intr(void *arg)
184 {
185         struct  ahd_softc *ahd;
186
187         ahd = (struct ahd_softc *)arg; 
188         ahd_intr(ahd);
189 }
190
191 /*
192  * We have an scb which has been processed by the
193  * adaptor, now we look to see how the operation
194  * went.
195  */
196 void
197 ahd_done(struct ahd_softc *ahd, struct scb *scb)
198 {
199         union ccb *ccb;
200
201         CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
202                   ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
203
204         ccb = scb->io_ctx;
205         LIST_REMOVE(scb, pending_links);
206         if ((scb->flags & SCB_TIMEDOUT) != 0)
207                 LIST_REMOVE(scb, timedout_links);
208
209         callout_stop(&ccb->ccb_h.timeout_ch);
210
211         if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
212                 bus_dmasync_op_t op;
213
214                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
215                         op = BUS_DMASYNC_POSTREAD;
216                 else
217                         op = BUS_DMASYNC_POSTWRITE;
218                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
219                 bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
220         }
221
222 #ifdef AHD_TARGET_MODE
223         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
224                 struct cam_path *ccb_path;
225
226                 /*
227                  * If we have finally disconnected, clean up our
228                  * pending device state.
229                  * XXX - There may be error states that cause where
230                  *       we will remain connected.
231                  */
232                 ccb_path = ccb->ccb_h.path;
233                 if (ahd->pending_device != NULL
234                  && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
235
236                         if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
237                                 ahd->pending_device = NULL;
238                         } else {
239                                 xpt_print_path(ccb->ccb_h.path);
240                                 kprintf("Still disconnected\n");
241                                 ahd_freeze_ccb(ccb);
242                         }
243                 }
244
245                 if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
246                         ccb->ccb_h.status |= CAM_REQ_CMP;
247                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
248                 ahd_free_scb(ahd, scb);
249                 xpt_done(ccb);
250                 return;
251         }
252 #endif
253
254         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
255                 struct  scb *list_scb;
256
257                 ahd->scb_data.recovery_scbs--;
258
259                 if (aic_get_transaction_status(scb) == CAM_BDR_SENT
260                  || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
261                         aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
262
263                 if (ahd->scb_data.recovery_scbs == 0) {
264                         /*
265                          * All recovery actions have completed successfully,
266                          * so reinstate the timeouts for all other pending
267                          * commands.
268                          */
269                         LIST_FOREACH(list_scb,
270                                      &ahd->pending_scbs, pending_links) {
271
272                                 aic_scb_timer_reset(scb, aic_get_timeout(scb));
273                         }
274
275                         ahd_print_path(ahd, scb);
276                         kprintf("no longer in timeout, status = %x\n",
277                                ccb->ccb_h.status);
278                 }
279         }
280
281         /* Don't clobber any existing error state */
282         if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
283                 ccb->ccb_h.status |= CAM_REQ_CMP;
284         } else if ((scb->flags & SCB_SENSE) != 0) {
285                 /*
286                  * We performed autosense retrieval.
287                  *
288                  * Zero any sense not transferred by the
289                  * device.  The SCSI spec mandates that any
290                  * untransfered data should be assumed to be
291                  * zero.  Complete the 'bounce' of sense information
292                  * through buffers accessible via bus-space by
293                  * copying it into the clients csio.
294                  */
295                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
296                 memcpy(&ccb->csio.sense_data,
297                        ahd_get_sense_buf(ahd, scb),
298 /* XXX What size do we want to use??? */
299                         sizeof(ccb->csio.sense_data)
300                        - ccb->csio.sense_resid);
301                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
302         } else if ((scb->flags & SCB_PKT_SENSE) != 0) {
303                 struct scsi_status_iu_header *siu;
304                 u_int sense_len;
305                 int i;
306
307                 /*
308                  * Copy only the sense data into the provided buffer.
309                  */
310                 siu = (struct scsi_status_iu_header *)scb->sense_data;
311                 sense_len = MIN(scsi_4btoul(siu->sense_length),
312                                 sizeof(ccb->csio.sense_data));
313                 memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
314                 memcpy(&ccb->csio.sense_data,
315                        ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
316                        sense_len);
317                 kprintf("Copied %d bytes of sense data offset %d:", sense_len,
318                        SIU_SENSE_OFFSET(siu));
319                 for (i = 0; i < sense_len; i++)
320                         kprintf(" 0x%x", ((uint8_t *)&ccb->csio.sense_data)[i]);
321                 kprintf("\n");
322                 scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
323         }
324         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
325         ahd_free_scb(ahd, scb);
326         xpt_done(ccb);
327 }
328
329 static void
330 ahd_action(struct cam_sim *sim, union ccb *ccb)
331 {
332         struct  ahd_softc *ahd;
333 #ifdef AHD_TARGET_MODE
334         struct  ahd_tmode_lstate *lstate;
335 #endif
336         u_int   target_id;
337         u_int   our_id;
338
339         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
340         
341         ahd = (struct ahd_softc *)cam_sim_softc(sim);
342
343         target_id = ccb->ccb_h.target_id;
344         our_id = SIM_SCSI_ID(ahd, sim);
345         
346         switch (ccb->ccb_h.func_code) {
347         /* Common cases first */
348 #ifdef AHD_TARGET_MODE
349         case XPT_ACCEPT_TARGET_IO:      /* Accept Host Target Mode CDB */
350         case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
351         {
352                 struct     ahd_tmode_tstate *tstate;
353                 cam_status status;
354
355                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
356                                              &lstate, TRUE);
357
358                 if (status != CAM_REQ_CMP) {
359                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
360                                 /* Response from the black hole device */
361                                 tstate = NULL;
362                                 lstate = ahd->black_hole;
363                         } else {
364                                 ccb->ccb_h.status = status;
365                                 xpt_done(ccb);
366                                 break;
367                         }
368                 }
369                 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
370
371                         ahd_lock();
372                         SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
373                                           sim_links.sle);
374                         ccb->ccb_h.status = CAM_REQ_INPROG;
375                         if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
376                                 ahd_run_tqinfifo(ahd, /*paused*/FALSE);
377                         ahd_unlock();
378                         break;
379                 }
380
381                 /*
382                  * The target_id represents the target we attempt to
383                  * select.  In target mode, this is the initiator of
384                  * the original command.
385                  */
386                 our_id = target_id;
387                 target_id = ccb->csio.init_id;
388                 /* FALLTHROUGH */
389         }
390 #endif
391         case XPT_SCSI_IO:       /* Execute the requested I/O operation */
392         case XPT_RESET_DEV:     /* Bus Device Reset the specified SCSI device */
393         {
394                 struct  scb *scb;
395                 struct  hardware_scb *hscb;     
396                 struct  ahd_initiator_tinfo *tinfo;
397                 struct  ahd_tmode_tstate *tstate;
398                 u_int   col_idx;
399
400                 if ((ahd->flags & AHD_INITIATORROLE) == 0
401                  && (ccb->ccb_h.func_code == XPT_SCSI_IO
402                   || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
403                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
404                         xpt_done(ccb);
405                         return;
406                 }
407
408                 /*
409                  * get an scb to use.
410                  */
411                 ahd_lock();
412                 tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
413                                             target_id, &tstate);
414                 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
415                  || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
416                  || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
417                         col_idx = AHD_NEVER_COL_IDX;
418                 } else {
419                         col_idx = AHD_BUILD_COL_IDX(target_id,
420                                                     ccb->ccb_h.target_lun);
421                 }
422                 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
423         
424                         xpt_freeze_simq(sim, /*count*/1);
425                         ahd->flags |= AHD_RESOURCE_SHORTAGE;
426                         ahd_unlock();
427                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
428                         xpt_done(ccb);
429                         return;
430                 }
431                 ahd_unlock();
432                 
433                 hscb = scb->hscb;
434                 
435                 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
436                           ("start scb(%p)\n", scb));
437                 scb->io_ctx = ccb;
438                 /*
439                  * So we can find the SCB when an abort is requested
440                  */
441                 ccb->ccb_h.ccb_scb_ptr = scb;
442
443                 /*
444                  * Put all the arguments for the xfer in the scb
445                  */
446                 hscb->control = 0;
447                 hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
448                 hscb->lun = ccb->ccb_h.target_lun;
449                 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
450                         hscb->cdb_len = 0;
451                         scb->flags |= SCB_DEVICE_RESET;
452                         hscb->control |= MK_MESSAGE;
453                         hscb->task_management = SIU_TASKMGMT_LUN_RESET;
454                         ahd_execute_scb(scb, NULL, 0, 0);
455                 } else {
456 #ifdef AHD_TARGET_MODE
457                         if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
458                                 struct target_data *tdata;
459
460                                 tdata = &hscb->shared_data.tdata;
461                                 if (ahd->pending_device == lstate)
462                                         scb->flags |= SCB_TARGET_IMMEDIATE;
463                                 hscb->control |= TARGET_SCB;
464                                 tdata->target_phases = 0;
465                                 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
466                                         tdata->target_phases |= SPHASE_PENDING;
467                                         tdata->scsi_status =
468                                             ccb->csio.scsi_status;
469                                 }
470                                 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
471                                         tdata->target_phases |= NO_DISCONNECT;
472
473                                 tdata->initiator_tag =
474                                     ahd_htole16(ccb->csio.tag_id);
475                         }
476 #endif
477                         hscb->task_management = 0;
478                         if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
479                                 hscb->control |= ccb->csio.tag_action;
480                         
481                         ahd_setup_data(ahd, sim, &ccb->csio, scb);
482                 }
483                 break;
484         }
485 #ifdef AHD_TARGET_MODE
486         case XPT_NOTIFY_ACK:
487         case XPT_IMMED_NOTIFY:
488         {
489                 struct     ahd_tmode_tstate *tstate;
490                 struct     ahd_tmode_lstate *lstate;
491                 cam_status status;
492
493                 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
494                                              &lstate, TRUE);
495
496                 if (status != CAM_REQ_CMP) {
497                         ccb->ccb_h.status = status;
498                         xpt_done(ccb);
499                         break;
500                 }
501                 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
502                                   sim_links.sle);
503                 ccb->ccb_h.status = CAM_REQ_INPROG;
504                 ahd_send_lstate_events(ahd, lstate);
505                 break;
506         }
507         case XPT_EN_LUN:                /* Enable LUN as a target */
508                 ahd_handle_en_lun(ahd, sim, ccb);
509                 xpt_done(ccb);
510                 break;
511 #endif
512         case XPT_ABORT:                 /* Abort the specified CCB */
513         {
514                 ahd_abort_ccb(ahd, sim, ccb);
515                 break;
516         }
517         case XPT_SET_TRAN_SETTINGS:
518         {
519                 ahd_lock();
520                 ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
521                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
522                 ahd_unlock();
523                 xpt_done(ccb);
524                 break;
525         }
526         case XPT_GET_TRAN_SETTINGS:
527         /* Get default/user set transfer settings for the target */
528         {
529                 ahd_lock();
530                 ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
531                                       SIM_CHANNEL(ahd, sim), &ccb->cts);
532                 ahd_unlock();
533                 xpt_done(ccb);
534                 break;
535         }
536         case XPT_CALC_GEOMETRY:
537         {
538                 cam_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
539                 xpt_done(ccb);
540                 break;
541         }
542         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
543         {
544                 int  found;
545                 
546                 ahd_lock();
547                 found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
548                                           /*initiate reset*/TRUE);
549                 ahd_unlock();
550                 if (bootverbose) {
551                         xpt_print_path(SIM_PATH(ahd, sim));
552                         kprintf("SCSI bus reset delivered. "
553                                "%d SCBs aborted.\n", found);
554                 }
555                 ccb->ccb_h.status = CAM_REQ_CMP;
556                 xpt_done(ccb);
557                 break;
558         }
559         case XPT_TERM_IO:               /* Terminate the I/O process */
560                 /* XXX Implement */
561                 ccb->ccb_h.status = CAM_REQ_INVALID;
562                 xpt_done(ccb);
563                 break;
564         case XPT_PATH_INQ:              /* Path routing inquiry */
565         {
566                 struct ccb_pathinq *cpi = &ccb->cpi;
567                 
568                 cpi->version_num = 1; /* XXX??? */
569                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
570                 if ((ahd->features & AHD_WIDE) != 0)
571                         cpi->hba_inquiry |= PI_WIDE_16;
572                 if ((ahd->features & AHD_TARGETMODE) != 0) {
573                         cpi->target_sprt = PIT_PROCESSOR
574                                          | PIT_DISCONNECT
575                                          | PIT_TERM_IO;
576                 } else {
577                         cpi->target_sprt = 0;
578                 }
579                 cpi->hba_misc = 0;
580                 cpi->hba_eng_cnt = 0;
581                 cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
582                 cpi->max_lun = AHD_NUM_LUNS_NONPKT - 1;
583                 cpi->initiator_id = ahd->our_id;
584                 if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
585                         cpi->hba_misc |= PIM_NOBUSRESET;
586                 }
587                 cpi->bus_id = cam_sim_bus(sim);
588                 cpi->base_transfer_speed = 3300;
589                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
590                 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
591                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
592                 cpi->unit_number = cam_sim_unit(sim);
593 #ifdef AHD_NEW_TRAN_SETTINGS
594                 cpi->protocol = PROTO_SCSI;
595                 cpi->protocol_version = SCSI_REV_2;
596                 cpi->transport = XPORT_SPI;
597                 cpi->transport_version = 2;
598                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
599                 cpi->transport_version = 4;
600                 cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST;
601 #endif
602                 cpi->ccb_h.status = CAM_REQ_CMP;
603                 xpt_done(ccb);
604                 break;
605         }
606         default:
607                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
608                 xpt_done(ccb);
609                 break;
610         }
611 }
612
613
614 static void
615 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
616                       struct ccb_trans_settings *cts)
617 {
618 #ifdef AHD_NEW_TRAN_SETTINGS
619         struct    ahd_devinfo devinfo;
620         struct    ccb_trans_settings_scsi *scsi;
621         struct    ccb_trans_settings_spi *spi;
622         struct    ahd_initiator_tinfo *tinfo;
623         struct    ahd_tmode_tstate *tstate;
624         uint16_t *discenable;
625         uint16_t *tagenable;
626         u_int     update_type;
627
628         scsi = &cts->proto_specific.scsi;
629         spi = &cts->xport_specific.spi;
630         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
631                             cts->ccb_h.target_id,
632                             cts->ccb_h.target_lun,
633                             SIM_CHANNEL(ahd, sim),
634                             ROLE_UNKNOWN);
635         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
636                                     devinfo.our_scsiid,
637                                     devinfo.target, &tstate);
638         update_type = 0;
639         if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
640                 update_type |= AHD_TRANS_GOAL;
641                 discenable = &tstate->discenable;
642                 tagenable = &tstate->tagenable;
643                 tinfo->curr.protocol_version = cts->protocol_version;
644                 tinfo->curr.transport_version = cts->transport_version;
645                 tinfo->goal.protocol_version = cts->protocol_version;
646                 tinfo->goal.transport_version = cts->transport_version;
647         } else if (cts->type == CTS_TYPE_USER_SETTINGS) {
648                 update_type |= AHD_TRANS_USER;
649                 discenable = &ahd->user_discenable;
650                 tagenable = &ahd->user_tagenable;
651                 tinfo->user.protocol_version = cts->protocol_version;
652                 tinfo->user.transport_version = cts->transport_version;
653         } else {
654                 cts->ccb_h.status = CAM_REQ_INVALID;
655                 return;
656         }
657         
658         if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
659                 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
660                         *discenable |= devinfo.target_mask;
661                 else
662                         *discenable &= ~devinfo.target_mask;
663         }
664         
665         if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
666                 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
667                         *tagenable |= devinfo.target_mask;
668                 else
669                         *tagenable &= ~devinfo.target_mask;
670         }       
671
672         if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
673                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
674                                    &spi->bus_width, ROLE_UNKNOWN);
675                 ahd_set_width(ahd, &devinfo, spi->bus_width,
676                               update_type, /*paused*/FALSE);
677         }
678
679         if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
680                 if (update_type == AHD_TRANS_USER)
681                         spi->ppr_options = tinfo->user.ppr_options;
682                 else
683                         spi->ppr_options = tinfo->goal.ppr_options;
684         }
685
686         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
687                 if (update_type == AHD_TRANS_USER)
688                         spi->sync_offset = tinfo->user.offset;
689                 else
690                         spi->sync_offset = tinfo->goal.offset;
691         }
692
693         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
694                 if (update_type == AHD_TRANS_USER)
695                         spi->sync_period = tinfo->user.period;
696                 else
697                         spi->sync_period = tinfo->goal.period;
698         }
699
700         if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
701          || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
702                 u_int   maxsync;
703
704                 maxsync = AHD_SYNCRATE_MAX;
705
706                 if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
707                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
708
709                 if ((*discenable & devinfo.target_mask) == 0)
710                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
711
712                 ahd_find_syncrate(ahd, &spi->sync_period,
713                                   &spi->ppr_options, maxsync);
714                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
715                                     spi->sync_period, &spi->sync_offset,
716                                     spi->bus_width, ROLE_UNKNOWN);
717
718                 /* We use a period of 0 to represent async */
719                 if (spi->sync_offset == 0) {
720                         spi->sync_period = 0;
721                         spi->ppr_options = 0;
722                 }
723
724                 ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
725                                  spi->sync_offset, spi->ppr_options,
726                                  update_type, /*paused*/FALSE);
727         }
728         cts->ccb_h.status = CAM_REQ_CMP;
729 #else
730         struct    ahd_devinfo devinfo;
731         struct    ahd_initiator_tinfo *tinfo;
732         struct    ahd_tmode_tstate *tstate;
733         uint16_t *discenable;
734         uint16_t *tagenable;
735         u_int     update_type;
736
737         ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
738                             cts->ccb_h.target_id,
739                             cts->ccb_h.target_lun,
740                             SIM_CHANNEL(ahd, sim),
741                             ROLE_UNKNOWN);
742         tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
743                                     devinfo.our_scsiid,
744                                     devinfo.target, &tstate);
745         update_type = 0;
746         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
747                 update_type |= AHD_TRANS_GOAL;
748                 discenable = &tstate->discenable;
749                 tagenable = &tstate->tagenable;
750         } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
751                 update_type |= AHD_TRANS_USER;
752                 discenable = &ahd->user_discenable;
753                 tagenable = &ahd->user_tagenable;
754         } else {
755                 cts->ccb_h.status = CAM_REQ_INVALID;
756                 return;
757         }
758         
759         if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
760                 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0)
761                         *discenable |= devinfo.target_mask;
762                 else
763                         *discenable &= ~devinfo.target_mask;
764         }
765         
766         if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
767                 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0)
768                         *tagenable |= devinfo.target_mask;
769                 else
770                         *tagenable &= ~devinfo.target_mask;
771         }       
772
773         if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
774                 ahd_validate_width(ahd, /*tinfo limit*/NULL,
775                                    &cts->bus_width, ROLE_UNKNOWN);
776                 ahd_set_width(ahd, &devinfo, cts->bus_width,
777                               update_type, /*paused*/FALSE);
778         }
779
780         if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
781                 if (update_type == AHD_TRANS_USER)
782                         cts->sync_offset = tinfo->user.offset;
783                 else
784                         cts->sync_offset = tinfo->goal.offset;
785         }
786
787         if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
788                 if (update_type == AHD_TRANS_USER)
789                         cts->sync_period = tinfo->user.period;
790                 else
791                         cts->sync_period = tinfo->goal.period;
792         }
793
794         if (((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0)
795          || ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
796          || ((cts->valid & CCB_TRANS_TQ_VALID) != 0)
797          || ((cts->valid & CCB_TRANS_DISC_VALID) != 0)) {
798                 u_int ppr_options;
799                 u_int maxsync;
800
801                 maxsync = AHD_SYNCRATE_MAX;
802                 ppr_options = 0;
803                 if (cts->sync_period <= AHD_SYNCRATE_DT
804                  && cts->bus_width == MSG_EXT_WDTR_BUS_16_BIT) {
805                         ppr_options = tinfo->user.ppr_options
806                                     | MSG_EXT_PPR_DT_REQ;
807                 }
808
809                 if ((*tagenable & devinfo.target_mask) == 0
810                  || (*discenable & devinfo.target_mask) == 0)
811                         ppr_options &= ~MSG_EXT_PPR_IU_REQ;
812
813                 ahd_find_syncrate(ahd, &cts->sync_period,
814                                   &ppr_options, maxsync);
815                 ahd_validate_offset(ahd, /*tinfo limit*/NULL,
816                                     cts->sync_period, &cts->sync_offset,
817                                     MSG_EXT_WDTR_BUS_8_BIT,
818                                     ROLE_UNKNOWN);
819
820                 /* We use a period of 0 to represent async */
821                 if (cts->sync_offset == 0) {
822                         cts->sync_period = 0;
823                         ppr_options = 0;
824                 }
825
826                 if (ppr_options != 0
827                  && tinfo->user.transport_version >= 3) {
828                         tinfo->goal.transport_version =
829                             tinfo->user.transport_version;
830                         tinfo->curr.transport_version =
831                             tinfo->user.transport_version;
832                 }
833                 
834                 ahd_set_syncrate(ahd, &devinfo, cts->sync_period,
835                                  cts->sync_offset, ppr_options,
836                                  update_type, /*paused*/FALSE);
837         }
838         cts->ccb_h.status = CAM_REQ_CMP;
839 #endif
840 }
841
842 static void
843 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
844                       struct ccb_trans_settings *cts)
845 {
846 #ifdef AHD_NEW_TRAN_SETTINGS
847         struct  ahd_devinfo devinfo;
848         struct  ccb_trans_settings_scsi *scsi;
849         struct  ccb_trans_settings_spi *spi;
850         struct  ahd_initiator_tinfo *targ_info;
851         struct  ahd_tmode_tstate *tstate;
852         struct  ahd_transinfo *tinfo;
853
854         scsi = &cts->proto_specific.scsi;
855         spi = &cts->xport_specific.spi;
856         ahd_compile_devinfo(&devinfo, our_id,
857                             cts->ccb_h.target_id,
858                             cts->ccb_h.target_lun,
859                             channel, ROLE_UNKNOWN);
860         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
861                                         devinfo.our_scsiid,
862                                         devinfo.target, &tstate);
863         
864         if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
865                 tinfo = &targ_info->curr;
866         else
867                 tinfo = &targ_info->user;
868         
869         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
870         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
871         if (cts->type == CTS_TYPE_USER_SETTINGS) {
872                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
873                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
874
875                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
876                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
877         } else {
878                 if ((tstate->discenable & devinfo.target_mask) != 0)
879                         spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
880
881                 if ((tstate->tagenable & devinfo.target_mask) != 0)
882                         scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
883         }
884         cts->protocol_version = tinfo->protocol_version;
885         cts->transport_version = tinfo->transport_version;
886
887         spi->sync_period = tinfo->period;
888         spi->sync_offset = tinfo->offset;
889         spi->bus_width = tinfo->width;
890         spi->ppr_options = tinfo->ppr_options;
891         
892         cts->protocol = PROTO_SCSI;
893         cts->transport = XPORT_SPI;
894         spi->valid = CTS_SPI_VALID_SYNC_RATE
895                    | CTS_SPI_VALID_SYNC_OFFSET
896                    | CTS_SPI_VALID_BUS_WIDTH
897                    | CTS_SPI_VALID_PPR_OPTIONS;
898
899         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
900                 scsi->valid = CTS_SCSI_VALID_TQ;
901                 spi->valid |= CTS_SPI_VALID_DISC;
902         } else {
903                 scsi->valid = 0;
904         }
905
906         cts->ccb_h.status = CAM_REQ_CMP;
907 #else
908         struct  ahd_devinfo devinfo;
909         struct  ahd_initiator_tinfo *targ_info;
910         struct  ahd_tmode_tstate *tstate;
911         struct  ahd_transinfo *tinfo;
912
913         ahd_compile_devinfo(&devinfo, our_id,
914                             cts->ccb_h.target_id,
915                             cts->ccb_h.target_lun,
916                             channel, ROLE_UNKNOWN);
917         targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
918                                         devinfo.our_scsiid,
919                                         devinfo.target, &tstate);
920         
921         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0)
922                 tinfo = &targ_info->curr;
923         else
924                 tinfo = &targ_info->user;
925         
926         cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
927         if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) == 0) {
928                 if ((ahd->user_discenable & devinfo.target_mask) != 0)
929                         cts->flags |= CCB_TRANS_DISC_ENB;
930
931                 if ((ahd->user_tagenable & devinfo.target_mask) != 0)
932                         cts->flags |= CCB_TRANS_TAG_ENB;
933         } else {
934                 if ((tstate->discenable & devinfo.target_mask) != 0)
935                         cts->flags |= CCB_TRANS_DISC_ENB;
936
937                 if ((tstate->tagenable & devinfo.target_mask) != 0)
938                         cts->flags |= CCB_TRANS_TAG_ENB;
939         }
940         cts->sync_period = tinfo->period;
941         cts->sync_offset = tinfo->offset;
942         cts->bus_width = tinfo->width;
943         
944         cts->valid = CCB_TRANS_SYNC_RATE_VALID
945                    | CCB_TRANS_SYNC_OFFSET_VALID
946                    | CCB_TRANS_BUS_WIDTH_VALID;
947
948         if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD)
949                 cts->valid |= CCB_TRANS_DISC_VALID|CCB_TRANS_TQ_VALID;
950
951         cts->ccb_h.status = CAM_REQ_CMP;
952 #endif
953 }
954
955 static void
956 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
957 {
958         struct ahd_softc *ahd;
959         struct cam_sim *sim;
960
961         sim = (struct cam_sim *)callback_arg;
962         ahd = (struct ahd_softc *)cam_sim_softc(sim);
963         switch (code) {
964         case AC_LOST_DEVICE:
965         {
966                 struct  ahd_devinfo devinfo;
967
968                 ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
969                                     xpt_path_target_id(path),
970                                     xpt_path_lun_id(path),
971                                     SIM_CHANNEL(ahd, sim),
972                                     ROLE_UNKNOWN);
973
974                 /*
975                  * Revert to async/narrow transfers
976                  * for the next device.
977                  */
978                 ahd_lock();
979                 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
980                               AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
981                 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
982                                  /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
983                                  /*paused*/FALSE);
984                 ahd_unlock();
985                 break;
986         }
987         default:
988                 break;
989         }
990 }
991
992 static void
993 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
994                 int error)
995 {
996         struct  scb *scb;
997         union   ccb *ccb;
998         struct  ahd_softc *ahd;
999         struct  ahd_initiator_tinfo *tinfo;
1000         struct  ahd_tmode_tstate *tstate;
1001         u_int   mask;
1002
1003         scb = (struct scb *)arg;
1004         ccb = scb->io_ctx;
1005         ahd = scb->ahd_softc;
1006
1007         if (error != 0) {
1008                 if (error == EFBIG)
1009                         aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
1010                 else
1011                         aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
1012                 if (nsegments != 0)
1013                         bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
1014                 ahd_lock();
1015                 ahd_free_scb(ahd, scb);
1016                 ahd_unlock();
1017                 xpt_done(ccb);
1018                 return;
1019         }
1020         scb->sg_count = 0;
1021         if (nsegments != 0) {
1022                 void *sg;
1023                 bus_dmasync_op_t op;
1024                 u_int i;
1025
1026                 /* Copy the segments into our SG list */
1027                 for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
1028
1029                         sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
1030                                           dm_segs->ds_len,
1031                                           /*last*/i == 1);
1032                         dm_segs++;
1033                 }
1034                 
1035                 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1036                         op = BUS_DMASYNC_PREREAD;
1037                 else
1038                         op = BUS_DMASYNC_PREWRITE;
1039
1040                 bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
1041
1042                 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
1043                         struct target_data *tdata;
1044
1045                         tdata = &scb->hscb->shared_data.tdata;
1046                         tdata->target_phases |= DPHASE_PENDING;
1047                         if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1048                                 tdata->data_phase = P_DATAOUT;
1049                         else
1050                                 tdata->data_phase = P_DATAIN;
1051                 }
1052         }
1053
1054         ahd_lock();
1055
1056         /*
1057          * Last time we need to check if this SCB needs to
1058          * be aborted.
1059          */
1060         if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1061                 if (nsegments != 0)
1062                         bus_dmamap_unload(ahd->buffer_dmat,
1063                                           scb->dmamap);
1064                 ahd_free_scb(ahd, scb);
1065                 ahd_unlock();
1066                 xpt_done(ccb);
1067                 return;
1068         }
1069
1070         tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1071                                     SCSIID_OUR_ID(scb->hscb->scsiid),
1072                                     SCSIID_TARGET(ahd, scb->hscb->scsiid),
1073                                     &tstate);
1074
1075         mask = SCB_GET_TARGET_MASK(ahd, scb);
1076
1077         if ((tstate->discenable & mask) != 0
1078          && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1079                 scb->hscb->control |= DISCENB;
1080
1081         if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1082                 scb->flags |= SCB_PACKETIZED;
1083                 if (scb->hscb->task_management != 0)
1084                         scb->hscb->control &= ~MK_MESSAGE;
1085         }
1086
1087         if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1088          && (tinfo->goal.width != 0
1089           || tinfo->goal.period != 0
1090           || tinfo->goal.ppr_options != 0)) {
1091                 scb->flags |= SCB_NEGOTIATE;
1092                 scb->hscb->control |= MK_MESSAGE;
1093         } else if ((tstate->auto_negotiate & mask) != 0) {
1094                 scb->flags |= SCB_AUTO_NEGOTIATE;
1095                 scb->hscb->control |= MK_MESSAGE;
1096         }
1097
1098         LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1099
1100         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1101
1102         aic_scb_timer_start(scb);
1103
1104         if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1105                 /* Define a mapping from our tag to the SCB. */
1106                 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1107                 ahd_pause(ahd);
1108                 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1109                 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1110                 ahd_unpause(ahd);
1111         } else {
1112                 ahd_queue_scb(ahd, scb);
1113         }
1114
1115         ahd_unlock();
1116 }
1117
1118 static void
1119 ahd_poll(struct cam_sim *sim)
1120 {
1121         ahd_intr(cam_sim_softc(sim));
1122 }
1123
1124 static void
1125 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1126                struct ccb_scsiio *csio, struct scb *scb)
1127 {
1128         struct hardware_scb *hscb;
1129         struct ccb_hdr *ccb_h;
1130         
1131         hscb = scb->hscb;
1132         ccb_h = &csio->ccb_h;
1133         
1134         csio->resid = 0;
1135         csio->sense_resid = 0;
1136         if (ccb_h->func_code == XPT_SCSI_IO) {
1137                 hscb->cdb_len = csio->cdb_len;
1138                 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1139
1140                         if (hscb->cdb_len > MAX_CDB_LEN
1141                          && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1142                                 /*
1143                                  * Should CAM start to support CDB sizes
1144                                  * greater than 16 bytes, we could use
1145                                  * the sense buffer to store the CDB.
1146                                  */
1147                                 aic_set_transaction_status(scb,
1148                                                            CAM_REQ_INVALID);
1149                                 ahd_lock();
1150                                 ahd_free_scb(ahd, scb);
1151                                 ahd_unlock();
1152                                 xpt_done((union ccb *)csio);
1153                                 return;
1154                         }
1155                         if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1156                                 hscb->shared_data.idata.cdb_from_host.cdbptr =
1157                                    aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1158                                 hscb->shared_data.idata.cdb_from_host.cdblen =
1159                                    csio->cdb_len;
1160                                 hscb->cdb_len |= SCB_CDB_LEN_PTR;
1161                         } else {
1162                                 memcpy(hscb->shared_data.idata.cdb, 
1163                                        csio->cdb_io.cdb_ptr,
1164                                        hscb->cdb_len);
1165                         }
1166                 } else {
1167                         if (hscb->cdb_len > MAX_CDB_LEN) {
1168                                 aic_set_transaction_status(scb,
1169                                                            CAM_REQ_INVALID);
1170                                 ahd_lock();
1171                                 ahd_free_scb(ahd, scb);
1172                                 ahd_unlock();
1173                                 xpt_done((union ccb *)csio);
1174                                 return;
1175                         }
1176                         memcpy(hscb->shared_data.idata.cdb,
1177                                csio->cdb_io.cdb_bytes, hscb->cdb_len);
1178                 }
1179         }
1180                 
1181         /* Only use S/G if there is a transfer */
1182         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1183                 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) {
1184                         /* We've been given a pointer to a single buffer */
1185                         if ((ccb_h->flags & CAM_DATA_PHYS) == 0) {
1186                                 int error;
1187
1188                                 crit_enter();
1189                                 error = bus_dmamap_load(ahd->buffer_dmat,
1190                                                         scb->dmamap,
1191                                                         csio->data_ptr,
1192                                                         csio->dxfer_len,
1193                                                         ahd_execute_scb,
1194                                                         scb, /*flags*/0);
1195                                 if (error == EINPROGRESS) {
1196                                         /*
1197                                          * So as to maintain ordering,
1198                                          * freeze the controller queue
1199                                          * until our mapping is
1200                                          * returned.
1201                                          */
1202                                         xpt_freeze_simq(sim,
1203                                                         /*count*/1);
1204                                         scb->io_ctx->ccb_h.status |=
1205                                             CAM_RELEASE_SIMQ;
1206                                 }
1207                                 crit_exit();
1208                         } else {
1209                                 struct bus_dma_segment seg;
1210
1211                                 /* Pointer to physical buffer */
1212                                 if (csio->dxfer_len > AHD_MAXTRANSFER_SIZE)
1213                                         panic("ahd_setup_data - Transfer size "
1214                                               "larger than can device max");
1215
1216                                 seg.ds_addr =
1217                                     (bus_addr_t)(vm_offset_t)csio->data_ptr;
1218                                 seg.ds_len = csio->dxfer_len;
1219                                 ahd_execute_scb(scb, &seg, 1, 0);
1220                         }
1221                 } else {
1222                         struct bus_dma_segment *segs;
1223
1224                         if ((ccb_h->flags & CAM_DATA_PHYS) != 0)
1225                                 panic("ahd_setup_data - Physical segment "
1226                                       "pointers unsupported");
1227
1228                         if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0)
1229                                 panic("ahd_setup_data - Virtual segment "
1230                                       "addresses unsupported");
1231
1232                         /* Just use the segments provided */
1233                         segs = (struct bus_dma_segment *)csio->data_ptr;
1234                         ahd_execute_scb(scb, segs, csio->sglist_cnt, 0);
1235                 }
1236         } else {
1237                 ahd_execute_scb(scb, NULL, 0, 0);
1238         }
1239 }
1240
1241 static void
1242 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1243 {
1244         union ccb *abort_ccb;
1245
1246         abort_ccb = ccb->cab.abort_ccb;
1247         switch (abort_ccb->ccb_h.func_code) {
1248 #ifdef AHD_TARGET_MODE
1249         case XPT_ACCEPT_TARGET_IO:
1250         case XPT_IMMED_NOTIFY:
1251         case XPT_CONT_TARGET_IO:
1252         {
1253                 struct ahd_tmode_tstate *tstate;
1254                 struct ahd_tmode_lstate *lstate;
1255                 struct ccb_hdr_slist *list;
1256                 cam_status status;
1257
1258                 status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1259                                              &lstate, TRUE);
1260
1261                 if (status != CAM_REQ_CMP) {
1262                         ccb->ccb_h.status = status;
1263                         break;
1264                 }
1265
1266                 if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1267                         list = &lstate->accept_tios;
1268                 else if (abort_ccb->ccb_h.func_code == XPT_IMMED_NOTIFY)
1269                         list = &lstate->immed_notifies;
1270                 else
1271                         list = NULL;
1272
1273                 if (list != NULL) {
1274                         struct ccb_hdr *curelm;
1275                         int found;
1276
1277                         curelm = SLIST_FIRST(list);
1278                         found = 0;
1279                         if (curelm == &abort_ccb->ccb_h) {
1280                                 found = 1;
1281                                 SLIST_REMOVE_HEAD(list, sim_links.sle);
1282                         } else {
1283                                 while(curelm != NULL) {
1284                                         struct ccb_hdr *nextelm;
1285
1286                                         nextelm =
1287                                             SLIST_NEXT(curelm, sim_links.sle);
1288
1289                                         if (nextelm == &abort_ccb->ccb_h) {
1290                                                 found = 1;
1291                                                 SLIST_NEXT(curelm,
1292                                                            sim_links.sle) =
1293                                                     SLIST_NEXT(nextelm,
1294                                                                sim_links.sle);
1295                                                 break;
1296                                         }
1297                                         curelm = nextelm;
1298                                 }
1299                         }
1300
1301                         if (found) {
1302                                 abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1303                                 xpt_done(abort_ccb);
1304                                 ccb->ccb_h.status = CAM_REQ_CMP;
1305                         } else {
1306                                 xpt_print_path(abort_ccb->ccb_h.path);
1307                                 kprintf("Not found\n");
1308                                 ccb->ccb_h.status = CAM_PATH_INVALID;
1309                         }
1310                         break;
1311                 }
1312                 /* FALLTHROUGH */
1313         }
1314 #endif
1315         case XPT_SCSI_IO:
1316                 /* XXX Fully implement the hard ones */
1317                 ccb->ccb_h.status = CAM_UA_ABORT;
1318                 break;
1319         default:
1320                 ccb->ccb_h.status = CAM_REQ_INVALID;
1321                 break;
1322         }
1323         xpt_done(ccb);
1324 }
1325
1326 void
1327 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1328                 u_int lun, ac_code code, void *opt_arg)
1329 {
1330         struct  ccb_trans_settings cts;
1331         struct cam_path *path;
1332         void *arg;
1333         int error;
1334
1335         arg = NULL;
1336         error = ahd_create_path(ahd, channel, target, lun, &path);
1337
1338         if (error != CAM_REQ_CMP)
1339                 return;
1340
1341         switch (code) {
1342         case AC_TRANSFER_NEG:
1343         {
1344 #ifdef AHD_NEW_TRAN_SETTINGS
1345                 struct  ccb_trans_settings_scsi *scsi;
1346         
1347                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1348                 scsi = &cts.proto_specific.scsi;
1349 #else
1350                 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1351 #endif
1352                 cts.ccb_h.path = path;
1353                 cts.ccb_h.target_id = target;
1354                 cts.ccb_h.target_lun = lun;
1355                 ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1356                 arg = &cts;
1357 #ifdef AHD_NEW_TRAN_SETTINGS
1358                 scsi->valid &= ~CTS_SCSI_VALID_TQ;
1359                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1360 #else
1361                 cts.valid &= ~CCB_TRANS_TQ_VALID;
1362                 cts.flags &= ~CCB_TRANS_TAG_ENB;
1363 #endif
1364                 if (opt_arg == NULL)
1365                         break;
1366                 if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1367 #ifdef AHD_NEW_TRAN_SETTINGS
1368                         scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1369                 scsi->valid |= CTS_SCSI_VALID_TQ;
1370 #else
1371                         cts.flags |= CCB_TRANS_TAG_ENB;
1372                 cts.valid |= CCB_TRANS_TQ_VALID;
1373 #endif
1374                 break;
1375         }
1376         case AC_SENT_BDR:
1377         case AC_BUS_RESET:
1378                 break;
1379         default:
1380                 panic("ahd_send_async: Unexpected async event");
1381         }
1382         xpt_async(code, path, arg);
1383         xpt_free_path(path);
1384 }
1385
1386 void
1387 ahd_platform_set_tags(struct ahd_softc *ahd,
1388                       struct ahd_devinfo *devinfo, int enable)
1389 {
1390 }
1391
1392 int
1393 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1394 {
1395         ahd->platform_data = kmalloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1396                                     M_INTWAIT | M_ZERO);
1397         return (0);
1398 }
1399
1400 void
1401 ahd_platform_free(struct ahd_softc *ahd)
1402 {
1403         struct ahd_platform_data *pdata;
1404
1405         pdata = ahd->platform_data;
1406         if (pdata != NULL) {
1407                 if (pdata->regs[0] != NULL)
1408                         bus_release_resource(ahd->dev_softc,
1409                                              pdata->regs_res_type[0],
1410                                              pdata->regs_res_id[0],
1411                                              pdata->regs[0]);
1412
1413                 if (pdata->regs[1] != NULL)
1414                         bus_release_resource(ahd->dev_softc,
1415                                              pdata->regs_res_type[1],
1416                                              pdata->regs_res_id[1],
1417                                              pdata->regs[1]);
1418
1419                 if (pdata->irq != NULL)
1420                         bus_release_resource(ahd->dev_softc,
1421                                              pdata->irq_res_type,
1422                                              0, pdata->irq);
1423
1424                 if (pdata->sim != NULL) {
1425                         xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1426                         xpt_free_path(pdata->path);
1427                         xpt_bus_deregister(cam_sim_path(pdata->sim));
1428                         cam_sim_free(pdata->sim);
1429                 }
1430                 if (pdata->eh != NULL)
1431                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, pdata->eh);
1432                 kfree(ahd->platform_data, M_DEVBUF);
1433         }
1434 }
1435
1436 int
1437 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1438 {
1439         /* We don't sort softcs under FreeBSD so report equal always */
1440         return (0);
1441 }
1442
1443 int
1444 ahd_detach(device_t dev)
1445 {
1446         struct ahd_softc *ahd;
1447
1448         device_printf(dev, "detaching device\n");
1449         ahd = device_get_softc(dev);
1450         ahd = ahd_find_softc(ahd);
1451         if (ahd == NULL) {
1452                 device_printf(dev, "aic7xxx already detached\n");
1453                 return (ENOENT);
1454         }
1455         TAILQ_REMOVE(&ahd_tailq, ahd, links);
1456         ahd_lock();
1457         ahd_intr_enable(ahd, FALSE);
1458         bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1459         ahd_unlock();
1460         ahd_free(ahd);
1461         return (0);
1462 }
1463
1464 #if UNUSED
1465 static void
1466 ahd_dump_targcmd(struct target_cmd *cmd)
1467 {
1468         uint8_t *byte;
1469         uint8_t *last_byte;
1470         int i;
1471
1472         byte = &cmd->initiator_channel;
1473         /* Debugging info for received commands */
1474         last_byte = &cmd[1].initiator_channel;
1475
1476         i = 0;
1477         while (byte < last_byte) {
1478                 if (i == 0)
1479                         kprintf("\t");
1480                 kprintf("%#x", *byte++);
1481                 i++;
1482                 if (i == 8) {
1483                         kprintf("\n");
1484                         i = 0;
1485                 } else {
1486                         kprintf(", ");
1487                 }
1488         }
1489 }
1490 #endif
1491
1492 static int
1493 ahd_modevent(module_t mod, int type, void *data)
1494 {
1495         /* XXX Deal with busy status on unload. */
1496         return 0;
1497 }
1498   
1499 static moduledata_t ahd_mod = {
1500         "ahd",
1501         ahd_modevent,
1502         NULL
1503 };
1504
1505 /********************************** DDB Hooks *********************************/
1506 #ifdef DDB
1507 static struct ahd_softc *ahd_ddb_softc;
1508 static int ahd_ddb_paused;
1509 static int ahd_ddb_paused_on_entry;
1510 DB_COMMAND(ahd_sunit, ahd_ddb_sunit)
1511 {
1512         struct ahd_softc *list_ahd;
1513
1514         ahd_ddb_softc = NULL;
1515         TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1516                 if (list_ahd->unit == addr)
1517                         ahd_ddb_softc = list_ahd;
1518         }
1519         if (ahd_ddb_softc == NULL)
1520                 db_error("No matching softc found!\n");
1521 }
1522
1523 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1524 {
1525         if (ahd_ddb_softc == NULL) {
1526                 db_error("Must set unit with ahd_sunit first!\n");
1527                 return;
1528         }
1529         if (ahd_ddb_paused == 0) {
1530                 ahd_ddb_paused++;
1531                 if (ahd_is_paused(ahd_ddb_softc)) {
1532                         ahd_ddb_paused_on_entry++;
1533                         return;
1534                 }
1535                 ahd_pause(ahd_ddb_softc);
1536         }
1537 }
1538
1539 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1540 {
1541         if (ahd_ddb_softc == NULL) {
1542                 db_error("Must set unit with ahd_sunit first!\n");
1543                 return;
1544         }
1545         if (ahd_ddb_paused != 0) {
1546                 ahd_ddb_paused = 0;
1547                 if (ahd_ddb_paused_on_entry)
1548                         return;
1549                 ahd_unpause(ahd_ddb_softc);
1550         } else if (ahd_ddb_paused_on_entry != 0) {
1551                 /* Two unpauses to clear a paused on entry. */
1552                 ahd_ddb_paused_on_entry = 0;
1553                 ahd_unpause(ahd_ddb_softc);
1554         }
1555 }
1556
1557 DB_COMMAND(ahd_in, ahd_ddb_in)
1558 {
1559         int c;
1560         int size;
1561  
1562         if (ahd_ddb_softc == NULL) {
1563                 db_error("Must set unit with ahd_sunit first!\n");
1564                 return;
1565         }
1566         if (have_addr == 0)
1567                 return;
1568
1569         size = 1;
1570         while ((c = *modif++) != '\0') {
1571                 switch (c) {
1572                 case 'b':
1573                         size = 1;
1574                         break;
1575                 case 'w':
1576                         size = 2;
1577                         break;
1578                 case 'l':
1579                         size = 4;
1580                 break;
1581                 }
1582         }
1583
1584         if (count <= 0)
1585                 count = 1;
1586         while (--count >= 0) {
1587                 db_printf("%04lx (M)%x: \t", (u_long)addr,
1588                           ahd_inb(ahd_ddb_softc, MODE_PTR));
1589                 switch (size) {
1590                 case 1:
1591                         db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1592                         break;
1593                 case 2:
1594                         db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1595                         break;
1596                 case 4:
1597                         db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1598                         break;
1599                 }
1600         }
1601 }
1602
1603 DB_SET(ahd_out, ahd_ddb_out, db_cmd_set, CS_MORE, NULL)
1604 {
1605         db_expr_t old_value;
1606         db_expr_t new_value;
1607         int       size;
1608  
1609         if (ahd_ddb_softc == NULL) {
1610                 db_error("Must set unit with ahd_sunit first!\n");
1611                 return;
1612         }
1613
1614         switch (modif[0]) {
1615         case '\0':
1616         case 'b':
1617                 size = 1;
1618                 break;
1619         case 'h':
1620                 size = 2;
1621                 break;
1622         case 'l':
1623                 size = 4;
1624                 break;
1625         default:
1626                 db_error("Unknown size\n");
1627                 return;
1628         }
1629  
1630         while (db_expression(&new_value)) {
1631                 switch (size) {
1632                 default:
1633                 case 1:
1634                         old_value = ahd_inb(ahd_ddb_softc, addr);
1635                         ahd_outb(ahd_ddb_softc, addr, new_value);
1636                         break;
1637                 case 2:
1638                         old_value = ahd_inw(ahd_ddb_softc, addr);
1639                         ahd_outw(ahd_ddb_softc, addr, new_value);
1640                         break;
1641                 case 4:
1642                         old_value = ahd_inl(ahd_ddb_softc, addr);
1643                         ahd_outl(ahd_ddb_softc, addr, new_value);
1644                         break;
1645                 }
1646                 db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1647                           (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1648                           (u_long)old_value, (u_long)new_value);
1649                 addr += size;
1650         }
1651         db_skip_to_eol();
1652 }
1653
1654 DB_COMMAND(ahd_dump, ahd_ddb_dump)
1655 {
1656         if (ahd_ddb_softc == NULL) {
1657                 db_error("Must set unit with ahd_sunit first!\n");
1658                 return;
1659         }
1660         ahd_dump_card_state(ahd_ddb_softc);
1661 }
1662
1663 #endif
1664
1665
1666 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1667 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1668 MODULE_VERSION(ahd, 1);