2 * Copyright (c) 1999 Luoqi Chen.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/aic/aic.c,v 1.8 2000/01/14 23:42:35 imp Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/thread2.h>
35 #include <bus/cam/cam.h>
36 #include <bus/cam/cam_ccb.h>
37 #include <bus/cam/cam_sim.h>
38 #include <bus/cam/cam_xpt_sim.h>
39 #include <bus/cam/cam_debug.h>
41 #include <bus/cam/scsi/scsi_message.h>
43 #include "aic6360reg.h"
46 static void aic_action (struct cam_sim *sim, union ccb *ccb);
47 static void aic_execute_scb (void *arg, bus_dma_segment_t *dm_segs,
49 static void aic_start (struct aic_softc *aic);
50 static void aic_select (struct aic_softc *aic);
51 static void aic_selected (struct aic_softc *aic);
52 static void aic_reselected (struct aic_softc *aic);
53 static void aic_reconnect (struct aic_softc *aic, int tag);
54 static void aic_cmd (struct aic_softc *aic);
55 static void aic_msgin (struct aic_softc *aic);
56 static void aic_handle_msgin (struct aic_softc *aic);
57 static void aic_msgout (struct aic_softc *aic);
58 static void aic_datain (struct aic_softc *aic);
59 static void aic_dataout (struct aic_softc *aic);
60 static void aic_done (struct aic_softc *aic, struct aic_scb *scb);
61 static void aic_poll (struct cam_sim *sim);
62 static void aic_timeout (void *arg);
63 static void aic_scsi_reset (struct aic_softc *aic);
64 static void aic_chip_reset (struct aic_softc *aic);
65 static void aic_reset (struct aic_softc *aic, int initiate_reset);
67 devclass_t aic_devclass;
69 static struct aic_scb *free_scbs;
71 static struct aic_scb *
72 aic_get_scb(struct aic_softc *aic)
78 if ((scb = free_scbs) != NULL)
79 free_scbs = (struct aic_scb *)free_scbs->ccb;
86 aic_free_scb(struct aic_softc *aic, struct aic_scb *scb)
90 if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0 &&
91 (scb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
92 scb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
93 aic->flags &= ~AIC_RESOURCE_SHORTAGE;
96 scb->ccb = (union ccb *)free_scbs;
103 aic_action(struct cam_sim *sim, union ccb *ccb)
105 struct aic_softc *aic;
107 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("aic_action\n"));
109 aic = (struct aic_softc *)cam_sim_softc(sim);
111 switch (ccb->ccb_h.func_code) {
112 case XPT_SCSI_IO: /* Execute the requested I/O operation */
113 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
117 if ((scb = aic_get_scb(aic)) == NULL) {
119 aic->flags |= AIC_RESOURCE_SHORTAGE;
121 xpt_freeze_simq(aic->sim, /*count*/1);
122 ccb->ccb_h.status = CAM_REQUEUE_REQ;
128 ccb->ccb_h.ccb_scb_ptr = scb;
129 ccb->ccb_h.ccb_aic_ptr = aic;
131 scb->target = ccb->ccb_h.target_id;
132 scb->lun = ccb->ccb_h.target_lun;
134 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
135 scb->cmd_len = ccb->csio.cdb_len;
136 if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
137 if (ccb->ccb_h.flags & CAM_CDB_PHYS) {
138 ccb->ccb_h.status = CAM_REQ_INVALID;
139 aic_free_scb(aic, scb);
143 scb->cmd_ptr = ccb->csio.cdb_io.cdb_ptr;
145 scb->cmd_ptr = ccb->csio.cdb_io.cdb_bytes;
147 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
148 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) ||
149 (ccb->ccb_h.flags & CAM_DATA_PHYS)) {
150 ccb->ccb_h.status = CAM_REQ_INVALID;
151 aic_free_scb(aic, scb);
155 scb->data_ptr = ccb->csio.data_ptr;
156 scb->data_len = ccb->csio.dxfer_len;
158 scb->data_ptr = NULL;
161 aic_execute_scb(scb, NULL, 0, 0);
163 scb->flags |= SCB_DEVICE_RESET;
164 aic_execute_scb(scb, NULL, 0, 0);
168 case XPT_SET_TRAN_SETTINGS:
170 struct ccb_trans_settings *cts = cts = &ccb->cts;
171 struct aic_tinfo *ti = &aic->tinfo[ccb->ccb_h.target_id];
172 struct ccb_trans_settings_scsi *scsi =
173 &cts->proto_specific.scsi;
174 struct ccb_trans_settings_spi *spi =
175 &cts->xport_specific.spi;
179 if ((spi->valid & CTS_SPI_VALID_DISC) != 0 &&
180 (aic->flags & AIC_DISC_ENABLE) != 0) {
181 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
182 ti->flags |= TINFO_DISC_ENB;
184 ti->flags &= ~TINFO_DISC_ENB;
187 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
188 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
189 ti->flags |= TINFO_TAG_ENB;
191 ti->flags &= ~TINFO_TAG_ENB;
194 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
195 ti->goal.period = spi->sync_period;
197 if (ti->goal.period > aic->min_period) {
200 } else if (ti->goal.period < aic->max_period)
201 ti->goal.period = aic->max_period;
204 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
205 ti->goal.offset = spi->sync_offset;
206 if (ti->goal.offset == 0)
208 else if (ti->goal.offset > AIC_SYNC_OFFSET)
209 ti->goal.offset = AIC_SYNC_OFFSET;
212 if ((ti->goal.period != ti->current.period)
213 || (ti->goal.offset != ti->current.offset))
214 ti->flags |= TINFO_SDTR_NEGO;
217 ccb->ccb_h.status = CAM_REQ_CMP;
221 case XPT_GET_TRAN_SETTINGS:
223 struct ccb_trans_settings *cts = &ccb->cts;
224 struct aic_tinfo *ti = &aic->tinfo[ccb->ccb_h.target_id];
225 struct ccb_trans_settings_scsi *scsi =
226 &cts->proto_specific.scsi;
227 struct ccb_trans_settings_spi *spi =
228 &cts->xport_specific.spi;
230 cts->protocol = PROTO_SCSI;
231 cts->protocol_version = SCSI_REV_2;
232 cts->transport = XPORT_SPI;
233 cts->transport_version = 2;
234 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
235 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
238 if ((ti->flags & TINFO_DISC_ENB) != 0)
239 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
240 if ((ti->flags & TINFO_TAG_ENB) != 0)
241 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
243 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
244 spi->sync_period = ti->current.period;
245 spi->sync_offset = ti->current.offset;
247 spi->sync_period = ti->user.period;
248 spi->sync_offset = ti->user.offset;
252 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
253 spi->valid = CTS_SPI_VALID_SYNC_RATE
254 | CTS_SPI_VALID_SYNC_OFFSET
255 | CTS_SPI_VALID_BUS_WIDTH
256 | CTS_SPI_VALID_DISC;
257 scsi->valid = CTS_SCSI_VALID_TQ;
259 ccb->ccb_h.status = CAM_REQ_CMP;
263 case XPT_CALC_GEOMETRY:
265 cam_calc_geometry(&ccb->ccg, /*extended*/1);
269 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
270 aic_reset(aic, /*initiate_reset*/TRUE);
271 ccb->ccb_h.status = CAM_REQ_CMP;
274 case XPT_PATH_INQ: /* Path routing inquiry */
276 struct ccb_pathinq *cpi = &ccb->cpi;
278 cpi->version_num = 1; /* XXX??? */
279 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
280 cpi->target_sprt = 0;
282 cpi->hba_eng_cnt = 0;
285 cpi->initiator_id = aic->initiator;
286 cpi->bus_id = cam_sim_bus(sim);
287 cpi->base_transfer_speed = 3300;
288 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
289 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
290 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
291 cpi->unit_number = cam_sim_unit(sim);
292 cpi->transport = XPORT_SPI;
293 cpi->transport_version = 2;
294 cpi->protocol = PROTO_SCSI;
295 cpi->protocol_version = SCSI_REV_2;
296 cpi->ccb_h.status = CAM_REQ_CMP;
301 ccb->ccb_h.status = CAM_REQ_INVALID;
308 aic_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
310 struct aic_scb *scb = (struct aic_scb *)arg;
311 union ccb *ccb = scb->ccb;
312 struct aic_softc *aic = (struct aic_softc *)ccb->ccb_h.ccb_aic_ptr;
316 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
318 aic_free_scb(aic, scb);
323 scb->flags |= SCB_ACTIVE;
324 ccb->ccb_h.status |= CAM_SIM_QUEUED;
325 TAILQ_INSERT_TAIL(&aic->pending_ccbs, &ccb->ccb_h, sim_links.tqe);
327 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
335 * Start another command if the controller is not busy.
338 aic_start(struct aic_softc *aic)
340 struct ccb_hdr *ccb_h;
341 struct aic_tinfo *ti;
343 if (aic->state != AIC_IDLE)
346 TAILQ_FOREACH(ccb_h, &aic->pending_ccbs, sim_links.tqe) {
347 ti = &aic->tinfo[ccb_h->target_id];
348 if ((ti->lubusy & (1 << ccb_h->target_lun)) == 0) {
349 TAILQ_REMOVE(&aic->pending_ccbs, ccb_h, sim_links.tqe);
350 aic->nexus = (struct aic_scb *)ccb_h->ccb_scb_ptr;
356 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_start: idle\n"));
358 aic_outb(aic, SIMODE0, ENSELDI);
359 aic_outb(aic, SIMODE1, ENSCSIRST);
360 aic_outb(aic, SCSISEQ, ENRESELI);
367 aic_select(struct aic_softc *aic)
369 struct aic_scb *scb = aic->nexus;
371 CAM_DEBUG(scb->ccb->ccb_h.path, CAM_DEBUG_TRACE,
372 ("aic_select - ccb %p\n", scb->ccb));
374 aic->state = AIC_SELECTING;
376 aic_outb(aic, DMACNTRL1, 0);
377 aic_outb(aic, SCSIID, aic->initiator << OID_S | scb->target);
378 aic_outb(aic, SXFRCTL1, STIMO_256ms | ENSTIMER |
379 (aic->flags & AIC_PARITY_ENABLE ? ENSPCHK : 0));
381 aic_outb(aic, SIMODE0, ENSELDI|ENSELDO);
382 aic_outb(aic, SIMODE1, ENSCSIRST|ENSELTIMO);
383 aic_outb(aic, SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO);
387 * We have successfully selected a target, prepare for the information
391 aic_selected(struct aic_softc *aic)
393 struct aic_scb *scb = aic->nexus;
394 union ccb *ccb = scb->ccb;
395 struct aic_tinfo *ti = &aic->tinfo[scb->target];
397 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
398 ("aic_selected - ccb %p\n", ccb));
400 aic->state = AIC_HASNEXUS;
402 if (scb->flags & SCB_DEVICE_RESET) {
403 aic->msg_buf[0] = MSG_BUS_DEV_RESET;
405 aic->msg_outq = AIC_MSG_MSGBUF;
407 aic->msg_outq = AIC_MSG_IDENTIFY;
408 if ((ti->flags & TINFO_TAG_ENB) != 0 &&
409 (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0)
410 aic->msg_outq |= AIC_MSG_TAG_Q;
412 ti->lubusy |= 1 << scb->lun;
413 if ((ti->flags & TINFO_SDTR_NEGO) != 0)
414 aic->msg_outq |= AIC_MSG_SDTR;
417 aic_outb(aic, CLRSINT0, CLRSELDO);
418 aic_outb(aic, CLRSINT1, CLRBUSFREE);
419 aic_outb(aic, SCSISEQ, ENAUTOATNP);
420 aic_outb(aic, SIMODE0, 0);
421 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
422 aic_outb(aic, SCSIRATE, ti->scsirate);
426 * We are re-selected by a target, save the target id and wait for the
427 * target to further identify itself.
430 aic_reselected(struct aic_softc *aic)
434 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reselected\n"));
437 * If we have started a selection, it must have lost out in
438 * the arbitration, put the command back to the pending queue.
441 TAILQ_INSERT_HEAD(&aic->pending_ccbs,
442 &aic->nexus->ccb->ccb_h, sim_links.tqe);
446 selid = aic_inb(aic, SELID) & ~(1 << aic->initiator);
447 if (selid & (selid - 1)) {
448 /* this should never have happened */
449 kprintf("aic_reselected: invalid selid %x\n", selid);
450 aic_reset(aic, /*initiate_reset*/TRUE);
454 aic->state = AIC_RESELECTED;
455 aic->target = ffs(selid) - 1;
458 aic_outb(aic, CLRSINT0, CLRSELDI);
459 aic_outb(aic, CLRSINT1, CLRBUSFREE);
460 aic_outb(aic, SIMODE0, 0);
461 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
462 aic_outb(aic, SCSISEQ, ENAUTOATNP);
463 aic_outb(aic, SCSIRATE, aic->tinfo[aic->target].scsirate);
467 * Raise ATNO to signal the target that we have a message for it.
470 aic_sched_msgout(struct aic_softc *aic, u_int8_t msg)
473 aic->msg_buf[0] = msg;
476 aic->msg_outq |= AIC_MSG_MSGBUF;
477 aic_outb(aic, SCSISIGO, aic_inb(aic, SCSISIGI) | ATNO);
481 * Wait for SPIORDY (SCSI PIO ready) flag, or a phase change.
484 aic_spiordy(struct aic_softc *aic)
486 while (!(aic_inb(aic, DMASTAT) & INTSTAT) &&
487 !(aic_inb(aic, SSTAT0) & SPIORDY))
489 return !(aic_inb(aic, DMASTAT) & INTSTAT);
493 * Reestablish a disconnected nexus.
496 aic_reconnect(struct aic_softc *aic, int tag)
499 struct ccb_hdr *ccb_h;
501 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reconnect\n"));
505 TAILQ_FOREACH(ccb_h, &aic->nexus_ccbs, sim_links.tqe) {
506 scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
507 if (scb->target == aic->target && scb->lun == aic->lun &&
508 (tag == -1 || scb->tag == tag))
512 /* ABORT if nothing is found */
515 aic_sched_msgout(aic, MSG_ABORT);
517 aic_sched_msgout(aic, MSG_ABORT_TAG);
518 xpt_async(AC_UNSOL_RESEL, aic->path, NULL);
522 /* Reestablish the nexus */
523 TAILQ_REMOVE(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
525 scb->flags &= ~SCB_DISCONNECTED;
526 aic->state = AIC_HASNEXUS;
533 aic_msgin(struct aic_softc *aic)
537 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_msgin\n"));
539 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
540 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
542 aic->flags &= ~AIC_DROP_MSGIN;
546 * If a parity error is detected, drop the remaining
547 * bytes and inform the target so it could resend
550 if (aic_inb(aic, SSTAT1) & SCSIPERR) {
551 aic_outb(aic, CLRSINT1, CLRSCSIPERR);
552 aic->flags |= AIC_DROP_MSGIN;
553 aic_sched_msgout(aic, MSG_PARITY_ERROR);
555 if ((aic->flags & AIC_DROP_MSGIN)) {
556 aic_inb(aic, SCSIDAT);
559 /* read the message byte without ACKing on it */
560 aic->msg_buf[aic->msg_len++] = aic_inb(aic, SCSIBUS);
561 if (aic->msg_buf[0] == MSG_EXTENDED) {
562 if (aic->msg_len < 2) {
563 (void) aic_inb(aic, SCSIDAT);
566 switch (aic->msg_buf[2]) {
568 msglen = MSG_EXT_SDTR_LEN;
571 msglen = MSG_EXT_WDTR_LEN;
577 if (aic->msg_buf[1] != msglen) {
578 aic->flags |= AIC_DROP_MSGIN;
579 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
582 } else if (aic->msg_buf[0] >= 0x20 && aic->msg_buf[0] <= 0x2f)
587 * If we have a complete message, handle it before the final
588 * ACK (in case we decide to reject the message).
590 if (aic->msg_len == msglen) {
591 aic_handle_msgin(aic);
594 /* ACK on the message byte */
595 (void) aic_inb(aic, SCSIDAT);
596 } while (aic_spiordy(aic));
598 aic_outb(aic, SXFRCTL0, CHEN);
599 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
606 aic_handle_msgin(struct aic_softc *aic)
609 struct ccb_hdr *ccb_h;
610 struct aic_tinfo *ti;
611 struct ccb_trans_settings neg;
612 struct ccb_trans_settings_spi *spi = &neg.xport_specific.spi;
614 if (aic->state == AIC_RESELECTED) {
615 if (!MSG_ISIDENTIFY(aic->msg_buf[0])) {
616 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
619 aic->lun = aic->msg_buf[0] & MSG_IDENTIFY_LUNMASK;
620 if (aic->tinfo[aic->target].lubusy & (1 << aic->lun))
621 aic_reconnect(aic, -1);
623 aic->state = AIC_RECONNECTING;
627 if (aic->state == AIC_RECONNECTING) {
628 if (aic->msg_buf[0] != MSG_SIMPLE_Q_TAG) {
629 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
632 aic_reconnect(aic, aic->msg_buf[1]);
636 switch (aic->msg_buf[0]) {
637 case MSG_CMDCOMPLETE: {
638 struct ccb_scsiio *csio;
640 ccb_h = &scb->ccb->ccb_h;
641 csio = &scb->ccb->csio;
642 if ((scb->flags & SCB_SENSE) != 0) {
643 /* auto REQUEST SENSE command */
644 scb->flags &= ~SCB_SENSE;
645 csio->sense_resid = scb->data_len;
646 if (scb->status == SCSI_STATUS_OK) {
648 CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
649 /*scsi_sense_print(csio);*/
651 ccb_h->status |= CAM_AUTOSENSE_FAIL;
652 kprintf("ccb %p sense failed %x\n",
656 csio->scsi_status = scb->status;
657 csio->resid = scb->data_len;
658 if (scb->status == SCSI_STATUS_OK) {
659 /* everything goes well */
660 ccb_h->status |= CAM_REQ_CMP;
661 } else if ((ccb_h->flags & CAM_DIS_AUTOSENSE) == 0 &&
662 (csio->scsi_status == SCSI_STATUS_CHECK_COND ||
663 csio->scsi_status == SCSI_STATUS_CMD_TERMINATED)) {
664 /* try to retrieve sense information */
665 scb->flags |= SCB_SENSE;
666 aic->flags |= AIC_BUSFREE_OK;
669 ccb_h->status |= CAM_SCSI_STATUS_ERROR;
672 aic->flags |= AIC_BUSFREE_OK;
676 switch (aic->msg_buf[2]) {
679 ti = &aic->tinfo[scb->target];
680 if (ti->flags & TINFO_SDTR_SENT) {
681 ti->current.period = aic->msg_buf[3];
682 ti->current.offset = aic->msg_buf[4];
684 ti->current.period = aic->msg_buf[3] =
685 max(ti->goal.period, aic->msg_buf[3]);
686 ti->current.offset = aic->msg_buf[4] =
687 min(ti->goal.offset, aic->msg_buf[4]);
689 * The target initiated the negotiation,
690 * send back a response.
692 aic_sched_msgout(aic, 0);
694 ti->flags &= ~(TINFO_SDTR_SENT|TINFO_SDTR_NEGO);
695 ti->scsirate = ti->current.offset ? ti->current.offset |
696 ((ti->current.period * 4 + 49) / 50 - 2) << 4 : 0;
697 aic_outb(aic, SCSIRATE, ti->scsirate);
698 memset(&neg, 0, sizeof (neg));
699 neg.protocol = PROTO_SCSI;
700 neg.protocol_version = SCSI_REV_2;
701 neg.transport = XPORT_SPI;
702 neg.transport_version = 2;
703 spi->sync_period = ti->goal.period = ti->current.period;
704 spi->sync_offset = ti->goal.offset = ti->current.offset;
705 spi->valid = CTS_SPI_VALID_SYNC_RATE
706 | CTS_SPI_VALID_SYNC_OFFSET;
707 ccb_h = &scb->ccb->ccb_h;
708 xpt_setup_ccb(&neg.ccb_h, ccb_h->path, 1);
709 xpt_async(AC_TRANSFER_NEG, ccb_h->path, &neg);
713 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
719 ccb_h = &scb->ccb->ccb_h;
720 TAILQ_INSERT_TAIL(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
721 scb->flags |= SCB_DISCONNECTED;
722 aic->flags |= AIC_BUSFREE_OK;
724 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE, ("disconnected\n"));
726 case MSG_MESSAGE_REJECT:
727 switch (aic->msg_outq & -aic->msg_outq) {
730 ti = &aic->tinfo[scb->target];
731 ti->flags &= ~TINFO_TAG_ENB;
732 ti->lubusy |= 1 << scb->lun;
736 ti = &aic->tinfo[scb->target];
737 ti->current.period = ti->goal.period = 0;
738 ti->current.offset = ti->goal.offset = 0;
739 ti->flags &= ~(TINFO_SDTR_SENT|TINFO_SDTR_NEGO);
741 aic_outb(aic, SCSIRATE, ti->scsirate);
742 memset(&neg, 0, sizeof (neg));
743 neg.protocol = PROTO_SCSI;
744 neg.protocol_version = SCSI_REV_2;
745 neg.transport = XPORT_SPI;
746 neg.transport_version = 2;
747 spi->sync_period = ti->current.period;
748 spi->sync_offset = ti->current.offset;
749 spi->valid = CTS_SPI_VALID_SYNC_RATE
750 | CTS_SPI_VALID_SYNC_OFFSET;
751 ccb_h = &scb->ccb->ccb_h;
752 xpt_setup_ccb(&neg.ccb_h, ccb_h->path, 1);
753 xpt_async(AC_TRANSFER_NEG, ccb_h->path, &neg);
759 case MSG_SAVEDATAPOINTER:
761 case MSG_RESTOREPOINTERS:
766 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
775 aic_msgout(struct aic_softc *aic)
779 struct aic_tinfo *ti;
782 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_msgout\n"));
784 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
785 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
788 * If the previous phase is also the message out phase,
789 * we need to retransmit all the messages, probably
790 * because the target has detected a parity error during
791 * the past transmission.
793 if (aic->prev_phase == PH_MSGOUT)
794 aic->msg_outq = aic->msg_sent;
797 int q = aic->msg_outq;
798 if (msgidx > 0 && msgidx == aic->msg_len) {
799 /* complete message sent, start the next one */
807 /* setup the message */
809 case AIC_MSG_IDENTIFY:
812 ti = &aic->tinfo[scb->target];
813 aic->msg_buf[0] = MSG_IDENTIFY(scb->lun,
814 (ti->flags & TINFO_DISC_ENB) &&
815 !(ccb->ccb_h.flags & CAM_DIS_DISCONNECT));
821 aic->msg_buf[0] = ccb->csio.tag_action;
822 aic->msg_buf[1] = scb->tag;
827 ti = &aic->tinfo[scb->target];
828 aic->msg_buf[0] = MSG_EXTENDED;
829 aic->msg_buf[1] = MSG_EXT_SDTR_LEN;
830 aic->msg_buf[2] = MSG_EXT_SDTR;
831 aic->msg_buf[3] = ti->goal.period;
832 aic->msg_buf[4] = ti->goal.offset;
833 aic->msg_len = MSG_EXT_SDTR_LEN + 2;
834 ti->flags |= TINFO_SDTR_SENT;
837 /* a single message already in the buffer */
838 if (aic->msg_buf[0] == MSG_BUS_DEV_RESET ||
839 aic->msg_buf[0] == MSG_ABORT ||
840 aic->msg_buf[0] == MSG_ABORT_TAG)
841 aic->flags |= AIC_BUSFREE_OK;
846 * If this is the last message byte of all messages,
847 * clear ATNO to signal transmission complete.
849 if ((q & (q - 1)) == 0 && msgidx == aic->msg_len - 1)
850 aic_outb(aic, CLRSINT1, CLRATNO);
851 /* transmit the message byte */
852 aic_outb(aic, SCSIDAT, aic->msg_buf[msgidx++]);
853 } while (aic_spiordy(aic));
855 aic_outb(aic, SXFRCTL0, CHEN);
856 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
863 aic_datain(struct aic_softc *aic)
865 struct aic_scb *scb = aic->nexus;
866 u_int8_t dmastat, dmacntrl0;
869 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_datain\n"));
871 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
872 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
875 if (aic->flags & AIC_DWIO_ENABLE)
876 dmacntrl0 |= DWORDPIO;
877 aic_outb(aic, DMACNTRL0, dmacntrl0);
879 while (scb->data_len > 0) {
881 /* wait for the fifo to fill up or a phase change */
882 dmastat = aic_inb(aic, DMASTAT);
883 if (dmastat & (INTSTAT|DFIFOFULL))
886 if (dmastat & DFIFOFULL) {
890 * No more data, wait for the remaining bytes in
891 * the scsi fifo to be transfer to the host fifo.
893 while (!(aic_inb(aic, SSTAT2) & SEMPTY))
895 n = aic_inb(aic, FIFOSTAT);
897 n = imin(scb->data_len, n);
898 if (aic->flags & AIC_DWIO_ENABLE) {
900 aic_insl(aic, DMADATALONG, scb->data_ptr, n>>2);
901 scb->data_ptr += n & ~3;
902 scb->data_len -= n & ~3;
907 aic_insw(aic, DMADATA, scb->data_ptr, n >> 1);
908 scb->data_ptr += n & ~1;
909 scb->data_len -= n & ~1;
914 aic_outb(aic, DMACNTRL0, ENDMA|B8MODE);
915 aic_insb(aic, DMADATA, scb->data_ptr, n);
918 aic_outb(aic, DMACNTRL0, dmacntrl0);
921 if (dmastat & INTSTAT)
925 aic_outb(aic, SXFRCTL0, CHEN);
926 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
933 aic_dataout(struct aic_softc *aic)
935 struct aic_scb *scb = aic->nexus;
936 u_int8_t dmastat, dmacntrl0, sstat2;
939 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_dataout\n"));
941 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
942 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
944 dmacntrl0 = ENDMA|WRITE;
945 if (aic->flags & AIC_DWIO_ENABLE)
946 dmacntrl0 |= DWORDPIO;
947 aic_outb(aic, DMACNTRL0, dmacntrl0);
949 while (scb->data_len > 0) {
951 /* wait for the fifo to clear up or a phase change */
952 dmastat = aic_inb(aic, DMASTAT);
953 if (dmastat & (INTSTAT|DFIFOEMP))
956 if (dmastat & INTSTAT)
958 n = imin(scb->data_len, FIFOSIZE);
959 if (aic->flags & AIC_DWIO_ENABLE) {
961 aic_outsl(aic, DMADATALONG, scb->data_ptr,n>>2);
962 scb->data_ptr += n & ~3;
963 scb->data_len -= n & ~3;
968 aic_outsw(aic, DMADATA, scb->data_ptr, n >> 1);
969 scb->data_ptr += n & ~1;
970 scb->data_len -= n & ~1;
975 aic_outb(aic, DMACNTRL0, ENDMA|WRITE|B8MODE);
976 aic_outsb(aic, DMADATA, scb->data_ptr, n);
979 aic_outb(aic, DMACNTRL0, dmacntrl0);
984 /* wait until all bytes in the fifos are transmitted */
985 dmastat = aic_inb(aic, DMASTAT);
986 sstat2 = aic_inb(aic, SSTAT2);
987 if ((dmastat & DFIFOEMP) && (sstat2 & SEMPTY))
989 if (dmastat & INTSTAT) {
990 /* adjust for untransmitted bytes */
991 n = aic_inb(aic, FIFOSTAT) + (sstat2 & 0xf);
995 aic_outb(aic, SXFRCTL0, CHEN|CLRCH);
996 aic_outb(aic, DMACNTRL0, RSTFIFO);
1001 aic_outb(aic, SXFRCTL0, CHEN);
1002 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
1006 * Send the scsi command.
1009 aic_cmd(struct aic_softc *aic)
1011 struct aic_scb *scb = aic->nexus;
1012 struct scsi_request_sense sense_cmd;
1014 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_cmd\n"));
1016 if (scb->flags & SCB_SENSE) {
1017 /* autosense request */
1018 sense_cmd.opcode = REQUEST_SENSE;
1019 sense_cmd.byte2 = scb->lun << 5;
1020 sense_cmd.length = scb->ccb->csio.sense_len;
1021 sense_cmd.control = 0;
1022 sense_cmd.unused[0] = 0;
1023 sense_cmd.unused[1] = 0;
1024 scb->cmd_ptr = (u_int8_t *)&sense_cmd;
1025 scb->cmd_len = sizeof(sense_cmd);
1026 scb->data_ptr = (u_int8_t *)&scb->ccb->csio.sense_data;
1027 scb->data_len = scb->ccb->csio.sense_len;
1030 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
1031 aic_outb(aic, DMACNTRL0, ENDMA|WRITE);
1032 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
1033 aic_outsw(aic, DMADATA, (u_int16_t *)scb->cmd_ptr, scb->cmd_len >> 1);
1034 while ((aic_inb(aic, SSTAT2) & SEMPTY) == 0 &&
1035 (aic_inb(aic, DMASTAT) & INTSTAT) == 0)
1037 aic_outb(aic, SXFRCTL0, CHEN);
1038 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
1042 * Finish off a command. The caller is responsible to remove the ccb
1046 aic_done(struct aic_softc *aic, struct aic_scb *scb)
1048 union ccb *ccb = scb->ccb;
1050 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1051 ("aic_done - ccb %p status %x resid %d\n",
1052 ccb, ccb->ccb_h.status, ccb->csio.resid));
1054 callout_stop(&ccb->ccb_h.timeout_ch);
1056 if ((scb->flags & SCB_DEVICE_RESET) != 0 &&
1057 ccb->ccb_h.func_code != XPT_RESET_DEV) {
1058 struct cam_path *path;
1059 struct ccb_hdr *ccb_h;
1062 error = xpt_create_path(&path, /*periph*/NULL,
1063 cam_sim_path(aic->sim),
1067 if (error == CAM_REQ_CMP) {
1068 xpt_async(AC_SENT_BDR, path, NULL);
1069 xpt_free_path(path);
1072 ccb_h = TAILQ_FIRST(&aic->pending_ccbs);
1073 while (ccb_h != NULL) {
1074 struct aic_scb *pending_scb;
1076 pending_scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
1077 if (ccb_h->target_id == scb->target) {
1078 ccb_h->status |= CAM_BDR_SENT;
1079 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1080 TAILQ_REMOVE(&aic->pending_ccbs,
1081 &pending_scb->ccb->ccb_h, sim_links.tqe);
1082 aic_done(aic, pending_scb);
1084 callout_reset(&ccb_h->timeout_ch,
1085 (ccb_h->timeout * hz) / 1000,
1086 aic_timeout, pending_scb);
1087 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1091 ccb_h = TAILQ_FIRST(&aic->nexus_ccbs);
1092 while (ccb_h != NULL) {
1093 struct aic_scb *nexus_scb;
1095 nexus_scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
1096 if (ccb_h->target_id == scb->target) {
1097 ccb_h->status |= CAM_BDR_SENT;
1098 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1099 TAILQ_REMOVE(&aic->nexus_ccbs,
1100 &nexus_scb->ccb->ccb_h, sim_links.tqe);
1101 aic_done(aic, nexus_scb);
1103 callout_reset(&ccb_h->timeout_ch,
1104 (ccb_h->timeout * hz) / 1000,
1105 aic_timeout, nexus_scb);
1106 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1111 if (aic->nexus == scb || scb->flags & SCB_DISCONNECTED)
1112 aic->tinfo[scb->target].lubusy &= ~(1 << scb->lun);
1114 if (aic->nexus == scb) {
1117 aic_free_scb(aic, scb);
1122 aic_poll(struct cam_sim *sim)
1124 aic_intr(cam_sim_softc(sim));
1128 aic_timeout(void *arg)
1130 struct aic_scb *scb = (struct aic_scb *)arg;
1131 union ccb *ccb = scb->ccb;
1132 struct aic_softc *aic = (struct aic_softc *)ccb->ccb_h.ccb_aic_ptr;
1134 xpt_print_path(ccb->ccb_h.path);
1135 kprintf("ccb %p - timed out", ccb);
1136 if (aic->nexus && aic->nexus != scb)
1137 kprintf(", nexus %p", aic->nexus->ccb);
1138 kprintf(", phase 0x%x, state %d\n", aic_inb(aic, SCSISIGI), aic->state);
1142 if ((scb->flags & SCB_ACTIVE) == 0) {
1144 xpt_print_path(ccb->ccb_h.path);
1145 kprintf("ccb %p - timed out already completed\n", ccb);
1149 if ((scb->flags & SCB_DEVICE_RESET) == 0 && aic->nexus == scb) {
1150 struct ccb_hdr *ccb_h = &scb->ccb->ccb_h;
1152 if ((ccb_h->status & CAM_RELEASE_SIMQ) == 0) {
1153 xpt_freeze_simq(aic->sim, /*count*/1);
1154 ccb_h->status |= CAM_RELEASE_SIMQ;
1157 TAILQ_FOREACH(ccb_h, &aic->pending_ccbs, sim_links.tqe)
1158 callout_stop(&ccb_h->timeout_ch);
1160 TAILQ_FOREACH(ccb_h, &aic->nexus_ccbs, sim_links.tqe)
1161 callout_stop(&ccb_h->timeout_ch);
1163 scb->flags |= SCB_DEVICE_RESET;
1164 callout_reset(&ccb->ccb_h.timeout_ch, 5 * hz, aic_timeout, scb);
1165 aic_sched_msgout(aic, MSG_BUS_DEV_RESET);
1167 if (aic->nexus == scb) {
1168 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1171 aic_reset(aic, /*initiate_reset*/TRUE);
1180 struct aic_softc *aic = (struct aic_softc *)arg;
1181 u_int8_t sstat0, sstat1;
1183 struct aic_scb *scb;
1185 if (!(aic_inb(aic, DMASTAT) & INTSTAT))
1188 aic_outb(aic, DMACNTRL0, 0);
1190 sstat0 = aic_inb(aic, SSTAT0);
1191 sstat1 = aic_inb(aic, SSTAT1);
1193 if ((sstat1 & SCSIRSTI) != 0) {
1194 /* a device-initiated bus reset */
1195 aic_outb(aic, CLRSINT1, CLRSCSIRSTI);
1196 aic_reset(aic, /*initiate_reset*/FALSE);
1200 if ((sstat1 & SCSIPERR) != 0) {
1201 aic_outb(aic, CLRSINT1, CLRSCSIPERR);
1202 aic_sched_msgout(aic, MSG_PARITY_ERROR);
1203 aic_outb(aic, DMACNTRL0, INTEN);
1207 if (aic_inb(aic, SSTAT4)) {
1208 aic_outb(aic, CLRSERR, CLRSYNCERR|CLRFWERR|CLRFRERR);
1209 aic_reset(aic, /*initiate_reset*/TRUE);
1213 if (aic->state <= AIC_SELECTING) {
1214 if ((sstat0 & SELDI) != 0) {
1215 aic_reselected(aic);
1216 aic_outb(aic, DMACNTRL0, INTEN);
1220 if ((sstat0 & SELDO) != 0) {
1222 aic_outb(aic, DMACNTRL0, INTEN);
1226 if ((sstat1 & SELTO) != 0) {
1229 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1231 while ((sstat1 & BUSFREE) == 0)
1232 sstat1 = aic_inb(aic, SSTAT1);
1233 aic->flags |= AIC_BUSFREE_OK;
1237 if ((sstat1 & BUSFREE) != 0) {
1238 aic_outb(aic, SCSISEQ, 0);
1239 aic_outb(aic, CLRSINT0, sstat0);
1240 aic_outb(aic, CLRSINT1, sstat1);
1241 if ((scb = aic->nexus)) {
1242 if ((aic->flags & AIC_BUSFREE_OK) == 0) {
1244 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1246 } else if (scb->flags & SCB_DEVICE_RESET) {
1248 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
1249 xpt_async(AC_SENT_BDR,
1250 ccb->ccb_h.path, NULL);
1251 ccb->ccb_h.status |= CAM_REQ_CMP;
1253 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1255 } else if (scb->flags & SCB_SENSE) {
1256 /* autosense request */
1257 aic->flags &= ~AIC_BUSFREE_OK;
1258 aic->tinfo[scb->target].lubusy &=
1261 aic_outb(aic, DMACNTRL0, INTEN);
1265 aic->flags &= ~AIC_BUSFREE_OK;
1266 aic->state = AIC_IDLE;
1268 aic_outb(aic, DMACNTRL0, INTEN);
1272 if ((sstat1 & REQINIT) != 0) {
1273 u_int8_t phase = aic_inb(aic, SCSISIGI) & PH_MASK;
1274 aic_outb(aic, SCSISIGO, phase);
1275 aic_outb(aic, CLRSINT1, CLRPHASECHG);
1287 aic_outb(aic, DMACNTRL0, 0);
1288 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
1289 scb->status = aic_inb(aic, SCSIDAT);
1290 aic_outb(aic, SXFRCTL0, CHEN);
1302 aic->prev_phase = phase;
1303 aic_outb(aic, DMACNTRL0, INTEN);
1307 kprintf("aic_intr: unexpected intr sstat0 %x sstat1 %x\n",
1309 aic_outb(aic, DMACNTRL0, INTEN);
1316 aic_chip_reset(struct aic_softc *aic)
1319 * Doc. recommends to clear these two registers before
1320 * operations commence
1322 aic_outb(aic, SCSITEST, 0);
1323 aic_outb(aic, TEST, 0);
1325 /* Reset SCSI-FIFO and abort any transfers */
1326 aic_outb(aic, SXFRCTL0, CHEN|CLRCH|CLRSTCNT);
1328 /* Reset HOST-FIFO */
1329 aic_outb(aic, DMACNTRL0, RSTFIFO);
1330 aic_outb(aic, DMACNTRL1, 0);
1332 /* Disable all selection features */
1333 aic_outb(aic, SCSISEQ, 0);
1334 aic_outb(aic, SXFRCTL1, 0);
1336 /* Disable interrupts */
1337 aic_outb(aic, SIMODE0, 0);
1338 aic_outb(aic, SIMODE1, 0);
1340 /* Clear interrupts */
1341 aic_outb(aic, CLRSINT0, 0x7f);
1342 aic_outb(aic, CLRSINT1, 0xef);
1344 /* Disable synchronous transfers */
1345 aic_outb(aic, SCSIRATE, 0);
1347 /* Haven't seen ant errors (yet) */
1348 aic_outb(aic, CLRSERR, 0x07);
1350 /* Set our SCSI-ID */
1351 aic_outb(aic, SCSIID, aic->initiator << OID_S);
1352 aic_outb(aic, BRSTCNTRL, EISA_BRST_TIM);
1356 * Reset the SCSI bus
1359 aic_scsi_reset(struct aic_softc *aic)
1361 aic_outb(aic, SCSISEQ, SCSIRSTO);
1363 aic_outb(aic, SCSISEQ, 0);
1368 * Reset. Abort all pending commands.
1371 aic_reset(struct aic_softc *aic, int initiate_reset)
1373 struct ccb_hdr *ccb_h;
1375 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reset\n"));
1378 aic_scsi_reset(aic);
1379 aic_chip_reset(aic);
1381 xpt_async(AC_BUS_RESET, aic->path, NULL);
1383 while ((ccb_h = TAILQ_FIRST(&aic->pending_ccbs)) != NULL) {
1384 TAILQ_REMOVE(&aic->pending_ccbs, ccb_h, sim_links.tqe);
1385 ccb_h->status |= CAM_SCSI_BUS_RESET;
1386 aic_done(aic, (struct aic_scb *)ccb_h->ccb_scb_ptr);
1389 while ((ccb_h = TAILQ_FIRST(&aic->nexus_ccbs)) != NULL) {
1390 TAILQ_REMOVE(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
1391 ccb_h->status |= CAM_SCSI_BUS_RESET;
1392 aic_done(aic, (struct aic_scb *)ccb_h->ccb_scb_ptr);
1396 ccb_h = &aic->nexus->ccb->ccb_h;
1397 ccb_h->status |= CAM_SCSI_BUS_RESET;
1398 aic_done(aic, aic->nexus);
1401 aic->state = AIC_IDLE;
1402 aic_outb(aic, DMACNTRL0, INTEN);
1405 static char *aic_chip_names[] = {
1406 "AIC6260", "AIC6360", "AIC6370", "GM82C700",
1412 } aic_chip_ids[] = {
1413 { AIC6360, IDSTRING_AIC6360 },
1414 { AIC6370, IDSTRING_AIC6370 },
1415 { GM82C700, IDSTRING_GM82C700 },
1419 aic_init(struct aic_softc *aic)
1421 struct aic_scb *scb;
1422 struct aic_tinfo *ti;
1423 u_int8_t porta, portb;
1427 TAILQ_INIT(&aic->pending_ccbs);
1428 TAILQ_INIT(&aic->nexus_ccbs);
1430 aic->state = AIC_IDLE;
1431 aic->prev_phase = -1;
1434 aic_chip_reset(aic);
1435 aic_scsi_reset(aic);
1437 /* determine the chip type from its ID string */
1438 aic->chip_type = AIC6260;
1439 aic_insb(aic, ID, chip_id, sizeof(chip_id) - 1);
1440 chip_id[sizeof(chip_id) - 1] = '\0';
1441 for (i = 0; i < NELEM(aic_chip_ids); i++) {
1442 if (!strcmp(chip_id, aic_chip_ids[i].idstring)) {
1443 aic->chip_type = aic_chip_ids[i].type;
1448 porta = aic_inb(aic, PORTA);
1449 portb = aic_inb(aic, PORTB);
1451 aic->initiator = PORTA_ID(porta);
1452 if (PORTA_PARITY(porta))
1453 aic->flags |= AIC_PARITY_ENABLE;
1454 if (PORTB_DISC(portb))
1455 aic->flags |= AIC_DISC_ENABLE;
1456 if (PORTB_DMA(portb))
1457 aic->flags |= AIC_DMA_ENABLE;
1460 * We can do fast SCSI (10MHz clock rate) if bit 4 of portb
1461 * is set and we've got a 6360. The 6260 can only do standard
1464 if (aic->chip_type > AIC6260 || aic_inb(aic, REV)) {
1465 if (PORTB_FSYNC(portb))
1466 aic->flags |= AIC_FAST_ENABLE;
1467 aic->flags |= AIC_DWIO_ENABLE;
1470 if (aic->flags & AIC_FAST_ENABLE)
1471 aic->max_period = AIC_FAST_SYNC_PERIOD;
1473 aic->max_period = AIC_SYNC_PERIOD;
1474 aic->min_period = AIC_MIN_SYNC_PERIOD;
1477 for (i = 255; i >= 0; i--) {
1478 scb = &aic->scbs[i];
1480 aic_free_scb(aic, scb);
1483 for (i = 0; i < 8; i++) {
1484 if (i == aic->initiator)
1486 ti = &aic->tinfo[i];
1487 bzero(ti, sizeof(*ti));
1488 ti->flags = TINFO_TAG_ENB;
1489 if (aic->flags & AIC_DISC_ENABLE)
1490 ti->flags |= TINFO_DISC_ENB;
1491 ti->user.period = aic->max_period;
1492 ti->user.offset = AIC_SYNC_OFFSET;
1496 aic_outb(aic, DMACNTRL0, INTEN);
1500 aic_probe(struct aic_softc *aic)
1504 /* Remove aic6360 from possible powerdown mode */
1505 aic_outb(aic, DMACNTRL0, 0);
1508 aic_outb(aic, DMACNTRL1, 0); /* Reset stack pointer */
1509 for (i = 0; i < STSIZE; i++)
1510 aic_outb(aic, STACK, i);
1512 /* See if we can pull out the same sequence */
1513 aic_outb(aic, DMACNTRL1, 0);
1514 for (i = 0; i < STSIZE && aic_inb(aic, STACK) == i; i++)
1523 aic_attach(struct aic_softc *aic)
1526 * Construct our SIM entry
1528 aic->sim = cam_sim_alloc(aic_action, aic_poll, "aic", aic,
1529 aic->unit, &sim_mplock, 2, 256, NULL);
1530 if (aic->sim == NULL)
1533 if (xpt_bus_register(aic->sim, 0) != CAM_SUCCESS) {
1534 cam_sim_free(aic->sim);
1538 if (xpt_create_path(&aic->path, /*periph*/NULL,
1539 cam_sim_path(aic->sim), CAM_TARGET_WILDCARD,
1540 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1541 xpt_bus_deregister(cam_sim_path(aic->sim));
1542 cam_sim_free(aic->sim);
1548 kprintf("aic%d: %s", aic->unit, aic_chip_names[aic->chip_type]);
1549 if (aic->flags & AIC_DMA_ENABLE)
1551 if (aic->flags & AIC_DISC_ENABLE)
1552 kprintf(", disconnection");
1553 if (aic->flags & AIC_PARITY_ENABLE)
1554 kprintf(", parity check");
1555 if (aic->flags & AIC_FAST_ENABLE)
1556 kprintf(", fast SCSI");
1562 aic_detach(struct aic_softc *aic)
1564 xpt_async(AC_LOST_DEVICE, aic->path, NULL);
1565 xpt_free_path(aic->path);
1566 xpt_bus_deregister(cam_sim_path(aic->sim));
1567 cam_sim_free(aic->sim);