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

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

merge Realtek RTL8319 and Intel E1000 drivers from lp:~helenos-nicf/helenos/nicf
(the drivers do not compile and are not part of the build system so far)

  • Property mode set to 100644
File size: 58.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Michalec
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <assert.h>
30#include <errno.h>
31#include <align.h>
32#include <byteorder.h>
33#include <libarch/ddi.h>
34#include <libarch/barrier.h>
35#include <nlog.h>
36
37#include <dma.h>
38#include <ddf/interrupt.h>
39#include <devman.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 "rtl8139_defs.h"
53#include "rtl8139_driver.h"
54#include "rtl8139_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 nlog_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_add_device(ddf_dev_t *dev);
376
377/** Basic driver operations for RTL8139 driver */
378static driver_ops_t rtl8139_driver_ops = {
379 .add_device = &rtl8139_add_device,
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 nlog_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 nlog_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 nlog_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 nlog_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.physical));
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 nlog_error("Can not allocate frame list for received packets.");
591
592 void *rx_buffer = rtl8139->rx_buff.virtual;
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 nlog_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 nlog_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 nlog_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 nlog_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_mem.physical + 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.physical));
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 nlog_info("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 nlog_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 nlog_error("%s device: unexpected irq count", dev->name);
1082 return EINVAL;
1083 };
1084 if (hw_resources->io_ranges.count != 1) {
1085 nlog_error("%s device: unexpected io ranges count", dev->name);
1086 return EINVAL;
1087 }
1088
1089 rtl8139->irq = hw_resources->irqs.irqs[0];
1090 nlog_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 nlog_error("i/o range assigned to the device "
1095 "%s is too small.", dev->name);
1096 return EINVAL;
1097 }
1098 nlog_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 assert(rtl8139);
1145 size_t i = 0;
1146 int rc;
1147
1148 nlog_debug("Creating buffers");
1149
1150 rtl8139->tx_buff_mem.size = TX_PAGES;
1151 rtl8139->tx_buff_mem.mapping_flags = AS_AREA_WRITE;
1152 rc = dma_allocate_anonymous(&rtl8139->tx_buff_mem, DMA_32_BITS);
1153 if (rc != EOK) {
1154 nlog_error("Can not allocate transmitter buffers.");
1155 goto err_tx_alloc;
1156 }
1157
1158 for(i = 0; i < TX_BUFF_COUNT; ++i)
1159 rtl8139->tx_buff[i] = rtl8139->tx_buff_mem.virtual + i * TX_BUFF_SIZE;
1160
1161 nlog_debug("The transmittion buffers allocated");
1162
1163 /* Use the first buffer for next transmittion */
1164 rtl8139->tx_next = 0;
1165 rtl8139->tx_used = 0;
1166
1167 /* Allocate buffer for receiver */
1168 size_t rx_pages = ALIGN_UP(RxBUF_TOT_LENGTH, PAGE_SIZE) / PAGE_SIZE;
1169 nlog_debug("Allocating receiver buffer of the size %zd pages", rx_pages);
1170
1171 rtl8139->rx_buff.size = rx_pages;
1172 rtl8139->rx_buff.mapping_flags = AS_AREA_READ;
1173 rc = dma_allocate_anonymous(&rtl8139->rx_buff, DMA_32_BITS);
1174 if( rc != EOK ) {
1175 nlog_error("Can not allocate receiver buffer.");
1176 goto err_rx_alloc;
1177 }
1178 nlog_debug("The buffers created");
1179
1180 return EOK;
1181
1182err_rx_alloc:
1183 dma_free(&rtl8139->tx_buff_mem);
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 nlog_debug("rtl8139_dev_initialize %s", dev->name);
1197
1198 int ret = EOK;
1199
1200 nlog_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 nlog_error("Not enough memory for initializing %s.", dev->name);
1206 return ENOMEM;
1207 }
1208
1209 nlog_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 nlog_error("Can not obatin hw resources information");
1215 goto failed;
1216 }
1217
1218 nlog_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 packet 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 packet length */
1231 rtl8139->rcr_data.rcr_base = (0x8 << RCR_ERTH_SHIFT);
1232
1233 nlog_debug("The device is initialized");
1234 return ret;
1235
1236failed:
1237 nlog_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 nlog_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 nlog_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 nlog_info("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_add_device(ddf_dev_t *dev)
1299{
1300 assert(dev);
1301 nlog_info("RTL8139_add_device %s (handle = %d)", dev->name, dev->handle);
1302
1303 /* Init device structure for rtl8139 */
1304 int rc = rtl8139_device_initialize(dev);
1305 if (rc != EOK)
1306 return rc;
1307
1308 /* Map I/O ports */
1309 rc = rtl8139_pio_enable(dev);
1310 if (rc != EOK)
1311 goto err_destroy;
1312
1313 nic_t *nic_data = nic_get_from_ddf_dev(dev);
1314 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1315
1316 nic_address_t addr;
1317 rtl8139_hw_get_addr(rtl8139, &addr);
1318 rc = nic_report_address(nic_data, &addr);
1319 if (rc != EOK)
1320 goto err_pio;
1321
1322 /* Initialize the driver private structure */
1323 rtl8139_data_init(rtl8139);
1324
1325 /* Register interrupt handler */
1326 rc = rtl8139_register_int_handler(nic_data);
1327 if (rc != EOK)
1328 goto err_pio;
1329
1330 rc = nic_connect_to_services(nic_data);
1331 if (rc != EOK) {
1332 nlog_error("Failed to connect to services", rc);
1333 goto err_irq;
1334 }
1335
1336 rc = nic_register_as_ddf_fun(nic_data, &rtl8139_dev_ops);
1337 if (rc != EOK) {
1338 nlog_error("Failed to register as DDF function - error %d", rc);
1339 goto err_irq;
1340 }
1341
1342 nlog_info("The %s device has been successfully initialized.",
1343 dev->name);
1344
1345 return EOK;
1346
1347err_irq:
1348 unregister_interrupt_handler(dev, rtl8139->irq);
1349err_pio:
1350 // rtl8139_pio_disable(dev);
1351 /* TODO: find out if the pio_disable is needed */
1352err_destroy:
1353 rtl8139_dev_cleanup(dev);
1354 return rc;
1355};
1356
1357/** Set card MAC address
1358 *
1359 * @param device The RTL8139 device
1360 * @param address The place to store the address
1361 * @param max_len Maximal addresss length to store
1362 *
1363 * @return EOK if succeed, negative error code otherwise
1364 */
1365static int rtl8139_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
1366{
1367 assert(fun);
1368 assert(addr);
1369
1370 nic_t *nic_data =nic_get_from_ddf_fun((fun));
1371 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1372 assert(rtl8139);
1373
1374 rtl8139_lock_all(rtl8139);
1375
1376 int rc = nic_report_address(nic_data, addr);
1377 if ( rc != EOK) {
1378 rtl8139_unlock_all(rtl8139);
1379 return rc;
1380 }
1381
1382 rtl8139_hw_set_addr(rtl8139, addr);
1383
1384 rtl8139_unlock_all(rtl8139);
1385 return EOK;
1386}
1387
1388/** Get the device information
1389 *
1390 * @param dev The NIC device
1391 * @param info The information to fill
1392 *
1393 * @return EOK
1394 */
1395static int rtl8139_get_device_info(ddf_fun_t *fun, nic_device_info_t *info)
1396{
1397 assert(fun);
1398 assert(info);
1399
1400 nic_t *nic_data = nic_get_from_ddf_fun(fun);
1401 assert(nic_data);
1402 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1403 assert(rtl8139);
1404
1405 /* TODO: fill the information more completely */
1406 info->vendor_id = 0x10ec;
1407 str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH, "Realtek");
1408
1409 if (rtl8139->hw_version < RTL8139_VER_COUNT) {
1410 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
1411 model_names[rtl8139->hw_version]);
1412 } else {
1413 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, "RTL8139");
1414 }
1415
1416 info->ethernet_support[ETH_10M] = ETH_10BASE_T;
1417 info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
1418
1419 info->autoneg_support = RTL8139_AUTONEG_CAPS;
1420 return EOK;
1421}
1422
1423/** Check the cable state
1424 *
1425 * @param[in] dev The device
1426 * @param[out] state The state to fill
1427 *
1428 * @return EOK
1429 */
1430static int rtl8139_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
1431{
1432 assert(fun);
1433 assert(state);
1434
1435 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1436 assert(rtl8139);
1437
1438 if (pio_read_16(rtl8139->io_port + CSCR) & CS_CON_STATUS) {
1439 *state = NIC_CS_PLUGGED;
1440 } else {
1441 *state = NIC_CS_UNPLUGGED;
1442 }
1443
1444 return EOK;
1445}
1446
1447/** Get operation mode of the device
1448 */
1449static int rtl8139_get_operation_mode(ddf_fun_t *fun, int *speed,
1450 nic_channel_mode_t *duplex, nic_role_t *role)
1451{
1452 assert(fun);
1453 assert(speed);
1454 assert(duplex);
1455 assert(role);
1456
1457 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1458 assert(rtl8139);
1459
1460 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1461 uint8_t msr_val = pio_read_8(rtl8139->io_port + MSR);
1462
1463 if (bmcr_val & BMCR_DUPLEX) {
1464 *duplex = NIC_CM_FULL_DUPLEX;
1465 } else {
1466 *duplex = NIC_CM_HALF_DUPLEX;
1467 }
1468
1469 if (msr_val & MSR_SPEED10) {
1470 *speed = 10;
1471 } else {
1472 *speed = 100;
1473 }
1474
1475 *role = NIC_ROLE_UNKNOWN;
1476 return EOK;
1477}
1478
1479/** Value validity */
1480enum access_mode {
1481 /** Value is invalid now */
1482 VALUE_INVALID = 0,
1483 /** Read-only */
1484 VALUE_RO,
1485 /** Read-write */
1486 VALUE_RW
1487};
1488
1489/** Check if pause packet operations are valid in current situation
1490 *
1491 * @param rtl8139 RTL8139 private structure
1492 *
1493 * @return VALUE_INVALID if the value has no sense in current moment
1494 * @return VALUE_RO if the state is read-only in current moment
1495 * @return VALUE_RW if the state can be modified
1496 */
1497static int rtl8139_pause_is_valid(rtl8139_t *rtl8139)
1498{
1499 assert(rtl8139);
1500
1501 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1502 if ((bmcr & (BMCR_AN_ENABLE | BMCR_DUPLEX)) == 0)
1503 return VALUE_INVALID;
1504
1505 if (bmcr & BMCR_AN_ENABLE) {
1506 uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
1507 if (anar_lp & ANAR_PAUSE)
1508 return VALUE_RO;
1509 }
1510
1511 return VALUE_RW;
1512}
1513
1514/** Get current pause packet configuration
1515 *
1516 * Values are filled with NIC_RESULT_NOT_AVAILABLE if the value has no sense in
1517 * the moment (half-duplex).
1518 *
1519 * @param[in] fun The DDF structure of the RTL8139
1520 * @param[out] we_send Sign if local constroller sends pause packets
1521 * @param[out] we_receive Sign if local constroller receives pause packets
1522 * @param[out] time Time filled in pause packets. 0xFFFF in rtl8139
1523 *
1524 * @return EOK if succeed
1525 */
1526static int rtl8139_pause_get(ddf_fun_t *fun, nic_result_t *we_send,
1527 nic_result_t *we_receive, uint16_t *time)
1528{
1529 assert(fun);
1530 assert(we_send);
1531 assert(we_receive);
1532
1533 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1534 assert(rtl8139);
1535
1536 if (rtl8139_pause_is_valid(rtl8139) == VALUE_INVALID) {
1537 *we_send = NIC_RESULT_NOT_AVAILABLE;
1538 *we_receive = NIC_RESULT_NOT_AVAILABLE;
1539 *time = 0;
1540 return EOK;
1541 }
1542
1543 uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
1544
1545 *we_send = (msr & MSR_TXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
1546 *we_receive = (msr & MSR_RXFCE) ? NIC_RESULT_ENABLED : NIC_RESULT_DISABLED;
1547 *time = RTL8139_PAUSE_VAL;
1548
1549 return EOK;
1550};
1551
1552/** Set current pause packet configuration
1553 *
1554 * @param fun The DDF structure of the RTL8139
1555 * @param allow_send Sign if local constroller sends pause packets
1556 * @param allow_receive Sign if local constroller receives pause packets
1557 * @param time Time to use, ignored (not supported by device)
1558 *
1559 * @return EOK if succeed, INVAL if the pause packet has no sence
1560 */
1561static int rtl8139_pause_set(ddf_fun_t *fun, int allow_send, int allow_receive,
1562 uint16_t time)
1563{
1564 assert(fun);
1565
1566 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1567 assert(rtl8139);
1568
1569 if (rtl8139_pause_is_valid(rtl8139) != VALUE_RW)
1570 return EINVAL;
1571
1572 uint8_t msr = pio_read_8(rtl8139->io_port + MSR);
1573 msr &= ~(uint8_t)(MSR_TXFCE | MSR_RXFCE);
1574
1575 if (allow_receive)
1576 msr |= MSR_RXFCE;
1577 if (allow_send)
1578 msr |= MSR_TXFCE;
1579
1580 pio_write_8(rtl8139->io_port + MSR, msr);
1581
1582 if (allow_send && time > 0) {
1583 nlog_warning("Time setting is not supported in set_pause method.");
1584 }
1585 return EOK;
1586};
1587
1588/** Set operation mode of the device
1589 *
1590 */
1591static int rtl8139_set_operation_mode(ddf_fun_t *fun, int speed,
1592 nic_channel_mode_t duplex, nic_role_t role)
1593{
1594 assert(fun);
1595
1596 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1597 assert(rtl8139);
1598
1599 if (speed != 10 && speed != 100)
1600 return EINVAL;
1601 if (duplex != NIC_CM_HALF_DUPLEX && duplex != NIC_CM_FULL_DUPLEX)
1602 return EINVAL;
1603
1604 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1605
1606 /* Set autonegotion disabled */
1607 bmcr_val &= ~(uint16_t)BMCR_AN_ENABLE;
1608
1609 if (duplex == NIC_CM_FULL_DUPLEX) {
1610 bmcr_val |= BMCR_DUPLEX;
1611 } else {
1612 bmcr_val &= ~((uint16_t)BMCR_DUPLEX);
1613 }
1614
1615 if (speed == 100) {
1616 bmcr_val |= BMCR_Spd_100;
1617 } else {
1618 bmcr_val &= ~((uint16_t)BMCR_Spd_100);
1619 }
1620
1621 rtl8139_regs_unlock(rtl8139->io_port);
1622 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1623 rtl8139_regs_lock(rtl8139->io_port);
1624 return EOK;
1625}
1626
1627/** Enable autonegoation with specific advertisement
1628 *
1629 * @param dev The device to update
1630 * @param advertisement The advertisement to set
1631 *
1632 * @returns EINVAL if the advertisement mode is not supproted
1633 * @returns EOK if advertisement mode set successfully
1634 */
1635static int rtl8139_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement)
1636{
1637 assert(fun);
1638
1639 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1640 assert(rtl8139);
1641
1642 if (advertisement == 0) {
1643 advertisement = RTL8139_AUTONEG_CAPS;
1644 }
1645
1646 if ((advertisement | RTL8139_AUTONEG_CAPS) != RTL8139_AUTONEG_CAPS)
1647 return EINVAL; /* some unsuported mode is requested */
1648
1649 assert(advertisement != 0);
1650
1651 /* Set the autonegotiation advertisement */
1652 uint16_t anar = ANAR_SELECTOR; /* default selector */
1653 if (advertisement & ETH_AUTONEG_10BASE_T_FULL)
1654 anar |= ANAR_10_FD;
1655 if (advertisement & ETH_AUTONEG_10BASE_T_HALF)
1656 anar |= ANAR_10_HD;
1657 if (advertisement & ETH_AUTONEG_100BASE_TX_FULL)
1658 anar |= ANAR_100TX_FD;
1659 if (advertisement & ETH_AUTONEG_100BASE_TX_HALF)
1660 anar |= ANAR_100TX_HD;
1661 if (advertisement & ETH_AUTONEG_PAUSE_SYMETRIC)
1662 anar |= ANAR_PAUSE;
1663
1664 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1665 bmcr_val |= BMCR_AN_ENABLE;
1666
1667 pio_write_16(rtl8139->io_port + ANAR, anar);
1668
1669 rtl8139_regs_unlock(rtl8139->io_port);
1670 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1671 rtl8139_regs_lock(rtl8139->io_port);
1672 return EOK;
1673}
1674
1675/** Disable advertisement functionality
1676 *
1677 * @param dev The device to update
1678 *
1679 * @returns EOK
1680 */
1681static int rtl8139_autoneg_disable(ddf_fun_t *fun)
1682{
1683 assert(fun);
1684
1685 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1686 assert(rtl8139);
1687
1688 uint16_t bmcr_val = pio_read_16(rtl8139->io_port + BMCR);
1689
1690 bmcr_val &= ~((uint16_t)BMCR_AN_ENABLE);
1691
1692 rtl8139_regs_unlock(rtl8139->io_port);
1693 pio_write_16(rtl8139->io_port + BMCR, bmcr_val);
1694 rtl8139_regs_lock(rtl8139->io_port);
1695
1696 return EOK;
1697}
1698
1699/** Obtain the advertisement NIC framework value from the ANAR/ANLPAR register
1700 * value
1701 *
1702 * @param[in] anar The ANAR register value
1703 * @param[out] advertisement The advertisement result
1704 */
1705static void rtl8139_get_anar_state(uint16_t anar, uint32_t *advertisement)
1706{
1707 *advertisement = 0;
1708 if (anar & ANAR_10_HD)
1709 *advertisement |= ETH_AUTONEG_10BASE_T_HALF;
1710 if (anar & ANAR_10_FD)
1711 *advertisement |= ETH_AUTONEG_10BASE_T_FULL;
1712 if (anar & ANAR_100TX_HD)
1713 *advertisement |= ETH_AUTONEG_100BASE_TX_HALF;
1714 if (anar & ANAR_100TX_FD)
1715 *advertisement |= ETH_AUTONEG_100BASE_TX_FULL;
1716 if (anar & ANAR_100T4)
1717 *advertisement |= ETH_AUTONEG_100BASE_T4_HALF;
1718 if (anar & ANAR_PAUSE)
1719 *advertisement |= ETH_AUTONEG_PAUSE_SYMETRIC;
1720}
1721
1722/** Check the autonegotion state
1723 *
1724 * @param[in] dev The device to check
1725 * @param[out] advertisement The device advertisement
1726 * @param[out] their_adv The partners advertisement
1727 * @param[out] result The autonegotion state
1728 * @param[out] their_result The link partner autonegotion status
1729 *
1730 * @returns EOK
1731 */
1732static int rtl8139_autoneg_probe(ddf_fun_t *fun, uint32_t *advertisement,
1733 uint32_t *their_adv, nic_result_t *result, nic_result_t *their_result)
1734{
1735 assert(fun);
1736
1737 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1738 assert(rtl8139);
1739
1740 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1741 uint16_t anar = pio_read_16(rtl8139->io_port + ANAR);
1742 uint16_t anar_lp = pio_read_16(rtl8139->io_port + ANLPAR);
1743 uint16_t aner = pio_read_16(rtl8139->io_port + ANER);
1744
1745 if (bmcr & BMCR_AN_ENABLE) {
1746 *result = NIC_RESULT_ENABLED;
1747 } else {
1748 *result = NIC_RESULT_DISABLED;
1749 }
1750
1751 if (aner & ANER_LP_NW_ABLE) {
1752 *their_result = NIC_RESULT_ENABLED;
1753 } else {
1754 *their_result = NIC_RESULT_DISABLED;
1755 }
1756
1757 rtl8139_get_anar_state(anar, advertisement);
1758 rtl8139_get_anar_state(anar_lp, their_adv);
1759
1760 return EOK;
1761}
1762
1763/** Restart autonegotiation process
1764 *
1765 * @param dev The device to update
1766 *
1767 * @returns EOK
1768 */
1769static int rtl8139_autoneg_restart(ddf_fun_t *fun)
1770{
1771 assert(fun);
1772
1773 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1774 assert(rtl8139);
1775
1776 uint16_t bmcr = pio_read_16(rtl8139->io_port + BMCR);
1777 bmcr |= BMCR_AN_RESTART;
1778 bmcr |= BMCR_AN_ENABLE;
1779
1780 rtl8139_regs_unlock(rtl8139);
1781 pio_write_16(rtl8139->io_port + BMCR, bmcr);
1782 rtl8139_regs_lock(rtl8139);
1783
1784 return EOK;
1785}
1786
1787/** Notify NIC framework about HW filtering state when promisc mode was disabled
1788 *
1789 * @param nic_data The NIC data
1790 * @param mcast_mode Current multicast mode
1791 * @param was_promisc Sign if the promiscuous mode was active before disabling
1792 */
1793inline static void rtl8139_rcx_promics_rem(nic_t *nic_data,
1794 nic_multicast_mode_t mcast_mode, uint8_t was_promisc)
1795{
1796 assert(nic_data);
1797
1798 if (was_promisc != 0) {
1799 if (mcast_mode == NIC_MULTICAST_LIST)
1800 nic_report_hw_filtering(nic_data, 1, 0, -1);
1801 else
1802 nic_report_hw_filtering(nic_data, 1, 1, -1);
1803 } else {
1804 nic_report_hw_filtering(nic_data, 1, -1, -1);
1805 }
1806}
1807
1808/** Set unicast packets acceptance mode
1809 *
1810 * @param nic_data The nic device to update
1811 * @param mode The mode to set
1812 * @param addr Ignored, no HW support, just use unicast promisc
1813 * @param addr_cnt Ignored, no HW support
1814 *
1815 * @returns EOK
1816 */
1817static int rtl8139_unicast_set(nic_t *nic_data, nic_unicast_mode_t mode,
1818 const nic_address_t *addr, size_t addr_cnt)
1819{
1820 assert(nic_data);
1821
1822 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1823 assert(rtl8139);
1824
1825 uint8_t was_promisc = rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS;
1826
1827 nic_multicast_mode_t mcast_mode;
1828 nic_query_multicast(nic_data, &mcast_mode, 0, NULL, NULL);
1829
1830 switch (mode) {
1831 case NIC_UNICAST_BLOCKED:
1832 rtl8139->rcr_data.ucast_mask = 0;
1833 rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
1834 break;
1835 case NIC_UNICAST_DEFAULT:
1836 rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH;
1837 rtl8139_rcx_promics_rem(nic_data, mcast_mode, was_promisc);
1838 break;
1839 case NIC_UNICAST_LIST:
1840 rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH
1841 | RCR_ACCEPT_ALL_PHYS;
1842
1843 if (mcast_mode == NIC_MULTICAST_PROMISC)
1844 nic_report_hw_filtering(nic_data, 0, 1, -1);
1845 else
1846 nic_report_hw_filtering(nic_data, 0, 0, -1);
1847 break;
1848 case NIC_UNICAST_PROMISC:
1849 rtl8139->rcr_data.ucast_mask = RCR_ACCEPT_PHYS_MATCH
1850 | RCR_ACCEPT_ALL_PHYS;
1851
1852 if (mcast_mode == NIC_MULTICAST_PROMISC)
1853 nic_report_hw_filtering(nic_data, 1, 1, -1);
1854 else
1855 nic_report_hw_filtering(nic_data, 1, 0, -1);
1856 break;
1857 default:
1858 return ENOTSUP;
1859 }
1860 fibril_mutex_lock(&rtl8139->rx_lock);
1861 rtl8139_hw_update_rcr(rtl8139);
1862 fibril_mutex_unlock(&rtl8139->rx_lock);
1863 return EOK;
1864}
1865
1866/** Set multicast packets acceptance mode
1867 *
1868 * @param nic_data The nic device to update
1869 * @param mode The mode to set
1870 * @param addr Addresses to accept
1871 * @param addr_cnt Addresses count
1872 *
1873 * @returns EOK
1874 */
1875static int rtl8139_multicast_set(nic_t *nic_data, nic_multicast_mode_t mode,
1876 const nic_address_t *addr, size_t addr_count)
1877{
1878 assert(nic_data);
1879 assert(addr_count == 0 || addr);
1880
1881 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1882 assert(rtl8139);
1883
1884 switch (mode) {
1885 case NIC_MULTICAST_BLOCKED:
1886 rtl8139->rcr_data.mcast_mask = 0;
1887 if ((rtl8139->rcr_data.ucast_mask & RCR_ACCEPT_ALL_PHYS) != 0)
1888 nic_report_hw_filtering(nic_data, -1, 0, -1);
1889 else
1890 nic_report_hw_filtering(nic_data, -1, 1, -1);
1891 break;
1892 case NIC_MULTICAST_LIST:
1893 rtl8139_hw_set_mcast_mask(rtl8139, nic_mcast_hash(addr, addr_count));
1894 rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
1895 nic_report_hw_filtering(nic_data, -1, 0, -1);
1896 break;
1897 case NIC_MULTICAST_PROMISC:
1898 rtl8139_hw_set_mcast_mask(rtl8139, RTL8139_MCAST_MASK_PROMISC);
1899 rtl8139->rcr_data.mcast_mask = RCR_ACCEPT_MULTICAST;
1900 nic_report_hw_filtering(nic_data, -1, 1, -1);
1901 break;
1902 default:
1903 return ENOTSUP;
1904 }
1905 fibril_mutex_lock(&rtl8139->rx_lock);
1906 rtl8139_hw_update_rcr(rtl8139);
1907 fibril_mutex_unlock(&rtl8139->rx_lock);
1908 return EOK;
1909}
1910
1911/** Set broadcast packets acceptance mode
1912 *
1913 * @param nic_data The nic device to update
1914 * @param mode The mode to set
1915 *
1916 * @returns EOK
1917 */
1918static int rtl8139_broadcast_set(nic_t *nic_data, nic_broadcast_mode_t mode)
1919{
1920 assert(nic_data);
1921
1922 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
1923 assert(rtl8139);
1924
1925 switch (mode) {
1926 case NIC_BROADCAST_BLOCKED:
1927 rtl8139->rcr_data.bcast_mask = 0;
1928 break;
1929 case NIC_BROADCAST_ACCEPTED:
1930 rtl8139->rcr_data.bcast_mask = RCR_ACCEPT_BROADCAST;
1931 break;
1932 default:
1933 return ENOTSUP;
1934 }
1935 fibril_mutex_lock(&rtl8139->rx_lock);
1936 rtl8139_hw_update_rcr(rtl8139);
1937 fibril_mutex_unlock(&rtl8139->rx_lock);
1938 return EOK;
1939}
1940
1941/** Get state of acceptance of weird packets
1942 *
1943 * @param[in] device The device to check
1944 * @param[out] mode The current mode
1945 */
1946static int rtl8139_defective_get_mode(ddf_fun_t *fun, uint32_t *mode)
1947{
1948 assert(fun);
1949 assert(mode);
1950
1951 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1952 assert(rtl8139);
1953
1954 *mode = 0;
1955 if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_ERROR)
1956 *mode |= NIC_DEFECTIVE_BAD_CRC;
1957 if (rtl8139->rcr_data.defect_mask & RCR_ACCEPT_RUNT)
1958 *mode |= NIC_DEFECTIVE_SHORT;
1959
1960 return EOK;
1961};
1962
1963/** Set acceptance of weird packets
1964 *
1965 * @param device The device to update
1966 * @param mode The mode to set
1967 *
1968 * @returns ENOTSUP if the mode is not supported
1969 * @returns EOK of mode was set
1970 */
1971static int rtl8139_defective_set_mode(ddf_fun_t *fun, uint32_t mode)
1972{
1973 assert(fun);
1974
1975 rtl8139_t *rtl8139 = nic_get_specific(nic_get_from_ddf_fun(fun));
1976 assert(rtl8139);
1977
1978 if ((mode & (NIC_DEFECTIVE_SHORT | NIC_DEFECTIVE_BAD_CRC)) != mode)
1979 return ENOTSUP;
1980
1981 rtl8139->rcr_data.defect_mask = 0;
1982 if (mode & NIC_DEFECTIVE_SHORT)
1983 rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_RUNT;
1984 if (mode & NIC_DEFECTIVE_BAD_CRC)
1985 rtl8139->rcr_data.defect_mask |= RCR_ACCEPT_ERROR;
1986
1987 fibril_mutex_lock(&rtl8139->rx_lock);
1988 rtl8139_hw_update_rcr(rtl8139);
1989 fibril_mutex_unlock(&rtl8139->rx_lock);
1990 return EOK;
1991};
1992
1993
1994/** Turn Wakeup On Lan method on
1995 *
1996 * @param nic_data The NIC to update
1997 * @param virtue The method to turn on
1998 *
1999 * @returns EINVAL if the method is not supported
2000 * @returns EOK if succeed
2001 * @returns ELIMIT if no more methods of this kind can be enabled
2002 */
2003static int rtl8139_wol_virtue_add(nic_t *nic_data,
2004 const nic_wol_virtue_t *virtue)
2005{
2006 assert(nic_data);
2007 assert(virtue);
2008
2009 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2010 assert(rtl8139);
2011
2012 switch(virtue->type) {
2013 case NIC_WV_BROADCAST:
2014 rtl8139_hw_reg_add_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
2015 break;
2016 case NIC_WV_LINK_CHANGE:
2017 rtl8139_regs_unlock(rtl8139->io_port);
2018 rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
2019 rtl8139_regs_lock(rtl8139->io_port);
2020 break;
2021 case NIC_WV_MAGIC_PACKET:
2022 if (virtue->data)
2023 return EINVAL;
2024 rtl8139_regs_unlock(rtl8139->io_port);
2025 rtl8139_hw_reg_add_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
2026 rtl8139_regs_lock(rtl8139->io_port);
2027 break;
2028 default:
2029 return EINVAL;
2030 };
2031 if(rtl8139->pm.active++ == 0)
2032 rtl8139_hw_pmen_set(rtl8139, 1);
2033 return EOK;
2034}
2035
2036/** Turn Wakeup On Lan method off
2037 *
2038 * @param nic_data The NIC to update
2039 * @param virtue The method to turn off
2040 */
2041static void rtl8139_wol_virtue_rem(nic_t *nic_data,
2042 const nic_wol_virtue_t *virtue)
2043{
2044 assert(nic_data);
2045 assert(virtue);
2046
2047 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2048 assert(rtl8139);
2049
2050 switch(virtue->type) {
2051 case NIC_WV_BROADCAST:
2052 rtl8139_hw_reg_rem_8(rtl8139, CONFIG5, CONFIG5_BROADCAST_WAKEUP);
2053 break;
2054 case NIC_WV_LINK_CHANGE:
2055 rtl8139_regs_unlock(rtl8139->io_port);
2056 rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_LINK_UP);
2057 rtl8139_regs_lock(rtl8139->io_port);
2058 break;
2059 case NIC_WV_MAGIC_PACKET:
2060 rtl8139_regs_unlock(rtl8139->io_port);
2061 rtl8139_hw_reg_rem_8(rtl8139, CONFIG3, CONFIG3_MAGIC);
2062 rtl8139_regs_lock(rtl8139->io_port);
2063 break;
2064 default:
2065 return;
2066 };
2067 rtl8139->pm.active--;
2068 if (rtl8139->pm.active == 0)
2069 rtl8139_hw_pmen_set(rtl8139, 0);
2070}
2071
2072
2073/** Set polling mode
2074 *
2075 * @param device The device to set
2076 * @param mode The mode to set
2077 * @param period The period for NIC_POLL_PERIODIC
2078 *
2079 * @returns EOK if succeed
2080 * @returns ENOTSUP if the mode is not supported
2081 */
2082static int rtl8139_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
2083 const struct timeval *period)
2084{
2085 assert(nic_data);
2086 int rc = EOK;
2087
2088 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2089 assert(rtl8139);
2090
2091 fibril_mutex_lock(&rtl8139->rx_lock);
2092
2093 switch(mode) {
2094 case NIC_POLL_IMMEDIATE:
2095 rtl8139->int_mask = RTL_DEFAULT_INTERRUPTS;
2096 break;
2097 case NIC_POLL_ON_DEMAND:
2098 rtl8139->int_mask = 0;
2099 break;
2100 case NIC_POLL_PERIODIC:
2101 assert(period);
2102
2103 rtl8139_timer_act_t new_timer;
2104 rc = rtl8139_timer_act_init(&new_timer, RTL8139_PCI_FREQ_KHZ, period);
2105 if (rc != EOK)
2106 break;
2107
2108 /* Disable timer interrupts while working with timer-related data */
2109 rtl8139->int_mask = 0;
2110 rtl8139_hw_int_enable(rtl8139);
2111
2112 rtl8139->poll_timer = new_timer;
2113 rtl8139->int_mask = INT_TIME_OUT;
2114
2115 /* Force timer interrupt start be writing nonzero value to timer
2116 * interrutp register (should be small to prevent big delay)
2117 * Read TCTR to reset timer counter
2118 * Change values to simulate the last interrupt from the period.
2119 */
2120 pio_write_32(rtl8139->io_port + TIMERINT, 10);
2121 pio_write_32(rtl8139->io_port + TCTR, 0);
2122
2123 nlog_debug("Periodic mode. Interrupt mask %"PRIx16", poll.full_skips %"
2124 PRIu32", last timer %"PRIu32".", rtl8139->int_mask,
2125 rtl8139->poll_timer.full_skips, rtl8139->poll_timer.last_val);
2126 break;
2127 default:
2128 rc = ENOTSUP;
2129 break;
2130 }
2131
2132 rtl8139_hw_int_enable(rtl8139);
2133
2134 fibril_mutex_unlock(&rtl8139->rx_lock);
2135
2136 return rc;
2137}
2138
2139/** Force receiving all packets in the receive buffer
2140 *
2141 * @param device The device to receive
2142 */
2143static void rtl8139_poll(nic_t *nic_data)
2144{
2145 assert(nic_data);
2146
2147 rtl8139_t *rtl8139 = nic_get_specific(nic_data);
2148 assert(rtl8139);
2149
2150 uint16_t isr = pio_read_16(rtl8139->io_port + ISR);
2151 pio_write_16(rtl8139->io_port + ISR, 0);
2152
2153 rtl8139_interrupt_impl(nic_data, isr);
2154}
2155
2156
2157/** Main function of RTL8139 driver
2158 *
2159 * Just initialize the driver structures and
2160 * put it into the device drivers interface
2161 */
2162int main(void)
2163{
2164 int rc = nic_driver_init(NAME);
2165 if (rc != EOK)
2166 return rc;
2167 nic_driver_implement(
2168 &rtl8139_driver_ops, &rtl8139_dev_ops, &rtl8139_nic_iface);
2169
2170 nlog_set_min_severity(DEBUG);
2171 nlog_info("HelenOS RTL8139 driver started");
2172 return ddf_driver_main(&rtl8139_driver);
2173}
Note: See TracBrowser for help on using the repository browser.