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>
17 #include <net/net_namespace.h>
19 #include <net/addrconf.h>
21 #include <net/if_inet6.h>
22 #include <net/genetlink.h>
26 #include "ncsi-netlink.h"
28 LIST_HEAD(ncsi_dev_list
);
29 DEFINE_SPINLOCK(ncsi_dev_lock
);
31 bool ncsi_channel_has_link(struct ncsi_channel
*channel
)
33 return !!(channel
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1);
36 bool ncsi_channel_is_last(struct ncsi_dev_priv
*ndp
,
37 struct ncsi_channel
*channel
)
39 struct ncsi_package
*np
;
40 struct ncsi_channel
*nc
;
42 NCSI_FOR_EACH_PACKAGE(ndp
, np
)
43 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
46 if (nc
->state
== NCSI_CHANNEL_ACTIVE
&&
47 ncsi_channel_has_link(nc
))
54 static void ncsi_report_link(struct ncsi_dev_priv
*ndp
, bool force_down
)
56 struct ncsi_dev
*nd
= &ndp
->ndev
;
57 struct ncsi_package
*np
;
58 struct ncsi_channel
*nc
;
61 nd
->state
= ncsi_dev_state_functional
;
68 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
69 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
70 spin_lock_irqsave(&nc
->lock
, flags
);
72 if (!list_empty(&nc
->link
) ||
73 nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
74 spin_unlock_irqrestore(&nc
->lock
, flags
);
78 if (ncsi_channel_has_link(nc
)) {
79 spin_unlock_irqrestore(&nc
->lock
, flags
);
84 spin_unlock_irqrestore(&nc
->lock
, flags
);
92 static void ncsi_channel_monitor(struct timer_list
*t
)
94 struct ncsi_channel
*nc
= from_timer(nc
, t
, monitor
.timer
);
95 struct ncsi_package
*np
= nc
->package
;
96 struct ncsi_dev_priv
*ndp
= np
->ndp
;
97 struct ncsi_channel_mode
*ncm
;
98 struct ncsi_cmd_arg nca
;
99 bool enabled
, chained
;
100 unsigned int monitor_state
;
104 spin_lock_irqsave(&nc
->lock
, flags
);
106 chained
= !list_empty(&nc
->link
);
107 enabled
= nc
->monitor
.enabled
;
108 monitor_state
= nc
->monitor
.state
;
109 spin_unlock_irqrestore(&nc
->lock
, flags
);
111 if (!enabled
|| chained
) {
112 ncsi_stop_channel_monitor(nc
);
115 if (state
!= NCSI_CHANNEL_INACTIVE
&&
116 state
!= NCSI_CHANNEL_ACTIVE
) {
117 ncsi_stop_channel_monitor(nc
);
121 switch (monitor_state
) {
122 case NCSI_CHANNEL_MONITOR_START
:
123 case NCSI_CHANNEL_MONITOR_RETRY
:
125 nca
.package
= np
->id
;
126 nca
.channel
= nc
->id
;
127 nca
.type
= NCSI_PKT_CMD_GLS
;
129 ret
= ncsi_xmit_cmd(&nca
);
131 netdev_err(ndp
->ndev
.dev
, "Error %d sending GLS\n",
134 case NCSI_CHANNEL_MONITOR_WAIT
... NCSI_CHANNEL_MONITOR_WAIT_MAX
:
137 netdev_err(ndp
->ndev
.dev
, "NCSI Channel %d timed out!\n",
139 ncsi_report_link(ndp
, true);
140 ndp
->flags
|= NCSI_DEV_RESHUFFLE
;
142 ncsi_stop_channel_monitor(nc
);
144 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
145 spin_lock_irqsave(&nc
->lock
, flags
);
146 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
147 ncm
->data
[2] &= ~0x1;
148 spin_unlock_irqrestore(&nc
->lock
, flags
);
150 spin_lock_irqsave(&ndp
->lock
, flags
);
151 nc
->state
= NCSI_CHANNEL_ACTIVE
;
152 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
153 spin_unlock_irqrestore(&ndp
->lock
, flags
);
154 ncsi_process_next_channel(ndp
);
158 spin_lock_irqsave(&nc
->lock
, flags
);
160 spin_unlock_irqrestore(&nc
->lock
, flags
);
161 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
164 void ncsi_start_channel_monitor(struct ncsi_channel
*nc
)
168 spin_lock_irqsave(&nc
->lock
, flags
);
169 WARN_ON_ONCE(nc
->monitor
.enabled
);
170 nc
->monitor
.enabled
= true;
171 nc
->monitor
.state
= NCSI_CHANNEL_MONITOR_START
;
172 spin_unlock_irqrestore(&nc
->lock
, flags
);
174 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
177 void ncsi_stop_channel_monitor(struct ncsi_channel
*nc
)
181 spin_lock_irqsave(&nc
->lock
, flags
);
182 if (!nc
->monitor
.enabled
) {
183 spin_unlock_irqrestore(&nc
->lock
, flags
);
186 nc
->monitor
.enabled
= false;
187 spin_unlock_irqrestore(&nc
->lock
, flags
);
189 del_timer_sync(&nc
->monitor
.timer
);
192 struct ncsi_channel
*ncsi_find_channel(struct ncsi_package
*np
,
195 struct ncsi_channel
*nc
;
197 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
205 struct ncsi_channel
*ncsi_add_channel(struct ncsi_package
*np
, unsigned char id
)
207 struct ncsi_channel
*nc
, *tmp
;
211 nc
= kzalloc(sizeof(*nc
), GFP_ATOMIC
);
217 nc
->state
= NCSI_CHANNEL_INACTIVE
;
218 nc
->monitor
.enabled
= false;
219 timer_setup(&nc
->monitor
.timer
, ncsi_channel_monitor
, 0);
220 spin_lock_init(&nc
->lock
);
221 INIT_LIST_HEAD(&nc
->link
);
222 for (index
= 0; index
< NCSI_CAP_MAX
; index
++)
223 nc
->caps
[index
].index
= index
;
224 for (index
= 0; index
< NCSI_MODE_MAX
; index
++)
225 nc
->modes
[index
].index
= index
;
227 spin_lock_irqsave(&np
->lock
, flags
);
228 tmp
= ncsi_find_channel(np
, id
);
230 spin_unlock_irqrestore(&np
->lock
, flags
);
235 list_add_tail_rcu(&nc
->node
, &np
->channels
);
237 spin_unlock_irqrestore(&np
->lock
, flags
);
242 static void ncsi_remove_channel(struct ncsi_channel
*nc
)
244 struct ncsi_package
*np
= nc
->package
;
247 spin_lock_irqsave(&nc
->lock
, flags
);
249 /* Release filters */
250 kfree(nc
->mac_filter
.addrs
);
251 kfree(nc
->vlan_filter
.vids
);
253 nc
->state
= NCSI_CHANNEL_INACTIVE
;
254 spin_unlock_irqrestore(&nc
->lock
, flags
);
255 ncsi_stop_channel_monitor(nc
);
257 /* Remove and free channel */
258 spin_lock_irqsave(&np
->lock
, flags
);
259 list_del_rcu(&nc
->node
);
261 spin_unlock_irqrestore(&np
->lock
, flags
);
266 struct ncsi_package
*ncsi_find_package(struct ncsi_dev_priv
*ndp
,
269 struct ncsi_package
*np
;
271 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
279 struct ncsi_package
*ncsi_add_package(struct ncsi_dev_priv
*ndp
,
282 struct ncsi_package
*np
, *tmp
;
285 np
= kzalloc(sizeof(*np
), GFP_ATOMIC
);
291 spin_lock_init(&np
->lock
);
292 INIT_LIST_HEAD(&np
->channels
);
293 np
->channel_whitelist
= UINT_MAX
;
295 spin_lock_irqsave(&ndp
->lock
, flags
);
296 tmp
= ncsi_find_package(ndp
, id
);
298 spin_unlock_irqrestore(&ndp
->lock
, flags
);
303 list_add_tail_rcu(&np
->node
, &ndp
->packages
);
305 spin_unlock_irqrestore(&ndp
->lock
, flags
);
310 void ncsi_remove_package(struct ncsi_package
*np
)
312 struct ncsi_dev_priv
*ndp
= np
->ndp
;
313 struct ncsi_channel
*nc
, *tmp
;
316 /* Release all child channels */
317 list_for_each_entry_safe(nc
, tmp
, &np
->channels
, node
)
318 ncsi_remove_channel(nc
);
320 /* Remove and free package */
321 spin_lock_irqsave(&ndp
->lock
, flags
);
322 list_del_rcu(&np
->node
);
324 spin_unlock_irqrestore(&ndp
->lock
, flags
);
329 void ncsi_find_package_and_channel(struct ncsi_dev_priv
*ndp
,
331 struct ncsi_package
**np
,
332 struct ncsi_channel
**nc
)
334 struct ncsi_package
*p
;
335 struct ncsi_channel
*c
;
337 p
= ncsi_find_package(ndp
, NCSI_PACKAGE_INDEX(id
));
338 c
= p
? ncsi_find_channel(p
, NCSI_CHANNEL_INDEX(id
)) : NULL
;
346 /* For two consecutive NCSI commands, the packet IDs shouldn't
347 * be same. Otherwise, the bogus response might be replied. So
348 * the available IDs are allocated in round-robin fashion.
350 struct ncsi_request
*ncsi_alloc_request(struct ncsi_dev_priv
*ndp
,
351 unsigned int req_flags
)
353 struct ncsi_request
*nr
= NULL
;
354 int i
, limit
= ARRAY_SIZE(ndp
->requests
);
357 /* Check if there is one available request until the ceiling */
358 spin_lock_irqsave(&ndp
->lock
, flags
);
359 for (i
= ndp
->request_id
; i
< limit
; i
++) {
360 if (ndp
->requests
[i
].used
)
363 nr
= &ndp
->requests
[i
];
365 nr
->flags
= req_flags
;
366 ndp
->request_id
= i
+ 1;
370 /* Fail back to check from the starting cursor */
371 for (i
= NCSI_REQ_START_IDX
; i
< ndp
->request_id
; i
++) {
372 if (ndp
->requests
[i
].used
)
375 nr
= &ndp
->requests
[i
];
377 nr
->flags
= req_flags
;
378 ndp
->request_id
= i
+ 1;
383 spin_unlock_irqrestore(&ndp
->lock
, flags
);
387 void ncsi_free_request(struct ncsi_request
*nr
)
389 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
390 struct sk_buff
*cmd
, *rsp
;
396 del_timer_sync(&nr
->timer
);
399 spin_lock_irqsave(&ndp
->lock
, flags
);
405 driven
= !!(nr
->flags
& NCSI_REQ_FLAG_EVENT_DRIVEN
);
406 spin_unlock_irqrestore(&ndp
->lock
, flags
);
408 if (driven
&& cmd
&& --ndp
->pending_req_num
== 0)
409 schedule_work(&ndp
->work
);
411 /* Release command and response */
416 struct ncsi_dev
*ncsi_find_dev(struct net_device
*dev
)
418 struct ncsi_dev_priv
*ndp
;
420 NCSI_FOR_EACH_DEV(ndp
) {
421 if (ndp
->ndev
.dev
== dev
)
428 static void ncsi_request_timeout(struct timer_list
*t
)
430 struct ncsi_request
*nr
= from_timer(nr
, t
, timer
);
431 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
432 struct ncsi_cmd_pkt
*cmd
;
433 struct ncsi_package
*np
;
434 struct ncsi_channel
*nc
;
437 /* If the request already had associated response,
438 * let the response handler to release it.
440 spin_lock_irqsave(&ndp
->lock
, flags
);
442 if (nr
->rsp
|| !nr
->cmd
) {
443 spin_unlock_irqrestore(&ndp
->lock
, flags
);
446 spin_unlock_irqrestore(&ndp
->lock
, flags
);
448 if (nr
->flags
== NCSI_REQ_FLAG_NETLINK_DRIVEN
) {
450 /* Find the package */
451 cmd
= (struct ncsi_cmd_pkt
*)
452 skb_network_header(nr
->cmd
);
453 ncsi_find_package_and_channel(ndp
,
454 cmd
->cmd
.common
.channel
,
456 ncsi_send_netlink_timeout(nr
, np
, nc
);
460 /* Release the request */
461 ncsi_free_request(nr
);
464 static void ncsi_suspend_channel(struct ncsi_dev_priv
*ndp
)
466 struct ncsi_dev
*nd
= &ndp
->ndev
;
467 struct ncsi_package
*np
;
468 struct ncsi_channel
*nc
, *tmp
;
469 struct ncsi_cmd_arg nca
;
473 np
= ndp
->active_package
;
474 nc
= ndp
->active_channel
;
476 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
478 case ncsi_dev_state_suspend
:
479 nd
->state
= ncsi_dev_state_suspend_select
;
481 case ncsi_dev_state_suspend_select
:
482 ndp
->pending_req_num
= 1;
484 nca
.type
= NCSI_PKT_CMD_SP
;
485 nca
.package
= np
->id
;
486 nca
.channel
= NCSI_RESERVED_CHANNEL
;
487 if (ndp
->flags
& NCSI_DEV_HWA
)
492 /* To retrieve the last link states of channels in current
493 * package when current active channel needs fail over to
494 * another one. It means we will possibly select another
495 * channel as next active one. The link states of channels
496 * are most important factor of the selection. So we need
497 * accurate link states. Unfortunately, the link states on
498 * inactive channels can't be updated with LSC AEN in time.
500 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
)
501 nd
->state
= ncsi_dev_state_suspend_gls
;
503 nd
->state
= ncsi_dev_state_suspend_dcnt
;
504 ret
= ncsi_xmit_cmd(&nca
);
509 case ncsi_dev_state_suspend_gls
:
510 ndp
->pending_req_num
= np
->channel_num
;
512 nca
.type
= NCSI_PKT_CMD_GLS
;
513 nca
.package
= np
->id
;
515 nd
->state
= ncsi_dev_state_suspend_dcnt
;
516 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
517 nca
.channel
= nc
->id
;
518 ret
= ncsi_xmit_cmd(&nca
);
524 case ncsi_dev_state_suspend_dcnt
:
525 ndp
->pending_req_num
= 1;
527 nca
.type
= NCSI_PKT_CMD_DCNT
;
528 nca
.package
= np
->id
;
529 nca
.channel
= nc
->id
;
531 nd
->state
= ncsi_dev_state_suspend_dc
;
532 ret
= ncsi_xmit_cmd(&nca
);
537 case ncsi_dev_state_suspend_dc
:
538 ndp
->pending_req_num
= 1;
540 nca
.type
= NCSI_PKT_CMD_DC
;
541 nca
.package
= np
->id
;
542 nca
.channel
= nc
->id
;
545 nd
->state
= ncsi_dev_state_suspend_deselect
;
546 ret
= ncsi_xmit_cmd(&nca
);
550 NCSI_FOR_EACH_CHANNEL(np
, tmp
) {
551 /* If there is another channel active on this package
552 * do not deselect the package.
554 if (tmp
!= nc
&& tmp
->state
== NCSI_CHANNEL_ACTIVE
) {
555 nd
->state
= ncsi_dev_state_suspend_done
;
560 case ncsi_dev_state_suspend_deselect
:
561 ndp
->pending_req_num
= 1;
563 nca
.type
= NCSI_PKT_CMD_DP
;
564 nca
.package
= np
->id
;
565 nca
.channel
= NCSI_RESERVED_CHANNEL
;
567 nd
->state
= ncsi_dev_state_suspend_done
;
568 ret
= ncsi_xmit_cmd(&nca
);
573 case ncsi_dev_state_suspend_done
:
574 spin_lock_irqsave(&nc
->lock
, flags
);
575 nc
->state
= NCSI_CHANNEL_INACTIVE
;
576 spin_unlock_irqrestore(&nc
->lock
, flags
);
577 if (ndp
->flags
& NCSI_DEV_RESET
)
580 ncsi_process_next_channel(ndp
);
583 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in suspend\n",
589 nd
->state
= ncsi_dev_state_functional
;
592 /* Check the VLAN filter bitmap for a set filter, and construct a
593 * "Set VLAN Filter - Disable" packet if found.
595 static int clear_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
596 struct ncsi_cmd_arg
*nca
)
598 struct ncsi_channel_vlan_filter
*ncf
;
604 ncf
= &nc
->vlan_filter
;
605 bitmap
= &ncf
->bitmap
;
607 spin_lock_irqsave(&nc
->lock
, flags
);
608 index
= find_next_bit(bitmap
, ncf
->n_vids
, 0);
609 if (index
>= ncf
->n_vids
) {
610 spin_unlock_irqrestore(&nc
->lock
, flags
);
613 vid
= ncf
->vids
[index
];
615 clear_bit(index
, bitmap
);
616 ncf
->vids
[index
] = 0;
617 spin_unlock_irqrestore(&nc
->lock
, flags
);
619 nca
->type
= NCSI_PKT_CMD_SVF
;
621 /* HW filter index starts at 1 */
622 nca
->bytes
[6] = index
+ 1;
623 nca
->bytes
[7] = 0x00;
627 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
630 static int set_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
631 struct ncsi_cmd_arg
*nca
)
633 struct ncsi_channel_vlan_filter
*ncf
;
634 struct vlan_vid
*vlan
= NULL
;
640 if (list_empty(&ndp
->vlan_vids
))
643 ncf
= &nc
->vlan_filter
;
644 bitmap
= &ncf
->bitmap
;
646 spin_lock_irqsave(&nc
->lock
, flags
);
649 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
651 for (i
= 0; i
< ncf
->n_vids
; i
++)
652 if (ncf
->vids
[i
] == vid
) {
662 /* No VLAN ID is not set */
663 spin_unlock_irqrestore(&nc
->lock
, flags
);
667 index
= find_next_zero_bit(bitmap
, ncf
->n_vids
, 0);
668 if (index
< 0 || index
>= ncf
->n_vids
) {
669 netdev_err(ndp
->ndev
.dev
,
670 "Channel %u already has all VLAN filters set\n",
672 spin_unlock_irqrestore(&nc
->lock
, flags
);
676 ncf
->vids
[index
] = vid
;
677 set_bit(index
, bitmap
);
678 spin_unlock_irqrestore(&nc
->lock
, flags
);
680 nca
->type
= NCSI_PKT_CMD_SVF
;
682 /* HW filter index starts at 1 */
683 nca
->bytes
[6] = index
+ 1;
684 nca
->bytes
[7] = 0x01;
689 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
691 /* NCSI OEM Command APIs */
692 static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg
*nca
)
694 unsigned char data
[NCSI_OEM_BCM_CMD_GMA_LEN
];
697 nca
->payload
= NCSI_OEM_BCM_CMD_GMA_LEN
;
699 memset(data
, 0, NCSI_OEM_BCM_CMD_GMA_LEN
);
700 *(unsigned int *)data
= ntohl(NCSI_OEM_MFR_BCM_ID
);
701 data
[5] = NCSI_OEM_BCM_CMD_GMA
;
705 ret
= ncsi_xmit_cmd(nca
);
707 netdev_err(nca
->ndp
->ndev
.dev
,
708 "NCSI: Failed to transmit cmd 0x%x during configure\n",
713 static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg
*nca
)
716 u8 data_u8
[NCSI_OEM_MLX_CMD_GMA_LEN
];
717 u32 data_u32
[NCSI_OEM_MLX_CMD_GMA_LEN
/ sizeof(u32
)];
721 nca
->payload
= NCSI_OEM_MLX_CMD_GMA_LEN
;
723 memset(&u
, 0, sizeof(u
));
724 u
.data_u32
[0] = ntohl(NCSI_OEM_MFR_MLX_ID
);
725 u
.data_u8
[5] = NCSI_OEM_MLX_CMD_GMA
;
726 u
.data_u8
[6] = NCSI_OEM_MLX_CMD_GMA_PARAM
;
728 nca
->data
= u
.data_u8
;
730 ret
= ncsi_xmit_cmd(nca
);
732 netdev_err(nca
->ndp
->ndev
.dev
,
733 "NCSI: Failed to transmit cmd 0x%x during configure\n",
738 /* OEM Command handlers initialization */
739 static struct ncsi_oem_gma_handler
{
741 int (*handler
)(struct ncsi_cmd_arg
*nca
);
742 } ncsi_oem_gma_handlers
[] = {
743 { NCSI_OEM_MFR_BCM_ID
, ncsi_oem_gma_handler_bcm
},
744 { NCSI_OEM_MFR_MLX_ID
, ncsi_oem_gma_handler_mlx
}
747 static int ncsi_gma_handler(struct ncsi_cmd_arg
*nca
, unsigned int mf_id
)
749 struct ncsi_oem_gma_handler
*nch
= NULL
;
752 /* This function should only be called once, return if flag set */
753 if (nca
->ndp
->gma_flag
== 1)
756 /* Find gma handler for given manufacturer id */
757 for (i
= 0; i
< ARRAY_SIZE(ncsi_oem_gma_handlers
); i
++) {
758 if (ncsi_oem_gma_handlers
[i
].mfr_id
== mf_id
) {
759 if (ncsi_oem_gma_handlers
[i
].handler
)
760 nch
= &ncsi_oem_gma_handlers
[i
];
766 netdev_err(nca
->ndp
->ndev
.dev
,
767 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
772 /* Set the flag for GMA command which should only be called once */
773 nca
->ndp
->gma_flag
= 1;
775 /* Get Mac address from NCSI device */
776 return nch
->handler(nca
);
779 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
781 /* Determine if a given channel from the channel_queue should be used for Tx */
782 static bool ncsi_channel_is_tx(struct ncsi_dev_priv
*ndp
,
783 struct ncsi_channel
*nc
)
785 struct ncsi_channel_mode
*ncm
;
786 struct ncsi_channel
*channel
;
787 struct ncsi_package
*np
;
789 /* Check if any other channel has Tx enabled; a channel may have already
790 * been configured and removed from the channel queue.
792 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
793 if (!ndp
->multi_package
&& np
!= nc
->package
)
795 NCSI_FOR_EACH_CHANNEL(np
, channel
) {
796 ncm
= &channel
->modes
[NCSI_MODE_TX_ENABLE
];
802 /* This channel is the preferred channel and has link */
803 list_for_each_entry_rcu(channel
, &ndp
->channel_queue
, link
) {
804 np
= channel
->package
;
805 if (np
->preferred_channel
&&
806 ncsi_channel_has_link(np
->preferred_channel
)) {
807 return np
->preferred_channel
== nc
;
811 /* This channel has link */
812 if (ncsi_channel_has_link(nc
))
815 list_for_each_entry_rcu(channel
, &ndp
->channel_queue
, link
)
816 if (ncsi_channel_has_link(channel
))
819 /* No other channel has link; default to this one */
823 /* Change the active Tx channel in a multi-channel setup */
824 int ncsi_update_tx_channel(struct ncsi_dev_priv
*ndp
,
825 struct ncsi_package
*package
,
826 struct ncsi_channel
*disable
,
827 struct ncsi_channel
*enable
)
829 struct ncsi_cmd_arg nca
;
830 struct ncsi_channel
*nc
;
831 struct ncsi_package
*np
;
834 if (!package
->multi_channel
&& !ndp
->multi_package
)
835 netdev_warn(ndp
->ndev
.dev
,
836 "NCSI: Trying to update Tx channel in single-channel mode\n");
840 /* Find current channel with Tx enabled */
841 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
844 if (!ndp
->multi_package
&& np
!= package
)
847 NCSI_FOR_EACH_CHANNEL(np
, nc
)
848 if (nc
->modes
[NCSI_MODE_TX_ENABLE
].enable
) {
854 /* Find a suitable channel for Tx */
855 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
858 if (!ndp
->multi_package
&& np
!= package
)
860 if (!(ndp
->package_whitelist
& (0x1 << np
->id
)))
863 if (np
->preferred_channel
&&
864 ncsi_channel_has_link(np
->preferred_channel
)) {
865 enable
= np
->preferred_channel
;
869 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
870 if (!(np
->channel_whitelist
& 0x1 << nc
->id
))
872 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
)
874 if (ncsi_channel_has_link(nc
)) {
881 if (disable
== enable
)
888 nca
.channel
= disable
->id
;
889 nca
.package
= disable
->package
->id
;
890 nca
.type
= NCSI_PKT_CMD_DCNT
;
891 ret
= ncsi_xmit_cmd(&nca
);
893 netdev_err(ndp
->ndev
.dev
,
894 "Error %d sending DCNT\n",
898 netdev_info(ndp
->ndev
.dev
, "NCSI: channel %u enables Tx\n", enable
->id
);
900 nca
.channel
= enable
->id
;
901 nca
.package
= enable
->package
->id
;
902 nca
.type
= NCSI_PKT_CMD_ECNT
;
903 ret
= ncsi_xmit_cmd(&nca
);
905 netdev_err(ndp
->ndev
.dev
,
906 "Error %d sending ECNT\n",
912 static void ncsi_configure_channel(struct ncsi_dev_priv
*ndp
)
914 struct ncsi_package
*np
= ndp
->active_package
;
915 struct ncsi_channel
*nc
= ndp
->active_channel
;
916 struct ncsi_channel
*hot_nc
= NULL
;
917 struct ncsi_dev
*nd
= &ndp
->ndev
;
918 struct net_device
*dev
= nd
->dev
;
919 struct ncsi_cmd_arg nca
;
925 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
927 case ncsi_dev_state_config
:
928 case ncsi_dev_state_config_sp
:
929 ndp
->pending_req_num
= 1;
931 /* Select the specific package */
932 nca
.type
= NCSI_PKT_CMD_SP
;
933 if (ndp
->flags
& NCSI_DEV_HWA
)
937 nca
.package
= np
->id
;
938 nca
.channel
= NCSI_RESERVED_CHANNEL
;
939 ret
= ncsi_xmit_cmd(&nca
);
941 netdev_err(ndp
->ndev
.dev
,
942 "NCSI: Failed to transmit CMD_SP\n");
946 nd
->state
= ncsi_dev_state_config_cis
;
948 case ncsi_dev_state_config_cis
:
949 ndp
->pending_req_num
= 1;
951 /* Clear initial state */
952 nca
.type
= NCSI_PKT_CMD_CIS
;
953 nca
.package
= np
->id
;
954 nca
.channel
= nc
->id
;
955 ret
= ncsi_xmit_cmd(&nca
);
957 netdev_err(ndp
->ndev
.dev
,
958 "NCSI: Failed to transmit CMD_CIS\n");
962 nd
->state
= ncsi_dev_state_config_oem_gma
;
964 case ncsi_dev_state_config_oem_gma
:
965 nd
->state
= ncsi_dev_state_config_clear_vids
;
968 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
969 nca
.type
= NCSI_PKT_CMD_OEM
;
970 nca
.package
= np
->id
;
971 nca
.channel
= nc
->id
;
972 ndp
->pending_req_num
= 1;
973 ret
= ncsi_gma_handler(&nca
, nc
->version
.mf_id
);
974 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
977 schedule_work(&ndp
->work
);
980 case ncsi_dev_state_config_clear_vids
:
981 case ncsi_dev_state_config_svf
:
982 case ncsi_dev_state_config_ev
:
983 case ncsi_dev_state_config_sma
:
984 case ncsi_dev_state_config_ebf
:
985 #if IS_ENABLED(CONFIG_IPV6)
986 case ncsi_dev_state_config_egmf
:
988 case ncsi_dev_state_config_ecnt
:
989 case ncsi_dev_state_config_ec
:
990 case ncsi_dev_state_config_ae
:
991 case ncsi_dev_state_config_gls
:
992 ndp
->pending_req_num
= 1;
994 nca
.package
= np
->id
;
995 nca
.channel
= nc
->id
;
997 /* Clear any active filters on the channel before setting */
998 if (nd
->state
== ncsi_dev_state_config_clear_vids
) {
999 ret
= clear_one_vid(ndp
, nc
, &nca
);
1001 nd
->state
= ncsi_dev_state_config_svf
;
1002 schedule_work(&ndp
->work
);
1006 nd
->state
= ncsi_dev_state_config_clear_vids
;
1007 /* Add known VLAN tags to the filter */
1008 } else if (nd
->state
== ncsi_dev_state_config_svf
) {
1009 ret
= set_one_vid(ndp
, nc
, &nca
);
1011 nd
->state
= ncsi_dev_state_config_ev
;
1012 schedule_work(&ndp
->work
);
1016 nd
->state
= ncsi_dev_state_config_svf
;
1017 /* Enable/Disable the VLAN filter */
1018 } else if (nd
->state
== ncsi_dev_state_config_ev
) {
1019 if (list_empty(&ndp
->vlan_vids
)) {
1020 nca
.type
= NCSI_PKT_CMD_DV
;
1022 nca
.type
= NCSI_PKT_CMD_EV
;
1023 nca
.bytes
[3] = NCSI_CAP_VLAN_NO
;
1025 nd
->state
= ncsi_dev_state_config_sma
;
1026 } else if (nd
->state
== ncsi_dev_state_config_sma
) {
1027 /* Use first entry in unicast filter table. Note that
1028 * the MAC filter table starts from entry 1 instead of
1031 nca
.type
= NCSI_PKT_CMD_SMA
;
1032 for (index
= 0; index
< 6; index
++)
1033 nca
.bytes
[index
] = dev
->dev_addr
[index
];
1036 nd
->state
= ncsi_dev_state_config_ebf
;
1037 } else if (nd
->state
== ncsi_dev_state_config_ebf
) {
1038 nca
.type
= NCSI_PKT_CMD_EBF
;
1039 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_BC
].cap
;
1040 if (ncsi_channel_is_tx(ndp
, nc
))
1041 nd
->state
= ncsi_dev_state_config_ecnt
;
1043 nd
->state
= ncsi_dev_state_config_ec
;
1044 #if IS_ENABLED(CONFIG_IPV6)
1045 if (ndp
->inet6_addr_num
> 0 &&
1046 (nc
->caps
[NCSI_CAP_GENERIC
].cap
&
1047 NCSI_CAP_GENERIC_MC
))
1048 nd
->state
= ncsi_dev_state_config_egmf
;
1049 } else if (nd
->state
== ncsi_dev_state_config_egmf
) {
1050 nca
.type
= NCSI_PKT_CMD_EGMF
;
1051 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1052 if (ncsi_channel_is_tx(ndp
, nc
))
1053 nd
->state
= ncsi_dev_state_config_ecnt
;
1055 nd
->state
= ncsi_dev_state_config_ec
;
1056 #endif /* CONFIG_IPV6 */
1057 } else if (nd
->state
== ncsi_dev_state_config_ecnt
) {
1058 if (np
->preferred_channel
&&
1059 nc
!= np
->preferred_channel
)
1060 netdev_info(ndp
->ndev
.dev
,
1061 "NCSI: Tx failed over to channel %u\n",
1063 nca
.type
= NCSI_PKT_CMD_ECNT
;
1064 nd
->state
= ncsi_dev_state_config_ec
;
1065 } else if (nd
->state
== ncsi_dev_state_config_ec
) {
1066 /* Enable AEN if it's supported */
1067 nca
.type
= NCSI_PKT_CMD_EC
;
1068 nd
->state
= ncsi_dev_state_config_ae
;
1069 if (!(nc
->caps
[NCSI_CAP_AEN
].cap
& NCSI_CAP_AEN_MASK
))
1070 nd
->state
= ncsi_dev_state_config_gls
;
1071 } else if (nd
->state
== ncsi_dev_state_config_ae
) {
1072 nca
.type
= NCSI_PKT_CMD_AE
;
1074 nca
.dwords
[1] = nc
->caps
[NCSI_CAP_AEN
].cap
;
1075 nd
->state
= ncsi_dev_state_config_gls
;
1076 } else if (nd
->state
== ncsi_dev_state_config_gls
) {
1077 nca
.type
= NCSI_PKT_CMD_GLS
;
1078 nd
->state
= ncsi_dev_state_config_done
;
1081 ret
= ncsi_xmit_cmd(&nca
);
1083 netdev_err(ndp
->ndev
.dev
,
1084 "NCSI: Failed to transmit CMD %x\n",
1089 case ncsi_dev_state_config_done
:
1090 netdev_dbg(ndp
->ndev
.dev
, "NCSI: channel %u config done\n",
1092 spin_lock_irqsave(&nc
->lock
, flags
);
1093 nc
->state
= NCSI_CHANNEL_ACTIVE
;
1095 if (ndp
->flags
& NCSI_DEV_RESET
) {
1096 /* A reset event happened during config, start it now */
1097 nc
->reconfigure_needed
= false;
1098 spin_unlock_irqrestore(&nc
->lock
, flags
);
1103 if (nc
->reconfigure_needed
) {
1104 /* This channel's configuration has been updated
1105 * part-way during the config state - start the
1106 * channel configuration over
1108 nc
->reconfigure_needed
= false;
1109 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1110 spin_unlock_irqrestore(&nc
->lock
, flags
);
1112 spin_lock_irqsave(&ndp
->lock
, flags
);
1113 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1114 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1116 netdev_dbg(dev
, "Dirty NCSI channel state reset\n");
1117 ncsi_process_next_channel(ndp
);
1121 if (nc
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1) {
1125 netdev_dbg(ndp
->ndev
.dev
,
1126 "NCSI: channel %u link down after config\n",
1129 spin_unlock_irqrestore(&nc
->lock
, flags
);
1131 /* Update the hot channel */
1132 spin_lock_irqsave(&ndp
->lock
, flags
);
1133 ndp
->hot_channel
= hot_nc
;
1134 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1136 ncsi_start_channel_monitor(nc
);
1137 ncsi_process_next_channel(ndp
);
1140 netdev_alert(dev
, "Wrong NCSI state 0x%x in config\n",
1147 ncsi_report_link(ndp
, true);
1150 static int ncsi_choose_active_channel(struct ncsi_dev_priv
*ndp
)
1152 struct ncsi_channel
*nc
, *found
, *hot_nc
;
1153 struct ncsi_channel_mode
*ncm
;
1154 unsigned long flags
, cflags
;
1155 struct ncsi_package
*np
;
1158 spin_lock_irqsave(&ndp
->lock
, flags
);
1159 hot_nc
= ndp
->hot_channel
;
1160 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1162 /* By default the search is done once an inactive channel with up
1163 * link is found, unless a preferred channel is set.
1164 * If multi_package or multi_channel are configured all channels in the
1165 * whitelist are added to the channel queue.
1169 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1170 if (!(ndp
->package_whitelist
& (0x1 << np
->id
)))
1172 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1173 if (!(np
->channel_whitelist
& (0x1 << nc
->id
)))
1176 spin_lock_irqsave(&nc
->lock
, cflags
);
1178 if (!list_empty(&nc
->link
) ||
1179 nc
->state
!= NCSI_CHANNEL_INACTIVE
) {
1180 spin_unlock_irqrestore(&nc
->lock
, cflags
);
1190 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
1191 if (ncm
->data
[2] & 0x1) {
1196 /* If multi_channel is enabled configure all valid
1197 * channels whether or not they currently have link
1198 * so they will have AENs enabled.
1200 if (with_link
|| np
->multi_channel
) {
1201 spin_lock_irqsave(&ndp
->lock
, flags
);
1202 list_add_tail_rcu(&nc
->link
,
1203 &ndp
->channel_queue
);
1204 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1206 netdev_dbg(ndp
->ndev
.dev
,
1207 "NCSI: Channel %u added to queue (link %s)\n",
1209 ncm
->data
[2] & 0x1 ? "up" : "down");
1212 spin_unlock_irqrestore(&nc
->lock
, cflags
);
1214 if (with_link
&& !np
->multi_channel
)
1217 if (with_link
&& !ndp
->multi_package
)
1221 if (list_empty(&ndp
->channel_queue
) && found
) {
1222 netdev_info(ndp
->ndev
.dev
,
1223 "NCSI: No channel with link found, configuring channel %u\n",
1225 spin_lock_irqsave(&ndp
->lock
, flags
);
1226 list_add_tail_rcu(&found
->link
, &ndp
->channel_queue
);
1227 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1228 } else if (!found
) {
1229 netdev_warn(ndp
->ndev
.dev
,
1230 "NCSI: No channel found to configure!\n");
1231 ncsi_report_link(ndp
, true);
1235 return ncsi_process_next_channel(ndp
);
1238 static bool ncsi_check_hwa(struct ncsi_dev_priv
*ndp
)
1240 struct ncsi_package
*np
;
1241 struct ncsi_channel
*nc
;
1243 bool has_channel
= false;
1245 /* The hardware arbitration is disabled if any one channel
1246 * doesn't support explicitly.
1248 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1249 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1252 cap
= nc
->caps
[NCSI_CAP_GENERIC
].cap
;
1253 if (!(cap
& NCSI_CAP_GENERIC_HWA
) ||
1254 (cap
& NCSI_CAP_GENERIC_HWA_MASK
) !=
1255 NCSI_CAP_GENERIC_HWA_SUPPORT
) {
1256 ndp
->flags
&= ~NCSI_DEV_HWA
;
1263 ndp
->flags
|= NCSI_DEV_HWA
;
1267 ndp
->flags
&= ~NCSI_DEV_HWA
;
1271 static void ncsi_probe_channel(struct ncsi_dev_priv
*ndp
)
1273 struct ncsi_dev
*nd
= &ndp
->ndev
;
1274 struct ncsi_package
*np
;
1275 struct ncsi_channel
*nc
;
1276 struct ncsi_cmd_arg nca
;
1277 unsigned char index
;
1281 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
1282 switch (nd
->state
) {
1283 case ncsi_dev_state_probe
:
1284 nd
->state
= ncsi_dev_state_probe_deselect
;
1286 case ncsi_dev_state_probe_deselect
:
1287 ndp
->pending_req_num
= 8;
1289 /* Deselect all possible packages */
1290 nca
.type
= NCSI_PKT_CMD_DP
;
1291 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1292 for (index
= 0; index
< 8; index
++) {
1293 nca
.package
= index
;
1294 ret
= ncsi_xmit_cmd(&nca
);
1299 nd
->state
= ncsi_dev_state_probe_package
;
1301 case ncsi_dev_state_probe_package
:
1302 ndp
->pending_req_num
= 1;
1304 nca
.type
= NCSI_PKT_CMD_SP
;
1306 nca
.package
= ndp
->package_probe_id
;
1307 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1308 ret
= ncsi_xmit_cmd(&nca
);
1311 nd
->state
= ncsi_dev_state_probe_channel
;
1313 case ncsi_dev_state_probe_channel
:
1314 ndp
->active_package
= ncsi_find_package(ndp
,
1315 ndp
->package_probe_id
);
1316 if (!ndp
->active_package
) {
1318 nd
->state
= ncsi_dev_state_probe_dp
;
1319 schedule_work(&ndp
->work
);
1322 nd
->state
= ncsi_dev_state_probe_cis
;
1323 schedule_work(&ndp
->work
);
1325 case ncsi_dev_state_probe_cis
:
1326 ndp
->pending_req_num
= NCSI_RESERVED_CHANNEL
;
1328 /* Clear initial state */
1329 nca
.type
= NCSI_PKT_CMD_CIS
;
1330 nca
.package
= ndp
->active_package
->id
;
1331 for (index
= 0; index
< NCSI_RESERVED_CHANNEL
; index
++) {
1332 nca
.channel
= index
;
1333 ret
= ncsi_xmit_cmd(&nca
);
1338 nd
->state
= ncsi_dev_state_probe_gvi
;
1340 case ncsi_dev_state_probe_gvi
:
1341 case ncsi_dev_state_probe_gc
:
1342 case ncsi_dev_state_probe_gls
:
1343 np
= ndp
->active_package
;
1344 ndp
->pending_req_num
= np
->channel_num
;
1346 /* Retrieve version, capability or link status */
1347 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1348 nca
.type
= NCSI_PKT_CMD_GVI
;
1349 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1350 nca
.type
= NCSI_PKT_CMD_GC
;
1352 nca
.type
= NCSI_PKT_CMD_GLS
;
1354 nca
.package
= np
->id
;
1355 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1356 nca
.channel
= nc
->id
;
1357 ret
= ncsi_xmit_cmd(&nca
);
1362 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1363 nd
->state
= ncsi_dev_state_probe_gc
;
1364 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1365 nd
->state
= ncsi_dev_state_probe_gls
;
1367 nd
->state
= ncsi_dev_state_probe_dp
;
1369 case ncsi_dev_state_probe_dp
:
1370 ndp
->pending_req_num
= 1;
1372 /* Deselect the current package */
1373 nca
.type
= NCSI_PKT_CMD_DP
;
1374 nca
.package
= ndp
->package_probe_id
;
1375 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1376 ret
= ncsi_xmit_cmd(&nca
);
1380 /* Probe next package */
1381 ndp
->package_probe_id
++;
1382 if (ndp
->package_probe_id
>= 8) {
1383 /* Probe finished */
1384 ndp
->flags
|= NCSI_DEV_PROBED
;
1387 nd
->state
= ncsi_dev_state_probe_package
;
1388 ndp
->active_package
= NULL
;
1391 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%0x in enumeration\n",
1395 if (ndp
->flags
& NCSI_DEV_PROBED
) {
1396 /* Check if all packages have HWA support */
1397 ncsi_check_hwa(ndp
);
1398 ncsi_choose_active_channel(ndp
);
1403 netdev_err(ndp
->ndev
.dev
,
1404 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1406 ncsi_report_link(ndp
, true);
1409 static void ncsi_dev_work(struct work_struct
*work
)
1411 struct ncsi_dev_priv
*ndp
= container_of(work
,
1412 struct ncsi_dev_priv
, work
);
1413 struct ncsi_dev
*nd
= &ndp
->ndev
;
1415 switch (nd
->state
& ncsi_dev_state_major
) {
1416 case ncsi_dev_state_probe
:
1417 ncsi_probe_channel(ndp
);
1419 case ncsi_dev_state_suspend
:
1420 ncsi_suspend_channel(ndp
);
1422 case ncsi_dev_state_config
:
1423 ncsi_configure_channel(ndp
);
1426 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in workqueue\n",
1431 int ncsi_process_next_channel(struct ncsi_dev_priv
*ndp
)
1433 struct ncsi_channel
*nc
;
1435 unsigned long flags
;
1437 spin_lock_irqsave(&ndp
->lock
, flags
);
1438 nc
= list_first_or_null_rcu(&ndp
->channel_queue
,
1439 struct ncsi_channel
, link
);
1441 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1445 list_del_init(&nc
->link
);
1446 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1448 spin_lock_irqsave(&nc
->lock
, flags
);
1449 old_state
= nc
->state
;
1450 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1451 spin_unlock_irqrestore(&nc
->lock
, flags
);
1453 ndp
->active_channel
= nc
;
1454 ndp
->active_package
= nc
->package
;
1456 switch (old_state
) {
1457 case NCSI_CHANNEL_INACTIVE
:
1458 ndp
->ndev
.state
= ncsi_dev_state_config
;
1459 netdev_dbg(ndp
->ndev
.dev
, "NCSI: configuring channel %u\n",
1461 ncsi_configure_channel(ndp
);
1463 case NCSI_CHANNEL_ACTIVE
:
1464 ndp
->ndev
.state
= ncsi_dev_state_suspend
;
1465 netdev_dbg(ndp
->ndev
.dev
, "NCSI: suspending channel %u\n",
1467 ncsi_suspend_channel(ndp
);
1470 netdev_err(ndp
->ndev
.dev
, "Invalid state 0x%x on %d:%d\n",
1471 old_state
, nc
->package
->id
, nc
->id
);
1472 ncsi_report_link(ndp
, false);
1479 ndp
->active_channel
= NULL
;
1480 ndp
->active_package
= NULL
;
1481 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
) {
1482 ndp
->flags
&= ~NCSI_DEV_RESHUFFLE
;
1483 return ncsi_choose_active_channel(ndp
);
1486 ncsi_report_link(ndp
, false);
1490 #if IS_ENABLED(CONFIG_IPV6)
1491 static int ncsi_inet6addr_event(struct notifier_block
*this,
1492 unsigned long event
, void *data
)
1494 struct inet6_ifaddr
*ifa
= data
;
1495 struct net_device
*dev
= ifa
->idev
->dev
;
1496 struct ncsi_dev
*nd
= ncsi_find_dev(dev
);
1497 struct ncsi_dev_priv
*ndp
= nd
? TO_NCSI_DEV_PRIV(nd
) : NULL
;
1498 struct ncsi_package
*np
;
1499 struct ncsi_channel
*nc
;
1500 struct ncsi_cmd_arg nca
;
1504 if (!ndp
|| (ipv6_addr_type(&ifa
->addr
) &
1505 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
)))
1510 action
= (++ndp
->inet6_addr_num
) == 1;
1511 nca
.type
= NCSI_PKT_CMD_EGMF
;
1514 action
= (--ndp
->inet6_addr_num
== 0);
1515 nca
.type
= NCSI_PKT_CMD_DGMF
;
1521 /* We might not have active channel or packages. The IPv6
1522 * required multicast will be enabled when active channel
1523 * or packages are chosen.
1525 np
= ndp
->active_package
;
1526 nc
= ndp
->active_channel
;
1527 if (!action
|| !np
|| !nc
)
1530 /* We needn't enable or disable it if the function isn't supported */
1531 if (!(nc
->caps
[NCSI_CAP_GENERIC
].cap
& NCSI_CAP_GENERIC_MC
))
1536 nca
.package
= np
->id
;
1537 nca
.channel
= nc
->id
;
1538 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1539 ret
= ncsi_xmit_cmd(&nca
);
1541 netdev_warn(dev
, "Fail to %s global multicast filter (%d)\n",
1542 (event
== NETDEV_UP
) ? "enable" : "disable", ret
);
1549 static struct notifier_block ncsi_inet6addr_notifier
= {
1550 .notifier_call
= ncsi_inet6addr_event
,
1552 #endif /* CONFIG_IPV6 */
1554 static int ncsi_kick_channels(struct ncsi_dev_priv
*ndp
)
1556 struct ncsi_dev
*nd
= &ndp
->ndev
;
1557 struct ncsi_channel
*nc
;
1558 struct ncsi_package
*np
;
1559 unsigned long flags
;
1562 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1563 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1564 spin_lock_irqsave(&nc
->lock
, flags
);
1566 /* Channels may be busy, mark dirty instead of
1568 * a) not ACTIVE (configured)
1569 * b) in the channel_queue (to be configured)
1570 * c) it's ndev is in the config state
1572 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
1573 if ((ndp
->ndev
.state
& 0xff00) ==
1574 ncsi_dev_state_config
||
1575 !list_empty(&nc
->link
)) {
1577 "NCSI: channel %p marked dirty\n",
1579 nc
->reconfigure_needed
= true;
1581 spin_unlock_irqrestore(&nc
->lock
, flags
);
1585 spin_unlock_irqrestore(&nc
->lock
, flags
);
1587 ncsi_stop_channel_monitor(nc
);
1588 spin_lock_irqsave(&nc
->lock
, flags
);
1589 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1590 spin_unlock_irqrestore(&nc
->lock
, flags
);
1592 spin_lock_irqsave(&ndp
->lock
, flags
);
1593 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1594 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1596 netdev_dbg(nd
->dev
, "NCSI: kicked channel %p\n", nc
);
1604 int ncsi_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1606 struct ncsi_dev_priv
*ndp
;
1607 unsigned int n_vids
= 0;
1608 struct vlan_vid
*vlan
;
1609 struct ncsi_dev
*nd
;
1615 nd
= ncsi_find_dev(dev
);
1617 netdev_warn(dev
, "NCSI: No net_device?\n");
1621 ndp
= TO_NCSI_DEV_PRIV(nd
);
1623 /* Add the VLAN id to our internal list */
1624 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
1626 if (vlan
->vid
== vid
) {
1627 netdev_dbg(dev
, "NCSI: vid %u already registered\n",
1632 if (n_vids
>= NCSI_MAX_VLAN_VIDS
) {
1634 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1635 vid
, NCSI_MAX_VLAN_VIDS
);
1639 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1643 vlan
->proto
= proto
;
1645 list_add_rcu(&vlan
->list
, &ndp
->vlan_vids
);
1647 netdev_dbg(dev
, "NCSI: Added new vid %u\n", vid
);
1649 found
= ncsi_kick_channels(ndp
) != 0;
1651 return found
? ncsi_process_next_channel(ndp
) : 0;
1653 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid
);
1655 int ncsi_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1657 struct vlan_vid
*vlan
, *tmp
;
1658 struct ncsi_dev_priv
*ndp
;
1659 struct ncsi_dev
*nd
;
1665 nd
= ncsi_find_dev(dev
);
1667 netdev_warn(dev
, "NCSI: no net_device?\n");
1671 ndp
= TO_NCSI_DEV_PRIV(nd
);
1673 /* Remove the VLAN id from our internal list */
1674 list_for_each_entry_safe(vlan
, tmp
, &ndp
->vlan_vids
, list
)
1675 if (vlan
->vid
== vid
) {
1676 netdev_dbg(dev
, "NCSI: vid %u found, removing\n", vid
);
1677 list_del_rcu(&vlan
->list
);
1683 netdev_err(dev
, "NCSI: vid %u wasn't registered!\n", vid
);
1687 found
= ncsi_kick_channels(ndp
) != 0;
1689 return found
? ncsi_process_next_channel(ndp
) : 0;
1691 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid
);
1693 struct ncsi_dev
*ncsi_register_dev(struct net_device
*dev
,
1694 void (*handler
)(struct ncsi_dev
*ndev
))
1696 struct ncsi_dev_priv
*ndp
;
1697 struct ncsi_dev
*nd
;
1698 unsigned long flags
;
1701 /* Check if the device has been registered or not */
1702 nd
= ncsi_find_dev(dev
);
1706 /* Create NCSI device */
1707 ndp
= kzalloc(sizeof(*ndp
), GFP_ATOMIC
);
1712 nd
->state
= ncsi_dev_state_registered
;
1714 nd
->handler
= handler
;
1715 ndp
->pending_req_num
= 0;
1716 INIT_LIST_HEAD(&ndp
->channel_queue
);
1717 INIT_LIST_HEAD(&ndp
->vlan_vids
);
1718 INIT_WORK(&ndp
->work
, ncsi_dev_work
);
1719 ndp
->package_whitelist
= UINT_MAX
;
1721 /* Initialize private NCSI device */
1722 spin_lock_init(&ndp
->lock
);
1723 INIT_LIST_HEAD(&ndp
->packages
);
1724 ndp
->request_id
= NCSI_REQ_START_IDX
;
1725 for (i
= 0; i
< ARRAY_SIZE(ndp
->requests
); i
++) {
1726 ndp
->requests
[i
].id
= i
;
1727 ndp
->requests
[i
].ndp
= ndp
;
1728 timer_setup(&ndp
->requests
[i
].timer
, ncsi_request_timeout
, 0);
1731 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1732 #if IS_ENABLED(CONFIG_IPV6)
1733 ndp
->inet6_addr_num
= 0;
1734 if (list_empty(&ncsi_dev_list
))
1735 register_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1737 list_add_tail_rcu(&ndp
->node
, &ncsi_dev_list
);
1738 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1740 /* Register NCSI packet Rx handler */
1741 ndp
->ptype
.type
= cpu_to_be16(ETH_P_NCSI
);
1742 ndp
->ptype
.func
= ncsi_rcv_rsp
;
1743 ndp
->ptype
.dev
= dev
;
1744 dev_add_pack(&ndp
->ptype
);
1746 /* Set up generic netlink interface */
1747 ncsi_init_netlink(dev
);
1751 EXPORT_SYMBOL_GPL(ncsi_register_dev
);
1753 int ncsi_start_dev(struct ncsi_dev
*nd
)
1755 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1757 if (nd
->state
!= ncsi_dev_state_registered
&&
1758 nd
->state
!= ncsi_dev_state_functional
)
1761 if (!(ndp
->flags
& NCSI_DEV_PROBED
)) {
1762 ndp
->package_probe_id
= 0;
1763 nd
->state
= ncsi_dev_state_probe
;
1764 schedule_work(&ndp
->work
);
1768 return ncsi_reset_dev(nd
);
1770 EXPORT_SYMBOL_GPL(ncsi_start_dev
);
1772 void ncsi_stop_dev(struct ncsi_dev
*nd
)
1774 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1775 struct ncsi_package
*np
;
1776 struct ncsi_channel
*nc
;
1779 unsigned long flags
;
1781 /* Stop the channel monitor on any active channels. Don't reset the
1782 * channel state so we know which were active when ncsi_start_dev()
1785 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1786 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1787 ncsi_stop_channel_monitor(nc
);
1789 spin_lock_irqsave(&nc
->lock
, flags
);
1790 chained
= !list_empty(&nc
->link
);
1791 old_state
= nc
->state
;
1792 spin_unlock_irqrestore(&nc
->lock
, flags
);
1794 WARN_ON_ONCE(chained
||
1795 old_state
== NCSI_CHANNEL_INVISIBLE
);
1799 netdev_dbg(ndp
->ndev
.dev
, "NCSI: Stopping device\n");
1800 ncsi_report_link(ndp
, true);
1802 EXPORT_SYMBOL_GPL(ncsi_stop_dev
);
1804 int ncsi_reset_dev(struct ncsi_dev
*nd
)
1806 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1807 struct ncsi_channel
*nc
, *active
, *tmp
;
1808 struct ncsi_package
*np
;
1809 unsigned long flags
;
1811 spin_lock_irqsave(&ndp
->lock
, flags
);
1813 if (!(ndp
->flags
& NCSI_DEV_RESET
)) {
1814 /* Haven't been called yet, check states */
1815 switch (nd
->state
& ncsi_dev_state_major
) {
1816 case ncsi_dev_state_registered
:
1817 case ncsi_dev_state_probe
:
1818 /* Not even probed yet - do nothing */
1819 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1821 case ncsi_dev_state_suspend
:
1822 case ncsi_dev_state_config
:
1823 /* Wait for the channel to finish its suspend/config
1824 * operation; once it finishes it will check for
1825 * NCSI_DEV_RESET and reset the state.
1827 ndp
->flags
|= NCSI_DEV_RESET
;
1828 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1832 switch (nd
->state
) {
1833 case ncsi_dev_state_suspend_done
:
1834 case ncsi_dev_state_config_done
:
1835 case ncsi_dev_state_functional
:
1839 /* Current reset operation happening */
1840 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1845 if (!list_empty(&ndp
->channel_queue
)) {
1846 /* Clear any channel queue we may have interrupted */
1847 list_for_each_entry_safe(nc
, tmp
, &ndp
->channel_queue
, link
)
1848 list_del_init(&nc
->link
);
1850 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1853 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1854 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1855 spin_lock_irqsave(&nc
->lock
, flags
);
1857 if (nc
->state
== NCSI_CHANNEL_ACTIVE
) {
1859 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1860 spin_unlock_irqrestore(&nc
->lock
, flags
);
1861 ncsi_stop_channel_monitor(nc
);
1865 spin_unlock_irqrestore(&nc
->lock
, flags
);
1873 spin_lock_irqsave(&ndp
->lock
, flags
);
1874 ndp
->flags
&= ~NCSI_DEV_RESET
;
1875 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1876 return ncsi_choose_active_channel(ndp
);
1879 spin_lock_irqsave(&ndp
->lock
, flags
);
1880 ndp
->flags
|= NCSI_DEV_RESET
;
1881 ndp
->active_channel
= active
;
1882 ndp
->active_package
= active
->package
;
1883 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1885 nd
->state
= ncsi_dev_state_suspend
;
1886 schedule_work(&ndp
->work
);
1890 void ncsi_unregister_dev(struct ncsi_dev
*nd
)
1892 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1893 struct ncsi_package
*np
, *tmp
;
1894 unsigned long flags
;
1896 dev_remove_pack(&ndp
->ptype
);
1898 list_for_each_entry_safe(np
, tmp
, &ndp
->packages
, node
)
1899 ncsi_remove_package(np
);
1901 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1902 list_del_rcu(&ndp
->node
);
1903 #if IS_ENABLED(CONFIG_IPV6)
1904 if (list_empty(&ncsi_dev_list
))
1905 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1907 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1909 ncsi_unregister_netlink(nd
->dev
);
1913 EXPORT_SYMBOL_GPL(ncsi_unregister_dev
);