ata.c 24 KB

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