📄 mindrvr.c
字号:
// exec_non_data_cmd() - Execute a non-data command.
//
// This includes the strange ATAPI DEVICE RESET 'command'
// (command code 08H).
//
// Note special handling for Execute Device Diagnostics
// command when there is no device 0.
//
// See ATA-2 Section 9.5, ATA-3 Section 9.5,
// ATA-4 Section 8.8 Figure 12. Also see Section 8.5.
//
//*************************************************************
static int exec_non_data_cmd( unsigned char dev );
static int exec_non_data_cmd( unsigned char dev )
{
unsigned char secCnt;
unsigned char secNum;
unsigned char status;
int polled = 0;
// reset Bus Master Error bit
pio_writeBusMstrStatus( BM_SR_MASK_ERR );
// Set command time out.
tmr_set_timeout();
// PAY ATTENTION HERE
// If the caller is attempting a Device Reset command, then
// don't do most of the normal stuff. Device Reset has no
// parameters, should not generate an interrupt and it is the
// only command that can be written to the Command register
// when a device has BSY=1 (a very strange command!). Not
// all devices support this command (even some ATAPI devices
// don't support the command.
if ( reg_cmd_info.cmd != CMD_DEVICE_RESET )
{
// Select the drive - call the sub_select function.
// Quit now if this fails.
if ( sub_select( dev ) )
{
return 1;
}
// Set up all the registers except the command register.
sub_setup_command();
}
// Start the command by setting the Command register. The drive
// should immediately set BUSY status.
pio_outbyte( CB_CMD, reg_cmd_info.cmd );
// 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;
// IF
// This is an Exec Dev Diag command (cmd=0x90)
// and there is no device 0 then
// there will be no interrupt. So we must
// poll device 1 until it allows register
// access and then do normal polling of the Status
// register for BSY=0.
// ELSE
// IF
// This is a Dev Reset command (cmd=0x08) then
// there should be no interrupt. So we must
// poll for BSY=0.
// ELSE
// Do the normal wait for interrupt or polling for
// completion.
if ( ( reg_cmd_info.cmd == CMD_EXECUTE_DEVICE_DIAGNOSTIC )
&&
( reg_config_info[0] == REG_CONFIG_TYPE_NONE )
)
{
polled = 1;
while ( 1 )
{
pio_outbyte( CB_DH, CB_DH_DEV1 );
DELAY400NS;
secCnt = pio_inbyte( CB_SC );
secNum = pio_inbyte( CB_SN );
if ( ( secCnt == 0x01 ) && ( secNum == 0x01 ) )
break;
if ( tmr_chk_timeout() )
{
reg_cmd_info.to = 1;
reg_cmd_info.ec = 24;
break;
}
}
}
else
{
if ( reg_cmd_info.cmd == CMD_DEVICE_RESET )
{
// Wait for not BUSY -or- wait for time out.
polled = 1;
sub_wait_poll( 0, 23 );
}
else
{
// Wait for interrupt -or- wait for not BUSY -or- wait for time out.
if ( ! int_use_intr_flag )
polled = 1;
sub_wait_poll( 22, 23 );
}
}
// If status was polled or if any error read the status register,
// otherwise get the status that was read by the interrupt handler.
if ( ( polled ) || ( reg_cmd_info.ec ) )
status = pio_inbyte( CB_STAT );
else
status = int_ata_status;
// Error if BUSY, DEVICE FAULT, DRQ or ERROR status now.
if ( reg_cmd_info.ec == 0 )
{
if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_DRQ | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 21;
}
}
// BMIDE Error=1?
if ( pio_readBusMstrStatus() & BM_SR_MASK_ERR )
{
reg_cmd_info.ec = 78; // yes
}
// NON_DATA_DONE:
// All done. The return values of this function are described in
// MINDRVR.H.
sub_trace_command();
if ( reg_cmd_info.ec )
return 1;
return 0;
}
//*************************************************************
//
// reg_non_data_lba28() - Easy way to execute a non-data command
// using an LBA sector address.
//
//*************************************************************
int reg_non_data_lba28( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lba )
{
// Setup current command information.
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.ns = sc;
reg_cmd_info.lbaSize = LBA28;
reg_cmd_info.lbaHigh = 0L;
reg_cmd_info.lbaLow = lba;
// Execute the command.
return exec_non_data_cmd( dev );
}
//*************************************************************
//
// reg_non_data_lba48() - Easy way to execute a non-data command
// using an LBA sector address.
//
//*************************************************************
int reg_non_data_lba48( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lbahi, unsigned long lbalo )
{
// Setup current command infomation.
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.ns = sc;
reg_cmd_info.lbaSize = LBA48;
reg_cmd_info.lbaHigh = lbahi;
reg_cmd_info.lbaLow = lbalo;
// Execute the command.
return exec_non_data_cmd( dev );
}
//*************************************************************
//
// exec_pio_data_in_cmd() - Execute a PIO Data In command.
//
// See ATA-2 Section 9.3, ATA-3 Section 9.3,
// ATA-4 Section 8.6 Figure 10.
//
//*************************************************************
static int exec_pio_data_in_cmd( unsigned char dev,
unsigned char * bufAddr,
long numSect, int multiCnt );
static int exec_pio_data_in_cmd( unsigned char dev,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
unsigned char status;
long wordCnt;
// reset Bus Master Error bit
pio_writeBusMstrStatus( BM_SR_MASK_ERR );
// Set command time out.
tmr_set_timeout();
// Select the drive - call the sub_select function.
// Quit now if this fails.
if ( sub_select( dev ) )
{
return 1;
}
// Set up all the registers except the command register.
sub_setup_command();
// Start the command by setting the Command register. The drive
// should immediately set BUSY status.
pio_outbyte( CB_CMD, reg_cmd_info.cmd );
// 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;
// Loop to read each sector.
while ( 1 )
{
// READ_LOOP:
//
// NOTE NOTE NOTE ... The primary status register (1f7) MUST NOT be
// read more than ONCE for each sector transferred! When the
// primary status register is read, the drive resets IRQ. The
// alternate status register (3f6) can be read any number of times.
// After interrupt read the the primary status register ONCE
// and transfer the 256 words (REP INSW). AS SOON as BOTH the
// primary status register has been read AND the last of the 256
// words has been read, the drive is allowed to generate the next
// IRQ (newer and faster drives could generate the next IRQ in
// 50 microseconds or less). If the primary status register is read
// more than once, there is the possibility of a race between the
// drive and the software and the next IRQ could be reset before
// the system interrupt controller sees it.
// Wait for interrupt -or- wait for not BUSY -or- wait for time out.
sub_wait_poll( 34, 35 );
// If polling or error read the status, otherwise
// get the status that was read by the interrupt handler.
if ( ( ! int_use_intr_flag ) || ( reg_cmd_info.ec ) )
status = pio_inbyte( CB_STAT );
else
status = int_ata_status;
// If there was a time out error, go to READ_DONE.
if ( reg_cmd_info.ec )
break; // go to READ_DONE
// If BSY=0 and DRQ=1, transfer the data,
// even if we find out there is an error later.
if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ ) ) == CB_STAT_DRQ )
{
// increment number of DRQ packets
reg_cmd_info.drqPackets ++ ;
// determine the number of sectors to transfer
wordCnt = multiCnt ? multiCnt : 1;
if ( wordCnt > numSect )
wordCnt = numSect;
wordCnt = wordCnt * 256;
// Do the REP INSW to read the data for one DRQ block.
reg_cmd_info.totalBytesXfer += ( wordCnt << 1 );
pio_drq_block_in( CB_DATA, bufAddr, wordCnt );
DELAY400NS; // delay so device can get the status updated
// Note: The drive should have dropped DATA REQUEST by now. If there
// are more sectors to transfer, BUSY should be active now (unless
// there is an error).
// Decrement the count of sectors to be transferred
// and increment buffer address.
numSect = numSect - ( multiCnt ? multiCnt : 1 );
bufAddr = bufAddr + ( 512 * ( multiCnt ? multiCnt : 1 ) );
}
// So was there any error condition?
if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 31;
break; // go to READ_DONE
}
// DRQ should have been set -- was it?
if ( ( status & CB_STAT_DRQ ) == 0 )
{
reg_cmd_info.ec = 32;
break; // go to READ_DONE
}
// If all of the requested sectors have been transferred, make a
// few more checks before we exit.
if ( numSect < 1 )
{
// Since the drive has transferred all of the requested sectors
// without error, the drive should not have BUSY, DEVICE FAULT,
// DATA REQUEST or ERROR active now.
status = pio_inbyte( CB_STAT );
if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_DRQ | CB_STAT_ERR ) )
{
reg_cmd_info.ec = 33;
break; // go to READ_DONE
}
// All sectors have been read without error, go to READ_DONE.
break; // go to READ_DONE
}
// This is the end of the read loop. If we get here, the loop is
// repeated to read the next sector. Go back to READ_LOOP.
}
// BMIDE Error=1?
if ( pio_readBusMstrStatus() & BM_SR_MASK_ERR )
{
reg_cmd_info.ec = 78; // yes
}
// READ_DONE:
// All done. The return values of this function are described in
// MINDRVR.H.
if ( reg_cmd_info.ec )
return 1;
return 0;
}
//*************************************************************
//
// reg_pio_data_in_lba28() - Easy way to execute a PIO Data In command
// using an LBA sector address.
//
//*************************************************************
int reg_pio_data_in_lba28( unsigned char dev, unsigned char cmd,
unsigned int fr, unsigned int sc,
unsigned long lba,
unsigned char * bufAddr,
long numSect, int multiCnt )
{
reg_cmd_info.cmd = cmd;
reg_cmd_info.fr = fr;
reg_cmd_info.sc = sc;
reg_cmd_info.dh = (unsigned char) ( CB_DH_LBA | ( dev ? CB_DH_DEV1 : CB_DH_DEV0 ) );
reg_cmd_info.dc = (unsigned char) ( int_use_intr_flag ? 0 : CB_DC_NIEN );
reg_cmd_info.lbaSize = LBA28;
reg_cmd_info.lbaHigh = 0L;
reg_cmd_info.lbaLow = lba;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -