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

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

rtl8139: Fix unmapping of DMA memory

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