db768445c6be390a0cab788591f0276759689394
[tpg/acess2.git] / KernelLand / Modules / Network / PRO100 / main.c
1 /*
2  * Acess2 PRO/100 Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Driver core
7  *
8  * Built with reference to the linux e100 driver (drivers/net/ethernet/intel/e100.c)
9  * 82559-fast-ethernet-multifunciton-pci-datasheet.pdf
10  */
11 #define DEBUG   1
12 #include <acess.h>
13 #include <IPStack/include/adapters_api.h>
14 #include <modules.h>
15 #include <drv_pci.h>
16 #include <timers.h>
17 #include "pro100.h"
18
19 // === CONSTANTS ===
20 #define NUM_STATIC_CARDS        2
21 static const Uint16     caSupportedCards[][2] = {
22         {0x8086, 0x103D},       // prelude's card
23         {0x8086, 0x1209},       // qemu's i82559 emulation
24         };
25 static const int        ciNumSupportedCards = sizeof(caSupportedCards)/sizeof(caSupportedCards[0]);
26
27 // === PROTOTYPES ===
28  int    PRO100_Install(char **Arguments);
29  int    PRO100_InitCard(tCard *Card);
30  int    PRO100_Cleanup(void);
31 tIPStackBuffer  *PRO100_WaitForPacket(void *Ptr);
32  int    PRO100_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
33 void    PRO100_IRQHandler(int Num, void *Ptr);
34
35 Uint16  PRO100_int_ReadEEPROM(tCard *Card, Uint8 *AddrLen, size_t Ofs);
36
37 static void     _Write8(tCard *Card, int Ofs, Uint8 Val);
38 static void     _Write16(tCard *Card, int Ofs, Uint16 Val);
39 static void     _Write32(tCard *Card, int Ofs, Uint32 Val);
40 static Uint8    _Read8(tCard *Card, int Ofs);
41 static Uint16   _Read16(tCard *Card, int Ofs);
42 //static Uint32 _Read32(tCard *Card, int Ofs);
43 static void     _FlushWait(tCard *Card, int Delay);
44
45 // === GLOBALS ===
46 MODULE_DEFINE(0, 0x100, PRO100, PRO100_Install, PRO100_Cleanup, "IPStack", NULL);
47 tIPStack_AdapterType    gPRO100_AdapterType = {
48         .Name = "PRO/100",
49         .Type = ADAPTERTYPE_ETHERNET_100M,
50         .Flags = 0,     
51         .SendPacket = PRO100_SendPacket,
52         .WaitForPacket = PRO100_WaitForPacket
53 };
54 tCard   gaPRO100_StaticCards[NUM_STATIC_CARDS];
55
56 // === CODE ===
57 int PRO100_Install(char **Arguments)
58 {
59          int    cardidx = 0;
60         for( int i = 0; i < ciNumSupportedCards; i ++ )
61         {
62                 const Uint16    *ven_dev = caSupportedCards[i];
63                 LOG("Checking %04x:%04x: %i reported", ven_dev[0], ven_dev[1],
64                         PCI_CountDevices(ven_dev[0], ven_dev[1]));
65                 for( int idx = 0, pciid = -1; -1 != (pciid = PCI_GetDevice(ven_dev[0], ven_dev[1], idx)); idx++ )
66                 {
67                         Uint8   irq = PCI_GetIRQ(pciid);
68                         Uint32  mmiobase = PCI_GetValidBAR(pciid, 0, PCI_BARTYPE_MEM32);
69                         Uint16  iobase = PCI_GetValidBAR(pciid, 1, PCI_BARTYPE_IO);
70                         LOG("IO Base = %x, MMIOBase = %x", iobase, mmiobase);
71
72                         PCI_SetCommand(pciid, PCI_CMD_IOENABLE|PCI_CMD_BUSMASTER, 0);
73                         
74                         tCard   *card;
75                         if( cardidx < NUM_STATIC_CARDS ) {
76                                 card = &gaPRO100_StaticCards[cardidx++];
77                         }
78                         else {
79                                 card = malloc(sizeof(tCard));
80                         }
81
82                         card->IOBase = iobase;
83                         //card->MMIO = MM_MapHWPages(mmiobase, 1);
84                         IRQ_AddHandler(irq, PRO100_IRQHandler, card); 
85                         
86                         // TODO: Error check
87                         PRO100_InitCard(card);
88                         
89                         IPStack_Adapter_Add(&gPRO100_AdapterType, card, card->MAC.Bytes);
90                 }
91         }
92         return MODULE_ERR_OK;
93 }
94
95 int PRO100_InitCard(tCard *Card)
96 {
97         // Initialise structures
98         Semaphore_Init(&Card->TXCommandSem, NUM_TX, NUM_TX, "PRO100", "Command Buffers");
99         Semaphore_Init(&Card->RXSemaphore, 0, NUM_RX, "PRO100", "Receive");
100
101         // Card reset
102         _Write32(Card, REG_Port, PORT_SELECTIVERESET);
103         _FlushWait(Card, 20);   // - Write Flush, wait 20us
104         _Write32(Card, REG_Port, PORT_SOFTWARERESET);
105         _FlushWait(Card, 20);   // - Write Flush, wait 20us
106
107         // Read MAC address
108         Uint8   addr_len = 8;   // default to 8, updated on first read
109         Card->MAC.Words[0] = PRO100_int_ReadEEPROM(Card, &addr_len, 0);
110         Card->MAC.Words[1] = PRO100_int_ReadEEPROM(Card, &addr_len, 1);
111         Card->MAC.Words[2] = PRO100_int_ReadEEPROM(Card, &addr_len, 2);
112
113         // Set interrupt mask
114         _Write8(Card, REG_IntMask, 0);
115
116         // Prepare Command Unit
117         Card->TXCommands = MM_AllocDMA(1, 32, NULL);
118         Uint32  txbase = MM_GetPhysAddr(Card->TXCommands);
119         ASSERT(Card->TXCommands);
120         for( int i = 0; i < NUM_TX; i ++ )
121         {
122                 tCommandUnit    *cu = &Card->TXCommands[i].Desc.CU;
123                 cu->Status = 0;
124                 cu->Command = CMD_Nop|CMD_Suspend;
125                 cu->Link = MM_GetPhysAddr(&Card->TXCommands[(i+1)%NUM_TX]) - txbase;
126         }
127         
128         _Write32(Card, REG_GenPtr, txbase);
129         _Write16(Card, REG_Command, CU_CMD_BASE);
130         // Ensure CU is in suspend before we attempt sending
131         Card->LastTXIndex = 1;
132         Card->CurTXIndex = 1;
133         _Write32(Card, REG_GenPtr, 0);
134         _Write16(Card, REG_Command, CU_CMD_START);
135
136         // Create RX Descriptors
137         for( int i = 0; i < NUM_RX; i += 2 )
138         {
139                 char    *base = MM_AllocDMA(1, 32, NULL);
140                 ASSERT(base);
141                 Card->RXBufs[i+0] = (void*)base;
142                 Card->RXBufs[i+1] = (void*)(base + 0x800);
143                 for( int j = 0; j < 2; j ++ )
144                 {
145                         tRXBuffer       *rx = Card->RXBufs[i+j];
146                         rx->CU.Status = 0;
147                         rx->CU.Command = 0;
148                         // Link is populated later
149                         rx->Size = RX_BUF_SIZE;
150                         rx->RXBufAddr = 0;      // unused?
151                 }
152         }
153
154         // NOTE: All `Link` values are relative to the RX base address  
155         Uint32  rx_desc_phys = MM_GetPhysAddr(Card->RXBufs[0]);
156         for( int i = 0; i < NUM_RX-1; i ++ )
157         {
158                 tRXBuffer       *rx = Card->RXBufs[i];
159                 rx->CU.Link = MM_GetPhysAddr(Card->RXBufs[i+1]) - rx_desc_phys;
160         }
161         Card->RXBufs[NUM_RX-1]->CU.Command = CMD_Suspend;
162         Card->RXBufs[NUM_RX-1]->CU.Link = 0;    // link = 0, loop back
163         
164         // Set RX Buffer base
165         _Write32(Card, REG_GenPtr, rx_desc_phys);
166         _Write16(Card, REG_Command, RX_CMD_ADDR_LOAD);
167         
168         _Write32(Card, REG_GenPtr, 0);
169         _Write16(Card, REG_Command, RX_CMD_START);
170
171         return 0;
172 }
173
174 int PRO100_Cleanup(void)
175 {
176         return 0;
177 }
178
179 void PRO100_ReleaseRXBuf(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
180 {
181         tCard   *Card = Arg;
182         tRXBuffer       *buf = (tRXBuffer*)Data - 1;
183
184          int    idx;
185         for( idx = 0; idx < NUM_RX && Card->RXBufs[idx] != buf; idx ++ )
186                 ;
187         ASSERT(idx != NUM_RX);
188
189         tRXBuffer       *prev = Card->RXBufs[ (idx-1+NUM_RX)%NUM_RX ];
190         buf->CU.Status = 0;
191         buf->CU.Command = 0;
192         prev->CU.Command &= ~CMD_Suspend;
193         
194         // Resume
195         _Write16(Card, REG_Command, RX_CMD_RESUME);
196 }
197
198
199 tIPStackBuffer *PRO100_WaitForPacket(void *Ptr)
200 {
201         tCard   *Card = Ptr;
202         // Wait for a packet
203         do {
204                 Semaphore_Wait(&Card->RXSemaphore, 1);
205         } while( Card->RXBufs[Card->CurRXIndex]->CU.Status == 0 );
206         // Mark previous buffer as suspend (stops the RX unit running into old packets
207         Card->RXBufs[ (Card->CurRXIndex-1+NUM_RX)%NUM_RX ]->CU.Command |= CMD_Suspend;
208         tRXBuffer *buf = Card->RXBufs[Card->CurRXIndex++];
209         
210         // Return packet (freed in PRO100_ReleaseRXBuf);
211         tIPStackBuffer  *ret = IPStack_Buffer_CreateBuffer(1);
212         // - actual data is just after the descriptor
213         IPStack_Buffer_AppendSubBuffer(ret, buf->Count, 0, buf+1, PRO100_ReleaseRXBuf, Card);
214         
215         return ret;
216 }
217
218 void PRO100_int_SetBuf(tTXCommand *TXC, int *IndexPtr, Uint32 Addr, Uint16 Len)
219 {
220         ASSERTC(*IndexPtr, <, NUM_LOCAL_TXBUFS);
221         
222         tTXBufDesc      *txb = &TXC->LocalBufs[*IndexPtr];
223         txb->Addr = Addr;
224         txb->Len = Len;
225         (*IndexPtr) ++;
226 }
227
228 int PRO100_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
229 {
230         tCard   *Card = Ptr;
231
232         Semaphore_Wait(&Card->TXCommandSem, 1);
233
234         // Acquire a command buffer
235         Mutex_Acquire(&Card->lTXCommands);
236         int txc_idx = Card->CurTXIndex;
237         Card->CurTXIndex = (Card->CurTXIndex + 1) % NUM_TX;
238         Mutex_Release(&Card->lTXCommands);
239         tTXCommand      *txc = &Card->TXCommands[txc_idx];
240
241         // Populate
242          int    txb_idx = 0;
243         const void      *ptr;
244         size_t  len;
245         size_t  total_size = 0;
246          int    buf_idx = -1;
247         while( (buf_idx = IPStack_Buffer_GetBuffer(Buffer, buf_idx, &len, &ptr)) != -1 ) 
248         {
249                 #if PHYS_BITS > 32
250                 if( MM_GetPhysAddr(ptr) >> 32 ) {
251                         // Need to bounce
252                         TODO();
253                         continue ;
254                 }
255                 #endif
256                 
257                 ASSERTC(len, <, PAGE_SIZE);
258                 
259                 // Check if buffer split is required
260                 if( MM_GetPhysAddr((char*)ptr + len-1) != MM_GetPhysAddr(ptr)+len-1 )
261                 {
262                         // Need to split buffer
263                         size_t  space = PAGE_SIZE - ((tVAddr)ptr % PAGE_SIZE);
264                         PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr(ptr), space);
265                         PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr((char*)ptr+space), len-space);
266                 }
267                 else
268                 {
269                         // Single buffer
270                         PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr(ptr), len);
271                 }
272                 
273                 total_size += len;
274         }
275
276         // Set buffer pointer
277         Card->TXBuffers[txc_idx] = Buffer;
278         // Mark as usable
279         txc->Desc.TBDArrayAddr = 0xFFFFFFFF;
280         txc->Desc.TCBBytes = total_size;
281         txc->Desc.TXThreshold = 0;      // TODO: What does this do on RHW?
282         txc->Desc.TBDCount = 0;
283         txc->Desc.CU.Command = CMD_Suspend|CMD_Tx;
284         // - Mark previous as not suspended
285         Card->TXCommands[ (txc_idx-1+NUM_TX) % NUM_TX ].Desc.CU.Command &= ~CMD_Suspend;
286
287         IPStack_Buffer_LockBuffer(Buffer);
288
289         // And dispatch
290         // - If currently running or idle, this should not matter
291         // NOTE: Qemu describes this behavior as 'broken'
292         _Write16(Card, REG_Command, CU_CMD_RESUME);
293
294         IPStack_Buffer_LockBuffer(Buffer);
295         IPStack_Buffer_UnlockBuffer(Buffer);
296
297         return 0;
298 }
299
300 void PRO100_IRQHandler(int Num, void *Ptr)
301 {
302         tCard   *Card = Ptr;
303         Uint8   status = _Read8(Card, REG_Ack);
304         
305         if( !status )
306                 return ;
307         
308         _Write8(Card, REG_Ack, status);
309         LOG("status = %x", status);
310         if( status & ISR_FR ) {
311                 LOG("FR");
312                 Semaphore_Signal(&Card->RXSemaphore, 1);
313         }
314         
315         // CU Idle
316         if( status & ISR_CNA )
317         {
318                 // Chase the next command buffer
319                 while( Card->LastTXIndex != Card->CurTXIndex )
320                 {
321                          int    idx = Card->LastTXIndex++;
322                         // Once we hit an incomplete command, stop
323                         if( !(Card->TXCommands[idx].Desc.CU.Status & CU_Status_Complete) )
324                                 break ;
325                         IPStack_Buffer_UnlockBuffer( Card->TXBuffers[idx] );
326                         Semaphore_Signal(&Card->TXCommandSem, 1);
327                 }
328                 LOG("CU Idle (%i / %i)", Card->LastTXIndex, Card->CurTXIndex);
329         }
330 }
331
332 Uint16 PRO100_int_ReadEEPROM(tCard *Card, Uint8 *addr_len, size_t Ofs)
333 {
334         ASSERTC( *addr_len, <=, 12 );
335         
336         Uint32  addr_data = ((EEPROM_OP_READ << *addr_len) | Ofs) << 16;
337
338         // Deslect chip (god knows what state it was left in)
339         _Write16( Card, REG_EEPROMCtrl, 0 );
340         _FlushWait(Card, 4);    // Flush + 4us
341         // Raise CS
342         _Write16( Card, REG_EEPROMCtrl, EEPROM_CTRL_CS | EEPROM_CTRL_SK );
343         _FlushWait(Card, 4);    // Flush + 4us
344
345         Uint32  data = 0;       
346
347         // 2 preamble (0,1) + 2 command (read=1,0) + n address + 16 data
348         for( int i = (2+2+*addr_len+16); i --; )
349         {
350                 Uint16  ctrl = EEPROM_CTRL_CS | ((addr_data & (1 << i)) ? EEPROM_CTRL_DI : 0);
351                 _Write16( Card, REG_EEPROMCtrl, ctrl );
352                 _FlushWait(Card, 4);    // Flush + 4us
353                 _Write16( Card, REG_EEPROMCtrl, ctrl | EEPROM_CTRL_SK );
354                 _FlushWait(Card, 4);    // Flush + 4us
355                 
356                 ctrl = _Read16( Card, REG_EEPROMCtrl );
357                 // Once the address is fully recieved, the card emits a zero bit
358                 if( !(ctrl & EEPROM_CTRL_DO) && i > 16 )
359                 {
360                         *addr_len = *addr_len - (i - 16);
361                         LOG("addr_len = %i", *addr_len);
362                         
363                         i = 16;
364                 }
365                 
366                 data = (data << 1) | (ctrl & EEPROM_CTRL_DO ? 1 : 0);
367         }
368
369         // Deslect chip
370         _Write16( Card, REG_EEPROMCtrl, 0 );
371         _FlushWait(Card, 4);    // Flush + 4us
372
373         LOG("Read %x from EEPROM ofs %i", data&0xFFFF, Ofs);
374         return (data & 0xFFFF);
375 }
376
377 static void _Write8(tCard *Card, int Ofs, Uint8 Val) {
378         //LOG("%p +%i := %02x", Card, Ofs, Val);
379         outb(Card->IOBase + Ofs, Val);
380 }
381 static void _Write16(tCard *Card, int Ofs, Uint16 Val) {
382         //LOG("%p +%i := %04x", Card, Ofs, Val);
383         ASSERT( !(Ofs & 1) );
384         outw(Card->IOBase + Ofs, Val);
385 }
386 static void _Write32(tCard *Card, int Ofs, Uint32 Val) {
387         //LOG("%p +%i := %08x", Card, Ofs, Val);
388         ASSERT( !(Ofs & 3) );
389         outd(Card->IOBase + Ofs, Val);
390 }
391 static Uint8 _Read8(tCard *Card, int Ofs) {
392         Uint8 rv = inb(Card->IOBase + Ofs);
393         //LOG("%p +%i == %02x", Card, Ofs, rv);
394         return rv;
395 }
396 static Uint16 _Read16(tCard *Card, int Ofs) {
397         ASSERT( !(Ofs & 1) );
398         Uint16 rv = inw(Card->IOBase + Ofs);
399         //LOG("%p +%i == %04x", Card, Ofs, rv);
400         return rv;
401 }
402 //static Uint32 _Read32(tCard *Card, int Ofs) { return ind(Card->IOBase + Ofs); }
403
404 static void _FlushWait(tCard *Card, int Delay)
405 {
406         _Read16( Card, REG_Status );
407         if(Delay > 0)
408         {
409                 Time_MicroSleep(Delay);
410         }
411 }
412

UCC git Repository :: git.ucc.asn.au