ata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * ATA Disk Driver
  7. *
  8. * Provides raw block access to an (Parallel) ATA drive.
  9. */
  10. #include <kernel/system.h>
  11. #include <kernel/logging.h>
  12. #include <kernel/module.h>
  13. #include <kernel/fs.h>
  14. #include <kernel/printf.h>
  15. #include <kernel/pci.h>
  16. /* TODO: Move this to mod/ata.h */
  17. #include <kernel/ata.h>
  18. #include <toaru/list.h>
  19. static char ata_drive_char = 'a';
  20. static int cdrom_number = 0;
  21. static uint32_t ata_pci = 0x00000000;
  22. static list_t * atapi_waiter;
  23. static int atapi_in_progress = 0;
  24. typedef union {
  25. uint8_t command_bytes[12];
  26. uint16_t command_words[6];
  27. } atapi_command_t;
  28. /* 8086:7010 */
  29. static void find_ata_pci(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  30. if ((vendorid == 0x8086) && (deviceid == 0x7010 || deviceid == 0x7111)) {
  31. *((uint32_t *)extra) = device;
  32. }
  33. }
  34. typedef struct {
  35. uintptr_t offset;
  36. uint16_t bytes;
  37. uint16_t last;
  38. } prdt_t;
  39. struct ata_device {
  40. int io_base;
  41. int control;
  42. int slave;
  43. int is_atapi;
  44. ata_identify_t identity;
  45. prdt_t * dma_prdt;
  46. uintptr_t dma_prdt_phys;
  47. uint8_t * dma_start;
  48. uintptr_t dma_start_phys;
  49. uint32_t bar4;
  50. uint32_t atapi_lba;
  51. uint32_t atapi_sector_size;
  52. };
  53. static struct ata_device ata_primary_master = {.io_base = 0x1F0, .control = 0x3F6, .slave = 0};
  54. static struct ata_device ata_primary_slave = {.io_base = 0x1F0, .control = 0x3F6, .slave = 1};
  55. static struct ata_device ata_secondary_master = {.io_base = 0x170, .control = 0x376, .slave = 0};
  56. static struct ata_device ata_secondary_slave = {.io_base = 0x170, .control = 0x376, .slave = 1};
  57. //static volatile uint8_t ata_lock = 0;
  58. static spin_lock_t ata_lock = { 0 };
  59. /* TODO support other sector sizes */
  60. #define ATA_SECTOR_SIZE 512
  61. static void ata_device_read_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf);
  62. static void ata_device_read_sector_atapi(struct ata_device * dev, uint32_t lba, uint8_t * buf);
  63. static void ata_device_write_sector_retry(struct ata_device * dev, uint32_t lba, uint8_t * buf);
  64. static uint32_t read_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  65. static uint32_t write_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  66. static void open_ata(fs_node_t *node, unsigned int flags);
  67. static void close_ata(fs_node_t *node);
  68. static uint64_t ata_max_offset(struct ata_device * dev) {
  69. uint64_t sectors = dev->identity.sectors_48;
  70. if (!sectors) {
  71. /* Fall back to sectors_28 */
  72. sectors = dev->identity.sectors_28;
  73. }
  74. return sectors * ATA_SECTOR_SIZE;
  75. }
  76. static uint64_t atapi_max_offset(struct ata_device * dev) {
  77. uint64_t max_sector = dev->atapi_lba;
  78. if (!max_sector) return 0;
  79. return (max_sector + 1) * dev->atapi_sector_size;
  80. }
  81. static uint32_t read_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  82. struct ata_device * dev = (struct ata_device *)node->device;
  83. unsigned int start_block = offset / ATA_SECTOR_SIZE;
  84. unsigned int end_block = (offset + size - 1) / ATA_SECTOR_SIZE;
  85. unsigned int x_offset = 0;
  86. if (offset > ata_max_offset(dev)) {
  87. return 0;
  88. }
  89. if (offset + size > ata_max_offset(dev)) {
  90. unsigned int i = ata_max_offset(dev) - offset;
  91. size = i;
  92. }
  93. if (offset % ATA_SECTOR_SIZE) {
  94. unsigned int prefix_size = (ATA_SECTOR_SIZE - (offset % ATA_SECTOR_SIZE));
  95. char * tmp = malloc(ATA_SECTOR_SIZE);
  96. ata_device_read_sector(dev, start_block, (uint8_t *)tmp);
  97. memcpy(buffer, (void *)((uintptr_t)tmp + (offset % ATA_SECTOR_SIZE)), prefix_size);
  98. free(tmp);
  99. x_offset += prefix_size;
  100. start_block++;
  101. }
  102. if ((offset + size) % ATA_SECTOR_SIZE && start_block <= end_block) {
  103. unsigned int postfix_size = (offset + size) % ATA_SECTOR_SIZE;
  104. char * tmp = malloc(ATA_SECTOR_SIZE);
  105. ata_device_read_sector(dev, end_block, (uint8_t *)tmp);
  106. memcpy((void *)((uintptr_t)buffer + size - postfix_size), tmp, postfix_size);
  107. free(tmp);
  108. end_block--;
  109. }
  110. while (start_block <= end_block) {
  111. ata_device_read_sector(dev, start_block, (uint8_t *)((uintptr_t)buffer + x_offset));
  112. x_offset += ATA_SECTOR_SIZE;
  113. start_block++;
  114. }
  115. return size;
  116. }
  117. static uint32_t read_atapi(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  118. struct ata_device * dev = (struct ata_device *)node->device;
  119. unsigned int start_block = offset / dev->atapi_sector_size;
  120. unsigned int end_block = (offset + size - 1) / dev->atapi_sector_size;
  121. unsigned int x_offset = 0;
  122. if (offset > atapi_max_offset(dev)) {
  123. return 0;
  124. }
  125. if (offset + size > atapi_max_offset(dev)) {
  126. unsigned int i = atapi_max_offset(dev) - offset;
  127. size = i;
  128. }
  129. if (offset % dev->atapi_sector_size) {
  130. unsigned int prefix_size = (dev->atapi_sector_size - (offset % dev->atapi_sector_size));
  131. char * tmp = malloc(dev->atapi_sector_size);
  132. ata_device_read_sector_atapi(dev, start_block, (uint8_t *)tmp);
  133. memcpy(buffer, (void *)((uintptr_t)tmp + (offset % dev->atapi_sector_size)), prefix_size);
  134. free(tmp);
  135. x_offset += prefix_size;
  136. start_block++;
  137. }
  138. if ((offset + size) % dev->atapi_sector_size && start_block <= end_block) {
  139. unsigned int postfix_size = (offset + size) % dev->atapi_sector_size;
  140. char * tmp = malloc(dev->atapi_sector_size);
  141. ata_device_read_sector_atapi(dev, end_block, (uint8_t *)tmp);
  142. memcpy((void *)((uintptr_t)buffer + size - postfix_size), tmp, postfix_size);
  143. free(tmp);
  144. end_block--;
  145. }
  146. while (start_block <= end_block) {
  147. ata_device_read_sector_atapi(dev, start_block, (uint8_t *)((uintptr_t)buffer + x_offset));
  148. x_offset += dev->atapi_sector_size;
  149. start_block++;
  150. }
  151. return size;
  152. }
  153. static uint32_t write_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  154. struct ata_device * dev = (struct ata_device *)node->device;
  155. unsigned int start_block = offset / ATA_SECTOR_SIZE;
  156. unsigned int end_block = (offset + size - 1) / ATA_SECTOR_SIZE;
  157. unsigned int x_offset = 0;
  158. if (offset > ata_max_offset(dev)) {
  159. return 0;
  160. }
  161. if (offset + size > ata_max_offset(dev)) {
  162. unsigned int i = ata_max_offset(dev) - offset;
  163. size = i;
  164. }
  165. if (offset % ATA_SECTOR_SIZE) {
  166. unsigned int prefix_size = (ATA_SECTOR_SIZE - (offset % ATA_SECTOR_SIZE));
  167. char * tmp = malloc(ATA_SECTOR_SIZE);
  168. ata_device_read_sector(dev, start_block, (uint8_t *)tmp);
  169. debug_print(NOTICE, "Writing first block");
  170. memcpy((void *)((uintptr_t)tmp + (offset % ATA_SECTOR_SIZE)), buffer, prefix_size);
  171. ata_device_write_sector_retry(dev, start_block, (uint8_t *)tmp);
  172. free(tmp);
  173. x_offset += prefix_size;
  174. start_block++;
  175. }
  176. if ((offset + size) % ATA_SECTOR_SIZE && start_block <= end_block) {
  177. unsigned int postfix_size = (offset + size) % ATA_SECTOR_SIZE;
  178. char * tmp = malloc(ATA_SECTOR_SIZE);
  179. ata_device_read_sector(dev, end_block, (uint8_t *)tmp);
  180. debug_print(NOTICE, "Writing last block");
  181. memcpy(tmp, (void *)((uintptr_t)buffer + size - postfix_size), postfix_size);
  182. ata_device_write_sector_retry(dev, end_block, (uint8_t *)tmp);
  183. free(tmp);
  184. end_block--;
  185. }
  186. while (start_block <= end_block) {
  187. ata_device_write_sector_retry(dev, start_block, (uint8_t *)((uintptr_t)buffer + x_offset));
  188. x_offset += ATA_SECTOR_SIZE;
  189. start_block++;
  190. }
  191. return size;
  192. }
  193. static void open_ata(fs_node_t * node, unsigned int flags) {
  194. return;
  195. }
  196. static void close_ata(fs_node_t * node) {
  197. return;
  198. }
  199. static fs_node_t * atapi_device_create(struct ata_device * device) {
  200. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  201. memset(fnode, 0x00, sizeof(fs_node_t));
  202. fnode->inode = 0;
  203. sprintf(fnode->name, "cdrom%d", cdrom_number);
  204. fnode->device = device;
  205. fnode->uid = 0;
  206. fnode->gid = 0;
  207. fnode->mask = 0660;
  208. fnode->length = atapi_max_offset(device);
  209. fnode->flags = FS_BLOCKDEVICE;
  210. fnode->read = read_atapi;
  211. fnode->write = NULL; /* no write support */
  212. fnode->open = open_ata;
  213. fnode->close = close_ata;
  214. fnode->readdir = NULL;
  215. fnode->finddir = NULL;
  216. fnode->ioctl = NULL; /* TODO, identify, etc? */
  217. return fnode;
  218. }
  219. static fs_node_t * ata_device_create(struct ata_device * device) {
  220. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  221. memset(fnode, 0x00, sizeof(fs_node_t));
  222. fnode->inode = 0;
  223. sprintf(fnode->name, "atadev%d", ata_drive_char - 'a');
  224. fnode->device = device;
  225. fnode->uid = 0;
  226. fnode->gid = 0;
  227. fnode->mask = 0660;
  228. fnode->length = ata_max_offset(device); /* TODO */
  229. fnode->flags = FS_BLOCKDEVICE;
  230. fnode->read = read_ata;
  231. fnode->write = write_ata;
  232. fnode->open = open_ata;
  233. fnode->close = close_ata;
  234. fnode->readdir = NULL;
  235. fnode->finddir = NULL;
  236. fnode->ioctl = NULL; /* TODO, identify, etc? */
  237. return fnode;
  238. }
  239. static void ata_io_wait(struct ata_device * dev) {
  240. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  241. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  242. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  243. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  244. }
  245. static int ata_status_wait(struct ata_device * dev, int timeout) {
  246. int status;
  247. if (timeout > 0) {
  248. int i = 0;
  249. while ((status = inportb(dev->io_base + ATA_REG_STATUS)) & ATA_SR_BSY && (i < timeout)) i++;
  250. } else {
  251. while ((status = inportb(dev->io_base + ATA_REG_STATUS)) & ATA_SR_BSY);
  252. }
  253. return status;
  254. }
  255. static int ata_wait(struct ata_device * dev, int advanced) {
  256. uint8_t status = 0;
  257. ata_io_wait(dev);
  258. status = ata_status_wait(dev, -1);
  259. if (advanced) {
  260. status = inportb(dev->io_base + ATA_REG_STATUS);
  261. if (status & ATA_SR_ERR) return 1;
  262. if (status & ATA_SR_DF) return 1;
  263. if (!(status & ATA_SR_DRQ)) return 1;
  264. }
  265. return 0;
  266. }
  267. static void ata_soft_reset(struct ata_device * dev) {
  268. outportb(dev->control, 0x04);
  269. ata_io_wait(dev);
  270. outportb(dev->control, 0x00);
  271. }
  272. static int ata_irq_handler(struct regs *r) {
  273. inportb(ata_primary_master.io_base + ATA_REG_STATUS);
  274. if (atapi_in_progress) {
  275. wakeup_queue(atapi_waiter);
  276. }
  277. irq_ack(14);
  278. return 1;
  279. }
  280. static int ata_irq_handler_s(struct regs *r) {
  281. inportb(ata_secondary_master.io_base + ATA_REG_STATUS);
  282. if (atapi_in_progress) {
  283. wakeup_queue(atapi_waiter);
  284. }
  285. irq_ack(15);
  286. return 1;
  287. }
  288. static void ata_device_init(struct ata_device * dev) {
  289. debug_print(NOTICE, "Initializing IDE device on bus %d", dev->io_base);
  290. outportb(dev->io_base + 1, 1);
  291. outportb(dev->control, 0);
  292. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  293. ata_io_wait(dev);
  294. outportb(dev->io_base + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
  295. ata_io_wait(dev);
  296. int status = inportb(dev->io_base + ATA_REG_COMMAND);
  297. debug_print(INFO, "Device status: %d", status);
  298. ata_wait(dev, 0);
  299. uint16_t * buf = (uint16_t *)&dev->identity;
  300. for (int i = 0; i < 256; ++i) {
  301. buf[i] = inports(dev->io_base);
  302. }
  303. uint8_t * ptr = (uint8_t *)&dev->identity.model;
  304. for (int i = 0; i < 39; i+=2) {
  305. uint8_t tmp = ptr[i+1];
  306. ptr[i+1] = ptr[i];
  307. ptr[i] = tmp;
  308. }
  309. dev->is_atapi = 0;
  310. debug_print(NOTICE, "Device Name: %s", dev->identity.model);
  311. debug_print(NOTICE, "Sectors (48): %d", (uint32_t)dev->identity.sectors_48);
  312. debug_print(NOTICE, "Sectors (24): %d", dev->identity.sectors_28);
  313. debug_print(NOTICE, "Setting up DMA...");
  314. dev->dma_prdt = (void *)kvmalloc_p(sizeof(prdt_t) * 1, &dev->dma_prdt_phys);
  315. dev->dma_start = (void *)kvmalloc_p(4096, &dev->dma_start_phys);
  316. debug_print(NOTICE, "Putting prdt at 0x%x (0x%x phys)", dev->dma_prdt, dev->dma_prdt_phys);
  317. debug_print(NOTICE, "Putting prdt[0] at 0x%x (0x%x phys)", dev->dma_start, dev->dma_start_phys);
  318. dev->dma_prdt[0].offset = dev->dma_start_phys;
  319. dev->dma_prdt[0].bytes = 512;
  320. dev->dma_prdt[0].last = 0x8000;
  321. debug_print(NOTICE, "ATA PCI device ID: 0x%x", ata_pci);
  322. uint16_t command_reg = pci_read_field(ata_pci, PCI_COMMAND, 4);
  323. debug_print(NOTICE, "COMMAND register before: 0x%4x", command_reg);
  324. if (command_reg & (1 << 2)) {
  325. debug_print(NOTICE, "Bus mastering already enabled.");
  326. } else {
  327. command_reg |= (1 << 2); /* bit 2 */
  328. debug_print(NOTICE, "Enabling bus mastering...");
  329. pci_write_field(ata_pci, PCI_COMMAND, 4, command_reg);
  330. command_reg = pci_read_field(ata_pci, PCI_COMMAND, 4);
  331. debug_print(NOTICE, "COMMAND register after: 0x%4x", command_reg);
  332. }
  333. dev->bar4 = pci_read_field(ata_pci, PCI_BAR4, 4);
  334. debug_print(NOTICE, "BAR4: 0x%x", dev->bar4);
  335. if (dev->bar4 & 0x00000001) {
  336. dev->bar4 = dev->bar4 & 0xFFFFFFFC;
  337. } else {
  338. debug_print(WARNING, "? ATA bus master registers are /usually/ I/O ports.\n");
  339. return; /* No DMA because we're not sure what to do here */
  340. }
  341. #if 0
  342. pci_write_field(ata_pci, PCI_INTERRUPT_LINE, 1, 0xFE);
  343. if (pci_read_field(ata_pci, PCI_INTERRUPT_LINE, 1) == 0xFE) {
  344. /* needs assignment */
  345. pci_write_field(ata_pci, PCI_INTERRUPT_LINE, 1, 14);
  346. }
  347. #endif
  348. }
  349. static int atapi_device_init(struct ata_device * dev) {
  350. dev->is_atapi = 1;
  351. outportb(dev->io_base + 1, 1);
  352. outportb(dev->control, 0);
  353. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  354. ata_io_wait(dev);
  355. outportb(dev->io_base + ATA_REG_COMMAND, ATA_CMD_IDENTIFY_PACKET);
  356. ata_io_wait(dev);
  357. int status = inportb(dev->io_base + ATA_REG_COMMAND);
  358. debug_print(INFO, "Device status: %d", status);
  359. ata_wait(dev, 0);
  360. uint16_t * buf = (uint16_t *)&dev->identity;
  361. for (int i = 0; i < 256; ++i) {
  362. buf[i] = inports(dev->io_base);
  363. }
  364. uint8_t * ptr = (uint8_t *)&dev->identity.model;
  365. for (int i = 0; i < 39; i+=2) {
  366. uint8_t tmp = ptr[i+1];
  367. ptr[i+1] = ptr[i];
  368. ptr[i] = tmp;
  369. }
  370. debug_print(NOTICE, "Device Name: %s", dev->identity.model);
  371. /* Detect medium */
  372. atapi_command_t command;
  373. command.command_bytes[0] = 0x25;
  374. command.command_bytes[1] = 0;
  375. command.command_bytes[2] = 0;
  376. command.command_bytes[3] = 0;
  377. command.command_bytes[4] = 0;
  378. command.command_bytes[5] = 0;
  379. command.command_bytes[6] = 0;
  380. command.command_bytes[7] = 0;
  381. command.command_bytes[8] = 0; /* bit 0 = PMI (0, last sector) */
  382. command.command_bytes[9] = 0; /* control */
  383. command.command_bytes[10] = 0;
  384. command.command_bytes[11] = 0;
  385. uint16_t bus = dev->io_base;
  386. outportb(bus + ATA_REG_FEATURES, 0x00);
  387. outportb(bus + ATA_REG_LBA1, 0x08);
  388. outportb(bus + ATA_REG_LBA2, 0x08);
  389. outportb(bus + ATA_REG_COMMAND, ATA_CMD_PACKET);
  390. /* poll */
  391. while (1) {
  392. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  393. if ((status & ATA_SR_ERR)) goto atapi_error;
  394. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY)) break;
  395. }
  396. for (int i = 0; i < 6; ++i) {
  397. outports(bus, command.command_words[i]);
  398. }
  399. /* poll */
  400. while (1) {
  401. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  402. if ((status & ATA_SR_ERR)) goto atapi_error_read;
  403. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY)) break;
  404. if ((status & ATA_SR_DRQ)) break;
  405. }
  406. uint16_t data[4];
  407. for (int i = 0; i < 4; ++i) {
  408. data[i] = inports(bus);
  409. }
  410. #define htonl(l) ( (((l) & 0xFF) << 24) | (((l) & 0xFF00) << 8) | (((l) & 0xFF0000) >> 8) | (((l) & 0xFF000000) >> 24))
  411. uint32_t lba, blocks;;
  412. memcpy(&lba, &data[0], sizeof(uint32_t));
  413. lba = htonl(lba);
  414. memcpy(&blocks, &data[2], sizeof(uint32_t));
  415. blocks = htonl(blocks);
  416. dev->atapi_lba = lba;
  417. dev->atapi_sector_size = blocks;
  418. if (!lba) return 1;
  419. debug_print(WARNING, "Finished! LBA = %x; block length = %x", lba, blocks);
  420. return 0;
  421. atapi_error_read:
  422. debug_print(ERROR, "ATAPI error; no medium?");
  423. return 1;
  424. atapi_error:
  425. debug_print(ERROR, "ATAPI early error; unsure");
  426. return 1;
  427. }
  428. static int ata_device_detect(struct ata_device * dev) {
  429. ata_soft_reset(dev);
  430. ata_io_wait(dev);
  431. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  432. ata_io_wait(dev);
  433. ata_status_wait(dev, 10000);
  434. unsigned char cl = inportb(dev->io_base + ATA_REG_LBA1); /* CYL_LO */
  435. unsigned char ch = inportb(dev->io_base + ATA_REG_LBA2); /* CYL_HI */
  436. debug_print(NOTICE, "Device detected: 0x%2x 0x%2x", cl, ch);
  437. if (cl == 0xFF && ch == 0xFF) {
  438. /* Nothing here */
  439. return 0;
  440. }
  441. if ((cl == 0x00 && ch == 0x00) ||
  442. (cl == 0x3C && ch == 0xC3)) {
  443. /* Parallel ATA device, or emulated SATA */
  444. char devname[64];
  445. sprintf((char *)&devname, "/dev/hd%c", ata_drive_char);
  446. fs_node_t * node = ata_device_create(dev);
  447. vfs_mount(devname, node);
  448. ata_drive_char++;
  449. ata_device_init(dev);
  450. return 1;
  451. } else if ((cl == 0x14 && ch == 0xEB) ||
  452. (cl == 0x69 && ch == 0x96)) {
  453. debug_print(WARNING, "Detected ATAPI device at io-base 0x%3x, control 0x%3x, slave %d", dev->io_base, dev->control, dev->slave);
  454. char devname[64];
  455. sprintf((char *)&devname, "/dev/cdrom%d", cdrom_number);
  456. if (atapi_device_init(dev)) {
  457. return 0;
  458. }
  459. fs_node_t * node = atapi_device_create(dev);
  460. vfs_mount(devname, node);
  461. cdrom_number++;
  462. return 2;
  463. }
  464. /* TODO: ATAPI, SATA, SATAPI */
  465. return 0;
  466. }
  467. static void ata_device_read_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  468. uint16_t bus = dev->io_base;
  469. uint8_t slave = dev->slave;
  470. if (dev->is_atapi) return;
  471. spin_lock(ata_lock);
  472. #if 0
  473. int errors = 0;
  474. try_again:
  475. #endif
  476. ata_wait(dev, 0);
  477. /* Stop */
  478. outportb(dev->bar4, 0x00);
  479. /* Set the PRDT */
  480. outportl(dev->bar4 + 0x04, dev->dma_prdt_phys);
  481. /* Enable error, irq status */
  482. outportb(dev->bar4 + 0x2, inportb(dev->bar4 + 0x02) | 0x04 | 0x02);
  483. /* set read */
  484. outportb(dev->bar4, 0x08);
  485. IRQ_ON;
  486. while (1) {
  487. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  488. if (!(status & ATA_SR_BSY)) break;
  489. }
  490. outportb(bus + ATA_REG_CONTROL, 0x00);
  491. outportb(bus + ATA_REG_HDDEVSEL, 0xe0 | slave << 4 | (lba & 0x0f000000) >> 24);
  492. ata_io_wait(dev);
  493. outportb(bus + ATA_REG_FEATURES, 0x00);
  494. outportb(bus + ATA_REG_SECCOUNT0, 1);
  495. outportb(bus + ATA_REG_LBA0, (lba & 0x000000ff) >> 0);
  496. outportb(bus + ATA_REG_LBA1, (lba & 0x0000ff00) >> 8);
  497. outportb(bus + ATA_REG_LBA2, (lba & 0x00ff0000) >> 16);
  498. //outportb(bus + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
  499. #if 1
  500. while (1) {
  501. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  502. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY)) break;
  503. }
  504. #endif
  505. outportb(bus + ATA_REG_COMMAND, ATA_CMD_READ_DMA);
  506. ata_io_wait(dev);
  507. outportb(dev->bar4, 0x08 | 0x01);
  508. while (1) {
  509. int status = inportb(dev->bar4 + 0x02);
  510. int dstatus = inportb(dev->io_base + ATA_REG_STATUS);
  511. if (!(status & 0x04)) {
  512. continue;
  513. }
  514. if (!(dstatus & ATA_SR_BSY)) {
  515. break;
  516. }
  517. }
  518. IRQ_OFF;
  519. #if 0
  520. if (ata_wait(dev, 1)) {
  521. debug_print(WARNING, "Error during ATA read of lba block %d", lba);
  522. errors++;
  523. if (errors > 4) {
  524. debug_print(WARNING, "-- Too many errors trying to read this block. Bailing.");
  525. spin_unlock(ata_lock);
  526. return;
  527. }
  528. goto try_again;
  529. }
  530. #endif
  531. /* Copy from DMA buffer to output buffer. */
  532. memcpy(buf, dev->dma_start, 512);
  533. /* Inform device we are done. */
  534. outportb(dev->bar4 + 0x2, inportb(dev->bar4 + 0x02) | 0x04 | 0x02);
  535. #if 0
  536. int size = 256;
  537. inportsm(bus,buf,size);
  538. ata_wait(dev, 0);
  539. outportb(bus + ATA_REG_CONTROL, 0x02);
  540. #endif
  541. spin_unlock(ata_lock);
  542. }
  543. static void ata_device_read_sector_atapi(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  544. if (!dev->is_atapi) return;
  545. uint16_t bus = dev->io_base;
  546. spin_lock(ata_lock);
  547. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  548. ata_io_wait(dev);
  549. outportb(bus + ATA_REG_FEATURES, 0x00);
  550. outportb(bus + ATA_REG_LBA1, dev->atapi_sector_size & 0xFF);
  551. outportb(bus + ATA_REG_LBA2, dev->atapi_sector_size >> 8);
  552. outportb(bus + ATA_REG_COMMAND, ATA_CMD_PACKET);
  553. /* poll */
  554. while (1) {
  555. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  556. if ((status & ATA_SR_ERR)) goto atapi_error_on_read_setup;
  557. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) break;
  558. }
  559. atapi_in_progress = 1;
  560. atapi_command_t command;
  561. command.command_bytes[0] = 0xA8;
  562. command.command_bytes[1] = 0;
  563. command.command_bytes[2] = (lba >> 0x18) & 0xFF;
  564. command.command_bytes[3] = (lba >> 0x10) & 0xFF;
  565. command.command_bytes[4] = (lba >> 0x08) & 0xFF;
  566. command.command_bytes[5] = (lba >> 0x00) & 0xFF;
  567. command.command_bytes[6] = 0;
  568. command.command_bytes[7] = 0;
  569. command.command_bytes[8] = 0; /* bit 0 = PMI (0, last sector) */
  570. command.command_bytes[9] = 1; /* control */
  571. command.command_bytes[10] = 0;
  572. command.command_bytes[11] = 0;
  573. for (int i = 0; i < 6; ++i) {
  574. outports(bus, command.command_words[i]);
  575. }
  576. /* Wait */
  577. sleep_on(atapi_waiter);
  578. atapi_in_progress = 0;
  579. while (1) {
  580. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  581. if ((status & ATA_SR_ERR)) goto atapi_error_on_read_setup;
  582. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) break;
  583. }
  584. uint16_t size_to_read = inportb(bus + ATA_REG_LBA2) << 8;
  585. size_to_read = size_to_read | inportb(bus + ATA_REG_LBA1);
  586. inportsm(bus,buf,size_to_read/2);
  587. while (1) {
  588. uint8_t status = inportb(dev->io_base + ATA_REG_STATUS);
  589. if ((status & ATA_SR_ERR)) goto atapi_error_on_read_setup;
  590. if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY)) break;
  591. }
  592. atapi_error_on_read_setup:
  593. spin_unlock(ata_lock);
  594. }
  595. static void ata_device_write_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  596. uint16_t bus = dev->io_base;
  597. uint8_t slave = dev->slave;
  598. spin_lock(ata_lock);
  599. outportb(bus + ATA_REG_CONTROL, 0x02);
  600. ata_wait(dev, 0);
  601. outportb(bus + ATA_REG_HDDEVSEL, 0xe0 | slave << 4 | (lba & 0x0f000000) >> 24);
  602. ata_wait(dev, 0);
  603. outportb(bus + ATA_REG_FEATURES, 0x00);
  604. outportb(bus + ATA_REG_SECCOUNT0, 0x01);
  605. outportb(bus + ATA_REG_LBA0, (lba & 0x000000ff) >> 0);
  606. outportb(bus + ATA_REG_LBA1, (lba & 0x0000ff00) >> 8);
  607. outportb(bus + ATA_REG_LBA2, (lba & 0x00ff0000) >> 16);
  608. outportb(bus + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
  609. ata_wait(dev, 0);
  610. int size = ATA_SECTOR_SIZE / 2;
  611. outportsm(bus,buf,size);
  612. outportb(bus + 0x07, ATA_CMD_CACHE_FLUSH);
  613. ata_wait(dev, 0);
  614. spin_unlock(ata_lock);
  615. }
  616. static int buffer_compare(uint32_t * ptr1, uint32_t * ptr2, size_t size) {
  617. assert(!(size % 4));
  618. size_t i = 0;
  619. while (i < size) {
  620. if (*ptr1 != *ptr2) return 1;
  621. ptr1++;
  622. ptr2++;
  623. i += sizeof(uint32_t);
  624. }
  625. return 0;
  626. }
  627. static void ata_device_write_sector_retry(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  628. uint8_t * read_buf = malloc(ATA_SECTOR_SIZE);
  629. do {
  630. ata_device_write_sector(dev, lba, buf);
  631. ata_device_read_sector(dev, lba, read_buf);
  632. } while (buffer_compare((uint32_t *)buf, (uint32_t *)read_buf, ATA_SECTOR_SIZE));
  633. free(read_buf);
  634. }
  635. static int ata_initialize(void) {
  636. /* Detect drives and mount them */
  637. /* Locate ATA device via PCI */
  638. pci_scan(&find_ata_pci, -1, &ata_pci);
  639. irq_install_handler(14, ata_irq_handler, "ide master");
  640. irq_install_handler(15, ata_irq_handler_s, "ide slave");
  641. atapi_waiter = list_create();
  642. ata_device_detect(&ata_primary_master);
  643. ata_device_detect(&ata_primary_slave);
  644. ata_device_detect(&ata_secondary_master);
  645. ata_device_detect(&ata_secondary_slave);
  646. return 0;
  647. }
  648. static int ata_finalize(void) {
  649. return 0;
  650. }
  651. MODULE_DEF(ata, ata_initialize, ata_finalize);