1 /////////////////////////////////////////////////////////////////////////
2 // $Id: pcipnic.cc,v 1.28 2008/07/26 08:02:27 vruppert Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2003 Fen Systems Ltd.
6 // http://www.fensystems.co.uk/
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /////////////////////////////////////////////////////////////////////////
23 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
24 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
25 // is used to know when we are exporting symbols and when we are importing.
30 #if BX_SUPPORT_PCI && BX_SUPPORT_PCIPNIC
34 #define LOG_THIS thePNICDevice->
36 bx_pcipnic_c
* thePNICDevice
= NULL
;
38 const Bit8u pnic_iomask
[16] = {2, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
40 int libpcipnic_LTX_plugin_init(plugin_t
*plugin
, plugintype_t type
, int argc
, char *argv
[])
42 thePNICDevice
= new bx_pcipnic_c();
43 BX_REGISTER_DEVICE_DEVMODEL(plugin
, type
, thePNICDevice
, BX_PLUGIN_PCIPNIC
);
47 void libpcipnic_LTX_plugin_fini(void)
52 bx_pcipnic_c::bx_pcipnic_c()
58 bx_pcipnic_c::~bx_pcipnic_c()
63 void bx_pcipnic_c::init(void)
67 // Read in values from config interface
68 base
= (bx_list_c
*) SIM
->get_param(BXPN_PNIC
);
69 memcpy(BX_PNIC_THIS s
.macaddr
, SIM
->get_param_string("macaddr", base
)->getptr(), 6);
71 BX_PNIC_THIS s
.devfunc
= 0x00;
72 DEV_register_pci_handlers(this, &BX_PNIC_THIS s
.devfunc
, BX_PLUGIN_PCIPNIC
,
73 "Experimental PCI Pseudo NIC");
75 for (unsigned i
=0; i
<256; i
++) {
76 BX_PNIC_THIS s
.pci_conf
[i
] = 0x0;
79 // This code ripped wholesale from ne2k.cc:
80 // Attach to the simulated ethernet dev
81 const char *ethmod
= SIM
->get_param_enum("ethmod", base
)->get_selected();
82 BX_PNIC_THIS ethdev
= eth_locator_c::create(ethmod
,
83 SIM
->get_param_string("ethdev", base
)->getptr(),
84 (const char *) SIM
->get_param_string("macaddr", base
)->getptr(),
87 SIM
->get_param_string("script", base
)->getptr());
89 if (BX_PNIC_THIS ethdev
== NULL
) {
90 BX_PANIC(("could not find eth module %s", ethmod
));
91 // if they continue, use null.
92 BX_INFO(("could not find eth module %s - using null instead", ethmod
));
94 BX_PNIC_THIS ethdev
= eth_locator_c::create("null", NULL
,
95 (const char *) SIM
->get_param_string("macaddr", base
)->getptr(),
98 if (BX_PNIC_THIS ethdev
== NULL
)
99 BX_PANIC(("could not locate null module"));
102 BX_PNIC_THIS s
.base_ioaddr
= 0;
104 BX_INFO(("PCI Pseudo NIC initialized - I/O base and IRQ assigned by PCI BIOS"));
107 void bx_pcipnic_c::reset(unsigned type
)
111 static const struct reset_vals_t
{
115 { 0x00, PNIC_PCI_VENDOR
& 0xff },
116 { 0x01, PNIC_PCI_VENDOR
>> 8 },
117 { 0x02, PNIC_PCI_DEVICE
& 0xff },
118 { 0x03, PNIC_PCI_DEVICE
>> 8 },
119 { 0x04, 0x05 }, { 0x05, 0x00 }, // command_io
120 { 0x06, 0x80 }, { 0x07, 0x02 }, // status
121 { 0x08, 0x01 }, // revision number
122 { 0x09, 0x00 }, // interface
123 { 0x0a, 0x00 }, // class_sub
124 { 0x0b, 0x02 }, // class_base Network Controller
125 { 0x0D, 0x20 }, // bus latency
126 { 0x0e, 0x00 }, // header_type_generic
127 // address space 0x20 - 0x23
128 { 0x20, 0x01 }, { 0x21, 0x00 },
129 { 0x22, 0x00 }, { 0x23, 0x00 },
130 { 0x3c, 0x00, }, // IRQ
131 { 0x3d, BX_PCI_INTA
}, // INT
132 { 0x6a, 0x01 }, // PNIC clock
133 { 0xc1, 0x20 } // PIRQ enable
136 for (i
= 0; i
< sizeof(reset_vals
) / sizeof(*reset_vals
); ++i
) {
137 BX_PNIC_THIS s
.pci_conf
[reset_vals
[i
].addr
] = reset_vals
[i
].val
;
140 // Set up initial register values
141 BX_PNIC_THIS s
.rCmd
= PNIC_CMD_NOOP
;
142 BX_PNIC_THIS s
.rStatus
= PNIC_STATUS_OK
;
143 BX_PNIC_THIS s
.rLength
= 0;
144 BX_PNIC_THIS s
.rDataCursor
= 0;
145 BX_PNIC_THIS s
.recvIndex
= 0;
146 BX_PNIC_THIS s
.recvQueueLength
= 0;
147 BX_PNIC_THIS s
.irqEnabled
= 0;
153 void bx_pcipnic_c::register_state(void)
158 bx_list_c
*list
= new bx_list_c(SIM
->get_bochs_root(), "pcipnic", "PCI Pseudo NIC State", 11);
159 new bx_shadow_num_c(list
, "irqEnabled", &BX_PNIC_THIS s
.irqEnabled
);
160 new bx_shadow_num_c(list
, "rCmd", &BX_PNIC_THIS s
.rCmd
);
161 new bx_shadow_num_c(list
, "rStatus", &BX_PNIC_THIS s
.rStatus
);
162 new bx_shadow_num_c(list
, "rLength", &BX_PNIC_THIS s
.rLength
);
163 new bx_shadow_num_c(list
, "rDataCursor", &BX_PNIC_THIS s
.rDataCursor
);
164 new bx_shadow_num_c(list
, "recvIndex", &BX_PNIC_THIS s
.recvIndex
);
165 new bx_shadow_num_c(list
, "recvQueueLength", &BX_PNIC_THIS s
.recvQueueLength
);
166 bx_list_c
*recvRL
= new bx_list_c(list
, "recvRingLength", PNIC_RECV_RINGS
);
167 for (i
=0; i
<PNIC_RECV_RINGS
; i
++) {
168 sprintf(name
, "%d", i
);
169 new bx_shadow_num_c(recvRL
, name
, &BX_PNIC_THIS s
.recvRingLength
[i
]);
171 new bx_shadow_data_c(list
, "rData", BX_PNIC_THIS s
.rData
, PNIC_DATA_SIZE
);
172 new bx_shadow_data_c(list
, "recvRing", (Bit8u
*)BX_PNIC_THIS s
.recvRing
, PNIC_RECV_RINGS
*PNIC_DATA_SIZE
);
173 register_pci_state(list
, BX_PNIC_THIS s
.pci_conf
);
176 void bx_pcipnic_c::after_restore_state(void)
178 if (DEV_pci_set_base_io(BX_PNIC_THIS_PTR
, read_handler
, write_handler
,
179 &BX_PNIC_THIS s
.base_ioaddr
,
180 &BX_PNIC_THIS s
.pci_conf
[0x10],
181 16, &pnic_iomask
[0], "PNIC")) {
182 BX_INFO(("new base address: 0x%04x", BX_PNIC_THIS s
.base_ioaddr
));
186 void bx_pcipnic_c::set_irq_level(bx_bool level
)
188 DEV_pci_set_irq(BX_PNIC_THIS s
.devfunc
, BX_PNIC_THIS s
.pci_conf
[0x3d], level
);
191 // static IO port read callback handler
192 // redirects to non-static class handler to avoid virtual functions
194 Bit32u
bx_pcipnic_c::read_handler(void *this_ptr
, Bit32u address
, unsigned io_len
)
196 #if !BX_USE_PCIPNIC_SMF
197 bx_pcipnic_c
*class_ptr
= (bx_pcipnic_c
*) this_ptr
;
198 return class_ptr
->read(address
, io_len
);
201 Bit32u
bx_pcipnic_c::read(Bit32u address
, unsigned io_len
)
205 #endif // !BX_USE_PCIPNIC_SMF
209 BX_DEBUG(("register read from address 0x%04x - ", (unsigned) address
));
211 offset
= address
- BX_PNIC_THIS s
.base_ioaddr
;
215 val
= BX_PNIC_THIS s
.rStatus
;
219 val
= BX_PNIC_THIS s
.rLength
;
223 if (BX_PNIC_THIS s
.rDataCursor
>= BX_PNIC_THIS s
.rLength
)
224 BX_PANIC(("PNIC read at %u, beyond end of data register array",
225 BX_PNIC_THIS s
.rDataCursor
));
226 val
= BX_PNIC_THIS s
.rData
[BX_PNIC_THIS s
.rDataCursor
++];
230 val
= 0; // keep compiler happy
231 BX_PANIC(("unsupported io read from address=0x%04x!", (unsigned) address
));
235 BX_DEBUG(("val = 0x%04x", (Bit16u
) val
));
240 // static IO port write callback handler
241 // redirects to non-static class handler to avoid virtual functions
243 void bx_pcipnic_c::write_handler(void *this_ptr
, Bit32u address
, Bit32u value
, unsigned io_len
)
245 #if !BX_USE_PCIPNIC_SMF
246 bx_pcipnic_c
*class_ptr
= (bx_pcipnic_c
*) this_ptr
;
248 class_ptr
->write(address
, value
, io_len
);
251 void bx_pcipnic_c::write(Bit32u address
, Bit32u value
, unsigned io_len
)
255 #endif // !BX_USE_PCIPNIC_SMF
258 BX_DEBUG(("register write to address 0x%04x - ", (unsigned) address
));
260 offset
= address
- BX_PNIC_THIS s
.base_ioaddr
;
264 BX_PNIC_THIS s
.rCmd
= value
;
265 BX_PNIC_THIS
exec_command();
269 if (value
> PNIC_DATA_SIZE
)
270 BX_PANIC(("PNIC bad length %u written to length register, max is %u",
271 value
, PNIC_DATA_SIZE
));
272 BX_PNIC_THIS s
.rLength
= value
;
273 BX_PNIC_THIS s
.rDataCursor
= 0;
277 if (BX_PNIC_THIS s
.rDataCursor
>= BX_PNIC_THIS s
.rLength
)
278 BX_PANIC(("PNIC write at %u, beyond end of data register array",
279 BX_PNIC_THIS s
.rDataCursor
));
280 BX_PNIC_THIS s
.rData
[BX_PNIC_THIS s
.rDataCursor
++] = value
;
284 BX_PANIC(("unsupported io write to address=0x%04x!", (unsigned) address
));
289 void bx_pcipnic_c::pnic_timer_handler(void *this_ptr
)
291 bx_pcipnic_c
*class_ptr
= (bx_pcipnic_c
*) this_ptr
;
292 class_ptr
->pnic_timer();
295 // Called once every 1ms
296 void bx_pcipnic_c::pnic_timer(void)
302 // pci configuration space read callback handler
303 Bit32u
bx_pcipnic_c::pci_read_handler(Bit8u address
, unsigned io_len
)
307 if (io_len
> 4 || io_len
== 0) {
308 BX_ERROR(("Experimental PNIC PCI read register 0x%02x, len=%u !",
309 (unsigned) address
, (unsigned) io_len
));
313 const char* pszName
= " ";
315 case 0x00: if (io_len
== 2) {
316 pszName
= "(vendor id) ";
317 } else if (io_len
== 4) {
318 pszName
= "(vendor + device) ";
321 case 0x04: if (io_len
== 2) {
322 pszName
= "(command) ";
323 } else if (io_len
== 4) {
324 pszName
= "(command+status) ";
327 case 0x08: if (io_len
== 1) {
328 pszName
= "(revision id) ";
329 } else if (io_len
== 4) {
330 pszName
= "(rev.+class code) ";
333 case 0x0c: pszName
= "(cache line size) "; break;
334 case 0x20: pszName
= "(base address) "; break;
335 case 0x28: pszName
= "(cardbus cis) "; break;
336 case 0x2c: pszName
= "(subsys. vendor+) "; break;
337 case 0x30: pszName
= "(rom base) "; break;
338 case 0x3c: pszName
= "(interrupt line+) "; break;
339 case 0x3d: pszName
= "(interrupt pin) "; break;
342 // This odd code is to display only what bytes actually were read.
347 for (unsigned i
=0; i
<io_len
; i
++) {
348 value
|= (BX_PNIC_THIS s
.pci_conf
[address
+i
] << (i
*8));
349 sprintf(szTmp2
, "%02x", (BX_PNIC_THIS s
.pci_conf
[address
+i
]));
351 strcat(szTmp
, szTmp2
);
354 BX_DEBUG(("Experimental PNIC PCI read register 0x%02x %svalue 0x%s",
355 address
, pszName
, szTmp
));
360 // pci configuration space write callback handler
361 void bx_pcipnic_c::pci_write_handler(Bit8u address
, Bit32u value
, unsigned io_len
)
363 Bit8u value8
, oldval
;
364 bx_bool baseaddr_change
= 0;
366 if (((address
>= 0x10) && (address
< 0x20)) ||
367 ((address
> 0x23) && (address
< 0x34)))
370 // This odd code is to display only what bytes actually were written.
376 for (unsigned i
=0; i
<io_len
; i
++) {
377 value8
= (value
>> (i
*8)) & 0xFF;
378 oldval
= BX_PNIC_THIS s
.pci_conf
[address
+i
];
381 case 0x05: // disallowing write to command hi-byte
382 case 0x06: // disallowing write to status lo-byte (is that expected?)
383 strcpy(szTmp2
, "..");
386 if (value8
!= oldval
) {
387 BX_INFO(("new irq line = %d", value8
));
388 BX_PNIC_THIS s
.pci_conf
[address
+i
] = value8
;
392 value8
= (value8
& 0xfc) | 0x01;
396 baseaddr_change
= (value8
!= oldval
);
398 BX_PNIC_THIS s
.pci_conf
[address
+i
] = value8
;
399 sprintf(szTmp2
, "%02x", value8
);
402 strcat(szTmp
, szTmp2
);
404 if (baseaddr_change
) {
405 if (DEV_pci_set_base_io(BX_PNIC_THIS_PTR
, read_handler
, write_handler
,
406 &BX_PNIC_THIS s
.base_ioaddr
,
407 &BX_PNIC_THIS s
.pci_conf
[0x20],
408 16, &pnic_iomask
[0], "PNIC")) {
409 BX_INFO(("new base address: 0x%04x", BX_PNIC_THIS s
.base_ioaddr
));
414 BX_DEBUG(("Experimental PNIC PCI write register 0x%02x value 0x%s", address
, szTmp
));
419 * Execute a hardware command.
421 void bx_pcipnic_c::exec_command(void)
423 Bit16u command
= BX_PNIC_THIS s
.rCmd
;
424 Bit16u ilength
= BX_PNIC_THIS s
.rLength
;
425 Bit8u
*data
= BX_PNIC_THIS s
.rData
;
426 // Default return values
427 Bit16u status
= PNIC_STATUS_UNKNOWN_CMD
;
430 if (ilength
!= BX_PNIC_THIS s
.rDataCursor
)
431 BX_PANIC(("PNIC command issued with incomplete data (should be %u, is %u)",
432 ilength
, BX_PNIC_THIS s
.rDataCursor
));
436 status
= PNIC_STATUS_OK
;
439 case PNIC_CMD_API_VER
:
442 api_version
= PNIC_API_VERSION
;
443 olength
= sizeof(api_version
);
444 memcpy (data
, &api_version
, sizeof(api_version
));
445 status
= PNIC_STATUS_OK
;
448 case PNIC_CMD_READ_MAC
:
449 olength
= sizeof (BX_PNIC_THIS s
.macaddr
);
450 memcpy (data
, BX_PNIC_THIS s
.macaddr
, olength
);
451 status
= PNIC_STATUS_OK
;
455 /* Flush the receive queue */
456 BX_PNIC_THIS s
.recvQueueLength
= 0;
457 status
= PNIC_STATUS_OK
;
461 BX_PNIC_THIS ethdev
->sendpkt(data
, ilength
);
462 if (BX_PNIC_THIS s
.irqEnabled
) {
465 status
= PNIC_STATUS_OK
;
469 if (BX_PNIC_THIS s
.recvQueueLength
> 0) {
470 int idx
= (BX_PNIC_THIS s
.recvIndex
- BX_PNIC_THIS s
.recvQueueLength
471 + PNIC_RECV_RINGS
) % PNIC_RECV_RINGS
;
472 olength
= BX_PNIC_THIS s
.recvRingLength
[idx
];
473 memcpy (data
, BX_PNIC_THIS s
.recvRing
[idx
], olength
);
474 BX_PNIC_THIS s
.recvQueueLength
--;
476 if (! BX_PNIC_THIS s
.recvQueueLength
) {
479 status
= PNIC_STATUS_OK
;
482 case PNIC_CMD_RECV_QLEN
:
485 qlen
= BX_PNIC_THIS s
.recvQueueLength
;
486 olength
= sizeof(qlen
);
487 memcpy (data
, &qlen
, sizeof(qlen
));
488 status
= PNIC_STATUS_OK
;
491 case PNIC_CMD_MASK_IRQ
:
494 enabled
= *((Bit8u
*)data
);
495 BX_PNIC_THIS s
.irqEnabled
= enabled
;
496 if (enabled
&& BX_PNIC_THIS s
.recvQueueLength
) {
501 status
= PNIC_STATUS_OK
;
504 case PNIC_CMD_FORCE_IRQ
:
506 status
= PNIC_STATUS_OK
;
510 BX_ERROR(("Unknown PNIC command %x (data length %u)", command
, ilength
));
511 status
= PNIC_STATUS_UNKNOWN_CMD
;
516 BX_PNIC_THIS s
.rStatus
= status
;
517 BX_PNIC_THIS s
.rLength
= olength
;
518 BX_PNIC_THIS s
.rDataCursor
= 0;
522 * Callback from the eth system driver when a frame has arrived
524 void bx_pcipnic_c::rx_handler(void *arg
, const void *buf
, unsigned len
)
526 // BX_DEBUG(("rx_handler with length %d", len));
527 bx_pcipnic_c
*class_ptr
= (bx_pcipnic_c
*) arg
;
528 class_ptr
->rx_frame(buf
, len
);
532 * rx_frame() - called by the platform-specific code when an
533 * ethernet frame has been received. The destination address
534 * is tested to see if it should be accepted, and if the
535 * rx ring has enough room, it is copied into it and
536 * the receive process is updated
538 void bx_pcipnic_c::rx_frame(const void *buf
, unsigned io_len
)
540 // Check packet length
541 if (io_len
> PNIC_DATA_SIZE
) {
542 BX_PANIC(("PNIC receive: data size %u exceeded buffer size %u",
543 io_len
, PNIC_DATA_SIZE
));
544 // Truncate if user continues
545 io_len
= PNIC_DATA_SIZE
;
547 // Check receive ring is not full
548 if (BX_PNIC_THIS s
.recvQueueLength
== PNIC_RECV_RINGS
) {
549 BX_ERROR(("PNIC receive: receive ring full, discarding packet"));
552 // Copy data to receive ring and record length
553 memcpy (BX_PNIC_THIS s
.recvRing
[BX_PNIC_THIS s
.recvIndex
], buf
, io_len
);
554 BX_PNIC_THIS s
.recvRingLength
[BX_PNIC_THIS s
.recvIndex
] = io_len
;
555 // Move to next ring entry
556 BX_PNIC_THIS s
.recvIndex
= (BX_PNIC_THIS s
.recvIndex
+ 1) % PNIC_RECV_RINGS
;
557 BX_PNIC_THIS s
.recvQueueLength
++;
559 // Generate interrupt if enabled
560 if (BX_PNIC_THIS s
.irqEnabled
) {
565 #endif // BX_SUPPORT_PCI && BX_SUPPORT_PCIPNIC