ncsi-manage.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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 <net/ncsi.h>
  15. #include <net/net_namespace.h>
  16. #include <net/sock.h>
  17. #include <net/addrconf.h>
  18. #include <net/ipv6.h>
  19. #include <net/if_inet6.h>
  20. #include <net/genetlink.h>
  21. #include "internal.h"
  22. #include "ncsi-pkt.h"
  23. #include "ncsi-netlink.h"
  24. LIST_HEAD(ncsi_dev_list);
  25. DEFINE_SPINLOCK(ncsi_dev_lock);
  26. static void ncsi_report_link(struct ncsi_dev_priv *ndp, bool force_down)
  27. {
  28. struct ncsi_dev *nd = &ndp->ndev;
  29. struct ncsi_package *np;
  30. struct ncsi_channel *nc;
  31. unsigned long flags;
  32. nd->state = ncsi_dev_state_functional;
  33. if (force_down) {
  34. nd->link_up = 0;
  35. goto report;
  36. }
  37. nd->link_up = 0;
  38. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  39. NCSI_FOR_EACH_CHANNEL(np, nc) {
  40. spin_lock_irqsave(&nc->lock, flags);
  41. if (!list_empty(&nc->link) ||
  42. nc->state != NCSI_CHANNEL_ACTIVE) {
  43. spin_unlock_irqrestore(&nc->lock, flags);
  44. continue;
  45. }
  46. if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
  47. spin_unlock_irqrestore(&nc->lock, flags);
  48. nd->link_up = 1;
  49. goto report;
  50. }
  51. spin_unlock_irqrestore(&nc->lock, flags);
  52. }
  53. }
  54. report:
  55. nd->handler(nd);
  56. }
  57. static void ncsi_channel_monitor(struct timer_list *t)
  58. {
  59. struct ncsi_channel *nc = from_timer(nc, t, monitor.timer);
  60. struct ncsi_package *np = nc->package;
  61. struct ncsi_dev_priv *ndp = np->ndp;
  62. struct ncsi_channel_mode *ncm;
  63. struct ncsi_cmd_arg nca;
  64. bool enabled, chained;
  65. unsigned int monitor_state;
  66. unsigned long flags;
  67. int state, ret;
  68. spin_lock_irqsave(&nc->lock, flags);
  69. state = nc->state;
  70. chained = !list_empty(&nc->link);
  71. enabled = nc->monitor.enabled;
  72. monitor_state = nc->monitor.state;
  73. spin_unlock_irqrestore(&nc->lock, flags);
  74. if (!enabled || chained) {
  75. ncsi_stop_channel_monitor(nc);
  76. return;
  77. }
  78. if (state != NCSI_CHANNEL_INACTIVE &&
  79. state != NCSI_CHANNEL_ACTIVE) {
  80. ncsi_stop_channel_monitor(nc);
  81. return;
  82. }
  83. switch (monitor_state) {
  84. case NCSI_CHANNEL_MONITOR_START:
  85. case NCSI_CHANNEL_MONITOR_RETRY:
  86. nca.ndp = ndp;
  87. nca.package = np->id;
  88. nca.channel = nc->id;
  89. nca.type = NCSI_PKT_CMD_GLS;
  90. nca.req_flags = 0;
  91. ret = ncsi_xmit_cmd(&nca);
  92. if (ret)
  93. netdev_err(ndp->ndev.dev, "Error %d sending GLS\n",
  94. ret);
  95. break;
  96. case NCSI_CHANNEL_MONITOR_WAIT ... NCSI_CHANNEL_MONITOR_WAIT_MAX:
  97. break;
  98. default:
  99. netdev_err(ndp->ndev.dev, "NCSI Channel %d timed out!\n",
  100. nc->id);
  101. if (!(ndp->flags & NCSI_DEV_HWA)) {
  102. ncsi_report_link(ndp, true);
  103. ndp->flags |= NCSI_DEV_RESHUFFLE;
  104. }
  105. ncsi_stop_channel_monitor(nc);
  106. ncm = &nc->modes[NCSI_MODE_LINK];
  107. spin_lock_irqsave(&nc->lock, flags);
  108. nc->state = NCSI_CHANNEL_INVISIBLE;
  109. ncm->data[2] &= ~0x1;
  110. spin_unlock_irqrestore(&nc->lock, flags);
  111. spin_lock_irqsave(&ndp->lock, flags);
  112. nc->state = NCSI_CHANNEL_ACTIVE;
  113. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  114. spin_unlock_irqrestore(&ndp->lock, flags);
  115. ncsi_process_next_channel(ndp);
  116. return;
  117. }
  118. spin_lock_irqsave(&nc->lock, flags);
  119. nc->monitor.state++;
  120. spin_unlock_irqrestore(&nc->lock, flags);
  121. mod_timer(&nc->monitor.timer, jiffies + HZ);
  122. }
  123. void ncsi_start_channel_monitor(struct ncsi_channel *nc)
  124. {
  125. unsigned long flags;
  126. spin_lock_irqsave(&nc->lock, flags);
  127. WARN_ON_ONCE(nc->monitor.enabled);
  128. nc->monitor.enabled = true;
  129. nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
  130. spin_unlock_irqrestore(&nc->lock, flags);
  131. mod_timer(&nc->monitor.timer, jiffies + HZ);
  132. }
  133. void ncsi_stop_channel_monitor(struct ncsi_channel *nc)
  134. {
  135. unsigned long flags;
  136. spin_lock_irqsave(&nc->lock, flags);
  137. if (!nc->monitor.enabled) {
  138. spin_unlock_irqrestore(&nc->lock, flags);
  139. return;
  140. }
  141. nc->monitor.enabled = false;
  142. spin_unlock_irqrestore(&nc->lock, flags);
  143. del_timer_sync(&nc->monitor.timer);
  144. }
  145. struct ncsi_channel *ncsi_find_channel(struct ncsi_package *np,
  146. unsigned char id)
  147. {
  148. struct ncsi_channel *nc;
  149. NCSI_FOR_EACH_CHANNEL(np, nc) {
  150. if (nc->id == id)
  151. return nc;
  152. }
  153. return NULL;
  154. }
  155. struct ncsi_channel *ncsi_add_channel(struct ncsi_package *np, unsigned char id)
  156. {
  157. struct ncsi_channel *nc, *tmp;
  158. int index;
  159. unsigned long flags;
  160. nc = kzalloc(sizeof(*nc), GFP_ATOMIC);
  161. if (!nc)
  162. return NULL;
  163. nc->id = id;
  164. nc->package = np;
  165. nc->state = NCSI_CHANNEL_INACTIVE;
  166. nc->monitor.enabled = false;
  167. timer_setup(&nc->monitor.timer, ncsi_channel_monitor, 0);
  168. spin_lock_init(&nc->lock);
  169. INIT_LIST_HEAD(&nc->link);
  170. for (index = 0; index < NCSI_CAP_MAX; index++)
  171. nc->caps[index].index = index;
  172. for (index = 0; index < NCSI_MODE_MAX; index++)
  173. nc->modes[index].index = index;
  174. spin_lock_irqsave(&np->lock, flags);
  175. tmp = ncsi_find_channel(np, id);
  176. if (tmp) {
  177. spin_unlock_irqrestore(&np->lock, flags);
  178. kfree(nc);
  179. return tmp;
  180. }
  181. list_add_tail_rcu(&nc->node, &np->channels);
  182. np->channel_num++;
  183. spin_unlock_irqrestore(&np->lock, flags);
  184. return nc;
  185. }
  186. static void ncsi_remove_channel(struct ncsi_channel *nc)
  187. {
  188. struct ncsi_package *np = nc->package;
  189. unsigned long flags;
  190. spin_lock_irqsave(&nc->lock, flags);
  191. /* Release filters */
  192. kfree(nc->mac_filter.addrs);
  193. kfree(nc->vlan_filter.vids);
  194. nc->state = NCSI_CHANNEL_INACTIVE;
  195. spin_unlock_irqrestore(&nc->lock, flags);
  196. ncsi_stop_channel_monitor(nc);
  197. /* Remove and free channel */
  198. spin_lock_irqsave(&np->lock, flags);
  199. list_del_rcu(&nc->node);
  200. np->channel_num--;
  201. spin_unlock_irqrestore(&np->lock, flags);
  202. kfree(nc);
  203. }
  204. struct ncsi_package *ncsi_find_package(struct ncsi_dev_priv *ndp,
  205. unsigned char id)
  206. {
  207. struct ncsi_package *np;
  208. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  209. if (np->id == id)
  210. return np;
  211. }
  212. return NULL;
  213. }
  214. struct ncsi_package *ncsi_add_package(struct ncsi_dev_priv *ndp,
  215. unsigned char id)
  216. {
  217. struct ncsi_package *np, *tmp;
  218. unsigned long flags;
  219. np = kzalloc(sizeof(*np), GFP_ATOMIC);
  220. if (!np)
  221. return NULL;
  222. np->id = id;
  223. np->ndp = ndp;
  224. spin_lock_init(&np->lock);
  225. INIT_LIST_HEAD(&np->channels);
  226. spin_lock_irqsave(&ndp->lock, flags);
  227. tmp = ncsi_find_package(ndp, id);
  228. if (tmp) {
  229. spin_unlock_irqrestore(&ndp->lock, flags);
  230. kfree(np);
  231. return tmp;
  232. }
  233. list_add_tail_rcu(&np->node, &ndp->packages);
  234. ndp->package_num++;
  235. spin_unlock_irqrestore(&ndp->lock, flags);
  236. return np;
  237. }
  238. void ncsi_remove_package(struct ncsi_package *np)
  239. {
  240. struct ncsi_dev_priv *ndp = np->ndp;
  241. struct ncsi_channel *nc, *tmp;
  242. unsigned long flags;
  243. /* Release all child channels */
  244. list_for_each_entry_safe(nc, tmp, &np->channels, node)
  245. ncsi_remove_channel(nc);
  246. /* Remove and free package */
  247. spin_lock_irqsave(&ndp->lock, flags);
  248. list_del_rcu(&np->node);
  249. ndp->package_num--;
  250. spin_unlock_irqrestore(&ndp->lock, flags);
  251. kfree(np);
  252. }
  253. void ncsi_find_package_and_channel(struct ncsi_dev_priv *ndp,
  254. unsigned char id,
  255. struct ncsi_package **np,
  256. struct ncsi_channel **nc)
  257. {
  258. struct ncsi_package *p;
  259. struct ncsi_channel *c;
  260. p = ncsi_find_package(ndp, NCSI_PACKAGE_INDEX(id));
  261. c = p ? ncsi_find_channel(p, NCSI_CHANNEL_INDEX(id)) : NULL;
  262. if (np)
  263. *np = p;
  264. if (nc)
  265. *nc = c;
  266. }
  267. /* For two consecutive NCSI commands, the packet IDs shouldn't
  268. * be same. Otherwise, the bogus response might be replied. So
  269. * the available IDs are allocated in round-robin fashion.
  270. */
  271. struct ncsi_request *ncsi_alloc_request(struct ncsi_dev_priv *ndp,
  272. unsigned int req_flags)
  273. {
  274. struct ncsi_request *nr = NULL;
  275. int i, limit = ARRAY_SIZE(ndp->requests);
  276. unsigned long flags;
  277. /* Check if there is one available request until the ceiling */
  278. spin_lock_irqsave(&ndp->lock, flags);
  279. for (i = ndp->request_id; i < limit; i++) {
  280. if (ndp->requests[i].used)
  281. continue;
  282. nr = &ndp->requests[i];
  283. nr->used = true;
  284. nr->flags = req_flags;
  285. ndp->request_id = i + 1;
  286. goto found;
  287. }
  288. /* Fail back to check from the starting cursor */
  289. for (i = NCSI_REQ_START_IDX; i < ndp->request_id; i++) {
  290. if (ndp->requests[i].used)
  291. continue;
  292. nr = &ndp->requests[i];
  293. nr->used = true;
  294. nr->flags = req_flags;
  295. ndp->request_id = i + 1;
  296. goto found;
  297. }
  298. found:
  299. spin_unlock_irqrestore(&ndp->lock, flags);
  300. return nr;
  301. }
  302. void ncsi_free_request(struct ncsi_request *nr)
  303. {
  304. struct ncsi_dev_priv *ndp = nr->ndp;
  305. struct sk_buff *cmd, *rsp;
  306. unsigned long flags;
  307. bool driven;
  308. if (nr->enabled) {
  309. nr->enabled = false;
  310. del_timer_sync(&nr->timer);
  311. }
  312. spin_lock_irqsave(&ndp->lock, flags);
  313. cmd = nr->cmd;
  314. rsp = nr->rsp;
  315. nr->cmd = NULL;
  316. nr->rsp = NULL;
  317. nr->used = false;
  318. driven = !!(nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN);
  319. spin_unlock_irqrestore(&ndp->lock, flags);
  320. if (driven && cmd && --ndp->pending_req_num == 0)
  321. schedule_work(&ndp->work);
  322. /* Release command and response */
  323. consume_skb(cmd);
  324. consume_skb(rsp);
  325. }
  326. struct ncsi_dev *ncsi_find_dev(struct net_device *dev)
  327. {
  328. struct ncsi_dev_priv *ndp;
  329. NCSI_FOR_EACH_DEV(ndp) {
  330. if (ndp->ndev.dev == dev)
  331. return &ndp->ndev;
  332. }
  333. return NULL;
  334. }
  335. static void ncsi_request_timeout(struct timer_list *t)
  336. {
  337. struct ncsi_request *nr = from_timer(nr, t, timer);
  338. struct ncsi_dev_priv *ndp = nr->ndp;
  339. struct ncsi_cmd_pkt *cmd;
  340. struct ncsi_package *np;
  341. struct ncsi_channel *nc;
  342. unsigned long flags;
  343. /* If the request already had associated response,
  344. * let the response handler to release it.
  345. */
  346. spin_lock_irqsave(&ndp->lock, flags);
  347. nr->enabled = false;
  348. if (nr->rsp || !nr->cmd) {
  349. spin_unlock_irqrestore(&ndp->lock, flags);
  350. return;
  351. }
  352. spin_unlock_irqrestore(&ndp->lock, flags);
  353. if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
  354. if (nr->cmd) {
  355. /* Find the package */
  356. cmd = (struct ncsi_cmd_pkt *)
  357. skb_network_header(nr->cmd);
  358. ncsi_find_package_and_channel(ndp,
  359. cmd->cmd.common.channel,
  360. &np, &nc);
  361. ncsi_send_netlink_timeout(nr, np, nc);
  362. }
  363. }
  364. /* Release the request */
  365. ncsi_free_request(nr);
  366. }
  367. static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
  368. {
  369. struct ncsi_dev *nd = &ndp->ndev;
  370. struct ncsi_package *np = ndp->active_package;
  371. struct ncsi_channel *nc = ndp->active_channel;
  372. struct ncsi_cmd_arg nca;
  373. unsigned long flags;
  374. int ret;
  375. nca.ndp = ndp;
  376. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  377. switch (nd->state) {
  378. case ncsi_dev_state_suspend:
  379. nd->state = ncsi_dev_state_suspend_select;
  380. /* Fall through */
  381. case ncsi_dev_state_suspend_select:
  382. ndp->pending_req_num = 1;
  383. nca.type = NCSI_PKT_CMD_SP;
  384. nca.package = np->id;
  385. nca.channel = NCSI_RESERVED_CHANNEL;
  386. if (ndp->flags & NCSI_DEV_HWA)
  387. nca.bytes[0] = 0;
  388. else
  389. nca.bytes[0] = 1;
  390. /* To retrieve the last link states of channels in current
  391. * package when current active channel needs fail over to
  392. * another one. It means we will possibly select another
  393. * channel as next active one. The link states of channels
  394. * are most important factor of the selection. So we need
  395. * accurate link states. Unfortunately, the link states on
  396. * inactive channels can't be updated with LSC AEN in time.
  397. */
  398. if (ndp->flags & NCSI_DEV_RESHUFFLE)
  399. nd->state = ncsi_dev_state_suspend_gls;
  400. else
  401. nd->state = ncsi_dev_state_suspend_dcnt;
  402. ret = ncsi_xmit_cmd(&nca);
  403. if (ret)
  404. goto error;
  405. break;
  406. case ncsi_dev_state_suspend_gls:
  407. ndp->pending_req_num = np->channel_num;
  408. nca.type = NCSI_PKT_CMD_GLS;
  409. nca.package = np->id;
  410. nd->state = ncsi_dev_state_suspend_dcnt;
  411. NCSI_FOR_EACH_CHANNEL(np, nc) {
  412. nca.channel = nc->id;
  413. ret = ncsi_xmit_cmd(&nca);
  414. if (ret)
  415. goto error;
  416. }
  417. break;
  418. case ncsi_dev_state_suspend_dcnt:
  419. ndp->pending_req_num = 1;
  420. nca.type = NCSI_PKT_CMD_DCNT;
  421. nca.package = np->id;
  422. nca.channel = nc->id;
  423. nd->state = ncsi_dev_state_suspend_dc;
  424. ret = ncsi_xmit_cmd(&nca);
  425. if (ret)
  426. goto error;
  427. break;
  428. case ncsi_dev_state_suspend_dc:
  429. ndp->pending_req_num = 1;
  430. nca.type = NCSI_PKT_CMD_DC;
  431. nca.package = np->id;
  432. nca.channel = nc->id;
  433. nca.bytes[0] = 1;
  434. nd->state = ncsi_dev_state_suspend_deselect;
  435. ret = ncsi_xmit_cmd(&nca);
  436. if (ret)
  437. goto error;
  438. break;
  439. case ncsi_dev_state_suspend_deselect:
  440. ndp->pending_req_num = 1;
  441. nca.type = NCSI_PKT_CMD_DP;
  442. nca.package = np->id;
  443. nca.channel = NCSI_RESERVED_CHANNEL;
  444. nd->state = ncsi_dev_state_suspend_done;
  445. ret = ncsi_xmit_cmd(&nca);
  446. if (ret)
  447. goto error;
  448. break;
  449. case ncsi_dev_state_suspend_done:
  450. spin_lock_irqsave(&nc->lock, flags);
  451. nc->state = NCSI_CHANNEL_INACTIVE;
  452. spin_unlock_irqrestore(&nc->lock, flags);
  453. ncsi_process_next_channel(ndp);
  454. break;
  455. default:
  456. netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
  457. nd->state);
  458. }
  459. return;
  460. error:
  461. nd->state = ncsi_dev_state_functional;
  462. }
  463. /* Check the VLAN filter bitmap for a set filter, and construct a
  464. * "Set VLAN Filter - Disable" packet if found.
  465. */
  466. static int clear_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
  467. struct ncsi_cmd_arg *nca)
  468. {
  469. struct ncsi_channel_vlan_filter *ncf;
  470. unsigned long flags;
  471. void *bitmap;
  472. int index;
  473. u16 vid;
  474. ncf = &nc->vlan_filter;
  475. bitmap = &ncf->bitmap;
  476. spin_lock_irqsave(&nc->lock, flags);
  477. index = find_next_bit(bitmap, ncf->n_vids, 0);
  478. if (index >= ncf->n_vids) {
  479. spin_unlock_irqrestore(&nc->lock, flags);
  480. return -1;
  481. }
  482. vid = ncf->vids[index];
  483. clear_bit(index, bitmap);
  484. ncf->vids[index] = 0;
  485. spin_unlock_irqrestore(&nc->lock, flags);
  486. nca->type = NCSI_PKT_CMD_SVF;
  487. nca->words[1] = vid;
  488. /* HW filter index starts at 1 */
  489. nca->bytes[6] = index + 1;
  490. nca->bytes[7] = 0x00;
  491. return 0;
  492. }
  493. /* Find an outstanding VLAN tag and constuct a "Set VLAN Filter - Enable"
  494. * packet.
  495. */
  496. static int set_one_vid(struct ncsi_dev_priv *ndp, struct ncsi_channel *nc,
  497. struct ncsi_cmd_arg *nca)
  498. {
  499. struct ncsi_channel_vlan_filter *ncf;
  500. struct vlan_vid *vlan = NULL;
  501. unsigned long flags;
  502. int i, index;
  503. void *bitmap;
  504. u16 vid;
  505. if (list_empty(&ndp->vlan_vids))
  506. return -1;
  507. ncf = &nc->vlan_filter;
  508. bitmap = &ncf->bitmap;
  509. spin_lock_irqsave(&nc->lock, flags);
  510. rcu_read_lock();
  511. list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
  512. vid = vlan->vid;
  513. for (i = 0; i < ncf->n_vids; i++)
  514. if (ncf->vids[i] == vid) {
  515. vid = 0;
  516. break;
  517. }
  518. if (vid)
  519. break;
  520. }
  521. rcu_read_unlock();
  522. if (!vid) {
  523. /* No VLAN ID is not set */
  524. spin_unlock_irqrestore(&nc->lock, flags);
  525. return -1;
  526. }
  527. index = find_next_zero_bit(bitmap, ncf->n_vids, 0);
  528. if (index < 0 || index >= ncf->n_vids) {
  529. netdev_err(ndp->ndev.dev,
  530. "Channel %u already has all VLAN filters set\n",
  531. nc->id);
  532. spin_unlock_irqrestore(&nc->lock, flags);
  533. return -1;
  534. }
  535. ncf->vids[index] = vid;
  536. set_bit(index, bitmap);
  537. spin_unlock_irqrestore(&nc->lock, flags);
  538. nca->type = NCSI_PKT_CMD_SVF;
  539. nca->words[1] = vid;
  540. /* HW filter index starts at 1 */
  541. nca->bytes[6] = index + 1;
  542. nca->bytes[7] = 0x01;
  543. return 0;
  544. }
  545. #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
  546. /* NCSI OEM Command APIs */
  547. static int ncsi_oem_gma_handler_bcm(struct ncsi_cmd_arg *nca)
  548. {
  549. unsigned char data[NCSI_OEM_BCM_CMD_GMA_LEN];
  550. int ret = 0;
  551. nca->payload = NCSI_OEM_BCM_CMD_GMA_LEN;
  552. memset(data, 0, NCSI_OEM_BCM_CMD_GMA_LEN);
  553. *(unsigned int *)data = ntohl(NCSI_OEM_MFR_BCM_ID);
  554. data[5] = NCSI_OEM_BCM_CMD_GMA;
  555. nca->data = data;
  556. ret = ncsi_xmit_cmd(nca);
  557. if (ret)
  558. netdev_err(nca->ndp->ndev.dev,
  559. "NCSI: Failed to transmit cmd 0x%x during configure\n",
  560. nca->type);
  561. return ret;
  562. }
  563. /* OEM Command handlers initialization */
  564. static struct ncsi_oem_gma_handler {
  565. unsigned int mfr_id;
  566. int (*handler)(struct ncsi_cmd_arg *nca);
  567. } ncsi_oem_gma_handlers[] = {
  568. { NCSI_OEM_MFR_BCM_ID, ncsi_oem_gma_handler_bcm }
  569. };
  570. static int ncsi_gma_handler(struct ncsi_cmd_arg *nca, unsigned int mf_id)
  571. {
  572. struct ncsi_oem_gma_handler *nch = NULL;
  573. int i;
  574. /* This function should only be called once, return if flag set */
  575. if (nca->ndp->gma_flag == 1)
  576. return -1;
  577. /* Find gma handler for given manufacturer id */
  578. for (i = 0; i < ARRAY_SIZE(ncsi_oem_gma_handlers); i++) {
  579. if (ncsi_oem_gma_handlers[i].mfr_id == mf_id) {
  580. if (ncsi_oem_gma_handlers[i].handler)
  581. nch = &ncsi_oem_gma_handlers[i];
  582. break;
  583. }
  584. }
  585. if (!nch) {
  586. netdev_err(nca->ndp->ndev.dev,
  587. "NCSI: No GMA handler available for MFR-ID (0x%x)\n",
  588. mf_id);
  589. return -1;
  590. }
  591. /* Set the flag for GMA command which should only be called once */
  592. nca->ndp->gma_flag = 1;
  593. /* Get Mac address from NCSI device */
  594. return nch->handler(nca);
  595. }
  596. #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
  597. static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
  598. {
  599. struct ncsi_dev *nd = &ndp->ndev;
  600. struct net_device *dev = nd->dev;
  601. struct ncsi_package *np = ndp->active_package;
  602. struct ncsi_channel *nc = ndp->active_channel;
  603. struct ncsi_channel *hot_nc = NULL;
  604. struct ncsi_cmd_arg nca;
  605. unsigned char index;
  606. unsigned long flags;
  607. int ret;
  608. nca.ndp = ndp;
  609. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  610. switch (nd->state) {
  611. case ncsi_dev_state_config:
  612. case ncsi_dev_state_config_sp:
  613. ndp->pending_req_num = 1;
  614. /* Select the specific package */
  615. nca.type = NCSI_PKT_CMD_SP;
  616. if (ndp->flags & NCSI_DEV_HWA)
  617. nca.bytes[0] = 0;
  618. else
  619. nca.bytes[0] = 1;
  620. nca.package = np->id;
  621. nca.channel = NCSI_RESERVED_CHANNEL;
  622. ret = ncsi_xmit_cmd(&nca);
  623. if (ret) {
  624. netdev_err(ndp->ndev.dev,
  625. "NCSI: Failed to transmit CMD_SP\n");
  626. goto error;
  627. }
  628. nd->state = ncsi_dev_state_config_cis;
  629. break;
  630. case ncsi_dev_state_config_cis:
  631. ndp->pending_req_num = 1;
  632. /* Clear initial state */
  633. nca.type = NCSI_PKT_CMD_CIS;
  634. nca.package = np->id;
  635. nca.channel = nc->id;
  636. ret = ncsi_xmit_cmd(&nca);
  637. if (ret) {
  638. netdev_err(ndp->ndev.dev,
  639. "NCSI: Failed to transmit CMD_CIS\n");
  640. goto error;
  641. }
  642. nd->state = ncsi_dev_state_config_oem_gma;
  643. break;
  644. case ncsi_dev_state_config_oem_gma:
  645. nd->state = ncsi_dev_state_config_clear_vids;
  646. ret = -1;
  647. #if IS_ENABLED(CONFIG_NCSI_OEM_CMD_GET_MAC)
  648. nca.type = NCSI_PKT_CMD_OEM;
  649. nca.package = np->id;
  650. nca.channel = nc->id;
  651. ndp->pending_req_num = 1;
  652. ret = ncsi_gma_handler(&nca, nc->version.mf_id);
  653. #endif /* CONFIG_NCSI_OEM_CMD_GET_MAC */
  654. if (ret < 0)
  655. schedule_work(&ndp->work);
  656. break;
  657. case ncsi_dev_state_config_clear_vids:
  658. case ncsi_dev_state_config_svf:
  659. case ncsi_dev_state_config_ev:
  660. case ncsi_dev_state_config_sma:
  661. case ncsi_dev_state_config_ebf:
  662. #if IS_ENABLED(CONFIG_IPV6)
  663. case ncsi_dev_state_config_egmf:
  664. #endif
  665. case ncsi_dev_state_config_ecnt:
  666. case ncsi_dev_state_config_ec:
  667. case ncsi_dev_state_config_ae:
  668. case ncsi_dev_state_config_gls:
  669. ndp->pending_req_num = 1;
  670. nca.package = np->id;
  671. nca.channel = nc->id;
  672. /* Clear any active filters on the channel before setting */
  673. if (nd->state == ncsi_dev_state_config_clear_vids) {
  674. ret = clear_one_vid(ndp, nc, &nca);
  675. if (ret) {
  676. nd->state = ncsi_dev_state_config_svf;
  677. schedule_work(&ndp->work);
  678. break;
  679. }
  680. /* Repeat */
  681. nd->state = ncsi_dev_state_config_clear_vids;
  682. /* Add known VLAN tags to the filter */
  683. } else if (nd->state == ncsi_dev_state_config_svf) {
  684. ret = set_one_vid(ndp, nc, &nca);
  685. if (ret) {
  686. nd->state = ncsi_dev_state_config_ev;
  687. schedule_work(&ndp->work);
  688. break;
  689. }
  690. /* Repeat */
  691. nd->state = ncsi_dev_state_config_svf;
  692. /* Enable/Disable the VLAN filter */
  693. } else if (nd->state == ncsi_dev_state_config_ev) {
  694. if (list_empty(&ndp->vlan_vids)) {
  695. nca.type = NCSI_PKT_CMD_DV;
  696. } else {
  697. nca.type = NCSI_PKT_CMD_EV;
  698. nca.bytes[3] = NCSI_CAP_VLAN_NO;
  699. }
  700. nd->state = ncsi_dev_state_config_sma;
  701. } else if (nd->state == ncsi_dev_state_config_sma) {
  702. /* Use first entry in unicast filter table. Note that
  703. * the MAC filter table starts from entry 1 instead of
  704. * 0.
  705. */
  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. ret = ncsi_xmit_cmd(&nca);
  747. if (ret) {
  748. netdev_err(ndp->ndev.dev,
  749. "NCSI: Failed to transmit CMD %x\n",
  750. nca.type);
  751. goto error;
  752. }
  753. break;
  754. case ncsi_dev_state_config_done:
  755. netdev_dbg(ndp->ndev.dev, "NCSI: channel %u config done\n",
  756. nc->id);
  757. spin_lock_irqsave(&nc->lock, flags);
  758. if (nc->reconfigure_needed) {
  759. /* This channel's configuration has been updated
  760. * part-way during the config state - start the
  761. * channel configuration over
  762. */
  763. nc->reconfigure_needed = false;
  764. nc->state = NCSI_CHANNEL_INACTIVE;
  765. spin_unlock_irqrestore(&nc->lock, flags);
  766. spin_lock_irqsave(&ndp->lock, flags);
  767. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  768. spin_unlock_irqrestore(&ndp->lock, flags);
  769. netdev_dbg(dev, "Dirty NCSI channel state reset\n");
  770. ncsi_process_next_channel(ndp);
  771. break;
  772. }
  773. if (nc->modes[NCSI_MODE_LINK].data[2] & 0x1) {
  774. hot_nc = nc;
  775. nc->state = NCSI_CHANNEL_ACTIVE;
  776. } else {
  777. hot_nc = NULL;
  778. nc->state = NCSI_CHANNEL_INACTIVE;
  779. netdev_dbg(ndp->ndev.dev,
  780. "NCSI: channel %u link down after config\n",
  781. nc->id);
  782. }
  783. spin_unlock_irqrestore(&nc->lock, flags);
  784. /* Update the hot channel */
  785. spin_lock_irqsave(&ndp->lock, flags);
  786. ndp->hot_channel = hot_nc;
  787. spin_unlock_irqrestore(&ndp->lock, flags);
  788. ncsi_start_channel_monitor(nc);
  789. ncsi_process_next_channel(ndp);
  790. break;
  791. default:
  792. netdev_alert(dev, "Wrong NCSI state 0x%x in config\n",
  793. nd->state);
  794. }
  795. return;
  796. error:
  797. ncsi_report_link(ndp, true);
  798. }
  799. static int ncsi_choose_active_channel(struct ncsi_dev_priv *ndp)
  800. {
  801. struct ncsi_package *np, *force_package;
  802. struct ncsi_channel *nc, *found, *hot_nc, *force_channel;
  803. struct ncsi_channel_mode *ncm;
  804. unsigned long flags;
  805. spin_lock_irqsave(&ndp->lock, flags);
  806. hot_nc = ndp->hot_channel;
  807. force_channel = ndp->force_channel;
  808. force_package = ndp->force_package;
  809. spin_unlock_irqrestore(&ndp->lock, flags);
  810. /* Force a specific channel whether or not it has link if we have been
  811. * configured to do so
  812. */
  813. if (force_package && force_channel) {
  814. found = force_channel;
  815. ncm = &found->modes[NCSI_MODE_LINK];
  816. if (!(ncm->data[2] & 0x1))
  817. netdev_info(ndp->ndev.dev,
  818. "NCSI: Channel %u forced, but it is link down\n",
  819. found->id);
  820. goto out;
  821. }
  822. /* The search is done once an inactive channel with up
  823. * link is found.
  824. */
  825. found = NULL;
  826. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  827. if (ndp->force_package && np != ndp->force_package)
  828. continue;
  829. NCSI_FOR_EACH_CHANNEL(np, nc) {
  830. spin_lock_irqsave(&nc->lock, flags);
  831. if (!list_empty(&nc->link) ||
  832. nc->state != NCSI_CHANNEL_INACTIVE) {
  833. spin_unlock_irqrestore(&nc->lock, flags);
  834. continue;
  835. }
  836. if (!found)
  837. found = nc;
  838. if (nc == hot_nc)
  839. found = nc;
  840. ncm = &nc->modes[NCSI_MODE_LINK];
  841. if (ncm->data[2] & 0x1) {
  842. spin_unlock_irqrestore(&nc->lock, flags);
  843. found = nc;
  844. goto out;
  845. }
  846. spin_unlock_irqrestore(&nc->lock, flags);
  847. }
  848. }
  849. if (!found) {
  850. netdev_warn(ndp->ndev.dev,
  851. "NCSI: No channel found with link\n");
  852. ncsi_report_link(ndp, true);
  853. return -ENODEV;
  854. }
  855. ncm = &found->modes[NCSI_MODE_LINK];
  856. netdev_dbg(ndp->ndev.dev,
  857. "NCSI: Channel %u added to queue (link %s)\n",
  858. found->id, ncm->data[2] & 0x1 ? "up" : "down");
  859. out:
  860. spin_lock_irqsave(&ndp->lock, flags);
  861. list_add_tail_rcu(&found->link, &ndp->channel_queue);
  862. spin_unlock_irqrestore(&ndp->lock, flags);
  863. return ncsi_process_next_channel(ndp);
  864. }
  865. static bool ncsi_check_hwa(struct ncsi_dev_priv *ndp)
  866. {
  867. struct ncsi_package *np;
  868. struct ncsi_channel *nc;
  869. unsigned int cap;
  870. bool has_channel = false;
  871. /* The hardware arbitration is disabled if any one channel
  872. * doesn't support explicitly.
  873. */
  874. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  875. NCSI_FOR_EACH_CHANNEL(np, nc) {
  876. has_channel = true;
  877. cap = nc->caps[NCSI_CAP_GENERIC].cap;
  878. if (!(cap & NCSI_CAP_GENERIC_HWA) ||
  879. (cap & NCSI_CAP_GENERIC_HWA_MASK) !=
  880. NCSI_CAP_GENERIC_HWA_SUPPORT) {
  881. ndp->flags &= ~NCSI_DEV_HWA;
  882. return false;
  883. }
  884. }
  885. }
  886. if (has_channel) {
  887. ndp->flags |= NCSI_DEV_HWA;
  888. return true;
  889. }
  890. ndp->flags &= ~NCSI_DEV_HWA;
  891. return false;
  892. }
  893. static int ncsi_enable_hwa(struct ncsi_dev_priv *ndp)
  894. {
  895. struct ncsi_package *np;
  896. struct ncsi_channel *nc;
  897. unsigned long flags;
  898. /* Move all available channels to processing queue */
  899. spin_lock_irqsave(&ndp->lock, flags);
  900. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  901. NCSI_FOR_EACH_CHANNEL(np, nc) {
  902. WARN_ON_ONCE(nc->state != NCSI_CHANNEL_INACTIVE ||
  903. !list_empty(&nc->link));
  904. ncsi_stop_channel_monitor(nc);
  905. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  906. }
  907. }
  908. spin_unlock_irqrestore(&ndp->lock, flags);
  909. /* We can have no channels in extremely case */
  910. if (list_empty(&ndp->channel_queue)) {
  911. netdev_err(ndp->ndev.dev,
  912. "NCSI: No available channels for HWA\n");
  913. ncsi_report_link(ndp, false);
  914. return -ENOENT;
  915. }
  916. return ncsi_process_next_channel(ndp);
  917. }
  918. static void ncsi_probe_channel(struct ncsi_dev_priv *ndp)
  919. {
  920. struct ncsi_dev *nd = &ndp->ndev;
  921. struct ncsi_package *np;
  922. struct ncsi_channel *nc;
  923. struct ncsi_cmd_arg nca;
  924. unsigned char index;
  925. int ret;
  926. nca.ndp = ndp;
  927. nca.req_flags = NCSI_REQ_FLAG_EVENT_DRIVEN;
  928. switch (nd->state) {
  929. case ncsi_dev_state_probe:
  930. nd->state = ncsi_dev_state_probe_deselect;
  931. /* Fall through */
  932. case ncsi_dev_state_probe_deselect:
  933. ndp->pending_req_num = 8;
  934. /* Deselect all possible packages */
  935. nca.type = NCSI_PKT_CMD_DP;
  936. nca.channel = NCSI_RESERVED_CHANNEL;
  937. for (index = 0; index < 8; index++) {
  938. nca.package = index;
  939. ret = ncsi_xmit_cmd(&nca);
  940. if (ret)
  941. goto error;
  942. }
  943. nd->state = ncsi_dev_state_probe_package;
  944. break;
  945. case ncsi_dev_state_probe_package:
  946. ndp->pending_req_num = 16;
  947. /* Select all possible packages */
  948. nca.type = NCSI_PKT_CMD_SP;
  949. nca.bytes[0] = 1;
  950. nca.channel = NCSI_RESERVED_CHANNEL;
  951. for (index = 0; index < 8; index++) {
  952. nca.package = index;
  953. ret = ncsi_xmit_cmd(&nca);
  954. if (ret)
  955. goto error;
  956. }
  957. /* Disable all possible packages */
  958. nca.type = NCSI_PKT_CMD_DP;
  959. for (index = 0; index < 8; index++) {
  960. nca.package = index;
  961. ret = ncsi_xmit_cmd(&nca);
  962. if (ret)
  963. goto error;
  964. }
  965. nd->state = ncsi_dev_state_probe_channel;
  966. break;
  967. case ncsi_dev_state_probe_channel:
  968. if (!ndp->active_package)
  969. ndp->active_package = list_first_or_null_rcu(
  970. &ndp->packages, struct ncsi_package, node);
  971. else if (list_is_last(&ndp->active_package->node,
  972. &ndp->packages))
  973. ndp->active_package = NULL;
  974. else
  975. ndp->active_package = list_next_entry(
  976. ndp->active_package, node);
  977. /* All available packages and channels are enumerated. The
  978. * enumeration happens for once when the NCSI interface is
  979. * started. So we need continue to start the interface after
  980. * the enumeration.
  981. *
  982. * We have to choose an active channel before configuring it.
  983. * Note that we possibly don't have active channel in extreme
  984. * situation.
  985. */
  986. if (!ndp->active_package) {
  987. ndp->flags |= NCSI_DEV_PROBED;
  988. if (ncsi_check_hwa(ndp))
  989. ncsi_enable_hwa(ndp);
  990. else
  991. ncsi_choose_active_channel(ndp);
  992. return;
  993. }
  994. /* Select the active package */
  995. ndp->pending_req_num = 1;
  996. nca.type = NCSI_PKT_CMD_SP;
  997. nca.bytes[0] = 1;
  998. nca.package = ndp->active_package->id;
  999. nca.channel = NCSI_RESERVED_CHANNEL;
  1000. ret = ncsi_xmit_cmd(&nca);
  1001. if (ret)
  1002. goto error;
  1003. nd->state = ncsi_dev_state_probe_cis;
  1004. break;
  1005. case ncsi_dev_state_probe_cis:
  1006. ndp->pending_req_num = NCSI_RESERVED_CHANNEL;
  1007. /* Clear initial state */
  1008. nca.type = NCSI_PKT_CMD_CIS;
  1009. nca.package = ndp->active_package->id;
  1010. for (index = 0; index < NCSI_RESERVED_CHANNEL; index++) {
  1011. nca.channel = index;
  1012. ret = ncsi_xmit_cmd(&nca);
  1013. if (ret)
  1014. goto error;
  1015. }
  1016. nd->state = ncsi_dev_state_probe_gvi;
  1017. break;
  1018. case ncsi_dev_state_probe_gvi:
  1019. case ncsi_dev_state_probe_gc:
  1020. case ncsi_dev_state_probe_gls:
  1021. np = ndp->active_package;
  1022. ndp->pending_req_num = np->channel_num;
  1023. /* Retrieve version, capability or link status */
  1024. if (nd->state == ncsi_dev_state_probe_gvi)
  1025. nca.type = NCSI_PKT_CMD_GVI;
  1026. else if (nd->state == ncsi_dev_state_probe_gc)
  1027. nca.type = NCSI_PKT_CMD_GC;
  1028. else
  1029. nca.type = NCSI_PKT_CMD_GLS;
  1030. nca.package = np->id;
  1031. NCSI_FOR_EACH_CHANNEL(np, nc) {
  1032. nca.channel = nc->id;
  1033. ret = ncsi_xmit_cmd(&nca);
  1034. if (ret)
  1035. goto error;
  1036. }
  1037. if (nd->state == ncsi_dev_state_probe_gvi)
  1038. nd->state = ncsi_dev_state_probe_gc;
  1039. else if (nd->state == ncsi_dev_state_probe_gc)
  1040. nd->state = ncsi_dev_state_probe_gls;
  1041. else
  1042. nd->state = ncsi_dev_state_probe_dp;
  1043. break;
  1044. case ncsi_dev_state_probe_dp:
  1045. ndp->pending_req_num = 1;
  1046. /* Deselect the active package */
  1047. nca.type = NCSI_PKT_CMD_DP;
  1048. nca.package = ndp->active_package->id;
  1049. nca.channel = NCSI_RESERVED_CHANNEL;
  1050. ret = ncsi_xmit_cmd(&nca);
  1051. if (ret)
  1052. goto error;
  1053. /* Scan channels in next package */
  1054. nd->state = ncsi_dev_state_probe_channel;
  1055. break;
  1056. default:
  1057. netdev_warn(nd->dev, "Wrong NCSI state 0x%0x in enumeration\n",
  1058. nd->state);
  1059. }
  1060. return;
  1061. error:
  1062. netdev_err(ndp->ndev.dev,
  1063. "NCSI: Failed to transmit cmd 0x%x during probe\n",
  1064. nca.type);
  1065. ncsi_report_link(ndp, true);
  1066. }
  1067. static void ncsi_dev_work(struct work_struct *work)
  1068. {
  1069. struct ncsi_dev_priv *ndp = container_of(work,
  1070. struct ncsi_dev_priv, work);
  1071. struct ncsi_dev *nd = &ndp->ndev;
  1072. switch (nd->state & ncsi_dev_state_major) {
  1073. case ncsi_dev_state_probe:
  1074. ncsi_probe_channel(ndp);
  1075. break;
  1076. case ncsi_dev_state_suspend:
  1077. ncsi_suspend_channel(ndp);
  1078. break;
  1079. case ncsi_dev_state_config:
  1080. ncsi_configure_channel(ndp);
  1081. break;
  1082. default:
  1083. netdev_warn(nd->dev, "Wrong NCSI state 0x%x in workqueue\n",
  1084. nd->state);
  1085. }
  1086. }
  1087. int ncsi_process_next_channel(struct ncsi_dev_priv *ndp)
  1088. {
  1089. struct ncsi_channel *nc;
  1090. int old_state;
  1091. unsigned long flags;
  1092. spin_lock_irqsave(&ndp->lock, flags);
  1093. nc = list_first_or_null_rcu(&ndp->channel_queue,
  1094. struct ncsi_channel, link);
  1095. if (!nc) {
  1096. spin_unlock_irqrestore(&ndp->lock, flags);
  1097. goto out;
  1098. }
  1099. list_del_init(&nc->link);
  1100. spin_unlock_irqrestore(&ndp->lock, flags);
  1101. spin_lock_irqsave(&nc->lock, flags);
  1102. old_state = nc->state;
  1103. nc->state = NCSI_CHANNEL_INVISIBLE;
  1104. spin_unlock_irqrestore(&nc->lock, flags);
  1105. ndp->active_channel = nc;
  1106. ndp->active_package = nc->package;
  1107. switch (old_state) {
  1108. case NCSI_CHANNEL_INACTIVE:
  1109. ndp->ndev.state = ncsi_dev_state_config;
  1110. netdev_dbg(ndp->ndev.dev, "NCSI: configuring channel %u\n",
  1111. nc->id);
  1112. ncsi_configure_channel(ndp);
  1113. break;
  1114. case NCSI_CHANNEL_ACTIVE:
  1115. ndp->ndev.state = ncsi_dev_state_suspend;
  1116. netdev_dbg(ndp->ndev.dev, "NCSI: suspending channel %u\n",
  1117. nc->id);
  1118. ncsi_suspend_channel(ndp);
  1119. break;
  1120. default:
  1121. netdev_err(ndp->ndev.dev, "Invalid state 0x%x on %d:%d\n",
  1122. old_state, nc->package->id, nc->id);
  1123. ncsi_report_link(ndp, false);
  1124. return -EINVAL;
  1125. }
  1126. return 0;
  1127. out:
  1128. ndp->active_channel = NULL;
  1129. ndp->active_package = NULL;
  1130. if (ndp->flags & NCSI_DEV_RESHUFFLE) {
  1131. ndp->flags &= ~NCSI_DEV_RESHUFFLE;
  1132. return ncsi_choose_active_channel(ndp);
  1133. }
  1134. ncsi_report_link(ndp, false);
  1135. return -ENODEV;
  1136. }
  1137. #if IS_ENABLED(CONFIG_IPV6)
  1138. static int ncsi_inet6addr_event(struct notifier_block *this,
  1139. unsigned long event, void *data)
  1140. {
  1141. struct inet6_ifaddr *ifa = data;
  1142. struct net_device *dev = ifa->idev->dev;
  1143. struct ncsi_dev *nd = ncsi_find_dev(dev);
  1144. struct ncsi_dev_priv *ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
  1145. struct ncsi_package *np;
  1146. struct ncsi_channel *nc;
  1147. struct ncsi_cmd_arg nca;
  1148. bool action;
  1149. int ret;
  1150. if (!ndp || (ipv6_addr_type(&ifa->addr) &
  1151. (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK)))
  1152. return NOTIFY_OK;
  1153. switch (event) {
  1154. case NETDEV_UP:
  1155. action = (++ndp->inet6_addr_num) == 1;
  1156. nca.type = NCSI_PKT_CMD_EGMF;
  1157. break;
  1158. case NETDEV_DOWN:
  1159. action = (--ndp->inet6_addr_num == 0);
  1160. nca.type = NCSI_PKT_CMD_DGMF;
  1161. break;
  1162. default:
  1163. return NOTIFY_OK;
  1164. }
  1165. /* We might not have active channel or packages. The IPv6
  1166. * required multicast will be enabled when active channel
  1167. * or packages are chosen.
  1168. */
  1169. np = ndp->active_package;
  1170. nc = ndp->active_channel;
  1171. if (!action || !np || !nc)
  1172. return NOTIFY_OK;
  1173. /* We needn't enable or disable it if the function isn't supported */
  1174. if (!(nc->caps[NCSI_CAP_GENERIC].cap & NCSI_CAP_GENERIC_MC))
  1175. return NOTIFY_OK;
  1176. nca.ndp = ndp;
  1177. nca.req_flags = 0;
  1178. nca.package = np->id;
  1179. nca.channel = nc->id;
  1180. nca.dwords[0] = nc->caps[NCSI_CAP_MC].cap;
  1181. ret = ncsi_xmit_cmd(&nca);
  1182. if (ret) {
  1183. netdev_warn(dev, "Fail to %s global multicast filter (%d)\n",
  1184. (event == NETDEV_UP) ? "enable" : "disable", ret);
  1185. return NOTIFY_DONE;
  1186. }
  1187. return NOTIFY_OK;
  1188. }
  1189. static struct notifier_block ncsi_inet6addr_notifier = {
  1190. .notifier_call = ncsi_inet6addr_event,
  1191. };
  1192. #endif /* CONFIG_IPV6 */
  1193. static int ncsi_kick_channels(struct ncsi_dev_priv *ndp)
  1194. {
  1195. struct ncsi_dev *nd = &ndp->ndev;
  1196. struct ncsi_channel *nc;
  1197. struct ncsi_package *np;
  1198. unsigned long flags;
  1199. unsigned int n = 0;
  1200. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  1201. NCSI_FOR_EACH_CHANNEL(np, nc) {
  1202. spin_lock_irqsave(&nc->lock, flags);
  1203. /* Channels may be busy, mark dirty instead of
  1204. * kicking if;
  1205. * a) not ACTIVE (configured)
  1206. * b) in the channel_queue (to be configured)
  1207. * c) it's ndev is in the config state
  1208. */
  1209. if (nc->state != NCSI_CHANNEL_ACTIVE) {
  1210. if ((ndp->ndev.state & 0xff00) ==
  1211. ncsi_dev_state_config ||
  1212. !list_empty(&nc->link)) {
  1213. netdev_dbg(nd->dev,
  1214. "NCSI: channel %p marked dirty\n",
  1215. nc);
  1216. nc->reconfigure_needed = true;
  1217. }
  1218. spin_unlock_irqrestore(&nc->lock, flags);
  1219. continue;
  1220. }
  1221. spin_unlock_irqrestore(&nc->lock, flags);
  1222. ncsi_stop_channel_monitor(nc);
  1223. spin_lock_irqsave(&nc->lock, flags);
  1224. nc->state = NCSI_CHANNEL_INACTIVE;
  1225. spin_unlock_irqrestore(&nc->lock, flags);
  1226. spin_lock_irqsave(&ndp->lock, flags);
  1227. list_add_tail_rcu(&nc->link, &ndp->channel_queue);
  1228. spin_unlock_irqrestore(&ndp->lock, flags);
  1229. netdev_dbg(nd->dev, "NCSI: kicked channel %p\n", nc);
  1230. n++;
  1231. }
  1232. }
  1233. return n;
  1234. }
  1235. int ncsi_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1236. {
  1237. struct ncsi_dev_priv *ndp;
  1238. unsigned int n_vids = 0;
  1239. struct vlan_vid *vlan;
  1240. struct ncsi_dev *nd;
  1241. bool found = false;
  1242. if (vid == 0)
  1243. return 0;
  1244. nd = ncsi_find_dev(dev);
  1245. if (!nd) {
  1246. netdev_warn(dev, "NCSI: No net_device?\n");
  1247. return 0;
  1248. }
  1249. ndp = TO_NCSI_DEV_PRIV(nd);
  1250. /* Add the VLAN id to our internal list */
  1251. list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
  1252. n_vids++;
  1253. if (vlan->vid == vid) {
  1254. netdev_dbg(dev, "NCSI: vid %u already registered\n",
  1255. vid);
  1256. return 0;
  1257. }
  1258. }
  1259. if (n_vids >= NCSI_MAX_VLAN_VIDS) {
  1260. netdev_warn(dev,
  1261. "tried to add vlan id %u but NCSI max already registered (%u)\n",
  1262. vid, NCSI_MAX_VLAN_VIDS);
  1263. return -ENOSPC;
  1264. }
  1265. vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
  1266. if (!vlan)
  1267. return -ENOMEM;
  1268. vlan->proto = proto;
  1269. vlan->vid = vid;
  1270. list_add_rcu(&vlan->list, &ndp->vlan_vids);
  1271. netdev_dbg(dev, "NCSI: Added new vid %u\n", vid);
  1272. found = ncsi_kick_channels(ndp) != 0;
  1273. return found ? ncsi_process_next_channel(ndp) : 0;
  1274. }
  1275. EXPORT_SYMBOL_GPL(ncsi_vlan_rx_add_vid);
  1276. int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1277. {
  1278. struct vlan_vid *vlan, *tmp;
  1279. struct ncsi_dev_priv *ndp;
  1280. struct ncsi_dev *nd;
  1281. bool found = false;
  1282. if (vid == 0)
  1283. return 0;
  1284. nd = ncsi_find_dev(dev);
  1285. if (!nd) {
  1286. netdev_warn(dev, "NCSI: no net_device?\n");
  1287. return 0;
  1288. }
  1289. ndp = TO_NCSI_DEV_PRIV(nd);
  1290. /* Remove the VLAN id from our internal list */
  1291. list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
  1292. if (vlan->vid == vid) {
  1293. netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
  1294. list_del_rcu(&vlan->list);
  1295. found = true;
  1296. kfree(vlan);
  1297. }
  1298. if (!found) {
  1299. netdev_err(dev, "NCSI: vid %u wasn't registered!\n", vid);
  1300. return -EINVAL;
  1301. }
  1302. found = ncsi_kick_channels(ndp) != 0;
  1303. return found ? ncsi_process_next_channel(ndp) : 0;
  1304. }
  1305. EXPORT_SYMBOL_GPL(ncsi_vlan_rx_kill_vid);
  1306. struct ncsi_dev *ncsi_register_dev(struct net_device *dev,
  1307. void (*handler)(struct ncsi_dev *ndev))
  1308. {
  1309. struct ncsi_dev_priv *ndp;
  1310. struct ncsi_dev *nd;
  1311. unsigned long flags;
  1312. int i;
  1313. /* Check if the device has been registered or not */
  1314. nd = ncsi_find_dev(dev);
  1315. if (nd)
  1316. return nd;
  1317. /* Create NCSI device */
  1318. ndp = kzalloc(sizeof(*ndp), GFP_ATOMIC);
  1319. if (!ndp)
  1320. return NULL;
  1321. nd = &ndp->ndev;
  1322. nd->state = ncsi_dev_state_registered;
  1323. nd->dev = dev;
  1324. nd->handler = handler;
  1325. ndp->pending_req_num = 0;
  1326. INIT_LIST_HEAD(&ndp->channel_queue);
  1327. INIT_LIST_HEAD(&ndp->vlan_vids);
  1328. INIT_WORK(&ndp->work, ncsi_dev_work);
  1329. /* Initialize private NCSI device */
  1330. spin_lock_init(&ndp->lock);
  1331. INIT_LIST_HEAD(&ndp->packages);
  1332. ndp->request_id = NCSI_REQ_START_IDX;
  1333. for (i = 0; i < ARRAY_SIZE(ndp->requests); i++) {
  1334. ndp->requests[i].id = i;
  1335. ndp->requests[i].ndp = ndp;
  1336. timer_setup(&ndp->requests[i].timer, ncsi_request_timeout, 0);
  1337. }
  1338. spin_lock_irqsave(&ncsi_dev_lock, flags);
  1339. #if IS_ENABLED(CONFIG_IPV6)
  1340. ndp->inet6_addr_num = 0;
  1341. if (list_empty(&ncsi_dev_list))
  1342. register_inet6addr_notifier(&ncsi_inet6addr_notifier);
  1343. #endif
  1344. list_add_tail_rcu(&ndp->node, &ncsi_dev_list);
  1345. spin_unlock_irqrestore(&ncsi_dev_lock, flags);
  1346. /* Register NCSI packet Rx handler */
  1347. ndp->ptype.type = cpu_to_be16(ETH_P_NCSI);
  1348. ndp->ptype.func = ncsi_rcv_rsp;
  1349. ndp->ptype.dev = dev;
  1350. dev_add_pack(&ndp->ptype);
  1351. /* Set up generic netlink interface */
  1352. ncsi_init_netlink(dev);
  1353. return nd;
  1354. }
  1355. EXPORT_SYMBOL_GPL(ncsi_register_dev);
  1356. int ncsi_start_dev(struct ncsi_dev *nd)
  1357. {
  1358. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1359. int ret;
  1360. if (nd->state != ncsi_dev_state_registered &&
  1361. nd->state != ncsi_dev_state_functional)
  1362. return -ENOTTY;
  1363. if (!(ndp->flags & NCSI_DEV_PROBED)) {
  1364. nd->state = ncsi_dev_state_probe;
  1365. schedule_work(&ndp->work);
  1366. return 0;
  1367. }
  1368. if (ndp->flags & NCSI_DEV_HWA) {
  1369. netdev_info(ndp->ndev.dev, "NCSI: Enabling HWA mode\n");
  1370. ret = ncsi_enable_hwa(ndp);
  1371. } else {
  1372. ret = ncsi_choose_active_channel(ndp);
  1373. }
  1374. return ret;
  1375. }
  1376. EXPORT_SYMBOL_GPL(ncsi_start_dev);
  1377. void ncsi_stop_dev(struct ncsi_dev *nd)
  1378. {
  1379. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1380. struct ncsi_package *np;
  1381. struct ncsi_channel *nc;
  1382. bool chained;
  1383. int old_state;
  1384. unsigned long flags;
  1385. /* Stop the channel monitor and reset channel's state */
  1386. NCSI_FOR_EACH_PACKAGE(ndp, np) {
  1387. NCSI_FOR_EACH_CHANNEL(np, nc) {
  1388. ncsi_stop_channel_monitor(nc);
  1389. spin_lock_irqsave(&nc->lock, flags);
  1390. chained = !list_empty(&nc->link);
  1391. old_state = nc->state;
  1392. nc->state = NCSI_CHANNEL_INACTIVE;
  1393. spin_unlock_irqrestore(&nc->lock, flags);
  1394. WARN_ON_ONCE(chained ||
  1395. old_state == NCSI_CHANNEL_INVISIBLE);
  1396. }
  1397. }
  1398. netdev_dbg(ndp->ndev.dev, "NCSI: Stopping device\n");
  1399. ncsi_report_link(ndp, true);
  1400. }
  1401. EXPORT_SYMBOL_GPL(ncsi_stop_dev);
  1402. void ncsi_unregister_dev(struct ncsi_dev *nd)
  1403. {
  1404. struct ncsi_dev_priv *ndp = TO_NCSI_DEV_PRIV(nd);
  1405. struct ncsi_package *np, *tmp;
  1406. unsigned long flags;
  1407. dev_remove_pack(&ndp->ptype);
  1408. list_for_each_entry_safe(np, tmp, &ndp->packages, node)
  1409. ncsi_remove_package(np);
  1410. spin_lock_irqsave(&ncsi_dev_lock, flags);
  1411. list_del_rcu(&ndp->node);
  1412. #if IS_ENABLED(CONFIG_IPV6)
  1413. if (list_empty(&ncsi_dev_list))
  1414. unregister_inet6addr_notifier(&ncsi_inet6addr_notifier);
  1415. #endif
  1416. spin_unlock_irqrestore(&ncsi_dev_lock, flags);
  1417. ncsi_unregister_netlink(nd->dev);
  1418. kfree(ndp);
  1419. }
  1420. EXPORT_SYMBOL_GPL(ncsi_unregister_dev);