2 * Copyright Gavin Shan, IBM Corporation 2016.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/netlink.h>
18 #include <net/net_namespace.h>
20 #include <net/addrconf.h>
22 #include <net/if_inet6.h>
27 LIST_HEAD(ncsi_dev_list
);
28 DEFINE_SPINLOCK(ncsi_dev_lock
);
30 static inline int ncsi_filter_size(int table
)
32 int sizes
[] = { 2, 6, 6, 6 };
34 BUILD_BUG_ON(ARRAY_SIZE(sizes
) != NCSI_FILTER_MAX
);
35 if (table
< NCSI_FILTER_BASE
|| table
>= NCSI_FILTER_MAX
)
41 static u32
*ncsi_get_filter(struct ncsi_channel
*nc
, int table
, int index
)
43 struct ncsi_channel_filter
*ncf
;
46 ncf
= nc
->filters
[table
];
50 size
= ncsi_filter_size(table
);
54 return ncf
->data
+ size
* index
;
57 /* Find the first active filter in a filter table that matches the given
58 * data parameter. If data is NULL, this returns the first active filter.
60 int ncsi_find_filter(struct ncsi_channel
*nc
, int table
, void *data
)
62 struct ncsi_channel_filter
*ncf
;
67 ncf
= nc
->filters
[table
];
71 size
= ncsi_filter_size(table
);
75 spin_lock_irqsave(&nc
->lock
, flags
);
76 bitmap
= (void *)&ncf
->bitmap
;
78 while ((index
= find_next_bit(bitmap
, ncf
->total
, index
+ 1))
80 if (!data
|| !memcmp(ncf
->data
+ size
* index
, data
, size
)) {
81 spin_unlock_irqrestore(&nc
->lock
, flags
);
85 spin_unlock_irqrestore(&nc
->lock
, flags
);
90 int ncsi_add_filter(struct ncsi_channel
*nc
, int table
, void *data
)
92 struct ncsi_channel_filter
*ncf
;
97 size
= ncsi_filter_size(table
);
101 index
= ncsi_find_filter(nc
, table
, data
);
105 ncf
= nc
->filters
[table
];
109 spin_lock_irqsave(&nc
->lock
, flags
);
110 bitmap
= (void *)&ncf
->bitmap
;
112 index
= find_next_zero_bit(bitmap
, ncf
->total
, 0);
113 if (index
>= ncf
->total
) {
114 spin_unlock_irqrestore(&nc
->lock
, flags
);
117 } while (test_and_set_bit(index
, bitmap
));
119 memcpy(ncf
->data
+ size
* index
, data
, size
);
120 spin_unlock_irqrestore(&nc
->lock
, flags
);
125 int ncsi_remove_filter(struct ncsi_channel
*nc
, int table
, int index
)
127 struct ncsi_channel_filter
*ncf
;
132 size
= ncsi_filter_size(table
);
136 ncf
= nc
->filters
[table
];
137 if (!ncf
|| index
>= ncf
->total
)
140 spin_lock_irqsave(&nc
->lock
, flags
);
141 bitmap
= (void *)&ncf
->bitmap
;
142 if (test_and_clear_bit(index
, bitmap
))
143 memset(ncf
->data
+ size
* index
, 0, size
);
144 spin_unlock_irqrestore(&nc
->lock
, flags
);
149 static void ncsi_report_link(struct ncsi_dev_priv
*ndp
, bool force_down
)
151 struct ncsi_dev
*nd
= &ndp
->ndev
;
152 struct ncsi_package
*np
;
153 struct ncsi_channel
*nc
;
156 nd
->state
= ncsi_dev_state_functional
;
163 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
164 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
165 spin_lock_irqsave(&nc
->lock
, flags
);
167 if (!list_empty(&nc
->link
) ||
168 nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
169 spin_unlock_irqrestore(&nc
->lock
, flags
);
173 if (nc
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1) {
174 spin_unlock_irqrestore(&nc
->lock
, flags
);
179 spin_unlock_irqrestore(&nc
->lock
, flags
);
187 static void ncsi_channel_monitor(struct timer_list
*t
)
189 struct ncsi_channel
*nc
= from_timer(nc
, t
, monitor
.timer
);
190 struct ncsi_package
*np
= nc
->package
;
191 struct ncsi_dev_priv
*ndp
= np
->ndp
;
192 struct ncsi_channel_mode
*ncm
;
193 struct ncsi_cmd_arg nca
;
194 bool enabled
, chained
;
195 unsigned int monitor_state
;
199 spin_lock_irqsave(&nc
->lock
, flags
);
201 chained
= !list_empty(&nc
->link
);
202 enabled
= nc
->monitor
.enabled
;
203 monitor_state
= nc
->monitor
.state
;
204 spin_unlock_irqrestore(&nc
->lock
, flags
);
206 if (!enabled
|| chained
) {
207 ncsi_stop_channel_monitor(nc
);
210 if (state
!= NCSI_CHANNEL_INACTIVE
&&
211 state
!= NCSI_CHANNEL_ACTIVE
) {
212 ncsi_stop_channel_monitor(nc
);
216 switch (monitor_state
) {
217 case NCSI_CHANNEL_MONITOR_START
:
218 case NCSI_CHANNEL_MONITOR_RETRY
:
220 nca
.package
= np
->id
;
221 nca
.channel
= nc
->id
;
222 nca
.type
= NCSI_PKT_CMD_GLS
;
224 ret
= ncsi_xmit_cmd(&nca
);
226 netdev_err(ndp
->ndev
.dev
, "Error %d sending GLS\n",
229 case NCSI_CHANNEL_MONITOR_WAIT
... NCSI_CHANNEL_MONITOR_WAIT_MAX
:
232 netdev_err(ndp
->ndev
.dev
, "NCSI Channel %d timed out!\n",
234 if (!(ndp
->flags
& NCSI_DEV_HWA
)) {
235 ncsi_report_link(ndp
, true);
236 ndp
->flags
|= NCSI_DEV_RESHUFFLE
;
239 ncsi_stop_channel_monitor(nc
);
241 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
242 spin_lock_irqsave(&nc
->lock
, flags
);
243 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
244 ncm
->data
[2] &= ~0x1;
245 spin_unlock_irqrestore(&nc
->lock
, flags
);
247 spin_lock_irqsave(&ndp
->lock
, flags
);
248 nc
->state
= NCSI_CHANNEL_ACTIVE
;
249 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
250 spin_unlock_irqrestore(&ndp
->lock
, flags
);
251 ncsi_process_next_channel(ndp
);
255 spin_lock_irqsave(&nc
->lock
, flags
);
257 spin_unlock_irqrestore(&nc
->lock
, flags
);
258 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
261 void ncsi_start_channel_monitor(struct ncsi_channel
*nc
)
265 spin_lock_irqsave(&nc
->lock
, flags
);
266 WARN_ON_ONCE(nc
->monitor
.enabled
);
267 nc
->monitor
.enabled
= true;
268 nc
->monitor
.state
= NCSI_CHANNEL_MONITOR_START
;
269 spin_unlock_irqrestore(&nc
->lock
, flags
);
271 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
274 void ncsi_stop_channel_monitor(struct ncsi_channel
*nc
)
278 spin_lock_irqsave(&nc
->lock
, flags
);
279 if (!nc
->monitor
.enabled
) {
280 spin_unlock_irqrestore(&nc
->lock
, flags
);
283 nc
->monitor
.enabled
= false;
284 spin_unlock_irqrestore(&nc
->lock
, flags
);
286 del_timer_sync(&nc
->monitor
.timer
);
289 struct ncsi_channel
*ncsi_find_channel(struct ncsi_package
*np
,
292 struct ncsi_channel
*nc
;
294 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
302 struct ncsi_channel
*ncsi_add_channel(struct ncsi_package
*np
, unsigned char id
)
304 struct ncsi_channel
*nc
, *tmp
;
308 nc
= kzalloc(sizeof(*nc
), GFP_ATOMIC
);
314 nc
->state
= NCSI_CHANNEL_INACTIVE
;
315 nc
->monitor
.enabled
= false;
316 timer_setup(&nc
->monitor
.timer
, ncsi_channel_monitor
, 0);
317 spin_lock_init(&nc
->lock
);
318 INIT_LIST_HEAD(&nc
->link
);
319 for (index
= 0; index
< NCSI_CAP_MAX
; index
++)
320 nc
->caps
[index
].index
= index
;
321 for (index
= 0; index
< NCSI_MODE_MAX
; index
++)
322 nc
->modes
[index
].index
= index
;
324 spin_lock_irqsave(&np
->lock
, flags
);
325 tmp
= ncsi_find_channel(np
, id
);
327 spin_unlock_irqrestore(&np
->lock
, flags
);
332 list_add_tail_rcu(&nc
->node
, &np
->channels
);
334 spin_unlock_irqrestore(&np
->lock
, flags
);
339 static void ncsi_remove_channel(struct ncsi_channel
*nc
)
341 struct ncsi_package
*np
= nc
->package
;
342 struct ncsi_channel_filter
*ncf
;
346 /* Release filters */
347 spin_lock_irqsave(&nc
->lock
, flags
);
348 for (i
= 0; i
< NCSI_FILTER_MAX
; i
++) {
349 ncf
= nc
->filters
[i
];
353 nc
->filters
[i
] = NULL
;
357 nc
->state
= NCSI_CHANNEL_INACTIVE
;
358 spin_unlock_irqrestore(&nc
->lock
, flags
);
359 ncsi_stop_channel_monitor(nc
);
361 /* Remove and free channel */
362 spin_lock_irqsave(&np
->lock
, flags
);
363 list_del_rcu(&nc
->node
);
365 spin_unlock_irqrestore(&np
->lock
, flags
);
370 struct ncsi_package
*ncsi_find_package(struct ncsi_dev_priv
*ndp
,
373 struct ncsi_package
*np
;
375 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
383 struct ncsi_package
*ncsi_add_package(struct ncsi_dev_priv
*ndp
,
386 struct ncsi_package
*np
, *tmp
;
389 np
= kzalloc(sizeof(*np
), GFP_ATOMIC
);
395 spin_lock_init(&np
->lock
);
396 INIT_LIST_HEAD(&np
->channels
);
398 spin_lock_irqsave(&ndp
->lock
, flags
);
399 tmp
= ncsi_find_package(ndp
, id
);
401 spin_unlock_irqrestore(&ndp
->lock
, flags
);
406 list_add_tail_rcu(&np
->node
, &ndp
->packages
);
408 spin_unlock_irqrestore(&ndp
->lock
, flags
);
413 void ncsi_remove_package(struct ncsi_package
*np
)
415 struct ncsi_dev_priv
*ndp
= np
->ndp
;
416 struct ncsi_channel
*nc
, *tmp
;
419 /* Release all child channels */
420 list_for_each_entry_safe(nc
, tmp
, &np
->channels
, node
)
421 ncsi_remove_channel(nc
);
423 /* Remove and free package */
424 spin_lock_irqsave(&ndp
->lock
, flags
);
425 list_del_rcu(&np
->node
);
427 spin_unlock_irqrestore(&ndp
->lock
, flags
);
432 void ncsi_find_package_and_channel(struct ncsi_dev_priv
*ndp
,
434 struct ncsi_package
**np
,
435 struct ncsi_channel
**nc
)
437 struct ncsi_package
*p
;
438 struct ncsi_channel
*c
;
440 p
= ncsi_find_package(ndp
, NCSI_PACKAGE_INDEX(id
));
441 c
= p
? ncsi_find_channel(p
, NCSI_CHANNEL_INDEX(id
)) : NULL
;
449 /* For two consecutive NCSI commands, the packet IDs shouldn't
450 * be same. Otherwise, the bogus response might be replied. So
451 * the available IDs are allocated in round-robin fashion.
453 struct ncsi_request
*ncsi_alloc_request(struct ncsi_dev_priv
*ndp
,
454 unsigned int req_flags
)
456 struct ncsi_request
*nr
= NULL
;
457 int i
, limit
= ARRAY_SIZE(ndp
->requests
);
460 /* Check if there is one available request until the ceiling */
461 spin_lock_irqsave(&ndp
->lock
, flags
);
462 for (i
= ndp
->request_id
; i
< limit
; i
++) {
463 if (ndp
->requests
[i
].used
)
466 nr
= &ndp
->requests
[i
];
468 nr
->flags
= req_flags
;
469 ndp
->request_id
= i
+ 1;
473 /* Fail back to check from the starting cursor */
474 for (i
= NCSI_REQ_START_IDX
; i
< ndp
->request_id
; i
++) {
475 if (ndp
->requests
[i
].used
)
478 nr
= &ndp
->requests
[i
];
480 nr
->flags
= req_flags
;
481 ndp
->request_id
= i
+ 1;
486 spin_unlock_irqrestore(&ndp
->lock
, flags
);
490 void ncsi_free_request(struct ncsi_request
*nr
)
492 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
493 struct sk_buff
*cmd
, *rsp
;
499 del_timer_sync(&nr
->timer
);
502 spin_lock_irqsave(&ndp
->lock
, flags
);
508 driven
= !!(nr
->flags
& NCSI_REQ_FLAG_EVENT_DRIVEN
);
509 spin_unlock_irqrestore(&ndp
->lock
, flags
);
511 if (driven
&& cmd
&& --ndp
->pending_req_num
== 0)
512 schedule_work(&ndp
->work
);
514 /* Release command and response */
519 struct ncsi_dev
*ncsi_find_dev(struct net_device
*dev
)
521 struct ncsi_dev_priv
*ndp
;
523 NCSI_FOR_EACH_DEV(ndp
) {
524 if (ndp
->ndev
.dev
== dev
)
531 static void ncsi_request_timeout(struct timer_list
*t
)
533 struct ncsi_request
*nr
= from_timer(nr
, t
, timer
);
534 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
537 /* If the request already had associated response,
538 * let the response handler to release it.
540 spin_lock_irqsave(&ndp
->lock
, flags
);
542 if (nr
->rsp
|| !nr
->cmd
) {
543 spin_unlock_irqrestore(&ndp
->lock
, flags
);
546 spin_unlock_irqrestore(&ndp
->lock
, flags
);
548 /* Release the request */
549 ncsi_free_request(nr
);
552 static void ncsi_suspend_channel(struct ncsi_dev_priv
*ndp
)
554 struct ncsi_dev
*nd
= &ndp
->ndev
;
555 struct ncsi_package
*np
= ndp
->active_package
;
556 struct ncsi_channel
*nc
= ndp
->active_channel
;
557 struct ncsi_cmd_arg nca
;
562 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
564 case ncsi_dev_state_suspend
:
565 nd
->state
= ncsi_dev_state_suspend_select
;
567 case ncsi_dev_state_suspend_select
:
568 ndp
->pending_req_num
= 1;
570 nca
.type
= NCSI_PKT_CMD_SP
;
571 nca
.package
= np
->id
;
572 nca
.channel
= NCSI_RESERVED_CHANNEL
;
573 if (ndp
->flags
& NCSI_DEV_HWA
)
578 /* To retrieve the last link states of channels in current
579 * package when current active channel needs fail over to
580 * another one. It means we will possibly select another
581 * channel as next active one. The link states of channels
582 * are most important factor of the selection. So we need
583 * accurate link states. Unfortunately, the link states on
584 * inactive channels can't be updated with LSC AEN in time.
586 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
)
587 nd
->state
= ncsi_dev_state_suspend_gls
;
589 nd
->state
= ncsi_dev_state_suspend_dcnt
;
590 ret
= ncsi_xmit_cmd(&nca
);
595 case ncsi_dev_state_suspend_gls
:
596 ndp
->pending_req_num
= np
->channel_num
;
598 nca
.type
= NCSI_PKT_CMD_GLS
;
599 nca
.package
= np
->id
;
601 nd
->state
= ncsi_dev_state_suspend_dcnt
;
602 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
603 nca
.channel
= nc
->id
;
604 ret
= ncsi_xmit_cmd(&nca
);
610 case ncsi_dev_state_suspend_dcnt
:
611 ndp
->pending_req_num
= 1;
613 nca
.type
= NCSI_PKT_CMD_DCNT
;
614 nca
.package
= np
->id
;
615 nca
.channel
= nc
->id
;
617 nd
->state
= ncsi_dev_state_suspend_dc
;
618 ret
= ncsi_xmit_cmd(&nca
);
623 case ncsi_dev_state_suspend_dc
:
624 ndp
->pending_req_num
= 1;
626 nca
.type
= NCSI_PKT_CMD_DC
;
627 nca
.package
= np
->id
;
628 nca
.channel
= nc
->id
;
631 nd
->state
= ncsi_dev_state_suspend_deselect
;
632 ret
= ncsi_xmit_cmd(&nca
);
637 case ncsi_dev_state_suspend_deselect
:
638 ndp
->pending_req_num
= 1;
640 nca
.type
= NCSI_PKT_CMD_DP
;
641 nca
.package
= np
->id
;
642 nca
.channel
= NCSI_RESERVED_CHANNEL
;
644 nd
->state
= ncsi_dev_state_suspend_done
;
645 ret
= ncsi_xmit_cmd(&nca
);
650 case ncsi_dev_state_suspend_done
:
651 spin_lock_irqsave(&nc
->lock
, flags
);
652 nc
->state
= NCSI_CHANNEL_INACTIVE
;
653 spin_unlock_irqrestore(&nc
->lock
, flags
);
654 ncsi_process_next_channel(ndp
);
658 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in suspend\n",
664 nd
->state
= ncsi_dev_state_functional
;
667 /* Check the VLAN filter bitmap for a set filter, and construct a
668 * "Set VLAN Filter - Disable" packet if found.
670 static int clear_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
671 struct ncsi_cmd_arg
*nca
)
677 index
= ncsi_find_filter(nc
, NCSI_FILTER_VLAN
, NULL
);
679 /* Filter table empty */
683 data
= ncsi_get_filter(nc
, NCSI_FILTER_VLAN
, index
);
685 netdev_err(ndp
->ndev
.dev
,
686 "NCSI: failed to retrieve filter %d\n", index
);
687 /* Set the VLAN id to 0 - this will still disable the entry in
688 * the filter table, but we won't know what it was.
695 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
696 "NCSI: removed vlan tag %u at index %d\n",
698 ncsi_remove_filter(nc
, NCSI_FILTER_VLAN
, index
);
700 nca
->type
= NCSI_PKT_CMD_SVF
;
702 /* HW filter index starts at 1 */
703 nca
->bytes
[6] = index
+ 1;
704 nca
->bytes
[7] = 0x00;
708 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
711 static int set_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
712 struct ncsi_cmd_arg
*nca
)
714 struct vlan_vid
*vlan
= NULL
;
717 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
718 index
= ncsi_find_filter(nc
, NCSI_FILTER_VLAN
, &vlan
->vid
);
721 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
722 "NCSI: new vlan id to set: %u\n",
726 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
727 "vid %u already at filter pos %d\n",
731 if (!vlan
|| index
>= 0) {
732 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
733 "no vlan ids left to set\n");
737 index
= ncsi_add_filter(nc
, NCSI_FILTER_VLAN
, &vlan
->vid
);
739 netdev_err(ndp
->ndev
.dev
,
740 "Failed to add new VLAN tag, error %d\n", index
);
741 if (index
== -ENOSPC
)
742 netdev_err(ndp
->ndev
.dev
,
743 "Channel %u already has all VLAN filters set\n",
748 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
749 "NCSI: set vid %u in packet, index %u\n",
750 vlan
->vid
, index
+ 1);
751 nca
->type
= NCSI_PKT_CMD_SVF
;
752 nca
->words
[1] = vlan
->vid
;
753 /* HW filter index starts at 1 */
754 nca
->bytes
[6] = index
+ 1;
755 nca
->bytes
[7] = 0x01;
760 static void ncsi_configure_channel(struct ncsi_dev_priv
*ndp
)
762 struct ncsi_dev
*nd
= &ndp
->ndev
;
763 struct net_device
*dev
= nd
->dev
;
764 struct ncsi_package
*np
= ndp
->active_package
;
765 struct ncsi_channel
*nc
= ndp
->active_channel
;
766 struct ncsi_channel
*hot_nc
= NULL
;
767 struct ncsi_cmd_arg nca
;
773 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
775 case ncsi_dev_state_config
:
776 case ncsi_dev_state_config_sp
:
777 ndp
->pending_req_num
= 1;
779 /* Select the specific package */
780 nca
.type
= NCSI_PKT_CMD_SP
;
781 if (ndp
->flags
& NCSI_DEV_HWA
)
785 nca
.package
= np
->id
;
786 nca
.channel
= NCSI_RESERVED_CHANNEL
;
787 ret
= ncsi_xmit_cmd(&nca
);
789 netdev_err(ndp
->ndev
.dev
,
790 "NCSI: Failed to transmit CMD_SP\n");
794 nd
->state
= ncsi_dev_state_config_cis
;
796 case ncsi_dev_state_config_cis
:
797 ndp
->pending_req_num
= 1;
799 /* Clear initial state */
800 nca
.type
= NCSI_PKT_CMD_CIS
;
801 nca
.package
= np
->id
;
802 nca
.channel
= nc
->id
;
803 ret
= ncsi_xmit_cmd(&nca
);
805 netdev_err(ndp
->ndev
.dev
,
806 "NCSI: Failed to transmit CMD_CIS\n");
810 nd
->state
= ncsi_dev_state_config_clear_vids
;
812 case ncsi_dev_state_config_clear_vids
:
813 case ncsi_dev_state_config_svf
:
814 case ncsi_dev_state_config_ev
:
815 case ncsi_dev_state_config_sma
:
816 case ncsi_dev_state_config_ebf
:
817 #if IS_ENABLED(CONFIG_IPV6)
818 case ncsi_dev_state_config_egmf
:
820 case ncsi_dev_state_config_ecnt
:
821 case ncsi_dev_state_config_ec
:
822 case ncsi_dev_state_config_ae
:
823 case ncsi_dev_state_config_gls
:
824 ndp
->pending_req_num
= 1;
826 nca
.package
= np
->id
;
827 nca
.channel
= nc
->id
;
829 /* Clear any active filters on the channel before setting */
830 if (nd
->state
== ncsi_dev_state_config_clear_vids
) {
831 ret
= clear_one_vid(ndp
, nc
, &nca
);
833 nd
->state
= ncsi_dev_state_config_svf
;
834 schedule_work(&ndp
->work
);
838 nd
->state
= ncsi_dev_state_config_clear_vids
;
839 /* Add known VLAN tags to the filter */
840 } else if (nd
->state
== ncsi_dev_state_config_svf
) {
841 ret
= set_one_vid(ndp
, nc
, &nca
);
843 nd
->state
= ncsi_dev_state_config_ev
;
844 schedule_work(&ndp
->work
);
848 nd
->state
= ncsi_dev_state_config_svf
;
849 /* Enable/Disable the VLAN filter */
850 } else if (nd
->state
== ncsi_dev_state_config_ev
) {
851 if (list_empty(&ndp
->vlan_vids
)) {
852 nca
.type
= NCSI_PKT_CMD_DV
;
854 nca
.type
= NCSI_PKT_CMD_EV
;
855 nca
.bytes
[3] = NCSI_CAP_VLAN_NO
;
857 nd
->state
= ncsi_dev_state_config_sma
;
858 } else if (nd
->state
== ncsi_dev_state_config_sma
) {
859 /* Use first entry in unicast filter table. Note that
860 * the MAC filter table starts from entry 1 instead of
863 nca
.type
= NCSI_PKT_CMD_SMA
;
864 for (index
= 0; index
< 6; index
++)
865 nca
.bytes
[index
] = dev
->dev_addr
[index
];
868 nd
->state
= ncsi_dev_state_config_ebf
;
869 } else if (nd
->state
== ncsi_dev_state_config_ebf
) {
870 nca
.type
= NCSI_PKT_CMD_EBF
;
871 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_BC
].cap
;
872 nd
->state
= ncsi_dev_state_config_ecnt
;
873 #if IS_ENABLED(CONFIG_IPV6)
874 if (ndp
->inet6_addr_num
> 0 &&
875 (nc
->caps
[NCSI_CAP_GENERIC
].cap
&
876 NCSI_CAP_GENERIC_MC
))
877 nd
->state
= ncsi_dev_state_config_egmf
;
879 nd
->state
= ncsi_dev_state_config_ecnt
;
880 } else if (nd
->state
== ncsi_dev_state_config_egmf
) {
881 nca
.type
= NCSI_PKT_CMD_EGMF
;
882 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
883 nd
->state
= ncsi_dev_state_config_ecnt
;
884 #endif /* CONFIG_IPV6 */
885 } else if (nd
->state
== ncsi_dev_state_config_ecnt
) {
886 nca
.type
= NCSI_PKT_CMD_ECNT
;
887 nd
->state
= ncsi_dev_state_config_ec
;
888 } else if (nd
->state
== ncsi_dev_state_config_ec
) {
889 /* Enable AEN if it's supported */
890 nca
.type
= NCSI_PKT_CMD_EC
;
891 nd
->state
= ncsi_dev_state_config_ae
;
892 if (!(nc
->caps
[NCSI_CAP_AEN
].cap
& NCSI_CAP_AEN_MASK
))
893 nd
->state
= ncsi_dev_state_config_gls
;
894 } else if (nd
->state
== ncsi_dev_state_config_ae
) {
895 nca
.type
= NCSI_PKT_CMD_AE
;
897 nca
.dwords
[1] = nc
->caps
[NCSI_CAP_AEN
].cap
;
898 nd
->state
= ncsi_dev_state_config_gls
;
899 } else if (nd
->state
== ncsi_dev_state_config_gls
) {
900 nca
.type
= NCSI_PKT_CMD_GLS
;
901 nd
->state
= ncsi_dev_state_config_done
;
904 ret
= ncsi_xmit_cmd(&nca
);
906 netdev_err(ndp
->ndev
.dev
,
907 "NCSI: Failed to transmit CMD %x\n",
912 case ncsi_dev_state_config_done
:
913 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
914 "NCSI: channel %u config done\n", nc
->id
);
915 spin_lock_irqsave(&nc
->lock
, flags
);
916 if (nc
->reconfigure_needed
) {
917 /* This channel's configuration has been updated
918 * part-way during the config state - start the
919 * channel configuration over
921 nc
->reconfigure_needed
= false;
922 nc
->state
= NCSI_CHANNEL_INACTIVE
;
923 spin_unlock_irqrestore(&nc
->lock
, flags
);
925 spin_lock_irqsave(&ndp
->lock
, flags
);
926 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
927 spin_unlock_irqrestore(&ndp
->lock
, flags
);
929 netdev_printk(KERN_DEBUG
, dev
,
930 "Dirty NCSI channel state reset\n");
931 ncsi_process_next_channel(ndp
);
935 if (nc
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1) {
937 nc
->state
= NCSI_CHANNEL_ACTIVE
;
940 nc
->state
= NCSI_CHANNEL_INACTIVE
;
941 netdev_warn(ndp
->ndev
.dev
,
942 "NCSI: channel %u link down after config\n",
945 spin_unlock_irqrestore(&nc
->lock
, flags
);
947 /* Update the hot channel */
948 spin_lock_irqsave(&ndp
->lock
, flags
);
949 ndp
->hot_channel
= hot_nc
;
950 spin_unlock_irqrestore(&ndp
->lock
, flags
);
952 ncsi_start_channel_monitor(nc
);
953 ncsi_process_next_channel(ndp
);
956 netdev_alert(dev
, "Wrong NCSI state 0x%x in config\n",
963 ncsi_report_link(ndp
, true);
966 static int ncsi_choose_active_channel(struct ncsi_dev_priv
*ndp
)
968 struct ncsi_package
*np
;
969 struct ncsi_channel
*nc
, *found
, *hot_nc
;
970 struct ncsi_channel_mode
*ncm
;
973 spin_lock_irqsave(&ndp
->lock
, flags
);
974 hot_nc
= ndp
->hot_channel
;
975 spin_unlock_irqrestore(&ndp
->lock
, flags
);
977 /* The search is done once an inactive channel with up
981 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
982 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
983 spin_lock_irqsave(&nc
->lock
, flags
);
985 if (!list_empty(&nc
->link
) ||
986 nc
->state
!= NCSI_CHANNEL_INACTIVE
) {
987 spin_unlock_irqrestore(&nc
->lock
, flags
);
997 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
998 if (ncm
->data
[2] & 0x1) {
999 spin_unlock_irqrestore(&nc
->lock
, flags
);
1004 spin_unlock_irqrestore(&nc
->lock
, flags
);
1009 netdev_warn(ndp
->ndev
.dev
,
1010 "NCSI: No channel found with link\n");
1011 ncsi_report_link(ndp
, true);
1015 ncm
= &found
->modes
[NCSI_MODE_LINK
];
1016 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
1017 "NCSI: Channel %u added to queue (link %s)\n",
1018 found
->id
, ncm
->data
[2] & 0x1 ? "up" : "down");
1021 spin_lock_irqsave(&ndp
->lock
, flags
);
1022 list_add_tail_rcu(&found
->link
, &ndp
->channel_queue
);
1023 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1025 return ncsi_process_next_channel(ndp
);
1028 static bool ncsi_check_hwa(struct ncsi_dev_priv
*ndp
)
1030 struct ncsi_package
*np
;
1031 struct ncsi_channel
*nc
;
1033 bool has_channel
= false;
1035 /* The hardware arbitration is disabled if any one channel
1036 * doesn't support explicitly.
1038 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1039 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1042 cap
= nc
->caps
[NCSI_CAP_GENERIC
].cap
;
1043 if (!(cap
& NCSI_CAP_GENERIC_HWA
) ||
1044 (cap
& NCSI_CAP_GENERIC_HWA_MASK
) !=
1045 NCSI_CAP_GENERIC_HWA_SUPPORT
) {
1046 ndp
->flags
&= ~NCSI_DEV_HWA
;
1053 ndp
->flags
|= NCSI_DEV_HWA
;
1057 ndp
->flags
&= ~NCSI_DEV_HWA
;
1061 static int ncsi_enable_hwa(struct ncsi_dev_priv
*ndp
)
1063 struct ncsi_package
*np
;
1064 struct ncsi_channel
*nc
;
1065 unsigned long flags
;
1067 /* Move all available channels to processing queue */
1068 spin_lock_irqsave(&ndp
->lock
, flags
);
1069 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1070 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1071 WARN_ON_ONCE(nc
->state
!= NCSI_CHANNEL_INACTIVE
||
1072 !list_empty(&nc
->link
));
1073 ncsi_stop_channel_monitor(nc
);
1074 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1077 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1079 /* We can have no channels in extremely case */
1080 if (list_empty(&ndp
->channel_queue
)) {
1081 netdev_err(ndp
->ndev
.dev
,
1082 "NCSI: No available channels for HWA\n");
1083 ncsi_report_link(ndp
, false);
1087 return ncsi_process_next_channel(ndp
);
1090 static void ncsi_probe_channel(struct ncsi_dev_priv
*ndp
)
1092 struct ncsi_dev
*nd
= &ndp
->ndev
;
1093 struct ncsi_package
*np
;
1094 struct ncsi_channel
*nc
;
1095 struct ncsi_cmd_arg nca
;
1096 unsigned char index
;
1100 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
1101 switch (nd
->state
) {
1102 case ncsi_dev_state_probe
:
1103 nd
->state
= ncsi_dev_state_probe_deselect
;
1105 case ncsi_dev_state_probe_deselect
:
1106 ndp
->pending_req_num
= 8;
1108 /* Deselect all possible packages */
1109 nca
.type
= NCSI_PKT_CMD_DP
;
1110 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1111 for (index
= 0; index
< 8; index
++) {
1112 nca
.package
= index
;
1113 ret
= ncsi_xmit_cmd(&nca
);
1118 nd
->state
= ncsi_dev_state_probe_package
;
1120 case ncsi_dev_state_probe_package
:
1121 ndp
->pending_req_num
= 16;
1123 /* Select all possible packages */
1124 nca
.type
= NCSI_PKT_CMD_SP
;
1126 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1127 for (index
= 0; index
< 8; index
++) {
1128 nca
.package
= index
;
1129 ret
= ncsi_xmit_cmd(&nca
);
1134 /* Disable all possible packages */
1135 nca
.type
= NCSI_PKT_CMD_DP
;
1136 for (index
= 0; index
< 8; index
++) {
1137 nca
.package
= index
;
1138 ret
= ncsi_xmit_cmd(&nca
);
1143 nd
->state
= ncsi_dev_state_probe_channel
;
1145 case ncsi_dev_state_probe_channel
:
1146 if (!ndp
->active_package
)
1147 ndp
->active_package
= list_first_or_null_rcu(
1148 &ndp
->packages
, struct ncsi_package
, node
);
1149 else if (list_is_last(&ndp
->active_package
->node
,
1151 ndp
->active_package
= NULL
;
1153 ndp
->active_package
= list_next_entry(
1154 ndp
->active_package
, node
);
1156 /* All available packages and channels are enumerated. The
1157 * enumeration happens for once when the NCSI interface is
1158 * started. So we need continue to start the interface after
1161 * We have to choose an active channel before configuring it.
1162 * Note that we possibly don't have active channel in extreme
1165 if (!ndp
->active_package
) {
1166 ndp
->flags
|= NCSI_DEV_PROBED
;
1167 if (ncsi_check_hwa(ndp
))
1168 ncsi_enable_hwa(ndp
);
1170 ncsi_choose_active_channel(ndp
);
1174 /* Select the active package */
1175 ndp
->pending_req_num
= 1;
1176 nca
.type
= NCSI_PKT_CMD_SP
;
1178 nca
.package
= ndp
->active_package
->id
;
1179 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1180 ret
= ncsi_xmit_cmd(&nca
);
1184 nd
->state
= ncsi_dev_state_probe_cis
;
1186 case ncsi_dev_state_probe_cis
:
1187 ndp
->pending_req_num
= NCSI_RESERVED_CHANNEL
;
1189 /* Clear initial state */
1190 nca
.type
= NCSI_PKT_CMD_CIS
;
1191 nca
.package
= ndp
->active_package
->id
;
1192 for (index
= 0; index
< NCSI_RESERVED_CHANNEL
; index
++) {
1193 nca
.channel
= index
;
1194 ret
= ncsi_xmit_cmd(&nca
);
1199 nd
->state
= ncsi_dev_state_probe_gvi
;
1201 case ncsi_dev_state_probe_gvi
:
1202 case ncsi_dev_state_probe_gc
:
1203 case ncsi_dev_state_probe_gls
:
1204 np
= ndp
->active_package
;
1205 ndp
->pending_req_num
= np
->channel_num
;
1207 /* Retrieve version, capability or link status */
1208 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1209 nca
.type
= NCSI_PKT_CMD_GVI
;
1210 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1211 nca
.type
= NCSI_PKT_CMD_GC
;
1213 nca
.type
= NCSI_PKT_CMD_GLS
;
1215 nca
.package
= np
->id
;
1216 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1217 nca
.channel
= nc
->id
;
1218 ret
= ncsi_xmit_cmd(&nca
);
1223 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1224 nd
->state
= ncsi_dev_state_probe_gc
;
1225 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1226 nd
->state
= ncsi_dev_state_probe_gls
;
1228 nd
->state
= ncsi_dev_state_probe_dp
;
1230 case ncsi_dev_state_probe_dp
:
1231 ndp
->pending_req_num
= 1;
1233 /* Deselect the active package */
1234 nca
.type
= NCSI_PKT_CMD_DP
;
1235 nca
.package
= ndp
->active_package
->id
;
1236 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1237 ret
= ncsi_xmit_cmd(&nca
);
1241 /* Scan channels in next package */
1242 nd
->state
= ncsi_dev_state_probe_channel
;
1245 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%0x in enumeration\n",
1251 netdev_err(ndp
->ndev
.dev
,
1252 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1254 ncsi_report_link(ndp
, true);
1257 static void ncsi_dev_work(struct work_struct
*work
)
1259 struct ncsi_dev_priv
*ndp
= container_of(work
,
1260 struct ncsi_dev_priv
, work
);
1261 struct ncsi_dev
*nd
= &ndp
->ndev
;
1263 switch (nd
->state
& ncsi_dev_state_major
) {
1264 case ncsi_dev_state_probe
:
1265 ncsi_probe_channel(ndp
);
1267 case ncsi_dev_state_suspend
:
1268 ncsi_suspend_channel(ndp
);
1270 case ncsi_dev_state_config
:
1271 ncsi_configure_channel(ndp
);
1274 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in workqueue\n",
1279 int ncsi_process_next_channel(struct ncsi_dev_priv
*ndp
)
1281 struct ncsi_channel
*nc
;
1283 unsigned long flags
;
1285 spin_lock_irqsave(&ndp
->lock
, flags
);
1286 nc
= list_first_or_null_rcu(&ndp
->channel_queue
,
1287 struct ncsi_channel
, link
);
1289 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1293 list_del_init(&nc
->link
);
1294 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1296 spin_lock_irqsave(&nc
->lock
, flags
);
1297 old_state
= nc
->state
;
1298 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1299 spin_unlock_irqrestore(&nc
->lock
, flags
);
1301 ndp
->active_channel
= nc
;
1302 ndp
->active_package
= nc
->package
;
1304 switch (old_state
) {
1305 case NCSI_CHANNEL_INACTIVE
:
1306 ndp
->ndev
.state
= ncsi_dev_state_config
;
1307 netdev_info(ndp
->ndev
.dev
, "NCSI: configuring channel %u\n",
1309 ncsi_configure_channel(ndp
);
1311 case NCSI_CHANNEL_ACTIVE
:
1312 ndp
->ndev
.state
= ncsi_dev_state_suspend
;
1313 netdev_info(ndp
->ndev
.dev
, "NCSI: suspending channel %u\n",
1315 ncsi_suspend_channel(ndp
);
1318 netdev_err(ndp
->ndev
.dev
, "Invalid state 0x%x on %d:%d\n",
1319 old_state
, nc
->package
->id
, nc
->id
);
1320 ncsi_report_link(ndp
, false);
1327 ndp
->active_channel
= NULL
;
1328 ndp
->active_package
= NULL
;
1329 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
) {
1330 ndp
->flags
&= ~NCSI_DEV_RESHUFFLE
;
1331 return ncsi_choose_active_channel(ndp
);
1334 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
1335 "NCSI: No more channels to process\n");
1336 ncsi_report_link(ndp
, false);
1340 #if IS_ENABLED(CONFIG_IPV6)
1341 static int ncsi_inet6addr_event(struct notifier_block
*this,
1342 unsigned long event
, void *data
)
1344 struct inet6_ifaddr
*ifa
= data
;
1345 struct net_device
*dev
= ifa
->idev
->dev
;
1346 struct ncsi_dev
*nd
= ncsi_find_dev(dev
);
1347 struct ncsi_dev_priv
*ndp
= nd
? TO_NCSI_DEV_PRIV(nd
) : NULL
;
1348 struct ncsi_package
*np
;
1349 struct ncsi_channel
*nc
;
1350 struct ncsi_cmd_arg nca
;
1354 if (!ndp
|| (ipv6_addr_type(&ifa
->addr
) &
1355 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
)))
1360 action
= (++ndp
->inet6_addr_num
) == 1;
1361 nca
.type
= NCSI_PKT_CMD_EGMF
;
1364 action
= (--ndp
->inet6_addr_num
== 0);
1365 nca
.type
= NCSI_PKT_CMD_DGMF
;
1371 /* We might not have active channel or packages. The IPv6
1372 * required multicast will be enabled when active channel
1373 * or packages are chosen.
1375 np
= ndp
->active_package
;
1376 nc
= ndp
->active_channel
;
1377 if (!action
|| !np
|| !nc
)
1380 /* We needn't enable or disable it if the function isn't supported */
1381 if (!(nc
->caps
[NCSI_CAP_GENERIC
].cap
& NCSI_CAP_GENERIC_MC
))
1386 nca
.package
= np
->id
;
1387 nca
.channel
= nc
->id
;
1388 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1389 ret
= ncsi_xmit_cmd(&nca
);
1391 netdev_warn(dev
, "Fail to %s global multicast filter (%d)\n",
1392 (event
== NETDEV_UP
) ? "enable" : "disable", ret
);
1399 static struct notifier_block ncsi_inet6addr_notifier
= {
1400 .notifier_call
= ncsi_inet6addr_event
,
1402 #endif /* CONFIG_IPV6 */
1404 static int ncsi_kick_channels(struct ncsi_dev_priv
*ndp
)
1406 struct ncsi_dev
*nd
= &ndp
->ndev
;
1407 struct ncsi_channel
*nc
;
1408 struct ncsi_package
*np
;
1409 unsigned long flags
;
1412 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1413 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1414 spin_lock_irqsave(&nc
->lock
, flags
);
1416 /* Channels may be busy, mark dirty instead of
1418 * a) not ACTIVE (configured)
1419 * b) in the channel_queue (to be configured)
1420 * c) it's ndev is in the config state
1422 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
1423 if ((ndp
->ndev
.state
& 0xff00) ==
1424 ncsi_dev_state_config
||
1425 !list_empty(&nc
->link
)) {
1426 netdev_printk(KERN_DEBUG
, nd
->dev
,
1427 "NCSI: channel %p marked dirty\n",
1429 nc
->reconfigure_needed
= true;
1431 spin_unlock_irqrestore(&nc
->lock
, flags
);
1435 spin_unlock_irqrestore(&nc
->lock
, flags
);
1437 ncsi_stop_channel_monitor(nc
);
1438 spin_lock_irqsave(&nc
->lock
, flags
);
1439 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1440 spin_unlock_irqrestore(&nc
->lock
, flags
);
1442 spin_lock_irqsave(&ndp
->lock
, flags
);
1443 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1444 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1446 netdev_printk(KERN_DEBUG
, nd
->dev
,
1447 "NCSI: kicked channel %p\n", nc
);
1455 int ncsi_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1457 struct ncsi_dev_priv
*ndp
;
1458 unsigned int n_vids
= 0;
1459 struct vlan_vid
*vlan
;
1460 struct ncsi_dev
*nd
;
1466 nd
= ncsi_find_dev(dev
);
1468 netdev_warn(dev
, "NCSI: No net_device?\n");
1472 ndp
= TO_NCSI_DEV_PRIV(nd
);
1474 /* Add the VLAN id to our internal list */
1475 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
1477 if (vlan
->vid
== vid
) {
1478 netdev_printk(KERN_DEBUG
, dev
,
1479 "NCSI: vid %u already registered\n", vid
);
1483 if (n_vids
>= NCSI_MAX_VLAN_VIDS
) {
1485 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1486 vid
, NCSI_MAX_VLAN_VIDS
);
1490 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1494 vlan
->proto
= proto
;
1496 list_add_rcu(&vlan
->list
, &ndp
->vlan_vids
);
1498 netdev_printk(KERN_DEBUG
, dev
, "NCSI: Added new vid %u\n", vid
);
1500 found
= ncsi_kick_channels(ndp
) != 0;
1502 return found
? ncsi_process_next_channel(ndp
) : 0;
1504 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid
);
1506 int ncsi_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1508 struct vlan_vid
*vlan
, *tmp
;
1509 struct ncsi_dev_priv
*ndp
;
1510 struct ncsi_dev
*nd
;
1516 nd
= ncsi_find_dev(dev
);
1518 netdev_warn(dev
, "NCSI: no net_device?\n");
1522 ndp
= TO_NCSI_DEV_PRIV(nd
);
1524 /* Remove the VLAN id from our internal list */
1525 list_for_each_entry_safe(vlan
, tmp
, &ndp
->vlan_vids
, list
)
1526 if (vlan
->vid
== vid
) {
1527 netdev_printk(KERN_DEBUG
, dev
,
1528 "NCSI: vid %u found, removing\n", vid
);
1529 list_del_rcu(&vlan
->list
);
1535 netdev_err(dev
, "NCSI: vid %u wasn't registered!\n", vid
);
1539 found
= ncsi_kick_channels(ndp
) != 0;
1541 return found
? ncsi_process_next_channel(ndp
) : 0;
1543 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid
);
1545 struct ncsi_dev
*ncsi_register_dev(struct net_device
*dev
,
1546 void (*handler
)(struct ncsi_dev
*ndev
))
1548 struct ncsi_dev_priv
*ndp
;
1549 struct ncsi_dev
*nd
;
1550 unsigned long flags
;
1553 /* Check if the device has been registered or not */
1554 nd
= ncsi_find_dev(dev
);
1558 /* Create NCSI device */
1559 ndp
= kzalloc(sizeof(*ndp
), GFP_ATOMIC
);
1564 nd
->state
= ncsi_dev_state_registered
;
1566 nd
->handler
= handler
;
1567 ndp
->pending_req_num
= 0;
1568 INIT_LIST_HEAD(&ndp
->channel_queue
);
1569 INIT_LIST_HEAD(&ndp
->vlan_vids
);
1570 INIT_WORK(&ndp
->work
, ncsi_dev_work
);
1572 /* Initialize private NCSI device */
1573 spin_lock_init(&ndp
->lock
);
1574 INIT_LIST_HEAD(&ndp
->packages
);
1575 ndp
->request_id
= NCSI_REQ_START_IDX
;
1576 for (i
= 0; i
< ARRAY_SIZE(ndp
->requests
); i
++) {
1577 ndp
->requests
[i
].id
= i
;
1578 ndp
->requests
[i
].ndp
= ndp
;
1579 timer_setup(&ndp
->requests
[i
].timer
, ncsi_request_timeout
, 0);
1582 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1583 #if IS_ENABLED(CONFIG_IPV6)
1584 ndp
->inet6_addr_num
= 0;
1585 if (list_empty(&ncsi_dev_list
))
1586 register_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1588 list_add_tail_rcu(&ndp
->node
, &ncsi_dev_list
);
1589 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1591 /* Register NCSI packet Rx handler */
1592 ndp
->ptype
.type
= cpu_to_be16(ETH_P_NCSI
);
1593 ndp
->ptype
.func
= ncsi_rcv_rsp
;
1594 ndp
->ptype
.dev
= dev
;
1595 dev_add_pack(&ndp
->ptype
);
1599 EXPORT_SYMBOL_GPL(ncsi_register_dev
);
1601 int ncsi_start_dev(struct ncsi_dev
*nd
)
1603 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1606 if (nd
->state
!= ncsi_dev_state_registered
&&
1607 nd
->state
!= ncsi_dev_state_functional
)
1610 if (!(ndp
->flags
& NCSI_DEV_PROBED
)) {
1611 nd
->state
= ncsi_dev_state_probe
;
1612 schedule_work(&ndp
->work
);
1616 if (ndp
->flags
& NCSI_DEV_HWA
) {
1617 netdev_info(ndp
->ndev
.dev
, "NCSI: Enabling HWA mode\n");
1618 ret
= ncsi_enable_hwa(ndp
);
1620 ret
= ncsi_choose_active_channel(ndp
);
1625 EXPORT_SYMBOL_GPL(ncsi_start_dev
);
1627 void ncsi_stop_dev(struct ncsi_dev
*nd
)
1629 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1630 struct ncsi_package
*np
;
1631 struct ncsi_channel
*nc
;
1634 unsigned long flags
;
1636 /* Stop the channel monitor and reset channel's state */
1637 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1638 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1639 ncsi_stop_channel_monitor(nc
);
1641 spin_lock_irqsave(&nc
->lock
, flags
);
1642 chained
= !list_empty(&nc
->link
);
1643 old_state
= nc
->state
;
1644 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1645 spin_unlock_irqrestore(&nc
->lock
, flags
);
1647 WARN_ON_ONCE(chained
||
1648 old_state
== NCSI_CHANNEL_INVISIBLE
);
1652 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
, "NCSI: Stopping device\n");
1653 ncsi_report_link(ndp
, true);
1655 EXPORT_SYMBOL_GPL(ncsi_stop_dev
);
1657 void ncsi_unregister_dev(struct ncsi_dev
*nd
)
1659 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1660 struct ncsi_package
*np
, *tmp
;
1661 unsigned long flags
;
1663 dev_remove_pack(&ndp
->ptype
);
1665 list_for_each_entry_safe(np
, tmp
, &ndp
->packages
, node
)
1666 ncsi_remove_package(np
);
1668 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1669 list_del_rcu(&ndp
->node
);
1670 #if IS_ENABLED(CONFIG_IPV6)
1671 if (list_empty(&ncsi_dev_list
))
1672 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1674 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1678 EXPORT_SYMBOL_GPL(ncsi_unregister_dev
);