iPXE
realtek.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * (EEPROM code originally implemented for rtl8139.c)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 *
21 * You can also choose to distribute this program under the terms of
22 * the Unmodified Binary Distribution Licence (as given in the file
23 * COPYING.UBDL), provided that you have satisfied its requirements.
24 */
25
26FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27FILE_SECBOOT ( PERMITTED );
28
29#include <stdint.h>
30#include <string.h>
31#include <unistd.h>
32#include <errno.h>
33#include <byteswap.h>
34#include <ipxe/netdevice.h>
35#include <ipxe/ethernet.h>
36#include <ipxe/if_ether.h>
37#include <ipxe/iobuf.h>
38#include <ipxe/malloc.h>
39#include <ipxe/pci.h>
40#include <ipxe/dma.h>
41#include <ipxe/nvs.h>
42#include <ipxe/threewire.h>
43#include <ipxe/bitbash.h>
44#include <ipxe/mii.h>
45#include "realtek.h"
46
47/** @file
48 *
49 * Realtek 10/100/1000 network card driver
50 *
51 * Based on the following datasheets:
52 *
53 * http://www.datasheetarchive.com/dl/Datasheets-8/DSA-153536.pdf
54 * http://www.datasheetarchive.com/indexdl/Datasheet-028/DSA00494723.pdf
55 */
56
57/******************************************************************************
58 *
59 * Debugging
60 *
61 ******************************************************************************
62 */
63
64/**
65 * Dump all registers (for debugging)
66 *
67 * @v rtl Realtek device
68 */
69static __attribute__ (( unused )) void realtek_dump ( struct realtek_nic *rtl ){
70 uint8_t regs[256];
71 unsigned int i;
72
73 /* Do nothing unless debug output is enabled */
74 if ( ! DBG_LOG )
75 return;
76
77 /* Dump registers (via byte accesses; may not work for all registers) */
78 for ( i = 0 ; i < sizeof ( regs ) ; i++ )
79 regs[i] = readb ( rtl->regs + i );
80 DBGC ( rtl, "REALTEK %p register dump:\n", rtl );
81 DBGC_HDA ( rtl, 0, regs, sizeof ( regs ) );
82}
83
84/******************************************************************************
85 *
86 * EEPROM interface
87 *
88 ******************************************************************************
89 */
90
91/** Pin mapping for SPI bit-bashing interface */
98
99/**
100 * Open bit-bashing interface
101 *
102 * @v basher Bit-bashing interface
103 */
104static void realtek_spi_open_bit ( struct bit_basher *basher ) {
105 struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
106 spibit.basher );
107
108 /* Enable EEPROM access */
110 readb ( rtl->regs + RTL_9346CR ); /* Ensure write reaches chip */
111}
112
113/**
114 * Close bit-bashing interface
115 *
116 * @v basher Bit-bashing interface
117 */
118static void realtek_spi_close_bit ( struct bit_basher *basher ) {
119 struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
120 spibit.basher );
121
122 /* Disable EEPROM access */
124 readb ( rtl->regs + RTL_9346CR ); /* Ensure write reaches chip */
125}
126
127/**
128 * Read input bit
129 *
130 * @v basher Bit-bashing interface
131 * @v bit_id Bit number
132 * @ret zero Input is a logic 0
133 * @ret non-zero Input is a logic 1
134 */
135static int realtek_spi_read_bit ( struct bit_basher *basher,
136 unsigned int bit_id ) {
137 struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
138 spibit.basher );
139 uint8_t mask = realtek_eeprom_bits[bit_id];
140 uint8_t reg;
141
143 reg = readb ( rtl->regs + RTL_9346CR );
145 return ( reg & mask );
146}
147
148/**
149 * Set/clear output bit
150 *
151 * @v basher Bit-bashing interface
152 * @v bit_id Bit number
153 * @v data Value to write
154 */
155static void realtek_spi_write_bit ( struct bit_basher *basher,
156 unsigned int bit_id, unsigned long data ) {
157 struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
158 spibit.basher );
159 uint8_t mask = realtek_eeprom_bits[bit_id];
160 uint8_t reg;
161
163 reg = readb ( rtl->regs + RTL_9346CR );
164 reg &= ~mask;
165 reg |= ( data & mask );
166 writeb ( reg, rtl->regs + RTL_9346CR );
167 readb ( rtl->regs + RTL_9346CR ); /* Ensure write reaches chip */
169}
170
171/** SPI bit-bashing interface */
173 .open = realtek_spi_open_bit,
174 .close = realtek_spi_close_bit,
175 .read = realtek_spi_read_bit,
176 .write = realtek_spi_write_bit,
177};
178
179/**
180 * Initialise EEPROM
181 *
182 * @v netdev Network device
183 * @ret rc Return status code
184 */
185static int realtek_init_eeprom ( struct net_device *netdev ) {
186 struct realtek_nic *rtl = netdev->priv;
187 uint16_t id;
188 int rc;
189
190 /* Initialise SPI bit-bashing interface */
193 init_spi_bit_basher ( &rtl->spibit );
194
195 /* Detect EEPROM type and initialise three-wire device */
196 if ( readl ( rtl->regs + RTL_RCR ) & RTL_RCR_9356SEL ) {
197 DBGC ( rtl, "REALTEK %p EEPROM is a 93C56\n", rtl );
198 init_at93c56 ( &rtl->eeprom, 16 );
199 } else {
200 DBGC ( rtl, "REALTEK %p EEPROM is a 93C46\n", rtl );
201 init_at93c46 ( &rtl->eeprom, 16 );
202 }
203
204 /* Check for EEPROM presence. Some onboard NICs will have no
205 * EEPROM connected, with the BIOS being responsible for
206 * programming the initial register values.
207 */
208 if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_ID,
209 &id, sizeof ( id ) ) ) != 0 ) {
210 DBGC ( rtl, "REALTEK %p could not read EEPROM ID: %s\n",
211 rtl, strerror ( rc ) );
212 return rc;
213 }
214 if ( id != cpu_to_le16 ( RTL_EEPROM_ID_MAGIC ) ) {
215 DBGC ( rtl, "REALTEK %p EEPROM ID incorrect (%#04x); assuming "
216 "no EEPROM\n", rtl, le16_to_cpu ( id ) );
217 return -ENODEV;
218 }
219
220 /* Initialise space for non-volatile options, if available
221 *
222 * We use offset 0x40 (i.e. address 0x20), length 0x40. This
223 * block is marked as VPD in the Realtek datasheets, so we use
224 * it only if we detect that the card is not supporting VPD.
225 */
226 if ( readb ( rtl->regs + RTL_CONFIG1 ) & RTL_CONFIG1_VPD ) {
227 DBGC ( rtl, "REALTEK %p EEPROM in use for VPD; cannot use "
228 "for options\n", rtl );
229 } else {
230 nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, RTL_EEPROM_VPD,
231 RTL_EEPROM_VPD_LEN, NULL, &netdev->refcnt );
232 }
233
234 return 0;
235}
236
237/******************************************************************************
238 *
239 * MII interface
240 *
241 ******************************************************************************
242 */
243
244/**
245 * Read from MII register
246 *
247 * @v mdio MII interface
248 * @v phy PHY address
249 * @v reg Register address
250 * @ret value Data read, or negative error
251 */
253 unsigned int phy __unused, unsigned int reg ) {
254 struct realtek_nic *rtl =
255 container_of ( mdio, struct realtek_nic, mdio );
256 unsigned int i;
258
259 /* Fail if PHYAR register is not present */
260 if ( ! rtl->have_phy_regs )
261 return -ENOTSUP;
262
263 /* Initiate read */
264 writel ( RTL_PHYAR_VALUE ( 0, reg, 0 ), rtl->regs + RTL_PHYAR );
265
266 /* Wait for read to complete */
267 for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
268
269 /* If read is not complete, delay 1us and retry */
270 value = readl ( rtl->regs + RTL_PHYAR );
271 if ( ! ( value & RTL_PHYAR_FLAG ) ) {
272 udelay ( 1 );
273 continue;
274 }
275
276 /* Return register value */
277 return ( RTL_PHYAR_DATA ( value ) );
278 }
279
280 DBGC ( rtl, "REALTEK %p timed out waiting for MII read\n", rtl );
281 return -ETIMEDOUT;
282}
283
284/**
285 * Write to MII register
286 *
287 * @v mdio MII interface
288 * @v phy PHY address
289 * @v reg Register address
290 * @v data Data to write
291 * @ret rc Return status code
292 */
294 unsigned int phy __unused, unsigned int reg,
295 unsigned int data ) {
296 struct realtek_nic *rtl =
297 container_of ( mdio, struct realtek_nic, mdio );
298 unsigned int i;
299
300 /* Fail if PHYAR register is not present */
301 if ( ! rtl->have_phy_regs )
302 return -ENOTSUP;
303
304 /* Initiate write */
306 rtl->regs + RTL_PHYAR );
307
308 /* Wait for write to complete */
309 for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
310
311 /* If write is not complete, delay 1us and retry */
312 if ( readl ( rtl->regs + RTL_PHYAR ) & RTL_PHYAR_FLAG ) {
313 udelay ( 1 );
314 continue;
315 }
316
317 return 0;
318 }
319
320 DBGC ( rtl, "REALTEK %p timed out waiting for MII write\n", rtl );
321 return -ETIMEDOUT;
322}
323
324/** Realtek MII operations */
326 .read = realtek_mii_read,
327 .write = realtek_mii_write,
328};
329
330/******************************************************************************
331 *
332 * Device reset
333 *
334 ******************************************************************************
335 */
336
337/**
338 * Reset hardware
339 *
340 * @v rtl Realtek device
341 * @ret rc Return status code
342 */
343static int realtek_reset ( struct realtek_nic *rtl ) {
344 unsigned int i;
345
346 /* Issue reset */
347 writeb ( RTL_CR_RST, rtl->regs + RTL_CR );
348
349 /* Wait for reset to complete */
350 for ( i = 0 ; i < RTL_RESET_MAX_WAIT_MS ; i++ ) {
351
352 /* If reset is not complete, delay 1ms and retry */
353 if ( readb ( rtl->regs + RTL_CR ) & RTL_CR_RST ) {
354 mdelay ( 1 );
355 continue;
356 }
357
358 return 0;
359 }
360
361 DBGC ( rtl, "REALTEK %p timed out waiting for reset\n", rtl );
362 return -ETIMEDOUT;
363}
364
365/**
366 * Configure PHY for Gigabit operation
367 *
368 * @v rtl Realtek device
369 * @ret rc Return status code
370 */
371static int realtek_phy_speed ( struct realtek_nic *rtl ) {
372 int ctrl1000;
373 int rc;
374
375 /* Read CTRL1000 register */
376 ctrl1000 = mii_read ( &rtl->mii, MII_CTRL1000 );
377 if ( ctrl1000 < 0 ) {
378 rc = ctrl1000;
379 DBGC ( rtl, "REALTEK %p could not read CTRL1000: %s\n",
380 rtl, strerror ( rc ) );
381 return rc;
382 }
383
384 /* Advertise 1000Mbps speeds */
385 ctrl1000 |= ( ADVERTISE_1000FULL | ADVERTISE_1000HALF );
386 if ( ( rc = mii_write ( &rtl->mii, MII_CTRL1000, ctrl1000 ) ) != 0 ) {
387 DBGC ( rtl, "REALTEK %p could not write CTRL1000: %s\n",
388 rtl, strerror ( rc ) );
389 return rc;
390 }
391
392 return 0;
393}
394
395/**
396 * Reset PHY
397 *
398 * @v rtl Realtek device
399 * @ret rc Return status code
400 */
401static int realtek_phy_reset ( struct realtek_nic *rtl ) {
402 int rc;
403
404 /* Do nothing if we have no separate PHY register access */
405 if ( ! rtl->have_phy_regs )
406 return 0;
407
408 /* Perform MII reset */
409 if ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) {
410 DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
411 rtl, strerror ( rc ) );
412 return rc;
413 }
414
415 /* Some cards (e.g. RTL8169SC) do not advertise Gigabit by
416 * default. Try to enable advertisement of Gigabit speeds.
417 */
418 if ( ( rc = realtek_phy_speed ( rtl ) ) != 0 ) {
419 /* Ignore failures, since the register may not be
420 * present on non-Gigabit PHYs (e.g. RTL8101).
421 */
422 }
423
424 /* Some cards (e.g. RTL8211B) have a hardware errata that
425 * requires the MII_MMD_DATA register to be cleared before the
426 * link will come up.
427 */
428 if ( ( rc = mii_write ( &rtl->mii, MII_MMD_DATA, 0 ) ) != 0 ) {
429 /* Ignore failures, since the register may not be
430 * present on all PHYs.
431 */
432 }
433
434 /* Restart autonegotiation */
435 if ( ( rc = mii_restart ( &rtl->mii ) ) != 0 ) {
436 DBGC ( rtl, "REALTEK %p could not restart MII: %s\n",
437 rtl, strerror ( rc ) );
438 return rc;
439 }
440
441 return 0;
442}
443
444/******************************************************************************
445 *
446 * Link state
447 *
448 ******************************************************************************
449 */
450
451/**
452 * Check link state
453 *
454 * @v netdev Network device
455 */
456static void realtek_check_link ( struct net_device *netdev ) {
457 struct realtek_nic *rtl = netdev->priv;
458 uint8_t phystatus;
459 uint8_t msr;
460 int link_up;
461
462 /* Determine link state */
463 if ( rtl->have_phy_regs ) {
464 mii_dump ( &rtl->mii );
465 phystatus = readb ( rtl->regs + RTL_PHYSTATUS );
466 link_up = ( phystatus & RTL_PHYSTATUS_LINKSTS );
467 DBGC ( rtl, "REALTEK %p PHY status is %02x (%s%s%s%s%s%s, "
468 "Link%s, %sDuplex)\n", rtl, phystatus,
469 ( ( phystatus & RTL_PHYSTATUS_ENTBI ) ? "TBI" : "GMII" ),
470 ( ( phystatus & RTL_PHYSTATUS_TXFLOW ) ?
471 ", TxFlow" : "" ),
472 ( ( phystatus & RTL_PHYSTATUS_RXFLOW ) ?
473 ", RxFlow" : "" ),
474 ( ( phystatus & RTL_PHYSTATUS_1000MF ) ?
475 ", 1000Mbps" : "" ),
476 ( ( phystatus & RTL_PHYSTATUS_100M ) ?
477 ", 100Mbps" : "" ),
478 ( ( phystatus & RTL_PHYSTATUS_10M ) ?
479 ", 10Mbps" : "" ),
480 ( ( phystatus & RTL_PHYSTATUS_LINKSTS ) ?
481 "Up" : "Down" ),
482 ( ( phystatus & RTL_PHYSTATUS_FULLDUP ) ?
483 "Full" : "Half" ) );
484 } else {
485 msr = readb ( rtl->regs + RTL_MSR );
486 link_up = ( ! ( msr & RTL_MSR_LINKB ) );
487 DBGC ( rtl, "REALTEK %p media status is %02x (Link%s, "
488 "%dMbps%s%s%s%s%s)\n", rtl, msr,
489 ( ( msr & RTL_MSR_LINKB ) ? "Down" : "Up" ),
490 ( ( msr & RTL_MSR_SPEED_10 ) ? 10 : 100 ),
491 ( ( msr & RTL_MSR_TXFCE ) ? ", TxFlow" : "" ),
492 ( ( msr & RTL_MSR_RXFCE ) ? ", RxFlow" : "" ),
493 ( ( msr & RTL_MSR_AUX_STATUS ) ? ", AuxPwr" : "" ),
494 ( ( msr & RTL_MSR_TXPF ) ? ", TxPause" : "" ),
495 ( ( msr & RTL_MSR_RXPF ) ? ", RxPause" : "" ) );
496 }
497
498 /* Report link state */
499 if ( link_up ) {
501 } else {
503 }
504}
505
506/******************************************************************************
507 *
508 * Network device interface
509 *
510 ******************************************************************************
511 */
512
513/**
514 * Create receive buffer (legacy mode)
515 *
516 * @v rtl Realtek device
517 * @ret rc Return status code
518 */
519static int realtek_create_buffer ( struct realtek_nic *rtl ) {
520 struct realtek_rx_buffer *rxbuf = &rtl->rxbuf;
521 size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
522
523 /* Do nothing unless in legacy mode */
524 if ( ! rtl->legacy )
525 return 0;
526
527 /* Allocate buffer */
528 rxbuf->data = dma_alloc ( rtl->dma, &rxbuf->map, len,
530 if ( ! rxbuf->data )
531 return -ENOMEM;
532
533 /* Program buffer address */
534 writel ( dma ( &rxbuf->map, rxbuf->data ), rtl->regs + RTL_RBSTART );
535 DBGC ( rtl, "REALTEK %p receive buffer is at [%08lx,%08lx,%08lx)\n",
536 rtl, virt_to_phys ( rxbuf->data ),
537 ( virt_to_phys ( rxbuf->data ) + RTL_RXBUF_LEN ),
538 ( virt_to_phys ( rxbuf->data ) + len ) );
539
540 return 0;
541}
542
543/**
544 * Destroy receive buffer (legacy mode)
545 *
546 * @v rtl Realtek device
547 */
548static void realtek_destroy_buffer ( struct realtek_nic *rtl ) {
549 struct realtek_rx_buffer *rxbuf = &rtl->rxbuf;
550 size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
551
552 /* Do nothing unless in legacy mode */
553 if ( ! rtl->legacy )
554 return;
555
556 /* Clear buffer address */
557 writel ( 0, rtl->regs + RTL_RBSTART );
558
559 /* Free buffer */
560 dma_free ( &rxbuf->map, rxbuf->data, len );
561 rxbuf->data = NULL;
562 rxbuf->offset = 0;
563}
564
565/**
566 * Create descriptor ring
567 *
568 * @v rtl Realtek device
569 * @v ring Descriptor ring
570 * @ret rc Return status code
571 */
572static int realtek_create_ring ( struct realtek_nic *rtl,
573 struct realtek_ring *ring ) {
575
576 /* Do nothing in legacy mode */
577 if ( rtl->legacy )
578 return 0;
579
580 /* Allocate descriptor ring */
581 ring->desc = dma_alloc ( rtl->dma, &ring->map, ring->len,
583 if ( ! ring->desc )
584 return -ENOMEM;
585
586 /* Initialise descriptor ring */
587 memset ( ring->desc, 0, ring->len );
588
589 /* Program ring address */
590 address = dma ( &ring->map, ring->desc );
591 writel ( ( ( ( uint64_t ) address ) >> 32 ),
592 rtl->regs + ring->reg + 4 );
593 writel ( ( address & 0xffffffffUL ), rtl->regs + ring->reg );
594 DBGC ( rtl, "REALTEK %p ring %02x is at [%08lx,%08lx)\n",
595 rtl, ring->reg, virt_to_phys ( ring->desc ),
596 ( virt_to_phys ( ring->desc ) + ring->len ) );
597
598 return 0;
599}
600
601/**
602 * Destroy descriptor ring
603 *
604 * @v rtl Realtek device
605 * @v ring Descriptor ring
606 */
607static void realtek_destroy_ring ( struct realtek_nic *rtl,
608 struct realtek_ring *ring ) {
609
610 /* Reset producer and consumer counters */
611 ring->prod = 0;
612 ring->cons = 0;
613
614 /* Do nothing more if in legacy mode */
615 if ( rtl->legacy )
616 return;
617
618 /* Clear ring address */
619 writel ( 0, rtl->regs + ring->reg );
620 writel ( 0, rtl->regs + ring->reg + 4 );
621
622 /* Free descriptor ring */
623 dma_free ( &ring->map, ring->desc, ring->len );
624 ring->desc = NULL;
625}
626
627/**
628 * Refill receive descriptor ring
629 *
630 * @v rtl Realtek device
631 */
632static void realtek_refill_rx ( struct realtek_nic *rtl ) {
633 struct realtek_descriptor *rx;
634 struct io_buffer *iobuf;
635 unsigned int rx_idx;
636 int is_last;
637
638 /* Do nothing in legacy mode */
639 if ( rtl->legacy )
640 return;
641
642 while ( ( rtl->rx.prod - rtl->rx.cons ) < RTL_NUM_RX_DESC ) {
643
644 /* Allocate I/O buffer */
645 iobuf = alloc_rx_iob ( RTL_RX_MAX_LEN, rtl->dma );
646 if ( ! iobuf ) {
647 /* Wait for next refill */
648 return;
649 }
650
651 /* Get next receive descriptor */
652 rx_idx = ( rtl->rx.prod++ % RTL_NUM_RX_DESC );
653 is_last = ( rx_idx == ( RTL_NUM_RX_DESC - 1 ) );
654 rx = &rtl->rx.desc[rx_idx];
655
656 /* Populate receive descriptor */
657 rx->address = cpu_to_le64 ( iob_dma ( iobuf ) );
658 rx->length = cpu_to_le16 ( RTL_RX_MAX_LEN );
659 wmb();
660 rx->flags = ( cpu_to_le16 ( RTL_DESC_OWN ) |
661 ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
662 wmb();
663
664 /* Record I/O buffer */
665 assert ( rtl->rx_iobuf[rx_idx] == NULL );
666 rtl->rx_iobuf[rx_idx] = iobuf;
667
668 DBGC2 ( rtl, "REALTEK %p RX %d is [%lx,%lx)\n",
669 rtl, rx_idx, virt_to_phys ( iobuf->data ),
670 ( virt_to_phys ( iobuf->data ) + RTL_RX_MAX_LEN ) );
671 }
672}
673
674/**
675 * Open network device
676 *
677 * @v netdev Network device
678 * @ret rc Return status code
679 */
680static int realtek_open ( struct net_device *netdev ) {
681 struct realtek_nic *rtl = netdev->priv;
682 uint32_t tcr;
683 uint32_t rcr;
684 int rc;
685
686 /* Create transmit descriptor ring */
687 if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
688 goto err_create_tx;
689
690 /* Create receive descriptor ring */
691 if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
692 goto err_create_rx;
693
694 /* Create receive buffer */
695 if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
696 goto err_create_buffer;
697
698 /* Accept all packets */
699 writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
700 writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
701
702 /* Enable transmitter and receiver. RTL8139 requires that
703 * this happens before writing to RCR.
704 */
705 writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
706
707 /* Configure transmitter */
708 tcr = readl ( rtl->regs + RTL_TCR );
709 tcr &= ~RTL_TCR_MXDMA_MASK;
711 writel ( tcr, rtl->regs + RTL_TCR );
712
713 /* Configure receiver */
714 rcr = readl ( rtl->regs + RTL_RCR );
720 writel ( rcr, rtl->regs + RTL_RCR );
721
722 /* Fill receive ring */
723 realtek_refill_rx ( rtl );
724
725 /* Update link state */
727
728 return 0;
729
731 err_create_buffer:
732 realtek_destroy_ring ( rtl, &rtl->rx );
733 err_create_rx:
734 realtek_destroy_ring ( rtl, &rtl->tx );
735 err_create_tx:
736 return rc;
737}
738
739/**
740 * Close network device
741 *
742 * @v netdev Network device
743 */
744static void realtek_close ( struct net_device *netdev ) {
745 struct realtek_nic *rtl = netdev->priv;
746 unsigned int i;
747
748 /* Disable receiver and transmitter */
749 writeb ( 0, rtl->regs + RTL_CR );
750
751 /* Destroy receive buffer */
753
754 /* Destroy receive descriptor ring */
755 realtek_destroy_ring ( rtl, &rtl->rx );
756
757 /* Discard any unused receive buffers */
758 for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
759 if ( rtl->rx_iobuf[i] )
760 free_rx_iob ( rtl->rx_iobuf[i] );
761 rtl->rx_iobuf[i] = NULL;
762 }
763
764 /* Destroy transmit descriptor ring */
765 realtek_destroy_ring ( rtl, &rtl->tx );
766
767 /* Reset legacy transmit descriptor index, if applicable */
768 if ( rtl->legacy )
769 realtek_reset ( rtl );
770}
771
772/**
773 * Transmit packet
774 *
775 * @v netdev Network device
776 * @v iobuf I/O buffer
777 * @ret rc Return status code
778 */
779static int realtek_transmit ( struct net_device *netdev,
780 struct io_buffer *iobuf ) {
781 struct realtek_nic *rtl = netdev->priv;
782 struct realtek_descriptor *tx;
783 unsigned int tx_idx;
784 int is_last;
785 int rc;
786
787 /* Get next transmit descriptor */
788 if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
789 netdev_tx_defer ( netdev, iobuf );
790 return 0;
791 }
792 tx_idx = ( rtl->tx.prod % RTL_NUM_TX_DESC );
793
794 /* Pad and align packet, if needed */
795 if ( rtl->legacy )
796 iob_pad ( iobuf, ETH_ZLEN );
797
798 /* Map I/O buffer */
799 if ( ( rc = iob_map_tx ( iobuf, rtl->dma ) ) != 0 )
800 return rc;
801
802 /* Update producer index */
803 rtl->tx.prod++;
804
805 /* Transmit packet */
806 if ( rtl->legacy ) {
807
808 /* Add to transmit ring */
809 writel ( iob_dma ( iobuf ), rtl->regs + RTL_TSAD ( tx_idx ) );
810 writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
811 rtl->regs + RTL_TSD ( tx_idx ) );
812
813 } else {
814
815 /* Populate transmit descriptor */
816 is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
817 tx = &rtl->tx.desc[tx_idx];
818 tx->address = cpu_to_le64 ( iob_dma ( iobuf ) );
819 tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
820 wmb();
821 tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
822 RTL_DESC_LS ) |
823 ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
824 wmb();
825
826 /* Notify card that there are packets ready to transmit */
827 writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
828 }
829
830 DBGC2 ( rtl, "REALTEK %p TX %d is [%lx,%lx)\n",
831 rtl, tx_idx, virt_to_phys ( iobuf->data ),
832 virt_to_phys ( iobuf->data ) + iob_len ( iobuf ) );
833
834 return 0;
835}
836
837/**
838 * Poll for completed packets
839 *
840 * @v netdev Network device
841 */
842static void realtek_poll_tx ( struct net_device *netdev ) {
843 struct realtek_nic *rtl = netdev->priv;
844 struct realtek_descriptor *tx;
845 unsigned int tx_idx;
846
847 /* Check for completed packets */
848 while ( rtl->tx.cons != rtl->tx.prod ) {
849
850 /* Get next transmit descriptor */
851 tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
852
853 /* Stop if descriptor is still in use */
854 if ( rtl->legacy ) {
855
856 /* Check ownership bit in transmit status register */
857 if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
858 RTL_TSD_OWN ) )
859 return;
860
861 } else {
862
863 /* Check ownership bit in descriptor */
864 tx = &rtl->tx.desc[tx_idx];
865 if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
866 return;
867 }
868
869 DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
870
871 /* Complete TX descriptor */
872 rtl->tx.cons++;
874 }
875}
876
877/**
878 * Poll for received packets (legacy mode)
879 *
880 * @v netdev Network device
881 */
882static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
883 struct realtek_nic *rtl = netdev->priv;
885 struct io_buffer *iobuf;
886 size_t len;
887
888 /* Check for received packets */
889 while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
890
891 /* Extract packet from receive buffer */
892 rx = ( rtl->rxbuf.data + rtl->rxbuf.offset );
893 len = le16_to_cpu ( rx->length );
894 if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
895
896 DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
897 rtl, rtl->rxbuf.offset, len );
898
899 /* Allocate I/O buffer */
900 iobuf = alloc_iob ( len );
901 if ( ! iobuf ) {
903 /* Leave packet for next poll */
904 break;
905 }
906
907 /* Copy data to I/O buffer */
908 memcpy ( iob_put ( iobuf, len ), rx->data, len );
909 iob_unput ( iobuf, 4 /* strip CRC */ );
910
911 /* Hand off to network stack */
912 netdev_rx ( netdev, iobuf );
913
914 } else {
915
916 DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
917 rtl, rtl->rxbuf.offset, len,
918 le16_to_cpu ( rx->status ) );
920 }
921
922 /* Update buffer offset */
923 rtl->rxbuf.offset += ( sizeof ( *rx ) + len );
924 rtl->rxbuf.offset = ( ( rtl->rxbuf.offset + 3 ) & ~3 );
925 rtl->rxbuf.offset = ( rtl->rxbuf.offset % RTL_RXBUF_LEN );
926 writew ( ( rtl->rxbuf.offset - 16 ), rtl->regs + RTL_CAPR );
927
928 /* Give chip time to react before rechecking RTL_CR */
929 readw ( rtl->regs + RTL_CAPR );
930 }
931}
932
933/**
934 * Poll for received packets
935 *
936 * @v netdev Network device
937 */
938static void realtek_poll_rx ( struct net_device *netdev ) {
939 struct realtek_nic *rtl = netdev->priv;
940 struct realtek_descriptor *rx;
941 struct io_buffer *iobuf;
942 unsigned int rx_idx;
943 size_t len;
944
945 /* Poll receive buffer if in legacy mode */
946 if ( rtl->legacy ) {
948 return;
949 }
950
951 /* Check for received packets */
952 while ( rtl->rx.cons != rtl->rx.prod ) {
953
954 /* Get next receive descriptor */
955 rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
956 rx = &rtl->rx.desc[rx_idx];
957
958 /* Stop if descriptor is still in use */
959 if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
960 return;
961
962 /* Populate I/O buffer */
963 iobuf = rtl->rx_iobuf[rx_idx];
964 rtl->rx_iobuf[rx_idx] = NULL;
965 len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
966 iob_put ( iobuf, ( len - 4 /* strip CRC */ ) );
967
968 /* Hand off to network stack */
969 if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
970 DBGC ( rtl, "REALTEK %p RX %d error (length %zd, "
971 "flags %04x)\n", rtl, rx_idx, len,
972 le16_to_cpu ( rx->flags ) );
973 netdev_rx_err ( netdev, iobuf, -EIO );
974 } else {
975 DBGC2 ( rtl, "REALTEK %p RX %d complete (length "
976 "%zd)\n", rtl, rx_idx, len );
977 netdev_rx ( netdev, iobuf );
978 }
979 rtl->rx.cons++;
980 }
981}
982
983/**
984 * Poll for completed and received packets
985 *
986 * @v netdev Network device
987 */
988static void realtek_poll ( struct net_device *netdev ) {
989 struct realtek_nic *rtl = netdev->priv;
991
992 /* Check for and acknowledge interrupts */
993 isr = readw ( rtl->regs + RTL_ISR );
994 if ( ! isr )
995 return;
996 writew ( isr, rtl->regs + RTL_ISR );
997
998 /* Poll for TX completions, if applicable */
999 if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
1001
1002 /* Poll for RX completionsm, if applicable */
1003 if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
1005
1006 /* Check link state, if applicable */
1007 if ( isr & RTL_IRQ_PUN_LINKCHG )
1009
1010 /* Refill RX ring */
1011 realtek_refill_rx ( rtl );
1012}
1013
1014/**
1015 * Enable or disable interrupts
1016 *
1017 * @v netdev Network device
1018 * @v enable Interrupts should be enabled
1019 */
1020static void realtek_irq ( struct net_device *netdev, int enable ) {
1021 struct realtek_nic *rtl = netdev->priv;
1022 uint16_t imr;
1023
1024 /* Set interrupt mask */
1025 imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
1026 RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
1027 writew ( imr, rtl->regs + RTL_IMR );
1028}
1029
1030/** Realtek network device operations */
1032 .open = realtek_open,
1033 .close = realtek_close,
1034 .transmit = realtek_transmit,
1035 .poll = realtek_poll,
1036 .irq = realtek_irq,
1037};
1038
1039/******************************************************************************
1040 *
1041 * PCI interface
1042 *
1043 ******************************************************************************
1044 */
1045
1046/**
1047 * Detect device type
1048 *
1049 * @v rtl Realtek device
1050 */
1051static void realtek_detect ( struct realtek_nic *rtl ) {
1052 uint16_t rms;
1053 uint16_t check_rms;
1054 uint16_t cpcr;
1055 uint16_t check_cpcr;
1056
1057 /* The RX Packet Maximum Size register is present only on
1058 * 8169. Try to set to our intended MTU.
1059 */
1060 rms = RTL_RX_MAX_LEN;
1061 writew ( rms, rtl->regs + RTL_RMS );
1062 check_rms = readw ( rtl->regs + RTL_RMS );
1063
1064 /* The C+ Command register is present only on 8169 and 8139C+.
1065 * Try to enable C+ mode and PCI Dual Address Cycle (for
1066 * 64-bit systems), if supported.
1067 *
1068 * Note that enabling DAC seems to cause bizarre behaviour
1069 * (lockups, garbage data on the wire) on some systems, even
1070 * if only 32-bit addresses are used.
1071 *
1072 * Disable VLAN offload, since some cards seem to have it
1073 * enabled by default.
1074 */
1075 cpcr = readw ( rtl->regs + RTL_CPCR );
1077 if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
1078 cpcr |= RTL_CPCR_DAC;
1079 cpcr &= ~RTL_CPCR_VLAN;
1080 writew ( cpcr, rtl->regs + RTL_CPCR );
1081 check_cpcr = readw ( rtl->regs + RTL_CPCR );
1082
1083 /* Detect device type */
1084 if ( check_rms == rms ) {
1085 DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
1086 rtl->have_phy_regs = 1;
1087 rtl->tppoll = RTL_TPPOLL_8169;
1088 dma_set_mask_64bit ( rtl->dma );
1089 } else {
1090 if ( ( check_cpcr == cpcr ) && ( cpcr != 0xffff ) ) {
1091 DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
1092 rtl );
1094 dma_set_mask_64bit ( rtl->dma );
1095 } else {
1096 DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
1097 rtl );
1098 rtl->legacy = 1;
1099 }
1100 rtl->eeprom.bus = &rtl->spibit.bus;
1101 }
1102}
1103
1104/**
1105 * Probe PCI device
1106 *
1107 * @v pci PCI device
1108 * @ret rc Return status code
1109 */
1110static int realtek_probe ( struct pci_device *pci ) {
1111 struct net_device *netdev;
1112 struct realtek_nic *rtl;
1113 unsigned int i;
1114 int rc;
1115
1116 /* Allocate and initialise net device */
1117 netdev = alloc_etherdev ( sizeof ( *rtl ) );
1118 if ( ! netdev ) {
1119 rc = -ENOMEM;
1120 goto err_alloc;
1121 }
1123 rtl = netdev->priv;
1124 pci_set_drvdata ( pci, netdev );
1125 netdev->dev = &pci->dev;
1126 memset ( rtl, 0, sizeof ( *rtl ) );
1129
1130 /* Fix up PCI device */
1131 adjust_pci_device ( pci );
1132
1133 /* Map registers */
1134 rtl->regs = pci_ioremap ( pci, pci->membase, RTL_BAR_SIZE );
1135 if ( ! rtl->regs ) {
1136 rc = -ENODEV;
1137 goto err_ioremap;
1138 }
1139
1140 /* Configure DMA */
1141 rtl->dma = &pci->dma;
1142
1143 /* Reset the NIC */
1144 if ( ( rc = realtek_reset ( rtl ) ) != 0 )
1145 goto err_reset;
1146
1147 /* Detect device type */
1148 realtek_detect ( rtl );
1149
1150 /* Initialise EEPROM */
1151 if ( rtl->eeprom.bus &&
1152 ( ( rc = realtek_init_eeprom ( netdev ) ) == 0 ) ) {
1153
1154 /* Read MAC address from EEPROM */
1155 if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
1156 netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
1157 DBGC ( rtl, "REALTEK %p could not read MAC address: "
1158 "%s\n", rtl, strerror ( rc ) );
1159 goto err_nvs_read;
1160 }
1161
1162 } else {
1163
1164 /* EEPROM not present. Fall back to reading the
1165 * current ID register value, which will hopefully
1166 * have been programmed by the platform firmware.
1167 */
1168 for ( i = 0 ; i < ETH_ALEN ; i++ )
1169 netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
1170 }
1171
1172 /* Initialise and reset MII interface */
1174 mii_init ( &rtl->mii, &rtl->mdio, 0 );
1175 if ( ( rc = realtek_phy_reset ( rtl ) ) != 0 )
1176 goto err_phy_reset;
1177
1178 /* Register network device */
1179 if ( ( rc = register_netdev ( netdev ) ) != 0 )
1180 goto err_register_netdev;
1181
1182 /* Set initial link state */
1184
1185 /* Register non-volatile options, if applicable */
1186 if ( rtl->nvo.nvs ) {
1187 if ( ( rc = register_nvo ( &rtl->nvo,
1188 netdev_settings ( netdev ) ) ) != 0)
1189 goto err_register_nvo;
1190 }
1191
1192 return 0;
1193
1194 err_register_nvo:
1196 err_register_netdev:
1197 err_phy_reset:
1198 err_nvs_read:
1199 realtek_reset ( rtl );
1200 err_reset:
1201 iounmap ( rtl->regs );
1202 err_ioremap:
1204 netdev_put ( netdev );
1205 err_alloc:
1206 return rc;
1207}
1208
1209/**
1210 * Remove PCI device
1211 *
1212 * @v pci PCI device
1213 */
1214static void realtek_remove ( struct pci_device *pci ) {
1215 struct net_device *netdev = pci_get_drvdata ( pci );
1216 struct realtek_nic *rtl = netdev->priv;
1217
1218 /* Unregister non-volatile options, if applicable */
1219 if ( rtl->nvo.nvs )
1220 unregister_nvo ( &rtl->nvo );
1221
1222 /* Unregister network device */
1224
1225 /* Reset card */
1226 realtek_reset ( rtl );
1227
1228 /* Free network device */
1229 iounmap ( rtl->regs );
1231 netdev_put ( netdev );
1232}
1233
1234/** Realtek PCI device IDs */
1235static struct pci_device_id realtek_nics[] = {
1236 PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
1237 PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
1238 PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
1239 PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
1240 PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
1241 PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
1242 PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
1243 PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
1244 PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
1245 PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
1246 PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
1247 PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
1248 PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
1249 PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
1250 PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
1251 PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
1252 PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
1253 PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
1254 PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
1255 PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
1256 PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
1257 PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
1258 PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
1259 PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
1260 PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
1261 PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
1262 PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
1263 PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
1264 PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
1265 PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
1266 PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
1267};
1268
1269/** Realtek PCI driver */
1270struct pci_driver realtek_driver __pci_driver = {
1271 .ids = realtek_nics,
1272 .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
1275};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long physaddr_t
Definition stdint.h:20
unsigned long long uint64_t
Definition stdint.h:13
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
Bit-bashing interfaces.
ring len
Length.
Definition dwmac.h:226
uint8_t id
Request identifier.
Definition ena.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint64_t address
Base address.
Definition ena.h:13
static int mii_read(int phy_id, int location)
Definition epic100.c:500
Error codes.
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition ethernet.c:265
Ethernet protocol.
static struct net_device * netdev
Definition gdbudp.c:53
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC2(...)
Definition compiler.h:522
#define DBGLVL_IO
Definition compiler.h:322
#define DBG_DISABLE(level)
Definition compiler.h:312
#define DBG_ENABLE(level)
Definition compiler.h:313
#define DBGC(...)
Definition compiler.h:505
#define DBG_LOG
Definition compiler.h:317
#define DBGC_HDA(...)
Definition compiler.h:506
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ETIMEDOUT
Connection timed out.
Definition errno.h:670
#define ENOMEM
Not enough space.
Definition errno.h:535
#define EIO
Input/output error.
Definition errno.h:434
#define ENOTSUP
Operation not supported.
Definition errno.h:590
#define ENODEV
No such device.
Definition errno.h:510
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define ETH_ZLEN
Definition if_ether.h:11
#define ETH_ALEN
Definition if_ether.h:9
#define cpu_to_le64(value)
Definition byteswap.h:109
#define le16_to_cpu(value)
Definition byteswap.h:113
#define cpu_to_le16(value)
Definition byteswap.h:107
#define __attribute__(x)
Definition compiler.h:10
#define wmb()
Definition io.h:546
void iounmap(volatile const void *io_addr)
Unmap I/O address.
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void iob_pad(struct io_buffer *iobuf, size_t min_len)
Pad I/O buffer.
Definition iobpad.c:50
struct io_buffer * alloc_rx_iob(size_t len, struct dma_device *dma)
Allocate and map I/O buffer for receive DMA.
Definition iobuf.c:188
void free_rx_iob(struct io_buffer *iobuf)
Unmap and free I/O buffer for receive DMA.
Definition iobuf.c:215
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition iobuf.c:131
I/O buffers.
#define iob_put(iobuf, len)
Definition iobuf.h:125
static __always_inline int iob_map_tx(struct io_buffer *iobuf, struct dma_device *dma)
Map I/O buffer for transmit DMA.
Definition iobuf.h:244
static __always_inline physaddr_t iob_dma(struct io_buffer *iobuf)
Get I/O buffer DMA address.
Definition iobuf.h:268
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:160
#define iob_unput(iobuf, len)
Definition iobuf.h:140
DMA mappings.
static __always_inline void dma_set_mask_64bit(struct dma_device *dma)
Set 64-bit addressable space mask.
Definition dma.h:467
void dma_free(struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer.
void * dma_alloc(struct dma_device *dma, struct dma_mapping *map, size_t len, size_t align)
Allocate and map DMA-coherent buffer.
physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
Media Independent Interface.
static int mii_write(struct mii_device *mii, unsigned int reg, unsigned int data)
Write to MII register.
Definition mii.h:105
static void mdio_init(struct mii_interface *mdio, struct mii_operations *op)
Initialise MII interface.
Definition mii.h:64
static void mii_init(struct mii_device *mii, struct mii_interface *mdio, unsigned int address)
Initialise MII device.
Definition mii.h:76
static void mii_dump(struct mii_device *mii)
Dump MII registers (for debugging)
Definition mii.h:117
uint8_t unused
Unused.
Definition librm.h:5
Dynamic memory allocation.
int mii_reset(struct mii_device *mii)
Reset MII device.
Definition mii.c:75
int mii_restart(struct mii_device *mii)
Restart autonegotiation.
Definition mii.c:44
#define MII_MMD_DATA
Definition mii.h:28
#define ADVERTISE_1000HALF
Definition mii.h:135
#define MII_CTRL1000
Definition mii.h:25
#define ADVERTISE_1000FULL
Definition mii.h:134
static unsigned int unsigned int reg
Definition myson.h:162
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition netdevice.c:231
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition netdevice.c:549
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition netdevice.c:942
void netdev_tx_defer(struct net_device *netdev, struct io_buffer *iobuf)
Defer transmitted packet.
Definition netdevice.c:413
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition netdevice.c:587
int register_netdev(struct net_device *netdev)
Register network device.
Definition netdevice.c:760
Network device management.
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition netdevice.h:789
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition netdevice.h:519
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition netdevice.h:532
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition netdevice.h:576
static void netdev_tx_complete_next(struct net_device *netdev)
Complete network transmission.
Definition netdevice.h:779
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition netdevice.h:587
void nvo_init(struct nvo_block *nvo, struct nvs_device *nvs, size_t address, size_t len, int(*resize)(struct nvo_block *nvo, size_t len), struct refcnt *refcnt)
Initialise non-volatile stored options.
Definition nvo.c:274
void unregister_nvo(struct nvo_block *nvo)
Unregister non-volatile stored options.
Definition nvo.c:325
int register_nvo(struct nvo_block *nvo, struct settings *parent)
Register non-volatile stored options.
Definition nvo.c:294
int nvs_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read from non-volatile storage device.
Definition nvs.c:76
Non-volatile storage.
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition pci.c:241
PCI bus.
#define __pci_driver
Declare a PCI driver.
Definition pci.h:278
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition pci.h:366
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition pci.h:308
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition pci.h:376
static struct net_device_operations realtek_operations
Realtek network device operations.
Definition realtek.c:1031
static void realtek_destroy_buffer(struct realtek_nic *rtl)
Destroy receive buffer (legacy mode)
Definition realtek.c:548
static void realtek_spi_close_bit(struct bit_basher *basher)
Close bit-bashing interface.
Definition realtek.c:118
static int realtek_create_ring(struct realtek_nic *rtl, struct realtek_ring *ring)
Create descriptor ring.
Definition realtek.c:572
static void realtek_poll_tx(struct net_device *netdev)
Poll for completed packets.
Definition realtek.c:842
static void realtek_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition realtek.c:988
static struct pci_device_id realtek_nics[]
Realtek PCI device IDs.
Definition realtek.c:1235
static int realtek_spi_read_bit(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition realtek.c:135
static int realtek_phy_speed(struct realtek_nic *rtl)
Configure PHY for Gigabit operation.
Definition realtek.c:371
static void realtek_refill_rx(struct realtek_nic *rtl)
Refill receive descriptor ring.
Definition realtek.c:632
static int realtek_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition realtek.c:779
static int realtek_init_eeprom(struct net_device *netdev)
Initialise EEPROM.
Definition realtek.c:185
static void realtek_check_link(struct net_device *netdev)
Check link state.
Definition realtek.c:456
static void realtek_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition realtek.c:1020
static void realtek_legacy_poll_rx(struct net_device *netdev)
Poll for received packets (legacy mode)
Definition realtek.c:882
static void realtek_spi_write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition realtek.c:155
static void realtek_spi_open_bit(struct bit_basher *basher)
Open bit-bashing interface.
Definition realtek.c:104
static void realtek_destroy_ring(struct realtek_nic *rtl, struct realtek_ring *ring)
Destroy descriptor ring.
Definition realtek.c:607
static void realtek_dump(struct realtek_nic *rtl)
Dump all registers (for debugging)
Definition realtek.c:69
static int realtek_create_buffer(struct realtek_nic *rtl)
Create receive buffer (legacy mode)
Definition realtek.c:519
static int realtek_phy_reset(struct realtek_nic *rtl)
Reset PHY.
Definition realtek.c:401
static int realtek_mii_read(struct mii_interface *mdio, unsigned int phy __unused, unsigned int reg)
Read from MII register.
Definition realtek.c:252
static int realtek_open(struct net_device *netdev)
Open network device.
Definition realtek.c:680
static int realtek_probe(struct pci_device *pci)
Probe PCI device.
Definition realtek.c:1110
static void realtek_close(struct net_device *netdev)
Close network device.
Definition realtek.c:744
static int realtek_mii_write(struct mii_interface *mdio, unsigned int phy __unused, unsigned int reg, unsigned int data)
Write to MII register.
Definition realtek.c:293
static void realtek_remove(struct pci_device *pci)
Remove PCI device.
Definition realtek.c:1214
static const uint8_t realtek_eeprom_bits[]
Pin mapping for SPI bit-bashing interface.
Definition realtek.c:92
static struct mii_operations realtek_mii_operations
Realtek MII operations.
Definition realtek.c:325
static struct bit_basher_operations realtek_basher_ops
SPI bit-bashing interface.
Definition realtek.c:172
static int realtek_reset(struct realtek_nic *rtl)
Reset hardware.
Definition realtek.c:343
static void realtek_detect(struct realtek_nic *rtl)
Detect device type.
Definition realtek.c:1051
static void realtek_poll_rx(struct net_device *netdev)
Poll for received packets.
Definition realtek.c:938
Realtek 10/100/1000 network card driver.
#define RTL_TPPOLL_8139CP
Transmit Priority Polling Register (byte, 8139C+ only)
Definition realtek.h:225
#define RTL_PHYAR
PHY Access Register (dword, 8169 only)
Definition realtek.h:201
#define RTL_PHYSTATUS_FULLDUP
Full duplex.
Definition realtek.h:222
#define RTL_NUM_TX_DESC
Number of transmit descriptors.
Definition realtek.h:94
#define RTL_IRQ_ROK
Receive OK.
Definition realtek.h:131
#define RTL_BAR_SIZE
PCI memory BAR size.
Definition realtek.h:19
#define RTL_RCR_WRAP
Overrun receive buffer.
Definition realtek.h:154
#define RTL_DESC_SIZE_MASK
Descriptor buffer size mask.
Definition realtek.h:34
#define RTL_MSR
Media Status Register (byte, 8139 only)
Definition realtek.h:191
#define RTL_RCR_AAP
Accept all packets.
Definition realtek.h:159
#define RTL_PHYSTATUS_10M
10Mbps
Definition realtek.h:220
#define RTL_RCR_RBLEN_MASK
Definition realtek.h:149
#define RTL_RESET_MAX_WAIT_MS
Maximum time to wait for a reset, in milliseconds.
Definition realtek.h:116
#define RTL_RCR_MXDMA_DEFAULT
Definition realtek.h:153
#define RTL_MSR_TXFCE
TX flow control enabled.
Definition realtek.h:192
#define RTL_EEPROM_ID_MAGIC
EEPROM code word magic value.
Definition realtek.h:175
#define RTL_TSAD(n)
Transmit Start Address of Descriptor N (dword, 8139 only)
Definition realtek.h:85
#define RTL_CR_RST
Reset.
Definition realtek.h:110
#define RTL_RCR_MXDMA_MASK
Definition realtek.h:152
#define RTL_RXBUF_LEN
Receive buffer length.
Definition realtek.h:100
#define RTL_RCR_RXFTH_MASK
Definition realtek.h:146
#define RTL_CPCR_CPRX
C+ receive enable.
Definition realtek.h:235
#define RTL_CPCR_CPTX
C+ transmit enable.
Definition realtek.h:236
static void realtek_init_ring(struct realtek_ring *ring, unsigned int count, unsigned int reg)
Initialise descriptor ring.
Definition realtek.h:273
#define RTL_TSD_OWN
Ownership.
Definition realtek.h:82
#define RTL_MSR_RXFCE
RX flow control enabled.
Definition realtek.h:193
#define RTL_PHYSTATUS_ENTBI
TBI / GMII mode.
Definition realtek.h:215
#define RTL_IRQ_PUN_LINKCHG
Packet underrun / link change.
Definition realtek.h:127
#define RTL_PHYSTATUS_LINKSTS
Link ok.
Definition realtek.h:221
#define RTL_RCR_APM
Accept physical match.
Definition realtek.h:158
#define RTL_MSR_RXPF
RX pause flag.
Definition realtek.h:198
#define RTL_RDSAR
Receive Descriptor Start Address Register (qword)
Definition realtek.h:239
#define RTL_CPCR_DAC
PCI Dual Address Cycle enable.
Definition realtek.h:233
#define RTL_RING_ALIGN
Descriptor ring alignment.
Definition realtek.h:51
#define RTL_PHYSTATUS_100M
100Mbps
Definition realtek.h:219
#define RTL_CONFIG1_VPD
Vital Product Data enabled.
Definition realtek.h:188
#define RTL_IRQ_TER
Transmit error.
Definition realtek.h:128
#define RTL_9346CR_EEM_EEPROM
EEPROM mode.
Definition realtek.h:164
@ RTL_DESC_OWN
Descriptor is owned by NIC.
Definition realtek.h:39
@ RTL_DESC_RES
Receive error summary.
Definition realtek.h:47
@ RTL_DESC_LS
Last segment descriptor.
Definition realtek.h:45
@ RTL_DESC_EOR
End of descriptor ring.
Definition realtek.h:41
@ RTL_DESC_FS
First segment descriptor.
Definition realtek.h:43
#define RTL_TSD(n)
Transmit Status of Descriptor N (dword, 8139 only)
Definition realtek.h:79
#define RTL_PHYAR_FLAG
Read/write flag.
Definition realtek.h:202
#define RTL_9346CR_EEDO
Data out.
Definition realtek.h:169
#define RTL_9346CR_EEDI
Data in.
Definition realtek.h:168
#define RTL_PHYSTATUS_TXFLOW
TX flow control enabled.
Definition realtek.h:216
#define RTL_CPCR
C+ Command Register (word)
Definition realtek.h:231
#define RTL_EEPROM_VPD
Word offset of VPD / non-volatile options within EEPROM.
Definition realtek.h:181
#define RTL_MII_MAX_WAIT_US
Maximum time to wait for PHY access, in microseconds.
Definition realtek.h:211
#define RTL_TCR_MXDMA_DEFAULT
Definition realtek.h:140
#define RTL_EEPROM_ID
Word offset of ID code word within EEPROM.
Definition realtek.h:172
#define RTL_RCR_RXFTH_DEFAULT
Definition realtek.h:147
#define RTL_RX_MAX_LEN
Receive buffer length.
Definition realtek.h:245
#define RTL_MSR_LINKB
Inverse of link status.
Definition realtek.h:196
#define RTL_RMS
RX Packet Maximum Size Register (word)
Definition realtek.h:228
#define RTL_MSR_SPEED_10
10Mbps
Definition realtek.h:195
#define RTL_PHYSTATUS_RXFLOW
RX flow control enabled.
Definition realtek.h:217
#define RTL_NUM_RX_DESC
Number of receive descriptors.
Definition realtek.h:242
#define RTL_RXBUF_ALIGN
Receive buffer alignment.
Definition realtek.h:106
#define RTL_CR
Command Register (byte)
Definition realtek.h:109
#define RTL_CAPR
Current Address of Packet Read (word, 8139 only)
Definition realtek.h:119
#define RTL_RCR_AM
Accept multicast packets.
Definition realtek.h:157
#define RTL_9346CR_EEM_NORMAL
Normal mode.
Definition realtek.h:165
#define RTL_IRQ_RER
Receive error.
Definition realtek.h:130
#define RTL_TCR_MXDMA_MASK
Definition realtek.h:139
#define RTL_CR_RE
Receiver Enable.
Definition realtek.h:111
#define RTL_RXBUF_PAD
Receive buffer padding.
Definition realtek.h:103
#define RTL_IMR
Interrupt Mask Register (word)
Definition realtek.h:126
#define RTL_MAR0
Multicast Register 0 (dword)
Definition realtek.h:73
@ RTL_STAT_ROK
Received OK.
Definition realtek.h:66
#define RTL_CONFIG1
Configuration Register 1 (byte)
Definition realtek.h:187
#define RTL_IDR0
ID Register 0 (6 bytes)
Definition realtek.h:70
#define RTL_9346CR
93C46 (93C56) Command Register (byte)
Definition realtek.h:162
#define RTL_CPCR_VLAN
VLAN tag stripping enable.
Definition realtek.h:232
#define RTL_RCR_RBLEN_DEFAULT
Definition realtek.h:150
#define RTL_CR_BUFE
Receive buffer empty.
Definition realtek.h:113
#define RTL_RCR_STOP_WORKING
Here be dragons.
Definition realtek.h:144
#define RTL_MSR_TXPF
TX pause flag.
Definition realtek.h:197
#define RTL_RCR_9356SEL
EEPROM is a 93C56.
Definition realtek.h:155
#define RTL_CR_TE
Transmit Enable.
Definition realtek.h:112
#define RTL_TNPDS
Transmit Normal Priority Descriptors (qword)
Definition realtek.h:88
#define RTL_IRQ_TOK
Transmit OK.
Definition realtek.h:129
#define RTL_MSR_AUX_STATUS
Aux power present.
Definition realtek.h:194
#define RTL_RCR_AB
Accept broadcast packets.
Definition realtek.h:156
#define RTL_TPPOLL_NPQ
Normal Priority Queue Polling.
Definition realtek.h:123
#define RTL_PHYSTATUS_1000MF
1000Mbps full-duplex
Definition realtek.h:218
#define RTL_TCR
Transmit (Tx) Configuration Register (dword)
Definition realtek.h:137
#define RTL_TSD_ERTXTH_DEFAULT
Definition realtek.h:81
#define RTL_EEPROM_MAC
Word offset of MAC address within EEPROM.
Definition realtek.h:178
#define RTL_MAR4
Multicast Register 4 (dword)
Definition realtek.h:76
#define RTL_PHYAR_DATA(value)
Extract PHY Access Register data.
Definition realtek.h:208
#define RTL_PHYAR_VALUE(flag, reg, data)
Construct PHY Access Register value.
Definition realtek.h:205
#define RTL_RCR
Receive (Rx) Configuration Register (dword)
Definition realtek.h:143
#define RTL_TPPOLL_8169
Transmit Priority Polling Register (byte, 8169 only)
Definition realtek.h:122
#define RTL_CPCR_MULRW
PCI Multiple Read/Write enable.
Definition realtek.h:234
#define RTL_PHYSTATUS
PHY (GMII, MII, or TBI) Status Register (byte, 8169 only)
Definition realtek.h:214
#define RTL_RBSTART
Receive Buffer Start Address (dword, 8139 only)
Definition realtek.h:97
#define RTL_9346CR_EECS
Chip select.
Definition realtek.h:166
#define RTL_9346CR_EESK
Clock.
Definition realtek.h:167
#define RTL_ISR
Interrupt Status Register (word)
Definition realtek.h:134
#define RTL_EEPROM_VPD_LEN
Length of VPD / non-volatile options within EEPROM.
Definition realtek.h:184
struct i386_regs regs
Definition registers.h:1
@ isr
Definition sis900.h:26
@ imr
Definition sis900.h:27
#define SPI_MODE_THREEWIRE
Threewire-compatible mode.
Definition spi.h:200
void init_spi_bit_basher(struct spi_bit_basher *spibit)
Initialise SPI bit-bashing interface.
Definition spi_bit.c:236
@ SPI_BIT_MOSI
Master Out Slave In.
Definition spi_bit.h:38
@ SPI_BIT_MISO
Master In Slave Out.
Definition spi_bit.h:40
@ SPI_BIT_SCLK
Serial clock.
Definition spi_bit.h:36
#define SPI_BIT_SS(slave)
Determine bit index for a particular slave.
Definition spi_bit.h:51
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
Bit-bashing operations.
Definition bitbash.h:16
A bit-bashing interface.
Definition bitbash.h:56
struct bit_basher_operations * op
Bit-bashing operations.
Definition bitbash.h:58
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
An MII interface.
Definition mii.h:44
MII interface operations.
Definition mii.h:19
Network device operations.
Definition netdevice.h:214
A network device.
Definition netdevice.h:353
struct nvs_device * nvs
Underlying non-volatile storage device.
Definition nvo.h:27
A PCI device ID list entry.
Definition pci.h:175
A PCI device.
Definition pci.h:211
unsigned long membase
Memory base.
Definition pci.h:220
struct device dev
Generic device.
Definition pci.h:213
struct dma_device dma
DMA device.
Definition pci.h:215
A PCI driver.
Definition pci.h:252
int(* probe)(struct pci_device *pci)
Probe device.
Definition pci.h:265
A packet descriptor.
Definition realtek.h:22
A legacy mode receive packet header.
Definition realtek.h:54
A Realtek network card.
Definition realtek.h:290
struct realtek_rx_buffer rxbuf
Receive buffer (legacy mode)
Definition realtek.h:320
int have_phy_regs
PHYAR and PHYSTATUS registers are present.
Definition realtek.h:309
int legacy
Legacy datapath mode.
Definition realtek.h:307
struct io_buffer * rx_iobuf[RTL_NUM_RX_DESC]
Receive I/O buffers.
Definition realtek.h:318
struct mii_device mii
MII device.
Definition realtek.h:304
struct nvo_block nvo
Non-volatile options.
Definition realtek.h:300
struct spi_device eeprom
EEPROM.
Definition realtek.h:298
struct dma_device * dma
DMA device.
Definition realtek.h:294
unsigned int tppoll
TPPoll register offset.
Definition realtek.h:311
struct spi_bit_basher spibit
SPI bit-bashing interface.
Definition realtek.h:296
struct realtek_ring tx
Transmit descriptor ring.
Definition realtek.h:314
struct realtek_ring rx
Receive descriptor ring.
Definition realtek.h:316
void * regs
Registers.
Definition realtek.h:292
struct mii_interface mdio
MII interface.
Definition realtek.h:302
A Realtek descriptor ring.
Definition realtek.h:249
unsigned int prod
Producer index.
Definition realtek.h:255
unsigned int cons
Consumer index.
Definition realtek.h:257
size_t len
Length (in bytes)
Definition realtek.h:262
struct dma_mapping map
Descriptor ring DMA mapping.
Definition realtek.h:253
unsigned int reg
Descriptor start address register.
Definition realtek.h:260
struct realtek_descriptor * desc
Descriptors.
Definition realtek.h:251
Receive buffer (legacy mode *)
Definition realtek.h:280
struct dma_mapping map
Buffer DMA mapping.
Definition realtek.h:284
void * data
Buffer.
Definition realtek.h:282
unsigned int offset
Offset within buffer.
Definition realtek.h:286
struct spi_bus bus
SPI bus.
Definition spi_bit.h:19
struct bit_basher basher
Bit-bashing interface.
Definition spi_bit.h:21
unsigned int mode
SPI interface mode.
Definition spi.h:137
struct nvs_device nvs
NVS device.
Definition spi.h:89
struct spi_bus * bus
SPI bus to which device is attached.
Definition spi.h:91
Three-wire serial interface.
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition timer.c:61
#define readl
Definition w89c840.c:157
#define writel
Definition w89c840.c:160
#define writew
Definition w89c840.c:159
#define readb
Definition w89c840.c:155
#define writeb
Definition w89c840.c:158
#define readw
Definition w89c840.c:156
u8 tx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets to the AP.
Definition wpa.h:4
u8 rx[WPA_TKIP_MIC_KEY_LEN]
MIC key for packets from the AP.
Definition wpa.h:1
static struct xen_remove_from_physmap * remove
Definition xenmem.h:40