2 *********************************************************************
4 * BY : C.L. Huang (ching@tekram.com.tw)
5 * Erich Chen (erich@tekram.com.tw)
6 * Description: Device Driver for the amd53c974 PCI Bus Master
7 * SCSI Host adapter found on cards such as
8 * the Tekram DC-390(T).
9 * (C)Copyright 1995-1999 Tekram Technology Co., Ltd.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *********************************************************************
33 * $FreeBSD: src/sys/pci/amd.c,v 1.3.2.2 2001/06/02 04:32:50 nyan Exp $
34 * $DragonFly: src/sys/dev/disk/amd/amd.c,v 1.7 2005/05/24 20:58:59 dillon Exp $
38 *********************************************************************
41 * REV# DATE NAME DESCRIPTION
42 * 1.00 07/02/96 CLH First release for RELEASE-2.1.0
43 * 1.01 08/20/96 CLH Update for RELEASE-2.1.5
44 * 1.02 11/06/96 CLH Fixed more than 1 LUN scanning
45 * 1.03 12/20/96 CLH Modify to support 2.2-ALPHA
46 * 1.04 12/26/97 CLH Modify to support RELEASE-2.2.5
47 * 1.05 01/01/99 ERICH CHEN Modify to support RELEASE-3.0.x (CAM)
48 *********************************************************************
51 /* #define AMD_DEBUG0 */
52 /* #define AMD_DEBUG_SCSI_PHASE */
54 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/malloc.h>
58 #include <sys/queue.h>
60 #include <sys/kernel.h>
65 #include <machine/bus_pio.h>
66 #include <machine/bus.h>
67 #include <machine/clock.h>
68 #include <machine/resource.h>
72 #include <bus/cam/cam.h>
73 #include <bus/cam/cam_ccb.h>
74 #include <bus/cam/cam_sim.h>
75 #include <bus/cam/cam_xpt_sim.h>
76 #include <bus/cam/cam_debug.h>
78 #include <bus/cam/scsi/scsi_all.h>
79 #include <bus/cam/scsi/scsi_message.h>
81 #include <bus/pci/pcivar.h>
82 #include <bus/pci/pcireg.h>
85 #define PCI_DEVICE_ID_AMD53C974 0x20201022ul
86 #define PCI_BASE_ADDR0 0x10
88 typedef u_int (phase_handler_t)(struct amd_softc *, struct amd_srb *, u_int);
89 typedef phase_handler_t *phase_handler_func_t;
91 static void amd_intr(void *vamd);
92 static int amdstart(struct amd_softc *amd, struct amd_srb * pSRB);
93 static phase_handler_t amd_NopPhase;
95 static phase_handler_t amd_DataOutPhase0;
96 static phase_handler_t amd_DataInPhase0;
97 #define amd_CommandPhase0 amd_NopPhase
98 static phase_handler_t amd_StatusPhase0;
99 static phase_handler_t amd_MsgOutPhase0;
100 static phase_handler_t amd_MsgInPhase0;
101 static phase_handler_t amd_DataOutPhase1;
102 static phase_handler_t amd_DataInPhase1;
103 static phase_handler_t amd_CommandPhase1;
104 static phase_handler_t amd_StatusPhase1;
105 static phase_handler_t amd_MsgOutPhase1;
106 static phase_handler_t amd_MsgInPhase1;
108 static void amdsetupcommand(struct amd_softc *amd, struct amd_srb *srb);
109 static int amdparsemsg(struct amd_softc *amd);
110 static int amdhandlemsgreject(struct amd_softc *amd);
111 static void amdconstructsdtr(struct amd_softc *amd,
112 u_int period, u_int offset);
113 static u_int amdfindclockrate(struct amd_softc *amd, u_int *period);
114 static int amdsentmsg(struct amd_softc *amd, u_int msgtype, int full);
116 static void DataIO_Comm(struct amd_softc *amd, struct amd_srb *pSRB, u_int dir);
117 static void amd_Disconnect(struct amd_softc *amd);
118 static void amd_Reselect(struct amd_softc *amd);
119 static void SRBdone(struct amd_softc *amd, struct amd_srb *pSRB);
120 static void amd_ScsiRstDetect(struct amd_softc *amd);
121 static void amd_ResetSCSIBus(struct amd_softc *amd);
122 static void RequestSense(struct amd_softc *amd, struct amd_srb *pSRB);
123 static void amd_InvalidCmd(struct amd_softc *amd);
126 static void amd_timeout(void *arg1);
127 static void amd_reset(struct amd_softc *amd);
129 static u_int8_t * phystovirt(struct amd_srb *pSRB, u_int32_t xferCnt);
131 void amd_linkSRB(struct amd_softc *amd);
132 static int amd_init(device_t);
133 static void amd_load_defaults(struct amd_softc *amd);
134 static void amd_load_eeprom_or_defaults(struct amd_softc *amd);
135 static int amd_EEpromInDO(struct amd_softc *amd);
136 static u_int16_t EEpromGetData1(struct amd_softc *amd);
137 static void amd_EnDisableCE(struct amd_softc *amd, int mode, int *regval);
138 static void amd_EEpromOutDI(struct amd_softc *amd, int *regval, int Carry);
139 static void amd_Prepare(struct amd_softc *amd, int *regval, u_int8_t EEpromCmd);
140 static void amd_ReadEEprom(struct amd_softc *amd);
142 static int amd_probe(device_t);
143 static int amd_attach(device_t);
144 static void amdcompletematch(struct amd_softc *amd, target_id_t target,
145 lun_id_t lun, u_int tag, struct srb_queue *queue,
147 static void amdsetsync(struct amd_softc *amd, u_int target, u_int clockrate,
148 u_int period, u_int offset, u_int type);
149 static void amdsettags(struct amd_softc *amd, u_int target, int tagenb);
151 static __inline void amd_clear_msg_state(struct amd_softc *amd);
154 amd_clear_msg_state(struct amd_softc *amd)
157 amd->msgout_index = 0;
158 amd->msgin_index = 0;
161 /* CAM SIM entry points */
162 #define ccb_srb_ptr spriv_ptr0
163 #define ccb_amd_ptr spriv_ptr1
164 static void amd_action(struct cam_sim *sim, union ccb *ccb);
165 static void amd_poll(struct cam_sim *sim);
168 * State engine function tables indexed by SCSI phase number
170 phase_handler_func_t amd_SCSI_phase0[] = {
181 phase_handler_func_t amd_SCSI_phase1[] = {
193 * EEProm/BIOS negotiation periods
195 u_int8_t eeprom_period[] = {
207 * chip clock setting to SCSI specified sync parameter table.
209 u_int8_t tinfo_sync_period[] = {
222 static __inline struct amd_srb *
223 amdgetsrb(struct amd_softc * amd)
226 struct amd_srb * pSRB;
229 pSRB = TAILQ_FIRST(&amd->free_srbs);
231 TAILQ_REMOVE(&amd->free_srbs, pSRB, links);
237 amdsetupcommand(struct amd_softc *amd, struct amd_srb *srb)
239 struct scsi_request_sense sense_cmd;
240 struct ccb_scsiio *csio;
244 csio = &srb->pccb->csio;
246 if (srb->SRBFlag & AUTO_REQSENSE) {
247 sense_cmd.opcode = REQUEST_SENSE;
248 sense_cmd.byte2 = srb->pccb->ccb_h.target_lun << 5;
249 sense_cmd.unused[0] = 0;
250 sense_cmd.unused[1] = 0;
251 sense_cmd.length = csio->sense_len;
252 sense_cmd.control = 0;
253 cdb = &sense_cmd.opcode;
254 cdb_len = sizeof(sense_cmd);
256 cdb = &srb->CmdBlock[0];
257 cdb_len = srb->ScsiCmdLen;
259 amd_write8_multi(amd, SCSIFIFOREG, cdb, cdb_len);
263 * Attempt to start a waiting transaction. Interrupts must be disabled
264 * upon entry to this function.
267 amdrunwaiting(struct amd_softc *amd) {
270 if (amd->last_phase != SCSI_BUS_FREE)
273 srb = TAILQ_FIRST(&amd->waiting_srbs);
277 if (amdstart(amd, srb) == 0) {
278 TAILQ_REMOVE(&amd->waiting_srbs, srb, links);
279 TAILQ_INSERT_HEAD(&amd->running_srbs, srb, links);
284 amdexecutesrb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
288 struct amd_softc *amd;
291 srb = (struct amd_srb *)arg;
293 amd = (struct amd_softc *)ccb->ccb_h.ccb_amd_ptr;
297 printf("amd%d: Unexepected error 0x%x returned from "
298 "bus_dmamap_load\n", amd->unit, error);
299 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
300 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
301 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
303 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
310 bus_dma_segment_t *end_seg;
313 end_seg = dm_segs + nseg;
315 /* Copy the segments into our SG list */
316 srb->pSGlist = &srb->SGsegment[0];
318 while (dm_segs < end_seg) {
319 sg->SGXLen = dm_segs->ds_len;
320 sg->SGXPtr = dm_segs->ds_addr;
325 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
326 op = BUS_DMASYNC_PREREAD;
328 op = BUS_DMASYNC_PREWRITE;
330 bus_dmamap_sync(amd->buffer_dmat, srb->dmamap, op);
335 srb->AdaptStatus = 0;
336 srb->TargetStatus = 0;
341 srb->TotalXferredLen = 0;
343 srb->SGToBeXferLen = 0;
349 * Last time we need to check if this CCB needs to
352 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
354 bus_dmamap_unload(amd->buffer_dmat, srb->dmamap);
355 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
360 ccb->ccb_h.status |= CAM_SIM_QUEUED;
362 /* XXX Need a timeout handler */
363 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
366 TAILQ_INSERT_TAIL(&amd->waiting_srbs, srb, links);
372 amd_action(struct cam_sim * psim, union ccb * pccb)
374 struct amd_softc * amd;
375 u_int target_id, target_lun;
377 CAM_DEBUG(pccb->ccb_h.path, CAM_DEBUG_TRACE, ("amd_action\n"));
379 amd = (struct amd_softc *) cam_sim_softc(psim);
380 target_id = pccb->ccb_h.target_id;
381 target_lun = pccb->ccb_h.target_lun;
383 switch (pccb->ccb_h.func_code) {
386 struct amd_srb * pSRB;
387 struct ccb_scsiio *pcsio;
392 * Assign an SRB and connect it with this ccb.
394 pSRB = amdgetsrb(amd);
398 pccb->ccb_h.status = CAM_RESRC_UNAVAIL;
403 pccb->ccb_h.ccb_srb_ptr = pSRB;
404 pccb->ccb_h.ccb_amd_ptr = amd;
405 pSRB->ScsiCmdLen = pcsio->cdb_len;
406 bcopy(pcsio->cdb_io.cdb_bytes, pSRB->CmdBlock, pcsio->cdb_len);
407 if ((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
408 if ((pccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
410 * We've been given a pointer
411 * to a single buffer.
413 if ((pccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
419 bus_dmamap_load(amd->buffer_dmat,
425 if (error == EINPROGRESS) {
428 * ordering, freeze the
430 * until our mapping is
433 xpt_freeze_simq(amd->psim, 1);
434 pccb->ccb_h.status |=
439 struct bus_dma_segment seg;
441 /* Pointer to physical buffer */
443 (bus_addr_t)pcsio->data_ptr;
444 seg.ds_len = pcsio->dxfer_len;
445 amdexecutesrb(pSRB, &seg, 1, 0);
448 struct bus_dma_segment *segs;
450 if ((pccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0
451 || (pccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
452 TAILQ_INSERT_HEAD(&amd->free_srbs,
454 pccb->ccb_h.status = CAM_PROVIDE_FAIL;
459 /* Just use the segments provided */
461 (struct bus_dma_segment *)pcsio->data_ptr;
462 amdexecutesrb(pSRB, segs, pcsio->sglist_cnt, 0);
465 amdexecutesrb(pSRB, NULL, 0, 0);
470 struct ccb_pathinq *cpi = &pccb->cpi;
472 cpi->version_num = 1;
473 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
474 cpi->target_sprt = 0;
476 cpi->hba_eng_cnt = 0;
478 cpi->max_lun = amd->max_lun; /* 7 or 0 */
479 cpi->initiator_id = amd->AdaptSCSIID;
480 cpi->bus_id = cam_sim_bus(psim);
481 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
482 strncpy(cpi->hba_vid, "TRM-AMD", HBA_IDLEN);
483 strncpy(cpi->dev_name, cam_sim_name(psim), DEV_IDLEN);
484 cpi->unit_number = cam_sim_unit(psim);
485 cpi->ccb_h.status = CAM_REQ_CMP;
490 pccb->ccb_h.status = CAM_REQ_INVALID;
498 amd_ResetSCSIBus(amd);
501 for (i = 0; i < 500; i++) {
502 DELAY(1000); /* Wait until our interrupt
506 pccb->ccb_h.status = CAM_REQ_CMP;
511 pccb->ccb_h.status = CAM_REQ_INVALID;
515 pccb->ccb_h.status = CAM_REQ_INVALID;
517 case XPT_GET_TRAN_SETTINGS:
519 struct ccb_trans_settings *cts;
520 struct amd_target_info *targ_info;
521 struct amd_transinfo *tinfo;
526 targ_info = &amd->tinfo[target_id];
527 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
528 /* current transfer settings */
529 if (targ_info->disc_tag & AMD_CUR_DISCENB) {
530 cts->flags = CCB_TRANS_DISC_ENB;
532 cts->flags = 0; /* no tag & disconnect */
534 if (targ_info->disc_tag & AMD_CUR_TAGENB) {
535 cts->flags |= CCB_TRANS_TAG_ENB;
537 tinfo = &targ_info->current;
539 /* default(user) transfer settings */
540 if (targ_info->disc_tag & AMD_USR_DISCENB) {
541 cts->flags = CCB_TRANS_DISC_ENB;
545 if (targ_info->disc_tag & AMD_USR_TAGENB) {
546 cts->flags |= CCB_TRANS_TAG_ENB;
548 tinfo = &targ_info->user;
551 cts->sync_period = tinfo->period;
552 cts->sync_offset = tinfo->offset;
553 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
555 cts->valid = CCB_TRANS_SYNC_RATE_VALID
556 | CCB_TRANS_SYNC_OFFSET_VALID
557 | CCB_TRANS_BUS_WIDTH_VALID
558 | CCB_TRANS_DISC_VALID
559 | CCB_TRANS_TQ_VALID;
560 pccb->ccb_h.status = CAM_REQ_CMP;
564 case XPT_SET_TRAN_SETTINGS:
566 struct ccb_trans_settings *cts;
567 struct amd_target_info *targ_info;
574 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) {
575 update_type |= AMD_TRANS_GOAL;
576 } else if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
577 update_type |= AMD_TRANS_USER;
580 || update_type == (AMD_TRANS_USER|AMD_TRANS_GOAL)) {
581 cts->ccb_h.status = CAM_REQ_INVALID;
586 targ_info = &amd->tinfo[target_id];
588 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
589 if (update_type & AMD_TRANS_GOAL) {
590 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
591 targ_info->disc_tag |= AMD_CUR_DISCENB;
593 targ_info->disc_tag &= ~AMD_CUR_DISCENB;
596 if (update_type & AMD_TRANS_USER) {
597 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) {
598 targ_info->disc_tag |= AMD_USR_DISCENB;
600 targ_info->disc_tag &= ~AMD_USR_DISCENB;
604 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
605 if (update_type & AMD_TRANS_GOAL) {
606 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
607 targ_info->disc_tag |= AMD_CUR_TAGENB;
609 targ_info->disc_tag &= ~AMD_CUR_TAGENB;
612 if (update_type & AMD_TRANS_USER) {
613 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
614 targ_info->disc_tag |= AMD_USR_TAGENB;
616 targ_info->disc_tag &= ~AMD_USR_TAGENB;
621 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0) {
622 if (update_type & AMD_TRANS_GOAL)
623 cts->sync_offset = targ_info->goal.offset;
625 cts->sync_offset = targ_info->user.offset;
628 if (cts->sync_offset > AMD_MAX_SYNC_OFFSET)
629 cts->sync_offset = AMD_MAX_SYNC_OFFSET;
631 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0) {
632 if (update_type & AMD_TRANS_GOAL)
633 cts->sync_period = targ_info->goal.period;
635 cts->sync_period = targ_info->user.period;
638 last_entry = sizeof(tinfo_sync_period) - 1;
639 if ((cts->sync_period != 0)
640 && (cts->sync_period < tinfo_sync_period[0]))
641 cts->sync_period = tinfo_sync_period[0];
642 if (cts->sync_period > tinfo_sync_period[last_entry])
643 cts->sync_period = 0;
644 if (cts->sync_offset == 0)
645 cts->sync_period = 0;
647 if ((update_type & AMD_TRANS_USER) != 0) {
648 targ_info->user.period = cts->sync_period;
649 targ_info->user.offset = cts->sync_offset;
651 if ((update_type & AMD_TRANS_GOAL) != 0) {
652 targ_info->goal.period = cts->sync_period;
653 targ_info->goal.offset = cts->sync_offset;
656 pccb->ccb_h.status = CAM_REQ_CMP;
660 case XPT_CALC_GEOMETRY:
662 struct ccb_calc_geometry *ccg;
664 u_int32_t secs_per_cylinder;
668 size_mb = ccg->volume_size/((1024L * 1024L)/ccg->block_size);
669 extended = (amd->eepromBuf[EE_MODE2] & GREATER_1G) != 0;
671 if (size_mb > 1024 && extended) {
673 ccg->secs_per_track = 63;
676 ccg->secs_per_track = 32;
678 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
679 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
680 pccb->ccb_h.status = CAM_REQ_CMP;
685 pccb->ccb_h.status = CAM_REQ_INVALID;
692 amd_poll(struct cam_sim * psim)
694 amd_intr(cam_sim_softc(psim));
698 phystovirt(struct amd_srb * pSRB, u_int32_t xferCnt)
701 struct ccb_scsiio *pcsio;
703 struct amd_sg * pseg;
706 pcsio = &pSRB->pccb->csio;
708 dataPtr = (int) pcsio->data_ptr;
709 pseg = pSRB->SGsegment;
710 for (i = 0; i < pSRB->SGIndex; i++) {
711 dataPtr += (int) pseg->SGXLen;
714 dataPtr += (int) xferCnt;
715 return ((u_int8_t *) dataPtr);
719 ResetDevParam(struct amd_softc * amd)
723 for (target = 0; target <= amd->max_id; target++) {
724 if (amd->AdaptSCSIID != target) {
725 amdsetsync(amd, target, /*clockrate*/0,
726 /*period*/0, /*offset*/0, AMD_TRANS_CUR);
732 amdcompletematch(struct amd_softc *amd, target_id_t target, lun_id_t lun,
733 u_int tag, struct srb_queue *queue, cam_status status)
736 struct amd_srb *next_srb;
738 for (srb = TAILQ_FIRST(queue); srb != NULL; srb = next_srb) {
741 next_srb = TAILQ_NEXT(srb, links);
742 if (srb->pccb->ccb_h.target_id != target
743 && target != CAM_TARGET_WILDCARD)
746 if (srb->pccb->ccb_h.target_lun != lun
747 && lun != CAM_LUN_WILDCARD)
750 if (srb->TagNumber != tag
751 && tag != AMD_TAG_WILDCARD)
755 TAILQ_REMOVE(queue, srb, links);
756 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
757 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0
758 && (status & CAM_DEV_QFRZN) != 0)
759 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
760 ccb->ccb_h.status = status;
767 amdsetsync(struct amd_softc *amd, u_int target, u_int clockrate,
768 u_int period, u_int offset, u_int type)
770 struct amd_target_info *tinfo;
774 tinfo = &amd->tinfo[target];
775 old_period = tinfo->current.period;
776 old_offset = tinfo->current.offset;
777 if ((type & AMD_TRANS_CUR) != 0
778 && (old_period != period || old_offset != offset)) {
779 struct cam_path *path;
781 tinfo->current.period = period;
782 tinfo->current.offset = offset;
783 tinfo->sync_period_reg = clockrate;
784 tinfo->sync_offset_reg = offset;
785 tinfo->CtrlR3 &= ~FAST_SCSI;
786 tinfo->CtrlR4 &= ~EATER_25NS;
788 tinfo->CtrlR4 |= EATER_25NS;
790 tinfo->CtrlR3 |= FAST_SCSI;
792 if ((type & AMD_TRANS_ACTIVE) == AMD_TRANS_ACTIVE) {
793 amd_write8(amd, SYNCPERIOREG, tinfo->sync_period_reg);
794 amd_write8(amd, SYNCOFFREG, tinfo->sync_offset_reg);
795 amd_write8(amd, CNTLREG3, tinfo->CtrlR3);
796 amd_write8(amd, CNTLREG4, tinfo->CtrlR4);
798 /* If possible, update the XPT's notion of our transfer rate */
799 if (xpt_create_path(&path, /*periph*/NULL,
800 cam_sim_path(amd->psim), target,
801 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
802 struct ccb_trans_settings neg;
804 xpt_setup_ccb(&neg.ccb_h, path, /*priority*/1);
805 neg.sync_period = period;
806 neg.sync_offset = offset;
807 neg.valid = CCB_TRANS_SYNC_RATE_VALID
808 | CCB_TRANS_SYNC_OFFSET_VALID;
809 xpt_async(AC_TRANSFER_NEG, path, &neg);
813 if ((type & AMD_TRANS_GOAL) != 0) {
814 tinfo->goal.period = period;
815 tinfo->goal.offset = offset;
818 if ((type & AMD_TRANS_USER) != 0) {
819 tinfo->user.period = period;
820 tinfo->user.offset = offset;
825 amdsettags(struct amd_softc *amd, u_int target, int tagenb)
827 panic("Implement me!\n");
833 **********************************************************************
834 * Function : amd_reset (struct amd_softc * amd)
835 * Purpose : perform a hard reset on the SCSI bus( and AMD chip).
836 * Inputs : cmd - command which caused the SCSI RESET
837 **********************************************************************
840 amd_reset(struct amd_softc * amd)
848 printf("DC390: RESET");
852 bval = amd_read8(amd, CNTLREG1);
853 bval |= DIS_INT_ON_SCSI_RST;
854 amd_write8(amd, CNTLREG1, bval); /* disable interrupt */
855 amd_ResetSCSIBus(amd);
857 for (i = 0; i < 500; i++) {
861 bval = amd_read8(amd, CNTLREG1);
862 bval &= ~DIS_INT_ON_SCSI_RST;
863 amd_write8(amd, CNTLREG1, bval); /* re-enable interrupt */
865 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
866 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
869 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
870 AMD_TAG_WILDCARD, &amd->running_srbs,
871 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
872 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
873 AMD_TAG_WILDCARD, &amd->waiting_srbs,
874 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
875 amd->active_srb = NULL;
882 amd_timeout(void *arg1)
884 struct amd_srb * pSRB;
886 pSRB = (struct amd_srb *) arg1;
891 amdstart(struct amd_softc *amd, struct amd_srb *pSRB)
894 struct ccb_scsiio *pcsio;
895 struct amd_target_info *targ_info;
904 target = pccb->ccb_h.target_id;
905 lun = pccb->ccb_h.target_lun;
906 targ_info = &amd->tinfo[target];
908 amd_clear_msg_state(amd);
909 amd_write8(amd, SCSIDESTIDREG, target);
910 amd_write8(amd, SYNCPERIOREG, targ_info->sync_period_reg);
911 amd_write8(amd, SYNCOFFREG, targ_info->sync_offset_reg);
912 amd_write8(amd, CNTLREG1, targ_info->CtrlR1);
913 amd_write8(amd, CNTLREG3, targ_info->CtrlR3);
914 amd_write8(amd, CNTLREG4, targ_info->CtrlR4);
915 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
917 identify_msg = MSG_IDENTIFYFLAG | lun;
918 if ((targ_info->disc_tag & AMD_CUR_DISCENB) != 0
919 && (pccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0
920 && (pSRB->CmdBlock[0] != REQUEST_SENSE)
921 && (pSRB->SRBFlag & AUTO_REQSENSE) == 0)
922 identify_msg |= MSG_IDENTIFY_DISCFLAG;
924 amd_write8(amd, SCSIFIFOREG, identify_msg);
926 if ((targ_info->disc_tag & AMD_CUR_TAGENB) == 0
927 || (identify_msg & MSG_IDENTIFY_DISCFLAG) == 0)
928 pccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
929 if (targ_info->current.period != targ_info->goal.period
930 || targ_info->current.offset != targ_info->goal.offset) {
931 command = SEL_W_ATN_STOP;
932 amdconstructsdtr(amd, targ_info->goal.period,
933 targ_info->goal.offset);
934 } else if ((pccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
935 command = SEL_W_ATN2;
936 pSRB->SRBState = SRB_START;
937 amd_write8(amd, SCSIFIFOREG, pcsio->tag_action);
938 amd_write8(amd, SCSIFIFOREG, pSRB->TagNumber);
942 pSRB->SRBState = SRB_START;
944 if (command != SEL_W_ATN_STOP)
945 amdsetupcommand(amd, pSRB);
947 if (amd_read8(amd, SCSISTATREG) & INTERRUPT) {
948 pSRB->SRBState = SRB_READY;
951 amd->last_phase = SCSI_ARBITRATING;
952 amd_write8(amd, SCSICMDREG, command);
953 amd->active_srb = pSRB;
954 amd->cur_target = target;
961 * Catch an interrupt from the adapter.
962 * Process pending device interrupts.
967 struct amd_softc *amd;
968 struct amd_srb *pSRB;
969 u_int internstat = 0;
973 amd = (struct amd_softc *)arg;
977 printf("amd_intr: amd NULL return......");
982 scsistat = amd_read8(amd, SCSISTATREG);
983 if (!(scsistat & INTERRUPT)) {
985 printf("amd_intr: scsistat = NULL ,return......");
989 #ifdef AMD_DEBUG_SCSI_PHASE
990 printf("scsistat=%2x,", scsistat);
993 internstat = amd_read8(amd, INTERNSTATREG);
994 intstat = amd_read8(amd, INTSTATREG);
996 #ifdef AMD_DEBUG_SCSI_PHASE
997 printf("intstat=%2x,", intstat);
1000 if (intstat & DISCONNECTED) {
1001 amd_Disconnect(amd);
1004 if (intstat & RESELECTED) {
1008 if (intstat & INVALID_CMD) {
1009 amd_InvalidCmd(amd);
1012 if (intstat & SCSI_RESET_) {
1013 amd_ScsiRstDetect(amd);
1016 if (intstat & (SUCCESSFUL_OP + SERVICE_REQUEST)) {
1017 pSRB = amd->active_srb;
1019 * Run our state engine. First perform
1020 * post processing for the last phase we
1021 * were in, followed by any processing
1022 * required to handle the current phase.
1025 amd_SCSI_phase0[amd->last_phase](amd, pSRB, scsistat);
1026 amd->last_phase = scsistat & SCSI_PHASE_MASK;
1027 (void)amd_SCSI_phase1[amd->last_phase](amd, pSRB, scsistat);
1032 amd_DataOutPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1034 struct amd_sg *psgl;
1035 u_int32_t ResidCnt, xferCnt;
1037 if (!(pSRB->SRBState & SRB_XFERPAD)) {
1038 if (scsistat & PARITY_ERR) {
1039 pSRB->SRBStatus |= PARITY_ERROR;
1041 if (scsistat & COUNT_2_ZERO) {
1042 while ((amd_read8(amd, DMA_Status)&DMA_XFER_DONE) == 0)
1044 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
1046 if (pSRB->SGIndex < pSRB->SGcount) {
1048 psgl = pSRB->pSGlist;
1049 pSRB->SGPhysAddr = psgl->SGXPtr;
1050 pSRB->SGToBeXferLen = psgl->SGXLen;
1052 pSRB->SGToBeXferLen = 0;
1055 ResidCnt = amd_read8(amd, CURRENTFIFOREG) & 0x1f;
1056 ResidCnt += amd_read8(amd, CTCREG_LOW)
1057 | (amd_read8(amd, CTCREG_MID) << 8)
1058 | (amd_read8(amd, CURTXTCNTREG) << 16);
1060 xferCnt = pSRB->SGToBeXferLen - ResidCnt;
1061 pSRB->SGPhysAddr += xferCnt;
1062 pSRB->TotalXferredLen += xferCnt;
1063 pSRB->SGToBeXferLen = ResidCnt;
1066 amd_write8(amd, DMA_Cmd, WRITE_DIRECTION | DMA_IDLE_CMD);
1071 amd_DataInPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1074 u_int16_t i, residual;
1075 struct amd_sg *psgl;
1076 u_int32_t ResidCnt, xferCnt;
1079 if (!(pSRB->SRBState & SRB_XFERPAD)) {
1080 if (scsistat & PARITY_ERR) {
1081 pSRB->SRBStatus |= PARITY_ERROR;
1083 if (scsistat & COUNT_2_ZERO) {
1085 bval = amd_read8(amd, DMA_Status);
1086 if ((bval & DMA_XFER_DONE) != 0)
1089 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_IDLE_CMD);
1091 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
1093 if (pSRB->SGIndex < pSRB->SGcount) {
1095 psgl = pSRB->pSGlist;
1096 pSRB->SGPhysAddr = psgl->SGXPtr;
1097 pSRB->SGToBeXferLen = psgl->SGXLen;
1099 pSRB->SGToBeXferLen = 0;
1101 } else { /* phase changed */
1103 bval = amd_read8(amd, CURRENTFIFOREG);
1104 while (bval & 0x1f) {
1105 if ((bval & 0x1f) == 1) {
1106 for (i = 0; i < 0x100; i++) {
1107 bval = amd_read8(amd, CURRENTFIFOREG);
1108 if (!(bval & 0x1f)) {
1110 } else if (i == 0x0ff) {
1116 bval = amd_read8(amd, CURRENTFIFOREG);
1120 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_BLAST_CMD);
1121 for (i = 0; i < 0x8000; i++) {
1122 if ((amd_read8(amd, DMA_Status)&BLAST_COMPLETE))
1125 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_IDLE_CMD);
1127 ResidCnt = amd_read8(amd, CTCREG_LOW)
1128 | (amd_read8(amd, CTCREG_MID) << 8)
1129 | (amd_read8(amd, CURTXTCNTREG) << 16);
1130 xferCnt = pSRB->SGToBeXferLen - ResidCnt;
1131 pSRB->SGPhysAddr += xferCnt;
1132 pSRB->TotalXferredLen += xferCnt;
1133 pSRB->SGToBeXferLen = ResidCnt;
1135 /* get residual byte */
1136 bval = amd_read8(amd, SCSIFIFOREG);
1137 ptr = phystovirt(pSRB, xferCnt);
1140 pSRB->TotalXferredLen++;
1141 pSRB->SGToBeXferLen--;
1149 amd_StatusPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1151 pSRB->TargetStatus = amd_read8(amd, SCSIFIFOREG);
1153 pSRB->EndMessage = amd_read8(amd, SCSIFIFOREG);
1154 pSRB->SRBState = SRB_COMPLETED;
1155 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1160 amd_MsgOutPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1162 if (pSRB->SRBState & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT)) {
1163 scsistat = SCSI_NOP0;
1169 amd_MsgInPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1173 amd->msgin_buf[amd->msgin_index] = amd_read8(amd, SCSIFIFOREG);
1175 done = amdparsemsg(amd);
1177 amd->msgin_index = 0;
1184 amdparsemsg(struct amd_softc *amd)
1186 struct amd_target_info *targ_info;
1195 targ_info = &amd->tinfo[amd->cur_target];
1198 * Parse as much of the message as is availible,
1199 * rejecting it if we don't support it. When
1200 * the entire message is availible and has been
1201 * handled, return TRUE indicating that we have
1202 * parsed an entire message.
1204 switch (amd->msgin_buf[0]) {
1205 case MSG_DISCONNECT:
1206 amd->active_srb->SRBState = SRB_DISCONNECT;
1207 amd->disc_count[amd->cur_target][amd->cur_lun]++;
1210 case MSG_SIMPLE_Q_TAG:
1212 struct amd_srb *disc_srb;
1214 if (amd->msgin_index < 1)
1216 disc_srb = &amd->SRB_array[amd->msgin_buf[1]];
1217 if (amd->active_srb != NULL
1218 || disc_srb->SRBState != SRB_DISCONNECT
1219 || disc_srb->pccb->ccb_h.target_id != amd->cur_target
1220 || disc_srb->pccb->ccb_h.target_lun != amd->cur_lun) {
1221 printf("amd%d: Unexpected tagged reselection "
1222 "for target %d, Issuing Abort\n", amd->unit,
1224 amd->msgout_buf[0] = MSG_ABORT;
1225 amd->msgout_len = 1;
1229 amd->active_srb = disc_srb;
1230 amd->disc_count[amd->cur_target][amd->cur_lun]--;
1234 case MSG_MESSAGE_REJECT:
1235 response = amdhandlemsgreject(amd);
1236 if (response == FALSE)
1237 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1249 /* Wait for enough of the message to begin validation */
1250 if (amd->msgin_index < 1)
1252 if (amd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
1257 /* Wait for opcode */
1258 if (amd->msgin_index < 2)
1261 if (amd->msgin_buf[2] != MSG_EXT_SDTR) {
1267 * Wait until we have both args before validating
1268 * and acting on this message.
1270 * Add one to MSG_EXT_SDTR_LEN to account for
1271 * the extended message preamble.
1273 if (amd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
1276 period = amd->msgin_buf[3];
1277 saved_offset = offset = amd->msgin_buf[4];
1278 clockrate = amdfindclockrate(amd, &period);
1279 if (offset > AMD_MAX_SYNC_OFFSET)
1280 offset = AMD_MAX_SYNC_OFFSET;
1281 if (period == 0 || offset == 0) {
1286 amdsetsync(amd, amd->cur_target, clockrate, period, offset,
1287 AMD_TRANS_ACTIVE|AMD_TRANS_GOAL);
1290 * See if we initiated Sync Negotiation
1291 * and didn't have to fall down to async
1294 if (amdsentmsg(amd, MSG_EXT_SDTR, /*full*/TRUE)) {
1296 if (saved_offset != offset) {
1297 /* Went too low - force async */
1302 * Send our own SDTR in reply
1305 printf("Sending SDTR!\n");
1306 amd->msgout_index = 0;
1307 amd->msgout_len = 0;
1308 amdconstructsdtr(amd, period, offset);
1309 amd->msgout_index = 0;
1315 case MSG_SAVEDATAPOINTER:
1316 case MSG_RESTOREPOINTERS:
1317 /* XXX Implement!!! */
1326 amd->msgout_index = 0;
1327 amd->msgout_len = 1;
1328 amd->msgout_buf[0] = MSG_MESSAGE_REJECT;
1334 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1336 if (done && !response)
1337 /* Clear the outgoing message buffer */
1338 amd->msgout_len = 0;
1341 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1347 amdfindclockrate(struct amd_softc *amd, u_int *period)
1352 for (i = 0; i < sizeof(tinfo_sync_period); i++) {
1353 u_int8_t *table_entry;
1355 table_entry = &tinfo_sync_period[i];
1356 if (*period <= *table_entry) {
1358 * When responding to a target that requests
1359 * sync, the requested rate may fall between
1360 * two rates that we can output, but still be
1361 * a rate that we can receive. Because of this,
1362 * we want to respond to the target with
1363 * the same rate that it sent to us even
1364 * if the period we use to send data to it
1365 * is lower. Only lower the response period
1369 *period = *table_entry;
1375 if (i == sizeof(tinfo_sync_period)) {
1376 /* Too slow for us. Use asnyc transfers. */
1386 * See if we sent a particular extended message to the target.
1387 * If "full" is true, the target saw the full message.
1388 * If "full" is false, the target saw at least the first
1389 * byte of the message.
1392 amdsentmsg(struct amd_softc *amd, u_int msgtype, int full)
1400 while (index < amd->msgout_len) {
1401 if ((amd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
1402 || amd->msgout_buf[index] == MSG_MESSAGE_REJECT)
1404 else if (amd->msgout_buf[index] >= MSG_SIMPLE_Q_TAG
1405 && amd->msgout_buf[index] < MSG_IGN_WIDE_RESIDUE) {
1406 /* Skip tag type and tag id */
1408 } else if (amd->msgout_buf[index] == MSG_EXTENDED) {
1409 /* Found a candidate */
1410 if (amd->msgout_buf[index+2] == msgtype) {
1413 end_index = index + 1
1414 + amd->msgout_buf[index + 1];
1416 if (amd->msgout_index > end_index)
1418 } else if (amd->msgout_index > index)
1423 panic("amdsentmsg: Inconsistent msg buffer");
1430 amdconstructsdtr(struct amd_softc *amd, u_int period, u_int offset)
1432 amd->msgout_buf[amd->msgout_index++] = MSG_EXTENDED;
1433 amd->msgout_buf[amd->msgout_index++] = MSG_EXT_SDTR_LEN;
1434 amd->msgout_buf[amd->msgout_index++] = MSG_EXT_SDTR;
1435 amd->msgout_buf[amd->msgout_index++] = period;
1436 amd->msgout_buf[amd->msgout_index++] = offset;
1437 amd->msgout_len += 5;
1441 amdhandlemsgreject(struct amd_softc *amd)
1444 * If we had an outstanding SDTR for this
1445 * target, this is a signal that the target
1446 * is refusing negotiation. Also watch out
1447 * for rejected tag messages.
1449 struct amd_srb *srb;
1450 struct amd_target_info *targ_info;
1451 int response = FALSE;
1453 srb = amd->active_srb;
1454 targ_info = &amd->tinfo[amd->cur_target];
1455 if (amdsentmsg(amd, MSG_EXT_SDTR, /*full*/FALSE)) {
1456 /* note asynch xfers and clear flag */
1457 amdsetsync(amd, amd->cur_target, /*clockrate*/0,
1458 /*period*/0, /*offset*/0,
1459 AMD_TRANS_ACTIVE|AMD_TRANS_GOAL);
1460 printf("amd%d:%d: refuses synchronous negotiation. "
1461 "Using asynchronous transfers\n",
1462 amd->unit, amd->cur_target);
1463 } else if ((srb != NULL)
1464 && (srb->pccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
1465 struct ccb_trans_settings neg;
1467 printf("amd%d:%d: refuses tagged commands. Performing "
1468 "non-tagged I/O\n", amd->unit, amd->cur_target);
1470 amdsettags(amd, amd->cur_target, FALSE);
1472 neg.valid = CCB_TRANS_TQ_VALID;
1473 xpt_setup_ccb(&neg.ccb_h, srb->pccb->ccb_h.path, /*priority*/1);
1474 xpt_async(AC_TRANSFER_NEG, srb->pccb->ccb_h.path, &neg);
1477 * Resend the identify for this CCB as the target
1478 * may believe that the selection is invalid otherwise.
1480 if (amd->msgout_len != 0)
1481 bcopy(&amd->msgout_buf[0], &amd->msgout_buf[1],
1483 amd->msgout_buf[0] = MSG_IDENTIFYFLAG
1484 | srb->pccb->ccb_h.target_lun;
1486 if ((targ_info->disc_tag & AMD_CUR_DISCENB) != 0
1487 && (srb->pccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1488 amd->msgout_buf[0] |= MSG_IDENTIFY_DISCFLAG;
1490 srb->pccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
1493 * Requeue all tagged commands for this target
1494 * currently in our posession so they can be
1495 * converted to untagged commands.
1497 amdcompletematch(amd, amd->cur_target, amd->cur_lun,
1498 AMD_TAG_WILDCARD, &amd->waiting_srbs,
1499 CAM_DEV_QFRZN|CAM_REQUEUE_REQ);
1502 * Otherwise, we ignore it.
1504 printf("amd%d:%d: Message reject received -- ignored\n",
1505 amd->unit, amd->cur_target);
1511 if (!(pSRB->SRBState & SRB_MSGIN_MULTI)) {
1512 if (bval == MSG_DISCONNECT) {
1513 pSRB->SRBState = SRB_DISCONNECT;
1514 } else if (bval == MSG_SAVEDATAPOINTER) {
1516 } else if ((bval == MSG_EXTENDED)
1517 || ((bval >= MSG_SIMPLE_Q_TAG)
1518 && (bval <= MSG_ORDERED_Q_TAG))) {
1519 pSRB->SRBState |= SRB_MSGIN_MULTI;
1520 pSRB->MsgInBuf[0] = bval;
1522 pSRB->pMsgPtr = &pSRB->MsgInBuf[1];
1523 } else if (bval == MSG_MESSAGE_REJECT) {
1524 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1526 if (pSRB->SRBState & DO_SYNC_NEGO) {
1529 } else if (bval == MSG_RESTOREPOINTERS) {
1534 } else { /* minx: */
1535 *pSRB->pMsgPtr = bval;
1538 if ((pSRB->MsgInBuf[0] >= MSG_SIMPLE_Q_TAG)
1539 && (pSRB->MsgInBuf[0] <= MSG_ORDERED_Q_TAG)) {
1540 if (pSRB->MsgCnt == 2) {
1542 pSRB = &amd->SRB_array[pSRB->MsgInBuf[1]];
1543 if (pSRB->SRBState & SRB_DISCONNECT) == 0) {
1544 pSRB = amd->pTmpSRB;
1545 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1546 pDCB->pActiveSRB = pSRB;
1547 pSRB->MsgOutBuf[0] = MSG_ABORT_TAG;
1548 EnableMsgOut2(amd, pSRB);
1550 if (pDCB->DCBFlag & ABORT_DEV_) {
1551 pSRB->SRBState = SRB_ABORT_SENT;
1552 EnableMsgOut1(amd, pSRB);
1554 pDCB->pActiveSRB = pSRB;
1555 pSRB->SRBState = SRB_DATA_XFER;
1558 } else if ((pSRB->MsgInBuf[0] == MSG_EXTENDED)
1559 && (pSRB->MsgCnt == 5)) {
1560 pSRB->SRBState &= ~(SRB_MSGIN_MULTI + DO_SYNC_NEGO);
1561 if ((pSRB->MsgInBuf[1] != 3)
1562 || (pSRB->MsgInBuf[2] != 1)) { /* reject_msg: */
1564 pSRB->MsgInBuf[0] = MSG_MESSAGE_REJECT;
1565 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1566 } else if (!(pSRB->MsgInBuf[3])
1567 || !(pSRB->MsgInBuf[4])) {
1568 set_async: /* set async */
1570 pDCB = pSRB->pSRBDCB;
1571 /* disable sync & sync nego */
1572 pDCB->SyncMode &= ~(SYNC_ENABLE|SYNC_NEGO_DONE);
1573 pDCB->SyncPeriod = 0;
1574 pDCB->SyncOffset = 0;
1576 pDCB->tinfo.goal.period = 0;
1577 pDCB->tinfo.goal.offset = 0;
1579 pDCB->tinfo.current.period = 0;
1580 pDCB->tinfo.current.offset = 0;
1581 pDCB->tinfo.current.width =
1582 MSG_EXT_WDTR_BUS_8_BIT;
1584 pDCB->CtrlR3 = FAST_CLK; /* non_fast */
1585 pDCB->CtrlR4 &= 0x3f;
1586 pDCB->CtrlR4 |= EATER_25NS;
1588 } else {/* set sync */
1590 pDCB = pSRB->pSRBDCB;
1591 /* enable sync & sync nego */
1592 pDCB->SyncMode |= SYNC_ENABLE|SYNC_NEGO_DONE;
1594 /* set sync offset */
1595 pDCB->SyncOffset &= 0x0f0;
1596 pDCB->SyncOffset |= pSRB->MsgInBuf[4];
1598 /* set sync period */
1599 pDCB->MaxNegoPeriod = pSRB->MsgInBuf[3];
1601 wval = (u_int16_t) pSRB->MsgInBuf[3];
1605 if ((wval1 * 25) != wval) {
1608 bval = FAST_CLK|FAST_SCSI;
1609 pDCB->CtrlR4 &= 0x3f;
1614 pDCB->CtrlR4 |= EATER_25NS;
1616 pDCB->CtrlR3 = bval;
1617 pDCB->SyncPeriod = (u_int8_t) wval1;
1619 pDCB->tinfo.goal.period =
1620 tinfo_sync_period[pDCB->SyncPeriod - 4];
1621 pDCB->tinfo.goal.offset = pDCB->SyncOffset;
1622 pDCB->tinfo.current.period =
1623 tinfo_sync_period[pDCB->SyncPeriod - 4];;
1624 pDCB->tinfo.current.offset = pDCB->SyncOffset;
1627 * program SCSI control register
1630 amd_write8(amd, SYNCPERIOREG, pDCB->SyncPeriod);
1631 amd_write8(amd, SYNCOFFREG, pDCB->SyncOffset);
1632 amd_write8(amd, CNTLREG3, pDCB->CtrlR3);
1633 amd_write8(amd, CNTLREG4, pDCB->CtrlR4);
1638 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1644 amd_DataOutPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1646 DataIO_Comm(amd, pSRB, WRITE_DIRECTION);
1651 amd_DataInPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1653 DataIO_Comm(amd, pSRB, READ_DIRECTION);
1658 DataIO_Comm(struct amd_softc *amd, struct amd_srb *pSRB, u_int ioDir)
1660 struct amd_sg * psgl;
1663 if (pSRB->SGIndex < pSRB->SGcount) {
1664 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD|ioDir);/* |EN_DMA_INT */
1666 if (!pSRB->SGToBeXferLen) {
1667 psgl = pSRB->pSGlist;
1668 pSRB->SGPhysAddr = psgl->SGXPtr;
1669 pSRB->SGToBeXferLen = psgl->SGXLen;
1671 lval = pSRB->SGToBeXferLen;
1672 amd_write8(amd, CTCREG_LOW, lval);
1673 amd_write8(amd, CTCREG_MID, lval >> 8);
1674 amd_write8(amd, CURTXTCNTREG, lval >> 16);
1676 amd_write32(amd, DMA_XferCnt, pSRB->SGToBeXferLen);
1678 amd_write32(amd, DMA_XferAddr, pSRB->SGPhysAddr);
1680 pSRB->SRBState = SRB_DATA_XFER;
1682 amd_write8(amd, SCSICMDREG, DMA_COMMAND|INFO_XFER_CMD);
1684 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD|ioDir); /* |EN_DMA_INT */
1686 amd_write8(amd, DMA_Cmd, DMA_START_CMD|ioDir);/* |EN_DMA_INT */
1687 } else { /* xfer pad */
1688 if (pSRB->SGcount) {
1689 pSRB->AdaptStatus = H_OVER_UNDER_RUN;
1690 pSRB->SRBStatus |= OVER_RUN;
1692 amd_write8(amd, CTCREG_LOW, 0);
1693 amd_write8(amd, CTCREG_MID, 0);
1694 amd_write8(amd, CURTXTCNTREG, 0);
1696 pSRB->SRBState |= SRB_XFERPAD;
1697 amd_write8(amd, SCSICMDREG, DMA_COMMAND|XFER_PAD_BYTE);
1702 amd_CommandPhase1(struct amd_softc *amd, struct amd_srb *srb, u_int scsistat)
1704 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1705 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1707 amdsetupcommand(amd, srb);
1709 srb->SRBState = SRB_COMMAND;
1710 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1715 amd_StatusPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1717 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1718 pSRB->SRBState = SRB_STATUS;
1719 amd_write8(amd, SCSICMDREG, INITIATOR_CMD_CMPLTE);
1724 amd_MsgOutPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1726 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1728 if (amd->msgout_len == 0) {
1729 amd->msgout_buf[0] = MSG_NOOP;
1730 amd->msgout_len = 1;
1732 amd_write8_multi(amd, SCSIFIFOREG, amd->msgout_buf, amd->msgout_len);
1733 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1738 amd_MsgInPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1740 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1741 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1746 amd_NopPhase(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1752 amd_Disconnect(struct amd_softc * amd)
1754 struct amd_srb *srb;
1758 srb = amd->active_srb;
1759 amd->active_srb = NULL;
1760 amd->last_phase = SCSI_BUS_FREE;
1761 amd_write8(amd, SCSICMDREG, EN_SEL_RESEL);
1762 target = amd->cur_target;
1766 /* Invalid reselection */
1768 } else if (srb->SRBState & SRB_ABORT_SENT) {
1769 /* Clean up and done this srb */
1771 while (( = TAILQ_FIRST(&amd->running_srbs)) != NULL) {
1772 /* XXX What about "done'ing" these srbs??? */
1773 if (pSRB->pSRBDCB == pDCB) {
1774 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
1775 TAILQ_INSERT_HEAD(&amd->free_srbs, pSRB, links);
1781 if ((srb->SRBState & (SRB_START | SRB_MSGOUT))
1782 || !(srb->SRBState & (SRB_DISCONNECT | SRB_COMPLETED))) {
1783 srb->TargetStatus = AMD_SCSI_STAT_SEL_TIMEOUT;
1785 } else if (srb->SRBState & SRB_DISCONNECT) {
1786 if (!(srb->pccb->ccb_h.flags & CAM_TAG_ACTION_VALID))
1787 amd->untagged_srbs[target][lun] = srb;
1789 } else if (srb->SRBState & SRB_COMPLETED) {
1791 srb->SRBState = SRB_FREE;
1799 amd_Reselect(struct amd_softc *amd)
1801 struct amd_target_info *tinfo;
1802 u_int16_t disc_count;
1804 amd_clear_msg_state(amd);
1805 if (amd->active_srb != NULL) {
1806 /* Requeue the SRB for our attempted Selection */
1807 TAILQ_REMOVE(&amd->running_srbs, amd->active_srb, links);
1808 TAILQ_INSERT_HEAD(&amd->waiting_srbs, amd->active_srb, links);
1809 amd->active_srb = NULL;
1812 amd->cur_target = amd_read8(amd, SCSIFIFOREG);
1813 amd->cur_target ^= amd->HostID_Bit;
1814 amd->cur_target = ffs(amd->cur_target) - 1;
1815 amd->cur_lun = amd_read8(amd, SCSIFIFOREG) & 7;
1816 tinfo = &amd->tinfo[amd->cur_target];
1817 amd->active_srb = amd->untagged_srbs[amd->cur_target][amd->cur_lun];
1818 disc_count = amd->disc_count[amd->cur_target][amd->cur_lun];
1819 if (disc_count == 0) {
1820 printf("amd%d: Unexpected reselection for target %d, "
1821 "Issuing Abort\n", amd->unit, amd->cur_target);
1822 amd->msgout_buf[0] = MSG_ABORT;
1823 amd->msgout_len = 1;
1824 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1826 if (amd->active_srb != NULL) {
1827 amd->disc_count[amd->cur_target][amd->cur_lun]--;
1828 amd->untagged_srbs[amd->cur_target][amd->cur_lun] = NULL;
1831 amd_write8(amd, SCSIDESTIDREG, amd->cur_target);
1832 amd_write8(amd, SYNCPERIOREG, tinfo->sync_period_reg);
1833 amd_write8(amd, SYNCOFFREG, tinfo->sync_offset_reg);
1834 amd_write8(amd, CNTLREG1, tinfo->CtrlR1);
1835 amd_write8(amd, CNTLREG3, tinfo->CtrlR3);
1836 amd_write8(amd, CNTLREG4, tinfo->CtrlR4);
1837 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);/* drop /ACK */
1838 amd->last_phase = SCSI_NOP0;
1842 SRBdone(struct amd_softc *amd, struct amd_srb *pSRB)
1844 u_int8_t bval, i, status;
1846 struct ccb_scsiio *pcsio;
1848 struct amd_sg *ptr2;
1850 u_int target_id, target_lun;
1853 pcsio = &pccb->csio;
1854 target_id = pSRB->pccb->ccb_h.target_id;
1855 target_lun = pSRB->pccb->ccb_h.target_lun;
1857 CAM_DEBUG(pccb->ccb_h.path, CAM_DEBUG_TRACE,
1858 ("SRBdone - TagNumber %d\n", pSRB->TagNumber));
1860 if ((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1861 bus_dmasync_op_t op;
1863 if ((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1864 op = BUS_DMASYNC_POSTREAD;
1866 op = BUS_DMASYNC_POSTWRITE;
1867 bus_dmamap_sync(amd->buffer_dmat, pSRB->dmamap, op);
1868 bus_dmamap_unload(amd->buffer_dmat, pSRB->dmamap);
1871 status = pSRB->TargetStatus;
1872 pccb->ccb_h.status = CAM_REQ_CMP;
1873 pccb->ccb_h.status = CAM_REQ_CMP;
1874 if (pSRB->SRBFlag & AUTO_REQSENSE) {
1875 pSRB->SRBFlag &= ~AUTO_REQSENSE;
1876 pSRB->AdaptStatus = 0;
1877 pSRB->TargetStatus = SCSI_STATUS_CHECK_COND;
1879 if (status == SCSI_STATUS_CHECK_COND) {
1880 pccb->ccb_h.status = CAM_SEL_TIMEOUT;
1883 *((u_int32_t *)&(pSRB->CmdBlock[0])) = pSRB->Segment0[0];
1885 pcsio->sense_resid = pcsio->sense_len
1886 - pSRB->TotalXferredLen;
1887 pSRB->TotalXferredLen = pSRB->Segment1[1];
1888 if (pSRB->TotalXferredLen) {
1890 pcsio->resid = pcsio->dxfer_len
1891 - pSRB->TotalXferredLen;
1892 /* The resid field contains valid data */
1893 /* Flush resid bytes on complete */
1895 pcsio->scsi_status = SCSI_STATUS_CHECK_COND;
1897 pccb->ccb_h.status = CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
1901 if (status == SCSI_STATUS_CHECK_COND) {
1903 if ((pSRB->SGIndex < pSRB->SGcount)
1904 && (pSRB->SGcount) && (pSRB->SGToBeXferLen)) {
1905 bval = pSRB->SGcount;
1906 swlval = pSRB->SGToBeXferLen;
1907 ptr2 = pSRB->pSGlist;
1909 for (i = pSRB->SGIndex + 1; i < bval; i++) {
1910 swlval += ptr2->SGXLen;
1914 pcsio->resid = (u_int32_t) swlval;
1917 printf("XferredLen=%8x,NotYetXferLen=%8x,",
1918 pSRB->TotalXferredLen, swlval);
1921 if ((pcsio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
1923 printf("RequestSense..................\n");
1925 RequestSense(amd, pSRB);
1928 pcsio->scsi_status = SCSI_STATUS_CHECK_COND;
1929 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1931 } else if (status == SCSI_STATUS_QUEUE_FULL) {
1932 pSRB->AdaptStatus = 0;
1933 pSRB->TargetStatus = 0;
1934 pcsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
1935 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1937 } else if (status == AMD_SCSI_STAT_SEL_TIMEOUT) {
1938 pSRB->AdaptStatus = H_SEL_TIMEOUT;
1939 pSRB->TargetStatus = 0;
1941 pcsio->scsi_status = AMD_SCSI_STAT_SEL_TIMEOUT;
1942 pccb->ccb_h.status = CAM_SEL_TIMEOUT;
1943 } else if (status == SCSI_STATUS_BUSY) {
1945 printf("DC390: target busy at %s %d\n",
1946 __FILE__, __LINE__);
1948 pcsio->scsi_status = SCSI_STATUS_BUSY;
1949 pccb->ccb_h.status = CAM_SCSI_BUSY;
1950 } else if (status == SCSI_STATUS_RESERV_CONFLICT) {
1952 printf("DC390: target reserved at %s %d\n",
1953 __FILE__, __LINE__);
1955 pcsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
1956 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; /* XXX */
1958 pSRB->AdaptStatus = 0;
1960 printf("DC390: driver stuffup at %s %d\n",
1961 __FILE__, __LINE__);
1963 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1966 status = pSRB->AdaptStatus;
1967 if (status & H_OVER_UNDER_RUN) {
1968 pSRB->TargetStatus = 0;
1970 pccb->ccb_h.status = CAM_DATA_RUN_ERR;
1971 } else if (pSRB->SRBStatus & PARITY_ERROR) {
1973 printf("DC390: driver stuffup %s %d\n",
1974 __FILE__, __LINE__);
1976 /* Driver failed to perform operation */
1977 pccb->ccb_h.status = CAM_UNCOR_PARITY;
1978 } else { /* No error */
1979 pSRB->AdaptStatus = 0;
1980 pSRB->TargetStatus = 0;
1982 /* there is no error, (sense is invalid) */
1987 if ((pccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1988 /* CAM request not yet complete =>device_Q frozen */
1989 xpt_freeze_devq(pccb->ccb_h.path, 1);
1990 pccb->ccb_h.status |= CAM_DEV_QFRZN;
1992 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
1993 TAILQ_INSERT_HEAD(&amd->free_srbs, pSRB, links);
2001 amd_ResetSCSIBus(struct amd_softc * amd)
2006 amd->ACBFlag |= RESET_DEV;
2007 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
2008 amd_write8(amd, SCSICMDREG, RST_SCSI_BUS_CMD);
2014 amd_ScsiRstDetect(struct amd_softc * amd)
2020 printf("amd_ScsiRstDetect \n");
2024 while (--wlval) { /* delay 1 sec */
2029 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
2030 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
2032 if (amd->ACBFlag & RESET_DEV) {
2033 amd->ACBFlag |= RESET_DONE;
2035 amd->ACBFlag |= RESET_DETECT;
2037 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
2038 AMD_TAG_WILDCARD, &amd->running_srbs,
2039 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
2040 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
2041 AMD_TAG_WILDCARD, &amd->waiting_srbs,
2042 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
2043 amd->active_srb = NULL;
2052 RequestSense(struct amd_softc *amd, struct amd_srb *pSRB)
2055 struct ccb_scsiio *pcsio;
2058 pcsio = &pccb->csio;
2060 pSRB->SRBFlag |= AUTO_REQSENSE;
2061 pSRB->Segment0[0] = *((u_int32_t *) & (pSRB->CmdBlock[0]));
2062 pSRB->Segment0[1] = *((u_int32_t *) & (pSRB->CmdBlock[4]));
2063 pSRB->Segment1[0] = (pSRB->ScsiCmdLen << 8) + pSRB->SGcount;
2064 pSRB->Segment1[1] = pSRB->TotalXferredLen;
2066 pSRB->AdaptStatus = 0;
2067 pSRB->TargetStatus = 0;
2069 pSRB->Segmentx.SGXPtr = (u_int32_t) vtophys(&pcsio->sense_data);
2070 pSRB->Segmentx.SGXLen = (u_int32_t) pcsio->sense_len;
2072 pSRB->pSGlist = &pSRB->Segmentx;
2076 *((u_int32_t *) & (pSRB->CmdBlock[0])) = 0x00000003;
2077 pSRB->CmdBlock[1] = pSRB->pccb->ccb_h.target_lun << 5;
2078 *((u_int16_t *) & (pSRB->CmdBlock[4])) = pcsio->sense_len;
2079 pSRB->ScsiCmdLen = 6;
2081 pSRB->TotalXferredLen = 0;
2082 pSRB->SGToBeXferLen = 0;
2083 if (amdstart(amd, pSRB) != 0) {
2084 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
2085 TAILQ_INSERT_HEAD(&amd->waiting_srbs, pSRB, links);
2090 amd_InvalidCmd(struct amd_softc * amd)
2092 struct amd_srb *srb;
2094 srb = amd->active_srb;
2095 if (srb->SRBState & (SRB_START|SRB_MSGOUT))
2096 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
2100 amd_linkSRB(struct amd_softc *amd)
2103 struct amd_srb *psrb;
2106 count = amd->SRBCount;
2108 for (i = 0; i < count; i++) {
2109 psrb = (struct amd_srb *)&amd->SRB_array[i];
2110 psrb->TagNumber = i;
2113 * Create the dmamap. This is no longer optional!
2115 error = bus_dmamap_create(amd->buffer_dmat, 0, &psrb->dmamap);
2117 device_printf(amd->dev, "Error %d creating buffer "
2118 "dmamap!\n", error);
2121 TAILQ_INSERT_TAIL(&amd->free_srbs, psrb, links);
2126 amd_EnDisableCE(struct amd_softc *amd, int mode, int *regval)
2128 if (mode == ENABLE_CE) {
2133 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2134 if (mode == DISABLE_CE) {
2135 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2141 amd_EEpromOutDI(struct amd_softc *amd, int *regval, int Carry)
2149 pci_write_config(amd->dev, *regval, bval, /*bytes*/1);
2153 pci_write_config(amd->dev, *regval, bval, /*bytes*/1);
2155 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2160 amd_EEpromInDO(struct amd_softc *amd)
2162 pci_write_config(amd->dev, 0x80, 0x80, /*bytes*/1);
2164 pci_write_config(amd->dev, 0x80, 0x40, /*bytes*/1);
2166 if (pci_read_config(amd->dev, 0, /*bytes*/1) == 0x22)
2172 EEpromGetData1(struct amd_softc *amd)
2179 for (i = 0; i < 16; i++) {
2181 carryFlag = amd_EEpromInDO(amd);
2188 amd_Prepare(struct amd_softc *amd, int *regval, u_int8_t EEpromCmd)
2195 for (i = 0; i < 9; i++) {
2196 amd_EEpromOutDI(amd, regval, carryFlag);
2197 carryFlag = (EEpromCmd & j) ? 1 : 0;
2203 amd_ReadEEprom(struct amd_softc *amd)
2210 ptr = (u_int16_t *)&amd->eepromBuf[0];
2212 for (i = 0; i < 0x40; i++) {
2213 amd_EnDisableCE(amd, ENABLE_CE, ®val);
2214 amd_Prepare(amd, ®val, cmd);
2215 *ptr = EEpromGetData1(amd);
2218 amd_EnDisableCE(amd, DISABLE_CE, ®val);
2223 amd_load_defaults(struct amd_softc *amd)
2227 bzero(&amd->eepromBuf, sizeof amd->eepromBuf);
2228 for (target = 0; target < MAX_SCSI_ID; target++)
2229 amd->eepromBuf[target << 2] =
2230 (TAG_QUEUING|EN_DISCONNECT|SYNC_NEGO|PARITY_CHK);
2231 amd->eepromBuf[EE_ADAPT_SCSI_ID] = 7;
2232 amd->eepromBuf[EE_MODE2] = ACTIVE_NEGATION|LUN_CHECK|GREATER_1G;
2233 amd->eepromBuf[EE_TAG_CMD_NUM] = 4;
2237 amd_load_eeprom_or_defaults(struct amd_softc *amd)
2239 u_int16_t wval, *ptr;
2242 amd_ReadEEprom(amd);
2244 ptr = (u_int16_t *) & amd->eepromBuf[0];
2245 for (i = 0; i < EE_DATA_SIZE; i += 2, ptr++)
2248 if (wval != EE_CHECKSUM) {
2250 printf("amd%d: SEEPROM data unavailable. "
2251 "Using default device parameters.\n",
2253 amd_load_defaults(amd);
2258 **********************************************************************
2259 * Function : static int amd_init (struct Scsi_Host *host)
2260 * Purpose : initialize the internal structures for a given SCSI host
2261 * Inputs : host - pointer to this host adapter's structure/
2262 **********************************************************************
2265 amd_init(device_t dev)
2267 struct amd_softc *amd = device_get_softc(dev);
2268 struct resource *iores;
2272 rid = PCI_BASE_ADDR0;
2273 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
2275 if (iores == NULL) {
2277 printf("amd_init: bus_alloc_resource failure!\n");
2280 amd->tag = rman_get_bustag(iores);
2281 amd->bsh = rman_get_bushandle(iores);
2283 /* DMA tag for mapping buffers into device visible space. */
2284 if (bus_dma_tag_create(/*parent_dmat*/NULL, /*alignment*/1,
2286 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2287 /*highaddr*/BUS_SPACE_MAXADDR,
2288 /*filter*/NULL, /*filterarg*/NULL,
2289 /*maxsize*/MAXBSIZE, /*nsegments*/AMD_NSEG,
2290 /*maxsegsz*/AMD_MAXTRANSFER_SIZE,
2291 /*flags*/BUS_DMA_ALLOCNOW,
2292 &amd->buffer_dmat) != 0) {
2294 printf("amd_init: bus_dma_tag_create failure!\n");
2297 TAILQ_INIT(&amd->free_srbs);
2298 TAILQ_INIT(&amd->running_srbs);
2299 TAILQ_INIT(&amd->waiting_srbs);
2300 amd->last_phase = SCSI_BUS_FREE;
2302 amd->unit = device_get_unit(dev);
2303 amd->SRBCount = MAX_SRB_CNT;
2305 amd_load_eeprom_or_defaults(amd);
2307 if (amd->eepromBuf[EE_MODE2] & LUN_CHECK) {
2312 amd->AdaptSCSIID = amd->eepromBuf[EE_ADAPT_SCSI_ID];
2313 amd->HostID_Bit = (1 << amd->AdaptSCSIID);
2314 amd->AdaptSCSILUN = 0;
2315 /* (eepromBuf[EE_TAG_CMD_NUM]) << 2; */
2317 amd->Gmode2 = amd->eepromBuf[EE_MODE2];
2319 for (i = 0; i <= amd->max_id; i++) {
2321 if (amd->AdaptSCSIID != i) {
2322 struct amd_target_info *tinfo;
2325 tinfo = &amd->tinfo[i];
2326 prom = (PEEprom)&amd->eepromBuf[i << 2];
2327 if ((prom->EE_MODE1 & EN_DISCONNECT) != 0) {
2328 tinfo->disc_tag |= AMD_USR_DISCENB;
2329 if ((prom->EE_MODE1 & TAG_QUEUING) != 0)
2330 tinfo->disc_tag |= AMD_USR_TAGENB;
2332 if ((prom->EE_MODE1 & SYNC_NEGO) != 0) {
2333 tinfo->user.period =
2334 eeprom_period[prom->EE_SPEED];
2335 tinfo->user.offset = AMD_MAX_SYNC_OFFSET;
2337 tinfo->CtrlR1 = amd->AdaptSCSIID;
2338 if ((prom->EE_MODE1 & PARITY_CHK) != 0)
2339 tinfo->CtrlR1 |= PARITY_ERR_REPO;
2340 tinfo->CtrlR3 = FAST_CLK;
2341 tinfo->CtrlR4 = EATER_25NS;
2342 if ((amd->eepromBuf[EE_MODE2] & ACTIVE_NEGATION) != 0)
2343 tinfo->CtrlR4 |= NEGATE_REQACKDATA;
2346 amd_write8(amd, SCSITIMEOUTREG, 153); /* 250ms selection timeout */
2347 /* Conversion factor = 0 , 40MHz clock */
2348 amd_write8(amd, CLKFACTREG, CLK_FREQ_40MHZ);
2349 /* NOP cmd - clear command register */
2350 amd_write8(amd, SCSICMDREG, NOP_CMD);
2351 amd_write8(amd, CNTLREG2, EN_FEATURE|EN_SCSI2_CMD);
2352 amd_write8(amd, CNTLREG3, FAST_CLK);
2354 if (amd->eepromBuf[EE_MODE2] & ACTIVE_NEGATION) {
2355 bval |= NEGATE_REQACKDATA;
2357 amd_write8(amd, CNTLREG4, bval);
2359 /* Disable SCSI bus reset interrupt */
2360 amd_write8(amd, CNTLREG1, DIS_INT_ON_SCSI_RST);
2366 * attach and init a host adapter
2369 amd_attach(device_t dev)
2371 struct cam_devq *devq; /* Device Queue to use for this SIM */
2373 struct amd_softc *amd = device_get_softc(dev);
2374 int unit = device_get_unit(dev);
2377 struct resource *irqres;
2379 if (amd_init(dev)) {
2381 printf("amd_attach: amd_init failure!\n");
2385 /* Reset Pending INT */
2386 intstat = amd_read8(amd, INTSTATREG);
2388 /* After setting up the adapter, map our interrupt */
2390 irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2391 RF_SHAREABLE | RF_ACTIVE);
2392 if (irqres == NULL ||
2393 bus_setup_intr(dev, irqres, INTR_TYPE_CAM, amd_intr, amd, &ih, NULL)
2396 printf("amd%d: unable to register interrupt handler!\n",
2402 * Now let the CAM generic SCSI layer find the SCSI devices on
2403 * the bus * start queue to reset to the idle loop. *
2404 * Create device queue of SIM(s) * (MAX_START_JOB - 1) :
2405 * max_sim_transactions
2407 devq = cam_simq_alloc(MAX_START_JOB);
2410 printf("amd_attach: cam_simq_alloc failure!\n");
2414 amd->psim = cam_sim_alloc(amd_action, amd_poll, "amd",
2415 amd, amd->unit, 1, MAX_TAGS_CMD_QUEUE,
2417 cam_simq_release(devq);
2418 if (amd->psim == NULL) {
2420 printf("amd_attach: cam_sim_alloc failure!\n");
2424 if (xpt_bus_register(amd->psim, 0) != CAM_SUCCESS) {
2425 cam_sim_free(amd->psim);
2427 printf("amd_attach: xpt_bus_register failure!\n");
2431 if (xpt_create_path(&amd->ppath, /* periph */ NULL,
2432 cam_sim_path(amd->psim), CAM_TARGET_WILDCARD,
2433 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2434 xpt_bus_deregister(cam_sim_path(amd->psim));
2435 cam_sim_free(amd->psim);
2437 printf("amd_attach: xpt_create_path failure!\n");
2445 amd_probe(device_t dev)
2447 if (pci_get_devid(dev) == PCI_DEVICE_ID_AMD53C974) {
2448 device_set_desc(dev,
2449 "Tekram DC390(T)/AMD53c974 SCSI Host Adapter");
2455 static device_method_t amd_methods[] = {
2456 /* Device interface */
2457 DEVMETHOD(device_probe, amd_probe),
2458 DEVMETHOD(device_attach, amd_attach),
2462 static driver_t amd_driver = {
2463 "amd", amd_methods, sizeof(struct amd_softc)
2466 static devclass_t amd_devclass;
2467 DRIVER_MODULE(amd, pci, amd_driver, amd_devclass, 0, 0);