efx_port.c revision 228078
119080Sache/*-
219080Sache * Copyright 2009 Solarflare Communications Inc.  All rights reserved.
319080Sache *
419080Sache * Redistribution and use in source and binary forms, with or without
519080Sache * modification, are permitted provided that the following conditions
619080Sache * are met:
719080Sache * 1. Redistributions of source code must retain the above copyright
819080Sache *    notice, this list of conditions and the following disclaimer.
932822Syokota * 2. Redistributions in binary form must reproduce the above copyright
1019080Sache *    notice, this list of conditions and the following disclaimer in the
1119080Sache *    documentation and/or other materials provided with the distribution.
1219080Sache *
1319080Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND
1419080Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1519080Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1619080Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1719080Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1819080Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1919080Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2019080Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2119080Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2219080Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2319080Sache * SUCH DAMAGE.
2419080Sache */
2519080Sache
2619080Sache#include <sys/cdefs.h>
2719080Sache__FBSDID("$FreeBSD: head/sys/dev/sfxge/common/efx_port.c 228078 2011-11-28 17:19:05Z philip $");
2819080Sache
2919080Sache#include "efsys.h"
3019080Sache#include "efx.h"
3119080Sache#include "efx_types.h"
3219080Sache#include "efx_impl.h"
3319080Sache
3419080Sache	__checkReturn	int
3519080Sacheefx_port_init(
3619080Sache	__in		efx_nic_t *enp)
3719080Sache{
3819080Sache	efx_port_t *epp = &(enp->en_port);
3919080Sache	efx_phy_ops_t *epop = epp->ep_epop;
4019080Sache	int rc;
4119080Sache
4219080Sache	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
4319080Sache	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
4419080Sache	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
4519080Sache
4619080Sache	if (enp->en_mod_flags & EFX_MOD_PORT) {
4719080Sache		rc = EINVAL;
4819080Sache		goto fail1;
4919080Sache	}
5019080Sache
5119080Sache	enp->en_mod_flags |= EFX_MOD_PORT;
5219080Sache
5319080Sache	epp->ep_mac_type = EFX_MAC_INVALID;
5419080Sache	epp->ep_link_mode = EFX_LINK_UNKNOWN;
5519080Sache	epp->ep_mac_poll_needed = B_TRUE;
5619080Sache	epp->ep_mac_drain = B_TRUE;
5719080Sache
5819080Sache	/* Configure the MAC */
5919080Sache	if ((rc = efx_mac_select(enp)) != 0)
6019080Sache		goto fail1;
6138140Syokota
6219080Sache	epp->ep_emop->emo_reconfigure(enp);
6343334Syokota
6419080Sache	/*
6532822Syokota	 * Turn on the PHY if available, otherwise reset it, and
6619080Sache	 * reconfigure it with the current configuration.
6719080Sache	 */
6819080Sache	if (epop->epo_power != NULL) {
6919080Sache		if ((rc = epop->epo_power(enp, B_TRUE)) != 0)
7019080Sache			goto fail2;
7119080Sache	} else {
7219080Sache		if ((rc = epop->epo_reset(enp)) != 0)
7319080Sache			goto fail2;
7419080Sache	}
7519080Sache
7619080Sache	EFSYS_ASSERT(enp->en_reset_flags & EFX_RESET_PHY);
7743334Syokota	enp->en_reset_flags &= ~EFX_RESET_PHY;
7843334Syokota
7919080Sache	if ((rc = epop->epo_reconfigure(enp)) != 0)
8019080Sache		goto fail3;
8119080Sache
8219080Sache	return (0);
8319080Sache
8419080Sachefail3:
8519080Sache	EFSYS_PROBE(fail3);
8619080Sachefail2:
8719080Sache	EFSYS_PROBE(fail2);
8819080Sachefail1:
8919080Sache	EFSYS_PROBE1(fail1, int, rc);
9019080Sache
9119080Sache	enp->en_mod_flags &= ~EFX_MOD_PORT;
9243334Syokota
9319080Sache	return (rc);
9419080Sache}
9519080Sache
9619080Sache	__checkReturn	int
9719080Sacheefx_port_poll(
9819080Sache	__in		efx_nic_t *enp,
9919080Sache	__out		efx_link_mode_t	*link_modep)
10043334Syokota{
10119080Sache	efx_port_t *epp = &(enp->en_port);
10219080Sache	efx_mac_ops_t *emop = epp->ep_emop;
10319080Sache	efx_link_mode_t ignore_link_mode;
10419080Sache	int rc;
10519080Sache
10619080Sache	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
10719080Sache	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
10819080Sache
10919080Sache	EFSYS_ASSERT(emop != NULL);
11019080Sache	EFSYS_ASSERT(!epp->ep_mac_stats_pending);
11119080Sache
11243334Syokota	if (link_modep == NULL)
11319080Sache		link_modep = &ignore_link_mode;
11419080Sache
11519080Sache	if ((rc = emop->emo_poll(enp, link_modep)) != 0)
11643334Syokota		goto fail1;
117
118	return (0);
119
120fail1:
121	EFSYS_PROBE1(fail1, int, rc);
122
123	return (rc);
124}
125
126#if EFSYS_OPT_LOOPBACK
127
128	__checkReturn	int
129efx_port_loopback_set(
130	__in		efx_nic_t *enp,
131	__in		efx_link_mode_t link_mode,
132	__in		efx_loopback_type_t loopback_type)
133{
134	efx_port_t *epp = &(enp->en_port);
135	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
136	efx_mac_ops_t *emop = epp->ep_emop;
137	int rc;
138
139	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
140	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
141	EFSYS_ASSERT(emop != NULL);
142
143	EFSYS_ASSERT(link_mode < EFX_LINK_NMODES);
144	if ((1 << loopback_type) & ~encp->enc_loopback_types[link_mode]) {
145		rc = ENOTSUP;
146		goto fail1;
147	}
148
149	if (epp->ep_loopback_type == loopback_type &&
150	    epp->ep_loopback_link_mode == link_mode)
151		return (0);
152
153	if ((rc = emop->emo_loopback_set(enp, link_mode, loopback_type)) != 0)
154		goto fail2;
155
156	return (0);
157
158fail2:
159	EFSYS_PROBE(fail2);
160fail1:
161	EFSYS_PROBE1(fail1, int, rc);
162
163	return (rc);
164}
165
166#if EFSYS_OPT_NAMES
167
168static const char 	__cs * __cs __efx_loopback_type_name[] = {
169	"OFF",
170	"DATA",
171	"GMAC",
172	"XGMII",
173	"XGXS",
174	"XAUI",
175	"GMII",
176	"SGMII",
177	"XGBR",
178	"XFI",
179	"XAUI_FAR",
180	"GMII_FAR",
181	"SGMII_FAR",
182	"XFI_FAR",
183	"GPHY",
184	"PHY_XS",
185	"PCS",
186	"PMA_PMD",
187};
188
189	__checkReturn	const char __cs *
190efx_loopback_type_name(
191	__in		efx_nic_t *enp,
192	__in		efx_loopback_type_t type)
193{
194	_NOTE(ARGUNUSED(enp))
195	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
196	EFSYS_ASSERT3U(type, <, EFX_LOOPBACK_NTYPES);
197
198	return (__efx_loopback_type_name[type]);
199}
200
201#endif	/* EFSYS_OPT_NAMES */
202
203#endif	/* EFSYS_OPT_LOOPBACK */
204
205			void
206efx_port_fini(
207	__in		efx_nic_t *enp)
208{
209	efx_port_t *epp = &(enp->en_port);
210	efx_phy_ops_t *epop = epp->ep_epop;
211
212	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
213	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
214	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
215	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
216
217	EFSYS_ASSERT(epp->ep_mac_drain);
218
219	epp->ep_emop = NULL;
220	epp->ep_mac_type = EFX_MAC_INVALID;
221	epp->ep_mac_drain = B_FALSE;
222	epp->ep_mac_poll_needed = B_FALSE;
223
224	/* Turn off the PHY */
225	if (epop->epo_power != NULL)
226		(void) epop->epo_power(enp, B_FALSE);
227
228	enp->en_mod_flags &= ~EFX_MOD_PORT;
229}
230