3c527.c 42 KB

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