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/skbuff.h>
17 #include <net/net_namespace.h>
19 #include <net/addrconf.h>
21 #include <net/if_inet6.h>
25 #include "ncsi-netlink.h"
27 LIST_HEAD(ncsi_dev_list);
28 DEFINE_SPINLOCK(ncsi_dev_lock);
30 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
32 struct ncsi_dev *nd = &ndp->ndev;
33 struct ncsi_package *np;
34 struct ncsi_channel *nc;
37 nd->state = ncsi_dev_state_functional;
44 NCSI_FOR_EACH_PACKAGE(ndp, np) {
45 NCSI_FOR_EACH_CHANNEL(np, nc) {
46 spin_lock_irqsave(&nc->lock, flags);
48 if (!list_empty(&nc->link) ||
49 nc->state != NCSI_CHANNEL_ACTIVE) {
50 spin_unlock_irqrestore(&nc->lock, flags);
54 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
55 spin_unlock_irqrestore(&nc->lock, flags);
60 spin_unlock_irqrestore(&nc->lock, flags);
68 static void ncsi_channel_monitor(struct timer_list *t)
70 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
71 struct ncsi_package *np = nc->package;
72 struct ncsi_dev_priv *ndp = np->ndp;
73 struct ncsi_channel_mode *ncm;
74 struct ncsi_cmd_arg nca;
75 bool enabled, chained;
76 unsigned int monitor_state;
80 spin_lock_irqsave(&nc->lock, flags);
82 chained = !list_empty(&nc->link);
83 enabled = nc->monitor.enabled;
84 monitor_state = nc->monitor.state;
85 spin_unlock_irqrestore(&nc->lock, flags);
88 return; /* expected race disabling timer */
89 if (WARN_ON_ONCE(chained))
92 if (state != NCSI_CHANNEL_INACTIVE &&
93 state != NCSI_CHANNEL_ACTIVE) {
95 netdev_warn(ndp->ndev.dev,
96 "Bad NCSI monitor state channel %d 0x%x %s queue\n",
97 nc->id, state, chained ? "on" : "off");
98 spin_lock_irqsave(&nc->lock, flags);
99 nc->monitor.enabled = false;
100 spin_unlock_irqrestore(&nc->lock, flags);
104 switch (monitor_state) {
105 case NCSI_CHANNEL_MONITOR_START:
106 case NCSI_CHANNEL_MONITOR_RETRY:
108 nca.package = np->id;
109 nca.channel = nc->id;
110 nca.type = NCSI_PKT_CMD_GLS;
112 ret = ncsi_xmit_cmd(&nca);
114 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
117 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
120 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
122 if (!(ndp->flags & NCSI_DEV_HWA)) {
123 ncsi_report_link(ndp, true);
124 ndp->flags |= NCSI_DEV_RESHUFFLE;
127 ncm = &nc->modes[NCSI_MODE_LINK];
128 spin_lock_irqsave(&nc->lock, flags);
129 nc->monitor.enabled = false;
130 nc->state = NCSI_CHANNEL_INVISIBLE;
131 ncm->data[2] &= ~0x1;
132 spin_unlock_irqrestore(&nc->lock, flags);
134 spin_lock_irqsave(&ndp->lock, flags);
135 nc->state = NCSI_CHANNEL_ACTIVE;
136 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
137 spin_unlock_irqrestore(&ndp->lock, flags);
138 ncsi_process_next_channel(ndp);
142 spin_lock_irqsave(&nc->lock, flags);
144 spin_unlock_irqrestore(&nc->lock, flags);
145 mod_timer(&nc->monitor.timer, jiffies + HZ);
148 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
152 spin_lock_irqsave(&nc->lock, flags);
153 WARN_ON_ONCE(nc->monitor.enabled);
154 nc->monitor.enabled = true;
155 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
156 spin_unlock_irqrestore(&nc->lock, flags);
158 mod_timer(&nc->monitor.timer, jiffies + HZ);
161 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
165 spin_lock_irqsave(&nc->lock, flags);
166 if (!nc->monitor.enabled) {
167 spin_unlock_irqrestore(&nc->lock, flags);
170 nc->monitor.enabled = false;
171 spin_unlock_irqrestore(&nc->lock, flags);
173 del_timer_sync(&nc->monitor.timer);
176 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
179 struct ncsi_channel *nc;
181 NCSI_FOR_EACH_CHANNEL(np, nc) {
189 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
191 struct ncsi_channel *nc, *tmp;
195 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
201 nc->state = NCSI_CHANNEL_INACTIVE;
202 nc->monitor.enabled = false;
203 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
204 spin_lock_init(&nc->lock);
205 INIT_LIST_HEAD(&nc->link);
206 for (index = 0; index < NCSI_CAP_MAX; index++)
207 nc->caps[index].index = index;
208 for (index = 0; index < NCSI_MODE_MAX; index++)
209 nc->modes[index].index = index;
211 spin_lock_irqsave(&np->lock, flags);
212 tmp = ncsi_find_channel(np, id);
214 spin_unlock_irqrestore(&np->lock, flags);
219 list_add_tail_rcu(&nc->node, &np->channels);
221 spin_unlock_irqrestore(&np->lock, flags);
226 static void ncsi_remove_channel(struct ncsi_channel *nc)
228 struct ncsi_package *np = nc->package;
231 spin_lock_irqsave(&nc->lock, flags);
233 /* Release filters */
234 kfree(nc->mac_filter.addrs);
235 kfree(nc->vlan_filter.vids);
237 nc->state = NCSI_CHANNEL_INACTIVE;
238 spin_unlock_irqrestore(&nc->lock, flags);
239 ncsi_stop_channel_monitor(nc);
241 /* Remove and free channel */
242 spin_lock_irqsave(&np->lock, flags);
243 list_del_rcu(&nc->node);
245 spin_unlock_irqrestore(&np->lock, flags);
250 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
253 struct ncsi_package *np;
255 NCSI_FOR_EACH_PACKAGE(ndp, np) {
263 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
266 struct ncsi_package *np, *tmp;
269 np = kzalloc(sizeof(*np), GFP_ATOMIC);
275 spin_lock_init(&np->lock);
276 INIT_LIST_HEAD(&np->channels);
278 spin_lock_irqsave(&ndp->lock, flags);
279 tmp = ncsi_find_package(ndp, id);
281 spin_unlock_irqrestore(&ndp->lock, flags);
286 list_add_tail_rcu(&np->node, &ndp->packages);
288 spin_unlock_irqrestore(&ndp->lock, flags);
293 void ncsi_remove_package(struct ncsi_package *np)
295 struct ncsi_dev_priv *ndp = np->ndp;
296 struct ncsi_channel *nc, *tmp;
299 /* Release all child channels */
300 list_for_each_entry_safe(nc, tmp, &np->channels, node)
301 ncsi_remove_channel(nc);
303 /* Remove and free package */
304 spin_lock_irqsave(&ndp->lock, flags);
305 list_del_rcu(&np->node);
307 spin_unlock_irqrestore(&ndp->lock, flags);
312 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
314 struct ncsi_package **np,
315 struct ncsi_channel **nc)
317 struct ncsi_package *p;
318 struct ncsi_channel *c;
320 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
321 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
329 /* For two consecutive NCSI commands, the packet IDs shouldn't
330 * be same. Otherwise, the bogus response might be replied. So
331 * the available IDs are allocated in round-robin fashion.
333 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
334 unsigned int req_flags)
336 struct ncsi_request *nr = NULL;
337 int i, limit = ARRAY_SIZE(ndp->requests);
340 /* Check if there is one available request until the ceiling */
341 spin_lock_irqsave(&ndp->lock, flags);
342 for (i = ndp->request_id; i < limit; i++) {
343 if (ndp->requests[i].used)
346 nr = &ndp->requests[i];
348 nr->flags = req_flags;
349 ndp->request_id = i + 1;
353 /* Fail back to check from the starting cursor */
354 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
355 if (ndp->requests[i].used)
358 nr = &ndp->requests[i];
360 nr->flags = req_flags;
361 ndp->request_id = i + 1;
366 spin_unlock_irqrestore(&ndp->lock, flags);
370 void ncsi_free_request(struct ncsi_request *nr)
372 struct ncsi_dev_priv *ndp = nr->ndp;
373 struct sk_buff *cmd, *rsp;
379 del_timer_sync(&nr->timer);
382 spin_lock_irqsave(&ndp->lock, flags);
388 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
389 spin_unlock_irqrestore(&ndp->lock, flags);
391 if (driven && cmd && --ndp->pending_req_num == 0)
392 schedule_work(&ndp->work);
394 /* Release command and response */
399 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
401 struct ncsi_dev_priv *ndp;
403 NCSI_FOR_EACH_DEV(ndp) {
404 if (ndp->ndev.dev == dev)
411 static void ncsi_request_timeout(struct timer_list *t)
413 struct ncsi_request *nr = from_timer(nr, t, timer);
414 struct ncsi_dev_priv *ndp = nr->ndp;
417 /* If the request already had associated response,
418 * let the response handler to release it.
420 spin_lock_irqsave(&ndp->lock, flags);
422 if (nr->rsp || !nr->cmd) {
423 spin_unlock_irqrestore(&ndp->lock, flags);
426 spin_unlock_irqrestore(&ndp->lock, flags);
428 /* Release the request */
429 ncsi_free_request(nr);
432 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
434 struct ncsi_dev *nd = &ndp->ndev;
435 struct ncsi_package *np = ndp->active_package;
436 struct ncsi_channel *nc = ndp->active_channel;
437 struct ncsi_cmd_arg nca;
442 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
444 case ncsi_dev_state_suspend:
445 nd->state = ncsi_dev_state_suspend_select;
447 case ncsi_dev_state_suspend_select:
448 ndp->pending_req_num = 1;
450 nca.type = NCSI_PKT_CMD_SP;
451 nca.package = np->id;
452 nca.channel = NCSI_RESERVED_CHANNEL;
453 if (ndp->flags & NCSI_DEV_HWA)
458 /* To retrieve the last link states of channels in current
459 * package when current active channel needs fail over to
460 * another one. It means we will possibly select another
461 * channel as next active one. The link states of channels
462 * are most important factor of the selection. So we need
463 * accurate link states. Unfortunately, the link states on
464 * inactive channels can't be updated with LSC AEN in time.
466 if (ndp->flags & NCSI_DEV_RESHUFFLE)
467 nd->state = ncsi_dev_state_suspend_gls;
469 nd->state = ncsi_dev_state_suspend_dcnt;
470 ret = ncsi_xmit_cmd(&nca);
475 case ncsi_dev_state_suspend_gls:
476 ndp->pending_req_num = np->channel_num;
478 nca.type = NCSI_PKT_CMD_GLS;
479 nca.package = np->id;
481 nd->state = ncsi_dev_state_suspend_dcnt;
482 NCSI_FOR_EACH_CHANNEL(np, nc) {
483 nca.channel = nc->id;
484 ret = ncsi_xmit_cmd(&nca);
490 case ncsi_dev_state_suspend_dcnt:
491 ndp->pending_req_num = 1;
493 nca.type = NCSI_PKT_CMD_DCNT;
494 nca.package = np->id;
495 nca.channel = nc->id;
497 nd->state = ncsi_dev_state_suspend_dc;
498 ret = ncsi_xmit_cmd(&nca);
503 case ncsi_dev_state_suspend_dc:
504 ndp->pending_req_num = 1;
506 nca.type = NCSI_PKT_CMD_DC;
507 nca.package = np->id;
508 nca.channel = nc->id;
511 nd->state = ncsi_dev_state_suspend_deselect;
512 ret = ncsi_xmit_cmd(&nca);
517 case ncsi_dev_state_suspend_deselect:
518 ndp->pending_req_num = 1;
520 nca.type = NCSI_PKT_CMD_DP;
521 nca.package = np->id;
522 nca.channel = NCSI_RESERVED_CHANNEL;
524 nd->state = ncsi_dev_state_suspend_done;
525 ret = ncsi_xmit_cmd(&nca);
530 case ncsi_dev_state_suspend_done:
531 spin_lock_irqsave(&nc->lock, flags);
532 nc->state = NCSI_CHANNEL_INACTIVE;
533 spin_unlock_irqrestore(&nc->lock, flags);
534 ncsi_process_next_channel(ndp);
538 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
544 nd->state = ncsi_dev_state_functional;
547 /* Check the VLAN filter bitmap for a set filter, and construct a
548 * "Set VLAN Filter - Disable" packet if found.
550 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
551 struct ncsi_cmd_arg *nca)
553 struct ncsi_channel_vlan_filter *ncf;
559 ncf = &nc->vlan_filter;
560 bitmap = &ncf->bitmap;
562 spin_lock_irqsave(&nc->lock, flags);
563 index = find_next_bit(bitmap, ncf->n_vids, 0);
564 if (index >= ncf->n_vids) {
565 spin_unlock_irqrestore(&nc->lock, flags);
568 vid = ncf->vids[index];
570 clear_bit(index, bitmap);
571 ncf->vids[index] = 0;
572 spin_unlock_irqrestore(&nc->lock, flags);
574 nca->type = NCSI_PKT_CMD_SVF;
576 /* HW filter index starts at 1 */
577 nca->bytes[6] = index + 1;
578 nca->bytes[7] = 0x00;
582 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
585 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
586 struct ncsi_cmd_arg *nca)
588 struct ncsi_channel_vlan_filter *ncf;
589 struct vlan_vid *vlan = NULL;
595 if (list_empty(&ndp->vlan_vids))
598 ncf = &nc->vlan_filter;
599 bitmap = &ncf->bitmap;
601 spin_lock_irqsave(&nc->lock, flags);
604 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
606 for (i = 0; i < ncf->n_vids; i++)
607 if (ncf->vids[i] == vid) {
617 /* No VLAN ID is not set */
618 spin_unlock_irqrestore(&nc->lock, flags);
622 index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
623 if (index < 0 || index >= ncf->n_vids) {
624 netdev_err(ndp->ndev.dev,
625 "Channel %u already has all VLAN filters set\n",
627 spin_unlock_irqrestore(&nc->lock, flags);
631 ncf->vids[index] = vid;
632 set_bit(index, bitmap);
633 spin_unlock_irqrestore(&nc->lock, flags);
635 nca->type = NCSI_PKT_CMD_SVF;
637 /* HW filter index starts at 1 */
638 nca->bytes[6] = index + 1;
639 nca->bytes[7] = 0x01;
644 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
646 struct ncsi_dev *nd = &ndp->ndev;
647 struct net_device *dev = nd->dev;
648 struct ncsi_package *np = ndp->active_package;
649 struct ncsi_channel *nc = ndp->active_channel;
650 struct ncsi_channel *hot_nc = NULL;
651 struct ncsi_cmd_arg nca;
657 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
659 case ncsi_dev_state_config:
660 case ncsi_dev_state_config_sp:
661 ndp->pending_req_num = 1;
663 /* Select the specific package */
664 nca.type = NCSI_PKT_CMD_SP;
665 if (ndp->flags & NCSI_DEV_HWA)
669 nca.package = np->id;
670 nca.channel = NCSI_RESERVED_CHANNEL;
671 ret = ncsi_xmit_cmd(&nca);
673 netdev_err(ndp->ndev.dev,
674 "NCSI: Failed to transmit CMD_SP\n");
678 nd->state = ncsi_dev_state_config_cis;
680 case ncsi_dev_state_config_cis:
681 ndp->pending_req_num = 1;
683 /* Clear initial state */
684 nca.type = NCSI_PKT_CMD_CIS;
685 nca.package = np->id;
686 nca.channel = nc->id;
687 ret = ncsi_xmit_cmd(&nca);
689 netdev_err(ndp->ndev.dev,
690 "NCSI: Failed to transmit CMD_CIS\n");
694 nd->state = ncsi_dev_state_config_clear_vids;
696 case ncsi_dev_state_config_clear_vids:
697 case ncsi_dev_state_config_svf:
698 case ncsi_dev_state_config_ev:
699 case ncsi_dev_state_config_sma:
700 case ncsi_dev_state_config_ebf:
701 #if IS_ENABLED(CONFIG_IPV6)
702 case ncsi_dev_state_config_egmf:
704 case ncsi_dev_state_config_ecnt:
705 case ncsi_dev_state_config_ec:
706 case ncsi_dev_state_config_ae:
707 case ncsi_dev_state_config_gls:
708 ndp->pending_req_num = 1;
710 nca.package = np->id;
711 nca.channel = nc->id;
713 /* Clear any active filters on the channel before setting */
714 if (nd->state == ncsi_dev_state_config_clear_vids) {
715 ret = clear_one_vid(ndp, nc, &nca);
717 nd->state = ncsi_dev_state_config_svf;
718 schedule_work(&ndp->work);
722 nd->state = ncsi_dev_state_config_clear_vids;
723 /* Add known VLAN tags to the filter */
724 } else if (nd->state == ncsi_dev_state_config_svf) {
725 ret = set_one_vid(ndp, nc, &nca);
727 nd->state = ncsi_dev_state_config_ev;
728 schedule_work(&ndp->work);
732 nd->state = ncsi_dev_state_config_svf;
733 /* Enable/Disable the VLAN filter */
734 } else if (nd->state == ncsi_dev_state_config_ev) {
735 if (list_empty(&ndp->vlan_vids)) {
736 nca.type = NCSI_PKT_CMD_DV;
738 nca.type = NCSI_PKT_CMD_EV;
739 nca.bytes[3] = NCSI_CAP_VLAN_NO;
741 nd->state = ncsi_dev_state_config_sma;
742 } else if (nd->state == ncsi_dev_state_config_sma) {
743 /* Use first entry in unicast filter table. Note that
744 * the MAC filter table starts from entry 1 instead of
747 nca.type = NCSI_PKT_CMD_SMA;
748 for (index = 0; index < 6; index++)
749 nca.bytes[index] = dev->dev_addr[index];
752 nd->state = ncsi_dev_state_config_ebf;
753 } else if (nd->state == ncsi_dev_state_config_ebf) {
754 nca.type = NCSI_PKT_CMD_EBF;
755 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
756 nd->state = ncsi_dev_state_config_ecnt;
757 #if IS_ENABLED(CONFIG_IPV6)
758 if (ndp->inet6_addr_num > 0 &&
759 (nc->caps[NCSI_CAP_GENERIC].cap &
760 NCSI_CAP_GENERIC_MC))
761 nd->state = ncsi_dev_state_config_egmf;
763 nd->state = ncsi_dev_state_config_ecnt;
764 } else if (nd->state == ncsi_dev_state_config_egmf) {
765 nca.type = NCSI_PKT_CMD_EGMF;
766 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
767 nd->state = ncsi_dev_state_config_ecnt;
768 #endif /* CONFIG_IPV6 */
769 } else if (nd->state == ncsi_dev_state_config_ecnt) {
770 nca.type = NCSI_PKT_CMD_ECNT;
771 nd->state = ncsi_dev_state_config_ec;
772 } else if (nd->state == ncsi_dev_state_config_ec) {
773 /* Enable AEN if it's supported */
774 nca.type = NCSI_PKT_CMD_EC;
775 nd->state = ncsi_dev_state_config_ae;
776 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
777 nd->state = ncsi_dev_state_config_gls;
778 } else if (nd->state == ncsi_dev_state_config_ae) {
779 nca.type = NCSI_PKT_CMD_AE;
781 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
782 nd->state = ncsi_dev_state_config_gls;
783 } else if (nd->state == ncsi_dev_state_config_gls) {
784 nca.type = NCSI_PKT_CMD_GLS;
785 nd->state = ncsi_dev_state_config_done;
788 ret = ncsi_xmit_cmd(&nca);
790 netdev_err(ndp->ndev.dev,
791 "NCSI: Failed to transmit CMD %x\n",
796 case ncsi_dev_state_config_done:
797 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
799 spin_lock_irqsave(&nc->lock, flags);
800 if (nc->reconfigure_needed) {
801 /* This channel's configuration has been updated
802 * part-way during the config state - start the
803 * channel configuration over
805 nc->reconfigure_needed = false;
806 nc->state = NCSI_CHANNEL_INACTIVE;
807 spin_unlock_irqrestore(&nc->lock, flags);
809 spin_lock_irqsave(&ndp->lock, flags);
810 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
811 spin_unlock_irqrestore(&ndp->lock, flags);
813 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
814 ncsi_process_next_channel(ndp);
818 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
820 nc->state = NCSI_CHANNEL_ACTIVE;
823 nc->state = NCSI_CHANNEL_INACTIVE;
824 netdev_dbg(ndp->ndev.dev,
825 "NCSI: channel %u link down after config\n",
828 spin_unlock_irqrestore(&nc->lock, flags);
830 /* Update the hot channel */
831 spin_lock_irqsave(&ndp->lock, flags);
832 ndp->hot_channel = hot_nc;
833 spin_unlock_irqrestore(&ndp->lock, flags);
835 ncsi_start_channel_monitor(nc);
836 ncsi_process_next_channel(ndp);
839 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
846 ncsi_report_link(ndp, true);
849 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
851 struct ncsi_package *np, *force_package;
852 struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
853 struct ncsi_channel_mode *ncm;
856 spin_lock_irqsave(&ndp->lock, flags);
857 hot_nc = ndp->hot_channel;
858 force_channel = ndp->force_channel;
859 force_package = ndp->force_package;
860 spin_unlock_irqrestore(&ndp->lock, flags);
862 /* Force a specific channel whether or not it has link if we have been
863 * configured to do so
865 if (force_package && force_channel) {
866 found = force_channel;
867 ncm = &found->modes[NCSI_MODE_LINK];
868 if (!(ncm->data[2] & 0x1))
869 netdev_info(ndp->ndev.dev,
870 "NCSI: Channel %u forced, but it is link down\n",
875 /* The search is done once an inactive channel with up
879 NCSI_FOR_EACH_PACKAGE(ndp, np) {
880 if (ndp->force_package && np != ndp->force_package)
882 NCSI_FOR_EACH_CHANNEL(np, nc) {
883 spin_lock_irqsave(&nc->lock, flags);
885 if (!list_empty(&nc->link) ||
886 nc->state != NCSI_CHANNEL_INACTIVE) {
887 spin_unlock_irqrestore(&nc->lock, flags);
897 ncm = &nc->modes[NCSI_MODE_LINK];
898 if (ncm->data[2] & 0x1) {
899 spin_unlock_irqrestore(&nc->lock, flags);
904 spin_unlock_irqrestore(&nc->lock, flags);
909 netdev_warn(ndp->ndev.dev,
910 "NCSI: No channel found with link\n");
911 ncsi_report_link(ndp, true);
915 ncm = &found->modes[NCSI_MODE_LINK];
916 netdev_dbg(ndp->ndev.dev,
917 "NCSI: Channel %u added to queue (link %s)\n",
918 found->id, ncm->data[2] & 0x1 ? "up" : "down");
921 spin_lock_irqsave(&ndp->lock, flags);
922 list_add_tail_rcu(&found->link, &ndp->channel_queue);
923 spin_unlock_irqrestore(&ndp->lock, flags);
925 return ncsi_process_next_channel(ndp);
928 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
930 struct ncsi_package *np;
931 struct ncsi_channel *nc;
933 bool has_channel = false;
935 /* The hardware arbitration is disabled if any one channel
936 * doesn't support explicitly.
938 NCSI_FOR_EACH_PACKAGE(ndp, np) {
939 NCSI_FOR_EACH_CHANNEL(np, nc) {
942 cap = nc->caps[NCSI_CAP_GENERIC].cap;
943 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
944 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
945 NCSI_CAP_GENERIC_HWA_SUPPORT) {
946 ndp->flags &= ~NCSI_DEV_HWA;
953 ndp->flags |= NCSI_DEV_HWA;
957 ndp->flags &= ~NCSI_DEV_HWA;
961 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
963 struct ncsi_package *np;
964 struct ncsi_channel *nc;
967 /* Move all available channels to processing queue */
968 spin_lock_irqsave(&ndp->lock, flags);
969 NCSI_FOR_EACH_PACKAGE(ndp, np) {
970 NCSI_FOR_EACH_CHANNEL(np, nc) {
971 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
972 !list_empty(&nc->link));
973 ncsi_stop_channel_monitor(nc);
974 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
977 spin_unlock_irqrestore(&ndp->lock, flags);
979 /* We can have no channels in extremely case */
980 if (list_empty(&ndp->channel_queue)) {
981 netdev_err(ndp->ndev.dev,
982 "NCSI: No available channels for HWA\n");
983 ncsi_report_link(ndp, false);
987 return ncsi_process_next_channel(ndp);
990 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
992 struct ncsi_dev *nd = &ndp->ndev;
993 struct ncsi_package *np;
994 struct ncsi_channel *nc;
995 struct ncsi_cmd_arg nca;
1000 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1001 switch (nd->state) {
1002 case ncsi_dev_state_probe:
1003 nd->state = ncsi_dev_state_probe_deselect;
1005 case ncsi_dev_state_probe_deselect:
1006 ndp->pending_req_num = 8;
1008 /* Deselect all possible packages */
1009 nca.type = NCSI_PKT_CMD_DP;
1010 nca.channel = NCSI_RESERVED_CHANNEL;
1011 for (index = 0; index < 8; index++) {
1012 nca.package = index;
1013 ret = ncsi_xmit_cmd(&nca);
1018 nd->state = ncsi_dev_state_probe_package;
1020 case ncsi_dev_state_probe_package:
1021 ndp->pending_req_num = 16;
1023 /* Select all possible packages */
1024 nca.type = NCSI_PKT_CMD_SP;
1026 nca.channel = NCSI_RESERVED_CHANNEL;
1027 for (index = 0; index < 8; index++) {
1028 nca.package = index;
1029 ret = ncsi_xmit_cmd(&nca);
1034 /* Disable all possible packages */
1035 nca.type = NCSI_PKT_CMD_DP;
1036 for (index = 0; index < 8; index++) {
1037 nca.package = index;
1038 ret = ncsi_xmit_cmd(&nca);
1043 nd->state = ncsi_dev_state_probe_channel;
1045 case ncsi_dev_state_probe_channel:
1046 if (!ndp->active_package)
1047 ndp->active_package = list_first_or_null_rcu(
1048 &ndp->packages, struct ncsi_package, node);
1049 else if (list_is_last(&ndp->active_package->node,
1051 ndp->active_package = NULL;
1053 ndp->active_package = list_next_entry(
1054 ndp->active_package, node);
1056 /* All available packages and channels are enumerated. The
1057 * enumeration happens for once when the NCSI interface is
1058 * started. So we need continue to start the interface after
1061 * We have to choose an active channel before configuring it.
1062 * Note that we possibly don't have active channel in extreme
1065 if (!ndp->active_package) {
1066 ndp->flags |= NCSI_DEV_PROBED;
1067 if (ncsi_check_hwa(ndp))
1068 ncsi_enable_hwa(ndp);
1070 ncsi_choose_active_channel(ndp);
1074 /* Select the active package */
1075 ndp->pending_req_num = 1;
1076 nca.type = NCSI_PKT_CMD_SP;
1078 nca.package = ndp->active_package->id;
1079 nca.channel = NCSI_RESERVED_CHANNEL;
1080 ret = ncsi_xmit_cmd(&nca);
1084 nd->state = ncsi_dev_state_probe_cis;
1086 case ncsi_dev_state_probe_cis:
1087 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1089 /* Clear initial state */
1090 nca.type = NCSI_PKT_CMD_CIS;
1091 nca.package = ndp->active_package->id;
1092 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1093 nca.channel = index;
1094 ret = ncsi_xmit_cmd(&nca);
1099 nd->state = ncsi_dev_state_probe_gvi;
1101 case ncsi_dev_state_probe_gvi:
1102 case ncsi_dev_state_probe_gc:
1103 case ncsi_dev_state_probe_gls:
1104 np = ndp->active_package;
1105 ndp->pending_req_num = np->channel_num;
1107 /* Retrieve version, capability or link status */
1108 if (nd->state == ncsi_dev_state_probe_gvi)
1109 nca.type = NCSI_PKT_CMD_GVI;
1110 else if (nd->state == ncsi_dev_state_probe_gc)
1111 nca.type = NCSI_PKT_CMD_GC;
1113 nca.type = NCSI_PKT_CMD_GLS;
1115 nca.package = np->id;
1116 NCSI_FOR_EACH_CHANNEL(np, nc) {
1117 nca.channel = nc->id;
1118 ret = ncsi_xmit_cmd(&nca);
1123 if (nd->state == ncsi_dev_state_probe_gvi)
1124 nd->state = ncsi_dev_state_probe_gc;
1125 else if (nd->state == ncsi_dev_state_probe_gc)
1126 nd->state = ncsi_dev_state_probe_gls;
1128 nd->state = ncsi_dev_state_probe_dp;
1130 case ncsi_dev_state_probe_dp:
1131 ndp->pending_req_num = 1;
1133 /* Deselect the active package */
1134 nca.type = NCSI_PKT_CMD_DP;
1135 nca.package = ndp->active_package->id;
1136 nca.channel = NCSI_RESERVED_CHANNEL;
1137 ret = ncsi_xmit_cmd(&nca);
1141 /* Scan channels in next package */
1142 nd->state = ncsi_dev_state_probe_channel;
1145 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1151 netdev_err(ndp->ndev.dev,
1152 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1154 ncsi_report_link(ndp, true);
1157 static void ncsi_dev_work(struct work_struct *work)
1159 struct ncsi_dev_priv *ndp = container_of(work,
1160 struct ncsi_dev_priv, work);
1161 struct ncsi_dev *nd = &ndp->ndev;
1163 switch (nd->state & ncsi_dev_state_major) {
1164 case ncsi_dev_state_probe:
1165 ncsi_probe_channel(ndp);
1167 case ncsi_dev_state_suspend:
1168 ncsi_suspend_channel(ndp);
1170 case ncsi_dev_state_config:
1171 ncsi_configure_channel(ndp);
1174 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1179 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1181 struct ncsi_channel *nc;
1183 unsigned long flags;
1185 spin_lock_irqsave(&ndp->lock, flags);
1186 nc = list_first_or_null_rcu(&ndp->channel_queue,
1187 struct ncsi_channel, link);
1189 spin_unlock_irqrestore(&ndp->lock, flags);
1193 list_del_init(&nc->link);
1194 spin_unlock_irqrestore(&ndp->lock, flags);
1196 spin_lock_irqsave(&nc->lock, flags);
1197 old_state = nc->state;
1198 nc->state = NCSI_CHANNEL_INVISIBLE;
1199 spin_unlock_irqrestore(&nc->lock, flags);
1201 ndp->active_channel = nc;
1202 ndp->active_package = nc->package;
1204 switch (old_state) {
1205 case NCSI_CHANNEL_INACTIVE:
1206 ndp->ndev.state = ncsi_dev_state_config;
1207 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1209 ncsi_configure_channel(ndp);
1211 case NCSI_CHANNEL_ACTIVE:
1212 ndp->ndev.state = ncsi_dev_state_suspend;
1213 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1215 ncsi_suspend_channel(ndp);
1218 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1219 old_state, nc->package->id, nc->id);
1220 ncsi_report_link(ndp, false);
1227 ndp->active_channel = NULL;
1228 ndp->active_package = NULL;
1229 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1230 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1231 return ncsi_choose_active_channel(ndp);
1234 ncsi_report_link(ndp, false);
1238 #if IS_ENABLED(CONFIG_IPV6)
1239 static int ncsi_inet6addr_event(struct notifier_block *this,
1240 unsigned long event, void *data)
1242 struct inet6_ifaddr *ifa = data;
1243 struct net_device *dev = ifa->idev->dev;
1244 struct ncsi_dev *nd = ncsi_find_dev(dev);
1245 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1246 struct ncsi_package *np;
1247 struct ncsi_channel *nc;
1248 struct ncsi_cmd_arg nca;
1252 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1253 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1258 action = (++ndp->inet6_addr_num) == 1;
1259 nca.type = NCSI_PKT_CMD_EGMF;
1262 action = (--ndp->inet6_addr_num == 0);
1263 nca.type = NCSI_PKT_CMD_DGMF;
1269 /* We might not have active channel or packages. The IPv6
1270 * required multicast will be enabled when active channel
1271 * or packages are chosen.
1273 np = ndp->active_package;
1274 nc = ndp->active_channel;
1275 if (!action || !np || !nc)
1278 /* We needn't enable or disable it if the function isn't supported */
1279 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1284 nca.package = np->id;
1285 nca.channel = nc->id;
1286 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1287 ret = ncsi_xmit_cmd(&nca);
1289 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1290 (event == NETDEV_UP) ? "enable" : "disable", ret);
1297 static struct notifier_block ncsi_inet6addr_notifier = {
1298 .notifier_call = ncsi_inet6addr_event,
1300 #endif /* CONFIG_IPV6 */
1302 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1304 struct ncsi_dev *nd = &ndp->ndev;
1305 struct ncsi_channel *nc;
1306 struct ncsi_package *np;
1307 unsigned long flags;
1310 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1311 NCSI_FOR_EACH_CHANNEL(np, nc) {
1312 spin_lock_irqsave(&nc->lock, flags);
1314 /* Channels may be busy, mark dirty instead of
1316 * a) not ACTIVE (configured)
1317 * b) in the channel_queue (to be configured)
1318 * c) it's ndev is in the config state
1320 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1321 if ((ndp->ndev.state & 0xff00) ==
1322 ncsi_dev_state_config ||
1323 !list_empty(&nc->link)) {
1325 "NCSI: channel %p marked dirty\n",
1327 nc->reconfigure_needed = true;
1329 spin_unlock_irqrestore(&nc->lock, flags);
1333 spin_unlock_irqrestore(&nc->lock, flags);
1335 ncsi_stop_channel_monitor(nc);
1336 spin_lock_irqsave(&nc->lock, flags);
1337 nc->state = NCSI_CHANNEL_INACTIVE;
1338 spin_unlock_irqrestore(&nc->lock, flags);
1340 spin_lock_irqsave(&ndp->lock, flags);
1341 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1342 spin_unlock_irqrestore(&ndp->lock, flags);
1344 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
1352 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1354 struct ncsi_dev_priv *ndp;
1355 unsigned int n_vids = 0;
1356 struct vlan_vid *vlan;
1357 struct ncsi_dev *nd;
1363 nd = ncsi_find_dev(dev);
1365 netdev_warn(dev, "NCSI: No net_device?\n");
1369 ndp = TO_NCSI_DEV_PRIV(nd);
1371 /* Add the VLAN id to our internal list */
1372 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1374 if (vlan->vid == vid) {
1375 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1380 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1382 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1383 vid, NCSI_MAX_VLAN_VIDS);
1387 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1391 vlan->proto = proto;
1393 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1395 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
1397 found = ncsi_kick_channels(ndp) != 0;
1399 return found ? ncsi_process_next_channel(ndp) : 0;
1401 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1403 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1405 struct vlan_vid *vlan, *tmp;
1406 struct ncsi_dev_priv *ndp;
1407 struct ncsi_dev *nd;
1413 nd = ncsi_find_dev(dev);
1415 netdev_warn(dev, "NCSI: no net_device?\n");
1419 ndp = TO_NCSI_DEV_PRIV(nd);
1421 /* Remove the VLAN id from our internal list */
1422 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1423 if (vlan->vid == vid) {
1424 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
1425 list_del_rcu(&vlan->list);
1431 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1435 found = ncsi_kick_channels(ndp) != 0;
1437 return found ? ncsi_process_next_channel(ndp) : 0;
1439 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1441 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1442 void (*handler)(struct ncsi_dev *ndev))
1444 struct ncsi_dev_priv *ndp;
1445 struct ncsi_dev *nd;
1446 unsigned long flags;
1449 /* Check if the device has been registered or not */
1450 nd = ncsi_find_dev(dev);
1454 /* Create NCSI device */
1455 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1460 nd->state = ncsi_dev_state_registered;
1462 nd->handler = handler;
1463 ndp->pending_req_num = 0;
1464 INIT_LIST_HEAD(&ndp->channel_queue);
1465 INIT_LIST_HEAD(&ndp->vlan_vids);
1466 INIT_WORK(&ndp->work, ncsi_dev_work);
1468 /* Initialize private NCSI device */
1469 spin_lock_init(&ndp->lock);
1470 INIT_LIST_HEAD(&ndp->packages);
1471 ndp->request_id = NCSI_REQ_START_IDX;
1472 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1473 ndp->requests[i].id = i;
1474 ndp->requests[i].ndp = ndp;
1475 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
1478 spin_lock_irqsave(&ncsi_dev_lock, flags);
1479 #if IS_ENABLED(CONFIG_IPV6)
1480 ndp->inet6_addr_num = 0;
1481 if (list_empty(&ncsi_dev_list))
1482 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1484 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1485 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1487 /* Register NCSI packet Rx handler */
1488 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1489 ndp->ptype.func = ncsi_rcv_rsp;
1490 ndp->ptype.dev = dev;
1491 dev_add_pack(&ndp->ptype);
1495 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1497 int ncsi_start_dev(struct ncsi_dev *nd)
1499 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1502 if (nd->state != ncsi_dev_state_registered &&
1503 nd->state != ncsi_dev_state_functional)
1506 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1507 nd->state = ncsi_dev_state_probe;
1508 schedule_work(&ndp->work);
1512 if (ndp->flags & NCSI_DEV_HWA) {
1513 netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
1514 ret = ncsi_enable_hwa(ndp);
1516 ret = ncsi_choose_active_channel(ndp);
1521 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1523 void ncsi_stop_dev(struct ncsi_dev *nd)
1525 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1526 struct ncsi_package *np;
1527 struct ncsi_channel *nc;
1530 unsigned long flags;
1532 /* Stop the channel monitor and reset channel's state */
1533 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1534 NCSI_FOR_EACH_CHANNEL(np, nc) {
1535 ncsi_stop_channel_monitor(nc);
1537 spin_lock_irqsave(&nc->lock, flags);
1538 chained = !list_empty(&nc->link);
1539 old_state = nc->state;
1540 nc->state = NCSI_CHANNEL_INACTIVE;
1541 spin_unlock_irqrestore(&nc->lock, flags);
1543 WARN_ON_ONCE(chained ||
1544 old_state == NCSI_CHANNEL_INVISIBLE);
1548 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
1549 ncsi_report_link(ndp, true);
1551 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1553 void ncsi_unregister_dev(struct ncsi_dev *nd)
1555 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1556 struct ncsi_package *np, *tmp;
1557 unsigned long flags;
1559 dev_remove_pack(&ndp->ptype);
1561 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1562 ncsi_remove_package(np);
1564 spin_lock_irqsave(&ncsi_dev_lock, flags);
1565 list_del_rcu(&ndp->node);
1566 #if IS_ENABLED(CONFIG_IPV6)
1567 if (list_empty(&ncsi_dev_list))
1568 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1570 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1574 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);