iPXE
mii.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <ipxe/mii.h>
30 
31 /** @file
32  *
33  * Media Independent Interface
34  *
35  */
36 
37 /**
38  * Restart autonegotiation
39  *
40  * @v mii MII device
41  * @ret rc Return status code
42  */
43 int mii_restart ( struct mii_device *mii ) {
44  int bmcr;
45  int rc;
46 
47  /* Read BMCR */
48  bmcr = mii_read ( mii, MII_BMCR );
49  if ( bmcr < 0 ) {
50  rc = bmcr;
51  DBGC ( mii, "MII %p could not read BMCR: %s\n",
52  mii, strerror ( rc ) );
53  return rc;
54  }
55 
56  /* Enable and restart autonegotiation */
57  bmcr |= ( BMCR_ANENABLE | BMCR_ANRESTART );
58  if ( ( rc = mii_write ( mii, MII_BMCR, bmcr ) ) != 0 ) {
59  DBGC ( mii, "MII %p could not write BMCR: %s\n",
60  mii, strerror ( rc ) );
61  return rc;
62  }
63 
64  DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
65  return 0;
66 }
67 
68 /**
69  * Reset MII device
70  *
71  * @v mii MII device
72  * @ret rc Return status code
73  */
74 int mii_reset ( struct mii_device *mii ) {
75  unsigned int i;
76  int bmcr;
77  int rc;
78 
79  /* Power-up, enable autonegotiation and initiate reset */
80  if ( ( rc = mii_write ( mii, MII_BMCR,
81  ( BMCR_RESET | BMCR_ANENABLE ) ) ) != 0 ) {
82  DBGC ( mii, "MII %p could not write BMCR: %s\n",
83  mii, strerror ( rc ) );
84  return rc;
85  }
86 
87  /* Wait for reset to complete */
88  for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {
89 
90  /* Check if reset has completed */
91  bmcr = mii_read ( mii, MII_BMCR );
92  if ( bmcr < 0 ) {
93  rc = bmcr;
94  DBGC ( mii, "MII %p could not read BMCR: %s\n",
95  mii, strerror ( rc ) );
96  return rc;
97  }
98 
99  /* If reset is not complete, delay 1ms and retry */
100  if ( bmcr & BMCR_RESET ) {
101  mdelay ( 1 );
102  continue;
103  }
104 
105  /* Force autonegotation on again, in case it was
106  * cleared by the reset.
107  */
108  if ( ( rc = mii_restart ( mii ) ) != 0 )
109  return rc;
110 
111  DBGC ( mii, "MII %p reset after %dms\n", mii, i );
112  return 0;
113  }
114 
115  DBGC ( mii, "MII %p timed out waiting for reset\n", mii );
116  return -ETIMEDOUT;
117 }
118 
119 /**
120  * Update link status via MII
121  *
122  * @v mii MII device
123  * @v netdev Network device
124  * @ret rc Return status code
125  */
126 int mii_check_link ( struct mii_device *mii, struct net_device *netdev ) {
127  int bmsr;
128  int link;
129  int rc;
130 
131  /* Read BMSR */
132  bmsr = mii_read ( mii, MII_BMSR );
133  if ( bmsr < 0 ) {
134  rc = bmsr;
135  return rc;
136  }
137 
138  /* Report link status */
139  link = ( bmsr & BMSR_LSTATUS );
140  DBGC ( mii, "MII %p link %s (BMSR %#04x)\n",
141  mii, ( link ? "up" : "down" ), bmsr );
142  if ( link ) {
144  } else {
146  }
147 
148  return 0;
149 }
150 
151 /**
152  * Find PHY address
153  *
154  * @v mii MII device
155  * @ret rc Return status code
156  */
157 int mii_find ( struct mii_device *mii ) {
158  unsigned int address;
159  int id;
160 
161  /* Try all possible PHY addresses */
162  for ( address = 0 ; address <= MII_MAX_PHY_ADDRESS ; address++ ) {
163  mii->address = address;
164  id = mii_read ( mii, MII_PHYSID1 );
165  if ( ( id > 0x0000 ) && ( id < 0xffff ) ) {
166  DBGC ( mii, "MII %p found PHY at address %d\n",
167  mii, address );
168  return 0;
169  }
170  }
171 
172  DBGC ( mii, "MII %p failed to find an address\n", mii );
173  return -ENOENT;
174 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct mii_phy mii
Error codes.
uint64_t address
Base address.
Definition: ena.h:24
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define BMCR_ANRESTART
Definition: mii.h:46
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:230
int mii_restart(struct mii_device *mii)
Restart autonegotiation.
Definition: mii.c:43
#define MII_RESET_MAX_WAIT_MS
Maximum time to wait for a reset, in milliseconds.
Definition: mii.h:142
An MII device.
Definition: mii.h:49
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:774
static struct net_device * netdev
Definition: gdbudp.c:52
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:68
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define BMSR_LSTATUS
Definition: mii.h:57
uint8_t id
Request identifier.
Definition: ena.h:12
A network device.
Definition: netdevice.h:352
Media Independent Interface.
#define MII_BMCR
Definition: atl1e.h:871
int mii_reset(struct mii_device *mii)
Reset MII device.
Definition: mii.c:74
int mii_check_link(struct mii_device *mii, struct net_device *netdev)
Update link status via MII.
Definition: mii.c:126
int mii_find(struct mii_device *mii)
Find PHY address.
Definition: mii.c:157
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define BMCR_RESET
Definition: mii.h:52
#define MII_MAX_PHY_ADDRESS
Maximum PHY address.
Definition: mii.h:145
static int mii_read(int phy_id, int location)
Definition: epic100.c:499
#define BMCR_ANENABLE
Definition: mii.h:49
#define MII_BMSR
Definition: atl1e.h:872
#define MII_PHYSID1
Definition: atl1e.h:873
static int mii_write(struct mii_device *mii, unsigned int reg, unsigned int data)
Write to MII register.
Definition: mii.h:104
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.