4 * See: ~/Sources/bochs/bochs.../iodev/ne2k.cc
7 #define VERSION ((0<<8)|50)
12 #include <api_drv_network.h>
13 #include <semaphore.h>
16 #define MEM_START 0x40
18 #define RX_FIRST (MEM_START)
19 #define RX_LAST (MEM_START+RX_BUF_SIZE-1)
20 #define RX_BUF_SIZE 0x40
21 #define TX_FIRST (MEM_START+RX_BUF_SIZE)
22 #define TX_LAST (MEM_END)
23 #define TX_BUF_SIZE 0x40
24 #define MAX_PACKET_QUEUE 10
29 } csaCOMPAT_DEVICES[] = {
30 {0x10EC, 0x8029}, // Realtek 8029
31 {0x10EC, 0x8129} // Realtek 8129
33 #define NUM_COMPAT_DEVICES ((int)(sizeof(csaCOMPAT_DEVICES)/sizeof(csaCOMPAT_DEVICES[0])))
35 enum eNe2k_Page0Read {
36 CMD = 0, //!< the master command register
37 CLDA0, //!< Current Local DMA Address 0
38 CLDA1, //!< Current Local DMA Address 1
39 BNRY, //!< Boundary Pointer (for ringbuffer)
40 TSR, //!< Transmit Status Register
41 NCR, //!< collisions counter
42 FIFO, //!< (for what purpose ??)
43 ISR, //!< Interrupt Status Register
44 CRDA0, //!< Current Remote DMA Address 0
45 CRDA1, //!< Current Remote DMA Address 1
46 RSR = 0xC //!< Receive Status Register
49 enum eNe2k_Page0Write {
50 PSTART = 1, //!< page start (init only)
51 PSTOP, //!< page stop (init only)
52 TPSR = 4, //!< transmit page start address
53 TBCR0, //!< transmit byte count (low)
54 TBCR1, //!< transmit byte count (high)
55 RSAR0 = 8, //!< remote start address (lo)
56 RSAR1, //!< remote start address (hi)
57 RBCR0, //!< remote byte count (lo)
58 RBCR1, //!< remote byte count (hi)
59 RCR, //!< receive config register
60 TCR, //!< transmit config register
61 DCR, //!< data config register (init)
62 IMR //!< interrupt mask register (init)
65 enum eNe2k_Page1Read {
66 CURR = 7 //!< current page
70 typedef struct sNe2k_Card {
71 Uint16 IOBase; //!< IO Port Address from PCI
72 Uint8 IRQ; //!< IRQ Assigned from PCI
74 tSemaphore Semaphore; //!< Semaphore for incoming packets
75 int NextRXPage; //!< Next expected RX page
77 int NextMemPage; //!< Next Card Memory page to use
80 tVFS_Node Node; //!< VFS Node
81 Uint8 MacAddr[6]; //!< Cached MAC address
85 int Ne2k_Install(char **Arguments);
86 char *Ne2k_ReadDir(tVFS_Node *Node, int Pos);
87 tVFS_Node *Ne2k_FindDir(tVFS_Node *Node, const char *Name);
88 int Ne2k_IOCtl(tVFS_Node *Node, int ID, void *Data);
89 Uint64 Ne2k_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
90 Uint64 Ne2k_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
92 int Ne2k_int_ReadDMA(tCard *Card, int FirstPage, int NumPages, void *Buffer);
93 Uint8 Ne2k_int_GetWritePage(tCard *Card, Uint16 Length);
94 void Ne2k_IRQHandler(int IntNum, void *Ptr);
97 MODULE_DEFINE(0, VERSION, Ne2k, Ne2k_Install, NULL, NULL);
98 tDevFS_Driver gNe2k_DriverInfo = {
102 .ACLs = &gVFS_ACL_EveryoneRX,
103 .Flags = VFS_FFLAG_DIRECTORY,
104 .ReadDir = Ne2k_ReadDir,
105 .FindDir = Ne2k_FindDir,
109 Uint16 gNe2k_BaseAddress;
110 int giNe2k_CardCount = 0;
111 tCard *gpNe2k_Cards = NULL;
115 * \fn int Ne2k_Install(char **Options)
116 * \brief Installs the NE2000 Driver
118 int Ne2k_Install(char **Options)
124 // --- Scan PCI Bus ---
126 giNe2k_CardCount = 0;
127 for( i = 0; i < NUM_COMPAT_DEVICES; i ++ )
129 giNe2k_CardCount += PCI_CountDevices( csaCOMPAT_DEVICES[i].Vendor, csaCOMPAT_DEVICES[i].Device );
132 if( giNe2k_CardCount == 0 ) {
133 Log_Warning("Ne2k", "No cards detected");
134 return MODULE_ERR_NOTNEEDED;
139 gpNe2k_Cards = calloc( giNe2k_CardCount, sizeof(tCard) );
141 for( i = 0; i < NUM_COMPAT_DEVICES; i ++ )
143 count = PCI_CountDevices( csaCOMPAT_DEVICES[i].Vendor, csaCOMPAT_DEVICES[i].Device );
144 for( j = 0; j < count; j ++,k ++ )
146 id = PCI_GetDevice( csaCOMPAT_DEVICES[i].Vendor, csaCOMPAT_DEVICES[i].Device, j );
148 base = PCI_GetBAR( id, 0 );
149 gpNe2k_Cards[ k ].IOBase = base;
150 gpNe2k_Cards[ k ].IRQ = PCI_GetIRQ( id );
151 gpNe2k_Cards[ k ].NextMemPage = 64;
152 gpNe2k_Cards[ k ].NextRXPage = RX_FIRST;
154 // Install IRQ Handler
155 IRQ_AddHandler(gpNe2k_Cards[ k ].IRQ, Ne2k_IRQHandler, &gpNe2k_Cards[k]);
158 outb( base + 0x1F, inb(base + 0x1F) );
159 while( (inb( base+ISR ) & 0x80) == 0 );
160 outb( base + ISR, 0x80 );
163 outb( base + CMD, 0x40|0x21 ); // Page 1, No DMA, Stop
164 outb( base + CURR, RX_FIRST ); // Current RX page
165 outb( base + CMD, 0x21 ); // No DMA and Stop
166 outb( base + DCR, 0x49 ); // Set WORD mode
167 outb( base + IMR, 0x00 ); // Interrupt Mask Register
168 outb( base + ISR, 0xFF );
169 outb( base + RCR, 0x20 ); // Reciever to Monitor
170 outb( base + TCR, 0x02 ); // Transmitter OFF (TCR.LB = 1, Internal Loopback)
173 outb( base + RBCR0, 6*4 ); // Remote Byte Count
174 outb( base + RBCR1, 0 );
175 outb( base + RSAR0, 0 ); // Clear Source Address
176 outb( base + RSAR1, 0 );
177 outb( base + CMD, 0x0A ); // Remote Read, Start
178 gpNe2k_Cards[ k ].MacAddr[0] = inb(base+0x10);// inb(base+0x10);
179 gpNe2k_Cards[ k ].MacAddr[1] = inb(base+0x10);// inb(base+0x10);
180 gpNe2k_Cards[ k ].MacAddr[2] = inb(base+0x10);// inb(base+0x10);
181 gpNe2k_Cards[ k ].MacAddr[3] = inb(base+0x10);// inb(base+0x10);
182 gpNe2k_Cards[ k ].MacAddr[4] = inb(base+0x10);// inb(base+0x10);
183 gpNe2k_Cards[ k ].MacAddr[5] = inb(base+0x10);// inb(base+0x10);
185 outb( base+PSTART, RX_FIRST); // Set Receive Start
186 outb( base+BNRY, RX_LAST-1); // Set Boundary Page
187 outb( base+PSTOP, RX_LAST); // Set Stop Page
188 outb( base+ISR, 0xFF ); // Clear all ints
189 outb( base+CMD, 0x22 ); // No DMA, Start
190 outb( base+IMR, 0x3F ); // Set Interupt Mask
191 outb( base+RCR, 0x0F ); // Set WRAP and allow all packet matches
192 outb( base+TCR, 0x00 ); // Set Normal Transmitter mode
193 outb( base+TPSR, 0x40); // Set Transmit Start
195 Log_Log("Ne2k", "Card %i 0x%04x IRQ%i %02x:%02x:%02x:%02x:%02x:%02x",
196 k, base, gpNe2k_Cards[ k ].IRQ,
197 gpNe2k_Cards[k].MacAddr[0], gpNe2k_Cards[k].MacAddr[1],
198 gpNe2k_Cards[k].MacAddr[2], gpNe2k_Cards[k].MacAddr[3],
199 gpNe2k_Cards[k].MacAddr[4], gpNe2k_Cards[k].MacAddr[5]
203 gpNe2k_Cards[ k ].Name[0] = '0'+k;
204 gpNe2k_Cards[ k ].Name[1] = '\0';
205 gpNe2k_Cards[ k ].Node.ImplPtr = &gpNe2k_Cards[ k ];
206 gpNe2k_Cards[ k ].Node.NumACLs = 0; // Root Only
207 gpNe2k_Cards[ k ].Node.CTime = now();
208 gpNe2k_Cards[ k ].Node.Write = Ne2k_Write;
209 gpNe2k_Cards[ k ].Node.Read = Ne2k_Read;
210 gpNe2k_Cards[ k ].Node.IOCtl = Ne2k_IOCtl;
212 // Initialise packet semaphore
213 // - Start at zero, no max
214 Semaphore_Init( &gpNe2k_Cards[k].Semaphore, 0, 0, "NE2000", gpNe2k_Cards[ k ].Name );
218 gNe2k_DriverInfo.RootNode.Size = giNe2k_CardCount;
219 DevFS_AddDevice( &gNe2k_DriverInfo );
220 return MODULE_ERR_OK;
224 * \fn char *Ne2k_ReadDir(tVFS_Node *Node, int Pos)
226 char *Ne2k_ReadDir(tVFS_Node *Node, int Pos)
229 if(Pos < 0 || Pos >= giNe2k_CardCount) return NULL;
236 * \fn tVFS_Node *Ne2k_FindDir(tVFS_Node *Node, const char *Name)
238 tVFS_Node *Ne2k_FindDir(tVFS_Node *Node, const char *Name)
240 if(Name[0] == '\0' || Name[1] != '\0') return NULL;
242 return &gpNe2k_Cards[ Name[0]-'0' ].Node;
245 static const char *casIOCtls[] = { DRV_IOCTLNAMES, DRV_NETWORK_IOCTLNAMES, NULL };
247 * \fn int Ne2k_IOCtl(tVFS_Node *Node, int ID, void *Data)
248 * \brief IOCtl calls for a network device
250 int Ne2k_IOCtl(tVFS_Node *Node, int ID, void *Data)
252 ENTER("pNode iID pData", Node, ID, Data);
255 BASE_IOCTLS(DRV_TYPE_NETWORK, "NE2000", VERSION, casIOCtls);
258 // If this is the root, return
259 if( Node == &gNe2k_DriverInfo.RootNode ) {
264 // Device specific settings
267 case NET_IOCTL_GETMAC:
268 if( !CheckMem(Data, 6) ) {
272 memcpy( Data, ((tCard*)Node->ImplPtr)->MacAddr, 6 );
281 * \fn Uint64 Ne2k_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
282 * \brief Send a packet from the network card
284 Uint64 Ne2k_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
286 tCard *Card = (tCard*)Node->ImplPtr;
287 Uint16 *buf = Buffer;
291 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
295 // Sanity Check Length
296 if(Length > TX_BUF_SIZE*256) {
299 "Ne2k_Write - Attempting to send over TX_BUF_SIZE*256 (%i) bytes (%i)",
300 TX_BUF_SIZE*256, Length
306 // Make sure that the card is in page 0
307 outb(Card->IOBase + CMD, 0|0x22); // Page 0, Start, NoDMA
309 // Clear Remote DMA Flag
310 outb(Card->IOBase + ISR, 0x40); // Bit 6
312 // Send Size - Transfer Byte Count Register
313 outb(Card->IOBase + TBCR0, Length & 0xFF);
314 outb(Card->IOBase + TBCR1, Length >> 8);
316 // Send Size - Remote Byte Count Register
317 outb(Card->IOBase + RBCR0, Length & 0xFF);
318 outb(Card->IOBase + RBCR1, Length >> 8);
321 outb(Card->IOBase + RSAR0, 0x00); // Page Offset
322 page = Ne2k_int_GetWritePage(Card, Length);
323 outb(Card->IOBase + RSAR1, page); // Page Offset
325 outb(Card->IOBase + CMD, 0|0x10|0x2); // Page 0, Remote Write, Start
328 for(rem = Length; rem > 0; rem -= 2) {
329 outw(Card->IOBase + 0x10, *buf++);
332 while( !(inb(Card->IOBase + ISR) & 0x40) ) // Wait for Remote DMA Complete
335 outb( Card->IOBase + ISR, 0x40 ); // ACK Interrupt
338 outb(Card->IOBase + TPSR, page);
339 outb(Card->IOBase + CMD, 0|0x10|0x4|0x2);
342 //outb(Card->IOBase + CMD, 0|0x20);
349 * \fn Uint64 Ne2k_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
350 * \brief Wait for and read a packet from the network card
352 Uint64 Ne2k_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
354 tCard *Card = (tCard*)Node->ImplPtr;
359 Uint8 NextPacketPage;
360 Uint16 Length; // Little Endian
363 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
366 if( Semaphore_Wait( &Card->Semaphore, 1 ) != 1 )
368 // Error or interrupted
372 outb(Card->IOBase, 0x22 | (1 << 6)); // Page 6
373 LOG("CURR : 0x%02x", inb(Card->IOBase + CURR));
375 // Get current read page
376 page = Card->NextRXPage;
377 LOG("page = %i", page);
379 Ne2k_int_ReadDMA(Card, page, 1, data);
381 pktHdr = (void*)data;
383 LOG("pktHdr->Status = 0x%x", pktHdr->Status);
384 LOG("pktHdr->NextPacketPage = %i", pktHdr->NextPacketPage);
385 LOG("pktHdr->Length = 0x%03x", pktHdr->Length);
387 // Have we read all the required bytes yet?
388 if(pktHdr->Length < 256 - 4)
390 if(Length > pktHdr->Length)
391 Length = pktHdr->Length;
392 memcpy(Buffer, &data[4], Length);
394 // No? oh damn, now we need to allocate a buffer
397 int pages = pktHdr->NextPacketPage - page;
398 char *buf = malloc( pages*256 );
400 LOG("pktHdr->Length (%i) > 256 - 4, allocated buffer %p", pktHdr->Length, buf);
402 if(!buf) LEAVE_RET('i', -1);
404 // Copy the already read data
405 memcpy(buf, data, 256);
407 // Read all the needed pages
409 if(page == RX_LAST+1) page = RX_FIRST;
411 if( page + pages > RX_LAST )
413 int first_count = RX_LAST+1 - page;
415 tmp += Ne2k_int_ReadDMA(Card, page, first_count, buf+256);
416 tmp += Ne2k_int_ReadDMA(Card, RX_FIRST, pages-1-first_count, buf+(first_count+1)*256);
417 LOG("composite return count = %i", tmp);
420 Ne2k_int_ReadDMA(Card, page, pages-1, buf+256);
422 // Wrap length to the packet length
423 if(Length > pktHdr->Length)
424 Length = pktHdr->Length;
425 else if( Length < pktHdr->Length ) {
426 Log_Warning("NE2000", "Packet truncated! (%i bytes truncated to %i)",
427 pktHdr->Length, Length);
429 memcpy(Buffer, &buf[4], Length);
434 // Write BNRY (maximum page for incoming packets)
435 if(pktHdr->NextPacketPage == RX_FIRST)
436 outb( Card->IOBase + BNRY, RX_LAST-1 );
438 outb( Card->IOBase + BNRY, pktHdr->NextPacketPage-1 );
439 // Set next RX Page and decrement the waiting list
440 Card->NextRXPage = pktHdr->NextPacketPage;
446 int Ne2k_int_ReadDMA(tCard *Card, int FirstPage, int NumPages, void *Buffer)
451 if( !(0 <= FirstPage && FirstPage < 256) ) {
452 Log_Warning("NE2000", "Ne2k_int_ReadDMA: BUG - FirstPage(%i) not 8-bit", FirstPage);
455 if( !(0 <= NumPages && NumPages < 256) ) {
456 Log_Warning("NE2000", "Ne2k_int_ReadDMA: BUG - NumPages(%i) not 8-bit", NumPages);
460 ENTER("pCard iFirstPage iNumPages pBuffer", Card, FirstPage, NumPages, Buffer);
462 // Make sure that the card is in bank 0
463 outb(Card->IOBase + CMD, 0|0x22); // Bank 0, Start, NoDMA
464 outb(Card->IOBase + ISR, 0x40); // Clear Remote DMA Flag
467 outb(Card->IOBase + RBCR0, 0);
468 outb(Card->IOBase + RBCR1, NumPages); // page count
469 outb(Card->IOBase + RSAR0, 0x00); // Page Offset
470 outb(Card->IOBase + RSAR1, FirstPage); // Page Number
471 outb(Card->IOBase + CMD, 0|0x08|0x2); // Bank 0, Remote Read, Start
473 // TODO: Less expensive
474 //while( !(inb(Card->IOBase + ISR) & 0x40) ) {
476 // LOG("inb(ISR) = 0x%02x", inb(Card->IOBase + ISR));
478 HALT(); // Small delay?
481 for(i = 0; i < 128*NumPages; i ++)
482 ((Uint16*)Buffer)[i] = inw(Card->IOBase + 0x10);
485 outb(Card->IOBase + ISR, 0x40); // Clear Remote DMA Flag
487 LEAVE('i', NumPages);
492 * \fn Uint8 Ne2k_int_GetWritePage(tCard *Card, Uint16 Length)
494 Uint8 Ne2k_int_GetWritePage(tCard *Card, Uint16 Length)
496 Uint8 ret = Card->NextMemPage;
498 Card->NextMemPage += (Length + 0xFF) >> 8;
499 if(Card->NextMemPage >= TX_LAST) {
500 Card->NextMemPage -= TX_BUF_SIZE;
507 * \fn void Ne2k_IRQHandler(int IntNum)
509 void Ne2k_IRQHandler(int IntNum, void *Ptr)
514 if(card->IRQ != IntNum) return;
516 byte = inb( card->IOBase + ISR );
518 LOG("byte = 0x%02x", byte);
521 // Reset All (save for RDMA), that's polled
522 outb( card->IOBase + ISR, 0xFF&(~0x40) );
524 // 0: Packet recieved (no error)
527 //if( card->NumWaitingPackets > MAX_PACKET_QUEUE )
528 // card->NumWaitingPackets = MAX_PACKET_QUEUE;
529 if( Semaphore_Signal( &card->Semaphore, 1 ) != 1 ) {
533 // 1: Packet sent (no error)
534 // 2: Recieved with error
535 // 3: Transmission Halted (Excessive Collisions)
536 // 4: Recieve Buffer Exhausted
538 // 6: Remote DMA Complete