/[dynamips]/upstream/dynamips-0.2.6-RC3/dev_c2691.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.6-RC3/dev_c2691.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations)
Sat Oct 6 16:06:49 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 35058 byte(s)
dynamips-0.2.6-RC3

1 /*
2 * Cisco 2691 simulation platform.
3 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
4 *
5 * Generic Cisco 2691 routines and definitions (EEPROM,...).
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <assert.h>
14
15 #include "mips64.h"
16 #include "dynamips.h"
17 #include "memory.h"
18 #include "device.h"
19 #include "pci_io.h"
20 #include "dev_gt.h"
21 #include "cisco_eeprom.h"
22 #include "dev_c2691.h"
23 #include "dev_vtty.h"
24 #include "registry.h"
25
26 /* ======================================================================== */
27 /* EEPROM definitions */
28 /* ======================================================================== */
29
30 /* Cisco 2691 mainboard EEPROM */
31 static m_uint16_t eeprom_c2691_mainboard_data[] = {
32 0x04FF, 0xC18B, 0x5858, 0x5858, 0x5858, 0x5858, 0x5858, 0x5809,
33 0x6640, 0x0258, 0xC046, 0x0320, 0x0025, 0x9002, 0x4246, 0x3085,
34 0x1C10, 0x8206, 0x80FF, 0xFFFF, 0xFFC4, 0x08FF, 0xFFFF, 0xFFFF,
35 0xFFFF, 0xFF81, 0xFFFF, 0xFFFF, 0x03FF, 0x04FF, 0xC28B, 0x5858,
36 0x5858, 0x5858, 0x5858, 0x5858, 0x58C3, 0x0600, 0x1280, 0xD387,
37 0xC043, 0x0020, 0xC508, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x4100,
38 0x0101, 0x01FF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
39 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
40 };
41
42 struct cisco_eeprom eeprom_c2691_mainboard = {
43 "C2691 Backplane",
44 eeprom_c2691_mainboard_data,
45 sizeof(eeprom_c2691_mainboard_data)/2,
46 };
47
48 /* ======================================================================== */
49 /* Network Module Drivers */
50 /* ======================================================================== */
51 static struct c2691_nm_driver *nm_drivers[] = {
52 &dev_c2691_nm_1fe_tx_driver,
53 &dev_c2691_nm_16esw_driver,
54 &dev_c2691_gt96100_fe_driver,
55 &dev_c2691_nm_4t_driver,
56 NULL,
57 };
58
59 /* ======================================================================== */
60 /* Cisco 2691 router instances */
61 /* ======================================================================== */
62
63 /* Directly extract the configuration from the NVRAM device */
64 ssize_t c2691_nvram_extract_config(vm_instance_t *vm,char **buffer)
65 {
66 u_char *base_ptr,*ios_ptr,*cfg_ptr,*end_ptr;
67 m_uint32_t start,nvlen;
68 m_uint16_t magic1,magic2;
69 struct vdevice *nvram_dev;
70 off_t nvram_size;
71 int fd;
72
73 if ((nvram_dev = dev_get_by_name(vm,"rom")))
74 dev_sync(nvram_dev);
75
76 fd = vm_mmap_open_file(vm,"rom",&base_ptr,&nvram_size);
77
78 if (fd == -1)
79 return(-1);
80
81 ios_ptr = base_ptr + C2691_NVRAM_OFFSET;
82 end_ptr = base_ptr + nvram_size;
83
84 if ((ios_ptr + 0x30) >= end_ptr) {
85 vm_error(vm,"NVRAM file too small\n");
86 return(-1);
87 }
88
89 magic1 = ntohs(*PTR_ADJUST(m_uint16_t *,ios_ptr,0x06));
90 magic2 = ntohs(*PTR_ADJUST(m_uint16_t *,ios_ptr,0x08));
91
92 if ((magic1 != 0xF0A5) || (magic2 != 0xABCD)) {
93 vm_error(vm,"unable to find IOS magic numbers (0x%x,0x%x)!\n",
94 magic1,magic2);
95 return(-1);
96 }
97
98 start = ntohl(*PTR_ADJUST(m_uint32_t *,ios_ptr,0x10)) + 1;
99 nvlen = ntohl(*PTR_ADJUST(m_uint32_t *,ios_ptr,0x18));
100
101 if (!(*buffer = malloc(nvlen+1))) {
102 vm_error(vm,"unable to allocate config buffer (%u bytes)\n",nvlen);
103 return(-1);
104 }
105
106 cfg_ptr = ios_ptr + start + 0x08;
107
108 if ((cfg_ptr + nvlen) > end_ptr) {
109 vm_error(vm,"NVRAM file too small\n");
110 return(-1);
111 }
112
113 memcpy(*buffer,cfg_ptr,nvlen-1);
114 (*buffer)[nvlen-1] = 0;
115 return(nvlen-1);
116 }
117
118 static int c2691_nvram_push_config_part(vm_instance_t *vm,
119 char *buffer,size_t len,
120 u_char *ios_ptr)
121 {
122 m_uint32_t cfg_offset,cklen,tmp;
123 m_uint16_t cksum;
124 u_char *cfg_ptr;
125
126 cfg_offset = 0x2c;
127 cfg_ptr = ios_ptr + cfg_offset;
128
129 /* Write IOS tag, uncompressed config... */
130 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x06) = htons(0xF0A5);
131 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x08) = htons(0xABCD);
132 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0a) = htons(0x0001);
133 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0c) = htons(0x0000);
134 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0e) = htons(0x0c04);
135
136 /* Store file contents to NVRAM */
137 memcpy(cfg_ptr,buffer,len);
138
139 /* Write config addresses + size */
140 tmp = cfg_offset - 0x08;
141
142 *PTR_ADJUST(m_uint32_t *,ios_ptr,0x10) = htonl(tmp);
143 *PTR_ADJUST(m_uint32_t *,ios_ptr,0x14) = htonl(tmp + len);
144 *PTR_ADJUST(m_uint32_t *,ios_ptr,0x18) = htonl(len);
145
146 /* Compute the checksum */
147 cklen = C2691_NVRAM_SIZE - 0x08;
148 cksum = nvram_cksum((m_uint16_t *)(ios_ptr+0x08),cklen);
149 *PTR_ADJUST(m_uint16_t *,ios_ptr,0x0c) = htons(cksum);
150 return(0);
151 }
152
153 /* Directly push the IOS configuration to the NVRAM device */
154 int c2691_nvram_push_config(vm_instance_t *vm,char *buffer,size_t len)
155 {
156 u_char *base_ptr,*ios_ptr;
157 int fd;
158
159 fd = vm_mmap_create_file(vm,"rom",vm->rom_size*1048576,&base_ptr);
160
161 if (fd == -1)
162 return(-1);
163
164 ios_ptr = base_ptr + C2691_NVRAM_OFFSET;
165
166 /* Normal config */
167 c2691_nvram_push_config_part(vm,buffer,len,ios_ptr);
168
169 /* Backup config */
170 c2691_nvram_push_config_part(vm,buffer,len,ios_ptr + C2691_NVRAM_SIZE);
171
172 vm_mmap_close_file(fd,base_ptr,vm->rom_size*1048576);
173 return(0);
174 }
175
176 /* Check for empty config */
177 int c2691_nvram_check_empty_config(vm_instance_t *vm)
178 {
179 struct vdevice *rom_dev;
180 m_uint64_t addr;
181 size_t len;
182
183 if (!(rom_dev = dev_get_by_name(vm,"rom")))
184 return(-1);
185
186 addr = rom_dev->phys_addr + C2691_NVRAM_OFFSET;
187 len = C2691_NVRAM_SIZE;
188
189 while(len > 0) {
190 if (physmem_copy_u32_from_vm(vm,addr) != 0)
191 return(0);
192
193 addr += sizeof(m_uint32_t);
194 len -= sizeof(m_uint32_t);
195 }
196
197 /* Empty NVRAM */
198 vm->conf_reg |= 0x0040;
199 printf("NVRAM is empty, setting config register to 0x%x\n",vm->conf_reg);
200 return(0);
201 }
202
203 /* Create a new router instance */
204 c2691_t *c2691_create_instance(char *name,int instance_id)
205 {
206 c2691_t *router;
207
208 if (!(router = malloc(sizeof(*router)))) {
209 fprintf(stderr,"C2691 '%s': Unable to create new instance!\n",name);
210 return NULL;
211 }
212
213 memset(router,0,sizeof(*router));
214
215 if (!(router->vm = vm_create(name,instance_id,VM_TYPE_C2691))) {
216 fprintf(stderr,"C2691 '%s': unable to create VM instance!\n",name);
217 goto err_vm;
218 }
219
220 c2691_init_defaults(router);
221 router->vm->hw_data = router;
222 return router;
223
224 err_vm:
225 free(router);
226 return NULL;
227 }
228
229 /* Free resources used by a router instance */
230 static int c2691_free_instance(void *data,void *arg)
231 {
232 vm_instance_t *vm = data;
233 c2691_t *router;
234 int i;
235
236 if (vm->type == VM_TYPE_C2691) {
237 router = VM_C2691(vm);
238
239 /* Stop all CPUs */
240 if (vm->cpu_group != NULL) {
241 vm_stop(vm);
242
243 if (cpu_group_sync_state(vm->cpu_group) == -1) {
244 vm_error(vm,"unable to sync with system CPUs.\n");
245 return(FALSE);
246 }
247 }
248
249 /* Remove NIO bindings */
250 for(i=0;i<C2691_MAX_NM_BAYS;i++)
251 c2691_nm_remove_all_nio_bindings(router,i);
252
253 /* Shutdown all Network Modules */
254 c2691_nm_shutdown_all(router);
255
256 /* Free mainboard EEPROM */
257 cisco_eeprom_free(&router->mb_eeprom);
258
259 /* Free all resources used by VM */
260 vm_free(vm);
261
262 /* Free the router structure */
263 free(router);
264 return(TRUE);
265 }
266
267 return(FALSE);
268 }
269
270 /* Delete a router instance */
271 int c2691_delete_instance(char *name)
272 {
273 return(registry_delete_if_unused(name,OBJ_TYPE_VM,
274 c2691_free_instance,NULL));
275 }
276
277 /* Delete all router instances */
278 int c2691_delete_all_instances(void)
279 {
280 return(registry_delete_type(OBJ_TYPE_VM,c2691_free_instance,NULL));
281 }
282
283 /* Save configuration of a C2691 instance */
284 void c2691_save_config(c2691_t *router,FILE *fd)
285 {
286 vm_instance_t *vm = router->vm;
287 struct c2691_nio_binding *nb;
288 struct c2691_nm_bay *bay;
289 int i;
290
291 /* General settings */
292 fprintf(fd,"c2691 create %s %u\n",vm->name,vm->instance_id);
293
294 /* VM configuration */
295 vm_save_config(vm,fd);
296
297 /* Network Module settings */
298 for(i=0;i<C2691_MAX_NM_BAYS;i++) {
299 if (!(bay = c2691_nm_get_info(router,i)))
300 continue;
301
302 if (bay->dev_type) {
303 fprintf(fd,"c2691 add_nm_binding %s %u %s\n",
304 vm->name,i,bay->dev_type);
305 }
306
307 for(nb=bay->nio_list;nb;nb=nb->next) {
308 fprintf(fd,"c2691 add_nio_binding %s %u %u %s\n",
309 vm->name,i,nb->port_id,nb->nio->name);
310 }
311 }
312
313 fprintf(fd,"\n");
314 }
315
316 /* Save configurations of all C2691 instances */
317 static void c2691_reg_save_config(registry_entry_t *entry,void *opt,int *err)
318 {
319 vm_instance_t *vm = entry->data;
320 c2691_t *router = VM_C2691(vm);
321
322 if (vm->type == VM_TYPE_C2691)
323 c2691_save_config(router,(FILE *)opt);
324 }
325
326 void c2691_save_config_all(FILE *fd)
327 {
328 registry_foreach_type(OBJ_TYPE_VM,c2691_reg_save_config,fd,NULL);
329 }
330
331 /* Set NM EEPROM definition */
332 int c2691_nm_set_eeprom(c2691_t *router,u_int nm_bay,
333 const struct cisco_eeprom *eeprom)
334 {
335 if (nm_bay != 1) {
336 vm_error(router->vm,"c2691_nm_set_eeprom: invalid NM Bay %u.\n",nm_bay);
337 return(-1);
338 }
339
340 if (cisco_eeprom_copy(&router->nm_bay[nm_bay].eeprom,eeprom) == -1) {
341 vm_error(router->vm,"c2691_nm_set_eeprom: no memory.\n");
342 return(-1);
343 }
344
345 return(0);
346 }
347
348 /* Unset NM EEPROM definition (empty bay) */
349 int c2691_nm_unset_eeprom(c2691_t *router,u_int nm_bay)
350 {
351 if (nm_bay != 1) {
352 vm_error(router->vm,"c2691_nm_set_eeprom: invalid NM Bay %u.\n",nm_bay);
353 return(-1);
354 }
355
356 cisco_eeprom_free(&router->nm_bay[nm_bay].eeprom);
357 return(0);
358 }
359
360 /* Check if a bay has a port adapter */
361 int c2691_nm_check_eeprom(c2691_t *router,u_int nm_bay)
362 {
363 if (nm_bay != 1)
364 return(FALSE);
365
366 return(cisco_eeprom_valid(&router->nm_bay[nm_bay].eeprom));
367 }
368
369 /* Get bay info */
370 struct c2691_nm_bay *c2691_nm_get_info(c2691_t *router,u_int nm_bay)
371 {
372 if (nm_bay >= C2691_MAX_NM_BAYS)
373 return NULL;
374
375 return(&router->nm_bay[nm_bay]);
376 }
377
378 /* Get NM type */
379 char *c2691_nm_get_type(c2691_t *router,u_int nm_bay)
380 {
381 struct c2691_nm_bay *bay;
382
383 bay = c2691_nm_get_info(router,nm_bay);
384 return((bay != NULL) ? bay->dev_type : NULL);
385 }
386
387 /* Get driver info about the specified slot */
388 void *c2691_nm_get_drvinfo(c2691_t *router,u_int nm_bay)
389 {
390 struct c2691_nm_bay *bay;
391
392 bay = c2691_nm_get_info(router,nm_bay);
393 return((bay != NULL) ? bay->drv_info : NULL);
394 }
395
396 /* Set driver info for the specified slot */
397 int c2691_nm_set_drvinfo(c2691_t *router,u_int nm_bay,void *drv_info)
398 {
399 struct c2691_nm_bay *bay;
400
401 if (!(bay = c2691_nm_get_info(router,nm_bay)))
402 return(-1);
403
404 bay->drv_info = drv_info;
405 return(0);
406 }
407
408 /* Get a NM driver */
409 static struct c2691_nm_driver *c2691_nm_get_driver(char *dev_type)
410 {
411 int i;
412
413 for(i=0;nm_drivers[i];i++)
414 if (!strcmp(nm_drivers[i]->dev_type,dev_type))
415 return nm_drivers[i];
416
417 return NULL;
418 }
419
420 /* Add a NM binding */
421 int c2691_nm_add_binding(c2691_t *router,char *dev_type,u_int nm_bay)
422 {
423 struct c2691_nm_driver *nm_driver;
424 struct c2691_nm_bay *bay;
425
426 if (!(bay = c2691_nm_get_info(router,nm_bay)))
427 return(-1);
428
429 /* check that this bay is empty */
430 if (bay->dev_type != NULL) {
431 vm_error(router->vm,"a NM already exists in slot %u.\n",nm_bay);
432 return(-1);
433 }
434
435 /* find the NM driver */
436 if (!(nm_driver = c2691_nm_get_driver(dev_type))) {
437 vm_error(router->vm,"unknown NM type '%s'.\n",dev_type);
438 return(-1);
439 }
440
441 bay->dev_type = nm_driver->dev_type;
442 bay->nm_driver = nm_driver;
443 return(0);
444 }
445
446 /* Remove a NM binding */
447 int c2691_nm_remove_binding(c2691_t *router,u_int nm_bay)
448 {
449 struct c2691_nm_bay *bay;
450
451 if (!(bay = c2691_nm_get_info(router,nm_bay)))
452 return(-1);
453
454 /* stop if this bay is still active */
455 if (bay->drv_info != NULL) {
456 vm_error(router->vm,"slot %u still active.\n",nm_bay);
457 return(-1);
458 }
459
460 /* check that this bay is not empty */
461 if (bay->dev_type == NULL) {
462 vm_error(router->vm,"slot %u is empty.\n",nm_bay);
463 return(-1);
464 }
465
466 /* remove all NIOs bindings */
467 c2691_nm_remove_all_nio_bindings(router,nm_bay);
468
469 bay->dev_type = NULL;
470 bay->nm_driver = NULL;
471 return(0);
472 }
473
474 /* Find a NIO binding */
475 struct c2691_nio_binding *
476 c2691_nm_find_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id)
477 {
478 struct c2691_nio_binding *nb;
479 struct c2691_nm_bay *bay;
480
481 if (!(bay = c2691_nm_get_info(router,nm_bay)))
482 return NULL;
483
484 for(nb=bay->nio_list;nb;nb=nb->next)
485 if (nb->port_id == port_id)
486 return nb;
487
488 return NULL;
489 }
490
491 /* Add a network IO binding */
492 int c2691_nm_add_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id,
493 char *nio_name)
494 {
495 struct c2691_nio_binding *nb;
496 struct c2691_nm_bay *bay;
497 netio_desc_t *nio;
498
499 if (!(bay = c2691_nm_get_info(router,nm_bay)))
500 return(-1);
501
502 /* check that a NIO is not already bound to this port */
503 if (c2691_nm_find_nio_binding(router,nm_bay,port_id) != NULL) {
504 vm_error(router->vm,"a NIO already exists for interface %u/%u.\n",
505 nm_bay,port_id);
506 return(-1);
507 }
508
509 /* acquire a reference on the NIO object */
510 if (!(nio = netio_acquire(nio_name))) {
511 vm_error(router->vm,"unable to find NIO '%s'.\n",nio_name);
512 return(-1);
513 }
514
515 /* create a new binding */
516 if (!(nb = malloc(sizeof(*nb)))) {
517 vm_error(router->vm,"unable to create NIO binding "
518 "for interface %u/%u.\n",nm_bay,port_id);
519 netio_release(nio_name);
520 return(-1);
521 }
522
523 memset(nb,0,sizeof(*nb));
524 nb->nio = nio;
525 nb->port_id = port_id;
526 nb->next = bay->nio_list;
527 if (nb->next) nb->next->prev = nb;
528 bay->nio_list = nb;
529 return(0);
530 }
531
532 /* Remove a NIO binding */
533 int c2691_nm_remove_nio_binding(c2691_t *router,u_int nm_bay,u_int port_id)
534 {
535 struct c2691_nio_binding *nb;
536 struct c2691_nm_bay *bay;
537
538 if (!(bay = c2691_nm_get_info(router,nm_bay)))
539 return(-1);
540
541 if (!(nb = c2691_nm_find_nio_binding(router,nm_bay,port_id)))
542 return(-1); /* no nio binding for this slot/port */
543
544 /* tell the NM driver to stop using this NIO */
545 if (bay->nm_driver)
546 bay->nm_driver->nm_unset_nio(router,nm_bay,port_id);
547
548 /* remove this entry from the double linked list */
549 if (nb->next)
550 nb->next->prev = nb->prev;
551
552 if (nb->prev) {
553 nb->prev->next = nb->next;
554 } else {
555 bay->nio_list = nb->next;
556 }
557
558 /* unreference NIO object */
559 netio_release(nb->nio->name);
560 free(nb);
561 return(0);
562 }
563
564 /* Remove all NIO bindings for the specified NM */
565 int c2691_nm_remove_all_nio_bindings(c2691_t *router,u_int nm_bay)
566 {
567 struct c2691_nio_binding *nb,*next;
568 struct c2691_nm_bay *bay;
569
570 if (!(bay = c2691_nm_get_info(router,nm_bay)))
571 return(-1);
572
573 for(nb=bay->nio_list;nb;nb=next) {
574 next = nb->next;
575
576 /* tell the NM driver to stop using this NIO */
577 if (bay->nm_driver)
578 bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id);
579
580 /* unreference NIO object */
581 netio_release(nb->nio->name);
582 free(nb);
583 }
584
585 bay->nio_list = NULL;
586 return(0);
587 }
588
589 /* Enable a Network IO descriptor for a Network Module */
590 int c2691_nm_enable_nio(c2691_t *router,u_int nm_bay,u_int port_id)
591 {
592 struct c2691_nio_binding *nb;
593 struct c2691_nm_bay *bay;
594
595 if (!(bay = c2691_nm_get_info(router,nm_bay)))
596 return(-1);
597
598 /* check that we have an NIO binding for this interface */
599 if (!(nb = c2691_nm_find_nio_binding(router,nm_bay,port_id)))
600 return(-1);
601
602 /* check that the driver is defined and successfully initialized */
603 if (!bay->nm_driver || !bay->drv_info)
604 return(-1);
605
606 return(bay->nm_driver->nm_set_nio(router,nm_bay,port_id,nb->nio));
607 }
608
609 /* Disable Network IO descriptor of a Network Module */
610 int c2691_nm_disable_nio(c2691_t *router,u_int nm_bay,u_int port_id)
611 {
612 struct c2691_nm_bay *bay;
613
614 if (!(bay = c2691_nm_get_info(router,nm_bay)))
615 return(-1);
616
617 /* check that the driver is defined and successfully initialized */
618 if (!bay->nm_driver || !bay->drv_info)
619 return(-1);
620
621 return(bay->nm_driver->nm_unset_nio(router,nm_bay,port_id));
622 }
623
624 /* Enable all NIO of the specified NM */
625 int c2691_nm_enable_all_nio(c2691_t *router,u_int nm_bay)
626 {
627 struct c2691_nio_binding *nb;
628 struct c2691_nm_bay *bay;
629
630 if (!(bay = c2691_nm_get_info(router,nm_bay)))
631 return(-1);
632
633 /* check that the driver is defined and successfully initialized */
634 if (!bay->nm_driver || !bay->drv_info)
635 return(-1);
636
637 for(nb=bay->nio_list;nb;nb=nb->next)
638 bay->nm_driver->nm_set_nio(router,nm_bay,nb->port_id,nb->nio);
639
640 return(0);
641 }
642
643 /* Disable all NIO of the specified NM */
644 int c2691_nm_disable_all_nio(c2691_t *router,u_int nm_bay)
645 {
646 struct c2691_nio_binding *nb;
647 struct c2691_nm_bay *bay;
648
649 if (!(bay = c2691_nm_get_info(router,nm_bay)))
650 return(-1);
651
652 /* check that the driver is defined and successfully initialized */
653 if (!bay->nm_driver || !bay->drv_info)
654 return(-1);
655
656 for(nb=bay->nio_list;nb;nb=nb->next)
657 bay->nm_driver->nm_unset_nio(router,nm_bay,nb->port_id);
658
659 return(0);
660 }
661
662 /* Initialize a Network Module */
663 int c2691_nm_init(c2691_t *router,u_int nm_bay)
664 {
665 struct c2691_nm_bay *bay;
666 size_t len;
667
668 if (!(bay = c2691_nm_get_info(router,nm_bay)))
669 return(-1);
670
671 /* Check that a device type is defined for this bay */
672 if (!bay->dev_type || !bay->nm_driver) {
673 vm_error(router->vm,"trying to init empty slot %u.\n",nm_bay);
674 return(-1);
675 }
676
677 /* Allocate device name */
678 len = strlen(bay->dev_type) + 10;
679 if (!(bay->dev_name = malloc(len))) {
680 vm_error(router->vm,"unable to allocate device name.\n");
681 return(-1);
682 }
683
684 snprintf(bay->dev_name,len,"%s(%u)",bay->dev_type,nm_bay);
685
686 /* Initialize NM driver */
687 if (bay->nm_driver->nm_init(router,bay->dev_name,nm_bay) == 1) {
688 vm_error(router->vm,"unable to initialize NM %u.\n",nm_bay);
689 return(-1);
690 }
691
692 /* Enable all NIO */
693 c2691_nm_enable_all_nio(router,nm_bay);
694 return(0);
695 }
696
697 /* Shutdown a Network Module */
698 int c2691_nm_shutdown(c2691_t *router,u_int nm_bay)
699 {
700 struct c2691_nm_bay *bay;
701
702 if (!(bay = c2691_nm_get_info(router,nm_bay)))
703 return(-1);
704
705 /* Check that a device type is defined for this bay */
706 if (!bay->dev_type || !bay->nm_driver) {
707 vm_error(router->vm,"trying to shut down empty slot %u.\n",nm_bay);
708 return(-1);
709 }
710
711 /* Disable all NIO */
712 c2691_nm_disable_all_nio(router,nm_bay);
713
714 /* Shutdown the NM driver */
715 if (bay->drv_info && (bay->nm_driver->nm_shutdown(router,nm_bay) == -1)) {
716 vm_error(router->vm,"unable to shutdown NM %u.\n",nm_bay);
717 return(-1);
718 }
719
720 free(bay->dev_name);
721 bay->dev_name = NULL;
722 bay->drv_info = NULL;
723 return(0);
724 }
725
726 /* Shutdown all NM of a router */
727 int c2691_nm_shutdown_all(c2691_t *router)
728 {
729 int i;
730
731 for(i=0;i<C2691_MAX_NM_BAYS;i++) {
732 if (!router->nm_bay[i].dev_type)
733 continue;
734
735 c2691_nm_shutdown(router,i);
736 }
737
738 return(0);
739 }
740
741 /* Show info about all NMs */
742 int c2691_nm_show_all_info(c2691_t *router)
743 {
744 struct c2691_nm_bay *bay;
745 int i;
746
747 for(i=0;i<C2691_MAX_NM_BAYS;i++) {
748 if (!(bay = c2691_nm_get_info(router,i)) || !bay->nm_driver)
749 continue;
750
751 if (bay->nm_driver->nm_show_info != NULL)
752 bay->nm_driver->nm_show_info(router,i);
753 }
754
755 return(0);
756 }
757
758 /* Maximum number of tokens in a NM description */
759 #define NM_DESC_MAX_TOKENS 8
760
761 /* Create a Network Module (command line) */
762 int c2691_cmd_nm_create(c2691_t *router,char *str)
763 {
764 char *tokens[NM_DESC_MAX_TOKENS];
765 int i,count,res;
766 u_int nm_bay;
767
768 /* A port adapter description is like "1:NM-1FE" */
769 if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) != 2) {
770 vm_error(router->vm,"unable to parse NM description '%s'.\n",str);
771 return(-1);
772 }
773
774 /* Parse the NM bay id */
775 nm_bay = atoi(tokens[0]);
776
777 /* Add this new NM to the current NM list */
778 res = c2691_nm_add_binding(router,tokens[1],nm_bay);
779
780 /* The complete array was cleaned by strsplit */
781 for(i=0;i<NM_DESC_MAX_TOKENS;i++)
782 free(tokens[i]);
783
784 return(res);
785 }
786
787 /* Add a Network IO descriptor binding (command line) */
788 int c2691_cmd_add_nio(c2691_t *router,char *str)
789 {
790 char *tokens[NM_DESC_MAX_TOKENS];
791 int i,count,nio_type,res=-1;
792 u_int nm_bay,port_id;
793 netio_desc_t *nio;
794 char nio_name[128];
795
796 /* A port adapter description is like "1:3:tap:tap0" */
797 if ((count = m_strsplit(str,':',tokens,NM_DESC_MAX_TOKENS)) < 3) {
798 vm_error(router->vm,"unable to parse NIO description '%s'.\n",str);
799 return(-1);
800 }
801
802 /* Parse the NM bay */
803 nm_bay = atoi(tokens[0]);
804
805 /* Parse the NM port id */
806 port_id = atoi(tokens[1]);
807
808 /* Autogenerate a NIO name */
809 snprintf(nio_name,sizeof(nio_name),"c2691-i%u/%u/%u",
810 router->vm->instance_id,nm_bay,port_id);
811
812 /* Create the Network IO descriptor */
813 nio = NULL;
814 nio_type = netio_get_type(tokens[2]);
815
816 switch(nio_type) {
817 case NETIO_TYPE_UNIX:
818 if (count != 5) {
819 vm_error(router->vm,
820 "invalid number of arguments for UNIX NIO '%s'\n",str);
821 goto done;
822 }
823
824 nio = netio_desc_create_unix(nio_name,tokens[3],tokens[4]);
825 break;
826
827 case NETIO_TYPE_VDE:
828 if (count != 5) {
829 vm_error(router->vm,
830 "invalid number of arguments for VDE NIO '%s'\n",str);
831 goto done;
832 }
833
834 nio = netio_desc_create_vde(nio_name,tokens[3],tokens[4]);
835 break;
836
837 case NETIO_TYPE_TAP:
838 if (count != 4) {
839 vm_error(router->vm,
840 "invalid number of arguments for TAP NIO '%s'\n",str);
841 goto done;
842 }
843
844 nio = netio_desc_create_tap(nio_name,tokens[3]);
845 break;
846
847 case NETIO_TYPE_UDP:
848 if (count != 6) {
849 vm_error(router->vm,
850 "invalid number of arguments for UDP NIO '%s'\n",str);
851 goto done;
852 }
853
854 nio = netio_desc_create_udp(nio_name,atoi(tokens[3]),
855 tokens[4],atoi(tokens[5]));
856 break;
857
858 case NETIO_TYPE_TCP_CLI:
859 if (count != 5) {
860 vm_error(router->vm,
861 "invalid number of arguments for TCP CLI NIO '%s'\n",str);
862 goto done;
863 }
864
865 nio = netio_desc_create_tcp_cli(nio_name,tokens[3],tokens[4]);
866 break;
867
868 case NETIO_TYPE_TCP_SER:
869 if (count != 4) {
870 vm_error(router->vm,
871 "invalid number of arguments for TCP SER NIO '%s'\n",str);
872 goto done;
873 }
874
875 nio = netio_desc_create_tcp_ser(nio_name,tokens[3]);
876 break;
877
878 case NETIO_TYPE_NULL:
879 nio = netio_desc_create_null(nio_name);
880 break;
881
882 #ifdef LINUX_ETH
883 case NETIO_TYPE_LINUX_ETH:
884 if (count != 4) {
885 vm_error(router->vm,
886 "invalid number of arguments for Linux Eth NIO '%s'\n",
887 str);
888 goto done;
889 }
890
891 nio = netio_desc_create_lnxeth(nio_name,tokens[3]);
892 break;
893 #endif
894
895 #ifdef GEN_ETH
896 case NETIO_TYPE_GEN_ETH:
897 if (count != 4) {
898 vm_error(router->vm,
899 "invalid number of arguments for Generic Eth NIO '%s'\n",
900 str);
901 goto done;
902 }
903
904 nio = netio_desc_create_geneth(nio_name,tokens[3]);
905 break;
906 #endif
907
908 default:
909 vm_error(router->vm,"unknown NETIO type '%s'\n",tokens[2]);
910 goto done;
911 }
912
913 if (!nio) {
914 vm_error(router->vm,"unable to create NETIO "
915 "descriptor for NM slot %u\n",nm_bay);
916 goto done;
917 }
918
919 if (c2691_nm_add_nio_binding(router,nm_bay,port_id,nio_name) == -1) {
920 vm_error(router->vm,"unable to add NETIO binding for slot %u\n",nm_bay);
921 netio_release(nio_name);
922 netio_delete(nio_name);
923 goto done;
924 }
925
926 netio_release(nio_name);
927 res = 0;
928
929 done:
930 /* The complete array was cleaned by strsplit */
931 for(i=0;i<NM_DESC_MAX_TOKENS;i++)
932 free(tokens[i]);
933
934 return(res);
935 }
936
937 /* Show the list of available NM drivers */
938 void c2691_nm_show_drivers(void)
939 {
940 int i;
941
942 printf("Available C2691 Network Module drivers:\n");
943
944 for(i=0;nm_drivers[i];i++) {
945 printf(" * %s %s\n",
946 nm_drivers[i]->dev_type,
947 !nm_drivers[i]->supported ? "(NOT WORKING)" : "");
948 }
949
950 printf("\n");
951 }
952
953 /* Set the base MAC address of the chassis */
954 static int c2691_burn_mac_addr(c2691_t *router,n_eth_addr_t *addr)
955 {
956 m_uint8_t eeprom_ver;
957 size_t offset;
958
959 /* Read EEPROM format version */
960 cisco_eeprom_get_byte(&router->mb_eeprom,0,&eeprom_ver);
961
962 switch(eeprom_ver) {
963 case 0:
964 cisco_eeprom_set_region(&router->mb_eeprom,2,addr->eth_addr_byte,6);
965 break;
966
967 case 4:
968 if (!cisco_eeprom_v4_find_field(&router->mb_eeprom,0xC3,&offset)) {
969 cisco_eeprom_set_region(&router->mb_eeprom,offset,
970 addr->eth_addr_byte,6);
971 }
972 break;
973
974 default:
975 vm_error(router->vm,"c2691_burn_mac_addr: unable to handle "
976 "EEPROM version %u\n",eeprom_ver);
977 return(-1);
978 }
979
980 return(0);
981 }
982
983 /* Set chassis MAC address */
984 int c2691_chassis_set_mac_addr(c2691_t *router,char *mac_addr)
985 {
986 if (parse_mac_addr(&router->mac_addr,mac_addr) == -1) {
987 vm_error(router->vm,"unable to parse MAC address '%s'.\n",mac_addr);
988 return(-1);
989 }
990
991 /* Set the chassis base MAC address */
992 c2691_burn_mac_addr(router,&router->mac_addr);
993 return(0);
994 }
995
996 /* Create the two main PCI busses for a GT64120 based system */
997 static int c2691_init_gt96100(c2691_t *router)
998 {
999 vm_instance_t *vm = router->vm;
1000
1001 vm->pci_bus[0] = pci_bus_create("PCI bus #0",0);
1002 vm->pci_bus[1] = pci_bus_create("PCI bus #1",0);
1003
1004 if (!vm->pci_bus[0] || !vm->pci_bus[1]) {
1005 vm_error(router->vm,"unable to create PCI data.\n");
1006 return(-1);
1007 }
1008
1009 return(dev_gt96100_init(vm,"gt96100",C2691_GT96K_ADDR,0x200000,
1010 C2691_GT96K_IRQ,C2691_NETIO_IRQ));
1011 }
1012
1013 /* Initialize a Cisco 2691 */
1014 static int c2691_init(c2691_t *router)
1015 {
1016 vm_instance_t *vm = router->vm;
1017
1018 /* Set the processor type: R7000 */
1019 mips64_set_prid(vm->boot_cpu,MIPS_PRID_R7000);
1020
1021 /* Initialize the Galileo GT-96100 PCI controller */
1022 if (c2691_init_gt96100(router) == -1)
1023 return(-1);
1024
1025 /* Initialize PCI map (NM slot 1) */
1026 router->nm_bay[1].pci_map = vm->pci_bus[1];
1027
1028 vm->elf_machine_id = C2691_ELF_MACHINE_ID;
1029 return(0);
1030 }
1031
1032 /* Show C2691 hardware info */
1033 void c2691_show_hardware(c2691_t *router)
1034 {
1035 vm_instance_t *vm = router->vm;
1036
1037 printf("C2691 instance '%s' (id %d):\n",vm->name,vm->instance_id);
1038
1039 printf(" VM Status : %d\n",vm->status);
1040 printf(" RAM size : %u Mb\n",vm->ram_size);
1041 printf(" NVRAM size : %u Kb\n",vm->nvram_size);
1042 printf(" IOS image : %s\n\n",vm->ios_image);
1043
1044 if (vm->debug_level > 0) {
1045 dev_show_list(vm);
1046 pci_dev_show_list(vm->pci_bus[0]);
1047 pci_dev_show_list(vm->pci_bus[1]);
1048 printf("\n");
1049 }
1050 }
1051
1052 /* Initialize default parameters for a C2691 */
1053 void c2691_init_defaults(c2691_t *router)
1054 {
1055 vm_instance_t *vm = router->vm;
1056 n_eth_addr_t *m;
1057 m_uint16_t pid;
1058
1059 pid = (m_uint16_t)getpid();
1060
1061 /* Generate a chassis MAC address based on the instance ID */
1062 m = &router->mac_addr;
1063 m->eth_addr_byte[0] = vm_get_mac_addr_msb(vm);
1064 m->eth_addr_byte[1] = vm->instance_id & 0xFF;
1065 m->eth_addr_byte[2] = pid >> 8;
1066 m->eth_addr_byte[3] = pid & 0xFF;
1067 m->eth_addr_byte[4] = 0x00;
1068 m->eth_addr_byte[5] = 0x00;
1069
1070 c2691_init_eeprom_groups(router);
1071 cisco_eeprom_copy(&router->mb_eeprom,&eeprom_c2691_mainboard);
1072 c2691_burn_mac_addr(router,&router->mac_addr);
1073
1074 vm->ram_mmap = C2691_DEFAULT_RAM_MMAP;
1075 vm->ram_size = C2691_DEFAULT_RAM_SIZE;
1076 vm->rom_size = C2691_DEFAULT_ROM_SIZE;
1077 vm->nvram_size = C2691_DEFAULT_NVRAM_SIZE;
1078 vm->conf_reg_setup = C2691_DEFAULT_CONF_REG;
1079 vm->clock_divisor = C2691_DEFAULT_CLOCK_DIV;
1080 vm->nvram_rom_space = C2691_NVRAM_ROM_RES_SIZE;
1081 router->nm_iomem_size = C2691_DEFAULT_IOMEM_SIZE;
1082
1083 vm->pcmcia_disk_size[0] = C2691_DEFAULT_DISK0_SIZE;
1084 vm->pcmcia_disk_size[1] = C2691_DEFAULT_DISK1_SIZE;
1085
1086 /* Enable NVRAM operations to load/store configs */
1087 vm->nvram_extract_config = c2691_nvram_extract_config;
1088 vm->nvram_push_config = c2691_nvram_push_config;
1089 }
1090
1091 /* Initialize the C2691 Platform */
1092 int c2691_init_platform(c2691_t *router)
1093 {
1094 extern m_uint8_t microcode[];
1095 extern ssize_t microcode_len;
1096 vm_instance_t *vm = router->vm;
1097 struct c2691_nm_bay *nm_bay;
1098 cpu_mips_t *cpu;
1099 vm_obj_t *obj;
1100 int i;
1101
1102 /* Copy config register setup into "active" config register */
1103 vm->conf_reg = vm->conf_reg_setup;
1104
1105 /* Create Console and AUX ports */
1106 vm_init_vtty(vm);
1107
1108 /* Create a CPU group */
1109 vm->cpu_group = cpu_group_create("System CPU");
1110
1111 /* Initialize the virtual MIPS processor */
1112 if (!(cpu = cpu_create(vm,0))) {
1113 vm_error(vm,"unable to create CPU!\n");
1114 return(-1);
1115 }
1116
1117 /* Add this CPU to the system CPU group */
1118 cpu_group_add(vm->cpu_group,cpu);
1119 vm->boot_cpu = cpu;
1120
1121 /* Mark the Network IO interrupt as high priority */
1122 cpu->irq_idle_preempt[C2691_NETIO_IRQ] = TRUE;
1123 cpu->irq_idle_preempt[C2691_GT96K_IRQ] = TRUE;
1124 cpu->irq_idle_preempt[C2691_DUART_IRQ] = TRUE;
1125
1126 /* Copy some parameters from VM to CPU (idle PC, ...) */
1127 cpu->idle_pc = vm->idle_pc;
1128
1129 if (vm->timer_irq_check_itv)
1130 cpu->timer_irq_check_itv = vm->timer_irq_check_itv;
1131
1132 /* Remote emulator control */
1133 dev_remote_control_init(vm,0x16000000,0x1000);
1134
1135 /* Specific Storage Area (SSA) */
1136 dev_ram_init(vm,"ssa",TRUE,FALSE,NULL,0x16001000ULL,0x7000);
1137
1138 /* IO FPGA */
1139 if (dev_c2691_iofpga_init(router,C2691_IOFPGA_ADDR,0x40000) == -1)
1140 return(-1);
1141
1142 #if 0
1143 /* PCI IO space */
1144 if (!(vm->pci_io_space = pci_io_data_init(vm,C2691_PCI_IO_ADDR)))
1145 return(-1);
1146 #endif
1147
1148 /* Initialize the chassis */
1149 if (c2691_init(router) == -1)
1150 return(-1);
1151
1152 /* Initialize RAM */
1153 vm_ram_init(vm,0x00000000ULL);
1154
1155 /* Initialize ROM (as a Flash) */
1156 if (!(obj = dev_flash_init(vm,"rom",C2691_ROM_ADDR,vm->rom_size*1048576)))
1157 return(-1);
1158
1159 dev_flash_copy_data(obj,0,microcode,microcode_len);
1160 c2691_nvram_check_empty_config(vm);
1161
1162 /* Initialize the NS16552 DUART */
1163 dev_ns16552_init(vm,C2691_DUART_ADDR,0x1000,3,C2691_DUART_IRQ,
1164 vm->vtty_con,vm->vtty_aux);
1165
1166 /* PCMCIA Slot 0 */
1167 dev_pcmcia_disk_init(vm,"slot0",C2691_SLOT0_ADDR,0x200000,
1168 vm->pcmcia_disk_size[0],1);
1169
1170 /* PCMCIA Slot 1 */
1171 dev_pcmcia_disk_init(vm,"slot1",C2691_SLOT1_ADDR,0x200000,
1172 vm->pcmcia_disk_size[1],1);
1173
1174 /* The GT96100 system controller has 2 integrated FastEthernet ports */
1175 c2691_nm_add_binding(router,"GT96100-FE",0);
1176
1177 /* Initialize Network Modules */
1178 for(i=0;i<C2691_MAX_NM_BAYS;i++) {
1179 nm_bay = &router->nm_bay[i];
1180
1181 if (!nm_bay->dev_type)
1182 continue;
1183
1184 if (c2691_nm_init(router,i) == -1) {
1185 vm_error(vm,"unable to create Network Module \"%s\"\n",
1186 nm_bay->dev_type);
1187 return(-1);
1188 }
1189 }
1190
1191 /* Show device list */
1192 c2691_show_hardware(router);
1193 return(0);
1194 }
1195
1196 /* Boot the IOS image */
1197 int c2691_boot_ios(c2691_t *router)
1198 {
1199 vm_instance_t *vm = router->vm;
1200
1201 if (!vm->boot_cpu)
1202 return(-1);
1203
1204 /* Suspend CPU activity since we will restart directly from ROM */
1205 vm_suspend(vm);
1206
1207 /* Check that CPU activity is really suspended */
1208 if (cpu_group_sync_state(vm->cpu_group) == -1) {
1209 vm_error(vm,"unable to sync with system CPUs.\n");
1210 return(-1);
1211 }
1212
1213 /* Reset the boot CPU */
1214 mips64_reset(vm->boot_cpu);
1215
1216 /* Load IOS image */
1217 if (mips64_load_elf_image(vm->boot_cpu,vm->ios_image,
1218 (vm->ghost_status == VM_GHOST_RAM_USE),
1219 &vm->ios_entry_point) < 0)
1220 {
1221 vm_error(vm,"failed to load Cisco IOS image '%s'.\n",vm->ios_image);
1222 return(-1);
1223 }
1224
1225 /* Launch the simulation */
1226 printf("\nC2691 '%s': starting simulation (CPU0 PC=0x%llx), "
1227 "JIT %sabled.\n",
1228 vm->name,vm->boot_cpu->pc,vm->jit_use ? "en":"dis");
1229
1230 vm_log(vm,"C2691_BOOT",
1231 "starting instance (CPU0 PC=0x%llx,idle_pc=0x%llx,JIT %s)\n",
1232 vm->boot_cpu->pc,vm->boot_cpu->idle_pc,vm->jit_use ? "on":"off");
1233
1234 /* Start main CPU */
1235 if (vm->ghost_status != VM_GHOST_RAM_GENERATE) {
1236 vm->status = VM_STATUS_RUNNING;
1237 cpu_start(vm->boot_cpu);
1238 } else {
1239 vm->status = VM_STATUS_SHUTDOWN;
1240 }
1241 return(0);
1242 }
1243
1244 /* Initialize a Cisco 2691 instance */
1245 int c2691_init_instance(c2691_t *router)
1246 {
1247 vm_instance_t *vm = router->vm;
1248 m_uint32_t rom_entry_point;
1249 cpu_mips_t *cpu0;
1250
1251 if (!vm->ios_image) {
1252 vm_error(vm,"no Cisco IOS image defined.");
1253 return(-1);
1254 }
1255
1256 /* Initialize the C2691 platform */
1257 if (c2691_init_platform(router) == -1) {
1258 vm_error(vm,"unable to initialize the platform hardware.\n");
1259 return(-1);
1260 }
1261
1262 /* Load IOS configuration file */
1263 if (vm->ios_config != NULL) {
1264 vm_nvram_push_config(vm,vm->ios_config);
1265 vm->conf_reg &= ~0x40;
1266 }
1267
1268 /* Load ROM (ELF image or embedded) */
1269 cpu0 = vm->boot_cpu;
1270 rom_entry_point = (m_uint32_t)MIPS_ROM_PC;
1271
1272 if ((vm->rom_filename != NULL) &&
1273 (mips64_load_elf_image(cpu0,vm->rom_filename,0,&rom_entry_point) < 0))
1274 {
1275 vm_error(vm,"unable to load alternate ROM '%s', "
1276 "fallback to embedded ROM.\n\n",vm->rom_filename);
1277 vm->rom_filename = NULL;
1278 }
1279
1280 /* Load symbol file */
1281 if (vm->sym_filename) {
1282 mips64_sym_load_file(cpu0,vm->sym_filename);
1283 cpu0->sym_trace = 1;
1284 }
1285
1286 return(c2691_boot_ios(router));
1287 }
1288
1289 /* Stop a Cisco 2691 instance */
1290 int c2691_stop_instance(c2691_t *router)
1291 {
1292 vm_instance_t *vm = router->vm;
1293
1294 printf("\nC2691 '%s': stopping simulation.\n",vm->name);
1295 vm_log(vm,"C2691_STOP","stopping simulation.\n");
1296
1297 /* Stop all CPUs */
1298 if (vm->cpu_group != NULL) {
1299 vm_stop(vm);
1300
1301 if (cpu_group_sync_state(vm->cpu_group) == -1) {
1302 vm_error(vm,"unable to sync with system CPUs.\n");
1303 return(-1);
1304 }
1305 }
1306
1307 /* Free resources that were used during execution to emulate hardware */
1308 c2691_nm_shutdown_all(router);
1309 vm_hardware_shutdown(vm);
1310 return(0);
1311 }

  ViewVC Help
Powered by ViewVC 1.1.26