blob: b8b4e765a04c22308c9727f87d3cead98ca6b8d5 [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);
Samuel Mendoza-Jonas60ab49b2018-11-16 15:51:54 +1100116 ncsi_report_link(ndp, true);
117 ndp->flags |= NCSI_DEV_RESHUFFLE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000118
Samuel Mendoza-Jonas0795fb22017-10-19 13:43:06 +1100119 ncsi_stop_channel_monitor(nc);
120
Gavin Shan52b4c862017-10-19 13:43:08 +1100121 ncm = &nc->modes[NCSI_MODE_LINK];
Gavin Shand8cedaa2016-10-04 11:25:47 +1100122 spin_lock_irqsave(&nc->lock, flags);
123 nc->state = NCSI_CHANNEL_INVISIBLE;
Gavin Shan52b4c862017-10-19 13:43:08 +1100124 ncm->data[2] &= ~0x1;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100125 spin_unlock_irqrestore(&nc->lock, flags);
126
Gavin Shane6f44ed2016-07-19 11:54:19 +1000127 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shan52b4c862017-10-19 13:43:08 +1100128 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000129 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
130 spin_unlock_irqrestore(&ndp->lock, flags);
131 ncsi_process_next_channel(ndp);
132 return;
133 }
134
135 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100136 nc->monitor.state++;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000137 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100138 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000139}
140
141void ncsi_start_channel_monitor(struct ncsi_channel *nc)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100146 WARN_ON_ONCE(nc->monitor.enabled);
147 nc->monitor.enabled = true;
148 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000149 spin_unlock_irqrestore(&nc->lock, flags);
150
Gavin Shan83afdc62016-10-04 11:25:52 +1100151 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000152}
153
154void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
155{
156 unsigned long flags;
157
158 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100159 if (!nc->monitor.enabled) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000160 spin_unlock_irqrestore(&nc->lock, flags);
161 return;
162 }
Gavin Shan83afdc62016-10-04 11:25:52 +1100163 nc->monitor.enabled = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000164 spin_unlock_irqrestore(&nc->lock, flags);
165
Gavin Shan83afdc62016-10-04 11:25:52 +1100166 del_timer_sync(&nc->monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000167}
168
Gavin Shan2d283bd2016-07-19 11:54:16 +1000169struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
170 unsigned char id)
171{
172 struct ncsi_channel *nc;
173
174 NCSI_FOR_EACH_CHANNEL(np, nc) {
175 if (nc->id == id)
176 return nc;
177 }
178
179 return NULL;
180}
181
182struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
183{
184 struct ncsi_channel *nc, *tmp;
185 int index;
186 unsigned long flags;
187
188 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
189 if (!nc)
190 return NULL;
191
192 nc->id = id;
193 nc->package = np;
194 nc->state = NCSI_CHANNEL_INACTIVE;
Gavin Shan83afdc62016-10-04 11:25:52 +1100195 nc->monitor.enabled = false;
Kees Cook86cb30e2017-10-17 20:21:24 -0700196 timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000197 spin_lock_init(&nc->lock);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000198 INIT_LIST_HEAD(&nc->link);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000199 for (index = 0; index < NCSI_CAP_MAX; index++)
200 nc->caps[index].index = index;
201 for (index = 0; index < NCSI_MODE_MAX; index++)
202 nc->modes[index].index = index;
203
204 spin_lock_irqsave(&np->lock, flags);
205 tmp = ncsi_find_channel(np, id);
206 if (tmp) {
207 spin_unlock_irqrestore(&np->lock, flags);
208 kfree(nc);
209 return tmp;
210 }
211
212 list_add_tail_rcu(&nc->node, &np->channels);
213 np->channel_num++;
214 spin_unlock_irqrestore(&np->lock, flags);
215
216 return nc;
217}
218
219static void ncsi_remove_channel(struct ncsi_channel *nc)
220{
221 struct ncsi_package *np = nc->package;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000222 unsigned long flags;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000223
224 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000225
226 /* Release filters */
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000227 kfree(nc->mac_filter.addrs);
228 kfree(nc->vlan_filter.vids);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000229
230 nc->state = NCSI_CHANNEL_INACTIVE;
231 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000232 ncsi_stop_channel_monitor(nc);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000233
234 /* Remove and free channel */
235 spin_lock_irqsave(&np->lock, flags);
236 list_del_rcu(&nc->node);
237 np->channel_num--;
238 spin_unlock_irqrestore(&np->lock, flags);
239
240 kfree(nc);
241}
242
243struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
244 unsigned char id)
245{
246 struct ncsi_package *np;
247
248 NCSI_FOR_EACH_PACKAGE(ndp, np) {
249 if (np->id == id)
250 return np;
251 }
252
253 return NULL;
254}
255
256struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
257 unsigned char id)
258{
259 struct ncsi_package *np, *tmp;
260 unsigned long flags;
261
262 np = kzalloc(sizeof(*np), GFP_ATOMIC);
263 if (!np)
264 return NULL;
265
266 np->id = id;
267 np->ndp = ndp;
268 spin_lock_init(&np->lock);
269 INIT_LIST_HEAD(&np->channels);
270
271 spin_lock_irqsave(&ndp->lock, flags);
272 tmp = ncsi_find_package(ndp, id);
273 if (tmp) {
274 spin_unlock_irqrestore(&ndp->lock, flags);
275 kfree(np);
276 return tmp;
277 }
278
279 list_add_tail_rcu(&np->node, &ndp->packages);
280 ndp->package_num++;
281 spin_unlock_irqrestore(&ndp->lock, flags);
282
283 return np;
284}
285
286void ncsi_remove_package(struct ncsi_package *np)
287{
288 struct ncsi_dev_priv *ndp = np->ndp;
289 struct ncsi_channel *nc, *tmp;
290 unsigned long flags;
291
292 /* Release all child channels */
293 list_for_each_entry_safe(nc, tmp, &np->channels, node)
294 ncsi_remove_channel(nc);
295
296 /* Remove and free package */
297 spin_lock_irqsave(&ndp->lock, flags);
298 list_del_rcu(&np->node);
299 ndp->package_num--;
300 spin_unlock_irqrestore(&ndp->lock, flags);
301
302 kfree(np);
303}
304
305void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
306 unsigned char id,
307 struct ncsi_package **np,
308 struct ncsi_channel **nc)
309{
310 struct ncsi_package *p;
311 struct ncsi_channel *c;
312
313 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
314 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
315
316 if (np)
317 *np = p;
318 if (nc)
319 *nc = c;
320}
321
322/* For two consecutive NCSI commands, the packet IDs shouldn't
323 * be same. Otherwise, the bogus response might be replied. So
324 * the available IDs are allocated in round-robin fashion.
325 */
Gavin Shana0509cb2016-10-04 11:25:51 +1100326struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
327 unsigned int req_flags)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000328{
329 struct ncsi_request *nr = NULL;
330 int i, limit = ARRAY_SIZE(ndp->requests);
331 unsigned long flags;
332
333 /* Check if there is one available request until the ceiling */
334 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shana15af542016-10-04 11:25:50 +1100335 for (i = ndp->request_id; i < limit; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000336 if (ndp->requests[i].used)
337 continue;
338
339 nr = &ndp->requests[i];
340 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100341 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100342 ndp->request_id = i + 1;
343 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000344 }
345
346 /* Fail back to check from the starting cursor */
Gavin Shana15af542016-10-04 11:25:50 +1100347 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000348 if (ndp->requests[i].used)
349 continue;
350
351 nr = &ndp->requests[i];
352 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100353 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100354 ndp->request_id = i + 1;
355 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000356 }
Gavin Shan2d283bd2016-07-19 11:54:16 +1000357
Gavin Shana15af542016-10-04 11:25:50 +1100358found:
359 spin_unlock_irqrestore(&ndp->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000360 return nr;
361}
362
363void ncsi_free_request(struct ncsi_request *nr)
364{
365 struct ncsi_dev_priv *ndp = nr->ndp;
366 struct sk_buff *cmd, *rsp;
367 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000368 bool driven;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000369
370 if (nr->enabled) {
371 nr->enabled = false;
372 del_timer_sync(&nr->timer);
373 }
374
375 spin_lock_irqsave(&ndp->lock, flags);
376 cmd = nr->cmd;
377 rsp = nr->rsp;
378 nr->cmd = NULL;
379 nr->rsp = NULL;
380 nr->used = false;
Gavin Shana0509cb2016-10-04 11:25:51 +1100381 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000382 spin_unlock_irqrestore(&ndp->lock, flags);
383
Gavin Shane6f44ed2016-07-19 11:54:19 +1000384 if (driven && cmd && --ndp->pending_req_num == 0)
385 schedule_work(&ndp->work);
386
Gavin Shan2d283bd2016-07-19 11:54:16 +1000387 /* Release command and response */
388 consume_skb(cmd);
389 consume_skb(rsp);
390}
391
392struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
393{
394 struct ncsi_dev_priv *ndp;
395
396 NCSI_FOR_EACH_DEV(ndp) {
397 if (ndp->ndev.dev == dev)
398 return &ndp->ndev;
399 }
400
401 return NULL;
402}
403
Kees Cooke99e88a2017-10-16 14:43:17 -0700404static void ncsi_request_timeout(struct timer_list *t)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000405{
Kees Cooke99e88a2017-10-16 14:43:17 -0700406 struct ncsi_request *nr = from_timer(nr, t, timer);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000407 struct ncsi_dev_priv *ndp = nr->ndp;
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000408 struct ncsi_cmd_pkt *cmd;
409 struct ncsi_package *np;
410 struct ncsi_channel *nc;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000411 unsigned long flags;
412
413 /* If the request already had associated response,
414 * let the response handler to release it.
415 */
416 spin_lock_irqsave(&ndp->lock, flags);
417 nr->enabled = false;
418 if (nr->rsp || !nr->cmd) {
419 spin_unlock_irqrestore(&ndp->lock, flags);
420 return;
421 }
422 spin_unlock_irqrestore(&ndp->lock, flags);
423
Justin.Lee1@Dell.com9771b8c2018-10-11 18:07:37 +0000424 if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
425 if (nr->cmd) {
426 /* Find the package */
427 cmd = (struct ncsi_cmd_pkt *)
428 skb_network_header(nr->cmd);
429 ncsi_find_package_and_channel(ndp,
430 cmd->cmd.common.channel,
431 &np, &nc);
432 ncsi_send_netlink_timeout(nr, np, nc);
433 }
434 }
435
Gavin Shan2d283bd2016-07-19 11:54:16 +1000436 /* Release the request */
437 ncsi_free_request(nr);
438}
439
Gavin Shane6f44ed2016-07-19 11:54:19 +1000440static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
441{
442 struct ncsi_dev *nd = &ndp->ndev;
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100443 struct ncsi_package *np;
444 struct ncsi_channel *nc, *tmp;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000445 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100446 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000447 int ret;
448
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100449 np = ndp->active_package;
450 nc = ndp->active_channel;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000451 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
Samuel Mendoza-Jonascd09ab02018-11-16 15:51:56 +1100526 NCSI_FOR_EACH_CHANNEL(np, tmp) {
527 /* If there is another channel active on this package
528 * do not deselect the package.
529 */
530 if (tmp != nc && tmp->state == NCSI_CHANNEL_ACTIVE) {
531 nd->state = ncsi_dev_state_suspend_done;
532 break;
533 }
534 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100535 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000536 case ncsi_dev_state_suspend_deselect:
537 ndp->pending_req_num = 1;
538
Gavin Shan7ba5c002016-10-20 11:45:49 +1100539 nca.type = NCSI_PKT_CMD_DP;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000540 nca.package = np->id;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100541 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000542
Gavin Shan7ba5c002016-10-20 11:45:49 +1100543 nd->state = ncsi_dev_state_suspend_done;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000544 ret = ncsi_xmit_cmd(&nca);
Gavin Shan7ba5c002016-10-20 11:45:49 +1100545 if (ret)
546 goto error;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000547
548 break;
549 case ncsi_dev_state_suspend_done:
Gavin Shand8cedaa2016-10-04 11:25:47 +1100550 spin_lock_irqsave(&nc->lock, flags);
551 nc->state = NCSI_CHANNEL_INACTIVE;
552 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000553 ncsi_process_next_channel(ndp);
554
555 break;
556 default:
557 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
558 nd->state);
559 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100560
561 return;
562error:
563 nd->state = ncsi_dev_state_functional;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000564}
565
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000566/* Check the VLAN filter bitmap for a set filter, and construct a
567 * "Set VLAN Filter - Disable" packet if found.
568 */
569static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
570 struct ncsi_cmd_arg *nca)
571{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000572 struct ncsi_channel_vlan_filter *ncf;
573 unsigned long flags;
574 void *bitmap;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000575 int index;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000576 u16 vid;
577
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000578 ncf = &nc->vlan_filter;
579 bitmap = &ncf->bitmap;
580
581 spin_lock_irqsave(&nc->lock, flags);
582 index = find_next_bit(bitmap, ncf->n_vids, 0);
583 if (index >= ncf->n_vids) {
584 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000585 return -1;
586 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000587 vid = ncf->vids[index];
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000588
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000589 clear_bit(index, bitmap);
590 ncf->vids[index] = 0;
591 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000592
593 nca->type = NCSI_PKT_CMD_SVF;
594 nca->words[1] = vid;
595 /* HW filter index starts at 1 */
596 nca->bytes[6] = index + 1;
597 nca->bytes[7] = 0x00;
598 return 0;
599}
600
601/* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
602 * packet.
603 */
604static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
605 struct ncsi_cmd_arg *nca)
606{
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000607 struct ncsi_channel_vlan_filter *ncf;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000608 struct vlan_vid *vlan = NULL;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000609 unsigned long flags;
610 int i, index;
611 void *bitmap;
612 u16 vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000613
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000614 if (list_empty(&ndp->vlan_vids))
615 return -1;
616
617 ncf = &nc->vlan_filter;
618 bitmap = &ncf->bitmap;
619
620 spin_lock_irqsave(&nc->lock, flags);
621
622 rcu_read_lock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000623 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000624 vid = vlan->vid;
625 for (i = 0; i < ncf->n_vids; i++)
626 if (ncf->vids[i] == vid) {
627 vid = 0;
628 break;
629 }
630 if (vid)
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000631 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000632 }
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000633 rcu_read_unlock();
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000634
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000635 if (!vid) {
636 /* No VLAN ID is not set */
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 index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
642 if (index < 0 || index >= ncf->n_vids) {
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000643 netdev_err(ndp->ndev.dev,
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000644 "Channel %u already has all VLAN filters set\n",
645 nc->id);
646 spin_unlock_irqrestore(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000647 return -1;
648 }
649
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000650 ncf->vids[index] = vid;
651 set_bit(index, bitmap);
652 spin_unlock_irqrestore(&nc->lock, flags);
653
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000654 nca->type = NCSI_PKT_CMD_SVF;
Samuel Mendoza-Jonas062b3e12018-04-17 14:23:23 +1000655 nca->words[1] = vid;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000656 /* HW filter index starts at 1 */
657 nca->bytes[6] = index + 1;
658 nca->bytes[7] = 0x01;
659
660 return 0;
661}
662
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700663#if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
664
665/* NCSI OEM Command APIs */
666static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
667{
668 unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
669 int ret = 0;
670
671 nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
672
673 memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN);
674 *(unsigned int *)data = ntohl(NCSI_OEM_MFR_BCM_ID);
675 data[5] = NCSI_OEM_BCM_CMD_GMA;
676
677 nca->data = data;
678
679 ret = ncsi_xmit_cmd(nca);
680 if (ret)
681 netdev_err(nca->ndp->ndev.dev,
682 "NCSI: Failed to transmit cmd 0x%x during configure\n",
683 nca->type);
684 return ret;
685}
686
687/* OEM Command handlers initialization */
688static struct ncsi_oem_gma_handler {
689 unsigned int mfr_id;
690 int (*handler)(struct ncsi_cmd_arg *nca);
691} ncsi_oem_gma_handlers[] = {
692 { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm }
693};
694
695static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
696{
697 struct ncsi_oem_gma_handler *nch = NULL;
698 int i;
699
700 /* This function should only be called once, return if flag set */
701 if (nca->ndp->gma_flag == 1)
702 return -1;
703
704 /* Find gma handler for given manufacturer id */
705 for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) {
706 if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) {
707 if (ncsi_oem_gma_handlers[i].handler)
708 nch = &ncsi_oem_gma_handlers[i];
709 break;
710 }
711 }
712
713 if (!nch) {
714 netdev_err(nca->ndp->ndev.dev,
715 "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
716 mf_id);
717 return -1;
718 }
719
720 /* Set the flag for GMA command which should only be called once */
721 nca->ndp->gma_flag = 1;
722
723 /* Get Mac address from NCSI device */
724 return nch->handler(nca);
725}
726
727#endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
728
Gavin Shane6f44ed2016-07-19 11:54:19 +1000729static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
730{
731 struct ncsi_dev *nd = &ndp->ndev;
732 struct net_device *dev = nd->dev;
733 struct ncsi_package *np = ndp->active_package;
734 struct ncsi_channel *nc = ndp->active_channel;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100735 struct ncsi_channel *hot_nc = NULL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000736 struct ncsi_cmd_arg nca;
737 unsigned char index;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100738 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000739 int ret;
740
741 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100742 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000743 switch (nd->state) {
744 case ncsi_dev_state_config:
745 case ncsi_dev_state_config_sp:
746 ndp->pending_req_num = 1;
747
748 /* Select the specific package */
749 nca.type = NCSI_PKT_CMD_SP;
750 if (ndp->flags & NCSI_DEV_HWA)
751 nca.bytes[0] = 0;
752 else
753 nca.bytes[0] = 1;
754 nca.package = np->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100755 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000756 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100757 if (ret) {
758 netdev_err(ndp->ndev.dev,
759 "NCSI: Failed to transmit CMD_SP\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000760 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100761 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000762
763 nd->state = ncsi_dev_state_config_cis;
764 break;
765 case ncsi_dev_state_config_cis:
766 ndp->pending_req_num = 1;
767
768 /* Clear initial state */
769 nca.type = NCSI_PKT_CMD_CIS;
770 nca.package = np->id;
771 nca.channel = nc->id;
772 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100773 if (ret) {
774 netdev_err(ndp->ndev.dev,
775 "NCSI: Failed to transmit CMD_CIS\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +1000776 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100777 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000778
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700779 nd->state = ncsi_dev_state_config_oem_gma;
780 break;
781 case ncsi_dev_state_config_oem_gma:
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000782 nd->state = ncsi_dev_state_config_clear_vids;
Vijay Khemkacb10c7c2018-10-16 12:13:19 -0700783 ret = -1;
784
785#if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
786 nca.type = NCSI_PKT_CMD_OEM;
787 nca.package = np->id;
788 nca.channel = nc->id;
789 ndp->pending_req_num = 1;
790 ret = ncsi_gma_handler(&nca, nc->version.mf_id);
791#endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
792
793 if (ret < 0)
794 schedule_work(&ndp->work);
795
Gavin Shane6f44ed2016-07-19 11:54:19 +1000796 break;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000797 case ncsi_dev_state_config_clear_vids:
798 case ncsi_dev_state_config_svf:
799 case ncsi_dev_state_config_ev:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000800 case ncsi_dev_state_config_sma:
801 case ncsi_dev_state_config_ebf:
802#if IS_ENABLED(CONFIG_IPV6)
803 case ncsi_dev_state_config_egmf:
804#endif
805 case ncsi_dev_state_config_ecnt:
806 case ncsi_dev_state_config_ec:
807 case ncsi_dev_state_config_ae:
808 case ncsi_dev_state_config_gls:
809 ndp->pending_req_num = 1;
810
811 nca.package = np->id;
812 nca.channel = nc->id;
813
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000814 /* Clear any active filters on the channel before setting */
815 if (nd->state == ncsi_dev_state_config_clear_vids) {
816 ret = clear_one_vid(ndp, nc, &nca);
817 if (ret) {
818 nd->state = ncsi_dev_state_config_svf;
819 schedule_work(&ndp->work);
820 break;
821 }
822 /* Repeat */
823 nd->state = ncsi_dev_state_config_clear_vids;
824 /* Add known VLAN tags to the filter */
825 } else if (nd->state == ncsi_dev_state_config_svf) {
826 ret = set_one_vid(ndp, nc, &nca);
827 if (ret) {
828 nd->state = ncsi_dev_state_config_ev;
829 schedule_work(&ndp->work);
830 break;
831 }
832 /* Repeat */
833 nd->state = ncsi_dev_state_config_svf;
834 /* Enable/Disable the VLAN filter */
835 } else if (nd->state == ncsi_dev_state_config_ev) {
836 if (list_empty(&ndp->vlan_vids)) {
837 nca.type = NCSI_PKT_CMD_DV;
838 } else {
839 nca.type = NCSI_PKT_CMD_EV;
840 nca.bytes[3] = NCSI_CAP_VLAN_NO;
841 }
842 nd->state = ncsi_dev_state_config_sma;
843 } else if (nd->state == ncsi_dev_state_config_sma) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000844 /* Use first entry in unicast filter table. Note that
845 * the MAC filter table starts from entry 1 instead of
846 * 0.
847 */
Gavin Shane6f44ed2016-07-19 11:54:19 +1000848 nca.type = NCSI_PKT_CMD_SMA;
849 for (index = 0; index < 6; index++)
850 nca.bytes[index] = dev->dev_addr[index];
851 nca.bytes[6] = 0x1;
852 nca.bytes[7] = 0x1;
853 nd->state = ncsi_dev_state_config_ebf;
854 } else if (nd->state == ncsi_dev_state_config_ebf) {
855 nca.type = NCSI_PKT_CMD_EBF;
856 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
857 nd->state = ncsi_dev_state_config_ecnt;
858#if IS_ENABLED(CONFIG_IPV6)
859 if (ndp->inet6_addr_num > 0 &&
860 (nc->caps[NCSI_CAP_GENERIC].cap &
861 NCSI_CAP_GENERIC_MC))
862 nd->state = ncsi_dev_state_config_egmf;
863 else
864 nd->state = ncsi_dev_state_config_ecnt;
865 } else if (nd->state == ncsi_dev_state_config_egmf) {
866 nca.type = NCSI_PKT_CMD_EGMF;
867 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
868 nd->state = ncsi_dev_state_config_ecnt;
869#endif /* CONFIG_IPV6 */
870 } else if (nd->state == ncsi_dev_state_config_ecnt) {
871 nca.type = NCSI_PKT_CMD_ECNT;
872 nd->state = ncsi_dev_state_config_ec;
873 } else if (nd->state == ncsi_dev_state_config_ec) {
874 /* Enable AEN if it's supported */
875 nca.type = NCSI_PKT_CMD_EC;
876 nd->state = ncsi_dev_state_config_ae;
877 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
878 nd->state = ncsi_dev_state_config_gls;
879 } else if (nd->state == ncsi_dev_state_config_ae) {
880 nca.type = NCSI_PKT_CMD_AE;
881 nca.bytes[0] = 0;
882 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
883 nd->state = ncsi_dev_state_config_gls;
884 } else if (nd->state == ncsi_dev_state_config_gls) {
885 nca.type = NCSI_PKT_CMD_GLS;
886 nd->state = ncsi_dev_state_config_done;
887 }
888
889 ret = ncsi_xmit_cmd(&nca);
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100890 if (ret) {
891 netdev_err(ndp->ndev.dev,
892 "NCSI: Failed to transmit CMD %x\n",
893 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000894 goto error;
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100895 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000896 break;
897 case ncsi_dev_state_config_done:
Joel Stanley6e42a3f2018-06-19 15:08:33 +0930898 netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
899 nc->id);
Gavin Shand8cedaa2016-10-04 11:25:47 +1100900 spin_lock_irqsave(&nc->lock, flags);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000901 if (nc->reconfigure_needed) {
902 /* This channel's configuration has been updated
903 * part-way during the config state - start the
904 * channel configuration over
905 */
906 nc->reconfigure_needed = false;
907 nc->state = NCSI_CHANNEL_INACTIVE;
908 spin_unlock_irqrestore(&nc->lock, flags);
909
910 spin_lock_irqsave(&ndp->lock, flags);
911 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
912 spin_unlock_irqrestore(&ndp->lock, flags);
913
Joel Stanley6e42a3f2018-06-19 15:08:33 +0930914 netdev_dbg(dev, "Dirty NCSI channel state reset\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +1000915 ncsi_process_next_channel(ndp);
916 break;
917 }
918
Gavin Shanbbc7c012016-10-20 11:45:51 +1100919 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
920 hot_nc = nc;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100921 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100922 } else {
923 hot_nc = NULL;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100924 nc->state = NCSI_CHANNEL_INACTIVE;
Joel Stanley87975a02018-06-19 15:08:31 +0930925 netdev_dbg(ndp->ndev.dev,
926 "NCSI: channel %u link down after config\n",
927 nc->id);
Gavin Shanbbc7c012016-10-20 11:45:51 +1100928 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100929 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000930
Gavin Shanbbc7c012016-10-20 11:45:51 +1100931 /* Update the hot channel */
932 spin_lock_irqsave(&ndp->lock, flags);
933 ndp->hot_channel = hot_nc;
934 spin_unlock_irqrestore(&ndp->lock, flags);
935
Gavin Shane6f44ed2016-07-19 11:54:19 +1000936 ncsi_start_channel_monitor(nc);
937 ncsi_process_next_channel(ndp);
938 break;
939 default:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +1100940 netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
941 nd->state);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000942 }
943
944 return;
945
946error:
947 ncsi_report_link(ndp, true);
948}
949
950static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
951{
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100952 struct ncsi_package *np, *force_package;
953 struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000954 struct ncsi_channel_mode *ncm;
955 unsigned long flags;
956
Gavin Shanbbc7c012016-10-20 11:45:51 +1100957 spin_lock_irqsave(&ndp->lock, flags);
958 hot_nc = ndp->hot_channel;
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100959 force_channel = ndp->force_channel;
960 force_package = ndp->force_package;
Gavin Shanbbc7c012016-10-20 11:45:51 +1100961 spin_unlock_irqrestore(&ndp->lock, flags);
962
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100963 /* Force a specific channel whether or not it has link if we have been
964 * configured to do so
965 */
966 if (force_package && force_channel) {
967 found = force_channel;
968 ncm = &found->modes[NCSI_MODE_LINK];
969 if (!(ncm->data[2] & 0x1))
970 netdev_info(ndp->ndev.dev,
971 "NCSI: Channel %u forced, but it is link down\n",
972 found->id);
973 goto out;
974 }
975
Gavin Shane6f44ed2016-07-19 11:54:19 +1000976 /* The search is done once an inactive channel with up
977 * link is found.
978 */
979 found = NULL;
980 NCSI_FOR_EACH_PACKAGE(ndp, np) {
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +1100981 if (ndp->force_package && np != ndp->force_package)
982 continue;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000983 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100984 spin_lock_irqsave(&nc->lock, flags);
985
Gavin Shane6f44ed2016-07-19 11:54:19 +1000986 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +1100987 nc->state != NCSI_CHANNEL_INACTIVE) {
988 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000989 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100990 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000991
992 if (!found)
993 found = nc;
994
Gavin Shanbbc7c012016-10-20 11:45:51 +1100995 if (nc == hot_nc)
996 found = nc;
997
Gavin Shane6f44ed2016-07-19 11:54:19 +1000998 ncm = &nc->modes[NCSI_MODE_LINK];
999 if (ncm->data[2] & 0x1) {
Gavin Shand8cedaa2016-10-04 11:25:47 +11001000 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001001 found = nc;
1002 goto out;
1003 }
Gavin Shand8cedaa2016-10-04 11:25:47 +11001004
1005 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001006 }
1007 }
1008
1009 if (!found) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001010 netdev_warn(ndp->ndev.dev,
1011 "NCSI: No channel found with link\n");
Gavin Shane6f44ed2016-07-19 11:54:19 +10001012 ncsi_report_link(ndp, true);
1013 return -ENODEV;
1014 }
1015
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001016 ncm = &found->modes[NCSI_MODE_LINK];
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301017 netdev_dbg(ndp->ndev.dev,
1018 "NCSI: Channel %u added to queue (link %s)\n",
1019 found->id, ncm->data[2] & 0x1 ? "up" : "down");
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001020
Gavin Shane6f44ed2016-07-19 11:54:19 +10001021out:
1022 spin_lock_irqsave(&ndp->lock, flags);
1023 list_add_tail_rcu(&found->link, &ndp->channel_queue);
1024 spin_unlock_irqrestore(&ndp->lock, flags);
1025
1026 return ncsi_process_next_channel(ndp);
1027}
1028
1029static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
1030{
1031 struct ncsi_package *np;
1032 struct ncsi_channel *nc;
1033 unsigned int cap;
Gavin Shan100ef012017-10-19 13:43:07 +11001034 bool has_channel = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001035
1036 /* The hardware arbitration is disabled if any one channel
1037 * doesn't support explicitly.
1038 */
1039 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1040 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shan100ef012017-10-19 13:43:07 +11001041 has_channel = true;
1042
Gavin Shane6f44ed2016-07-19 11:54:19 +10001043 cap = nc->caps[NCSI_CAP_GENERIC].cap;
1044 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
1045 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
1046 NCSI_CAP_GENERIC_HWA_SUPPORT) {
1047 ndp->flags &= ~NCSI_DEV_HWA;
1048 return false;
1049 }
1050 }
1051 }
1052
Gavin Shan100ef012017-10-19 13:43:07 +11001053 if (has_channel) {
1054 ndp->flags |= NCSI_DEV_HWA;
1055 return true;
1056 }
1057
1058 ndp->flags &= ~NCSI_DEV_HWA;
1059 return false;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001060}
1061
Gavin Shane6f44ed2016-07-19 11:54:19 +10001062static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
1063{
1064 struct ncsi_dev *nd = &ndp->ndev;
1065 struct ncsi_package *np;
1066 struct ncsi_channel *nc;
1067 struct ncsi_cmd_arg nca;
1068 unsigned char index;
1069 int ret;
1070
1071 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001072 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001073 switch (nd->state) {
1074 case ncsi_dev_state_probe:
1075 nd->state = ncsi_dev_state_probe_deselect;
1076 /* Fall through */
1077 case ncsi_dev_state_probe_deselect:
1078 ndp->pending_req_num = 8;
1079
1080 /* Deselect all possible packages */
1081 nca.type = NCSI_PKT_CMD_DP;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001082 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001083 for (index = 0; index < 8; index++) {
1084 nca.package = index;
1085 ret = ncsi_xmit_cmd(&nca);
1086 if (ret)
1087 goto error;
1088 }
1089
1090 nd->state = ncsi_dev_state_probe_package;
1091 break;
1092 case ncsi_dev_state_probe_package:
Gavin Shane6f44ed2016-07-19 11:54:19 +10001093 ndp->pending_req_num = 1;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001094
Gavin Shane6f44ed2016-07-19 11:54:19 +10001095 nca.type = NCSI_PKT_CMD_SP;
1096 nca.bytes[0] = 1;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001097 nca.package = ndp->package_probe_id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001098 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001099 ret = ncsi_xmit_cmd(&nca);
1100 if (ret)
1101 goto error;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001102 nd->state = ncsi_dev_state_probe_channel;
1103 break;
1104 case ncsi_dev_state_probe_channel:
1105 ndp->active_package = ncsi_find_package(ndp,
1106 ndp->package_probe_id);
1107 if (!ndp->active_package) {
1108 /* No response */
1109 nd->state = ncsi_dev_state_probe_dp;
1110 schedule_work(&ndp->work);
1111 break;
1112 }
Gavin Shane6f44ed2016-07-19 11:54:19 +10001113 nd->state = ncsi_dev_state_probe_cis;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001114 schedule_work(&ndp->work);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001115 break;
1116 case ncsi_dev_state_probe_cis:
Gavin Shan55e02d02016-10-04 11:25:49 +11001117 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001118
1119 /* Clear initial state */
1120 nca.type = NCSI_PKT_CMD_CIS;
1121 nca.package = ndp->active_package->id;
Gavin Shan55e02d02016-10-04 11:25:49 +11001122 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
Gavin Shane6f44ed2016-07-19 11:54:19 +10001123 nca.channel = index;
1124 ret = ncsi_xmit_cmd(&nca);
1125 if (ret)
1126 goto error;
1127 }
1128
1129 nd->state = ncsi_dev_state_probe_gvi;
1130 break;
1131 case ncsi_dev_state_probe_gvi:
1132 case ncsi_dev_state_probe_gc:
1133 case ncsi_dev_state_probe_gls:
1134 np = ndp->active_package;
1135 ndp->pending_req_num = np->channel_num;
1136
1137 /* Retrieve version, capability or link status */
1138 if (nd->state == ncsi_dev_state_probe_gvi)
1139 nca.type = NCSI_PKT_CMD_GVI;
1140 else if (nd->state == ncsi_dev_state_probe_gc)
1141 nca.type = NCSI_PKT_CMD_GC;
1142 else
1143 nca.type = NCSI_PKT_CMD_GLS;
1144
1145 nca.package = np->id;
1146 NCSI_FOR_EACH_CHANNEL(np, nc) {
1147 nca.channel = nc->id;
1148 ret = ncsi_xmit_cmd(&nca);
1149 if (ret)
1150 goto error;
1151 }
1152
1153 if (nd->state == ncsi_dev_state_probe_gvi)
1154 nd->state = ncsi_dev_state_probe_gc;
1155 else if (nd->state == ncsi_dev_state_probe_gc)
1156 nd->state = ncsi_dev_state_probe_gls;
1157 else
1158 nd->state = ncsi_dev_state_probe_dp;
1159 break;
1160 case ncsi_dev_state_probe_dp:
1161 ndp->pending_req_num = 1;
1162
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001163 /* Deselect the current package */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001164 nca.type = NCSI_PKT_CMD_DP;
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001165 nca.package = ndp->package_probe_id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001166 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001167 ret = ncsi_xmit_cmd(&nca);
1168 if (ret)
1169 goto error;
1170
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001171 /* Probe next package */
1172 ndp->package_probe_id++;
1173 if (ndp->package_probe_id >= 8) {
1174 /* Probe finished */
1175 ndp->flags |= NCSI_DEV_PROBED;
1176 break;
1177 }
1178 nd->state = ncsi_dev_state_probe_package;
1179 ndp->active_package = NULL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001180 break;
1181 default:
1182 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1183 nd->state);
1184 }
1185
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001186 if (ndp->flags & NCSI_DEV_PROBED) {
1187 /* Check if all packages have HWA support */
1188 ncsi_check_hwa(ndp);
1189 ncsi_choose_active_channel(ndp);
1190 }
1191
Gavin Shane6f44ed2016-07-19 11:54:19 +10001192 return;
1193error:
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001194 netdev_err(ndp->ndev.dev,
1195 "NCSI: Failed to transmit cmd 0x%x during probe\n",
1196 nca.type);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001197 ncsi_report_link(ndp, true);
1198}
1199
1200static void ncsi_dev_work(struct work_struct *work)
1201{
1202 struct ncsi_dev_priv *ndp = container_of(work,
1203 struct ncsi_dev_priv, work);
1204 struct ncsi_dev *nd = &ndp->ndev;
1205
1206 switch (nd->state & ncsi_dev_state_major) {
1207 case ncsi_dev_state_probe:
1208 ncsi_probe_channel(ndp);
1209 break;
1210 case ncsi_dev_state_suspend:
1211 ncsi_suspend_channel(ndp);
1212 break;
1213 case ncsi_dev_state_config:
1214 ncsi_configure_channel(ndp);
1215 break;
1216 default:
1217 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1218 nd->state);
1219 }
1220}
1221
1222int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1223{
1224 struct ncsi_channel *nc;
1225 int old_state;
1226 unsigned long flags;
1227
1228 spin_lock_irqsave(&ndp->lock, flags);
1229 nc = list_first_or_null_rcu(&ndp->channel_queue,
1230 struct ncsi_channel, link);
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001231 if (!nc) {
1232 spin_unlock_irqrestore(&ndp->lock, flags);
1233 goto out;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001234 }
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001235
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001236 list_del_init(&nc->link);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001237 spin_unlock_irqrestore(&ndp->lock, flags);
1238
Gavin Shand8cedaa2016-10-04 11:25:47 +11001239 spin_lock_irqsave(&nc->lock, flags);
1240 old_state = nc->state;
1241 nc->state = NCSI_CHANNEL_INVISIBLE;
1242 spin_unlock_irqrestore(&nc->lock, flags);
1243
Gavin Shane6f44ed2016-07-19 11:54:19 +10001244 ndp->active_channel = nc;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001245 ndp->active_package = nc->package;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001246
1247 switch (old_state) {
1248 case NCSI_CHANNEL_INACTIVE:
1249 ndp->ndev.state = ncsi_dev_state_config;
Joel Stanley87975a02018-06-19 15:08:31 +09301250 netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
1251 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001252 ncsi_configure_channel(ndp);
1253 break;
1254 case NCSI_CHANNEL_ACTIVE:
1255 ndp->ndev.state = ncsi_dev_state_suspend;
Joel Stanley87975a02018-06-19 15:08:31 +09301256 netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
1257 nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001258 ncsi_suspend_channel(ndp);
1259 break;
1260 default:
1261 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
Gavin Shand8cedaa2016-10-04 11:25:47 +11001262 old_state, nc->package->id, nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001263 ncsi_report_link(ndp, false);
1264 return -EINVAL;
1265 }
1266
1267 return 0;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001268
1269out:
1270 ndp->active_channel = NULL;
1271 ndp->active_package = NULL;
1272 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1273 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1274 return ncsi_choose_active_channel(ndp);
1275 }
1276
1277 ncsi_report_link(ndp, false);
1278 return -ENODEV;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001279}
1280
1281#if IS_ENABLED(CONFIG_IPV6)
1282static int ncsi_inet6addr_event(struct notifier_block *this,
1283 unsigned long event, void *data)
1284{
1285 struct inet6_ifaddr *ifa = data;
1286 struct net_device *dev = ifa->idev->dev;
1287 struct ncsi_dev *nd = ncsi_find_dev(dev);
1288 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1289 struct ncsi_package *np;
1290 struct ncsi_channel *nc;
1291 struct ncsi_cmd_arg nca;
1292 bool action;
1293 int ret;
1294
1295 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1296 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1297 return NOTIFY_OK;
1298
1299 switch (event) {
1300 case NETDEV_UP:
1301 action = (++ndp->inet6_addr_num) == 1;
1302 nca.type = NCSI_PKT_CMD_EGMF;
1303 break;
1304 case NETDEV_DOWN:
1305 action = (--ndp->inet6_addr_num == 0);
1306 nca.type = NCSI_PKT_CMD_DGMF;
1307 break;
1308 default:
1309 return NOTIFY_OK;
1310 }
1311
1312 /* We might not have active channel or packages. The IPv6
1313 * required multicast will be enabled when active channel
1314 * or packages are chosen.
1315 */
1316 np = ndp->active_package;
1317 nc = ndp->active_channel;
1318 if (!action || !np || !nc)
1319 return NOTIFY_OK;
1320
1321 /* We needn't enable or disable it if the function isn't supported */
1322 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1323 return NOTIFY_OK;
1324
1325 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001326 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001327 nca.package = np->id;
1328 nca.channel = nc->id;
1329 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1330 ret = ncsi_xmit_cmd(&nca);
1331 if (ret) {
1332 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1333 (event == NETDEV_UP) ? "enable" : "disable", ret);
1334 return NOTIFY_DONE;
1335 }
1336
1337 return NOTIFY_OK;
1338}
1339
1340static struct notifier_block ncsi_inet6addr_notifier = {
1341 .notifier_call = ncsi_inet6addr_event,
1342};
1343#endif /* CONFIG_IPV6 */
1344
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001345static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
1346{
1347 struct ncsi_dev *nd = &ndp->ndev;
1348 struct ncsi_channel *nc;
1349 struct ncsi_package *np;
1350 unsigned long flags;
1351 unsigned int n = 0;
1352
1353 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1354 NCSI_FOR_EACH_CHANNEL(np, nc) {
1355 spin_lock_irqsave(&nc->lock, flags);
1356
1357 /* Channels may be busy, mark dirty instead of
1358 * kicking if;
1359 * a) not ACTIVE (configured)
1360 * b) in the channel_queue (to be configured)
1361 * c) it's ndev is in the config state
1362 */
1363 if (nc->state != NCSI_CHANNEL_ACTIVE) {
1364 if ((ndp->ndev.state & 0xff00) ==
1365 ncsi_dev_state_config ||
1366 !list_empty(&nc->link)) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301367 netdev_dbg(nd->dev,
1368 "NCSI: channel %p marked dirty\n",
1369 nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001370 nc->reconfigure_needed = true;
1371 }
1372 spin_unlock_irqrestore(&nc->lock, flags);
1373 continue;
1374 }
1375
1376 spin_unlock_irqrestore(&nc->lock, flags);
1377
1378 ncsi_stop_channel_monitor(nc);
1379 spin_lock_irqsave(&nc->lock, flags);
1380 nc->state = NCSI_CHANNEL_INACTIVE;
1381 spin_unlock_irqrestore(&nc->lock, flags);
1382
1383 spin_lock_irqsave(&ndp->lock, flags);
1384 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
1385 spin_unlock_irqrestore(&ndp->lock, flags);
1386
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301387 netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001388 n++;
1389 }
1390 }
1391
1392 return n;
1393}
1394
1395int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
1396{
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001397 struct ncsi_dev_priv *ndp;
1398 unsigned int n_vids = 0;
1399 struct vlan_vid *vlan;
1400 struct ncsi_dev *nd;
1401 bool found = false;
1402
1403 if (vid == 0)
1404 return 0;
1405
1406 nd = ncsi_find_dev(dev);
1407 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001408 netdev_warn(dev, "NCSI: No net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001409 return 0;
1410 }
1411
1412 ndp = TO_NCSI_DEV_PRIV(nd);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001413
1414 /* Add the VLAN id to our internal list */
1415 list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
1416 n_vids++;
1417 if (vlan->vid == vid) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301418 netdev_dbg(dev, "NCSI: vid %u already registered\n",
1419 vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001420 return 0;
1421 }
1422 }
Samuel Mendoza-Jonas6e9c0072017-10-11 16:54:27 +11001423 if (n_vids >= NCSI_MAX_VLAN_VIDS) {
1424 netdev_warn(dev,
1425 "tried to add vlan id %u but NCSI max already registered (%u)\n",
1426 vid, NCSI_MAX_VLAN_VIDS);
1427 return -ENOSPC;
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001428 }
1429
1430 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1431 if (!vlan)
1432 return -ENOMEM;
1433
1434 vlan->proto = proto;
1435 vlan->vid = vid;
1436 list_add_rcu(&vlan->list, &ndp->vlan_vids);
1437
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301438 netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001439
1440 found = ncsi_kick_channels(ndp) != 0;
1441
1442 return found ? ncsi_process_next_channel(ndp) : 0;
1443}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001444EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001445
1446int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
1447{
1448 struct vlan_vid *vlan, *tmp;
1449 struct ncsi_dev_priv *ndp;
1450 struct ncsi_dev *nd;
1451 bool found = false;
1452
1453 if (vid == 0)
1454 return 0;
1455
1456 nd = ncsi_find_dev(dev);
1457 if (!nd) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001458 netdev_warn(dev, "NCSI: no net_device?\n");
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001459 return 0;
1460 }
1461
1462 ndp = TO_NCSI_DEV_PRIV(nd);
1463
1464 /* Remove the VLAN id from our internal list */
1465 list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
1466 if (vlan->vid == vid) {
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301467 netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001468 list_del_rcu(&vlan->list);
1469 found = true;
1470 kfree(vlan);
1471 }
1472
1473 if (!found) {
Samuel Mendoza-Jonas9ef86902017-11-08 16:30:44 +11001474 netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001475 return -EINVAL;
1476 }
1477
1478 found = ncsi_kick_channels(ndp) != 0;
1479
1480 return found ? ncsi_process_next_channel(ndp) : 0;
1481}
Arnd Bergmannfd0c88b2017-09-05 10:05:47 +02001482EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001483
Gavin Shan2d283bd2016-07-19 11:54:16 +10001484struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1485 void (*handler)(struct ncsi_dev *ndev))
1486{
1487 struct ncsi_dev_priv *ndp;
1488 struct ncsi_dev *nd;
1489 unsigned long flags;
1490 int i;
1491
1492 /* Check if the device has been registered or not */
1493 nd = ncsi_find_dev(dev);
1494 if (nd)
1495 return nd;
1496
1497 /* Create NCSI device */
1498 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1499 if (!ndp)
1500 return NULL;
1501
1502 nd = &ndp->ndev;
1503 nd->state = ncsi_dev_state_registered;
1504 nd->dev = dev;
1505 nd->handler = handler;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001506 ndp->pending_req_num = 0;
1507 INIT_LIST_HEAD(&ndp->channel_queue);
Samuel Mendoza-Jonas21acf632017-08-28 16:18:42 +10001508 INIT_LIST_HEAD(&ndp->vlan_vids);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001509 INIT_WORK(&ndp->work, ncsi_dev_work);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001510
1511 /* Initialize private NCSI device */
1512 spin_lock_init(&ndp->lock);
1513 INIT_LIST_HEAD(&ndp->packages);
Gavin Shana15af542016-10-04 11:25:50 +11001514 ndp->request_id = NCSI_REQ_START_IDX;
Gavin Shan2d283bd2016-07-19 11:54:16 +10001515 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1516 ndp->requests[i].id = i;
1517 ndp->requests[i].ndp = ndp;
Kees Cooke99e88a2017-10-16 14:43:17 -07001518 timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001519 }
1520
1521 spin_lock_irqsave(&ncsi_dev_lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001522#if IS_ENABLED(CONFIG_IPV6)
1523 ndp->inet6_addr_num = 0;
1524 if (list_empty(&ncsi_dev_list))
1525 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1526#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001527 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1528 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1529
Gavin Shane6f44ed2016-07-19 11:54:19 +10001530 /* Register NCSI packet Rx handler */
1531 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1532 ndp->ptype.func = ncsi_rcv_rsp;
1533 ndp->ptype.dev = dev;
1534 dev_add_pack(&ndp->ptype);
1535
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001536 /* Set up generic netlink interface */
1537 ncsi_init_netlink(dev);
1538
Gavin Shan2d283bd2016-07-19 11:54:16 +10001539 return nd;
1540}
1541EXPORT_SYMBOL_GPL(ncsi_register_dev);
1542
Gavin Shane6f44ed2016-07-19 11:54:19 +10001543int ncsi_start_dev(struct ncsi_dev *nd)
1544{
1545 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001546
1547 if (nd->state != ncsi_dev_state_registered &&
1548 nd->state != ncsi_dev_state_functional)
1549 return -ENOTTY;
1550
1551 if (!(ndp->flags & NCSI_DEV_PROBED)) {
Samuel Mendoza-Jonas8e13f702018-11-16 15:51:55 +11001552 ndp->package_probe_id = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001553 nd->state = ncsi_dev_state_probe;
1554 schedule_work(&ndp->work);
1555 return 0;
1556 }
1557
Samuel Mendoza-Jonas60ab49b2018-11-16 15:51:54 +11001558 return ncsi_choose_active_channel(ndp);
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001559}
1560EXPORT_SYMBOL_GPL(ncsi_start_dev);
1561
1562void ncsi_stop_dev(struct ncsi_dev *nd)
1563{
1564 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1565 struct ncsi_package *np;
1566 struct ncsi_channel *nc;
1567 bool chained;
1568 int old_state;
1569 unsigned long flags;
1570
1571 /* Stop the channel monitor and reset channel's state */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001572 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1573 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001574 ncsi_stop_channel_monitor(nc);
1575
Gavin Shand8cedaa2016-10-04 11:25:47 +11001576 spin_lock_irqsave(&nc->lock, flags);
1577 chained = !list_empty(&nc->link);
1578 old_state = nc->state;
1579 nc->state = NCSI_CHANNEL_INACTIVE;
1580 spin_unlock_irqrestore(&nc->lock, flags);
1581
1582 WARN_ON_ONCE(chained ||
Gavin Shane6f44ed2016-07-19 11:54:19 +10001583 old_state == NCSI_CHANNEL_INVISIBLE);
1584 }
1585 }
1586
Joel Stanley6e42a3f2018-06-19 15:08:33 +09301587 netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001588 ncsi_report_link(ndp, true);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001589}
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001590EXPORT_SYMBOL_GPL(ncsi_stop_dev);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001591
Gavin Shan2d283bd2016-07-19 11:54:16 +10001592void ncsi_unregister_dev(struct ncsi_dev *nd)
1593{
1594 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1595 struct ncsi_package *np, *tmp;
1596 unsigned long flags;
1597
Gavin Shane6f44ed2016-07-19 11:54:19 +10001598 dev_remove_pack(&ndp->ptype);
1599
Gavin Shan2d283bd2016-07-19 11:54:16 +10001600 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1601 ncsi_remove_package(np);
1602
1603 spin_lock_irqsave(&ncsi_dev_lock, flags);
1604 list_del_rcu(&ndp->node);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001605#if IS_ENABLED(CONFIG_IPV6)
1606 if (list_empty(&ncsi_dev_list))
1607 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1608#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001609 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1610
Samuel Mendoza-Jonas955dc682018-03-05 11:39:05 +11001611 ncsi_unregister_netlink(nd->dev);
1612
Gavin Shan2d283bd2016-07-19 11:54:16 +10001613 kfree(ndp);
1614}
1615EXPORT_SYMBOL_GPL(ncsi_unregister_dev);