Changes in uspace/drv/nic/e1k/e1k.c [e882e3a:ec52752] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/e1k/e1k.c
re882e3a rec52752 46 46 #include <ddf/log.h> 47 47 #include <ddf/interrupt.h> 48 #include <devman.h> 48 49 #include <device/hw_res_parsed.h> 49 50 #include <device/pci.h> … … 51 52 #include <nil_remote.h> 52 53 #include <ops/nic.h> 54 #include <packet_client.h> 55 #include <packet_remote.h> 56 #include <net/packet_header.h> 53 57 #include "e1k.h" 54 58 … … 58 62 59 63 /* Must be power of 8 */ 60 #define E1000_RX_ FRAME_COUNT 12861 #define E1000_TX_ FRAME_COUNT 12864 #define E1000_RX_PACKETS_COUNT 128 65 #define E1000_TX_PACKETS_COUNT 128 62 66 63 67 #define E1000_RECEIVE_ADDRESS 16 64 68 65 /** Maximum sending framesize */69 /** Maximum sending packet size */ 66 70 #define E1000_MAX_SEND_FRAME_SIZE 2048 67 /** Maximum receiving framesize */68 #define E1000_MAX_RECEIVE_ FRAME_SIZE 204871 /** Maximum receiving packet size */ 72 #define E1000_MAX_RECEIVE_PACKET_SIZE 2048 69 73 70 74 /** nic_driver_data_t* -> e1000_t* cast */ … … 133 137 void *rx_ring_virt; 134 138 135 /** Ring of RX frames, physical address */ 136 void **rx_frame_phys; 137 /** Ring of RX frames, virtual address */ 138 void **rx_frame_virt; 139 /** Packets in rx ring */ 140 packet_t **rx_ring_packets; 139 141 140 142 /** VLAN tag */ 141 143 uint16_t vlan_tag; 142 144 143 /** Add VLAN tag to frame*/145 /** Add VLAN tag to packet */ 144 146 bool vlan_tag_add; 145 147 … … 486 488 } 487 489 488 /** Get state of acceptance of weird frames490 /** Get state of acceptance of weird packets 489 491 * 490 492 * @param device Device to check … … 504 506 }; 505 507 506 /** Set acceptance of weird frames508 /** Set acceptance of weird packets 507 509 * 508 510 * @param device Device to update … … 688 690 } 689 691 690 /** Disable receiving frames for default address692 /** Disable receiving packets for default address 691 693 * 692 694 * @param e1000 E1000 data structure … … 700 702 } 701 703 702 /** Enable receiving frames for default address704 /** Enable receiving packets for default address 703 705 * 704 706 * @param e1000 E1000 data structure … … 760 762 } 761 763 762 /** Enable accepting of broadcast frames764 /** Enable accepting of broadcast packets 763 765 * 764 766 * @param e1000 E1000 data structure … … 772 774 } 773 775 774 /** Disable accepting of broadcast frames776 /** Disable accepting of broadcast packets 775 777 * 776 778 * @param e1000 E1000 data structure … … 808 810 } 809 811 810 /** Set multicast frames acceptance mode812 /** Set multicast packets acceptance mode 811 813 * 812 814 * @param nic NIC device to update … … 862 864 } 863 865 864 /** Set unicast frames acceptance mode866 /** Set unicast packets acceptance mode 865 867 * 866 868 * @param nic NIC device to update … … 920 922 } 921 923 922 /** Set broadcast frames acceptance mode924 /** Set broadcast packets acceptance mode 923 925 * 924 926 * @param nic NIC device to update … … 1005 1007 if (vlan_mask) { 1006 1008 /* 1007 * Disable receiving, so that framematching1009 * Disable receiving, so that packet matching 1008 1010 * partially written VLAN is not received. 1009 1011 */ … … 1072 1074 } 1073 1075 1074 /** Fill receive descriptor with new empty buffer1075 * 1076 * Store frame in e1000->rx_frame_phys1076 /** Fill receive descriptor with new empty packet 1077 * 1078 * Store packet in e1000->rx_ring_packets 1077 1079 * 1078 1080 * @param nic NIC data stricture … … 1083 1085 { 1084 1086 e1000_t *e1000 = DRIVER_DATA_NIC(nic); 1085 1087 packet_t *packet = 1088 nic_alloc_packet(nic, E1000_MAX_RECEIVE_PACKET_SIZE); 1089 1090 assert(packet); 1091 1092 *(e1000->rx_ring_packets + offset) = packet; 1086 1093 e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *) 1087 1094 (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t)); 1088 1095 1089 rx_descriptor->phys_addr = PTR_TO_U64(e1000->rx_frame_phys[offset]); 1096 void *phys; 1097 int rc = 1098 nic_dma_lock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE, &phys); 1099 1100 if (rc == EOK) 1101 rx_descriptor->phys_addr = PTR_TO_U64(phys + packet->data_start); 1102 else 1103 rx_descriptor->phys_addr = 0; 1104 1090 1105 rx_descriptor->length = 0; 1091 1106 rx_descriptor->checksum = 0; … … 1151 1166 } 1152 1167 1153 /** Receive frames1168 /** Receive packets 1154 1169 * 1155 1170 * @param nic NIC data 1156 1171 * 1157 1172 */ 1158 static void e1000_receive_ frames(nic_t *nic)1173 static void e1000_receive_packets(nic_t *nic) 1159 1174 { 1160 1175 e1000_t *e1000 = DRIVER_DATA_NIC(nic); … … 1163 1178 1164 1179 uint32_t *tail_addr = E1000_REG_ADDR(e1000, E1000_RDT); 1165 uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1180 uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1166 1181 1167 1182 e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *) … … 1169 1184 1170 1185 while (rx_descriptor->status & 0x01) { 1171 uint32_t frame_size = rx_descriptor->length - E1000_CRC_SIZE;1186 uint32_t packet_size = rx_descriptor->length - E1000_CRC_SIZE; 1172 1187 1173 nic_frame_t *frame = nic_alloc_frame(nic, frame_size); 1174 if (frame != NULL) { 1175 memcpy(frame->data, e1000->rx_frame_virt[next_tail], frame_size); 1176 nic_received_frame(nic, frame); 1177 } else { 1178 ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped."); 1179 } 1188 packet_t *packet = *(e1000->rx_ring_packets + next_tail); 1189 packet_suffix(packet, packet_size); 1190 1191 nic_dma_unlock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE); 1192 nic_received_packet(nic, packet); 1180 1193 1181 1194 e1000_fill_new_rx_descriptor(nic, next_tail); 1182 1195 1183 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1184 next_tail = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1196 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1197 next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1185 1198 1186 1199 rx_descriptor = (e1000_rx_descriptor_t *) … … 1223 1236 { 1224 1237 if (icr & ICR_RXT0) 1225 e1000_receive_ frames(nic);1238 e1000_receive_packets(nic); 1226 1239 } 1227 1240 … … 1273 1286 } 1274 1287 1275 /** Force receiving all frames in the receive buffer1288 /** Force receiving all packets in the receive buffer 1276 1289 * 1277 1290 * @param nic NIC data … … 1346 1359 static void e1000_initialize_rx_registers(e1000_t *e1000) 1347 1360 { 1348 E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_ FRAME_COUNT * 16);1361 E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_PACKETS_COUNT * 16); 1349 1362 E1000_REG_WRITE(e1000, E1000_RDH, 0); 1350 1363 1351 1364 /* It is not posible to let HW use all descriptors */ 1352 E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_ FRAME_COUNT - 1);1365 E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_PACKETS_COUNT - 1); 1353 1366 1354 1367 /* Set Broadcast Enable Bit */ … … 1370 1383 1371 1384 int rc = dmamem_map_anonymous( 1372 E1000_RX_ FRAME_COUNT * sizeof(e1000_rx_descriptor_t),1385 E1000_RX_PACKETS_COUNT * sizeof(e1000_rx_descriptor_t), 1373 1386 AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->rx_ring_phys, 1374 1387 &e1000->rx_ring_virt); … … 1381 1394 (uint32_t) PTR_TO_U64(e1000->rx_ring_phys)); 1382 1395 1383 e1000->rx_frame_phys = 1384 calloc(E1000_RX_FRAME_COUNT, sizeof(void *)); 1385 e1000->rx_frame_virt = 1386 calloc(E1000_RX_FRAME_COUNT, sizeof(void *)); 1387 if (e1000->rx_frame_phys == NULL || e1000->rx_frame_virt == NULL) { 1388 rc = ENOMEM; 1389 goto error; 1390 } 1391 1392 size_t i; 1393 void *frame_virt; 1394 void *frame_phys; 1395 1396 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) { 1397 rc = dmamem_map_anonymous( 1398 E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE, 1399 0, &frame_phys, &frame_virt); 1400 if (rc != EOK) 1401 goto error; 1402 1403 e1000->rx_frame_virt[i] = frame_virt; 1404 e1000->rx_frame_phys[i] = frame_phys; 1405 } 1396 e1000->rx_ring_packets = 1397 malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *)); 1398 // FIXME: Check return value 1406 1399 1407 1400 /* Write descriptor */ 1408 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) 1409 e1000_fill_new_rx_descriptor(nic, i); 1401 for (unsigned int offset = 0; 1402 offset < E1000_RX_PACKETS_COUNT; 1403 offset++) 1404 e1000_fill_new_rx_descriptor(nic, offset); 1410 1405 1411 1406 e1000_initialize_rx_registers(e1000); … … 1413 1408 fibril_mutex_unlock(&e1000->rx_lock); 1414 1409 return EOK; 1415 1416 error:1417 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) {1418 if (e1000->rx_frame_virt[i] != NULL) {1419 dmamem_unmap_anonymous(e1000->rx_frame_virt[i]);1420 e1000->rx_frame_virt[i] = NULL;1421 e1000->rx_frame_phys[i] = NULL;1422 }1423 }1424 1425 if (e1000->rx_frame_phys != NULL) {1426 free(e1000->rx_frame_phys);1427 e1000->rx_frame_phys = NULL;1428 }1429 1430 if (e1000->rx_frame_virt != NULL) {1431 free(e1000->rx_frame_virt);1432 e1000->rx_frame_phys = NULL;1433 }1434 1435 return rc;1436 1410 } 1437 1411 … … 1445 1419 e1000_t *e1000 = DRIVER_DATA_NIC(nic); 1446 1420 1447 /* Write descriptor */1448 for (unsigned int offset = 0; offset < E1000_RX_FRAME_COUNT; offset++) {1449 dmamem_unmap_anonymous(e1000->rx_frame_virt[offset]);1450 e1000->rx_frame_virt[offset] = NULL;1451 e1000->rx_frame_phys[offset] = NULL;1452 }1453 1454 free(e1000->rx_frame_virt);1455 free(e1000->rx_frame_phys);1456 e1000->rx_frame_virt = NULL;1457 e1000->rx_frame_phys = NULL;1458 dmamem_unmap_anonymous(e1000->rx_ring_virt);1459 }1460 1461 /** Clear receive descriptor ring1462 *1463 * @param e1000 E1000 data1464 *1465 */1466 static void e1000_clear_rx_ring(e1000_t *e1000)1467 {1468 1421 /* Write descriptor */ 1469 1422 for (unsigned int offset = 0; 1470 offset < E1000_RX_FRAME_COUNT; 1423 offset < E1000_RX_PACKETS_COUNT; 1424 offset++) { 1425 packet_t *packet = *(e1000->rx_ring_packets + offset); 1426 nic_dma_unlock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE); 1427 nic_release_packet(nic, packet); 1428 } 1429 1430 free(e1000->rx_ring_packets); 1431 dmamem_unmap_anonymous(e1000->rx_ring_virt); 1432 } 1433 1434 /** Clear receive descriptor ring 1435 * 1436 * @param e1000 E1000 data 1437 * 1438 */ 1439 static void e1000_clear_rx_ring(e1000_t *e1000) 1440 { 1441 /* Write descriptor */ 1442 for (unsigned int offset = 0; 1443 offset < E1000_RX_PACKETS_COUNT; 1471 1444 offset++) 1472 1445 e1000_clear_rx_descriptor(e1000, offset); … … 1537 1510 static void e1000_initialize_tx_registers(e1000_t *e1000) 1538 1511 { 1539 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_ FRAME_COUNT * 16);1512 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16); 1540 1513 E1000_REG_WRITE(e1000, E1000_TDH, 0); 1541 1514 E1000_REG_WRITE(e1000, E1000_TDT, 0); … … 1569 1542 1570 1543 int rc = dmamem_map_anonymous( 1571 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t),1544 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t), 1572 1545 AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys, 1573 1546 &e1000->tx_ring_virt); … … 1576 1549 1577 1550 bzero(e1000->tx_ring_virt, 1578 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t));1579 1580 e1000->tx_frame_phys = calloc(E1000_TX_ FRAME_COUNT, sizeof(void *));1581 e1000->tx_frame_virt = calloc(E1000_TX_ FRAME_COUNT, sizeof(void *));1551 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t)); 1552 1553 e1000->tx_frame_phys = calloc(E1000_TX_PACKETS_COUNT, sizeof(void *)); 1554 e1000->tx_frame_virt = calloc(E1000_TX_PACKETS_COUNT, sizeof(void *)); 1582 1555 1583 1556 if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) { … … 1586 1559 } 1587 1560 1588 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1561 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1589 1562 rc = dmamem_map_anonymous( 1590 1563 E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE, … … 1611 1584 1612 1585 if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) { 1613 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1586 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1614 1587 if (e1000->tx_frame_virt[i] != NULL) { 1615 1588 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); … … 1642 1615 size_t i; 1643 1616 1644 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1617 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1645 1618 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); 1646 1619 e1000->tx_frame_virt[i] = NULL; … … 1657 1630 e1000->tx_frame_phys = NULL; 1658 1631 } 1659 1660 1632 dmamem_unmap_anonymous(e1000->tx_ring_virt); 1661 1633 } … … 1670 1642 /* Write descriptor */ 1671 1643 for (unsigned int offset = 0; 1672 offset < E1000_TX_ FRAME_COUNT;1644 offset < E1000_TX_PACKETS_COUNT; 1673 1645 offset++) 1674 1646 e1000_clear_tx_descriptor(nic, offset); … … 1727 1699 } 1728 1700 1729 /** Activate the device to receive and transmit frames1701 /** Activate the device to receive and transmit packets 1730 1702 * 1731 1703 * @param nic NIC driver data … … 2068 2040 case E1000_82545: 2069 2041 case E1000_82546: 2042 case E1000_82572: 2070 2043 e1000->info.eerd_start = 0x01; 2071 2044 e1000->info.eerd_done = 0x10; … … 2074 2047 break; 2075 2048 case E1000_82547: 2076 case E1000_82572:2077 2049 case E1000_80003ES2: 2078 2050 e1000->info.eerd_start = 0x01; … … 2113 2085 int e1000_dev_add(ddf_dev_t *dev) 2114 2086 { 2115 ddf_fun_t *fun;2116 2087 assert(dev); 2117 2088 … … 2144 2115 e1000_initialize_vlan(e1000); 2145 2116 2146 fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");2147 if ( fun == NULL)2117 rc = nic_register_as_ddf_fun(nic, &e1000_dev_ops); 2118 if (rc != EOK) 2148 2119 goto err_tx_structure; 2149 nic_set_ddf_fun(nic, fun);2150 fun->ops = &e1000_dev_ops;2151 fun->driver_data = nic;2152 2120 2153 2121 rc = e1000_register_int_handler(nic); 2154 2122 if (rc != EOK) 2155 goto err_ fun_create;2123 goto err_tx_structure; 2156 2124 2157 2125 rc = nic_connect_to_services(nic); … … 2176 2144 goto err_rx_structure; 2177 2145 2178 rc = ddf_fun_bind(fun);2179 if (rc != EOK)2180 goto err_fun_bind;2181 2182 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);2183 if (rc != EOK)2184 goto err_add_to_cat;2185 2186 2146 return EOK; 2187 2147 2188 err_add_to_cat:2189 ddf_fun_unbind(fun);2190 err_fun_bind:2191 2148 err_rx_structure: 2192 2149 e1000_uninitialize_rx_structure(nic); 2193 2150 err_irq: 2194 2151 unregister_interrupt_handler(dev, DRIVER_DATA_DEV(dev)->irq); 2195 err_fun_create:2196 ddf_fun_destroy(fun);2197 nic_set_ddf_fun(nic, NULL);2198 2152 err_tx_structure: 2199 2153 e1000_uninitialize_tx_structure(e1000); … … 2341 2295 2342 2296 if (!descriptor_available) { 2343 /* Framelost */2297 /* Packet lost */ 2344 2298 fibril_mutex_unlock(&e1000->tx_lock); 2345 2299 return; … … 2370 2324 2371 2325 tdt++; 2372 if (tdt == E1000_TX_ FRAME_COUNT)2326 if (tdt == E1000_TX_PACKETS_COUNT) 2373 2327 tdt = 0; 2374 2328
Note:
See TracChangeset
for help on using the changeset viewer.