3c505.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. * 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. adapter->current_dma.direction = 0;
  529. adapter->current_dma.length = rlen;
  530. adapter->current_dma.skb = skb;
  531. adapter->current_dma.start_time = jiffies;
  532. outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
  533. flags=claim_dma_lock();
  534. disable_dma(dev->dma);
  535. clear_dma_ff(dev->dma);
  536. set_dma_mode(dev->dma, 0x04); /* dma read */
  537. set_dma_addr(dev->dma, isa_virt_to_bus(target));
  538. set_dma_count(dev->dma, rlen);
  539. enable_dma(dev->dma);
  540. release_dma_lock(flags);
  541. if (elp_debug >= 3) {
  542. printk(KERN_DEBUG "%s: rx DMA transfer started\n", dev->name);
  543. }
  544. if (adapter->rx_active)
  545. adapter->rx_active--;
  546. if (!adapter->busy)
  547. printk(KERN_WARNING "%s: receive_packet called, busy not set.\n", dev->name);
  548. }
  549. /******************************************************
  550. *
  551. * interrupt handler
  552. *
  553. ******************************************************/
  554. static irqreturn_t elp_interrupt(int irq, void *dev_id)
  555. {
  556. int len;
  557. int dlen;
  558. int icount = 0;
  559. struct net_device *dev;
  560. elp_device *adapter;
  561. unsigned long timeout;
  562. dev = dev_id;
  563. adapter = (elp_device *) dev->priv;
  564. spin_lock(&adapter->lock);
  565. do {
  566. /*
  567. * has a DMA transfer finished?
  568. */
  569. if (inb_status(dev->base_addr) & DONE) {
  570. if (!adapter->dmaing) {
  571. printk(KERN_WARNING "%s: phantom DMA completed\n", dev->name);
  572. }
  573. if (elp_debug >= 3) {
  574. printk(KERN_DEBUG "%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
  575. }
  576. outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
  577. if (adapter->current_dma.direction) {
  578. dev_kfree_skb_irq(adapter->current_dma.skb);
  579. } else {
  580. struct sk_buff *skb = adapter->current_dma.skb;
  581. if (skb) {
  582. if (adapter->current_dma.target) {
  583. /* have already done the skb_put() */
  584. memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
  585. }
  586. skb->protocol = eth_type_trans(skb,dev);
  587. adapter->stats.rx_bytes += skb->len;
  588. netif_rx(skb);
  589. dev->last_rx = jiffies;
  590. }
  591. }
  592. adapter->dmaing = 0;
  593. if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
  594. int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
  595. adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
  596. if (elp_debug >= 2)
  597. printk(KERN_DEBUG "%s: receiving backlogged packet (%d)\n", dev->name, t);
  598. receive_packet(dev, t);
  599. } else {
  600. adapter->busy = 0;
  601. }
  602. } else {
  603. /* has one timed out? */
  604. check_3c505_dma(dev);
  605. }
  606. /*
  607. * receive a PCB from the adapter
  608. */
  609. timeout = jiffies + 3*HZ/100;
  610. while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
  611. if (receive_pcb(dev, &adapter->irx_pcb)) {
  612. switch (adapter->irx_pcb.command)
  613. {
  614. case 0:
  615. break;
  616. /*
  617. * received a packet - this must be handled fast
  618. */
  619. case 0xff:
  620. case CMD_RECEIVE_PACKET_COMPLETE:
  621. /* if the device isn't open, don't pass packets up the stack */
  622. if (!netif_running(dev))
  623. break;
  624. len = adapter->irx_pcb.data.rcv_resp.pkt_len;
  625. dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
  626. if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
  627. printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
  628. } else {
  629. if (elp_debug >= 3) {
  630. printk(KERN_DEBUG "%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
  631. }
  632. if (adapter->irx_pcb.command == 0xff) {
  633. if (elp_debug >= 2)
  634. printk(KERN_DEBUG "%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
  635. adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
  636. adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
  637. } else {
  638. receive_packet(dev, dlen);
  639. }
  640. if (elp_debug >= 3)
  641. printk(KERN_DEBUG "%s: packet received\n", dev->name);
  642. }
  643. break;
  644. /*
  645. * 82586 configured correctly
  646. */
  647. case CMD_CONFIGURE_82586_RESPONSE:
  648. adapter->got[CMD_CONFIGURE_82586] = 1;
  649. if (elp_debug >= 3)
  650. printk(KERN_DEBUG "%s: interrupt - configure response received\n", dev->name);
  651. break;
  652. /*
  653. * Adapter memory configuration
  654. */
  655. case CMD_CONFIGURE_ADAPTER_RESPONSE:
  656. adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
  657. if (elp_debug >= 3)
  658. printk(KERN_DEBUG "%s: Adapter memory configuration %s.\n", dev->name,
  659. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  660. break;
  661. /*
  662. * Multicast list loading
  663. */
  664. case CMD_LOAD_MULTICAST_RESPONSE:
  665. adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
  666. if (elp_debug >= 3)
  667. printk(KERN_DEBUG "%s: Multicast address list loading %s.\n", dev->name,
  668. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  669. break;
  670. /*
  671. * Station address setting
  672. */
  673. case CMD_SET_ADDRESS_RESPONSE:
  674. adapter->got[CMD_SET_STATION_ADDRESS] = 1;
  675. if (elp_debug >= 3)
  676. printk(KERN_DEBUG "%s: Ethernet address setting %s.\n", dev->name,
  677. adapter->irx_pcb.data.failed ? "failed" : "succeeded");
  678. break;
  679. /*
  680. * received board statistics
  681. */
  682. case CMD_NETWORK_STATISTICS_RESPONSE:
  683. adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
  684. adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
  685. adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
  686. adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
  687. adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
  688. adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
  689. adapter->got[CMD_NETWORK_STATISTICS] = 1;
  690. if (elp_debug >= 3)
  691. printk(KERN_DEBUG "%s: interrupt - statistics response received\n", dev->name);
  692. break;
  693. /*
  694. * sent a packet
  695. */
  696. case CMD_TRANSMIT_PACKET_COMPLETE:
  697. if (elp_debug >= 3)
  698. printk(KERN_DEBUG "%s: interrupt - packet sent\n", dev->name);
  699. if (!netif_running(dev))
  700. break;
  701. switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
  702. case 0xffff:
  703. adapter->stats.tx_aborted_errors++;
  704. printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
  705. break;
  706. case 0xfffe:
  707. adapter->stats.tx_fifo_errors++;
  708. printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
  709. break;
  710. }
  711. netif_wake_queue(dev);
  712. break;
  713. /*
  714. * some unknown PCB
  715. */
  716. default:
  717. printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
  718. break;
  719. }
  720. } else {
  721. printk(KERN_WARNING "%s: failed to read PCB on interrupt\n", dev->name);
  722. adapter_reset(dev);
  723. }
  724. }
  725. } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
  726. prime_rx(dev);
  727. /*
  728. * indicate no longer in interrupt routine
  729. */
  730. spin_unlock(&adapter->lock);
  731. return IRQ_HANDLED;
  732. }
  733. /******************************************************
  734. *
  735. * open the board
  736. *
  737. ******************************************************/
  738. static int elp_open(struct net_device *dev)
  739. {
  740. elp_device *adapter;
  741. int retval;
  742. adapter = dev->priv;
  743. if (elp_debug >= 3)
  744. printk(KERN_DEBUG "%s: request to open device\n", dev->name);
  745. /*
  746. * make sure we actually found the device
  747. */
  748. if (adapter == NULL) {
  749. printk(KERN_ERR "%s: Opening a non-existent physical device\n", dev->name);
  750. return -EAGAIN;
  751. }
  752. /*
  753. * disable interrupts on the board
  754. */
  755. outb_control(0, dev);
  756. /*
  757. * clear any pending interrupts
  758. */
  759. inb_command(dev->base_addr);
  760. adapter_reset(dev);
  761. /*
  762. * no receive PCBs active
  763. */
  764. adapter->rx_active = 0;
  765. adapter->busy = 0;
  766. adapter->send_pcb_semaphore = 0;
  767. adapter->rx_backlog.in = 0;
  768. adapter->rx_backlog.out = 0;
  769. spin_lock_init(&adapter->lock);
  770. /*
  771. * install our interrupt service routine
  772. */
  773. if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
  774. printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
  775. return retval;
  776. }
  777. if ((retval = request_dma(dev->dma, dev->name))) {
  778. free_irq(dev->irq, dev);
  779. printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
  780. return retval;
  781. }
  782. adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
  783. if (!adapter->dma_buffer) {
  784. printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
  785. free_dma(dev->dma);
  786. free_irq(dev->irq, dev);
  787. return -ENOMEM;
  788. }
  789. adapter->dmaing = 0;
  790. /*
  791. * enable interrupts on the board
  792. */
  793. outb_control(CMDE, dev);
  794. /*
  795. * configure adapter memory: we need 10 multicast addresses, default==0
  796. */
  797. if (elp_debug >= 3)
  798. printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
  799. adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
  800. adapter->tx_pcb.data.memconf.cmd_q = 10;
  801. adapter->tx_pcb.data.memconf.rcv_q = 20;
  802. adapter->tx_pcb.data.memconf.mcast = 10;
  803. adapter->tx_pcb.data.memconf.frame = 20;
  804. adapter->tx_pcb.data.memconf.rcv_b = 20;
  805. adapter->tx_pcb.data.memconf.progs = 0;
  806. adapter->tx_pcb.length = sizeof(struct Memconf);
  807. adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
  808. if (!send_pcb(dev, &adapter->tx_pcb))
  809. printk(KERN_ERR "%s: couldn't send memory configuration command\n", dev->name);
  810. else {
  811. unsigned long timeout = jiffies + TIMEOUT;
  812. while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
  813. if (time_after_eq(jiffies, timeout))
  814. TIMEOUT_MSG(__LINE__);
  815. }
  816. /*
  817. * configure adapter to receive broadcast messages and wait for response
  818. */
  819. if (elp_debug >= 3)
  820. printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
  821. adapter->tx_pcb.command = CMD_CONFIGURE_82586;
  822. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
  823. adapter->tx_pcb.length = 2;
  824. adapter->got[CMD_CONFIGURE_82586] = 0;
  825. if (!send_pcb(dev, &adapter->tx_pcb))
  826. printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
  827. else {
  828. unsigned long timeout = jiffies + TIMEOUT;
  829. while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
  830. if (time_after_eq(jiffies, timeout))
  831. TIMEOUT_MSG(__LINE__);
  832. }
  833. /* enable burst-mode DMA */
  834. /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
  835. /*
  836. * queue receive commands to provide buffering
  837. */
  838. prime_rx(dev);
  839. if (elp_debug >= 3)
  840. printk(KERN_DEBUG "%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
  841. /*
  842. * device is now officially open!
  843. */
  844. netif_start_queue(dev);
  845. return 0;
  846. }
  847. /******************************************************
  848. *
  849. * send a packet to the adapter
  850. *
  851. ******************************************************/
  852. static int send_packet(struct net_device *dev, struct sk_buff *skb)
  853. {
  854. elp_device *adapter = dev->priv;
  855. unsigned long target;
  856. unsigned long flags;
  857. /*
  858. * make sure the length is even and no shorter than 60 bytes
  859. */
  860. unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
  861. if (test_and_set_bit(0, (void *) &adapter->busy)) {
  862. if (elp_debug >= 2)
  863. printk(KERN_DEBUG "%s: transmit blocked\n", dev->name);
  864. return FALSE;
  865. }
  866. adapter->stats.tx_bytes += nlen;
  867. /*
  868. * send the adapter a transmit packet command. Ignore segment and offset
  869. * and make sure the length is even
  870. */
  871. adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
  872. adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
  873. adapter->tx_pcb.data.xmit_pkt.buf_ofs
  874. = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
  875. adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
  876. if (!send_pcb(dev, &adapter->tx_pcb)) {
  877. adapter->busy = 0;
  878. return FALSE;
  879. }
  880. /* if this happens, we die */
  881. if (test_and_set_bit(0, (void *) &adapter->dmaing))
  882. printk(KERN_DEBUG "%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
  883. adapter->current_dma.direction = 1;
  884. adapter->current_dma.start_time = jiffies;
  885. if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
  886. skb_copy_from_linear_data(skb, adapter->dma_buffer, nlen);
  887. memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
  888. target = isa_virt_to_bus(adapter->dma_buffer);
  889. }
  890. else {
  891. target = isa_virt_to_bus(skb->data);
  892. }
  893. adapter->current_dma.skb = skb;
  894. flags=claim_dma_lock();
  895. disable_dma(dev->dma);
  896. clear_dma_ff(dev->dma);
  897. set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
  898. set_dma_addr(dev->dma, target);
  899. set_dma_count(dev->dma, nlen);
  900. outb_control(adapter->hcr_val | DMAE | TCEN, dev);
  901. enable_dma(dev->dma);
  902. release_dma_lock(flags);
  903. if (elp_debug >= 3)
  904. printk(KERN_DEBUG "%s: DMA transfer started\n", dev->name);
  905. return TRUE;
  906. }
  907. /*
  908. * The upper layer thinks we timed out
  909. */
  910. static void elp_timeout(struct net_device *dev)
  911. {
  912. elp_device *adapter = dev->priv;
  913. int stat;
  914. stat = inb_status(dev->base_addr);
  915. printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
  916. if (elp_debug >= 1)
  917. printk(KERN_DEBUG "%s: status %#02x\n", dev->name, stat);
  918. dev->trans_start = jiffies;
  919. adapter->stats.tx_dropped++;
  920. netif_wake_queue(dev);
  921. }
  922. /******************************************************
  923. *
  924. * start the transmitter
  925. * return 0 if sent OK, else return 1
  926. *
  927. ******************************************************/
  928. static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  929. {
  930. unsigned long flags;
  931. elp_device *adapter = dev->priv;
  932. spin_lock_irqsave(&adapter->lock, flags);
  933. check_3c505_dma(dev);
  934. if (elp_debug >= 3)
  935. printk(KERN_DEBUG "%s: request to send packet of length %d\n", dev->name, (int) skb->len);
  936. netif_stop_queue(dev);
  937. /*
  938. * send the packet at skb->data for skb->len
  939. */
  940. if (!send_packet(dev, skb)) {
  941. if (elp_debug >= 2) {
  942. printk(KERN_DEBUG "%s: failed to transmit packet\n", dev->name);
  943. }
  944. spin_unlock_irqrestore(&adapter->lock, flags);
  945. return 1;
  946. }
  947. if (elp_debug >= 3)
  948. printk(KERN_DEBUG "%s: packet of length %d sent\n", dev->name, (int) skb->len);
  949. /*
  950. * start the transmit timeout
  951. */
  952. dev->trans_start = jiffies;
  953. prime_rx(dev);
  954. spin_unlock_irqrestore(&adapter->lock, flags);
  955. netif_start_queue(dev);
  956. return 0;
  957. }
  958. /******************************************************
  959. *
  960. * return statistics on the board
  961. *
  962. ******************************************************/
  963. static struct net_device_stats *elp_get_stats(struct net_device *dev)
  964. {
  965. elp_device *adapter = (elp_device *) dev->priv;
  966. if (elp_debug >= 3)
  967. printk(KERN_DEBUG "%s: request for stats\n", dev->name);
  968. /* If the device is closed, just return the latest stats we have,
  969. - we cannot ask from the adapter without interrupts */
  970. if (!netif_running(dev))
  971. return &adapter->stats;
  972. /* send a get statistics command to the board */
  973. adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
  974. adapter->tx_pcb.length = 0;
  975. adapter->got[CMD_NETWORK_STATISTICS] = 0;
  976. if (!send_pcb(dev, &adapter->tx_pcb))
  977. printk(KERN_ERR "%s: couldn't send get statistics command\n", dev->name);
  978. else {
  979. unsigned long timeout = jiffies + TIMEOUT;
  980. while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
  981. if (time_after_eq(jiffies, timeout)) {
  982. TIMEOUT_MSG(__LINE__);
  983. return &adapter->stats;
  984. }
  985. }
  986. /* statistics are now up to date */
  987. return &adapter->stats;
  988. }
  989. static void netdev_get_drvinfo(struct net_device *dev,
  990. struct ethtool_drvinfo *info)
  991. {
  992. strcpy(info->driver, DRV_NAME);
  993. strcpy(info->version, DRV_VERSION);
  994. sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
  995. }
  996. static u32 netdev_get_msglevel(struct net_device *dev)
  997. {
  998. return debug;
  999. }
  1000. static void netdev_set_msglevel(struct net_device *dev, u32 level)
  1001. {
  1002. debug = level;
  1003. }
  1004. static const struct ethtool_ops netdev_ethtool_ops = {
  1005. .get_drvinfo = netdev_get_drvinfo,
  1006. .get_msglevel = netdev_get_msglevel,
  1007. .set_msglevel = netdev_set_msglevel,
  1008. };
  1009. /******************************************************
  1010. *
  1011. * close the board
  1012. *
  1013. ******************************************************/
  1014. static int elp_close(struct net_device *dev)
  1015. {
  1016. elp_device *adapter;
  1017. adapter = dev->priv;
  1018. if (elp_debug >= 3)
  1019. printk(KERN_DEBUG "%s: request to close device\n", dev->name);
  1020. netif_stop_queue(dev);
  1021. /* Someone may request the device statistic information even when
  1022. * the interface is closed. The following will update the statistics
  1023. * structure in the driver, so we'll be able to give current statistics.
  1024. */
  1025. (void) elp_get_stats(dev);
  1026. /*
  1027. * disable interrupts on the board
  1028. */
  1029. outb_control(0, dev);
  1030. /*
  1031. * release the IRQ
  1032. */
  1033. free_irq(dev->irq, dev);
  1034. free_dma(dev->dma);
  1035. free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
  1036. return 0;
  1037. }
  1038. /************************************************************
  1039. *
  1040. * Set multicast list
  1041. * num_addrs==0: clear mc_list
  1042. * num_addrs==-1: set promiscuous mode
  1043. * num_addrs>0: set mc_list
  1044. *
  1045. ************************************************************/
  1046. static void elp_set_mc_list(struct net_device *dev)
  1047. {
  1048. elp_device *adapter = (elp_device *) dev->priv;
  1049. struct dev_mc_list *dmi = dev->mc_list;
  1050. int i;
  1051. unsigned long flags;
  1052. if (elp_debug >= 3)
  1053. printk(KERN_DEBUG "%s: request to set multicast list\n", dev->name);
  1054. spin_lock_irqsave(&adapter->lock, flags);
  1055. if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
  1056. /* send a "load multicast list" command to the board, max 10 addrs/cmd */
  1057. /* if num_addrs==0 the list will be cleared */
  1058. adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
  1059. adapter->tx_pcb.length = 6 * dev->mc_count;
  1060. for (i = 0; i < dev->mc_count; i++) {
  1061. memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
  1062. dmi = dmi->next;
  1063. }
  1064. adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
  1065. if (!send_pcb(dev, &adapter->tx_pcb))
  1066. printk(KERN_ERR "%s: couldn't send set_multicast command\n", dev->name);
  1067. else {
  1068. unsigned long timeout = jiffies + TIMEOUT;
  1069. while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
  1070. if (time_after_eq(jiffies, timeout)) {
  1071. TIMEOUT_MSG(__LINE__);
  1072. }
  1073. }
  1074. if (dev->mc_count)
  1075. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
  1076. else /* num_addrs == 0 */
  1077. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
  1078. } else
  1079. adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
  1080. /*
  1081. * configure adapter to receive messages (as specified above)
  1082. * and wait for response
  1083. */
  1084. if (elp_debug >= 3)
  1085. printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
  1086. adapter->tx_pcb.command = CMD_CONFIGURE_82586;
  1087. adapter->tx_pcb.length = 2;
  1088. adapter->got[CMD_CONFIGURE_82586] = 0;
  1089. if (!send_pcb(dev, &adapter->tx_pcb))
  1090. {
  1091. spin_unlock_irqrestore(&adapter->lock, flags);
  1092. printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
  1093. }
  1094. else {
  1095. unsigned long timeout = jiffies + TIMEOUT;
  1096. spin_unlock_irqrestore(&adapter->lock, flags);
  1097. while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
  1098. if (time_after_eq(jiffies, timeout))
  1099. TIMEOUT_MSG(__LINE__);
  1100. }
  1101. }
  1102. /************************************************************
  1103. *
  1104. * A couple of tests to see if there's 3C505 or not
  1105. * Called only by elp_autodetect
  1106. ************************************************************/
  1107. static int __init elp_sense(struct net_device *dev)
  1108. {
  1109. int addr = dev->base_addr;
  1110. const char *name = dev->name;
  1111. byte orig_HSR;
  1112. if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
  1113. return -ENODEV;
  1114. orig_HSR = inb_status(addr);
  1115. if (elp_debug > 0)
  1116. printk(search_msg, name, addr);
  1117. if (orig_HSR == 0xff) {
  1118. if (elp_debug > 0)
  1119. printk(notfound_msg, 1);
  1120. goto out;
  1121. }
  1122. /* Wait for a while; the adapter may still be booting up */
  1123. if (elp_debug > 0)
  1124. printk(stilllooking_msg);
  1125. if (orig_HSR & DIR) {
  1126. /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
  1127. outb(0, dev->base_addr + PORT_CONTROL);
  1128. msleep(300);
  1129. if (inb_status(addr) & DIR) {
  1130. if (elp_debug > 0)
  1131. printk(notfound_msg, 2);
  1132. goto out;
  1133. }
  1134. } else {
  1135. /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
  1136. outb(DIR, dev->base_addr + PORT_CONTROL);
  1137. msleep(300);
  1138. if (!(inb_status(addr) & DIR)) {
  1139. if (elp_debug > 0)
  1140. printk(notfound_msg, 3);
  1141. goto out;
  1142. }
  1143. }
  1144. /*
  1145. * It certainly looks like a 3c505.
  1146. */
  1147. if (elp_debug > 0)
  1148. printk(found_msg);
  1149. return 0;
  1150. out:
  1151. release_region(addr, ELP_IO_EXTENT);
  1152. return -ENODEV;
  1153. }
  1154. /*************************************************************
  1155. *
  1156. * Search through addr_list[] and try to find a 3C505
  1157. * Called only by eplus_probe
  1158. *************************************************************/
  1159. static int __init elp_autodetect(struct net_device *dev)
  1160. {
  1161. int idx = 0;
  1162. /* if base address set, then only check that address
  1163. otherwise, run through the table */
  1164. if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
  1165. if (elp_sense(dev) == 0)
  1166. return dev->base_addr;
  1167. } else
  1168. while ((dev->base_addr = addr_list[idx++])) {
  1169. if (elp_sense(dev) == 0)
  1170. return dev->base_addr;
  1171. }
  1172. /* could not find an adapter */
  1173. if (elp_debug > 0)
  1174. printk(couldnot_msg, dev->name);
  1175. return 0; /* Because of this, the layer above will return -ENODEV */
  1176. }
  1177. /******************************************************
  1178. *
  1179. * probe for an Etherlink Plus board at the specified address
  1180. *
  1181. ******************************************************/
  1182. /* There are three situations we need to be able to detect here:
  1183. * a) the card is idle
  1184. * b) the card is still booting up
  1185. * c) the card is stuck in a strange state (some DOS drivers do this)
  1186. *
  1187. * In case (a), all is well. In case (b), we wait 10 seconds to see if the
  1188. * card finishes booting, and carry on if so. In case (c), we do a hard reset,
  1189. * loop round, and hope for the best.
  1190. *
  1191. * This is all very unpleasant, but hopefully avoids the problems with the old
  1192. * probe code (which had a 15-second delay if the card was idle, and didn't
  1193. * work at all if it was in a weird state).
  1194. */
  1195. static int __init elplus_setup(struct net_device *dev)
  1196. {
  1197. elp_device *adapter = dev->priv;
  1198. int i, tries, tries1, okay;
  1199. unsigned long timeout;
  1200. unsigned long cookie = 0;
  1201. int err = -ENODEV;
  1202. SET_MODULE_OWNER(dev);
  1203. /*
  1204. * setup adapter structure
  1205. */
  1206. dev->base_addr = elp_autodetect(dev);
  1207. if (!dev->base_addr)
  1208. return -ENODEV;
  1209. adapter->send_pcb_semaphore = 0;
  1210. for (tries1 = 0; tries1 < 3; tries1++) {
  1211. outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
  1212. /* First try to write just one byte, to see if the card is
  1213. * responding at all normally.
  1214. */
  1215. timeout = jiffies + 5*HZ/100;
  1216. okay = 0;
  1217. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1218. if ((inb_status(dev->base_addr) & HCRE)) {
  1219. outb_command(0, dev->base_addr); /* send a spurious byte */
  1220. timeout = jiffies + 5*HZ/100;
  1221. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1222. if (inb_status(dev->base_addr) & HCRE)
  1223. okay = 1;
  1224. }
  1225. if (!okay) {
  1226. /* Nope, it's ignoring the command register. This means that
  1227. * either it's still booting up, or it's died.
  1228. */
  1229. printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
  1230. if ((inb_status(dev->base_addr) & 7) == 3) {
  1231. /* If the adapter status is 3, it *could* still be booting.
  1232. * Give it the benefit of the doubt for 10 seconds.
  1233. */
  1234. printk("assuming 3c505 still starting\n");
  1235. timeout = jiffies + 10*HZ;
  1236. while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
  1237. if (inb_status(dev->base_addr) & 7) {
  1238. printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
  1239. } else {
  1240. okay = 1; /* It started */
  1241. }
  1242. } else {
  1243. /* Otherwise, it must just be in a strange
  1244. * state. We probably need to kick it.
  1245. */
  1246. printk("3c505 is sulking\n");
  1247. }
  1248. }
  1249. for (tries = 0; tries < 5 && okay; tries++) {
  1250. /*
  1251. * Try to set the Ethernet address, to make sure that the board
  1252. * is working.
  1253. */
  1254. adapter->tx_pcb.command = CMD_STATION_ADDRESS;
  1255. adapter->tx_pcb.length = 0;
  1256. cookie = probe_irq_on();
  1257. if (!send_pcb(dev, &adapter->tx_pcb)) {
  1258. printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
  1259. probe_irq_off(cookie);
  1260. continue;
  1261. }
  1262. if (!receive_pcb(dev, &adapter->rx_pcb)) {
  1263. printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
  1264. probe_irq_off(cookie);
  1265. continue;
  1266. }
  1267. if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
  1268. (adapter->rx_pcb.length != 6)) {
  1269. printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
  1270. probe_irq_off(cookie);
  1271. continue;
  1272. }
  1273. goto okay;
  1274. }
  1275. /* It's broken. Do a hard reset to re-initialise the board,
  1276. * and try again.
  1277. */
  1278. printk(KERN_INFO "%s: resetting adapter\n", dev->name);
  1279. outb_control(adapter->hcr_val | FLSH | ATTN, dev);
  1280. outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
  1281. }
  1282. printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
  1283. goto out;
  1284. okay:
  1285. if (dev->irq) { /* Is there a preset IRQ? */
  1286. int rpt = probe_irq_off(cookie);
  1287. if (dev->irq != rpt) {
  1288. printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
  1289. }
  1290. /* if dev->irq == probe_irq_off(cookie), all is well */
  1291. } else /* No preset IRQ; just use what we can detect */
  1292. dev->irq = probe_irq_off(cookie);
  1293. switch (dev->irq) { /* Legal, sane? */
  1294. case 0:
  1295. printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
  1296. dev->name);
  1297. goto out;
  1298. case 1:
  1299. case 6:
  1300. case 8:
  1301. case 13:
  1302. printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
  1303. dev->name, dev->irq);
  1304. goto out;
  1305. }
  1306. /*
  1307. * Now we have the IRQ number so we can disable the interrupts from
  1308. * the board until the board is opened.
  1309. */
  1310. outb_control(adapter->hcr_val & ~CMDE, dev);
  1311. /*
  1312. * copy Ethernet address into structure
  1313. */
  1314. for (i = 0; i < 6; i++)
  1315. dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
  1316. /* find a DMA channel */
  1317. if (!dev->dma) {
  1318. if (dev->mem_start) {
  1319. dev->dma = dev->mem_start & 7;
  1320. }
  1321. else {
  1322. printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
  1323. dev->dma = ELP_DMA;
  1324. }
  1325. }
  1326. /*
  1327. * print remainder of startup message
  1328. */
  1329. printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, ",
  1330. dev->name, dev->base_addr, dev->irq, dev->dma);
  1331. printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
  1332. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  1333. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  1334. /*
  1335. * read more information from the adapter
  1336. */
  1337. adapter->tx_pcb.command = CMD_ADAPTER_INFO;
  1338. adapter->tx_pcb.length = 0;
  1339. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1340. !receive_pcb(dev, &adapter->rx_pcb) ||
  1341. (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
  1342. (adapter->rx_pcb.length != 10)) {
  1343. printk("not responding to second PCB\n");
  1344. }
  1345. 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);
  1346. /*
  1347. * reconfigure the adapter memory to better suit our purposes
  1348. */
  1349. adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
  1350. adapter->tx_pcb.length = 12;
  1351. adapter->tx_pcb.data.memconf.cmd_q = 8;
  1352. adapter->tx_pcb.data.memconf.rcv_q = 8;
  1353. adapter->tx_pcb.data.memconf.mcast = 10;
  1354. adapter->tx_pcb.data.memconf.frame = 10;
  1355. adapter->tx_pcb.data.memconf.rcv_b = 10;
  1356. adapter->tx_pcb.data.memconf.progs = 0;
  1357. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1358. !receive_pcb(dev, &adapter->rx_pcb) ||
  1359. (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
  1360. (adapter->rx_pcb.length != 2)) {
  1361. printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
  1362. }
  1363. if (adapter->rx_pcb.data.configure) {
  1364. printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
  1365. }
  1366. dev->open = elp_open; /* local */
  1367. dev->stop = elp_close; /* local */
  1368. dev->get_stats = elp_get_stats; /* local */
  1369. dev->hard_start_xmit = elp_start_xmit; /* local */
  1370. dev->tx_timeout = elp_timeout; /* local */
  1371. dev->watchdog_timeo = 10*HZ;
  1372. dev->set_multicast_list = elp_set_mc_list; /* local */
  1373. dev->ethtool_ops = &netdev_ethtool_ops; /* local */
  1374. memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
  1375. dev->mem_start = dev->mem_end = 0;
  1376. err = register_netdev(dev);
  1377. if (err)
  1378. goto out;
  1379. return 0;
  1380. out:
  1381. release_region(dev->base_addr, ELP_IO_EXTENT);
  1382. return err;
  1383. }
  1384. #ifndef MODULE
  1385. struct net_device * __init elplus_probe(int unit)
  1386. {
  1387. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1388. int err;
  1389. if (!dev)
  1390. return ERR_PTR(-ENOMEM);
  1391. sprintf(dev->name, "eth%d", unit);
  1392. netdev_boot_setup_check(dev);
  1393. err = elplus_setup(dev);
  1394. if (err) {
  1395. free_netdev(dev);
  1396. return ERR_PTR(err);
  1397. }
  1398. return dev;
  1399. }
  1400. #else
  1401. static struct net_device *dev_3c505[ELP_MAX_CARDS];
  1402. static int io[ELP_MAX_CARDS];
  1403. static int irq[ELP_MAX_CARDS];
  1404. static int dma[ELP_MAX_CARDS];
  1405. module_param_array(io, int, NULL, 0);
  1406. module_param_array(irq, int, NULL, 0);
  1407. module_param_array(dma, int, NULL, 0);
  1408. MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
  1409. MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
  1410. MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
  1411. int __init init_module(void)
  1412. {
  1413. int this_dev, found = 0;
  1414. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1415. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1416. if (!dev)
  1417. break;
  1418. dev->irq = irq[this_dev];
  1419. dev->base_addr = io[this_dev];
  1420. if (dma[this_dev]) {
  1421. dev->dma = dma[this_dev];
  1422. } else {
  1423. dev->dma = ELP_DMA;
  1424. printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
  1425. }
  1426. if (io[this_dev] == 0) {
  1427. if (this_dev) {
  1428. free_netdev(dev);
  1429. break;
  1430. }
  1431. printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
  1432. }
  1433. if (elplus_setup(dev) != 0) {
  1434. printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
  1435. free_netdev(dev);
  1436. break;
  1437. }
  1438. dev_3c505[this_dev] = dev;
  1439. found++;
  1440. }
  1441. if (!found)
  1442. return -ENODEV;
  1443. return 0;
  1444. }
  1445. void __exit cleanup_module(void)
  1446. {
  1447. int this_dev;
  1448. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1449. struct net_device *dev = dev_3c505[this_dev];
  1450. if (dev) {
  1451. unregister_netdev(dev);
  1452. release_region(dev->base_addr, ELP_IO_EXTENT);
  1453. free_netdev(dev);
  1454. }
  1455. }
  1456. }
  1457. #endif /* MODULE */
  1458. MODULE_LICENSE("GPL");