1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright Gavin Shan, IBM Corporation 2016.
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
13 #include <net/net_namespace.h>
15 #include <net/addrconf.h>
17 #include <net/if_inet6.h>
18 #include <net/genetlink.h>
22 #include "ncsi-netlink.h"
24 LIST_HEAD(ncsi_dev_list
);
25 DEFINE_SPINLOCK(ncsi_dev_lock
);
27 bool ncsi_channel_has_link(struct ncsi_channel
*channel
)
29 return !!(channel
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1);
32 bool ncsi_channel_is_last(struct ncsi_dev_priv
*ndp
,
33 struct ncsi_channel
*channel
)
35 struct ncsi_package
*np
;
36 struct ncsi_channel
*nc
;
38 NCSI_FOR_EACH_PACKAGE(ndp
, np
)
39 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
42 if (nc
->state
== NCSI_CHANNEL_ACTIVE
&&
43 ncsi_channel_has_link(nc
))
50 static void ncsi_report_link(struct ncsi_dev_priv
*ndp
, bool force_down
)
52 struct ncsi_dev
*nd
= &ndp
->ndev
;
53 struct ncsi_package
*np
;
54 struct ncsi_channel
*nc
;
57 nd
->state
= ncsi_dev_state_functional
;
64 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
65 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
66 spin_lock_irqsave(&nc
->lock
, flags
);
68 if (!list_empty(&nc
->link
) ||
69 nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
70 spin_unlock_irqrestore(&nc
->lock
, flags
);
74 if (ncsi_channel_has_link(nc
)) {
75 spin_unlock_irqrestore(&nc
->lock
, flags
);
80 spin_unlock_irqrestore(&nc
->lock
, flags
);
88 static void ncsi_channel_monitor(struct timer_list
*t
)
90 struct ncsi_channel
*nc
= from_timer(nc
, t
, monitor
.timer
);
91 struct ncsi_package
*np
= nc
->package
;
92 struct ncsi_dev_priv
*ndp
= np
->ndp
;
93 struct ncsi_channel_mode
*ncm
;
94 struct ncsi_cmd_arg nca
;
95 bool enabled
, chained
;
96 unsigned int monitor_state
;
100 spin_lock_irqsave(&nc
->lock
, flags
);
102 chained
= !list_empty(&nc
->link
);
103 enabled
= nc
->monitor
.enabled
;
104 monitor_state
= nc
->monitor
.state
;
105 spin_unlock_irqrestore(&nc
->lock
, flags
);
107 if (!enabled
|| chained
) {
108 ncsi_stop_channel_monitor(nc
);
111 if (state
!= NCSI_CHANNEL_INACTIVE
&&
112 state
!= NCSI_CHANNEL_ACTIVE
) {
113 ncsi_stop_channel_monitor(nc
);
117 switch (monitor_state
) {
118 case NCSI_CHANNEL_MONITOR_START
:
119 case NCSI_CHANNEL_MONITOR_RETRY
:
121 nca
.package
= np
->id
;
122 nca
.channel
= nc
->id
;
123 nca
.type
= NCSI_PKT_CMD_GLS
;
125 ret
= ncsi_xmit_cmd(&nca
);
127 netdev_err(ndp
->ndev
.dev
, "Error %d sending GLS\n",
130 case NCSI_CHANNEL_MONITOR_WAIT
... NCSI_CHANNEL_MONITOR_WAIT_MAX
:
133 netdev_err(ndp
->ndev
.dev
, "NCSI Channel %d timed out!\n",
135 ncsi_report_link(ndp
, true);
136 ndp
->flags
|= NCSI_DEV_RESHUFFLE
;
138 ncsi_stop_channel_monitor(nc
);
140 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
141 spin_lock_irqsave(&nc
->lock
, flags
);
142 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
143 ncm
->data
[2] &= ~0x1;
144 spin_unlock_irqrestore(&nc
->lock
, flags
);
146 spin_lock_irqsave(&ndp
->lock
, flags
);
147 nc
->state
= NCSI_CHANNEL_ACTIVE
;
148 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
149 spin_unlock_irqrestore(&ndp
->lock
, flags
);
150 ncsi_process_next_channel(ndp
);
154 spin_lock_irqsave(&nc
->lock
, flags
);
156 spin_unlock_irqrestore(&nc
->lock
, flags
);
157 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
160 void ncsi_start_channel_monitor(struct ncsi_channel
*nc
)
164 spin_lock_irqsave(&nc
->lock
, flags
);
165 WARN_ON_ONCE(nc
->monitor
.enabled
);
166 nc
->monitor
.enabled
= true;
167 nc
->monitor
.state
= NCSI_CHANNEL_MONITOR_START
;
168 spin_unlock_irqrestore(&nc
->lock
, flags
);
170 mod_timer(&nc
->monitor
.timer
, jiffies
+ HZ
);
173 void ncsi_stop_channel_monitor(struct ncsi_channel
*nc
)
177 spin_lock_irqsave(&nc
->lock
, flags
);
178 if (!nc
->monitor
.enabled
) {
179 spin_unlock_irqrestore(&nc
->lock
, flags
);
182 nc
->monitor
.enabled
= false;
183 spin_unlock_irqrestore(&nc
->lock
, flags
);
185 del_timer_sync(&nc
->monitor
.timer
);
188 struct ncsi_channel
*ncsi_find_channel(struct ncsi_package
*np
,
191 struct ncsi_channel
*nc
;
193 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
201 struct ncsi_channel
*ncsi_add_channel(struct ncsi_package
*np
, unsigned char id
)
203 struct ncsi_channel
*nc
, *tmp
;
207 nc
= kzalloc(sizeof(*nc
), GFP_ATOMIC
);
213 nc
->state
= NCSI_CHANNEL_INACTIVE
;
214 nc
->monitor
.enabled
= false;
215 timer_setup(&nc
->monitor
.timer
, ncsi_channel_monitor
, 0);
216 spin_lock_init(&nc
->lock
);
217 INIT_LIST_HEAD(&nc
->link
);
218 for (index
= 0; index
< NCSI_CAP_MAX
; index
++)
219 nc
->caps
[index
].index
= index
;
220 for (index
= 0; index
< NCSI_MODE_MAX
; index
++)
221 nc
->modes
[index
].index
= index
;
223 spin_lock_irqsave(&np
->lock
, flags
);
224 tmp
= ncsi_find_channel(np
, id
);
226 spin_unlock_irqrestore(&np
->lock
, flags
);
231 list_add_tail_rcu(&nc
->node
, &np
->channels
);
233 spin_unlock_irqrestore(&np
->lock
, flags
);
238 static void ncsi_remove_channel(struct ncsi_channel
*nc
)
240 struct ncsi_package
*np
= nc
->package
;
243 spin_lock_irqsave(&nc
->lock
, flags
);
245 /* Release filters */
246 kfree(nc
->mac_filter
.addrs
);
247 kfree(nc
->vlan_filter
.vids
);
249 nc
->state
= NCSI_CHANNEL_INACTIVE
;
250 spin_unlock_irqrestore(&nc
->lock
, flags
);
251 ncsi_stop_channel_monitor(nc
);
253 /* Remove and free channel */
254 spin_lock_irqsave(&np
->lock
, flags
);
255 list_del_rcu(&nc
->node
);
257 spin_unlock_irqrestore(&np
->lock
, flags
);
262 struct ncsi_package
*ncsi_find_package(struct ncsi_dev_priv
*ndp
,
265 struct ncsi_package
*np
;
267 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
275 struct ncsi_package
*ncsi_add_package(struct ncsi_dev_priv
*ndp
,
278 struct ncsi_package
*np
, *tmp
;
281 np
= kzalloc(sizeof(*np
), GFP_ATOMIC
);
287 spin_lock_init(&np
->lock
);
288 INIT_LIST_HEAD(&np
->channels
);
289 np
->channel_whitelist
= UINT_MAX
;
291 spin_lock_irqsave(&ndp
->lock
, flags
);
292 tmp
= ncsi_find_package(ndp
, id
);
294 spin_unlock_irqrestore(&ndp
->lock
, flags
);
299 list_add_tail_rcu(&np
->node
, &ndp
->packages
);
301 spin_unlock_irqrestore(&ndp
->lock
, flags
);
306 void ncsi_remove_package(struct ncsi_package
*np
)
308 struct ncsi_dev_priv
*ndp
= np
->ndp
;
309 struct ncsi_channel
*nc
, *tmp
;
312 /* Release all child channels */
313 list_for_each_entry_safe(nc
, tmp
, &np
->channels
, node
)
314 ncsi_remove_channel(nc
);
316 /* Remove and free package */
317 spin_lock_irqsave(&ndp
->lock
, flags
);
318 list_del_rcu(&np
->node
);
320 spin_unlock_irqrestore(&ndp
->lock
, flags
);
325 void ncsi_find_package_and_channel(struct ncsi_dev_priv
*ndp
,
327 struct ncsi_package
**np
,
328 struct ncsi_channel
**nc
)
330 struct ncsi_package
*p
;
331 struct ncsi_channel
*c
;
333 p
= ncsi_find_package(ndp
, NCSI_PACKAGE_INDEX(id
));
334 c
= p
? ncsi_find_channel(p
, NCSI_CHANNEL_INDEX(id
)) : NULL
;
342 /* For two consecutive NCSI commands, the packet IDs shouldn't
343 * be same. Otherwise, the bogus response might be replied. So
344 * the available IDs are allocated in round-robin fashion.
346 struct ncsi_request
*ncsi_alloc_request(struct ncsi_dev_priv
*ndp
,
347 unsigned int req_flags
)
349 struct ncsi_request
*nr
= NULL
;
350 int i
, limit
= ARRAY_SIZE(ndp
->requests
);
353 /* Check if there is one available request until the ceiling */
354 spin_lock_irqsave(&ndp
->lock
, flags
);
355 for (i
= ndp
->request_id
; i
< limit
; i
++) {
356 if (ndp
->requests
[i
].used
)
359 nr
= &ndp
->requests
[i
];
361 nr
->flags
= req_flags
;
362 ndp
->request_id
= i
+ 1;
366 /* Fail back to check from the starting cursor */
367 for (i
= NCSI_REQ_START_IDX
; i
< ndp
->request_id
; i
++) {
368 if (ndp
->requests
[i
].used
)
371 nr
= &ndp
->requests
[i
];
373 nr
->flags
= req_flags
;
374 ndp
->request_id
= i
+ 1;
379 spin_unlock_irqrestore(&ndp
->lock
, flags
);
383 void ncsi_free_request(struct ncsi_request
*nr
)
385 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
386 struct sk_buff
*cmd
, *rsp
;
392 del_timer_sync(&nr
->timer
);
395 spin_lock_irqsave(&ndp
->lock
, flags
);
401 driven
= !!(nr
->flags
& NCSI_REQ_FLAG_EVENT_DRIVEN
);
402 spin_unlock_irqrestore(&ndp
->lock
, flags
);
404 if (driven
&& cmd
&& --ndp
->pending_req_num
== 0)
405 schedule_work(&ndp
->work
);
407 /* Release command and response */
412 struct ncsi_dev
*ncsi_find_dev(struct net_device
*dev
)
414 struct ncsi_dev_priv
*ndp
;
416 NCSI_FOR_EACH_DEV(ndp
) {
417 if (ndp
->ndev
.dev
== dev
)
424 static void ncsi_request_timeout(struct timer_list
*t
)
426 struct ncsi_request
*nr
= from_timer(nr
, t
, timer
);
427 struct ncsi_dev_priv
*ndp
= nr
->ndp
;
428 struct ncsi_cmd_pkt
*cmd
;
429 struct ncsi_package
*np
;
430 struct ncsi_channel
*nc
;
433 /* If the request already had associated response,
434 * let the response handler to release it.
436 spin_lock_irqsave(&ndp
->lock
, flags
);
438 if (nr
->rsp
|| !nr
->cmd
) {
439 spin_unlock_irqrestore(&ndp
->lock
, flags
);
442 spin_unlock_irqrestore(&ndp
->lock
, flags
);
444 if (nr
->flags
== NCSI_REQ_FLAG_NETLINK_DRIVEN
) {
446 /* Find the package */
447 cmd
= (struct ncsi_cmd_pkt
*)
448 skb_network_header(nr
->cmd
);
449 ncsi_find_package_and_channel(ndp
,
450 cmd
->cmd
.common
.channel
,
452 ncsi_send_netlink_timeout(nr
, np
, nc
);
456 /* Release the request */
457 ncsi_free_request(nr
);
460 static void ncsi_suspend_channel(struct ncsi_dev_priv
*ndp
)
462 struct ncsi_dev
*nd
= &ndp
->ndev
;
463 struct ncsi_package
*np
;
464 struct ncsi_channel
*nc
, *tmp
;
465 struct ncsi_cmd_arg nca
;
469 np
= ndp
->active_package
;
470 nc
= ndp
->active_channel
;
472 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
474 case ncsi_dev_state_suspend
:
475 nd
->state
= ncsi_dev_state_suspend_select
;
477 case ncsi_dev_state_suspend_select
:
478 ndp
->pending_req_num
= 1;
480 nca
.type
= NCSI_PKT_CMD_SP
;
481 nca
.package
= np
->id
;
482 nca
.channel
= NCSI_RESERVED_CHANNEL
;
483 if (ndp
->flags
& NCSI_DEV_HWA
)
488 /* To retrieve the last link states of channels in current
489 * package when current active channel needs fail over to
490 * another one. It means we will possibly select another
491 * channel as next active one. The link states of channels
492 * are most important factor of the selection. So we need
493 * accurate link states. Unfortunately, the link states on
494 * inactive channels can't be updated with LSC AEN in time.
496 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
)
497 nd
->state
= ncsi_dev_state_suspend_gls
;
499 nd
->state
= ncsi_dev_state_suspend_dcnt
;
500 ret
= ncsi_xmit_cmd(&nca
);
505 case ncsi_dev_state_suspend_gls
:
506 ndp
->pending_req_num
= np
->channel_num
;
508 nca
.type
= NCSI_PKT_CMD_GLS
;
509 nca
.package
= np
->id
;
511 nd
->state
= ncsi_dev_state_suspend_dcnt
;
512 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
513 nca
.channel
= nc
->id
;
514 ret
= ncsi_xmit_cmd(&nca
);
520 case ncsi_dev_state_suspend_dcnt
:
521 ndp
->pending_req_num
= 1;
523 nca
.type
= NCSI_PKT_CMD_DCNT
;
524 nca
.package
= np
->id
;
525 nca
.channel
= nc
->id
;
527 nd
->state
= ncsi_dev_state_suspend_dc
;
528 ret
= ncsi_xmit_cmd(&nca
);
533 case ncsi_dev_state_suspend_dc
:
534 ndp
->pending_req_num
= 1;
536 nca
.type
= NCSI_PKT_CMD_DC
;
537 nca
.package
= np
->id
;
538 nca
.channel
= nc
->id
;
541 nd
->state
= ncsi_dev_state_suspend_deselect
;
542 ret
= ncsi_xmit_cmd(&nca
);
546 NCSI_FOR_EACH_CHANNEL(np
, tmp
) {
547 /* If there is another channel active on this package
548 * do not deselect the package.
550 if (tmp
!= nc
&& tmp
->state
== NCSI_CHANNEL_ACTIVE
) {
551 nd
->state
= ncsi_dev_state_suspend_done
;
556 case ncsi_dev_state_suspend_deselect
:
557 ndp
->pending_req_num
= 1;
559 nca
.type
= NCSI_PKT_CMD_DP
;
560 nca
.package
= np
->id
;
561 nca
.channel
= NCSI_RESERVED_CHANNEL
;
563 nd
->state
= ncsi_dev_state_suspend_done
;
564 ret
= ncsi_xmit_cmd(&nca
);
569 case ncsi_dev_state_suspend_done
:
570 spin_lock_irqsave(&nc
->lock
, flags
);
571 nc
->state
= NCSI_CHANNEL_INACTIVE
;
572 spin_unlock_irqrestore(&nc
->lock
, flags
);
573 if (ndp
->flags
& NCSI_DEV_RESET
)
576 ncsi_process_next_channel(ndp
);
579 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in suspend\n",
585 nd
->state
= ncsi_dev_state_functional
;
588 /* Check the VLAN filter bitmap for a set filter, and construct a
589 * "Set VLAN Filter - Disable" packet if found.
591 static int clear_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
592 struct ncsi_cmd_arg
*nca
)
594 struct ncsi_channel_vlan_filter
*ncf
;
600 ncf
= &nc
->vlan_filter
;
601 bitmap
= &ncf
->bitmap
;
603 spin_lock_irqsave(&nc
->lock
, flags
);
604 index
= find_next_bit(bitmap
, ncf
->n_vids
, 0);
605 if (index
>= ncf
->n_vids
) {
606 spin_unlock_irqrestore(&nc
->lock
, flags
);
609 vid
= ncf
->vids
[index
];
611 clear_bit(index
, bitmap
);
612 ncf
->vids
[index
] = 0;
613 spin_unlock_irqrestore(&nc
->lock
, flags
);
615 nca
->type
= NCSI_PKT_CMD_SVF
;
617 /* HW filter index starts at 1 */
618 nca
->bytes
[6] = index
+ 1;
619 nca
->bytes
[7] = 0x00;
623 /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
626 static int set_one_vid(struct ncsi_dev_priv
*ndp
, struct ncsi_channel
*nc
,
627 struct ncsi_cmd_arg
*nca
)
629 struct ncsi_channel_vlan_filter
*ncf
;
630 struct vlan_vid
*vlan
= NULL
;
636 if (list_empty(&ndp
->vlan_vids
))
639 ncf
= &nc
->vlan_filter
;
640 bitmap
= &ncf
->bitmap
;
642 spin_lock_irqsave(&nc
->lock
, flags
);
645 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
647 for (i
= 0; i
< ncf
->n_vids
; i
++)
648 if (ncf
->vids
[i
] == vid
) {
658 /* No VLAN ID is not set */
659 spin_unlock_irqrestore(&nc
->lock
, flags
);
663 index
= find_next_zero_bit(bitmap
, ncf
->n_vids
, 0);
664 if (index
< 0 || index
>= ncf
->n_vids
) {
665 netdev_err(ndp
->ndev
.dev
,
666 "Channel %u already has all VLAN filters set\n",
668 spin_unlock_irqrestore(&nc
->lock
, flags
);
672 ncf
->vids
[index
] = vid
;
673 set_bit(index
, bitmap
);
674 spin_unlock_irqrestore(&nc
->lock
, flags
);
676 nca
->type
= NCSI_PKT_CMD_SVF
;
678 /* HW filter index starts at 1 */
679 nca
->bytes
[6] = index
+ 1;
680 nca
->bytes
[7] = 0x01;
685 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
687 /* NCSI OEM Command APIs */
688 static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg
*nca
)
690 unsigned char data
[NCSI_OEM_BCM_CMD_GMA_LEN
];
693 nca
->payload
= NCSI_OEM_BCM_CMD_GMA_LEN
;
695 memset(data
, 0, NCSI_OEM_BCM_CMD_GMA_LEN
);
696 *(unsigned int *)data
= ntohl(NCSI_OEM_MFR_BCM_ID
);
697 data
[5] = NCSI_OEM_BCM_CMD_GMA
;
701 ret
= ncsi_xmit_cmd(nca
);
703 netdev_err(nca
->ndp
->ndev
.dev
,
704 "NCSI: Failed to transmit cmd 0x%x during configure\n",
709 static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg
*nca
)
712 u8 data_u8
[NCSI_OEM_MLX_CMD_GMA_LEN
];
713 u32 data_u32
[NCSI_OEM_MLX_CMD_GMA_LEN
/ sizeof(u32
)];
717 nca
->payload
= NCSI_OEM_MLX_CMD_GMA_LEN
;
719 memset(&u
, 0, sizeof(u
));
720 u
.data_u32
[0] = ntohl(NCSI_OEM_MFR_MLX_ID
);
721 u
.data_u8
[5] = NCSI_OEM_MLX_CMD_GMA
;
722 u
.data_u8
[6] = NCSI_OEM_MLX_CMD_GMA_PARAM
;
724 nca
->data
= u
.data_u8
;
726 ret
= ncsi_xmit_cmd(nca
);
728 netdev_err(nca
->ndp
->ndev
.dev
,
729 "NCSI: Failed to transmit cmd 0x%x during configure\n",
734 /* OEM Command handlers initialization */
735 static struct ncsi_oem_gma_handler
{
737 int (*handler
)(struct ncsi_cmd_arg
*nca
);
738 } ncsi_oem_gma_handlers
[] = {
739 { NCSI_OEM_MFR_BCM_ID
, ncsi_oem_gma_handler_bcm
},
740 { NCSI_OEM_MFR_MLX_ID
, ncsi_oem_gma_handler_mlx
}
743 static int ncsi_gma_handler(struct ncsi_cmd_arg
*nca
, unsigned int mf_id
)
745 struct ncsi_oem_gma_handler
*nch
= NULL
;
748 /* This function should only be called once, return if flag set */
749 if (nca
->ndp
->gma_flag
== 1)
752 /* Find gma handler for given manufacturer id */
753 for (i
= 0; i
< ARRAY_SIZE(ncsi_oem_gma_handlers
); i
++) {
754 if (ncsi_oem_gma_handlers
[i
].mfr_id
== mf_id
) {
755 if (ncsi_oem_gma_handlers
[i
].handler
)
756 nch
= &ncsi_oem_gma_handlers
[i
];
762 netdev_err(nca
->ndp
->ndev
.dev
,
763 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
768 /* Set the flag for GMA command which should only be called once */
769 nca
->ndp
->gma_flag
= 1;
771 /* Get Mac address from NCSI device */
772 return nch
->handler(nca
);
775 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
777 /* Determine if a given channel from the channel_queue should be used for Tx */
778 static bool ncsi_channel_is_tx(struct ncsi_dev_priv
*ndp
,
779 struct ncsi_channel
*nc
)
781 struct ncsi_channel_mode
*ncm
;
782 struct ncsi_channel
*channel
;
783 struct ncsi_package
*np
;
785 /* Check if any other channel has Tx enabled; a channel may have already
786 * been configured and removed from the channel queue.
788 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
789 if (!ndp
->multi_package
&& np
!= nc
->package
)
791 NCSI_FOR_EACH_CHANNEL(np
, channel
) {
792 ncm
= &channel
->modes
[NCSI_MODE_TX_ENABLE
];
798 /* This channel is the preferred channel and has link */
799 list_for_each_entry_rcu(channel
, &ndp
->channel_queue
, link
) {
800 np
= channel
->package
;
801 if (np
->preferred_channel
&&
802 ncsi_channel_has_link(np
->preferred_channel
)) {
803 return np
->preferred_channel
== nc
;
807 /* This channel has link */
808 if (ncsi_channel_has_link(nc
))
811 list_for_each_entry_rcu(channel
, &ndp
->channel_queue
, link
)
812 if (ncsi_channel_has_link(channel
))
815 /* No other channel has link; default to this one */
819 /* Change the active Tx channel in a multi-channel setup */
820 int ncsi_update_tx_channel(struct ncsi_dev_priv
*ndp
,
821 struct ncsi_package
*package
,
822 struct ncsi_channel
*disable
,
823 struct ncsi_channel
*enable
)
825 struct ncsi_cmd_arg nca
;
826 struct ncsi_channel
*nc
;
827 struct ncsi_package
*np
;
830 if (!package
->multi_channel
&& !ndp
->multi_package
)
831 netdev_warn(ndp
->ndev
.dev
,
832 "NCSI: Trying to update Tx channel in single-channel mode\n");
836 /* Find current channel with Tx enabled */
837 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
840 if (!ndp
->multi_package
&& np
!= package
)
843 NCSI_FOR_EACH_CHANNEL(np
, nc
)
844 if (nc
->modes
[NCSI_MODE_TX_ENABLE
].enable
) {
850 /* Find a suitable channel for Tx */
851 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
854 if (!ndp
->multi_package
&& np
!= package
)
856 if (!(ndp
->package_whitelist
& (0x1 << np
->id
)))
859 if (np
->preferred_channel
&&
860 ncsi_channel_has_link(np
->preferred_channel
)) {
861 enable
= np
->preferred_channel
;
865 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
866 if (!(np
->channel_whitelist
& 0x1 << nc
->id
))
868 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
)
870 if (ncsi_channel_has_link(nc
)) {
877 if (disable
== enable
)
884 nca
.channel
= disable
->id
;
885 nca
.package
= disable
->package
->id
;
886 nca
.type
= NCSI_PKT_CMD_DCNT
;
887 ret
= ncsi_xmit_cmd(&nca
);
889 netdev_err(ndp
->ndev
.dev
,
890 "Error %d sending DCNT\n",
894 netdev_info(ndp
->ndev
.dev
, "NCSI: channel %u enables Tx\n", enable
->id
);
896 nca
.channel
= enable
->id
;
897 nca
.package
= enable
->package
->id
;
898 nca
.type
= NCSI_PKT_CMD_ECNT
;
899 ret
= ncsi_xmit_cmd(&nca
);
901 netdev_err(ndp
->ndev
.dev
,
902 "Error %d sending ECNT\n",
908 static void ncsi_configure_channel(struct ncsi_dev_priv
*ndp
)
910 struct ncsi_package
*np
= ndp
->active_package
;
911 struct ncsi_channel
*nc
= ndp
->active_channel
;
912 struct ncsi_channel
*hot_nc
= NULL
;
913 struct ncsi_dev
*nd
= &ndp
->ndev
;
914 struct net_device
*dev
= nd
->dev
;
915 struct ncsi_cmd_arg nca
;
921 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
923 case ncsi_dev_state_config
:
924 case ncsi_dev_state_config_sp
:
925 ndp
->pending_req_num
= 1;
927 /* Select the specific package */
928 nca
.type
= NCSI_PKT_CMD_SP
;
929 if (ndp
->flags
& NCSI_DEV_HWA
)
933 nca
.package
= np
->id
;
934 nca
.channel
= NCSI_RESERVED_CHANNEL
;
935 ret
= ncsi_xmit_cmd(&nca
);
937 netdev_err(ndp
->ndev
.dev
,
938 "NCSI: Failed to transmit CMD_SP\n");
942 nd
->state
= ncsi_dev_state_config_cis
;
944 case ncsi_dev_state_config_cis
:
945 ndp
->pending_req_num
= 1;
947 /* Clear initial state */
948 nca
.type
= NCSI_PKT_CMD_CIS
;
949 nca
.package
= np
->id
;
950 nca
.channel
= nc
->id
;
951 ret
= ncsi_xmit_cmd(&nca
);
953 netdev_err(ndp
->ndev
.dev
,
954 "NCSI: Failed to transmit CMD_CIS\n");
958 nd
->state
= ncsi_dev_state_config_oem_gma
;
960 case ncsi_dev_state_config_oem_gma
:
961 nd
->state
= ncsi_dev_state_config_clear_vids
;
964 #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
965 nca
.type
= NCSI_PKT_CMD_OEM
;
966 nca
.package
= np
->id
;
967 nca
.channel
= nc
->id
;
968 ndp
->pending_req_num
= 1;
969 ret
= ncsi_gma_handler(&nca
, nc
->version
.mf_id
);
970 #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
973 schedule_work(&ndp
->work
);
976 case ncsi_dev_state_config_clear_vids
:
977 case ncsi_dev_state_config_svf
:
978 case ncsi_dev_state_config_ev
:
979 case ncsi_dev_state_config_sma
:
980 case ncsi_dev_state_config_ebf
:
981 #if IS_ENABLED(CONFIG_IPV6)
982 case ncsi_dev_state_config_egmf
:
984 case ncsi_dev_state_config_ecnt
:
985 case ncsi_dev_state_config_ec
:
986 case ncsi_dev_state_config_ae
:
987 case ncsi_dev_state_config_gls
:
988 ndp
->pending_req_num
= 1;
990 nca
.package
= np
->id
;
991 nca
.channel
= nc
->id
;
993 /* Clear any active filters on the channel before setting */
994 if (nd
->state
== ncsi_dev_state_config_clear_vids
) {
995 ret
= clear_one_vid(ndp
, nc
, &nca
);
997 nd
->state
= ncsi_dev_state_config_svf
;
998 schedule_work(&ndp
->work
);
1002 nd
->state
= ncsi_dev_state_config_clear_vids
;
1003 /* Add known VLAN tags to the filter */
1004 } else if (nd
->state
== ncsi_dev_state_config_svf
) {
1005 ret
= set_one_vid(ndp
, nc
, &nca
);
1007 nd
->state
= ncsi_dev_state_config_ev
;
1008 schedule_work(&ndp
->work
);
1012 nd
->state
= ncsi_dev_state_config_svf
;
1013 /* Enable/Disable the VLAN filter */
1014 } else if (nd
->state
== ncsi_dev_state_config_ev
) {
1015 if (list_empty(&ndp
->vlan_vids
)) {
1016 nca
.type
= NCSI_PKT_CMD_DV
;
1018 nca
.type
= NCSI_PKT_CMD_EV
;
1019 nca
.bytes
[3] = NCSI_CAP_VLAN_NO
;
1021 nd
->state
= ncsi_dev_state_config_sma
;
1022 } else if (nd
->state
== ncsi_dev_state_config_sma
) {
1023 /* Use first entry in unicast filter table. Note that
1024 * the MAC filter table starts from entry 1 instead of
1027 nca
.type
= NCSI_PKT_CMD_SMA
;
1028 for (index
= 0; index
< 6; index
++)
1029 nca
.bytes
[index
] = dev
->dev_addr
[index
];
1032 nd
->state
= ncsi_dev_state_config_ebf
;
1033 } else if (nd
->state
== ncsi_dev_state_config_ebf
) {
1034 nca
.type
= NCSI_PKT_CMD_EBF
;
1035 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_BC
].cap
;
1036 if (ncsi_channel_is_tx(ndp
, nc
))
1037 nd
->state
= ncsi_dev_state_config_ecnt
;
1039 nd
->state
= ncsi_dev_state_config_ec
;
1040 #if IS_ENABLED(CONFIG_IPV6)
1041 if (ndp
->inet6_addr_num
> 0 &&
1042 (nc
->caps
[NCSI_CAP_GENERIC
].cap
&
1043 NCSI_CAP_GENERIC_MC
))
1044 nd
->state
= ncsi_dev_state_config_egmf
;
1045 } else if (nd
->state
== ncsi_dev_state_config_egmf
) {
1046 nca
.type
= NCSI_PKT_CMD_EGMF
;
1047 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1048 if (ncsi_channel_is_tx(ndp
, nc
))
1049 nd
->state
= ncsi_dev_state_config_ecnt
;
1051 nd
->state
= ncsi_dev_state_config_ec
;
1052 #endif /* CONFIG_IPV6 */
1053 } else if (nd
->state
== ncsi_dev_state_config_ecnt
) {
1054 if (np
->preferred_channel
&&
1055 nc
!= np
->preferred_channel
)
1056 netdev_info(ndp
->ndev
.dev
,
1057 "NCSI: Tx failed over to channel %u\n",
1059 nca
.type
= NCSI_PKT_CMD_ECNT
;
1060 nd
->state
= ncsi_dev_state_config_ec
;
1061 } else if (nd
->state
== ncsi_dev_state_config_ec
) {
1062 /* Enable AEN if it's supported */
1063 nca
.type
= NCSI_PKT_CMD_EC
;
1064 nd
->state
= ncsi_dev_state_config_ae
;
1065 if (!(nc
->caps
[NCSI_CAP_AEN
].cap
& NCSI_CAP_AEN_MASK
))
1066 nd
->state
= ncsi_dev_state_config_gls
;
1067 } else if (nd
->state
== ncsi_dev_state_config_ae
) {
1068 nca
.type
= NCSI_PKT_CMD_AE
;
1070 nca
.dwords
[1] = nc
->caps
[NCSI_CAP_AEN
].cap
;
1071 nd
->state
= ncsi_dev_state_config_gls
;
1072 } else if (nd
->state
== ncsi_dev_state_config_gls
) {
1073 nca
.type
= NCSI_PKT_CMD_GLS
;
1074 nd
->state
= ncsi_dev_state_config_done
;
1077 ret
= ncsi_xmit_cmd(&nca
);
1079 netdev_err(ndp
->ndev
.dev
,
1080 "NCSI: Failed to transmit CMD %x\n",
1085 case ncsi_dev_state_config_done
:
1086 netdev_dbg(ndp
->ndev
.dev
, "NCSI: channel %u config done\n",
1088 spin_lock_irqsave(&nc
->lock
, flags
);
1089 nc
->state
= NCSI_CHANNEL_ACTIVE
;
1091 if (ndp
->flags
& NCSI_DEV_RESET
) {
1092 /* A reset event happened during config, start it now */
1093 nc
->reconfigure_needed
= false;
1094 spin_unlock_irqrestore(&nc
->lock
, flags
);
1099 if (nc
->reconfigure_needed
) {
1100 /* This channel's configuration has been updated
1101 * part-way during the config state - start the
1102 * channel configuration over
1104 nc
->reconfigure_needed
= false;
1105 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1106 spin_unlock_irqrestore(&nc
->lock
, flags
);
1108 spin_lock_irqsave(&ndp
->lock
, flags
);
1109 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1110 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1112 netdev_dbg(dev
, "Dirty NCSI channel state reset\n");
1113 ncsi_process_next_channel(ndp
);
1117 if (nc
->modes
[NCSI_MODE_LINK
].data
[2] & 0x1) {
1121 netdev_dbg(ndp
->ndev
.dev
,
1122 "NCSI: channel %u link down after config\n",
1125 spin_unlock_irqrestore(&nc
->lock
, flags
);
1127 /* Update the hot channel */
1128 spin_lock_irqsave(&ndp
->lock
, flags
);
1129 ndp
->hot_channel
= hot_nc
;
1130 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1132 ncsi_start_channel_monitor(nc
);
1133 ncsi_process_next_channel(ndp
);
1136 netdev_alert(dev
, "Wrong NCSI state 0x%x in config\n",
1143 ncsi_report_link(ndp
, true);
1146 static int ncsi_choose_active_channel(struct ncsi_dev_priv
*ndp
)
1148 struct ncsi_channel
*nc
, *found
, *hot_nc
;
1149 struct ncsi_channel_mode
*ncm
;
1150 unsigned long flags
, cflags
;
1151 struct ncsi_package
*np
;
1154 spin_lock_irqsave(&ndp
->lock
, flags
);
1155 hot_nc
= ndp
->hot_channel
;
1156 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1158 /* By default the search is done once an inactive channel with up
1159 * link is found, unless a preferred channel is set.
1160 * If multi_package or multi_channel are configured all channels in the
1161 * whitelist are added to the channel queue.
1165 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1166 if (!(ndp
->package_whitelist
& (0x1 << np
->id
)))
1168 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1169 if (!(np
->channel_whitelist
& (0x1 << nc
->id
)))
1172 spin_lock_irqsave(&nc
->lock
, cflags
);
1174 if (!list_empty(&nc
->link
) ||
1175 nc
->state
!= NCSI_CHANNEL_INACTIVE
) {
1176 spin_unlock_irqrestore(&nc
->lock
, cflags
);
1186 ncm
= &nc
->modes
[NCSI_MODE_LINK
];
1187 if (ncm
->data
[2] & 0x1) {
1192 /* If multi_channel is enabled configure all valid
1193 * channels whether or not they currently have link
1194 * so they will have AENs enabled.
1196 if (with_link
|| np
->multi_channel
) {
1197 spin_lock_irqsave(&ndp
->lock
, flags
);
1198 list_add_tail_rcu(&nc
->link
,
1199 &ndp
->channel_queue
);
1200 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1202 netdev_dbg(ndp
->ndev
.dev
,
1203 "NCSI: Channel %u added to queue (link %s)\n",
1205 ncm
->data
[2] & 0x1 ? "up" : "down");
1208 spin_unlock_irqrestore(&nc
->lock
, cflags
);
1210 if (with_link
&& !np
->multi_channel
)
1213 if (with_link
&& !ndp
->multi_package
)
1217 if (list_empty(&ndp
->channel_queue
) && found
) {
1218 netdev_info(ndp
->ndev
.dev
,
1219 "NCSI: No channel with link found, configuring channel %u\n",
1221 spin_lock_irqsave(&ndp
->lock
, flags
);
1222 list_add_tail_rcu(&found
->link
, &ndp
->channel_queue
);
1223 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1224 } else if (!found
) {
1225 netdev_warn(ndp
->ndev
.dev
,
1226 "NCSI: No channel found to configure!\n");
1227 ncsi_report_link(ndp
, true);
1231 return ncsi_process_next_channel(ndp
);
1234 static bool ncsi_check_hwa(struct ncsi_dev_priv
*ndp
)
1236 struct ncsi_package
*np
;
1237 struct ncsi_channel
*nc
;
1239 bool has_channel
= false;
1241 /* The hardware arbitration is disabled if any one channel
1242 * doesn't support explicitly.
1244 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1245 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1248 cap
= nc
->caps
[NCSI_CAP_GENERIC
].cap
;
1249 if (!(cap
& NCSI_CAP_GENERIC_HWA
) ||
1250 (cap
& NCSI_CAP_GENERIC_HWA_MASK
) !=
1251 NCSI_CAP_GENERIC_HWA_SUPPORT
) {
1252 ndp
->flags
&= ~NCSI_DEV_HWA
;
1259 ndp
->flags
|= NCSI_DEV_HWA
;
1263 ndp
->flags
&= ~NCSI_DEV_HWA
;
1267 static void ncsi_probe_channel(struct ncsi_dev_priv
*ndp
)
1269 struct ncsi_dev
*nd
= &ndp
->ndev
;
1270 struct ncsi_package
*np
;
1271 struct ncsi_channel
*nc
;
1272 struct ncsi_cmd_arg nca
;
1273 unsigned char index
;
1277 nca
.req_flags
= NCSI_REQ_FLAG_EVENT_DRIVEN
;
1278 switch (nd
->state
) {
1279 case ncsi_dev_state_probe
:
1280 nd
->state
= ncsi_dev_state_probe_deselect
;
1282 case ncsi_dev_state_probe_deselect
:
1283 ndp
->pending_req_num
= 8;
1285 /* Deselect all possible packages */
1286 nca
.type
= NCSI_PKT_CMD_DP
;
1287 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1288 for (index
= 0; index
< 8; index
++) {
1289 nca
.package
= index
;
1290 ret
= ncsi_xmit_cmd(&nca
);
1295 nd
->state
= ncsi_dev_state_probe_package
;
1297 case ncsi_dev_state_probe_package
:
1298 ndp
->pending_req_num
= 1;
1300 nca
.type
= NCSI_PKT_CMD_SP
;
1302 nca
.package
= ndp
->package_probe_id
;
1303 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1304 ret
= ncsi_xmit_cmd(&nca
);
1307 nd
->state
= ncsi_dev_state_probe_channel
;
1309 case ncsi_dev_state_probe_channel
:
1310 ndp
->active_package
= ncsi_find_package(ndp
,
1311 ndp
->package_probe_id
);
1312 if (!ndp
->active_package
) {
1314 nd
->state
= ncsi_dev_state_probe_dp
;
1315 schedule_work(&ndp
->work
);
1318 nd
->state
= ncsi_dev_state_probe_cis
;
1319 schedule_work(&ndp
->work
);
1321 case ncsi_dev_state_probe_cis
:
1322 ndp
->pending_req_num
= NCSI_RESERVED_CHANNEL
;
1324 /* Clear initial state */
1325 nca
.type
= NCSI_PKT_CMD_CIS
;
1326 nca
.package
= ndp
->active_package
->id
;
1327 for (index
= 0; index
< NCSI_RESERVED_CHANNEL
; index
++) {
1328 nca
.channel
= index
;
1329 ret
= ncsi_xmit_cmd(&nca
);
1334 nd
->state
= ncsi_dev_state_probe_gvi
;
1336 case ncsi_dev_state_probe_gvi
:
1337 case ncsi_dev_state_probe_gc
:
1338 case ncsi_dev_state_probe_gls
:
1339 np
= ndp
->active_package
;
1340 ndp
->pending_req_num
= np
->channel_num
;
1342 /* Retrieve version, capability or link status */
1343 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1344 nca
.type
= NCSI_PKT_CMD_GVI
;
1345 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1346 nca
.type
= NCSI_PKT_CMD_GC
;
1348 nca
.type
= NCSI_PKT_CMD_GLS
;
1350 nca
.package
= np
->id
;
1351 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1352 nca
.channel
= nc
->id
;
1353 ret
= ncsi_xmit_cmd(&nca
);
1358 if (nd
->state
== ncsi_dev_state_probe_gvi
)
1359 nd
->state
= ncsi_dev_state_probe_gc
;
1360 else if (nd
->state
== ncsi_dev_state_probe_gc
)
1361 nd
->state
= ncsi_dev_state_probe_gls
;
1363 nd
->state
= ncsi_dev_state_probe_dp
;
1365 case ncsi_dev_state_probe_dp
:
1366 ndp
->pending_req_num
= 1;
1368 /* Deselect the current package */
1369 nca
.type
= NCSI_PKT_CMD_DP
;
1370 nca
.package
= ndp
->package_probe_id
;
1371 nca
.channel
= NCSI_RESERVED_CHANNEL
;
1372 ret
= ncsi_xmit_cmd(&nca
);
1376 /* Probe next package */
1377 ndp
->package_probe_id
++;
1378 if (ndp
->package_probe_id
>= 8) {
1379 /* Probe finished */
1380 ndp
->flags
|= NCSI_DEV_PROBED
;
1383 nd
->state
= ncsi_dev_state_probe_package
;
1384 ndp
->active_package
= NULL
;
1387 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%0x in enumeration\n",
1391 if (ndp
->flags
& NCSI_DEV_PROBED
) {
1392 /* Check if all packages have HWA support */
1393 ncsi_check_hwa(ndp
);
1394 ncsi_choose_active_channel(ndp
);
1399 netdev_err(ndp
->ndev
.dev
,
1400 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1402 ncsi_report_link(ndp
, true);
1405 static void ncsi_dev_work(struct work_struct
*work
)
1407 struct ncsi_dev_priv
*ndp
= container_of(work
,
1408 struct ncsi_dev_priv
, work
);
1409 struct ncsi_dev
*nd
= &ndp
->ndev
;
1411 switch (nd
->state
& ncsi_dev_state_major
) {
1412 case ncsi_dev_state_probe
:
1413 ncsi_probe_channel(ndp
);
1415 case ncsi_dev_state_suspend
:
1416 ncsi_suspend_channel(ndp
);
1418 case ncsi_dev_state_config
:
1419 ncsi_configure_channel(ndp
);
1422 netdev_warn(nd
->dev
, "Wrong NCSI state 0x%x in workqueue\n",
1427 int ncsi_process_next_channel(struct ncsi_dev_priv
*ndp
)
1429 struct ncsi_channel
*nc
;
1431 unsigned long flags
;
1433 spin_lock_irqsave(&ndp
->lock
, flags
);
1434 nc
= list_first_or_null_rcu(&ndp
->channel_queue
,
1435 struct ncsi_channel
, link
);
1437 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1441 list_del_init(&nc
->link
);
1442 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1444 spin_lock_irqsave(&nc
->lock
, flags
);
1445 old_state
= nc
->state
;
1446 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1447 spin_unlock_irqrestore(&nc
->lock
, flags
);
1449 ndp
->active_channel
= nc
;
1450 ndp
->active_package
= nc
->package
;
1452 switch (old_state
) {
1453 case NCSI_CHANNEL_INACTIVE
:
1454 ndp
->ndev
.state
= ncsi_dev_state_config
;
1455 netdev_dbg(ndp
->ndev
.dev
, "NCSI: configuring channel %u\n",
1457 ncsi_configure_channel(ndp
);
1459 case NCSI_CHANNEL_ACTIVE
:
1460 ndp
->ndev
.state
= ncsi_dev_state_suspend
;
1461 netdev_dbg(ndp
->ndev
.dev
, "NCSI: suspending channel %u\n",
1463 ncsi_suspend_channel(ndp
);
1466 netdev_err(ndp
->ndev
.dev
, "Invalid state 0x%x on %d:%d\n",
1467 old_state
, nc
->package
->id
, nc
->id
);
1468 ncsi_report_link(ndp
, false);
1475 ndp
->active_channel
= NULL
;
1476 ndp
->active_package
= NULL
;
1477 if (ndp
->flags
& NCSI_DEV_RESHUFFLE
) {
1478 ndp
->flags
&= ~NCSI_DEV_RESHUFFLE
;
1479 return ncsi_choose_active_channel(ndp
);
1482 ncsi_report_link(ndp
, false);
1486 #if IS_ENABLED(CONFIG_IPV6)
1487 static int ncsi_inet6addr_event(struct notifier_block
*this,
1488 unsigned long event
, void *data
)
1490 struct inet6_ifaddr
*ifa
= data
;
1491 struct net_device
*dev
= ifa
->idev
->dev
;
1492 struct ncsi_dev
*nd
= ncsi_find_dev(dev
);
1493 struct ncsi_dev_priv
*ndp
= nd
? TO_NCSI_DEV_PRIV(nd
) : NULL
;
1494 struct ncsi_package
*np
;
1495 struct ncsi_channel
*nc
;
1496 struct ncsi_cmd_arg nca
;
1500 if (!ndp
|| (ipv6_addr_type(&ifa
->addr
) &
1501 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
)))
1506 action
= (++ndp
->inet6_addr_num
) == 1;
1507 nca
.type
= NCSI_PKT_CMD_EGMF
;
1510 action
= (--ndp
->inet6_addr_num
== 0);
1511 nca
.type
= NCSI_PKT_CMD_DGMF
;
1517 /* We might not have active channel or packages. The IPv6
1518 * required multicast will be enabled when active channel
1519 * or packages are chosen.
1521 np
= ndp
->active_package
;
1522 nc
= ndp
->active_channel
;
1523 if (!action
|| !np
|| !nc
)
1526 /* We needn't enable or disable it if the function isn't supported */
1527 if (!(nc
->caps
[NCSI_CAP_GENERIC
].cap
& NCSI_CAP_GENERIC_MC
))
1532 nca
.package
= np
->id
;
1533 nca
.channel
= nc
->id
;
1534 nca
.dwords
[0] = nc
->caps
[NCSI_CAP_MC
].cap
;
1535 ret
= ncsi_xmit_cmd(&nca
);
1537 netdev_warn(dev
, "Fail to %s global multicast filter (%d)\n",
1538 (event
== NETDEV_UP
) ? "enable" : "disable", ret
);
1545 static struct notifier_block ncsi_inet6addr_notifier
= {
1546 .notifier_call
= ncsi_inet6addr_event
,
1548 #endif /* CONFIG_IPV6 */
1550 static int ncsi_kick_channels(struct ncsi_dev_priv
*ndp
)
1552 struct ncsi_dev
*nd
= &ndp
->ndev
;
1553 struct ncsi_channel
*nc
;
1554 struct ncsi_package
*np
;
1555 unsigned long flags
;
1558 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1559 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1560 spin_lock_irqsave(&nc
->lock
, flags
);
1562 /* Channels may be busy, mark dirty instead of
1564 * a) not ACTIVE (configured)
1565 * b) in the channel_queue (to be configured)
1566 * c) it's ndev is in the config state
1568 if (nc
->state
!= NCSI_CHANNEL_ACTIVE
) {
1569 if ((ndp
->ndev
.state
& 0xff00) ==
1570 ncsi_dev_state_config
||
1571 !list_empty(&nc
->link
)) {
1573 "NCSI: channel %p marked dirty\n",
1575 nc
->reconfigure_needed
= true;
1577 spin_unlock_irqrestore(&nc
->lock
, flags
);
1581 spin_unlock_irqrestore(&nc
->lock
, flags
);
1583 ncsi_stop_channel_monitor(nc
);
1584 spin_lock_irqsave(&nc
->lock
, flags
);
1585 nc
->state
= NCSI_CHANNEL_INACTIVE
;
1586 spin_unlock_irqrestore(&nc
->lock
, flags
);
1588 spin_lock_irqsave(&ndp
->lock
, flags
);
1589 list_add_tail_rcu(&nc
->link
, &ndp
->channel_queue
);
1590 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1592 netdev_dbg(nd
->dev
, "NCSI: kicked channel %p\n", nc
);
1600 int ncsi_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1602 struct ncsi_dev_priv
*ndp
;
1603 unsigned int n_vids
= 0;
1604 struct vlan_vid
*vlan
;
1605 struct ncsi_dev
*nd
;
1611 nd
= ncsi_find_dev(dev
);
1613 netdev_warn(dev
, "NCSI: No net_device?\n");
1617 ndp
= TO_NCSI_DEV_PRIV(nd
);
1619 /* Add the VLAN id to our internal list */
1620 list_for_each_entry_rcu(vlan
, &ndp
->vlan_vids
, list
) {
1622 if (vlan
->vid
== vid
) {
1623 netdev_dbg(dev
, "NCSI: vid %u already registered\n",
1628 if (n_vids
>= NCSI_MAX_VLAN_VIDS
) {
1630 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1631 vid
, NCSI_MAX_VLAN_VIDS
);
1635 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1639 vlan
->proto
= proto
;
1641 list_add_rcu(&vlan
->list
, &ndp
->vlan_vids
);
1643 netdev_dbg(dev
, "NCSI: Added new vid %u\n", vid
);
1645 found
= ncsi_kick_channels(ndp
) != 0;
1647 return found
? ncsi_process_next_channel(ndp
) : 0;
1649 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid
);
1651 int ncsi_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
, u16 vid
)
1653 struct vlan_vid
*vlan
, *tmp
;
1654 struct ncsi_dev_priv
*ndp
;
1655 struct ncsi_dev
*nd
;
1661 nd
= ncsi_find_dev(dev
);
1663 netdev_warn(dev
, "NCSI: no net_device?\n");
1667 ndp
= TO_NCSI_DEV_PRIV(nd
);
1669 /* Remove the VLAN id from our internal list */
1670 list_for_each_entry_safe(vlan
, tmp
, &ndp
->vlan_vids
, list
)
1671 if (vlan
->vid
== vid
) {
1672 netdev_dbg(dev
, "NCSI: vid %u found, removing\n", vid
);
1673 list_del_rcu(&vlan
->list
);
1679 netdev_err(dev
, "NCSI: vid %u wasn't registered!\n", vid
);
1683 found
= ncsi_kick_channels(ndp
) != 0;
1685 return found
? ncsi_process_next_channel(ndp
) : 0;
1687 EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid
);
1689 struct ncsi_dev
*ncsi_register_dev(struct net_device
*dev
,
1690 void (*handler
)(struct ncsi_dev
*ndev
))
1692 struct ncsi_dev_priv
*ndp
;
1693 struct ncsi_dev
*nd
;
1694 unsigned long flags
;
1697 /* Check if the device has been registered or not */
1698 nd
= ncsi_find_dev(dev
);
1702 /* Create NCSI device */
1703 ndp
= kzalloc(sizeof(*ndp
), GFP_ATOMIC
);
1708 nd
->state
= ncsi_dev_state_registered
;
1710 nd
->handler
= handler
;
1711 ndp
->pending_req_num
= 0;
1712 INIT_LIST_HEAD(&ndp
->channel_queue
);
1713 INIT_LIST_HEAD(&ndp
->vlan_vids
);
1714 INIT_WORK(&ndp
->work
, ncsi_dev_work
);
1715 ndp
->package_whitelist
= UINT_MAX
;
1717 /* Initialize private NCSI device */
1718 spin_lock_init(&ndp
->lock
);
1719 INIT_LIST_HEAD(&ndp
->packages
);
1720 ndp
->request_id
= NCSI_REQ_START_IDX
;
1721 for (i
= 0; i
< ARRAY_SIZE(ndp
->requests
); i
++) {
1722 ndp
->requests
[i
].id
= i
;
1723 ndp
->requests
[i
].ndp
= ndp
;
1724 timer_setup(&ndp
->requests
[i
].timer
, ncsi_request_timeout
, 0);
1727 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1728 #if IS_ENABLED(CONFIG_IPV6)
1729 ndp
->inet6_addr_num
= 0;
1730 if (list_empty(&ncsi_dev_list
))
1731 register_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1733 list_add_tail_rcu(&ndp
->node
, &ncsi_dev_list
);
1734 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1736 /* Register NCSI packet Rx handler */
1737 ndp
->ptype
.type
= cpu_to_be16(ETH_P_NCSI
);
1738 ndp
->ptype
.func
= ncsi_rcv_rsp
;
1739 ndp
->ptype
.dev
= dev
;
1740 dev_add_pack(&ndp
->ptype
);
1742 /* Set up generic netlink interface */
1743 ncsi_init_netlink(dev
);
1747 EXPORT_SYMBOL_GPL(ncsi_register_dev
);
1749 int ncsi_start_dev(struct ncsi_dev
*nd
)
1751 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1753 if (nd
->state
!= ncsi_dev_state_registered
&&
1754 nd
->state
!= ncsi_dev_state_functional
)
1757 if (!(ndp
->flags
& NCSI_DEV_PROBED
)) {
1758 ndp
->package_probe_id
= 0;
1759 nd
->state
= ncsi_dev_state_probe
;
1760 schedule_work(&ndp
->work
);
1764 return ncsi_reset_dev(nd
);
1766 EXPORT_SYMBOL_GPL(ncsi_start_dev
);
1768 void ncsi_stop_dev(struct ncsi_dev
*nd
)
1770 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1771 struct ncsi_package
*np
;
1772 struct ncsi_channel
*nc
;
1775 unsigned long flags
;
1777 /* Stop the channel monitor on any active channels. Don't reset the
1778 * channel state so we know which were active when ncsi_start_dev()
1781 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1782 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1783 ncsi_stop_channel_monitor(nc
);
1785 spin_lock_irqsave(&nc
->lock
, flags
);
1786 chained
= !list_empty(&nc
->link
);
1787 old_state
= nc
->state
;
1788 spin_unlock_irqrestore(&nc
->lock
, flags
);
1790 WARN_ON_ONCE(chained
||
1791 old_state
== NCSI_CHANNEL_INVISIBLE
);
1795 netdev_dbg(ndp
->ndev
.dev
, "NCSI: Stopping device\n");
1796 ncsi_report_link(ndp
, true);
1798 EXPORT_SYMBOL_GPL(ncsi_stop_dev
);
1800 int ncsi_reset_dev(struct ncsi_dev
*nd
)
1802 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1803 struct ncsi_channel
*nc
, *active
, *tmp
;
1804 struct ncsi_package
*np
;
1805 unsigned long flags
;
1807 spin_lock_irqsave(&ndp
->lock
, flags
);
1809 if (!(ndp
->flags
& NCSI_DEV_RESET
)) {
1810 /* Haven't been called yet, check states */
1811 switch (nd
->state
& ncsi_dev_state_major
) {
1812 case ncsi_dev_state_registered
:
1813 case ncsi_dev_state_probe
:
1814 /* Not even probed yet - do nothing */
1815 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1817 case ncsi_dev_state_suspend
:
1818 case ncsi_dev_state_config
:
1819 /* Wait for the channel to finish its suspend/config
1820 * operation; once it finishes it will check for
1821 * NCSI_DEV_RESET and reset the state.
1823 ndp
->flags
|= NCSI_DEV_RESET
;
1824 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1828 switch (nd
->state
) {
1829 case ncsi_dev_state_suspend_done
:
1830 case ncsi_dev_state_config_done
:
1831 case ncsi_dev_state_functional
:
1835 /* Current reset operation happening */
1836 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1841 if (!list_empty(&ndp
->channel_queue
)) {
1842 /* Clear any channel queue we may have interrupted */
1843 list_for_each_entry_safe(nc
, tmp
, &ndp
->channel_queue
, link
)
1844 list_del_init(&nc
->link
);
1846 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1849 NCSI_FOR_EACH_PACKAGE(ndp
, np
) {
1850 NCSI_FOR_EACH_CHANNEL(np
, nc
) {
1851 spin_lock_irqsave(&nc
->lock
, flags
);
1853 if (nc
->state
== NCSI_CHANNEL_ACTIVE
) {
1855 nc
->state
= NCSI_CHANNEL_INVISIBLE
;
1856 spin_unlock_irqrestore(&nc
->lock
, flags
);
1857 ncsi_stop_channel_monitor(nc
);
1861 spin_unlock_irqrestore(&nc
->lock
, flags
);
1869 spin_lock_irqsave(&ndp
->lock
, flags
);
1870 ndp
->flags
&= ~NCSI_DEV_RESET
;
1871 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1872 return ncsi_choose_active_channel(ndp
);
1875 spin_lock_irqsave(&ndp
->lock
, flags
);
1876 ndp
->flags
|= NCSI_DEV_RESET
;
1877 ndp
->active_channel
= active
;
1878 ndp
->active_package
= active
->package
;
1879 spin_unlock_irqrestore(&ndp
->lock
, flags
);
1881 nd
->state
= ncsi_dev_state_suspend
;
1882 schedule_work(&ndp
->work
);
1886 void ncsi_unregister_dev(struct ncsi_dev
*nd
)
1888 struct ncsi_dev_priv
*ndp
= TO_NCSI_DEV_PRIV(nd
);
1889 struct ncsi_package
*np
, *tmp
;
1890 unsigned long flags
;
1892 dev_remove_pack(&ndp
->ptype
);
1894 list_for_each_entry_safe(np
, tmp
, &ndp
->packages
, node
)
1895 ncsi_remove_package(np
);
1897 spin_lock_irqsave(&ncsi_dev_lock
, flags
);
1898 list_del_rcu(&ndp
->node
);
1899 #if IS_ENABLED(CONFIG_IPV6)
1900 if (list_empty(&ncsi_dev_list
))
1901 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier
);
1903 spin_unlock_irqrestore(&ncsi_dev_lock
, flags
);
1905 ncsi_unregister_netlink(nd
->dev
);
1909 EXPORT_SYMBOL_GPL(ncsi_unregister_dev
);