2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Luoqi Chen.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <sys/malloc.h>
41 #include <machine/bus.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_sim.h>
47 #include <cam/cam_xpt_sim.h>
48 #include <cam/cam_debug.h>
50 #include <cam/scsi/scsi_message.h>
52 #include <dev/aic/aic6360reg.h>
53 #include <dev/aic/aicvar.h>
55 static void aic_action(struct cam_sim *sim, union ccb *ccb);
56 static void aic_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
58 static void aic_intr_locked(struct aic_softc *aic);
59 static void aic_start(struct aic_softc *aic);
60 static void aic_select(struct aic_softc *aic);
61 static void aic_selected(struct aic_softc *aic);
62 static void aic_reselected(struct aic_softc *aic);
63 static void aic_reconnect(struct aic_softc *aic, int tag);
64 static void aic_cmd(struct aic_softc *aic);
65 static void aic_msgin(struct aic_softc *aic);
66 static void aic_handle_msgin(struct aic_softc *aic);
67 static void aic_msgout(struct aic_softc *aic);
68 static void aic_datain(struct aic_softc *aic);
69 static void aic_dataout(struct aic_softc *aic);
70 static void aic_done(struct aic_softc *aic, struct aic_scb *scb);
71 static void aic_poll(struct cam_sim *sim);
72 static void aic_timeout(void *arg);
73 static void aic_scsi_reset(struct aic_softc *aic);
74 static void aic_chip_reset(struct aic_softc *aic);
75 static void aic_reset(struct aic_softc *aic, int initiate_reset);
77 devclass_t aic_devclass;
79 static struct aic_scb *
80 aic_get_scb(struct aic_softc *aic)
85 mtx_assert(&aic->lock, MA_OWNED);
86 if ((scb = SLIST_FIRST(&aic->free_scbs)) != NULL)
87 SLIST_REMOVE_HEAD(&aic->free_scbs, link);
92 aic_free_scb(struct aic_softc *aic, struct aic_scb *scb)
96 mtx_assert(&aic->lock, MA_OWNED);
97 if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0 &&
98 (scb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
99 scb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
100 aic->flags &= ~AIC_RESOURCE_SHORTAGE;
103 SLIST_INSERT_HEAD(&aic->free_scbs, scb, link);
107 aic_action(struct cam_sim *sim, union ccb *ccb)
109 struct aic_softc *aic;
111 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("aic_action\n"));
113 aic = (struct aic_softc *)cam_sim_softc(sim);
114 mtx_assert(&aic->lock, MA_OWNED);
116 switch (ccb->ccb_h.func_code) {
117 case XPT_SCSI_IO: /* Execute the requested I/O operation */
118 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
122 if ((scb = aic_get_scb(aic)) == NULL) {
123 aic->flags |= AIC_RESOURCE_SHORTAGE;
124 xpt_freeze_simq(aic->sim, /*count*/1);
125 ccb->ccb_h.status = CAM_REQUEUE_REQ;
131 ccb->ccb_h.ccb_scb_ptr = scb;
132 ccb->ccb_h.ccb_aic_ptr = aic;
134 scb->target = ccb->ccb_h.target_id;
135 scb->lun = ccb->ccb_h.target_lun;
137 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
138 scb->cmd_len = ccb->csio.cdb_len;
139 if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
140 if (ccb->ccb_h.flags & CAM_CDB_PHYS) {
141 ccb->ccb_h.status = CAM_REQ_INVALID;
142 aic_free_scb(aic, scb);
146 scb->cmd_ptr = ccb->csio.cdb_io.cdb_ptr;
148 scb->cmd_ptr = ccb->csio.cdb_io.cdb_bytes;
150 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
151 if ((ccb->ccb_h.flags & CAM_DATA_MASK) !=
153 ccb->ccb_h.status = CAM_REQ_INVALID;
154 aic_free_scb(aic, scb);
158 scb->data_ptr = ccb->csio.data_ptr;
159 scb->data_len = ccb->csio.dxfer_len;
161 scb->data_ptr = NULL;
164 aic_execute_scb(scb, NULL, 0, 0);
166 scb->flags |= SCB_DEVICE_RESET;
167 aic_execute_scb(scb, NULL, 0, 0);
171 case XPT_SET_TRAN_SETTINGS:
173 struct ccb_trans_settings *cts = &ccb->cts;
174 struct aic_tinfo *ti = &aic->tinfo[ccb->ccb_h.target_id];
175 struct ccb_trans_settings_scsi *scsi =
176 &cts->proto_specific.scsi;
177 struct ccb_trans_settings_spi *spi =
178 &cts->xport_specific.spi;
180 if ((spi->valid & CTS_SPI_VALID_DISC) != 0 &&
181 (aic->flags & AIC_DISC_ENABLE) != 0) {
182 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
183 ti->flags |= TINFO_DISC_ENB;
185 ti->flags &= ~TINFO_DISC_ENB;
188 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
189 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
190 ti->flags |= TINFO_TAG_ENB;
192 ti->flags &= ~TINFO_TAG_ENB;
195 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
196 ti->goal.period = spi->sync_period;
198 if (ti->goal.period > aic->min_period) {
201 } else if (ti->goal.period < aic->max_period)
202 ti->goal.period = aic->max_period;
205 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
206 ti->goal.offset = spi->sync_offset;
207 if (ti->goal.offset == 0)
209 else if (ti->goal.offset > AIC_SYNC_OFFSET)
210 ti->goal.offset = AIC_SYNC_OFFSET;
213 if ((ti->goal.period != ti->current.period)
214 || (ti->goal.offset != ti->current.offset))
215 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;
237 if ((ti->flags & TINFO_DISC_ENB) != 0)
238 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
239 if ((ti->flags & TINFO_TAG_ENB) != 0)
240 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
242 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
243 spi->sync_period = ti->current.period;
244 spi->sync_offset = ti->current.offset;
246 spi->sync_period = ti->user.period;
247 spi->sync_offset = ti->user.offset;
250 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
251 spi->valid = CTS_SPI_VALID_SYNC_RATE
252 | CTS_SPI_VALID_SYNC_OFFSET
253 | CTS_SPI_VALID_BUS_WIDTH
254 | CTS_SPI_VALID_DISC;
255 scsi->valid = CTS_SCSI_VALID_TQ;
257 ccb->ccb_h.status = CAM_REQ_CMP;
261 case XPT_CALC_GEOMETRY:
263 cam_calc_geometry(&ccb->ccg, /*extended*/1);
267 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
268 aic_reset(aic, /*initiate_reset*/TRUE);
269 ccb->ccb_h.status = CAM_REQ_CMP;
272 case XPT_PATH_INQ: /* Path routing inquiry */
274 struct ccb_pathinq *cpi = &ccb->cpi;
276 cpi->version_num = 1; /* XXX??? */
277 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
278 cpi->target_sprt = 0;
280 cpi->hba_eng_cnt = 0;
283 cpi->initiator_id = aic->initiator;
284 cpi->bus_id = cam_sim_bus(sim);
285 cpi->base_transfer_speed = 3300;
286 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
287 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
288 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
289 cpi->unit_number = cam_sim_unit(sim);
290 cpi->transport = XPORT_SPI;
291 cpi->transport_version = 2;
292 cpi->protocol = PROTO_SCSI;
293 cpi->protocol_version = SCSI_REV_2;
294 cpi->ccb_h.status = CAM_REQ_CMP;
299 ccb->ccb_h.status = CAM_REQ_INVALID;
306 aic_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
308 struct aic_scb *scb = (struct aic_scb *)arg;
309 union ccb *ccb = scb->ccb;
310 struct aic_softc *aic = (struct aic_softc *)ccb->ccb_h.ccb_aic_ptr;
313 mtx_assert(&aic->lock, MA_OWNED);
314 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
315 aic_free_scb(aic, scb);
320 scb->flags |= SCB_ACTIVE;
321 ccb->ccb_h.status |= CAM_SIM_QUEUED;
322 TAILQ_INSERT_TAIL(&aic->pending_ccbs, &ccb->ccb_h, sim_links.tqe);
324 callout_reset_sbt(&scb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
325 aic_timeout, scb, 0);
331 * Start another command if the controller is not busy.
334 aic_start(struct aic_softc *aic)
336 struct ccb_hdr *ccb_h;
337 struct aic_tinfo *ti;
339 if (aic->state != AIC_IDLE)
342 TAILQ_FOREACH(ccb_h, &aic->pending_ccbs, sim_links.tqe) {
343 ti = &aic->tinfo[ccb_h->target_id];
344 if ((ti->lubusy & (1 << ccb_h->target_lun)) == 0) {
345 TAILQ_REMOVE(&aic->pending_ccbs, ccb_h, sim_links.tqe);
346 aic->nexus = (struct aic_scb *)ccb_h->ccb_scb_ptr;
352 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_start: idle\n"));
354 aic_outb(aic, SIMODE0, ENSELDI);
355 aic_outb(aic, SIMODE1, ENSCSIRST);
356 aic_outb(aic, SCSISEQ, ENRESELI);
363 aic_select(struct aic_softc *aic)
365 struct aic_scb *scb = aic->nexus;
367 CAM_DEBUG(scb->ccb->ccb_h.path, CAM_DEBUG_TRACE,
368 ("aic_select - ccb %p\n", scb->ccb));
370 aic->state = AIC_SELECTING;
372 aic_outb(aic, DMACNTRL1, 0);
373 aic_outb(aic, SCSIID, aic->initiator << OID_S | scb->target);
374 aic_outb(aic, SXFRCTL1, STIMO_256ms | ENSTIMER |
375 (aic->flags & AIC_PARITY_ENABLE ? ENSPCHK : 0));
377 aic_outb(aic, SIMODE0, ENSELDI|ENSELDO);
378 aic_outb(aic, SIMODE1, ENSCSIRST|ENSELTIMO);
379 aic_outb(aic, SCSISEQ, ENRESELI|ENSELO|ENAUTOATNO);
383 * We have successfully selected a target, prepare for the information
387 aic_selected(struct aic_softc *aic)
389 struct aic_scb *scb = aic->nexus;
390 union ccb *ccb = scb->ccb;
391 struct aic_tinfo *ti = &aic->tinfo[scb->target];
393 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
394 ("aic_selected - ccb %p\n", ccb));
396 aic->state = AIC_HASNEXUS;
398 if (scb->flags & SCB_DEVICE_RESET) {
399 aic->msg_buf[0] = MSG_BUS_DEV_RESET;
401 aic->msg_outq = AIC_MSG_MSGBUF;
403 aic->msg_outq = AIC_MSG_IDENTIFY;
404 if ((ti->flags & TINFO_TAG_ENB) != 0 &&
405 (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0)
406 aic->msg_outq |= AIC_MSG_TAG_Q;
408 ti->lubusy |= 1 << scb->lun;
409 if ((ti->flags & TINFO_SDTR_NEGO) != 0)
410 aic->msg_outq |= AIC_MSG_SDTR;
413 aic_outb(aic, CLRSINT0, CLRSELDO);
414 aic_outb(aic, CLRSINT1, CLRBUSFREE);
415 aic_outb(aic, SCSISEQ, ENAUTOATNP);
416 aic_outb(aic, SIMODE0, 0);
417 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
418 aic_outb(aic, SCSIRATE, ti->scsirate);
422 * We are re-selected by a target, save the target id and wait for the
423 * target to further identify itself.
426 aic_reselected(struct aic_softc *aic)
430 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reselected\n"));
433 * If we have started a selection, it must have lost out in
434 * the arbitration, put the command back to the pending queue.
437 TAILQ_INSERT_HEAD(&aic->pending_ccbs,
438 &aic->nexus->ccb->ccb_h, sim_links.tqe);
442 selid = aic_inb(aic, SELID) & ~(1 << aic->initiator);
443 if (selid & (selid - 1)) {
444 /* this should never have happened */
445 printf("aic_reselected: invalid selid %x\n", selid);
446 aic_reset(aic, /*initiate_reset*/TRUE);
450 aic->state = AIC_RESELECTED;
451 aic->target = ffs(selid) - 1;
454 aic_outb(aic, CLRSINT0, CLRSELDI);
455 aic_outb(aic, CLRSINT1, CLRBUSFREE);
456 aic_outb(aic, SIMODE0, 0);
457 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
458 aic_outb(aic, SCSISEQ, ENAUTOATNP);
459 aic_outb(aic, SCSIRATE, aic->tinfo[aic->target].scsirate);
463 * Raise ATNO to signal the target that we have a message for it.
466 aic_sched_msgout(struct aic_softc *aic, u_int8_t msg)
469 aic->msg_buf[0] = msg;
472 aic->msg_outq |= AIC_MSG_MSGBUF;
473 aic_outb(aic, SCSISIGO, aic_inb(aic, SCSISIGI) | ATNO);
477 * Wait for SPIORDY (SCSI PIO ready) flag, or a phase change.
480 aic_spiordy(struct aic_softc *aic)
482 while (!(aic_inb(aic, DMASTAT) & INTSTAT) &&
483 !(aic_inb(aic, SSTAT0) & SPIORDY))
485 return !(aic_inb(aic, DMASTAT) & INTSTAT);
489 * Reestablish a disconnected nexus.
492 aic_reconnect(struct aic_softc *aic, int tag)
495 struct ccb_hdr *ccb_h;
497 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reconnect\n"));
501 TAILQ_FOREACH(ccb_h, &aic->nexus_ccbs, sim_links.tqe) {
502 scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
503 if (scb->target == aic->target && scb->lun == aic->lun &&
504 (tag == -1 || scb->tag == tag))
508 /* ABORT if nothing is found */
511 aic_sched_msgout(aic, MSG_ABORT);
513 aic_sched_msgout(aic, MSG_ABORT_TAG);
514 xpt_async(AC_UNSOL_RESEL, aic->path, NULL);
518 /* Reestablish the nexus */
519 TAILQ_REMOVE(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
521 scb->flags &= ~SCB_DISCONNECTED;
522 aic->state = AIC_HASNEXUS;
529 aic_msgin(struct aic_softc *aic)
533 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_msgin\n"));
535 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
536 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
538 aic->flags &= ~AIC_DROP_MSGIN;
542 * If a parity error is detected, drop the remaining
543 * bytes and inform the target so it could resend
546 if (aic_inb(aic, SSTAT1) & SCSIPERR) {
547 aic_outb(aic, CLRSINT1, CLRSCSIPERR);
548 aic->flags |= AIC_DROP_MSGIN;
549 aic_sched_msgout(aic, MSG_PARITY_ERROR);
551 if ((aic->flags & AIC_DROP_MSGIN)) {
552 aic_inb(aic, SCSIDAT);
555 /* read the message byte without ACKing on it */
556 aic->msg_buf[aic->msg_len++] = aic_inb(aic, SCSIBUS);
557 if (aic->msg_buf[0] == MSG_EXTENDED) {
558 if (aic->msg_len < 2) {
559 (void) aic_inb(aic, SCSIDAT);
562 switch (aic->msg_buf[2]) {
564 msglen = MSG_EXT_SDTR_LEN;
567 msglen = MSG_EXT_WDTR_LEN;
573 if (aic->msg_buf[1] != msglen) {
574 aic->flags |= AIC_DROP_MSGIN;
575 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
578 } else if (aic->msg_buf[0] >= 0x20 && aic->msg_buf[0] <= 0x2f)
583 * If we have a complete message, handle it before the final
584 * ACK (in case we decide to reject the message).
586 if (aic->msg_len == msglen) {
587 aic_handle_msgin(aic);
590 /* ACK on the message byte */
591 (void) aic_inb(aic, SCSIDAT);
592 } while (aic_spiordy(aic));
594 aic_outb(aic, SXFRCTL0, CHEN);
595 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
602 aic_handle_msgin(struct aic_softc *aic)
605 struct ccb_hdr *ccb_h;
606 struct aic_tinfo *ti;
607 struct ccb_trans_settings neg;
608 struct ccb_trans_settings_spi *spi = &neg.xport_specific.spi;
610 if (aic->state == AIC_RESELECTED) {
611 if (!MSG_ISIDENTIFY(aic->msg_buf[0])) {
612 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
615 aic->lun = aic->msg_buf[0] & MSG_IDENTIFY_LUNMASK;
616 if (aic->tinfo[aic->target].lubusy & (1 << aic->lun))
617 aic_reconnect(aic, -1);
619 aic->state = AIC_RECONNECTING;
623 if (aic->state == AIC_RECONNECTING) {
624 if (aic->msg_buf[0] != MSG_SIMPLE_Q_TAG) {
625 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
628 aic_reconnect(aic, aic->msg_buf[1]);
632 switch (aic->msg_buf[0]) {
633 case MSG_CMDCOMPLETE: {
634 struct ccb_scsiio *csio;
636 ccb_h = &scb->ccb->ccb_h;
637 csio = &scb->ccb->csio;
638 if ((scb->flags & SCB_SENSE) != 0) {
639 /* auto REQUEST SENSE command */
640 scb->flags &= ~SCB_SENSE;
641 csio->sense_resid = scb->data_len;
642 if (scb->status == SCSI_STATUS_OK) {
644 CAM_SCSI_STATUS_ERROR|CAM_AUTOSNS_VALID;
645 /*scsi_sense_print(csio);*/
647 ccb_h->status |= CAM_AUTOSENSE_FAIL;
648 printf("ccb %p sense failed %x\n",
652 csio->scsi_status = scb->status;
653 csio->resid = scb->data_len;
654 if (scb->status == SCSI_STATUS_OK) {
655 /* everything goes well */
656 ccb_h->status |= CAM_REQ_CMP;
657 } else if ((ccb_h->flags & CAM_DIS_AUTOSENSE) == 0 &&
658 (csio->scsi_status == SCSI_STATUS_CHECK_COND ||
659 csio->scsi_status == SCSI_STATUS_CMD_TERMINATED)) {
660 /* try to retrieve sense information */
661 scb->flags |= SCB_SENSE;
662 aic->flags |= AIC_BUSFREE_OK;
665 ccb_h->status |= CAM_SCSI_STATUS_ERROR;
668 aic->flags |= AIC_BUSFREE_OK;
672 switch (aic->msg_buf[2]) {
675 ti = &aic->tinfo[scb->target];
676 if (ti->flags & TINFO_SDTR_SENT) {
677 ti->current.period = aic->msg_buf[3];
678 ti->current.offset = aic->msg_buf[4];
680 ti->current.period = aic->msg_buf[3] =
681 max(ti->goal.period, aic->msg_buf[3]);
682 ti->current.offset = aic->msg_buf[4] =
683 min(ti->goal.offset, aic->msg_buf[4]);
685 * The target initiated the negotiation,
686 * send back a response.
688 aic_sched_msgout(aic, 0);
690 ti->flags &= ~(TINFO_SDTR_SENT|TINFO_SDTR_NEGO);
691 ti->scsirate = ti->current.offset ? ti->current.offset |
692 ((ti->current.period * 4 + 49) / 50 - 2) << 4 : 0;
693 aic_outb(aic, SCSIRATE, ti->scsirate);
694 memset(&neg, 0, sizeof (neg));
695 neg.protocol = PROTO_SCSI;
696 neg.protocol_version = SCSI_REV_2;
697 neg.transport = XPORT_SPI;
698 neg.transport_version = 2;
699 spi->sync_period = ti->goal.period = ti->current.period;
700 spi->sync_offset = ti->goal.offset = ti->current.offset;
701 spi->valid = CTS_SPI_VALID_SYNC_RATE
702 | CTS_SPI_VALID_SYNC_OFFSET;
703 ccb_h = &scb->ccb->ccb_h;
704 xpt_setup_ccb(&neg.ccb_h, ccb_h->path, 1);
705 xpt_async(AC_TRANSFER_NEG, ccb_h->path, &neg);
709 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
715 ccb_h = &scb->ccb->ccb_h;
716 TAILQ_INSERT_TAIL(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
717 scb->flags |= SCB_DISCONNECTED;
718 aic->flags |= AIC_BUSFREE_OK;
720 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE, ("disconnected\n"));
722 case MSG_MESSAGE_REJECT:
723 switch (aic->msg_outq & -aic->msg_outq) {
726 ti = &aic->tinfo[scb->target];
727 ti->flags &= ~TINFO_TAG_ENB;
728 ti->lubusy |= 1 << scb->lun;
732 ti = &aic->tinfo[scb->target];
733 ti->current.period = ti->goal.period = 0;
734 ti->current.offset = ti->goal.offset = 0;
735 ti->flags &= ~(TINFO_SDTR_SENT|TINFO_SDTR_NEGO);
737 aic_outb(aic, SCSIRATE, ti->scsirate);
738 memset(&neg, 0, sizeof (neg));
739 neg.protocol = PROTO_SCSI;
740 neg.protocol_version = SCSI_REV_2;
741 neg.transport = XPORT_SPI;
742 neg.transport_version = 2;
743 spi->sync_period = ti->current.period;
744 spi->sync_offset = ti->current.offset;
745 spi->valid = CTS_SPI_VALID_SYNC_RATE
746 | CTS_SPI_VALID_SYNC_OFFSET;
747 ccb_h = &scb->ccb->ccb_h;
748 xpt_setup_ccb(&neg.ccb_h, ccb_h->path, 1);
749 xpt_async(AC_TRANSFER_NEG, ccb_h->path, &neg);
755 case MSG_SAVEDATAPOINTER:
757 case MSG_RESTOREPOINTERS:
762 aic_sched_msgout(aic, MSG_MESSAGE_REJECT);
771 aic_msgout(struct aic_softc *aic)
775 struct aic_tinfo *ti;
778 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_msgout\n"));
780 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
781 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
784 * If the previous phase is also the message out phase,
785 * we need to retransmit all the messages, probably
786 * because the target has detected a parity error during
787 * the past transmission.
789 if (aic->prev_phase == PH_MSGOUT)
790 aic->msg_outq = aic->msg_sent;
793 int q = aic->msg_outq;
794 if (msgidx > 0 && msgidx == aic->msg_len) {
795 /* complete message sent, start the next one */
803 /* setup the message */
805 case AIC_MSG_IDENTIFY:
808 ti = &aic->tinfo[scb->target];
809 aic->msg_buf[0] = MSG_IDENTIFY(scb->lun,
810 (ti->flags & TINFO_DISC_ENB) &&
811 !(ccb->ccb_h.flags & CAM_DIS_DISCONNECT));
817 aic->msg_buf[0] = ccb->csio.tag_action;
818 aic->msg_buf[1] = scb->tag;
823 ti = &aic->tinfo[scb->target];
824 aic->msg_buf[0] = MSG_EXTENDED;
825 aic->msg_buf[1] = MSG_EXT_SDTR_LEN;
826 aic->msg_buf[2] = MSG_EXT_SDTR;
827 aic->msg_buf[3] = ti->goal.period;
828 aic->msg_buf[4] = ti->goal.offset;
829 aic->msg_len = MSG_EXT_SDTR_LEN + 2;
830 ti->flags |= TINFO_SDTR_SENT;
833 /* a single message already in the buffer */
834 if (aic->msg_buf[0] == MSG_BUS_DEV_RESET ||
835 aic->msg_buf[0] == MSG_ABORT ||
836 aic->msg_buf[0] == MSG_ABORT_TAG)
837 aic->flags |= AIC_BUSFREE_OK;
842 * If this is the last message byte of all messages,
843 * clear ATNO to signal transmission complete.
845 if ((q & (q - 1)) == 0 && msgidx == aic->msg_len - 1)
846 aic_outb(aic, CLRSINT1, CLRATNO);
847 /* transmit the message byte */
848 aic_outb(aic, SCSIDAT, aic->msg_buf[msgidx++]);
849 } while (aic_spiordy(aic));
851 aic_outb(aic, SXFRCTL0, CHEN);
852 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
859 aic_datain(struct aic_softc *aic)
861 struct aic_scb *scb = aic->nexus;
862 u_int8_t dmastat, dmacntrl0;
865 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_datain\n"));
867 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
868 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
871 if (aic->flags & AIC_DWIO_ENABLE)
872 dmacntrl0 |= DWORDPIO;
873 aic_outb(aic, DMACNTRL0, dmacntrl0);
875 while (scb->data_len > 0) {
877 /* wait for the fifo to fill up or a phase change */
878 dmastat = aic_inb(aic, DMASTAT);
879 if (dmastat & (INTSTAT|DFIFOFULL))
882 if (dmastat & DFIFOFULL) {
886 * No more data, wait for the remaining bytes in
887 * the scsi fifo to be transfer to the host fifo.
889 while (!(aic_inb(aic, SSTAT2) & SEMPTY))
891 n = aic_inb(aic, FIFOSTAT);
893 n = imin(scb->data_len, n);
894 if (aic->flags & AIC_DWIO_ENABLE) {
896 aic_insl(aic, DMADATALONG, scb->data_ptr, n>>2);
897 scb->data_ptr += n & ~3;
898 scb->data_len -= n & ~3;
903 aic_insw(aic, DMADATA, scb->data_ptr, n >> 1);
904 scb->data_ptr += n & ~1;
905 scb->data_len -= n & ~1;
910 aic_outb(aic, DMACNTRL0, ENDMA|B8MODE);
911 aic_insb(aic, DMADATA, scb->data_ptr, n);
914 aic_outb(aic, DMACNTRL0, dmacntrl0);
917 if (dmastat & INTSTAT)
921 aic_outb(aic, SXFRCTL0, CHEN);
922 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
929 aic_dataout(struct aic_softc *aic)
931 struct aic_scb *scb = aic->nexus;
932 u_int8_t dmastat, dmacntrl0, sstat2;
935 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_dataout\n"));
937 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
938 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
940 dmacntrl0 = ENDMA|WRITE;
941 if (aic->flags & AIC_DWIO_ENABLE)
942 dmacntrl0 |= DWORDPIO;
943 aic_outb(aic, DMACNTRL0, dmacntrl0);
945 while (scb->data_len > 0) {
947 /* wait for the fifo to clear up or a phase change */
948 dmastat = aic_inb(aic, DMASTAT);
949 if (dmastat & (INTSTAT|DFIFOEMP))
952 if (dmastat & INTSTAT)
954 n = imin(scb->data_len, FIFOSIZE);
955 if (aic->flags & AIC_DWIO_ENABLE) {
957 aic_outsl(aic, DMADATALONG, scb->data_ptr,n>>2);
958 scb->data_ptr += n & ~3;
959 scb->data_len -= n & ~3;
964 aic_outsw(aic, DMADATA, scb->data_ptr, n >> 1);
965 scb->data_ptr += n & ~1;
966 scb->data_len -= n & ~1;
971 aic_outb(aic, DMACNTRL0, ENDMA|WRITE|B8MODE);
972 aic_outsb(aic, DMADATA, scb->data_ptr, n);
975 aic_outb(aic, DMACNTRL0, dmacntrl0);
980 /* wait until all bytes in the fifos are transmitted */
981 dmastat = aic_inb(aic, DMASTAT);
982 sstat2 = aic_inb(aic, SSTAT2);
983 if ((dmastat & DFIFOEMP) && (sstat2 & SEMPTY))
985 if (dmastat & INTSTAT) {
986 /* adjust for untransmitted bytes */
987 n = aic_inb(aic, FIFOSTAT) + (sstat2 & 0xf);
991 aic_outb(aic, SXFRCTL0, CHEN|CLRCH);
992 aic_outb(aic, DMACNTRL0, RSTFIFO);
997 aic_outb(aic, SXFRCTL0, CHEN);
998 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
1002 * Send the scsi command.
1005 aic_cmd(struct aic_softc *aic)
1007 struct aic_scb *scb = aic->nexus;
1008 struct scsi_request_sense sense_cmd;
1010 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_cmd\n"));
1012 if (scb->flags & SCB_SENSE) {
1013 /* autosense request */
1014 sense_cmd.opcode = REQUEST_SENSE;
1015 sense_cmd.byte2 = scb->lun << 5;
1016 sense_cmd.length = scb->ccb->csio.sense_len;
1017 sense_cmd.control = 0;
1018 sense_cmd.unused[0] = 0;
1019 sense_cmd.unused[1] = 0;
1020 scb->cmd_ptr = (u_int8_t *)&sense_cmd;
1021 scb->cmd_len = sizeof(sense_cmd);
1022 scb->data_ptr = (u_int8_t *)&scb->ccb->csio.sense_data;
1023 scb->data_len = scb->ccb->csio.sense_len;
1026 aic_outb(aic, SIMODE1, ENSCSIRST|ENPHASEMIS|ENBUSFREE);
1027 aic_outb(aic, DMACNTRL0, ENDMA|WRITE);
1028 aic_outb(aic, SXFRCTL0, SCSIEN|DMAEN|CHEN);
1029 aic_outsw(aic, DMADATA, (u_int16_t *)scb->cmd_ptr, scb->cmd_len >> 1);
1030 while ((aic_inb(aic, SSTAT2) & SEMPTY) == 0 &&
1031 (aic_inb(aic, DMASTAT) & INTSTAT) == 0)
1033 aic_outb(aic, SXFRCTL0, CHEN);
1034 aic_outb(aic, SIMODE1, ENSCSIRST|ENBUSFREE|ENREQINIT);
1038 * Finish off a command. The caller is responsible to remove the ccb
1042 aic_done(struct aic_softc *aic, struct aic_scb *scb)
1044 union ccb *ccb = scb->ccb;
1046 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1047 ("aic_done - ccb %p status %x resid %d\n",
1048 ccb, ccb->ccb_h.status, ccb->csio.resid));
1050 callout_stop(&scb->timer);
1052 if ((scb->flags & SCB_DEVICE_RESET) != 0 &&
1053 ccb->ccb_h.func_code != XPT_RESET_DEV) {
1054 struct cam_path *path;
1055 struct ccb_hdr *ccb_h;
1058 error = xpt_create_path(&path, /*periph*/NULL,
1059 cam_sim_path(aic->sim),
1063 if (error == CAM_REQ_CMP) {
1064 xpt_async(AC_SENT_BDR, path, NULL);
1065 xpt_free_path(path);
1068 ccb_h = TAILQ_FIRST(&aic->pending_ccbs);
1069 while (ccb_h != NULL) {
1070 struct aic_scb *pending_scb;
1072 pending_scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
1073 if (ccb_h->target_id == scb->target) {
1074 ccb_h->status |= CAM_BDR_SENT;
1075 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1076 TAILQ_REMOVE(&aic->pending_ccbs,
1077 &pending_scb->ccb->ccb_h, sim_links.tqe);
1078 aic_done(aic, pending_scb);
1080 callout_reset_sbt(&pending_scb->timer,
1081 SBT_1MS * ccb_h->timeout, 0, aic_timeout,
1083 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1087 ccb_h = TAILQ_FIRST(&aic->nexus_ccbs);
1088 while (ccb_h != NULL) {
1089 struct aic_scb *nexus_scb;
1091 nexus_scb = (struct aic_scb *)ccb_h->ccb_scb_ptr;
1092 if (ccb_h->target_id == scb->target) {
1093 ccb_h->status |= CAM_BDR_SENT;
1094 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1095 TAILQ_REMOVE(&aic->nexus_ccbs,
1096 &nexus_scb->ccb->ccb_h, sim_links.tqe);
1097 aic_done(aic, nexus_scb);
1099 callout_reset_sbt(&nexus_scb->timer,
1100 SBT_1MS * ccb_h->timeout, 0, aic_timeout,
1102 ccb_h = TAILQ_NEXT(ccb_h, sim_links.tqe);
1107 if (aic->nexus == scb || scb->flags & SCB_DISCONNECTED)
1108 aic->tinfo[scb->target].lubusy &= ~(1 << scb->lun);
1110 if (aic->nexus == scb) {
1113 aic_free_scb(aic, scb);
1118 aic_poll(struct cam_sim *sim)
1120 aic_intr_locked(cam_sim_softc(sim));
1124 aic_timeout(void *arg)
1126 struct aic_scb *scb = (struct aic_scb *)arg;
1127 union ccb *ccb = scb->ccb;
1128 struct aic_softc *aic = (struct aic_softc *)ccb->ccb_h.ccb_aic_ptr;
1130 mtx_assert(&aic->lock, MA_OWNED);
1131 xpt_print_path(ccb->ccb_h.path);
1132 printf("ccb %p - timed out", ccb);
1133 if (aic->nexus && aic->nexus != scb)
1134 printf(", nexus %p", aic->nexus->ccb);
1135 printf(", phase 0x%x, state %d\n", aic_inb(aic, SCSISIGI), aic->state);
1137 if ((scb->flags & SCB_ACTIVE) == 0) {
1138 xpt_print_path(ccb->ccb_h.path);
1139 printf("ccb %p - timed out already completed\n", ccb);
1143 if ((scb->flags & SCB_DEVICE_RESET) == 0 && aic->nexus == scb) {
1144 struct ccb_hdr *ccb_h = &scb->ccb->ccb_h;
1145 struct aic_scb *pending_scb;
1147 if ((ccb_h->status & CAM_RELEASE_SIMQ) == 0) {
1148 xpt_freeze_simq(aic->sim, /*count*/1);
1149 ccb_h->status |= CAM_RELEASE_SIMQ;
1152 TAILQ_FOREACH(ccb_h, &aic->pending_ccbs, sim_links.tqe) {
1153 pending_scb = ccb_h->ccb_scb_ptr;
1154 callout_stop(&pending_scb->timer);
1157 TAILQ_FOREACH(ccb_h, &aic->nexus_ccbs, sim_links.tqe) {
1158 pending_scb = ccb_h->ccb_scb_ptr;
1159 callout_stop(&pending_scb->timer);
1162 scb->flags |= SCB_DEVICE_RESET;
1163 callout_reset(&scb->timer, 5 * hz, aic_timeout, scb);
1164 aic_sched_msgout(aic, MSG_BUS_DEV_RESET);
1166 if (aic->nexus == scb) {
1167 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1170 aic_reset(aic, /*initiate_reset*/TRUE);
1177 struct aic_softc *aic = (struct aic_softc *)arg;
1179 mtx_lock(&aic->lock);
1180 aic_intr_locked(aic);
1181 mtx_unlock(&aic->lock);
1185 aic_intr_locked(struct aic_softc *aic)
1187 u_int8_t sstat0, sstat1;
1189 struct aic_scb *scb;
1191 if (!(aic_inb(aic, DMASTAT) & INTSTAT))
1194 aic_outb(aic, DMACNTRL0, 0);
1196 sstat0 = aic_inb(aic, SSTAT0);
1197 sstat1 = aic_inb(aic, SSTAT1);
1199 if ((sstat1 & SCSIRSTI) != 0) {
1200 /* a device-initiated bus reset */
1201 aic_outb(aic, CLRSINT1, CLRSCSIRSTI);
1202 aic_reset(aic, /*initiate_reset*/FALSE);
1206 if ((sstat1 & SCSIPERR) != 0) {
1207 aic_outb(aic, CLRSINT1, CLRSCSIPERR);
1208 aic_sched_msgout(aic, MSG_PARITY_ERROR);
1209 aic_outb(aic, DMACNTRL0, INTEN);
1213 if (aic_inb(aic, SSTAT4)) {
1214 aic_outb(aic, CLRSERR, CLRSYNCERR|CLRFWERR|CLRFRERR);
1215 aic_reset(aic, /*initiate_reset*/TRUE);
1219 if (aic->state <= AIC_SELECTING) {
1220 if ((sstat0 & SELDI) != 0) {
1221 aic_reselected(aic);
1222 aic_outb(aic, DMACNTRL0, INTEN);
1226 if ((sstat0 & SELDO) != 0) {
1228 aic_outb(aic, DMACNTRL0, INTEN);
1232 if ((sstat1 & SELTO) != 0) {
1235 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1237 while ((sstat1 & BUSFREE) == 0)
1238 sstat1 = aic_inb(aic, SSTAT1);
1239 aic->flags |= AIC_BUSFREE_OK;
1243 if ((sstat1 & BUSFREE) != 0) {
1244 aic_outb(aic, SCSISEQ, 0);
1245 aic_outb(aic, CLRSINT0, sstat0);
1246 aic_outb(aic, CLRSINT1, sstat1);
1247 if ((scb = aic->nexus)) {
1248 if ((aic->flags & AIC_BUSFREE_OK) == 0) {
1250 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1252 } else if (scb->flags & SCB_DEVICE_RESET) {
1254 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
1255 xpt_async(AC_SENT_BDR,
1256 ccb->ccb_h.path, NULL);
1257 ccb->ccb_h.status |= CAM_REQ_CMP;
1259 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1261 } else if (scb->flags & SCB_SENSE) {
1262 /* autosense request */
1263 aic->flags &= ~AIC_BUSFREE_OK;
1264 aic->tinfo[scb->target].lubusy &=
1267 aic_outb(aic, DMACNTRL0, INTEN);
1271 aic->flags &= ~AIC_BUSFREE_OK;
1272 aic->state = AIC_IDLE;
1274 aic_outb(aic, DMACNTRL0, INTEN);
1278 if ((sstat1 & REQINIT) != 0) {
1279 u_int8_t phase = aic_inb(aic, SCSISIGI) & PH_MASK;
1280 aic_outb(aic, SCSISIGO, phase);
1281 aic_outb(aic, CLRSINT1, CLRPHASECHG);
1293 aic_outb(aic, DMACNTRL0, 0);
1294 aic_outb(aic, SXFRCTL0, CHEN|SPIOEN);
1295 scb->status = aic_inb(aic, SCSIDAT);
1296 aic_outb(aic, SXFRCTL0, CHEN);
1308 aic->prev_phase = phase;
1309 aic_outb(aic, DMACNTRL0, INTEN);
1313 printf("aic_intr: unexpected intr sstat0 %x sstat1 %x\n",
1315 aic_outb(aic, DMACNTRL0, INTEN);
1322 aic_chip_reset(struct aic_softc *aic)
1325 * Doc. recommends to clear these two registers before
1326 * operations commence
1328 aic_outb(aic, SCSITEST, 0);
1329 aic_outb(aic, TEST, 0);
1331 /* Reset SCSI-FIFO and abort any transfers */
1332 aic_outb(aic, SXFRCTL0, CHEN|CLRCH|CLRSTCNT);
1334 /* Reset HOST-FIFO */
1335 aic_outb(aic, DMACNTRL0, RSTFIFO);
1336 aic_outb(aic, DMACNTRL1, 0);
1338 /* Disable all selection features */
1339 aic_outb(aic, SCSISEQ, 0);
1340 aic_outb(aic, SXFRCTL1, 0);
1342 /* Disable interrupts */
1343 aic_outb(aic, SIMODE0, 0);
1344 aic_outb(aic, SIMODE1, 0);
1346 /* Clear interrupts */
1347 aic_outb(aic, CLRSINT0, 0x7f);
1348 aic_outb(aic, CLRSINT1, 0xef);
1350 /* Disable synchronous transfers */
1351 aic_outb(aic, SCSIRATE, 0);
1353 /* Haven't seen ant errors (yet) */
1354 aic_outb(aic, CLRSERR, 0x07);
1356 /* Set our SCSI-ID */
1357 aic_outb(aic, SCSIID, aic->initiator << OID_S);
1358 aic_outb(aic, BRSTCNTRL, EISA_BRST_TIM);
1362 * Reset the SCSI bus
1365 aic_scsi_reset(struct aic_softc *aic)
1367 aic_outb(aic, SCSISEQ, SCSIRSTO);
1369 aic_outb(aic, SCSISEQ, 0);
1374 * Reset. Abort all pending commands.
1377 aic_reset(struct aic_softc *aic, int initiate_reset)
1379 struct ccb_hdr *ccb_h;
1381 CAM_DEBUG_PRINT(CAM_DEBUG_TRACE, ("aic_reset\n"));
1384 aic_scsi_reset(aic);
1385 aic_chip_reset(aic);
1387 xpt_async(AC_BUS_RESET, aic->path, NULL);
1389 while ((ccb_h = TAILQ_FIRST(&aic->pending_ccbs)) != NULL) {
1390 TAILQ_REMOVE(&aic->pending_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);
1395 while ((ccb_h = TAILQ_FIRST(&aic->nexus_ccbs)) != NULL) {
1396 TAILQ_REMOVE(&aic->nexus_ccbs, ccb_h, sim_links.tqe);
1397 ccb_h->status |= CAM_SCSI_BUS_RESET;
1398 aic_done(aic, (struct aic_scb *)ccb_h->ccb_scb_ptr);
1402 ccb_h = &aic->nexus->ccb->ccb_h;
1403 ccb_h->status |= CAM_SCSI_BUS_RESET;
1404 aic_done(aic, aic->nexus);
1407 aic->state = AIC_IDLE;
1408 aic_outb(aic, DMACNTRL0, INTEN);
1411 static char *aic_chip_names[] = {
1412 "AIC6260", "AIC6360", "AIC6370", "GM82C700",
1418 } aic_chip_ids[] = {
1419 { AIC6360, IDSTRING_AIC6360 },
1420 { AIC6370, IDSTRING_AIC6370 },
1421 { GM82C700, IDSTRING_GM82C700 },
1425 aic_init(struct aic_softc *aic)
1427 struct aic_scb *scb;
1428 struct aic_tinfo *ti;
1429 u_int8_t porta, portb;
1433 TAILQ_INIT(&aic->pending_ccbs);
1434 TAILQ_INIT(&aic->nexus_ccbs);
1435 SLIST_INIT(&aic->free_scbs);
1437 aic->state = AIC_IDLE;
1438 aic->prev_phase = -1;
1441 aic_chip_reset(aic);
1442 aic_scsi_reset(aic);
1444 /* determine the chip type from its ID string */
1445 aic->chip_type = AIC6260;
1446 aic_insb(aic, ID, chip_id, sizeof(chip_id) - 1);
1447 chip_id[sizeof(chip_id) - 1] = '\0';
1448 for (i = 0; i < nitems(aic_chip_ids); i++) {
1449 if (!strcmp(chip_id, aic_chip_ids[i].idstring)) {
1450 aic->chip_type = aic_chip_ids[i].type;
1455 porta = aic_inb(aic, PORTA);
1456 portb = aic_inb(aic, PORTB);
1458 aic->initiator = PORTA_ID(porta);
1459 if (PORTA_PARITY(porta))
1460 aic->flags |= AIC_PARITY_ENABLE;
1461 if (PORTB_DISC(portb))
1462 aic->flags |= AIC_DISC_ENABLE;
1463 if (PORTB_DMA(portb))
1464 aic->flags |= AIC_DMA_ENABLE;
1467 * We can do fast SCSI (10MHz clock rate) if bit 4 of portb
1468 * is set and we've got a 6360. The 6260 can only do standard
1471 if (aic->chip_type > AIC6260 || aic_inb(aic, REV)) {
1472 if (PORTB_FSYNC(portb))
1473 aic->flags |= AIC_FAST_ENABLE;
1474 aic->flags |= AIC_DWIO_ENABLE;
1477 if (aic->flags & AIC_FAST_ENABLE)
1478 aic->max_period = AIC_FAST_SYNC_PERIOD;
1480 aic->max_period = AIC_SYNC_PERIOD;
1481 aic->min_period = AIC_MIN_SYNC_PERIOD;
1483 for (i = 255; i >= 0; i--) {
1484 scb = &aic->scbs[i];
1486 callout_init_mtx(&scb->timer, &aic->lock, 0);
1487 aic_free_scb(aic, scb);
1490 for (i = 0; i < 8; i++) {
1491 if (i == aic->initiator)
1493 ti = &aic->tinfo[i];
1494 bzero(ti, sizeof(*ti));
1495 ti->flags = TINFO_TAG_ENB;
1496 if (aic->flags & AIC_DISC_ENABLE)
1497 ti->flags |= TINFO_DISC_ENB;
1498 ti->user.period = aic->max_period;
1499 ti->user.offset = AIC_SYNC_OFFSET;
1503 aic_outb(aic, DMACNTRL0, INTEN);
1507 aic_probe(struct aic_softc *aic)
1511 /* Remove aic6360 from possible powerdown mode */
1512 aic_outb(aic, DMACNTRL0, 0);
1515 aic_outb(aic, DMACNTRL1, 0); /* Reset stack pointer */
1516 for (i = 0; i < STSIZE; i++)
1517 aic_outb(aic, STACK, i);
1519 /* See if we can pull out the same sequence */
1520 aic_outb(aic, DMACNTRL1, 0);
1521 for (i = 0; i < STSIZE && aic_inb(aic, STACK) == i; i++)
1530 aic_attach(struct aic_softc *aic)
1532 struct cam_devq *devq;
1535 * Create the device queue for our SIM.
1537 devq = cam_simq_alloc(256);
1542 * Construct our SIM entry
1544 aic->sim = cam_sim_alloc(aic_action, aic_poll, "aic", aic,
1545 device_get_unit(aic->dev), &aic->lock, 2, 256, devq);
1546 if (aic->sim == NULL) {
1547 cam_simq_free(devq);
1551 mtx_lock(&aic->lock);
1552 if (xpt_bus_register(aic->sim, aic->dev, 0) != CAM_SUCCESS) {
1553 cam_sim_free(aic->sim, /*free_devq*/TRUE);
1554 mtx_unlock(&aic->lock);
1558 if (xpt_create_path(&aic->path, /*periph*/NULL,
1559 cam_sim_path(aic->sim), CAM_TARGET_WILDCARD,
1560 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1561 xpt_bus_deregister(cam_sim_path(aic->sim));
1562 cam_sim_free(aic->sim, /*free_devq*/TRUE);
1563 mtx_unlock(&aic->lock);
1569 device_printf(aic->dev, "%s", aic_chip_names[aic->chip_type]);
1570 if (aic->flags & AIC_DMA_ENABLE)
1572 if (aic->flags & AIC_DISC_ENABLE)
1573 printf(", disconnection");
1574 if (aic->flags & AIC_PARITY_ENABLE)
1575 printf(", parity check");
1576 if (aic->flags & AIC_FAST_ENABLE)
1577 printf(", fast SCSI");
1579 mtx_unlock(&aic->lock);
1584 aic_detach(struct aic_softc *aic)
1586 struct aic_scb *scb;
1589 mtx_lock(&aic->lock);
1590 xpt_async(AC_LOST_DEVICE, aic->path, NULL);
1591 xpt_free_path(aic->path);
1592 xpt_bus_deregister(cam_sim_path(aic->sim));
1593 cam_sim_free(aic->sim, /*free_devq*/TRUE);
1594 mtx_unlock(&aic->lock);
1595 for (i = 255; i >= 0; i--) {
1596 scb = &aic->scbs[i];
1597 callout_drain(&scb->timer);