Ignore:
File:
1 edited

Legend:

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

    rec52752 rd81eaf94  
    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 frame 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       
     
    228227static int e1000_on_stopping(nic_t *);
    229228static void e1000_send_frame(nic_t *, void *, size_t);
    230 
    231 /** PIO ranges used in the IRQ code. */
    232 irq_pio_range_t e1000_irq_pio_ranges[] = {
    233         {
    234                 .base = 0,
    235                 .size = PAGE_SIZE,      /* XXX */
    236         }
    237 };
    238229
    239230/** Commands to deal with interrupt
     
    265256/** Interrupt code definition */
    266257irq_code_t e1000_irq_code = {
    267         .rangecount = sizeof(e1000_irq_pio_ranges) /
    268             sizeof(irq_pio_range_t),
    269         .ranges = e1000_irq_pio_ranges,
    270258        .cmdcount = sizeof(e1000_irq_commands) / sizeof(irq_cmd_t),
    271259        .cmds = e1000_irq_commands
     
    488476}
    489477
    490 /** Get state of acceptance of weird packets
     478/** Get state of acceptance of weird frames
    491479 *
    492480 * @param      device Device to check
     
    506494};
    507495
    508 /** Set acceptance of weird packets
     496/** Set acceptance of weird frames
    509497 *
    510498 * @param device Device to update
     
    690678}
    691679
    692 /** Disable receiving packets for default address
     680/** Disable receiving frames for default address
    693681 *
    694682 * @param e1000 E1000 data structure
     
    702690}
    703691
    704 /** Enable receiving packets for default address
     692/** Enable receiving frames for default address
    705693 *
    706694 * @param e1000 E1000 data structure
     
    762750}
    763751
    764 /** Enable accepting of broadcast packets
     752/** Enable accepting of broadcast frames
    765753 *
    766754 * @param e1000 E1000 data structure
     
    774762}
    775763
    776 /** Disable accepting of broadcast packets
     764/** Disable accepting of broadcast frames
    777765 *
    778766 * @param e1000 E1000 data structure
     
    810798}
    811799
    812 /** Set multicast packets acceptance mode
     800/** Set multicast frames acceptance mode
    813801 *
    814802 * @param nic      NIC device to update
     
    864852}
    865853
    866 /** Set unicast packets acceptance mode
     854/** Set unicast frames acceptance mode
    867855 *
    868856 * @param nic      NIC device to update
     
    922910}
    923911
    924 /** Set broadcast packets acceptance mode
     912/** Set broadcast frames acceptance mode
    925913 *
    926914 * @param nic  NIC device to update
     
    1007995        if (vlan_mask) {
    1008996                /*
    1009                  * Disable receiving, so that packet matching
     997                 * Disable receiving, so that frame matching
    1010998                 * partially written VLAN is not received.
    1011999                 */
     
    10741062}
    10751063
    1076 /** Fill receive descriptor with new empty packet
    1077  *
    1078  * Store packet in e1000->rx_ring_packets
     1064/** Fill receive descriptor with new empty buffer
     1065 *
     1066 * Store frame in e1000->rx_frame_phys
    10791067 *
    10801068 * @param nic    NIC data stricture
     
    10851073{
    10861074        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;
     1075       
    10931076        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
    10941077            (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t));
    10951078       
    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        
     1079        rx_descriptor->phys_addr = PTR_TO_U64(e1000->rx_frame_phys[offset]);
    11051080        rx_descriptor->length = 0;
    11061081        rx_descriptor->checksum = 0;
     
    11661141}
    11671142
    1168 /** Receive packets
     1143/** Receive frames
    11691144 *
    11701145 * @param nic NIC data
    11711146 *
    11721147 */
    1173 static void e1000_receive_packets(nic_t *nic)
     1148static void e1000_receive_frames(nic_t *nic)
    11741149{
    11751150        e1000_t *e1000 = DRIVER_DATA_NIC(nic);
     
    11781153       
    11791154        uint32_t *tail_addr = E1000_REG_ADDR(e1000, E1000_RDT);
    1180         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);
    11811156       
    11821157        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
     
    11841159       
    11851160        while (rx_descriptor->status & 0x01) {
    1186                 uint32_t packet_size = rx_descriptor->length - E1000_CRC_SIZE;
     1161                uint32_t frame_size = rx_descriptor->length - E1000_CRC_SIZE;
    11871162               
    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);
     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                }
    11931170               
    11941171                e1000_fill_new_rx_descriptor(nic, next_tail);
    11951172               
    1196                 *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
    1197                 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);
    11981175               
    11991176                rx_descriptor = (e1000_rx_descriptor_t *)
     
    12361213{
    12371214        if (icr & ICR_RXT0)
    1238                 e1000_receive_packets(nic);
     1215                e1000_receive_frames(nic);
    12391216}
    12401217
     
    12751252        fibril_mutex_lock(&irq_reg_mutex);
    12761253       
    1277         e1000_irq_code.ranges[0].base = (uintptr_t) e1000->reg_base_phys;
    1278         e1000_irq_code.cmds[0].addr = e1000->reg_base_phys + E1000_ICR;
    1279         e1000_irq_code.cmds[2].addr = e1000->reg_base_phys + E1000_IMC;
     1254        e1000_irq_code.cmds[0].addr = e1000->reg_base_virt + E1000_ICR;
     1255        e1000_irq_code.cmds[2].addr = e1000->reg_base_virt + E1000_IMC;
    12801256       
    12811257        int rc = register_interrupt_handler(nic_get_ddf_dev(nic),
     
    12861262}
    12871263
    1288 /** Force receiving all packets in the receive buffer
     1264/** Force receiving all frames in the receive buffer
    12891265 *
    12901266 * @param nic NIC data
     
    13591335static void e1000_initialize_rx_registers(e1000_t *e1000)
    13601336{
    1361         E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_PACKETS_COUNT * 16);
     1337        E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_FRAME_COUNT * 16);
    13621338        E1000_REG_WRITE(e1000, E1000_RDH, 0);
    13631339       
    13641340        /* It is not posible to let HW use all descriptors */
    1365         E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_PACKETS_COUNT - 1);
     1341        E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_FRAME_COUNT - 1);
    13661342       
    13671343        /* Set Broadcast Enable Bit */
     
    13831359       
    13841360        int rc = dmamem_map_anonymous(
    1385             E1000_RX_PACKETS_COUNT * sizeof(e1000_rx_descriptor_t),
     1361            E1000_RX_FRAME_COUNT * sizeof(e1000_rx_descriptor_t),
    13861362            AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->rx_ring_phys,
    13871363            &e1000->rx_ring_virt);
     
    13941370            (uint32_t) PTR_TO_U64(e1000->rx_ring_phys));
    13951371       
    1396         e1000->rx_ring_packets =
    1397             malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *));
    1398         // FIXME: Check return value
    1399        
     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;
     1404       
     1405error:
     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}
     1426
     1427/** Uninitialize receive structure
     1428 *
     1429 * @param nic NIC data
     1430 *
     1431 */
     1432static void e1000_uninitialize_rx_structure(nic_t *nic)
     1433{
     1434        e1000_t *e1000 = DRIVER_DATA_NIC(nic);
     1435       
     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 ring
     1451 *
     1452 * @param e1000 E1000 data
     1453 *
     1454 */
     1455static void e1000_clear_rx_ring(e1000_t *e1000)
     1456{
    14001457        /* Write descriptor */
    14011458        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;
     1459            offset < E1000_RX_FRAME_COUNT;
    14441460            offset++)
    14451461                e1000_clear_rx_descriptor(e1000, offset);
     
    15101526static void e1000_initialize_tx_registers(e1000_t *e1000)
    15111527{
    1512         E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16);
     1528        E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_FRAME_COUNT * 16);
    15131529        E1000_REG_WRITE(e1000, E1000_TDH, 0);
    15141530        E1000_REG_WRITE(e1000, E1000_TDT, 0);
     
    15421558       
    15431559        int rc = dmamem_map_anonymous(
    1544             E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t),
     1560            E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t),
    15451561            AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys,
    15461562            &e1000->tx_ring_virt);
     
    15491565       
    15501566        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 *));
     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 *));
    15551571
    15561572        if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) {
     
    15591575        }
    15601576       
    1561         for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1577        for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    15621578                rc = dmamem_map_anonymous(
    15631579                    E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE,
     
    15841600       
    15851601        if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) {
    1586                 for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1602                for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    15871603                        if (e1000->tx_frame_virt[i] != NULL) {
    15881604                                dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
     
    16151631        size_t i;
    16161632       
    1617         for (i = 0; i < E1000_TX_PACKETS_COUNT; i++) {
     1633        for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
    16181634                dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
    16191635                e1000->tx_frame_virt[i] = NULL;
     
    16301646                e1000->tx_frame_phys = NULL;
    16311647        }
     1648       
    16321649        dmamem_unmap_anonymous(e1000->tx_ring_virt);
    16331650}
     
    16421659        /* Write descriptor */
    16431660        for (unsigned int offset = 0;
    1644             offset < E1000_TX_PACKETS_COUNT;
     1661            offset < E1000_TX_FRAME_COUNT;
    16451662            offset++)
    16461663                e1000_clear_tx_descriptor(nic, offset);
     
    16991716}
    17001717
    1701 /** Activate the device to receive and transmit packets
     1718/** Activate the device to receive and transmit frames
    17021719 *
    17031720 * @param nic NIC driver data
     
    20402057        case E1000_82545:
    20412058        case E1000_82546:
    2042         case E1000_82572:
    20432059                e1000->info.eerd_start = 0x01;
    20442060                e1000->info.eerd_done = 0x10;
     
    20472063                break;
    20482064        case E1000_82547:
     2065        case E1000_82572:
    20492066        case E1000_80003ES2:
    20502067                e1000->info.eerd_start = 0x01;
     
    20852102int e1000_dev_add(ddf_dev_t *dev)
    20862103{
     2104        ddf_fun_t *fun;
    20872105        assert(dev);
    20882106       
     
    21152133        e1000_initialize_vlan(e1000);
    21162134       
    2117         rc = nic_register_as_ddf_fun(nic, &e1000_dev_ops);
    2118         if (rc != EOK)
     2135        fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");
     2136        if (fun == NULL)
    21192137                goto err_tx_structure;
     2138        nic_set_ddf_fun(nic, fun);
     2139        fun->ops = &e1000_dev_ops;
     2140        fun->driver_data = nic;
    21202141       
    21212142        rc = e1000_register_int_handler(nic);
    21222143        if (rc != EOK)
    2123                 goto err_tx_structure;
     2144                goto err_fun_create;
    21242145       
    21252146        rc = nic_connect_to_services(nic);
     
    21442165                goto err_rx_structure;
    21452166       
     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       
    21462175        return EOK;
    21472176       
     2177err_add_to_cat:
     2178        ddf_fun_unbind(fun);
     2179err_fun_bind:
    21482180err_rx_structure:
    21492181        e1000_uninitialize_rx_structure(nic);
    21502182err_irq:
    21512183        unregister_interrupt_handler(dev, DRIVER_DATA_DEV(dev)->irq);
     2184err_fun_create:
     2185        ddf_fun_destroy(fun);
     2186        nic_set_ddf_fun(nic, NULL);
    21522187err_tx_structure:
    21532188        e1000_uninitialize_tx_structure(e1000);
     
    22952330       
    22962331        if (!descriptor_available) {
    2297                 /* Packet lost */
     2332                /* Frame lost */
    22982333                fibril_mutex_unlock(&e1000->tx_lock);
    22992334                return;
     
    23242359       
    23252360        tdt++;
    2326         if (tdt == E1000_TX_PACKETS_COUNT)
     2361        if (tdt == E1000_TX_FRAME_COUNT)
    23272362                tdt = 0;
    23282363       
Note: See TracChangeset for help on using the changeset viewer.