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