Use sys/xattr.h instead of attr/xattr.h
[platform/upstream/net-tools.git] / mii-tool.c
1 /*
2
3     mii-tool: monitor and control the MII for a network interface
4
5     Usage:
6
7         mii-tool [-VvRrw] [-A media,... | -F media] [interface ...]
8
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
11     harder to use.
12
13     Copyright (C) 2000 David A. Hinds -- dhinds@pcmcia.sourceforge.org
14
15     mii-diag is written/copyright 1997-2000 by Donald Becker
16         <becker@scyld.com>
17
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.
21
22     Donald Becker may be reached as becker@scyld.com, or C/O
23     Scyld Computing Corporation, 410 Severn Av., Suite 210,
24     Annapolis, MD 21403
25
26     References
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
30 */
31
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)";
33
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <getopt.h>
42 #include <time.h>
43 #include <syslog.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <net/if.h>
48 #include <linux/sockios.h>
49
50 #ifndef __GLIBC__
51 #include <linux/if_arp.h>
52 #include <linux/if_ether.h>
53 #endif
54 #include <linux/mii.h>
55 #include <linux/sockios.h>
56 #include "version.h"
57 #include "net-support.h"
58
59 #define MAX_ETH         8               /* Maximum # of interfaces */
60 #define LPA_ABILITY_MASK        0x07e0
61
62 /* Table of known MII's */
63 static const struct {
64     u_short     id1, id2;
65     char        *name;
66 } mii_id[] = {
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" },
85 };
86 #define NMII (sizeof(mii_id)/sizeof(mii_id[0]))
87
88 /*--------------------------------------------------------------------*/
89
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 */
102     { 0, 0, 0, 0 }
103 };
104
105 static unsigned int
106     verbose = 0,
107     opt_version = 0,
108     opt_restart = 0,
109     opt_reset = 0,
110     opt_log = 0,
111     opt_watch = 0;
112 static int nway_advertise = 0;
113 static int fixed_speed = 0;
114 static int override_phy = -1;
115
116 static int skfd = -1;           /* AF_INET socket for ioctl() calls. */
117 static struct ifreq ifr;
118
119 /*--------------------------------------------------------------------*/
120
121 static int mdio_read(int skfd, int location)
122 {
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,
127                 strerror(errno));
128         return -1;
129     }
130     return mii->val_out;
131 }
132
133 static void mdio_write(int skfd, int location, int value)
134 {
135     struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
136     mii->reg_num = location;
137     mii->val_in = value;
138     if (ioctl(skfd, SIOCSMIIREG, &ifr) < 0) {
139         fprintf(stderr, "SIOCSMIIREG on %s failed: %s\n", ifr.ifr_name,
140                 strerror(errno));
141     }
142 }
143
144 /*--------------------------------------------------------------------*/
145
146 const struct {
147     char        *name;
148     u_short     value[2];
149 } media[] = {
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} },
158
159     { "1000baseT-HD",   {0, ADVERTISE_1000HALF} },
160     { "1000baseT-FD",   {0, ADVERTISE_1000FULL} },
161     { "1000baseT",      {0, ADVERTISE_1000HALF|ADVERTISE_1000FULL} },
162 };
163 #define NMEDIA (sizeof(media)/sizeof(media[0]))
164         
165 /* Parse an argument list of media types */
166 static int parse_media(char *arg, unsigned *bmcr2)
167 {
168     int mask, i;
169     char *s;
170     mask = strtoul(arg, &s, 16);
171     if ((*arg != '\0') && (*s == '\0')) {
172         if ((mask & LPA_ABILITY_MASK) &&
173             !(mask & ~LPA_ABILITY_MASK)) {
174                 *bmcr2 = 0;
175                 return mask;
176         }
177         goto failed;
178     }
179     mask = 0;
180     *bmcr2 = 0;
181     s = strtok(arg, ", ");
182     do {
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);
189
190     return mask;
191 failed:
192     fprintf(stderr, "Invalid media specification '%s'.\n", arg);
193     return -1;
194 }
195
196 /*--------------------------------------------------------------------*/
197
198 static const char *media_list(unsigned mask, unsigned mask2, int best)
199 {
200     static char buf[100];
201     int i;
202     *buf = '\0';
203
204     if (mask & BMCR_SPEED1000) {
205         if (mask2 & ADVERTISE_1000HALF) {
206             strcat(buf, " ");
207             strcat(buf, "1000baseT-HD");
208             if (best) goto out;
209         }
210         if (mask2 & ADVERTISE_1000FULL) {
211             strcat(buf, " ");
212             strcat(buf, "1000baseT-FD");
213             if (best) goto out;
214         }
215     }
216
217     mask >>= 5;
218     for (i = 4; i >= 0; i--) {
219         if (mask & (1<<i)) {
220             strcat(buf, " ");
221             strcat(buf, media[i].name);
222             if (best) break;
223         }
224     }
225  out:
226     if (mask & (1<<5))
227         strcat(buf, " flow-control");
228     return buf;
229 }
230
231 int show_basic_mii(int sock, int phy_id)
232 {
233     char buf[100];
234     int i, mii_val[32];
235     unsigned bmcr, bmsr, advert, lkpar, bmcr2, lpa2;
236
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++)
241         switch (i & 0x1F) {
242             case MII_BMCR:
243             case MII_BMSR:
244             case MII_PHYSID1:
245             case MII_PHYSID2:
246             case MII_ADVERTISE:
247             case MII_LPA:
248             case MII_EXPANSION:
249             case MII_CTRL1000:
250             case MII_STAT1000:
251             case MII_ESTATUS:
252             case MII_DCOUNTER:
253             case MII_FCSCOUNTER:
254             case MII_NWAYTEST:
255             case MII_RERRCOUNTER:
256             case MII_SREVISION:
257             case MII_RESV1:
258             case MII_LBRERROR:
259             case MII_PHYADDR:
260             case MII_RESV2:
261             case MII_TPISTATUS:
262             case MII_NCONFIG:
263                 mii_val[i] = mdio_read(sock, i);
264                 break;
265             default:
266                 mii_val[i] = 0;
267                 break;
268         }
269
270     if (mii_val[MII_BMCR] == 0xffff  || mii_val[MII_BMSR] == 0x0000) {
271         fprintf(stderr, "  No MII transceiver present!.\n");
272         return -1;
273     }
274
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];
279
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));
287                 strcat(buf, ", ");
288             } else {
289                 strcat(buf, "autonegotiation failed, ");
290             }
291         } else if (bmcr & BMCR_ANRESTART) {
292             strcat(buf, "autonegotiation restarted, ");
293         }
294     } else {
295         sprintf(buf+strlen(buf), "%s Mbit, %s duplex, ",
296                 ((bmcr2 & (ADVERTISE_1000HALF | ADVERTISE_1000FULL)) & lpa2 >> 2)
297                 ? "1000"
298                 : (bmcr & BMCR_SPEED100) ? "100" : "10",
299                 (bmcr & BMCR_FULLDPLX) ? "full" : "half");
300     }
301     strcat(buf, (bmsr & BMSR_LSTATUS) ? "link ok" : "no link");
302
303     if (opt_watch) {
304         if (opt_log) {
305             syslog(LOG_INFO, "%s", buf);
306         } else {
307             char s[20];
308             time_t t = time(NULL);
309             strftime(s, sizeof(s), "%T", localtime(&t));
310             printf("%s %s\n", s, buf);
311         }
312     } else {
313         printf("%s\n", buf);
314     }
315
316     if (verbose > 1) {
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]);
320         printf("\n");
321     }
322
323     if (verbose) {
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)))
328                 break;
329         if (i < NMII)
330             printf("%s rev %d\n", mii_id[i].name, mii_val[3]&0x0f);
331         else
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)
342             printf("isolate, ");
343         if (bmcr & BMCR_CTST)
344             printf("collision test, ");
345         if (bmcr & BMCR_ANENABLE) {
346             printf("autonegotiation enabled\n");
347         } else {
348             printf("%s Mbit, %s duplex\n",
349                    (bmcr & BMCR_SPEED100) ? "100" : "10",
350                    (bmcr & BMCR_FULLDPLX) ? "full" : "half");
351         }
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));
364         printf("\n");
365     }
366     fflush(stdout);
367     return 0;
368 }
369
370 /*--------------------------------------------------------------------*/
371
372 static int do_one_xcvr(int skfd, char *ifname, int maybe)
373 {
374     struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
375
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));
382         return 1;
383     }
384
385     if (override_phy >= 0) {
386         printf("using the specified MII index %d.\n", override_phy);
387         mii->phy_id = override_phy;
388     }
389
390     if (opt_reset) {
391         printf("resetting the transceiver...\n");
392         mdio_write(skfd, MII_BMCR, BMCR_RESET);
393     }
394     if (nway_advertise > 0) {
395         mdio_write(skfd, MII_ADVERTISE, nway_advertise | 1);
396         opt_restart = 1;
397     }
398     if (opt_restart) {
399         printf("restarting autonegotiation...\n");
400         mdio_write(skfd, MII_BMCR, 0x0000);
401         mdio_write(skfd, MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART);
402     }
403     if (fixed_speed) {
404         int bmcr = 0;
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);
410     }
411
412     if (!opt_restart && !opt_reset && !fixed_speed && !nway_advertise)
413         show_basic_mii(skfd, mii->phy_id);
414
415     return 0;
416 }
417
418 /*--------------------------------------------------------------------*/
419
420 static void watch_one_xcvr(int skfd, char *ifname, int index)
421 {
422     struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
423     static int status[MAX_ETH] = { 0, /* ... */ };
424     int now;
425
426     /* Get the vitals from the interface. */
427     strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
428     if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
429         if (errno != ENODEV)
430             fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
431                     ifname, strerror(errno));
432         return;
433     }
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);
438     status[index] = now;
439 }
440
441 /*--------------------------------------------------------------------*/
442
443 const char *usage =
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";
457
458
459 static void version(void)
460 {
461     fprintf(stderr, "%s\n%s\n", Version, RELEASE);
462     exit(E_VERSION);
463 }
464
465
466 int main(int argc, char **argv)
467 {
468     int i, c, ret, errflag = 0;
469     unsigned ctrl1000 = 0;
470     
471     while ((c = getopt_long(argc, argv, "A:F:p:lrRvVw?", longopts, 0)) != EOF)
472         switch (c) {
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;
482         case '?': errflag++;
483         }
484     /* Check for a few inappropriate option combinations */
485     if (opt_watch) verbose = 0;
486
487     if ((nway_advertise < 0) || (fixed_speed < 0))
488         return 2;
489
490     if (errflag || (fixed_speed & (fixed_speed-1)) ||
491         (fixed_speed && (opt_restart || nway_advertise))) {
492         fprintf(stderr, usage, argv[0]);
493         return 2;
494     }
495
496     if (opt_version)
497         version();
498
499     /* Open a basic socket. */
500     if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
501         perror("socket");
502         exit(-1);
503     }
504
505     if (verbose > 1)
506         printf("Using SIOCGMIIPHY=0x%x\n", SIOCGMIIPHY);        
507
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]);
512         close(skfd);
513         return 2;
514     } else {
515         ret = 0;
516         for (i = optind; i < argc; i++) {
517             ret |= do_one_xcvr(skfd, argv[i], 0);
518         }
519     }
520
521     if (opt_watch && (ret == 0)) {
522         while (1) {
523             sleep(1);
524             for (i = optind; i < argc; i++)
525                     watch_one_xcvr(skfd, argv[i], i-optind);
526         }
527     }
528
529     close(skfd);
530     return ret;
531 }