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/skbuff.h>
12 #include <linux/platform_device.h>
15 #include <net/net_namespace.h>
17 #include <net/addrconf.h>
19 #include <net/genetlink.h>
23 #include "ncsi-netlink.h"
25 LIST_HEAD(ncsi_dev_list);
26 DEFINE_SPINLOCK(ncsi_dev_lock);
28 bool ncsi_channel_has_link(struct ncsi_channel *channel)
30 return !!(channel->modes[NCSI_MODE_LINK].data[2] & 0x1);
33 bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp,
34 struct ncsi_channel *channel)
36 struct ncsi_package *np;
37 struct ncsi_channel *nc;
39 NCSI_FOR_EACH_PACKAGE(ndp, np)
40 NCSI_FOR_EACH_CHANNEL(np, nc) {
43 if (nc->state == NCSI_CHANNEL_ACTIVE &&
44 ncsi_channel_has_link(nc))
51 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
53 struct ncsi_dev *nd = &ndp->ndev;
54 struct ncsi_package *np;
55 struct ncsi_channel *nc;
58 nd->state = ncsi_dev_state_functional;
65 NCSI_FOR_EACH_PACKAGE(ndp, np) {
66 NCSI_FOR_EACH_CHANNEL(np, nc) {
67 spin_lock_irqsave(&nc->lock, flags);
69 if (!list_empty(&nc->link) ||
70 nc->state != NCSI_CHANNEL_ACTIVE) {
71 spin_unlock_irqrestore(&nc->lock, flags);
75 if (ncsi_channel_has_link(nc)) {
76 spin_unlock_irqrestore(&nc->lock, flags);
81 spin_unlock_irqrestore(&nc->lock, flags);
89 static void ncsi_channel_monitor(struct timer_list *t)
91 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
92 struct ncsi_package *np = nc->package;
93 struct ncsi_dev_priv *ndp = np->ndp;
94 struct ncsi_channel_mode *ncm;
95 struct ncsi_cmd_arg nca;
96 bool enabled, chained;
97 unsigned int monitor_state;
101 spin_lock_irqsave(&nc->lock, flags);
103 chained = !list_empty(&nc->link);
104 enabled = nc->monitor.enabled;
105 monitor_state = nc->monitor.state;
106 spin_unlock_irqrestore(&nc->lock, flags);
109 return; /* expected race disabling timer */
110 if (WARN_ON_ONCE(chained))
113 if (state != NCSI_CHANNEL_INACTIVE &&
114 state != NCSI_CHANNEL_ACTIVE) {
116 netdev_warn(ndp->ndev.dev,
117 "Bad NCSI monitor state channel %d 0x%x %s queue\n",
118 nc->id, state, chained ? "on" : "off");
119 spin_lock_irqsave(&nc->lock, flags);
120 nc->monitor.enabled = false;
121 spin_unlock_irqrestore(&nc->lock, flags);
125 switch (monitor_state) {
126 case NCSI_CHANNEL_MONITOR_START:
127 case NCSI_CHANNEL_MONITOR_RETRY:
129 nca.package = np->id;
130 nca.channel = nc->id;
131 nca.type = NCSI_PKT_CMD_GLS;
133 ret = ncsi_xmit_cmd(&nca);
135 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
138 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
141 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
143 ncsi_report_link(ndp, true);
144 ndp->flags |= NCSI_DEV_RESHUFFLE;
146 ncm = &nc->modes[NCSI_MODE_LINK];
147 spin_lock_irqsave(&nc->lock, flags);
148 nc->monitor.enabled = false;
149 nc->state = NCSI_CHANNEL_INVISIBLE;
150 ncm->data[2] &= ~0x1;
151 spin_unlock_irqrestore(&nc->lock, flags);
153 spin_lock_irqsave(&ndp->lock, flags);
154 nc->state = NCSI_CHANNEL_ACTIVE;
155 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
156 spin_unlock_irqrestore(&ndp->lock, flags);
157 ncsi_process_next_channel(ndp);
161 spin_lock_irqsave(&nc->lock, flags);
163 spin_unlock_irqrestore(&nc->lock, flags);
164 mod_timer(&nc->monitor.timer, jiffies + HZ);
167 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
171 spin_lock_irqsave(&nc->lock, flags);
172 WARN_ON_ONCE(nc->monitor.enabled);
173 nc->monitor.enabled = true;
174 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
175 spin_unlock_irqrestore(&nc->lock, flags);
177 mod_timer(&nc->monitor.timer, jiffies + HZ);
180 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
184 spin_lock_irqsave(&nc->lock, flags);
185 if (!nc->monitor.enabled) {
186 spin_unlock_irqrestore(&nc->lock, flags);
189 nc->monitor.enabled = false;
190 spin_unlock_irqrestore(&nc->lock, flags);
192 del_timer_sync(&nc->monitor.timer);
195 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
198 struct ncsi_channel *nc;
200 NCSI_FOR_EACH_CHANNEL(np, nc) {
208 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
210 struct ncsi_channel *nc, *tmp;
214 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
220 nc->state = NCSI_CHANNEL_INACTIVE;
221 nc->monitor.enabled = false;
222 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
223 spin_lock_init(&nc->lock);
224 INIT_LIST_HEAD(&nc->link);
225 for (index = 0; index < NCSI_CAP_MAX; index++)
226 nc->caps[index].index = index;
227 for (index = 0; index < NCSI_MODE_MAX; index++)
228 nc->modes[index].index = index;
230 spin_lock_irqsave(&np->lock, flags);
231 tmp = ncsi_find_channel(np, id);
233 spin_unlock_irqrestore(&np->lock, flags);
238 list_add_tail_rcu(&nc->node, &np->channels);
240 spin_unlock_irqrestore(&np->lock, flags);
245 static void ncsi_remove_channel(struct ncsi_channel *nc)
247 struct ncsi_package *np = nc->package;
250 spin_lock_irqsave(&nc->lock, flags);
252 /* Release filters */
253 kfree(nc->mac_filter.addrs);
254 kfree(nc->vlan_filter.vids);
256 nc->state = NCSI_CHANNEL_INACTIVE;
257 spin_unlock_irqrestore(&nc->lock, flags);
258 ncsi_stop_channel_monitor(nc);
260 /* Remove and free channel */
261 spin_lock_irqsave(&np->lock, flags);
262 list_del_rcu(&nc->node);
264 spin_unlock_irqrestore(&np->lock, flags);
269 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
272 struct ncsi_package *np;
274 NCSI_FOR_EACH_PACKAGE(ndp, np) {
282 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
285 struct ncsi_package *np, *tmp;
288 np = kzalloc(sizeof(*np), GFP_ATOMIC);
294 spin_lock_init(&np->lock);
295 INIT_LIST_HEAD(&np->channels);
296 np->channel_whitelist = UINT_MAX;
298 spin_lock_irqsave(&ndp->lock, flags);
299 tmp = ncsi_find_package(ndp, id);
301 spin_unlock_irqrestore(&ndp->lock, flags);
306 list_add_tail_rcu(&np->node, &ndp->packages);
308 spin_unlock_irqrestore(&ndp->lock, flags);
313 void ncsi_remove_package(struct ncsi_package *np)
315 struct ncsi_dev_priv *ndp = np->ndp;
316 struct ncsi_channel *nc, *tmp;
319 /* Release all child channels */
320 list_for_each_entry_safe(nc, tmp, &np->channels, node)
321 ncsi_remove_channel(nc);
323 /* Remove and free package */
324 spin_lock_irqsave(&ndp->lock, flags);
325 list_del_rcu(&np->node);
327 spin_unlock_irqrestore(&ndp->lock, flags);
332 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
334 struct ncsi_package **np,
335 struct ncsi_channel **nc)
337 struct ncsi_package *p;
338 struct ncsi_channel *c;
340 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
341 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
349 /* For two consecutive NCSI commands, the packet IDs shouldn't
350 * be same. Otherwise, the bogus response might be replied. So
351 * the available IDs are allocated in round-robin fashion.
353 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
354 unsigned int req_flags)
356 struct ncsi_request *nr = NULL;
357 int i, limit = ARRAY_SIZE(ndp->requests);
360 /* Check if there is one available request until the ceiling */
361 spin_lock_irqsave(&ndp->lock, flags);
362 for (i = ndp->request_id; i < limit; i++) {
363 if (ndp->requests[i].used)
366 nr = &ndp->requests[i];
368 nr->flags = req_flags;
369 ndp->request_id = i + 1;
373 /* Fail back to check from the starting cursor */
374 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
375 if (ndp->requests[i].used)
378 nr = &ndp->requests[i];
380 nr->flags = req_flags;
381 ndp->request_id = i + 1;
386 spin_unlock_irqrestore(&ndp->lock, flags);
390 void ncsi_free_request(struct ncsi_request *nr)
392 struct ncsi_dev_priv *ndp = nr->ndp;
393 struct sk_buff *cmd, *rsp;
399 del_timer_sync(&nr->timer);
402 spin_lock_irqsave(&ndp->lock, flags);
408 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
409 spin_unlock_irqrestore(&ndp->lock, flags);
411 if (driven && cmd && --ndp->pending_req_num == 0)
412 schedule_work(&ndp->work);
414 /* Release command and response */
419 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
421 struct ncsi_dev_priv *ndp;
423 NCSI_FOR_EACH_DEV(ndp) {
424 if (ndp->ndev.dev == dev)
431 static void ncsi_request_timeout(struct timer_list *t)
433 struct ncsi_request *nr = from_timer(nr, t, timer);
434 struct ncsi_dev_priv *ndp = nr->ndp;
435 struct ncsi_cmd_pkt *cmd;
436 struct ncsi_package *np;
437 struct ncsi_channel *nc;
440 /* If the request already had associated response,
441 * let the response handler to release it.
443 spin_lock_irqsave(&ndp->lock, flags);
445 if (nr->rsp || !nr->cmd) {
446 spin_unlock_irqrestore(&ndp->lock, flags);
449 spin_unlock_irqrestore(&ndp->lock, flags);
451 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
453 /* Find the package */
454 cmd = (struct ncsi_cmd_pkt *)
455 skb_network_header(nr->cmd);
456 ncsi_find_package_and_channel(ndp,
457 cmd->cmd.common.channel,
459 ncsi_send_netlink_timeout(nr, np, nc);
463 /* Release the request */
464 ncsi_free_request(nr);
467 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
469 struct ncsi_dev *nd = &ndp->ndev;
470 struct ncsi_package *np;
471 struct ncsi_channel *nc, *tmp;
472 struct ncsi_cmd_arg nca;
476 np = ndp->active_package;
477 nc = ndp->active_channel;
479 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
481 case ncsi_dev_state_suspend:
482 nd->state = ncsi_dev_state_suspend_select;
484 case ncsi_dev_state_suspend_select:
485 ndp->pending_req_num = 1;
487 nca.type = NCSI_PKT_CMD_SP;
488 nca.package = np->id;
489 nca.channel = NCSI_RESERVED_CHANNEL;
490 if (ndp->flags & NCSI_DEV_HWA)
495 /* To retrieve the last link states of channels in current
496 * package when current active channel needs fail over to
497 * another one. It means we will possibly select another
498 * channel as next active one. The link states of channels
499 * are most important factor of the selection. So we need
500 * accurate link states. Unfortunately, the link states on
501 * inactive channels can't be updated with LSC AEN in time.
503 if (ndp->flags & NCSI_DEV_RESHUFFLE)
504 nd->state = ncsi_dev_state_suspend_gls;
506 nd->state = ncsi_dev_state_suspend_dcnt;
507 ret = ncsi_xmit_cmd(&nca);
512 case ncsi_dev_state_suspend_gls:
513 ndp->pending_req_num = np->channel_num;
515 nca.type = NCSI_PKT_CMD_GLS;
516 nca.package = np->id;
518 nd->state = ncsi_dev_state_suspend_dcnt;
519 NCSI_FOR_EACH_CHANNEL(np, nc) {
520 nca.channel = nc->id;
521 ret = ncsi_xmit_cmd(&nca);
527 case ncsi_dev_state_suspend_dcnt:
528 ndp->pending_req_num = 1;
530 nca.type = NCSI_PKT_CMD_DCNT;
531 nca.package = np->id;
532 nca.channel = nc->id;
534 nd->state = ncsi_dev_state_suspend_dc;
535 ret = ncsi_xmit_cmd(&nca);
540 case ncsi_dev_state_suspend_dc:
541 ndp->pending_req_num = 1;
543 nca.type = NCSI_PKT_CMD_DC;
544 nca.package = np->id;
545 nca.channel = nc->id;
548 nd->state = ncsi_dev_state_suspend_deselect;
549 ret = ncsi_xmit_cmd(&nca);
553 NCSI_FOR_EACH_CHANNEL(np, tmp) {
554 /* If there is another channel active on this package
555 * do not deselect the package.
557 if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) {
558 nd->state = ncsi_dev_state_suspend_done;
563 case ncsi_dev_state_suspend_deselect:
564 ndp->pending_req_num = 1;
566 nca.type = NCSI_PKT_CMD_DP;
567 nca.package = np->id;
568 nca.channel = NCSI_RESERVED_CHANNEL;
570 nd->state = ncsi_dev_state_suspend_done;
571 ret = ncsi_xmit_cmd(&nca);
576 case ncsi_dev_state_suspend_done:
577 spin_lock_irqsave(&nc->lock, flags);
578 nc->state = NCSI_CHANNEL_INACTIVE;
579 spin_unlock_irqrestore(&nc->lock, flags);
580 if (ndp->flags & NCSI_DEV_RESET)
583 ncsi_process_next_channel(ndp);
586 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
592 nd->state = ncsi_dev_state_functional;
595 /* Check the VLAN filter bitmap for a set filter, and construct a
596 * "Set VLAN Filter - Disable" packet if found.
598 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
599 struct ncsi_cmd_arg *nca)
601 struct ncsi_channel_vlan_filter *ncf;
607 ncf = &nc->vlan_filter;
608 bitmap = &ncf->bitmap;
610 spin_lock_irqsave(&nc->lock, flags);
611 index = find_first_bit(bitmap, ncf->n_vids);
612 if (index >= ncf->n_vids) {
613 spin_unlock_irqrestore(&nc->lock, flags);
616 vid = ncf->vids[index];
618 clear_bit(index, bitmap);
619 ncf->vids[index] = 0;
620 spin_unlock_irqrestore(&nc->lock, flags);
622 nca->type = NCSI_PKT_CMD_SVF;
624 /* HW filter index starts at 1 */
625 nca->bytes[6] = index + 1;
626 nca->bytes[7] = 0x00;
630 /* Find an outstanding VLAN tag and construct a "Set VLAN Filter - Enable"
633 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
634 struct ncsi_cmd_arg *nca)
636 struct ncsi_channel_vlan_filter *ncf;
637 struct vlan_vid *vlan = NULL;
643 if (list_empty(&ndp->vlan_vids))
646 ncf = &nc->vlan_filter;
647 bitmap = &ncf->bitmap;
649 spin_lock_irqsave(&nc->lock, flags);
652 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
654 for (i = 0; i < ncf->n_vids; i++)
655 if (ncf->vids[i] == vid) {
665 /* No VLAN ID is not set */
666 spin_unlock_irqrestore(&nc->lock, flags);
670 index = find_first_zero_bit(bitmap, ncf->n_vids);
671 if (index < 0 || index >= ncf->n_vids) {
672 netdev_err(ndp->ndev.dev,
673 "Channel %u already has all VLAN filters set\n",
675 spin_unlock_irqrestore(&nc->lock, flags);
679 ncf->vids[index] = vid;
680 set_bit(index, bitmap);
681 spin_unlock_irqrestore(&nc->lock, flags);
683 nca->type = NCSI_PKT_CMD_SVF;
685 /* HW filter index starts at 1 */
686 nca->bytes[6] = index + 1;
687 nca->bytes[7] = 0x01;
692 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY)
694 static int ncsi_oem_keep_phy_intel(struct ncsi_cmd_arg *nca)
696 unsigned char data[NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN];
699 nca->payload = NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN;
701 memset(data, 0, NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN);
702 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
704 data[4] = NCSI_OEM_INTEL_CMD_KEEP_PHY;
706 /* PHY Link up attribute */
711 ret = ncsi_xmit_cmd(nca);
713 netdev_err(nca->ndp->ndev.dev,
714 "NCSI: Failed to transmit cmd 0x%x during configure\n",
721 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
723 /* NCSI OEM Command APIs */
724 static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
726 unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
729 nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
731 memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN);
732 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_BCM_ID);
733 data[5] = NCSI_OEM_BCM_CMD_GMA;
737 ret = ncsi_xmit_cmd(nca);
739 netdev_err(nca->ndp->ndev.dev,
740 "NCSI: Failed to transmit cmd 0x%x during configure\n",
745 static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg *nca)
748 u8 data_u8[NCSI_OEM_MLX_CMD_GMA_LEN];
749 u32 data_u32[NCSI_OEM_MLX_CMD_GMA_LEN / sizeof(u32)];
753 nca->payload = NCSI_OEM_MLX_CMD_GMA_LEN;
755 memset(&u, 0, sizeof(u));
756 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
757 u.data_u8[5] = NCSI_OEM_MLX_CMD_GMA;
758 u.data_u8[6] = NCSI_OEM_MLX_CMD_GMA_PARAM;
760 nca->data = u.data_u8;
762 ret = ncsi_xmit_cmd(nca);
764 netdev_err(nca->ndp->ndev.dev,
765 "NCSI: Failed to transmit cmd 0x%x during configure\n",
770 static int ncsi_oem_smaf_mlx(struct ncsi_cmd_arg *nca)
773 u8 data_u8[NCSI_OEM_MLX_CMD_SMAF_LEN];
774 u32 data_u32[NCSI_OEM_MLX_CMD_SMAF_LEN / sizeof(u32)];
778 memset(&u, 0, sizeof(u));
779 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
780 u.data_u8[5] = NCSI_OEM_MLX_CMD_SMAF;
781 u.data_u8[6] = NCSI_OEM_MLX_CMD_SMAF_PARAM;
782 memcpy(&u.data_u8[MLX_SMAF_MAC_ADDR_OFFSET],
783 nca->ndp->ndev.dev->dev_addr, ETH_ALEN);
784 u.data_u8[MLX_SMAF_MED_SUPPORT_OFFSET] =
785 (MLX_MC_RBT_AVL | MLX_MC_RBT_SUPPORT);
787 nca->payload = NCSI_OEM_MLX_CMD_SMAF_LEN;
788 nca->data = u.data_u8;
790 ret = ncsi_xmit_cmd(nca);
792 netdev_err(nca->ndp->ndev.dev,
793 "NCSI: Failed to transmit cmd 0x%x during probe\n",
798 static int ncsi_oem_gma_handler_intel(struct ncsi_cmd_arg *nca)
800 unsigned char data[NCSI_OEM_INTEL_CMD_GMA_LEN];
803 nca->payload = NCSI_OEM_INTEL_CMD_GMA_LEN;
805 memset(data, 0, NCSI_OEM_INTEL_CMD_GMA_LEN);
806 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
807 data[4] = NCSI_OEM_INTEL_CMD_GMA;
811 ret = ncsi_xmit_cmd(nca);
813 netdev_err(nca->ndp->ndev.dev,
814 "NCSI: Failed to transmit cmd 0x%x during configure\n",
820 /* OEM Command handlers initialization */
821 static struct ncsi_oem_gma_handler {
823 int (*handler)(struct ncsi_cmd_arg *nca);
824 } ncsi_oem_gma_handlers[] = {
825 { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm },
826 { NCSI_OEM_MFR_MLX_ID, ncsi_oem_gma_handler_mlx },
827 { NCSI_OEM_MFR_INTEL_ID, ncsi_oem_gma_handler_intel }
830 static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
832 struct ncsi_oem_gma_handler *nch = NULL;
835 /* This function should only be called once, return if flag set */
836 if (nca->ndp->gma_flag == 1)
839 /* Find gma handler for given manufacturer id */
840 for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) {
841 if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) {
842 if (ncsi_oem_gma_handlers[i].handler)
843 nch = &ncsi_oem_gma_handlers[i];
849 netdev_err(nca->ndp->ndev.dev,
850 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
855 /* Get Mac address from NCSI device */
856 return nch->handler(nca);
859 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
861 /* Determine if a given channel from the channel_queue should be used for Tx */
862 static bool ncsi_channel_is_tx(struct ncsi_dev_priv *ndp,
863 struct ncsi_channel *nc)
865 struct ncsi_channel_mode *ncm;
866 struct ncsi_channel *channel;
867 struct ncsi_package *np;
869 /* Check if any other channel has Tx enabled; a channel may have already
870 * been configured and removed from the channel queue.
872 NCSI_FOR_EACH_PACKAGE(ndp, np) {
873 if (!ndp->multi_package && np != nc->package)
875 NCSI_FOR_EACH_CHANNEL(np, channel) {
876 ncm = &channel->modes[NCSI_MODE_TX_ENABLE];
882 /* This channel is the preferred channel and has link */
883 list_for_each_entry_rcu(channel, &ndp->channel_queue, link) {
884 np = channel->package;
885 if (np->preferred_channel &&
886 ncsi_channel_has_link(np->preferred_channel)) {
887 return np->preferred_channel == nc;
891 /* This channel has link */
892 if (ncsi_channel_has_link(nc))
895 list_for_each_entry_rcu(channel, &ndp->channel_queue, link)
896 if (ncsi_channel_has_link(channel))
899 /* No other channel has link; default to this one */
903 /* Change the active Tx channel in a multi-channel setup */
904 int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp,
905 struct ncsi_package *package,
906 struct ncsi_channel *disable,
907 struct ncsi_channel *enable)
909 struct ncsi_cmd_arg nca;
910 struct ncsi_channel *nc;
911 struct ncsi_package *np;
914 if (!package->multi_channel && !ndp->multi_package)
915 netdev_warn(ndp->ndev.dev,
916 "NCSI: Trying to update Tx channel in single-channel mode\n");
920 /* Find current channel with Tx enabled */
921 NCSI_FOR_EACH_PACKAGE(ndp, np) {
924 if (!ndp->multi_package && np != package)
927 NCSI_FOR_EACH_CHANNEL(np, nc)
928 if (nc->modes[NCSI_MODE_TX_ENABLE].enable) {
934 /* Find a suitable channel for Tx */
935 NCSI_FOR_EACH_PACKAGE(ndp, np) {
938 if (!ndp->multi_package && np != package)
940 if (!(ndp->package_whitelist & (0x1 << np->id)))
943 if (np->preferred_channel &&
944 ncsi_channel_has_link(np->preferred_channel)) {
945 enable = np->preferred_channel;
949 NCSI_FOR_EACH_CHANNEL(np, nc) {
950 if (!(np->channel_whitelist & 0x1 << nc->id))
952 if (nc->state != NCSI_CHANNEL_ACTIVE)
954 if (ncsi_channel_has_link(nc)) {
961 if (disable == enable)
968 nca.channel = disable->id;
969 nca.package = disable->package->id;
970 nca.type = NCSI_PKT_CMD_DCNT;
971 ret = ncsi_xmit_cmd(&nca);
973 netdev_err(ndp->ndev.dev,
974 "Error %d sending DCNT\n",
978 netdev_info(ndp->ndev.dev, "NCSI: channel %u enables Tx\n", enable->id);
980 nca.channel = enable->id;
981 nca.package = enable->package->id;
982 nca.type = NCSI_PKT_CMD_ECNT;
983 ret = ncsi_xmit_cmd(&nca);
985 netdev_err(ndp->ndev.dev,
986 "Error %d sending ECNT\n",
992 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
994 struct ncsi_package *np = ndp->active_package;
995 struct ncsi_channel *nc = ndp->active_channel;
996 struct ncsi_channel *hot_nc = NULL;
997 struct ncsi_dev *nd = &ndp->ndev;
998 struct net_device *dev = nd->dev;
999 struct ncsi_cmd_arg nca;
1000 unsigned char index;
1001 unsigned long flags;
1005 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1006 switch (nd->state) {
1007 case ncsi_dev_state_config:
1008 case ncsi_dev_state_config_sp:
1009 ndp->pending_req_num = 1;
1011 /* Select the specific package */
1012 nca.type = NCSI_PKT_CMD_SP;
1013 if (ndp->flags & NCSI_DEV_HWA)
1017 nca.package = np->id;
1018 nca.channel = NCSI_RESERVED_CHANNEL;
1019 ret = ncsi_xmit_cmd(&nca);
1021 netdev_err(ndp->ndev.dev,
1022 "NCSI: Failed to transmit CMD_SP\n");
1026 nd->state = ncsi_dev_state_config_cis;
1028 case ncsi_dev_state_config_cis:
1029 ndp->pending_req_num = 1;
1031 /* Clear initial state */
1032 nca.type = NCSI_PKT_CMD_CIS;
1033 nca.package = np->id;
1034 nca.channel = nc->id;
1035 ret = ncsi_xmit_cmd(&nca);
1037 netdev_err(ndp->ndev.dev,
1038 "NCSI: Failed to transmit CMD_CIS\n");
1042 nd->state = ncsi_dev_state_config_oem_gma;
1044 case ncsi_dev_state_config_oem_gma:
1045 nd->state = ncsi_dev_state_config_clear_vids;
1048 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
1049 nca.type = NCSI_PKT_CMD_OEM;
1050 nca.package = np->id;
1051 nca.channel = nc->id;
1052 ndp->pending_req_num = 1;
1053 ret = ncsi_gma_handler(&nca, nc->version.mf_id);
1054 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
1057 schedule_work(&ndp->work);
1060 case ncsi_dev_state_config_clear_vids:
1061 case ncsi_dev_state_config_svf:
1062 case ncsi_dev_state_config_ev:
1063 case ncsi_dev_state_config_sma:
1064 case ncsi_dev_state_config_ebf:
1065 case ncsi_dev_state_config_dgmf:
1066 case ncsi_dev_state_config_ecnt:
1067 case ncsi_dev_state_config_ec:
1068 case ncsi_dev_state_config_ae:
1069 case ncsi_dev_state_config_gls:
1070 ndp->pending_req_num = 1;
1072 nca.package = np->id;
1073 nca.channel = nc->id;
1075 /* Clear any active filters on the channel before setting */
1076 if (nd->state == ncsi_dev_state_config_clear_vids) {
1077 ret = clear_one_vid(ndp, nc, &nca);
1079 nd->state = ncsi_dev_state_config_svf;
1080 schedule_work(&ndp->work);
1084 nd->state = ncsi_dev_state_config_clear_vids;
1085 /* Add known VLAN tags to the filter */
1086 } else if (nd->state == ncsi_dev_state_config_svf) {
1087 ret = set_one_vid(ndp, nc, &nca);
1089 nd->state = ncsi_dev_state_config_ev;
1090 schedule_work(&ndp->work);
1094 nd->state = ncsi_dev_state_config_svf;
1095 /* Enable/Disable the VLAN filter */
1096 } else if (nd->state == ncsi_dev_state_config_ev) {
1097 if (list_empty(&ndp->vlan_vids)) {
1098 nca.type = NCSI_PKT_CMD_DV;
1100 nca.type = NCSI_PKT_CMD_EV;
1101 nca.bytes[3] = NCSI_CAP_VLAN_NO;
1103 nd->state = ncsi_dev_state_config_sma;
1104 } else if (nd->state == ncsi_dev_state_config_sma) {
1105 /* Use first entry in unicast filter table. Note that
1106 * the MAC filter table starts from entry 1 instead of
1109 nca.type = NCSI_PKT_CMD_SMA;
1110 for (index = 0; index < 6; index++)
1111 nca.bytes[index] = dev->dev_addr[index];
1114 nd->state = ncsi_dev_state_config_ebf;
1115 } else if (nd->state == ncsi_dev_state_config_ebf) {
1116 nca.type = NCSI_PKT_CMD_EBF;
1117 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
1118 /* if multicast global filtering is supported then
1119 * disable it so that all multicast packet will be
1120 * forwarded to management controller
1122 if (nc->caps[NCSI_CAP_GENERIC].cap &
1123 NCSI_CAP_GENERIC_MC)
1124 nd->state = ncsi_dev_state_config_dgmf;
1125 else if (ncsi_channel_is_tx(ndp, nc))
1126 nd->state = ncsi_dev_state_config_ecnt;
1128 nd->state = ncsi_dev_state_config_ec;
1129 } else if (nd->state == ncsi_dev_state_config_dgmf) {
1130 nca.type = NCSI_PKT_CMD_DGMF;
1131 if (ncsi_channel_is_tx(ndp, nc))
1132 nd->state = ncsi_dev_state_config_ecnt;
1134 nd->state = ncsi_dev_state_config_ec;
1135 } else if (nd->state == ncsi_dev_state_config_ecnt) {
1136 if (np->preferred_channel &&
1137 nc != np->preferred_channel)
1138 netdev_info(ndp->ndev.dev,
1139 "NCSI: Tx failed over to channel %u\n",
1141 nca.type = NCSI_PKT_CMD_ECNT;
1142 nd->state = ncsi_dev_state_config_ec;
1143 } else if (nd->state == ncsi_dev_state_config_ec) {
1144 /* Enable AEN if it's supported */
1145 nca.type = NCSI_PKT_CMD_EC;
1146 nd->state = ncsi_dev_state_config_ae;
1147 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
1148 nd->state = ncsi_dev_state_config_gls;
1149 } else if (nd->state == ncsi_dev_state_config_ae) {
1150 nca.type = NCSI_PKT_CMD_AE;
1152 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
1153 nd->state = ncsi_dev_state_config_gls;
1154 } else if (nd->state == ncsi_dev_state_config_gls) {
1155 nca.type = NCSI_PKT_CMD_GLS;
1156 nd->state = ncsi_dev_state_config_done;
1159 ret = ncsi_xmit_cmd(&nca);
1161 netdev_err(ndp->ndev.dev,
1162 "NCSI: Failed to transmit CMD %x\n",
1167 case ncsi_dev_state_config_done:
1168 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
1170 spin_lock_irqsave(&nc->lock, flags);
1171 nc->state = NCSI_CHANNEL_ACTIVE;
1173 if (ndp->flags & NCSI_DEV_RESET) {
1174 /* A reset event happened during config, start it now */
1175 nc->reconfigure_needed = false;
1176 spin_unlock_irqrestore(&nc->lock, flags);
1181 if (nc->reconfigure_needed) {
1182 /* This channel's configuration has been updated
1183 * part-way during the config state - start the
1184 * channel configuration over
1186 nc->reconfigure_needed = false;
1187 nc->state = NCSI_CHANNEL_INACTIVE;
1188 spin_unlock_irqrestore(&nc->lock, flags);
1190 spin_lock_irqsave(&ndp->lock, flags);
1191 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1192 spin_unlock_irqrestore(&ndp->lock, flags);
1194 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
1195 ncsi_process_next_channel(ndp);
1199 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
1203 netdev_dbg(ndp->ndev.dev,
1204 "NCSI: channel %u link down after config\n",
1207 spin_unlock_irqrestore(&nc->lock, flags);
1209 /* Update the hot channel */
1210 spin_lock_irqsave(&ndp->lock, flags);
1211 ndp->hot_channel = hot_nc;
1212 spin_unlock_irqrestore(&ndp->lock, flags);
1214 ncsi_start_channel_monitor(nc);
1215 ncsi_process_next_channel(ndp);
1218 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
1225 ncsi_report_link(ndp, true);
1228 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
1230 struct ncsi_channel *nc, *found, *hot_nc;
1231 struct ncsi_channel_mode *ncm;
1232 unsigned long flags, cflags;
1233 struct ncsi_package *np;
1236 spin_lock_irqsave(&ndp->lock, flags);
1237 hot_nc = ndp->hot_channel;
1238 spin_unlock_irqrestore(&ndp->lock, flags);
1240 /* By default the search is done once an inactive channel with up
1241 * link is found, unless a preferred channel is set.
1242 * If multi_package or multi_channel are configured all channels in the
1243 * whitelist are added to the channel queue.
1247 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1248 if (!(ndp->package_whitelist & (0x1 << np->id)))
1250 NCSI_FOR_EACH_CHANNEL(np, nc) {
1251 if (!(np->channel_whitelist & (0x1 << nc->id)))
1254 spin_lock_irqsave(&nc->lock, cflags);
1256 if (!list_empty(&nc->link) ||
1257 nc->state != NCSI_CHANNEL_INACTIVE) {
1258 spin_unlock_irqrestore(&nc->lock, cflags);
1268 ncm = &nc->modes[NCSI_MODE_LINK];
1269 if (ncm->data[2] & 0x1) {
1274 /* If multi_channel is enabled configure all valid
1275 * channels whether or not they currently have link
1276 * so they will have AENs enabled.
1278 if (with_link || np->multi_channel) {
1279 spin_lock_irqsave(&ndp->lock, flags);
1280 list_add_tail_rcu(&nc->link,
1281 &ndp->channel_queue);
1282 spin_unlock_irqrestore(&ndp->lock, flags);
1284 netdev_dbg(ndp->ndev.dev,
1285 "NCSI: Channel %u added to queue (link %s)\n",
1287 ncm->data[2] & 0x1 ? "up" : "down");
1290 spin_unlock_irqrestore(&nc->lock, cflags);
1292 if (with_link && !np->multi_channel)
1295 if (with_link && !ndp->multi_package)
1299 if (list_empty(&ndp->channel_queue) && found) {
1300 netdev_info(ndp->ndev.dev,
1301 "NCSI: No channel with link found, configuring channel %u\n",
1303 spin_lock_irqsave(&ndp->lock, flags);
1304 list_add_tail_rcu(&found->link, &ndp->channel_queue);
1305 spin_unlock_irqrestore(&ndp->lock, flags);
1306 } else if (!found) {
1307 netdev_warn(ndp->ndev.dev,
1308 "NCSI: No channel found to configure!\n");
1309 ncsi_report_link(ndp, true);
1313 return ncsi_process_next_channel(ndp);
1316 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1318 struct ncsi_package *np;
1319 struct ncsi_channel *nc;
1321 bool has_channel = false;
1323 /* The hardware arbitration is disabled if any one channel
1324 * doesn't support explicitly.
1326 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1327 NCSI_FOR_EACH_CHANNEL(np, nc) {
1330 cap = nc->caps[NCSI_CAP_GENERIC].cap;
1331 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
1332 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
1333 NCSI_CAP_GENERIC_HWA_SUPPORT) {
1334 ndp->flags &= ~NCSI_DEV_HWA;
1341 ndp->flags |= NCSI_DEV_HWA;
1345 ndp->flags &= ~NCSI_DEV_HWA;
1349 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1351 struct ncsi_dev *nd = &ndp->ndev;
1352 struct ncsi_package *np;
1353 struct ncsi_channel *nc;
1354 struct ncsi_cmd_arg nca;
1355 unsigned char index;
1359 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1360 switch (nd->state) {
1361 case ncsi_dev_state_probe:
1362 nd->state = ncsi_dev_state_probe_deselect;
1364 case ncsi_dev_state_probe_deselect:
1365 ndp->pending_req_num = 8;
1367 /* Deselect all possible packages */
1368 nca.type = NCSI_PKT_CMD_DP;
1369 nca.channel = NCSI_RESERVED_CHANNEL;
1370 for (index = 0; index < 8; index++) {
1371 nca.package = index;
1372 ret = ncsi_xmit_cmd(&nca);
1377 nd->state = ncsi_dev_state_probe_package;
1379 case ncsi_dev_state_probe_package:
1380 ndp->pending_req_num = 1;
1382 nca.type = NCSI_PKT_CMD_SP;
1384 nca.package = ndp->package_probe_id;
1385 nca.channel = NCSI_RESERVED_CHANNEL;
1386 ret = ncsi_xmit_cmd(&nca);
1389 nd->state = ncsi_dev_state_probe_channel;
1391 case ncsi_dev_state_probe_channel:
1392 ndp->active_package = ncsi_find_package(ndp,
1393 ndp->package_probe_id);
1394 if (!ndp->active_package) {
1396 nd->state = ncsi_dev_state_probe_dp;
1397 schedule_work(&ndp->work);
1400 nd->state = ncsi_dev_state_probe_cis;
1401 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC) &&
1402 ndp->mlx_multi_host)
1403 nd->state = ncsi_dev_state_probe_mlx_gma;
1405 schedule_work(&ndp->work);
1407 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
1408 case ncsi_dev_state_probe_mlx_gma:
1409 ndp->pending_req_num = 1;
1411 nca.type = NCSI_PKT_CMD_OEM;
1412 nca.package = ndp->active_package->id;
1414 ret = ncsi_oem_gma_handler_mlx(&nca);
1418 nd->state = ncsi_dev_state_probe_mlx_smaf;
1420 case ncsi_dev_state_probe_mlx_smaf:
1421 ndp->pending_req_num = 1;
1423 nca.type = NCSI_PKT_CMD_OEM;
1424 nca.package = ndp->active_package->id;
1426 ret = ncsi_oem_smaf_mlx(&nca);
1430 nd->state = ncsi_dev_state_probe_cis;
1432 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
1433 case ncsi_dev_state_probe_cis:
1434 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1436 /* Clear initial state */
1437 nca.type = NCSI_PKT_CMD_CIS;
1438 nca.package = ndp->active_package->id;
1439 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1440 nca.channel = index;
1441 ret = ncsi_xmit_cmd(&nca);
1446 nd->state = ncsi_dev_state_probe_gvi;
1447 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY))
1448 nd->state = ncsi_dev_state_probe_keep_phy;
1450 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY)
1451 case ncsi_dev_state_probe_keep_phy:
1452 ndp->pending_req_num = 1;
1454 nca.type = NCSI_PKT_CMD_OEM;
1455 nca.package = ndp->active_package->id;
1457 ret = ncsi_oem_keep_phy_intel(&nca);
1461 nd->state = ncsi_dev_state_probe_gvi;
1463 #endif /* CONFIG_NCSI_OEM_CMD_KEEP_PHY */
1464 case ncsi_dev_state_probe_gvi:
1465 case ncsi_dev_state_probe_gc:
1466 case ncsi_dev_state_probe_gls:
1467 np = ndp->active_package;
1468 ndp->pending_req_num = np->channel_num;
1470 /* Retrieve version, capability or link status */
1471 if (nd->state == ncsi_dev_state_probe_gvi)
1472 nca.type = NCSI_PKT_CMD_GVI;
1473 else if (nd->state == ncsi_dev_state_probe_gc)
1474 nca.type = NCSI_PKT_CMD_GC;
1476 nca.type = NCSI_PKT_CMD_GLS;
1478 nca.package = np->id;
1479 NCSI_FOR_EACH_CHANNEL(np, nc) {
1480 nca.channel = nc->id;
1481 ret = ncsi_xmit_cmd(&nca);
1486 if (nd->state == ncsi_dev_state_probe_gvi)
1487 nd->state = ncsi_dev_state_probe_gc;
1488 else if (nd->state == ncsi_dev_state_probe_gc)
1489 nd->state = ncsi_dev_state_probe_gls;
1491 nd->state = ncsi_dev_state_probe_dp;
1493 case ncsi_dev_state_probe_dp:
1494 ndp->pending_req_num = 1;
1496 /* Deselect the current package */
1497 nca.type = NCSI_PKT_CMD_DP;
1498 nca.package = ndp->package_probe_id;
1499 nca.channel = NCSI_RESERVED_CHANNEL;
1500 ret = ncsi_xmit_cmd(&nca);
1504 /* Probe next package */
1505 ndp->package_probe_id++;
1506 if (ndp->package_probe_id >= 8) {
1507 /* Probe finished */
1508 ndp->flags |= NCSI_DEV_PROBED;
1511 nd->state = ncsi_dev_state_probe_package;
1512 ndp->active_package = NULL;
1515 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1519 if (ndp->flags & NCSI_DEV_PROBED) {
1520 /* Check if all packages have HWA support */
1521 ncsi_check_hwa(ndp);
1522 ncsi_choose_active_channel(ndp);
1527 netdev_err(ndp->ndev.dev,
1528 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1530 ncsi_report_link(ndp, true);
1533 static void ncsi_dev_work(struct work_struct *work)
1535 struct ncsi_dev_priv *ndp = container_of(work,
1536 struct ncsi_dev_priv, work);
1537 struct ncsi_dev *nd = &ndp->ndev;
1539 switch (nd->state & ncsi_dev_state_major) {
1540 case ncsi_dev_state_probe:
1541 ncsi_probe_channel(ndp);
1543 case ncsi_dev_state_suspend:
1544 ncsi_suspend_channel(ndp);
1546 case ncsi_dev_state_config:
1547 ncsi_configure_channel(ndp);
1550 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1555 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1557 struct ncsi_channel *nc;
1559 unsigned long flags;
1561 spin_lock_irqsave(&ndp->lock, flags);
1562 nc = list_first_or_null_rcu(&ndp->channel_queue,
1563 struct ncsi_channel, link);
1565 spin_unlock_irqrestore(&ndp->lock, flags);
1569 list_del_init(&nc->link);
1570 spin_unlock_irqrestore(&ndp->lock, flags);
1572 spin_lock_irqsave(&nc->lock, flags);
1573 old_state = nc->state;
1574 nc->state = NCSI_CHANNEL_INVISIBLE;
1575 spin_unlock_irqrestore(&nc->lock, flags);
1577 ndp->active_channel = nc;
1578 ndp->active_package = nc->package;
1580 switch (old_state) {
1581 case NCSI_CHANNEL_INACTIVE:
1582 ndp->ndev.state = ncsi_dev_state_config;
1583 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1585 ncsi_configure_channel(ndp);
1587 case NCSI_CHANNEL_ACTIVE:
1588 ndp->ndev.state = ncsi_dev_state_suspend;
1589 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1591 ncsi_suspend_channel(ndp);
1594 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1595 old_state, nc->package->id, nc->id);
1596 ncsi_report_link(ndp, false);
1603 ndp->active_channel = NULL;
1604 ndp->active_package = NULL;
1605 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1606 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1607 return ncsi_choose_active_channel(ndp);
1610 ncsi_report_link(ndp, false);
1614 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1616 struct ncsi_dev *nd = &ndp->ndev;
1617 struct ncsi_channel *nc;
1618 struct ncsi_package *np;
1619 unsigned long flags;
1622 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1623 NCSI_FOR_EACH_CHANNEL(np, nc) {
1624 spin_lock_irqsave(&nc->lock, flags);
1626 /* Channels may be busy, mark dirty instead of
1628 * a) not ACTIVE (configured)
1629 * b) in the channel_queue (to be configured)
1630 * c) it's ndev is in the config state
1632 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1633 if ((ndp->ndev.state & 0xff00) ==
1634 ncsi_dev_state_config ||
1635 !list_empty(&nc->link)) {
1637 "NCSI: channel %p marked dirty\n",
1639 nc->reconfigure_needed = true;
1641 spin_unlock_irqrestore(&nc->lock, flags);
1645 spin_unlock_irqrestore(&nc->lock, flags);
1647 ncsi_stop_channel_monitor(nc);
1648 spin_lock_irqsave(&nc->lock, flags);
1649 nc->state = NCSI_CHANNEL_INACTIVE;
1650 spin_unlock_irqrestore(&nc->lock, flags);
1652 spin_lock_irqsave(&ndp->lock, flags);
1653 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1654 spin_unlock_irqrestore(&ndp->lock, flags);
1656 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
1664 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1666 struct ncsi_dev_priv *ndp;
1667 unsigned int n_vids = 0;
1668 struct vlan_vid *vlan;
1669 struct ncsi_dev *nd;
1675 nd = ncsi_find_dev(dev);
1677 netdev_warn(dev, "NCSI: No net_device?\n");
1681 ndp = TO_NCSI_DEV_PRIV(nd);
1683 /* Add the VLAN id to our internal list */
1684 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1686 if (vlan->vid == vid) {
1687 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1692 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1694 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1695 vid, NCSI_MAX_VLAN_VIDS);
1699 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1703 vlan->proto = proto;
1705 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1707 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
1709 found = ncsi_kick_channels(ndp) != 0;
1711 return found ? ncsi_process_next_channel(ndp) : 0;
1713 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1715 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1717 struct vlan_vid *vlan, *tmp;
1718 struct ncsi_dev_priv *ndp;
1719 struct ncsi_dev *nd;
1725 nd = ncsi_find_dev(dev);
1727 netdev_warn(dev, "NCSI: no net_device?\n");
1731 ndp = TO_NCSI_DEV_PRIV(nd);
1733 /* Remove the VLAN id from our internal list */
1734 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1735 if (vlan->vid == vid) {
1736 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
1737 list_del_rcu(&vlan->list);
1743 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1747 found = ncsi_kick_channels(ndp) != 0;
1749 return found ? ncsi_process_next_channel(ndp) : 0;
1751 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1753 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1754 void (*handler)(struct ncsi_dev *ndev))
1756 struct ncsi_dev_priv *ndp;
1757 struct ncsi_dev *nd;
1758 struct platform_device *pdev;
1759 struct device_node *np;
1760 unsigned long flags;
1763 /* Check if the device has been registered or not */
1764 nd = ncsi_find_dev(dev);
1768 /* Create NCSI device */
1769 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1774 nd->state = ncsi_dev_state_registered;
1776 nd->handler = handler;
1777 ndp->pending_req_num = 0;
1778 INIT_LIST_HEAD(&ndp->channel_queue);
1779 INIT_LIST_HEAD(&ndp->vlan_vids);
1780 INIT_WORK(&ndp->work, ncsi_dev_work);
1781 ndp->package_whitelist = UINT_MAX;
1783 /* Initialize private NCSI device */
1784 spin_lock_init(&ndp->lock);
1785 INIT_LIST_HEAD(&ndp->packages);
1786 ndp->request_id = NCSI_REQ_START_IDX;
1787 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1788 ndp->requests[i].id = i;
1789 ndp->requests[i].ndp = ndp;
1790 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
1793 spin_lock_irqsave(&ncsi_dev_lock, flags);
1794 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1795 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1797 /* Register NCSI packet Rx handler */
1798 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1799 ndp->ptype.func = ncsi_rcv_rsp;
1800 ndp->ptype.dev = dev;
1801 dev_add_pack(&ndp->ptype);
1803 pdev = to_platform_device(dev->dev.parent);
1805 np = pdev->dev.of_node;
1806 if (np && (of_property_read_bool(np, "mellanox,multi-host") ||
1807 of_property_read_bool(np, "mlx,multi-host")))
1808 ndp->mlx_multi_host = true;
1813 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1815 int ncsi_start_dev(struct ncsi_dev *nd)
1817 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1819 if (nd->state != ncsi_dev_state_registered &&
1820 nd->state != ncsi_dev_state_functional)
1823 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1824 ndp->package_probe_id = 0;
1825 nd->state = ncsi_dev_state_probe;
1826 schedule_work(&ndp->work);
1830 return ncsi_reset_dev(nd);
1832 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1834 void ncsi_stop_dev(struct ncsi_dev *nd)
1836 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1837 struct ncsi_package *np;
1838 struct ncsi_channel *nc;
1841 unsigned long flags;
1843 /* Stop the channel monitor on any active channels. Don't reset the
1844 * channel state so we know which were active when ncsi_start_dev()
1847 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1848 NCSI_FOR_EACH_CHANNEL(np, nc) {
1849 ncsi_stop_channel_monitor(nc);
1851 spin_lock_irqsave(&nc->lock, flags);
1852 chained = !list_empty(&nc->link);
1853 old_state = nc->state;
1854 spin_unlock_irqrestore(&nc->lock, flags);
1856 WARN_ON_ONCE(chained ||
1857 old_state == NCSI_CHANNEL_INVISIBLE);
1861 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
1862 ncsi_report_link(ndp, true);
1864 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1866 int ncsi_reset_dev(struct ncsi_dev *nd)
1868 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1869 struct ncsi_channel *nc, *active, *tmp;
1870 struct ncsi_package *np;
1871 unsigned long flags;
1873 spin_lock_irqsave(&ndp->lock, flags);
1875 if (!(ndp->flags & NCSI_DEV_RESET)) {
1876 /* Haven't been called yet, check states */
1877 switch (nd->state & ncsi_dev_state_major) {
1878 case ncsi_dev_state_registered:
1879 case ncsi_dev_state_probe:
1880 /* Not even probed yet - do nothing */
1881 spin_unlock_irqrestore(&ndp->lock, flags);
1883 case ncsi_dev_state_suspend:
1884 case ncsi_dev_state_config:
1885 /* Wait for the channel to finish its suspend/config
1886 * operation; once it finishes it will check for
1887 * NCSI_DEV_RESET and reset the state.
1889 ndp->flags |= NCSI_DEV_RESET;
1890 spin_unlock_irqrestore(&ndp->lock, flags);
1894 switch (nd->state) {
1895 case ncsi_dev_state_suspend_done:
1896 case ncsi_dev_state_config_done:
1897 case ncsi_dev_state_functional:
1901 /* Current reset operation happening */
1902 spin_unlock_irqrestore(&ndp->lock, flags);
1907 if (!list_empty(&ndp->channel_queue)) {
1908 /* Clear any channel queue we may have interrupted */
1909 list_for_each_entry_safe(nc, tmp, &ndp->channel_queue, link)
1910 list_del_init(&nc->link);
1912 spin_unlock_irqrestore(&ndp->lock, flags);
1915 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1916 NCSI_FOR_EACH_CHANNEL(np, nc) {
1917 spin_lock_irqsave(&nc->lock, flags);
1919 if (nc->state == NCSI_CHANNEL_ACTIVE) {
1921 nc->state = NCSI_CHANNEL_INVISIBLE;
1922 spin_unlock_irqrestore(&nc->lock, flags);
1923 ncsi_stop_channel_monitor(nc);
1927 spin_unlock_irqrestore(&nc->lock, flags);
1935 spin_lock_irqsave(&ndp->lock, flags);
1936 ndp->flags &= ~NCSI_DEV_RESET;
1937 spin_unlock_irqrestore(&ndp->lock, flags);
1938 return ncsi_choose_active_channel(ndp);
1941 spin_lock_irqsave(&ndp->lock, flags);
1942 ndp->flags |= NCSI_DEV_RESET;
1943 ndp->active_channel = active;
1944 ndp->active_package = active->package;
1945 spin_unlock_irqrestore(&ndp->lock, flags);
1947 nd->state = ncsi_dev_state_suspend;
1948 schedule_work(&ndp->work);
1952 void ncsi_unregister_dev(struct ncsi_dev *nd)
1954 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1955 struct ncsi_package *np, *tmp;
1956 unsigned long flags;
1958 dev_remove_pack(&ndp->ptype);
1960 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1961 ncsi_remove_package(np);
1963 spin_lock_irqsave(&ncsi_dev_lock, flags);
1964 list_del_rcu(&ndp->node);
1965 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1969 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);