forked from GNS3/dynamips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_c2691_iofpga.c
484 lines (417 loc) · 11.8 KB
/
dev_c2691_iofpga.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Cisco router simulation platform.
* Copyright (c) 2006 Christophe Fillot ([email protected])
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <termios.h>
#include <fcntl.h>
#include <pthread.h>
#include "ptask.h"
#include "cpu.h"
#include "vm.h"
#include "dynamips.h"
#include "memory.h"
#include "device.h"
#include "dev_vtty.h"
#include "nmc93cX6.h"
#include "dev_c2691.h"
/* Debugging flags */
#define DEBUG_UNKNOWN 1
#define DEBUG_ACCESS 0
#define DEBUG_NET_IRQ 0
/* Definitions for Mainboard EEPROM */
#define EEPROM_MB_DOUT 3
#define EEPROM_MB_DIN 2
#define EEPROM_MB_CLK 1
#define EEPROM_MB_CS 0
/* Definitions for Network Modules EEPROM */
#define EEPROM_NM_DOUT 7
#define EEPROM_NM_DIN 6
#define EEPROM_NM_CLK 2
#define EEPROM_NM_CS 4
/* Network IRQ distribution */
struct net_irq_distrib {
u_int reg;
u_int offset;
};
static struct net_irq_distrib net_irq_dist[C2691_MAX_NM_BAYS] = {
{ 0, 0 }, /* Slot 0: reg 0x26, 0x000000XX */
{ 1, 0 }, /* Slot 1: reg 0x28, 0x000000XX */
};
/* IO FPGA structure */
struct c2691_iofpga_data {
vm_obj_t vm_obj;
struct vdevice dev;
c2691_t *router;
/* Network IRQ status */
m_uint16_t net_irq_status[2];
/* Interrupt mask */
m_uint16_t intr_mask;
/* WIC select */
u_int wic_select;
u_int wic_cmd_pos;
u_int wic_cmd_valid;
m_uint16_t wic_cmd[2];
};
/* Mainboard EEPROM definition */
static const struct nmc93cX6_eeprom_def eeprom_mb_def = {
EEPROM_MB_CLK, EEPROM_MB_CS,
EEPROM_MB_DIN, EEPROM_MB_DOUT,
};
/* Mainboard EEPROM */
static const struct nmc93cX6_group eeprom_mb_group = {
EEPROM_TYPE_NMC93C46, 1, 0,
EEPROM_DORD_NORMAL,
EEPROM_DOUT_HIGH,
EEPROM_DEBUG_DISABLED,
"Mainboard EEPROM",
{ &eeprom_mb_def },
};
/* NM EEPROM definition */
static const struct nmc93cX6_eeprom_def eeprom_nm_def = {
EEPROM_NM_CLK, EEPROM_NM_CS,
EEPROM_NM_DIN, EEPROM_NM_DOUT,
};
/* NM EEPROM */
static const struct nmc93cX6_group eeprom_nm_group = {
EEPROM_TYPE_NMC93C46, 1, 0,
EEPROM_DORD_NORMAL,
EEPROM_DOUT_HIGH,
EEPROM_DEBUG_DISABLED,
"NM EEPROM",
{ &eeprom_nm_def },
};
/* Update network interrupt status */
static inline void dev_c2691_iofpga_net_update_irq(struct c2691_iofpga_data *d)
{
if ((d->net_irq_status[0] != 0xFFFF) || (d->net_irq_status[1] != 0xFFFF)) {
vm_set_irq(d->router->vm,C2691_NETIO_IRQ);
} else {
vm_clear_irq(d->router->vm,C2691_NETIO_IRQ);
}
}
/* Trigger a Network IRQ for the specified slot/port */
void dev_c2691_iofpga_net_set_irq(struct c2691_iofpga_data *d,
u_int slot,u_int port)
{
struct net_irq_distrib *irq_dist;
#if DEBUG_NET_IRQ
vm_log(d->router->vm,"IO_FPGA","setting NetIRQ for slot %u port %u\n",
slot,port);
#endif
irq_dist = &net_irq_dist[slot];
d->net_irq_status[irq_dist->reg] &= ~(1 << (irq_dist->offset + port));
dev_c2691_iofpga_net_update_irq(d);
}
/* Clear a Network IRQ for the specified slot/port */
void dev_c2691_iofpga_net_clear_irq(struct c2691_iofpga_data *d,
u_int slot,u_int port)
{
struct net_irq_distrib *irq_dist;
#if DEBUG_NET_IRQ
vm_log(d->router->vm,"IO_FPGA","clearing NetIRQ for slot %u port %u\n",
slot,port);
#endif
irq_dist = &net_irq_dist[slot];
d->net_irq_status[irq_dist->reg] |= (1 << (irq_dist->offset + port));
dev_c2691_iofpga_net_update_irq(d);
}
/* Read a WIC EEPROM */
static m_uint16_t dev_c2691_read_wic_eeprom(struct c2691_iofpga_data *d)
{
struct cisco_eeprom *eeprom;
u_int wic_port;
u_int eeprom_offset;
m_uint8_t val[2];
switch(d->wic_select) {
case 0x1700:
wic_port = 0x10;
break;
case 0x1D00:
wic_port = 0x20;
break;
case 0x3500:
wic_port = 0x30;
break;
default:
wic_port = 0;
}
/* No WIC in slot or no EEPROM: fake an empty EEPROM */
if (!wic_port || !(eeprom = vm_slot_get_eeprom(d->router->vm,0,wic_port)))
return(0xFFFF);
/* EEPROM offset is in the lowest 6 bits */
eeprom_offset = d->wic_cmd[0] & 0x3F;
cisco_eeprom_get_byte(eeprom,eeprom_offset,&val[0]);
cisco_eeprom_get_byte(eeprom,eeprom_offset+1,&val[1]);
return(((m_uint16_t)val[0] << 8) | val[1]);
}
/*
* dev_c2691_iofpga_access()
*/
static void *
dev_c2691_iofpga_access(cpu_gen_t *cpu,struct vdevice *dev,
m_uint32_t offset,u_int op_size,u_int op_type,
m_uint64_t *data)
{
struct c2691_iofpga_data *d = dev->priv_data;
if (op_type == MTS_READ)
*data = 0x0;
#if DEBUG_ACCESS
if (op_type == MTS_READ) {
cpu_log(cpu,"IO_FPGA","reading reg 0x%x at pc=0x%llx (size=%u)\n",
offset,cpu_get_pc(cpu),op_size);
} else {
cpu_log(cpu,"IO_FPGA",
"writing reg 0x%x at pc=0x%llx, data=0x%llx (size=%u)\n",
offset,cpu_get_pc(cpu),*data,op_size);
}
#endif
switch(offset) {
/*
* Platform type ?
* 0x04 and 0x05 seem to work.
*/
case 0x36:
if (op_type == MTS_READ)
*data = 0x04 << 5;
break;
/* Mainboard EEPROM */
case 0x0e:
if (op_type == MTS_WRITE)
nmc93cX6_write(&d->router->mb_eeprom_group,(u_int)(*data));
else
*data = nmc93cX6_read(&d->router->mb_eeprom_group);
break;
case 0x12:
/*
* Bit 0: 1=No WIC in slot 0.
* Bit 1: 1=No WIC in slot 1.
* Bit 2: 1=No WIC in slot 2.
*/
if (op_type == MTS_READ) {
*data = 0xFFFF;
/* check WIC 0 */
if (vm_slot_check_eeprom(d->router->vm,0,0x10))
*data &= ~0x01;
/* check WIC 1 */
if (vm_slot_check_eeprom(d->router->vm,0,0x20))
*data &= ~0x02;
/* check WIC 2 */
if (vm_slot_check_eeprom(d->router->vm,0,0x30))
*data &= ~0x04;
} else {
d->wic_select = *data;
}
break;
case 0x14:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
case 0x18:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
/* wic/vwic related */
case 0x40:
if (op_type == MTS_READ)
*data = 0x0004;
break;
/* WIC related: 16-bit data */
case 0x42:
if (op_type == MTS_READ) {
if (d->wic_cmd_valid) {
*data = dev_c2691_read_wic_eeprom(d);
d->wic_cmd_valid = FALSE;
} else {
*data = 0xFFFF;
}
} else {
/*
* Store the EEPROM command (in 2 words).
*
* For a read, we have:
* Word 0: 0x180 (nmc93c46 READ) + offset (6-bits).
* Word 1: 0 (no data).
*/
d->wic_cmd[d->wic_cmd_pos++] = *data;
if (d->wic_cmd_pos == 2) {
d->wic_cmd_pos = 0;
d->wic_cmd_valid = TRUE;
}
}
break;
/* NM Slot 1 EEPROM */
case 0x44:
if (op_type == MTS_WRITE)
nmc93cX6_write(&d->router->nm_eeprom_group,(u_int)(*data));
else
*data = nmc93cX6_read(&d->router->nm_eeprom_group);
break;
/* AIM EEPROM #0 */
case 0x48:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
/* AIM EEPROM #1 */
case 0x4a:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
/*
* NM Presence.
*
* Bit 7: 0=NM present in slot 1.
* Other bits unknown.
*/
case 0x20:
if (op_type == MTS_READ) {
*data = 0xFFFF;
if (vm_slot_get_card_ptr(d->router->vm,1))
*data &= ~0x08;
}
break;
/* ??? */
case 0x24:
break;
/* Intr Mask (sh platform) */
case 0x30:
if (op_type == MTS_READ)
*data = d->intr_mask;
else
d->intr_mask = *data;
break;
/*
* Network interrupt status.
*
* Bit 0: 0 = GT96100 Ethernet ports.
* Other bits unknown.
*/
case 0x26:
if (op_type == MTS_READ)
*data = d->net_irq_status[0];
break;
/*
* Network interrupt status.
*
* Bit 0: 0 = NM in Slot 1.
* Other bits unknown.
*/
case 0x28:
if (op_type == MTS_READ)
*data = d->net_irq_status[1];
break;
case 0x2c:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
/* OIR interrupt but not supported (IRQ 6) */
case 0x2e:
if (op_type == MTS_READ)
*data = 0xFFFF;
break;
/*
* Environmental monitor, determined with "sh env all".
*
* Bit 0: 1 = Fan Error
* Bit 1: 1 = Fan Error
* Bit 2: 1 = Over-temperature
* Bit 3: ???
* Bit 4: 0 = RPS present.
* Bit 5: 0 = Input Voltage status failure.
* Bit 6: 1 = Thermal status failure.
* Bit 7: 1 = DC Output Voltage status failure.
*/
case 0x3a:
if (op_type == MTS_READ)
*data = 0x0020;
break;
/*
* Bit 0: Slot0 Compact Flash presence.
* Bit 1: System Compact Flash presence.
*/
case 0x3c:
if (op_type == MTS_READ) {
*data = 0xFFFF;
/* System Flash ? */
if (cpu->vm->pcmcia_disk_size[0])
*data &= ~0x02;
/* Slot0 Flash ? */
if (cpu->vm->pcmcia_disk_size[1])
*data &= ~0x01;
}
break;
#if DEBUG_UNKNOWN
default:
if (op_type == MTS_READ) {
cpu_log(cpu,"IO_FPGA",
"read from unknown addr 0x%x, pc=0x%llx (size=%u)\n",
offset,cpu_get_pc(cpu),op_size);
} else {
cpu_log(cpu,"IO_FPGA",
"write to unknown addr 0x%x, value=0x%llx, "
"pc=0x%llx (size=%u)\n",
offset,*data,cpu_get_pc(cpu),op_size);
}
#endif
}
return NULL;
}
/* Initialize EEPROM groups */
void c2691_init_eeprom_groups(c2691_t *router)
{
/* Initialize Mainboard EEPROM */
router->mb_eeprom_group = eeprom_mb_group;
router->mb_eeprom_group.eeprom[0] = &router->mb_eeprom;
router->mb_eeprom.data = NULL;
router->mb_eeprom.len = 0;
/* EEPROM for NM slot 1 */
router->nm_eeprom_group = eeprom_nm_group;
router->nm_eeprom_group.eeprom[0] = NULL;
}
/* Shutdown the IO FPGA device */
static void
dev_c2691_iofpga_shutdown(vm_instance_t *vm,struct c2691_iofpga_data *d)
{
if (d != NULL) {
/* Remove the device */
dev_remove(vm,&d->dev);
/* Free the structure itself */
free(d);
}
}
/*
* dev_c2691_iofpga_init()
*/
int dev_c2691_iofpga_init(c2691_t *router,m_uint64_t paddr,m_uint32_t len)
{
vm_instance_t *vm = router->vm;
struct c2691_iofpga_data *d;
/* Allocate private data structure */
if (!(d = malloc(sizeof(*d)))) {
fprintf(stderr,"IO_FPGA: out of memory\n");
return(-1);
}
memset(d,0,sizeof(*d));
d->router = router;
d->net_irq_status[0] = 0xFFFF;
d->net_irq_status[1] = 0xFFFF;
vm_object_init(&d->vm_obj);
d->vm_obj.name = "io_fpga";
d->vm_obj.data = d;
d->vm_obj.shutdown = (vm_shutdown_t)dev_c2691_iofpga_shutdown;
/* Set device properties */
dev_init(&d->dev);
d->dev.name = "io_fpga";
d->dev.phys_addr = paddr;
d->dev.phys_len = len;
d->dev.priv_data = d;
d->dev.handler = dev_c2691_iofpga_access;
/* Map this device to the VM */
vm_bind_device(router->vm,&d->dev);
vm_object_add(vm,&d->vm_obj);
return(0);
}