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(unsigned long data
)
189 struct ncsi_channel
*nc
= (struct ncsi_channel
*)data
;
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 setup_timer(&nc
->monitor
.timer
,
317 ncsi_channel_monitor
, (unsigned long)nc
);
318 spin_lock_init(&nc
->lock
);
319 INIT_LIST_HEAD(&nc
->link
);
320 for (index
= 0; index
< NCSI_CAP_MAX
; index
++)
321 nc
->caps
[index
].index
= index
;
322 for (index
= 0; index
< NCSI_MODE_MAX
; index
++)
323 nc
->modes
[index
].index
= index
;
325 spin_lock_irqsave(&np
->lock
, flags
);
326 tmp
= ncsi_find_channel(np
, id
);
328 spin_unlock_irqrestore(&np
->lock
, flags
);
333 list_add_tail_rcu(&nc
->node
, &np
->channels
);
335 spin_unlock_irqrestore(&np
->lock
, flags
);
340 static void ncsi_remove_channel(struct ncsi_channel
*nc
)
342 struct ncsi_package
*np
= nc
->package
;
343 struct ncsi_channel_filter
*ncf
;
347 /* Release filters */
348 spin_lock_irqsave(&nc
->lock
, flags
);
349 for (i
= 0; i
< NCSI_FILTER_MAX
; i
++) {
350 ncf
= nc
->filters
[i
];
354 nc
->filters
[i
] = NULL
;
358 nc
->state
= NCSI_CHANNEL_INACTIVE
;
359 spin_unlock_irqrestore(&nc
->lock
, flags
);
360 ncsi_stop_channel_monitor(nc
);
362 /* Remove and free channel */
363 spin_lock_irqsave(&np
->lock
, flags
);
364 list_del_rcu(&nc
->node
);
366 spin_unlock_irqrestore(&np
->lock
, flags
);
371 struct ncsi_package
*ncsi_find_package(struct ncsi_dev_priv
*ndp
,
374 struct ncsi_package
*np
;
376 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
384 struct ncsi_package
*ncsi_add_package(struct ncsi_dev_priv
*ndp
,
387 struct ncsi_package
*np
, *tmp
;
390 np
= kzalloc(sizeof(*np
), GFP_ATOMIC
);
396 spin_lock_init(&np
->lock
);
397 INIT_LIST_HEAD(&np
->channels
);
399 spin_lock_irqsave(&ndp
->lock
, flags
);
400 tmp
= ncsi_find_package(ndp
, id
);
402 spin_unlock_irqrestore(&ndp
->lock
, flags
);
407 list_add_tail_rcu(&np
->node
, &ndp
->packages
);
409 spin_unlock_irqrestore(&ndp
->lock
, flags
);
414 void ncsi_remove_package(struct ncsi_package
*np
)
416 struct ncsi_dev_priv
*ndp
= np
->ndp
;
417 struct ncsi_channel
*nc
, *tmp
;
420 /* Release all child channels */
421 list_for_each_entry_safe(nc
, tmp
, &np
->channels
, node
)
422 ncsi_remove_channel(nc
);
424 /* Remove and free package */
425 spin_lock_irqsave(&ndp
->lock
, flags
);
426 list_del_rcu(&np
->node
);
428 spin_unlock_irqrestore(&ndp
->lock
, flags
);
433 void ncsi_find_package_and_channel(struct ncsi_dev_priv
*ndp
,
435 struct ncsi_package
**np
,
436 struct ncsi_channel
**nc
)
438 struct ncsi_package
*p
;
439 struct ncsi_channel
*c
;
441 p
= ncsi_find_package(ndp
, NCSI_PACKAGE_INDEX(id
));
442 c
= p
? ncsi_find_channel(p
, NCSI_CHANNEL_INDEX(id
)) : NULL
;
450 /* For two consecutive NCSI commands, the packet IDs shouldn't
451 * be same. Otherwise, the bogus response might be replied. So
452 * the available IDs are allocated in round-robin fashion.
454 struct ncsi_request
*ncsi_alloc_request(struct ncsi_dev_priv
*ndp
,
455 unsigned int req_flags
)
457 struct ncsi_request
*nr
= NULL
;
458 int i
, limit
= ARRAY_SIZE(ndp
->requests
);
461 /* Check if there is one available request until the ceiling */
462 spin_lock_irqsave(&ndp
->lock
, flags
);
463 for (i
= ndp
->request_id
; i
< limit
; i
++) {
464 if (ndp
->requests
[i
].used
)
467 nr
= &ndp
->requests
[i
];
469 nr
->flags
= req_flags
;
470 ndp
->request_id
= i
+ 1;
474 /* Fail back to check from the starting cursor */
475 for (i
= NCSI_REQ_START_IDX
; i
< ndp
->request_id
; i
++) {
476 if (ndp
->requests
[i
].used
)
479 nr
= &ndp
->requests
[i
];
481 nr
->flags
= req_flags
;
482 ndp
->request_id
= i
+ 1;
487 spin_unlock_irqrestore(&ndp
->lock
, flags
);
491 void ncsi_free_request(struct ncsi_request
*nr
)
493 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
494 struct sk_buff
*cmd
, *rsp
;
500 del_timer_sync(&nr
->timer
);
503 spin_lock_irqsave(&ndp
->lock
, flags
);
509 driven
= !!(nr
->flags
& NCSI_REQ_FLAG_EVENT_DRIVEN
);
510 spin_unlock_irqrestore(&ndp
->lock
, flags
);
512 if (driven
&& cmd
&& --ndp
->pending_req_num
== 0)
513 schedule_work(&ndp
->work
);
515 /* Release command and response */
520 struct ncsi_dev
*ncsi_find_dev(struct net_device
*dev
)
522 struct ncsi_dev_priv
*ndp
;
524 NCSI_FOR_EACH_DEV(ndp
) {
525 if (ndp
->ndev
.dev
== dev
)
532 static void ncsi_request_timeout(unsigned long data
)
534 struct ncsi_request
*nr
= (struct ncsi_request
*)data
;
535 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
538 /* If the request already had associated response,
539 * let the response handler to release it.
541 spin_lock_irqsave(&ndp
->lock
, flags
);
543 if (nr
->rsp
|| !nr
->cmd
) {
544 spin_unlock_irqrestore(&ndp
->lock
, flags
);
547 spin_unlock_irqrestore(&ndp
->lock
, flags
);
549 /* Release the request */
550 ncsi_free_request(nr
);
553 static void ncsi_suspend_channel(struct ncsi_dev_priv
*ndp
)
555 struct ncsi_dev
*nd
= &ndp
->ndev
;
556 struct ncsi_package
*np
= ndp
->active_package
;
557 struct ncsi_channel
*nc
= ndp
->active_channel
;
558 struct ncsi_cmd_arg nca
;
563 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
565 case ncsi_dev_state_suspend
:
566 nd
->state
= ncsi_dev_state_suspend_select
;
568 case ncsi_dev_state_suspend_select
:
569 ndp
->pending_req_num
= 1;
571 nca
.type
= NCSI_PKT_CMD_SP
;
572 nca
.package
= np
->id
;
573 nca
.channel
= NCSI_RESERVED_CHANNEL
;
574 if (ndp
->flags
& NCSI_DEV_HWA
)
579 /* To retrieve the last link states of channels in current
580 * package when current active channel needs fail over to
581 * another one. It means we will possibly select another
582 * channel as next active one. The link states of channels
583 * are most important factor of the selection. So we need
584 * accurate link states. Unfortunately, the link states on
585 * inactive channels can't be updated with LSC AEN in time.
587 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
)
588 nd
->state
= ncsi_dev_state_suspend_gls
;
590 nd
->state
= ncsi_dev_state_suspend_dcnt
;
591 ret
= ncsi_xmit_cmd(&nca
);
596 case ncsi_dev_state_suspend_gls
:
597 ndp
->pending_req_num
= np
->channel_num
;
599 nca
.type
= NCSI_PKT_CMD_GLS
;
600 nca
.package
= np
->id
;
602 nd
->state
= ncsi_dev_state_suspend_dcnt
;
603 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
604 nca
.channel
= nc
->id
;
605 ret
= ncsi_xmit_cmd(&nca
);
611 case ncsi_dev_state_suspend_dcnt
:
612 ndp
->pending_req_num
= 1;
614 nca
.type
= NCSI_PKT_CMD_DCNT
;
615 nca
.package
= np
->id
;
616 nca
.channel
= nc
->id
;
618 nd
->state
= ncsi_dev_state_suspend_dc
;
619 ret
= ncsi_xmit_cmd(&nca
);
624 case ncsi_dev_state_suspend_dc
:
625 ndp
->pending_req_num
= 1;
627 nca
.type
= NCSI_PKT_CMD_DC
;
628 nca
.package
= np
->id
;
629 nca
.channel
= nc
->id
;
632 nd
->state
= ncsi_dev_state_suspend_deselect
;
633 ret
= ncsi_xmit_cmd(&nca
);
638 case ncsi_dev_state_suspend_deselect
:
639 ndp
->pending_req_num
= 1;
641 nca
.type
= NCSI_PKT_CMD_DP
;
642 nca
.package
= np
->id
;
643 nca
.channel
= NCSI_RESERVED_CHANNEL
;
645 nd
->state
= ncsi_dev_state_suspend_done
;
646 ret
= ncsi_xmit_cmd(&nca
);
651 case ncsi_dev_state_suspend_done
:
652 spin_lock_irqsave(&nc
->lock
, flags
);
653 nc
->state
= NCSI_CHANNEL_INACTIVE
;
654 spin_unlock_irqrestore(&nc
->lock
, flags
);
655 ncsi_process_next_channel(ndp
);
659 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in suspend\n",
665 nd
->state
= ncsi_dev_state_functional
;
668 /* Check the VLAN filter bitmap for a set filter, and construct a
669 * "Set VLAN Filter - Disable" packet if found.
671 static int clear_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
672 struct ncsi_cmd_arg
*nca
)
678 index
= ncsi_find_filter(nc
, NCSI_FILTER_VLAN
, NULL
);
680 /* Filter table empty */
684 data
= ncsi_get_filter(nc
, NCSI_FILTER_VLAN
, index
);
686 netdev_err(ndp
->ndev
.dev
,
687 "NCSI: failed to retrieve filter %d\n", index
);
688 /* Set the VLAN id to 0 - this will still disable the entry in
689 * the filter table, but we won't know what it was.
696 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
697 "NCSI: removed vlan tag %u at index %d\n",
699 ncsi_remove_filter(nc
, NCSI_FILTER_VLAN
, index
);
701 nca
->type
= NCSI_PKT_CMD_SVF
;
703 /* HW filter index starts at 1 */
704 nca
->bytes
[6] = index
+ 1;
705 nca
->bytes
[7] = 0x00;
709 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
712 static int set_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
713 struct ncsi_cmd_arg
*nca
)
715 struct vlan_vid
*vlan
= NULL
;
718 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
719 index
= ncsi_find_filter(nc
, NCSI_FILTER_VLAN
, &vlan
->vid
);
722 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
723 "NCSI: new vlan id to set: %u\n",
727 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
728 "vid %u already at filter pos %d\n",
732 if (!vlan
|| index
>= 0) {
733 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
734 "no vlan ids left to set\n");
738 index
= ncsi_add_filter(nc
, NCSI_FILTER_VLAN
, &vlan
->vid
);
740 netdev_err(ndp
->ndev
.dev
,
741 "Failed to add new VLAN tag, error %d\n", index
);
742 if (index
== -ENOSPC
)
743 netdev_err(ndp
->ndev
.dev
,
744 "Channel %u already has all VLAN filters set\n",
749 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
750 "NCSI: set vid %u in packet, index %u\n",
751 vlan
->vid
, index
+ 1);
752 nca
->type
= NCSI_PKT_CMD_SVF
;
753 nca
->words
[1] = vlan
->vid
;
754 /* HW filter index starts at 1 */
755 nca
->bytes
[6] = index
+ 1;
756 nca
->bytes
[7] = 0x01;
761 static void ncsi_configure_channel(struct ncsi_dev_priv
*ndp
)
763 struct ncsi_dev
*nd
= &ndp
->ndev
;
764 struct net_device
*dev
= nd
->dev
;
765 struct ncsi_package
*np
= ndp
->active_package
;
766 struct ncsi_channel
*nc
= ndp
->active_channel
;
767 struct ncsi_channel
*hot_nc
= NULL
;
768 struct ncsi_cmd_arg nca
;
774 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
776 case ncsi_dev_state_config
:
777 case ncsi_dev_state_config_sp
:
778 ndp
->pending_req_num
= 1;
780 /* Select the specific package */
781 nca
.type
= NCSI_PKT_CMD_SP
;
782 if (ndp
->flags
& NCSI_DEV_HWA
)
786 nca
.package
= np
->id
;
787 nca
.channel
= NCSI_RESERVED_CHANNEL
;
788 ret
= ncsi_xmit_cmd(&nca
);
790 netdev_err(ndp
->ndev
.dev
,
791 "NCSI: Failed to transmit CMD_SP\n");
795 nd
->state
= ncsi_dev_state_config_cis
;
797 case ncsi_dev_state_config_cis
:
798 ndp
->pending_req_num
= 1;
800 /* Clear initial state */
801 nca
.type
= NCSI_PKT_CMD_CIS
;
802 nca
.package
= np
->id
;
803 nca
.channel
= nc
->id
;
804 ret
= ncsi_xmit_cmd(&nca
);
806 netdev_err(ndp
->ndev
.dev
,
807 "NCSI: Failed to transmit CMD_CIS\n");
811 nd
->state
= ncsi_dev_state_config_clear_vids
;
813 case ncsi_dev_state_config_clear_vids
:
814 case ncsi_dev_state_config_svf
:
815 case ncsi_dev_state_config_ev
:
816 case ncsi_dev_state_config_sma
:
817 case ncsi_dev_state_config_ebf
:
818 #if IS_ENABLED(CONFIG_IPV6)
819 case ncsi_dev_state_config_egmf
:
821 case ncsi_dev_state_config_ecnt
:
822 case ncsi_dev_state_config_ec
:
823 case ncsi_dev_state_config_ae
:
824 case ncsi_dev_state_config_gls
:
825 ndp
->pending_req_num
= 1;
827 nca
.package
= np
->id
;
828 nca
.channel
= nc
->id
;
830 /* Clear any active filters on the channel before setting */
831 if (nd
->state
== ncsi_dev_state_config_clear_vids
) {
832 ret
= clear_one_vid(ndp
, nc
, &nca
);
834 nd
->state
= ncsi_dev_state_config_svf
;
835 schedule_work(&ndp
->work
);
839 nd
->state
= ncsi_dev_state_config_clear_vids
;
840 /* Add known VLAN tags to the filter */
841 } else if (nd
->state
== ncsi_dev_state_config_svf
) {
842 ret
= set_one_vid(ndp
, nc
, &nca
);
844 nd
->state
= ncsi_dev_state_config_ev
;
845 schedule_work(&ndp
->work
);
849 nd
->state
= ncsi_dev_state_config_svf
;
850 /* Enable/Disable the VLAN filter */
851 } else if (nd
->state
== ncsi_dev_state_config_ev
) {
852 if (list_empty(&ndp
->vlan_vids
)) {
853 nca
.type
= NCSI_PKT_CMD_DV
;
855 nca
.type
= NCSI_PKT_CMD_EV
;
856 nca
.bytes
[3] = NCSI_CAP_VLAN_NO
;
858 nd
->state
= ncsi_dev_state_config_sma
;
859 } else if (nd
->state
== ncsi_dev_state_config_sma
) {
860 /* Use first entry in unicast filter table. Note that
861 * the MAC filter table starts from entry 1 instead of
864 nca
.type
= NCSI_PKT_CMD_SMA
;
865 for (index
= 0; index
< 6; index
++)
866 nca
.bytes
[index
] = dev
->dev_addr
[index
];
869 nd
->state
= ncsi_dev_state_config_ebf
;
870 } else if (nd
->state
== ncsi_dev_state_config_ebf
) {
871 nca
.type
= NCSI_PKT_CMD_EBF
;
872 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_BC
].cap
;
873 nd
->state
= ncsi_dev_state_config_ecnt
;
874 #if IS_ENABLED(CONFIG_IPV6)
875 if (ndp
->inet6_addr_num
> 0 &&
876 (nc
->caps
[NCSI_CAP_GENERIC
].cap
&
877 NCSI_CAP_GENERIC_MC
))
878 nd
->state
= ncsi_dev_state_config_egmf
;
880 nd
->state
= ncsi_dev_state_config_ecnt
;
881 } else if (nd
->state
== ncsi_dev_state_config_egmf
) {
882 nca
.type
= NCSI_PKT_CMD_EGMF
;
883 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
884 nd
->state
= ncsi_dev_state_config_ecnt
;
885 #endif /* CONFIG_IPV6 */
886 } else if (nd
->state
== ncsi_dev_state_config_ecnt
) {
887 nca
.type
= NCSI_PKT_CMD_ECNT
;
888 nd
->state
= ncsi_dev_state_config_ec
;
889 } else if (nd
->state
== ncsi_dev_state_config_ec
) {
890 /* Enable AEN if it's supported */
891 nca
.type
= NCSI_PKT_CMD_EC
;
892 nd
->state
= ncsi_dev_state_config_ae
;
893 if (!(nc
->caps
[NCSI_CAP_AEN
].cap
& NCSI_CAP_AEN_MASK
))
894 nd
->state
= ncsi_dev_state_config_gls
;
895 } else if (nd
->state
== ncsi_dev_state_config_ae
) {
896 nca
.type
= NCSI_PKT_CMD_AE
;
898 nca
.dwords
[1] = nc
->caps
[NCSI_CAP_AEN
].cap
;
899 nd
->state
= ncsi_dev_state_config_gls
;
900 } else if (nd
->state
== ncsi_dev_state_config_gls
) {
901 nca
.type
= NCSI_PKT_CMD_GLS
;
902 nd
->state
= ncsi_dev_state_config_done
;
905 ret
= ncsi_xmit_cmd(&nca
);
907 netdev_err(ndp
->ndev
.dev
,
908 "NCSI: Failed to transmit CMD %x\n",
913 case ncsi_dev_state_config_done
:
914 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
915 "NCSI: channel %u config done\n", nc
->id
);
916 spin_lock_irqsave(&nc
->lock
, flags
);
917 if (nc
->reconfigure_needed
) {
918 /* This channel's configuration has been updated
919 * part-way during the config state - start the
920 * channel configuration over
922 nc
->reconfigure_needed
= false;
923 nc
->state
= NCSI_CHANNEL_INACTIVE
;
924 spin_unlock_irqrestore(&nc
->lock
, flags
);
926 spin_lock_irqsave(&ndp
->lock
, flags
);
927 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
928 spin_unlock_irqrestore(&ndp
->lock
, flags
);
930 netdev_printk(KERN_DEBUG
, dev
,
931 "Dirty NCSI channel state reset\n");
932 ncsi_process_next_channel(ndp
);
936 if (nc
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1) {
938 nc
->state
= NCSI_CHANNEL_ACTIVE
;
941 nc
->state
= NCSI_CHANNEL_INACTIVE
;
942 netdev_warn(ndp
->ndev
.dev
,
943 "NCSI: channel %u link down after config\n",
946 spin_unlock_irqrestore(&nc
->lock
, flags
);
948 /* Update the hot channel */
949 spin_lock_irqsave(&ndp
->lock
, flags
);
950 ndp
->hot_channel
= hot_nc
;
951 spin_unlock_irqrestore(&ndp
->lock
, flags
);
953 ncsi_start_channel_monitor(nc
);
954 ncsi_process_next_channel(ndp
);
957 netdev_alert(dev
, "Wrong NCSI state 0x%x in config\n",
964 ncsi_report_link(ndp
, true);
967 static int ncsi_choose_active_channel(struct ncsi_dev_priv
*ndp
)
969 struct ncsi_package
*np
;
970 struct ncsi_channel
*nc
, *found
, *hot_nc
;
971 struct ncsi_channel_mode
*ncm
;
974 spin_lock_irqsave(&ndp
->lock
, flags
);
975 hot_nc
= ndp
->hot_channel
;
976 spin_unlock_irqrestore(&ndp
->lock
, flags
);
978 /* The search is done once an inactive channel with up
982 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
983 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
984 spin_lock_irqsave(&nc
->lock
, flags
);
986 if (!list_empty(&nc
->link
) ||
987 nc
->state
!= NCSI_CHANNEL_INACTIVE
) {
988 spin_unlock_irqrestore(&nc
->lock
, flags
);
998 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
999 if (ncm
->data
[2] & 0x1) {
1000 spin_unlock_irqrestore(&nc
->lock
, flags
);
1005 spin_unlock_irqrestore(&nc
->lock
, flags
);
1010 netdev_warn(ndp
->ndev
.dev
,
1011 "NCSI: No channel found with link\n");
1012 ncsi_report_link(ndp
, true);
1016 ncm
= &found
->modes
[NCSI_MODE_LINK
];
1017 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
1018 "NCSI: Channel %u added to queue (link %s)\n",
1019 found
->id
, ncm
->data
[2] & 0x1 ? "up" : "down");
1022 spin_lock_irqsave(&ndp
->lock
, flags
);
1023 list_add_tail_rcu(&found
->link
, &ndp
->channel_queue
);
1024 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1026 return ncsi_process_next_channel(ndp
);
1029 static bool ncsi_check_hwa(struct ncsi_dev_priv
*ndp
)
1031 struct ncsi_package
*np
;
1032 struct ncsi_channel
*nc
;
1034 bool has_channel
= false;
1036 /* The hardware arbitration is disabled if any one channel
1037 * doesn't support explicitly.
1039 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1040 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1043 cap
= nc
->caps
[NCSI_CAP_GENERIC
].cap
;
1044 if (!(cap
& NCSI_CAP_GENERIC_HWA
) ||
1045 (cap
& NCSI_CAP_GENERIC_HWA_MASK
) !=
1046 NCSI_CAP_GENERIC_HWA_SUPPORT
) {
1047 ndp
->flags
&= ~NCSI_DEV_HWA
;
1054 ndp
->flags
|= NCSI_DEV_HWA
;
1058 ndp
->flags
&= ~NCSI_DEV_HWA
;
1062 static int ncsi_enable_hwa(struct ncsi_dev_priv
*ndp
)
1064 struct ncsi_package
*np
;
1065 struct ncsi_channel
*nc
;
1066 unsigned long flags
;
1068 /* Move all available channels to processing queue */
1069 spin_lock_irqsave(&ndp
->lock
, flags
);
1070 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1071 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1072 WARN_ON_ONCE(nc
->state
!= NCSI_CHANNEL_INACTIVE
||
1073 !list_empty(&nc
->link
));
1074 ncsi_stop_channel_monitor(nc
);
1075 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1078 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1080 /* We can have no channels in extremely case */
1081 if (list_empty(&ndp
->channel_queue
)) {
1082 netdev_err(ndp
->ndev
.dev
,
1083 "NCSI: No available channels for HWA\n");
1084 ncsi_report_link(ndp
, false);
1088 return ncsi_process_next_channel(ndp
);
1091 static void ncsi_probe_channel(struct ncsi_dev_priv
*ndp
)
1093 struct ncsi_dev
*nd
= &ndp
->ndev
;
1094 struct ncsi_package
*np
;
1095 struct ncsi_channel
*nc
;
1096 struct ncsi_cmd_arg nca
;
1097 unsigned char index
;
1101 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
1102 switch (nd
->state
) {
1103 case ncsi_dev_state_probe
:
1104 nd
->state
= ncsi_dev_state_probe_deselect
;
1106 case ncsi_dev_state_probe_deselect
:
1107 ndp
->pending_req_num
= 8;
1109 /* Deselect all possible packages */
1110 nca
.type
= NCSI_PKT_CMD_DP
;
1111 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1112 for (index
= 0; index
< 8; index
++) {
1113 nca
.package
= index
;
1114 ret
= ncsi_xmit_cmd(&nca
);
1119 nd
->state
= ncsi_dev_state_probe_package
;
1121 case ncsi_dev_state_probe_package
:
1122 ndp
->pending_req_num
= 16;
1124 /* Select all possible packages */
1125 nca
.type
= NCSI_PKT_CMD_SP
;
1127 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1128 for (index
= 0; index
< 8; index
++) {
1129 nca
.package
= index
;
1130 ret
= ncsi_xmit_cmd(&nca
);
1135 /* Disable all possible packages */
1136 nca
.type
= NCSI_PKT_CMD_DP
;
1137 for (index
= 0; index
< 8; index
++) {
1138 nca
.package
= index
;
1139 ret
= ncsi_xmit_cmd(&nca
);
1144 nd
->state
= ncsi_dev_state_probe_channel
;
1146 case ncsi_dev_state_probe_channel
:
1147 if (!ndp
->active_package
)
1148 ndp
->active_package
= list_first_or_null_rcu(
1149 &ndp
->packages
, struct ncsi_package
, node
);
1150 else if (list_is_last(&ndp
->active_package
->node
,
1152 ndp
->active_package
= NULL
;
1154 ndp
->active_package
= list_next_entry(
1155 ndp
->active_package
, node
);
1157 /* All available packages and channels are enumerated. The
1158 * enumeration happens for once when the NCSI interface is
1159 * started. So we need continue to start the interface after
1162 * We have to choose an active channel before configuring it.
1163 * Note that we possibly don't have active channel in extreme
1166 if (!ndp
->active_package
) {
1167 ndp
->flags
|= NCSI_DEV_PROBED
;
1168 if (ncsi_check_hwa(ndp
))
1169 ncsi_enable_hwa(ndp
);
1171 ncsi_choose_active_channel(ndp
);
1175 /* Select the active package */
1176 ndp
->pending_req_num
= 1;
1177 nca
.type
= NCSI_PKT_CMD_SP
;
1179 nca
.package
= ndp
->active_package
->id
;
1180 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1181 ret
= ncsi_xmit_cmd(&nca
);
1185 nd
->state
= ncsi_dev_state_probe_cis
;
1187 case ncsi_dev_state_probe_cis
:
1188 ndp
->pending_req_num
= NCSI_RESERVED_CHANNEL
;
1190 /* Clear initial state */
1191 nca
.type
= NCSI_PKT_CMD_CIS
;
1192 nca
.package
= ndp
->active_package
->id
;
1193 for (index
= 0; index
< NCSI_RESERVED_CHANNEL
; index
++) {
1194 nca
.channel
= index
;
1195 ret
= ncsi_xmit_cmd(&nca
);
1200 nd
->state
= ncsi_dev_state_probe_gvi
;
1202 case ncsi_dev_state_probe_gvi
:
1203 case ncsi_dev_state_probe_gc
:
1204 case ncsi_dev_state_probe_gls
:
1205 np
= ndp
->active_package
;
1206 ndp
->pending_req_num
= np
->channel_num
;
1208 /* Retrieve version, capability or link status */
1209 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1210 nca
.type
= NCSI_PKT_CMD_GVI
;
1211 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1212 nca
.type
= NCSI_PKT_CMD_GC
;
1214 nca
.type
= NCSI_PKT_CMD_GLS
;
1216 nca
.package
= np
->id
;
1217 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1218 nca
.channel
= nc
->id
;
1219 ret
= ncsi_xmit_cmd(&nca
);
1224 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1225 nd
->state
= ncsi_dev_state_probe_gc
;
1226 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1227 nd
->state
= ncsi_dev_state_probe_gls
;
1229 nd
->state
= ncsi_dev_state_probe_dp
;
1231 case ncsi_dev_state_probe_dp
:
1232 ndp
->pending_req_num
= 1;
1234 /* Deselect the active package */
1235 nca
.type
= NCSI_PKT_CMD_DP
;
1236 nca
.package
= ndp
->active_package
->id
;
1237 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1238 ret
= ncsi_xmit_cmd(&nca
);
1242 /* Scan channels in next package */
1243 nd
->state
= ncsi_dev_state_probe_channel
;
1246 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%0x in enumeration\n",
1252 netdev_err(ndp
->ndev
.dev
,
1253 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1255 ncsi_report_link(ndp
, true);
1258 static void ncsi_dev_work(struct work_struct
*work
)
1260 struct ncsi_dev_priv
*ndp
= container_of(work
,
1261 struct ncsi_dev_priv
, work
);
1262 struct ncsi_dev
*nd
= &ndp
->ndev
;
1264 switch (nd
->state
& ncsi_dev_state_major
) {
1265 case ncsi_dev_state_probe
:
1266 ncsi_probe_channel(ndp
);
1268 case ncsi_dev_state_suspend
:
1269 ncsi_suspend_channel(ndp
);
1271 case ncsi_dev_state_config
:
1272 ncsi_configure_channel(ndp
);
1275 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in workqueue\n",
1280 int ncsi_process_next_channel(struct ncsi_dev_priv
*ndp
)
1282 struct ncsi_channel
*nc
;
1284 unsigned long flags
;
1286 spin_lock_irqsave(&ndp
->lock
, flags
);
1287 nc
= list_first_or_null_rcu(&ndp
->channel_queue
,
1288 struct ncsi_channel
, link
);
1290 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1294 list_del_init(&nc
->link
);
1295 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1297 spin_lock_irqsave(&nc
->lock
, flags
);
1298 old_state
= nc
->state
;
1299 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1300 spin_unlock_irqrestore(&nc
->lock
, flags
);
1302 ndp
->active_channel
= nc
;
1303 ndp
->active_package
= nc
->package
;
1305 switch (old_state
) {
1306 case NCSI_CHANNEL_INACTIVE
:
1307 ndp
->ndev
.state
= ncsi_dev_state_config
;
1308 netdev_info(ndp
->ndev
.dev
, "NCSI: configuring channel %u\n",
1310 ncsi_configure_channel(ndp
);
1312 case NCSI_CHANNEL_ACTIVE
:
1313 ndp
->ndev
.state
= ncsi_dev_state_suspend
;
1314 netdev_info(ndp
->ndev
.dev
, "NCSI: suspending channel %u\n",
1316 ncsi_suspend_channel(ndp
);
1319 netdev_err(ndp
->ndev
.dev
, "Invalid state 0x%x on %d:%d\n",
1320 old_state
, nc
->package
->id
, nc
->id
);
1321 ncsi_report_link(ndp
, false);
1328 ndp
->active_channel
= NULL
;
1329 ndp
->active_package
= NULL
;
1330 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
) {
1331 ndp
->flags
&= ~NCSI_DEV_RESHUFFLE
;
1332 return ncsi_choose_active_channel(ndp
);
1335 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
,
1336 "NCSI: No more channels to process\n");
1337 ncsi_report_link(ndp
, false);
1341 #if IS_ENABLED(CONFIG_IPV6)
1342 static int ncsi_inet6addr_event(struct notifier_block
*this,
1343 unsigned long event
, void *data
)
1345 struct inet6_ifaddr
*ifa
= data
;
1346 struct net_device
*dev
= ifa
->idev
->dev
;
1347 struct ncsi_dev
*nd
= ncsi_find_dev(dev
);
1348 struct ncsi_dev_priv
*ndp
= nd
? TO_NCSI_DEV_PRIV(nd
) : NULL
;
1349 struct ncsi_package
*np
;
1350 struct ncsi_channel
*nc
;
1351 struct ncsi_cmd_arg nca
;
1355 if (!ndp
|| (ipv6_addr_type(&ifa
->addr
) &
1356 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
)))
1361 action
= (++ndp
->inet6_addr_num
) == 1;
1362 nca
.type
= NCSI_PKT_CMD_EGMF
;
1365 action
= (--ndp
->inet6_addr_num
== 0);
1366 nca
.type
= NCSI_PKT_CMD_DGMF
;
1372 /* We might not have active channel or packages. The IPv6
1373 * required multicast will be enabled when active channel
1374 * or packages are chosen.
1376 np
= ndp
->active_package
;
1377 nc
= ndp
->active_channel
;
1378 if (!action
|| !np
|| !nc
)
1381 /* We needn't enable or disable it if the function isn't supported */
1382 if (!(nc
->caps
[NCSI_CAP_GENERIC
].cap
& NCSI_CAP_GENERIC_MC
))
1387 nca
.package
= np
->id
;
1388 nca
.channel
= nc
->id
;
1389 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1390 ret
= ncsi_xmit_cmd(&nca
);
1392 netdev_warn(dev
, "Fail to %s global multicast filter (%d)\n",
1393 (event
== NETDEV_UP
) ? "enable" : "disable", ret
);
1400 static struct notifier_block ncsi_inet6addr_notifier
= {
1401 .notifier_call
= ncsi_inet6addr_event
,
1403 #endif /* CONFIG_IPV6 */
1405 static int ncsi_kick_channels(struct ncsi_dev_priv
*ndp
)
1407 struct ncsi_dev
*nd
= &ndp
->ndev
;
1408 struct ncsi_channel
*nc
;
1409 struct ncsi_package
*np
;
1410 unsigned long flags
;
1413 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1414 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1415 spin_lock_irqsave(&nc
->lock
, flags
);
1417 /* Channels may be busy, mark dirty instead of
1419 * a) not ACTIVE (configured)
1420 * b) in the channel_queue (to be configured)
1421 * c) it's ndev is in the config state
1423 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
1424 if ((ndp
->ndev
.state
& 0xff00) ==
1425 ncsi_dev_state_config
||
1426 !list_empty(&nc
->link
)) {
1427 netdev_printk(KERN_DEBUG
, nd
->dev
,
1428 "NCSI: channel %p marked dirty\n",
1430 nc
->reconfigure_needed
= true;
1432 spin_unlock_irqrestore(&nc
->lock
, flags
);
1436 spin_unlock_irqrestore(&nc
->lock
, flags
);
1438 ncsi_stop_channel_monitor(nc
);
1439 spin_lock_irqsave(&nc
->lock
, flags
);
1440 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1441 spin_unlock_irqrestore(&nc
->lock
, flags
);
1443 spin_lock_irqsave(&ndp
->lock
, flags
);
1444 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1445 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1447 netdev_printk(KERN_DEBUG
, nd
->dev
,
1448 "NCSI: kicked channel %p\n", nc
);
1456 int ncsi_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1458 struct ncsi_dev_priv
*ndp
;
1459 unsigned int n_vids
= 0;
1460 struct vlan_vid
*vlan
;
1461 struct ncsi_dev
*nd
;
1467 nd
= ncsi_find_dev(dev
);
1469 netdev_warn(dev
, "NCSI: No net_device?\n");
1473 ndp
= TO_NCSI_DEV_PRIV(nd
);
1475 /* Add the VLAN id to our internal list */
1476 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
1478 if (vlan
->vid
== vid
) {
1479 netdev_printk(KERN_DEBUG
, dev
,
1480 "NCSI: vid %u already registered\n", vid
);
1484 if (n_vids
>= NCSI_MAX_VLAN_VIDS
) {
1486 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1487 vid
, NCSI_MAX_VLAN_VIDS
);
1491 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1495 vlan
->proto
= proto
;
1497 list_add_rcu(&vlan
->list
, &ndp
->vlan_vids
);
1499 netdev_printk(KERN_DEBUG
, dev
, "NCSI: Added new vid %u\n", vid
);
1501 found
= ncsi_kick_channels(ndp
) != 0;
1503 return found
? ncsi_process_next_channel(ndp
) : 0;
1505 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid
);
1507 int ncsi_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1509 struct vlan_vid
*vlan
, *tmp
;
1510 struct ncsi_dev_priv
*ndp
;
1511 struct ncsi_dev
*nd
;
1517 nd
= ncsi_find_dev(dev
);
1519 netdev_warn(dev
, "NCSI: no net_device?\n");
1523 ndp
= TO_NCSI_DEV_PRIV(nd
);
1525 /* Remove the VLAN id from our internal list */
1526 list_for_each_entry_safe(vlan
, tmp
, &ndp
->vlan_vids
, list
)
1527 if (vlan
->vid
== vid
) {
1528 netdev_printk(KERN_DEBUG
, dev
,
1529 "NCSI: vid %u found, removing\n", vid
);
1530 list_del_rcu(&vlan
->list
);
1536 netdev_err(dev
, "NCSI: vid %u wasn't registered!\n", vid
);
1540 found
= ncsi_kick_channels(ndp
) != 0;
1542 return found
? ncsi_process_next_channel(ndp
) : 0;
1544 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid
);
1546 struct ncsi_dev
*ncsi_register_dev(struct net_device
*dev
,
1547 void (*handler
)(struct ncsi_dev
*ndev
))
1549 struct ncsi_dev_priv
*ndp
;
1550 struct ncsi_dev
*nd
;
1551 unsigned long flags
;
1554 /* Check if the device has been registered or not */
1555 nd
= ncsi_find_dev(dev
);
1559 /* Create NCSI device */
1560 ndp
= kzalloc(sizeof(*ndp
), GFP_ATOMIC
);
1565 nd
->state
= ncsi_dev_state_registered
;
1567 nd
->handler
= handler
;
1568 ndp
->pending_req_num
= 0;
1569 INIT_LIST_HEAD(&ndp
->channel_queue
);
1570 INIT_LIST_HEAD(&ndp
->vlan_vids
);
1571 INIT_WORK(&ndp
->work
, ncsi_dev_work
);
1573 /* Initialize private NCSI device */
1574 spin_lock_init(&ndp
->lock
);
1575 INIT_LIST_HEAD(&ndp
->packages
);
1576 ndp
->request_id
= NCSI_REQ_START_IDX
;
1577 for (i
= 0; i
< ARRAY_SIZE(ndp
->requests
); i
++) {
1578 ndp
->requests
[i
].id
= i
;
1579 ndp
->requests
[i
].ndp
= ndp
;
1580 setup_timer(&ndp
->requests
[i
].timer
,
1581 ncsi_request_timeout
,
1582 (unsigned long)&ndp
->requests
[i
]);
1585 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1586 #if IS_ENABLED(CONFIG_IPV6)
1587 ndp
->inet6_addr_num
= 0;
1588 if (list_empty(&ncsi_dev_list
))
1589 register_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1591 list_add_tail_rcu(&ndp
->node
, &ncsi_dev_list
);
1592 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1594 /* Register NCSI packet Rx handler */
1595 ndp
->ptype
.type
= cpu_to_be16(ETH_P_NCSI
);
1596 ndp
->ptype
.func
= ncsi_rcv_rsp
;
1597 ndp
->ptype
.dev
= dev
;
1598 dev_add_pack(&ndp
->ptype
);
1602 EXPORT_SYMBOL_GPL(ncsi_register_dev
);
1604 int ncsi_start_dev(struct ncsi_dev
*nd
)
1606 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1609 if (nd
->state
!= ncsi_dev_state_registered
&&
1610 nd
->state
!= ncsi_dev_state_functional
)
1613 if (!(ndp
->flags
& NCSI_DEV_PROBED
)) {
1614 nd
->state
= ncsi_dev_state_probe
;
1615 schedule_work(&ndp
->work
);
1619 if (ndp
->flags
& NCSI_DEV_HWA
) {
1620 netdev_info(ndp
->ndev
.dev
, "NCSI: Enabling HWA mode\n");
1621 ret
= ncsi_enable_hwa(ndp
);
1623 ret
= ncsi_choose_active_channel(ndp
);
1628 EXPORT_SYMBOL_GPL(ncsi_start_dev
);
1630 void ncsi_stop_dev(struct ncsi_dev
*nd
)
1632 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1633 struct ncsi_package
*np
;
1634 struct ncsi_channel
*nc
;
1637 unsigned long flags
;
1639 /* Stop the channel monitor and reset channel's state */
1640 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1641 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1642 ncsi_stop_channel_monitor(nc
);
1644 spin_lock_irqsave(&nc
->lock
, flags
);
1645 chained
= !list_empty(&nc
->link
);
1646 old_state
= nc
->state
;
1647 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1648 spin_unlock_irqrestore(&nc
->lock
, flags
);
1650 WARN_ON_ONCE(chained
||
1651 old_state
== NCSI_CHANNEL_INVISIBLE
);
1655 netdev_printk(KERN_DEBUG
, ndp
->ndev
.dev
, "NCSI: Stopping device\n");
1656 ncsi_report_link(ndp
, true);
1658 EXPORT_SYMBOL_GPL(ncsi_stop_dev
);
1660 void ncsi_unregister_dev(struct ncsi_dev
*nd
)
1662 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1663 struct ncsi_package
*np
, *tmp
;
1664 unsigned long flags
;
1666 dev_remove_pack(&ndp
->ptype
);
1668 list_for_each_entry_safe(np
, tmp
, &ndp
->packages
, node
)
1669 ncsi_remove_package(np
);
1671 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1672 list_del_rcu(&ndp
->node
);
1673 #if IS_ENABLED(CONFIG_IPV6)
1674 if (list_empty(&ncsi_dev_list
))
1675 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1677 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1681 EXPORT_SYMBOL_GPL(ncsi_unregister_dev
);