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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a9654bf was 70253688, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Merge mainline changes

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