1 // SPDX-License-Identifier: GPL-2.0+
3 * NC-SI protocol configuration
5 * Copyright (C) 2019, IBM Corporation.
13 #include <net/ncsi-pkt.h>
14 #include <asm/unaligned.h>
16 #define NCSI_PACKAGE_MAX 8
17 #define NCSI_CHANNEL_MAX 31
19 #define NCSI_PACKAGE_SHIFT 5
20 #define NCSI_PACKAGE_INDEX(c) (((c) >> NCSI_PACKAGE_SHIFT) & 0x7)
21 #define NCSI_RESERVED_CHANNEL 0x1f
22 #define NCSI_CHANNEL_INDEX(c) ((c) & ((1 << NCSI_PACKAGE_SHIFT) - 1))
23 #define NCSI_TO_CHANNEL(p, c) (((p) << NCSI_PACKAGE_SHIFT) | (c))
25 #define NCSI_PKT_REVISION 0x01
27 #define NCSI_CAP_GENERIC_MASK 0x7f
28 #define NCSI_CAP_BC_MASK 0x0f
29 #define NCSI_CAP_MC_MASK 0x3f
30 #define NCSI_CAP_AEN_MASK 0x07
31 #define NCSI_CAP_VLAN_MASK 0x07
33 static void ncsi_send_ebf(unsigned int np, unsigned int nc);
34 static void ncsi_send_ae(unsigned int np, unsigned int nc);
35 static void ncsi_send_gls(unsigned int np, unsigned int nc);
36 static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
37 uchar *payload, int len, bool wait);
51 /* version information */
53 u32 version; /* Supported BCD encoded NCSI version */
54 u32 alpha2; /* Supported BCD encoded NCSI version */
55 u8 fw_name[12]; /* Firmware name string */
56 u32 fw_version; /* Firmware version */
57 u16 pci_ids[4]; /* PCI identification */
58 u32 mf_id; /* Manufacture ID */
65 unsigned int n_channels;
66 struct ncsi_channel *channels;
71 NCSI_PROBE_PACKAGE_SP,
72 NCSI_PROBE_PACKAGE_DP,
73 NCSI_PROBE_CHANNEL_SP,
78 unsigned int pending_requests;
79 unsigned int requests[256];
80 unsigned int last_request;
82 unsigned int current_package;
83 unsigned int current_channel;
85 unsigned int n_packages;
86 struct ncsi_package *packages;
89 struct ncsi *ncsi_priv;
91 bool ncsi_active(void)
98 np = ncsi_priv->current_package;
99 nc = ncsi_priv->current_channel;
101 if (ncsi_priv->state != NCSI_CONFIG)
104 return np < NCSI_PACKAGE_MAX && nc < NCSI_CHANNEL_MAX &&
105 ncsi_priv->packages[np].channels[nc].has_link;
108 static unsigned int cmd_payload(int cmd)
111 case NCSI_PKT_CMD_CIS:
113 case NCSI_PKT_CMD_SP:
115 case NCSI_PKT_CMD_DP:
117 case NCSI_PKT_CMD_EC:
119 case NCSI_PKT_CMD_DC:
121 case NCSI_PKT_CMD_RC:
123 case NCSI_PKT_CMD_ECNT:
125 case NCSI_PKT_CMD_DCNT:
127 case NCSI_PKT_CMD_AE:
129 case NCSI_PKT_CMD_SL:
131 case NCSI_PKT_CMD_GLS:
133 case NCSI_PKT_CMD_SVF:
135 case NCSI_PKT_CMD_EV:
137 case NCSI_PKT_CMD_DV:
139 case NCSI_PKT_CMD_SMA:
141 case NCSI_PKT_CMD_EBF:
143 case NCSI_PKT_CMD_DBF:
145 case NCSI_PKT_CMD_EGMF:
147 case NCSI_PKT_CMD_DGMF:
149 case NCSI_PKT_CMD_SNFC:
151 case NCSI_PKT_CMD_GVI:
153 case NCSI_PKT_CMD_GC:
155 case NCSI_PKT_CMD_GP:
157 case NCSI_PKT_CMD_GCPS:
159 case NCSI_PKT_CMD_GNS:
161 case NCSI_PKT_CMD_GNPTS:
163 case NCSI_PKT_CMD_GPS:
166 printf("NCSI: Unknown command 0x%02x\n", cmd);
171 static u32 ncsi_calculate_checksum(unsigned char *data, int len)
176 for (i = 0; i < len; i += 2)
177 checksum += (((u32)data[i] << 8) | data[i + 1]);
179 checksum = (~checksum + 1);
183 static int ncsi_validate_rsp(struct ncsi_rsp_pkt *pkt, int payload)
185 struct ncsi_rsp_pkt_hdr *hdr = &pkt->rsp;
186 u32 checksum, c_offset;
189 if (hdr->common.revision != 1) {
190 printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
191 hdr->common.type, hdr->common.revision);
195 if (hdr->code != 0) {
196 printf("NCSI: 0x%02x response returns error %d\n",
197 hdr->common.type, __be16_to_cpu(hdr->code));
198 if (ntohs(hdr->reason) == 0x05)
199 printf("(Invalid command length)\n");
203 if (ntohs(hdr->common.length) != payload) {
204 printf("NCSI: 0x%02x response has incorrect length %d\n",
205 hdr->common.type, hdr->common.length);
209 c_offset = sizeof(struct ncsi_rsp_pkt_hdr) + payload - sizeof(checksum);
210 pchecksum = get_unaligned_be32((void *)hdr + c_offset);
211 if (pchecksum != 0) {
212 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
214 if (pchecksum != checksum) {
215 printf("NCSI: 0x%02x response has invalid checksum\n",
224 static void ncsi_rsp_ec(struct ncsi_rsp_pkt *pkt)
226 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
229 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
230 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
232 if (ncsi_priv->packages[np].channels[nc].cap_aen != 0)
233 ncsi_send_ae(np, nc);
237 static void ncsi_rsp_ecnt(struct ncsi_rsp_pkt *pkt)
239 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
242 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
243 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
245 ncsi_send_command(np, nc, NCSI_PKT_CMD_EC, NULL, 0, true);
248 static void ncsi_rsp_ebf(struct ncsi_rsp_pkt *pkt)
250 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
253 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
254 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
256 ncsi_send_command(np, nc, NCSI_PKT_CMD_ECNT, NULL, 0, true);
259 static void ncsi_rsp_sma(struct ncsi_rsp_pkt *pkt)
261 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
264 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
265 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
267 ncsi_send_ebf(np, nc);
270 static void ncsi_rsp_gc(struct ncsi_rsp_pkt *pkt)
272 struct ncsi_rsp_gc_pkt *gc = (struct ncsi_rsp_gc_pkt *)pkt;
273 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gc->rsp;
274 struct ncsi_channel *c;
277 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
278 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",
287 c = &ncsi_priv->packages[np].channels[nc];
288 c->cap_generic = ntohl(gc->cap) & NCSI_CAP_GENERIC_MASK;
289 c->cap_bc = ntohl(gc->bc_cap) & NCSI_CAP_BC_MASK;
290 c->cap_mc = ntohl(gc->mc_cap) & NCSI_CAP_MC_MASK;
291 c->cap_aen = ntohl(gc->aen_cap) & NCSI_CAP_AEN_MASK;
292 c->cap_vlan = ntohl(gc->vlan_mode) & NCSI_CAP_VLAN_MASK;
294 /* End of probe for this channel */
297 static void ncsi_rsp_gvi(struct ncsi_rsp_pkt *pkt)
299 struct ncsi_rsp_gvi_pkt *gvi = (struct ncsi_rsp_gvi_pkt *)pkt;
300 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gvi->rsp;
301 struct ncsi_channel *c;
302 unsigned int np, nc, i;
304 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
305 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
307 if (np >= ncsi_priv->n_packages ||
308 nc >= ncsi_priv->packages[np].n_channels) {
309 printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
314 c = &ncsi_priv->packages[np].channels[nc];
315 c->version.version = get_unaligned_be32(&gvi->ncsi_version);
316 c->version.alpha2 = gvi->alpha2;
317 memcpy(c->version.fw_name, gvi->fw_name, sizeof(c->version.fw_name));
318 c->version.fw_version = get_unaligned_be32(&gvi->fw_version);
319 for (i = 0; i < ARRAY_SIZE(c->version.pci_ids); i++)
320 c->version.pci_ids[i] = get_unaligned_be16(gvi->pci_ids + i);
321 c->version.mf_id = get_unaligned_be32(&gvi->mf_id);
323 if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
324 ncsi_send_command(np, nc, NCSI_PKT_CMD_GC, NULL, 0, true);
327 static void ncsi_rsp_gls(struct ncsi_rsp_pkt *pkt)
329 struct ncsi_rsp_gls_pkt *gls = (struct ncsi_rsp_gls_pkt *)pkt;
330 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gls->rsp;
333 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
334 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
336 if (np >= ncsi_priv->n_packages ||
337 nc >= ncsi_priv->packages[np].n_channels) {
338 printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
343 ncsi_priv->packages[np].channels[nc].has_link =
344 !!(get_unaligned_be32(&gls->status));
346 if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
347 ncsi_send_command(np, nc, NCSI_PKT_CMD_GVI, NULL, 0, true);
350 static void ncsi_rsp_cis(struct ncsi_rsp_pkt *pkt)
352 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
353 struct ncsi_package *package;
356 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
357 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
359 if (np >= ncsi_priv->n_packages) {
360 printf("NCSI: Mystery package 0x%02x from CIS\n", np);
364 package = &ncsi_priv->packages[np];
366 if (nc < package->n_channels) {
368 * This is fine in general but in the current design we
369 * don't send CIS commands to known channels.
371 debug("NCSI: Duplicate channel 0x%02x\n", nc);
375 package->channels = realloc(package->channels,
376 sizeof(struct ncsi_channel) *
377 (package->n_channels + 1));
378 if (!package->channels) {
379 printf("NCSI: Could not allocate memory for new channel\n");
383 debug("NCSI: New channel 0x%02x\n", nc);
385 package->channels[nc].id = nc;
386 package->channels[nc].has_link = false;
387 package->n_channels++;
389 ncsi_send_gls(np, nc);
392 static void ncsi_rsp_dp(struct ncsi_rsp_pkt *pkt)
394 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
397 /* No action needed */
399 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
400 if (np >= ncsi_priv->n_packages)
401 debug("NCSI: DP response from unknown package %d\n", np);
404 static void ncsi_rsp_sp(struct ncsi_rsp_pkt *pkt)
406 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
409 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
411 if (np < ncsi_priv->n_packages) {
412 /* Already know about this package */
413 debug("NCSI: package 0x%02x selected\n", np);
417 debug("NCSI: adding new package %d\n", np);
419 ncsi_priv->packages = realloc(ncsi_priv->packages,
420 sizeof(struct ncsi_package) *
421 (ncsi_priv->n_packages + 1));
422 if (!ncsi_priv->packages) {
423 printf("NCSI: could not allocate memory for new package\n");
427 ncsi_priv->packages[np].id = np;
428 ncsi_priv->packages[np].n_channels = 0;
429 ncsi_priv->packages[np].channels = NULL;
430 ncsi_priv->n_packages++;
433 static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
438 switch (ncsi_priv->state) {
439 case NCSI_PROBE_PACKAGE_SP:
441 ncsi_priv->current_package + 1 < NCSI_PACKAGE_MAX) {
442 ncsi_priv->current_package++;
444 ncsi_priv->state = NCSI_PROBE_PACKAGE_DP;
445 ncsi_priv->current_package = 0;
447 return ncsi_probe_packages();
448 case NCSI_PROBE_PACKAGE_DP:
449 if (ncsi_priv->current_package + 1 < ncsi_priv->n_packages &&
451 ncsi_priv->current_package++;
453 if (!ncsi_priv->n_packages) {
454 printf("NCSI: no packages found\n");
455 net_set_state(NETLOOP_FAIL);
458 printf("NCSI: probing channels\n");
459 ncsi_priv->state = NCSI_PROBE_CHANNEL_SP;
460 ncsi_priv->current_package = 0;
461 ncsi_priv->current_channel = 0;
463 return ncsi_probe_packages();
464 case NCSI_PROBE_CHANNEL_SP:
465 if (!timeout && nh->common.type == NCSI_PKT_RSP_SP) {
466 ncsi_priv->state = NCSI_PROBE_CHANNEL;
467 return ncsi_probe_packages();
469 printf("NCSI: failed to select package 0x%0x2 or timeout\n",
470 ncsi_priv->current_package);
471 net_set_state(NETLOOP_FAIL);
473 case NCSI_PROBE_CHANNEL:
474 // TODO only does package 0 for now
475 if (ncsi_priv->pending_requests == 0) {
476 np = ncsi_priv->current_package;
477 nc = ncsi_priv->current_channel;
479 /* Configure first channel that has link */
480 if (ncsi_priv->packages[np].channels[nc].has_link) {
481 ncsi_priv->state = NCSI_CONFIG;
482 } else if (ncsi_priv->current_channel + 1 <
484 ncsi_priv->current_channel++;
486 // XXX As above only package 0
487 printf("NCSI: no channel found with link\n");
488 net_set_state(NETLOOP_FAIL);
491 return ncsi_probe_packages();
495 if (ncsi_priv->pending_requests == 0) {
496 printf("NCSI: configuration done!\n");
497 net_set_state(NETLOOP_SUCCESS);
498 } else if (timeout) {
499 printf("NCSI: timeout during configure\n");
500 net_set_state(NETLOOP_FAIL);
504 printf("NCSI: something went very wrong, nevermind\n");
505 net_set_state(NETLOOP_FAIL);
510 static void ncsi_timeout_handler(void)
512 if (ncsi_priv->pending_requests)
513 ncsi_priv->pending_requests--;
515 ncsi_update_state(NULL);
518 static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
519 uchar *payload, int len, bool wait)
521 struct ncsi_pkt_hdr *hdr;
528 pkt = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
533 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_NCSI);
536 /* Set NCSI command header fields */
537 hdr = (struct ncsi_pkt_hdr *)pkt;
539 hdr->revision = NCSI_PKT_REVISION;
540 hdr->id = ++ncsi_priv->last_request;
541 ncsi_priv->requests[ncsi_priv->last_request] = 1;
543 hdr->channel = NCSI_TO_CHANNEL(np, nc);
544 hdr->length = htons(len);
547 memcpy(pkt + sizeof(struct ncsi_pkt_hdr), payload, len);
549 /* Calculate checksum */
550 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
552 pchecksum = (__be32 *)((void *)(hdr + 1) + len);
553 put_unaligned_be32(htonl(checksum), pchecksum);
556 net_set_timeout_handler(1000UL, ncsi_timeout_handler);
557 ncsi_priv->pending_requests++;
562 /* frame header, packet header, payload, checksum */
563 final_len = eth_hdr_size + sizeof(struct ncsi_cmd_pkt_hdr) + len + 4;
565 net_send_packet(start, final_len);
570 static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
572 struct ncsi_aen_pkt_hdr *hdr = (struct ncsi_aen_pkt_hdr *)ip;
578 case NCSI_PKT_AEN_LSC:
579 printf("NCSI: link state changed\n");
582 case NCSI_PKT_AEN_CR:
583 printf("NCSI: re-configuration required\n");
586 case NCSI_PKT_AEN_HNCDSC:
587 /* Host notifcation - N/A but weird */
588 debug("NCSI: HNCDSC AEN received\n");
591 printf("%s: Invalid type 0x%02x\n", __func__, hdr->type);
595 /* Validate packet */
596 if (hdr->common.revision != 1) {
597 printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
598 hdr->common.type, hdr->common.revision);
602 if (ntohs(hdr->common.length) != payload) {
603 printf("NCSI: 0x%02x response has incorrect length %d\n",
604 hdr->common.type, hdr->common.length);
608 pchecksum = get_unaligned_be32((void *)(hdr + 1) + payload - 4);
609 if (pchecksum != 0) {
610 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
611 sizeof(*hdr) + payload - 4);
612 if (pchecksum != checksum) {
613 printf("NCSI: 0x%02x response has invalid checksum\n",
619 /* Link or configuration lost - just redo the discovery process */
620 ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
621 for (i = 0; i < ncsi_priv->n_packages; i++)
622 free(ncsi_priv->packages[i].channels);
623 free(ncsi_priv->packages);
624 ncsi_priv->n_packages = 0;
626 ncsi_priv->current_package = NCSI_PACKAGE_MAX;
627 ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
629 ncsi_probe_packages();
632 void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
635 struct ncsi_rsp_pkt *pkt = (struct ncsi_rsp_pkt *)ip;
636 struct ncsi_rsp_pkt_hdr *nh = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
637 void (*handler)(struct ncsi_rsp_pkt *pkt) = NULL;
638 unsigned short payload;
640 if (ncsi_priv->pending_requests)
641 ncsi_priv->pending_requests--;
643 if (len < sizeof(struct ncsi_rsp_pkt_hdr)) {
644 printf("NCSI: undersized packet: %u bytes\n", len);
648 if (nh->common.type == NCSI_PKT_AEN)
649 return ncsi_handle_aen(ip, len);
651 switch (nh->common.type) {
652 case NCSI_PKT_RSP_SP:
654 handler = ncsi_rsp_sp;
656 case NCSI_PKT_RSP_DP:
658 handler = ncsi_rsp_dp;
660 case NCSI_PKT_RSP_CIS:
662 handler = ncsi_rsp_cis;
664 case NCSI_PKT_RSP_GLS:
666 handler = ncsi_rsp_gls;
668 case NCSI_PKT_RSP_GVI:
670 handler = ncsi_rsp_gvi;
672 case NCSI_PKT_RSP_GC:
674 handler = ncsi_rsp_gc;
676 case NCSI_PKT_RSP_SMA:
678 handler = ncsi_rsp_sma;
680 case NCSI_PKT_RSP_EBF:
682 handler = ncsi_rsp_ebf;
684 case NCSI_PKT_RSP_ECNT:
686 handler = ncsi_rsp_ecnt;
688 case NCSI_PKT_RSP_EC:
690 handler = ncsi_rsp_ec;
692 case NCSI_PKT_RSP_AE:
697 printf("NCSI: unsupported packet type 0x%02x\n",
702 if (ncsi_validate_rsp(pkt, payload) != 0) {
703 printf("NCSI: discarding invalid packet of type 0x%02x\n",
711 ncsi_update_state(nh);
714 static void ncsi_send_sp(unsigned int np)
716 uchar payload[4] = {0};
718 ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_SP,
719 (unsigned char *)&payload,
720 cmd_payload(NCSI_PKT_CMD_SP), true);
723 static void ncsi_send_dp(unsigned int np)
725 ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_DP, NULL, 0,
729 static void ncsi_send_gls(unsigned int np, unsigned int nc)
731 ncsi_send_command(np, nc, NCSI_PKT_CMD_GLS, NULL, 0, true);
734 static void ncsi_send_cis(unsigned int np, unsigned int nc)
736 ncsi_send_command(np, nc, NCSI_PKT_CMD_CIS, NULL, 0, true);
739 static void ncsi_send_ae(unsigned int np, unsigned int nc)
741 struct ncsi_cmd_ae_pkt cmd;
743 memset(&cmd, 0, sizeof(cmd));
744 cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_aen);
746 ncsi_send_command(np, nc, NCSI_PKT_CMD_AE,
747 ((unsigned char *)&cmd)
748 + sizeof(struct ncsi_cmd_pkt_hdr),
749 cmd_payload(NCSI_PKT_CMD_AE), true);
752 static void ncsi_send_ebf(unsigned int np, unsigned int nc)
754 struct ncsi_cmd_ebf_pkt cmd;
756 memset(&cmd, 0, sizeof(cmd));
757 cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_bc);
759 ncsi_send_command(np, nc, NCSI_PKT_CMD_EBF,
760 ((unsigned char *)&cmd)
761 + sizeof(struct ncsi_cmd_pkt_hdr),
762 cmd_payload(NCSI_PKT_CMD_EBF), true);
765 static void ncsi_send_sma(unsigned int np, unsigned int nc)
767 struct ncsi_cmd_sma_pkt cmd;
768 unsigned char *addr, i;
770 addr = eth_get_ethaddr();
772 printf("NCSI: no MAC address configured\n");
776 memset(&cmd, 0, sizeof(cmd));
777 for (i = 0; i < ARP_HLEN; i++)
778 cmd.mac[i] = addr[i];
782 ncsi_send_command(np, nc, NCSI_PKT_CMD_SMA,
783 ((unsigned char *)&cmd)
784 + sizeof(struct ncsi_cmd_pkt_hdr),
785 cmd_payload(NCSI_PKT_CMD_SMA), true);
788 void ncsi_probe_packages(void)
790 struct ncsi_package *package;
793 switch (ncsi_priv->state) {
794 case NCSI_PROBE_PACKAGE_SP:
795 if (ncsi_priv->current_package == NCSI_PACKAGE_MAX)
796 ncsi_priv->current_package = 0;
797 ncsi_send_sp(ncsi_priv->current_package);
799 case NCSI_PROBE_PACKAGE_DP:
800 ncsi_send_dp(ncsi_priv->current_package);
802 case NCSI_PROBE_CHANNEL_SP:
803 if (ncsi_priv->n_packages > 0)
804 ncsi_send_sp(ncsi_priv->current_package);
806 printf("NCSI: no packages discovered, configuration not possible\n");
808 case NCSI_PROBE_CHANNEL:
809 /* Kicks off chain of channel discovery */
810 ncsi_send_cis(ncsi_priv->current_package,
811 ncsi_priv->current_channel);
814 for (np = 0; np < ncsi_priv->n_packages; np++) {
815 package = &ncsi_priv->packages[np];
816 for (nc = 0; nc < package->n_channels; nc++)
817 if (package->channels[nc].has_link)
819 if (nc < package->n_channels)
822 if (np == ncsi_priv->n_packages) {
823 printf("NCSI: no link available\n");
827 printf("NCSI: configuring channel %d\n", nc);
828 ncsi_priv->current_package = np;
829 ncsi_priv->current_channel = nc;
830 /* Kicks off rest of configure chain */
831 ncsi_send_sma(np, nc);
834 printf("NCSI: unknown state 0x%x\n", ncsi_priv->state);
838 int ncsi_probe(struct phy_device *phydev)
841 phydev->priv = malloc(sizeof(struct ncsi));
844 memset(phydev->priv, 0, sizeof(struct ncsi));
847 ncsi_priv = phydev->priv;
852 int ncsi_startup(struct phy_device *phydev)
854 /* Set phydev parameters */
855 phydev->speed = SPEED_100;
856 phydev->duplex = DUPLEX_FULL;
857 /* Normal phy reset is N/A */
858 phydev->flags |= PHY_FLAG_BROKEN_RESET;
860 /* Set initial probe state */
861 ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
863 /* No active package/channel yet */
864 ncsi_priv->current_package = NCSI_PACKAGE_MAX;
865 ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
867 /* Pretend link works so the MAC driver sets final bits up */
870 /* Set ncsi_priv so we can use it when called from net_loop() */
871 ncsi_priv = phydev->priv;
876 int ncsi_shutdown(struct phy_device *phydev)
878 printf("NCSI: Disabling package %d\n", ncsi_priv->current_package);
879 ncsi_send_dp(ncsi_priv->current_package);
883 static struct phy_driver ncsi_driver = {
887 .features = PHY_100BT_FEATURES | PHY_DEFAULT_FEATURES |
888 SUPPORTED_100baseT_Full | SUPPORTED_MII,
890 .startup = ncsi_startup,
891 .shutdown = ncsi_shutdown,
894 int phy_ncsi_init(void)
896 phy_register(&ncsi_driver);