Ignore:
File:
1 edited

Legend:

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

    rc6533a7 re777847  
    3939#include <io/log.h>
    4040#include <nic.h>
     41#include <packet_client.h>
    4142#include <device/pci.h>
    4243
     
    4546#include <ipc/ns.h>
    4647
     48#include <net_checksum.h>
     49
    4750#include <str.h>
    4851
     
    5356/** Global mutex for work with shared irq structure */
    5457FIBRIL_MUTEX_INITIALIZE(irq_reg_lock);
    55 
    5658/** Lock interrupt structure mutex */
    57 #define RTL8139_IRQ_STRUCT_LOCK() \
    58         fibril_mutex_lock(&irq_reg_lock)
    59 
     59#define RTL8139_IRQ_STRUCT_LOCK() fibril_mutex_lock(&irq_reg_lock)
    6060/** Unlock interrupt structure mutex */
    61 #define RTL8139_IRQ_STRUCT_UNLOCK() \
    62         fibril_mutex_unlock(&irq_reg_lock)
     61#define RTL8139_IRQ_STRUCT_UNLOCK() fibril_mutex_unlock(&irq_reg_lock)
    6362
    6463/** PCI clock frequency in kHz */
    65 #define RTL8139_PCI_FREQ_KHZ  33000
    66 
    67 #define RTL8139_AUTONEG_CAPS (ETH_AUTONEG_10BASE_T_HALF | \
    68         ETH_AUTONEG_10BASE_T_FULL | ETH_AUTONEG_100BASE_TX_HALF | \
    69         ETH_AUTONEG_100BASE_TX_FULL | ETH_AUTONEG_PAUSE_SYMETRIC)
     64#define RTL8139_PCI_FREQ_KHZ 33000
     65
     66#define RTL8139_AUTONEG_CAPS (ETH_AUTONEG_10BASE_T_HALF \
     67    | ETH_AUTONEG_10BASE_T_FULL | ETH_AUTONEG_100BASE_TX_HALF \
     68    | ETH_AUTONEG_100BASE_TX_FULL | ETH_AUTONEG_PAUSE_SYMETRIC)
    7069
    7170/** Lock transmitter and receiver data
    72  *
    73  * This function shall be called whenever
    74  * both transmitter and receiver locking
    75  * to force safe lock ordering (deadlock prevention)
    76  *
    77  * @param rtl8139 RTL8139 private data
    78  *
     71 *  This function shall be called whenever both transmitter and receiver locking
     72 *  to force safe lock ordering (deadlock prevention)
     73 *
     74 *  @param rtl8139  RTL8139 private data
    7975 */
    8076inline static void rtl8139_lock_all(rtl8139_t *rtl8139)
     
    8783/** Unlock transmitter and receiver data
    8884 *
    89  * @param rtl8139 RTL8139 private data
    90  *
     85 *  @param rtl8139  RTL8139 private data
    9186 */
    9287inline static void rtl8139_unlock_all(rtl8139_t *rtl8139)
     
    157152}
    158153
    159 /** 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
    160155 * rcr_accept_mode value in rtl8139_t
    161156 *
     
    175170}
    176171
    177 /** Fill the mask of accepted multicast frames in the card registers
     172/** Fill the mask of accepted multicast packets in the card registers
    178173 *
    179174 *  @param rtl8139  The rtl8139 private data
     
    399394#define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0)
    400395
    401 /** Send frame with the hardware
     396/** Send packet with the hardware
    402397 *
    403398 * note: the main_lock is locked when framework calls this function
     
    417412        ddf_msg(LVL_DEBUG, "Sending frame");
    418413
    419         if (size > RTL8139_FRAME_MAX_LENGTH) {
     414        if (size > RTL8139_PACKET_MAX_LENGTH) {
    420415                ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes",
    421416                    size);
     
    442437        fibril_mutex_unlock(&rtl8139->tx_lock);
    443438
    444         /* Get address of the buffer descriptor and frame data */
     439        /* Get address of the buffer descriptor and packet data */
    445440        void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4;
    446441        void *buf_addr = rtl8139->tx_buff[tx_curr];
     
    463458        pio_write_32(tsd, tsd_value);
    464459        return;
    465        
     460
    466461err_busy_no_inc:
    467462err_size:
     
    510505}
    511506
    512 /** Create frame structure from the buffer data
     507/** Create packet structure from the buffer data
    513508 *
    514509 * @param nic_data      NIC driver data
    515510 * @param rx_buffer     The receiver buffer
    516511 * @param rx_size       The buffer size
    517  * @param frame_start   The offset where packet data start
    518  * @param frame_size    The size of the frame data
    519  *
    520  * @return The frame list node (not connected)
    521  *
    522  */
    523 static nic_frame_t *rtl8139_read_frame(nic_t *nic_data,
    524     void *rx_buffer, size_t rx_size, size_t frame_start, size_t frame_size)
    525 {
    526         nic_frame_t *frame = nic_alloc_frame(nic_data, frame_size);
     512 * @param packet_start  The offset where packet data start
     513 * @param packet_size   The size of the packet data
     514 *
     515 * @return The packet   list node (not connected)
     516 */
     517static nic_frame_t *rtl8139_read_packet(nic_t *nic_data,
     518    void *rx_buffer, size_t rx_size, size_t packet_start, size_t packet_size)
     519{
     520        nic_frame_t *frame = nic_alloc_frame(nic_data, packet_size);
    527521        if (! frame) {
    528                 ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");
     522                ddf_msg(LVL_ERROR, "Can not allocate frame for received packet.");
    529523                return NULL;
    530524        }
    531525
    532         void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start,
    533             RxBUF_SIZE, frame_size);
     526        void *packet_data = packet_suffix(frame->packet, packet_size);
     527        if (!packet_data) {
     528                ddf_msg(LVL_ERROR, "Can not get the packet suffix.");
     529                nic_release_frame(nic_data, frame);
     530                return NULL;
     531        }
     532
     533        void *ret = rtl8139_memcpy_wrapped(packet_data, rx_buffer, packet_start,
     534            RxBUF_SIZE, packet_size);
    534535        if (ret == NULL) {
    535536                nic_release_frame(nic_data, frame);
     
    567568}
    568569
    569 /** Receive all frames in queue
     570/** Receive all packets in queue
    570571 *
    571572 *  @param nic_data  The controller data
    572  *  @return The linked list of nic_frame_list_t nodes, each containing one frame
    573  */
    574 static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data)
     573 *  @return The linked list of packet_list_t nodes, each containing one packet
     574 */
     575static nic_frame_list_t *rtl8139_packet_receive(nic_t *nic_data)
    575576{
    576577        rtl8139_t *rtl8139 = nic_get_specific(nic_data);
     
    580581        nic_frame_list_t *frames = nic_alloc_frame_list();
    581582        if (!frames)
    582                 ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames.");
     583                ddf_msg(LVL_ERROR, "Can not allocate frame list for received packets.");
    583584
    584585        void *rx_buffer = rtl8139->rx_buff_virt;
     
    604605        while (!rtl8139_hw_buffer_empty(rtl8139)) {
    605606                void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE;
    606                 uint32_t frame_header = uint32_t_le2host( *((uint32_t*)rx_ptr) );
    607                 uint16_t size = frame_header >> 16;
    608                 uint16_t frame_size = size - RTL8139_CRC_SIZE;
    609                 /* received frame flags in frame header */
    610                 uint16_t rcs = (uint16_t) frame_header;
     607                uint32_t packet_header = uint32_t_le2host( *((uint32_t*)rx_ptr) );
     608                uint16_t size = packet_header >> 16;
     609                uint16_t packet_size = size - RTL8139_CRC_SIZE;
     610                /* received packet flags in packet header */
     611                uint16_t rcs = (uint16_t) packet_header;
    611612
    612613                if (size == RTL8139_EARLY_SIZE) {
    613                         /* The frame copying is still in progress, break receiving */
     614                        /* The packet copying is still in progress, break receiving */
    614615                        ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied");
    615616                        break;
     
    617618
    618619                /* Check if the header is valid, otherwise we are lost in the buffer */
    619                 if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) {
     620                if (size == 0 || size > RTL8139_PACKET_MAX_LENGTH) {
    620621                        ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", "
    621                             "header 0x%4"PRIx16". Offset: %zu)", size, frame_header,
     622                            "header 0x%4"PRIx16". Offset: %zu)", size, packet_header,
    622623                            rx_offset);
    623624                        goto rx_err;
     
    628629                }
    629630
    630                 cur_read += size + RTL_FRAME_HEADER_SIZE;
     631                cur_read += size + RTL_PACKET_HEADER_SIZE;
    631632                if (cur_read > max_read)
    632633                        break;
    633634
    634635                if (frames) {
    635                         nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer,
    636                             RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size);
     636                        nic_frame_t *frame = rtl8139_read_packet(nic_data, rx_buffer,
     637                            RxBUF_SIZE, rx_offset + RTL_PACKET_HEADER_SIZE, packet_size);
    637638
    638639                        if (frame)
     
    641642
    642643                /* Update offset */
    643                 rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4);
    644 
    645                 /* Write lesser value to prevent overflow into unread frame
     644                rx_offset = ALIGN_UP(rx_offset + size + RTL_PACKET_HEADER_SIZE, 4);
     645
     646                /* Write lesser value to prevent overflow into unread packet
    646647                 * (the recomendation from the RealTech rtl8139 programming guide)
    647648                 */
     
    659660
    660661
     662irq_pio_range_t rtl8139_irq_pio_ranges[] = {
     663        {
     664                .base = 0,
     665                .size = RTL8139_IO_SIZE
     666        }
     667};
    661668
    662669/** Commands to deal with interrupt
     
    668675 */
    669676irq_cmd_t rtl8139_irq_commands[] = {
    670                 {
    671                                 /* Get the interrupt status */
    672                                 .cmd = CMD_PIO_READ_16,
    673                                 .addr = NULL,
    674                                 .dstarg = 2
    675                 },
    676                 {
    677                                 .cmd = CMD_PREDICATE,
    678                                 .value = 3,
    679                                 .srcarg = 2
    680                 },
    681                 {
    682                                 /* Mark interrupts as solved */
    683                                 .cmd = CMD_PIO_WRITE_16,
    684                                 .addr = NULL,
    685                                 .value = 0xFFFF
    686                 },
    687                 {
    688                                 /* Disable interrupts until interrupt routine is finished */
    689                                 .cmd = CMD_PIO_WRITE_16,
    690                                 .addr = NULL,
    691                                 .value = 0x0000
    692                 },
    693                 {
    694                                 .cmd = CMD_ACCEPT
    695                 }
     677        {
     678                /* Get the interrupt status */
     679                .cmd = CMD_PIO_READ_16,
     680                .addr = NULL,
     681                .dstarg = 2
     682        },
     683        {
     684                .cmd = CMD_PREDICATE,
     685                .value = 3,
     686                .srcarg = 2
     687        },
     688        {
     689                /* Mark interrupts as solved */
     690                .cmd = CMD_PIO_WRITE_16,
     691                .addr = NULL,
     692                .value = 0xFFFF
     693        },
     694        {
     695                /* Disable interrupts until interrupt routine is finished */
     696                .cmd = CMD_PIO_WRITE_16,
     697                .addr = NULL,
     698                .value = 0x0000
     699        },
     700        {
     701                .cmd = CMD_ACCEPT
     702        }
    696703};
    697704
    698705/** Interrupt code definition */
    699706irq_code_t rtl8139_irq_code = {
    700         .cmdcount = sizeof(rtl8139_irq_commands)/sizeof(irq_cmd_t),
     707        .rangecount = sizeof(rtl8139_irq_pio_ranges) / sizeof(irq_pio_range_t),
     708        .ranges = rtl8139_irq_pio_ranges,
     709        .cmdcount = sizeof(rtl8139_irq_commands) / sizeof(irq_cmd_t),
    701710        .cmds = rtl8139_irq_commands
    702711};
     
    726735                tx_used++;
    727736
    728                 /* If the frame was sent */
     737                /* If the packet was sent */
    729738                if (tsd_value & TSD_TOK) {
    730739                        size_t size = REG_GET_VAL(tsd_value, TSD_SIZE);
     
    756765}
    757766
    758 /** Receive all frames from the buffer
     767/** Receive all packets from the buffer
    759768 *
    760769 *  @param rtl8139  driver private data
    761770 */
    762 static void rtl8139_receive_frames(nic_t *nic_data)
     771static void rtl8139_receive_packets(nic_t *nic_data)
    763772{
    764773        assert(nic_data);
     
    768777
    769778        fibril_mutex_lock(&rtl8139->rx_lock);
    770         nic_frame_list_t *frames = rtl8139_frame_receive(nic_data);
     779        nic_frame_list_t *frames = rtl8139_packet_receive(nic_data);
    771780        fibril_mutex_unlock(&rtl8139->rx_lock);
    772781
     
    824833        }
    825834
    826         /* Check transmittion interrupts first to allow transmit next frames
     835        /* Check transmittion interrupts first to allow transmit next packets
    827836         * sooner
    828837         */
     
    831840        }
    832841        if (isr & INT_ROK) {
    833                 rtl8139_receive_frames(nic_data);
     842                rtl8139_receive_packets(nic_data);
    834843        }
    835844        if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) {
     
    888897        RTL8139_IRQ_STRUCT_LOCK();
    889898
    890         rtl8139_irq_code.cmds[0].addr = rtl8139->io_port + ISR;
    891         rtl8139_irq_code.cmds[2].addr = rtl8139->io_port + ISR;
    892         rtl8139_irq_code.cmds[3].addr = rtl8139->io_port + IMR;
     899        rtl8139_irq_code.ranges[0].base = (uintptr_t) rtl8139->io_addr;
     900        rtl8139_irq_code.cmds[0].addr = rtl8139->io_addr + ISR;
     901        rtl8139_irq_code.cmds[2].addr = rtl8139->io_addr + ISR;
     902        rtl8139_irq_code.cmds[3].addr = rtl8139->io_addr + IMR;
    893903        int rc = register_interrupt_handler(nic_get_ddf_dev(nic_data),
    894                 rtl8139->irq, rtl8139_interrupt_handler, &rtl8139_irq_code);
     904            rtl8139->irq, rtl8139_interrupt_handler, &rtl8139_irq_code);
    895905
    896906        RTL8139_IRQ_STRUCT_UNLOCK();
     
    932942}
    933943
    934 /** Activate the device to receive and transmit frames
     944/** Activate the device to receive and transmit packets
    935945 *
    936946 *  @param nic_data  The nic driver data
     
    12121222                goto failed;
    12131223
    1214         /* Set default frame acceptance */
     1224        /* Set default packet acceptance */
    12151225        rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
    12161226        rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
    12171227        rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
    12181228        rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
    1219         /* Set receiver early treshold to 8/16 of frame length */
     1229        /* Set receiver early treshold to 8/16 of packet length */
    12201230        rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
    12211231
    12221232        ddf_msg(LVL_DEBUG, "The device is initialized");
    12231233        return ret;
    1224        
     1234
    12251235failed:
    12261236        ddf_msg(LVL_ERROR, "The device initialization failed");
     
    12871297int rtl8139_dev_add(ddf_dev_t *dev)
    12881298{
    1289         ddf_fun_t *fun;
    1290 
    12911299        assert(dev);
    12921300        ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle);
     
    13251333        }
    13261334
    1327         fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
    1328         if (fun == NULL) {
    1329                 ddf_msg(LVL_ERROR, "Failed creating device function");
    1330                 goto err_srv;
    1331         }
    1332         nic_set_ddf_fun(nic_data, fun);
    1333         fun->ops = &rtl8139_dev_ops;
    1334         fun->driver_data = nic_data;
    1335 
    1336         rc = ddf_fun_bind(fun);
     1335        rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops);
    13371336        if (rc != EOK) {
    1338                 ddf_msg(LVL_ERROR, "Failed binding device function");
    1339                 goto err_fun_create;
    1340         }
    1341         rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
    1342         if (rc != EOK) {
    1343                 ddf_msg(LVL_ERROR, "Failed adding function to category");
    1344                 goto err_fun_bind;
     1337                ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc);
     1338                goto err_irq;
    13451339        }
    13461340
     
    13491343
    13501344        return EOK;
    1351        
    1352 err_fun_bind:
    1353         ddf_fun_unbind(fun);
    1354 err_fun_create:
    1355         ddf_fun_destroy(fun);
    1356 err_srv:
    1357         /* XXX Disconnect from services */
     1345
    13581346err_irq:
    13591347        unregister_interrupt_handler(dev, rtl8139->irq);
     
    14981486};
    14991487
    1500 /** Check if pause frame operations are valid in current situation
     1488/** Check if pause packet operations are valid in current situation
    15011489 *
    15021490 *  @param rtl8139  RTL8139 private structure
     
    15231511}
    15241512
    1525 /** Get current pause frame configuration
     1513/** Get current pause packet configuration
    15261514 *
    15271515 *  Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
     
    15291517 *
    15301518 *  @param[in]  fun         The DDF structure of the RTL8139
    1531  *  @param[out] we_send     Sign if local constroller sends pause frame
    1532  *  @param[out] we_receive  Sign if local constroller receives pause frame
    1533  *  @param[out] time        Time filled in pause frames. 0xFFFF in rtl8139
     1519 *  @param[out] we_send     Sign if local constroller sends pause packets
     1520 *  @param[out] we_receive  Sign if local constroller receives pause packets
     1521 *  @param[out] time        Time filled in pause packets. 0xFFFF in rtl8139
    15341522 *
    15351523 *  @return EOK if succeed
     
    15611549};
    15621550
    1563 /** Set current pause frame configuration
     1551/** Set current pause packet configuration
    15641552 *
    15651553 *  @param fun            The DDF structure of the RTL8139
    1566  *  @param allow_send     Sign if local constroller sends pause frame
    1567  *  @param allow_receive  Sign if local constroller receives pause frames
     1554 *  @param allow_send     Sign if local constroller sends pause packets
     1555 *  @param allow_receive  Sign if local constroller receives pause packets
    15681556 *  @param time           Time to use, ignored (not supported by device)
    15691557 *
    1570  *  @return EOK if succeed, INVAL if the pause frame has no sence
     1558 *  @return EOK if succeed, INVAL if the pause packet has no sence
    15711559 */
    15721560static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
     
    18171805}
    18181806
    1819 /** Set unicast frames acceptance mode
     1807/** Set unicast packets acceptance mode
    18201808 *
    18211809 *  @param nic_data  The nic device to update
     
    18751863}
    18761864
    1877 /** Set multicast frames acceptance mode
     1865/** Set multicast packets acceptance mode
    18781866 *
    18791867 *  @param nic_data  The nic device to update
     
    19201908}
    19211909
    1922 /** Set broadcast frames acceptance mode
     1910/** Set broadcast packets acceptance mode
    19231911 *
    19241912 *  @param nic_data  The nic device to update
     
    19501938}
    19511939
    1952 /** Get state of acceptance of weird frames
     1940/** Get state of acceptance of weird packets
    19531941 *
    19541942 *  @param[in]  device  The device to check
     
    19721960};
    19731961
    1974 /** Set acceptance of weird frames
     1962/** Set acceptance of weird packets
    19751963 *
    19761964 *  @param device  The device to update
     
    21482136}
    21492137
    2150 /** Force receiving all frames in the receive buffer
     2138/** Force receiving all packets in the receive buffer
    21512139 *
    21522140 *  @param device  The device to receive
Note: See TracChangeset for help on using the changeset viewer.