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