pata.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* ata_pthru.c - ATA pass through for ata.mod. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/ata.h>
  20. #include <grub/scsi.h>
  21. #include <grub/disk.h>
  22. #include <grub/dl.h>
  23. #include <grub/mm.h>
  24. #ifndef GRUB_MACHINE_MIPS_QEMU_MIPS
  25. #include <grub/pci.h>
  26. #include <grub/cs5536.h>
  27. #else
  28. #define GRUB_MACHINE_PCI_IO_BASE 0xb4000000
  29. #endif
  30. #include <grub/time.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. /* At the moment, only two IDE ports are supported. */
  33. static const grub_port_t grub_pata_ioaddress[] = { GRUB_ATA_CH0_PORT1,
  34. GRUB_ATA_CH1_PORT1 };
  35. struct grub_pata_device
  36. {
  37. /* IDE port to use. */
  38. int port;
  39. /* IO addresses on which the registers for this device can be
  40. found. */
  41. grub_port_t ioaddress;
  42. /* Two devices can be connected to a single cable. Use this field
  43. to select device 0 (commonly known as "master") or device 1
  44. (commonly known as "slave"). */
  45. int device;
  46. int present;
  47. struct grub_pata_device *next;
  48. };
  49. static struct grub_pata_device *grub_pata_devices;
  50. static inline void
  51. grub_pata_regset (struct grub_pata_device *dev, int reg, int val)
  52. {
  53. grub_outb (val, dev->ioaddress + reg);
  54. }
  55. static inline grub_uint8_t
  56. grub_pata_regget (struct grub_pata_device *dev, int reg)
  57. {
  58. return grub_inb (dev->ioaddress + reg);
  59. }
  60. /* Wait for !BSY. */
  61. static grub_err_t
  62. grub_pata_wait_not_busy (struct grub_pata_device *dev, int milliseconds)
  63. {
  64. /* ATA requires 400ns (after a write to CMD register) or
  65. 1 PIO cycle (after a DRQ block transfer) before
  66. first check of BSY. */
  67. grub_millisleep (1);
  68. int i = 1;
  69. grub_uint8_t sts;
  70. while ((sts = grub_pata_regget (dev, GRUB_ATA_REG_STATUS))
  71. & GRUB_ATA_STATUS_BUSY)
  72. {
  73. if (i >= milliseconds)
  74. {
  75. grub_dprintf ("pata", "timeout: %dms, status=0x%x\n",
  76. milliseconds, sts);
  77. return grub_error (GRUB_ERR_TIMEOUT, "PATA timeout");
  78. }
  79. grub_millisleep (1);
  80. i++;
  81. }
  82. return GRUB_ERR_NONE;
  83. }
  84. static inline grub_err_t
  85. grub_pata_check_ready (struct grub_pata_device *dev, int spinup)
  86. {
  87. if (grub_pata_regget (dev, GRUB_ATA_REG_STATUS) & GRUB_ATA_STATUS_BUSY)
  88. return grub_pata_wait_not_busy (dev, spinup ? GRUB_ATA_TOUT_SPINUP
  89. : GRUB_ATA_TOUT_STD);
  90. return GRUB_ERR_NONE;
  91. }
  92. static inline void
  93. grub_pata_wait (void)
  94. {
  95. grub_millisleep (50);
  96. }
  97. #ifdef GRUB_MACHINE_MIPS_QEMU_MIPS
  98. #define grub_ata_to_cpu16(x) ((grub_uint16_t) (x))
  99. #define grub_cpu_to_ata16(x) ((grub_uint16_t) (x))
  100. #else
  101. #define grub_ata_to_cpu16 grub_le_to_cpu16
  102. #define grub_cpu_to_ata16 grub_cpu_to_le16
  103. #endif
  104. static void
  105. grub_pata_pio_read (struct grub_pata_device *dev, char *buf, grub_size_t size)
  106. {
  107. unsigned int i;
  108. /* Read in the data, word by word. */
  109. for (i = 0; i < size / 2; i++)
  110. grub_set_unaligned16 (buf + 2 * i,
  111. grub_ata_to_cpu16 (grub_inw(dev->ioaddress
  112. + GRUB_ATA_REG_DATA)));
  113. if (size & 1)
  114. buf[size - 1] = (char) grub_ata_to_cpu16 (grub_inw (dev->ioaddress
  115. + GRUB_ATA_REG_DATA));
  116. }
  117. static void
  118. grub_pata_pio_write (struct grub_pata_device *dev, char *buf, grub_size_t size)
  119. {
  120. unsigned int i;
  121. /* Write the data, word by word. */
  122. for (i = 0; i < size / 2; i++)
  123. grub_outw(grub_cpu_to_ata16 (grub_get_unaligned16 (buf + 2 * i)), dev->ioaddress + GRUB_ATA_REG_DATA);
  124. }
  125. /* ATA pass through support, used by hdparm.mod. */
  126. static grub_err_t
  127. grub_pata_readwrite (struct grub_ata *disk,
  128. struct grub_disk_ata_pass_through_parms *parms,
  129. int spinup)
  130. {
  131. struct grub_pata_device *dev = (struct grub_pata_device *) disk->data;
  132. grub_size_t nread = 0;
  133. int i;
  134. if (! (parms->cmdsize == 0 || parms->cmdsize == 12))
  135. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  136. "ATAPI non-12 byte commands not supported");
  137. grub_dprintf ("pata", "pata_pass_through: cmd=0x%x, features=0x%x, sectors=0x%x\n",
  138. parms->taskfile.cmd,
  139. parms->taskfile.features,
  140. parms->taskfile.sectors);
  141. grub_dprintf ("pata", "lba_high=0x%x, lba_mid=0x%x, lba_low=0x%x, size=%"
  142. PRIuGRUB_SIZE "\n",
  143. parms->taskfile.lba_high,
  144. parms->taskfile.lba_mid,
  145. parms->taskfile.lba_low, parms->size);
  146. /* Set registers. */
  147. grub_pata_regset (dev, GRUB_ATA_REG_DISK, (dev->device << 4)
  148. | (parms->taskfile.disk & 0xef));
  149. if (grub_pata_check_ready (dev, spinup))
  150. return grub_errno;
  151. for (i = GRUB_ATA_REG_SECTORS; i <= GRUB_ATA_REG_LBAHIGH; i++)
  152. grub_pata_regset (dev, i,
  153. parms->taskfile.raw[7 + (i - GRUB_ATA_REG_SECTORS)]);
  154. for (i = GRUB_ATA_REG_FEATURES; i <= GRUB_ATA_REG_LBAHIGH; i++)
  155. grub_pata_regset (dev, i, parms->taskfile.raw[i - GRUB_ATA_REG_FEATURES]);
  156. /* Start command. */
  157. grub_pata_regset (dev, GRUB_ATA_REG_CMD, parms->taskfile.cmd);
  158. /* Wait for !BSY. */
  159. if (grub_pata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  160. return grub_errno;
  161. /* Check status. */
  162. grub_int8_t sts = grub_pata_regget (dev, GRUB_ATA_REG_STATUS);
  163. grub_dprintf ("pata", "status=0x%x\n", sts);
  164. if (parms->cmdsize)
  165. {
  166. grub_uint8_t irs;
  167. /* Wait for !BSY. */
  168. if (grub_pata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  169. return grub_errno;
  170. irs = grub_pata_regget (dev, GRUB_ATAPI_REG_IREASON);
  171. /* OK if DRQ is asserted and interrupt reason is as expected. */
  172. if (!((sts & GRUB_ATA_STATUS_DRQ)
  173. && (irs & GRUB_ATAPI_IREASON_MASK) == GRUB_ATAPI_IREASON_CMD_OUT))
  174. return grub_error (GRUB_ERR_READ_ERROR, "ATAPI protocol error");
  175. /* Write the packet. */
  176. grub_pata_pio_write (dev, parms->cmd, parms->cmdsize);
  177. }
  178. /* Transfer data. */
  179. while (nread < parms->size
  180. && (sts & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  181. == GRUB_ATA_STATUS_DRQ)
  182. {
  183. unsigned cnt;
  184. /* Wait for !BSY. */
  185. if (grub_pata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  186. return grub_errno;
  187. if (parms->cmdsize)
  188. {
  189. if ((grub_pata_regget (dev, GRUB_ATAPI_REG_IREASON)
  190. & GRUB_ATAPI_IREASON_MASK) != GRUB_ATAPI_IREASON_DATA_IN)
  191. return grub_error (GRUB_ERR_READ_ERROR, "ATAPI protocol error");
  192. cnt = grub_pata_regget (dev, GRUB_ATAPI_REG_CNTHIGH) << 8
  193. | grub_pata_regget (dev, GRUB_ATAPI_REG_CNTLOW);
  194. grub_dprintf("pata", "DRQ count=%u\n", cnt);
  195. /* Count of last transfer may be uneven. */
  196. if (! (0 < cnt && cnt <= parms->size - nread
  197. && (! (cnt & 1) || cnt == parms->size - nread)))
  198. return grub_error (GRUB_ERR_READ_ERROR,
  199. "invalid ATAPI transfer count");
  200. }
  201. else
  202. cnt = GRUB_DISK_SECTOR_SIZE;
  203. if (cnt > parms->size - nread)
  204. cnt = parms->size - nread;
  205. if (parms->write)
  206. grub_pata_pio_write (dev, (char *) parms->buffer + nread, cnt);
  207. else
  208. grub_pata_pio_read (dev, (char *) parms->buffer + nread, cnt);
  209. nread += cnt;
  210. }
  211. if (parms->write)
  212. {
  213. /* Check for write error. */
  214. if (grub_pata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  215. return grub_errno;
  216. if (grub_pata_regget (dev, GRUB_ATA_REG_STATUS)
  217. & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  218. return grub_error (GRUB_ERR_WRITE_ERROR, "ATA write error");
  219. }
  220. parms->size = nread;
  221. /* Wait for !BSY. */
  222. if (grub_pata_wait_not_busy (dev, GRUB_ATA_TOUT_DATA))
  223. return grub_errno;
  224. /* Return registers. */
  225. for (i = GRUB_ATA_REG_ERROR; i <= GRUB_ATA_REG_STATUS; i++)
  226. parms->taskfile.raw[i - GRUB_ATA_REG_FEATURES] = grub_pata_regget (dev, i);
  227. grub_dprintf ("pata", "status=0x%x, error=0x%x, sectors=0x%x\n",
  228. parms->taskfile.status,
  229. parms->taskfile.error,
  230. parms->taskfile.sectors);
  231. if (parms->taskfile.status
  232. & (GRUB_ATA_STATUS_DRQ | GRUB_ATA_STATUS_ERR))
  233. return grub_error (GRUB_ERR_READ_ERROR, "PATA passthrough failed");
  234. return GRUB_ERR_NONE;
  235. }
  236. static grub_err_t
  237. check_device (struct grub_pata_device *dev)
  238. {
  239. grub_pata_regset (dev, GRUB_ATA_REG_DISK, dev->device << 4);
  240. grub_pata_wait ();
  241. /* Try to detect if the port is in use by writing to it,
  242. waiting for a while and reading it again. If the value
  243. was preserved, there is a device connected. */
  244. grub_pata_regset (dev, GRUB_ATA_REG_SECTORS, 0x5A);
  245. grub_pata_wait ();
  246. grub_uint8_t sec = grub_pata_regget (dev, GRUB_ATA_REG_SECTORS);
  247. grub_dprintf ("ata", "sectors=0x%x\n", sec);
  248. if (sec != 0x5A)
  249. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no device connected");
  250. /* The above test may detect a second (slave) device
  251. connected to a SATA controller which supports only one
  252. (master) device. It is not safe to use the status register
  253. READY bit to check for controller channel existence. Some
  254. ATAPI commands (RESET, DIAGNOSTIC) may clear this bit. */
  255. return GRUB_ERR_NONE;
  256. }
  257. static grub_err_t
  258. grub_pata_device_initialize (int port, int device, int addr)
  259. {
  260. struct grub_pata_device *dev;
  261. struct grub_pata_device **devp;
  262. grub_err_t err;
  263. grub_dprintf ("pata", "detecting device %d,%d (0x%x)\n",
  264. port, device, addr);
  265. dev = grub_malloc (sizeof(*dev));
  266. if (! dev)
  267. return grub_errno;
  268. /* Setup the device information. */
  269. dev->port = port;
  270. dev->device = device;
  271. dev->ioaddress = addr + GRUB_MACHINE_PCI_IO_BASE;
  272. dev->present = 1;
  273. dev->next = NULL;
  274. /* Register the device. */
  275. for (devp = &grub_pata_devices; *devp; devp = &(*devp)->next);
  276. *devp = dev;
  277. err = check_device (dev);
  278. if (err == GRUB_ERR_UNKNOWN_DEVICE)
  279. {
  280. grub_dprintf ("pata", "%s\n", grub_errmsg);
  281. grub_error_pop ();
  282. }
  283. if (err)
  284. grub_print_error ();
  285. return 0;
  286. }
  287. #ifndef GRUB_MACHINE_MIPS_QEMU_MIPS
  288. static int
  289. grub_pata_pciinit (grub_pci_device_t dev,
  290. grub_pci_id_t pciid,
  291. void *data __attribute__ ((unused)))
  292. {
  293. static int compat_use[2] = { 0 };
  294. grub_pci_address_t addr;
  295. grub_uint32_t class;
  296. grub_uint32_t bar1;
  297. grub_uint32_t bar2;
  298. int rega;
  299. int i;
  300. static int controller = 0;
  301. int cs5536 = 0;
  302. int nports = 2;
  303. /* Read class. */
  304. addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  305. class = grub_pci_read (addr);
  306. /* AMD CS5536 Southbridge. */
  307. if (pciid == GRUB_CS5536_PCIID)
  308. {
  309. cs5536 = 1;
  310. nports = 1;
  311. }
  312. /* Check if this class ID matches that of a PCI IDE Controller. */
  313. if (!cs5536 && (class >> 16 != 0x0101))
  314. return 0;
  315. for (i = 0; i < nports; i++)
  316. {
  317. /* Set to 0 when the channel operated in compatibility mode. */
  318. int compat;
  319. /* We don't support non-compatibility mode for CS5536. */
  320. if (cs5536)
  321. compat = 0;
  322. else
  323. compat = (class >> (8 + 2 * i)) & 1;
  324. rega = 0;
  325. /* If the channel is in compatibility mode, just assign the
  326. default registers. */
  327. if (compat == 0 && !compat_use[i])
  328. {
  329. rega = grub_pata_ioaddress[i];
  330. compat_use[i] = 1;
  331. }
  332. else if (compat)
  333. {
  334. /* Read the BARs, which either contain a mmapped IO address
  335. or the IO port address. */
  336. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESSES
  337. + sizeof (grub_uint64_t) * i);
  338. bar1 = grub_pci_read (addr);
  339. addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESSES
  340. + sizeof (grub_uint64_t) * i
  341. + sizeof (grub_uint32_t));
  342. bar2 = grub_pci_read (addr);
  343. /* Check if the BARs describe an IO region. */
  344. if ((bar1 & 1) && (bar2 & 1) && (bar1 & ~3))
  345. {
  346. rega = bar1 & ~3;
  347. addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  348. grub_pci_write_word (addr, grub_pci_read_word (addr)
  349. | GRUB_PCI_COMMAND_IO_ENABLED
  350. | GRUB_PCI_COMMAND_MEM_ENABLED
  351. | GRUB_PCI_COMMAND_BUS_MASTER);
  352. }
  353. }
  354. grub_dprintf ("pata",
  355. "PCI dev (%d,%d,%d) compat=%d rega=0x%x\n",
  356. grub_pci_get_bus (dev), grub_pci_get_device (dev),
  357. grub_pci_get_function (dev), compat, rega);
  358. if (rega)
  359. {
  360. grub_errno = GRUB_ERR_NONE;
  361. grub_pata_device_initialize (controller * 2 + i, 0, rega);
  362. /* Most errors raised by grub_ata_device_initialize() are harmless.
  363. They just indicate this particular drive is not responding, most
  364. likely because it doesn't exist. We might want to ignore specific
  365. error types here, instead of printing them. */
  366. if (grub_errno)
  367. {
  368. grub_print_error ();
  369. grub_errno = GRUB_ERR_NONE;
  370. }
  371. grub_pata_device_initialize (controller * 2 + i, 1, rega);
  372. /* Likewise. */
  373. if (grub_errno)
  374. {
  375. grub_print_error ();
  376. grub_errno = GRUB_ERR_NONE;
  377. }
  378. }
  379. }
  380. controller++;
  381. return 0;
  382. }
  383. static grub_err_t
  384. grub_pata_initialize (void)
  385. {
  386. grub_pci_iterate (grub_pata_pciinit, NULL);
  387. return 0;
  388. }
  389. #else
  390. static grub_err_t
  391. grub_pata_initialize (void)
  392. {
  393. int i;
  394. for (i = 0; i < 2; i++)
  395. {
  396. grub_pata_device_initialize (i, 0, grub_pata_ioaddress[i]);
  397. grub_pata_device_initialize (i, 1, grub_pata_ioaddress[i]);
  398. }
  399. return 0;
  400. }
  401. #endif
  402. static grub_err_t
  403. grub_pata_open (int id, int devnum, struct grub_ata *ata)
  404. {
  405. struct grub_pata_device *dev;
  406. struct grub_pata_device *devfnd = 0;
  407. grub_err_t err;
  408. if (id != GRUB_SCSI_SUBSYSTEM_PATA)
  409. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a PATA device");
  410. for (dev = grub_pata_devices; dev; dev = dev->next)
  411. {
  412. if (dev->port * 2 + dev->device == devnum)
  413. {
  414. devfnd = dev;
  415. break;
  416. }
  417. }
  418. grub_dprintf ("pata", "opening PATA dev `ata%d'\n", devnum);
  419. if (! devfnd)
  420. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such PATA device");
  421. err = check_device (devfnd);
  422. if (err)
  423. return err;
  424. ata->data = devfnd;
  425. ata->dma = 0;
  426. ata->maxbuffer = 256 * 512;
  427. ata->present = &devfnd->present;
  428. return GRUB_ERR_NONE;
  429. }
  430. static int
  431. grub_pata_iterate (grub_ata_dev_iterate_hook_t hook, void *hook_data,
  432. grub_disk_pull_t pull)
  433. {
  434. struct grub_pata_device *dev;
  435. if (pull != GRUB_DISK_PULL_NONE)
  436. return 0;
  437. for (dev = grub_pata_devices; dev; dev = dev->next)
  438. if (hook (GRUB_SCSI_SUBSYSTEM_PATA, dev->port * 2 + dev->device,
  439. hook_data))
  440. return 1;
  441. return 0;
  442. }
  443. static struct grub_ata_dev grub_pata_dev =
  444. {
  445. .iterate = grub_pata_iterate,
  446. .open = grub_pata_open,
  447. .readwrite = grub_pata_readwrite,
  448. };
  449. GRUB_MOD_INIT(ata_pthru)
  450. {
  451. grub_stop_disk_firmware ();
  452. /* ATA initialization. */
  453. grub_pata_initialize ();
  454. grub_ata_dev_register (&grub_pata_dev);
  455. }
  456. GRUB_MOD_FINI(ata_pthru)
  457. {
  458. grub_ata_dev_unregister (&grub_pata_dev);
  459. }