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