Analyze the read/write process of the IDE hard drive

Source: Internet
Author: User
Article title: analyzes the read/write process of the IDE hard drive. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: opera
The Linux kernel supports a maximum of 10 IDE interfaces in the default configuration. The IDE interfaces are described in the ide_hwif_t structure. each IDE interface has a pair of primary-slave driver interfaces, which are described in the ide_drive_t structure, each drive interface can be connected to different types of IDE devices, such as IDE hard disks and optical drives. They are described in ide_driver_t structure.
Each drive interface contains a Command request queue, which is described in the request_queue_t structure. the specific request is described in the request structure.
Multiple IDE drives can share one interrupt. the drive that shares the same interrupt forms a group and is described in the ide_hwgroup_t structure. ide_intr () is the hardware interrupt entry shared by all ide drivers. the ide_hwgroup_t pointer is passed to ide_intr as dev_id.
Before reading or writing a drive, you must use ide_set_handler () to set the interrupt function pointer to be called by ide_intr. after the interrupt is generated, the function pointer is automatically cleared.
Do_rw_disk (drive, rq, block) writes the content described in rq to the drive of the IDE hard drive starting from the logical sector code block.
The following code describes the transmission mode of the hard disk PIO.
  
; Drivers/ide/ide-disk.c
Static ide_startstop_t do_rw_disk (ide_drive_t * drive, struct request * rq, unsigned long block)
{
If (IDE_CONTROL_REG)
OUT_BYTE (drive-> ctl, IDE_CONTROL_REG );
OUT_BYTE (rq-> nr_sectors, IDE_NSECTOR_REG );
If (drive-> select. B. lba) {if it is a logical block addressing mode
OUT_BYTE (block, IDE_SECTOR_REG );
OUT_BYTE (block >>= 8, IDE_LCYL_REG );
OUT_BYTE (block >>= 8, IDE_HCYL_REG );
OUT_BYTE (block >>& 0x0f) | drive-> select. all, IDE_SELECT_REG );
} Else {
Unsigned int sect, head, cyl, track;
Track = block/drive-> sect;
Sect = block % drive-> sect + 1;
OUT_BYTE (sect, IDE_SECTOR_REG );
Head = track % drive-> head;
Cyl = track/drive-> head;
OUT_BYTE (cyl, IDE_LCYL_REG );
OUT_BYTE (cyl> 8, IDE_HCYL_REG );
OUT_BYTE (head | drive-> select. all, IDE_SELECT_REG );
}
If (rq-> cmd = READ ){{
Ide_set_handler (drive, & read_intr, WAIT_CMD, NULL); WAIT_CMD times out in 10 seconds
OUT_BYTE (drive-> mult_count? WIN_MULTREAD: WIN_READ, IDE_COMMAND_REG );
Return ide_started;
}
If (rq-> cmd = WRITE ){
Ide_startstop_t startstop;
OUT_BYTE (drive-> mult_count? WIN_MULTWRITE: WIN_WRITE, IDE_COMMAND_REG );
If (ide_wait_stat (& startstop, drive, DATA_READY, drive-> bad_wstat, WAIT_DRQ )){
Printk (KERN_ERR "% s: no DRQ after issuing % s \ n", drive-> name,
Drive-> mult_count? "MULTWRITE": "WRITE ");
Return startstop;
}
If (! Drive-> unmask)
_ Cli ();/* local CPU only */
If (drive-> mult_count) {if multi-sector transfer is allowed
Ide_hwgroup_t * hwgroup = HWGROUP (drive );
/*
* Ugh .. this part looks Uugly because we MUST set up
* The interrupt handler before outputting the first block
* Of data to be written. If we hit an error (writable upted buffer list)
* In ide_multwrite (), then we need to remove the handler/timer
* Before returning. Fortunately, this NEVER happens (right ?).
*
* When t when you get an error it seems...
*/
Hwgroup-> wrq = * rq;/* scratchpad */
Ide_set_handler (drive, & multwrite_intr, WAIT_CMD, NULL );
If (ide_multwrite (drive, drive-> mult_count )){
Unsigned long flags;
Spin_lock_irqsave (& io_request_lock, flags );
Hwgroup-> handler = NULL;
Del_timer (& hwgroup-> timer );
Spin_unlock_irqrestore (& io_request_lock, flags );
Return ide_stopped;
}
} Else {
Ide_set_handler (drive, & write_intr, WAIT_CMD, NULL );
Idedisk_output_data (drive, rq-> buffer, SECTOR_WORDS); write one-sector SECTOR_WORDS = 512/4
}
Return ide_started;
}
Printk (KERN_ERR "% s: bad command: % d \ n", drive-> name, rq-> cmd );
Ide_end_request (0, HWGROUP (drive ));
Return ide_stopped;
}
Void ide_set_handler (ide_drive_t * drive, ide_handler_t * handler,
Unsigned int timeout, ide_expiry_t * expiry)
{
Unsigned long flags;
Ide_hwgroup_t * hwgroup = HWGROUP (drive );
Spin_lock_irqsave (& io_request_lock, flags );
If (hwgroup-> handler! = NULL ){
Printk ("% s: ide_set_handler: handler not null; old = % p, new = % p \ n ",
Drive-> name, hwgroup-> handler, handler );
}
Hwgroup-> handler = handler;
Hwgroup-> expiry = expiry;
Hwgroup-> timer. expires = jiffies + timeout;
Add_timer (& hwgroup-> timer );
Spin_unlock_irqrestore (& io_request_lock, flags );
}
Static inline void idedisk_output_data (ide_drive_t * drive, void * buffer, unsigned int wcount)
{
If (drive-> bswap ){
Idedisk_bswap_data (buffer, wcount );
Ide_output_data (drive, buffer, wcount );
Idedisk_bswap_data (buffer, wcount );
} Else
Ide_output_data (drive, buffer, wcount );
}
Void ide_output_data (ide_drive_t * drive, void * buffer, unsigned int wcount)
{
Byte io_32bit = drive-> io_32bit;
If (io_32bit ){
# If SUPPORT_VLB_SYNC
If (io_32bit & 2 ){
Unsigned long flags;
_ Save_flags (flags);/* local CPU only */
_ Cli ();/* local CPU only */
Do_vlb_sync (IDE_NSECTOR_REG );
Outsl (IDE_DATA_REG, buffer, wcount );
_ Restore_flags (flags);/* local CPU only */
} Else
# Endif/* SUPPORT_VLB_SYNC */
Outsl (IDE_DATA_REG, buffer, wcount );
} Else {
# If SUPPORT_SLOW_DATA_PORTS
If (drive-> slow ){
Unsigned short * ptr = (unsigned short *) buffer;
While (wcount --){
Outw_p (* ptr ++, IDE_DATA_REG );
Outw_p (* ptr ++, IDE_DATA_REG );
}
} Else
# Endif/* SUPPORT_SLOW_DATA_PORTS */
Outsw (IDE_DATA_REG, buffer, wcount <1 );
}
}
Int ide_multwrite (ide_drive_t * drive, unsigned int mcount)
{
Ide_hwgroup_t * hwgroup = HWGROUP (drive );
/*
* This may look a bit odd, but remember wrq is a copy of
* Request not the original. The pointers are real however so
* Bh's are not copies. Remember that or bad stuff will happen
*
* At the point we are called the drive has asked us for
* Data, and its our job to feed it, walking Upload SS bh boundaries
* If need be.
*/
Struct request * rq = & hwgroup-> wrq;
Do {
Unsigned long flags;
Unsigned int nsect = rq-> current_nr_sectors;
If (nsect> mcount)
Nsect = mcount;
Mcount-= nsect;
At this time, mcount is the number of required sectors to be transferred
Idedisk_output_data (drive, rq-> buffer, nsect <7 );
Spin_lock_irqsave (& io_request_lock, flags);/* Is this really necessary? */
# Ifdef CONFIG_BLK_DEV_PDC4030
Rq-> sector + = nsect;
# Endif
If (long) (rq-> nr_sectors-= nsect) <= 0)
Spin_unlock_irqrestore (& io_request_lock, flags );
Break;
}
If (rq-> current_nr_sectors-= nsect) = 0 ){
If (rq-> bh = rq-> bh-> B _reqnext )! = NULL ){{
Rq-> current_nr_sectors = rq-> bh-> B _size> 9;
Rq-> buffer = rq-> bh-> B _data;
} Else {
Spin_unlock_irqrestore (& io_request_lock, flags );
Printk ("% s: buffer list upted (% ld, % ld, % d) \ n ",
Drive-> name, rq-> current_nr_sectors,
Rq-> nr_sectors, nsect );
Ide_end_request (0, hwgroup );
Return 1;
}
} Else {
/* Fix the pointer .. we ate data */
Rq-> buffer + = nsect <9;
}
Spin_unlock_irqrestore (& io_request_lock, flags );
} While (mcount );
Return 0;
}
; IDE interface shared interrupt entry
Void ide_intr (int irq, void * dev_id, struct pt_regs * regs)
{
Unsigned long flags;
Ide_hwgroup_t * hwgroup = (ide_hwgroup_t *) dev_id;
Ide_hwif_t * hwif;
Ide_drive_t * drive;
Ide_handler_t * handler;
Ide_startstop_t startstop;
Spin_lock_irqsave (& io_request_lock, flags );
Hwif = hwgroup-> hwif;
If (! Ide_ack_intr (hwif )){
Spin_unlock_irqrestore (& I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.