sfxge_port.c revision 229613
1227569Sphilip/*-
2227569Sphilip * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3227569Sphilip * All rights reserved.
4227569Sphilip *
5227569Sphilip * This software was developed in part by Philip Paeps under contract for
6227569Sphilip * Solarflare Communications, Inc.
7227569Sphilip *
8227569Sphilip * Redistribution and use in source and binary forms, with or without
9227569Sphilip * modification, are permitted provided that the following conditions
10227569Sphilip * are met:
11227569Sphilip * 1. Redistributions of source code must retain the above copyright
12227569Sphilip *    notice, this list of conditions and the following disclaimer.
13227569Sphilip * 2. Redistributions in binary form must reproduce the above copyright
14227569Sphilip *    notice, this list of conditions and the following disclaimer in the
15227569Sphilip *    documentation and/or other materials provided with the distribution.
16227569Sphilip *
17227569Sphilip * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18227569Sphilip * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19227569Sphilip * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20227569Sphilip * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21227569Sphilip * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22227569Sphilip * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23227569Sphilip * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24227569Sphilip * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25227569Sphilip * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26227569Sphilip * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27227569Sphilip * SUCH DAMAGE.
28227569Sphilip */
29227569Sphilip
30227569Sphilip#include <sys/cdefs.h>
31227569Sphilip__FBSDID("$FreeBSD: head/sys/dev/sfxge/sfxge_port.c 229613 2012-01-05 18:32:37Z jhb $");
32227569Sphilip
33227569Sphilip#include <sys/types.h>
34227699Sphilip#include <sys/limits.h>
35227569Sphilip#include <net/ethernet.h>
36227569Sphilip#include <net/if_dl.h>
37227569Sphilip
38227569Sphilip#include "common/efx.h"
39227569Sphilip
40227569Sphilip#include "sfxge.h"
41227569Sphilip
42227569Sphilipstatic int
43227569Sphilipsfxge_mac_stat_update(struct sfxge_softc *sc)
44227569Sphilip{
45227569Sphilip	struct sfxge_port *port = &sc->port;
46227569Sphilip	efsys_mem_t *esmp = &(port->mac_stats.dma_buf);
47227569Sphilip	clock_t now;
48227569Sphilip	unsigned int count;
49227569Sphilip	int rc;
50227569Sphilip
51227569Sphilip	mtx_lock(&port->lock);
52227569Sphilip
53227569Sphilip	if (port->init_state != SFXGE_PORT_STARTED) {
54227569Sphilip		rc = 0;
55227569Sphilip		goto out;
56227569Sphilip	}
57227569Sphilip
58227569Sphilip	now = ticks;
59227569Sphilip	if (now - port->mac_stats.update_time < hz) {
60227569Sphilip		rc = 0;
61227569Sphilip		goto out;
62227569Sphilip	}
63227569Sphilip
64227569Sphilip	port->mac_stats.update_time = now;
65227569Sphilip
66227569Sphilip	/* If we're unlucky enough to read statistics wduring the DMA, wait
67227569Sphilip	 * up to 10ms for it to finish (typically takes <500us) */
68227569Sphilip	for (count = 0; count < 100; ++count) {
69227569Sphilip		EFSYS_PROBE1(wait, unsigned int, count);
70227569Sphilip
71227569Sphilip		/* Synchronize the DMA memory for reading */
72227569Sphilip		bus_dmamap_sync(esmp->esm_tag, esmp->esm_map,
73227569Sphilip		    BUS_DMASYNC_POSTREAD);
74227569Sphilip
75227569Sphilip		/* Try to update the cached counters */
76227569Sphilip		if ((rc = efx_mac_stats_update(sc->enp, esmp,
77227569Sphilip                    port->mac_stats.decode_buf, NULL)) != EAGAIN)
78227569Sphilip			goto out;
79227569Sphilip
80227569Sphilip		DELAY(100);
81227569Sphilip	}
82227569Sphilip
83227569Sphilip	rc = ETIMEDOUT;
84227569Sphilipout:
85227569Sphilip	mtx_unlock(&port->lock);
86227569Sphilip	return rc;
87227569Sphilip}
88227569Sphilip
89227569Sphilipstatic int
90227569Sphilipsfxge_mac_stat_handler(SYSCTL_HANDLER_ARGS)
91227569Sphilip{
92227569Sphilip	struct sfxge_softc *sc = arg1;
93227569Sphilip	unsigned int id = arg2;
94227569Sphilip	int rc;
95227569Sphilip
96227569Sphilip	if ((rc = sfxge_mac_stat_update(sc)) != 0)
97227569Sphilip		return rc;
98227569Sphilip
99227569Sphilip	return SYSCTL_OUT(req,
100227569Sphilip			  (uint64_t *)sc->port.mac_stats.decode_buf + id,
101227569Sphilip			  sizeof(uint64_t));
102227569Sphilip}
103227569Sphilip
104227569Sphilipstatic void
105227569Sphilipsfxge_mac_stat_init(struct sfxge_softc *sc)
106227569Sphilip{
107227569Sphilip	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
108227569Sphilip	struct sysctl_oid_list *stat_list;
109227569Sphilip	unsigned int id;
110227569Sphilip	const char *name;
111227569Sphilip
112227569Sphilip	stat_list = SYSCTL_CHILDREN(sc->stats_node);
113227569Sphilip
114227569Sphilip	/* Initialise the named stats */
115227569Sphilip	for (id = 0; id < EFX_MAC_NSTATS; id++) {
116227569Sphilip		name = efx_mac_stat_name(sc->enp, id);
117227569Sphilip		SYSCTL_ADD_PROC(
118227569Sphilip			ctx, stat_list,
119227569Sphilip			OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
120227569Sphilip			sc, id, sfxge_mac_stat_handler, "Q",
121227569Sphilip			"");
122227569Sphilip	}
123227569Sphilip}
124227569Sphilip
125227569Sphilip#ifdef SFXGE_HAVE_PAUSE_MEDIAOPTS
126227569Sphilip
127227569Sphilipstatic unsigned int
128227569Sphilipsfxge_port_wanted_fc(struct sfxge_softc *sc)
129227569Sphilip{
130227569Sphilip	struct ifmedia_entry *ifm = sc->media.ifm_cur;
131227569Sphilip
132227569Sphilip	if (ifm->ifm_media == (IFM_ETHER | IFM_AUTO))
133227569Sphilip		return EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
134227569Sphilip	return ((ifm->ifm_media & IFM_ETH_RXPAUSE) ? EFX_FCNTL_RESPOND : 0) |
135227569Sphilip		((ifm->ifm_media & IFM_ETH_TXPAUSE) ? EFX_FCNTL_GENERATE : 0);
136227569Sphilip}
137227569Sphilip
138227569Sphilipstatic unsigned int
139227569Sphilipsfxge_port_link_fc_ifm(struct sfxge_softc *sc)
140227569Sphilip{
141227569Sphilip	unsigned int wanted_fc, link_fc;
142227569Sphilip
143227569Sphilip	efx_mac_fcntl_get(sc->enp, &wanted_fc, &link_fc);
144227569Sphilip	return ((link_fc & EFX_FCNTL_RESPOND) ? IFM_ETH_RXPAUSE : 0) |
145227569Sphilip		((link_fc & EFX_FCNTL_GENERATE) ? IFM_ETH_TXPAUSE : 0);
146227569Sphilip}
147227569Sphilip
148227569Sphilip#else /* !SFXGE_HAVE_PAUSE_MEDIAOPTS */
149227569Sphilip
150227569Sphilipstatic unsigned int
151227569Sphilipsfxge_port_wanted_fc(struct sfxge_softc *sc)
152227569Sphilip{
153227569Sphilip	return sc->port.wanted_fc;
154227569Sphilip}
155227569Sphilip
156227569Sphilipstatic unsigned int
157227569Sphilipsfxge_port_link_fc_ifm(struct sfxge_softc *sc)
158227569Sphilip{
159227569Sphilip	return 0;
160227569Sphilip}
161227569Sphilip
162227569Sphilipstatic int
163227569Sphilipsfxge_port_wanted_fc_handler(SYSCTL_HANDLER_ARGS)
164227569Sphilip{
165227569Sphilip	struct sfxge_softc *sc;
166227569Sphilip	struct sfxge_port *port;
167227569Sphilip	unsigned int fcntl;
168227569Sphilip	int error;
169227569Sphilip
170227569Sphilip	sc = arg1;
171227569Sphilip	port = &sc->port;
172227569Sphilip
173227569Sphilip	mtx_lock(&port->lock);
174227569Sphilip
175227569Sphilip	if (req->newptr) {
176227569Sphilip		if ((error = SYSCTL_IN(req, &fcntl, sizeof(fcntl))) != 0)
177227569Sphilip			goto out;
178227569Sphilip
179227569Sphilip		if (port->wanted_fc == fcntl)
180227569Sphilip			goto out;
181227569Sphilip
182227569Sphilip		port->wanted_fc = fcntl;
183227569Sphilip
184227569Sphilip		if (port->init_state != SFXGE_PORT_STARTED)
185227569Sphilip			goto out;
186227569Sphilip
187227569Sphilip		error = efx_mac_fcntl_set(sc->enp, port->wanted_fc, B_TRUE);
188227569Sphilip	} else {
189227569Sphilip		error = SYSCTL_OUT(req, &port->wanted_fc,
190227569Sphilip				   sizeof(port->wanted_fc));
191227569Sphilip	}
192227569Sphilip
193227569Sphilipout:
194227569Sphilip	mtx_unlock(&port->lock);
195227569Sphilip
196227569Sphilip	return (error);
197227569Sphilip}
198227569Sphilip
199227569Sphilipstatic int
200227569Sphilipsfxge_port_link_fc_handler(SYSCTL_HANDLER_ARGS)
201227569Sphilip{
202227569Sphilip	struct sfxge_softc *sc;
203227569Sphilip	struct sfxge_port *port;
204227569Sphilip	unsigned int wanted_fc, link_fc;
205227569Sphilip	int error;
206227569Sphilip
207227569Sphilip	sc = arg1;
208227569Sphilip	port = &sc->port;
209227569Sphilip
210227569Sphilip	mtx_lock(&port->lock);
211227569Sphilip	if (port->init_state == SFXGE_PORT_STARTED && SFXGE_LINK_UP(sc))
212227569Sphilip		efx_mac_fcntl_get(sc->enp, &wanted_fc, &link_fc);
213227569Sphilip	else
214227569Sphilip		link_fc = 0;
215227569Sphilip	error = SYSCTL_OUT(req, &link_fc, sizeof(link_fc));
216227569Sphilip	mtx_unlock(&port->lock);
217227569Sphilip
218227569Sphilip	return (error);
219227569Sphilip}
220227569Sphilip
221227569Sphilip#endif /* SFXGE_HAVE_PAUSE_MEDIAOPTS */
222227569Sphilip
223227699Sphilipstatic const u_long sfxge_link_baudrate[EFX_LINK_NMODES] = {
224227699Sphilip	[EFX_LINK_10HDX]	= IF_Mbps(10),
225227699Sphilip	[EFX_LINK_10FDX]	= IF_Mbps(10),
226227699Sphilip	[EFX_LINK_100HDX]	= IF_Mbps(100),
227227699Sphilip	[EFX_LINK_100FDX]	= IF_Mbps(100),
228227699Sphilip	[EFX_LINK_1000HDX]	= IF_Gbps(1),
229227699Sphilip	[EFX_LINK_1000FDX]	= IF_Gbps(1),
230227699Sphilip	[EFX_LINK_10000FDX]	= MIN(IF_Gbps(10ULL), ULONG_MAX),
231227569Sphilip};
232227569Sphilip
233227569Sphilipvoid
234227569Sphilipsfxge_mac_link_update(struct sfxge_softc *sc, efx_link_mode_t mode)
235227569Sphilip{
236227569Sphilip	struct sfxge_port *port;
237227569Sphilip	int link_state;
238227569Sphilip
239227569Sphilip	port = &sc->port;
240227569Sphilip
241227569Sphilip	if (port->link_mode == mode)
242227569Sphilip		return;
243227569Sphilip
244227569Sphilip	port->link_mode = mode;
245227569Sphilip
246227569Sphilip	/* Push link state update to the OS */
247227569Sphilip	link_state = (port->link_mode != EFX_LINK_DOWN ?
248227569Sphilip		      LINK_STATE_UP : LINK_STATE_DOWN);
249227699Sphilip	sc->ifnet->if_baudrate = sfxge_link_baudrate[port->link_mode];
250227569Sphilip	if_link_state_change(sc->ifnet, link_state);
251227569Sphilip}
252227569Sphilip
253227569Sphilipstatic void
254227569Sphilipsfxge_mac_poll_work(void *arg, int npending)
255227569Sphilip{
256227569Sphilip	struct sfxge_softc *sc;
257227569Sphilip	efx_nic_t *enp;
258227569Sphilip	struct sfxge_port *port;
259227569Sphilip	efx_link_mode_t mode;
260227569Sphilip
261227569Sphilip	sc = (struct sfxge_softc *)arg;
262227569Sphilip	enp = sc->enp;
263227569Sphilip	port = &sc->port;
264227569Sphilip
265227569Sphilip	mtx_lock(&port->lock);
266227569Sphilip
267227569Sphilip	if (port->init_state != SFXGE_PORT_STARTED)
268227569Sphilip		goto done;
269227569Sphilip
270227569Sphilip	/* This may sleep waiting for MCDI completion */
271227569Sphilip	(void)efx_port_poll(enp, &mode);
272227569Sphilip	sfxge_mac_link_update(sc, mode);
273227569Sphilip
274227569Sphilipdone:
275227569Sphilip	mtx_unlock(&port->lock);
276227569Sphilip}
277227569Sphilip
278227569Sphilipstatic int
279227569Sphilipsfxge_mac_filter_set_locked(struct sfxge_softc *sc)
280227569Sphilip{
281227569Sphilip	unsigned int bucket[EFX_MAC_HASH_BITS];
282227569Sphilip	struct ifnet *ifp = sc->ifnet;
283227569Sphilip	struct ifmultiaddr *ifma;
284227569Sphilip	struct sockaddr_dl *sa;
285227569Sphilip	efx_nic_t *enp = sc->enp;
286227569Sphilip	unsigned int index;
287227569Sphilip	int rc;
288227569Sphilip
289227569Sphilip	/* Set promisc-unicast and broadcast filter bits */
290227569Sphilip	if ((rc = efx_mac_filter_set(enp, !!(ifp->if_flags & IFF_PROMISC),
291227569Sphilip				     B_TRUE)) != 0)
292227569Sphilip		return rc;
293227569Sphilip
294227569Sphilip	/* Set multicast hash filter */
295227569Sphilip	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
296227569Sphilip		for (index = 0; index < EFX_MAC_HASH_BITS; index++)
297227569Sphilip			bucket[index] = 1;
298227569Sphilip	} else {
299227569Sphilip		/* Broadcast frames also go through the multicast
300227569Sphilip		 * filter, and the broadcast address hashes to
301227569Sphilip		 * 0xff. */
302227569Sphilip		bucket[0xff] = 1;
303227569Sphilip
304229613Sjhb		if_maddr_rlock(ifp);
305227569Sphilip		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
306227569Sphilip			if (ifma->ifma_addr->sa_family == AF_LINK) {
307227569Sphilip				sa = (struct sockaddr_dl *)ifma->ifma_addr;
308227569Sphilip				index = ether_crc32_le(LLADDR(sa), 6) & 0xff;
309227569Sphilip				bucket[index] = 1;
310227569Sphilip			}
311227569Sphilip		}
312229613Sjhb		if_maddr_runlock(ifp);
313227569Sphilip	}
314227569Sphilip	return efx_mac_hash_set(enp, bucket);
315227569Sphilip}
316227569Sphilip
317227569Sphilipint
318227569Sphilipsfxge_mac_filter_set(struct sfxge_softc *sc)
319227569Sphilip{
320227569Sphilip	struct sfxge_port *port = &sc->port;
321227569Sphilip	int rc;
322227569Sphilip
323227569Sphilip	KASSERT(port->init_state == SFXGE_PORT_STARTED, ("port not started"));
324227569Sphilip
325227569Sphilip	mtx_lock(&port->lock);
326227569Sphilip	rc = sfxge_mac_filter_set_locked(sc);
327227569Sphilip	mtx_unlock(&port->lock);
328227569Sphilip	return rc;
329227569Sphilip}
330227569Sphilip
331227569Sphilipvoid
332227569Sphilipsfxge_port_stop(struct sfxge_softc *sc)
333227569Sphilip{
334227569Sphilip	struct sfxge_port *port;
335227569Sphilip	efx_nic_t *enp;
336227569Sphilip
337227569Sphilip	port = &sc->port;
338227569Sphilip	enp = sc->enp;
339227569Sphilip
340227569Sphilip	mtx_lock(&port->lock);
341227569Sphilip
342227569Sphilip	KASSERT(port->init_state == SFXGE_PORT_STARTED,
343227569Sphilip	    ("port not started"));
344227569Sphilip
345227569Sphilip	port->init_state = SFXGE_PORT_INITIALIZED;
346227569Sphilip
347227569Sphilip	port->mac_stats.update_time = 0;
348227569Sphilip
349227569Sphilip	/* This may call MCDI */
350227569Sphilip	(void)efx_mac_drain(enp, B_TRUE);
351227569Sphilip
352227569Sphilip	(void)efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf, 0, B_FALSE);
353227569Sphilip
354227569Sphilip	port->link_mode = EFX_LINK_UNKNOWN;
355227569Sphilip
356227569Sphilip	/* Destroy the common code port object. */
357227569Sphilip	efx_port_fini(sc->enp);
358227569Sphilip
359227569Sphilip	mtx_unlock(&port->lock);
360227569Sphilip}
361227569Sphilip
362227569Sphilipint
363227569Sphilipsfxge_port_start(struct sfxge_softc *sc)
364227569Sphilip{
365227569Sphilip	uint8_t mac_addr[ETHER_ADDR_LEN];
366227569Sphilip	struct ifnet *ifp = sc->ifnet;
367227569Sphilip	struct sfxge_port *port;
368227569Sphilip	efx_nic_t *enp;
369227569Sphilip	size_t pdu;
370227569Sphilip	int rc;
371227569Sphilip
372227569Sphilip	port = &sc->port;
373227569Sphilip	enp = sc->enp;
374227569Sphilip
375227569Sphilip	mtx_lock(&port->lock);
376227569Sphilip
377227569Sphilip	KASSERT(port->init_state == SFXGE_PORT_INITIALIZED,
378227569Sphilip	    ("port not initialized"));
379227569Sphilip
380227569Sphilip	/* Initialize the port object in the common code. */
381227569Sphilip	if ((rc = efx_port_init(sc->enp)) != 0)
382227569Sphilip		goto fail;
383227569Sphilip
384227569Sphilip	/* Set the SDU */
385227569Sphilip	pdu = EFX_MAC_PDU(ifp->if_mtu);
386227569Sphilip	if ((rc = efx_mac_pdu_set(enp, pdu)) != 0)
387227569Sphilip		goto fail2;
388227569Sphilip
389227569Sphilip	if ((rc = efx_mac_fcntl_set(enp, sfxge_port_wanted_fc(sc), B_TRUE))
390227569Sphilip	    != 0)
391227569Sphilip		goto fail2;
392227569Sphilip
393227569Sphilip	/* Set the unicast address */
394229613Sjhb	if_addr_rlock(ifp);
395227569Sphilip	bcopy(LLADDR((struct sockaddr_dl *)ifp->if_addr->ifa_addr),
396227569Sphilip	      mac_addr, sizeof(mac_addr));
397229613Sjhb	if_addr_runlock(ifp);
398227569Sphilip	if ((rc = efx_mac_addr_set(enp, mac_addr)) != 0)
399227569Sphilip		goto fail;
400227569Sphilip
401227569Sphilip	sfxge_mac_filter_set_locked(sc);
402227569Sphilip
403227569Sphilip	/* Update MAC stats by DMA every second */
404227569Sphilip	if ((rc = efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf,
405227569Sphilip            1000, B_FALSE)) != 0)
406227569Sphilip		goto fail2;
407227569Sphilip
408227569Sphilip	if ((rc = efx_mac_drain(enp, B_FALSE)) != 0)
409227569Sphilip		goto fail3;
410227569Sphilip
411227569Sphilip	if ((rc = efx_phy_adv_cap_set(sc->enp, sc->media.ifm_cur->ifm_data))
412227569Sphilip	    != 0)
413227569Sphilip		goto fail4;
414227569Sphilip
415227569Sphilip	port->init_state = SFXGE_PORT_STARTED;
416227569Sphilip
417227569Sphilip	/* Single poll in case there were missing initial events */
418227569Sphilip	mtx_unlock(&port->lock);
419227569Sphilip	sfxge_mac_poll_work(sc, 0);
420227569Sphilip
421227569Sphilip	return (0);
422227569Sphilip
423227569Sphilipfail4:
424227569Sphilip	(void)efx_mac_drain(enp, B_TRUE);
425227569Sphilipfail3:
426227569Sphilip	(void)efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf,
427227569Sphilip            0, B_FALSE);
428227569Sphilipfail2:
429227569Sphilip	efx_port_fini(sc->enp);
430227569Sphilipfail:
431227569Sphilip	mtx_unlock(&port->lock);
432227569Sphilip
433227569Sphilip	return (rc);
434227569Sphilip}
435227569Sphilip
436227569Sphilipstatic int
437227569Sphilipsfxge_phy_stat_update(struct sfxge_softc *sc)
438227569Sphilip{
439227569Sphilip	struct sfxge_port *port = &sc->port;
440227569Sphilip	efsys_mem_t *esmp = &port->phy_stats.dma_buf;
441227569Sphilip	clock_t now;
442227569Sphilip	unsigned int count;
443227569Sphilip	int rc;
444227569Sphilip
445227569Sphilip	mtx_lock(&port->lock);
446227569Sphilip
447227569Sphilip	if (port->init_state != SFXGE_PORT_STARTED) {
448227569Sphilip		rc = 0;
449227569Sphilip		goto out;
450227569Sphilip	}
451227569Sphilip
452227569Sphilip	now = ticks;
453227569Sphilip	if (now - port->phy_stats.update_time < hz) {
454227569Sphilip		rc = 0;
455227569Sphilip		goto out;
456227569Sphilip	}
457227569Sphilip
458227569Sphilip	port->phy_stats.update_time = now;
459227569Sphilip
460227569Sphilip	/* If we're unlucky enough to read statistics wduring the DMA, wait
461227569Sphilip	 * up to 10ms for it to finish (typically takes <500us) */
462227569Sphilip	for (count = 0; count < 100; ++count) {
463227569Sphilip		EFSYS_PROBE1(wait, unsigned int, count);
464227569Sphilip
465227569Sphilip		/* Synchronize the DMA memory for reading */
466227569Sphilip		bus_dmamap_sync(esmp->esm_tag, esmp->esm_map,
467227569Sphilip		    BUS_DMASYNC_POSTREAD);
468227569Sphilip
469227569Sphilip		/* Try to update the cached counters */
470227569Sphilip		if ((rc = efx_phy_stats_update(sc->enp, esmp,
471227569Sphilip		    port->phy_stats.decode_buf)) != EAGAIN)
472227569Sphilip			goto out;
473227569Sphilip
474227569Sphilip		DELAY(100);
475227569Sphilip	}
476227569Sphilip
477227569Sphilip	rc = ETIMEDOUT;
478227569Sphilipout:
479227569Sphilip	mtx_unlock(&port->lock);
480227569Sphilip	return rc;
481227569Sphilip}
482227569Sphilip
483227569Sphilipstatic int
484227569Sphilipsfxge_phy_stat_handler(SYSCTL_HANDLER_ARGS)
485227569Sphilip{
486227569Sphilip	struct sfxge_softc *sc = arg1;
487227569Sphilip	unsigned int id = arg2;
488227569Sphilip	int rc;
489227569Sphilip
490227569Sphilip	if ((rc = sfxge_phy_stat_update(sc)) != 0)
491227569Sphilip		return rc;
492227569Sphilip
493227569Sphilip	return SYSCTL_OUT(req,
494227569Sphilip			  (uint32_t *)sc->port.phy_stats.decode_buf + id,
495227569Sphilip			  sizeof(uint32_t));
496227569Sphilip}
497227569Sphilip
498227569Sphilipstatic void
499227569Sphilipsfxge_phy_stat_init(struct sfxge_softc *sc)
500227569Sphilip{
501227569Sphilip	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
502227569Sphilip	struct sysctl_oid_list *stat_list;
503227569Sphilip	unsigned int id;
504227569Sphilip	const char *name;
505227569Sphilip	uint64_t stat_mask = efx_nic_cfg_get(sc->enp)->enc_phy_stat_mask;
506227569Sphilip
507227569Sphilip	stat_list = SYSCTL_CHILDREN(sc->stats_node);
508227569Sphilip
509227569Sphilip	/* Initialise the named stats */
510227569Sphilip	for (id = 0; id < EFX_PHY_NSTATS; id++) {
511227569Sphilip		if (!(stat_mask & ((uint64_t)1 << id)))
512227569Sphilip			continue;
513227569Sphilip		name = efx_phy_stat_name(sc->enp, id);
514227569Sphilip		SYSCTL_ADD_PROC(
515227569Sphilip			ctx, stat_list,
516227569Sphilip			OID_AUTO, name, CTLTYPE_UINT|CTLFLAG_RD,
517227569Sphilip			sc, id, sfxge_phy_stat_handler,
518227569Sphilip			id == EFX_PHY_STAT_OUI ? "IX" : "IU",
519227569Sphilip			"");
520227569Sphilip	}
521227569Sphilip}
522227569Sphilip
523227569Sphilipvoid
524227569Sphilipsfxge_port_fini(struct sfxge_softc *sc)
525227569Sphilip{
526227569Sphilip	struct sfxge_port *port;
527227569Sphilip	efsys_mem_t *esmp;
528227569Sphilip
529227569Sphilip	port = &sc->port;
530227569Sphilip	esmp = &port->mac_stats.dma_buf;
531227569Sphilip
532227569Sphilip	KASSERT(port->init_state == SFXGE_PORT_INITIALIZED,
533227569Sphilip	    ("Port not initialized"));
534227569Sphilip
535227569Sphilip	port->init_state = SFXGE_PORT_UNINITIALIZED;
536227569Sphilip
537227569Sphilip	port->link_mode = EFX_LINK_UNKNOWN;
538227569Sphilip
539227569Sphilip	/* Finish with PHY DMA memory */
540227569Sphilip	sfxge_dma_free(&port->phy_stats.dma_buf);
541227569Sphilip	free(port->phy_stats.decode_buf, M_SFXGE);
542227569Sphilip
543227569Sphilip	sfxge_dma_free(esmp);
544227569Sphilip	free(port->mac_stats.decode_buf, M_SFXGE);
545227569Sphilip
546227569Sphilip	mtx_destroy(&port->lock);
547227569Sphilip
548227569Sphilip	port->sc = NULL;
549227569Sphilip}
550227569Sphilip
551227569Sphilipint
552227569Sphilipsfxge_port_init(struct sfxge_softc *sc)
553227569Sphilip{
554227569Sphilip	struct sfxge_port *port;
555227569Sphilip	struct sysctl_ctx_list *sysctl_ctx;
556227569Sphilip	struct sysctl_oid *sysctl_tree;
557227569Sphilip	efsys_mem_t *mac_stats_buf, *phy_stats_buf;
558227569Sphilip	int rc;
559227569Sphilip
560227569Sphilip	port = &sc->port;
561227569Sphilip	mac_stats_buf = &port->mac_stats.dma_buf;
562227569Sphilip	phy_stats_buf = &port->phy_stats.dma_buf;
563227569Sphilip
564227569Sphilip	KASSERT(port->init_state == SFXGE_PORT_UNINITIALIZED,
565227569Sphilip	    ("Port already initialized"));
566227569Sphilip
567227569Sphilip	port->sc = sc;
568227569Sphilip
569227569Sphilip	mtx_init(&port->lock, "sfxge_port", NULL, MTX_DEF);
570227569Sphilip
571227569Sphilip	port->phy_stats.decode_buf = malloc(EFX_PHY_NSTATS * sizeof(uint32_t),
572227569Sphilip					    M_SFXGE, M_WAITOK | M_ZERO);
573227569Sphilip	if ((rc = sfxge_dma_alloc(sc, EFX_PHY_STATS_SIZE, phy_stats_buf)) != 0)
574227569Sphilip		goto fail;
575227569Sphilip	bzero(phy_stats_buf->esm_base, phy_stats_buf->esm_size);
576227569Sphilip	sfxge_phy_stat_init(sc);
577227569Sphilip
578227569Sphilip	sysctl_ctx = device_get_sysctl_ctx(sc->dev);
579227569Sphilip	sysctl_tree = device_get_sysctl_tree(sc->dev);
580227569Sphilip
581227569Sphilip#ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
582227569Sphilip	/* If flow control cannot be configured or reported through
583227569Sphilip	 * ifmedia, provide sysctls for it. */
584227569Sphilip	port->wanted_fc = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
585227569Sphilip	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
586227569Sphilip	    "wanted_fc", CTLTYPE_UINT|CTLFLAG_RW, sc, 0,
587227569Sphilip	    sfxge_port_wanted_fc_handler, "IU", "wanted flow control mode");
588227569Sphilip	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
589227569Sphilip	    "link_fc", CTLTYPE_UINT|CTLFLAG_RD, sc, 0,
590227569Sphilip	    sfxge_port_link_fc_handler, "IU", "link flow control mode");
591227569Sphilip#endif
592227569Sphilip
593227569Sphilip	port->mac_stats.decode_buf = malloc(EFX_MAC_NSTATS * sizeof(uint64_t),
594227569Sphilip					    M_SFXGE, M_WAITOK | M_ZERO);
595227569Sphilip	if ((rc = sfxge_dma_alloc(sc, EFX_MAC_STATS_SIZE, mac_stats_buf)) != 0)
596227569Sphilip		goto fail2;
597227569Sphilip	bzero(mac_stats_buf->esm_base, mac_stats_buf->esm_size);
598227569Sphilip	sfxge_mac_stat_init(sc);
599227569Sphilip
600227569Sphilip	port->init_state = SFXGE_PORT_INITIALIZED;
601227569Sphilip
602227569Sphilip	return (0);
603227569Sphilip
604227569Sphilipfail2:
605227569Sphilip	free(port->mac_stats.decode_buf, M_SFXGE);
606227569Sphilip	sfxge_dma_free(phy_stats_buf);
607227569Sphilipfail:
608227569Sphilip	free(port->phy_stats.decode_buf, M_SFXGE);
609227569Sphilip	(void)mtx_destroy(&port->lock);
610227569Sphilip	port->sc = NULL;
611227569Sphilip	return rc;
612227569Sphilip}
613227569Sphilip
614227569Sphilipstatic int sfxge_link_mode[EFX_PHY_MEDIA_NTYPES][EFX_LINK_NMODES] = {
615227569Sphilip	[EFX_PHY_MEDIA_CX4] = {
616227569Sphilip		[EFX_LINK_10000FDX]	= IFM_ETHER | IFM_FDX | IFM_10G_CX4,
617227569Sphilip	},
618227569Sphilip	[EFX_PHY_MEDIA_KX4] = {
619227569Sphilip		[EFX_LINK_10000FDX]	= IFM_ETHER | IFM_FDX | IFM_10G_KX4,
620227569Sphilip	},
621227569Sphilip	[EFX_PHY_MEDIA_XFP] = {
622227569Sphilip		/* Don't know the module type, but assume SR for now. */
623227569Sphilip		[EFX_LINK_10000FDX]	= IFM_ETHER | IFM_FDX | IFM_10G_SR,
624227569Sphilip	},
625227569Sphilip	[EFX_PHY_MEDIA_SFP_PLUS] = {
626227569Sphilip		/* Don't know the module type, but assume SX/SR for now. */
627227569Sphilip		[EFX_LINK_1000FDX]	= IFM_ETHER | IFM_FDX | IFM_1000_SX,
628227569Sphilip		[EFX_LINK_10000FDX]	= IFM_ETHER | IFM_FDX | IFM_10G_SR,
629227569Sphilip	},
630227569Sphilip	[EFX_PHY_MEDIA_BASE_T] = {
631227569Sphilip		[EFX_LINK_10HDX]	= IFM_ETHER | IFM_HDX | IFM_10_T,
632227569Sphilip		[EFX_LINK_10FDX]	= IFM_ETHER | IFM_FDX | IFM_10_T,
633227569Sphilip		[EFX_LINK_100HDX]	= IFM_ETHER | IFM_HDX | IFM_100_TX,
634227569Sphilip		[EFX_LINK_100FDX]	= IFM_ETHER | IFM_FDX | IFM_100_TX,
635227569Sphilip		[EFX_LINK_1000HDX]	= IFM_ETHER | IFM_HDX | IFM_1000_T,
636227569Sphilip		[EFX_LINK_1000FDX]	= IFM_ETHER | IFM_FDX | IFM_1000_T,
637227569Sphilip		[EFX_LINK_10000FDX]	= IFM_ETHER | IFM_FDX | IFM_10G_T,
638227569Sphilip	},
639227569Sphilip};
640227569Sphilip
641227569Sphilipstatic void
642227569Sphilipsfxge_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
643227569Sphilip{
644227569Sphilip	struct sfxge_softc *sc;
645227569Sphilip	efx_phy_media_type_t medium_type;
646227569Sphilip	efx_link_mode_t mode;
647227569Sphilip
648227569Sphilip	sc = ifp->if_softc;
649227569Sphilip	sx_xlock(&sc->softc_lock);
650227569Sphilip
651227569Sphilip	ifmr->ifm_status = IFM_AVALID;
652227569Sphilip	ifmr->ifm_active = IFM_ETHER;
653227569Sphilip
654227569Sphilip	if (SFXGE_RUNNING(sc) && SFXGE_LINK_UP(sc)) {
655227569Sphilip		ifmr->ifm_status |= IFM_ACTIVE;
656227569Sphilip
657227569Sphilip		efx_phy_media_type_get(sc->enp, &medium_type);
658227569Sphilip		mode = sc->port.link_mode;
659227569Sphilip		ifmr->ifm_active |= sfxge_link_mode[medium_type][mode];
660227569Sphilip		ifmr->ifm_active |= sfxge_port_link_fc_ifm(sc);
661227569Sphilip	}
662227569Sphilip
663227569Sphilip	sx_xunlock(&sc->softc_lock);
664227569Sphilip}
665227569Sphilip
666227569Sphilipstatic int
667227569Sphilipsfxge_media_change(struct ifnet *ifp)
668227569Sphilip{
669227569Sphilip	struct sfxge_softc *sc;
670227569Sphilip	struct ifmedia_entry *ifm;
671227569Sphilip	int rc;
672227569Sphilip
673227569Sphilip	sc = ifp->if_softc;
674227569Sphilip	ifm = sc->media.ifm_cur;
675227569Sphilip
676227569Sphilip	sx_xlock(&sc->softc_lock);
677227569Sphilip
678227569Sphilip	if (!SFXGE_RUNNING(sc)) {
679227569Sphilip		rc = 0;
680227569Sphilip		goto out;
681227569Sphilip	}
682227569Sphilip
683227569Sphilip	rc = efx_mac_fcntl_set(sc->enp, sfxge_port_wanted_fc(sc), B_TRUE);
684227569Sphilip	if (rc != 0)
685227569Sphilip		goto out;
686227569Sphilip
687227569Sphilip	rc = efx_phy_adv_cap_set(sc->enp, ifm->ifm_data);
688227569Sphilipout:
689227569Sphilip	sx_xunlock(&sc->softc_lock);
690227569Sphilip
691227569Sphilip	return rc;
692227569Sphilip}
693227569Sphilip
694227569Sphilipint sfxge_port_ifmedia_init(struct sfxge_softc *sc)
695227569Sphilip{
696227569Sphilip	efx_phy_media_type_t medium_type;
697227569Sphilip	uint32_t cap_mask, mode_cap_mask;
698227569Sphilip	efx_link_mode_t mode;
699227569Sphilip	int mode_ifm, best_mode_ifm = 0;
700227569Sphilip	int rc;
701227569Sphilip
702227569Sphilip	/* We need port state to initialise the ifmedia list. */
703227569Sphilip	if ((rc = efx_nic_init(sc->enp)) != 0)
704227569Sphilip		goto out;
705227569Sphilip	if ((rc = efx_port_init(sc->enp)) != 0)
706227569Sphilip		goto out2;
707227569Sphilip
708227569Sphilip	/*
709227569Sphilip	 * Register ifconfig callbacks for querying and setting the
710227569Sphilip	 * link mode and link status.
711227569Sphilip	 */
712227569Sphilip	ifmedia_init(&sc->media, IFM_IMASK, sfxge_media_change,
713227569Sphilip	    sfxge_media_status);
714227569Sphilip
715227569Sphilip	/*
716227569Sphilip	 * Map firmware medium type and capabilities to ifmedia types.
717227569Sphilip	 * ifmedia does not distinguish between forcing the link mode
718227569Sphilip	 * and disabling auto-negotiation.  1000BASE-T and 10GBASE-T
719227569Sphilip	 * require AN even if only one link mode is enabled, and for
720227569Sphilip	 * 100BASE-TX it is useful even if the link mode is forced.
721227569Sphilip	 * Therefore we never disable auto-negotiation.
722227569Sphilip	 *
723227569Sphilip	 * Also enable and advertise flow control by default.
724227569Sphilip	 */
725227569Sphilip
726227569Sphilip	efx_phy_media_type_get(sc->enp, &medium_type);
727227569Sphilip	efx_phy_adv_cap_get(sc->enp, EFX_PHY_CAP_PERM, &cap_mask);
728227569Sphilip
729227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_10HDX == EFX_PHY_CAP_10HDX + 1);
730227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_10FDX == EFX_PHY_CAP_10FDX + 1);
731227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_100HDX == EFX_PHY_CAP_100HDX + 1);
732227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_100FDX == EFX_PHY_CAP_100FDX + 1);
733227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_1000HDX == EFX_PHY_CAP_1000HDX + 1);
734227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_1000FDX == EFX_PHY_CAP_1000FDX + 1);
735227569Sphilip	EFX_STATIC_ASSERT(EFX_LINK_10000FDX == EFX_PHY_CAP_10000FDX + 1);
736227569Sphilip
737227569Sphilip	for (mode = EFX_LINK_10HDX; mode <= EFX_LINK_10000FDX; mode++) {
738227569Sphilip		mode_cap_mask = 1 << (mode - 1);
739227569Sphilip		mode_ifm = sfxge_link_mode[medium_type][mode];
740227569Sphilip
741227569Sphilip		if ((cap_mask & mode_cap_mask) && mode_ifm) {
742227569Sphilip			mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_AN);
743227569Sphilip
744227569Sphilip#ifdef SFXGE_HAVE_PAUSE_MEDIAOPTS
745227569Sphilip			/* No flow-control */
746227569Sphilip			ifmedia_add(&sc->media, mode_ifm, mode_cap_mask, NULL);
747227569Sphilip
748227569Sphilip			/* Respond-only.  If using AN, we implicitly
749227569Sphilip			 * offer symmetric as well, but that doesn't
750227569Sphilip			 * mean we *have* to generate pause frames.
751227569Sphilip			 */
752227569Sphilip			mode_cap_mask |= cap_mask & ((1 << EFX_PHY_CAP_PAUSE) |
753227569Sphilip						     (1 << EFX_PHY_CAP_ASYM));
754227569Sphilip			mode_ifm |= IFM_ETH_RXPAUSE;
755227569Sphilip			ifmedia_add(&sc->media, mode_ifm, mode_cap_mask, NULL);
756227569Sphilip
757227569Sphilip			/* Symmetric */
758227569Sphilip			mode_cap_mask &= ~(1 << EFX_PHY_CAP_ASYM);
759227569Sphilip			mode_ifm |= IFM_ETH_TXPAUSE;
760227569Sphilip#else /* !SFXGE_HAVE_PAUSE_MEDIAOPTS */
761227569Sphilip			mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_PAUSE);
762227569Sphilip#endif
763227569Sphilip			ifmedia_add(&sc->media, mode_ifm, mode_cap_mask, NULL);
764227569Sphilip
765227569Sphilip			/* Link modes are numbered in order of speed,
766227569Sphilip			 * so assume the last one available is the best.
767227569Sphilip			 */
768227569Sphilip			best_mode_ifm = mode_ifm;
769227569Sphilip		}
770227569Sphilip	}
771227569Sphilip
772227569Sphilip	if (cap_mask & (1 << EFX_PHY_CAP_AN)) {
773227569Sphilip		/* Add autoselect mode. */
774227569Sphilip		mode_ifm = IFM_ETHER | IFM_AUTO;
775227569Sphilip		ifmedia_add(&sc->media, mode_ifm,
776227569Sphilip			    cap_mask & ~(1 << EFX_PHY_CAP_ASYM), NULL);
777227569Sphilip		best_mode_ifm = mode_ifm;
778227569Sphilip	}
779227569Sphilip
780227569Sphilip	if (best_mode_ifm)
781227569Sphilip		ifmedia_set(&sc->media, best_mode_ifm);
782227569Sphilip
783227569Sphilip	/* Now discard port state until interface is started. */
784227569Sphilip	efx_port_fini(sc->enp);
785227569Sphilipout2:
786227569Sphilip	efx_nic_fini(sc->enp);
787227569Sphilipout:
788227569Sphilip	return rc;
789227569Sphilip}
790