3 mii-tool: monitor and control the MII for a network interface
7 mii-tool [-VvRrw] [-A media,... | -F media] [interface ...]
9 This program is based on Donald Becker's "mii-diag" program, which
10 is more capable and verbose than this tool, but also somewhat
13 Copyright (C) 2000 David A. Hinds -- dhinds@pcmcia.sourceforge.org
15 mii-diag is written/copyright 1997-2000 by Donald Becker
18 This program is free software; you can redistribute it
19 and/or modify it under the terms of the GNU General Public
20 License as published by the Free Software Foundation.
22 Donald Becker may be reached as becker@scyld.com, or C/O
23 Scyld Computing Corporation, 410 Severn Av., Suite 210,
27 http://www.scyld.com/diag/mii-status.html
28 http://www.scyld.com/expert/NWay.html
29 http://www.national.com/pf/DP/DP83840.html
32 static char Version[] = "$Id: mii-tool.c,v 1.9 2006/09/27 20:59:18 ecki Exp $\n(Author: David Hinds based on Donald Becker's mii-diag)";
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
48 #include <linux/sockios.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_ether.h>
54 #include <linux/mii.h>
55 #include <linux/sockios.h>
57 #include "net-support.h"
59 #define MAX_ETH 8 /* Maximum # of interfaces */
60 #define LPA_ABILITY_MASK 0x07e0
62 /* Table of known MII's */
67 { 0x0022, 0x5610, "AdHoc AH101LF" },
68 { 0x0022, 0x5520, "Altimata AC101LF" },
69 { 0x0000, 0x6b90, "AMD 79C901A HomePNA" },
70 { 0x0000, 0x6b70, "AMD 79C901A 10baseT" },
71 { 0x0181, 0xb800, "Davicom DM9101" },
72 { 0x0043, 0x7411, "Enable EL40-331" },
73 { 0x0015, 0xf410, "ICS 1889" },
74 { 0x0015, 0xf420, "ICS 1890" },
75 { 0x0015, 0xf430, "ICS 1892" },
76 { 0x02a8, 0x0150, "Intel 82555" },
77 { 0x7810, 0x0000, "Level One LXT970/971" },
78 { 0x2000, 0x5c00, "National DP83840A" },
79 { 0x0181, 0x4410, "Quality QS6612" },
80 { 0x0282, 0x1c50, "SMSC 83C180" },
81 { 0x0300, 0xe540, "TDK 78Q2120" },
82 { 0x0141, 0x0c20, "Yukon 88E1011" },
83 { 0x0141, 0x0cc0, "Yukon-EC 88E1111" },
84 { 0x0141, 0x0c90, "Yukon-2 88E1112" },
86 #define NMII (sizeof(mii_id)/sizeof(mii_id[0]))
88 /*--------------------------------------------------------------------*/
90 struct option longopts[] = {
91 /* { name has_arg *flag val } */
92 {"advertise", 1, 0, 'A'}, /* Change capabilities advertised. */
93 {"force", 1, 0, 'F'}, /* Change capabilities advertised. */
94 {"phy", 1, 0, 'p'}, /* Set PHY (MII address) to report. */
95 {"log", 0, 0, 'l'}, /* Set PHY (MII address) to report. */
96 {"restart", 0, 0, 'r'}, /* Restart link negotiation */
97 {"reset", 0, 0, 'R'}, /* Reset the transceiver. */
98 {"verbose", 0, 0, 'v'}, /* Report each action taken. */
99 {"version", 0, 0, 'V'}, /* Emit version information. */
100 {"watch", 0, 0, 'w'}, /* Constantly monitor the port. */
101 {"help", 0, 0, '?'}, /* Give help */
112 static int nway_advertise = 0;
113 static int fixed_speed = 0;
114 static int override_phy = -1;
116 static int skfd = -1; /* AF_INET socket for ioctl() calls. */
117 static struct ifreq ifr;
119 /*--------------------------------------------------------------------*/
121 static int mdio_read(int skfd, int location)
123 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
124 mii->reg_num = location;
125 if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0) {
126 fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name,
133 static void mdio_write(int skfd, int location, int value)
135 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
136 mii->reg_num = location;
138 if (ioctl(skfd, SIOCSMIIREG, &ifr) < 0) {
139 fprintf(stderr, "SIOCSMIIREG on %s failed: %s\n", ifr.ifr_name,
144 /*--------------------------------------------------------------------*/
150 /* The order through 100baseT4 matches bits in the BMSR */
151 { "10baseT-HD", {LPA_10HALF} },
152 { "10baseT-FD", {LPA_10FULL} },
153 { "100baseTx-HD", {LPA_100HALF} },
154 { "100baseTx-FD", {LPA_100FULL} },
155 { "100baseT4", {LPA_100BASE4} },
156 { "100baseTx", {LPA_100FULL | LPA_100HALF} },
157 { "10baseT", {LPA_10FULL | LPA_10HALF} },
159 { "1000baseT-HD", {0, ADVERTISE_1000HALF} },
160 { "1000baseT-FD", {0, ADVERTISE_1000FULL} },
161 { "1000baseT", {0, ADVERTISE_1000HALF|ADVERTISE_1000FULL} },
163 #define NMEDIA (sizeof(media)/sizeof(media[0]))
165 /* Parse an argument list of media types */
166 static int parse_media(char *arg, unsigned *bmcr2)
170 mask = strtoul(arg, &s, 16);
171 if ((*arg != '\0') && (*s == '\0')) {
172 if ((mask & LPA_ABILITY_MASK) &&
173 !(mask & ~LPA_ABILITY_MASK)) {
181 s = strtok(arg, ", ");
183 for (i = 0; i < NMEDIA; i++)
184 if (s && strcasecmp(media[i].name, s) == 0) break;
185 if (i == NMEDIA) goto failed;
186 mask |= media[i].value[0];
187 *bmcr2 |= media[i].value[1];
188 } while ((s = strtok(NULL, ", ")) != NULL);
192 fprintf(stderr, "Invalid media specification '%s'.\n", arg);
196 /*--------------------------------------------------------------------*/
198 static const char *media_list(unsigned mask, unsigned mask2, int best)
200 static char buf[100];
204 if (mask & BMCR_SPEED1000) {
205 if (mask2 & ADVERTISE_1000HALF) {
207 strcat(buf, "1000baseT-HD");
210 if (mask2 & ADVERTISE_1000FULL) {
212 strcat(buf, "1000baseT-FD");
218 for (i = 4; i >= 0; i--) {
221 strcat(buf, media[i].name);
227 strcat(buf, " flow-control");
231 int show_basic_mii(int sock, int phy_id)
235 unsigned bmcr, bmsr, advert, lkpar, bmcr2, lpa2;
237 /* Some bits in the BMSR are latched, but we can't rely on being
238 the only reader, so only the current values are meaningful */
239 mdio_read(sock, MII_BMSR);
240 for (i = 0; i < ((verbose > 1) ? 32 : (MII_STAT1000+1)); i++)
255 case MII_RERRCOUNTER:
263 mii_val[i] = mdio_read(sock, i);
270 if (mii_val[MII_BMCR] == 0xffff || mii_val[MII_BMSR] == 0x0000) {
271 fprintf(stderr, " No MII transceiver present!.\n");
275 /* Descriptive rename. */
276 bmcr = mii_val[MII_BMCR]; bmsr = mii_val[MII_BMSR];
277 advert = mii_val[MII_ADVERTISE]; lkpar = mii_val[MII_LPA];
278 bmcr2 = mii_val[MII_CTRL1000]; lpa2 = mii_val[MII_STAT1000];
280 sprintf(buf, "%s: ", ifr.ifr_name);
281 if (bmcr & BMCR_ANENABLE) {
282 if (bmsr & BMSR_ANEGCOMPLETE) {
283 if (advert & lkpar) {
284 strcat(buf, (lkpar & LPA_LPACK) ?
285 "negotiated" : "no autonegotiation,");
286 strcat(buf, media_list(advert & lkpar, bmcr2 & lpa2>>2, 1));
289 strcat(buf, "autonegotiation failed, ");
291 } else if (bmcr & BMCR_ANRESTART) {
292 strcat(buf, "autonegotiation restarted, ");
295 sprintf(buf+strlen(buf), "%s Mbit, %s duplex, ",
296 ((bmcr2 & (ADVERTISE_1000HALF | ADVERTISE_1000FULL)) & lpa2 >> 2)
298 : (bmcr & BMCR_SPEED100) ? "100" : "10",
299 (bmcr & BMCR_FULLDPLX) ? "full" : "half");
301 strcat(buf, (bmsr & BMSR_LSTATUS) ? "link ok" : "no link");
305 syslog(LOG_INFO, "%s", buf);
308 time_t t = time(NULL);
309 strftime(s, sizeof(s), "%T", localtime(&t));
310 printf("%s %s\n", s, buf);
317 printf(" registers for MII PHY %d: ", phy_id);
318 for (i = 0; i < 32; i++)
319 printf("%s %4.4x", ((i % 8) ? "" : "\n "), mii_val[i]);
324 printf(" product info: ");
325 for (i = 0; i < NMII; i++)
326 if ((mii_id[i].id1 == mii_val[2]) &&
327 (mii_id[i].id2 == (mii_val[3] & 0xfff0)))
330 printf("%s rev %d\n", mii_id[i].name, mii_val[3]&0x0f);
332 printf("vendor %02x:%02x:%02x, model %d rev %d\n",
333 mii_val[2]>>10, (mii_val[2]>>2)&0xff,
334 ((mii_val[2]<<6)|(mii_val[3]>>10))&0xff,
335 (mii_val[3]>>4)&0x3f, mii_val[3]&0x0f);
336 printf(" basic mode: ");
337 if (bmcr & BMCR_RESET)
338 printf("software reset, ");
339 if (bmcr & BMCR_LOOPBACK)
340 printf("loopback, ");
341 if (bmcr & BMCR_ISOLATE)
343 if (bmcr & BMCR_CTST)
344 printf("collision test, ");
345 if (bmcr & BMCR_ANENABLE) {
346 printf("autonegotiation enabled\n");
348 printf("%s Mbit, %s duplex\n",
349 (bmcr & BMCR_SPEED100) ? "100" : "10",
350 (bmcr & BMCR_FULLDPLX) ? "full" : "half");
352 printf(" basic status: ");
353 if (bmsr & BMSR_ANEGCOMPLETE)
354 printf("autonegotiation complete, ");
355 else if (bmcr & BMCR_ANRESTART)
356 printf("autonegotiation restarted, ");
357 if (bmsr & BMSR_RFAULT)
358 printf("remote fault, ");
359 printf((bmsr & BMSR_LSTATUS) ? "link ok" : "no link");
360 printf("\n capabilities:%s", media_list(bmsr >> 6, bmcr2, 0));
361 printf("\n advertising: %s", media_list(advert, bmcr2, 0));
362 if (lkpar & LPA_ABILITY_MASK)
363 printf("\n link partner:%s", media_list(lkpar, lpa2 >> 2, 0));
370 /*--------------------------------------------------------------------*/
372 static int do_one_xcvr(int skfd, char *ifname, int maybe)
374 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
376 /* Get the vitals from the interface. */
377 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
378 if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
379 if (!maybe || (errno != ENODEV))
380 fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
381 ifname, strerror(errno));
385 if (override_phy >= 0) {
386 printf("using the specified MII index %d.\n", override_phy);
387 mii->phy_id = override_phy;
391 printf("resetting the transceiver...\n");
392 mdio_write(skfd, MII_BMCR, BMCR_RESET);
394 if (nway_advertise > 0) {
395 mdio_write(skfd, MII_ADVERTISE, nway_advertise | 1);
399 printf("restarting autonegotiation...\n");
400 mdio_write(skfd, MII_BMCR, 0x0000);
401 mdio_write(skfd, MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART);
405 if (fixed_speed & (LPA_100FULL|LPA_100HALF))
406 bmcr |= BMCR_SPEED100;
407 if (fixed_speed & (LPA_100FULL|LPA_10FULL))
408 bmcr |= BMCR_FULLDPLX;
409 mdio_write(skfd, MII_BMCR, bmcr);
412 if (!opt_restart && !opt_reset && !fixed_speed && !nway_advertise)
413 show_basic_mii(skfd, mii->phy_id);
418 /*--------------------------------------------------------------------*/
420 static void watch_one_xcvr(int skfd, char *ifname, int index)
422 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
423 static int status[MAX_ETH] = { 0, /* ... */ };
426 /* Get the vitals from the interface. */
427 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
428 if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
430 fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
431 ifname, strerror(errno));
434 now = (mdio_read(skfd, MII_BMCR) |
435 (mdio_read(skfd, MII_BMSR) << 16));
436 if (status[index] && (status[index] != now))
437 show_basic_mii(skfd, mii->phy_id);
441 /*--------------------------------------------------------------------*/
444 "usage: %s [-VvRrwl] [-A media,... | -F media] <interface ...>\n"
445 " -V, --version display version information\n"
446 " -v, --verbose more verbose output\n"
447 " -R, --reset reset MII to poweron state\n"
448 " -r, --restart restart autonegotiation\n"
449 " -w, --watch monitor for link status changes\n"
450 " -l, --log with -w, write events to syslog\n"
451 " -A, --advertise=media,... advertise only specified media\n"
452 " -F, --force=media force specified media technology\n"
453 "media: 1000baseTx-HD, 1000baseTx-FD,\n"
454 " 100baseT4, 100baseTx-FD, 100baseTx-HD,\n"
455 " 10baseT-FD, 10baseT-HD,\n"
456 " (to advertise both HD and FD) 1000baseTx, 100baseTx, 10baseT\n";
459 static void version(void)
461 fprintf(stderr, "%s\n%s\n", Version, RELEASE);
466 int main(int argc, char **argv)
468 int i, c, ret, errflag = 0;
469 unsigned ctrl1000 = 0;
471 while ((c = getopt_long(argc, argv, "A:F:p:lrRvVw?", longopts, 0)) != EOF)
473 case 'A': nway_advertise = parse_media(optarg, &ctrl1000); break;
474 case 'F': fixed_speed = parse_media(optarg, &ctrl1000); break;
475 case 'p': override_phy = atoi(optarg); break;
476 case 'r': opt_restart++; break;
477 case 'R': opt_reset++; break;
478 case 'v': verbose++; break;
479 case 'V': opt_version++; break;
480 case 'w': opt_watch++; break;
481 case 'l': opt_log++; break;
484 /* Check for a few inappropriate option combinations */
485 if (opt_watch) verbose = 0;
487 if ((nway_advertise < 0) || (fixed_speed < 0))
490 if (errflag || (fixed_speed & (fixed_speed-1)) ||
491 (fixed_speed && (opt_restart || nway_advertise))) {
492 fprintf(stderr, usage, argv[0]);
499 /* Open a basic socket. */
500 if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
506 printf("Using SIOCGMIIPHY=0x%x\n", SIOCGMIIPHY);
508 /* No remaining args means show all interfaces. */
509 if (optind == argc) {
510 fprintf(stderr, "No interface specified\n");
511 fprintf(stderr, usage, argv[0]);
516 for (i = optind; i < argc; i++) {
517 ret |= do_one_xcvr(skfd, argv[i], 0);
521 if (opt_watch && (ret == 0)) {
524 for (i = optind; i < argc; i++)
525 watch_one_xcvr(skfd, argv[i], i-optind);