ncsi-manage.c 38 KB

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