ZealOS/docs/Home/Net/Drivers/E1000.CC.html

233 lines
18 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V0.06">
<style type="text/css">
body {background-color:#000000;}
.cF0{color:#ffffff;background-color:#000000;}
.cF1{color:#3465a4;background-color:#000000;}
.cF2{color:#4e9a06;background-color:#000000;}
.cF3{color:#06989a;background-color:#000000;}
.cF4{color:#a24444;background-color:#000000;}
.cF5{color:#75507b;background-color:#000000;}
.cF6{color:#ce982f;background-color:#000000;}
.cF7{color:#bcc0b9;background-color:#000000;}
.cF8{color:#555753;background-color:#000000;}
.cF9{color:#729fcf;background-color:#000000;}
.cFA{color:#82bc49;background-color:#000000;}
.cFB{color:#34e2e2;background-color:#000000;}
.cFC{color:#ac3535;background-color:#000000;}
.cFD{color:#ad7fa8;background-color:#000000;}
.cFE{color:#fce94f;background-color:#000000;}
.cFF{color:#000000;background-color:#000000;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>/*</span><span class=cF0> </span><span class=cF2>Intel(R) E1000 Driver</span><span class=cF0>
<a name="l2"></a> </span><span class=cF2>Author: TomAwezome</span><span class=cF0>
<a name="l3"></a>
<a name="l4"></a> </span><span class=cF2>Driver is based on:</span><span class=cF0>
<a name="l5"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>01000101's example i825xx driver</span><span class=cF0>
<a name="l6"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>OSDev Intel(R) 8254x documentation</span><span class=cF0>
<a name="l7"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>Intel(R) PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual</span><span class=cF0>
<a name="l8"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>Linux E1000 driver</span><span class=cF0>
<a name="l9"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>any other useful sources.</span><span class=cF0>
<a name="l10"></a>
<a name="l11"></a> </span><span class=cF2>Guidelines:</span><span class=cF0>
<a name="l12"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>Magic numbers are bad. #defines are good.</span><span class=cF0>
<a name="l13"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>Understandability over LOC.</span><span class=cF0>
<a name="l14"></a> </span><span class=cF2>-</span><span class=cF0> </span><span class=cF2>Clear documentation.</span><span class=cF0>
<a name="l15"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l16"></a>
<a name="l17"></a></span><span class=cF2>// TODO: clean up entire driver</span><span class=cF0>
<a name="l18"></a>
<a name="l19"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_REG_CTRL </span><span class=cFE>0x0000</span><span class=cF0>
<a name="l20"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_REG_EERD </span><span class=cFE>0x0014</span><span class=cF0> </span><span class=cF2>// EEPROM Read</span><span class=cF0>
<a name="l21"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_REG_IMS </span><span class=cFE>0x00D0</span><span class=cF0>
<a name="l22"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_REG_MTA </span><span class=cFE>0x5200</span><span class=cF0> </span><span class=cF2>// Multicast Table Array</span><span class=cF0>
<a name="l23"></a>
<a name="l24"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_CTRLf_SLU </span><span class=cFE>6</span><span class=cF0> </span><span class=cF2>// Set Link Up ?</span><span class=cF0>
<a name="l25"></a>
<a name="l26"></a>#</span><span class=cF1>define</span><span class=cF0> E1000_CTRLF_SLU (</span><span class=cFE>1</span><span class=cF0> &lt;&lt; E1000_CTRLf_SLU) </span><span class=cF2>// Set Link Up ?</span><span class=cF0>
<a name="l27"></a>
<a name="l28"></a></span><span class=cF1>class</span><span class=cF0> CE1000
<a name="l29"></a>{
<a name="l30"></a> </span><span class=cF9>CPCIDev</span><span class=cF0> *pci;
<a name="l31"></a>
<a name="l32"></a> </span><span class=cF1>U8</span><span class=cF0> mac_address[</span><span class=cFE>6</span><span class=cF0>];
<a name="l33"></a> </span><span class=cF9>U64</span><span class=cF0> mmio_address;
<a name="l34"></a>
<a name="l35"></a>} e1000; </span><span class=cF2>// e1000 is the global variable we store all of this into.</span><span class=cF0>
<a name="l36"></a>
<a name="l37"></a></span><span class=cF9>CPCIDev</span><span class=cF0> *E1000PCIDevFind()
<a name="l38"></a>{</span><span class=cF2>// Find and return E1000 card as a CPCIDev pointer.</span><span class=cF0>
<a name="l39"></a>
<a name="l40"></a> </span><span class=cF9>CPCIDev</span><span class=cF0> *pci = </span><span class=cF5>PCIDevFind</span><span class=cF0>(</span><span class=cF3>PCIC_NETWORK</span><span class=cF0>,, PCIV_E1000);
<a name="l41"></a>
<a name="l42"></a> </span><span class=cF1>if</span><span class=cF0> (!pci)
<a name="l43"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l44"></a>
<a name="l45"></a> </span><span class=cF5>ClassRep</span><span class=cF0>(pci);
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF1>switch</span><span class=cF0> (pci-&gt;device_id)
<a name="l48"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l49"></a> </span><span class=cF1>case</span><span class=cF0> PCID_82545EM:
<a name="l50"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l51"></a>
<a name="l52"></a> </span><span class=cF1>default</span><span class=cF0>:
<a name="l53"></a> pci = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l54"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l55"></a>
<a name="l56"></a> </span><span class=cF1>return</span><span class=cF0> pci;
<a name="l57"></a>}
<a name="l58"></a>
<a name="l59"></a></span><span class=cF9>U32</span><span class=cF0> E1000MMIORead(</span><span class=cF9>U32</span><span class=cF0> </span><span class=cF1>offset</span><span class=cF0>)
<a name="l60"></a>{
<a name="l61"></a> </span><span class=cF9>U32</span><span class=cF0> *val = e1000.mmio_address + </span><span class=cF1>offset</span><span class=cF0>;
<a name="l62"></a>
<a name="l63"></a> </span><span class=cF1>return</span><span class=cF0> *val;
<a name="l64"></a>}
<a name="l65"></a>
<a name="l66"></a></span><span class=cF1>U0</span><span class=cF0> E1000MMIOWrite(</span><span class=cF9>U32</span><span class=cF0> </span><span class=cF1>offset</span><span class=cF0>, </span><span class=cF9>U32</span><span class=cF0> val)
<a name="l67"></a>{
<a name="l68"></a> </span><span class=cF9>U32</span><span class=cF0> *addr = e1000.mmio_address + </span><span class=cF1>offset</span><span class=cF0>;
<a name="l69"></a>
<a name="l70"></a> *addr = val;
<a name="l71"></a>}
<a name="l72"></a>
<a name="l73"></a></span><span class=cF9>U16</span><span class=cF0> E1000EEPROMRead(</span><span class=cF1>U8</span><span class=cF0> word)
<a name="l74"></a>{ </span><span class=cF2>// word arg is which U16 to read</span><span class=cF0>
<a name="l75"></a> </span><span class=cF9>U16</span><span class=cF0> data;
<a name="l76"></a> </span><span class=cF9>U32</span><span class=cF0> temp;
<a name="l77"></a>
<a name="l78"></a> E1000MMIOWrite(E1000_REG_EERD, </span><span class=cFE>1</span><span class=cF0> | word &lt;&lt; </span><span class=cFE>8</span><span class=cF0>);
<a name="l79"></a>
<a name="l80"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>(temp = E1000MMIORead</span><span class=cF7>(</span><span class=cF0>E1000_REG_EERD</span><span class=cF7>)</span><span class=cF0>) &amp; (</span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cFE>4</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0>)
<a name="l81"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>1</span><span class=cF0>);
<a name="l82"></a>
<a name="l83"></a> data = (temp &gt;&gt; </span><span class=cFE>16</span><span class=cF0>) &amp; </span><span class=cFE>0xFFFF</span><span class=cF0>;
<a name="l84"></a> </span><span class=cF1>return</span><span class=cF0> data;
<a name="l85"></a>}
<a name="l86"></a>
<a name="l87"></a></span><span class=cF1>U0</span><span class=cF0> E1000MACGet()
<a name="l88"></a>{
<a name="l89"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l90"></a> </span><span class=cF9>U16</span><span class=cF0> mac;
<a name="l91"></a>
<a name="l92"></a> NetLog(</span><span class=cF6>&quot;E1000 GET MAC: Getting VM MAC.&quot;</span><span class=cF0>);
<a name="l93"></a>
<a name="l94"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFE>3</span><span class=cF0>; i++)
<a name="l95"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l96"></a> mac = E1000EEPROMRead(i);
<a name="l97"></a> e1000.mac_address[</span><span class=cFE>2</span><span class=cF0>*i] = mac &amp; </span><span class=cFE>0xFF</span><span class=cF0>;
<a name="l98"></a> e1000.mac_address[</span><span class=cFE>2</span><span class=cF0>*i+</span><span class=cFE>1</span><span class=cF0>] = (mac &gt;&gt; </span><span class=cFE>8</span><span class=cF0>) &amp; </span><span class=cFE>0xFF</span><span class=cF0>;
<a name="l99"></a> NetLog(</span><span class=cF6>&quot; %02X %02X&quot;</span><span class=cF0>, mac.u8[</span><span class=cFE>0</span><span class=cF0>], mac.u8[</span><span class=cFE>1</span><span class=cF0>]);
<a name="l100"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l101"></a>
<a name="l102"></a>}
<a name="l103"></a>
<a name="l104"></a></span><span class=cF1>U0</span><span class=cF0> EthernetFrameFinish(</span><span class=cF9>I64</span><span class=cF0> de_index)
<a name="l105"></a>{</span><span class=cF2>//Alias for driver Finish TX function.</span><span class=cF0>
<a name="l106"></a> </span><span class=cF2>//E1000TransmitPacketFinish(de_index);</span><span class=cF0>
<a name="l107"></a> </span><span class=cF1>no_warn</span><span class=cF0> de_index;
<a name="l108"></a> NetErr(</span><span class=cF6>&quot;TODO E1000&quot;</span><span class=cF0>);
<a name="l109"></a>}
<a name="l110"></a>
<a name="l111"></a></span><span class=cF2>/*</span><span class=cF0>
<a name="l112"></a></span><span class=cF2>U0 PCIInterruptsReroute(I64 base)</span><span class=cF0>
<a name="l113"></a></span><span class=cF2>{ // todo: comments explaining process, maybe better var names</span><span class=cF0>
<a name="l114"></a> </span><span class=cF2>I64</span><span class=cF0> </span><span class=cF2> i;</span><span class=cF0>
<a name="l115"></a> </span><span class=cF2>U8</span><span class=cF0> </span><span class=cF2>*da = dev.uncached_alias + IOAPIC_REG;</span><span class=cF0>
<a name="l116"></a> </span><span class=cF2>U32 *_d = dev.uncached_alias + IOAPIC_DATA;</span><span class=cF0>
<a name="l117"></a>
<a name="l118"></a> </span><span class=cF2>for (i = 0; i &lt; 4; i++)</span><span class=cF0>
<a name="l119"></a> </span><span class=cF2>{</span><span class=cF0>
<a name="l120"></a> </span><span class=cF2>*da = IOREDTAB + i * 2 + 1;</span><span class=cF0>
<a name="l121"></a> </span><span class=cF2>*_d = dev.mp_apic_ids[INT_DEST_CPU] &lt;&lt; 24;</span><span class=cF0>
<a name="l122"></a> </span><span class=cF2>*da = IOREDTAB + i * 2;</span><span class=cF0>
<a name="l123"></a> </span><span class=cF2>*_d = 0x4000 + base + i;</span><span class=cF0>
<a name="l124"></a> </span><span class=cF2>}</span><span class=cF0>
<a name="l125"></a></span><span class=cF2>}</span><span class=cF0>
<a name="l126"></a>
<a name="l127"></a>
<a name="l128"></a></span><span class=cF2>U0 E1000InterruptsSetup()</span><span class=cF0>
<a name="l129"></a></span><span class=cF2>{</span><span class=cF0>
<a name="l130"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>PCIInterruptsReroute(I_E1000);</span><span class=cF0>
<a name="l131"></a> </span><span class=cF2>NetErr(&quot;TODO E1000&quot;);</span><span class=cF0>
<a name="l132"></a></span><span class=cF2>}</span><span class=cF0>
<a name="l133"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l134"></a>
<a name="l135"></a></span><span class=cF1>U0</span><span class=cF0> E1000Init()
<a name="l136"></a>{
<a name="l137"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l138"></a>
<a name="l139"></a> </span><span class=cF5>MemSet</span><span class=cF0>(&amp;e1000, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CE1000</span><span class=cF7>)</span><span class=cF0>); </span><span class=cF2>// e1000 global var will hold member data the driver uses often.</span><span class=cF0>
<a name="l140"></a> </span><span class=cF6>&quot;\nE1000 driver WIP\n\n&quot;</span><span class=cF0>;
<a name="l141"></a>
<a name="l142"></a> e1000.pci = E1000PCIDevFind;
<a name="l143"></a> </span><span class=cF1>if</span><span class=cF0> (!e1000.pci)
<a name="l144"></a> </span><span class=cF1>return</span><span class=cF0>; </span><span class=cF2>// if we don't find the card, quit.</span><span class=cF0>
<a name="l145"></a>
<a name="l146"></a>
<a name="l147"></a> e1000.mmio_address = </span><span class=cFB>dev</span><span class=cF0>.uncached_alias + e1000.pci-&gt;base[</span><span class=cFE>0</span><span class=cF0>] &amp; ~</span><span class=cFE>0xF</span><span class=cF0>;
<a name="l148"></a> </span><span class=cF2>// Assuming card supports MMIO... lower 4 bits are hardwired zero (?)</span><span class=cF0>
<a name="l149"></a>
<a name="l150"></a> </span><span class=cF6>&quot;\nMMIO address: 0x%0X\n&quot;</span><span class=cF0>, e1000.mmio_address;
<a name="l151"></a>
<a name="l152"></a> </span><span class=cF2>// init rx/tx addrs? (linux)</span><span class=cF0>
<a name="l153"></a>
<a name="l154"></a> </span><span class=cF2>// eeprom? MAC ?</span><span class=cF0>
<a name="l155"></a> E1000MACGet;
<a name="l156"></a>
<a name="l157"></a> </span><span class=cF2>// setup link? (01000101's driver)</span><span class=cF0>
<a name="l158"></a> E1000MMIOWrite(E1000_REG_CTRL, E1000MMIORead</span><span class=cF7>(</span><span class=cF0>E1000_REG_CTRL</span><span class=cF7>)</span><span class=cF0> | E1000_CTRLF_SLU);
<a name="l159"></a>
<a name="l160"></a> </span><span class=cF2>// zero out multicast hash? (linux)</span><span class=cF0>
<a name="l161"></a> </span><span class=cF2>// zero out multicast table array (01000101's driver)</span><span class=cF0>
<a name="l162"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFE>128</span><span class=cF0>; i++)
<a name="l163"></a> E1000MMIOWrite(E1000_REG_MTA + i*</span><span class=cFE>4</span><span class=cF0>, </span><span class=cFE>0</span><span class=cF0>);
<a name="l164"></a>
<a name="l165"></a> </span><span class=cF2>// setup link? (linux)</span><span class=cF0>
<a name="l166"></a>
<a name="l167"></a> </span><span class=cF2>// clear all statistics regs after link establish attempt (linux)</span><span class=cF0>
<a name="l168"></a>
<a name="l169"></a> </span><span class=cF2>// enable &amp; clear existing interupts (01000101's driver)</span><span class=cF0>
<a name="l170"></a> E1000MMIOWrite(E1000_REG_IMS, </span><span class=cFE>0x1F6DC</span><span class=cF0>); </span><span class=cF2>// TODO: WHAT IS THIS</span><span class=cF0>
<a name="l171"></a> E1000MMIORead(</span><span class=cFE>0xC0</span><span class=cF0>); </span><span class=cF2>// TODO : WHAT IS THIS ???</span><span class=cF0>
<a name="l172"></a>
<a name="l173"></a> </span><span class=cF2>// start rx tx?</span><span class=cF0>
<a name="l174"></a>
<a name="l175"></a>
<a name="l176"></a> NetErr(</span><span class=cF6>&quot;TODO E1000&quot;</span><span class=cF0>);
<a name="l177"></a>}
<a name="l178"></a>
<a name="l179"></a></span><span class=cF9>I64</span><span class=cF0> EthernetFrameAllocate(</span><span class=cF1>U8</span><span class=cF0> **packet_buffer_out,
<a name="l180"></a> </span><span class=cF1>U8</span><span class=cF0> *source_address,
<a name="l181"></a> </span><span class=cF1>U8</span><span class=cF0> *destination_address,
<a name="l182"></a> </span><span class=cF9>U16</span><span class=cF0> ethertype,
<a name="l183"></a> </span><span class=cF9>I64</span><span class=cF0> packet_length)
<a name="l184"></a>{
<a name="l185"></a> </span><span class=cF1>no_warn</span><span class=cF0> packet_buffer_out, source_address, destination_address, ethertype, packet_length;
<a name="l186"></a> NetErr(</span><span class=cF6>&quot;TODO E1000&quot;</span><span class=cF0>);
<a name="l187"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
<a name="l188"></a>}
<a name="l189"></a>
<a name="l190"></a></span><span class=cF1>U8</span><span class=cF0> *EthernetMACGet()
<a name="l191"></a>{
<a name="l192"></a> </span><span class=cF1>return</span><span class=cF0> e1000.mac_address;
<a name="l193"></a>}
<a name="l194"></a>
<a name="l195"></a></span><span class=cF1>U0</span><span class=cF0> NetStop()
<a name="l196"></a>{
<a name="l197"></a>
<a name="l198"></a>}
<a name="l199"></a>
<a name="l200"></a></span><span class=cF1>U0</span><span class=cF0> NetStart()
<a name="l201"></a>{
<a name="l202"></a>
<a name="l203"></a>}
<a name="l204"></a>
<a name="l205"></a>E1000Init;</span></pre></body>
</html>