Changeset cfb79747 in mainline for uspace/drv/nic/rtl8139/driver.c
- Timestamp:
- 2012-02-14T22:06:15Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a31aad1
- Parents:
- 199112e4 (diff), e10d41a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/rtl8139/driver.c
r199112e4 rcfb79747 39 39 #include <io/log.h> 40 40 #include <nic.h> 41 #include <packet_client.h>42 41 #include <device/pci.h> 43 42 … … 56 55 /** Global mutex for work with shared irq structure */ 57 56 FIBRIL_MUTEX_INITIALIZE(irq_reg_lock); 57 58 58 /** Lock interrupt structure mutex */ 59 #define RTL8139_IRQ_STRUCT_LOCK() fibril_mutex_lock(&irq_reg_lock) 59 #define RTL8139_IRQ_STRUCT_LOCK() \ 60 fibril_mutex_lock(&irq_reg_lock) 61 60 62 /** Unlock interrupt structure mutex */ 61 #define RTL8139_IRQ_STRUCT_UNLOCK() fibril_mutex_unlock(&irq_reg_lock) 63 #define RTL8139_IRQ_STRUCT_UNLOCK() \ 64 fibril_mutex_unlock(&irq_reg_lock) 62 65 63 66 /** PCI clock frequency in kHz */ 64 #define RTL8139_PCI_FREQ_KHZ 3300065 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)67 #define RTL8139_PCI_FREQ_KHZ 33000 68 69 #define RTL8139_AUTONEG_CAPS (ETH_AUTONEG_10BASE_T_HALF | \ 70 ETH_AUTONEG_10BASE_T_FULL | ETH_AUTONEG_100BASE_TX_HALF | \ 71 ETH_AUTONEG_100BASE_TX_FULL | ETH_AUTONEG_PAUSE_SYMETRIC) 69 72 70 73 /** Lock transmitter and receiver data 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 74 * 75 * This function shall be called whenever 76 * both transmitter and receiver locking 77 * to force safe lock ordering (deadlock prevention) 78 * 79 * @param rtl8139 RTL8139 private data 80 * 75 81 */ 76 82 inline static void rtl8139_lock_all(rtl8139_t *rtl8139) … … 83 89 /** Unlock transmitter and receiver data 84 90 * 85 * @param rtl8139 RTL8139 private data 91 * @param rtl8139 RTL8139 private data 92 * 86 93 */ 87 94 inline static void rtl8139_unlock_all(rtl8139_t *rtl8139) … … 152 159 } 153 160 154 /** Update the mask of accepted packets in the RCR register according to161 /** Update the mask of accepted frames in the RCR register according to 155 162 * rcr_accept_mode value in rtl8139_t 156 163 * … … 170 177 } 171 178 172 /** Fill the mask of accepted multicast packets in the card registers179 /** Fill the mask of accepted multicast frames in the card registers 173 180 * 174 181 * @param rtl8139 The rtl8139 private data … … 394 401 #define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0) 395 402 396 /** Send packetwith the hardware403 /** Send frame with the hardware 397 404 * 398 405 * note: the main_lock is locked when framework calls this function … … 412 419 ddf_msg(LVL_DEBUG, "Sending frame"); 413 420 414 if (size > RTL8139_ PACKET_MAX_LENGTH) {421 if (size > RTL8139_FRAME_MAX_LENGTH) { 415 422 ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes", 416 423 size); … … 437 444 fibril_mutex_unlock(&rtl8139->tx_lock); 438 445 439 /* Get address of the buffer descriptor and packetdata */446 /* Get address of the buffer descriptor and frame data */ 440 447 void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4; 441 448 void *buf_addr = rtl8139->tx_buff[tx_curr]; … … 458 465 pio_write_32(tsd, tsd_value); 459 466 return; 460 467 461 468 err_busy_no_inc: 462 469 err_size: … … 505 512 } 506 513 507 /** Create packetstructure from the buffer data514 /** Create frame structure from the buffer data 508 515 * 509 516 * @param nic_data NIC driver data 510 517 * @param rx_buffer The receiver buffer 511 518 * @param rx_size The buffer 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 */ 517 static 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); 519 * @param frame_start The offset where packet data start 520 * @param frame_size The size of the frame data 521 * 522 * @return The frame list node (not connected) 523 * 524 */ 525 static nic_frame_t *rtl8139_read_frame(nic_t *nic_data, 526 void *rx_buffer, size_t rx_size, size_t frame_start, size_t frame_size) 527 { 528 nic_frame_t *frame = nic_alloc_frame(nic_data, frame_size); 521 529 if (! frame) { 522 ddf_msg(LVL_ERROR, "Can not allocate frame for received packet.");530 ddf_msg(LVL_ERROR, "Can not allocate frame for received frame."); 523 531 return NULL; 524 532 } 525 533 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); 534 void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start, 535 RxBUF_SIZE, frame_size); 535 536 if (ret == NULL) { 536 537 nic_release_frame(nic_data, frame); … … 568 569 } 569 570 570 /** Receive all packets in queue571 /** Receive all frames in queue 571 572 * 572 573 * @param nic_data The controller data 573 * @return The linked list of packet_list_t nodes, each containing one packet574 */ 575 static nic_frame_list_t *rtl8139_ packet_receive(nic_t *nic_data)574 * @return The linked list of nic_frame_list_t nodes, each containing one frame 575 */ 576 static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data) 576 577 { 577 578 rtl8139_t *rtl8139 = nic_get_specific(nic_data); … … 581 582 nic_frame_list_t *frames = nic_alloc_frame_list(); 582 583 if (!frames) 583 ddf_msg(LVL_ERROR, "Can not allocate frame list for received packets.");584 ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames."); 584 585 585 586 void *rx_buffer = rtl8139->rx_buff_virt; … … 605 606 while (!rtl8139_hw_buffer_empty(rtl8139)) { 606 607 void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE; 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 packetheader */611 uint16_t rcs = (uint16_t) packet_header;608 uint32_t frame_header = uint32_t_le2host( *((uint32_t*)rx_ptr) ); 609 uint16_t size = frame_header >> 16; 610 uint16_t frame_size = size - RTL8139_CRC_SIZE; 611 /* received frame flags in frame header */ 612 uint16_t rcs = (uint16_t) frame_header; 612 613 613 614 if (size == RTL8139_EARLY_SIZE) { 614 /* The packetcopying is still in progress, break receiving */615 /* The frame copying is still in progress, break receiving */ 615 616 ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied"); 616 617 break; … … 618 619 619 620 /* Check if the header is valid, otherwise we are lost in the buffer */ 620 if (size == 0 || size > RTL8139_ PACKET_MAX_LENGTH) {621 if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) { 621 622 ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", " 622 "header 0x%4"PRIx16". Offset: %zu)", size, packet_header,623 "header 0x%4"PRIx16". Offset: %zu)", size, frame_header, 623 624 rx_offset); 624 625 goto rx_err; … … 629 630 } 630 631 631 cur_read += size + RTL_ PACKET_HEADER_SIZE;632 cur_read += size + RTL_FRAME_HEADER_SIZE; 632 633 if (cur_read > max_read) 633 634 break; 634 635 635 636 if (frames) { 636 nic_frame_t *frame = rtl8139_read_ packet(nic_data, rx_buffer,637 RxBUF_SIZE, rx_offset + RTL_ PACKET_HEADER_SIZE, packet_size);637 nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer, 638 RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size); 638 639 639 640 if (frame) … … 642 643 643 644 /* Update offset */ 644 rx_offset = ALIGN_UP(rx_offset + size + RTL_ PACKET_HEADER_SIZE, 4);645 646 /* Write lesser value to prevent overflow into unread packet645 rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4); 646 647 /* Write lesser value to prevent overflow into unread frame 647 648 * (the recomendation from the RealTech rtl8139 programming guide) 648 649 */ … … 735 736 tx_used++; 736 737 737 /* If the packetwas sent */738 /* If the frame was sent */ 738 739 if (tsd_value & TSD_TOK) { 739 740 size_t size = REG_GET_VAL(tsd_value, TSD_SIZE); … … 765 766 } 766 767 767 /** Receive all packets from the buffer768 /** Receive all frames from the buffer 768 769 * 769 770 * @param rtl8139 driver private data 770 771 */ 771 static void rtl8139_receive_ packets(nic_t *nic_data)772 static void rtl8139_receive_frames(nic_t *nic_data) 772 773 { 773 774 assert(nic_data); … … 777 778 778 779 fibril_mutex_lock(&rtl8139->rx_lock); 779 nic_frame_list_t *frames = rtl8139_ packet_receive(nic_data);780 nic_frame_list_t *frames = rtl8139_frame_receive(nic_data); 780 781 fibril_mutex_unlock(&rtl8139->rx_lock); 781 782 … … 833 834 } 834 835 835 /* Check transmittion interrupts first to allow transmit next packets836 /* Check transmittion interrupts first to allow transmit next frames 836 837 * sooner 837 838 */ … … 840 841 } 841 842 if (isr & INT_ROK) { 842 rtl8139_receive_ packets(nic_data);843 rtl8139_receive_frames(nic_data); 843 844 } 844 845 if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) { … … 942 943 } 943 944 944 /** Activate the device to receive and transmit packets945 /** Activate the device to receive and transmit frames 945 946 * 946 947 * @param nic_data The nic driver data … … 1222 1223 goto failed; 1223 1224 1224 /* Set default packetacceptance */1225 /* Set default frame acceptance */ 1225 1226 rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT; 1226 1227 rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT; 1227 1228 rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT; 1228 1229 rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT; 1229 /* Set receiver early treshold to 8/16 of packetlength */1230 /* Set receiver early treshold to 8/16 of frame length */ 1230 1231 rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT); 1231 1232 1232 1233 ddf_msg(LVL_DEBUG, "The device is initialized"); 1233 1234 return ret; 1234 1235 1235 1236 failed: 1236 1237 ddf_msg(LVL_ERROR, "The device initialization failed"); … … 1297 1298 int rtl8139_dev_add(ddf_dev_t *dev) 1298 1299 { 1300 ddf_fun_t *fun; 1301 1299 1302 assert(dev); 1300 1303 ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle); … … 1333 1336 } 1334 1337 1335 rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops); 1338 fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0"); 1339 if (fun == NULL) { 1340 ddf_msg(LVL_ERROR, "Failed creating device function"); 1341 goto err_srv; 1342 } 1343 nic_set_ddf_fun(nic_data, fun); 1344 fun->ops = &rtl8139_dev_ops; 1345 fun->driver_data = nic_data; 1346 1347 rc = ddf_fun_bind(fun); 1336 1348 if (rc != EOK) { 1337 ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc); 1338 goto err_irq; 1349 ddf_msg(LVL_ERROR, "Failed binding device function"); 1350 goto err_fun_create; 1351 } 1352 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC); 1353 if (rc != EOK) { 1354 ddf_msg(LVL_ERROR, "Failed adding function to category"); 1355 goto err_fun_bind; 1339 1356 } 1340 1357 … … 1343 1360 1344 1361 return EOK; 1345 1362 1363 err_fun_bind: 1364 ddf_fun_unbind(fun); 1365 err_fun_create: 1366 ddf_fun_destroy(fun); 1367 err_srv: 1368 /* XXX Disconnect from services */ 1346 1369 err_irq: 1347 1370 unregister_interrupt_handler(dev, rtl8139->irq); … … 1486 1509 }; 1487 1510 1488 /** Check if pause packetoperations are valid in current situation1511 /** Check if pause frame operations are valid in current situation 1489 1512 * 1490 1513 * @param rtl8139 RTL8139 private structure … … 1511 1534 } 1512 1535 1513 /** Get current pause packetconfiguration1536 /** Get current pause frame configuration 1514 1537 * 1515 1538 * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in … … 1517 1540 * 1518 1541 * @param[in] fun The DDF structure of the RTL8139 1519 * @param[out] we_send Sign if local constroller sends pause packets1520 * @param[out] we_receive Sign if local constroller receives pause packets1521 * @param[out] time Time filled in pause packets. 0xFFFF in rtl81391542 * @param[out] we_send Sign if local constroller sends pause frame 1543 * @param[out] we_receive Sign if local constroller receives pause frame 1544 * @param[out] time Time filled in pause frames. 0xFFFF in rtl8139 1522 1545 * 1523 1546 * @return EOK if succeed … … 1549 1572 }; 1550 1573 1551 /** Set current pause packetconfiguration1574 /** Set current pause frame configuration 1552 1575 * 1553 1576 * @param fun The DDF structure of the RTL8139 1554 * @param allow_send Sign if local constroller sends pause packets1555 * @param allow_receive Sign if local constroller receives pause packets1577 * @param allow_send Sign if local constroller sends pause frame 1578 * @param allow_receive Sign if local constroller receives pause frames 1556 1579 * @param time Time to use, ignored (not supported by device) 1557 1580 * 1558 * @return EOK if succeed, INVAL if the pause packethas no sence1581 * @return EOK if succeed, INVAL if the pause frame has no sence 1559 1582 */ 1560 1583 static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive, … … 1805 1828 } 1806 1829 1807 /** Set unicast packets acceptance mode1830 /** Set unicast frames acceptance mode 1808 1831 * 1809 1832 * @param nic_data The nic device to update … … 1863 1886 } 1864 1887 1865 /** Set multicast packets acceptance mode1888 /** Set multicast frames acceptance mode 1866 1889 * 1867 1890 * @param nic_data The nic device to update … … 1908 1931 } 1909 1932 1910 /** Set broadcast packets acceptance mode1933 /** Set broadcast frames acceptance mode 1911 1934 * 1912 1935 * @param nic_data The nic device to update … … 1938 1961 } 1939 1962 1940 /** Get state of acceptance of weird packets1963 /** Get state of acceptance of weird frames 1941 1964 * 1942 1965 * @param[in] device The device to check … … 1960 1983 }; 1961 1984 1962 /** Set acceptance of weird packets1985 /** Set acceptance of weird frames 1963 1986 * 1964 1987 * @param device The device to update … … 2136 2159 } 2137 2160 2138 /** Force receiving all packets in the receive buffer2161 /** Force receiving all frames in the receive buffer 2139 2162 * 2140 2163 * @param device The device to receive
Note:
See TracChangeset
for help on using the changeset viewer.