ataold.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /* TODO: Move this to mod/ata.h */
  16. #include <kernel/ata.h>
  17. static char ata_drive_char = 'a';
  18. struct ata_device {
  19. int io_base;
  20. int control;
  21. int slave;
  22. ata_identify_t identity;
  23. };
  24. //static volatile uint8_t ata_lock = 0;
  25. static spin_lock_t ata_lock = { 0 };
  26. /* TODO support other sector sizes */
  27. #define ATA_SECTOR_SIZE 512
  28. static void ata_device_read_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf);
  29. static void ata_device_write_sector_retry(struct ata_device * dev, uint32_t lba, uint8_t * buf);
  30. static uint32_t read_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  31. static uint32_t write_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  32. static void open_ata(fs_node_t *node, unsigned int flags);
  33. static void close_ata(fs_node_t *node);
  34. static uint64_t ata_max_offset(struct ata_device * dev) {
  35. uint64_t sectors = dev->identity.sectors_48;
  36. if (!sectors) {
  37. /* Fall back to sectors_28 */
  38. sectors = dev->identity.sectors_28;
  39. }
  40. return sectors * ATA_SECTOR_SIZE;
  41. }
  42. static uint32_t read_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  43. struct ata_device * dev = (struct ata_device *)node->device;
  44. unsigned int start_block = offset / ATA_SECTOR_SIZE;
  45. unsigned int end_block = (offset + size - 1) / ATA_SECTOR_SIZE;
  46. unsigned int x_offset = 0;
  47. if (offset > ata_max_offset(dev)) {
  48. return 0;
  49. }
  50. if (offset + size > ata_max_offset(dev)) {
  51. unsigned int i = ata_max_offset(dev) - offset;
  52. size = i;
  53. }
  54. if (offset % ATA_SECTOR_SIZE) {
  55. unsigned int prefix_size = (ATA_SECTOR_SIZE - (offset % ATA_SECTOR_SIZE));
  56. char * tmp = malloc(ATA_SECTOR_SIZE);
  57. ata_device_read_sector(dev, start_block, (uint8_t *)tmp);
  58. memcpy(buffer, (void *)((uintptr_t)tmp + (offset % ATA_SECTOR_SIZE)), prefix_size);
  59. free(tmp);
  60. x_offset += prefix_size;
  61. start_block++;
  62. }
  63. if ((offset + size) % ATA_SECTOR_SIZE && start_block < end_block) {
  64. unsigned int postfix_size = (offset + size) % ATA_SECTOR_SIZE;
  65. char * tmp = malloc(ATA_SECTOR_SIZE);
  66. ata_device_read_sector(dev, end_block, (uint8_t *)tmp);
  67. memcpy((void *)((uintptr_t)buffer + size - postfix_size), tmp, postfix_size);
  68. free(tmp);
  69. end_block--;
  70. }
  71. while (start_block <= end_block) {
  72. ata_device_read_sector(dev, start_block, (uint8_t *)((uintptr_t)buffer + x_offset));
  73. x_offset += ATA_SECTOR_SIZE;
  74. start_block++;
  75. }
  76. return size;
  77. }
  78. static uint32_t write_ata(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  79. struct ata_device * dev = (struct ata_device *)node->device;
  80. unsigned int start_block = offset / ATA_SECTOR_SIZE;
  81. unsigned int end_block = (offset + size - 1) / ATA_SECTOR_SIZE;
  82. unsigned int x_offset = 0;
  83. if (offset > ata_max_offset(dev)) {
  84. return 0;
  85. }
  86. if (offset + size > ata_max_offset(dev)) {
  87. unsigned int i = ata_max_offset(dev) - offset;
  88. size = i;
  89. }
  90. if (offset % ATA_SECTOR_SIZE) {
  91. unsigned int prefix_size = (ATA_SECTOR_SIZE - (offset % ATA_SECTOR_SIZE));
  92. char * tmp = malloc(ATA_SECTOR_SIZE);
  93. ata_device_read_sector(dev, start_block, (uint8_t *)tmp);
  94. debug_print(NOTICE, "Writing first block");
  95. memcpy((void *)((uintptr_t)tmp + (offset % ATA_SECTOR_SIZE)), buffer, prefix_size);
  96. ata_device_write_sector_retry(dev, start_block, (uint8_t *)tmp);
  97. free(tmp);
  98. x_offset += prefix_size;
  99. start_block++;
  100. }
  101. if ((offset + size) % ATA_SECTOR_SIZE && start_block < end_block) {
  102. unsigned int postfix_size = (offset + size) % ATA_SECTOR_SIZE;
  103. char * tmp = malloc(ATA_SECTOR_SIZE);
  104. ata_device_read_sector(dev, end_block, (uint8_t *)tmp);
  105. debug_print(NOTICE, "Writing last block");
  106. memcpy(tmp, (void *)((uintptr_t)buffer + size - postfix_size), postfix_size);
  107. ata_device_write_sector_retry(dev, end_block, (uint8_t *)tmp);
  108. free(tmp);
  109. end_block--;
  110. }
  111. while (start_block <= end_block) {
  112. ata_device_write_sector_retry(dev, start_block, (uint8_t *)((uintptr_t)buffer + x_offset));
  113. x_offset += ATA_SECTOR_SIZE;
  114. start_block++;
  115. }
  116. return size;
  117. }
  118. static void open_ata(fs_node_t * node, unsigned int flags) {
  119. return;
  120. }
  121. static void close_ata(fs_node_t * node) {
  122. return;
  123. }
  124. static fs_node_t * ata_device_create(struct ata_device * device) {
  125. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  126. memset(fnode, 0x00, sizeof(fs_node_t));
  127. fnode->inode = 0;
  128. sprintf(fnode->name, "atadev%d", ata_drive_char - 'a');
  129. fnode->device = device;
  130. fnode->uid = 0;
  131. fnode->gid = 0;
  132. fnode->mask = 0660;
  133. fnode->length = ata_max_offset(device); /* TODO */
  134. fnode->flags = FS_BLOCKDEVICE;
  135. fnode->read = read_ata;
  136. fnode->write = write_ata;
  137. fnode->open = open_ata;
  138. fnode->close = close_ata;
  139. fnode->readdir = NULL;
  140. fnode->finddir = NULL;
  141. fnode->ioctl = NULL; /* TODO, identify, etc? */
  142. return fnode;
  143. }
  144. static void ata_io_wait(struct ata_device * dev) {
  145. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  146. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  147. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  148. inportb(dev->io_base + ATA_REG_ALTSTATUS);
  149. }
  150. static int ata_status_wait(struct ata_device * dev, int timeout) {
  151. int status;
  152. if (timeout > 0) {
  153. int i = 0;
  154. while ((status = inportb(dev->io_base + ATA_REG_STATUS)) & ATA_SR_BSY && (i < timeout)) i++;
  155. } else {
  156. while ((status = inportb(dev->io_base + ATA_REG_STATUS)) & ATA_SR_BSY);
  157. }
  158. return status;
  159. }
  160. static int ata_wait(struct ata_device * dev, int advanced) {
  161. uint8_t status = 0;
  162. ata_io_wait(dev);
  163. status = ata_status_wait(dev, -1);
  164. if (advanced) {
  165. status = inportb(dev->io_base + ATA_REG_STATUS);
  166. if (status & ATA_SR_ERR) return 1;
  167. if (status & ATA_SR_DF) return 1;
  168. if (!(status & ATA_SR_DRQ)) return 1;
  169. }
  170. return 0;
  171. }
  172. static void ata_soft_reset(struct ata_device * dev) {
  173. outportb(dev->control, 0x04);
  174. ata_io_wait(dev);
  175. outportb(dev->control, 0x00);
  176. }
  177. static void ata_device_init(struct ata_device * dev) {
  178. debug_print(NOTICE, "Initializing IDE device on bus %d", dev->io_base);
  179. outportb(dev->io_base + 1, 1);
  180. outportb(dev->control, 0);
  181. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  182. ata_io_wait(dev);
  183. outportb(dev->io_base + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
  184. ata_io_wait(dev);
  185. int status = inportb(dev->io_base + ATA_REG_COMMAND);
  186. debug_print(INFO, "Device status: %d", status);
  187. ata_wait(dev, 0);
  188. uint16_t * buf = (uint16_t *)&dev->identity;
  189. for (int i = 0; i < 256; ++i) {
  190. buf[i] = inports(dev->io_base);
  191. }
  192. uint8_t * ptr = (uint8_t *)&dev->identity.model;
  193. for (int i = 0; i < 39; i+=2) {
  194. uint8_t tmp = ptr[i+1];
  195. ptr[i+1] = ptr[i];
  196. ptr[i] = tmp;
  197. }
  198. debug_print(NOTICE, "Device Name: %s", dev->identity.model);
  199. debug_print(NOTICE, "Sectors (48): %d", (uint32_t)dev->identity.sectors_48);
  200. debug_print(NOTICE, "Sectors (24): %d", dev->identity.sectors_28);
  201. outportb(dev->io_base + ATA_REG_CONTROL, 0x02);
  202. }
  203. static int ata_device_detect(struct ata_device * dev) {
  204. ata_soft_reset(dev);
  205. ata_io_wait(dev);
  206. outportb(dev->io_base + ATA_REG_HDDEVSEL, 0xA0 | dev->slave << 4);
  207. ata_io_wait(dev);
  208. ata_status_wait(dev, 10000);
  209. unsigned char cl = inportb(dev->io_base + ATA_REG_LBA1); /* CYL_LO */
  210. unsigned char ch = inportb(dev->io_base + ATA_REG_LBA2); /* CYL_HI */
  211. debug_print(NOTICE, "Device detected: 0x%2x 0x%2x", cl, ch);
  212. if (cl == 0xFF && ch == 0xFF) {
  213. /* Nothing here */
  214. return 0;
  215. }
  216. if ((cl == 0x00 && ch == 0x00) ||
  217. (cl == 0x3C && ch == 0xC3)) {
  218. /* Parallel ATA device, or emulated SATA */
  219. char devname[64];
  220. sprintf((char *)&devname, "/dev/hd%c", ata_drive_char);
  221. fs_node_t * node = ata_device_create(dev);
  222. vfs_mount(devname, node);
  223. ata_drive_char++;
  224. ata_device_init(dev);
  225. return 1;
  226. }
  227. /* TODO: ATAPI, SATA, SATAPI */
  228. return 0;
  229. }
  230. static void ata_device_read_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  231. uint16_t bus = dev->io_base;
  232. uint8_t slave = dev->slave;
  233. spin_lock(ata_lock);
  234. int errors = 0;
  235. try_again:
  236. outportb(bus + ATA_REG_CONTROL, 0x02);
  237. ata_wait(dev, 0);
  238. outportb(bus + ATA_REG_HDDEVSEL, 0xe0 | slave << 4 | (lba & 0x0f000000) >> 24);
  239. outportb(bus + ATA_REG_FEATURES, 0x00);
  240. outportb(bus + ATA_REG_SECCOUNT0, 1);
  241. outportb(bus + ATA_REG_LBA0, (lba & 0x000000ff) >> 0);
  242. outportb(bus + ATA_REG_LBA1, (lba & 0x0000ff00) >> 8);
  243. outportb(bus + ATA_REG_LBA2, (lba & 0x00ff0000) >> 16);
  244. outportb(bus + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
  245. if (ata_wait(dev, 1)) {
  246. debug_print(WARNING, "Error during ATA read of lba block %d", lba);
  247. errors++;
  248. if (errors > 4) {
  249. debug_print(WARNING, "-- Too many errors trying to read this block. Bailing.");
  250. spin_unlock(ata_lock);
  251. return;
  252. }
  253. goto try_again;
  254. }
  255. int size = 256;
  256. inportsm(bus,buf,size);
  257. ata_wait(dev, 0);
  258. spin_unlock(ata_lock);
  259. }
  260. static void ata_device_write_sector(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  261. uint16_t bus = dev->io_base;
  262. uint8_t slave = dev->slave;
  263. spin_lock(ata_lock);
  264. outportb(bus + ATA_REG_CONTROL, 0x02);
  265. ata_wait(dev, 0);
  266. outportb(bus + ATA_REG_HDDEVSEL, 0xe0 | slave << 4 | (lba & 0x0f000000) >> 24);
  267. ata_wait(dev, 0);
  268. outportb(bus + ATA_REG_FEATURES, 0x00);
  269. outportb(bus + ATA_REG_SECCOUNT0, 0x01);
  270. outportb(bus + ATA_REG_LBA0, (lba & 0x000000ff) >> 0);
  271. outportb(bus + ATA_REG_LBA1, (lba & 0x0000ff00) >> 8);
  272. outportb(bus + ATA_REG_LBA2, (lba & 0x00ff0000) >> 16);
  273. outportb(bus + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
  274. ata_wait(dev, 0);
  275. int size = ATA_SECTOR_SIZE / 2;
  276. outportsm(bus,buf,size);
  277. outportb(bus + 0x07, ATA_CMD_CACHE_FLUSH);
  278. ata_wait(dev, 0);
  279. spin_unlock(ata_lock);
  280. }
  281. static int buffer_compare(uint32_t * ptr1, uint32_t * ptr2, size_t size) {
  282. assert(!(size % 4));
  283. size_t i = 0;
  284. while (i < size) {
  285. if (*ptr1 != *ptr2) return 1;
  286. ptr1++;
  287. ptr2++;
  288. i += sizeof(uint32_t);
  289. }
  290. return 0;
  291. }
  292. static void ata_device_write_sector_retry(struct ata_device * dev, uint32_t lba, uint8_t * buf) {
  293. uint8_t * read_buf = malloc(ATA_SECTOR_SIZE);
  294. IRQ_OFF;
  295. do {
  296. ata_device_write_sector(dev, lba, buf);
  297. ata_device_read_sector(dev, lba, read_buf);
  298. } while (buffer_compare((uint32_t *)buf, (uint32_t *)read_buf, ATA_SECTOR_SIZE));
  299. IRQ_RES;
  300. free(read_buf);
  301. }
  302. static struct ata_device ata_primary_master = {.io_base = 0x1F0, .control = 0x3F6, .slave = 0};
  303. static struct ata_device ata_primary_slave = {.io_base = 0x1F0, .control = 0x3F6, .slave = 1};
  304. static struct ata_device ata_secondary_master = {.io_base = 0x170, .control = 0x376, .slave = 0};
  305. static struct ata_device ata_secondary_slave = {.io_base = 0x170, .control = 0x376, .slave = 1};
  306. static int ata_initialize(void) {
  307. /* Detect drives and mount them */
  308. ata_device_detect(&ata_primary_master);
  309. ata_device_detect(&ata_primary_slave);
  310. ata_device_detect(&ata_secondary_master);
  311. ata_device_detect(&ata_secondary_slave);
  312. return 0;
  313. }
  314. static int ata_finalize(void) {
  315. return 0;
  316. }
  317. MODULE_DEF(ata_legacy, ata_initialize, ata_finalize);