Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 07 May 2006 00:38:06 +0400
From:      Igor Kovalenko <garrison@mail.ru>
To:        Igor Kovalenko <garrison@mail.ru>,  freebsd-emulation@freebsd.org,  freebsd-current@freebsd.org, Gleb Smirnoff <glebius@freebsd.org>,  nox@jelal.kn-bremen.de
Subject:   Re: playing with qemu's 8139 nic and FreeBSD (loopback mode missing?
Message-ID:  <445D092E.2040501@mail.ru>
In-Reply-To: <20060506152059.GA33481@saturn.kn-bremen.de>
References:  <20060427203718.GA15953@saturn.kn-bremen.de> <445241DE.9020909@mail.ru> <20060428221142.GA11504@saturn.kn-bremen.de> <44530C50.6040902@mail.ru> <20060430004646.GA70632@saturn.kn-bremen.de> <4458277F.4010902@mail.ru> <20060506152059.GA33481@saturn.kn-bremen.de>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------090201030807060400040102
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi Juergen,

Juergen Lock wrote:
> [Cc'ing glebius@ because he did most of the recent re(4) commits, and
> -current in case possible other driver developers didn't see this thread
> in -emulation]
> 
> On Wed, May 03, 2006 at 07:46:07AM +0400, Igor Kovalenko wrote:
>> Juergen Lock wrote:
>>> On Sat, Apr 29, 2006 at 10:48:48AM +0400, Igor Kovalenko wrote:
>>>> Juergen Lock wrote:
>>>>> On Fri, Apr 28, 2006 at 08:25:02PM +0400, Igor Kovalenko wrote:
>>>>>> Juergen Lock wrote:
>>>>>>> I played with
>>>>>>> 	qemu -monitor stdio -m 256 -cdrom 6.1-RC1-i386-disc1.iso -usb -soundhw es1370 -kernel-kqemu -net nic,model=rtl8139 -net user
>>>>>>> and got it as far as
>>>>>>> 	re0: diagnostic failed, failed to receive packet in loopback mode
>>>>>>> (followed by a panic :) with the (experimental) patches below.
>>>>>>>
>>>>>>>  Anyone in the mood to implement loopback mode for this nic?
>>>>>>>
>>>>>>>  Hmm actually...  I just found the original posting in the archive,
>>>>>>> is C+ mode implemented now?  If not re is probably not what I want,
>>>>>> The rtl8139 is set up with PCI rev ID 0x20 which should be enough for OS driver
>>>>>> to detect C+ mode features. C+ mode is OK, tested with Linux driver.
>>>>> Cool, so I want FreeBSD's re driver.  That one checks TxConfig
>>>>> tho, as changed in my patch (inside #if 0).  And when changed,
>>>>> it still doesn't work as mentioned above because the driver expects
>>>>> loopback mode to be working.
>>>>>>> but the rl driver that it attaches without that #if 0'd (now) hunk
>>>>>>> below doesnt seem to be able to get data thru either and I get
>>>>>>> 	rl0: watchdog timeout
>>>>>>> in dmesg, which usually means the driver doesnt receive interrupts.
>>>>>>>
>>>>>>>  What the heck, I'll append a log of a run just doing in fixit->cdrom:
>>>>>>> 	ifconfig rl0 10.0.2.15
>>>>>>> and then exiting (which is enough to trigger the watchdog timeout...)
>>>>>>>
>>>>>> I'm too lasy to test with fresh freebsd installation :)
>>>>>  No need to install FreeBSD, you can get away by just using
>>>>> fixit mode of an install iso, i.e. disc1.  (which actually is
>>>>> what I did above. :)
>>>>>
>>>>>  You can look at 6.1RC's re driver here:
>>>>> http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/dev/re/if_re.c?annotate=1.46.2.14
>>>>>  which includes:
>>>>> http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/pci/if_rlreg.h?annotate=1.51.2.3
>>>>>
>>>>>  And 6.1RC disc1 iso is e.g. here:
>>>>> ftp://ftp.ru.freebsd.org:/pub/FreeBSD/ISO-IMAGES-i386/6.1/6.1-RC1-i386-disc1.iso
>>>>>
>>>>>
>>>> Thanks, that iso pointer made it.
>>>>
>>>  :)
>>>
>>>> Please try the following on top of your patch, at least ping should now work:
>>>  Thanks, that seems to get the rl driver going.  Now to fix C+ mode
>>> (re driver) change the #if 0 in my patch to #if 1...
>>>
>>>
>> I believe freebsd re driver is somewhat broken, e.g. it does not follow documented
>> procedure to detect hardware features (e.g. 8139 c+ mode)
> 
> Hmm, a bit of googling didn't reveal docs about this, do you have
> a pointer?  (I only found the data sheet at
> 	http://people.freebsd.org/~wpaul/RealTek/spec-8139cp(150).pdf
> which doesnt seem to mention detecting c+ hardware specifically)

Well, I might have misread some docs; please do not consider this as an assault :)
I remember PCI rev id >= 0x20 is C+ mode for realtek 8139.

> 
>>  and in tries to use 8169
>> registers (e.g. 0xda) on 8139 hardware etc.
> 
>  Oh, then it must have mis-detected the nic as a 8169 because
> the code in question reads:
> 
>         if (sc->rl_type == RL_8169)
>                 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
>> Therefore some action from driver people is needed; I can provide a patch which
>> enables board timer in rtl8139 emulation (and thus enables hardware timeout
>> events) if you need it.
> 
>  Yeah I guess that would be useful...
> 
> 

Please find rtl8139.c.freebsd.timer.diff attached hereto.
The diff contains part of your changes to chip identification so re driver
does see it as C+ mode chip. Timer support is added (and there is no kpanic now),
though the specs require it to be clocked with PCI bus; that is somewhat hard
to achieve. Common qemu timer is of much lower resolution; doing it differently
will require some effort.

-- 
Kind regards,
Igor V. Kovalenko

--------------090201030807060400040102
Content-Type: text/x-patch;
 name="rtl8139.c.freebsd.timer.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="rtl8139.c.freebsd.timer.diff"

Index: rtl8139.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/rtl8139.c,v
retrieving revision 1.1
diff -u -r1.1 rtl8139.c
--- rtl8139.c	5 Feb 2006 04:14:41 -0000	1.1
+++ rtl8139.c	6 May 2006 20:27:02 -0000
@@ -32,6 +32,8 @@
 /* debug RTL8139 card */
 //#define DEBUG_RTL8139 1
 
+#define PCI_FREQUENCY 33000000L
+
 /* debug RTL8139 card C+ mode only */
 //#define DEBUG_RTL8139CP 1
 
@@ -315,6 +317,11 @@
     (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
 #define HW_REVID_MASK    HW_REVID(1, 1, 1, 1, 1, 1, 1)
 
+#define RTL8139_PCI_REVID_8139      0x10
+#define RTL8139_PCI_REVID_8139CPLUS 0x20
+
+#define RTL8139_PCI_REVID           RTL8139_PCI_REVID_8139CPLUS
+
 /* Size is 64 * 16bit words */
 #define EEPROM_9346_ADDR_BITS 6
 #define EEPROM_9346_SIZE  (1 << EEPROM_9346_ADDR_BITS)
@@ -414,7 +421,13 @@
     uint32_t   RxRingAddrHI;
 
     EEprom9346 eeprom;
-    
+
+	uint32_t   TCTR;
+	uint32_t   TimerInt;
+	int64_t    TCTR_base;
+
+	QEMUTimer *timer;
+
 } RTL8139State;
 
 void prom9346_decode_command(EEprom9346 *eeprom, uint8_t command)
@@ -512,6 +525,19 @@
             eeprom->output <<= 1;
             if (eeprom->tick == 16)
             {
+#if 1
+		// the FreeBSD drivers (rl and re) don't explicitly toggle
+		// CS between reads (or does setting Cfg9346 to 0 count too?),
+		// so we need to enter wait-for-command state here
+                eeprom->mode = Chip9346_enter_command_mode;
+                eeprom->input = 0;
+                eeprom->tick = 0;
+
+#if defined(DEBUG_RTL8139)
+                printf("eeprom: +++ end of read, awaiting next command\n");
+#endif
+#else
+		// original behaviour
                 ++eeprom->address;
                 eeprom->address &= EEPROM_9346_ADDR_MASK;
                 eeprom->output = eeprom->contents[eeprom->address];
@@ -521,6 +547,7 @@
                 printf("eeprom: +++ read next address 0x%02x data=0x%04x\n",
                        eeprom->address, eeprom->output);
 #endif
+#endif
             }
             break;
 
@@ -751,7 +778,7 @@
     }
 }
 
-static void rtl8139_receive(void *opaque, const uint8_t *buf, int size)
+static void rtl8139_do_receive(void *opaque, const uint8_t *buf, int size, int do_interrupt)
 {
     RTL8139State *s = opaque;
 
@@ -1078,7 +1105,16 @@
     }
 
     s->IntrStatus |= RxOK;
-    rtl8139_update_irq(s);
+
+	if (do_interrupt)
+	{
+		rtl8139_update_irq(s);
+	}
+}
+
+static void rtl8139_receive(void *opaque, const uint8_t *buf, int size)
+{
+	rtl8139_do_receive(opaque, buf, size, 1);
 }
 
 static void rtl8139_reset_rxring(RTL8139State *s, uint32_t bufferSize)
@@ -1103,6 +1139,11 @@
 
     /* prepare eeprom */
     s->eeprom.contents[0] = 0x8129;
+#if 1
+    // PCI vendor and device ID should be mirrored here
+    s->eeprom.contents[1] = 0x10ec;
+    s->eeprom.contents[2] = 0x8139;
+#endif
     memcpy(&s->eeprom.contents[7], s->macaddr, 6);
 
     /* mark all status registers as owned by host */
@@ -1129,7 +1170,7 @@
 //    s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139  HasHltClk
     s->clock_enabled = 0;
 #else
-    s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 0, 0); // RTL-8139C HasLWake
+    s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake
     s->clock_enabled = 1;
 #endif
 
@@ -1157,6 +1198,11 @@
     s->NWayAdvert    = 0x05e1; /* all modes, full duplex */
     s->NWayLPAR      = 0x05e1; /* all modes, full duplex */
     s->NWayExpansion = 0x0001; /* autonegotiation supported */
+
+	/* also reset timer and disable timer interrupt */
+	s->TCTR = 0;
+	s->TimerInt = 0;
+	s->TCTR_base = 0;
 }
 
 static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val)
@@ -1622,12 +1668,22 @@
 #endif
     cpu_physical_memory_read(s->TxAddr[descriptor], txbuffer, txsize);
 
-    qemu_send_packet(s->vc, txbuffer, txsize);
-
     /* Mark descriptor as transferred */
     s->TxStatus[descriptor] |= TxHostOwns;
     s->TxStatus[descriptor] |= TxStatOK;
 
+	if (TxLoopBack == (s->TxConfig & TxLoopBack))
+	{
+#ifdef DEBUG_RTL8139
+		printf("RTL8139: +++ transmit loopback mode\n");
+#endif
+		rtl8139_do_receive(s, txbuffer, txsize, 0);
+	}
+	else
+	{
+		qemu_send_packet(s->vc, txbuffer, txsize);
+	}
+
 #ifdef DEBUG_RTL8139
     printf("RTL8139: +++ transmitted %d bytes from descriptor %d\n", txsize, descriptor);
 #endif
@@ -1748,9 +1804,6 @@
 #endif
     cpu_physical_memory_read(tx_addr, txbuffer, txsize);
 
-    /* transmit the packet */
-    qemu_send_packet(s->vc, txbuffer, txsize);
-
     /* transfer ownership to target */
     txdw0 &= ~CP_RX_OWN;
 
@@ -1777,6 +1830,19 @@
         ++s->currCPlusTxDesc;
     }
 
+	if (TxLoopBack == (s->TxConfig & TxLoopBack))
+	{
+#ifdef DEBUG_RTL8139
+		printf("RTL8139: +++ C+ transmit loopback mode\n");
+#endif
+		rtl8139_receive(s, txbuffer, txsize);
+	}
+	else
+	{
+		/* transmit the packet */
+		qemu_send_packet(s->vc, txbuffer, txsize);
+	}
+
 #ifdef DEBUG_RTL8139
     printf("RTL8139: +++ C+ mode transmitted %d bytes from descriptor %d\n", txsize, descriptor);
 #endif
@@ -1909,6 +1975,8 @@
 #endif
 
     s->TxAddr[txAddrOffset/4] = le32_to_cpu(val);
+
+	s->currCPlusTxDesc = 0;
 }
 
 static uint32_t rtl8139_TxAddr_read(RTL8139State *s, uint32_t txAddrOffset)
@@ -1949,6 +2017,18 @@
     return ret;
 }
 
+static uint32_t rtl8139_RxBufAddr_read(RTL8139State *s)
+{
+    /* this value is NOT off by 16 */
+    uint32_t ret = s->RxBufAddr;
+
+#ifdef DEBUG_RTL8139
+    printf("RTL8139: RxBufAddr read val=0x%04x\n", ret);
+#endif
+
+    return ret;
+}
+
 static void rtl8139_RxBuf_write(RTL8139State *s, uint32_t val)
 {
 #ifdef DEBUG_RTL8139
@@ -2281,6 +2361,21 @@
             s->RxRingAddrHI = val;
             break;
 
+		case Timer:
+#ifdef DEBUG_RTL8139
+			printf("RTL8139: TCTR Timer reset on write\n");
+#endif
+			s->TCTR = 0;
+			s->TCTR_base = qemu_get_clock(vm_clock);
+			break;
+
+		case FlashReg:
+#ifdef DEBUG_RTL8139
+			printf("RTL8139: FlashReg TimerInt write val=0x%08x\n", val);
+#endif
+			s->TimerInt = val;
+			break;
+
         default:
 #ifdef DEBUG_RTL8139
             printf("RTL8139: ioport write(l) addr=0x%x val=0x%08x via write(b)\n", addr, val);
@@ -2355,7 +2450,7 @@
             break;
 
         case PCIRevisionID:
-            ret = 0x10;
+            ret = RTL8139_PCI_REVID;
 #ifdef DEBUG_RTL8139
             printf("RTL8139: PCI Revision ID read 0x%x\n", ret);
 #endif
@@ -2411,6 +2506,10 @@
             ret = rtl8139_RxBufPtr_read(s);
             break;
 
+        case RxBufAddr:
+            ret = rtl8139_RxBufAddr_read(s);
+            break;
+
         case BasicModeCtrl:
             ret = rtl8139_BasicModeCtrl_read(s);
             break;
@@ -2521,6 +2620,20 @@
 #endif
             break;
 
+		case Timer:
+			ret = s->TCTR;
+#ifdef DEBUG_RTL8139
+			printf("RTL8139: TCTR Timer read val=0x%08x\n", ret);
+#endif
+			break;
+
+		case FlashReg:
+			ret = s->TimerInt;
+#ifdef DEBUG_RTL8139
+			printf("RTL8139: FlashReg TimerInt read val=0x%08x\n", ret);
+#endif
+			break;
+
         default:
 #ifdef DEBUG_RTL8139
             printf("RTL8139: ioport read(l) addr=0x%x via read(b)\n", addr);
@@ -2688,6 +2801,10 @@
     qemu_put_8s(f, &s->eeprom.eesk);
     qemu_put_8s(f, &s->eeprom.eedi);
     qemu_put_8s(f, &s->eeprom.eedo);
+
+	qemu_put_be32s(f, &s->TCTR);
+	qemu_put_be32s(f, &s->TimerInt);
+	qemu_put_be64s(f, &s->TCTR_base);
 }
 
 static int rtl8139_load(QEMUFile* f,void* opaque,int version_id)
@@ -2695,9 +2812,11 @@
     RTL8139State* s=(RTL8139State*)opaque;
     int i;
 
-    if (version_id != 1)
+	/* just 2 versions for now */
+    if (version_id > 2)
             return -EINVAL;
 
+	/* saved since version 1 */
     qemu_get_buffer(f, s->phys, 6);
     qemu_get_buffer(f, s->mult, 8);
 
@@ -2769,7 +2888,22 @@
     qemu_get_8s(f, &s->eeprom.eedi);
     qemu_get_8s(f, &s->eeprom.eedo);
 
-    return 0;
+	/* saved since version 2 */
+	if (version_id >= 2)
+	{
+		qemu_get_be32s(f, &s->TCTR);
+		qemu_get_be32s(f, &s->TimerInt);
+		qemu_get_be64s(f, &s->TCTR_base);
+	}
+	else
+	{
+		/* not saved, use default */
+		s->TCTR = 0;
+		s->TimerInt = 0;
+		s->TCTR_base = 0;
+	}
+
+	return 0;
 }
 
 /***********************************************************/
@@ -2817,6 +2951,63 @@
     rtl8139_mmio_writel,
 };
 
+static inline int64_t rtl8139_get_next_tctr_time(RTL8139State *s, int64_t current_time)
+{
+    int64_t next_time = current_time + 
+        muldiv64(1, ticks_per_sec, PCI_FREQUENCY);
+    if (next_time <= current_time)
+        next_time = current_time + 1;
+    return next_time;
+}
+
+static void rtl8139_timer(void *opaque)
+{
+	RTL8139State *s = opaque;
+
+	int is_timeout = 0;
+
+	int64_t  curr_time;
+	uint32_t curr_tick;
+
+	if (!s->clock_enabled)
+	{
+#ifdef DEBUG_RTL8139
+		printf("RTL8139: >>> timer: clock is not running\n");
+#endif
+		return;
+	}
+
+	curr_time = qemu_get_clock(vm_clock);
+
+	curr_tick = muldiv64(curr_time - s->TCTR_base, PCI_FREQUENCY, ticks_per_sec);
+
+	if (s->TimerInt && curr_tick >= s->TimerInt)
+	{
+		if (s->TCTR < s->TimerInt || curr_tick < s->TCTR)
+		{
+			is_timeout = 1;
+		}
+	}
+
+	s->TCTR = curr_tick;
+
+#ifdef DEBUG_RTL8139
+	printf("RTL8139: >>> timer: tick=%08u\n", s->TCTR);
+#endif
+
+	if (is_timeout)
+	{
+#ifdef DEBUG_RTL8139
+		printf("RTL8139: >>> timer: timeout tick=%08u\n", s->TCTR);
+#endif
+		s->IntrStatus |= PCSTimeout;
+		rtl8139_update_irq(s);
+	}
+
+	qemu_mod_timer(s->timer, 
+		rtl8139_get_next_tctr_time(s,curr_time));
+}
+
 void pci_rtl8139_init(PCIBus *bus, NICInfo *nd)
 {
     PCIRTL8139State *d;
@@ -2833,7 +3024,7 @@
     pci_conf[0x02] = 0x39;
     pci_conf[0x03] = 0x81;
     pci_conf[0x04] = 0x05; /* command = I/O space, Bus Master */
-    pci_conf[0x08] = 0x20; /* 0x10 */ /* PCI revision ID; >=0x20 is for 8139C+ */
+    pci_conf[0x08] = RTL8139_PCI_REVID; /* PCI revision ID; >=0x20 is for 8139C+ */
     pci_conf[0x0a] = 0x00; /* ethernet network controller */
     pci_conf[0x0b] = 0x02;
     pci_conf[0x0e] = 0x00; /* header_type */
@@ -2869,7 +3060,13 @@
              s->macaddr[5]);
              
     /* XXX: instance number ? */
-    register_savevm("rtl8139", 0, 1, rtl8139_save, rtl8139_load, s);
+    register_savevm("rtl8139", 0, 2, rtl8139_save, rtl8139_load, s);
     register_savevm("rtl8139_pci", 0, 1, generic_pci_save, generic_pci_load, 
                     &d->dev);
+
+	s->timer = qemu_new_timer(vm_clock, rtl8139_timer, s);
+
+	qemu_mod_timer(s->timer, 
+		rtl8139_get_next_tctr_time(s,qemu_get_clock(vm_clock)));
 }
+

--------------090201030807060400040102--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?445D092E.2040501>