1/*-
2 * Copyright 2009 Solarflare Communications Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD$");
28
29#include "efsys.h"
30#include "efx.h"
31#include "efx_types.h"
32#include "efx_impl.h"
33
34	__checkReturn	int
35efx_port_init(
36	__in		efx_nic_t *enp)
37{
38	efx_port_t *epp = &(enp->en_port);
39	efx_phy_ops_t *epop = epp->ep_epop;
40	int rc;
41
42	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
43	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
44	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
45
46	if (enp->en_mod_flags & EFX_MOD_PORT) {
47		rc = EINVAL;
48		goto fail1;
49	}
50
51	enp->en_mod_flags |= EFX_MOD_PORT;
52
53	epp->ep_mac_type = EFX_MAC_INVALID;
54	epp->ep_link_mode = EFX_LINK_UNKNOWN;
55	epp->ep_mac_poll_needed = B_TRUE;
56	epp->ep_mac_drain = B_TRUE;
57
58	/* Configure the MAC */
59	if ((rc = efx_mac_select(enp)) != 0)
60		goto fail1;
61
62	epp->ep_emop->emo_reconfigure(enp);
63
64	/*
65	 * Turn on the PHY if available, otherwise reset it, and
66	 * reconfigure it with the current configuration.
67	 */
68	if (epop->epo_power != NULL) {
69		if ((rc = epop->epo_power(enp, B_TRUE)) != 0)
70			goto fail2;
71	} else {
72		if ((rc = epop->epo_reset(enp)) != 0)
73			goto fail2;
74	}
75
76	EFSYS_ASSERT(enp->en_reset_flags & EFX_RESET_PHY);
77	enp->en_reset_flags &= ~EFX_RESET_PHY;
78
79	if ((rc = epop->epo_reconfigure(enp)) != 0)
80		goto fail3;
81
82	return (0);
83
84fail3:
85	EFSYS_PROBE(fail3);
86fail2:
87	EFSYS_PROBE(fail2);
88fail1:
89	EFSYS_PROBE1(fail1, int, rc);
90
91	enp->en_mod_flags &= ~EFX_MOD_PORT;
92
93	return (rc);
94}
95
96	__checkReturn	int
97efx_port_poll(
98	__in		efx_nic_t *enp,
99	__out		efx_link_mode_t	*link_modep)
100{
101	efx_port_t *epp = &(enp->en_port);
102	efx_mac_ops_t *emop = epp->ep_emop;
103	efx_link_mode_t ignore_link_mode;
104	int rc;
105
106	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
107	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
108
109	EFSYS_ASSERT(emop != NULL);
110	EFSYS_ASSERT(!epp->ep_mac_stats_pending);
111
112	if (link_modep == NULL)
113		link_modep = &ignore_link_mode;
114
115	if ((rc = emop->emo_poll(enp, link_modep)) != 0)
116		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