3c505.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. /*
  2. * Linux Ethernet device driver for the 3Com Etherlink Plus (3C505)
  3. * By Craig Southeren, Juha Laiho and Philip Blundell
  4. *
  5. * 3c505.c This module implements an interface to the 3Com
  6. * Etherlink Plus (3c505) Ethernet card. Linux device
  7. * driver interface reverse engineered from the Linux 3C509
  8. * device drivers. Some 3C505 information gleaned from
  9. * the Crynwr packet driver. Still this driver would not
  10. * be here without 3C505 technical reference provided by
  11. * 3Com.
  12. *
  13. * $Id: 3c505.c,v 1.10 1996/04/16 13:06:27 phil Exp $
  14. *
  15. * Authors: Linux 3c505 device driver by
  16. * Craig Southeren, <craigs@ineluki.apana.org.au>
  17. * Final debugging by
  18. * Andrew Tridgell, <tridge@nimbus.anu.edu.au>
  19. * Auto irq/address, tuning, cleanup and v1.1.4+ kernel mods by
  20. * Juha Laiho, <jlaiho@ichaos.nullnet.fi>
  21. * Linux 3C509 driver by
  22. * Donald Becker, <becker@super.org>
  23. * (Now at <becker@scyld.com>)
  24. * Crynwr packet driver by
  25. * Krishnan Gopalan and Gregg Stefancik,
  26. * Clemson University Engineering Computer Operations.
  27. * Portions of the code have been adapted from the 3c505
  28. * driver for NCSA Telnet by Bruce Orchard and later
  29. * modified by Warren Van Houten and krus@diku.dk.
  30. * 3C505 technical information provided by
  31. * Terry Murphy, of 3Com Network Adapter Division
  32. * Linux 1.3.0 changes by
  33. * Alan Cox <Alan.Cox@linux.org>
  34. * More debugging, DMA support, currently maintained by
  35. * Philip Blundell <philb@gnu.org>
  36. * Multicard/soft configurable dma channel/rev 2 hardware support
  37. * by Christopher Collins <ccollins@pcug.org.au>
  38. * Ethtool support (jgarzik), 11/17/2001
  39. */
  40. #define DRV_NAME "3c505"
  41. #define DRV_VERSION "1.10a"
  42. /* Theory of operation:
  43. *
  44. * The 3c505 is quite an intelligent board. All communication with it is done
  45. * by means of Primary Command Blocks (PCBs); these are transferred using PIO
  46. * through the command register. The card has 256k of on-board RAM, which is
  47. * used to buffer received packets. It might seem at first that more buffers
  48. * are better, but in fact this isn't true. From my tests, it seems that
  49. * more than about 10 buffers are unnecessary, and there is a noticeable
  50. * performance hit in having more active on the card. So the majority of the
  51. * card's memory isn't, in fact, used. Sadly, the card only has one transmit
  52. * buffer and, short of loading our own firmware into it (which is what some
  53. * drivers resort to) there's nothing we can do about this.
  54. *
  55. * We keep up to 4 "receive packet" commands active on the board at a time.
  56. * When a packet comes in, so long as there is a receive command active, the
  57. * board will send us a "packet received" PCB and then add the data for that
  58. * packet to the DMA queue. If a DMA transfer is not already in progress, we
  59. * set one up to start uploading the data. We have to maintain a list of
  60. * backlogged receive packets, because the card may decide to tell us about
  61. * a newly-arrived packet at any time, and we may not be able to start a DMA
  62. * transfer immediately (ie one may already be going on). We can't NAK the
  63. * PCB, because then it would throw the packet away.
  64. *
  65. * Trying to send a PCB to the card at the wrong moment seems to have bad
  66. * effects. If we send it a transmit PCB while a receive DMA is happening,
  67. * it will just NAK the PCB and so we will have wasted our time. Worse, it
  68. * sometimes seems to interrupt the transfer. The majority of the low-level
  69. * code is protected by one huge semaphore -- "busy" -- which is set whenever
  70. * it probably isn't safe to do anything to the card. The receive routine
  71. * must gain a lock on "busy" before it can start a DMA transfer, and the
  72. * transmit routine must gain a lock before it sends the first PCB to the card.
  73. * The send_pcb() routine also has an internal semaphore to protect it against
  74. * being re-entered (which would be disastrous) -- this is needed because
  75. * several things can happen asynchronously (re-priming the receiver and
  76. * asking the card for statistics, for example). send_pcb() will also refuse
  77. * to talk to the card at all if a DMA upload is happening. The higher-level
  78. * networking code will reschedule a later retry if some part of the driver
  79. * is blocked. In practice, this doesn't seem to happen very often.
  80. */
  81. /* This driver may now work with revision 2.x hardware, since all the read
  82. * operations on the HCR have been removed (we now keep our own softcopy).
  83. * But I don't have an old card to test it on.
  84. *
  85. * This has had the bad effect that the autoprobe routine is now a bit
  86. * less friendly to other devices. However, it was never very good.
  87. * before, so I doubt it will hurt anybody.
  88. */
  89. /* The driver is a mess. I took Craig's and Juha's code, and hacked it firstly
  90. * to make it more reliable, and secondly to add DMA mode. Many things could
  91. * probably be done better; the concurrency protection is particularly awful.
  92. */
  93. #include <linux/module.h>
  94. #include <linux/kernel.h>
  95. #include <linux/string.h>
  96. #include <linux/interrupt.h>
  97. #include <linux/errno.h>
  98. #include <linux/in.h>
  99. #include <linux/slab.h>
  100. #include <linux/ioport.h>
  101. #include <linux/spinlock.h>
  102. #include <linux/ethtool.h>
  103. #include <linux/delay.h>
  104. #include <linux/bitops.h>
  105. #include <asm/uaccess.h>
  106. #include <asm/io.h>
  107. #include <asm/dma.h>
  108. #include <linux/netdevice.h>
  109. #include <linux/etherdevice.h>
  110. #include <linux/skbuff.h>
  111. #include <linux/init.h>
  112. #include "3c505.h"
  113. /*********************************************************
  114. *
  115. * define debug messages here as common strings to reduce space
  116. *
  117. *********************************************************/
  118. static const char filename[] = __FILE__;
  119. static const char timeout_msg[] = "*** timeout at %s:%s (line %d) ***\n";
  120. #define TIMEOUT_MSG(lineno) \
  121. printk(timeout_msg, filename,__FUNCTION__,(lineno))
  122. static const char invalid_pcb_msg[] =
  123. "*** invalid pcb length %d at %s:%s (line %d) ***\n";
  124. #define INVALID_PCB_MSG(len) \
  125. printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
  126. static char search_msg[] __initdata = KERN_INFO "%s: Looking for 3c505 adapter at address %#x...";
  127. static char stilllooking_msg[] __initdata = "still looking...";
  128. static char found_msg[] __initdata = "found.\n";
  129. static char notfound_msg[] __initdata = "not found (reason = %d)\n";
  130. static char couldnot_msg[] __initdata = KERN_INFO "%s: 3c505 not found\n";
  131. /*********************************************************
  132. *
  133. * various other debug stuff
  134. *
  135. *********************************************************/
  136. #ifdef ELP_DEBUG
  137. static int elp_debug = ELP_DEBUG;
  138. #else
  139. static int elp_debug;
  140. #endif
  141. #define debug elp_debug
  142. /*
  143. * 0 = no messages (well, some)
  144. * 1 = messages when high level commands performed
  145. * 2 = messages when low level commands performed
  146. * 3 = messages when interrupts received
  147. */
  148. /*****************************************************************
  149. *
  150. * List of I/O-addresses we try to auto-sense
  151. * Last element MUST BE 0!
  152. *****************************************************************/
  153. static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
  154. /* Dma Memory related stuff */
  155. static unsigned long dma_mem_alloc(int size)
  156. {
  157. int order = get_order(size);
  158. return __get_dma_pages(GFP_KERNEL, order);
  159. }
  160. /*****************************************************************
  161. *
  162. * Functions for I/O (note the inline !)
  163. *
  164. *****************************************************************/
  165. static inline unsigned char inb_status(unsigned int base_addr)
  166. {
  167. return inb(base_addr + PORT_STATUS);
  168. }
  169. static inline int inb_command(unsigned int base_addr)
  170. {
  171. return inb(base_addr + PORT_COMMAND);
  172. }
  173. static inline void outb_control(unsigned char val, struct net_device *dev)
  174. {
  175. outb(val, dev->base_addr + PORT_CONTROL);
  176. ((elp_device *)(dev->priv))->hcr_val = val;
  177. }
  178. #define HCR_VAL(x) (((elp_device *)((x)->priv))->hcr_val)
  179. static inline void outb_command(unsigned char val, unsigned int base_addr)
  180. {
  181. outb(val, base_addr + PORT_COMMAND);
  182. }
  183. static inline unsigned int backlog_next(unsigned int n)
  184. {
  185. return (n + 1) % BACKLOG_SIZE;
  186. }
  187. /*****************************************************************
  188. *
  189. * useful functions for accessing the adapter
  190. *
  191. *****************************************************************/
  192. /*
  193. * use this routine when accessing the ASF bits as they are
  194. * changed asynchronously by the adapter
  195. */
  196. /* get adapter PCB status */
  197. #define GET_ASF(addr) \
  198. (get_status(addr)&ASF_PCB_MASK)
  199. static inline int get_status(unsigned int base_addr)
  200. {
  201. unsigned long timeout = jiffies + 10*HZ/100;
  202. register int stat1;
  203. do {
  204. stat1 = inb_status(base_addr);
  205. } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
  206. if (time_after_eq(jiffies, timeout))
  207. TIMEOUT_MSG(__LINE__);
  208. return stat1;
  209. }
  210. static inline void set_hsf(struct net_device *dev, int hsf)
  211. {
  212. elp_device *adapter = dev->priv;
  213. unsigned long flags;
  214. spin_lock_irqsave(&adapter->lock, flags);
  215. outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
  216. spin_unlock_irqrestore(&adapter->lock, flags);
  217. }
  218. static bool start_receive(struct net_device *, pcb_struct *);
  219. static inline void adapter_reset(struct net_device *dev)
  220. {
  221. unsigned long timeout;
  222. elp_device *adapter = dev->priv;
  223. unsigned char orig_hcr = adapter->hcr_val;
  224. outb_control(0, dev);
  225. if (inb_status(dev->base_addr) & ACRF) {
  226. do {
  227. inb_command(dev->base_addr);
  228. timeout = jiffies + 2*HZ/100;
  229. while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
  230. } while (inb_status(dev->base_addr) & ACRF);
  231. set_hsf(dev, HSF_PCB_NAK);
  232. }
  233. outb_control(adapter->hcr_val | ATTN | DIR, dev);
  234. mdelay(10);
  235. outb_control(adapter->hcr_val & ~ATTN, dev);
  236. mdelay(10);
  237. outb_control(adapter->hcr_val | FLSH, dev);
  238. mdelay(10);
  239. outb_control(adapter->hcr_val & ~FLSH, dev);
  240. mdelay(10);
  241. outb_control(orig_hcr, dev);
  242. if (!start_receive(dev, &adapter->tx_pcb))
  243. printk(KERN_ERR "%s: start receive command failed \n", dev->name);
  244. }
  245. /* Check to make sure that a DMA transfer hasn't timed out. This should
  246. * never happen in theory, but seems to occur occasionally if the card gets
  247. * prodded at the wrong time.
  248. */
  249. static inline void check_3c505_dma(struct net_device *dev)
  250. {
  251. elp_device *adapter = dev->priv;
  252. if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
  253. unsigned long flags, f;
  254. printk(KERN_ERR "%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
  255. spin_lock_irqsave(&adapter->lock, flags);
  256. adapter->dmaing = 0;
  257. adapter->busy = 0;
  258. f=claim_dma_lock();
  259. disable_dma(dev->dma);
  260. release_dma_lock(f);
  261. if (adapter->rx_active)
  262. adapter->rx_active--;
  263. outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
  264. spin_unlock_irqrestore(&adapter->lock, flags);
  265. }
  266. }
  267. /* Primitive functions used by send_pcb() */
  268. static inline bool send_pcb_slow(unsigned int base_addr, unsigned char byte)
  269. {
  270. unsigned long timeout;
  271. outb_command(byte, base_addr);
  272. for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
  273. if (inb_status(base_addr) & HCRE)
  274. return false;
  275. }
  276. printk(KERN_WARNING "3c505: send_pcb_slow timed out\n");
  277. return true;
  278. }
  279. static inline bool send_pcb_fast(unsigned int base_addr, unsigned char byte)
  280. {
  281. unsigned int timeout;
  282. outb_command(byte, base_addr);
  283. for (timeout = 0; timeout < 40000; timeout++) {
  284. if (inb_status(base_addr) & HCRE)
  285. return false;
  286. }
  287. printk(KERN_WARNING "3c505: send_pcb_fast timed out\n");
  288. return true;
  289. }
  290. /* Check to see if the receiver needs restarting, and kick it if so */
  291. static inline void prime_rx(struct net_device *dev)
  292. {
  293. elp_device *adapter = dev->priv;
  294. while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
  295. if (!start_receive(dev, &adapter->itx_pcb))
  296. break;
  297. }
  298. }
  299. /*****************************************************************
  300. *
  301. * send_pcb
  302. * Send a PCB to the adapter.
  303. *
  304. * output byte to command reg --<--+
  305. * wait until HCRE is non zero |
  306. * loop until all bytes sent -->--+
  307. * set HSF1 and HSF2 to 1
  308. * output pcb length
  309. * wait until ASF give ACK or NAK
  310. * set HSF1 and HSF2 to 0
  311. *
  312. *****************************************************************/
  313. /* This can be quite slow -- the adapter is allowed to take up to 40ms
  314. * to respond to the initial interrupt.
  315. *
  316. * We run initially with interrupts turned on, but with a semaphore set
  317. * so that nobody tries to re-enter this code. Once the first byte has
  318. * gone through, we turn interrupts off and then send the others (the
  319. * timeout is reduced to 500us).
  320. */
  321. static bool send_pcb(struct net_device *dev, pcb_struct * pcb)
  322. {
  323. int i;
  324. unsigned long timeout;
  325. elp_device *adapter = dev->priv;
  326. unsigned long flags;
  327. check_3c505_dma(dev);
  328. if (adapter->dmaing && adapter->current_dma.direction == 0)
  329. return false;
  330. /* Avoid contention */
  331. if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
  332. if (elp_debug >= 3) {
  333. printk(KERN_DEBUG "%s: send_pcb entered while threaded\n", dev->name);
  334. }
  335. return false;
  336. }
  337. /*
  338. * load each byte into the command register and
  339. * wait for the HCRE bit to indicate the adapter
  340. * had read the byte
  341. */
  342. set_hsf(dev, 0);
  343. if (send_pcb_slow(dev->base_addr, pcb->command))
  344. goto abort;
  345. spin_lock_irqsave(&adapter->lock, flags);
  346. if (send_pcb_fast(dev->base_addr, pcb->length))
  347. goto sti_abort;
  348. for (i = 0; i < pcb->length; i++) {
  349. if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
  350. goto sti_abort;
  351. }
  352. outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
  353. outb_command(2 + pcb->length, dev->base_addr);
  354. /* now wait for the acknowledgement */
  355. spin_unlock_irqrestore(&adapter->lock, flags);
  356. for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
  357. switch (GET_ASF(dev->base_addr)) {
  358. case ASF_PCB_ACK:
  359. adapter->send_pcb_semaphore = 0;
  360. return true;
  361. case ASF_PCB_NAK:
  362. #ifdef ELP_DEBUG
  363. printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
  364. #endif
  365. goto abort;
  366. }
  367. }
  368. if (elp_debug >= 1)
  369. printk(KERN_DEBUG "%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
  370. goto abort;
  371. sti_abort:
  372. spin_unlock_irqrestore(&adapter->lock, flags);
  373. abort:
  374. adapter->send_pcb_semaphore = 0;
  375. return false;
  376. }
  377. /*****************************************************************
  378. *
  379. * receive_pcb
  380. * Read a PCB from the adapter
  381. *
  382. * wait for ACRF to be non-zero ---<---+
  383. * input a byte |
  384. * if ASF1 and ASF2 were not both one |
  385. * before byte was read, loop --->---+
  386. * set HSF1 and HSF2 for ack
  387. *
  388. *****************************************************************/
  389. static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
  390. {
  391. int i, j;
  392. int total_length;
  393. int stat;
  394. unsigned long timeout;
  395. unsigned long flags;
  396. elp_device *adapter = dev->priv;
  397. set_hsf(dev, 0);
  398. /* get the command code */
  399. timeout = jiffies + 2*HZ/100;
  400. while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
  401. if (time_after_eq(jiffies, timeout)) {
  402. TIMEOUT_MSG(__LINE__);
  403. return false;
  404. }
  405. pcb->command = inb_command(dev->base_addr);
  406. /* read the data length */
  407. timeout = jiffies + 3*HZ/100;
  408. while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
  409. if (time_after_eq(jiffies, timeout)) {
  410. TIMEOUT_MSG(__LINE__);
  411. printk(KERN_INFO "%s: status %02x\n", dev->name, stat);
  412. return false;
  413. }
  414. pcb->length = inb_command(dev->base_addr);
  415. if (pcb->length > MAX_PCB_DATA) {
  416. INVALID_PCB_MSG(pcb->length);
  417. adapter_reset(dev);
  418. return false;
  419. }
  420. /* read the data */
  421. spin_lock_irqsave(&adapter->lock, flags);
  422. i = 0;
  423. do {
  424. j = 0;
  425. while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
  426. pcb->data.raw[i++] = inb_command(dev->base_addr);
  427. if (i > MAX_PCB_DATA)
  428. INVALID_PCB_MSG(i);
  429. } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
  430. spin_unlock_irqrestore(&adapter->lock, flags);
  431. if (j >= 20000) {
  432. TIMEOUT_MSG(__LINE__);
  433. return false;
  434. }
  435. /* woops, the last "data" byte was really the length! */
  436. total_length = pcb->data.raw[--i];
  437. /* safety check total length vs data length */
  438. if (total_length != (pcb->length + 2)) {
  439. if (elp_debug >= 2)
  440. printk(KERN_WARNING "%s: mangled PCB received\n", dev->name);
  441. set_hsf(dev, HSF_PCB_NAK);
  442. return false;
  443. }
  444. if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
  445. if (test_and_set_bit(0, (void *) &adapter->busy)) {
  446. if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
  447. set_hsf(dev, HSF_PCB_NAK);
  448. printk(KERN_WARNING "%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
  449. pcb->command = 0;
  450. return true;
  451. } else {
  452. pcb->command = 0xff;
  453. }
  454. }
  455. }
  456. set_hsf(dev, HSF_PCB_ACK);
  457. return true;
  458. }
  459. /******************************************************
  460. *
  461. * queue a receive command on the adapter so we will get an
  462. * interrupt when a packet is received.
  463. *
  464. ******************************************************/
  465. static bool start_receive(struct net_device *dev, pcb_struct * tx_pcb)
  466. {
  467. bool status;
  468. elp_device *adapter = dev->priv;
  469. if (elp_debug >= 3)
  470. printk(KERN_DEBUG "%s: restarting receiver\n", dev->name);
  471. tx_pcb->command = CMD_RECEIVE_PACKET;
  472. tx_pcb->length = sizeof(struct Rcv_pkt);
  473. tx_pcb->data.rcv_pkt.buf_seg
  474. = tx_pcb->data.rcv_pkt.buf_ofs = 0; /* Unused */
  475. tx_pcb->data.rcv_pkt.buf_len = 1600;
  476. tx_pcb->data.rcv_pkt.timeout = 0; /* set timeout to zero */
  477. status = send_pcb(dev, tx_pcb);
  478. if (status)
  479. adapter->rx_active++;
  480. return status;
  481. }
  482. /******************************************************
  483. *
  484. * extract a packet from the adapter
  485. * this routine is only called from within the interrupt
  486. * service routine, so no cli/sti calls are needed
  487. * note that the length is always assumed to be even
  488. *
  489. ******************************************************/
  490. static void receive_packet(struct net_device *dev, int len)
  491. {
  492. int rlen;
  493. elp_device *adapter = dev->priv;
  494. void *target;
  495. struct sk_buff *skb;
  496. unsigned long flags;
  497. rlen = (len + 1) & ~1;
  498. skb = dev_alloc_skb(rlen + 2);
  499. if (!skb) {
  500. printk(KERN_WARNING "%s: memory squeeze, dropping packet\n", dev->name);
  501. target = adapter->dma_buffer;
  502. adapter->current_dma.target = NULL;
  503. /* FIXME: stats */
  504. return;
  505. }
  506. skb_reserve(skb, 2);
  507. target = skb_put(skb, rlen);
  508. if ((unsigned long)(target + rlen) >= MAX_DMA_ADDRESS) {
  509. adapter->current_dma.target = target;
  510. target = adapter->dma_buffer;
  511. } else {
  512. adapter->current_dma.target = NULL;
  513. }
  514. /* if this happens, we die */
  515. if (test_and_set_bit(0, (void *) &adapter->dmaing))
  516. printk(KERN_ERR "%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
  517. adapter->current_dma.direction = 0;
  518. adapter->current_dma.length = rlen;
  519. adapter->current_dma.skb = skb;
  520. adapter->current_dma.start_time = jiffies;
  521. outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
  522. flags=claim_dma_lock();
  523. disable_dma(dev->dma);
  524. clear_dma_ff(dev->dma);
  525. set_dma_mode(dev->dma, 0x04); /* dma read */
  526. set_dma_addr(dev->dma, isa_virt_to_bus(target));
  527. set_dma_count(dev->dma, rlen);
  528. enable_dma(dev->dma);
  529. release_dma_lock(flags);
  530. if (elp_debug >= 3) {
  531. printk(KERN_DEBUG "%s: rx DMA transfer started\n", dev->name);
  532. }
  533. if (adapter->rx_active)
  534. adapter->rx_active--;
  535. if (!adapter->busy)
  536. printk(KERN_WARNING "%s: receive_packet called, busy not set.\n", dev->name);
  537. }
  538. /******************************************************
  539. *
  540. * interrupt handler
  541. *
  542. ******************************************************/
  543. static irqreturn_t elp_interrupt(int irq, void *dev_id)
  544. {
  545. int len;
  546. int dlen;
  547. int icount = 0;
  548. struct net_device *dev;
  549. elp_device *adapter;
  550. unsigned long timeout;
  551. dev = dev_id;
  552. adapter = (elp_device *) dev->priv;
  553. spin_lock(&adapter->lock);
  554. do {
  555. /*
  556. * has a DMA transfer finished?
  557. */
  558. if (inb_status(dev->base_addr) & DONE) {
  559. if (!adapter->dmaing) {
  560. printk(KERN_WARNING "%s: phantom DMA completed\n", dev->name);
  561. }
  562. if (elp_debug >= 3) {
  563. printk(KERN_DEBUG "%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
  564. }
  565. outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
  566. if (adapter->current_dma.direction) {
  567. dev_kfree_skb_irq(adapter->current_dma.skb);
  568. } else {
  569. struct sk_buff *skb = adapter->current_dma.skb;
  570. if (skb) {
  571. if (adapter->current_dma.target) {
  572. /* have already done the skb_put() */
  573. memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
  574. }
  575. skb->protocol = eth_type_trans(skb,dev);
  576. adapter->stats.rx_bytes += skb->len;
  577. netif_rx(skb);
  578. dev->last_rx = jiffies;
  579. }
  580. }
  581. adapter->dmaing = 0;
  582. if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
  583. int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
  584. adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
  585. if (elp_debug >= 2)
  586. printk(KERN_DEBUG "%s: receiving backlogged packet (%d)\n", dev->name, t);
  587. receive_packet(dev, t);
  588. } else {
  589. adapter->busy = 0;
  590. }
  591. } else {
  592. /* has one timed out? */
  593. check_3c505_dma(dev);
  594. }
  595. /*
  596. * receive a PCB from the adapter
  597. */
  598. timeout = jiffies + 3*HZ/100;
  599. while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
  600. if (receive_pcb(dev, &adapter->irx_pcb)) {
  601. switch (adapter->irx_pcb.command)
  602. {
  603. case 0:
  604. break;
  605. /*
  606. * received a packet - this must be handled fast
  607. */
  608. case 0xff:
  609. case CMD_RECEIVE_PACKET_COMPLETE:
  610. /* if the device isn't open, don't pass packets up the stack */
  611. if (!netif_running(dev))
  612. break;
  613. len = adapter->irx_pcb.data.rcv_resp.pkt_len;
  614. dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
  615. if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
  616. printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
  617. } else {
  618. if (elp_debug >= 3) {
  619. printk(KERN_DEBUG "%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
  620. }
  621. if (adapter->irx_pcb.command == 0xff) {
  622. if (elp_debug >= 2)
  623. printk(KERN_DEBUG "%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
  624. adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
  625. adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
  626. } else {
  627. receive_packet(dev, dlen);
  628. }
  629. if (elp_debug >= 3)
  630. printk(KERN_DEBUG "%s: packet received\n", dev->name);
  631. }
  632. break;
  633. /*
  634. * 82586 configured correctly
  635. */
  636. case CMD_CONFIGURE_82586_RESPONSE:
  637. adapter->got[CMD_CONFIGURE_82586] = 1;
  638. if (elp_debug >= 3)
  639. printk(KERN_DEBUG "%s: interrupt - configure response received\n", dev->name);
  640. break;
  641. /*
  642. * Adapter memory configuration
  643. */
  644. case CMD_CONFIGURE_ADAPTER_RESPONSE:
  645. adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
  646. if (elp_debug >= 3)
  647. printk(KERN_DEBUG "%s: Adapter memory configuration %s.\n", dev->name,
  648. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  649. break;
  650. /*
  651. * Multicast list loading
  652. */
  653. case CMD_LOAD_MULTICAST_RESPONSE:
  654. adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
  655. if (elp_debug >= 3)
  656. printk(KERN_DEBUG "%s: Multicast address list loading %s.\n", dev->name,
  657. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  658. break;
  659. /*
  660. * Station address setting
  661. */
  662. case CMD_SET_ADDRESS_RESPONSE:
  663. adapter->got[CMD_SET_STATION_ADDRESS] = 1;
  664. if (elp_debug >= 3)
  665. printk(KERN_DEBUG "%s: Ethernet address setting %s.\n", dev->name,
  666. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  667. break;
  668. /*
  669. * received board statistics
  670. */
  671. case CMD_NETWORK_STATISTICS_RESPONSE:
  672. adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
  673. adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
  674. adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
  675. adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
  676. adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
  677. adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
  678. adapter->got[CMD_NETWORK_STATISTICS] = 1;
  679. if (elp_debug >= 3)
  680. printk(KERN_DEBUG "%s: interrupt - statistics response received\n", dev->name);
  681. break;
  682. /*
  683. * sent a packet
  684. */
  685. case CMD_TRANSMIT_PACKET_COMPLETE:
  686. if (elp_debug >= 3)
  687. printk(KERN_DEBUG "%s: interrupt - packet sent\n", dev->name);
  688. if (!netif_running(dev))
  689. break;
  690. switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
  691. case 0xffff:
  692. adapter->stats.tx_aborted_errors++;
  693. printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
  694. break;
  695. case 0xfffe:
  696. adapter->stats.tx_fifo_errors++;
  697. printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
  698. break;
  699. }
  700. netif_wake_queue(dev);
  701. break;
  702. /*
  703. * some unknown PCB
  704. */
  705. default:
  706. printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
  707. break;
  708. }
  709. } else {
  710. printk(KERN_WARNING "%s: failed to read PCB on interrupt\n", dev->name);
  711. adapter_reset(dev);
  712. }
  713. }
  714. } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
  715. prime_rx(dev);
  716. /*
  717. * indicate no longer in interrupt routine
  718. */
  719. spin_unlock(&adapter->lock);
  720. return IRQ_HANDLED;
  721. }
  722. /******************************************************
  723. *
  724. * open the board
  725. *
  726. ******************************************************/
  727. static int elp_open(struct net_device *dev)
  728. {
  729. elp_device *adapter;
  730. int retval;
  731. adapter = dev->priv;
  732. if (elp_debug >= 3)
  733. printk(KERN_DEBUG "%s: request to open device\n", dev->name);
  734. /*
  735. * make sure we actually found the device
  736. */
  737. if (adapter == NULL) {
  738. printk(KERN_ERR "%s: Opening a non-existent physical device\n", dev->name);
  739. return -EAGAIN;
  740. }
  741. /*
  742. * disable interrupts on the board
  743. */
  744. outb_control(0, dev);
  745. /*
  746. * clear any pending interrupts
  747. */
  748. inb_command(dev->base_addr);
  749. adapter_reset(dev);
  750. /*
  751. * no receive PCBs active
  752. */
  753. adapter->rx_active = 0;
  754. adapter->busy = 0;
  755. adapter->send_pcb_semaphore = 0;
  756. adapter->rx_backlog.in = 0;
  757. adapter->rx_backlog.out = 0;
  758. spin_lock_init(&adapter->lock);
  759. /*
  760. * install our interrupt service routine
  761. */
  762. if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
  763. printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
  764. return retval;
  765. }
  766. if ((retval = request_dma(dev->dma, dev->name))) {
  767. free_irq(dev->irq, dev);
  768. printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
  769. return retval;
  770. }
  771. adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
  772. if (!adapter->dma_buffer) {
  773. printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
  774. free_dma(dev->dma);
  775. free_irq(dev->irq, dev);
  776. return -ENOMEM;
  777. }
  778. adapter->dmaing = 0;
  779. /*
  780. * enable interrupts on the board
  781. */
  782. outb_control(CMDE, dev);
  783. /*
  784. * configure adapter memory: we need 10 multicast addresses, default==0
  785. */
  786. if (elp_debug >= 3)
  787. printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
  788. adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
  789. adapter->tx_pcb.data.memconf.cmd_q = 10;
  790. adapter->tx_pcb.data.memconf.rcv_q = 20;
  791. adapter->tx_pcb.data.memconf.mcast = 10;
  792. adapter->tx_pcb.data.memconf.frame = 20;
  793. adapter->tx_pcb.data.memconf.rcv_b = 20;
  794. adapter->tx_pcb.data.memconf.progs = 0;
  795. adapter->tx_pcb.length = sizeof(struct Memconf);
  796. adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
  797. if (!send_pcb(dev, &adapter->tx_pcb))
  798. printk(KERN_ERR "%s: couldn't send memory configuration command\n", dev->name);
  799. else {
  800. unsigned long timeout = jiffies + TIMEOUT;
  801. while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
  802. if (time_after_eq(jiffies, timeout))
  803. TIMEOUT_MSG(__LINE__);
  804. }
  805. /*
  806. * configure adapter to receive broadcast messages and wait for response
  807. */
  808. if (elp_debug >= 3)
  809. printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
  810. adapter->tx_pcb.command = CMD_CONFIGURE_82586;
  811. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
  812. adapter->tx_pcb.length = 2;
  813. adapter->got[CMD_CONFIGURE_82586] = 0;
  814. if (!send_pcb(dev, &adapter->tx_pcb))
  815. printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
  816. else {
  817. unsigned long timeout = jiffies + TIMEOUT;
  818. while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
  819. if (time_after_eq(jiffies, timeout))
  820. TIMEOUT_MSG(__LINE__);
  821. }
  822. /* enable burst-mode DMA */
  823. /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
  824. /*
  825. * queue receive commands to provide buffering
  826. */
  827. prime_rx(dev);
  828. if (elp_debug >= 3)
  829. printk(KERN_DEBUG "%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
  830. /*
  831. * device is now officially open!
  832. */
  833. netif_start_queue(dev);
  834. return 0;
  835. }
  836. /******************************************************
  837. *
  838. * send a packet to the adapter
  839. *
  840. ******************************************************/
  841. static bool send_packet(struct net_device *dev, struct sk_buff *skb)
  842. {
  843. elp_device *adapter = dev->priv;
  844. unsigned long target;
  845. unsigned long flags;
  846. /*
  847. * make sure the length is even and no shorter than 60 bytes
  848. */
  849. unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
  850. if (test_and_set_bit(0, (void *) &adapter->busy)) {
  851. if (elp_debug >= 2)
  852. printk(KERN_DEBUG "%s: transmit blocked\n", dev->name);
  853. return false;
  854. }
  855. adapter->stats.tx_bytes += nlen;
  856. /*
  857. * send the adapter a transmit packet command. Ignore segment and offset
  858. * and make sure the length is even
  859. */
  860. adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
  861. adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
  862. adapter->tx_pcb.data.xmit_pkt.buf_ofs
  863. = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
  864. adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
  865. if (!send_pcb(dev, &adapter->tx_pcb)) {
  866. adapter->busy = 0;
  867. return false;
  868. }
  869. /* if this happens, we die */
  870. if (test_and_set_bit(0, (void *) &adapter->dmaing))
  871. printk(KERN_DEBUG "%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
  872. adapter->current_dma.direction = 1;
  873. adapter->current_dma.start_time = jiffies;
  874. if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
  875. skb_copy_from_linear_data(skb, adapter->dma_buffer, nlen);
  876. memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
  877. target = isa_virt_to_bus(adapter->dma_buffer);
  878. }
  879. else {
  880. target = isa_virt_to_bus(skb->data);
  881. }
  882. adapter->current_dma.skb = skb;
  883. flags=claim_dma_lock();
  884. disable_dma(dev->dma);
  885. clear_dma_ff(dev->dma);
  886. set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
  887. set_dma_addr(dev->dma, target);
  888. set_dma_count(dev->dma, nlen);
  889. outb_control(adapter->hcr_val | DMAE | TCEN, dev);
  890. enable_dma(dev->dma);
  891. release_dma_lock(flags);
  892. if (elp_debug >= 3)
  893. printk(KERN_DEBUG "%s: DMA transfer started\n", dev->name);
  894. return true;
  895. }
  896. /*
  897. * The upper layer thinks we timed out
  898. */
  899. static void elp_timeout(struct net_device *dev)
  900. {
  901. elp_device *adapter = dev->priv;
  902. int stat;
  903. stat = inb_status(dev->base_addr);
  904. printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
  905. if (elp_debug >= 1)
  906. printk(KERN_DEBUG "%s: status %#02x\n", dev->name, stat);
  907. dev->trans_start = jiffies;
  908. adapter->stats.tx_dropped++;
  909. netif_wake_queue(dev);
  910. }
  911. /******************************************************
  912. *
  913. * start the transmitter
  914. * return 0 if sent OK, else return 1
  915. *
  916. ******************************************************/
  917. static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  918. {
  919. unsigned long flags;
  920. elp_device *adapter = dev->priv;
  921. spin_lock_irqsave(&adapter->lock, flags);
  922. check_3c505_dma(dev);
  923. if (elp_debug >= 3)
  924. printk(KERN_DEBUG "%s: request to send packet of length %d\n", dev->name, (int) skb->len);
  925. netif_stop_queue(dev);
  926. /*
  927. * send the packet at skb->data for skb->len
  928. */
  929. if (!send_packet(dev, skb)) {
  930. if (elp_debug >= 2) {
  931. printk(KERN_DEBUG "%s: failed to transmit packet\n", dev->name);
  932. }
  933. spin_unlock_irqrestore(&adapter->lock, flags);
  934. return 1;
  935. }
  936. if (elp_debug >= 3)
  937. printk(KERN_DEBUG "%s: packet of length %d sent\n", dev->name, (int) skb->len);
  938. /*
  939. * start the transmit timeout
  940. */
  941. dev->trans_start = jiffies;
  942. prime_rx(dev);
  943. spin_unlock_irqrestore(&adapter->lock, flags);
  944. netif_start_queue(dev);
  945. return 0;
  946. }
  947. /******************************************************
  948. *
  949. * return statistics on the board
  950. *
  951. ******************************************************/
  952. static struct net_device_stats *elp_get_stats(struct net_device *dev)
  953. {
  954. elp_device *adapter = (elp_device *) dev->priv;
  955. if (elp_debug >= 3)
  956. printk(KERN_DEBUG "%s: request for stats\n", dev->name);
  957. /* If the device is closed, just return the latest stats we have,
  958. - we cannot ask from the adapter without interrupts */
  959. if (!netif_running(dev))
  960. return &adapter->stats;
  961. /* send a get statistics command to the board */
  962. adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
  963. adapter->tx_pcb.length = 0;
  964. adapter->got[CMD_NETWORK_STATISTICS] = 0;
  965. if (!send_pcb(dev, &adapter->tx_pcb))
  966. printk(KERN_ERR "%s: couldn't send get statistics command\n", dev->name);
  967. else {
  968. unsigned long timeout = jiffies + TIMEOUT;
  969. while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
  970. if (time_after_eq(jiffies, timeout)) {
  971. TIMEOUT_MSG(__LINE__);
  972. return &adapter->stats;
  973. }
  974. }
  975. /* statistics are now up to date */
  976. return &adapter->stats;
  977. }
  978. static void netdev_get_drvinfo(struct net_device *dev,
  979. struct ethtool_drvinfo *info)
  980. {
  981. strcpy(info->driver, DRV_NAME);
  982. strcpy(info->version, DRV_VERSION);
  983. sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
  984. }
  985. static u32 netdev_get_msglevel(struct net_device *dev)
  986. {
  987. return debug;
  988. }
  989. static void netdev_set_msglevel(struct net_device *dev, u32 level)
  990. {
  991. debug = level;
  992. }
  993. static const struct ethtool_ops netdev_ethtool_ops = {
  994. .get_drvinfo = netdev_get_drvinfo,
  995. .get_msglevel = netdev_get_msglevel,
  996. .set_msglevel = netdev_set_msglevel,
  997. };
  998. /******************************************************
  999. *
  1000. * close the board
  1001. *
  1002. ******************************************************/
  1003. static int elp_close(struct net_device *dev)
  1004. {
  1005. elp_device *adapter;
  1006. adapter = dev->priv;
  1007. if (elp_debug >= 3)
  1008. printk(KERN_DEBUG "%s: request to close device\n", dev->name);
  1009. netif_stop_queue(dev);
  1010. /* Someone may request the device statistic information even when
  1011. * the interface is closed. The following will update the statistics
  1012. * structure in the driver, so we'll be able to give current statistics.
  1013. */
  1014. (void) elp_get_stats(dev);
  1015. /*
  1016. * disable interrupts on the board
  1017. */
  1018. outb_control(0, dev);
  1019. /*
  1020. * release the IRQ
  1021. */
  1022. free_irq(dev->irq, dev);
  1023. free_dma(dev->dma);
  1024. free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
  1025. return 0;
  1026. }
  1027. /************************************************************
  1028. *
  1029. * Set multicast list
  1030. * num_addrs==0: clear mc_list
  1031. * num_addrs==-1: set promiscuous mode
  1032. * num_addrs>0: set mc_list
  1033. *
  1034. ************************************************************/
  1035. static void elp_set_mc_list(struct net_device *dev)
  1036. {
  1037. elp_device *adapter = (elp_device *) dev->priv;
  1038. struct dev_mc_list *dmi = dev->mc_list;
  1039. int i;
  1040. unsigned long flags;
  1041. if (elp_debug >= 3)
  1042. printk(KERN_DEBUG "%s: request to set multicast list\n", dev->name);
  1043. spin_lock_irqsave(&adapter->lock, flags);
  1044. if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
  1045. /* send a "load multicast list" command to the board, max 10 addrs/cmd */
  1046. /* if num_addrs==0 the list will be cleared */
  1047. adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
  1048. adapter->tx_pcb.length = 6 * dev->mc_count;
  1049. for (i = 0; i < dev->mc_count; i++) {
  1050. memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
  1051. dmi = dmi->next;
  1052. }
  1053. adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
  1054. if (!send_pcb(dev, &adapter->tx_pcb))
  1055. printk(KERN_ERR "%s: couldn't send set_multicast command\n", dev->name);
  1056. else {
  1057. unsigned long timeout = jiffies + TIMEOUT;
  1058. while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
  1059. if (time_after_eq(jiffies, timeout)) {
  1060. TIMEOUT_MSG(__LINE__);
  1061. }
  1062. }
  1063. if (dev->mc_count)
  1064. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
  1065. else /* num_addrs == 0 */
  1066. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
  1067. } else
  1068. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
  1069. /*
  1070. * configure adapter to receive messages (as specified above)
  1071. * and wait for response
  1072. */
  1073. if (elp_debug >= 3)
  1074. printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
  1075. adapter->tx_pcb.command = CMD_CONFIGURE_82586;
  1076. adapter->tx_pcb.length = 2;
  1077. adapter->got[CMD_CONFIGURE_82586] = 0;
  1078. if (!send_pcb(dev, &adapter->tx_pcb))
  1079. {
  1080. spin_unlock_irqrestore(&adapter->lock, flags);
  1081. printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
  1082. }
  1083. else {
  1084. unsigned long timeout = jiffies + TIMEOUT;
  1085. spin_unlock_irqrestore(&adapter->lock, flags);
  1086. while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
  1087. if (time_after_eq(jiffies, timeout))
  1088. TIMEOUT_MSG(__LINE__);
  1089. }
  1090. }
  1091. /************************************************************
  1092. *
  1093. * A couple of tests to see if there's 3C505 or not
  1094. * Called only by elp_autodetect
  1095. ************************************************************/
  1096. static int __init elp_sense(struct net_device *dev)
  1097. {
  1098. int addr = dev->base_addr;
  1099. const char *name = dev->name;
  1100. byte orig_HSR;
  1101. if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
  1102. return -ENODEV;
  1103. orig_HSR = inb_status(addr);
  1104. if (elp_debug > 0)
  1105. printk(search_msg, name, addr);
  1106. if (orig_HSR == 0xff) {
  1107. if (elp_debug > 0)
  1108. printk(notfound_msg, 1);
  1109. goto out;
  1110. }
  1111. /* Wait for a while; the adapter may still be booting up */
  1112. if (elp_debug > 0)
  1113. printk(stilllooking_msg);
  1114. if (orig_HSR & DIR) {
  1115. /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
  1116. outb(0, dev->base_addr + PORT_CONTROL);
  1117. msleep(300);
  1118. if (inb_status(addr) & DIR) {
  1119. if (elp_debug > 0)
  1120. printk(notfound_msg, 2);
  1121. goto out;
  1122. }
  1123. } else {
  1124. /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
  1125. outb(DIR, dev->base_addr + PORT_CONTROL);
  1126. msleep(300);
  1127. if (!(inb_status(addr) & DIR)) {
  1128. if (elp_debug > 0)
  1129. printk(notfound_msg, 3);
  1130. goto out;
  1131. }
  1132. }
  1133. /*
  1134. * It certainly looks like a 3c505.
  1135. */
  1136. if (elp_debug > 0)
  1137. printk(found_msg);
  1138. return 0;
  1139. out:
  1140. release_region(addr, ELP_IO_EXTENT);
  1141. return -ENODEV;
  1142. }
  1143. /*************************************************************
  1144. *
  1145. * Search through addr_list[] and try to find a 3C505
  1146. * Called only by eplus_probe
  1147. *************************************************************/
  1148. static int __init elp_autodetect(struct net_device *dev)
  1149. {
  1150. int idx = 0;
  1151. /* if base address set, then only check that address
  1152. otherwise, run through the table */
  1153. if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
  1154. if (elp_sense(dev) == 0)
  1155. return dev->base_addr;
  1156. } else
  1157. while ((dev->base_addr = addr_list[idx++])) {
  1158. if (elp_sense(dev) == 0)
  1159. return dev->base_addr;
  1160. }
  1161. /* could not find an adapter */
  1162. if (elp_debug > 0)
  1163. printk(couldnot_msg, dev->name);
  1164. return 0; /* Because of this, the layer above will return -ENODEV */
  1165. }
  1166. /******************************************************
  1167. *
  1168. * probe for an Etherlink Plus board at the specified address
  1169. *
  1170. ******************************************************/
  1171. /* There are three situations we need to be able to detect here:
  1172. * a) the card is idle
  1173. * b) the card is still booting up
  1174. * c) the card is stuck in a strange state (some DOS drivers do this)
  1175. *
  1176. * In case (a), all is well. In case (b), we wait 10 seconds to see if the
  1177. * card finishes booting, and carry on if so. In case (c), we do a hard reset,
  1178. * loop round, and hope for the best.
  1179. *
  1180. * This is all very unpleasant, but hopefully avoids the problems with the old
  1181. * probe code (which had a 15-second delay if the card was idle, and didn't
  1182. * work at all if it was in a weird state).
  1183. */
  1184. static int __init elplus_setup(struct net_device *dev)
  1185. {
  1186. elp_device *adapter = dev->priv;
  1187. int i, tries, tries1, okay;
  1188. unsigned long timeout;
  1189. unsigned long cookie = 0;
  1190. int err = -ENODEV;
  1191. DECLARE_MAC_BUF(mac);
  1192. /*
  1193. * setup adapter structure
  1194. */
  1195. dev->base_addr = elp_autodetect(dev);
  1196. if (!dev->base_addr)
  1197. return -ENODEV;
  1198. adapter->send_pcb_semaphore = 0;
  1199. for (tries1 = 0; tries1 < 3; tries1++) {
  1200. outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
  1201. /* First try to write just one byte, to see if the card is
  1202. * responding at all normally.
  1203. */
  1204. timeout = jiffies + 5*HZ/100;
  1205. okay = 0;
  1206. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1207. if ((inb_status(dev->base_addr) & HCRE)) {
  1208. outb_command(0, dev->base_addr); /* send a spurious byte */
  1209. timeout = jiffies + 5*HZ/100;
  1210. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1211. if (inb_status(dev->base_addr) & HCRE)
  1212. okay = 1;
  1213. }
  1214. if (!okay) {
  1215. /* Nope, it's ignoring the command register. This means that
  1216. * either it's still booting up, or it's died.
  1217. */
  1218. printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
  1219. if ((inb_status(dev->base_addr) & 7) == 3) {
  1220. /* If the adapter status is 3, it *could* still be booting.
  1221. * Give it the benefit of the doubt for 10 seconds.
  1222. */
  1223. printk("assuming 3c505 still starting\n");
  1224. timeout = jiffies + 10*HZ;
  1225. while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
  1226. if (inb_status(dev->base_addr) & 7) {
  1227. printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
  1228. } else {
  1229. okay = 1; /* It started */
  1230. }
  1231. } else {
  1232. /* Otherwise, it must just be in a strange
  1233. * state. We probably need to kick it.
  1234. */
  1235. printk("3c505 is sulking\n");
  1236. }
  1237. }
  1238. for (tries = 0; tries < 5 && okay; tries++) {
  1239. /*
  1240. * Try to set the Ethernet address, to make sure that the board
  1241. * is working.
  1242. */
  1243. adapter->tx_pcb.command = CMD_STATION_ADDRESS;
  1244. adapter->tx_pcb.length = 0;
  1245. cookie = probe_irq_on();
  1246. if (!send_pcb(dev, &adapter->tx_pcb)) {
  1247. printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
  1248. probe_irq_off(cookie);
  1249. continue;
  1250. }
  1251. if (!receive_pcb(dev, &adapter->rx_pcb)) {
  1252. printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
  1253. probe_irq_off(cookie);
  1254. continue;
  1255. }
  1256. if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
  1257. (adapter->rx_pcb.length != 6)) {
  1258. printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
  1259. probe_irq_off(cookie);
  1260. continue;
  1261. }
  1262. goto okay;
  1263. }
  1264. /* It's broken. Do a hard reset to re-initialise the board,
  1265. * and try again.
  1266. */
  1267. printk(KERN_INFO "%s: resetting adapter\n", dev->name);
  1268. outb_control(adapter->hcr_val | FLSH | ATTN, dev);
  1269. outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
  1270. }
  1271. printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
  1272. goto out;
  1273. okay:
  1274. if (dev->irq) { /* Is there a preset IRQ? */
  1275. int rpt = probe_irq_off(cookie);
  1276. if (dev->irq != rpt) {
  1277. printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
  1278. }
  1279. /* if dev->irq == probe_irq_off(cookie), all is well */
  1280. } else /* No preset IRQ; just use what we can detect */
  1281. dev->irq = probe_irq_off(cookie);
  1282. switch (dev->irq) { /* Legal, sane? */
  1283. case 0:
  1284. printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
  1285. dev->name);
  1286. goto out;
  1287. case 1:
  1288. case 6:
  1289. case 8:
  1290. case 13:
  1291. printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
  1292. dev->name, dev->irq);
  1293. goto out;
  1294. }
  1295. /*
  1296. * Now we have the IRQ number so we can disable the interrupts from
  1297. * the board until the board is opened.
  1298. */
  1299. outb_control(adapter->hcr_val & ~CMDE, dev);
  1300. /*
  1301. * copy Ethernet address into structure
  1302. */
  1303. for (i = 0; i < 6; i++)
  1304. dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
  1305. /* find a DMA channel */
  1306. if (!dev->dma) {
  1307. if (dev->mem_start) {
  1308. dev->dma = dev->mem_start & 7;
  1309. }
  1310. else {
  1311. printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
  1312. dev->dma = ELP_DMA;
  1313. }
  1314. }
  1315. /*
  1316. * print remainder of startup message
  1317. */
  1318. printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, "
  1319. "addr %s, ",
  1320. dev->name, dev->base_addr, dev->irq, dev->dma,
  1321. print_mac(mac, dev->dev_addr));
  1322. /*
  1323. * read more information from the adapter
  1324. */
  1325. adapter->tx_pcb.command = CMD_ADAPTER_INFO;
  1326. adapter->tx_pcb.length = 0;
  1327. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1328. !receive_pcb(dev, &adapter->rx_pcb) ||
  1329. (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
  1330. (adapter->rx_pcb.length != 10)) {
  1331. printk("not responding to second PCB\n");
  1332. }
  1333. printk("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers, adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
  1334. /*
  1335. * reconfigure the adapter memory to better suit our purposes
  1336. */
  1337. adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
  1338. adapter->tx_pcb.length = 12;
  1339. adapter->tx_pcb.data.memconf.cmd_q = 8;
  1340. adapter->tx_pcb.data.memconf.rcv_q = 8;
  1341. adapter->tx_pcb.data.memconf.mcast = 10;
  1342. adapter->tx_pcb.data.memconf.frame = 10;
  1343. adapter->tx_pcb.data.memconf.rcv_b = 10;
  1344. adapter->tx_pcb.data.memconf.progs = 0;
  1345. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1346. !receive_pcb(dev, &adapter->rx_pcb) ||
  1347. (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
  1348. (adapter->rx_pcb.length != 2)) {
  1349. printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
  1350. }
  1351. if (adapter->rx_pcb.data.configure) {
  1352. printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
  1353. }
  1354. dev->open = elp_open; /* local */
  1355. dev->stop = elp_close; /* local */
  1356. dev->get_stats = elp_get_stats; /* local */
  1357. dev->hard_start_xmit = elp_start_xmit; /* local */
  1358. dev->tx_timeout = elp_timeout; /* local */
  1359. dev->watchdog_timeo = 10*HZ;
  1360. dev->set_multicast_list = elp_set_mc_list; /* local */
  1361. dev->ethtool_ops = &netdev_ethtool_ops; /* local */
  1362. memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
  1363. dev->mem_start = dev->mem_end = 0;
  1364. err = register_netdev(dev);
  1365. if (err)
  1366. goto out;
  1367. return 0;
  1368. out:
  1369. release_region(dev->base_addr, ELP_IO_EXTENT);
  1370. return err;
  1371. }
  1372. #ifndef MODULE
  1373. struct net_device * __init elplus_probe(int unit)
  1374. {
  1375. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1376. int err;
  1377. if (!dev)
  1378. return ERR_PTR(-ENOMEM);
  1379. sprintf(dev->name, "eth%d", unit);
  1380. netdev_boot_setup_check(dev);
  1381. err = elplus_setup(dev);
  1382. if (err) {
  1383. free_netdev(dev);
  1384. return ERR_PTR(err);
  1385. }
  1386. return dev;
  1387. }
  1388. #else
  1389. static struct net_device *dev_3c505[ELP_MAX_CARDS];
  1390. static int io[ELP_MAX_CARDS];
  1391. static int irq[ELP_MAX_CARDS];
  1392. static int dma[ELP_MAX_CARDS];
  1393. module_param_array(io, int, NULL, 0);
  1394. module_param_array(irq, int, NULL, 0);
  1395. module_param_array(dma, int, NULL, 0);
  1396. MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
  1397. MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
  1398. MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
  1399. int __init init_module(void)
  1400. {
  1401. int this_dev, found = 0;
  1402. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1403. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1404. if (!dev)
  1405. break;
  1406. dev->irq = irq[this_dev];
  1407. dev->base_addr = io[this_dev];
  1408. if (dma[this_dev]) {
  1409. dev->dma = dma[this_dev];
  1410. } else {
  1411. dev->dma = ELP_DMA;
  1412. printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
  1413. }
  1414. if (io[this_dev] == 0) {
  1415. if (this_dev) {
  1416. free_netdev(dev);
  1417. break;
  1418. }
  1419. printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
  1420. }
  1421. if (elplus_setup(dev) != 0) {
  1422. printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
  1423. free_netdev(dev);
  1424. break;
  1425. }
  1426. dev_3c505[this_dev] = dev;
  1427. found++;
  1428. }
  1429. if (!found)
  1430. return -ENODEV;
  1431. return 0;
  1432. }
  1433. void __exit cleanup_module(void)
  1434. {
  1435. int this_dev;
  1436. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1437. struct net_device *dev = dev_3c505[this_dev];
  1438. if (dev) {
  1439. unregister_netdev(dev);
  1440. release_region(dev->base_addr, ELP_IO_EXTENT);
  1441. free_netdev(dev);
  1442. }
  1443. }
  1444. }
  1445. #endif /* MODULE */
  1446. MODULE_LICENSE("GPL");