Fixing bugs and removing debug statements
[tpg/acess2.git] / Modules / Storage / ATA / main.c
1 /*
2  * Acess2 IDE Harddisk Driver
3  * - main.c
4  */
5 #define DEBUG   1
6 #include <acess.h>
7 #include <modules.h>
8 #include <vfs.h>
9 #include <fs_devfs.h>
10 #include <drv_pci.h>
11 #include <tpl_drv_common.h>
12 #include <tpl_drv_disk.h>
13 #include "common.h"
14
15 #define IO_DELAY()      do{inb(0x80); inb(0x80); inb(0x80); inb(0x80);}while(0)
16
17 // === STRUCTURES ===
18 typedef struct
19 {
20         Uint32  PBufAddr;
21         Uint16  Bytes;
22         Uint16  Flags;
23 } __attribute__ ((packed))      tPRDT_Ent;
24 typedef struct
25 {
26         Uint16  Flags;          // 1
27         Uint16  Usused1[9];     // 10
28         char    SerialNum[20];  // 20
29         Uint16  Usused2[3];     // 23
30         char    FirmwareVer[8]; // 27
31         char    ModelNumber[40];        // 47
32         Uint16  SectPerInt;     // 48 - AND with 0xFF to get true value;
33         Uint16  Unused3;        // 49
34         Uint16  Capabilities[2];        // 51
35         Uint16  Unused4[2];     // 53
36         Uint16  ValidExtData;   // 54
37         Uint16  Unused5[5];      // 59
38         Uint16  SizeOfRWMultiple;       // 60
39         Uint32  Sectors28;      // 62
40         Uint16  Unused6[100-62];
41         Uint64  Sectors48;
42         Uint16  Unused7[256-104];
43 } __attribute__ ((packed))      tIdentify;
44
45 // === IMPORTS ===
46 extern void     ATA_ParseMBR(int Disk, tMBR *MBR);
47
48 // === PROTOTYPES ===
49  int    ATA_Install();
50  int    ATA_SetupIO();
51 void    ATA_SetupPartitions();
52 void    ATA_SetupVFS();
53  int    ATA_ScanDisk(int Disk);
54 void    ATA_ParseGPT(int Disk);
55 void    ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length);
56 Uint16  ATA_GetBasePort(int Disk);
57 // Filesystem Interface
58 char    *ATA_ReadDir(tVFS_Node *Node, int Pos);
59 tVFS_Node       *ATA_FindDir(tVFS_Node *Node, char *Name);
60 Uint64  ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
61 Uint64  ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
62  int    ATA_IOCtl(tVFS_Node *Node, int Id, void *Data);
63 // Read/Write Interface/Quantiser
64 Uint    ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk);
65 Uint    ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk);
66 // Read/Write DMA
67  int    ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer);
68  int    ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer);
69 // IRQs
70 void    ATA_IRQHandlerPri(int unused);
71 void    ATA_IRQHandlerSec(int unused);
72 // Controller IO
73 Uint8   ATA_int_BusMasterReadByte(int Ofs);
74 void    ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value);
75 void    ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value);
76
77 // === GLOBALS ===
78 MODULE_DEFINE(0, 0x0032, i386ATA, ATA_Install, NULL, "PCI", NULL);
79 tDevFS_Driver   gATA_DriverInfo = {
80         NULL, "ata",
81         {
82                 .NumACLs = 1,
83                 .Size = -1,
84                 .Flags = VFS_FFLAG_DIRECTORY,
85                 .ACLs = &gVFS_ACL_EveryoneRX,
86                 .ReadDir = ATA_ReadDir,
87                 .FindDir = ATA_FindDir
88         }
89 };
90 tATA_Disk       gATA_Disks[MAX_ATA_DISKS];
91  int    giATA_NumNodes;
92 tVFS_Node       **gATA_Nodes;
93 Uint16  gATA_BusMasterBase = 0;
94 Uint8   *gATA_BusMasterBasePtr = 0;
95  int    gATA_IRQPri = 14;
96  int    gATA_IRQSec = 15;
97  int    giaATA_ControllerLock[2] = {0}; //!< Spinlocks for each controller
98 Uint8   gATA_Buffers[2][(MAX_DMA_SECTORS+0xFFF)&~0xFFF] __attribute__ ((section(".padata")));
99 volatile int    gaATA_IRQs[2] = {0};
100 tPRDT_Ent       gATA_PRDTs[2] = {
101         {0, 512, IDE_PRDT_LAST},
102         {0, 512, IDE_PRDT_LAST}
103 };
104
105 // === CODE ===
106 /**
107  * \fn int ATA_Install()
108  */
109 int ATA_Install()
110 {
111         int     ret;
112
113         ret = ATA_SetupIO();
114         if(ret) return ret;
115
116         ATA_SetupPartitions();
117
118         ATA_SetupVFS();
119
120         if( DevFS_AddDevice( &gATA_DriverInfo ) == 0 )
121                 return MODULE_ERR_MISC;
122
123         return MODULE_ERR_OK;
124 }
125
126 /**
127  * \fn int ATA_SetupIO()
128  * \brief Sets up the ATA controller's DMA mode
129  */
130 int ATA_SetupIO()
131 {
132          int    ent;
133         tPAddr  addr;
134
135         ENTER("");
136
137         // Get IDE Controller's PCI Entry
138         ent = PCI_GetDeviceByClass(0x0101, 0xFFFF, -1);
139         LOG("ent = %i", ent);
140         gATA_BusMasterBase = PCI_GetBAR4( ent );
141         if( gATA_BusMasterBase == 0 ) {
142                 Log_Warning("ATA", "It seems that there is no Bus Master Controller on this machine. Get one");
143                 // TODO: Use PIO mode instead
144                 LEAVE('i', MODULE_ERR_NOTNEEDED);
145                 return MODULE_ERR_NOTNEEDED;
146         }
147         
148         // Map memory
149         if( !(gATA_BusMasterBase & 1) )
150         {
151                 if( gATA_BusMasterBase < 0x100000 )
152                         gATA_BusMasterBasePtr = (void*)(KERNEL_BASE | (tVAddr)gATA_BusMasterBase);
153                 else
154                         gATA_BusMasterBasePtr = (void*)( MM_MapHWPages( gATA_BusMasterBase, 1 ) + (gATA_BusMasterBase&0xFFF) );
155                 LOG("gATA_BusMasterBasePtr = %p", gATA_BusMasterBasePtr);
156         }
157         else {
158                 // Bit 0 is left set as a flag to other functions
159                 LOG("gATA_BusMasterBase = 0x%x", gATA_BusMasterBase & ~1);
160         }
161
162         // Register IRQs and get Buffers
163         IRQ_AddHandler( gATA_IRQPri, ATA_IRQHandlerPri );
164         IRQ_AddHandler( gATA_IRQSec, ATA_IRQHandlerSec );
165
166         gATA_PRDTs[0].PBufAddr = MM_GetPhysAddr( (tVAddr)&gATA_Buffers[0] );
167         gATA_PRDTs[1].PBufAddr = MM_GetPhysAddr( (tVAddr)&gATA_Buffers[1] );
168
169         LOG("gATA_PRDTs = {PBufAddr: 0x%x, PBufAddr: 0x%x}", gATA_PRDTs[0].PBufAddr, gATA_PRDTs[1].PBufAddr);
170
171         addr = MM_GetPhysAddr( (tVAddr)&gATA_PRDTs[0] );
172         LOG("addr = 0x%x", addr);
173         ATA_int_BusMasterWriteDWord(4, addr);
174         addr = MM_GetPhysAddr( (tVAddr)&gATA_PRDTs[1] );
175         LOG("addr = 0x%x", addr);
176         ATA_int_BusMasterWriteDWord(12, addr);
177
178         // Enable controllers
179         outb(IDE_PRI_BASE+1, 1);
180         outb(IDE_SEC_BASE+1, 1);
181
182         // return
183         LEAVE('i', MODULE_ERR_OK);
184         return MODULE_ERR_OK;
185 }
186
187 /**
188  * \fn void ATA_SetupPartitions()
189  */
190 void ATA_SetupPartitions()
191 {
192          int    i;
193         for( i = 0; i < MAX_ATA_DISKS; i ++ )
194         {
195                 if( !ATA_ScanDisk(i) ) {
196                         gATA_Disks[i].Name[0] = '\0';   // Mark as unused
197                         continue;
198                 }
199         }
200 }
201
202 /**
203  * \fn void ATA_SetupVFS()
204  * \brief Sets up the ATA drivers VFS information and registers with DevFS
205  */
206 void ATA_SetupVFS()
207 {
208          int    i, j, k;
209
210         // Count number of nodes needed
211         giATA_NumNodes = 0;
212         for( i = 0; i < MAX_ATA_DISKS; i++ )
213         {
214                 if(gATA_Disks[i].Name[0] == '\0')       continue;       // Ignore
215                 giATA_NumNodes ++;
216                 giATA_NumNodes += gATA_Disks[i].NumPartitions;
217         }
218
219         // Allocate Node space
220         gATA_Nodes = malloc( giATA_NumNodes * sizeof(void*) );
221
222         // Set nodes
223         k = 0;
224         for( i = 0; i < MAX_ATA_DISKS; i++ )
225         {
226                 if(gATA_Disks[i].Name[0] == '\0')       continue;       // Ignore
227                 gATA_Nodes[ k++ ] = &gATA_Disks[i].Node;
228                 for( j = 0; j < gATA_Disks[i].NumPartitions; j ++ )
229                         gATA_Nodes[ k++ ] = &gATA_Disks[i].Partitions[j].Node;
230         }
231
232         gATA_DriverInfo.RootNode.Size = giATA_NumNodes;
233 }
234
235 /**
236  * \fn int ATA_ScanDisk(int Disk)
237  */
238 int ATA_ScanDisk(int Disk)
239 {
240         union {
241                 Uint16  buf[256];
242                 tIdentify       identify;
243                 tMBR    mbr;
244         }       data;
245         Uint16  base;
246         Uint8   val;
247          int    i;
248         tVFS_Node       *node;
249
250         ENTER("iDisk", Disk);
251
252         base = ATA_GetBasePort( Disk );
253
254         LOG("base = 0x%x", base);
255
256         // Send Disk Selector
257         if(Disk == 1 || Disk == 3)
258                 outb(base+6, 0xB0);
259         else
260                 outb(base+6, 0xA0);
261         IO_DELAY();
262         
263         // Check for a floating bus
264         if( 0xFF == inb(base+7) ) {
265                 LOG("Floating bus");
266                 LEAVE('i', 0);
267                 return 0;
268         }
269         
270         // Check for the controller
271         outb(base+0x02, 0x66);
272         outb(base+0x03, 0xFF);
273         if(inb(base+0x02) != 0x66 || inb(base+0x03) != 0xFF) {
274                 LOG("No controller");
275                 LEAVE('i', 0);
276                 return 0;
277         }
278
279         // Send IDENTIFY
280         outb(base+7, 0xEC);
281         IO_DELAY();
282         val = inb(base+7);      // Read status
283         LOG("val = 0x%02x", val);
284         if(val == 0) {
285                 LEAVE('i', 0);
286                 return 0;       // Disk does not exist
287         }
288
289         // Poll until BSY clears and DRQ sets or ERR is set
290         while( ((val & 0x80) || !(val & 0x08)) && !(val & 1))   val = inb(base+7);
291
292         if(val & 1) {
293                 LEAVE('i', 0);
294                 return 0;       // Error occured, so return false
295         }
296
297         // Read Data
298         for(i=0;i<256;i++)      data.buf[i] = inw(base);
299
300         // Populate Disk Structure
301         if(data.identify.Sectors48 != 0)
302                 gATA_Disks[ Disk ].Sectors = data.identify.Sectors48;
303         else
304                 gATA_Disks[ Disk ].Sectors = data.identify.Sectors28;
305
306
307         LOG("gATA_Disks[ %i ].Sectors = 0x%x", Disk, gATA_Disks[ Disk ].Sectors);
308
309         {
310                 Uint64  val = gATA_Disks[ Disk ].Sectors / 2;
311                 char    *units = "KiB";
312                 if( val > 4*1024 ) {
313                         val /= 1024;
314                         units = "MiB";
315                 }
316                 else if( val > 4*1024 ) {
317                         val /= 1024;
318                         units = "GiB";
319                 }
320                 else if( val > 4*1024 ) {
321                         val /= 1024;
322                         units = "TiB";
323                 }
324                 Log_Log("ATA", "Disk %i: 0x%llx Sectors (%lli %s)", Disk,
325                         gATA_Disks[ Disk ].Sectors, val, units);
326         }
327
328         // Create Name
329         gATA_Disks[ Disk ].Name[0] = 'A'+Disk;
330         gATA_Disks[ Disk ].Name[1] = '\0';
331
332         // Get pointer to vfs node and populate it
333         node = &gATA_Disks[ Disk ].Node;
334         node->Size = gATA_Disks[Disk].Sectors * SECTOR_SIZE;
335         node->NumACLs = 0;      // Means Superuser only can access it
336         node->Inode = (Disk << 8) | 0xFF;
337         node->ImplPtr = gATA_Disks[ Disk ].Name;
338
339         node->ATime = node->MTime
340                 = node->CTime = now();
341
342         node->Read = ATA_ReadFS;
343         node->Write = ATA_WriteFS;
344         node->IOCtl = ATA_IOCtl;
345
346         // --- Scan Partitions ---
347         LOG("Reading MBR");
348         // Read Boot Sector
349         ATA_ReadDMA( Disk, 0, 1, &data.mbr );
350
351         // Check for a GPT table
352         if(data.mbr.Parts[0].SystemID == 0xEE)
353                 ATA_ParseGPT(Disk);
354         else    // No? Just parse the MBR
355                 ATA_ParseMBR(Disk, &data.mbr);
356         
357         #if DEBUG >= 2
358         ATA_ReadDMA( Disk, 1, 1, &data );
359         Debug_HexDump("ATA_ScanDisk", &data, 512);
360         #endif
361
362         LEAVE('i', 0);
363         return 1;
364 }
365
366 /**
367  * \fn void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
368  * \brief Fills a parition's information structure
369  */
370 void ATA_int_MakePartition(tATA_Partition *Part, int Disk, int Num, Uint64 Start, Uint64 Length)
371 {
372         ENTER("pPart iDisk iNum XStart XLength", Part, Disk, Num, Start, Length);
373         Part->Start = Start;
374         Part->Length = Length;
375         Part->Name[0] = 'A'+Disk;
376         if(Num >= 10) {
377                 Part->Name[1] = '1'+Num/10;
378                 Part->Name[2] = '1'+Num%10;
379                 Part->Name[3] = '\0';
380         } else {
381                 Part->Name[1] = '1'+Num;
382                 Part->Name[2] = '\0';
383         }
384         Part->Node.NumACLs = 0; // Only root can read/write raw block devices
385         Part->Node.Inode = (Disk << 8) | Num;
386         Part->Node.ImplPtr = Part->Name;
387
388         Part->Node.Read = ATA_ReadFS;
389         Part->Node.Write = ATA_WriteFS;
390         Part->Node.IOCtl = ATA_IOCtl;
391         Log_Notice("ATA", "Note '%s' at 0x%llx, 0x%llx long", Part->Name, Part->Start, Part->Length);
392         LOG("Made '%s' (&Node=%p)", Part->Name, &Part->Node);
393         LEAVE('-');
394 }
395
396 /**
397  * \fn void ATA_ParseGPT(int Disk)
398  * \brief Parses the GUID Partition Table
399  */
400 void ATA_ParseGPT(int Disk)
401 {
402         ///\todo Support GPT Disks
403         Warning("GPT Disks are currently unsupported");
404 }
405
406 /**
407  * \fn Uint16 ATA_GetPortBase(int Disk)
408  * \brief Returns the base port for a given disk
409  */
410 Uint16 ATA_GetBasePort(int Disk)
411 {
412         switch(Disk)
413         {
414         case 0: case 1:         return IDE_PRI_BASE;
415         case 2: case 3:         return IDE_SEC_BASE;
416         }
417         return 0;
418 }
419
420 /**
421  * \fn char *ATA_ReadDir(tVFS_Node *Node, int Pos)
422  */
423 char *ATA_ReadDir(tVFS_Node *Node, int Pos)
424 {
425         if(Pos >= giATA_NumNodes || Pos < 0)    return NULL;
426         return strdup( gATA_Nodes[Pos]->ImplPtr );
427 }
428
429 /**
430  * \fn tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
431  */
432 tVFS_Node *ATA_FindDir(tVFS_Node *Node, char *Name)
433 {
434          int    part;
435         tATA_Disk       *disk;
436         
437         // Check first character
438         if(Name[0] < 'A' || Name[0] > 'A'+MAX_ATA_DISKS)
439                 return NULL;
440         disk = &gATA_Disks[Name[0]-'A'];
441         // Raw Disk
442         if(Name[1] == '\0') {
443                 if( disk->Sectors == 0 && disk->Name[0] == '\0')
444                         return NULL;
445                 return &disk->Node;
446         }
447
448         // Partitions
449         if(Name[1] < '0' || '9' < Name[1])      return NULL;
450         if(Name[2] == '\0') {   // <= 9
451                 part = Name[1] - '0';
452                 part --;
453                 return &disk->Partitions[part].Node;
454         }
455         // > 9
456         if('0' > Name[2] || '9' < Name[2])      return NULL;
457         if(Name[3] != '\0')     return NULL;
458
459         part = (Name[1] - '0') * 10;
460         part += Name[2] - '0';
461         part --;
462         return &disk->Partitions[part].Node;
463
464 }
465
466 /**
467  * \fn Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
468  */
469 Uint64 ATA_ReadFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
470 {
471          int    disk = Node->Inode >> 8;
472          int    part = Node->Inode & 0xFF;
473
474         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
475
476         // Raw Disk Access
477         if(part == 0xFF)
478         {
479                 if( Offset >= gATA_Disks[disk].Sectors * SECTOR_SIZE ) {
480                         LEAVE('i', 0);
481                         return 0;
482                 }
483                 if( Offset + Length > gATA_Disks[disk].Sectors*SECTOR_SIZE )
484                         Length = gATA_Disks[disk].Sectors*SECTOR_SIZE - Offset;
485         }
486         // Partition
487         else
488         {
489                 if( Offset >= gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE ) {
490                         LEAVE('i', 0);
491                         return 0;
492                 }
493                 if( Offset + Length > gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
494                         Length = gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE - Offset;
495                 Offset += gATA_Disks[disk].Partitions[part].Start * SECTOR_SIZE;
496         }
497
498         {
499                 int ret = DrvUtil_ReadBlock(Offset, Length, Buffer, ATA_ReadRaw, SECTOR_SIZE, disk);
500                 Log("ATA_ReadFS: disk=%i, Offset=%lli, Length=%lli", disk, Offset, Length);
501                 Debug_HexDump("ATA_ReadFS", Buffer, Length);
502                 LEAVE('i', ret);
503                 return ret;
504         }
505 }
506
507 /**
508  * \fn Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
509  */
510 Uint64 ATA_WriteFS(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
511 {
512          int    disk = Node->Inode >> 8;
513          int    part = Node->Inode & 0xFF;
514
515         // Raw Disk Access
516         if(part == 0xFF)
517         {
518                 if( Offset >= gATA_Disks[disk].Sectors * SECTOR_SIZE )
519                         return 0;
520                 if( Offset + Length > gATA_Disks[disk].Sectors*SECTOR_SIZE )
521                         Length = gATA_Disks[disk].Sectors*SECTOR_SIZE - Offset;
522         }
523         // Partition
524         else
525         {
526                 if( Offset >= gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
527                         return 0;
528                 if( Offset + Length > gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE )
529                         Length = gATA_Disks[disk].Partitions[part].Length * SECTOR_SIZE - Offset;
530                 Offset += gATA_Disks[disk].Partitions[part].Start * SECTOR_SIZE;
531         }
532
533         Log("ATA_WriteFS: (Node=%p, Offset=0x%llx, Length=0x%llx, Buffer=%p)", Node, Offset, Length, Buffer);
534         Debug_HexDump("ATA_WriteFS", Buffer, Length);
535         return DrvUtil_WriteBlock(Offset, Length, Buffer, ATA_ReadRaw, ATA_WriteRaw, SECTOR_SIZE, disk);
536 }
537
538 /**
539  * \fn int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
540  * \brief IO Control Funtion
541  */
542 int ATA_IOCtl(tVFS_Node *Node, int Id, void *Data)
543 {
544         switch(Id)
545         {
546         case DRV_IOCTL_TYPE:    return DRV_TYPE_DISK;
547         }
548         return 0;
549 }
550
551 // --- Disk Access ---
552 /**
553  * \fn Uint ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
554  */
555 Uint ATA_ReadRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
556 {
557          int    ret;
558         Uint    offset;
559         Uint    done = 0;
560
561         // Pass straight on to ATA_ReadDMAPage if we can
562         if(Count <= MAX_DMA_SECTORS)
563         {
564                 ret = ATA_ReadDMA(Disk, Address, Count, Buffer);
565                 if(ret == 0)    return 0;
566                 return Count;
567         }
568
569         // Else we will have to break up the transfer
570         offset = 0;
571         while(Count > MAX_DMA_SECTORS)
572         {
573                 ret = ATA_ReadDMA(Disk, Address+offset, MAX_DMA_SECTORS, Buffer+offset);
574                 // Check for errors
575                 if(ret != 1)    return done;
576                 // Change Position
577                 done += MAX_DMA_SECTORS;
578                 Count -= MAX_DMA_SECTORS;
579                 offset += MAX_DMA_SECTORS*SECTOR_SIZE;
580         }
581
582         ret = ATA_ReadDMA(Disk, Address+offset, Count, Buffer+offset);
583         if(ret != 1)    return 0;
584         return done+Count;
585 }
586
587 /**
588  * \fn Uint ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
589  */
590 Uint ATA_WriteRaw(Uint64 Address, Uint Count, void *Buffer, Uint Disk)
591 {
592          int    ret;
593         Uint    offset;
594         Uint    done = 0;
595
596         // Pass straight on to ATA_WriteDMA, if we can
597         if(Count <= MAX_DMA_SECTORS)
598         {
599                 ret = ATA_WriteDMA(Disk, Address, Count, Buffer);
600                 if(ret == 0)    return 0;
601                 return Count;
602         }
603
604         // Else we will have to break up the transfer
605         offset = 0;
606         while(Count > MAX_DMA_SECTORS)
607         {
608                 ret = ATA_WriteDMA(Disk, Address+offset, MAX_DMA_SECTORS, Buffer+offset);
609                 // Check for errors
610                 if(ret != 1)    return done;
611                 // Change Position
612                 done += MAX_DMA_SECTORS;
613                 Count -= MAX_DMA_SECTORS;
614                 offset += MAX_DMA_SECTORS*SECTOR_SIZE;
615         }
616
617         ret = ATA_WriteDMA(Disk, Address+offset, Count, Buffer+offset);
618         if(ret != 1)    return 0;
619         return done+Count;
620 }
621
622 /**
623  * \fn int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
624  */
625 int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
626 {
627          int    cont = (Disk>>1)&1;     // Controller ID
628          int    disk = Disk & 1;
629         Uint16  base;
630         Uint8   val;
631
632         ENTER("iDisk XAddress iCount pBuffer", Disk, Address, Count, Buffer);
633
634         // Check if the count is small enough
635         if(Count > MAX_DMA_SECTORS) {
636                 Warning("Passed too many sectors for a bulk DMA read (%i > %i)",
637                         Count, MAX_DMA_SECTORS);
638                 LEAVE('i');
639                 return 0;
640         }
641
642         // Get exclusive access to the disk controller
643         LOCK( &giaATA_ControllerLock[ cont ] );
644
645         // Set Size
646         gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE;
647
648         // Get Port Base
649         base = ATA_GetBasePort(Disk);
650
651         // Reset IRQ Flag
652         gaATA_IRQs[cont] = 0;
653
654         // Set up transfer
655         outb(base+0x01, 0x00);
656         if( Address > 0x0FFFFFFF )      // Use LBA48
657         {
658                 outb(base+0x6, 0x40 | (disk << 4));
659                 outb(base+0x2, 0 >> 8); // Upper Sector Count
660                 outb(base+0x3, Address >> 24);  // Low 2 Addr
661                 outb(base+0x4, Address >> 28);  // Mid 2 Addr
662                 outb(base+0x5, Address >> 32);  // High 2 Addr
663         }
664         else
665         {
666                 outb(base+0x06, 0xE0 | (disk << 4) | ((Address >> 24) & 0x0F)); // Magic, Disk, High addr
667         }
668
669         outb(base+0x02, (Uint8) Count);         // Sector Count
670         outb(base+0x03, (Uint8) Address);               // Low Addr
671         outb(base+0x04, (Uint8) (Address >> 8));        // Middle Addr
672         outb(base+0x05, (Uint8) (Address >> 16));       // High Addr
673
674         LOG("Starting Transfer");
675         if( Address > 0x0FFFFFFF )
676                 outb(base+0x07, HDD_DMA_R48);   // Read Command (LBA48)
677         else
678                 outb(base+0x07, HDD_DMA_R28);   // Read Command (LBA28)
679         // Start transfer
680         ATA_int_BusMasterWriteByte( cont << 3, 9 );     // Read and start
681
682         // Wait for transfer to complete
683         while( gaATA_IRQs[cont] == 0 )  Threads_Yield();
684
685         // Complete Transfer
686         ATA_int_BusMasterWriteByte( cont << 3, 8 );     // Read and stop
687
688         val = inb(base+0x7);
689         LOG("Status byte = 0x%02x", val);
690
691         LOG("Transfer Completed & Acknowledged");
692
693         // Copy to destination buffer
694         memcpy( Buffer, gATA_Buffers[cont], Count*SECTOR_SIZE );
695
696         // Release controller lock
697         RELEASE( &giaATA_ControllerLock[ cont ] );
698
699         LEAVE('i', 1);
700         return 1;
701 }
702
703 /**
704  * \fn int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
705  */
706 int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer)
707 {
708          int    cont = (Disk>>1)&1;     // Controller ID
709          int    disk = Disk & 1;
710         Uint16  base;
711
712         // Check if the count is small enough
713         if(Count > MAX_DMA_SECTORS)     return 0;
714
715         // Get exclusive access to the disk controller
716         LOCK( &giaATA_ControllerLock[ cont ] );
717
718         // Set Size
719         gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE;
720
721         // Get Port Base
722         base = ATA_GetBasePort(Disk);
723
724         // Set up transfer
725         outb(base+0x01, 0x00);
726         if( Address > 0x0FFFFFFF )      // Use LBA48
727         {
728                 outb(base+0x6, 0x40 | (disk << 4));
729                 outb(base+0x2, 0 >> 8); // Upper Sector Count
730                 outb(base+0x3, Address >> 24);  // Low 2 Addr
731                 outb(base+0x3, Address >> 28);  // Mid 2 Addr
732                 outb(base+0x3, Address >> 32);  // High 2 Addr
733         }
734         else
735         {
736                 outb(base+0x06, 0xE0 | (disk << 4) | ((Address >> 24) & 0x0F)); //Disk,Magic,High addr
737         }
738
739         outb(base+0x02, (Uint8) Count);         // Sector Count
740         outb(base+0x03, (Uint8) Address);               // Low Addr
741         outb(base+0x04, (Uint8) (Address >> 8));        // Middle Addr
742         outb(base+0x05, (Uint8) (Address >> 16));       // High Addr
743         if( Address > 0x0FFFFFFF )
744                 outb(base+0x07, HDD_DMA_W48);   // Write Command (LBA48)
745         else
746                 outb(base+0x07, HDD_DMA_W28);   // Write Command (LBA28)
747
748         // Reset IRQ Flag
749         gaATA_IRQs[cont] = 0;
750
751         // Copy to output buffer
752         memcpy( gATA_Buffers[cont], Buffer, Count*SECTOR_SIZE );
753
754         // Start transfer
755         ATA_int_BusMasterWriteByte( cont << 3, 1 );     // Write and start
756
757         // Wait for transfer to complete
758         while( gaATA_IRQs[cont] == 0 )  Threads_Yield();
759
760         // Complete Transfer
761         ATA_int_BusMasterWriteByte( cont << 3, 0 );     // Write and stop
762
763         // Release controller lock
764         RELEASE( &giaATA_ControllerLock[ cont ] );
765
766         return 1;
767 }
768
769 /**
770  * \fn void ATA_IRQHandlerPri(int unused)
771  */
772 void ATA_IRQHandlerPri(int unused)
773 {
774         Uint8   val;
775
776         // IRQ bit set for Primary Controller
777         val = ATA_int_BusMasterReadByte( 0x2 );
778         LOG("IRQ val = 0x%x", val);
779         if(val & 4) {
780                 LOG("IRQ hit (val = 0x%x)", val);
781                 ATA_int_BusMasterWriteByte( 0x2, 4 );
782                 gaATA_IRQs[0] = 1;
783                 return ;
784         }
785 }
786
787 /**
788  * \fn void ATA_IRQHandlerSec(int unused)
789  */
790 void ATA_IRQHandlerSec(int unused)
791 {
792         Uint8   val;
793         // IRQ bit set for Secondary Controller
794         val = ATA_int_BusMasterReadByte( 0xA );
795         LOG("IRQ val = 0x%x", val);
796         if(val & 4) {
797                 LOG("IRQ hit (val = 0x%x)", val);
798                 ATA_int_BusMasterWriteByte( 0xA, 4 );
799                 gaATA_IRQs[1] = 1;
800                 return ;
801         }
802 }
803
804 /**
805  * \fn Uint8 ATA_int_BusMasterReadByte(int Ofs)
806  */
807 Uint8 ATA_int_BusMasterReadByte(int Ofs)
808 {
809         if( gATA_BusMasterBase & 1 )
810                 return inb( (gATA_BusMasterBase & ~1) + Ofs );
811         else
812                 return *(Uint8*)(gATA_BusMasterBasePtr + Ofs);
813 }
814
815 /**
816  * \fn void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
817  * \brief Writes a byte to a Bus Master Register
818  */
819 void ATA_int_BusMasterWriteByte(int Ofs, Uint8 Value)
820 {
821         if( gATA_BusMasterBase & 1 )
822                 outb( (gATA_BusMasterBase & ~1) + Ofs, Value );
823         else
824                 *(Uint8*)(gATA_BusMasterBasePtr + Ofs) = Value;
825 }
826
827 /**
828  * \fn void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
829  * \brief Writes a dword to a Bus Master Register
830  */
831 void ATA_int_BusMasterWriteDWord(int Ofs, Uint32 Value)
832 {
833
834         if( gATA_BusMasterBase & 1 )
835                 outd( (gATA_BusMasterBase & ~1) + Ofs, Value );
836         else
837                 *(Uint32*)(gATA_BusMasterBasePtr + Ofs) = Value;
838 }

UCC git Repository :: git.ucc.asn.au