How to write a disk driver can serve a batch of requests?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
strikew
Posts: 2
Joined: Wed Sep 09, 2015 4:51 am
Libera.chat IRC: strikew

How to write a disk driver can serve a batch of requests?

Post by strikew »

Hey guys, I am playing with the xv6 kernel and want to improve its IDE disk driver. The xv6 doc said ''Modern disk controllers typically accept a batch of disk requests at a time", and that is my goal. But as far as I know, IDE driver uses in/out instructions to communicate with disk, as the following code shows:

Code: Select all

  idewait(0);// wait for IDE disk to become ready.
  outb(0x3f6, 0);  // generate interrupt
  outb(0x1f2, sector_per_block);  // number of sectors
  outb(0x1f3, sector & 0xff);
  outb(0x1f4, (sector >> 8) & 0xff);
  outb(0x1f5, (sector >> 16) & 0xff);
  outb(0x1f6, 0xe0 | ((b->dev&1)<<4) | ((sector>>24)&0x0f));
  if(b->flags & B_DIRTY){
    outb(0x1f7, IDE_CMD_WRITE);
    outsl(0x1f0, b->data, BSIZE/4);
  } else {
    outb(0x1f7, IDE_CMD_READ);
  }
Based on above knowledge, I don't know how to implement an IDE driver that can issue a batch of requests(e.g., read block 3, read block 5) to disk all at once. Maybe I can use a loop to wrap all outbs in above code? I want someone to point me to the right direction, thanks.
heat
Member
Member
Posts: 103
Joined: Sat Mar 28, 2015 11:23 am
Libera.chat IRC: heat

Re: How to write a disk driver can serve a batch of requests

Post by heat »

I'm not sure if xv6 already uses PCI DMA for the ATA driver, but if it doesn't, look into it. You can serve batches of requests by just adding another entry to the PRDTs.
If some of you people keep insisting on having backwards compatibitity with the stone age, we'll have stone tools forever.
My Hobby OS: https://github.com/heatd/Onyx
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: How to write a disk driver can serve a batch of requests

Post by Kevin »

You can't, the PRDT only lets you use a scatter/gather list for a single request. IDE just isn't a modern disk controller interface and allows only a single request at a time. If you want more than that, AHCI can do parallel read/writes with the NCQ commands.
Developer of tyndur - community OS of Lowlevel (German)
User avatar
crunch
Member
Member
Posts: 81
Joined: Wed Aug 31, 2016 9:53 pm
Libera.chat IRC: crunch
Location: San Diego, CA

Re: How to write a disk driver can serve a batch of requests

Post by crunch »

Are you asking about something similar to a disk-block queue? That's what I do. (and I believe xv6 does as well)

I just add requests to a queue and then the driver responds to each request on the queue whenever an interrupt is triggered
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to write a disk driver can serve a batch of requests

Post by Schol-R-LEA »

Part of the problem here is that the statement - and hence the answer - is ambiguous. What constitutes a 'modern disk controller'? IDE probably doesn't count; it is a legacy standard from the late 1980s and early 1990s. Most likely, the author was talking about controllers that implement AHCI (the implementation-independent ABI developed for the SATA physical interface standard), as Kevin said.

All compliant SATA drives should support an AHCI mode, but they usually will also emulate PATA (the retroactively-applied term for IDE and other related parallel interfaces) for legacy operating systems, and some will have an implementation-specific mode as well which exploits the specific hardware better than AHCI can. While the ideal would be to have a driver for as many of the individual drive models as possible, most low-end manufacturers don't bother to do more than AHCI does, and even for those who do, there are far too many for even Microsoft to support themselves. Even if you want to write a driver for a specific high-performance drive, the interface is probably proprietary, meaning that unless you get a driver from the manufacturer themselves you need to just use AHCI.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to write a disk driver can serve a batch of requests

Post by Schol-R-LEA »

Sorry, I should have finished that but I had to go take care of something quick.

Anyway, the first question becomes, is it actually an IDE drive, or a later-model EIDE, PATA, SATA (or even SCSI, depending on the age of the drive and the controller) drive that is emulating IDE for backwards compatibility? Most true IDE and EIDE drives have long since succumbed to stiction (of which the infamous "click o' death" is the first symptom), so chances are it is a SATA drive masquerading as IDE. The drive device info in either Windows or Linux should be able to tell you, and if not, and you can open up the case, you should be able to tell just by looking at the physical connectors (the PATA style connectors are broad and narrow, about 2"/5 cm wide but about a 0.25"/6 mm thick), and usually used a ribbon cable, while the SATA connectors are about 0.4" /1 cm wide and 0.125" / 3 mm thick and use a thinner, singly clad cable).
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
strikew
Posts: 2
Joined: Wed Sep 09, 2015 4:51 am
Libera.chat IRC: strikew

Re: How to write a disk driver can serve a batch of requests

Post by strikew »

Thank you all. So the hardware, disk controller, must have a command to support receiving a batch of requests, or nothing I can do. And it seems like the old IDE(PATA) disk doesn't support this feature.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to write a disk driver can serve a batch of requests

Post by Schol-R-LEA »

Do you know the make and model of the drive, and the year it was manufactured? According to Wikipedia, the ATA-4 standard has a 'tagged queued operations' mode, meaning that PATA drives which are from 2002 or later will generally support it. As I said earlier, most drives that old or older won't even be functional anymore due to stiction - while drives made after the mid-1990s are a lot less prone to it and would run for several years longer than earlier drives, is still happens to most HDDs eventually IIUC.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: How to write a disk driver can serve a batch of requests

Post by Kevin »

Schol-R-LEA wrote:According to Wikipedia, the ATA-4 standard has a 'tagged queued operations' mode, meaning that PATA drives which are from 2002 or later will generally support it.
You're jumping to conclusions. That a standard contains some optional feature doesn't mean that it gets implemented by actual hardware. If you'd read a sentence or two further in the same article: "However, support for these is extremely rare in actual parallel ATA products and device drivers because these feature sets were implemented in such a way as to maintain software compatibility with its heritage as originally an extension of the ISA bus. This implementation resulted in excessive CPU utilization which largely negated the advantages of command queuing."
Developer of tyndur - community OS of Lowlevel (German)
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to write a disk driver can serve a batch of requests

Post by Schol-R-LEA »

OK, I didn't read that part. I don't know why I am being so careless about things like that lately. Sorry about that.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Post Reply