ahci.c 35 KB

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