Deleted Added
full compact
if_xe.c (331722) if_xe.c (347962)
1/*-
2 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*-
27 * Portions of this software were derived from Werner Koch's xirc2ps driver
28 * for Linux under the terms of the following license (from v1.30 of the
29 * xirc2ps driver):
30 *
31 * Copyright (c) 1997 by Werner Koch (dd9jn)
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, and the entire permission notice in its entirety,
38 * including the disclaimer of warranties.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote
43 * products derived from this software without specific prior
44 * written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26/*-
27 * Portions of this software were derived from Werner Koch's xirc2ps driver
28 * for Linux under the terms of the following license (from v1.30 of the
29 * xirc2ps driver):
30 *
31 * Copyright (c) 1997 by Werner Koch (dd9jn)
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, and the entire permission notice in its entirety,
38 * including the disclaimer of warranties.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote
43 * products derived from this software without specific prior
44 * written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: stable/11/sys/dev/xe/if_xe.c 331722 2018-03-29 02:50:57Z eadler $");
60__FBSDID("$FreeBSD: stable/11/sys/dev/xe/if_xe.c 347962 2019-05-18 20:43:13Z brooks $");
61
62/*
63 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
64 * following cards are currently known to work with the driver:
65 * Xircom CreditCard 10/100 (CE3)
66 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
67 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
68 * Xircom RealPort Ethernet 10
69 * Xircom RealPort Ethernet 10/100
70 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
71 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
72 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
73 *
74 * Some other cards *should* work, but support for them is either broken or in
75 * an unknown state at the moment. I'm always interested in hearing from
76 * people who own any of these cards:
77 * Xircom CreditCard 10Base-T (PS-CE2-10)
78 * Xircom CreditCard Ethernet + ModemII (CEM2)
79 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
80 *
81 * Thanks to all who assisted with the development and testing of the driver,
82 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
83 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
84 * out anyone who deserves a mention here.
85 *
86 * Special thanks to Ade Lovett for both hosting the mailing list and doing
87 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
88 * the web pages.
89 *
90 * Author email: <scott@uk.freebsd.org>
91 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
92 */
93
94
95#include <sys/param.h>
96#include <sys/cdefs.h>
97#include <sys/errno.h>
98#include <sys/kernel.h>
99#include <sys/malloc.h>
100#include <sys/mbuf.h>
101#include <sys/socket.h>
102#include <sys/sockio.h>
103#include <sys/systm.h>
104#include <sys/uio.h>
105#include <sys/sysctl.h>
106
107#include <sys/module.h>
108#include <sys/bus.h>
109
110#include <machine/bus.h>
111#include <machine/resource.h>
112#include <sys/rman.h>
113
114#include <net/ethernet.h>
115#include <net/if.h>
116#include <net/if_var.h>
117#include <net/if_arp.h>
118#include <net/if_dl.h>
119#include <net/if_media.h>
120#include <net/if_mib.h>
121#include <net/bpf.h>
122#include <net/if_types.h>
123
124#include <dev/xe/if_xereg.h>
125#include <dev/xe/if_xevar.h>
126
127/*
128 * MII command structure
129 */
130struct xe_mii_frame {
131 uint8_t mii_stdelim;
132 uint8_t mii_opcode;
133 uint8_t mii_phyaddr;
134 uint8_t mii_regaddr;
135 uint8_t mii_turnaround;
136 uint16_t mii_data;
137};
138
139/*
140 * Media autonegotiation progress constants
141 */
142#define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
143#define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
144#define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
145#define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
146#define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
147
148/*
149 * Prototypes start here
150 */
151static void xe_init(void *xscp);
152static void xe_init_locked(struct xe_softc *scp);
153static void xe_start(struct ifnet *ifp);
154static void xe_start_locked(struct ifnet *ifp);
155static int xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
156static void xe_watchdog(void *arg);
157static void xe_intr(void *xscp);
158static void xe_txintr(struct xe_softc *scp, uint8_t txst1);
159static void xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0,
160 uint8_t txst1);
161static void xe_rxintr(struct xe_softc *scp, uint8_t rst0);
162static int xe_media_change(struct ifnet *ifp);
163static void xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp);
164static void xe_setmedia(void *arg);
165static void xe_reset(struct xe_softc *scp);
166static void xe_enable_intr(struct xe_softc *scp);
167static void xe_disable_intr(struct xe_softc *scp);
168static void xe_set_multicast(struct xe_softc *scp);
169static void xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx);
170static void xe_mchash(struct xe_softc *scp, const uint8_t *addr);
171static int xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp);
172
173/*
174 * MII functions
175 */
176static void xe_mii_sync(struct xe_softc *scp);
177static int xe_mii_init(struct xe_softc *scp);
178static void xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt);
179static int xe_mii_readreg(struct xe_softc *scp,
180 struct xe_mii_frame *frame);
181static int xe_mii_writereg(struct xe_softc *scp,
182 struct xe_mii_frame *frame);
183static uint16_t xe_phy_readreg(struct xe_softc *scp, uint16_t reg);
184static void xe_phy_writereg(struct xe_softc *scp, uint16_t reg,
185 uint16_t data);
186
187/*
188 * Debugging functions
189 */
190static void xe_mii_dump(struct xe_softc *scp);
191#if 0
192static void xe_reg_dump(struct xe_softc *scp);
193#endif
194
195/*
196 * Debug logging levels - set with hw.xe.debug sysctl
197 * 0 = None
198 * 1 = More hardware details, probe/attach progress
199 * 2 = Most function calls, ioctls and media selection progress
200 * 3 = Everything - interrupts, packets in/out and multicast address setup
201 */
202#define XE_DEBUG
203#ifdef XE_DEBUG
204
205/* sysctl vars */
206static SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
207
208int xe_debug = 0;
209SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0,
210 "if_xe debug level");
211
212#define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
213#define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
214#define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
215#if 0
216#define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
217#endif
218#else
219#define DEVPRINTF(level, arg)
220#define DPRINTF(level, arg)
221#define XE_MII_DUMP(scp)
222#if 0
223#define XE_REG_DUMP(scp)
224#endif
225#endif
226
227/*
228 * Attach a device.
229 */
230int
231xe_attach(device_t dev)
232{
233 struct xe_softc *scp = device_get_softc(dev);
234 int err;
235
236 DEVPRINTF(2, (dev, "attach\n"));
237
238 /* Initialise stuff... */
239 scp->dev = dev;
240 scp->ifp = if_alloc(IFT_ETHER);
241 if (scp->ifp == NULL)
242 return (ENOSPC);
243 scp->ifm = &scp->ifmedia;
244 scp->autoneg_status = XE_AUTONEG_NONE;
245 mtx_init(&scp->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
246 MTX_DEF);
247 callout_init_mtx(&scp->wdog_timer, &scp->lock, 0);
248
249 /* Initialise the ifnet structure */
250 scp->ifp->if_softc = scp;
251 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
252 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
253 scp->ifp->if_linkmib = &scp->mibdata;
254 scp->ifp->if_linkmiblen = sizeof(scp->mibdata);
255 scp->ifp->if_start = xe_start;
256 scp->ifp->if_ioctl = xe_ioctl;
257 scp->ifp->if_init = xe_init;
258 scp->ifp->if_baudrate = 100000000;
259 IFQ_SET_MAXLEN(&scp->ifp->if_snd, ifqmaxlen);
260
261 /* Initialise the ifmedia structure */
262 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
263 callout_init_mtx(&scp->media_timer, &scp->lock, 0);
264
265 /* Add supported media types */
266 if (scp->mohawk) {
267 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
268 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
269 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
270 }
271 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
272 if (scp->ce2)
273 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
274 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
275
276 /* Default is to autoselect best supported media type */
277 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
278
279 /* Get the hardware into a known state */
280 XE_LOCK(scp);
281 xe_reset(scp);
282 XE_UNLOCK(scp);
283
284 /* Get hardware version numbers */
285 XE_SELECT_PAGE(4);
286 scp->version = XE_INB(XE_BOV);
287 if (scp->mohawk)
288 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
289 else
290 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
291
292 /* Print some useful information */
293 device_printf(dev, "version 0x%02x/0x%02x%s%s\n", scp->version,
294 scp->srev, scp->mohawk ? ", 100Mbps capable" : "",
295 scp->modem ? ", with modem" : "");
296 if (scp->mohawk) {
297 XE_SELECT_PAGE(0x10);
298 DEVPRINTF(1, (dev,
299 "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
300 XE_INW(XE_DINGOID), XE_INW(XE_RevID), XE_INW(XE_VendorID)));
301 }
302 if (scp->ce2) {
303 XE_SELECT_PAGE(0x45);
304 DEVPRINTF(1, (dev, "CE2 version = 0x%02x\n", XE_INB(XE_REV)));
305 }
306
307 /* Attach the interface */
308 ether_ifattach(scp->ifp, scp->enaddr);
309
310 err = bus_setup_intr(dev, scp->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
311 NULL, xe_intr, scp, &scp->intrhand);
312 if (err) {
313 ether_ifdetach(scp->ifp);
314 mtx_destroy(&scp->lock);
315 return (err);
316 }
317
61
62/*
63 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
64 * following cards are currently known to work with the driver:
65 * Xircom CreditCard 10/100 (CE3)
66 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
67 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
68 * Xircom RealPort Ethernet 10
69 * Xircom RealPort Ethernet 10/100
70 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
71 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
72 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
73 *
74 * Some other cards *should* work, but support for them is either broken or in
75 * an unknown state at the moment. I'm always interested in hearing from
76 * people who own any of these cards:
77 * Xircom CreditCard 10Base-T (PS-CE2-10)
78 * Xircom CreditCard Ethernet + ModemII (CEM2)
79 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
80 *
81 * Thanks to all who assisted with the development and testing of the driver,
82 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
83 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
84 * out anyone who deserves a mention here.
85 *
86 * Special thanks to Ade Lovett for both hosting the mailing list and doing
87 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
88 * the web pages.
89 *
90 * Author email: <scott@uk.freebsd.org>
91 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
92 */
93
94
95#include <sys/param.h>
96#include <sys/cdefs.h>
97#include <sys/errno.h>
98#include <sys/kernel.h>
99#include <sys/malloc.h>
100#include <sys/mbuf.h>
101#include <sys/socket.h>
102#include <sys/sockio.h>
103#include <sys/systm.h>
104#include <sys/uio.h>
105#include <sys/sysctl.h>
106
107#include <sys/module.h>
108#include <sys/bus.h>
109
110#include <machine/bus.h>
111#include <machine/resource.h>
112#include <sys/rman.h>
113
114#include <net/ethernet.h>
115#include <net/if.h>
116#include <net/if_var.h>
117#include <net/if_arp.h>
118#include <net/if_dl.h>
119#include <net/if_media.h>
120#include <net/if_mib.h>
121#include <net/bpf.h>
122#include <net/if_types.h>
123
124#include <dev/xe/if_xereg.h>
125#include <dev/xe/if_xevar.h>
126
127/*
128 * MII command structure
129 */
130struct xe_mii_frame {
131 uint8_t mii_stdelim;
132 uint8_t mii_opcode;
133 uint8_t mii_phyaddr;
134 uint8_t mii_regaddr;
135 uint8_t mii_turnaround;
136 uint16_t mii_data;
137};
138
139/*
140 * Media autonegotiation progress constants
141 */
142#define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
143#define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
144#define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
145#define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
146#define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
147
148/*
149 * Prototypes start here
150 */
151static void xe_init(void *xscp);
152static void xe_init_locked(struct xe_softc *scp);
153static void xe_start(struct ifnet *ifp);
154static void xe_start_locked(struct ifnet *ifp);
155static int xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
156static void xe_watchdog(void *arg);
157static void xe_intr(void *xscp);
158static void xe_txintr(struct xe_softc *scp, uint8_t txst1);
159static void xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0,
160 uint8_t txst1);
161static void xe_rxintr(struct xe_softc *scp, uint8_t rst0);
162static int xe_media_change(struct ifnet *ifp);
163static void xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp);
164static void xe_setmedia(void *arg);
165static void xe_reset(struct xe_softc *scp);
166static void xe_enable_intr(struct xe_softc *scp);
167static void xe_disable_intr(struct xe_softc *scp);
168static void xe_set_multicast(struct xe_softc *scp);
169static void xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx);
170static void xe_mchash(struct xe_softc *scp, const uint8_t *addr);
171static int xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp);
172
173/*
174 * MII functions
175 */
176static void xe_mii_sync(struct xe_softc *scp);
177static int xe_mii_init(struct xe_softc *scp);
178static void xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt);
179static int xe_mii_readreg(struct xe_softc *scp,
180 struct xe_mii_frame *frame);
181static int xe_mii_writereg(struct xe_softc *scp,
182 struct xe_mii_frame *frame);
183static uint16_t xe_phy_readreg(struct xe_softc *scp, uint16_t reg);
184static void xe_phy_writereg(struct xe_softc *scp, uint16_t reg,
185 uint16_t data);
186
187/*
188 * Debugging functions
189 */
190static void xe_mii_dump(struct xe_softc *scp);
191#if 0
192static void xe_reg_dump(struct xe_softc *scp);
193#endif
194
195/*
196 * Debug logging levels - set with hw.xe.debug sysctl
197 * 0 = None
198 * 1 = More hardware details, probe/attach progress
199 * 2 = Most function calls, ioctls and media selection progress
200 * 3 = Everything - interrupts, packets in/out and multicast address setup
201 */
202#define XE_DEBUG
203#ifdef XE_DEBUG
204
205/* sysctl vars */
206static SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
207
208int xe_debug = 0;
209SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW, &xe_debug, 0,
210 "if_xe debug level");
211
212#define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
213#define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
214#define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
215#if 0
216#define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
217#endif
218#else
219#define DEVPRINTF(level, arg)
220#define DPRINTF(level, arg)
221#define XE_MII_DUMP(scp)
222#if 0
223#define XE_REG_DUMP(scp)
224#endif
225#endif
226
227/*
228 * Attach a device.
229 */
230int
231xe_attach(device_t dev)
232{
233 struct xe_softc *scp = device_get_softc(dev);
234 int err;
235
236 DEVPRINTF(2, (dev, "attach\n"));
237
238 /* Initialise stuff... */
239 scp->dev = dev;
240 scp->ifp = if_alloc(IFT_ETHER);
241 if (scp->ifp == NULL)
242 return (ENOSPC);
243 scp->ifm = &scp->ifmedia;
244 scp->autoneg_status = XE_AUTONEG_NONE;
245 mtx_init(&scp->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
246 MTX_DEF);
247 callout_init_mtx(&scp->wdog_timer, &scp->lock, 0);
248
249 /* Initialise the ifnet structure */
250 scp->ifp->if_softc = scp;
251 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
252 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
253 scp->ifp->if_linkmib = &scp->mibdata;
254 scp->ifp->if_linkmiblen = sizeof(scp->mibdata);
255 scp->ifp->if_start = xe_start;
256 scp->ifp->if_ioctl = xe_ioctl;
257 scp->ifp->if_init = xe_init;
258 scp->ifp->if_baudrate = 100000000;
259 IFQ_SET_MAXLEN(&scp->ifp->if_snd, ifqmaxlen);
260
261 /* Initialise the ifmedia structure */
262 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
263 callout_init_mtx(&scp->media_timer, &scp->lock, 0);
264
265 /* Add supported media types */
266 if (scp->mohawk) {
267 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
268 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
269 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
270 }
271 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
272 if (scp->ce2)
273 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
274 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
275
276 /* Default is to autoselect best supported media type */
277 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
278
279 /* Get the hardware into a known state */
280 XE_LOCK(scp);
281 xe_reset(scp);
282 XE_UNLOCK(scp);
283
284 /* Get hardware version numbers */
285 XE_SELECT_PAGE(4);
286 scp->version = XE_INB(XE_BOV);
287 if (scp->mohawk)
288 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
289 else
290 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
291
292 /* Print some useful information */
293 device_printf(dev, "version 0x%02x/0x%02x%s%s\n", scp->version,
294 scp->srev, scp->mohawk ? ", 100Mbps capable" : "",
295 scp->modem ? ", with modem" : "");
296 if (scp->mohawk) {
297 XE_SELECT_PAGE(0x10);
298 DEVPRINTF(1, (dev,
299 "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
300 XE_INW(XE_DINGOID), XE_INW(XE_RevID), XE_INW(XE_VendorID)));
301 }
302 if (scp->ce2) {
303 XE_SELECT_PAGE(0x45);
304 DEVPRINTF(1, (dev, "CE2 version = 0x%02x\n", XE_INB(XE_REV)));
305 }
306
307 /* Attach the interface */
308 ether_ifattach(scp->ifp, scp->enaddr);
309
310 err = bus_setup_intr(dev, scp->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
311 NULL, xe_intr, scp, &scp->intrhand);
312 if (err) {
313 ether_ifdetach(scp->ifp);
314 mtx_destroy(&scp->lock);
315 return (err);
316 }
317
318 gone_by_fcp101_dev(dev);
319
318 /* Done */
319 return (0);
320}
321
322/*
323 * Complete hardware intitialisation and enable output. Exits without doing
324 * anything if there's no address assigned to the card, or if media selection
325 * is in progress (the latter implies we've already run this function).
326 */
327static void
328xe_init(void *xscp)
329{
330 struct xe_softc *scp = xscp;
331
332 XE_LOCK(scp);
333 xe_init_locked(scp);
334 XE_UNLOCK(scp);
335}
336
337static void
338xe_init_locked(struct xe_softc *scp)
339{
340 unsigned i;
341
342 if (scp->autoneg_status != XE_AUTONEG_NONE)
343 return;
344
345 DEVPRINTF(2, (scp->dev, "init\n"));
346
347 /* Reset transmitter flags */
348 scp->tx_queued = 0;
349 scp->tx_tpr = 0;
350 scp->tx_timeouts = 0;
351 scp->tx_thres = 64;
352 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
353 scp->tx_timeout = 0;
354
355 /* Soft reset the card */
356 XE_SELECT_PAGE(0);
357 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
358 DELAY(40000);
359 XE_OUTB(XE_CR, 0);
360 DELAY(40000);
361
362 if (scp->mohawk) {
363 /*
364 * set GP1 and GP2 as outputs (bits 2 & 3)
365 * set GP1 low to power on the ML6692 (bit 0)
366 * set GP2 high to power on the 10Mhz chip (bit 1)
367 */
368 XE_SELECT_PAGE(4);
369 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
370 XE_GPR0_GP2_OUT);
371 }
372
373 /* Shut off interrupts */
374 xe_disable_intr(scp);
375
376 /* Wait for everything to wake up */
377 DELAY(500000);
378
379 /* Check for PHY */
380 if (scp->mohawk)
381 scp->phy_ok = xe_mii_init(scp);
382
383 /* Disable 'source insertion' (not sure what that means) */
384 XE_SELECT_PAGE(0x42);
385 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
386
387 /* Set 8K/24K Tx/Rx buffer split */
388 if (scp->srev != 1) {
389 XE_SELECT_PAGE(2);
390 XE_OUTW(XE_RBS, 0x2000);
391 }
392
393 /* Enable early transmit mode on Mohawk/Dingo */
394 if (scp->mohawk) {
395 XE_SELECT_PAGE(0x03);
396 XE_OUTW(XE_TPT, scp->tx_thres);
397 XE_SELECT_PAGE(0x01);
398 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
399 }
400
401 /* Put MAC address in first 'individual address' register */
402 XE_SELECT_PAGE(0x50);
403 for (i = 0; i < ETHER_ADDR_LEN; i++)
404 XE_OUTB(0x08 + i, IF_LLADDR(scp->ifp)[scp->mohawk ? 5 - i : i]);
405
406 /* Set up multicast addresses */
407 xe_set_multicast(scp);
408
409 /* Fix the receive data offset -- reset can leave it off-by-one */
410 XE_SELECT_PAGE(0);
411 XE_OUTW(XE_DO, 0x2000);
412
413 /* Set interrupt masks */
414 XE_SELECT_PAGE(1);
415 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR |
416 XE_IMR0_RX_PACKET);
417
418 /* Set MAC interrupt masks */
419 XE_SELECT_PAGE(0x40);
420 XE_OUTB(XE_RX0Msk,
421 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR | XE_RX0M_ALIGN_ERROR |
422 XE_RX0M_LONG_PACKET));
423 XE_OUTB(XE_TX0Msk,
424 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN |
425 XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
426
427 /* Clear MAC status registers */
428 XE_SELECT_PAGE(0x40);
429 XE_OUTB(XE_RST0, 0x00);
430 XE_OUTB(XE_TXST0, 0x00);
431
432 /* Enable receiver and put MAC online */
433 XE_SELECT_PAGE(0x40);
434 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
435
436 /* Set up IMR, enable interrupts */
437 xe_enable_intr(scp);
438
439 /* Start media selection */
440 xe_setmedia(scp);
441
442 /* Enable output */
443 scp->ifp->if_drv_flags |= IFF_DRV_RUNNING;
444 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
445 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
446}
447
448/*
449 * Start output on interface. Should be called at splimp() priority. Check
450 * that the output is idle (ie, IFF_DRV_OACTIVE is not set) before calling this
451 * function. If media selection is in progress we set IFF_DRV_OACTIVE ourselves
452 * and return immediately.
453 */
454static void
455xe_start(struct ifnet *ifp)
456{
457 struct xe_softc *scp = ifp->if_softc;
458
459 XE_LOCK(scp);
460 xe_start_locked(ifp);
461 XE_UNLOCK(scp);
462}
463
464static void
465xe_start_locked(struct ifnet *ifp)
466{
467 struct xe_softc *scp = ifp->if_softc;
468 struct mbuf *mbp;
469
470 if (scp->autoneg_status != XE_AUTONEG_NONE) {
471 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
472 return;
473 }
474
475 DEVPRINTF(3, (scp->dev, "start\n"));
476
477 /*
478 * Loop while there are packets to be sent, and space to send
479 * them.
480 */
481 for (;;) {
482 /* Suck a packet off the send queue */
483 IF_DEQUEUE(&ifp->if_snd, mbp);
484
485 if (mbp == NULL) {
486 /*
487 * We are using the !OACTIVE flag to indicate
488 * to the outside world that we can accept an
489 * additional packet rather than that the
490 * transmitter is _actually_ active. Indeed,
491 * the transmitter may be active, but if we
492 * haven't filled all the buffers with data
493 * then we still want to accept more.
494 */
495 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
496 return;
497 }
498
499 if (xe_pio_write_packet(scp, mbp) != 0) {
500 /* Push the packet back onto the queue */
501 IF_PREPEND(&ifp->if_snd, mbp);
502 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
503 return;
504 }
505
506 /* Tap off here if there is a bpf listener */
507 BPF_MTAP(ifp, mbp);
508
509 /* In case we don't hear from the card again... */
510 scp->tx_timeout = 5;
511 scp->tx_queued++;
512
513 m_freem(mbp);
514 }
515}
516
517/*
518 * Process an ioctl request. Adapted from the ed driver.
519 */
520static int
521xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
522{
523 struct xe_softc *scp;
524 int error;
525
526 scp = ifp->if_softc;
527 error = 0;
528
529 switch (command) {
530 case SIOCSIFFLAGS:
531 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n",
532 ifp->if_flags));
533 /*
534 * If the interface is marked up and stopped, then
535 * start it. If it is marked down and running, then
536 * stop it.
537 */
538 XE_LOCK(scp);
539 if (ifp->if_flags & IFF_UP) {
540 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
541 xe_reset(scp);
542 xe_init_locked(scp);
543 }
544 } else {
545 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
546 xe_stop(scp);
547 }
548
549 /* handle changes to PROMISC/ALLMULTI flags */
550 xe_set_multicast(scp);
551 XE_UNLOCK(scp);
552 error = 0;
553 break;
554 case SIOCADDMULTI:
555 case SIOCDELMULTI:
556 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
557 /*
558 * Multicast list has (maybe) changed; set the
559 * hardware filters accordingly.
560 */
561 XE_LOCK(scp);
562 xe_set_multicast(scp);
563 XE_UNLOCK(scp);
564 error = 0;
565 break;
566 case SIOCSIFMEDIA:
567 case SIOCGIFMEDIA:
568 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
569 /*
570 * Someone wants to get/set media options.
571 */
572 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia,
573 command);
574 break;
575 default:
576 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
577 error = ether_ioctl(ifp, command, data);
578 }
579
580 return (error);
581}
582
583/*
584 * Card interrupt handler.
585 *
586 * This function is probably more complicated than it needs to be, as it
587 * attempts to deal with the case where multiple packets get sent between
588 * interrupts. This is especially annoying when working out the collision
589 * stats. Not sure whether this case ever really happens or not (maybe on a
590 * slow/heavily loaded machine?) so it's probably best to leave this like it
591 * is.
592 *
593 * Note that the crappy PIO used to get packets on and off the card means that
594 * you will spend a lot of time in this routine -- I can get my P150 to spend
595 * 90% of its time servicing interrupts if I really hammer the network. Could
596 * fix this, but then you'd start dropping/losing packets. The moral of this
597 * story? If you want good network performance _and_ some cycles left over to
598 * get your work done, don't buy a Xircom card. Or convince them to tell me
599 * how to do memory-mapped I/O :)
600 */
601static void
602xe_txintr(struct xe_softc *scp, uint8_t txst1)
603{
604 struct ifnet *ifp;
605 uint8_t tpr, sent, coll;
606
607 ifp = scp->ifp;
608
609 /* Update packet count, accounting for rollover */
610 tpr = XE_INB(XE_TPR);
611 sent = -scp->tx_tpr + tpr;
612
613 /* Update statistics if we actually sent anything */
614 if (sent > 0) {
615 coll = txst1 & XE_TXST1_RETRY_COUNT;
616 scp->tx_tpr = tpr;
617 scp->tx_queued -= sent;
618 if_inc_counter(ifp, IFCOUNTER_OPACKETS, sent);
619 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, coll);
620
621 /*
622 * According to the Xircom manual, Dingo will
623 * sometimes manage to transmit a packet with
624 * triggering an interrupt. If this happens, we have
625 * sent > 1 and the collision count only reflects
626 * collisions on the last packet sent (the one that
627 * triggered the interrupt). Collision stats might
628 * therefore be a bit low, but there doesn't seem to
629 * be anything we can do about that.
630 */
631 switch (coll) {
632 case 0:
633 break;
634 case 1:
635 scp->mibdata.dot3StatsSingleCollisionFrames++;
636 scp->mibdata.dot3StatsCollFrequencies[0]++;
637 break;
638 default:
639 scp->mibdata.dot3StatsMultipleCollisionFrames++;
640 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
641 }
642 }
643 scp->tx_timeout = 0;
644 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
645}
646
647/* Handle most MAC interrupts */
648static void
649xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0, uint8_t txst1)
650{
651 struct ifnet *ifp;
652
653 ifp = scp->ifp;
654
655#if 0
656 /* Carrier sense lost -- only in 10Mbit HDX mode */
657 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
658 /* XXX - Need to update media status here */
659 device_printf(scp->dev, "no carrier\n");
660 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
661 scp->mibdata.dot3StatsCarrierSenseErrors++;
662 }
663#endif
664 /* Excessive collisions -- try sending again */
665 if (txst0 & XE_TXST0_16_COLLISIONS) {
666 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 16);
667 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
668 scp->mibdata.dot3StatsExcessiveCollisions++;
669 scp->mibdata.dot3StatsMultipleCollisionFrames++;
670 scp->mibdata.dot3StatsCollFrequencies[15]++;
671 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
672 }
673
674 /* Transmit underrun -- increase early transmit threshold */
675 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
676 DEVPRINTF(1, (scp->dev, "transmit underrun"));
677 if (scp->tx_thres < ETHER_MAX_LEN) {
678 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
679 scp->tx_thres = ETHER_MAX_LEN;
680 DPRINTF(1, (": increasing transmit threshold to %u",
681 scp->tx_thres));
682 XE_SELECT_PAGE(0x3);
683 XE_OUTW(XE_TPT, scp->tx_thres);
684 XE_SELECT_PAGE(0x0);
685 }
686 DPRINTF(1, ("\n"));
687 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
688 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
689 }
690
691 /* Late collision -- just complain about it */
692 if (txst0 & XE_TXST0_LATE_COLLISION) {
693 device_printf(scp->dev, "late collision\n");
694 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
695 scp->mibdata.dot3StatsLateCollisions++;
696 }
697
698 /* SQE test failure -- just complain about it */
699 if (txst0 & XE_TXST0_SQE_FAIL) {
700 device_printf(scp->dev, "SQE test failure\n");
701 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
702 scp->mibdata.dot3StatsSQETestErrors++;
703 }
704
705 /* Packet too long -- what happens to these */
706 if (rst0 & XE_RST0_LONG_PACKET) {
707 device_printf(scp->dev, "received giant packet\n");
708 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
709 scp->mibdata.dot3StatsFrameTooLongs++;
710 }
711
712 /* CRC error -- packet dropped */
713 if (rst0 & XE_RST0_CRC_ERROR) {
714 device_printf(scp->dev, "CRC error\n");
715 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
716 scp->mibdata.dot3StatsFCSErrors++;
717 }
718}
719
720static void
721xe_rxintr(struct xe_softc *scp, uint8_t rst0)
722{
723 struct ifnet *ifp;
724 uint8_t esr, rsr;
725
726 ifp = scp->ifp;
727
728 /* Handle received packet(s) */
729 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
730 rsr = XE_INB(XE_RSR);
731
732 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr,
733 rsr));
734
735 /* Make sure packet is a good one */
736 if (rsr & XE_RSR_RX_OK) {
737 struct ether_header *ehp;
738 struct mbuf *mbp;
739 uint16_t len;
740
741 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
742
743 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n",
744 len));
745
746 if (len == 0) {
747 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
748 continue;
749 }
750
751 /*
752 * Allocate mbuf to hold received packet. If
753 * the mbuf header isn't big enough, we attach
754 * an mbuf cluster to hold the packet. Note
755 * the +=2 to align the packet data on a
756 * 32-bit boundary, and the +3 to allow for
757 * the possibility of reading one more byte
758 * than the actual packet length (we always
759 * read 16-bit words). XXX - Surely there's a
760 * better way to do this alignment?
761 */
762 MGETHDR(mbp, M_NOWAIT, MT_DATA);
763 if (mbp == NULL) {
764 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
765 continue;
766 }
767
768 if (len + 3 > MHLEN) {
769 if (!(MCLGET(mbp, M_NOWAIT))) {
770 m_freem(mbp);
771 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
772 continue;
773 }
774 }
775
776 mbp->m_data += 2;
777 ehp = mtod(mbp, struct ether_header *);
778
779 /*
780 * Now get the packet in PIO mode, including
781 * the Ethernet header but omitting the
782 * trailing CRC.
783 */
784
785 /*
786 * Work around a bug in CE2 cards. There
787 * seems to be a problem with duplicated and
788 * extraneous bytes in the receive buffer, but
789 * without any real documentation for the CE2
790 * it's hard to tell for sure. XXX - Needs
791 * testing on CE2 hardware
792 */
793 if (scp->srev == 0) {
794 u_short rhs;
795
796 XE_SELECT_PAGE(5);
797 rhs = XE_INW(XE_RHSA);
798 XE_SELECT_PAGE(0);
799
800 rhs += 3; /* Skip control info */
801
802 if (rhs >= 0x8000)
803 rhs = 0;
804
805 if (rhs + len > 0x8000) {
806 int i;
807
808 for (i = 0; i < len; i++, rhs++) {
809 ((char *)ehp)[i] =
810 XE_INB(XE_EDP);
811 if (rhs == 0x8000) {
812 rhs = 0;
813 i--;
814 }
815 }
816 } else
817 bus_read_multi_2(scp->port_res, XE_EDP,
818 (uint16_t *)ehp, (len + 1) >> 1);
819 } else
820 bus_read_multi_2(scp->port_res, XE_EDP,
821 (uint16_t *)ehp, (len + 1) >> 1);
822
823 /* Deliver packet to upper layers */
824 mbp->m_pkthdr.rcvif = ifp;
825 mbp->m_pkthdr.len = mbp->m_len = len;
826 XE_UNLOCK(scp);
827 (*ifp->if_input)(ifp, mbp);
828 XE_LOCK(scp);
829 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
830
831 } else if (rsr & XE_RSR_ALIGN_ERROR) {
832 /* Packet alignment error -- drop packet */
833 device_printf(scp->dev, "alignment error\n");
834 scp->mibdata.dot3StatsAlignmentErrors++;
835 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
836 }
837
838 /* Skip to next packet, if there is one */
839 XE_OUTW(XE_DO, 0x8000);
840 }
841
842 /* Clear receiver overruns now we have some free buffer space */
843 if (rst0 & XE_RST0_RX_OVERRUN) {
844 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
845 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
846 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
847 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
848 }
849}
850
851static void
852xe_intr(void *xscp)
853{
854 struct xe_softc *scp = (struct xe_softc *) xscp;
855 struct ifnet *ifp;
856 uint8_t psr, isr, rst0, txst0, txst1;
857
858 ifp = scp->ifp;
859 XE_LOCK(scp);
860
861 /* Disable interrupts */
862 if (scp->mohawk)
863 XE_OUTB(XE_CR, 0);
864
865 /* Cache current register page */
866 psr = XE_INB(XE_PR);
867
868 /* Read ISR to see what caused this interrupt */
869 while ((isr = XE_INB(XE_ISR)) != 0) {
870
871 /* 0xff might mean the card is no longer around */
872 if (isr == 0xff) {
873 DEVPRINTF(3, (scp->dev,
874 "intr: interrupt received for missing card?\n"));
875 break;
876 }
877
878 /* Read other status registers */
879 XE_SELECT_PAGE(0x40);
880 rst0 = XE_INB(XE_RST0);
881 XE_OUTB(XE_RST0, 0);
882 txst0 = XE_INB(XE_TXST0);
883 txst1 = XE_INB(XE_TXST1);
884 XE_OUTB(XE_TXST0, 0);
885 XE_OUTB(XE_TXST1, 0);
886 XE_SELECT_PAGE(0);
887
888 DEVPRINTF(3, (scp->dev,
889 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x\n", isr,
890 rst0, txst1, txst0));
891
892 if (isr & XE_ISR_TX_PACKET)
893 xe_txintr(scp, txst1);
894
895 if (isr & XE_ISR_MAC_INTR)
896 xe_macintr(scp, rst0, txst0, txst1);
897
898 xe_rxintr(scp, rst0);
899 }
900
901 /* Restore saved page */
902 XE_SELECT_PAGE(psr);
903
904 /* Re-enable interrupts */
905 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
906
907 XE_UNLOCK(scp);
908}
909
910/*
911 * Device timeout/watchdog routine. Called automatically if we queue a packet
912 * for transmission but don't get an interrupt within a specified timeout
913 * (usually 5 seconds). When this happens we assume the worst and reset the
914 * card.
915 */
916static void
917xe_watchdog(void *arg)
918{
919 struct xe_softc *scp = arg;
920
921 XE_ASSERT_LOCKED(scp);
922
923 if (scp->tx_timeout && --scp->tx_timeout == 0) {
924 device_printf(scp->dev, "watchdog timeout: resetting card\n");
925 scp->tx_timeouts++;
926 if_inc_counter(scp->ifp, IFCOUNTER_OERRORS, scp->tx_queued);
927 xe_stop(scp);
928 xe_reset(scp);
929 xe_init_locked(scp);
930 }
931 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
932}
933
934/*
935 * Change media selection.
936 */
937static int
938xe_media_change(struct ifnet *ifp)
939{
940 struct xe_softc *scp = ifp->if_softc;
941
942 DEVPRINTF(2, (scp->dev, "media_change\n"));
943
944 XE_LOCK(scp);
945 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER) {
946 XE_UNLOCK(scp);
947 return(EINVAL);
948 }
949
950 /*
951 * Some card/media combos aren't always possible -- filter
952 * those out here.
953 */
954 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
955 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok) {
956 XE_UNLOCK(scp);
957 return (EINVAL);
958 }
959
960 xe_setmedia(scp);
961 XE_UNLOCK(scp);
962
963 return (0);
964}
965
966/*
967 * Return current media selection.
968 */
969static void
970xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp)
971{
972 struct xe_softc *scp = ifp->if_softc;
973
974 DEVPRINTF(3, (scp->dev, "media_status\n"));
975
976 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
977 XE_LOCK(scp);
978 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
979 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
980 XE_UNLOCK(scp);
981}
982
983/*
984 * Select active media.
985 */
986static void
987xe_setmedia(void *xscp)
988{
989 struct xe_softc *scp = xscp;
990 uint16_t bmcr, bmsr, anar, lpar;
991
992 DEVPRINTF(2, (scp->dev, "setmedia\n"));
993
994 XE_ASSERT_LOCKED(scp);
995
996 /* Cancel any pending timeout */
997 callout_stop(&scp->media_timer);
998 xe_disable_intr(scp);
999
1000 /* Select media */
1001 scp->media = IFM_ETHER;
1002 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
1003
1004 case IFM_AUTO: /* Autoselect media */
1005 scp->media = IFM_ETHER|IFM_AUTO;
1006
1007 /*
1008 * Autoselection is really awful. It goes something like this:
1009 *
1010 * Wait until the transmitter goes idle (2sec timeout).
1011 * Reset card
1012 * IF a 100Mbit PHY exists
1013 * Start NWAY autonegotiation (3.5sec timeout)
1014 * IF that succeeds
1015 * Select 100baseTX or 10baseT, whichever was detected
1016 * ELSE
1017 * Reset card
1018 * IF a 100Mbit PHY exists
1019 * Try to force a 100baseTX link (3sec timeout)
1020 * IF that succeeds
1021 * Select 100baseTX
1022 * ELSE
1023 * Disable the PHY
1024 * ENDIF
1025 * ENDIF
1026 * ENDIF
1027 * ENDIF
1028 * IF nothing selected so far
1029 * IF a 100Mbit PHY exists
1030 * Select 10baseT
1031 * ELSE
1032 * Select 10baseT or 10base2, whichever is connected
1033 * ENDIF
1034 * ENDIF
1035 */
1036 switch (scp->autoneg_status) {
1037 case XE_AUTONEG_NONE:
1038 DEVPRINTF(2, (scp->dev,
1039 "Waiting for idle transmitter\n"));
1040 scp->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1041 scp->autoneg_status = XE_AUTONEG_WAITING;
1042 /* FALL THROUGH */
1043 case XE_AUTONEG_WAITING:
1044 if (scp->tx_queued != 0) {
1045 callout_reset(&scp->media_timer, hz / 2,
1046 xe_setmedia, scp);
1047 return;
1048 }
1049 if (scp->phy_ok) {
1050 DEVPRINTF(2, (scp->dev,
1051 "Starting autonegotiation\n"));
1052 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1053 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1054 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1055 anar = xe_phy_readreg(scp, PHY_ANAR);
1056 anar &= ~(PHY_ANAR_100BT4 |
1057 PHY_ANAR_100BTXFULL | PHY_ANAR_10BTFULL);
1058 anar |= PHY_ANAR_100BTXHALF | PHY_ANAR_10BTHALF;
1059 xe_phy_writereg(scp, PHY_ANAR, anar);
1060 bmcr |= PHY_BMCR_AUTONEGENBL |
1061 PHY_BMCR_AUTONEGRSTR;
1062 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1063 scp->autoneg_status = XE_AUTONEG_STARTED;
1064 callout_reset(&scp->media_timer, hz * 7/2,
1065 xe_setmedia, scp);
1066 return;
1067 } else {
1068 scp->autoneg_status = XE_AUTONEG_FAIL;
1069 }
1070 break;
1071 case XE_AUTONEG_STARTED:
1072 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1073 lpar = xe_phy_readreg(scp, PHY_LPAR);
1074 if (bmsr & (PHY_BMSR_AUTONEGCOMP | PHY_BMSR_LINKSTAT)) {
1075 DEVPRINTF(2, (scp->dev,
1076 "Autonegotiation complete!\n"));
1077
1078 /*
1079 * XXX - Shouldn't have to do this,
1080 * but (on my hub at least) the
1081 * transmitter won't work after a
1082 * successful autoneg. So we see what
1083 * the negotiation result was and
1084 * force that mode. I'm sure there is
1085 * an easy fix for this.
1086 */
1087 if (lpar & PHY_LPAR_100BTXHALF) {
1088 xe_phy_writereg(scp, PHY_BMCR,
1089 PHY_BMCR_SPEEDSEL);
1090 XE_MII_DUMP(scp);
1091 XE_SELECT_PAGE(2);
1092 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1093 scp->media = IFM_ETHER | IFM_100_TX;
1094 scp->autoneg_status = XE_AUTONEG_NONE;
1095 } else {
1096 /*
1097 * XXX - Bit of a hack going
1098 * on in here. This is
1099 * derived from Ken Hughes
1100 * patch to the Linux driver
1101 * to make it work with 10Mbit
1102 * _autonegotiated_ links on
1103 * CE3B cards. What's a CE3B
1104 * and how's it differ from a
1105 * plain CE3? these are the
1106 * things we need to find out.
1107 */
1108 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1109 XE_SELECT_PAGE(2);
1110 /* BEGIN HACK */
1111 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1112 XE_SELECT_PAGE(0x42);
1113 XE_OUTB(XE_SWC1, 0x80);
1114 scp->media = IFM_ETHER | IFM_10_T;
1115 scp->autoneg_status = XE_AUTONEG_NONE;
1116 /* END HACK */
1117#if 0
1118 /* Display PHY? */
1119 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1120 scp->autoneg_status = XE_AUTONEG_FAIL;
1121#endif
1122 }
1123 } else {
1124 DEVPRINTF(2, (scp->dev,
1125 "Autonegotiation failed; trying 100baseTX\n"));
1126 XE_MII_DUMP(scp);
1127 if (scp->phy_ok) {
1128 xe_phy_writereg(scp, PHY_BMCR,
1129 PHY_BMCR_SPEEDSEL);
1130 scp->autoneg_status = XE_AUTONEG_100TX;
1131 callout_reset(&scp->media_timer, hz * 3,
1132 xe_setmedia, scp);
1133 return;
1134 } else {
1135 scp->autoneg_status = XE_AUTONEG_FAIL;
1136 }
1137 }
1138 break;
1139 case XE_AUTONEG_100TX:
1140 (void)xe_phy_readreg(scp, PHY_BMSR);
1141 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1142 if (bmsr & PHY_BMSR_LINKSTAT) {
1143 DEVPRINTF(2, (scp->dev,
1144 "Got 100baseTX link!\n"));
1145 XE_MII_DUMP(scp);
1146 XE_SELECT_PAGE(2);
1147 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1148 scp->media = IFM_ETHER | IFM_100_TX;
1149 scp->autoneg_status = XE_AUTONEG_NONE;
1150 } else {
1151 DEVPRINTF(2, (scp->dev,
1152 "Autonegotiation failed; disabling PHY\n"));
1153 XE_MII_DUMP(scp);
1154 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1155 XE_SELECT_PAGE(2);
1156
1157 /* Disable PHY? */
1158 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1159 scp->autoneg_status = XE_AUTONEG_FAIL;
1160 }
1161 break;
1162 }
1163
1164 /*
1165 * If we got down here _and_ autoneg_status is
1166 * XE_AUTONEG_FAIL, then either autonegotiation
1167 * failed, or never got started to begin with. In
1168 * either case, select a suitable 10Mbit media and
1169 * hope it works. We don't need to reset the card
1170 * again, since it will have been done already by the
1171 * big switch above.
1172 */
1173 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1174 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1175 if (scp->mohawk) {
1176 XE_SELECT_PAGE(0x42);
1177 XE_OUTB(XE_SWC1, 0x80);
1178 scp->media = IFM_ETHER | IFM_10_T;
1179 scp->autoneg_status = XE_AUTONEG_NONE;
1180 } else {
1181 XE_SELECT_PAGE(4);
1182 XE_OUTB(XE_GPR0, 4);
1183 DELAY(50000);
1184 XE_SELECT_PAGE(0x42);
1185 XE_OUTB(XE_SWC1,
1186 (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ?
1187 0x80 : 0xc0);
1188 scp->media = IFM_ETHER | ((XE_INB(XE_ESR) &
1189 XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1190 scp->autoneg_status = XE_AUTONEG_NONE;
1191 }
1192 }
1193 break;
1194
1195 /*
1196 * If a specific media has been requested, we just reset the
1197 * card and select it (one small exception -- if 100baseTX is
1198 * requested but there is no PHY, we fall back to 10baseT
1199 * operation).
1200 */
1201 case IFM_100_TX: /* Force 100baseTX */
1202 if (scp->phy_ok) {
1203 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1204 XE_SELECT_PAGE(0x42);
1205 XE_OUTB(XE_SWC1, 0);
1206 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1207 XE_SELECT_PAGE(2);
1208 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1209 scp->media |= IFM_100_TX;
1210 break;
1211 }
1212 /* FALLTHROUGH */
1213 case IFM_10_T: /* Force 10baseT */
1214 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1215 if (scp->phy_ok) {
1216 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1217 XE_SELECT_PAGE(2);
1218
1219 /* Disable PHY */
1220 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1221 }
1222 XE_SELECT_PAGE(0x42);
1223 XE_OUTB(XE_SWC1, 0x80);
1224 scp->media |= IFM_10_T;
1225 break;
1226 case IFM_10_2:
1227 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1228 XE_SELECT_PAGE(0x42);
1229 XE_OUTB(XE_SWC1, 0xc0);
1230 scp->media |= IFM_10_2;
1231 break;
1232 }
1233
1234 /*
1235 * Finally, the LEDs are set to match whatever media was
1236 * chosen and the transmitter is unblocked.
1237 */
1238 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1239 XE_SELECT_PAGE(2);
1240 switch (IFM_SUBTYPE(scp->media)) {
1241 case IFM_100_TX:
1242 case IFM_10_T:
1243 XE_OUTB(XE_LED, 0x3b);
1244 if (scp->dingo)
1245 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1246 break;
1247 case IFM_10_2:
1248 XE_OUTB(XE_LED, 0x3a);
1249 break;
1250 }
1251
1252 /* Restart output? */
1253 xe_enable_intr(scp);
1254 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1255 xe_start_locked(scp->ifp);
1256}
1257
1258/*
1259 * Hard reset (power cycle) the card.
1260 */
1261static void
1262xe_reset(struct xe_softc *scp)
1263{
1264
1265 DEVPRINTF(2, (scp->dev, "reset\n"));
1266
1267 XE_ASSERT_LOCKED(scp);
1268
1269 /* Power down */
1270 XE_SELECT_PAGE(4);
1271 XE_OUTB(XE_GPR1, 0);
1272 DELAY(40000);
1273
1274 /* Power up again */
1275 if (scp->mohawk)
1276 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1277 else
1278 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN | XE_GPR1_AIC);
1279
1280 DELAY(40000);
1281 XE_SELECT_PAGE(0);
1282}
1283
1284/*
1285 * Take interface offline. This is done by powering down the device, which I
1286 * assume means just shutting down the transceiver and Ethernet logic. This
1287 * requires a _hard_ reset to recover from, as we need to power up again.
1288 */
1289void
1290xe_stop(struct xe_softc *scp)
1291{
1292
1293 DEVPRINTF(2, (scp->dev, "stop\n"));
1294
1295 XE_ASSERT_LOCKED(scp);
1296
1297 /*
1298 * Shut off interrupts.
1299 */
1300 xe_disable_intr(scp);
1301
1302 /*
1303 * Power down.
1304 */
1305 XE_SELECT_PAGE(4);
1306 XE_OUTB(XE_GPR1, 0);
1307 XE_SELECT_PAGE(0);
1308 if (scp->mohawk) {
1309 /*
1310 * set GP1 and GP2 as outputs (bits 2 & 3)
1311 * set GP1 high to power on the ML6692 (bit 0)
1312 * set GP2 low to power on the 10Mhz chip (bit 1)
1313 */
1314 XE_SELECT_PAGE(4);
1315 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
1316 XE_GPR0_GP1_OUT);
1317 }
1318
1319 /*
1320 * ~IFF_DRV_RUNNING == interface down.
1321 */
1322 scp->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1323 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1324 scp->tx_timeout = 0;
1325 callout_stop(&scp->wdog_timer);
1326 callout_stop(&scp->media_timer);
1327}
1328
1329/*
1330 * Enable interrupts from the card.
1331 */
1332static void
1333xe_enable_intr(struct xe_softc *scp)
1334{
1335
1336 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1337
1338 XE_SELECT_PAGE(0);
1339 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1340 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1341 if (!(XE_INB(0x10) & 0x01)) {
1342 XE_OUTB(0x10, 0x11); /* Unmask master int enable */
1343 }
1344 }
1345}
1346
1347/*
1348 * Disable interrupts from the card.
1349 */
1350static void
1351xe_disable_intr(struct xe_softc *scp)
1352{
1353
1354 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1355
1356 XE_SELECT_PAGE(0);
1357 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1358 if (scp->modem && !scp->dingo) { /* More magic */
1359 XE_OUTB(0x10, 0x10); /* Mask the master int enable */
1360 }
1361}
1362
1363/*
1364 * Set up multicast filter and promiscuous modes.
1365 */
1366static void
1367xe_set_multicast(struct xe_softc *scp)
1368{
1369 struct ifnet *ifp;
1370 struct ifmultiaddr *maddr;
1371 unsigned count, i;
1372
1373 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1374
1375 ifp = scp->ifp;
1376 XE_SELECT_PAGE(0x42);
1377
1378 /* Handle PROMISC flag */
1379 if (ifp->if_flags & IFF_PROMISC) {
1380 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1381 return;
1382 } else
1383 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1384
1385 /* Handle ALLMULTI flag */
1386 if (ifp->if_flags & IFF_ALLMULTI) {
1387 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1388 return;
1389 } else
1390 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1391
1392 /* Iterate over multicast address list */
1393 count = 0;
1394 if_maddr_rlock(ifp);
1395 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1396 if (maddr->ifma_addr->sa_family != AF_LINK)
1397 continue;
1398
1399 count++;
1400
1401 if (count < 10)
1402 /*
1403 * First 9 use Individual Addresses for exact
1404 * matching.
1405 */
1406 xe_set_addr(scp,
1407 LLADDR((struct sockaddr_dl *)maddr->ifma_addr),
1408 count);
1409 else if (scp->mohawk)
1410 /* Use hash filter on Mohawk and Dingo */
1411 xe_mchash(scp,
1412 LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1413 else
1414 /* Nowhere else to put them on CE2 */
1415 break;
1416 }
1417 if_maddr_runlock(ifp);
1418
1419 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1420
1421 /* Now do some cleanup and enable multicast handling as needed */
1422 if (count == 0) {
1423 /* Disable all multicast handling */
1424 XE_SELECT_PAGE(0x42);
1425 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) &
1426 ~(XE_SWC1_IA_ENABLE | XE_SWC1_ALLMULTI));
1427 if (scp->mohawk) {
1428 XE_SELECT_PAGE(0x02);
1429 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1430 }
1431 } else if (count < 10) {
1432 /*
1433 * Full in any unused Individual Addresses with our
1434 * MAC address.
1435 */
1436 for (i = count + 1; i < 10; i++)
1437 xe_set_addr(scp, IF_LLADDR(scp->ifp), i);
1438
1439 /* Enable Individual Address matching only */
1440 XE_SELECT_PAGE(0x42);
1441 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1442 XE_SWC1_IA_ENABLE);
1443 if (scp->mohawk) {
1444 XE_SELECT_PAGE(0x02);
1445 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1446 }
1447 } else if (scp->mohawk) {
1448 /* Check whether hash table is full */
1449 XE_SELECT_PAGE(0x58);
1450 for (i = 0x08; i < 0x10; i++)
1451 if (XE_INB(i) != 0xff)
1452 break;
1453 if (i == 0x10) {
1454 /*
1455 * Hash table full - enable
1456 * promiscuous multicast matching
1457 */
1458 XE_SELECT_PAGE(0x42);
1459 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) &
1460 ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1461 XE_SELECT_PAGE(0x02);
1462 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1463 } else {
1464 /* Enable hash table and Individual Address matching */
1465 XE_SELECT_PAGE(0x42);
1466 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1467 XE_SWC1_IA_ENABLE);
1468 XE_SELECT_PAGE(0x02);
1469 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1470 }
1471 } else {
1472 /* Enable promiscuous multicast matching */
1473 XE_SELECT_PAGE(0x42);
1474 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) |
1475 XE_SWC1_ALLMULTI);
1476 }
1477
1478 XE_SELECT_PAGE(0);
1479}
1480
1481/*
1482 * Copy the Ethernet multicast address in addr to the on-chip registers for
1483 * Individual Address idx. Assumes that addr is really a multicast address
1484 * and that idx > 0 (slot 0 is always used for the card MAC address).
1485 */
1486static void
1487xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx)
1488{
1489 uint8_t page, reg;
1490 unsigned i;
1491
1492 /*
1493 * Individual Addresses are stored in registers 8-F of pages
1494 * 0x50-0x57. IA1 therefore starts at register 0xE on page
1495 * 0x50. The expressions below compute the starting page and
1496 * register for any IA index > 0.
1497 */
1498 --idx;
1499 page = 0x50 + idx % 4 + idx / 4 * 3;
1500 reg = 0x0e - 2 * (idx % 4);
1501
1502 DEVPRINTF(3, (scp->dev,
1503 "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n", idx + 1, page,
1504 reg));
1505
1506 /*
1507 * Copy the IA bytes. Note that the byte order is reversed
1508 * for Mohawk and Dingo wrt. CE2 hardware.
1509 */
1510 XE_SELECT_PAGE(page);
1511 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1512 if (i > 0) {
1513 DPRINTF(3, (":%02x", addr[i]));
1514 } else {
1515 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1516 }
1517 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1518 if (++reg == 0x10) {
1519 reg = 0x08;
1520 XE_SELECT_PAGE(++page);
1521 }
1522 }
1523 DPRINTF(3, ("\n"));
1524}
1525
1526/*
1527 * Set the appropriate bit in the multicast hash table for the supplied
1528 * Ethernet multicast address addr. Assumes that addr is really a multicast
1529 * address.
1530 */
1531static void
1532xe_mchash(struct xe_softc* scp, const uint8_t *addr)
1533{
1534 int bit;
1535 uint8_t byte, hash;
1536
1537 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1538
1539 /*
1540 * Top 3 bits of hash give register - 8, bottom 3 give bit
1541 * within register.
1542 */
1543 byte = hash >> 3 | 0x08;
1544 bit = 0x01 << (hash & 0x07);
1545
1546 DEVPRINTF(3, (scp->dev,
1547 "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash,
1548 byte, bit));
1549
1550 XE_SELECT_PAGE(0x58);
1551 XE_OUTB(byte, XE_INB(byte) | bit);
1552}
1553
1554/*
1555 * Write an outgoing packet to the card using programmed I/O.
1556 */
1557static int
1558xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp)
1559{
1560 unsigned len, pad;
1561 unsigned char wantbyte;
1562 uint8_t *data;
1563 uint8_t savebyte[2];
1564
1565 /* Get total packet length */
1566 if (mbp->m_flags & M_PKTHDR)
1567 len = mbp->m_pkthdr.len;
1568 else {
1569 struct mbuf* mbp2 = mbp;
1570 for (len = 0; mbp2 != NULL;
1571 len += mbp2->m_len, mbp2 = mbp2->m_next);
1572 }
1573
1574 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1575
1576 /* Packets < minimum length may need to be padded out */
1577 pad = 0;
1578 if (len < scp->tx_min) {
1579 pad = scp->tx_min - len;
1580 len = scp->tx_min;
1581 }
1582
1583 /* Check transmit buffer space */
1584 XE_SELECT_PAGE(0);
1585 XE_OUTW(XE_TRS, len + 2); /* Only effective on rev. 1 CE2 cards */
1586 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1587 return (1);
1588
1589 /* Send packet length to card */
1590 XE_OUTW(XE_EDP, len);
1591
1592 /*
1593 * Write packet to card using PIO (code stolen from the ed driver)
1594 */
1595 wantbyte = 0;
1596 while (mbp != NULL) {
1597 len = mbp->m_len;
1598 if (len > 0) {
1599 data = mtod(mbp, caddr_t);
1600 if (wantbyte) { /* Finish the last word */
1601 savebyte[1] = *data;
1602 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1603 data++;
1604 len--;
1605 wantbyte = 0;
1606 }
1607 if (len > 1) { /* Output contiguous words */
1608 bus_write_multi_2(scp->port_res, XE_EDP,
1609 (uint16_t *)data, len >> 1);
1610 data += len & ~1;
1611 len &= 1;
1612 }
1613 if (len == 1) { /* Save last byte, if needed */
1614 savebyte[0] = *data;
1615 wantbyte = 1;
1616 }
1617 }
1618 mbp = mbp->m_next;
1619 }
1620
1621 /*
1622 * Send last byte of odd-length packets
1623 */
1624 if (wantbyte)
1625 XE_OUTB(XE_EDP, savebyte[0]);
1626
1627 /*
1628 * Can just tell CE3 cards to send; short packets will be
1629 * padded out with random cruft automatically. For CE2,
1630 * manually pad the packet with garbage; it will be sent when
1631 * the required number of bytes have been delivered to the
1632 * card.
1633 */
1634 if (scp->mohawk)
1635 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX |
1636 XE_CR_ENABLE_INTR);
1637 else if (pad > 0) {
1638 if (pad & 0x01)
1639 XE_OUTB(XE_EDP, 0xaa);
1640 pad >>= 1;
1641 while (pad > 0) {
1642 XE_OUTW(XE_EDP, 0xdead);
1643 pad--;
1644 }
1645 }
1646
1647 return (0);
1648}
1649
1650/**************************************************************
1651 * *
1652 * M I I F U N C T I O N S *
1653 * *
1654 **************************************************************/
1655
1656/*
1657 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1658 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1659 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1660 * XXX - some kind of common MII-handling code, shared by all drivers. But
1661 * XXX - that's a whole other mission.
1662 */
1663#define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1664#define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1665
1666/*
1667 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1668 */
1669static void
1670xe_mii_sync(struct xe_softc *scp)
1671{
1672 int i;
1673
1674 XE_SELECT_PAGE(2);
1675 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1676
1677 for (i = 0; i < 32; i++) {
1678 XE_MII_SET(XE_MII_CLK);
1679 DELAY(1);
1680 XE_MII_CLR(XE_MII_CLK);
1681 DELAY(1);
1682 }
1683}
1684
1685/*
1686 * Look for a MII-compliant PHY. If we find one, reset it.
1687 */
1688static int
1689xe_mii_init(struct xe_softc *scp)
1690{
1691 uint16_t status;
1692
1693 status = xe_phy_readreg(scp, PHY_BMSR);
1694 if ((status & 0xff00) != 0x7800) {
1695 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1696 return (0);
1697 } else {
1698 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1699
1700 /* Reset the PHY */
1701 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1702 DELAY(500);
1703 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET)
1704 ; /* nothing */
1705 XE_MII_DUMP(scp);
1706 return (1);
1707 }
1708}
1709
1710/*
1711 * Clock a series of bits through the MII.
1712 */
1713static void
1714xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt)
1715{
1716 int i;
1717
1718 XE_SELECT_PAGE(2);
1719 XE_MII_CLR(XE_MII_CLK);
1720
1721 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1722 if (bits & i) {
1723 XE_MII_SET(XE_MII_WRD);
1724 } else {
1725 XE_MII_CLR(XE_MII_WRD);
1726 }
1727 DELAY(1);
1728 XE_MII_CLR(XE_MII_CLK);
1729 DELAY(1);
1730 XE_MII_SET(XE_MII_CLK);
1731 }
1732}
1733
1734/*
1735 * Read an PHY register through the MII.
1736 */
1737static int
1738xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame)
1739{
1740 int i, ack;
1741
1742 XE_ASSERT_LOCKED(scp);
1743
1744 /*
1745 * Set up frame for RX.
1746 */
1747 frame->mii_stdelim = XE_MII_STARTDELIM;
1748 frame->mii_opcode = XE_MII_READOP;
1749 frame->mii_turnaround = 0;
1750 frame->mii_data = 0;
1751
1752 XE_SELECT_PAGE(2);
1753 XE_OUTB(XE_GPR2, 0);
1754
1755 /*
1756 * Turn on data xmit.
1757 */
1758 XE_MII_SET(XE_MII_DIR);
1759
1760 xe_mii_sync(scp);
1761
1762 /*
1763 * Send command/address info.
1764 */
1765 xe_mii_send(scp, frame->mii_stdelim, 2);
1766 xe_mii_send(scp, frame->mii_opcode, 2);
1767 xe_mii_send(scp, frame->mii_phyaddr, 5);
1768 xe_mii_send(scp, frame->mii_regaddr, 5);
1769
1770 /* Idle bit */
1771 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1772 DELAY(1);
1773 XE_MII_SET(XE_MII_CLK);
1774 DELAY(1);
1775
1776 /* Turn off xmit. */
1777 XE_MII_CLR(XE_MII_DIR);
1778
1779 /* Check for ack */
1780 XE_MII_CLR(XE_MII_CLK);
1781 DELAY(1);
1782 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1783 XE_MII_SET(XE_MII_CLK);
1784 DELAY(1);
1785
1786 /*
1787 * Now try reading data bits. If the ack failed, we still
1788 * need to clock through 16 cycles to keep the PHY(s) in sync.
1789 */
1790 if (ack) {
1791 for(i = 0; i < 16; i++) {
1792 XE_MII_CLR(XE_MII_CLK);
1793 DELAY(1);
1794 XE_MII_SET(XE_MII_CLK);
1795 DELAY(1);
1796 }
1797 goto fail;
1798 }
1799
1800 for (i = 0x8000; i; i >>= 1) {
1801 XE_MII_CLR(XE_MII_CLK);
1802 DELAY(1);
1803 if (!ack) {
1804 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1805 frame->mii_data |= i;
1806 DELAY(1);
1807 }
1808 XE_MII_SET(XE_MII_CLK);
1809 DELAY(1);
1810 }
1811
1812fail:
1813 XE_MII_CLR(XE_MII_CLK);
1814 DELAY(1);
1815 XE_MII_SET(XE_MII_CLK);
1816 DELAY(1);
1817
1818 if (ack)
1819 return(1);
1820 return(0);
1821}
1822
1823/*
1824 * Write to a PHY register through the MII.
1825 */
1826static int
1827xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame)
1828{
1829
1830 XE_ASSERT_LOCKED(scp);
1831
1832 /*
1833 * Set up frame for TX.
1834 */
1835 frame->mii_stdelim = XE_MII_STARTDELIM;
1836 frame->mii_opcode = XE_MII_WRITEOP;
1837 frame->mii_turnaround = XE_MII_TURNAROUND;
1838
1839 XE_SELECT_PAGE(2);
1840
1841 /*
1842 * Turn on data output.
1843 */
1844 XE_MII_SET(XE_MII_DIR);
1845
1846 xe_mii_sync(scp);
1847
1848 xe_mii_send(scp, frame->mii_stdelim, 2);
1849 xe_mii_send(scp, frame->mii_opcode, 2);
1850 xe_mii_send(scp, frame->mii_phyaddr, 5);
1851 xe_mii_send(scp, frame->mii_regaddr, 5);
1852 xe_mii_send(scp, frame->mii_turnaround, 2);
1853 xe_mii_send(scp, frame->mii_data, 16);
1854
1855 /* Idle bit. */
1856 XE_MII_SET(XE_MII_CLK);
1857 DELAY(1);
1858 XE_MII_CLR(XE_MII_CLK);
1859 DELAY(1);
1860
1861 /*
1862 * Turn off xmit.
1863 */
1864 XE_MII_CLR(XE_MII_DIR);
1865
1866 return(0);
1867}
1868
1869/*
1870 * Read a register from the PHY.
1871 */
1872static uint16_t
1873xe_phy_readreg(struct xe_softc *scp, uint16_t reg)
1874{
1875 struct xe_mii_frame frame;
1876
1877 bzero((char *)&frame, sizeof(frame));
1878
1879 frame.mii_phyaddr = 0;
1880 frame.mii_regaddr = reg;
1881 xe_mii_readreg(scp, &frame);
1882
1883 return (frame.mii_data);
1884}
1885
1886/*
1887 * Write to a PHY register.
1888 */
1889static void
1890xe_phy_writereg(struct xe_softc *scp, uint16_t reg, uint16_t data)
1891{
1892 struct xe_mii_frame frame;
1893
1894 bzero((char *)&frame, sizeof(frame));
1895
1896 frame.mii_phyaddr = 0;
1897 frame.mii_regaddr = reg;
1898 frame.mii_data = data;
1899 xe_mii_writereg(scp, &frame);
1900}
1901
1902/*
1903 * A bit of debugging code.
1904 */
1905static void
1906xe_mii_dump(struct xe_softc *scp)
1907{
1908 int i;
1909
1910 device_printf(scp->dev, "MII registers: ");
1911 for (i = 0; i < 2; i++) {
1912 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1913 }
1914 for (i = 4; i < 7; i++) {
1915 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1916 }
1917 printf("\n");
1918}
1919
1920#if 0
1921void
1922xe_reg_dump(struct xe_softc *scp)
1923{
1924 int page, i;
1925
1926 device_printf(scp->dev, "Common registers: ");
1927 for (i = 0; i < 8; i++) {
1928 printf(" %2.2x", XE_INB(i));
1929 }
1930 printf("\n");
1931
1932 for (page = 0; page <= 8; page++) {
1933 device_printf(scp->dev, "Register page %2.2x: ", page);
1934 XE_SELECT_PAGE(page);
1935 for (i = 8; i < 16; i++) {
1936 printf(" %2.2x", XE_INB(i));
1937 }
1938 printf("\n");
1939 }
1940
1941 for (page = 0x10; page < 0x5f; page++) {
1942 if ((page >= 0x11 && page <= 0x3f) ||
1943 (page == 0x41) ||
1944 (page >= 0x43 && page <= 0x4f) ||
1945 (page >= 0x59))
1946 continue;
1947 device_printf(scp->dev, "Register page %2.2x: ", page);
1948 XE_SELECT_PAGE(page);
1949 for (i = 8; i < 16; i++) {
1950 printf(" %2.2x", XE_INB(i));
1951 }
1952 printf("\n");
1953 }
1954}
1955#endif
1956
1957int
1958xe_activate(device_t dev)
1959{
1960 struct xe_softc *sc = device_get_softc(dev);
1961 int start, i;
1962
1963 DEVPRINTF(2, (dev, "activate\n"));
1964
1965 if (!sc->modem) {
1966 sc->port_rid = 0; /* 0 is managed by pccard */
1967 sc->port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
1968 &sc->port_rid, 16, RF_ACTIVE);
1969 } else if (sc->dingo) {
1970 /*
1971 * Find a 16 byte aligned ioport for the card.
1972 */
1973 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1974 sc->port_rid = 1; /* 0 is managed by pccard */
1975 start = 0x100;
1976 do {
1977 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1978 &sc->port_rid, start, 0x3ff, 16, RF_ACTIVE);
1979 if (sc->port_res == NULL)
1980 break;
1981 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1982 break;
1983 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1984 sc->port_res);
1985 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1986 } while (1);
1987 DEVPRINTF(1, (dev, "RealPort port 0x%0jx, size 0x%0jx\n",
1988 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1989 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1990 } else if (sc->ce2) {
1991 /*
1992 * Find contiguous I/O port for the Ethernet function
1993 * on CEM2 and CEM3 cards. We allocate window 0
1994 * wherever pccard has decided it should be, then find
1995 * an available window adjacent to it for the second
1996 * function. Not sure that both windows are actually
1997 * needed.
1998 */
1999 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
2000 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
2001 sc->ce2_port_res = bus_alloc_resource_anywhere(dev,
2002 SYS_RES_IOPORT, &sc->ce2_port_rid, 8, RF_ACTIVE);
2003 if (sc->ce2_port_res == NULL) {
2004 DEVPRINTF(1, (dev,
2005 "Cannot allocate I/O port for modem\n"));
2006 xe_deactivate(dev);
2007 return (ENOMEM);
2008 }
2009
2010 sc->port_rid = 1;
2011 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
2012 sc->ce2_port_rid);
2013 for (i = 0; i < 2; i++) {
2014 start += (i == 0 ? 8 : -24);
2015 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2016 &sc->port_rid, start, start + 15, 16, RF_ACTIVE);
2017 if (sc->port_res == NULL)
2018 continue;
2019 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
2020 sc->port_rid) == start)
2021 break;
2022
2023 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2024 sc->port_res);
2025 sc->port_res = NULL;
2026 }
2027 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0jx, size 0x%0jx\n",
2028 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
2029 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
2030 }
2031
2032 if (!sc->port_res) {
2033 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
2034 xe_deactivate(dev);
2035 return (ENOMEM);
2036 }
2037
2038 sc->irq_rid = 0;
2039 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2040 RF_ACTIVE);
2041 if (sc->irq_res == NULL) {
2042 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
2043 xe_deactivate(dev);
2044 return (ENOMEM);
2045 }
2046
2047 return (0);
2048}
2049
2050void
2051xe_deactivate(device_t dev)
2052{
2053 struct xe_softc *sc = device_get_softc(dev);
2054
2055 DEVPRINTF(2, (dev, "deactivate\n"));
2056 if (sc->intrhand)
2057 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2058 sc->intrhand = NULL;
2059 if (sc->port_res)
2060 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2061 sc->port_res);
2062 sc->port_res = NULL;
2063 if (sc->ce2_port_res)
2064 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2065 sc->ce2_port_res);
2066 sc->ce2_port_res = NULL;
2067 if (sc->irq_res)
2068 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
2069 sc->irq_res);
2070 sc->irq_res = NULL;
2071 if (sc->ifp)
2072 if_free(sc->ifp);
2073 sc->ifp = NULL;
2074}
320 /* Done */
321 return (0);
322}
323
324/*
325 * Complete hardware intitialisation and enable output. Exits without doing
326 * anything if there's no address assigned to the card, or if media selection
327 * is in progress (the latter implies we've already run this function).
328 */
329static void
330xe_init(void *xscp)
331{
332 struct xe_softc *scp = xscp;
333
334 XE_LOCK(scp);
335 xe_init_locked(scp);
336 XE_UNLOCK(scp);
337}
338
339static void
340xe_init_locked(struct xe_softc *scp)
341{
342 unsigned i;
343
344 if (scp->autoneg_status != XE_AUTONEG_NONE)
345 return;
346
347 DEVPRINTF(2, (scp->dev, "init\n"));
348
349 /* Reset transmitter flags */
350 scp->tx_queued = 0;
351 scp->tx_tpr = 0;
352 scp->tx_timeouts = 0;
353 scp->tx_thres = 64;
354 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
355 scp->tx_timeout = 0;
356
357 /* Soft reset the card */
358 XE_SELECT_PAGE(0);
359 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
360 DELAY(40000);
361 XE_OUTB(XE_CR, 0);
362 DELAY(40000);
363
364 if (scp->mohawk) {
365 /*
366 * set GP1 and GP2 as outputs (bits 2 & 3)
367 * set GP1 low to power on the ML6692 (bit 0)
368 * set GP2 high to power on the 10Mhz chip (bit 1)
369 */
370 XE_SELECT_PAGE(4);
371 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
372 XE_GPR0_GP2_OUT);
373 }
374
375 /* Shut off interrupts */
376 xe_disable_intr(scp);
377
378 /* Wait for everything to wake up */
379 DELAY(500000);
380
381 /* Check for PHY */
382 if (scp->mohawk)
383 scp->phy_ok = xe_mii_init(scp);
384
385 /* Disable 'source insertion' (not sure what that means) */
386 XE_SELECT_PAGE(0x42);
387 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
388
389 /* Set 8K/24K Tx/Rx buffer split */
390 if (scp->srev != 1) {
391 XE_SELECT_PAGE(2);
392 XE_OUTW(XE_RBS, 0x2000);
393 }
394
395 /* Enable early transmit mode on Mohawk/Dingo */
396 if (scp->mohawk) {
397 XE_SELECT_PAGE(0x03);
398 XE_OUTW(XE_TPT, scp->tx_thres);
399 XE_SELECT_PAGE(0x01);
400 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
401 }
402
403 /* Put MAC address in first 'individual address' register */
404 XE_SELECT_PAGE(0x50);
405 for (i = 0; i < ETHER_ADDR_LEN; i++)
406 XE_OUTB(0x08 + i, IF_LLADDR(scp->ifp)[scp->mohawk ? 5 - i : i]);
407
408 /* Set up multicast addresses */
409 xe_set_multicast(scp);
410
411 /* Fix the receive data offset -- reset can leave it off-by-one */
412 XE_SELECT_PAGE(0);
413 XE_OUTW(XE_DO, 0x2000);
414
415 /* Set interrupt masks */
416 XE_SELECT_PAGE(1);
417 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR |
418 XE_IMR0_RX_PACKET);
419
420 /* Set MAC interrupt masks */
421 XE_SELECT_PAGE(0x40);
422 XE_OUTB(XE_RX0Msk,
423 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR | XE_RX0M_ALIGN_ERROR |
424 XE_RX0M_LONG_PACKET));
425 XE_OUTB(XE_TX0Msk,
426 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN |
427 XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
428
429 /* Clear MAC status registers */
430 XE_SELECT_PAGE(0x40);
431 XE_OUTB(XE_RST0, 0x00);
432 XE_OUTB(XE_TXST0, 0x00);
433
434 /* Enable receiver and put MAC online */
435 XE_SELECT_PAGE(0x40);
436 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
437
438 /* Set up IMR, enable interrupts */
439 xe_enable_intr(scp);
440
441 /* Start media selection */
442 xe_setmedia(scp);
443
444 /* Enable output */
445 scp->ifp->if_drv_flags |= IFF_DRV_RUNNING;
446 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
447 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
448}
449
450/*
451 * Start output on interface. Should be called at splimp() priority. Check
452 * that the output is idle (ie, IFF_DRV_OACTIVE is not set) before calling this
453 * function. If media selection is in progress we set IFF_DRV_OACTIVE ourselves
454 * and return immediately.
455 */
456static void
457xe_start(struct ifnet *ifp)
458{
459 struct xe_softc *scp = ifp->if_softc;
460
461 XE_LOCK(scp);
462 xe_start_locked(ifp);
463 XE_UNLOCK(scp);
464}
465
466static void
467xe_start_locked(struct ifnet *ifp)
468{
469 struct xe_softc *scp = ifp->if_softc;
470 struct mbuf *mbp;
471
472 if (scp->autoneg_status != XE_AUTONEG_NONE) {
473 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
474 return;
475 }
476
477 DEVPRINTF(3, (scp->dev, "start\n"));
478
479 /*
480 * Loop while there are packets to be sent, and space to send
481 * them.
482 */
483 for (;;) {
484 /* Suck a packet off the send queue */
485 IF_DEQUEUE(&ifp->if_snd, mbp);
486
487 if (mbp == NULL) {
488 /*
489 * We are using the !OACTIVE flag to indicate
490 * to the outside world that we can accept an
491 * additional packet rather than that the
492 * transmitter is _actually_ active. Indeed,
493 * the transmitter may be active, but if we
494 * haven't filled all the buffers with data
495 * then we still want to accept more.
496 */
497 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
498 return;
499 }
500
501 if (xe_pio_write_packet(scp, mbp) != 0) {
502 /* Push the packet back onto the queue */
503 IF_PREPEND(&ifp->if_snd, mbp);
504 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
505 return;
506 }
507
508 /* Tap off here if there is a bpf listener */
509 BPF_MTAP(ifp, mbp);
510
511 /* In case we don't hear from the card again... */
512 scp->tx_timeout = 5;
513 scp->tx_queued++;
514
515 m_freem(mbp);
516 }
517}
518
519/*
520 * Process an ioctl request. Adapted from the ed driver.
521 */
522static int
523xe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
524{
525 struct xe_softc *scp;
526 int error;
527
528 scp = ifp->if_softc;
529 error = 0;
530
531 switch (command) {
532 case SIOCSIFFLAGS:
533 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n",
534 ifp->if_flags));
535 /*
536 * If the interface is marked up and stopped, then
537 * start it. If it is marked down and running, then
538 * stop it.
539 */
540 XE_LOCK(scp);
541 if (ifp->if_flags & IFF_UP) {
542 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
543 xe_reset(scp);
544 xe_init_locked(scp);
545 }
546 } else {
547 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
548 xe_stop(scp);
549 }
550
551 /* handle changes to PROMISC/ALLMULTI flags */
552 xe_set_multicast(scp);
553 XE_UNLOCK(scp);
554 error = 0;
555 break;
556 case SIOCADDMULTI:
557 case SIOCDELMULTI:
558 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
559 /*
560 * Multicast list has (maybe) changed; set the
561 * hardware filters accordingly.
562 */
563 XE_LOCK(scp);
564 xe_set_multicast(scp);
565 XE_UNLOCK(scp);
566 error = 0;
567 break;
568 case SIOCSIFMEDIA:
569 case SIOCGIFMEDIA:
570 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
571 /*
572 * Someone wants to get/set media options.
573 */
574 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia,
575 command);
576 break;
577 default:
578 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
579 error = ether_ioctl(ifp, command, data);
580 }
581
582 return (error);
583}
584
585/*
586 * Card interrupt handler.
587 *
588 * This function is probably more complicated than it needs to be, as it
589 * attempts to deal with the case where multiple packets get sent between
590 * interrupts. This is especially annoying when working out the collision
591 * stats. Not sure whether this case ever really happens or not (maybe on a
592 * slow/heavily loaded machine?) so it's probably best to leave this like it
593 * is.
594 *
595 * Note that the crappy PIO used to get packets on and off the card means that
596 * you will spend a lot of time in this routine -- I can get my P150 to spend
597 * 90% of its time servicing interrupts if I really hammer the network. Could
598 * fix this, but then you'd start dropping/losing packets. The moral of this
599 * story? If you want good network performance _and_ some cycles left over to
600 * get your work done, don't buy a Xircom card. Or convince them to tell me
601 * how to do memory-mapped I/O :)
602 */
603static void
604xe_txintr(struct xe_softc *scp, uint8_t txst1)
605{
606 struct ifnet *ifp;
607 uint8_t tpr, sent, coll;
608
609 ifp = scp->ifp;
610
611 /* Update packet count, accounting for rollover */
612 tpr = XE_INB(XE_TPR);
613 sent = -scp->tx_tpr + tpr;
614
615 /* Update statistics if we actually sent anything */
616 if (sent > 0) {
617 coll = txst1 & XE_TXST1_RETRY_COUNT;
618 scp->tx_tpr = tpr;
619 scp->tx_queued -= sent;
620 if_inc_counter(ifp, IFCOUNTER_OPACKETS, sent);
621 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, coll);
622
623 /*
624 * According to the Xircom manual, Dingo will
625 * sometimes manage to transmit a packet with
626 * triggering an interrupt. If this happens, we have
627 * sent > 1 and the collision count only reflects
628 * collisions on the last packet sent (the one that
629 * triggered the interrupt). Collision stats might
630 * therefore be a bit low, but there doesn't seem to
631 * be anything we can do about that.
632 */
633 switch (coll) {
634 case 0:
635 break;
636 case 1:
637 scp->mibdata.dot3StatsSingleCollisionFrames++;
638 scp->mibdata.dot3StatsCollFrequencies[0]++;
639 break;
640 default:
641 scp->mibdata.dot3StatsMultipleCollisionFrames++;
642 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
643 }
644 }
645 scp->tx_timeout = 0;
646 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
647}
648
649/* Handle most MAC interrupts */
650static void
651xe_macintr(struct xe_softc *scp, uint8_t rst0, uint8_t txst0, uint8_t txst1)
652{
653 struct ifnet *ifp;
654
655 ifp = scp->ifp;
656
657#if 0
658 /* Carrier sense lost -- only in 10Mbit HDX mode */
659 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
660 /* XXX - Need to update media status here */
661 device_printf(scp->dev, "no carrier\n");
662 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
663 scp->mibdata.dot3StatsCarrierSenseErrors++;
664 }
665#endif
666 /* Excessive collisions -- try sending again */
667 if (txst0 & XE_TXST0_16_COLLISIONS) {
668 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 16);
669 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
670 scp->mibdata.dot3StatsExcessiveCollisions++;
671 scp->mibdata.dot3StatsMultipleCollisionFrames++;
672 scp->mibdata.dot3StatsCollFrequencies[15]++;
673 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
674 }
675
676 /* Transmit underrun -- increase early transmit threshold */
677 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
678 DEVPRINTF(1, (scp->dev, "transmit underrun"));
679 if (scp->tx_thres < ETHER_MAX_LEN) {
680 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
681 scp->tx_thres = ETHER_MAX_LEN;
682 DPRINTF(1, (": increasing transmit threshold to %u",
683 scp->tx_thres));
684 XE_SELECT_PAGE(0x3);
685 XE_OUTW(XE_TPT, scp->tx_thres);
686 XE_SELECT_PAGE(0x0);
687 }
688 DPRINTF(1, ("\n"));
689 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
690 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
691 }
692
693 /* Late collision -- just complain about it */
694 if (txst0 & XE_TXST0_LATE_COLLISION) {
695 device_printf(scp->dev, "late collision\n");
696 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
697 scp->mibdata.dot3StatsLateCollisions++;
698 }
699
700 /* SQE test failure -- just complain about it */
701 if (txst0 & XE_TXST0_SQE_FAIL) {
702 device_printf(scp->dev, "SQE test failure\n");
703 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
704 scp->mibdata.dot3StatsSQETestErrors++;
705 }
706
707 /* Packet too long -- what happens to these */
708 if (rst0 & XE_RST0_LONG_PACKET) {
709 device_printf(scp->dev, "received giant packet\n");
710 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
711 scp->mibdata.dot3StatsFrameTooLongs++;
712 }
713
714 /* CRC error -- packet dropped */
715 if (rst0 & XE_RST0_CRC_ERROR) {
716 device_printf(scp->dev, "CRC error\n");
717 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
718 scp->mibdata.dot3StatsFCSErrors++;
719 }
720}
721
722static void
723xe_rxintr(struct xe_softc *scp, uint8_t rst0)
724{
725 struct ifnet *ifp;
726 uint8_t esr, rsr;
727
728 ifp = scp->ifp;
729
730 /* Handle received packet(s) */
731 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
732 rsr = XE_INB(XE_RSR);
733
734 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr,
735 rsr));
736
737 /* Make sure packet is a good one */
738 if (rsr & XE_RSR_RX_OK) {
739 struct ether_header *ehp;
740 struct mbuf *mbp;
741 uint16_t len;
742
743 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
744
745 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n",
746 len));
747
748 if (len == 0) {
749 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
750 continue;
751 }
752
753 /*
754 * Allocate mbuf to hold received packet. If
755 * the mbuf header isn't big enough, we attach
756 * an mbuf cluster to hold the packet. Note
757 * the +=2 to align the packet data on a
758 * 32-bit boundary, and the +3 to allow for
759 * the possibility of reading one more byte
760 * than the actual packet length (we always
761 * read 16-bit words). XXX - Surely there's a
762 * better way to do this alignment?
763 */
764 MGETHDR(mbp, M_NOWAIT, MT_DATA);
765 if (mbp == NULL) {
766 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
767 continue;
768 }
769
770 if (len + 3 > MHLEN) {
771 if (!(MCLGET(mbp, M_NOWAIT))) {
772 m_freem(mbp);
773 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
774 continue;
775 }
776 }
777
778 mbp->m_data += 2;
779 ehp = mtod(mbp, struct ether_header *);
780
781 /*
782 * Now get the packet in PIO mode, including
783 * the Ethernet header but omitting the
784 * trailing CRC.
785 */
786
787 /*
788 * Work around a bug in CE2 cards. There
789 * seems to be a problem with duplicated and
790 * extraneous bytes in the receive buffer, but
791 * without any real documentation for the CE2
792 * it's hard to tell for sure. XXX - Needs
793 * testing on CE2 hardware
794 */
795 if (scp->srev == 0) {
796 u_short rhs;
797
798 XE_SELECT_PAGE(5);
799 rhs = XE_INW(XE_RHSA);
800 XE_SELECT_PAGE(0);
801
802 rhs += 3; /* Skip control info */
803
804 if (rhs >= 0x8000)
805 rhs = 0;
806
807 if (rhs + len > 0x8000) {
808 int i;
809
810 for (i = 0; i < len; i++, rhs++) {
811 ((char *)ehp)[i] =
812 XE_INB(XE_EDP);
813 if (rhs == 0x8000) {
814 rhs = 0;
815 i--;
816 }
817 }
818 } else
819 bus_read_multi_2(scp->port_res, XE_EDP,
820 (uint16_t *)ehp, (len + 1) >> 1);
821 } else
822 bus_read_multi_2(scp->port_res, XE_EDP,
823 (uint16_t *)ehp, (len + 1) >> 1);
824
825 /* Deliver packet to upper layers */
826 mbp->m_pkthdr.rcvif = ifp;
827 mbp->m_pkthdr.len = mbp->m_len = len;
828 XE_UNLOCK(scp);
829 (*ifp->if_input)(ifp, mbp);
830 XE_LOCK(scp);
831 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
832
833 } else if (rsr & XE_RSR_ALIGN_ERROR) {
834 /* Packet alignment error -- drop packet */
835 device_printf(scp->dev, "alignment error\n");
836 scp->mibdata.dot3StatsAlignmentErrors++;
837 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
838 }
839
840 /* Skip to next packet, if there is one */
841 XE_OUTW(XE_DO, 0x8000);
842 }
843
844 /* Clear receiver overruns now we have some free buffer space */
845 if (rst0 & XE_RST0_RX_OVERRUN) {
846 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
847 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
848 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
849 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
850 }
851}
852
853static void
854xe_intr(void *xscp)
855{
856 struct xe_softc *scp = (struct xe_softc *) xscp;
857 struct ifnet *ifp;
858 uint8_t psr, isr, rst0, txst0, txst1;
859
860 ifp = scp->ifp;
861 XE_LOCK(scp);
862
863 /* Disable interrupts */
864 if (scp->mohawk)
865 XE_OUTB(XE_CR, 0);
866
867 /* Cache current register page */
868 psr = XE_INB(XE_PR);
869
870 /* Read ISR to see what caused this interrupt */
871 while ((isr = XE_INB(XE_ISR)) != 0) {
872
873 /* 0xff might mean the card is no longer around */
874 if (isr == 0xff) {
875 DEVPRINTF(3, (scp->dev,
876 "intr: interrupt received for missing card?\n"));
877 break;
878 }
879
880 /* Read other status registers */
881 XE_SELECT_PAGE(0x40);
882 rst0 = XE_INB(XE_RST0);
883 XE_OUTB(XE_RST0, 0);
884 txst0 = XE_INB(XE_TXST0);
885 txst1 = XE_INB(XE_TXST1);
886 XE_OUTB(XE_TXST0, 0);
887 XE_OUTB(XE_TXST1, 0);
888 XE_SELECT_PAGE(0);
889
890 DEVPRINTF(3, (scp->dev,
891 "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x\n", isr,
892 rst0, txst1, txst0));
893
894 if (isr & XE_ISR_TX_PACKET)
895 xe_txintr(scp, txst1);
896
897 if (isr & XE_ISR_MAC_INTR)
898 xe_macintr(scp, rst0, txst0, txst1);
899
900 xe_rxintr(scp, rst0);
901 }
902
903 /* Restore saved page */
904 XE_SELECT_PAGE(psr);
905
906 /* Re-enable interrupts */
907 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
908
909 XE_UNLOCK(scp);
910}
911
912/*
913 * Device timeout/watchdog routine. Called automatically if we queue a packet
914 * for transmission but don't get an interrupt within a specified timeout
915 * (usually 5 seconds). When this happens we assume the worst and reset the
916 * card.
917 */
918static void
919xe_watchdog(void *arg)
920{
921 struct xe_softc *scp = arg;
922
923 XE_ASSERT_LOCKED(scp);
924
925 if (scp->tx_timeout && --scp->tx_timeout == 0) {
926 device_printf(scp->dev, "watchdog timeout: resetting card\n");
927 scp->tx_timeouts++;
928 if_inc_counter(scp->ifp, IFCOUNTER_OERRORS, scp->tx_queued);
929 xe_stop(scp);
930 xe_reset(scp);
931 xe_init_locked(scp);
932 }
933 callout_reset(&scp->wdog_timer, hz, xe_watchdog, scp);
934}
935
936/*
937 * Change media selection.
938 */
939static int
940xe_media_change(struct ifnet *ifp)
941{
942 struct xe_softc *scp = ifp->if_softc;
943
944 DEVPRINTF(2, (scp->dev, "media_change\n"));
945
946 XE_LOCK(scp);
947 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER) {
948 XE_UNLOCK(scp);
949 return(EINVAL);
950 }
951
952 /*
953 * Some card/media combos aren't always possible -- filter
954 * those out here.
955 */
956 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
957 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok) {
958 XE_UNLOCK(scp);
959 return (EINVAL);
960 }
961
962 xe_setmedia(scp);
963 XE_UNLOCK(scp);
964
965 return (0);
966}
967
968/*
969 * Return current media selection.
970 */
971static void
972xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp)
973{
974 struct xe_softc *scp = ifp->if_softc;
975
976 DEVPRINTF(3, (scp->dev, "media_status\n"));
977
978 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
979 XE_LOCK(scp);
980 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
981 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
982 XE_UNLOCK(scp);
983}
984
985/*
986 * Select active media.
987 */
988static void
989xe_setmedia(void *xscp)
990{
991 struct xe_softc *scp = xscp;
992 uint16_t bmcr, bmsr, anar, lpar;
993
994 DEVPRINTF(2, (scp->dev, "setmedia\n"));
995
996 XE_ASSERT_LOCKED(scp);
997
998 /* Cancel any pending timeout */
999 callout_stop(&scp->media_timer);
1000 xe_disable_intr(scp);
1001
1002 /* Select media */
1003 scp->media = IFM_ETHER;
1004 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
1005
1006 case IFM_AUTO: /* Autoselect media */
1007 scp->media = IFM_ETHER|IFM_AUTO;
1008
1009 /*
1010 * Autoselection is really awful. It goes something like this:
1011 *
1012 * Wait until the transmitter goes idle (2sec timeout).
1013 * Reset card
1014 * IF a 100Mbit PHY exists
1015 * Start NWAY autonegotiation (3.5sec timeout)
1016 * IF that succeeds
1017 * Select 100baseTX or 10baseT, whichever was detected
1018 * ELSE
1019 * Reset card
1020 * IF a 100Mbit PHY exists
1021 * Try to force a 100baseTX link (3sec timeout)
1022 * IF that succeeds
1023 * Select 100baseTX
1024 * ELSE
1025 * Disable the PHY
1026 * ENDIF
1027 * ENDIF
1028 * ENDIF
1029 * ENDIF
1030 * IF nothing selected so far
1031 * IF a 100Mbit PHY exists
1032 * Select 10baseT
1033 * ELSE
1034 * Select 10baseT or 10base2, whichever is connected
1035 * ENDIF
1036 * ENDIF
1037 */
1038 switch (scp->autoneg_status) {
1039 case XE_AUTONEG_NONE:
1040 DEVPRINTF(2, (scp->dev,
1041 "Waiting for idle transmitter\n"));
1042 scp->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1043 scp->autoneg_status = XE_AUTONEG_WAITING;
1044 /* FALL THROUGH */
1045 case XE_AUTONEG_WAITING:
1046 if (scp->tx_queued != 0) {
1047 callout_reset(&scp->media_timer, hz / 2,
1048 xe_setmedia, scp);
1049 return;
1050 }
1051 if (scp->phy_ok) {
1052 DEVPRINTF(2, (scp->dev,
1053 "Starting autonegotiation\n"));
1054 bmcr = xe_phy_readreg(scp, PHY_BMCR);
1055 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
1056 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1057 anar = xe_phy_readreg(scp, PHY_ANAR);
1058 anar &= ~(PHY_ANAR_100BT4 |
1059 PHY_ANAR_100BTXFULL | PHY_ANAR_10BTFULL);
1060 anar |= PHY_ANAR_100BTXHALF | PHY_ANAR_10BTHALF;
1061 xe_phy_writereg(scp, PHY_ANAR, anar);
1062 bmcr |= PHY_BMCR_AUTONEGENBL |
1063 PHY_BMCR_AUTONEGRSTR;
1064 xe_phy_writereg(scp, PHY_BMCR, bmcr);
1065 scp->autoneg_status = XE_AUTONEG_STARTED;
1066 callout_reset(&scp->media_timer, hz * 7/2,
1067 xe_setmedia, scp);
1068 return;
1069 } else {
1070 scp->autoneg_status = XE_AUTONEG_FAIL;
1071 }
1072 break;
1073 case XE_AUTONEG_STARTED:
1074 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1075 lpar = xe_phy_readreg(scp, PHY_LPAR);
1076 if (bmsr & (PHY_BMSR_AUTONEGCOMP | PHY_BMSR_LINKSTAT)) {
1077 DEVPRINTF(2, (scp->dev,
1078 "Autonegotiation complete!\n"));
1079
1080 /*
1081 * XXX - Shouldn't have to do this,
1082 * but (on my hub at least) the
1083 * transmitter won't work after a
1084 * successful autoneg. So we see what
1085 * the negotiation result was and
1086 * force that mode. I'm sure there is
1087 * an easy fix for this.
1088 */
1089 if (lpar & PHY_LPAR_100BTXHALF) {
1090 xe_phy_writereg(scp, PHY_BMCR,
1091 PHY_BMCR_SPEEDSEL);
1092 XE_MII_DUMP(scp);
1093 XE_SELECT_PAGE(2);
1094 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1095 scp->media = IFM_ETHER | IFM_100_TX;
1096 scp->autoneg_status = XE_AUTONEG_NONE;
1097 } else {
1098 /*
1099 * XXX - Bit of a hack going
1100 * on in here. This is
1101 * derived from Ken Hughes
1102 * patch to the Linux driver
1103 * to make it work with 10Mbit
1104 * _autonegotiated_ links on
1105 * CE3B cards. What's a CE3B
1106 * and how's it differ from a
1107 * plain CE3? these are the
1108 * things we need to find out.
1109 */
1110 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1111 XE_SELECT_PAGE(2);
1112 /* BEGIN HACK */
1113 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1114 XE_SELECT_PAGE(0x42);
1115 XE_OUTB(XE_SWC1, 0x80);
1116 scp->media = IFM_ETHER | IFM_10_T;
1117 scp->autoneg_status = XE_AUTONEG_NONE;
1118 /* END HACK */
1119#if 0
1120 /* Display PHY? */
1121 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1122 scp->autoneg_status = XE_AUTONEG_FAIL;
1123#endif
1124 }
1125 } else {
1126 DEVPRINTF(2, (scp->dev,
1127 "Autonegotiation failed; trying 100baseTX\n"));
1128 XE_MII_DUMP(scp);
1129 if (scp->phy_ok) {
1130 xe_phy_writereg(scp, PHY_BMCR,
1131 PHY_BMCR_SPEEDSEL);
1132 scp->autoneg_status = XE_AUTONEG_100TX;
1133 callout_reset(&scp->media_timer, hz * 3,
1134 xe_setmedia, scp);
1135 return;
1136 } else {
1137 scp->autoneg_status = XE_AUTONEG_FAIL;
1138 }
1139 }
1140 break;
1141 case XE_AUTONEG_100TX:
1142 (void)xe_phy_readreg(scp, PHY_BMSR);
1143 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1144 if (bmsr & PHY_BMSR_LINKSTAT) {
1145 DEVPRINTF(2, (scp->dev,
1146 "Got 100baseTX link!\n"));
1147 XE_MII_DUMP(scp);
1148 XE_SELECT_PAGE(2);
1149 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1150 scp->media = IFM_ETHER | IFM_100_TX;
1151 scp->autoneg_status = XE_AUTONEG_NONE;
1152 } else {
1153 DEVPRINTF(2, (scp->dev,
1154 "Autonegotiation failed; disabling PHY\n"));
1155 XE_MII_DUMP(scp);
1156 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1157 XE_SELECT_PAGE(2);
1158
1159 /* Disable PHY? */
1160 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1161 scp->autoneg_status = XE_AUTONEG_FAIL;
1162 }
1163 break;
1164 }
1165
1166 /*
1167 * If we got down here _and_ autoneg_status is
1168 * XE_AUTONEG_FAIL, then either autonegotiation
1169 * failed, or never got started to begin with. In
1170 * either case, select a suitable 10Mbit media and
1171 * hope it works. We don't need to reset the card
1172 * again, since it will have been done already by the
1173 * big switch above.
1174 */
1175 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1176 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1177 if (scp->mohawk) {
1178 XE_SELECT_PAGE(0x42);
1179 XE_OUTB(XE_SWC1, 0x80);
1180 scp->media = IFM_ETHER | IFM_10_T;
1181 scp->autoneg_status = XE_AUTONEG_NONE;
1182 } else {
1183 XE_SELECT_PAGE(4);
1184 XE_OUTB(XE_GPR0, 4);
1185 DELAY(50000);
1186 XE_SELECT_PAGE(0x42);
1187 XE_OUTB(XE_SWC1,
1188 (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ?
1189 0x80 : 0xc0);
1190 scp->media = IFM_ETHER | ((XE_INB(XE_ESR) &
1191 XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1192 scp->autoneg_status = XE_AUTONEG_NONE;
1193 }
1194 }
1195 break;
1196
1197 /*
1198 * If a specific media has been requested, we just reset the
1199 * card and select it (one small exception -- if 100baseTX is
1200 * requested but there is no PHY, we fall back to 10baseT
1201 * operation).
1202 */
1203 case IFM_100_TX: /* Force 100baseTX */
1204 if (scp->phy_ok) {
1205 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1206 XE_SELECT_PAGE(0x42);
1207 XE_OUTB(XE_SWC1, 0);
1208 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1209 XE_SELECT_PAGE(2);
1210 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1211 scp->media |= IFM_100_TX;
1212 break;
1213 }
1214 /* FALLTHROUGH */
1215 case IFM_10_T: /* Force 10baseT */
1216 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1217 if (scp->phy_ok) {
1218 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1219 XE_SELECT_PAGE(2);
1220
1221 /* Disable PHY */
1222 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);
1223 }
1224 XE_SELECT_PAGE(0x42);
1225 XE_OUTB(XE_SWC1, 0x80);
1226 scp->media |= IFM_10_T;
1227 break;
1228 case IFM_10_2:
1229 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1230 XE_SELECT_PAGE(0x42);
1231 XE_OUTB(XE_SWC1, 0xc0);
1232 scp->media |= IFM_10_2;
1233 break;
1234 }
1235
1236 /*
1237 * Finally, the LEDs are set to match whatever media was
1238 * chosen and the transmitter is unblocked.
1239 */
1240 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1241 XE_SELECT_PAGE(2);
1242 switch (IFM_SUBTYPE(scp->media)) {
1243 case IFM_100_TX:
1244 case IFM_10_T:
1245 XE_OUTB(XE_LED, 0x3b);
1246 if (scp->dingo)
1247 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1248 break;
1249 case IFM_10_2:
1250 XE_OUTB(XE_LED, 0x3a);
1251 break;
1252 }
1253
1254 /* Restart output? */
1255 xe_enable_intr(scp);
1256 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1257 xe_start_locked(scp->ifp);
1258}
1259
1260/*
1261 * Hard reset (power cycle) the card.
1262 */
1263static void
1264xe_reset(struct xe_softc *scp)
1265{
1266
1267 DEVPRINTF(2, (scp->dev, "reset\n"));
1268
1269 XE_ASSERT_LOCKED(scp);
1270
1271 /* Power down */
1272 XE_SELECT_PAGE(4);
1273 XE_OUTB(XE_GPR1, 0);
1274 DELAY(40000);
1275
1276 /* Power up again */
1277 if (scp->mohawk)
1278 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1279 else
1280 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN | XE_GPR1_AIC);
1281
1282 DELAY(40000);
1283 XE_SELECT_PAGE(0);
1284}
1285
1286/*
1287 * Take interface offline. This is done by powering down the device, which I
1288 * assume means just shutting down the transceiver and Ethernet logic. This
1289 * requires a _hard_ reset to recover from, as we need to power up again.
1290 */
1291void
1292xe_stop(struct xe_softc *scp)
1293{
1294
1295 DEVPRINTF(2, (scp->dev, "stop\n"));
1296
1297 XE_ASSERT_LOCKED(scp);
1298
1299 /*
1300 * Shut off interrupts.
1301 */
1302 xe_disable_intr(scp);
1303
1304 /*
1305 * Power down.
1306 */
1307 XE_SELECT_PAGE(4);
1308 XE_OUTB(XE_GPR1, 0);
1309 XE_SELECT_PAGE(0);
1310 if (scp->mohawk) {
1311 /*
1312 * set GP1 and GP2 as outputs (bits 2 & 3)
1313 * set GP1 high to power on the ML6692 (bit 0)
1314 * set GP2 low to power on the 10Mhz chip (bit 1)
1315 */
1316 XE_SELECT_PAGE(4);
1317 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT | XE_GPR0_GP1_SELECT |
1318 XE_GPR0_GP1_OUT);
1319 }
1320
1321 /*
1322 * ~IFF_DRV_RUNNING == interface down.
1323 */
1324 scp->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1325 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1326 scp->tx_timeout = 0;
1327 callout_stop(&scp->wdog_timer);
1328 callout_stop(&scp->media_timer);
1329}
1330
1331/*
1332 * Enable interrupts from the card.
1333 */
1334static void
1335xe_enable_intr(struct xe_softc *scp)
1336{
1337
1338 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1339
1340 XE_SELECT_PAGE(0);
1341 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1342 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1343 if (!(XE_INB(0x10) & 0x01)) {
1344 XE_OUTB(0x10, 0x11); /* Unmask master int enable */
1345 }
1346 }
1347}
1348
1349/*
1350 * Disable interrupts from the card.
1351 */
1352static void
1353xe_disable_intr(struct xe_softc *scp)
1354{
1355
1356 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1357
1358 XE_SELECT_PAGE(0);
1359 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1360 if (scp->modem && !scp->dingo) { /* More magic */
1361 XE_OUTB(0x10, 0x10); /* Mask the master int enable */
1362 }
1363}
1364
1365/*
1366 * Set up multicast filter and promiscuous modes.
1367 */
1368static void
1369xe_set_multicast(struct xe_softc *scp)
1370{
1371 struct ifnet *ifp;
1372 struct ifmultiaddr *maddr;
1373 unsigned count, i;
1374
1375 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1376
1377 ifp = scp->ifp;
1378 XE_SELECT_PAGE(0x42);
1379
1380 /* Handle PROMISC flag */
1381 if (ifp->if_flags & IFF_PROMISC) {
1382 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1383 return;
1384 } else
1385 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1386
1387 /* Handle ALLMULTI flag */
1388 if (ifp->if_flags & IFF_ALLMULTI) {
1389 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1390 return;
1391 } else
1392 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1393
1394 /* Iterate over multicast address list */
1395 count = 0;
1396 if_maddr_rlock(ifp);
1397 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1398 if (maddr->ifma_addr->sa_family != AF_LINK)
1399 continue;
1400
1401 count++;
1402
1403 if (count < 10)
1404 /*
1405 * First 9 use Individual Addresses for exact
1406 * matching.
1407 */
1408 xe_set_addr(scp,
1409 LLADDR((struct sockaddr_dl *)maddr->ifma_addr),
1410 count);
1411 else if (scp->mohawk)
1412 /* Use hash filter on Mohawk and Dingo */
1413 xe_mchash(scp,
1414 LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1415 else
1416 /* Nowhere else to put them on CE2 */
1417 break;
1418 }
1419 if_maddr_runlock(ifp);
1420
1421 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1422
1423 /* Now do some cleanup and enable multicast handling as needed */
1424 if (count == 0) {
1425 /* Disable all multicast handling */
1426 XE_SELECT_PAGE(0x42);
1427 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) &
1428 ~(XE_SWC1_IA_ENABLE | XE_SWC1_ALLMULTI));
1429 if (scp->mohawk) {
1430 XE_SELECT_PAGE(0x02);
1431 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1432 }
1433 } else if (count < 10) {
1434 /*
1435 * Full in any unused Individual Addresses with our
1436 * MAC address.
1437 */
1438 for (i = count + 1; i < 10; i++)
1439 xe_set_addr(scp, IF_LLADDR(scp->ifp), i);
1440
1441 /* Enable Individual Address matching only */
1442 XE_SELECT_PAGE(0x42);
1443 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1444 XE_SWC1_IA_ENABLE);
1445 if (scp->mohawk) {
1446 XE_SELECT_PAGE(0x02);
1447 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1448 }
1449 } else if (scp->mohawk) {
1450 /* Check whether hash table is full */
1451 XE_SELECT_PAGE(0x58);
1452 for (i = 0x08; i < 0x10; i++)
1453 if (XE_INB(i) != 0xff)
1454 break;
1455 if (i == 0x10) {
1456 /*
1457 * Hash table full - enable
1458 * promiscuous multicast matching
1459 */
1460 XE_SELECT_PAGE(0x42);
1461 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) &
1462 ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1463 XE_SELECT_PAGE(0x02);
1464 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1465 } else {
1466 /* Enable hash table and Individual Address matching */
1467 XE_SELECT_PAGE(0x42);
1468 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) |
1469 XE_SWC1_IA_ENABLE);
1470 XE_SELECT_PAGE(0x02);
1471 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1472 }
1473 } else {
1474 /* Enable promiscuous multicast matching */
1475 XE_SELECT_PAGE(0x42);
1476 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) |
1477 XE_SWC1_ALLMULTI);
1478 }
1479
1480 XE_SELECT_PAGE(0);
1481}
1482
1483/*
1484 * Copy the Ethernet multicast address in addr to the on-chip registers for
1485 * Individual Address idx. Assumes that addr is really a multicast address
1486 * and that idx > 0 (slot 0 is always used for the card MAC address).
1487 */
1488static void
1489xe_set_addr(struct xe_softc *scp, uint8_t* addr, unsigned idx)
1490{
1491 uint8_t page, reg;
1492 unsigned i;
1493
1494 /*
1495 * Individual Addresses are stored in registers 8-F of pages
1496 * 0x50-0x57. IA1 therefore starts at register 0xE on page
1497 * 0x50. The expressions below compute the starting page and
1498 * register for any IA index > 0.
1499 */
1500 --idx;
1501 page = 0x50 + idx % 4 + idx / 4 * 3;
1502 reg = 0x0e - 2 * (idx % 4);
1503
1504 DEVPRINTF(3, (scp->dev,
1505 "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n", idx + 1, page,
1506 reg));
1507
1508 /*
1509 * Copy the IA bytes. Note that the byte order is reversed
1510 * for Mohawk and Dingo wrt. CE2 hardware.
1511 */
1512 XE_SELECT_PAGE(page);
1513 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1514 if (i > 0) {
1515 DPRINTF(3, (":%02x", addr[i]));
1516 } else {
1517 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1518 }
1519 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1520 if (++reg == 0x10) {
1521 reg = 0x08;
1522 XE_SELECT_PAGE(++page);
1523 }
1524 }
1525 DPRINTF(3, ("\n"));
1526}
1527
1528/*
1529 * Set the appropriate bit in the multicast hash table for the supplied
1530 * Ethernet multicast address addr. Assumes that addr is really a multicast
1531 * address.
1532 */
1533static void
1534xe_mchash(struct xe_softc* scp, const uint8_t *addr)
1535{
1536 int bit;
1537 uint8_t byte, hash;
1538
1539 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1540
1541 /*
1542 * Top 3 bits of hash give register - 8, bottom 3 give bit
1543 * within register.
1544 */
1545 byte = hash >> 3 | 0x08;
1546 bit = 0x01 << (hash & 0x07);
1547
1548 DEVPRINTF(3, (scp->dev,
1549 "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash,
1550 byte, bit));
1551
1552 XE_SELECT_PAGE(0x58);
1553 XE_OUTB(byte, XE_INB(byte) | bit);
1554}
1555
1556/*
1557 * Write an outgoing packet to the card using programmed I/O.
1558 */
1559static int
1560xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp)
1561{
1562 unsigned len, pad;
1563 unsigned char wantbyte;
1564 uint8_t *data;
1565 uint8_t savebyte[2];
1566
1567 /* Get total packet length */
1568 if (mbp->m_flags & M_PKTHDR)
1569 len = mbp->m_pkthdr.len;
1570 else {
1571 struct mbuf* mbp2 = mbp;
1572 for (len = 0; mbp2 != NULL;
1573 len += mbp2->m_len, mbp2 = mbp2->m_next);
1574 }
1575
1576 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1577
1578 /* Packets < minimum length may need to be padded out */
1579 pad = 0;
1580 if (len < scp->tx_min) {
1581 pad = scp->tx_min - len;
1582 len = scp->tx_min;
1583 }
1584
1585 /* Check transmit buffer space */
1586 XE_SELECT_PAGE(0);
1587 XE_OUTW(XE_TRS, len + 2); /* Only effective on rev. 1 CE2 cards */
1588 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1589 return (1);
1590
1591 /* Send packet length to card */
1592 XE_OUTW(XE_EDP, len);
1593
1594 /*
1595 * Write packet to card using PIO (code stolen from the ed driver)
1596 */
1597 wantbyte = 0;
1598 while (mbp != NULL) {
1599 len = mbp->m_len;
1600 if (len > 0) {
1601 data = mtod(mbp, caddr_t);
1602 if (wantbyte) { /* Finish the last word */
1603 savebyte[1] = *data;
1604 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1605 data++;
1606 len--;
1607 wantbyte = 0;
1608 }
1609 if (len > 1) { /* Output contiguous words */
1610 bus_write_multi_2(scp->port_res, XE_EDP,
1611 (uint16_t *)data, len >> 1);
1612 data += len & ~1;
1613 len &= 1;
1614 }
1615 if (len == 1) { /* Save last byte, if needed */
1616 savebyte[0] = *data;
1617 wantbyte = 1;
1618 }
1619 }
1620 mbp = mbp->m_next;
1621 }
1622
1623 /*
1624 * Send last byte of odd-length packets
1625 */
1626 if (wantbyte)
1627 XE_OUTB(XE_EDP, savebyte[0]);
1628
1629 /*
1630 * Can just tell CE3 cards to send; short packets will be
1631 * padded out with random cruft automatically. For CE2,
1632 * manually pad the packet with garbage; it will be sent when
1633 * the required number of bytes have been delivered to the
1634 * card.
1635 */
1636 if (scp->mohawk)
1637 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX |
1638 XE_CR_ENABLE_INTR);
1639 else if (pad > 0) {
1640 if (pad & 0x01)
1641 XE_OUTB(XE_EDP, 0xaa);
1642 pad >>= 1;
1643 while (pad > 0) {
1644 XE_OUTW(XE_EDP, 0xdead);
1645 pad--;
1646 }
1647 }
1648
1649 return (0);
1650}
1651
1652/**************************************************************
1653 * *
1654 * M I I F U N C T I O N S *
1655 * *
1656 **************************************************************/
1657
1658/*
1659 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1660 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1661 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1662 * XXX - some kind of common MII-handling code, shared by all drivers. But
1663 * XXX - that's a whole other mission.
1664 */
1665#define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1666#define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1667
1668/*
1669 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1670 */
1671static void
1672xe_mii_sync(struct xe_softc *scp)
1673{
1674 int i;
1675
1676 XE_SELECT_PAGE(2);
1677 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1678
1679 for (i = 0; i < 32; i++) {
1680 XE_MII_SET(XE_MII_CLK);
1681 DELAY(1);
1682 XE_MII_CLR(XE_MII_CLK);
1683 DELAY(1);
1684 }
1685}
1686
1687/*
1688 * Look for a MII-compliant PHY. If we find one, reset it.
1689 */
1690static int
1691xe_mii_init(struct xe_softc *scp)
1692{
1693 uint16_t status;
1694
1695 status = xe_phy_readreg(scp, PHY_BMSR);
1696 if ((status & 0xff00) != 0x7800) {
1697 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1698 return (0);
1699 } else {
1700 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1701
1702 /* Reset the PHY */
1703 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1704 DELAY(500);
1705 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET)
1706 ; /* nothing */
1707 XE_MII_DUMP(scp);
1708 return (1);
1709 }
1710}
1711
1712/*
1713 * Clock a series of bits through the MII.
1714 */
1715static void
1716xe_mii_send(struct xe_softc *scp, uint32_t bits, int cnt)
1717{
1718 int i;
1719
1720 XE_SELECT_PAGE(2);
1721 XE_MII_CLR(XE_MII_CLK);
1722
1723 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1724 if (bits & i) {
1725 XE_MII_SET(XE_MII_WRD);
1726 } else {
1727 XE_MII_CLR(XE_MII_WRD);
1728 }
1729 DELAY(1);
1730 XE_MII_CLR(XE_MII_CLK);
1731 DELAY(1);
1732 XE_MII_SET(XE_MII_CLK);
1733 }
1734}
1735
1736/*
1737 * Read an PHY register through the MII.
1738 */
1739static int
1740xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame)
1741{
1742 int i, ack;
1743
1744 XE_ASSERT_LOCKED(scp);
1745
1746 /*
1747 * Set up frame for RX.
1748 */
1749 frame->mii_stdelim = XE_MII_STARTDELIM;
1750 frame->mii_opcode = XE_MII_READOP;
1751 frame->mii_turnaround = 0;
1752 frame->mii_data = 0;
1753
1754 XE_SELECT_PAGE(2);
1755 XE_OUTB(XE_GPR2, 0);
1756
1757 /*
1758 * Turn on data xmit.
1759 */
1760 XE_MII_SET(XE_MII_DIR);
1761
1762 xe_mii_sync(scp);
1763
1764 /*
1765 * Send command/address info.
1766 */
1767 xe_mii_send(scp, frame->mii_stdelim, 2);
1768 xe_mii_send(scp, frame->mii_opcode, 2);
1769 xe_mii_send(scp, frame->mii_phyaddr, 5);
1770 xe_mii_send(scp, frame->mii_regaddr, 5);
1771
1772 /* Idle bit */
1773 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1774 DELAY(1);
1775 XE_MII_SET(XE_MII_CLK);
1776 DELAY(1);
1777
1778 /* Turn off xmit. */
1779 XE_MII_CLR(XE_MII_DIR);
1780
1781 /* Check for ack */
1782 XE_MII_CLR(XE_MII_CLK);
1783 DELAY(1);
1784 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1785 XE_MII_SET(XE_MII_CLK);
1786 DELAY(1);
1787
1788 /*
1789 * Now try reading data bits. If the ack failed, we still
1790 * need to clock through 16 cycles to keep the PHY(s) in sync.
1791 */
1792 if (ack) {
1793 for(i = 0; i < 16; i++) {
1794 XE_MII_CLR(XE_MII_CLK);
1795 DELAY(1);
1796 XE_MII_SET(XE_MII_CLK);
1797 DELAY(1);
1798 }
1799 goto fail;
1800 }
1801
1802 for (i = 0x8000; i; i >>= 1) {
1803 XE_MII_CLR(XE_MII_CLK);
1804 DELAY(1);
1805 if (!ack) {
1806 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1807 frame->mii_data |= i;
1808 DELAY(1);
1809 }
1810 XE_MII_SET(XE_MII_CLK);
1811 DELAY(1);
1812 }
1813
1814fail:
1815 XE_MII_CLR(XE_MII_CLK);
1816 DELAY(1);
1817 XE_MII_SET(XE_MII_CLK);
1818 DELAY(1);
1819
1820 if (ack)
1821 return(1);
1822 return(0);
1823}
1824
1825/*
1826 * Write to a PHY register through the MII.
1827 */
1828static int
1829xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame)
1830{
1831
1832 XE_ASSERT_LOCKED(scp);
1833
1834 /*
1835 * Set up frame for TX.
1836 */
1837 frame->mii_stdelim = XE_MII_STARTDELIM;
1838 frame->mii_opcode = XE_MII_WRITEOP;
1839 frame->mii_turnaround = XE_MII_TURNAROUND;
1840
1841 XE_SELECT_PAGE(2);
1842
1843 /*
1844 * Turn on data output.
1845 */
1846 XE_MII_SET(XE_MII_DIR);
1847
1848 xe_mii_sync(scp);
1849
1850 xe_mii_send(scp, frame->mii_stdelim, 2);
1851 xe_mii_send(scp, frame->mii_opcode, 2);
1852 xe_mii_send(scp, frame->mii_phyaddr, 5);
1853 xe_mii_send(scp, frame->mii_regaddr, 5);
1854 xe_mii_send(scp, frame->mii_turnaround, 2);
1855 xe_mii_send(scp, frame->mii_data, 16);
1856
1857 /* Idle bit. */
1858 XE_MII_SET(XE_MII_CLK);
1859 DELAY(1);
1860 XE_MII_CLR(XE_MII_CLK);
1861 DELAY(1);
1862
1863 /*
1864 * Turn off xmit.
1865 */
1866 XE_MII_CLR(XE_MII_DIR);
1867
1868 return(0);
1869}
1870
1871/*
1872 * Read a register from the PHY.
1873 */
1874static uint16_t
1875xe_phy_readreg(struct xe_softc *scp, uint16_t reg)
1876{
1877 struct xe_mii_frame frame;
1878
1879 bzero((char *)&frame, sizeof(frame));
1880
1881 frame.mii_phyaddr = 0;
1882 frame.mii_regaddr = reg;
1883 xe_mii_readreg(scp, &frame);
1884
1885 return (frame.mii_data);
1886}
1887
1888/*
1889 * Write to a PHY register.
1890 */
1891static void
1892xe_phy_writereg(struct xe_softc *scp, uint16_t reg, uint16_t data)
1893{
1894 struct xe_mii_frame frame;
1895
1896 bzero((char *)&frame, sizeof(frame));
1897
1898 frame.mii_phyaddr = 0;
1899 frame.mii_regaddr = reg;
1900 frame.mii_data = data;
1901 xe_mii_writereg(scp, &frame);
1902}
1903
1904/*
1905 * A bit of debugging code.
1906 */
1907static void
1908xe_mii_dump(struct xe_softc *scp)
1909{
1910 int i;
1911
1912 device_printf(scp->dev, "MII registers: ");
1913 for (i = 0; i < 2; i++) {
1914 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1915 }
1916 for (i = 4; i < 7; i++) {
1917 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1918 }
1919 printf("\n");
1920}
1921
1922#if 0
1923void
1924xe_reg_dump(struct xe_softc *scp)
1925{
1926 int page, i;
1927
1928 device_printf(scp->dev, "Common registers: ");
1929 for (i = 0; i < 8; i++) {
1930 printf(" %2.2x", XE_INB(i));
1931 }
1932 printf("\n");
1933
1934 for (page = 0; page <= 8; page++) {
1935 device_printf(scp->dev, "Register page %2.2x: ", page);
1936 XE_SELECT_PAGE(page);
1937 for (i = 8; i < 16; i++) {
1938 printf(" %2.2x", XE_INB(i));
1939 }
1940 printf("\n");
1941 }
1942
1943 for (page = 0x10; page < 0x5f; page++) {
1944 if ((page >= 0x11 && page <= 0x3f) ||
1945 (page == 0x41) ||
1946 (page >= 0x43 && page <= 0x4f) ||
1947 (page >= 0x59))
1948 continue;
1949 device_printf(scp->dev, "Register page %2.2x: ", page);
1950 XE_SELECT_PAGE(page);
1951 for (i = 8; i < 16; i++) {
1952 printf(" %2.2x", XE_INB(i));
1953 }
1954 printf("\n");
1955 }
1956}
1957#endif
1958
1959int
1960xe_activate(device_t dev)
1961{
1962 struct xe_softc *sc = device_get_softc(dev);
1963 int start, i;
1964
1965 DEVPRINTF(2, (dev, "activate\n"));
1966
1967 if (!sc->modem) {
1968 sc->port_rid = 0; /* 0 is managed by pccard */
1969 sc->port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
1970 &sc->port_rid, 16, RF_ACTIVE);
1971 } else if (sc->dingo) {
1972 /*
1973 * Find a 16 byte aligned ioport for the card.
1974 */
1975 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1976 sc->port_rid = 1; /* 0 is managed by pccard */
1977 start = 0x100;
1978 do {
1979 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1980 &sc->port_rid, start, 0x3ff, 16, RF_ACTIVE);
1981 if (sc->port_res == NULL)
1982 break;
1983 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1984 break;
1985 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1986 sc->port_res);
1987 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1988 } while (1);
1989 DEVPRINTF(1, (dev, "RealPort port 0x%0jx, size 0x%0jx\n",
1990 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1991 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1992 } else if (sc->ce2) {
1993 /*
1994 * Find contiguous I/O port for the Ethernet function
1995 * on CEM2 and CEM3 cards. We allocate window 0
1996 * wherever pccard has decided it should be, then find
1997 * an available window adjacent to it for the second
1998 * function. Not sure that both windows are actually
1999 * needed.
2000 */
2001 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
2002 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
2003 sc->ce2_port_res = bus_alloc_resource_anywhere(dev,
2004 SYS_RES_IOPORT, &sc->ce2_port_rid, 8, RF_ACTIVE);
2005 if (sc->ce2_port_res == NULL) {
2006 DEVPRINTF(1, (dev,
2007 "Cannot allocate I/O port for modem\n"));
2008 xe_deactivate(dev);
2009 return (ENOMEM);
2010 }
2011
2012 sc->port_rid = 1;
2013 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
2014 sc->ce2_port_rid);
2015 for (i = 0; i < 2; i++) {
2016 start += (i == 0 ? 8 : -24);
2017 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
2018 &sc->port_rid, start, start + 15, 16, RF_ACTIVE);
2019 if (sc->port_res == NULL)
2020 continue;
2021 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
2022 sc->port_rid) == start)
2023 break;
2024
2025 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2026 sc->port_res);
2027 sc->port_res = NULL;
2028 }
2029 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0jx, size 0x%0jx\n",
2030 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
2031 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
2032 }
2033
2034 if (!sc->port_res) {
2035 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
2036 xe_deactivate(dev);
2037 return (ENOMEM);
2038 }
2039
2040 sc->irq_rid = 0;
2041 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2042 RF_ACTIVE);
2043 if (sc->irq_res == NULL) {
2044 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
2045 xe_deactivate(dev);
2046 return (ENOMEM);
2047 }
2048
2049 return (0);
2050}
2051
2052void
2053xe_deactivate(device_t dev)
2054{
2055 struct xe_softc *sc = device_get_softc(dev);
2056
2057 DEVPRINTF(2, (dev, "deactivate\n"));
2058 if (sc->intrhand)
2059 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
2060 sc->intrhand = NULL;
2061 if (sc->port_res)
2062 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
2063 sc->port_res);
2064 sc->port_res = NULL;
2065 if (sc->ce2_port_res)
2066 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
2067 sc->ce2_port_res);
2068 sc->ce2_port_res = NULL;
2069 if (sc->irq_res)
2070 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
2071 sc->irq_res);
2072 sc->irq_res = NULL;
2073 if (sc->ifp)
2074 if_free(sc->ifp);
2075 sc->ifp = NULL;
2076}