GNU Linux-libre 4.14.265-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(unsigned long data)
69 {
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;
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         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;
211
212         spin_lock_irqsave(&np->lock, flags);
213         tmp = ncsi_find_channel(np, id);
214         if (tmp) {
215                 spin_unlock_irqrestore(&np->lock, flags);
216                 kfree(nc);
217                 return tmp;
218         }
219
220         list_add_tail_rcu(&nc->node, &np->channels);
221         np->channel_num++;
222         spin_unlock_irqrestore(&np->lock, flags);
223
224         return nc;
225 }
226
227 static void ncsi_remove_channel(struct ncsi_channel *nc)
228 {
229         struct ncsi_package *np = nc->package;
230         unsigned long flags;
231
232         spin_lock_irqsave(&nc->lock, flags);
233
234         /* Release filters */
235         kfree(nc->mac_filter.addrs);
236         kfree(nc->vlan_filter.vids);
237
238         nc->state = NCSI_CHANNEL_INACTIVE;
239         spin_unlock_irqrestore(&nc->lock, flags);
240         ncsi_stop_channel_monitor(nc);
241
242         /* Remove and free channel */
243         spin_lock_irqsave(&np->lock, flags);
244         list_del_rcu(&nc->node);
245         np->channel_num--;
246         spin_unlock_irqrestore(&np->lock, flags);
247
248         kfree(nc);
249 }
250
251 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
252                                        unsigned char id)
253 {
254         struct ncsi_package *np;
255
256         NCSI_FOR_EACH_PACKAGE(ndp, np) {
257                 if (np->id == id)
258                         return np;
259         }
260
261         return NULL;
262 }
263
264 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
265                                       unsigned char id)
266 {
267         struct ncsi_package *np, *tmp;
268         unsigned long flags;
269
270         np = kzalloc(sizeof(*np), GFP_ATOMIC);
271         if (!np)
272                 return NULL;
273
274         np->id = id;
275         np->ndp = ndp;
276         spin_lock_init(&np->lock);
277         INIT_LIST_HEAD(&np->channels);
278
279         spin_lock_irqsave(&ndp->lock, flags);
280         tmp = ncsi_find_package(ndp, id);
281         if (tmp) {
282                 spin_unlock_irqrestore(&ndp->lock, flags);
283                 kfree(np);
284                 return tmp;
285         }
286
287         list_add_tail_rcu(&np->node, &ndp->packages);
288         ndp->package_num++;
289         spin_unlock_irqrestore(&ndp->lock, flags);
290
291         return np;
292 }
293
294 void ncsi_remove_package(struct ncsi_package *np)
295 {
296         struct ncsi_dev_priv *ndp = np->ndp;
297         struct ncsi_channel *nc, *tmp;
298         unsigned long flags;
299
300         /* Release all child channels */
301         list_for_each_entry_safe(nc, tmp, &np->channels, node)
302                 ncsi_remove_channel(nc);
303
304         /* Remove and free package */
305         spin_lock_irqsave(&ndp->lock, flags);
306         list_del_rcu(&np->node);
307         ndp->package_num--;
308         spin_unlock_irqrestore(&ndp->lock, flags);
309
310         kfree(np);
311 }
312
313 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
314                                    unsigned char id,
315                                    struct ncsi_package **np,
316                                    struct ncsi_channel **nc)
317 {
318         struct ncsi_package *p;
319         struct ncsi_channel *c;
320
321         p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
322         c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
323
324         if (np)
325                 *np = p;
326         if (nc)
327                 *nc = c;
328 }
329
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.
333  */
334 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
335                                         unsigned int req_flags)
336 {
337         struct ncsi_request *nr = NULL;
338         int i, limit = ARRAY_SIZE(ndp->requests);
339         unsigned long flags;
340
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)
345                         continue;
346
347                 nr = &ndp->requests[i];
348                 nr->used = true;
349                 nr->flags = req_flags;
350                 ndp->request_id = i + 1;
351                 goto found;
352         }
353
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)
357                         continue;
358
359                 nr = &ndp->requests[i];
360                 nr->used = true;
361                 nr->flags = req_flags;
362                 ndp->request_id = i + 1;
363                 goto found;
364         }
365
366 found:
367         spin_unlock_irqrestore(&ndp->lock, flags);
368         return nr;
369 }
370
371 void ncsi_free_request(struct ncsi_request *nr)
372 {
373         struct ncsi_dev_priv *ndp = nr->ndp;
374         struct sk_buff *cmd, *rsp;
375         unsigned long flags;
376         bool driven;
377
378         if (nr->enabled) {
379                 nr->enabled = false;
380                 del_timer_sync(&nr->timer);
381         }
382
383         spin_lock_irqsave(&ndp->lock, flags);
384         cmd = nr->cmd;
385         rsp = nr->rsp;
386         nr->cmd = NULL;
387         nr->rsp = NULL;
388         nr->used = false;
389         driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
390         spin_unlock_irqrestore(&ndp->lock, flags);
391
392         if (driven && cmd && --ndp->pending_req_num == 0)
393                 schedule_work(&ndp->work);
394
395         /* Release command and response */
396         consume_skb(cmd);
397         consume_skb(rsp);
398 }
399
400 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
401 {
402         struct ncsi_dev_priv *ndp;
403
404         NCSI_FOR_EACH_DEV(ndp) {
405                 if (ndp->ndev.dev == dev)
406                         return &ndp->ndev;
407         }
408
409         return NULL;
410 }
411
412 static void ncsi_request_timeout(unsigned long data)
413 {
414         struct ncsi_request *nr = (struct ncsi_request *)data;
415         struct ncsi_dev_priv *ndp = nr->ndp;
416         unsigned long flags;
417
418         /* If the request already had associated response,
419          * let the response handler to release it.
420          */
421         spin_lock_irqsave(&ndp->lock, flags);
422         nr->enabled = false;
423         if (nr->rsp || !nr->cmd) {
424                 spin_unlock_irqrestore(&ndp->lock, flags);
425                 return;
426         }
427         spin_unlock_irqrestore(&ndp->lock, flags);
428
429         /* Release the request */
430         ncsi_free_request(nr);
431 }
432
433 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
434 {
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;
439         unsigned long flags;
440         int ret;
441
442         nca.ndp = ndp;
443         nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
444         switch (nd->state) {
445         case ncsi_dev_state_suspend:
446                 nd->state = ncsi_dev_state_suspend_select;
447                 /* Fall through */
448         case ncsi_dev_state_suspend_select:
449                 ndp->pending_req_num = 1;
450
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)
455                         nca.bytes[0] = 0;
456                 else
457                         nca.bytes[0] = 1;
458
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.
466                  */
467                 if (ndp->flags & NCSI_DEV_RESHUFFLE)
468                         nd->state = ncsi_dev_state_suspend_gls;
469                 else
470                         nd->state = ncsi_dev_state_suspend_dcnt;
471                 ret = ncsi_xmit_cmd(&nca);
472                 if (ret)
473                         goto error;
474
475                 break;
476         case ncsi_dev_state_suspend_gls:
477                 ndp->pending_req_num = np->channel_num;
478
479                 nca.type = NCSI_PKT_CMD_GLS;
480                 nca.package = np->id;
481
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);
486                         if (ret)
487                                 goto error;
488                 }
489
490                 break;
491         case ncsi_dev_state_suspend_dcnt:
492                 ndp->pending_req_num = 1;
493
494                 nca.type = NCSI_PKT_CMD_DCNT;
495                 nca.package = np->id;
496                 nca.channel = nc->id;
497
498                 nd->state = ncsi_dev_state_suspend_dc;
499                 ret = ncsi_xmit_cmd(&nca);
500                 if (ret)
501                         goto error;
502
503                 break;
504         case ncsi_dev_state_suspend_dc:
505                 ndp->pending_req_num = 1;
506
507                 nca.type = NCSI_PKT_CMD_DC;
508                 nca.package = np->id;
509                 nca.channel = nc->id;
510                 nca.bytes[0] = 1;
511
512                 nd->state = ncsi_dev_state_suspend_deselect;
513                 ret = ncsi_xmit_cmd(&nca);
514                 if (ret)
515                         goto error;
516
517                 break;
518         case ncsi_dev_state_suspend_deselect:
519                 ndp->pending_req_num = 1;
520
521                 nca.type = NCSI_PKT_CMD_DP;
522                 nca.package = np->id;
523                 nca.channel = NCSI_RESERVED_CHANNEL;
524
525                 nd->state = ncsi_dev_state_suspend_done;
526                 ret = ncsi_xmit_cmd(&nca);
527                 if (ret)
528                         goto error;
529
530                 break;
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);
536
537                 break;
538         default:
539                 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
540                             nd->state);
541         }
542
543         return;
544 error:
545         nd->state = ncsi_dev_state_functional;
546 }
547
548 /* Check the VLAN filter bitmap for a set filter, and construct a
549  * "Set VLAN Filter - Disable" packet if found.
550  */
551 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
552                          struct ncsi_cmd_arg *nca)
553 {
554         struct ncsi_channel_vlan_filter *ncf;
555         unsigned long flags;
556         void *bitmap;
557         int index;
558         u16 vid;
559
560         ncf = &nc->vlan_filter;
561         bitmap = &ncf->bitmap;
562
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);
567                 return -1;
568         }
569         vid = ncf->vids[index];
570
571         clear_bit(index, bitmap);
572         ncf->vids[index] = 0;
573         spin_unlock_irqrestore(&nc->lock, flags);
574
575         nca->type = NCSI_PKT_CMD_SVF;
576         nca->words[1] = vid;
577         /* HW filter index starts at 1 */
578         nca->bytes[6] = index + 1;
579         nca->bytes[7] = 0x00;
580         return 0;
581 }
582
583 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
584  * packet.
585  */
586 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
587                        struct ncsi_cmd_arg *nca)
588 {
589         struct ncsi_channel_vlan_filter *ncf;
590         struct vlan_vid *vlan = NULL;
591         unsigned long flags;
592         int i, index;
593         void *bitmap;
594         u16 vid;
595
596         if (list_empty(&ndp->vlan_vids))
597                 return -1;
598
599         ncf = &nc->vlan_filter;
600         bitmap = &ncf->bitmap;
601
602         spin_lock_irqsave(&nc->lock, flags);
603
604         rcu_read_lock();
605         list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
606                 vid = vlan->vid;
607                 for (i = 0; i < ncf->n_vids; i++)
608                         if (ncf->vids[i] == vid) {
609                                 vid = 0;
610                                 break;
611                         }
612                 if (vid)
613                         break;
614         }
615         rcu_read_unlock();
616
617         if (!vid) {
618                 /* No VLAN ID is not set */
619                 spin_unlock_irqrestore(&nc->lock, flags);
620                 return -1;
621         }
622
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",
627                            nc->id);
628                 spin_unlock_irqrestore(&nc->lock, flags);
629                 return -1;
630         }
631
632         ncf->vids[index] = vid;
633         set_bit(index, bitmap);
634         spin_unlock_irqrestore(&nc->lock, flags);
635
636         nca->type = NCSI_PKT_CMD_SVF;
637         nca->words[1] = vid;
638         /* HW filter index starts at 1 */
639         nca->bytes[6] = index + 1;
640         nca->bytes[7] = 0x01;
641
642         return 0;
643 }
644
645 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
646 {
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;
653         unsigned char index;
654         unsigned long flags;
655         int ret;
656
657         nca.ndp = ndp;
658         nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
659         switch (nd->state) {
660         case ncsi_dev_state_config:
661         case ncsi_dev_state_config_sp:
662                 ndp->pending_req_num = 1;
663
664                 /* Select the specific package */
665                 nca.type = NCSI_PKT_CMD_SP;
666                 if (ndp->flags & NCSI_DEV_HWA)
667                         nca.bytes[0] = 0;
668                 else
669                         nca.bytes[0] = 1;
670                 nca.package = np->id;
671                 nca.channel = NCSI_RESERVED_CHANNEL;
672                 ret = ncsi_xmit_cmd(&nca);
673                 if (ret) {
674                         netdev_err(ndp->ndev.dev,
675                                    "NCSI: Failed to transmit CMD_SP\n");
676                         goto error;
677                 }
678
679                 nd->state = ncsi_dev_state_config_cis;
680                 break;
681         case ncsi_dev_state_config_cis:
682                 ndp->pending_req_num = 1;
683
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);
689                 if (ret) {
690                         netdev_err(ndp->ndev.dev,
691                                    "NCSI: Failed to transmit CMD_CIS\n");
692                         goto error;
693                 }
694
695                 nd->state = ncsi_dev_state_config_clear_vids;
696                 break;
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:
704 #endif
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;
710
711                 nca.package = np->id;
712                 nca.channel = nc->id;
713
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);
717                         if (ret) {
718                                 nd->state = ncsi_dev_state_config_svf;
719                                 schedule_work(&ndp->work);
720                                 break;
721                         }
722                         /* Repeat */
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);
727                         if (ret) {
728                                 nd->state = ncsi_dev_state_config_ev;
729                                 schedule_work(&ndp->work);
730                                 break;
731                         }
732                         /* Repeat */
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;
738                         } else {
739                                 nca.type = NCSI_PKT_CMD_EV;
740                                 nca.bytes[3] = NCSI_CAP_VLAN_NO;
741                         }
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
746                  * 0.
747                  */
748                         nca.type = NCSI_PKT_CMD_SMA;
749                         for (index = 0; index < 6; index++)
750                                 nca.bytes[index] = dev->dev_addr[index];
751                         nca.bytes[6] = 0x1;
752                         nca.bytes[7] = 0x1;
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;
763                         else
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;
781                         nca.bytes[0] = 0;
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;
787                 }
788
789                 ret = ncsi_xmit_cmd(&nca);
790                 if (ret) {
791                         netdev_err(ndp->ndev.dev,
792                                    "NCSI: Failed to transmit CMD %x\n",
793                                    nca.type);
794                         goto error;
795                 }
796                 break;
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
805                          */
806                         nc->reconfigure_needed = false;
807                         nc->state = NCSI_CHANNEL_INACTIVE;
808                         spin_unlock_irqrestore(&nc->lock, flags);
809
810                         spin_lock_irqsave(&ndp->lock, flags);
811                         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
812                         spin_unlock_irqrestore(&ndp->lock, flags);
813
814                         netdev_printk(KERN_DEBUG, dev,
815                                       "Dirty NCSI channel state reset\n");
816                         ncsi_process_next_channel(ndp);
817                         break;
818                 }
819
820                 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
821                         hot_nc = nc;
822                         nc->state = NCSI_CHANNEL_ACTIVE;
823                 } else {
824                         hot_nc = NULL;
825                         nc->state = NCSI_CHANNEL_INACTIVE;
826                         netdev_warn(ndp->ndev.dev,
827                                     "NCSI: channel %u link down after config\n",
828                                     nc->id);
829                 }
830                 spin_unlock_irqrestore(&nc->lock, flags);
831
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);
836
837                 ncsi_start_channel_monitor(nc);
838                 ncsi_process_next_channel(ndp);
839                 break;
840         default:
841                 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
842                              nd->state);
843         }
844
845         return;
846
847 error:
848         ncsi_report_link(ndp, true);
849 }
850
851 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
852 {
853         struct ncsi_package *np, *force_package;
854         struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
855         struct ncsi_channel_mode *ncm;
856         unsigned long flags;
857
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);
863
864         /* Force a specific channel whether or not it has link if we have been
865          * configured to do so
866          */
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",
873                                     found->id);
874                 goto out;
875         }
876
877         /* The search is done once an inactive channel with up
878          * link is found.
879          */
880         found = NULL;
881         NCSI_FOR_EACH_PACKAGE(ndp, np) {
882                 if (ndp->force_package && np != ndp->force_package)
883                         continue;
884                 NCSI_FOR_EACH_CHANNEL(np, nc) {
885                         spin_lock_irqsave(&nc->lock, flags);
886
887                         if (!list_empty(&nc->link) ||
888                             nc->state != NCSI_CHANNEL_INACTIVE) {
889                                 spin_unlock_irqrestore(&nc->lock, flags);
890                                 continue;
891                         }
892
893                         if (!found)
894                                 found = nc;
895
896                         if (nc == hot_nc)
897                                 found = nc;
898
899                         ncm = &nc->modes[NCSI_MODE_LINK];
900                         if (ncm->data[2] & 0x1) {
901                                 spin_unlock_irqrestore(&nc->lock, flags);
902                                 found = nc;
903                                 goto out;
904                         }
905
906                         spin_unlock_irqrestore(&nc->lock, flags);
907                 }
908         }
909
910         if (!found) {
911                 netdev_warn(ndp->ndev.dev,
912                             "NCSI: No channel found with link\n");
913                 ncsi_report_link(ndp, true);
914                 return -ENODEV;
915         }
916
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");
921
922 out:
923         spin_lock_irqsave(&ndp->lock, flags);
924         list_add_tail_rcu(&found->link, &ndp->channel_queue);
925         spin_unlock_irqrestore(&ndp->lock, flags);
926
927         return ncsi_process_next_channel(ndp);
928 }
929
930 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
931 {
932         struct ncsi_package *np;
933         struct ncsi_channel *nc;
934         unsigned int cap;
935         bool has_channel = false;
936
937         /* The hardware arbitration is disabled if any one channel
938          * doesn't support explicitly.
939          */
940         NCSI_FOR_EACH_PACKAGE(ndp, np) {
941                 NCSI_FOR_EACH_CHANNEL(np, nc) {
942                         has_channel = true;
943
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;
949                                 return false;
950                         }
951                 }
952         }
953
954         if (has_channel) {
955                 ndp->flags |= NCSI_DEV_HWA;
956                 return true;
957         }
958
959         ndp->flags &= ~NCSI_DEV_HWA;
960         return false;
961 }
962
963 static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
964 {
965         struct ncsi_package *np;
966         struct ncsi_channel *nc;
967         unsigned long flags;
968
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);
977                 }
978         }
979         spin_unlock_irqrestore(&ndp->lock, flags);
980
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);
986                 return -ENOENT;
987         }
988
989         return ncsi_process_next_channel(ndp);
990 }
991
992 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
993 {
994         struct ncsi_dev *nd = &ndp->ndev;
995         struct ncsi_package *np;
996         struct ncsi_channel *nc;
997         struct ncsi_cmd_arg nca;
998         unsigned char index;
999         int ret;
1000
1001         nca.ndp = ndp;
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;
1006                 /* Fall through */
1007         case ncsi_dev_state_probe_deselect:
1008                 ndp->pending_req_num = 8;
1009
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);
1016                         if (ret)
1017                                 goto error;
1018                 }
1019
1020                 nd->state = ncsi_dev_state_probe_package;
1021                 break;
1022         case ncsi_dev_state_probe_package:
1023                 ndp->pending_req_num = 16;
1024
1025                 /* Select all possible packages */
1026                 nca.type = NCSI_PKT_CMD_SP;
1027                 nca.bytes[0] = 1;
1028                 nca.channel = NCSI_RESERVED_CHANNEL;
1029                 for (index = 0; index < 8; index++) {
1030                         nca.package = index;
1031                         ret = ncsi_xmit_cmd(&nca);
1032                         if (ret)
1033                                 goto error;
1034                 }
1035
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);
1041                         if (ret)
1042                                 goto error;
1043                 }
1044
1045                 nd->state = ncsi_dev_state_probe_channel;
1046                 break;
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,
1052                                       &ndp->packages))
1053                         ndp->active_package = NULL;
1054                 else
1055                         ndp->active_package = list_next_entry(
1056                                 ndp->active_package, node);
1057
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
1061                  * the enumeration.
1062                  *
1063                  * We have to choose an active channel before configuring it.
1064                  * Note that we possibly don't have active channel in extreme
1065                  * situation.
1066                  */
1067                 if (!ndp->active_package) {
1068                         ndp->flags |= NCSI_DEV_PROBED;
1069                         if (ncsi_check_hwa(ndp))
1070                                 ncsi_enable_hwa(ndp);
1071                         else
1072                                 ncsi_choose_active_channel(ndp);
1073                         return;
1074                 }
1075
1076                 /* Select the active package */
1077                 ndp->pending_req_num = 1;
1078                 nca.type = NCSI_PKT_CMD_SP;
1079                 nca.bytes[0] = 1;
1080                 nca.package = ndp->active_package->id;
1081                 nca.channel = NCSI_RESERVED_CHANNEL;
1082                 ret = ncsi_xmit_cmd(&nca);
1083                 if (ret)
1084                         goto error;
1085
1086                 nd->state = ncsi_dev_state_probe_cis;
1087                 break;
1088         case ncsi_dev_state_probe_cis:
1089                 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
1090
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);
1097                         if (ret)
1098                                 goto error;
1099                 }
1100
1101                 nd->state = ncsi_dev_state_probe_gvi;
1102                 break;
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;
1108
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;
1114                 else
1115                         nca.type = NCSI_PKT_CMD_GLS;
1116
1117                 nca.package = np->id;
1118                 NCSI_FOR_EACH_CHANNEL(np, nc) {
1119                         nca.channel = nc->id;
1120                         ret = ncsi_xmit_cmd(&nca);
1121                         if (ret)
1122                                 goto error;
1123                 }
1124
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;
1129                 else
1130                         nd->state = ncsi_dev_state_probe_dp;
1131                 break;
1132         case ncsi_dev_state_probe_dp:
1133                 ndp->pending_req_num = 1;
1134
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);
1140                 if (ret)
1141                         goto error;
1142
1143                 /* Scan channels in next package */
1144                 nd->state = ncsi_dev_state_probe_channel;
1145                 break;
1146         default:
1147                 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1148                             nd->state);
1149         }
1150
1151         return;
1152 error:
1153         netdev_err(ndp->ndev.dev,
1154                    "NCSI: Failed to transmit cmd 0x%x during probe\n",
1155                    nca.type);
1156         ncsi_report_link(ndp, true);
1157 }
1158
1159 static void ncsi_dev_work(struct work_struct *work)
1160 {
1161         struct ncsi_dev_priv *ndp = container_of(work,
1162                         struct ncsi_dev_priv, work);
1163         struct ncsi_dev *nd = &ndp->ndev;
1164
1165         switch (nd->state & ncsi_dev_state_major) {
1166         case ncsi_dev_state_probe:
1167                 ncsi_probe_channel(ndp);
1168                 break;
1169         case ncsi_dev_state_suspend:
1170                 ncsi_suspend_channel(ndp);
1171                 break;
1172         case ncsi_dev_state_config:
1173                 ncsi_configure_channel(ndp);
1174                 break;
1175         default:
1176                 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1177                             nd->state);
1178         }
1179 }
1180
1181 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1182 {
1183         struct ncsi_channel *nc;
1184         int old_state;
1185         unsigned long flags;
1186
1187         spin_lock_irqsave(&ndp->lock, flags);
1188         nc = list_first_or_null_rcu(&ndp->channel_queue,
1189                                     struct ncsi_channel, link);
1190         if (!nc) {
1191                 spin_unlock_irqrestore(&ndp->lock, flags);
1192                 goto out;
1193         }
1194
1195         list_del_init(&nc->link);
1196         spin_unlock_irqrestore(&ndp->lock, flags);
1197
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);
1202
1203         ndp->active_channel = nc;
1204         ndp->active_package = nc->package;
1205
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",
1210                             nc->id);
1211                 ncsi_configure_channel(ndp);
1212                 break;
1213         case NCSI_CHANNEL_ACTIVE:
1214                 ndp->ndev.state = ncsi_dev_state_suspend;
1215                 netdev_info(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1216                             nc->id);
1217                 ncsi_suspend_channel(ndp);
1218                 break;
1219         default:
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);
1223                 return -EINVAL;
1224         }
1225
1226         return 0;
1227
1228 out:
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);
1234         }
1235
1236         netdev_printk(KERN_DEBUG, ndp->ndev.dev,
1237                       "NCSI: No more channels to process\n");
1238         ncsi_report_link(ndp, false);
1239         return -ENODEV;
1240 }
1241
1242 #if IS_ENABLED(CONFIG_IPV6)
1243 static int ncsi_inet6addr_event(struct notifier_block *this,
1244                                 unsigned long event, void *data)
1245 {
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;
1253         bool action;
1254         int ret;
1255
1256         if (!ndp || (ipv6_addr_type(&ifa->addr) &
1257             (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1258                 return NOTIFY_OK;
1259
1260         switch (event) {
1261         case NETDEV_UP:
1262                 action = (++ndp->inet6_addr_num) == 1;
1263                 nca.type = NCSI_PKT_CMD_EGMF;
1264                 break;
1265         case NETDEV_DOWN:
1266                 action = (--ndp->inet6_addr_num == 0);
1267                 nca.type = NCSI_PKT_CMD_DGMF;
1268                 break;
1269         default:
1270                 return NOTIFY_OK;
1271         }
1272
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.
1276          */
1277         np = ndp->active_package;
1278         nc = ndp->active_channel;
1279         if (!action || !np || !nc)
1280                 return NOTIFY_OK;
1281
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))
1284                 return NOTIFY_OK;
1285
1286         nca.ndp = ndp;
1287         nca.req_flags = 0;
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);
1292         if (ret) {
1293                 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1294                             (event == NETDEV_UP) ? "enable" : "disable", ret);
1295                 return NOTIFY_DONE;
1296         }
1297
1298         return NOTIFY_OK;
1299 }
1300
1301 static struct notifier_block ncsi_inet6addr_notifier = {
1302         .notifier_call = ncsi_inet6addr_event,
1303 };
1304 #endif /* CONFIG_IPV6 */
1305
1306 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1307 {
1308         struct ncsi_dev *nd = &ndp->ndev;
1309         struct ncsi_channel *nc;
1310         struct ncsi_package *np;
1311         unsigned long flags;
1312         unsigned int n = 0;
1313
1314         NCSI_FOR_EACH_PACKAGE(ndp, np) {
1315                 NCSI_FOR_EACH_CHANNEL(np, nc) {
1316                         spin_lock_irqsave(&nc->lock, flags);
1317
1318                         /* Channels may be busy, mark dirty instead of
1319                          * kicking if;
1320                          * a) not ACTIVE (configured)
1321                          * b) in the channel_queue (to be configured)
1322                          * c) it's ndev is in the config state
1323                          */
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",
1330                                                       nc);
1331                                         nc->reconfigure_needed = true;
1332                                 }
1333                                 spin_unlock_irqrestore(&nc->lock, flags);
1334                                 continue;
1335                         }
1336
1337                         spin_unlock_irqrestore(&nc->lock, flags);
1338
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);
1343
1344                         spin_lock_irqsave(&ndp->lock, flags);
1345                         list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1346                         spin_unlock_irqrestore(&ndp->lock, flags);
1347
1348                         netdev_printk(KERN_DEBUG, nd->dev,
1349                                       "NCSI: kicked channel %p\n", nc);
1350                         n++;
1351                 }
1352         }
1353
1354         return n;
1355 }
1356
1357 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1358 {
1359         struct ncsi_dev_priv *ndp;
1360         unsigned int n_vids = 0;
1361         struct vlan_vid *vlan;
1362         struct ncsi_dev *nd;
1363         bool found = false;
1364
1365         if (vid == 0)
1366                 return 0;
1367
1368         nd = ncsi_find_dev(dev);
1369         if (!nd) {
1370                 netdev_warn(dev, "NCSI: No net_device?\n");
1371                 return 0;
1372         }
1373
1374         ndp = TO_NCSI_DEV_PRIV(nd);
1375
1376         /* Add the VLAN id to our internal list */
1377         list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1378                 n_vids++;
1379                 if (vlan->vid == vid) {
1380                         netdev_printk(KERN_DEBUG, dev,
1381                                       "NCSI: vid %u already registered\n", vid);
1382                         return 0;
1383                 }
1384         }
1385         if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1386                 netdev_warn(dev,
1387                             "tried to add vlan id %u but NCSI max already registered (%u)\n",
1388                             vid, NCSI_MAX_VLAN_VIDS);
1389                 return -ENOSPC;
1390         }
1391
1392         vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1393         if (!vlan)
1394                 return -ENOMEM;
1395
1396         vlan->proto = proto;
1397         vlan->vid = vid;
1398         list_add_rcu(&vlan->list, &ndp->vlan_vids);
1399
1400         netdev_printk(KERN_DEBUG, dev, "NCSI: Added new vid %u\n", vid);
1401
1402         found = ncsi_kick_channels(ndp) != 0;
1403
1404         return found ? ncsi_process_next_channel(ndp) : 0;
1405 }
1406 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1407
1408 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1409 {
1410         struct vlan_vid *vlan, *tmp;
1411         struct ncsi_dev_priv *ndp;
1412         struct ncsi_dev *nd;
1413         bool found = false;
1414
1415         if (vid == 0)
1416                 return 0;
1417
1418         nd = ncsi_find_dev(dev);
1419         if (!nd) {
1420                 netdev_warn(dev, "NCSI: no net_device?\n");
1421                 return 0;
1422         }
1423
1424         ndp = TO_NCSI_DEV_PRIV(nd);
1425
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);
1432                         found = true;
1433                         kfree(vlan);
1434                 }
1435
1436         if (!found) {
1437                 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1438                 return -EINVAL;
1439         }
1440
1441         found = ncsi_kick_channels(ndp) != 0;
1442
1443         return found ? ncsi_process_next_channel(ndp) : 0;
1444 }
1445 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1446
1447 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1448                                    void (*handler)(struct ncsi_dev *ndev))
1449 {
1450         struct ncsi_dev_priv *ndp;
1451         struct ncsi_dev *nd;
1452         unsigned long flags;
1453         int i;
1454
1455         /* Check if the device has been registered or not */
1456         nd = ncsi_find_dev(dev);
1457         if (nd)
1458                 return nd;
1459
1460         /* Create NCSI device */
1461         ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1462         if (!ndp)
1463                 return NULL;
1464
1465         nd = &ndp->ndev;
1466         nd->state = ncsi_dev_state_registered;
1467         nd->dev = dev;
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);
1473
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]);
1484         }
1485
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);
1491 #endif
1492         list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1493         spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1494
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);
1500
1501         /* Set up generic netlink interface */
1502         ncsi_init_netlink(dev);
1503
1504         return nd;
1505 }
1506 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1507
1508 int ncsi_start_dev(struct ncsi_dev *nd)
1509 {
1510         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1511         int ret;
1512
1513         if (nd->state != ncsi_dev_state_registered &&
1514             nd->state != ncsi_dev_state_functional)
1515                 return -ENOTTY;
1516
1517         if (!(ndp->flags & NCSI_DEV_PROBED)) {
1518                 nd->state = ncsi_dev_state_probe;
1519                 schedule_work(&ndp->work);
1520                 return 0;
1521         }
1522
1523         if (ndp->flags & NCSI_DEV_HWA) {
1524                 netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
1525                 ret = ncsi_enable_hwa(ndp);
1526         } else {
1527                 ret = ncsi_choose_active_channel(ndp);
1528         }
1529
1530         return ret;
1531 }
1532 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1533
1534 void ncsi_stop_dev(struct ncsi_dev *nd)
1535 {
1536         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1537         struct ncsi_package *np;
1538         struct ncsi_channel *nc;
1539         bool chained;
1540         int old_state;
1541         unsigned long flags;
1542
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);
1547
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);
1553
1554                         WARN_ON_ONCE(chained ||
1555                                      old_state == NCSI_CHANNEL_INVISIBLE);
1556                 }
1557         }
1558
1559         netdev_printk(KERN_DEBUG, ndp->ndev.dev, "NCSI: Stopping device\n");
1560         ncsi_report_link(ndp, true);
1561 }
1562 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1563
1564 void ncsi_unregister_dev(struct ncsi_dev *nd)
1565 {
1566         struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1567         struct ncsi_package *np, *tmp;
1568         unsigned long flags;
1569
1570         dev_remove_pack(&ndp->ptype);
1571
1572         list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1573                 ncsi_remove_package(np);
1574
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);
1580 #endif
1581         spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1582
1583         ncsi_unregister_netlink(nd->dev);
1584
1585         kfree(ndp);
1586 }
1587 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);