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

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