]> Git Repo - qemu.git/blob - slirp/ncsi.c
lm32-softmmu.mak: express dependencies with Kconfig
[qemu.git] / slirp / ncsi.c
1 /*
2  * NC-SI (Network Controller Sideband Interface) "echo" model
3  *
4  * Copyright (C) 2016-2018 IBM Corp.
5  *
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"
10
11 #include "ncsi-pkt.h"
12
13 static uint32_t ncsi_calculate_checksum(uint16_t *data, int len)
14 {
15     uint32_t checksum = 0;
16     int i;
17
18     /*
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.
21      */
22     for (i = 0; i < len; i++) {
23         checksum += htons(data[i]);
24     }
25
26     checksum = (~checksum + 1);
27     return checksum;
28 }
29
30 /* Get Capabilities */
31 static int ncsi_rsp_handler_gc(struct ncsi_rsp_pkt_hdr *rnh)
32 {
33     struct ncsi_rsp_gc_pkt *rsp = (struct ncsi_rsp_gc_pkt *) rnh;
34
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;
43 }
44
45 /* Get Link status */
46 static int ncsi_rsp_handler_gls(struct ncsi_rsp_pkt_hdr *rnh)
47 {
48     struct ncsi_rsp_gls_pkt *rsp = (struct ncsi_rsp_gls_pkt *) rnh;
49
50     rsp->status = htonl(0x1);
51     return 0;
52 }
53
54 /* Get Parameters */
55 static int ncsi_rsp_handler_gp(struct ncsi_rsp_pkt_hdr *rnh)
56 {
57     struct ncsi_rsp_gp_pkt *rsp = (struct ncsi_rsp_gp_pkt *) rnh;
58
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;
64
65     return 0;
66 }
67
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 }
103 };
104
105 /*
106  * packet format : ncsi header + payload + checksum
107  */
108 #define NCSI_MAX_PAYLOAD 172
109 #define NCSI_MAX_LEN     (sizeof(struct ncsi_pkt_hdr) + NCSI_MAX_PAYLOAD + 4)
110
111 void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
112 {
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;
123
124     memset(ncsi_reply, 0, sizeof(ncsi_reply));
125
126     memset(reh->h_dest, 0xff, ETH_ALEN);
127     memset(reh->h_source, 0xff, ETH_ALEN);
128     reh->h_proto = htons(ETH_P_NCSI);
129
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;
134         }
135     }
136
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;
142
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);
147
148         if (handler->handler) {
149             /* TODO: handle errors */
150             handler->handler(rnh);
151         }
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);
157     }
158
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;
164
165     slirp_send_packet_all(slirp, ncsi_reply, ETH_HLEN + ncsi_rsp_len);
166 }
This page took 0.033125 seconds and 4 git commands to generate.