ncsi-manage.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /*
  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. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/netlink.h>
  15. #include <net/ncsi.h>
  16. #include <net/net_namespace.h>
  17. #include <net/sock.h>
  18. #include <net/addrconf.h>
  19. #include <net/ipv6.h>
  20. #include <net/if_inet6.h>
  21. #include "internal.h"
  22. #include "ncsi-pkt.h"
  23. LIST_HEAD(ncsi_dev_list);
  24. DEFINE_SPINLOCK(ncsi_dev_lock);
  25. static inline int ncsi_filter_size(int table)
  26. {
  27. int sizes[] = { 2, 6, 6, 6 };
  28. BUILD_BUG_ON(ARRAY_SIZE(sizes) != NCSI_FILTER_MAX);
  29. if (table < NCSI_FILTER_BASE || table >= NCSI_FILTER_MAX)
  30. return -EINVAL;
  31. return sizes[table];
  32. }
  33. int ncsi_find_filter(struct ncsi_channel *nc, int table, void *data)
  34. {
  35. struct ncsi_channel_filter *ncf;
  36. void *bitmap;
  37. int index, size;
  38. unsigned long flags;
  39. ncf = nc->filters[table];
  40. if (!ncf)
  41. return -ENXIO;
  42. size = ncsi_filter_size(table);
  43. if (size < 0)
  44. return size;
  45. spin_lock_irqsave(&nc->lock, flags);
  46. bitmap = (void *)&ncf->bitmap;
  47. index = -1;
  48. while ((index = find_next_bit(bitmap, ncf->total, index + 1))
  49. < ncf->total) {
  50. if (!memcmp(ncf->data + size * index, data, size)) {
  51. spin_unlock_irqrestore(&nc->lock, flags);
  52. return index;
  53. }
  54. }
  55. spin_unlock_irqrestore(&nc->lock, flags);
  56. return -ENOENT;
  57. }
  58. int ncsi_add_filter(struct ncsi_channel *nc, int table, void *data)
  59. {
  60. struct ncsi_channel_filter *ncf;
  61. int index, size;
  62. void *bitmap;
  63. unsigned long flags;
  64. size = ncsi_filter_size(table);
  65. if (size < 0)
  66. return size;
  67. index = ncsi_find_filter(nc, table, data);
  68. if (index >= 0)
  69. return index;
  70. ncf = nc->filters[table];
  71. if (!ncf)
  72. return -ENODEV;
  73. spin_lock_irqsave(&nc->lock, flags);
  74. bitmap = (void *)&ncf->bitmap;
  75. do {
  76. index = find_next_zero_bit(bitmap, ncf->total, 0);
  77. if (index >= ncf->total) {
  78. spin_unlock_irqrestore(&nc->lock, flags);
  79. return -ENOSPC;
  80. }
  81. } while (test_and_set_bit(index, bitmap));
  82. memcpy(ncf->data + size * index, data, size);
  83. spin_unlock_irqrestore(&nc->lock, flags);
  84. return index;
  85. }
  86. int ncsi_remove_filter(struct ncsi_channel *nc, int table, int index)
  87. {
  88. struct ncsi_channel_filter *ncf;
  89. int size;
  90. void *bitmap;
  91. unsigned long flags;
  92. size = ncsi_filter_size(table);
  93. if (size < 0)
  94. return size;
  95. ncf = nc->filters[table];
  96. if (!ncf || index >= ncf->total)
  97. return -ENODEV;
  98. spin_lock_irqsave(&nc->lock, flags);
  99. bitmap = (void *)&ncf->bitmap;
  100. if (test_and_clear_bit(index, bitmap))
  101. memset(ncf->data + size * index, 0, size);
  102. spin_unlock_irqrestore(&nc->lock, flags);
  103. return 0;
  104. }
  105. static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
  106. {
  107. struct ncsi_dev *nd = &ndp->ndev;
  108. struct ncsi_package *np;
  109. struct ncsi_channel *nc;
  110. unsigned long flags;
  111. nd->state = ncsi_dev_state_functional;
  112. if (force_down) {
  113. nd->link_up = 0;
  114. goto report;
  115. }
  116. nd->link_up = 0;
  117. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  118. NCSI_FOR_EACH_CHANNEL(np, nc) {
  119. spin_lock_irqsave(&nc->lock, flags);
  120. if (!list_empty(&nc->link) ||
  121. nc->state != NCSI_CHANNEL_ACTIVE) {
  122. spin_unlock_irqrestore(&nc->lock, flags);
  123. continue;
  124. }
  125. if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
  126. spin_unlock_irqrestore(&nc->lock, flags);
  127. nd->link_up = 1;
  128. goto report;
  129. }
  130. spin_unlock_irqrestore(&nc->lock, flags);
  131. }
  132. }
  133. report:
  134. nd->handler(nd);
  135. }
  136. static void ncsi_channel_monitor(unsigned long data)
  137. {
  138. struct ncsi_channel *nc = (struct ncsi_channel *)data;
  139. struct ncsi_package *np = nc->package;
  140. struct ncsi_dev_priv *ndp = np->ndp;
  141. struct ncsi_cmd_arg nca;
  142. bool enabled, chained;
  143. unsigned int monitor_state;
  144. unsigned long flags;
  145. int state, ret;
  146. spin_lock_irqsave(&nc->lock, flags);
  147. state = nc->state;
  148. chained = !list_empty(&nc->link);
  149. enabled = nc->monitor.enabled;
  150. monitor_state = nc->monitor.state;
  151. spin_unlock_irqrestore(&nc->lock, flags);
  152. if (!enabled || chained)
  153. return;
  154. if (state != NCSI_CHANNEL_INACTIVE &&
  155. state != NCSI_CHANNEL_ACTIVE)
  156. return;
  157. switch (monitor_state) {
  158. case NCSI_CHANNEL_MONITOR_START:
  159. case NCSI_CHANNEL_MONITOR_RETRY:
  160. nca.ndp = ndp;
  161. nca.package = np->id;
  162. nca.channel = nc->id;
  163. nca.type = NCSI_PKT_CMD_GLS;
  164. nca.req_flags = 0;
  165. ret = ncsi_xmit_cmd(&nca);
  166. if (ret) {
  167. netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
  168. ret);
  169. return;
  170. }
  171. break;
  172. case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
  173. break;
  174. default:
  175. if (!(ndp->flags & NCSI_DEV_HWA) &&
  176. state == NCSI_CHANNEL_ACTIVE) {
  177. ncsi_report_link(ndp, true);
  178. ndp->flags |= NCSI_DEV_RESHUFFLE;
  179. }
  180. spin_lock_irqsave(&nc->lock, flags);
  181. nc->state = NCSI_CHANNEL_INVISIBLE;
  182. spin_unlock_irqrestore(&nc->lock, flags);
  183. spin_lock_irqsave(&ndp->lock, flags);
  184. nc->state = NCSI_CHANNEL_INACTIVE;
  185. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  186. spin_unlock_irqrestore(&ndp->lock, flags);
  187. ncsi_process_next_channel(ndp);
  188. return;
  189. }
  190. spin_lock_irqsave(&nc->lock, flags);
  191. nc->monitor.state++;
  192. spin_unlock_irqrestore(&nc->lock, flags);
  193. mod_timer(&nc->monitor.timer, jiffies + HZ);
  194. }
  195. void ncsi_start_channel_monitor(struct ncsi_channel *nc)
  196. {
  197. unsigned long flags;
  198. spin_lock_irqsave(&nc->lock, flags);
  199. WARN_ON_ONCE(nc->monitor.enabled);
  200. nc->monitor.enabled = true;
  201. nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
  202. spin_unlock_irqrestore(&nc->lock, flags);
  203. mod_timer(&nc->monitor.timer, jiffies + HZ);
  204. }
  205. void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
  206. {
  207. unsigned long flags;
  208. spin_lock_irqsave(&nc->lock, flags);
  209. if (!nc->monitor.enabled) {
  210. spin_unlock_irqrestore(&nc->lock, flags);
  211. return;
  212. }
  213. nc->monitor.enabled = false;
  214. spin_unlock_irqrestore(&nc->lock, flags);
  215. del_timer_sync(&nc->monitor.timer);
  216. }
  217. struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
  218. unsigned char id)
  219. {
  220. struct ncsi_channel *nc;
  221. NCSI_FOR_EACH_CHANNEL(np, nc) {
  222. if (nc->id == id)
  223. return nc;
  224. }
  225. return NULL;
  226. }
  227. struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
  228. {
  229. struct ncsi_channel *nc, *tmp;
  230. int index;
  231. unsigned long flags;
  232. nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
  233. if (!nc)
  234. return NULL;
  235. nc->id = id;
  236. nc->package = np;
  237. nc->state = NCSI_CHANNEL_INACTIVE;
  238. nc->monitor.enabled = false;
  239. setup_timer(&nc->monitor.timer,
  240. ncsi_channel_monitor, (unsigned long)nc);
  241. spin_lock_init(&nc->lock);
  242. INIT_LIST_HEAD(&nc->link);
  243. for (index = 0; index < NCSI_CAP_MAX; index++)
  244. nc->caps[index].index = index;
  245. for (index = 0; index < NCSI_MODE_MAX; index++)
  246. nc->modes[index].index = index;
  247. spin_lock_irqsave(&np->lock, flags);
  248. tmp = ncsi_find_channel(np, id);
  249. if (tmp) {
  250. spin_unlock_irqrestore(&np->lock, flags);
  251. kfree(nc);
  252. return tmp;
  253. }
  254. list_add_tail_rcu(&nc->node, &np->channels);
  255. np->channel_num++;
  256. spin_unlock_irqrestore(&np->lock, flags);
  257. return nc;
  258. }
  259. static void ncsi_remove_channel(struct ncsi_channel *nc)
  260. {
  261. struct ncsi_package *np = nc->package;
  262. struct ncsi_channel_filter *ncf;
  263. unsigned long flags;
  264. int i;
  265. /* Release filters */
  266. spin_lock_irqsave(&nc->lock, flags);
  267. for (i = 0; i < NCSI_FILTER_MAX; i++) {
  268. ncf = nc->filters[i];
  269. if (!ncf)
  270. continue;
  271. nc->filters[i] = NULL;
  272. kfree(ncf);
  273. }
  274. nc->state = NCSI_CHANNEL_INACTIVE;
  275. spin_unlock_irqrestore(&nc->lock, flags);
  276. ncsi_stop_channel_monitor(nc);
  277. /* Remove and free channel */
  278. spin_lock_irqsave(&np->lock, flags);
  279. list_del_rcu(&nc->node);
  280. np->channel_num--;
  281. spin_unlock_irqrestore(&np->lock, flags);
  282. kfree(nc);
  283. }
  284. struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
  285. unsigned char id)
  286. {
  287. struct ncsi_package *np;
  288. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  289. if (np->id == id)
  290. return np;
  291. }
  292. return NULL;
  293. }
  294. struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
  295. unsigned char id)
  296. {
  297. struct ncsi_package *np, *tmp;
  298. unsigned long flags;
  299. np = kzalloc(sizeof(*np), GFP_ATOMIC);
  300. if (!np)
  301. return NULL;
  302. np->id = id;
  303. np->ndp = ndp;
  304. spin_lock_init(&np->lock);
  305. INIT_LIST_HEAD(&np->channels);
  306. spin_lock_irqsave(&ndp->lock, flags);
  307. tmp = ncsi_find_package(ndp, id);
  308. if (tmp) {
  309. spin_unlock_irqrestore(&ndp->lock, flags);
  310. kfree(np);
  311. return tmp;
  312. }
  313. list_add_tail_rcu(&np->node, &ndp->packages);
  314. ndp->package_num++;
  315. spin_unlock_irqrestore(&ndp->lock, flags);
  316. return np;
  317. }
  318. void ncsi_remove_package(struct ncsi_package *np)
  319. {
  320. struct ncsi_dev_priv *ndp = np->ndp;
  321. struct ncsi_channel *nc, *tmp;
  322. unsigned long flags;
  323. /* Release all child channels */
  324. list_for_each_entry_safe(nc, tmp, &np->channels, node)
  325. ncsi_remove_channel(nc);
  326. /* Remove and free package */
  327. spin_lock_irqsave(&ndp->lock, flags);
  328. list_del_rcu(&np->node);
  329. ndp->package_num--;
  330. spin_unlock_irqrestore(&ndp->lock, flags);
  331. kfree(np);
  332. }
  333. void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
  334. unsigned char id,
  335. struct ncsi_package **np,
  336. struct ncsi_channel **nc)
  337. {
  338. struct ncsi_package *p;
  339. struct ncsi_channel *c;
  340. p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
  341. c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
  342. if (np)
  343. *np = p;
  344. if (nc)
  345. *nc = c;
  346. }
  347. /* For two consecutive NCSI commands, the packet IDs shouldn't
  348. * be same. Otherwise, the bogus response might be replied. So
  349. * the available IDs are allocated in round-robin fashion.
  350. */
  351. struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
  352. unsigned int req_flags)
  353. {
  354. struct ncsi_request *nr = NULL;
  355. int i, limit = ARRAY_SIZE(ndp->requests);
  356. unsigned long flags;
  357. /* Check if there is one available request until the ceiling */
  358. spin_lock_irqsave(&ndp->lock, flags);
  359. for (i = ndp->request_id; i < limit; i++) {
  360. if (ndp->requests[i].used)
  361. continue;
  362. nr = &ndp->requests[i];
  363. nr->used = true;
  364. nr->flags = req_flags;
  365. ndp->request_id = i + 1;
  366. goto found;
  367. }
  368. /* Fail back to check from the starting cursor */
  369. for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
  370. if (ndp->requests[i].used)
  371. continue;
  372. nr = &ndp->requests[i];
  373. nr->used = true;
  374. nr->flags = req_flags;
  375. ndp->request_id = i + 1;
  376. goto found;
  377. }
  378. found:
  379. spin_unlock_irqrestore(&ndp->lock, flags);
  380. return nr;
  381. }
  382. void ncsi_free_request(struct ncsi_request *nr)
  383. {
  384. struct ncsi_dev_priv *ndp = nr->ndp;
  385. struct sk_buff *cmd, *rsp;
  386. unsigned long flags;
  387. bool driven;
  388. if (nr->enabled) {
  389. nr->enabled = false;
  390. del_timer_sync(&nr->timer);
  391. }
  392. spin_lock_irqsave(&ndp->lock, flags);
  393. cmd = nr->cmd;
  394. rsp = nr->rsp;
  395. nr->cmd = NULL;
  396. nr->rsp = NULL;
  397. nr->used = false;
  398. driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
  399. spin_unlock_irqrestore(&ndp->lock, flags);
  400. if (driven && cmd && --ndp->pending_req_num == 0)
  401. schedule_work(&ndp->work);
  402. /* Release command and response */
  403. consume_skb(cmd);
  404. consume_skb(rsp);
  405. }
  406. struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
  407. {
  408. struct ncsi_dev_priv *ndp;
  409. NCSI_FOR_EACH_DEV(ndp) {
  410. if (ndp->ndev.dev == dev)
  411. return &ndp->ndev;
  412. }
  413. return NULL;
  414. }
  415. static void ncsi_request_timeout(unsigned long data)
  416. {
  417. struct ncsi_request *nr = (struct ncsi_request *)data;
  418. struct ncsi_dev_priv *ndp = nr->ndp;
  419. unsigned long flags;
  420. /* If the request already had associated response,
  421. * let the response handler to release it.
  422. */
  423. spin_lock_irqsave(&ndp->lock, flags);
  424. nr->enabled = false;
  425. if (nr->rsp || !nr->cmd) {
  426. spin_unlock_irqrestore(&ndp->lock, flags);
  427. return;
  428. }
  429. spin_unlock_irqrestore(&ndp->lock, flags);
  430. /* Release the request */
  431. ncsi_free_request(nr);
  432. }
  433. static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
  434. {
  435. struct ncsi_dev *nd = &ndp->ndev;
  436. struct ncsi_package *np = ndp->active_package;
  437. struct ncsi_channel *nc = ndp->active_channel;
  438. struct ncsi_cmd_arg nca;
  439. unsigned long flags;
  440. int ret;
  441. nca.ndp = ndp;
  442. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  443. switch (nd->state) {
  444. case ncsi_dev_state_suspend:
  445. nd->state = ncsi_dev_state_suspend_select;
  446. /* Fall through */
  447. case ncsi_dev_state_suspend_select:
  448. ndp->pending_req_num = 1;
  449. nca.type = NCSI_PKT_CMD_SP;
  450. nca.package = np->id;
  451. nca.channel = NCSI_RESERVED_CHANNEL;
  452. if (ndp->flags & NCSI_DEV_HWA)
  453. nca.bytes[0] = 0;
  454. else
  455. nca.bytes[0] = 1;
  456. /* To retrieve the last link states of channels in current
  457. * package when current active channel needs fail over to
  458. * another one. It means we will possibly select another
  459. * channel as next active one. The link states of channels
  460. * are most important factor of the selection. So we need
  461. * accurate link states. Unfortunately, the link states on
  462. * inactive channels can't be updated with LSC AEN in time.
  463. */
  464. if (ndp->flags & NCSI_DEV_RESHUFFLE)
  465. nd->state = ncsi_dev_state_suspend_gls;
  466. else
  467. nd->state = ncsi_dev_state_suspend_dcnt;
  468. ret = ncsi_xmit_cmd(&nca);
  469. if (ret)
  470. goto error;
  471. break;
  472. case ncsi_dev_state_suspend_gls:
  473. ndp->pending_req_num = np->channel_num;
  474. nca.type = NCSI_PKT_CMD_GLS;
  475. nca.package = np->id;
  476. nd->state = ncsi_dev_state_suspend_dcnt;
  477. NCSI_FOR_EACH_CHANNEL(np, nc) {
  478. nca.channel = nc->id;
  479. ret = ncsi_xmit_cmd(&nca);
  480. if (ret)
  481. goto error;
  482. }
  483. break;
  484. case ncsi_dev_state_suspend_dcnt:
  485. ndp->pending_req_num = 1;
  486. nca.type = NCSI_PKT_CMD_DCNT;
  487. nca.package = np->id;
  488. nca.channel = nc->id;
  489. nd->state = ncsi_dev_state_suspend_dc;
  490. ret = ncsi_xmit_cmd(&nca);
  491. if (ret)
  492. goto error;
  493. break;
  494. case ncsi_dev_state_suspend_dc:
  495. ndp->pending_req_num = 1;
  496. nca.type = NCSI_PKT_CMD_DC;
  497. nca.package = np->id;
  498. nca.channel = nc->id;
  499. nca.bytes[0] = 1;
  500. nd->state = ncsi_dev_state_suspend_deselect;
  501. ret = ncsi_xmit_cmd(&nca);
  502. if (ret)
  503. goto error;
  504. break;
  505. case ncsi_dev_state_suspend_deselect:
  506. ndp->pending_req_num = 1;
  507. nca.type = NCSI_PKT_CMD_DP;
  508. nca.package = np->id;
  509. nca.channel = NCSI_RESERVED_CHANNEL;
  510. nd->state = ncsi_dev_state_suspend_done;
  511. ret = ncsi_xmit_cmd(&nca);
  512. if (ret)
  513. goto error;
  514. break;
  515. case ncsi_dev_state_suspend_done:
  516. spin_lock_irqsave(&nc->lock, flags);
  517. nc->state = NCSI_CHANNEL_INACTIVE;
  518. spin_unlock_irqrestore(&nc->lock, flags);
  519. ncsi_process_next_channel(ndp);
  520. break;
  521. default:
  522. netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
  523. nd->state);
  524. }
  525. return;
  526. error:
  527. nd->state = ncsi_dev_state_functional;
  528. }
  529. static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
  530. {
  531. struct ncsi_dev *nd = &ndp->ndev;
  532. struct net_device *dev = nd->dev;
  533. struct ncsi_package *np = ndp->active_package;
  534. struct ncsi_channel *nc = ndp->active_channel;
  535. struct ncsi_channel *hot_nc = NULL;
  536. struct ncsi_cmd_arg nca;
  537. unsigned char index;
  538. unsigned long flags;
  539. int ret;
  540. nca.ndp = ndp;
  541. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  542. switch (nd->state) {
  543. case ncsi_dev_state_config:
  544. case ncsi_dev_state_config_sp:
  545. ndp->pending_req_num = 1;
  546. /* Select the specific package */
  547. nca.type = NCSI_PKT_CMD_SP;
  548. if (ndp->flags & NCSI_DEV_HWA)
  549. nca.bytes[0] = 0;
  550. else
  551. nca.bytes[0] = 1;
  552. nca.package = np->id;
  553. nca.channel = NCSI_RESERVED_CHANNEL;
  554. ret = ncsi_xmit_cmd(&nca);
  555. if (ret)
  556. goto error;
  557. nd->state = ncsi_dev_state_config_cis;
  558. break;
  559. case ncsi_dev_state_config_cis:
  560. ndp->pending_req_num = 1;
  561. /* Clear initial state */
  562. nca.type = NCSI_PKT_CMD_CIS;
  563. nca.package = np->id;
  564. nca.channel = nc->id;
  565. ret = ncsi_xmit_cmd(&nca);
  566. if (ret)
  567. goto error;
  568. nd->state = ncsi_dev_state_config_sma;
  569. break;
  570. case ncsi_dev_state_config_sma:
  571. case ncsi_dev_state_config_ebf:
  572. #if IS_ENABLED(CONFIG_IPV6)
  573. case ncsi_dev_state_config_egmf:
  574. #endif
  575. case ncsi_dev_state_config_ecnt:
  576. case ncsi_dev_state_config_ec:
  577. case ncsi_dev_state_config_ae:
  578. case ncsi_dev_state_config_gls:
  579. ndp->pending_req_num = 1;
  580. nca.package = np->id;
  581. nca.channel = nc->id;
  582. /* Use first entry in unicast filter table. Note that
  583. * the MAC filter table starts from entry 1 instead of
  584. * 0.
  585. */
  586. if (nd->state == ncsi_dev_state_config_sma) {
  587. nca.type = NCSI_PKT_CMD_SMA;
  588. for (index = 0; index < 6; index++)
  589. nca.bytes[index] = dev->dev_addr[index];
  590. nca.bytes[6] = 0x1;
  591. nca.bytes[7] = 0x1;
  592. nd->state = ncsi_dev_state_config_ebf;
  593. } else if (nd->state == ncsi_dev_state_config_ebf) {
  594. nca.type = NCSI_PKT_CMD_EBF;
  595. nca.dwords[0] = nc->caps[NCSI_CAP_BC].cap;
  596. nd->state = ncsi_dev_state_config_ecnt;
  597. #if IS_ENABLED(CONFIG_IPV6)
  598. if (ndp->inet6_addr_num > 0 &&
  599. (nc->caps[NCSI_CAP_GENERIC].cap &
  600. NCSI_CAP_GENERIC_MC))
  601. nd->state = ncsi_dev_state_config_egmf;
  602. else
  603. nd->state = ncsi_dev_state_config_ecnt;
  604. } else if (nd->state == ncsi_dev_state_config_egmf) {
  605. nca.type = NCSI_PKT_CMD_EGMF;
  606. nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
  607. nd->state = ncsi_dev_state_config_ecnt;
  608. #endif /* CONFIG_IPV6 */
  609. } else if (nd->state == ncsi_dev_state_config_ecnt) {
  610. nca.type = NCSI_PKT_CMD_ECNT;
  611. nd->state = ncsi_dev_state_config_ec;
  612. } else if (nd->state == ncsi_dev_state_config_ec) {
  613. /* Enable AEN if it's supported */
  614. nca.type = NCSI_PKT_CMD_EC;
  615. nd->state = ncsi_dev_state_config_ae;
  616. if (!(nc->caps[NCSI_CAP_AEN].cap & NCSI_CAP_AEN_MASK))
  617. nd->state = ncsi_dev_state_config_gls;
  618. } else if (nd->state == ncsi_dev_state_config_ae) {
  619. nca.type = NCSI_PKT_CMD_AE;
  620. nca.bytes[0] = 0;
  621. nca.dwords[1] = nc->caps[NCSI_CAP_AEN].cap;
  622. nd->state = ncsi_dev_state_config_gls;
  623. } else if (nd->state == ncsi_dev_state_config_gls) {
  624. nca.type = NCSI_PKT_CMD_GLS;
  625. nd->state = ncsi_dev_state_config_done;
  626. }
  627. ret = ncsi_xmit_cmd(&nca);
  628. if (ret)
  629. goto error;
  630. break;
  631. case ncsi_dev_state_config_done:
  632. spin_lock_irqsave(&nc->lock, flags);
  633. if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
  634. hot_nc = nc;
  635. nc->state = NCSI_CHANNEL_ACTIVE;
  636. } else {
  637. hot_nc = NULL;
  638. nc->state = NCSI_CHANNEL_INACTIVE;
  639. }
  640. spin_unlock_irqrestore(&nc->lock, flags);
  641. /* Update the hot channel */
  642. spin_lock_irqsave(&ndp->lock, flags);
  643. ndp->hot_channel = hot_nc;
  644. spin_unlock_irqrestore(&ndp->lock, flags);
  645. ncsi_start_channel_monitor(nc);
  646. ncsi_process_next_channel(ndp);
  647. break;
  648. default:
  649. netdev_warn(dev, "Wrong NCSI state 0x%x in config\n",
  650. nd->state);
  651. }
  652. return;
  653. error:
  654. ncsi_report_link(ndp, true);
  655. }
  656. static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
  657. {
  658. struct ncsi_package *np;
  659. struct ncsi_channel *nc, *found, *hot_nc;
  660. struct ncsi_channel_mode *ncm;
  661. unsigned long flags;
  662. spin_lock_irqsave(&ndp->lock, flags);
  663. hot_nc = ndp->hot_channel;
  664. spin_unlock_irqrestore(&ndp->lock, flags);
  665. /* The search is done once an inactive channel with up
  666. * link is found.
  667. */
  668. found = NULL;
  669. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  670. NCSI_FOR_EACH_CHANNEL(np, nc) {
  671. spin_lock_irqsave(&nc->lock, flags);
  672. if (!list_empty(&nc->link) ||
  673. nc->state != NCSI_CHANNEL_INACTIVE) {
  674. spin_unlock_irqrestore(&nc->lock, flags);
  675. continue;
  676. }
  677. if (!found)
  678. found = nc;
  679. if (nc == hot_nc)
  680. found = nc;
  681. ncm = &nc->modes[NCSI_MODE_LINK];
  682. if (ncm->data[2] & 0x1) {
  683. spin_unlock_irqrestore(&nc->lock, flags);
  684. found = nc;
  685. goto out;
  686. }
  687. spin_unlock_irqrestore(&nc->lock, flags);
  688. }
  689. }
  690. if (!found) {
  691. ncsi_report_link(ndp, true);
  692. return -ENODEV;
  693. }
  694. out:
  695. spin_lock_irqsave(&ndp->lock, flags);
  696. list_add_tail_rcu(&found->link, &ndp->channel_queue);
  697. spin_unlock_irqrestore(&ndp->lock, flags);
  698. return ncsi_process_next_channel(ndp);
  699. }
  700. static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
  701. {
  702. struct ncsi_package *np;
  703. struct ncsi_channel *nc;
  704. unsigned int cap;
  705. /* The hardware arbitration is disabled if any one channel
  706. * doesn't support explicitly.
  707. */
  708. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  709. NCSI_FOR_EACH_CHANNEL(np, nc) {
  710. cap = nc->caps[NCSI_CAP_GENERIC].cap;
  711. if (!(cap & NCSI_CAP_GENERIC_HWA) ||
  712. (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
  713. NCSI_CAP_GENERIC_HWA_SUPPORT) {
  714. ndp->flags &= ~NCSI_DEV_HWA;
  715. return false;
  716. }
  717. }
  718. }
  719. ndp->flags |= NCSI_DEV_HWA;
  720. return true;
  721. }
  722. static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
  723. {
  724. struct ncsi_package *np;
  725. struct ncsi_channel *nc;
  726. unsigned long flags;
  727. /* Move all available channels to processing queue */
  728. spin_lock_irqsave(&ndp->lock, flags);
  729. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  730. NCSI_FOR_EACH_CHANNEL(np, nc) {
  731. WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
  732. !list_empty(&nc->link));
  733. ncsi_stop_channel_monitor(nc);
  734. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  735. }
  736. }
  737. spin_unlock_irqrestore(&ndp->lock, flags);
  738. /* We can have no channels in extremely case */
  739. if (list_empty(&ndp->channel_queue)) {
  740. ncsi_report_link(ndp, false);
  741. return -ENOENT;
  742. }
  743. return ncsi_process_next_channel(ndp);
  744. }
  745. static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
  746. {
  747. struct ncsi_dev *nd = &ndp->ndev;
  748. struct ncsi_package *np;
  749. struct ncsi_channel *nc;
  750. struct ncsi_cmd_arg nca;
  751. unsigned char index;
  752. int ret;
  753. nca.ndp = ndp;
  754. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  755. switch (nd->state) {
  756. case ncsi_dev_state_probe:
  757. nd->state = ncsi_dev_state_probe_deselect;
  758. /* Fall through */
  759. case ncsi_dev_state_probe_deselect:
  760. ndp->pending_req_num = 8;
  761. /* Deselect all possible packages */
  762. nca.type = NCSI_PKT_CMD_DP;
  763. nca.channel = NCSI_RESERVED_CHANNEL;
  764. for (index = 0; index < 8; index++) {
  765. nca.package = index;
  766. ret = ncsi_xmit_cmd(&nca);
  767. if (ret)
  768. goto error;
  769. }
  770. nd->state = ncsi_dev_state_probe_package;
  771. break;
  772. case ncsi_dev_state_probe_package:
  773. ndp->pending_req_num = 16;
  774. /* Select all possible packages */
  775. nca.type = NCSI_PKT_CMD_SP;
  776. nca.bytes[0] = 1;
  777. nca.channel = NCSI_RESERVED_CHANNEL;
  778. for (index = 0; index < 8; index++) {
  779. nca.package = index;
  780. ret = ncsi_xmit_cmd(&nca);
  781. if (ret)
  782. goto error;
  783. }
  784. /* Disable all possible packages */
  785. nca.type = NCSI_PKT_CMD_DP;
  786. for (index = 0; index < 8; index++) {
  787. nca.package = index;
  788. ret = ncsi_xmit_cmd(&nca);
  789. if (ret)
  790. goto error;
  791. }
  792. nd->state = ncsi_dev_state_probe_channel;
  793. break;
  794. case ncsi_dev_state_probe_channel:
  795. if (!ndp->active_package)
  796. ndp->active_package = list_first_or_null_rcu(
  797. &ndp->packages, struct ncsi_package, node);
  798. else if (list_is_last(&ndp->active_package->node,
  799. &ndp->packages))
  800. ndp->active_package = NULL;
  801. else
  802. ndp->active_package = list_next_entry(
  803. ndp->active_package, node);
  804. /* All available packages and channels are enumerated. The
  805. * enumeration happens for once when the NCSI interface is
  806. * started. So we need continue to start the interface after
  807. * the enumeration.
  808. *
  809. * We have to choose an active channel before configuring it.
  810. * Note that we possibly don't have active channel in extreme
  811. * situation.
  812. */
  813. if (!ndp->active_package) {
  814. ndp->flags |= NCSI_DEV_PROBED;
  815. if (ncsi_check_hwa(ndp))
  816. ncsi_enable_hwa(ndp);
  817. else
  818. ncsi_choose_active_channel(ndp);
  819. return;
  820. }
  821. /* Select the active package */
  822. ndp->pending_req_num = 1;
  823. nca.type = NCSI_PKT_CMD_SP;
  824. nca.bytes[0] = 1;
  825. nca.package = ndp->active_package->id;
  826. nca.channel = NCSI_RESERVED_CHANNEL;
  827. ret = ncsi_xmit_cmd(&nca);
  828. if (ret)
  829. goto error;
  830. nd->state = ncsi_dev_state_probe_cis;
  831. break;
  832. case ncsi_dev_state_probe_cis:
  833. ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
  834. /* Clear initial state */
  835. nca.type = NCSI_PKT_CMD_CIS;
  836. nca.package = ndp->active_package->id;
  837. for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
  838. nca.channel = index;
  839. ret = ncsi_xmit_cmd(&nca);
  840. if (ret)
  841. goto error;
  842. }
  843. nd->state = ncsi_dev_state_probe_gvi;
  844. break;
  845. case ncsi_dev_state_probe_gvi:
  846. case ncsi_dev_state_probe_gc:
  847. case ncsi_dev_state_probe_gls:
  848. np = ndp->active_package;
  849. ndp->pending_req_num = np->channel_num;
  850. /* Retrieve version, capability or link status */
  851. if (nd->state == ncsi_dev_state_probe_gvi)
  852. nca.type = NCSI_PKT_CMD_GVI;
  853. else if (nd->state == ncsi_dev_state_probe_gc)
  854. nca.type = NCSI_PKT_CMD_GC;
  855. else
  856. nca.type = NCSI_PKT_CMD_GLS;
  857. nca.package = np->id;
  858. NCSI_FOR_EACH_CHANNEL(np, nc) {
  859. nca.channel = nc->id;
  860. ret = ncsi_xmit_cmd(&nca);
  861. if (ret)
  862. goto error;
  863. }
  864. if (nd->state == ncsi_dev_state_probe_gvi)
  865. nd->state = ncsi_dev_state_probe_gc;
  866. else if (nd->state == ncsi_dev_state_probe_gc)
  867. nd->state = ncsi_dev_state_probe_gls;
  868. else
  869. nd->state = ncsi_dev_state_probe_dp;
  870. break;
  871. case ncsi_dev_state_probe_dp:
  872. ndp->pending_req_num = 1;
  873. /* Deselect the active package */
  874. nca.type = NCSI_PKT_CMD_DP;
  875. nca.package = ndp->active_package->id;
  876. nca.channel = NCSI_RESERVED_CHANNEL;
  877. ret = ncsi_xmit_cmd(&nca);
  878. if (ret)
  879. goto error;
  880. /* Scan channels in next package */
  881. nd->state = ncsi_dev_state_probe_channel;
  882. break;
  883. default:
  884. netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
  885. nd->state);
  886. }
  887. return;
  888. error:
  889. ncsi_report_link(ndp, true);
  890. }
  891. static void ncsi_dev_work(struct work_struct *work)
  892. {
  893. struct ncsi_dev_priv *ndp = container_of(work,
  894. struct ncsi_dev_priv, work);
  895. struct ncsi_dev *nd = &ndp->ndev;
  896. switch (nd->state & ncsi_dev_state_major) {
  897. case ncsi_dev_state_probe:
  898. ncsi_probe_channel(ndp);
  899. break;
  900. case ncsi_dev_state_suspend:
  901. ncsi_suspend_channel(ndp);
  902. break;
  903. case ncsi_dev_state_config:
  904. ncsi_configure_channel(ndp);
  905. break;
  906. default:
  907. netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
  908. nd->state);
  909. }
  910. }
  911. int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
  912. {
  913. struct ncsi_channel *nc;
  914. int old_state;
  915. unsigned long flags;
  916. spin_lock_irqsave(&ndp->lock, flags);
  917. nc = list_first_or_null_rcu(&ndp->channel_queue,
  918. struct ncsi_channel, link);
  919. if (!nc) {
  920. spin_unlock_irqrestore(&ndp->lock, flags);
  921. goto out;
  922. }
  923. list_del_init(&nc->link);
  924. spin_unlock_irqrestore(&ndp->lock, flags);
  925. spin_lock_irqsave(&nc->lock, flags);
  926. old_state = nc->state;
  927. nc->state = NCSI_CHANNEL_INVISIBLE;
  928. spin_unlock_irqrestore(&nc->lock, flags);
  929. ndp->active_channel = nc;
  930. ndp->active_package = nc->package;
  931. switch (old_state) {
  932. case NCSI_CHANNEL_INACTIVE:
  933. ndp->ndev.state = ncsi_dev_state_config;
  934. ncsi_configure_channel(ndp);
  935. break;
  936. case NCSI_CHANNEL_ACTIVE:
  937. ndp->ndev.state = ncsi_dev_state_suspend;
  938. ncsi_suspend_channel(ndp);
  939. break;
  940. default:
  941. netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
  942. old_state, nc->package->id, nc->id);
  943. ncsi_report_link(ndp, false);
  944. return -EINVAL;
  945. }
  946. return 0;
  947. out:
  948. ndp->active_channel = NULL;
  949. ndp->active_package = NULL;
  950. if (ndp->flags & NCSI_DEV_RESHUFFLE) {
  951. ndp->flags &= ~NCSI_DEV_RESHUFFLE;
  952. return ncsi_choose_active_channel(ndp);
  953. }
  954. ncsi_report_link(ndp, false);
  955. return -ENODEV;
  956. }
  957. #if IS_ENABLED(CONFIG_IPV6)
  958. static int ncsi_inet6addr_event(struct notifier_block *this,
  959. unsigned long event, void *data)
  960. {
  961. struct inet6_ifaddr *ifa = data;
  962. struct net_device *dev = ifa->idev->dev;
  963. struct ncsi_dev *nd = ncsi_find_dev(dev);
  964. struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
  965. struct ncsi_package *np;
  966. struct ncsi_channel *nc;
  967. struct ncsi_cmd_arg nca;
  968. bool action;
  969. int ret;
  970. if (!ndp || (ipv6_addr_type(&ifa->addr) &
  971. (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
  972. return NOTIFY_OK;
  973. switch (event) {
  974. case NETDEV_UP:
  975. action = (++ndp->inet6_addr_num) == 1;
  976. nca.type = NCSI_PKT_CMD_EGMF;
  977. break;
  978. case NETDEV_DOWN:
  979. action = (--ndp->inet6_addr_num == 0);
  980. nca.type = NCSI_PKT_CMD_DGMF;
  981. break;
  982. default:
  983. return NOTIFY_OK;
  984. }
  985. /* We might not have active channel or packages. The IPv6
  986. * required multicast will be enabled when active channel
  987. * or packages are chosen.
  988. */
  989. np = ndp->active_package;
  990. nc = ndp->active_channel;
  991. if (!action || !np || !nc)
  992. return NOTIFY_OK;
  993. /* We needn't enable or disable it if the function isn't supported */
  994. if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
  995. return NOTIFY_OK;
  996. nca.ndp = ndp;
  997. nca.req_flags = 0;
  998. nca.package = np->id;
  999. nca.channel = nc->id;
  1000. nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
  1001. ret = ncsi_xmit_cmd(&nca);
  1002. if (ret) {
  1003. netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
  1004. (event == NETDEV_UP) ? "enable" : "disable", ret);
  1005. return NOTIFY_DONE;
  1006. }
  1007. return NOTIFY_OK;
  1008. }
  1009. static struct notifier_block ncsi_inet6addr_notifier = {
  1010. .notifier_call = ncsi_inet6addr_event,
  1011. };
  1012. #endif /* CONFIG_IPV6 */
  1013. struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
  1014. void (*handler)(struct ncsi_dev *ndev))
  1015. {
  1016. struct ncsi_dev_priv *ndp;
  1017. struct ncsi_dev *nd;
  1018. unsigned long flags;
  1019. int i;
  1020. /* Check if the device has been registered or not */
  1021. nd = ncsi_find_dev(dev);
  1022. if (nd)
  1023. return nd;
  1024. /* Create NCSI device */
  1025. ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
  1026. if (!ndp)
  1027. return NULL;
  1028. nd = &ndp->ndev;
  1029. nd->state = ncsi_dev_state_registered;
  1030. nd->dev = dev;
  1031. nd->handler = handler;
  1032. ndp->pending_req_num = 0;
  1033. INIT_LIST_HEAD(&ndp->channel_queue);
  1034. INIT_WORK(&ndp->work, ncsi_dev_work);
  1035. /* Initialize private NCSI device */
  1036. spin_lock_init(&ndp->lock);
  1037. INIT_LIST_HEAD(&ndp->packages);
  1038. ndp->request_id = NCSI_REQ_START_IDX;
  1039. for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
  1040. ndp->requests[i].id = i;
  1041. ndp->requests[i].ndp = ndp;
  1042. setup_timer(&ndp->requests[i].timer,
  1043. ncsi_request_timeout,
  1044. (unsigned long)&ndp->requests[i]);
  1045. }
  1046. spin_lock_irqsave(&ncsi_dev_lock, flags);
  1047. #if IS_ENABLED(CONFIG_IPV6)
  1048. ndp->inet6_addr_num = 0;
  1049. if (list_empty(&ncsi_dev_list))
  1050. register_inet6addr_notifier(&ncsi_inet6addr_notifier);
  1051. #endif
  1052. list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
  1053. spin_unlock_irqrestore(&ncsi_dev_lock, flags);
  1054. /* Register NCSI packet Rx handler */
  1055. ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
  1056. ndp->ptype.func = ncsi_rcv_rsp;
  1057. ndp->ptype.dev = dev;
  1058. dev_add_pack(&ndp->ptype);
  1059. return nd;
  1060. }
  1061. EXPORT_SYMBOL_GPL(ncsi_register_dev);
  1062. int ncsi_start_dev(struct ncsi_dev *nd)
  1063. {
  1064. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1065. int ret;
  1066. if (nd->state != ncsi_dev_state_registered &&
  1067. nd->state != ncsi_dev_state_functional)
  1068. return -ENOTTY;
  1069. if (!(ndp->flags & NCSI_DEV_PROBED)) {
  1070. nd->state = ncsi_dev_state_probe;
  1071. schedule_work(&ndp->work);
  1072. return 0;
  1073. }
  1074. if (ndp->flags & NCSI_DEV_HWA)
  1075. ret = ncsi_enable_hwa(ndp);
  1076. else
  1077. ret = ncsi_choose_active_channel(ndp);
  1078. return ret;
  1079. }
  1080. EXPORT_SYMBOL_GPL(ncsi_start_dev);
  1081. void ncsi_stop_dev(struct ncsi_dev *nd)
  1082. {
  1083. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1084. struct ncsi_package *np;
  1085. struct ncsi_channel *nc;
  1086. bool chained;
  1087. int old_state;
  1088. unsigned long flags;
  1089. /* Stop the channel monitor and reset channel's state */
  1090. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  1091. NCSI_FOR_EACH_CHANNEL(np, nc) {
  1092. ncsi_stop_channel_monitor(nc);
  1093. spin_lock_irqsave(&nc->lock, flags);
  1094. chained = !list_empty(&nc->link);
  1095. old_state = nc->state;
  1096. nc->state = NCSI_CHANNEL_INACTIVE;
  1097. spin_unlock_irqrestore(&nc->lock, flags);
  1098. WARN_ON_ONCE(chained ||
  1099. old_state == NCSI_CHANNEL_INVISIBLE);
  1100. }
  1101. }
  1102. ncsi_report_link(ndp, true);
  1103. }
  1104. EXPORT_SYMBOL_GPL(ncsi_stop_dev);
  1105. void ncsi_unregister_dev(struct ncsi_dev *nd)
  1106. {
  1107. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1108. struct ncsi_package *np, *tmp;
  1109. unsigned long flags;
  1110. dev_remove_pack(&ndp->ptype);
  1111. list_for_each_entry_safe(np, tmp, &ndp->packages, node)
  1112. ncsi_remove_package(np);
  1113. spin_lock_irqsave(&ncsi_dev_lock, flags);
  1114. list_del_rcu(&ndp->node);
  1115. #if IS_ENABLED(CONFIG_IPV6)
  1116. if (list_empty(&ncsi_dev_list))
  1117. unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
  1118. #endif
  1119. spin_unlock_irqrestore(&ncsi_dev_lock, flags);
  1120. kfree(ndp);
  1121. }
  1122. EXPORT_SYMBOL_GPL(ncsi_unregister_dev);