Ignore:
File:
1 edited

Legend:

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

    rf0b74b2 re86b8f0  
    3939#include <io/log.h>
    4040#include <nic.h>
    41 #include <packet_client.h>
    4241#include <device/pci.h>
    4342
     
    152151}
    153152
    154 /** Update the mask of accepted packets in the RCR register according to
     153/** Update the mask of accepted frames in the RCR register according to
    155154 * rcr_accept_mode value in rtl8139_t
    156155 *
     
    170169}
    171170
    172 /** Fill the mask of accepted multicast packets in the card registers
     171/** Fill the mask of accepted multicast frames in the card registers
    173172 *
    174173 *  @param rtl8139  The rtl8139 private data
     
    389388static int rtl8139_on_activated(nic_t *nic_data);
    390389static int rtl8139_on_stopped(nic_t *nic_data);
    391 static void rtl8139_write_packet(nic_t *nic_data, packet_t *packet);
     390static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size);
    392391
    393392/** Check if the transmit buffer is busy */
    394393#define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0)
    395394
    396 /** Send packet with the hardware
     395/** Send frame with the hardware
    397396 *
    398397 * note: the main_lock is locked when framework calls this function
    399398 *
    400399 * @param nic_data  The nic driver data structure
    401  * @param packet    The packet to send
     400 * @param data      Frame data
     401 * @param size      Frame size in bytes
    402402 *
    403403 * @return EOK if succeed, error code in the case of error
    404404 */
    405 static void rtl8139_write_packet(nic_t *nic_data, packet_t *packet)
     405static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size)
    406406{
    407407        assert(nic_data);
     
    409409        rtl8139_t *rtl8139 = nic_get_specific(nic_data);
    410410        assert(rtl8139);
    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);
     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);
    422416                nic_report_send_error(rtl8139->nic_data, NIC_SEC_OTHER, 1);
    423417                goto err_size;
    424418        }
    425419
    426         assert((packet_length & TSD_SIZE_MASK) == packet_length);
     420        assert((size & TSD_SIZE_MASK) == size);
    427421
    428422        /* Lock transmitter structure for obtaining next buffer */
     
    442436        fibril_mutex_unlock(&rtl8139->tx_lock);
    443437
    444         /* Get address of the buffer descriptor and packet data */
     438        /* Get address of the buffer descriptor and frame data */
    445439        void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4;
    446440        void *buf_addr = rtl8139->tx_buff[tx_curr];
     
    449443        assert(!rtl8139_tbuf_busy(tsd));
    450444
    451         /* Write packet data to the buffer, set the size to TSD and clear OWN bit */
    452         memcpy(buf_addr, packet_data, packet_length);
     445        /* Write frame data to the buffer, set the size to TSD and clear OWN bit */
     446        memcpy(buf_addr, data, size);
    453447
    454448        /* Set size of the data to send */
    455449        uint32_t tsd_value = pio_read_32(tsd);
    456         tsd_value = rtl8139_tsd_set_size(tsd_value, packet_length);
     450        tsd_value = rtl8139_tsd_set_size(tsd_value, size);
    457451        pio_write_32(tsd, tsd_value);
    458452
     
    462456        tsd_value &= ~(uint32_t)TSD_OWN;
    463457        pio_write_32(tsd, tsd_value);
    464         nic_release_packet(nic_data, packet);
    465458        return;
    466459
    467460err_busy_no_inc:
    468461err_size:
    469         nic_release_packet(nic_data, packet);
    470462        return;
    471463};
     
    512504}
    513505
    514 /** Create packet structure from the buffer data
     506/** Create frame structure from the buffer data
    515507 *
    516508 * @param nic_data      NIC driver data
    517509 * @param rx_buffer     The receiver buffer
    518510 * @param rx_size       The buffer 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  */
    524 static 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);
     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 */
     516static 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);
    528520        if (! frame) {
    529                 ddf_msg(LVL_ERROR, "Can not allocate frame for received packet.");
     521                ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");
    530522                return NULL;
    531523        }
    532524
    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);
     525        void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start,
     526            RxBUF_SIZE, frame_size);
    542527        if (ret == NULL) {
    543528                nic_release_frame(nic_data, frame);
     
    575560}
    576561
    577 /** Receive all packets in queue
     562/** Receive all frames in queue
    578563 *
    579564 *  @param nic_data  The controller data
    580  *  @return The linked list of packet_list_t nodes, each containing one packet
    581  */
    582 static nic_frame_list_t *rtl8139_packet_receive(nic_t *nic_data)
     565 *  @return The linked list of nic_frame_list_t nodes, each containing one frame
     566 */
     567static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data)
    583568{
    584569        rtl8139_t *rtl8139 = nic_get_specific(nic_data);
     
    588573        nic_frame_list_t *frames = nic_alloc_frame_list();
    589574        if (!frames)
    590                 ddf_msg(LVL_ERROR, "Can not allocate frame list for received packets.");
     575                ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames.");
    591576
    592577        void *rx_buffer = rtl8139->rx_buff_virt;
     
    612597        while (!rtl8139_hw_buffer_empty(rtl8139)) {
    613598                void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE;
    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;
     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;
    619604
    620605                if (size == RTL8139_EARLY_SIZE) {
    621                         /* The packet copying is still in progress, break receiving */
     606                        /* The frame copying is still in progress, break receiving */
    622607                        ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied");
    623608                        break;
     
    625610
    626611                /* Check if the header is valid, otherwise we are lost in the buffer */
    627                 if (size == 0 || size > RTL8139_PACKET_MAX_LENGTH) {
     612                if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) {
    628613                        ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", "
    629                             "header 0x%4"PRIx16". Offset: %zu)", size, packet_header,
     614                            "header 0x%4"PRIx16". Offset: %zu)", size, frame_header,
    630615                            rx_offset);
    631616                        goto rx_err;
     
    636621                }
    637622
    638                 cur_read += size + RTL_PACKET_HEADER_SIZE;
     623                cur_read += size + RTL_FRAME_HEADER_SIZE;
    639624                if (cur_read > max_read)
    640625                        break;
    641626
    642627                if (frames) {
    643                         nic_frame_t *frame = rtl8139_read_packet(nic_data, rx_buffer,
    644                             RxBUF_SIZE, rx_offset + RTL_PACKET_HEADER_SIZE, packet_size);
     628                        nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer,
     629                            RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size);
    645630
    646631                        if (frame)
     
    649634
    650635                /* Update offset */
    651                 rx_offset = ALIGN_UP(rx_offset + size + RTL_PACKET_HEADER_SIZE, 4);
    652 
    653                 /* Write lesser value to prevent overflow into unread packet
     636                rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4);
     637
     638                /* Write lesser value to prevent overflow into unread frame
    654639                 * (the recomendation from the RealTech rtl8139 programming guide)
    655640                 */
     
    734719                tx_used++;
    735720
    736                 /* If the packet was sent */
     721                /* If the frame was sent */
    737722                if (tsd_value & TSD_TOK) {
    738723                        size_t size = REG_GET_VAL(tsd_value, TSD_SIZE);
     
    764749}
    765750
    766 /** Receive all packets from the buffer
     751/** Receive all frames from the buffer
    767752 *
    768753 *  @param rtl8139  driver private data
    769754 */
    770 static void rtl8139_receive_packets(nic_t *nic_data)
     755static void rtl8139_receive_frames(nic_t *nic_data)
    771756{
    772757        assert(nic_data);
     
    776761
    777762        fibril_mutex_lock(&rtl8139->rx_lock);
    778         nic_frame_list_t *frames = rtl8139_packet_receive(nic_data);
     763        nic_frame_list_t *frames = rtl8139_frame_receive(nic_data);
    779764        fibril_mutex_unlock(&rtl8139->rx_lock);
    780765
     
    832817        }
    833818
    834         /* Check transmittion interrupts first to allow transmit next packets
     819        /* Check transmittion interrupts first to allow transmit next frames
    835820         * sooner
    836821         */
     
    839824        }
    840825        if (isr & INT_ROK) {
    841                 rtl8139_receive_packets(nic_data);
     826                rtl8139_receive_frames(nic_data);
    842827        }
    843828        if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) {
     
    940925}
    941926
    942 /** Activate the device to receive and transmit packets
     927/** Activate the device to receive and transmit frames
    943928 *
    944929 *  @param nic_data  The nic driver data
     
    10221007        rtl8139->nic_data = nic_data;
    10231008        nic_set_specific(nic_data, rtl8139);
    1024         nic_set_write_packet_handler(nic_data, rtl8139_write_packet);
     1009        nic_set_send_frame_handler(nic_data, rtl8139_send_frame);
    10251010        nic_set_state_change_handlers(nic_data,
    10261011                rtl8139_on_activated, NULL, rtl8139_on_stopped);
     
    12201205                goto failed;
    12211206
    1222         /* Set default packet acceptance */
     1207        /* Set default frame acceptance */
    12231208        rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
    12241209        rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
    12251210        rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
    12261211        rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
    1227         /* Set receiver early treshold to 8/16 of packet length */
     1212        /* Set receiver early treshold to 8/16 of frame length */
    12281213        rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
    12291214
     
    12951280int rtl8139_dev_add(ddf_dev_t *dev)
    12961281{
     1282        ddf_fun_t *fun;
     1283
    12971284        assert(dev);
    12981285        ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle);
     
    13311318        }
    13321319
    1333         rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops);
     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);
    13341330        if (rc != EOK) {
    1335                 ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc);
    1336                 goto err_irq;
     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;
    13371338        }
    13381339
     
    13421343        return EOK;
    13431344
     1345err_fun_bind:
     1346        ddf_fun_unbind(fun);
     1347err_fun_create:
     1348        ddf_fun_destroy(fun);
     1349err_srv:
     1350        /* XXX Disconnect from services */
    13441351err_irq:
    13451352        unregister_interrupt_handler(dev, rtl8139->irq);
     
    14841491};
    14851492
    1486 /** Check if pause packet operations are valid in current situation
     1493/** Check if pause frame operations are valid in current situation
    14871494 *
    14881495 *  @param rtl8139  RTL8139 private structure
     
    15091516}
    15101517
    1511 /** Get current pause packet configuration
     1518/** Get current pause frame configuration
    15121519 *
    15131520 *  Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
     
    15151522 *
    15161523 *  @param[in]  fun         The DDF structure of the 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
     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
    15201527 *
    15211528 *  @return EOK if succeed
     
    15471554};
    15481555
    1549 /** Set current pause packet configuration
     1556/** Set current pause frame configuration
    15501557 *
    15511558 *  @param fun            The DDF structure of the RTL8139
    1552  *  @param allow_send     Sign if local constroller sends pause packets
    1553  *  @param allow_receive  Sign if local constroller receives pause packets
     1559 *  @param allow_send     Sign if local constroller sends pause frame
     1560 *  @param allow_receive  Sign if local constroller receives pause frames
    15541561 *  @param time           Time to use, ignored (not supported by device)
    15551562 *
    1556  *  @return EOK if succeed, INVAL if the pause packet has no sence
     1563 *  @return EOK if succeed, INVAL if the pause frame has no sence
    15571564 */
    15581565static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
     
    18031810}
    18041811
    1805 /** Set unicast packets acceptance mode
     1812/** Set unicast frames acceptance mode
    18061813 *
    18071814 *  @param nic_data  The nic device to update
     
    18611868}
    18621869
    1863 /** Set multicast packets acceptance mode
     1870/** Set multicast frames acceptance mode
    18641871 *
    18651872 *  @param nic_data  The nic device to update
     
    19061913}
    19071914
    1908 /** Set broadcast packets acceptance mode
     1915/** Set broadcast frames acceptance mode
    19091916 *
    19101917 *  @param nic_data  The nic device to update
     
    19361943}
    19371944
    1938 /** Get state of acceptance of weird packets
     1945/** Get state of acceptance of weird frames
    19391946 *
    19401947 *  @param[in]  device  The device to check
     
    19581965};
    19591966
    1960 /** Set acceptance of weird packets
     1967/** Set acceptance of weird frames
    19611968 *
    19621969 *  @param device  The device to update
     
    21342141}
    21352142
    2136 /** Force receiving all packets in the receive buffer
     2143/** Force receiving all frames in the receive buffer
    21372144 *
    21382145 *  @param device  The device to receive
Note: See TracChangeset for help on using the changeset viewer.