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 $
37 *********************************************************************
40 * REV# DATE NAME DESCRIPTION
41 * 1.00 07/02/96 CLH First release for RELEASE-2.1.0
42 * 1.01 08/20/96 CLH Update for RELEASE-2.1.5
43 * 1.02 11/06/96 CLH Fixed more than 1 LUN scanning
44 * 1.03 12/20/96 CLH Modify to support 2.2-ALPHA
45 * 1.04 12/26/97 CLH Modify to support RELEASE-2.2.5
46 * 1.05 01/01/99 ERICH CHEN Modify to support RELEASE-3.0.x (CAM)
47 *********************************************************************
50 /* #define AMD_DEBUG0 */
51 /* #define AMD_DEBUG_SCSI_PHASE */
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/queue.h>
61 #include <sys/thread2.h>
66 #include <machine/clock.h>
68 #include <bus/cam/cam.h>
69 #include <bus/cam/cam_ccb.h>
70 #include <bus/cam/cam_sim.h>
71 #include <bus/cam/cam_xpt_sim.h>
72 #include <bus/cam/cam_debug.h>
74 #include <bus/cam/scsi/scsi_all.h>
75 #include <bus/cam/scsi/scsi_message.h>
77 #include <bus/pci/pcivar.h>
78 #include <bus/pci/pcireg.h>
81 #define PCI_DEVICE_ID_AMD53C974 0x20201022ul
82 #define PCI_BASE_ADDR0 0x10
84 typedef u_int (phase_handler_t)(struct amd_softc *, struct amd_srb *, u_int);
85 typedef phase_handler_t *phase_handler_func_t;
87 static void amd_intr(void *vamd);
88 static int amdstart(struct amd_softc *amd, struct amd_srb * pSRB);
89 static phase_handler_t amd_NopPhase;
91 static phase_handler_t amd_DataOutPhase0;
92 static phase_handler_t amd_DataInPhase0;
93 #define amd_CommandPhase0 amd_NopPhase
94 static phase_handler_t amd_StatusPhase0;
95 static phase_handler_t amd_MsgOutPhase0;
96 static phase_handler_t amd_MsgInPhase0;
97 static phase_handler_t amd_DataOutPhase1;
98 static phase_handler_t amd_DataInPhase1;
99 static phase_handler_t amd_CommandPhase1;
100 static phase_handler_t amd_StatusPhase1;
101 static phase_handler_t amd_MsgOutPhase1;
102 static phase_handler_t amd_MsgInPhase1;
104 static void amdsetupcommand(struct amd_softc *amd, struct amd_srb *srb);
105 static int amdparsemsg(struct amd_softc *amd);
106 static int amdhandlemsgreject(struct amd_softc *amd);
107 static void amdconstructsdtr(struct amd_softc *amd,
108 u_int period, u_int offset);
109 static u_int amdfindclockrate(struct amd_softc *amd, u_int *period);
110 static int amdsentmsg(struct amd_softc *amd, u_int msgtype, int full);
112 static void DataIO_Comm(struct amd_softc *amd, struct amd_srb *pSRB, u_int dir);
113 static void amd_Disconnect(struct amd_softc *amd);
114 static void amd_Reselect(struct amd_softc *amd);
115 static void SRBdone(struct amd_softc *amd, struct amd_srb *pSRB);
116 static void amd_ScsiRstDetect(struct amd_softc *amd);
117 static void amd_ResetSCSIBus(struct amd_softc *amd);
118 static void RequestSense(struct amd_softc *amd, struct amd_srb *pSRB);
119 static void amd_InvalidCmd(struct amd_softc *amd);
122 static void amd_timeout(void *arg1);
123 static void amd_reset(struct amd_softc *amd);
125 static u_int8_t * phystovirt(struct amd_srb *pSRB, u_int32_t xferCnt);
127 void amd_linkSRB(struct amd_softc *amd);
128 static int amd_init(device_t);
129 static void amd_load_defaults(struct amd_softc *amd);
130 static void amd_load_eeprom_or_defaults(struct amd_softc *amd);
131 static int amd_EEpromInDO(struct amd_softc *amd);
132 static u_int16_t EEpromGetData1(struct amd_softc *amd);
133 static void amd_EnDisableCE(struct amd_softc *amd, int mode, int *regval);
134 static void amd_EEpromOutDI(struct amd_softc *amd, int *regval, int Carry);
135 static void amd_Prepare(struct amd_softc *amd, int *regval, u_int8_t EEpromCmd);
136 static void amd_ReadEEprom(struct amd_softc *amd);
138 static int amd_probe(device_t);
139 static int amd_attach(device_t);
140 static void amdcompletematch(struct amd_softc *amd, target_id_t target,
141 lun_id_t lun, u_int tag, struct srb_queue *queue,
143 static void amdsetsync(struct amd_softc *amd, u_int target, u_int clockrate,
144 u_int period, u_int offset, u_int type);
145 static void amdsettags(struct amd_softc *amd, u_int target, int tagenb);
147 static __inline void amd_clear_msg_state(struct amd_softc *amd);
150 amd_clear_msg_state(struct amd_softc *amd)
153 amd->msgout_index = 0;
154 amd->msgin_index = 0;
157 /* CAM SIM entry points */
158 #define ccb_srb_ptr spriv_ptr0
159 #define ccb_amd_ptr spriv_ptr1
160 static void amd_action(struct cam_sim *sim, union ccb *ccb);
161 static void amd_poll(struct cam_sim *sim);
164 * State engine function tables indexed by SCSI phase number
166 phase_handler_func_t amd_SCSI_phase0[] = {
177 phase_handler_func_t amd_SCSI_phase1[] = {
189 * EEProm/BIOS negotiation periods
191 u_int8_t eeprom_period[] = {
203 * chip clock setting to SCSI specified sync parameter table.
205 u_int8_t tinfo_sync_period[] = {
218 static __inline struct amd_srb *
219 amdgetsrb(struct amd_softc * amd)
221 struct amd_srb * pSRB;
224 pSRB = TAILQ_FIRST(&amd->free_srbs);
226 TAILQ_REMOVE(&amd->free_srbs, pSRB, links);
232 amdsetupcommand(struct amd_softc *amd, struct amd_srb *srb)
234 struct scsi_request_sense sense_cmd;
235 struct ccb_scsiio *csio;
239 csio = &srb->pccb->csio;
241 if (srb->SRBFlag & AUTO_REQSENSE) {
242 sense_cmd.opcode = REQUEST_SENSE;
243 sense_cmd.byte2 = srb->pccb->ccb_h.target_lun << 5;
244 sense_cmd.unused[0] = 0;
245 sense_cmd.unused[1] = 0;
246 sense_cmd.length = csio->sense_len;
247 sense_cmd.control = 0;
248 cdb = &sense_cmd.opcode;
249 cdb_len = sizeof(sense_cmd);
251 cdb = &srb->CmdBlock[0];
252 cdb_len = srb->ScsiCmdLen;
254 amd_write8_multi(amd, SCSIFIFOREG, cdb, cdb_len);
258 * Attempt to start a waiting transaction. Interrupts must be disabled
259 * upon entry to this function.
262 amdrunwaiting(struct amd_softc *amd) {
265 if (amd->last_phase != SCSI_BUS_FREE)
268 srb = TAILQ_FIRST(&amd->waiting_srbs);
272 if (amdstart(amd, srb) == 0) {
273 TAILQ_REMOVE(&amd->waiting_srbs, srb, links);
274 TAILQ_INSERT_HEAD(&amd->running_srbs, srb, links);
279 amdexecutesrb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
283 struct amd_softc *amd;
285 srb = (struct amd_srb *)arg;
287 amd = (struct amd_softc *)ccb->ccb_h.ccb_amd_ptr;
291 kprintf("amd%d: Unexpected error 0x%x returned from "
292 "bus_dmamap_load\n", amd->unit, error);
293 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
294 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
295 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
297 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
304 bus_dma_segment_t *end_seg;
307 end_seg = dm_segs + nseg;
309 /* Copy the segments into our SG list */
310 srb->pSGlist = &srb->SGsegment[0];
312 while (dm_segs < end_seg) {
313 sg->SGXLen = dm_segs->ds_len;
314 sg->SGXPtr = dm_segs->ds_addr;
319 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
320 op = BUS_DMASYNC_PREREAD;
322 op = BUS_DMASYNC_PREWRITE;
324 bus_dmamap_sync(amd->buffer_dmat, srb->dmamap, op);
329 srb->AdaptStatus = 0;
330 srb->TargetStatus = 0;
335 srb->TotalXferredLen = 0;
337 srb->SGToBeXferLen = 0;
343 * Last time we need to check if this CCB needs to
346 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
348 bus_dmamap_unload(amd->buffer_dmat, srb->dmamap);
349 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
354 ccb->ccb_h.status |= CAM_SIM_QUEUED;
356 /* XXX Need a timeout handler */
357 callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
360 TAILQ_INSERT_TAIL(&amd->waiting_srbs, srb, links);
366 amd_action(struct cam_sim * psim, union ccb * pccb)
368 struct amd_softc * amd;
371 CAM_DEBUG(pccb->ccb_h.path, CAM_DEBUG_TRACE, ("amd_action\n"));
373 amd = (struct amd_softc *) cam_sim_softc(psim);
374 target_id = pccb->ccb_h.target_id;
376 switch (pccb->ccb_h.func_code) {
379 struct amd_srb * pSRB;
380 struct ccb_scsiio *pcsio;
385 * Assign an SRB and connect it with this ccb.
387 pSRB = amdgetsrb(amd);
391 pccb->ccb_h.status = CAM_RESRC_UNAVAIL;
396 pccb->ccb_h.ccb_srb_ptr = pSRB;
397 pccb->ccb_h.ccb_amd_ptr = amd;
398 pSRB->ScsiCmdLen = pcsio->cdb_len;
399 bcopy(pcsio->cdb_io.cdb_bytes, pSRB->CmdBlock, pcsio->cdb_len);
400 if ((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
401 if ((pccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
403 * We've been given a pointer
404 * to a single buffer.
406 if ((pccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
411 bus_dmamap_load(amd->buffer_dmat,
417 if (error == EINPROGRESS) {
420 * ordering, freeze the
422 * until our mapping is
425 xpt_freeze_simq(amd->psim, 1);
426 pccb->ccb_h.status |=
431 struct bus_dma_segment seg;
433 /* Pointer to physical buffer */
435 (bus_addr_t)pcsio->data_ptr;
436 seg.ds_len = pcsio->dxfer_len;
437 amdexecutesrb(pSRB, &seg, 1, 0);
440 struct bus_dma_segment *segs;
442 if ((pccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0
443 || (pccb->ccb_h.flags & CAM_DATA_PHYS) != 0) {
444 TAILQ_INSERT_HEAD(&amd->free_srbs,
446 pccb->ccb_h.status = CAM_PROVIDE_FAIL;
451 /* Just use the segments provided */
453 (struct bus_dma_segment *)pcsio->data_ptr;
454 amdexecutesrb(pSRB, segs, pcsio->sglist_cnt, 0);
457 amdexecutesrb(pSRB, NULL, 0, 0);
462 struct ccb_pathinq *cpi = &pccb->cpi;
464 cpi->version_num = 1;
465 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE;
466 cpi->target_sprt = 0;
468 cpi->hba_eng_cnt = 0;
470 cpi->max_lun = amd->max_lun; /* 7 or 0 */
471 cpi->initiator_id = amd->AdaptSCSIID;
472 cpi->bus_id = cam_sim_bus(psim);
473 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
474 strncpy(cpi->hba_vid, "TRM-AMD", HBA_IDLEN);
475 strncpy(cpi->dev_name, cam_sim_name(psim), DEV_IDLEN);
476 cpi->unit_number = cam_sim_unit(psim);
477 cpi->transport = XPORT_SPI;
478 cpi->transport_version = 2;
479 cpi->protocol = PROTO_SCSI;
480 cpi->protocol_version = SCSI_REV_2;
481 cpi->ccb_h.status = CAM_REQ_CMP;
486 pccb->ccb_h.status = CAM_REQ_INVALID;
494 amd_ResetSCSIBus(amd);
497 for (i = 0; i < 500; i++) {
498 DELAY(1000); /* Wait until our interrupt
502 pccb->ccb_h.status = CAM_REQ_CMP;
507 pccb->ccb_h.status = CAM_REQ_INVALID;
511 pccb->ccb_h.status = CAM_REQ_INVALID;
514 case XPT_GET_TRAN_SETTINGS:
516 struct ccb_trans_settings *cts = &pccb->cts;
517 struct amd_target_info *targ_info = &amd->tinfo[target_id];
518 struct amd_transinfo *tinfo;
519 struct ccb_trans_settings_scsi *scsi =
520 &cts->proto_specific.scsi;
521 struct ccb_trans_settings_spi *spi =
522 &cts->xport_specific.spi;
524 cts->protocol = PROTO_SCSI;
525 cts->protocol_version = SCSI_REV_2;
526 cts->transport = XPORT_SPI;
527 cts->transport_version = 2;
530 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
531 /* current transfer settings */
532 if (targ_info->disc_tag & AMD_CUR_DISCENB) {
533 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
537 if (targ_info->disc_tag & AMD_CUR_TAGENB) {
538 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
542 tinfo = &targ_info->current;
544 /* default(user) transfer settings */
545 if (targ_info->disc_tag & AMD_USR_DISCENB) {
546 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
550 if (targ_info->disc_tag & AMD_USR_TAGENB) {
551 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
555 tinfo = &targ_info->user;
557 spi->sync_period = tinfo->period;
558 spi->sync_offset = tinfo->offset;
561 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
562 spi->valid = CTS_SPI_VALID_SYNC_RATE
563 | CTS_SPI_VALID_SYNC_OFFSET
564 | CTS_SPI_VALID_BUS_WIDTH
565 | CTS_SPI_VALID_DISC;
566 scsi->valid = CTS_SCSI_VALID_TQ;
567 pccb->ccb_h.status = CAM_REQ_CMP;
571 #define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
572 #define IS_USER_SETTINGS(c) (c->type == CTS_TYPE_USER_SETTINGS)
573 case XPT_SET_TRAN_SETTINGS:
575 struct ccb_trans_settings *cts = &pccb->cts;
576 struct amd_target_info *targ_info;
577 u_int update_type = 0;
580 struct ccb_trans_settings_scsi *scsi =
581 &cts->proto_specific.scsi;
582 struct ccb_trans_settings_spi *spi =
583 &cts->xport_specific.spi;
584 if (IS_CURRENT_SETTINGS(cts)) {
585 update_type |= AMD_TRANS_GOAL;
586 } else if (IS_USER_SETTINGS(cts)) {
587 update_type |= AMD_TRANS_USER;
590 || update_type == (AMD_TRANS_USER|AMD_TRANS_GOAL)) {
591 cts->ccb_h.status = CAM_REQ_INVALID;
596 targ_info = &amd->tinfo[target_id];
598 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
599 if (update_type & AMD_TRANS_GOAL) {
600 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB)
602 targ_info->disc_tag |= AMD_CUR_DISCENB;
604 targ_info->disc_tag &= ~AMD_CUR_DISCENB;
607 if (update_type & AMD_TRANS_USER) {
608 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB)
610 targ_info->disc_tag |= AMD_USR_DISCENB;
612 targ_info->disc_tag &= ~AMD_USR_DISCENB;
616 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
617 if (update_type & AMD_TRANS_GOAL) {
618 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB)
620 targ_info->disc_tag |= AMD_CUR_TAGENB;
622 targ_info->disc_tag &= ~AMD_CUR_TAGENB;
625 if (update_type & AMD_TRANS_USER) {
626 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB)
628 targ_info->disc_tag |= AMD_USR_TAGENB;
630 targ_info->disc_tag &= ~AMD_USR_TAGENB;
635 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
636 if (update_type & AMD_TRANS_GOAL)
637 spi->sync_offset = targ_info->goal.offset;
639 spi->sync_offset = targ_info->user.offset;
642 if (spi->sync_offset > AMD_MAX_SYNC_OFFSET)
643 spi->sync_offset = AMD_MAX_SYNC_OFFSET;
645 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
646 if (update_type & AMD_TRANS_GOAL)
647 spi->sync_period = targ_info->goal.period;
649 spi->sync_period = targ_info->user.period;
652 last_entry = sizeof(tinfo_sync_period) - 1;
653 if ((spi->sync_period != 0)
654 && (spi->sync_period < tinfo_sync_period[0]))
655 spi->sync_period = tinfo_sync_period[0];
656 if (spi->sync_period > tinfo_sync_period[last_entry])
657 spi->sync_period = 0;
658 if (spi->sync_offset == 0)
659 spi->sync_period = 0;
661 if ((update_type & AMD_TRANS_USER) != 0) {
662 targ_info->user.period = spi->sync_period;
663 targ_info->user.offset = spi->sync_offset;
665 if ((update_type & AMD_TRANS_GOAL) != 0) {
666 targ_info->goal.period = spi->sync_period;
667 targ_info->goal.offset = spi->sync_offset;
670 pccb->ccb_h.status = CAM_REQ_CMP;
674 case XPT_CALC_GEOMETRY:
676 struct ccb_calc_geometry *ccg;
678 u_int32_t secs_per_cylinder;
682 size_mb = ccg->volume_size/((1024L * 1024L)/ccg->block_size);
683 extended = (amd->eepromBuf[EE_MODE2] & GREATER_1G) != 0;
685 if (size_mb > 1024 && extended) {
687 ccg->secs_per_track = 63;
690 ccg->secs_per_track = 32;
692 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
693 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
694 pccb->ccb_h.status = CAM_REQ_CMP;
699 pccb->ccb_h.status = CAM_REQ_INVALID;
706 amd_poll(struct cam_sim * psim)
708 amd_intr(cam_sim_softc(psim));
712 phystovirt(struct amd_srb * pSRB, u_int32_t xferCnt)
715 struct ccb_scsiio *pcsio;
717 struct amd_sg * pseg;
720 pcsio = &pSRB->pccb->csio;
722 dataPtr = (intptr_t) pcsio->data_ptr;
723 pseg = pSRB->SGsegment;
724 for (i = 0; i < pSRB->SGIndex; i++) {
725 dataPtr += (int) pseg->SGXLen;
728 dataPtr += (int) xferCnt;
729 return ((u_int8_t *) dataPtr);
733 ResetDevParam(struct amd_softc * amd)
737 for (target = 0; target <= amd->max_id; target++) {
738 if (amd->AdaptSCSIID != target) {
739 amdsetsync(amd, target, /*clockrate*/0,
740 /*period*/0, /*offset*/0, AMD_TRANS_CUR);
746 amdcompletematch(struct amd_softc *amd, target_id_t target, lun_id_t lun,
747 u_int tag, struct srb_queue *queue, cam_status status)
750 struct amd_srb *next_srb;
752 for (srb = TAILQ_FIRST(queue); srb != NULL; srb = next_srb) {
755 next_srb = TAILQ_NEXT(srb, links);
756 if (srb->pccb->ccb_h.target_id != target
757 && target != CAM_TARGET_WILDCARD)
760 if (srb->pccb->ccb_h.target_lun != lun
761 && lun != CAM_LUN_WILDCARD)
764 if (srb->TagNumber != tag
765 && tag != AMD_TAG_WILDCARD)
769 TAILQ_REMOVE(queue, srb, links);
770 TAILQ_INSERT_HEAD(&amd->free_srbs, srb, links);
771 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0
772 && (status & CAM_DEV_QFRZN) != 0)
773 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
774 ccb->ccb_h.status = status;
781 amdsetsync(struct amd_softc *amd, u_int target, u_int clockrate,
782 u_int period, u_int offset, u_int type)
784 struct amd_target_info *tinfo;
788 tinfo = &amd->tinfo[target];
789 old_period = tinfo->current.period;
790 old_offset = tinfo->current.offset;
791 if ((type & AMD_TRANS_CUR) != 0
792 && (old_period != period || old_offset != offset)) {
793 struct cam_path *path;
795 tinfo->current.period = period;
796 tinfo->current.offset = offset;
797 tinfo->sync_period_reg = clockrate;
798 tinfo->sync_offset_reg = offset;
799 tinfo->CtrlR3 &= ~FAST_SCSI;
800 tinfo->CtrlR4 &= ~EATER_25NS;
802 tinfo->CtrlR4 |= EATER_25NS;
804 tinfo->CtrlR3 |= FAST_SCSI;
806 if ((type & AMD_TRANS_ACTIVE) == AMD_TRANS_ACTIVE) {
807 amd_write8(amd, SYNCPERIOREG, tinfo->sync_period_reg);
808 amd_write8(amd, SYNCOFFREG, tinfo->sync_offset_reg);
809 amd_write8(amd, CNTLREG3, tinfo->CtrlR3);
810 amd_write8(amd, CNTLREG4, tinfo->CtrlR4);
812 /* If possible, update the XPT's notion of our transfer rate */
813 if (xpt_create_path(&path, /*periph*/NULL,
814 cam_sim_path(amd->psim), target,
815 CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
816 struct ccb_trans_settings neg;
817 struct ccb_trans_settings_spi *spi =
818 &neg.xport_specific.spi;
819 xpt_setup_ccb(&neg.ccb_h, path, /*priority*/1);
820 memset(&neg, 0, sizeof (neg));
821 spi->sync_period = period;
822 spi->sync_offset = offset;
823 spi->valid = CTS_SPI_VALID_SYNC_RATE
824 | CTS_SPI_VALID_SYNC_OFFSET;
825 xpt_async(AC_TRANSFER_NEG, path, &neg);
829 if ((type & AMD_TRANS_GOAL) != 0) {
830 tinfo->goal.period = period;
831 tinfo->goal.offset = offset;
834 if ((type & AMD_TRANS_USER) != 0) {
835 tinfo->user.period = period;
836 tinfo->user.offset = offset;
841 amdsettags(struct amd_softc *amd, u_int target, int tagenb)
843 panic("Implement me!");
849 **********************************************************************
850 * Function : amd_reset (struct amd_softc * amd)
851 * Purpose : perform a hard reset on the SCSI bus( and AMD chip).
852 * Inputs : cmd - command which caused the SCSI RESET
853 **********************************************************************
856 amd_reset(struct amd_softc * amd)
863 kprintf("DC390: RESET");
867 bval = amd_read8(amd, CNTLREG1);
868 bval |= DIS_INT_ON_SCSI_RST;
869 amd_write8(amd, CNTLREG1, bval); /* disable interrupt */
870 amd_ResetSCSIBus(amd);
872 for (i = 0; i < 500; i++) {
876 bval = amd_read8(amd, CNTLREG1);
877 bval &= ~DIS_INT_ON_SCSI_RST;
878 amd_write8(amd, CNTLREG1, bval); /* re-enable interrupt */
880 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
881 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
884 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
885 AMD_TAG_WILDCARD, &amd->running_srbs,
886 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
887 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
888 AMD_TAG_WILDCARD, &amd->waiting_srbs,
889 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
890 amd->active_srb = NULL;
897 amd_timeout(void *arg1)
899 struct amd_srb * pSRB;
901 pSRB = (struct amd_srb *) arg1;
906 amdstart(struct amd_softc *amd, struct amd_srb *pSRB)
909 struct ccb_scsiio *pcsio;
910 struct amd_target_info *targ_info;
919 target = pccb->ccb_h.target_id;
920 lun = pccb->ccb_h.target_lun;
921 targ_info = &amd->tinfo[target];
923 amd_clear_msg_state(amd);
924 amd_write8(amd, SCSIDESTIDREG, target);
925 amd_write8(amd, SYNCPERIOREG, targ_info->sync_period_reg);
926 amd_write8(amd, SYNCOFFREG, targ_info->sync_offset_reg);
927 amd_write8(amd, CNTLREG1, targ_info->CtrlR1);
928 amd_write8(amd, CNTLREG3, targ_info->CtrlR3);
929 amd_write8(amd, CNTLREG4, targ_info->CtrlR4);
930 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
932 identify_msg = MSG_IDENTIFYFLAG | lun;
933 if ((targ_info->disc_tag & AMD_CUR_DISCENB) != 0
934 && (pccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0
935 && (pSRB->CmdBlock[0] != REQUEST_SENSE)
936 && (pSRB->SRBFlag & AUTO_REQSENSE) == 0)
937 identify_msg |= MSG_IDENTIFY_DISCFLAG;
939 amd_write8(amd, SCSIFIFOREG, identify_msg);
941 if ((targ_info->disc_tag & AMD_CUR_TAGENB) == 0
942 || (identify_msg & MSG_IDENTIFY_DISCFLAG) == 0)
943 pccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
944 if (targ_info->current.period != targ_info->goal.period
945 || targ_info->current.offset != targ_info->goal.offset) {
946 command = SEL_W_ATN_STOP;
947 amdconstructsdtr(amd, targ_info->goal.period,
948 targ_info->goal.offset);
949 } else if ((pccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
950 command = SEL_W_ATN2;
951 pSRB->SRBState = SRB_START;
952 amd_write8(amd, SCSIFIFOREG, pcsio->tag_action);
953 amd_write8(amd, SCSIFIFOREG, pSRB->TagNumber);
957 pSRB->SRBState = SRB_START;
959 if (command != SEL_W_ATN_STOP)
960 amdsetupcommand(amd, pSRB);
962 if (amd_read8(amd, SCSISTATREG) & INTERRUPT) {
963 pSRB->SRBState = SRB_READY;
966 amd->last_phase = SCSI_ARBITRATING;
967 amd_write8(amd, SCSICMDREG, command);
968 amd->active_srb = pSRB;
969 amd->cur_target = target;
976 * Catch an interrupt from the adapter.
977 * Process pending device interrupts.
982 struct amd_softc *amd;
983 struct amd_srb *pSRB;
984 u_int internstat = 0;
988 amd = (struct amd_softc *)arg;
992 kprintf("amd_intr: amd NULL return......");
997 scsistat = amd_read8(amd, SCSISTATREG);
998 if (!(scsistat & INTERRUPT)) {
1000 kprintf("amd_intr: scsistat = NULL ,return......");
1004 #ifdef AMD_DEBUG_SCSI_PHASE
1005 kprintf("scsistat=%2x,", scsistat);
1008 internstat = amd_read8(amd, INTERNSTATREG);
1009 intstat = amd_read8(amd, INTSTATREG);
1011 #ifdef AMD_DEBUG_SCSI_PHASE
1012 kprintf("intstat=%2x,", intstat);
1015 if (intstat & DISCONNECTED) {
1016 amd_Disconnect(amd);
1019 if (intstat & RESELECTED) {
1023 if (intstat & INVALID_CMD) {
1024 amd_InvalidCmd(amd);
1027 if (intstat & SCSI_RESET_) {
1028 amd_ScsiRstDetect(amd);
1031 if (intstat & (SUCCESSFUL_OP + SERVICE_REQUEST)) {
1032 pSRB = amd->active_srb;
1034 * Run our state engine. First perform
1035 * post processing for the last phase we
1036 * were in, followed by any processing
1037 * required to handle the current phase.
1040 amd_SCSI_phase0[amd->last_phase](amd, pSRB, scsistat);
1041 amd->last_phase = scsistat & SCSI_PHASE_MASK;
1042 (void)amd_SCSI_phase1[amd->last_phase](amd, pSRB, scsistat);
1047 amd_DataOutPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1049 struct amd_sg *psgl;
1050 u_int32_t ResidCnt, xferCnt;
1052 if (!(pSRB->SRBState & SRB_XFERPAD)) {
1053 if (scsistat & PARITY_ERR) {
1054 pSRB->SRBStatus |= PARITY_ERROR;
1056 if (scsistat & COUNT_2_ZERO) {
1057 while ((amd_read8(amd, DMA_Status)&DMA_XFER_DONE) == 0)
1059 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
1061 if (pSRB->SGIndex < pSRB->SGcount) {
1063 psgl = pSRB->pSGlist;
1064 pSRB->SGPhysAddr = psgl->SGXPtr;
1065 pSRB->SGToBeXferLen = psgl->SGXLen;
1067 pSRB->SGToBeXferLen = 0;
1070 ResidCnt = amd_read8(amd, CURRENTFIFOREG) & 0x1f;
1071 ResidCnt += amd_read8(amd, CTCREG_LOW)
1072 | (amd_read8(amd, CTCREG_MID) << 8)
1073 | (amd_read8(amd, CURTXTCNTREG) << 16);
1075 xferCnt = pSRB->SGToBeXferLen - ResidCnt;
1076 pSRB->SGPhysAddr += xferCnt;
1077 pSRB->TotalXferredLen += xferCnt;
1078 pSRB->SGToBeXferLen = ResidCnt;
1081 amd_write8(amd, DMA_Cmd, WRITE_DIRECTION | DMA_IDLE_CMD);
1086 amd_DataInPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1089 u_int16_t i, residual;
1090 struct amd_sg *psgl;
1091 u_int32_t ResidCnt, xferCnt;
1094 if (!(pSRB->SRBState & SRB_XFERPAD)) {
1095 if (scsistat & PARITY_ERR) {
1096 pSRB->SRBStatus |= PARITY_ERROR;
1098 if (scsistat & COUNT_2_ZERO) {
1100 bval = amd_read8(amd, DMA_Status);
1101 if ((bval & DMA_XFER_DONE) != 0)
1104 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_IDLE_CMD);
1106 pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
1108 if (pSRB->SGIndex < pSRB->SGcount) {
1110 psgl = pSRB->pSGlist;
1111 pSRB->SGPhysAddr = psgl->SGXPtr;
1112 pSRB->SGToBeXferLen = psgl->SGXLen;
1114 pSRB->SGToBeXferLen = 0;
1116 } else { /* phase changed */
1118 bval = amd_read8(amd, CURRENTFIFOREG);
1119 while (bval & 0x1f) {
1120 if ((bval & 0x1f) == 1) {
1121 for (i = 0; i < 0x100; i++) {
1122 bval = amd_read8(amd, CURRENTFIFOREG);
1123 if (!(bval & 0x1f)) {
1125 } else if (i == 0x0ff) {
1131 bval = amd_read8(amd, CURRENTFIFOREG);
1135 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_BLAST_CMD);
1136 for (i = 0; i < 0x8000; i++) {
1137 if ((amd_read8(amd, DMA_Status)&BLAST_COMPLETE))
1140 amd_write8(amd, DMA_Cmd, READ_DIRECTION|DMA_IDLE_CMD);
1142 ResidCnt = amd_read8(amd, CTCREG_LOW)
1143 | (amd_read8(amd, CTCREG_MID) << 8)
1144 | (amd_read8(amd, CURTXTCNTREG) << 16);
1145 xferCnt = pSRB->SGToBeXferLen - ResidCnt;
1146 pSRB->SGPhysAddr += xferCnt;
1147 pSRB->TotalXferredLen += xferCnt;
1148 pSRB->SGToBeXferLen = ResidCnt;
1150 /* get residual byte */
1151 bval = amd_read8(amd, SCSIFIFOREG);
1152 ptr = phystovirt(pSRB, xferCnt);
1155 pSRB->TotalXferredLen++;
1156 pSRB->SGToBeXferLen--;
1164 amd_StatusPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1166 pSRB->TargetStatus = amd_read8(amd, SCSIFIFOREG);
1168 pSRB->EndMessage = amd_read8(amd, SCSIFIFOREG);
1169 pSRB->SRBState = SRB_COMPLETED;
1170 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1175 amd_MsgOutPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1177 if (pSRB->SRBState & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT)) {
1178 scsistat = SCSI_NOP0;
1184 amd_MsgInPhase0(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1188 amd->msgin_buf[amd->msgin_index] = amd_read8(amd, SCSIFIFOREG);
1190 done = amdparsemsg(amd);
1192 amd->msgin_index = 0;
1199 amdparsemsg(struct amd_softc *amd)
1210 * Parse as much of the message as is availible,
1211 * rejecting it if we don't support it. When
1212 * the entire message is availible and has been
1213 * handled, return TRUE indicating that we have
1214 * parsed an entire message.
1216 switch (amd->msgin_buf[0]) {
1217 case MSG_DISCONNECT:
1218 amd->active_srb->SRBState = SRB_DISCONNECT;
1219 amd->disc_count[amd->cur_target][amd->cur_lun]++;
1222 case MSG_SIMPLE_Q_TAG:
1224 struct amd_srb *disc_srb;
1226 if (amd->msgin_index < 1)
1228 disc_srb = &amd->SRB_array[amd->msgin_buf[1]];
1229 if (amd->active_srb != NULL
1230 || disc_srb->SRBState != SRB_DISCONNECT
1231 || disc_srb->pccb->ccb_h.target_id != amd->cur_target
1232 || disc_srb->pccb->ccb_h.target_lun != amd->cur_lun) {
1233 kprintf("amd%d: Unexpected tagged reselection "
1234 "for target %d, Issuing Abort\n", amd->unit,
1236 amd->msgout_buf[0] = MSG_ABORT;
1237 amd->msgout_len = 1;
1241 amd->active_srb = disc_srb;
1242 amd->disc_count[amd->cur_target][amd->cur_lun]--;
1246 case MSG_MESSAGE_REJECT:
1247 response = amdhandlemsgreject(amd);
1248 if (response == FALSE)
1249 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1261 /* Wait for enough of the message to begin validation */
1262 if (amd->msgin_index < 1)
1264 if (amd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
1269 /* Wait for opcode */
1270 if (amd->msgin_index < 2)
1273 if (amd->msgin_buf[2] != MSG_EXT_SDTR) {
1279 * Wait until we have both args before validating
1280 * and acting on this message.
1282 * Add one to MSG_EXT_SDTR_LEN to account for
1283 * the extended message preamble.
1285 if (amd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
1288 period = amd->msgin_buf[3];
1289 saved_offset = offset = amd->msgin_buf[4];
1290 clockrate = amdfindclockrate(amd, &period);
1291 if (offset > AMD_MAX_SYNC_OFFSET)
1292 offset = AMD_MAX_SYNC_OFFSET;
1293 if (period == 0 || offset == 0) {
1298 amdsetsync(amd, amd->cur_target, clockrate, period, offset,
1299 AMD_TRANS_ACTIVE|AMD_TRANS_GOAL);
1302 * See if we initiated Sync Negotiation
1303 * and didn't have to fall down to async
1306 if (amdsentmsg(amd, MSG_EXT_SDTR, /*full*/TRUE)) {
1308 if (saved_offset != offset) {
1309 /* Went too low - force async */
1314 * Send our own SDTR in reply
1317 kprintf("Sending SDTR!\n");
1318 amd->msgout_index = 0;
1319 amd->msgout_len = 0;
1320 amdconstructsdtr(amd, period, offset);
1321 amd->msgout_index = 0;
1327 case MSG_SAVEDATAPOINTER:
1328 case MSG_RESTOREPOINTERS:
1329 /* XXX Implement!!! */
1338 amd->msgout_index = 0;
1339 amd->msgout_len = 1;
1340 amd->msgout_buf[0] = MSG_MESSAGE_REJECT;
1346 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1348 if (done && !response)
1349 /* Clear the outgoing message buffer */
1350 amd->msgout_len = 0;
1353 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1359 amdfindclockrate(struct amd_softc *amd, u_int *period)
1364 for (i = 0; i < sizeof(tinfo_sync_period); i++) {
1365 u_int8_t *table_entry;
1367 table_entry = &tinfo_sync_period[i];
1368 if (*period <= *table_entry) {
1370 * When responding to a target that requests
1371 * sync, the requested rate may fall between
1372 * two rates that we can output, but still be
1373 * a rate that we can receive. Because of this,
1374 * we want to respond to the target with
1375 * the same rate that it sent to us even
1376 * if the period we use to send data to it
1377 * is lower. Only lower the response period
1381 *period = *table_entry;
1387 if (i == sizeof(tinfo_sync_period)) {
1388 /* Too slow for us. Use asnyc transfers. */
1398 * See if we sent a particular extended message to the target.
1399 * If "full" is true, the target saw the full message.
1400 * If "full" is false, the target saw at least the first
1401 * byte of the message.
1404 amdsentmsg(struct amd_softc *amd, u_int msgtype, int full)
1412 while (index < amd->msgout_len) {
1413 if ((amd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
1414 || amd->msgout_buf[index] == MSG_MESSAGE_REJECT)
1416 else if (amd->msgout_buf[index] >= MSG_SIMPLE_Q_TAG
1417 && amd->msgout_buf[index] < MSG_IGN_WIDE_RESIDUE) {
1418 /* Skip tag type and tag id */
1420 } else if (amd->msgout_buf[index] == MSG_EXTENDED) {
1421 /* Found a candidate */
1422 if (amd->msgout_buf[index+2] == msgtype) {
1425 end_index = index + 1
1426 + amd->msgout_buf[index + 1];
1428 if (amd->msgout_index > end_index)
1430 } else if (amd->msgout_index > index)
1435 panic("amdsentmsg: Inconsistent msg buffer");
1442 amdconstructsdtr(struct amd_softc *amd, u_int period, u_int offset)
1444 amd->msgout_buf[amd->msgout_index++] = MSG_EXTENDED;
1445 amd->msgout_buf[amd->msgout_index++] = MSG_EXT_SDTR_LEN;
1446 amd->msgout_buf[amd->msgout_index++] = MSG_EXT_SDTR;
1447 amd->msgout_buf[amd->msgout_index++] = period;
1448 amd->msgout_buf[amd->msgout_index++] = offset;
1449 amd->msgout_len += 5;
1453 amdhandlemsgreject(struct amd_softc *amd)
1456 * If we had an outstanding SDTR for this
1457 * target, this is a signal that the target
1458 * is refusing negotiation. Also watch out
1459 * for rejected tag messages.
1461 struct amd_srb *srb;
1462 struct amd_target_info *targ_info;
1463 int response = FALSE;
1465 srb = amd->active_srb;
1466 targ_info = &amd->tinfo[amd->cur_target];
1467 if (amdsentmsg(amd, MSG_EXT_SDTR, /*full*/FALSE)) {
1468 /* note asynch xfers and clear flag */
1469 amdsetsync(amd, amd->cur_target, /*clockrate*/0,
1470 /*period*/0, /*offset*/0,
1471 AMD_TRANS_ACTIVE|AMD_TRANS_GOAL);
1472 kprintf("amd%d:%d: refuses synchronous negotiation. "
1473 "Using asynchronous transfers\n",
1474 amd->unit, amd->cur_target);
1475 } else if ((srb != NULL)
1476 && (srb->pccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
1477 struct ccb_trans_settings neg;
1478 struct ccb_trans_settings_scsi *scsi = &neg.proto_specific.scsi;
1480 kprintf("amd%d:%d: refuses tagged commands. Performing "
1481 "non-tagged I/O\n", amd->unit, amd->cur_target);
1483 amdsettags(amd, amd->cur_target, FALSE);
1484 memset(&neg, 0, sizeof (neg));
1485 scsi->valid = CTS_SCSI_VALID_TQ;
1486 xpt_setup_ccb(&neg.ccb_h, srb->pccb->ccb_h.path, /*priority*/1);
1487 xpt_async(AC_TRANSFER_NEG, srb->pccb->ccb_h.path, &neg);
1490 * Resend the identify for this CCB as the target
1491 * may believe that the selection is invalid otherwise.
1493 if (amd->msgout_len != 0)
1494 bcopy(&amd->msgout_buf[0], &amd->msgout_buf[1],
1496 amd->msgout_buf[0] = MSG_IDENTIFYFLAG
1497 | srb->pccb->ccb_h.target_lun;
1499 if ((targ_info->disc_tag & AMD_CUR_DISCENB) != 0
1500 && (srb->pccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1501 amd->msgout_buf[0] |= MSG_IDENTIFY_DISCFLAG;
1503 srb->pccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
1506 * Requeue all tagged commands for this target
1507 * currently in our posession so they can be
1508 * converted to untagged commands.
1510 amdcompletematch(amd, amd->cur_target, amd->cur_lun,
1511 AMD_TAG_WILDCARD, &amd->waiting_srbs,
1512 CAM_DEV_QFRZN|CAM_REQUEUE_REQ);
1515 * Otherwise, we ignore it.
1517 kprintf("amd%d:%d: Message reject received -- ignored\n",
1518 amd->unit, amd->cur_target);
1524 if (!(pSRB->SRBState & SRB_MSGIN_MULTI)) {
1525 if (bval == MSG_DISCONNECT) {
1526 pSRB->SRBState = SRB_DISCONNECT;
1527 } else if (bval == MSG_SAVEDATAPOINTER) {
1529 } else if ((bval == MSG_EXTENDED)
1530 || ((bval >= MSG_SIMPLE_Q_TAG)
1531 && (bval <= MSG_ORDERED_Q_TAG))) {
1532 pSRB->SRBState |= SRB_MSGIN_MULTI;
1533 pSRB->MsgInBuf[0] = bval;
1535 pSRB->pMsgPtr = &pSRB->MsgInBuf[1];
1536 } else if (bval == MSG_MESSAGE_REJECT) {
1537 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1539 if (pSRB->SRBState & DO_SYNC_NEGO) {
1542 } else if (bval == MSG_RESTOREPOINTERS) {
1547 } else { /* minx: */
1548 *pSRB->pMsgPtr = bval;
1551 if ((pSRB->MsgInBuf[0] >= MSG_SIMPLE_Q_TAG)
1552 && (pSRB->MsgInBuf[0] <= MSG_ORDERED_Q_TAG)) {
1553 if (pSRB->MsgCnt == 2) {
1555 pSRB = &amd->SRB_array[pSRB->MsgInBuf[1]];
1556 if (pSRB->SRBState & SRB_DISCONNECT) == 0) {
1557 pSRB = amd->pTmpSRB;
1558 pSRB->SRBState = SRB_UNEXPECT_RESEL;
1559 pDCB->pActiveSRB = pSRB;
1560 pSRB->MsgOutBuf[0] = MSG_ABORT_TAG;
1561 EnableMsgOut2(amd, pSRB);
1563 if (pDCB->DCBFlag & ABORT_DEV_) {
1564 pSRB->SRBState = SRB_ABORT_SENT;
1565 EnableMsgOut1(amd, pSRB);
1567 pDCB->pActiveSRB = pSRB;
1568 pSRB->SRBState = SRB_DATA_XFER;
1571 } else if ((pSRB->MsgInBuf[0] == MSG_EXTENDED)
1572 && (pSRB->MsgCnt == 5)) {
1573 pSRB->SRBState &= ~(SRB_MSGIN_MULTI + DO_SYNC_NEGO);
1574 if ((pSRB->MsgInBuf[1] != 3)
1575 || (pSRB->MsgInBuf[2] != 1)) { /* reject_msg: */
1577 pSRB->MsgInBuf[0] = MSG_MESSAGE_REJECT;
1578 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1579 } else if (!(pSRB->MsgInBuf[3])
1580 || !(pSRB->MsgInBuf[4])) {
1581 set_async: /* set async */
1583 pDCB = pSRB->pSRBDCB;
1584 /* disable sync & sync nego */
1585 pDCB->SyncMode &= ~(SYNC_ENABLE|SYNC_NEGO_DONE);
1586 pDCB->SyncPeriod = 0;
1587 pDCB->SyncOffset = 0;
1589 pDCB->tinfo.goal.period = 0;
1590 pDCB->tinfo.goal.offset = 0;
1592 pDCB->tinfo.current.period = 0;
1593 pDCB->tinfo.current.offset = 0;
1594 pDCB->tinfo.current.width =
1595 MSG_EXT_WDTR_BUS_8_BIT;
1597 pDCB->CtrlR3 = FAST_CLK; /* non_fast */
1598 pDCB->CtrlR4 &= 0x3f;
1599 pDCB->CtrlR4 |= EATER_25NS;
1601 } else {/* set sync */
1603 pDCB = pSRB->pSRBDCB;
1604 /* enable sync & sync nego */
1605 pDCB->SyncMode |= SYNC_ENABLE|SYNC_NEGO_DONE;
1607 /* set sync offset */
1608 pDCB->SyncOffset &= 0x0f0;
1609 pDCB->SyncOffset |= pSRB->MsgInBuf[4];
1611 /* set sync period */
1612 pDCB->MaxNegoPeriod = pSRB->MsgInBuf[3];
1614 wval = (u_int16_t) pSRB->MsgInBuf[3];
1618 if ((wval1 * 25) != wval) {
1621 bval = FAST_CLK|FAST_SCSI;
1622 pDCB->CtrlR4 &= 0x3f;
1627 pDCB->CtrlR4 |= EATER_25NS;
1629 pDCB->CtrlR3 = bval;
1630 pDCB->SyncPeriod = (u_int8_t) wval1;
1632 pDCB->tinfo.goal.period =
1633 tinfo_sync_period[pDCB->SyncPeriod - 4];
1634 pDCB->tinfo.goal.offset = pDCB->SyncOffset;
1635 pDCB->tinfo.current.period =
1636 tinfo_sync_period[pDCB->SyncPeriod - 4];
1637 pDCB->tinfo.current.offset = pDCB->SyncOffset;
1640 * program SCSI control register
1643 amd_write8(amd, SYNCPERIOREG, pDCB->SyncPeriod);
1644 amd_write8(amd, SYNCOFFREG, pDCB->SyncOffset);
1645 amd_write8(amd, CNTLREG3, pDCB->CtrlR3);
1646 amd_write8(amd, CNTLREG4, pDCB->CtrlR4);
1651 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);
1657 amd_DataOutPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1659 DataIO_Comm(amd, pSRB, WRITE_DIRECTION);
1664 amd_DataInPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1666 DataIO_Comm(amd, pSRB, READ_DIRECTION);
1671 DataIO_Comm(struct amd_softc *amd, struct amd_srb *pSRB, u_int ioDir)
1673 struct amd_sg * psgl;
1676 if (pSRB->SGIndex < pSRB->SGcount) {
1677 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD|ioDir);/* |EN_DMA_INT */
1679 if (!pSRB->SGToBeXferLen) {
1680 psgl = pSRB->pSGlist;
1681 pSRB->SGPhysAddr = psgl->SGXPtr;
1682 pSRB->SGToBeXferLen = psgl->SGXLen;
1684 lval = pSRB->SGToBeXferLen;
1685 amd_write8(amd, CTCREG_LOW, lval);
1686 amd_write8(amd, CTCREG_MID, lval >> 8);
1687 amd_write8(amd, CURTXTCNTREG, lval >> 16);
1689 amd_write32(amd, DMA_XferCnt, pSRB->SGToBeXferLen);
1691 amd_write32(amd, DMA_XferAddr, pSRB->SGPhysAddr);
1693 pSRB->SRBState = SRB_DATA_XFER;
1695 amd_write8(amd, SCSICMDREG, DMA_COMMAND|INFO_XFER_CMD);
1697 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD|ioDir); /* |EN_DMA_INT */
1699 amd_write8(amd, DMA_Cmd, DMA_START_CMD|ioDir);/* |EN_DMA_INT */
1700 } else { /* xfer pad */
1701 if (pSRB->SGcount) {
1702 pSRB->AdaptStatus = H_OVER_UNDER_RUN;
1703 pSRB->SRBStatus |= OVER_RUN;
1705 amd_write8(amd, CTCREG_LOW, 0);
1706 amd_write8(amd, CTCREG_MID, 0);
1707 amd_write8(amd, CURTXTCNTREG, 0);
1709 pSRB->SRBState |= SRB_XFERPAD;
1710 amd_write8(amd, SCSICMDREG, DMA_COMMAND|XFER_PAD_BYTE);
1715 amd_CommandPhase1(struct amd_softc *amd, struct amd_srb *srb, u_int scsistat)
1717 amd_write8(amd, SCSICMDREG, RESET_ATN_CMD);
1718 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1720 amdsetupcommand(amd, srb);
1722 srb->SRBState = SRB_COMMAND;
1723 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1728 amd_StatusPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1730 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1731 pSRB->SRBState = SRB_STATUS;
1732 amd_write8(amd, SCSICMDREG, INITIATOR_CMD_CMPLTE);
1737 amd_MsgOutPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1739 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1741 if (amd->msgout_len == 0) {
1742 amd->msgout_buf[0] = MSG_NOOP;
1743 amd->msgout_len = 1;
1745 amd_write8_multi(amd, SCSIFIFOREG, amd->msgout_buf, amd->msgout_len);
1746 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1751 amd_MsgInPhase1(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1753 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
1754 amd_write8(amd, SCSICMDREG, INFO_XFER_CMD);
1759 amd_NopPhase(struct amd_softc *amd, struct amd_srb *pSRB, u_int scsistat)
1765 amd_Disconnect(struct amd_softc * amd)
1767 struct amd_srb *srb;
1771 srb = amd->active_srb;
1772 amd->active_srb = NULL;
1773 amd->last_phase = SCSI_BUS_FREE;
1774 amd_write8(amd, SCSICMDREG, EN_SEL_RESEL);
1775 target = amd->cur_target;
1779 /* Invalid reselection */
1781 } else if (srb->SRBState & SRB_ABORT_SENT) {
1782 /* Clean up and done this srb */
1784 while (( = TAILQ_FIRST(&amd->running_srbs)) != NULL) {
1785 /* XXX What about "done'ing" these srbs??? */
1786 if (pSRB->pSRBDCB == pDCB) {
1787 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
1788 TAILQ_INSERT_HEAD(&amd->free_srbs, pSRB, links);
1794 if ((srb->SRBState & (SRB_START | SRB_MSGOUT))
1795 || !(srb->SRBState & (SRB_DISCONNECT | SRB_COMPLETED))) {
1796 srb->TargetStatus = AMD_SCSI_STAT_SEL_TIMEOUT;
1798 } else if (srb->SRBState & SRB_DISCONNECT) {
1799 if (!(srb->pccb->ccb_h.flags & CAM_TAG_ACTION_VALID))
1800 amd->untagged_srbs[target][lun] = srb;
1802 } else if (srb->SRBState & SRB_COMPLETED) {
1804 srb->SRBState = SRB_FREE;
1812 amd_Reselect(struct amd_softc *amd)
1814 struct amd_target_info *tinfo;
1815 u_int16_t disc_count;
1817 amd_clear_msg_state(amd);
1818 if (amd->active_srb != NULL) {
1819 /* Requeue the SRB for our attempted Selection */
1820 TAILQ_REMOVE(&amd->running_srbs, amd->active_srb, links);
1821 TAILQ_INSERT_HEAD(&amd->waiting_srbs, amd->active_srb, links);
1822 amd->active_srb = NULL;
1825 amd->cur_target = amd_read8(amd, SCSIFIFOREG);
1826 amd->cur_target ^= amd->HostID_Bit;
1827 amd->cur_target = ffs(amd->cur_target) - 1;
1828 amd->cur_lun = amd_read8(amd, SCSIFIFOREG) & 7;
1829 tinfo = &amd->tinfo[amd->cur_target];
1830 amd->active_srb = amd->untagged_srbs[amd->cur_target][amd->cur_lun];
1831 disc_count = amd->disc_count[amd->cur_target][amd->cur_lun];
1832 if (disc_count == 0) {
1833 kprintf("amd%d: Unexpected reselection for target %d, "
1834 "Issuing Abort\n", amd->unit, amd->cur_target);
1835 amd->msgout_buf[0] = MSG_ABORT;
1836 amd->msgout_len = 1;
1837 amd_write8(amd, SCSICMDREG, SET_ATN_CMD);
1839 if (amd->active_srb != NULL) {
1840 amd->disc_count[amd->cur_target][amd->cur_lun]--;
1841 amd->untagged_srbs[amd->cur_target][amd->cur_lun] = NULL;
1844 amd_write8(amd, SCSIDESTIDREG, amd->cur_target);
1845 amd_write8(amd, SYNCPERIOREG, tinfo->sync_period_reg);
1846 amd_write8(amd, SYNCOFFREG, tinfo->sync_offset_reg);
1847 amd_write8(amd, CNTLREG1, tinfo->CtrlR1);
1848 amd_write8(amd, CNTLREG3, tinfo->CtrlR3);
1849 amd_write8(amd, CNTLREG4, tinfo->CtrlR4);
1850 amd_write8(amd, SCSICMDREG, MSG_ACCEPTED_CMD);/* drop /ACK */
1851 amd->last_phase = SCSI_NOP0;
1855 SRBdone(struct amd_softc *amd, struct amd_srb *pSRB)
1857 u_int8_t bval, i, status;
1859 struct ccb_scsiio *pcsio;
1860 struct amd_sg *ptr2;
1864 pcsio = &pccb->csio;
1866 CAM_DEBUG(pccb->ccb_h.path, CAM_DEBUG_TRACE,
1867 ("SRBdone - TagNumber %d\n", pSRB->TagNumber));
1869 if ((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1870 bus_dmasync_op_t op;
1872 if ((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1873 op = BUS_DMASYNC_POSTREAD;
1875 op = BUS_DMASYNC_POSTWRITE;
1876 bus_dmamap_sync(amd->buffer_dmat, pSRB->dmamap, op);
1877 bus_dmamap_unload(amd->buffer_dmat, pSRB->dmamap);
1880 status = pSRB->TargetStatus;
1881 pccb->ccb_h.status = CAM_REQ_CMP;
1882 if (pSRB->SRBFlag & AUTO_REQSENSE) {
1883 pSRB->SRBFlag &= ~AUTO_REQSENSE;
1884 pSRB->AdaptStatus = 0;
1885 pSRB->TargetStatus = SCSI_STATUS_CHECK_COND;
1887 if (status == SCSI_STATUS_CHECK_COND) {
1888 pccb->ccb_h.status = CAM_SEL_TIMEOUT;
1891 *((u_int32_t *)&(pSRB->CmdBlock[0])) = pSRB->Segment0[0];
1893 pcsio->sense_resid = pcsio->sense_len
1894 - pSRB->TotalXferredLen;
1895 pSRB->TotalXferredLen = pSRB->Segment1[1];
1896 if (pSRB->TotalXferredLen) {
1898 pcsio->resid = pcsio->dxfer_len
1899 - pSRB->TotalXferredLen;
1900 /* The resid field contains valid data */
1901 /* Flush resid bytes on complete */
1903 pcsio->scsi_status = SCSI_STATUS_CHECK_COND;
1905 pccb->ccb_h.status = CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR;
1909 if (status == SCSI_STATUS_CHECK_COND) {
1911 if ((pSRB->SGIndex < pSRB->SGcount)
1912 && (pSRB->SGcount) && (pSRB->SGToBeXferLen)) {
1913 bval = pSRB->SGcount;
1914 swlval = pSRB->SGToBeXferLen;
1915 ptr2 = pSRB->pSGlist;
1917 for (i = pSRB->SGIndex + 1; i < bval; i++) {
1918 swlval += ptr2->SGXLen;
1922 pcsio->resid = swlval;
1925 kprintf("XferredLen=%8x,NotYetXferLen=%8x,",
1926 pSRB->TotalXferredLen, swlval);
1929 if ((pcsio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
1931 kprintf("RequestSense..................\n");
1933 RequestSense(amd, pSRB);
1936 pcsio->scsi_status = SCSI_STATUS_CHECK_COND;
1937 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1939 } else if (status == SCSI_STATUS_QUEUE_FULL) {
1940 pSRB->AdaptStatus = 0;
1941 pSRB->TargetStatus = 0;
1942 pcsio->scsi_status = SCSI_STATUS_QUEUE_FULL;
1943 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1945 } else if (status == AMD_SCSI_STAT_SEL_TIMEOUT) {
1946 pSRB->AdaptStatus = H_SEL_TIMEOUT;
1947 pSRB->TargetStatus = 0;
1949 pcsio->scsi_status = AMD_SCSI_STAT_SEL_TIMEOUT;
1950 pccb->ccb_h.status = CAM_SEL_TIMEOUT;
1951 } else if (status == SCSI_STATUS_BUSY) {
1953 kprintf("DC390: target busy at %s %d\n",
1954 __FILE__, __LINE__);
1956 pcsio->scsi_status = SCSI_STATUS_BUSY;
1957 pccb->ccb_h.status = CAM_SCSI_BUSY;
1958 } else if (status == SCSI_STATUS_RESERV_CONFLICT) {
1960 kprintf("DC390: target reserved at %s %d\n",
1961 __FILE__, __LINE__);
1963 pcsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
1964 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR; /* XXX */
1966 pSRB->AdaptStatus = 0;
1968 kprintf("DC390: driver stuffup at %s %d\n",
1969 __FILE__, __LINE__);
1971 pccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1974 status = pSRB->AdaptStatus;
1975 if (status & H_OVER_UNDER_RUN) {
1976 pSRB->TargetStatus = 0;
1978 pccb->ccb_h.status = CAM_DATA_RUN_ERR;
1979 } else if (pSRB->SRBStatus & PARITY_ERROR) {
1981 kprintf("DC390: driver stuffup %s %d\n",
1982 __FILE__, __LINE__);
1984 /* Driver failed to perform operation */
1985 pccb->ccb_h.status = CAM_UNCOR_PARITY;
1986 } else { /* No error */
1987 pSRB->AdaptStatus = 0;
1988 pSRB->TargetStatus = 0;
1990 /* there is no error, (sense is invalid) */
1995 if ((pccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1996 /* CAM request not yet complete =>device_Q frozen */
1997 xpt_freeze_devq(pccb->ccb_h.path, 1);
1998 pccb->ccb_h.status |= CAM_DEV_QFRZN;
2000 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
2001 TAILQ_INSERT_HEAD(&amd->free_srbs, pSRB, links);
2009 amd_ResetSCSIBus(struct amd_softc * amd)
2012 amd->ACBFlag |= RESET_DEV;
2013 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
2014 amd_write8(amd, SCSICMDREG, RST_SCSI_BUS_CMD);
2020 amd_ScsiRstDetect(struct amd_softc * amd)
2025 kprintf("amd_ScsiRstDetect \n");
2029 while (--wlval) { /* delay 1 sec */
2034 amd_write8(amd, DMA_Cmd, DMA_IDLE_CMD);
2035 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
2037 if (amd->ACBFlag & RESET_DEV) {
2038 amd->ACBFlag |= RESET_DONE;
2040 amd->ACBFlag |= RESET_DETECT;
2042 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
2043 AMD_TAG_WILDCARD, &amd->running_srbs,
2044 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
2045 amdcompletematch(amd, CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD,
2046 AMD_TAG_WILDCARD, &amd->waiting_srbs,
2047 CAM_DEV_QFRZN|CAM_SCSI_BUS_RESET);
2048 amd->active_srb = NULL;
2057 RequestSense(struct amd_softc *amd, struct amd_srb *pSRB)
2060 struct ccb_scsiio *pcsio;
2063 pcsio = &pccb->csio;
2065 pSRB->SRBFlag |= AUTO_REQSENSE;
2066 pSRB->Segment0[0] = *((u_int32_t *) & (pSRB->CmdBlock[0]));
2067 pSRB->Segment0[1] = *((u_int32_t *) & (pSRB->CmdBlock[4]));
2068 pSRB->Segment1[0] = (pSRB->ScsiCmdLen << 8) + pSRB->SGcount;
2069 pSRB->Segment1[1] = pSRB->TotalXferredLen;
2071 pSRB->AdaptStatus = 0;
2072 pSRB->TargetStatus = 0;
2074 pSRB->Segmentx.SGXPtr = (u_int32_t) vtophys(&pcsio->sense_data);
2075 pSRB->Segmentx.SGXLen = (u_int32_t) pcsio->sense_len;
2077 pSRB->pSGlist = &pSRB->Segmentx;
2081 *((u_int32_t *) & (pSRB->CmdBlock[0])) = 0x00000003;
2082 pSRB->CmdBlock[1] = pSRB->pccb->ccb_h.target_lun << 5;
2083 *((u_int16_t *) & (pSRB->CmdBlock[4])) = pcsio->sense_len;
2084 pSRB->ScsiCmdLen = 6;
2086 pSRB->TotalXferredLen = 0;
2087 pSRB->SGToBeXferLen = 0;
2088 if (amdstart(amd, pSRB) != 0) {
2089 TAILQ_REMOVE(&amd->running_srbs, pSRB, links);
2090 TAILQ_INSERT_HEAD(&amd->waiting_srbs, pSRB, links);
2095 amd_InvalidCmd(struct amd_softc * amd)
2097 struct amd_srb *srb;
2099 srb = amd->active_srb;
2100 if (srb->SRBState & (SRB_START|SRB_MSGOUT))
2101 amd_write8(amd, SCSICMDREG, CLEAR_FIFO_CMD);
2105 amd_linkSRB(struct amd_softc *amd)
2108 struct amd_srb *psrb;
2111 count = amd->SRBCount;
2113 for (i = 0; i < count; i++) {
2114 psrb = (struct amd_srb *)&amd->SRB_array[i];
2115 psrb->TagNumber = i;
2118 * Create the dmamap. This is no longer optional!
2120 error = bus_dmamap_create(amd->buffer_dmat, 0, &psrb->dmamap);
2122 device_printf(amd->dev, "Error %d creating buffer "
2123 "dmamap!\n", error);
2126 TAILQ_INSERT_TAIL(&amd->free_srbs, psrb, links);
2131 amd_EnDisableCE(struct amd_softc *amd, int mode, int *regval)
2133 if (mode == ENABLE_CE) {
2138 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2139 if (mode == DISABLE_CE) {
2140 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2146 amd_EEpromOutDI(struct amd_softc *amd, int *regval, int Carry)
2154 pci_write_config(amd->dev, *regval, bval, /*bytes*/1);
2158 pci_write_config(amd->dev, *regval, bval, /*bytes*/1);
2160 pci_write_config(amd->dev, *regval, 0, /*bytes*/1);
2165 amd_EEpromInDO(struct amd_softc *amd)
2167 pci_write_config(amd->dev, 0x80, 0x80, /*bytes*/1);
2169 pci_write_config(amd->dev, 0x80, 0x40, /*bytes*/1);
2171 if (pci_read_config(amd->dev, 0, /*bytes*/1) == 0x22)
2177 EEpromGetData1(struct amd_softc *amd)
2184 for (i = 0; i < 16; i++) {
2186 carryFlag = amd_EEpromInDO(amd);
2193 amd_Prepare(struct amd_softc *amd, int *regval, u_int8_t EEpromCmd)
2200 for (i = 0; i < 9; i++) {
2201 amd_EEpromOutDI(amd, regval, carryFlag);
2202 carryFlag = (EEpromCmd & j) ? 1 : 0;
2208 amd_ReadEEprom(struct amd_softc *amd)
2215 ptr = (u_int16_t *)&amd->eepromBuf[0];
2217 for (i = 0; i < 0x40; i++) {
2218 amd_EnDisableCE(amd, ENABLE_CE, ®val);
2219 amd_Prepare(amd, ®val, cmd);
2220 *ptr = EEpromGetData1(amd);
2223 amd_EnDisableCE(amd, DISABLE_CE, ®val);
2228 amd_load_defaults(struct amd_softc *amd)
2232 bzero(&amd->eepromBuf, sizeof amd->eepromBuf);
2233 for (target = 0; target < MAX_SCSI_ID; target++)
2234 amd->eepromBuf[target << 2] =
2235 (TAG_QUEUING|EN_DISCONNECT|SYNC_NEGO|PARITY_CHK);
2236 amd->eepromBuf[EE_ADAPT_SCSI_ID] = 7;
2237 amd->eepromBuf[EE_MODE2] = ACTIVE_NEGATION|LUN_CHECK|GREATER_1G;
2238 amd->eepromBuf[EE_TAG_CMD_NUM] = 4;
2242 amd_load_eeprom_or_defaults(struct amd_softc *amd)
2244 u_int16_t wval, *ptr;
2247 amd_ReadEEprom(amd);
2249 ptr = (u_int16_t *) & amd->eepromBuf[0];
2250 for (i = 0; i < EE_DATA_SIZE; i += 2, ptr++)
2253 if (wval != EE_CHECKSUM) {
2255 kprintf("amd%d: SEEPROM data unavailable. "
2256 "Using default device parameters.\n",
2258 amd_load_defaults(amd);
2263 **********************************************************************
2264 * Function : static int amd_init (struct Scsi_Host *host)
2265 * Purpose : initialize the internal structures for a given SCSI host
2266 * Inputs : host - pointer to this host adapter's structure/
2267 **********************************************************************
2270 amd_init(device_t dev)
2272 struct amd_softc *amd = device_get_softc(dev);
2273 struct resource *iores;
2277 rid = PCI_BASE_ADDR0;
2278 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
2280 if (iores == NULL) {
2282 kprintf("amd_init: bus_alloc_resource failure!\n");
2285 amd->tag = rman_get_bustag(iores);
2286 amd->bsh = rman_get_bushandle(iores);
2288 /* DMA tag for mapping buffers into device visible space. */
2289 if (bus_dma_tag_create(/*parent_dmat*/NULL, /*alignment*/1,
2291 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2292 /*highaddr*/BUS_SPACE_MAXADDR,
2293 /*filter*/NULL, /*filterarg*/NULL,
2294 /*maxsize*/MAXBSIZE, /*nsegments*/AMD_NSEG,
2295 /*maxsegsz*/AMD_MAXTRANSFER_SIZE,
2296 /*flags*/BUS_DMA_ALLOCNOW,
2297 &amd->buffer_dmat) != 0) {
2299 kprintf("amd_init: bus_dma_tag_create failure!\n");
2302 TAILQ_INIT(&amd->free_srbs);
2303 TAILQ_INIT(&amd->running_srbs);
2304 TAILQ_INIT(&amd->waiting_srbs);
2305 amd->last_phase = SCSI_BUS_FREE;
2307 amd->unit = device_get_unit(dev);
2308 amd->SRBCount = MAX_SRB_CNT;
2310 amd_load_eeprom_or_defaults(amd);
2312 if (amd->eepromBuf[EE_MODE2] & LUN_CHECK) {
2317 amd->AdaptSCSIID = amd->eepromBuf[EE_ADAPT_SCSI_ID];
2318 amd->HostID_Bit = (1 << amd->AdaptSCSIID);
2319 amd->AdaptSCSILUN = 0;
2320 /* (eepromBuf[EE_TAG_CMD_NUM]) << 2; */
2322 amd->Gmode2 = amd->eepromBuf[EE_MODE2];
2324 for (i = 0; i <= amd->max_id; i++) {
2326 if (amd->AdaptSCSIID != i) {
2327 struct amd_target_info *tinfo;
2330 tinfo = &amd->tinfo[i];
2331 prom = (PEEprom)&amd->eepromBuf[i << 2];
2332 if ((prom->EE_MODE1 & EN_DISCONNECT) != 0) {
2333 tinfo->disc_tag |= AMD_USR_DISCENB;
2334 if ((prom->EE_MODE1 & TAG_QUEUING) != 0)
2335 tinfo->disc_tag |= AMD_USR_TAGENB;
2337 if ((prom->EE_MODE1 & SYNC_NEGO) != 0) {
2338 tinfo->user.period =
2339 eeprom_period[prom->EE_SPEED];
2340 tinfo->user.offset = AMD_MAX_SYNC_OFFSET;
2342 tinfo->CtrlR1 = amd->AdaptSCSIID;
2343 if ((prom->EE_MODE1 & PARITY_CHK) != 0)
2344 tinfo->CtrlR1 |= PARITY_ERR_REPO;
2345 tinfo->CtrlR3 = FAST_CLK;
2346 tinfo->CtrlR4 = EATER_25NS;
2347 if ((amd->eepromBuf[EE_MODE2] & ACTIVE_NEGATION) != 0)
2348 tinfo->CtrlR4 |= NEGATE_REQACKDATA;
2351 amd_write8(amd, SCSITIMEOUTREG, 153); /* 250ms selection timeout */
2352 /* Conversion factor = 0 , 40MHz clock */
2353 amd_write8(amd, CLKFACTREG, CLK_FREQ_40MHZ);
2354 /* NOP cmd - clear command register */
2355 amd_write8(amd, SCSICMDREG, NOP_CMD);
2356 amd_write8(amd, CNTLREG2, EN_FEATURE|EN_SCSI2_CMD);
2357 amd_write8(amd, CNTLREG3, FAST_CLK);
2359 if (amd->eepromBuf[EE_MODE2] & ACTIVE_NEGATION) {
2360 bval |= NEGATE_REQACKDATA;
2362 amd_write8(amd, CNTLREG4, bval);
2364 /* Disable SCSI bus reset interrupt */
2365 amd_write8(amd, CNTLREG1, DIS_INT_ON_SCSI_RST);
2371 * attach and init a host adapter
2374 amd_attach(device_t dev)
2376 struct cam_devq *devq; /* Device Queue to use for this SIM */
2378 struct amd_softc *amd = device_get_softc(dev);
2379 int unit = device_get_unit(dev);
2382 struct resource *irqres;
2384 if (amd_init(dev)) {
2386 kprintf("amd_attach: amd_init failure!\n");
2390 /* Reset Pending INT */
2391 intstat = amd_read8(amd, INTSTATREG);
2393 /* After setting up the adapter, map our interrupt */
2395 irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2396 RF_SHAREABLE | RF_ACTIVE);
2397 if (irqres == NULL ||
2398 bus_setup_intr(dev, irqres, 0, amd_intr, amd, &ih, NULL)
2401 kprintf("amd%d: unable to register interrupt handler!\n",
2407 * Now let the CAM generic SCSI layer find the SCSI devices on
2408 * the bus * start queue to reset to the idle loop. *
2409 * Create device queue of SIM(s) * (MAX_START_JOB - 1) :
2410 * max_sim_transactions
2412 devq = cam_simq_alloc(MAX_START_JOB);
2415 kprintf("amd_attach: cam_simq_alloc failure!\n");
2419 amd->psim = cam_sim_alloc(amd_action, amd_poll, "amd",
2420 amd, amd->unit, &sim_mplock, 1,
2421 MAX_TAGS_CMD_QUEUE, devq);
2422 cam_simq_release(devq);
2423 if (amd->psim == NULL) {
2425 kprintf("amd_attach: cam_sim_alloc failure!\n");
2429 if (xpt_bus_register(amd->psim, 0) != CAM_SUCCESS) {
2430 cam_sim_free(amd->psim);
2432 kprintf("amd_attach: xpt_bus_register failure!\n");
2436 if (xpt_create_path(&amd->ppath, /* periph */ NULL,
2437 cam_sim_path(amd->psim), CAM_TARGET_WILDCARD,
2438 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2439 xpt_bus_deregister(cam_sim_path(amd->psim));
2440 cam_sim_free(amd->psim);
2442 kprintf("amd_attach: xpt_create_path failure!\n");
2450 amd_probe(device_t dev)
2452 if (pci_get_devid(dev) == PCI_DEVICE_ID_AMD53C974) {
2453 device_set_desc(dev,
2454 "Tekram DC390(T)/AMD53c974 SCSI Host Adapter");
2460 static device_method_t amd_methods[] = {
2461 /* Device interface */
2462 DEVMETHOD(device_probe, amd_probe),
2463 DEVMETHOD(device_attach, amd_attach),
2467 static driver_t amd_driver = {
2468 "amd", amd_methods, sizeof(struct amd_softc)
2471 static devclass_t amd_devclass;
2472 DRIVER_MODULE(amd, pci, amd_driver, amd_devclass, NULL, NULL);