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(unsigned long data)
70 struct ncsi_channel *nc = (struct ncsi_channel *)data;
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 setup_timer(&nc->monitor.timer,
204 ncsi_channel_monitor, (unsigned long)nc);
205 spin_lock_init(&nc->lock);
206 INIT_LIST_HEAD(&nc->link);
207 for (index = 0; index < NCSI_CAP_MAX; index++)
208 nc->caps[index].index = index;
209 for (index = 0; index < NCSI_MODE_MAX; index++)
210 nc->modes[index].index = index;
212 spin_lock_irqsave(&np->lock, flags);
213 tmp = ncsi_find_channel(np, id);
215 spin_unlock_irqrestore(&np->lock, flags);
220 list_add_tail_rcu(&nc->node, &np->channels);
222 spin_unlock_irqrestore(&np->lock, flags);
227 static void ncsi_remove_channel(struct ncsi_channel *nc)
229 struct ncsi_package *np = nc->package;
232 spin_lock_irqsave(&nc->lock, flags);
234 /* Release filters */
235 kfree(nc->mac_filter.addrs);
236 kfree(nc->vlan_filter.vids);
238 nc->state = NCSI_CHANNEL_INACTIVE;
239 spin_unlock_irqrestore(&nc->lock, flags);
240 ncsi_stop_channel_monitor(nc);
242 /* Remove and free channel */
243 spin_lock_irqsave(&np->lock, flags);
244 list_del_rcu(&nc->node);
246 spin_unlock_irqrestore(&np->lock, flags);
251 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
254 struct ncsi_package *np;
256 NCSI_FOR_EACH_PACKAGE(ndp, np) {
264 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
267 struct ncsi_package *np, *tmp;
270 np = kzalloc(sizeof(*np), GFP_ATOMIC);
276 spin_lock_init(&np->lock);
277 INIT_LIST_HEAD(&np->channels);
279 spin_lock_irqsave(&ndp->lock, flags);
280 tmp = ncsi_find_package(ndp, id);
282 spin_unlock_irqrestore(&ndp->lock, flags);
287 list_add_tail_rcu(&np->node, &ndp->packages);
289 spin_unlock_irqrestore(&ndp->lock, flags);
294 void ncsi_remove_package(struct ncsi_package *np)
296 struct ncsi_dev_priv *ndp = np->ndp;
297 struct ncsi_channel *nc, *tmp;
300 /* Release all child channels */
301 list_for_each_entry_safe(nc, tmp, &np->channels, node)
302 ncsi_remove_channel(nc);
304 /* Remove and free package */
305 spin_lock_irqsave(&ndp->lock, flags);
306 list_del_rcu(&np->node);
308 spin_unlock_irqrestore(&ndp->lock, flags);
313 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
315 struct ncsi_package **np,
316 struct ncsi_channel **nc)
318 struct ncsi_package *p;
319 struct ncsi_channel *c;
321 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
322 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
330 /* For two consecutive NCSI commands, the packet IDs shouldn't
331 * be same. Otherwise, the bogus response might be replied. So
332 * the available IDs are allocated in round-robin fashion.
334 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
335 unsigned int req_flags)
337 struct ncsi_request *nr = NULL;
338 int i, limit = ARRAY_SIZE(ndp->requests);
341 /* Check if there is one available request until the ceiling */
342 spin_lock_irqsave(&ndp->lock, flags);
343 for (i = ndp->request_id; i < limit; i++) {
344 if (ndp->requests[i].used)
347 nr = &ndp->requests[i];
349 nr->flags = req_flags;
350 ndp->request_id = i + 1;
354 /* Fail back to check from the starting cursor */
355 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
356 if (ndp->requests[i].used)
359 nr = &ndp->requests[i];
361 nr->flags = req_flags;
362 ndp->request_id = i + 1;
367 spin_unlock_irqrestore(&ndp->lock, flags);
371 void ncsi_free_request(struct ncsi_request *nr)
373 struct ncsi_dev_priv *ndp = nr->ndp;
374 struct sk_buff *cmd, *rsp;
380 del_timer_sync(&nr->timer);
383 spin_lock_irqsave(&ndp->lock, flags);
389 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
390 spin_unlock_irqrestore(&ndp->lock, flags);
392 if (driven && cmd && --ndp->pending_req_num == 0)
393 schedule_work(&ndp->work);
395 /* Release command and response */
400 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
402 struct ncsi_dev_priv *ndp;
404 NCSI_FOR_EACH_DEV(ndp) {
405 if (ndp->ndev.dev == dev)
412 static void ncsi_request_timeout(unsigned long data)
414 struct ncsi_request *nr = (struct ncsi_request *)data;
415 struct ncsi_dev_priv *ndp = nr->ndp;
418 /* If the request already had associated response,
419 * let the response handler to release it.
421 spin_lock_irqsave(&ndp->lock, flags);
423 if (nr->rsp || !nr->cmd) {
424 spin_unlock_irqrestore(&ndp->lock, flags);
427 spin_unlock_irqrestore(&ndp->lock, flags);
429 /* Release the request */
430 ncsi_free_request(nr);
433 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
435 struct ncsi_dev *nd = &ndp->ndev;
436 struct ncsi_package *np = ndp->active_package;
437 struct ncsi_channel *nc = ndp->active_channel;
438 struct ncsi_cmd_arg nca;
443 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
445 case ncsi_dev_state_suspend:
446 nd->state = ncsi_dev_state_suspend_select;
448 case ncsi_dev_state_suspend_select:
449 ndp->pending_req_num = 1;
451 nca.type = NCSI_PKT_CMD_SP;
452 nca.package = np->id;
453 nca.channel = NCSI_RESERVED_CHANNEL;
454 if (ndp->flags & NCSI_DEV_HWA)
459 /* To retrieve the last link states of channels in current
460 * package when current active channel needs fail over to
461 * another one. It means we will possibly select another
462 * channel as next active one. The link states of channels
463 * are most important factor of the selection. So we need
464 * accurate link states. Unfortunately, the link states on
465 * inactive channels can't be updated with LSC AEN in time.
467 if (ndp->flags & NCSI_DEV_RESHUFFLE)
468 nd->state = ncsi_dev_state_suspend_gls;
470 nd->state = ncsi_dev_state_suspend_dcnt;
471 ret = ncsi_xmit_cmd(&nca);
476 case ncsi_dev_state_suspend_gls:
477 ndp->pending_req_num = np->channel_num;
479 nca.type = NCSI_PKT_CMD_GLS;
480 nca.package = np->id;
482 nd->state = ncsi_dev_state_suspend_dcnt;
483 NCSI_FOR_EACH_CHANNEL(np, nc) {
484 nca.channel = nc->id;
485 ret = ncsi_xmit_cmd(&nca);
491 case ncsi_dev_state_suspend_dcnt:
492 ndp->pending_req_num = 1;
494 nca.type = NCSI_PKT_CMD_DCNT;
495 nca.package = np->id;
496 nca.channel = nc->id;
498 nd->state = ncsi_dev_state_suspend_dc;
499 ret = ncsi_xmit_cmd(&nca);
504 case ncsi_dev_state_suspend_dc:
505 ndp->pending_req_num = 1;
507 nca.type = NCSI_PKT_CMD_DC;
508 nca.package = np->id;
509 nca.channel = nc->id;
512 nd->state = ncsi_dev_state_suspend_deselect;
513 ret = ncsi_xmit_cmd(&nca);
518 case ncsi_dev_state_suspend_deselect:
519 ndp->pending_req_num = 1;
521 nca.type = NCSI_PKT_CMD_DP;
522 nca.package = np->id;
523 nca.channel = NCSI_RESERVED_CHANNEL;
525 nd->state = ncsi_dev_state_suspend_done;
526 ret = ncsi_xmit_cmd(&nca);
531 case ncsi_dev_state_suspend_done:
532 spin_lock_irqsave(&nc->lock, flags);
533 nc->state = NCSI_CHANNEL_INACTIVE;
534 spin_unlock_irqrestore(&nc->lock, flags);
535 ncsi_process_next_channel(ndp);
539 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
545 nd->state = ncsi_dev_state_functional;
548 /* Check the VLAN filter bitmap for a set filter, and construct a
549 * "Set VLAN Filter - Disable" packet if found.
551 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
552 struct ncsi_cmd_arg *nca)
554 struct ncsi_channel_vlan_filter *ncf;
560 ncf = &nc->vlan_filter;
561 bitmap = &ncf->bitmap;
563 spin_lock_irqsave(&nc->lock, flags);
564 index = find_next_bit(bitmap, ncf->n_vids, 0);
565 if (index >= ncf->n_vids) {
566 spin_unlock_irqrestore(&nc->lock, flags);
569 vid = ncf->vids[index];
571 clear_bit(index, bitmap);
572 ncf->vids[index] = 0;
573 spin_unlock_irqrestore(&nc->lock, flags);
575 nca->type = NCSI_PKT_CMD_SVF;
577 /* HW filter index starts at 1 */
578 nca->bytes[6] = index + 1;
579 nca->bytes[7] = 0x00;
583 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
586 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
587 struct ncsi_cmd_arg *nca)
589 struct ncsi_channel_vlan_filter *ncf;
590 struct vlan_vid *vlan = NULL;
596 if (list_empty(&ndp->vlan_vids))
599 ncf = &nc->vlan_filter;
600 bitmap = &ncf->bitmap;
602 spin_lock_irqsave(&nc->lock, flags);
605 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
607 for (i = 0; i < ncf->n_vids; i++)
608 if (ncf->vids[i] == vid) {
618 /* No VLAN ID is not set */
619 spin_unlock_irqrestore(&nc->lock, flags);
623 index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
624 if (index < 0 || index >= ncf->n_vids) {
625 netdev_err(ndp->ndev.dev,
626 "Channel %u already has all VLAN filters set\n",
628 spin_unlock_irqrestore(&nc->lock, flags);
632 ncf->vids[index] = vid;
633 set_bit(index, bitmap);
634 spin_unlock_irqrestore(&nc->lock, flags);
636 nca->type = NCSI_PKT_CMD_SVF;
638 /* HW filter index starts at 1 */
639 nca->bytes[6] = index + 1;
640 nca->bytes[7] = 0x01;
645 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
647 struct ncsi_dev *nd = &ndp->ndev;
648 struct net_device *dev = nd->dev;
649 struct ncsi_package *np = ndp->active_package;
650 struct ncsi_channel *nc = ndp->active_channel;
651 struct ncsi_channel *hot_nc = NULL;
652 struct ncsi_cmd_arg nca;
658 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
660 case ncsi_dev_state_config:
661 case ncsi_dev_state_config_sp:
662 ndp->pending_req_num = 1;
664 /* Select the specific package */
665 nca.type = NCSI_PKT_CMD_SP;
666 if (ndp->flags & NCSI_DEV_HWA)
670 nca.package = np->id;
671 nca.channel = NCSI_RESERVED_CHANNEL;
672 ret = ncsi_xmit_cmd(&nca);
674 netdev_err(ndp->ndev.dev,
675 "NCSI: Failed to transmit CMD_SP\n");
679 nd->state = ncsi_dev_state_config_cis;
681 case ncsi_dev_state_config_cis:
682 ndp->pending_req_num = 1;
684 /* Clear initial state */
685 nca.type = NCSI_PKT_CMD_CIS;
686 nca.package = np->id;
687 nca.channel = nc->id;
688 ret = ncsi_xmit_cmd(&nca);
690 netdev_err(ndp->ndev.dev,
691 "NCSI: Failed to transmit CMD_CIS\n");
695 nd->state = ncsi_dev_state_config_clear_vids;
697 case ncsi_dev_state_config_clear_vids:
698 case ncsi_dev_state_config_svf:
699 case ncsi_dev_state_config_ev:
700 case ncsi_dev_state_config_sma:
701 case ncsi_dev_state_config_ebf:
702 #if IS_ENABLED(CONFIG_IPV6)
703 case ncsi_dev_state_config_egmf:
705 case ncsi_dev_state_config_ecnt:
706 case ncsi_dev_state_config_ec:
707 case ncsi_dev_state_config_ae:
708 case ncsi_dev_state_config_gls:
709 ndp->pending_req_num = 1;
711 nca.package = np->id;
712 nca.channel = nc->id;
714 /* Clear any active filters on the channel before setting */
715 if (nd->state == ncsi_dev_state_config_clear_vids) {
716 ret = clear_one_vid(ndp, nc, &nca);
718 nd->state = ncsi_dev_state_config_svf;
719 schedule_work(&ndp->work);
723 nd->state = ncsi_dev_state_config_clear_vids;
724 /* Add known VLAN tags to the filter */
725 } else if (nd->state == ncsi_dev_state_config_svf) {
726 ret = set_one_vid(ndp, nc, &nca);
728 nd->state = ncsi_dev_state_config_ev;
729 schedule_work(&ndp->work);
733 nd->state = ncsi_dev_state_config_svf;
734 /* Enable/Disable the VLAN filter */
735 } else if (nd->state == ncsi_dev_state_config_ev) {
736 if (list_empty(&ndp->vlan_vids)) {
737 nca.type = NCSI_PKT_CMD_DV;
739 nca.type = NCSI_PKT_CMD_EV;
740 nca.bytes[3] = NCSI_CAP_VLAN_NO;
742 nd->state = ncsi_dev_state_config_sma;
743 } else if (nd->state == ncsi_dev_state_config_sma) {
744 /* Use first entry in unicast filter table. Note that
745 * the MAC filter table starts from entry 1 instead of
748 nca.type = NCSI_PKT_CMD_SMA;
749 for (index = 0; index < 6; index++)
750 nca.bytes[index] = dev->dev_addr[index];
753 nd->state = ncsi_dev_state_config_ebf;
754 } else if (nd->state == ncsi_dev_state_config_ebf) {
755 nca.type = NCSI_PKT_CMD_EBF;
756 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
757 nd->state = ncsi_dev_state_config_ecnt;
758 #if IS_ENABLED(CONFIG_IPV6)
759 if (ndp->inet6_addr_num > 0 &&
760 (nc->caps[NCSI_CAP_GENERIC].cap &
761 NCSI_CAP_GENERIC_MC))
762 nd->state = ncsi_dev_state_config_egmf;
764 nd->state = ncsi_dev_state_config_ecnt;
765 } else if (nd->state == ncsi_dev_state_config_egmf) {
766 nca.type = NCSI_PKT_CMD_EGMF;
767 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
768 nd->state = ncsi_dev_state_config_ecnt;
769 #endif /* CONFIG_IPV6 */
770 } else if (nd->state == ncsi_dev_state_config_ecnt) {
771 nca.type = NCSI_PKT_CMD_ECNT;
772 nd->state = ncsi_dev_state_config_ec;
773 } else if (nd->state == ncsi_dev_state_config_ec) {
774 /* Enable AEN if it's supported */
775 nca.type = NCSI_PKT_CMD_EC;
776 nd->state = ncsi_dev_state_config_ae;
777 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
778 nd->state = ncsi_dev_state_config_gls;
779 } else if (nd->state == ncsi_dev_state_config_ae) {
780 nca.type = NCSI_PKT_CMD_AE;
782 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
783 nd->state = ncsi_dev_state_config_gls;
784 } else if (nd->state == ncsi_dev_state_config_gls) {
785 nca.type = NCSI_PKT_CMD_GLS;
786 nd->state = ncsi_dev_state_config_done;
789 ret = ncsi_xmit_cmd(&nca);
791 netdev_err(ndp->ndev.dev,
792 "NCSI: Failed to transmit CMD %x\n",
797 case ncsi_dev_state_config_done:
798 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
799 "NCSI: channel %u config done\n", nc->id);
800 spin_lock_irqsave(&nc->lock, flags);
801 if (nc->reconfigure_needed) {
802 /* This channel's configuration has been updated
803 * part-way during the config state - start the
804 * channel configuration over
806 nc->reconfigure_needed = false;
807 nc->state = NCSI_CHANNEL_INACTIVE;
808 spin_unlock_irqrestore(&nc->lock, flags);
810 spin_lock_irqsave(&ndp->lock, flags);
811 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
812 spin_unlock_irqrestore(&ndp->lock, flags);
814 netdev_printk(KERN_DEBUG, dev,
815 "Dirty NCSI channel state reset\n");
816 ncsi_process_next_channel(ndp);
820 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
822 nc->state = NCSI_CHANNEL_ACTIVE;
825 nc->state = NCSI_CHANNEL_INACTIVE;
826 netdev_warn(ndp->ndev.dev,
827 "NCSI: channel %u link down after config\n",
830 spin_unlock_irqrestore(&nc->lock, flags);
832 /* Update the hot channel */
833 spin_lock_irqsave(&ndp->lock, flags);
834 ndp->hot_channel = hot_nc;
835 spin_unlock_irqrestore(&ndp->lock, flags);
837 ncsi_start_channel_monitor(nc);
838 ncsi_process_next_channel(ndp);
841 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
848 ncsi_report_link(ndp, true);
851 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
853 struct ncsi_package *np, *force_package;
854 struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
855 struct ncsi_channel_mode *ncm;
858 spin_lock_irqsave(&ndp->lock, flags);
859 hot_nc = ndp->hot_channel;
860 force_channel = ndp->force_channel;
861 force_package = ndp->force_package;
862 spin_unlock_irqrestore(&ndp->lock, flags);
864 /* Force a specific channel whether or not it has link if we have been
865 * configured to do so
867 if (force_package && force_channel) {
868 found = force_channel;
869 ncm = &found->modes[NCSI_MODE_LINK];
870 if (!(ncm->data[2] & 0x1))
871 netdev_info(ndp->ndev.dev,
872 "NCSI: Channel %u forced, but it is link down\n",
877 /* The search is done once an inactive channel with up
881 NCSI_FOR_EACH_PACKAGE(ndp, np) {
882 if (ndp->force_package && np != ndp->force_package)
884 NCSI_FOR_EACH_CHANNEL(np, nc) {
885 spin_lock_irqsave(&nc->lock, flags);
887 if (!list_empty(&nc->link) ||
888 nc->state != NCSI_CHANNEL_INACTIVE) {
889 spin_unlock_irqrestore(&nc->lock, flags);
899 ncm = &nc->modes[NCSI_MODE_LINK];
900 if (ncm->data[2] & 0x1) {
901 spin_unlock_irqrestore(&nc->lock, flags);
906 spin_unlock_irqrestore(&nc->lock, flags);
911 netdev_warn(ndp->ndev.dev,
912 "NCSI: No channel found with link\n");
913 ncsi_report_link(ndp, true);
917 ncm = &found->modes[NCSI_MODE_LINK];
918 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
919 "NCSI: Channel %u added to queue (link %s)\n",
920 found->id, ncm->data[2] & 0x1 ? "up" : "down");
923 spin_lock_irqsave(&ndp->lock, flags);
924 list_add_tail_rcu(&found->link, &ndp->channel_queue);
925 spin_unlock_irqrestore(&ndp->lock, flags);
927 return ncsi_process_next_channel(ndp);
930 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
932 struct ncsi_package *np;
933 struct ncsi_channel *nc;
935 bool has_channel = false;
937 /* The hardware arbitration is disabled if any one channel
938 * doesn't support explicitly.
940 NCSI_FOR_EACH_PACKAGE(ndp, np) {
941 NCSI_FOR_EACH_CHANNEL(np, nc) {
944 cap = nc->caps[NCSI_CAP_GENERIC].cap;
945 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
946 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
947 NCSI_CAP_GENERIC_HWA_SUPPORT) {
948 ndp->flags &= ~NCSI_DEV_HWA;
955 ndp->flags |= NCSI_DEV_HWA;
959 ndp->flags &= ~NCSI_DEV_HWA;
963 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
965 struct ncsi_package *np;
966 struct ncsi_channel *nc;
969 /* Move all available channels to processing queue */
970 spin_lock_irqsave(&ndp->lock, flags);
971 NCSI_FOR_EACH_PACKAGE(ndp, np) {
972 NCSI_FOR_EACH_CHANNEL(np, nc) {
973 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
974 !list_empty(&nc->link));
975 ncsi_stop_channel_monitor(nc);
976 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
979 spin_unlock_irqrestore(&ndp->lock, flags);
981 /* We can have no channels in extremely case */
982 if (list_empty(&ndp->channel_queue)) {
983 netdev_err(ndp->ndev.dev,
984 "NCSI: No available channels for HWA\n");
985 ncsi_report_link(ndp, false);
989 return ncsi_process_next_channel(ndp);
992 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
994 struct ncsi_dev *nd = &ndp->ndev;
995 struct ncsi_package *np;
996 struct ncsi_channel *nc;
997 struct ncsi_cmd_arg nca;
1002 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1003 switch (nd->state) {
1004 case ncsi_dev_state_probe:
1005 nd->state = ncsi_dev_state_probe_deselect;
1007 case ncsi_dev_state_probe_deselect:
1008 ndp->pending_req_num = 8;
1010 /* Deselect all possible packages */
1011 nca.type = NCSI_PKT_CMD_DP;
1012 nca.channel = NCSI_RESERVED_CHANNEL;
1013 for (index = 0; index < 8; index++) {
1014 nca.package = index;
1015 ret = ncsi_xmit_cmd(&nca);
1020 nd->state = ncsi_dev_state_probe_package;
1022 case ncsi_dev_state_probe_package:
1023 ndp->pending_req_num = 16;
1025 /* Select all possible packages */
1026 nca.type = NCSI_PKT_CMD_SP;
1028 nca.channel = NCSI_RESERVED_CHANNEL;
1029 for (index = 0; index < 8; index++) {
1030 nca.package = index;
1031 ret = ncsi_xmit_cmd(&nca);
1036 /* Disable all possible packages */
1037 nca.type = NCSI_PKT_CMD_DP;
1038 for (index = 0; index < 8; index++) {
1039 nca.package = index;
1040 ret = ncsi_xmit_cmd(&nca);
1045 nd->state = ncsi_dev_state_probe_channel;
1047 case ncsi_dev_state_probe_channel:
1048 if (!ndp->active_package)
1049 ndp->active_package = list_first_or_null_rcu(
1050 &ndp->packages, struct ncsi_package, node);
1051 else if (list_is_last(&ndp->active_package->node,
1053 ndp->active_package = NULL;
1055 ndp->active_package = list_next_entry(
1056 ndp->active_package, node);
1058 /* All available packages and channels are enumerated. The
1059 * enumeration happens for once when the NCSI interface is
1060 * started. So we need continue to start the interface after
1063 * We have to choose an active channel before configuring it.
1064 * Note that we possibly don't have active channel in extreme
1067 if (!ndp->active_package) {
1068 ndp->flags |= NCSI_DEV_PROBED;
1069 if (ncsi_check_hwa(ndp))
1070 ncsi_enable_hwa(ndp);
1072 ncsi_choose_active_channel(ndp);
1076 /* Select the active package */
1077 ndp->pending_req_num = 1;
1078 nca.type = NCSI_PKT_CMD_SP;
1080 nca.package = ndp->active_package->id;
1081 nca.channel = NCSI_RESERVED_CHANNEL;
1082 ret = ncsi_xmit_cmd(&nca);
1086 nd->state = ncsi_dev_state_probe_cis;
1088 case ncsi_dev_state_probe_cis:
1089 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1091 /* Clear initial state */
1092 nca.type = NCSI_PKT_CMD_CIS;
1093 nca.package = ndp->active_package->id;
1094 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1095 nca.channel = index;
1096 ret = ncsi_xmit_cmd(&nca);
1101 nd->state = ncsi_dev_state_probe_gvi;
1103 case ncsi_dev_state_probe_gvi:
1104 case ncsi_dev_state_probe_gc:
1105 case ncsi_dev_state_probe_gls:
1106 np = ndp->active_package;
1107 ndp->pending_req_num = np->channel_num;
1109 /* Retrieve version, capability or link status */
1110 if (nd->state == ncsi_dev_state_probe_gvi)
1111 nca.type = NCSI_PKT_CMD_GVI;
1112 else if (nd->state == ncsi_dev_state_probe_gc)
1113 nca.type = NCSI_PKT_CMD_GC;
1115 nca.type = NCSI_PKT_CMD_GLS;
1117 nca.package = np->id;
1118 NCSI_FOR_EACH_CHANNEL(np, nc) {
1119 nca.channel = nc->id;
1120 ret = ncsi_xmit_cmd(&nca);
1125 if (nd->state == ncsi_dev_state_probe_gvi)
1126 nd->state = ncsi_dev_state_probe_gc;
1127 else if (nd->state == ncsi_dev_state_probe_gc)
1128 nd->state = ncsi_dev_state_probe_gls;
1130 nd->state = ncsi_dev_state_probe_dp;
1132 case ncsi_dev_state_probe_dp:
1133 ndp->pending_req_num = 1;
1135 /* Deselect the active package */
1136 nca.type = NCSI_PKT_CMD_DP;
1137 nca.package = ndp->active_package->id;
1138 nca.channel = NCSI_RESERVED_CHANNEL;
1139 ret = ncsi_xmit_cmd(&nca);
1143 /* Scan channels in next package */
1144 nd->state = ncsi_dev_state_probe_channel;
1147 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1153 netdev_err(ndp->ndev.dev,
1154 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1156 ncsi_report_link(ndp, true);
1159 static void ncsi_dev_work(struct work_struct *work)
1161 struct ncsi_dev_priv *ndp = container_of(work,
1162 struct ncsi_dev_priv, work);
1163 struct ncsi_dev *nd = &ndp->ndev;
1165 switch (nd->state & ncsi_dev_state_major) {
1166 case ncsi_dev_state_probe:
1167 ncsi_probe_channel(ndp);
1169 case ncsi_dev_state_suspend:
1170 ncsi_suspend_channel(ndp);
1172 case ncsi_dev_state_config:
1173 ncsi_configure_channel(ndp);
1176 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1181 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1183 struct ncsi_channel *nc;
1185 unsigned long flags;
1187 spin_lock_irqsave(&ndp->lock, flags);
1188 nc = list_first_or_null_rcu(&ndp->channel_queue,
1189 struct ncsi_channel, link);
1191 spin_unlock_irqrestore(&ndp->lock, flags);
1195 list_del_init(&nc->link);
1196 spin_unlock_irqrestore(&ndp->lock, flags);
1198 spin_lock_irqsave(&nc->lock, flags);
1199 old_state = nc->state;
1200 nc->state = NCSI_CHANNEL_INVISIBLE;
1201 spin_unlock_irqrestore(&nc->lock, flags);
1203 ndp->active_channel = nc;
1204 ndp->active_package = nc->package;
1206 switch (old_state) {
1207 case NCSI_CHANNEL_INACTIVE:
1208 ndp->ndev.state = ncsi_dev_state_config;
1209 netdev_info(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1211 ncsi_configure_channel(ndp);
1213 case NCSI_CHANNEL_ACTIVE:
1214 ndp->ndev.state = ncsi_dev_state_suspend;
1215 netdev_info(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1217 ncsi_suspend_channel(ndp);
1220 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1221 old_state, nc->package->id, nc->id);
1222 ncsi_report_link(ndp, false);
1229 ndp->active_channel = NULL;
1230 ndp->active_package = NULL;
1231 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1232 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1233 return ncsi_choose_active_channel(ndp);
1236 netdev_printk(KERN_DEBUG, ndp->ndev.dev,
1237 "NCSI: No more channels to process\n");
1238 ncsi_report_link(ndp, false);
1242 #if IS_ENABLED(CONFIG_IPV6)
1243 static int ncsi_inet6addr_event(struct notifier_block *this,
1244 unsigned long event, void *data)
1246 struct inet6_ifaddr *ifa = data;
1247 struct net_device *dev = ifa->idev->dev;
1248 struct ncsi_dev *nd = ncsi_find_dev(dev);
1249 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1250 struct ncsi_package *np;
1251 struct ncsi_channel *nc;
1252 struct ncsi_cmd_arg nca;
1256 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1257 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1262 action = (++ndp->inet6_addr_num) == 1;
1263 nca.type = NCSI_PKT_CMD_EGMF;
1266 action = (--ndp->inet6_addr_num == 0);
1267 nca.type = NCSI_PKT_CMD_DGMF;
1273 /* We might not have active channel or packages. The IPv6
1274 * required multicast will be enabled when active channel
1275 * or packages are chosen.
1277 np = ndp->active_package;
1278 nc = ndp->active_channel;
1279 if (!action || !np || !nc)
1282 /* We needn't enable or disable it if the function isn't supported */
1283 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1288 nca.package = np->id;
1289 nca.channel = nc->id;
1290 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1291 ret = ncsi_xmit_cmd(&nca);
1293 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1294 (event == NETDEV_UP) ? "enable" : "disable", ret);
1301 static struct notifier_block ncsi_inet6addr_notifier = {
1302 .notifier_call = ncsi_inet6addr_event,
1304 #endif /* CONFIG_IPV6 */
1306 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1308 struct ncsi_dev *nd = &ndp->ndev;
1309 struct ncsi_channel *nc;
1310 struct ncsi_package *np;
1311 unsigned long flags;
1314 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1315 NCSI_FOR_EACH_CHANNEL(np, nc) {
1316 spin_lock_irqsave(&nc->lock, flags);
1318 /* Channels may be busy, mark dirty instead of
1320 * a) not ACTIVE (configured)
1321 * b) in the channel_queue (to be configured)
1322 * c) it's ndev is in the config state
1324 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1325 if ((ndp->ndev.state & 0xff00) ==
1326 ncsi_dev_state_config ||
1327 !list_empty(&nc->link)) {
1328 netdev_printk(KERN_DEBUG, nd->dev,
1329 "NCSI: channel %p marked dirty\n",
1331 nc->reconfigure_needed = true;
1333 spin_unlock_irqrestore(&nc->lock, flags);
1337 spin_unlock_irqrestore(&nc->lock, flags);
1339 ncsi_stop_channel_monitor(nc);
1340 spin_lock_irqsave(&nc->lock, flags);
1341 nc->state = NCSI_CHANNEL_INACTIVE;
1342 spin_unlock_irqrestore(&nc->lock, flags);
1344 spin_lock_irqsave(&ndp->lock, flags);
1345 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1346 spin_unlock_irqrestore(&ndp->lock, flags);
1348 netdev_printk(KERN_DEBUG, nd->dev,
1349 "NCSI: kicked channel %p\n", nc);
1357 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1359 struct ncsi_dev_priv *ndp;
1360 unsigned int n_vids = 0;
1361 struct vlan_vid *vlan;
1362 struct ncsi_dev *nd;
1368 nd = ncsi_find_dev(dev);
1370 netdev_warn(dev, "NCSI: No net_device?\n");
1374 ndp = TO_NCSI_DEV_PRIV(nd);
1376 /* Add the VLAN id to our internal list */
1377 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1379 if (vlan->vid == vid) {
1380 netdev_printk(KERN_DEBUG, dev,
1381 "NCSI: vid %u already registered\n", vid);
1385 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1387 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1388 vid, NCSI_MAX_VLAN_VIDS);
1392 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1396 vlan->proto = proto;
1398 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1400 netdev_printk(KERN_DEBUG, dev, "NCSI: Added new vid %u\n", vid);
1402 found = ncsi_kick_channels(ndp) != 0;
1404 return found ? ncsi_process_next_channel(ndp) : 0;
1406 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1408 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1410 struct vlan_vid *vlan, *tmp;
1411 struct ncsi_dev_priv *ndp;
1412 struct ncsi_dev *nd;
1418 nd = ncsi_find_dev(dev);
1420 netdev_warn(dev, "NCSI: no net_device?\n");
1424 ndp = TO_NCSI_DEV_PRIV(nd);
1426 /* Remove the VLAN id from our internal list */
1427 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1428 if (vlan->vid == vid) {
1429 netdev_printk(KERN_DEBUG, dev,
1430 "NCSI: vid %u found, removing\n", vid);
1431 list_del_rcu(&vlan->list);
1437 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1441 found = ncsi_kick_channels(ndp) != 0;
1443 return found ? ncsi_process_next_channel(ndp) : 0;
1445 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1447 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1448 void (*handler)(struct ncsi_dev *ndev))
1450 struct ncsi_dev_priv *ndp;
1451 struct ncsi_dev *nd;
1452 unsigned long flags;
1455 /* Check if the device has been registered or not */
1456 nd = ncsi_find_dev(dev);
1460 /* Create NCSI device */
1461 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1466 nd->state = ncsi_dev_state_registered;
1468 nd->handler = handler;
1469 ndp->pending_req_num = 0;
1470 INIT_LIST_HEAD(&ndp->channel_queue);
1471 INIT_LIST_HEAD(&ndp->vlan_vids);
1472 INIT_WORK(&ndp->work, ncsi_dev_work);
1474 /* Initialize private NCSI device */
1475 spin_lock_init(&ndp->lock);
1476 INIT_LIST_HEAD(&ndp->packages);
1477 ndp->request_id = NCSI_REQ_START_IDX;
1478 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1479 ndp->requests[i].id = i;
1480 ndp->requests[i].ndp = ndp;
1481 setup_timer(&ndp->requests[i].timer,
1482 ncsi_request_timeout,
1483 (unsigned long)&ndp->requests[i]);
1486 spin_lock_irqsave(&ncsi_dev_lock, flags);
1487 #if IS_ENABLED(CONFIG_IPV6)
1488 ndp->inet6_addr_num = 0;
1489 if (list_empty(&ncsi_dev_list))
1490 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1492 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1493 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1495 /* Register NCSI packet Rx handler */
1496 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1497 ndp->ptype.func = ncsi_rcv_rsp;
1498 ndp->ptype.dev = dev;
1499 dev_add_pack(&ndp->ptype);
1501 /* Set up generic netlink interface */
1502 ncsi_init_netlink(dev);
1506 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1508 int ncsi_start_dev(struct ncsi_dev *nd)
1510 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1513 if (nd->state != ncsi_dev_state_registered &&
1514 nd->state != ncsi_dev_state_functional)
1517 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1518 nd->state = ncsi_dev_state_probe;
1519 schedule_work(&ndp->work);
1523 if (ndp->flags & NCSI_DEV_HWA) {
1524 netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
1525 ret = ncsi_enable_hwa(ndp);
1527 ret = ncsi_choose_active_channel(ndp);
1532 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1534 void ncsi_stop_dev(struct ncsi_dev *nd)
1536 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1537 struct ncsi_package *np;
1538 struct ncsi_channel *nc;
1541 unsigned long flags;
1543 /* Stop the channel monitor and reset channel's state */
1544 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1545 NCSI_FOR_EACH_CHANNEL(np, nc) {
1546 ncsi_stop_channel_monitor(nc);
1548 spin_lock_irqsave(&nc->lock, flags);
1549 chained = !list_empty(&nc->link);
1550 old_state = nc->state;
1551 nc->state = NCSI_CHANNEL_INACTIVE;
1552 spin_unlock_irqrestore(&nc->lock, flags);
1554 WARN_ON_ONCE(chained ||
1555 old_state == NCSI_CHANNEL_INVISIBLE);
1559 netdev_printk(KERN_DEBUG, ndp->ndev.dev, "NCSI: Stopping device\n");
1560 ncsi_report_link(ndp, true);
1562 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1564 void ncsi_unregister_dev(struct ncsi_dev *nd)
1566 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1567 struct ncsi_package *np, *tmp;
1568 unsigned long flags;
1570 dev_remove_pack(&ndp->ptype);
1572 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1573 ncsi_remove_package(np);
1575 spin_lock_irqsave(&ncsi_dev_lock, flags);
1576 list_del_rcu(&ndp->node);
1577 #if IS_ENABLED(CONFIG_IPV6)
1578 if (list_empty(&ncsi_dev_list))
1579 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1581 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1583 ncsi_unregister_netlink(nd->dev);
1587 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);