blob: 6aa0614d2d28686eb0e861147ca8ba7956ed1df9 [file] [log] [blame]
Gavin Shan2d283bd2016-07-19 11:54:16 +10001/*
2 * Copyright Gavin Shan, IBM Corporation 2016.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/netdevice.h>
14#include <linux/skbuff.h>
Gavin Shan2d283bd2016-07-19 11:54:16 +100015
16#include <net/ncsi.h>
17#include <net/net_namespace.h>
18#include <net/sock.h>
Gavin Shane6f44ed2016-07-19 11:54:19 +100019#include <net/addrconf.h>
20#include <net/ipv6.h>
21#include <net/if_inet6.h>
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +000022#include <net/genetlink.h>
Gavin Shan2d283bd2016-07-19 11:54:16 +100023
24#include "internal.h"
Gavin Shane6f44ed2016-07-19 11:54:19 +100025#include "ncsi-pkt.h"
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +110026#include "ncsi-netlink.h"
Gavin Shan2d283bd2016-07-19 11:54:16 +100027
28LIST_HEAD(ncsi_dev_list);
29DEFINE_SPINLOCK(ncsi_dev_lock);
30
Gavin Shane6f44ed2016-07-19 11:54:19 +100031static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
32{
33 struct ncsi_dev *nd = &ndp->ndev;
34 struct ncsi_package *np;
35 struct ncsi_channel *nc;
Gavin Shand8cedaa2016-10-04 11:25:47 +110036 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +100037
38 nd->state = ncsi_dev_state_functional;
39 if (force_down) {
40 nd->link_up = 0;
41 goto report;
42 }
43
44 nd->link_up = 0;
45 NCSI_FOR_EACH_PACKAGE(ndp, np) {
46 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +110047 spin_lock_irqsave(&nc->lock, flags);
48
Gavin Shane6f44ed2016-07-19 11:54:19 +100049 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +110050 nc->state != NCSI_CHANNEL_ACTIVE) {
51 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100052 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +110053 }
Gavin Shane6f44ed2016-07-19 11:54:19 +100054
55 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
Gavin Shand8cedaa2016-10-04 11:25:47 +110056 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100057 nd->link_up = 1;
58 goto report;
59 }
Gavin Shand8cedaa2016-10-04 11:25:47 +110060
61 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +100062 }
63 }
64
65report:
66 nd->handler(nd);
67}
68
Kees Cook86cb30e2017-10-17 20:21:24 -070069static void ncsi_channel_monitor(struct timer_list *t)
Gavin Shane6f44ed2016-07-19 11:54:19 +100070{
Kees Cook86cb30e2017-10-17 20:21:24 -070071 struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +100072 struct ncsi_package *np = nc->package;
73 struct ncsi_dev_priv *ndp = np->ndp;
Gavin Shan52b4c862017-10-19 13:43:08 +110074 struct ncsi_channel_mode *ncm;
Gavin Shane6f44ed2016-07-19 11:54:19 +100075 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +110076 bool enabled, chained;
Gavin Shan83afdc62016-10-04 11:25:52 +110077 unsigned int monitor_state;
Gavin Shane6f44ed2016-07-19 11:54:19 +100078 unsigned long flags;
Gavin Shand8cedaa2016-10-04 11:25:47 +110079 int state, ret;
Gavin Shane6f44ed2016-07-19 11:54:19 +100080
81 spin_lock_irqsave(&nc->lock, flags);
Gavin Shand8cedaa2016-10-04 11:25:47 +110082 state = nc->state;
83 chained = !list_empty(&nc->link);
Gavin Shan83afdc62016-10-04 11:25:52 +110084 enabled = nc->monitor.enabled;
85 monitor_state = nc->monitor.state;
Gavin Shane6f44ed2016-07-19 11:54:19 +100086 spin_unlock_irqrestore(&nc->lock, flags);
87
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +110088 if (!enabled || chained) {
89 ncsi_stop_channel_monitor(nc);
Gavin Shane6f44ed2016-07-19 11:54:19 +100090 return;
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +110091 }
Gavin Shand8cedaa2016-10-04 11:25:47 +110092 if (state != NCSI_CHANNEL_INACTIVE &&
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +110093 state != NCSI_CHANNEL_ACTIVE) {
94 ncsi_stop_channel_monitor(nc);
Gavin Shane6f44ed2016-07-19 11:54:19 +100095 return;
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +110096 }
Gavin Shane6f44ed2016-07-19 11:54:19 +100097
Gavin Shan83afdc62016-10-04 11:25:52 +110098 switch (monitor_state) {
99 case NCSI_CHANNEL_MONITOR_START:
100 case NCSI_CHANNEL_MONITOR_RETRY:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000101 nca.ndp = ndp;
102 nca.package = np->id;
103 nca.channel = nc->id;
104 nca.type = NCSI_PKT_CMD_GLS;
Gavin Shana0509cb2016-10-04 11:25:51 +1100105 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000106 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100107 if (ret)
Gavin Shane6f44ed2016-07-19 11:54:19 +1000108 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
109 ret);
Gavin Shan83afdc62016-10-04 11:25:52 +1100110 break;
111 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
112 break;
113 default:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100114 netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
115 nc->id);
Gavin Shan52b4c862017-10-19 13:43:08 +1100116 if (!(ndp->flags & NCSI_DEV_HWA)) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000117 ncsi_report_link(ndp, true);
Gavin Shan83afdc62016-10-04 11:25:52 +1100118 ndp->flags |= NCSI_DEV_RESHUFFLE;
119 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000120
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100121 ncsi_stop_channel_monitor(nc);
122
Gavin Shan52b4c862017-10-19 13:43:08 +1100123 ncm = &nc->modes[NCSI_MODE_LINK];
Gavin Shand8cedaa2016-10-04 11:25:47 +1100124 spin_lock_irqsave(&nc->lock, flags);
125 nc->state = NCSI_CHANNEL_INVISIBLE;
Gavin Shan52b4c862017-10-19 13:43:08 +1100126 ncm->data[2] &= ~0x1;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100127 spin_unlock_irqrestore(&nc->lock, flags);
128
Gavin Shane6f44ed2016-07-19 11:54:19 +1000129 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shan52b4c862017-10-19 13:43:08 +1100130 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000131 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
132 spin_unlock_irqrestore(&ndp->lock, flags);
133 ncsi_process_next_channel(ndp);
134 return;
135 }
136
137 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100138 nc->monitor.state++;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000139 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100140 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000141}
142
143void ncsi_start_channel_monitor(struct ncsi_channel *nc)
144{
145 unsigned long flags;
146
147 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100148 WARN_ON_ONCE(nc->monitor.enabled);
149 nc->monitor.enabled = true;
150 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000151 spin_unlock_irqrestore(&nc->lock, flags);
152
Gavin Shan83afdc62016-10-04 11:25:52 +1100153 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000154}
155
156void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
157{
158 unsigned long flags;
159
160 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100161 if (!nc->monitor.enabled) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000162 spin_unlock_irqrestore(&nc->lock, flags);
163 return;
164 }
Gavin Shan83afdc62016-10-04 11:25:52 +1100165 nc->monitor.enabled = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000166 spin_unlock_irqrestore(&nc->lock, flags);
167
Gavin Shan83afdc62016-10-04 11:25:52 +1100168 del_timer_sync(&nc->monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000169}
170
Gavin Shan2d283bd2016-07-19 11:54:16 +1000171struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
172 unsigned char id)
173{
174 struct ncsi_channel *nc;
175
176 NCSI_FOR_EACH_CHANNEL(np, nc) {
177 if (nc->id == id)
178 return nc;
179 }
180
181 return NULL;
182}
183
184struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
185{
186 struct ncsi_channel *nc, *tmp;
187 int index;
188 unsigned long flags;
189
190 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
191 if (!nc)
192 return NULL;
193
194 nc->id = id;
195 nc->package = np;
196 nc->state = NCSI_CHANNEL_INACTIVE;
Gavin Shan83afdc62016-10-04 11:25:52 +1100197 nc->monitor.enabled = false;
Kees Cook86cb30e2017-10-17 20:21:24 -0700198 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000199 spin_lock_init(&nc->lock);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000200 INIT_LIST_HEAD(&nc->link);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000201 for (index = 0; index < NCSI_CAP_MAX; index++)
202 nc->caps[index].index = index;
203 for (index = 0; index < NCSI_MODE_MAX; index++)
204 nc->modes[index].index = index;
205
206 spin_lock_irqsave(&np->lock, flags);
207 tmp = ncsi_find_channel(np, id);
208 if (tmp) {
209 spin_unlock_irqrestore(&np->lock, flags);
210 kfree(nc);
211 return tmp;
212 }
213
214 list_add_tail_rcu(&nc->node, &np->channels);
215 np->channel_num++;
216 spin_unlock_irqrestore(&np->lock, flags);
217
218 return nc;
219}
220
221static void ncsi_remove_channel(struct ncsi_channel *nc)
222{
223 struct ncsi_package *np = nc->package;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000224 unsigned long flags;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000225
226 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000227
228 /* Release filters */
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000229 kfree(nc->mac_filter.addrs);
230 kfree(nc->vlan_filter.vids);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000231
232 nc->state = NCSI_CHANNEL_INACTIVE;
233 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000234 ncsi_stop_channel_monitor(nc);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000235
236 /* Remove and free channel */
237 spin_lock_irqsave(&np->lock, flags);
238 list_del_rcu(&nc->node);
239 np->channel_num--;
240 spin_unlock_irqrestore(&np->lock, flags);
241
242 kfree(nc);
243}
244
245struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
246 unsigned char id)
247{
248 struct ncsi_package *np;
249
250 NCSI_FOR_EACH_PACKAGE(ndp, np) {
251 if (np->id == id)
252 return np;
253 }
254
255 return NULL;
256}
257
258struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
259 unsigned char id)
260{
261 struct ncsi_package *np, *tmp;
262 unsigned long flags;
263
264 np = kzalloc(sizeof(*np), GFP_ATOMIC);
265 if (!np)
266 return NULL;
267
268 np->id = id;
269 np->ndp = ndp;
270 spin_lock_init(&np->lock);
271 INIT_LIST_HEAD(&np->channels);
272
273 spin_lock_irqsave(&ndp->lock, flags);
274 tmp = ncsi_find_package(ndp, id);
275 if (tmp) {
276 spin_unlock_irqrestore(&ndp->lock, flags);
277 kfree(np);
278 return tmp;
279 }
280
281 list_add_tail_rcu(&np->node, &ndp->packages);
282 ndp->package_num++;
283 spin_unlock_irqrestore(&ndp->lock, flags);
284
285 return np;
286}
287
288void ncsi_remove_package(struct ncsi_package *np)
289{
290 struct ncsi_dev_priv *ndp = np->ndp;
291 struct ncsi_channel *nc, *tmp;
292 unsigned long flags;
293
294 /* Release all child channels */
295 list_for_each_entry_safe(nc, tmp, &np->channels, node)
296 ncsi_remove_channel(nc);
297
298 /* Remove and free package */
299 spin_lock_irqsave(&ndp->lock, flags);
300 list_del_rcu(&np->node);
301 ndp->package_num--;
302 spin_unlock_irqrestore(&ndp->lock, flags);
303
304 kfree(np);
305}
306
307void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
308 unsigned char id,
309 struct ncsi_package **np,
310 struct ncsi_channel **nc)
311{
312 struct ncsi_package *p;
313 struct ncsi_channel *c;
314
315 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
316 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
317
318 if (np)
319 *np = p;
320 if (nc)
321 *nc = c;
322}
323
324/* For two consecutive NCSI commands, the packet IDs shouldn't
325 * be same. Otherwise, the bogus response might be replied. So
326 * the available IDs are allocated in round-robin fashion.
327 */
Gavin Shana0509cb2016-10-04 11:25:51 +1100328struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
329 unsigned int req_flags)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000330{
331 struct ncsi_request *nr = NULL;
332 int i, limit = ARRAY_SIZE(ndp->requests);
333 unsigned long flags;
334
335 /* Check if there is one available request until the ceiling */
336 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shana15af542016-10-04 11:25:50 +1100337 for (i = ndp->request_id; i < limit; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000338 if (ndp->requests[i].used)
339 continue;
340
341 nr = &ndp->requests[i];
342 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100343 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100344 ndp->request_id = i + 1;
345 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000346 }
347
348 /* Fail back to check from the starting cursor */
Gavin Shana15af542016-10-04 11:25:50 +1100349 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000350 if (ndp->requests[i].used)
351 continue;
352
353 nr = &ndp->requests[i];
354 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100355 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100356 ndp->request_id = i + 1;
357 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000358 }
Gavin Shan2d283bd2016-07-19 11:54:16 +1000359
Gavin Shana15af542016-10-04 11:25:50 +1100360found:
361 spin_unlock_irqrestore(&ndp->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000362 return nr;
363}
364
365void ncsi_free_request(struct ncsi_request *nr)
366{
367 struct ncsi_dev_priv *ndp = nr->ndp;
368 struct sk_buff *cmd, *rsp;
369 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000370 bool driven;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000371
372 if (nr->enabled) {
373 nr->enabled = false;
374 del_timer_sync(&nr->timer);
375 }
376
377 spin_lock_irqsave(&ndp->lock, flags);
378 cmd = nr->cmd;
379 rsp = nr->rsp;
380 nr->cmd = NULL;
381 nr->rsp = NULL;
382 nr->used = false;
Gavin Shana0509cb2016-10-04 11:25:51 +1100383 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000384 spin_unlock_irqrestore(&ndp->lock, flags);
385
Gavin Shane6f44ed2016-07-19 11:54:19 +1000386 if (driven && cmd && --ndp->pending_req_num == 0)
387 schedule_work(&ndp->work);
388
Gavin Shan2d283bd2016-07-19 11:54:16 +1000389 /* Release command and response */
390 consume_skb(cmd);
391 consume_skb(rsp);
392}
393
394struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
395{
396 struct ncsi_dev_priv *ndp;
397
398 NCSI_FOR_EACH_DEV(ndp) {
399 if (ndp->ndev.dev == dev)
400 return &ndp->ndev;
401 }
402
403 return NULL;
404}
405
Kees Cooke99e88a2017-10-16 14:43:17 -0700406static void ncsi_request_timeout(struct timer_list *t)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000407{
Kees Cooke99e88a2017-10-16 14:43:17 -0700408 struct ncsi_request *nr = from_timer(nr, t, timer);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000409 struct ncsi_dev_priv *ndp = nr->ndp;
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000410 struct ncsi_cmd_pkt *cmd;
411 struct ncsi_package *np;
412 struct ncsi_channel *nc;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000413 unsigned long flags;
414
415 /* If the request already had associated response,
416 * let the response handler to release it.
417 */
418 spin_lock_irqsave(&ndp->lock, flags);
419 nr->enabled = false;
420 if (nr->rsp || !nr->cmd) {
421 spin_unlock_irqrestore(&ndp->lock, flags);
422 return;
423 }
424 spin_unlock_irqrestore(&ndp->lock, flags);
425
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000426 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
427 if (nr->cmd) {
428 /* Find the package */
429 cmd = (struct ncsi_cmd_pkt *)
430 skb_network_header(nr->cmd);
431 ncsi_find_package_and_channel(ndp,
432 cmd->cmd.common.channel,
433 &np, &nc);
434 ncsi_send_netlink_timeout(nr, np, nc);
435 }
436 }
437
Gavin Shan2d283bd2016-07-19 11:54:16 +1000438 /* Release the request */
439 ncsi_free_request(nr);
440}
441
Gavin Shane6f44ed2016-07-19 11:54:19 +1000442static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
443{
444 struct ncsi_dev *nd = &ndp->ndev;
445 struct ncsi_package *np = ndp->active_package;
446 struct ncsi_channel *nc = ndp->active_channel;
447 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100448 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000449 int ret;
450
451 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100452 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000453 switch (nd->state) {
454 case ncsi_dev_state_suspend:
455 nd->state = ncsi_dev_state_suspend_select;
456 /* Fall through */
457 case ncsi_dev_state_suspend_select:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100458 ndp->pending_req_num = 1;
459
460 nca.type = NCSI_PKT_CMD_SP;
461 nca.package = np->id;
462 nca.channel = NCSI_RESERVED_CHANNEL;
463 if (ndp->flags & NCSI_DEV_HWA)
464 nca.bytes[0] = 0;
465 else
466 nca.bytes[0] = 1;
467
Gavin Shan008a4242016-10-20 11:45:50 +1100468 /* To retrieve the last link states of channels in current
469 * package when current active channel needs fail over to
470 * another one. It means we will possibly select another
471 * channel as next active one. The link states of channels
472 * are most important factor of the selection. So we need
473 * accurate link states. Unfortunately, the link states on
474 * inactive channels can't be updated with LSC AEN in time.
475 */
476 if (ndp->flags & NCSI_DEV_RESHUFFLE)
477 nd->state = ncsi_dev_state_suspend_gls;
478 else
479 nd->state = ncsi_dev_state_suspend_dcnt;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100480 ret = ncsi_xmit_cmd(&nca);
481 if (ret)
482 goto error;
483
484 break;
Gavin Shan008a4242016-10-20 11:45:50 +1100485 case ncsi_dev_state_suspend_gls:
486 ndp->pending_req_num = np->channel_num;
487
488 nca.type = NCSI_PKT_CMD_GLS;
489 nca.package = np->id;
490
491 nd->state = ncsi_dev_state_suspend_dcnt;
492 NCSI_FOR_EACH_CHANNEL(np, nc) {
493 nca.channel = nc->id;
494 ret = ncsi_xmit_cmd(&nca);
495 if (ret)
496 goto error;
497 }
498
499 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000500 case ncsi_dev_state_suspend_dcnt:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100501 ndp->pending_req_num = 1;
502
503 nca.type = NCSI_PKT_CMD_DCNT;
504 nca.package = np->id;
505 nca.channel = nc->id;
506
507 nd->state = ncsi_dev_state_suspend_dc;
508 ret = ncsi_xmit_cmd(&nca);
509 if (ret)
510 goto error;
511
512 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000513 case ncsi_dev_state_suspend_dc:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100514 ndp->pending_req_num = 1;
515
516 nca.type = NCSI_PKT_CMD_DC;
517 nca.package = np->id;
518 nca.channel = nc->id;
519 nca.bytes[0] = 1;
520
521 nd->state = ncsi_dev_state_suspend_deselect;
522 ret = ncsi_xmit_cmd(&nca);
523 if (ret)
524 goto error;
525
526 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000527 case ncsi_dev_state_suspend_deselect:
528 ndp->pending_req_num = 1;
529
Gavin Shan7ba5c002016-10-20 11:45:49 +1100530 nca.type = NCSI_PKT_CMD_DP;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000531 nca.package = np->id;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100532 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000533
Gavin Shan7ba5c002016-10-20 11:45:49 +1100534 nd->state = ncsi_dev_state_suspend_done;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000535 ret = ncsi_xmit_cmd(&nca);
Gavin Shan7ba5c002016-10-20 11:45:49 +1100536 if (ret)
537 goto error;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000538
539 break;
540 case ncsi_dev_state_suspend_done:
Gavin Shand8cedaa2016-10-04 11:25:47 +1100541 spin_lock_irqsave(&nc->lock, flags);
542 nc->state = NCSI_CHANNEL_INACTIVE;
543 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000544 ncsi_process_next_channel(ndp);
545
546 break;
547 default:
548 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
549 nd->state);
550 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100551
552 return;
553error:
554 nd->state = ncsi_dev_state_functional;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000555}
556
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000557/* Check the VLAN filter bitmap for a set filter, and construct a
558 * "Set VLAN Filter - Disable" packet if found.
559 */
560static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
561 struct ncsi_cmd_arg *nca)
562{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000563 struct ncsi_channel_vlan_filter *ncf;
564 unsigned long flags;
565 void *bitmap;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000566 int index;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000567 u16 vid;
568
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000569 ncf = &nc->vlan_filter;
570 bitmap = &ncf->bitmap;
571
572 spin_lock_irqsave(&nc->lock, flags);
573 index = find_next_bit(bitmap, ncf->n_vids, 0);
574 if (index >= ncf->n_vids) {
575 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000576 return -1;
577 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000578 vid = ncf->vids[index];
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000579
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000580 clear_bit(index, bitmap);
581 ncf->vids[index] = 0;
582 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000583
584 nca->type = NCSI_PKT_CMD_SVF;
585 nca->words[1] = vid;
586 /* HW filter index starts at 1 */
587 nca->bytes[6] = index + 1;
588 nca->bytes[7] = 0x00;
589 return 0;
590}
591
592/* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
593 * packet.
594 */
595static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
596 struct ncsi_cmd_arg *nca)
597{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000598 struct ncsi_channel_vlan_filter *ncf;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000599 struct vlan_vid *vlan = NULL;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000600 unsigned long flags;
601 int i, index;
602 void *bitmap;
603 u16 vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000604
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000605 if (list_empty(&ndp->vlan_vids))
606 return -1;
607
608 ncf = &nc->vlan_filter;
609 bitmap = &ncf->bitmap;
610
611 spin_lock_irqsave(&nc->lock, flags);
612
613 rcu_read_lock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000614 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000615 vid = vlan->vid;
616 for (i = 0; i < ncf->n_vids; i++)
617 if (ncf->vids[i] == vid) {
618 vid = 0;
619 break;
620 }
621 if (vid)
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000622 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000623 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000624 rcu_read_unlock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000625
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000626 if (!vid) {
627 /* No VLAN ID is not set */
628 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000629 return -1;
630 }
631
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000632 index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
633 if (index < 0 || index >= ncf->n_vids) {
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000634 netdev_err(ndp->ndev.dev,
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000635 "Channel %u already has all VLAN filters set\n",
636 nc->id);
637 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000638 return -1;
639 }
640
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000641 ncf->vids[index] = vid;
642 set_bit(index, bitmap);
643 spin_unlock_irqrestore(&nc->lock, flags);
644
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000645 nca->type = NCSI_PKT_CMD_SVF;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000646 nca->words[1] = vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000647 /* HW filter index starts at 1 */
648 nca->bytes[6] = index + 1;
649 nca->bytes[7] = 0x01;
650
651 return 0;
652}
653
Gavin Shane6f44ed2016-07-19 11:54:19 +1000654static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
655{
656 struct ncsi_dev *nd = &ndp->ndev;
657 struct net_device *dev = nd->dev;
658 struct ncsi_package *np = ndp->active_package;
659 struct ncsi_channel *nc = ndp->active_channel;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100660 struct ncsi_channel *hot_nc = NULL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000661 struct ncsi_cmd_arg nca;
662 unsigned char index;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100663 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000664 int ret;
665
666 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100667 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000668 switch (nd->state) {
669 case ncsi_dev_state_config:
670 case ncsi_dev_state_config_sp:
671 ndp->pending_req_num = 1;
672
673 /* Select the specific package */
674 nca.type = NCSI_PKT_CMD_SP;
675 if (ndp->flags & NCSI_DEV_HWA)
676 nca.bytes[0] = 0;
677 else
678 nca.bytes[0] = 1;
679 nca.package = np->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100680 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000681 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100682 if (ret) {
683 netdev_err(ndp->ndev.dev,
684 "NCSI: Failed to transmit CMD_SP\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000685 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100686 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000687
688 nd->state = ncsi_dev_state_config_cis;
689 break;
690 case ncsi_dev_state_config_cis:
691 ndp->pending_req_num = 1;
692
693 /* Clear initial state */
694 nca.type = NCSI_PKT_CMD_CIS;
695 nca.package = np->id;
696 nca.channel = nc->id;
697 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100698 if (ret) {
699 netdev_err(ndp->ndev.dev,
700 "NCSI: Failed to transmit CMD_CIS\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000701 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100702 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000703
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000704 nd->state = ncsi_dev_state_config_clear_vids;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000705 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000706 case ncsi_dev_state_config_clear_vids:
707 case ncsi_dev_state_config_svf:
708 case ncsi_dev_state_config_ev:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000709 case ncsi_dev_state_config_sma:
710 case ncsi_dev_state_config_ebf:
711#if IS_ENABLED(CONFIG_IPV6)
712 case ncsi_dev_state_config_egmf:
713#endif
714 case ncsi_dev_state_config_ecnt:
715 case ncsi_dev_state_config_ec:
716 case ncsi_dev_state_config_ae:
717 case ncsi_dev_state_config_gls:
718 ndp->pending_req_num = 1;
719
720 nca.package = np->id;
721 nca.channel = nc->id;
722
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000723 /* Clear any active filters on the channel before setting */
724 if (nd->state == ncsi_dev_state_config_clear_vids) {
725 ret = clear_one_vid(ndp, nc, &nca);
726 if (ret) {
727 nd->state = ncsi_dev_state_config_svf;
728 schedule_work(&ndp->work);
729 break;
730 }
731 /* Repeat */
732 nd->state = ncsi_dev_state_config_clear_vids;
733 /* Add known VLAN tags to the filter */
734 } else if (nd->state == ncsi_dev_state_config_svf) {
735 ret = set_one_vid(ndp, nc, &nca);
736 if (ret) {
737 nd->state = ncsi_dev_state_config_ev;
738 schedule_work(&ndp->work);
739 break;
740 }
741 /* Repeat */
742 nd->state = ncsi_dev_state_config_svf;
743 /* Enable/Disable the VLAN filter */
744 } else if (nd->state == ncsi_dev_state_config_ev) {
745 if (list_empty(&ndp->vlan_vids)) {
746 nca.type = NCSI_PKT_CMD_DV;
747 } else {
748 nca.type = NCSI_PKT_CMD_EV;
749 nca.bytes[3] = NCSI_CAP_VLAN_NO;
750 }
751 nd->state = ncsi_dev_state_config_sma;
752 } else if (nd->state == ncsi_dev_state_config_sma) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000753 /* Use first entry in unicast filter table. Note that
754 * the MAC filter table starts from entry 1 instead of
755 * 0.
756 */
Gavin Shane6f44ed2016-07-19 11:54:19 +1000757 nca.type = NCSI_PKT_CMD_SMA;
758 for (index = 0; index < 6; index++)
759 nca.bytes[index] = dev->dev_addr[index];
760 nca.bytes[6] = 0x1;
761 nca.bytes[7] = 0x1;
762 nd->state = ncsi_dev_state_config_ebf;
763 } else if (nd->state == ncsi_dev_state_config_ebf) {
764 nca.type = NCSI_PKT_CMD_EBF;
765 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
766 nd->state = ncsi_dev_state_config_ecnt;
767#if IS_ENABLED(CONFIG_IPV6)
768 if (ndp->inet6_addr_num > 0 &&
769 (nc->caps[NCSI_CAP_GENERIC].cap &
770 NCSI_CAP_GENERIC_MC))
771 nd->state = ncsi_dev_state_config_egmf;
772 else
773 nd->state = ncsi_dev_state_config_ecnt;
774 } else if (nd->state == ncsi_dev_state_config_egmf) {
775 nca.type = NCSI_PKT_CMD_EGMF;
776 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
777 nd->state = ncsi_dev_state_config_ecnt;
778#endif /* CONFIG_IPV6 */
779 } else if (nd->state == ncsi_dev_state_config_ecnt) {
780 nca.type = NCSI_PKT_CMD_ECNT;
781 nd->state = ncsi_dev_state_config_ec;
782 } else if (nd->state == ncsi_dev_state_config_ec) {
783 /* Enable AEN if it's supported */
784 nca.type = NCSI_PKT_CMD_EC;
785 nd->state = ncsi_dev_state_config_ae;
786 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
787 nd->state = ncsi_dev_state_config_gls;
788 } else if (nd->state == ncsi_dev_state_config_ae) {
789 nca.type = NCSI_PKT_CMD_AE;
790 nca.bytes[0] = 0;
791 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
792 nd->state = ncsi_dev_state_config_gls;
793 } else if (nd->state == ncsi_dev_state_config_gls) {
794 nca.type = NCSI_PKT_CMD_GLS;
795 nd->state = ncsi_dev_state_config_done;
796 }
797
798 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100799 if (ret) {
800 netdev_err(ndp->ndev.dev,
801 "NCSI: Failed to transmit CMD %x\n",
802 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000803 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100804 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000805 break;
806 case ncsi_dev_state_config_done:
Joel Stanley6e42a3f2018-06-19 15:08:33 +0930807 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
808 nc->id);
Gavin Shand8cedaa2016-10-04 11:25:47 +1100809 spin_lock_irqsave(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000810 if (nc->reconfigure_needed) {
811 /* This channel's configuration has been updated
812 * part-way during the config state - start the
813 * channel configuration over
814 */
815 nc->reconfigure_needed = false;
816 nc->state = NCSI_CHANNEL_INACTIVE;
817 spin_unlock_irqrestore(&nc->lock, flags);
818
819 spin_lock_irqsave(&ndp->lock, flags);
820 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
821 spin_unlock_irqrestore(&ndp->lock, flags);
822
Joel Stanley6e42a3f2018-06-19 15:08:33 +0930823 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000824 ncsi_process_next_channel(ndp);
825 break;
826 }
827
Gavin Shanbbc7c012016-10-20 11:45:51 +1100828 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
829 hot_nc = nc;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100830 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100831 } else {
832 hot_nc = NULL;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100833 nc->state = NCSI_CHANNEL_INACTIVE;
Joel Stanley87975a02018-06-19 15:08:31 +0930834 netdev_dbg(ndp->ndev.dev,
835 "NCSI: channel %u link down after config\n",
836 nc->id);
Gavin Shanbbc7c012016-10-20 11:45:51 +1100837 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100838 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000839
Gavin Shanbbc7c012016-10-20 11:45:51 +1100840 /* Update the hot channel */
841 spin_lock_irqsave(&ndp->lock, flags);
842 ndp->hot_channel = hot_nc;
843 spin_unlock_irqrestore(&ndp->lock, flags);
844
Gavin Shane6f44ed2016-07-19 11:54:19 +1000845 ncsi_start_channel_monitor(nc);
846 ncsi_process_next_channel(ndp);
847 break;
848 default:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100849 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
850 nd->state);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000851 }
852
853 return;
854
855error:
856 ncsi_report_link(ndp, true);
857}
858
859static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
860{
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100861 struct ncsi_package *np, *force_package;
862 struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000863 struct ncsi_channel_mode *ncm;
864 unsigned long flags;
865
Gavin Shanbbc7c012016-10-20 11:45:51 +1100866 spin_lock_irqsave(&ndp->lock, flags);
867 hot_nc = ndp->hot_channel;
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100868 force_channel = ndp->force_channel;
869 force_package = ndp->force_package;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100870 spin_unlock_irqrestore(&ndp->lock, flags);
871
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100872 /* Force a specific channel whether or not it has link if we have been
873 * configured to do so
874 */
875 if (force_package && force_channel) {
876 found = force_channel;
877 ncm = &found->modes[NCSI_MODE_LINK];
878 if (!(ncm->data[2] & 0x1))
879 netdev_info(ndp->ndev.dev,
880 "NCSI: Channel %u forced, but it is link down\n",
881 found->id);
882 goto out;
883 }
884
Gavin Shane6f44ed2016-07-19 11:54:19 +1000885 /* The search is done once an inactive channel with up
886 * link is found.
887 */
888 found = NULL;
889 NCSI_FOR_EACH_PACKAGE(ndp, np) {
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100890 if (ndp->force_package && np != ndp->force_package)
891 continue;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000892 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100893 spin_lock_irqsave(&nc->lock, flags);
894
Gavin Shane6f44ed2016-07-19 11:54:19 +1000895 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +1100896 nc->state != NCSI_CHANNEL_INACTIVE) {
897 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000898 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100899 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000900
901 if (!found)
902 found = nc;
903
Gavin Shanbbc7c012016-10-20 11:45:51 +1100904 if (nc == hot_nc)
905 found = nc;
906
Gavin Shane6f44ed2016-07-19 11:54:19 +1000907 ncm = &nc->modes[NCSI_MODE_LINK];
908 if (ncm->data[2] & 0x1) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100909 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000910 found = nc;
911 goto out;
912 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100913
914 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000915 }
916 }
917
918 if (!found) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100919 netdev_warn(ndp->ndev.dev,
920 "NCSI: No channel found with link\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000921 ncsi_report_link(ndp, true);
922 return -ENODEV;
923 }
924
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100925 ncm = &found->modes[NCSI_MODE_LINK];
Joel Stanley6e42a3f2018-06-19 15:08:33 +0930926 netdev_dbg(ndp->ndev.dev,
927 "NCSI: Channel %u added to queue (link %s)\n",
928 found->id, ncm->data[2] & 0x1 ? "up" : "down");
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100929
Gavin Shane6f44ed2016-07-19 11:54:19 +1000930out:
931 spin_lock_irqsave(&ndp->lock, flags);
932 list_add_tail_rcu(&found->link, &ndp->channel_queue);
933 spin_unlock_irqrestore(&ndp->lock, flags);
934
935 return ncsi_process_next_channel(ndp);
936}
937
938static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
939{
940 struct ncsi_package *np;
941 struct ncsi_channel *nc;
942 unsigned int cap;
Gavin Shan100ef012017-10-19 13:43:07 +1100943 bool has_channel = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000944
945 /* The hardware arbitration is disabled if any one channel
946 * doesn't support explicitly.
947 */
948 NCSI_FOR_EACH_PACKAGE(ndp, np) {
949 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shan100ef012017-10-19 13:43:07 +1100950 has_channel = true;
951
Gavin Shane6f44ed2016-07-19 11:54:19 +1000952 cap = nc->caps[NCSI_CAP_GENERIC].cap;
953 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
954 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
955 NCSI_CAP_GENERIC_HWA_SUPPORT) {
956 ndp->flags &= ~NCSI_DEV_HWA;
957 return false;
958 }
959 }
960 }
961
Gavin Shan100ef012017-10-19 13:43:07 +1100962 if (has_channel) {
963 ndp->flags |= NCSI_DEV_HWA;
964 return true;
965 }
966
967 ndp->flags &= ~NCSI_DEV_HWA;
968 return false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000969}
970
971static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
972{
973 struct ncsi_package *np;
974 struct ncsi_channel *nc;
975 unsigned long flags;
976
977 /* Move all available channels to processing queue */
978 spin_lock_irqsave(&ndp->lock, flags);
979 NCSI_FOR_EACH_PACKAGE(ndp, np) {
980 NCSI_FOR_EACH_CHANNEL(np, nc) {
981 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
982 !list_empty(&nc->link));
983 ncsi_stop_channel_monitor(nc);
984 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
985 }
986 }
987 spin_unlock_irqrestore(&ndp->lock, flags);
988
989 /* We can have no channels in extremely case */
990 if (list_empty(&ndp->channel_queue)) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100991 netdev_err(ndp->ndev.dev,
992 "NCSI: No available channels for HWA\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000993 ncsi_report_link(ndp, false);
994 return -ENOENT;
995 }
996
997 return ncsi_process_next_channel(ndp);
998}
999
1000static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1001{
1002 struct ncsi_dev *nd = &ndp->ndev;
1003 struct ncsi_package *np;
1004 struct ncsi_channel *nc;
1005 struct ncsi_cmd_arg nca;
1006 unsigned char index;
1007 int ret;
1008
1009 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001010 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001011 switch (nd->state) {
1012 case ncsi_dev_state_probe:
1013 nd->state = ncsi_dev_state_probe_deselect;
1014 /* Fall through */
1015 case ncsi_dev_state_probe_deselect:
1016 ndp->pending_req_num = 8;
1017
1018 /* Deselect all possible packages */
1019 nca.type = NCSI_PKT_CMD_DP;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001020 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001021 for (index = 0; index < 8; index++) {
1022 nca.package = index;
1023 ret = ncsi_xmit_cmd(&nca);
1024 if (ret)
1025 goto error;
1026 }
1027
1028 nd->state = ncsi_dev_state_probe_package;
1029 break;
1030 case ncsi_dev_state_probe_package:
1031 ndp->pending_req_num = 16;
1032
1033 /* Select all possible packages */
1034 nca.type = NCSI_PKT_CMD_SP;
1035 nca.bytes[0] = 1;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001036 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001037 for (index = 0; index < 8; index++) {
1038 nca.package = index;
1039 ret = ncsi_xmit_cmd(&nca);
1040 if (ret)
1041 goto error;
1042 }
1043
1044 /* Disable all possible packages */
1045 nca.type = NCSI_PKT_CMD_DP;
1046 for (index = 0; index < 8; index++) {
1047 nca.package = index;
1048 ret = ncsi_xmit_cmd(&nca);
1049 if (ret)
1050 goto error;
1051 }
1052
1053 nd->state = ncsi_dev_state_probe_channel;
1054 break;
1055 case ncsi_dev_state_probe_channel:
1056 if (!ndp->active_package)
1057 ndp->active_package = list_first_or_null_rcu(
1058 &ndp->packages, struct ncsi_package, node);
1059 else if (list_is_last(&ndp->active_package->node,
1060 &ndp->packages))
1061 ndp->active_package = NULL;
1062 else
1063 ndp->active_package = list_next_entry(
1064 ndp->active_package, node);
1065
1066 /* All available packages and channels are enumerated. The
1067 * enumeration happens for once when the NCSI interface is
1068 * started. So we need continue to start the interface after
1069 * the enumeration.
1070 *
1071 * We have to choose an active channel before configuring it.
1072 * Note that we possibly don't have active channel in extreme
1073 * situation.
1074 */
1075 if (!ndp->active_package) {
1076 ndp->flags |= NCSI_DEV_PROBED;
1077 if (ncsi_check_hwa(ndp))
1078 ncsi_enable_hwa(ndp);
1079 else
1080 ncsi_choose_active_channel(ndp);
1081 return;
1082 }
1083
1084 /* Select the active package */
1085 ndp->pending_req_num = 1;
1086 nca.type = NCSI_PKT_CMD_SP;
1087 nca.bytes[0] = 1;
1088 nca.package = ndp->active_package->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001089 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001090 ret = ncsi_xmit_cmd(&nca);
1091 if (ret)
1092 goto error;
1093
1094 nd->state = ncsi_dev_state_probe_cis;
1095 break;
1096 case ncsi_dev_state_probe_cis:
Gavin Shan55e02d02016-10-04 11:25:49 +11001097 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001098
1099 /* Clear initial state */
1100 nca.type = NCSI_PKT_CMD_CIS;
1101 nca.package = ndp->active_package->id;
Gavin Shan55e02d02016-10-04 11:25:49 +11001102 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
Gavin Shane6f44ed2016-07-19 11:54:19 +10001103 nca.channel = index;
1104 ret = ncsi_xmit_cmd(&nca);
1105 if (ret)
1106 goto error;
1107 }
1108
1109 nd->state = ncsi_dev_state_probe_gvi;
1110 break;
1111 case ncsi_dev_state_probe_gvi:
1112 case ncsi_dev_state_probe_gc:
1113 case ncsi_dev_state_probe_gls:
1114 np = ndp->active_package;
1115 ndp->pending_req_num = np->channel_num;
1116
1117 /* Retrieve version, capability or link status */
1118 if (nd->state == ncsi_dev_state_probe_gvi)
1119 nca.type = NCSI_PKT_CMD_GVI;
1120 else if (nd->state == ncsi_dev_state_probe_gc)
1121 nca.type = NCSI_PKT_CMD_GC;
1122 else
1123 nca.type = NCSI_PKT_CMD_GLS;
1124
1125 nca.package = np->id;
1126 NCSI_FOR_EACH_CHANNEL(np, nc) {
1127 nca.channel = nc->id;
1128 ret = ncsi_xmit_cmd(&nca);
1129 if (ret)
1130 goto error;
1131 }
1132
1133 if (nd->state == ncsi_dev_state_probe_gvi)
1134 nd->state = ncsi_dev_state_probe_gc;
1135 else if (nd->state == ncsi_dev_state_probe_gc)
1136 nd->state = ncsi_dev_state_probe_gls;
1137 else
1138 nd->state = ncsi_dev_state_probe_dp;
1139 break;
1140 case ncsi_dev_state_probe_dp:
1141 ndp->pending_req_num = 1;
1142
1143 /* Deselect the active package */
1144 nca.type = NCSI_PKT_CMD_DP;
1145 nca.package = ndp->active_package->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001146 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001147 ret = ncsi_xmit_cmd(&nca);
1148 if (ret)
1149 goto error;
1150
1151 /* Scan channels in next package */
1152 nd->state = ncsi_dev_state_probe_channel;
1153 break;
1154 default:
1155 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1156 nd->state);
1157 }
1158
1159 return;
1160error:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001161 netdev_err(ndp->ndev.dev,
1162 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1163 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001164 ncsi_report_link(ndp, true);
1165}
1166
1167static void ncsi_dev_work(struct work_struct *work)
1168{
1169 struct ncsi_dev_priv *ndp = container_of(work,
1170 struct ncsi_dev_priv, work);
1171 struct ncsi_dev *nd = &ndp->ndev;
1172
1173 switch (nd->state & ncsi_dev_state_major) {
1174 case ncsi_dev_state_probe:
1175 ncsi_probe_channel(ndp);
1176 break;
1177 case ncsi_dev_state_suspend:
1178 ncsi_suspend_channel(ndp);
1179 break;
1180 case ncsi_dev_state_config:
1181 ncsi_configure_channel(ndp);
1182 break;
1183 default:
1184 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1185 nd->state);
1186 }
1187}
1188
1189int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1190{
1191 struct ncsi_channel *nc;
1192 int old_state;
1193 unsigned long flags;
1194
1195 spin_lock_irqsave(&ndp->lock, flags);
1196 nc = list_first_or_null_rcu(&ndp->channel_queue,
1197 struct ncsi_channel, link);
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001198 if (!nc) {
1199 spin_unlock_irqrestore(&ndp->lock, flags);
1200 goto out;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001201 }
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001202
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001203 list_del_init(&nc->link);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001204 spin_unlock_irqrestore(&ndp->lock, flags);
1205
Gavin Shand8cedaa2016-10-04 11:25:47 +11001206 spin_lock_irqsave(&nc->lock, flags);
1207 old_state = nc->state;
1208 nc->state = NCSI_CHANNEL_INVISIBLE;
1209 spin_unlock_irqrestore(&nc->lock, flags);
1210
Gavin Shane6f44ed2016-07-19 11:54:19 +10001211 ndp->active_channel = nc;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001212 ndp->active_package = nc->package;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001213
1214 switch (old_state) {
1215 case NCSI_CHANNEL_INACTIVE:
1216 ndp->ndev.state = ncsi_dev_state_config;
Joel Stanley87975a02018-06-19 15:08:31 +09301217 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1218 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001219 ncsi_configure_channel(ndp);
1220 break;
1221 case NCSI_CHANNEL_ACTIVE:
1222 ndp->ndev.state = ncsi_dev_state_suspend;
Joel Stanley87975a02018-06-19 15:08:31 +09301223 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1224 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001225 ncsi_suspend_channel(ndp);
1226 break;
1227 default:
1228 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
Gavin Shand8cedaa2016-10-04 11:25:47 +11001229 old_state, nc->package->id, nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001230 ncsi_report_link(ndp, false);
1231 return -EINVAL;
1232 }
1233
1234 return 0;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001235
1236out:
1237 ndp->active_channel = NULL;
1238 ndp->active_package = NULL;
1239 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1240 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1241 return ncsi_choose_active_channel(ndp);
1242 }
1243
1244 ncsi_report_link(ndp, false);
1245 return -ENODEV;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001246}
1247
1248#if IS_ENABLED(CONFIG_IPV6)
1249static int ncsi_inet6addr_event(struct notifier_block *this,
1250 unsigned long event, void *data)
1251{
1252 struct inet6_ifaddr *ifa = data;
1253 struct net_device *dev = ifa->idev->dev;
1254 struct ncsi_dev *nd = ncsi_find_dev(dev);
1255 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1256 struct ncsi_package *np;
1257 struct ncsi_channel *nc;
1258 struct ncsi_cmd_arg nca;
1259 bool action;
1260 int ret;
1261
1262 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1263 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1264 return NOTIFY_OK;
1265
1266 switch (event) {
1267 case NETDEV_UP:
1268 action = (++ndp->inet6_addr_num) == 1;
1269 nca.type = NCSI_PKT_CMD_EGMF;
1270 break;
1271 case NETDEV_DOWN:
1272 action = (--ndp->inet6_addr_num == 0);
1273 nca.type = NCSI_PKT_CMD_DGMF;
1274 break;
1275 default:
1276 return NOTIFY_OK;
1277 }
1278
1279 /* We might not have active channel or packages. The IPv6
1280 * required multicast will be enabled when active channel
1281 * or packages are chosen.
1282 */
1283 np = ndp->active_package;
1284 nc = ndp->active_channel;
1285 if (!action || !np || !nc)
1286 return NOTIFY_OK;
1287
1288 /* We needn't enable or disable it if the function isn't supported */
1289 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1290 return NOTIFY_OK;
1291
1292 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001293 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001294 nca.package = np->id;
1295 nca.channel = nc->id;
1296 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1297 ret = ncsi_xmit_cmd(&nca);
1298 if (ret) {
1299 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1300 (event == NETDEV_UP) ? "enable" : "disable", ret);
1301 return NOTIFY_DONE;
1302 }
1303
1304 return NOTIFY_OK;
1305}
1306
1307static struct notifier_block ncsi_inet6addr_notifier = {
1308 .notifier_call = ncsi_inet6addr_event,
1309};
1310#endif /* CONFIG_IPV6 */
1311
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001312static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1313{
1314 struct ncsi_dev *nd = &ndp->ndev;
1315 struct ncsi_channel *nc;
1316 struct ncsi_package *np;
1317 unsigned long flags;
1318 unsigned int n = 0;
1319
1320 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1321 NCSI_FOR_EACH_CHANNEL(np, nc) {
1322 spin_lock_irqsave(&nc->lock, flags);
1323
1324 /* Channels may be busy, mark dirty instead of
1325 * kicking if;
1326 * a) not ACTIVE (configured)
1327 * b) in the channel_queue (to be configured)
1328 * c) it's ndev is in the config state
1329 */
1330 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1331 if ((ndp->ndev.state & 0xff00) ==
1332 ncsi_dev_state_config ||
1333 !list_empty(&nc->link)) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301334 netdev_dbg(nd->dev,
1335 "NCSI: channel %p marked dirty\n",
1336 nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001337 nc->reconfigure_needed = true;
1338 }
1339 spin_unlock_irqrestore(&nc->lock, flags);
1340 continue;
1341 }
1342
1343 spin_unlock_irqrestore(&nc->lock, flags);
1344
1345 ncsi_stop_channel_monitor(nc);
1346 spin_lock_irqsave(&nc->lock, flags);
1347 nc->state = NCSI_CHANNEL_INACTIVE;
1348 spin_unlock_irqrestore(&nc->lock, flags);
1349
1350 spin_lock_irqsave(&ndp->lock, flags);
1351 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1352 spin_unlock_irqrestore(&ndp->lock, flags);
1353
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301354 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001355 n++;
1356 }
1357 }
1358
1359 return n;
1360}
1361
1362int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1363{
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001364 struct ncsi_dev_priv *ndp;
1365 unsigned int n_vids = 0;
1366 struct vlan_vid *vlan;
1367 struct ncsi_dev *nd;
1368 bool found = false;
1369
1370 if (vid == 0)
1371 return 0;
1372
1373 nd = ncsi_find_dev(dev);
1374 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001375 netdev_warn(dev, "NCSI: No net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001376 return 0;
1377 }
1378
1379 ndp = TO_NCSI_DEV_PRIV(nd);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001380
1381 /* Add the VLAN id to our internal list */
1382 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1383 n_vids++;
1384 if (vlan->vid == vid) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301385 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1386 vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001387 return 0;
1388 }
1389 }
Samuel Mendoza-Jonas6e9c0072017-10-11 16:54:27 +11001390 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1391 netdev_warn(dev,
1392 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1393 vid, NCSI_MAX_VLAN_VIDS);
1394 return -ENOSPC;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001395 }
1396
1397 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1398 if (!vlan)
1399 return -ENOMEM;
1400
1401 vlan->proto = proto;
1402 vlan->vid = vid;
1403 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1404
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301405 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001406
1407 found = ncsi_kick_channels(ndp) != 0;
1408
1409 return found ? ncsi_process_next_channel(ndp) : 0;
1410}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001411EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001412
1413int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1414{
1415 struct vlan_vid *vlan, *tmp;
1416 struct ncsi_dev_priv *ndp;
1417 struct ncsi_dev *nd;
1418 bool found = false;
1419
1420 if (vid == 0)
1421 return 0;
1422
1423 nd = ncsi_find_dev(dev);
1424 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001425 netdev_warn(dev, "NCSI: no net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001426 return 0;
1427 }
1428
1429 ndp = TO_NCSI_DEV_PRIV(nd);
1430
1431 /* Remove the VLAN id from our internal list */
1432 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1433 if (vlan->vid == vid) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301434 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001435 list_del_rcu(&vlan->list);
1436 found = true;
1437 kfree(vlan);
1438 }
1439
1440 if (!found) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001441 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001442 return -EINVAL;
1443 }
1444
1445 found = ncsi_kick_channels(ndp) != 0;
1446
1447 return found ? ncsi_process_next_channel(ndp) : 0;
1448}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001449EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001450
Gavin Shan2d283bd2016-07-19 11:54:16 +10001451struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1452 void (*handler)(struct ncsi_dev *ndev))
1453{
1454 struct ncsi_dev_priv *ndp;
1455 struct ncsi_dev *nd;
1456 unsigned long flags;
1457 int i;
1458
1459 /* Check if the device has been registered or not */
1460 nd = ncsi_find_dev(dev);
1461 if (nd)
1462 return nd;
1463
1464 /* Create NCSI device */
1465 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1466 if (!ndp)
1467 return NULL;
1468
1469 nd = &ndp->ndev;
1470 nd->state = ncsi_dev_state_registered;
1471 nd->dev = dev;
1472 nd->handler = handler;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001473 ndp->pending_req_num = 0;
1474 INIT_LIST_HEAD(&ndp->channel_queue);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001475 INIT_LIST_HEAD(&ndp->vlan_vids);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001476 INIT_WORK(&ndp->work, ncsi_dev_work);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001477
1478 /* Initialize private NCSI device */
1479 spin_lock_init(&ndp->lock);
1480 INIT_LIST_HEAD(&ndp->packages);
Gavin Shana15af542016-10-04 11:25:50 +11001481 ndp->request_id = NCSI_REQ_START_IDX;
Gavin Shan2d283bd2016-07-19 11:54:16 +10001482 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1483 ndp->requests[i].id = i;
1484 ndp->requests[i].ndp = ndp;
Kees Cooke99e88a2017-10-16 14:43:17 -07001485 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001486 }
1487
1488 spin_lock_irqsave(&ncsi_dev_lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001489#if IS_ENABLED(CONFIG_IPV6)
1490 ndp->inet6_addr_num = 0;
1491 if (list_empty(&ncsi_dev_list))
1492 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1493#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001494 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1495 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1496
Gavin Shane6f44ed2016-07-19 11:54:19 +10001497 /* Register NCSI packet Rx handler */
1498 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1499 ndp->ptype.func = ncsi_rcv_rsp;
1500 ndp->ptype.dev = dev;
1501 dev_add_pack(&ndp->ptype);
1502
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001503 /* Set up generic netlink interface */
1504 ncsi_init_netlink(dev);
1505
Gavin Shan2d283bd2016-07-19 11:54:16 +10001506 return nd;
1507}
1508EXPORT_SYMBOL_GPL(ncsi_register_dev);
1509
Gavin Shane6f44ed2016-07-19 11:54:19 +10001510int ncsi_start_dev(struct ncsi_dev *nd)
1511{
1512 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001513 int ret;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001514
1515 if (nd->state != ncsi_dev_state_registered &&
1516 nd->state != ncsi_dev_state_functional)
1517 return -ENOTTY;
1518
1519 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1520 nd->state = ncsi_dev_state_probe;
1521 schedule_work(&ndp->work);
1522 return 0;
1523 }
1524
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001525 if (ndp->flags & NCSI_DEV_HWA) {
1526 netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001527 ret = ncsi_enable_hwa(ndp);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001528 } else {
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001529 ret = ncsi_choose_active_channel(ndp);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001530 }
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001531
1532 return ret;
1533}
1534EXPORT_SYMBOL_GPL(ncsi_start_dev);
1535
1536void ncsi_stop_dev(struct ncsi_dev *nd)
1537{
1538 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1539 struct ncsi_package *np;
1540 struct ncsi_channel *nc;
1541 bool chained;
1542 int old_state;
1543 unsigned long flags;
1544
1545 /* Stop the channel monitor and reset channel's state */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001546 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1547 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001548 ncsi_stop_channel_monitor(nc);
1549
Gavin Shand8cedaa2016-10-04 11:25:47 +11001550 spin_lock_irqsave(&nc->lock, flags);
1551 chained = !list_empty(&nc->link);
1552 old_state = nc->state;
1553 nc->state = NCSI_CHANNEL_INACTIVE;
1554 spin_unlock_irqrestore(&nc->lock, flags);
1555
1556 WARN_ON_ONCE(chained ||
Gavin Shane6f44ed2016-07-19 11:54:19 +10001557 old_state == NCSI_CHANNEL_INVISIBLE);
1558 }
1559 }
1560
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301561 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001562 ncsi_report_link(ndp, true);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001563}
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001564EXPORT_SYMBOL_GPL(ncsi_stop_dev);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001565
Gavin Shan2d283bd2016-07-19 11:54:16 +10001566void ncsi_unregister_dev(struct ncsi_dev *nd)
1567{
1568 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1569 struct ncsi_package *np, *tmp;
1570 unsigned long flags;
1571
Gavin Shane6f44ed2016-07-19 11:54:19 +10001572 dev_remove_pack(&ndp->ptype);
1573
Gavin Shan2d283bd2016-07-19 11:54:16 +10001574 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1575 ncsi_remove_package(np);
1576
1577 spin_lock_irqsave(&ncsi_dev_lock, flags);
1578 list_del_rcu(&ndp->node);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001579#if IS_ENABLED(CONFIG_IPV6)
1580 if (list_empty(&ncsi_dev_list))
1581 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1582#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001583 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1584
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001585 ncsi_unregister_netlink(nd->dev);
1586
Gavin Shan2d283bd2016-07-19 11:54:16 +10001587 kfree(ndp);
1588}
1589EXPORT_SYMBOL_GPL(ncsi_unregister_dev);