blob: 4789f868169597e8f12dc8cc8e998fc05124bfe1 [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>
15#include <linux/netlink.h>
16
17#include <net/ncsi.h>
18#include <net/net_namespace.h>
19#include <net/sock.h>
Gavin Shane6f44ed2016-07-19 11:54:19 +100020#include <net/addrconf.h>
21#include <net/ipv6.h>
22#include <net/if_inet6.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"
Gavin Shan2d283bd2016-07-19 11:54:16 +100026
27LIST_HEAD(ncsi_dev_list);
28DEFINE_SPINLOCK(ncsi_dev_lock);
29
30static inline int ncsi_filter_size(int table)
31{
32 int sizes[] = { 2, 6, 6, 6 };
33
34 BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX);
35 if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX)
36 return -EINVAL;
37
38 return sizes[table];
39}
40
41int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
42{
43 struct ncsi_channel_filter *ncf;
44 void *bitmap;
45 int index, size;
46 unsigned long flags;
47
48 ncf = nc->filters[table];
49 if (!ncf)
50 return -ENXIO;
51
52 size = ncsi_filter_size(table);
53 if (size < 0)
54 return size;
55
56 spin_lock_irqsave(&nc->lock, flags);
57 bitmap = (void *)&ncf->bitmap;
58 index = -1;
59 while ((index = find_next_bit(bitmap, ncf->total, index + 1))
60 < ncf->total) {
61 if (!memcmp(ncf->data + size * index, data, size)) {
62 spin_unlock_irqrestore(&nc->lock, flags);
63 return index;
64 }
65 }
66 spin_unlock_irqrestore(&nc->lock, flags);
67
68 return -ENOENT;
69}
70
71int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
72{
73 struct ncsi_channel_filter *ncf;
74 int index, size;
75 void *bitmap;
76 unsigned long flags;
77
78 size = ncsi_filter_size(table);
79 if (size < 0)
80 return size;
81
82 index = ncsi_find_filter(nc, table, data);
83 if (index >= 0)
84 return index;
85
86 ncf = nc->filters[table];
87 if (!ncf)
88 return -ENODEV;
89
90 spin_lock_irqsave(&nc->lock, flags);
91 bitmap = (void *)&ncf->bitmap;
92 do {
93 index = find_next_zero_bit(bitmap, ncf->total, 0);
94 if (index >= ncf->total) {
95 spin_unlock_irqrestore(&nc->lock, flags);
96 return -ENOSPC;
97 }
98 } while (test_and_set_bit(index, bitmap));
99
100 memcpy(ncf->data + size * index, data, size);
101 spin_unlock_irqrestore(&nc->lock, flags);
102
103 return index;
104}
105
106int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
107{
108 struct ncsi_channel_filter *ncf;
109 int size;
110 void *bitmap;
111 unsigned long flags;
112
113 size = ncsi_filter_size(table);
114 if (size < 0)
115 return size;
116
117 ncf = nc->filters[table];
118 if (!ncf || index >= ncf->total)
119 return -ENODEV;
120
121 spin_lock_irqsave(&nc->lock, flags);
122 bitmap = (void *)&ncf->bitmap;
123 if (test_and_clear_bit(index, bitmap))
124 memset(ncf->data + size * index, 0, size);
125 spin_unlock_irqrestore(&nc->lock, flags);
126
127 return 0;
128}
129
Gavin Shane6f44ed2016-07-19 11:54:19 +1000130static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
131{
132 struct ncsi_dev *nd = &ndp->ndev;
133 struct ncsi_package *np;
134 struct ncsi_channel *nc;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100135 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000136
137 nd->state = ncsi_dev_state_functional;
138 if (force_down) {
139 nd->link_up = 0;
140 goto report;
141 }
142
143 nd->link_up = 0;
144 NCSI_FOR_EACH_PACKAGE(ndp, np) {
145 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100146 spin_lock_irqsave(&nc->lock, flags);
147
Gavin Shane6f44ed2016-07-19 11:54:19 +1000148 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +1100149 nc->state != NCSI_CHANNEL_ACTIVE) {
150 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000151 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100152 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000153
154 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100155 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000156 nd->link_up = 1;
157 goto report;
158 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100159
160 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000161 }
162 }
163
164report:
165 nd->handler(nd);
166}
167
168static void ncsi_channel_monitor(unsigned long data)
169{
170 struct ncsi_channel *nc = (struct ncsi_channel *)data;
171 struct ncsi_package *np = nc->package;
172 struct ncsi_dev_priv *ndp = np->ndp;
173 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100174 bool enabled, chained;
Gavin Shan83afdc62016-10-04 11:25:52 +1100175 unsigned int monitor_state;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000176 unsigned long flags;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100177 int state, ret;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000178
179 spin_lock_irqsave(&nc->lock, flags);
Gavin Shand8cedaa2016-10-04 11:25:47 +1100180 state = nc->state;
181 chained = !list_empty(&nc->link);
Gavin Shan83afdc62016-10-04 11:25:52 +1100182 enabled = nc->monitor.enabled;
183 monitor_state = nc->monitor.state;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000184 spin_unlock_irqrestore(&nc->lock, flags);
185
Gavin Shand8cedaa2016-10-04 11:25:47 +1100186 if (!enabled || chained)
Gavin Shane6f44ed2016-07-19 11:54:19 +1000187 return;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100188 if (state != NCSI_CHANNEL_INACTIVE &&
189 state != NCSI_CHANNEL_ACTIVE)
Gavin Shane6f44ed2016-07-19 11:54:19 +1000190 return;
191
Gavin Shan83afdc62016-10-04 11:25:52 +1100192 switch (monitor_state) {
193 case NCSI_CHANNEL_MONITOR_START:
194 case NCSI_CHANNEL_MONITOR_RETRY:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000195 nca.ndp = ndp;
196 nca.package = np->id;
197 nca.channel = nc->id;
198 nca.type = NCSI_PKT_CMD_GLS;
Gavin Shana0509cb2016-10-04 11:25:51 +1100199 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000200 ret = ncsi_xmit_cmd(&nca);
201 if (ret) {
202 netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
203 ret);
204 return;
205 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000206
Gavin Shan83afdc62016-10-04 11:25:52 +1100207 break;
208 case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
209 break;
210 default:
Gavin Shane6f44ed2016-07-19 11:54:19 +1000211 if (!(ndp->flags & NCSI_DEV_HWA) &&
Gavin Shan83afdc62016-10-04 11:25:52 +1100212 state == NCSI_CHANNEL_ACTIVE) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000213 ncsi_report_link(ndp, true);
Gavin Shan83afdc62016-10-04 11:25:52 +1100214 ndp->flags |= NCSI_DEV_RESHUFFLE;
215 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000216
Gavin Shand8cedaa2016-10-04 11:25:47 +1100217 spin_lock_irqsave(&nc->lock, flags);
218 nc->state = NCSI_CHANNEL_INVISIBLE;
219 spin_unlock_irqrestore(&nc->lock, flags);
220
Gavin Shane6f44ed2016-07-19 11:54:19 +1000221 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shand8cedaa2016-10-04 11:25:47 +1100222 nc->state = NCSI_CHANNEL_INACTIVE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000223 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
224 spin_unlock_irqrestore(&ndp->lock, flags);
225 ncsi_process_next_channel(ndp);
226 return;
227 }
228
229 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100230 nc->monitor.state++;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000231 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100232 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000233}
234
235void ncsi_start_channel_monitor(struct ncsi_channel *nc)
236{
237 unsigned long flags;
238
239 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100240 WARN_ON_ONCE(nc->monitor.enabled);
241 nc->monitor.enabled = true;
242 nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000243 spin_unlock_irqrestore(&nc->lock, flags);
244
Gavin Shan83afdc62016-10-04 11:25:52 +1100245 mod_timer(&nc->monitor.timer, jiffies + HZ);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000246}
247
248void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
249{
250 unsigned long flags;
251
252 spin_lock_irqsave(&nc->lock, flags);
Gavin Shan83afdc62016-10-04 11:25:52 +1100253 if (!nc->monitor.enabled) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000254 spin_unlock_irqrestore(&nc->lock, flags);
255 return;
256 }
Gavin Shan83afdc62016-10-04 11:25:52 +1100257 nc->monitor.enabled = false;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000258 spin_unlock_irqrestore(&nc->lock, flags);
259
Gavin Shan83afdc62016-10-04 11:25:52 +1100260 del_timer_sync(&nc->monitor.timer);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000261}
262
Gavin Shan2d283bd2016-07-19 11:54:16 +1000263struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
264 unsigned char id)
265{
266 struct ncsi_channel *nc;
267
268 NCSI_FOR_EACH_CHANNEL(np, nc) {
269 if (nc->id == id)
270 return nc;
271 }
272
273 return NULL;
274}
275
276struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
277{
278 struct ncsi_channel *nc, *tmp;
279 int index;
280 unsigned long flags;
281
282 nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
283 if (!nc)
284 return NULL;
285
286 nc->id = id;
287 nc->package = np;
288 nc->state = NCSI_CHANNEL_INACTIVE;
Gavin Shan83afdc62016-10-04 11:25:52 +1100289 nc->monitor.enabled = false;
290 setup_timer(&nc->monitor.timer,
291 ncsi_channel_monitor, (unsigned long)nc);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000292 spin_lock_init(&nc->lock);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000293 INIT_LIST_HEAD(&nc->link);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000294 for (index = 0; index < NCSI_CAP_MAX; index++)
295 nc->caps[index].index = index;
296 for (index = 0; index < NCSI_MODE_MAX; index++)
297 nc->modes[index].index = index;
298
299 spin_lock_irqsave(&np->lock, flags);
300 tmp = ncsi_find_channel(np, id);
301 if (tmp) {
302 spin_unlock_irqrestore(&np->lock, flags);
303 kfree(nc);
304 return tmp;
305 }
306
307 list_add_tail_rcu(&nc->node, &np->channels);
308 np->channel_num++;
309 spin_unlock_irqrestore(&np->lock, flags);
310
311 return nc;
312}
313
314static void ncsi_remove_channel(struct ncsi_channel *nc)
315{
316 struct ncsi_package *np = nc->package;
317 struct ncsi_channel_filter *ncf;
318 unsigned long flags;
319 int i;
320
321 /* Release filters */
322 spin_lock_irqsave(&nc->lock, flags);
323 for (i = 0; i < NCSI_FILTER_MAX; i++) {
324 ncf = nc->filters[i];
325 if (!ncf)
326 continue;
327
328 nc->filters[i] = NULL;
329 kfree(ncf);
330 }
331
332 nc->state = NCSI_CHANNEL_INACTIVE;
333 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000334 ncsi_stop_channel_monitor(nc);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000335
336 /* Remove and free channel */
337 spin_lock_irqsave(&np->lock, flags);
338 list_del_rcu(&nc->node);
339 np->channel_num--;
340 spin_unlock_irqrestore(&np->lock, flags);
341
342 kfree(nc);
343}
344
345struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
346 unsigned char id)
347{
348 struct ncsi_package *np;
349
350 NCSI_FOR_EACH_PACKAGE(ndp, np) {
351 if (np->id == id)
352 return np;
353 }
354
355 return NULL;
356}
357
358struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
359 unsigned char id)
360{
361 struct ncsi_package *np, *tmp;
362 unsigned long flags;
363
364 np = kzalloc(sizeof(*np), GFP_ATOMIC);
365 if (!np)
366 return NULL;
367
368 np->id = id;
369 np->ndp = ndp;
370 spin_lock_init(&np->lock);
371 INIT_LIST_HEAD(&np->channels);
372
373 spin_lock_irqsave(&ndp->lock, flags);
374 tmp = ncsi_find_package(ndp, id);
375 if (tmp) {
376 spin_unlock_irqrestore(&ndp->lock, flags);
377 kfree(np);
378 return tmp;
379 }
380
381 list_add_tail_rcu(&np->node, &ndp->packages);
382 ndp->package_num++;
383 spin_unlock_irqrestore(&ndp->lock, flags);
384
385 return np;
386}
387
388void ncsi_remove_package(struct ncsi_package *np)
389{
390 struct ncsi_dev_priv *ndp = np->ndp;
391 struct ncsi_channel *nc, *tmp;
392 unsigned long flags;
393
394 /* Release all child channels */
395 list_for_each_entry_safe(nc, tmp, &np->channels, node)
396 ncsi_remove_channel(nc);
397
398 /* Remove and free package */
399 spin_lock_irqsave(&ndp->lock, flags);
400 list_del_rcu(&np->node);
401 ndp->package_num--;
402 spin_unlock_irqrestore(&ndp->lock, flags);
403
404 kfree(np);
405}
406
407void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
408 unsigned char id,
409 struct ncsi_package **np,
410 struct ncsi_channel **nc)
411{
412 struct ncsi_package *p;
413 struct ncsi_channel *c;
414
415 p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
416 c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
417
418 if (np)
419 *np = p;
420 if (nc)
421 *nc = c;
422}
423
424/* For two consecutive NCSI commands, the packet IDs shouldn't
425 * be same. Otherwise, the bogus response might be replied. So
426 * the available IDs are allocated in round-robin fashion.
427 */
Gavin Shana0509cb2016-10-04 11:25:51 +1100428struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
429 unsigned int req_flags)
Gavin Shan2d283bd2016-07-19 11:54:16 +1000430{
431 struct ncsi_request *nr = NULL;
432 int i, limit = ARRAY_SIZE(ndp->requests);
433 unsigned long flags;
434
435 /* Check if there is one available request until the ceiling */
436 spin_lock_irqsave(&ndp->lock, flags);
Gavin Shana15af542016-10-04 11:25:50 +1100437 for (i = ndp->request_id; i < limit; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000438 if (ndp->requests[i].used)
439 continue;
440
441 nr = &ndp->requests[i];
442 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100443 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100444 ndp->request_id = i + 1;
445 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000446 }
447
448 /* Fail back to check from the starting cursor */
Gavin Shana15af542016-10-04 11:25:50 +1100449 for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
Gavin Shan2d283bd2016-07-19 11:54:16 +1000450 if (ndp->requests[i].used)
451 continue;
452
453 nr = &ndp->requests[i];
454 nr->used = true;
Gavin Shana0509cb2016-10-04 11:25:51 +1100455 nr->flags = req_flags;
Gavin Shana15af542016-10-04 11:25:50 +1100456 ndp->request_id = i + 1;
457 goto found;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000458 }
Gavin Shan2d283bd2016-07-19 11:54:16 +1000459
Gavin Shana15af542016-10-04 11:25:50 +1100460found:
461 spin_unlock_irqrestore(&ndp->lock, flags);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000462 return nr;
463}
464
465void ncsi_free_request(struct ncsi_request *nr)
466{
467 struct ncsi_dev_priv *ndp = nr->ndp;
468 struct sk_buff *cmd, *rsp;
469 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000470 bool driven;
Gavin Shan2d283bd2016-07-19 11:54:16 +1000471
472 if (nr->enabled) {
473 nr->enabled = false;
474 del_timer_sync(&nr->timer);
475 }
476
477 spin_lock_irqsave(&ndp->lock, flags);
478 cmd = nr->cmd;
479 rsp = nr->rsp;
480 nr->cmd = NULL;
481 nr->rsp = NULL;
482 nr->used = false;
Gavin Shana0509cb2016-10-04 11:25:51 +1100483 driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
Gavin Shan2d283bd2016-07-19 11:54:16 +1000484 spin_unlock_irqrestore(&ndp->lock, flags);
485
Gavin Shane6f44ed2016-07-19 11:54:19 +1000486 if (driven && cmd && --ndp->pending_req_num == 0)
487 schedule_work(&ndp->work);
488
Gavin Shan2d283bd2016-07-19 11:54:16 +1000489 /* Release command and response */
490 consume_skb(cmd);
491 consume_skb(rsp);
492}
493
494struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
495{
496 struct ncsi_dev_priv *ndp;
497
498 NCSI_FOR_EACH_DEV(ndp) {
499 if (ndp->ndev.dev == dev)
500 return &ndp->ndev;
501 }
502
503 return NULL;
504}
505
506static void ncsi_request_timeout(unsigned long data)
507{
508 struct ncsi_request *nr = (struct ncsi_request *)data;
509 struct ncsi_dev_priv *ndp = nr->ndp;
510 unsigned long flags;
511
512 /* If the request already had associated response,
513 * let the response handler to release it.
514 */
515 spin_lock_irqsave(&ndp->lock, flags);
516 nr->enabled = false;
517 if (nr->rsp || !nr->cmd) {
518 spin_unlock_irqrestore(&ndp->lock, flags);
519 return;
520 }
521 spin_unlock_irqrestore(&ndp->lock, flags);
522
523 /* Release the request */
524 ncsi_free_request(nr);
525}
526
Gavin Shane6f44ed2016-07-19 11:54:19 +1000527static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
528{
529 struct ncsi_dev *nd = &ndp->ndev;
530 struct ncsi_package *np = ndp->active_package;
531 struct ncsi_channel *nc = ndp->active_channel;
532 struct ncsi_cmd_arg nca;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100533 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000534 int ret;
535
536 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100537 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000538 switch (nd->state) {
539 case ncsi_dev_state_suspend:
540 nd->state = ncsi_dev_state_suspend_select;
541 /* Fall through */
542 case ncsi_dev_state_suspend_select:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100543 ndp->pending_req_num = 1;
544
545 nca.type = NCSI_PKT_CMD_SP;
546 nca.package = np->id;
547 nca.channel = NCSI_RESERVED_CHANNEL;
548 if (ndp->flags & NCSI_DEV_HWA)
549 nca.bytes[0] = 0;
550 else
551 nca.bytes[0] = 1;
552
Gavin Shan008a4242016-10-20 11:45:50 +1100553 /* To retrieve the last link states of channels in current
554 * package when current active channel needs fail over to
555 * another one. It means we will possibly select another
556 * channel as next active one. The link states of channels
557 * are most important factor of the selection. So we need
558 * accurate link states. Unfortunately, the link states on
559 * inactive channels can't be updated with LSC AEN in time.
560 */
561 if (ndp->flags & NCSI_DEV_RESHUFFLE)
562 nd->state = ncsi_dev_state_suspend_gls;
563 else
564 nd->state = ncsi_dev_state_suspend_dcnt;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100565 ret = ncsi_xmit_cmd(&nca);
566 if (ret)
567 goto error;
568
569 break;
Gavin Shan008a4242016-10-20 11:45:50 +1100570 case ncsi_dev_state_suspend_gls:
571 ndp->pending_req_num = np->channel_num;
572
573 nca.type = NCSI_PKT_CMD_GLS;
574 nca.package = np->id;
575
576 nd->state = ncsi_dev_state_suspend_dcnt;
577 NCSI_FOR_EACH_CHANNEL(np, nc) {
578 nca.channel = nc->id;
579 ret = ncsi_xmit_cmd(&nca);
580 if (ret)
581 goto error;
582 }
583
584 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000585 case ncsi_dev_state_suspend_dcnt:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100586 ndp->pending_req_num = 1;
587
588 nca.type = NCSI_PKT_CMD_DCNT;
589 nca.package = np->id;
590 nca.channel = nc->id;
591
592 nd->state = ncsi_dev_state_suspend_dc;
593 ret = ncsi_xmit_cmd(&nca);
594 if (ret)
595 goto error;
596
597 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000598 case ncsi_dev_state_suspend_dc:
Gavin Shan7ba5c002016-10-20 11:45:49 +1100599 ndp->pending_req_num = 1;
600
601 nca.type = NCSI_PKT_CMD_DC;
602 nca.package = np->id;
603 nca.channel = nc->id;
604 nca.bytes[0] = 1;
605
606 nd->state = ncsi_dev_state_suspend_deselect;
607 ret = ncsi_xmit_cmd(&nca);
608 if (ret)
609 goto error;
610
611 break;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000612 case ncsi_dev_state_suspend_deselect:
613 ndp->pending_req_num = 1;
614
Gavin Shan7ba5c002016-10-20 11:45:49 +1100615 nca.type = NCSI_PKT_CMD_DP;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000616 nca.package = np->id;
Gavin Shan7ba5c002016-10-20 11:45:49 +1100617 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000618
Gavin Shan7ba5c002016-10-20 11:45:49 +1100619 nd->state = ncsi_dev_state_suspend_done;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000620 ret = ncsi_xmit_cmd(&nca);
Gavin Shan7ba5c002016-10-20 11:45:49 +1100621 if (ret)
622 goto error;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000623
624 break;
625 case ncsi_dev_state_suspend_done:
Gavin Shand8cedaa2016-10-04 11:25:47 +1100626 spin_lock_irqsave(&nc->lock, flags);
627 nc->state = NCSI_CHANNEL_INACTIVE;
628 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000629 ncsi_process_next_channel(ndp);
630
631 break;
632 default:
633 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
634 nd->state);
635 }
Gavin Shan7ba5c002016-10-20 11:45:49 +1100636
637 return;
638error:
639 nd->state = ncsi_dev_state_functional;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000640}
641
642static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
643{
644 struct ncsi_dev *nd = &ndp->ndev;
645 struct net_device *dev = nd->dev;
646 struct ncsi_package *np = ndp->active_package;
647 struct ncsi_channel *nc = ndp->active_channel;
648 struct ncsi_cmd_arg nca;
649 unsigned char index;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100650 unsigned long flags;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000651 int ret;
652
653 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100654 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000655 switch (nd->state) {
656 case ncsi_dev_state_config:
657 case ncsi_dev_state_config_sp:
658 ndp->pending_req_num = 1;
659
660 /* Select the specific package */
661 nca.type = NCSI_PKT_CMD_SP;
662 if (ndp->flags & NCSI_DEV_HWA)
663 nca.bytes[0] = 0;
664 else
665 nca.bytes[0] = 1;
666 nca.package = np->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100667 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000668 ret = ncsi_xmit_cmd(&nca);
669 if (ret)
670 goto error;
671
672 nd->state = ncsi_dev_state_config_cis;
673 break;
674 case ncsi_dev_state_config_cis:
675 ndp->pending_req_num = 1;
676
677 /* Clear initial state */
678 nca.type = NCSI_PKT_CMD_CIS;
679 nca.package = np->id;
680 nca.channel = nc->id;
681 ret = ncsi_xmit_cmd(&nca);
682 if (ret)
683 goto error;
684
685 nd->state = ncsi_dev_state_config_sma;
686 break;
687 case ncsi_dev_state_config_sma:
688 case ncsi_dev_state_config_ebf:
689#if IS_ENABLED(CONFIG_IPV6)
690 case ncsi_dev_state_config_egmf:
691#endif
692 case ncsi_dev_state_config_ecnt:
693 case ncsi_dev_state_config_ec:
694 case ncsi_dev_state_config_ae:
695 case ncsi_dev_state_config_gls:
696 ndp->pending_req_num = 1;
697
698 nca.package = np->id;
699 nca.channel = nc->id;
700
701 /* Use first entry in unicast filter table. Note that
702 * the MAC filter table starts from entry 1 instead of
703 * 0.
704 */
705 if (nd->state == ncsi_dev_state_config_sma) {
706 nca.type = NCSI_PKT_CMD_SMA;
707 for (index = 0; index < 6; index++)
708 nca.bytes[index] = dev->dev_addr[index];
709 nca.bytes[6] = 0x1;
710 nca.bytes[7] = 0x1;
711 nd->state = ncsi_dev_state_config_ebf;
712 } else if (nd->state == ncsi_dev_state_config_ebf) {
713 nca.type = NCSI_PKT_CMD_EBF;
714 nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
715 nd->state = ncsi_dev_state_config_ecnt;
716#if IS_ENABLED(CONFIG_IPV6)
717 if (ndp->inet6_addr_num > 0 &&
718 (nc->caps[NCSI_CAP_GENERIC].cap &
719 NCSI_CAP_GENERIC_MC))
720 nd->state = ncsi_dev_state_config_egmf;
721 else
722 nd->state = ncsi_dev_state_config_ecnt;
723 } else if (nd->state == ncsi_dev_state_config_egmf) {
724 nca.type = NCSI_PKT_CMD_EGMF;
725 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
726 nd->state = ncsi_dev_state_config_ecnt;
727#endif /* CONFIG_IPV6 */
728 } else if (nd->state == ncsi_dev_state_config_ecnt) {
729 nca.type = NCSI_PKT_CMD_ECNT;
730 nd->state = ncsi_dev_state_config_ec;
731 } else if (nd->state == ncsi_dev_state_config_ec) {
732 /* Enable AEN if it's supported */
733 nca.type = NCSI_PKT_CMD_EC;
734 nd->state = ncsi_dev_state_config_ae;
735 if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
736 nd->state = ncsi_dev_state_config_gls;
737 } else if (nd->state == ncsi_dev_state_config_ae) {
738 nca.type = NCSI_PKT_CMD_AE;
739 nca.bytes[0] = 0;
740 nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
741 nd->state = ncsi_dev_state_config_gls;
742 } else if (nd->state == ncsi_dev_state_config_gls) {
743 nca.type = NCSI_PKT_CMD_GLS;
744 nd->state = ncsi_dev_state_config_done;
745 }
746
747 ret = ncsi_xmit_cmd(&nca);
748 if (ret)
749 goto error;
750 break;
751 case ncsi_dev_state_config_done:
Gavin Shand8cedaa2016-10-04 11:25:47 +1100752 spin_lock_irqsave(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000753 if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1)
Gavin Shand8cedaa2016-10-04 11:25:47 +1100754 nc->state = NCSI_CHANNEL_ACTIVE;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000755 else
Gavin Shand8cedaa2016-10-04 11:25:47 +1100756 nc->state = NCSI_CHANNEL_INACTIVE;
757 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000758
759 ncsi_start_channel_monitor(nc);
760 ncsi_process_next_channel(ndp);
761 break;
762 default:
763 netdev_warn(dev, "Wrong NCSI state 0x%x in config\n",
764 nd->state);
765 }
766
767 return;
768
769error:
770 ncsi_report_link(ndp, true);
771}
772
773static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
774{
775 struct ncsi_package *np;
776 struct ncsi_channel *nc, *found;
777 struct ncsi_channel_mode *ncm;
778 unsigned long flags;
779
780 /* The search is done once an inactive channel with up
781 * link is found.
782 */
783 found = NULL;
784 NCSI_FOR_EACH_PACKAGE(ndp, np) {
785 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100786 spin_lock_irqsave(&nc->lock, flags);
787
Gavin Shane6f44ed2016-07-19 11:54:19 +1000788 if (!list_empty(&nc->link) ||
Gavin Shand8cedaa2016-10-04 11:25:47 +1100789 nc->state != NCSI_CHANNEL_INACTIVE) {
790 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000791 continue;
Gavin Shand8cedaa2016-10-04 11:25:47 +1100792 }
Gavin Shane6f44ed2016-07-19 11:54:19 +1000793
794 if (!found)
795 found = nc;
796
797 ncm = &nc->modes[NCSI_MODE_LINK];
798 if (ncm->data[2] & 0x1) {
Gavin Shand8cedaa2016-10-04 11:25:47 +1100799 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000800 found = nc;
801 goto out;
802 }
Gavin Shand8cedaa2016-10-04 11:25:47 +1100803
804 spin_unlock_irqrestore(&nc->lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +1000805 }
806 }
807
808 if (!found) {
809 ncsi_report_link(ndp, true);
810 return -ENODEV;
811 }
812
813out:
814 spin_lock_irqsave(&ndp->lock, flags);
815 list_add_tail_rcu(&found->link, &ndp->channel_queue);
816 spin_unlock_irqrestore(&ndp->lock, flags);
817
818 return ncsi_process_next_channel(ndp);
819}
820
821static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
822{
823 struct ncsi_package *np;
824 struct ncsi_channel *nc;
825 unsigned int cap;
826
827 /* The hardware arbitration is disabled if any one channel
828 * doesn't support explicitly.
829 */
830 NCSI_FOR_EACH_PACKAGE(ndp, np) {
831 NCSI_FOR_EACH_CHANNEL(np, nc) {
832 cap = nc->caps[NCSI_CAP_GENERIC].cap;
833 if (!(cap & NCSI_CAP_GENERIC_HWA) ||
834 (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
835 NCSI_CAP_GENERIC_HWA_SUPPORT) {
836 ndp->flags &= ~NCSI_DEV_HWA;
837 return false;
838 }
839 }
840 }
841
842 ndp->flags |= NCSI_DEV_HWA;
843 return true;
844}
845
846static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
847{
848 struct ncsi_package *np;
849 struct ncsi_channel *nc;
850 unsigned long flags;
851
852 /* Move all available channels to processing queue */
853 spin_lock_irqsave(&ndp->lock, flags);
854 NCSI_FOR_EACH_PACKAGE(ndp, np) {
855 NCSI_FOR_EACH_CHANNEL(np, nc) {
856 WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
857 !list_empty(&nc->link));
858 ncsi_stop_channel_monitor(nc);
859 list_add_tail_rcu(&nc->link, &ndp->channel_queue);
860 }
861 }
862 spin_unlock_irqrestore(&ndp->lock, flags);
863
864 /* We can have no channels in extremely case */
865 if (list_empty(&ndp->channel_queue)) {
866 ncsi_report_link(ndp, false);
867 return -ENOENT;
868 }
869
870 return ncsi_process_next_channel(ndp);
871}
872
873static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
874{
875 struct ncsi_dev *nd = &ndp->ndev;
876 struct ncsi_package *np;
877 struct ncsi_channel *nc;
878 struct ncsi_cmd_arg nca;
879 unsigned char index;
880 int ret;
881
882 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +1100883 nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000884 switch (nd->state) {
885 case ncsi_dev_state_probe:
886 nd->state = ncsi_dev_state_probe_deselect;
887 /* Fall through */
888 case ncsi_dev_state_probe_deselect:
889 ndp->pending_req_num = 8;
890
891 /* Deselect all possible packages */
892 nca.type = NCSI_PKT_CMD_DP;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100893 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000894 for (index = 0; index < 8; index++) {
895 nca.package = index;
896 ret = ncsi_xmit_cmd(&nca);
897 if (ret)
898 goto error;
899 }
900
901 nd->state = ncsi_dev_state_probe_package;
902 break;
903 case ncsi_dev_state_probe_package:
904 ndp->pending_req_num = 16;
905
906 /* Select all possible packages */
907 nca.type = NCSI_PKT_CMD_SP;
908 nca.bytes[0] = 1;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100909 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000910 for (index = 0; index < 8; index++) {
911 nca.package = index;
912 ret = ncsi_xmit_cmd(&nca);
913 if (ret)
914 goto error;
915 }
916
917 /* Disable all possible packages */
918 nca.type = NCSI_PKT_CMD_DP;
919 for (index = 0; index < 8; index++) {
920 nca.package = index;
921 ret = ncsi_xmit_cmd(&nca);
922 if (ret)
923 goto error;
924 }
925
926 nd->state = ncsi_dev_state_probe_channel;
927 break;
928 case ncsi_dev_state_probe_channel:
929 if (!ndp->active_package)
930 ndp->active_package = list_first_or_null_rcu(
931 &ndp->packages, struct ncsi_package, node);
932 else if (list_is_last(&ndp->active_package->node,
933 &ndp->packages))
934 ndp->active_package = NULL;
935 else
936 ndp->active_package = list_next_entry(
937 ndp->active_package, node);
938
939 /* All available packages and channels are enumerated. The
940 * enumeration happens for once when the NCSI interface is
941 * started. So we need continue to start the interface after
942 * the enumeration.
943 *
944 * We have to choose an active channel before configuring it.
945 * Note that we possibly don't have active channel in extreme
946 * situation.
947 */
948 if (!ndp->active_package) {
949 ndp->flags |= NCSI_DEV_PROBED;
950 if (ncsi_check_hwa(ndp))
951 ncsi_enable_hwa(ndp);
952 else
953 ncsi_choose_active_channel(ndp);
954 return;
955 }
956
957 /* Select the active package */
958 ndp->pending_req_num = 1;
959 nca.type = NCSI_PKT_CMD_SP;
960 nca.bytes[0] = 1;
961 nca.package = ndp->active_package->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +1100962 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000963 ret = ncsi_xmit_cmd(&nca);
964 if (ret)
965 goto error;
966
967 nd->state = ncsi_dev_state_probe_cis;
968 break;
969 case ncsi_dev_state_probe_cis:
Gavin Shan55e02d02016-10-04 11:25:49 +1100970 ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +1000971
972 /* Clear initial state */
973 nca.type = NCSI_PKT_CMD_CIS;
974 nca.package = ndp->active_package->id;
Gavin Shan55e02d02016-10-04 11:25:49 +1100975 for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
Gavin Shane6f44ed2016-07-19 11:54:19 +1000976 nca.channel = index;
977 ret = ncsi_xmit_cmd(&nca);
978 if (ret)
979 goto error;
980 }
981
982 nd->state = ncsi_dev_state_probe_gvi;
983 break;
984 case ncsi_dev_state_probe_gvi:
985 case ncsi_dev_state_probe_gc:
986 case ncsi_dev_state_probe_gls:
987 np = ndp->active_package;
988 ndp->pending_req_num = np->channel_num;
989
990 /* Retrieve version, capability or link status */
991 if (nd->state == ncsi_dev_state_probe_gvi)
992 nca.type = NCSI_PKT_CMD_GVI;
993 else if (nd->state == ncsi_dev_state_probe_gc)
994 nca.type = NCSI_PKT_CMD_GC;
995 else
996 nca.type = NCSI_PKT_CMD_GLS;
997
998 nca.package = np->id;
999 NCSI_FOR_EACH_CHANNEL(np, nc) {
1000 nca.channel = nc->id;
1001 ret = ncsi_xmit_cmd(&nca);
1002 if (ret)
1003 goto error;
1004 }
1005
1006 if (nd->state == ncsi_dev_state_probe_gvi)
1007 nd->state = ncsi_dev_state_probe_gc;
1008 else if (nd->state == ncsi_dev_state_probe_gc)
1009 nd->state = ncsi_dev_state_probe_gls;
1010 else
1011 nd->state = ncsi_dev_state_probe_dp;
1012 break;
1013 case ncsi_dev_state_probe_dp:
1014 ndp->pending_req_num = 1;
1015
1016 /* Deselect the active package */
1017 nca.type = NCSI_PKT_CMD_DP;
1018 nca.package = ndp->active_package->id;
Gavin Shanbc7e0f52016-10-04 11:25:48 +11001019 nca.channel = NCSI_RESERVED_CHANNEL;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001020 ret = ncsi_xmit_cmd(&nca);
1021 if (ret)
1022 goto error;
1023
1024 /* Scan channels in next package */
1025 nd->state = ncsi_dev_state_probe_channel;
1026 break;
1027 default:
1028 netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
1029 nd->state);
1030 }
1031
1032 return;
1033error:
1034 ncsi_report_link(ndp, true);
1035}
1036
1037static void ncsi_dev_work(struct work_struct *work)
1038{
1039 struct ncsi_dev_priv *ndp = container_of(work,
1040 struct ncsi_dev_priv, work);
1041 struct ncsi_dev *nd = &ndp->ndev;
1042
1043 switch (nd->state & ncsi_dev_state_major) {
1044 case ncsi_dev_state_probe:
1045 ncsi_probe_channel(ndp);
1046 break;
1047 case ncsi_dev_state_suspend:
1048 ncsi_suspend_channel(ndp);
1049 break;
1050 case ncsi_dev_state_config:
1051 ncsi_configure_channel(ndp);
1052 break;
1053 default:
1054 netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
1055 nd->state);
1056 }
1057}
1058
1059int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
1060{
1061 struct ncsi_channel *nc;
1062 int old_state;
1063 unsigned long flags;
1064
1065 spin_lock_irqsave(&ndp->lock, flags);
1066 nc = list_first_or_null_rcu(&ndp->channel_queue,
1067 struct ncsi_channel, link);
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001068 if (!nc) {
1069 spin_unlock_irqrestore(&ndp->lock, flags);
1070 goto out;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001071 }
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001072
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001073 list_del_init(&nc->link);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001074 spin_unlock_irqrestore(&ndp->lock, flags);
1075
Gavin Shand8cedaa2016-10-04 11:25:47 +11001076 spin_lock_irqsave(&nc->lock, flags);
1077 old_state = nc->state;
1078 nc->state = NCSI_CHANNEL_INVISIBLE;
1079 spin_unlock_irqrestore(&nc->lock, flags);
1080
Gavin Shane6f44ed2016-07-19 11:54:19 +10001081 ndp->active_channel = nc;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001082 ndp->active_package = nc->package;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001083
1084 switch (old_state) {
1085 case NCSI_CHANNEL_INACTIVE:
1086 ndp->ndev.state = ncsi_dev_state_config;
1087 ncsi_configure_channel(ndp);
1088 break;
1089 case NCSI_CHANNEL_ACTIVE:
1090 ndp->ndev.state = ncsi_dev_state_suspend;
1091 ncsi_suspend_channel(ndp);
1092 break;
1093 default:
1094 netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
Gavin Shand8cedaa2016-10-04 11:25:47 +11001095 old_state, nc->package->id, nc->id);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001096 ncsi_report_link(ndp, false);
1097 return -EINVAL;
1098 }
1099
1100 return 0;
Arnd Bergmanna1b43ed2016-07-21 21:28:34 +02001101
1102out:
1103 ndp->active_channel = NULL;
1104 ndp->active_package = NULL;
1105 if (ndp->flags & NCSI_DEV_RESHUFFLE) {
1106 ndp->flags &= ~NCSI_DEV_RESHUFFLE;
1107 return ncsi_choose_active_channel(ndp);
1108 }
1109
1110 ncsi_report_link(ndp, false);
1111 return -ENODEV;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001112}
1113
1114#if IS_ENABLED(CONFIG_IPV6)
1115static int ncsi_inet6addr_event(struct notifier_block *this,
1116 unsigned long event, void *data)
1117{
1118 struct inet6_ifaddr *ifa = data;
1119 struct net_device *dev = ifa->idev->dev;
1120 struct ncsi_dev *nd = ncsi_find_dev(dev);
1121 struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
1122 struct ncsi_package *np;
1123 struct ncsi_channel *nc;
1124 struct ncsi_cmd_arg nca;
1125 bool action;
1126 int ret;
1127
1128 if (!ndp || (ipv6_addr_type(&ifa->addr) &
1129 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
1130 return NOTIFY_OK;
1131
1132 switch (event) {
1133 case NETDEV_UP:
1134 action = (++ndp->inet6_addr_num) == 1;
1135 nca.type = NCSI_PKT_CMD_EGMF;
1136 break;
1137 case NETDEV_DOWN:
1138 action = (--ndp->inet6_addr_num == 0);
1139 nca.type = NCSI_PKT_CMD_DGMF;
1140 break;
1141 default:
1142 return NOTIFY_OK;
1143 }
1144
1145 /* We might not have active channel or packages. The IPv6
1146 * required multicast will be enabled when active channel
1147 * or packages are chosen.
1148 */
1149 np = ndp->active_package;
1150 nc = ndp->active_channel;
1151 if (!action || !np || !nc)
1152 return NOTIFY_OK;
1153
1154 /* We needn't enable or disable it if the function isn't supported */
1155 if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
1156 return NOTIFY_OK;
1157
1158 nca.ndp = ndp;
Gavin Shana0509cb2016-10-04 11:25:51 +11001159 nca.req_flags = 0;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001160 nca.package = np->id;
1161 nca.channel = nc->id;
1162 nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
1163 ret = ncsi_xmit_cmd(&nca);
1164 if (ret) {
1165 netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
1166 (event == NETDEV_UP) ? "enable" : "disable", ret);
1167 return NOTIFY_DONE;
1168 }
1169
1170 return NOTIFY_OK;
1171}
1172
1173static struct notifier_block ncsi_inet6addr_notifier = {
1174 .notifier_call = ncsi_inet6addr_event,
1175};
1176#endif /* CONFIG_IPV6 */
1177
Gavin Shan2d283bd2016-07-19 11:54:16 +10001178struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
1179 void (*handler)(struct ncsi_dev *ndev))
1180{
1181 struct ncsi_dev_priv *ndp;
1182 struct ncsi_dev *nd;
1183 unsigned long flags;
1184 int i;
1185
1186 /* Check if the device has been registered or not */
1187 nd = ncsi_find_dev(dev);
1188 if (nd)
1189 return nd;
1190
1191 /* Create NCSI device */
1192 ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
1193 if (!ndp)
1194 return NULL;
1195
1196 nd = &ndp->ndev;
1197 nd->state = ncsi_dev_state_registered;
1198 nd->dev = dev;
1199 nd->handler = handler;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001200 ndp->pending_req_num = 0;
1201 INIT_LIST_HEAD(&ndp->channel_queue);
1202 INIT_WORK(&ndp->work, ncsi_dev_work);
Gavin Shan2d283bd2016-07-19 11:54:16 +10001203
1204 /* Initialize private NCSI device */
1205 spin_lock_init(&ndp->lock);
1206 INIT_LIST_HEAD(&ndp->packages);
Gavin Shana15af542016-10-04 11:25:50 +11001207 ndp->request_id = NCSI_REQ_START_IDX;
Gavin Shan2d283bd2016-07-19 11:54:16 +10001208 for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
1209 ndp->requests[i].id = i;
1210 ndp->requests[i].ndp = ndp;
1211 setup_timer(&ndp->requests[i].timer,
1212 ncsi_request_timeout,
1213 (unsigned long)&ndp->requests[i]);
1214 }
1215
1216 spin_lock_irqsave(&ncsi_dev_lock, flags);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001217#if IS_ENABLED(CONFIG_IPV6)
1218 ndp->inet6_addr_num = 0;
1219 if (list_empty(&ncsi_dev_list))
1220 register_inet6addr_notifier(&ncsi_inet6addr_notifier);
1221#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001222 list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
1223 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1224
Gavin Shane6f44ed2016-07-19 11:54:19 +10001225 /* Register NCSI packet Rx handler */
1226 ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
1227 ndp->ptype.func = ncsi_rcv_rsp;
1228 ndp->ptype.dev = dev;
1229 dev_add_pack(&ndp->ptype);
1230
Gavin Shan2d283bd2016-07-19 11:54:16 +10001231 return nd;
1232}
1233EXPORT_SYMBOL_GPL(ncsi_register_dev);
1234
Gavin Shane6f44ed2016-07-19 11:54:19 +10001235int ncsi_start_dev(struct ncsi_dev *nd)
1236{
1237 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001238 int ret;
Gavin Shane6f44ed2016-07-19 11:54:19 +10001239
1240 if (nd->state != ncsi_dev_state_registered &&
1241 nd->state != ncsi_dev_state_functional)
1242 return -ENOTTY;
1243
1244 if (!(ndp->flags & NCSI_DEV_PROBED)) {
1245 nd->state = ncsi_dev_state_probe;
1246 schedule_work(&ndp->work);
1247 return 0;
1248 }
1249
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001250 if (ndp->flags & NCSI_DEV_HWA)
1251 ret = ncsi_enable_hwa(ndp);
1252 else
1253 ret = ncsi_choose_active_channel(ndp);
1254
1255 return ret;
1256}
1257EXPORT_SYMBOL_GPL(ncsi_start_dev);
1258
1259void ncsi_stop_dev(struct ncsi_dev *nd)
1260{
1261 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1262 struct ncsi_package *np;
1263 struct ncsi_channel *nc;
1264 bool chained;
1265 int old_state;
1266 unsigned long flags;
1267
1268 /* Stop the channel monitor and reset channel's state */
Gavin Shane6f44ed2016-07-19 11:54:19 +10001269 NCSI_FOR_EACH_PACKAGE(ndp, np) {
1270 NCSI_FOR_EACH_CHANNEL(np, nc) {
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001271 ncsi_stop_channel_monitor(nc);
1272
Gavin Shand8cedaa2016-10-04 11:25:47 +11001273 spin_lock_irqsave(&nc->lock, flags);
1274 chained = !list_empty(&nc->link);
1275 old_state = nc->state;
1276 nc->state = NCSI_CHANNEL_INACTIVE;
1277 spin_unlock_irqrestore(&nc->lock, flags);
1278
1279 WARN_ON_ONCE(chained ||
Gavin Shane6f44ed2016-07-19 11:54:19 +10001280 old_state == NCSI_CHANNEL_INVISIBLE);
1281 }
1282 }
1283
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001284 ncsi_report_link(ndp, true);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001285}
Gavin Shanc0cd1ba2016-10-04 11:25:53 +11001286EXPORT_SYMBOL_GPL(ncsi_stop_dev);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001287
Gavin Shan2d283bd2016-07-19 11:54:16 +10001288void ncsi_unregister_dev(struct ncsi_dev *nd)
1289{
1290 struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
1291 struct ncsi_package *np, *tmp;
1292 unsigned long flags;
1293
Gavin Shane6f44ed2016-07-19 11:54:19 +10001294 dev_remove_pack(&ndp->ptype);
1295
Gavin Shan2d283bd2016-07-19 11:54:16 +10001296 list_for_each_entry_safe(np, tmp, &ndp->packages, node)
1297 ncsi_remove_package(np);
1298
1299 spin_lock_irqsave(&ncsi_dev_lock, flags);
1300 list_del_rcu(&ndp->node);
Gavin Shane6f44ed2016-07-19 11:54:19 +10001301#if IS_ENABLED(CONFIG_IPV6)
1302 if (list_empty(&ncsi_dev_list))
1303 unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
1304#endif
Gavin Shan2d283bd2016-07-19 11:54:16 +10001305 spin_unlock_irqrestore(&ncsi_dev_lock, flags);
1306
1307 kfree(ndp);
1308}
1309EXPORT_SYMBOL_GPL(ncsi_unregister_dev);