GNU Linux-libre 4.19.245-gnu1
[releases.git] / net / ncsi / ncsi-manage.c
1 /*
2  * Copyright Gavin Shan, IBM Corporation 2016.
3  *
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.
8  */
9
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
16 #include <net/ncsi.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 #include <net/addrconf.h>
20 #include <net/ipv6.h>
21 #include <net/if_inet6.h>
22
23 #include "internal.h"
24 #include "ncsi-pkt.h"
25 #include "ncsi-netlink.h"
26
27 LIST_HEAD(ncsi_dev_list);
28 DEFINE_SPINLOCK(ncsi_dev_lock);
29
30 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
31 {
32         struct ncsi_dev *nd = &ndp->ndev;
33         struct ncsi_package *np;
34         struct ncsi_channel *nc;
35         unsigned long flags;
36
37         nd->state = ncsi_dev_state_functional;
38         if (force_down) {
39                 nd->link_up = 0;
40                 goto report;
41         }
42
43         nd->link_up = 0;
44         NCSI_FOR_EACH_PACKAGE(ndp, np) {
45                 NCSI_FOR_EACH_CHANNEL(np, nc) {
46                         spin_lock_irqsave(&nc->lock, flags);
47
48                         if (!list_empty(&nc->link) ||
49                             nc->state != NCSI_CHANNEL_ACTIVE) {
50                                 spin_unlock_irqrestore(&nc->lock, flags);
51                                 continue;
52                         }
53
54                         if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
55                                 spin_unlock_irqrestore(&nc->lock, flags);
56                                 nd->link_up = 1;
57                                 goto report;
58                         }
59
60                         spin_unlock_irqrestore(&nc->lock, flags);
61                 }
62         }
63
64 report:
65         nd->handler(nd);
66 }
67
68 static void ncsi_channel_monitor(struct timer_list *t)
69 {
70         struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
71         struct ncsi_package *np = nc->package;
72         struct ncsi_dev_priv *ndp = np->ndp;
73         struct ncsi_channel_mode *ncm;
74         struct ncsi_cmd_arg nca;
75         bool enabled, chained;
76         unsigned int monitor_state;
77         unsigned long flags;
78         int state, ret;
79
80         spin_lock_irqsave(&nc->lock, flags);
81         state = nc->state;
82         chained = !list_empty(&nc->link);
83         enabled = nc->monitor.enabled;
84         monitor_state = nc->monitor.state;
85         spin_unlock_irqrestore(&nc->lock, flags);
86
87         if (!enabled)
88                 return;         /* expected race disabling timer */
89         if (WARN_ON_ONCE(chained))
90                 goto bad_state;
91
92         if (state != NCSI_CHANNEL_INACTIVE &&
93             state != NCSI_CHANNEL_ACTIVE) {
94 bad_state:
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);
101                 return;
102         }
103
104         switch (monitor_state) {
105         case NCSI_CHANNEL_MONITOR_START:
106         case NCSI_CHANNEL_MONITOR_RETRY:
107                 nca.ndp = ndp;
108                 nca.package = np->id;
109                 nca.channel = nc->id;
110                 nca.type = NCSI_PKT_CMD_GLS;
111                 nca.req_flags = 0;
112                 ret = ncsi_xmit_cmd(&nca);
113                 if (ret)
114                         netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
115                                    ret);
116                 break;
117         case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
118                 break;
119         default:
120                 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
121                            nc->id);
122                 if (!(ndp->flags & NCSI_DEV_HWA)) {
123                         ncsi_report_link(ndp, true);
124                         ndp->flags |= NCSI_DEV_RESHUFFLE;
125                 }
126
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);
133
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);
139                 return;
140         }
141
142         spin_lock_irqsave(&nc->lock, flags);
143         nc->monitor.state++;
144         spin_unlock_irqrestore(&nc->lock, flags);
145         mod_timer(&nc->monitor.timer, jiffies + HZ);
146 }
147
148 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
149 {
150         unsigned long flags;
151
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);
157
158         mod_timer(&nc->monitor.timer, jiffies + HZ);
159 }
160
161 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
162 {
163         unsigned long flags;
164
165         spin_lock_irqsave(&nc->lock, flags);
166         if (!nc->monitor.enabled) {
167                 spin_unlock_irqrestore(&nc->lock, flags);
168                 return;
169         }
170         nc->monitor.enabled = false;
171         spin_unlock_irqrestore(&nc->lock, flags);
172
173         del_timer_sync(&nc->monitor.timer);
174 }
175
176 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
177                                        unsigned char id)
178 {
179         struct ncsi_channel *nc;
180
181         NCSI_FOR_EACH_CHANNEL(np, nc) {
182                 if (nc->id == id)
183                         return nc;
184         }
185
186         return NULL;
187 }
188
189 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
190 {
191         struct ncsi_channel *nc, *tmp;
192         int index;
193         unsigned long flags;
194
195         nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
196         if (!nc)
197                 return NULL;
198
199         nc->id = id;
200         nc->package = np;
201         nc->state = NCSI_CHANNEL_INACTIVE;
202         nc->monitor.enabled = false;
203         timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
204         spin_lock_init(&nc->lock);
205         INIT_LIST_HEAD(&nc->link);
206         for (index = 0; index < NCSI_CAP_MAX; index++)
207                 nc->caps[index].index = index;
208         for (index = 0; index < NCSI_MODE_MAX; index++)
209                 nc->modes[index].index = index;
210
211         spin_lock_irqsave(&np->lock, flags);
212         tmp = ncsi_find_channel(np, id);
213         if (tmp) {
214                 spin_unlock_irqrestore(&np->lock, flags);
215                 kfree(nc);
216                 return tmp;
217         }
218
219         list_add_tail_rcu(&nc->node, &np->channels);
220         np->channel_num++;
221         spin_unlock_irqrestore(&np->lock, flags);
222
223         return nc;
224 }
225
226 static void ncsi_remove_channel(struct ncsi_channel *nc)
227 {
228         struct ncsi_package *np = nc->package;
229         unsigned long flags;
230
231         spin_lock_irqsave(&nc->lock, flags);
232
233         /* Release filters */
234         kfree(nc->mac_filter.addrs);
235         kfree(nc->vlan_filter.vids);
236
237         nc->state = NCSI_CHANNEL_INACTIVE;
238         spin_unlock_irqrestore(&nc->lock, flags);
239         ncsi_stop_channel_monitor(nc);
240
241         /* Remove and free channel */
242         spin_lock_irqsave(&np->lock, flags);
243         list_del_rcu(&nc->node);
244         np->channel_num--;
245         spin_unlock_irqrestore(&np->lock, flags);
246
247         kfree(nc);
248 }
249
250 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
251                                        unsigned char id)
252 {
253         struct ncsi_package *np;
254
255         NCSI_FOR_EACH_PACKAGE(ndp, np) {
256                 if (np->id == id)
257                         return np;
258         }
259
260         return NULL;
261 }
262
263 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
264                                       unsigned char id)
265 {
266         struct ncsi_package *np, *tmp;
267         unsigned long flags;
268
269         np = kzalloc(sizeof(*np), GFP_ATOMIC);
270         if (!np)
271                 return NULL;
272
273         np->id = id;
274         np->ndp = ndp;
275         spin_lock_init(&np->lock);
276         INIT_LIST_HEAD(&np->channels);
277
278         spin_lock_irqsave(&ndp->lock, flags);
279         tmp = ncsi_find_package(ndp, id);
280         if (tmp) {
281                 spin_unlock_irqrestore(&ndp->lock, flags);
282                 kfree(np);
283                 return tmp;
284         }
285
286         list_add_tail_rcu(&np->node, &ndp->packages);
287         ndp->package_num++;
288         spin_unlock_irqrestore(&ndp->lock, flags);
289
290         return np;
291 }
292
293 void ncsi_remove_package(struct ncsi_package *np)
294 {
295         struct ncsi_dev_priv *ndp = np->ndp;
296         struct ncsi_channel *nc, *tmp;
297         unsigned long flags;
298
299         /* Release all child channels */
300         list_for_each_entry_safe(nc, tmp, &np->channels, node)
301                 ncsi_remove_channel(nc);
302
303         /* Remove and free package */
304         spin_lock_irqsave(&ndp->lock, flags);
305         list_del_rcu(&np->node);
306         ndp->package_num--;
307         spin_unlock_irqrestore(&ndp->lock, flags);
308
309         kfree(np);
310 }
311
312 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
313                                    unsigned char id,
314                                    struct ncsi_package **np,
315                                    struct ncsi_channel **nc)
316 {
317         struct ncsi_package *p;
318         struct ncsi_channel *c;
319
320         p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
321         c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
322
323         if (np)
324                 *np = p;
325         if (nc)
326                 *nc = c;
327 }
328
329 /* For two consecutive NCSI commands, the packet IDs shouldn't
330  * be same. Otherwise, the bogus response might be replied. So
331  * the available IDs are allocated in round-robin fashion.
332  */
333 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
334                                         unsigned int req_flags)
335 {
336         struct ncsi_request *nr = NULL;
337         int i, limit = ARRAY_SIZE(ndp->requests);
338         unsigned long flags;
339
340         /* Check if there is one available request until the ceiling */
341         spin_lock_irqsave(&ndp->lock, flags);
342         for (i = ndp->request_id; i < limit; i++) {
343                 if (ndp->requests[i].used)
344                         continue;
345
346                 nr = &ndp->requests[i];
347                 nr->used = true;
348                 nr->flags = req_flags;
349                 ndp->request_id = i + 1;
350                 goto found;
351         }
352
353         /* Fail back to check from the starting cursor */
354         for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
355                 if (ndp->requests[i].used)
356                         continue;
357
358                 nr = &ndp->requests[i];
359                 nr->used = true;
360                 nr->flags = req_flags;
361                 ndp->request_id = i + 1;
362                 goto found;
363         }
364
365 found:
366         spin_unlock_irqrestore(&ndp->lock, flags);
367         return nr;
368 }
369
370 void ncsi_free_request(struct ncsi_request *nr)
371 {
372         struct ncsi_dev_priv *ndp = nr->ndp;
373         struct sk_buff *cmd, *rsp;
374         unsigned long flags;
375         bool driven;
376
377         if (nr->enabled) {
378                 nr->enabled = false;
379                 del_timer_sync(&nr->timer);
380         }
381
382         spin_lock_irqsave(&ndp->lock, flags);
383         cmd = nr->cmd;
384         rsp = nr->rsp;
385         nr->cmd = NULL;
386         nr->rsp = NULL;
387         nr->used = false;
388         driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
389         spin_unlock_irqrestore(&ndp->lock, flags);
390
391         if (driven && cmd && --ndp->pending_req_num == 0)
392                 schedule_work(&ndp->work);
393
394         /* Release command and response */
395         consume_skb(cmd);
396         consume_skb(rsp);
397 }
398
399 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
400 {
401         struct ncsi_dev_priv *ndp;
402
403         NCSI_FOR_EACH_DEV(ndp) {
404                 if (ndp->ndev.dev == dev)
405                         return &ndp->ndev;
406         }
407
408         return NULL;
409 }
410
411 static void ncsi_request_timeout(struct timer_list *t)
412 {
413         struct ncsi_request *nr = from_timer(nr, t, timer);
414         struct ncsi_dev_priv *ndp = nr->ndp;
415         unsigned long flags;
416
417         /* If the request already had associated response,
418          * let the response handler to release it.
419          */
420         spin_lock_irqsave(&ndp->lock, flags);
421         nr->enabled = false;
422         if (nr->rsp || !nr->cmd) {
423                 spin_unlock_irqrestore(&ndp->lock, flags);
424                 return;
425         }
426         spin_unlock_irqrestore(&ndp->lock, flags);
427
428         /* Release the request */
429         ncsi_free_request(nr);
430 }
431
432 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
433 {
434         struct ncsi_dev *nd = &ndp->ndev;
435         struct ncsi_package *np = ndp->active_package;
436         struct ncsi_channel *nc = ndp->active_channel;
437         struct ncsi_cmd_arg nca;
438         unsigned long flags;
439         int ret;
440
441         nca.ndp = ndp;
442         nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
443         switch (nd->state) {
444         case ncsi_dev_state_suspend:
445                 nd->state = ncsi_dev_state_suspend_select;
446                 /* Fall through */
447         case ncsi_dev_state_suspend_select:
448                 ndp->pending_req_num = 1;
449
450                 nca.type = NCSI_PKT_CMD_SP;
451                 nca.package = np->id;
452                 nca.channel = NCSI_RESERVED_CHANNEL;
453                 if (ndp->flags & NCSI_DEV_HWA)
454                         nca.bytes[0] = 0;
455                 else
456                         nca.bytes[0] = 1;
457
458                 /* To retrieve the last link states of channels in current
459                  * package when current active channel needs fail over to
460                  * another one. It means we will possibly select another
461                  * channel as next active one. The link states of channels
462                  * are most important factor of the selection. So we need
463                  * accurate link states. Unfortunately, the link states on
464                  * inactive channels can't be updated with LSC AEN in time.
465                  */
466                 if (ndp->flags & NCSI_DEV_RESHUFFLE)
467                         nd->state = ncsi_dev_state_suspend_gls;
468                 else
469                         nd->state = ncsi_dev_state_suspend_dcnt;
470                 ret = ncsi_xmit_cmd(&nca);
471                 if (ret)
472                         goto error;
473
474                 break;
475         case ncsi_dev_state_suspend_gls:
476                 ndp->pending_req_num = np->channel_num;
477
478                 nca.type = NCSI_PKT_CMD_GLS;
479                 nca.package = np->id;
480
481                 nd->state = ncsi_dev_state_suspend_dcnt;
482                 NCSI_FOR_EACH_CHANNEL(np, nc) {
483                         nca.channel = nc->id;
484                         ret = ncsi_xmit_cmd(&nca);
485                         if (ret)
486                                 goto error;
487                 }
488
489                 break;
490         case ncsi_dev_state_suspend_dcnt:
491                 ndp->pending_req_num = 1;
492
493                 nca.type = NCSI_PKT_CMD_DCNT;
494                 nca.package = np->id;
495                 nca.channel = nc->id;
496
497                 nd->state = ncsi_dev_state_suspend_dc;
498                 ret = ncsi_xmit_cmd(&nca);
499                 if (ret)
500                         goto error;
501
502                 break;
503         case ncsi_dev_state_suspend_dc:
504                 ndp->pending_req_num = 1;
505
506                 nca.type = NCSI_PKT_CMD_DC;
507                 nca.package = np->id;
508                 nca.channel = nc->id;
509                 nca.bytes[0] = 1;
510
511                 nd->state = ncsi_dev_state_suspend_deselect;
512                 ret = ncsi_xmit_cmd(&nca);
513                 if (ret)
514                         goto error;
515
516                 break;
517         case ncsi_dev_state_suspend_deselect:
518                 ndp->pending_req_num = 1;
519
520                 nca.type = NCSI_PKT_CMD_DP;
521                 nca.package = np->id;
522                 nca.channel = NCSI_RESERVED_CHANNEL;
523
524                 nd->state = ncsi_dev_state_suspend_done;
525                 ret = ncsi_xmit_cmd(&nca);
526                 if (ret)
527                         goto error;
528
529                 break;
530         case ncsi_dev_state_suspend_done:
531                 spin_lock_irqsave(&nc->lock, flags);
532                 nc->state = NCSI_CHANNEL_INACTIVE;
533                 spin_unlock_irqrestore(&nc->lock, flags);
534                 ncsi_process_next_channel(ndp);
535
536                 break;
537         default:
538                 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
539                             nd->state);
540         }
541
542         return;
543 error:
544         nd->state = ncsi_dev_state_functional;
545 }
546
547 /* Check the VLAN filter bitmap for a set filter, and construct a
548  * "Set VLAN Filter - Disable" packet if found.
549  */
550 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
551                          struct ncsi_cmd_arg *nca)
552 {
553         struct ncsi_channel_vlan_filter *ncf;
554         unsigned long flags;
555         void *bitmap;
556         int index;
557         u16 vid;
558
559         ncf = &nc->vlan_filter;
560         bitmap = &ncf->bitmap;
561
562         spin_lock_irqsave(&nc->lock, flags);
563         index = find_next_bit(bitmap, ncf->n_vids, 0);
564         if (index >= ncf->n_vids) {
565                 spin_unlock_irqrestore(&nc->lock, flags);
566                 return -1;
567         }
568         vid = ncf->vids[index];
569
570         clear_bit(index, bitmap);
571         ncf->vids[index] = 0;
572         spin_unlock_irqrestore(&nc->lock, flags);
573
574         nca->type = NCSI_PKT_CMD_SVF;
575         nca->words[1] = vid;
576         /* HW filter index starts at 1 */
577         nca->bytes[6] = index + 1;
578         nca->bytes[7] = 0x00;
579         return 0;
580 }
581
582 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
583  * packet.
584  */
585 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
586                        struct ncsi_cmd_arg *nca)
587 {
588         struct ncsi_channel_vlan_filter *ncf;
589         struct vlan_vid *vlan = NULL;
590         unsigned long flags;
591         int i, index;
592         void *bitmap;
593         u16 vid;
594
595         if (list_empty(&ndp->vlan_vids))
596                 return -1;
597
598         ncf = &nc->vlan_filter;
599         bitmap = &ncf->bitmap;
600
601         spin_lock_irqsave(&nc->lock, flags);
602
603         rcu_read_lock();
604         list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
605                 vid = vlan->vid;
606                 for (i = 0; i < ncf->n_vids; i++)
607                         if (ncf->vids[i] == vid) {
608                                 vid = 0;
609                                 break;
610                         }
611                 if (vid)
612                         break;
613         }
614         rcu_read_unlock();
615
616         if (!vid) {
617                 /* No VLAN ID is not set */
618                 spin_unlock_irqrestore(&nc->lock, flags);
619                 return -1;
620         }
621
622         index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
623         if (index < 0 || index >= ncf->n_vids) {
624                 netdev_err(ndp->ndev.dev,
625                            "Channel %u already has all VLAN filters set\n",
626                            nc->id);
627                 spin_unlock_irqrestore(&nc->lock, flags);
628                 return -1;
629         }
630
631         ncf->vids[index] = vid;
632         set_bit(index, bitmap);
633         spin_unlock_irqrestore(&nc->lock, flags);
634
635         nca->type = NCSI_PKT_CMD_SVF;
636         nca->words[1] = vid;
637         /* HW filter index starts at 1 */
638         nca->bytes[6] = index + 1;
639         nca->bytes[7] = 0x01;
640
641         return 0;
642 }
643
644 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
645 {
646         struct ncsi_dev *nd = &ndp->ndev;
647         struct net_device *dev = nd->dev;
648         struct ncsi_package *np = ndp->active_package;
649         struct ncsi_channel *nc = ndp->active_channel;
650         struct ncsi_channel *hot_nc = NULL;
651         struct ncsi_cmd_arg nca;
652         unsigned char index;
653         unsigned long flags;
654         int ret;
655
656         nca.ndp = ndp;
657         nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
658         switch (nd->state) {
659         case ncsi_dev_state_config:
660         case ncsi_dev_state_config_sp:
661                 ndp->pending_req_num = 1;
662
663                 /* Select the specific package */
664                 nca.type = NCSI_PKT_CMD_SP;
665                 if (ndp->flags & NCSI_DEV_HWA)
666                         nca.bytes[0] = 0;
667                 else
668                         nca.bytes[0] = 1;
669                 nca.package = np->id;
670                 nca.channel = NCSI_RESERVED_CHANNEL;
671                 ret = ncsi_xmit_cmd(&nca);
672                 if (ret) {
673                         netdev_err(ndp->ndev.dev,
674                                    "NCSI: Failed to transmit CMD_SP\n");
675                         goto error;
676                 }
677
678                 nd->state = ncsi_dev_state_config_cis;
679                 break;
680         case ncsi_dev_state_config_cis:
681                 ndp->pending_req_num = 1;
682
683                 /* Clear initial state */
684                 nca.type = NCSI_PKT_CMD_CIS;
685                 nca.package = np->id;
686                 nca.channel = nc->id;
687                 ret = ncsi_xmit_cmd(&nca);
688                 if (ret) {
689                         netdev_err(ndp->ndev.dev,
690                                    "NCSI: Failed to transmit CMD_CIS\n");
691                         goto error;
692                 }
693
694                 nd->state = ncsi_dev_state_config_clear_vids;
695                 break;
696         case ncsi_dev_state_config_clear_vids:
697         case ncsi_dev_state_config_svf:
698         case ncsi_dev_state_config_ev:
699         case ncsi_dev_state_config_sma:
700         case ncsi_dev_state_config_ebf:
701 #if IS_ENABLED(CONFIG_IPV6)
702         case ncsi_dev_state_config_egmf:
703 #endif
704         case ncsi_dev_state_config_ecnt:
705         case ncsi_dev_state_config_ec:
706         case ncsi_dev_state_config_ae:
707         case ncsi_dev_state_config_gls:
708                 ndp->pending_req_num = 1;
709
710                 nca.package = np->id;
711                 nca.channel = nc->id;
712
713                 /* Clear any active filters on the channel before setting */
714                 if (nd->state == ncsi_dev_state_config_clear_vids) {
715                         ret = clear_one_vid(ndp, nc, &nca);
716                         if (ret) {
717                                 nd->state = ncsi_dev_state_config_svf;
718                                 schedule_work(&ndp->work);
719                                 break;
720                         }
721                         /* Repeat */
722                         nd->state = ncsi_dev_state_config_clear_vids;
723                 /* Add known VLAN tags to the filter */
724                 } else if (nd->state == ncsi_dev_state_config_svf) {
725                         ret = set_one_vid(ndp, nc, &nca);
726                         if (ret) {
727                                 nd->state = ncsi_dev_state_config_ev;
728                                 schedule_work(&ndp->work);
729                                 break;
730                         }
731                         /* Repeat */
732                         nd->state = ncsi_dev_state_config_svf;
733                 /* Enable/Disable the VLAN filter */
734                 } else if (nd->state == ncsi_dev_state_config_ev) {
735                         if (list_empty(&ndp->vlan_vids)) {
736                                 nca.type = NCSI_PKT_CMD_DV;
737                         } else {
738                                 nca.type = NCSI_PKT_CMD_EV;
739                                 nca.bytes[3] = NCSI_CAP_VLAN_NO;
740                         }
741                         nd->state = ncsi_dev_state_config_sma;
742                 } else if (nd->state == ncsi_dev_state_config_sma) {
743                 /* Use first entry in unicast filter table. Note that
744                  * the MAC filter table starts from entry 1 instead of
745                  * 0.
746                  */
747                         nca.type = NCSI_PKT_CMD_SMA;
748                         for (index = 0; index < 6; index++)
749                                 nca.bytes[index] = dev->dev_addr[index];
750                         nca.bytes[6] = 0x1;
751                         nca.bytes[7] = 0x1;
752                         nd->state = ncsi_dev_state_config_ebf;
753                 } else if (nd->state == ncsi_dev_state_config_ebf) {
754                         nca.type = NCSI_PKT_CMD_EBF;
755                         nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
756                         nd->state = ncsi_dev_state_config_ecnt;
757 #if IS_ENABLED(CONFIG_IPV6)
758                         if (ndp->inet6_addr_num > 0 &&
759                             (nc->caps[NCSI_CAP_GENERIC].cap &
760                              NCSI_CAP_GENERIC_MC))
761                                 nd->state = ncsi_dev_state_config_egmf;
762                         else
763                                 nd->state = ncsi_dev_state_config_ecnt;
764                 } else if (nd->state == ncsi_dev_state_config_egmf) {
765                         nca.type = NCSI_PKT_CMD_EGMF;
766                         nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
767                         nd->state = ncsi_dev_state_config_ecnt;
768 #endif /* CONFIG_IPV6 */
769                 } else if (nd->state == ncsi_dev_state_config_ecnt) {
770                         nca.type = NCSI_PKT_CMD_ECNT;
771                         nd->state = ncsi_dev_state_config_ec;
772                 } else if (nd->state == ncsi_dev_state_config_ec) {
773                         /* Enable AEN if it's supported */
774                         nca.type = NCSI_PKT_CMD_EC;
775                         nd->state = ncsi_dev_state_config_ae;
776                         if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
777                                 nd->state = ncsi_dev_state_config_gls;
778                 } else if (nd->state == ncsi_dev_state_config_ae) {
779                         nca.type = NCSI_PKT_CMD_AE;
780                         nca.bytes[0] = 0;
781                         nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
782                         nd->state = ncsi_dev_state_config_gls;
783                 } else if (nd->state == ncsi_dev_state_config_gls) {
784                         nca.type = NCSI_PKT_CMD_GLS;
785                         nd->state = ncsi_dev_state_config_done;
786                 }
787
788                 ret = ncsi_xmit_cmd(&nca);
789                 if (ret) {
790                         netdev_err(ndp->ndev.dev,
791                                    "NCSI: Failed to transmit CMD %x\n",
792                                    nca.type);
793                         goto error;
794                 }
795                 break;
796         case ncsi_dev_state_config_done:
797                 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
798                            nc->id);
799                 spin_lock_irqsave(&nc->lock, flags);
800                 if (nc->reconfigure_needed) {
801                         /* This channel's configuration has been updated
802                          * part-way during the config state - start the
803                          * channel configuration over
804                          */
805                         nc->reconfigure_needed = false;
806                         nc->state = NCSI_CHANNEL_INACTIVE;
807                         spin_unlock_irqrestore(&nc->lock, flags);
808
809                         spin_lock_irqsave(&ndp->lock, flags);
810                         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
811                         spin_unlock_irqrestore(&ndp->lock, flags);
812
813                         netdev_dbg(dev, "Dirty NCSI channel state reset\n");
814                         ncsi_process_next_channel(ndp);
815                         break;
816                 }
817
818                 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
819                         hot_nc = nc;
820                         nc->state = NCSI_CHANNEL_ACTIVE;
821                 } else {
822                         hot_nc = NULL;
823                         nc->state = NCSI_CHANNEL_INACTIVE;
824                         netdev_dbg(ndp->ndev.dev,
825                                    "NCSI: channel %u link down after config\n",
826                                    nc->id);
827                 }
828                 spin_unlock_irqrestore(&nc->lock, flags);
829
830                 /* Update the hot channel */
831                 spin_lock_irqsave(&ndp->lock, flags);
832                 ndp->hot_channel = hot_nc;
833                 spin_unlock_irqrestore(&ndp->lock, flags);
834
835                 ncsi_start_channel_monitor(nc);
836                 ncsi_process_next_channel(ndp);
837                 break;
838         default:
839                 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
840                              nd->state);
841         }
842
843         return;
844
845 error:
846         ncsi_report_link(ndp, true);
847 }
848
849 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
850 {
851         struct ncsi_package *np, *force_package;
852         struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
853         struct ncsi_channel_mode *ncm;
854         unsigned long flags;
855
856         spin_lock_irqsave(&ndp->lock, flags);
857         hot_nc = ndp->hot_channel;
858         force_channel = ndp->force_channel;
859         force_package = ndp->force_package;
860         spin_unlock_irqrestore(&ndp->lock, flags);
861
862         /* Force a specific channel whether or not it has link if we have been
863          * configured to do so
864          */
865         if (force_package && force_channel) {
866                 found = force_channel;
867                 ncm = &found->modes[NCSI_MODE_LINK];
868                 if (!(ncm->data[2] & 0x1))
869                         netdev_info(ndp->ndev.dev,
870                                     "NCSI: Channel %u forced, but it is link down\n",
871                                     found->id);
872                 goto out;
873         }
874
875         /* The search is done once an inactive channel with up
876          * link is found.
877          */
878         found = NULL;
879         NCSI_FOR_EACH_PACKAGE(ndp, np) {
880                 if (ndp->force_package && np != ndp->force_package)
881                         continue;
882                 NCSI_FOR_EACH_CHANNEL(np, nc) {
883                         spin_lock_irqsave(&nc->lock, flags);
884
885                         if (!list_empty(&nc->link) ||
886                             nc->state != NCSI_CHANNEL_INACTIVE) {
887                                 spin_unlock_irqrestore(&nc->lock, flags);
888                                 continue;
889                         }
890
891                         if (!found)
892                                 found = nc;
893
894                         if (nc == hot_nc)
895                                 found = nc;
896
897                         ncm = &nc->modes[NCSI_MODE_LINK];
898                         if (ncm->data[2] & 0x1) {
899                                 spin_unlock_irqrestore(&nc->lock, flags);
900                                 found = nc;
901                                 goto out;
902                         }
903
904                         spin_unlock_irqrestore(&nc->lock, flags);
905                 }
906         }
907
908         if (!found) {
909                 netdev_warn(ndp->ndev.dev,
910                             "NCSI: No channel found with link\n");
911                 ncsi_report_link(ndp, true);
912                 return -ENODEV;
913         }
914
915         ncm = &found->modes[NCSI_MODE_LINK];
916         netdev_dbg(ndp->ndev.dev,
917                    "NCSI: Channel %u added to queue (link %s)\n",
918                    found->id, ncm->data[2] & 0x1 ? "up" : "down");
919
920 out:
921         spin_lock_irqsave(&ndp->lock, flags);
922         list_add_tail_rcu(&found->link, &ndp->channel_queue);
923         spin_unlock_irqrestore(&ndp->lock, flags);
924
925         return ncsi_process_next_channel(ndp);
926 }
927
928 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
929 {
930         struct ncsi_package *np;
931         struct ncsi_channel *nc;
932         unsigned int cap;
933         bool has_channel = false;
934
935         /* The hardware arbitration is disabled if any one channel
936          * doesn't support explicitly.
937          */
938         NCSI_FOR_EACH_PACKAGE(ndp, np) {
939                 NCSI_FOR_EACH_CHANNEL(np, nc) {
940                         has_channel = true;
941
942                         cap = nc->caps[NCSI_CAP_GENERIC].cap;
943                         if (!(cap & NCSI_CAP_GENERIC_HWA) ||
944                             (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
945                             NCSI_CAP_GENERIC_HWA_SUPPORT) {
946                                 ndp->flags &= ~NCSI_DEV_HWA;
947                                 return false;
948                         }
949                 }
950         }
951
952         if (has_channel) {
953                 ndp->flags |= NCSI_DEV_HWA;
954                 return true;
955         }
956
957         ndp->flags &= ~NCSI_DEV_HWA;
958         return false;
959 }
960
961 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
962 {
963         struct ncsi_package *np;
964         struct ncsi_channel *nc;
965         unsigned long flags;
966
967         /* Move all available channels to processing queue */
968         spin_lock_irqsave(&ndp->lock, flags);
969         NCSI_FOR_EACH_PACKAGE(ndp, np) {
970                 NCSI_FOR_EACH_CHANNEL(np, nc) {
971                         WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
972                                      !list_empty(&nc->link));
973                         ncsi_stop_channel_monitor(nc);
974                         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
975                 }
976         }
977         spin_unlock_irqrestore(&ndp->lock, flags);
978
979         /* We can have no channels in extremely case */
980         if (list_empty(&ndp->channel_queue)) {
981                 netdev_err(ndp->ndev.dev,
982                            "NCSI: No available channels for HWA\n");
983                 ncsi_report_link(ndp, false);
984                 return -ENOENT;
985         }
986
987         return ncsi_process_next_channel(ndp);
988 }
989
990 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
991 {
992         struct ncsi_dev *nd = &ndp->ndev;
993         struct ncsi_package *np;
994         struct ncsi_channel *nc;
995         struct ncsi_cmd_arg nca;
996         unsigned char index;
997         int ret;
998
999         nca.ndp = ndp;
1000         nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1001         switch (nd->state) {
1002         case ncsi_dev_state_probe:
1003                 nd->state = ncsi_dev_state_probe_deselect;
1004                 /* Fall through */
1005         case ncsi_dev_state_probe_deselect:
1006                 ndp->pending_req_num = 8;
1007
1008                 /* Deselect all possible packages */
1009                 nca.type = NCSI_PKT_CMD_DP;
1010                 nca.channel = NCSI_RESERVED_CHANNEL;
1011                 for (index = 0; index < 8; index++) {
1012                         nca.package = index;
1013                         ret = ncsi_xmit_cmd(&nca);
1014                         if (ret)
1015                                 goto error;
1016                 }
1017
1018                 nd->state = ncsi_dev_state_probe_package;
1019                 break;
1020         case ncsi_dev_state_probe_package:
1021                 ndp->pending_req_num = 16;
1022
1023                 /* Select all possible packages */
1024                 nca.type = NCSI_PKT_CMD_SP;
1025                 nca.bytes[0] = 1;
1026                 nca.channel = NCSI_RESERVED_CHANNEL;
1027                 for (index = 0; index < 8; index++) {
1028                         nca.package = index;
1029                         ret = ncsi_xmit_cmd(&nca);
1030                         if (ret)
1031                                 goto error;
1032                 }
1033
1034                 /* Disable all possible packages */
1035                 nca.type = NCSI_PKT_CMD_DP;
1036                 for (index = 0; index < 8; index++) {
1037                         nca.package = index;
1038                         ret = ncsi_xmit_cmd(&nca);
1039                         if (ret)
1040                                 goto error;
1041                 }
1042
1043                 nd->state = ncsi_dev_state_probe_channel;
1044                 break;
1045         case ncsi_dev_state_probe_channel:
1046                 if (!ndp->active_package)
1047                         ndp->active_package = list_first_or_null_rcu(
1048                                 &ndp->packages, struct ncsi_package, node);
1049                 else if (list_is_last(&ndp->active_package->node,
1050                                       &ndp->packages))
1051                         ndp->active_package = NULL;
1052                 else
1053                         ndp->active_package = list_next_entry(
1054                                 ndp->active_package, node);
1055
1056                 /* All available packages and channels are enumerated. The
1057                  * enumeration happens for once when the NCSI interface is
1058                  * started. So we need continue to start the interface after
1059                  * the enumeration.
1060                  *
1061                  * We have to choose an active channel before configuring it.
1062                  * Note that we possibly don't have active channel in extreme
1063                  * situation.
1064                  */
1065                 if (!ndp->active_package) {
1066                         ndp->flags |= NCSI_DEV_PROBED;
1067                         if (ncsi_check_hwa(ndp))
1068                                 ncsi_enable_hwa(ndp);
1069                         else
1070                                 ncsi_choose_active_channel(ndp);
1071                         return;
1072                 }
1073
1074                 /* Select the active package */
1075                 ndp->pending_req_num = 1;
1076                 nca.type = NCSI_PKT_CMD_SP;
1077                 nca.bytes[0] = 1;
1078                 nca.package = ndp->active_package->id;
1079                 nca.channel = NCSI_RESERVED_CHANNEL;
1080                 ret = ncsi_xmit_cmd(&nca);
1081                 if (ret)
1082                         goto error;
1083
1084                 nd->state = ncsi_dev_state_probe_cis;
1085                 break;
1086         case ncsi_dev_state_probe_cis:
1087                 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1088
1089                 /* Clear initial state */
1090                 nca.type = NCSI_PKT_CMD_CIS;
1091                 nca.package = ndp->active_package->id;
1092                 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
1093                         nca.channel = index;
1094                         ret = ncsi_xmit_cmd(&nca);
1095                         if (ret)
1096                                 goto error;
1097                 }
1098
1099                 nd->state = ncsi_dev_state_probe_gvi;
1100                 break;
1101         case ncsi_dev_state_probe_gvi:
1102         case ncsi_dev_state_probe_gc:
1103         case ncsi_dev_state_probe_gls:
1104                 np = ndp->active_package;
1105                 ndp->pending_req_num = np->channel_num;
1106
1107                 /* Retrieve version, capability or link status */
1108                 if (nd->state == ncsi_dev_state_probe_gvi)
1109                         nca.type = NCSI_PKT_CMD_GVI;
1110                 else if (nd->state == ncsi_dev_state_probe_gc)
1111                         nca.type = NCSI_PKT_CMD_GC;
1112                 else
1113                         nca.type = NCSI_PKT_CMD_GLS;
1114
1115                 nca.package = np->id;
1116                 NCSI_FOR_EACH_CHANNEL(np, nc) {
1117                         nca.channel = nc->id;
1118                         ret = ncsi_xmit_cmd(&nca);
1119                         if (ret)
1120                                 goto error;
1121                 }
1122
1123                 if (nd->state == ncsi_dev_state_probe_gvi)
1124                         nd->state = ncsi_dev_state_probe_gc;
1125                 else if (nd->state == ncsi_dev_state_probe_gc)
1126                         nd->state = ncsi_dev_state_probe_gls;
1127                 else
1128                         nd->state = ncsi_dev_state_probe_dp;
1129                 break;
1130         case ncsi_dev_state_probe_dp:
1131                 ndp->pending_req_num = 1;
1132
1133                 /* Deselect the active package */
1134                 nca.type = NCSI_PKT_CMD_DP;
1135                 nca.package = ndp->active_package->id;
1136                 nca.channel = NCSI_RESERVED_CHANNEL;
1137                 ret = ncsi_xmit_cmd(&nca);
1138                 if (ret)
1139                         goto error;
1140
1141                 /* Scan channels in next package */
1142                 nd->state = ncsi_dev_state_probe_channel;
1143                 break;
1144         default:
1145                 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1146                             nd->state);
1147         }
1148
1149         return;
1150 error:
1151         netdev_err(ndp->ndev.dev,
1152                    "NCSI: Failed to transmit cmd 0x%x during probe\n",
1153                    nca.type);
1154         ncsi_report_link(ndp, true);
1155 }
1156
1157 static void ncsi_dev_work(struct work_struct *work)
1158 {
1159         struct ncsi_dev_priv *ndp = container_of(work,
1160                         struct ncsi_dev_priv, work);
1161         struct ncsi_dev *nd = &ndp->ndev;
1162
1163         switch (nd->state & ncsi_dev_state_major) {
1164         case ncsi_dev_state_probe:
1165                 ncsi_probe_channel(ndp);
1166                 break;
1167         case ncsi_dev_state_suspend:
1168                 ncsi_suspend_channel(ndp);
1169                 break;
1170         case ncsi_dev_state_config:
1171                 ncsi_configure_channel(ndp);
1172                 break;
1173         default:
1174                 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1175                             nd->state);
1176         }
1177 }
1178
1179 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1180 {
1181         struct ncsi_channel *nc;
1182         int old_state;
1183         unsigned long flags;
1184
1185         spin_lock_irqsave(&ndp->lock, flags);
1186         nc = list_first_or_null_rcu(&ndp->channel_queue,
1187                                     struct ncsi_channel, link);
1188         if (!nc) {
1189                 spin_unlock_irqrestore(&ndp->lock, flags);
1190                 goto out;
1191         }
1192
1193         list_del_init(&nc->link);
1194         spin_unlock_irqrestore(&ndp->lock, flags);
1195
1196         spin_lock_irqsave(&nc->lock, flags);
1197         old_state = nc->state;
1198         nc->state = NCSI_CHANNEL_INVISIBLE;
1199         spin_unlock_irqrestore(&nc->lock, flags);
1200
1201         ndp->active_channel = nc;
1202         ndp->active_package = nc->package;
1203
1204         switch (old_state) {
1205         case NCSI_CHANNEL_INACTIVE:
1206                 ndp->ndev.state = ncsi_dev_state_config;
1207                 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1208                            nc->id);
1209                 ncsi_configure_channel(ndp);
1210                 break;
1211         case NCSI_CHANNEL_ACTIVE:
1212                 ndp->ndev.state = ncsi_dev_state_suspend;
1213                 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1214                            nc->id);
1215                 ncsi_suspend_channel(ndp);
1216                 break;
1217         default:
1218                 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1219                            old_state, nc->package->id, nc->id);
1220                 ncsi_report_link(ndp, false);
1221                 return -EINVAL;
1222         }
1223
1224         return 0;
1225
1226 out:
1227         ndp->active_channel = NULL;
1228         ndp->active_package = NULL;
1229         if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1230                 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1231                 return ncsi_choose_active_channel(ndp);
1232         }
1233
1234         ncsi_report_link(ndp, false);
1235         return -ENODEV;
1236 }
1237
1238 #if IS_ENABLED(CONFIG_IPV6)
1239 static int ncsi_inet6addr_event(struct notifier_block *this,
1240                                 unsigned long event, void *data)
1241 {
1242         struct inet6_ifaddr *ifa = data;
1243         struct net_device *dev = ifa->idev->dev;
1244         struct ncsi_dev *nd = ncsi_find_dev(dev);
1245         struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1246         struct ncsi_package *np;
1247         struct ncsi_channel *nc;
1248         struct ncsi_cmd_arg nca;
1249         bool action;
1250         int ret;
1251
1252         if (!ndp || (ipv6_addr_type(&ifa->addr) &
1253             (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1254                 return NOTIFY_OK;
1255
1256         switch (event) {
1257         case NETDEV_UP:
1258                 action = (++ndp->inet6_addr_num) == 1;
1259                 nca.type = NCSI_PKT_CMD_EGMF;
1260                 break;
1261         case NETDEV_DOWN:
1262                 action = (--ndp->inet6_addr_num == 0);
1263                 nca.type = NCSI_PKT_CMD_DGMF;
1264                 break;
1265         default:
1266                 return NOTIFY_OK;
1267         }
1268
1269         /* We might not have active channel or packages. The IPv6
1270          * required multicast will be enabled when active channel
1271          * or packages are chosen.
1272          */
1273         np = ndp->active_package;
1274         nc = ndp->active_channel;
1275         if (!action || !np || !nc)
1276                 return NOTIFY_OK;
1277
1278         /* We needn't enable or disable it if the function isn't supported */
1279         if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1280                 return NOTIFY_OK;
1281
1282         nca.ndp = ndp;
1283         nca.req_flags = 0;
1284         nca.package = np->id;
1285         nca.channel = nc->id;
1286         nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1287         ret = ncsi_xmit_cmd(&nca);
1288         if (ret) {
1289                 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1290                             (event == NETDEV_UP) ? "enable" : "disable", ret);
1291                 return NOTIFY_DONE;
1292         }
1293
1294         return NOTIFY_OK;
1295 }
1296
1297 static struct notifier_block ncsi_inet6addr_notifier = {
1298         .notifier_call = ncsi_inet6addr_event,
1299 };
1300 #endif /* CONFIG_IPV6 */
1301
1302 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1303 {
1304         struct ncsi_dev *nd = &ndp->ndev;
1305         struct ncsi_channel *nc;
1306         struct ncsi_package *np;
1307         unsigned long flags;
1308         unsigned int n = 0;
1309
1310         NCSI_FOR_EACH_PACKAGE(ndp, np) {
1311                 NCSI_FOR_EACH_CHANNEL(np, nc) {
1312                         spin_lock_irqsave(&nc->lock, flags);
1313
1314                         /* Channels may be busy, mark dirty instead of
1315                          * kicking if;
1316                          * a) not ACTIVE (configured)
1317                          * b) in the channel_queue (to be configured)
1318                          * c) it's ndev is in the config state
1319                          */
1320                         if (nc->state != NCSI_CHANNEL_ACTIVE) {
1321                                 if ((ndp->ndev.state & 0xff00) ==
1322                                                 ncsi_dev_state_config ||
1323                                                 !list_empty(&nc->link)) {
1324                                         netdev_dbg(nd->dev,
1325                                                    "NCSI: channel %p marked dirty\n",
1326                                                    nc);
1327                                         nc->reconfigure_needed = true;
1328                                 }
1329                                 spin_unlock_irqrestore(&nc->lock, flags);
1330                                 continue;
1331                         }
1332
1333                         spin_unlock_irqrestore(&nc->lock, flags);
1334
1335                         ncsi_stop_channel_monitor(nc);
1336                         spin_lock_irqsave(&nc->lock, flags);
1337                         nc->state = NCSI_CHANNEL_INACTIVE;
1338                         spin_unlock_irqrestore(&nc->lock, flags);
1339
1340                         spin_lock_irqsave(&ndp->lock, flags);
1341                         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1342                         spin_unlock_irqrestore(&ndp->lock, flags);
1343
1344                         netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
1345                         n++;
1346                 }
1347         }
1348
1349         return n;
1350 }
1351
1352 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1353 {
1354         struct ncsi_dev_priv *ndp;
1355         unsigned int n_vids = 0;
1356         struct vlan_vid *vlan;
1357         struct ncsi_dev *nd;
1358         bool found = false;
1359
1360         if (vid == 0)
1361                 return 0;
1362
1363         nd = ncsi_find_dev(dev);
1364         if (!nd) {
1365                 netdev_warn(dev, "NCSI: No net_device?\n");
1366                 return 0;
1367         }
1368
1369         ndp = TO_NCSI_DEV_PRIV(nd);
1370
1371         /* Add the VLAN id to our internal list */
1372         list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1373                 n_vids++;
1374                 if (vlan->vid == vid) {
1375                         netdev_dbg(dev, "NCSI: vid %u already registered\n",
1376                                    vid);
1377                         return 0;
1378                 }
1379         }
1380         if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1381                 netdev_warn(dev,
1382                             "tried to add vlan id %u but NCSI max already registered (%u)\n",
1383                             vid, NCSI_MAX_VLAN_VIDS);
1384                 return -ENOSPC;
1385         }
1386
1387         vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1388         if (!vlan)
1389                 return -ENOMEM;
1390
1391         vlan->proto = proto;
1392         vlan->vid = vid;
1393         list_add_rcu(&vlan->list, &ndp->vlan_vids);
1394
1395         netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
1396
1397         found = ncsi_kick_channels(ndp) != 0;
1398
1399         return found ? ncsi_process_next_channel(ndp) : 0;
1400 }
1401 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1402
1403 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1404 {
1405         struct vlan_vid *vlan, *tmp;
1406         struct ncsi_dev_priv *ndp;
1407         struct ncsi_dev *nd;
1408         bool found = false;
1409
1410         if (vid == 0)
1411                 return 0;
1412
1413         nd = ncsi_find_dev(dev);
1414         if (!nd) {
1415                 netdev_warn(dev, "NCSI: no net_device?\n");
1416                 return 0;
1417         }
1418
1419         ndp = TO_NCSI_DEV_PRIV(nd);
1420
1421         /* Remove the VLAN id from our internal list */
1422         list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1423                 if (vlan->vid == vid) {
1424                         netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
1425                         list_del_rcu(&vlan->list);
1426                         found = true;
1427                         kfree(vlan);
1428                 }
1429
1430         if (!found) {
1431                 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1432                 return -EINVAL;
1433         }
1434
1435         found = ncsi_kick_channels(ndp) != 0;
1436
1437         return found ? ncsi_process_next_channel(ndp) : 0;
1438 }
1439 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1440
1441 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1442                                    void (*handler)(struct ncsi_dev *ndev))
1443 {
1444         struct ncsi_dev_priv *ndp;
1445         struct ncsi_dev *nd;
1446         unsigned long flags;
1447         int i;
1448
1449         /* Check if the device has been registered or not */
1450         nd = ncsi_find_dev(dev);
1451         if (nd)
1452                 return nd;
1453
1454         /* Create NCSI device */
1455         ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1456         if (!ndp)
1457                 return NULL;
1458
1459         nd = &ndp->ndev;
1460         nd->state = ncsi_dev_state_registered;
1461         nd->dev = dev;
1462         nd->handler = handler;
1463         ndp->pending_req_num = 0;
1464         INIT_LIST_HEAD(&ndp->channel_queue);
1465         INIT_LIST_HEAD(&ndp->vlan_vids);
1466         INIT_WORK(&ndp->work, ncsi_dev_work);
1467
1468         /* Initialize private NCSI device */
1469         spin_lock_init(&ndp->lock);
1470         INIT_LIST_HEAD(&ndp->packages);
1471         ndp->request_id = NCSI_REQ_START_IDX;
1472         for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1473                 ndp->requests[i].id = i;
1474                 ndp->requests[i].ndp = ndp;
1475                 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
1476         }
1477
1478         spin_lock_irqsave(&ncsi_dev_lock, flags);
1479 #if IS_ENABLED(CONFIG_IPV6)
1480         ndp->inet6_addr_num = 0;
1481         if (list_empty(&ncsi_dev_list))
1482                 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1483 #endif
1484         list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1485         spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1486
1487         /* Register NCSI packet Rx handler */
1488         ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1489         ndp->ptype.func = ncsi_rcv_rsp;
1490         ndp->ptype.dev = dev;
1491         dev_add_pack(&ndp->ptype);
1492
1493         return nd;
1494 }
1495 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1496
1497 int ncsi_start_dev(struct ncsi_dev *nd)
1498 {
1499         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1500         int ret;
1501
1502         if (nd->state != ncsi_dev_state_registered &&
1503             nd->state != ncsi_dev_state_functional)
1504                 return -ENOTTY;
1505
1506         if (!(ndp->flags & NCSI_DEV_PROBED)) {
1507                 nd->state = ncsi_dev_state_probe;
1508                 schedule_work(&ndp->work);
1509                 return 0;
1510         }
1511
1512         if (ndp->flags & NCSI_DEV_HWA) {
1513                 netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
1514                 ret = ncsi_enable_hwa(ndp);
1515         } else {
1516                 ret = ncsi_choose_active_channel(ndp);
1517         }
1518
1519         return ret;
1520 }
1521 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1522
1523 void ncsi_stop_dev(struct ncsi_dev *nd)
1524 {
1525         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1526         struct ncsi_package *np;
1527         struct ncsi_channel *nc;
1528         bool chained;
1529         int old_state;
1530         unsigned long flags;
1531
1532         /* Stop the channel monitor and reset channel's state */
1533         NCSI_FOR_EACH_PACKAGE(ndp, np) {
1534                 NCSI_FOR_EACH_CHANNEL(np, nc) {
1535                         ncsi_stop_channel_monitor(nc);
1536
1537                         spin_lock_irqsave(&nc->lock, flags);
1538                         chained = !list_empty(&nc->link);
1539                         old_state = nc->state;
1540                         nc->state = NCSI_CHANNEL_INACTIVE;
1541                         spin_unlock_irqrestore(&nc->lock, flags);
1542
1543                         WARN_ON_ONCE(chained ||
1544                                      old_state == NCSI_CHANNEL_INVISIBLE);
1545                 }
1546         }
1547
1548         netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
1549         ncsi_report_link(ndp, true);
1550 }
1551 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1552
1553 void ncsi_unregister_dev(struct ncsi_dev *nd)
1554 {
1555         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1556         struct ncsi_package *np, *tmp;
1557         unsigned long flags;
1558
1559         dev_remove_pack(&ndp->ptype);
1560
1561         list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1562                 ncsi_remove_package(np);
1563
1564         spin_lock_irqsave(&ncsi_dev_lock, flags);
1565         list_del_rcu(&ndp->node);
1566 #if IS_ENABLED(CONFIG_IPV6)
1567         if (list_empty(&ncsi_dev_list))
1568                 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1569 #endif
1570         spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1571
1572         kfree(ndp);
1573 }
1574 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);