| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Michalec
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <assert.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <align.h>
|
|---|
| 32 | #include <byteorder.h>
|
|---|
| 33 | #include <libarch/barrier.h>
|
|---|
| 34 | #include <as.h>
|
|---|
| 35 | #include <ddf/log.h>
|
|---|
| 36 | #include <ddf/interrupt.h>
|
|---|
| 37 | #include <io/log.h>
|
|---|
| 38 | #include <nic.h>
|
|---|
| 39 | #include <pci_dev_iface.h>
|
|---|
| 40 | #include <irc.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "defs.h"
|
|---|
| 45 | #include "driver.h"
|
|---|
| 46 | #include "general.h"
|
|---|
| 47 |
|
|---|
| 48 | /** Global mutex for work with shared irq structure */
|
|---|
| 49 | FIBRIL_MUTEX_INITIALIZE(irq_reg_lock);
|
|---|
| 50 |
|
|---|
| 51 | /** Lock interrupt structure mutex */
|
|---|
| 52 | #define RTL8139_IRQ_STRUCT_LOCK() \
|
|---|
| 53 | fibril_mutex_lock(&irq_reg_lock)
|
|---|
| 54 |
|
|---|
| 55 | /** Unlock interrupt structure mutex */
|
|---|
| 56 | #define RTL8139_IRQ_STRUCT_UNLOCK() \
|
|---|
| 57 | fibril_mutex_unlock(&irq_reg_lock)
|
|---|
| 58 |
|
|---|
| 59 | /** PCI clock frequency in kHz */
|
|---|
| 60 | #define RTL8139_PCI_FREQ_KHZ 33000
|
|---|
| 61 |
|
|---|
| 62 | #define RTL8139_AUTONEG_CAPS (ETH_AUTONEG_10BASE_T_HALF | \
|
|---|
| 63 | ETH_AUTONEG_10BASE_T_FULL | ETH_AUTONEG_100BASE_TX_HALF | \
|
|---|
| 64 | ETH_AUTONEG_100BASE_TX_FULL | ETH_AUTONEG_PAUSE_SYMETRIC)
|
|---|
| 65 |
|
|---|
| 66 | /** Lock transmitter and receiver data
|
|---|
| 67 | *
|
|---|
| 68 | * This function shall be called whenever
|
|---|
| 69 | * both transmitter and receiver locking
|
|---|
| 70 | * to force safe lock ordering (deadlock prevention)
|
|---|
| 71 | *
|
|---|
| 72 | * @param rtl8139 RTL8139 private data
|
|---|
| 73 | *
|
|---|
| 74 | */
|
|---|
| 75 | inline static void rtl8139_lock_all(rtl8139_t *rtl8139)
|
|---|
| 76 | {
|
|---|
| 77 | assert(rtl8139);
|
|---|
| 78 | fibril_mutex_lock(&rtl8139->tx_lock);
|
|---|
| 79 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /** Unlock transmitter and receiver data
|
|---|
| 83 | *
|
|---|
| 84 | * @param rtl8139 RTL8139 private data
|
|---|
| 85 | *
|
|---|
| 86 | */
|
|---|
| 87 | inline static void rtl8139_unlock_all(rtl8139_t *rtl8139)
|
|---|
| 88 | {
|
|---|
| 89 | assert(rtl8139);
|
|---|
| 90 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 91 | fibril_mutex_unlock(&rtl8139->tx_lock);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | #ifndef RXBUF_SIZE_FLAGS
|
|---|
| 95 | /** Flags for receiver buffer - 16kB default */
|
|---|
| 96 | #define RXBUF_SIZE_FLAGS RTL8139_RXFLAGS_SIZE_16
|
|---|
| 97 | #endif
|
|---|
| 98 |
|
|---|
| 99 | #if (RXBUF_SIZE_FLAGS > RTL8139_RXFLAGS_SIZE_64) || (RXBUF_SIZE_FLAGS < 0)
|
|---|
| 100 | #error Bad receiver buffer flags size flags
|
|---|
| 101 | #endif
|
|---|
| 102 |
|
|---|
| 103 | /** Size of the receiver buffer
|
|---|
| 104 | *
|
|---|
| 105 | * Incrementing flags by one twices the buffer size
|
|---|
| 106 | * the lowest size is 8*1024 (flags = 0)
|
|---|
| 107 | */
|
|---|
| 108 | #define RxBUF_SIZE RTL8139_RXSIZE(RXBUF_SIZE_FLAGS)
|
|---|
| 109 |
|
|---|
| 110 | /** Total size of the receiver buffer to allocate */
|
|---|
| 111 | #define RxBUF_TOT_LENGTH RTL8139_RXBUF_LENGTH(RXBUF_SIZE_FLAGS)
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | /** Default interrupt mask */
|
|---|
| 115 | #define RTL_DEFAULT_INTERRUPTS UINT16_C(0xFFFF)
|
|---|
| 116 |
|
|---|
| 117 | /** Obtain the value of the register part
|
|---|
| 118 | * The bit operations will be done
|
|---|
| 119 | * The _SHIFT and _MASK for the register part must exists as macros
|
|---|
| 120 | * or variables
|
|---|
| 121 | */
|
|---|
| 122 | #define REG_GET_VAL(value, reg_part)\
|
|---|
| 123 | (((value) >> reg_part##_SHIFT) & reg_part##_MASK)
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | /** Disable interrupts on controller
|
|---|
| 127 | *
|
|---|
| 128 | * @param rtl8139 The card private structure
|
|---|
| 129 | */
|
|---|
| 130 | inline static void rtl8139_hw_int_disable(rtl8139_t *rtl8139)
|
|---|
| 131 | {
|
|---|
| 132 | pio_write_16(rtl8139->io_port + IMR, 0x0);
|
|---|
| 133 | }
|
|---|
| 134 | /** Enable interrupts on controller
|
|---|
| 135 | *
|
|---|
| 136 | * @param rtl8139 The card private structure
|
|---|
| 137 | */
|
|---|
| 138 | inline static void rtl8139_hw_int_enable(rtl8139_t *rtl8139)
|
|---|
| 139 | {
|
|---|
| 140 | pio_write_16(rtl8139->io_port + IMR, rtl8139->int_mask);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Check on the controller if the receiving buffer is empty
|
|---|
| 144 | *
|
|---|
| 145 | * @param rtl8139 The controller data
|
|---|
| 146 | *
|
|---|
| 147 | * @return Nonzero if empty, zero otherwise
|
|---|
| 148 | */
|
|---|
| 149 | inline static int rtl8139_hw_buffer_empty(rtl8139_t *rtl8139)
|
|---|
| 150 | {
|
|---|
| 151 | return pio_read_16(rtl8139->io_port + CR) & CR_BUFE;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** Update the mask of accepted frames in the RCR register according to
|
|---|
| 155 | * rcr_accept_mode value in rtl8139_t
|
|---|
| 156 | *
|
|---|
| 157 | * @param rtl8139 The rtl8139 private data
|
|---|
| 158 | */
|
|---|
| 159 | static void rtl8139_hw_update_rcr(rtl8139_t *rtl8139)
|
|---|
| 160 | {
|
|---|
| 161 | uint32_t rcr = rtl8139->rcr_data.rcr_base | rtl8139->rcr_data.ucast_mask
|
|---|
| 162 | | rtl8139->rcr_data.mcast_mask | rtl8139->rcr_data.bcast_mask
|
|---|
| 163 | | rtl8139->rcr_data.defect_mask |
|
|---|
| 164 | (RXBUF_SIZE_FLAGS << RCR_RBLEN_SHIFT);
|
|---|
| 165 |
|
|---|
| 166 | ddf_msg(LVL_DEBUG, "Rewriting rcr: %x -> %x", pio_read_32(rtl8139->io_port + RCR),
|
|---|
| 167 | rcr);
|
|---|
| 168 |
|
|---|
| 169 | pio_write_32(rtl8139->io_port + RCR, rcr);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Fill the mask of accepted multicast frames in the card registers
|
|---|
| 173 | *
|
|---|
| 174 | * @param rtl8139 The rtl8139 private data
|
|---|
| 175 | * @param mask The mask to set
|
|---|
| 176 | */
|
|---|
| 177 | inline static void rtl8139_hw_set_mcast_mask(rtl8139_t *rtl8139,
|
|---|
| 178 | uint64_t mask)
|
|---|
| 179 | {
|
|---|
| 180 | pio_write_32(rtl8139->io_port + MAR0, (uint32_t) mask);
|
|---|
| 181 | pio_write_32(rtl8139->io_port + MAR0 + sizeof(uint32_t),
|
|---|
| 182 | (uint32_t)(mask >> 32));
|
|---|
| 183 | return;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Set PmEn (Power management enable) bit value
|
|---|
| 187 | *
|
|---|
| 188 | * @param rtl8139 rtl8139 card data
|
|---|
| 189 | * @param bit_val If bit_val is zero pmen is set to 0, otherwise pmen is set to 1
|
|---|
| 190 | */
|
|---|
| 191 | inline static void rtl8139_hw_pmen_set(rtl8139_t *rtl8139, uint8_t bit_val)
|
|---|
| 192 | {
|
|---|
| 193 | uint8_t config1 = pio_read_8(rtl8139->io_port + CONFIG1);
|
|---|
| 194 | uint8_t config1_new;
|
|---|
| 195 | if (bit_val)
|
|---|
| 196 | config1_new = config1 | CONFIG1_PMEn;
|
|---|
| 197 | else
|
|---|
| 198 | config1_new = config1 & ~(uint8_t)(CONFIG1_PMEn);
|
|---|
| 199 |
|
|---|
| 200 | if (config1_new == config1)
|
|---|
| 201 | return;
|
|---|
| 202 |
|
|---|
| 203 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 204 | pio_write_8(rtl8139->io_port + CONFIG1, config1_new);
|
|---|
| 205 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 206 |
|
|---|
| 207 | async_sess_t *pci_sess =
|
|---|
| 208 | ddf_dev_parent_sess_get(nic_get_ddf_dev(rtl8139->nic_data));
|
|---|
| 209 |
|
|---|
| 210 | if (bit_val) {
|
|---|
| 211 | uint8_t pmen;
|
|---|
| 212 | pci_config_space_read_8(pci_sess, 0x55, &pmen);
|
|---|
| 213 | pci_config_space_write_8(pci_sess, 0x55, pmen | 1 | (1 << 7));
|
|---|
| 214 | } else {
|
|---|
| 215 | uint8_t pmen;
|
|---|
| 216 | pci_config_space_read_8(pci_sess, 0x55, &pmen);
|
|---|
| 217 | pci_config_space_write_8(pci_sess, 0x55, pmen & ~(1 | (1 << 7)));
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /** Get MAC address of the RTL8139 adapter
|
|---|
| 222 | *
|
|---|
| 223 | * @param rtl8139 The RTL8139 device
|
|---|
| 224 | * @param address The place to store the address
|
|---|
| 225 | *
|
|---|
| 226 | * @return EOK if succeed, negative error code otherwise
|
|---|
| 227 | */
|
|---|
| 228 | inline static void rtl8139_hw_get_addr(rtl8139_t *rtl8139,
|
|---|
| 229 | nic_address_t *addr)
|
|---|
| 230 | {
|
|---|
| 231 | assert(rtl8139);
|
|---|
| 232 | assert(addr);
|
|---|
| 233 |
|
|---|
| 234 | uint32_t *mac0_dest = (uint32_t *)addr->address;
|
|---|
| 235 | uint16_t *mac4_dest = (uint16_t *)(addr->address + 4);
|
|---|
| 236 |
|
|---|
| 237 | /* Read MAC address from the i/o (4byte + 2byte reads) */
|
|---|
| 238 | *mac0_dest = pio_read_32(rtl8139->io_port + MAC0);
|
|---|
| 239 | *mac4_dest = pio_read_16(rtl8139->io_port + MAC0 + 4);
|
|---|
| 240 | };
|
|---|
| 241 |
|
|---|
| 242 | /** Set MAC address to the device
|
|---|
| 243 | *
|
|---|
| 244 | * @param rtl8139 Controller private structure
|
|---|
| 245 | * @param addr The address to set
|
|---|
| 246 | */
|
|---|
| 247 | static void rtl8139_hw_set_addr(rtl8139_t *rtl8139, const nic_address_t *addr)
|
|---|
| 248 | {
|
|---|
| 249 | assert(rtl8139);
|
|---|
| 250 | assert(addr);
|
|---|
| 251 |
|
|---|
| 252 | const uint32_t *val1 = (const uint32_t*)addr->address;
|
|---|
| 253 | const uint16_t *val2 = (const uint16_t*)(addr->address + sizeof(uint32_t));
|
|---|
| 254 |
|
|---|
| 255 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 256 | pio_write_32(rtl8139->io_port + MAC0, *val1);
|
|---|
| 257 | pio_write_32(rtl8139->io_port + MAC0 + 4, *val2);
|
|---|
| 258 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /** Provide OR in the 8bit register (set selected bits to 1)
|
|---|
| 262 | *
|
|---|
| 263 | * @param rtl8139 The rtl8139 structure
|
|---|
| 264 | * @param reg_offset Register offset in the device IO space
|
|---|
| 265 | * @param bits_add The value to or
|
|---|
| 266 | */
|
|---|
| 267 | inline static void rtl8139_hw_reg_add_8(rtl8139_t * rtl8139, size_t reg_offset,
|
|---|
| 268 | uint8_t bits_add)
|
|---|
| 269 | {
|
|---|
| 270 | uint8_t value = pio_read_8(rtl8139->io_port + reg_offset);
|
|---|
| 271 | value |= bits_add;
|
|---|
| 272 | pio_write_8(rtl8139->io_port + reg_offset, value);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Provide OR in the 32bit register (set selected bits to 1)
|
|---|
| 276 | *
|
|---|
| 277 | * @param rtl8139 The rtl8139 structure
|
|---|
| 278 | * @param reg_offset Register offset in the device IO space
|
|---|
| 279 | * @param bits_add The value to or
|
|---|
| 280 | */
|
|---|
| 281 | inline static void rtl8139_hw_reg_add_32(rtl8139_t * rtl8139, size_t reg_offset,
|
|---|
| 282 | uint32_t bits_add)
|
|---|
| 283 | {
|
|---|
| 284 | uint32_t value = pio_read_32(rtl8139->io_port + reg_offset);
|
|---|
| 285 | value |= bits_add;
|
|---|
| 286 | pio_write_32(rtl8139->io_port + reg_offset, value);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /** Unset selected bits in 8bit register
|
|---|
| 290 | *
|
|---|
| 291 | * @param rtl8139 The rtl8139 structure
|
|---|
| 292 | * @param reg_offset Register offset in the device IO space
|
|---|
| 293 | * @param bits_add The mask of bits to remove
|
|---|
| 294 | */
|
|---|
| 295 | inline static void rtl8139_hw_reg_rem_8(rtl8139_t * rtl8139, size_t reg_offset,
|
|---|
| 296 | uint8_t bits_add)
|
|---|
| 297 | {
|
|---|
| 298 | uint8_t value = pio_read_8(rtl8139->io_port + reg_offset);
|
|---|
| 299 | value &= ~bits_add;
|
|---|
| 300 | pio_write_8(rtl8139->io_port + reg_offset, value);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /** Unset selected bits in 32bit register
|
|---|
| 304 | *
|
|---|
| 305 | * @param rtl8139 The rtl8139 structure
|
|---|
| 306 | * @param reg_offset Register offset in the device IO space
|
|---|
| 307 | * @param bits_add The mask of bits to remove
|
|---|
| 308 | */
|
|---|
| 309 | inline static void rtl8139_hw_reg_rem_32(rtl8139_t * rtl8139, size_t reg_offset,
|
|---|
| 310 | uint32_t bits_add)
|
|---|
| 311 | {
|
|---|
| 312 | uint32_t value = pio_read_32(rtl8139->io_port + reg_offset);
|
|---|
| 313 | value &= ~bits_add;
|
|---|
| 314 | pio_write_32(rtl8139->io_port + reg_offset, value);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 | static int rtl8139_set_addr(ddf_fun_t *fun, const nic_address_t *);
|
|---|
| 319 | static int rtl8139_get_device_info(ddf_fun_t *fun, nic_device_info_t *info);
|
|---|
| 320 | static int rtl8139_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state);
|
|---|
| 321 | static int rtl8139_get_operation_mode(ddf_fun_t *fun, int *speed,
|
|---|
| 322 | nic_channel_mode_t *duplex, nic_role_t *role);
|
|---|
| 323 | static int rtl8139_set_operation_mode(ddf_fun_t *fun, int speed,
|
|---|
| 324 | nic_channel_mode_t duplex, nic_role_t);
|
|---|
| 325 |
|
|---|
| 326 | static int rtl8139_pause_get(ddf_fun_t*, nic_result_t*, nic_result_t*,
|
|---|
| 327 | uint16_t *);
|
|---|
| 328 | static int rtl8139_pause_set(ddf_fun_t*, int, int, uint16_t);
|
|---|
| 329 |
|
|---|
| 330 | static int rtl8139_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement);
|
|---|
| 331 | static int rtl8139_autoneg_disable(ddf_fun_t *fun);
|
|---|
| 332 | static int rtl8139_autoneg_probe(ddf_fun_t *fun, uint32_t *our_advertisement,
|
|---|
| 333 | uint32_t *their_advertisement, nic_result_t *result,
|
|---|
| 334 | nic_result_t *their_result);
|
|---|
| 335 | static int rtl8139_autoneg_restart(ddf_fun_t *fun);
|
|---|
| 336 |
|
|---|
| 337 | static int rtl8139_defective_get_mode(ddf_fun_t *fun, uint32_t *mode);
|
|---|
| 338 | static int rtl8139_defective_set_mode(ddf_fun_t *fun, uint32_t mode);
|
|---|
| 339 |
|
|---|
| 340 | static int rtl8139_wol_virtue_add(nic_t *nic_data,
|
|---|
| 341 | const nic_wol_virtue_t *virtue);
|
|---|
| 342 | static void rtl8139_wol_virtue_rem(nic_t *nic_data,
|
|---|
| 343 | const nic_wol_virtue_t *virtue);
|
|---|
| 344 |
|
|---|
| 345 | static int rtl8139_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
|
|---|
| 346 | const struct timeval *period);
|
|---|
| 347 | static void rtl8139_poll(nic_t *nic_data);
|
|---|
| 348 |
|
|---|
| 349 | /** Network interface options for RTL8139 card driver */
|
|---|
| 350 | static nic_iface_t rtl8139_nic_iface = {
|
|---|
| 351 | .set_address = &rtl8139_set_addr,
|
|---|
| 352 | .get_device_info = &rtl8139_get_device_info,
|
|---|
| 353 | .get_cable_state = &rtl8139_get_cable_state,
|
|---|
| 354 | .get_operation_mode = &rtl8139_get_operation_mode,
|
|---|
| 355 | .set_operation_mode = &rtl8139_set_operation_mode,
|
|---|
| 356 |
|
|---|
| 357 | .get_pause = &rtl8139_pause_get,
|
|---|
| 358 | .set_pause = &rtl8139_pause_set,
|
|---|
| 359 |
|
|---|
| 360 | .autoneg_enable = &rtl8139_autoneg_enable,
|
|---|
| 361 | .autoneg_disable = &rtl8139_autoneg_disable,
|
|---|
| 362 | .autoneg_probe = &rtl8139_autoneg_probe,
|
|---|
| 363 | .autoneg_restart = &rtl8139_autoneg_restart,
|
|---|
| 364 |
|
|---|
| 365 | .defective_get_mode = &rtl8139_defective_get_mode,
|
|---|
| 366 | .defective_set_mode = &rtl8139_defective_set_mode,
|
|---|
| 367 | };
|
|---|
| 368 |
|
|---|
| 369 | /** Basic device operations for RTL8139 driver */
|
|---|
| 370 | static ddf_dev_ops_t rtl8139_dev_ops;
|
|---|
| 371 |
|
|---|
| 372 | static int rtl8139_dev_add(ddf_dev_t *dev);
|
|---|
| 373 |
|
|---|
| 374 | /** Basic driver operations for RTL8139 driver */
|
|---|
| 375 | static driver_ops_t rtl8139_driver_ops = {
|
|---|
| 376 | .dev_add = &rtl8139_dev_add,
|
|---|
| 377 | };
|
|---|
| 378 |
|
|---|
| 379 | /** Driver structure for RTL8139 driver */
|
|---|
| 380 | static driver_t rtl8139_driver = {
|
|---|
| 381 | .name = NAME,
|
|---|
| 382 | .driver_ops = &rtl8139_driver_ops
|
|---|
| 383 | };
|
|---|
| 384 |
|
|---|
| 385 | /* The default implementation callbacks */
|
|---|
| 386 | static int rtl8139_on_activated(nic_t *nic_data);
|
|---|
| 387 | static int rtl8139_on_stopped(nic_t *nic_data);
|
|---|
| 388 | static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size);
|
|---|
| 389 |
|
|---|
| 390 | /** Check if the transmit buffer is busy */
|
|---|
| 391 | #define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0)
|
|---|
| 392 |
|
|---|
| 393 | /** Send frame with the hardware
|
|---|
| 394 | *
|
|---|
| 395 | * note: the main_lock is locked when framework calls this function
|
|---|
| 396 | *
|
|---|
| 397 | * @param nic_data The nic driver data structure
|
|---|
| 398 | * @param data Frame data
|
|---|
| 399 | * @param size Frame size in bytes
|
|---|
| 400 | *
|
|---|
| 401 | * @return EOK if succeed, error code in the case of error
|
|---|
| 402 | */
|
|---|
| 403 | static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size)
|
|---|
| 404 | {
|
|---|
| 405 | assert(nic_data);
|
|---|
| 406 |
|
|---|
| 407 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 408 | assert(rtl8139);
|
|---|
| 409 | ddf_msg(LVL_DEBUG, "Sending frame");
|
|---|
| 410 |
|
|---|
| 411 | if (size > RTL8139_FRAME_MAX_LENGTH) {
|
|---|
| 412 | ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes",
|
|---|
| 413 | size);
|
|---|
| 414 | nic_report_send_error(rtl8139->nic_data, NIC_SEC_OTHER, 1);
|
|---|
| 415 | goto err_size;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | assert((size & TSD_SIZE_MASK) == size);
|
|---|
| 419 |
|
|---|
| 420 | /* Lock transmitter structure for obtaining next buffer */
|
|---|
| 421 | fibril_mutex_lock(&rtl8139->tx_lock);
|
|---|
| 422 |
|
|---|
| 423 | /* Check if there is free buffer */
|
|---|
| 424 | if (rtl8139->tx_next - TX_BUFF_COUNT == rtl8139->tx_used) {
|
|---|
| 425 | nic_set_tx_busy(nic_data, 1);
|
|---|
| 426 | fibril_mutex_unlock(&rtl8139->tx_lock);
|
|---|
| 427 | nic_report_send_error(nic_data, NIC_SEC_BUFFER_FULL, 1);
|
|---|
| 428 | goto err_busy_no_inc;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /* Get buffer id to use and set next buffer to use */
|
|---|
| 432 | size_t tx_curr = rtl8139->tx_next++ % TX_BUFF_COUNT;
|
|---|
| 433 |
|
|---|
| 434 | fibril_mutex_unlock(&rtl8139->tx_lock);
|
|---|
| 435 |
|
|---|
| 436 | /* Get address of the buffer descriptor and frame data */
|
|---|
| 437 | void *tsd = rtl8139->io_port + TSD0 + tx_curr * 4;
|
|---|
| 438 | void *buf_addr = rtl8139->tx_buff[tx_curr];
|
|---|
| 439 |
|
|---|
| 440 | /* Wait until the buffer is free */
|
|---|
| 441 | assert(!rtl8139_tbuf_busy(tsd));
|
|---|
| 442 |
|
|---|
| 443 | /* Write frame data to the buffer, set the size to TSD and clear OWN bit */
|
|---|
| 444 | memcpy(buf_addr, data, size);
|
|---|
| 445 |
|
|---|
| 446 | /* Set size of the data to send */
|
|---|
| 447 | uint32_t tsd_value = pio_read_32(tsd);
|
|---|
| 448 | tsd_value = rtl8139_tsd_set_size(tsd_value, size);
|
|---|
| 449 | pio_write_32(tsd, tsd_value);
|
|---|
| 450 |
|
|---|
| 451 | /* barrier for HW to really see the current buffer data */
|
|---|
| 452 | write_barrier();
|
|---|
| 453 |
|
|---|
| 454 | tsd_value &= ~(uint32_t)TSD_OWN;
|
|---|
| 455 | pio_write_32(tsd, tsd_value);
|
|---|
| 456 | return;
|
|---|
| 457 |
|
|---|
| 458 | err_busy_no_inc:
|
|---|
| 459 | err_size:
|
|---|
| 460 | return;
|
|---|
| 461 | };
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 | /** Reset the controller
|
|---|
| 465 | *
|
|---|
| 466 | * @param io_base The address of the i/o port mapping start
|
|---|
| 467 | */
|
|---|
| 468 | inline static void rtl8139_hw_soft_reset(void *io_base)
|
|---|
| 469 | {
|
|---|
| 470 | pio_write_8(io_base + CR, CR_RST);
|
|---|
| 471 | memory_barrier();
|
|---|
| 472 | while(pio_read_8(io_base + CR) & CR_RST) {
|
|---|
| 473 | usleep(1);
|
|---|
| 474 | read_barrier();
|
|---|
| 475 | }
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | /** Provide soft reset of the controller
|
|---|
| 479 | *
|
|---|
| 480 | * The caller must lock tx_lock and rx_lock before calling this function
|
|---|
| 481 | *
|
|---|
| 482 | */
|
|---|
| 483 | static void rtl8139_soft_reset(rtl8139_t *rtl8139)
|
|---|
| 484 | {
|
|---|
| 485 | assert(rtl8139);
|
|---|
| 486 |
|
|---|
| 487 | rtl8139_hw_soft_reset(rtl8139->io_port);
|
|---|
| 488 | nic_t *nic_data = rtl8139->nic_data;
|
|---|
| 489 |
|
|---|
| 490 | /* Write MAC address to the card */
|
|---|
| 491 | nic_address_t addr;
|
|---|
| 492 | nic_query_address(nic_data, &addr);
|
|---|
| 493 | rtl8139_hw_set_addr(rtl8139, &addr);
|
|---|
| 494 |
|
|---|
| 495 | /* Recover accept modes back */
|
|---|
| 496 | rtl8139_hw_set_mcast_mask(rtl8139, nic_query_mcast_hash(nic_data));
|
|---|
| 497 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 498 |
|
|---|
| 499 | rtl8139->tx_used = 0;
|
|---|
| 500 | rtl8139->tx_next = 0;
|
|---|
| 501 | nic_set_tx_busy(rtl8139->nic_data, 0);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /** Create frame structure from the buffer data
|
|---|
| 505 | *
|
|---|
| 506 | * @param nic_data NIC driver data
|
|---|
| 507 | * @param rx_buffer The receiver buffer
|
|---|
| 508 | * @param rx_size The buffer size
|
|---|
| 509 | * @param frame_start The offset where packet data start
|
|---|
| 510 | * @param frame_size The size of the frame data
|
|---|
| 511 | *
|
|---|
| 512 | * @return The frame list node (not connected)
|
|---|
| 513 | *
|
|---|
| 514 | */
|
|---|
| 515 | static nic_frame_t *rtl8139_read_frame(nic_t *nic_data,
|
|---|
| 516 | void *rx_buffer, size_t rx_size, size_t frame_start, size_t frame_size)
|
|---|
| 517 | {
|
|---|
| 518 | nic_frame_t *frame = nic_alloc_frame(nic_data, frame_size);
|
|---|
| 519 | if (! frame) {
|
|---|
| 520 | ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");
|
|---|
| 521 | return NULL;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start,
|
|---|
| 525 | RxBUF_SIZE, frame_size);
|
|---|
| 526 | if (ret == NULL) {
|
|---|
| 527 | nic_release_frame(nic_data, frame);
|
|---|
| 528 | return NULL;
|
|---|
| 529 | }
|
|---|
| 530 | return frame;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /* Reset receiver
|
|---|
| 534 | *
|
|---|
| 535 | * Use in the case of receiver error (lost in the rx_buff)
|
|---|
| 536 | *
|
|---|
| 537 | * @param rtl8139 controller private data
|
|---|
| 538 | */
|
|---|
| 539 | static void rtl8139_rx_reset(rtl8139_t *rtl8139)
|
|---|
| 540 | {
|
|---|
| 541 | /* Disable receiver, update offset and enable receiver again */
|
|---|
| 542 | uint8_t cr = pio_read_8(rtl8139->io_port + CR);
|
|---|
| 543 | rtl8139_regs_unlock(rtl8139);
|
|---|
| 544 |
|
|---|
| 545 | pio_write_8(rtl8139->io_port + CR, cr & ~(uint8_t)CR_RE);
|
|---|
| 546 |
|
|---|
| 547 | write_barrier();
|
|---|
| 548 | pio_write_32(rtl8139->io_port + CAPR, 0);
|
|---|
| 549 | pio_write_32(rtl8139->io_port + RBSTART,
|
|---|
| 550 | PTR2U32(rtl8139->rx_buff_phys));
|
|---|
| 551 |
|
|---|
| 552 | write_barrier();
|
|---|
| 553 |
|
|---|
| 554 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 555 | pio_write_8(rtl8139->io_port + CR, cr);
|
|---|
| 556 | rtl8139_regs_lock(rtl8139);
|
|---|
| 557 |
|
|---|
| 558 | nic_report_receive_error(rtl8139->nic_data, NIC_REC_OTHER, 1);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | /** Receive all frames in queue
|
|---|
| 562 | *
|
|---|
| 563 | * @param nic_data The controller data
|
|---|
| 564 | * @return The linked list of nic_frame_list_t nodes, each containing one frame
|
|---|
| 565 | */
|
|---|
| 566 | static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data)
|
|---|
| 567 | {
|
|---|
| 568 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 569 | if (rtl8139_hw_buffer_empty(rtl8139))
|
|---|
| 570 | return NULL;
|
|---|
| 571 |
|
|---|
| 572 | nic_frame_list_t *frames = nic_alloc_frame_list();
|
|---|
| 573 | if (!frames)
|
|---|
| 574 | ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames.");
|
|---|
| 575 |
|
|---|
| 576 | void *rx_buffer = rtl8139->rx_buff_virt;
|
|---|
| 577 |
|
|---|
| 578 | /* where to start reading */
|
|---|
| 579 | uint16_t rx_offset = pio_read_16(rtl8139->io_port + CAPR) + 16;
|
|---|
| 580 | /* unread bytes count */
|
|---|
| 581 | uint16_t bytes_received = pio_read_16(rtl8139->io_port + CBA);
|
|---|
| 582 | uint16_t max_read;
|
|---|
| 583 | uint16_t cur_read = 0;
|
|---|
| 584 |
|
|---|
| 585 | /* get values to the <0, buffer size) */
|
|---|
| 586 | bytes_received %= RxBUF_SIZE;
|
|---|
| 587 | rx_offset %= RxBUF_SIZE;
|
|---|
| 588 |
|
|---|
| 589 | /* count how many bytes to read maximaly */
|
|---|
| 590 | if (bytes_received < rx_offset)
|
|---|
| 591 | max_read = bytes_received + (RxBUF_SIZE - rx_offset);
|
|---|
| 592 | else
|
|---|
| 593 | max_read = bytes_received - rx_offset;
|
|---|
| 594 |
|
|---|
| 595 | memory_barrier();
|
|---|
| 596 | while (!rtl8139_hw_buffer_empty(rtl8139)) {
|
|---|
| 597 | void *rx_ptr = rx_buffer + rx_offset % RxBUF_SIZE;
|
|---|
| 598 | uint32_t frame_header = uint32_t_le2host( *((uint32_t*)rx_ptr) );
|
|---|
| 599 | uint16_t size = frame_header >> 16;
|
|---|
| 600 | uint16_t frame_size = size - RTL8139_CRC_SIZE;
|
|---|
| 601 | /* received frame flags in frame header */
|
|---|
| 602 | uint16_t rcs = (uint16_t) frame_header;
|
|---|
| 603 |
|
|---|
| 604 | if (size == RTL8139_EARLY_SIZE) {
|
|---|
| 605 | /* The frame copying is still in progress, break receiving */
|
|---|
| 606 | ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied");
|
|---|
| 607 | break;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /* Check if the header is valid, otherwise we are lost in the buffer */
|
|---|
| 611 | if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) {
|
|---|
| 612 | ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4" PRIu16 ", "
|
|---|
| 613 | "header 0x%4" PRIx32 ". Offset: %" PRIu16 ")", size, frame_header,
|
|---|
| 614 | rx_offset);
|
|---|
| 615 | goto rx_err;
|
|---|
| 616 | }
|
|---|
| 617 | if (size < RTL8139_RUNT_MAX_SIZE && !(rcs & RSR_RUNT)) {
|
|---|
| 618 | ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (%"PRIx16")", size);
|
|---|
| 619 | goto rx_err;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | cur_read += size + RTL_FRAME_HEADER_SIZE;
|
|---|
| 623 | if (cur_read > max_read)
|
|---|
| 624 | break;
|
|---|
| 625 |
|
|---|
| 626 | if (frames) {
|
|---|
| 627 | nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer,
|
|---|
| 628 | RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size);
|
|---|
| 629 |
|
|---|
| 630 | if (frame)
|
|---|
| 631 | nic_frame_list_append(frames, frame);
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | /* Update offset */
|
|---|
| 635 | rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4);
|
|---|
| 636 |
|
|---|
| 637 | /* Write lesser value to prevent overflow into unread frame
|
|---|
| 638 | * (the recomendation from the RealTech rtl8139 programming guide)
|
|---|
| 639 | */
|
|---|
| 640 | uint16_t capr_val = rx_offset - 16;
|
|---|
| 641 | pio_write_16(rtl8139->io_port + CAPR, capr_val);
|
|---|
| 642 |
|
|---|
| 643 | /* Ensure no CR read optimalization during next empty buffer test */
|
|---|
| 644 | memory_barrier();
|
|---|
| 645 | }
|
|---|
| 646 | return frames;
|
|---|
| 647 | rx_err:
|
|---|
| 648 | rtl8139_rx_reset(rtl8139);
|
|---|
| 649 | return frames;
|
|---|
| 650 | };
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 | irq_pio_range_t rtl8139_irq_pio_ranges[] = {
|
|---|
| 654 | {
|
|---|
| 655 | .base = 0,
|
|---|
| 656 | .size = RTL8139_IO_SIZE
|
|---|
| 657 | }
|
|---|
| 658 | };
|
|---|
| 659 |
|
|---|
| 660 | /** Commands to deal with interrupt
|
|---|
| 661 | *
|
|---|
| 662 | * Read ISR, check if tere is any interrupt pending.
|
|---|
| 663 | * If so, reset it and accept the interrupt.
|
|---|
| 664 | * The .addr of the first and third command must
|
|---|
| 665 | * be filled to the ISR port address
|
|---|
| 666 | */
|
|---|
| 667 | irq_cmd_t rtl8139_irq_commands[] = {
|
|---|
| 668 | {
|
|---|
| 669 | /* Get the interrupt status */
|
|---|
| 670 | .cmd = CMD_PIO_READ_16,
|
|---|
| 671 | .addr = NULL,
|
|---|
| 672 | .dstarg = 2
|
|---|
| 673 | },
|
|---|
| 674 | {
|
|---|
| 675 | .cmd = CMD_PREDICATE,
|
|---|
| 676 | .value = 3,
|
|---|
| 677 | .srcarg = 2
|
|---|
| 678 | },
|
|---|
| 679 | {
|
|---|
| 680 | /* Mark interrupts as solved */
|
|---|
| 681 | .cmd = CMD_PIO_WRITE_16,
|
|---|
| 682 | .addr = NULL,
|
|---|
| 683 | .value = 0xFFFF
|
|---|
| 684 | },
|
|---|
| 685 | {
|
|---|
| 686 | /* Disable interrupts until interrupt routine is finished */
|
|---|
| 687 | .cmd = CMD_PIO_WRITE_16,
|
|---|
| 688 | .addr = NULL,
|
|---|
| 689 | .value = 0x0000
|
|---|
| 690 | },
|
|---|
| 691 | {
|
|---|
| 692 | .cmd = CMD_ACCEPT
|
|---|
| 693 | }
|
|---|
| 694 | };
|
|---|
| 695 |
|
|---|
| 696 | /** Interrupt code definition */
|
|---|
| 697 | irq_code_t rtl8139_irq_code = {
|
|---|
| 698 | .rangecount = sizeof(rtl8139_irq_pio_ranges) / sizeof(irq_pio_range_t),
|
|---|
| 699 | .ranges = rtl8139_irq_pio_ranges,
|
|---|
| 700 | .cmdcount = sizeof(rtl8139_irq_commands) / sizeof(irq_cmd_t),
|
|---|
| 701 | .cmds = rtl8139_irq_commands
|
|---|
| 702 | };
|
|---|
| 703 |
|
|---|
| 704 | /** Deal with transmitter interrupt
|
|---|
| 705 | *
|
|---|
| 706 | * @param nic_data Nic driver data
|
|---|
| 707 | */
|
|---|
| 708 | static void rtl8139_tx_interrupt(nic_t *nic_data)
|
|---|
| 709 | {
|
|---|
| 710 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 711 |
|
|---|
| 712 | fibril_mutex_lock(&rtl8139->tx_lock);
|
|---|
| 713 |
|
|---|
| 714 | size_t tx_next = rtl8139->tx_next;
|
|---|
| 715 | size_t tx_used = rtl8139->tx_used;
|
|---|
| 716 | while (tx_used != tx_next) {
|
|---|
| 717 | size_t desc_to_check = tx_used % TX_BUFF_COUNT;
|
|---|
| 718 | void * tsd_to_check = rtl8139->io_port + TSD0
|
|---|
| 719 | + desc_to_check * sizeof(uint32_t);
|
|---|
| 720 | uint32_t tsd_value = pio_read_32(tsd_to_check);
|
|---|
| 721 |
|
|---|
| 722 | /* If sending is still in the progress */
|
|---|
| 723 | if ((tsd_value & TSD_OWN) == 0)
|
|---|
| 724 | break;
|
|---|
| 725 |
|
|---|
| 726 | tx_used++;
|
|---|
| 727 |
|
|---|
| 728 | /* If the frame was sent */
|
|---|
| 729 | if (tsd_value & TSD_TOK) {
|
|---|
| 730 | size_t size = REG_GET_VAL(tsd_value, TSD_SIZE);
|
|---|
| 731 | nic_report_send_ok(nic_data, 1, size);
|
|---|
| 732 | } else if (tsd_value & TSD_CRS) {
|
|---|
| 733 | nic_report_send_error(nic_data, NIC_SEC_CARRIER_LOST, 1);
|
|---|
| 734 | } else if (tsd_value & TSD_OWC) {
|
|---|
| 735 | nic_report_send_error(nic_data, NIC_SEC_WINDOW_ERROR, 1);
|
|---|
| 736 | } else if (tsd_value & TSD_TABT) {
|
|---|
| 737 | nic_report_send_error(nic_data, NIC_SEC_ABORTED, 1);
|
|---|
| 738 | } else if (tsd_value & TSD_CDH) {
|
|---|
| 739 | nic_report_send_error(nic_data, NIC_SEC_HEARTBEAT, 1);
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | unsigned collisions = REG_GET_VAL(tsd_value, TSD_NCC);
|
|---|
| 743 | if (collisions > 0) {
|
|---|
| 744 | nic_report_collisions(nic_data, collisions);
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | if (tsd_value & TSD_TUN) {
|
|---|
| 748 | nic_report_send_error(nic_data, NIC_SEC_FIFO_OVERRUN, 1);
|
|---|
| 749 | }
|
|---|
| 750 | }
|
|---|
| 751 | if (rtl8139->tx_used != tx_used) {
|
|---|
| 752 | rtl8139->tx_used = tx_used;
|
|---|
| 753 | nic_set_tx_busy(nic_data, 0);
|
|---|
| 754 | }
|
|---|
| 755 | fibril_mutex_unlock(&rtl8139->tx_lock);
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | /** Receive all frames from the buffer
|
|---|
| 759 | *
|
|---|
| 760 | * @param rtl8139 driver private data
|
|---|
| 761 | */
|
|---|
| 762 | static void rtl8139_receive_frames(nic_t *nic_data)
|
|---|
| 763 | {
|
|---|
| 764 | assert(nic_data);
|
|---|
| 765 |
|
|---|
| 766 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 767 | assert(rtl8139);
|
|---|
| 768 |
|
|---|
| 769 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 770 | nic_frame_list_t *frames = rtl8139_frame_receive(nic_data);
|
|---|
| 771 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 772 |
|
|---|
| 773 | if (frames)
|
|---|
| 774 | nic_received_frame_list(nic_data, frames);
|
|---|
| 775 | }
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 | /** Deal with poll interrupt
|
|---|
| 779 | *
|
|---|
| 780 | * @param nic_data Nic driver data
|
|---|
| 781 | */
|
|---|
| 782 | static int rtl8139_poll_interrupt(nic_t *nic_data)
|
|---|
| 783 | {
|
|---|
| 784 | assert(nic_data);
|
|---|
| 785 |
|
|---|
| 786 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 787 | assert(rtl8139);
|
|---|
| 788 |
|
|---|
| 789 | uint32_t timer_val;
|
|---|
| 790 | int receive = rtl8139_timer_act_step(&rtl8139->poll_timer, &timer_val);
|
|---|
| 791 |
|
|---|
| 792 | assert(timer_val);
|
|---|
| 793 | pio_write_32(rtl8139->io_port + TIMERINT, timer_val);
|
|---|
| 794 | pio_write_32(rtl8139->io_port + TCTR, 0x0);
|
|---|
| 795 | ddf_msg(LVL_DEBUG, "rtl8139 timer: %"PRIu32"\treceive: %d", timer_val, receive);
|
|---|
| 796 | return receive;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 | /** Poll device according to isr status
|
|---|
| 801 | *
|
|---|
| 802 | * The isr value must be obtained and cleared by the caller. The reason
|
|---|
| 803 | * of this function separate is to allow polling from both interrupt
|
|---|
| 804 | * (which clears controller ISR before the handler runs) and the polling
|
|---|
| 805 | * callbacks.
|
|---|
| 806 | *
|
|---|
| 807 | * @param nic_data Driver data
|
|---|
| 808 | * @param isr Interrupt status register value
|
|---|
| 809 | */
|
|---|
| 810 | static void rtl8139_interrupt_impl(nic_t *nic_data, uint16_t isr)
|
|---|
| 811 | {
|
|---|
| 812 | assert(nic_data);
|
|---|
| 813 |
|
|---|
| 814 | nic_poll_mode_t poll_mode = nic_query_poll_mode(nic_data, 0);
|
|---|
| 815 |
|
|---|
| 816 | /* Process only when should in the polling mode */
|
|---|
| 817 | if (poll_mode == NIC_POLL_PERIODIC) {
|
|---|
| 818 | int receive = 0;
|
|---|
| 819 | if (isr & INT_TIME_OUT) {
|
|---|
| 820 | receive = rtl8139_poll_interrupt(nic_data);
|
|---|
| 821 | }
|
|---|
| 822 | if (! receive)
|
|---|
| 823 | return;
|
|---|
| 824 | }
|
|---|
| 825 |
|
|---|
| 826 | /* Check transmittion interrupts first to allow transmit next frames
|
|---|
| 827 | * sooner
|
|---|
| 828 | */
|
|---|
| 829 | if (isr & (INT_TOK | INT_TER)) {
|
|---|
| 830 | rtl8139_tx_interrupt(nic_data);
|
|---|
| 831 | }
|
|---|
| 832 | if (isr & INT_ROK) {
|
|---|
| 833 | rtl8139_receive_frames(nic_data);
|
|---|
| 834 | }
|
|---|
| 835 | if (isr & (INT_RER | INT_RXOVW | INT_FIFOOVW)) {
|
|---|
| 836 | if (isr & INT_RER) {
|
|---|
| 837 | //TODO: is this only the general error, or any particular?
|
|---|
| 838 | }
|
|---|
| 839 | if (isr & (INT_FIFOOVW)) {
|
|---|
| 840 | nic_report_receive_error(nic_data, NIC_REC_FIFO_OVERRUN, 1);
|
|---|
| 841 | } else if (isr & (INT_RXOVW)) {
|
|---|
| 842 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 843 | assert(rtl8139);
|
|---|
| 844 |
|
|---|
| 845 | uint32_t miss = pio_read_32(rtl8139->io_port + MPC) & MPC_VMASK;
|
|---|
| 846 | pio_write_32(rtl8139->io_port + MPC, 0);
|
|---|
| 847 | nic_report_receive_error(nic_data, NIC_REC_BUFFER_OVERFLOW, miss);
|
|---|
| 848 | }
|
|---|
| 849 | }
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | /** Handle device interrupt
|
|---|
| 853 | *
|
|---|
| 854 | * @param dev The rtl8139 device
|
|---|
| 855 | * @param iid The IPC call id
|
|---|
| 856 | * @param icall The IPC call structure
|
|---|
| 857 | */
|
|---|
| 858 | static void rtl8139_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid,
|
|---|
| 859 | ipc_call_t *icall)
|
|---|
| 860 | {
|
|---|
| 861 | assert(dev);
|
|---|
| 862 | assert(icall);
|
|---|
| 863 |
|
|---|
| 864 | uint16_t isr = (uint16_t) IPC_GET_ARG2(*icall);
|
|---|
| 865 | nic_t *nic_data = nic_get_from_ddf_dev(dev);
|
|---|
| 866 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 867 |
|
|---|
| 868 | rtl8139_interrupt_impl(nic_data, isr);
|
|---|
| 869 |
|
|---|
| 870 | /* Turn the interrupts on again */
|
|---|
| 871 | rtl8139_hw_int_enable(rtl8139);
|
|---|
| 872 | };
|
|---|
| 873 |
|
|---|
| 874 | /** Register interrupt handler for the card in the system
|
|---|
| 875 | *
|
|---|
| 876 | * Note: the global irq_reg_mutex is locked because of work with global
|
|---|
| 877 | * structure.
|
|---|
| 878 | *
|
|---|
| 879 | * @param nic_data The driver data
|
|---|
| 880 | *
|
|---|
| 881 | * @return EOK if the handler was registered, negative error code otherwise
|
|---|
| 882 | */
|
|---|
| 883 | inline static int rtl8139_register_int_handler(nic_t *nic_data)
|
|---|
| 884 | {
|
|---|
| 885 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 886 |
|
|---|
| 887 | /* Lock the mutex in whole driver while working with global structure */
|
|---|
| 888 | RTL8139_IRQ_STRUCT_LOCK();
|
|---|
| 889 |
|
|---|
| 890 | rtl8139_irq_code.ranges[0].base = (uintptr_t) rtl8139->io_addr;
|
|---|
| 891 | rtl8139_irq_code.cmds[0].addr = rtl8139->io_addr + ISR;
|
|---|
| 892 | rtl8139_irq_code.cmds[2].addr = rtl8139->io_addr + ISR;
|
|---|
| 893 | rtl8139_irq_code.cmds[3].addr = rtl8139->io_addr + IMR;
|
|---|
| 894 | int rc = register_interrupt_handler(nic_get_ddf_dev(nic_data),
|
|---|
| 895 | rtl8139->irq, rtl8139_interrupt_handler, &rtl8139_irq_code);
|
|---|
| 896 |
|
|---|
| 897 | RTL8139_IRQ_STRUCT_UNLOCK();
|
|---|
| 898 |
|
|---|
| 899 | return rc;
|
|---|
| 900 | }
|
|---|
| 901 |
|
|---|
| 902 | /** Start the controller
|
|---|
| 903 | *
|
|---|
| 904 | * The caller must lock tx_lock and rx_lock before calling this function
|
|---|
| 905 | *
|
|---|
| 906 | * @param rtl8139 The card private data
|
|---|
| 907 | */
|
|---|
| 908 | inline static void rtl8139_card_up(rtl8139_t *rtl8139)
|
|---|
| 909 | {
|
|---|
| 910 | void *io_base = rtl8139->io_port;
|
|---|
| 911 | size_t i;
|
|---|
| 912 |
|
|---|
| 913 | /* Wake up the device */
|
|---|
| 914 | pio_write_8(io_base + CONFIG1, 0x00);
|
|---|
| 915 | /* Reset the device */
|
|---|
| 916 | rtl8139_soft_reset(rtl8139);
|
|---|
| 917 |
|
|---|
| 918 | /* Write transmittion buffer addresses */
|
|---|
| 919 | for(i = 0; i < TX_BUFF_COUNT; ++i) {
|
|---|
| 920 | uint32_t addr = PTR2U32(rtl8139->tx_buff_phys + i*TX_BUFF_SIZE);
|
|---|
| 921 | pio_write_32(io_base + TSAD0 + 4*i, addr);
|
|---|
| 922 | }
|
|---|
| 923 | rtl8139->tx_next = 0;
|
|---|
| 924 | rtl8139->tx_used = 0;
|
|---|
| 925 | nic_set_tx_busy(rtl8139->nic_data, 0);
|
|---|
| 926 |
|
|---|
| 927 | pio_write_32(io_base + RBSTART, PTR2U32(rtl8139->rx_buff_phys));
|
|---|
| 928 |
|
|---|
| 929 | /* Enable transmitter and receiver */
|
|---|
| 930 | uint8_t cr_value = pio_read_8(io_base + CR);
|
|---|
| 931 | pio_write_8(io_base + CR, cr_value | CR_TE | CR_RE);
|
|---|
| 932 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 933 | }
|
|---|
| 934 |
|
|---|
| 935 | /** Activate the device to receive and transmit frames
|
|---|
| 936 | *
|
|---|
| 937 | * @param nic_data The nic driver data
|
|---|
| 938 | *
|
|---|
| 939 | * @return EOK if activated successfully, error code otherwise
|
|---|
| 940 | */
|
|---|
| 941 | static int rtl8139_on_activated(nic_t *nic_data)
|
|---|
| 942 | {
|
|---|
| 943 | assert(nic_data);
|
|---|
| 944 | ddf_msg(LVL_NOTE, "Activating device");
|
|---|
| 945 |
|
|---|
| 946 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 947 | assert(rtl8139);
|
|---|
| 948 |
|
|---|
| 949 | rtl8139_lock_all(rtl8139);
|
|---|
| 950 | rtl8139_card_up(rtl8139);
|
|---|
| 951 | rtl8139_unlock_all(rtl8139);
|
|---|
| 952 |
|
|---|
| 953 | rtl8139->int_mask = RTL_DEFAULT_INTERRUPTS;
|
|---|
| 954 | rtl8139_hw_int_enable(rtl8139);
|
|---|
| 955 |
|
|---|
| 956 | int rc = irc_enable_interrupt(rtl8139->irq);
|
|---|
| 957 | if (rc != EOK) {
|
|---|
| 958 | rtl8139_on_stopped(nic_data);
|
|---|
| 959 | return rc;
|
|---|
| 960 | }
|
|---|
| 961 |
|
|---|
| 962 | ddf_msg(LVL_DEBUG, "Device activated, interrupt %d registered", rtl8139->irq);
|
|---|
| 963 | return EOK;
|
|---|
| 964 | }
|
|---|
| 965 |
|
|---|
| 966 | /** Callback for NIC_STATE_STOPPED change
|
|---|
| 967 | *
|
|---|
| 968 | * @param nic_data The nic driver data
|
|---|
| 969 | *
|
|---|
| 970 | * @return EOK if succeed, error code otherwise
|
|---|
| 971 | */
|
|---|
| 972 | static int rtl8139_on_stopped(nic_t *nic_data)
|
|---|
| 973 | {
|
|---|
| 974 | assert(nic_data);
|
|---|
| 975 |
|
|---|
| 976 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 977 | assert(rtl8139);
|
|---|
| 978 |
|
|---|
| 979 | rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
|
|---|
| 980 | rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
|
|---|
| 981 | rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
|
|---|
| 982 | rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
|
|---|
| 983 |
|
|---|
| 984 | /* Reset the card to the initial state (interrupts, Tx and Rx disabled) */
|
|---|
| 985 | rtl8139_lock_all(rtl8139);
|
|---|
| 986 | rtl8139_soft_reset(rtl8139);
|
|---|
| 987 | rtl8139_unlock_all(rtl8139);
|
|---|
| 988 | return EOK;
|
|---|
| 989 | }
|
|---|
| 990 |
|
|---|
| 991 |
|
|---|
| 992 | static int rtl8139_unicast_set(nic_t *nic_data, nic_unicast_mode_t mode,
|
|---|
| 993 | const nic_address_t *, size_t);
|
|---|
| 994 | static int rtl8139_multicast_set(nic_t *nic_data, nic_multicast_mode_t mode,
|
|---|
| 995 | const nic_address_t *addr, size_t addr_count);
|
|---|
| 996 | static int rtl8139_broadcast_set(nic_t *nic_data, nic_broadcast_mode_t mode);
|
|---|
| 997 |
|
|---|
| 998 |
|
|---|
| 999 | /** Create driver data structure
|
|---|
| 1000 | *
|
|---|
| 1001 | * @return Intialized device data structure or NULL
|
|---|
| 1002 | */
|
|---|
| 1003 | static rtl8139_t *rtl8139_create_dev_data(ddf_dev_t *dev)
|
|---|
| 1004 | {
|
|---|
| 1005 | assert(dev);
|
|---|
| 1006 | assert(!nic_get_from_ddf_dev(dev));
|
|---|
| 1007 |
|
|---|
| 1008 | nic_t *nic_data = nic_create_and_bind(dev);
|
|---|
| 1009 | if (!nic_data)
|
|---|
| 1010 | return NULL;
|
|---|
| 1011 |
|
|---|
| 1012 | rtl8139_t *rtl8139 = malloc(sizeof(rtl8139_t));
|
|---|
| 1013 | if (!rtl8139) {
|
|---|
| 1014 | nic_unbind_and_destroy(dev);
|
|---|
| 1015 | return NULL;
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 | memset(rtl8139, 0, sizeof(rtl8139_t));
|
|---|
| 1019 |
|
|---|
| 1020 | rtl8139->nic_data = nic_data;
|
|---|
| 1021 | nic_set_specific(nic_data, rtl8139);
|
|---|
| 1022 | nic_set_send_frame_handler(nic_data, rtl8139_send_frame);
|
|---|
| 1023 | nic_set_state_change_handlers(nic_data,
|
|---|
| 1024 | rtl8139_on_activated, NULL, rtl8139_on_stopped);
|
|---|
| 1025 | nic_set_filtering_change_handlers(nic_data,
|
|---|
| 1026 | rtl8139_unicast_set, rtl8139_multicast_set, rtl8139_broadcast_set,
|
|---|
| 1027 | NULL, NULL);
|
|---|
| 1028 | nic_set_wol_virtue_change_handlers(nic_data,
|
|---|
| 1029 | rtl8139_wol_virtue_add, rtl8139_wol_virtue_rem);
|
|---|
| 1030 | nic_set_poll_handlers(nic_data, rtl8139_poll_mode_change, rtl8139_poll);
|
|---|
| 1031 |
|
|---|
| 1032 |
|
|---|
| 1033 | fibril_mutex_initialize(&rtl8139->rx_lock);
|
|---|
| 1034 | fibril_mutex_initialize(&rtl8139->tx_lock);
|
|---|
| 1035 |
|
|---|
| 1036 | nic_set_wol_max_caps(nic_data, NIC_WV_BROADCAST, 1);
|
|---|
| 1037 | nic_set_wol_max_caps(nic_data, NIC_WV_LINK_CHANGE, 1);
|
|---|
| 1038 | nic_set_wol_max_caps(nic_data, NIC_WV_MAGIC_PACKET, 1);
|
|---|
| 1039 |
|
|---|
| 1040 | return rtl8139;
|
|---|
| 1041 | }
|
|---|
| 1042 |
|
|---|
| 1043 | /** Clean up the rtl8139 device structure.
|
|---|
| 1044 | *
|
|---|
| 1045 | * @param dev The device structure.
|
|---|
| 1046 | */
|
|---|
| 1047 | static void rtl8139_dev_cleanup(ddf_dev_t *dev)
|
|---|
| 1048 | {
|
|---|
| 1049 | assert(dev);
|
|---|
| 1050 |
|
|---|
| 1051 | if (ddf_dev_data_get(dev))
|
|---|
| 1052 | nic_unbind_and_destroy(dev);
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 | /** Fill the irq and io_addr part of device data structure
|
|---|
| 1056 | *
|
|---|
| 1057 | * The hw_resources must be obtained before calling this function
|
|---|
| 1058 | *
|
|---|
| 1059 | * @param dev The device structure
|
|---|
| 1060 | * @param hw_resources Devices hardware resources
|
|---|
| 1061 | *
|
|---|
| 1062 | * @return EOK if succeed, negative error code otherwise
|
|---|
| 1063 | */
|
|---|
| 1064 | static int rtl8139_fill_resource_info(ddf_dev_t *dev, const hw_res_list_parsed_t
|
|---|
| 1065 | *hw_resources)
|
|---|
| 1066 | {
|
|---|
| 1067 | assert(dev);
|
|---|
| 1068 | assert(hw_resources);
|
|---|
| 1069 |
|
|---|
| 1070 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_dev(dev));
|
|---|
| 1071 | assert(rtl8139);
|
|---|
| 1072 |
|
|---|
| 1073 | if (hw_resources->irqs.count != 1) {
|
|---|
| 1074 | ddf_msg(LVL_ERROR, "%s device: unexpected irq count", ddf_dev_get_name(dev));
|
|---|
| 1075 | return EINVAL;
|
|---|
| 1076 | };
|
|---|
| 1077 | if (hw_resources->io_ranges.count != 1) {
|
|---|
| 1078 | ddf_msg(LVL_ERROR, "%s device: unexpected io ranges count", ddf_dev_get_name(dev));
|
|---|
| 1079 | return EINVAL;
|
|---|
| 1080 | }
|
|---|
| 1081 |
|
|---|
| 1082 | rtl8139->irq = hw_resources->irqs.irqs[0];
|
|---|
| 1083 | ddf_msg(LVL_DEBUG, "%s device: irq 0x%x assigned", ddf_dev_get_name(dev), rtl8139->irq);
|
|---|
| 1084 |
|
|---|
| 1085 | rtl8139->io_addr = IOADDR_TO_PTR(RNGABS(hw_resources->io_ranges.ranges[0]));
|
|---|
| 1086 | if (hw_resources->io_ranges.ranges[0].size < RTL8139_IO_SIZE) {
|
|---|
| 1087 | ddf_msg(LVL_ERROR, "i/o range assigned to the device "
|
|---|
| 1088 | "%s is too small.", ddf_dev_get_name(dev));
|
|---|
| 1089 | return EINVAL;
|
|---|
| 1090 | }
|
|---|
| 1091 | ddf_msg(LVL_DEBUG, "%s device: i/o addr %p assigned.", ddf_dev_get_name(dev), rtl8139->io_addr);
|
|---|
| 1092 |
|
|---|
| 1093 | return EOK;
|
|---|
| 1094 | }
|
|---|
| 1095 |
|
|---|
| 1096 | /** Obtain information about hardware resources of the device
|
|---|
| 1097 | *
|
|---|
| 1098 | * The device must be connected to the parent
|
|---|
| 1099 | *
|
|---|
| 1100 | * @param dev The device structure
|
|---|
| 1101 | *
|
|---|
| 1102 | * @return EOK if succeed, negative error code otherwise
|
|---|
| 1103 | */
|
|---|
| 1104 | static int rtl8139_get_resource_info(ddf_dev_t *dev)
|
|---|
| 1105 | {
|
|---|
| 1106 | assert(dev);
|
|---|
| 1107 |
|
|---|
| 1108 | nic_t *nic_data = nic_get_from_ddf_dev(dev);
|
|---|
| 1109 | assert(nic_data);
|
|---|
| 1110 |
|
|---|
| 1111 | hw_res_list_parsed_t hw_res_parsed;
|
|---|
| 1112 | hw_res_list_parsed_init(&hw_res_parsed);
|
|---|
| 1113 |
|
|---|
| 1114 | /* Get hw resources form parent driver */
|
|---|
| 1115 | int rc = nic_get_resources(nic_data, &hw_res_parsed);
|
|---|
| 1116 | if (rc != EOK)
|
|---|
| 1117 | return rc;
|
|---|
| 1118 |
|
|---|
| 1119 | /* Fill resources information to the device */
|
|---|
| 1120 | int ret = rtl8139_fill_resource_info(dev, &hw_res_parsed);
|
|---|
| 1121 | hw_res_list_parsed_clean(&hw_res_parsed);
|
|---|
| 1122 |
|
|---|
| 1123 | return ret;
|
|---|
| 1124 | }
|
|---|
| 1125 |
|
|---|
| 1126 |
|
|---|
| 1127 | /** Allocate buffers using DMA framework
|
|---|
| 1128 | *
|
|---|
| 1129 | * The buffers structures in the device specific data is filled
|
|---|
| 1130 | *
|
|---|
| 1131 | * @param data The device specific structure to fill
|
|---|
| 1132 | *
|
|---|
| 1133 | * @return EOK in the case of success, error code otherwise
|
|---|
| 1134 | */
|
|---|
| 1135 | static int rtl8139_buffers_create(rtl8139_t *rtl8139)
|
|---|
| 1136 | {
|
|---|
| 1137 | size_t i = 0;
|
|---|
| 1138 | int rc;
|
|---|
| 1139 |
|
|---|
| 1140 | ddf_msg(LVL_DEBUG, "Creating buffers");
|
|---|
| 1141 |
|
|---|
| 1142 | rtl8139->tx_buff_virt = AS_AREA_ANY;
|
|---|
| 1143 | rc = dmamem_map_anonymous(TX_PAGES * PAGE_SIZE, DMAMEM_4GiB,
|
|---|
| 1144 | AS_AREA_WRITE, 0, &rtl8139->tx_buff_phys, &rtl8139->tx_buff_virt);
|
|---|
| 1145 | if (rc != EOK) {
|
|---|
| 1146 | ddf_msg(LVL_ERROR, "Can not allocate transmitter buffers.");
|
|---|
| 1147 | goto err_tx_alloc;
|
|---|
| 1148 | }
|
|---|
| 1149 |
|
|---|
| 1150 | for (i = 0; i < TX_BUFF_COUNT; ++i)
|
|---|
| 1151 | rtl8139->tx_buff[i] = rtl8139->tx_buff_virt + i * TX_BUFF_SIZE;
|
|---|
| 1152 |
|
|---|
| 1153 | ddf_msg(LVL_DEBUG, "The transmittion buffers allocated");
|
|---|
| 1154 |
|
|---|
| 1155 | /* Use the first buffer for next transmittion */
|
|---|
| 1156 | rtl8139->tx_next = 0;
|
|---|
| 1157 | rtl8139->tx_used = 0;
|
|---|
| 1158 |
|
|---|
| 1159 | /* Allocate buffer for receiver */
|
|---|
| 1160 | ddf_msg(LVL_DEBUG, "Allocating receiver buffer of the size %d bytes",
|
|---|
| 1161 | RxBUF_TOT_LENGTH);
|
|---|
| 1162 |
|
|---|
| 1163 | rtl8139->rx_buff_virt = AS_AREA_ANY;
|
|---|
| 1164 | rc = dmamem_map_anonymous(RxBUF_TOT_LENGTH, DMAMEM_4GiB,
|
|---|
| 1165 | AS_AREA_READ, 0, &rtl8139->rx_buff_phys, &rtl8139->rx_buff_virt);
|
|---|
| 1166 | if (rc != EOK) {
|
|---|
| 1167 | ddf_msg(LVL_ERROR, "Can not allocate receive buffer.");
|
|---|
| 1168 | goto err_rx_alloc;
|
|---|
| 1169 | }
|
|---|
| 1170 | ddf_msg(LVL_DEBUG, "The buffers created");
|
|---|
| 1171 |
|
|---|
| 1172 | return EOK;
|
|---|
| 1173 |
|
|---|
| 1174 | err_rx_alloc:
|
|---|
| 1175 | dmamem_unmap_anonymous(&rtl8139->tx_buff_virt);
|
|---|
| 1176 | err_tx_alloc:
|
|---|
| 1177 | return rc;
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | /** Initialize the rtl8139 device structure
|
|---|
| 1181 | *
|
|---|
| 1182 | * @param dev The device information
|
|---|
| 1183 | *
|
|---|
| 1184 | * @return EOK if succeed, negative error code otherwise
|
|---|
| 1185 | */
|
|---|
| 1186 | static int rtl8139_device_initialize(ddf_dev_t *dev)
|
|---|
| 1187 | {
|
|---|
| 1188 | ddf_msg(LVL_DEBUG, "rtl8139_dev_initialize %s", ddf_dev_get_name(dev));
|
|---|
| 1189 |
|
|---|
| 1190 | int ret = EOK;
|
|---|
| 1191 |
|
|---|
| 1192 | ddf_msg(LVL_DEBUG, "rtl8139: creating device data");
|
|---|
| 1193 |
|
|---|
| 1194 | /* Allocate driver data for the device. */
|
|---|
| 1195 | rtl8139_t *rtl8139 = rtl8139_create_dev_data(dev);
|
|---|
| 1196 | if (rtl8139 == NULL) {
|
|---|
| 1197 | ddf_msg(LVL_ERROR, "Not enough memory for initializing %s.", ddf_dev_get_name(dev));
|
|---|
| 1198 | return ENOMEM;
|
|---|
| 1199 | }
|
|---|
| 1200 |
|
|---|
| 1201 | ddf_msg(LVL_DEBUG, "rtl8139: dev_data created");
|
|---|
| 1202 |
|
|---|
| 1203 | /* Obtain and fill hardware resources info and connect to parent */
|
|---|
| 1204 | ret = rtl8139_get_resource_info(dev);
|
|---|
| 1205 | if (ret != EOK) {
|
|---|
| 1206 | ddf_msg(LVL_ERROR, "Can not obatin hw resources information");
|
|---|
| 1207 | goto failed;
|
|---|
| 1208 | }
|
|---|
| 1209 |
|
|---|
| 1210 | ddf_msg(LVL_DEBUG, "rtl8139: resource_info obtained");
|
|---|
| 1211 |
|
|---|
| 1212 | /* Allocate DMA buffers */
|
|---|
| 1213 | ret = rtl8139_buffers_create(rtl8139);
|
|---|
| 1214 | if (ret != EOK)
|
|---|
| 1215 | goto failed;
|
|---|
| 1216 |
|
|---|
| 1217 | /* Set default frame acceptance */
|
|---|
| 1218 | rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
|
|---|
| 1219 | rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
|
|---|
| 1220 | rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
|
|---|
| 1221 | rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
|
|---|
| 1222 | /* Set receiver early treshold to 8/16 of frame length */
|
|---|
| 1223 | rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
|
|---|
| 1224 |
|
|---|
| 1225 | ddf_msg(LVL_DEBUG, "The device is initialized");
|
|---|
| 1226 | return ret;
|
|---|
| 1227 |
|
|---|
| 1228 | failed:
|
|---|
| 1229 | ddf_msg(LVL_ERROR, "The device initialization failed");
|
|---|
| 1230 | rtl8139_dev_cleanup(dev);
|
|---|
| 1231 | return ret;
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 | /** Enable the i/o ports of the device.
|
|---|
| 1235 | *
|
|---|
| 1236 | * @param dev The RTL8139 device.
|
|---|
| 1237 | *
|
|---|
| 1238 | * @return EOK if successed, negative error code otherwise
|
|---|
| 1239 | */
|
|---|
| 1240 | static int rtl8139_pio_enable(ddf_dev_t *dev)
|
|---|
| 1241 | {
|
|---|
| 1242 | ddf_msg(LVL_DEBUG, NAME ": rtl8139_pio_enable %s", ddf_dev_get_name(dev));
|
|---|
| 1243 |
|
|---|
| 1244 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_dev(dev));
|
|---|
| 1245 |
|
|---|
| 1246 | /* Gain control over port's registers. */
|
|---|
| 1247 | if (pio_enable(rtl8139->io_addr, RTL8139_IO_SIZE, &rtl8139->io_port)) {
|
|---|
| 1248 | ddf_msg(LVL_ERROR, "Cannot gain the port %p for device %s.", rtl8139->io_addr,
|
|---|
| 1249 | ddf_dev_get_name(dev));
|
|---|
| 1250 | return EADDRNOTAVAIL;
|
|---|
| 1251 | }
|
|---|
| 1252 |
|
|---|
| 1253 | return EOK;
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 | /** Initialize the driver private data according to the
|
|---|
| 1257 | * device registers
|
|---|
| 1258 | *
|
|---|
| 1259 | * @param rtl8139 rtl8139 private data
|
|---|
| 1260 | */
|
|---|
| 1261 | static void rtl8139_data_init(rtl8139_t *rtl8139)
|
|---|
| 1262 | {
|
|---|
| 1263 | assert(rtl8139);
|
|---|
| 1264 |
|
|---|
| 1265 | /* Check the version id */
|
|---|
| 1266 | uint32_t tcr = pio_read_32(rtl8139->io_port + TCR);
|
|---|
| 1267 | uint32_t hwverid = RTL8139_HWVERID(tcr);
|
|---|
| 1268 | size_t i = 0;
|
|---|
| 1269 | rtl8139->hw_version = RTL8139_VER_COUNT;
|
|---|
| 1270 | for (i = 0; i < RTL8139_VER_COUNT; ++i) {
|
|---|
| 1271 | if (rtl8139_versions[i].hwverid == 0)
|
|---|
| 1272 | break;
|
|---|
| 1273 |
|
|---|
| 1274 | if (rtl8139_versions[i].hwverid == hwverid) {
|
|---|
| 1275 | rtl8139->hw_version = rtl8139_versions[i].ver_id;
|
|---|
| 1276 | ddf_msg(LVL_NOTE, "HW version found: index %zu, ver_id %d (%s)", i,
|
|---|
| 1277 | rtl8139_versions[i].ver_id, model_names[rtl8139->hw_version]);
|
|---|
| 1278 | }
|
|---|
| 1279 | }
|
|---|
| 1280 | }
|
|---|
| 1281 |
|
|---|
| 1282 | /** The add_device callback of RTL8139 callback
|
|---|
| 1283 | *
|
|---|
| 1284 | * Probe and initialize the newly added device.
|
|---|
| 1285 | *
|
|---|
| 1286 | * @param dev The RTL8139 device.
|
|---|
| 1287 | *
|
|---|
| 1288 | * @return EOK if added successfully, negative error code otherwise
|
|---|
| 1289 | */
|
|---|
| 1290 | int rtl8139_dev_add(ddf_dev_t *dev)
|
|---|
| 1291 | {
|
|---|
| 1292 | ddf_fun_t *fun;
|
|---|
| 1293 |
|
|---|
| 1294 | assert(dev);
|
|---|
| 1295 | ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %zu)",
|
|---|
| 1296 | ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
|
|---|
| 1297 |
|
|---|
| 1298 | /* Init device structure for rtl8139 */
|
|---|
| 1299 | int rc = rtl8139_device_initialize(dev);
|
|---|
| 1300 | if (rc != EOK)
|
|---|
| 1301 | return rc;
|
|---|
| 1302 |
|
|---|
| 1303 | /* Map I/O ports */
|
|---|
| 1304 | rc = rtl8139_pio_enable(dev);
|
|---|
| 1305 | if (rc != EOK)
|
|---|
| 1306 | goto err_destroy;
|
|---|
| 1307 |
|
|---|
| 1308 | nic_t *nic_data = nic_get_from_ddf_dev(dev);
|
|---|
| 1309 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1310 |
|
|---|
| 1311 | nic_address_t addr;
|
|---|
| 1312 | rtl8139_hw_get_addr(rtl8139, &addr);
|
|---|
| 1313 | rc = nic_report_address(nic_data, &addr);
|
|---|
| 1314 | if (rc != EOK)
|
|---|
| 1315 | goto err_pio;
|
|---|
| 1316 |
|
|---|
| 1317 | /* Initialize the driver private structure */
|
|---|
| 1318 | rtl8139_data_init(rtl8139);
|
|---|
| 1319 |
|
|---|
| 1320 | /* Register interrupt handler */
|
|---|
| 1321 | rc = rtl8139_register_int_handler(nic_data);
|
|---|
| 1322 | if (rc != EOK)
|
|---|
| 1323 | goto err_pio;
|
|---|
| 1324 |
|
|---|
| 1325 | fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
|
|---|
| 1326 | if (fun == NULL) {
|
|---|
| 1327 | ddf_msg(LVL_ERROR, "Failed creating device function");
|
|---|
| 1328 | goto err_srv;
|
|---|
| 1329 | }
|
|---|
| 1330 |
|
|---|
| 1331 | nic_set_ddf_fun(nic_data, fun);
|
|---|
| 1332 | ddf_fun_set_ops(fun, &rtl8139_dev_ops);
|
|---|
| 1333 |
|
|---|
| 1334 | rc = ddf_fun_bind(fun);
|
|---|
| 1335 | if (rc != EOK) {
|
|---|
| 1336 | ddf_msg(LVL_ERROR, "Failed binding device function");
|
|---|
| 1337 | goto err_fun_create;
|
|---|
| 1338 | }
|
|---|
| 1339 | rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
|
|---|
| 1340 | if (rc != EOK) {
|
|---|
| 1341 | ddf_msg(LVL_ERROR, "Failed adding function to category");
|
|---|
| 1342 | goto err_fun_bind;
|
|---|
| 1343 | }
|
|---|
| 1344 |
|
|---|
| 1345 | ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
|
|---|
| 1346 | ddf_dev_get_name(dev));
|
|---|
| 1347 |
|
|---|
| 1348 | return EOK;
|
|---|
| 1349 |
|
|---|
| 1350 | err_fun_bind:
|
|---|
| 1351 | ddf_fun_unbind(fun);
|
|---|
| 1352 | err_fun_create:
|
|---|
| 1353 | ddf_fun_destroy(fun);
|
|---|
| 1354 | err_srv:
|
|---|
| 1355 | unregister_interrupt_handler(dev, rtl8139->irq);
|
|---|
| 1356 | err_pio:
|
|---|
| 1357 | // rtl8139_pio_disable(dev);
|
|---|
| 1358 | /* TODO: find out if the pio_disable is needed */
|
|---|
| 1359 | err_destroy:
|
|---|
| 1360 | rtl8139_dev_cleanup(dev);
|
|---|
| 1361 | return rc;
|
|---|
| 1362 | };
|
|---|
| 1363 |
|
|---|
| 1364 | /** Set card MAC address
|
|---|
| 1365 | *
|
|---|
| 1366 | * @param device The RTL8139 device
|
|---|
| 1367 | * @param address The place to store the address
|
|---|
| 1368 | * @param max_len Maximal addresss length to store
|
|---|
| 1369 | *
|
|---|
| 1370 | * @return EOK if succeed, negative error code otherwise
|
|---|
| 1371 | */
|
|---|
| 1372 | static int rtl8139_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
|
|---|
| 1373 | {
|
|---|
| 1374 | assert(fun);
|
|---|
| 1375 | assert(addr);
|
|---|
| 1376 |
|
|---|
| 1377 | nic_t *nic_data =nic_get_from_ddf_fun((fun));
|
|---|
| 1378 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1379 | assert(rtl8139);
|
|---|
| 1380 |
|
|---|
| 1381 | rtl8139_lock_all(rtl8139);
|
|---|
| 1382 |
|
|---|
| 1383 | int rc = nic_report_address(nic_data, addr);
|
|---|
| 1384 | if ( rc != EOK) {
|
|---|
| 1385 | rtl8139_unlock_all(rtl8139);
|
|---|
| 1386 | return rc;
|
|---|
| 1387 | }
|
|---|
| 1388 |
|
|---|
| 1389 | rtl8139_hw_set_addr(rtl8139, addr);
|
|---|
| 1390 |
|
|---|
| 1391 | rtl8139_unlock_all(rtl8139);
|
|---|
| 1392 | return EOK;
|
|---|
| 1393 | }
|
|---|
| 1394 |
|
|---|
| 1395 | /** Get the device information
|
|---|
| 1396 | *
|
|---|
| 1397 | * @param dev The NIC device
|
|---|
| 1398 | * @param info The information to fill
|
|---|
| 1399 | *
|
|---|
| 1400 | * @return EOK
|
|---|
| 1401 | */
|
|---|
| 1402 | static int rtl8139_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
|
|---|
| 1403 | {
|
|---|
| 1404 | assert(fun);
|
|---|
| 1405 | assert(info);
|
|---|
| 1406 |
|
|---|
| 1407 | nic_t *nic_data = nic_get_from_ddf_fun(fun);
|
|---|
| 1408 | assert(nic_data);
|
|---|
| 1409 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1410 | assert(rtl8139);
|
|---|
| 1411 |
|
|---|
| 1412 | /* TODO: fill the information more completely */
|
|---|
| 1413 | info->vendor_id = 0x10ec;
|
|---|
| 1414 | str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH, "Realtek");
|
|---|
| 1415 |
|
|---|
| 1416 | if (rtl8139->hw_version < RTL8139_VER_COUNT) {
|
|---|
| 1417 | str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
|
|---|
| 1418 | model_names[rtl8139->hw_version]);
|
|---|
| 1419 | } else {
|
|---|
| 1420 | str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, "RTL8139");
|
|---|
| 1421 | }
|
|---|
| 1422 |
|
|---|
| 1423 | info->ethernet_support[ETH_10M] = ETH_10BASE_T;
|
|---|
| 1424 | info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
|
|---|
| 1425 |
|
|---|
| 1426 | info->autoneg_support = RTL8139_AUTONEG_CAPS;
|
|---|
| 1427 | return EOK;
|
|---|
| 1428 | }
|
|---|
| 1429 |
|
|---|
| 1430 | /** Check the cable state
|
|---|
| 1431 | *
|
|---|
| 1432 | * @param[in] dev The device
|
|---|
| 1433 | * @param[out] state The state to fill
|
|---|
| 1434 | *
|
|---|
| 1435 | * @return EOK
|
|---|
| 1436 | */
|
|---|
| 1437 | static int rtl8139_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
|
|---|
| 1438 | {
|
|---|
| 1439 | assert(fun);
|
|---|
| 1440 | assert(state);
|
|---|
| 1441 |
|
|---|
| 1442 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1443 | assert(rtl8139);
|
|---|
| 1444 |
|
|---|
| 1445 | if (pio_read_16(rtl8139->io_port + CSCR) & CS_CON_STATUS) {
|
|---|
| 1446 | *state = NIC_CS_PLUGGED;
|
|---|
| 1447 | } else {
|
|---|
| 1448 | *state = NIC_CS_UNPLUGGED;
|
|---|
| 1449 | }
|
|---|
| 1450 |
|
|---|
| 1451 | return EOK;
|
|---|
| 1452 | }
|
|---|
| 1453 |
|
|---|
| 1454 | /** Get operation mode of the device
|
|---|
| 1455 | */
|
|---|
| 1456 | static int rtl8139_get_operation_mode(ddf_fun_t *fun, int *speed,
|
|---|
| 1457 | nic_channel_mode_t *duplex, nic_role_t *role)
|
|---|
| 1458 | {
|
|---|
| 1459 | assert(fun);
|
|---|
| 1460 | assert(speed);
|
|---|
| 1461 | assert(duplex);
|
|---|
| 1462 | assert(role);
|
|---|
| 1463 |
|
|---|
| 1464 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1465 | assert(rtl8139);
|
|---|
| 1466 |
|
|---|
| 1467 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1468 | uint8_t msr_val = pio_read_8(rtl8139->io_port + MSR);
|
|---|
| 1469 |
|
|---|
| 1470 | if (bmcr_val & BMCR_DUPLEX) {
|
|---|
| 1471 | *duplex = NIC_CM_FULL_DUPLEX;
|
|---|
| 1472 | } else {
|
|---|
| 1473 | *duplex = NIC_CM_HALF_DUPLEX;
|
|---|
| 1474 | }
|
|---|
| 1475 |
|
|---|
| 1476 | if (msr_val & MSR_SPEED10) {
|
|---|
| 1477 | *speed = 10;
|
|---|
| 1478 | } else {
|
|---|
| 1479 | *speed = 100;
|
|---|
| 1480 | }
|
|---|
| 1481 |
|
|---|
| 1482 | *role = NIC_ROLE_UNKNOWN;
|
|---|
| 1483 | return EOK;
|
|---|
| 1484 | }
|
|---|
| 1485 |
|
|---|
| 1486 | /** Value validity */
|
|---|
| 1487 | enum access_mode {
|
|---|
| 1488 | /** Value is invalid now */
|
|---|
| 1489 | VALUE_INVALID = 0,
|
|---|
| 1490 | /** Read-only */
|
|---|
| 1491 | VALUE_RO,
|
|---|
| 1492 | /** Read-write */
|
|---|
| 1493 | VALUE_RW
|
|---|
| 1494 | };
|
|---|
| 1495 |
|
|---|
| 1496 | /** Check if pause frame operations are valid in current situation
|
|---|
| 1497 | *
|
|---|
| 1498 | * @param rtl8139 RTL8139 private structure
|
|---|
| 1499 | *
|
|---|
| 1500 | * @return VALUE_INVALID if the value has no sense in current moment
|
|---|
| 1501 | * @return VALUE_RO if the state is read-only in current moment
|
|---|
| 1502 | * @return VALUE_RW if the state can be modified
|
|---|
| 1503 | */
|
|---|
| 1504 | static int rtl8139_pause_is_valid(rtl8139_t *rtl8139)
|
|---|
| 1505 | {
|
|---|
| 1506 | assert(rtl8139);
|
|---|
| 1507 |
|
|---|
| 1508 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1509 | if ((bmcr & (BMCR_AN_ENABLE | BMCR_DUPLEX)) == 0)
|
|---|
| 1510 | return VALUE_INVALID;
|
|---|
| 1511 |
|
|---|
| 1512 | if (bmcr & BMCR_AN_ENABLE) {
|
|---|
| 1513 | uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
|
|---|
| 1514 | if (anar_lp & ANAR_PAUSE)
|
|---|
| 1515 | return VALUE_RO;
|
|---|
| 1516 | }
|
|---|
| 1517 |
|
|---|
| 1518 | return VALUE_RW;
|
|---|
| 1519 | }
|
|---|
| 1520 |
|
|---|
| 1521 | /** Get current pause frame configuration
|
|---|
| 1522 | *
|
|---|
| 1523 | * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
|
|---|
| 1524 | * the moment (half-duplex).
|
|---|
| 1525 | *
|
|---|
| 1526 | * @param[in] fun The DDF structure of the RTL8139
|
|---|
| 1527 | * @param[out] we_send Sign if local constroller sends pause frame
|
|---|
| 1528 | * @param[out] we_receive Sign if local constroller receives pause frame
|
|---|
| 1529 | * @param[out] time Time filled in pause frames. 0xFFFF in rtl8139
|
|---|
| 1530 | *
|
|---|
| 1531 | * @return EOK if succeed
|
|---|
| 1532 | */
|
|---|
| 1533 | static int rtl8139_pause_get(ddf_fun_t *fun, nic_result_t *we_send,
|
|---|
| 1534 | nic_result_t *we_receive, uint16_t *time)
|
|---|
| 1535 | {
|
|---|
| 1536 | assert(fun);
|
|---|
| 1537 | assert(we_send);
|
|---|
| 1538 | assert(we_receive);
|
|---|
| 1539 |
|
|---|
| 1540 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1541 | assert(rtl8139);
|
|---|
| 1542 |
|
|---|
| 1543 | if (rtl8139_pause_is_valid(rtl8139) == VALUE_INVALID) {
|
|---|
| 1544 | *we_send = NIC_RESULT_NOT_AVAILABLE;
|
|---|
| 1545 | *we_receive = NIC_RESULT_NOT_AVAILABLE;
|
|---|
| 1546 | *time = 0;
|
|---|
| 1547 | return EOK;
|
|---|
| 1548 | }
|
|---|
| 1549 |
|
|---|
| 1550 | uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
|
|---|
| 1551 |
|
|---|
| 1552 | *we_send = (msr & MSR_TXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
|
|---|
| 1553 | *we_receive = (msr & MSR_RXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
|
|---|
| 1554 | *time = RTL8139_PAUSE_VAL;
|
|---|
| 1555 |
|
|---|
| 1556 | return EOK;
|
|---|
| 1557 | };
|
|---|
| 1558 |
|
|---|
| 1559 | /** Set current pause frame configuration
|
|---|
| 1560 | *
|
|---|
| 1561 | * @param fun The DDF structure of the RTL8139
|
|---|
| 1562 | * @param allow_send Sign if local constroller sends pause frame
|
|---|
| 1563 | * @param allow_receive Sign if local constroller receives pause frames
|
|---|
| 1564 | * @param time Time to use, ignored (not supported by device)
|
|---|
| 1565 | *
|
|---|
| 1566 | * @return EOK if succeed, INVAL if the pause frame has no sence
|
|---|
| 1567 | */
|
|---|
| 1568 | static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
|
|---|
| 1569 | uint16_t time)
|
|---|
| 1570 | {
|
|---|
| 1571 | assert(fun);
|
|---|
| 1572 |
|
|---|
| 1573 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1574 | assert(rtl8139);
|
|---|
| 1575 |
|
|---|
| 1576 | if (rtl8139_pause_is_valid(rtl8139) != VALUE_RW)
|
|---|
| 1577 | return EINVAL;
|
|---|
| 1578 |
|
|---|
| 1579 | uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
|
|---|
| 1580 | msr &= ~(uint8_t)(MSR_TXFCE | MSR_RXFCE);
|
|---|
| 1581 |
|
|---|
| 1582 | if (allow_receive)
|
|---|
| 1583 | msr |= MSR_RXFCE;
|
|---|
| 1584 | if (allow_send)
|
|---|
| 1585 | msr |= MSR_TXFCE;
|
|---|
| 1586 |
|
|---|
| 1587 | pio_write_8(rtl8139->io_port + MSR, msr);
|
|---|
| 1588 |
|
|---|
| 1589 | if (allow_send && time > 0) {
|
|---|
| 1590 | ddf_msg(LVL_WARN, "Time setting is not supported in set_pause method.");
|
|---|
| 1591 | }
|
|---|
| 1592 | return EOK;
|
|---|
| 1593 | };
|
|---|
| 1594 |
|
|---|
| 1595 | /** Set operation mode of the device
|
|---|
| 1596 | *
|
|---|
| 1597 | */
|
|---|
| 1598 | static int rtl8139_set_operation_mode(ddf_fun_t *fun, int speed,
|
|---|
| 1599 | nic_channel_mode_t duplex, nic_role_t role)
|
|---|
| 1600 | {
|
|---|
| 1601 | assert(fun);
|
|---|
| 1602 |
|
|---|
| 1603 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1604 | assert(rtl8139);
|
|---|
| 1605 |
|
|---|
| 1606 | if (speed != 10 && speed != 100)
|
|---|
| 1607 | return EINVAL;
|
|---|
| 1608 | if (duplex != NIC_CM_HALF_DUPLEX && duplex != NIC_CM_FULL_DUPLEX)
|
|---|
| 1609 | return EINVAL;
|
|---|
| 1610 |
|
|---|
| 1611 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1612 |
|
|---|
| 1613 | /* Set autonegotion disabled */
|
|---|
| 1614 | bmcr_val &= ~(uint16_t)BMCR_AN_ENABLE;
|
|---|
| 1615 |
|
|---|
| 1616 | if (duplex == NIC_CM_FULL_DUPLEX) {
|
|---|
| 1617 | bmcr_val |= BMCR_DUPLEX;
|
|---|
| 1618 | } else {
|
|---|
| 1619 | bmcr_val &= ~((uint16_t)BMCR_DUPLEX);
|
|---|
| 1620 | }
|
|---|
| 1621 |
|
|---|
| 1622 | if (speed == 100) {
|
|---|
| 1623 | bmcr_val |= BMCR_Spd_100;
|
|---|
| 1624 | } else {
|
|---|
| 1625 | bmcr_val &= ~((uint16_t)BMCR_Spd_100);
|
|---|
| 1626 | }
|
|---|
| 1627 |
|
|---|
| 1628 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 1629 | pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
|
|---|
| 1630 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 1631 | return EOK;
|
|---|
| 1632 | }
|
|---|
| 1633 |
|
|---|
| 1634 | /** Enable autonegoation with specific advertisement
|
|---|
| 1635 | *
|
|---|
| 1636 | * @param dev The device to update
|
|---|
| 1637 | * @param advertisement The advertisement to set
|
|---|
| 1638 | *
|
|---|
| 1639 | * @returns EINVAL if the advertisement mode is not supproted
|
|---|
| 1640 | * @returns EOK if advertisement mode set successfully
|
|---|
| 1641 | */
|
|---|
| 1642 | static int rtl8139_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement)
|
|---|
| 1643 | {
|
|---|
| 1644 | assert(fun);
|
|---|
| 1645 |
|
|---|
| 1646 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1647 | assert(rtl8139);
|
|---|
| 1648 |
|
|---|
| 1649 | if (advertisement == 0) {
|
|---|
| 1650 | advertisement = RTL8139_AUTONEG_CAPS;
|
|---|
| 1651 | }
|
|---|
| 1652 |
|
|---|
| 1653 | if ((advertisement | RTL8139_AUTONEG_CAPS) != RTL8139_AUTONEG_CAPS)
|
|---|
| 1654 | return EINVAL; /* some unsuported mode is requested */
|
|---|
| 1655 |
|
|---|
| 1656 | assert(advertisement != 0);
|
|---|
| 1657 |
|
|---|
| 1658 | /* Set the autonegotiation advertisement */
|
|---|
| 1659 | uint16_t anar = ANAR_SELECTOR; /* default selector */
|
|---|
| 1660 | if (advertisement & ETH_AUTONEG_10BASE_T_FULL)
|
|---|
| 1661 | anar |= ANAR_10_FD;
|
|---|
| 1662 | if (advertisement & ETH_AUTONEG_10BASE_T_HALF)
|
|---|
| 1663 | anar |= ANAR_10_HD;
|
|---|
| 1664 | if (advertisement & ETH_AUTONEG_100BASE_TX_FULL)
|
|---|
| 1665 | anar |= ANAR_100TX_FD;
|
|---|
| 1666 | if (advertisement & ETH_AUTONEG_100BASE_TX_HALF)
|
|---|
| 1667 | anar |= ANAR_100TX_HD;
|
|---|
| 1668 | if (advertisement & ETH_AUTONEG_PAUSE_SYMETRIC)
|
|---|
| 1669 | anar |= ANAR_PAUSE;
|
|---|
| 1670 |
|
|---|
| 1671 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1672 | bmcr_val |= BMCR_AN_ENABLE;
|
|---|
| 1673 |
|
|---|
| 1674 | pio_write_16(rtl8139->io_port + ANAR, anar);
|
|---|
| 1675 |
|
|---|
| 1676 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 1677 | pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
|
|---|
| 1678 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 1679 | return EOK;
|
|---|
| 1680 | }
|
|---|
| 1681 |
|
|---|
| 1682 | /** Disable advertisement functionality
|
|---|
| 1683 | *
|
|---|
| 1684 | * @param dev The device to update
|
|---|
| 1685 | *
|
|---|
| 1686 | * @returns EOK
|
|---|
| 1687 | */
|
|---|
| 1688 | static int rtl8139_autoneg_disable(ddf_fun_t *fun)
|
|---|
| 1689 | {
|
|---|
| 1690 | assert(fun);
|
|---|
| 1691 |
|
|---|
| 1692 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1693 | assert(rtl8139);
|
|---|
| 1694 |
|
|---|
| 1695 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1696 |
|
|---|
| 1697 | bmcr_val &= ~((uint16_t)BMCR_AN_ENABLE);
|
|---|
| 1698 |
|
|---|
| 1699 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 1700 | pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
|
|---|
| 1701 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 1702 |
|
|---|
| 1703 | return EOK;
|
|---|
| 1704 | }
|
|---|
| 1705 |
|
|---|
| 1706 | /** Obtain the advertisement NIC framework value from the ANAR/ANLPAR register
|
|---|
| 1707 | * value
|
|---|
| 1708 | *
|
|---|
| 1709 | * @param[in] anar The ANAR register value
|
|---|
| 1710 | * @param[out] advertisement The advertisement result
|
|---|
| 1711 | */
|
|---|
| 1712 | static void rtl8139_get_anar_state(uint16_t anar, uint32_t *advertisement)
|
|---|
| 1713 | {
|
|---|
| 1714 | *advertisement = 0;
|
|---|
| 1715 | if (anar & ANAR_10_HD)
|
|---|
| 1716 | *advertisement |= ETH_AUTONEG_10BASE_T_HALF;
|
|---|
| 1717 | if (anar & ANAR_10_FD)
|
|---|
| 1718 | *advertisement |= ETH_AUTONEG_10BASE_T_FULL;
|
|---|
| 1719 | if (anar & ANAR_100TX_HD)
|
|---|
| 1720 | *advertisement |= ETH_AUTONEG_100BASE_TX_HALF;
|
|---|
| 1721 | if (anar & ANAR_100TX_FD)
|
|---|
| 1722 | *advertisement |= ETH_AUTONEG_100BASE_TX_FULL;
|
|---|
| 1723 | if (anar & ANAR_100T4)
|
|---|
| 1724 | *advertisement |= ETH_AUTONEG_100BASE_T4_HALF;
|
|---|
| 1725 | if (anar & ANAR_PAUSE)
|
|---|
| 1726 | *advertisement |= ETH_AUTONEG_PAUSE_SYMETRIC;
|
|---|
| 1727 | }
|
|---|
| 1728 |
|
|---|
| 1729 | /** Check the autonegotion state
|
|---|
| 1730 | *
|
|---|
| 1731 | * @param[in] dev The device to check
|
|---|
| 1732 | * @param[out] advertisement The device advertisement
|
|---|
| 1733 | * @param[out] their_adv The partners advertisement
|
|---|
| 1734 | * @param[out] result The autonegotion state
|
|---|
| 1735 | * @param[out] their_result The link partner autonegotion status
|
|---|
| 1736 | *
|
|---|
| 1737 | * @returns EOK
|
|---|
| 1738 | */
|
|---|
| 1739 | static int rtl8139_autoneg_probe(ddf_fun_t *fun, uint32_t *advertisement,
|
|---|
| 1740 | uint32_t *their_adv, nic_result_t *result, nic_result_t *their_result)
|
|---|
| 1741 | {
|
|---|
| 1742 | assert(fun);
|
|---|
| 1743 |
|
|---|
| 1744 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1745 | assert(rtl8139);
|
|---|
| 1746 |
|
|---|
| 1747 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1748 | uint16_t anar = pio_read_16(rtl8139->io_port + ANAR);
|
|---|
| 1749 | uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
|
|---|
| 1750 | uint16_t aner = pio_read_16(rtl8139->io_port + ANER);
|
|---|
| 1751 |
|
|---|
| 1752 | if (bmcr & BMCR_AN_ENABLE) {
|
|---|
| 1753 | *result = NIC_RESULT_ENABLED;
|
|---|
| 1754 | } else {
|
|---|
| 1755 | *result = NIC_RESULT_DISABLED;
|
|---|
| 1756 | }
|
|---|
| 1757 |
|
|---|
| 1758 | if (aner & ANER_LP_NW_ABLE) {
|
|---|
| 1759 | *their_result = NIC_RESULT_ENABLED;
|
|---|
| 1760 | } else {
|
|---|
| 1761 | *their_result = NIC_RESULT_DISABLED;
|
|---|
| 1762 | }
|
|---|
| 1763 |
|
|---|
| 1764 | rtl8139_get_anar_state(anar, advertisement);
|
|---|
| 1765 | rtl8139_get_anar_state(anar_lp, their_adv);
|
|---|
| 1766 |
|
|---|
| 1767 | return EOK;
|
|---|
| 1768 | }
|
|---|
| 1769 |
|
|---|
| 1770 | /** Restart autonegotiation process
|
|---|
| 1771 | *
|
|---|
| 1772 | * @param dev The device to update
|
|---|
| 1773 | *
|
|---|
| 1774 | * @returns EOK
|
|---|
| 1775 | */
|
|---|
| 1776 | static int rtl8139_autoneg_restart(ddf_fun_t *fun)
|
|---|
| 1777 | {
|
|---|
| 1778 | assert(fun);
|
|---|
| 1779 |
|
|---|
| 1780 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1781 | assert(rtl8139);
|
|---|
| 1782 |
|
|---|
| 1783 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
|---|
| 1784 | bmcr |= BMCR_AN_RESTART;
|
|---|
| 1785 | bmcr |= BMCR_AN_ENABLE;
|
|---|
| 1786 |
|
|---|
| 1787 | rtl8139_regs_unlock(rtl8139);
|
|---|
| 1788 | pio_write_16(rtl8139->io_port + BMCR, bmcr);
|
|---|
| 1789 | rtl8139_regs_lock(rtl8139);
|
|---|
| 1790 |
|
|---|
| 1791 | return EOK;
|
|---|
| 1792 | }
|
|---|
| 1793 |
|
|---|
| 1794 | /** Notify NIC framework about HW filtering state when promisc mode was disabled
|
|---|
| 1795 | *
|
|---|
| 1796 | * @param nic_data The NIC data
|
|---|
| 1797 | * @param mcast_mode Current multicast mode
|
|---|
| 1798 | * @param was_promisc Sign if the promiscuous mode was active before disabling
|
|---|
| 1799 | */
|
|---|
| 1800 | inline static void rtl8139_rcx_promics_rem(nic_t *nic_data,
|
|---|
| 1801 | nic_multicast_mode_t mcast_mode, uint8_t was_promisc)
|
|---|
| 1802 | {
|
|---|
| 1803 | assert(nic_data);
|
|---|
| 1804 |
|
|---|
| 1805 | if (was_promisc != 0) {
|
|---|
| 1806 | if (mcast_mode == NIC_MULTICAST_LIST)
|
|---|
| 1807 | nic_report_hw_filtering(nic_data, 1, 0, -1);
|
|---|
| 1808 | else
|
|---|
| 1809 | nic_report_hw_filtering(nic_data, 1, 1, -1);
|
|---|
| 1810 | } else {
|
|---|
| 1811 | nic_report_hw_filtering(nic_data, 1, -1, -1);
|
|---|
| 1812 | }
|
|---|
| 1813 | }
|
|---|
| 1814 |
|
|---|
| 1815 | /** Set unicast frames acceptance mode
|
|---|
| 1816 | *
|
|---|
| 1817 | * @param nic_data The nic device to update
|
|---|
| 1818 | * @param mode The mode to set
|
|---|
| 1819 | * @param addr Ignored, no HW support, just use unicast promisc
|
|---|
| 1820 | * @param addr_cnt Ignored, no HW support
|
|---|
| 1821 | *
|
|---|
| 1822 | * @returns EOK
|
|---|
| 1823 | */
|
|---|
| 1824 | static int rtl8139_unicast_set(nic_t *nic_data, nic_unicast_mode_t mode,
|
|---|
| 1825 | const nic_address_t *addr, size_t addr_cnt)
|
|---|
| 1826 | {
|
|---|
| 1827 | assert(nic_data);
|
|---|
| 1828 |
|
|---|
| 1829 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1830 | assert(rtl8139);
|
|---|
| 1831 |
|
|---|
| 1832 | uint8_t was_promisc = rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS;
|
|---|
| 1833 |
|
|---|
| 1834 | nic_multicast_mode_t mcast_mode;
|
|---|
| 1835 | nic_query_multicast(nic_data, &mcast_mode, 0, NULL, NULL);
|
|---|
| 1836 |
|
|---|
| 1837 | switch (mode) {
|
|---|
| 1838 | case NIC_UNICAST_BLOCKED:
|
|---|
| 1839 | rtl8139->rcr_data.ucast_mask = 0;
|
|---|
| 1840 | rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
|
|---|
| 1841 | break;
|
|---|
| 1842 | case NIC_UNICAST_DEFAULT:
|
|---|
| 1843 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH;
|
|---|
| 1844 | rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
|
|---|
| 1845 | break;
|
|---|
| 1846 | case NIC_UNICAST_LIST:
|
|---|
| 1847 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH
|
|---|
| 1848 | | RCR_ACCEPT_ALL_PHYS;
|
|---|
| 1849 |
|
|---|
| 1850 | if (mcast_mode == NIC_MULTICAST_PROMISC)
|
|---|
| 1851 | nic_report_hw_filtering(nic_data, 0, 1, -1);
|
|---|
| 1852 | else
|
|---|
| 1853 | nic_report_hw_filtering(nic_data, 0, 0, -1);
|
|---|
| 1854 | break;
|
|---|
| 1855 | case NIC_UNICAST_PROMISC:
|
|---|
| 1856 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH
|
|---|
| 1857 | | RCR_ACCEPT_ALL_PHYS;
|
|---|
| 1858 |
|
|---|
| 1859 | if (mcast_mode == NIC_MULTICAST_PROMISC)
|
|---|
| 1860 | nic_report_hw_filtering(nic_data, 1, 1, -1);
|
|---|
| 1861 | else
|
|---|
| 1862 | nic_report_hw_filtering(nic_data, 1, 0, -1);
|
|---|
| 1863 | break;
|
|---|
| 1864 | default:
|
|---|
| 1865 | return ENOTSUP;
|
|---|
| 1866 | }
|
|---|
| 1867 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 1868 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 1869 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 1870 | return EOK;
|
|---|
| 1871 | }
|
|---|
| 1872 |
|
|---|
| 1873 | /** Set multicast frames acceptance mode
|
|---|
| 1874 | *
|
|---|
| 1875 | * @param nic_data The nic device to update
|
|---|
| 1876 | * @param mode The mode to set
|
|---|
| 1877 | * @param addr Addresses to accept
|
|---|
| 1878 | * @param addr_cnt Addresses count
|
|---|
| 1879 | *
|
|---|
| 1880 | * @returns EOK
|
|---|
| 1881 | */
|
|---|
| 1882 | static int rtl8139_multicast_set(nic_t *nic_data, nic_multicast_mode_t mode,
|
|---|
| 1883 | const nic_address_t *addr, size_t addr_count)
|
|---|
| 1884 | {
|
|---|
| 1885 | assert(nic_data);
|
|---|
| 1886 | assert(addr_count == 0 || addr);
|
|---|
| 1887 |
|
|---|
| 1888 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1889 | assert(rtl8139);
|
|---|
| 1890 |
|
|---|
| 1891 | switch (mode) {
|
|---|
| 1892 | case NIC_MULTICAST_BLOCKED:
|
|---|
| 1893 | rtl8139->rcr_data.mcast_mask = 0;
|
|---|
| 1894 | if ((rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS) != 0)
|
|---|
| 1895 | nic_report_hw_filtering(nic_data, -1, 0, -1);
|
|---|
| 1896 | else
|
|---|
| 1897 | nic_report_hw_filtering(nic_data, -1, 1, -1);
|
|---|
| 1898 | break;
|
|---|
| 1899 | case NIC_MULTICAST_LIST:
|
|---|
| 1900 | rtl8139_hw_set_mcast_mask(rtl8139, nic_mcast_hash(addr, addr_count));
|
|---|
| 1901 | rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
|
|---|
| 1902 | nic_report_hw_filtering(nic_data, -1, 0, -1);
|
|---|
| 1903 | break;
|
|---|
| 1904 | case NIC_MULTICAST_PROMISC:
|
|---|
| 1905 | rtl8139_hw_set_mcast_mask(rtl8139, RTL8139_MCAST_MASK_PROMISC);
|
|---|
| 1906 | rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
|
|---|
| 1907 | nic_report_hw_filtering(nic_data, -1, 1, -1);
|
|---|
| 1908 | break;
|
|---|
| 1909 | default:
|
|---|
| 1910 | return ENOTSUP;
|
|---|
| 1911 | }
|
|---|
| 1912 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 1913 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 1914 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 1915 | return EOK;
|
|---|
| 1916 | }
|
|---|
| 1917 |
|
|---|
| 1918 | /** Set broadcast frames acceptance mode
|
|---|
| 1919 | *
|
|---|
| 1920 | * @param nic_data The nic device to update
|
|---|
| 1921 | * @param mode The mode to set
|
|---|
| 1922 | *
|
|---|
| 1923 | * @returns EOK
|
|---|
| 1924 | */
|
|---|
| 1925 | static int rtl8139_broadcast_set(nic_t *nic_data, nic_broadcast_mode_t mode)
|
|---|
| 1926 | {
|
|---|
| 1927 | assert(nic_data);
|
|---|
| 1928 |
|
|---|
| 1929 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 1930 | assert(rtl8139);
|
|---|
| 1931 |
|
|---|
| 1932 | switch (mode) {
|
|---|
| 1933 | case NIC_BROADCAST_BLOCKED:
|
|---|
| 1934 | rtl8139->rcr_data.bcast_mask = 0;
|
|---|
| 1935 | break;
|
|---|
| 1936 | case NIC_BROADCAST_ACCEPTED:
|
|---|
| 1937 | rtl8139->rcr_data.bcast_mask = RCR_ACCEPT_BROADCAST;
|
|---|
| 1938 | break;
|
|---|
| 1939 | default:
|
|---|
| 1940 | return ENOTSUP;
|
|---|
| 1941 | }
|
|---|
| 1942 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 1943 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 1944 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 1945 | return EOK;
|
|---|
| 1946 | }
|
|---|
| 1947 |
|
|---|
| 1948 | /** Get state of acceptance of weird frames
|
|---|
| 1949 | *
|
|---|
| 1950 | * @param[in] device The device to check
|
|---|
| 1951 | * @param[out] mode The current mode
|
|---|
| 1952 | */
|
|---|
| 1953 | static int rtl8139_defective_get_mode(ddf_fun_t *fun, uint32_t *mode)
|
|---|
| 1954 | {
|
|---|
| 1955 | assert(fun);
|
|---|
| 1956 | assert(mode);
|
|---|
| 1957 |
|
|---|
| 1958 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1959 | assert(rtl8139);
|
|---|
| 1960 |
|
|---|
| 1961 | *mode = 0;
|
|---|
| 1962 | if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_ERROR)
|
|---|
| 1963 | *mode |= NIC_DEFECTIVE_BAD_CRC;
|
|---|
| 1964 | if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_RUNT)
|
|---|
| 1965 | *mode |= NIC_DEFECTIVE_SHORT;
|
|---|
| 1966 |
|
|---|
| 1967 | return EOK;
|
|---|
| 1968 | };
|
|---|
| 1969 |
|
|---|
| 1970 | /** Set acceptance of weird frames
|
|---|
| 1971 | *
|
|---|
| 1972 | * @param device The device to update
|
|---|
| 1973 | * @param mode The mode to set
|
|---|
| 1974 | *
|
|---|
| 1975 | * @returns ENOTSUP if the mode is not supported
|
|---|
| 1976 | * @returns EOK of mode was set
|
|---|
| 1977 | */
|
|---|
| 1978 | static int rtl8139_defective_set_mode(ddf_fun_t *fun, uint32_t mode)
|
|---|
| 1979 | {
|
|---|
| 1980 | assert(fun);
|
|---|
| 1981 |
|
|---|
| 1982 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
|---|
| 1983 | assert(rtl8139);
|
|---|
| 1984 |
|
|---|
| 1985 | if ((mode & (NIC_DEFECTIVE_SHORT | NIC_DEFECTIVE_BAD_CRC)) != mode)
|
|---|
| 1986 | return ENOTSUP;
|
|---|
| 1987 |
|
|---|
| 1988 | rtl8139->rcr_data.defect_mask = 0;
|
|---|
| 1989 | if (mode & NIC_DEFECTIVE_SHORT)
|
|---|
| 1990 | rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_RUNT;
|
|---|
| 1991 | if (mode & NIC_DEFECTIVE_BAD_CRC)
|
|---|
| 1992 | rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_ERROR;
|
|---|
| 1993 |
|
|---|
| 1994 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 1995 | rtl8139_hw_update_rcr(rtl8139);
|
|---|
| 1996 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 1997 | return EOK;
|
|---|
| 1998 | };
|
|---|
| 1999 |
|
|---|
| 2000 |
|
|---|
| 2001 | /** Turn Wakeup On Lan method on
|
|---|
| 2002 | *
|
|---|
| 2003 | * @param nic_data The NIC to update
|
|---|
| 2004 | * @param virtue The method to turn on
|
|---|
| 2005 | *
|
|---|
| 2006 | * @returns EINVAL if the method is not supported
|
|---|
| 2007 | * @returns EOK if succeed
|
|---|
| 2008 | * @returns ELIMIT if no more methods of this kind can be enabled
|
|---|
| 2009 | */
|
|---|
| 2010 | static int rtl8139_wol_virtue_add(nic_t *nic_data,
|
|---|
| 2011 | const nic_wol_virtue_t *virtue)
|
|---|
| 2012 | {
|
|---|
| 2013 | assert(nic_data);
|
|---|
| 2014 | assert(virtue);
|
|---|
| 2015 |
|
|---|
| 2016 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 2017 | assert(rtl8139);
|
|---|
| 2018 |
|
|---|
| 2019 | switch(virtue->type) {
|
|---|
| 2020 | case NIC_WV_BROADCAST:
|
|---|
| 2021 | rtl8139_hw_reg_add_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
|
|---|
| 2022 | break;
|
|---|
| 2023 | case NIC_WV_LINK_CHANGE:
|
|---|
| 2024 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 2025 | rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
|
|---|
| 2026 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 2027 | break;
|
|---|
| 2028 | case NIC_WV_MAGIC_PACKET:
|
|---|
| 2029 | if (virtue->data)
|
|---|
| 2030 | return EINVAL;
|
|---|
| 2031 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 2032 | rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
|
|---|
| 2033 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 2034 | break;
|
|---|
| 2035 | default:
|
|---|
| 2036 | return EINVAL;
|
|---|
| 2037 | };
|
|---|
| 2038 | if(rtl8139->pm.active++ == 0)
|
|---|
| 2039 | rtl8139_hw_pmen_set(rtl8139, 1);
|
|---|
| 2040 | return EOK;
|
|---|
| 2041 | }
|
|---|
| 2042 |
|
|---|
| 2043 | /** Turn Wakeup On Lan method off
|
|---|
| 2044 | *
|
|---|
| 2045 | * @param nic_data The NIC to update
|
|---|
| 2046 | * @param virtue The method to turn off
|
|---|
| 2047 | */
|
|---|
| 2048 | static void rtl8139_wol_virtue_rem(nic_t *nic_data,
|
|---|
| 2049 | const nic_wol_virtue_t *virtue)
|
|---|
| 2050 | {
|
|---|
| 2051 | assert(nic_data);
|
|---|
| 2052 | assert(virtue);
|
|---|
| 2053 |
|
|---|
| 2054 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 2055 | assert(rtl8139);
|
|---|
| 2056 |
|
|---|
| 2057 | switch(virtue->type) {
|
|---|
| 2058 | case NIC_WV_BROADCAST:
|
|---|
| 2059 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
|
|---|
| 2060 | break;
|
|---|
| 2061 | case NIC_WV_LINK_CHANGE:
|
|---|
| 2062 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 2063 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
|
|---|
| 2064 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 2065 | break;
|
|---|
| 2066 | case NIC_WV_MAGIC_PACKET:
|
|---|
| 2067 | rtl8139_regs_unlock(rtl8139->io_port);
|
|---|
| 2068 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
|
|---|
| 2069 | rtl8139_regs_lock(rtl8139->io_port);
|
|---|
| 2070 | break;
|
|---|
| 2071 | default:
|
|---|
| 2072 | return;
|
|---|
| 2073 | };
|
|---|
| 2074 | rtl8139->pm.active--;
|
|---|
| 2075 | if (rtl8139->pm.active == 0)
|
|---|
| 2076 | rtl8139_hw_pmen_set(rtl8139, 0);
|
|---|
| 2077 | }
|
|---|
| 2078 |
|
|---|
| 2079 |
|
|---|
| 2080 | /** Set polling mode
|
|---|
| 2081 | *
|
|---|
| 2082 | * @param device The device to set
|
|---|
| 2083 | * @param mode The mode to set
|
|---|
| 2084 | * @param period The period for NIC_POLL_PERIODIC
|
|---|
| 2085 | *
|
|---|
| 2086 | * @returns EOK if succeed
|
|---|
| 2087 | * @returns ENOTSUP if the mode is not supported
|
|---|
| 2088 | */
|
|---|
| 2089 | static int rtl8139_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
|
|---|
| 2090 | const struct timeval *period)
|
|---|
| 2091 | {
|
|---|
| 2092 | assert(nic_data);
|
|---|
| 2093 | int rc = EOK;
|
|---|
| 2094 |
|
|---|
| 2095 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 2096 | assert(rtl8139);
|
|---|
| 2097 |
|
|---|
| 2098 | fibril_mutex_lock(&rtl8139->rx_lock);
|
|---|
| 2099 |
|
|---|
| 2100 | switch(mode) {
|
|---|
| 2101 | case NIC_POLL_IMMEDIATE:
|
|---|
| 2102 | rtl8139->int_mask = RTL_DEFAULT_INTERRUPTS;
|
|---|
| 2103 | break;
|
|---|
| 2104 | case NIC_POLL_ON_DEMAND:
|
|---|
| 2105 | rtl8139->int_mask = 0;
|
|---|
| 2106 | break;
|
|---|
| 2107 | case NIC_POLL_PERIODIC:
|
|---|
| 2108 | assert(period);
|
|---|
| 2109 |
|
|---|
| 2110 | rtl8139_timer_act_t new_timer;
|
|---|
| 2111 | rc = rtl8139_timer_act_init(&new_timer, RTL8139_PCI_FREQ_KHZ, period);
|
|---|
| 2112 | if (rc != EOK)
|
|---|
| 2113 | break;
|
|---|
| 2114 |
|
|---|
| 2115 | /* Disable timer interrupts while working with timer-related data */
|
|---|
| 2116 | rtl8139->int_mask = 0;
|
|---|
| 2117 | rtl8139_hw_int_enable(rtl8139);
|
|---|
| 2118 |
|
|---|
| 2119 | rtl8139->poll_timer = new_timer;
|
|---|
| 2120 | rtl8139->int_mask = INT_TIME_OUT;
|
|---|
| 2121 |
|
|---|
| 2122 | /* Force timer interrupt start be writing nonzero value to timer
|
|---|
| 2123 | * interrutp register (should be small to prevent big delay)
|
|---|
| 2124 | * Read TCTR to reset timer counter
|
|---|
| 2125 | * Change values to simulate the last interrupt from the period.
|
|---|
| 2126 | */
|
|---|
| 2127 | pio_write_32(rtl8139->io_port + TIMERINT, 10);
|
|---|
| 2128 | pio_write_32(rtl8139->io_port + TCTR, 0);
|
|---|
| 2129 |
|
|---|
| 2130 | ddf_msg(LVL_DEBUG, "Periodic mode. Interrupt mask %" PRIx16 ", "
|
|---|
| 2131 | "poll.full_skips %zu, last timer %" PRIu32,
|
|---|
| 2132 | rtl8139->int_mask, rtl8139->poll_timer.full_skips,
|
|---|
| 2133 | rtl8139->poll_timer.last_val);
|
|---|
| 2134 | break;
|
|---|
| 2135 | default:
|
|---|
| 2136 | rc = ENOTSUP;
|
|---|
| 2137 | break;
|
|---|
| 2138 | }
|
|---|
| 2139 |
|
|---|
| 2140 | rtl8139_hw_int_enable(rtl8139);
|
|---|
| 2141 |
|
|---|
| 2142 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
|---|
| 2143 |
|
|---|
| 2144 | return rc;
|
|---|
| 2145 | }
|
|---|
| 2146 |
|
|---|
| 2147 | /** Force receiving all frames in the receive buffer
|
|---|
| 2148 | *
|
|---|
| 2149 | * @param device The device to receive
|
|---|
| 2150 | */
|
|---|
| 2151 | static void rtl8139_poll(nic_t *nic_data)
|
|---|
| 2152 | {
|
|---|
| 2153 | assert(nic_data);
|
|---|
| 2154 |
|
|---|
| 2155 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
|---|
| 2156 | assert(rtl8139);
|
|---|
| 2157 |
|
|---|
| 2158 | uint16_t isr = pio_read_16(rtl8139->io_port + ISR);
|
|---|
| 2159 | pio_write_16(rtl8139->io_port + ISR, 0);
|
|---|
| 2160 |
|
|---|
| 2161 | rtl8139_interrupt_impl(nic_data, isr);
|
|---|
| 2162 | }
|
|---|
| 2163 |
|
|---|
| 2164 |
|
|---|
| 2165 | /** Main function of RTL8139 driver
|
|---|
| 2166 | *
|
|---|
| 2167 | * Just initialize the driver structures and
|
|---|
| 2168 | * put it into the device drivers interface
|
|---|
| 2169 | */
|
|---|
| 2170 | int main(void)
|
|---|
| 2171 | {
|
|---|
| 2172 | printf("%s: HelenOS RTL8139 network adapter driver\n", NAME);
|
|---|
| 2173 |
|
|---|
| 2174 | int rc = nic_driver_init(NAME);
|
|---|
| 2175 | if (rc != EOK)
|
|---|
| 2176 | return rc;
|
|---|
| 2177 |
|
|---|
| 2178 | nic_driver_implement(&rtl8139_driver_ops, &rtl8139_dev_ops,
|
|---|
| 2179 | &rtl8139_nic_iface);
|
|---|
| 2180 |
|
|---|
| 2181 | ddf_log_init(NAME);
|
|---|
| 2182 | return ddf_driver_main(&rtl8139_driver);
|
|---|
| 2183 | }
|
|---|