1 /* 2 * QEMU rocker switch emulation - switch worlds 3 * 4 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include "qemu/iov.h" 18 19 #include "rocker.h" 20 #include "rocker_world.h" 21 22 struct world { 23 Rocker *r; 24 enum rocker_world_type type; 25 WorldOps *ops; 26 }; 27 28 ssize_t world_ingress(World *world, uint32_t pport, 29 const struct iovec *iov, int iovcnt) 30 { 31 if (world->ops->ig) { 32 return world->ops->ig(world, pport, iov, iovcnt); 33 } 34 35 return -1; 36 } 37 38 int world_do_cmd(World *world, DescInfo *info, 39 char *buf, uint16_t cmd, RockerTlv *cmd_info_tlv) 40 { 41 if (world->ops->cmd) { 42 return world->ops->cmd(world, info, buf, cmd, cmd_info_tlv); 43 } 44 45 return -ROCKER_ENOTSUP; 46 } 47 48 World *world_alloc(Rocker *r, size_t sizeof_private, 49 enum rocker_world_type type, WorldOps *ops) 50 { 51 World *w = g_malloc0(sizeof(World) + sizeof_private); 52 53 if (w) { 54 w->r = r; 55 w->type = type; 56 w->ops = ops; 57 if (w->ops->init) { 58 w->ops->init(w); 59 } 60 } 61 62 return w; 63 } 64 65 void world_free(World *world) 66 { 67 if (world->ops->uninit) { 68 world->ops->uninit(world); 69 } 70 g_free(world); 71 } 72 73 void world_reset(World *world) 74 { 75 if (world->ops->uninit) { 76 world->ops->uninit(world); 77 } 78 if (world->ops->init) { 79 world->ops->init(world); 80 } 81 } 82 83 void *world_private(World *world) 84 { 85 return world + 1; 86 } 87 88 Rocker *world_rocker(World *world) 89 { 90 return world->r; 91 } 92 93 enum rocker_world_type world_type(World *world) 94 { 95 return world->type; 96 } 97 98 const char *world_name(World *world) 99 { 100 switch (world->type) { 101 case ROCKER_WORLD_TYPE_OF_DPA: 102 return "OF_DPA"; 103 default: 104 return "unknown"; 105 } 106 } 107