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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5a6cc679 was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

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