Debian Codegraph @

common/dev_c3745.c

raw
1 /*
2 * Cisco 3745 simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 * Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net)
5 *
6 * Generic Cisco 3745 routines and definitions (EEPROM,...).
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <assert.h>
15
16 #include "cpu.h"
17 #include "vm.h"
18 #include "dynamips.h"
19 #include "memory.h"
20 #include "device.h"
21 #include "pci_io.h"
22 #include "dev_gt.h"
23 #include "cisco_eeprom.h"
24 #include "dev_rom.h"
25 #include "dev_c3745.h"
26 #include "dev_c3745_iofpga.h"
27 #include "dev_vtty.h"
28 #include "registry.h"
29 #include "fs_nvram.h"
30
31 /* ======================================================================== */
32 /* EEPROM definitions */
33 /* ======================================================================== */
34
35 /* Cisco 3745 motherboard EEPROM */
36 static m_uint16_t eeprom_c3745_motherboard_data[] = {
37 0x04FF, 0xC18B, 0x5858, 0x5858, 0x5858, 0x5858, 0x5858, 0x5809,
38 0x6940, 0x02F7, 0xC046, 0x0320, 0x003E, 0x3E03, 0x4241, 0x3085,
39 0x1C12, 0x4004, 0x80FF, 0xFFFF, 0xFFC4, 0x08FF, 0xFFFF, 0xFFFF,
40 0xFFFF, 0xFF81, 0x0000, 0x0000, 0x0400, 0x0300, 0xC508, 0xFFFF,
41 0xFFFF, 0xFFFF, 0xFFFF, 0x4102, 0x0002, 0x04C2, 0x8B58, 0x5858,
42 0x5858, 0x5858, 0x5858, 0x5858, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
43 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
44 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
45 };
46
47 struct cisco_eeprom eeprom_c3745_motherboard = {
48 "C3745 Motherboard",
49 eeprom_c3745_motherboard_data,
50 sizeof(eeprom_c3745_motherboard_data)/2,
51 };
52
53 /* Cisco 3745 I/O board EEPROM */
54 static m_uint16_t eeprom_c3745_ioboard_data[] = {
55 0x04FF, 0x4002, 0xF841, 0x0200, 0xC046, 0x0320, 0x0038, 0x7E01,
56 0x4242, 0x3080, 0x0000, 0x0000, 0x0203, 0xC18B, 0x5858, 0x5858,
57 0x5858, 0x5858, 0x5858, 0x5803, 0x0081, 0x0000, 0x0000, 0x0400,
58 0xC809, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFC2, 0x8B58, 0x5858,
59 0x5858, 0x5858, 0x5858, 0x5858, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
60 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
61 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
62 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
63 };
64
65 struct cisco_eeprom eeprom_c3745_ioboard = {
66 "C3745 I/O board",
67 eeprom_c3745_ioboard_data,
68 sizeof(eeprom_c3745_ioboard_data)/2,
69 };
70
71 /* Cisco 3745 midplane EEPROM */
72 static m_uint16_t eeprom_c3745_midplane_data[] = {
73 0x04FF, 0x4003, 0x3E41, 0x0200, 0xC046, 0x0320, 0x0030, 0x0101,
74 0x4241, 0x3080, 0x0000, 0x0000, 0x0205, 0xC18B, 0x5858, 0x5858,
75 0x5858, 0x5858, 0x5858, 0x5803, 0x0081, 0x0000, 0x0000, 0x0400,
76 0xC809, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFC3, 0x0600, 0x0DED,
77 0xCD7D, 0x8043, 0x0050, 0xC28B, 0x4654, 0x5830, 0x3934, 0x3557,
78 0x304D, 0x59FF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
79 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
80 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
81 };
82
83 struct cisco_eeprom eeprom_c3745_midplane = {
84 "C3745 Midplane",
85 eeprom_c3745_midplane_data,
86 sizeof(eeprom_c3745_midplane_data)/2,
87 };
88
89
90 /* ======================================================================== */
91 /* Network Module Drivers */
92 /* ======================================================================== */
93 static struct cisco_card_driver *nm_drivers[] = {
94 &dev_c3745_nm_1fe_tx_driver,
95 &dev_c3745_nm_16esw_driver,
96 &dev_c3745_gt96100_fe_driver,
97 &dev_c3745_nm_4t_driver,
98 &dev_c3745_nm_nam_driver,
99 &dev_c3745_nm_cids_driver,
100 NULL,
101 };
102
103 /* ======================================================================== */
104 /* Cisco 3745 router instances */
105 /* ======================================================================== */
106
107 /* Initialize default parameters for a C3745 */
108 static void c3745_init_defaults(c3745_t *router);
109
110 /* Directly extract the configuration from the NVRAM device */
111 static int c3745_nvram_extract_config(vm_instance_t *vm,u_char **startup_config,size_t *startup_len,u_char **private_config,size_t *private_len)
112 {
113 int ret;
114
115 ret = generic_nvram_extract_config(vm, "rom", C3745_NVRAM_OFFSET, C3745_NVRAM_SIZE, 0, FS_NVRAM_FORMAT_WITH_BACKUP, startup_config, startup_len, private_config, private_len);
116
117 return(ret);
118 }
119
120 /* Directly push the IOS configuration to the NVRAM device */
121 static int c3745_nvram_push_config(vm_instance_t *vm,u_char *startup_config,size_t startup_len,u_char *private_config,size_t private_len)
122 {
123 int ret;
124
125 ret = generic_nvram_push_config(vm, "rom", vm->rom_size*1048576, C3745_NVRAM_OFFSET, C3745_NVRAM_SIZE, 0, FS_NVRAM_FORMAT_WITH_BACKUP, startup_config, startup_len, private_config, private_len);
126
127 return(ret);
128 }
129
130 /* Check for empty config */
131 static int c3745_nvram_check_empty_config(vm_instance_t *vm)
132 {
133 struct vdevice *rom_dev;
134 m_uint64_t addr;
135 size_t len;
136
137 if (!(rom_dev = dev_get_by_name(vm,"rom")))
138 return(-1);
139
140 addr = rom_dev->phys_addr + C3745_NVRAM_OFFSET;
141 len = C3745_NVRAM_SIZE;
142
143 while(len > 0) {
144 if (physmem_copy_u32_from_vm(vm,addr) != 0)
145 return(0);
146
147 addr += sizeof(m_uint32_t);
148 len -= sizeof(m_uint32_t);
149 }
150
151 /* Empty NVRAM */
152 vm->conf_reg |= 0x0040;
153 printf("NVRAM is empty, setting config register to 0x%x\n",vm->conf_reg);
154 return(0);
155 }
156
157 /* Create a new router instance */
158 static int c3745_create_instance(vm_instance_t *vm)
159 {
160 c3745_t *router;
161
162 if (!(router = malloc(sizeof(*router)))) {
163 fprintf(stderr,"C3745 '%s': Unable to create new instance!\n",vm->name);
164 return(-1);
165 }
166
167 memset(router,0,sizeof(*router));
168 router->vm = vm;
169 vm->hw_data = router;
170
171 c3745_init_defaults(router);
172 return(0);
173 }
174
175 /* Free resources used by a router instance */
176 static int c3745_delete_instance(vm_instance_t *vm)
177 {
178 c3745_t *router = VM_C3745(vm);
179 int i;
180
181 /* Stop all CPUs */
182 if (vm->cpu_group != NULL) {
183 vm_stop(vm);
184
185 if (cpu_group_sync_state(vm->cpu_group) == -1) {
186 vm_error(vm,"unable to sync with system CPUs.\n");
187 return(FALSE);
188 }
189 }
190
191 /* Remove NIO bindings */
192 for(i=0;i<vm->nr_slots;i++)
193 vm_slot_remove_all_nio_bindings(vm,i);
194
195 /* Shutdown all Network Modules */
196 vm_slot_shutdown_all(vm);
197
198 /* Free mainboard EEPROM */
199 for(i=0;i<3;i++)
200 cisco_eeprom_free(&router->sys_eeprom[i]);
201
202 /* Free all resources used by VM */
203 vm_free(vm);
204
205 /* Free the router structure */
206 free(router);
207 return(TRUE);
208 }
209
210 /* Get WIC device address for the specified onboard port */
211 int c3745_get_onboard_wic_addr(u_int slot,m_uint64_t *phys_addr)
212 {
213 if (slot >= C3745_MAX_WIC_BAYS)
214 return(-1);
215
216 *phys_addr = C3745_WIC_ADDR + (slot * C3745_WIC_SIZE);
217 return(0);
218 }
219
220 /* Set EEPROM for the specified slot */
221 int c3745_set_slot_eeprom(c3745_t *router,u_int slot,
222 struct cisco_eeprom *eeprom)
223 {
224 if ((slot < 1) || (slot >= C3745_MAX_NM_BAYS))
225 return(-1);
226
227 router->nm_eeprom_group[slot-1].eeprom[0] = eeprom;
228 return(0);
229 }
230
231 /* Get slot/port corresponding to specified network IRQ */
232 static inline void
233 c3745_net_irq_get_slot_port(u_int irq,u_int *slot,u_int *port)
234 {
235 irq -= C3745_NETIO_IRQ_BASE;
236 *port = irq & C3745_NETIO_IRQ_PORT_MASK;
237 *slot = irq >> C3745_NETIO_IRQ_PORT_BITS;
238 }
239
240 /* Get network IRQ for specified slot/port */
241 u_int c3745_net_irq_for_slot_port(u_int slot,u_int port)
242 {
243 u_int irq;
244
245 irq = (slot << C3745_NETIO_IRQ_PORT_BITS) + port;
246 irq += C3745_NETIO_IRQ_BASE;
247
248 return(irq);
249 }
250
251 /* Set the system id or processor board id in the eeprom */
252 int c3745_set_system_id(c3745_t *router,char *id)
253 {
254 /* 11 characters is enough. Array is 20 long */
255 strncpy(router->board_id,id,13);
256 /* Make sure it is null terminated */
257 router->board_id[13] = 0x00;
258 c3745_refresh_systemid(router);
259 return 0;
260 }
261 int c3745_refresh_systemid(c3745_t *router)
262 {
263 if (router->board_id[0] == 0x00) return(0);
264 m_uint8_t buf[11];
265 parse_board_id(buf,router->board_id,11);
266 cisco_eeprom_set_region(&router->sys_eeprom[2] ,72,buf,11);
267 return (0);
268 }
269
270 /* Set the base MAC address of the chassis */
271 static int c3745_burn_mac_addr(c3745_t *router,n_eth_addr_t *addr)
272 {
273 m_uint8_t eeprom_ver;
274 size_t offset;
275
276 /* Read EEPROM format version */
277 cisco_eeprom_get_byte(&router->sys_eeprom[2],0,&eeprom_ver);
278
279 switch(eeprom_ver) {
280 case 0:
281 cisco_eeprom_set_region(&router->sys_eeprom[2],2,
282 addr->eth_addr_byte,6);
283 break;
284
285 case 4:
286 if (!cisco_eeprom_v4_find_field(&router->sys_eeprom[2],
287 0xC3,&offset)) {
288 cisco_eeprom_set_region(&router->sys_eeprom[2],offset,
289 addr->eth_addr_byte,6);
290 }
291 break;
292
293 default:
294 vm_error(router->vm,"c3745_burn_mac_addr: unable to handle "
295 "EEPROM version %u\n",eeprom_ver);
296 return(-1);
297 }
298
299 return(0);
300 }
301
302 /* Set chassis MAC address */
303 int c3745_chassis_set_mac_addr(c3745_t *router,char *mac_addr)
304 {
305 if (parse_mac_addr(&router->mac_addr,mac_addr) == -1) {
306 vm_error(router->vm,"unable to parse MAC address '%s'.\n",mac_addr);
307 return(-1);
308 }
309
310 /* Set the chassis base MAC address */
311 c3745_burn_mac_addr(router,&router->mac_addr);
312 return(0);
313 }
314
315 /* Create the two main PCI busses for a GT64120 based system */
316 static int c3745_init_gt96100(c3745_t *router)
317 {
318 vm_instance_t *vm = router->vm;
319 vm_obj_t *obj;
320
321 vm->pci_bus[0] = pci_bus_create("PCI bus #0",0);
322 vm->pci_bus[1] = pci_bus_create("PCI bus #1",0);
323
324 if (!vm->pci_bus[0] || !vm->pci_bus[1]) {
325 vm_error(router->vm,"unable to create PCI data.\n");
326 return(-1);
327 }
328
329 if (dev_gt96100_init(vm,"gt96100",C3745_GT96K_ADDR,0x200000,
330 C3745_GT96K_IRQ,
331 C3745_EXT_IRQ,
332 c3745_net_irq_for_slot_port(0,0),
333 255) == -1)
334 return(-1);
335
336 if (!(obj = vm_object_find(router->vm,"gt96100")))
337 return(-1);
338
339 router->gt_data = obj->data;
340 return(0);
341 }
342
343 /* Initialize a Cisco 3745 */
344 static int c3745_init(c3745_t *router)
345 {
346 vm_instance_t *vm = router->vm;
347 char bus_name[128];
348 int i;
349
350 /* Set the processor type: R7000 */
351 mips64_set_prid(CPU_MIPS64(vm->boot_cpu),MIPS_PRID_R7000);
352
353 /* Initialize the Galileo GT-96100 PCI controller */
354 if (c3745_init_gt96100(router) == -1)
355 return(-1);
356
357 /* Create the NM PCI busses for slots 1-4 */
358 for(i=1;i<=4;i++) {
359 snprintf(bus_name,sizeof(bus_name),"NM Slot %d",i);
360 vm->pci_bus_pool[i] = pci_bus_create(bus_name,-1);
361
362 /* Map the NM PCI bus */
363 vm->slots_pci_bus[i] = vm->pci_bus_pool[i];
364
365 /* Create the PCI bridge */
366 dev_ti2050b_init(vm->pci_bus[1],i,vm->slots_pci_bus[i]);
367 }
368
369 vm->elf_machine_id = C3745_ELF_MACHINE_ID;
370 return(0);
371 }
372
373 /* Show C3745 hardware info */
374 void c3745_show_hardware(c3745_t *router)
375 {
376 vm_instance_t *vm = router->vm;
377
378 printf("C3745 instance '%s' (id %d):\n",vm->name,vm->instance_id);
379
380 printf(" VM Status : %d\n",vm->status);
381 printf(" RAM size : %u Mb\n",vm->ram_size);
382 printf(" NVRAM size : %u Kb\n",vm->nvram_size);
383 printf(" IOS image : %s\n\n",vm->ios_image);
384
385 if (vm->debug_level > 0) {
386 dev_show_list(vm);
387 pci_dev_show_list(vm->pci_bus[0]);
388 pci_dev_show_list(vm->pci_bus[1]);
389 printf("\n");
390 }
391 }
392
393 /* Initialize default parameters for a C3745 */
394 static void c3745_init_defaults(c3745_t *router)
395 {
396 vm_instance_t *vm = router->vm;
397 n_eth_addr_t *m;
398 m_uint16_t pid;
399
400 /* Set platform slots characteristics */
401 vm->nr_slots = C3745_MAX_NM_BAYS;
402 vm->slots_type = CISCO_CARD_TYPE_NM;
403 vm->slots_drivers = nm_drivers;
404
405 pid = (m_uint16_t)getpid();
406
407 /* Generate a chassis MAC address based on the instance ID */
408 m = &router->mac_addr;
409 m->eth_addr_byte[0] = vm_get_mac_addr_msb(vm);
410 m->eth_addr_byte[1] = vm->instance_id & 0xFF;
411 m->eth_addr_byte[2] = pid >> 8;
412 m->eth_addr_byte[3] = pid & 0xFF;
413 m->eth_addr_byte[4] = 0x00;
414 m->eth_addr_byte[5] = 0x00;
415 router->board_id[0] = 0x00;
416
417 c3745_init_eeprom_groups(router);
418 cisco_eeprom_copy(&router->sys_eeprom[0],&eeprom_c3745_motherboard);
419 cisco_eeprom_copy(&router->sys_eeprom[1],&eeprom_c3745_ioboard);
420 cisco_eeprom_copy(&router->sys_eeprom[2],&eeprom_c3745_midplane);
421 c3745_burn_mac_addr(router,&router->mac_addr);
422
423 /* The GT96100 system controller has 2 integrated FastEthernet ports */
424 vm_slot_add_binding(vm,"GT96100-FE",0,0);
425
426 vm->ram_mmap = C3745_DEFAULT_RAM_MMAP;
427 vm->ram_size = C3745_DEFAULT_RAM_SIZE;
428 vm->rom_size = C3745_DEFAULT_ROM_SIZE;
429 vm->nvram_size = C3745_DEFAULT_NVRAM_SIZE;
430 vm->conf_reg_setup = C3745_DEFAULT_CONF_REG;
431 vm->clock_divisor = C3745_DEFAULT_CLOCK_DIV;
432 vm->nvram_rom_space = C3745_NVRAM_ROM_RES_SIZE;
433 vm->nm_iomem_size = C3745_DEFAULT_IOMEM_SIZE;
434
435 vm->pcmcia_disk_size[0] = C3745_DEFAULT_DISK0_SIZE;
436 vm->pcmcia_disk_size[1] = C3745_DEFAULT_DISK1_SIZE;
437 }
438
439 /* Initialize the C3745 Platform */
440 static int c3745_init_platform(c3745_t *router)
441 {
442 vm_instance_t *vm = router->vm;
443 cpu_mips_t *cpu;
444 cpu_gen_t *gen;
445 vm_obj_t *obj;
446
447 /* Copy config register setup into "active" config register */
448 vm->conf_reg = vm->conf_reg_setup;
449
450 /* Create Console and AUX ports */
451 vm_init_vtty(vm);
452
453 /* Create a CPU group */
454 vm->cpu_group = cpu_group_create("System CPU");
455
456 /* Initialize the virtual MIPS processor */
457 if (!(gen = cpu_create(vm,CPU_TYPE_MIPS64,0))) {
458 vm_error(vm,"unable to create CPU!\n");
459 return(-1);
460 }
461
462 cpu = CPU_MIPS64(gen);
463
464 /* Add this CPU to the system CPU group */
465 cpu_group_add(vm->cpu_group,gen);
466 vm->boot_cpu = gen;
467
468 /* Initialize the IRQ routing vectors */
469 vm->set_irq = mips64_vm_set_irq;
470 vm->clear_irq = mips64_vm_clear_irq;
471
472 /* Mark the Network IO interrupt as high priority */
473 cpu->irq_idle_preempt[C3745_NETIO_IRQ] = TRUE;
474 cpu->irq_idle_preempt[C3745_GT96K_IRQ] = TRUE;
475 cpu->irq_idle_preempt[C3745_DUART_IRQ] = TRUE;
476
477 /* Copy some parameters from VM to CPU (idle PC, ...) */
478 cpu->idle_pc = vm->idle_pc;
479
480 if (vm->timer_irq_check_itv)
481 cpu->timer_irq_check_itv = vm->timer_irq_check_itv;
482
483 /* Remote emulator control */
484 dev_remote_control_init(vm,0x16000000,0x1000);
485
486 /* Specific Storage Area (SSA) */
487 dev_ram_init(vm,"ssa",TRUE,FALSE,NULL,FALSE,0x16001000ULL,0x7000);
488
489 /* IO FPGA */
490 if (dev_c3745_iofpga_init(router,C3745_IOFPGA_ADDR,0x200000) == -1)
491 return(-1);
492
493 if (!(obj = vm_object_find(router->vm,"io_fpga")))
494 return(-1);
495
496 router->iofpga_data = obj->data;
497
498 #if 0
499 /* PCI IO space */
500 if (!(vm->pci_io_space = pci_io_data_init(vm,C3745_PCI_IO_ADDR)))
501 return(-1);
502 #endif
503
504 /* Initialize the chassis */
505 if (c3745_init(router) == -1)
506 return(-1);
507
508 /* Initialize RAM */
509 vm_ram_init(vm,0x00000000ULL);
510
511 /* Initialize ROM (as a Flash) */
512 if (!(obj = dev_flash_init(vm,"rom",C3745_ROM_ADDR,vm->rom_size*1048576)))
513 return(-1);
514
515 dev_flash_copy_data(obj,0,mips64_microcode,mips64_microcode_len);
516 c3745_nvram_check_empty_config(vm);
517
518 /* Byte swapping */
519 dev_bswap_init(vm,"mem_bswap",C3745_BSWAP_ADDR,1024*1048576,0x00000000ULL);
520
521 /* Initialize the NS16552 DUART */
522 dev_ns16552_init(vm,C3745_DUART_ADDR,0x1000,3,C3745_DUART_IRQ,
523 vm->vtty_con,vm->vtty_aux);
524
525 /* PCMCIA Slot 0 */
526 dev_pcmcia_disk_init(vm,"slot0",C3745_SLOT0_ADDR,0x200000,
527 vm->pcmcia_disk_size[0],1);
528
529 /* PCMCIA Slot 1 */
530 dev_pcmcia_disk_init(vm,"slot1",C3745_SLOT1_ADDR,0x200000,
531 vm->pcmcia_disk_size[1],1);
532
533 /* Initialize Network Modules */
534 if (vm_slot_init_all(vm) == -1)
535 return(-1);
536
537 /* Show device list */
538 c3745_show_hardware(router);
539 return(0);
540 }
541
542 /* Boot the IOS image */
543 static int c3745_boot_ios(c3745_t *router)
544 {
545 vm_instance_t *vm = router->vm;
546 cpu_mips_t *cpu;
547
548 if (!vm->boot_cpu)
549 return(-1);
550
551 /* Suspend CPU activity since we will restart directly from ROM */
552 vm_suspend(vm);
553
554 /* Check that CPU activity is really suspended */
555 if (cpu_group_sync_state(vm->cpu_group) == -1) {
556 vm_error(vm,"unable to sync with system CPUs.\n");
557 return(-1);
558 }
559
560 /* Reset the boot CPU */
561 cpu = CPU_MIPS64(vm->boot_cpu);
562 mips64_reset(cpu);
563
564 /* Load IOS image */
565 if (mips64_load_elf_image(cpu,vm->ios_image,
566 (vm->ghost_status == VM_GHOST_RAM_USE),
567 &vm->ios_entry_point) < 0)
568 {
569 vm_error(vm,"failed to load Cisco IOS image '%s'.\n",vm->ios_image);
570 return(-1);
571 }
572
573 /* Launch the simulation */
574 printf("\nC3745 '%s': starting simulation (CPU0 PC=0x%llx), "
575 "JIT %sabled.\n",
576 vm->name,cpu->pc,vm->jit_use ? "en":"dis");
577
578 vm_log(vm,"C3745_BOOT",
579 "starting instance (CPU0 PC=0x%llx,idle_pc=0x%llx,JIT %s)\n",
580 cpu->pc,cpu->idle_pc,vm->jit_use ? "on":"off");
581
582 /* Start main CPU */
583 if (vm->ghost_status != VM_GHOST_RAM_GENERATE) {
584 vm->status = VM_STATUS_RUNNING;
585 cpu_start(vm->boot_cpu);
586 } else {
587 vm->status = VM_STATUS_SHUTDOWN;
588 }
589 return(0);
590 }
591
592 /* Set an IRQ */
593 static void c3745_set_irq(vm_instance_t *vm,u_int irq)
594 {
595 c3745_t *router = VM_C3745(vm);
596 cpu_mips_t *cpu0 = CPU_MIPS64(vm->boot_cpu);
597 u_int slot,port;
598
599 switch(irq) {
600 case 0 ... 7:
601 mips64_set_irq(cpu0,irq);
602
603 if (cpu0->irq_idle_preempt[irq])
604 cpu_idle_break_wait(cpu0->gen);
605 break;
606
607 case C3745_NETIO_IRQ_BASE ... C3745_NETIO_IRQ_END:
608 c3745_net_irq_get_slot_port(irq,&slot,&port);
609 dev_c3745_iofpga_net_set_irq(router->iofpga_data,slot,port);
610 break;
611 }
612 }
613
614 /* Clear an IRQ */
615 static void c3745_clear_irq(vm_instance_t *vm,u_int irq)
616 {
617 c3745_t *router = VM_C3745(vm);
618 cpu_mips_t *cpu0 = CPU_MIPS64(vm->boot_cpu);
619 u_int slot,port;
620
621 switch(irq) {
622 case 0 ... 7:
623 mips64_clear_irq(cpu0,irq);
624 break;
625
626 case C3745_NETIO_IRQ_BASE ... C3745_NETIO_IRQ_END:
627 c3745_net_irq_get_slot_port(irq,&slot,&port);
628 dev_c3745_iofpga_net_clear_irq(router->iofpga_data,slot,port);
629 break;
630 }
631 }
632
633 /* Initialize a Cisco 3745 instance */
634 static int c3745_init_instance(vm_instance_t *vm)
635 {
636 c3745_t *router = VM_C3745(vm);
637 m_uint32_t rom_entry_point;
638 cpu_mips_t *cpu0;
639
640 if (!vm->ios_image) {
641 vm_error(vm,"no Cisco IOS image defined.");
642 return(-1);
643 }
644
645 /* Initialize the C3745 platform */
646 if (c3745_init_platform(router) == -1) {
647 vm_error(vm,"unable to initialize the platform hardware.\n");
648 return(-1);
649 }
650
651 /* IRQ routing */
652 vm->set_irq = c3745_set_irq;
653 vm->clear_irq = c3745_clear_irq;
654
655 /* Load IOS configuration files */
656 if (vm->ios_startup_config != NULL || vm->ios_private_config != NULL) {
657 vm_nvram_push_config(vm,vm->ios_startup_config,vm->ios_private_config);
658 vm->conf_reg &= ~0x40;
659 }
660
661 /* Load ROM (ELF image or embedded) */
662 cpu0 = CPU_MIPS64(vm->boot_cpu);
663 rom_entry_point = (m_uint32_t)MIPS_ROM_PC;
664
665 if ((vm->rom_filename != NULL) &&
666 (mips64_load_elf_image(cpu0,vm->rom_filename,0,&rom_entry_point) < 0))
667 {
668 vm_error(vm,"unable to load alternate ROM '%s', "
669 "fallback to embedded ROM.\n\n",vm->rom_filename);
670 vm->rom_filename = NULL;
671 }
672
673 /* Load symbol file */
674 if (vm->sym_filename) {
675 mips64_sym_load_file(cpu0,vm->sym_filename);
676 cpu0->sym_trace = 1;
677 }
678
679 return(c3745_boot_ios(router));
680 }
681
682 /* Stop a Cisco 3745 instance */
683 static int c3745_stop_instance(vm_instance_t *vm)
684 {
685 printf("\nC3745 '%s': stopping simulation.\n",vm->name);
686 vm_log(vm,"C3745_STOP","stopping simulation.\n");
687
688 /* Stop all CPUs */
689 if (vm->cpu_group != NULL) {
690 vm_stop(vm);
691
692 if (cpu_group_sync_state(vm->cpu_group) == -1) {
693 vm_error(vm,"unable to sync with system CPUs.\n");
694 return(-1);
695 }
696 }
697
698 /* Free resources that were used during execution to emulate hardware */
699 vm_slot_shutdown_all(vm);
700 vm_hardware_shutdown(vm);
701
702 /* Cleanup */
703 VM_C3745(vm)->iofpga_data = NULL;
704 VM_C3745(vm)->gt_data = NULL;
705 return(0);
706 }
707
708 /* Trigger an OIR event */
709 static int c3745_trigger_oir_event(c3745_t *router,u_int slot_mask)
710 {
711 router->oir_status = slot_mask;
712 vm_set_irq(router->vm,C3745_EXT_IRQ);
713 return(0);
714 }
715
716 /* Initialize a new NM while the virtual router is online (OIR) */
717 static int c3745_nm_init_online(vm_instance_t *vm,u_int slot,u_int subslot)
718 {
719 if (!slot) {
720 vm_error(vm,"OIR not supported on slot 0.\n");
721 return(-1);
722 }
723
724 /*
725 * Suspend CPU activity while adding new hardware (since we change the
726 * memory maps).
727 */
728 vm_suspend(vm);
729
730 /* Check that CPU activity is really suspended */
731 if (cpu_group_sync_state(vm->cpu_group) == -1) {
732 vm_error(vm,"unable to sync with system CPUs.\n");
733 return(-1);
734 }
735
736 /* Add the new hardware elements */
737 if (vm_slot_init(vm,slot) == -1)
738 return(-1);
739
740 /* Resume normal operations */
741 vm_resume(vm);
742
743 /* Now, we can safely trigger the OIR event */
744 c3745_trigger_oir_event(VM_C3745(vm),1 << (slot - 1));
745 return(0);
746 }
747
748 /* Stop a NM while the virtual router is online (OIR) */
749 static int c3745_nm_stop_online(vm_instance_t *vm,u_int slot,u_int subslot)
750 {
751 if (!slot) {
752 vm_error(vm,"OIR not supported on slot 0.\n");
753 return(-1);
754 }
755
756 /* The NM driver must be initialized */
757 if (!vm_slot_get_card_ptr(vm,slot)) {
758 vm_error(vm,"trying to shut down empty slot %u.\n",slot);
759 return(-1);
760 }
761
762 /* Disable all NIOs to stop traffic forwarding */
763 vm_slot_disable_all_nio(vm,slot);
764
765 /* We can safely trigger the OIR event */
766 c3745_trigger_oir_event(VM_C3745(vm),1 << (slot - 1));
767
768 /*
769 * Suspend CPU activity while removing the hardware (since we change the
770 * memory maps).
771 */
772 vm_suspend(vm);
773
774 /* Device removal */
775 if (vm_slot_shutdown(vm,slot) != 0)
776 vm_error(vm,"unable to shutdown slot %u.\n",slot);
777
778 /* Resume normal operations */
779 vm_resume(vm);
780 return(0);
781 }
782
783 /* Get MAC address MSB */
784 static u_int c3745_get_mac_addr_msb(void)
785 {
786 return(0xC4);
787 }
788
789 /* Parse specific options for the Cisco 1700 platform */
790 static int c3745_cli_parse_options(vm_instance_t *vm,int option)
791 {
792 c3745_t *router = VM_C3745(vm);
793
794 switch(option) {
795 /* IO memory reserved for NMs (in percents!) */
796 case OPT_IOMEM_SIZE:
797 vm->nm_iomem_size = 0x8000 | atoi(optarg);
798 break;
799
800 /* Set the base MAC address */
801 case 'm':
802 if (!c3745_chassis_set_mac_addr(router,optarg))
803 printf("MAC address set to '%s'.\n",optarg);
804 break;
805
806 /* Set the System ID */
807 case 'I':
808 if (!c3745_set_system_id(router,optarg))
809 printf("System ID set to '%s'.\n",optarg);
810 break;
811
812 /* Unknown option */
813 default:
814 return(-1);
815 }
816
817 return(0);
818 }
819
820 /* Show specific CLI options */
821 static void c3745_cli_show_options(vm_instance_t *vm)
822 {
823 printf(" --iomem-size <val> : IO memory (in percents, default: %u)\n"
824 " -p <nm_desc> : Define a Network Module\n"
825 " -I <serialno> : Set Processor Board Serial Number\n"
826 " -s <nm_nio> : Bind a Network IO interface to a "
827 "Network Module\n",
828 vm->nm_iomem_size);
829 }
830
831 /* Platform definition */
832 static vm_platform_t c3745_platform = {
833 "c3745", "C3745", "3745",
834 c3745_create_instance,
835 c3745_delete_instance,
836 c3745_init_instance,
837 c3745_stop_instance,
838 c3745_nm_init_online,
839 c3745_nm_stop_online,
840 c3745_nvram_extract_config,
841 c3745_nvram_push_config,
842 c3745_get_mac_addr_msb,
843 NULL,
844 c3745_cli_parse_options,
845 c3745_cli_show_options,
846 NULL,
847 };
848
849 /* Register the c3745 platform */
850 int c3745_platform_register(void)
851 {
852 if (vm_platform_register(&c3745_platform) == -1)
853 return(-1);
854
855 return(hypervisor_c3745_init(&c3745_platform));
856 }