3c505.c 48 KB

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