3c505.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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. inline static 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, struct pt_regs *reg_ptr)
  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 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. set_current_state(TASK_UNINTERRUPTIBLE);
  1130. schedule_timeout(30*HZ/100);
  1131. if (inb_status(addr) & DIR) {
  1132. if (elp_debug > 0)
  1133. printk(notfound_msg, 2);
  1134. goto out;
  1135. }
  1136. } else {
  1137. /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
  1138. outb(DIR, dev->base_addr + PORT_CONTROL);
  1139. set_current_state(TASK_UNINTERRUPTIBLE);
  1140. schedule_timeout(30*HZ/100);
  1141. if (!(inb_status(addr) & DIR)) {
  1142. if (elp_debug > 0)
  1143. printk(notfound_msg, 3);
  1144. goto out;
  1145. }
  1146. }
  1147. /*
  1148. * It certainly looks like a 3c505.
  1149. */
  1150. if (elp_debug > 0)
  1151. printk(found_msg);
  1152. return 0;
  1153. out:
  1154. release_region(addr, ELP_IO_EXTENT);
  1155. return -ENODEV;
  1156. }
  1157. /*************************************************************
  1158. *
  1159. * Search through addr_list[] and try to find a 3C505
  1160. * Called only by eplus_probe
  1161. *************************************************************/
  1162. static int __init elp_autodetect(struct net_device *dev)
  1163. {
  1164. int idx = 0;
  1165. /* if base address set, then only check that address
  1166. otherwise, run through the table */
  1167. if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
  1168. if (elp_sense(dev) == 0)
  1169. return dev->base_addr;
  1170. } else
  1171. while ((dev->base_addr = addr_list[idx++])) {
  1172. if (elp_sense(dev) == 0)
  1173. return dev->base_addr;
  1174. }
  1175. /* could not find an adapter */
  1176. if (elp_debug > 0)
  1177. printk(couldnot_msg, dev->name);
  1178. return 0; /* Because of this, the layer above will return -ENODEV */
  1179. }
  1180. /******************************************************
  1181. *
  1182. * probe for an Etherlink Plus board at the specified address
  1183. *
  1184. ******************************************************/
  1185. /* There are three situations we need to be able to detect here:
  1186. * a) the card is idle
  1187. * b) the card is still booting up
  1188. * c) the card is stuck in a strange state (some DOS drivers do this)
  1189. *
  1190. * In case (a), all is well. In case (b), we wait 10 seconds to see if the
  1191. * card finishes booting, and carry on if so. In case (c), we do a hard reset,
  1192. * loop round, and hope for the best.
  1193. *
  1194. * This is all very unpleasant, but hopefully avoids the problems with the old
  1195. * probe code (which had a 15-second delay if the card was idle, and didn't
  1196. * work at all if it was in a weird state).
  1197. */
  1198. static int __init elplus_setup(struct net_device *dev)
  1199. {
  1200. elp_device *adapter = dev->priv;
  1201. int i, tries, tries1, okay;
  1202. unsigned long timeout;
  1203. unsigned long cookie = 0;
  1204. int err = -ENODEV;
  1205. SET_MODULE_OWNER(dev);
  1206. /*
  1207. * setup adapter structure
  1208. */
  1209. dev->base_addr = elp_autodetect(dev);
  1210. if (!dev->base_addr)
  1211. return -ENODEV;
  1212. adapter->send_pcb_semaphore = 0;
  1213. for (tries1 = 0; tries1 < 3; tries1++) {
  1214. outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
  1215. /* First try to write just one byte, to see if the card is
  1216. * responding at all normally.
  1217. */
  1218. timeout = jiffies + 5*HZ/100;
  1219. okay = 0;
  1220. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1221. if ((inb_status(dev->base_addr) & HCRE)) {
  1222. outb_command(0, dev->base_addr); /* send a spurious byte */
  1223. timeout = jiffies + 5*HZ/100;
  1224. while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
  1225. if (inb_status(dev->base_addr) & HCRE)
  1226. okay = 1;
  1227. }
  1228. if (!okay) {
  1229. /* Nope, it's ignoring the command register. This means that
  1230. * either it's still booting up, or it's died.
  1231. */
  1232. printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
  1233. if ((inb_status(dev->base_addr) & 7) == 3) {
  1234. /* If the adapter status is 3, it *could* still be booting.
  1235. * Give it the benefit of the doubt for 10 seconds.
  1236. */
  1237. printk("assuming 3c505 still starting\n");
  1238. timeout = jiffies + 10*HZ;
  1239. while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
  1240. if (inb_status(dev->base_addr) & 7) {
  1241. printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
  1242. } else {
  1243. okay = 1; /* It started */
  1244. }
  1245. } else {
  1246. /* Otherwise, it must just be in a strange
  1247. * state. We probably need to kick it.
  1248. */
  1249. printk("3c505 is sulking\n");
  1250. }
  1251. }
  1252. for (tries = 0; tries < 5 && okay; tries++) {
  1253. /*
  1254. * Try to set the Ethernet address, to make sure that the board
  1255. * is working.
  1256. */
  1257. adapter->tx_pcb.command = CMD_STATION_ADDRESS;
  1258. adapter->tx_pcb.length = 0;
  1259. cookie = probe_irq_on();
  1260. if (!send_pcb(dev, &adapter->tx_pcb)) {
  1261. printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
  1262. probe_irq_off(cookie);
  1263. continue;
  1264. }
  1265. if (!receive_pcb(dev, &adapter->rx_pcb)) {
  1266. printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
  1267. probe_irq_off(cookie);
  1268. continue;
  1269. }
  1270. if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
  1271. (adapter->rx_pcb.length != 6)) {
  1272. printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
  1273. probe_irq_off(cookie);
  1274. continue;
  1275. }
  1276. goto okay;
  1277. }
  1278. /* It's broken. Do a hard reset to re-initialise the board,
  1279. * and try again.
  1280. */
  1281. printk(KERN_INFO "%s: resetting adapter\n", dev->name);
  1282. outb_control(adapter->hcr_val | FLSH | ATTN, dev);
  1283. outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
  1284. }
  1285. printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
  1286. goto out;
  1287. okay:
  1288. if (dev->irq) { /* Is there a preset IRQ? */
  1289. int rpt = probe_irq_off(cookie);
  1290. if (dev->irq != rpt) {
  1291. printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
  1292. }
  1293. /* if dev->irq == probe_irq_off(cookie), all is well */
  1294. } else /* No preset IRQ; just use what we can detect */
  1295. dev->irq = probe_irq_off(cookie);
  1296. switch (dev->irq) { /* Legal, sane? */
  1297. case 0:
  1298. printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
  1299. dev->name);
  1300. goto out;
  1301. case 1:
  1302. case 6:
  1303. case 8:
  1304. case 13:
  1305. printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
  1306. dev->name, dev->irq);
  1307. goto out;
  1308. }
  1309. /*
  1310. * Now we have the IRQ number so we can disable the interrupts from
  1311. * the board until the board is opened.
  1312. */
  1313. outb_control(adapter->hcr_val & ~CMDE, dev);
  1314. /*
  1315. * copy Ethernet address into structure
  1316. */
  1317. for (i = 0; i < 6; i++)
  1318. dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
  1319. /* find a DMA channel */
  1320. if (!dev->dma) {
  1321. if (dev->mem_start) {
  1322. dev->dma = dev->mem_start & 7;
  1323. }
  1324. else {
  1325. printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
  1326. dev->dma = ELP_DMA;
  1327. }
  1328. }
  1329. /*
  1330. * print remainder of startup message
  1331. */
  1332. printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, ",
  1333. dev->name, dev->base_addr, dev->irq, dev->dma);
  1334. printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
  1335. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  1336. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
  1337. /*
  1338. * read more information from the adapter
  1339. */
  1340. adapter->tx_pcb.command = CMD_ADAPTER_INFO;
  1341. adapter->tx_pcb.length = 0;
  1342. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1343. !receive_pcb(dev, &adapter->rx_pcb) ||
  1344. (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
  1345. (adapter->rx_pcb.length != 10)) {
  1346. printk("not responding to second PCB\n");
  1347. }
  1348. 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);
  1349. /*
  1350. * reconfigure the adapter memory to better suit our purposes
  1351. */
  1352. adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
  1353. adapter->tx_pcb.length = 12;
  1354. adapter->tx_pcb.data.memconf.cmd_q = 8;
  1355. adapter->tx_pcb.data.memconf.rcv_q = 8;
  1356. adapter->tx_pcb.data.memconf.mcast = 10;
  1357. adapter->tx_pcb.data.memconf.frame = 10;
  1358. adapter->tx_pcb.data.memconf.rcv_b = 10;
  1359. adapter->tx_pcb.data.memconf.progs = 0;
  1360. if (!send_pcb(dev, &adapter->tx_pcb) ||
  1361. !receive_pcb(dev, &adapter->rx_pcb) ||
  1362. (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
  1363. (adapter->rx_pcb.length != 2)) {
  1364. printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
  1365. }
  1366. if (adapter->rx_pcb.data.configure) {
  1367. printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
  1368. }
  1369. dev->open = elp_open; /* local */
  1370. dev->stop = elp_close; /* local */
  1371. dev->get_stats = elp_get_stats; /* local */
  1372. dev->hard_start_xmit = elp_start_xmit; /* local */
  1373. dev->tx_timeout = elp_timeout; /* local */
  1374. dev->watchdog_timeo = 10*HZ;
  1375. dev->set_multicast_list = elp_set_mc_list; /* local */
  1376. dev->ethtool_ops = &netdev_ethtool_ops; /* local */
  1377. memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
  1378. dev->mem_start = dev->mem_end = 0;
  1379. err = register_netdev(dev);
  1380. if (err)
  1381. goto out;
  1382. return 0;
  1383. out:
  1384. release_region(dev->base_addr, ELP_IO_EXTENT);
  1385. return err;
  1386. }
  1387. #ifndef MODULE
  1388. struct net_device * __init elplus_probe(int unit)
  1389. {
  1390. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1391. int err;
  1392. if (!dev)
  1393. return ERR_PTR(-ENOMEM);
  1394. sprintf(dev->name, "eth%d", unit);
  1395. netdev_boot_setup_check(dev);
  1396. err = elplus_setup(dev);
  1397. if (err) {
  1398. free_netdev(dev);
  1399. return ERR_PTR(err);
  1400. }
  1401. return dev;
  1402. }
  1403. #else
  1404. static struct net_device *dev_3c505[ELP_MAX_CARDS];
  1405. static int io[ELP_MAX_CARDS];
  1406. static int irq[ELP_MAX_CARDS];
  1407. static int dma[ELP_MAX_CARDS];
  1408. module_param_array(io, int, NULL, 0);
  1409. module_param_array(irq, int, NULL, 0);
  1410. module_param_array(dma, int, NULL, 0);
  1411. MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
  1412. MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
  1413. MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
  1414. int init_module(void)
  1415. {
  1416. int this_dev, found = 0;
  1417. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1418. struct net_device *dev = alloc_etherdev(sizeof(elp_device));
  1419. if (!dev)
  1420. break;
  1421. dev->irq = irq[this_dev];
  1422. dev->base_addr = io[this_dev];
  1423. if (dma[this_dev]) {
  1424. dev->dma = dma[this_dev];
  1425. } else {
  1426. dev->dma = ELP_DMA;
  1427. printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
  1428. }
  1429. if (io[this_dev] == 0) {
  1430. if (this_dev) {
  1431. free_netdev(dev);
  1432. break;
  1433. }
  1434. printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
  1435. }
  1436. if (elplus_setup(dev) != 0) {
  1437. printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
  1438. free_netdev(dev);
  1439. break;
  1440. }
  1441. dev_3c505[this_dev] = dev;
  1442. found++;
  1443. }
  1444. if (!found)
  1445. return -ENODEV;
  1446. return 0;
  1447. }
  1448. void cleanup_module(void)
  1449. {
  1450. int this_dev;
  1451. for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
  1452. struct net_device *dev = dev_3c505[this_dev];
  1453. if (dev) {
  1454. unregister_netdev(dev);
  1455. release_region(dev->base_addr, ELP_IO_EXTENT);
  1456. free_netdev(dev);
  1457. }
  1458. }
  1459. }
  1460. #endif /* MODULE */
  1461. MODULE_LICENSE("GPL");