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 int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
43 struct ncsi_channel_filter *ncf;
48 ncf = nc->filters[table];
52 size = ncsi_filter_size(table);
56 spin_lock_irqsave(&nc->lock, flags);
57 bitmap = (void *)&ncf->bitmap;
59 while ((index = find_next_bit(bitmap, ncf->total, index + 1))
61 if (!memcmp(ncf->data + size * index, data, size)) {
62 spin_unlock_irqrestore(&nc->lock, flags);
66 spin_unlock_irqrestore(&nc->lock, flags);
71 int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
73 struct ncsi_channel_filter *ncf;
78 size = ncsi_filter_size(table);
82 index = ncsi_find_filter(nc, table, data);
86 ncf = nc->filters[table];
90 spin_lock_irqsave(&nc->lock, flags);
91 bitmap = (void *)&ncf->bitmap;
93 index = find_next_zero_bit(bitmap, ncf->total, 0);
94 if (index >= ncf->total) {
95 spin_unlock_irqrestore(&nc->lock, flags);
98 } while (test_and_set_bit(index, bitmap));
100 memcpy(ncf->data + size * index, data, size);
101 spin_unlock_irqrestore(&nc->lock, flags);
106 int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
108 struct ncsi_channel_filter *ncf;
113 size = ncsi_filter_size(table);
117 ncf = nc->filters[table];
118 if (!ncf || index >= ncf->total)
121 spin_lock_irqsave(&nc->lock, flags);
122 bitmap = (void *)&ncf->bitmap;
123 if (test_and_clear_bit(index, bitmap))
124 memset(ncf->data + size * index, 0, size);
125 spin_unlock_irqrestore(&nc->lock, flags);
130 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
132 struct ncsi_dev *nd = &ndp->ndev;
133 struct ncsi_package *np;
134 struct ncsi_channel *nc;
137 nd->state = ncsi_dev_state_functional;
144 NCSI_FOR_EACH_PACKAGE(ndp, np) {
145 NCSI_FOR_EACH_CHANNEL(np, nc) {
146 spin_lock_irqsave(&nc->lock, flags);
148 if (!list_empty(&nc->link) ||
149 nc->state != NCSI_CHANNEL_ACTIVE) {
150 spin_unlock_irqrestore(&nc->lock, flags);
154 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
155 spin_unlock_irqrestore(&nc->lock, flags);
160 spin_unlock_irqrestore(&nc->lock, flags);
168 static void ncsi_channel_monitor(unsigned long data)
170 struct ncsi_channel *nc = (struct ncsi_channel *)data;
171 struct ncsi_package *np = nc->package;
172 struct ncsi_dev_priv *ndp = np->ndp;
173 struct ncsi_cmd_arg nca;
174 bool enabled, chained;
175 unsigned int monitor_state;
179 spin_lock_irqsave(&nc->lock, flags);
181 chained = !list_empty(&nc->link);
182 enabled = nc->monitor.enabled;
183 monitor_state = nc->monitor.state;
184 spin_unlock_irqrestore(&nc->lock, flags);
186 if (!enabled || chained)
188 if (state != NCSI_CHANNEL_INACTIVE &&
189 state != NCSI_CHANNEL_ACTIVE)
192 switch (monitor_state) {
193 case NCSI_CHANNEL_MONITOR_START:
194 case NCSI_CHANNEL_MONITOR_RETRY:
196 nca.package = np->id;
197 nca.channel = nc->id;
198 nca.type = NCSI_PKT_CMD_GLS;
200 ret = ncsi_xmit_cmd(&nca);
202 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
208 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
211 if (!(ndp->flags & NCSI_DEV_HWA) &&
212 state == NCSI_CHANNEL_ACTIVE) {
213 ncsi_report_link(ndp, true);
214 ndp->flags |= NCSI_DEV_RESHUFFLE;
217 spin_lock_irqsave(&nc->lock, flags);
218 nc->state = NCSI_CHANNEL_INVISIBLE;
219 spin_unlock_irqrestore(&nc->lock, flags);
221 spin_lock_irqsave(&ndp->lock, flags);
222 nc->state = NCSI_CHANNEL_INACTIVE;
223 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
224 spin_unlock_irqrestore(&ndp->lock, flags);
225 ncsi_process_next_channel(ndp);
229 spin_lock_irqsave(&nc->lock, flags);
231 spin_unlock_irqrestore(&nc->lock, flags);
232 mod_timer(&nc->monitor.timer, jiffies + HZ);
235 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
239 spin_lock_irqsave(&nc->lock, flags);
240 WARN_ON_ONCE(nc->monitor.enabled);
241 nc->monitor.enabled = true;
242 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
243 spin_unlock_irqrestore(&nc->lock, flags);
245 mod_timer(&nc->monitor.timer, jiffies + HZ);
248 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
252 spin_lock_irqsave(&nc->lock, flags);
253 if (!nc->monitor.enabled) {
254 spin_unlock_irqrestore(&nc->lock, flags);
257 nc->monitor.enabled = false;
258 spin_unlock_irqrestore(&nc->lock, flags);
260 del_timer_sync(&nc->monitor.timer);
263 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
266 struct ncsi_channel *nc;
268 NCSI_FOR_EACH_CHANNEL(np, nc) {
276 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
278 struct ncsi_channel *nc, *tmp;
282 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
288 nc->state = NCSI_CHANNEL_INACTIVE;
289 nc->monitor.enabled = false;
290 setup_timer(&nc->monitor.timer,
291 ncsi_channel_monitor, (unsigned long)nc);
292 spin_lock_init(&nc->lock);
293 INIT_LIST_HEAD(&nc->link);
294 for (index = 0; index < NCSI_CAP_MAX; index++)
295 nc->caps[index].index = index;
296 for (index = 0; index < NCSI_MODE_MAX; index++)
297 nc->modes[index].index = index;
299 spin_lock_irqsave(&np->lock, flags);
300 tmp = ncsi_find_channel(np, id);
302 spin_unlock_irqrestore(&np->lock, flags);
307 list_add_tail_rcu(&nc->node, &np->channels);
309 spin_unlock_irqrestore(&np->lock, flags);
314 static void ncsi_remove_channel(struct ncsi_channel *nc)
316 struct ncsi_package *np = nc->package;
317 struct ncsi_channel_filter *ncf;
321 /* Release filters */
322 spin_lock_irqsave(&nc->lock, flags);
323 for (i = 0; i < NCSI_FILTER_MAX; i++) {
324 ncf = nc->filters[i];
328 nc->filters[i] = NULL;
332 nc->state = NCSI_CHANNEL_INACTIVE;
333 spin_unlock_irqrestore(&nc->lock, flags);
334 ncsi_stop_channel_monitor(nc);
336 /* Remove and free channel */
337 spin_lock_irqsave(&np->lock, flags);
338 list_del_rcu(&nc->node);
340 spin_unlock_irqrestore(&np->lock, flags);
345 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
348 struct ncsi_package *np;
350 NCSI_FOR_EACH_PACKAGE(ndp, np) {
358 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
361 struct ncsi_package *np, *tmp;
364 np = kzalloc(sizeof(*np), GFP_ATOMIC);
370 spin_lock_init(&np->lock);
371 INIT_LIST_HEAD(&np->channels);
373 spin_lock_irqsave(&ndp->lock, flags);
374 tmp = ncsi_find_package(ndp, id);
376 spin_unlock_irqrestore(&ndp->lock, flags);
381 list_add_tail_rcu(&np->node, &ndp->packages);
383 spin_unlock_irqrestore(&ndp->lock, flags);
388 void ncsi_remove_package(struct ncsi_package *np)
390 struct ncsi_dev_priv *ndp = np->ndp;
391 struct ncsi_channel *nc, *tmp;
394 /* Release all child channels */
395 list_for_each_entry_safe(nc, tmp, &np->channels, node)
396 ncsi_remove_channel(nc);
398 /* Remove and free package */
399 spin_lock_irqsave(&ndp->lock, flags);
400 list_del_rcu(&np->node);
402 spin_unlock_irqrestore(&ndp->lock, flags);
407 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
409 struct ncsi_package **np,
410 struct ncsi_channel **nc)
412 struct ncsi_package *p;
413 struct ncsi_channel *c;
415 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
416 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
424 /* For two consecutive NCSI commands, the packet IDs shouldn't
425 * be same. Otherwise, the bogus response might be replied. So
426 * the available IDs are allocated in round-robin fashion.
428 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
429 unsigned int req_flags)
431 struct ncsi_request *nr = NULL;
432 int i, limit = ARRAY_SIZE(ndp->requests);
435 /* Check if there is one available request until the ceiling */
436 spin_lock_irqsave(&ndp->lock, flags);
437 for (i = ndp->request_id; i < limit; i++) {
438 if (ndp->requests[i].used)
441 nr = &ndp->requests[i];
443 nr->flags = req_flags;
444 ndp->request_id = i + 1;
448 /* Fail back to check from the starting cursor */
449 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
450 if (ndp->requests[i].used)
453 nr = &ndp->requests[i];
455 nr->flags = req_flags;
456 ndp->request_id = i + 1;
461 spin_unlock_irqrestore(&ndp->lock, flags);
465 void ncsi_free_request(struct ncsi_request *nr)
467 struct ncsi_dev_priv *ndp = nr->ndp;
468 struct sk_buff *cmd, *rsp;
474 del_timer_sync(&nr->timer);
477 spin_lock_irqsave(&ndp->lock, flags);
483 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
484 spin_unlock_irqrestore(&ndp->lock, flags);
486 if (driven && cmd && --ndp->pending_req_num == 0)
487 schedule_work(&ndp->work);
489 /* Release command and response */
494 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
496 struct ncsi_dev_priv *ndp;
498 NCSI_FOR_EACH_DEV(ndp) {
499 if (ndp->ndev.dev == dev)
506 static void ncsi_request_timeout(unsigned long data)
508 struct ncsi_request *nr = (struct ncsi_request *)data;
509 struct ncsi_dev_priv *ndp = nr->ndp;
512 /* If the request already had associated response,
513 * let the response handler to release it.
515 spin_lock_irqsave(&ndp->lock, flags);
517 if (nr->rsp || !nr->cmd) {
518 spin_unlock_irqrestore(&ndp->lock, flags);
521 spin_unlock_irqrestore(&ndp->lock, flags);
523 /* Release the request */
524 ncsi_free_request(nr);
527 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
529 struct ncsi_dev *nd = &ndp->ndev;
530 struct ncsi_package *np = ndp->active_package;
531 struct ncsi_channel *nc = ndp->active_channel;
532 struct ncsi_cmd_arg nca;
537 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
539 case ncsi_dev_state_suspend:
540 nd->state = ncsi_dev_state_suspend_select;
542 case ncsi_dev_state_suspend_select:
543 ndp->pending_req_num = 1;
545 nca.type = NCSI_PKT_CMD_SP;
546 nca.package = np->id;
547 nca.channel = NCSI_RESERVED_CHANNEL;
548 if (ndp->flags & NCSI_DEV_HWA)
553 nd->state = ncsi_dev_state_suspend_dcnt;
554 ret = ncsi_xmit_cmd(&nca);
559 case ncsi_dev_state_suspend_dcnt:
560 ndp->pending_req_num = 1;
562 nca.type = NCSI_PKT_CMD_DCNT;
563 nca.package = np->id;
564 nca.channel = nc->id;
566 nd->state = ncsi_dev_state_suspend_dc;
567 ret = ncsi_xmit_cmd(&nca);
572 case ncsi_dev_state_suspend_dc:
573 ndp->pending_req_num = 1;
575 nca.type = NCSI_PKT_CMD_DC;
576 nca.package = np->id;
577 nca.channel = nc->id;
580 nd->state = ncsi_dev_state_suspend_deselect;
581 ret = ncsi_xmit_cmd(&nca);
586 case ncsi_dev_state_suspend_deselect:
587 ndp->pending_req_num = 1;
589 nca.type = NCSI_PKT_CMD_DP;
590 nca.package = np->id;
591 nca.channel = NCSI_RESERVED_CHANNEL;
593 nd->state = ncsi_dev_state_suspend_done;
594 ret = ncsi_xmit_cmd(&nca);
599 case ncsi_dev_state_suspend_done:
600 spin_lock_irqsave(&nc->lock, flags);
601 nc->state = NCSI_CHANNEL_INACTIVE;
602 spin_unlock_irqrestore(&nc->lock, flags);
603 ncsi_process_next_channel(ndp);
607 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
613 nd->state = ncsi_dev_state_functional;
616 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
618 struct ncsi_dev *nd = &ndp->ndev;
619 struct net_device *dev = nd->dev;
620 struct ncsi_package *np = ndp->active_package;
621 struct ncsi_channel *nc = ndp->active_channel;
622 struct ncsi_cmd_arg nca;
628 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
630 case ncsi_dev_state_config:
631 case ncsi_dev_state_config_sp:
632 ndp->pending_req_num = 1;
634 /* Select the specific package */
635 nca.type = NCSI_PKT_CMD_SP;
636 if (ndp->flags & NCSI_DEV_HWA)
640 nca.package = np->id;
641 nca.channel = NCSI_RESERVED_CHANNEL;
642 ret = ncsi_xmit_cmd(&nca);
646 nd->state = ncsi_dev_state_config_cis;
648 case ncsi_dev_state_config_cis:
649 ndp->pending_req_num = 1;
651 /* Clear initial state */
652 nca.type = NCSI_PKT_CMD_CIS;
653 nca.package = np->id;
654 nca.channel = nc->id;
655 ret = ncsi_xmit_cmd(&nca);
659 nd->state = ncsi_dev_state_config_sma;
661 case ncsi_dev_state_config_sma:
662 case ncsi_dev_state_config_ebf:
663 #if IS_ENABLED(CONFIG_IPV6)
664 case ncsi_dev_state_config_egmf:
666 case ncsi_dev_state_config_ecnt:
667 case ncsi_dev_state_config_ec:
668 case ncsi_dev_state_config_ae:
669 case ncsi_dev_state_config_gls:
670 ndp->pending_req_num = 1;
672 nca.package = np->id;
673 nca.channel = nc->id;
675 /* Use first entry in unicast filter table. Note that
676 * the MAC filter table starts from entry 1 instead of
679 if (nd->state == ncsi_dev_state_config_sma) {
680 nca.type = NCSI_PKT_CMD_SMA;
681 for (index = 0; index < 6; index++)
682 nca.bytes[index] = dev->dev_addr[index];
685 nd->state = ncsi_dev_state_config_ebf;
686 } else if (nd->state == ncsi_dev_state_config_ebf) {
687 nca.type = NCSI_PKT_CMD_EBF;
688 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
689 nd->state = ncsi_dev_state_config_ecnt;
690 #if IS_ENABLED(CONFIG_IPV6)
691 if (ndp->inet6_addr_num > 0 &&
692 (nc->caps[NCSI_CAP_GENERIC].cap &
693 NCSI_CAP_GENERIC_MC))
694 nd->state = ncsi_dev_state_config_egmf;
696 nd->state = ncsi_dev_state_config_ecnt;
697 } else if (nd->state == ncsi_dev_state_config_egmf) {
698 nca.type = NCSI_PKT_CMD_EGMF;
699 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
700 nd->state = ncsi_dev_state_config_ecnt;
701 #endif /* CONFIG_IPV6 */
702 } else if (nd->state == ncsi_dev_state_config_ecnt) {
703 nca.type = NCSI_PKT_CMD_ECNT;
704 nd->state = ncsi_dev_state_config_ec;
705 } else if (nd->state == ncsi_dev_state_config_ec) {
706 /* Enable AEN if it's supported */
707 nca.type = NCSI_PKT_CMD_EC;
708 nd->state = ncsi_dev_state_config_ae;
709 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
710 nd->state = ncsi_dev_state_config_gls;
711 } else if (nd->state == ncsi_dev_state_config_ae) {
712 nca.type = NCSI_PKT_CMD_AE;
714 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
715 nd->state = ncsi_dev_state_config_gls;
716 } else if (nd->state == ncsi_dev_state_config_gls) {
717 nca.type = NCSI_PKT_CMD_GLS;
718 nd->state = ncsi_dev_state_config_done;
721 ret = ncsi_xmit_cmd(&nca);
725 case ncsi_dev_state_config_done:
726 spin_lock_irqsave(&nc->lock, flags);
727 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1)
728 nc->state = NCSI_CHANNEL_ACTIVE;
730 nc->state = NCSI_CHANNEL_INACTIVE;
731 spin_unlock_irqrestore(&nc->lock, flags);
733 ncsi_start_channel_monitor(nc);
734 ncsi_process_next_channel(ndp);
737 netdev_warn(dev, "Wrong NCSI state 0x%x in config\n",
744 ncsi_report_link(ndp, true);
747 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
749 struct ncsi_package *np;
750 struct ncsi_channel *nc, *found;
751 struct ncsi_channel_mode *ncm;
754 /* The search is done once an inactive channel with up
758 NCSI_FOR_EACH_PACKAGE(ndp, np) {
759 NCSI_FOR_EACH_CHANNEL(np, nc) {
760 spin_lock_irqsave(&nc->lock, flags);
762 if (!list_empty(&nc->link) ||
763 nc->state != NCSI_CHANNEL_INACTIVE) {
764 spin_unlock_irqrestore(&nc->lock, flags);
771 ncm = &nc->modes[NCSI_MODE_LINK];
772 if (ncm->data[2] & 0x1) {
773 spin_unlock_irqrestore(&nc->lock, flags);
778 spin_unlock_irqrestore(&nc->lock, flags);
783 ncsi_report_link(ndp, true);
788 spin_lock_irqsave(&ndp->lock, flags);
789 list_add_tail_rcu(&found->link, &ndp->channel_queue);
790 spin_unlock_irqrestore(&ndp->lock, flags);
792 return ncsi_process_next_channel(ndp);
795 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
797 struct ncsi_package *np;
798 struct ncsi_channel *nc;
801 /* The hardware arbitration is disabled if any one channel
802 * doesn't support explicitly.
804 NCSI_FOR_EACH_PACKAGE(ndp, np) {
805 NCSI_FOR_EACH_CHANNEL(np, nc) {
806 cap = nc->caps[NCSI_CAP_GENERIC].cap;
807 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
808 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
809 NCSI_CAP_GENERIC_HWA_SUPPORT) {
810 ndp->flags &= ~NCSI_DEV_HWA;
816 ndp->flags |= NCSI_DEV_HWA;
820 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
822 struct ncsi_package *np;
823 struct ncsi_channel *nc;
826 /* Move all available channels to processing queue */
827 spin_lock_irqsave(&ndp->lock, flags);
828 NCSI_FOR_EACH_PACKAGE(ndp, np) {
829 NCSI_FOR_EACH_CHANNEL(np, nc) {
830 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
831 !list_empty(&nc->link));
832 ncsi_stop_channel_monitor(nc);
833 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
836 spin_unlock_irqrestore(&ndp->lock, flags);
838 /* We can have no channels in extremely case */
839 if (list_empty(&ndp->channel_queue)) {
840 ncsi_report_link(ndp, false);
844 return ncsi_process_next_channel(ndp);
847 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
849 struct ncsi_dev *nd = &ndp->ndev;
850 struct ncsi_package *np;
851 struct ncsi_channel *nc;
852 struct ncsi_cmd_arg nca;
857 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
859 case ncsi_dev_state_probe:
860 nd->state = ncsi_dev_state_probe_deselect;
862 case ncsi_dev_state_probe_deselect:
863 ndp->pending_req_num = 8;
865 /* Deselect all possible packages */
866 nca.type = NCSI_PKT_CMD_DP;
867 nca.channel = NCSI_RESERVED_CHANNEL;
868 for (index = 0; index < 8; index++) {
870 ret = ncsi_xmit_cmd(&nca);
875 nd->state = ncsi_dev_state_probe_package;
877 case ncsi_dev_state_probe_package:
878 ndp->pending_req_num = 16;
880 /* Select all possible packages */
881 nca.type = NCSI_PKT_CMD_SP;
883 nca.channel = NCSI_RESERVED_CHANNEL;
884 for (index = 0; index < 8; index++) {
886 ret = ncsi_xmit_cmd(&nca);
891 /* Disable all possible packages */
892 nca.type = NCSI_PKT_CMD_DP;
893 for (index = 0; index < 8; index++) {
895 ret = ncsi_xmit_cmd(&nca);
900 nd->state = ncsi_dev_state_probe_channel;
902 case ncsi_dev_state_probe_channel:
903 if (!ndp->active_package)
904 ndp->active_package = list_first_or_null_rcu(
905 &ndp->packages, struct ncsi_package, node);
906 else if (list_is_last(&ndp->active_package->node,
908 ndp->active_package = NULL;
910 ndp->active_package = list_next_entry(
911 ndp->active_package, node);
913 /* All available packages and channels are enumerated. The
914 * enumeration happens for once when the NCSI interface is
915 * started. So we need continue to start the interface after
918 * We have to choose an active channel before configuring it.
919 * Note that we possibly don't have active channel in extreme
922 if (!ndp->active_package) {
923 ndp->flags |= NCSI_DEV_PROBED;
924 if (ncsi_check_hwa(ndp))
925 ncsi_enable_hwa(ndp);
927 ncsi_choose_active_channel(ndp);
931 /* Select the active package */
932 ndp->pending_req_num = 1;
933 nca.type = NCSI_PKT_CMD_SP;
935 nca.package = ndp->active_package->id;
936 nca.channel = NCSI_RESERVED_CHANNEL;
937 ret = ncsi_xmit_cmd(&nca);
941 nd->state = ncsi_dev_state_probe_cis;
943 case ncsi_dev_state_probe_cis:
944 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
946 /* Clear initial state */
947 nca.type = NCSI_PKT_CMD_CIS;
948 nca.package = ndp->active_package->id;
949 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
951 ret = ncsi_xmit_cmd(&nca);
956 nd->state = ncsi_dev_state_probe_gvi;
958 case ncsi_dev_state_probe_gvi:
959 case ncsi_dev_state_probe_gc:
960 case ncsi_dev_state_probe_gls:
961 np = ndp->active_package;
962 ndp->pending_req_num = np->channel_num;
964 /* Retrieve version, capability or link status */
965 if (nd->state == ncsi_dev_state_probe_gvi)
966 nca.type = NCSI_PKT_CMD_GVI;
967 else if (nd->state == ncsi_dev_state_probe_gc)
968 nca.type = NCSI_PKT_CMD_GC;
970 nca.type = NCSI_PKT_CMD_GLS;
972 nca.package = np->id;
973 NCSI_FOR_EACH_CHANNEL(np, nc) {
974 nca.channel = nc->id;
975 ret = ncsi_xmit_cmd(&nca);
980 if (nd->state == ncsi_dev_state_probe_gvi)
981 nd->state = ncsi_dev_state_probe_gc;
982 else if (nd->state == ncsi_dev_state_probe_gc)
983 nd->state = ncsi_dev_state_probe_gls;
985 nd->state = ncsi_dev_state_probe_dp;
987 case ncsi_dev_state_probe_dp:
988 ndp->pending_req_num = 1;
990 /* Deselect the active package */
991 nca.type = NCSI_PKT_CMD_DP;
992 nca.package = ndp->active_package->id;
993 nca.channel = NCSI_RESERVED_CHANNEL;
994 ret = ncsi_xmit_cmd(&nca);
998 /* Scan channels in next package */
999 nd->state = ncsi_dev_state_probe_channel;
1002 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1008 ncsi_report_link(ndp, true);
1011 static void ncsi_dev_work(struct work_struct *work)
1013 struct ncsi_dev_priv *ndp = container_of(work,
1014 struct ncsi_dev_priv, work);
1015 struct ncsi_dev *nd = &ndp->ndev;
1017 switch (nd->state & ncsi_dev_state_major) {
1018 case ncsi_dev_state_probe:
1019 ncsi_probe_channel(ndp);
1021 case ncsi_dev_state_suspend:
1022 ncsi_suspend_channel(ndp);
1024 case ncsi_dev_state_config:
1025 ncsi_configure_channel(ndp);
1028 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1033 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1035 struct ncsi_channel *nc;
1037 unsigned long flags;
1039 spin_lock_irqsave(&ndp->lock, flags);
1040 nc = list_first_or_null_rcu(&ndp->channel_queue,
1041 struct ncsi_channel, link);
1043 spin_unlock_irqrestore(&ndp->lock, flags);
1047 list_del_init(&nc->link);
1048 spin_unlock_irqrestore(&ndp->lock, flags);
1050 spin_lock_irqsave(&nc->lock, flags);
1051 old_state = nc->state;
1052 nc->state = NCSI_CHANNEL_INVISIBLE;
1053 spin_unlock_irqrestore(&nc->lock, flags);
1055 ndp->active_channel = nc;
1056 ndp->active_package = nc->package;
1058 switch (old_state) {
1059 case NCSI_CHANNEL_INACTIVE:
1060 ndp->ndev.state = ncsi_dev_state_config;
1061 ncsi_configure_channel(ndp);
1063 case NCSI_CHANNEL_ACTIVE:
1064 ndp->ndev.state = ncsi_dev_state_suspend;
1065 ncsi_suspend_channel(ndp);
1068 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1069 old_state, nc->package->id, nc->id);
1070 ncsi_report_link(ndp, false);
1077 ndp->active_channel = NULL;
1078 ndp->active_package = NULL;
1079 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1080 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1081 return ncsi_choose_active_channel(ndp);
1084 ncsi_report_link(ndp, false);
1088 #if IS_ENABLED(CONFIG_IPV6)
1089 static int ncsi_inet6addr_event(struct notifier_block *this,
1090 unsigned long event, void *data)
1092 struct inet6_ifaddr *ifa = data;
1093 struct net_device *dev = ifa->idev->dev;
1094 struct ncsi_dev *nd = ncsi_find_dev(dev);
1095 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1096 struct ncsi_package *np;
1097 struct ncsi_channel *nc;
1098 struct ncsi_cmd_arg nca;
1102 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1103 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1108 action = (++ndp->inet6_addr_num) == 1;
1109 nca.type = NCSI_PKT_CMD_EGMF;
1112 action = (--ndp->inet6_addr_num == 0);
1113 nca.type = NCSI_PKT_CMD_DGMF;
1119 /* We might not have active channel or packages. The IPv6
1120 * required multicast will be enabled when active channel
1121 * or packages are chosen.
1123 np = ndp->active_package;
1124 nc = ndp->active_channel;
1125 if (!action || !np || !nc)
1128 /* We needn't enable or disable it if the function isn't supported */
1129 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1134 nca.package = np->id;
1135 nca.channel = nc->id;
1136 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1137 ret = ncsi_xmit_cmd(&nca);
1139 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1140 (event == NETDEV_UP) ? "enable" : "disable", ret);
1147 static struct notifier_block ncsi_inet6addr_notifier = {
1148 .notifier_call = ncsi_inet6addr_event,
1150 #endif /* CONFIG_IPV6 */
1152 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1153 void (*handler)(struct ncsi_dev *ndev))
1155 struct ncsi_dev_priv *ndp;
1156 struct ncsi_dev *nd;
1157 unsigned long flags;
1160 /* Check if the device has been registered or not */
1161 nd = ncsi_find_dev(dev);
1165 /* Create NCSI device */
1166 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1171 nd->state = ncsi_dev_state_registered;
1173 nd->handler = handler;
1174 ndp->pending_req_num = 0;
1175 INIT_LIST_HEAD(&ndp->channel_queue);
1176 INIT_WORK(&ndp->work, ncsi_dev_work);
1178 /* Initialize private NCSI device */
1179 spin_lock_init(&ndp->lock);
1180 INIT_LIST_HEAD(&ndp->packages);
1181 ndp->request_id = NCSI_REQ_START_IDX;
1182 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1183 ndp->requests[i].id = i;
1184 ndp->requests[i].ndp = ndp;
1185 setup_timer(&ndp->requests[i].timer,
1186 ncsi_request_timeout,
1187 (unsigned long)&ndp->requests[i]);
1190 spin_lock_irqsave(&ncsi_dev_lock, flags);
1191 #if IS_ENABLED(CONFIG_IPV6)
1192 ndp->inet6_addr_num = 0;
1193 if (list_empty(&ncsi_dev_list))
1194 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1196 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1197 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1199 /* Register NCSI packet Rx handler */
1200 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1201 ndp->ptype.func = ncsi_rcv_rsp;
1202 ndp->ptype.dev = dev;
1203 dev_add_pack(&ndp->ptype);
1207 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1209 int ncsi_start_dev(struct ncsi_dev *nd)
1211 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1214 if (nd->state != ncsi_dev_state_registered &&
1215 nd->state != ncsi_dev_state_functional)
1218 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1219 nd->state = ncsi_dev_state_probe;
1220 schedule_work(&ndp->work);
1224 if (ndp->flags & NCSI_DEV_HWA)
1225 ret = ncsi_enable_hwa(ndp);
1227 ret = ncsi_choose_active_channel(ndp);
1231 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1233 void ncsi_stop_dev(struct ncsi_dev *nd)
1235 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1236 struct ncsi_package *np;
1237 struct ncsi_channel *nc;
1240 unsigned long flags;
1242 /* Stop the channel monitor and reset channel's state */
1243 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1244 NCSI_FOR_EACH_CHANNEL(np, nc) {
1245 ncsi_stop_channel_monitor(nc);
1247 spin_lock_irqsave(&nc->lock, flags);
1248 chained = !list_empty(&nc->link);
1249 old_state = nc->state;
1250 nc->state = NCSI_CHANNEL_INACTIVE;
1251 spin_unlock_irqrestore(&nc->lock, flags);
1253 WARN_ON_ONCE(chained ||
1254 old_state == NCSI_CHANNEL_INVISIBLE);
1258 ncsi_report_link(ndp, true);
1260 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1262 void ncsi_unregister_dev(struct ncsi_dev *nd)
1264 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1265 struct ncsi_package *np, *tmp;
1266 unsigned long flags;
1268 dev_remove_pack(&ndp->ptype);
1270 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1271 ncsi_remove_package(np);
1273 spin_lock_irqsave(&ncsi_dev_lock, flags);
1274 list_del_rcu(&ndp->node);
1275 #if IS_ENABLED(CONFIG_IPV6)
1276 if (list_empty(&ncsi_dev_list))
1277 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1279 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1283 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);