ataold.c 11 KB

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