qe.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /* $OpenBSD: qe.c,v 1.30 2015/07/08 07:21:50 mpi Exp $ */
  2. /* $NetBSD: qe.c,v 1.16 2001/03/30 17:30:18 christos Exp $ */
  3. /*-
  4. * Copyright (c) 1999 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Paul Kranenburg.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1998 Jason L. Wright.
  33. * All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. * 2. Redistributions in binary form must reproduce the above copyright
  41. * notice, this list of conditions and the following disclaimer in the
  42. * documentation and/or other materials provided with the distribution.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  45. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  46. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  47. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  53. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. /*
  56. * Driver for the SBus qec+qe QuadEthernet board.
  57. *
  58. * This driver was written using the AMD MACE Am79C940 documentation, some
  59. * ideas gleaned from the S/Linux driver for this card, Solaris header files,
  60. * and a loan of a card from Paul Southworth of the Internet Engineering
  61. * Group (www.ieng.com).
  62. */
  63. #define QEDEBUG
  64. #include "bpfilter.h"
  65. #include <sys/param.h>
  66. #include <sys/systm.h>
  67. #include <sys/kernel.h>
  68. #include <sys/errno.h>
  69. #include <sys/ioctl.h>
  70. #include <sys/mbuf.h>
  71. #include <sys/socket.h>
  72. #include <sys/syslog.h>
  73. #include <sys/device.h>
  74. #include <sys/malloc.h>
  75. #include <net/if.h>
  76. #include <net/if_dl.h>
  77. #include <net/if_types.h>
  78. #include <net/netisr.h>
  79. #include <net/if_media.h>
  80. #include <netinet/in.h>
  81. #include <netinet/if_ether.h>
  82. #if NBPFILTER > 0
  83. #include <net/bpf.h>
  84. #endif
  85. #include <machine/bus.h>
  86. #include <machine/intr.h>
  87. #include <machine/autoconf.h>
  88. #include <dev/sbus/sbusvar.h>
  89. #include <dev/sbus/qecreg.h>
  90. #include <dev/sbus/qecvar.h>
  91. #include <dev/sbus/qereg.h>
  92. struct qe_softc {
  93. struct device sc_dev; /* base device */
  94. bus_space_tag_t sc_bustag; /* bus & dma tags */
  95. bus_dma_tag_t sc_dmatag;
  96. bus_dmamap_t sc_dmamap;
  97. struct arpcom sc_arpcom;
  98. struct ifmedia sc_ifmedia; /* interface media */
  99. struct qec_softc *sc_qec; /* QEC parent */
  100. bus_space_handle_t sc_qr; /* QEC registers */
  101. bus_space_handle_t sc_mr; /* MACE registers */
  102. bus_space_handle_t sc_cr; /* channel registers */
  103. int sc_channel; /* channel number */
  104. u_int sc_rev; /* board revision */
  105. int sc_burst;
  106. struct qec_ring sc_rb; /* Packet Ring Buffer */
  107. #ifdef QEDEBUG
  108. int sc_debug;
  109. #endif
  110. };
  111. int qematch(struct device *, void *, void *);
  112. void qeattach(struct device *, struct device *, void *);
  113. void qeinit(struct qe_softc *);
  114. void qestart(struct ifnet *);
  115. void qestop(struct qe_softc *);
  116. void qewatchdog(struct ifnet *);
  117. int qeioctl(struct ifnet *, u_long, caddr_t);
  118. void qereset(struct qe_softc *);
  119. int qeintr(void *);
  120. int qe_eint(struct qe_softc *, u_int32_t);
  121. int qe_rint(struct qe_softc *);
  122. int qe_tint(struct qe_softc *);
  123. void qe_mcreset(struct qe_softc *);
  124. int qe_put(struct qe_softc *, int, struct mbuf *);
  125. void qe_read(struct qe_softc *, int, int);
  126. struct mbuf *qe_get(struct qe_softc *, int, int);
  127. /* ifmedia callbacks */
  128. void qe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
  129. int qe_ifmedia_upd(struct ifnet *);
  130. struct cfattach qe_ca = {
  131. sizeof(struct qe_softc), qematch, qeattach
  132. };
  133. struct cfdriver qe_cd = {
  134. NULL, "qe", DV_IFNET
  135. };
  136. int
  137. qematch(parent, vcf, aux)
  138. struct device *parent;
  139. void *vcf;
  140. void *aux;
  141. {
  142. struct cfdata *cf = vcf;
  143. struct sbus_attach_args *sa = aux;
  144. return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
  145. }
  146. void
  147. qeattach(parent, self, aux)
  148. struct device *parent, *self;
  149. void *aux;
  150. {
  151. struct sbus_attach_args *sa = aux;
  152. struct qec_softc *qec = (struct qec_softc *)parent;
  153. struct qe_softc *sc = (struct qe_softc *)self;
  154. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  155. int node = sa->sa_node;
  156. bus_dma_tag_t dmatag = sa->sa_dmatag;
  157. bus_dma_segment_t seg;
  158. bus_size_t size;
  159. int rseg, error;
  160. extern void myetheraddr(u_char *);
  161. /* Pass on the bus tags */
  162. sc->sc_bustag = sa->sa_bustag;
  163. sc->sc_dmatag = sa->sa_dmatag;
  164. if (sa->sa_nreg < 2) {
  165. printf("%s: only %d register sets\n",
  166. self->dv_xname, sa->sa_nreg);
  167. return;
  168. }
  169. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
  170. (bus_addr_t)sa->sa_reg[0].sbr_offset,
  171. (bus_size_t)sa->sa_reg[0].sbr_size, 0, 0, &sc->sc_cr) != 0) {
  172. printf("%s: cannot map registers\n", self->dv_xname);
  173. return;
  174. }
  175. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[1].sbr_slot,
  176. (bus_addr_t)sa->sa_reg[1].sbr_offset,
  177. (bus_size_t)sa->sa_reg[1].sbr_size, 0, 0, &sc->sc_mr) != 0) {
  178. printf("%s: cannot map registers\n", self->dv_xname);
  179. return;
  180. }
  181. sc->sc_rev = getpropint(node, "mace-version", -1);
  182. printf(" rev %x", sc->sc_rev);
  183. sc->sc_qec = qec;
  184. sc->sc_qr = qec->sc_regs;
  185. sc->sc_channel = getpropint(node, "channel#", -1);
  186. sc->sc_burst = qec->sc_burst;
  187. qestop(sc);
  188. /* Note: no interrupt level passed */
  189. if (bus_intr_establish(sa->sa_bustag, 0, IPL_NET, 0, qeintr, sc,
  190. self->dv_xname) == NULL) {
  191. printf(": no interrupt established\n");
  192. return;
  193. }
  194. myetheraddr(sc->sc_arpcom.ac_enaddr);
  195. /*
  196. * Allocate descriptor ring and buffers.
  197. */
  198. /* for now, allocate as many bufs as there are ring descriptors */
  199. sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
  200. sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
  201. size =
  202. QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
  203. QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
  204. sc->sc_rb.rb_ntbuf * QE_PKT_BUF_SZ +
  205. sc->sc_rb.rb_nrbuf * QE_PKT_BUF_SZ;
  206. /* Get a DMA handle */
  207. if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
  208. BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
  209. printf("%s: DMA map create error %d\n", self->dv_xname, error);
  210. return;
  211. }
  212. /* Allocate DMA buffer */
  213. if ((error = bus_dmamem_alloc(dmatag, size, 0, 0,
  214. &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
  215. printf("%s: DMA buffer alloc error %d\n",
  216. self->dv_xname, error);
  217. return;
  218. }
  219. /* Map DMA buffer in CPU addressable space */
  220. if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
  221. &sc->sc_rb.rb_membase,
  222. BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
  223. printf("%s: DMA buffer map error %d\n",
  224. self->dv_xname, error);
  225. bus_dmamem_free(dmatag, &seg, rseg);
  226. return;
  227. }
  228. /* Load the buffer */
  229. if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
  230. sc->sc_rb.rb_membase, size, NULL, BUS_DMA_NOWAIT)) != 0) {
  231. printf("%s: DMA buffer map load error %d\n",
  232. self->dv_xname, error);
  233. bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
  234. bus_dmamem_free(dmatag, &seg, rseg);
  235. return;
  236. }
  237. sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
  238. /* Initialize media properties */
  239. ifmedia_init(&sc->sc_ifmedia, 0, qe_ifmedia_upd, qe_ifmedia_sts);
  240. ifmedia_add(&sc->sc_ifmedia,
  241. IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0), 0, NULL);
  242. ifmedia_add(&sc->sc_ifmedia,
  243. IFM_MAKEWORD(IFM_ETHER,IFM_10_5,0,0), 0, NULL);
  244. ifmedia_add(&sc->sc_ifmedia,
  245. IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0), 0, NULL);
  246. ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
  247. bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
  248. ifp->if_softc = sc;
  249. ifp->if_start = qestart;
  250. ifp->if_ioctl = qeioctl;
  251. ifp->if_watchdog = qewatchdog;
  252. ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
  253. IFF_MULTICAST;
  254. IFQ_SET_READY(&ifp->if_snd);
  255. /* Attach the interface. */
  256. if_attach(ifp);
  257. ether_ifattach(ifp);
  258. printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
  259. }
  260. /*
  261. * Pull data off an interface.
  262. * Len is the length of data, with local net header stripped.
  263. * We copy the data into mbufs. When full cluster sized units are present,
  264. * we copy into clusters.
  265. */
  266. struct mbuf *
  267. qe_get(sc, idx, totlen)
  268. struct qe_softc *sc;
  269. int idx, totlen;
  270. {
  271. struct mbuf *m;
  272. struct mbuf *top, **mp;
  273. int len, pad, boff = 0;
  274. caddr_t bp;
  275. bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * QE_PKT_BUF_SZ;
  276. MGETHDR(m, M_DONTWAIT, MT_DATA);
  277. if (m == NULL)
  278. return (NULL);
  279. m->m_pkthdr.len = totlen;
  280. pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
  281. m->m_data += pad;
  282. len = MHLEN - pad;
  283. top = NULL;
  284. mp = &top;
  285. while (totlen > 0) {
  286. if (top) {
  287. MGET(m, M_DONTWAIT, MT_DATA);
  288. if (m == NULL) {
  289. m_freem(top);
  290. return (NULL);
  291. }
  292. len = MLEN;
  293. }
  294. if (top && totlen >= MINCLSIZE) {
  295. MCLGET(m, M_DONTWAIT);
  296. if (m->m_flags & M_EXT)
  297. len = MCLBYTES;
  298. }
  299. m->m_len = len = min(totlen, len);
  300. bcopy(bp + boff, mtod(m, caddr_t), len);
  301. boff += len;
  302. totlen -= len;
  303. *mp = m;
  304. mp = &m->m_next;
  305. }
  306. return (top);
  307. }
  308. /*
  309. * Routine to copy from mbuf chain to transmit buffer in
  310. * network buffer memory.
  311. */
  312. __inline__ int
  313. qe_put(sc, idx, m)
  314. struct qe_softc *sc;
  315. int idx;
  316. struct mbuf *m;
  317. {
  318. struct mbuf *n;
  319. int len, tlen = 0, boff = 0;
  320. caddr_t bp;
  321. bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * QE_PKT_BUF_SZ;
  322. for (; m; m = n) {
  323. len = m->m_len;
  324. if (len == 0) {
  325. n = m_free(m);
  326. continue;
  327. }
  328. bcopy(mtod(m, caddr_t), bp+boff, len);
  329. boff += len;
  330. tlen += len;
  331. n = m_free(m);
  332. }
  333. return (tlen);
  334. }
  335. /*
  336. * Pass a packet to the higher levels.
  337. */
  338. __inline__ void
  339. qe_read(sc, idx, len)
  340. struct qe_softc *sc;
  341. int idx, len;
  342. {
  343. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  344. struct mbuf_list ml = MBUF_LIST_INITIALIZER();
  345. struct mbuf *m;
  346. if (len <= sizeof(struct ether_header) ||
  347. len > ETHERMTU + sizeof(struct ether_header)) {
  348. printf("%s: invalid packet size %d; dropping\n",
  349. ifp->if_xname, len);
  350. ifp->if_ierrors++;
  351. return;
  352. }
  353. /*
  354. * Pull packet off interface.
  355. */
  356. m = qe_get(sc, idx, len);
  357. if (m == NULL) {
  358. ifp->if_ierrors++;
  359. return;
  360. }
  361. ml_enqueue(&ml, m);
  362. if_input(ifp, &ml);
  363. }
  364. /*
  365. * Start output on interface.
  366. * We make two assumptions here:
  367. * 1) that the current priority is set to splnet _before_ this code
  368. * is called *and* is returned to the appropriate priority after
  369. * return
  370. * 2) that the IFF_OACTIVE flag is checked before this code is called
  371. * (i.e. that the output part of the interface is idle)
  372. */
  373. void
  374. qestart(ifp)
  375. struct ifnet *ifp;
  376. {
  377. struct qe_softc *sc = (struct qe_softc *)ifp->if_softc;
  378. struct qec_xd *txd = sc->sc_rb.rb_txd;
  379. struct mbuf *m;
  380. unsigned int bix, len;
  381. unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
  382. if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
  383. return;
  384. bix = sc->sc_rb.rb_tdhead;
  385. for (;;) {
  386. IFQ_POLL(&ifp->if_snd, m);
  387. if (m == NULL)
  388. break;
  389. IFQ_DEQUEUE(&ifp->if_snd, m);
  390. #if NBPFILTER > 0
  391. /*
  392. * If BPF is listening on this interface, let it see the
  393. * packet before we commit it to the wire.
  394. */
  395. if (ifp->if_bpf)
  396. bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
  397. #endif
  398. /*
  399. * Copy the mbuf chain into the transmit buffer.
  400. */
  401. len = qe_put(sc, bix, m);
  402. /*
  403. * Initialize transmit registers and start transmission
  404. */
  405. txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
  406. (len & QEC_XD_LENGTH);
  407. bus_space_write_4(sc->sc_bustag, sc->sc_cr, QE_CRI_CTRL,
  408. QE_CR_CTRL_TWAKEUP);
  409. if (++bix == QEC_XD_RING_MAXSIZE)
  410. bix = 0;
  411. if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
  412. ifp->if_flags |= IFF_OACTIVE;
  413. break;
  414. }
  415. }
  416. sc->sc_rb.rb_tdhead = bix;
  417. }
  418. void
  419. qestop(sc)
  420. struct qe_softc *sc;
  421. {
  422. bus_space_tag_t t = sc->sc_bustag;
  423. bus_space_handle_t mr = sc->sc_mr;
  424. bus_space_handle_t cr = sc->sc_cr;
  425. int n;
  426. /* Stop the schwurst */
  427. bus_space_write_1(t, mr, QE_MRI_BIUCC, QE_MR_BIUCC_SWRST);
  428. for (n = 200; n > 0; n--) {
  429. if ((bus_space_read_1(t, mr, QE_MRI_BIUCC) &
  430. QE_MR_BIUCC_SWRST) == 0)
  431. break;
  432. DELAY(20);
  433. }
  434. /* then reset */
  435. bus_space_write_4(t, cr, QE_CRI_CTRL, QE_CR_CTRL_RESET);
  436. for (n = 200; n > 0; n--) {
  437. if ((bus_space_read_4(t, cr, QE_CRI_CTRL) &
  438. QE_CR_CTRL_RESET) == 0)
  439. break;
  440. DELAY(20);
  441. }
  442. }
  443. /*
  444. * Reset interface.
  445. */
  446. void
  447. qereset(sc)
  448. struct qe_softc *sc;
  449. {
  450. int s;
  451. s = splnet();
  452. qestop(sc);
  453. qeinit(sc);
  454. splx(s);
  455. }
  456. void
  457. qewatchdog(ifp)
  458. struct ifnet *ifp;
  459. {
  460. struct qe_softc *sc = ifp->if_softc;
  461. log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
  462. ifp->if_oerrors++;
  463. qereset(sc);
  464. }
  465. /*
  466. * Interrupt dispatch.
  467. */
  468. int
  469. qeintr(arg)
  470. void *arg;
  471. {
  472. struct qe_softc *sc = (struct qe_softc *)arg;
  473. bus_space_tag_t t = sc->sc_bustag;
  474. u_int32_t qecstat, qestat;
  475. int r = 0;
  476. /* Read QEC status and channel status */
  477. qecstat = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
  478. #ifdef QEDEBUG
  479. if (sc->sc_debug) {
  480. printf("qe%d: intr: qecstat=%x\n", sc->sc_channel, qecstat);
  481. }
  482. #endif
  483. /* Filter out status for this channel */
  484. qecstat = qecstat >> (4 * sc->sc_channel);
  485. if ((qecstat & 0xf) == 0)
  486. return (r);
  487. qestat = bus_space_read_4(t, sc->sc_cr, QE_CRI_STAT);
  488. #ifdef QEDEBUG
  489. if (sc->sc_debug) {
  490. int i;
  491. bus_space_tag_t t = sc->sc_bustag;
  492. bus_space_handle_t mr = sc->sc_mr;
  493. printf("qe%d: intr: qestat=%b\n", sc->sc_channel,
  494. qestat, QE_CR_STAT_BITS);
  495. printf("MACE registers:\n");
  496. for (i = 0 ; i < 32; i++) {
  497. printf(" m[%d]=%x,", i, bus_space_read_1(t, mr, i));
  498. if (((i+1) & 7) == 0)
  499. printf("\n");
  500. }
  501. }
  502. #endif
  503. if (qestat & QE_CR_STAT_ALLERRORS) {
  504. #ifdef QEDEBUG
  505. if (sc->sc_debug)
  506. printf("qe%d: eint: qestat=%b\n", sc->sc_channel,
  507. qestat, QE_CR_STAT_BITS);
  508. #endif
  509. r |= qe_eint(sc, qestat);
  510. if (r == -1)
  511. return (1);
  512. }
  513. if (qestat & QE_CR_STAT_TXIRQ)
  514. r |= qe_tint(sc);
  515. if (qestat & QE_CR_STAT_RXIRQ)
  516. r |= qe_rint(sc);
  517. return (1);
  518. }
  519. /*
  520. * Transmit interrupt.
  521. */
  522. int
  523. qe_tint(sc)
  524. struct qe_softc *sc;
  525. {
  526. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  527. unsigned int bix, txflags;
  528. bix = sc->sc_rb.rb_tdtail;
  529. for (;;) {
  530. if (sc->sc_rb.rb_td_nbusy <= 0)
  531. break;
  532. txflags = sc->sc_rb.rb_txd[bix].xd_flags;
  533. if (txflags & QEC_XD_OWN)
  534. break;
  535. ifp->if_flags &= ~IFF_OACTIVE;
  536. ifp->if_opackets++;
  537. if (++bix == QEC_XD_RING_MAXSIZE)
  538. bix = 0;
  539. --sc->sc_rb.rb_td_nbusy;
  540. }
  541. if (sc->sc_rb.rb_td_nbusy == 0)
  542. ifp->if_timer = 0;
  543. if (sc->sc_rb.rb_tdtail != bix) {
  544. sc->sc_rb.rb_tdtail = bix;
  545. if (ifp->if_flags & IFF_OACTIVE) {
  546. ifp->if_flags &= ~IFF_OACTIVE;
  547. qestart(ifp);
  548. }
  549. }
  550. return (1);
  551. }
  552. /*
  553. * Receive interrupt.
  554. */
  555. int
  556. qe_rint(sc)
  557. struct qe_softc *sc;
  558. {
  559. struct qec_xd *xd = sc->sc_rb.rb_rxd;
  560. unsigned int bix, len;
  561. unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
  562. #ifdef QEDEBUG
  563. int npackets = 0;
  564. #endif
  565. bix = sc->sc_rb.rb_rdtail;
  566. /*
  567. * Process all buffers with valid data.
  568. */
  569. for (;;) {
  570. len = xd[bix].xd_flags;
  571. if (len & QEC_XD_OWN)
  572. break;
  573. #ifdef QEDEBUG
  574. npackets++;
  575. #endif
  576. len &= QEC_XD_LENGTH;
  577. len -= 4;
  578. qe_read(sc, bix, len);
  579. /* ... */
  580. xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
  581. QEC_XD_OWN | (QE_PKT_BUF_SZ & QEC_XD_LENGTH);
  582. if (++bix == QEC_XD_RING_MAXSIZE)
  583. bix = 0;
  584. }
  585. #ifdef QEDEBUG
  586. if (npackets == 0 && sc->sc_debug)
  587. printf("%s: rint: no packets; rb index %d; status 0x%x\n",
  588. sc->sc_dev.dv_xname, bix, len);
  589. #endif
  590. sc->sc_rb.rb_rdtail = bix;
  591. return (1);
  592. }
  593. /*
  594. * Error interrupt.
  595. */
  596. int
  597. qe_eint(sc, why)
  598. struct qe_softc *sc;
  599. u_int32_t why;
  600. {
  601. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  602. int r = 0, rst = 0;
  603. if (why & QE_CR_STAT_EDEFER) {
  604. printf("%s: excessive tx defers.\n", sc->sc_dev.dv_xname);
  605. r |= 1;
  606. ifp->if_oerrors++;
  607. }
  608. if (why & QE_CR_STAT_CLOSS) {
  609. ifp->if_oerrors++;
  610. r |= 1;
  611. }
  612. if (why & QE_CR_STAT_ERETRIES) {
  613. printf("%s: excessive tx retries\n", sc->sc_dev.dv_xname);
  614. ifp->if_oerrors++;
  615. r |= 1;
  616. rst = 1;
  617. }
  618. if (why & QE_CR_STAT_LCOLL) {
  619. printf("%s: late tx transmission\n", sc->sc_dev.dv_xname);
  620. ifp->if_oerrors++;
  621. r |= 1;
  622. rst = 1;
  623. }
  624. if (why & QE_CR_STAT_FUFLOW) {
  625. printf("%s: tx fifo underflow\n", sc->sc_dev.dv_xname);
  626. ifp->if_oerrors++;
  627. r |= 1;
  628. rst = 1;
  629. }
  630. if (why & QE_CR_STAT_JERROR) {
  631. printf("%s: jabber seen\n", sc->sc_dev.dv_xname);
  632. r |= 1;
  633. }
  634. if (why & QE_CR_STAT_BERROR) {
  635. printf("%s: babble seen\n", sc->sc_dev.dv_xname);
  636. r |= 1;
  637. }
  638. if (why & QE_CR_STAT_TCCOFLOW) {
  639. ifp->if_collisions += 256;
  640. ifp->if_oerrors += 256;
  641. r |= 1;
  642. }
  643. if (why & QE_CR_STAT_TXDERROR) {
  644. printf("%s: tx descriptor is bad\n", sc->sc_dev.dv_xname);
  645. rst = 1;
  646. r |= 1;
  647. }
  648. if (why & QE_CR_STAT_TXLERR) {
  649. printf("%s: tx late error\n", sc->sc_dev.dv_xname);
  650. ifp->if_oerrors++;
  651. rst = 1;
  652. r |= 1;
  653. }
  654. if (why & QE_CR_STAT_TXPERR) {
  655. printf("%s: tx dma parity error\n", sc->sc_dev.dv_xname);
  656. ifp->if_oerrors++;
  657. rst = 1;
  658. r |= 1;
  659. }
  660. if (why & QE_CR_STAT_TXSERR) {
  661. printf("%s: tx dma sbus error ack\n", sc->sc_dev.dv_xname);
  662. ifp->if_oerrors++;
  663. rst = 1;
  664. r |= 1;
  665. }
  666. if (why & QE_CR_STAT_RCCOFLOW) {
  667. ifp->if_collisions += 256;
  668. ifp->if_ierrors += 256;
  669. r |= 1;
  670. }
  671. if (why & QE_CR_STAT_RUOFLOW) {
  672. ifp->if_ierrors += 256;
  673. r |= 1;
  674. }
  675. if (why & QE_CR_STAT_MCOFLOW) {
  676. ifp->if_ierrors += 256;
  677. r |= 1;
  678. }
  679. if (why & QE_CR_STAT_RXFOFLOW) {
  680. printf("%s: rx fifo overflow\n", sc->sc_dev.dv_xname);
  681. ifp->if_ierrors++;
  682. r |= 1;
  683. }
  684. if (why & QE_CR_STAT_RLCOLL) {
  685. printf("%s: rx late collision\n", sc->sc_dev.dv_xname);
  686. ifp->if_ierrors++;
  687. ifp->if_collisions++;
  688. r |= 1;
  689. }
  690. if (why & QE_CR_STAT_FCOFLOW) {
  691. ifp->if_ierrors += 256;
  692. r |= 1;
  693. }
  694. if (why & QE_CR_STAT_CECOFLOW) {
  695. ifp->if_ierrors += 256;
  696. r |= 1;
  697. }
  698. if (why & QE_CR_STAT_RXDROP) {
  699. printf("%s: rx packet dropped\n", sc->sc_dev.dv_xname);
  700. ifp->if_ierrors++;
  701. r |= 1;
  702. }
  703. if (why & QE_CR_STAT_RXSMALL) {
  704. printf("%s: rx buffer too small\n", sc->sc_dev.dv_xname);
  705. ifp->if_ierrors++;
  706. r |= 1;
  707. rst = 1;
  708. }
  709. if (why & QE_CR_STAT_RXLERR) {
  710. printf("%s: rx late error\n", sc->sc_dev.dv_xname);
  711. ifp->if_ierrors++;
  712. r |= 1;
  713. rst = 1;
  714. }
  715. if (why & QE_CR_STAT_RXPERR) {
  716. printf("%s: rx dma parity error\n", sc->sc_dev.dv_xname);
  717. ifp->if_ierrors++;
  718. r |= 1;
  719. rst = 1;
  720. }
  721. if (why & QE_CR_STAT_RXSERR) {
  722. printf("%s: rx dma sbus error ack\n", sc->sc_dev.dv_xname);
  723. ifp->if_ierrors++;
  724. r |= 1;
  725. rst = 1;
  726. }
  727. if (r == 0)
  728. printf("%s: unexpected interrupt error: %08x\n",
  729. sc->sc_dev.dv_xname, why);
  730. if (rst) {
  731. printf("%s: resetting...\n", sc->sc_dev.dv_xname);
  732. qereset(sc);
  733. return (-1);
  734. }
  735. return (r);
  736. }
  737. int
  738. qeioctl(ifp, cmd, data)
  739. struct ifnet *ifp;
  740. u_long cmd;
  741. caddr_t data;
  742. {
  743. struct qe_softc *sc = ifp->if_softc;
  744. struct ifaddr *ifa = (struct ifaddr *)data;
  745. struct ifreq *ifr = (struct ifreq *)data;
  746. int s, error = 0;
  747. s = splnet();
  748. switch (cmd) {
  749. case SIOCSIFADDR:
  750. ifp->if_flags |= IFF_UP;
  751. switch (ifa->ifa_addr->sa_family) {
  752. case AF_INET:
  753. qeinit(sc);
  754. arp_ifinit(&sc->sc_arpcom, ifa);
  755. break;
  756. default:
  757. qeinit(sc);
  758. break;
  759. }
  760. break;
  761. case SIOCSIFFLAGS:
  762. if ((ifp->if_flags & IFF_UP) == 0 &&
  763. (ifp->if_flags & IFF_RUNNING) != 0) {
  764. /*
  765. * If interface is marked down and it is running, then
  766. * stop it.
  767. */
  768. qestop(sc);
  769. ifp->if_flags &= ~IFF_RUNNING;
  770. } else if ((ifp->if_flags & IFF_UP) != 0 &&
  771. (ifp->if_flags & IFF_RUNNING) == 0) {
  772. /*
  773. * If interface is marked up and it is stopped, then
  774. * start it.
  775. */
  776. qeinit(sc);
  777. } else {
  778. /*
  779. * Reset the interface to pick up changes in any other
  780. * flags that affect hardware registers.
  781. */
  782. qestop(sc);
  783. qeinit(sc);
  784. }
  785. #ifdef QEDEBUG
  786. sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
  787. #endif
  788. break;
  789. case SIOCGIFMEDIA:
  790. case SIOCSIFMEDIA:
  791. error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
  792. break;
  793. default:
  794. error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data);
  795. }
  796. if (error == ENETRESET) {
  797. if (ifp->if_flags & IFF_RUNNING)
  798. qe_mcreset(sc);
  799. error = 0;
  800. }
  801. splx(s);
  802. return (error);
  803. }
  804. void
  805. qeinit(sc)
  806. struct qe_softc *sc;
  807. {
  808. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  809. bus_space_tag_t t = sc->sc_bustag;
  810. bus_space_handle_t cr = sc->sc_cr;
  811. bus_space_handle_t mr = sc->sc_mr;
  812. struct qec_softc *qec = sc->sc_qec;
  813. u_int32_t qecaddr;
  814. u_int8_t *ea;
  815. int s;
  816. s = splnet();
  817. qestop(sc);
  818. /*
  819. * Allocate descriptor ring and buffers
  820. */
  821. qec_meminit(&sc->sc_rb, QE_PKT_BUF_SZ);
  822. /* Channel registers: */
  823. bus_space_write_4(t, cr, QE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
  824. bus_space_write_4(t, cr, QE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
  825. bus_space_write_4(t, cr, QE_CRI_RIMASK, 0);
  826. bus_space_write_4(t, cr, QE_CRI_TIMASK, 0);
  827. bus_space_write_4(t, cr, QE_CRI_QMASK, 0);
  828. bus_space_write_4(t, cr, QE_CRI_MMASK, QE_CR_MMASK_RXCOLL);
  829. bus_space_write_4(t, cr, QE_CRI_CCNT, 0);
  830. bus_space_write_4(t, cr, QE_CRI_PIPG, 0);
  831. qecaddr = sc->sc_channel * qec->sc_msize;
  832. bus_space_write_4(t, cr, QE_CRI_RXWBUF, qecaddr);
  833. bus_space_write_4(t, cr, QE_CRI_RXRBUF, qecaddr);
  834. bus_space_write_4(t, cr, QE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
  835. bus_space_write_4(t, cr, QE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
  836. /*
  837. * When switching from mace<->qec always guarantee an sbus
  838. * turnaround (if last op was read, perform a dummy write, and
  839. * vice versa).
  840. */
  841. bus_space_read_4(t, cr, QE_CRI_QMASK);
  842. /* MACE registers: */
  843. bus_space_write_1(t, mr, QE_MRI_PHYCC, QE_MR_PHYCC_ASEL);
  844. bus_space_write_1(t, mr, QE_MRI_XMTFC, QE_MR_XMTFC_APADXMT);
  845. bus_space_write_1(t, mr, QE_MRI_RCVFC, 0);
  846. /*
  847. * Mask MACE's receive interrupt, since we're being notified
  848. * by the QEC after DMA completes.
  849. */
  850. bus_space_write_1(t, mr, QE_MRI_IMR,
  851. QE_MR_IMR_CERRM | QE_MR_IMR_RCVINTM);
  852. bus_space_write_1(t, mr, QE_MRI_BIUCC,
  853. QE_MR_BIUCC_BSWAP | QE_MR_BIUCC_64TS);
  854. bus_space_write_1(t, mr, QE_MRI_FIFOFC,
  855. QE_MR_FIFOCC_TXF16 | QE_MR_FIFOCC_RXF32 |
  856. QE_MR_FIFOCC_RFWU | QE_MR_FIFOCC_TFWU);
  857. bus_space_write_1(t, mr, QE_MRI_PLSCC, QE_MR_PLSCC_TP);
  858. /*
  859. * Station address
  860. */
  861. ea = sc->sc_arpcom.ac_enaddr;
  862. bus_space_write_1(t, mr, QE_MRI_IAC,
  863. QE_MR_IAC_ADDRCHG | QE_MR_IAC_PHYADDR);
  864. bus_space_write_multi_1(t, mr, QE_MRI_PADR, ea, 6);
  865. /* Apply media settings */
  866. qe_ifmedia_upd(ifp);
  867. /*
  868. * Clear Logical address filter
  869. */
  870. bus_space_write_1(t, mr, QE_MRI_IAC,
  871. QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
  872. bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0, 8);
  873. bus_space_write_1(t, mr, QE_MRI_IAC, 0);
  874. /* Clear missed packet count (register cleared on read) */
  875. (void)bus_space_read_1(t, mr, QE_MRI_MPC);
  876. #if 0
  877. /* test register: */
  878. bus_space_write_1(t, mr, QE_MRI_UTR, 0);
  879. #endif
  880. /* Reset multicast filter */
  881. qe_mcreset(sc);
  882. ifp->if_flags |= IFF_RUNNING;
  883. ifp->if_flags &= ~IFF_OACTIVE;
  884. splx(s);
  885. }
  886. /*
  887. * Reset multicast filter.
  888. */
  889. void
  890. qe_mcreset(sc)
  891. struct qe_softc *sc;
  892. {
  893. struct arpcom *ac = &sc->sc_arpcom;
  894. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  895. bus_space_tag_t t = sc->sc_bustag;
  896. bus_space_handle_t mr = sc->sc_mr;
  897. struct ether_multi *enm;
  898. struct ether_multistep step;
  899. u_int32_t crc;
  900. u_int16_t hash[4];
  901. u_int8_t octet, maccc, *ladrp = (u_int8_t *)&hash[0];
  902. int i, j;
  903. /* We also enable transmitter & receiver here */
  904. maccc = QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV;
  905. if (ifp->if_flags & IFF_PROMISC) {
  906. maccc |= QE_MR_MACCC_PROM;
  907. bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
  908. return;
  909. }
  910. if (ac->ac_multirangecnt > 0)
  911. ifp->if_flags |= IFF_ALLMULTI;
  912. if (ifp->if_flags & IFF_ALLMULTI) {
  913. bus_space_write_1(t, mr, QE_MRI_IAC,
  914. QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
  915. bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0xff, 8);
  916. bus_space_write_1(t, mr, QE_MRI_IAC, 0);
  917. bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
  918. return;
  919. }
  920. hash[3] = hash[2] = hash[1] = hash[0] = 0;
  921. ETHER_FIRST_MULTI(step, ac, enm);
  922. while (enm != NULL) {
  923. crc = 0xffffffff;
  924. for (i = 0; i < ETHER_ADDR_LEN; i++) {
  925. octet = enm->enm_addrlo[i];
  926. for (j = 0; j < 8; j++) {
  927. if ((crc & 1) ^ (octet & 1)) {
  928. crc >>= 1;
  929. crc ^= MC_POLY_LE;
  930. }
  931. else
  932. crc >>= 1;
  933. octet >>= 1;
  934. }
  935. }
  936. crc >>= 26;
  937. hash[crc >> 4] |= 1 << (crc & 0xf);
  938. ETHER_NEXT_MULTI(step, enm);
  939. }
  940. bus_space_write_1(t, mr, QE_MRI_IAC,
  941. QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
  942. bus_space_write_multi_1(t, mr, QE_MRI_LADRF, ladrp, 8);
  943. bus_space_write_1(t, mr, QE_MRI_IAC, 0);
  944. bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
  945. }
  946. /*
  947. * Get current media settings.
  948. */
  949. void
  950. qe_ifmedia_sts(ifp, ifmr)
  951. struct ifnet *ifp;
  952. struct ifmediareq *ifmr;
  953. {
  954. struct qe_softc *sc = ifp->if_softc;
  955. u_int8_t phycc;
  956. ifmr->ifm_active = IFM_ETHER | IFM_10_T;
  957. phycc = bus_space_read_1(sc->sc_bustag, sc->sc_mr, QE_MRI_PHYCC);
  958. if ((phycc & QE_MR_PHYCC_DLNKTST) == 0) {
  959. ifmr->ifm_status |= IFM_AVALID;
  960. if (phycc & QE_MR_PHYCC_LNKFL)
  961. ifmr->ifm_status &= ~IFM_ACTIVE;
  962. else
  963. ifmr->ifm_status |= IFM_ACTIVE;
  964. }
  965. }
  966. /*
  967. * Set media options.
  968. */
  969. int
  970. qe_ifmedia_upd(ifp)
  971. struct ifnet *ifp;
  972. {
  973. struct qe_softc *sc = ifp->if_softc;
  974. int media = sc->sc_ifmedia.ifm_media;
  975. if (IFM_TYPE(media) != IFM_ETHER)
  976. return (EINVAL);
  977. if (IFM_SUBTYPE(media) != IFM_10_T)
  978. return (EINVAL);
  979. return (0);
  980. }