1 // SPDX-License-Identifier: GPL-2.0+
3 * NC-SI protocol configuration
5 * Copyright (C) 2019, IBM Corporation.
12 #include <net/ncsi-pkt.h>
13 #include <asm/unaligned.h>
15 #define NCSI_PACKAGE_MAX 8
16 #define NCSI_CHANNEL_MAX 31
18 #define NCSI_PACKAGE_SHIFT 5
19 #define NCSI_PACKAGE_INDEX(c) (((c) >> NCSI_PACKAGE_SHIFT) & 0x7)
20 #define NCSI_RESERVED_CHANNEL 0x1f
21 #define NCSI_CHANNEL_INDEX(c) ((c) & ((1 << NCSI_PACKAGE_SHIFT) - 1))
22 #define NCSI_TO_CHANNEL(p, c) (((p) << NCSI_PACKAGE_SHIFT) | (c))
24 #define NCSI_PKT_REVISION 0x01
26 #define NCSI_CAP_GENERIC_MASK 0x7f
27 #define NCSI_CAP_BC_MASK 0x0f
28 #define NCSI_CAP_MC_MASK 0x3f
29 #define NCSI_CAP_AEN_MASK 0x07
30 #define NCSI_CAP_VLAN_MASK 0x07
32 static void ncsi_send_ebf(unsigned int np, unsigned int nc);
33 static void ncsi_send_ae(unsigned int np, unsigned int nc);
34 static void ncsi_send_gls(unsigned int np, unsigned int nc);
35 static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
36 uchar *payload, int len, bool wait);
50 /* version information */
52 u32 version; /* Supported BCD encoded NCSI version */
53 u32 alpha2; /* Supported BCD encoded NCSI version */
54 u8 fw_name[12]; /* Firmware name string */
55 u32 fw_version; /* Firmware version */
56 u16 pci_ids[4]; /* PCI identification */
57 u32 mf_id; /* Manufacture ID */
64 unsigned int n_channels;
65 struct ncsi_channel *channels;
70 NCSI_PROBE_PACKAGE_SP,
71 NCSI_PROBE_PACKAGE_DP,
72 NCSI_PROBE_CHANNEL_SP,
77 unsigned int pending_requests;
78 unsigned int requests[256];
79 unsigned int last_request;
81 unsigned int current_package;
82 unsigned int current_channel;
84 unsigned int n_packages;
85 struct ncsi_package *packages;
88 struct ncsi *ncsi_priv;
90 bool ncsi_active(void)
97 np = ncsi_priv->current_package;
98 nc = ncsi_priv->current_channel;
100 if (ncsi_priv->state != NCSI_CONFIG)
103 return np < NCSI_PACKAGE_MAX && nc < NCSI_CHANNEL_MAX &&
104 ncsi_priv->packages[np].channels[nc].has_link;
107 static unsigned int cmd_payload(int cmd)
110 case NCSI_PKT_CMD_CIS:
112 case NCSI_PKT_CMD_SP:
114 case NCSI_PKT_CMD_DP:
116 case NCSI_PKT_CMD_EC:
118 case NCSI_PKT_CMD_DC:
120 case NCSI_PKT_CMD_RC:
122 case NCSI_PKT_CMD_ECNT:
124 case NCSI_PKT_CMD_DCNT:
126 case NCSI_PKT_CMD_AE:
128 case NCSI_PKT_CMD_SL:
130 case NCSI_PKT_CMD_GLS:
132 case NCSI_PKT_CMD_SVF:
134 case NCSI_PKT_CMD_EV:
136 case NCSI_PKT_CMD_DV:
138 case NCSI_PKT_CMD_SMA:
140 case NCSI_PKT_CMD_EBF:
142 case NCSI_PKT_CMD_DBF:
144 case NCSI_PKT_CMD_EGMF:
146 case NCSI_PKT_CMD_DGMF:
148 case NCSI_PKT_CMD_SNFC:
150 case NCSI_PKT_CMD_GVI:
152 case NCSI_PKT_CMD_GC:
154 case NCSI_PKT_CMD_GP:
156 case NCSI_PKT_CMD_GCPS:
158 case NCSI_PKT_CMD_GNS:
160 case NCSI_PKT_CMD_GNPTS:
162 case NCSI_PKT_CMD_GPS:
165 printf("NCSI: Unknown command 0x%02x\n", cmd);
170 static u32 ncsi_calculate_checksum(unsigned char *data, int len)
175 for (i = 0; i < len; i += 2)
176 checksum += (((u32)data[i] << 8) | data[i + 1]);
178 checksum = (~checksum + 1);
182 static int ncsi_validate_rsp(struct ncsi_rsp_pkt *pkt, int payload)
184 struct ncsi_rsp_pkt_hdr *hdr = &pkt->rsp;
185 u32 checksum, c_offset;
188 if (hdr->common.revision != 1) {
189 printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
190 hdr->common.type, hdr->common.revision);
194 if (hdr->code != 0) {
195 printf("NCSI: 0x%02x response returns error %d\n",
196 hdr->common.type, __be16_to_cpu(hdr->code));
197 if (ntohs(hdr->reason) == 0x05)
198 printf("(Invalid command length)\n");
202 if (ntohs(hdr->common.length) != payload) {
203 printf("NCSI: 0x%02x response has incorrect length %d\n",
204 hdr->common.type, hdr->common.length);
208 c_offset = sizeof(struct ncsi_rsp_pkt_hdr) + payload - sizeof(checksum);
209 pchecksum = get_unaligned_be32((void *)hdr + c_offset);
210 if (pchecksum != 0) {
211 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
213 if (pchecksum != checksum) {
214 printf("NCSI: 0x%02x response has invalid checksum\n",
223 static void ncsi_rsp_ec(struct ncsi_rsp_pkt *pkt)
225 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
228 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
229 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
231 if (ncsi_priv->packages[np].channels[nc].cap_aen != 0)
232 ncsi_send_ae(np, nc);
236 static void ncsi_rsp_ecnt(struct ncsi_rsp_pkt *pkt)
238 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
241 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
242 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
244 ncsi_send_command(np, nc, NCSI_PKT_CMD_EC, NULL, 0, true);
247 static void ncsi_rsp_ebf(struct ncsi_rsp_pkt *pkt)
249 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
252 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
253 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
255 ncsi_send_command(np, nc, NCSI_PKT_CMD_ECNT, NULL, 0, true);
258 static void ncsi_rsp_sma(struct ncsi_rsp_pkt *pkt)
260 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
263 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
264 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
266 ncsi_send_ebf(np, nc);
269 static void ncsi_rsp_gc(struct ncsi_rsp_pkt *pkt)
271 struct ncsi_rsp_gc_pkt *gc = (struct ncsi_rsp_gc_pkt *)pkt;
272 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gc->rsp;
273 struct ncsi_channel *c;
276 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
277 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",
286 c = &ncsi_priv->packages[np].channels[nc];
287 c->cap_generic = ntohl(gc->cap) & NCSI_CAP_GENERIC_MASK;
288 c->cap_bc = ntohl(gc->bc_cap) & NCSI_CAP_BC_MASK;
289 c->cap_mc = ntohl(gc->mc_cap) & NCSI_CAP_MC_MASK;
290 c->cap_aen = ntohl(gc->aen_cap) & NCSI_CAP_AEN_MASK;
291 c->cap_vlan = ntohl(gc->vlan_mode) & NCSI_CAP_VLAN_MASK;
293 /* End of probe for this channel */
296 static void ncsi_rsp_gvi(struct ncsi_rsp_pkt *pkt)
298 struct ncsi_rsp_gvi_pkt *gvi = (struct ncsi_rsp_gvi_pkt *)pkt;
299 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gvi->rsp;
300 struct ncsi_channel *c;
301 unsigned int np, nc, i;
303 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
304 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
306 if (np >= ncsi_priv->n_packages ||
307 nc >= ncsi_priv->packages[np].n_channels) {
308 printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
313 c = &ncsi_priv->packages[np].channels[nc];
314 c->version.version = get_unaligned_be32(&gvi->ncsi_version);
315 c->version.alpha2 = gvi->alpha2;
316 memcpy(c->version.fw_name, gvi->fw_name, sizeof(c->version.fw_name));
317 c->version.fw_version = get_unaligned_be32(&gvi->fw_version);
318 for (i = 0; i < ARRAY_SIZE(c->version.pci_ids); i++)
319 c->version.pci_ids[i] = get_unaligned_be16(gvi->pci_ids + i);
320 c->version.mf_id = get_unaligned_be32(&gvi->mf_id);
322 if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
323 ncsi_send_command(np, nc, NCSI_PKT_CMD_GC, NULL, 0, true);
326 static void ncsi_rsp_gls(struct ncsi_rsp_pkt *pkt)
328 struct ncsi_rsp_gls_pkt *gls = (struct ncsi_rsp_gls_pkt *)pkt;
329 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)&gls->rsp;
332 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
333 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
335 if (np >= ncsi_priv->n_packages ||
336 nc >= ncsi_priv->packages[np].n_channels) {
337 printf("NCSI: Invalid package / channel (0x%02x, 0x%02x)\n",
342 ncsi_priv->packages[np].channels[nc].has_link =
343 !!(get_unaligned_be32(&gls->status));
345 if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
346 ncsi_send_command(np, nc, NCSI_PKT_CMD_GVI, NULL, 0, true);
349 static void ncsi_rsp_cis(struct ncsi_rsp_pkt *pkt)
351 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
352 struct ncsi_package *package;
355 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
356 nc = NCSI_CHANNEL_INDEX(rsp->common.channel);
358 if (np >= ncsi_priv->n_packages) {
359 printf("NCSI: Mystery package 0x%02x from CIS\n", np);
363 package = &ncsi_priv->packages[np];
365 if (nc < package->n_channels) {
367 * This is fine in general but in the current design we
368 * don't send CIS commands to known channels.
370 debug("NCSI: Duplicate channel 0x%02x\n", nc);
374 package->channels = realloc(package->channels,
375 sizeof(struct ncsi_channel) *
376 (package->n_channels + 1));
377 if (!package->channels) {
378 printf("NCSI: Could not allocate memory for new channel\n");
382 debug("NCSI: New channel 0x%02x\n", nc);
384 package->channels[nc].id = nc;
385 package->channels[nc].has_link = false;
386 package->n_channels++;
388 ncsi_send_gls(np, nc);
391 static void ncsi_rsp_dp(struct ncsi_rsp_pkt *pkt)
393 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
396 /* No action needed */
398 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
399 if (np >= ncsi_priv->n_packages)
400 debug("NCSI: DP response from unknown package %d\n", np);
403 static void ncsi_rsp_sp(struct ncsi_rsp_pkt *pkt)
405 struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
408 np = NCSI_PACKAGE_INDEX(rsp->common.channel);
410 if (np < ncsi_priv->n_packages) {
411 /* Already know about this package */
412 debug("NCSI: package 0x%02x selected\n", np);
416 debug("NCSI: adding new package %d\n", np);
418 ncsi_priv->packages = realloc(ncsi_priv->packages,
419 sizeof(struct ncsi_package) *
420 (ncsi_priv->n_packages + 1));
421 if (!ncsi_priv->packages) {
422 printf("NCSI: could not allocate memory for new package\n");
426 ncsi_priv->packages[np].id = np;
427 ncsi_priv->packages[np].n_channels = 0;
428 ncsi_priv->packages[np].channels = NULL;
429 ncsi_priv->n_packages++;
432 static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
437 switch (ncsi_priv->state) {
438 case NCSI_PROBE_PACKAGE_SP:
440 ncsi_priv->current_package + 1 < NCSI_PACKAGE_MAX) {
441 ncsi_priv->current_package++;
443 ncsi_priv->state = NCSI_PROBE_PACKAGE_DP;
444 ncsi_priv->current_package = 0;
446 return ncsi_probe_packages();
447 case NCSI_PROBE_PACKAGE_DP:
448 if (ncsi_priv->current_package + 1 < ncsi_priv->n_packages &&
450 ncsi_priv->current_package++;
452 if (!ncsi_priv->n_packages) {
453 printf("NCSI: no packages found\n");
454 net_set_state(NETLOOP_FAIL);
457 printf("NCSI: probing channels\n");
458 ncsi_priv->state = NCSI_PROBE_CHANNEL_SP;
459 ncsi_priv->current_package = 0;
460 ncsi_priv->current_channel = 0;
462 return ncsi_probe_packages();
463 case NCSI_PROBE_CHANNEL_SP:
464 if (!timeout && nh->common.type == NCSI_PKT_RSP_SP) {
465 ncsi_priv->state = NCSI_PROBE_CHANNEL;
466 return ncsi_probe_packages();
468 printf("NCSI: failed to select package 0x%0x2 or timeout\n",
469 ncsi_priv->current_package);
470 net_set_state(NETLOOP_FAIL);
472 case NCSI_PROBE_CHANNEL:
473 // TODO only does package 0 for now
474 if (ncsi_priv->pending_requests == 0) {
475 np = ncsi_priv->current_package;
476 nc = ncsi_priv->current_channel;
478 /* Configure first channel that has link */
479 if (ncsi_priv->packages[np].channels[nc].has_link) {
480 ncsi_priv->state = NCSI_CONFIG;
481 } else if (ncsi_priv->current_channel + 1 <
483 ncsi_priv->current_channel++;
485 // XXX As above only package 0
486 printf("NCSI: no channel found with link\n");
487 net_set_state(NETLOOP_FAIL);
490 return ncsi_probe_packages();
494 if (ncsi_priv->pending_requests == 0) {
495 printf("NCSI: configuration done!\n");
496 net_set_state(NETLOOP_SUCCESS);
497 } else if (timeout) {
498 printf("NCSI: timeout during configure\n");
499 net_set_state(NETLOOP_FAIL);
503 printf("NCSI: something went very wrong, nevermind\n");
504 net_set_state(NETLOOP_FAIL);
509 static void ncsi_timeout_handler(void)
511 if (ncsi_priv->pending_requests)
512 ncsi_priv->pending_requests--;
514 ncsi_update_state(NULL);
517 static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
518 uchar *payload, int len, bool wait)
520 struct ncsi_pkt_hdr *hdr;
527 pkt = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
532 eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_NCSI);
535 /* Set NCSI command header fields */
536 hdr = (struct ncsi_pkt_hdr *)pkt;
538 hdr->revision = NCSI_PKT_REVISION;
539 hdr->id = ++ncsi_priv->last_request;
540 ncsi_priv->requests[ncsi_priv->last_request] = 1;
542 hdr->channel = NCSI_TO_CHANNEL(np, nc);
543 hdr->length = htons(len);
546 memcpy(pkt + sizeof(struct ncsi_pkt_hdr), payload, len);
548 /* Calculate checksum */
549 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
551 pchecksum = (__be32 *)((void *)(hdr + 1) + len);
552 put_unaligned_be32(htonl(checksum), pchecksum);
555 net_set_timeout_handler(1000UL, ncsi_timeout_handler);
556 ncsi_priv->pending_requests++;
561 /* frame header, packet header, payload, checksum */
562 final_len = eth_hdr_size + sizeof(struct ncsi_cmd_pkt_hdr) + len + 4;
564 net_send_packet(start, final_len);
569 static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
571 struct ncsi_aen_pkt_hdr *hdr = (struct ncsi_aen_pkt_hdr *)ip;
577 case NCSI_PKT_AEN_LSC:
578 printf("NCSI: link state changed\n");
581 case NCSI_PKT_AEN_CR:
582 printf("NCSI: re-configuration required\n");
585 case NCSI_PKT_AEN_HNCDSC:
586 /* Host notifcation - N/A but weird */
587 debug("NCSI: HNCDSC AEN received\n");
590 printf("%s: Invalid type 0x%02x\n", __func__, hdr->type);
594 /* Validate packet */
595 if (hdr->common.revision != 1) {
596 printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
597 hdr->common.type, hdr->common.revision);
601 if (ntohs(hdr->common.length) != payload) {
602 printf("NCSI: 0x%02x response has incorrect length %d\n",
603 hdr->common.type, hdr->common.length);
607 pchecksum = get_unaligned_be32((void *)(hdr + 1) + payload - 4);
608 if (pchecksum != 0) {
609 checksum = ncsi_calculate_checksum((unsigned char *)hdr,
610 sizeof(*hdr) + payload - 4);
611 if (pchecksum != checksum) {
612 printf("NCSI: 0x%02x response has invalid checksum\n",
618 /* Link or configuration lost - just redo the discovery process */
619 ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
620 for (i = 0; i < ncsi_priv->n_packages; i++)
621 free(ncsi_priv->packages[i].channels);
622 free(ncsi_priv->packages);
623 ncsi_priv->n_packages = 0;
625 ncsi_priv->current_package = NCSI_PACKAGE_MAX;
626 ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
628 ncsi_probe_packages();
631 void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
634 struct ncsi_rsp_pkt *pkt = (struct ncsi_rsp_pkt *)ip;
635 struct ncsi_rsp_pkt_hdr *nh = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
636 void (*handler)(struct ncsi_rsp_pkt *pkt) = NULL;
637 unsigned short payload;
639 if (ncsi_priv->pending_requests)
640 ncsi_priv->pending_requests--;
642 if (len < sizeof(struct ncsi_rsp_pkt_hdr)) {
643 printf("NCSI: undersized packet: %u bytes\n", len);
647 if (nh->common.type == NCSI_PKT_AEN)
648 return ncsi_handle_aen(ip, len);
650 switch (nh->common.type) {
651 case NCSI_PKT_RSP_SP:
653 handler = ncsi_rsp_sp;
655 case NCSI_PKT_RSP_DP:
657 handler = ncsi_rsp_dp;
659 case NCSI_PKT_RSP_CIS:
661 handler = ncsi_rsp_cis;
663 case NCSI_PKT_RSP_GLS:
665 handler = ncsi_rsp_gls;
667 case NCSI_PKT_RSP_GVI:
669 handler = ncsi_rsp_gvi;
671 case NCSI_PKT_RSP_GC:
673 handler = ncsi_rsp_gc;
675 case NCSI_PKT_RSP_SMA:
677 handler = ncsi_rsp_sma;
679 case NCSI_PKT_RSP_EBF:
681 handler = ncsi_rsp_ebf;
683 case NCSI_PKT_RSP_ECNT:
685 handler = ncsi_rsp_ecnt;
687 case NCSI_PKT_RSP_EC:
689 handler = ncsi_rsp_ec;
691 case NCSI_PKT_RSP_AE:
696 printf("NCSI: unsupported packet type 0x%02x\n",
701 if (ncsi_validate_rsp(pkt, payload) != 0) {
702 printf("NCSI: discarding invalid packet of type 0x%02x\n",
710 ncsi_update_state(nh);
713 static void ncsi_send_sp(unsigned int np)
715 uchar payload[4] = {0};
717 ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_SP,
718 (unsigned char *)&payload,
719 cmd_payload(NCSI_PKT_CMD_SP), true);
722 static void ncsi_send_dp(unsigned int np)
724 ncsi_send_command(np, NCSI_RESERVED_CHANNEL, NCSI_PKT_CMD_DP, NULL, 0,
728 static void ncsi_send_gls(unsigned int np, unsigned int nc)
730 ncsi_send_command(np, nc, NCSI_PKT_CMD_GLS, NULL, 0, true);
733 static void ncsi_send_cis(unsigned int np, unsigned int nc)
735 ncsi_send_command(np, nc, NCSI_PKT_CMD_CIS, NULL, 0, true);
738 static void ncsi_send_ae(unsigned int np, unsigned int nc)
740 struct ncsi_cmd_ae_pkt cmd;
742 memset(&cmd, 0, sizeof(cmd));
743 cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_aen);
745 ncsi_send_command(np, nc, NCSI_PKT_CMD_AE,
746 ((unsigned char *)&cmd)
747 + sizeof(struct ncsi_cmd_pkt_hdr),
748 cmd_payload(NCSI_PKT_CMD_AE), true);
751 static void ncsi_send_ebf(unsigned int np, unsigned int nc)
753 struct ncsi_cmd_ebf_pkt cmd;
755 memset(&cmd, 0, sizeof(cmd));
756 cmd.mode = htonl(ncsi_priv->packages[np].channels[nc].cap_bc);
758 ncsi_send_command(np, nc, NCSI_PKT_CMD_EBF,
759 ((unsigned char *)&cmd)
760 + sizeof(struct ncsi_cmd_pkt_hdr),
761 cmd_payload(NCSI_PKT_CMD_EBF), true);
764 static void ncsi_send_sma(unsigned int np, unsigned int nc)
766 struct ncsi_cmd_sma_pkt cmd;
767 unsigned char *addr, i;
769 addr = eth_get_ethaddr();
771 printf("NCSI: no MAC address configured\n");
775 memset(&cmd, 0, sizeof(cmd));
776 for (i = 0; i < ARP_HLEN; i++)
777 cmd.mac[i] = addr[i];
781 ncsi_send_command(np, nc, NCSI_PKT_CMD_SMA,
782 ((unsigned char *)&cmd)
783 + sizeof(struct ncsi_cmd_pkt_hdr),
784 cmd_payload(NCSI_PKT_CMD_SMA), true);
787 void ncsi_probe_packages(void)
789 struct ncsi_package *package;
792 switch (ncsi_priv->state) {
793 case NCSI_PROBE_PACKAGE_SP:
794 if (ncsi_priv->current_package == NCSI_PACKAGE_MAX)
795 ncsi_priv->current_package = 0;
796 ncsi_send_sp(ncsi_priv->current_package);
798 case NCSI_PROBE_PACKAGE_DP:
799 ncsi_send_dp(ncsi_priv->current_package);
801 case NCSI_PROBE_CHANNEL_SP:
802 if (ncsi_priv->n_packages > 0)
803 ncsi_send_sp(ncsi_priv->current_package);
805 printf("NCSI: no packages discovered, configuration not possible\n");
807 case NCSI_PROBE_CHANNEL:
808 /* Kicks off chain of channel discovery */
809 ncsi_send_cis(ncsi_priv->current_package,
810 ncsi_priv->current_channel);
813 for (np = 0; np < ncsi_priv->n_packages; np++) {
814 package = &ncsi_priv->packages[np];
815 for (nc = 0; nc < package->n_channels; nc++)
816 if (package->channels[nc].has_link)
818 if (nc < package->n_channels)
821 if (np == ncsi_priv->n_packages) {
822 printf("NCSI: no link available\n");
826 printf("NCSI: configuring channel %d\n", nc);
827 ncsi_priv->current_package = np;
828 ncsi_priv->current_channel = nc;
829 /* Kicks off rest of configure chain */
830 ncsi_send_sma(np, nc);
833 printf("NCSI: unknown state 0x%x\n", ncsi_priv->state);
837 int ncsi_probe(struct phy_device *phydev)
840 phydev->priv = malloc(sizeof(struct ncsi));
843 memset(phydev->priv, 0, sizeof(struct ncsi));
846 ncsi_priv = phydev->priv;
851 int ncsi_startup(struct phy_device *phydev)
853 /* Set phydev parameters */
854 phydev->speed = SPEED_100;
855 phydev->duplex = DUPLEX_FULL;
856 /* Normal phy reset is N/A */
857 phydev->flags |= PHY_FLAG_BROKEN_RESET;
859 /* Set initial probe state */
860 ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
862 /* No active package/channel yet */
863 ncsi_priv->current_package = NCSI_PACKAGE_MAX;
864 ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
866 /* Pretend link works so the MAC driver sets final bits up */
869 /* Set ncsi_priv so we can use it when called from net_loop() */
870 ncsi_priv = phydev->priv;
875 int ncsi_shutdown(struct phy_device *phydev)
877 printf("NCSI: Disabling package %d\n", ncsi_priv->current_package);
878 ncsi_send_dp(ncsi_priv->current_package);
882 static struct phy_driver ncsi_driver = {
886 .features = PHY_100BT_FEATURES | PHY_DEFAULT_FEATURES |
887 SUPPORTED_100baseT_Full | SUPPORTED_MII,
889 .startup = ncsi_startup,
890 .shutdown = ncsi_shutdown,
893 int phy_ncsi_init(void)
895 phy_register(&ncsi_driver);