e1000.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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) 2017-2018 K. Lange
  5. */
  6. #include <kernel/module.h>
  7. #include <kernel/logging.h>
  8. #include <kernel/printf.h>
  9. #include <kernel/pci.h>
  10. #include <kernel/mem.h>
  11. #include <kernel/pipe.h>
  12. #include <kernel/ipv4.h>
  13. #include <kernel/mod/net.h>
  14. #include <toaru/list.h>
  15. #define E1000_LOG_LEVEL NOTICE
  16. static uint32_t e1000_device_pci = 0x00000000;
  17. static int e1000_irq = 0;
  18. static uintptr_t mem_base = 0;
  19. static int has_eeprom = 0;
  20. static uint8_t mac[6];
  21. static int rx_index = 0;
  22. static int tx_index = 0;
  23. static list_t * net_queue = NULL;
  24. static spin_lock_t net_queue_lock = { 0 };
  25. static list_t * rx_wait;
  26. static uint32_t mmio_read32(uintptr_t addr) {
  27. return *((volatile uint32_t*)(addr));
  28. }
  29. static void mmio_write32(uintptr_t addr, uint32_t val) {
  30. (*((volatile uint32_t*)(addr))) = val;
  31. }
  32. static void write_command(uint16_t addr, uint32_t val) {
  33. mmio_write32(mem_base + addr, val);
  34. }
  35. static uint32_t read_command(uint16_t addr) {
  36. return mmio_read32(mem_base + addr);
  37. }
  38. #define E1000_NUM_RX_DESC 32
  39. #define E1000_NUM_TX_DESC 8
  40. struct rx_desc {
  41. volatile uint64_t addr;
  42. volatile uint16_t length;
  43. volatile uint16_t checksum;
  44. volatile uint8_t status;
  45. volatile uint8_t errors;
  46. volatile uint16_t special;
  47. } __attribute__((packed)); /* this looks like it should pack fine as-is */
  48. struct tx_desc {
  49. volatile uint64_t addr;
  50. volatile uint16_t length;
  51. volatile uint8_t cso;
  52. volatile uint8_t cmd;
  53. volatile uint8_t status;
  54. volatile uint8_t css;
  55. volatile uint16_t special;
  56. } __attribute__((packed));
  57. static uint8_t * rx_virt[E1000_NUM_RX_DESC];
  58. static uint8_t * tx_virt[E1000_NUM_TX_DESC];
  59. static struct rx_desc * rx;
  60. static struct tx_desc * tx;
  61. static uintptr_t rx_phys;
  62. static uintptr_t tx_phys;
  63. static void enqueue_packet(void * buffer) {
  64. spin_lock(net_queue_lock);
  65. list_insert(net_queue, buffer);
  66. spin_unlock(net_queue_lock);
  67. }
  68. static struct ethernet_packet * dequeue_packet(void) {
  69. while (!net_queue->length) {
  70. sleep_on(rx_wait);
  71. }
  72. spin_lock(net_queue_lock);
  73. node_t * n = list_dequeue(net_queue);
  74. void* value = n->value;
  75. free(n);
  76. spin_unlock(net_queue_lock);
  77. return value;
  78. }
  79. static uint8_t* get_mac() {
  80. return mac;
  81. }
  82. #define E1000_REG_CTRL 0x0000
  83. #define E1000_REG_STATUS 0x0008
  84. #define E1000_REG_EEPROM 0x0014
  85. #define E1000_REG_CTRL_EXT 0x0018
  86. #define E1000_REG_RCTRL 0x0100
  87. #define E1000_REG_RXDESCLO 0x2800
  88. #define E1000_REG_RXDESCHI 0x2804
  89. #define E1000_REG_RXDESCLEN 0x2808
  90. #define E1000_REG_RXDESCHEAD 0x2810
  91. #define E1000_REG_RXDESCTAIL 0x2818
  92. #define E1000_REG_TCTRL 0x0400
  93. #define E1000_REG_TXDESCLO 0x3800
  94. #define E1000_REG_TXDESCHI 0x3804
  95. #define E1000_REG_TXDESCLEN 0x3808
  96. #define E1000_REG_TXDESCHEAD 0x3810
  97. #define E1000_REG_TXDESCTAIL 0x3818
  98. #define RCTL_EN (1 << 1) /* Receiver Enable */
  99. #define RCTL_SBP (1 << 2) /* Store Bad Packets */
  100. #define RCTL_UPE (1 << 3) /* Unicast Promiscuous Enabled */
  101. #define RCTL_MPE (1 << 4) /* Multicast Promiscuous Enabled */
  102. #define RCTL_LPE (1 << 5) /* Long Packet Reception Enable */
  103. #define RCTL_LBM_NONE (0 << 6) /* No Loopback */
  104. #define RCTL_LBM_PHY (3 << 6) /* PHY or external SerDesc loopback */
  105. #define RTCL_RDMTS_HALF (0 << 8) /* Free Buffer Threshold is 1/2 of RDLEN */
  106. #define RTCL_RDMTS_QUARTER (1 << 8) /* Free Buffer Threshold is 1/4 of RDLEN */
  107. #define RTCL_RDMTS_EIGHTH (2 << 8) /* Free Buffer Threshold is 1/8 of RDLEN */
  108. #define RCTL_MO_36 (0 << 12) /* Multicast Offset - bits 47:36 */
  109. #define RCTL_MO_35 (1 << 12) /* Multicast Offset - bits 46:35 */
  110. #define RCTL_MO_34 (2 << 12) /* Multicast Offset - bits 45:34 */
  111. #define RCTL_MO_32 (3 << 12) /* Multicast Offset - bits 43:32 */
  112. #define RCTL_BAM (1 << 15) /* Broadcast Accept Mode */
  113. #define RCTL_VFE (1 << 18) /* VLAN Filter Enable */
  114. #define RCTL_CFIEN (1 << 19) /* Canonical Form Indicator Enable */
  115. #define RCTL_CFI (1 << 20) /* Canonical Form Indicator Bit Value */
  116. #define RCTL_DPF (1 << 22) /* Discard Pause Frames */
  117. #define RCTL_PMCF (1 << 23) /* Pass MAC Control Frames */
  118. #define RCTL_SECRC (1 << 26) /* Strip Ethernet CRC */
  119. #define RCTL_BSIZE_256 (3 << 16)
  120. #define RCTL_BSIZE_512 (2 << 16)
  121. #define RCTL_BSIZE_1024 (1 << 16)
  122. #define RCTL_BSIZE_2048 (0 << 16)
  123. #define RCTL_BSIZE_4096 ((3 << 16) | (1 << 25))
  124. #define RCTL_BSIZE_8192 ((2 << 16) | (1 << 25))
  125. #define RCTL_BSIZE_16384 ((1 << 16) | (1 << 25))
  126. #define TCTL_EN (1 << 1) /* Transmit Enable */
  127. #define TCTL_PSP (1 << 3) /* Pad Short Packets */
  128. #define TCTL_CT_SHIFT 4 /* Collision Threshold */
  129. #define TCTL_COLD_SHIFT 12 /* Collision Distance */
  130. #define TCTL_SWXOFF (1 << 22) /* Software XOFF Transmission */
  131. #define TCTL_RTLC (1 << 24) /* Re-transmit on Late Collision */
  132. #define CMD_EOP (1 << 0) /* End of Packet */
  133. #define CMD_IFCS (1 << 1) /* Insert FCS */
  134. #define CMD_IC (1 << 2) /* Insert Checksum */
  135. #define CMD_RS (1 << 3) /* Report Status */
  136. #define CMD_RPS (1 << 4) /* Report Packet Sent */
  137. #define CMD_VLE (1 << 6) /* VLAN Packet Enable */
  138. #define CMD_IDE (1 << 7) /* Interrupt Delay Enable */
  139. static int eeprom_detect(void) {
  140. write_command(E1000_REG_EEPROM, 1);
  141. for (int i = 0; i < 100000 && !has_eeprom; ++i) {
  142. uint32_t val = read_command(E1000_REG_EEPROM);
  143. if (val & 0x10) has_eeprom = 1;
  144. }
  145. return 0;
  146. }
  147. static uint16_t eeprom_read(uint8_t addr) {
  148. uint32_t temp = 0;
  149. write_command(E1000_REG_EEPROM, 1 | ((uint32_t)(addr) << 8));
  150. while (!((temp = read_command(E1000_REG_EEPROM)) & (1 << 4)));
  151. return (uint16_t)((temp >> 16) & 0xFFFF);
  152. }
  153. static void find_e1000(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  154. if ((vendorid == 0x8086) && (deviceid == 0x100e || deviceid == 0x1004 || deviceid == 0x100f)) {
  155. *((uint32_t *)extra) = device;
  156. }
  157. }
  158. static void read_mac(void) {
  159. if (has_eeprom) {
  160. uint32_t t;
  161. t = eeprom_read(0);
  162. mac[0] = t & 0xFF;
  163. mac[1] = t >> 8;
  164. t = eeprom_read(1);
  165. mac[2] = t & 0xFF;
  166. mac[3] = t >> 8;
  167. t = eeprom_read(2);
  168. mac[4] = t & 0xFF;
  169. mac[5] = t >> 8;
  170. } else {
  171. uint8_t * mac_addr = (uint8_t *)(mem_base + 0x5400);
  172. for (int i = 0; i < 6; ++i) {
  173. mac[i] = mac_addr[i];
  174. }
  175. }
  176. }
  177. static int irq_handler(struct regs *r) {
  178. uint32_t status = read_command(0xc0);
  179. irq_ack(e1000_irq);
  180. if (!status) {
  181. return 0;
  182. }
  183. if (status & 0x04) {
  184. /* Start link */
  185. debug_print(E1000_LOG_LEVEL, "start link");
  186. } else if (status & 0x10) {
  187. /* ?? */
  188. } else if (status & ((1 << 6) | (1 << 7))) {
  189. /* receive packet */
  190. do {
  191. rx_index = read_command(E1000_REG_RXDESCTAIL);
  192. if (rx_index == (int)read_command(E1000_REG_RXDESCHEAD)) return 1;
  193. rx_index = (rx_index + 1) % E1000_NUM_RX_DESC;
  194. if (rx[rx_index].status & 0x01) {
  195. uint8_t * pbuf = (uint8_t *)rx_virt[rx_index];
  196. uint16_t plen = rx[rx_index].length;
  197. void * packet = malloc(plen);
  198. memcpy(packet, pbuf, plen);
  199. rx[rx_index].status = 0;
  200. enqueue_packet(packet);
  201. write_command(E1000_REG_RXDESCTAIL, rx_index);
  202. } else {
  203. break;
  204. }
  205. } while (1);
  206. wakeup_queue(rx_wait);
  207. }
  208. return 1;
  209. }
  210. static void send_packet(uint8_t* payload, size_t payload_size) {
  211. tx_index = read_command(E1000_REG_TXDESCTAIL);
  212. debug_print(E1000_LOG_LEVEL,"sending packet 0x%x, %d desc[%d]", payload, payload_size, tx_index);
  213. memcpy(tx_virt[tx_index], payload, payload_size);
  214. tx[tx_index].length = payload_size;
  215. tx[tx_index].cmd = CMD_EOP | CMD_IFCS | CMD_RS; //| CMD_RPS;
  216. tx[tx_index].status = 0;
  217. tx_index = (tx_index + 1) % E1000_NUM_TX_DESC;
  218. write_command(E1000_REG_TXDESCTAIL, tx_index);
  219. }
  220. static void init_rx(void) {
  221. write_command(E1000_REG_RXDESCLO, rx_phys);
  222. write_command(E1000_REG_RXDESCHI, 0);
  223. write_command(E1000_REG_RXDESCLEN, E1000_NUM_RX_DESC * sizeof(struct rx_desc));
  224. write_command(E1000_REG_RXDESCHEAD, 0);
  225. write_command(E1000_REG_RXDESCTAIL, E1000_NUM_RX_DESC - 1);
  226. rx_index = 0;
  227. write_command(E1000_REG_RCTRL,
  228. RCTL_EN |
  229. (read_command(E1000_REG_RCTRL) & (~((1 << 17) | (1 << 16)))));
  230. }
  231. static void init_tx(void) {
  232. write_command(E1000_REG_TXDESCLO, tx_phys);
  233. write_command(E1000_REG_TXDESCHI, 0);
  234. write_command(E1000_REG_TXDESCLEN, E1000_NUM_TX_DESC * sizeof(struct tx_desc));
  235. write_command(E1000_REG_TXDESCHEAD, 0);
  236. write_command(E1000_REG_TXDESCTAIL, 0);
  237. tx_index = 0;
  238. write_command(E1000_REG_TCTRL,
  239. TCTL_EN |
  240. TCTL_PSP |
  241. read_command(E1000_REG_TCTRL));
  242. }
  243. static void e1000_init(void * data, char * name) {
  244. debug_print(E1000_LOG_LEVEL, "enabling bus mastering");
  245. uint16_t command_reg = pci_read_field(e1000_device_pci, PCI_COMMAND, 2);
  246. command_reg |= (1 << 2);
  247. command_reg |= (1 << 0);
  248. pci_write_field(e1000_device_pci, PCI_COMMAND, 2, command_reg);
  249. debug_print(E1000_LOG_LEVEL, "mem base: 0x%x", mem_base);
  250. eeprom_detect();
  251. debug_print(E1000_LOG_LEVEL, "has_eeprom = %d", has_eeprom);
  252. read_mac();
  253. debug_print(E1000_LOG_LEVEL, "device mac %2x:%2x:%2x:%2x:%2x:%2x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  254. unsigned long s, ss;
  255. uint32_t ctrl = read_command(E1000_REG_CTRL);
  256. /* reset phy */
  257. write_command(E1000_REG_CTRL, ctrl | (0x80000000));
  258. read_command(E1000_REG_STATUS);
  259. relative_time(0, 10, &s, &ss);
  260. sleep_until((process_t *)current_process, s, ss);
  261. switch_task(0);
  262. /* reset mac */
  263. write_command(E1000_REG_CTRL, ctrl | (0x04000000));
  264. read_command(E1000_REG_STATUS);
  265. relative_time(0, 10, &s, &ss);
  266. sleep_until((process_t *)current_process, s, ss);
  267. switch_task(0);
  268. /* Reload EEPROM */
  269. write_command(E1000_REG_CTRL, ctrl | (0x00002000));
  270. read_command(E1000_REG_STATUS);
  271. relative_time(0, 20, &s, &ss);
  272. sleep_until((process_t *)current_process, s, ss);
  273. switch_task(0);
  274. /* initialize */
  275. write_command(E1000_REG_CTRL, ctrl | (1 << 26));
  276. /* wait */
  277. relative_time(0, 10, &s, &ss);
  278. sleep_until((process_t *)current_process, s, ss);
  279. switch_task(0);
  280. debug_print(E1000_LOG_LEVEL, "back from sleep");
  281. uint32_t status = read_command(E1000_REG_CTRL);
  282. status |= (1 << 5); /* set auto speed detection */
  283. status |= (1 << 6); /* set link up */
  284. status &= ~(1 << 3); /* unset link reset */
  285. status &= ~(1UL << 31UL); /* unset phy reset */
  286. status &= ~(1 << 7); /* unset invert loss-of-signal */
  287. write_command(E1000_REG_CTRL, status);
  288. /* Disables flow control */
  289. write_command(0x0028, 0);
  290. write_command(0x002c, 0);
  291. write_command(0x0030, 0);
  292. write_command(0x0170, 0);
  293. /* Unset flow control */
  294. status = read_command(E1000_REG_CTRL);
  295. status &= ~(1 << 30);
  296. write_command(E1000_REG_CTRL, status);
  297. relative_time(0, 10, &s, &ss);
  298. sleep_until((process_t *)current_process, s, ss);
  299. switch_task(0);
  300. net_queue = list_create();
  301. rx_wait = list_create();
  302. e1000_irq = pci_get_interrupt(e1000_device_pci);
  303. irq_install_handler(e1000_irq, irq_handler, "e1000");
  304. debug_print(E1000_LOG_LEVEL, "Binding interrupt %d", e1000_irq);
  305. for (int i = 0; i < 128; ++i) {
  306. write_command(0x5200 + i * 4, 0);
  307. }
  308. for (int i = 0; i < 64; ++i) {
  309. write_command(0x4000 + i * 4, 0);
  310. }
  311. #if 0
  312. /* This would rewrite the MAC address... */
  313. write_command(0x5400, *(uint32_t*)(&mac[0]));
  314. write_command(0x5404, *(uint16_t*)(&mac[4]));
  315. write_command(0x5404, read_command(0x5404) | (1 << 31));
  316. #endif
  317. write_command(E1000_REG_RCTRL, (1 << 4));
  318. init_rx();
  319. init_tx();
  320. /* Twiddle interrupts */
  321. write_command(0x00D0, 0xFF);
  322. write_command(0x00D8, 0xFF);
  323. write_command(0x00D0,(1 << 2) | (1 << 6) | (1 << 7) | (1 << 1) | (1 << 0));
  324. relative_time(0, 10, &s, &ss);
  325. sleep_until((process_t *)current_process, s, ss);
  326. switch_task(0);
  327. int link_is_up = (read_command(E1000_REG_STATUS) & (1 << 1));
  328. debug_print(E1000_LOG_LEVEL,"e1000 done. has_eeprom = %d, link is up = %d, irq=%d", has_eeprom, link_is_up, e1000_irq);
  329. init_netif_funcs(get_mac, dequeue_packet, send_packet, "Intel E1000");
  330. }
  331. static int init(void) {
  332. pci_scan(&find_e1000, -1, &e1000_device_pci);
  333. if (!e1000_device_pci) {
  334. debug_print(E1000_LOG_LEVEL, "No e1000 device found.");
  335. return 1;
  336. }
  337. /* This seems to always be memory mapped on important devices. */
  338. mem_base = pci_read_field(e1000_device_pci, PCI_BAR0, 4) & 0xFFFFFFF0;
  339. for (size_t x = 0; x < 0x10000; x += 0x1000) {
  340. uintptr_t addr = (mem_base & 0xFFFFF000) + x;
  341. dma_frame(get_page(addr, 1, kernel_directory), 1, 1, addr);
  342. }
  343. rx = (void*)kvmalloc_p(sizeof(struct rx_desc) * E1000_NUM_RX_DESC + 16, &rx_phys);
  344. for (int i = 0; i < E1000_NUM_RX_DESC; ++i) {
  345. rx_virt[i] = (void*)kvmalloc_p(8192 + 16, (uint32_t *)&rx[i].addr);
  346. debug_print(E1000_LOG_LEVEL, "rx[%d] 0x%x → 0x%x", i, rx_virt[i], (uint32_t)rx[i].addr);
  347. rx[i].status = 0;
  348. }
  349. tx = (void*)kvmalloc_p(sizeof(struct tx_desc) * E1000_NUM_TX_DESC + 16, &tx_phys);
  350. for (int i = 0; i < E1000_NUM_TX_DESC; ++i) {
  351. tx_virt[i] = (void*)kvmalloc_p(8192+16, (uint32_t *)&tx[i].addr);
  352. debug_print(E1000_LOG_LEVEL, "tx[%d] 0x%x → 0x%x", i, tx_virt[i], (uint32_t)tx[i].addr);
  353. tx[i].status = 0;
  354. tx[i].cmd = (1 << 0);
  355. }
  356. create_kernel_tasklet(e1000_init, "[e1000]", NULL);
  357. return 0;
  358. }
  359. static int fini(void) {
  360. return 0;
  361. }
  362. MODULE_DEF(e1000, init, fini);
  363. MODULE_DEPENDS(net);