Don't flush caches when waiting for free MailBox slot in VC. Instead use
[AROS.git] / rom / devs / ata / ata_init.c
bloba4b5a1eae020bcc36028496959f30828037c7196
1 /*
2 Copyright © 2004-2013, The AROS Development Team. All rights reserved
3 $Id$
5 Desc:
6 Lang: English
7 */
9 #define __OOP_NOMETHODBASES__
11 #include <aros/bootloader.h>
12 #include <aros/debug.h>
13 #include <aros/symbolsets.h>
14 #include <exec/exec.h>
15 #include <exec/resident.h>
16 #include <exec/tasks.h>
17 #include <exec/memory.h>
18 #include <exec/nodes.h>
19 #include <hidd/hidd.h>
20 #include <utility/utility.h>
21 #include <libraries/expansion.h>
22 #include <libraries/configvars.h>
23 #include <dos/bptr.h>
24 #include <dos/dosextens.h>
25 #include <dos/filehandler.h>
27 #include <proto/exec.h>
28 #include <proto/timer.h>
29 #include <proto/bootloader.h>
30 #include <proto/expansion.h>
31 #include <proto/oop.h>
33 #include <string.h>
35 #include "ata.h"
36 #include "timer.h"
38 #include LC_LIBDEFS_FILE
40 /* Add a bootnode using expansion.library */
41 BOOL ata_RegisterVolume(ULONG StartCyl, ULONG EndCyl, struct ata_Unit *unit)
43 struct ExpansionBase *ExpansionBase;
44 struct DeviceNode *devnode;
45 TEXT dosdevname[4] = "HD0";
46 const ULONG IdDOS = AROS_MAKE_ID('D','O','S','\001');
47 const ULONG IdCDVD = AROS_MAKE_ID('C','D','V','D');
49 ExpansionBase = (struct ExpansionBase *)OpenLibrary("expansion.library",
50 40L);
52 if (ExpansionBase)
54 IPTR pp[24];
56 /* This should be dealt with using some sort of volume manager or such. */
57 switch (unit->au_DevType)
59 case DG_DIRECT_ACCESS:
60 break;
61 case DG_CDROM:
62 dosdevname[0] = 'C';
63 break;
64 default:
65 D(bug("[ATA>>]:-ata_RegisterVolume called on unknown devicetype\n"));
68 if (unit->au_UnitNum < 10)
69 dosdevname[2] += unit->au_UnitNum % 10;
70 else
71 dosdevname[2] = 'A' - 10 + unit->au_UnitNum;
73 pp[0] = (IPTR)dosdevname;
74 pp[1] = (IPTR)MOD_NAME_STRING;
75 pp[2] = unit->au_UnitNum;
76 pp[DE_TABLESIZE + 4] = DE_BOOTBLOCKS;
77 pp[DE_SIZEBLOCK + 4] = 1 << (unit->au_SectorShift - 2);
78 pp[DE_NUMHEADS + 4] = unit->au_Heads;
79 pp[DE_SECSPERBLOCK + 4] = 1;
80 pp[DE_BLKSPERTRACK + 4] = unit->au_Sectors;
81 pp[DE_RESERVEDBLKS + 4] = 2;
82 pp[DE_LOWCYL + 4] = StartCyl;
83 pp[DE_HIGHCYL + 4] = EndCyl;
84 pp[DE_NUMBUFFERS + 4] = 10;
85 pp[DE_BUFMEMTYPE + 4] = MEMF_PUBLIC | MEMF_31BIT;
86 pp[DE_MAXTRANSFER + 4] = 0x00200000;
87 pp[DE_MASK + 4] = 0x7FFFFFFE;
88 pp[DE_BOOTPRI + 4] = ((unit->au_DevType == DG_DIRECT_ACCESS) ? 0 : 10);
89 pp[DE_DOSTYPE + 4] = ((unit->au_DevType == DG_DIRECT_ACCESS) ? IdDOS : IdCDVD);
90 pp[DE_CONTROL + 4] = 0;
91 pp[DE_BOOTBLOCKS + 4] = 2;
93 devnode = MakeDosNode(pp);
95 if (devnode)
97 D(bug("[ATA>>]:-ata_RegisterVolume: '%b', type=0x%08lx with StartCyl=%d, EndCyl=%d .. ",
98 devnode->dn_Name, pp[DE_DOSTYPE + 4], StartCyl, EndCyl));
100 AddBootNode(pp[DE_BOOTPRI + 4], ADNF_STARTPROC, devnode, NULL);
101 D(bug("done\n"));
103 return TRUE;
106 CloseLibrary((struct Library *)ExpansionBase);
109 return FALSE;
112 /* Keep order the same as order of IDs in struct ataBase! */
113 static CONST_STRPTR const attrBaseIDs[] =
115 IID_Hidd_ATAUnit,
116 IID_HW,
117 IID_Hidd_ATABus,
118 NULL
121 #define ATA_METHOD_ID_START 1
124 Here shall we start. Make function static as it shouldn't be visible from
125 outside.
127 static int ata_init(struct ataBase *ATABase)
129 OOP_Object *hwRoot;
130 struct BootLoaderBase *BootLoaderBase;
132 D(bug("[ATA--] ata_init: ata.device Initialization\n"));
134 ATABase->ata_UtilityBase = OpenLibrary("utility.library", 36);
135 if (!ATABase->ata_UtilityBase)
136 return FALSE;
139 * I've decided to use memory pools again. Alloc everything needed from
140 * a pool, so that we avoid memory fragmentation.
142 ATABase->ata_MemPool = CreatePool(MEMF_CLEAR | MEMF_PUBLIC | MEMF_SEM_PROTECTED , 8192, 4096);
143 if (ATABase->ata_MemPool == NULL)
144 return FALSE;
146 D(bug("[ATA--] ata_init: MemPool @ %p\n", ATABase->ata_MemPool));
148 if (OOP_ObtainAttrBasesArray(&ATABase->unitAttrBase, attrBaseIDs))
149 return FALSE;
151 /* This is our own method base, so no check needed */
152 if (OOP_ObtainMethodBasesArray(&ATABase->hwMethodBase, &attrBaseIDs[ATA_METHOD_ID_START]))
153 return FALSE;
155 hwRoot = OOP_NewObject(NULL, CLID_HW_Root, NULL);
156 if (!hwRoot)
157 return FALSE;
159 if (!HW_AddDriver(hwRoot, ATABase->ataClass, NULL))
160 return FALSE;
162 /* Set default ata.device config options */
163 ATABase->ata_32bit = FALSE;
164 ATABase->ata_NoMulti = FALSE;
165 ATABase->ata_NoDMA = FALSE;
166 ATABase->ata_Poll = FALSE;
169 * start initialization:
170 * obtain kernel parameters
172 BootLoaderBase = OpenResource("bootloader.resource");
173 D(bug("[ATA--] ata_init: BootloaderBase = %p\n", BootLoaderBase));
174 if (BootLoaderBase != NULL)
176 struct List *list;
177 struct Node *node;
179 list = (struct List *)GetBootInfo(BL_Args);
180 if (list)
182 ForeachNode(list, node)
184 if (strncmp(node->ln_Name, "ATA=", 4) == 0)
186 const char *CmdLine = &node->ln_Name[4];
188 if (strstr(CmdLine, "32bit"))
190 D(bug("[ATA ] ata_init: Using 32-bit IO transfers\n"));
191 ATABase->ata_32bit = TRUE;
193 if (strstr(CmdLine, "nomulti"))
195 D(bug("[ATA ] ata_init: Disabled multisector transfers\n"));
196 ATABase->ata_NoMulti = TRUE;
198 if (strstr(CmdLine, "nodma"))
200 D(bug("[ATA ] ata_init: Disabled DMA transfers\n"));
201 ATABase->ata_NoDMA = TRUE;
203 if (strstr(CmdLine, "poll"))
205 D(bug("[ATA ] ata_init: Using polling to detect end of busy state\n"));
206 ATABase->ata_Poll = TRUE;
213 /* Try to setup daemon task looking for diskchanges */
214 NEWLIST(&ATABase->Daemon_ios);
215 InitSemaphore(&ATABase->DaemonSem);
216 InitSemaphore(&ATABase->DetectionSem);
217 ATABase->daemonParent = FindTask(NULL);
218 SetSignal(0, SIGF_SINGLE);
220 if (!NewCreateTask(TASKTAG_PC, DaemonCode,
221 TASKTAG_NAME , "ATA.daemon",
222 TASKTAG_STACKSIZE , STACK_SIZE,
223 TASKTAG_TASKMSGPORT, &ATABase->DaemonPort,
224 TASKTAG_PRI , TASK_PRI - 1, /* The daemon should have a little bit lower Pri than handler tasks */
225 TASKTAG_ARG1 , ATABase,
226 TAG_DONE))
228 D(bug("[ATA ] Failed to start up daemon!\n"));
229 return FALSE;
232 /* Wait for handshake */
233 Wait(SIGF_SINGLE);
234 D(bug("[ATA ] Daemon task set to 0x%p\n", ATABase->ata_Daemon));
236 return ATABase->ata_Daemon ? TRUE : FALSE;
239 static int ata_expunge(struct ataBase *ATABase)
241 if (ATABase->ataObj)
244 * CLID_HWRoot is a singletone, you can get it as many times as
245 * you want. Here we save up some space in struct ataBase by
246 * obtaining hwRoot object only when we need it. This happens
247 * rarely, so small performance loss is OK here.
249 OOP_Object *hwRoot = OOP_NewObject(NULL, CLID_HW_Root, NULL);
251 if (HW_RemoveDriver(hwRoot, ATABase->ataObj))
253 /* Destroy our singletone */
254 OOP_MethodID disp_msg = OOP_GetMethodID(IID_Root, moRoot_Dispose);
256 D(bug("[ATA ] ata_expunge: Stopping Daemon...\n"));
257 ATABase->daemonParent = FindTask(NULL);
258 SetSignal(0, SIGF_SINGLE);
259 Signal(ATABase->ata_Daemon, SIGBREAKF_CTRL_C);
260 Wait(SIGF_SINGLE);
262 D(bug("[ATA ] ata_expunge: Done, destroying subystem object\n"));
263 OOP_DoSuperMethod(ATABase->ataClass, ATABase->ataObj, &disp_msg);
265 else
267 /* Our subsystem is in use, we have some bus driver(s) around. */
268 D(bug("[ATA ] ata_expunge: ATA subsystem is in use\n"));
269 return FALSE;
273 D(bug("[ATA ] ata_expunge: Releasing attribute bases\n"));
274 OOP_ReleaseAttrBasesArray(&ATABase->hwAttrBase, attrBaseIDs);
276 if (ATABase->ata_UtilityBase)
277 CloseLibrary(ATABase->ata_UtilityBase);
279 D(bug("[ATA ] ata_expunge: Exiting\n"));
280 return TRUE;
283 static int open(struct ataBase *ATABase, struct IORequest *iorq,
284 ULONG unitnum, ULONG flags)
286 struct Hook searchHook =
288 .h_Entry = Hidd_ATABus_Open,
289 .h_Data = iorq
292 /* Assume it failed */
293 iorq->io_Error = IOERR_OPENFAIL;
294 iorq->io_Device = &ATABase->ata_Device;
295 iorq->io_Unit = (APTR)(IPTR)-1;
297 /* Try to find the unit */
298 HW_EnumDrivers(ATABase->ataObj, &searchHook, (APTR)(IPTR)unitnum);
300 D(bug("[ATA%02d] Open result: %d\n", unitnum, iorq->io_Error));
302 /* If found, io_Error will be reset to zero */
303 return iorq->io_Error ? FALSE : TRUE;
306 /* Close given device */
307 static int close
309 LIBBASETYPEPTR LIBBASE,
310 struct IORequest *iorq
313 struct ata_Unit *unit = (struct ata_Unit *)iorq->io_Unit;
315 /* First of all make the important fields of struct IORequest invalid! */
316 iorq->io_Unit = (struct Unit *)~0;
318 /* Decrease use counters of unit */
319 unit->au_Unit.unit_OpenCnt--;
321 return TRUE;
324 ADD2INITLIB(ata_init, 0)
325 ADD2EXPUNGELIB(ata_expunge, 0)
326 ADD2OPENDEV(open, 0)
327 ADD2CLOSEDEV(close, 0)