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

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