Changes in uspace/drv/nic/e1k/e1k.c [321052f7:6d8455d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/e1k/e1k.c
r321052f7 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 error:1405 for (i = 0; i < E1000_RX_FRAME_COUNT; i++) {1406 if (e1000->rx_frame_virt[i] != NULL) {1407 dmamem_unmap_anonymous(e1000->rx_frame_virt[i]);1408 e1000->rx_frame_virt[i] = NULL;1409 e1000->rx_frame_phys[i] = NULL;1410 }1411 }1412 if (e1000->rx_frame_phys != NULL) {1413 free(e1000->rx_frame_phys);1414 e1000->rx_frame_phys = NULL;1415 }1416 if (e1000->rx_frame_virt != NULL) {1417 free(e1000->rx_frame_virt);1418 e1000->rx_frame_phys = NULL;1419 }1420 return rc;1421 1398 } 1422 1399 … … 1430 1407 e1000_t *e1000 = DRIVER_DATA_NIC(nic); 1431 1408 1432 /* Write descriptor */1433 for (unsigned int offset = 0; offset < E1000_RX_FRAME_COUNT; offset++) {1434 dmamem_unmap_anonymous(e1000->rx_frame_virt[offset]);1435 e1000->rx_frame_virt[offset] = NULL;1436 e1000->rx_frame_phys[offset] = NULL;1437 }1438 1439 free(e1000->rx_frame_virt);1440 free(e1000->rx_frame_phys);1441 e1000->rx_frame_virt = NULL;1442 e1000->rx_frame_phys = NULL;1443 dmamem_unmap_anonymous(e1000->rx_ring_virt);1444 }1445 1446 /** Clear receive descriptor ring1447 *1448 * @param e1000 E1000 data1449 *1450 */1451 static void e1000_clear_rx_ring(e1000_t *e1000)1452 {1453 1409 /* Write descriptor */ 1454 1410 for (unsigned int offset = 0; 1455 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; 1456 1432 offset++) 1457 1433 e1000_clear_rx_descriptor(e1000, offset); … … 1522 1498 static void e1000_initialize_tx_registers(e1000_t *e1000) 1523 1499 { 1524 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_ FRAME_COUNT * 16);1500 E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16); 1525 1501 E1000_REG_WRITE(e1000, E1000_TDH, 0); 1526 1502 E1000_REG_WRITE(e1000, E1000_TDT, 0); … … 1554 1530 1555 1531 int rc = dmamem_map_anonymous( 1556 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t),1532 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t), 1557 1533 AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys, 1558 1534 &e1000->tx_ring_virt); … … 1561 1537 1562 1538 bzero(e1000->tx_ring_virt, 1563 E1000_TX_ FRAME_COUNT * sizeof(e1000_tx_descriptor_t));1564 1565 e1000->tx_frame_phys = calloc(E1000_TX_ FRAME_COUNT, sizeof(void *));1566 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 *)); 1567 1543 1568 1544 if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) { … … 1571 1547 } 1572 1548 1573 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1549 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1574 1550 rc = dmamem_map_anonymous( 1575 1551 E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE, … … 1596 1572 1597 1573 if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) { 1598 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1574 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1599 1575 if (e1000->tx_frame_virt[i] != NULL) { 1600 1576 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); … … 1627 1603 size_t i; 1628 1604 1629 for (i = 0; i < E1000_TX_ FRAME_COUNT; i++) {1605 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) { 1630 1606 dmamem_unmap_anonymous(e1000->tx_frame_virt[i]); 1631 1607 e1000->tx_frame_virt[i] = NULL; … … 1654 1630 /* Write descriptor */ 1655 1631 for (unsigned int offset = 0; 1656 offset < E1000_TX_ FRAME_COUNT;1632 offset < E1000_TX_PACKETS_COUNT; 1657 1633 offset++) 1658 1634 e1000_clear_tx_descriptor(nic, offset); … … 1711 1687 } 1712 1688 1713 /** Activate the device to receive and transmit frames1689 /** Activate the device to receive and transmit packets 1714 1690 * 1715 1691 * @param nic NIC driver data … … 2307 2283 2308 2284 if (!descriptor_available) { 2309 /* Framelost */2285 /* Packet lost */ 2310 2286 fibril_mutex_unlock(&e1000->tx_lock); 2311 2287 return; … … 2336 2312 2337 2313 tdt++; 2338 if (tdt == E1000_TX_ FRAME_COUNT)2314 if (tdt == E1000_TX_PACKETS_COUNT) 2339 2315 tdt = 0; 2340 2316
Note:
See TracChangeset
for help on using the changeset viewer.