3c527.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /* 3c527.c: 3Com Etherlink/MC32 driver for Linux 2.4 and 2.6.
  2. *
  3. * (c) Copyright 1998 Red Hat Software Inc
  4. * Written by Alan Cox.
  5. * Further debugging by Carl Drougge.
  6. * Initial SMP support by Felipe W Damasio <felipewd@terra.com.br>
  7. * Heavily modified by Richard Procter <rnp@paradise.net.nz>
  8. *
  9. * Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
  10. * (for the MCA stuff) written by Wim Dumon.
  11. *
  12. * Thanks to 3Com for making this possible by providing me with the
  13. * documentation.
  14. *
  15. * This software may be used and distributed according to the terms
  16. * of the GNU General Public License, incorporated herein by reference.
  17. *
  18. */
  19. #define DRV_NAME "3c527"
  20. #define DRV_VERSION "0.7-SMP"
  21. #define DRV_RELDATE "2003/09/21"
  22. static const char *version =
  23. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Richard Procter <rnp@paradise.net.nz>\n";
  24. /**
  25. * DOC: Traps for the unwary
  26. *
  27. * The diagram (Figure 1-1) and the POS summary disagree with the
  28. * "Interrupt Level" section in the manual.
  29. *
  30. * The manual contradicts itself when describing the minimum number
  31. * buffers in the 'configure lists' command.
  32. * My card accepts a buffer config of 4/4.
  33. *
  34. * Setting the SAV BP bit does not save bad packets, but
  35. * only enables RX on-card stats collection.
  36. *
  37. * The documentation in places seems to miss things. In actual fact
  38. * I've always eventually found everything is documented, it just
  39. * requires careful study.
  40. *
  41. * DOC: Theory Of Operation
  42. *
  43. * The 3com 3c527 is a 32bit MCA bus mastering adapter with a large
  44. * amount of on board intelligence that housekeeps a somewhat dumber
  45. * Intel NIC. For performance we want to keep the transmit queue deep
  46. * as the card can transmit packets while fetching others from main
  47. * memory by bus master DMA. Transmission and reception are driven by
  48. * circular buffer queues.
  49. *
  50. * The mailboxes can be used for controlling how the card traverses
  51. * its buffer rings, but are used only for initial setup in this
  52. * implementation. The exec mailbox allows a variety of commands to
  53. * be executed. Each command must complete before the next is
  54. * executed. Primarily we use the exec mailbox for controlling the
  55. * multicast lists. We have to do a certain amount of interesting
  56. * hoop jumping as the multicast list changes can occur in interrupt
  57. * state when the card has an exec command pending. We defer such
  58. * events until the command completion interrupt.
  59. *
  60. * A copy break scheme (taken from 3c59x.c) is employed whereby
  61. * received frames exceeding a configurable length are passed
  62. * directly to the higher networking layers without incuring a copy,
  63. * in what amounts to a time/space trade-off.
  64. *
  65. * The card also keeps a large amount of statistical information
  66. * on-board. In a perfect world, these could be used safely at no
  67. * cost. However, lacking information to the contrary, processing
  68. * them without races would involve so much extra complexity as to
  69. * make it unworthwhile to do so. In the end, a hybrid SW/HW
  70. * implementation was made necessary --- see mc32_update_stats().
  71. *
  72. * DOC: Notes
  73. *
  74. * It should be possible to use two or more cards, but at this stage
  75. * only by loading two copies of the same module.
  76. *
  77. * The on-board 82586 NIC has trouble receiving multiple
  78. * back-to-back frames and so is likely to drop packets from fast
  79. * senders.
  80. **/
  81. #include <linux/module.h>
  82. #include <linux/errno.h>
  83. #include <linux/netdevice.h>
  84. #include <linux/etherdevice.h>
  85. #include <linux/if_ether.h>
  86. #include <linux/init.h>
  87. #include <linux/kernel.h>
  88. #include <linux/types.h>
  89. #include <linux/fcntl.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/mca-legacy.h>
  92. #include <linux/ioport.h>
  93. #include <linux/in.h>
  94. #include <linux/skbuff.h>
  95. #include <linux/slab.h>
  96. #include <linux/string.h>
  97. #include <linux/wait.h>
  98. #include <linux/ethtool.h>
  99. #include <linux/completion.h>
  100. #include <linux/bitops.h>
  101. #include <linux/semaphore.h>
  102. #include <asm/uaccess.h>
  103. #include <asm/system.h>
  104. #include <asm/io.h>
  105. #include <asm/dma.h>
  106. #include "3c527.h"
  107. MODULE_LICENSE("GPL");
  108. /*
  109. * The name of the card. Is used for messages and in the requests for
  110. * io regions, irqs and dma channels
  111. */
  112. static const char* cardname = DRV_NAME;
  113. /* use 0 for production, 1 for verification, >2 for debug */
  114. #ifndef NET_DEBUG
  115. #define NET_DEBUG 2
  116. #endif
  117. static unsigned int mc32_debug = NET_DEBUG;
  118. /* The number of low I/O ports used by the ethercard. */
  119. #define MC32_IO_EXTENT 8
  120. /* As implemented, values must be a power-of-2 -- 4/8/16/32 */
  121. #define TX_RING_LEN 32 /* Typically the card supports 37 */
  122. #define RX_RING_LEN 8 /* " " " */
  123. /* Copy break point, see above for details.
  124. * Setting to > 1512 effectively disables this feature. */
  125. #define RX_COPYBREAK 200 /* Value from 3c59x.c */
  126. /* Issue the 82586 workaround command - this is for "busy lans", but
  127. * basically means for all lans now days - has a performance (latency)
  128. * cost, but best set. */
  129. static const int WORKAROUND_82586=1;
  130. /* Pointers to buffers and their on-card records */
  131. struct mc32_ring_desc
  132. {
  133. volatile struct skb_header *p;
  134. struct sk_buff *skb;
  135. };
  136. /* Information that needs to be kept for each board. */
  137. struct mc32_local
  138. {
  139. int slot;
  140. u32 base;
  141. volatile struct mc32_mailbox *rx_box;
  142. volatile struct mc32_mailbox *tx_box;
  143. volatile struct mc32_mailbox *exec_box;
  144. volatile struct mc32_stats *stats; /* Start of on-card statistics */
  145. u16 tx_chain; /* Transmit list start offset */
  146. u16 rx_chain; /* Receive list start offset */
  147. u16 tx_len; /* Transmit list count */
  148. u16 rx_len; /* Receive list count */
  149. u16 xceiver_desired_state; /* HALTED or RUNNING */
  150. u16 cmd_nonblocking; /* Thread is uninterested in command result */
  151. u16 mc_reload_wait; /* A multicast load request is pending */
  152. u32 mc_list_valid; /* True when the mclist is set */
  153. struct mc32_ring_desc tx_ring[TX_RING_LEN]; /* Host Transmit ring */
  154. struct mc32_ring_desc rx_ring[RX_RING_LEN]; /* Host Receive ring */
  155. atomic_t tx_count; /* buffers left */
  156. atomic_t tx_ring_head; /* index to tx en-queue end */
  157. u16 tx_ring_tail; /* index to tx de-queue end */
  158. u16 rx_ring_tail; /* index to rx de-queue end */
  159. struct semaphore cmd_mutex; /* Serialises issuing of execute commands */
  160. struct completion execution_cmd; /* Card has completed an execute command */
  161. struct completion xceiver_cmd; /* Card has completed a tx or rx command */
  162. };
  163. /* The station (ethernet) address prefix, used for a sanity check. */
  164. #define SA_ADDR0 0x02
  165. #define SA_ADDR1 0x60
  166. #define SA_ADDR2 0xAC
  167. struct mca_adapters_t {
  168. unsigned int id;
  169. char *name;
  170. };
  171. static const struct mca_adapters_t mc32_adapters[] = {
  172. { 0x0041, "3COM EtherLink MC/32" },
  173. { 0x8EF5, "IBM High Performance Lan Adapter" },
  174. { 0x0000, NULL }
  175. };
  176. /* Macros for ring index manipulations */
  177. static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
  178. static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
  179. static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
  180. /* Index to functions, as function prototypes. */
  181. static int mc32_probe1(struct net_device *dev, int ioaddr);
  182. static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
  183. static int mc32_open(struct net_device *dev);
  184. static void mc32_timeout(struct net_device *dev);
  185. static netdev_tx_t mc32_send_packet(struct sk_buff *skb,
  186. struct net_device *dev);
  187. static irqreturn_t mc32_interrupt(int irq, void *dev_id);
  188. static int mc32_close(struct net_device *dev);
  189. static struct net_device_stats *mc32_get_stats(struct net_device *dev);
  190. static void mc32_set_multicast_list(struct net_device *dev);
  191. static void mc32_reset_multicast_list(struct net_device *dev);
  192. static const struct ethtool_ops netdev_ethtool_ops;
  193. static void cleanup_card(struct net_device *dev)
  194. {
  195. struct mc32_local *lp = netdev_priv(dev);
  196. unsigned slot = lp->slot;
  197. mca_mark_as_unused(slot);
  198. mca_set_adapter_name(slot, NULL);
  199. free_irq(dev->irq, dev);
  200. release_region(dev->base_addr, MC32_IO_EXTENT);
  201. }
  202. /**
  203. * mc32_probe - Search for supported boards
  204. * @unit: interface number to use
  205. *
  206. * Because MCA bus is a real bus and we can scan for cards we could do a
  207. * single scan for all boards here. Right now we use the passed in device
  208. * structure and scan for only one board. This needs fixing for modules
  209. * in particular.
  210. */
  211. struct net_device *__init mc32_probe(int unit)
  212. {
  213. struct net_device *dev = alloc_etherdev(sizeof(struct mc32_local));
  214. static int current_mca_slot = -1;
  215. int i;
  216. int err;
  217. if (!dev)
  218. return ERR_PTR(-ENOMEM);
  219. if (unit >= 0)
  220. sprintf(dev->name, "eth%d", unit);
  221. /* Do not check any supplied i/o locations.
  222. POS registers usually don't fail :) */
  223. /* MCA cards have POS registers.
  224. Autodetecting MCA cards is extremely simple.
  225. Just search for the card. */
  226. for(i = 0; (mc32_adapters[i].name != NULL); i++) {
  227. current_mca_slot =
  228. mca_find_unused_adapter(mc32_adapters[i].id, 0);
  229. if(current_mca_slot != MCA_NOTFOUND) {
  230. if(!mc32_probe1(dev, current_mca_slot))
  231. {
  232. mca_set_adapter_name(current_mca_slot,
  233. mc32_adapters[i].name);
  234. mca_mark_as_used(current_mca_slot);
  235. err = register_netdev(dev);
  236. if (err) {
  237. cleanup_card(dev);
  238. free_netdev(dev);
  239. dev = ERR_PTR(err);
  240. }
  241. return dev;
  242. }
  243. }
  244. }
  245. free_netdev(dev);
  246. return ERR_PTR(-ENODEV);
  247. }
  248. static const struct net_device_ops netdev_ops = {
  249. .ndo_open = mc32_open,
  250. .ndo_stop = mc32_close,
  251. .ndo_start_xmit = mc32_send_packet,
  252. .ndo_get_stats = mc32_get_stats,
  253. .ndo_set_multicast_list = mc32_set_multicast_list,
  254. .ndo_tx_timeout = mc32_timeout,
  255. .ndo_change_mtu = eth_change_mtu,
  256. .ndo_set_mac_address = eth_mac_addr,
  257. .ndo_validate_addr = eth_validate_addr,
  258. };
  259. /**
  260. * mc32_probe1 - Check a given slot for a board and test the card
  261. * @dev: Device structure to fill in
  262. * @slot: The MCA bus slot being used by this card
  263. *
  264. * Decode the slot data and configure the card structures. Having done this we
  265. * can reset the card and configure it. The card does a full self test cycle
  266. * in firmware so we have to wait for it to return and post us either a
  267. * failure case or some addresses we use to find the board internals.
  268. */
  269. static int __init mc32_probe1(struct net_device *dev, int slot)
  270. {
  271. static unsigned version_printed;
  272. int i, err;
  273. u8 POS;
  274. u32 base;
  275. struct mc32_local *lp = netdev_priv(dev);
  276. static const u16 mca_io_bases[] = {
  277. 0x7280,0x7290,
  278. 0x7680,0x7690,
  279. 0x7A80,0x7A90,
  280. 0x7E80,0x7E90
  281. };
  282. static const u32 mca_mem_bases[] = {
  283. 0x00C0000,
  284. 0x00C4000,
  285. 0x00C8000,
  286. 0x00CC000,
  287. 0x00D0000,
  288. 0x00D4000,
  289. 0x00D8000,
  290. 0x00DC000
  291. };
  292. static const char * const failures[] = {
  293. "Processor instruction",
  294. "Processor data bus",
  295. "Processor data bus",
  296. "Processor data bus",
  297. "Adapter bus",
  298. "ROM checksum",
  299. "Base RAM",
  300. "Extended RAM",
  301. "82586 internal loopback",
  302. "82586 initialisation failure",
  303. "Adapter list configuration error"
  304. };
  305. /* Time to play MCA games */
  306. if (mc32_debug && version_printed++ == 0)
  307. pr_debug("%s", version);
  308. pr_info("%s: %s found in slot %d: ", dev->name, cardname, slot);
  309. POS = mca_read_stored_pos(slot, 2);
  310. if(!(POS&1))
  311. {
  312. pr_cont("disabled.\n");
  313. return -ENODEV;
  314. }
  315. /* Fill in the 'dev' fields. */
  316. dev->base_addr = mca_io_bases[(POS>>1)&7];
  317. dev->mem_start = mca_mem_bases[(POS>>4)&7];
  318. POS = mca_read_stored_pos(slot, 4);
  319. if(!(POS&1))
  320. {
  321. pr_cont("memory window disabled.\n");
  322. return -ENODEV;
  323. }
  324. POS = mca_read_stored_pos(slot, 5);
  325. i=(POS>>4)&3;
  326. if(i==3)
  327. {
  328. pr_cont("invalid memory window.\n");
  329. return -ENODEV;
  330. }
  331. i*=16384;
  332. i+=16384;
  333. dev->mem_end=dev->mem_start + i;
  334. dev->irq = ((POS>>2)&3)+9;
  335. if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
  336. {
  337. pr_cont("io 0x%3lX, which is busy.\n", dev->base_addr);
  338. return -EBUSY;
  339. }
  340. pr_cont("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
  341. dev->base_addr, dev->irq, dev->mem_start, i/1024);
  342. /* We ought to set the cache line size here.. */
  343. /*
  344. * Go PROM browsing
  345. */
  346. /* Retrieve and print the ethernet address. */
  347. for (i = 0; i < 6; i++)
  348. {
  349. mca_write_pos(slot, 6, i+12);
  350. mca_write_pos(slot, 7, 0);
  351. dev->dev_addr[i] = mca_read_pos(slot,3);
  352. }
  353. pr_info("%s: Address %pM ", dev->name, dev->dev_addr);
  354. mca_write_pos(slot, 6, 0);
  355. mca_write_pos(slot, 7, 0);
  356. POS = mca_read_stored_pos(slot, 4);
  357. if(POS&2)
  358. pr_cont(": BNC port selected.\n");
  359. else
  360. pr_cont(": AUI port selected.\n");
  361. POS=inb(dev->base_addr+HOST_CTRL);
  362. POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
  363. POS&=~HOST_CTRL_INTE;
  364. outb(POS, dev->base_addr+HOST_CTRL);
  365. /* Reset adapter */
  366. udelay(100);
  367. /* Reset off */
  368. POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
  369. outb(POS, dev->base_addr+HOST_CTRL);
  370. udelay(300);
  371. /*
  372. * Grab the IRQ
  373. */
  374. err = request_irq(dev->irq, mc32_interrupt, IRQF_SHARED, DRV_NAME, dev);
  375. if (err) {
  376. release_region(dev->base_addr, MC32_IO_EXTENT);
  377. pr_err("%s: unable to get IRQ %d.\n", DRV_NAME, dev->irq);
  378. goto err_exit_ports;
  379. }
  380. memset(lp, 0, sizeof(struct mc32_local));
  381. lp->slot = slot;
  382. i=0;
  383. base = inb(dev->base_addr);
  384. while(base == 0xFF)
  385. {
  386. i++;
  387. if(i == 1000)
  388. {
  389. pr_err("%s: failed to boot adapter.\n", dev->name);
  390. err = -ENODEV;
  391. goto err_exit_irq;
  392. }
  393. udelay(1000);
  394. if(inb(dev->base_addr+2)&(1<<5))
  395. base = inb(dev->base_addr);
  396. }
  397. if(base>0)
  398. {
  399. if(base < 0x0C)
  400. pr_err("%s: %s%s.\n", dev->name, failures[base-1],
  401. base<0x0A?" test failure":"");
  402. else
  403. pr_err("%s: unknown failure %d.\n", dev->name, base);
  404. err = -ENODEV;
  405. goto err_exit_irq;
  406. }
  407. base=0;
  408. for(i=0;i<4;i++)
  409. {
  410. int n=0;
  411. while(!(inb(dev->base_addr+2)&(1<<5)))
  412. {
  413. n++;
  414. udelay(50);
  415. if(n>100)
  416. {
  417. pr_err("%s: mailbox read fail (%d).\n", dev->name, i);
  418. err = -ENODEV;
  419. goto err_exit_irq;
  420. }
  421. }
  422. base|=(inb(dev->base_addr)<<(8*i));
  423. }
  424. lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
  425. base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
  426. lp->base = dev->mem_start+base;
  427. lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]);
  428. lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
  429. lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
  430. /*
  431. * Descriptor chains (card relative)
  432. */
  433. lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
  434. lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
  435. lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
  436. lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
  437. sema_init(&lp->cmd_mutex, 0);
  438. init_completion(&lp->execution_cmd);
  439. init_completion(&lp->xceiver_cmd);
  440. pr_info("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
  441. dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
  442. dev->netdev_ops = &netdev_ops;
  443. dev->watchdog_timeo = HZ*5; /* Board does all the work */
  444. dev->ethtool_ops = &netdev_ethtool_ops;
  445. return 0;
  446. err_exit_irq:
  447. free_irq(dev->irq, dev);
  448. err_exit_ports:
  449. release_region(dev->base_addr, MC32_IO_EXTENT);
  450. return err;
  451. }
  452. /**
  453. * mc32_ready_poll - wait until we can feed it a command
  454. * @dev: The device to wait for
  455. *
  456. * Wait until the card becomes ready to accept a command via the
  457. * command register. This tells us nothing about the completion
  458. * status of any pending commands and takes very little time at all.
  459. */
  460. static inline void mc32_ready_poll(struct net_device *dev)
  461. {
  462. int ioaddr = dev->base_addr;
  463. while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
  464. }
  465. /**
  466. * mc32_command_nowait - send a command non blocking
  467. * @dev: The 3c527 to issue the command to
  468. * @cmd: The command word to write to the mailbox
  469. * @data: A data block if the command expects one
  470. * @len: Length of the data block
  471. *
  472. * Send a command from interrupt state. If there is a command
  473. * currently being executed then we return an error of -1. It
  474. * simply isn't viable to wait around as commands may be
  475. * slow. This can theoretically be starved on SMP, but it's hard
  476. * to see a realistic situation. We do not wait for the command
  477. * to complete --- we rely on the interrupt handler to tidy up
  478. * after us.
  479. */
  480. static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
  481. {
  482. struct mc32_local *lp = netdev_priv(dev);
  483. int ioaddr = dev->base_addr;
  484. int ret = -1;
  485. if (down_trylock(&lp->cmd_mutex) == 0)
  486. {
  487. lp->cmd_nonblocking=1;
  488. lp->exec_box->mbox=0;
  489. lp->exec_box->mbox=cmd;
  490. memcpy((void *)lp->exec_box->data, data, len);
  491. barrier(); /* the memcpy forgot the volatile so be sure */
  492. /* Send the command */
  493. mc32_ready_poll(dev);
  494. outb(1<<6, ioaddr+HOST_CMD);
  495. ret = 0;
  496. /* Interrupt handler will signal mutex on completion */
  497. }
  498. return ret;
  499. }
  500. /**
  501. * mc32_command - send a command and sleep until completion
  502. * @dev: The 3c527 card to issue the command to
  503. * @cmd: The command word to write to the mailbox
  504. * @data: A data block if the command expects one
  505. * @len: Length of the data block
  506. *
  507. * Sends exec commands in a user context. This permits us to wait around
  508. * for the replies and also to wait for the command buffer to complete
  509. * from a previous command before we execute our command. After our
  510. * command completes we will attempt any pending multicast reload
  511. * we blocked off by hogging the exec buffer.
  512. *
  513. * You feed the card a command, you wait, it interrupts you get a
  514. * reply. All well and good. The complication arises because you use
  515. * commands for filter list changes which come in at bh level from things
  516. * like IPV6 group stuff.
  517. */
  518. static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
  519. {
  520. struct mc32_local *lp = netdev_priv(dev);
  521. int ioaddr = dev->base_addr;
  522. int ret = 0;
  523. down(&lp->cmd_mutex);
  524. /*
  525. * My Turn
  526. */
  527. lp->cmd_nonblocking=0;
  528. lp->exec_box->mbox=0;
  529. lp->exec_box->mbox=cmd;
  530. memcpy((void *)lp->exec_box->data, data, len);
  531. barrier(); /* the memcpy forgot the volatile so be sure */
  532. mc32_ready_poll(dev);
  533. outb(1<<6, ioaddr+HOST_CMD);
  534. wait_for_completion(&lp->execution_cmd);
  535. if(lp->exec_box->mbox&(1<<13))
  536. ret = -1;
  537. up(&lp->cmd_mutex);
  538. /*
  539. * A multicast set got blocked - try it now
  540. */
  541. if(lp->mc_reload_wait)
  542. {
  543. mc32_reset_multicast_list(dev);
  544. }
  545. return ret;
  546. }
  547. /**
  548. * mc32_start_transceiver - tell board to restart tx/rx
  549. * @dev: The 3c527 card to issue the command to
  550. *
  551. * This may be called from the interrupt state, where it is used
  552. * to restart the rx ring if the card runs out of rx buffers.
  553. *
  554. * We must first check if it's ok to (re)start the transceiver. See
  555. * mc32_close for details.
  556. */
  557. static void mc32_start_transceiver(struct net_device *dev) {
  558. struct mc32_local *lp = netdev_priv(dev);
  559. int ioaddr = dev->base_addr;
  560. /* Ignore RX overflow on device closure */
  561. if (lp->xceiver_desired_state==HALTED)
  562. return;
  563. /* Give the card the offset to the post-EOL-bit RX descriptor */
  564. mc32_ready_poll(dev);
  565. lp->rx_box->mbox=0;
  566. lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next;
  567. outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);
  568. mc32_ready_poll(dev);
  569. lp->tx_box->mbox=0;
  570. outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD); /* card ignores this on RX restart */
  571. /* We are not interrupted on start completion */
  572. }
  573. /**
  574. * mc32_halt_transceiver - tell board to stop tx/rx
  575. * @dev: The 3c527 card to issue the command to
  576. *
  577. * We issue the commands to halt the card's transceiver. In fact,
  578. * after some experimenting we now simply tell the card to
  579. * suspend. When issuing aborts occasionally odd things happened.
  580. *
  581. * We then sleep until the card has notified us that both rx and
  582. * tx have been suspended.
  583. */
  584. static void mc32_halt_transceiver(struct net_device *dev)
  585. {
  586. struct mc32_local *lp = netdev_priv(dev);
  587. int ioaddr = dev->base_addr;
  588. mc32_ready_poll(dev);
  589. lp->rx_box->mbox=0;
  590. outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);
  591. wait_for_completion(&lp->xceiver_cmd);
  592. mc32_ready_poll(dev);
  593. lp->tx_box->mbox=0;
  594. outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);
  595. wait_for_completion(&lp->xceiver_cmd);
  596. }
  597. /**
  598. * mc32_load_rx_ring - load the ring of receive buffers
  599. * @dev: 3c527 to build the ring for
  600. *
  601. * This initialises the on-card and driver datastructures to
  602. * the point where mc32_start_transceiver() can be called.
  603. *
  604. * The card sets up the receive ring for us. We are required to use the
  605. * ring it provides, although the size of the ring is configurable.
  606. *
  607. * We allocate an sk_buff for each ring entry in turn and
  608. * initialise its house-keeping info. At the same time, we read
  609. * each 'next' pointer in our rx_ring array. This reduces slow
  610. * shared-memory reads and makes it easy to access predecessor
  611. * descriptors.
  612. *
  613. * We then set the end-of-list bit for the last entry so that the
  614. * card will know when it has run out of buffers.
  615. */
  616. static int mc32_load_rx_ring(struct net_device *dev)
  617. {
  618. struct mc32_local *lp = netdev_priv(dev);
  619. int i;
  620. u16 rx_base;
  621. volatile struct skb_header *p;
  622. rx_base=lp->rx_chain;
  623. for(i=0; i<RX_RING_LEN; i++) {
  624. lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
  625. if (lp->rx_ring[i].skb==NULL) {
  626. for (;i>=0;i--)
  627. kfree_skb(lp->rx_ring[i].skb);
  628. return -ENOBUFS;
  629. }
  630. skb_reserve(lp->rx_ring[i].skb, 18);
  631. p=isa_bus_to_virt(lp->base+rx_base);
  632. p->control=0;
  633. p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
  634. p->status=0;
  635. p->length=1532;
  636. lp->rx_ring[i].p=p;
  637. rx_base=p->next;
  638. }
  639. lp->rx_ring[i-1].p->control |= CONTROL_EOL;
  640. lp->rx_ring_tail=0;
  641. return 0;
  642. }
  643. /**
  644. * mc32_flush_rx_ring - free the ring of receive buffers
  645. * @lp: Local data of 3c527 to flush the rx ring of
  646. *
  647. * Free the buffer for each ring slot. This may be called
  648. * before mc32_load_rx_ring(), eg. on error in mc32_open().
  649. * Requires rx skb pointers to point to a valid skb, or NULL.
  650. */
  651. static void mc32_flush_rx_ring(struct net_device *dev)
  652. {
  653. struct mc32_local *lp = netdev_priv(dev);
  654. int i;
  655. for(i=0; i < RX_RING_LEN; i++)
  656. {
  657. if (lp->rx_ring[i].skb) {
  658. dev_kfree_skb(lp->rx_ring[i].skb);
  659. lp->rx_ring[i].skb = NULL;
  660. }
  661. lp->rx_ring[i].p=NULL;
  662. }
  663. }
  664. /**
  665. * mc32_load_tx_ring - load transmit ring
  666. * @dev: The 3c527 card to issue the command to
  667. *
  668. * This sets up the host transmit data-structures.
  669. *
  670. * First, we obtain from the card it's current position in the tx
  671. * ring, so that we will know where to begin transmitting
  672. * packets.
  673. *
  674. * Then, we read the 'next' pointers from the on-card tx ring into
  675. * our tx_ring array to reduce slow shared-mem reads. Finally, we
  676. * intitalise the tx house keeping variables.
  677. *
  678. */
  679. static void mc32_load_tx_ring(struct net_device *dev)
  680. {
  681. struct mc32_local *lp = netdev_priv(dev);
  682. volatile struct skb_header *p;
  683. int i;
  684. u16 tx_base;
  685. tx_base=lp->tx_box->data[0];
  686. for(i=0 ; i<TX_RING_LEN ; i++)
  687. {
  688. p=isa_bus_to_virt(lp->base+tx_base);
  689. lp->tx_ring[i].p=p;
  690. lp->tx_ring[i].skb=NULL;
  691. tx_base=p->next;
  692. }
  693. /* -1 so that tx_ring_head cannot "lap" tx_ring_tail */
  694. /* see mc32_tx_ring */
  695. atomic_set(&lp->tx_count, TX_RING_LEN-1);
  696. atomic_set(&lp->tx_ring_head, 0);
  697. lp->tx_ring_tail=0;
  698. }
  699. /**
  700. * mc32_flush_tx_ring - free transmit ring
  701. * @lp: Local data of 3c527 to flush the tx ring of
  702. *
  703. * If the ring is non-empty, zip over the it, freeing any
  704. * allocated skb_buffs. The tx ring house-keeping variables are
  705. * then reset. Requires rx skb pointers to point to a valid skb,
  706. * or NULL.
  707. */
  708. static void mc32_flush_tx_ring(struct net_device *dev)
  709. {
  710. struct mc32_local *lp = netdev_priv(dev);
  711. int i;
  712. for (i=0; i < TX_RING_LEN; i++)
  713. {
  714. if (lp->tx_ring[i].skb)
  715. {
  716. dev_kfree_skb(lp->tx_ring[i].skb);
  717. lp->tx_ring[i].skb = NULL;
  718. }
  719. }
  720. atomic_set(&lp->tx_count, 0);
  721. atomic_set(&lp->tx_ring_head, 0);
  722. lp->tx_ring_tail=0;
  723. }
  724. /**
  725. * mc32_open - handle 'up' of card
  726. * @dev: device to open
  727. *
  728. * The user is trying to bring the card into ready state. This requires
  729. * a brief dialogue with the card. Firstly we enable interrupts and then
  730. * 'indications'. Without these enabled the card doesn't bother telling
  731. * us what it has done. This had me puzzled for a week.
  732. *
  733. * We configure the number of card descriptors, then load the network
  734. * address and multicast filters. Turn on the workaround mode. This
  735. * works around a bug in the 82586 - it asks the firmware to do
  736. * so. It has a performance (latency) hit but is needed on busy
  737. * [read most] lans. We load the ring with buffers then we kick it
  738. * all off.
  739. */
  740. static int mc32_open(struct net_device *dev)
  741. {
  742. int ioaddr = dev->base_addr;
  743. struct mc32_local *lp = netdev_priv(dev);
  744. u8 one=1;
  745. u8 regs;
  746. u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
  747. /*
  748. * Interrupts enabled
  749. */
  750. regs=inb(ioaddr+HOST_CTRL);
  751. regs|=HOST_CTRL_INTE;
  752. outb(regs, ioaddr+HOST_CTRL);
  753. /*
  754. * Allow ourselves to issue commands
  755. */
  756. up(&lp->cmd_mutex);
  757. /*
  758. * Send the indications on command
  759. */
  760. mc32_command(dev, 4, &one, 2);
  761. /*
  762. * Poke it to make sure it's really dead.
  763. */
  764. mc32_halt_transceiver(dev);
  765. mc32_flush_tx_ring(dev);
  766. /*
  767. * Ask card to set up on-card descriptors to our spec
  768. */
  769. if(mc32_command(dev, 8, descnumbuffs, 4)) {
  770. pr_info("%s: %s rejected our buffer configuration!\n",
  771. dev->name, cardname);
  772. mc32_close(dev);
  773. return -ENOBUFS;
  774. }
  775. /* Report new configuration */
  776. mc32_command(dev, 6, NULL, 0);
  777. lp->tx_chain = lp->exec_box->data[8]; /* Transmit list start offset */
  778. lp->rx_chain = lp->exec_box->data[10]; /* Receive list start offset */
  779. lp->tx_len = lp->exec_box->data[9]; /* Transmit list count */
  780. lp->rx_len = lp->exec_box->data[11]; /* Receive list count */
  781. /* Set Network Address */
  782. mc32_command(dev, 1, dev->dev_addr, 6);
  783. /* Set the filters */
  784. mc32_set_multicast_list(dev);
  785. if (WORKAROUND_82586) {
  786. u16 zero_word=0;
  787. mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
  788. }
  789. mc32_load_tx_ring(dev);
  790. if(mc32_load_rx_ring(dev))
  791. {
  792. mc32_close(dev);
  793. return -ENOBUFS;
  794. }
  795. lp->xceiver_desired_state = RUNNING;
  796. /* And finally, set the ball rolling... */
  797. mc32_start_transceiver(dev);
  798. netif_start_queue(dev);
  799. return 0;
  800. }
  801. /**
  802. * mc32_timeout - handle a timeout from the network layer
  803. * @dev: 3c527 that timed out
  804. *
  805. * Handle a timeout on transmit from the 3c527. This normally means
  806. * bad things as the hardware handles cable timeouts and mess for
  807. * us.
  808. *
  809. */
  810. static void mc32_timeout(struct net_device *dev)
  811. {
  812. pr_warning("%s: transmit timed out?\n", dev->name);
  813. /* Try to restart the adaptor. */
  814. netif_wake_queue(dev);
  815. }
  816. /**
  817. * mc32_send_packet - queue a frame for transmit
  818. * @skb: buffer to transmit
  819. * @dev: 3c527 to send it out of
  820. *
  821. * Transmit a buffer. This normally means throwing the buffer onto
  822. * the transmit queue as the queue is quite large. If the queue is
  823. * full then we set tx_busy and return. Once the interrupt handler
  824. * gets messages telling it to reclaim transmit queue entries, we will
  825. * clear tx_busy and the kernel will start calling this again.
  826. *
  827. * We do not disable interrupts or acquire any locks; this can
  828. * run concurrently with mc32_tx_ring(), and the function itself
  829. * is serialised at a higher layer. However, similarly for the
  830. * card itself, we must ensure that we update tx_ring_head only
  831. * after we've established a valid packet on the tx ring (and
  832. * before we let the card "see" it, to prevent it racing with the
  833. * irq handler).
  834. *
  835. */
  836. static netdev_tx_t mc32_send_packet(struct sk_buff *skb,
  837. struct net_device *dev)
  838. {
  839. struct mc32_local *lp = netdev_priv(dev);
  840. u32 head = atomic_read(&lp->tx_ring_head);
  841. volatile struct skb_header *p, *np;
  842. netif_stop_queue(dev);
  843. if(atomic_read(&lp->tx_count)==0) {
  844. return NETDEV_TX_BUSY;
  845. }
  846. if (skb_padto(skb, ETH_ZLEN)) {
  847. netif_wake_queue(dev);
  848. return NETDEV_TX_OK;
  849. }
  850. atomic_dec(&lp->tx_count);
  851. /* P is the last sending/sent buffer as a pointer */
  852. p=lp->tx_ring[head].p;
  853. head = next_tx(head);
  854. /* NP is the buffer we will be loading */
  855. np=lp->tx_ring[head].p;
  856. /* We will need this to flush the buffer out */
  857. lp->tx_ring[head].skb=skb;
  858. np->length = unlikely(skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
  859. np->data = isa_virt_to_bus(skb->data);
  860. np->status = 0;
  861. np->control = CONTROL_EOP | CONTROL_EOL;
  862. wmb();
  863. /*
  864. * The new frame has been setup; we can now
  865. * let the interrupt handler and card "see" it
  866. */
  867. atomic_set(&lp->tx_ring_head, head);
  868. p->control &= ~CONTROL_EOL;
  869. netif_wake_queue(dev);
  870. return NETDEV_TX_OK;
  871. }
  872. /**
  873. * mc32_update_stats - pull off the on board statistics
  874. * @dev: 3c527 to service
  875. *
  876. *
  877. * Query and reset the on-card stats. There's the small possibility
  878. * of a race here, which would result in an underestimation of
  879. * actual errors. As such, we'd prefer to keep all our stats
  880. * collection in software. As a rule, we do. However it can't be
  881. * used for rx errors and collisions as, by default, the card discards
  882. * bad rx packets.
  883. *
  884. * Setting the SAV BP in the rx filter command supposedly
  885. * stops this behaviour. However, testing shows that it only seems to
  886. * enable the collation of on-card rx statistics --- the driver
  887. * never sees an RX descriptor with an error status set.
  888. *
  889. */
  890. static void mc32_update_stats(struct net_device *dev)
  891. {
  892. struct mc32_local *lp = netdev_priv(dev);
  893. volatile struct mc32_stats *st = lp->stats;
  894. u32 rx_errors=0;
  895. rx_errors+=dev->stats.rx_crc_errors +=st->rx_crc_errors;
  896. st->rx_crc_errors=0;
  897. rx_errors+=dev->stats.rx_fifo_errors +=st->rx_overrun_errors;
  898. st->rx_overrun_errors=0;
  899. rx_errors+=dev->stats.rx_frame_errors +=st->rx_alignment_errors;
  900. st->rx_alignment_errors=0;
  901. rx_errors+=dev->stats.rx_length_errors+=st->rx_tooshort_errors;
  902. st->rx_tooshort_errors=0;
  903. rx_errors+=dev->stats.rx_missed_errors+=st->rx_outofresource_errors;
  904. st->rx_outofresource_errors=0;
  905. dev->stats.rx_errors=rx_errors;
  906. /* Number of packets which saw one collision */
  907. dev->stats.collisions+=st->dataC[10];
  908. st->dataC[10]=0;
  909. /* Number of packets which saw 2--15 collisions */
  910. dev->stats.collisions+=st->dataC[11];
  911. st->dataC[11]=0;
  912. }
  913. /**
  914. * mc32_rx_ring - process the receive ring
  915. * @dev: 3c527 that needs its receive ring processing
  916. *
  917. *
  918. * We have received one or more indications from the card that a
  919. * receive has completed. The buffer ring thus contains dirty
  920. * entries. We walk the ring by iterating over the circular rx_ring
  921. * array, starting at the next dirty buffer (which happens to be the
  922. * one we finished up at last time around).
  923. *
  924. * For each completed packet, we will either copy it and pass it up
  925. * the stack or, if the packet is near MTU sized, we allocate
  926. * another buffer and flip the old one up the stack.
  927. *
  928. * We must succeed in keeping a buffer on the ring. If necessary we
  929. * will toss a received packet rather than lose a ring entry. Once
  930. * the first uncompleted descriptor is found, we move the
  931. * End-Of-List bit to include the buffers just processed.
  932. *
  933. */
  934. static void mc32_rx_ring(struct net_device *dev)
  935. {
  936. struct mc32_local *lp = netdev_priv(dev);
  937. volatile struct skb_header *p;
  938. u16 rx_ring_tail;
  939. u16 rx_old_tail;
  940. int x=0;
  941. rx_old_tail = rx_ring_tail = lp->rx_ring_tail;
  942. do
  943. {
  944. p=lp->rx_ring[rx_ring_tail].p;
  945. if(!(p->status & (1<<7))) { /* Not COMPLETED */
  946. break;
  947. }
  948. if(p->status & (1<<6)) /* COMPLETED_OK */
  949. {
  950. u16 length=p->length;
  951. struct sk_buff *skb;
  952. struct sk_buff *newskb;
  953. /* Try to save time by avoiding a copy on big frames */
  954. if ((length > RX_COPYBREAK) &&
  955. ((newskb=dev_alloc_skb(1532)) != NULL))
  956. {
  957. skb=lp->rx_ring[rx_ring_tail].skb;
  958. skb_put(skb, length);
  959. skb_reserve(newskb,18);
  960. lp->rx_ring[rx_ring_tail].skb=newskb;
  961. p->data=isa_virt_to_bus(newskb->data);
  962. }
  963. else
  964. {
  965. skb=dev_alloc_skb(length+2);
  966. if(skb==NULL) {
  967. dev->stats.rx_dropped++;
  968. goto dropped;
  969. }
  970. skb_reserve(skb,2);
  971. memcpy(skb_put(skb, length),
  972. lp->rx_ring[rx_ring_tail].skb->data, length);
  973. }
  974. skb->protocol=eth_type_trans(skb,dev);
  975. dev->stats.rx_packets++;
  976. dev->stats.rx_bytes += length;
  977. netif_rx(skb);
  978. }
  979. dropped:
  980. p->length = 1532;
  981. p->status = 0;
  982. rx_ring_tail=next_rx(rx_ring_tail);
  983. }
  984. while(x++<48);
  985. /* If there was actually a frame to be processed, place the EOL bit */
  986. /* at the descriptor prior to the one to be filled next */
  987. if (rx_ring_tail != rx_old_tail)
  988. {
  989. lp->rx_ring[prev_rx(rx_ring_tail)].p->control |= CONTROL_EOL;
  990. lp->rx_ring[prev_rx(rx_old_tail)].p->control &= ~CONTROL_EOL;
  991. lp->rx_ring_tail=rx_ring_tail;
  992. }
  993. }
  994. /**
  995. * mc32_tx_ring - process completed transmits
  996. * @dev: 3c527 that needs its transmit ring processing
  997. *
  998. *
  999. * This operates in a similar fashion to mc32_rx_ring. We iterate
  1000. * over the transmit ring. For each descriptor which has been
  1001. * processed by the card, we free its associated buffer and note
  1002. * any errors. This continues until the transmit ring is emptied
  1003. * or we reach a descriptor that hasn't yet been processed by the
  1004. * card.
  1005. *
  1006. */
  1007. static void mc32_tx_ring(struct net_device *dev)
  1008. {
  1009. struct mc32_local *lp = netdev_priv(dev);
  1010. volatile struct skb_header *np;
  1011. /*
  1012. * We rely on head==tail to mean 'queue empty'.
  1013. * This is why lp->tx_count=TX_RING_LEN-1: in order to prevent
  1014. * tx_ring_head wrapping to tail and confusing a 'queue empty'
  1015. * condition with 'queue full'
  1016. */
  1017. while (lp->tx_ring_tail != atomic_read(&lp->tx_ring_head))
  1018. {
  1019. u16 t;
  1020. t=next_tx(lp->tx_ring_tail);
  1021. np=lp->tx_ring[t].p;
  1022. if(!(np->status & (1<<7)))
  1023. {
  1024. /* Not COMPLETED */
  1025. break;
  1026. }
  1027. dev->stats.tx_packets++;
  1028. if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
  1029. {
  1030. dev->stats.tx_errors++;
  1031. switch(np->status&0x0F)
  1032. {
  1033. case 1:
  1034. dev->stats.tx_aborted_errors++;
  1035. break; /* Max collisions */
  1036. case 2:
  1037. dev->stats.tx_fifo_errors++;
  1038. break;
  1039. case 3:
  1040. dev->stats.tx_carrier_errors++;
  1041. break;
  1042. case 4:
  1043. dev->stats.tx_window_errors++;
  1044. break; /* CTS Lost */
  1045. case 5:
  1046. dev->stats.tx_aborted_errors++;
  1047. break; /* Transmit timeout */
  1048. }
  1049. }
  1050. /* Packets are sent in order - this is
  1051. basically a FIFO queue of buffers matching
  1052. the card ring */
  1053. dev->stats.tx_bytes+=lp->tx_ring[t].skb->len;
  1054. dev_kfree_skb_irq(lp->tx_ring[t].skb);
  1055. lp->tx_ring[t].skb=NULL;
  1056. atomic_inc(&lp->tx_count);
  1057. netif_wake_queue(dev);
  1058. lp->tx_ring_tail=t;
  1059. }
  1060. }
  1061. /**
  1062. * mc32_interrupt - handle an interrupt from a 3c527
  1063. * @irq: Interrupt number
  1064. * @dev_id: 3c527 that requires servicing
  1065. * @regs: Registers (unused)
  1066. *
  1067. *
  1068. * An interrupt is raised whenever the 3c527 writes to the command
  1069. * register. This register contains the message it wishes to send us
  1070. * packed into a single byte field. We keep reading status entries
  1071. * until we have processed all the control items, but simply count
  1072. * transmit and receive reports. When all reports are in we empty the
  1073. * transceiver rings as appropriate. This saves the overhead of
  1074. * multiple command requests.
  1075. *
  1076. * Because MCA is level-triggered, we shouldn't miss indications.
  1077. * Therefore, we needn't ask the card to suspend interrupts within
  1078. * this handler. The card receives an implicit acknowledgment of the
  1079. * current interrupt when we read the command register.
  1080. *
  1081. */
  1082. static irqreturn_t mc32_interrupt(int irq, void *dev_id)
  1083. {
  1084. struct net_device *dev = dev_id;
  1085. struct mc32_local *lp;
  1086. int ioaddr, status, boguscount = 0;
  1087. int rx_event = 0;
  1088. int tx_event = 0;
  1089. ioaddr = dev->base_addr;
  1090. lp = netdev_priv(dev);
  1091. /* See whats cooking */
  1092. while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
  1093. {
  1094. status=inb(ioaddr+HOST_CMD);
  1095. pr_debug("Status TX%d RX%d EX%d OV%d BC%d\n",
  1096. (status&7), (status>>3)&7, (status>>6)&1,
  1097. (status>>7)&1, boguscount);
  1098. switch(status&7)
  1099. {
  1100. case 0:
  1101. break;
  1102. case 6: /* TX fail */
  1103. case 2: /* TX ok */
  1104. tx_event = 1;
  1105. break;
  1106. case 3: /* Halt */
  1107. case 4: /* Abort */
  1108. complete(&lp->xceiver_cmd);
  1109. break;
  1110. default:
  1111. pr_notice("%s: strange tx ack %d\n", dev->name, status&7);
  1112. }
  1113. status>>=3;
  1114. switch(status&7)
  1115. {
  1116. case 0:
  1117. break;
  1118. case 2: /* RX */
  1119. rx_event=1;
  1120. break;
  1121. case 3: /* Halt */
  1122. case 4: /* Abort */
  1123. complete(&lp->xceiver_cmd);
  1124. break;
  1125. case 6:
  1126. /* Out of RX buffers stat */
  1127. /* Must restart rx */
  1128. dev->stats.rx_dropped++;
  1129. mc32_rx_ring(dev);
  1130. mc32_start_transceiver(dev);
  1131. break;
  1132. default:
  1133. pr_notice("%s: strange rx ack %d\n",
  1134. dev->name, status&7);
  1135. }
  1136. status>>=3;
  1137. if(status&1)
  1138. {
  1139. /*
  1140. * No thread is waiting: we need to tidy
  1141. * up ourself.
  1142. */
  1143. if (lp->cmd_nonblocking) {
  1144. up(&lp->cmd_mutex);
  1145. if (lp->mc_reload_wait)
  1146. mc32_reset_multicast_list(dev);
  1147. }
  1148. else complete(&lp->execution_cmd);
  1149. }
  1150. if(status&2)
  1151. {
  1152. /*
  1153. * We get interrupted once per
  1154. * counter that is about to overflow.
  1155. */
  1156. mc32_update_stats(dev);
  1157. }
  1158. }
  1159. /*
  1160. * Process the transmit and receive rings
  1161. */
  1162. if(tx_event)
  1163. mc32_tx_ring(dev);
  1164. if(rx_event)
  1165. mc32_rx_ring(dev);
  1166. return IRQ_HANDLED;
  1167. }
  1168. /**
  1169. * mc32_close - user configuring the 3c527 down
  1170. * @dev: 3c527 card to shut down
  1171. *
  1172. * The 3c527 is a bus mastering device. We must be careful how we
  1173. * shut it down. It may also be running shared interrupt so we have
  1174. * to be sure to silence it properly
  1175. *
  1176. * We indicate that the card is closing to the rest of the
  1177. * driver. Otherwise, it is possible that the card may run out
  1178. * of receive buffers and restart the transceiver while we're
  1179. * trying to close it.
  1180. *
  1181. * We abort any receive and transmits going on and then wait until
  1182. * any pending exec commands have completed in other code threads.
  1183. * In theory we can't get here while that is true, in practice I am
  1184. * paranoid
  1185. *
  1186. * We turn off the interrupt enable for the board to be sure it can't
  1187. * intefere with other devices.
  1188. */
  1189. static int mc32_close(struct net_device *dev)
  1190. {
  1191. struct mc32_local *lp = netdev_priv(dev);
  1192. int ioaddr = dev->base_addr;
  1193. u8 regs;
  1194. u16 one=1;
  1195. lp->xceiver_desired_state = HALTED;
  1196. netif_stop_queue(dev);
  1197. /*
  1198. * Send the indications on command (handy debug check)
  1199. */
  1200. mc32_command(dev, 4, &one, 2);
  1201. /* Shut down the transceiver */
  1202. mc32_halt_transceiver(dev);
  1203. /* Ensure we issue no more commands beyond this point */
  1204. down(&lp->cmd_mutex);
  1205. /* Ok the card is now stopping */
  1206. regs=inb(ioaddr+HOST_CTRL);
  1207. regs&=~HOST_CTRL_INTE;
  1208. outb(regs, ioaddr+HOST_CTRL);
  1209. mc32_flush_rx_ring(dev);
  1210. mc32_flush_tx_ring(dev);
  1211. mc32_update_stats(dev);
  1212. return 0;
  1213. }
  1214. /**
  1215. * mc32_get_stats - hand back stats to network layer
  1216. * @dev: The 3c527 card to handle
  1217. *
  1218. * We've collected all the stats we can in software already. Now
  1219. * it's time to update those kept on-card and return the lot.
  1220. *
  1221. */
  1222. static struct net_device_stats *mc32_get_stats(struct net_device *dev)
  1223. {
  1224. mc32_update_stats(dev);
  1225. return &dev->stats;
  1226. }
  1227. /**
  1228. * do_mc32_set_multicast_list - attempt to update multicasts
  1229. * @dev: 3c527 device to load the list on
  1230. * @retry: indicates this is not the first call.
  1231. *
  1232. *
  1233. * Actually set or clear the multicast filter for this adaptor. The
  1234. * locking issues are handled by this routine. We have to track
  1235. * state as it may take multiple calls to get the command sequence
  1236. * completed. We just keep trying to schedule the loads until we
  1237. * manage to process them all.
  1238. *
  1239. * num_addrs == -1 Promiscuous mode, receive all packets
  1240. *
  1241. * num_addrs == 0 Normal mode, clear multicast list
  1242. *
  1243. * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1244. * and do best-effort filtering.
  1245. *
  1246. * See mc32_update_stats() regards setting the SAV BP bit.
  1247. *
  1248. */
  1249. static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
  1250. {
  1251. struct mc32_local *lp = netdev_priv(dev);
  1252. u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */
  1253. if ((dev->flags&IFF_PROMISC) ||
  1254. (dev->flags&IFF_ALLMULTI) ||
  1255. netdev_mc_count(dev) > 10)
  1256. /* Enable promiscuous mode */
  1257. filt |= 1;
  1258. else if (!netdev_mc_empty(dev))
  1259. {
  1260. unsigned char block[62];
  1261. unsigned char *bp;
  1262. struct netdev_hw_addr *ha;
  1263. if(retry==0)
  1264. lp->mc_list_valid = 0;
  1265. if(!lp->mc_list_valid)
  1266. {
  1267. block[1]=0;
  1268. block[0]=netdev_mc_count(dev);
  1269. bp=block+2;
  1270. netdev_for_each_mc_addr(ha, dev) {
  1271. memcpy(bp, ha->addr, 6);
  1272. bp+=6;
  1273. }
  1274. if(mc32_command_nowait(dev, 2, block,
  1275. 2+6*netdev_mc_count(dev))==-1)
  1276. {
  1277. lp->mc_reload_wait = 1;
  1278. return;
  1279. }
  1280. lp->mc_list_valid=1;
  1281. }
  1282. }
  1283. if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
  1284. {
  1285. lp->mc_reload_wait = 1;
  1286. }
  1287. else {
  1288. lp->mc_reload_wait = 0;
  1289. }
  1290. }
  1291. /**
  1292. * mc32_set_multicast_list - queue multicast list update
  1293. * @dev: The 3c527 to use
  1294. *
  1295. * Commence loading the multicast list. This is called when the kernel
  1296. * changes the lists. It will override any pending list we are trying to
  1297. * load.
  1298. */
  1299. static void mc32_set_multicast_list(struct net_device *dev)
  1300. {
  1301. do_mc32_set_multicast_list(dev,0);
  1302. }
  1303. /**
  1304. * mc32_reset_multicast_list - reset multicast list
  1305. * @dev: The 3c527 to use
  1306. *
  1307. * Attempt the next step in loading the multicast lists. If this attempt
  1308. * fails to complete then it will be scheduled and this function called
  1309. * again later from elsewhere.
  1310. */
  1311. static void mc32_reset_multicast_list(struct net_device *dev)
  1312. {
  1313. do_mc32_set_multicast_list(dev,1);
  1314. }
  1315. static void netdev_get_drvinfo(struct net_device *dev,
  1316. struct ethtool_drvinfo *info)
  1317. {
  1318. strcpy(info->driver, DRV_NAME);
  1319. strcpy(info->version, DRV_VERSION);
  1320. sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
  1321. }
  1322. static u32 netdev_get_msglevel(struct net_device *dev)
  1323. {
  1324. return mc32_debug;
  1325. }
  1326. static void netdev_set_msglevel(struct net_device *dev, u32 level)
  1327. {
  1328. mc32_debug = level;
  1329. }
  1330. static const struct ethtool_ops netdev_ethtool_ops = {
  1331. .get_drvinfo = netdev_get_drvinfo,
  1332. .get_msglevel = netdev_get_msglevel,
  1333. .set_msglevel = netdev_set_msglevel,
  1334. };
  1335. #ifdef MODULE
  1336. static struct net_device *this_device;
  1337. /**
  1338. * init_module - entry point
  1339. *
  1340. * Probe and locate a 3c527 card. This really should probe and locate
  1341. * all the 3c527 cards in the machine not just one of them. Yes you can
  1342. * insmod multiple modules for now but it's a hack.
  1343. */
  1344. int __init init_module(void)
  1345. {
  1346. this_device = mc32_probe(-1);
  1347. if (IS_ERR(this_device))
  1348. return PTR_ERR(this_device);
  1349. return 0;
  1350. }
  1351. /**
  1352. * cleanup_module - free resources for an unload
  1353. *
  1354. * Unloading time. We release the MCA bus resources and the interrupt
  1355. * at which point everything is ready to unload. The card must be stopped
  1356. * at this point or we would not have been called. When we unload we
  1357. * leave the card stopped but not totally shut down. When the card is
  1358. * initialized it must be rebooted or the rings reloaded before any
  1359. * transmit operations are allowed to start scribbling into memory.
  1360. */
  1361. void __exit cleanup_module(void)
  1362. {
  1363. unregister_netdev(this_device);
  1364. cleanup_card(this_device);
  1365. free_netdev(this_device);
  1366. }
  1367. #endif /* MODULE */