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>
15 #include <linux/netlink.h>
18 #include <net/net_namespace.h>
20 #include <net/addrconf.h>
22 #include <net/if_inet6.h>
27 LIST_HEAD(ncsi_dev_list);
28 DEFINE_SPINLOCK(ncsi_dev_lock);
30 static inline int ncsi_filter_size(int table)
32 int sizes[] = { 2, 6, 6, 6 };
34 BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX);
35 if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX)
41 u32 *ncsi_get_filter(struct ncsi_channel *nc, int table, int index)
43 struct ncsi_channel_filter *ncf;
46 ncf = nc->filters[table];
50 size = ncsi_filter_size(table);
54 return ncf->data + size * index;
57 /* Find the first active filter in a filter table that matches the given
58 * data parameter. If data is NULL, this returns the first active filter.
60 int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
62 struct ncsi_channel_filter *ncf;
67 ncf = nc->filters[table];
71 size = ncsi_filter_size(table);
75 spin_lock_irqsave(&nc->lock, flags);
76 bitmap = (void *)&ncf->bitmap;
78 while ((index = find_next_bit(bitmap, ncf->total, index + 1))
80 if (!data || !memcmp(ncf->data + size * index, data, size)) {
81 spin_unlock_irqrestore(&nc->lock, flags);
85 spin_unlock_irqrestore(&nc->lock, flags);
90 int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
92 struct ncsi_channel_filter *ncf;
97 size = ncsi_filter_size(table);
101 index = ncsi_find_filter(nc, table, data);
105 ncf = nc->filters[table];
109 spin_lock_irqsave(&nc->lock, flags);
110 bitmap = (void *)&ncf->bitmap;
112 index = find_next_zero_bit(bitmap, ncf->total, 0);
113 if (index >= ncf->total) {
114 spin_unlock_irqrestore(&nc->lock, flags);
117 } while (test_and_set_bit(index, bitmap));
119 memcpy(ncf->data + size * index, data, size);
120 spin_unlock_irqrestore(&nc->lock, flags);
125 int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
127 struct ncsi_channel_filter *ncf;
132 size = ncsi_filter_size(table);
136 ncf = nc->filters[table];
137 if (!ncf || index >= ncf->total)
140 spin_lock_irqsave(&nc->lock, flags);
141 bitmap = (void *)&ncf->bitmap;
142 if (test_and_clear_bit(index, bitmap))
143 memset(ncf->data + size * index, 0, size);
144 spin_unlock_irqrestore(&nc->lock, flags);
149 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
151 struct ncsi_dev *nd = &ndp->ndev;
152 struct ncsi_package *np;
153 struct ncsi_channel *nc;
156 nd->state = ncsi_dev_state_functional;
163 NCSI_FOR_EACH_PACKAGE(ndp, np) {
164 NCSI_FOR_EACH_CHANNEL(np, nc) {
165 spin_lock_irqsave(&nc->lock, flags);
167 if (!list_empty(&nc->link) ||
168 nc->state != NCSI_CHANNEL_ACTIVE) {
169 spin_unlock_irqrestore(&nc->lock, flags);
173 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
174 spin_unlock_irqrestore(&nc->lock, flags);
179 spin_unlock_irqrestore(&nc->lock, flags);
187 static void ncsi_channel_monitor(unsigned long data)
189 struct ncsi_channel *nc = (struct ncsi_channel *)data;
190 struct ncsi_package *np = nc->package;
191 struct ncsi_dev_priv *ndp = np->ndp;
192 struct ncsi_cmd_arg nca;
193 bool enabled, chained;
194 unsigned int monitor_state;
198 spin_lock_irqsave(&nc->lock, flags);
200 chained = !list_empty(&nc->link);
201 enabled = nc->monitor.enabled;
202 monitor_state = nc->monitor.state;
203 spin_unlock_irqrestore(&nc->lock, flags);
205 if (!enabled || chained) {
206 ncsi_stop_channel_monitor(nc);
209 if (state != NCSI_CHANNEL_INACTIVE &&
210 state != NCSI_CHANNEL_ACTIVE) {
211 ncsi_stop_channel_monitor(nc);
215 switch (monitor_state) {
216 case NCSI_CHANNEL_MONITOR_START:
217 case NCSI_CHANNEL_MONITOR_RETRY:
219 nca.package = np->id;
220 nca.channel = nc->id;
221 nca.type = NCSI_PKT_CMD_GLS;
223 ret = ncsi_xmit_cmd(&nca);
225 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
228 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
231 if (!(ndp->flags & NCSI_DEV_HWA) &&
232 state == NCSI_CHANNEL_ACTIVE) {
233 ncsi_report_link(ndp, true);
234 ndp->flags |= NCSI_DEV_RESHUFFLE;
237 ncsi_stop_channel_monitor(nc);
239 spin_lock_irqsave(&nc->lock, flags);
240 nc->state = NCSI_CHANNEL_INVISIBLE;
241 spin_unlock_irqrestore(&nc->lock, flags);
243 spin_lock_irqsave(&ndp->lock, flags);
244 nc->state = NCSI_CHANNEL_INACTIVE;
245 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
246 spin_unlock_irqrestore(&ndp->lock, flags);
247 ncsi_process_next_channel(ndp);
251 spin_lock_irqsave(&nc->lock, flags);
253 spin_unlock_irqrestore(&nc->lock, flags);
254 mod_timer(&nc->monitor.timer, jiffies + HZ);
257 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
261 spin_lock_irqsave(&nc->lock, flags);
262 WARN_ON_ONCE(nc->monitor.enabled);
263 nc->monitor.enabled = true;
264 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
265 spin_unlock_irqrestore(&nc->lock, flags);
267 mod_timer(&nc->monitor.timer, jiffies + HZ);
270 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
274 spin_lock_irqsave(&nc->lock, flags);
275 if (!nc->monitor.enabled) {
276 spin_unlock_irqrestore(&nc->lock, flags);
279 nc->monitor.enabled = false;
280 spin_unlock_irqrestore(&nc->lock, flags);
282 del_timer_sync(&nc->monitor.timer);
285 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
288 struct ncsi_channel *nc;
290 NCSI_FOR_EACH_CHANNEL(np, nc) {
298 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
300 struct ncsi_channel *nc, *tmp;
304 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
310 nc->state = NCSI_CHANNEL_INACTIVE;
311 nc->monitor.enabled = false;
312 setup_timer(&nc->monitor.timer,
313 ncsi_channel_monitor, (unsigned long)nc);
314 spin_lock_init(&nc->lock);
315 INIT_LIST_HEAD(&nc->link);
316 for (index = 0; index < NCSI_CAP_MAX; index++)
317 nc->caps[index].index = index;
318 for (index = 0; index < NCSI_MODE_MAX; index++)
319 nc->modes[index].index = index;
321 spin_lock_irqsave(&np->lock, flags);
322 tmp = ncsi_find_channel(np, id);
324 spin_unlock_irqrestore(&np->lock, flags);
329 list_add_tail_rcu(&nc->node, &np->channels);
331 spin_unlock_irqrestore(&np->lock, flags);
336 static void ncsi_remove_channel(struct ncsi_channel *nc)
338 struct ncsi_package *np = nc->package;
339 struct ncsi_channel_filter *ncf;
343 /* Release filters */
344 spin_lock_irqsave(&nc->lock, flags);
345 for (i = 0; i < NCSI_FILTER_MAX; i++) {
346 ncf = nc->filters[i];
350 nc->filters[i] = NULL;
354 nc->state = NCSI_CHANNEL_INACTIVE;
355 spin_unlock_irqrestore(&nc->lock, flags);
356 ncsi_stop_channel_monitor(nc);
358 /* Remove and free channel */
359 spin_lock_irqsave(&np->lock, flags);
360 list_del_rcu(&nc->node);
362 spin_unlock_irqrestore(&np->lock, flags);
367 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
370 struct ncsi_package *np;
372 NCSI_FOR_EACH_PACKAGE(ndp, np) {
380 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
383 struct ncsi_package *np, *tmp;
386 np = kzalloc(sizeof(*np), GFP_ATOMIC);
392 spin_lock_init(&np->lock);
393 INIT_LIST_HEAD(&np->channels);
395 spin_lock_irqsave(&ndp->lock, flags);
396 tmp = ncsi_find_package(ndp, id);
398 spin_unlock_irqrestore(&ndp->lock, flags);
403 list_add_tail_rcu(&np->node, &ndp->packages);
405 spin_unlock_irqrestore(&ndp->lock, flags);
410 void ncsi_remove_package(struct ncsi_package *np)
412 struct ncsi_dev_priv *ndp = np->ndp;
413 struct ncsi_channel *nc, *tmp;
416 /* Release all child channels */
417 list_for_each_entry_safe(nc, tmp, &np->channels, node)
418 ncsi_remove_channel(nc);
420 /* Remove and free package */
421 spin_lock_irqsave(&ndp->lock, flags);
422 list_del_rcu(&np->node);
424 spin_unlock_irqrestore(&ndp->lock, flags);
429 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
431 struct ncsi_package **np,
432 struct ncsi_channel **nc)
434 struct ncsi_package *p;
435 struct ncsi_channel *c;
437 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
438 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
446 /* For two consecutive NCSI commands, the packet IDs shouldn't
447 * be same. Otherwise, the bogus response might be replied. So
448 * the available IDs are allocated in round-robin fashion.
450 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
451 unsigned int req_flags)
453 struct ncsi_request *nr = NULL;
454 int i, limit = ARRAY_SIZE(ndp->requests);
457 /* Check if there is one available request until the ceiling */
458 spin_lock_irqsave(&ndp->lock, flags);
459 for (i = ndp->request_id; i < limit; i++) {
460 if (ndp->requests[i].used)
463 nr = &ndp->requests[i];
465 nr->flags = req_flags;
466 ndp->request_id = i + 1;
470 /* Fail back to check from the starting cursor */
471 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
472 if (ndp->requests[i].used)
475 nr = &ndp->requests[i];
477 nr->flags = req_flags;
478 ndp->request_id = i + 1;
483 spin_unlock_irqrestore(&ndp->lock, flags);
487 void ncsi_free_request(struct ncsi_request *nr)
489 struct ncsi_dev_priv *ndp = nr->ndp;
490 struct sk_buff *cmd, *rsp;
496 del_timer_sync(&nr->timer);
499 spin_lock_irqsave(&ndp->lock, flags);
505 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
506 spin_unlock_irqrestore(&ndp->lock, flags);
508 if (driven && cmd && --ndp->pending_req_num == 0)
509 schedule_work(&ndp->work);
511 /* Release command and response */
516 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
518 struct ncsi_dev_priv *ndp;
520 NCSI_FOR_EACH_DEV(ndp) {
521 if (ndp->ndev.dev == dev)
528 static void ncsi_request_timeout(unsigned long data)
530 struct ncsi_request *nr = (struct ncsi_request *)data;
531 struct ncsi_dev_priv *ndp = nr->ndp;
534 /* If the request already had associated response,
535 * let the response handler to release it.
537 spin_lock_irqsave(&ndp->lock, flags);
539 if (nr->rsp || !nr->cmd) {
540 spin_unlock_irqrestore(&ndp->lock, flags);
543 spin_unlock_irqrestore(&ndp->lock, flags);
545 /* Release the request */
546 ncsi_free_request(nr);
549 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
551 struct ncsi_dev *nd = &ndp->ndev;
552 struct ncsi_package *np = ndp->active_package;
553 struct ncsi_channel *nc = ndp->active_channel;
554 struct ncsi_cmd_arg nca;
559 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
561 case ncsi_dev_state_suspend:
562 nd->state = ncsi_dev_state_suspend_select;
564 case ncsi_dev_state_suspend_select:
565 ndp->pending_req_num = 1;
567 nca.type = NCSI_PKT_CMD_SP;
568 nca.package = np->id;
569 nca.channel = NCSI_RESERVED_CHANNEL;
570 if (ndp->flags & NCSI_DEV_HWA)
575 /* To retrieve the last link states of channels in current
576 * package when current active channel needs fail over to
577 * another one. It means we will possibly select another
578 * channel as next active one. The link states of channels
579 * are most important factor of the selection. So we need
580 * accurate link states. Unfortunately, the link states on
581 * inactive channels can't be updated with LSC AEN in time.
583 if (ndp->flags & NCSI_DEV_RESHUFFLE)
584 nd->state = ncsi_dev_state_suspend_gls;
586 nd->state = ncsi_dev_state_suspend_dcnt;
587 ret = ncsi_xmit_cmd(&nca);
592 case ncsi_dev_state_suspend_gls:
593 ndp->pending_req_num = np->channel_num;
595 nca.type = NCSI_PKT_CMD_GLS;
596 nca.package = np->id;
598 nd->state = ncsi_dev_state_suspend_dcnt;
599 NCSI_FOR_EACH_CHANNEL(np, nc) {
600 nca.channel = nc->id;
601 ret = ncsi_xmit_cmd(&nca);
607 case ncsi_dev_state_suspend_dcnt:
608 ndp->pending_req_num = 1;
610 nca.type = NCSI_PKT_CMD_DCNT;
611 nca.package = np->id;
612 nca.channel = nc->id;
614 nd->state = ncsi_dev_state_suspend_dc;
615 ret = ncsi_xmit_cmd(&nca);
620 case ncsi_dev_state_suspend_dc:
621 ndp->pending_req_num = 1;
623 nca.type = NCSI_PKT_CMD_DC;
624 nca.package = np->id;
625 nca.channel = nc->id;
628 nd->state = ncsi_dev_state_suspend_deselect;
629 ret = ncsi_xmit_cmd(&nca);
634 case ncsi_dev_state_suspend_deselect:
635 ndp->pending_req_num = 1;
637 nca.type = NCSI_PKT_CMD_DP;
638 nca.package = np->id;
639 nca.channel = NCSI_RESERVED_CHANNEL;
641 nd->state = ncsi_dev_state_suspend_done;
642 ret = ncsi_xmit_cmd(&nca);
647 case ncsi_dev_state_suspend_done:
648 spin_lock_irqsave(&nc->lock, flags);
649 nc->state = NCSI_CHANNEL_INACTIVE;
650 spin_unlock_irqrestore(&nc->lock, flags);
651 ncsi_process_next_channel(ndp);
655 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
661 nd->state = ncsi_dev_state_functional;
664 /* Check the VLAN filter bitmap for a set filter, and construct a
665 * "Set VLAN Filter - Disable" packet if found.
667 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
668 struct ncsi_cmd_arg *nca)
674 index = ncsi_find_filter(nc, NCSI_FILTER_VLAN, NULL);
676 /* Filter table empty */
680 data = ncsi_get_filter(nc, NCSI_FILTER_VLAN, index);
682 netdev_err(ndp->ndev.dev,
683 "ncsi: failed to retrieve filter %d\n", index);
684 /* Set the VLAN id to 0 - this will still disable the entry in
685 * the filter table, but we won't know what it was.
692 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
693 "ncsi: removed vlan tag %u at index %d\n",
695 ncsi_remove_filter(nc, NCSI_FILTER_VLAN, index);
697 nca->type = NCSI_PKT_CMD_SVF;
699 /* HW filter index starts at 1 */
700 nca->bytes[6] = index + 1;
701 nca->bytes[7] = 0x00;
705 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
708 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
709 struct ncsi_cmd_arg *nca)
711 struct vlan_vid *vlan = NULL;
714 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
715 index = ncsi_find_filter(nc, NCSI_FILTER_VLAN, &vlan->vid);
718 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
719 "ncsi: new vlan id to set: %u\n",
723 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
724 "vid %u already at filter pos %d\n",
728 if (!vlan || index >= 0) {
729 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
730 "no vlan ids left to set\n");
734 index = ncsi_add_filter(nc, NCSI_FILTER_VLAN, &vlan->vid);
736 netdev_err(ndp->ndev.dev,
737 "Failed to add new VLAN tag, error %d\n", index);
738 if (index == -ENOSPC)
739 netdev_err(ndp->ndev.dev,
740 "Channel %u already has all VLAN filters set\n",
745 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
746 "ncsi: set vid %u in packet, index %u\n",
747 vlan->vid, index + 1);
748 nca->type = NCSI_PKT_CMD_SVF;
749 nca->words[1] = vlan->vid;
750 /* HW filter index starts at 1 */
751 nca->bytes[6] = index + 1;
752 nca->bytes[7] = 0x01;
757 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
759 struct ncsi_dev *nd = &ndp->ndev;
760 struct net_device *dev = nd->dev;
761 struct ncsi_package *np = ndp->active_package;
762 struct ncsi_channel *nc = ndp->active_channel;
763 struct ncsi_channel *hot_nc = NULL;
764 struct ncsi_cmd_arg nca;
770 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
772 case ncsi_dev_state_config:
773 case ncsi_dev_state_config_sp:
774 ndp->pending_req_num = 1;
776 /* Select the specific package */
777 nca.type = NCSI_PKT_CMD_SP;
778 if (ndp->flags & NCSI_DEV_HWA)
782 nca.package = np->id;
783 nca.channel = NCSI_RESERVED_CHANNEL;
784 ret = ncsi_xmit_cmd(&nca);
788 nd->state = ncsi_dev_state_config_cis;
790 case ncsi_dev_state_config_cis:
791 ndp->pending_req_num = 1;
793 /* Clear initial state */
794 nca.type = NCSI_PKT_CMD_CIS;
795 nca.package = np->id;
796 nca.channel = nc->id;
797 ret = ncsi_xmit_cmd(&nca);
801 nd->state = ncsi_dev_state_config_clear_vids;
803 case ncsi_dev_state_config_clear_vids:
804 case ncsi_dev_state_config_svf:
805 case ncsi_dev_state_config_ev:
806 case ncsi_dev_state_config_sma:
807 case ncsi_dev_state_config_ebf:
808 #if IS_ENABLED(CONFIG_IPV6)
809 case ncsi_dev_state_config_egmf:
811 case ncsi_dev_state_config_ecnt:
812 case ncsi_dev_state_config_ec:
813 case ncsi_dev_state_config_ae:
814 case ncsi_dev_state_config_gls:
815 ndp->pending_req_num = 1;
817 nca.package = np->id;
818 nca.channel = nc->id;
820 /* Clear any active filters on the channel before setting */
821 if (nd->state == ncsi_dev_state_config_clear_vids) {
822 ret = clear_one_vid(ndp, nc, &nca);
824 nd->state = ncsi_dev_state_config_svf;
825 schedule_work(&ndp->work);
829 nd->state = ncsi_dev_state_config_clear_vids;
830 /* Add known VLAN tags to the filter */
831 } else if (nd->state == ncsi_dev_state_config_svf) {
832 ret = set_one_vid(ndp, nc, &nca);
834 nd->state = ncsi_dev_state_config_ev;
835 schedule_work(&ndp->work);
839 nd->state = ncsi_dev_state_config_svf;
840 /* Enable/Disable the VLAN filter */
841 } else if (nd->state == ncsi_dev_state_config_ev) {
842 if (list_empty(&ndp->vlan_vids)) {
843 nca.type = NCSI_PKT_CMD_DV;
845 nca.type = NCSI_PKT_CMD_EV;
846 nca.bytes[3] = NCSI_CAP_VLAN_NO;
848 nd->state = ncsi_dev_state_config_sma;
849 } else if (nd->state == ncsi_dev_state_config_sma) {
850 /* Use first entry in unicast filter table. Note that
851 * the MAC filter table starts from entry 1 instead of
854 nca.type = NCSI_PKT_CMD_SMA;
855 for (index = 0; index < 6; index++)
856 nca.bytes[index] = dev->dev_addr[index];
859 nd->state = ncsi_dev_state_config_ebf;
860 } else if (nd->state == ncsi_dev_state_config_ebf) {
861 nca.type = NCSI_PKT_CMD_EBF;
862 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
863 nd->state = ncsi_dev_state_config_ecnt;
864 #if IS_ENABLED(CONFIG_IPV6)
865 if (ndp->inet6_addr_num > 0 &&
866 (nc->caps[NCSI_CAP_GENERIC].cap &
867 NCSI_CAP_GENERIC_MC))
868 nd->state = ncsi_dev_state_config_egmf;
870 nd->state = ncsi_dev_state_config_ecnt;
871 } else if (nd->state == ncsi_dev_state_config_egmf) {
872 nca.type = NCSI_PKT_CMD_EGMF;
873 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
874 nd->state = ncsi_dev_state_config_ecnt;
875 #endif /* CONFIG_IPV6 */
876 } else if (nd->state == ncsi_dev_state_config_ecnt) {
877 nca.type = NCSI_PKT_CMD_ECNT;
878 nd->state = ncsi_dev_state_config_ec;
879 } else if (nd->state == ncsi_dev_state_config_ec) {
880 /* Enable AEN if it's supported */
881 nca.type = NCSI_PKT_CMD_EC;
882 nd->state = ncsi_dev_state_config_ae;
883 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
884 nd->state = ncsi_dev_state_config_gls;
885 } else if (nd->state == ncsi_dev_state_config_ae) {
886 nca.type = NCSI_PKT_CMD_AE;
888 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
889 nd->state = ncsi_dev_state_config_gls;
890 } else if (nd->state == ncsi_dev_state_config_gls) {
891 nca.type = NCSI_PKT_CMD_GLS;
892 nd->state = ncsi_dev_state_config_done;
895 ret = ncsi_xmit_cmd(&nca);
899 case ncsi_dev_state_config_done:
900 spin_lock_irqsave(&nc->lock, flags);
901 if (nc->reconfigure_needed) {
902 /* This channel's configuration has been updated
903 * part-way during the config state - start the
904 * channel configuration over
906 nc->reconfigure_needed = false;
907 nc->state = NCSI_CHANNEL_INACTIVE;
908 spin_unlock_irqrestore(&nc->lock, flags);
910 spin_lock_irqsave(&ndp->lock, flags);
911 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
912 spin_unlock_irqrestore(&ndp->lock, flags);
914 netdev_printk(KERN_DEBUG, dev,
915 "Dirty NCSI channel state reset\n");
916 ncsi_process_next_channel(ndp);
920 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
922 nc->state = NCSI_CHANNEL_ACTIVE;
925 nc->state = NCSI_CHANNEL_INACTIVE;
927 spin_unlock_irqrestore(&nc->lock, flags);
929 /* Update the hot channel */
930 spin_lock_irqsave(&ndp->lock, flags);
931 ndp->hot_channel = hot_nc;
932 spin_unlock_irqrestore(&ndp->lock, flags);
934 ncsi_start_channel_monitor(nc);
935 ncsi_process_next_channel(ndp);
938 netdev_warn(dev, "Wrong NCSI state 0x%x in config\n",
945 ncsi_report_link(ndp, true);
948 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
950 struct ncsi_package *np;
951 struct ncsi_channel *nc, *found, *hot_nc;
952 struct ncsi_channel_mode *ncm;
955 spin_lock_irqsave(&ndp->lock, flags);
956 hot_nc = ndp->hot_channel;
957 spin_unlock_irqrestore(&ndp->lock, flags);
959 /* The search is done once an inactive channel with up
963 NCSI_FOR_EACH_PACKAGE(ndp, np) {
964 NCSI_FOR_EACH_CHANNEL(np, nc) {
965 spin_lock_irqsave(&nc->lock, flags);
967 if (!list_empty(&nc->link) ||
968 nc->state != NCSI_CHANNEL_INACTIVE) {
969 spin_unlock_irqrestore(&nc->lock, flags);
979 ncm = &nc->modes[NCSI_MODE_LINK];
980 if (ncm->data[2] & 0x1) {
981 spin_unlock_irqrestore(&nc->lock, flags);
986 spin_unlock_irqrestore(&nc->lock, flags);
991 ncsi_report_link(ndp, true);
996 spin_lock_irqsave(&ndp->lock, flags);
997 list_add_tail_rcu(&found->link, &ndp->channel_queue);
998 spin_unlock_irqrestore(&ndp->lock, flags);
1000 return ncsi_process_next_channel(ndp);
1003 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1005 struct ncsi_package *np;
1006 struct ncsi_channel *nc;
1009 /* The hardware arbitration is disabled if any one channel
1010 * doesn't support explicitly.
1012 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1013 NCSI_FOR_EACH_CHANNEL(np, nc) {
1014 cap = nc->caps[NCSI_CAP_GENERIC].cap;
1015 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
1016 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
1017 NCSI_CAP_GENERIC_HWA_SUPPORT) {
1018 ndp->flags &= ~NCSI_DEV_HWA;
1024 ndp->flags |= NCSI_DEV_HWA;
1028 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
1030 struct ncsi_package *np;
1031 struct ncsi_channel *nc;
1032 unsigned long flags;
1034 /* Move all available channels to processing queue */
1035 spin_lock_irqsave(&ndp->lock, flags);
1036 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1037 NCSI_FOR_EACH_CHANNEL(np, nc) {
1038 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
1039 !list_empty(&nc->link));
1040 ncsi_stop_channel_monitor(nc);
1041 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1044 spin_unlock_irqrestore(&ndp->lock, flags);
1046 /* We can have no channels in extremely case */
1047 if (list_empty(&ndp->channel_queue)) {
1048 ncsi_report_link(ndp, false);
1052 return ncsi_process_next_channel(ndp);
1055 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1057 struct ncsi_dev *nd = &ndp->ndev;
1058 struct ncsi_package *np;
1059 struct ncsi_channel *nc;
1060 struct ncsi_cmd_arg nca;
1061 unsigned char index;
1065 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1066 switch (nd->state) {
1067 case ncsi_dev_state_probe:
1068 nd->state = ncsi_dev_state_probe_deselect;
1070 case ncsi_dev_state_probe_deselect:
1071 ndp->pending_req_num = 8;
1073 /* Deselect all possible packages */
1074 nca.type = NCSI_PKT_CMD_DP;
1075 nca.channel = NCSI_RESERVED_CHANNEL;
1076 for (index = 0; index < 8; index++) {
1077 nca.package = index;
1078 ret = ncsi_xmit_cmd(&nca);
1083 nd->state = ncsi_dev_state_probe_package;
1085 case ncsi_dev_state_probe_package:
1086 ndp->pending_req_num = 16;
1088 /* Select all possible packages */
1089 nca.type = NCSI_PKT_CMD_SP;
1091 nca.channel = NCSI_RESERVED_CHANNEL;
1092 for (index = 0; index < 8; index++) {
1093 nca.package = index;
1094 ret = ncsi_xmit_cmd(&nca);
1099 /* Disable all possible packages */
1100 nca.type = NCSI_PKT_CMD_DP;
1101 for (index = 0; index < 8; index++) {
1102 nca.package = index;
1103 ret = ncsi_xmit_cmd(&nca);
1108 nd->state = ncsi_dev_state_probe_channel;
1110 case ncsi_dev_state_probe_channel:
1111 if (!ndp->active_package)
1112 ndp->active_package = list_first_or_null_rcu(
1113 &ndp->packages, struct ncsi_package, node);
1114 else if (list_is_last(&ndp->active_package->node,
1116 ndp->active_package = NULL;
1118 ndp->active_package = list_next_entry(
1119 ndp->active_package, node);
1121 /* All available packages and channels are enumerated. The
1122 * enumeration happens for once when the NCSI interface is
1123 * started. So we need continue to start the interface after
1126 * We have to choose an active channel before configuring it.
1127 * Note that we possibly don't have active channel in extreme
1130 if (!ndp->active_package) {
1131 ndp->flags |= NCSI_DEV_PROBED;
1132 if (ncsi_check_hwa(ndp))
1133 ncsi_enable_hwa(ndp);
1135 ncsi_choose_active_channel(ndp);
1139 /* Select the active package */
1140 ndp->pending_req_num = 1;
1141 nca.type = NCSI_PKT_CMD_SP;
1143 nca.package = ndp->active_package->id;
1144 nca.channel = NCSI_RESERVED_CHANNEL;
1145 ret = ncsi_xmit_cmd(&nca);
1149 nd->state = ncsi_dev_state_probe_cis;
1151 case ncsi_dev_state_probe_cis:
1152 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1154 /* Clear initial state */
1155 nca.type = NCSI_PKT_CMD_CIS;
1156 nca.package = ndp->active_package->id;
1157 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1158 nca.channel = index;
1159 ret = ncsi_xmit_cmd(&nca);
1164 nd->state = ncsi_dev_state_probe_gvi;
1166 case ncsi_dev_state_probe_gvi:
1167 case ncsi_dev_state_probe_gc:
1168 case ncsi_dev_state_probe_gls:
1169 np = ndp->active_package;
1170 ndp->pending_req_num = np->channel_num;
1172 /* Retrieve version, capability or link status */
1173 if (nd->state == ncsi_dev_state_probe_gvi)
1174 nca.type = NCSI_PKT_CMD_GVI;
1175 else if (nd->state == ncsi_dev_state_probe_gc)
1176 nca.type = NCSI_PKT_CMD_GC;
1178 nca.type = NCSI_PKT_CMD_GLS;
1180 nca.package = np->id;
1181 NCSI_FOR_EACH_CHANNEL(np, nc) {
1182 nca.channel = nc->id;
1183 ret = ncsi_xmit_cmd(&nca);
1188 if (nd->state == ncsi_dev_state_probe_gvi)
1189 nd->state = ncsi_dev_state_probe_gc;
1190 else if (nd->state == ncsi_dev_state_probe_gc)
1191 nd->state = ncsi_dev_state_probe_gls;
1193 nd->state = ncsi_dev_state_probe_dp;
1195 case ncsi_dev_state_probe_dp:
1196 ndp->pending_req_num = 1;
1198 /* Deselect the active package */
1199 nca.type = NCSI_PKT_CMD_DP;
1200 nca.package = ndp->active_package->id;
1201 nca.channel = NCSI_RESERVED_CHANNEL;
1202 ret = ncsi_xmit_cmd(&nca);
1206 /* Scan channels in next package */
1207 nd->state = ncsi_dev_state_probe_channel;
1210 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1216 ncsi_report_link(ndp, true);
1219 static void ncsi_dev_work(struct work_struct *work)
1221 struct ncsi_dev_priv *ndp = container_of(work,
1222 struct ncsi_dev_priv, work);
1223 struct ncsi_dev *nd = &ndp->ndev;
1225 switch (nd->state & ncsi_dev_state_major) {
1226 case ncsi_dev_state_probe:
1227 ncsi_probe_channel(ndp);
1229 case ncsi_dev_state_suspend:
1230 ncsi_suspend_channel(ndp);
1232 case ncsi_dev_state_config:
1233 ncsi_configure_channel(ndp);
1236 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1241 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1243 struct ncsi_channel *nc;
1245 unsigned long flags;
1247 spin_lock_irqsave(&ndp->lock, flags);
1248 nc = list_first_or_null_rcu(&ndp->channel_queue,
1249 struct ncsi_channel, link);
1251 spin_unlock_irqrestore(&ndp->lock, flags);
1255 list_del_init(&nc->link);
1256 spin_unlock_irqrestore(&ndp->lock, flags);
1258 spin_lock_irqsave(&nc->lock, flags);
1259 old_state = nc->state;
1260 nc->state = NCSI_CHANNEL_INVISIBLE;
1261 spin_unlock_irqrestore(&nc->lock, flags);
1263 ndp->active_channel = nc;
1264 ndp->active_package = nc->package;
1266 switch (old_state) {
1267 case NCSI_CHANNEL_INACTIVE:
1268 ndp->ndev.state = ncsi_dev_state_config;
1269 ncsi_configure_channel(ndp);
1271 case NCSI_CHANNEL_ACTIVE:
1272 ndp->ndev.state = ncsi_dev_state_suspend;
1273 ncsi_suspend_channel(ndp);
1276 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1277 old_state, nc->package->id, nc->id);
1278 ncsi_report_link(ndp, false);
1285 ndp->active_channel = NULL;
1286 ndp->active_package = NULL;
1287 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1288 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1289 return ncsi_choose_active_channel(ndp);
1292 ncsi_report_link(ndp, false);
1296 #if IS_ENABLED(CONFIG_IPV6)
1297 static int ncsi_inet6addr_event(struct notifier_block *this,
1298 unsigned long event, void *data)
1300 struct inet6_ifaddr *ifa = data;
1301 struct net_device *dev = ifa->idev->dev;
1302 struct ncsi_dev *nd = ncsi_find_dev(dev);
1303 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1304 struct ncsi_package *np;
1305 struct ncsi_channel *nc;
1306 struct ncsi_cmd_arg nca;
1310 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1311 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1316 action = (++ndp->inet6_addr_num) == 1;
1317 nca.type = NCSI_PKT_CMD_EGMF;
1320 action = (--ndp->inet6_addr_num == 0);
1321 nca.type = NCSI_PKT_CMD_DGMF;
1327 /* We might not have active channel or packages. The IPv6
1328 * required multicast will be enabled when active channel
1329 * or packages are chosen.
1331 np = ndp->active_package;
1332 nc = ndp->active_channel;
1333 if (!action || !np || !nc)
1336 /* We needn't enable or disable it if the function isn't supported */
1337 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1342 nca.package = np->id;
1343 nca.channel = nc->id;
1344 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1345 ret = ncsi_xmit_cmd(&nca);
1347 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1348 (event == NETDEV_UP) ? "enable" : "disable", ret);
1355 static struct notifier_block ncsi_inet6addr_notifier = {
1356 .notifier_call = ncsi_inet6addr_event,
1358 #endif /* CONFIG_IPV6 */
1360 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1362 struct ncsi_dev *nd = &ndp->ndev;
1363 struct ncsi_channel *nc;
1364 struct ncsi_package *np;
1365 unsigned long flags;
1368 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1369 NCSI_FOR_EACH_CHANNEL(np, nc) {
1370 spin_lock_irqsave(&nc->lock, flags);
1372 /* Channels may be busy, mark dirty instead of
1374 * a) not ACTIVE (configured)
1375 * b) in the channel_queue (to be configured)
1376 * c) it's ndev is in the config state
1378 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1379 if ((ndp->ndev.state & 0xff00) ==
1380 ncsi_dev_state_config ||
1381 !list_empty(&nc->link)) {
1382 netdev_printk(KERN_DEBUG, nd->dev,
1383 "ncsi: channel %p marked dirty\n",
1385 nc->reconfigure_needed = true;
1387 spin_unlock_irqrestore(&nc->lock, flags);
1391 spin_unlock_irqrestore(&nc->lock, flags);
1393 ncsi_stop_channel_monitor(nc);
1394 spin_lock_irqsave(&nc->lock, flags);
1395 nc->state = NCSI_CHANNEL_INACTIVE;
1396 spin_unlock_irqrestore(&nc->lock, flags);
1398 spin_lock_irqsave(&ndp->lock, flags);
1399 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1400 spin_unlock_irqrestore(&ndp->lock, flags);
1402 netdev_printk(KERN_DEBUG, nd->dev,
1403 "ncsi: kicked channel %p\n", nc);
1411 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1413 struct ncsi_dev_priv *ndp;
1414 unsigned int n_vids = 0;
1415 struct vlan_vid *vlan;
1416 struct ncsi_dev *nd;
1422 nd = ncsi_find_dev(dev);
1424 netdev_warn(dev, "ncsi: No net_device?\n");
1428 ndp = TO_NCSI_DEV_PRIV(nd);
1430 /* Add the VLAN id to our internal list */
1431 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1433 if (vlan->vid == vid) {
1434 netdev_printk(KERN_DEBUG, dev,
1435 "vid %u already registered\n", vid);
1439 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1441 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1442 vid, NCSI_MAX_VLAN_VIDS);
1446 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1450 vlan->proto = proto;
1452 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1454 netdev_printk(KERN_DEBUG, dev, "Added new vid %u\n", vid);
1456 found = ncsi_kick_channels(ndp) != 0;
1458 return found ? ncsi_process_next_channel(ndp) : 0;
1460 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1462 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1464 struct vlan_vid *vlan, *tmp;
1465 struct ncsi_dev_priv *ndp;
1466 struct ncsi_dev *nd;
1472 nd = ncsi_find_dev(dev);
1474 netdev_warn(dev, "ncsi: no net_device?\n");
1478 ndp = TO_NCSI_DEV_PRIV(nd);
1480 /* Remove the VLAN id from our internal list */
1481 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1482 if (vlan->vid == vid) {
1483 netdev_printk(KERN_DEBUG, dev,
1484 "vid %u found, removing\n", vid);
1485 list_del_rcu(&vlan->list);
1491 netdev_err(dev, "ncsi: vid %u wasn't registered!\n", vid);
1495 found = ncsi_kick_channels(ndp) != 0;
1497 return found ? ncsi_process_next_channel(ndp) : 0;
1499 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1501 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1502 void (*handler)(struct ncsi_dev *ndev))
1504 struct ncsi_dev_priv *ndp;
1505 struct ncsi_dev *nd;
1506 unsigned long flags;
1509 /* Check if the device has been registered or not */
1510 nd = ncsi_find_dev(dev);
1514 /* Create NCSI device */
1515 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1520 nd->state = ncsi_dev_state_registered;
1522 nd->handler = handler;
1523 ndp->pending_req_num = 0;
1524 INIT_LIST_HEAD(&ndp->channel_queue);
1525 INIT_LIST_HEAD(&ndp->vlan_vids);
1526 INIT_WORK(&ndp->work, ncsi_dev_work);
1528 /* Initialize private NCSI device */
1529 spin_lock_init(&ndp->lock);
1530 INIT_LIST_HEAD(&ndp->packages);
1531 ndp->request_id = NCSI_REQ_START_IDX;
1532 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1533 ndp->requests[i].id = i;
1534 ndp->requests[i].ndp = ndp;
1535 setup_timer(&ndp->requests[i].timer,
1536 ncsi_request_timeout,
1537 (unsigned long)&ndp->requests[i]);
1540 spin_lock_irqsave(&ncsi_dev_lock, flags);
1541 #if IS_ENABLED(CONFIG_IPV6)
1542 ndp->inet6_addr_num = 0;
1543 if (list_empty(&ncsi_dev_list))
1544 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1546 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1547 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1549 /* Register NCSI packet Rx handler */
1550 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1551 ndp->ptype.func = ncsi_rcv_rsp;
1552 ndp->ptype.dev = dev;
1553 dev_add_pack(&ndp->ptype);
1557 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1559 int ncsi_start_dev(struct ncsi_dev *nd)
1561 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1564 if (nd->state != ncsi_dev_state_registered &&
1565 nd->state != ncsi_dev_state_functional)
1568 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1569 nd->state = ncsi_dev_state_probe;
1570 schedule_work(&ndp->work);
1574 if (ndp->flags & NCSI_DEV_HWA)
1575 ret = ncsi_enable_hwa(ndp);
1577 ret = ncsi_choose_active_channel(ndp);
1581 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1583 void ncsi_stop_dev(struct ncsi_dev *nd)
1585 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1586 struct ncsi_package *np;
1587 struct ncsi_channel *nc;
1590 unsigned long flags;
1592 /* Stop the channel monitor and reset channel's state */
1593 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1594 NCSI_FOR_EACH_CHANNEL(np, nc) {
1595 ncsi_stop_channel_monitor(nc);
1597 spin_lock_irqsave(&nc->lock, flags);
1598 chained = !list_empty(&nc->link);
1599 old_state = nc->state;
1600 nc->state = NCSI_CHANNEL_INACTIVE;
1601 spin_unlock_irqrestore(&nc->lock, flags);
1603 WARN_ON_ONCE(chained ||
1604 old_state == NCSI_CHANNEL_INVISIBLE);
1608 ncsi_report_link(ndp, true);
1610 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1612 void ncsi_unregister_dev(struct ncsi_dev *nd)
1614 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1615 struct ncsi_package *np, *tmp;
1616 unsigned long flags;
1618 dev_remove_pack(&ndp->ptype);
1620 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1621 ncsi_remove_package(np);
1623 spin_lock_irqsave(&ncsi_dev_lock, flags);
1624 list_del_rcu(&ndp->node);
1625 #if IS_ENABLED(CONFIG_IPV6)
1626 if (list_empty(&ncsi_dev_list))
1627 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1629 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1633 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);