ncsi-rsp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 "internal.h"
  18. #include "ncsi-pkt.h"
  19. static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
  20. unsigned short payload)
  21. {
  22. struct ncsi_rsp_pkt_hdr *h;
  23. u32 checksum;
  24. __be32 *pchecksum;
  25. /* Check NCSI packet header. We don't need validate
  26. * the packet type, which should have been checked
  27. * before calling this function.
  28. */
  29. h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
  30. if (h->common.revision != NCSI_PKT_REVISION)
  31. return -EINVAL;
  32. if (ntohs(h->common.length) != payload)
  33. return -EINVAL;
  34. /* Check on code and reason */
  35. if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
  36. ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR)
  37. return -EINVAL;
  38. /* Validate checksum, which might be zeroes if the
  39. * sender doesn't support checksum according to NCSI
  40. * specification.
  41. */
  42. pchecksum = (__be32 *)((void *)(h + 1) + payload - 4);
  43. if (ntohl(*pchecksum) == 0)
  44. return 0;
  45. checksum = ncsi_calculate_checksum((unsigned char *)h,
  46. sizeof(*h) + payload - 4);
  47. if (*pchecksum != htonl(checksum))
  48. return -EINVAL;
  49. return 0;
  50. }
  51. static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
  52. {
  53. struct ncsi_rsp_pkt *rsp;
  54. struct ncsi_dev_priv *ndp = nr->ndp;
  55. struct ncsi_package *np;
  56. struct ncsi_channel *nc;
  57. unsigned char id;
  58. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  59. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
  60. if (!nc) {
  61. if (ndp->flags & NCSI_DEV_PROBED)
  62. return -ENXIO;
  63. id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
  64. nc = ncsi_add_channel(np, id);
  65. }
  66. return nc ? 0 : -ENODEV;
  67. }
  68. static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
  69. {
  70. struct ncsi_rsp_pkt *rsp;
  71. struct ncsi_dev_priv *ndp = nr->ndp;
  72. struct ncsi_package *np;
  73. unsigned char id;
  74. /* Add the package if it's not existing. Otherwise,
  75. * to change the state of its child channels.
  76. */
  77. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  78. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  79. &np, NULL);
  80. if (!np) {
  81. if (ndp->flags & NCSI_DEV_PROBED)
  82. return -ENXIO;
  83. id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
  84. np = ncsi_add_package(ndp, id);
  85. if (!np)
  86. return -ENODEV;
  87. }
  88. return 0;
  89. }
  90. static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
  91. {
  92. struct ncsi_rsp_pkt *rsp;
  93. struct ncsi_dev_priv *ndp = nr->ndp;
  94. struct ncsi_package *np;
  95. struct ncsi_channel *nc;
  96. unsigned long flags;
  97. /* Find the package */
  98. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  99. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  100. &np, NULL);
  101. if (!np)
  102. return -ENODEV;
  103. /* Change state of all channels attached to the package */
  104. NCSI_FOR_EACH_CHANNEL(np, nc) {
  105. spin_lock_irqsave(&nc->lock, flags);
  106. nc->state = NCSI_CHANNEL_INACTIVE;
  107. spin_unlock_irqrestore(&nc->lock, flags);
  108. }
  109. return 0;
  110. }
  111. static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
  112. {
  113. struct ncsi_rsp_pkt *rsp;
  114. struct ncsi_dev_priv *ndp = nr->ndp;
  115. struct ncsi_channel *nc;
  116. struct ncsi_channel_mode *ncm;
  117. /* Find the package and channel */
  118. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  119. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  120. NULL, &nc);
  121. if (!nc)
  122. return -ENODEV;
  123. ncm = &nc->modes[NCSI_MODE_ENABLE];
  124. if (ncm->enable)
  125. return 0;
  126. ncm->enable = 1;
  127. return 0;
  128. }
  129. static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
  130. {
  131. struct ncsi_rsp_pkt *rsp;
  132. struct ncsi_dev_priv *ndp = nr->ndp;
  133. struct ncsi_channel *nc;
  134. struct ncsi_channel_mode *ncm;
  135. int ret;
  136. ret = ncsi_validate_rsp_pkt(nr, 4);
  137. if (ret)
  138. return ret;
  139. /* Find the package and channel */
  140. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  141. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  142. NULL, &nc);
  143. if (!nc)
  144. return -ENODEV;
  145. ncm = &nc->modes[NCSI_MODE_ENABLE];
  146. if (!ncm->enable)
  147. return 0;
  148. ncm->enable = 0;
  149. return 0;
  150. }
  151. static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
  152. {
  153. struct ncsi_rsp_pkt *rsp;
  154. struct ncsi_dev_priv *ndp = nr->ndp;
  155. struct ncsi_channel *nc;
  156. unsigned long flags;
  157. /* Find the package and channel */
  158. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  159. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  160. NULL, &nc);
  161. if (!nc)
  162. return -ENODEV;
  163. /* Update state for the specified channel */
  164. spin_lock_irqsave(&nc->lock, flags);
  165. nc->state = NCSI_CHANNEL_INACTIVE;
  166. spin_unlock_irqrestore(&nc->lock, flags);
  167. return 0;
  168. }
  169. static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
  170. {
  171. struct ncsi_rsp_pkt *rsp;
  172. struct ncsi_dev_priv *ndp = nr->ndp;
  173. struct ncsi_channel *nc;
  174. struct ncsi_channel_mode *ncm;
  175. /* Find the package and channel */
  176. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  177. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  178. NULL, &nc);
  179. if (!nc)
  180. return -ENODEV;
  181. ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
  182. if (ncm->enable)
  183. return 0;
  184. ncm->enable = 1;
  185. return 0;
  186. }
  187. static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
  188. {
  189. struct ncsi_rsp_pkt *rsp;
  190. struct ncsi_dev_priv *ndp = nr->ndp;
  191. struct ncsi_channel *nc;
  192. struct ncsi_channel_mode *ncm;
  193. /* Find the package and channel */
  194. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  195. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  196. NULL, &nc);
  197. if (!nc)
  198. return -ENODEV;
  199. ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
  200. if (!ncm->enable)
  201. return 0;
  202. ncm->enable = 1;
  203. return 0;
  204. }
  205. static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
  206. {
  207. struct ncsi_cmd_ae_pkt *cmd;
  208. struct ncsi_rsp_pkt *rsp;
  209. struct ncsi_dev_priv *ndp = nr->ndp;
  210. struct ncsi_channel *nc;
  211. struct ncsi_channel_mode *ncm;
  212. /* Find the package and channel */
  213. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  214. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  215. NULL, &nc);
  216. if (!nc)
  217. return -ENODEV;
  218. /* Check if the AEN has been enabled */
  219. ncm = &nc->modes[NCSI_MODE_AEN];
  220. if (ncm->enable)
  221. return 0;
  222. /* Update to AEN configuration */
  223. cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
  224. ncm->enable = 1;
  225. ncm->data[0] = cmd->mc_id;
  226. ncm->data[1] = ntohl(cmd->mode);
  227. return 0;
  228. }
  229. static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
  230. {
  231. struct ncsi_cmd_sl_pkt *cmd;
  232. struct ncsi_rsp_pkt *rsp;
  233. struct ncsi_dev_priv *ndp = nr->ndp;
  234. struct ncsi_channel *nc;
  235. struct ncsi_channel_mode *ncm;
  236. /* Find the package and channel */
  237. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  238. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  239. NULL, &nc);
  240. if (!nc)
  241. return -ENODEV;
  242. cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
  243. ncm = &nc->modes[NCSI_MODE_LINK];
  244. ncm->data[0] = ntohl(cmd->mode);
  245. ncm->data[1] = ntohl(cmd->oem_mode);
  246. return 0;
  247. }
  248. static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
  249. {
  250. struct ncsi_rsp_gls_pkt *rsp;
  251. struct ncsi_dev_priv *ndp = nr->ndp;
  252. struct ncsi_channel *nc;
  253. struct ncsi_channel_mode *ncm;
  254. unsigned long flags;
  255. /* Find the package and channel */
  256. rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
  257. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  258. NULL, &nc);
  259. if (!nc)
  260. return -ENODEV;
  261. ncm = &nc->modes[NCSI_MODE_LINK];
  262. ncm->data[2] = ntohl(rsp->status);
  263. ncm->data[3] = ntohl(rsp->other);
  264. ncm->data[4] = ntohl(rsp->oem_status);
  265. if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
  266. return 0;
  267. /* Reset the channel monitor if it has been enabled */
  268. spin_lock_irqsave(&nc->lock, flags);
  269. nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
  270. spin_unlock_irqrestore(&nc->lock, flags);
  271. return 0;
  272. }
  273. static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
  274. {
  275. struct ncsi_cmd_svf_pkt *cmd;
  276. struct ncsi_rsp_pkt *rsp;
  277. struct ncsi_dev_priv *ndp = nr->ndp;
  278. struct ncsi_channel *nc;
  279. struct ncsi_channel_vlan_filter *ncf;
  280. unsigned long flags;
  281. void *bitmap;
  282. /* Find the package and channel */
  283. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  284. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  285. NULL, &nc);
  286. if (!nc)
  287. return -ENODEV;
  288. cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
  289. ncf = &nc->vlan_filter;
  290. if (cmd->index == 0 || cmd->index > ncf->n_vids)
  291. return -ERANGE;
  292. /* Add or remove the VLAN filter. Remember HW indexes from 1 */
  293. spin_lock_irqsave(&nc->lock, flags);
  294. bitmap = &ncf->bitmap;
  295. if (!(cmd->enable & 0x1)) {
  296. if (test_and_clear_bit(cmd->index - 1, bitmap))
  297. ncf->vids[cmd->index - 1] = 0;
  298. } else {
  299. set_bit(cmd->index - 1, bitmap);
  300. ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
  301. }
  302. spin_unlock_irqrestore(&nc->lock, flags);
  303. return 0;
  304. }
  305. static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
  306. {
  307. struct ncsi_cmd_ev_pkt *cmd;
  308. struct ncsi_rsp_pkt *rsp;
  309. struct ncsi_dev_priv *ndp = nr->ndp;
  310. struct ncsi_channel *nc;
  311. struct ncsi_channel_mode *ncm;
  312. /* Find the package and channel */
  313. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  314. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  315. NULL, &nc);
  316. if (!nc)
  317. return -ENODEV;
  318. /* Check if VLAN mode has been enabled */
  319. ncm = &nc->modes[NCSI_MODE_VLAN];
  320. if (ncm->enable)
  321. return 0;
  322. /* Update to VLAN mode */
  323. cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
  324. ncm->enable = 1;
  325. ncm->data[0] = ntohl(cmd->mode);
  326. return 0;
  327. }
  328. static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
  329. {
  330. struct ncsi_rsp_pkt *rsp;
  331. struct ncsi_dev_priv *ndp = nr->ndp;
  332. struct ncsi_channel *nc;
  333. struct ncsi_channel_mode *ncm;
  334. /* Find the package and channel */
  335. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  336. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  337. NULL, &nc);
  338. if (!nc)
  339. return -ENODEV;
  340. /* Check if VLAN mode has been enabled */
  341. ncm = &nc->modes[NCSI_MODE_VLAN];
  342. if (!ncm->enable)
  343. return 0;
  344. /* Update to VLAN mode */
  345. ncm->enable = 0;
  346. return 0;
  347. }
  348. static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
  349. {
  350. struct ncsi_cmd_sma_pkt *cmd;
  351. struct ncsi_rsp_pkt *rsp;
  352. struct ncsi_dev_priv *ndp = nr->ndp;
  353. struct ncsi_channel *nc;
  354. struct ncsi_channel_mac_filter *ncf;
  355. unsigned long flags;
  356. void *bitmap;
  357. bool enabled;
  358. int index;
  359. /* Find the package and channel */
  360. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  361. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  362. NULL, &nc);
  363. if (!nc)
  364. return -ENODEV;
  365. /* According to NCSI spec 1.01, the mixed filter table
  366. * isn't supported yet.
  367. */
  368. cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
  369. enabled = cmd->at_e & 0x1;
  370. ncf = &nc->mac_filter;
  371. bitmap = &ncf->bitmap;
  372. if (cmd->index == 0 ||
  373. cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
  374. return -ERANGE;
  375. index = (cmd->index - 1) * ETH_ALEN;
  376. spin_lock_irqsave(&nc->lock, flags);
  377. if (enabled) {
  378. set_bit(cmd->index - 1, bitmap);
  379. memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
  380. } else {
  381. clear_bit(cmd->index - 1, bitmap);
  382. memset(&ncf->addrs[index], 0, ETH_ALEN);
  383. }
  384. spin_unlock_irqrestore(&nc->lock, flags);
  385. return 0;
  386. }
  387. static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
  388. {
  389. struct ncsi_cmd_ebf_pkt *cmd;
  390. struct ncsi_rsp_pkt *rsp;
  391. struct ncsi_dev_priv *ndp = nr->ndp;
  392. struct ncsi_channel *nc;
  393. struct ncsi_channel_mode *ncm;
  394. /* Find the package and channel */
  395. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  396. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
  397. if (!nc)
  398. return -ENODEV;
  399. /* Check if broadcast filter has been enabled */
  400. ncm = &nc->modes[NCSI_MODE_BC];
  401. if (ncm->enable)
  402. return 0;
  403. /* Update to broadcast filter mode */
  404. cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
  405. ncm->enable = 1;
  406. ncm->data[0] = ntohl(cmd->mode);
  407. return 0;
  408. }
  409. static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
  410. {
  411. struct ncsi_rsp_pkt *rsp;
  412. struct ncsi_dev_priv *ndp = nr->ndp;
  413. struct ncsi_channel *nc;
  414. struct ncsi_channel_mode *ncm;
  415. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  416. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  417. NULL, &nc);
  418. if (!nc)
  419. return -ENODEV;
  420. /* Check if broadcast filter isn't enabled */
  421. ncm = &nc->modes[NCSI_MODE_BC];
  422. if (!ncm->enable)
  423. return 0;
  424. /* Update to broadcast filter mode */
  425. ncm->enable = 0;
  426. ncm->data[0] = 0;
  427. return 0;
  428. }
  429. static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
  430. {
  431. struct ncsi_cmd_egmf_pkt *cmd;
  432. struct ncsi_rsp_pkt *rsp;
  433. struct ncsi_dev_priv *ndp = nr->ndp;
  434. struct ncsi_channel *nc;
  435. struct ncsi_channel_mode *ncm;
  436. /* Find the channel */
  437. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  438. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  439. NULL, &nc);
  440. if (!nc)
  441. return -ENODEV;
  442. /* Check if multicast filter has been enabled */
  443. ncm = &nc->modes[NCSI_MODE_MC];
  444. if (ncm->enable)
  445. return 0;
  446. /* Update to multicast filter mode */
  447. cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
  448. ncm->enable = 1;
  449. ncm->data[0] = ntohl(cmd->mode);
  450. return 0;
  451. }
  452. static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
  453. {
  454. struct ncsi_rsp_pkt *rsp;
  455. struct ncsi_dev_priv *ndp = nr->ndp;
  456. struct ncsi_channel *nc;
  457. struct ncsi_channel_mode *ncm;
  458. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  459. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  460. NULL, &nc);
  461. if (!nc)
  462. return -ENODEV;
  463. /* Check if multicast filter has been enabled */
  464. ncm = &nc->modes[NCSI_MODE_MC];
  465. if (!ncm->enable)
  466. return 0;
  467. /* Update to multicast filter mode */
  468. ncm->enable = 0;
  469. ncm->data[0] = 0;
  470. return 0;
  471. }
  472. static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
  473. {
  474. struct ncsi_cmd_snfc_pkt *cmd;
  475. struct ncsi_rsp_pkt *rsp;
  476. struct ncsi_dev_priv *ndp = nr->ndp;
  477. struct ncsi_channel *nc;
  478. struct ncsi_channel_mode *ncm;
  479. /* Find the channel */
  480. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  481. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  482. NULL, &nc);
  483. if (!nc)
  484. return -ENODEV;
  485. /* Check if flow control has been enabled */
  486. ncm = &nc->modes[NCSI_MODE_FC];
  487. if (ncm->enable)
  488. return 0;
  489. /* Update to flow control mode */
  490. cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
  491. ncm->enable = 1;
  492. ncm->data[0] = cmd->mode;
  493. return 0;
  494. }
  495. static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
  496. {
  497. struct ncsi_rsp_gvi_pkt *rsp;
  498. struct ncsi_dev_priv *ndp = nr->ndp;
  499. struct ncsi_channel *nc;
  500. struct ncsi_channel_version *ncv;
  501. int i;
  502. /* Find the channel */
  503. rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
  504. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  505. NULL, &nc);
  506. if (!nc)
  507. return -ENODEV;
  508. /* Update to channel's version info */
  509. ncv = &nc->version;
  510. ncv->version = ntohl(rsp->ncsi_version);
  511. ncv->alpha2 = rsp->alpha2;
  512. memcpy(ncv->fw_name, rsp->fw_name, 12);
  513. ncv->fw_version = ntohl(rsp->fw_version);
  514. for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
  515. ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
  516. ncv->mf_id = ntohl(rsp->mf_id);
  517. return 0;
  518. }
  519. static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
  520. {
  521. struct ncsi_rsp_gc_pkt *rsp;
  522. struct ncsi_dev_priv *ndp = nr->ndp;
  523. struct ncsi_channel *nc;
  524. size_t size;
  525. /* Find the channel */
  526. rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
  527. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  528. NULL, &nc);
  529. if (!nc)
  530. return -ENODEV;
  531. /* Update channel's capabilities */
  532. nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
  533. NCSI_CAP_GENERIC_MASK;
  534. nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
  535. NCSI_CAP_BC_MASK;
  536. nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
  537. NCSI_CAP_MC_MASK;
  538. nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
  539. nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
  540. NCSI_CAP_AEN_MASK;
  541. nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
  542. NCSI_CAP_VLAN_MASK;
  543. size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
  544. nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
  545. if (!nc->mac_filter.addrs)
  546. return -ENOMEM;
  547. nc->mac_filter.n_uc = rsp->uc_cnt;
  548. nc->mac_filter.n_mc = rsp->mc_cnt;
  549. nc->mac_filter.n_mixed = rsp->mixed_cnt;
  550. nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
  551. sizeof(*nc->vlan_filter.vids),
  552. GFP_ATOMIC);
  553. if (!nc->vlan_filter.vids)
  554. return -ENOMEM;
  555. /* Set VLAN filters active so they are cleared in the first
  556. * configuration state
  557. */
  558. nc->vlan_filter.bitmap = U64_MAX;
  559. nc->vlan_filter.n_vids = rsp->vlan_cnt;
  560. return 0;
  561. }
  562. static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
  563. {
  564. struct ncsi_channel_vlan_filter *ncvf;
  565. struct ncsi_channel_mac_filter *ncmf;
  566. struct ncsi_dev_priv *ndp = nr->ndp;
  567. struct ncsi_rsp_gp_pkt *rsp;
  568. struct ncsi_channel *nc;
  569. unsigned short enable;
  570. unsigned char *pdata;
  571. unsigned long flags;
  572. void *bitmap;
  573. int i;
  574. /* Find the channel */
  575. rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
  576. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  577. NULL, &nc);
  578. if (!nc)
  579. return -ENODEV;
  580. /* Modes with explicit enabled indications */
  581. if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
  582. nc->modes[NCSI_MODE_BC].enable = 1;
  583. nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
  584. }
  585. if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
  586. nc->modes[NCSI_MODE_ENABLE].enable = 1;
  587. if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
  588. nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
  589. if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
  590. nc->modes[NCSI_MODE_MC].enable = 1;
  591. /* Modes without explicit enabled indications */
  592. nc->modes[NCSI_MODE_LINK].enable = 1;
  593. nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
  594. nc->modes[NCSI_MODE_VLAN].enable = 1;
  595. nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
  596. nc->modes[NCSI_MODE_FC].enable = 1;
  597. nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
  598. nc->modes[NCSI_MODE_AEN].enable = 1;
  599. nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
  600. /* MAC addresses filter table */
  601. pdata = (unsigned char *)rsp + 48;
  602. enable = rsp->mac_enable;
  603. ncmf = &nc->mac_filter;
  604. spin_lock_irqsave(&nc->lock, flags);
  605. bitmap = &ncmf->bitmap;
  606. for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
  607. if (!(enable & (0x1 << i)))
  608. clear_bit(i, bitmap);
  609. else
  610. set_bit(i, bitmap);
  611. memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
  612. }
  613. spin_unlock_irqrestore(&nc->lock, flags);
  614. /* VLAN filter table */
  615. enable = ntohs(rsp->vlan_enable);
  616. ncvf = &nc->vlan_filter;
  617. bitmap = &ncvf->bitmap;
  618. spin_lock_irqsave(&nc->lock, flags);
  619. for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
  620. if (!(enable & (0x1 << i)))
  621. clear_bit(i, bitmap);
  622. else
  623. set_bit(i, bitmap);
  624. ncvf->vids[i] = ntohs(*(__be16 *)pdata);
  625. }
  626. spin_unlock_irqrestore(&nc->lock, flags);
  627. return 0;
  628. }
  629. static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
  630. {
  631. struct ncsi_rsp_gcps_pkt *rsp;
  632. struct ncsi_dev_priv *ndp = nr->ndp;
  633. struct ncsi_channel *nc;
  634. struct ncsi_channel_stats *ncs;
  635. /* Find the channel */
  636. rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
  637. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  638. NULL, &nc);
  639. if (!nc)
  640. return -ENODEV;
  641. /* Update HNC's statistics */
  642. ncs = &nc->stats;
  643. ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
  644. ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
  645. ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
  646. ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
  647. ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
  648. ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
  649. ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
  650. ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
  651. ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
  652. ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
  653. ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
  654. ncs->hnc_align_err = ntohl(rsp->align_err);
  655. ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
  656. ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
  657. ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
  658. ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
  659. ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
  660. ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
  661. ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
  662. ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
  663. ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
  664. ncs->hnc_l_collision = ntohl(rsp->l_collision);
  665. ncs->hnc_e_collision = ntohl(rsp->e_collision);
  666. ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
  667. ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
  668. ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
  669. ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
  670. ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
  671. ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
  672. ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
  673. ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
  674. ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
  675. ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
  676. ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
  677. ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
  678. ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
  679. ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
  680. ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
  681. ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
  682. ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
  683. ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
  684. return 0;
  685. }
  686. static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
  687. {
  688. struct ncsi_rsp_gns_pkt *rsp;
  689. struct ncsi_dev_priv *ndp = nr->ndp;
  690. struct ncsi_channel *nc;
  691. struct ncsi_channel_stats *ncs;
  692. /* Find the channel */
  693. rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
  694. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  695. NULL, &nc);
  696. if (!nc)
  697. return -ENODEV;
  698. /* Update HNC's statistics */
  699. ncs = &nc->stats;
  700. ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
  701. ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
  702. ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
  703. ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
  704. ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
  705. ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
  706. ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
  707. return 0;
  708. }
  709. static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
  710. {
  711. struct ncsi_rsp_gnpts_pkt *rsp;
  712. struct ncsi_dev_priv *ndp = nr->ndp;
  713. struct ncsi_channel *nc;
  714. struct ncsi_channel_stats *ncs;
  715. /* Find the channel */
  716. rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
  717. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  718. NULL, &nc);
  719. if (!nc)
  720. return -ENODEV;
  721. /* Update HNC's statistics */
  722. ncs = &nc->stats;
  723. ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
  724. ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
  725. ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
  726. ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
  727. ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
  728. ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
  729. ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
  730. ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
  731. ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
  732. return 0;
  733. }
  734. static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
  735. {
  736. struct ncsi_rsp_gps_pkt *rsp;
  737. struct ncsi_dev_priv *ndp = nr->ndp;
  738. struct ncsi_package *np;
  739. /* Find the package */
  740. rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
  741. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  742. &np, NULL);
  743. if (!np)
  744. return -ENODEV;
  745. return 0;
  746. }
  747. static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
  748. {
  749. struct ncsi_rsp_gpuuid_pkt *rsp;
  750. struct ncsi_dev_priv *ndp = nr->ndp;
  751. struct ncsi_package *np;
  752. /* Find the package */
  753. rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
  754. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  755. &np, NULL);
  756. if (!np)
  757. return -ENODEV;
  758. memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
  759. return 0;
  760. }
  761. static struct ncsi_rsp_handler {
  762. unsigned char type;
  763. int payload;
  764. int (*handler)(struct ncsi_request *nr);
  765. } ncsi_rsp_handlers[] = {
  766. { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
  767. { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
  768. { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
  769. { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
  770. { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
  771. { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
  772. { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
  773. { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
  774. { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
  775. { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
  776. { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
  777. { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
  778. { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
  779. { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
  780. { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
  781. { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
  782. { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
  783. { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
  784. { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
  785. { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
  786. { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
  787. { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
  788. { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
  789. { NCSI_PKT_RSP_GCPS, 172, ncsi_rsp_handler_gcps },
  790. { NCSI_PKT_RSP_GNS, 172, ncsi_rsp_handler_gns },
  791. { NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts },
  792. { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
  793. { NCSI_PKT_RSP_OEM, 0, NULL },
  794. { NCSI_PKT_RSP_PLDM, 0, NULL },
  795. { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid }
  796. };
  797. int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
  798. struct packet_type *pt, struct net_device *orig_dev)
  799. {
  800. struct ncsi_rsp_handler *nrh = NULL;
  801. struct ncsi_dev *nd;
  802. struct ncsi_dev_priv *ndp;
  803. struct ncsi_request *nr;
  804. struct ncsi_pkt_hdr *hdr;
  805. unsigned long flags;
  806. int payload, i, ret;
  807. /* Find the NCSI device */
  808. nd = ncsi_find_dev(dev);
  809. ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
  810. if (!ndp)
  811. return -ENODEV;
  812. /* Check if it is AEN packet */
  813. hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
  814. if (hdr->type == NCSI_PKT_AEN)
  815. return ncsi_aen_handler(ndp, skb);
  816. /* Find the handler */
  817. for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
  818. if (ncsi_rsp_handlers[i].type == hdr->type) {
  819. if (ncsi_rsp_handlers[i].handler)
  820. nrh = &ncsi_rsp_handlers[i];
  821. else
  822. nrh = NULL;
  823. break;
  824. }
  825. }
  826. if (!nrh) {
  827. netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
  828. hdr->type);
  829. return -ENOENT;
  830. }
  831. /* Associate with the request */
  832. spin_lock_irqsave(&ndp->lock, flags);
  833. nr = &ndp->requests[hdr->id];
  834. if (!nr->used) {
  835. spin_unlock_irqrestore(&ndp->lock, flags);
  836. return -ENODEV;
  837. }
  838. nr->rsp = skb;
  839. if (!nr->enabled) {
  840. spin_unlock_irqrestore(&ndp->lock, flags);
  841. ret = -ENOENT;
  842. goto out;
  843. }
  844. /* Validate the packet */
  845. spin_unlock_irqrestore(&ndp->lock, flags);
  846. payload = nrh->payload;
  847. if (payload < 0)
  848. payload = ntohs(hdr->length);
  849. ret = ncsi_validate_rsp_pkt(nr, payload);
  850. if (ret) {
  851. netdev_warn(ndp->ndev.dev,
  852. "NCSI: 'bad' packet ignored for type 0x%x\n",
  853. hdr->type);
  854. goto out;
  855. }
  856. /* Process the packet */
  857. ret = nrh->handler(nr);
  858. if (ret)
  859. netdev_err(ndp->ndev.dev,
  860. "NCSI: Handler for packet type 0x%x returned %d\n",
  861. hdr->type, ret);
  862. out:
  863. ncsi_free_request(nr);
  864. return ret;
  865. }