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 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
23 unsigned short payload)
25 struct ncsi_rsp_pkt_hdr *h;
29 /* Check NCSI packet header. We don't need validate
30 * the packet type, which should have been checked
31 * before calling this function.
33 h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
35 if (h->common.revision != NCSI_PKT_REVISION) {
36 netdev_dbg(nr->ndp->ndev.dev,
37 "NCSI: unsupported header revision\n");
40 if (ntohs(h->common.length) != payload) {
41 netdev_dbg(nr->ndp->ndev.dev,
42 "NCSI: payload length mismatched\n");
46 /* Check on code and reason */
47 if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
48 ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR) {
49 netdev_dbg(nr->ndp->ndev.dev,
50 "NCSI: non zero response/reason code %04xh, %04xh\n",
51 ntohs(h->code), ntohs(h->reason));
55 /* Validate checksum, which might be zeroes if the
56 * sender doesn't support checksum according to NCSI
59 pchecksum = (__be32 *)((void *)(h + 1) + ALIGN(payload, 4) - 4);
60 if (ntohl(*pchecksum) == 0)
63 checksum = ncsi_calculate_checksum((unsigned char *)h,
64 sizeof(*h) + payload - 4);
66 if (*pchecksum != htonl(checksum)) {
67 netdev_dbg(nr->ndp->ndev.dev,
68 "NCSI: checksum mismatched; recd: %08x calc: %08x\n",
69 *pchecksum, htonl(checksum));
76 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
78 struct ncsi_rsp_pkt *rsp;
79 struct ncsi_dev_priv *ndp = nr->ndp;
80 struct ncsi_package *np;
81 struct ncsi_channel *nc;
84 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
85 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
87 if (ndp->flags & NCSI_DEV_PROBED)
90 id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
91 nc = ncsi_add_channel(np, id);
94 return nc ? 0 : -ENODEV;
97 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
99 struct ncsi_rsp_pkt *rsp;
100 struct ncsi_dev_priv *ndp = nr->ndp;
101 struct ncsi_package *np;
104 /* Add the package if it's not existing. Otherwise,
105 * to change the state of its child channels.
107 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
108 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
111 if (ndp->flags & NCSI_DEV_PROBED)
114 id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
115 np = ncsi_add_package(ndp, id);
123 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
125 struct ncsi_rsp_pkt *rsp;
126 struct ncsi_dev_priv *ndp = nr->ndp;
127 struct ncsi_package *np;
128 struct ncsi_channel *nc;
131 /* Find the package */
132 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
133 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
138 /* Change state of all channels attached to the package */
139 NCSI_FOR_EACH_CHANNEL(np, nc) {
140 spin_lock_irqsave(&nc->lock, flags);
141 nc->state = NCSI_CHANNEL_INACTIVE;
142 spin_unlock_irqrestore(&nc->lock, flags);
148 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
150 struct ncsi_rsp_pkt *rsp;
151 struct ncsi_dev_priv *ndp = nr->ndp;
152 struct ncsi_channel *nc;
153 struct ncsi_channel_mode *ncm;
155 /* Find the package and channel */
156 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
157 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
162 ncm = &nc->modes[NCSI_MODE_ENABLE];
170 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
172 struct ncsi_rsp_pkt *rsp;
173 struct ncsi_dev_priv *ndp = nr->ndp;
174 struct ncsi_channel *nc;
175 struct ncsi_channel_mode *ncm;
178 ret = ncsi_validate_rsp_pkt(nr, 4);
182 /* Find the package and channel */
183 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
184 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
189 ncm = &nc->modes[NCSI_MODE_ENABLE];
197 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
199 struct ncsi_rsp_pkt *rsp;
200 struct ncsi_dev_priv *ndp = nr->ndp;
201 struct ncsi_channel *nc;
204 /* Find the package and channel */
205 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
206 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
211 /* Update state for the specified channel */
212 spin_lock_irqsave(&nc->lock, flags);
213 nc->state = NCSI_CHANNEL_INACTIVE;
214 spin_unlock_irqrestore(&nc->lock, flags);
219 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
221 struct ncsi_rsp_pkt *rsp;
222 struct ncsi_dev_priv *ndp = nr->ndp;
223 struct ncsi_channel *nc;
224 struct ncsi_channel_mode *ncm;
226 /* Find the package and channel */
227 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
228 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
233 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
241 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
243 struct ncsi_rsp_pkt *rsp;
244 struct ncsi_dev_priv *ndp = nr->ndp;
245 struct ncsi_channel *nc;
246 struct ncsi_channel_mode *ncm;
248 /* Find the package and channel */
249 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
250 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
255 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
263 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
265 struct ncsi_cmd_ae_pkt *cmd;
266 struct ncsi_rsp_pkt *rsp;
267 struct ncsi_dev_priv *ndp = nr->ndp;
268 struct ncsi_channel *nc;
269 struct ncsi_channel_mode *ncm;
271 /* Find the package and channel */
272 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
273 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
278 /* Check if the AEN has been enabled */
279 ncm = &nc->modes[NCSI_MODE_AEN];
283 /* Update to AEN configuration */
284 cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
286 ncm->data[0] = cmd->mc_id;
287 ncm->data[1] = ntohl(cmd->mode);
292 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
294 struct ncsi_cmd_sl_pkt *cmd;
295 struct ncsi_rsp_pkt *rsp;
296 struct ncsi_dev_priv *ndp = nr->ndp;
297 struct ncsi_channel *nc;
298 struct ncsi_channel_mode *ncm;
300 /* Find the package and channel */
301 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
302 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
307 cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
308 ncm = &nc->modes[NCSI_MODE_LINK];
309 ncm->data[0] = ntohl(cmd->mode);
310 ncm->data[1] = ntohl(cmd->oem_mode);
315 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
317 struct ncsi_rsp_gls_pkt *rsp;
318 struct ncsi_dev_priv *ndp = nr->ndp;
319 struct ncsi_channel *nc;
320 struct ncsi_channel_mode *ncm;
323 /* Find the package and channel */
324 rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
325 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
330 ncm = &nc->modes[NCSI_MODE_LINK];
331 ncm->data[2] = ntohl(rsp->status);
332 ncm->data[3] = ntohl(rsp->other);
333 ncm->data[4] = ntohl(rsp->oem_status);
335 if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
338 /* Reset the channel monitor if it has been enabled */
339 spin_lock_irqsave(&nc->lock, flags);
340 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
341 spin_unlock_irqrestore(&nc->lock, flags);
346 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
348 struct ncsi_cmd_svf_pkt *cmd;
349 struct ncsi_rsp_pkt *rsp;
350 struct ncsi_dev_priv *ndp = nr->ndp;
351 struct ncsi_channel *nc;
352 struct ncsi_channel_vlan_filter *ncf;
356 /* Find the package and channel */
357 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
358 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
363 cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
364 ncf = &nc->vlan_filter;
365 if (cmd->index == 0 || cmd->index > ncf->n_vids)
368 /* Add or remove the VLAN filter. Remember HW indexes from 1 */
369 spin_lock_irqsave(&nc->lock, flags);
370 bitmap = &ncf->bitmap;
371 if (!(cmd->enable & 0x1)) {
372 if (test_and_clear_bit(cmd->index - 1, bitmap))
373 ncf->vids[cmd->index - 1] = 0;
375 set_bit(cmd->index - 1, bitmap);
376 ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
378 spin_unlock_irqrestore(&nc->lock, flags);
383 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
385 struct ncsi_cmd_ev_pkt *cmd;
386 struct ncsi_rsp_pkt *rsp;
387 struct ncsi_dev_priv *ndp = nr->ndp;
388 struct ncsi_channel *nc;
389 struct ncsi_channel_mode *ncm;
391 /* Find the package and channel */
392 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
393 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
398 /* Check if VLAN mode has been enabled */
399 ncm = &nc->modes[NCSI_MODE_VLAN];
403 /* Update to VLAN mode */
404 cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
406 ncm->data[0] = ntohl((__force __be32)cmd->mode);
411 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
413 struct ncsi_rsp_pkt *rsp;
414 struct ncsi_dev_priv *ndp = nr->ndp;
415 struct ncsi_channel *nc;
416 struct ncsi_channel_mode *ncm;
418 /* Find the package and channel */
419 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
420 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
425 /* Check if VLAN mode has been enabled */
426 ncm = &nc->modes[NCSI_MODE_VLAN];
430 /* Update to VLAN mode */
435 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
437 struct ncsi_cmd_sma_pkt *cmd;
438 struct ncsi_rsp_pkt *rsp;
439 struct ncsi_dev_priv *ndp = nr->ndp;
440 struct ncsi_channel *nc;
441 struct ncsi_channel_mac_filter *ncf;
448 /* Find the package and channel */
449 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
450 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
455 /* According to NCSI spec 1.01, the mixed filter table
456 * isn't supported yet.
458 cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
459 enabled = cmd->at_e & 0x1;
460 ncf = &nc->mac_filter;
461 bitmap = &ncf->bitmap;
463 if (cmd->index == 0 ||
464 cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
467 index = (cmd->index - 1) * ETH_ALEN;
468 spin_lock_irqsave(&nc->lock, flags);
470 set_bit(cmd->index - 1, bitmap);
471 memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
473 clear_bit(cmd->index - 1, bitmap);
474 eth_zero_addr(&ncf->addrs[index]);
476 spin_unlock_irqrestore(&nc->lock, flags);
481 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
483 struct ncsi_cmd_ebf_pkt *cmd;
484 struct ncsi_rsp_pkt *rsp;
485 struct ncsi_dev_priv *ndp = nr->ndp;
486 struct ncsi_channel *nc;
487 struct ncsi_channel_mode *ncm;
489 /* Find the package and channel */
490 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
491 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
495 /* Check if broadcast filter has been enabled */
496 ncm = &nc->modes[NCSI_MODE_BC];
500 /* Update to broadcast filter mode */
501 cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
503 ncm->data[0] = ntohl(cmd->mode);
508 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
510 struct ncsi_rsp_pkt *rsp;
511 struct ncsi_dev_priv *ndp = nr->ndp;
512 struct ncsi_channel *nc;
513 struct ncsi_channel_mode *ncm;
515 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
516 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
521 /* Check if broadcast filter isn't enabled */
522 ncm = &nc->modes[NCSI_MODE_BC];
526 /* Update to broadcast filter mode */
533 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
535 struct ncsi_cmd_egmf_pkt *cmd;
536 struct ncsi_rsp_pkt *rsp;
537 struct ncsi_dev_priv *ndp = nr->ndp;
538 struct ncsi_channel *nc;
539 struct ncsi_channel_mode *ncm;
541 /* Find the channel */
542 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
543 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
548 /* Check if multicast filter has been enabled */
549 ncm = &nc->modes[NCSI_MODE_MC];
553 /* Update to multicast filter mode */
554 cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
556 ncm->data[0] = ntohl(cmd->mode);
561 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
563 struct ncsi_rsp_pkt *rsp;
564 struct ncsi_dev_priv *ndp = nr->ndp;
565 struct ncsi_channel *nc;
566 struct ncsi_channel_mode *ncm;
568 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
569 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
574 /* Check if multicast filter has been enabled */
575 ncm = &nc->modes[NCSI_MODE_MC];
579 /* Update to multicast filter mode */
586 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
588 struct ncsi_cmd_snfc_pkt *cmd;
589 struct ncsi_rsp_pkt *rsp;
590 struct ncsi_dev_priv *ndp = nr->ndp;
591 struct ncsi_channel *nc;
592 struct ncsi_channel_mode *ncm;
594 /* Find the channel */
595 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
596 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
601 /* Check if flow control has been enabled */
602 ncm = &nc->modes[NCSI_MODE_FC];
606 /* Update to flow control mode */
607 cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
609 ncm->data[0] = cmd->mode;
614 /* Response handler for Mellanox command Get Mac Address */
615 static int ncsi_rsp_handler_oem_mlx_gma(struct ncsi_request *nr)
617 struct ncsi_dev_priv *ndp = nr->ndp;
618 struct net_device *ndev = ndp->ndev.dev;
619 const struct net_device_ops *ops = ndev->netdev_ops;
620 struct ncsi_rsp_oem_pkt *rsp;
621 struct sockaddr saddr;
624 /* Get the response header */
625 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
627 saddr.sa_family = ndev->type;
628 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
629 memcpy(saddr.sa_data, &rsp->data[MLX_MAC_ADDR_OFFSET], ETH_ALEN);
630 /* Set the flag for GMA command which should only be called once */
633 ret = ops->ndo_set_mac_address(ndev, &saddr);
635 netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
640 /* Response handler for Mellanox card */
641 static int ncsi_rsp_handler_oem_mlx(struct ncsi_request *nr)
643 struct ncsi_rsp_oem_mlx_pkt *mlx;
644 struct ncsi_rsp_oem_pkt *rsp;
646 /* Get the response header */
647 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
648 mlx = (struct ncsi_rsp_oem_mlx_pkt *)(rsp->data);
650 if (mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
651 mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
652 return ncsi_rsp_handler_oem_mlx_gma(nr);
656 /* Response handler for Broadcom command Get Mac Address */
657 static int ncsi_rsp_handler_oem_bcm_gma(struct ncsi_request *nr)
659 struct ncsi_dev_priv *ndp = nr->ndp;
660 struct net_device *ndev = ndp->ndev.dev;
661 const struct net_device_ops *ops = ndev->netdev_ops;
662 struct ncsi_rsp_oem_pkt *rsp;
663 struct sockaddr saddr;
666 /* Get the response header */
667 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
669 saddr.sa_family = ndev->type;
670 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
671 memcpy(saddr.sa_data, &rsp->data[BCM_MAC_ADDR_OFFSET], ETH_ALEN);
672 /* Increase mac address by 1 for BMC's address */
673 eth_addr_inc((u8 *)saddr.sa_data);
674 if (!is_valid_ether_addr((const u8 *)saddr.sa_data))
677 /* Set the flag for GMA command which should only be called once */
680 ret = ops->ndo_set_mac_address(ndev, &saddr);
682 netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
687 /* Response handler for Broadcom card */
688 static int ncsi_rsp_handler_oem_bcm(struct ncsi_request *nr)
690 struct ncsi_rsp_oem_bcm_pkt *bcm;
691 struct ncsi_rsp_oem_pkt *rsp;
693 /* Get the response header */
694 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
695 bcm = (struct ncsi_rsp_oem_bcm_pkt *)(rsp->data);
697 if (bcm->type == NCSI_OEM_BCM_CMD_GMA)
698 return ncsi_rsp_handler_oem_bcm_gma(nr);
702 /* Response handler for Intel card */
703 static int ncsi_rsp_handler_oem_intel(struct ncsi_request *nr)
708 static struct ncsi_rsp_oem_handler {
710 int (*handler)(struct ncsi_request *nr);
711 } ncsi_rsp_oem_handlers[] = {
712 { NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
713 { NCSI_OEM_MFR_BCM_ID, ncsi_rsp_handler_oem_bcm },
714 { NCSI_OEM_MFR_INTEL_ID, ncsi_rsp_handler_oem_intel }
717 /* Response handler for OEM command */
718 static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
720 struct ncsi_rsp_oem_handler *nrh = NULL;
721 struct ncsi_rsp_oem_pkt *rsp;
722 unsigned int mfr_id, i;
724 /* Get the response header */
725 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
726 mfr_id = ntohl(rsp->mfr_id);
728 /* Check for manufacturer id and Find the handler */
729 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
730 if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
731 if (ncsi_rsp_oem_handlers[i].handler)
732 nrh = &ncsi_rsp_oem_handlers[i];
741 netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
746 /* Process the packet */
747 return nrh->handler(nr);
750 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
752 struct ncsi_rsp_gvi_pkt *rsp;
753 struct ncsi_dev_priv *ndp = nr->ndp;
754 struct ncsi_channel *nc;
755 struct ncsi_channel_version *ncv;
758 /* Find the channel */
759 rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
760 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
765 /* Update to channel's version info */
767 ncv->version = ntohl(rsp->ncsi_version);
768 ncv->alpha2 = rsp->alpha2;
769 memcpy(ncv->fw_name, rsp->fw_name, 12);
770 ncv->fw_version = ntohl(rsp->fw_version);
771 for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
772 ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
773 ncv->mf_id = ntohl(rsp->mf_id);
778 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
780 struct ncsi_rsp_gc_pkt *rsp;
781 struct ncsi_dev_priv *ndp = nr->ndp;
782 struct ncsi_channel *nc;
785 /* Find the channel */
786 rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
787 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
792 /* Update channel's capabilities */
793 nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
794 NCSI_CAP_GENERIC_MASK;
795 nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
797 nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
799 nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
800 nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
802 nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
805 size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
806 nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
807 if (!nc->mac_filter.addrs)
809 nc->mac_filter.n_uc = rsp->uc_cnt;
810 nc->mac_filter.n_mc = rsp->mc_cnt;
811 nc->mac_filter.n_mixed = rsp->mixed_cnt;
813 nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
814 sizeof(*nc->vlan_filter.vids),
816 if (!nc->vlan_filter.vids)
818 /* Set VLAN filters active so they are cleared in the first
819 * configuration state
821 nc->vlan_filter.bitmap = U64_MAX;
822 nc->vlan_filter.n_vids = rsp->vlan_cnt;
827 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
829 struct ncsi_channel_vlan_filter *ncvf;
830 struct ncsi_channel_mac_filter *ncmf;
831 struct ncsi_dev_priv *ndp = nr->ndp;
832 struct ncsi_rsp_gp_pkt *rsp;
833 struct ncsi_channel *nc;
834 unsigned short enable;
835 unsigned char *pdata;
840 /* Find the channel */
841 rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
842 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
847 /* Modes with explicit enabled indications */
848 if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
849 nc->modes[NCSI_MODE_BC].enable = 1;
850 nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
852 if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
853 nc->modes[NCSI_MODE_ENABLE].enable = 1;
854 if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
855 nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
856 if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
857 nc->modes[NCSI_MODE_MC].enable = 1;
859 /* Modes without explicit enabled indications */
860 nc->modes[NCSI_MODE_LINK].enable = 1;
861 nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
862 nc->modes[NCSI_MODE_VLAN].enable = 1;
863 nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
864 nc->modes[NCSI_MODE_FC].enable = 1;
865 nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
866 nc->modes[NCSI_MODE_AEN].enable = 1;
867 nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
869 /* MAC addresses filter table */
870 pdata = (unsigned char *)rsp + 48;
871 enable = rsp->mac_enable;
872 ncmf = &nc->mac_filter;
873 spin_lock_irqsave(&nc->lock, flags);
874 bitmap = &ncmf->bitmap;
875 for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
876 if (!(enable & (0x1 << i)))
877 clear_bit(i, bitmap);
881 memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
883 spin_unlock_irqrestore(&nc->lock, flags);
885 /* VLAN filter table */
886 enable = ntohs(rsp->vlan_enable);
887 ncvf = &nc->vlan_filter;
888 bitmap = &ncvf->bitmap;
889 spin_lock_irqsave(&nc->lock, flags);
890 for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
891 if (!(enable & (0x1 << i)))
892 clear_bit(i, bitmap);
896 ncvf->vids[i] = ntohs(*(__be16 *)pdata);
898 spin_unlock_irqrestore(&nc->lock, flags);
903 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
905 struct ncsi_rsp_gcps_pkt *rsp;
906 struct ncsi_dev_priv *ndp = nr->ndp;
907 struct ncsi_channel *nc;
908 struct ncsi_channel_stats *ncs;
910 /* Find the channel */
911 rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
912 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
917 /* Update HNC's statistics */
919 ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
920 ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
921 ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
922 ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
923 ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
924 ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
925 ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
926 ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
927 ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
928 ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
929 ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
930 ncs->hnc_align_err = ntohl(rsp->align_err);
931 ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
932 ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
933 ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
934 ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
935 ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
936 ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
937 ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
938 ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
939 ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
940 ncs->hnc_l_collision = ntohl(rsp->l_collision);
941 ncs->hnc_e_collision = ntohl(rsp->e_collision);
942 ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
943 ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
944 ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
945 ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
946 ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
947 ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
948 ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
949 ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
950 ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
951 ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
952 ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
953 ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
954 ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
955 ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
956 ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
957 ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
958 ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
959 ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
964 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
966 struct ncsi_rsp_gns_pkt *rsp;
967 struct ncsi_dev_priv *ndp = nr->ndp;
968 struct ncsi_channel *nc;
969 struct ncsi_channel_stats *ncs;
971 /* Find the channel */
972 rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
973 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
978 /* Update HNC's statistics */
980 ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
981 ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
982 ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
983 ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
984 ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
985 ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
986 ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
991 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
993 struct ncsi_rsp_gnpts_pkt *rsp;
994 struct ncsi_dev_priv *ndp = nr->ndp;
995 struct ncsi_channel *nc;
996 struct ncsi_channel_stats *ncs;
998 /* Find the channel */
999 rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
1000 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1005 /* Update HNC's statistics */
1007 ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
1008 ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
1009 ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
1010 ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
1011 ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
1012 ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
1013 ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
1014 ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
1015 ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
1020 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
1022 struct ncsi_rsp_gps_pkt *rsp;
1023 struct ncsi_dev_priv *ndp = nr->ndp;
1024 struct ncsi_package *np;
1026 /* Find the package */
1027 rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
1028 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1036 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
1038 struct ncsi_rsp_gpuuid_pkt *rsp;
1039 struct ncsi_dev_priv *ndp = nr->ndp;
1040 struct ncsi_package *np;
1042 /* Find the package */
1043 rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
1044 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1049 memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
1054 static int ncsi_rsp_handler_pldm(struct ncsi_request *nr)
1059 static int ncsi_rsp_handler_netlink(struct ncsi_request *nr)
1061 struct ncsi_dev_priv *ndp = nr->ndp;
1062 struct ncsi_rsp_pkt *rsp;
1063 struct ncsi_package *np;
1064 struct ncsi_channel *nc;
1067 /* Find the package */
1068 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
1069 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1074 ret = ncsi_send_netlink_rsp(nr, np, nc);
1079 static struct ncsi_rsp_handler {
1082 int (*handler)(struct ncsi_request *nr);
1083 } ncsi_rsp_handlers[] = {
1084 { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
1085 { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
1086 { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
1087 { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
1088 { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
1089 { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
1090 { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
1091 { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
1092 { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
1093 { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
1094 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
1095 { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
1096 { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
1097 { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
1098 { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
1099 { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
1100 { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
1101 { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
1102 { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
1103 { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
1104 { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
1105 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
1106 { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
1107 { NCSI_PKT_RSP_GCPS, 204, ncsi_rsp_handler_gcps },
1108 { NCSI_PKT_RSP_GNS, 32, ncsi_rsp_handler_gns },
1109 { NCSI_PKT_RSP_GNPTS, 48, ncsi_rsp_handler_gnpts },
1110 { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
1111 { NCSI_PKT_RSP_OEM, -1, ncsi_rsp_handler_oem },
1112 { NCSI_PKT_RSP_PLDM, -1, ncsi_rsp_handler_pldm },
1113 { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid },
1114 { NCSI_PKT_RSP_QPNPR, -1, ncsi_rsp_handler_pldm },
1115 { NCSI_PKT_RSP_SNPR, -1, ncsi_rsp_handler_pldm }
1118 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
1119 struct packet_type *pt, struct net_device *orig_dev)
1121 struct ncsi_rsp_handler *nrh = NULL;
1122 struct ncsi_dev *nd;
1123 struct ncsi_dev_priv *ndp;
1124 struct ncsi_request *nr;
1125 struct ncsi_pkt_hdr *hdr;
1126 unsigned long flags;
1127 int payload, i, ret;
1129 /* Find the NCSI device */
1130 nd = ncsi_find_dev(orig_dev);
1131 ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1135 /* Check if it is AEN packet */
1136 hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
1137 if (hdr->type == NCSI_PKT_AEN)
1138 return ncsi_aen_handler(ndp, skb);
1140 /* Find the handler */
1141 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
1142 if (ncsi_rsp_handlers[i].type == hdr->type) {
1143 if (ncsi_rsp_handlers[i].handler)
1144 nrh = &ncsi_rsp_handlers[i];
1153 netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1158 /* Associate with the request */
1159 spin_lock_irqsave(&ndp->lock, flags);
1160 nr = &ndp->requests[hdr->id];
1162 spin_unlock_irqrestore(&ndp->lock, flags);
1168 spin_unlock_irqrestore(&ndp->lock, flags);
1173 /* Validate the packet */
1174 spin_unlock_irqrestore(&ndp->lock, flags);
1175 payload = nrh->payload;
1177 payload = ntohs(hdr->length);
1178 ret = ncsi_validate_rsp_pkt(nr, payload);
1180 netdev_warn(ndp->ndev.dev,
1181 "NCSI: 'bad' packet ignored for type 0x%x\n",
1184 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1188 ncsi_send_netlink_err(ndp->ndev.dev,
1197 /* Process the packet */
1198 ret = nrh->handler(nr);
1200 netdev_err(ndp->ndev.dev,
1201 "NCSI: Handler for packet type 0x%x returned %d\n",
1205 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1206 ret = ncsi_rsp_handler_netlink(nr);
1208 netdev_err(ndp->ndev.dev,
1209 "NCSI: Netlink handler for packet type 0x%x returned %d\n",
1215 ncsi_free_request(nr);