Changes in uspace/drv/nic/rtl8139/driver.c [5cd3d67:e777847] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/rtl8139/driver.c
r5cd3d67 re777847 39 39 #include <io/log.h> 40 40 #include <nic.h> 41 #include <packet_client.h> 41 42 #include <device/pci.h> 42 43 … … 55 56 /** Global mutex for work with shared irq structure */ 56 57 FIBRIL_MUTEX_INITIALIZE(irq_reg_lock); 57 58 58 /** Lock interrupt structure mutex */ 59 #define RTL8139_IRQ_STRUCT_LOCK() \ 60 fibril_mutex_lock(&irq_reg_lock) 61 59 #define RTL8139_IRQ_STRUCT_LOCK() fibril_mutex_lock(&irq_reg_lock) 62 60 /** Unlock interrupt structure mutex */ 63 #define RTL8139_IRQ_STRUCT_UNLOCK() \ 64 fibril_mutex_unlock(&irq_reg_lock) 61 #define RTL8139_IRQ_STRUCT_UNLOCK() fibril_mutex_unlock(&irq_reg_lock) 65 62 66 63 /** PCI clock frequency in kHz */ 67 #define RTL8139_PCI_FREQ_KHZ 68 69 #define RTL8139_AUTONEG_CAPS (ETH_AUTONEG_10BASE_T_HALF |\70 ETH_AUTONEG_10BASE_T_FULL | ETH_AUTONEG_100BASE_TX_HALF |\71 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) 72 69 73 70 /** Lock transmitter and receiver 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 * 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 81 75 */ 82 76 inline static void rtl8139_lock_all(rtl8139_t *rtl8139) … … 89 83 /** Unlock transmitter and receiver data 90 84 * 91 * @param rtl8139 RTL8139 private data 92 * 85 * @param rtl8139 RTL8139 private data 93 86 */ 94 87 inline static void rtl8139_unlock_all(rtl8139_t *rtl8139) … … 159 152 } 160 153 161 /** Update the mask of accepted frames in the RCR register according to154 /** Update the mask of accepted packets in the RCR register according to 162 155 * rcr_accept_mode value in rtl8139_t 163 156 * … … 177 170 } 178 171 179 /** Fill the mask of accepted multicast frames in the card registers172 /** Fill the mask of accepted multicast packets in the card registers 180 173 * 181 174 * @param rtl8139 The rtl8139 private data … … 401 394 #define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0) 402 395 403 /** Send framewith the hardware396 /** Send packet with the hardware 404 397 * 405 398 * note: the main_lock is locked when framework calls this function … … 419 412 ddf_msg(LVL_DEBUG, "Sending frame"); 420 413 421 if (size > RTL8139_ FRAME_MAX_LENGTH) {414 if (size > RTL8139_PACKET_MAX_LENGTH) { 422 415 ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes", 423 416 size); … … 444 437 fibril_mutex_unlock(&rtl8139->tx_lock); 445 438 446 /* Get address of the buffer descriptor and framedata */439 /* Get address of the buffer descriptor and packet data */ 447 440 void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4; 448 441 void *buf_addr = rtl8139->tx_buff[tx_curr]; … … 465 458 pio_write_32(tsd, tsd_value); 466 459 return; 467 460 468 461 err_busy_no_inc: 469 462 err_size: … … 512 505 } 513 506 514 /** Create framestructure from the buffer data507 /** Create packet structure from the buffer data 515 508 * 516 509 * @param nic_data NIC driver data 517 510 * @param rx_buffer The receiver buffer 518 511 * @param rx_size The buffer 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); 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); 529 521 if (! frame) { 530 ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");522 ddf_msg(LVL_ERROR, "Can not allocate frame for received packet."); 531 523 return NULL; 532 524 } 533 525 534 void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start, 535 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); 536 535 if (ret == NULL) { 537 536 nic_release_frame(nic_data, frame); … … 569 568 } 570 569 571 /** Receive all frames in queue570 /** Receive all packets in queue 572 571 * 573 572 * @param nic_data The controller data 574 * @return The linked list of nic_frame_list_t nodes, each containing one frame575 */ 576 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 */ 575 static nic_frame_list_t *rtl8139_packet_receive(nic_t *nic_data) 577 576 { 578 577 rtl8139_t *rtl8139 = nic_get_specific(nic_data); … … 582 581 nic_frame_list_t *frames = nic_alloc_frame_list(); 583 582 if (!frames) 584 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."); 585 584 586 585 void *rx_buffer = rtl8139->rx_buff_virt; … … 606 605 while (!rtl8139_hw_buffer_empty(rtl8139)) { 607 606 void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE; 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 frameheader */612 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; 613 612 614 613 if (size == RTL8139_EARLY_SIZE) { 615 /* The framecopying is still in progress, break receiving */614 /* The packet copying is still in progress, break receiving */ 616 615 ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied"); 617 616 break; … … 619 618 620 619 /* Check if the header is valid, otherwise we are lost in the buffer */ 621 if (size == 0 || size > RTL8139_ FRAME_MAX_LENGTH) {620 if (size == 0 || size > RTL8139_PACKET_MAX_LENGTH) { 622 621 ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", " 623 "header 0x%4"PRIx16". Offset: %zu)", size, frame_header,622 "header 0x%4"PRIx16". Offset: %zu)", size, packet_header, 624 623 rx_offset); 625 624 goto rx_err; … … 630 629 } 631 630 632 cur_read += size + RTL_ FRAME_HEADER_SIZE;631 cur_read += size + RTL_PACKET_HEADER_SIZE; 633 632 if (cur_read > max_read) 634 633 break; 635 634 636 635 if (frames) { 637 nic_frame_t *frame = rtl8139_read_ frame(nic_data, rx_buffer,638 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); 639 638 640 639 if (frame) … … 643 642 644 643 /* Update offset */ 645 rx_offset = ALIGN_UP(rx_offset + size + RTL_ FRAME_HEADER_SIZE, 4);646 647 /* Write lesser value to prevent overflow into unread frame644 rx_offset = ALIGN_UP(rx_offset + size + RTL_PACKET_HEADER_SIZE, 4); 645 646 /* Write lesser value to prevent overflow into unread packet 648 647 * (the recomendation from the RealTech rtl8139 programming guide) 649 648 */ … … 661 660 662 661 662 irq_pio_range_t rtl8139_irq_pio_ranges[] = { 663 { 664 .base = 0, 665 .size = RTL8139_IO_SIZE 666 } 667 }; 663 668 664 669 /** Commands to deal with interrupt … … 670 675 */ 671 676 irq_cmd_t rtl8139_irq_commands[] = { 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 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 } 698 703 }; 699 704 700 705 /** Interrupt code definition */ 701 706 irq_code_t rtl8139_irq_code = { 702 .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), 703 710 .cmds = rtl8139_irq_commands 704 711 }; … … 728 735 tx_used++; 729 736 730 /* If the framewas sent */737 /* If the packet was sent */ 731 738 if (tsd_value & TSD_TOK) { 732 739 size_t size = REG_GET_VAL(tsd_value, TSD_SIZE); … … 758 765 } 759 766 760 /** Receive all frames from the buffer767 /** Receive all packets from the buffer 761 768 * 762 769 * @param rtl8139 driver private data 763 770 */ 764 static void rtl8139_receive_ frames(nic_t *nic_data)771 static void rtl8139_receive_packets(nic_t *nic_data) 765 772 { 766 773 assert(nic_data); … … 770 777 771 778 fibril_mutex_lock(&rtl8139->rx_lock); 772 nic_frame_list_t *frames = rtl8139_ frame_receive(nic_data);779 nic_frame_list_t *frames = rtl8139_packet_receive(nic_data); 773 780 fibril_mutex_unlock(&rtl8139->rx_lock); 774 781 … … 826 833 } 827 834 828 /* Check transmittion interrupts first to allow transmit next frames835 /* Check transmittion interrupts first to allow transmit next packets 829 836 * sooner 830 837 */ … … 833 840 } 834 841 if (isr & INT_ROK) { 835 rtl8139_receive_ frames(nic_data);842 rtl8139_receive_packets(nic_data); 836 843 } 837 844 if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) { … … 890 897 RTL8139_IRQ_STRUCT_LOCK(); 891 898 892 rtl8139_irq_code.cmds[0].addr = rtl8139->io_port + ISR; 893 rtl8139_irq_code.cmds[2].addr = rtl8139->io_port + ISR; 894 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; 895 903 int rc = register_interrupt_handler(nic_get_ddf_dev(nic_data), 896 904 rtl8139->irq, rtl8139_interrupt_handler, &rtl8139_irq_code); 897 905 898 906 RTL8139_IRQ_STRUCT_UNLOCK(); … … 934 942 } 935 943 936 /** Activate the device to receive and transmit frames944 /** Activate the device to receive and transmit packets 937 945 * 938 946 * @param nic_data The nic driver data … … 1214 1222 goto failed; 1215 1223 1216 /* Set default frameacceptance */1224 /* Set default packet acceptance */ 1217 1225 rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT; 1218 1226 rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT; 1219 1227 rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT; 1220 1228 rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT; 1221 /* Set receiver early treshold to 8/16 of framelength */1229 /* Set receiver early treshold to 8/16 of packet length */ 1222 1230 rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT); 1223 1231 1224 1232 ddf_msg(LVL_DEBUG, "The device is initialized"); 1225 1233 return ret; 1226 1234 1227 1235 failed: 1228 1236 ddf_msg(LVL_ERROR, "The device initialization failed"); … … 1289 1297 int rtl8139_dev_add(ddf_dev_t *dev) 1290 1298 { 1291 ddf_fun_t *fun;1292 1293 1299 assert(dev); 1294 1300 ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle); … … 1327 1333 } 1328 1334 1329 fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0"); 1330 if (fun == NULL) { 1331 ddf_msg(LVL_ERROR, "Failed creating device function"); 1332 goto err_srv; 1333 } 1334 nic_set_ddf_fun(nic_data, fun); 1335 fun->ops = &rtl8139_dev_ops; 1336 fun->driver_data = nic_data; 1337 1338 rc = ddf_fun_bind(fun); 1335 rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops); 1339 1336 if (rc != EOK) { 1340 ddf_msg(LVL_ERROR, "Failed binding device function"); 1341 goto err_fun_create; 1342 } 1343 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC); 1344 if (rc != EOK) { 1345 ddf_msg(LVL_ERROR, "Failed adding function to category"); 1346 goto err_fun_bind; 1337 ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc); 1338 goto err_irq; 1347 1339 } 1348 1340 … … 1351 1343 1352 1344 return EOK; 1353 1354 err_fun_bind: 1355 ddf_fun_unbind(fun); 1356 err_fun_create: 1357 ddf_fun_destroy(fun); 1358 err_srv: 1359 /* XXX Disconnect from services */ 1345 1360 1346 err_irq: 1361 1347 unregister_interrupt_handler(dev, rtl8139->irq); … … 1500 1486 }; 1501 1487 1502 /** Check if pause frameoperations are valid in current situation1488 /** Check if pause packet operations are valid in current situation 1503 1489 * 1504 1490 * @param rtl8139 RTL8139 private structure … … 1525 1511 } 1526 1512 1527 /** Get current pause frameconfiguration1513 /** Get current pause packet configuration 1528 1514 * 1529 1515 * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in … … 1531 1517 * 1532 1518 * @param[in] fun The DDF structure of the RTL8139 1533 * @param[out] we_send Sign if local constroller sends pause frame1534 * @param[out] we_receive Sign if local constroller receives pause frame1535 * @param[out] time Time filled in pause frames. 0xFFFF in rtl81391519 * @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 1536 1522 * 1537 1523 * @return EOK if succeed … … 1563 1549 }; 1564 1550 1565 /** Set current pause frameconfiguration1551 /** Set current pause packet configuration 1566 1552 * 1567 1553 * @param fun The DDF structure of the RTL8139 1568 * @param allow_send Sign if local constroller sends pause frame1569 * @param allow_receive Sign if local constroller receives pause frames1554 * @param allow_send Sign if local constroller sends pause packets 1555 * @param allow_receive Sign if local constroller receives pause packets 1570 1556 * @param time Time to use, ignored (not supported by device) 1571 1557 * 1572 * @return EOK if succeed, INVAL if the pause framehas no sence1558 * @return EOK if succeed, INVAL if the pause packet has no sence 1573 1559 */ 1574 1560 static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive, … … 1819 1805 } 1820 1806 1821 /** Set unicast frames acceptance mode1807 /** Set unicast packets acceptance mode 1822 1808 * 1823 1809 * @param nic_data The nic device to update … … 1877 1863 } 1878 1864 1879 /** Set multicast frames acceptance mode1865 /** Set multicast packets acceptance mode 1880 1866 * 1881 1867 * @param nic_data The nic device to update … … 1922 1908 } 1923 1909 1924 /** Set broadcast frames acceptance mode1910 /** Set broadcast packets acceptance mode 1925 1911 * 1926 1912 * @param nic_data The nic device to update … … 1952 1938 } 1953 1939 1954 /** Get state of acceptance of weird frames1940 /** Get state of acceptance of weird packets 1955 1941 * 1956 1942 * @param[in] device The device to check … … 1974 1960 }; 1975 1961 1976 /** Set acceptance of weird frames1962 /** Set acceptance of weird packets 1977 1963 * 1978 1964 * @param device The device to update … … 2150 2136 } 2151 2137 2152 /** Force receiving all frames in the receive buffer2138 /** Force receiving all packets in the receive buffer 2153 2139 * 2154 2140 * @param device The device to receive
Note:
See TracChangeset
for help on using the changeset viewer.