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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e30369 was 5cd3d67, checked in by Martin Decky <martin@…>, 14 years ago

cstyle
(no change in functionality)

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