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/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/skbuff.h>
14 #include <net/net_namespace.h>
16 #include <net/genetlink.h>
20 #include "ncsi-netlink.h"
22 /* Nibbles within [0xA, 0xF] add zero "0" to the returned value.
23 * Optional fields (encoded as 0xFF) will default to zero.
25 static u8 decode_bcd_u8(u8 x)
30 lo = lo < 0xA ? lo : 0;
31 hi = hi < 0xA ? hi : 0;
35 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
36 unsigned short payload)
38 struct ncsi_rsp_pkt_hdr *h;
42 /* Check NCSI packet header. We don't need validate
43 * the packet type, which should have been checked
44 * before calling this function.
46 h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
48 if (h->common.revision != NCSI_PKT_REVISION) {
49 netdev_dbg(nr->ndp->ndev.dev,
50 "NCSI: unsupported header revision\n");
53 if (ntohs(h->common.length) != payload) {
54 netdev_dbg(nr->ndp->ndev.dev,
55 "NCSI: payload length mismatched\n");
59 /* Check on code and reason */
60 if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
61 ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR) {
62 netdev_dbg(nr->ndp->ndev.dev,
63 "NCSI: non zero response/reason code %04xh, %04xh\n",
64 ntohs(h->code), ntohs(h->reason));
68 /* Validate checksum, which might be zeroes if the
69 * sender doesn't support checksum according to NCSI
72 pchecksum = (__be32 *)((void *)(h + 1) + ALIGN(payload, 4) - 4);
73 if (ntohl(*pchecksum) == 0)
76 checksum = ncsi_calculate_checksum((unsigned char *)h,
77 sizeof(*h) + payload - 4);
79 if (*pchecksum != htonl(checksum)) {
80 netdev_dbg(nr->ndp->ndev.dev,
81 "NCSI: checksum mismatched; recd: %08x calc: %08x\n",
82 *pchecksum, htonl(checksum));
89 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
91 struct ncsi_rsp_pkt *rsp;
92 struct ncsi_dev_priv *ndp = nr->ndp;
93 struct ncsi_package *np;
94 struct ncsi_channel *nc;
97 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
98 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
100 if (ndp->flags & NCSI_DEV_PROBED)
103 id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
104 nc = ncsi_add_channel(np, id);
107 return nc ? 0 : -ENODEV;
110 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
112 struct ncsi_rsp_pkt *rsp;
113 struct ncsi_dev_priv *ndp = nr->ndp;
114 struct ncsi_package *np;
117 /* Add the package if it's not existing. Otherwise,
118 * to change the state of its child channels.
120 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
121 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
124 if (ndp->flags & NCSI_DEV_PROBED)
127 id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
128 np = ncsi_add_package(ndp, id);
136 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
138 struct ncsi_rsp_pkt *rsp;
139 struct ncsi_dev_priv *ndp = nr->ndp;
140 struct ncsi_package *np;
141 struct ncsi_channel *nc;
144 /* Find the package */
145 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
146 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
151 /* Change state of all channels attached to the package */
152 NCSI_FOR_EACH_CHANNEL(np, nc) {
153 spin_lock_irqsave(&nc->lock, flags);
154 nc->state = NCSI_CHANNEL_INACTIVE;
155 spin_unlock_irqrestore(&nc->lock, flags);
161 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
163 struct ncsi_rsp_pkt *rsp;
164 struct ncsi_dev_priv *ndp = nr->ndp;
165 struct ncsi_channel *nc;
166 struct ncsi_channel_mode *ncm;
168 /* Find the package and channel */
169 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
170 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
175 ncm = &nc->modes[NCSI_MODE_ENABLE];
183 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
185 struct ncsi_rsp_pkt *rsp;
186 struct ncsi_dev_priv *ndp = nr->ndp;
187 struct ncsi_channel *nc;
188 struct ncsi_channel_mode *ncm;
191 ret = ncsi_validate_rsp_pkt(nr, 4);
195 /* Find the package and channel */
196 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
197 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
202 ncm = &nc->modes[NCSI_MODE_ENABLE];
210 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
212 struct ncsi_rsp_pkt *rsp;
213 struct ncsi_dev_priv *ndp = nr->ndp;
214 struct ncsi_channel *nc;
217 /* Find the package and channel */
218 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
219 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
224 /* Update state for the specified channel */
225 spin_lock_irqsave(&nc->lock, flags);
226 nc->state = NCSI_CHANNEL_INACTIVE;
227 spin_unlock_irqrestore(&nc->lock, flags);
232 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
234 struct ncsi_rsp_pkt *rsp;
235 struct ncsi_dev_priv *ndp = nr->ndp;
236 struct ncsi_channel *nc;
237 struct ncsi_channel_mode *ncm;
239 /* Find the package and channel */
240 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
241 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
246 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
254 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
256 struct ncsi_rsp_pkt *rsp;
257 struct ncsi_dev_priv *ndp = nr->ndp;
258 struct ncsi_channel *nc;
259 struct ncsi_channel_mode *ncm;
261 /* Find the package and channel */
262 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
263 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
268 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
276 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
278 struct ncsi_cmd_ae_pkt *cmd;
279 struct ncsi_rsp_pkt *rsp;
280 struct ncsi_dev_priv *ndp = nr->ndp;
281 struct ncsi_channel *nc;
282 struct ncsi_channel_mode *ncm;
284 /* Find the package and channel */
285 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
286 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
291 /* Check if the AEN has been enabled */
292 ncm = &nc->modes[NCSI_MODE_AEN];
296 /* Update to AEN configuration */
297 cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
299 ncm->data[0] = cmd->mc_id;
300 ncm->data[1] = ntohl(cmd->mode);
305 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
307 struct ncsi_cmd_sl_pkt *cmd;
308 struct ncsi_rsp_pkt *rsp;
309 struct ncsi_dev_priv *ndp = nr->ndp;
310 struct ncsi_channel *nc;
311 struct ncsi_channel_mode *ncm;
313 /* Find the package and channel */
314 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
315 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
320 cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
321 ncm = &nc->modes[NCSI_MODE_LINK];
322 ncm->data[0] = ntohl(cmd->mode);
323 ncm->data[1] = ntohl(cmd->oem_mode);
328 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
330 struct ncsi_rsp_gls_pkt *rsp;
331 struct ncsi_dev_priv *ndp = nr->ndp;
332 struct ncsi_channel *nc;
333 struct ncsi_channel_mode *ncm;
336 /* Find the package and channel */
337 rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
338 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
343 ncm = &nc->modes[NCSI_MODE_LINK];
344 ncm->data[2] = ntohl(rsp->status);
345 ncm->data[3] = ntohl(rsp->other);
346 ncm->data[4] = ntohl(rsp->oem_status);
348 if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
351 /* Reset the channel monitor if it has been enabled */
352 spin_lock_irqsave(&nc->lock, flags);
353 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
354 spin_unlock_irqrestore(&nc->lock, flags);
359 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
361 struct ncsi_cmd_svf_pkt *cmd;
362 struct ncsi_rsp_pkt *rsp;
363 struct ncsi_dev_priv *ndp = nr->ndp;
364 struct ncsi_channel *nc;
365 struct ncsi_channel_vlan_filter *ncf;
369 /* Find the package and channel */
370 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
371 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
376 cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
377 ncf = &nc->vlan_filter;
378 if (cmd->index == 0 || cmd->index > ncf->n_vids)
381 /* Add or remove the VLAN filter. Remember HW indexes from 1 */
382 spin_lock_irqsave(&nc->lock, flags);
383 bitmap = &ncf->bitmap;
384 if (!(cmd->enable & 0x1)) {
385 if (test_and_clear_bit(cmd->index - 1, bitmap))
386 ncf->vids[cmd->index - 1] = 0;
388 set_bit(cmd->index - 1, bitmap);
389 ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
391 spin_unlock_irqrestore(&nc->lock, flags);
396 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
398 struct ncsi_cmd_ev_pkt *cmd;
399 struct ncsi_rsp_pkt *rsp;
400 struct ncsi_dev_priv *ndp = nr->ndp;
401 struct ncsi_channel *nc;
402 struct ncsi_channel_mode *ncm;
404 /* Find the package and channel */
405 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
406 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
411 /* Check if VLAN mode has been enabled */
412 ncm = &nc->modes[NCSI_MODE_VLAN];
416 /* Update to VLAN mode */
417 cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
419 ncm->data[0] = ntohl((__force __be32)cmd->mode);
424 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
426 struct ncsi_rsp_pkt *rsp;
427 struct ncsi_dev_priv *ndp = nr->ndp;
428 struct ncsi_channel *nc;
429 struct ncsi_channel_mode *ncm;
431 /* Find the package and channel */
432 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
433 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
438 /* Check if VLAN mode has been enabled */
439 ncm = &nc->modes[NCSI_MODE_VLAN];
443 /* Update to VLAN mode */
448 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
450 struct ncsi_cmd_sma_pkt *cmd;
451 struct ncsi_rsp_pkt *rsp;
452 struct ncsi_dev_priv *ndp = nr->ndp;
453 struct ncsi_channel *nc;
454 struct ncsi_channel_mac_filter *ncf;
461 /* Find the package and channel */
462 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
463 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
468 /* According to NCSI spec 1.01, the mixed filter table
469 * isn't supported yet.
471 cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
472 enabled = cmd->at_e & 0x1;
473 ncf = &nc->mac_filter;
474 bitmap = &ncf->bitmap;
476 if (cmd->index == 0 ||
477 cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
480 index = (cmd->index - 1) * ETH_ALEN;
481 spin_lock_irqsave(&nc->lock, flags);
483 set_bit(cmd->index - 1, bitmap);
484 memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
486 clear_bit(cmd->index - 1, bitmap);
487 eth_zero_addr(&ncf->addrs[index]);
489 spin_unlock_irqrestore(&nc->lock, flags);
494 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
496 struct ncsi_cmd_ebf_pkt *cmd;
497 struct ncsi_rsp_pkt *rsp;
498 struct ncsi_dev_priv *ndp = nr->ndp;
499 struct ncsi_channel *nc;
500 struct ncsi_channel_mode *ncm;
502 /* Find the package and channel */
503 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
504 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
508 /* Check if broadcast filter has been enabled */
509 ncm = &nc->modes[NCSI_MODE_BC];
513 /* Update to broadcast filter mode */
514 cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
516 ncm->data[0] = ntohl(cmd->mode);
521 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
523 struct ncsi_rsp_pkt *rsp;
524 struct ncsi_dev_priv *ndp = nr->ndp;
525 struct ncsi_channel *nc;
526 struct ncsi_channel_mode *ncm;
528 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
529 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
534 /* Check if broadcast filter isn't enabled */
535 ncm = &nc->modes[NCSI_MODE_BC];
539 /* Update to broadcast filter mode */
546 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
548 struct ncsi_cmd_egmf_pkt *cmd;
549 struct ncsi_rsp_pkt *rsp;
550 struct ncsi_dev_priv *ndp = nr->ndp;
551 struct ncsi_channel *nc;
552 struct ncsi_channel_mode *ncm;
554 /* Find the channel */
555 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
556 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
561 /* Check if multicast filter has been enabled */
562 ncm = &nc->modes[NCSI_MODE_MC];
566 /* Update to multicast filter mode */
567 cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
569 ncm->data[0] = ntohl(cmd->mode);
574 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
576 struct ncsi_rsp_pkt *rsp;
577 struct ncsi_dev_priv *ndp = nr->ndp;
578 struct ncsi_channel *nc;
579 struct ncsi_channel_mode *ncm;
581 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
582 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
587 /* Check if multicast filter has been enabled */
588 ncm = &nc->modes[NCSI_MODE_MC];
592 /* Update to multicast filter mode */
599 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
601 struct ncsi_cmd_snfc_pkt *cmd;
602 struct ncsi_rsp_pkt *rsp;
603 struct ncsi_dev_priv *ndp = nr->ndp;
604 struct ncsi_channel *nc;
605 struct ncsi_channel_mode *ncm;
607 /* Find the channel */
608 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
609 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
614 /* Check if flow control has been enabled */
615 ncm = &nc->modes[NCSI_MODE_FC];
619 /* Update to flow control mode */
620 cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
622 ncm->data[0] = cmd->mode;
627 /* Response handler for Get Mac Address command */
628 static int ncsi_rsp_handler_oem_gma(struct ncsi_request *nr, int mfr_id)
630 struct ncsi_dev_priv *ndp = nr->ndp;
631 struct net_device *ndev = ndp->ndev.dev;
632 struct ncsi_rsp_oem_pkt *rsp;
633 struct sockaddr saddr;
634 u32 mac_addr_off = 0;
637 /* Get the response header */
638 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
640 saddr.sa_family = ndev->type;
641 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
642 if (mfr_id == NCSI_OEM_MFR_BCM_ID)
643 mac_addr_off = BCM_MAC_ADDR_OFFSET;
644 else if (mfr_id == NCSI_OEM_MFR_MLX_ID)
645 mac_addr_off = MLX_MAC_ADDR_OFFSET;
646 else if (mfr_id == NCSI_OEM_MFR_INTEL_ID)
647 mac_addr_off = INTEL_MAC_ADDR_OFFSET;
649 memcpy(saddr.sa_data, &rsp->data[mac_addr_off], ETH_ALEN);
650 if (mfr_id == NCSI_OEM_MFR_BCM_ID || mfr_id == NCSI_OEM_MFR_INTEL_ID)
651 eth_addr_inc((u8 *)saddr.sa_data);
652 if (!is_valid_ether_addr((const u8 *)saddr.sa_data))
655 /* Set the flag for GMA command which should only be called once */
659 ret = dev_set_mac_address(ndev, &saddr, NULL);
662 netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
667 /* Response handler for Mellanox card */
668 static int ncsi_rsp_handler_oem_mlx(struct ncsi_request *nr)
670 struct ncsi_rsp_oem_mlx_pkt *mlx;
671 struct ncsi_rsp_oem_pkt *rsp;
673 /* Get the response header */
674 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
675 mlx = (struct ncsi_rsp_oem_mlx_pkt *)(rsp->data);
677 if (mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
678 mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
679 return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_MLX_ID);
683 /* Response handler for Broadcom card */
684 static int ncsi_rsp_handler_oem_bcm(struct ncsi_request *nr)
686 struct ncsi_rsp_oem_bcm_pkt *bcm;
687 struct ncsi_rsp_oem_pkt *rsp;
689 /* Get the response header */
690 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
691 bcm = (struct ncsi_rsp_oem_bcm_pkt *)(rsp->data);
693 if (bcm->type == NCSI_OEM_BCM_CMD_GMA)
694 return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_BCM_ID);
698 /* Response handler for Intel card */
699 static int ncsi_rsp_handler_oem_intel(struct ncsi_request *nr)
701 struct ncsi_rsp_oem_intel_pkt *intel;
702 struct ncsi_rsp_oem_pkt *rsp;
704 /* Get the response header */
705 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
706 intel = (struct ncsi_rsp_oem_intel_pkt *)(rsp->data);
708 if (intel->cmd == NCSI_OEM_INTEL_CMD_GMA)
709 return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_INTEL_ID);
714 static struct ncsi_rsp_oem_handler {
716 int (*handler)(struct ncsi_request *nr);
717 } ncsi_rsp_oem_handlers[] = {
718 { NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
719 { NCSI_OEM_MFR_BCM_ID, ncsi_rsp_handler_oem_bcm },
720 { NCSI_OEM_MFR_INTEL_ID, ncsi_rsp_handler_oem_intel }
723 /* Response handler for OEM command */
724 static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
726 struct ncsi_rsp_oem_handler *nrh = NULL;
727 struct ncsi_rsp_oem_pkt *rsp;
728 unsigned int mfr_id, i;
730 /* Get the response header */
731 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
732 mfr_id = ntohl(rsp->mfr_id);
734 /* Check for manufacturer id and Find the handler */
735 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
736 if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
737 if (ncsi_rsp_oem_handlers[i].handler)
738 nrh = &ncsi_rsp_oem_handlers[i];
747 netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
752 /* Process the packet */
753 return nrh->handler(nr);
756 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
758 struct ncsi_rsp_gvi_pkt *rsp;
759 struct ncsi_dev_priv *ndp = nr->ndp;
760 struct ncsi_channel *nc;
761 struct ncsi_channel_version *ncv;
764 /* Find the channel */
765 rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
766 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
771 /* Update channel's version info
773 * Major, minor, and update fields are supposed to be
774 * unsigned integers encoded as packed BCD.
776 * Alpha1 and alpha2 are ISO/IEC 8859-1 characters.
779 ncv->major = decode_bcd_u8(rsp->major);
780 ncv->minor = decode_bcd_u8(rsp->minor);
781 ncv->update = decode_bcd_u8(rsp->update);
782 ncv->alpha1 = rsp->alpha1;
783 ncv->alpha2 = rsp->alpha2;
784 memcpy(ncv->fw_name, rsp->fw_name, 12);
785 ncv->fw_version = ntohl(rsp->fw_version);
786 for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
787 ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
788 ncv->mf_id = ntohl(rsp->mf_id);
793 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
795 struct ncsi_rsp_gc_pkt *rsp;
796 struct ncsi_dev_priv *ndp = nr->ndp;
797 struct ncsi_channel *nc;
800 /* Find the channel */
801 rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
802 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
807 /* Update channel's capabilities */
808 nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
809 NCSI_CAP_GENERIC_MASK;
810 nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
812 nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
814 nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
815 nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
817 nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
820 size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
821 nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
822 if (!nc->mac_filter.addrs)
824 nc->mac_filter.n_uc = rsp->uc_cnt;
825 nc->mac_filter.n_mc = rsp->mc_cnt;
826 nc->mac_filter.n_mixed = rsp->mixed_cnt;
828 nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
829 sizeof(*nc->vlan_filter.vids),
831 if (!nc->vlan_filter.vids)
833 /* Set VLAN filters active so they are cleared in the first
834 * configuration state
836 nc->vlan_filter.bitmap = U64_MAX;
837 nc->vlan_filter.n_vids = rsp->vlan_cnt;
842 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
844 struct ncsi_channel_vlan_filter *ncvf;
845 struct ncsi_channel_mac_filter *ncmf;
846 struct ncsi_dev_priv *ndp = nr->ndp;
847 struct ncsi_rsp_gp_pkt *rsp;
848 struct ncsi_channel *nc;
849 unsigned short enable;
850 unsigned char *pdata;
855 /* Find the channel */
856 rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
857 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
862 /* Modes with explicit enabled indications */
863 if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
864 nc->modes[NCSI_MODE_BC].enable = 1;
865 nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
867 if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
868 nc->modes[NCSI_MODE_ENABLE].enable = 1;
869 if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
870 nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
871 if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
872 nc->modes[NCSI_MODE_MC].enable = 1;
874 /* Modes without explicit enabled indications */
875 nc->modes[NCSI_MODE_LINK].enable = 1;
876 nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
877 nc->modes[NCSI_MODE_VLAN].enable = 1;
878 nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
879 nc->modes[NCSI_MODE_FC].enable = 1;
880 nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
881 nc->modes[NCSI_MODE_AEN].enable = 1;
882 nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
884 /* MAC addresses filter table */
885 pdata = (unsigned char *)rsp + 48;
886 enable = rsp->mac_enable;
887 ncmf = &nc->mac_filter;
888 spin_lock_irqsave(&nc->lock, flags);
889 bitmap = &ncmf->bitmap;
890 for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
891 if (!(enable & (0x1 << i)))
892 clear_bit(i, bitmap);
896 memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
898 spin_unlock_irqrestore(&nc->lock, flags);
900 /* VLAN filter table */
901 enable = ntohs(rsp->vlan_enable);
902 ncvf = &nc->vlan_filter;
903 bitmap = &ncvf->bitmap;
904 spin_lock_irqsave(&nc->lock, flags);
905 for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
906 if (!(enable & (0x1 << i)))
907 clear_bit(i, bitmap);
911 ncvf->vids[i] = ntohs(*(__be16 *)pdata);
913 spin_unlock_irqrestore(&nc->lock, flags);
918 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
920 struct ncsi_rsp_gcps_pkt *rsp;
921 struct ncsi_dev_priv *ndp = nr->ndp;
922 struct ncsi_channel *nc;
923 struct ncsi_channel_stats *ncs;
925 /* Find the channel */
926 rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
927 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
932 /* Update HNC's statistics */
934 ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
935 ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
936 ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
937 ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
938 ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
939 ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
940 ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
941 ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
942 ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
943 ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
944 ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
945 ncs->hnc_align_err = ntohl(rsp->align_err);
946 ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
947 ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
948 ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
949 ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
950 ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
951 ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
952 ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
953 ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
954 ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
955 ncs->hnc_l_collision = ntohl(rsp->l_collision);
956 ncs->hnc_e_collision = ntohl(rsp->e_collision);
957 ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
958 ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
959 ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
960 ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
961 ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
962 ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
963 ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
964 ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
965 ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
966 ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
967 ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
968 ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
969 ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
970 ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
971 ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
972 ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
973 ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
974 ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
979 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
981 struct ncsi_rsp_gns_pkt *rsp;
982 struct ncsi_dev_priv *ndp = nr->ndp;
983 struct ncsi_channel *nc;
984 struct ncsi_channel_stats *ncs;
986 /* Find the channel */
987 rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
988 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
993 /* Update HNC's statistics */
995 ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
996 ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
997 ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
998 ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
999 ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
1000 ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
1001 ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
1006 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
1008 struct ncsi_rsp_gnpts_pkt *rsp;
1009 struct ncsi_dev_priv *ndp = nr->ndp;
1010 struct ncsi_channel *nc;
1011 struct ncsi_channel_stats *ncs;
1013 /* Find the channel */
1014 rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
1015 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1020 /* Update HNC's statistics */
1022 ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
1023 ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
1024 ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
1025 ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
1026 ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
1027 ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
1028 ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
1029 ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
1030 ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
1035 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
1037 struct ncsi_rsp_gps_pkt *rsp;
1038 struct ncsi_dev_priv *ndp = nr->ndp;
1039 struct ncsi_package *np;
1041 /* Find the package */
1042 rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
1043 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1051 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
1053 struct ncsi_rsp_gpuuid_pkt *rsp;
1054 struct ncsi_dev_priv *ndp = nr->ndp;
1055 struct ncsi_package *np;
1057 /* Find the package */
1058 rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
1059 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1064 memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
1069 static int ncsi_rsp_handler_pldm(struct ncsi_request *nr)
1074 static int ncsi_rsp_handler_netlink(struct ncsi_request *nr)
1076 struct ncsi_dev_priv *ndp = nr->ndp;
1077 struct ncsi_rsp_pkt *rsp;
1078 struct ncsi_package *np;
1079 struct ncsi_channel *nc;
1082 /* Find the package */
1083 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
1084 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1089 ret = ncsi_send_netlink_rsp(nr, np, nc);
1094 static struct ncsi_rsp_handler {
1097 int (*handler)(struct ncsi_request *nr);
1098 } ncsi_rsp_handlers[] = {
1099 { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
1100 { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
1101 { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
1102 { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
1103 { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
1104 { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
1105 { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
1106 { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
1107 { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
1108 { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
1109 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
1110 { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
1111 { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
1112 { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
1113 { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
1114 { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
1115 { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
1116 { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
1117 { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
1118 { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
1119 { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
1120 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
1121 { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
1122 { NCSI_PKT_RSP_GCPS, 204, ncsi_rsp_handler_gcps },
1123 { NCSI_PKT_RSP_GNS, 32, ncsi_rsp_handler_gns },
1124 { NCSI_PKT_RSP_GNPTS, 48, ncsi_rsp_handler_gnpts },
1125 { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
1126 { NCSI_PKT_RSP_OEM, -1, ncsi_rsp_handler_oem },
1127 { NCSI_PKT_RSP_PLDM, -1, ncsi_rsp_handler_pldm },
1128 { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid },
1129 { NCSI_PKT_RSP_QPNPR, -1, ncsi_rsp_handler_pldm },
1130 { NCSI_PKT_RSP_SNPR, -1, ncsi_rsp_handler_pldm }
1133 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
1134 struct packet_type *pt, struct net_device *orig_dev)
1136 struct ncsi_rsp_handler *nrh = NULL;
1137 struct ncsi_dev *nd;
1138 struct ncsi_dev_priv *ndp;
1139 struct ncsi_request *nr;
1140 struct ncsi_pkt_hdr *hdr;
1141 unsigned long flags;
1142 int payload, i, ret;
1144 /* Find the NCSI device */
1145 nd = ncsi_find_dev(orig_dev);
1146 ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1150 /* Check if it is AEN packet */
1151 hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
1152 if (hdr->type == NCSI_PKT_AEN)
1153 return ncsi_aen_handler(ndp, skb);
1155 /* Find the handler */
1156 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
1157 if (ncsi_rsp_handlers[i].type == hdr->type) {
1158 if (ncsi_rsp_handlers[i].handler)
1159 nrh = &ncsi_rsp_handlers[i];
1168 netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1173 /* Associate with the request */
1174 spin_lock_irqsave(&ndp->lock, flags);
1175 nr = &ndp->requests[hdr->id];
1177 spin_unlock_irqrestore(&ndp->lock, flags);
1183 spin_unlock_irqrestore(&ndp->lock, flags);
1188 /* Validate the packet */
1189 spin_unlock_irqrestore(&ndp->lock, flags);
1190 payload = nrh->payload;
1192 payload = ntohs(hdr->length);
1193 ret = ncsi_validate_rsp_pkt(nr, payload);
1195 netdev_warn(ndp->ndev.dev,
1196 "NCSI: 'bad' packet ignored for type 0x%x\n",
1199 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1203 ncsi_send_netlink_err(ndp->ndev.dev,
1212 /* Process the packet */
1213 ret = nrh->handler(nr);
1215 netdev_err(ndp->ndev.dev,
1216 "NCSI: Handler for packet type 0x%x returned %d\n",
1220 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1221 ret = ncsi_rsp_handler_netlink(nr);
1223 netdev_err(ndp->ndev.dev,
1224 "NCSI: Netlink handler for packet type 0x%x returned %d\n",
1230 ncsi_free_request(nr);