if_ie.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /* $OpenBSD: if_ie.c,v 1.43 2015/06/24 09:40:54 mpi Exp $ */
  2. /* $NetBSD: if_ie.c,v 1.51 1996/05/12 23:52:48 mycroft Exp $ */
  3. /*-
  4. * Copyright (c) 1993, 1994, 1995 Charles Hannum.
  5. * Copyright (c) 1992, 1993, University of Vermont and State
  6. * Agricultural College.
  7. * Copyright (c) 1992, 1993, Garrett A. Wollman.
  8. *
  9. * Portions:
  10. * Copyright (c) 1993, 1994, 1995, Rodney W. Grimes
  11. * Copyright (c) 1994, 1995, Rafal K. Boni
  12. * Copyright (c) 1990, 1991, William F. Jolitz
  13. * Copyright (c) 1990, The Regents of the University of California
  14. *
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * 3. All advertising materials mentioning features or use of this software
  26. * must display the following acknowledgement:
  27. * This product includes software developed by Charles Hannum, by the
  28. * University of Vermont and State Agricultural College and Garrett A.
  29. * Wollman, by William F. Jolitz, and by the University of California,
  30. * Berkeley, Lawrence Berkeley Laboratory, and its contributors.
  31. * 4. Neither the names of the Universities nor the names of the authors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. */
  47. /*
  48. * Intel 82586 Ethernet chip
  49. * Register, bit, and structure definitions.
  50. *
  51. * Original StarLAN driver written by Garrett Wollman with reference to the
  52. * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
  53. *
  54. * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
  55. *
  56. * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
  57. *
  58. * Intel EtherExpress 16 support taken from FreeBSD's if_ix.c, written
  59. * by Rodney W. Grimes.
  60. *
  61. * Majorly cleaned up and 3C507 code merged by Charles Hannum.
  62. */
  63. /*
  64. * The i82586 is a very versatile chip, found in many implementations.
  65. * Programming this chip is mostly the same, but certain details differ
  66. * from card to card. This driver is written so that different cards
  67. * can be automatically detected at run-time.
  68. */
  69. /*
  70. Mode of operation:
  71. We run the 82586 in a standard Ethernet mode. We keep NFRAMES received frame
  72. descriptors around for the receiver to use, and NRXBUF associated receive
  73. buffer descriptors, both in a circular list. Whenever a frame is received, we
  74. rotate both lists as necessary. (The 586 treats both lists as a simple
  75. queue.) We also keep a transmit command around so that packets can be sent
  76. off quickly.
  77. We configure the adapter in AL-LOC = 1 mode, which means that the
  78. Ethernet/802.3 MAC header is placed at the beginning of the receive buffer
  79. rather than being split off into various fields in the RFD. This also means
  80. that we must include this header in the transmit buffer as well.
  81. By convention, all transmit commands, and only transmit commands, shall have
  82. the I (IE_CMD_INTR) bit set in the command. This way, when an interrupt
  83. arrives at ieintr(), it is immediately possible to tell what precisely caused
  84. it. ANY OTHER command-sending routines should run at splnet(), and should
  85. post an acknowledgement to every interrupt they generate.
  86. The 82586 has a 24-bit address space internally, and the adaptor's memory is
  87. located at the top of this region. However, the value we are given in
  88. configuration is the CPU's idea of where the adaptor RAM is. So, we must go
  89. through a few gyrations to come up with a kernel virtual address which
  90. represents the actual beginning of the 586 address space. First, we autosize
  91. the RAM by running through several possible sizes and trying to initialize the
  92. adapter under the assumption that the selected size is correct. Then, knowing
  93. the correct RAM size, we set up our pointers in the softc. `sc_maddr'
  94. represents the computed base of the 586 address space. `iomembot' represents
  95. the actual configured base of adapter RAM. Finally, `sc_msize' represents the
  96. calculated size of 586 RAM. Then, when laying out commands, we use the
  97. interval [sc_maddr, sc_maddr + sc_msize); to make 24-pointers, we subtract
  98. iomem, and to make 16-pointers, we subtract sc_maddr and and with 0xffff.
  99. */
  100. #include "bpfilter.h"
  101. #include <sys/param.h>
  102. #include <sys/systm.h>
  103. #include <sys/mbuf.h>
  104. #include <sys/buf.h>
  105. #include <sys/protosw.h>
  106. #include <sys/socket.h>
  107. #include <sys/ioctl.h>
  108. #include <sys/errno.h>
  109. #include <sys/syslog.h>
  110. #include <sys/device.h>
  111. #include <sys/timeout.h>
  112. #include <net/if.h>
  113. #include <net/if_types.h>
  114. #include <net/if_dl.h>
  115. #include <net/netisr.h>
  116. #include <net/route.h>
  117. #if NBPFILTER > 0
  118. #include <net/bpf.h>
  119. #endif
  120. #include <netinet/in.h>
  121. #include <netinet/if_ether.h>
  122. #include <machine/cpu.h>
  123. #include <machine/bus.h>
  124. #include <machine/intr.h>
  125. #include <dev/isa/isareg.h>
  126. #include <dev/isa/isavar.h>
  127. #include <i386/isa/isa_machdep.h> /* XXX USES ISA HOLE DIRECTLY */
  128. #include <dev/ic/i82586reg.h>
  129. #include <dev/isa/if_ieatt.h>
  130. #include <dev/isa/if_ie507.h>
  131. #include <dev/isa/if_iee16.h>
  132. #include <dev/isa/elink.h>
  133. #define IED_RINT 0x01
  134. #define IED_TINT 0x02
  135. #define IED_RNR 0x04
  136. #define IED_CNA 0x08
  137. #define IED_READFRAME 0x10
  138. #define IED_ENQ 0x20
  139. #define IED_XMIT 0x40
  140. #define IED_ALL 0x7f
  141. /*
  142. sizeof(iscp) == 1+1+2+4 == 8
  143. sizeof(scb) == 2+2+2+2+2+2+2+2 == 16
  144. NFRAMES * sizeof(rfd) == NFRAMES*(2+2+2+2+6+6+2+2) == NFRAMES*24 == 384
  145. sizeof(xmit_cmd) == 2+2+2+2+6+2 == 18
  146. sizeof(transmit buffer) == ETHER_MAX_LEN == 1518
  147. sizeof(transmit buffer desc) == 8
  148. -----
  149. 1952
  150. NRXBUF * sizeof(rbd) == NRXBUF*(2+2+4+2+2) == NRXBUF*12
  151. NRXBUF * IE_RBUF_SIZE == NRXBUF*256
  152. NRXBUF should be (16384 - 1952) / (256 + 12) == 14432 / 268 == 53
  153. With NRXBUF == 48, this leaves us 1568 bytes for another command or
  154. more buffers. Another transmit command would be 18+8+1518 == 1544
  155. ---just barely fits!
  156. Obviously all these would have to be reduced for smaller memory sizes.
  157. With a larger memory, it would be possible to roughly double the number of
  158. both transmit and receive buffers.
  159. */
  160. #define NFRAMES 16 /* number of receive frames */
  161. #define NRXBUF 48 /* number of buffers to allocate */
  162. #define IE_RBUF_SIZE 256 /* size of each receive buffer;
  163. MUST BE POWER OF TWO */
  164. #define NTXBUF 2 /* number of transmit commands */
  165. #define IE_TBUF_SIZE ETHER_MAX_LEN /* length of transmit buffer */
  166. enum ie_hardware {
  167. IE_STARLAN10,
  168. IE_EN100,
  169. IE_SLFIBER,
  170. IE_3C507,
  171. IE_EE16,
  172. IE_UNKNOWN
  173. };
  174. const char *ie_hardware_names[] = {
  175. "StarLAN 10",
  176. "EN100",
  177. "StarLAN Fiber",
  178. "3C507",
  179. "EtherExpress 16",
  180. "Unknown"
  181. };
  182. /*
  183. * Ethernet status, per interface.
  184. */
  185. struct ie_softc {
  186. struct device sc_dev;
  187. void *sc_ih;
  188. int sc_iobase;
  189. caddr_t sc_maddr;
  190. u_int sc_msize;
  191. struct arpcom sc_arpcom;
  192. void (*reset_586)(struct ie_softc *);
  193. void (*chan_attn)(struct ie_softc *);
  194. enum ie_hardware hard_type;
  195. int hard_vers;
  196. int want_mcsetup;
  197. int promisc;
  198. volatile struct ie_int_sys_conf_ptr *iscp;
  199. volatile struct ie_sys_ctl_block *scb;
  200. int rfhead, rftail, rbhead, rbtail;
  201. volatile struct ie_recv_frame_desc *rframes[NFRAMES];
  202. volatile struct ie_recv_buf_desc *rbuffs[NRXBUF];
  203. volatile char *cbuffs[NRXBUF];
  204. int xmit_busy;
  205. int xchead, xctail;
  206. volatile struct ie_xmit_cmd *xmit_cmds[NTXBUF];
  207. volatile struct ie_xmit_buf *xmit_buffs[NTXBUF];
  208. u_char *xmit_cbuffs[NTXBUF];
  209. struct ie_en_addr mcast_addrs[MAXMCAST + 1];
  210. int mcast_count;
  211. u_short irq_encoded; /* encoded interrupt on IEE16 */
  212. #ifdef IEDEBUG
  213. int sc_debug;
  214. #endif
  215. };
  216. void iewatchdog(struct ifnet *);
  217. int ieintr(void *);
  218. void iestop(struct ie_softc *);
  219. int ieinit(struct ie_softc *);
  220. int ieioctl(struct ifnet *, u_long, caddr_t);
  221. void iestart(struct ifnet *);
  222. static void el_reset_586(struct ie_softc *);
  223. static void sl_reset_586(struct ie_softc *);
  224. static void el_chan_attn(struct ie_softc *);
  225. static void sl_chan_attn(struct ie_softc *);
  226. static void slel_get_address(struct ie_softc *);
  227. static void ee16_reset_586(struct ie_softc *);
  228. static void ee16_chan_attn(struct ie_softc *);
  229. static void ee16_interrupt_enable(struct ie_softc *);
  230. void ee16_eeprom_outbits(struct ie_softc *, int, int);
  231. void ee16_eeprom_clock(struct ie_softc *, int);
  232. u_short ee16_read_eeprom(struct ie_softc *, int);
  233. int ee16_eeprom_inbits(struct ie_softc *);
  234. void iereset(struct ie_softc *);
  235. void ie_readframe(struct ie_softc *, int);
  236. void ie_drop_packet_buffer(struct ie_softc *);
  237. void ie_find_mem_size(struct ie_softc *);
  238. static int command_and_wait(struct ie_softc *, int,
  239. void volatile *, int);
  240. void ierint(struct ie_softc *);
  241. void ietint(struct ie_softc *);
  242. void iexmit(struct ie_softc *);
  243. struct mbuf *ieget(struct ie_softc *, struct ether_header *);
  244. void iememinit(void *, struct ie_softc *);
  245. static int mc_setup(struct ie_softc *, void *);
  246. static void mc_reset(struct ie_softc *);
  247. #ifdef IEDEBUG
  248. void print_rbd(volatile struct ie_recv_buf_desc *);
  249. int in_ierint = 0;
  250. int in_ietint = 0;
  251. #endif
  252. int ieprobe(struct device *, void *, void *);
  253. void ieattach(struct device *, struct device *, void *);
  254. int sl_probe(struct ie_softc *, struct isa_attach_args *);
  255. int el_probe(struct ie_softc *, struct isa_attach_args *);
  256. int ee16_probe(struct ie_softc *, struct isa_attach_args *);
  257. int check_ie_present(struct ie_softc *, caddr_t, u_int);
  258. static __inline void ie_setup_config(volatile struct ie_config_cmd *,
  259. int, int);
  260. static __inline void ie_ack(struct ie_softc *, u_int);
  261. static __inline int ether_equal(u_char *, u_char *);
  262. static __inline int check_eh(struct ie_softc *, struct ether_header *);
  263. static __inline int ie_buflen(struct ie_softc *, int);
  264. static __inline int ie_packet_len(struct ie_softc *);
  265. static void run_tdr(struct ie_softc *, struct ie_tdr_cmd *);
  266. struct cfattach ie_isa_ca = {
  267. sizeof(struct ie_softc), ieprobe, ieattach
  268. };
  269. struct cfdriver ie_cd = {
  270. NULL, "ie", DV_IFNET
  271. };
  272. #define MK_24(base, ptr) ((caddr_t)((u_long)ptr - (u_long)base))
  273. #define MK_16(base, ptr) ((u_short)(u_long)MK_24(base, ptr))
  274. #define PORT sc->sc_iobase
  275. #define MEM sc->sc_maddr
  276. /*
  277. * Here are a few useful functions. We could have done these as macros, but
  278. * since we have the inline facility, it makes sense to use that instead.
  279. */
  280. static __inline void
  281. ie_setup_config(cmd, promiscuous, manchester)
  282. volatile struct ie_config_cmd *cmd;
  283. int promiscuous, manchester;
  284. {
  285. cmd->ie_config_count = 0x0c;
  286. cmd->ie_fifo = 8;
  287. cmd->ie_save_bad = 0x40;
  288. cmd->ie_addr_len = 0x2e;
  289. cmd->ie_priority = 0;
  290. cmd->ie_ifs = 0x60;
  291. cmd->ie_slot_low = 0;
  292. cmd->ie_slot_high = 0xf2;
  293. cmd->ie_promisc = promiscuous | manchester << 2;
  294. cmd->ie_crs_cdt = 0;
  295. cmd->ie_min_len = 64;
  296. cmd->ie_junk = 0xff;
  297. }
  298. static __inline void
  299. ie_ack(sc, mask)
  300. struct ie_softc *sc;
  301. u_int mask;
  302. {
  303. volatile struct ie_sys_ctl_block *scb = sc->scb;
  304. scb->ie_command = scb->ie_status & mask;
  305. (sc->chan_attn)(sc);
  306. while (scb->ie_command)
  307. ; /* Spin Lock */
  308. }
  309. int
  310. ieprobe(parent, match, aux)
  311. struct device *parent;
  312. void *match, *aux;
  313. {
  314. struct ie_softc *sc = match;
  315. struct isa_attach_args *ia = aux;
  316. if (sl_probe(sc, ia))
  317. return 1;
  318. if (el_probe(sc, ia))
  319. return 1;
  320. if (ee16_probe(sc, ia))
  321. return 1;
  322. return 0;
  323. }
  324. int
  325. sl_probe(sc, ia)
  326. struct ie_softc *sc;
  327. struct isa_attach_args *ia;
  328. {
  329. u_char c;
  330. sc->sc_iobase = ia->ia_iobase;
  331. /* Need this for part of the probe. */
  332. sc->reset_586 = sl_reset_586;
  333. sc->chan_attn = sl_chan_attn;
  334. c = inb(PORT + IEATT_REVISION);
  335. switch (SL_BOARD(c)) {
  336. case SL10_BOARD:
  337. sc->hard_type = IE_STARLAN10;
  338. break;
  339. case EN100_BOARD:
  340. sc->hard_type = IE_EN100;
  341. break;
  342. case SLFIBER_BOARD:
  343. sc->hard_type = IE_SLFIBER;
  344. break;
  345. default:
  346. /* Anything else is not recognized or cannot be used. */
  347. #if 0
  348. printf("%s: unknown AT&T board type code %d\n",
  349. sc->sc_dev.dv_xname, SL_BOARD(c));
  350. #endif
  351. return 0;
  352. }
  353. sc->hard_vers = SL_REV(c);
  354. if (ia->ia_irq == IRQUNK || ia->ia_maddr == MADDRUNK) {
  355. printf("%s: %s does not have soft configuration\n",
  356. sc->sc_dev.dv_xname, ie_hardware_names[sc->hard_type]);
  357. return 0;
  358. }
  359. /*
  360. * Divine memory size on-board the card. Ususally 16k.
  361. */
  362. sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
  363. ie_find_mem_size(sc);
  364. if (!sc->sc_msize) {
  365. printf("%s: can't find shared memory\n", sc->sc_dev.dv_xname);
  366. return 0;
  367. }
  368. if (!ia->ia_msize)
  369. ia->ia_msize = sc->sc_msize;
  370. else if (ia->ia_msize != sc->sc_msize) {
  371. printf("%s: msize mismatch; kernel configured %d != board configured %d\n",
  372. sc->sc_dev.dv_xname, ia->ia_msize, sc->sc_msize);
  373. return 0;
  374. }
  375. slel_get_address(sc);
  376. ia->ia_iosize = 16;
  377. return 1;
  378. }
  379. int
  380. el_probe(sc, ia)
  381. struct ie_softc *sc;
  382. struct isa_attach_args *ia;
  383. {
  384. bus_space_tag_t iot = ia->ia_iot;
  385. bus_space_handle_t ioh;
  386. u_char c;
  387. int i, rval = 0;
  388. u_char signature[] = "*3COM*";
  389. sc->sc_iobase = ia->ia_iobase;
  390. /* Need this for part of the probe. */
  391. sc->reset_586 = el_reset_586;
  392. sc->chan_attn = el_chan_attn;
  393. /*
  394. * Map the Etherlink ID port for the probe sequence.
  395. */
  396. if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
  397. printf("3c507 probe: can't map Etherlink ID port\n");
  398. return 0;
  399. }
  400. /*
  401. * Reset and put card in CONFIG state without changing address.
  402. * XXX Indirect brokenness here!
  403. */
  404. elink_reset(iot, ioh, sc->sc_dev.dv_parent->dv_unit);
  405. elink_idseq(iot, ioh, ELINK_507_POLY);
  406. elink_idseq(iot, ioh, ELINK_507_POLY);
  407. outb(ELINK_ID_PORT, 0xff);
  408. /* Check for 3COM signature before proceeding. */
  409. outb(PORT + IE507_CTRL, inb(PORT + IE507_CTRL) & 0xfc); /* XXX */
  410. for (i = 0; i < 6; i++)
  411. if (inb(PORT + i) != signature[i])
  412. goto out;
  413. c = inb(PORT + IE507_MADDR);
  414. if (c & 0x20) {
  415. printf("%s: can't map 3C507 RAM in high memory\n",
  416. sc->sc_dev.dv_xname);
  417. goto out;
  418. }
  419. /* Go to RUN state. */
  420. outb(ELINK_ID_PORT, 0x00);
  421. elink_idseq(iot, ioh, ELINK_507_POLY);
  422. outb(ELINK_ID_PORT, 0x00);
  423. /* Set bank 2 for version info and read BCD version byte. */
  424. outb(PORT + IE507_CTRL, EL_CTRL_NRST | EL_CTRL_BNK2);
  425. i = inb(PORT + 3);
  426. sc->hard_type = IE_3C507;
  427. sc->hard_vers = 10*(i / 16) + (i % 16) - 1;
  428. i = inb(PORT + IE507_IRQ) & 0x0f;
  429. if (ia->ia_irq != IRQUNK) {
  430. if (ia->ia_irq != i) {
  431. printf("%s: irq mismatch; kernel configured %d != board configured %d\n",
  432. sc->sc_dev.dv_xname, ia->ia_irq, i);
  433. goto out;
  434. }
  435. } else
  436. ia->ia_irq = i;
  437. i = ((inb(PORT + IE507_MADDR) & 0x1c) << 12) + 0xc0000;
  438. if (ia->ia_maddr != MADDRUNK) {
  439. if (ia->ia_maddr != i) {
  440. printf("%s: maddr mismatch; kernel configured %x != board configured %x\n",
  441. sc->sc_dev.dv_xname, ia->ia_maddr, i);
  442. goto out;
  443. }
  444. } else
  445. ia->ia_maddr = i;
  446. outb(PORT + IE507_CTRL, EL_CTRL_NORMAL);
  447. /*
  448. * Divine memory size on-board the card.
  449. */
  450. sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
  451. ie_find_mem_size(sc);
  452. if (!sc->sc_msize) {
  453. printf("%s: can't find shared memory\n", sc->sc_dev.dv_xname);
  454. outb(PORT + IE507_CTRL, EL_CTRL_NRST);
  455. goto out;
  456. }
  457. if (!ia->ia_msize)
  458. ia->ia_msize = sc->sc_msize;
  459. else if (ia->ia_msize != sc->sc_msize) {
  460. printf("%s: msize mismatch; kernel configured %d != board configured %d\n",
  461. sc->sc_dev.dv_xname, ia->ia_msize, sc->sc_msize);
  462. outb(PORT + IE507_CTRL, EL_CTRL_NRST);
  463. goto out;
  464. }
  465. slel_get_address(sc);
  466. /* Clear the interrupt latch just in case. */
  467. outb(PORT + IE507_ICTRL, 1);
  468. ia->ia_iosize = 16;
  469. rval = 1;
  470. out:
  471. bus_space_unmap(iot, ioh, 1);
  472. return rval;
  473. }
  474. /* Taken almost exactly from Rod's if_ix.c. */
  475. int
  476. ee16_probe(sc, ia)
  477. struct ie_softc *sc;
  478. struct isa_attach_args *ia;
  479. {
  480. int i;
  481. u_short board_id, id_var1, id_var2, checksum = 0;
  482. u_short eaddrtemp, irq;
  483. u_short pg, adjust, decode, edecode;
  484. u_char bart_config;
  485. short irq_translate[] = {0, 0x09, 0x03, 0x04, 0x05, 0x0a, 0x0b, 0};
  486. /* Need this for part of the probe. */
  487. sc->reset_586 = ee16_reset_586;
  488. sc->chan_attn = ee16_chan_attn;
  489. /* reset any ee16 at the current iobase */
  490. outb(ia->ia_iobase + IEE16_ECTRL, IEE16_RESET_ASIC);
  491. outb(ia->ia_iobase + IEE16_ECTRL, 0);
  492. delay(240);
  493. /* now look for ee16. */
  494. board_id = id_var1 = id_var2 = 0;
  495. for (i=0; i<4 ; i++) {
  496. id_var1 = inb(ia->ia_iobase + IEE16_ID_PORT);
  497. id_var2 = ((id_var1 & 0x03) << 2);
  498. board_id |= (( id_var1 >> 4) << id_var2);
  499. }
  500. if (board_id != IEE16_ID)
  501. return 0;
  502. /* need sc->sc_iobase for ee16_read_eeprom */
  503. sc->sc_iobase = ia->ia_iobase;
  504. sc->hard_type = IE_EE16;
  505. /*
  506. * If ia->maddr == MADDRUNK, use value in eeprom location 6.
  507. *
  508. * The shared RAM location on the EE16 is encoded into bits
  509. * 3-7 of EEPROM location 6. We zero the upper byte, and
  510. * shift the 5 bits right 3. The resulting number tells us
  511. * the RAM location. Because the EE16 supports either 16k or 32k
  512. * of shared RAM, we only worry about the 32k locations.
  513. *
  514. * NOTE: if a 64k EE16 exists, it should be added to this switch.
  515. * then the ia->ia_msize would need to be set per case statement.
  516. *
  517. * value msize location
  518. * ===== ===== ========
  519. * 0x03 0x8000 0xCC000
  520. * 0x06 0x8000 0xD0000
  521. * 0x0C 0x8000 0xD4000
  522. * 0x18 0x8000 0xD8000
  523. *
  524. */
  525. if ((ia->ia_maddr == MADDRUNK) || (ia->ia_msize == 0)) {
  526. i = (ee16_read_eeprom(sc, 6) & 0x00ff ) >> 3;
  527. switch(i) {
  528. case 0x03:
  529. ia->ia_maddr = 0xCC000;
  530. break;
  531. case 0x06:
  532. ia->ia_maddr = 0xD0000;
  533. break;
  534. case 0x0c:
  535. ia->ia_maddr = 0xD4000;
  536. break;
  537. case 0x18:
  538. ia->ia_maddr = 0xD8000;
  539. break;
  540. default:
  541. return 0 ;
  542. break; /* NOTREACHED */
  543. }
  544. ia->ia_msize = 0x8000;
  545. }
  546. /* need to set these after checking for MADDRUNK */
  547. sc->sc_maddr = ISA_HOLE_VADDR(ia->ia_maddr);
  548. sc->sc_msize = ia->ia_msize;
  549. /* need to put the 586 in RESET, and leave it */
  550. outb( PORT + IEE16_ECTRL, IEE16_RESET_586);
  551. /* read the eeprom and checksum it, should == IEE16_ID */
  552. for(i=0 ; i< 0x40 ; i++)
  553. checksum += ee16_read_eeprom(sc, i);
  554. if (checksum != IEE16_ID)
  555. return 0;
  556. /*
  557. * Size and test the memory on the board. The size of the memory
  558. * can be one of 16k, 32k, 48k or 64k. It can be located in the
  559. * address range 0xC0000 to 0xEFFFF on 16k boundaries.
  560. *
  561. * If the size does not match the passed in memory allocation size
  562. * issue a warning, but continue with the minimum of the two sizes.
  563. */
  564. switch (ia->ia_msize) {
  565. case 65536:
  566. case 32768: /* XXX Only support 32k and 64k right now */
  567. break;
  568. case 16384:
  569. case 49512:
  570. default:
  571. printf("ieprobe mapped memory size out of range\n");
  572. return 0;
  573. break; /* NOTREACHED */
  574. }
  575. if ((kvtop(sc->sc_maddr) < 0xC0000) ||
  576. (kvtop(sc->sc_maddr) + sc->sc_msize > 0xF0000)) {
  577. printf("ieprobe mapped memory address out of range\n");
  578. return 0;
  579. }
  580. pg = (kvtop(sc->sc_maddr) & 0x3C000) >> 14;
  581. adjust = IEE16_MCTRL_FMCS16 | (pg & 0x3) << 2;
  582. decode = ((1 << (sc->sc_msize / 16384)) - 1) << pg;
  583. edecode = ((~decode >> 4) & 0xF0) | (decode >> 8);
  584. /* ZZZ This should be checked against eeprom location 6, low byte */
  585. outb(PORT + IEE16_MEMDEC, decode & 0xFF);
  586. /* ZZZ This should be checked against eeprom location 1, low byte */
  587. outb(PORT + IEE16_MCTRL, adjust);
  588. /* ZZZ Now if I could find this one I would have it made */
  589. outb(PORT + IEE16_MPCTRL, (~decode & 0xFF));
  590. /* ZZZ I think this is location 6, high byte */
  591. outb(PORT + IEE16_MECTRL, edecode); /*XXX disable Exxx */
  592. /*
  593. * first prime the stupid bart DRAM controller so that it
  594. * works, then zero out all of memory.
  595. */
  596. bzero(sc->sc_maddr, 32);
  597. bzero(sc->sc_maddr, sc->sc_msize);
  598. /*
  599. * Get the encoded interrupt number from the EEPROM, check it
  600. * against the passed in IRQ. Issue a warning if they do not
  601. * match, and fail the probe. If irq is 'IRQUNK' then we
  602. * use the EEPROM irq, and continue.
  603. */
  604. irq = ee16_read_eeprom(sc, IEE16_EEPROM_CONFIG1);
  605. irq = (irq & IEE16_EEPROM_IRQ) >> IEE16_EEPROM_IRQ_SHIFT;
  606. sc->irq_encoded = irq;
  607. irq = irq_translate[irq];
  608. if (ia->ia_irq != IRQUNK) {
  609. if (irq != ia->ia_irq) {
  610. #ifdef DIAGNOSTIC
  611. printf("\nie%d: fatal: board IRQ %d does not match kernel\n", sc->sc_dev.dv_unit, irq);
  612. #endif /* DIAGNOSTIC */
  613. return 0; /* _must_ match or probe fails */
  614. }
  615. } else
  616. ia->ia_irq = irq;
  617. /*
  618. * Get the hardware ethernet address from the EEPROM and
  619. * save it in the softc for use by the 586 setup code.
  620. */
  621. eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_HIGH);
  622. sc->sc_arpcom.ac_enaddr[1] = eaddrtemp & 0xFF;
  623. sc->sc_arpcom.ac_enaddr[0] = eaddrtemp >> 8;
  624. eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_MID);
  625. sc->sc_arpcom.ac_enaddr[3] = eaddrtemp & 0xFF;
  626. sc->sc_arpcom.ac_enaddr[2] = eaddrtemp >> 8;
  627. eaddrtemp = ee16_read_eeprom(sc, IEE16_EEPROM_ENET_LOW);
  628. sc->sc_arpcom.ac_enaddr[5] = eaddrtemp & 0xFF;
  629. sc->sc_arpcom.ac_enaddr[4] = eaddrtemp >> 8;
  630. /* disable the board interrupts */
  631. outb(PORT + IEE16_IRQ, sc->irq_encoded);
  632. /* enable loopback to keep bad packets off the wire */
  633. if(sc->hard_type == IE_EE16) {
  634. bart_config = inb(PORT + IEE16_CONFIG);
  635. bart_config |= IEE16_BART_LOOPBACK;
  636. bart_config |= IEE16_BART_MCS16_TEST; /* inb doesn't get bit! */
  637. outb(PORT + IEE16_CONFIG, bart_config);
  638. bart_config = inb(PORT + IEE16_CONFIG);
  639. }
  640. outb(PORT + IEE16_ECTRL, 0);
  641. delay(100);
  642. if (!check_ie_present(sc, sc->sc_maddr, sc->sc_msize))
  643. return 0;
  644. ia->ia_iosize = 16; /* the number of I/O ports */
  645. return 1; /* found */
  646. }
  647. /*
  648. * Taken almost exactly from Bill's if_is.c, then modified beyond recognition.
  649. */
  650. void
  651. ieattach(parent, self, aux)
  652. struct device *parent, *self;
  653. void *aux;
  654. {
  655. struct ie_softc *sc = (void *)self;
  656. struct isa_attach_args *ia = aux;
  657. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  658. bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
  659. ifp->if_softc = sc;
  660. ifp->if_start = iestart;
  661. ifp->if_ioctl = ieioctl;
  662. ifp->if_watchdog = iewatchdog;
  663. ifp->if_flags =
  664. IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
  665. IFQ_SET_READY(&ifp->if_snd);
  666. /* Attach the interface. */
  667. if_attach(ifp);
  668. ether_ifattach(ifp);
  669. printf(": address %s, type %s R%d\n",
  670. ether_sprintf(sc->sc_arpcom.ac_enaddr),
  671. ie_hardware_names[sc->hard_type], sc->hard_vers + 1);
  672. sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
  673. IPL_NET, ieintr, sc, sc->sc_dev.dv_xname);
  674. }
  675. /*
  676. * Device timeout/watchdog routine. Entered if the device neglects to generate
  677. * an interrupt after a transmit has been started on it.
  678. */
  679. void
  680. iewatchdog(ifp)
  681. struct ifnet *ifp;
  682. {
  683. struct ie_softc *sc = ifp->if_softc;
  684. log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
  685. ++sc->sc_arpcom.ac_if.if_oerrors;
  686. iereset(sc);
  687. }
  688. /*
  689. * What to do upon receipt of an interrupt.
  690. */
  691. int
  692. ieintr(arg)
  693. void *arg;
  694. {
  695. struct ie_softc *sc = arg;
  696. register u_short status;
  697. /* Clear the interrupt latch on the 3C507. */
  698. if (sc->hard_type == IE_3C507)
  699. outb(PORT + IE507_ICTRL, 1);
  700. /* disable interrupts on the EE16. */
  701. if (sc->hard_type == IE_EE16)
  702. outb(PORT + IEE16_IRQ, sc->irq_encoded);
  703. status = sc->scb->ie_status & IE_ST_WHENCE;
  704. if (status == 0)
  705. return 0;
  706. loop:
  707. /* Ack interrupts FIRST in case we receive more during the ISR. */
  708. ie_ack(sc, status);
  709. if (status & (IE_ST_FR | IE_ST_RNR)) {
  710. #ifdef IEDEBUG
  711. in_ierint++;
  712. if (sc->sc_debug & IED_RINT)
  713. printf("%s: rint\n", sc->sc_dev.dv_xname);
  714. #endif
  715. ierint(sc);
  716. #ifdef IEDEBUG
  717. in_ierint--;
  718. #endif
  719. }
  720. if (status & IE_ST_CX) {
  721. #ifdef IEDEBUG
  722. in_ietint++;
  723. if (sc->sc_debug & IED_TINT)
  724. printf("%s: tint\n", sc->sc_dev.dv_xname);
  725. #endif
  726. ietint(sc);
  727. #ifdef IEDEBUG
  728. in_ietint--;
  729. #endif
  730. }
  731. if (status & IE_ST_RNR) {
  732. printf("%s: receiver not ready\n", sc->sc_dev.dv_xname);
  733. sc->sc_arpcom.ac_if.if_ierrors++;
  734. iereset(sc);
  735. }
  736. #ifdef IEDEBUG
  737. if ((status & IE_ST_CNA) && (sc->sc_debug & IED_CNA))
  738. printf("%s: cna\n", sc->sc_dev.dv_xname);
  739. #endif
  740. /* Clear the interrupt latch on the 3C507. */
  741. if (sc->hard_type == IE_3C507)
  742. outb(PORT + IE507_ICTRL, 1);
  743. status = sc->scb->ie_status & IE_ST_WHENCE;
  744. if (status == 0) {
  745. /* enable interrupts on the EE16. */
  746. if (sc->hard_type == IE_EE16)
  747. outb(PORT + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
  748. return 1;
  749. }
  750. goto loop;
  751. }
  752. /*
  753. * Process a received-frame interrupt.
  754. */
  755. void
  756. ierint(sc)
  757. struct ie_softc *sc;
  758. {
  759. volatile struct ie_sys_ctl_block *scb = sc->scb;
  760. int i, status;
  761. static int timesthru = 1024;
  762. i = sc->rfhead;
  763. for (;;) {
  764. status = sc->rframes[i]->ie_fd_status;
  765. if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
  766. if (!--timesthru) {
  767. sc->sc_arpcom.ac_if.if_ierrors +=
  768. scb->ie_err_crc + scb->ie_err_align +
  769. scb->ie_err_resource + scb->ie_err_overrun;
  770. scb->ie_err_crc = scb->ie_err_align =
  771. scb->ie_err_resource = scb->ie_err_overrun =
  772. 0;
  773. timesthru = 1024;
  774. }
  775. ie_readframe(sc, i);
  776. } else {
  777. if ((status & IE_FD_RNR) != 0 &&
  778. (scb->ie_status & IE_RU_READY) == 0) {
  779. sc->rframes[0]->ie_fd_buf_desc =
  780. MK_16(MEM, sc->rbuffs[0]);
  781. scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
  782. command_and_wait(sc, IE_RU_START, 0, 0);
  783. }
  784. break;
  785. }
  786. i = (i + 1) % NFRAMES;
  787. }
  788. }
  789. /*
  790. * Process a command-complete interrupt. These are only generated by the
  791. * transmission of frames. This routine is deceptively simple, since most of
  792. * the real work is done by iestart().
  793. */
  794. void
  795. ietint(sc)
  796. struct ie_softc *sc;
  797. {
  798. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  799. int status;
  800. ifp->if_timer = 0;
  801. ifp->if_flags &= ~IFF_OACTIVE;
  802. status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
  803. if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
  804. printf("ietint: command still busy!\n");
  805. if (status & IE_STAT_OK) {
  806. ifp->if_opackets++;
  807. ifp->if_collisions += status & IE_XS_MAXCOLL;
  808. } else {
  809. ifp->if_oerrors++;
  810. /*
  811. * XXX
  812. * Check SQE and DEFERRED?
  813. * What if more than one bit is set?
  814. */
  815. if (status & IE_STAT_ABORT)
  816. printf("%s: send aborted\n", sc->sc_dev.dv_xname);
  817. else if (status & IE_XS_LATECOLL)
  818. printf("%s: late collision\n", sc->sc_dev.dv_xname);
  819. else if (status & IE_XS_NOCARRIER)
  820. printf("%s: no carrier\n", sc->sc_dev.dv_xname);
  821. else if (status & IE_XS_LOSTCTS)
  822. printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
  823. else if (status & IE_XS_UNDERRUN)
  824. printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
  825. else if (status & IE_XS_EXCMAX) {
  826. printf("%s: too many collisions\n", sc->sc_dev.dv_xname);
  827. ifp->if_collisions += 16;
  828. }
  829. }
  830. /*
  831. * If multicast addresses were added or deleted while transmitting,
  832. * mc_reset() set the want_mcsetup flag indicating that we should do
  833. * it.
  834. */
  835. if (sc->want_mcsetup) {
  836. mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
  837. sc->want_mcsetup = 0;
  838. }
  839. /* Done with the buffer. */
  840. sc->xmit_busy--;
  841. sc->xctail = (sc->xctail + 1) % NTXBUF;
  842. /* Start the next packet, if any, transmitting. */
  843. if (sc->xmit_busy > 0)
  844. iexmit(sc);
  845. iestart(ifp);
  846. }
  847. /*
  848. * Compare two Ether/802 addresses for equality, inlined and unrolled for
  849. * speed. I'd love to have an inline assembler version of this...
  850. */
  851. static __inline int
  852. ether_equal(one, two)
  853. u_char *one, *two;
  854. {
  855. if (one[0] != two[0] || one[1] != two[1] || one[2] != two[2] ||
  856. one[3] != two[3] || one[4] != two[4] || one[5] != two[5])
  857. return 0;
  858. return 1;
  859. }
  860. /*
  861. * Check for a valid address.
  862. * Return value is true if the packet is for us, and false otherwise.
  863. */
  864. static __inline int
  865. check_eh(sc, eh)
  866. struct ie_softc *sc;
  867. struct ether_header *eh;
  868. {
  869. int i;
  870. switch (sc->promisc) {
  871. case IFF_ALLMULTI:
  872. /*
  873. * Receiving all multicasts, but no unicasts except those
  874. * destined for us.
  875. */
  876. if (eh->ether_dhost[0] & 1)
  877. return 1;
  878. if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
  879. return 1;
  880. return 0;
  881. case IFF_PROMISC:
  882. /* If for us, accept and hand up to BPF */
  883. if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
  884. return 1;
  885. /*
  886. * Not a multicast, so BPF wants to see it but we don't.
  887. */
  888. if (!(eh->ether_dhost[0] & 1))
  889. return 1;
  890. /*
  891. * If it's one of our multicast groups, accept it and pass it
  892. * up.
  893. */
  894. for (i = 0; i < sc->mcast_count; i++) {
  895. if (ether_equal(eh->ether_dhost, (u_char *)&sc->mcast_addrs[i])) {
  896. return 1;
  897. }
  898. }
  899. return 1;
  900. case IFF_ALLMULTI | IFF_PROMISC:
  901. /* We want to see multicasts. */
  902. if (eh->ether_dhost[0] & 1)
  903. return 1;
  904. /* We want to see our own packets */
  905. if (ether_equal(eh->ether_dhost, sc->sc_arpcom.ac_enaddr))
  906. return 1;
  907. return 1;
  908. case 0:
  909. return 1;
  910. }
  911. #ifdef DIAGNOSTIC
  912. panic("check_eh: impossible");
  913. #endif
  914. return 0;
  915. }
  916. /*
  917. * We want to isolate the bits that have meaning... This assumes that
  918. * IE_RBUF_SIZE is an even power of two. If somehow the act_len exceeds
  919. * the size of the buffer, then we are screwed anyway.
  920. */
  921. static __inline int
  922. ie_buflen(sc, head)
  923. struct ie_softc *sc;
  924. int head;
  925. {
  926. return (sc->rbuffs[head]->ie_rbd_actual
  927. & (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1)));
  928. }
  929. static __inline int
  930. ie_packet_len(sc)
  931. struct ie_softc *sc;
  932. {
  933. int i;
  934. int head = sc->rbhead;
  935. int acc = 0;
  936. do {
  937. if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED))
  938. return -1;
  939. i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
  940. acc += ie_buflen(sc, head);
  941. head = (head + 1) % NRXBUF;
  942. } while (!i);
  943. return acc;
  944. }
  945. /*
  946. * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
  947. * command to the chip to be executed. On the way, if we have a BPF listener
  948. * also give him a copy.
  949. */
  950. void
  951. iexmit(sc)
  952. struct ie_softc *sc;
  953. {
  954. #ifdef IEDEBUG
  955. if (sc->sc_debug & IED_XMIT)
  956. printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname,
  957. sc->xctail);
  958. #endif
  959. #if NBPFILTER > 0
  960. /*
  961. * If BPF is listening on this interface, let it see the packet before
  962. * we push it on the wire.
  963. */
  964. if (sc->sc_arpcom.ac_if.if_bpf)
  965. bpf_tap(sc->sc_arpcom.ac_if.if_bpf,
  966. sc->xmit_cbuffs[sc->xctail],
  967. sc->xmit_buffs[sc->xctail]->ie_xmit_flags,
  968. BPF_DIRECTION_OUT);
  969. #endif
  970. sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
  971. sc->xmit_buffs[sc->xctail]->ie_xmit_next = 0xffff;
  972. sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
  973. MK_24(MEM, sc->xmit_cbuffs[sc->xctail]);
  974. sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = 0xffff;
  975. sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
  976. IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
  977. sc->xmit_cmds[sc->xctail]->ie_xmit_status = 0;
  978. sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
  979. MK_16(MEM, sc->xmit_buffs[sc->xctail]);
  980. sc->scb->ie_command_list = MK_16(MEM, sc->xmit_cmds[sc->xctail]);
  981. command_and_wait(sc, IE_CU_START, 0, 0);
  982. sc->sc_arpcom.ac_if.if_timer = 5;
  983. }
  984. /*
  985. * Read data off the interface, and turn it into an mbuf chain.
  986. *
  987. * This code is DRAMATICALLY different from the previous version; this version
  988. * tries to allocate the entire mbuf chain up front, given the length of the
  989. * data available. This enables us to allocate mbuf clusters in many
  990. * situations where before we would have had a long chain of partially-full
  991. * mbufs. This should help to speed up the operation considerably. (Provided
  992. * that it works, of course.)
  993. */
  994. struct mbuf *
  995. ieget(sc, ehp)
  996. struct ie_softc *sc;
  997. struct ether_header *ehp;
  998. {
  999. struct mbuf *top, **mp, *m;
  1000. int len, totlen, resid;
  1001. int thisrboff, thismboff;
  1002. int head;
  1003. resid = totlen = ie_packet_len(sc);
  1004. if (totlen <= 0)
  1005. return 0;
  1006. head = sc->rbhead;
  1007. /*
  1008. * Snarf the Ethernet header.
  1009. */
  1010. bcopy((caddr_t)sc->cbuffs[head], (caddr_t)ehp, sizeof *ehp);
  1011. /*
  1012. * As quickly as possible, check if this packet is for us.
  1013. * If not, don't waste a single cycle copying the rest of the
  1014. * packet in.
  1015. * This is only a consideration when FILTER is defined; i.e., when
  1016. * we are either running BPF or doing multicasting.
  1017. */
  1018. if (!check_eh(sc, ehp)) {
  1019. sc->sc_arpcom.ac_if.if_ierrors--; /* just this case, it's not an error */
  1020. return 0;
  1021. }
  1022. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1023. if (m == NULL)
  1024. return 0;
  1025. m->m_pkthdr.len = totlen;
  1026. len = MHLEN;
  1027. top = 0;
  1028. mp = &top;
  1029. /*
  1030. * This loop goes through and allocates mbufs for all the data we will
  1031. * be copying in. It does not actually do the copying yet.
  1032. */
  1033. while (totlen > 0) {
  1034. if (top) {
  1035. MGET(m, M_DONTWAIT, MT_DATA);
  1036. if (m == NULL) {
  1037. m_freem(top);
  1038. return 0;
  1039. }
  1040. len = MLEN;
  1041. }
  1042. if (totlen >= MINCLSIZE) {
  1043. MCLGET(m, M_DONTWAIT);
  1044. if (m->m_flags & M_EXT)
  1045. len = MCLBYTES;
  1046. }
  1047. m->m_len = len = min(totlen, len);
  1048. totlen -= len;
  1049. *mp = m;
  1050. mp = &m->m_next;
  1051. }
  1052. m = top;
  1053. thisrboff = 0;
  1054. thismboff = 0;
  1055. /*
  1056. * Now we take the mbuf chain (hopefully only one mbuf most of the
  1057. * time) and stuff the data into it. There are no possible failures at
  1058. * or after this point.
  1059. */
  1060. while (resid > 0) {
  1061. int thisrblen = ie_buflen(sc, head) - thisrboff,
  1062. thismblen = m->m_len - thismboff;
  1063. len = min(thisrblen, thismblen);
  1064. bcopy((caddr_t)(sc->cbuffs[head] + thisrboff),
  1065. mtod(m, caddr_t) + thismboff, (u_int)len);
  1066. resid -= len;
  1067. if (len == thismblen) {
  1068. m = m->m_next;
  1069. thismboff = 0;
  1070. } else
  1071. thismboff += len;
  1072. if (len == thisrblen) {
  1073. head = (head + 1) % NRXBUF;
  1074. thisrboff = 0;
  1075. } else
  1076. thisrboff += len;
  1077. }
  1078. /*
  1079. * Unless something changed strangely while we were doing the copy, we
  1080. * have now copied everything in from the shared memory.
  1081. * This means that we are done.
  1082. */
  1083. return top;
  1084. }
  1085. /*
  1086. * Read frame NUM from unit UNIT (pre-cached as IE).
  1087. *
  1088. * This routine reads the RFD at NUM, and copies in the buffers from the list
  1089. * of RBD, then rotates the RBD and RFD lists so that the receiver doesn't
  1090. * start complaining. Trailers are DROPPED---there's no point in wasting time
  1091. * on confusing code to deal with them. Hopefully, this machine will never ARP
  1092. * for trailers anyway.
  1093. */
  1094. void
  1095. ie_readframe(sc, num)
  1096. struct ie_softc *sc;
  1097. int num; /* frame number to read */
  1098. {
  1099. int status;
  1100. struct mbuf *m = NULL;
  1101. struct mbuf_list ml = MBUF_LIST_INITIALIZER();
  1102. struct ether_header eh;
  1103. status = sc->rframes[num]->ie_fd_status;
  1104. /* Advance the RFD list, since we're done with this descriptor. */
  1105. sc->rframes[num]->ie_fd_status = 0;
  1106. sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
  1107. sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
  1108. sc->rftail = (sc->rftail + 1) % NFRAMES;
  1109. sc->rfhead = (sc->rfhead + 1) % NFRAMES;
  1110. if (status & IE_FD_OK) {
  1111. m = ieget(sc, &eh);
  1112. ie_drop_packet_buffer(sc);
  1113. }
  1114. if (m == NULL) {
  1115. sc->sc_arpcom.ac_if.if_ierrors++;
  1116. return;
  1117. }
  1118. #ifdef IEDEBUG
  1119. if (sc->sc_debug & IED_READFRAME)
  1120. printf("%s: frame from ether %s type %x\n", sc->sc_dev.dv_xname,
  1121. ether_sprintf(eh.ether_shost), (u_int)eh.ether_type);
  1122. #endif
  1123. ml_enqueue(&ml, m);
  1124. if_input(&sc->sc_arpcom.ac_if, &ml);
  1125. }
  1126. void
  1127. ie_drop_packet_buffer(sc)
  1128. struct ie_softc *sc;
  1129. {
  1130. int i;
  1131. do {
  1132. /*
  1133. * This means we are somehow out of sync. So, we reset the
  1134. * adapter.
  1135. */
  1136. if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
  1137. #ifdef IEDEBUG
  1138. print_rbd(sc->rbuffs[sc->rbhead]);
  1139. #endif
  1140. log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
  1141. sc->sc_dev.dv_xname, sc->rbhead);
  1142. iereset(sc);
  1143. return;
  1144. }
  1145. i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
  1146. sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
  1147. sc->rbuffs[sc->rbhead]->ie_rbd_actual = 0;
  1148. sc->rbhead = (sc->rbhead + 1) % NRXBUF;
  1149. sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
  1150. sc->rbtail = (sc->rbtail + 1) % NRXBUF;
  1151. } while (!i);
  1152. }
  1153. /*
  1154. * Start transmission on an interface.
  1155. */
  1156. void
  1157. iestart(ifp)
  1158. struct ifnet *ifp;
  1159. {
  1160. struct ie_softc *sc = ifp->if_softc;
  1161. struct mbuf *m0, *m;
  1162. u_char *buffer;
  1163. u_short len;
  1164. if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
  1165. return;
  1166. for (;;) {
  1167. if (sc->xmit_busy == NTXBUF) {
  1168. ifp->if_flags |= IFF_OACTIVE;
  1169. break;
  1170. }
  1171. IFQ_DEQUEUE(&ifp->if_snd, m0);
  1172. if (m0 == NULL)
  1173. break;
  1174. /* We need to use m->m_pkthdr.len, so require the header */
  1175. if ((m0->m_flags & M_PKTHDR) == 0)
  1176. panic("iestart: no header mbuf");
  1177. #if NBPFILTER > 0
  1178. /* Tap off here if there is a BPF listener. */
  1179. if (ifp->if_bpf)
  1180. bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
  1181. #endif
  1182. #ifdef IEDEBUG
  1183. if (sc->sc_debug & IED_ENQ)
  1184. printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
  1185. sc->xchead);
  1186. #endif
  1187. len = 0;
  1188. buffer = sc->xmit_cbuffs[sc->xchead];
  1189. for (m = m0; m != NULL && (len + m->m_len) < IE_TBUF_SIZE;
  1190. m = m->m_next) {
  1191. bcopy(mtod(m, caddr_t), buffer, m->m_len);
  1192. buffer += m->m_len;
  1193. len += m->m_len;
  1194. }
  1195. if (m != NULL)
  1196. printf("%s: tbuf overflow\n", sc->sc_dev.dv_xname);
  1197. m_freem(m0);
  1198. if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
  1199. bzero(buffer, ETHER_MIN_LEN - ETHER_CRC_LEN - len);
  1200. len = ETHER_MIN_LEN - ETHER_CRC_LEN;
  1201. buffer += ETHER_MIN_LEN - ETHER_CRC_LEN;
  1202. }
  1203. sc->xmit_buffs[sc->xchead]->ie_xmit_flags = len;
  1204. /* Start the first packet transmitting. */
  1205. if (sc->xmit_busy == 0)
  1206. iexmit(sc);
  1207. sc->xchead = (sc->xchead + 1) % NTXBUF;
  1208. sc->xmit_busy++;
  1209. }
  1210. }
  1211. /*
  1212. * Check to see if there's an 82586 out there.
  1213. */
  1214. int
  1215. check_ie_present(sc, where, size)
  1216. struct ie_softc *sc;
  1217. caddr_t where;
  1218. u_int size;
  1219. {
  1220. volatile struct ie_sys_conf_ptr *scp;
  1221. volatile struct ie_int_sys_conf_ptr *iscp;
  1222. volatile struct ie_sys_ctl_block *scb;
  1223. u_long realbase;
  1224. int s;
  1225. s = splnet();
  1226. realbase = (u_long)where + size - (1 << 24);
  1227. scp = (volatile struct ie_sys_conf_ptr *)(realbase + IE_SCP_ADDR);
  1228. bzero((char *)scp, sizeof *scp);
  1229. /*
  1230. * First we put the ISCP at the bottom of memory; this tests to make
  1231. * sure that our idea of the size of memory is the same as the
  1232. * controller's. This is NOT where the ISCP will be in normal
  1233. * operation.
  1234. */
  1235. iscp = (volatile struct ie_int_sys_conf_ptr *)where;
  1236. bzero((char *)iscp, sizeof *iscp);
  1237. scb = (volatile struct ie_sys_ctl_block *)where;
  1238. bzero((char *)scb, sizeof *scb);
  1239. scp->ie_bus_use = 0; /* 16-bit */
  1240. scp->ie_iscp_ptr = (caddr_t)((volatile caddr_t)iscp -
  1241. (volatile caddr_t)realbase);
  1242. iscp->ie_busy = 1;
  1243. iscp->ie_scb_offset = MK_16(realbase, scb) + 256;
  1244. (sc->reset_586)(sc);
  1245. (sc->chan_attn)(sc);
  1246. delay(100); /* wait a while... */
  1247. if (iscp->ie_busy) {
  1248. splx(s);
  1249. return 0;
  1250. }
  1251. /*
  1252. * Now relocate the ISCP to its real home, and reset the controller
  1253. * again.
  1254. */
  1255. iscp = (void *)ALIGN(realbase + IE_SCP_ADDR - sizeof(*iscp));
  1256. bzero((char *)iscp, sizeof *iscp);
  1257. scp->ie_iscp_ptr = (caddr_t)((caddr_t)iscp - (caddr_t)realbase);
  1258. iscp->ie_busy = 1;
  1259. iscp->ie_scb_offset = MK_16(realbase, scb);
  1260. (sc->reset_586)(sc);
  1261. (sc->chan_attn)(sc);
  1262. delay(100);
  1263. if (iscp->ie_busy) {
  1264. splx(s);
  1265. return 0;
  1266. }
  1267. sc->sc_msize = size;
  1268. sc->sc_maddr = (caddr_t)realbase;
  1269. sc->iscp = iscp;
  1270. sc->scb = scb;
  1271. /*
  1272. * Acknowledge any interrupts we may have caused...
  1273. */
  1274. ie_ack(sc, IE_ST_WHENCE);
  1275. splx(s);
  1276. return 1;
  1277. }
  1278. /*
  1279. * Divine the memory size of ie board UNIT.
  1280. * Better hope there's nothing important hiding just below the ie card...
  1281. */
  1282. void
  1283. ie_find_mem_size(sc)
  1284. struct ie_softc *sc;
  1285. {
  1286. u_int size;
  1287. sc->sc_msize = 0;
  1288. for (size = 65536; size >= 16384; size -= 16384)
  1289. if (check_ie_present(sc, sc->sc_maddr, size))
  1290. return;
  1291. return;
  1292. }
  1293. void
  1294. el_reset_586(sc)
  1295. struct ie_softc *sc;
  1296. {
  1297. outb(PORT + IE507_CTRL, EL_CTRL_RESET);
  1298. delay(100);
  1299. outb(PORT + IE507_CTRL, EL_CTRL_NORMAL);
  1300. delay(100);
  1301. }
  1302. void
  1303. sl_reset_586(sc)
  1304. struct ie_softc *sc;
  1305. {
  1306. outb(PORT + IEATT_RESET, 0);
  1307. }
  1308. void
  1309. ee16_reset_586(sc)
  1310. struct ie_softc *sc;
  1311. {
  1312. outb(PORT + IEE16_ECTRL, IEE16_RESET_586);
  1313. delay(100);
  1314. outb(PORT + IEE16_ECTRL, 0);
  1315. delay(100);
  1316. }
  1317. void
  1318. el_chan_attn(sc)
  1319. struct ie_softc *sc;
  1320. {
  1321. outb(PORT + IE507_ATTN, 1);
  1322. }
  1323. void
  1324. sl_chan_attn(sc)
  1325. struct ie_softc *sc;
  1326. {
  1327. outb(PORT + IEATT_ATTN, 0);
  1328. }
  1329. void
  1330. ee16_chan_attn(sc)
  1331. struct ie_softc *sc;
  1332. {
  1333. outb(PORT + IEE16_ATTN, 0);
  1334. }
  1335. u_short
  1336. ee16_read_eeprom(sc, location)
  1337. struct ie_softc *sc;
  1338. int location;
  1339. {
  1340. int ectrl, edata;
  1341. ectrl = inb(PORT + IEE16_ECTRL);
  1342. ectrl &= IEE16_ECTRL_MASK;
  1343. ectrl |= IEE16_ECTRL_EECS;
  1344. outb(PORT + IEE16_ECTRL, ectrl);
  1345. ee16_eeprom_outbits(sc, IEE16_EEPROM_READ, IEE16_EEPROM_OPSIZE1);
  1346. ee16_eeprom_outbits(sc, location, IEE16_EEPROM_ADDR_SIZE);
  1347. edata = ee16_eeprom_inbits(sc);
  1348. ectrl = inb(PORT + IEE16_ECTRL);
  1349. ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EEDI | IEE16_ECTRL_EECS);
  1350. outb(PORT + IEE16_ECTRL, ectrl);
  1351. ee16_eeprom_clock(sc, 1);
  1352. ee16_eeprom_clock(sc, 0);
  1353. return edata;
  1354. }
  1355. void
  1356. ee16_eeprom_outbits(sc, edata, count)
  1357. struct ie_softc *sc;
  1358. int edata, count;
  1359. {
  1360. int ectrl, i;
  1361. ectrl = inb(PORT + IEE16_ECTRL);
  1362. ectrl &= ~IEE16_RESET_ASIC;
  1363. for (i = count - 1; i >= 0; i--) {
  1364. ectrl &= ~IEE16_ECTRL_EEDI;
  1365. if (edata & (1 << i)) {
  1366. ectrl |= IEE16_ECTRL_EEDI;
  1367. }
  1368. outb(PORT + IEE16_ECTRL, ectrl);
  1369. delay(1); /* eeprom data must be setup for 0.4 uSec */
  1370. ee16_eeprom_clock(sc, 1);
  1371. ee16_eeprom_clock(sc, 0);
  1372. }
  1373. ectrl &= ~IEE16_ECTRL_EEDI;
  1374. outb(PORT + IEE16_ECTRL, ectrl);
  1375. delay(1); /* eeprom data must be held for 0.4 uSec */
  1376. }
  1377. int
  1378. ee16_eeprom_inbits(sc)
  1379. struct ie_softc *sc;
  1380. {
  1381. int ectrl, edata, i;
  1382. ectrl = inb(PORT + IEE16_ECTRL);
  1383. ectrl &= ~IEE16_RESET_ASIC;
  1384. for (edata = 0, i = 0; i < 16; i++) {
  1385. edata = edata << 1;
  1386. ee16_eeprom_clock(sc, 1);
  1387. ectrl = inb(PORT + IEE16_ECTRL);
  1388. if (ectrl & IEE16_ECTRL_EEDO) {
  1389. edata |= 1;
  1390. }
  1391. ee16_eeprom_clock(sc, 0);
  1392. }
  1393. return (edata);
  1394. }
  1395. void
  1396. ee16_eeprom_clock(sc, state)
  1397. struct ie_softc *sc;
  1398. int state;
  1399. {
  1400. int ectrl;
  1401. ectrl = inb(PORT + IEE16_ECTRL);
  1402. ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EESK);
  1403. if (state) {
  1404. ectrl |= IEE16_ECTRL_EESK;
  1405. }
  1406. outb(PORT + IEE16_ECTRL, ectrl);
  1407. delay(9); /* EESK must be stable for 8.38 uSec */
  1408. }
  1409. static inline void
  1410. ee16_interrupt_enable(sc)
  1411. struct ie_softc *sc;
  1412. {
  1413. delay(100);
  1414. outb(PORT + IEE16_IRQ, sc->irq_encoded | IEE16_IRQ_ENABLE);
  1415. delay(100);
  1416. }
  1417. void
  1418. slel_get_address(sc)
  1419. struct ie_softc *sc;
  1420. {
  1421. u_char *addr = sc->sc_arpcom.ac_enaddr;
  1422. int i;
  1423. for (i = 0; i < ETHER_ADDR_LEN; i++)
  1424. addr[i] = inb(PORT + i);
  1425. }
  1426. void
  1427. iereset(sc)
  1428. struct ie_softc *sc;
  1429. {
  1430. int s = splnet();
  1431. iestop(sc);
  1432. /*
  1433. * Stop i82586 dead in its tracks.
  1434. */
  1435. if (command_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
  1436. printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
  1437. if (command_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
  1438. printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
  1439. ieinit(sc);
  1440. splx(s);
  1441. }
  1442. /*
  1443. * Send a command to the controller and wait for it to either complete or be
  1444. * accepted, depending on the command. If the command pointer is null, then
  1445. * pretend that the command is not an action command. If the command pointer
  1446. * is not null, and the command is an action command, wait for
  1447. * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
  1448. * to become true.
  1449. */
  1450. static int
  1451. command_and_wait(sc, cmd, pcmd, mask)
  1452. struct ie_softc *sc;
  1453. int cmd;
  1454. volatile void *pcmd;
  1455. int mask;
  1456. {
  1457. volatile struct ie_cmd_common *cc = pcmd;
  1458. volatile struct ie_sys_ctl_block *scb = sc->scb;
  1459. int i;
  1460. scb->ie_command = (u_short)cmd;
  1461. if (IE_ACTION_COMMAND(cmd) && pcmd) {
  1462. (sc->chan_attn)(sc);
  1463. /*
  1464. * According to the packet driver, the minimum timeout should
  1465. * be .369 seconds, which we round up to .4.
  1466. *
  1467. * Now spin-lock waiting for status. This is not a very nice
  1468. * thing to do, but I haven't figured out how, or indeed if, we
  1469. * can put the process waiting for action to sleep. (We may
  1470. * be getting called through some other timeout running in the
  1471. * kernel.)
  1472. */
  1473. for (i = 36900; i--; DELAY(10))
  1474. if ((cc->ie_cmd_status & mask))
  1475. break;
  1476. return i < 0;
  1477. } else {
  1478. /*
  1479. * Otherwise, just wait for the command to be accepted.
  1480. */
  1481. (sc->chan_attn)(sc);
  1482. while (scb->ie_command)
  1483. ; /* spin lock */
  1484. return 0;
  1485. }
  1486. }
  1487. /*
  1488. * Run the time-domain reflectometer.
  1489. */
  1490. static void
  1491. run_tdr(sc, cmd)
  1492. struct ie_softc *sc;
  1493. struct ie_tdr_cmd *cmd;
  1494. {
  1495. int result;
  1496. cmd->com.ie_cmd_status = 0;
  1497. cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
  1498. cmd->com.ie_cmd_link = 0xffff;
  1499. sc->scb->ie_command_list = MK_16(MEM, cmd);
  1500. cmd->ie_tdr_time = 0;
  1501. if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
  1502. !(cmd->com.ie_cmd_status & IE_STAT_OK))
  1503. result = 0x10000;
  1504. else
  1505. result = cmd->ie_tdr_time;
  1506. ie_ack(sc, IE_ST_WHENCE);
  1507. if (result & IE_TDR_SUCCESS)
  1508. return;
  1509. if (result & 0x10000)
  1510. printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
  1511. else if (result & IE_TDR_XCVR)
  1512. printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
  1513. else if (result & IE_TDR_OPEN)
  1514. printf("%s: TDR detected an open %d clocks away\n",
  1515. sc->sc_dev.dv_xname, result & IE_TDR_TIME);
  1516. else if (result & IE_TDR_SHORT)
  1517. printf("%s: TDR detected a short %d clocks away\n",
  1518. sc->sc_dev.dv_xname, result & IE_TDR_TIME);
  1519. else
  1520. printf("%s: TDR returned unknown status %x\n",
  1521. sc->sc_dev.dv_xname, result);
  1522. }
  1523. #define _ALLOC(p, n) (bzero(p, n), p += n, p - n)
  1524. #define ALLOC(p, n) _ALLOC(p, ALIGN(n))
  1525. /*
  1526. * Here is a helper routine for ieinit(). This sets up the buffers.
  1527. */
  1528. void
  1529. iememinit(ptr, sc)
  1530. void *ptr;
  1531. struct ie_softc *sc;
  1532. {
  1533. int i;
  1534. /* First lay them out. */
  1535. for (i = 0; i < NFRAMES; i++)
  1536. sc->rframes[i] = ALLOC(ptr, sizeof(*sc->rframes[i]));
  1537. /* Now link them together. */
  1538. for (i = 0; i < NFRAMES; i++)
  1539. sc->rframes[i]->ie_fd_next =
  1540. MK_16(MEM, sc->rframes[(i + 1) % NFRAMES]);
  1541. /* Finally, set the EOL bit on the last one. */
  1542. sc->rframes[NFRAMES - 1]->ie_fd_last |= IE_FD_LAST;
  1543. /*
  1544. * Now lay out some buffers for the incoming frames. Note that we set
  1545. * aside a bit of slop in each buffer, to make sure that we have enough
  1546. * space to hold a single frame in every buffer.
  1547. */
  1548. for (i = 0; i < NRXBUF; i++) {
  1549. sc->rbuffs[i] = ALLOC(ptr, sizeof(*sc->rbuffs[i]));
  1550. sc->rbuffs[i]->ie_rbd_length = IE_RBUF_SIZE;
  1551. sc->rbuffs[i]->ie_rbd_buffer = MK_24(MEM, ptr);
  1552. sc->cbuffs[i] = ALLOC(ptr, IE_RBUF_SIZE);
  1553. }
  1554. /* Now link them together. */
  1555. for (i = 0; i < NRXBUF; i++)
  1556. sc->rbuffs[i]->ie_rbd_next =
  1557. MK_16(MEM, sc->rbuffs[(i + 1) % NRXBUF]);
  1558. /* Tag EOF on the last one. */
  1559. sc->rbuffs[NRXBUF - 1]->ie_rbd_length |= IE_RBD_LAST;
  1560. /*
  1561. * We use the head and tail pointers on receive to keep track of the
  1562. * order in which RFDs and RBDs are used.
  1563. */
  1564. sc->rfhead = 0;
  1565. sc->rftail = NFRAMES - 1;
  1566. sc->rbhead = 0;
  1567. sc->rbtail = NRXBUF - 1;
  1568. sc->scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
  1569. sc->rframes[0]->ie_fd_buf_desc = MK_16(MEM, sc->rbuffs[0]);
  1570. /*
  1571. * Finally, the transmit command and buffer are the last little bit of
  1572. * work.
  1573. */
  1574. for (i = 0; i < NTXBUF; i++) {
  1575. sc->xmit_cmds[i] = ALLOC(ptr, sizeof(*sc->xmit_cmds[i]));
  1576. sc->xmit_buffs[i] = ALLOC(ptr, sizeof(*sc->xmit_buffs[i]));
  1577. }
  1578. for (i = 0; i < NTXBUF; i++)
  1579. sc->xmit_cbuffs[i] = ALLOC(ptr, IE_TBUF_SIZE);
  1580. /* Pointers to last packet sent and next available transmit buffer. */
  1581. sc->xchead = sc->xctail = 0;
  1582. /* Clear transmit-busy flag and set number of free transmit buffers. */
  1583. sc->xmit_busy = 0;
  1584. }
  1585. /*
  1586. * Run the multicast setup command.
  1587. * Called at splnet().
  1588. */
  1589. static int
  1590. mc_setup(sc, ptr)
  1591. struct ie_softc *sc;
  1592. void *ptr;
  1593. {
  1594. volatile struct ie_mcast_cmd *cmd = ptr;
  1595. cmd->com.ie_cmd_status = 0;
  1596. cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
  1597. cmd->com.ie_cmd_link = 0xffff;
  1598. bcopy((caddr_t)sc->mcast_addrs, (caddr_t)cmd->ie_mcast_addrs,
  1599. sc->mcast_count * sizeof *sc->mcast_addrs);
  1600. cmd->ie_mcast_bytes = sc->mcast_count * ETHER_ADDR_LEN; /* grrr... */
  1601. sc->scb->ie_command_list = MK_16(MEM, cmd);
  1602. if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
  1603. !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
  1604. printf("%s: multicast address setup command failed\n",
  1605. sc->sc_dev.dv_xname);
  1606. return 0;
  1607. }
  1608. return 1;
  1609. }
  1610. /*
  1611. * This routine takes the environment generated by check_ie_present() and adds
  1612. * to it all the other structures we need to operate the adapter. This
  1613. * includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands, starting
  1614. * the receiver unit, and clearing interrupts.
  1615. *
  1616. * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
  1617. */
  1618. int
  1619. ieinit(sc)
  1620. struct ie_softc *sc;
  1621. {
  1622. volatile struct ie_sys_ctl_block *scb = sc->scb;
  1623. void *ptr;
  1624. ptr = (void *)ALIGN(scb + 1);
  1625. /*
  1626. * Send the configure command first.
  1627. */
  1628. {
  1629. volatile struct ie_config_cmd *cmd = ptr;
  1630. scb->ie_command_list = MK_16(MEM, cmd);
  1631. cmd->com.ie_cmd_status = 0;
  1632. cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
  1633. cmd->com.ie_cmd_link = 0xffff;
  1634. ie_setup_config(cmd, sc->promisc != 0,
  1635. sc->hard_type == IE_STARLAN10);
  1636. if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
  1637. !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
  1638. printf("%s: configure command failed\n",
  1639. sc->sc_dev.dv_xname);
  1640. return 0;
  1641. }
  1642. }
  1643. /*
  1644. * Now send the Individual Address Setup command.
  1645. */
  1646. {
  1647. volatile struct ie_iasetup_cmd *cmd = ptr;
  1648. scb->ie_command_list = MK_16(MEM, cmd);
  1649. cmd->com.ie_cmd_status = 0;
  1650. cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
  1651. cmd->com.ie_cmd_link = 0xffff;
  1652. bcopy(sc->sc_arpcom.ac_enaddr, (caddr_t)&cmd->ie_address,
  1653. sizeof cmd->ie_address);
  1654. if (command_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
  1655. !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
  1656. printf("%s: individual address setup command failed\n",
  1657. sc->sc_dev.dv_xname);
  1658. return 0;
  1659. }
  1660. }
  1661. /*
  1662. * Now run the time-domain reflectometer.
  1663. */
  1664. run_tdr(sc, ptr);
  1665. /*
  1666. * Acknowledge any interrupts we have generated thus far.
  1667. */
  1668. ie_ack(sc, IE_ST_WHENCE);
  1669. /*
  1670. * Set up the RFA.
  1671. */
  1672. iememinit(ptr, sc);
  1673. sc->sc_arpcom.ac_if.if_flags |= IFF_RUNNING;
  1674. sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
  1675. sc->scb->ie_recv_list = MK_16(MEM, sc->rframes[0]);
  1676. command_and_wait(sc, IE_RU_START, 0, 0);
  1677. ie_ack(sc, IE_ST_WHENCE);
  1678. /* take the ee16 out of loopback */
  1679. {
  1680. u_char bart_config;
  1681. if(sc->hard_type == IE_EE16) {
  1682. bart_config = inb(PORT + IEE16_CONFIG);
  1683. bart_config &= ~IEE16_BART_LOOPBACK;
  1684. bart_config |= IEE16_BART_MCS16_TEST; /* inb doesn't get bit! */
  1685. outb(PORT + IEE16_CONFIG, bart_config);
  1686. ee16_interrupt_enable(sc);
  1687. ee16_chan_attn(sc);
  1688. }
  1689. }
  1690. return 0;
  1691. }
  1692. void
  1693. iestop(sc)
  1694. struct ie_softc *sc;
  1695. {
  1696. command_and_wait(sc, IE_RU_DISABLE, 0, 0);
  1697. }
  1698. int
  1699. ieioctl(ifp, cmd, data)
  1700. register struct ifnet *ifp;
  1701. u_long cmd;
  1702. caddr_t data;
  1703. {
  1704. struct ie_softc *sc = ifp->if_softc;
  1705. struct ifaddr *ifa = (struct ifaddr *)data;
  1706. int s, error = 0;
  1707. s = splnet();
  1708. switch (cmd) {
  1709. case SIOCSIFADDR:
  1710. ifp->if_flags |= IFF_UP;
  1711. switch (ifa->ifa_addr->sa_family) {
  1712. case AF_INET:
  1713. ieinit(sc);
  1714. arp_ifinit(&sc->sc_arpcom, ifa);
  1715. break;
  1716. default:
  1717. ieinit(sc);
  1718. break;
  1719. }
  1720. break;
  1721. case SIOCSIFFLAGS:
  1722. sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
  1723. if ((ifp->if_flags & IFF_UP) == 0 &&
  1724. (ifp->if_flags & IFF_RUNNING) != 0) {
  1725. /*
  1726. * If interface is marked down and it is running, then
  1727. * stop it.
  1728. */
  1729. iestop(sc);
  1730. ifp->if_flags &= ~IFF_RUNNING;
  1731. } else if ((ifp->if_flags & IFF_UP) != 0 &&
  1732. (ifp->if_flags & IFF_RUNNING) == 0) {
  1733. /*
  1734. * If interface is marked up and it is stopped, then
  1735. * start it.
  1736. */
  1737. ieinit(sc);
  1738. } else {
  1739. /*
  1740. * Reset the interface to pick up changes in any other
  1741. * flags that affect hardware registers.
  1742. */
  1743. iestop(sc);
  1744. ieinit(sc);
  1745. }
  1746. #ifdef IEDEBUG
  1747. if (ifp->if_flags & IFF_DEBUG)
  1748. sc->sc_debug = IED_ALL;
  1749. else
  1750. sc->sc_debug = 0;
  1751. #endif
  1752. break;
  1753. default:
  1754. error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data);
  1755. }
  1756. if (error == ENETRESET) {
  1757. if (ifp->if_flags & IFF_RUNNING)
  1758. mc_reset(sc);
  1759. error = 0;
  1760. }
  1761. splx(s);
  1762. return error;
  1763. }
  1764. static void
  1765. mc_reset(sc)
  1766. struct ie_softc *sc;
  1767. {
  1768. struct arpcom *ac = &sc->sc_arpcom;
  1769. struct ether_multi *enm;
  1770. struct ether_multistep step;
  1771. if (ac->ac_multirangecnt > 0) {
  1772. ac->ac_if.if_flags |= IFF_ALLMULTI;
  1773. ieioctl(&ac->ac_if, SIOCSIFFLAGS, (void *)0);
  1774. goto setflag;
  1775. }
  1776. /*
  1777. * Step through the list of addresses.
  1778. */
  1779. sc->mcast_count = 0;
  1780. ETHER_FIRST_MULTI(step, ac, enm);
  1781. while (enm) {
  1782. if (sc->mcast_count >= MAXMCAST) {
  1783. ac->ac_if.if_flags |= IFF_ALLMULTI;
  1784. ieioctl(&ac->ac_if, SIOCSIFFLAGS, (void *)0);
  1785. goto setflag;
  1786. }
  1787. bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
  1788. sc->mcast_count++;
  1789. ETHER_NEXT_MULTI(step, enm);
  1790. }
  1791. setflag:
  1792. sc->want_mcsetup = 1;
  1793. }
  1794. #ifdef IEDEBUG
  1795. void
  1796. print_rbd(rbd)
  1797. volatile struct ie_recv_buf_desc *rbd;
  1798. {
  1799. printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
  1800. "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
  1801. rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
  1802. rbd->mbz);
  1803. }
  1804. #endif