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