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