Changeset 1bc35b5 in mainline for uspace/drv/nic/e1k/e1k.c


Ignore:
Timestamp:
2012-01-19T08:13:45Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d8da56b
Parents:
3ea725e
Message:

Remove most use of packet_t from NIC drivers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/nic/e1k/e1k.c

    r3ea725e r1bc35b5  
    5252#include <nil_remote.h>
    5353#include <ops/nic.h>
    54 #include <packet_client.h>
    55 #include <packet_remote.h>
    56 #include <net/packet_header.h>
    5754#include "e1k.h"
    5855
     
    6259
    6360/* Must be power of 8 */
    64 #define E1000_RX_PACKETS_COUNT  128
    65 #define E1000_TX_PACKETS_COUNT  128
     61#define E1000_RX_FRAME_COUNT  128
     62#define E1000_TX_FRAME_COUNT  128
    6663
    6764#define E1000_RECEIVE_ADDRESS  16
    6865
    69 /** Maximum sending packet size */
     66/** Maximum sending frame size */
    7067#define E1000_MAX_SEND_FRAME_SIZE  2048
    71 /** Maximum receiving packet size */
    72 #define E1000_MAX_RECEIVE_PACKET_SIZE  2048
     68/** Maximum receiving frmae size */
     69#define E1000_MAX_RECEIVE_FRAME_SIZE  2048
    7370
    7471/** nic_driver_data_t* -> e1000_t* cast */
     
    137134        void *rx_ring_virt;
    138135       
    139         /** Packets in rx ring  */
    140         packet_t **rx_ring_packets;
     136        /** Ring of RX frames, physical address */
     137        void **rx_frame_phys;
     138        /** Ring of RX frames, virtual address */
     139        void **rx_frame_virt;
    141140       
    142141        /** VLAN tag */
    143142        uint16_t vlan_tag;
    144143       
    145         /** Add VLAN tag to packet */
     144        /** Add VLAN tag to frame */
    146145        bool vlan_tag_add;
    147146       
     
    477476}
    478477
    479 /** Get state of acceptance of weird packets
     478/** Get state of acceptance of weird frames
    480479 *
    481480 * @param      device Device to check
     
    495494};
    496495
    497 /** Set acceptance of weird packets
     496/** Set acceptance of weird frames
    498497 *
    499498 * @param device Device to update
     
    679678}
    680679
    681 /** Disable receiving packets for default address
     680/** Disable receiving frames for default address
    682681 *
    683682 * @param e1000 E1000 data structure
     
    691690}
    692691
    693 /** Enable receiving packets for default address
     692/** Enable receiving frames for default address
    694693 *
    695694 * @param e1000 E1000 data structure
     
    751750}
    752751
    753 /** Enable accepting of broadcast packets
     752/** Enable accepting of broadcast frames
    754753 *
    755754 * @param e1000 E1000 data structure
     
    763762}
    764763
    765 /** Disable accepting of broadcast packets
     764/** Disable accepting of broadcast frames
    766765 *
    767766 * @param e1000 E1000 data structure
     
    799798}
    800799
    801 /** Set multicast packets acceptance mode
     800/** Set multicast frames acceptance mode
    802801 *
    803802 * @param nic      NIC device to update
     
    853852}
    854853
    855 /** Set unicast packets acceptance mode
     854/** Set unicast frames acceptance mode
    856855 *
    857856 * @param nic      NIC device to update
     
    911910}
    912911
    913 /** Set broadcast packets acceptance mode
     912/** Set broadcast frames acceptance mode
    914913 *
    915914 * @param nic  NIC device to update
     
    996995        if (vlan_mask) {
    997996                /*
    998                  * Disable receiving, so that packet matching
     997                 * Disable receiving, so that frame matching
    999998                 * partially written VLAN is not received.
    1000999                 */
     
    10631062}
    10641063
    1065 /** Fill receive descriptor with new empty packet
    1066  *
    1067  * Store packet in e1000->rx_ring_packets
     1064/** Fill receive descriptor with new empty buffer
     1065 *
     1066 * Store frame in e1000->rx_frame_phys
    10681067 *
    10691068 * @param nic    NIC data stricture
     
    10741073{
    10751074        e1000_t *e1000 = DRIVER_DATA_NIC(nic);
    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;
     1075       
    10821076        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
    10831077            (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t));
    10841078       
    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        
     1079        rx_descriptor->phys_addr = PTR_TO_U64(e1000->rx_frame_phys[offset]);
    10941080        rx_descriptor->length = 0;
    10951081        rx_descriptor->checksum = 0;
     
    11551141}
    11561142
    1157 /** Receive packets
     1143/** Receive frames
    11581144 *
    11591145 * @param nic NIC data
    11601146 *
    11611147 */
    1162 static void e1000_receive_packets(nic_t *nic)
     1148static void e1000_receive_frames(nic_t *nic)
    11631149{
    11641150        e1000_t *e1000 = DRIVER_DATA_NIC(nic);
     
    11671153       
    11681154        uint32_t *tail_addr = E1000_REG_ADDR(e1000, E1000_RDT);
    1169         uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
     1155        uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
    11701156       
    11711157        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
     
    11731159       
    11741160        while (rx_descriptor->status & 0x01) {
    1175                 uint32_t packet_size = rx_descriptor->length - E1000_CRC_SIZE;
     1161                uint32_t frame_size = rx_descriptor->length - E1000_CRC_SIZE;
    11761162               
    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);
     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                }
    11821170               
    11831171                e1000_fill_new_rx_descriptor(nic, next_tail);
    11841172               
    1185                 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
    1186                 next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
     1173                *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
     1174                next_tail = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
    11871175               
    11881176                rx_descriptor = (e1000_rx_descriptor_t *)
     
    12251213{
    12261214        if (icr & ICR_RXT0)
    1227                 e1000_receive_packets(nic);
     1215                e1000_receive_frames(nic);
    12281216}
    12291217
     
    12741262}
    12751263
    1276 /** Force receiving all packets in the receive buffer
     1264/** Force receiving all frames in the receive buffer
    12771265 *
    12781266 * @param nic NIC data
     
    13471335static void e1000_initialize_rx_registers(e1000_t *e1000)
    13481336{
    1349         E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_PACKETS_COUNT * 16);
     1337        E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_FRAME_COUNT * 16);
    13501338        E1000_REG_WRITE(e1000, E1000_RDH, 0);
    13511339       
    13521340        /* It is not posible to let HW use all descriptors */
    1353         E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_PACKETS_COUNT - 1);
     1341        E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_FRAME_COUNT - 1);
    13541342       
    13551343        /* Set Broadcast Enable Bit */
     
    13711359       
    13721360        int rc = dmamem_map_anonymous(
    1373             E1000_RX_PACKETS_COUNT * sizeof(e1000_rx_descriptor_t),
     1361            E1000_RX_FRAME_COUNT * sizeof(e1000_rx_descriptor_t),
    13741362            AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->rx_ring_phys,
    13751363            &e1000->rx_ring_virt);
     
    13821370            (uint32_t) PTR_TO_U64(e1000->rx_ring_phys));
    13831371       
    1384         e1000->rx_ring_packets =
    1385             malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *));
    1386         // FIXME: Check return value
    1387        
     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        }
     1395       
     1396        /* Write descriptor */
     1397        for (i = 0; i < E1000_RX_FRAME_COUNT; i++)
     1398                e1000_fill_new_rx_descriptor(nic, i);
     1399       
     1400        e1000_initialize_rx_registers(e1000);
     1401       
     1402        fibril_mutex_unlock(&e1000->rx_lock);
     1403        return EOK;
     1404error:
     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}
     1422
     1423/** Uninitialize receive structure
     1424 *
     1425 * @param nic NIC data
     1426 *
     1427 */
     1428static void e1000_uninitialize_rx_structure(nic_t *nic)
     1429{
     1430        e1000_t *e1000 = DRIVER_DATA_NIC(nic);
     1431       
     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 ring
     1447 *
     1448 * @param e1000 E1000 data
     1449 *
     1450 */
     1451static void e1000_clear_rx_ring(e1000_t *e1000)
     1452{
    13881453        /* Write descriptor */
    13891454        for (unsigned int offset = 0;
    1390             offset < E1000_RX_PACKETS_COUNT;
    1391             offset++)
    1392                 e1000_fill_new_rx_descriptor(nic, offset);
    1393        
    1394         e1000_initialize_rx_registers(e1000);
    1395        
    1396         fibril_mutex_unlock(&e1000->rx_lock);
    1397         return EOK;
    1398 }
    1399 
    1400 /** Uninitialize receive structure
    1401  *
    1402  * @param nic NIC data
    1403  *
    1404  */
    1405 static void e1000_uninitialize_rx_structure(nic_t *nic)
    1406 {
    1407         e1000_t *e1000 = DRIVER_DATA_NIC(nic);
    1408        
    1409         /* Write descriptor */
    1410         for (unsigned int offset = 0;
    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;
     1455            offset < E1000_RX_FRAME_COUNT;
    14321456            offset++)
    14331457                e1000_clear_rx_descriptor(e1000, offset);
     
    14981522static void e1000_initialize_tx_registers(e1000_t *e1000)
    14991523{
    1500         E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16);
     1524        E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_FRAME_COUNT * 16);
    15011525        E1000_REG_WRITE(e1000, E1000_TDH, 0);
    15021526        E1000_REG_WRITE(e1000, E1000_TDT, 0);
     
    15301554       
    15311555        int rc = dmamem_map_anonymous(
    1532             E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t),
     1556            E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t),
    15331557            AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys,
    15341558            &e1000->tx_ring_virt);
     
    15371561       
    15381562        bzero(e1000->tx_ring_virt,
    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 *));
     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 *));
    15431567
    15441568        if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) {
     
    15471571        }
    15481572       
    1549         for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1573        for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    15501574                rc = dmamem_map_anonymous(
    15511575                    E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE,
     
    15721596       
    15731597        if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) {
    1574                 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1598                for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    15751599                        if (e1000->tx_frame_virt[i] != NULL) {
    15761600                                dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
     
    16031627        size_t i;
    16041628       
    1605         for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1629        for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    16061630                dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
    16071631                e1000->tx_frame_virt[i] = NULL;
     
    16301654        /* Write descriptor */
    16311655        for (unsigned int offset = 0;
    1632             offset < E1000_TX_PACKETS_COUNT;
     1656            offset < E1000_TX_FRAME_COUNT;
    16331657            offset++)
    16341658                e1000_clear_tx_descriptor(nic, offset);
     
    16871711}
    16881712
    1689 /** Activate the device to receive and transmit packets
     1713/** Activate the device to receive and transmit frames
    16901714 *
    16911715 * @param nic NIC driver data
     
    22832307       
    22842308        if (!descriptor_available) {
    2285                 /* Packet lost */
     2309                /* Frame lost */
    22862310                fibril_mutex_unlock(&e1000->tx_lock);
    22872311                return;
     
    23122336       
    23132337        tdt++;
    2314         if (tdt == E1000_TX_PACKETS_COUNT)
     2338        if (tdt == E1000_TX_FRAME_COUNT)
    23152339                tdt = 0;
    23162340       
Note: See TracChangeset for help on using the changeset viewer.