1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Michalec
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <assert.h>
|
---|
30 | #include <async.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <align.h>
|
---|
33 | #include <byteorder.h>
|
---|
34 | #include <barrier.h>
|
---|
35 | #include <as.h>
|
---|
36 | #include <ddf/log.h>
|
---|
37 | #include <ddf/interrupt.h>
|
---|
38 | #include <device/hw_res.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <nic.h>
|
---|
41 | #include <pci_dev_iface.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <pcapdump_iface.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 | 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 | *
|
---|
86 | * @param rtl8139 RTL8139 private data
|
---|
87 | *
|
---|
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
|
---|
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 | inline 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 | inline 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 | 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 |
|
---|
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 | 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);
|
---|
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 | inline 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 | inline 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 |
|
---|
342 | /** Basic driver operations for RTL8139 driver */
|
---|
343 | static driver_ops_t rtl8139_driver_ops = {
|
---|
344 | .dev_add = &rtl8139_dev_add,
|
---|
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 */
|
---|
354 | static errno_t rtl8139_on_activated(nic_t *nic_data);
|
---|
355 | static errno_t rtl8139_on_stopped(nic_t *nic_data);
|
---|
356 | static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size);
|
---|
357 |
|
---|
358 | /** Check if the transmit buffer is busy */
|
---|
359 | #define rtl8139_tbuf_busy(tsd) ((pio_read_32(tsd) & TSD_OWN) == 0)
|
---|
360 |
|
---|
361 | /** Send frame with the hardware
|
---|
362 | *
|
---|
363 | * note: the main_lock is locked when framework calls this function
|
---|
364 | *
|
---|
365 | * @param nic_data The nic driver data structure
|
---|
366 | * @param data Frame data
|
---|
367 | * @param size Frame size in bytes
|
---|
368 | *
|
---|
369 | * @return EOK if succeed, error code in the case of error
|
---|
370 | */
|
---|
371 | static void rtl8139_send_frame(nic_t *nic_data, void *data, size_t size)
|
---|
372 | {
|
---|
373 | assert(nic_data);
|
---|
374 |
|
---|
375 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
376 | assert(rtl8139);
|
---|
377 | ddf_msg(LVL_DEBUG, "Sending frame");
|
---|
378 |
|
---|
379 | if (size > RTL8139_FRAME_MAX_LENGTH) {
|
---|
380 | ddf_msg(LVL_ERROR, "Send frame: frame too long, %zu bytes",
|
---|
381 | size);
|
---|
382 | nic_report_send_error(rtl8139->nic_data, NIC_SEC_OTHER, 1);
|
---|
383 | goto err_size;
|
---|
384 | }
|
---|
385 |
|
---|
386 | assert((size & TSD_SIZE_MASK) == size);
|
---|
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 |
|
---|
404 | /* Get address of the buffer descriptor and frame data */
|
---|
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 |
|
---|
411 | /* Write frame data to the buffer, set the size to TSD and clear OWN bit */
|
---|
412 | memcpy(buf_addr, data, size);
|
---|
413 |
|
---|
414 | /* Set size of the data to send */
|
---|
415 | uint32_t tsd_value = pio_read_32(tsd);
|
---|
416 | tsd_value = rtl8139_tsd_set_size(tsd_value, size);
|
---|
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;
|
---|
425 |
|
---|
426 | err_busy_no_inc:
|
---|
427 | err_size:
|
---|
428 | return;
|
---|
429 | }
|
---|
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();
|
---|
439 | while (pio_read_8(io_base + CR) & CR_RST) {
|
---|
440 | fibril_usleep(1);
|
---|
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 |
|
---|
471 | /** Create frame structure from the buffer data
|
---|
472 | *
|
---|
473 | * @param nic_data NIC driver data
|
---|
474 | * @param rx_buffer The receiver buffer
|
---|
475 | * @param rx_size The buffer size
|
---|
476 | * @param frame_start The offset where packet data start
|
---|
477 | * @param frame_size The size of the frame data
|
---|
478 | *
|
---|
479 | * @return The frame list node (not connected)
|
---|
480 | *
|
---|
481 | */
|
---|
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)
|
---|
484 | {
|
---|
485 | nic_frame_t *frame = nic_alloc_frame(nic_data, frame_size);
|
---|
486 | if (!frame) {
|
---|
487 | ddf_msg(LVL_ERROR, "Can not allocate frame for received frame.");
|
---|
488 | return NULL;
|
---|
489 | }
|
---|
490 |
|
---|
491 | void *ret = rtl8139_memcpy_wrapped(frame->data, rx_buffer, frame_start,
|
---|
492 | RxBUF_SIZE, frame_size);
|
---|
493 | if (ret == NULL) {
|
---|
494 | nic_release_frame(nic_data, frame);
|
---|
495 | return NULL;
|
---|
496 | }
|
---|
497 | return frame;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Reset receiver
|
---|
502 | *
|
---|
503 | * Use in the case of receiver error (lost in the rx_buff)
|
---|
504 | *
|
---|
505 | * @param rtl8139 controller private data
|
---|
506 | */
|
---|
507 | static void rtl8139_rx_reset(rtl8139_t *rtl8139)
|
---|
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);
|
---|
517 | pio_write_32(rtl8139->io_port + RBSTART,
|
---|
518 | PTR2U32(rtl8139->rx_buff_phys));
|
---|
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 |
|
---|
529 | /** Receive all frames in queue
|
---|
530 | *
|
---|
531 | * @param nic_data The controller data
|
---|
532 | * @return The linked list of nic_frame_list_t nodes, each containing one frame
|
---|
533 | */
|
---|
534 | static nic_frame_list_t *rtl8139_frame_receive(nic_t *nic_data)
|
---|
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)
|
---|
542 | ddf_msg(LVL_ERROR, "Can not allocate frame list for received frames.");
|
---|
543 |
|
---|
544 | void *rx_buffer = rtl8139->rx_buff_virt;
|
---|
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;
|
---|
556 |
|
---|
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;
|
---|
566 | uint32_t frame_header = uint32_t_le2host(*((uint32_t *)rx_ptr));
|
---|
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;
|
---|
571 |
|
---|
572 | if (size == RTL8139_EARLY_SIZE) {
|
---|
573 | /* The frame copying is still in progress, break receiving */
|
---|
574 | ddf_msg(LVL_DEBUG, "Early threshold reached, not completely coppied");
|
---|
575 | break;
|
---|
576 | }
|
---|
577 |
|
---|
578 | /* Check if the header is valid, otherwise we are lost in the buffer */
|
---|
579 | if (size == 0 || size > RTL8139_FRAME_MAX_LENGTH) {
|
---|
580 | ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (size: %4" PRIu16 ", "
|
---|
581 | "header 0x%4" PRIx32 ". Offset: %" PRIu16 ")", size, frame_header,
|
---|
582 | rx_offset);
|
---|
583 | goto rx_err;
|
---|
584 | }
|
---|
585 | if (size < RTL8139_RUNT_MAX_SIZE && !(rcs & RSR_RUNT)) {
|
---|
586 | ddf_msg(LVL_ERROR, "Receiver error -> receiver reset (%" PRIx16 ")", size);
|
---|
587 | goto rx_err;
|
---|
588 | }
|
---|
589 |
|
---|
590 | cur_read += size + RTL_FRAME_HEADER_SIZE;
|
---|
591 | if (cur_read > max_read)
|
---|
592 | break;
|
---|
593 |
|
---|
594 | if (frames) {
|
---|
595 | nic_frame_t *frame = rtl8139_read_frame(nic_data, rx_buffer,
|
---|
596 | RxBUF_SIZE, rx_offset + RTL_FRAME_HEADER_SIZE, frame_size);
|
---|
597 |
|
---|
598 | if (frame)
|
---|
599 | nic_frame_list_append(frames, frame);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /* Update offset */
|
---|
603 | rx_offset = ALIGN_UP(rx_offset + size + RTL_FRAME_HEADER_SIZE, 4);
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Write lesser value to prevent overflow into unread frame
|
---|
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;
|
---|
619 | }
|
---|
620 |
|
---|
621 | irq_pio_range_t rtl8139_irq_pio_ranges[] = {
|
---|
622 | {
|
---|
623 | .base = 0,
|
---|
624 | .size = RTL8139_IO_SIZE
|
---|
625 | }
|
---|
626 | };
|
---|
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[] = {
|
---|
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 | }
|
---|
662 | };
|
---|
663 |
|
---|
664 | /** Interrupt code definition */
|
---|
665 | irq_code_t rtl8139_irq_code = {
|
---|
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),
|
---|
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;
|
---|
686 | void *tsd_to_check = rtl8139->io_port + TSD0 +
|
---|
687 | desc_to_check * sizeof(uint32_t);
|
---|
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 |
|
---|
696 | /* If the frame was sent */
|
---|
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 |
|
---|
726 | /** Receive all frames from the buffer
|
---|
727 | *
|
---|
728 | * @param rtl8139 driver private data
|
---|
729 | */
|
---|
730 | static void rtl8139_receive_frames(nic_t *nic_data)
|
---|
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);
|
---|
738 | nic_frame_list_t *frames = rtl8139_frame_receive(nic_data);
|
---|
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);
|
---|
762 | ddf_msg(LVL_DEBUG, "rtl8139 timer: %" PRIu32 "\treceive: %d", timer_val, receive);
|
---|
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);
|
---|
779 |
|
---|
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 | }
|
---|
788 | if (!receive)
|
---|
789 | return;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /*
|
---|
793 | * Check transmittion interrupts first to allow transmit next frames
|
---|
794 | * sooner
|
---|
795 | */
|
---|
796 | if (isr & (INT_TOK | INT_TER)) {
|
---|
797 | rtl8139_tx_interrupt(nic_data);
|
---|
798 | }
|
---|
799 | if (isr & INT_ROK) {
|
---|
800 | rtl8139_receive_frames(nic_data);
|
---|
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 | *
|
---|
821 | * @param icall The IPC call structure
|
---|
822 | * @param dev The rtl8139 device
|
---|
823 | *
|
---|
824 | */
|
---|
825 | static void rtl8139_interrupt_handler(ipc_call_t *icall, ddf_dev_t *dev)
|
---|
826 | {
|
---|
827 | assert(dev);
|
---|
828 | assert(icall);
|
---|
829 |
|
---|
830 | uint16_t isr = (uint16_t) ipc_get_arg2(icall);
|
---|
831 | nic_t *nic_data = nic_get_from_ddf_dev(dev);
|
---|
832 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
833 |
|
---|
834 | rtl8139_interrupt_impl(nic_data, isr);
|
---|
835 |
|
---|
836 | /* Turn the interrupts on again */
|
---|
837 | rtl8139_hw_int_set(rtl8139);
|
---|
838 | }
|
---|
839 |
|
---|
840 | /** Register interrupt handler for the card in the system
|
---|
841 | *
|
---|
842 | * Note: the global irq_reg_mutex is locked because of work with global
|
---|
843 | * structure.
|
---|
844 | *
|
---|
845 | * @param nic_data The driver data
|
---|
846 | *
|
---|
847 | * @param[out] handle IRQ capability handle if the handler was registered.
|
---|
848 | *
|
---|
849 | * @return An error code otherwise.
|
---|
850 | */
|
---|
851 | inline static errno_t rtl8139_register_int_handler(nic_t *nic_data,
|
---|
852 | cap_irq_handle_t *handle)
|
---|
853 | {
|
---|
854 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
855 |
|
---|
856 | /* Lock the mutex in whole driver while working with global structure */
|
---|
857 | RTL8139_IRQ_STRUCT_LOCK();
|
---|
858 |
|
---|
859 | rtl8139_irq_code.ranges[0].base = (uintptr_t) rtl8139->io_addr;
|
---|
860 | rtl8139_irq_code.cmds[0].addr = rtl8139->io_addr + ISR;
|
---|
861 | rtl8139_irq_code.cmds[2].addr = rtl8139->io_addr + ISR;
|
---|
862 | rtl8139_irq_code.cmds[3].addr = rtl8139->io_addr + IMR;
|
---|
863 | errno_t rc = register_interrupt_handler(nic_get_ddf_dev(nic_data),
|
---|
864 | rtl8139->irq, rtl8139_interrupt_handler, &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 | inline 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 | /** The add_device callback of RTL8139 callback
|
---|
1253 | *
|
---|
1254 | * Probe and initialize the newly added device.
|
---|
1255 | *
|
---|
1256 | * @param dev The RTL8139 device.
|
---|
1257 | *
|
---|
1258 | * @return EOK if added successfully, error code otherwise
|
---|
1259 | */
|
---|
1260 | errno_t rtl8139_dev_add(ddf_dev_t *dev)
|
---|
1261 | {
|
---|
1262 | ddf_fun_t *fun;
|
---|
1263 |
|
---|
1264 | ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %zu)",
|
---|
1265 | ddf_dev_get_name(dev), ddf_dev_get_handle(dev));
|
---|
1266 |
|
---|
1267 | /* Init device structure for rtl8139 */
|
---|
1268 | errno_t rc = rtl8139_device_initialize(dev);
|
---|
1269 | if (rc != EOK)
|
---|
1270 | return rc;
|
---|
1271 |
|
---|
1272 | /* Map I/O ports */
|
---|
1273 | rc = rtl8139_pio_enable(dev);
|
---|
1274 | if (rc != EOK)
|
---|
1275 | goto err_destroy;
|
---|
1276 |
|
---|
1277 | nic_t *nic_data = nic_get_from_ddf_dev(dev);
|
---|
1278 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1279 |
|
---|
1280 | nic_address_t addr;
|
---|
1281 | rtl8139_hw_get_addr(rtl8139, &addr);
|
---|
1282 | rc = nic_report_address(nic_data, &addr);
|
---|
1283 | if (rc != EOK)
|
---|
1284 | goto err_pio;
|
---|
1285 |
|
---|
1286 | /* Initialize the driver private structure */
|
---|
1287 | rtl8139_data_init(rtl8139);
|
---|
1288 |
|
---|
1289 | /* Register interrupt handler */
|
---|
1290 | cap_irq_handle_t irq_handle;
|
---|
1291 | rc = rtl8139_register_int_handler(nic_data, &irq_handle);
|
---|
1292 | if (rc != EOK) {
|
---|
1293 | goto err_pio;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
|
---|
1297 | if (fun == NULL) {
|
---|
1298 | ddf_msg(LVL_ERROR, "Failed creating device function");
|
---|
1299 | goto err_srv;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | nic_set_ddf_fun(nic_data, fun);
|
---|
1303 | ddf_fun_set_ops(fun, &rtl8139_dev_ops);
|
---|
1304 |
|
---|
1305 | rc = ddf_fun_bind(fun);
|
---|
1306 | if (rc != EOK) {
|
---|
1307 | ddf_msg(LVL_ERROR, "Failed binding device function");
|
---|
1308 | goto err_fun_create;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | rc = nic_fun_add_to_cats(fun);
|
---|
1312 | if (rc != EOK) {
|
---|
1313 | ddf_msg(LVL_ERROR, "Failed adding function to categories");
|
---|
1314 | ddf_fun_unbind(fun);
|
---|
1315 | return rc;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
|
---|
1319 | ddf_dev_get_name(dev));
|
---|
1320 |
|
---|
1321 | return EOK;
|
---|
1322 |
|
---|
1323 | // err_fun_bind:
|
---|
1324 | // ddf_fun_unbind(fun);
|
---|
1325 | err_fun_create:
|
---|
1326 | ddf_fun_destroy(fun);
|
---|
1327 | err_srv:
|
---|
1328 | unregister_interrupt_handler(dev, irq_handle);
|
---|
1329 | err_pio:
|
---|
1330 | // rtl8139_pio_disable(dev);
|
---|
1331 | /* TODO: find out if the pio_disable is needed */
|
---|
1332 | err_destroy:
|
---|
1333 | rtl8139_dev_cleanup(dev);
|
---|
1334 | return rc;
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | /** Set card MAC address
|
---|
1338 | *
|
---|
1339 | * @param device The RTL8139 device
|
---|
1340 | * @param address The place to store the address
|
---|
1341 | * @param max_len Maximal addresss length to store
|
---|
1342 | *
|
---|
1343 | * @return EOK if succeed, error code otherwise
|
---|
1344 | */
|
---|
1345 | static errno_t rtl8139_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
|
---|
1346 | {
|
---|
1347 | assert(fun);
|
---|
1348 | assert(addr);
|
---|
1349 |
|
---|
1350 | nic_t *nic_data = nic_get_from_ddf_fun((fun));
|
---|
1351 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1352 | assert(rtl8139);
|
---|
1353 |
|
---|
1354 | rtl8139_lock_all(rtl8139);
|
---|
1355 |
|
---|
1356 | errno_t rc = nic_report_address(nic_data, addr);
|
---|
1357 | if (rc != EOK) {
|
---|
1358 | rtl8139_unlock_all(rtl8139);
|
---|
1359 | return rc;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | rtl8139_hw_set_addr(rtl8139, addr);
|
---|
1363 |
|
---|
1364 | rtl8139_unlock_all(rtl8139);
|
---|
1365 | return EOK;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | /** Get the device information
|
---|
1369 | *
|
---|
1370 | * @param dev The NIC device
|
---|
1371 | * @param info The information to fill
|
---|
1372 | *
|
---|
1373 | * @return EOK
|
---|
1374 | */
|
---|
1375 | static errno_t rtl8139_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
|
---|
1376 | {
|
---|
1377 | assert(fun);
|
---|
1378 | assert(info);
|
---|
1379 |
|
---|
1380 | nic_t *nic_data = nic_get_from_ddf_fun(fun);
|
---|
1381 | assert(nic_data);
|
---|
1382 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1383 | assert(rtl8139);
|
---|
1384 |
|
---|
1385 | /* TODO: fill the information more completely */
|
---|
1386 | info->vendor_id = 0x10ec;
|
---|
1387 | str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH, "Realtek");
|
---|
1388 |
|
---|
1389 | if (rtl8139->hw_version < RTL8139_VER_COUNT) {
|
---|
1390 | str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
|
---|
1391 | model_names[rtl8139->hw_version]);
|
---|
1392 | } else {
|
---|
1393 | str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, "RTL8139");
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | info->ethernet_support[ETH_10M] = ETH_10BASE_T;
|
---|
1397 | info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
|
---|
1398 |
|
---|
1399 | info->autoneg_support = RTL8139_AUTONEG_CAPS;
|
---|
1400 | return EOK;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | /** Check the cable state
|
---|
1404 | *
|
---|
1405 | * @param[in] dev The device
|
---|
1406 | * @param[out] state The state to fill
|
---|
1407 | *
|
---|
1408 | * @return EOK
|
---|
1409 | */
|
---|
1410 | static errno_t rtl8139_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
|
---|
1411 | {
|
---|
1412 | assert(fun);
|
---|
1413 | assert(state);
|
---|
1414 |
|
---|
1415 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1416 | assert(rtl8139);
|
---|
1417 |
|
---|
1418 | if (pio_read_16(rtl8139->io_port + CSCR) & CS_CON_STATUS) {
|
---|
1419 | *state = NIC_CS_PLUGGED;
|
---|
1420 | } else {
|
---|
1421 | *state = NIC_CS_UNPLUGGED;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | return EOK;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | /** Get operation mode of the device
|
---|
1428 | */
|
---|
1429 | static errno_t rtl8139_get_operation_mode(ddf_fun_t *fun, int *speed,
|
---|
1430 | nic_channel_mode_t *duplex, nic_role_t *role)
|
---|
1431 | {
|
---|
1432 | assert(fun);
|
---|
1433 | assert(speed);
|
---|
1434 | assert(duplex);
|
---|
1435 | assert(role);
|
---|
1436 |
|
---|
1437 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1438 | assert(rtl8139);
|
---|
1439 |
|
---|
1440 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1441 | uint8_t msr_val = pio_read_8(rtl8139->io_port + MSR);
|
---|
1442 |
|
---|
1443 | if (bmcr_val & BMCR_DUPLEX) {
|
---|
1444 | *duplex = NIC_CM_FULL_DUPLEX;
|
---|
1445 | } else {
|
---|
1446 | *duplex = NIC_CM_HALF_DUPLEX;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | if (msr_val & MSR_SPEED10) {
|
---|
1450 | *speed = 10;
|
---|
1451 | } else {
|
---|
1452 | *speed = 100;
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | *role = NIC_ROLE_UNKNOWN;
|
---|
1456 | return EOK;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | /** Value validity */
|
---|
1460 | enum access_mode {
|
---|
1461 | /** Value is invalid now */
|
---|
1462 | VALUE_INVALID = 0,
|
---|
1463 | /** Read-only */
|
---|
1464 | VALUE_RO,
|
---|
1465 | /** Read-write */
|
---|
1466 | VALUE_RW
|
---|
1467 | };
|
---|
1468 |
|
---|
1469 | /** Check if pause frame operations are valid in current situation
|
---|
1470 | *
|
---|
1471 | * @param rtl8139 RTL8139 private structure
|
---|
1472 | *
|
---|
1473 | * @return VALUE_INVALID if the value has no sense in current moment
|
---|
1474 | * @return VALUE_RO if the state is read-only in current moment
|
---|
1475 | * @return VALUE_RW if the state can be modified
|
---|
1476 | */
|
---|
1477 | static int rtl8139_pause_is_valid(rtl8139_t *rtl8139)
|
---|
1478 | {
|
---|
1479 | assert(rtl8139);
|
---|
1480 |
|
---|
1481 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1482 | if ((bmcr & (BMCR_AN_ENABLE | BMCR_DUPLEX)) == 0)
|
---|
1483 | return VALUE_INVALID;
|
---|
1484 |
|
---|
1485 | if (bmcr & BMCR_AN_ENABLE) {
|
---|
1486 | uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
|
---|
1487 | if (anar_lp & ANAR_PAUSE)
|
---|
1488 | return VALUE_RO;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | return VALUE_RW;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | /** Get current pause frame configuration
|
---|
1495 | *
|
---|
1496 | * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
|
---|
1497 | * the moment (half-duplex).
|
---|
1498 | *
|
---|
1499 | * @param[in] fun The DDF structure of the RTL8139
|
---|
1500 | * @param[out] we_send Sign if local constroller sends pause frame
|
---|
1501 | * @param[out] we_receive Sign if local constroller receives pause frame
|
---|
1502 | * @param[out] time Time filled in pause frames. 0xFFFF in rtl8139
|
---|
1503 | *
|
---|
1504 | * @return EOK if succeed
|
---|
1505 | */
|
---|
1506 | static errno_t rtl8139_pause_get(ddf_fun_t *fun, nic_result_t *we_send,
|
---|
1507 | nic_result_t *we_receive, uint16_t *time)
|
---|
1508 | {
|
---|
1509 | assert(fun);
|
---|
1510 | assert(we_send);
|
---|
1511 | assert(we_receive);
|
---|
1512 |
|
---|
1513 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1514 | assert(rtl8139);
|
---|
1515 |
|
---|
1516 | if (rtl8139_pause_is_valid(rtl8139) == VALUE_INVALID) {
|
---|
1517 | *we_send = NIC_RESULT_NOT_AVAILABLE;
|
---|
1518 | *we_receive = NIC_RESULT_NOT_AVAILABLE;
|
---|
1519 | *time = 0;
|
---|
1520 | return EOK;
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
|
---|
1524 |
|
---|
1525 | *we_send = (msr & MSR_TXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
|
---|
1526 | *we_receive = (msr & MSR_RXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
|
---|
1527 | *time = RTL8139_PAUSE_VAL;
|
---|
1528 |
|
---|
1529 | return EOK;
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | /** Set current pause frame configuration
|
---|
1533 | *
|
---|
1534 | * @param fun The DDF structure of the RTL8139
|
---|
1535 | * @param allow_send Sign if local constroller sends pause frame
|
---|
1536 | * @param allow_receive Sign if local constroller receives pause frames
|
---|
1537 | * @param time Time to use, ignored (not supported by device)
|
---|
1538 | *
|
---|
1539 | * @return EOK if succeed, INVAL if the pause frame has no sence
|
---|
1540 | */
|
---|
1541 | static errno_t rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
|
---|
1542 | uint16_t time)
|
---|
1543 | {
|
---|
1544 | assert(fun);
|
---|
1545 |
|
---|
1546 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1547 | assert(rtl8139);
|
---|
1548 |
|
---|
1549 | if (rtl8139_pause_is_valid(rtl8139) != VALUE_RW)
|
---|
1550 | return EINVAL;
|
---|
1551 |
|
---|
1552 | uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
|
---|
1553 | msr &= ~(uint8_t)(MSR_TXFCE | MSR_RXFCE);
|
---|
1554 |
|
---|
1555 | if (allow_receive)
|
---|
1556 | msr |= MSR_RXFCE;
|
---|
1557 | if (allow_send)
|
---|
1558 | msr |= MSR_TXFCE;
|
---|
1559 |
|
---|
1560 | pio_write_8(rtl8139->io_port + MSR, msr);
|
---|
1561 |
|
---|
1562 | if (allow_send && time > 0) {
|
---|
1563 | ddf_msg(LVL_WARN, "Time setting is not supported in set_pause method.");
|
---|
1564 | }
|
---|
1565 | return EOK;
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | /** Set operation mode of the device
|
---|
1569 | *
|
---|
1570 | */
|
---|
1571 | static errno_t rtl8139_set_operation_mode(ddf_fun_t *fun, int speed,
|
---|
1572 | nic_channel_mode_t duplex, nic_role_t role)
|
---|
1573 | {
|
---|
1574 | assert(fun);
|
---|
1575 |
|
---|
1576 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1577 | assert(rtl8139);
|
---|
1578 |
|
---|
1579 | if (speed != 10 && speed != 100)
|
---|
1580 | return EINVAL;
|
---|
1581 | if (duplex != NIC_CM_HALF_DUPLEX && duplex != NIC_CM_FULL_DUPLEX)
|
---|
1582 | return EINVAL;
|
---|
1583 |
|
---|
1584 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1585 |
|
---|
1586 | /* Set autonegotion disabled */
|
---|
1587 | bmcr_val &= ~(uint16_t)BMCR_AN_ENABLE;
|
---|
1588 |
|
---|
1589 | if (duplex == NIC_CM_FULL_DUPLEX) {
|
---|
1590 | bmcr_val |= BMCR_DUPLEX;
|
---|
1591 | } else {
|
---|
1592 | bmcr_val &= ~((uint16_t)BMCR_DUPLEX);
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | if (speed == 100) {
|
---|
1596 | bmcr_val |= BMCR_Spd_100;
|
---|
1597 | } else {
|
---|
1598 | bmcr_val &= ~((uint16_t)BMCR_Spd_100);
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
1602 | pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
|
---|
1603 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
1604 | return EOK;
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /** Enable autonegoation with specific advertisement
|
---|
1608 | *
|
---|
1609 | * @param dev The device to update
|
---|
1610 | * @param advertisement The advertisement to set
|
---|
1611 | *
|
---|
1612 | * @returns EINVAL if the advertisement mode is not supproted
|
---|
1613 | * @returns EOK if advertisement mode set successfully
|
---|
1614 | */
|
---|
1615 | static errno_t rtl8139_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement)
|
---|
1616 | {
|
---|
1617 | assert(fun);
|
---|
1618 |
|
---|
1619 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1620 | assert(rtl8139);
|
---|
1621 |
|
---|
1622 | if (advertisement == 0) {
|
---|
1623 | advertisement = RTL8139_AUTONEG_CAPS;
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | if ((advertisement | RTL8139_AUTONEG_CAPS) != RTL8139_AUTONEG_CAPS)
|
---|
1627 | return EINVAL; /* some unsuported mode is requested */
|
---|
1628 |
|
---|
1629 | assert(advertisement != 0);
|
---|
1630 |
|
---|
1631 | /* Set the autonegotiation advertisement */
|
---|
1632 | uint16_t anar = ANAR_SELECTOR; /* default selector */
|
---|
1633 | if (advertisement & ETH_AUTONEG_10BASE_T_FULL)
|
---|
1634 | anar |= ANAR_10_FD;
|
---|
1635 | if (advertisement & ETH_AUTONEG_10BASE_T_HALF)
|
---|
1636 | anar |= ANAR_10_HD;
|
---|
1637 | if (advertisement & ETH_AUTONEG_100BASE_TX_FULL)
|
---|
1638 | anar |= ANAR_100TX_FD;
|
---|
1639 | if (advertisement & ETH_AUTONEG_100BASE_TX_HALF)
|
---|
1640 | anar |= ANAR_100TX_HD;
|
---|
1641 | if (advertisement & ETH_AUTONEG_PAUSE_SYMETRIC)
|
---|
1642 | anar |= ANAR_PAUSE;
|
---|
1643 |
|
---|
1644 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1645 | bmcr_val |= BMCR_AN_ENABLE;
|
---|
1646 |
|
---|
1647 | pio_write_16(rtl8139->io_port + ANAR, anar);
|
---|
1648 |
|
---|
1649 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
1650 | pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
|
---|
1651 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
1652 | return EOK;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | /** Disable advertisement functionality
|
---|
1656 | *
|
---|
1657 | * @param dev The device to update
|
---|
1658 | *
|
---|
1659 | * @returns EOK
|
---|
1660 | */
|
---|
1661 | static errno_t rtl8139_autoneg_disable(ddf_fun_t *fun)
|
---|
1662 | {
|
---|
1663 | assert(fun);
|
---|
1664 |
|
---|
1665 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1666 | assert(rtl8139);
|
---|
1667 |
|
---|
1668 | uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1669 |
|
---|
1670 | bmcr_val &= ~((uint16_t)BMCR_AN_ENABLE);
|
---|
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 |
|
---|
1676 | return EOK;
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | /** Obtain the advertisement NIC framework value from the ANAR/ANLPAR register
|
---|
1680 | * value
|
---|
1681 | *
|
---|
1682 | * @param[in] anar The ANAR register value
|
---|
1683 | * @param[out] advertisement The advertisement result
|
---|
1684 | */
|
---|
1685 | static void rtl8139_get_anar_state(uint16_t anar, uint32_t *advertisement)
|
---|
1686 | {
|
---|
1687 | *advertisement = 0;
|
---|
1688 | if (anar & ANAR_10_HD)
|
---|
1689 | *advertisement |= ETH_AUTONEG_10BASE_T_HALF;
|
---|
1690 | if (anar & ANAR_10_FD)
|
---|
1691 | *advertisement |= ETH_AUTONEG_10BASE_T_FULL;
|
---|
1692 | if (anar & ANAR_100TX_HD)
|
---|
1693 | *advertisement |= ETH_AUTONEG_100BASE_TX_HALF;
|
---|
1694 | if (anar & ANAR_100TX_FD)
|
---|
1695 | *advertisement |= ETH_AUTONEG_100BASE_TX_FULL;
|
---|
1696 | if (anar & ANAR_100T4)
|
---|
1697 | *advertisement |= ETH_AUTONEG_100BASE_T4_HALF;
|
---|
1698 | if (anar & ANAR_PAUSE)
|
---|
1699 | *advertisement |= ETH_AUTONEG_PAUSE_SYMETRIC;
|
---|
1700 | }
|
---|
1701 |
|
---|
1702 | /** Check the autonegotion state
|
---|
1703 | *
|
---|
1704 | * @param[in] dev The device to check
|
---|
1705 | * @param[out] advertisement The device advertisement
|
---|
1706 | * @param[out] their_adv The partners advertisement
|
---|
1707 | * @param[out] result The autonegotion state
|
---|
1708 | * @param[out] their_result The link partner autonegotion status
|
---|
1709 | *
|
---|
1710 | * @returns EOK
|
---|
1711 | */
|
---|
1712 | static errno_t rtl8139_autoneg_probe(ddf_fun_t *fun, uint32_t *advertisement,
|
---|
1713 | uint32_t *their_adv, nic_result_t *result, nic_result_t *their_result)
|
---|
1714 | {
|
---|
1715 | assert(fun);
|
---|
1716 |
|
---|
1717 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1718 | assert(rtl8139);
|
---|
1719 |
|
---|
1720 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1721 | uint16_t anar = pio_read_16(rtl8139->io_port + ANAR);
|
---|
1722 | uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
|
---|
1723 | uint16_t aner = pio_read_16(rtl8139->io_port + ANER);
|
---|
1724 |
|
---|
1725 | if (bmcr & BMCR_AN_ENABLE) {
|
---|
1726 | *result = NIC_RESULT_ENABLED;
|
---|
1727 | } else {
|
---|
1728 | *result = NIC_RESULT_DISABLED;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | if (aner & ANER_LP_NW_ABLE) {
|
---|
1732 | *their_result = NIC_RESULT_ENABLED;
|
---|
1733 | } else {
|
---|
1734 | *their_result = NIC_RESULT_DISABLED;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | rtl8139_get_anar_state(anar, advertisement);
|
---|
1738 | rtl8139_get_anar_state(anar_lp, their_adv);
|
---|
1739 |
|
---|
1740 | return EOK;
|
---|
1741 | }
|
---|
1742 |
|
---|
1743 | /** Restart autonegotiation process
|
---|
1744 | *
|
---|
1745 | * @param dev The device to update
|
---|
1746 | *
|
---|
1747 | * @returns EOK
|
---|
1748 | */
|
---|
1749 | static errno_t rtl8139_autoneg_restart(ddf_fun_t *fun)
|
---|
1750 | {
|
---|
1751 | assert(fun);
|
---|
1752 |
|
---|
1753 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1754 | assert(rtl8139);
|
---|
1755 |
|
---|
1756 | uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
|
---|
1757 | bmcr |= BMCR_AN_RESTART;
|
---|
1758 | bmcr |= BMCR_AN_ENABLE;
|
---|
1759 |
|
---|
1760 | rtl8139_regs_unlock(rtl8139);
|
---|
1761 | pio_write_16(rtl8139->io_port + BMCR, bmcr);
|
---|
1762 | rtl8139_regs_lock(rtl8139);
|
---|
1763 |
|
---|
1764 | return EOK;
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | /** Notify NIC framework about HW filtering state when promisc mode was disabled
|
---|
1768 | *
|
---|
1769 | * @param nic_data The NIC data
|
---|
1770 | * @param mcast_mode Current multicast mode
|
---|
1771 | * @param was_promisc Sign if the promiscuous mode was active before disabling
|
---|
1772 | */
|
---|
1773 | inline static void rtl8139_rcx_promics_rem(nic_t *nic_data,
|
---|
1774 | nic_multicast_mode_t mcast_mode, uint8_t was_promisc)
|
---|
1775 | {
|
---|
1776 | assert(nic_data);
|
---|
1777 |
|
---|
1778 | if (was_promisc != 0) {
|
---|
1779 | if (mcast_mode == NIC_MULTICAST_LIST)
|
---|
1780 | nic_report_hw_filtering(nic_data, 1, 0, -1);
|
---|
1781 | else
|
---|
1782 | nic_report_hw_filtering(nic_data, 1, 1, -1);
|
---|
1783 | } else {
|
---|
1784 | nic_report_hw_filtering(nic_data, 1, -1, -1);
|
---|
1785 | }
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | /** Set unicast frames acceptance mode
|
---|
1789 | *
|
---|
1790 | * @param nic_data The nic device to update
|
---|
1791 | * @param mode The mode to set
|
---|
1792 | * @param addr Ignored, no HW support, just use unicast promisc
|
---|
1793 | * @param addr_cnt Ignored, no HW support
|
---|
1794 | *
|
---|
1795 | * @returns EOK
|
---|
1796 | */
|
---|
1797 | static errno_t rtl8139_unicast_set(nic_t *nic_data, nic_unicast_mode_t mode,
|
---|
1798 | const nic_address_t *addr, size_t addr_cnt)
|
---|
1799 | {
|
---|
1800 | assert(nic_data);
|
---|
1801 |
|
---|
1802 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1803 | assert(rtl8139);
|
---|
1804 |
|
---|
1805 | uint8_t was_promisc = rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS;
|
---|
1806 |
|
---|
1807 | nic_multicast_mode_t mcast_mode;
|
---|
1808 | nic_query_multicast(nic_data, &mcast_mode, 0, NULL, NULL);
|
---|
1809 |
|
---|
1810 | switch (mode) {
|
---|
1811 | case NIC_UNICAST_BLOCKED:
|
---|
1812 | rtl8139->rcr_data.ucast_mask = 0;
|
---|
1813 | rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
|
---|
1814 | break;
|
---|
1815 | case NIC_UNICAST_DEFAULT:
|
---|
1816 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH;
|
---|
1817 | rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
|
---|
1818 | break;
|
---|
1819 | case NIC_UNICAST_LIST:
|
---|
1820 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH |
|
---|
1821 | RCR_ACCEPT_ALL_PHYS;
|
---|
1822 |
|
---|
1823 | if (mcast_mode == NIC_MULTICAST_PROMISC)
|
---|
1824 | nic_report_hw_filtering(nic_data, 0, 1, -1);
|
---|
1825 | else
|
---|
1826 | nic_report_hw_filtering(nic_data, 0, 0, -1);
|
---|
1827 | break;
|
---|
1828 | case NIC_UNICAST_PROMISC:
|
---|
1829 | rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH |
|
---|
1830 | RCR_ACCEPT_ALL_PHYS;
|
---|
1831 |
|
---|
1832 | if (mcast_mode == NIC_MULTICAST_PROMISC)
|
---|
1833 | nic_report_hw_filtering(nic_data, 1, 1, -1);
|
---|
1834 | else
|
---|
1835 | nic_report_hw_filtering(nic_data, 1, 0, -1);
|
---|
1836 | break;
|
---|
1837 | default:
|
---|
1838 | return ENOTSUP;
|
---|
1839 | }
|
---|
1840 | fibril_mutex_lock(&rtl8139->rx_lock);
|
---|
1841 | rtl8139_hw_update_rcr(rtl8139);
|
---|
1842 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
---|
1843 | return EOK;
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | /** Set multicast frames acceptance mode
|
---|
1847 | *
|
---|
1848 | * @param nic_data The nic device to update
|
---|
1849 | * @param mode The mode to set
|
---|
1850 | * @param addr Addresses to accept
|
---|
1851 | * @param addr_cnt Addresses count
|
---|
1852 | *
|
---|
1853 | * @returns EOK
|
---|
1854 | */
|
---|
1855 | static errno_t rtl8139_multicast_set(nic_t *nic_data, nic_multicast_mode_t mode,
|
---|
1856 | const nic_address_t *addr, size_t addr_count)
|
---|
1857 | {
|
---|
1858 | assert(nic_data);
|
---|
1859 | assert(addr_count == 0 || addr);
|
---|
1860 |
|
---|
1861 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1862 | assert(rtl8139);
|
---|
1863 |
|
---|
1864 | switch (mode) {
|
---|
1865 | case NIC_MULTICAST_BLOCKED:
|
---|
1866 | rtl8139->rcr_data.mcast_mask = 0;
|
---|
1867 | if ((rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS) != 0)
|
---|
1868 | nic_report_hw_filtering(nic_data, -1, 0, -1);
|
---|
1869 | else
|
---|
1870 | nic_report_hw_filtering(nic_data, -1, 1, -1);
|
---|
1871 | break;
|
---|
1872 | case NIC_MULTICAST_LIST:
|
---|
1873 | rtl8139_hw_set_mcast_mask(rtl8139, nic_mcast_hash(addr, addr_count));
|
---|
1874 | rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
|
---|
1875 | nic_report_hw_filtering(nic_data, -1, 0, -1);
|
---|
1876 | break;
|
---|
1877 | case NIC_MULTICAST_PROMISC:
|
---|
1878 | rtl8139_hw_set_mcast_mask(rtl8139, RTL8139_MCAST_MASK_PROMISC);
|
---|
1879 | rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
|
---|
1880 | nic_report_hw_filtering(nic_data, -1, 1, -1);
|
---|
1881 | break;
|
---|
1882 | default:
|
---|
1883 | return ENOTSUP;
|
---|
1884 | }
|
---|
1885 | fibril_mutex_lock(&rtl8139->rx_lock);
|
---|
1886 | rtl8139_hw_update_rcr(rtl8139);
|
---|
1887 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
---|
1888 | return EOK;
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | /** Set broadcast frames acceptance mode
|
---|
1892 | *
|
---|
1893 | * @param nic_data The nic device to update
|
---|
1894 | * @param mode The mode to set
|
---|
1895 | *
|
---|
1896 | * @returns EOK
|
---|
1897 | */
|
---|
1898 | static errno_t rtl8139_broadcast_set(nic_t *nic_data, nic_broadcast_mode_t mode)
|
---|
1899 | {
|
---|
1900 | assert(nic_data);
|
---|
1901 |
|
---|
1902 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1903 | assert(rtl8139);
|
---|
1904 |
|
---|
1905 | switch (mode) {
|
---|
1906 | case NIC_BROADCAST_BLOCKED:
|
---|
1907 | rtl8139->rcr_data.bcast_mask = 0;
|
---|
1908 | break;
|
---|
1909 | case NIC_BROADCAST_ACCEPTED:
|
---|
1910 | rtl8139->rcr_data.bcast_mask = RCR_ACCEPT_BROADCAST;
|
---|
1911 | break;
|
---|
1912 | default:
|
---|
1913 | return ENOTSUP;
|
---|
1914 | }
|
---|
1915 | fibril_mutex_lock(&rtl8139->rx_lock);
|
---|
1916 | rtl8139_hw_update_rcr(rtl8139);
|
---|
1917 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
---|
1918 | return EOK;
|
---|
1919 | }
|
---|
1920 |
|
---|
1921 | /** Get state of acceptance of weird frames
|
---|
1922 | *
|
---|
1923 | * @param[in] device The device to check
|
---|
1924 | * @param[out] mode The current mode
|
---|
1925 | */
|
---|
1926 | static errno_t rtl8139_defective_get_mode(ddf_fun_t *fun, uint32_t *mode)
|
---|
1927 | {
|
---|
1928 | assert(fun);
|
---|
1929 | assert(mode);
|
---|
1930 |
|
---|
1931 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1932 | assert(rtl8139);
|
---|
1933 |
|
---|
1934 | *mode = 0;
|
---|
1935 | if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_ERROR)
|
---|
1936 | *mode |= NIC_DEFECTIVE_BAD_CRC;
|
---|
1937 | if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_RUNT)
|
---|
1938 | *mode |= NIC_DEFECTIVE_SHORT;
|
---|
1939 |
|
---|
1940 | return EOK;
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | /** Set acceptance of weird frames
|
---|
1944 | *
|
---|
1945 | * @param device The device to update
|
---|
1946 | * @param mode The mode to set
|
---|
1947 | *
|
---|
1948 | * @returns ENOTSUP if the mode is not supported
|
---|
1949 | * @returns EOK of mode was set
|
---|
1950 | */
|
---|
1951 | static errno_t rtl8139_defective_set_mode(ddf_fun_t *fun, uint32_t mode)
|
---|
1952 | {
|
---|
1953 | assert(fun);
|
---|
1954 |
|
---|
1955 | rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
|
---|
1956 | assert(rtl8139);
|
---|
1957 |
|
---|
1958 | if ((mode & (NIC_DEFECTIVE_SHORT | NIC_DEFECTIVE_BAD_CRC)) != mode)
|
---|
1959 | return ENOTSUP;
|
---|
1960 |
|
---|
1961 | rtl8139->rcr_data.defect_mask = 0;
|
---|
1962 | if (mode & NIC_DEFECTIVE_SHORT)
|
---|
1963 | rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_RUNT;
|
---|
1964 | if (mode & NIC_DEFECTIVE_BAD_CRC)
|
---|
1965 | rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_ERROR;
|
---|
1966 |
|
---|
1967 | fibril_mutex_lock(&rtl8139->rx_lock);
|
---|
1968 | rtl8139_hw_update_rcr(rtl8139);
|
---|
1969 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
---|
1970 | return EOK;
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | /** Turn Wakeup On Lan method on
|
---|
1974 | *
|
---|
1975 | * @param nic_data The NIC to update
|
---|
1976 | * @param virtue The method to turn on
|
---|
1977 | *
|
---|
1978 | * @returns EINVAL if the method is not supported
|
---|
1979 | * @returns EOK if succeed
|
---|
1980 | * @returns ELIMIT if no more methods of this kind can be enabled
|
---|
1981 | */
|
---|
1982 | static errno_t rtl8139_wol_virtue_add(nic_t *nic_data,
|
---|
1983 | const nic_wol_virtue_t *virtue)
|
---|
1984 | {
|
---|
1985 | assert(nic_data);
|
---|
1986 | assert(virtue);
|
---|
1987 |
|
---|
1988 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
1989 | assert(rtl8139);
|
---|
1990 |
|
---|
1991 | switch (virtue->type) {
|
---|
1992 | case NIC_WV_BROADCAST:
|
---|
1993 | rtl8139_hw_reg_add_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
|
---|
1994 | break;
|
---|
1995 | case NIC_WV_LINK_CHANGE:
|
---|
1996 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
1997 | rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
|
---|
1998 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
1999 | break;
|
---|
2000 | case NIC_WV_MAGIC_PACKET:
|
---|
2001 | if (virtue->data)
|
---|
2002 | return EINVAL;
|
---|
2003 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
2004 | rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
|
---|
2005 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
2006 | break;
|
---|
2007 | default:
|
---|
2008 | return EINVAL;
|
---|
2009 | }
|
---|
2010 | if (rtl8139->pm.active++ == 0)
|
---|
2011 | rtl8139_hw_pmen_set(rtl8139, 1);
|
---|
2012 | return EOK;
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | /** Turn Wakeup On Lan method off
|
---|
2016 | *
|
---|
2017 | * @param nic_data The NIC to update
|
---|
2018 | * @param virtue The method to turn off
|
---|
2019 | */
|
---|
2020 | static void rtl8139_wol_virtue_rem(nic_t *nic_data,
|
---|
2021 | const nic_wol_virtue_t *virtue)
|
---|
2022 | {
|
---|
2023 | assert(nic_data);
|
---|
2024 | assert(virtue);
|
---|
2025 |
|
---|
2026 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
2027 | assert(rtl8139);
|
---|
2028 |
|
---|
2029 | switch (virtue->type) {
|
---|
2030 | case NIC_WV_BROADCAST:
|
---|
2031 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
|
---|
2032 | break;
|
---|
2033 | case NIC_WV_LINK_CHANGE:
|
---|
2034 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
2035 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
|
---|
2036 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
2037 | break;
|
---|
2038 | case NIC_WV_MAGIC_PACKET:
|
---|
2039 | rtl8139_regs_unlock(rtl8139->io_port);
|
---|
2040 | rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
|
---|
2041 | rtl8139_regs_lock(rtl8139->io_port);
|
---|
2042 | break;
|
---|
2043 | default:
|
---|
2044 | return;
|
---|
2045 | }
|
---|
2046 | rtl8139->pm.active--;
|
---|
2047 | if (rtl8139->pm.active == 0)
|
---|
2048 | rtl8139_hw_pmen_set(rtl8139, 0);
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | /** Set polling mode
|
---|
2052 | *
|
---|
2053 | * @param device The device to set
|
---|
2054 | * @param mode The mode to set
|
---|
2055 | * @param period The period for NIC_POLL_PERIODIC
|
---|
2056 | *
|
---|
2057 | * @returns EOK if succeed
|
---|
2058 | * @returns ENOTSUP if the mode is not supported
|
---|
2059 | */
|
---|
2060 | static errno_t rtl8139_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
|
---|
2061 | const struct timespec *period)
|
---|
2062 | {
|
---|
2063 | assert(nic_data);
|
---|
2064 | errno_t rc = EOK;
|
---|
2065 |
|
---|
2066 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
2067 | assert(rtl8139);
|
---|
2068 |
|
---|
2069 | fibril_mutex_lock(&rtl8139->rx_lock);
|
---|
2070 |
|
---|
2071 | switch (mode) {
|
---|
2072 | case NIC_POLL_IMMEDIATE:
|
---|
2073 | rtl8139->int_mask = RTL_DEFAULT_INTERRUPTS;
|
---|
2074 | break;
|
---|
2075 | case NIC_POLL_ON_DEMAND:
|
---|
2076 | rtl8139->int_mask = 0;
|
---|
2077 | break;
|
---|
2078 | case NIC_POLL_PERIODIC:
|
---|
2079 | assert(period);
|
---|
2080 |
|
---|
2081 | rtl8139_timer_act_t new_timer;
|
---|
2082 | rc = rtl8139_timer_act_init(&new_timer, RTL8139_PCI_FREQ_KHZ, period);
|
---|
2083 | if (rc != EOK)
|
---|
2084 | break;
|
---|
2085 |
|
---|
2086 | /* Disable timer interrupts while working with timer-related data */
|
---|
2087 | rtl8139->int_mask = 0;
|
---|
2088 | rtl8139_hw_int_set(rtl8139);
|
---|
2089 |
|
---|
2090 | rtl8139->poll_timer = new_timer;
|
---|
2091 | rtl8139->int_mask = INT_TIME_OUT;
|
---|
2092 |
|
---|
2093 | /*
|
---|
2094 | * Force timer interrupt start be writing nonzero value to timer
|
---|
2095 | * interrutp register (should be small to prevent big delay)
|
---|
2096 | * Read TCTR to reset timer counter
|
---|
2097 | * Change values to simulate the last interrupt from the period.
|
---|
2098 | */
|
---|
2099 | pio_write_32(rtl8139->io_port + TIMERINT, 10);
|
---|
2100 | pio_write_32(rtl8139->io_port + TCTR, 0);
|
---|
2101 |
|
---|
2102 | ddf_msg(LVL_DEBUG, "Periodic mode. Interrupt mask %" PRIx16 ", "
|
---|
2103 | "poll.full_skips %zu, last timer %" PRIu32,
|
---|
2104 | rtl8139->int_mask, rtl8139->poll_timer.full_skips,
|
---|
2105 | rtl8139->poll_timer.last_val);
|
---|
2106 | break;
|
---|
2107 | default:
|
---|
2108 | rc = ENOTSUP;
|
---|
2109 | break;
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 | rtl8139_hw_int_set(rtl8139);
|
---|
2113 |
|
---|
2114 | fibril_mutex_unlock(&rtl8139->rx_lock);
|
---|
2115 |
|
---|
2116 | return rc;
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | /** Force receiving all frames in the receive buffer
|
---|
2120 | *
|
---|
2121 | * @param device The device to receive
|
---|
2122 | */
|
---|
2123 | static void rtl8139_poll(nic_t *nic_data)
|
---|
2124 | {
|
---|
2125 | assert(nic_data);
|
---|
2126 |
|
---|
2127 | rtl8139_t *rtl8139 = nic_get_specific(nic_data);
|
---|
2128 | assert(rtl8139);
|
---|
2129 |
|
---|
2130 | uint16_t isr = pio_read_16(rtl8139->io_port + ISR);
|
---|
2131 | pio_write_16(rtl8139->io_port + ISR, 0);
|
---|
2132 |
|
---|
2133 | rtl8139_interrupt_impl(nic_data, isr);
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | /** Main function of RTL8139 driver
|
---|
2137 | *
|
---|
2138 | * Just initialize the driver structures and
|
---|
2139 | * put it into the device drivers interface
|
---|
2140 | */
|
---|
2141 | int main(void)
|
---|
2142 | {
|
---|
2143 | printf("%s: HelenOS RTL8139 network adapter driver\n", NAME);
|
---|
2144 |
|
---|
2145 | errno_t rc = nic_driver_init(NAME);
|
---|
2146 | if (rc != EOK)
|
---|
2147 | return rc;
|
---|
2148 |
|
---|
2149 | nic_driver_implement(&rtl8139_driver_ops, &rtl8139_dev_ops,
|
---|
2150 | &rtl8139_nic_iface);
|
---|
2151 |
|
---|
2152 | ddf_log_init(NAME);
|
---|
2153 | return ddf_driver_main(&rtl8139_driver);
|
---|
2154 | }
|
---|