ncsi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NC-SI protocol configuration
  4. *
  5. * Copyright (C) 2019, IBM Corporation.
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <phy.h>
  10. #include <net/ncsi.h>
  11. #include <net/ncsi-pkt.h>
  12. #include <asm/unaligned.h>
  13. #define NCSI_PACKAGE_MAX 8
  14. #define NCSI_CHANNEL_MAX 31
  15. #define NCSI_PACKAGE_SHIFT 5
  16. #define NCSI_PACKAGE_INDEX(c) (((c) >> NCSI_PACKAGE_SHIFT) & 0x7)
  17. #define NCSI_RESERVED_CHANNEL 0x1f
  18. #define NCSI_CHANNEL_INDEX(c) ((c) & ((1 << NCSI_PACKAGE_SHIFT) - 1))
  19. #define NCSI_TO_CHANNEL(p, c) (((p) << NCSI_PACKAGE_SHIFT) | (c))
  20. #define NCSI_PKT_REVISION 0x01
  21. #define NCSI_CAP_GENERIC_MASK 0x7f
  22. #define NCSI_CAP_BC_MASK 0x0f
  23. #define NCSI_CAP_MC_MASK 0x3f
  24. #define NCSI_CAP_AEN_MASK 0x07
  25. #define NCSI_CAP_VLAN_MASK 0x07
  26. static void ncsi_send_ebf(unsigned int np, unsigned int nc);
  27. static void ncsi_send_ae(unsigned int np, unsigned int nc);
  28. static void ncsi_send_gls(unsigned int np, unsigned int nc);
  29. static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
  30. uchar *payload, int len, bool wait);
  31. struct ncsi_channel {
  32. unsigned int id;
  33. bool has_link;
  34. /* capabilities */
  35. u32 cap_generic;
  36. u32 cap_bc;
  37. u32 cap_mc;
  38. u32 cap_buffer;
  39. u32 cap_aen;
  40. u32 cap_vlan;
  41. /* version information */
  42. struct {
  43. u32 version; /* Supported BCD encoded NCSI version */
  44. u32 alpha2; /* Supported BCD encoded NCSI version */
  45. u8 fw_name[12]; /* Firmware name string */
  46. u32 fw_version; /* Firmware version */
  47. u16 pci_ids[4]; /* PCI identification */
  48. u32 mf_id; /* Manufacture ID */
  49. } version;
  50. };
  51. struct ncsi_package {
  52. unsigned int id;
  53. unsigned int n_channels;
  54. struct ncsi_channel *channels;
  55. };
  56. struct ncsi {
  57. enum {
  58. NCSI_PROBE_PACKAGE_SP,
  59. NCSI_PROBE_PACKAGE_DP,
  60. NCSI_PROBE_CHANNEL_SP,
  61. NCSI_PROBE_CHANNEL,
  62. NCSI_CONFIG,
  63. } state;
  64. unsigned int pending_requests;
  65. unsigned int requests[256];
  66. unsigned int last_request;
  67. unsigned int current_package;
  68. unsigned int current_channel;
  69. unsigned int n_packages;
  70. struct ncsi_package *packages;
  71. };
  72. struct ncsi *ncsi_priv;
  73. bool ncsi_active(void)
  74. {
  75. unsigned int np, nc;
  76. if (!ncsi_priv)
  77. return false;
  78. np = ncsi_priv->current_package;
  79. nc = ncsi_priv->current_channel;
  80. if (ncsi_priv->state != NCSI_CONFIG)
  81. return false;
  82. return np < NCSI_PACKAGE_MAX && nc < NCSI_CHANNEL_MAX &&
  83. ncsi_priv->packages[np].channels[nc].has_link;
  84. }
  85. static unsigned int cmd_payload(int cmd)
  86. {
  87. switch (cmd) {
  88. case NCSI_PKT_CMD_CIS:
  89. return 0;
  90. case NCSI_PKT_CMD_SP:
  91. return 4;
  92. case NCSI_PKT_CMD_DP:
  93. return 0;
  94. case NCSI_PKT_CMD_EC:
  95. return 0;
  96. case NCSI_PKT_CMD_DC:
  97. return 4;
  98. case NCSI_PKT_CMD_RC:
  99. return 4;
  100. case NCSI_PKT_CMD_ECNT:
  101. return 0;
  102. case NCSI_PKT_CMD_DCNT:
  103. return 0;
  104. case NCSI_PKT_CMD_AE:
  105. return 8;
  106. case NCSI_PKT_CMD_SL:
  107. return 8;
  108. case NCSI_PKT_CMD_GLS:
  109. return 0;
  110. case NCSI_PKT_CMD_SVF:
  111. return 8;
  112. case NCSI_PKT_CMD_EV:
  113. return 4;
  114. case NCSI_PKT_CMD_DV:
  115. return 0;
  116. case NCSI_PKT_CMD_SMA:
  117. return 8;
  118. case NCSI_PKT_CMD_EBF:
  119. return 4;
  120. case NCSI_PKT_CMD_DBF:
  121. return 0;
  122. case NCSI_PKT_CMD_EGMF:
  123. return 4;
  124. case NCSI_PKT_CMD_DGMF:
  125. return 0;
  126. case NCSI_PKT_CMD_SNFC:
  127. return 4;
  128. case NCSI_PKT_CMD_GVI:
  129. return 0;
  130. case NCSI_PKT_CMD_GC:
  131. return 0;
  132. case NCSI_PKT_CMD_GP:
  133. return 0;
  134. case NCSI_PKT_CMD_GCPS:
  135. return 0;
  136. case NCSI_PKT_CMD_GNS:
  137. return 0;
  138. case NCSI_PKT_CMD_GNPTS:
  139. return 0;
  140. case NCSI_PKT_CMD_GPS:
  141. return 0;
  142. default:
  143. printf("NCSI: Unknown command 0x%02x\n", cmd);
  144. return 0;
  145. }
  146. }
  147. static u32 ncsi_calculate_checksum(unsigned char *data, int len)
  148. {
  149. u32 checksum = 0;
  150. int i;
  151. for (i = 0; i < len; i += 2)
  152. checksum += (((u32)data[i] << 8) | data[i + 1]);
  153. checksum = (~checksum + 1);
  154. return checksum;
  155. }
  156. static int ncsi_validate_rsp(struct ncsi_rsp_pkt *pkt, int payload)
  157. {
  158. struct ncsi_rsp_pkt_hdr *hdr = &pkt->rsp;
  159. u32 checksum, c_offset;
  160. __be32 pchecksum;
  161. if (hdr->common.revision != 1) {
  162. printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
  163. hdr->common.type, hdr->common.revision);
  164. return -1;
  165. }
  166. if (hdr->code != 0) {
  167. printf("NCSI: 0x%02x response returns error %d\n",
  168. hdr->common.type, __be16_to_cpu(hdr->code));
  169. if (ntohs(hdr->reason) == 0x05)
  170. printf("(Invalid command length)\n");
  171. return -1;
  172. }
  173. if (ntohs(hdr->common.length) != payload) {
  174. printf("NCSI: 0x%02x response has incorrect length %d\n",
  175. hdr->common.type, hdr->common.length);
  176. return -1;
  177. }
  178. c_offset = sizeof(struct ncsi_rsp_pkt_hdr) + payload - sizeof(checksum);
  179. pchecksum = get_unaligned_be32((void *)hdr + c_offset);
  180. if (pchecksum != 0) {
  181. checksum = ncsi_calculate_checksum((unsigned char *)hdr,
  182. c_offset);
  183. if (pchecksum != checksum) {
  184. printf("NCSI: 0x%02x response has invalid checksum\n",
  185. hdr->common.type);
  186. return -1;
  187. }
  188. }
  189. return 0;
  190. }
  191. static void ncsi_rsp_ec(struct ncsi_rsp_pkt *pkt)
  192. {
  193. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
  194. unsigned int np, nc;
  195. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  196. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  197. if (ncsi_priv->packages[np].channels[nc].cap_aen != 0)
  198. ncsi_send_ae(np, nc);
  199. /* else, done */
  200. }
  201. static void ncsi_rsp_ecnt(struct ncsi_rsp_pkt *pkt)
  202. {
  203. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
  204. unsigned int np, nc;
  205. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  206. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  207. ncsi_send_command(np, nc, NCSI_PKT_CMD_EC, NULL, 0, true);
  208. }
  209. static void ncsi_rsp_ebf(struct ncsi_rsp_pkt *pkt)
  210. {
  211. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
  212. unsigned int np, nc;
  213. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  214. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  215. ncsi_send_command(np, nc, NCSI_PKT_CMD_ECNT, NULL, 0, true);
  216. }
  217. static void ncsi_rsp_sma(struct ncsi_rsp_pkt *pkt)
  218. {
  219. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
  220. unsigned int np, nc;
  221. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  222. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  223. ncsi_send_ebf(np, nc);
  224. }
  225. static void ncsi_rsp_gc(struct ncsi_rsp_pkt *pkt)
  226. {
  227. struct ncsi_rsp_gc_pkt *gc = (struct ncsi_rsp_gc_pkt *)pkt;
  228. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gc->rsp;
  229. struct ncsi_channel *c;
  230. unsigned int np, nc;
  231. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  232. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  233. if (np >= ncsi_priv->n_packages ||
  234. nc >= ncsi_priv->packages[np].n_channels) {
  235. printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
  236. np, nc);
  237. return;
  238. }
  239. c = &ncsi_priv->packages[np].channels[nc];
  240. c->cap_generic = ntohl(gc->cap) & NCSI_CAP_GENERIC_MASK;
  241. c->cap_bc = ntohl(gc->bc_cap) & NCSI_CAP_BC_MASK;
  242. c->cap_mc = ntohl(gc->mc_cap) & NCSI_CAP_MC_MASK;
  243. c->cap_aen = ntohl(gc->aen_cap) & NCSI_CAP_AEN_MASK;
  244. c->cap_vlan = ntohl(gc->vlan_mode) & NCSI_CAP_VLAN_MASK;
  245. /* End of probe for this channel */
  246. }
  247. static void ncsi_rsp_gvi(struct ncsi_rsp_pkt *pkt)
  248. {
  249. struct ncsi_rsp_gvi_pkt *gvi = (struct ncsi_rsp_gvi_pkt *)pkt;
  250. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gvi->rsp;
  251. struct ncsi_channel *c;
  252. unsigned int np, nc, i;
  253. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  254. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  255. if (np >= ncsi_priv->n_packages ||
  256. nc >= ncsi_priv->packages[np].n_channels) {
  257. printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
  258. np, nc);
  259. return;
  260. }
  261. c = &ncsi_priv->packages[np].channels[nc];
  262. c->version.version = get_unaligned_be32(&gvi->ncsi_version);
  263. c->version.alpha2 = gvi->alpha2;
  264. memcpy(c->version.fw_name, gvi->fw_name, sizeof(c->version.fw_name));
  265. c->version.fw_version = get_unaligned_be32(&gvi->fw_version);
  266. for (i = 0; i < ARRAY_SIZE(c->version.pci_ids); i++)
  267. c->version.pci_ids[i] = get_unaligned_be16(gvi->pci_ids + i);
  268. c->version.mf_id = get_unaligned_be32(&gvi->mf_id);
  269. if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
  270. ncsi_send_command(np, nc, NCSI_PKT_CMD_GC, NULL, 0, true);
  271. }
  272. static void ncsi_rsp_gls(struct ncsi_rsp_pkt *pkt)
  273. {
  274. struct ncsi_rsp_gls_pkt *gls = (struct ncsi_rsp_gls_pkt *)pkt;
  275. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gls->rsp;
  276. unsigned int np, nc;
  277. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  278. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  279. if (np >= ncsi_priv->n_packages ||
  280. nc >= ncsi_priv->packages[np].n_channels) {
  281. printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
  282. np, nc);
  283. return;
  284. }
  285. ncsi_priv->packages[np].channels[nc].has_link =
  286. !!(get_unaligned_be32(&gls->status));
  287. if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
  288. ncsi_send_command(np, nc, NCSI_PKT_CMD_GVI, NULL, 0, true);
  289. }
  290. static void ncsi_rsp_cis(struct ncsi_rsp_pkt *pkt)
  291. {
  292. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
  293. struct ncsi_package *package;
  294. unsigned int np, nc;
  295. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  296. nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
  297. if (np >= ncsi_priv->n_packages) {
  298. printf("NCSI: Mystery package 0x%02x from CIS\n", np);
  299. return;
  300. }
  301. package = &ncsi_priv->packages[np];
  302. if (nc < package->n_channels) {
  303. /*
  304. * This is fine in general but in the current design we
  305. * don't send CIS commands to known channels.
  306. */
  307. debug("NCSI: Duplicate channel 0x%02x\n", nc);
  308. return;
  309. }
  310. package->channels = realloc(package->channels,
  311. sizeof(struct ncsi_channel) *
  312. (package->n_channels + 1));
  313. if (!package->channels) {
  314. printf("NCSI: Could not allocate memory for new channel\n");
  315. return;
  316. }
  317. debug("NCSI: New channel 0x%02x\n", nc);
  318. package->channels[nc].id = nc;
  319. package->channels[nc].has_link = false;
  320. package->n_channels++;
  321. ncsi_send_gls(np, nc);
  322. }
  323. static void ncsi_rsp_dp(struct ncsi_rsp_pkt *pkt)
  324. {
  325. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
  326. unsigned int np;
  327. /* No action needed */
  328. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  329. if (np >= ncsi_priv->n_packages)
  330. debug("NCSI: DP response from unknown package %d\n", np);
  331. }
  332. static void ncsi_rsp_sp(struct ncsi_rsp_pkt *pkt)
  333. {
  334. struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
  335. unsigned int np;
  336. np = NCSI_PACKAGE_INDEX(rsp->common.channel);
  337. if (np < ncsi_priv->n_packages) {
  338. /* Already know about this package */
  339. debug("NCSI: package 0x%02x selected\n", np);
  340. return;
  341. }
  342. debug("NCSI: adding new package %d\n", np);
  343. ncsi_priv->packages = realloc(ncsi_priv->packages,
  344. sizeof(struct ncsi_package) *
  345. (ncsi_priv->n_packages + 1));
  346. if (!ncsi_priv->packages) {
  347. printf("NCSI: could not allocate memory for new package\n");
  348. return;
  349. }
  350. ncsi_priv->packages[np].id = np;
  351. ncsi_priv->packages[np].n_channels = 0;
  352. ncsi_priv->packages[np].channels = NULL;
  353. ncsi_priv->n_packages++;
  354. }
  355. static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
  356. {
  357. bool timeout = !nh;
  358. int np, nc;
  359. switch (ncsi_priv->state) {
  360. case NCSI_PROBE_PACKAGE_SP:
  361. if (!timeout &&
  362. ncsi_priv->current_package + 1 < NCSI_PACKAGE_MAX) {
  363. ncsi_priv->current_package++;
  364. } else {
  365. ncsi_priv->state = NCSI_PROBE_PACKAGE_DP;
  366. ncsi_priv->current_package = 0;
  367. }
  368. return ncsi_probe_packages();
  369. case NCSI_PROBE_PACKAGE_DP:
  370. if (ncsi_priv->current_package + 1 < ncsi_priv->n_packages &&
  371. !timeout) {
  372. ncsi_priv->current_package++;
  373. } else {
  374. if (!ncsi_priv->n_packages) {
  375. printf("NCSI: no packages found\n");
  376. net_set_state(NETLOOP_FAIL);
  377. return;
  378. }
  379. printf("NCSI: probing channels\n");
  380. ncsi_priv->state = NCSI_PROBE_CHANNEL_SP;
  381. ncsi_priv->current_package = 0;
  382. ncsi_priv->current_channel = 0;
  383. }
  384. return ncsi_probe_packages();
  385. case NCSI_PROBE_CHANNEL_SP:
  386. if (!timeout && nh->common.type == NCSI_PKT_RSP_SP) {
  387. ncsi_priv->state = NCSI_PROBE_CHANNEL;
  388. return ncsi_probe_packages();
  389. }
  390. printf("NCSI: failed to select package 0x%0x2 or timeout\n",
  391. ncsi_priv->current_package);
  392. net_set_state(NETLOOP_FAIL);
  393. break;
  394. case NCSI_PROBE_CHANNEL:
  395. // TODO only does package 0 for now
  396. if (ncsi_priv->pending_requests == 0) {
  397. np = ncsi_priv->current_package;
  398. nc = ncsi_priv->current_channel;
  399. /* Configure first channel that has link */
  400. if (ncsi_priv->packages[np].channels[nc].has_link) {
  401. ncsi_priv->state = NCSI_CONFIG;
  402. } else if (ncsi_priv->current_channel + 1 <
  403. NCSI_CHANNEL_MAX) {
  404. ncsi_priv->current_channel++;
  405. } else {
  406. // XXX As above only package 0
  407. printf("NCSI: no channel found with link\n");
  408. net_set_state(NETLOOP_FAIL);
  409. return;
  410. }
  411. return ncsi_probe_packages();
  412. }
  413. break;
  414. case NCSI_CONFIG:
  415. if (ncsi_priv->pending_requests == 0) {
  416. printf("NCSI: configuration done!\n");
  417. net_set_state(NETLOOP_SUCCESS);
  418. } else if (timeout) {
  419. printf("NCSI: timeout during configure\n");
  420. net_set_state(NETLOOP_FAIL);
  421. }
  422. break;
  423. default:
  424. printf("NCSI: something went very wrong, nevermind\n");
  425. net_set_state(NETLOOP_FAIL);
  426. break;
  427. }
  428. }
  429. static void ncsi_timeout_handler(void)
  430. {
  431. if (ncsi_priv->pending_requests)
  432. ncsi_priv->pending_requests--;
  433. ncsi_update_state(NULL);
  434. }
  435. static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
  436. uchar *payload, int len, bool wait)
  437. {
  438. struct ncsi_pkt_hdr *hdr;
  439. __be32 *pchecksum;
  440. int eth_hdr_size;
  441. u32 checksum;
  442. uchar *pkt, *start;
  443. int final_len;
  444. pkt = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
  445. if (!pkt)
  446. return -ENOMEM;
  447. start = pkt;
  448. eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_NCSI);
  449. pkt += eth_hdr_size;
  450. /* Set NCSI command header fields */
  451. hdr = (struct ncsi_pkt_hdr *)pkt;
  452. hdr->mc_id = 0;
  453. hdr->revision = NCSI_PKT_REVISION;
  454. hdr->id = ++ncsi_priv->last_request;
  455. ncsi_priv->requests[ncsi_priv->last_request] = 1;
  456. hdr->type = cmd;
  457. hdr->channel = NCSI_TO_CHANNEL(np, nc);
  458. hdr->length = htons(len);
  459. if (payload && len)
  460. memcpy(pkt + sizeof(struct ncsi_pkt_hdr), payload, len);
  461. /* Calculate checksum */
  462. checksum = ncsi_calculate_checksum((unsigned char *)hdr,
  463. sizeof(*hdr) + len);
  464. pchecksum = (__be32 *)((void *)(hdr + 1) + len);
  465. put_unaligned_be32(htonl(checksum), pchecksum);
  466. if (wait) {
  467. net_set_timeout_handler(1000UL, ncsi_timeout_handler);
  468. ncsi_priv->pending_requests++;
  469. }
  470. if (len < 26)
  471. len = 26;
  472. /* frame header, packet header, payload, checksum */
  473. final_len = eth_hdr_size + sizeof(struct ncsi_cmd_pkt_hdr) + len + 4;
  474. net_send_packet(start, final_len);
  475. free(start);
  476. return 0;
  477. }
  478. static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
  479. {
  480. struct ncsi_aen_pkt_hdr *hdr = (struct ncsi_aen_pkt_hdr *)ip;
  481. int payload, i;
  482. __be32 pchecksum;
  483. u32 checksum;
  484. switch (hdr->type) {
  485. case NCSI_PKT_AEN_LSC:
  486. printf("NCSI: link state changed\n");
  487. payload = 12;
  488. break;
  489. case NCSI_PKT_AEN_CR:
  490. printf("NCSI: re-configuration required\n");
  491. payload = 4;
  492. break;
  493. case NCSI_PKT_AEN_HNCDSC:
  494. /* Host notifcation - N/A but weird */
  495. debug("NCSI: HNCDSC AEN received\n");
  496. return;
  497. default:
  498. printf("%s: Invalid type 0x%02x\n", __func__, hdr->type);
  499. return;
  500. }
  501. /* Validate packet */
  502. if (hdr->common.revision != 1) {
  503. printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
  504. hdr->common.type, hdr->common.revision);
  505. return;
  506. }
  507. if (ntohs(hdr->common.length) != payload) {
  508. printf("NCSI: 0x%02x response has incorrect length %d\n",
  509. hdr->common.type, hdr->common.length);
  510. return;
  511. }
  512. pchecksum = get_unaligned_be32((void *)(hdr + 1) + payload - 4);
  513. if (pchecksum != 0) {
  514. checksum = ncsi_calculate_checksum((unsigned char *)hdr,
  515. sizeof(*hdr) + payload - 4);
  516. if (pchecksum != checksum) {
  517. printf("NCSI: 0x%02x response has invalid checksum\n",
  518. hdr->common.type);
  519. return;
  520. }
  521. }
  522. /* Link or configuration lost - just redo the discovery process */
  523. ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
  524. for (i = 0; i < ncsi_priv->n_packages; i++)
  525. free(ncsi_priv->packages[i].channels);
  526. free(ncsi_priv->packages);
  527. ncsi_priv->n_packages = 0;
  528. ncsi_priv->current_package = NCSI_PACKAGE_MAX;
  529. ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
  530. ncsi_probe_packages();
  531. }
  532. void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
  533. unsigned int len)
  534. {
  535. struct ncsi_rsp_pkt *pkt = (struct ncsi_rsp_pkt *)ip;
  536. struct ncsi_rsp_pkt_hdr *nh = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
  537. void (*handler)(struct ncsi_rsp_pkt *pkt) = NULL;
  538. unsigned short payload;
  539. if (ncsi_priv->pending_requests)
  540. ncsi_priv->pending_requests--;
  541. if (len < sizeof(struct ncsi_rsp_pkt_hdr)) {
  542. printf("NCSI: undersized packet: %u bytes\n", len);
  543. goto out;
  544. }
  545. if (nh->common.type == NCSI_PKT_AEN)
  546. return ncsi_handle_aen(ip, len);
  547. switch (nh->common.type) {
  548. case NCSI_PKT_RSP_SP:
  549. payload = 4;
  550. handler = ncsi_rsp_sp;
  551. break;
  552. case NCSI_PKT_RSP_DP:
  553. payload = 4;
  554. handler = ncsi_rsp_dp;
  555. break;
  556. case NCSI_PKT_RSP_CIS:
  557. payload = 4;
  558. handler = ncsi_rsp_cis;
  559. break;
  560. case NCSI_PKT_RSP_GLS:
  561. payload = 16;
  562. handler = ncsi_rsp_gls;
  563. break;
  564. case NCSI_PKT_RSP_GVI:
  565. payload = 40;
  566. handler = ncsi_rsp_gvi;
  567. break;
  568. case NCSI_PKT_RSP_GC:
  569. payload = 32;
  570. handler = ncsi_rsp_gc;
  571. break;
  572. case NCSI_PKT_RSP_SMA:
  573. payload = 4;
  574. handler = ncsi_rsp_sma;
  575. break;
  576. case NCSI_PKT_RSP_EBF:
  577. payload = 4;
  578. handler = ncsi_rsp_ebf;
  579. break;
  580. case NCSI_PKT_RSP_ECNT:
  581. payload = 4;
  582. handler = ncsi_rsp_ecnt;
  583. break;
  584. case NCSI_PKT_RSP_EC:
  585. payload = 4;
  586. handler = ncsi_rsp_ec;
  587. break;
  588. case NCSI_PKT_RSP_AE:
  589. payload = 4;
  590. handler = NULL;
  591. break;
  592. default:
  593. printf("NCSI: unsupported packet type 0x%02x\n",
  594. nh->common.type);
  595. goto out;
  596. }
  597. if (ncsi_validate_rsp(pkt, payload) != 0) {
  598. printf("NCSI: discarding invalid packet of type 0x%02x\n",
  599. nh->common.type);
  600. goto out;
  601. }
  602. if (handler)
  603. handler(pkt);
  604. out:
  605. ncsi_update_state(nh);
  606. }
  607. static void ncsi_send_sp(unsigned int np)
  608. {
  609. uchar payload[4] = {0};
  610. ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_SP,
  611. (unsigned char *)&payload,
  612. cmd_payload(NCSI_PKT_CMD_SP), true);
  613. }
  614. static void ncsi_send_dp(unsigned int np)
  615. {
  616. ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_DP, NULL, 0,
  617. true);
  618. }
  619. static void ncsi_send_gls(unsigned int np, unsigned int nc)
  620. {
  621. ncsi_send_command(np, nc, NCSI_PKT_CMD_GLS, NULL, 0, true);
  622. }
  623. static void ncsi_send_cis(unsigned int np, unsigned int nc)
  624. {
  625. ncsi_send_command(np, nc, NCSI_PKT_CMD_CIS, NULL, 0, true);
  626. }
  627. static void ncsi_send_ae(unsigned int np, unsigned int nc)
  628. {
  629. struct ncsi_cmd_ae_pkt cmd;
  630. memset(&cmd, 0, sizeof(cmd));
  631. cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_aen);
  632. ncsi_send_command(np, nc, NCSI_PKT_CMD_AE,
  633. ((unsigned char *)&cmd)
  634. + sizeof(struct ncsi_cmd_pkt_hdr),
  635. cmd_payload(NCSI_PKT_CMD_AE), true);
  636. }
  637. static void ncsi_send_ebf(unsigned int np, unsigned int nc)
  638. {
  639. struct ncsi_cmd_ebf_pkt cmd;
  640. memset(&cmd, 0, sizeof(cmd));
  641. cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_bc);
  642. ncsi_send_command(np, nc, NCSI_PKT_CMD_EBF,
  643. ((unsigned char *)&cmd)
  644. + sizeof(struct ncsi_cmd_pkt_hdr),
  645. cmd_payload(NCSI_PKT_CMD_EBF), true);
  646. }
  647. static void ncsi_send_sma(unsigned int np, unsigned int nc)
  648. {
  649. struct ncsi_cmd_sma_pkt cmd;
  650. unsigned char *addr, i;
  651. addr = eth_get_ethaddr();
  652. if (!addr) {
  653. printf("NCSI: no MAC address configured\n");
  654. return;
  655. }
  656. memset(&cmd, 0, sizeof(cmd));
  657. for (i = 0; i < ARP_HLEN; i++)
  658. cmd.mac[i] = addr[i];
  659. cmd.index = 1;
  660. cmd.at_e = 1;
  661. ncsi_send_command(np, nc, NCSI_PKT_CMD_SMA,
  662. ((unsigned char *)&cmd)
  663. + sizeof(struct ncsi_cmd_pkt_hdr),
  664. cmd_payload(NCSI_PKT_CMD_SMA), true);
  665. }
  666. void ncsi_probe_packages(void)
  667. {
  668. struct ncsi_package *package;
  669. unsigned int np, nc;
  670. switch (ncsi_priv->state) {
  671. case NCSI_PROBE_PACKAGE_SP:
  672. if (ncsi_priv->current_package == NCSI_PACKAGE_MAX)
  673. ncsi_priv->current_package = 0;
  674. ncsi_send_sp(ncsi_priv->current_package);
  675. break;
  676. case NCSI_PROBE_PACKAGE_DP:
  677. ncsi_send_dp(ncsi_priv->current_package);
  678. break;
  679. case NCSI_PROBE_CHANNEL_SP:
  680. if (ncsi_priv->n_packages > 0)
  681. ncsi_send_sp(ncsi_priv->current_package);
  682. else
  683. printf("NCSI: no packages discovered, configuration not possible\n");
  684. break;
  685. case NCSI_PROBE_CHANNEL:
  686. /* Kicks off chain of channel discovery */
  687. ncsi_send_cis(ncsi_priv->current_package,
  688. ncsi_priv->current_channel);
  689. break;
  690. case NCSI_CONFIG:
  691. for (np = 0; np < ncsi_priv->n_packages; np++) {
  692. package = &ncsi_priv->packages[np];
  693. for (nc = 0; nc < package->n_channels; nc++)
  694. if (package->channels[nc].has_link)
  695. break;
  696. if (nc < package->n_channels)
  697. break;
  698. }
  699. if (np == ncsi_priv->n_packages) {
  700. printf("NCSI: no link available\n");
  701. return;
  702. }
  703. printf("NCSI: configuring channel %d\n", nc);
  704. ncsi_priv->current_package = np;
  705. ncsi_priv->current_channel = nc;
  706. /* Kicks off rest of configure chain */
  707. ncsi_send_sma(np, nc);
  708. break;
  709. default:
  710. printf("NCSI: unknown state 0x%x\n", ncsi_priv->state);
  711. }
  712. }
  713. int ncsi_probe(struct phy_device *phydev)
  714. {
  715. if (!phydev->priv) {
  716. phydev->priv = malloc(sizeof(struct ncsi));
  717. if (!phydev->priv)
  718. return -ENOMEM;
  719. memset(phydev->priv, 0, sizeof(struct ncsi));
  720. }
  721. ncsi_priv = phydev->priv;
  722. return 0;
  723. }
  724. int ncsi_startup(struct phy_device *phydev)
  725. {
  726. /* Set phydev parameters */
  727. phydev->speed = SPEED_100;
  728. phydev->duplex = DUPLEX_FULL;
  729. /* Normal phy reset is N/A */
  730. phydev->flags |= PHY_FLAG_BROKEN_RESET;
  731. /* Set initial probe state */
  732. ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
  733. /* No active package/channel yet */
  734. ncsi_priv->current_package = NCSI_PACKAGE_MAX;
  735. ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
  736. /* Pretend link works so the MAC driver sets final bits up */
  737. phydev->link = true;
  738. /* Set ncsi_priv so we can use it when called from net_loop() */
  739. ncsi_priv = phydev->priv;
  740. return 0;
  741. }
  742. int ncsi_shutdown(struct phy_device *phydev)
  743. {
  744. printf("NCSI: Disabling package %d\n", ncsi_priv->current_package);
  745. ncsi_send_dp(ncsi_priv->current_package);
  746. return 0;
  747. }
  748. static struct phy_driver ncsi_driver = {
  749. .uid = PHY_NCSI_ID,
  750. .mask = 0xffffffff,
  751. .name = "NC-SI",
  752. .features = PHY_100BT_FEATURES | PHY_DEFAULT_FEATURES |
  753. SUPPORTED_100baseT_Full | SUPPORTED_MII,
  754. .probe = ncsi_probe,
  755. .startup = ncsi_startup,
  756. .shutdown = ncsi_shutdown,
  757. };
  758. int phy_ncsi_init(void)
  759. {
  760. phy_register(&ncsi_driver);
  761. return 0;
  762. }