📄 ataioreg.c
字号:
unsigned char cylHigh;
unsigned char frReg;
unsigned char scReg;
unsigned char snReg;
unsigned char status;
unsigned char reason;
unsigned char lowCyl;
unsigned char highCyl;
unsigned int byteCnt;
unsigned int wordCnt;
int ndx;
unsigned long dpaddr;
unsigned char * cp;
unsigned char far * cfp;
int slowXferCntr = 0;
int prevFailBit7 = 0;
// mark start of PI cmd in low level trace
trc_llt( 0, 0, TRC_LLT_S_PI );
// setup register values
devCtrl = CB_DC_HD15 | ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_atapi_reg_dh = reg_atapi_reg_dh & 0x0f;
devHead = ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) | reg_atapi_reg_dh;
cylLow = dpbc & 0x00ff;
cylHigh = ( dpbc & 0xff00 ) >> 8;
frReg = reg_atapi_reg_fr;
scReg = reg_atapi_reg_sc;
snReg = reg_atapi_reg_sn;
reg_atapi_reg_fr = 0;
reg_atapi_reg_sc = 0;
reg_atapi_reg_sn = 0;
reg_atapi_reg_dh = 0;
// Reset error return data.
sub_zero_return_data();
reg_cmd_info.flg = TRC_FLAG_CMD;
reg_cmd_info.ct = dir ? TRC_TYPE_PPDO : TRC_TYPE_PPDI;
reg_cmd_info.cmd = CMD_PACKET;
reg_cmd_info.fr1 = frReg;
reg_cmd_info.sc1 = scReg;
reg_cmd_info.sn1 = snReg;
reg_cmd_info.cl1 = cylLow;
reg_cmd_info.ch1 = cylHigh;
reg_cmd_info.dh1 = devHead;
reg_cmd_info.dc1 = devCtrl;
// Make sure the command packet size is either 12 or 16
// and save the command packet size and data.
cpbc = cpbc < 12 ? 12 : cpbc;
cpbc = cpbc > 12 ? 16 : cpbc;
reg_atapi_cp_size = cpbc;
cp = reg_atapi_cp_data;
cfp = MK_FP( cpseg, cpoff );
for ( ndx = 0; ndx < cpbc; ndx ++ )
{
* cp = * cfp;
cp ++ ;
cfp ++ ;
}
// Set command time out.
tmr_set_timeout();
// Select the drive - call the sub_select function.
// Quit now if this fails.
if ( sub_select( dev ) )
{
sub_trace_command();
trc_llt( 0, 0, TRC_LLT_E_PI );
reg_atapi_max_bytes = 32768L; // reset max bytes
return 1;
}
// Set up all the registers except the command register.
pio_outbyte( CB_DC, devCtrl );
pio_outbyte( CB_FR, frReg );
pio_outbyte( CB_SC, scReg );
pio_outbyte( CB_SN, snReg );
pio_outbyte( CB_CL, cylLow );
pio_outbyte( CB_CH, cylHigh );
pio_outbyte( CB_DH, devHead );
// For interrupt mode,
// Take over INT 7x and initialize interrupt controller
// and reset interrupt flag.
int_save_int_vect();
// Start the command by setting the Command register. The drive
// should immediately set BUSY status.
pio_outbyte( CB_CMD, CMD_PACKET );
// Waste some time by reading the alternate status a few times.
// This gives the drive time to set BUSY in the status register on
// really fast systems. If we don't do this, a slow drive on a fast
// system may not set BUSY fast enough and we would think it had
// completed the command when it really had not even started the
// command yet.
DELAY400NS;
// Command packet transfer...
// Check for protocol failures,
// the device should have BSY=1 or
// if BSY=0 then either DRQ=1 or CHK=1.
sub_atapi_delay( dev );
status = pio_inbyte( CB_ASTAT );
if ( status & CB_STAT_BSY )
{
// BSY=1 is OK
}
else
{
if ( status & ( CB_STAT_DRQ | CB_STAT_ERR ) )
{
// BSY=0 and DRQ=1 is OK
// BSY=0 and ERR=1 is OK
}
else
{
reg_cmd_info.failbits |= FAILBIT0; // not OK
}
}
// Command packet transfer...
// Poll Alternate Status for BSY=0.
trc_llt( 0, 0, TRC_LLT_PNBSY );
while ( 1 )
{
status = pio_inbyte( CB_ASTAT ); // poll for not busy
if ( ( status & CB_STAT_BSY ) == 0 )
break;
if ( tmr_chk_timeout() ) // time out yet ?
{
trc_llt( 0, 0, TRC_LLT_TOUT ); // yes
reg_cmd_info.to = 1;
reg_cmd_info.ec = 51;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
dir = -1; // command done
break;
}
}
// Command packet transfer...
// Check for protocol failures... no interrupt here please!
// Clear any interrupt the command packet transfer may have caused.
if ( int_intr_flag )
reg_cmd_info.failbits |= FAILBIT1;
int_intr_flag = 0;
// Command packet transfer...
// If no error, transfer the command packet.
if ( reg_cmd_info.ec == 0 )
{
// Command packet transfer...
// Read the primary status register and the other ATAPI registers.
status = pio_inbyte( CB_STAT );
reason = pio_inbyte( CB_SC );
lowCyl = pio_inbyte( CB_CL );
highCyl = pio_inbyte( CB_CH );
// Command packet transfer...
// check status: must have BSY=0, DRQ=1 now
if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ | CB_STAT_ERR ) )
!= CB_STAT_DRQ
)
{
reg_cmd_info.ec = 52;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
dir = -1; // command done
}
else
{
// Command packet transfer...
// Check for protocol failures...
// check: C/nD=1, IO=0.
if ( ( reason & ( CB_SC_P_TAG | CB_SC_P_REL | CB_SC_P_IO ) )
|| ( ! ( reason & CB_SC_P_CD ) )
)
reg_cmd_info.failbits |= FAILBIT2;
if ( ( lowCyl != cylLow ) || ( highCyl != cylHigh ) )
reg_cmd_info.failbits |= FAILBIT3;
// Command packet transfer...
// trace cdb byte 0,
// xfer the command packet (the cdb)
trc_llt( 0, * (unsigned char far *) MK_FP( cpseg, cpoff ), TRC_LLT_P_CMD );
pio_rep_outword( CB_DATA, cpseg, cpoff, cpbc >> 1 );
DELAY400NS; // delay so device can get the status updated
}
}
// Data transfer loop...
// If there is no error, enter the data transfer loop.
// First adjust the I/O buffer address so we are able to
// transfer large amounts of data (more than 64K).
dpaddr = dpseg;
dpaddr = dpaddr << 4;
dpaddr = dpaddr + dpoff;
while ( reg_cmd_info.ec == 0 )
{
// Data transfer loop...
// Wait for INT 7x -or- wait for not BUSY -or- wait for time out.
sub_atapi_delay( dev );
reg_wait_poll( 53, 54 );
// Data transfer loop...
// If there was a time out error, exit the data transfer loop.
if ( reg_cmd_info.ec )
{
dir = -1; // command done
break;
}
// Data transfer loop...
// Read the primary status register.
status = pio_inbyte( CB_STAT );
reason = pio_inbyte( CB_SC );
lowCyl = pio_inbyte( CB_CL );
highCyl = pio_inbyte( CB_CH );
// Data transfer loop...
// Exit the read data loop if the device indicates this
// is the end of the command.
if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ ) ) == 0 )
{
dir = -1; // command done
break;
}
// Data transfer loop...
// The device must want to transfer data...
// check status: must have BSY=0, DRQ=1 now.
if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ ) ) != CB_STAT_DRQ )
{
reg_cmd_info.ec = 55;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
dir = -1; // command done
break;
}
else
{
// Data transfer loop...
// Check for protocol failures...
// check: C/nD=0, IO=1 (read) or IO=0 (write).
if ( ( reason & ( CB_SC_P_TAG | CB_SC_P_REL ) )
|| ( reason & CB_SC_P_CD )
)
reg_cmd_info.failbits |= FAILBIT4;
if ( ( reason & CB_SC_P_IO ) && dir )
reg_cmd_info.failbits |= FAILBIT5;
}
// do the slow data transfer thing
if ( reg_slow_xfer_flag )
{
slowXferCntr ++ ;
if ( slowXferCntr <= reg_slow_xfer_flag )
{
sub_xfer_delay();
reg_slow_xfer_flag = 0;
}
}
// Data transfer loop...
// get the byte count, check for zero...
byteCnt = ( highCyl << 8 ) | lowCyl;
if ( byteCnt < 1 )
{
reg_cmd_info.ec = 60;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
dir = -1; // command done
break;
}
// Data transfer loop...
// and check protocol failures...
if ( byteCnt > dpbc )
reg_cmd_info.failbits |= FAILBIT6;
reg_cmd_info.failbits |= prevFailBit7;
prevFailBit7 = 0;
if ( byteCnt & 0x0001 )
prevFailBit7 = FAILBIT7;
// Data transfer loop...
// make sure we don't overrun the caller's buffer
if ( ( reg_cmd_info.totalBytesXfer + byteCnt ) > reg_atapi_max_bytes )
{
reg_cmd_info.ec = 59;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
dir = -1; // command done
break;
}
// increment number of DRQ packets
reg_cmd_info.drqPackets ++ ;
// Data transfer loop...
// transfer the data and update the i/o buffer address
// and the number of bytes transfered.
wordCnt = ( byteCnt >> 1 ) + ( byteCnt & 0x0001 );
reg_cmd_info.totalBytesXfer += ( wordCnt << 1 );
dpseg = (unsigned int) ( dpaddr >> 4 );
dpoff = (unsigned int) ( dpaddr & 0x0000000fL );
if ( dir )
pio_rep_outword( CB_DATA, dpseg, dpoff, wordCnt );
else
pio_rep_inword( CB_DATA, dpseg, dpoff, wordCnt );
dpaddr = dpaddr + byteCnt;
DELAY400NS; // delay so device can get the status updated
}
// End of command...
// Wait for interrupt or poll for BSY=0,
// but don't do this if there was any error or if this
// was a commmand that did not transfer data.
if ( ( reg_cmd_info.ec == 0 ) && ( dir >= 0 ) )
{
sub_atapi_delay( dev );
reg_wait_poll( 56, 57 );
}
// Final status check, only if no previous error.
if ( reg_cmd_info.ec == 0 )
{
// Final status check...
// Read the primary status register and other regs.
status = pio_inbyte( CB_STAT );
reason = pio_inbyte( CB_SC );
lowCyl = pio_inbyte( CB_CL );
highCyl = pio_inbyte( CB_CH );
// Final status check...
// check for any error.
if ( status & ( CB_STAT_BSY | CB_STAT_DRQ | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 58;
trc_llt( 0, reg_cmd_info.ec, TRC_LLT_ERROR );
}
}
// Final status check...
// Check for protocol failures...
// check: C/nD=1, IO=1.
if ( ( reason & ( CB_SC_P_TAG | CB_SC_P_REL ) )
|| ( ! ( reason & CB_SC_P_IO ) )
|| ( ! ( reason & CB_SC_P_CD ) )
)
reg_cmd_info.failbits |= FAILBIT8;
// Done...
// Read the output registers and trace the command.
// Also put the command packet in the trace.
if ( ! reg_cmd_info.totalBytesXfer )
reg_cmd_info.ct = TRC_TYPE_PND;
sub_trace_command();
trc_cht_pkt();
// For interrupt mode, restore the INT 7x vector.
int_restore_int_vect();
// mark end of PI cmd in low level trace
trc_llt( 0, 0, TRC_LLT_E_PI );
// All done. The return values of this function are described in
// ATAIO.H.
reg_atapi_max_bytes = 32768L; // reset max bytes
if ( reg_cmd_info.ec )
return 1;
return 0;
}
// end ataioreg.c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -