3c505.c 49 KB

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