ncsi.c 22 KB

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