3c505.c 48 KB

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