1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright Gavin Shan, IBM Corporation 2016.
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
14 #include <net/net_namespace.h>
16 #include <net/genetlink.h>
21 static const int padding_bytes = 26;
23 u32 ncsi_calculate_checksum(unsigned char *data, int len)
28 for (i = 0; i < len; i += 2)
29 checksum += (((u32)data[i] << 8) | data[i + 1]);
31 checksum = (~checksum + 1);
35 /* This function should be called after the data area has been
36 * populated completely.
38 static void ncsi_cmd_build_header(struct ncsi_pkt_hdr *h,
39 struct ncsi_cmd_arg *nca)
45 h->revision = NCSI_PKT_REVISION;
49 h->channel = NCSI_TO_CHANNEL(nca->package,
51 h->length = htons(nca->payload);
55 /* Fill with calculated checksum */
56 checksum = ncsi_calculate_checksum((unsigned char *)h,
57 sizeof(*h) + nca->payload);
58 pchecksum = (__be32 *)((void *)h + sizeof(struct ncsi_pkt_hdr) +
59 ALIGN(nca->payload, 4));
60 *pchecksum = htonl(checksum);
63 static int ncsi_cmd_handler_default(struct sk_buff *skb,
64 struct ncsi_cmd_arg *nca)
66 struct ncsi_cmd_pkt *cmd;
68 cmd = skb_put_zero(skb, sizeof(*cmd));
69 ncsi_cmd_build_header(&cmd->cmd.common, nca);
74 static int ncsi_cmd_handler_sp(struct sk_buff *skb,
75 struct ncsi_cmd_arg *nca)
77 struct ncsi_cmd_sp_pkt *cmd;
79 cmd = skb_put_zero(skb, sizeof(*cmd));
80 cmd->hw_arbitration = nca->bytes[0];
81 ncsi_cmd_build_header(&cmd->cmd.common, nca);
86 static int ncsi_cmd_handler_dc(struct sk_buff *skb,
87 struct ncsi_cmd_arg *nca)
89 struct ncsi_cmd_dc_pkt *cmd;
91 cmd = skb_put_zero(skb, sizeof(*cmd));
92 cmd->ald = nca->bytes[0];
93 ncsi_cmd_build_header(&cmd->cmd.common, nca);
98 static int ncsi_cmd_handler_rc(struct sk_buff *skb,
99 struct ncsi_cmd_arg *nca)
101 struct ncsi_cmd_rc_pkt *cmd;
103 cmd = skb_put_zero(skb, sizeof(*cmd));
104 ncsi_cmd_build_header(&cmd->cmd.common, nca);
109 static int ncsi_cmd_handler_ae(struct sk_buff *skb,
110 struct ncsi_cmd_arg *nca)
112 struct ncsi_cmd_ae_pkt *cmd;
114 cmd = skb_put_zero(skb, sizeof(*cmd));
115 cmd->mc_id = nca->bytes[0];
116 cmd->mode = htonl(nca->dwords[1]);
117 ncsi_cmd_build_header(&cmd->cmd.common, nca);
122 static int ncsi_cmd_handler_sl(struct sk_buff *skb,
123 struct ncsi_cmd_arg *nca)
125 struct ncsi_cmd_sl_pkt *cmd;
127 cmd = skb_put_zero(skb, sizeof(*cmd));
128 cmd->mode = htonl(nca->dwords[0]);
129 cmd->oem_mode = htonl(nca->dwords[1]);
130 ncsi_cmd_build_header(&cmd->cmd.common, nca);
135 static int ncsi_cmd_handler_svf(struct sk_buff *skb,
136 struct ncsi_cmd_arg *nca)
138 struct ncsi_cmd_svf_pkt *cmd;
140 cmd = skb_put_zero(skb, sizeof(*cmd));
141 cmd->vlan = htons(nca->words[1]);
142 cmd->index = nca->bytes[6];
143 cmd->enable = nca->bytes[7];
144 ncsi_cmd_build_header(&cmd->cmd.common, nca);
149 static int ncsi_cmd_handler_ev(struct sk_buff *skb,
150 struct ncsi_cmd_arg *nca)
152 struct ncsi_cmd_ev_pkt *cmd;
154 cmd = skb_put_zero(skb, sizeof(*cmd));
155 cmd->mode = nca->bytes[3];
156 ncsi_cmd_build_header(&cmd->cmd.common, nca);
161 static int ncsi_cmd_handler_sma(struct sk_buff *skb,
162 struct ncsi_cmd_arg *nca)
164 struct ncsi_cmd_sma_pkt *cmd;
167 cmd = skb_put_zero(skb, sizeof(*cmd));
168 for (i = 0; i < 6; i++)
169 cmd->mac[i] = nca->bytes[i];
170 cmd->index = nca->bytes[6];
171 cmd->at_e = nca->bytes[7];
172 ncsi_cmd_build_header(&cmd->cmd.common, nca);
177 static int ncsi_cmd_handler_ebf(struct sk_buff *skb,
178 struct ncsi_cmd_arg *nca)
180 struct ncsi_cmd_ebf_pkt *cmd;
182 cmd = skb_put_zero(skb, sizeof(*cmd));
183 cmd->mode = htonl(nca->dwords[0]);
184 ncsi_cmd_build_header(&cmd->cmd.common, nca);
189 static int ncsi_cmd_handler_egmf(struct sk_buff *skb,
190 struct ncsi_cmd_arg *nca)
192 struct ncsi_cmd_egmf_pkt *cmd;
194 cmd = skb_put_zero(skb, sizeof(*cmd));
195 cmd->mode = htonl(nca->dwords[0]);
196 ncsi_cmd_build_header(&cmd->cmd.common, nca);
201 static int ncsi_cmd_handler_snfc(struct sk_buff *skb,
202 struct ncsi_cmd_arg *nca)
204 struct ncsi_cmd_snfc_pkt *cmd;
206 cmd = skb_put_zero(skb, sizeof(*cmd));
207 cmd->mode = nca->bytes[0];
208 ncsi_cmd_build_header(&cmd->cmd.common, nca);
213 static int ncsi_cmd_handler_oem(struct sk_buff *skb,
214 struct ncsi_cmd_arg *nca)
216 struct ncsi_cmd_oem_pkt *cmd;
219 /* NC-SI spec DSP_0222_1.2.0, section 8.2.2.2
220 * requires payload to be padded with 0 to
221 * 32-bit boundary before the checksum field.
222 * Ensure the padding bytes are accounted for in
226 payload = ALIGN(nca->payload, 4);
227 len = sizeof(struct ncsi_cmd_pkt_hdr) + 4;
228 len += max(payload, padding_bytes);
230 cmd = skb_put_zero(skb, len);
231 unsafe_memcpy(&cmd->mfr_id, nca->data, nca->payload,
232 /* skb allocated with enough to load the payload */);
233 ncsi_cmd_build_header(&cmd->cmd.common, nca);
238 static struct ncsi_cmd_handler {
241 int (*handler)(struct sk_buff *skb,
242 struct ncsi_cmd_arg *nca);
243 } ncsi_cmd_handlers[] = {
244 { NCSI_PKT_CMD_CIS, 0, ncsi_cmd_handler_default },
245 { NCSI_PKT_CMD_SP, 4, ncsi_cmd_handler_sp },
246 { NCSI_PKT_CMD_DP, 0, ncsi_cmd_handler_default },
247 { NCSI_PKT_CMD_EC, 0, ncsi_cmd_handler_default },
248 { NCSI_PKT_CMD_DC, 4, ncsi_cmd_handler_dc },
249 { NCSI_PKT_CMD_RC, 4, ncsi_cmd_handler_rc },
250 { NCSI_PKT_CMD_ECNT, 0, ncsi_cmd_handler_default },
251 { NCSI_PKT_CMD_DCNT, 0, ncsi_cmd_handler_default },
252 { NCSI_PKT_CMD_AE, 8, ncsi_cmd_handler_ae },
253 { NCSI_PKT_CMD_SL, 8, ncsi_cmd_handler_sl },
254 { NCSI_PKT_CMD_GLS, 0, ncsi_cmd_handler_default },
255 { NCSI_PKT_CMD_SVF, 8, ncsi_cmd_handler_svf },
256 { NCSI_PKT_CMD_EV, 4, ncsi_cmd_handler_ev },
257 { NCSI_PKT_CMD_DV, 0, ncsi_cmd_handler_default },
258 { NCSI_PKT_CMD_SMA, 8, ncsi_cmd_handler_sma },
259 { NCSI_PKT_CMD_EBF, 4, ncsi_cmd_handler_ebf },
260 { NCSI_PKT_CMD_DBF, 0, ncsi_cmd_handler_default },
261 { NCSI_PKT_CMD_EGMF, 4, ncsi_cmd_handler_egmf },
262 { NCSI_PKT_CMD_DGMF, 0, ncsi_cmd_handler_default },
263 { NCSI_PKT_CMD_SNFC, 4, ncsi_cmd_handler_snfc },
264 { NCSI_PKT_CMD_GVI, 0, ncsi_cmd_handler_default },
265 { NCSI_PKT_CMD_GC, 0, ncsi_cmd_handler_default },
266 { NCSI_PKT_CMD_GP, 0, ncsi_cmd_handler_default },
267 { NCSI_PKT_CMD_GCPS, 0, ncsi_cmd_handler_default },
268 { NCSI_PKT_CMD_GNS, 0, ncsi_cmd_handler_default },
269 { NCSI_PKT_CMD_GNPTS, 0, ncsi_cmd_handler_default },
270 { NCSI_PKT_CMD_GPS, 0, ncsi_cmd_handler_default },
271 { NCSI_PKT_CMD_OEM, -1, ncsi_cmd_handler_oem },
272 { NCSI_PKT_CMD_PLDM, 0, NULL },
273 { NCSI_PKT_CMD_GPUUID, 0, ncsi_cmd_handler_default }
276 static struct ncsi_request *ncsi_alloc_command(struct ncsi_cmd_arg *nca)
278 struct ncsi_dev_priv *ndp = nca->ndp;
279 struct ncsi_dev *nd = &ndp->ndev;
280 struct net_device *dev = nd->dev;
281 int hlen = LL_RESERVED_SPACE(dev);
282 int tlen = dev->needed_tailroom;
284 int len = hlen + tlen;
286 struct ncsi_request *nr;
288 nr = ncsi_alloc_request(ndp, nca->req_flags);
292 /* NCSI command packet has 16-bytes header, payload, 4 bytes checksum.
293 * Payload needs padding so that the checksum field following payload is
294 * aligned to 32-bit boundary.
295 * The packet needs padding if its payload is less than 26 bytes to
296 * meet 64 bytes minimal ethernet frame length.
298 len += sizeof(struct ncsi_cmd_pkt_hdr) + 4;
299 payload = ALIGN(nca->payload, 4);
300 len += max(payload, padding_bytes);
303 skb = alloc_skb(len, GFP_ATOMIC);
305 ncsi_free_request(nr);
310 skb_reserve(skb, hlen);
311 skb_reset_network_header(skb);
314 skb->protocol = htons(ETH_P_NCSI);
319 int ncsi_xmit_cmd(struct ncsi_cmd_arg *nca)
321 struct ncsi_cmd_handler *nch = NULL;
322 struct ncsi_request *nr;
327 /* Use OEM generic handler for Netlink request */
328 if (nca->req_flags == NCSI_REQ_FLAG_NETLINK_DRIVEN)
329 type = NCSI_PKT_CMD_OEM;
333 /* Search for the handler */
334 for (i = 0; i < ARRAY_SIZE(ncsi_cmd_handlers); i++) {
335 if (ncsi_cmd_handlers[i].type == type) {
336 if (ncsi_cmd_handlers[i].handler)
337 nch = &ncsi_cmd_handlers[i];
346 netdev_err(nca->ndp->ndev.dev,
347 "Cannot send packet with type 0x%02x\n", nca->type);
351 /* Get packet payload length and allocate the request
352 * It is expected that if length set as negative in
353 * handler structure means caller is initializing it
354 * and setting length in nca before calling xmit function
356 if (nch->payload >= 0)
357 nca->payload = nch->payload;
358 nr = ncsi_alloc_command(nca);
362 /* track netlink information */
363 if (nca->req_flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
364 nr->snd_seq = nca->info->snd_seq;
365 nr->snd_portid = nca->info->snd_portid;
366 nr->nlhdr = *nca->info->nlhdr;
369 /* Prepare the packet */
371 ret = nch->handler(nr->cmd, nca);
373 ncsi_free_request(nr);
377 /* Fill the ethernet header */
378 eh = skb_push(nr->cmd, sizeof(*eh));
379 eh->h_proto = htons(ETH_P_NCSI);
380 eth_broadcast_addr(eh->h_dest);
382 /* If mac address received from device then use it for
383 * source address as unicast address else use broadcast
384 * address as source address
386 if (nca->ndp->gma_flag == 1)
387 memcpy(eh->h_source, nca->ndp->ndev.dev->dev_addr, ETH_ALEN);
389 eth_broadcast_addr(eh->h_source);
391 /* Start the timer for the request that might not have
392 * corresponding response. Given NCSI is an internal
393 * connection a 1 second delay should be sufficient.
396 mod_timer(&nr->timer, jiffies + 1 * HZ);
398 /* Send NCSI packet */
400 ret = dev_queue_xmit(nr->cmd);
402 ncsi_free_request(nr);