11

I understand that there is such a thing as a disk controller which contains a buffer, but was wondering if the CPU reads directly from this buffer, or whether the data must first go to a specific location in memory, and then allow itself to be read by the CPU?

Does anyone also know what the buffer is called? And how does DMA fit into all this?

0

2 Answers 2

23

For a disk read the data flow is essentially:

  • after the read/write assembly is at the requested cylinder, the requested r/w head is selected.
  • at each sector, the sector ID is read. If the sector number that was read matches the requested sector number, then the sector data is read.
  • the sector data is read as a serial bit stream, and converted to bytes.
  • the bytes of the sector are stored in a sector buffer (usually SRAM in the controller); this sector buffer is distinct from the "disk cache".
  • once the entire sector has been read, the data is validated using ECC, and perhaps corrected.
  • once validated, the sector data is transferred from the controller to the host PC. Note: existence of this sector buffer is not well known, and there is widespread misinformation that the transfer speed on the host (e.g.ATA) interface is tied or limited by the bit rate at the R/W head. That is completely false, since these two data transfers are independent and sequential operations and not concurrent.
  • as the PC receives the data from the disk controller through the ATA interface, the PC can use either programmed I/O (the CPU repeatedly reads the ATA port's data register and copies the value to the destination memory) or DMA (the DMA controller is setup to copy N bytes from the ATA port's data register to a memory buffer w/o further CPU intervention).
  • the PC's memory "buffer" that receives the data could be the application's buffer (when using block I/O system call), or could be an internal system buffer under filesystem control, or even program or data memory if the data came from the swap area (or page file).

A write to disk is similar, except that the data is transferred from PC to disk controller, the ECC is calculated, the requested sector is located, and then the data is written from the sector buffer to the platter.

For extra credit:

Read up on "scatter-gather" transfers, which uses DMA chaining and a scatter-gather list of memory addresses and buffer lengths. Instead of one large, contiguous memory buffer, a "scatter-gather" transfer allows the use of non-contiguous memory buffers to be aggregated for the disk I/O request.

3
  • 1
    Could perhaps be a little less specific about 'r/w heads' since non-mechanical drives are being used much more frequently these days, but it's a good answer.
    – Shinrai
    Oct 26, 2011 at 14:16
  • @Shinrai - I only have first-hand programming experience with moving-arm disk drives (and I've used fixed-head drives). I choose not to speculate or repeat 2nd or 3rd-hand info on how SSDs operate.
    – sawdust
    Oct 29, 2011 at 21:18
  • +1 for this great answer, well documented and written. Very professional.
    – climenole
    Oct 9, 2012 at 2:51
2

It goes into a specific memory area that the kernel has allocated the disk's driver. I don't think it is specifically named. This memory area is usually configured as a ring buffer. Depending on the OS, this memory may be copied a second time into user space for the application to use.

DMA is "direct memory access" which means that the disk drive's controller can directly write to the RAM without having the CPU repeatedly ask it if more data is available (known as polling - the way things used to be done). Instead, when the entire buffer has been transferred, the controller then interrupts the CPU to let it know data is now available.

If you are curious about an even lower level, the driver makes ioctl calls.

2
  • 1
    Ring buffers are typically employed by network devices (e.g. Ethernet controller) because the data can arrive unsolicited (unexpectedly w/o a pending read request). Data from a disk drive/controller is always in response to a request made by the host. The data is solicited, so there is no need for a "ring buffer" in a disk driver.
    – sawdust
    Oct 26, 2011 at 2:16
  • OK I've only worked with DACs at the driver level and they use ring buffers too, so I had assumed. Oct 27, 2011 at 1:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .