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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f0b74b2 was f0b74b2, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Make rtl8139 driver work.

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