2 * Copyright Gavin Shan, IBM Corporation 2016.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/skbuff.h>
18 #include <net/net_namespace.h>
20 #include <net/genetlink.h>
24 #include "ncsi-netlink.h"
26 static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
27 unsigned short payload)
29 struct ncsi_rsp_pkt_hdr *h;
33 /* Check NCSI packet header. We don't need validate
34 * the packet type, which should have been checked
35 * before calling this function.
37 h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
39 if (h->common.revision != NCSI_PKT_REVISION) {
40 netdev_dbg(nr->ndp->ndev.dev,
41 "NCSI: unsupported header revision\n");
44 if (ntohs(h->common.length) != payload) {
45 netdev_dbg(nr->ndp->ndev.dev,
46 "NCSI: payload length mismatched\n");
50 /* Check on code and reason */
51 if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
52 ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR) {
53 netdev_dbg(nr->ndp->ndev.dev,
54 "NCSI: non zero response/reason code\n");
58 /* Validate checksum, which might be zeroes if the
59 * sender doesn't support checksum according to NCSI
62 pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
63 if (ntohl(*pchecksum) == 0)
66 checksum = ncsi_calculate_checksum((unsigned char *)h,
67 sizeof(*h) + payload - 4);
69 if (*pchecksum != htonl(checksum)) {
70 netdev_dbg(nr->ndp->ndev.dev, "NCSI: checksum mismatched\n");
77 static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
79 struct ncsi_rsp_pkt *rsp;
80 struct ncsi_dev_priv *ndp = nr->ndp;
81 struct ncsi_package *np;
82 struct ncsi_channel *nc;
85 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
86 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
88 if (ndp->flags & NCSI_DEV_PROBED)
91 id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
92 nc = ncsi_add_channel(np, id);
95 return nc ? 0 : -ENODEV;
98 static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
100 struct ncsi_rsp_pkt *rsp;
101 struct ncsi_dev_priv *ndp = nr->ndp;
102 struct ncsi_package *np;
105 /* Add the package if it's not existing. Otherwise,
106 * to change the state of its child channels.
108 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
109 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
112 if (ndp->flags & NCSI_DEV_PROBED)
115 id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
116 np = ncsi_add_package(ndp, id);
124 static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
126 struct ncsi_rsp_pkt *rsp;
127 struct ncsi_dev_priv *ndp = nr->ndp;
128 struct ncsi_package *np;
129 struct ncsi_channel *nc;
132 /* Find the package */
133 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
134 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
139 /* Change state of all channels attached to the package */
140 NCSI_FOR_EACH_CHANNEL(np, nc) {
141 spin_lock_irqsave(&nc->lock, flags);
142 nc->state = NCSI_CHANNEL_INACTIVE;
143 spin_unlock_irqrestore(&nc->lock, flags);
149 static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
151 struct ncsi_rsp_pkt *rsp;
152 struct ncsi_dev_priv *ndp = nr->ndp;
153 struct ncsi_channel *nc;
154 struct ncsi_channel_mode *ncm;
156 /* Find the package and channel */
157 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
158 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
163 ncm = &nc->modes[NCSI_MODE_ENABLE];
171 static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
173 struct ncsi_rsp_pkt *rsp;
174 struct ncsi_dev_priv *ndp = nr->ndp;
175 struct ncsi_channel *nc;
176 struct ncsi_channel_mode *ncm;
179 ret = ncsi_validate_rsp_pkt(nr, 4);
183 /* Find the package and channel */
184 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
185 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
190 ncm = &nc->modes[NCSI_MODE_ENABLE];
198 static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
200 struct ncsi_rsp_pkt *rsp;
201 struct ncsi_dev_priv *ndp = nr->ndp;
202 struct ncsi_channel *nc;
205 /* Find the package and channel */
206 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
207 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
212 /* Update state for the specified channel */
213 spin_lock_irqsave(&nc->lock, flags);
214 nc->state = NCSI_CHANNEL_INACTIVE;
215 spin_unlock_irqrestore(&nc->lock, flags);
220 static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
222 struct ncsi_rsp_pkt *rsp;
223 struct ncsi_dev_priv *ndp = nr->ndp;
224 struct ncsi_channel *nc;
225 struct ncsi_channel_mode *ncm;
227 /* Find the package and channel */
228 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
229 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
234 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
242 static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
244 struct ncsi_rsp_pkt *rsp;
245 struct ncsi_dev_priv *ndp = nr->ndp;
246 struct ncsi_channel *nc;
247 struct ncsi_channel_mode *ncm;
249 /* Find the package and channel */
250 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
251 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
256 ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
264 static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
266 struct ncsi_cmd_ae_pkt *cmd;
267 struct ncsi_rsp_pkt *rsp;
268 struct ncsi_dev_priv *ndp = nr->ndp;
269 struct ncsi_channel *nc;
270 struct ncsi_channel_mode *ncm;
272 /* Find the package and channel */
273 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
274 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
279 /* Check if the AEN has been enabled */
280 ncm = &nc->modes[NCSI_MODE_AEN];
284 /* Update to AEN configuration */
285 cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
287 ncm->data[0] = cmd->mc_id;
288 ncm->data[1] = ntohl(cmd->mode);
293 static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
295 struct ncsi_cmd_sl_pkt *cmd;
296 struct ncsi_rsp_pkt *rsp;
297 struct ncsi_dev_priv *ndp = nr->ndp;
298 struct ncsi_channel *nc;
299 struct ncsi_channel_mode *ncm;
301 /* Find the package and channel */
302 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
303 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
308 cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
309 ncm = &nc->modes[NCSI_MODE_LINK];
310 ncm->data[0] = ntohl(cmd->mode);
311 ncm->data[1] = ntohl(cmd->oem_mode);
316 static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
318 struct ncsi_rsp_gls_pkt *rsp;
319 struct ncsi_dev_priv *ndp = nr->ndp;
320 struct ncsi_channel *nc;
321 struct ncsi_channel_mode *ncm;
324 /* Find the package and channel */
325 rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
326 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
331 ncm = &nc->modes[NCSI_MODE_LINK];
332 ncm->data[2] = ntohl(rsp->status);
333 ncm->data[3] = ntohl(rsp->other);
334 ncm->data[4] = ntohl(rsp->oem_status);
336 if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
339 /* Reset the channel monitor if it has been enabled */
340 spin_lock_irqsave(&nc->lock, flags);
341 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
342 spin_unlock_irqrestore(&nc->lock, flags);
347 static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
349 struct ncsi_cmd_svf_pkt *cmd;
350 struct ncsi_rsp_pkt *rsp;
351 struct ncsi_dev_priv *ndp = nr->ndp;
352 struct ncsi_channel *nc;
353 struct ncsi_channel_vlan_filter *ncf;
357 /* Find the package and channel */
358 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
359 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
364 cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
365 ncf = &nc->vlan_filter;
366 if (cmd->index == 0 || cmd->index > ncf->n_vids)
369 /* Add or remove the VLAN filter. Remember HW indexes from 1 */
370 spin_lock_irqsave(&nc->lock, flags);
371 bitmap = &ncf->bitmap;
372 if (!(cmd->enable & 0x1)) {
373 if (test_and_clear_bit(cmd->index - 1, bitmap))
374 ncf->vids[cmd->index - 1] = 0;
376 set_bit(cmd->index - 1, bitmap);
377 ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
379 spin_unlock_irqrestore(&nc->lock, flags);
384 static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
386 struct ncsi_cmd_ev_pkt *cmd;
387 struct ncsi_rsp_pkt *rsp;
388 struct ncsi_dev_priv *ndp = nr->ndp;
389 struct ncsi_channel *nc;
390 struct ncsi_channel_mode *ncm;
392 /* Find the package and channel */
393 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
394 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
399 /* Check if VLAN mode has been enabled */
400 ncm = &nc->modes[NCSI_MODE_VLAN];
404 /* Update to VLAN mode */
405 cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
407 ncm->data[0] = ntohl(cmd->mode);
412 static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
414 struct ncsi_rsp_pkt *rsp;
415 struct ncsi_dev_priv *ndp = nr->ndp;
416 struct ncsi_channel *nc;
417 struct ncsi_channel_mode *ncm;
419 /* Find the package and channel */
420 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
421 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
426 /* Check if VLAN mode has been enabled */
427 ncm = &nc->modes[NCSI_MODE_VLAN];
431 /* Update to VLAN mode */
436 static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
438 struct ncsi_cmd_sma_pkt *cmd;
439 struct ncsi_rsp_pkt *rsp;
440 struct ncsi_dev_priv *ndp = nr->ndp;
441 struct ncsi_channel *nc;
442 struct ncsi_channel_mac_filter *ncf;
449 /* Find the package and channel */
450 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
451 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
456 /* According to NCSI spec 1.01, the mixed filter table
457 * isn't supported yet.
459 cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
460 enabled = cmd->at_e & 0x1;
461 ncf = &nc->mac_filter;
462 bitmap = &ncf->bitmap;
464 if (cmd->index == 0 ||
465 cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
468 index = (cmd->index - 1) * ETH_ALEN;
469 spin_lock_irqsave(&nc->lock, flags);
471 set_bit(cmd->index - 1, bitmap);
472 memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
474 clear_bit(cmd->index - 1, bitmap);
475 memset(&ncf->addrs[index], 0, ETH_ALEN);
477 spin_unlock_irqrestore(&nc->lock, flags);
482 static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
484 struct ncsi_cmd_ebf_pkt *cmd;
485 struct ncsi_rsp_pkt *rsp;
486 struct ncsi_dev_priv *ndp = nr->ndp;
487 struct ncsi_channel *nc;
488 struct ncsi_channel_mode *ncm;
490 /* Find the package and channel */
491 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
492 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
496 /* Check if broadcast filter has been enabled */
497 ncm = &nc->modes[NCSI_MODE_BC];
501 /* Update to broadcast filter mode */
502 cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
504 ncm->data[0] = ntohl(cmd->mode);
509 static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
511 struct ncsi_rsp_pkt *rsp;
512 struct ncsi_dev_priv *ndp = nr->ndp;
513 struct ncsi_channel *nc;
514 struct ncsi_channel_mode *ncm;
516 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
517 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
522 /* Check if broadcast filter isn't enabled */
523 ncm = &nc->modes[NCSI_MODE_BC];
527 /* Update to broadcast filter mode */
534 static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
536 struct ncsi_cmd_egmf_pkt *cmd;
537 struct ncsi_rsp_pkt *rsp;
538 struct ncsi_dev_priv *ndp = nr->ndp;
539 struct ncsi_channel *nc;
540 struct ncsi_channel_mode *ncm;
542 /* Find the channel */
543 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
544 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
549 /* Check if multicast filter has been enabled */
550 ncm = &nc->modes[NCSI_MODE_MC];
554 /* Update to multicast filter mode */
555 cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
557 ncm->data[0] = ntohl(cmd->mode);
562 static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
564 struct ncsi_rsp_pkt *rsp;
565 struct ncsi_dev_priv *ndp = nr->ndp;
566 struct ncsi_channel *nc;
567 struct ncsi_channel_mode *ncm;
569 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
570 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
575 /* Check if multicast filter has been enabled */
576 ncm = &nc->modes[NCSI_MODE_MC];
580 /* Update to multicast filter mode */
587 static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
589 struct ncsi_cmd_snfc_pkt *cmd;
590 struct ncsi_rsp_pkt *rsp;
591 struct ncsi_dev_priv *ndp = nr->ndp;
592 struct ncsi_channel *nc;
593 struct ncsi_channel_mode *ncm;
595 /* Find the channel */
596 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
597 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
602 /* Check if flow control has been enabled */
603 ncm = &nc->modes[NCSI_MODE_FC];
607 /* Update to flow control mode */
608 cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
610 ncm->data[0] = cmd->mode;
615 /* Response handler for Mellanox command Get Mac Address */
616 static int ncsi_rsp_handler_oem_mlx_gma(struct ncsi_request *nr)
618 struct ncsi_dev_priv *ndp = nr->ndp;
619 struct net_device *ndev = ndp->ndev.dev;
620 const struct net_device_ops *ops = ndev->netdev_ops;
621 struct ncsi_rsp_oem_pkt *rsp;
622 struct sockaddr saddr;
625 /* Get the response header */
626 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
628 saddr.sa_family = ndev->type;
629 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
630 memcpy(saddr.sa_data, &rsp->data[MLX_MAC_ADDR_OFFSET], ETH_ALEN);
631 ret = ops->ndo_set_mac_address(ndev, &saddr);
633 netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
638 /* Response handler for Mellanox card */
639 static int ncsi_rsp_handler_oem_mlx(struct ncsi_request *nr)
641 struct ncsi_rsp_oem_mlx_pkt *mlx;
642 struct ncsi_rsp_oem_pkt *rsp;
644 /* Get the response header */
645 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
646 mlx = (struct ncsi_rsp_oem_mlx_pkt *)(rsp->data);
648 if (mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
649 mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
650 return ncsi_rsp_handler_oem_mlx_gma(nr);
654 /* Response handler for Broadcom command Get Mac Address */
655 static int ncsi_rsp_handler_oem_bcm_gma(struct ncsi_request *nr)
657 struct ncsi_dev_priv *ndp = nr->ndp;
658 struct net_device *ndev = ndp->ndev.dev;
659 const struct net_device_ops *ops = ndev->netdev_ops;
660 struct ncsi_rsp_oem_pkt *rsp;
661 struct sockaddr saddr;
664 /* Get the response header */
665 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
667 saddr.sa_family = ndev->type;
668 ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
669 memcpy(saddr.sa_data, &rsp->data[BCM_MAC_ADDR_OFFSET], ETH_ALEN);
670 /* Increase mac address by 1 for BMC's address */
671 eth_addr_inc((u8 *)saddr.sa_data);
672 if (!is_valid_ether_addr((const u8 *)saddr.sa_data))
675 ret = ops->ndo_set_mac_address(ndev, &saddr);
677 netdev_warn(ndev, "NCSI: 'Writing mac address to device failed\n");
682 /* Response handler for Broadcom card */
683 static int ncsi_rsp_handler_oem_bcm(struct ncsi_request *nr)
685 struct ncsi_rsp_oem_bcm_pkt *bcm;
686 struct ncsi_rsp_oem_pkt *rsp;
688 /* Get the response header */
689 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
690 bcm = (struct ncsi_rsp_oem_bcm_pkt *)(rsp->data);
692 if (bcm->type == NCSI_OEM_BCM_CMD_GMA)
693 return ncsi_rsp_handler_oem_bcm_gma(nr);
697 static struct ncsi_rsp_oem_handler {
699 int (*handler)(struct ncsi_request *nr);
700 } ncsi_rsp_oem_handlers[] = {
701 { NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
702 { NCSI_OEM_MFR_BCM_ID, ncsi_rsp_handler_oem_bcm }
705 /* Response handler for OEM command */
706 static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
708 struct ncsi_rsp_oem_handler *nrh = NULL;
709 struct ncsi_rsp_oem_pkt *rsp;
710 unsigned int mfr_id, i;
712 /* Get the response header */
713 rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
714 mfr_id = ntohl(rsp->mfr_id);
716 /* Check for manufacturer id and Find the handler */
717 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
718 if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
719 if (ncsi_rsp_oem_handlers[i].handler)
720 nrh = &ncsi_rsp_oem_handlers[i];
729 netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
734 /* Process the packet */
735 return nrh->handler(nr);
738 static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
740 struct ncsi_rsp_gvi_pkt *rsp;
741 struct ncsi_dev_priv *ndp = nr->ndp;
742 struct ncsi_channel *nc;
743 struct ncsi_channel_version *ncv;
746 /* Find the channel */
747 rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
748 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
753 /* Update to channel's version info */
755 ncv->version = ntohl(rsp->ncsi_version);
756 ncv->alpha2 = rsp->alpha2;
757 memcpy(ncv->fw_name, rsp->fw_name, 12);
758 ncv->fw_version = ntohl(rsp->fw_version);
759 for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
760 ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
761 ncv->mf_id = ntohl(rsp->mf_id);
766 static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
768 struct ncsi_rsp_gc_pkt *rsp;
769 struct ncsi_dev_priv *ndp = nr->ndp;
770 struct ncsi_channel *nc;
773 /* Find the channel */
774 rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
775 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
780 /* Update channel's capabilities */
781 nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
782 NCSI_CAP_GENERIC_MASK;
783 nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
785 nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
787 nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
788 nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
790 nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
793 size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
794 nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
795 if (!nc->mac_filter.addrs)
797 nc->mac_filter.n_uc = rsp->uc_cnt;
798 nc->mac_filter.n_mc = rsp->mc_cnt;
799 nc->mac_filter.n_mixed = rsp->mixed_cnt;
801 nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
802 sizeof(*nc->vlan_filter.vids),
804 if (!nc->vlan_filter.vids)
806 /* Set VLAN filters active so they are cleared in the first
807 * configuration state
809 nc->vlan_filter.bitmap = U64_MAX;
810 nc->vlan_filter.n_vids = rsp->vlan_cnt;
815 static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
817 struct ncsi_channel_vlan_filter *ncvf;
818 struct ncsi_channel_mac_filter *ncmf;
819 struct ncsi_dev_priv *ndp = nr->ndp;
820 struct ncsi_rsp_gp_pkt *rsp;
821 struct ncsi_channel *nc;
822 unsigned short enable;
823 unsigned char *pdata;
828 /* Find the channel */
829 rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
830 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
835 /* Modes with explicit enabled indications */
836 if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
837 nc->modes[NCSI_MODE_BC].enable = 1;
838 nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
840 if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
841 nc->modes[NCSI_MODE_ENABLE].enable = 1;
842 if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
843 nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
844 if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
845 nc->modes[NCSI_MODE_MC].enable = 1;
847 /* Modes without explicit enabled indications */
848 nc->modes[NCSI_MODE_LINK].enable = 1;
849 nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
850 nc->modes[NCSI_MODE_VLAN].enable = 1;
851 nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
852 nc->modes[NCSI_MODE_FC].enable = 1;
853 nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
854 nc->modes[NCSI_MODE_AEN].enable = 1;
855 nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
857 /* MAC addresses filter table */
858 pdata = (unsigned char *)rsp + 48;
859 enable = rsp->mac_enable;
860 ncmf = &nc->mac_filter;
861 spin_lock_irqsave(&nc->lock, flags);
862 bitmap = &ncmf->bitmap;
863 for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
864 if (!(enable & (0x1 << i)))
865 clear_bit(i, bitmap);
869 memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
871 spin_unlock_irqrestore(&nc->lock, flags);
873 /* VLAN filter table */
874 enable = ntohs(rsp->vlan_enable);
875 ncvf = &nc->vlan_filter;
876 bitmap = &ncvf->bitmap;
877 spin_lock_irqsave(&nc->lock, flags);
878 for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
879 if (!(enable & (0x1 << i)))
880 clear_bit(i, bitmap);
884 ncvf->vids[i] = ntohs(*(__be16 *)pdata);
886 spin_unlock_irqrestore(&nc->lock, flags);
891 static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
893 struct ncsi_rsp_gcps_pkt *rsp;
894 struct ncsi_dev_priv *ndp = nr->ndp;
895 struct ncsi_channel *nc;
896 struct ncsi_channel_stats *ncs;
898 /* Find the channel */
899 rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
900 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
905 /* Update HNC's statistics */
907 ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
908 ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
909 ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
910 ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
911 ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
912 ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
913 ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
914 ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
915 ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
916 ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
917 ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
918 ncs->hnc_align_err = ntohl(rsp->align_err);
919 ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
920 ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
921 ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
922 ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
923 ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
924 ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
925 ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
926 ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
927 ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
928 ncs->hnc_l_collision = ntohl(rsp->l_collision);
929 ncs->hnc_e_collision = ntohl(rsp->e_collision);
930 ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
931 ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
932 ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
933 ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
934 ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
935 ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
936 ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
937 ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
938 ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
939 ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
940 ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
941 ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
942 ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
943 ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
944 ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
945 ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
946 ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
947 ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
952 static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
954 struct ncsi_rsp_gns_pkt *rsp;
955 struct ncsi_dev_priv *ndp = nr->ndp;
956 struct ncsi_channel *nc;
957 struct ncsi_channel_stats *ncs;
959 /* Find the channel */
960 rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
961 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
966 /* Update HNC's statistics */
968 ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
969 ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
970 ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
971 ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
972 ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
973 ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
974 ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
979 static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
981 struct ncsi_rsp_gnpts_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_gnpts_pkt *)skb_network_header(nr->rsp);
988 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
993 /* Update HNC's statistics */
995 ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
996 ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
997 ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
998 ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
999 ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
1000 ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
1001 ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
1002 ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
1003 ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
1008 static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
1010 struct ncsi_rsp_gps_pkt *rsp;
1011 struct ncsi_dev_priv *ndp = nr->ndp;
1012 struct ncsi_package *np;
1014 /* Find the package */
1015 rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
1016 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1024 static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
1026 struct ncsi_rsp_gpuuid_pkt *rsp;
1027 struct ncsi_dev_priv *ndp = nr->ndp;
1028 struct ncsi_package *np;
1030 /* Find the package */
1031 rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
1032 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1037 memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
1042 static int ncsi_rsp_handler_netlink(struct ncsi_request *nr)
1044 struct ncsi_dev_priv *ndp = nr->ndp;
1045 struct ncsi_rsp_pkt *rsp;
1046 struct ncsi_package *np;
1047 struct ncsi_channel *nc;
1050 /* Find the package */
1051 rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
1052 ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
1057 ret = ncsi_send_netlink_rsp(nr, np, nc);
1062 static struct ncsi_rsp_handler {
1065 int (*handler)(struct ncsi_request *nr);
1066 } ncsi_rsp_handlers[] = {
1067 { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
1068 { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
1069 { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
1070 { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
1071 { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
1072 { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
1073 { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
1074 { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
1075 { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
1076 { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
1077 { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
1078 { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
1079 { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
1080 { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
1081 { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
1082 { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
1083 { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
1084 { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
1085 { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
1086 { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
1087 { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
1088 { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
1089 { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
1090 { NCSI_PKT_RSP_GCPS, 172, ncsi_rsp_handler_gcps },
1091 { NCSI_PKT_RSP_GNS, 172, ncsi_rsp_handler_gns },
1092 { NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts },
1093 { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
1094 { NCSI_PKT_RSP_OEM, -1, ncsi_rsp_handler_oem },
1095 { NCSI_PKT_RSP_PLDM, 0, NULL },
1096 { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid }
1099 int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
1100 struct packet_type *pt, struct net_device *orig_dev)
1102 struct ncsi_rsp_handler *nrh = NULL;
1103 struct ncsi_dev *nd;
1104 struct ncsi_dev_priv *ndp;
1105 struct ncsi_request *nr;
1106 struct ncsi_pkt_hdr *hdr;
1107 unsigned long flags;
1108 int payload, i, ret;
1110 /* Find the NCSI device */
1111 nd = ncsi_find_dev(dev);
1112 ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1116 /* Check if it is AEN packet */
1117 hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
1118 if (hdr->type == NCSI_PKT_AEN)
1119 return ncsi_aen_handler(ndp, skb);
1121 /* Find the handler */
1122 for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
1123 if (ncsi_rsp_handlers[i].type == hdr->type) {
1124 if (ncsi_rsp_handlers[i].handler)
1125 nrh = &ncsi_rsp_handlers[i];
1134 netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
1139 /* Associate with the request */
1140 spin_lock_irqsave(&ndp->lock, flags);
1141 nr = &ndp->requests[hdr->id];
1143 spin_unlock_irqrestore(&ndp->lock, flags);
1149 spin_unlock_irqrestore(&ndp->lock, flags);
1154 /* Validate the packet */
1155 spin_unlock_irqrestore(&ndp->lock, flags);
1156 payload = nrh->payload;
1158 payload = ntohs(hdr->length);
1159 ret = ncsi_validate_rsp_pkt(nr, payload);
1161 netdev_warn(ndp->ndev.dev,
1162 "NCSI: 'bad' packet ignored for type 0x%x\n",
1165 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1169 ncsi_send_netlink_err(ndp->ndev.dev,
1178 /* Process the packet */
1179 ret = nrh->handler(nr);
1181 netdev_err(ndp->ndev.dev,
1182 "NCSI: Handler for packet type 0x%x returned %d\n",
1186 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
1187 ret = ncsi_rsp_handler_netlink(nr);
1189 netdev_err(ndp->ndev.dev,
1190 "NCSI: Netlink handler for packet type 0x%x returned %d\n",
1196 ncsi_free_request(nr);