ncsi-rsp.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright Gavin Shan, IBM Corporation 2016.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <net/ncsi.h>
  12. #include <net/net_namespace.h>
  13. #include <net/sock.h>
  14. #include <net/genetlink.h>
  15. #include "internal.h"
  16. #include "ncsi-pkt.h"
  17. #include "ncsi-netlink.h"
  18. /* Nibbles within [0xA, 0xF] add zero "0" to the returned value.
  19. * Optional fields (encoded as 0xFF) will default to zero.
  20. */
  21. static u8 decode_bcd_u8(u8 x)
  22. {
  23. int lo = x & 0xF;
  24. int hi = x >> 4;
  25. lo = lo < 0xA ? lo : 0;
  26. hi = hi < 0xA ? hi : 0;
  27. return lo + hi * 10;
  28. }
  29. static int ncsi_validate_rsp_pkt(struct ncsi_request *nr,
  30. unsigned short payload)
  31. {
  32. struct ncsi_rsp_pkt_hdr *h;
  33. u32 checksum;
  34. __be32 *pchecksum;
  35. /* Check NCSI packet header. We don't need validate
  36. * the packet type, which should have been checked
  37. * before calling this function.
  38. */
  39. h = (struct ncsi_rsp_pkt_hdr *)skb_network_header(nr->rsp);
  40. if (h->common.revision != NCSI_PKT_REVISION) {
  41. netdev_dbg(nr->ndp->ndev.dev,
  42. "NCSI: unsupported header revision\n");
  43. return -EINVAL;
  44. }
  45. if (ntohs(h->common.length) != payload) {
  46. netdev_dbg(nr->ndp->ndev.dev,
  47. "NCSI: payload length mismatched\n");
  48. return -EINVAL;
  49. }
  50. /* Check on code and reason */
  51. if (ntohs(h->code) != NCSI_PKT_RSP_C_COMPLETED ||
  52. ntohs(h->reason) != NCSI_PKT_RSP_R_NO_ERROR) {
  53. netdev_dbg(nr->ndp->ndev.dev,
  54. "NCSI: non zero response/reason code %04xh, %04xh\n",
  55. ntohs(h->code), ntohs(h->reason));
  56. return -EPERM;
  57. }
  58. /* Validate checksum, which might be zeroes if the
  59. * sender doesn't support checksum according to NCSI
  60. * specification.
  61. */
  62. pchecksum = (__be32 *)((void *)(h + 1) + ALIGN(payload, 4) - 4);
  63. if (ntohl(*pchecksum) == 0)
  64. return 0;
  65. checksum = ncsi_calculate_checksum((unsigned char *)h,
  66. sizeof(*h) + payload - 4);
  67. if (*pchecksum != htonl(checksum)) {
  68. netdev_dbg(nr->ndp->ndev.dev,
  69. "NCSI: checksum mismatched; recd: %08x calc: %08x\n",
  70. *pchecksum, htonl(checksum));
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. static int ncsi_rsp_handler_cis(struct ncsi_request *nr)
  76. {
  77. struct ncsi_rsp_pkt *rsp;
  78. struct ncsi_dev_priv *ndp = nr->ndp;
  79. struct ncsi_package *np;
  80. struct ncsi_channel *nc;
  81. unsigned char id;
  82. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  83. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
  84. if (!nc) {
  85. if (ndp->flags & NCSI_DEV_PROBED)
  86. return -ENXIO;
  87. id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
  88. nc = ncsi_add_channel(np, id);
  89. }
  90. return nc ? 0 : -ENODEV;
  91. }
  92. static int ncsi_rsp_handler_sp(struct ncsi_request *nr)
  93. {
  94. struct ncsi_rsp_pkt *rsp;
  95. struct ncsi_dev_priv *ndp = nr->ndp;
  96. struct ncsi_package *np;
  97. unsigned char id;
  98. /* Add the package if it's not existing. Otherwise,
  99. * to change the state of its child channels.
  100. */
  101. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  102. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  103. &np, NULL);
  104. if (!np) {
  105. if (ndp->flags & NCSI_DEV_PROBED)
  106. return -ENXIO;
  107. id = NCSI_PACKAGE_INDEX(rsp->rsp.common.channel);
  108. np = ncsi_add_package(ndp, id);
  109. if (!np)
  110. return -ENODEV;
  111. }
  112. return 0;
  113. }
  114. static int ncsi_rsp_handler_dp(struct ncsi_request *nr)
  115. {
  116. struct ncsi_rsp_pkt *rsp;
  117. struct ncsi_dev_priv *ndp = nr->ndp;
  118. struct ncsi_package *np;
  119. struct ncsi_channel *nc;
  120. unsigned long flags;
  121. /* Find the package */
  122. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  123. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  124. &np, NULL);
  125. if (!np)
  126. return -ENODEV;
  127. /* Change state of all channels attached to the package */
  128. NCSI_FOR_EACH_CHANNEL(np, nc) {
  129. spin_lock_irqsave(&nc->lock, flags);
  130. nc->state = NCSI_CHANNEL_INACTIVE;
  131. spin_unlock_irqrestore(&nc->lock, flags);
  132. }
  133. return 0;
  134. }
  135. static int ncsi_rsp_handler_ec(struct ncsi_request *nr)
  136. {
  137. struct ncsi_rsp_pkt *rsp;
  138. struct ncsi_dev_priv *ndp = nr->ndp;
  139. struct ncsi_channel *nc;
  140. struct ncsi_channel_mode *ncm;
  141. /* Find the package and channel */
  142. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  143. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  144. NULL, &nc);
  145. if (!nc)
  146. return -ENODEV;
  147. ncm = &nc->modes[NCSI_MODE_ENABLE];
  148. if (ncm->enable)
  149. return 0;
  150. ncm->enable = 1;
  151. return 0;
  152. }
  153. static int ncsi_rsp_handler_dc(struct ncsi_request *nr)
  154. {
  155. struct ncsi_rsp_pkt *rsp;
  156. struct ncsi_dev_priv *ndp = nr->ndp;
  157. struct ncsi_channel *nc;
  158. struct ncsi_channel_mode *ncm;
  159. int ret;
  160. ret = ncsi_validate_rsp_pkt(nr, 4);
  161. if (ret)
  162. return ret;
  163. /* Find the package and channel */
  164. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  165. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  166. NULL, &nc);
  167. if (!nc)
  168. return -ENODEV;
  169. ncm = &nc->modes[NCSI_MODE_ENABLE];
  170. if (!ncm->enable)
  171. return 0;
  172. ncm->enable = 0;
  173. return 0;
  174. }
  175. static int ncsi_rsp_handler_rc(struct ncsi_request *nr)
  176. {
  177. struct ncsi_rsp_pkt *rsp;
  178. struct ncsi_dev_priv *ndp = nr->ndp;
  179. struct ncsi_channel *nc;
  180. unsigned long flags;
  181. /* Find the package and channel */
  182. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  183. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  184. NULL, &nc);
  185. if (!nc)
  186. return -ENODEV;
  187. /* Update state for the specified channel */
  188. spin_lock_irqsave(&nc->lock, flags);
  189. nc->state = NCSI_CHANNEL_INACTIVE;
  190. spin_unlock_irqrestore(&nc->lock, flags);
  191. return 0;
  192. }
  193. static int ncsi_rsp_handler_ecnt(struct ncsi_request *nr)
  194. {
  195. struct ncsi_rsp_pkt *rsp;
  196. struct ncsi_dev_priv *ndp = nr->ndp;
  197. struct ncsi_channel *nc;
  198. struct ncsi_channel_mode *ncm;
  199. /* Find the package and channel */
  200. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  201. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  202. NULL, &nc);
  203. if (!nc)
  204. return -ENODEV;
  205. ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
  206. if (ncm->enable)
  207. return 0;
  208. ncm->enable = 1;
  209. return 0;
  210. }
  211. static int ncsi_rsp_handler_dcnt(struct ncsi_request *nr)
  212. {
  213. struct ncsi_rsp_pkt *rsp;
  214. struct ncsi_dev_priv *ndp = nr->ndp;
  215. struct ncsi_channel *nc;
  216. struct ncsi_channel_mode *ncm;
  217. /* Find the package and channel */
  218. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  219. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  220. NULL, &nc);
  221. if (!nc)
  222. return -ENODEV;
  223. ncm = &nc->modes[NCSI_MODE_TX_ENABLE];
  224. if (!ncm->enable)
  225. return 0;
  226. ncm->enable = 0;
  227. return 0;
  228. }
  229. static int ncsi_rsp_handler_ae(struct ncsi_request *nr)
  230. {
  231. struct ncsi_cmd_ae_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. /* Check if the AEN has been enabled */
  243. ncm = &nc->modes[NCSI_MODE_AEN];
  244. if (ncm->enable)
  245. return 0;
  246. /* Update to AEN configuration */
  247. cmd = (struct ncsi_cmd_ae_pkt *)skb_network_header(nr->cmd);
  248. ncm->enable = 1;
  249. ncm->data[0] = cmd->mc_id;
  250. ncm->data[1] = ntohl(cmd->mode);
  251. return 0;
  252. }
  253. static int ncsi_rsp_handler_sl(struct ncsi_request *nr)
  254. {
  255. struct ncsi_cmd_sl_pkt *cmd;
  256. struct ncsi_rsp_pkt *rsp;
  257. struct ncsi_dev_priv *ndp = nr->ndp;
  258. struct ncsi_channel *nc;
  259. struct ncsi_channel_mode *ncm;
  260. /* Find the package and channel */
  261. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  262. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  263. NULL, &nc);
  264. if (!nc)
  265. return -ENODEV;
  266. cmd = (struct ncsi_cmd_sl_pkt *)skb_network_header(nr->cmd);
  267. ncm = &nc->modes[NCSI_MODE_LINK];
  268. ncm->data[0] = ntohl(cmd->mode);
  269. ncm->data[1] = ntohl(cmd->oem_mode);
  270. return 0;
  271. }
  272. static int ncsi_rsp_handler_gls(struct ncsi_request *nr)
  273. {
  274. struct ncsi_rsp_gls_pkt *rsp;
  275. struct ncsi_dev_priv *ndp = nr->ndp;
  276. struct ncsi_channel *nc;
  277. struct ncsi_channel_mode *ncm;
  278. unsigned long flags;
  279. /* Find the package and channel */
  280. rsp = (struct ncsi_rsp_gls_pkt *)skb_network_header(nr->rsp);
  281. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  282. NULL, &nc);
  283. if (!nc)
  284. return -ENODEV;
  285. ncm = &nc->modes[NCSI_MODE_LINK];
  286. ncm->data[2] = ntohl(rsp->status);
  287. ncm->data[3] = ntohl(rsp->other);
  288. ncm->data[4] = ntohl(rsp->oem_status);
  289. if (nr->flags & NCSI_REQ_FLAG_EVENT_DRIVEN)
  290. return 0;
  291. /* Reset the channel monitor if it has been enabled */
  292. spin_lock_irqsave(&nc->lock, flags);
  293. nc->monitor.state = NCSI_CHANNEL_MONITOR_START;
  294. spin_unlock_irqrestore(&nc->lock, flags);
  295. return 0;
  296. }
  297. static int ncsi_rsp_handler_svf(struct ncsi_request *nr)
  298. {
  299. struct ncsi_cmd_svf_pkt *cmd;
  300. struct ncsi_rsp_pkt *rsp;
  301. struct ncsi_dev_priv *ndp = nr->ndp;
  302. struct ncsi_channel *nc;
  303. struct ncsi_channel_vlan_filter *ncf;
  304. unsigned long flags;
  305. void *bitmap;
  306. /* Find the package and channel */
  307. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  308. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  309. NULL, &nc);
  310. if (!nc)
  311. return -ENODEV;
  312. cmd = (struct ncsi_cmd_svf_pkt *)skb_network_header(nr->cmd);
  313. ncf = &nc->vlan_filter;
  314. if (cmd->index == 0 || cmd->index > ncf->n_vids)
  315. return -ERANGE;
  316. /* Add or remove the VLAN filter. Remember HW indexes from 1 */
  317. spin_lock_irqsave(&nc->lock, flags);
  318. bitmap = &ncf->bitmap;
  319. if (!(cmd->enable & 0x1)) {
  320. if (test_and_clear_bit(cmd->index - 1, bitmap))
  321. ncf->vids[cmd->index - 1] = 0;
  322. } else {
  323. set_bit(cmd->index - 1, bitmap);
  324. ncf->vids[cmd->index - 1] = ntohs(cmd->vlan);
  325. }
  326. spin_unlock_irqrestore(&nc->lock, flags);
  327. return 0;
  328. }
  329. static int ncsi_rsp_handler_ev(struct ncsi_request *nr)
  330. {
  331. struct ncsi_cmd_ev_pkt *cmd;
  332. struct ncsi_rsp_pkt *rsp;
  333. struct ncsi_dev_priv *ndp = nr->ndp;
  334. struct ncsi_channel *nc;
  335. struct ncsi_channel_mode *ncm;
  336. /* Find the package and channel */
  337. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  338. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  339. NULL, &nc);
  340. if (!nc)
  341. return -ENODEV;
  342. /* Check if VLAN mode has been enabled */
  343. ncm = &nc->modes[NCSI_MODE_VLAN];
  344. if (ncm->enable)
  345. return 0;
  346. /* Update to VLAN mode */
  347. cmd = (struct ncsi_cmd_ev_pkt *)skb_network_header(nr->cmd);
  348. ncm->enable = 1;
  349. ncm->data[0] = ntohl((__force __be32)cmd->mode);
  350. return 0;
  351. }
  352. static int ncsi_rsp_handler_dv(struct ncsi_request *nr)
  353. {
  354. struct ncsi_rsp_pkt *rsp;
  355. struct ncsi_dev_priv *ndp = nr->ndp;
  356. struct ncsi_channel *nc;
  357. struct ncsi_channel_mode *ncm;
  358. /* Find the package and channel */
  359. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  360. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  361. NULL, &nc);
  362. if (!nc)
  363. return -ENODEV;
  364. /* Check if VLAN mode has been enabled */
  365. ncm = &nc->modes[NCSI_MODE_VLAN];
  366. if (!ncm->enable)
  367. return 0;
  368. /* Update to VLAN mode */
  369. ncm->enable = 0;
  370. return 0;
  371. }
  372. static int ncsi_rsp_handler_sma(struct ncsi_request *nr)
  373. {
  374. struct ncsi_cmd_sma_pkt *cmd;
  375. struct ncsi_rsp_pkt *rsp;
  376. struct ncsi_dev_priv *ndp = nr->ndp;
  377. struct ncsi_channel *nc;
  378. struct ncsi_channel_mac_filter *ncf;
  379. unsigned long flags;
  380. void *bitmap;
  381. bool enabled;
  382. int index;
  383. /* Find the package and channel */
  384. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  385. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  386. NULL, &nc);
  387. if (!nc)
  388. return -ENODEV;
  389. /* According to NCSI spec 1.01, the mixed filter table
  390. * isn't supported yet.
  391. */
  392. cmd = (struct ncsi_cmd_sma_pkt *)skb_network_header(nr->cmd);
  393. enabled = cmd->at_e & 0x1;
  394. ncf = &nc->mac_filter;
  395. bitmap = &ncf->bitmap;
  396. if (cmd->index == 0 ||
  397. cmd->index > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
  398. return -ERANGE;
  399. index = (cmd->index - 1) * ETH_ALEN;
  400. spin_lock_irqsave(&nc->lock, flags);
  401. if (enabled) {
  402. set_bit(cmd->index - 1, bitmap);
  403. memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
  404. } else {
  405. clear_bit(cmd->index - 1, bitmap);
  406. eth_zero_addr(&ncf->addrs[index]);
  407. }
  408. spin_unlock_irqrestore(&nc->lock, flags);
  409. return 0;
  410. }
  411. static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
  412. {
  413. struct ncsi_cmd_ebf_pkt *cmd;
  414. struct ncsi_rsp_pkt *rsp;
  415. struct ncsi_dev_priv *ndp = nr->ndp;
  416. struct ncsi_channel *nc;
  417. struct ncsi_channel_mode *ncm;
  418. /* Find the package and channel */
  419. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  420. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
  421. if (!nc)
  422. return -ENODEV;
  423. /* Check if broadcast filter has been enabled */
  424. ncm = &nc->modes[NCSI_MODE_BC];
  425. if (ncm->enable)
  426. return 0;
  427. /* Update to broadcast filter mode */
  428. cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
  429. ncm->enable = 1;
  430. ncm->data[0] = ntohl(cmd->mode);
  431. return 0;
  432. }
  433. static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
  434. {
  435. struct ncsi_rsp_pkt *rsp;
  436. struct ncsi_dev_priv *ndp = nr->ndp;
  437. struct ncsi_channel *nc;
  438. struct ncsi_channel_mode *ncm;
  439. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  440. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  441. NULL, &nc);
  442. if (!nc)
  443. return -ENODEV;
  444. /* Check if broadcast filter isn't enabled */
  445. ncm = &nc->modes[NCSI_MODE_BC];
  446. if (!ncm->enable)
  447. return 0;
  448. /* Update to broadcast filter mode */
  449. ncm->enable = 0;
  450. ncm->data[0] = 0;
  451. return 0;
  452. }
  453. static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
  454. {
  455. struct ncsi_cmd_egmf_pkt *cmd;
  456. struct ncsi_rsp_pkt *rsp;
  457. struct ncsi_dev_priv *ndp = nr->ndp;
  458. struct ncsi_channel *nc;
  459. struct ncsi_channel_mode *ncm;
  460. /* Find the channel */
  461. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  462. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  463. NULL, &nc);
  464. if (!nc)
  465. return -ENODEV;
  466. /* Check if multicast filter has been enabled */
  467. ncm = &nc->modes[NCSI_MODE_MC];
  468. if (ncm->enable)
  469. return 0;
  470. /* Update to multicast filter mode */
  471. cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
  472. ncm->enable = 1;
  473. ncm->data[0] = ntohl(cmd->mode);
  474. return 0;
  475. }
  476. static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
  477. {
  478. struct ncsi_rsp_pkt *rsp;
  479. struct ncsi_dev_priv *ndp = nr->ndp;
  480. struct ncsi_channel *nc;
  481. struct ncsi_channel_mode *ncm;
  482. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  483. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  484. NULL, &nc);
  485. if (!nc)
  486. return -ENODEV;
  487. /* Check if multicast filter has been enabled */
  488. ncm = &nc->modes[NCSI_MODE_MC];
  489. if (!ncm->enable)
  490. return 0;
  491. /* Update to multicast filter mode */
  492. ncm->enable = 0;
  493. ncm->data[0] = 0;
  494. return 0;
  495. }
  496. static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
  497. {
  498. struct ncsi_cmd_snfc_pkt *cmd;
  499. struct ncsi_rsp_pkt *rsp;
  500. struct ncsi_dev_priv *ndp = nr->ndp;
  501. struct ncsi_channel *nc;
  502. struct ncsi_channel_mode *ncm;
  503. /* Find the channel */
  504. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  505. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  506. NULL, &nc);
  507. if (!nc)
  508. return -ENODEV;
  509. /* Check if flow control has been enabled */
  510. ncm = &nc->modes[NCSI_MODE_FC];
  511. if (ncm->enable)
  512. return 0;
  513. /* Update to flow control mode */
  514. cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
  515. ncm->enable = 1;
  516. ncm->data[0] = cmd->mode;
  517. return 0;
  518. }
  519. /* Response handler for Get Mac Address command */
  520. static int ncsi_rsp_handler_oem_gma(struct ncsi_request *nr, int mfr_id)
  521. {
  522. struct ncsi_dev_priv *ndp = nr->ndp;
  523. struct sockaddr *saddr = &ndp->pending_mac;
  524. struct net_device *ndev = ndp->ndev.dev;
  525. struct ncsi_rsp_oem_pkt *rsp;
  526. u32 mac_addr_off = 0;
  527. /* Get the response header */
  528. rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
  529. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  530. if (mfr_id == NCSI_OEM_MFR_BCM_ID)
  531. mac_addr_off = BCM_MAC_ADDR_OFFSET;
  532. else if (mfr_id == NCSI_OEM_MFR_MLX_ID)
  533. mac_addr_off = MLX_MAC_ADDR_OFFSET;
  534. else if (mfr_id == NCSI_OEM_MFR_INTEL_ID)
  535. mac_addr_off = INTEL_MAC_ADDR_OFFSET;
  536. saddr->sa_family = ndev->type;
  537. memcpy(saddr->sa_data, &rsp->data[mac_addr_off], ETH_ALEN);
  538. if (mfr_id == NCSI_OEM_MFR_BCM_ID || mfr_id == NCSI_OEM_MFR_INTEL_ID)
  539. eth_addr_inc((u8 *)saddr->sa_data);
  540. if (!is_valid_ether_addr((const u8 *)saddr->sa_data))
  541. return -ENXIO;
  542. /* Set the flag for GMA command which should only be called once */
  543. ndp->gma_flag = 1;
  544. return 0;
  545. }
  546. /* Response handler for Mellanox card */
  547. static int ncsi_rsp_handler_oem_mlx(struct ncsi_request *nr)
  548. {
  549. struct ncsi_rsp_oem_mlx_pkt *mlx;
  550. struct ncsi_rsp_oem_pkt *rsp;
  551. /* Get the response header */
  552. rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
  553. mlx = (struct ncsi_rsp_oem_mlx_pkt *)(rsp->data);
  554. if (mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
  555. mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
  556. return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_MLX_ID);
  557. return 0;
  558. }
  559. /* Response handler for Broadcom card */
  560. static int ncsi_rsp_handler_oem_bcm(struct ncsi_request *nr)
  561. {
  562. struct ncsi_rsp_oem_bcm_pkt *bcm;
  563. struct ncsi_rsp_oem_pkt *rsp;
  564. /* Get the response header */
  565. rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
  566. bcm = (struct ncsi_rsp_oem_bcm_pkt *)(rsp->data);
  567. if (bcm->type == NCSI_OEM_BCM_CMD_GMA)
  568. return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_BCM_ID);
  569. return 0;
  570. }
  571. /* Response handler for Intel card */
  572. static int ncsi_rsp_handler_oem_intel(struct ncsi_request *nr)
  573. {
  574. struct ncsi_rsp_oem_intel_pkt *intel;
  575. struct ncsi_rsp_oem_pkt *rsp;
  576. /* Get the response header */
  577. rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
  578. intel = (struct ncsi_rsp_oem_intel_pkt *)(rsp->data);
  579. if (intel->cmd == NCSI_OEM_INTEL_CMD_GMA)
  580. return ncsi_rsp_handler_oem_gma(nr, NCSI_OEM_MFR_INTEL_ID);
  581. return 0;
  582. }
  583. static struct ncsi_rsp_oem_handler {
  584. unsigned int mfr_id;
  585. int (*handler)(struct ncsi_request *nr);
  586. } ncsi_rsp_oem_handlers[] = {
  587. { NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
  588. { NCSI_OEM_MFR_BCM_ID, ncsi_rsp_handler_oem_bcm },
  589. { NCSI_OEM_MFR_INTEL_ID, ncsi_rsp_handler_oem_intel }
  590. };
  591. /* Response handler for OEM command */
  592. static int ncsi_rsp_handler_oem(struct ncsi_request *nr)
  593. {
  594. struct ncsi_rsp_oem_handler *nrh = NULL;
  595. struct ncsi_rsp_oem_pkt *rsp;
  596. unsigned int mfr_id, i;
  597. /* Get the response header */
  598. rsp = (struct ncsi_rsp_oem_pkt *)skb_network_header(nr->rsp);
  599. mfr_id = ntohl(rsp->mfr_id);
  600. /* Check for manufacturer id and Find the handler */
  601. for (i = 0; i < ARRAY_SIZE(ncsi_rsp_oem_handlers); i++) {
  602. if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
  603. if (ncsi_rsp_oem_handlers[i].handler)
  604. nrh = &ncsi_rsp_oem_handlers[i];
  605. else
  606. nrh = NULL;
  607. break;
  608. }
  609. }
  610. if (!nrh) {
  611. netdev_err(nr->ndp->ndev.dev, "Received unrecognized OEM packet with MFR-ID (0x%x)\n",
  612. mfr_id);
  613. return -ENOENT;
  614. }
  615. /* Process the packet */
  616. return nrh->handler(nr);
  617. }
  618. static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
  619. {
  620. struct ncsi_rsp_gvi_pkt *rsp;
  621. struct ncsi_dev_priv *ndp = nr->ndp;
  622. struct ncsi_channel *nc;
  623. struct ncsi_channel_version *ncv;
  624. int i;
  625. /* Find the channel */
  626. rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
  627. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  628. NULL, &nc);
  629. if (!nc)
  630. return -ENODEV;
  631. /* Update channel's version info
  632. *
  633. * Major, minor, and update fields are supposed to be
  634. * unsigned integers encoded as packed BCD.
  635. *
  636. * Alpha1 and alpha2 are ISO/IEC 8859-1 characters.
  637. */
  638. ncv = &nc->version;
  639. ncv->major = decode_bcd_u8(rsp->major);
  640. ncv->minor = decode_bcd_u8(rsp->minor);
  641. ncv->update = decode_bcd_u8(rsp->update);
  642. ncv->alpha1 = rsp->alpha1;
  643. ncv->alpha2 = rsp->alpha2;
  644. memcpy(ncv->fw_name, rsp->fw_name, 12);
  645. ncv->fw_name[12] = '\0';
  646. ncv->fw_version = ntohl(rsp->fw_version);
  647. for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
  648. ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
  649. ncv->mf_id = ntohl(rsp->mf_id);
  650. return 0;
  651. }
  652. static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
  653. {
  654. struct ncsi_rsp_gc_pkt *rsp;
  655. struct ncsi_dev_priv *ndp = nr->ndp;
  656. struct ncsi_channel *nc;
  657. struct ncsi_package *np;
  658. size_t size;
  659. /* Find the channel */
  660. rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
  661. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  662. &np, &nc);
  663. if (!nc)
  664. return -ENODEV;
  665. /* Update channel's capabilities */
  666. nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
  667. NCSI_CAP_GENERIC_MASK;
  668. nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
  669. NCSI_CAP_BC_MASK;
  670. nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
  671. NCSI_CAP_MC_MASK;
  672. nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
  673. nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
  674. NCSI_CAP_AEN_MASK;
  675. nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
  676. NCSI_CAP_VLAN_MASK;
  677. size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
  678. nc->mac_filter.addrs = kzalloc(size, GFP_ATOMIC);
  679. if (!nc->mac_filter.addrs)
  680. return -ENOMEM;
  681. nc->mac_filter.n_uc = rsp->uc_cnt;
  682. nc->mac_filter.n_mc = rsp->mc_cnt;
  683. nc->mac_filter.n_mixed = rsp->mixed_cnt;
  684. nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
  685. sizeof(*nc->vlan_filter.vids),
  686. GFP_ATOMIC);
  687. if (!nc->vlan_filter.vids)
  688. return -ENOMEM;
  689. /* Set VLAN filters active so they are cleared in the first
  690. * configuration state
  691. */
  692. nc->vlan_filter.bitmap = U64_MAX;
  693. nc->vlan_filter.n_vids = rsp->vlan_cnt;
  694. np->ndp->channel_count = rsp->channel_cnt;
  695. return 0;
  696. }
  697. static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
  698. {
  699. struct ncsi_channel_vlan_filter *ncvf;
  700. struct ncsi_channel_mac_filter *ncmf;
  701. struct ncsi_dev_priv *ndp = nr->ndp;
  702. struct ncsi_rsp_gp_pkt *rsp;
  703. struct ncsi_channel *nc;
  704. unsigned short enable;
  705. unsigned char *pdata;
  706. unsigned long flags;
  707. void *bitmap;
  708. int i;
  709. /* Find the channel */
  710. rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
  711. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  712. NULL, &nc);
  713. if (!nc)
  714. return -ENODEV;
  715. /* Modes with explicit enabled indications */
  716. if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
  717. nc->modes[NCSI_MODE_BC].enable = 1;
  718. nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
  719. }
  720. if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
  721. nc->modes[NCSI_MODE_ENABLE].enable = 1;
  722. if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
  723. nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
  724. if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
  725. nc->modes[NCSI_MODE_MC].enable = 1;
  726. /* Modes without explicit enabled indications */
  727. nc->modes[NCSI_MODE_LINK].enable = 1;
  728. nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
  729. nc->modes[NCSI_MODE_VLAN].enable = 1;
  730. nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
  731. nc->modes[NCSI_MODE_FC].enable = 1;
  732. nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
  733. nc->modes[NCSI_MODE_AEN].enable = 1;
  734. nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
  735. /* MAC addresses filter table */
  736. pdata = (unsigned char *)rsp + 48;
  737. enable = rsp->mac_enable;
  738. ncmf = &nc->mac_filter;
  739. spin_lock_irqsave(&nc->lock, flags);
  740. bitmap = &ncmf->bitmap;
  741. for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
  742. if (!(enable & (0x1 << i)))
  743. clear_bit(i, bitmap);
  744. else
  745. set_bit(i, bitmap);
  746. memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
  747. }
  748. spin_unlock_irqrestore(&nc->lock, flags);
  749. /* VLAN filter table */
  750. enable = ntohs(rsp->vlan_enable);
  751. ncvf = &nc->vlan_filter;
  752. bitmap = &ncvf->bitmap;
  753. spin_lock_irqsave(&nc->lock, flags);
  754. for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
  755. if (!(enable & (0x1 << i)))
  756. clear_bit(i, bitmap);
  757. else
  758. set_bit(i, bitmap);
  759. ncvf->vids[i] = ntohs(*(__be16 *)pdata);
  760. }
  761. spin_unlock_irqrestore(&nc->lock, flags);
  762. return 0;
  763. }
  764. static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
  765. {
  766. struct ncsi_rsp_gcps_pkt *rsp;
  767. struct ncsi_dev_priv *ndp = nr->ndp;
  768. struct ncsi_channel *nc;
  769. struct ncsi_channel_stats *ncs;
  770. /* Find the channel */
  771. rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
  772. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  773. NULL, &nc);
  774. if (!nc)
  775. return -ENODEV;
  776. /* Update HNC's statistics */
  777. ncs = &nc->stats;
  778. ncs->hnc_cnt = be64_to_cpu(rsp->cnt);
  779. ncs->hnc_rx_bytes = be64_to_cpu(rsp->rx_bytes);
  780. ncs->hnc_tx_bytes = be64_to_cpu(rsp->tx_bytes);
  781. ncs->hnc_rx_uc_pkts = be64_to_cpu(rsp->rx_uc_pkts);
  782. ncs->hnc_rx_mc_pkts = be64_to_cpu(rsp->rx_mc_pkts);
  783. ncs->hnc_rx_bc_pkts = be64_to_cpu(rsp->rx_bc_pkts);
  784. ncs->hnc_tx_uc_pkts = be64_to_cpu(rsp->tx_uc_pkts);
  785. ncs->hnc_tx_mc_pkts = be64_to_cpu(rsp->tx_mc_pkts);
  786. ncs->hnc_tx_bc_pkts = be64_to_cpu(rsp->tx_bc_pkts);
  787. ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
  788. ncs->hnc_align_err = ntohl(rsp->align_err);
  789. ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
  790. ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
  791. ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
  792. ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
  793. ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
  794. ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
  795. ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
  796. ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
  797. ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
  798. ncs->hnc_l_collision = ntohl(rsp->l_collision);
  799. ncs->hnc_e_collision = ntohl(rsp->e_collision);
  800. ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
  801. ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
  802. ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
  803. ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
  804. ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
  805. ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
  806. ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
  807. ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
  808. ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
  809. ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
  810. ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
  811. ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
  812. ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
  813. ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
  814. ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
  815. ncs->hnc_rx_valid_bytes = be64_to_cpu(rsp->rx_valid_bytes);
  816. ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
  817. ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
  818. return 0;
  819. }
  820. static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
  821. {
  822. struct ncsi_rsp_gns_pkt *rsp;
  823. struct ncsi_dev_priv *ndp = nr->ndp;
  824. struct ncsi_channel *nc;
  825. struct ncsi_channel_stats *ncs;
  826. /* Find the channel */
  827. rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
  828. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  829. NULL, &nc);
  830. if (!nc)
  831. return -ENODEV;
  832. /* Update HNC's statistics */
  833. ncs = &nc->stats;
  834. ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
  835. ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
  836. ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
  837. ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
  838. ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
  839. ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
  840. ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
  841. return 0;
  842. }
  843. static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
  844. {
  845. struct ncsi_rsp_gnpts_pkt *rsp;
  846. struct ncsi_dev_priv *ndp = nr->ndp;
  847. struct ncsi_channel *nc;
  848. struct ncsi_channel_stats *ncs;
  849. /* Find the channel */
  850. rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
  851. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  852. NULL, &nc);
  853. if (!nc)
  854. return -ENODEV;
  855. /* Update HNC's statistics */
  856. ncs = &nc->stats;
  857. ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
  858. ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
  859. ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
  860. ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
  861. ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
  862. ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
  863. ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
  864. ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
  865. ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
  866. return 0;
  867. }
  868. static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
  869. {
  870. struct ncsi_rsp_gps_pkt *rsp;
  871. struct ncsi_dev_priv *ndp = nr->ndp;
  872. struct ncsi_package *np;
  873. /* Find the package */
  874. rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
  875. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  876. &np, NULL);
  877. if (!np)
  878. return -ENODEV;
  879. return 0;
  880. }
  881. static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
  882. {
  883. struct ncsi_rsp_gpuuid_pkt *rsp;
  884. struct ncsi_dev_priv *ndp = nr->ndp;
  885. struct ncsi_package *np;
  886. /* Find the package */
  887. rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
  888. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  889. &np, NULL);
  890. if (!np)
  891. return -ENODEV;
  892. memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
  893. return 0;
  894. }
  895. static int ncsi_rsp_handler_pldm(struct ncsi_request *nr)
  896. {
  897. return 0;
  898. }
  899. static int ncsi_rsp_handler_netlink(struct ncsi_request *nr)
  900. {
  901. struct ncsi_dev_priv *ndp = nr->ndp;
  902. struct ncsi_rsp_pkt *rsp;
  903. struct ncsi_package *np;
  904. struct ncsi_channel *nc;
  905. int ret;
  906. /* Find the package */
  907. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  908. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  909. &np, &nc);
  910. if (!np)
  911. return -ENODEV;
  912. ret = ncsi_send_netlink_rsp(nr, np, nc);
  913. return ret;
  914. }
  915. static int ncsi_rsp_handler_gmcma(struct ncsi_request *nr)
  916. {
  917. struct ncsi_dev_priv *ndp = nr->ndp;
  918. struct sockaddr *saddr = &ndp->pending_mac;
  919. struct net_device *ndev = ndp->ndev.dev;
  920. struct ncsi_rsp_gmcma_pkt *rsp;
  921. int i;
  922. rsp = (struct ncsi_rsp_gmcma_pkt *)skb_network_header(nr->rsp);
  923. ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  924. netdev_info(ndev, "NCSI: Received %d provisioned MAC addresses\n",
  925. rsp->address_count);
  926. for (i = 0; i < rsp->address_count; i++) {
  927. netdev_info(ndev, "NCSI: MAC address %d: %02x:%02x:%02x:%02x:%02x:%02x\n",
  928. i, rsp->addresses[i][0], rsp->addresses[i][1],
  929. rsp->addresses[i][2], rsp->addresses[i][3],
  930. rsp->addresses[i][4], rsp->addresses[i][5]);
  931. }
  932. saddr->sa_family = ndev->type;
  933. for (i = 0; i < rsp->address_count; i++) {
  934. if (!is_valid_ether_addr(rsp->addresses[i])) {
  935. netdev_warn(ndev, "NCSI: Unable to assign %pM to device\n",
  936. rsp->addresses[i]);
  937. continue;
  938. }
  939. memcpy(saddr->sa_data, rsp->addresses[i], ETH_ALEN);
  940. netdev_warn(ndev, "NCSI: Will set MAC address to %pM\n", saddr->sa_data);
  941. break;
  942. }
  943. ndp->gma_flag = 1;
  944. return 0;
  945. }
  946. static struct ncsi_rsp_handler {
  947. unsigned char type;
  948. int payload;
  949. int (*handler)(struct ncsi_request *nr);
  950. } ncsi_rsp_handlers[] = {
  951. { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
  952. { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
  953. { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
  954. { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
  955. { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
  956. { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
  957. { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
  958. { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
  959. { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
  960. { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
  961. { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
  962. { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
  963. { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
  964. { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
  965. { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
  966. { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
  967. { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
  968. { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
  969. { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
  970. { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
  971. { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
  972. { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
  973. { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
  974. { NCSI_PKT_RSP_GCPS, 204, ncsi_rsp_handler_gcps },
  975. { NCSI_PKT_RSP_GNS, 32, ncsi_rsp_handler_gns },
  976. { NCSI_PKT_RSP_GNPTS, 48, ncsi_rsp_handler_gnpts },
  977. { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
  978. { NCSI_PKT_RSP_OEM, -1, ncsi_rsp_handler_oem },
  979. { NCSI_PKT_RSP_PLDM, -1, ncsi_rsp_handler_pldm },
  980. { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid },
  981. { NCSI_PKT_RSP_QPNPR, -1, ncsi_rsp_handler_pldm },
  982. { NCSI_PKT_RSP_SNPR, -1, ncsi_rsp_handler_pldm },
  983. { NCSI_PKT_RSP_GMCMA, -1, ncsi_rsp_handler_gmcma },
  984. };
  985. int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
  986. struct packet_type *pt, struct net_device *orig_dev)
  987. {
  988. struct ncsi_rsp_handler *nrh = NULL;
  989. struct ncsi_dev *nd;
  990. struct ncsi_dev_priv *ndp;
  991. struct ncsi_request *nr;
  992. struct ncsi_pkt_hdr *hdr;
  993. unsigned long flags;
  994. int payload, i, ret;
  995. /* Find the NCSI device */
  996. nd = ncsi_find_dev(orig_dev);
  997. ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
  998. if (!ndp)
  999. return -ENODEV;
  1000. /* Check if it is AEN packet */
  1001. hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
  1002. if (hdr->type == NCSI_PKT_AEN)
  1003. return ncsi_aen_handler(ndp, skb);
  1004. /* Find the handler */
  1005. for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
  1006. if (ncsi_rsp_handlers[i].type == hdr->type) {
  1007. if (ncsi_rsp_handlers[i].handler)
  1008. nrh = &ncsi_rsp_handlers[i];
  1009. else
  1010. nrh = NULL;
  1011. break;
  1012. }
  1013. }
  1014. if (!nrh) {
  1015. netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
  1016. hdr->type);
  1017. return -ENOENT;
  1018. }
  1019. /* Associate with the request */
  1020. spin_lock_irqsave(&ndp->lock, flags);
  1021. nr = &ndp->requests[hdr->id];
  1022. if (!nr->used) {
  1023. spin_unlock_irqrestore(&ndp->lock, flags);
  1024. return -ENODEV;
  1025. }
  1026. nr->rsp = skb;
  1027. if (!nr->enabled) {
  1028. spin_unlock_irqrestore(&ndp->lock, flags);
  1029. ret = -ENOENT;
  1030. goto out;
  1031. }
  1032. /* Validate the packet */
  1033. spin_unlock_irqrestore(&ndp->lock, flags);
  1034. payload = nrh->payload;
  1035. if (payload < 0)
  1036. payload = ntohs(hdr->length);
  1037. ret = ncsi_validate_rsp_pkt(nr, payload);
  1038. if (ret) {
  1039. netdev_warn(ndp->ndev.dev,
  1040. "NCSI: 'bad' packet ignored for type 0x%x\n",
  1041. hdr->type);
  1042. if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
  1043. if (ret == -EPERM)
  1044. goto out_netlink;
  1045. else
  1046. ncsi_send_netlink_err(ndp->ndev.dev,
  1047. nr->snd_seq,
  1048. nr->snd_portid,
  1049. &nr->nlhdr,
  1050. ret);
  1051. }
  1052. goto out;
  1053. }
  1054. /* Process the packet */
  1055. ret = nrh->handler(nr);
  1056. if (ret)
  1057. netdev_err(ndp->ndev.dev,
  1058. "NCSI: Handler for packet type 0x%x returned %d\n",
  1059. hdr->type, ret);
  1060. out_netlink:
  1061. if (nr->flags == NCSI_REQ_FLAG_NETLINK_DRIVEN) {
  1062. ret = ncsi_rsp_handler_netlink(nr);
  1063. if (ret) {
  1064. netdev_err(ndp->ndev.dev,
  1065. "NCSI: Netlink handler for packet type 0x%x returned %d\n",
  1066. hdr->type, ret);
  1067. }
  1068. }
  1069. out:
  1070. ncsi_free_request(nr);
  1071. return ret;
  1072. }