blob: 755aab66dcab75f8a1eada1e29d1545e78fb7199 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Gavin Shan2d283bd2016-07-19 11:54:16 +10002/*
3 * Copyright Gavin Shan, IBM Corporation 2016.
Gavin Shan2d283bd2016-07-19 11:54:16 +10004 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/netdevice.h>
10#include <linux/skbuff.h>
Gavin Shan2d283bd2016-07-19 11:54:16 +100011
12#include <net/ncsi.h>
13#include <net/net_namespace.h>
14#include <net/sock.h>
Gavin Shane6f44ed2016-07-19 11:54:19 +100015#include <net/addrconf.h>
16#include <net/ipv6.h>
17#include <net/if_inet6.h>
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +000018#include <net/genetlink.h>
Gavin Shan2d283bd2016-07-19 11:54:16 +100019
20#include "internal.h"
Gavin Shane6f44ed2016-07-19 11:54:19 +100021#include "ncsi-pkt.h"
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +110022#include "ncsi-netlink.h"
Gavin Shan2d283bd2016-07-19 11:54:16 +100023
24LIST_HEAD(ncsi_dev_list);
25DEFINE_SPINLOCK(ncsi_dev_lock);
26
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +110027bool ncsi_channel_has_link(struct ncsi_channel *channel)
28{
29 return !!(channel->modes[NCSI_MODE_LINK].data[2] & 0x1);
30}
31
32bool ncsi_channel_is_last(struct ncsi_dev_priv *ndp,
33 struct ncsi_channel *channel)
34{
35 struct ncsi_package *np;
36 struct ncsi_channel *nc;
37
38 NCSI_FOR_EACH_PACKAGE(ndp, np)
39 NCSI_FOR_EACH_CHANNEL(np, nc) {
40 if (nc == channel)
41 continue;
42 if (nc->state == NCSI_CHANNEL_ACTIVE &&
43 ncsi_channel_has_link(nc))
44 return false;
45 }
46
47 return true;
48}
49
Gavin Shane6f44ed2016-07-19 11:54:19 +100050static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
51{
52 struct ncsi_dev *nd = &ndp->ndev;
53 struct ncsi_package *np;
54 struct ncsi_channel *nc;
Gavin Shand8cedaa2016-10-04 11:25:47 +110055 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +100056
57 nd->state = ncsi_dev_state_functional;
58 if (force_down) {
59 nd->link_up = 0;
60 goto report;
61 }
62
63 nd->link_up = 0;
64 NCSI_FOR_EACH_PACKAGE(ndp, np) {
65 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +110066 spin_lock_irqsave(&nc->lock, flags);
67
Gavin Shane6f44ed2016-07-19 11:54:19 +100068 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +110069 nc->state != NCSI_CHANNEL_ACTIVE) {
70 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100071 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +110072 }
Gavin Shane6f44ed2016-07-19 11:54:19 +100073
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +110074 if (ncsi_channel_has_link(nc)) {
Gavin Shand8cedaa2016-10-04 11:25:47 +110075 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100076 nd->link_up = 1;
77 goto report;
78 }
Gavin Shand8cedaa2016-10-04 11:25:47 +110079
80 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100081 }
82 }
83
84report:
85 nd->handler(nd);
86}
87
Kees Cook86cb30e2017-10-17 20:21:24 -070088static void ncsi_channel_monitor(struct timer_list *t)
Gavin Shane6f44ed2016-07-19 11:54:19 +100089{
Kees Cook86cb30e2017-10-17 20:21:24 -070090 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +100091 struct ncsi_package *np = nc->package;
92 struct ncsi_dev_priv *ndp = np->ndp;
Gavin Shan52b4c862017-10-19 13:43:08 +110093 struct ncsi_channel_mode *ncm;
Gavin Shane6f44ed2016-07-19 11:54:19 +100094 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +110095 bool enabled, chained;
Gavin Shan83afdc62016-10-04 11:25:52 +110096 unsigned int monitor_state;
Gavin Shane6f44ed2016-07-19 11:54:19 +100097 unsigned long flags;
Gavin Shand8cedaa2016-10-04 11:25:47 +110098 int state, ret;
Gavin Shane6f44ed2016-07-19 11:54:19 +100099
100 spin_lock_irqsave(&nc->lock, flags);
Gavin Shand8cedaa2016-10-04 11:25:47 +1100101 state = nc->state;
102 chained = !list_empty(&nc->link);
Gavin Shan83afdc62016-10-04 11:25:52 +1100103 enabled = nc->monitor.enabled;
104 monitor_state = nc->monitor.state;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000105 spin_unlock_irqrestore(&nc->lock, flags);
106
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100107 if (!enabled || chained) {
108 ncsi_stop_channel_monitor(nc);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000109 return;
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100110 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100111 if (state != NCSI_CHANNEL_INACTIVE &&
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100112 state != NCSI_CHANNEL_ACTIVE) {
113 ncsi_stop_channel_monitor(nc);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000114 return;
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100115 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000116
Gavin Shan83afdc62016-10-04 11:25:52 +1100117 switch (monitor_state) {
118 case NCSI_CHANNEL_MONITOR_START:
119 case NCSI_CHANNEL_MONITOR_RETRY:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000120 nca.ndp = ndp;
121 nca.package = np->id;
122 nca.channel = nc->id;
123 nca.type = NCSI_PKT_CMD_GLS;
Gavin Shana0509cb2016-10-04 11:25:51 +1100124 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000125 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100126 if (ret)
Gavin Shane6f44ed2016-07-19 11:54:19 +1000127 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
128 ret);
Gavin Shan83afdc62016-10-04 11:25:52 +1100129 break;
130 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
131 break;
132 default:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100133 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
134 nc->id);
Samuel Mendoza-Jonas60ab49b2018-11-16 15:51:54 +1100135 ncsi_report_link(ndp, true);
136 ndp->flags |= NCSI_DEV_RESHUFFLE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000137
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100138 ncsi_stop_channel_monitor(nc);
139
Gavin Shan52b4c862017-10-19 13:43:08 +1100140 ncm = &nc->modes[NCSI_MODE_LINK];
Gavin Shand8cedaa2016-10-04 11:25:47 +1100141 spin_lock_irqsave(&nc->lock, flags);
142 nc->state = NCSI_CHANNEL_INVISIBLE;
Gavin Shan52b4c862017-10-19 13:43:08 +1100143 ncm->data[2] &= ~0x1;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100144 spin_unlock_irqrestore(&nc->lock, flags);
145
Gavin Shane6f44ed2016-07-19 11:54:19 +1000146 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shan52b4c862017-10-19 13:43:08 +1100147 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000148 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
149 spin_unlock_irqrestore(&ndp->lock, flags);
150 ncsi_process_next_channel(ndp);
151 return;
152 }
153
154 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100155 nc->monitor.state++;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000156 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100157 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000158}
159
160void ncsi_start_channel_monitor(struct ncsi_channel *nc)
161{
162 unsigned long flags;
163
164 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100165 WARN_ON_ONCE(nc->monitor.enabled);
166 nc->monitor.enabled = true;
167 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000168 spin_unlock_irqrestore(&nc->lock, flags);
169
Gavin Shan83afdc62016-10-04 11:25:52 +1100170 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000171}
172
173void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
174{
175 unsigned long flags;
176
177 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100178 if (!nc->monitor.enabled) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000179 spin_unlock_irqrestore(&nc->lock, flags);
180 return;
181 }
Gavin Shan83afdc62016-10-04 11:25:52 +1100182 nc->monitor.enabled = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000183 spin_unlock_irqrestore(&nc->lock, flags);
184
Gavin Shan83afdc62016-10-04 11:25:52 +1100185 del_timer_sync(&nc->monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000186}
187
Gavin Shan2d283bd2016-07-19 11:54:16 +1000188struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
189 unsigned char id)
190{
191 struct ncsi_channel *nc;
192
193 NCSI_FOR_EACH_CHANNEL(np, nc) {
194 if (nc->id == id)
195 return nc;
196 }
197
198 return NULL;
199}
200
201struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
202{
203 struct ncsi_channel *nc, *tmp;
204 int index;
205 unsigned long flags;
206
207 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
208 if (!nc)
209 return NULL;
210
211 nc->id = id;
212 nc->package = np;
213 nc->state = NCSI_CHANNEL_INACTIVE;
Gavin Shan83afdc62016-10-04 11:25:52 +1100214 nc->monitor.enabled = false;
Kees Cook86cb30e2017-10-17 20:21:24 -0700215 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000216 spin_lock_init(&nc->lock);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000217 INIT_LIST_HEAD(&nc->link);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000218 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;
222
223 spin_lock_irqsave(&np->lock, flags);
224 tmp = ncsi_find_channel(np, id);
225 if (tmp) {
226 spin_unlock_irqrestore(&np->lock, flags);
227 kfree(nc);
228 return tmp;
229 }
230
231 list_add_tail_rcu(&nc->node, &np->channels);
232 np->channel_num++;
233 spin_unlock_irqrestore(&np->lock, flags);
234
235 return nc;
236}
237
238static void ncsi_remove_channel(struct ncsi_channel *nc)
239{
240 struct ncsi_package *np = nc->package;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000241 unsigned long flags;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000242
243 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000244
245 /* Release filters */
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000246 kfree(nc->mac_filter.addrs);
247 kfree(nc->vlan_filter.vids);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000248
249 nc->state = NCSI_CHANNEL_INACTIVE;
250 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000251 ncsi_stop_channel_monitor(nc);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000252
253 /* Remove and free channel */
254 spin_lock_irqsave(&np->lock, flags);
255 list_del_rcu(&nc->node);
256 np->channel_num--;
257 spin_unlock_irqrestore(&np->lock, flags);
258
259 kfree(nc);
260}
261
262struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
263 unsigned char id)
264{
265 struct ncsi_package *np;
266
267 NCSI_FOR_EACH_PACKAGE(ndp, np) {
268 if (np->id == id)
269 return np;
270 }
271
272 return NULL;
273}
274
275struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
276 unsigned char id)
277{
278 struct ncsi_package *np, *tmp;
279 unsigned long flags;
280
281 np = kzalloc(sizeof(*np), GFP_ATOMIC);
282 if (!np)
283 return NULL;
284
285 np->id = id;
286 np->ndp = ndp;
287 spin_lock_init(&np->lock);
288 INIT_LIST_HEAD(&np->channels);
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +1100289 np->channel_whitelist = UINT_MAX;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000290
291 spin_lock_irqsave(&ndp->lock, flags);
292 tmp = ncsi_find_package(ndp, id);
293 if (tmp) {
294 spin_unlock_irqrestore(&ndp->lock, flags);
295 kfree(np);
296 return tmp;
297 }
298
299 list_add_tail_rcu(&np->node, &ndp->packages);
300 ndp->package_num++;
301 spin_unlock_irqrestore(&ndp->lock, flags);
302
303 return np;
304}
305
306void ncsi_remove_package(struct ncsi_package *np)
307{
308 struct ncsi_dev_priv *ndp = np->ndp;
309 struct ncsi_channel *nc, *tmp;
310 unsigned long flags;
311
312 /* Release all child channels */
313 list_for_each_entry_safe(nc, tmp, &np->channels, node)
314 ncsi_remove_channel(nc);
315
316 /* Remove and free package */
317 spin_lock_irqsave(&ndp->lock, flags);
318 list_del_rcu(&np->node);
319 ndp->package_num--;
320 spin_unlock_irqrestore(&ndp->lock, flags);
321
322 kfree(np);
323}
324
325void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
326 unsigned char id,
327 struct ncsi_package **np,
328 struct ncsi_channel **nc)
329{
330 struct ncsi_package *p;
331 struct ncsi_channel *c;
332
333 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
334 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
335
336 if (np)
337 *np = p;
338 if (nc)
339 *nc = c;
340}
341
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.
345 */
Gavin Shana0509cb2016-10-04 11:25:51 +1100346struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
347 unsigned int req_flags)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000348{
349 struct ncsi_request *nr = NULL;
350 int i, limit = ARRAY_SIZE(ndp->requests);
351 unsigned long flags;
352
353 /* Check if there is one available request until the ceiling */
354 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shana15af542016-10-04 11:25:50 +1100355 for (i = ndp->request_id; i < limit; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000356 if (ndp->requests[i].used)
357 continue;
358
359 nr = &ndp->requests[i];
360 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100361 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100362 ndp->request_id = i + 1;
363 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000364 }
365
366 /* Fail back to check from the starting cursor */
Gavin Shana15af542016-10-04 11:25:50 +1100367 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000368 if (ndp->requests[i].used)
369 continue;
370
371 nr = &ndp->requests[i];
372 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100373 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100374 ndp->request_id = i + 1;
375 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000376 }
Gavin Shan2d283bd2016-07-19 11:54:16 +1000377
Gavin Shana15af542016-10-04 11:25:50 +1100378found:
379 spin_unlock_irqrestore(&ndp->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000380 return nr;
381}
382
383void ncsi_free_request(struct ncsi_request *nr)
384{
385 struct ncsi_dev_priv *ndp = nr->ndp;
386 struct sk_buff *cmd, *rsp;
387 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000388 bool driven;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000389
390 if (nr->enabled) {
391 nr->enabled = false;
392 del_timer_sync(&nr->timer);
393 }
394
395 spin_lock_irqsave(&ndp->lock, flags);
396 cmd = nr->cmd;
397 rsp = nr->rsp;
398 nr->cmd = NULL;
399 nr->rsp = NULL;
400 nr->used = false;
Gavin Shana0509cb2016-10-04 11:25:51 +1100401 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000402 spin_unlock_irqrestore(&ndp->lock, flags);
403
Gavin Shane6f44ed2016-07-19 11:54:19 +1000404 if (driven && cmd && --ndp->pending_req_num == 0)
405 schedule_work(&ndp->work);
406
Gavin Shan2d283bd2016-07-19 11:54:16 +1000407 /* Release command and response */
408 consume_skb(cmd);
409 consume_skb(rsp);
410}
411
412struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
413{
414 struct ncsi_dev_priv *ndp;
415
416 NCSI_FOR_EACH_DEV(ndp) {
417 if (ndp->ndev.dev == dev)
418 return &ndp->ndev;
419 }
420
421 return NULL;
422}
423
Kees Cooke99e88a2017-10-16 14:43:17 -0700424static void ncsi_request_timeout(struct timer_list *t)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000425{
Kees Cooke99e88a2017-10-16 14:43:17 -0700426 struct ncsi_request *nr = from_timer(nr, t, timer);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000427 struct ncsi_dev_priv *ndp = nr->ndp;
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000428 struct ncsi_cmd_pkt *cmd;
429 struct ncsi_package *np;
430 struct ncsi_channel *nc;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000431 unsigned long flags;
432
433 /* If the request already had associated response,
434 * let the response handler to release it.
435 */
436 spin_lock_irqsave(&ndp->lock, flags);
437 nr->enabled = false;
438 if (nr->rsp || !nr->cmd) {
439 spin_unlock_irqrestore(&ndp->lock, flags);
440 return;
441 }
442 spin_unlock_irqrestore(&ndp->lock, flags);
443
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000444 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
445 if (nr->cmd) {
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,
451 &np, &nc);
452 ncsi_send_netlink_timeout(nr, np, nc);
453 }
454 }
455
Gavin Shan2d283bd2016-07-19 11:54:16 +1000456 /* Release the request */
457 ncsi_free_request(nr);
458}
459
Gavin Shane6f44ed2016-07-19 11:54:19 +1000460static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
461{
462 struct ncsi_dev *nd = &ndp->ndev;
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100463 struct ncsi_package *np;
464 struct ncsi_channel *nc, *tmp;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000465 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100466 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000467 int ret;
468
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100469 np = ndp->active_package;
470 nc = ndp->active_channel;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000471 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100472 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000473 switch (nd->state) {
474 case ncsi_dev_state_suspend:
475 nd->state = ncsi_dev_state_suspend_select;
476 /* Fall through */
477 case ncsi_dev_state_suspend_select:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100478 ndp->pending_req_num = 1;
479
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)
484 nca.bytes[0] = 0;
485 else
486 nca.bytes[0] = 1;
487
Gavin Shan008a4242016-10-20 11:45:50 +1100488 /* 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.
495 */
496 if (ndp->flags & NCSI_DEV_RESHUFFLE)
497 nd->state = ncsi_dev_state_suspend_gls;
498 else
499 nd->state = ncsi_dev_state_suspend_dcnt;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100500 ret = ncsi_xmit_cmd(&nca);
501 if (ret)
502 goto error;
503
504 break;
Gavin Shan008a4242016-10-20 11:45:50 +1100505 case ncsi_dev_state_suspend_gls:
506 ndp->pending_req_num = np->channel_num;
507
508 nca.type = NCSI_PKT_CMD_GLS;
509 nca.package = np->id;
510
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);
515 if (ret)
516 goto error;
517 }
518
519 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000520 case ncsi_dev_state_suspend_dcnt:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100521 ndp->pending_req_num = 1;
522
523 nca.type = NCSI_PKT_CMD_DCNT;
524 nca.package = np->id;
525 nca.channel = nc->id;
526
527 nd->state = ncsi_dev_state_suspend_dc;
528 ret = ncsi_xmit_cmd(&nca);
529 if (ret)
530 goto error;
531
532 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000533 case ncsi_dev_state_suspend_dc:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100534 ndp->pending_req_num = 1;
535
536 nca.type = NCSI_PKT_CMD_DC;
537 nca.package = np->id;
538 nca.channel = nc->id;
539 nca.bytes[0] = 1;
540
541 nd->state = ncsi_dev_state_suspend_deselect;
542 ret = ncsi_xmit_cmd(&nca);
543 if (ret)
544 goto error;
545
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100546 NCSI_FOR_EACH_CHANNEL(np, tmp) {
547 /* If there is another channel active on this package
548 * do not deselect the package.
549 */
550 if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) {
551 nd->state = ncsi_dev_state_suspend_done;
552 break;
553 }
554 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100555 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000556 case ncsi_dev_state_suspend_deselect:
557 ndp->pending_req_num = 1;
558
Gavin Shan7ba5c002016-10-20 11:45:49 +1100559 nca.type = NCSI_PKT_CMD_DP;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000560 nca.package = np->id;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100561 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000562
Gavin Shan7ba5c002016-10-20 11:45:49 +1100563 nd->state = ncsi_dev_state_suspend_done;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000564 ret = ncsi_xmit_cmd(&nca);
Gavin Shan7ba5c002016-10-20 11:45:49 +1100565 if (ret)
566 goto error;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000567
568 break;
569 case ncsi_dev_state_suspend_done:
Gavin Shand8cedaa2016-10-04 11:25:47 +1100570 spin_lock_irqsave(&nc->lock, flags);
571 nc->state = NCSI_CHANNEL_INACTIVE;
572 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas2878a2c2018-11-16 15:51:58 +1100573 if (ndp->flags & NCSI_DEV_RESET)
574 ncsi_reset_dev(nd);
575 else
576 ncsi_process_next_channel(ndp);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000577 break;
578 default:
579 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
580 nd->state);
581 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100582
583 return;
584error:
585 nd->state = ncsi_dev_state_functional;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000586}
587
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000588/* Check the VLAN filter bitmap for a set filter, and construct a
589 * "Set VLAN Filter - Disable" packet if found.
590 */
591static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
592 struct ncsi_cmd_arg *nca)
593{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000594 struct ncsi_channel_vlan_filter *ncf;
595 unsigned long flags;
596 void *bitmap;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000597 int index;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000598 u16 vid;
599
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000600 ncf = &nc->vlan_filter;
601 bitmap = &ncf->bitmap;
602
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);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000607 return -1;
608 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000609 vid = ncf->vids[index];
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000610
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000611 clear_bit(index, bitmap);
612 ncf->vids[index] = 0;
613 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000614
615 nca->type = NCSI_PKT_CMD_SVF;
616 nca->words[1] = vid;
617 /* HW filter index starts at 1 */
618 nca->bytes[6] = index + 1;
619 nca->bytes[7] = 0x00;
620 return 0;
621}
622
623/* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
624 * packet.
625 */
626static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
627 struct ncsi_cmd_arg *nca)
628{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000629 struct ncsi_channel_vlan_filter *ncf;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000630 struct vlan_vid *vlan = NULL;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000631 unsigned long flags;
632 int i, index;
633 void *bitmap;
634 u16 vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000635
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000636 if (list_empty(&ndp->vlan_vids))
637 return -1;
638
639 ncf = &nc->vlan_filter;
640 bitmap = &ncf->bitmap;
641
642 spin_lock_irqsave(&nc->lock, flags);
643
644 rcu_read_lock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000645 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000646 vid = vlan->vid;
647 for (i = 0; i < ncf->n_vids; i++)
648 if (ncf->vids[i] == vid) {
649 vid = 0;
650 break;
651 }
652 if (vid)
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000653 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000654 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000655 rcu_read_unlock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000656
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000657 if (!vid) {
658 /* No VLAN ID is not set */
659 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000660 return -1;
661 }
662
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000663 index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
664 if (index < 0 || index >= ncf->n_vids) {
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000665 netdev_err(ndp->ndev.dev,
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000666 "Channel %u already has all VLAN filters set\n",
667 nc->id);
668 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000669 return -1;
670 }
671
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000672 ncf->vids[index] = vid;
673 set_bit(index, bitmap);
674 spin_unlock_irqrestore(&nc->lock, flags);
675
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000676 nca->type = NCSI_PKT_CMD_SVF;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000677 nca->words[1] = vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000678 /* HW filter index starts at 1 */
679 nca->bytes[6] = index + 1;
680 nca->bytes[7] = 0x01;
681
682 return 0;
683}
684
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700685#if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
686
687/* NCSI OEM Command APIs */
688static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
689{
690 unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
691 int ret = 0;
692
693 nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
694
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;
698
699 nca->data = data;
700
701 ret = ncsi_xmit_cmd(nca);
702 if (ret)
703 netdev_err(nca->ndp->ndev.dev,
704 "NCSI: Failed to transmit cmd 0x%x during configure\n",
705 nca->type);
706 return ret;
707}
708
Vijay Khemka16e8c4c2018-11-26 13:49:04 -0800709static int ncsi_oem_gma_handler_mlx(struct ncsi_cmd_arg *nca)
710{
711 union {
712 u8 data_u8[NCSI_OEM_MLX_CMD_GMA_LEN];
713 u32 data_u32[NCSI_OEM_MLX_CMD_GMA_LEN / sizeof(u32)];
714 } u;
715 int ret = 0;
716
717 nca->payload = NCSI_OEM_MLX_CMD_GMA_LEN;
718
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;
723
724 nca->data = u.data_u8;
725
726 ret = ncsi_xmit_cmd(nca);
727 if (ret)
728 netdev_err(nca->ndp->ndev.dev,
729 "NCSI: Failed to transmit cmd 0x%x during configure\n",
730 nca->type);
731 return ret;
732}
733
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700734/* OEM Command handlers initialization */
735static struct ncsi_oem_gma_handler {
736 unsigned int mfr_id;
737 int (*handler)(struct ncsi_cmd_arg *nca);
738} ncsi_oem_gma_handlers[] = {
Vijay Khemka16e8c4c2018-11-26 13:49:04 -0800739 { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm },
740 { NCSI_OEM_MFR_MLX_ID, ncsi_oem_gma_handler_mlx }
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700741};
742
743static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
744{
745 struct ncsi_oem_gma_handler *nch = NULL;
746 int i;
747
748 /* This function should only be called once, return if flag set */
749 if (nca->ndp->gma_flag == 1)
750 return -1;
751
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];
757 break;
758 }
759 }
760
761 if (!nch) {
762 netdev_err(nca->ndp->ndev.dev,
763 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
764 mf_id);
765 return -1;
766 }
767
768 /* Set the flag for GMA command which should only be called once */
769 nca->ndp->gma_flag = 1;
770
771 /* Get Mac address from NCSI device */
772 return nch->handler(nca);
773}
774
775#endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
776
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +1100777/* Determine if a given channel from the channel_queue should be used for Tx */
778static bool ncsi_channel_is_tx(struct ncsi_dev_priv *ndp,
779 struct ncsi_channel *nc)
780{
781 struct ncsi_channel_mode *ncm;
782 struct ncsi_channel *channel;
783 struct ncsi_package *np;
784
785 /* Check if any other channel has Tx enabled; a channel may have already
786 * been configured and removed from the channel queue.
787 */
788 NCSI_FOR_EACH_PACKAGE(ndp, np) {
789 if (!ndp->multi_package && np != nc->package)
790 continue;
791 NCSI_FOR_EACH_CHANNEL(np, channel) {
792 ncm = &channel->modes[NCSI_MODE_TX_ENABLE];
793 if (ncm->enable)
794 return false;
795 }
796 }
797
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;
804 }
805 }
806
807 /* This channel has link */
808 if (ncsi_channel_has_link(nc))
809 return true;
810
811 list_for_each_entry_rcu(channel, &ndp->channel_queue, link)
812 if (ncsi_channel_has_link(channel))
813 return false;
814
815 /* No other channel has link; default to this one */
816 return true;
817}
818
819/* Change the active Tx channel in a multi-channel setup */
820int ncsi_update_tx_channel(struct ncsi_dev_priv *ndp,
821 struct ncsi_package *package,
822 struct ncsi_channel *disable,
823 struct ncsi_channel *enable)
824{
825 struct ncsi_cmd_arg nca;
826 struct ncsi_channel *nc;
827 struct ncsi_package *np;
828 int ret = 0;
829
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");
833 nca.ndp = ndp;
834 nca.req_flags = 0;
835
836 /* Find current channel with Tx enabled */
837 NCSI_FOR_EACH_PACKAGE(ndp, np) {
838 if (disable)
839 break;
840 if (!ndp->multi_package && np != package)
841 continue;
842
843 NCSI_FOR_EACH_CHANNEL(np, nc)
844 if (nc->modes[NCSI_MODE_TX_ENABLE].enable) {
845 disable = nc;
846 break;
847 }
848 }
849
850 /* Find a suitable channel for Tx */
851 NCSI_FOR_EACH_PACKAGE(ndp, np) {
852 if (enable)
853 break;
854 if (!ndp->multi_package && np != package)
855 continue;
856 if (!(ndp->package_whitelist & (0x1 << np->id)))
857 continue;
858
859 if (np->preferred_channel &&
860 ncsi_channel_has_link(np->preferred_channel)) {
861 enable = np->preferred_channel;
862 break;
863 }
864
865 NCSI_FOR_EACH_CHANNEL(np, nc) {
866 if (!(np->channel_whitelist & 0x1 << nc->id))
867 continue;
868 if (nc->state != NCSI_CHANNEL_ACTIVE)
869 continue;
870 if (ncsi_channel_has_link(nc)) {
871 enable = nc;
872 break;
873 }
874 }
875 }
876
877 if (disable == enable)
878 return -1;
879
880 if (!enable)
881 return -1;
882
883 if (disable) {
884 nca.channel = disable->id;
885 nca.package = disable->package->id;
886 nca.type = NCSI_PKT_CMD_DCNT;
887 ret = ncsi_xmit_cmd(&nca);
888 if (ret)
889 netdev_err(ndp->ndev.dev,
890 "Error %d sending DCNT\n",
891 ret);
892 }
893
894 netdev_info(ndp->ndev.dev, "NCSI: channel %u enables Tx\n", enable->id);
895
896 nca.channel = enable->id;
897 nca.package = enable->package->id;
898 nca.type = NCSI_PKT_CMD_ECNT;
899 ret = ncsi_xmit_cmd(&nca);
900 if (ret)
901 netdev_err(ndp->ndev.dev,
902 "Error %d sending ECNT\n",
903 ret);
904
905 return ret;
906}
907
Gavin Shane6f44ed2016-07-19 11:54:19 +1000908static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
909{
Gavin Shane6f44ed2016-07-19 11:54:19 +1000910 struct ncsi_package *np = ndp->active_package;
911 struct ncsi_channel *nc = ndp->active_channel;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100912 struct ncsi_channel *hot_nc = NULL;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +1100913 struct ncsi_dev *nd = &ndp->ndev;
914 struct net_device *dev = nd->dev;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000915 struct ncsi_cmd_arg nca;
916 unsigned char index;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100917 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000918 int ret;
919
920 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100921 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000922 switch (nd->state) {
923 case ncsi_dev_state_config:
924 case ncsi_dev_state_config_sp:
925 ndp->pending_req_num = 1;
926
927 /* Select the specific package */
928 nca.type = NCSI_PKT_CMD_SP;
929 if (ndp->flags & NCSI_DEV_HWA)
930 nca.bytes[0] = 0;
931 else
932 nca.bytes[0] = 1;
933 nca.package = np->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100934 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000935 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100936 if (ret) {
937 netdev_err(ndp->ndev.dev,
938 "NCSI: Failed to transmit CMD_SP\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000939 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100940 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000941
942 nd->state = ncsi_dev_state_config_cis;
943 break;
944 case ncsi_dev_state_config_cis:
945 ndp->pending_req_num = 1;
946
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);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100952 if (ret) {
953 netdev_err(ndp->ndev.dev,
954 "NCSI: Failed to transmit CMD_CIS\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000955 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100956 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000957
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700958 nd->state = ncsi_dev_state_config_oem_gma;
959 break;
960 case ncsi_dev_state_config_oem_gma:
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000961 nd->state = ncsi_dev_state_config_clear_vids;
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700962 ret = -1;
963
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 */
971
972 if (ret < 0)
973 schedule_work(&ndp->work);
974
Gavin Shane6f44ed2016-07-19 11:54:19 +1000975 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000976 case ncsi_dev_state_config_clear_vids:
977 case ncsi_dev_state_config_svf:
978 case ncsi_dev_state_config_ev:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000979 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:
983#endif
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;
989
990 nca.package = np->id;
991 nca.channel = nc->id;
992
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000993 /* 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);
996 if (ret) {
997 nd->state = ncsi_dev_state_config_svf;
998 schedule_work(&ndp->work);
999 break;
1000 }
1001 /* Repeat */
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);
1006 if (ret) {
1007 nd->state = ncsi_dev_state_config_ev;
1008 schedule_work(&ndp->work);
1009 break;
1010 }
1011 /* Repeat */
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;
1017 } else {
1018 nca.type = NCSI_PKT_CMD_EV;
1019 nca.bytes[3] = NCSI_CAP_VLAN_NO;
1020 }
1021 nd->state = ncsi_dev_state_config_sma;
1022 } else if (nd->state == ncsi_dev_state_config_sma) {
Gavin Shane6f44ed2016-07-19 11:54:19 +10001023 /* Use first entry in unicast filter table. Note that
1024 * the MAC filter table starts from entry 1 instead of
1025 * 0.
1026 */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001027 nca.type = NCSI_PKT_CMD_SMA;
1028 for (index = 0; index < 6; index++)
1029 nca.bytes[index] = dev->dev_addr[index];
1030 nca.bytes[6] = 0x1;
1031 nca.bytes[7] = 0x1;
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;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001036 if (ncsi_channel_is_tx(ndp, nc))
1037 nd->state = ncsi_dev_state_config_ecnt;
1038 else
1039 nd->state = ncsi_dev_state_config_ec;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001040#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;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001045 } 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;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001048 if (ncsi_channel_is_tx(ndp, nc))
1049 nd->state = ncsi_dev_state_config_ecnt;
1050 else
1051 nd->state = ncsi_dev_state_config_ec;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001052#endif /* CONFIG_IPV6 */
1053 } else if (nd->state == ncsi_dev_state_config_ecnt) {
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001054 if (np->preferred_channel &&
1055 nc != np->preferred_channel)
1056 netdev_info(ndp->ndev.dev,
1057 "NCSI: Tx failed over to channel %u\n",
1058 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001059 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;
1069 nca.bytes[0] = 0;
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;
1075 }
1076
1077 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001078 if (ret) {
1079 netdev_err(ndp->ndev.dev,
1080 "NCSI: Failed to transmit CMD %x\n",
1081 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001082 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001083 }
Gavin Shane6f44ed2016-07-19 11:54:19 +10001084 break;
1085 case ncsi_dev_state_config_done:
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301086 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
1087 nc->id);
Gavin Shand8cedaa2016-10-04 11:25:47 +11001088 spin_lock_irqsave(&nc->lock, flags);
Samuel Mendoza-Jonas2878a2c2018-11-16 15:51:58 +11001089 nc->state = NCSI_CHANNEL_ACTIVE;
1090
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);
1095 ncsi_reset_dev(nd);
1096 break;
1097 }
1098
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001099 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
1103 */
1104 nc->reconfigure_needed = false;
1105 nc->state = NCSI_CHANNEL_INACTIVE;
1106 spin_unlock_irqrestore(&nc->lock, flags);
1107
1108 spin_lock_irqsave(&ndp->lock, flags);
1109 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1110 spin_unlock_irqrestore(&ndp->lock, flags);
1111
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301112 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001113 ncsi_process_next_channel(ndp);
1114 break;
1115 }
1116
Gavin Shanbbc7c012016-10-20 11:45:51 +11001117 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
1118 hot_nc = nc;
Gavin Shanbbc7c012016-10-20 11:45:51 +11001119 } else {
1120 hot_nc = NULL;
Joel Stanley87975a02018-06-19 15:08:31 +09301121 netdev_dbg(ndp->ndev.dev,
1122 "NCSI: channel %u link down after config\n",
1123 nc->id);
Gavin Shanbbc7c012016-10-20 11:45:51 +11001124 }
Gavin Shand8cedaa2016-10-04 11:25:47 +11001125 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001126
Gavin Shanbbc7c012016-10-20 11:45:51 +11001127 /* Update the hot channel */
1128 spin_lock_irqsave(&ndp->lock, flags);
1129 ndp->hot_channel = hot_nc;
1130 spin_unlock_irqrestore(&ndp->lock, flags);
1131
Gavin Shane6f44ed2016-07-19 11:54:19 +10001132 ncsi_start_channel_monitor(nc);
1133 ncsi_process_next_channel(ndp);
1134 break;
1135 default:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001136 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
1137 nd->state);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001138 }
1139
1140 return;
1141
1142error:
1143 ncsi_report_link(ndp, true);
1144}
1145
1146static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
1147{
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001148 struct ncsi_channel *nc, *found, *hot_nc;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001149 struct ncsi_channel_mode *ncm;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001150 unsigned long flags, cflags;
1151 struct ncsi_package *np;
1152 bool with_link;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001153
Gavin Shanbbc7c012016-10-20 11:45:51 +11001154 spin_lock_irqsave(&ndp->lock, flags);
1155 hot_nc = ndp->hot_channel;
1156 spin_unlock_irqrestore(&ndp->lock, flags);
1157
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001158 /* 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.
Gavin Shane6f44ed2016-07-19 11:54:19 +10001162 */
1163 found = NULL;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001164 with_link = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001165 NCSI_FOR_EACH_PACKAGE(ndp, np) {
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001166 if (!(ndp->package_whitelist & (0x1 << np->id)))
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001167 continue;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001168 NCSI_FOR_EACH_CHANNEL(np, nc) {
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001169 if (!(np->channel_whitelist & (0x1 << nc->id)))
1170 continue;
1171
1172 spin_lock_irqsave(&nc->lock, cflags);
Gavin Shand8cedaa2016-10-04 11:25:47 +11001173
Gavin Shane6f44ed2016-07-19 11:54:19 +10001174 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +11001175 nc->state != NCSI_CHANNEL_INACTIVE) {
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001176 spin_unlock_irqrestore(&nc->lock, cflags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001177 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +11001178 }
Gavin Shane6f44ed2016-07-19 11:54:19 +10001179
1180 if (!found)
1181 found = nc;
1182
Gavin Shanbbc7c012016-10-20 11:45:51 +11001183 if (nc == hot_nc)
1184 found = nc;
1185
Gavin Shane6f44ed2016-07-19 11:54:19 +10001186 ncm = &nc->modes[NCSI_MODE_LINK];
1187 if (ncm->data[2] & 0x1) {
1188 found = nc;
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001189 with_link = true;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001190 }
Gavin Shand8cedaa2016-10-04 11:25:47 +11001191
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001192 /* If multi_channel is enabled configure all valid
1193 * channels whether or not they currently have link
1194 * so they will have AENs enabled.
1195 */
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);
1201
1202 netdev_dbg(ndp->ndev.dev,
1203 "NCSI: Channel %u added to queue (link %s)\n",
1204 nc->id,
1205 ncm->data[2] & 0x1 ? "up" : "down");
1206 }
1207
1208 spin_unlock_irqrestore(&nc->lock, cflags);
1209
1210 if (with_link && !np->multi_channel)
1211 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001212 }
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001213 if (with_link && !ndp->multi_package)
1214 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001215 }
1216
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001217 if (list_empty(&ndp->channel_queue) && found) {
1218 netdev_info(ndp->ndev.dev,
1219 "NCSI: No channel with link found, configuring channel %u\n",
1220 found->id);
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) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001225 netdev_warn(ndp->ndev.dev,
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001226 "NCSI: No channel found to configure!\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +10001227 ncsi_report_link(ndp, true);
1228 return -ENODEV;
1229 }
1230
Gavin Shane6f44ed2016-07-19 11:54:19 +10001231 return ncsi_process_next_channel(ndp);
1232}
1233
1234static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1235{
1236 struct ncsi_package *np;
1237 struct ncsi_channel *nc;
1238 unsigned int cap;
Gavin Shan100ef012017-10-19 13:43:07 +11001239 bool has_channel = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001240
1241 /* The hardware arbitration is disabled if any one channel
1242 * doesn't support explicitly.
1243 */
1244 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1245 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shan100ef012017-10-19 13:43:07 +11001246 has_channel = true;
1247
Gavin Shane6f44ed2016-07-19 11:54:19 +10001248 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;
1253 return false;
1254 }
1255 }
1256 }
1257
Gavin Shan100ef012017-10-19 13:43:07 +11001258 if (has_channel) {
1259 ndp->flags |= NCSI_DEV_HWA;
1260 return true;
1261 }
1262
1263 ndp->flags &= ~NCSI_DEV_HWA;
1264 return false;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001265}
1266
Gavin Shane6f44ed2016-07-19 11:54:19 +10001267static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1268{
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;
1274 int ret;
1275
1276 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001277 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001278 switch (nd->state) {
1279 case ncsi_dev_state_probe:
1280 nd->state = ncsi_dev_state_probe_deselect;
1281 /* Fall through */
1282 case ncsi_dev_state_probe_deselect:
1283 ndp->pending_req_num = 8;
1284
1285 /* Deselect all possible packages */
1286 nca.type = NCSI_PKT_CMD_DP;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001287 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001288 for (index = 0; index < 8; index++) {
1289 nca.package = index;
1290 ret = ncsi_xmit_cmd(&nca);
1291 if (ret)
1292 goto error;
1293 }
1294
1295 nd->state = ncsi_dev_state_probe_package;
1296 break;
1297 case ncsi_dev_state_probe_package:
Gavin Shane6f44ed2016-07-19 11:54:19 +10001298 ndp->pending_req_num = 1;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001299
Gavin Shane6f44ed2016-07-19 11:54:19 +10001300 nca.type = NCSI_PKT_CMD_SP;
1301 nca.bytes[0] = 1;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001302 nca.package = ndp->package_probe_id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001303 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001304 ret = ncsi_xmit_cmd(&nca);
1305 if (ret)
1306 goto error;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001307 nd->state = ncsi_dev_state_probe_channel;
1308 break;
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) {
1313 /* No response */
1314 nd->state = ncsi_dev_state_probe_dp;
1315 schedule_work(&ndp->work);
1316 break;
1317 }
Gavin Shane6f44ed2016-07-19 11:54:19 +10001318 nd->state = ncsi_dev_state_probe_cis;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001319 schedule_work(&ndp->work);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001320 break;
1321 case ncsi_dev_state_probe_cis:
Gavin Shan55e02d02016-10-04 11:25:49 +11001322 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001323
1324 /* Clear initial state */
1325 nca.type = NCSI_PKT_CMD_CIS;
1326 nca.package = ndp->active_package->id;
Gavin Shan55e02d02016-10-04 11:25:49 +11001327 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
Gavin Shane6f44ed2016-07-19 11:54:19 +10001328 nca.channel = index;
1329 ret = ncsi_xmit_cmd(&nca);
1330 if (ret)
1331 goto error;
1332 }
1333
1334 nd->state = ncsi_dev_state_probe_gvi;
1335 break;
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;
1341
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;
1347 else
1348 nca.type = NCSI_PKT_CMD_GLS;
1349
1350 nca.package = np->id;
1351 NCSI_FOR_EACH_CHANNEL(np, nc) {
1352 nca.channel = nc->id;
1353 ret = ncsi_xmit_cmd(&nca);
1354 if (ret)
1355 goto error;
1356 }
1357
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;
1362 else
1363 nd->state = ncsi_dev_state_probe_dp;
1364 break;
1365 case ncsi_dev_state_probe_dp:
1366 ndp->pending_req_num = 1;
1367
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001368 /* Deselect the current package */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001369 nca.type = NCSI_PKT_CMD_DP;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001370 nca.package = ndp->package_probe_id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001371 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001372 ret = ncsi_xmit_cmd(&nca);
1373 if (ret)
1374 goto error;
1375
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001376 /* Probe next package */
1377 ndp->package_probe_id++;
1378 if (ndp->package_probe_id >= 8) {
1379 /* Probe finished */
1380 ndp->flags |= NCSI_DEV_PROBED;
1381 break;
1382 }
1383 nd->state = ncsi_dev_state_probe_package;
1384 ndp->active_package = NULL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001385 break;
1386 default:
1387 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1388 nd->state);
1389 }
1390
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001391 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);
1395 }
1396
Gavin Shane6f44ed2016-07-19 11:54:19 +10001397 return;
1398error:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001399 netdev_err(ndp->ndev.dev,
1400 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1401 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001402 ncsi_report_link(ndp, true);
1403}
1404
1405static void ncsi_dev_work(struct work_struct *work)
1406{
1407 struct ncsi_dev_priv *ndp = container_of(work,
1408 struct ncsi_dev_priv, work);
1409 struct ncsi_dev *nd = &ndp->ndev;
1410
1411 switch (nd->state & ncsi_dev_state_major) {
1412 case ncsi_dev_state_probe:
1413 ncsi_probe_channel(ndp);
1414 break;
1415 case ncsi_dev_state_suspend:
1416 ncsi_suspend_channel(ndp);
1417 break;
1418 case ncsi_dev_state_config:
1419 ncsi_configure_channel(ndp);
1420 break;
1421 default:
1422 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1423 nd->state);
1424 }
1425}
1426
1427int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1428{
1429 struct ncsi_channel *nc;
1430 int old_state;
1431 unsigned long flags;
1432
1433 spin_lock_irqsave(&ndp->lock, flags);
1434 nc = list_first_or_null_rcu(&ndp->channel_queue,
1435 struct ncsi_channel, link);
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001436 if (!nc) {
1437 spin_unlock_irqrestore(&ndp->lock, flags);
1438 goto out;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001439 }
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001440
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001441 list_del_init(&nc->link);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001442 spin_unlock_irqrestore(&ndp->lock, flags);
1443
Gavin Shand8cedaa2016-10-04 11:25:47 +11001444 spin_lock_irqsave(&nc->lock, flags);
1445 old_state = nc->state;
1446 nc->state = NCSI_CHANNEL_INVISIBLE;
1447 spin_unlock_irqrestore(&nc->lock, flags);
1448
Gavin Shane6f44ed2016-07-19 11:54:19 +10001449 ndp->active_channel = nc;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001450 ndp->active_package = nc->package;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001451
1452 switch (old_state) {
1453 case NCSI_CHANNEL_INACTIVE:
1454 ndp->ndev.state = ncsi_dev_state_config;
Joel Stanley87975a02018-06-19 15:08:31 +09301455 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1456 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001457 ncsi_configure_channel(ndp);
1458 break;
1459 case NCSI_CHANNEL_ACTIVE:
1460 ndp->ndev.state = ncsi_dev_state_suspend;
Joel Stanley87975a02018-06-19 15:08:31 +09301461 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1462 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001463 ncsi_suspend_channel(ndp);
1464 break;
1465 default:
1466 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
Gavin Shand8cedaa2016-10-04 11:25:47 +11001467 old_state, nc->package->id, nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001468 ncsi_report_link(ndp, false);
1469 return -EINVAL;
1470 }
1471
1472 return 0;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001473
1474out:
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);
1480 }
1481
1482 ncsi_report_link(ndp, false);
1483 return -ENODEV;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001484}
1485
1486#if IS_ENABLED(CONFIG_IPV6)
1487static int ncsi_inet6addr_event(struct notifier_block *this,
1488 unsigned long event, void *data)
1489{
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;
1497 bool action;
1498 int ret;
1499
1500 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1501 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1502 return NOTIFY_OK;
1503
1504 switch (event) {
1505 case NETDEV_UP:
1506 action = (++ndp->inet6_addr_num) == 1;
1507 nca.type = NCSI_PKT_CMD_EGMF;
1508 break;
1509 case NETDEV_DOWN:
1510 action = (--ndp->inet6_addr_num == 0);
1511 nca.type = NCSI_PKT_CMD_DGMF;
1512 break;
1513 default:
1514 return NOTIFY_OK;
1515 }
1516
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.
1520 */
1521 np = ndp->active_package;
1522 nc = ndp->active_channel;
1523 if (!action || !np || !nc)
1524 return NOTIFY_OK;
1525
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))
1528 return NOTIFY_OK;
1529
1530 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001531 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001532 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);
1536 if (ret) {
1537 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1538 (event == NETDEV_UP) ? "enable" : "disable", ret);
1539 return NOTIFY_DONE;
1540 }
1541
1542 return NOTIFY_OK;
1543}
1544
1545static struct notifier_block ncsi_inet6addr_notifier = {
1546 .notifier_call = ncsi_inet6addr_event,
1547};
1548#endif /* CONFIG_IPV6 */
1549
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001550static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1551{
1552 struct ncsi_dev *nd = &ndp->ndev;
1553 struct ncsi_channel *nc;
1554 struct ncsi_package *np;
1555 unsigned long flags;
1556 unsigned int n = 0;
1557
1558 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1559 NCSI_FOR_EACH_CHANNEL(np, nc) {
1560 spin_lock_irqsave(&nc->lock, flags);
1561
1562 /* Channels may be busy, mark dirty instead of
1563 * kicking if;
1564 * a) not ACTIVE (configured)
1565 * b) in the channel_queue (to be configured)
1566 * c) it's ndev is in the config state
1567 */
1568 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1569 if ((ndp->ndev.state & 0xff00) ==
1570 ncsi_dev_state_config ||
1571 !list_empty(&nc->link)) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301572 netdev_dbg(nd->dev,
1573 "NCSI: channel %p marked dirty\n",
1574 nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001575 nc->reconfigure_needed = true;
1576 }
1577 spin_unlock_irqrestore(&nc->lock, flags);
1578 continue;
1579 }
1580
1581 spin_unlock_irqrestore(&nc->lock, flags);
1582
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);
1587
1588 spin_lock_irqsave(&ndp->lock, flags);
1589 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1590 spin_unlock_irqrestore(&ndp->lock, flags);
1591
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301592 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001593 n++;
1594 }
1595 }
1596
1597 return n;
1598}
1599
1600int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1601{
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001602 struct ncsi_dev_priv *ndp;
1603 unsigned int n_vids = 0;
1604 struct vlan_vid *vlan;
1605 struct ncsi_dev *nd;
1606 bool found = false;
1607
1608 if (vid == 0)
1609 return 0;
1610
1611 nd = ncsi_find_dev(dev);
1612 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001613 netdev_warn(dev, "NCSI: No net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001614 return 0;
1615 }
1616
1617 ndp = TO_NCSI_DEV_PRIV(nd);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001618
1619 /* Add the VLAN id to our internal list */
1620 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1621 n_vids++;
1622 if (vlan->vid == vid) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301623 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1624 vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001625 return 0;
1626 }
1627 }
Samuel Mendoza-Jonas6e9c0072017-10-11 16:54:27 +11001628 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1629 netdev_warn(dev,
1630 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1631 vid, NCSI_MAX_VLAN_VIDS);
1632 return -ENOSPC;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001633 }
1634
1635 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1636 if (!vlan)
1637 return -ENOMEM;
1638
1639 vlan->proto = proto;
1640 vlan->vid = vid;
1641 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1642
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301643 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001644
1645 found = ncsi_kick_channels(ndp) != 0;
1646
1647 return found ? ncsi_process_next_channel(ndp) : 0;
1648}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001649EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001650
1651int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1652{
1653 struct vlan_vid *vlan, *tmp;
1654 struct ncsi_dev_priv *ndp;
1655 struct ncsi_dev *nd;
1656 bool found = false;
1657
1658 if (vid == 0)
1659 return 0;
1660
1661 nd = ncsi_find_dev(dev);
1662 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001663 netdev_warn(dev, "NCSI: no net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001664 return 0;
1665 }
1666
1667 ndp = TO_NCSI_DEV_PRIV(nd);
1668
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) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301672 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001673 list_del_rcu(&vlan->list);
1674 found = true;
1675 kfree(vlan);
1676 }
1677
1678 if (!found) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001679 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001680 return -EINVAL;
1681 }
1682
1683 found = ncsi_kick_channels(ndp) != 0;
1684
1685 return found ? ncsi_process_next_channel(ndp) : 0;
1686}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001687EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001688
Gavin Shan2d283bd2016-07-19 11:54:16 +10001689struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1690 void (*handler)(struct ncsi_dev *ndev))
1691{
1692 struct ncsi_dev_priv *ndp;
1693 struct ncsi_dev *nd;
1694 unsigned long flags;
1695 int i;
1696
1697 /* Check if the device has been registered or not */
1698 nd = ncsi_find_dev(dev);
1699 if (nd)
1700 return nd;
1701
1702 /* Create NCSI device */
1703 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1704 if (!ndp)
1705 return NULL;
1706
1707 nd = &ndp->ndev;
1708 nd->state = ncsi_dev_state_registered;
1709 nd->dev = dev;
1710 nd->handler = handler;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001711 ndp->pending_req_num = 0;
1712 INIT_LIST_HEAD(&ndp->channel_queue);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001713 INIT_LIST_HEAD(&ndp->vlan_vids);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001714 INIT_WORK(&ndp->work, ncsi_dev_work);
Samuel Mendoza-Jonas8d951a72018-11-16 15:51:59 +11001715 ndp->package_whitelist = UINT_MAX;
Gavin Shan2d283bd2016-07-19 11:54:16 +10001716
1717 /* Initialize private NCSI device */
1718 spin_lock_init(&ndp->lock);
1719 INIT_LIST_HEAD(&ndp->packages);
Gavin Shana15af542016-10-04 11:25:50 +11001720 ndp->request_id = NCSI_REQ_START_IDX;
Gavin Shan2d283bd2016-07-19 11:54:16 +10001721 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1722 ndp->requests[i].id = i;
1723 ndp->requests[i].ndp = ndp;
Kees Cooke99e88a2017-10-16 14:43:17 -07001724 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001725 }
1726
1727 spin_lock_irqsave(&ncsi_dev_lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001728#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);
1732#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001733 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1734 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1735
Gavin Shane6f44ed2016-07-19 11:54:19 +10001736 /* 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);
1741
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001742 /* Set up generic netlink interface */
1743 ncsi_init_netlink(dev);
1744
Gavin Shan2d283bd2016-07-19 11:54:16 +10001745 return nd;
1746}
1747EXPORT_SYMBOL_GPL(ncsi_register_dev);
1748
Gavin Shane6f44ed2016-07-19 11:54:19 +10001749int ncsi_start_dev(struct ncsi_dev *nd)
1750{
1751 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001752
1753 if (nd->state != ncsi_dev_state_registered &&
1754 nd->state != ncsi_dev_state_functional)
1755 return -ENOTTY;
1756
1757 if (!(ndp->flags & NCSI_DEV_PROBED)) {
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001758 ndp->package_probe_id = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001759 nd->state = ncsi_dev_state_probe;
1760 schedule_work(&ndp->work);
1761 return 0;
1762 }
1763
Samuel Mendoza-Jonas2878a2c2018-11-16 15:51:58 +11001764 return ncsi_reset_dev(nd);
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001765}
1766EXPORT_SYMBOL_GPL(ncsi_start_dev);
1767
1768void ncsi_stop_dev(struct ncsi_dev *nd)
1769{
1770 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1771 struct ncsi_package *np;
1772 struct ncsi_channel *nc;
1773 bool chained;
1774 int old_state;
1775 unsigned long flags;
1776
Samuel Mendoza-Jonas2878a2c2018-11-16 15:51:58 +11001777 /* 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()
1779 * is next called.
1780 */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001781 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1782 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001783 ncsi_stop_channel_monitor(nc);
1784
Gavin Shand8cedaa2016-10-04 11:25:47 +11001785 spin_lock_irqsave(&nc->lock, flags);
1786 chained = !list_empty(&nc->link);
1787 old_state = nc->state;
Gavin Shand8cedaa2016-10-04 11:25:47 +11001788 spin_unlock_irqrestore(&nc->lock, flags);
1789
1790 WARN_ON_ONCE(chained ||
Gavin Shane6f44ed2016-07-19 11:54:19 +10001791 old_state == NCSI_CHANNEL_INVISIBLE);
1792 }
1793 }
1794
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301795 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001796 ncsi_report_link(ndp, true);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001797}
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001798EXPORT_SYMBOL_GPL(ncsi_stop_dev);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001799
Samuel Mendoza-Jonas2878a2c2018-11-16 15:51:58 +11001800int ncsi_reset_dev(struct ncsi_dev *nd)
1801{
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;
1806
1807 spin_lock_irqsave(&ndp->lock, flags);
1808
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);
1816 return 0;
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.
1822 */
1823 ndp->flags |= NCSI_DEV_RESET;
1824 spin_unlock_irqrestore(&ndp->lock, flags);
1825 return 0;
1826 }
1827 } else {
1828 switch (nd->state) {
1829 case ncsi_dev_state_suspend_done:
1830 case ncsi_dev_state_config_done:
1831 case ncsi_dev_state_functional:
1832 /* Ok */
1833 break;
1834 default:
1835 /* Current reset operation happening */
1836 spin_unlock_irqrestore(&ndp->lock, flags);
1837 return 0;
1838 }
1839 }
1840
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);
1845 }
1846 spin_unlock_irqrestore(&ndp->lock, flags);
1847
1848 active = NULL;
1849 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1850 NCSI_FOR_EACH_CHANNEL(np, nc) {
1851 spin_lock_irqsave(&nc->lock, flags);
1852
1853 if (nc->state == NCSI_CHANNEL_ACTIVE) {
1854 active = nc;
1855 nc->state = NCSI_CHANNEL_INVISIBLE;
1856 spin_unlock_irqrestore(&nc->lock, flags);
1857 ncsi_stop_channel_monitor(nc);
1858 break;
1859 }
1860
1861 spin_unlock_irqrestore(&nc->lock, flags);
1862 }
1863 if (active)
1864 break;
1865 }
1866
1867 if (!active) {
1868 /* Done */
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);
1873 }
1874
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);
1880
1881 nd->state = ncsi_dev_state_suspend;
1882 schedule_work(&ndp->work);
1883 return 0;
1884}
1885
Gavin Shan2d283bd2016-07-19 11:54:16 +10001886void ncsi_unregister_dev(struct ncsi_dev *nd)
1887{
1888 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1889 struct ncsi_package *np, *tmp;
1890 unsigned long flags;
1891
Gavin Shane6f44ed2016-07-19 11:54:19 +10001892 dev_remove_pack(&ndp->ptype);
1893
Gavin Shan2d283bd2016-07-19 11:54:16 +10001894 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1895 ncsi_remove_package(np);
1896
1897 spin_lock_irqsave(&ncsi_dev_lock, flags);
1898 list_del_rcu(&ndp->node);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001899#if IS_ENABLED(CONFIG_IPV6)
1900 if (list_empty(&ncsi_dev_list))
1901 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1902#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001903 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1904
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001905 ncsi_unregister_netlink(nd->dev);
1906
Gavin Shan2d283bd2016-07-19 11:54:16 +10001907 kfree(ndp);
1908}
1909EXPORT_SYMBOL_GPL(ncsi_unregister_dev);