if_xe.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /* $OpenBSD: if_xe.c,v 1.52 2015/07/08 07:21:50 mpi Exp $ */
  2. /*
  3. * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Niklas Hallqvist,
  17. * C Stone and Job de Haas.
  18. * 4. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * A driver for Xircom ethernet PC-cards.
  34. *
  35. * The driver has been inspired by the xirc2ps_cs.c driver found in Linux'
  36. * PCMCIA package written by Werner Koch <werner.koch@guug.de>:
  37. * [xirc2ps_cs.c wk 14.04.97] (1.31 1998/12/09 19:32:55)
  38. * I will note that no code was used verbatim from that driver as it is under
  39. * the much too strong GNU General Public License, it was only used as a
  40. * "specification" of sorts.
  41. * Other inspirations have been if_fxp.c, if_ep_pcmcia.c and elink3.c as
  42. * they were found in OpenBSD 2.4.
  43. */
  44. #include "bpfilter.h"
  45. #include <sys/param.h>
  46. #include <sys/systm.h>
  47. #include <sys/device.h>
  48. #include <sys/ioctl.h>
  49. #include <sys/mbuf.h>
  50. #include <sys/malloc.h>
  51. #include <sys/kernel.h>
  52. #include <sys/socket.h>
  53. #include <sys/syslog.h>
  54. #include <net/if.h>
  55. #include <net/if_media.h>
  56. #include <net/if_types.h>
  57. #include <netinet/in.h>
  58. #include <netinet/if_ether.h>
  59. #if NBPFILTER > 0
  60. #include <net/bpf.h>
  61. #endif
  62. /*
  63. * Maximum number of bytes to read per interrupt. Linux recommends
  64. * somewhere between 2000-22000.
  65. * XXX This is currently a hard maximum.
  66. */
  67. #define MAX_BYTES_INTR 12000
  68. #include <dev/mii/miivar.h>
  69. #include <dev/pcmcia/pcmciareg.h>
  70. #include <dev/pcmcia/pcmciavar.h>
  71. #include <dev/pcmcia/pcmciadevs.h>
  72. #include <dev/pcmcia/if_xereg.h>
  73. #ifdef __GNUC__
  74. #define INLINE __inline
  75. #else
  76. #define INLINE
  77. #endif /* __GNUC__ */
  78. #ifdef XEDEBUG
  79. #define XED_CONFIG 0x1
  80. #define XED_MII 0x2
  81. #define XED_INTR 0x4
  82. #define XED_FIFO 0x8
  83. #ifndef XEDEBUG_DEF
  84. #define XEDEBUG_DEF (XED_CONFIG|XED_INTR)
  85. #endif /* XEDEBUG_DEF */
  86. int xedebug = XEDEBUG_DEF;
  87. #define DPRINTF(cat, x) if (xedebug & (cat)) printf x
  88. #else /* XEDEBUG */
  89. #define DPRINTF(cat, x) (void)0
  90. #endif /* XEDEBUG */
  91. int xe_pcmcia_match(struct device *, void *, void *);
  92. void xe_pcmcia_attach(struct device *, struct device *, void *);
  93. int xe_pcmcia_detach(struct device *, int);
  94. int xe_pcmcia_activate(struct device *, int);
  95. /*
  96. * In case this chipset ever turns up out of pcmcia attachments (very
  97. * unlikely) do the driver splitup.
  98. */
  99. struct xe_softc {
  100. struct device sc_dev; /* Generic device info */
  101. u_int32_t sc_flags; /* Misc. flags */
  102. void *sc_ih; /* Interrupt handler */
  103. struct arpcom sc_arpcom; /* Ethernet common part */
  104. struct ifmedia sc_media; /* Media control */
  105. struct mii_data sc_mii; /* MII media information */
  106. int sc_all_mcasts; /* Receive all multicasts */
  107. bus_space_tag_t sc_bst; /* Bus cookie */
  108. bus_space_handle_t sc_bsh; /* Bus I/O handle */
  109. bus_size_t sc_offset; /* Offset of registers */
  110. u_int8_t sc_rev; /* Chip revision */
  111. };
  112. #define XEF_MOHAWK 0x001
  113. #define XEF_DINGO 0x002
  114. #define XEF_MODEM 0x004
  115. #define XEF_UNSUPPORTED 0x008
  116. #define XEF_CE 0x010
  117. #define XEF_CE2 0x020
  118. #define XEF_CE3 0x040
  119. #define XEF_CE33 0x080
  120. #define XEF_CE56 0x100
  121. struct xe_pcmcia_softc {
  122. struct xe_softc sc_xe; /* Generic device info */
  123. struct pcmcia_mem_handle sc_pcmh; /* PCMCIA memspace info */
  124. int sc_mem_window; /* mem window */
  125. struct pcmcia_io_handle sc_pcioh; /* iospace info */
  126. int sc_io_window; /* io window info */
  127. struct pcmcia_function *sc_pf; /* PCMCIA function */
  128. };
  129. /* Autoconfig definition of driver back-end */
  130. struct cfdriver xe_cd = {
  131. NULL, "xe", DV_IFNET
  132. };
  133. struct cfattach xe_pcmcia_ca = {
  134. sizeof (struct xe_pcmcia_softc), xe_pcmcia_match, xe_pcmcia_attach,
  135. xe_pcmcia_detach, xe_pcmcia_activate
  136. };
  137. void xe_cycle_power(struct xe_softc *);
  138. void xe_full_reset(struct xe_softc *);
  139. void xe_init(struct xe_softc *);
  140. int xe_intr(void *);
  141. int xe_ioctl(struct ifnet *, u_long, caddr_t);
  142. int xe_mdi_read(struct device *, int, int);
  143. void xe_mdi_write(struct device *, int, int, int);
  144. int xe_mediachange(struct ifnet *);
  145. void xe_mediastatus(struct ifnet *, struct ifmediareq *);
  146. int xe_pcmcia_funce_enaddr(struct device *, u_int8_t *);
  147. u_int32_t xe_pcmcia_interpret_manfid(struct device *);
  148. int xe_pcmcia_lan_nid_ciscallback(struct pcmcia_tuple *, void *);
  149. int xe_pcmcia_manfid_ciscallback(struct pcmcia_tuple *, void *);
  150. u_int16_t xe_get(struct xe_softc *);
  151. void xe_reset(struct xe_softc *);
  152. void xe_set_address(struct xe_softc *);
  153. void xe_start(struct ifnet *);
  154. void xe_statchg(struct device *);
  155. void xe_stop(struct xe_softc *);
  156. void xe_watchdog(struct ifnet *);
  157. #ifdef XEDEBUG
  158. void xe_reg_dump(struct xe_softc *);
  159. #endif /* XEDEBUG */
  160. int
  161. xe_pcmcia_match(parent, match, aux)
  162. struct device *parent;
  163. void *match, *aux;
  164. {
  165. struct pcmcia_attach_args *pa = aux;
  166. if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
  167. return (0);
  168. switch (pa->manufacturer) {
  169. case PCMCIA_VENDOR_COMPAQ:
  170. case PCMCIA_VENDOR_COMPAQ2:
  171. return (0);
  172. case PCMCIA_VENDOR_INTEL:
  173. case PCMCIA_VENDOR_XIRCOM:
  174. /* XXX Per-productid checking here. */
  175. return (1);
  176. default:
  177. return (0);
  178. }
  179. }
  180. void
  181. xe_pcmcia_attach(parent, self, aux)
  182. struct device *parent, *self;
  183. void *aux;
  184. {
  185. struct xe_pcmcia_softc *psc = (struct xe_pcmcia_softc *)self;
  186. struct xe_softc *sc = &psc->sc_xe;
  187. struct pcmcia_attach_args *pa = aux;
  188. struct pcmcia_function *pf = pa->pf;
  189. struct pcmcia_config_entry *cfe = NULL;
  190. struct ifnet *ifp;
  191. u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL;
  192. int state = 0;
  193. struct pcmcia_mem_handle pcmh;
  194. int ccr_window;
  195. bus_size_t ccr_offset;
  196. const char *intrstr;
  197. psc->sc_pf = pf;
  198. #if 0
  199. /* Figure out what card we are. */
  200. sc->sc_flags = xe_pcmcia_interpret_manfid(parent);
  201. #endif
  202. if (sc->sc_flags & XEF_UNSUPPORTED) {
  203. printf(": card unsupported\n");
  204. goto bad;
  205. }
  206. /* Tell the pcmcia framework where the CCR is. */
  207. pf->ccr_base = 0x800;
  208. pf->ccr_mask = 0x67;
  209. /* Fake a cfe. */
  210. SIMPLEQ_FIRST(&pa->pf->cfe_head) = cfe = (struct pcmcia_config_entry *)
  211. malloc(sizeof *cfe, M_DEVBUF, M_NOWAIT | M_ZERO);
  212. if (!cfe) {
  213. printf(": function enable failed\n");
  214. return;
  215. }
  216. /*
  217. * XXX Use preprocessor symbols instead.
  218. * Enable ethernet & its interrupts, wiring them to -INT
  219. * No I/O base.
  220. */
  221. cfe->number = 0x5;
  222. cfe->flags = 0; /* XXX Check! */
  223. cfe->iftype = PCMCIA_IFTYPE_IO;
  224. cfe->num_iospace = 0;
  225. cfe->num_memspace = 0;
  226. cfe->irqmask = 0x8eb0;
  227. /* Enable the card. */
  228. pcmcia_function_init(pa->pf, cfe);
  229. if (pcmcia_function_enable(pa->pf)) {
  230. printf(": function enable failed\n");
  231. goto bad;
  232. }
  233. state++;
  234. if (pcmcia_io_alloc(pa->pf, 0, 16, 16, &psc->sc_pcioh)) {
  235. printf(": io allocation failed\n");
  236. goto bad;
  237. }
  238. state++;
  239. if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, 16, &psc->sc_pcioh,
  240. &psc->sc_io_window)) {
  241. printf(": can't map io space\n");
  242. goto bad;
  243. }
  244. sc->sc_bst = psc->sc_pcioh.iot;
  245. sc->sc_bsh = psc->sc_pcioh.ioh;
  246. sc->sc_offset = 0;
  247. printf(" port 0x%lx/%d", psc->sc_pcioh.addr, 16);
  248. #if 0
  249. if (pcmcia_mem_alloc(pf, 16, &psc->sc_pcmh)) {
  250. printf(": pcmcia memory allocation failed\n");
  251. goto bad;
  252. }
  253. state++;
  254. if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, 0x300, 16, &psc->sc_pcmh,
  255. &sc->sc_offset, &psc->sc_mem_window)) {
  256. printf(": pcmcia memory mapping failed\n");
  257. goto bad;
  258. }
  259. sc->sc_bst = psc->sc_pcmh.memt;
  260. sc->sc_bsh = psc->sc_pcmh.memh;
  261. #endif
  262. /* Figure out what card we are. */
  263. sc->sc_flags = xe_pcmcia_interpret_manfid(parent);
  264. /*
  265. * Configuration as advised by DINGO documentation.
  266. * We only know about this flag after the manfid interpretation.
  267. * Dingo has some extra configuration registers in the CCR space.
  268. */
  269. if (sc->sc_flags & XEF_DINGO) {
  270. if (pcmcia_mem_alloc(pf, PCMCIA_CCR_SIZE_DINGO, &pcmh)) {
  271. DPRINTF(XED_CONFIG, ("bad mem alloc\n"));
  272. goto bad;
  273. }
  274. if (pcmcia_mem_map(pf, PCMCIA_MEM_ATTR, pf->ccr_base,
  275. PCMCIA_CCR_SIZE_DINGO, &pcmh, &ccr_offset,
  276. &ccr_window)) {
  277. DPRINTF(XED_CONFIG, ("bad mem map\n"));
  278. pcmcia_mem_free(pf, &pcmh);
  279. goto bad;
  280. }
  281. bus_space_write_1(pcmh.memt, pcmh.memh,
  282. ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
  283. bus_space_write_1(pcmh.memt, pcmh.memh,
  284. ccr_offset + PCMCIA_CCR_DCOR1,
  285. PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
  286. bus_space_write_1(pcmh.memt, pcmh.memh,
  287. ccr_offset + PCMCIA_CCR_DCOR2, 0);
  288. bus_space_write_1(pcmh.memt, pcmh.memh,
  289. ccr_offset + PCMCIA_CCR_DCOR3, 0);
  290. bus_space_write_1(pcmh.memt, pcmh.memh,
  291. ccr_offset + PCMCIA_CCR_DCOR4, 0);
  292. /* We don't need them anymore and can free them (I think). */
  293. pcmcia_mem_unmap(pf, ccr_window);
  294. pcmcia_mem_free(pf, &pcmh);
  295. }
  296. /*
  297. * Try to get the ethernet address from FUNCE/LAN_NID tuple.
  298. */
  299. if (xe_pcmcia_funce_enaddr(parent, myla))
  300. enaddr = myla;
  301. ifp = &sc->sc_arpcom.ac_if;
  302. if (enaddr)
  303. bcopy(enaddr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
  304. else {
  305. printf(", unable to get ethernet address\n");
  306. goto bad;
  307. }
  308. bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
  309. ifp->if_softc = sc;
  310. ifp->if_flags =
  311. IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
  312. ifp->if_ioctl = xe_ioctl;
  313. ifp->if_start = xe_start;
  314. ifp->if_watchdog = xe_watchdog;
  315. IFQ_SET_READY(&ifp->if_snd);
  316. /* Establish the interrupt. */
  317. sc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_NET, xe_intr, sc,
  318. sc->sc_dev.dv_xname);
  319. if (sc->sc_ih == NULL) {
  320. printf(", couldn't establish interrupt\n");
  321. goto bad;
  322. }
  323. intrstr = pcmcia_intr_string(psc->sc_pf, sc->sc_ih);
  324. printf("%s%s: address %s\n", *intrstr ? ", " : "", intrstr,
  325. ether_sprintf(sc->sc_arpcom.ac_enaddr));
  326. /* Reset and initialize the card. */
  327. xe_full_reset(sc);
  328. /* Initialize our media structures and probe the phy. */
  329. sc->sc_mii.mii_ifp = ifp;
  330. sc->sc_mii.mii_readreg = xe_mdi_read;
  331. sc->sc_mii.mii_writereg = xe_mdi_write;
  332. sc->sc_mii.mii_statchg = xe_statchg;
  333. ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, xe_mediachange,
  334. xe_mediastatus);
  335. DPRINTF(XED_MII | XED_CONFIG,
  336. ("bmsr %x\n", xe_mdi_read(&sc->sc_dev, 0, 1)));
  337. mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
  338. 0);
  339. if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
  340. ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
  341. NULL);
  342. ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
  343. /*
  344. * Attach the interface.
  345. */
  346. if_attach(ifp);
  347. ether_ifattach(ifp);
  348. /*
  349. * Reset and initialize the card again for DINGO (as found in Linux
  350. * driver). Without this Dingo will get a watchdog timeout the first
  351. * time. The ugly media tickling seems to be necessary for getting
  352. * autonegotiation to work too.
  353. */
  354. if (sc->sc_flags & XEF_DINGO) {
  355. xe_full_reset(sc);
  356. xe_init(sc);
  357. ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
  358. ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE);
  359. xe_stop(sc);
  360. }
  361. return;
  362. bad:
  363. if (state > 2)
  364. pcmcia_io_unmap(pf, psc->sc_io_window);
  365. if (state > 1)
  366. pcmcia_io_free(pf, &psc->sc_pcioh);
  367. if (state > 0)
  368. pcmcia_function_disable(pa->pf);
  369. free(cfe, M_DEVBUF, 0);
  370. }
  371. int
  372. xe_pcmcia_detach(dev, flags)
  373. struct device *dev;
  374. int flags;
  375. {
  376. struct xe_pcmcia_softc *psc = (struct xe_pcmcia_softc *)dev;
  377. struct xe_softc *sc = &psc->sc_xe;
  378. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  379. int rv = 0;
  380. mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
  381. ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
  382. pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
  383. pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
  384. ether_ifdetach(ifp);
  385. if_detach(ifp);
  386. return (rv);
  387. }
  388. int
  389. xe_pcmcia_activate(dev, act)
  390. struct device *dev;
  391. int act;
  392. {
  393. struct xe_pcmcia_softc *sc = (struct xe_pcmcia_softc *)dev;
  394. struct ifnet *ifp = &sc->sc_xe.sc_arpcom.ac_if;
  395. switch (act) {
  396. case DVACT_SUSPEND:
  397. if (ifp->if_flags & IFF_RUNNING)
  398. xe_stop(&sc->sc_xe);
  399. ifp->if_flags &= ~IFF_RUNNING;
  400. if (sc->sc_xe.sc_ih)
  401. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_xe.sc_ih);
  402. sc->sc_xe.sc_ih = NULL;
  403. pcmcia_function_disable(sc->sc_pf);
  404. break;
  405. case DVACT_RESUME:
  406. pcmcia_function_enable(sc->sc_pf);
  407. sc->sc_xe.sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET,
  408. xe_intr, sc, sc->sc_xe.sc_dev.dv_xname);
  409. /* XXX this is a ridiculous */
  410. xe_reset(&sc->sc_xe);
  411. if ((ifp->if_flags & IFF_UP) == 0)
  412. xe_stop(&sc->sc_xe);
  413. break;
  414. case DVACT_DEACTIVATE:
  415. ifp->if_timer = 0;
  416. ifp->if_flags &= ~IFF_RUNNING;
  417. if (sc->sc_xe.sc_ih)
  418. pcmcia_intr_disestablish(sc->sc_pf, sc->sc_xe.sc_ih);
  419. sc->sc_xe.sc_ih = NULL;
  420. pcmcia_function_disable(sc->sc_pf);
  421. break;
  422. }
  423. return (0);
  424. }
  425. /*
  426. * XXX These two functions might be OK to factor out into pcmcia.c since
  427. * if_sm_pcmcia.c uses similar ones.
  428. */
  429. int
  430. xe_pcmcia_funce_enaddr(parent, myla)
  431. struct device *parent;
  432. u_int8_t *myla;
  433. {
  434. /* XXX The Linux driver has more ways to do this in case of failure. */
  435. return (pcmcia_scan_cis(parent, xe_pcmcia_lan_nid_ciscallback, myla));
  436. }
  437. int
  438. xe_pcmcia_lan_nid_ciscallback(tuple, arg)
  439. struct pcmcia_tuple *tuple;
  440. void *arg;
  441. {
  442. u_int8_t *myla = arg;
  443. int i;
  444. if (tuple->code == PCMCIA_CISTPL_FUNCE) {
  445. if (tuple->length < 2)
  446. return (0);
  447. switch (pcmcia_tuple_read_1(tuple, 0)) {
  448. case PCMCIA_TPLFE_TYPE_LAN_NID:
  449. if (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)
  450. return (0);
  451. break;
  452. case 0x02:
  453. /*
  454. * Not sure about this, I don't have a CE2
  455. * that puts the ethernet addr here.
  456. */
  457. if (pcmcia_tuple_read_1(tuple, 1) != 13)
  458. return (0);
  459. break;
  460. default:
  461. return (0);
  462. }
  463. for (i = 0; i < ETHER_ADDR_LEN; i++)
  464. myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
  465. return (1);
  466. }
  467. /* Yet another spot where this might be. */
  468. if (tuple->code == 0x89) {
  469. pcmcia_tuple_read_1(tuple, 1);
  470. for (i = 0; i < ETHER_ADDR_LEN; i++)
  471. myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
  472. return (1);
  473. }
  474. return (0);
  475. }
  476. u_int32_t
  477. xe_pcmcia_interpret_manfid (parent)
  478. struct device *parent;
  479. {
  480. u_int32_t flags = 0;
  481. struct pcmcia_softc *psc = (struct pcmcia_softc *)parent;
  482. char *tptr;
  483. if (!pcmcia_scan_cis(parent, xe_pcmcia_manfid_ciscallback, &flags))
  484. return (XEF_UNSUPPORTED);
  485. if (flags & XEF_CE) {
  486. tptr = memchr(psc->card.cis1_info[2], 'C',
  487. strlen(psc->card.cis1_info[2]));
  488. /* XXX not sure if other CE2s hide "CE2" in different places */
  489. if (tptr && *(tptr + 1) == 'E' && *(tptr + 2) == '2') {
  490. flags ^= (XEF_CE | XEF_UNSUPPORTED);
  491. flags |= XEF_CE2;
  492. }
  493. }
  494. return (flags);
  495. }
  496. int
  497. xe_pcmcia_manfid_ciscallback(tuple, arg)
  498. struct pcmcia_tuple *tuple;
  499. void *arg;
  500. {
  501. u_int32_t *flagsp = arg;
  502. u_int8_t media, product;
  503. if (tuple->code == PCMCIA_CISTPL_MANFID) {
  504. if (tuple->length < 2)
  505. return (0);
  506. media = pcmcia_tuple_read_1(tuple, 3);
  507. product = pcmcia_tuple_read_1(tuple, 4);
  508. if (!(product & XEPROD_CREDITCARD) ||
  509. !(media & XEMEDIA_ETHER)) {
  510. *flagsp |= XEF_UNSUPPORTED;
  511. return (1);
  512. }
  513. if (media & XEMEDIA_MODEM)
  514. *flagsp |= XEF_MODEM;
  515. switch (product & XEPROD_IDMASK) {
  516. case 1:
  517. /* XXX Can be CE2 too (we double-check later). */
  518. *flagsp |= XEF_CE | XEF_UNSUPPORTED;
  519. break;
  520. case 2:
  521. *flagsp |= XEF_CE2;
  522. break;
  523. case 3:
  524. if (!(*flagsp & XEF_MODEM))
  525. *flagsp |= XEF_MOHAWK;
  526. *flagsp |= XEF_CE3;
  527. break;
  528. case 4:
  529. *flagsp |= XEF_CE33;
  530. break;
  531. case 5:
  532. *flagsp |= XEF_CE56 | XEF_MOHAWK;
  533. break;
  534. case 6:
  535. case 7:
  536. *flagsp |= XEF_CE56 | XEF_MOHAWK | XEF_DINGO;
  537. break;
  538. default:
  539. *flagsp |= XEF_UNSUPPORTED;
  540. break;
  541. }
  542. return (1);
  543. }
  544. return (0);
  545. }
  546. int
  547. xe_intr(arg)
  548. void *arg;
  549. {
  550. struct xe_softc *sc = arg;
  551. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  552. u_int8_t esr, rsr, isr, rx_status, savedpage;
  553. u_int16_t tx_status, recvcount = 0, tempint;
  554. ifp->if_timer = 0; /* turn watchdog timer off */
  555. if (sc->sc_flags & XEF_MOHAWK) {
  556. /* Disable interrupt (Linux does it). */
  557. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
  558. 0);
  559. }
  560. savedpage =
  561. bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
  562. PAGE(sc, 0);
  563. esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
  564. isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
  565. rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
  566. /* Check to see if card has been ejected. */
  567. if (isr == 0xff) {
  568. printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
  569. goto end;
  570. }
  571. PAGE(sc, 40);
  572. rx_status =
  573. bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
  574. tx_status =
  575. bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
  576. /*
  577. * XXX Linux writes to RXST0 and TXST* here. My CE2 works just fine
  578. * without it, and I can't see an obvious reason for it.
  579. */
  580. PAGE(sc, 0);
  581. while (esr & FULL_PKT_RCV) {
  582. if (!(rsr & RSR_RX_OK))
  583. break;
  584. /* Compare bytes read this interrupt to hard maximum. */
  585. if (recvcount > MAX_BYTES_INTR) {
  586. DPRINTF(XED_INTR,
  587. ("%s: too many bytes this interrupt\n",
  588. sc->sc_dev.dv_xname));
  589. ifp->if_iqdrops++;
  590. /* Drop packet. */
  591. bus_space_write_2(sc->sc_bst, sc->sc_bsh,
  592. sc->sc_offset + DO0, DO_SKIP_RX_PKT);
  593. }
  594. tempint = xe_get(sc);
  595. recvcount += tempint;
  596. esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
  597. sc->sc_offset + ESR);
  598. rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
  599. sc->sc_offset + RSR);
  600. }
  601. /* Packet too long? */
  602. if (rsr & RSR_TOO_LONG) {
  603. ifp->if_ierrors++;
  604. DPRINTF(XED_INTR,
  605. ("%s: packet too long\n", sc->sc_dev.dv_xname));
  606. }
  607. /* CRC error? */
  608. if (rsr & RSR_CRCERR) {
  609. ifp->if_ierrors++;
  610. DPRINTF(XED_INTR,
  611. ("%s: CRC error detected\n", sc->sc_dev.dv_xname));
  612. }
  613. /* Alignment error? */
  614. if (rsr & RSR_ALIGNERR) {
  615. ifp->if_ierrors++;
  616. DPRINTF(XED_INTR,
  617. ("%s: alignment error detected\n", sc->sc_dev.dv_xname));
  618. }
  619. /* Check for rx overrun. */
  620. if (rx_status & RX_OVERRUN) {
  621. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
  622. CLR_RX_OVERRUN);
  623. DPRINTF(XED_INTR, ("overrun cleared\n"));
  624. }
  625. /* Try to start more packets transmitting. */
  626. if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
  627. xe_start(ifp);
  628. /* Detected excessive collisions? */
  629. if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
  630. DPRINTF(XED_INTR,
  631. ("%s: excessive collisions\n", sc->sc_dev.dv_xname));
  632. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
  633. RESTART_TX);
  634. ifp->if_oerrors++;
  635. }
  636. if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
  637. ifp->if_oerrors++;
  638. end:
  639. /* Reenable interrupts. */
  640. PAGE(sc, savedpage);
  641. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
  642. ENABLE_INT);
  643. return (1);
  644. }
  645. u_int16_t
  646. xe_get(sc)
  647. struct xe_softc *sc;
  648. {
  649. u_int8_t rsr;
  650. struct mbuf *top, **mp, *m;
  651. struct mbuf_list ml = MBUF_LIST_INITIALIZER();
  652. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  653. u_int16_t pktlen, len, recvcount = 0;
  654. u_int8_t *data;
  655. PAGE(sc, 0);
  656. rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
  657. pktlen =
  658. bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
  659. RBC_COUNT_MASK;
  660. if (pktlen == 0) {
  661. /*
  662. * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
  663. * when MPE is set. It is not known why.
  664. */
  665. return (0);
  666. }
  667. recvcount += pktlen;
  668. MGETHDR(m, M_DONTWAIT, MT_DATA);
  669. if (m == NULL)
  670. return (recvcount);
  671. m->m_pkthdr.len = pktlen;
  672. len = MHLEN;
  673. top = 0;
  674. mp = &top;
  675. while (pktlen > 0) {
  676. if (top) {
  677. MGET(m, M_DONTWAIT, MT_DATA);
  678. if (m == NULL) {
  679. m_freem(top);
  680. return (recvcount);
  681. }
  682. len = MLEN;
  683. }
  684. if (pktlen >= MINCLSIZE) {
  685. MCLGET(m, M_DONTWAIT);
  686. if (!(m->m_flags & M_EXT)) {
  687. m_freem(m);
  688. m_freem(top);
  689. return (recvcount);
  690. }
  691. len = MCLBYTES;
  692. }
  693. if (!top) {
  694. caddr_t newdata = (caddr_t)ALIGN(m->m_data +
  695. sizeof (struct ether_header)) -
  696. sizeof (struct ether_header);
  697. len -= newdata - m->m_data;
  698. m->m_data = newdata;
  699. }
  700. len = min(pktlen, len);
  701. data = mtod(m, u_int8_t *);
  702. if (len > 1) {
  703. len &= ~1;
  704. bus_space_read_raw_multi_2(sc->sc_bst, sc->sc_bsh,
  705. sc->sc_offset + EDP, data, len);
  706. } else
  707. *data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
  708. sc->sc_offset + EDP);
  709. m->m_len = len;
  710. pktlen -= len;
  711. *mp = m;
  712. mp = &m->m_next;
  713. }
  714. /* Skip Rx packet. */
  715. bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
  716. DO_SKIP_RX_PKT);
  717. ml_enqueue(&ml, top);
  718. if_input(ifp, &ml);
  719. return (recvcount);
  720. }
  721. /*
  722. * Serial management for the MII.
  723. * The DELAY's below stem from the fact that the maximum frequency
  724. * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
  725. * go much faster than that.
  726. */
  727. /* Let the MII serial management be idle for one period. */
  728. static INLINE void xe_mdi_idle(struct xe_softc *);
  729. static INLINE void
  730. xe_mdi_idle(sc)
  731. struct xe_softc *sc;
  732. {
  733. bus_space_tag_t bst = sc->sc_bst;
  734. bus_space_handle_t bsh = sc->sc_bsh;
  735. bus_size_t offset = sc->sc_offset;
  736. /* Drive MDC low... */
  737. bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
  738. DELAY(1);
  739. /* and high again. */
  740. bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
  741. DELAY(1);
  742. }
  743. /* Pulse out one bit of data. */
  744. static INLINE void xe_mdi_pulse(struct xe_softc *, int);
  745. static INLINE void
  746. xe_mdi_pulse(sc, data)
  747. struct xe_softc *sc;
  748. int data;
  749. {
  750. bus_space_tag_t bst = sc->sc_bst;
  751. bus_space_handle_t bsh = sc->sc_bsh;
  752. bus_size_t offset = sc->sc_offset;
  753. u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
  754. /* First latch the data bit MDIO with clock bit MDC low...*/
  755. bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
  756. DELAY(1);
  757. /* then raise the clock again, preserving the data bit. */
  758. bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
  759. DELAY(1);
  760. }
  761. /* Probe one bit of data. */
  762. static INLINE int xe_mdi_probe(struct xe_softc *sc);
  763. static INLINE int
  764. xe_mdi_probe(sc)
  765. struct xe_softc *sc;
  766. {
  767. bus_space_tag_t bst = sc->sc_bst;
  768. bus_space_handle_t bsh = sc->sc_bsh;
  769. bus_size_t offset = sc->sc_offset;
  770. u_int8_t x;
  771. /* Pull clock bit MDCK low... */
  772. bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
  773. DELAY(1);
  774. /* Read data and drive clock high again. */
  775. x = bus_space_read_1(bst, bsh, offset + GP2) & MDIO;
  776. bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
  777. DELAY(1);
  778. return (x);
  779. }
  780. /* Pulse out a sequence of data bits. */
  781. static INLINE void xe_mdi_pulse_bits(struct xe_softc *, u_int32_t, int);
  782. static INLINE void
  783. xe_mdi_pulse_bits(sc, data, len)
  784. struct xe_softc *sc;
  785. u_int32_t data;
  786. int len;
  787. {
  788. u_int32_t mask;
  789. for (mask = 1 << (len - 1); mask; mask >>= 1)
  790. xe_mdi_pulse(sc, data & mask);
  791. }
  792. /* Read a PHY register. */
  793. int
  794. xe_mdi_read(self, phy, reg)
  795. struct device *self;
  796. int phy;
  797. int reg;
  798. {
  799. struct xe_softc *sc = (struct xe_softc *)self;
  800. int i;
  801. u_int32_t mask;
  802. u_int32_t data = 0;
  803. PAGE(sc, 2);
  804. for (i = 0; i < 32; i++) /* Synchronize. */
  805. xe_mdi_pulse(sc, 1);
  806. xe_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
  807. xe_mdi_pulse_bits(sc, phy, 5); /* PHY address */
  808. xe_mdi_pulse_bits(sc, reg, 5); /* PHY register */
  809. xe_mdi_idle(sc); /* Turn around. */
  810. xe_mdi_probe(sc); /* Drop initial zero bit. */
  811. for (mask = 1 << 15; mask; mask >>= 1)
  812. if (xe_mdi_probe(sc))
  813. data |= mask;
  814. xe_mdi_idle(sc);
  815. DPRINTF(XED_MII,
  816. ("xe_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
  817. return (data);
  818. }
  819. /* Write a PHY register. */
  820. void
  821. xe_mdi_write(self, phy, reg, value)
  822. struct device *self;
  823. int phy;
  824. int reg;
  825. int value;
  826. {
  827. struct xe_softc *sc = (struct xe_softc *)self;
  828. int i;
  829. PAGE(sc, 2);
  830. for (i = 0; i < 32; i++) /* Synchronize. */
  831. xe_mdi_pulse(sc, 1);
  832. xe_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
  833. xe_mdi_pulse_bits(sc, phy, 5); /* PHY address */
  834. xe_mdi_pulse_bits(sc, reg, 5); /* PHY register */
  835. xe_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
  836. xe_mdi_pulse_bits(sc, value, 16); /* Write the data */
  837. xe_mdi_idle(sc); /* Idle away. */
  838. DPRINTF(XED_MII,
  839. ("xe_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
  840. }
  841. void
  842. xe_statchg(self)
  843. struct device *self;
  844. {
  845. }
  846. /*
  847. * Change media according to request.
  848. */
  849. int
  850. xe_mediachange(ifp)
  851. struct ifnet *ifp;
  852. {
  853. if (ifp->if_flags & IFF_UP)
  854. xe_init(ifp->if_softc);
  855. return (0);
  856. }
  857. /*
  858. * Notify the world which media we're using.
  859. */
  860. void
  861. xe_mediastatus(ifp, ifmr)
  862. struct ifnet *ifp;
  863. struct ifmediareq *ifmr;
  864. {
  865. struct xe_softc *sc = ifp->if_softc;
  866. mii_pollstat(&sc->sc_mii);
  867. ifmr->ifm_status = sc->sc_mii.mii_media_status;
  868. ifmr->ifm_active = sc->sc_mii.mii_media_active;
  869. }
  870. void
  871. xe_reset(sc)
  872. struct xe_softc *sc;
  873. {
  874. int s;
  875. s = splnet();
  876. xe_stop(sc);
  877. xe_full_reset(sc);
  878. xe_init(sc);
  879. splx(s);
  880. }
  881. void
  882. xe_watchdog(ifp)
  883. struct ifnet *ifp;
  884. {
  885. struct xe_softc *sc = ifp->if_softc;
  886. log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
  887. ++sc->sc_arpcom.ac_if.if_oerrors;
  888. xe_reset(sc);
  889. }
  890. void
  891. xe_stop(sc)
  892. register struct xe_softc *sc;
  893. {
  894. /* Disable interrupts. */
  895. PAGE(sc, 0);
  896. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR, 0);
  897. PAGE(sc, 1);
  898. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + IMR0, 0);
  899. /* Power down, wait. */
  900. PAGE(sc, 4);
  901. bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + GP1, 0);
  902. DELAY(40000);
  903. /* Cancel watchdog timer. */
  904. sc->sc_arpcom.ac_if.if_timer = 0;
  905. }
  906. void
  907. xe_init(sc)
  908. struct xe_softc *sc;
  909. {
  910. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  911. int s;
  912. DPRINTF(XED_CONFIG, ("xe_init\n"));
  913. s = splnet();
  914. xe_set_address(sc);
  915. /* Set current media. */
  916. mii_mediachg(&sc->sc_mii);
  917. ifp->if_flags |= IFF_RUNNING;
  918. ifp->if_flags &= ~IFF_OACTIVE;
  919. splx(s);
  920. }
  921. /*
  922. * Start outputting on the interface.
  923. * Always called as splnet().
  924. */
  925. void
  926. xe_start(ifp)
  927. struct ifnet *ifp;
  928. {
  929. struct xe_softc *sc = ifp->if_softc;
  930. bus_space_tag_t bst = sc->sc_bst;
  931. bus_space_handle_t bsh = sc->sc_bsh;
  932. bus_size_t offset = sc->sc_offset;
  933. unsigned int s, len, pad = 0;
  934. struct mbuf *m0, *m;
  935. u_int16_t space;
  936. /* Don't transmit if interface is busy or not running. */
  937. if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
  938. return;
  939. /* Peek at the next packet. */
  940. IFQ_POLL(&ifp->if_snd, m0);
  941. if (m0 == NULL)
  942. return;
  943. /* We need to use m->m_pkthdr.len, so require the header. */
  944. if (!(m0->m_flags & M_PKTHDR))
  945. panic("xe_start: no header mbuf");
  946. len = m0->m_pkthdr.len;
  947. /* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
  948. if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
  949. pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
  950. PAGE(sc, 0);
  951. space = bus_space_read_2(bst, bsh, offset + TSO0) & 0x7fff;
  952. if (len + pad + 2 > space) {
  953. DPRINTF(XED_FIFO,
  954. ("%s: not enough space in output FIFO (%d > %d)\n",
  955. sc->sc_dev.dv_xname, len + pad + 2, space));
  956. return;
  957. }
  958. IFQ_DEQUEUE(&ifp->if_snd, m0);
  959. #if NBPFILTER > 0
  960. if (ifp->if_bpf)
  961. bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
  962. #endif
  963. /*
  964. * Do the output at splhigh() so that an interrupt from another device
  965. * won't cause a FIFO underrun.
  966. */
  967. s = splhigh();
  968. bus_space_write_2(bst, bsh, offset + TSO2, (u_int16_t)len + pad + 2);
  969. bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
  970. for (m = m0; m; ) {
  971. if (m->m_len > 1)
  972. bus_space_write_raw_multi_2(bst, bsh, offset + EDP,
  973. mtod(m, u_int8_t *), m->m_len & ~1);
  974. if (m->m_len & 1)
  975. bus_space_write_1(bst, bsh, offset + EDP,
  976. *(mtod(m, u_int8_t *) + m->m_len - 1));
  977. m0 = m_free(m);
  978. m = m0;
  979. }
  980. if (sc->sc_flags & XEF_MOHAWK)
  981. bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
  982. else {
  983. for (; pad > 1; pad -= 2)
  984. bus_space_write_2(bst, bsh, offset + EDP, 0);
  985. if (pad == 1)
  986. bus_space_write_1(bst, bsh, offset + EDP, 0);
  987. }
  988. splx(s);
  989. ifp->if_timer = 5;
  990. ++ifp->if_opackets;
  991. }
  992. int
  993. xe_ioctl(ifp, command, data)
  994. struct ifnet *ifp;
  995. u_long command;
  996. caddr_t data;
  997. {
  998. struct xe_softc *sc = ifp->if_softc;
  999. struct ifaddr *ifa = (struct ifaddr *)data;
  1000. struct ifreq *ifr = (struct ifreq *)data;
  1001. int s, error = 0;
  1002. s = splnet();
  1003. switch (command) {
  1004. case SIOCSIFADDR:
  1005. ifp->if_flags |= IFF_UP;
  1006. xe_init(sc);
  1007. if (ifa->ifa_addr->sa_family == AF_INET)
  1008. arp_ifinit(&sc->sc_arpcom, ifa);
  1009. break;
  1010. case SIOCSIFFLAGS:
  1011. sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
  1012. PAGE(sc, 0x42);
  1013. if ((ifp->if_flags & IFF_PROMISC) ||
  1014. (ifp->if_flags & IFF_ALLMULTI))
  1015. bus_space_write_1(sc->sc_bst, sc->sc_bsh,
  1016. sc->sc_offset + SWC1,
  1017. SWC1_PROMISC | SWC1_MCAST_PROM);
  1018. else
  1019. bus_space_write_1(sc->sc_bst, sc->sc_bsh,
  1020. sc->sc_offset + SWC1, 0);
  1021. /*
  1022. * If interface is marked up and not running, then start it.
  1023. * If it is marked down and running, stop it.
  1024. * XXX If it's up then re-initialize it. This is so flags
  1025. * such as IFF_PROMISC are handled.
  1026. */
  1027. if (ifp->if_flags & IFF_UP) {
  1028. xe_init(sc);
  1029. } else {
  1030. if (ifp->if_flags & IFF_RUNNING)
  1031. xe_stop(sc);
  1032. }
  1033. break;
  1034. case SIOCADDMULTI:
  1035. case SIOCDELMULTI:
  1036. sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
  1037. error = (command == SIOCADDMULTI) ?
  1038. ether_addmulti(ifr, &sc->sc_arpcom) :
  1039. ether_delmulti(ifr, &sc->sc_arpcom);
  1040. if (error == ENETRESET) {
  1041. /*
  1042. * Multicast list has changed; set the hardware
  1043. * filter accordingly.
  1044. */
  1045. if (!sc->sc_all_mcasts &&
  1046. !(ifp->if_flags & IFF_PROMISC))
  1047. xe_set_address(sc);
  1048. /*
  1049. * xe_set_address() can turn on all_mcasts if we run
  1050. * out of space, so check it again rather than else {}.
  1051. */
  1052. if (sc->sc_all_mcasts)
  1053. xe_init(sc);
  1054. error = 0;
  1055. }
  1056. break;
  1057. case SIOCSIFMEDIA:
  1058. case SIOCGIFMEDIA:
  1059. error =
  1060. ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
  1061. break;
  1062. default:
  1063. error = ENOTTY;
  1064. }
  1065. splx(s);
  1066. return (error);
  1067. }
  1068. void
  1069. xe_set_address(sc)
  1070. struct xe_softc *sc;
  1071. {
  1072. bus_space_tag_t bst = sc->sc_bst;
  1073. bus_space_handle_t bsh = sc->sc_bsh;
  1074. bus_size_t offset = sc->sc_offset;
  1075. struct arpcom *arp = &sc->sc_arpcom;
  1076. struct ether_multi *enm;
  1077. struct ether_multistep step;
  1078. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  1079. int i, page, pos, num;
  1080. PAGE(sc, 0x50);
  1081. for (i = 0; i < 6; i++) {
  1082. bus_space_write_1(bst, bsh, offset + IA + i,
  1083. sc->sc_arpcom.ac_enaddr[(sc->sc_flags & XEF_MOHAWK) ?
  1084. 5 - i : i]);
  1085. }
  1086. if (arp->ac_multirangecnt > 0) {
  1087. ifp->if_flags |= IFF_ALLMULTI;
  1088. sc->sc_all_mcasts=1;
  1089. } else if (arp->ac_multicnt > 0) {
  1090. if (arp->ac_multicnt > 9) {
  1091. PAGE(sc, 0x42);
  1092. bus_space_write_1(sc->sc_bst, sc->sc_bsh,
  1093. sc->sc_offset + SWC1,
  1094. SWC1_PROMISC | SWC1_MCAST_PROM);
  1095. return;
  1096. }
  1097. ETHER_FIRST_MULTI(step, arp, enm);
  1098. pos = IA + 6;
  1099. for (page = 0x50, num = arp->ac_multicnt; num > 0 && enm;
  1100. num--) {
  1101. for (i = 0; i < 6; i++) {
  1102. bus_space_write_1(bst, bsh, offset + pos,
  1103. enm->enm_addrlo[
  1104. (sc->sc_flags & XEF_MOHAWK) ? 5 - i : i]);
  1105. if (++pos > 15) {
  1106. pos = IA;
  1107. page++;
  1108. PAGE(sc, page);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. }
  1114. void
  1115. xe_cycle_power(sc)
  1116. struct xe_softc *sc;
  1117. {
  1118. bus_space_tag_t bst = sc->sc_bst;
  1119. bus_space_handle_t bsh = sc->sc_bsh;
  1120. bus_size_t offset = sc->sc_offset;
  1121. PAGE(sc, 4);
  1122. DELAY(1);
  1123. bus_space_write_1(bst, bsh, offset + GP1, 0);
  1124. DELAY(40000);
  1125. if (sc->sc_flags & XEF_MOHAWK)
  1126. bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
  1127. else
  1128. /* XXX What is bit 2 (aka AIC)? */
  1129. bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
  1130. DELAY(20000);
  1131. }
  1132. void
  1133. xe_full_reset(sc)
  1134. struct xe_softc *sc;
  1135. {
  1136. bus_space_tag_t bst = sc->sc_bst;
  1137. bus_space_handle_t bsh = sc->sc_bsh;
  1138. bus_size_t offset = sc->sc_offset;
  1139. /* Do an as extensive reset as possible on all functions. */
  1140. xe_cycle_power(sc);
  1141. bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
  1142. DELAY(20000);
  1143. bus_space_write_1(bst, bsh, offset + CR, 0);
  1144. DELAY(20000);
  1145. if (sc->sc_flags & XEF_MOHAWK) {
  1146. PAGE(sc, 4);
  1147. /*
  1148. * Drive GP1 low to power up ML6692 and GP2 high to power up
  1149. * the 10MHz chip. XXX What chip is that? The phy?
  1150. */
  1151. bus_space_write_1(bst, bsh, offset + GP0,
  1152. GP1_OUT | GP2_OUT | GP2_WR);
  1153. }
  1154. DELAY(500000);
  1155. /* Get revision information. XXX Symbolic constants. */
  1156. sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
  1157. ((sc->sc_flags & XEF_MOHAWK) ? 0x70 : 0x30) >> 4;
  1158. /* Media selection. XXX Maybe manual overriding too? */
  1159. if (!(sc->sc_flags & XEF_MOHAWK)) {
  1160. PAGE(sc, 4);
  1161. /*
  1162. * XXX I have no idea what this really does, it is from the
  1163. * Linux driver.
  1164. */
  1165. bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
  1166. }
  1167. DELAY(40000);
  1168. /* Setup the ethernet interrupt mask. */
  1169. PAGE(sc, 1);
  1170. bus_space_write_1(bst, bsh, offset + IMR0,
  1171. ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
  1172. ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
  1173. #if 0
  1174. bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
  1175. #endif
  1176. if (!(sc->sc_flags & XEF_DINGO))
  1177. /* XXX What is this? Not for Dingo at least. */
  1178. bus_space_write_1(bst, bsh, offset + IMR1, 1);
  1179. /*
  1180. * Disable source insertion.
  1181. * XXX Dingo does not have this bit, but Linux does it unconditionally.
  1182. */
  1183. if (!(sc->sc_flags & XEF_DINGO)) {
  1184. PAGE(sc, 0x42);
  1185. bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
  1186. }
  1187. /* Set the local memory dividing line. */
  1188. if (sc->sc_rev != 1) {
  1189. PAGE(sc, 2);
  1190. /* XXX Symbolic constant preferrable. */
  1191. bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
  1192. }
  1193. xe_set_address(sc);
  1194. /*
  1195. * Apparently the receive byte pointer can be bad after a reset, so
  1196. * we hardwire it correctly.
  1197. */
  1198. PAGE(sc, 0);
  1199. bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
  1200. /* Setup ethernet MAC registers. XXX Symbolic constants. */
  1201. PAGE(sc, 0x40);
  1202. bus_space_write_1(bst, bsh, offset + RX0MSK,
  1203. PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
  1204. bus_space_write_1(bst, bsh, offset + TX0MSK,
  1205. CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
  1206. SQE | TX_ABORT | TX_OK);
  1207. if (!(sc->sc_flags & XEF_DINGO))
  1208. /* XXX From Linux, dunno what 0xb0 means. */
  1209. bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
  1210. bus_space_write_1(bst, bsh, offset + RXST0, 0);
  1211. bus_space_write_1(bst, bsh, offset + TXST0, 0);
  1212. bus_space_write_1(bst, bsh, offset + TXST1, 0);
  1213. /* Enable MII function if available. */
  1214. if (LIST_FIRST(&sc->sc_mii.mii_phys)) {
  1215. PAGE(sc, 2);
  1216. bus_space_write_1(bst, bsh, offset + MSR,
  1217. bus_space_read_1(bst, bsh, offset + MSR) | SELECT_MII);
  1218. DELAY(20000);
  1219. } else {
  1220. PAGE(sc, 0);
  1221. /* XXX Do we need to do this? */
  1222. PAGE(sc, 0x42);
  1223. bus_space_write_1(bst, bsh, offset + SWC1, SWC1_AUTO_MEDIA);
  1224. DELAY(50000);
  1225. /* XXX Linux probes the media here. */
  1226. }
  1227. /* Configure the LED registers. */
  1228. PAGE(sc, 2);
  1229. /* XXX This is not good for 10base2. */
  1230. bus_space_write_1(bst, bsh, offset + LED,
  1231. LED_TX_ACT << LED1_SHIFT | LED_10MB_LINK << LED0_SHIFT);
  1232. if (sc->sc_flags & XEF_DINGO)
  1233. bus_space_write_1(bst, bsh, offset + LED3,
  1234. LED_100MB_LINK << LED3_SHIFT);
  1235. /* Enable receiver and go online. */
  1236. PAGE(sc, 0x40);
  1237. bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
  1238. #if 0
  1239. /* XXX Linux does this here - is it necessary? */
  1240. PAGE(sc, 1);
  1241. bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
  1242. if (!(sc->sc_flags & XEF_DINGO))
  1243. /* XXX What is this? Not for Dingo at least. */
  1244. bus_space_write_1(bst, bsh, offset + IMR1, 1);
  1245. #endif
  1246. /* Enable interrupts. */
  1247. PAGE(sc, 0);
  1248. bus_space_write_1(bst, bsh, offset + CR, ENABLE_INT);
  1249. /* XXX This is pure magic for me, found in the Linux driver. */
  1250. if ((sc->sc_flags & (XEF_DINGO | XEF_MODEM)) == XEF_MODEM) {
  1251. if ((bus_space_read_1(bst, bsh, offset + 0x10) & 0x01) == 0)
  1252. /* Unmask the master interrupt bit. */
  1253. bus_space_write_1(bst, bsh, offset + 0x10, 0x11);
  1254. }
  1255. /*
  1256. * The Linux driver says this:
  1257. * We should switch back to page 0 to avoid a bug in revision 0
  1258. * where regs with offset below 8 can't be read after an access
  1259. * to the MAC registers.
  1260. */
  1261. PAGE(sc, 0);
  1262. }
  1263. #ifdef XEDEBUG
  1264. void
  1265. xe_reg_dump(sc)
  1266. struct xe_softc *sc;
  1267. {
  1268. int page, i;
  1269. bus_space_tag_t bst = sc->sc_bst;
  1270. bus_space_handle_t bsh = sc->sc_bsh;
  1271. bus_size_t offset = sc->sc_offset;
  1272. printf("%x: Common registers: ", sc->sc_dev.dv_xname);
  1273. for (i = 0; i < 8; i++) {
  1274. printf(" %2.2x", bus_space_read_1(bst, bsh, offset + i));
  1275. }
  1276. printf("\n");
  1277. for (page = 0; page < 8; page++) {
  1278. printf("%s: Register page %2.2x: ", sc->sc_dev.dv_xname, page);
  1279. PAGE(sc, page);
  1280. for (i = 8; i < 16; i++) {
  1281. printf(" %2.2x",
  1282. bus_space_read_1(bst, bsh, offset + i));
  1283. }
  1284. printf("\n");
  1285. }
  1286. for (page = 0x40; page < 0x5f; page++) {
  1287. if (page == 0x43 || (page >= 0x46 && page <= 0x4f) ||
  1288. (page >= 0x51 && page <= 0x5e))
  1289. continue;
  1290. printf("%s: Register page %2.2x: ", sc->sc_dev.dv_xname, page);
  1291. PAGE(sc, page);
  1292. for (i = 8; i < 16; i++) {
  1293. printf(" %2.2x",
  1294. bus_space_read_1(bst, bsh, offset + i));
  1295. }
  1296. printf("\n");
  1297. }
  1298. }
  1299. #endif /* XEDEBUG */