ahci.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/disk.h>
  20. #include <grub/mm.h>
  21. #include <grub/time.h>
  22. #include <grub/pci.h>
  23. #include <grub/ata.h>
  24. #include <grub/scsi.h>
  25. #include <grub/misc.h>
  26. #include <grub/list.h>
  27. #include <grub/loader.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. struct grub_ahci_cmd_head
  30. {
  31. grub_uint32_t config;
  32. grub_uint32_t transferred;
  33. grub_uint64_t command_table_base;
  34. grub_uint32_t unused[4];
  35. };
  36. struct grub_ahci_prdt_entry
  37. {
  38. grub_uint64_t data_base;
  39. grub_uint32_t unused;
  40. grub_uint32_t size;
  41. };
  42. struct grub_ahci_cmd_table
  43. {
  44. grub_uint8_t cfis[0x40];
  45. grub_uint8_t command[0x10];
  46. grub_uint8_t reserved[0x30];
  47. struct grub_ahci_prdt_entry prdt[1];
  48. };
  49. struct grub_ahci_hba_port
  50. {
  51. grub_uint64_t command_list_base;
  52. grub_uint64_t fis_base;
  53. grub_uint32_t intstatus;
  54. grub_uint32_t inten;
  55. grub_uint32_t command;
  56. grub_uint32_t unused1;
  57. grub_uint32_t task_file_data;
  58. grub_uint32_t sig;
  59. grub_uint32_t status;
  60. grub_uint32_t unused2;
  61. grub_uint32_t sata_error;
  62. grub_uint32_t sata_active;
  63. grub_uint32_t command_issue;
  64. grub_uint32_t unused3;
  65. grub_uint32_t fbs;
  66. grub_uint32_t unused4[15];
  67. };
  68. enum grub_ahci_hba_port_command
  69. {
  70. GRUB_AHCI_HBA_PORT_CMD_ST = 0x01,
  71. GRUB_AHCI_HBA_PORT_CMD_SPIN_UP = 0x02,
  72. GRUB_AHCI_HBA_PORT_CMD_POWER_ON = 0x04,
  73. GRUB_AHCI_HBA_PORT_CMD_FRE = 0x10,
  74. GRUB_AHCI_HBA_PORT_CMD_CR = 0x8000,
  75. GRUB_AHCI_HBA_PORT_CMD_FR = 0x4000,
  76. };
  77. struct grub_ahci_hba
  78. {
  79. grub_uint32_t cap;
  80. grub_uint32_t global_control;
  81. grub_uint32_t intr_status;
  82. grub_uint32_t ports_implemented;
  83. grub_uint32_t unused1[6];
  84. grub_uint32_t bios_handoff;
  85. grub_uint32_t unused2[53];
  86. struct grub_ahci_hba_port ports[32];
  87. };
  88. struct grub_ahci_received_fis
  89. {
  90. char raw[4096];
  91. };
  92. enum
  93. {
  94. GRUB_AHCI_HBA_CAP_NPORTS_MASK = 0x1f
  95. };
  96. enum
  97. {
  98. GRUB_AHCI_HBA_GLOBAL_CONTROL_RESET = 0x00000001,
  99. GRUB_AHCI_HBA_GLOBAL_CONTROL_INTR_EN = 0x00000002,
  100. GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN = 0x80000000,
  101. };
  102. enum
  103. {
  104. GRUB_AHCI_BIOS_HANDOFF_BIOS_OWNED = 1,
  105. GRUB_AHCI_BIOS_HANDOFF_OS_OWNED = 2,
  106. GRUB_AHCI_BIOS_HANDOFF_OS_OWNERSHIP_CHANGED = 8,
  107. GRUB_AHCI_BIOS_HANDOFF_RWC = 8
  108. };
  109. struct grub_ahci_device
  110. {
  111. struct grub_ahci_device *next;
  112. struct grub_ahci_device **prev;
  113. volatile struct grub_ahci_hba *hba;
  114. int port;
  115. int num;
  116. struct grub_pci_dma_chunk *command_list_chunk;
  117. volatile struct grub_ahci_cmd_head *command_list;
  118. struct grub_pci_dma_chunk *command_table_chunk;
  119. volatile struct grub_ahci_cmd_table *command_table;
  120. struct grub_pci_dma_chunk *rfis;
  121. int present;
  122. int atapi;
  123. };
  124. static grub_err_t
  125. grub_ahci_readwrite_real (struct grub_ahci_device *dev,
  126. struct grub_disk_ata_pass_through_parms *parms,
  127. int spinup, int reset);
  128. enum
  129. {
  130. GRUB_AHCI_CONFIG_READ = 0,
  131. GRUB_AHCI_CONFIG_CFIS_LENGTH_MASK = 0x1f,
  132. GRUB_AHCI_CONFIG_ATAPI = 0x20,
  133. GRUB_AHCI_CONFIG_WRITE = 0x40,
  134. GRUB_AHCI_CONFIG_PREFETCH = 0x80,
  135. GRUB_AHCI_CONFIG_RESET = 0x100,
  136. GRUB_AHCI_CONFIG_BIST = 0x200,
  137. GRUB_AHCI_CONFIG_CLEAR_R_OK = 0x400,
  138. GRUB_AHCI_CONFIG_PMP_MASK = 0xf000,
  139. GRUB_AHCI_CONFIG_PRDT_LENGTH_MASK = 0xffff0000,
  140. };
  141. #define GRUB_AHCI_CONFIG_CFIS_LENGTH_SHIFT 0
  142. #define GRUB_AHCI_CONFIG_PMP_SHIFT 12
  143. #define GRUB_AHCI_CONFIG_PRDT_LENGTH_SHIFT 16
  144. #define GRUB_AHCI_INTERRUPT_ON_COMPLETE 0x80000000
  145. #define GRUB_AHCI_PRDT_MAX_CHUNK_LENGTH 0x200000
  146. static struct grub_ahci_device *grub_ahci_devices;
  147. static int numdevs;
  148. static int
  149. grub_ahci_pciinit (grub_pci_device_t dev,
  150. grub_pci_id_t pciid __attribute__ ((unused)),
  151. void *data __attribute__ ((unused)))
  152. {
  153. grub_pci_address_t addr;
  154. grub_uint32_t class;
  155. grub_uint32_t bar;
  156. unsigned i, nports;
  157. volatile struct grub_ahci_hba *hba;
  158. /* Read class. */
  159. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  160. class = grub_pci_read (addr);
  161. /* Check if this class ID matches that of a PCI IDE Controller. */
  162. if (class >> 8 != 0x010601)
  163. return 0;
  164. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG5);
  165. bar = grub_pci_read (addr);
  166. if ((bar & (GRUB_PCI_ADDR_SPACE_MASK | GRUB_PCI_ADDR_MEM_TYPE_MASK
  167. | GRUB_PCI_ADDR_MEM_PREFETCH))
  168. != (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_32))
  169. return 0;
  170. addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  171. grub_pci_write_word (addr, grub_pci_read_word (addr)
  172. | GRUB_PCI_COMMAND_MEM_ENABLED | GRUB_PCI_COMMAND_BUS_MASTER);
  173. hba = grub_pci_device_map_range (dev, bar & GRUB_PCI_ADDR_MEM_MASK,
  174. sizeof (*hba));
  175. grub_dprintf ("ahci", "dev: %x:%x.%x\n", dev.bus, dev.device, dev.function);
  176. grub_dprintf ("ahci", "tfd[0]: %x\n",
  177. hba->ports[0].task_file_data);
  178. grub_dprintf ("ahci", "cmd[0]: %x\n",
  179. hba->ports[0].command);
  180. grub_dprintf ("ahci", "st[0]: %x\n",
  181. hba->ports[0].status);
  182. grub_dprintf ("ahci", "err[0]: %x\n",
  183. hba->ports[0].sata_error);
  184. grub_dprintf ("ahci", "tfd[1]: %x\n",
  185. hba->ports[1].task_file_data);
  186. grub_dprintf ("ahci", "cmd[1]: %x\n",
  187. hba->ports[1].command);
  188. grub_dprintf ("ahci", "st[1]: %x\n",
  189. hba->ports[1].status);
  190. grub_dprintf ("ahci", "err[1]: %x\n",
  191. hba->ports[1].sata_error);
  192. hba->ports[1].sata_error = hba->ports[1].sata_error;
  193. grub_dprintf ("ahci", "err[1]: %x\n",
  194. hba->ports[1].sata_error);
  195. grub_dprintf ("ahci", "BH:%x\n", hba->bios_handoff);
  196. if (! (hba->bios_handoff & GRUB_AHCI_BIOS_HANDOFF_OS_OWNED))
  197. {
  198. grub_uint64_t endtime;
  199. grub_dprintf ("ahci", "Requesting AHCI ownership\n");
  200. hba->bios_handoff = (hba->bios_handoff & ~GRUB_AHCI_BIOS_HANDOFF_RWC)
  201. | GRUB_AHCI_BIOS_HANDOFF_OS_OWNED;
  202. grub_dprintf ("ahci", "Waiting for BIOS to give up ownership\n");
  203. endtime = grub_get_time_ms () + 1000;
  204. while ((hba->bios_handoff & GRUB_AHCI_BIOS_HANDOFF_BIOS_OWNED)
  205. && grub_get_time_ms () < endtime);
  206. if (hba->bios_handoff & GRUB_AHCI_BIOS_HANDOFF_BIOS_OWNED)
  207. {
  208. grub_dprintf ("ahci", "Forcibly taking ownership\n");
  209. hba->bios_handoff = GRUB_AHCI_BIOS_HANDOFF_OS_OWNED;
  210. hba->bios_handoff |= GRUB_AHCI_BIOS_HANDOFF_OS_OWNERSHIP_CHANGED;
  211. }
  212. else
  213. grub_dprintf ("ahci", "AHCI ownership obtained\n");
  214. }
  215. else
  216. grub_dprintf ("ahci", "AHCI is already in OS mode\n");
  217. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  218. grub_dprintf ("ahci", "err[1]: %x\n",
  219. hba->ports[1].sata_error);
  220. if (!(hba->global_control & GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN))
  221. grub_dprintf ("ahci", "AHCI is in compat mode. Switching\n");
  222. else
  223. grub_dprintf ("ahci", "AHCI is in AHCI mode.\n");
  224. grub_dprintf ("ahci", "err[1]: %x\n",
  225. hba->ports[1].sata_error);
  226. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  227. /* {
  228. grub_uint64_t endtime;
  229. hba->global_control |= 1;
  230. endtime = grub_get_time_ms () + 1000;
  231. while (hba->global_control & 1)
  232. if (grub_get_time_ms () > endtime)
  233. {
  234. grub_dprintf ("ahci", "couldn't reset AHCI\n");
  235. return 0;
  236. }
  237. }*/
  238. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  239. grub_dprintf ("ahci", "err[1]: %x\n",
  240. hba->ports[1].sata_error);
  241. for (i = 0; i < 5; i++)
  242. {
  243. hba->global_control |= GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN;
  244. grub_millisleep (1);
  245. if (hba->global_control & GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN)
  246. break;
  247. }
  248. if (i == 5)
  249. {
  250. grub_dprintf ("ahci", "Couldn't put AHCI in AHCI mode\n");
  251. return 0;
  252. }
  253. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  254. grub_dprintf ("ahci", "err[1]: %x\n",
  255. hba->ports[1].sata_error);
  256. grub_dprintf ("ahci", "err[1]: %x\n",
  257. hba->ports[1].sata_error);
  258. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  259. for (i = 0; i < 5; i++)
  260. {
  261. hba->global_control |= GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN;
  262. grub_millisleep (1);
  263. if (hba->global_control & GRUB_AHCI_HBA_GLOBAL_CONTROL_AHCI_EN)
  264. break;
  265. }
  266. if (i == 5)
  267. {
  268. grub_dprintf ("ahci", "Couldn't put AHCI in AHCI mode\n");
  269. return 0;
  270. }
  271. grub_dprintf ("ahci", "err[1]: %x\n",
  272. hba->ports[1].sata_error);
  273. grub_dprintf ("ahci", "GLC:%x\n", hba->global_control);
  274. nports = (GRUB_AHCI_HBA_CAP_NPORTS_MASK) + 1;
  275. grub_dprintf ("ahci", "%d AHCI ports, PI = 0x%x\n", nports,
  276. hba->ports_implemented);
  277. struct grub_ahci_device *adevs[GRUB_AHCI_HBA_CAP_NPORTS_MASK + 1];
  278. struct grub_ahci_device *failed_adevs[GRUB_AHCI_HBA_CAP_NPORTS_MASK + 1];
  279. grub_uint32_t fr_running = 0;
  280. for (i = 0; i < nports; i++)
  281. failed_adevs[i] = 0;
  282. for (i = 0; i < nports; i++)
  283. {
  284. if (!(hba->ports_implemented & (1 << i)))
  285. {
  286. adevs[i] = 0;
  287. continue;
  288. }
  289. adevs[i] = grub_zalloc (sizeof (*adevs[i]));
  290. if (!adevs[i])
  291. return 1;
  292. adevs[i]->hba = hba;
  293. adevs[i]->port = i;
  294. adevs[i]->present = 1;
  295. adevs[i]->num = numdevs++;
  296. }
  297. for (i = 0; i < nports; i++)
  298. if (adevs[i])
  299. {
  300. adevs[i]->hba->ports[adevs[i]->port].sata_error = adevs[i]->hba->ports[adevs[i]->port].sata_error;
  301. grub_dprintf ("ahci", "port: %d, err: %x\n", adevs[i]->port,
  302. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  303. adevs[i]->command_list_chunk = grub_memalign_dma32 (1024, sizeof (struct grub_ahci_cmd_head) * 32);
  304. if (!adevs[i]->command_list_chunk)
  305. {
  306. adevs[i] = 0;
  307. continue;
  308. }
  309. adevs[i]->command_table_chunk = grub_memalign_dma32 (1024,
  310. sizeof (struct grub_ahci_cmd_table));
  311. if (!adevs[i]->command_table_chunk)
  312. {
  313. grub_dma_free (adevs[i]->command_list_chunk);
  314. adevs[i] = 0;
  315. continue;
  316. }
  317. adevs[i]->command_list = grub_dma_get_virt (adevs[i]->command_list_chunk);
  318. adevs[i]->command_table = grub_dma_get_virt (adevs[i]->command_table_chunk);
  319. grub_memset ((void *) adevs[i]->command_list, 0,
  320. sizeof (struct grub_ahci_cmd_table));
  321. grub_memset ((void *) adevs[i]->command_table, 0,
  322. sizeof (struct grub_ahci_cmd_head) * 32);
  323. adevs[i]->command_list->command_table_base
  324. = grub_dma_get_phys (adevs[i]->command_table_chunk);
  325. grub_dprintf ("ahci", "found device ahci%d (port %d), command_table = %p, command_list = %p\n",
  326. adevs[i]->num, adevs[i]->port, grub_dma_get_virt (adevs[i]->command_table_chunk),
  327. grub_dma_get_virt (adevs[i]->command_list_chunk));
  328. adevs[i]->hba->ports[adevs[i]->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  329. }
  330. grub_uint64_t endtime;
  331. endtime = grub_get_time_ms () + 1000;
  332. while (grub_get_time_ms () < endtime)
  333. {
  334. for (i = 0; i < nports; i++)
  335. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  336. break;
  337. if (i == nports)
  338. break;
  339. }
  340. for (i = 0; i < nports; i++)
  341. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  342. {
  343. grub_dprintf ("ahci", "couldn't stop FR on port %d\n", i);
  344. failed_adevs[i] = adevs[i];
  345. adevs[i] = 0;
  346. }
  347. for (i = 0; i < nports; i++)
  348. if (adevs[i])
  349. adevs[i]->hba->ports[adevs[i]->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_ST;
  350. endtime = grub_get_time_ms () + 1000;
  351. while (grub_get_time_ms () < endtime)
  352. {
  353. for (i = 0; i < nports; i++)
  354. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  355. break;
  356. if (i == nports)
  357. break;
  358. }
  359. for (i = 0; i < nports; i++)
  360. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  361. {
  362. grub_dprintf ("ahci", "couldn't stop CR on port %d\n", i);
  363. failed_adevs[i] = adevs[i];
  364. adevs[i] = 0;
  365. }
  366. for (i = 0; i < nports; i++)
  367. if (adevs[i])
  368. {
  369. adevs[i]->hba->ports[adevs[i]->port].inten = 0;
  370. adevs[i]->hba->ports[adevs[i]->port].intstatus = ~0;
  371. // adevs[i]->hba->ports[adevs[i]->port].fbs = 0;
  372. grub_dprintf ("ahci", "port: %d, err: %x\n", adevs[i]->port,
  373. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  374. adevs[i]->rfis = grub_memalign_dma32 (4096,
  375. sizeof (struct grub_ahci_received_fis));
  376. grub_memset ((char *) grub_dma_get_virt (adevs[i]->rfis), 0,
  377. sizeof (struct grub_ahci_received_fis));
  378. grub_memset ((char *) grub_dma_get_virt (adevs[i]->command_list_chunk), 0,
  379. sizeof (struct grub_ahci_cmd_head));
  380. grub_memset ((char *) grub_dma_get_virt (adevs[i]->command_table_chunk), 0,
  381. sizeof (struct grub_ahci_cmd_table));
  382. adevs[i]->hba->ports[adevs[i]->port].fis_base = grub_dma_get_phys (adevs[i]->rfis);
  383. adevs[i]->hba->ports[adevs[i]->port].command_list_base
  384. = grub_dma_get_phys (adevs[i]->command_list_chunk);
  385. adevs[i]->hba->ports[adevs[i]->port].command_issue = 0;
  386. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_FRE;
  387. }
  388. endtime = grub_get_time_ms () + 1000;
  389. while (grub_get_time_ms () < endtime)
  390. {
  391. for (i = 0; i < nports; i++)
  392. if (adevs[i] && !(adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  393. break;
  394. if (i == nports)
  395. break;
  396. }
  397. for (i = 0; i < nports; i++)
  398. if (adevs[i] && !(adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  399. {
  400. grub_dprintf ("ahci", "couldn't start FR on port %d\n", i);
  401. failed_adevs[i] = adevs[i];
  402. adevs[i] = 0;
  403. }
  404. for (i = 0; i < nports; i++)
  405. if (adevs[i])
  406. {
  407. grub_dprintf ("ahci", "port: %d, err: %x\n", adevs[i]->port,
  408. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  409. fr_running |= (1 << i);
  410. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_SPIN_UP;
  411. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_POWER_ON;
  412. adevs[i]->hba->ports[adevs[i]->port].command |= 1 << 28;
  413. grub_dprintf ("ahci", "port: %d, err: %x\n", adevs[i]->port,
  414. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  415. }
  416. /* 10ms should actually be enough. */
  417. endtime = grub_get_time_ms () + 100;
  418. while (grub_get_time_ms () < endtime)
  419. {
  420. for (i = 0; i < nports; i++)
  421. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].status & 7) != 3)
  422. break;
  423. if (i == nports)
  424. break;
  425. }
  426. for (i = 0; i < nports; i++)
  427. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].status & 7) != 3)
  428. {
  429. grub_dprintf ("ahci", "couldn't detect device on port %d\n", i);
  430. failed_adevs[i] = adevs[i];
  431. adevs[i] = 0;
  432. }
  433. for (i = 0; i < nports; i++)
  434. if (adevs[i])
  435. {
  436. grub_dprintf ("ahci", "port %d, err: %x\n", adevs[i]->port,
  437. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  438. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_POWER_ON;
  439. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_SPIN_UP;
  440. grub_dprintf ("ahci", "port %d, err: %x\n", adevs[i]->port,
  441. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  442. adevs[i]->hba->ports[adevs[i]->port].sata_error = ~0;
  443. grub_dprintf ("ahci", "port %d, err: %x\n", adevs[i]->port,
  444. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  445. grub_dprintf ("ahci", "port %d, offset: %x, tfd:%x, CMD: %x\n", adevs[i]->port,
  446. (int) ((char *) &adevs[i]->hba->ports[adevs[i]->port].task_file_data -
  447. (char *) adevs[i]->hba),
  448. adevs[i]->hba->ports[adevs[i]->port].task_file_data,
  449. adevs[i]->hba->ports[adevs[i]->port].command);
  450. grub_dprintf ("ahci", "port %d, err: %x\n", adevs[i]->port,
  451. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  452. }
  453. for (i = 0; i < nports; i++)
  454. if (adevs[i])
  455. {
  456. grub_dprintf ("ahci", "port %d, offset: %x, tfd:%x, CMD: %x\n", adevs[i]->port,
  457. (int) ((char *) &adevs[i]->hba->ports[adevs[i]->port].task_file_data -
  458. (char *) adevs[i]->hba),
  459. adevs[i]->hba->ports[adevs[i]->port].task_file_data,
  460. adevs[i]->hba->ports[adevs[i]->port].command);
  461. grub_dprintf ("ahci", "port: %d, err: %x\n", adevs[i]->port,
  462. adevs[i]->hba->ports[adevs[i]->port].sata_error);
  463. adevs[i]->hba->ports[adevs[i]->port].command
  464. = (adevs[i]->hba->ports[adevs[i]->port].command & 0x0fffffff) | (1 << 28)
  465. | GRUB_AHCI_HBA_PORT_CMD_SPIN_UP
  466. | GRUB_AHCI_HBA_PORT_CMD_POWER_ON;
  467. /* struct grub_disk_ata_pass_through_parms parms2;
  468. grub_memset (&parms2, 0, sizeof (parms2));
  469. parms2.taskfile.cmd = 8;
  470. grub_ahci_readwrite_real (dev, &parms2, 1, 1);*/
  471. }
  472. endtime = grub_get_time_ms () + 10000;
  473. while (grub_get_time_ms () < endtime)
  474. {
  475. for (i = 0; i < nports; i++)
  476. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].task_file_data & (GRUB_ATA_STATUS_BUSY | GRUB_ATA_STATUS_DRQ)))
  477. break;
  478. if (i == nports)
  479. break;
  480. }
  481. for (i = 0; i < nports; i++)
  482. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].task_file_data & (GRUB_ATA_STATUS_BUSY | GRUB_ATA_STATUS_DRQ)))
  483. {
  484. grub_dprintf ("ahci", "port %d is busy\n", i);
  485. failed_adevs[i] = adevs[i];
  486. adevs[i] = 0;
  487. }
  488. for (i = 0; i < nports; i++)
  489. if (adevs[i])
  490. adevs[i]->hba->ports[adevs[i]->port].command |= GRUB_AHCI_HBA_PORT_CMD_ST;
  491. endtime = grub_get_time_ms () + 1000;
  492. while (grub_get_time_ms () < endtime)
  493. {
  494. for (i = 0; i < nports; i++)
  495. if (adevs[i] && !(adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  496. break;
  497. if (i == nports)
  498. break;
  499. }
  500. for (i = 0; i < nports; i++)
  501. if (adevs[i] && !(adevs[i]->hba->ports[adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  502. {
  503. grub_dprintf ("ahci", "couldn't start CR on port %d\n", i);
  504. failed_adevs[i] = adevs[i];
  505. adevs[i] = 0;
  506. }
  507. grub_dprintf ("ahci", "cleaning up failed devs\n");
  508. for (i = 0; i < nports; i++)
  509. if (failed_adevs[i] && (fr_running & (1 << i)))
  510. failed_adevs[i]->hba->ports[failed_adevs[i]->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  511. endtime = grub_get_time_ms () + 1000;
  512. while (grub_get_time_ms () < endtime)
  513. {
  514. for (i = 0; i < nports; i++)
  515. if (failed_adevs[i] && (fr_running & (1 << i)) && (failed_adevs[i]->hba->ports[failed_adevs[i]->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  516. break;
  517. if (i == nports)
  518. break;
  519. }
  520. for (i = 0; i < nports; i++)
  521. if (failed_adevs[i])
  522. {
  523. grub_dma_free (failed_adevs[i]->command_list_chunk);
  524. grub_dma_free (failed_adevs[i]->command_table_chunk);
  525. grub_dma_free (failed_adevs[i]->rfis);
  526. }
  527. for (i = 0; i < nports; i++)
  528. if (adevs[i] && (adevs[i]->hba->ports[adevs[i]->port].sig >> 16) == 0xeb14)
  529. adevs[i]->atapi = 1;
  530. addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  531. grub_pci_write_word (addr, grub_pci_read_word (addr)
  532. | GRUB_PCI_COMMAND_BUS_MASTER);
  533. for (i = 0; i < nports; i++)
  534. if (adevs[i])
  535. {
  536. grub_list_push (GRUB_AS_LIST_P (&grub_ahci_devices),
  537. GRUB_AS_LIST (adevs[i]));
  538. }
  539. return 0;
  540. }
  541. static grub_err_t
  542. grub_ahci_initialize (void)
  543. {
  544. grub_pci_iterate (grub_ahci_pciinit, NULL);
  545. return grub_errno;
  546. }
  547. static grub_err_t
  548. grub_ahci_fini_hw (int noreturn __attribute__ ((unused)))
  549. {
  550. struct grub_ahci_device *dev;
  551. for (dev = grub_ahci_devices; dev; dev = dev->next)
  552. {
  553. grub_uint64_t endtime;
  554. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  555. endtime = grub_get_time_ms () + 1000;
  556. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  557. if (grub_get_time_ms () > endtime)
  558. {
  559. grub_dprintf ("ahci", "couldn't stop FR\n");
  560. break;
  561. }
  562. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_ST;
  563. endtime = grub_get_time_ms () + 1000;
  564. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  565. if (grub_get_time_ms () > endtime)
  566. {
  567. grub_dprintf ("ahci", "couldn't stop CR\n");
  568. break;
  569. }
  570. grub_dma_free (dev->command_list_chunk);
  571. grub_dma_free (dev->command_table_chunk);
  572. grub_dma_free (dev->rfis);
  573. dev->command_list_chunk = NULL;
  574. dev->command_table_chunk = NULL;
  575. dev->rfis = NULL;
  576. }
  577. return GRUB_ERR_NONE;
  578. }
  579. static int
  580. reinit_port (struct grub_ahci_device *dev)
  581. {
  582. struct grub_pci_dma_chunk *command_list;
  583. struct grub_pci_dma_chunk *command_table;
  584. grub_uint64_t endtime;
  585. command_list = grub_memalign_dma32 (1024, sizeof (struct grub_ahci_cmd_head));
  586. if (!command_list)
  587. return 1;
  588. command_table = grub_memalign_dma32 (1024,
  589. sizeof (struct grub_ahci_cmd_table));
  590. if (!command_table)
  591. {
  592. grub_dma_free (command_list);
  593. return 1;
  594. }
  595. grub_dprintf ("ahci", "found device ahci%d (port %d)\n", dev->num, dev->port);
  596. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  597. endtime = grub_get_time_ms () + 1000;
  598. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  599. if (grub_get_time_ms () > endtime)
  600. {
  601. grub_dprintf ("ahci", "couldn't stop FR\n");
  602. goto out;
  603. }
  604. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_ST;
  605. endtime = grub_get_time_ms () + 1000;
  606. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  607. if (grub_get_time_ms () > endtime)
  608. {
  609. grub_dprintf ("ahci", "couldn't stop CR\n");
  610. goto out;
  611. }
  612. dev->hba->ports[dev->port].fbs = 2;
  613. dev->rfis = grub_memalign_dma32 (4096,
  614. sizeof (struct grub_ahci_received_fis));
  615. grub_memset ((char *) grub_dma_get_virt (dev->rfis), 0,
  616. sizeof (struct grub_ahci_received_fis));
  617. dev->hba->ports[dev->port].fis_base = grub_dma_get_phys (dev->rfis);
  618. dev->hba->ports[dev->port].command_list_base
  619. = grub_dma_get_phys (command_list);
  620. dev->hba->ports[dev->port].command |= GRUB_AHCI_HBA_PORT_CMD_FRE;
  621. while (!(dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  622. if (grub_get_time_ms () > endtime)
  623. {
  624. grub_dprintf ("ahci", "couldn't start FR\n");
  625. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  626. goto out;
  627. }
  628. dev->hba->ports[dev->port].command |= GRUB_AHCI_HBA_PORT_CMD_ST;
  629. while (!(dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  630. if (grub_get_time_ms () > endtime)
  631. {
  632. grub_dprintf ("ahci", "couldn't start CR\n");
  633. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_CR;
  634. goto out_stop_fr;
  635. }
  636. dev->hba->ports[dev->port].command
  637. = (dev->hba->ports[dev->port].command & 0x0fffffff) | (1 << 28) | 2 | 4;
  638. dev->command_list_chunk = command_list;
  639. dev->command_list = grub_dma_get_virt (command_list);
  640. dev->command_table_chunk = command_table;
  641. dev->command_table = grub_dma_get_virt (command_table);
  642. dev->command_list->command_table_base
  643. = grub_dma_get_phys (command_table);
  644. return 0;
  645. out_stop_fr:
  646. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  647. endtime = grub_get_time_ms () + 1000;
  648. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_FR))
  649. if (grub_get_time_ms () > endtime)
  650. {
  651. grub_dprintf ("ahci", "couldn't stop FR\n");
  652. break;
  653. }
  654. out:
  655. grub_dma_free (command_list);
  656. grub_dma_free (command_table);
  657. grub_dma_free (dev->rfis);
  658. return 1;
  659. }
  660. static grub_err_t
  661. grub_ahci_restore_hw (void)
  662. {
  663. struct grub_ahci_device **pdev;
  664. for (pdev = &grub_ahci_devices; *pdev; pdev = &((*pdev)->next))
  665. if (reinit_port (*pdev))
  666. {
  667. struct grub_ahci_device *odev;
  668. odev = *pdev;
  669. *pdev = (*pdev)->next;
  670. grub_free (odev);
  671. }
  672. return GRUB_ERR_NONE;
  673. }
  674. static int
  675. grub_ahci_iterate (grub_ata_dev_iterate_hook_t hook, void *hook_data,
  676. grub_disk_pull_t pull)
  677. {
  678. struct grub_ahci_device *dev;
  679. if (pull != GRUB_DISK_PULL_NONE)
  680. return 0;
  681. FOR_LIST_ELEMENTS(dev, grub_ahci_devices)
  682. if (hook (GRUB_SCSI_SUBSYSTEM_AHCI, dev->num, hook_data))
  683. return 1;
  684. return 0;
  685. }
  686. #if 0
  687. static int
  688. find_free_cmd_slot (struct grub_ahci_device *dev)
  689. {
  690. int i;
  691. for (i = 0; i < 32; i++)
  692. {
  693. if (dev->hda->ports[dev->port].command_issue & (1 << i))
  694. continue;
  695. if (dev->hda->ports[dev->port].sata_active & (1 << i))
  696. continue;
  697. return i;
  698. }
  699. return -1;
  700. }
  701. #endif
  702. enum
  703. {
  704. GRUB_AHCI_FIS_REG_H2D = 0x27
  705. };
  706. static const int register_map[11] = { 3 /* Features */,
  707. 12 /* Sectors */,
  708. 4 /* LBA low */,
  709. 5 /* LBA mid */,
  710. 6 /* LBA high */,
  711. 7 /* Device */,
  712. 2 /* CMD register */,
  713. 13 /* Sectors 48 */,
  714. 8 /* LBA48 low */,
  715. 9 /* LBA48 mid */,
  716. 10 /* LBA48 high */ };
  717. static grub_err_t
  718. grub_ahci_reset_port (struct grub_ahci_device *dev, int force)
  719. {
  720. grub_uint64_t endtime;
  721. dev->hba->ports[dev->port].sata_error = dev->hba->ports[dev->port].sata_error;
  722. if (force || (dev->hba->ports[dev->port].command_issue & 1)
  723. || (dev->hba->ports[dev->port].task_file_data & 0x80))
  724. {
  725. struct grub_disk_ata_pass_through_parms parms2;
  726. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_ST;
  727. dev->hba->ports[dev->port].command_issue = 0;
  728. dev->command_list[0].config = 0;
  729. dev->command_table[0].prdt[0].unused = 0;
  730. dev->command_table[0].prdt[0].size = 0;
  731. dev->command_table[0].prdt[0].data_base = 0;
  732. endtime = grub_get_time_ms () + 1000;
  733. while ((dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  734. if (grub_get_time_ms () > endtime)
  735. {
  736. grub_dprintf ("ahci", "couldn't stop CR");
  737. return grub_error (GRUB_ERR_IO, "couldn't stop CR");
  738. }
  739. dev->hba->ports[dev->port].command |= 8;
  740. while (dev->hba->ports[dev->port].command & 8)
  741. if (grub_get_time_ms () > endtime)
  742. {
  743. grub_dprintf ("ahci", "couldn't set CLO\n");
  744. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_FRE;
  745. return grub_error (GRUB_ERR_IO, "couldn't set CLO");
  746. }
  747. dev->hba->ports[dev->port].command |= GRUB_AHCI_HBA_PORT_CMD_ST;
  748. while (!(dev->hba->ports[dev->port].command & GRUB_AHCI_HBA_PORT_CMD_CR))
  749. if (grub_get_time_ms () > endtime)
  750. {
  751. grub_dprintf ("ahci", "couldn't stop CR");
  752. dev->hba->ports[dev->port].command &= ~GRUB_AHCI_HBA_PORT_CMD_ST;
  753. return grub_error (GRUB_ERR_IO, "couldn't stop CR");
  754. }
  755. dev->hba->ports[dev->port].sata_error = dev->hba->ports[dev->port].sata_error;
  756. grub_memset (&parms2, 0, sizeof (parms2));
  757. parms2.taskfile.cmd = 8;
  758. return grub_ahci_readwrite_real (dev, &parms2, 1, 1);
  759. }
  760. return GRUB_ERR_NONE;
  761. }
  762. static grub_err_t
  763. grub_ahci_readwrite_real (struct grub_ahci_device *dev,
  764. struct grub_disk_ata_pass_through_parms *parms,
  765. int spinup, int reset)
  766. {
  767. struct grub_pci_dma_chunk *bufc;
  768. grub_uint64_t endtime;
  769. unsigned i;
  770. grub_err_t err = GRUB_ERR_NONE;
  771. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  772. dev->hba->ports[dev->port].task_file_data);
  773. if (!reset)
  774. grub_ahci_reset_port (dev, 0);
  775. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  776. dev->hba->ports[dev->port].task_file_data);
  777. dev->hba->ports[dev->port].task_file_data = 0;
  778. dev->hba->ports[dev->port].command_issue = 0;
  779. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  780. dev->hba->ports[dev->port].task_file_data);
  781. dev->hba->ports[dev->port].sata_error = dev->hba->ports[dev->port].sata_error;
  782. grub_dprintf("ahci", "grub_ahci_read (size=%llu, cmdsize = %llu)\n",
  783. (unsigned long long) parms->size,
  784. (unsigned long long) parms->cmdsize);
  785. if (parms->cmdsize != 0 && parms->cmdsize != 12 && parms->cmdsize != 16)
  786. return grub_error (GRUB_ERR_BUG, "incorrect ATAPI command size");
  787. if (parms->size > GRUB_AHCI_PRDT_MAX_CHUNK_LENGTH)
  788. return grub_error (GRUB_ERR_BUG, "too big data buffer");
  789. if (parms->size)
  790. bufc = grub_memalign_dma32 (1024, parms->size + (parms->size & 1));
  791. else
  792. bufc = grub_memalign_dma32 (1024, 512);
  793. grub_dprintf ("ahci", "AHCI tfd = %x, CL=%p\n",
  794. dev->hba->ports[dev->port].task_file_data,
  795. dev->command_list);
  796. /* FIXME: support port multipliers. */
  797. dev->command_list[0].config
  798. = (5 << GRUB_AHCI_CONFIG_CFIS_LENGTH_SHIFT)
  799. // | GRUB_AHCI_CONFIG_CLEAR_R_OK
  800. | (0 << GRUB_AHCI_CONFIG_PMP_SHIFT)
  801. | ((parms->size ? 1 : 0) << GRUB_AHCI_CONFIG_PRDT_LENGTH_SHIFT)
  802. | (parms->cmdsize ? GRUB_AHCI_CONFIG_ATAPI : 0)
  803. | (parms->write ? GRUB_AHCI_CONFIG_WRITE : GRUB_AHCI_CONFIG_READ)
  804. | (parms->taskfile.cmd == 8 ? (1 << 8) : 0);
  805. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  806. dev->hba->ports[dev->port].task_file_data);
  807. dev->command_list[0].transferred = 0;
  808. dev->command_list[0].command_table_base
  809. = grub_dma_get_phys (dev->command_table_chunk);
  810. grub_memset ((char *) dev->command_list[0].unused, 0,
  811. sizeof (dev->command_list[0].unused));
  812. grub_memset ((char *) &dev->command_table[0], 0,
  813. sizeof (dev->command_table[0]));
  814. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  815. dev->hba->ports[dev->port].task_file_data);
  816. if (parms->cmdsize)
  817. grub_memcpy ((char *) dev->command_table[0].command, parms->cmd,
  818. parms->cmdsize);
  819. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  820. dev->hba->ports[dev->port].task_file_data);
  821. dev->command_table[0].cfis[0] = GRUB_AHCI_FIS_REG_H2D;
  822. dev->command_table[0].cfis[1] = 0x80;
  823. for (i = 0; i < sizeof (parms->taskfile.raw); i++)
  824. dev->command_table[0].cfis[register_map[i]] = parms->taskfile.raw[i];
  825. grub_dprintf ("ahci", "cfis: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  826. dev->command_table[0].cfis[0], dev->command_table[0].cfis[1],
  827. dev->command_table[0].cfis[2], dev->command_table[0].cfis[3],
  828. dev->command_table[0].cfis[4], dev->command_table[0].cfis[5],
  829. dev->command_table[0].cfis[6], dev->command_table[0].cfis[7]);
  830. grub_dprintf ("ahci", "cfis: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  831. dev->command_table[0].cfis[8], dev->command_table[0].cfis[9],
  832. dev->command_table[0].cfis[10], dev->command_table[0].cfis[11],
  833. dev->command_table[0].cfis[12], dev->command_table[0].cfis[13],
  834. dev->command_table[0].cfis[14], dev->command_table[0].cfis[15]);
  835. dev->command_table[0].prdt[0].data_base = grub_dma_get_phys (bufc);
  836. dev->command_table[0].prdt[0].unused = 0;
  837. dev->command_table[0].prdt[0].size = (parms->size - 1);
  838. grub_dprintf ("ahci", "PRDT = %" PRIxGRUB_UINT64_T ", %x, %x (%"
  839. PRIuGRUB_SIZE ")\n",
  840. dev->command_table[0].prdt[0].data_base,
  841. dev->command_table[0].prdt[0].unused,
  842. dev->command_table[0].prdt[0].size,
  843. (grub_size_t) ((char *) &dev->command_table[0].prdt[0]
  844. - (char *) &dev->command_table[0]));
  845. if (parms->write)
  846. grub_memcpy ((char *) grub_dma_get_virt (bufc), parms->buffer, parms->size);
  847. grub_dprintf ("ahci", "AHCI command scheduled\n");
  848. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  849. dev->hba->ports[dev->port].task_file_data);
  850. grub_dprintf ("ahci", "AHCI inten = %x\n",
  851. dev->hba->ports[dev->port].inten);
  852. grub_dprintf ("ahci", "AHCI intstatus = %x\n",
  853. dev->hba->ports[dev->port].intstatus);
  854. dev->hba->ports[dev->port].inten = 0xffffffff;//(1 << 2) | (1 << 5);
  855. dev->hba->ports[dev->port].intstatus = 0xffffffff;//(1 << 2) | (1 << 5);
  856. grub_dprintf ("ahci", "AHCI inten = %x\n",
  857. dev->hba->ports[dev->port].inten);
  858. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  859. dev->hba->ports[dev->port].task_file_data);
  860. dev->hba->ports[dev->port].sata_active = 1;
  861. dev->hba->ports[dev->port].command_issue = 1;
  862. grub_dprintf ("ahci", "AHCI sig = %x\n", dev->hba->ports[dev->port].sig);
  863. grub_dprintf ("ahci", "AHCI tfd = %x\n",
  864. dev->hba->ports[dev->port].task_file_data);
  865. endtime = grub_get_time_ms () + (spinup ? 20000 : 20000);
  866. while ((dev->hba->ports[dev->port].command_issue & 1))
  867. if (grub_get_time_ms () > endtime)
  868. {
  869. grub_dprintf ("ahci", "AHCI status <%x %x %x %x>\n",
  870. dev->hba->ports[dev->port].command_issue,
  871. dev->hba->ports[dev->port].sata_active,
  872. dev->hba->ports[dev->port].intstatus,
  873. dev->hba->ports[dev->port].task_file_data);
  874. dev->hba->ports[dev->port].command_issue = 0;
  875. err = grub_error (GRUB_ERR_IO, "AHCI transfer timed out");
  876. if (!reset)
  877. grub_ahci_reset_port (dev, 1);
  878. break;
  879. }
  880. grub_dprintf ("ahci", "AHCI command completed <%x %x %x %x %x, %x %x>\n",
  881. dev->hba->ports[dev->port].command_issue,
  882. dev->hba->ports[dev->port].intstatus,
  883. dev->hba->ports[dev->port].task_file_data,
  884. dev->command_list[0].transferred,
  885. dev->hba->ports[dev->port].sata_error,
  886. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x00],
  887. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x18]);
  888. grub_dprintf ("ahci",
  889. "last PIO FIS %08x %08x %08x %08x %08x %08x %08x %08x\n",
  890. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x08],
  891. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x09],
  892. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0a],
  893. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0b],
  894. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0c],
  895. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0d],
  896. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0e],
  897. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x0f]);
  898. grub_dprintf ("ahci",
  899. "last REG FIS %08x %08x %08x %08x %08x %08x %08x %08x\n",
  900. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x10],
  901. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x11],
  902. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x12],
  903. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x13],
  904. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x14],
  905. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x15],
  906. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x16],
  907. ((grub_uint32_t *) grub_dma_get_virt (dev->rfis))[0x17]);
  908. if (!parms->write)
  909. grub_memcpy (parms->buffer, (char *) grub_dma_get_virt (bufc), parms->size);
  910. grub_dma_free (bufc);
  911. return err;
  912. }
  913. static grub_err_t
  914. grub_ahci_readwrite (grub_ata_t disk,
  915. struct grub_disk_ata_pass_through_parms *parms,
  916. int spinup)
  917. {
  918. return grub_ahci_readwrite_real (disk->data, parms, spinup, 0);
  919. }
  920. static grub_err_t
  921. grub_ahci_open (int id, int devnum, struct grub_ata *ata)
  922. {
  923. struct grub_ahci_device *dev;
  924. if (id != GRUB_SCSI_SUBSYSTEM_AHCI)
  925. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not an AHCI device");
  926. FOR_LIST_ELEMENTS(dev, grub_ahci_devices)
  927. if (dev->num == devnum)
  928. break;
  929. if (! dev)
  930. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such AHCI device");
  931. grub_dprintf ("ahci", "opening AHCI dev `ahci%d'\n", dev->num);
  932. ata->data = dev;
  933. ata->dma = 1;
  934. ata->atapi = dev->atapi;
  935. ata->maxbuffer = GRUB_AHCI_PRDT_MAX_CHUNK_LENGTH;
  936. ata->present = &dev->present;
  937. return GRUB_ERR_NONE;
  938. }
  939. static struct grub_ata_dev grub_ahci_dev =
  940. {
  941. .iterate = grub_ahci_iterate,
  942. .open = grub_ahci_open,
  943. .readwrite = grub_ahci_readwrite,
  944. };
  945. static struct grub_preboot *fini_hnd;
  946. GRUB_MOD_INIT(ahci)
  947. {
  948. grub_stop_disk_firmware ();
  949. /* AHCI initialization. */
  950. grub_ahci_initialize ();
  951. /* AHCI devices are handled by scsi.mod. */
  952. grub_ata_dev_register (&grub_ahci_dev);
  953. fini_hnd = grub_loader_register_preboot_hook (grub_ahci_fini_hw,
  954. grub_ahci_restore_hw,
  955. GRUB_LOADER_PREBOOT_HOOK_PRIO_DISK);
  956. }
  957. GRUB_MOD_FINI(ahci)
  958. {
  959. grub_ahci_fini_hw (0);
  960. grub_loader_unregister_preboot_hook (fini_hnd);
  961. grub_ata_dev_unregister (&grub_ahci_dev);
  962. }