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

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

Merge mainline changes.

  • Property mode set to 100644
File size: 58.3 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
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 (dev->driver_data)
1057 nic_unbind_and_destroy(dev);
1058
1059 if (dev->parent_sess != NULL) {
1060 async_hangup(dev->parent_sess);
1061 dev->parent_sess = NULL;
1062 }
1063}
1064
1065/** Fill the irq and io_addr part of device data structure
1066 *
1067 * The hw_resources must be obtained before calling this function
1068 *
1069 * @param dev The device structure
1070 * @param hw_resources Devices hardware resources
1071 *
1072 * @return EOK if succeed, negative error code otherwise
1073 */
1074static int rtl8139_fill_resource_info(ddf_dev_t *dev, const hw_res_list_parsed_t
1075 *hw_resources)
1076{
1077 assert(dev);
1078 assert(hw_resources);
1079
1080 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_dev(dev));
1081 assert(rtl8139);
1082
1083 if (hw_resources->irqs.count != 1) {
1084 ddf_msg(LVL_ERROR, "%s device: unexpected irq count", dev->name);
1085 return EINVAL;
1086 };
1087 if (hw_resources->io_ranges.count != 1) {
1088 ddf_msg(LVL_ERROR, "%s device: unexpected io ranges count", dev->name);
1089 return EINVAL;
1090 }
1091
1092 rtl8139->irq = hw_resources->irqs.irqs[0];
1093 ddf_msg(LVL_DEBUG, "%s device: irq 0x%x assigned", dev->name, rtl8139->irq);
1094
1095 rtl8139->io_addr = IOADDR_TO_PTR(hw_resources->io_ranges.ranges[0].address);
1096 if (hw_resources->io_ranges.ranges[0].size < RTL8139_IO_SIZE) {
1097 ddf_msg(LVL_ERROR, "i/o range assigned to the device "
1098 "%s is too small.", dev->name);
1099 return EINVAL;
1100 }
1101 ddf_msg(LVL_DEBUG, "%s device: i/o addr %p assigned.", dev->name, rtl8139->io_addr);
1102
1103 return EOK;
1104}
1105
1106/** Obtain information about hardware resources of the device
1107 *
1108 * The device must be connected to the parent
1109 *
1110 * @param dev The device structure
1111 *
1112 * @return EOK if succeed, negative error code otherwise
1113 */
1114static int rtl8139_get_resource_info(ddf_dev_t *dev)
1115{
1116 assert(dev);
1117
1118 nic_t *nic_data = nic_get_from_ddf_dev(dev);
1119 assert(nic_data);
1120
1121 hw_res_list_parsed_t hw_res_parsed;
1122 hw_res_list_parsed_init(&hw_res_parsed);
1123
1124 /* Get hw resources form parent driver */
1125 int rc = nic_get_resources(nic_data, &hw_res_parsed);
1126 if (rc != EOK)
1127 return rc;
1128
1129 /* Fill resources information to the device */
1130 int ret = rtl8139_fill_resource_info(dev, &hw_res_parsed);
1131 hw_res_list_parsed_clean(&hw_res_parsed);
1132
1133 return ret;
1134}
1135
1136
1137/** Allocate buffers using DMA framework
1138 *
1139 * The buffers structures in the device specific data is filled
1140 *
1141 * @param data The device specific structure to fill
1142 *
1143 * @return EOK in the case of success, error code otherwise
1144 */
1145static int rtl8139_buffers_create(rtl8139_t *rtl8139)
1146{
1147 size_t i = 0;
1148 int rc;
1149
1150 ddf_msg(LVL_DEBUG, "Creating buffers");
1151
1152 rc = dmamem_map_anonymous(TX_PAGES * PAGE_SIZE, AS_AREA_WRITE, 0,
1153 &rtl8139->tx_buff_phys, &rtl8139->tx_buff_virt);
1154 if (rc != EOK) {
1155 ddf_msg(LVL_ERROR, "Can not allocate transmitter buffers.");
1156 goto err_tx_alloc;
1157 }
1158
1159 for (i = 0; i < TX_BUFF_COUNT; ++i)
1160 rtl8139->tx_buff[i] = rtl8139->tx_buff_virt + i * TX_BUFF_SIZE;
1161
1162 ddf_msg(LVL_DEBUG, "The transmittion buffers allocated");
1163
1164 /* Use the first buffer for next transmittion */
1165 rtl8139->tx_next = 0;
1166 rtl8139->tx_used = 0;
1167
1168 /* Allocate buffer for receiver */
1169 ddf_msg(LVL_DEBUG, "Allocating receiver buffer of the size %zu bytes",
1170 RxBUF_TOT_LENGTH);
1171
1172 rc = dmamem_map_anonymous(RxBUF_TOT_LENGTH, AS_AREA_READ, 0,
1173 &rtl8139->rx_buff_phys, &rtl8139->rx_buff_virt);
1174 if (rc != EOK) {
1175 ddf_msg(LVL_ERROR, "Can not allocate receive buffer.");
1176 goto err_rx_alloc;
1177 }
1178 ddf_msg(LVL_DEBUG, "The buffers created");
1179
1180 return EOK;
1181
1182err_rx_alloc:
1183 dmamem_unmap_anonymous(&rtl8139->tx_buff_virt);
1184err_tx_alloc:
1185 return rc;
1186}
1187
1188/** Initialize the rtl8139 device structure
1189 *
1190 * @param dev The device information
1191 *
1192 * @return EOK if succeed, negative error code otherwise
1193 */
1194static int rtl8139_device_initialize(ddf_dev_t *dev)
1195{
1196 ddf_msg(LVL_DEBUG, "rtl8139_dev_initialize %s", dev->name);
1197
1198 int ret = EOK;
1199
1200 ddf_msg(LVL_DEBUG, "rtl8139: creating device data");
1201
1202 /* Allocate driver data for the device. */
1203 rtl8139_t *rtl8139 = rtl8139_create_dev_data(dev);
1204 if (rtl8139 == NULL) {
1205 ddf_msg(LVL_ERROR, "Not enough memory for initializing %s.", dev->name);
1206 return ENOMEM;
1207 }
1208
1209 ddf_msg(LVL_DEBUG, "rtl8139: dev_data created");
1210
1211 /* Obtain and fill hardware resources info and connect to parent */
1212 ret = rtl8139_get_resource_info(dev);
1213 if (ret != EOK) {
1214 ddf_msg(LVL_ERROR, "Can not obatin hw resources information");
1215 goto failed;
1216 }
1217
1218 ddf_msg(LVL_DEBUG, "rtl8139: resource_info obtained");
1219
1220 /* Allocate DMA buffers */
1221 ret = rtl8139_buffers_create(rtl8139);
1222 if (ret != EOK)
1223 goto failed;
1224
1225 /* Set default frame acceptance */
1226 rtl8139->rcr_data.ucast_mask = RTL8139_RCR_UCAST_DEFAULT;
1227 rtl8139->rcr_data.mcast_mask = RTL8139_RCR_MCAST_DEFAULT;
1228 rtl8139->rcr_data.bcast_mask = RTL8139_RCR_BCAST_DEFAULT;
1229 rtl8139->rcr_data.defect_mask = RTL8139_RCR_DEFECT_DEFAULT;
1230 /* Set receiver early treshold to 8/16 of frame length */
1231 rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
1232
1233 ddf_msg(LVL_DEBUG, "The device is initialized");
1234 return ret;
1235
1236failed:
1237 ddf_msg(LVL_ERROR, "The device initialization failed");
1238 rtl8139_dev_cleanup(dev);
1239 return ret;
1240}
1241
1242/** Enable the i/o ports of the device.
1243 *
1244 * @param dev The RTL8139 device.
1245 *
1246 * @return EOK if successed, negative error code otherwise
1247 */
1248static int rtl8139_pio_enable(ddf_dev_t *dev)
1249{
1250 ddf_msg(LVL_DEBUG, NAME ": rtl8139_pio_enable %s", dev->name);
1251
1252 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_dev(dev));
1253
1254 /* Gain control over port's registers. */
1255 if (pio_enable(rtl8139->io_addr, RTL8139_IO_SIZE, &rtl8139->io_port)) {
1256 ddf_msg(LVL_ERROR, "Cannot gain the port %lx for device %s.", rtl8139->io_addr,
1257 dev->name);
1258 return EADDRNOTAVAIL;
1259 }
1260
1261 return EOK;
1262}
1263
1264/** Initialize the driver private data according to the
1265 * device registers
1266 *
1267 * @param rtl8139 rtl8139 private data
1268 */
1269static void rtl8139_data_init(rtl8139_t *rtl8139)
1270{
1271 assert(rtl8139);
1272
1273 /* Check the version id */
1274 uint32_t tcr = pio_read_32(rtl8139->io_port + TCR);
1275 uint32_t hwverid = RTL8139_HWVERID(tcr);
1276 size_t i = 0;
1277 rtl8139->hw_version = RTL8139_VER_COUNT;
1278 for (i = 0; i < RTL8139_VER_COUNT; ++i) {
1279 if (rtl8139_versions[i].hwverid == 0)
1280 break;
1281
1282 if (rtl8139_versions[i].hwverid == hwverid) {
1283 rtl8139->hw_version = rtl8139_versions[i].ver_id;
1284 ddf_msg(LVL_NOTE, "HW version found: index %zu, ver_id %d (%s)", i,
1285 rtl8139_versions[i].ver_id, model_names[rtl8139->hw_version]);
1286 }
1287 }
1288}
1289
1290/** The add_device callback of RTL8139 callback
1291 *
1292 * Probe and initialize the newly added device.
1293 *
1294 * @param dev The RTL8139 device.
1295 *
1296 * @return EOK if added successfully, negative error code otherwise
1297 */
1298int rtl8139_dev_add(ddf_dev_t *dev)
1299{
1300 ddf_fun_t *fun;
1301
1302 assert(dev);
1303 ddf_msg(LVL_NOTE, "RTL8139_dev_add %s (handle = %d)", dev->name, dev->handle);
1304
1305 /* Init device structure for rtl8139 */
1306 int rc = rtl8139_device_initialize(dev);
1307 if (rc != EOK)
1308 return rc;
1309
1310 /* Map I/O ports */
1311 rc = rtl8139_pio_enable(dev);
1312 if (rc != EOK)
1313 goto err_destroy;
1314
1315 nic_t *nic_data = nic_get_from_ddf_dev(dev);
1316 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1317
1318 nic_address_t addr;
1319 rtl8139_hw_get_addr(rtl8139, &addr);
1320 rc = nic_report_address(nic_data, &addr);
1321 if (rc != EOK)
1322 goto err_pio;
1323
1324 /* Initialize the driver private structure */
1325 rtl8139_data_init(rtl8139);
1326
1327 /* Register interrupt handler */
1328 rc = rtl8139_register_int_handler(nic_data);
1329 if (rc != EOK)
1330 goto err_pio;
1331
1332 rc = nic_connect_to_services(nic_data);
1333 if (rc != EOK) {
1334 ddf_msg(LVL_ERROR, "Failed to connect to services", rc);
1335 goto err_irq;
1336 }
1337
1338 fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
1339 if (fun == NULL) {
1340 ddf_msg(LVL_ERROR, "Failed creating device function");
1341 goto err_srv;
1342 }
1343 nic_set_ddf_fun(nic_data, fun);
1344 fun->ops = &rtl8139_dev_ops;
1345 fun->driver_data = nic_data;
1346
1347 rc = ddf_fun_bind(fun);
1348 if (rc != EOK) {
1349 ddf_msg(LVL_ERROR, "Failed binding device function");
1350 goto err_fun_create;
1351 }
1352 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
1353 if (rc != EOK) {
1354 ddf_msg(LVL_ERROR, "Failed adding function to category");
1355 goto err_fun_bind;
1356 }
1357
1358 ddf_msg(LVL_NOTE, "The %s device has been successfully initialized.",
1359 dev->name);
1360
1361 return EOK;
1362
1363err_fun_bind:
1364 ddf_fun_unbind(fun);
1365err_fun_create:
1366 ddf_fun_destroy(fun);
1367err_srv:
1368 /* XXX Disconnect from services */
1369err_irq:
1370 unregister_interrupt_handler(dev, rtl8139->irq);
1371err_pio:
1372 // rtl8139_pio_disable(dev);
1373 /* TODO: find out if the pio_disable is needed */
1374err_destroy:
1375 rtl8139_dev_cleanup(dev);
1376 return rc;
1377};
1378
1379/** Set card MAC address
1380 *
1381 * @param device The RTL8139 device
1382 * @param address The place to store the address
1383 * @param max_len Maximal addresss length to store
1384 *
1385 * @return EOK if succeed, negative error code otherwise
1386 */
1387static int rtl8139_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
1388{
1389 assert(fun);
1390 assert(addr);
1391
1392 nic_t *nic_data =nic_get_from_ddf_fun((fun));
1393 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1394 assert(rtl8139);
1395
1396 rtl8139_lock_all(rtl8139);
1397
1398 int rc = nic_report_address(nic_data, addr);
1399 if ( rc != EOK) {
1400 rtl8139_unlock_all(rtl8139);
1401 return rc;
1402 }
1403
1404 rtl8139_hw_set_addr(rtl8139, addr);
1405
1406 rtl8139_unlock_all(rtl8139);
1407 return EOK;
1408}
1409
1410/** Get the device information
1411 *
1412 * @param dev The NIC device
1413 * @param info The information to fill
1414 *
1415 * @return EOK
1416 */
1417static int rtl8139_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
1418{
1419 assert(fun);
1420 assert(info);
1421
1422 nic_t *nic_data = nic_get_from_ddf_fun(fun);
1423 assert(nic_data);
1424 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1425 assert(rtl8139);
1426
1427 /* TODO: fill the information more completely */
1428 info->vendor_id = 0x10ec;
1429 str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH, "Realtek");
1430
1431 if (rtl8139->hw_version < RTL8139_VER_COUNT) {
1432 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
1433 model_names[rtl8139->hw_version]);
1434 } else {
1435 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, "RTL8139");
1436 }
1437
1438 info->ethernet_support[ETH_10M] = ETH_10BASE_T;
1439 info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
1440
1441 info->autoneg_support = RTL8139_AUTONEG_CAPS;
1442 return EOK;
1443}
1444
1445/** Check the cable state
1446 *
1447 * @param[in] dev The device
1448 * @param[out] state The state to fill
1449 *
1450 * @return EOK
1451 */
1452static int rtl8139_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
1453{
1454 assert(fun);
1455 assert(state);
1456
1457 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1458 assert(rtl8139);
1459
1460 if (pio_read_16(rtl8139->io_port + CSCR) & CS_CON_STATUS) {
1461 *state = NIC_CS_PLUGGED;
1462 } else {
1463 *state = NIC_CS_UNPLUGGED;
1464 }
1465
1466 return EOK;
1467}
1468
1469/** Get operation mode of the device
1470 */
1471static int rtl8139_get_operation_mode(ddf_fun_t *fun, int *speed,
1472 nic_channel_mode_t *duplex, nic_role_t *role)
1473{
1474 assert(fun);
1475 assert(speed);
1476 assert(duplex);
1477 assert(role);
1478
1479 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1480 assert(rtl8139);
1481
1482 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1483 uint8_t msr_val = pio_read_8(rtl8139->io_port + MSR);
1484
1485 if (bmcr_val & BMCR_DUPLEX) {
1486 *duplex = NIC_CM_FULL_DUPLEX;
1487 } else {
1488 *duplex = NIC_CM_HALF_DUPLEX;
1489 }
1490
1491 if (msr_val & MSR_SPEED10) {
1492 *speed = 10;
1493 } else {
1494 *speed = 100;
1495 }
1496
1497 *role = NIC_ROLE_UNKNOWN;
1498 return EOK;
1499}
1500
1501/** Value validity */
1502enum access_mode {
1503 /** Value is invalid now */
1504 VALUE_INVALID = 0,
1505 /** Read-only */
1506 VALUE_RO,
1507 /** Read-write */
1508 VALUE_RW
1509};
1510
1511/** Check if pause frame operations are valid in current situation
1512 *
1513 * @param rtl8139 RTL8139 private structure
1514 *
1515 * @return VALUE_INVALID if the value has no sense in current moment
1516 * @return VALUE_RO if the state is read-only in current moment
1517 * @return VALUE_RW if the state can be modified
1518 */
1519static int rtl8139_pause_is_valid(rtl8139_t *rtl8139)
1520{
1521 assert(rtl8139);
1522
1523 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1524 if ((bmcr & (BMCR_AN_ENABLE | BMCR_DUPLEX)) == 0)
1525 return VALUE_INVALID;
1526
1527 if (bmcr & BMCR_AN_ENABLE) {
1528 uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
1529 if (anar_lp & ANAR_PAUSE)
1530 return VALUE_RO;
1531 }
1532
1533 return VALUE_RW;
1534}
1535
1536/** Get current pause frame configuration
1537 *
1538 * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
1539 * the moment (half-duplex).
1540 *
1541 * @param[in] fun The DDF structure of the RTL8139
1542 * @param[out] we_send Sign if local constroller sends pause frame
1543 * @param[out] we_receive Sign if local constroller receives pause frame
1544 * @param[out] time Time filled in pause frames. 0xFFFF in rtl8139
1545 *
1546 * @return EOK if succeed
1547 */
1548static int rtl8139_pause_get(ddf_fun_t *fun, nic_result_t *we_send,
1549 nic_result_t *we_receive, uint16_t *time)
1550{
1551 assert(fun);
1552 assert(we_send);
1553 assert(we_receive);
1554
1555 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1556 assert(rtl8139);
1557
1558 if (rtl8139_pause_is_valid(rtl8139) == VALUE_INVALID) {
1559 *we_send = NIC_RESULT_NOT_AVAILABLE;
1560 *we_receive = NIC_RESULT_NOT_AVAILABLE;
1561 *time = 0;
1562 return EOK;
1563 }
1564
1565 uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
1566
1567 *we_send = (msr & MSR_TXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
1568 *we_receive = (msr & MSR_RXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
1569 *time = RTL8139_PAUSE_VAL;
1570
1571 return EOK;
1572};
1573
1574/** Set current pause frame configuration
1575 *
1576 * @param fun The DDF structure of the RTL8139
1577 * @param allow_send Sign if local constroller sends pause frame
1578 * @param allow_receive Sign if local constroller receives pause frames
1579 * @param time Time to use, ignored (not supported by device)
1580 *
1581 * @return EOK if succeed, INVAL if the pause frame has no sence
1582 */
1583static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
1584 uint16_t time)
1585{
1586 assert(fun);
1587
1588 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1589 assert(rtl8139);
1590
1591 if (rtl8139_pause_is_valid(rtl8139) != VALUE_RW)
1592 return EINVAL;
1593
1594 uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
1595 msr &= ~(uint8_t)(MSR_TXFCE | MSR_RXFCE);
1596
1597 if (allow_receive)
1598 msr |= MSR_RXFCE;
1599 if (allow_send)
1600 msr |= MSR_TXFCE;
1601
1602 pio_write_8(rtl8139->io_port + MSR, msr);
1603
1604 if (allow_send && time > 0) {
1605 ddf_msg(LVL_WARN, "Time setting is not supported in set_pause method.");
1606 }
1607 return EOK;
1608};
1609
1610/** Set operation mode of the device
1611 *
1612 */
1613static int rtl8139_set_operation_mode(ddf_fun_t *fun, int speed,
1614 nic_channel_mode_t duplex, nic_role_t role)
1615{
1616 assert(fun);
1617
1618 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1619 assert(rtl8139);
1620
1621 if (speed != 10 && speed != 100)
1622 return EINVAL;
1623 if (duplex != NIC_CM_HALF_DUPLEX && duplex != NIC_CM_FULL_DUPLEX)
1624 return EINVAL;
1625
1626 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1627
1628 /* Set autonegotion disabled */
1629 bmcr_val &= ~(uint16_t)BMCR_AN_ENABLE;
1630
1631 if (duplex == NIC_CM_FULL_DUPLEX) {
1632 bmcr_val |= BMCR_DUPLEX;
1633 } else {
1634 bmcr_val &= ~((uint16_t)BMCR_DUPLEX);
1635 }
1636
1637 if (speed == 100) {
1638 bmcr_val |= BMCR_Spd_100;
1639 } else {
1640 bmcr_val &= ~((uint16_t)BMCR_Spd_100);
1641 }
1642
1643 rtl8139_regs_unlock(rtl8139->io_port);
1644 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1645 rtl8139_regs_lock(rtl8139->io_port);
1646 return EOK;
1647}
1648
1649/** Enable autonegoation with specific advertisement
1650 *
1651 * @param dev The device to update
1652 * @param advertisement The advertisement to set
1653 *
1654 * @returns EINVAL if the advertisement mode is not supproted
1655 * @returns EOK if advertisement mode set successfully
1656 */
1657static int rtl8139_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement)
1658{
1659 assert(fun);
1660
1661 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1662 assert(rtl8139);
1663
1664 if (advertisement == 0) {
1665 advertisement = RTL8139_AUTONEG_CAPS;
1666 }
1667
1668 if ((advertisement | RTL8139_AUTONEG_CAPS) != RTL8139_AUTONEG_CAPS)
1669 return EINVAL; /* some unsuported mode is requested */
1670
1671 assert(advertisement != 0);
1672
1673 /* Set the autonegotiation advertisement */
1674 uint16_t anar = ANAR_SELECTOR; /* default selector */
1675 if (advertisement & ETH_AUTONEG_10BASE_T_FULL)
1676 anar |= ANAR_10_FD;
1677 if (advertisement & ETH_AUTONEG_10BASE_T_HALF)
1678 anar |= ANAR_10_HD;
1679 if (advertisement & ETH_AUTONEG_100BASE_TX_FULL)
1680 anar |= ANAR_100TX_FD;
1681 if (advertisement & ETH_AUTONEG_100BASE_TX_HALF)
1682 anar |= ANAR_100TX_HD;
1683 if (advertisement & ETH_AUTONEG_PAUSE_SYMETRIC)
1684 anar |= ANAR_PAUSE;
1685
1686 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1687 bmcr_val |= BMCR_AN_ENABLE;
1688
1689 pio_write_16(rtl8139->io_port + ANAR, anar);
1690
1691 rtl8139_regs_unlock(rtl8139->io_port);
1692 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1693 rtl8139_regs_lock(rtl8139->io_port);
1694 return EOK;
1695}
1696
1697/** Disable advertisement functionality
1698 *
1699 * @param dev The device to update
1700 *
1701 * @returns EOK
1702 */
1703static int rtl8139_autoneg_disable(ddf_fun_t *fun)
1704{
1705 assert(fun);
1706
1707 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1708 assert(rtl8139);
1709
1710 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1711
1712 bmcr_val &= ~((uint16_t)BMCR_AN_ENABLE);
1713
1714 rtl8139_regs_unlock(rtl8139->io_port);
1715 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1716 rtl8139_regs_lock(rtl8139->io_port);
1717
1718 return EOK;
1719}
1720
1721/** Obtain the advertisement NIC framework value from the ANAR/ANLPAR register
1722 * value
1723 *
1724 * @param[in] anar The ANAR register value
1725 * @param[out] advertisement The advertisement result
1726 */
1727static void rtl8139_get_anar_state(uint16_t anar, uint32_t *advertisement)
1728{
1729 *advertisement = 0;
1730 if (anar & ANAR_10_HD)
1731 *advertisement |= ETH_AUTONEG_10BASE_T_HALF;
1732 if (anar & ANAR_10_FD)
1733 *advertisement |= ETH_AUTONEG_10BASE_T_FULL;
1734 if (anar & ANAR_100TX_HD)
1735 *advertisement |= ETH_AUTONEG_100BASE_TX_HALF;
1736 if (anar & ANAR_100TX_FD)
1737 *advertisement |= ETH_AUTONEG_100BASE_TX_FULL;
1738 if (anar & ANAR_100T4)
1739 *advertisement |= ETH_AUTONEG_100BASE_T4_HALF;
1740 if (anar & ANAR_PAUSE)
1741 *advertisement |= ETH_AUTONEG_PAUSE_SYMETRIC;
1742}
1743
1744/** Check the autonegotion state
1745 *
1746 * @param[in] dev The device to check
1747 * @param[out] advertisement The device advertisement
1748 * @param[out] their_adv The partners advertisement
1749 * @param[out] result The autonegotion state
1750 * @param[out] their_result The link partner autonegotion status
1751 *
1752 * @returns EOK
1753 */
1754static int rtl8139_autoneg_probe(ddf_fun_t *fun, uint32_t *advertisement,
1755 uint32_t *their_adv, nic_result_t *result, nic_result_t *their_result)
1756{
1757 assert(fun);
1758
1759 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1760 assert(rtl8139);
1761
1762 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1763 uint16_t anar = pio_read_16(rtl8139->io_port + ANAR);
1764 uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
1765 uint16_t aner = pio_read_16(rtl8139->io_port + ANER);
1766
1767 if (bmcr & BMCR_AN_ENABLE) {
1768 *result = NIC_RESULT_ENABLED;
1769 } else {
1770 *result = NIC_RESULT_DISABLED;
1771 }
1772
1773 if (aner & ANER_LP_NW_ABLE) {
1774 *their_result = NIC_RESULT_ENABLED;
1775 } else {
1776 *their_result = NIC_RESULT_DISABLED;
1777 }
1778
1779 rtl8139_get_anar_state(anar, advertisement);
1780 rtl8139_get_anar_state(anar_lp, their_adv);
1781
1782 return EOK;
1783}
1784
1785/** Restart autonegotiation process
1786 *
1787 * @param dev The device to update
1788 *
1789 * @returns EOK
1790 */
1791static int rtl8139_autoneg_restart(ddf_fun_t *fun)
1792{
1793 assert(fun);
1794
1795 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1796 assert(rtl8139);
1797
1798 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1799 bmcr |= BMCR_AN_RESTART;
1800 bmcr |= BMCR_AN_ENABLE;
1801
1802 rtl8139_regs_unlock(rtl8139);
1803 pio_write_16(rtl8139->io_port + BMCR, bmcr);
1804 rtl8139_regs_lock(rtl8139);
1805
1806 return EOK;
1807}
1808
1809/** Notify NIC framework about HW filtering state when promisc mode was disabled
1810 *
1811 * @param nic_data The NIC data
1812 * @param mcast_mode Current multicast mode
1813 * @param was_promisc Sign if the promiscuous mode was active before disabling
1814 */
1815inline static void rtl8139_rcx_promics_rem(nic_t *nic_data,
1816 nic_multicast_mode_t mcast_mode, uint8_t was_promisc)
1817{
1818 assert(nic_data);
1819
1820 if (was_promisc != 0) {
1821 if (mcast_mode == NIC_MULTICAST_LIST)
1822 nic_report_hw_filtering(nic_data, 1, 0, -1);
1823 else
1824 nic_report_hw_filtering(nic_data, 1, 1, -1);
1825 } else {
1826 nic_report_hw_filtering(nic_data, 1, -1, -1);
1827 }
1828}
1829
1830/** Set unicast frames acceptance mode
1831 *
1832 * @param nic_data The nic device to update
1833 * @param mode The mode to set
1834 * @param addr Ignored, no HW support, just use unicast promisc
1835 * @param addr_cnt Ignored, no HW support
1836 *
1837 * @returns EOK
1838 */
1839static int rtl8139_unicast_set(nic_t *nic_data, nic_unicast_mode_t mode,
1840 const nic_address_t *addr, size_t addr_cnt)
1841{
1842 assert(nic_data);
1843
1844 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1845 assert(rtl8139);
1846
1847 uint8_t was_promisc = rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS;
1848
1849 nic_multicast_mode_t mcast_mode;
1850 nic_query_multicast(nic_data, &mcast_mode, 0, NULL, NULL);
1851
1852 switch (mode) {
1853 case NIC_UNICAST_BLOCKED:
1854 rtl8139->rcr_data.ucast_mask = 0;
1855 rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
1856 break;
1857 case NIC_UNICAST_DEFAULT:
1858 rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH;
1859 rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
1860 break;
1861 case NIC_UNICAST_LIST:
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, 0, 1, -1);
1867 else
1868 nic_report_hw_filtering(nic_data, 0, 0, -1);
1869 break;
1870 case NIC_UNICAST_PROMISC:
1871 rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH
1872 | RCR_ACCEPT_ALL_PHYS;
1873
1874 if (mcast_mode == NIC_MULTICAST_PROMISC)
1875 nic_report_hw_filtering(nic_data, 1, 1, -1);
1876 else
1877 nic_report_hw_filtering(nic_data, 1, 0, -1);
1878 break;
1879 default:
1880 return ENOTSUP;
1881 }
1882 fibril_mutex_lock(&rtl8139->rx_lock);
1883 rtl8139_hw_update_rcr(rtl8139);
1884 fibril_mutex_unlock(&rtl8139->rx_lock);
1885 return EOK;
1886}
1887
1888/** Set multicast frames acceptance mode
1889 *
1890 * @param nic_data The nic device to update
1891 * @param mode The mode to set
1892 * @param addr Addresses to accept
1893 * @param addr_cnt Addresses count
1894 *
1895 * @returns EOK
1896 */
1897static int rtl8139_multicast_set(nic_t *nic_data, nic_multicast_mode_t mode,
1898 const nic_address_t *addr, size_t addr_count)
1899{
1900 assert(nic_data);
1901 assert(addr_count == 0 || addr);
1902
1903 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1904 assert(rtl8139);
1905
1906 switch (mode) {
1907 case NIC_MULTICAST_BLOCKED:
1908 rtl8139->rcr_data.mcast_mask = 0;
1909 if ((rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS) != 0)
1910 nic_report_hw_filtering(nic_data, -1, 0, -1);
1911 else
1912 nic_report_hw_filtering(nic_data, -1, 1, -1);
1913 break;
1914 case NIC_MULTICAST_LIST:
1915 rtl8139_hw_set_mcast_mask(rtl8139, nic_mcast_hash(addr, addr_count));
1916 rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
1917 nic_report_hw_filtering(nic_data, -1, 0, -1);
1918 break;
1919 case NIC_MULTICAST_PROMISC:
1920 rtl8139_hw_set_mcast_mask(rtl8139, RTL8139_MCAST_MASK_PROMISC);
1921 rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
1922 nic_report_hw_filtering(nic_data, -1, 1, -1);
1923 break;
1924 default:
1925 return ENOTSUP;
1926 }
1927 fibril_mutex_lock(&rtl8139->rx_lock);
1928 rtl8139_hw_update_rcr(rtl8139);
1929 fibril_mutex_unlock(&rtl8139->rx_lock);
1930 return EOK;
1931}
1932
1933/** Set broadcast frames acceptance mode
1934 *
1935 * @param nic_data The nic device to update
1936 * @param mode The mode to set
1937 *
1938 * @returns EOK
1939 */
1940static int rtl8139_broadcast_set(nic_t *nic_data, nic_broadcast_mode_t mode)
1941{
1942 assert(nic_data);
1943
1944 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1945 assert(rtl8139);
1946
1947 switch (mode) {
1948 case NIC_BROADCAST_BLOCKED:
1949 rtl8139->rcr_data.bcast_mask = 0;
1950 break;
1951 case NIC_BROADCAST_ACCEPTED:
1952 rtl8139->rcr_data.bcast_mask = RCR_ACCEPT_BROADCAST;
1953 break;
1954 default:
1955 return ENOTSUP;
1956 }
1957 fibril_mutex_lock(&rtl8139->rx_lock);
1958 rtl8139_hw_update_rcr(rtl8139);
1959 fibril_mutex_unlock(&rtl8139->rx_lock);
1960 return EOK;
1961}
1962
1963/** Get state of acceptance of weird frames
1964 *
1965 * @param[in] device The device to check
1966 * @param[out] mode The current mode
1967 */
1968static int rtl8139_defective_get_mode(ddf_fun_t *fun, uint32_t *mode)
1969{
1970 assert(fun);
1971 assert(mode);
1972
1973 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1974 assert(rtl8139);
1975
1976 *mode = 0;
1977 if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_ERROR)
1978 *mode |= NIC_DEFECTIVE_BAD_CRC;
1979 if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_RUNT)
1980 *mode |= NIC_DEFECTIVE_SHORT;
1981
1982 return EOK;
1983};
1984
1985/** Set acceptance of weird frames
1986 *
1987 * @param device The device to update
1988 * @param mode The mode to set
1989 *
1990 * @returns ENOTSUP if the mode is not supported
1991 * @returns EOK of mode was set
1992 */
1993static int rtl8139_defective_set_mode(ddf_fun_t *fun, uint32_t mode)
1994{
1995 assert(fun);
1996
1997 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1998 assert(rtl8139);
1999
2000 if ((mode & (NIC_DEFECTIVE_SHORT | NIC_DEFECTIVE_BAD_CRC)) != mode)
2001 return ENOTSUP;
2002
2003 rtl8139->rcr_data.defect_mask = 0;
2004 if (mode & NIC_DEFECTIVE_SHORT)
2005 rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_RUNT;
2006 if (mode & NIC_DEFECTIVE_BAD_CRC)
2007 rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_ERROR;
2008
2009 fibril_mutex_lock(&rtl8139->rx_lock);
2010 rtl8139_hw_update_rcr(rtl8139);
2011 fibril_mutex_unlock(&rtl8139->rx_lock);
2012 return EOK;
2013};
2014
2015
2016/** Turn Wakeup On Lan method on
2017 *
2018 * @param nic_data The NIC to update
2019 * @param virtue The method to turn on
2020 *
2021 * @returns EINVAL if the method is not supported
2022 * @returns EOK if succeed
2023 * @returns ELIMIT if no more methods of this kind can be enabled
2024 */
2025static int rtl8139_wol_virtue_add(nic_t *nic_data,
2026 const nic_wol_virtue_t *virtue)
2027{
2028 assert(nic_data);
2029 assert(virtue);
2030
2031 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2032 assert(rtl8139);
2033
2034 switch(virtue->type) {
2035 case NIC_WV_BROADCAST:
2036 rtl8139_hw_reg_add_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
2037 break;
2038 case NIC_WV_LINK_CHANGE:
2039 rtl8139_regs_unlock(rtl8139->io_port);
2040 rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
2041 rtl8139_regs_lock(rtl8139->io_port);
2042 break;
2043 case NIC_WV_MAGIC_PACKET:
2044 if (virtue->data)
2045 return EINVAL;
2046 rtl8139_regs_unlock(rtl8139->io_port);
2047 rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
2048 rtl8139_regs_lock(rtl8139->io_port);
2049 break;
2050 default:
2051 return EINVAL;
2052 };
2053 if(rtl8139->pm.active++ == 0)
2054 rtl8139_hw_pmen_set(rtl8139, 1);
2055 return EOK;
2056}
2057
2058/** Turn Wakeup On Lan method off
2059 *
2060 * @param nic_data The NIC to update
2061 * @param virtue The method to turn off
2062 */
2063static void rtl8139_wol_virtue_rem(nic_t *nic_data,
2064 const nic_wol_virtue_t *virtue)
2065{
2066 assert(nic_data);
2067 assert(virtue);
2068
2069 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2070 assert(rtl8139);
2071
2072 switch(virtue->type) {
2073 case NIC_WV_BROADCAST:
2074 rtl8139_hw_reg_rem_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
2075 break;
2076 case NIC_WV_LINK_CHANGE:
2077 rtl8139_regs_unlock(rtl8139->io_port);
2078 rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
2079 rtl8139_regs_lock(rtl8139->io_port);
2080 break;
2081 case NIC_WV_MAGIC_PACKET:
2082 rtl8139_regs_unlock(rtl8139->io_port);
2083 rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
2084 rtl8139_regs_lock(rtl8139->io_port);
2085 break;
2086 default:
2087 return;
2088 };
2089 rtl8139->pm.active--;
2090 if (rtl8139->pm.active == 0)
2091 rtl8139_hw_pmen_set(rtl8139, 0);
2092}
2093
2094
2095/** Set polling mode
2096 *
2097 * @param device The device to set
2098 * @param mode The mode to set
2099 * @param period The period for NIC_POLL_PERIODIC
2100 *
2101 * @returns EOK if succeed
2102 * @returns ENOTSUP if the mode is not supported
2103 */
2104static int rtl8139_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
2105 const struct timeval *period)
2106{
2107 assert(nic_data);
2108 int rc = EOK;
2109
2110 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2111 assert(rtl8139);
2112
2113 fibril_mutex_lock(&rtl8139->rx_lock);
2114
2115 switch(mode) {
2116 case NIC_POLL_IMMEDIATE:
2117 rtl8139->int_mask = RTL_DEFAULT_INTERRUPTS;
2118 break;
2119 case NIC_POLL_ON_DEMAND:
2120 rtl8139->int_mask = 0;
2121 break;
2122 case NIC_POLL_PERIODIC:
2123 assert(period);
2124
2125 rtl8139_timer_act_t new_timer;
2126 rc = rtl8139_timer_act_init(&new_timer, RTL8139_PCI_FREQ_KHZ, period);
2127 if (rc != EOK)
2128 break;
2129
2130 /* Disable timer interrupts while working with timer-related data */
2131 rtl8139->int_mask = 0;
2132 rtl8139_hw_int_enable(rtl8139);
2133
2134 rtl8139->poll_timer = new_timer;
2135 rtl8139->int_mask = INT_TIME_OUT;
2136
2137 /* Force timer interrupt start be writing nonzero value to timer
2138 * interrutp register (should be small to prevent big delay)
2139 * Read TCTR to reset timer counter
2140 * Change values to simulate the last interrupt from the period.
2141 */
2142 pio_write_32(rtl8139->io_port + TIMERINT, 10);
2143 pio_write_32(rtl8139->io_port + TCTR, 0);
2144
2145 ddf_msg(LVL_DEBUG, "Periodic mode. Interrupt mask %"PRIx16", poll.full_skips %"
2146 PRIu32", last timer %"PRIu32".", rtl8139->int_mask,
2147 rtl8139->poll_timer.full_skips, rtl8139->poll_timer.last_val);
2148 break;
2149 default:
2150 rc = ENOTSUP;
2151 break;
2152 }
2153
2154 rtl8139_hw_int_enable(rtl8139);
2155
2156 fibril_mutex_unlock(&rtl8139->rx_lock);
2157
2158 return rc;
2159}
2160
2161/** Force receiving all frames in the receive buffer
2162 *
2163 * @param device The device to receive
2164 */
2165static void rtl8139_poll(nic_t *nic_data)
2166{
2167 assert(nic_data);
2168
2169 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2170 assert(rtl8139);
2171
2172 uint16_t isr = pio_read_16(rtl8139->io_port + ISR);
2173 pio_write_16(rtl8139->io_port + ISR, 0);
2174
2175 rtl8139_interrupt_impl(nic_data, isr);
2176}
2177
2178
2179/** Main function of RTL8139 driver
2180 *
2181 * Just initialize the driver structures and
2182 * put it into the device drivers interface
2183 */
2184int main(void)
2185{
2186 int rc = nic_driver_init(NAME);
2187 if (rc != EOK)
2188 return rc;
2189 nic_driver_implement(
2190 &rtl8139_driver_ops, &rtl8139_dev_ops, &rtl8139_nic_iface);
2191
2192 ddf_log_init(NAME, LVL_ERROR);
2193 ddf_msg(LVL_NOTE, "HelenOS RTL8139 driver started");
2194 return ddf_driver_main(&rtl8139_driver);
2195}
Note: See TracBrowser for help on using the repository browser.