Page 1 of 1

SATA as IDE [SOLVED]

Posted: Mon Oct 07, 2013 2:07 pm
by bigbob
Hi,

My ATA/PATA (PIO mode) driver works with ATA/PATA winchesters (I tested it on a real HW and in emulators) but doesn't work with SATA winchesters set to "SATA as IDE" in BIOS. Unfortunately VirtualBox only has SATA as AHCI, so it can't be tested there. Bochs doesn't support SATA(AHCI) and I couldn't get QEMU working with SATA(AHCI).
Do I have to do something differently in the driver if a SATA winchester is emulating PATA?

The algorithm of my driver (kernel calls hd_detect):
hd_detect:
set nIEN for primary and secondary bus (maybe this should be done after drive select but it's not important now)
loop through primary and secondary busses (master and slave) by calling ata_identify with the proper addresses(port and ctrl) and slavebit
if a winchester was found then exit loop and set hd_detected variable to 1; otherwise "Couldn't Detect"

ata_identify:
send (0xA0 | slavebit <<4) to PORT_DRV_HEAD (offset 6)
sleep (1) // 1 ms
set 0 to ports from offset 2 to offset 5 (PORT_SECT_COUNT, PORT_SECT_NUM, PORT_CYL_LOW and _HIGH)
sleep(1)
send cmd IDENTIFY to PORT_COMMAND (offset 7)
sleep(1)
Read PORT_CYL_LOW and _HIGH // check signature bytes
If both zero then jump to polling (ATA/PATA disk)
If not 0x3C and 0xC3 respectively then return with NOT_FOUND (SATA disk)
polling (wait 1ms, test BSY bit (for zero) in loop with a 2-second-timeout, test ERR (for zero), test DF (for zero), test DRQ (for 1))
If not ok then return with ERROR
read 256 words from PORT_DATA (offset 0)
return FOUND

EDIT: During polling if Timeout, ERR or DF fails then return with with ERROR.

Neither of the signature-byte-pairs match but if I remove that code then polling fails.
A winchester gets detected in Bochs regardless of being a primary or secondary and master or slave.
I wouldn't like to write the AHCI driver if it's not necessary. Getting "SATA disks emulating IDE" working would be much easier.

EDIT: link to thread about HDD Detection:
http://forum.osdev.org/viewtopic.php?p=111815

Re: SATA as IDE

Posted: Mon Oct 07, 2013 8:36 pm
by Brynet-Inc
A SATA controller in IDE mode is often operating in native PCI IDE mode, not in compatibility mode.. the fixed IRQ 14/15 and legacy ISA mappings are not guaranteed or even likely to be configured by the chipset.

Re: SATA as IDE

Posted: Tue Oct 08, 2013 12:38 am
by bigbob
Brynet-Inc wrote:A SATA controller in IDE mode is often operating in native PCI IDE mode, not in compatibility mode.. the fixed IRQ 14/15 and legacy ISA mappings are not guaranteed or even likely to be configured by the chipset.
I use polling, so IRQ is no problem now.
There is an extra option in the BIOS (of my netbook):
SATA RUN MODE Configuration: [Enhanched] / [Disabled] / [Compatible]
Enhanced is the default selection and I will test the others today.
Perhaps you are referring to this.

EDIT: on a laptop, in the BIOS there is only "SATA Mode [AHCI Mode] / [IDE Mode]", so that extra option is not available.
If the OS needs to set the Compatibility mode, then how?

Re: SATA as IDE

Posted: Tue Oct 08, 2013 1:24 am
by bigbob
Setting "SATA RUN Mode Configuration" to [Compatible] works. The winchester gets detected on my Netbook.
However in the BIOS of the Laptop there is no such option and just setting "SATA Mode" to [IDE] doesn't work. The winchester doesn't get detected.
This is already wonderful. Thanks for the help!

Re: SATA as IDE

Posted: Tue Oct 08, 2013 6:20 am
by bigbob
I have found how to set AHCI-mode without BIOS:
first enumerate PCI (lspci on Linux) in order to find the controller (bus:device and f is function):
00:1f.2 SATA controller: Intel Corporation 82801GBM/GHM (ICH7-M Family) SATA Controller [IDE mode] (rev 02)
So bus=0, device(or slot)=1 and function=2

Then execute:
setpci -s 0:1f.2 90.b=40

According to "man setpci" with the "-s" option we can specify the bus, slot and function.
I am not sure about "90.b=40" .
"setpci --dumpregs" prints all the registers: "09 B CLASS_PROG":
cap pos w name
00 W VENDOR_ID
02 W DEVICE_ID
04 W COMMAND
06 W STATUS
08 B REVISION
09 B CLASS_PROG
0a W CLASS_DEVICE
0c B CACHE_LINE_SIZE
0d B LATENCY_TIMER
...
They are the offsets in the 256-byte-PCI-config.
Register 9 is a byte perhaps and we set it to 40h. There is no register 90. Since it's a byte-size register 0.b doesn't mean the lower byte.
The command register would be: "04 W COMMAND" so that's WORD-size but it's not 90.

However "90.b=40" can mean to set ProgIF to 40h, so 0 means lower byte regardless of the byte-size of ProgIF.
Still need to figure out how to set IDE-Mode, not to mention Compatibility mode.

Re: SATA as IDE

Posted: Tue Oct 08, 2013 7:25 am
by jnc100
Have you read the ICH7 datasheet? You are right in that register 90h (byte-sized) is the one you want. Setting to 0 should set IDE mode, 40h AHCI mode and 80h RAID mode (which is not supported by the -M version of the chipset).

Once in IDE mode, you should then be able to set legacy/native modes by bits 0 and 2 (for the primary and secondary channels respectively) of byte-sized register 09h.

I wouldn't try this in linux though as the linux driver may attempt to interfere (as changing modes also changes the PCI device ID of the controller and then linux may attempt to load another driver which will use its own settings).

Regards,
John.

Re: SATA as IDE

Posted: Tue Oct 08, 2013 7:38 am
by bigbob
Hi John,

I will, thanks. I googled for hours but found almost nothing. Very valuable info!

Regards,
Rob

Re: SATA as IDE

Posted: Tue Oct 08, 2013 1:45 pm
by Combuster
bigbob wrote:I will, thanks. I googled for hours
Google is not a substitute for RTFM :wink:

Re: SATA as IDE

Posted: Tue Oct 08, 2013 1:57 pm
by bigbob
Combuster wrote:Google is not a substitute for RTFM :wink:
I see :oops:
I didn't know that ICH7 was the document I had to read.
No article/wiki mentioned that. :D

Re: SATA as IDE

Posted: Tue Oct 08, 2013 2:05 pm
by Combuster
bigbob wrote:I didn't know that ICH7 was the document I had to read.
No article/wiki mentioned that.
Something more important did #-o
Forum rules wrote:Try to find an answer by reading the manual.