Changes in uspace/drv/nic/e1k/e1k.c [d81eaf94:6d8455d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/e1k/e1k.c
rd81eaf94 r6d8455d 52 52 #include <nil_remote.h> 53 53 #include <ops/nic.h> 54 #include <packet_client.h> 55 #include <packet_remote.h> 56 #include <net/packet_header.h> 54 57 #include "e1k.h" 55 58 … … 59 62 60 63 /* Must be power of 8 */ 61 #define E1000_RX_ FRAME_COUNT 12862 #define E1000_TX_ FRAME_COUNT 12864 #define E1000_RX_PACKETS_COUNT 128 65 #define E1000_TX_PACKETS_COUNT 128 63 66 64 67 #define E1000_RECEIVE_ADDRESS 16 65 68 66 /** Maximum sending framesize */69 /** Maximum sending packet size */ 67 70 #define E1000_MAX_SEND_FRAME_SIZE 2048 68 /** Maximum receiving framesize */69 #define E1000_MAX_RECEIVE_ FRAME_SIZE 204871 /** Maximum receiving packet size */ 72 #define E1000_MAX_RECEIVE_PACKET_SIZE 2048 70 73 71 74 /** nic_driver_data_t* -> e1000_t* cast */ … … 134 137 void *rx_ring_virt; 135 138 136 /** Ring of RX frames, physical address */ 137 void **rx_frame_phys; 138 /** Ring of RX frames, virtual address */ 139 void **rx_frame_virt; 139 /** Packets in rx ring */ 140 packet_t **rx_ring_packets; 140 141 141 142 /** VLAN tag */ 142 143 uint16_t vlan_tag; 143 144 144 /** Add VLAN tag to frame*/145 /** Add VLAN tag to packet */ 145 146 bool vlan_tag_add; 146 147 … … 476 477 } 477 478 478 /** Get state of acceptance of weird frames479 /** Get state of acceptance of weird packets 479 480 * 480 481 * @param device Device to check … … 494 495 }; 495 496 496 /** Set acceptance of weird frames497 /** Set acceptance of weird packets 497 498 * 498 499 * @param device Device to update … … 678 679 } 679 680 680 /** Disable receiving frames for default address681 /** Disable receiving packets for default address 681 682 * 682 683 * @param e1000 E1000 data structure … … 690 691 } 691 692 692 /** Enable receiving frames for default address693 /** Enable receiving packets for default address 693 694 * 694 695 * @param e1000 E1000 data structure … … 750 751 } 751 752 752 /** Enable accepting of broadcast frames753 /** Enable accepting of broadcast packets 753 754 * 754 755 * @param e1000 E1000 data structure … … 762 763 } 763 764 764 /** Disable accepting of broadcast frames765 /** Disable accepting of broadcast packets 765 766 * 766 767 * @param e1000 E1000 data structure … … 798 799 } 799 800 800 /** Set multicast frames acceptance mode801 /** Set multicast packets acceptance mode 801 802 * 802 803 * @param nic NIC device to update … … 852 853 } 853 854 854 /** Set unicast frames acceptance mode855 /** Set unicast packets acceptance mode 855 856 * 856 857 * @param nic NIC device to update … … 910 911 } 911 912 912 /** Set broadcast frames acceptance mode913 /** Set broadcast packets acceptance mode 913 914 * 914 915 * @param nic NIC device to update … … 995 996 if (vlan_mask) { 996 997 /* 997 * Disable receiving, so that framematching998 * Disable receiving, so that packet matching 998 999 * partially written VLAN is not received. 999 1000 */ … … 1062 1063 } 1063 1064 1064 /** Fill receive descriptor with new empty buffer1065 * 1066 * Store frame in e1000->rx_frame_phys1065 /** Fill receive descriptor with new empty packet 1066 * 1067 * Store packet in e1000->rx_ring_packets 1067 1068 * 1068 1069 * @param nic NIC data stricture … … 1073 1074 { 1074 1075 e1000_t *e1000 = DRIVER_DATA_NIC(nic); 1075 1076 packet_t *packet = 1077 nic_alloc_packet(nic, E1000_MAX_RECEIVE_PACKET_SIZE); 1078 1079 assert(packet); 1080 1081 *(e1000->rx_ring_packets + offset) = packet; 1076 1082 e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *) 1077 1083 (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t)); 1078 1084 1079 rx_descriptor->phys_addr = PTR_TO_U64(e1000->rx_frame_phys[offset]); 1085 void *phys; 1086 int rc = 1087 nic_dma_lock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE, &phys); 1088 1089 if (rc == EOK) 1090 rx_descriptor->phys_addr = PTR_TO_U64(phys + packet->data_start); 1091 else 1092 rx_descriptor->phys_addr = 0; 1093 1080 1094 rx_descriptor->length = 0; 1081 1095 rx_descriptor->checksum = 0; … … 1141 1155 } 1142 1156 1143 /** Receive frames1157 /** Receive packets 1144 1158 * 1145 1159 * @param nic NIC data 1146 1160 * 1147 1161 */ 1148 static void e1000_receive_ frames(nic_t *nic)1162 static void e1000_receive_packets(nic_t *nic) 1149 1163 { 1150 1164 e1000_t *e1000 = DRIVER_DATA_NIC(nic); … … 1153 1167 1154 1168 uint32_t *tail_addr = E1000_REG_ADDR(e1000, E1000_RDT); 1155 uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1169 uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1156 1170 1157 1171 e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *) … … 1159 1173 1160 1174 while (rx_descriptor->status & 0x01) { 1161 uint32_t frame_size = rx_descriptor->length - E1000_CRC_SIZE;1175 uint32_t packet_size = rx_descriptor->length - E1000_CRC_SIZE; 1162 1176 1163 nic_frame_t *frame = nic_alloc_frame(nic, frame_size); 1164 if (frame != NULL) { 1165 memcpy(frame->data, e1000->rx_frame_virt[next_tail], frame_size); 1166 nic_received_frame(nic, frame); 1167 } else { 1168 ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped."); 1169 } 1177 packet_t *packet = *(e1000->rx_ring_packets + next_tail); 1178 packet_suffix(packet, packet_size); 1179 1180 nic_dma_unlock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE); 1181 nic_received_packet(nic, packet); 1170 1182 1171 1183 e1000_fill_new_rx_descriptor(nic, next_tail); 1172 1184 1173 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1174 next_tail = e1000_inc_tail(*tail_addr, E1000_RX_ FRAME_COUNT);1185 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1186 next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT); 1175 1187 1176 1188 rx_descriptor = (e1000_rx_descriptor_t *) … … 1213 1225 { 1214 1226 if (icr & ICR_RXT0) 1215 e1000_receive_ frames(nic);1227 e1000_receive_packets(nic); 1216 1228 } 1217 1229 … … 1262 1274 } 1263 1275 1264 /** Force receiving all frames in the receive buffer1276 /** Force receiving all packets in the receive buffer 1265 1277 * 1266 1278 * @param nic NIC data … … 1335 1347 static void e1000_initialize_rx_registers(e1000_t *e1000) 1336 1348 { 1337 E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_ FRAME_COUNT * 16);1349 E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_PACKETS_COUNT * 16); 1338 1350 E1000_REG_WRITE(e1000, E1000_RDH, 0); 1339 1351 1340 1352 /* It is not posible to let HW use all descriptors */ 1341 E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_ FRAME_COUNT - 1);1353 E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_PACKETS_COUNT - 1); 1342 1354 1343 1355 /* Set Broadcast Enable Bit */ … … 1359 1371 1360 1372 int rc = dmamem_map_anonymous( 1361 E1000_RX_ FRAME_COUNT * sizeof(e1000_rx_descriptor_t),1373 E1000_RX_PACKETS_COUNT * sizeof(e1000_rx_descriptor_t), 1362 1374 AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->rx_ring_phys, 1363 1375 &e1000->rx_ring_virt); … … 1370 1382 (uint32_t) PTR_TO_U64(e1000->rx_ring_phys)); 1371 1383 1372 e1000->rx_frame_phys = 1373 calloc(E1000_RX_FRAME_COUNT, sizeof(void *)); 1374 e1000->rx_frame_virt = 1375 calloc(E1000_RX_FRAME_COUNT, sizeof(void *)); 1376 if (e1000->rx_frame_phys == NULL || e1000->rx_frame_virt == NULL) { 1377 rc = ENOMEM; 1378 goto error; 1379 } 1380 1381 size_t i; 1382 void *frame_virt; 1383 void *frame_phys; 1384 1385 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) { 1386 rc = dmamem_map_anonymous( 1387 E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE, 1388 0, &frame_phys, &frame_virt); 1389 if (rc != EOK) 1390 goto error; 1391 1392 e1000->rx_frame_virt[i] = frame_virt; 1393 e1000->rx_frame_phys[i] = frame_phys; 1394 } 1384 e1000->rx_ring_packets = 1385 malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *)); 1386 // FIXME: Check return value 1395 1387 1396 1388 /* Write descriptor */ 1397 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) 1398 e1000_fill_new_rx_descriptor(nic, i); 1389 for (unsigned int offset = 0; 1390 offset < E1000_RX_PACKETS_COUNT; 1391 offset++) 1392 e1000_fill_new_rx_descriptor(nic, offset); 1399 1393 1400 1394 e1000_initialize_rx_registers(e1000); … … 1402 1396 fibril_mutex_unlock(&e1000->rx_lock); 1403 1397 return EOK; 1404 1405 error:1406 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) {1407 if (e1000->rx_frame_virt[i] != NULL) {1408 dmamem_unmap_anonymous(e1000->rx_frame_virt[i]);1409 e1000->rx_frame_virt[i] = NULL;1410 e1000->rx_frame_phys[i] = NULL;1411 }1412 }1413 1414 if (e1000->rx_frame_phys != NULL) {1415 free(e1000->rx_frame_phys);1416 e1000->rx_frame_phys = NULL;1417 }1418 1419 if (e1000->rx_frame_virt != NULL) {1420 free(e1000->rx_frame_virt);1421 e1000->rx_frame_phys = NULL;1422 }1423 1424 return rc;1425 1398 } 1426 1399 … … 1434 1407 e1000_t *e1000 = DRIVER_DATA_NIC(nic); 1435 1408 1436 /* Write descriptor */1437 for (unsigned int offset = 0; offset < E1000_RX_FRAME_COUNT; offset++) {1438 dmamem_unmap_anonymous(e1000->rx_frame_virt[offset]);1439 e1000->rx_frame_virt[offset] = NULL;1440 e1000->rx_frame_phys[offset] = NULL;1441 }1442 1443 free(e1000->rx_frame_virt);1444 free(e1000->rx_frame_phys);1445 e1000->rx_frame_virt = NULL;1446 e1000->rx_frame_phys = NULL;1447 dmamem_unmap_anonymous(e1000->rx_ring_virt);1448 }1449 1450 /** Clear receive descriptor ring1451 *1452 * @param e1000 E1000 data1453 *1454 */1455 static void e1000_clear_rx_ring(e1000_t *e1000)1456 {1457 1409 /* Write descriptor */ 1458 1410 for (unsigned int offset = 0; 1459 offset < E1000_RX_FRAME_COUNT; 1411 offset < E1000_RX_PACKETS_COUNT; 1412 offset++) { 1413 packet_t *packet = *(e1000->rx_ring_packets + offset); 1414 nic_dma_unlock_packet(packet, E1000_MAX_RECEIVE_PACKET_SIZE); 1415 nic_release_packet(nic, packet); 1416 } 1417 1418 free(e1000->rx_ring_packets); 1419 dmamem_unmap_anonymous(e1000->rx_ring_virt); 1420 } 1421 1422 /** Clear receive descriptor ring 1423 * 1424 * @param e1000 E1000 data 1425 * 1426 */ 1427 static void e1000_clear_rx_ring(e1000_t *e1000) 1428 { 1429 /* Write descriptor */ 1430 for (unsigned int offset = 0; 1431 offset < E1000_RX_PACKETS_COUNT; 1460 1432 offset++) 1461 1433 e1000_clear_rx_descriptor(e1000, offset); … … 1526 1498 static void e1000_initialize_tx_registers(e1000_t *e1000) 1527 1499 { 1528 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_ FRAME_COUNT * 16);1500 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16); 1529 1501 E1000_REG_WRITE(e1000, E1000_TDH, 0); 1530 1502 E1000_REG_WRITE(e1000, E1000_TDT, 0); … … 1558 1530 1559 1531 int rc = dmamem_map_anonymous( 1560 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t),1532 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t), 1561 1533 AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys, 1562 1534 &e1000->tx_ring_virt); … … 1565 1537 1566 1538 bzero(e1000->tx_ring_virt, 1567 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t));1568 1569 e1000->tx_frame_phys = calloc(E1000_TX_ FRAME_COUNT, sizeof(void *));1570 e1000->tx_frame_virt = calloc(E1000_TX_ FRAME_COUNT, sizeof(void *));1539 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t)); 1540 1541 e1000->tx_frame_phys = calloc(E1000_TX_PACKETS_COUNT, sizeof(void *)); 1542 e1000->tx_frame_virt = calloc(E1000_TX_PACKETS_COUNT, sizeof(void *)); 1571 1543 1572 1544 if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) { … … 1575 1547 } 1576 1548 1577 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1549 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1578 1550 rc = dmamem_map_anonymous( 1579 1551 E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE, … … 1600 1572 1601 1573 if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) { 1602 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1574 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1603 1575 if (e1000->tx_frame_virt[i] != NULL) { 1604 1576 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); … … 1631 1603 size_t i; 1632 1604 1633 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1605 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1634 1606 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); 1635 1607 e1000->tx_frame_virt[i] = NULL; … … 1646 1618 e1000->tx_frame_phys = NULL; 1647 1619 } 1648 1649 1620 dmamem_unmap_anonymous(e1000->tx_ring_virt); 1650 1621 } … … 1659 1630 /* Write descriptor */ 1660 1631 for (unsigned int offset = 0; 1661 offset < E1000_TX_ FRAME_COUNT;1632 offset < E1000_TX_PACKETS_COUNT; 1662 1633 offset++) 1663 1634 e1000_clear_tx_descriptor(nic, offset); … … 1716 1687 } 1717 1688 1718 /** Activate the device to receive and transmit frames1689 /** Activate the device to receive and transmit packets 1719 1690 * 1720 1691 * @param nic NIC driver data … … 2057 2028 case E1000_82545: 2058 2029 case E1000_82546: 2030 case E1000_82572: 2059 2031 e1000->info.eerd_start = 0x01; 2060 2032 e1000->info.eerd_done = 0x10; … … 2063 2035 break; 2064 2036 case E1000_82547: 2065 case E1000_82572:2066 2037 case E1000_80003ES2: 2067 2038 e1000->info.eerd_start = 0x01; … … 2102 2073 int e1000_dev_add(ddf_dev_t *dev) 2103 2074 { 2104 ddf_fun_t *fun;2105 2075 assert(dev); 2106 2076 … … 2133 2103 e1000_initialize_vlan(e1000); 2134 2104 2135 fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");2136 if ( fun == NULL)2105 rc = nic_register_as_ddf_fun(nic, &e1000_dev_ops); 2106 if (rc != EOK) 2137 2107 goto err_tx_structure; 2138 nic_set_ddf_fun(nic, fun);2139 fun->ops = &e1000_dev_ops;2140 fun->driver_data = nic;2141 2108 2142 2109 rc = e1000_register_int_handler(nic); 2143 2110 if (rc != EOK) 2144 goto err_ fun_create;2111 goto err_tx_structure; 2145 2112 2146 2113 rc = nic_connect_to_services(nic); … … 2165 2132 goto err_rx_structure; 2166 2133 2167 rc = ddf_fun_bind(fun);2168 if (rc != EOK)2169 goto err_fun_bind;2170 2171 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);2172 if (rc != EOK)2173 goto err_add_to_cat;2174 2175 2134 return EOK; 2176 2135 2177 err_add_to_cat:2178 ddf_fun_unbind(fun);2179 err_fun_bind:2180 2136 err_rx_structure: 2181 2137 e1000_uninitialize_rx_structure(nic); 2182 2138 err_irq: 2183 2139 unregister_interrupt_handler(dev, DRIVER_DATA_DEV(dev)->irq); 2184 err_fun_create:2185 ddf_fun_destroy(fun);2186 nic_set_ddf_fun(nic, NULL);2187 2140 err_tx_structure: 2188 2141 e1000_uninitialize_tx_structure(e1000); … … 2330 2283 2331 2284 if (!descriptor_available) { 2332 /* Framelost */2285 /* Packet lost */ 2333 2286 fibril_mutex_unlock(&e1000->tx_lock); 2334 2287 return; … … 2359 2312 2360 2313 tdt++; 2361 if (tdt == E1000_TX_ FRAME_COUNT)2314 if (tdt == E1000_TX_PACKETS_COUNT) 2362 2315 tdt = 0; 2363 2316
Note:
See TracChangeset
for help on using the changeset viewer.