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