1 #include <geekos/ne2k.h>
2 #include <geekos/debug.h>
4 #include <geekos/irq.h>
5 #include <geekos/malloc.h>
6 #include <geekos/string.h>
9 #define TX_START_BUFF 0x40
10 #define RX_START_BUFF 0x4c
11 #define RX_END_BUFF 0x80
13 uint_t next = (RX_START_BUFF << 8);
14 static uint_t received = 0;
15 static uint_t send_done = 1;
18 int (*packet_received)(struct NE2K_Packet_Info *info, uchar_t *packet);
22 static void Dump_Registers()
25 PrintBoth("Dumping NIC registers for page %x...\n", (In_Byte(NE2K_CR) & 0xc0) >> 6);
27 for(i = 0; i <= 0x0f; i += 0x01) {
28 data = In_Byte(NE2K_BASE_ADDR+i);
29 PrintBoth("\t%x: %x\n", NE2K_BASE_ADDR + i, data);
34 static int NE2K_Transmit(struct NE2K_REGS *regs)
39 struct _CR * cr = (struct _CR*)&(regs->cr);
40 uint_t packet_size = 80;
42 cr->stp = 0x0; //toggle start on
44 Out_Byte(NE2K_CR, regs->cr);
46 // Read-before-write bug fix?
47 Out_Byte(NE2K_RBCR0, 0x42);
48 Out_Byte(NE2K_RBCR1, 0x00);
49 Out_Byte(NE2K_RSAR0, 0x42);
50 Out_Byte(NE2K_RSAR1, 0x00);
52 cr->rd = 0x01; // set remote DMA to 'remote read'
53 Out_Byte(NE2K_CR, regs->cr);
55 regs->isr = 0x40; // clear and set Remote DMA high
56 Out_Byte(NE2K_ISR, regs->isr);
58 Out_Byte(NE2K_RBCR0, packet_size);
59 Out_Byte(NE2K_RBCR1, 0x00);
61 Out_Byte(NE2K_TBCR0, packet_size);
62 Out_Byte(NE2K_TBCR1, 0x00);
64 Out_Byte(NE2K_RSAR0, 0x00);
65 Out_Byte(NE2K_RSAR1, 0x40);
68 Out_Byte(NE2K_CR, regs->cr);
70 /* Begin pushing the packet into the dataport (located at 0x10 from the base address) */
71 /* Destination address = 52:54:00:12:34:56 */
72 Out_Word(NE2K_CR+0x10, 0x5452);
73 Out_Word(NE2K_CR+0x10, 0x1200);
74 Out_Word(NE2K_CR+0x10, 0x5634);
76 /* Source address = 52:54:00:12:34:58 */
77 Out_Word(NE2K_CR+0x10, 0x5452);
78 Out_Word(NE2K_CR+0x10, 0x1200);
79 Out_Word(NE2K_CR+0x10, 0x5834);
81 /* Type length and data; currently random data */
84 for(i = 1; i <= packet_size/2-12; i++, n+=2) {
85 //Out_Word(NE2K_CR+0x10, 0x0f0b);
86 Out_Word(NE2K_CR+0x10, (n<<8) | (n+1));
90 //Out_Byte(NE2K_ISR, regs->isr); /* Do we need this here? */
95 static void NE2K_Interrupt_Handler(struct Interrupt_State * state)
98 PrintBoth("NIC Interrupt Occured!\n");
99 uchar_t isr_content = In_Byte(NE2K_ISR);
101 PrintBoth("Contents of ISR: %x\n", isr_content);
103 if(isr_content & 0x01) /* A packet has been received. */
106 Out_Byte(NE2K_CR, 0x4a); /* Page 1 */
107 current = In_Byte(NE2K_CURR);
108 Out_Byte(NE2K_CR, 0x0a); /* Page 0 */
111 /* When CURR equals BNRY, all packets in the receive ring buffer have been read, and
112 the packet received bit in the interrupt status register can be cleared. */
113 if(current == In_Byte(NE2K_BNRY))
114 Out_Byte(NE2K_ISR, 0x01);
118 if(isr_content & 0x02) /* A packet has been successfully transmitted. */
121 Out_Byte(NE2K_ISR, 0x02);
124 //Out_Byte(NE2K_ISR, 0xff); /* Clear all interrupts. */
127 int Init_Ne2k(int (*rcvd_fn)(struct NE2K_Packet_Info *info, uchar_t *packet))
129 callbacks.packet_received = rcvd_fn;
131 PrintBoth("Initializing network card...\n");
132 Out_Byte(NE2K_CR+0x1f, In_Byte(NE2K_CR+0x1f)); /* Reset? */
134 struct NE2K_REGS* regs = Malloc(sizeof(struct NE2K_REGS));
135 struct _CR * cr = (struct _CR *)&(regs->cr);
136 struct _RCR * rcr = (struct _RCR*)&(regs->rcr);
137 struct _IMR * imr = (struct _IMR *)&(regs->imr);
140 regs->dcr = 0x49; /* Word-wide DMA transfer. */
141 regs->isr = 0xff; /* Clear all interrupts. */
142 regs->rcr = 0x20; /* Accept packets shorter than 64 bytes. */
143 regs->tcr = 0x02; /* Internal loopback mode. */
145 Out_Byte(NE2K_CR, regs->cr);
146 Out_Byte(NE2K_DCR, regs->dcr);
147 Out_Byte(NE2K_ISR, regs->isr);
148 Out_Byte(NE2K_RCR, regs->rcr);
149 Out_Byte(NE2K_TCR, regs->tcr);
150 Out_Byte(NE2K_IMR, regs->imr);
152 /* Remote byte count registers. */
153 Out_Byte(NE2K_RBCR0, 0x00);
154 Out_Byte(NE2K_RBCR1, 0x00);
155 /* Remote start address registers. */
156 Out_Byte(NE2K_RSAR0, 0x00);
157 Out_Byte(NE2K_RSAR1, 0x00);
159 Out_Byte(NE2K_TPSR, TX_START_BUFF); /* Transmit page start register */
160 Out_Byte(NE2K_PSTART, RX_START_BUFF); /* Page start register */
161 Out_Byte(NE2K_PSTOP, RX_END_BUFF); /* Page stop register */
162 Out_Byte(NE2K_BNRY, RX_START_BUFF); /* Boundary register */
164 cr->ps = 0x01; /* Switch to reg page 1. */
165 Out_Byte(NE2K_CR, regs->cr);
166 /* Current page register: points to first free page that can be used for packet reception. */
167 Out_Byte(NE2K_CURR, RX_START_BUFF);
168 cr->ps = 0x00; /* Switch to page 0 */
170 Out_Byte(NE2K_CR, regs->cr);
171 Out_Byte(NE2K_ISR, regs->isr);
173 /* Interrupt mask register: setting a bit to 1 enables the
174 corresponding interrupt in ISR. */
181 Out_Byte(NE2K_IMR, regs->imr);
183 cr->ps = 0x01; /* Switch to reg page 1 */
184 Out_Byte(NE2K_CR, regs->cr);
186 /* Set the physical address of the card to 52:54:00:12:34:58 */
187 Out_Byte(NE2K_CR+0x01, 0x52);
188 Out_Byte(NE2K_CR+0x02, 0x54);
189 Out_Byte(NE2K_CR+0x03, 0x00);
190 Out_Byte(NE2K_CR+0x04, 0x12);
191 Out_Byte(NE2K_CR+0x05, 0x34);
192 Out_Byte(NE2K_CR+0x06, 0x58);
194 /* Set the multicast address register to all 1s; accepts all multicast packets */
196 for(i = 0x08; i <= 0x0f; i++) {
197 Out_Byte(NE2K_CR+i, 0xff);
200 regs->cr = 0x21; //set CR to start value
201 Out_Byte(NE2K_CR, regs->cr);
204 Out_Byte(NE2K_TCR, regs->tcr);
210 rcr->pro = 0x1; /* Promiscuous mode: accept all packets. */
212 Out_Byte(NE2K_RCR, regs->rcr);
214 cr->sta = 0x1; // toggle start bit
216 Out_Byte(NE2K_CR, regs->cr);
222 Out_Byte(NE2K_CR, regs->cr);
226 Out_Byte(NE2K_CR, regs->cr);
230 Out_Byte(NE2K_CR, regs->cr);
233 Install_IRQ(NE2K_IRQ, NE2K_Interrupt_Handler);
234 Enable_IRQ(NE2K_IRQ);
236 for(i = 0; i < 0; i++)
239 PrintBoth("Transmitting a packet\n");
242 uchar_t src_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x58 };
243 uchar_t dest_addr[6] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x56 };
246 uchar_t *data = Malloc(size);
247 data = "This is a 64-byte string that will be used to test transmission.";
249 for(i = 0; i < 0; i++) {
250 NE2K_Send(regs, src_addr, dest_addr, 0x01, data, size);
253 //Free(data); // Why does this crash?
258 /* Assumes src and dest are arrays of 6 characters. */
259 int NE2K_Send(struct NE2K_REGS *regs, uchar_t src[], uchar_t dest[], uint_t type, uchar_t *data, uint_t size)
261 struct _CR * cr = (struct _CR*)&(regs->cr);
262 uint_t packet_size = size + 16;
264 cr->stp = 0x0; //toggle start on
266 Out_Byte(NE2K_CR, regs->cr);
268 // Read-before-write bug fix?
269 Out_Byte(NE2K_RBCR0, 0x42);
270 Out_Byte(NE2K_RBCR1, 0x00);
271 Out_Byte(NE2K_RSAR0, 0x42);
272 Out_Byte(NE2K_RSAR1, 0x00);
274 cr->rd = 0x01; // set remote DMA to 'remote read'
275 Out_Byte(NE2K_CR, regs->cr);
277 regs->isr = 0x40; // clear and set Remote DMA high
278 Out_Byte(NE2K_ISR, regs->isr);
280 /* Set remote byte count registers */
281 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
282 Out_Byte(NE2K_RBCR1, (packet_size >> 8) & 0xff);
284 /* Set transmit byte count registers. */
285 Out_Byte(NE2K_TBCR0, packet_size & 0xff);
286 Out_Byte(NE2K_TBCR1, (packet_size >> 8) & 0xff);
288 Out_Byte(NE2K_RSAR0, 0x00);
289 Out_Byte(NE2K_RSAR1, TX_START_BUFF);
292 Out_Byte(NE2K_CR, regs->cr);
294 /* Destination Address */
295 Out_Word(NE2K_CR + 0x10, (dest[1] << 8) | dest[0]);
296 Out_Word(NE2K_CR + 0x10, (dest[3] << 8) | dest[2]);
297 Out_Word(NE2K_CR + 0x10, (dest[5] << 8) | dest[4]);
300 Out_Word(NE2K_CR + 0x10, (src[1] << 8) | src[0]);
301 Out_Word(NE2K_CR + 0x10, (src[3] << 8) | src[2]);
302 Out_Word(NE2K_CR + 0x10, (src[5] << 8) | src[4]);
305 Out_Word(NE2K_CR + 0x10, packet_size);
308 for(i = 0; i < size; i += 2) {
309 Out_Word(NE2K_CR + 0x10, (*(data + i + 1) << 8) | *(data + i));
317 PrintBoth("Packet Received\n");
319 Out_Byte(NE2K_CR, 0x22);
320 /* Set RSAR to the start address of the received packet. */
321 Out_Byte(NE2K_RSAR0, next & 0xff);
322 Out_Byte(NE2K_RSAR1, next >> 8);
323 Out_Byte(NE2K_CR, 0x0a);
326 * A four byte header is added to the beginning of each received packet by the NIC.
327 * The first byte is the location of the next packet in the ring buffer.
328 * The second byte is the receive status code.
329 * The third and fourth bytes are the size of the packet.
334 PrintBoth("\nPacket data:\n\t");
336 data = In_Word(NE2K_CR + 0x10);
337 PrintBoth("%x ", data);
339 /* Get the location of the next packet */
340 next = data & 0xff00;
342 /* Retrieve the packet size from the header, and store it in RBCR. */
343 uint_t packet_size = In_Word(NE2K_CR + 0x10) - 4;
344 uchar_t *packet = Malloc(packet_size);
345 Out_Byte(NE2K_RBCR0, packet_size & 0xff);
346 Out_Byte(NE2K_RBCR1, (packet_size>>8) & 0xff);
349 PrintBoth("packetsize = %x\n\t", packet_size);
352 /* Copy the received packet over from the ring buffer. */
353 for(i = 0; i < packet_size; i+=2) {
354 data = In_Word(NE2K_CR + 0x10);
355 *(packet + i) = data & 0x00ff;
356 *(packet + i + 1) = (data & 0xff00) >> 8;
358 PrintBoth("BNRY = %x\n", In_Byte(NE2K_BNRY));
359 Out_Byte(NE2K_CR, 0x4a);
360 PrintBoth("CURR = %x\n", In_Byte(NE2K_CURR));
361 Out_Byte(NE2K_CR, 0x0a);
365 //Out_Byte(NE2K_RBCR0, (In_Byte(NE2K_RBCR0))-2);
366 //Out_Byte(NE2K_RSAR0, (In_Byte(NE2K_RSAR0))+2);
368 PrintBoth("\n%d packets have been received", ++received);
371 Out_Byte(NE2K_ISR, 0x40); /* Clear the remote DMA complete interrupt. */
373 /* The BNRY register stores the location of the first packet that hasn't been read yet */
374 Out_Byte(NE2K_BNRY, next >> 8);
376 struct NE2K_Packet_Info *info = Malloc(sizeof(struct NE2K_Packet_Info));
377 info->size = packet_size;
379 memcpy(info->dest, packet, 6);
380 memcpy(info->src, packet + 6, 6);
381 callbacks.packet_received(info, packet);