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