advlib.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 2,047 行 · 第 1/4 页

C
2,047 行
字号
		printf("Addr 0x%x: 0x%04x\n", s_addr, *wbuf);#endif	}	*wbuf = adv_read_eeprom_16(adv, s_addr);	return (sum);}intadv_set_eeprom_config(struct adv_softc *adv,		      struct adv_eeprom_config *eeprom_config){	int	retry;	retry = 0;	while (1) {		if (adv_set_eeprom_config_once(adv, eeprom_config) == 0) {			break;		}		if (++retry > ADV_EEPROM_MAX_RETRY) {			break;		}	}	return (retry > ADV_EEPROM_MAX_RETRY);}intadv_reset_chip_and_scsi_bus(struct adv_softc *adv){	adv_stop_chip(adv);	ADV_OUTB(adv, ADV_CHIP_CTRL,		 ADV_CC_CHIP_RESET | ADV_CC_SCSI_RESET | ADV_CC_HALT);	DELAY(200 * 1000);	adv_set_chip_ih(adv, ADV_INS_RFLAG_WTM);	adv_set_chip_ih(adv, ADV_INS_HALT);	ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_CHIP_RESET | ADV_CC_HALT);	ADV_OUTB(adv, ADV_CHIP_CTRL, ADV_CC_HALT);	DELAY(200 * 1000);	return (adv_is_chip_halted(adv));}intadv_test_external_lram(struct adv_softc* adv){	u_int16_t	q_addr;	u_int16_t	saved_value;	int		success;	success = 0;	q_addr = ADV_QNO_TO_QADDR(241);	saved_value = adv_read_lram_16(adv, q_addr);	if (adv_write_and_verify_lram_16(adv, q_addr, 0x55AA) == 0) {		success = 1;		adv_write_lram_16(adv, q_addr, saved_value);	}	return (success);}intadv_init_lram_and_mcode(struct adv_softc *adv){	u_int32_t	retval;	adv_disable_interrupt(adv);	adv_init_lram(adv);	retval = adv_load_microcode(adv, 0, (u_int16_t *)adv_mcode,				    adv_mcode_size);	if (retval != adv_mcode_chksum) {		printf("adv%d: Microcode download failed checksum!\n",		       adv->unit);		return (1);	}		if (adv_init_microcode_var(adv) != 0)		return (1);	adv_enable_interrupt(adv);	return (0);}u_int8_tadv_get_chip_irq(struct adv_softc *adv){	u_int16_t	cfg_lsw;	u_int8_t	chip_irq;	cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW);	if ((adv->type & ADV_VL) != 0) {		chip_irq = (u_int8_t)(((cfg_lsw >> 2) & 0x07));		if ((chip_irq == 0) ||		    (chip_irq == 4) ||		    (chip_irq == 7)) {			return (0);		}		return (chip_irq + (ADV_MIN_IRQ_NO - 1));	}	chip_irq = (u_int8_t)(((cfg_lsw >> 2) & 0x03));	if (chip_irq == 3)		chip_irq += 2;	return (chip_irq + ADV_MIN_IRQ_NO);}u_int8_tadv_set_chip_irq(struct adv_softc *adv, u_int8_t irq_no){	u_int16_t	cfg_lsw;	if ((adv->type & ADV_VL) != 0) {		if (irq_no != 0) {			if ((irq_no < ADV_MIN_IRQ_NO)			 || (irq_no > ADV_MAX_IRQ_NO)) {				irq_no = 0;			} else {				irq_no -= ADV_MIN_IRQ_NO - 1;			}		}		cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFE3;		cfg_lsw |= 0x0010;		ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);		adv_toggle_irq_act(adv);		cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFE0;		cfg_lsw |= (irq_no & 0x07) << 2;		ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);		adv_toggle_irq_act(adv);	} else if ((adv->type & ADV_ISA) != 0) {		if (irq_no == 15)			irq_no -= 2;		irq_no -= ADV_MIN_IRQ_NO;		cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW) & 0xFFF3;		cfg_lsw |= (irq_no & 0x03) << 2;		ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);	}	return (adv_get_chip_irq(adv));}voidadv_set_chip_scsiid(struct adv_softc *adv, int new_id){	u_int16_t cfg_lsw;	cfg_lsw = ADV_INW(adv, ADV_CONFIG_LSW);	if (ADV_CONFIG_SCSIID(cfg_lsw) == new_id)		return;    	cfg_lsw &= ~ADV_CFG_LSW_SCSIID;	cfg_lsw |= (new_id & ADV_MAX_TID) << ADV_CFG_LSW_SCSIID_SHIFT;	ADV_OUTW(adv, ADV_CONFIG_LSW, cfg_lsw);}intadv_execute_scsi_queue(struct adv_softc *adv, struct adv_scsi_q *scsiq,		       u_int32_t datalen){	struct		adv_target_transinfo* tinfo;	u_int32_t	*p_data_addr;	u_int32_t	*p_data_bcount;	int		disable_syn_offset_one_fix;	int		retval;	u_int		n_q_required;	u_int32_t	addr;	u_int8_t	sg_entry_cnt;	u_int8_t	target_ix;	u_int8_t	sg_entry_cnt_minus_one;	u_int8_t	tid_no;	scsiq->q1.q_no = 0;	retval = 1;  /* Default to error case */	target_ix = scsiq->q2.target_ix;	tid_no = ADV_TIX_TO_TID(target_ix);	tinfo = &adv->tinfo[tid_no];	if (scsiq->cdbptr[0] == REQUEST_SENSE) {		/* Renegotiate if appropriate. */		adv_set_syncrate(adv, /*struct cam_path */NULL,				 tid_no, /*period*/0, /*offset*/0,				 ADV_TRANS_CUR);		if (tinfo->current.period != tinfo->goal.period) {			adv_msgout_sdtr(adv, tinfo->goal.period,					tinfo->goal.offset);			scsiq->q1.cntl |= (QC_MSG_OUT | QC_URGENT);		}	}	if ((scsiq->q1.cntl & QC_SG_HEAD) != 0) {		sg_entry_cnt = scsiq->sg_head->entry_cnt;		sg_entry_cnt_minus_one = sg_entry_cnt - 1;#ifdef DIAGNOSTIC		if (sg_entry_cnt <= 1) 			panic("adv_execute_scsi_queue: Queue "			      "with QC_SG_HEAD set but %d segs.", sg_entry_cnt);		if (sg_entry_cnt > ADV_MAX_SG_LIST)			panic("adv_execute_scsi_queue: "			      "Queue with too many segs.");		if (adv->type & (ADV_ISA | ADV_VL | ADV_EISA)) {			int i;			for (i = 0; i < sg_entry_cnt_minus_one; i++) {				addr = scsiq->sg_head->sg_list[i].addr +				       scsiq->sg_head->sg_list[i].bytes;				if ((addr & 0x0003) != 0)					panic("adv_execute_scsi_queue: SG "					      "with odd address or byte count");			}		}#endif		p_data_addr =		    &scsiq->sg_head->sg_list[sg_entry_cnt_minus_one].addr;		p_data_bcount =		    &scsiq->sg_head->sg_list[sg_entry_cnt_minus_one].bytes;		n_q_required = adv_sgcount_to_qcount(sg_entry_cnt);		scsiq->sg_head->queue_cnt = n_q_required - 1;	} else {		p_data_addr = &scsiq->q1.data_addr;		p_data_bcount = &scsiq->q1.data_cnt;		n_q_required = 1;	}	disable_syn_offset_one_fix = FALSE;	if ((adv->fix_asyn_xfer & scsiq->q1.target_id) != 0	 && (adv->fix_asyn_xfer_always & scsiq->q1.target_id) == 0) {		if (datalen != 0) {			if (datalen < 512) {				disable_syn_offset_one_fix = TRUE;			} else {				if (scsiq->cdbptr[0] == INQUIRY				 || scsiq->cdbptr[0] == REQUEST_SENSE				 || scsiq->cdbptr[0] == READ_CAPACITY				 || scsiq->cdbptr[0] == MODE_SELECT_6 				 || scsiq->cdbptr[0] == MODE_SENSE_6				 || scsiq->cdbptr[0] == MODE_SENSE_10 				 || scsiq->cdbptr[0] == MODE_SELECT_10 				 || scsiq->cdbptr[0] == READ_TOC) {					disable_syn_offset_one_fix = TRUE;				}			}		}	}	if (disable_syn_offset_one_fix) {		scsiq->q2.tag_code &=		    ~(MSG_SIMPLE_Q_TAG|MSG_HEAD_OF_Q_TAG|MSG_ORDERED_Q_TAG);		scsiq->q2.tag_code |= (ADV_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX				     | ADV_TAG_FLAG_DISABLE_DISCONNECT);	}	if ((adv->bug_fix_control & ADV_BUG_FIX_IF_NOT_DWB) != 0	 && (scsiq->cdbptr[0] == READ_10 || scsiq->cdbptr[0] == READ_6)) {		u_int8_t extra_bytes;		addr = *p_data_addr + *p_data_bcount;		extra_bytes = addr & 0x0003;		if (extra_bytes != 0		 && ((scsiq->q1.cntl & QC_SG_HEAD) != 0		  || (scsiq->q1.data_cnt & 0x01FF) == 0)) {			scsiq->q2.tag_code |= ADV_TAG_FLAG_EXTRA_BYTES;			scsiq->q1.extra_bytes = extra_bytes;			*p_data_bcount -= extra_bytes;		}	}	if ((adv_get_num_free_queues(adv, n_q_required) >= n_q_required)	 || ((scsiq->q1.cntl & QC_URGENT) != 0))		retval = adv_send_scsi_queue(adv, scsiq, n_q_required);		return (retval);}u_int8_tadv_copy_lram_doneq(struct adv_softc *adv, u_int16_t q_addr,		    struct adv_q_done_info *scsiq, u_int32_t max_dma_count){	u_int16_t val;	u_int8_t  sg_queue_cnt;	adv_get_q_info(adv, q_addr + ADV_SCSIQ_DONE_INFO_BEG,		       (u_int16_t *)scsiq,		       (sizeof(scsiq->d2) + sizeof(scsiq->d3)) / 2);#if BYTE_ORDER == BIG_ENDIAN	adv_adj_endian_qdone_info(scsiq);#endif	val = adv_read_lram_16(adv, q_addr + ADV_SCSIQ_B_STATUS);	scsiq->q_status = val & 0xFF;	scsiq->q_no = (val >> 8) & 0XFF;	val = adv_read_lram_16(adv, q_addr + ADV_SCSIQ_B_CNTL);	scsiq->cntl = val & 0xFF;	sg_queue_cnt = (val >> 8) & 0xFF;	val = adv_read_lram_16(adv,q_addr + ADV_SCSIQ_B_SENSE_LEN);	scsiq->sense_len = val & 0xFF;	scsiq->extra_bytes = (val >> 8) & 0xFF;	/*	 * XXX 	 * Due to a bug in accessing LRAM on the 940UA, we only pull	 * the low 16bits of residual information.  In the future, we'll	 * want to allow transfers larger than 64K, but hopefully we'll	 * get a new firmware revision from AdvanSys that address this	 * problem before we up the transfer size.	 */	scsiq->remain_bytes =	    adv_read_lram_16(adv, q_addr + ADV_SCSIQ_DW_REMAIN_XFER_CNT);	/*	 * XXX Is this just a safeguard or will the counter really	 * have bogus upper bits?	 */	scsiq->remain_bytes &= max_dma_count;	return (sg_queue_cnt);}intadv_start_chip(struct adv_softc *adv){	ADV_OUTB(adv, ADV_CHIP_CTRL, 0);	if ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_HALTED) != 0)		return (0);	return (1);}intadv_stop_execution(struct adv_softc *adv){	int count;	count = 0;	if (adv_read_lram_8(adv, ADV_STOP_CODE_B) == 0) {		adv_write_lram_8(adv, ADV_STOP_CODE_B,				 ADV_STOP_REQ_RISC_STOP);		do {			if (adv_read_lram_8(adv, ADV_STOP_CODE_B) &				ADV_STOP_ACK_RISC_STOP) {				return (1);			}			DELAY(1000);		} while (count++ < 20);	}	return (0);}intadv_is_chip_halted(struct adv_softc *adv){	if ((ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_HALTED) != 0) {		if ((ADV_INB(adv, ADV_CHIP_CTRL) & ADV_CC_HALT) != 0) {			return (1);		}	}	return (0);}/* * XXX The numeric constants and the loops in this routine * need to be documented. */voidadv_ack_interrupt(struct adv_softc *adv){	u_int8_t	host_flag;	u_int8_t	risc_flag;	int		loop;	loop = 0;	do {		risc_flag = adv_read_lram_8(adv, ADVV_RISC_FLAG_B);		if (loop++ > 0x7FFF) {			break;		}	} while ((risc_flag & ADV_RISC_FLAG_GEN_INT) != 0);	host_flag = adv_read_lram_8(adv, ADVV_HOST_FLAG_B);	adv_write_lram_8(adv, ADVV_HOST_FLAG_B,			 host_flag | ADV_HOST_FLAG_ACK_INT);	ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_INT_ACK);	loop = 0;	while (ADV_INW(adv, ADV_CHIP_STATUS) & ADV_CSW_INT_PENDING) {		ADV_OUTW(adv, ADV_CHIP_STATUS, ADV_CIW_INT_ACK);		if (loop++ > 3) {			break;		}	}	adv_write_lram_8(adv, ADVV_HOST_FLAG_B, host_flag);}/* * Handle all conditions that may halt the chip waiting * for us to intervene. */voidadv_isr_chip_halted(struct adv_softc *adv){	u_int16_t	  int_halt_code;	u_int16_t	  halt_q_addr;	target_bit_vector target_mask;	target_bit_vector scsi_busy;	u_int8_t	  halt_qp;	u_int8_t	  target_ix;	u_int8_t	  q_cntl;	u_int8_t	  tid_no;	int_halt_code = adv_read_lram_16(adv, ADVV_HALTCODE_W);	halt_qp = adv_read_lram_8(adv, ADVV_CURCDB_B);	halt_q_addr = ADV_QNO_TO_QADDR(halt_qp);	target_ix = adv_read_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_TARGET_IX);	q_cntl = adv_read_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL);	tid_no = ADV_TIX_TO_TID(target_ix);	target_mask = ADV_TID_TO_TARGET_MASK(tid_no);	if (int_halt_code == ADV_HALT_DISABLE_ASYN_USE_SYN_FIX) {		/*		 * Temporarily disable the async fix by removing		 * this target from the list of affected targets,		 * setting our async rate, and then putting us		 * back into the mask.		 */		adv->fix_asyn_xfer &= ~target_mask;		adv_set_syncrate(adv, /*struct cam_path */NULL,				 tid_no, /*period*/0, /*offset*/0,				 ADV_TRANS_ACTIVE);		adv->fix_asyn_xfer |= target_mask;	} else if (int_halt_code == ADV_HALT_ENABLE_ASYN_USE_SYN_FIX) {		adv_set_syncrate(adv, /*struct cam_path */NULL,				 tid_no, /*period*/0, /*offset*/0,				 ADV_TRANS_ACTIVE);	} else if (int_halt_code == ADV_HALT_EXTMSG_IN) {		adv_handle_extmsg_in(adv, halt_q_addr, q_cntl,				     target_mask, tid_no);	} else if (int_halt_code == ADV_HALT_CHK_CONDITION) {		struct	 adv_target_transinfo* tinfo;		union	 ccb *ccb;		u_int8_t tag_code;		u_int8_t q_status;		tinfo = &adv->tinfo[tid_no];		q_cntl |= QC_REQ_SENSE;		/* Renegotiate if appropriate. */		adv_set_syncrate(adv, /*struct cam_path */NULL,				 tid_no, /*period*/0, /*offset*/0,				 ADV_TRANS_CUR);		if (tinfo->current.period != tinfo->goal.period) {			adv_msgout_sdtr(adv, tinfo->goal.period,					tinfo->goal.offset);			q_cntl |= QC_MSG_OUT;		}		adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_CNTL, q_cntl);		/* Don't tag request sense commands */		tag_code = adv_read_lram_8(adv,					   halt_q_addr + ADV_SCSIQ_B_TAG_CODE);		tag_code &=		    ~(MSG_SIMPLE_Q_TAG|MSG_HEAD_OF_Q_TAG|MSG_ORDERED_Q_TAG);		if ((adv->fix_asyn_xfer & target_mask) != 0		 && (adv->fix_asyn_xfer_always & target_mask) == 0) {			tag_code |= (ADV_TAG_FLAG_DISABLE_DISCONNECT				 | ADV_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);		}		adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_TAG_CODE,				 tag_code);		q_status = adv_read_lram_8(adv,					   halt_q_addr + ADV_SCSIQ_B_STATUS);		q_status |= (QS_READY | QS_BUSY);		adv_write_lram_8(adv, halt_q_addr + ADV_SCSIQ_B_STATUS,				 q_status);		/*		 * Freeze the devq until we can handle the sense condition.		 */		ccb = (union ccb *) adv_read_lram_32(adv, halt_q_addr							 + ADV_SCSIQ_D_CCBPTR);		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);		ccb->ccb_h.status |= CAM_DEV_QFRZN;		adv_abort_ccb(adv, tid_no, ADV_TIX_TO_LUN(target_ix),			      /*ccb*/NULL, CAM_REQUEUE_REQ,			      /*queued_only*/TRUE);		scsi_busy = adv_read_lram_8(adv, ADVV_SCSIBUSY_B);		scsi_busy &= ~target_mask;		adv_write_lram_8(adv, ADVV_SCSIBUSY_B, scsi_busy);		/*		 * Ensure we have enough time to actually		 * retrieve the sense.		 */		untimeout(adv_timeout, (caddr_t)ccb, ccb->ccb_h.timeout_ch);		ccb->ccb_h.timeout_ch =		    timeout(adv_timeout, (caddr_t)ccb, 5 * hz);	} else if (int_halt_code == ADV_HALT_SDTR_REJECTED) {		struct	ext_msg out_msg;		adv_read_lram_16_multi(adv, ADVV_MSGOUT_BEG,				       (u_int16_t *) &out_msg,				       sizeof(out_msg)/2);		if ((out_msg.msg_type == MSG_EXTENDED)		 && (out_msg.msg_len == MSG_EXT_SDTR_LEN)		 && (out_msg.msg_req == MSG_EXT_SDTR)) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?