3c505.c 47 KB

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