ncsi-rsp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 > 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 > ncf->n_uc + ncf->n_mc + ncf->n_mixed)
  373. return -ERANGE;
  374. index = (cmd->index - 1) * ETH_ALEN;
  375. spin_lock_irqsave(&nc->lock, flags);
  376. if (enabled) {
  377. set_bit(cmd->index - 1, bitmap);
  378. memcpy(&ncf->addrs[index], cmd->mac, ETH_ALEN);
  379. } else {
  380. clear_bit(cmd->index - 1, bitmap);
  381. memset(&ncf->addrs[index], 0, ETH_ALEN);
  382. }
  383. spin_unlock_irqrestore(&nc->lock, flags);
  384. return 0;
  385. }
  386. static int ncsi_rsp_handler_ebf(struct ncsi_request *nr)
  387. {
  388. struct ncsi_cmd_ebf_pkt *cmd;
  389. struct ncsi_rsp_pkt *rsp;
  390. struct ncsi_dev_priv *ndp = nr->ndp;
  391. struct ncsi_channel *nc;
  392. struct ncsi_channel_mode *ncm;
  393. /* Find the package and channel */
  394. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  395. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, NULL, &nc);
  396. if (!nc)
  397. return -ENODEV;
  398. /* Check if broadcast filter has been enabled */
  399. ncm = &nc->modes[NCSI_MODE_BC];
  400. if (ncm->enable)
  401. return 0;
  402. /* Update to broadcast filter mode */
  403. cmd = (struct ncsi_cmd_ebf_pkt *)skb_network_header(nr->cmd);
  404. ncm->enable = 1;
  405. ncm->data[0] = ntohl(cmd->mode);
  406. return 0;
  407. }
  408. static int ncsi_rsp_handler_dbf(struct ncsi_request *nr)
  409. {
  410. struct ncsi_rsp_pkt *rsp;
  411. struct ncsi_dev_priv *ndp = nr->ndp;
  412. struct ncsi_channel *nc;
  413. struct ncsi_channel_mode *ncm;
  414. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  415. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  416. NULL, &nc);
  417. if (!nc)
  418. return -ENODEV;
  419. /* Check if broadcast filter isn't enabled */
  420. ncm = &nc->modes[NCSI_MODE_BC];
  421. if (!ncm->enable)
  422. return 0;
  423. /* Update to broadcast filter mode */
  424. ncm->enable = 0;
  425. ncm->data[0] = 0;
  426. return 0;
  427. }
  428. static int ncsi_rsp_handler_egmf(struct ncsi_request *nr)
  429. {
  430. struct ncsi_cmd_egmf_pkt *cmd;
  431. struct ncsi_rsp_pkt *rsp;
  432. struct ncsi_dev_priv *ndp = nr->ndp;
  433. struct ncsi_channel *nc;
  434. struct ncsi_channel_mode *ncm;
  435. /* Find the channel */
  436. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  437. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  438. NULL, &nc);
  439. if (!nc)
  440. return -ENODEV;
  441. /* Check if multicast filter has been enabled */
  442. ncm = &nc->modes[NCSI_MODE_MC];
  443. if (ncm->enable)
  444. return 0;
  445. /* Update to multicast filter mode */
  446. cmd = (struct ncsi_cmd_egmf_pkt *)skb_network_header(nr->cmd);
  447. ncm->enable = 1;
  448. ncm->data[0] = ntohl(cmd->mode);
  449. return 0;
  450. }
  451. static int ncsi_rsp_handler_dgmf(struct ncsi_request *nr)
  452. {
  453. struct ncsi_rsp_pkt *rsp;
  454. struct ncsi_dev_priv *ndp = nr->ndp;
  455. struct ncsi_channel *nc;
  456. struct ncsi_channel_mode *ncm;
  457. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  458. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  459. NULL, &nc);
  460. if (!nc)
  461. return -ENODEV;
  462. /* Check if multicast filter has been enabled */
  463. ncm = &nc->modes[NCSI_MODE_MC];
  464. if (!ncm->enable)
  465. return 0;
  466. /* Update to multicast filter mode */
  467. ncm->enable = 0;
  468. ncm->data[0] = 0;
  469. return 0;
  470. }
  471. static int ncsi_rsp_handler_snfc(struct ncsi_request *nr)
  472. {
  473. struct ncsi_cmd_snfc_pkt *cmd;
  474. struct ncsi_rsp_pkt *rsp;
  475. struct ncsi_dev_priv *ndp = nr->ndp;
  476. struct ncsi_channel *nc;
  477. struct ncsi_channel_mode *ncm;
  478. /* Find the channel */
  479. rsp = (struct ncsi_rsp_pkt *)skb_network_header(nr->rsp);
  480. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  481. NULL, &nc);
  482. if (!nc)
  483. return -ENODEV;
  484. /* Check if flow control has been enabled */
  485. ncm = &nc->modes[NCSI_MODE_FC];
  486. if (ncm->enable)
  487. return 0;
  488. /* Update to flow control mode */
  489. cmd = (struct ncsi_cmd_snfc_pkt *)skb_network_header(nr->cmd);
  490. ncm->enable = 1;
  491. ncm->data[0] = cmd->mode;
  492. return 0;
  493. }
  494. static int ncsi_rsp_handler_gvi(struct ncsi_request *nr)
  495. {
  496. struct ncsi_rsp_gvi_pkt *rsp;
  497. struct ncsi_dev_priv *ndp = nr->ndp;
  498. struct ncsi_channel *nc;
  499. struct ncsi_channel_version *ncv;
  500. int i;
  501. /* Find the channel */
  502. rsp = (struct ncsi_rsp_gvi_pkt *)skb_network_header(nr->rsp);
  503. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  504. NULL, &nc);
  505. if (!nc)
  506. return -ENODEV;
  507. /* Update to channel's version info */
  508. ncv = &nc->version;
  509. ncv->version = ntohl(rsp->ncsi_version);
  510. ncv->alpha2 = rsp->alpha2;
  511. memcpy(ncv->fw_name, rsp->fw_name, 12);
  512. ncv->fw_version = ntohl(rsp->fw_version);
  513. for (i = 0; i < ARRAY_SIZE(ncv->pci_ids); i++)
  514. ncv->pci_ids[i] = ntohs(rsp->pci_ids[i]);
  515. ncv->mf_id = ntohl(rsp->mf_id);
  516. return 0;
  517. }
  518. static int ncsi_rsp_handler_gc(struct ncsi_request *nr)
  519. {
  520. struct ncsi_rsp_gc_pkt *rsp;
  521. struct ncsi_dev_priv *ndp = nr->ndp;
  522. struct ncsi_channel *nc;
  523. size_t size;
  524. /* Find the channel */
  525. rsp = (struct ncsi_rsp_gc_pkt *)skb_network_header(nr->rsp);
  526. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  527. NULL, &nc);
  528. if (!nc)
  529. return -ENODEV;
  530. /* Update channel's capabilities */
  531. nc->caps[NCSI_CAP_GENERIC].cap = ntohl(rsp->cap) &
  532. NCSI_CAP_GENERIC_MASK;
  533. nc->caps[NCSI_CAP_BC].cap = ntohl(rsp->bc_cap) &
  534. NCSI_CAP_BC_MASK;
  535. nc->caps[NCSI_CAP_MC].cap = ntohl(rsp->mc_cap) &
  536. NCSI_CAP_MC_MASK;
  537. nc->caps[NCSI_CAP_BUFFER].cap = ntohl(rsp->buf_cap);
  538. nc->caps[NCSI_CAP_AEN].cap = ntohl(rsp->aen_cap) &
  539. NCSI_CAP_AEN_MASK;
  540. nc->caps[NCSI_CAP_VLAN].cap = rsp->vlan_mode &
  541. NCSI_CAP_VLAN_MASK;
  542. size = (rsp->uc_cnt + rsp->mc_cnt + rsp->mixed_cnt) * ETH_ALEN;
  543. nc->mac_filter.addrs = kzalloc(size, GFP_KERNEL);
  544. if (!nc->mac_filter.addrs)
  545. return -ENOMEM;
  546. nc->mac_filter.n_uc = rsp->uc_cnt;
  547. nc->mac_filter.n_mc = rsp->mc_cnt;
  548. nc->mac_filter.n_mixed = rsp->mixed_cnt;
  549. nc->vlan_filter.vids = kcalloc(rsp->vlan_cnt,
  550. sizeof(*nc->vlan_filter.vids),
  551. GFP_KERNEL);
  552. if (!nc->vlan_filter.vids)
  553. return -ENOMEM;
  554. /* Set VLAN filters active so they are cleared in the first
  555. * configuration state
  556. */
  557. nc->vlan_filter.bitmap = U64_MAX;
  558. nc->vlan_filter.n_vids = rsp->vlan_cnt;
  559. return 0;
  560. }
  561. static int ncsi_rsp_handler_gp(struct ncsi_request *nr)
  562. {
  563. struct ncsi_channel_vlan_filter *ncvf;
  564. struct ncsi_channel_mac_filter *ncmf;
  565. struct ncsi_dev_priv *ndp = nr->ndp;
  566. struct ncsi_rsp_gp_pkt *rsp;
  567. struct ncsi_channel *nc;
  568. unsigned short enable;
  569. unsigned char *pdata;
  570. unsigned long flags;
  571. void *bitmap;
  572. int i;
  573. /* Find the channel */
  574. rsp = (struct ncsi_rsp_gp_pkt *)skb_network_header(nr->rsp);
  575. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  576. NULL, &nc);
  577. if (!nc)
  578. return -ENODEV;
  579. /* Modes with explicit enabled indications */
  580. if (ntohl(rsp->valid_modes) & 0x1) { /* BC filter mode */
  581. nc->modes[NCSI_MODE_BC].enable = 1;
  582. nc->modes[NCSI_MODE_BC].data[0] = ntohl(rsp->bc_mode);
  583. }
  584. if (ntohl(rsp->valid_modes) & 0x2) /* Channel enabled */
  585. nc->modes[NCSI_MODE_ENABLE].enable = 1;
  586. if (ntohl(rsp->valid_modes) & 0x4) /* Channel Tx enabled */
  587. nc->modes[NCSI_MODE_TX_ENABLE].enable = 1;
  588. if (ntohl(rsp->valid_modes) & 0x8) /* MC filter mode */
  589. nc->modes[NCSI_MODE_MC].enable = 1;
  590. /* Modes without explicit enabled indications */
  591. nc->modes[NCSI_MODE_LINK].enable = 1;
  592. nc->modes[NCSI_MODE_LINK].data[0] = ntohl(rsp->link_mode);
  593. nc->modes[NCSI_MODE_VLAN].enable = 1;
  594. nc->modes[NCSI_MODE_VLAN].data[0] = rsp->vlan_mode;
  595. nc->modes[NCSI_MODE_FC].enable = 1;
  596. nc->modes[NCSI_MODE_FC].data[0] = rsp->fc_mode;
  597. nc->modes[NCSI_MODE_AEN].enable = 1;
  598. nc->modes[NCSI_MODE_AEN].data[0] = ntohl(rsp->aen_mode);
  599. /* MAC addresses filter table */
  600. pdata = (unsigned char *)rsp + 48;
  601. enable = rsp->mac_enable;
  602. ncmf = &nc->mac_filter;
  603. spin_lock_irqsave(&nc->lock, flags);
  604. bitmap = &ncmf->bitmap;
  605. for (i = 0; i < rsp->mac_cnt; i++, pdata += 6) {
  606. if (!(enable & (0x1 << i)))
  607. clear_bit(i, bitmap);
  608. else
  609. set_bit(i, bitmap);
  610. memcpy(&ncmf->addrs[i * ETH_ALEN], pdata, ETH_ALEN);
  611. }
  612. spin_unlock_irqrestore(&nc->lock, flags);
  613. /* VLAN filter table */
  614. enable = ntohs(rsp->vlan_enable);
  615. ncvf = &nc->vlan_filter;
  616. bitmap = &ncvf->bitmap;
  617. spin_lock_irqsave(&nc->lock, flags);
  618. for (i = 0; i < rsp->vlan_cnt; i++, pdata += 2) {
  619. if (!(enable & (0x1 << i)))
  620. clear_bit(i, bitmap);
  621. else
  622. set_bit(i, bitmap);
  623. ncvf->vids[i] = ntohs(*(__be16 *)pdata);
  624. }
  625. spin_unlock_irqrestore(&nc->lock, flags);
  626. return 0;
  627. }
  628. static int ncsi_rsp_handler_gcps(struct ncsi_request *nr)
  629. {
  630. struct ncsi_rsp_gcps_pkt *rsp;
  631. struct ncsi_dev_priv *ndp = nr->ndp;
  632. struct ncsi_channel *nc;
  633. struct ncsi_channel_stats *ncs;
  634. /* Find the channel */
  635. rsp = (struct ncsi_rsp_gcps_pkt *)skb_network_header(nr->rsp);
  636. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  637. NULL, &nc);
  638. if (!nc)
  639. return -ENODEV;
  640. /* Update HNC's statistics */
  641. ncs = &nc->stats;
  642. ncs->hnc_cnt_hi = ntohl(rsp->cnt_hi);
  643. ncs->hnc_cnt_lo = ntohl(rsp->cnt_lo);
  644. ncs->hnc_rx_bytes = ntohl(rsp->rx_bytes);
  645. ncs->hnc_tx_bytes = ntohl(rsp->tx_bytes);
  646. ncs->hnc_rx_uc_pkts = ntohl(rsp->rx_uc_pkts);
  647. ncs->hnc_rx_mc_pkts = ntohl(rsp->rx_mc_pkts);
  648. ncs->hnc_rx_bc_pkts = ntohl(rsp->rx_bc_pkts);
  649. ncs->hnc_tx_uc_pkts = ntohl(rsp->tx_uc_pkts);
  650. ncs->hnc_tx_mc_pkts = ntohl(rsp->tx_mc_pkts);
  651. ncs->hnc_tx_bc_pkts = ntohl(rsp->tx_bc_pkts);
  652. ncs->hnc_fcs_err = ntohl(rsp->fcs_err);
  653. ncs->hnc_align_err = ntohl(rsp->align_err);
  654. ncs->hnc_false_carrier = ntohl(rsp->false_carrier);
  655. ncs->hnc_runt_pkts = ntohl(rsp->runt_pkts);
  656. ncs->hnc_jabber_pkts = ntohl(rsp->jabber_pkts);
  657. ncs->hnc_rx_pause_xon = ntohl(rsp->rx_pause_xon);
  658. ncs->hnc_rx_pause_xoff = ntohl(rsp->rx_pause_xoff);
  659. ncs->hnc_tx_pause_xon = ntohl(rsp->tx_pause_xon);
  660. ncs->hnc_tx_pause_xoff = ntohl(rsp->tx_pause_xoff);
  661. ncs->hnc_tx_s_collision = ntohl(rsp->tx_s_collision);
  662. ncs->hnc_tx_m_collision = ntohl(rsp->tx_m_collision);
  663. ncs->hnc_l_collision = ntohl(rsp->l_collision);
  664. ncs->hnc_e_collision = ntohl(rsp->e_collision);
  665. ncs->hnc_rx_ctl_frames = ntohl(rsp->rx_ctl_frames);
  666. ncs->hnc_rx_64_frames = ntohl(rsp->rx_64_frames);
  667. ncs->hnc_rx_127_frames = ntohl(rsp->rx_127_frames);
  668. ncs->hnc_rx_255_frames = ntohl(rsp->rx_255_frames);
  669. ncs->hnc_rx_511_frames = ntohl(rsp->rx_511_frames);
  670. ncs->hnc_rx_1023_frames = ntohl(rsp->rx_1023_frames);
  671. ncs->hnc_rx_1522_frames = ntohl(rsp->rx_1522_frames);
  672. ncs->hnc_rx_9022_frames = ntohl(rsp->rx_9022_frames);
  673. ncs->hnc_tx_64_frames = ntohl(rsp->tx_64_frames);
  674. ncs->hnc_tx_127_frames = ntohl(rsp->tx_127_frames);
  675. ncs->hnc_tx_255_frames = ntohl(rsp->tx_255_frames);
  676. ncs->hnc_tx_511_frames = ntohl(rsp->tx_511_frames);
  677. ncs->hnc_tx_1023_frames = ntohl(rsp->tx_1023_frames);
  678. ncs->hnc_tx_1522_frames = ntohl(rsp->tx_1522_frames);
  679. ncs->hnc_tx_9022_frames = ntohl(rsp->tx_9022_frames);
  680. ncs->hnc_rx_valid_bytes = ntohl(rsp->rx_valid_bytes);
  681. ncs->hnc_rx_runt_pkts = ntohl(rsp->rx_runt_pkts);
  682. ncs->hnc_rx_jabber_pkts = ntohl(rsp->rx_jabber_pkts);
  683. return 0;
  684. }
  685. static int ncsi_rsp_handler_gns(struct ncsi_request *nr)
  686. {
  687. struct ncsi_rsp_gns_pkt *rsp;
  688. struct ncsi_dev_priv *ndp = nr->ndp;
  689. struct ncsi_channel *nc;
  690. struct ncsi_channel_stats *ncs;
  691. /* Find the channel */
  692. rsp = (struct ncsi_rsp_gns_pkt *)skb_network_header(nr->rsp);
  693. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  694. NULL, &nc);
  695. if (!nc)
  696. return -ENODEV;
  697. /* Update HNC's statistics */
  698. ncs = &nc->stats;
  699. ncs->ncsi_rx_cmds = ntohl(rsp->rx_cmds);
  700. ncs->ncsi_dropped_cmds = ntohl(rsp->dropped_cmds);
  701. ncs->ncsi_cmd_type_errs = ntohl(rsp->cmd_type_errs);
  702. ncs->ncsi_cmd_csum_errs = ntohl(rsp->cmd_csum_errs);
  703. ncs->ncsi_rx_pkts = ntohl(rsp->rx_pkts);
  704. ncs->ncsi_tx_pkts = ntohl(rsp->tx_pkts);
  705. ncs->ncsi_tx_aen_pkts = ntohl(rsp->tx_aen_pkts);
  706. return 0;
  707. }
  708. static int ncsi_rsp_handler_gnpts(struct ncsi_request *nr)
  709. {
  710. struct ncsi_rsp_gnpts_pkt *rsp;
  711. struct ncsi_dev_priv *ndp = nr->ndp;
  712. struct ncsi_channel *nc;
  713. struct ncsi_channel_stats *ncs;
  714. /* Find the channel */
  715. rsp = (struct ncsi_rsp_gnpts_pkt *)skb_network_header(nr->rsp);
  716. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  717. NULL, &nc);
  718. if (!nc)
  719. return -ENODEV;
  720. /* Update HNC's statistics */
  721. ncs = &nc->stats;
  722. ncs->pt_tx_pkts = ntohl(rsp->tx_pkts);
  723. ncs->pt_tx_dropped = ntohl(rsp->tx_dropped);
  724. ncs->pt_tx_channel_err = ntohl(rsp->tx_channel_err);
  725. ncs->pt_tx_us_err = ntohl(rsp->tx_us_err);
  726. ncs->pt_rx_pkts = ntohl(rsp->rx_pkts);
  727. ncs->pt_rx_dropped = ntohl(rsp->rx_dropped);
  728. ncs->pt_rx_channel_err = ntohl(rsp->rx_channel_err);
  729. ncs->pt_rx_us_err = ntohl(rsp->rx_us_err);
  730. ncs->pt_rx_os_err = ntohl(rsp->rx_os_err);
  731. return 0;
  732. }
  733. static int ncsi_rsp_handler_gps(struct ncsi_request *nr)
  734. {
  735. struct ncsi_rsp_gps_pkt *rsp;
  736. struct ncsi_dev_priv *ndp = nr->ndp;
  737. struct ncsi_package *np;
  738. /* Find the package */
  739. rsp = (struct ncsi_rsp_gps_pkt *)skb_network_header(nr->rsp);
  740. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  741. &np, NULL);
  742. if (!np)
  743. return -ENODEV;
  744. return 0;
  745. }
  746. static int ncsi_rsp_handler_gpuuid(struct ncsi_request *nr)
  747. {
  748. struct ncsi_rsp_gpuuid_pkt *rsp;
  749. struct ncsi_dev_priv *ndp = nr->ndp;
  750. struct ncsi_package *np;
  751. /* Find the package */
  752. rsp = (struct ncsi_rsp_gpuuid_pkt *)skb_network_header(nr->rsp);
  753. ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel,
  754. &np, NULL);
  755. if (!np)
  756. return -ENODEV;
  757. memcpy(np->uuid, rsp->uuid, sizeof(rsp->uuid));
  758. return 0;
  759. }
  760. static struct ncsi_rsp_handler {
  761. unsigned char type;
  762. int payload;
  763. int (*handler)(struct ncsi_request *nr);
  764. } ncsi_rsp_handlers[] = {
  765. { NCSI_PKT_RSP_CIS, 4, ncsi_rsp_handler_cis },
  766. { NCSI_PKT_RSP_SP, 4, ncsi_rsp_handler_sp },
  767. { NCSI_PKT_RSP_DP, 4, ncsi_rsp_handler_dp },
  768. { NCSI_PKT_RSP_EC, 4, ncsi_rsp_handler_ec },
  769. { NCSI_PKT_RSP_DC, 4, ncsi_rsp_handler_dc },
  770. { NCSI_PKT_RSP_RC, 4, ncsi_rsp_handler_rc },
  771. { NCSI_PKT_RSP_ECNT, 4, ncsi_rsp_handler_ecnt },
  772. { NCSI_PKT_RSP_DCNT, 4, ncsi_rsp_handler_dcnt },
  773. { NCSI_PKT_RSP_AE, 4, ncsi_rsp_handler_ae },
  774. { NCSI_PKT_RSP_SL, 4, ncsi_rsp_handler_sl },
  775. { NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
  776. { NCSI_PKT_RSP_SVF, 4, ncsi_rsp_handler_svf },
  777. { NCSI_PKT_RSP_EV, 4, ncsi_rsp_handler_ev },
  778. { NCSI_PKT_RSP_DV, 4, ncsi_rsp_handler_dv },
  779. { NCSI_PKT_RSP_SMA, 4, ncsi_rsp_handler_sma },
  780. { NCSI_PKT_RSP_EBF, 4, ncsi_rsp_handler_ebf },
  781. { NCSI_PKT_RSP_DBF, 4, ncsi_rsp_handler_dbf },
  782. { NCSI_PKT_RSP_EGMF, 4, ncsi_rsp_handler_egmf },
  783. { NCSI_PKT_RSP_DGMF, 4, ncsi_rsp_handler_dgmf },
  784. { NCSI_PKT_RSP_SNFC, 4, ncsi_rsp_handler_snfc },
  785. { NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
  786. { NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
  787. { NCSI_PKT_RSP_GP, -1, ncsi_rsp_handler_gp },
  788. { NCSI_PKT_RSP_GCPS, 172, ncsi_rsp_handler_gcps },
  789. { NCSI_PKT_RSP_GNS, 172, ncsi_rsp_handler_gns },
  790. { NCSI_PKT_RSP_GNPTS, 172, ncsi_rsp_handler_gnpts },
  791. { NCSI_PKT_RSP_GPS, 8, ncsi_rsp_handler_gps },
  792. { NCSI_PKT_RSP_OEM, 0, NULL },
  793. { NCSI_PKT_RSP_PLDM, 0, NULL },
  794. { NCSI_PKT_RSP_GPUUID, 20, ncsi_rsp_handler_gpuuid }
  795. };
  796. int ncsi_rcv_rsp(struct sk_buff *skb, struct net_device *dev,
  797. struct packet_type *pt, struct net_device *orig_dev)
  798. {
  799. struct ncsi_rsp_handler *nrh = NULL;
  800. struct ncsi_dev *nd;
  801. struct ncsi_dev_priv *ndp;
  802. struct ncsi_request *nr;
  803. struct ncsi_pkt_hdr *hdr;
  804. unsigned long flags;
  805. int payload, i, ret;
  806. /* Find the NCSI device */
  807. nd = ncsi_find_dev(dev);
  808. ndp = nd ? TO_NCSI_DEV_PRIV(nd) : NULL;
  809. if (!ndp)
  810. return -ENODEV;
  811. /* Check if it is AEN packet */
  812. hdr = (struct ncsi_pkt_hdr *)skb_network_header(skb);
  813. if (hdr->type == NCSI_PKT_AEN)
  814. return ncsi_aen_handler(ndp, skb);
  815. /* Find the handler */
  816. for (i = 0; i < ARRAY_SIZE(ncsi_rsp_handlers); i++) {
  817. if (ncsi_rsp_handlers[i].type == hdr->type) {
  818. if (ncsi_rsp_handlers[i].handler)
  819. nrh = &ncsi_rsp_handlers[i];
  820. else
  821. nrh = NULL;
  822. break;
  823. }
  824. }
  825. if (!nrh) {
  826. netdev_err(nd->dev, "Received unrecognized packet (0x%x)\n",
  827. hdr->type);
  828. return -ENOENT;
  829. }
  830. /* Associate with the request */
  831. spin_lock_irqsave(&ndp->lock, flags);
  832. nr = &ndp->requests[hdr->id];
  833. if (!nr->used) {
  834. spin_unlock_irqrestore(&ndp->lock, flags);
  835. return -ENODEV;
  836. }
  837. nr->rsp = skb;
  838. if (!nr->enabled) {
  839. spin_unlock_irqrestore(&ndp->lock, flags);
  840. ret = -ENOENT;
  841. goto out;
  842. }
  843. /* Validate the packet */
  844. spin_unlock_irqrestore(&ndp->lock, flags);
  845. payload = nrh->payload;
  846. if (payload < 0)
  847. payload = ntohs(hdr->length);
  848. ret = ncsi_validate_rsp_pkt(nr, payload);
  849. if (ret) {
  850. netdev_warn(ndp->ndev.dev,
  851. "NCSI: 'bad' packet ignored for type 0x%x\n",
  852. hdr->type);
  853. goto out;
  854. }
  855. /* Process the packet */
  856. ret = nrh->handler(nr);
  857. if (ret)
  858. netdev_err(ndp->ndev.dev,
  859. "NCSI: Handler for packet type 0x%x returned %d\n",
  860. hdr->type, ret);
  861. out:
  862. ncsi_free_request(nr);
  863. return ret;
  864. }