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