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