3c505.c 47 KB

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