target/arm: Implement VFMAL and VFMSL for aarch32
[qemu/ar7.git] / slirp / ncsi.c
blob359f52c284042237e437c441264b7ecfe0d0b32e
1 /*
2 * NC-SI (Network Controller Sideband Interface) "echo" model
4 * Copyright (C) 2016-2018 IBM Corp.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
9 #include "slirp.h"
11 #include "ncsi-pkt.h"
13 static uint32_t ncsi_calculate_checksum(uint16_t *data, int len)
15 uint32_t checksum = 0;
16 int i;
19 * 32-bit unsigned sum of the NC-SI packet header and NC-SI packet
20 * payload interpreted as a series of 16-bit unsigned integer values.
22 for (i = 0; i < len; i++) {
23 checksum += htons(data[i]);
26 checksum = (~checksum + 1);
27 return checksum;
30 /* Get Capabilities */
31 static int ncsi_rsp_handler_gc(struct ncsi_rsp_pkt_hdr *rnh)
33 struct ncsi_rsp_gc_pkt *rsp = (struct ncsi_rsp_gc_pkt *) rnh;
35 rsp->cap = htonl(~0);
36 rsp->bc_cap = htonl(~0);
37 rsp->mc_cap = htonl(~0);
38 rsp->buf_cap = htonl(~0);
39 rsp->aen_cap = htonl(~0);
40 rsp->vlan_mode = 0xff;
41 rsp->uc_cnt = 2;
42 return 0;
45 /* Get Link status */
46 static int ncsi_rsp_handler_gls(struct ncsi_rsp_pkt_hdr *rnh)
48 struct ncsi_rsp_gls_pkt *rsp = (struct ncsi_rsp_gls_pkt *) rnh;
50 rsp->status = htonl(0x1);
51 return 0;
54 /* Get Parameters */
55 static int ncsi_rsp_handler_gp(struct ncsi_rsp_pkt_hdr *rnh)
57 struct ncsi_rsp_gp_pkt *rsp = (struct ncsi_rsp_gp_pkt *) rnh;
59 /* no MAC address filters or VLAN filters on the channel */
60 rsp->mac_cnt = 0;
61 rsp->mac_enable = 0;
62 rsp->vlan_cnt = 0;
63 rsp->vlan_enable = 0;
65 return 0;
68 static const struct ncsi_rsp_handler {
69 unsigned char type;
70 int payload;
71 int (*handler)(struct ncsi_rsp_pkt_hdr *rnh);
72 } ncsi_rsp_handlers[] = {
73 { NCSI_PKT_RSP_CIS, 4, NULL },
74 { NCSI_PKT_RSP_SP, 4, NULL },
75 { NCSI_PKT_RSP_DP, 4, NULL },
76 { NCSI_PKT_RSP_EC, 4, NULL },
77 { NCSI_PKT_RSP_DC, 4, NULL },
78 { NCSI_PKT_RSP_RC, 4, NULL },
79 { NCSI_PKT_RSP_ECNT, 4, NULL },
80 { NCSI_PKT_RSP_DCNT, 4, NULL },
81 { NCSI_PKT_RSP_AE, 4, NULL },
82 { NCSI_PKT_RSP_SL, 4, NULL },
83 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
84 { NCSI_PKT_RSP_SVF, 4, NULL },
85 { NCSI_PKT_RSP_EV, 4, NULL },
86 { NCSI_PKT_RSP_DV, 4, NULL },
87 { NCSI_PKT_RSP_SMA, 4, NULL },
88 { NCSI_PKT_RSP_EBF, 4, NULL },
89 { NCSI_PKT_RSP_DBF, 4, NULL },
90 { NCSI_PKT_RSP_EGMF, 4, NULL },
91 { NCSI_PKT_RSP_DGMF, 4, NULL },
92 { NCSI_PKT_RSP_SNFC, 4, NULL },
93 { NCSI_PKT_RSP_GVI, 40, NULL },
94 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
95 { NCSI_PKT_RSP_GP, 40, ncsi_rsp_handler_gp },
96 { NCSI_PKT_RSP_GCPS, 172, NULL },
97 { NCSI_PKT_RSP_GNS, 172, NULL },
98 { NCSI_PKT_RSP_GNPTS, 172, NULL },
99 { NCSI_PKT_RSP_GPS, 8, NULL },
100 { NCSI_PKT_RSP_OEM, 0, NULL },
101 { NCSI_PKT_RSP_PLDM, 0, NULL },
102 { NCSI_PKT_RSP_GPUUID, 20, NULL }
106 * packet format : ncsi header + payload + checksum
108 #define NCSI_MAX_PAYLOAD 172
109 #define NCSI_MAX_LEN (sizeof(struct ncsi_pkt_hdr) + NCSI_MAX_PAYLOAD + 4)
111 void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
113 struct ncsi_pkt_hdr *nh = (struct ncsi_pkt_hdr *)(pkt + ETH_HLEN);
114 uint8_t ncsi_reply[ETH_HLEN + NCSI_MAX_LEN];
115 struct ethhdr *reh = (struct ethhdr *)ncsi_reply;
116 struct ncsi_rsp_pkt_hdr *rnh = (struct ncsi_rsp_pkt_hdr *)
117 (ncsi_reply + ETH_HLEN);
118 const struct ncsi_rsp_handler *handler = NULL;
119 int i;
120 int ncsi_rsp_len = sizeof(*nh);
121 uint32_t checksum;
122 uint32_t *pchecksum;
124 memset(ncsi_reply, 0, sizeof(ncsi_reply));
126 memset(reh->h_dest, 0xff, ETH_ALEN);
127 memset(reh->h_source, 0xff, ETH_ALEN);
128 reh->h_proto = htons(ETH_P_NCSI);
130 for (i = 0; i < G_N_ELEMENTS(ncsi_rsp_handlers); i++) {
131 if (ncsi_rsp_handlers[i].type == nh->type + 0x80) {
132 handler = &ncsi_rsp_handlers[i];
133 break;
137 rnh->common.mc_id = nh->mc_id;
138 rnh->common.revision = NCSI_PKT_REVISION;
139 rnh->common.id = nh->id;
140 rnh->common.type = nh->type + 0x80;
141 rnh->common.channel = nh->channel;
143 if (handler) {
144 rnh->common.length = htons(handler->payload);
145 rnh->code = htons(NCSI_PKT_RSP_C_COMPLETED);
146 rnh->reason = htons(NCSI_PKT_RSP_R_NO_ERROR);
148 if (handler->handler) {
149 /* TODO: handle errors */
150 handler->handler(rnh);
152 ncsi_rsp_len += handler->payload;
153 } else {
154 rnh->common.length = 0;
155 rnh->code = htons(NCSI_PKT_RSP_C_UNAVAILABLE);
156 rnh->reason = htons(NCSI_PKT_RSP_R_UNKNOWN);
159 /* Add the optional checksum at the end of the frame. */
160 checksum = ncsi_calculate_checksum((uint16_t *) rnh, ncsi_rsp_len);
161 pchecksum = (uint32_t *)((void *) rnh + ncsi_rsp_len);
162 *pchecksum = htonl(checksum);
163 ncsi_rsp_len += 4;
165 slirp_send_packet_all(slirp, ncsi_reply, ETH_HLEN + ncsi_rsp_len);