Changeset 7943c43 in mainline for uspace/drv/nic/rtl8139/driver.c
- Timestamp:
- 2012-01-16T22:45:38Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 32817cc, 3fe58d3c
- Parents:
- 9117ef9b (diff), 3ea725e (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
r9117ef9b r7943c43 33 33 #include <libarch/ddi.h> 34 34 #include <libarch/barrier.h> 35 #include <nlog.h> 36 37 #include <d ma.h>35 36 #include <as.h> 37 #include <ddf/log.h> 38 38 #include <ddf/interrupt.h> 39 #include < devman.h>39 #include <io/log.h> 40 40 #include <nic.h> 41 41 #include <packet_client.h> … … 50 50 #include <str.h> 51 51 52 #include " rtl8139_defs.h"53 #include " rtl8139_driver.h"54 #include " rtl8139_general.h"52 #include "defs.h" 53 #include "driver.h" 54 #include "general.h" 55 55 56 56 /** Global mutex for work with shared irq structure */ … … 164 164 (RXBUF_SIZE_FLAGS << RCR_RBLEN_SHIFT); 165 165 166 nlog_debug("Rewriting rcr: %x -> %x", pio_read_32(rtl8139->io_port + RCR),166 ddf_msg(LVL_DEBUG, "Rewriting rcr: %x -> %x", pio_read_32(rtl8139->io_port + RCR), 167 167 rcr); 168 168 … … 373 373 static ddf_dev_ops_t rtl8139_dev_ops; 374 374 375 static int rtl8139_ add_device(ddf_dev_t *dev);375 static int rtl8139_dev_add(ddf_dev_t *dev); 376 376 377 377 /** Basic driver operations for RTL8139 driver */ 378 378 static driver_ops_t rtl8139_driver_ops = { 379 . add_device = &rtl8139_add_device,379 .dev_add = &rtl8139_dev_add, 380 380 }; 381 381 … … 389 389 static int rtl8139_on_activated(nic_t *nic_data); 390 390 static int rtl8139_on_stopped(nic_t *nic_data); 391 static void rtl8139_ write_packet(nic_t *nic_data, packet_t *packet);391 static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size); 392 392 393 393 /** Check if the transmit buffer is busy */ … … 399 399 * 400 400 * @param nic_data The nic driver data structure 401 * @param packet The packet to send 401 * @param data Frame data 402 * @param size Frame size in bytes 402 403 * 403 404 * @return EOK if succeed, error code in the case of error 404 405 */ 405 static void rtl8139_ write_packet(nic_t *nic_data, packet_t *packet)406 static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size) 406 407 { 407 408 assert(nic_data); … … 409 410 rtl8139_t *rtl8139 = nic_get_specific(nic_data); 410 411 assert(rtl8139); 411 nlog_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 nlog_error("Write packet length error: data %p, length %z", 421 packet_data, packet_length); 412 ddf_msg(LVL_DEBUG, "Sending frame"); 413 414 if (size > RTL8139_PACKET_MAX_LENGTH) { 415 ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes", 416 size); 422 417 nic_report_send_error(rtl8139->nic_data, NIC_SEC_OTHER, 1); 423 418 goto err_size; 424 419 } 425 420 426 assert(( packet_length & TSD_SIZE_MASK) == packet_length);421 assert((size & TSD_SIZE_MASK) == size); 427 422 428 423 /* Lock transmitter structure for obtaining next buffer */ … … 449 444 assert(!rtl8139_tbuf_busy(tsd)); 450 445 451 /* Write packetdata to the buffer, set the size to TSD and clear OWN bit */452 memcpy(buf_addr, packet_data, packet_length);446 /* Write frame data to the buffer, set the size to TSD and clear OWN bit */ 447 memcpy(buf_addr, data, size); 453 448 454 449 /* Set size of the data to send */ 455 450 uint32_t tsd_value = pio_read_32(tsd); 456 tsd_value = rtl8139_tsd_set_size(tsd_value, packet_length);451 tsd_value = rtl8139_tsd_set_size(tsd_value, size); 457 452 pio_write_32(tsd, tsd_value); 458 453 … … 462 457 tsd_value &= ~(uint32_t)TSD_OWN; 463 458 pio_write_32(tsd, tsd_value); 464 nic_release_packet(nic_data, packet);465 459 return; 466 460 467 461 err_busy_no_inc: 468 462 err_size: 469 nic_release_packet(nic_data, packet);470 463 return; 471 464 }; … … 527 520 nic_frame_t *frame = nic_alloc_frame(nic_data, packet_size); 528 521 if (! frame) { 529 nlog_error("Can not allocate frame for received packet.");522 ddf_msg(LVL_ERROR, "Can not allocate frame for received packet."); 530 523 return NULL; 531 524 } … … 533 526 void *packet_data = packet_suffix(frame->packet, packet_size); 534 527 if (!packet_data) { 535 nlog_error("Can not get the packet suffix.");528 ddf_msg(LVL_ERROR, "Can not get the packet suffix."); 536 529 nic_release_frame(nic_data, frame); 537 530 return NULL; … … 564 557 pio_write_32(rtl8139->io_port + CAPR, 0); 565 558 pio_write_32(rtl8139->io_port + RBSTART, 566 PTR2U32(rtl8139->rx_buff .physical));559 PTR2U32(rtl8139->rx_buff_phys)); 567 560 568 561 write_barrier(); … … 588 581 nic_frame_list_t *frames = nic_alloc_frame_list(); 589 582 if (!frames) 590 nlog_error("Can not allocate frame list for received packets.");591 592 void *rx_buffer = rtl8139->rx_buff .virtual;583 ddf_msg(LVL_ERROR, "Can not allocate frame list for received packets."); 584 585 void *rx_buffer = rtl8139->rx_buff_virt; 593 586 594 587 /* where to start reading */ … … 620 613 if (size == RTL8139_EARLY_SIZE) { 621 614 /* The packet copying is still in progress, break receiving */ 622 nlog_debug("Early threshold reached, not completely coppied");615 ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied"); 623 616 break; 624 617 } … … 626 619 /* Check if the header is valid, otherwise we are lost in the buffer */ 627 620 if (size == 0 || size > RTL8139_PACKET_MAX_LENGTH) { 628 nlog_error("Receiver error -> receiver reset (size: %4"PRIu16", "621 ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4"PRIu16", " 629 622 "header 0x%4"PRIx16". Offset: %zu)", size, packet_header, 630 623 rx_offset); … … 632 625 } 633 626 if (size < RTL8139_RUNT_MAX_SIZE && !(rcs & RSR_RUNT)) { 634 nlog_error("Receiver error -> receiver reset (%"PRIx16")", size);627 ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (%"PRIx16")", size); 635 628 goto rx_err; 636 629 } … … 801 794 pio_write_32(rtl8139->io_port + TIMERINT, timer_val); 802 795 pio_write_32(rtl8139->io_port + TCTR, 0x0); 803 nlog_debug("rtl8139 timer: %"PRIu32"\treceive: %d", timer_val, receive);796 ddf_msg(LVL_DEBUG, "rtl8139 timer: %"PRIu32"\treceive: %d", timer_val, receive); 804 797 return receive; 805 798 } … … 925 918 /* Write transmittion buffer addresses */ 926 919 for(i = 0; i < TX_BUFF_COUNT; ++i) { 927 uint32_t addr = PTR2U32(rtl8139->tx_buff_ mem.physical+ i*TX_BUFF_SIZE);920 uint32_t addr = PTR2U32(rtl8139->tx_buff_phys + i*TX_BUFF_SIZE); 928 921 pio_write_32(io_base + TSAD0 + 4*i, addr); 929 922 } … … 932 925 nic_set_tx_busy(rtl8139->nic_data, 0); 933 926 934 pio_write_32(io_base + RBSTART, PTR2U32(rtl8139->rx_buff .physical));927 pio_write_32(io_base + RBSTART, PTR2U32(rtl8139->rx_buff_phys)); 935 928 936 929 /* Enable transmitter and receiver */ … … 949 942 { 950 943 assert(nic_data); 951 nlog_info("Activating device");944 ddf_msg(LVL_NOTE, "Activating device"); 952 945 953 946 rtl8139_t *rtl8139 = nic_get_specific(nic_data); … … 962 955 nic_enable_interrupt(nic_data, rtl8139->irq); 963 956 964 nlog_debug("Device activated, interrupt %d registered", rtl8139->irq);957 ddf_msg(LVL_DEBUG, "Device activated, interrupt %d registered", rtl8139->irq); 965 958 return EOK; 966 959 } … … 1022 1015 rtl8139->nic_data = nic_data; 1023 1016 nic_set_specific(nic_data, rtl8139); 1024 nic_set_ write_packet_handler(nic_data, rtl8139_write_packet);1017 nic_set_send_frame_handler(nic_data, rtl8139_send_frame); 1025 1018 nic_set_state_change_handlers(nic_data, 1026 1019 rtl8139_on_activated, NULL, rtl8139_on_stopped); … … 1079 1072 1080 1073 if (hw_resources->irqs.count != 1) { 1081 nlog_error("%s device: unexpected irq count", dev->name);1074 ddf_msg(LVL_ERROR, "%s device: unexpected irq count", dev->name); 1082 1075 return EINVAL; 1083 1076 }; 1084 1077 if (hw_resources->io_ranges.count != 1) { 1085 nlog_error("%s device: unexpected io ranges count", dev->name);1078 ddf_msg(LVL_ERROR, "%s device: unexpected io ranges count", dev->name); 1086 1079 return EINVAL; 1087 1080 } 1088 1081 1089 1082 rtl8139->irq = hw_resources->irqs.irqs[0]; 1090 nlog_debug("%s device: irq 0x%x assigned", dev->name, rtl8139->irq);1083 ddf_msg(LVL_DEBUG, "%s device: irq 0x%x assigned", dev->name, rtl8139->irq); 1091 1084 1092 1085 rtl8139->io_addr = IOADDR_TO_PTR(hw_resources->io_ranges.ranges[0].address); 1093 1086 if (hw_resources->io_ranges.ranges[0].size < RTL8139_IO_SIZE) { 1094 nlog_error("i/o range assigned to the device "1087 ddf_msg(LVL_ERROR, "i/o range assigned to the device " 1095 1088 "%s is too small.", dev->name); 1096 1089 return EINVAL; 1097 1090 } 1098 nlog_debug("%s device: i/o addr %p assigned.", dev->name, rtl8139->io_addr);1091 ddf_msg(LVL_DEBUG, "%s device: i/o addr %p assigned.", dev->name, rtl8139->io_addr); 1099 1092 1100 1093 return EOK; … … 1142 1135 static int rtl8139_buffers_create(rtl8139_t *rtl8139) 1143 1136 { 1144 assert(rtl8139);1145 1137 size_t i = 0; 1146 1138 int rc; 1147 1139 1148 nlog_debug("Creating buffers"); 1149 1150 rtl8139->tx_buff_mem.size = TX_PAGES; 1151 rtl8139->tx_buff_mem.mapping_flags = AS_AREA_WRITE; 1152 rc = dma_allocate_anonymous(&rtl8139->tx_buff_mem, DMA_32_BITS); 1140 ddf_msg(LVL_DEBUG, "Creating buffers"); 1141 1142 rc = dmamem_map_anonymous(TX_PAGES * PAGE_SIZE, AS_AREA_WRITE, 0, 1143 &rtl8139->tx_buff_phys, &rtl8139->tx_buff_virt); 1153 1144 if (rc != EOK) { 1154 nlog_error("Can not allocate transmitter buffers.");1145 ddf_msg(LVL_ERROR, "Can not allocate transmitter buffers."); 1155 1146 goto err_tx_alloc; 1156 1147 } 1157 1148 1158 for (i = 0; i < TX_BUFF_COUNT; ++i)1159 rtl8139->tx_buff[i] = rtl8139->tx_buff_ mem.virtual+ i * TX_BUFF_SIZE;1160 1161 nlog_debug("The transmittion buffers allocated");1149 for (i = 0; i < TX_BUFF_COUNT; ++i) 1150 rtl8139->tx_buff[i] = rtl8139->tx_buff_virt + i * TX_BUFF_SIZE; 1151 1152 ddf_msg(LVL_DEBUG, "The transmittion buffers allocated"); 1162 1153 1163 1154 /* Use the first buffer for next transmittion */ … … 1166 1157 1167 1158 /* Allocate buffer for receiver */ 1168 size_t rx_pages = ALIGN_UP(RxBUF_TOT_LENGTH, PAGE_SIZE) / PAGE_SIZE; 1169 nlog_debug("Allocating receiver buffer of the size %zd pages", rx_pages); 1170 1171 rtl8139->rx_buff.size = rx_pages; 1172 rtl8139->rx_buff.mapping_flags = AS_AREA_READ; 1173 rc = dma_allocate_anonymous(&rtl8139->rx_buff, DMA_32_BITS); 1174 if( rc != EOK ) { 1175 nlog_error("Can not allocate receiver buffer."); 1159 ddf_msg(LVL_DEBUG, "Allocating receiver buffer of the size %zu bytes", 1160 RxBUF_TOT_LENGTH); 1161 1162 rc = dmamem_map_anonymous(RxBUF_TOT_LENGTH, AS_AREA_READ, 0, 1163 &rtl8139->rx_buff_phys, &rtl8139->rx_buff_virt); 1164 if (rc != EOK) { 1165 ddf_msg(LVL_ERROR, "Can not allocate receive buffer."); 1176 1166 goto err_rx_alloc; 1177 1167 } 1178 nlog_debug("The buffers created");1168 ddf_msg(LVL_DEBUG, "The buffers created"); 1179 1169 1180 1170 return EOK; 1181 1171 1182 1172 err_rx_alloc: 1183 dma _free(&rtl8139->tx_buff_mem);1173 dmamem_unmap_anonymous(&rtl8139->tx_buff_virt); 1184 1174 err_tx_alloc: 1185 1175 return rc; … … 1194 1184 static int rtl8139_device_initialize(ddf_dev_t *dev) 1195 1185 { 1196 nlog_debug("rtl8139_dev_initialize %s", dev->name);1186 ddf_msg(LVL_DEBUG, "rtl8139_dev_initialize %s", dev->name); 1197 1187 1198 1188 int ret = EOK; 1199 1189 1200 nlog_debug("rtl8139: creating device data");1190 ddf_msg(LVL_DEBUG, "rtl8139: creating device data"); 1201 1191 1202 1192 /* Allocate driver data for the device. */ 1203 1193 rtl8139_t *rtl8139 = rtl8139_create_dev_data(dev); 1204 1194 if (rtl8139 == NULL) { 1205 nlog_error("Not enough memory for initializing %s.", dev->name);1195 ddf_msg(LVL_ERROR, "Not enough memory for initializing %s.", dev->name); 1206 1196 return ENOMEM; 1207 1197 } 1208 1198 1209 nlog_debug("rtl8139: dev_data created");1199 ddf_msg(LVL_DEBUG, "rtl8139: dev_data created"); 1210 1200 1211 1201 /* Obtain and fill hardware resources info and connect to parent */ 1212 1202 ret = rtl8139_get_resource_info(dev); 1213 1203 if (ret != EOK) { 1214 nlog_error("Can not obatin hw resources information");1204 ddf_msg(LVL_ERROR, "Can not obatin hw resources information"); 1215 1205 goto failed; 1216 1206 } 1217 1207 1218 nlog_debug("rtl8139: resource_info obtained");1208 ddf_msg(LVL_DEBUG, "rtl8139: resource_info obtained"); 1219 1209 1220 1210 /* Allocate DMA buffers */ … … 1231 1221 rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT); 1232 1222 1233 nlog_debug("The device is initialized");1223 ddf_msg(LVL_DEBUG, "The device is initialized"); 1234 1224 return ret; 1235 1225 1236 1226 failed: 1237 nlog_error("The device initialization failed");1227 ddf_msg(LVL_ERROR, "The device initialization failed"); 1238 1228 rtl8139_dev_cleanup(dev); 1239 1229 return ret; … … 1248 1238 static int rtl8139_pio_enable(ddf_dev_t *dev) 1249 1239 { 1250 nlog_debug(NAME ": rtl8139_pio_enable %s", dev->name);1240 ddf_msg(LVL_DEBUG, NAME ": rtl8139_pio_enable %s", dev->name); 1251 1241 1252 1242 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_dev(dev)); … … 1254 1244 /* Gain control over port's registers. */ 1255 1245 if (pio_enable(rtl8139->io_addr, RTL8139_IO_SIZE, &rtl8139->io_port)) { 1256 nlog_error("Cannot gain the port %lx for device %s.", rtl8139->io_addr,1246 ddf_msg(LVL_ERROR, "Cannot gain the port %lx for device %s.", rtl8139->io_addr, 1257 1247 dev->name); 1258 1248 return EADDRNOTAVAIL; … … 1282 1272 if (rtl8139_versions[i].hwverid == hwverid) { 1283 1273 rtl8139->hw_version = rtl8139_versions[i].ver_id; 1284 nlog_info("HW version found: index %zu, ver_id %d (%s)", i,1274 ddf_msg(LVL_NOTE, "HW version found: index %zu, ver_id %d (%s)", i, 1285 1275 rtl8139_versions[i].ver_id, model_names[rtl8139->hw_version]); 1286 1276 } … … 1296 1286 * @return EOK if added successfully, negative error code otherwise 1297 1287 */ 1298 int rtl8139_ add_device(ddf_dev_t *dev)1288 int rtl8139_dev_add(ddf_dev_t *dev) 1299 1289 { 1300 1290 assert(dev); 1301 nlog_info("RTL8139_add_device%s (handle = %d)", dev->name, dev->handle);1291 ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle); 1302 1292 1303 1293 /* Init device structure for rtl8139 */ … … 1330 1320 rc = nic_connect_to_services(nic_data); 1331 1321 if (rc != EOK) { 1332 nlog_error("Failed to connect to services", rc);1322 ddf_msg(LVL_ERROR, "Failed to connect to services", rc); 1333 1323 goto err_irq; 1334 1324 } … … 1336 1326 rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops); 1337 1327 if (rc != EOK) { 1338 nlog_error("Failed to register as DDF function - error %d", rc);1328 ddf_msg(LVL_ERROR, "Failed to register as DDF function - error %d", rc); 1339 1329 goto err_irq; 1340 1330 } 1341 1331 1342 nlog_info("The %s device has been successfully initialized.",1332 ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.", 1343 1333 dev->name); 1344 1334 … … 1581 1571 1582 1572 if (allow_send && time > 0) { 1583 nlog_warning("Time setting is not supported in set_pause method.");1573 ddf_msg(LVL_WARN, "Time setting is not supported in set_pause method."); 1584 1574 } 1585 1575 return EOK; … … 2121 2111 pio_write_32(rtl8139->io_port + TCTR, 0); 2122 2112 2123 nlog_debug("Periodic mode. Interrupt mask %"PRIx16", poll.full_skips %"2113 ddf_msg(LVL_DEBUG, "Periodic mode. Interrupt mask %"PRIx16", poll.full_skips %" 2124 2114 PRIu32", last timer %"PRIu32".", rtl8139->int_mask, 2125 2115 rtl8139->poll_timer.full_skips, rtl8139->poll_timer.last_val); … … 2168 2158 &rtl8139_driver_ops, &rtl8139_dev_ops, &rtl8139_nic_iface); 2169 2159 2170 nlog_set_min_severity(DEBUG);2171 nlog_info("HelenOS RTL8139 driver started");2160 ddf_log_init(NAME, LVL_ERROR); 2161 ddf_msg(LVL_NOTE, "HelenOS RTL8139 driver started"); 2172 2162 return ddf_driver_main(&rtl8139_driver); 2173 2163 }
Note:
See TracChangeset
for help on using the changeset viewer.