Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ncsi.c
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1/*2* NC-SI (Network Controller Sideband Interface) "echo" model3*4* Copyright (C) 2016-2018 IBM Corp.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* 1. Redistributions of source code must retain the above11* copyright notice, this list of conditions and the following12* disclaimer.13*14* 2. Redistributions in binary form must reproduce the above15* copyright notice, this list of conditions and the following16* disclaimer in the documentation and/or other materials provided17* with the distribution.18*19* 3. Neither the name of the copyright holder nor the names of its20* contributors may be used to endorse or promote products derived21* from this software without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS24* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT25* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS26* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE27* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,28* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES29* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR30* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,32* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED34* OF THE POSSIBILITY OF SUCH DAMAGE.35*/36#include "slirp.h"3738#include "ncsi-pkt.h"3940static uint32_t ncsi_calculate_checksum(uint8_t *data, int len)41{42uint32_t checksum = 0;43int i;4445/*46* 32-bit unsigned sum of the NC-SI packet header and NC-SI packet47* payload interpreted as a series of 16-bit unsigned integer values.48*/49for (i = 0; i < len; i += 2) {50checksum += (((uint16_t) data[i]) << 8) + data[i+1];51}5253checksum = (~checksum + 1);54return checksum;55}5657/* Response handler for Mellanox command Get Mac Address */58static int ncsi_rsp_handler_oem_mlx_gma(Slirp *slirp,59const struct ncsi_pkt_hdr *nh,60struct ncsi_rsp_pkt_hdr *rnh)61{62uint8_t oob_eth_addr_allocated = 0;63struct ncsi_rsp_oem_pkt *rsp;64int i;6566rsp = (struct ncsi_rsp_oem_pkt *)rnh;6768/* Set the payload length */69rsp->rsp.common.length = htons(MLX_GMA_PAYLOAD_LEN);7071for (i = 0; i < ETH_ALEN; i++) {72if (slirp->oob_eth_addr[i] != 0x00) {73oob_eth_addr_allocated = 1;74break;75}76}77rsp->data[MLX_GMA_STATUS_OFFSET] = oob_eth_addr_allocated;7879/* Set the allocated management address */80memcpy(&rsp->data[MLX_MAC_ADDR_OFFSET], slirp->oob_eth_addr, ETH_ALEN);8182return 0;83}8485/* Response handler for Mellanox card */86static int ncsi_rsp_handler_oem_mlx(Slirp *slirp, const struct ncsi_pkt_hdr *nh,87struct ncsi_rsp_pkt_hdr *rnh)88{89const struct ncsi_cmd_oem_pkt *cmd;90const struct ncsi_rsp_oem_mlx_pkt *cmd_mlx;91struct ncsi_rsp_oem_pkt *rsp;92struct ncsi_rsp_oem_mlx_pkt *rsp_mlx;9394/* Get the command header */95cmd = (const struct ncsi_cmd_oem_pkt *)nh;96cmd_mlx = (const struct ncsi_rsp_oem_mlx_pkt *)cmd->data;9798/* Get the response header */99rsp = (struct ncsi_rsp_oem_pkt *)rnh;100rsp_mlx = (struct ncsi_rsp_oem_mlx_pkt *)rsp->data;101102/* Ensure the OEM response header matches the command's */103rsp_mlx->cmd_rev = cmd_mlx->cmd_rev;104rsp_mlx->cmd = cmd_mlx->cmd;105rsp_mlx->param = cmd_mlx->param;106rsp_mlx->optional = cmd_mlx->optional;107108if (cmd_mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&109cmd_mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)110return ncsi_rsp_handler_oem_mlx_gma(slirp, nh, rnh);111112rsp->rsp.common.length = htons(8);113rsp->rsp.code = htons(NCSI_PKT_RSP_C_UNSUPPORTED);114rsp->rsp.reason = htons(NCSI_PKT_RSP_R_UNKNOWN);115return -ENOENT;116}117118static const struct ncsi_rsp_oem_handler {119unsigned int mfr_id;120int (*handler)(Slirp *slirp, const struct ncsi_pkt_hdr *nh,121struct ncsi_rsp_pkt_hdr *rnh);122} ncsi_rsp_oem_handlers[] = {123{ NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },124{ NCSI_OEM_MFR_BCM_ID, NULL },125{ NCSI_OEM_MFR_INTEL_ID, NULL },126};127128/* Response handler for OEM command */129static int ncsi_rsp_handler_oem(Slirp *slirp, const struct ncsi_pkt_hdr *nh,130struct ncsi_rsp_pkt_hdr *rnh)131{132const struct ncsi_rsp_oem_handler *nrh = NULL;133const struct ncsi_cmd_oem_pkt *cmd = (const struct ncsi_cmd_oem_pkt *)nh;134struct ncsi_rsp_oem_pkt *rsp = (struct ncsi_rsp_oem_pkt *)rnh;135uint32_t mfr_id = ntohl(cmd->mfr_id);136int i;137138rsp->mfr_id = cmd->mfr_id;139140if (mfr_id != slirp->mfr_id) {141goto error;142}143144/* Check for manufacturer id and Find the handler */145for (i = 0; i < G_N_ELEMENTS(ncsi_rsp_oem_handlers); i++) {146if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {147if (ncsi_rsp_oem_handlers[i].handler)148nrh = &ncsi_rsp_oem_handlers[i];149else150nrh = NULL;151152break;153}154}155156if (!nrh) {157goto error;158}159160/* Process the packet */161return nrh->handler(slirp, nh, rnh);162163error:164rsp->rsp.common.length = htons(8);165rsp->rsp.code = htons(NCSI_PKT_RSP_C_UNSUPPORTED);166rsp->rsp.reason = htons(NCSI_PKT_RSP_R_UNKNOWN);167return -ENOENT;168}169170171/* Get Version ID */172static int ncsi_rsp_handler_gvi(Slirp *slirp, const struct ncsi_pkt_hdr *nh,173struct ncsi_rsp_pkt_hdr *rnh)174{175struct ncsi_rsp_gvi_pkt *rsp = (struct ncsi_rsp_gvi_pkt *)rnh;176177rsp->ncsi_version = htonl(0xF1F0F000);178rsp->mf_id = htonl(slirp->mfr_id);179180return 0;181}182183/* Get Capabilities */184static int ncsi_rsp_handler_gc(Slirp *slirp, const struct ncsi_pkt_hdr *nh,185struct ncsi_rsp_pkt_hdr *rnh)186{187struct ncsi_rsp_gc_pkt *rsp = (struct ncsi_rsp_gc_pkt *)rnh;188189rsp->cap = htonl(~0);190rsp->bc_cap = htonl(~0);191rsp->mc_cap = htonl(~0);192rsp->buf_cap = htonl(~0);193rsp->aen_cap = htonl(~0);194rsp->vlan_mode = 0xff;195rsp->uc_cnt = 2;196return 0;197}198199/* Get Link status */200static int ncsi_rsp_handler_gls(Slirp *slirp, const struct ncsi_pkt_hdr *nh,201struct ncsi_rsp_pkt_hdr *rnh)202{203struct ncsi_rsp_gls_pkt *rsp = (struct ncsi_rsp_gls_pkt *)rnh;204205rsp->status = htonl(0x1);206return 0;207}208209/* Get Parameters */210static int ncsi_rsp_handler_gp(Slirp *slirp, const struct ncsi_pkt_hdr *nh,211struct ncsi_rsp_pkt_hdr *rnh)212{213struct ncsi_rsp_gp_pkt *rsp = (struct ncsi_rsp_gp_pkt *)rnh;214215/* no MAC address filters or VLAN filters on the channel */216rsp->mac_cnt = 0;217rsp->mac_enable = 0;218rsp->vlan_cnt = 0;219rsp->vlan_enable = 0;220221return 0;222}223224static const struct ncsi_rsp_handler {225unsigned char type;226int payload;227int (*handler)(Slirp *slirp, const struct ncsi_pkt_hdr *nh,228struct ncsi_rsp_pkt_hdr *rnh);229} ncsi_rsp_handlers[] = { { NCSI_PKT_RSP_CIS, 4, NULL },230{ NCSI_PKT_RSP_SP, 4, NULL },231{ NCSI_PKT_RSP_DP, 4, NULL },232{ NCSI_PKT_RSP_EC, 4, NULL },233{ NCSI_PKT_RSP_DC, 4, NULL },234{ NCSI_PKT_RSP_RC, 4, NULL },235{ NCSI_PKT_RSP_ECNT, 4, NULL },236{ NCSI_PKT_RSP_DCNT, 4, NULL },237{ NCSI_PKT_RSP_AE, 4, NULL },238{ NCSI_PKT_RSP_SL, 4, NULL },239{ NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },240{ NCSI_PKT_RSP_SVF, 4, NULL },241{ NCSI_PKT_RSP_EV, 4, NULL },242{ NCSI_PKT_RSP_DV, 4, NULL },243{ NCSI_PKT_RSP_SMA, 4, NULL },244{ NCSI_PKT_RSP_EBF, 4, NULL },245{ NCSI_PKT_RSP_DBF, 4, NULL },246{ NCSI_PKT_RSP_EGMF, 4, NULL },247{ NCSI_PKT_RSP_DGMF, 4, NULL },248{ NCSI_PKT_RSP_SNFC, 4, NULL },249{ NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },250{ NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },251{ NCSI_PKT_RSP_GP, 40, ncsi_rsp_handler_gp },252{ NCSI_PKT_RSP_GCPS, 172, NULL },253{ NCSI_PKT_RSP_GNS, 172, NULL },254{ NCSI_PKT_RSP_GNPTS, 172, NULL },255{ NCSI_PKT_RSP_GPS, 8, NULL },256{ NCSI_PKT_RSP_OEM, 0, ncsi_rsp_handler_oem },257{ NCSI_PKT_RSP_PLDM, 0, NULL },258{ NCSI_PKT_RSP_GPUUID, 20, NULL } };259260/*261* packet format : ncsi header + payload + checksum262*/263#define NCSI_MAX_PAYLOAD 172264#define NCSI_MAX_LEN (sizeof(struct ncsi_pkt_hdr) + NCSI_MAX_PAYLOAD + 4)265266void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)267{268const struct ncsi_pkt_hdr *nh =269(const struct ncsi_pkt_hdr *)(pkt + ETH_HLEN);270uint8_t ncsi_reply[2 + ETH_HLEN + NCSI_MAX_LEN];271struct ethhdr *reh = (struct ethhdr *)(ncsi_reply + 2);272struct ncsi_rsp_pkt_hdr *rnh =273(struct ncsi_rsp_pkt_hdr *)(ncsi_reply + 2 + ETH_HLEN);274const struct ncsi_rsp_handler *handler = NULL;275int i;276int ncsi_rsp_len = sizeof(*nh);277uint32_t checksum;278uint32_t *pchecksum;279280if (pkt_len < ETH_HLEN + sizeof(struct ncsi_pkt_hdr)) {281return; /* packet too short */282}283284memset(ncsi_reply, 0, sizeof(ncsi_reply));285286memset(reh->h_dest, 0xff, ETH_ALEN);287memset(reh->h_source, 0xff, ETH_ALEN);288reh->h_proto = htons(ETH_P_NCSI);289290for (i = 0; i < G_N_ELEMENTS(ncsi_rsp_handlers); i++) {291if (ncsi_rsp_handlers[i].type == nh->type + 0x80) {292handler = &ncsi_rsp_handlers[i];293break;294}295}296297rnh->common.mc_id = nh->mc_id;298rnh->common.revision = NCSI_PKT_REVISION;299rnh->common.id = nh->id;300rnh->common.type = nh->type + 0x80;301rnh->common.channel = nh->channel;302303if (handler) {304rnh->common.length = htons(handler->payload);305rnh->code = htons(NCSI_PKT_RSP_C_COMPLETED);306rnh->reason = htons(NCSI_PKT_RSP_R_NO_ERROR);307308if (handler->handler) {309handler->handler(slirp, nh, rnh);310}311ncsi_rsp_len += ntohs(rnh->common.length);312} else {313rnh->common.length = 0;314rnh->code = htons(NCSI_PKT_RSP_C_UNAVAILABLE);315rnh->reason = htons(NCSI_PKT_RSP_R_UNKNOWN);316}317318/* Add the optional checksum at the end of the frame. */319checksum = ncsi_calculate_checksum((uint8_t *)rnh, ncsi_rsp_len);320pchecksum = (uint32_t *)((char *)rnh + ncsi_rsp_len);321*pchecksum = htonl(checksum);322ncsi_rsp_len += 4;323324slirp_send_packet_all(slirp, ncsi_reply + 2, ETH_HLEN + ncsi_rsp_len);325}326327328