Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/nic/rtl8139/driver.c

    re86b8f0 rf0b74b2  
    3939#include <io/log.h>
    4040#include <nic.h>
     41#include <packet_client.h>
    4142#include <device/pci.h>
    4243
     
    151152}
    152153
    153 /** Update the mask of accepted frames in the RCR register according to
     154/** Update the mask of accepted packets in the RCR register according to
    154155 * rcr_accept_mode value in rtl8139_t
    155156 *
     
    169170}
    170171
    171 /** Fill the mask of accepted multicast frames in the card registers
     172/** Fill the mask of accepted multicast packets in the card registers
    172173 *
    173174 *  @param rtl8139  The rtl8139 private data
     
    388389static int rtl8139_on_activated(nic_t *nic_data);
    389390static int rtl8139_on_stopped(nic_t *nic_data);
    390 static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size);
     391static void rtl8139_write_packet(nic_t *nic_data, packet_t *packet);
    391392
    392393/** Check if the transmit buffer is busy */
    393394#define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0)
    394395
    395 /** Send frame with the hardware
     396/** Send packet with the hardware
    396397 *
    397398 * note: the main_lock is locked when framework calls this function
    398399 *
    399400 * @param nic_data  The nic driver data structure
    400  * @param data      Frame data
    401  * @param size      Frame size in bytes
     401 * @param packet    The packet to send
    402402 *
    403403 * @return EOK if succeed, error code in the case of error
    404404 */
    405 static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size)
     405static void rtl8139_write_packet(nic_t *nic_data, packet_t *packet)
    406406{
    407407        assert(nic_data);
     
    409409        rtl8139_t *rtl8139 = nic_get_specific(nic_data);
    410410        assert(rtl8139);
    411         ddf_msg(LVL_DEBUG, "Sending frame");
    412 
    413         if (size > RTL8139_FRAME_MAX_LENGTH) {
    414                 ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes",
    415                     size);
     411        ddf_msg(LVL_DEBUG, "Sending packet");
     412
     413        /* Get the packet data and check if it can be send */
     414        size_t packet_length = packet_get_data_length(packet);
     415        void *packet_data = packet_get_data(packet);
     416
     417        assert(packet_data);
     418
     419        if ((packet_length > RTL8139_PACKET_MAX_LENGTH) || !packet_data) {
     420                ddf_msg(LVL_ERROR, "Write packet length error: data %p, length %z",
     421                    packet_data, packet_length);
    416422                nic_report_send_error(rtl8139->nic_data, NIC_SEC_OTHER, 1);
    417423                goto err_size;
    418424        }
    419425
    420         assert((size & TSD_SIZE_MASK) == size);
     426        assert((packet_length & TSD_SIZE_MASK) == packet_length);
    421427
    422428        /* Lock transmitter structure for obtaining next buffer */
     
    436442        fibril_mutex_unlock(&rtl8139->tx_lock);
    437443
    438         /* Get address of the buffer descriptor and frame data */
     444        /* Get address of the buffer descriptor and packet data */
    439445        void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4;
    440446        void *buf_addr = rtl8139->tx_buff[tx_curr];
     
    443449        assert(!rtl8139_tbuf_busy(tsd));
    444450
    445         /* Write frame data to the buffer, set the size to TSD and clear OWN bit */
    446         memcpy(buf_addr, data, size);
     451        /* Write packet data to the buffer, set the size to TSD and clear OWN bit */
     452        memcpy(buf_addr, packet_data, packet_length);
    447453
    448454        /* Set size of the data to send */
    449455        uint32_t tsd_value = pio_read_32(tsd);
    450         tsd_value = rtl8139_tsd_set_size(tsd_value, size);
     456        tsd_value = rtl8139_tsd_set_size(tsd_value, packet_length);
    451457        pio_write_32(tsd, tsd_value);
    452458
     
    456462        tsd_value &= ~(uint32_t)TSD_OWN;
    457463        pio_write_32(tsd, tsd_value);
     464        nic_release_packet(nic_data, packet);
    458465        return;
    459466
    460467err_busy_no_inc:
    461468err_size:
     469        nic_release_packet(nic_data, packet);
    462470        return;
    463471};
     
    504512}
    505513
    506 /** Create frame structure from the buffer data
     514/** Create packet structure from the buffer data
    507515 *
    508516 * @param nic_data      NIC driver data
    509517 * @param rx_buffer     The receiver buffer
    510518 * @param rx_size       The buffer size
    511  * @param frame_start   The offset where packet data start
    512  * @param frame_size    The size of the frame data
    513  *
    514  * @return The frame list node (not connected)
    515  */
    516 static nic_frame_t *rtl8139_read_frame(nic_t *nic_data,
    517     void *rx_buffer, size_t rx_size, size_t frame_start, size_t frame_size)
    518 {
    519         nic_frame_t *frame = nic_alloc_frame(nic_data, frame_size);
     519 * @param packet_start  The offset where packet data start
     520 * @param packet_size   The size of the packet data
     521 *
     522 * @return The packet  list node (not connected)
     523 */
     524static nic_frame_t *rtl8139_read_packet(nic_t *nic_data,
     525    void *rx_buffer, size_t rx_size, size_t packet_start, size_t packet_size)
     526{
     527        nic_frame_t *frame = nic_alloc_frame(nic_data, packet_size);
    520528        if (! frame) {
    521                 ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");
     529                ddf_msg(LVL_ERROR, "Can not allocate frame for received packet.");
    522530                return NULL;
    523531        }
    524532
    525         void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start,
    526             RxBUF_SIZE, frame_size);
     533        void *packet_data = packet_suffix(frame->packet, packet_size);
     534        if (!packet_data) {
     535                ddf_msg(LVL_ERROR, "Can not get the packet suffix.");
     536                nic_release_frame(nic_data, frame);
     537                return NULL;
     538        }
     539
     540        void *ret = rtl8139_memcpy_wrapped(packet_data, rx_buffer, packet_start,
     541            RxBUF_SIZE, packet_size);
    527542        if (ret == NULL) {
    528543                nic_release_frame(nic_data, frame);
     
    560575}
    561576
    562 /** Receive all frames in queue
     577/** Receive all packets in queue
    563578 *
    564579 *  @param nic_data  The controller data
    565  *  @return The linked list of nic_frame_list_t nodes, each containing one frame
    566  */
    567 static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data)
     580 *  @return The linked list of packet_list_t nodes, each containing one packet
     581 */
     582static nic_frame_list_t *rtl8139_packet_receive(nic_t *nic_data)
    568583{
    569584        rtl8139_t *rtl8139 = nic_get_specific(nic_data);
     
    573588        nic_frame_list_t *frames = nic_alloc_frame_list();
    574589        if (!frames)
    575                 ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames.");
     590                ddf_msg(LVL_ERROR, "Can not allocate frame list for received packets.");
    576591
    577592        void *rx_buffer = rtl8139->rx_buff_virt;
     
    597612        while (!rtl8139_hw_buffer_empty(rtl8139)) {
    598613                void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE;
    599                 uint32_t frame_header = uint32_t_le2host( *((uint32_t*)rx_ptr) );
    600                 uint16_t size = frame_header >> 16;
    601                 uint16_t frame_size = size - RTL8139_CRC_SIZE;
    602                 /* received frame flags in frame header */
    603                 uint16_t rcs = (uint16_t) frame_header;
     614                uint32_t packet_header = uint32_t_le2host( *((uint32_t*)rx_ptr) );
     615                uint16_t size = packet_header >> 16;
     616                uint16_t packet_size = size - RTL8139_CRC_SIZE;
     617                /* received packet flags in packet header */
     618                uint16_t rcs = (uint16_t) packet_header;
    604619
    605620                if (size == RTL8139_EARLY_SIZE) {
    606                         /* The frame copying is still in progress, break receiving */
     621                        /* The packet copying is still in progress, break receiving */
    607622                        ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied");
    608623                        break;
     
    610625
    611626                /* Check if the header is valid, otherwise we are lost in the buffer */
    612                 if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) {
     627                if (size == 0 || size > RTL8139_PACKET_MAX_LENGTH) {
    613628                        ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", "
    614                             "header 0x%4"PRIx16". Offset: %zu)", size, frame_header,
     629                            "header 0x%4"PRIx16". Offset: %zu)", size, packet_header,
    615630                            rx_offset);
    616631                        goto rx_err;
     
    621636                }
    622637
    623                 cur_read += size + RTL_FRAME_HEADER_SIZE;
     638                cur_read += size + RTL_PACKET_HEADER_SIZE;
    624639                if (cur_read > max_read)
    625640                        break;
    626641
    627642                if (frames) {
    628                         nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer,
    629                             RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size);
     643                        nic_frame_t *frame = rtl8139_read_packet(nic_data, rx_buffer,
     644                            RxBUF_SIZE, rx_offset + RTL_PACKET_HEADER_SIZE, packet_size);
    630645
    631646                        if (frame)
     
    634649
    635650                /* Update offset */
    636                 rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4);
    637 
    638                 /* Write lesser value to prevent overflow into unread frame
     651                rx_offset = ALIGN_UP(rx_offset + size + RTL_PACKET_HEADER_SIZE, 4);
     652
     653                /* Write lesser value to prevent overflow into unread packet
    639654                 * (the recomendation from the RealTech rtl8139 programming guide)
    640655                 */
     
    719734                tx_used++;
    720735
    721                 /* If the frame was sent */
     736                /* If the packet was sent */
    722737                if (tsd_value & TSD_TOK) {
    723738                        size_t size = REG_GET_VAL(tsd_value, TSD_SIZE);
     
    749764}
    750765
    751 /** Receive all frames from the buffer
     766/** Receive all packets from the buffer
    752767 *
    753768 *  @param rtl8139  driver private data
    754769 */
    755 static void rtl8139_receive_frames(nic_t *nic_data)
     770static void rtl8139_receive_packets(nic_t *nic_data)
    756771{
    757772        assert(nic_data);
     
    761776
    762777        fibril_mutex_lock(&rtl8139->rx_lock);
    763         nic_frame_list_t *frames = rtl8139_frame_receive(nic_data);
     778        nic_frame_list_t *frames = rtl8139_packet_receive(nic_data);
    764779        fibril_mutex_unlock(&rtl8139->rx_lock);
    765780
     
    817832        }
    818833
    819         /* Check transmittion interrupts first to allow transmit next frames
     834        /* Check transmittion interrupts first to allow transmit next packets
    820835         * sooner
    821836         */
     
    824839        }
    825840        if (isr & INT_ROK) {
    826                 rtl8139_receive_frames(nic_data);
     841                rtl8139_receive_packets(nic_data);
    827842        }
    828843        if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) {
     
    925940}
    926941
    927 /** Activate the device to receive and transmit frames
     942/** Activate the device to receive and transmit packets
    928943 *
    929944 *  @param nic_data  The nic driver data
     
    10071022        rtl8139->nic_data = nic_data;
    10081023        nic_set_specific(nic_data, rtl8139);
    1009         nic_set_send_frame_handler(nic_data, rtl8139_send_frame);
     1024        nic_set_write_packet_handler(nic_data, rtl8139_write_packet);
    10101025        nic_set_state_change_handlers(nic_data,
    10111026                rtl8139_on_activated, NULL, rtl8139_on_stopped);
     
    12051220                goto failed;
    12061221
    1207         /* Set default frame acceptance */
     1222        /* Set default packet acceptance */
    12081223        rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
    12091224        rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
    12101225        rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
    12111226        rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
    1212         /* Set receiver early treshold to 8/16 of frame length */
     1227        /* Set receiver early treshold to 8/16 of packet length */
    12131228        rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
    12141229
     
    12801295int rtl8139_dev_add(ddf_dev_t *dev)
    12811296{
    1282         ddf_fun_t *fun;
    1283 
    12841297        assert(dev);
    12851298        ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle);
     
    13181331        }
    13191332
    1320         fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
    1321         if (fun == NULL) {
    1322                 ddf_msg(LVL_ERROR, "Failed creating device function");
    1323                 goto err_srv;
    1324         }
    1325         nic_set_ddf_fun(nic_data, fun);
    1326         fun->ops = &rtl8139_dev_ops;
    1327         fun->driver_data = nic_data;
    1328 
    1329         rc = ddf_fun_bind(fun);
     1333        rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops);
    13301334        if (rc != EOK) {
    1331                 ddf_msg(LVL_ERROR, "Failed binding device function");
    1332                 goto err_fun_create;
    1333         }
    1334         rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
    1335         if (rc != EOK) {
    1336                 ddf_msg(LVL_ERROR, "Failed adding function to category");
    1337                 goto err_fun_bind;
     1335                ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc);
     1336                goto err_irq;
    13381337        }
    13391338
     
    13431342        return EOK;
    13441343
    1345 err_fun_bind:
    1346         ddf_fun_unbind(fun);
    1347 err_fun_create:
    1348         ddf_fun_destroy(fun);
    1349 err_srv:
    1350         /* XXX Disconnect from services */
    13511344err_irq:
    13521345        unregister_interrupt_handler(dev, rtl8139->irq);
     
    14911484};
    14921485
    1493 /** Check if pause frame operations are valid in current situation
     1486/** Check if pause packet operations are valid in current situation
    14941487 *
    14951488 *  @param rtl8139  RTL8139 private structure
     
    15161509}
    15171510
    1518 /** Get current pause frame configuration
     1511/** Get current pause packet configuration
    15191512 *
    15201513 *  Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
     
    15221515 *
    15231516 *  @param[in]  fun         The DDF structure of the RTL8139
    1524  *  @param[out] we_send     Sign if local constroller sends pause frame
    1525  *  @param[out] we_receive  Sign if local constroller receives pause frame
    1526  *  @param[out] time        Time filled in pause frames. 0xFFFF in rtl8139
     1517 *  @param[out] we_send     Sign if local constroller sends pause packets
     1518 *  @param[out] we_receive  Sign if local constroller receives pause packets
     1519 *  @param[out] time        Time filled in pause packets. 0xFFFF in rtl8139
    15271520 *
    15281521 *  @return EOK if succeed
     
    15541547};
    15551548
    1556 /** Set current pause frame configuration
     1549/** Set current pause packet configuration
    15571550 *
    15581551 *  @param fun            The DDF structure of the RTL8139
    1559  *  @param allow_send     Sign if local constroller sends pause frame
    1560  *  @param allow_receive  Sign if local constroller receives pause frames
     1552 *  @param allow_send     Sign if local constroller sends pause packets
     1553 *  @param allow_receive  Sign if local constroller receives pause packets
    15611554 *  @param time           Time to use, ignored (not supported by device)
    15621555 *
    1563  *  @return EOK if succeed, INVAL if the pause frame has no sence
     1556 *  @return EOK if succeed, INVAL if the pause packet has no sence
    15641557 */
    15651558static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
     
    18101803}
    18111804
    1812 /** Set unicast frames acceptance mode
     1805/** Set unicast packets acceptance mode
    18131806 *
    18141807 *  @param nic_data  The nic device to update
     
    18681861}
    18691862
    1870 /** Set multicast frames acceptance mode
     1863/** Set multicast packets acceptance mode
    18711864 *
    18721865 *  @param nic_data  The nic device to update
     
    19131906}
    19141907
    1915 /** Set broadcast frames acceptance mode
     1908/** Set broadcast packets acceptance mode
    19161909 *
    19171910 *  @param nic_data  The nic device to update
     
    19431936}
    19441937
    1945 /** Get state of acceptance of weird frames
     1938/** Get state of acceptance of weird packets
    19461939 *
    19471940 *  @param[in]  device  The device to check
     
    19651958};
    19661959
    1967 /** Set acceptance of weird frames
     1960/** Set acceptance of weird packets
    19681961 *
    19691962 *  @param device  The device to update
     
    21412134}
    21422135
    2143 /** Force receiving all frames in the receive buffer
     2136/** Force receiving all packets in the receive buffer
    21442137 *
    21452138 *  @param device  The device to receive
Note: See TracChangeset for help on using the changeset viewer.