1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright Gavin Shan, IBM Corporation 2016.
4 */
5
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13
14 #include <net/ncsi.h>
15 #include <net/net_namespace.h>
16 #include <net/sock.h>
17 #include <net/addrconf.h>
18 #include <net/ipv6.h>
19 #include <net/genetlink.h>
20
21 #include "internal.h"
22 #include "ncsi-pkt.h"
23 #include "ncsi-netlink.h"
24
25 LIST_HEAD(ncsi_dev_list);
26 DEFINE_SPINLOCK(ncsi_dev_lock);
27
ncsi_channel_has_link(struct ncsi_channel * channel)28 bool ncsi_channel_has_link(struct ncsi_channel *channel)
29 {
30 return !!(channel->modes[NCSI_MODE_LINK].data[2] & 0x1);
31 }
32
ncsi_channel_is_last(struct ncsi_dev_priv * ndp,struct ncsi_channel * channel)33 bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp,
34 struct ncsi_channel *channel)
35 {
36 struct ncsi_package *np;
37 struct ncsi_channel *nc;
38
39 NCSI_FOR_EACH_PACKAGE(ndp, np)
40 NCSI_FOR_EACH_CHANNEL(np, nc) {
41 if (nc == channel)
42 continue;
43 if (nc->state == NCSI_CHANNEL_ACTIVE &&
44 ncsi_channel_has_link(nc))
45 return false;
46 }
47
48 return true;
49 }
50
ncsi_report_link(struct ncsi_dev_priv * ndp,bool force_down)51 static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
52 {
53 struct ncsi_dev *nd = &ndp->ndev;
54 struct ncsi_package *np;
55 struct ncsi_channel *nc;
56 unsigned long flags;
57
58 nd->state = ncsi_dev_state_functional;
59 if (force_down) {
60 nd->link_up = 0;
61 goto report;
62 }
63
64 nd->link_up = 0;
65 NCSI_FOR_EACH_PACKAGE(ndp, np) {
66 NCSI_FOR_EACH_CHANNEL(np, nc) {
67 spin_lock_irqsave(&nc->lock, flags);
68
69 if (!list_empty(&nc->link) ||
70 nc->state != NCSI_CHANNEL_ACTIVE) {
71 spin_unlock_irqrestore(&nc->lock, flags);
72 continue;
73 }
74
75 if (ncsi_channel_has_link(nc)) {
76 spin_unlock_irqrestore(&nc->lock, flags);
77 nd->link_up = 1;
78 goto report;
79 }
80
81 spin_unlock_irqrestore(&nc->lock, flags);
82 }
83 }
84
85 report:
86 nd->handler(nd);
87 }
88
ncsi_channel_monitor(struct timer_list * t)89 static void ncsi_channel_monitor(struct timer_list *t)
90 {
91 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
92 struct ncsi_package *np = nc->package;
93 struct ncsi_dev_priv *ndp = np->ndp;
94 struct ncsi_channel_mode *ncm;
95 struct ncsi_cmd_arg nca;
96 bool enabled, chained;
97 unsigned int monitor_state;
98 unsigned long flags;
99 int state, ret;
100
101 spin_lock_irqsave(&nc->lock, flags);
102 state = nc->state;
103 chained = !list_empty(&nc->link);
104 enabled = nc->monitor.enabled;
105 monitor_state = nc->monitor.state;
106 spin_unlock_irqrestore(&nc->lock, flags);
107
108 if (!enabled)
109 return; /* expected race disabling timer */
110 if (WARN_ON_ONCE(chained))
111 goto bad_state;
112
113 if (state != NCSI_CHANNEL_INACTIVE &&
114 state != NCSI_CHANNEL_ACTIVE) {
115 bad_state:
116 netdev_warn(ndp->ndev.dev,
117 "Bad NCSI monitor state channel %d 0x%x %s queue\n",
118 nc->id, state, chained ? "on" : "off");
119 spin_lock_irqsave(&nc->lock, flags);
120 nc->monitor.enabled = false;
121 spin_unlock_irqrestore(&nc->lock, flags);
122 return;
123 }
124
125 switch (monitor_state) {
126 case NCSI_CHANNEL_MONITOR_START:
127 case NCSI_CHANNEL_MONITOR_RETRY:
128 nca.ndp = ndp;
129 nca.package = np->id;
130 nca.channel = nc->id;
131 nca.type = NCSI_PKT_CMD_GLS;
132 nca.req_flags = 0;
133 ret = ncsi_xmit_cmd(&nca);
134 if (ret)
135 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
136 ret);
137 break;
138 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
139 break;
140 default:
141 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
142 nc->id);
143 ncsi_report_link(ndp, true);
144 ndp->flags |= NCSI_DEV_RESHUFFLE;
145
146 ncm = &nc->modes[NCSI_MODE_LINK];
147 spin_lock_irqsave(&nc->lock, flags);
148 nc->monitor.enabled = false;
149 nc->state = NCSI_CHANNEL_INVISIBLE;
150 ncm->data[2] &= ~0x1;
151 spin_unlock_irqrestore(&nc->lock, flags);
152
153 spin_lock_irqsave(&ndp->lock, flags);
154 nc->state = NCSI_CHANNEL_ACTIVE;
155 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
156 spin_unlock_irqrestore(&ndp->lock, flags);
157 ncsi_process_next_channel(ndp);
158 return;
159 }
160
161 spin_lock_irqsave(&nc->lock, flags);
162 nc->monitor.state++;
163 spin_unlock_irqrestore(&nc->lock, flags);
164 mod_timer(&nc->monitor.timer, jiffies + HZ);
165 }
166
ncsi_start_channel_monitor(struct ncsi_channel * nc)167 void ncsi_start_channel_monitor(struct ncsi_channel *nc)
168 {
169 unsigned long flags;
170
171 spin_lock_irqsave(&nc->lock, flags);
172 WARN_ON_ONCE(nc->monitor.enabled);
173 nc->monitor.enabled = true;
174 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
175 spin_unlock_irqrestore(&nc->lock, flags);
176
177 mod_timer(&nc->monitor.timer, jiffies + HZ);
178 }
179
ncsi_stop_channel_monitor(struct ncsi_channel * nc)180 void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
181 {
182 unsigned long flags;
183
184 spin_lock_irqsave(&nc->lock, flags);
185 if (!nc->monitor.enabled) {
186 spin_unlock_irqrestore(&nc->lock, flags);
187 return;
188 }
189 nc->monitor.enabled = false;
190 spin_unlock_irqrestore(&nc->lock, flags);
191
192 del_timer_sync(&nc->monitor.timer);
193 }
194
ncsi_find_channel(struct ncsi_package * np,unsigned char id)195 struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
196 unsigned char id)
197 {
198 struct ncsi_channel *nc;
199
200 NCSI_FOR_EACH_CHANNEL(np, nc) {
201 if (nc->id == id)
202 return nc;
203 }
204
205 return NULL;
206 }
207
ncsi_add_channel(struct ncsi_package * np,unsigned char id)208 struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
209 {
210 struct ncsi_channel *nc, *tmp;
211 int index;
212 unsigned long flags;
213
214 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
215 if (!nc)
216 return NULL;
217
218 nc->id = id;
219 nc->package = np;
220 nc->state = NCSI_CHANNEL_INACTIVE;
221 nc->monitor.enabled = false;
222 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
223 spin_lock_init(&nc->lock);
224 INIT_LIST_HEAD(&nc->link);
225 for (index = 0; index < NCSI_CAP_MAX; index++)
226 nc->caps[index].index = index;
227 for (index = 0; index < NCSI_MODE_MAX; index++)
228 nc->modes[index].index = index;
229
230 spin_lock_irqsave(&np->lock, flags);
231 tmp = ncsi_find_channel(np, id);
232 if (tmp) {
233 spin_unlock_irqrestore(&np->lock, flags);
234 kfree(nc);
235 return tmp;
236 }
237
238 list_add_tail_rcu(&nc->node, &np->channels);
239 np->channel_num++;
240 spin_unlock_irqrestore(&np->lock, flags);
241
242 return nc;
243 }
244
ncsi_remove_channel(struct ncsi_channel * nc)245 static void ncsi_remove_channel(struct ncsi_channel *nc)
246 {
247 struct ncsi_package *np = nc->package;
248 unsigned long flags;
249
250 spin_lock_irqsave(&nc->lock, flags);
251
252 /* Release filters */
253 kfree(nc->mac_filter.addrs);
254 kfree(nc->vlan_filter.vids);
255
256 nc->state = NCSI_CHANNEL_INACTIVE;
257 spin_unlock_irqrestore(&nc->lock, flags);
258 ncsi_stop_channel_monitor(nc);
259
260 /* Remove and free channel */
261 spin_lock_irqsave(&np->lock, flags);
262 list_del_rcu(&nc->node);
263 np->channel_num--;
264 spin_unlock_irqrestore(&np->lock, flags);
265
266 kfree(nc);
267 }
268
ncsi_find_package(struct ncsi_dev_priv * ndp,unsigned char id)269 struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
270 unsigned char id)
271 {
272 struct ncsi_package *np;
273
274 NCSI_FOR_EACH_PACKAGE(ndp, np) {
275 if (np->id == id)
276 return np;
277 }
278
279 return NULL;
280 }
281
ncsi_add_package(struct ncsi_dev_priv * ndp,unsigned char id)282 struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
283 unsigned char id)
284 {
285 struct ncsi_package *np, *tmp;
286 unsigned long flags;
287
288 np = kzalloc(sizeof(*np), GFP_ATOMIC);
289 if (!np)
290 return NULL;
291
292 np->id = id;
293 np->ndp = ndp;
294 spin_lock_init(&np->lock);
295 INIT_LIST_HEAD(&np->channels);
296 np->channel_whitelist = UINT_MAX;
297
298 spin_lock_irqsave(&ndp->lock, flags);
299 tmp = ncsi_find_package(ndp, id);
300 if (tmp) {
301 spin_unlock_irqrestore(&ndp->lock, flags);
302 kfree(np);
303 return tmp;
304 }
305
306 list_add_tail_rcu(&np->node, &ndp->packages);
307 ndp->package_num++;
308 spin_unlock_irqrestore(&ndp->lock, flags);
309
310 return np;
311 }
312
ncsi_remove_package(struct ncsi_package * np)313 void ncsi_remove_package(struct ncsi_package *np)
314 {
315 struct ncsi_dev_priv *ndp = np->ndp;
316 struct ncsi_channel *nc, *tmp;
317 unsigned long flags;
318
319 /* Release all child channels */
320 list_for_each_entry_safe(nc, tmp, &np->channels, node)
321 ncsi_remove_channel(nc);
322
323 /* Remove and free package */
324 spin_lock_irqsave(&ndp->lock, flags);
325 list_del_rcu(&np->node);
326 ndp->package_num--;
327 spin_unlock_irqrestore(&ndp->lock, flags);
328
329 kfree(np);
330 }
331
ncsi_find_package_and_channel(struct ncsi_dev_priv * ndp,unsigned char id,struct ncsi_package ** np,struct ncsi_channel ** nc)332 void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
333 unsigned char id,
334 struct ncsi_package **np,
335 struct ncsi_channel **nc)
336 {
337 struct ncsi_package *p;
338 struct ncsi_channel *c;
339
340 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
341 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
342
343 if (np)
344 *np = p;
345 if (nc)
346 *nc = c;
347 }
348
349 /* For two consecutive NCSI commands, the packet IDs shouldn't
350 * be same. Otherwise, the bogus response might be replied. So
351 * the available IDs are allocated in round-robin fashion.
352 */
ncsi_alloc_request(struct ncsi_dev_priv * ndp,unsigned int req_flags)353 struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
354 unsigned int req_flags)
355 {
356 struct ncsi_request *nr = NULL;
357 int i, limit = ARRAY_SIZE(ndp->requests);
358 unsigned long flags;
359
360 /* Check if there is one available request until the ceiling */
361 spin_lock_irqsave(&ndp->lock, flags);
362 for (i = ndp->request_id; i < limit; i++) {
363 if (ndp->requests[i].used)
364 continue;
365
366 nr = &ndp->requests[i];
367 nr->used = true;
368 nr->flags = req_flags;
369 ndp->request_id = i + 1;
370 goto found;
371 }
372
373 /* Fail back to check from the starting cursor */
374 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
375 if (ndp->requests[i].used)
376 continue;
377
378 nr = &ndp->requests[i];
379 nr->used = true;
380 nr->flags = req_flags;
381 ndp->request_id = i + 1;
382 goto found;
383 }
384
385 found:
386 spin_unlock_irqrestore(&ndp->lock, flags);
387 return nr;
388 }
389
ncsi_free_request(struct ncsi_request * nr)390 void ncsi_free_request(struct ncsi_request *nr)
391 {
392 struct ncsi_dev_priv *ndp = nr->ndp;
393 struct sk_buff *cmd, *rsp;
394 unsigned long flags;
395 bool driven;
396
397 if (nr->enabled) {
398 nr->enabled = false;
399 del_timer_sync(&nr->timer);
400 }
401
402 spin_lock_irqsave(&ndp->lock, flags);
403 cmd = nr->cmd;
404 rsp = nr->rsp;
405 nr->cmd = NULL;
406 nr->rsp = NULL;
407 nr->used = false;
408 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
409 spin_unlock_irqrestore(&ndp->lock, flags);
410
411 if (driven && cmd && --ndp->pending_req_num == 0)
412 schedule_work(&ndp->work);
413
414 /* Release command and response */
415 consume_skb(cmd);
416 consume_skb(rsp);
417 }
418
ncsi_find_dev(struct net_device * dev)419 struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
420 {
421 struct ncsi_dev_priv *ndp;
422
423 NCSI_FOR_EACH_DEV(ndp) {
424 if (ndp->ndev.dev == dev)
425 return &ndp->ndev;
426 }
427
428 return NULL;
429 }
430
ncsi_request_timeout(struct timer_list * t)431 static void ncsi_request_timeout(struct timer_list *t)
432 {
433 struct ncsi_request *nr = from_timer(nr, t, timer);
434 struct ncsi_dev_priv *ndp = nr->ndp;
435 struct ncsi_cmd_pkt *cmd;
436 struct ncsi_package *np;
437 struct ncsi_channel *nc;
438 unsigned long flags;
439
440 /* If the request already had associated response,
441 * let the response handler to release it.
442 */
443 spin_lock_irqsave(&ndp->lock, flags);
444 nr->enabled = false;
445 if (nr->rsp || !nr->cmd) {
446 spin_unlock_irqrestore(&ndp->lock, flags);
447 return;
448 }
449 spin_unlock_irqrestore(&ndp->lock, flags);
450
451 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
452 if (nr->cmd) {
453 /* Find the package */
454 cmd = (struct ncsi_cmd_pkt *)
455 skb_network_header(nr->cmd);
456 ncsi_find_package_and_channel(ndp,
457 cmd->cmd.common.channel,
458 &np, &nc);
459 ncsi_send_netlink_timeout(nr, np, nc);
460 }
461 }
462
463 /* Release the request */
464 ncsi_free_request(nr);
465 }
466
ncsi_suspend_channel(struct ncsi_dev_priv * ndp)467 static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
468 {
469 struct ncsi_dev *nd = &ndp->ndev;
470 struct ncsi_package *np;
471 struct ncsi_channel *nc, *tmp;
472 struct ncsi_cmd_arg nca;
473 unsigned long flags;
474 int ret;
475
476 np = ndp->active_package;
477 nc = ndp->active_channel;
478 nca.ndp = ndp;
479 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
480 switch (nd->state) {
481 case ncsi_dev_state_suspend:
482 nd->state = ncsi_dev_state_suspend_select;
483 fallthrough;
484 case ncsi_dev_state_suspend_select:
485 ndp->pending_req_num = 1;
486
487 nca.type = NCSI_PKT_CMD_SP;
488 nca.package = np->id;
489 nca.channel = NCSI_RESERVED_CHANNEL;
490 if (ndp->flags & NCSI_DEV_HWA)
491 nca.bytes[0] = 0;
492 else
493 nca.bytes[0] = 1;
494
495 /* To retrieve the last link states of channels in current
496 * package when current active channel needs fail over to
497 * another one. It means we will possibly select another
498 * channel as next active one. The link states of channels
499 * are most important factor of the selection. So we need
500 * accurate link states. Unfortunately, the link states on
501 * inactive channels can't be updated with LSC AEN in time.
502 */
503 if (ndp->flags & NCSI_DEV_RESHUFFLE)
504 nd->state = ncsi_dev_state_suspend_gls;
505 else
506 nd->state = ncsi_dev_state_suspend_dcnt;
507 ret = ncsi_xmit_cmd(&nca);
508 if (ret)
509 goto error;
510
511 break;
512 case ncsi_dev_state_suspend_gls:
513 ndp->pending_req_num = 1;
514
515 nca.type = NCSI_PKT_CMD_GLS;
516 nca.package = np->id;
517 nca.channel = ndp->channel_probe_id;
518 ret = ncsi_xmit_cmd(&nca);
519 if (ret)
520 goto error;
521 ndp->channel_probe_id++;
522
523 if (ndp->channel_probe_id == ndp->channel_count) {
524 ndp->channel_probe_id = 0;
525 nd->state = ncsi_dev_state_suspend_dcnt;
526 }
527
528 break;
529 case ncsi_dev_state_suspend_dcnt:
530 ndp->pending_req_num = 1;
531
532 nca.type = NCSI_PKT_CMD_DCNT;
533 nca.package = np->id;
534 nca.channel = nc->id;
535
536 nd->state = ncsi_dev_state_suspend_dc;
537 ret = ncsi_xmit_cmd(&nca);
538 if (ret)
539 goto error;
540
541 break;
542 case ncsi_dev_state_suspend_dc:
543 ndp->pending_req_num = 1;
544
545 nca.type = NCSI_PKT_CMD_DC;
546 nca.package = np->id;
547 nca.channel = nc->id;
548 nca.bytes[0] = 1;
549
550 nd->state = ncsi_dev_state_suspend_deselect;
551 ret = ncsi_xmit_cmd(&nca);
552 if (ret)
553 goto error;
554
555 NCSI_FOR_EACH_CHANNEL(np, tmp) {
556 /* If there is another channel active on this package
557 * do not deselect the package.
558 */
559 if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) {
560 nd->state = ncsi_dev_state_suspend_done;
561 break;
562 }
563 }
564 break;
565 case ncsi_dev_state_suspend_deselect:
566 ndp->pending_req_num = 1;
567
568 nca.type = NCSI_PKT_CMD_DP;
569 nca.package = np->id;
570 nca.channel = NCSI_RESERVED_CHANNEL;
571
572 nd->state = ncsi_dev_state_suspend_done;
573 ret = ncsi_xmit_cmd(&nca);
574 if (ret)
575 goto error;
576
577 break;
578 case ncsi_dev_state_suspend_done:
579 spin_lock_irqsave(&nc->lock, flags);
580 nc->state = NCSI_CHANNEL_INACTIVE;
581 spin_unlock_irqrestore(&nc->lock, flags);
582 if (ndp->flags & NCSI_DEV_RESET)
583 ncsi_reset_dev(nd);
584 else
585 ncsi_process_next_channel(ndp);
586 break;
587 default:
588 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
589 nd->state);
590 }
591
592 return;
593 error:
594 nd->state = ncsi_dev_state_functional;
595 }
596
597 /* Check the VLAN filter bitmap for a set filter, and construct a
598 * "Set VLAN Filter - Disable" packet if found.
599 */
clear_one_vid(struct ncsi_dev_priv * ndp,struct ncsi_channel * nc,struct ncsi_cmd_arg * nca)600 static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
601 struct ncsi_cmd_arg *nca)
602 {
603 struct ncsi_channel_vlan_filter *ncf;
604 unsigned long flags;
605 void *bitmap;
606 int index;
607 u16 vid;
608
609 ncf = &nc->vlan_filter;
610 bitmap = &ncf->bitmap;
611
612 spin_lock_irqsave(&nc->lock, flags);
613 index = find_first_bit(bitmap, ncf->n_vids);
614 if (index >= ncf->n_vids) {
615 spin_unlock_irqrestore(&nc->lock, flags);
616 return -1;
617 }
618 vid = ncf->vids[index];
619
620 clear_bit(index, bitmap);
621 ncf->vids[index] = 0;
622 spin_unlock_irqrestore(&nc->lock, flags);
623
624 nca->type = NCSI_PKT_CMD_SVF;
625 nca->words[1] = vid;
626 /* HW filter index starts at 1 */
627 nca->bytes[6] = index + 1;
628 nca->bytes[7] = 0x00;
629 return 0;
630 }
631
632 /* Find an outstanding VLAN tag and construct a "Set VLAN Filter - Enable"
633 * packet.
634 */
set_one_vid(struct ncsi_dev_priv * ndp,struct ncsi_channel * nc,struct ncsi_cmd_arg * nca)635 static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
636 struct ncsi_cmd_arg *nca)
637 {
638 struct ncsi_channel_vlan_filter *ncf;
639 struct vlan_vid *vlan = NULL;
640 unsigned long flags;
641 int i, index;
642 void *bitmap;
643 u16 vid;
644
645 if (list_empty(&ndp->vlan_vids))
646 return -1;
647
648 ncf = &nc->vlan_filter;
649 bitmap = &ncf->bitmap;
650
651 spin_lock_irqsave(&nc->lock, flags);
652
653 rcu_read_lock();
654 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
655 vid = vlan->vid;
656 for (i = 0; i < ncf->n_vids; i++)
657 if (ncf->vids[i] == vid) {
658 vid = 0;
659 break;
660 }
661 if (vid)
662 break;
663 }
664 rcu_read_unlock();
665
666 if (!vid) {
667 /* No VLAN ID is not set */
668 spin_unlock_irqrestore(&nc->lock, flags);
669 return -1;
670 }
671
672 index = find_first_zero_bit(bitmap, ncf->n_vids);
673 if (index < 0 || index >= ncf->n_vids) {
674 netdev_err(ndp->ndev.dev,
675 "Channel %u already has all VLAN filters set\n",
676 nc->id);
677 spin_unlock_irqrestore(&nc->lock, flags);
678 return -1;
679 }
680
681 ncf->vids[index] = vid;
682 set_bit(index, bitmap);
683 spin_unlock_irqrestore(&nc->lock, flags);
684
685 nca->type = NCSI_PKT_CMD_SVF;
686 nca->words[1] = vid;
687 /* HW filter index starts at 1 */
688 nca->bytes[6] = index + 1;
689 nca->bytes[7] = 0x01;
690
691 return 0;
692 }
693
ncsi_oem_keep_phy_intel(struct ncsi_cmd_arg * nca)694 static int ncsi_oem_keep_phy_intel(struct ncsi_cmd_arg *nca)
695 {
696 unsigned char data[NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN];
697 int ret = 0;
698
699 nca->payload = NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN;
700
701 memset(data, 0, NCSI_OEM_INTEL_CMD_KEEP_PHY_LEN);
702 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
703
704 data[4] = NCSI_OEM_INTEL_CMD_KEEP_PHY;
705
706 /* PHY Link up attribute */
707 data[6] = 0x1;
708
709 nca->data = data;
710
711 ret = ncsi_xmit_cmd(nca);
712 if (ret)
713 netdev_err(nca->ndp->ndev.dev,
714 "NCSI: Failed to transmit cmd 0x%x during configure\n",
715 nca->type);
716 return ret;
717 }
718
719 /* NCSI OEM Command APIs */
ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg * nca)720 static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
721 {
722 unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
723 int ret = 0;
724
725 nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
726
727 memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN);
728 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_BCM_ID);
729 data[5] = NCSI_OEM_BCM_CMD_GMA;
730
731 nca->data = data;
732
733 ret = ncsi_xmit_cmd(nca);
734 if (ret)
735 netdev_err(nca->ndp->ndev.dev,
736 "NCSI: Failed to transmit cmd 0x%x during configure\n",
737 nca->type);
738 return ret;
739 }
740
ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg * nca)741 static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg *nca)
742 {
743 union {
744 u8 data_u8[NCSI_OEM_MLX_CMD_GMA_LEN];
745 u32 data_u32[NCSI_OEM_MLX_CMD_GMA_LEN / sizeof(u32)];
746 } u;
747 int ret = 0;
748
749 nca->payload = NCSI_OEM_MLX_CMD_GMA_LEN;
750
751 memset(&u, 0, sizeof(u));
752 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
753 u.data_u8[5] = NCSI_OEM_MLX_CMD_GMA;
754 u.data_u8[6] = NCSI_OEM_MLX_CMD_GMA_PARAM;
755
756 nca->data = u.data_u8;
757
758 ret = ncsi_xmit_cmd(nca);
759 if (ret)
760 netdev_err(nca->ndp->ndev.dev,
761 "NCSI: Failed to transmit cmd 0x%x during configure\n",
762 nca->type);
763 return ret;
764 }
765
ncsi_oem_smaf_mlx(struct ncsi_cmd_arg * nca)766 static int ncsi_oem_smaf_mlx(struct ncsi_cmd_arg *nca)
767 {
768 union {
769 u8 data_u8[NCSI_OEM_MLX_CMD_SMAF_LEN];
770 u32 data_u32[NCSI_OEM_MLX_CMD_SMAF_LEN / sizeof(u32)];
771 } u;
772 int ret = 0;
773
774 memset(&u, 0, sizeof(u));
775 u.data_u32[0] = ntohl((__force __be32)NCSI_OEM_MFR_MLX_ID);
776 u.data_u8[5] = NCSI_OEM_MLX_CMD_SMAF;
777 u.data_u8[6] = NCSI_OEM_MLX_CMD_SMAF_PARAM;
778 memcpy(&u.data_u8[MLX_SMAF_MAC_ADDR_OFFSET],
779 nca->ndp->ndev.dev->dev_addr, ETH_ALEN);
780 u.data_u8[MLX_SMAF_MED_SUPPORT_OFFSET] =
781 (MLX_MC_RBT_AVL | MLX_MC_RBT_SUPPORT);
782
783 nca->payload = NCSI_OEM_MLX_CMD_SMAF_LEN;
784 nca->data = u.data_u8;
785
786 ret = ncsi_xmit_cmd(nca);
787 if (ret)
788 netdev_err(nca->ndp->ndev.dev,
789 "NCSI: Failed to transmit cmd 0x%x during probe\n",
790 nca->type);
791 return ret;
792 }
793
ncsi_oem_gma_handler_intel(struct ncsi_cmd_arg * nca)794 static int ncsi_oem_gma_handler_intel(struct ncsi_cmd_arg *nca)
795 {
796 unsigned char data[NCSI_OEM_INTEL_CMD_GMA_LEN];
797 int ret = 0;
798
799 nca->payload = NCSI_OEM_INTEL_CMD_GMA_LEN;
800
801 memset(data, 0, NCSI_OEM_INTEL_CMD_GMA_LEN);
802 *(unsigned int *)data = ntohl((__force __be32)NCSI_OEM_MFR_INTEL_ID);
803 data[4] = NCSI_OEM_INTEL_CMD_GMA;
804
805 nca->data = data;
806
807 ret = ncsi_xmit_cmd(nca);
808 if (ret)
809 netdev_err(nca->ndp->ndev.dev,
810 "NCSI: Failed to transmit cmd 0x%x during configure\n",
811 nca->type);
812
813 return ret;
814 }
815
816 /* OEM Command handlers initialization */
817 static struct ncsi_oem_gma_handler {
818 unsigned int mfr_id;
819 int (*handler)(struct ncsi_cmd_arg *nca);
820 } ncsi_oem_gma_handlers[] = {
821 { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm },
822 { NCSI_OEM_MFR_MLX_ID, ncsi_oem_gma_handler_mlx },
823 { NCSI_OEM_MFR_INTEL_ID, ncsi_oem_gma_handler_intel }
824 };
825
ncsi_gma_handler(struct ncsi_cmd_arg * nca,unsigned int mf_id)826 static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
827 {
828 struct ncsi_oem_gma_handler *nch = NULL;
829 int i;
830
831 /* This function should only be called once, return if flag set */
832 if (nca->ndp->gma_flag == 1)
833 return -1;
834
835 /* Find gma handler for given manufacturer id */
836 for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) {
837 if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) {
838 if (ncsi_oem_gma_handlers[i].handler)
839 nch = &ncsi_oem_gma_handlers[i];
840 break;
841 }
842 }
843
844 if (!nch) {
845 netdev_err(nca->ndp->ndev.dev,
846 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
847 mf_id);
848 return -1;
849 }
850
851 /* Get Mac address from NCSI device */
852 return nch->handler(nca);
853 }
854
855 /* Determine if a given channel from the channel_queue should be used for Tx */
ncsi_channel_is_tx(struct ncsi_dev_priv * ndp,struct ncsi_channel * nc)856 static bool ncsi_channel_is_tx(struct ncsi_dev_priv *ndp,
857 struct ncsi_channel *nc)
858 {
859 struct ncsi_channel_mode *ncm;
860 struct ncsi_channel *channel;
861 struct ncsi_package *np;
862
863 /* Check if any other channel has Tx enabled; a channel may have already
864 * been configured and removed from the channel queue.
865 */
866 NCSI_FOR_EACH_PACKAGE(ndp, np) {
867 if (!ndp->multi_package && np != nc->package)
868 continue;
869 NCSI_FOR_EACH_CHANNEL(np, channel) {
870 ncm = &channel->modes[NCSI_MODE_TX_ENABLE];
871 if (ncm->enable)
872 return false;
873 }
874 }
875
876 /* This channel is the preferred channel and has link */
877 list_for_each_entry_rcu(channel, &ndp->channel_queue, link) {
878 np = channel->package;
879 if (np->preferred_channel &&
880 ncsi_channel_has_link(np->preferred_channel)) {
881 return np->preferred_channel == nc;
882 }
883 }
884
885 /* This channel has link */
886 if (ncsi_channel_has_link(nc))
887 return true;
888
889 list_for_each_entry_rcu(channel, &ndp->channel_queue, link)
890 if (ncsi_channel_has_link(channel))
891 return false;
892
893 /* No other channel has link; default to this one */
894 return true;
895 }
896
897 /* Change the active Tx channel in a multi-channel setup */
ncsi_update_tx_channel(struct ncsi_dev_priv * ndp,struct ncsi_package * package,struct ncsi_channel * disable,struct ncsi_channel * enable)898 int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp,
899 struct ncsi_package *package,
900 struct ncsi_channel *disable,
901 struct ncsi_channel *enable)
902 {
903 struct ncsi_cmd_arg nca;
904 struct ncsi_channel *nc;
905 struct ncsi_package *np;
906 int ret = 0;
907
908 if (!package->multi_channel && !ndp->multi_package)
909 netdev_warn(ndp->ndev.dev,
910 "NCSI: Trying to update Tx channel in single-channel mode\n");
911 nca.ndp = ndp;
912 nca.req_flags = 0;
913
914 /* Find current channel with Tx enabled */
915 NCSI_FOR_EACH_PACKAGE(ndp, np) {
916 if (disable)
917 break;
918 if (!ndp->multi_package && np != package)
919 continue;
920
921 NCSI_FOR_EACH_CHANNEL(np, nc)
922 if (nc->modes[NCSI_MODE_TX_ENABLE].enable) {
923 disable = nc;
924 break;
925 }
926 }
927
928 /* Find a suitable channel for Tx */
929 NCSI_FOR_EACH_PACKAGE(ndp, np) {
930 if (enable)
931 break;
932 if (!ndp->multi_package && np != package)
933 continue;
934 if (!(ndp->package_whitelist & (0x1 << np->id)))
935 continue;
936
937 if (np->preferred_channel &&
938 ncsi_channel_has_link(np->preferred_channel)) {
939 enable = np->preferred_channel;
940 break;
941 }
942
943 NCSI_FOR_EACH_CHANNEL(np, nc) {
944 if (!(np->channel_whitelist & 0x1 << nc->id))
945 continue;
946 if (nc->state != NCSI_CHANNEL_ACTIVE)
947 continue;
948 if (ncsi_channel_has_link(nc)) {
949 enable = nc;
950 break;
951 }
952 }
953 }
954
955 if (disable == enable)
956 return -1;
957
958 if (!enable)
959 return -1;
960
961 if (disable) {
962 nca.channel = disable->id;
963 nca.package = disable->package->id;
964 nca.type = NCSI_PKT_CMD_DCNT;
965 ret = ncsi_xmit_cmd(&nca);
966 if (ret)
967 netdev_err(ndp->ndev.dev,
968 "Error %d sending DCNT\n",
969 ret);
970 }
971
972 netdev_info(ndp->ndev.dev, "NCSI: channel %u enables Tx\n", enable->id);
973
974 nca.channel = enable->id;
975 nca.package = enable->package->id;
976 nca.type = NCSI_PKT_CMD_ECNT;
977 ret = ncsi_xmit_cmd(&nca);
978 if (ret)
979 netdev_err(ndp->ndev.dev,
980 "Error %d sending ECNT\n",
981 ret);
982
983 return ret;
984 }
985
ncsi_configure_channel(struct ncsi_dev_priv * ndp)986 static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
987 {
988 struct ncsi_package *np = ndp->active_package;
989 struct ncsi_channel *nc = ndp->active_channel;
990 struct ncsi_channel *hot_nc = NULL;
991 struct ncsi_dev *nd = &ndp->ndev;
992 struct net_device *dev = nd->dev;
993 struct ncsi_cmd_arg nca;
994 unsigned char index;
995 unsigned long flags;
996 int ret;
997
998 nca.ndp = ndp;
999 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1000 switch (nd->state) {
1001 case ncsi_dev_state_config:
1002 case ncsi_dev_state_config_sp:
1003 ndp->pending_req_num = 1;
1004
1005 /* Select the specific package */
1006 nca.type = NCSI_PKT_CMD_SP;
1007 if (ndp->flags & NCSI_DEV_HWA)
1008 nca.bytes[0] = 0;
1009 else
1010 nca.bytes[0] = 1;
1011 nca.package = np->id;
1012 nca.channel = NCSI_RESERVED_CHANNEL;
1013 ret = ncsi_xmit_cmd(&nca);
1014 if (ret) {
1015 netdev_err(ndp->ndev.dev,
1016 "NCSI: Failed to transmit CMD_SP\n");
1017 goto error;
1018 }
1019
1020 nd->state = ncsi_dev_state_config_cis;
1021 break;
1022 case ncsi_dev_state_config_cis:
1023 ndp->pending_req_num = 1;
1024
1025 /* Clear initial state */
1026 nca.type = NCSI_PKT_CMD_CIS;
1027 nca.package = np->id;
1028 nca.channel = nc->id;
1029 ret = ncsi_xmit_cmd(&nca);
1030 if (ret) {
1031 netdev_err(ndp->ndev.dev,
1032 "NCSI: Failed to transmit CMD_CIS\n");
1033 goto error;
1034 }
1035
1036 nd->state = IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
1037 ? ncsi_dev_state_config_oem_gma
1038 : ncsi_dev_state_config_clear_vids;
1039 break;
1040 case ncsi_dev_state_config_oem_gma:
1041 nd->state = ncsi_dev_state_config_clear_vids;
1042
1043 nca.type = NCSI_PKT_CMD_OEM;
1044 nca.package = np->id;
1045 nca.channel = nc->id;
1046 ndp->pending_req_num = 1;
1047 ret = ncsi_gma_handler(&nca, nc->version.mf_id);
1048 if (ret < 0)
1049 schedule_work(&ndp->work);
1050
1051 break;
1052 case ncsi_dev_state_config_clear_vids:
1053 case ncsi_dev_state_config_svf:
1054 case ncsi_dev_state_config_ev:
1055 case ncsi_dev_state_config_sma:
1056 case ncsi_dev_state_config_ebf:
1057 case ncsi_dev_state_config_dgmf:
1058 case ncsi_dev_state_config_ecnt:
1059 case ncsi_dev_state_config_ec:
1060 case ncsi_dev_state_config_ae:
1061 case ncsi_dev_state_config_gls:
1062 ndp->pending_req_num = 1;
1063
1064 nca.package = np->id;
1065 nca.channel = nc->id;
1066
1067 /* Clear any active filters on the channel before setting */
1068 if (nd->state == ncsi_dev_state_config_clear_vids) {
1069 ret = clear_one_vid(ndp, nc, &nca);
1070 if (ret) {
1071 nd->state = ncsi_dev_state_config_svf;
1072 schedule_work(&ndp->work);
1073 break;
1074 }
1075 /* Repeat */
1076 nd->state = ncsi_dev_state_config_clear_vids;
1077 /* Add known VLAN tags to the filter */
1078 } else if (nd->state == ncsi_dev_state_config_svf) {
1079 ret = set_one_vid(ndp, nc, &nca);
1080 if (ret) {
1081 nd->state = ncsi_dev_state_config_ev;
1082 schedule_work(&ndp->work);
1083 break;
1084 }
1085 /* Repeat */
1086 nd->state = ncsi_dev_state_config_svf;
1087 /* Enable/Disable the VLAN filter */
1088 } else if (nd->state == ncsi_dev_state_config_ev) {
1089 if (list_empty(&ndp->vlan_vids)) {
1090 nca.type = NCSI_PKT_CMD_DV;
1091 } else {
1092 nca.type = NCSI_PKT_CMD_EV;
1093 nca.bytes[3] = NCSI_CAP_VLAN_NO;
1094 }
1095 nd->state = ncsi_dev_state_config_sma;
1096 } else if (nd->state == ncsi_dev_state_config_sma) {
1097 /* Use first entry in unicast filter table. Note that
1098 * the MAC filter table starts from entry 1 instead of
1099 * 0.
1100 */
1101 nca.type = NCSI_PKT_CMD_SMA;
1102 for (index = 0; index < 6; index++)
1103 nca.bytes[index] = dev->dev_addr[index];
1104 nca.bytes[6] = 0x1;
1105 nca.bytes[7] = 0x1;
1106 nd->state = ncsi_dev_state_config_ebf;
1107 } else if (nd->state == ncsi_dev_state_config_ebf) {
1108 nca.type = NCSI_PKT_CMD_EBF;
1109 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
1110 /* if multicast global filtering is supported then
1111 * disable it so that all multicast packet will be
1112 * forwarded to management controller
1113 */
1114 if (nc->caps[NCSI_CAP_GENERIC].cap &
1115 NCSI_CAP_GENERIC_MC)
1116 nd->state = ncsi_dev_state_config_dgmf;
1117 else if (ncsi_channel_is_tx(ndp, nc))
1118 nd->state = ncsi_dev_state_config_ecnt;
1119 else
1120 nd->state = ncsi_dev_state_config_ec;
1121 } else if (nd->state == ncsi_dev_state_config_dgmf) {
1122 nca.type = NCSI_PKT_CMD_DGMF;
1123 if (ncsi_channel_is_tx(ndp, nc))
1124 nd->state = ncsi_dev_state_config_ecnt;
1125 else
1126 nd->state = ncsi_dev_state_config_ec;
1127 } else if (nd->state == ncsi_dev_state_config_ecnt) {
1128 if (np->preferred_channel &&
1129 nc != np->preferred_channel)
1130 netdev_info(ndp->ndev.dev,
1131 "NCSI: Tx failed over to channel %u\n",
1132 nc->id);
1133 nca.type = NCSI_PKT_CMD_ECNT;
1134 nd->state = ncsi_dev_state_config_ec;
1135 } else if (nd->state == ncsi_dev_state_config_ec) {
1136 /* Enable AEN if it's supported */
1137 nca.type = NCSI_PKT_CMD_EC;
1138 nd->state = ncsi_dev_state_config_ae;
1139 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
1140 nd->state = ncsi_dev_state_config_gls;
1141 } else if (nd->state == ncsi_dev_state_config_ae) {
1142 nca.type = NCSI_PKT_CMD_AE;
1143 nca.bytes[0] = 0;
1144 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
1145 nd->state = ncsi_dev_state_config_gls;
1146 } else if (nd->state == ncsi_dev_state_config_gls) {
1147 nca.type = NCSI_PKT_CMD_GLS;
1148 nd->state = ncsi_dev_state_config_done;
1149 }
1150
1151 ret = ncsi_xmit_cmd(&nca);
1152 if (ret) {
1153 netdev_err(ndp->ndev.dev,
1154 "NCSI: Failed to transmit CMD %x\n",
1155 nca.type);
1156 goto error;
1157 }
1158 break;
1159 case ncsi_dev_state_config_done:
1160 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
1161 nc->id);
1162 spin_lock_irqsave(&nc->lock, flags);
1163 nc->state = NCSI_CHANNEL_ACTIVE;
1164
1165 if (ndp->flags & NCSI_DEV_RESET) {
1166 /* A reset event happened during config, start it now */
1167 nc->reconfigure_needed = false;
1168 spin_unlock_irqrestore(&nc->lock, flags);
1169 ncsi_reset_dev(nd);
1170 break;
1171 }
1172
1173 if (nc->reconfigure_needed) {
1174 /* This channel's configuration has been updated
1175 * part-way during the config state - start the
1176 * channel configuration over
1177 */
1178 nc->reconfigure_needed = false;
1179 nc->state = NCSI_CHANNEL_INACTIVE;
1180 spin_unlock_irqrestore(&nc->lock, flags);
1181
1182 spin_lock_irqsave(&ndp->lock, flags);
1183 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1184 spin_unlock_irqrestore(&ndp->lock, flags);
1185
1186 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
1187 ncsi_process_next_channel(ndp);
1188 break;
1189 }
1190
1191 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
1192 hot_nc = nc;
1193 } else {
1194 hot_nc = NULL;
1195 netdev_dbg(ndp->ndev.dev,
1196 "NCSI: channel %u link down after config\n",
1197 nc->id);
1198 }
1199 spin_unlock_irqrestore(&nc->lock, flags);
1200
1201 /* Update the hot channel */
1202 spin_lock_irqsave(&ndp->lock, flags);
1203 ndp->hot_channel = hot_nc;
1204 spin_unlock_irqrestore(&ndp->lock, flags);
1205
1206 ncsi_start_channel_monitor(nc);
1207 ncsi_process_next_channel(ndp);
1208 break;
1209 default:
1210 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
1211 nd->state);
1212 }
1213
1214 return;
1215
1216 error:
1217 ncsi_report_link(ndp, true);
1218 }
1219
ncsi_choose_active_channel(struct ncsi_dev_priv * ndp)1220 static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
1221 {
1222 struct ncsi_channel *nc, *found, *hot_nc;
1223 struct ncsi_channel_mode *ncm;
1224 unsigned long flags, cflags;
1225 struct ncsi_package *np;
1226 bool with_link;
1227
1228 spin_lock_irqsave(&ndp->lock, flags);
1229 hot_nc = ndp->hot_channel;
1230 spin_unlock_irqrestore(&ndp->lock, flags);
1231
1232 /* By default the search is done once an inactive channel with up
1233 * link is found, unless a preferred channel is set.
1234 * If multi_package or multi_channel are configured all channels in the
1235 * whitelist are added to the channel queue.
1236 */
1237 found = NULL;
1238 with_link = false;
1239 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1240 if (!(ndp->package_whitelist & (0x1 << np->id)))
1241 continue;
1242 NCSI_FOR_EACH_CHANNEL(np, nc) {
1243 if (!(np->channel_whitelist & (0x1 << nc->id)))
1244 continue;
1245
1246 spin_lock_irqsave(&nc->lock, cflags);
1247
1248 if (!list_empty(&nc->link) ||
1249 nc->state != NCSI_CHANNEL_INACTIVE) {
1250 spin_unlock_irqrestore(&nc->lock, cflags);
1251 continue;
1252 }
1253
1254 if (!found)
1255 found = nc;
1256
1257 if (nc == hot_nc)
1258 found = nc;
1259
1260 ncm = &nc->modes[NCSI_MODE_LINK];
1261 if (ncm->data[2] & 0x1) {
1262 found = nc;
1263 with_link = true;
1264 }
1265
1266 /* If multi_channel is enabled configure all valid
1267 * channels whether or not they currently have link
1268 * so they will have AENs enabled.
1269 */
1270 if (with_link || np->multi_channel) {
1271 spin_lock_irqsave(&ndp->lock, flags);
1272 list_add_tail_rcu(&nc->link,
1273 &ndp->channel_queue);
1274 spin_unlock_irqrestore(&ndp->lock, flags);
1275
1276 netdev_dbg(ndp->ndev.dev,
1277 "NCSI: Channel %u added to queue (link %s)\n",
1278 nc->id,
1279 ncm->data[2] & 0x1 ? "up" : "down");
1280 }
1281
1282 spin_unlock_irqrestore(&nc->lock, cflags);
1283
1284 if (with_link && !np->multi_channel)
1285 break;
1286 }
1287 if (with_link && !ndp->multi_package)
1288 break;
1289 }
1290
1291 if (list_empty(&ndp->channel_queue) && found) {
1292 netdev_info(ndp->ndev.dev,
1293 "NCSI: No channel with link found, configuring channel %u\n",
1294 found->id);
1295 spin_lock_irqsave(&ndp->lock, flags);
1296 list_add_tail_rcu(&found->link, &ndp->channel_queue);
1297 spin_unlock_irqrestore(&ndp->lock, flags);
1298 } else if (!found) {
1299 netdev_warn(ndp->ndev.dev,
1300 "NCSI: No channel found to configure!\n");
1301 ncsi_report_link(ndp, true);
1302 return -ENODEV;
1303 }
1304
1305 return ncsi_process_next_channel(ndp);
1306 }
1307
ncsi_check_hwa(struct ncsi_dev_priv * ndp)1308 static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1309 {
1310 struct ncsi_package *np;
1311 struct ncsi_channel *nc;
1312 unsigned int cap;
1313 bool has_channel = false;
1314
1315 /* The hardware arbitration is disabled if any one channel
1316 * doesn't support explicitly.
1317 */
1318 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1319 NCSI_FOR_EACH_CHANNEL(np, nc) {
1320 has_channel = true;
1321
1322 cap = nc->caps[NCSI_CAP_GENERIC].cap;
1323 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
1324 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
1325 NCSI_CAP_GENERIC_HWA_SUPPORT) {
1326 ndp->flags &= ~NCSI_DEV_HWA;
1327 return false;
1328 }
1329 }
1330 }
1331
1332 if (has_channel) {
1333 ndp->flags |= NCSI_DEV_HWA;
1334 return true;
1335 }
1336
1337 ndp->flags &= ~NCSI_DEV_HWA;
1338 return false;
1339 }
1340
ncsi_probe_channel(struct ncsi_dev_priv * ndp)1341 static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1342 {
1343 struct ncsi_dev *nd = &ndp->ndev;
1344 struct ncsi_package *np;
1345 struct ncsi_cmd_arg nca;
1346 unsigned char index;
1347 int ret;
1348
1349 nca.ndp = ndp;
1350 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
1351 switch (nd->state) {
1352 case ncsi_dev_state_probe:
1353 nd->state = ncsi_dev_state_probe_deselect;
1354 fallthrough;
1355 case ncsi_dev_state_probe_deselect:
1356 ndp->pending_req_num = 8;
1357
1358 /* Deselect all possible packages */
1359 nca.type = NCSI_PKT_CMD_DP;
1360 nca.channel = NCSI_RESERVED_CHANNEL;
1361 for (index = 0; index < 8; index++) {
1362 nca.package = index;
1363 ret = ncsi_xmit_cmd(&nca);
1364 if (ret)
1365 goto error;
1366 }
1367
1368 nd->state = ncsi_dev_state_probe_package;
1369 break;
1370 case ncsi_dev_state_probe_package:
1371 ndp->pending_req_num = 1;
1372
1373 nca.type = NCSI_PKT_CMD_SP;
1374 nca.bytes[0] = 1;
1375 nca.package = ndp->package_probe_id;
1376 nca.channel = NCSI_RESERVED_CHANNEL;
1377 ret = ncsi_xmit_cmd(&nca);
1378 if (ret)
1379 goto error;
1380 nd->state = ncsi_dev_state_probe_channel;
1381 break;
1382 case ncsi_dev_state_probe_channel:
1383 ndp->active_package = ncsi_find_package(ndp,
1384 ndp->package_probe_id);
1385 if (!ndp->active_package) {
1386 /* No response */
1387 nd->state = ncsi_dev_state_probe_dp;
1388 schedule_work(&ndp->work);
1389 break;
1390 }
1391 nd->state = ncsi_dev_state_probe_cis;
1392 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC) &&
1393 ndp->mlx_multi_host)
1394 nd->state = ncsi_dev_state_probe_mlx_gma;
1395
1396 schedule_work(&ndp->work);
1397 break;
1398 case ncsi_dev_state_probe_mlx_gma:
1399 ndp->pending_req_num = 1;
1400
1401 nca.type = NCSI_PKT_CMD_OEM;
1402 nca.package = ndp->active_package->id;
1403 nca.channel = 0;
1404 ret = ncsi_oem_gma_handler_mlx(&nca);
1405 if (ret)
1406 goto error;
1407
1408 nd->state = ncsi_dev_state_probe_mlx_smaf;
1409 break;
1410 case ncsi_dev_state_probe_mlx_smaf:
1411 ndp->pending_req_num = 1;
1412
1413 nca.type = NCSI_PKT_CMD_OEM;
1414 nca.package = ndp->active_package->id;
1415 nca.channel = 0;
1416 ret = ncsi_oem_smaf_mlx(&nca);
1417 if (ret)
1418 goto error;
1419
1420 nd->state = ncsi_dev_state_probe_cis;
1421 break;
1422 case ncsi_dev_state_probe_keep_phy:
1423 ndp->pending_req_num = 1;
1424
1425 nca.type = NCSI_PKT_CMD_OEM;
1426 nca.package = ndp->active_package->id;
1427 nca.channel = 0;
1428 ret = ncsi_oem_keep_phy_intel(&nca);
1429 if (ret)
1430 goto error;
1431
1432 nd->state = ncsi_dev_state_probe_gvi;
1433 break;
1434 case ncsi_dev_state_probe_cis:
1435 case ncsi_dev_state_probe_gvi:
1436 case ncsi_dev_state_probe_gc:
1437 case ncsi_dev_state_probe_gls:
1438 np = ndp->active_package;
1439 ndp->pending_req_num = 1;
1440
1441 /* Clear initial state Retrieve version, capability or link status */
1442 if (nd->state == ncsi_dev_state_probe_cis)
1443 nca.type = NCSI_PKT_CMD_CIS;
1444 else if (nd->state == ncsi_dev_state_probe_gvi)
1445 nca.type = NCSI_PKT_CMD_GVI;
1446 else if (nd->state == ncsi_dev_state_probe_gc)
1447 nca.type = NCSI_PKT_CMD_GC;
1448 else
1449 nca.type = NCSI_PKT_CMD_GLS;
1450
1451 nca.package = np->id;
1452 nca.channel = ndp->channel_probe_id;
1453
1454 ret = ncsi_xmit_cmd(&nca);
1455 if (ret)
1456 goto error;
1457
1458 if (nd->state == ncsi_dev_state_probe_cis) {
1459 nd->state = ncsi_dev_state_probe_gvi;
1460 if (IS_ENABLED(CONFIG_NCSI_OEM_CMD_KEEP_PHY) && ndp->channel_probe_id == 0)
1461 nd->state = ncsi_dev_state_probe_keep_phy;
1462 } else if (nd->state == ncsi_dev_state_probe_gvi) {
1463 nd->state = ncsi_dev_state_probe_gc;
1464 } else if (nd->state == ncsi_dev_state_probe_gc) {
1465 nd->state = ncsi_dev_state_probe_gls;
1466 } else {
1467 nd->state = ncsi_dev_state_probe_cis;
1468 ndp->channel_probe_id++;
1469 }
1470
1471 if (ndp->channel_probe_id == ndp->channel_count) {
1472 ndp->channel_probe_id = 0;
1473 nd->state = ncsi_dev_state_probe_dp;
1474 }
1475 break;
1476 case ncsi_dev_state_probe_dp:
1477 ndp->pending_req_num = 1;
1478
1479 /* Deselect the current package */
1480 nca.type = NCSI_PKT_CMD_DP;
1481 nca.package = ndp->package_probe_id;
1482 nca.channel = NCSI_RESERVED_CHANNEL;
1483 ret = ncsi_xmit_cmd(&nca);
1484 if (ret)
1485 goto error;
1486
1487 /* Probe next package */
1488 ndp->package_probe_id++;
1489 if (ndp->package_probe_id >= 8) {
1490 /* Probe finished */
1491 ndp->flags |= NCSI_DEV_PROBED;
1492 break;
1493 }
1494 nd->state = ncsi_dev_state_probe_package;
1495 ndp->active_package = NULL;
1496 break;
1497 default:
1498 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1499 nd->state);
1500 }
1501
1502 if (ndp->flags & NCSI_DEV_PROBED) {
1503 /* Check if all packages have HWA support */
1504 ncsi_check_hwa(ndp);
1505 ncsi_choose_active_channel(ndp);
1506 }
1507
1508 return;
1509 error:
1510 netdev_err(ndp->ndev.dev,
1511 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1512 nca.type);
1513 ncsi_report_link(ndp, true);
1514 }
1515
ncsi_dev_work(struct work_struct * work)1516 static void ncsi_dev_work(struct work_struct *work)
1517 {
1518 struct ncsi_dev_priv *ndp = container_of(work,
1519 struct ncsi_dev_priv, work);
1520 struct ncsi_dev *nd = &ndp->ndev;
1521
1522 switch (nd->state & ncsi_dev_state_major) {
1523 case ncsi_dev_state_probe:
1524 ncsi_probe_channel(ndp);
1525 break;
1526 case ncsi_dev_state_suspend:
1527 ncsi_suspend_channel(ndp);
1528 break;
1529 case ncsi_dev_state_config:
1530 ncsi_configure_channel(ndp);
1531 break;
1532 default:
1533 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1534 nd->state);
1535 }
1536 }
1537
ncsi_process_next_channel(struct ncsi_dev_priv * ndp)1538 int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1539 {
1540 struct ncsi_channel *nc;
1541 int old_state;
1542 unsigned long flags;
1543
1544 spin_lock_irqsave(&ndp->lock, flags);
1545 nc = list_first_or_null_rcu(&ndp->channel_queue,
1546 struct ncsi_channel, link);
1547 if (!nc) {
1548 spin_unlock_irqrestore(&ndp->lock, flags);
1549 goto out;
1550 }
1551
1552 list_del_init(&nc->link);
1553 spin_unlock_irqrestore(&ndp->lock, flags);
1554
1555 spin_lock_irqsave(&nc->lock, flags);
1556 old_state = nc->state;
1557 nc->state = NCSI_CHANNEL_INVISIBLE;
1558 spin_unlock_irqrestore(&nc->lock, flags);
1559
1560 ndp->active_channel = nc;
1561 ndp->active_package = nc->package;
1562
1563 switch (old_state) {
1564 case NCSI_CHANNEL_INACTIVE:
1565 ndp->ndev.state = ncsi_dev_state_config;
1566 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1567 nc->id);
1568 ncsi_configure_channel(ndp);
1569 break;
1570 case NCSI_CHANNEL_ACTIVE:
1571 ndp->ndev.state = ncsi_dev_state_suspend;
1572 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1573 nc->id);
1574 ncsi_suspend_channel(ndp);
1575 break;
1576 default:
1577 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
1578 old_state, nc->package->id, nc->id);
1579 ncsi_report_link(ndp, false);
1580 return -EINVAL;
1581 }
1582
1583 return 0;
1584
1585 out:
1586 ndp->active_channel = NULL;
1587 ndp->active_package = NULL;
1588 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1589 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1590 return ncsi_choose_active_channel(ndp);
1591 }
1592
1593 ncsi_report_link(ndp, false);
1594 return -ENODEV;
1595 }
1596
ncsi_kick_channels(struct ncsi_dev_priv * ndp)1597 static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1598 {
1599 struct ncsi_dev *nd = &ndp->ndev;
1600 struct ncsi_channel *nc;
1601 struct ncsi_package *np;
1602 unsigned long flags;
1603 unsigned int n = 0;
1604
1605 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1606 NCSI_FOR_EACH_CHANNEL(np, nc) {
1607 spin_lock_irqsave(&nc->lock, flags);
1608
1609 /* Channels may be busy, mark dirty instead of
1610 * kicking if;
1611 * a) not ACTIVE (configured)
1612 * b) in the channel_queue (to be configured)
1613 * c) it's ndev is in the config state
1614 */
1615 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1616 if ((ndp->ndev.state & 0xff00) ==
1617 ncsi_dev_state_config ||
1618 !list_empty(&nc->link)) {
1619 netdev_dbg(nd->dev,
1620 "NCSI: channel %p marked dirty\n",
1621 nc);
1622 nc->reconfigure_needed = true;
1623 }
1624 spin_unlock_irqrestore(&nc->lock, flags);
1625 continue;
1626 }
1627
1628 spin_unlock_irqrestore(&nc->lock, flags);
1629
1630 ncsi_stop_channel_monitor(nc);
1631 spin_lock_irqsave(&nc->lock, flags);
1632 nc->state = NCSI_CHANNEL_INACTIVE;
1633 spin_unlock_irqrestore(&nc->lock, flags);
1634
1635 spin_lock_irqsave(&ndp->lock, flags);
1636 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1637 spin_unlock_irqrestore(&ndp->lock, flags);
1638
1639 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
1640 n++;
1641 }
1642 }
1643
1644 return n;
1645 }
1646
ncsi_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1647 int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1648 {
1649 struct ncsi_dev_priv *ndp;
1650 unsigned int n_vids = 0;
1651 struct vlan_vid *vlan;
1652 struct ncsi_dev *nd;
1653 bool found = false;
1654
1655 if (vid == 0)
1656 return 0;
1657
1658 nd = ncsi_find_dev(dev);
1659 if (!nd) {
1660 netdev_warn(dev, "NCSI: No net_device?\n");
1661 return 0;
1662 }
1663
1664 ndp = TO_NCSI_DEV_PRIV(nd);
1665
1666 /* Add the VLAN id to our internal list */
1667 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1668 n_vids++;
1669 if (vlan->vid == vid) {
1670 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1671 vid);
1672 return 0;
1673 }
1674 }
1675 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1676 netdev_warn(dev,
1677 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1678 vid, NCSI_MAX_VLAN_VIDS);
1679 return -ENOSPC;
1680 }
1681
1682 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1683 if (!vlan)
1684 return -ENOMEM;
1685
1686 vlan->proto = proto;
1687 vlan->vid = vid;
1688 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1689
1690 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
1691
1692 found = ncsi_kick_channels(ndp) != 0;
1693
1694 return found ? ncsi_process_next_channel(ndp) : 0;
1695 }
1696 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
1697
ncsi_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1698 int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1699 {
1700 struct vlan_vid *vlan, *tmp;
1701 struct ncsi_dev_priv *ndp;
1702 struct ncsi_dev *nd;
1703 bool found = false;
1704
1705 if (vid == 0)
1706 return 0;
1707
1708 nd = ncsi_find_dev(dev);
1709 if (!nd) {
1710 netdev_warn(dev, "NCSI: no net_device?\n");
1711 return 0;
1712 }
1713
1714 ndp = TO_NCSI_DEV_PRIV(nd);
1715
1716 /* Remove the VLAN id from our internal list */
1717 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1718 if (vlan->vid == vid) {
1719 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
1720 list_del_rcu(&vlan->list);
1721 found = true;
1722 kfree(vlan);
1723 }
1724
1725 if (!found) {
1726 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
1727 return -EINVAL;
1728 }
1729
1730 found = ncsi_kick_channels(ndp) != 0;
1731
1732 return found ? ncsi_process_next_channel(ndp) : 0;
1733 }
1734 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
1735
ncsi_register_dev(struct net_device * dev,void (* handler)(struct ncsi_dev * ndev))1736 struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1737 void (*handler)(struct ncsi_dev *ndev))
1738 {
1739 struct ncsi_dev_priv *ndp;
1740 struct ncsi_dev *nd;
1741 struct platform_device *pdev;
1742 struct device_node *np;
1743 unsigned long flags;
1744 int i;
1745
1746 /* Check if the device has been registered or not */
1747 nd = ncsi_find_dev(dev);
1748 if (nd)
1749 return nd;
1750
1751 /* Create NCSI device */
1752 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1753 if (!ndp)
1754 return NULL;
1755
1756 nd = &ndp->ndev;
1757 nd->state = ncsi_dev_state_registered;
1758 nd->dev = dev;
1759 nd->handler = handler;
1760 ndp->pending_req_num = 0;
1761 INIT_LIST_HEAD(&ndp->channel_queue);
1762 INIT_LIST_HEAD(&ndp->vlan_vids);
1763 INIT_WORK(&ndp->work, ncsi_dev_work);
1764 ndp->package_whitelist = UINT_MAX;
1765
1766 /* Initialize private NCSI device */
1767 spin_lock_init(&ndp->lock);
1768 INIT_LIST_HEAD(&ndp->packages);
1769 ndp->request_id = NCSI_REQ_START_IDX;
1770 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1771 ndp->requests[i].id = i;
1772 ndp->requests[i].ndp = ndp;
1773 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
1774 }
1775 ndp->channel_count = NCSI_RESERVED_CHANNEL;
1776
1777 spin_lock_irqsave(&ncsi_dev_lock, flags);
1778 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1779 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1780
1781 /* Register NCSI packet Rx handler */
1782 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1783 ndp->ptype.func = ncsi_rcv_rsp;
1784 ndp->ptype.dev = dev;
1785 dev_add_pack(&ndp->ptype);
1786
1787 pdev = to_platform_device(dev->dev.parent);
1788 if (pdev) {
1789 np = pdev->dev.of_node;
1790 if (np && (of_property_read_bool(np, "mellanox,multi-host") ||
1791 of_property_read_bool(np, "mlx,multi-host")))
1792 ndp->mlx_multi_host = true;
1793 }
1794
1795 return nd;
1796 }
1797 EXPORT_SYMBOL_GPL(ncsi_register_dev);
1798
ncsi_start_dev(struct ncsi_dev * nd)1799 int ncsi_start_dev(struct ncsi_dev *nd)
1800 {
1801 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1802
1803 if (nd->state != ncsi_dev_state_registered &&
1804 nd->state != ncsi_dev_state_functional)
1805 return -ENOTTY;
1806
1807 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1808 ndp->package_probe_id = 0;
1809 ndp->channel_probe_id = 0;
1810 nd->state = ncsi_dev_state_probe;
1811 schedule_work(&ndp->work);
1812 return 0;
1813 }
1814
1815 return ncsi_reset_dev(nd);
1816 }
1817 EXPORT_SYMBOL_GPL(ncsi_start_dev);
1818
ncsi_stop_dev(struct ncsi_dev * nd)1819 void ncsi_stop_dev(struct ncsi_dev *nd)
1820 {
1821 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1822 struct ncsi_package *np;
1823 struct ncsi_channel *nc;
1824 bool chained;
1825 int old_state;
1826 unsigned long flags;
1827
1828 /* Stop the channel monitor on any active channels. Don't reset the
1829 * channel state so we know which were active when ncsi_start_dev()
1830 * is next called.
1831 */
1832 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1833 NCSI_FOR_EACH_CHANNEL(np, nc) {
1834 ncsi_stop_channel_monitor(nc);
1835
1836 spin_lock_irqsave(&nc->lock, flags);
1837 chained = !list_empty(&nc->link);
1838 old_state = nc->state;
1839 spin_unlock_irqrestore(&nc->lock, flags);
1840
1841 WARN_ON_ONCE(chained ||
1842 old_state == NCSI_CHANNEL_INVISIBLE);
1843 }
1844 }
1845
1846 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
1847 ncsi_report_link(ndp, true);
1848 }
1849 EXPORT_SYMBOL_GPL(ncsi_stop_dev);
1850
ncsi_reset_dev(struct ncsi_dev * nd)1851 int ncsi_reset_dev(struct ncsi_dev *nd)
1852 {
1853 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1854 struct ncsi_channel *nc, *active, *tmp;
1855 struct ncsi_package *np;
1856 unsigned long flags;
1857
1858 spin_lock_irqsave(&ndp->lock, flags);
1859
1860 if (!(ndp->flags & NCSI_DEV_RESET)) {
1861 /* Haven't been called yet, check states */
1862 switch (nd->state & ncsi_dev_state_major) {
1863 case ncsi_dev_state_registered:
1864 case ncsi_dev_state_probe:
1865 /* Not even probed yet - do nothing */
1866 spin_unlock_irqrestore(&ndp->lock, flags);
1867 return 0;
1868 case ncsi_dev_state_suspend:
1869 case ncsi_dev_state_config:
1870 /* Wait for the channel to finish its suspend/config
1871 * operation; once it finishes it will check for
1872 * NCSI_DEV_RESET and reset the state.
1873 */
1874 ndp->flags |= NCSI_DEV_RESET;
1875 spin_unlock_irqrestore(&ndp->lock, flags);
1876 return 0;
1877 }
1878 } else {
1879 switch (nd->state) {
1880 case ncsi_dev_state_suspend_done:
1881 case ncsi_dev_state_config_done:
1882 case ncsi_dev_state_functional:
1883 /* Ok */
1884 break;
1885 default:
1886 /* Current reset operation happening */
1887 spin_unlock_irqrestore(&ndp->lock, flags);
1888 return 0;
1889 }
1890 }
1891
1892 if (!list_empty(&ndp->channel_queue)) {
1893 /* Clear any channel queue we may have interrupted */
1894 list_for_each_entry_safe(nc, tmp, &ndp->channel_queue, link)
1895 list_del_init(&nc->link);
1896 }
1897 spin_unlock_irqrestore(&ndp->lock, flags);
1898
1899 active = NULL;
1900 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1901 NCSI_FOR_EACH_CHANNEL(np, nc) {
1902 spin_lock_irqsave(&nc->lock, flags);
1903
1904 if (nc->state == NCSI_CHANNEL_ACTIVE) {
1905 active = nc;
1906 nc->state = NCSI_CHANNEL_INVISIBLE;
1907 spin_unlock_irqrestore(&nc->lock, flags);
1908 ncsi_stop_channel_monitor(nc);
1909 break;
1910 }
1911
1912 spin_unlock_irqrestore(&nc->lock, flags);
1913 }
1914 if (active)
1915 break;
1916 }
1917
1918 if (!active) {
1919 /* Done */
1920 spin_lock_irqsave(&ndp->lock, flags);
1921 ndp->flags &= ~NCSI_DEV_RESET;
1922 spin_unlock_irqrestore(&ndp->lock, flags);
1923 return ncsi_choose_active_channel(ndp);
1924 }
1925
1926 spin_lock_irqsave(&ndp->lock, flags);
1927 ndp->flags |= NCSI_DEV_RESET;
1928 ndp->active_channel = active;
1929 ndp->active_package = active->package;
1930 spin_unlock_irqrestore(&ndp->lock, flags);
1931
1932 nd->state = ncsi_dev_state_suspend;
1933 schedule_work(&ndp->work);
1934 return 0;
1935 }
1936
ncsi_unregister_dev(struct ncsi_dev * nd)1937 void ncsi_unregister_dev(struct ncsi_dev *nd)
1938 {
1939 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1940 struct ncsi_package *np, *tmp;
1941 unsigned long flags;
1942
1943 dev_remove_pack(&ndp->ptype);
1944
1945 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1946 ncsi_remove_package(np);
1947
1948 spin_lock_irqsave(&ncsi_dev_lock, flags);
1949 list_del_rcu(&ndp->node);
1950 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1951
1952 kfree(ndp);
1953 }
1954 EXPORT_SYMBOL_GPL(ncsi_unregister_dev);
1955