source: mainline/uspace/drv/nic/rtl8139/driver.c@ c8ea6eca

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c8ea6eca was bd41ac52, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

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