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

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

Create DDF functions manually.

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