3c505.c 47 KB

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