axnet_cs.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. /*======================================================================
  2. A PCMCIA ethernet driver for Asix AX88190-based cards
  3. The Asix AX88190 is a NS8390-derived chipset with a few nasty
  4. idiosyncracies that make it very inconvenient to support with a
  5. standard 8390 driver. This driver is based on pcnet_cs, with the
  6. tweaked 8390 code grafted on the end. Much of what I did was to
  7. clean up and update a similar driver supplied by Asix, which was
  8. adapted by William Lee, william@asix.com.tw.
  9. Copyright (C) 2001 David A. Hinds -- dahinds@users.sourceforge.net
  10. axnet_cs.c 1.28 2002/06/29 06:27:37
  11. The network driver code is based on Donald Becker's NE2000 code:
  12. Written 1992,1993 by Donald Becker.
  13. Copyright 1993 United States Government as represented by the
  14. Director, National Security Agency. This software may be used and
  15. distributed according to the terms of the GNU General Public License,
  16. incorporated herein by reference.
  17. Donald Becker may be reached at becker@scyld.com
  18. ======================================================================*/
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/timer.h>
  26. #include <linux/delay.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/netdevice.h>
  30. #include "../8390.h"
  31. #include <pcmcia/version.h>
  32. #include <pcmcia/cs_types.h>
  33. #include <pcmcia/cs.h>
  34. #include <pcmcia/cistpl.h>
  35. #include <pcmcia/ciscode.h>
  36. #include <pcmcia/ds.h>
  37. #include <pcmcia/cisreg.h>
  38. #include <asm/io.h>
  39. #include <asm/system.h>
  40. #include <asm/byteorder.h>
  41. #include <asm/uaccess.h>
  42. #define AXNET_CMD 0x00
  43. #define AXNET_DATAPORT 0x10 /* NatSemi-defined port window offset. */
  44. #define AXNET_RESET 0x1f /* Issue a read to reset, a write to clear. */
  45. #define AXNET_MII_EEP 0x14 /* Offset of MII access port */
  46. #define AXNET_TEST 0x15 /* Offset of TEST Register port */
  47. #define AXNET_GPIO 0x17 /* Offset of General Purpose Register Port */
  48. #define AXNET_START_PG 0x40 /* First page of TX buffer */
  49. #define AXNET_STOP_PG 0x80 /* Last page +1 of RX ring */
  50. #define AXNET_RDC_TIMEOUT 0x02 /* Max wait in jiffies for Tx RDC */
  51. #define IS_AX88190 0x0001
  52. #define IS_AX88790 0x0002
  53. /*====================================================================*/
  54. /* Module parameters */
  55. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  56. MODULE_DESCRIPTION("Asix AX88190 PCMCIA ethernet driver");
  57. MODULE_LICENSE("GPL");
  58. #ifdef PCMCIA_DEBUG
  59. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
  60. INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
  61. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  62. static char *version =
  63. "axnet_cs.c 1.28 2002/06/29 06:27:37 (David Hinds)";
  64. #else
  65. #define DEBUG(n, args...)
  66. #endif
  67. /*====================================================================*/
  68. static void axnet_config(dev_link_t *link);
  69. static void axnet_release(dev_link_t *link);
  70. static int axnet_event(event_t event, int priority,
  71. event_callback_args_t *args);
  72. static int axnet_open(struct net_device *dev);
  73. static int axnet_close(struct net_device *dev);
  74. static int axnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  75. static struct ethtool_ops netdev_ethtool_ops;
  76. static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs);
  77. static void ei_watchdog(u_long arg);
  78. static void axnet_reset_8390(struct net_device *dev);
  79. static int mdio_read(kio_addr_t addr, int phy_id, int loc);
  80. static void mdio_write(kio_addr_t addr, int phy_id, int loc, int value);
  81. static void get_8390_hdr(struct net_device *,
  82. struct e8390_pkt_hdr *, int);
  83. static void block_input(struct net_device *dev, int count,
  84. struct sk_buff *skb, int ring_offset);
  85. static void block_output(struct net_device *dev, int count,
  86. const u_char *buf, const int start_page);
  87. static dev_link_t *axnet_attach(void);
  88. static void axnet_detach(dev_link_t *);
  89. static dev_info_t dev_info = "axnet_cs";
  90. static dev_link_t *dev_list;
  91. static void axdev_setup(struct net_device *dev);
  92. static void AX88190_init(struct net_device *dev, int startp);
  93. static int ax_open(struct net_device *dev);
  94. static int ax_close(struct net_device *dev);
  95. static irqreturn_t ax_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  96. /*====================================================================*/
  97. typedef struct axnet_dev_t {
  98. dev_link_t link;
  99. dev_node_t node;
  100. caddr_t base;
  101. struct timer_list watchdog;
  102. int stale, fast_poll;
  103. u_short link_status;
  104. u_char duplex_flag;
  105. int phy_id;
  106. int flags;
  107. } axnet_dev_t;
  108. static inline axnet_dev_t *PRIV(struct net_device *dev)
  109. {
  110. void *p = (char *)netdev_priv(dev) + sizeof(struct ei_device);
  111. return p;
  112. }
  113. /*======================================================================
  114. axnet_attach() creates an "instance" of the driver, allocating
  115. local data structures for one device. The device is registered
  116. with Card Services.
  117. ======================================================================*/
  118. static dev_link_t *axnet_attach(void)
  119. {
  120. axnet_dev_t *info;
  121. dev_link_t *link;
  122. struct net_device *dev;
  123. client_reg_t client_reg;
  124. int ret;
  125. DEBUG(0, "axnet_attach()\n");
  126. dev = alloc_netdev(sizeof(struct ei_device) + sizeof(axnet_dev_t),
  127. "eth%d", axdev_setup);
  128. if (!dev)
  129. return NULL;
  130. info = PRIV(dev);
  131. link = &info->link;
  132. link->priv = dev;
  133. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  134. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  135. link->conf.Attributes = CONF_ENABLE_IRQ;
  136. link->conf.IntType = INT_MEMORY_AND_IO;
  137. dev->open = &axnet_open;
  138. dev->stop = &axnet_close;
  139. dev->do_ioctl = &axnet_ioctl;
  140. SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
  141. /* Register with Card Services */
  142. link->next = dev_list;
  143. dev_list = link;
  144. client_reg.dev_info = &dev_info;
  145. client_reg.EventMask =
  146. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  147. CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  148. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  149. client_reg.event_handler = &axnet_event;
  150. client_reg.Version = 0x0210;
  151. client_reg.event_callback_args.client_data = link;
  152. ret = pcmcia_register_client(&link->handle, &client_reg);
  153. if (ret != CS_SUCCESS) {
  154. cs_error(link->handle, RegisterClient, ret);
  155. axnet_detach(link);
  156. return NULL;
  157. }
  158. return link;
  159. } /* axnet_attach */
  160. /*======================================================================
  161. This deletes a driver "instance". The device is de-registered
  162. with Card Services. If it has been released, all local data
  163. structures are freed. Otherwise, the structures will be freed
  164. when the device is released.
  165. ======================================================================*/
  166. static void axnet_detach(dev_link_t *link)
  167. {
  168. struct net_device *dev = link->priv;
  169. dev_link_t **linkp;
  170. DEBUG(0, "axnet_detach(0x%p)\n", link);
  171. /* Locate device structure */
  172. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  173. if (*linkp == link) break;
  174. if (*linkp == NULL)
  175. return;
  176. if (link->dev)
  177. unregister_netdev(dev);
  178. if (link->state & DEV_CONFIG)
  179. axnet_release(link);
  180. if (link->handle)
  181. pcmcia_deregister_client(link->handle);
  182. /* Unlink device structure, free bits */
  183. *linkp = link->next;
  184. free_netdev(dev);
  185. } /* axnet_detach */
  186. /*======================================================================
  187. This probes for a card's hardware address by reading the PROM.
  188. ======================================================================*/
  189. static int get_prom(dev_link_t *link)
  190. {
  191. struct net_device *dev = link->priv;
  192. kio_addr_t ioaddr = dev->base_addr;
  193. int i, j;
  194. /* This is based on drivers/net/ne.c */
  195. struct {
  196. u_char value, offset;
  197. } program_seq[] = {
  198. {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
  199. {0x01, EN0_DCFG}, /* Set word-wide access. */
  200. {0x00, EN0_RCNTLO}, /* Clear the count regs. */
  201. {0x00, EN0_RCNTHI},
  202. {0x00, EN0_IMR}, /* Mask completion irq. */
  203. {0xFF, EN0_ISR},
  204. {E8390_RXOFF|0x40, EN0_RXCR}, /* 0x60 Set to monitor */
  205. {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
  206. {0x10, EN0_RCNTLO},
  207. {0x00, EN0_RCNTHI},
  208. {0x00, EN0_RSARLO}, /* DMA starting at 0x0400. */
  209. {0x04, EN0_RSARHI},
  210. {E8390_RREAD+E8390_START, E8390_CMD},
  211. };
  212. /* Not much of a test, but the alternatives are messy */
  213. if (link->conf.ConfigBase != 0x03c0)
  214. return 0;
  215. axnet_reset_8390(dev);
  216. mdelay(10);
  217. for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
  218. outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
  219. for (i = 0; i < 6; i += 2) {
  220. j = inw(ioaddr + AXNET_DATAPORT);
  221. dev->dev_addr[i] = j & 0xff;
  222. dev->dev_addr[i+1] = j >> 8;
  223. }
  224. return 1;
  225. } /* get_prom */
  226. /*======================================================================
  227. axnet_config() is scheduled to run after a CARD_INSERTION event
  228. is received, to configure the PCMCIA socket, and to make the
  229. ethernet device available to the system.
  230. ======================================================================*/
  231. #define CS_CHECK(fn, ret) \
  232. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  233. static int try_io_port(dev_link_t *link)
  234. {
  235. int j, ret;
  236. if (link->io.NumPorts1 == 32) {
  237. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  238. if (link->io.NumPorts2 > 0) {
  239. /* for master/slave multifunction cards */
  240. link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
  241. link->irq.Attributes =
  242. IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  243. }
  244. } else {
  245. /* This should be two 16-port windows */
  246. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  247. link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
  248. }
  249. if (link->io.BasePort1 == 0) {
  250. link->io.IOAddrLines = 16;
  251. for (j = 0; j < 0x400; j += 0x20) {
  252. link->io.BasePort1 = j ^ 0x300;
  253. link->io.BasePort2 = (j ^ 0x300) + 0x10;
  254. ret = pcmcia_request_io(link->handle, &link->io);
  255. if (ret == CS_SUCCESS) return ret;
  256. }
  257. return ret;
  258. } else {
  259. return pcmcia_request_io(link->handle, &link->io);
  260. }
  261. }
  262. static void axnet_config(dev_link_t *link)
  263. {
  264. client_handle_t handle = link->handle;
  265. struct net_device *dev = link->priv;
  266. axnet_dev_t *info = PRIV(dev);
  267. tuple_t tuple;
  268. cisparse_t parse;
  269. int i, j, last_ret, last_fn;
  270. u_short buf[64];
  271. config_info_t conf;
  272. DEBUG(0, "axnet_config(0x%p)\n", link);
  273. tuple.Attributes = 0;
  274. tuple.TupleData = (cisdata_t *)buf;
  275. tuple.TupleDataMax = sizeof(buf);
  276. tuple.TupleOffset = 0;
  277. tuple.DesiredTuple = CISTPL_CONFIG;
  278. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  279. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  280. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  281. link->conf.ConfigBase = parse.config.base;
  282. /* don't trust the CIS on this; Linksys got it wrong */
  283. link->conf.Present = 0x63;
  284. /* Configure card */
  285. link->state |= DEV_CONFIG;
  286. /* Look up current Vcc */
  287. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  288. link->conf.Vcc = conf.Vcc;
  289. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  290. tuple.Attributes = 0;
  291. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  292. while (last_ret == CS_SUCCESS) {
  293. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  294. cistpl_io_t *io = &(parse.cftable_entry.io);
  295. if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
  296. pcmcia_parse_tuple(handle, &tuple, &parse) != 0 ||
  297. cfg->index == 0 || cfg->io.nwin == 0)
  298. goto next_entry;
  299. link->conf.ConfigIndex = 0x05;
  300. /* For multifunction cards, by convention, we configure the
  301. network function with window 0, and serial with window 1 */
  302. if (io->nwin > 1) {
  303. i = (io->win[1].len > io->win[0].len);
  304. link->io.BasePort2 = io->win[1-i].base;
  305. link->io.NumPorts2 = io->win[1-i].len;
  306. } else {
  307. i = link->io.NumPorts2 = 0;
  308. }
  309. link->io.BasePort1 = io->win[i].base;
  310. link->io.NumPorts1 = io->win[i].len;
  311. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  312. if (link->io.NumPorts1 + link->io.NumPorts2 >= 32) {
  313. last_ret = try_io_port(link);
  314. if (last_ret == CS_SUCCESS) break;
  315. }
  316. next_entry:
  317. last_ret = pcmcia_get_next_tuple(handle, &tuple);
  318. }
  319. if (last_ret != CS_SUCCESS) {
  320. cs_error(handle, RequestIO, last_ret);
  321. goto failed;
  322. }
  323. CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
  324. if (link->io.NumPorts2 == 8) {
  325. link->conf.Attributes |= CONF_ENABLE_SPKR;
  326. link->conf.Status = CCSR_AUDIO_ENA;
  327. }
  328. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
  329. dev->irq = link->irq.AssignedIRQ;
  330. dev->base_addr = link->io.BasePort1;
  331. if (!get_prom(link)) {
  332. printk(KERN_NOTICE "axnet_cs: this is not an AX88190 card!\n");
  333. printk(KERN_NOTICE "axnet_cs: use pcnet_cs instead.\n");
  334. goto failed;
  335. }
  336. ei_status.name = "AX88190";
  337. ei_status.word16 = 1;
  338. ei_status.tx_start_page = AXNET_START_PG;
  339. ei_status.rx_start_page = AXNET_START_PG + TX_PAGES;
  340. ei_status.stop_page = AXNET_STOP_PG;
  341. ei_status.reset_8390 = &axnet_reset_8390;
  342. ei_status.get_8390_hdr = &get_8390_hdr;
  343. ei_status.block_input = &block_input;
  344. ei_status.block_output = &block_output;
  345. if (inb(dev->base_addr + AXNET_TEST) != 0)
  346. info->flags |= IS_AX88790;
  347. else
  348. info->flags |= IS_AX88190;
  349. if (info->flags & IS_AX88790)
  350. outb(0x10, dev->base_addr + AXNET_GPIO); /* select Internal PHY */
  351. for (i = 0; i < 32; i++) {
  352. j = mdio_read(dev->base_addr + AXNET_MII_EEP, i, 1);
  353. if ((j != 0) && (j != 0xffff)) break;
  354. }
  355. /* Maybe PHY is in power down mode. (PPD_SET = 1)
  356. Bit 2 of CCSR is active low. */
  357. if (i == 32) {
  358. conf_reg_t reg = { 0, CS_WRITE, CISREG_CCSR, 0x04 };
  359. pcmcia_access_configuration_register(link->handle, &reg);
  360. for (i = 0; i < 32; i++) {
  361. j = mdio_read(dev->base_addr + AXNET_MII_EEP, i, 1);
  362. if ((j != 0) && (j != 0xffff)) break;
  363. }
  364. }
  365. info->phy_id = (i < 32) ? i : -1;
  366. link->dev = &info->node;
  367. link->state &= ~DEV_CONFIG_PENDING;
  368. SET_NETDEV_DEV(dev, &handle_to_dev(handle));
  369. if (register_netdev(dev) != 0) {
  370. printk(KERN_NOTICE "axnet_cs: register_netdev() failed\n");
  371. link->dev = NULL;
  372. goto failed;
  373. }
  374. strcpy(info->node.dev_name, dev->name);
  375. printk(KERN_INFO "%s: Asix AX88%d90: io %#3lx, irq %d, hw_addr ",
  376. dev->name, ((info->flags & IS_AX88790) ? 7 : 1),
  377. dev->base_addr, dev->irq);
  378. for (i = 0; i < 6; i++)
  379. printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
  380. if (info->phy_id != -1) {
  381. DEBUG(0, " MII transceiver at index %d, status %x.\n", info->phy_id, j);
  382. } else {
  383. printk(KERN_NOTICE " No MII transceivers found!\n");
  384. }
  385. return;
  386. cs_failed:
  387. cs_error(link->handle, last_fn, last_ret);
  388. failed:
  389. axnet_release(link);
  390. link->state &= ~DEV_CONFIG_PENDING;
  391. return;
  392. } /* axnet_config */
  393. /*======================================================================
  394. After a card is removed, axnet_release() will unregister the net
  395. device, and release the PCMCIA configuration. If the device is
  396. still open, this will be postponed until it is closed.
  397. ======================================================================*/
  398. static void axnet_release(dev_link_t *link)
  399. {
  400. DEBUG(0, "axnet_release(0x%p)\n", link);
  401. pcmcia_release_configuration(link->handle);
  402. pcmcia_release_io(link->handle, &link->io);
  403. pcmcia_release_irq(link->handle, &link->irq);
  404. link->state &= ~DEV_CONFIG;
  405. }
  406. /*======================================================================
  407. The card status event handler. Mostly, this schedules other
  408. stuff to run after an event is received. A CARD_REMOVAL event
  409. also sets some flags to discourage the net drivers from trying
  410. to talk to the card any more.
  411. ======================================================================*/
  412. static int axnet_event(event_t event, int priority,
  413. event_callback_args_t *args)
  414. {
  415. dev_link_t *link = args->client_data;
  416. struct net_device *dev = link->priv;
  417. DEBUG(2, "axnet_event(0x%06x)\n", event);
  418. switch (event) {
  419. case CS_EVENT_CARD_REMOVAL:
  420. link->state &= ~DEV_PRESENT;
  421. if (link->state & DEV_CONFIG)
  422. netif_device_detach(dev);
  423. break;
  424. case CS_EVENT_CARD_INSERTION:
  425. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  426. axnet_config(link);
  427. break;
  428. case CS_EVENT_PM_SUSPEND:
  429. link->state |= DEV_SUSPEND;
  430. /* Fall through... */
  431. case CS_EVENT_RESET_PHYSICAL:
  432. if (link->state & DEV_CONFIG) {
  433. if (link->open)
  434. netif_device_detach(dev);
  435. pcmcia_release_configuration(link->handle);
  436. }
  437. break;
  438. case CS_EVENT_PM_RESUME:
  439. link->state &= ~DEV_SUSPEND;
  440. /* Fall through... */
  441. case CS_EVENT_CARD_RESET:
  442. if (link->state & DEV_CONFIG) {
  443. pcmcia_request_configuration(link->handle, &link->conf);
  444. if (link->open) {
  445. axnet_reset_8390(dev);
  446. AX88190_init(dev, 1);
  447. netif_device_attach(dev);
  448. }
  449. }
  450. break;
  451. }
  452. return 0;
  453. } /* axnet_event */
  454. /*======================================================================
  455. MII interface support
  456. ======================================================================*/
  457. #define MDIO_SHIFT_CLK 0x01
  458. #define MDIO_DATA_WRITE0 0x00
  459. #define MDIO_DATA_WRITE1 0x08
  460. #define MDIO_DATA_READ 0x04
  461. #define MDIO_MASK 0x0f
  462. #define MDIO_ENB_IN 0x02
  463. static void mdio_sync(kio_addr_t addr)
  464. {
  465. int bits;
  466. for (bits = 0; bits < 32; bits++) {
  467. outb_p(MDIO_DATA_WRITE1, addr);
  468. outb_p(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
  469. }
  470. }
  471. static int mdio_read(kio_addr_t addr, int phy_id, int loc)
  472. {
  473. u_int cmd = (0xf6<<10)|(phy_id<<5)|loc;
  474. int i, retval = 0;
  475. mdio_sync(addr);
  476. for (i = 14; i >= 0; i--) {
  477. int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
  478. outb_p(dat, addr);
  479. outb_p(dat | MDIO_SHIFT_CLK, addr);
  480. }
  481. for (i = 19; i > 0; i--) {
  482. outb_p(MDIO_ENB_IN, addr);
  483. retval = (retval << 1) | ((inb_p(addr) & MDIO_DATA_READ) != 0);
  484. outb_p(MDIO_ENB_IN | MDIO_SHIFT_CLK, addr);
  485. }
  486. return (retval>>1) & 0xffff;
  487. }
  488. static void mdio_write(kio_addr_t addr, int phy_id, int loc, int value)
  489. {
  490. u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
  491. int i;
  492. mdio_sync(addr);
  493. for (i = 31; i >= 0; i--) {
  494. int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
  495. outb_p(dat, addr);
  496. outb_p(dat | MDIO_SHIFT_CLK, addr);
  497. }
  498. for (i = 1; i >= 0; i--) {
  499. outb_p(MDIO_ENB_IN, addr);
  500. outb_p(MDIO_ENB_IN | MDIO_SHIFT_CLK, addr);
  501. }
  502. }
  503. /*====================================================================*/
  504. static int axnet_open(struct net_device *dev)
  505. {
  506. axnet_dev_t *info = PRIV(dev);
  507. dev_link_t *link = &info->link;
  508. DEBUG(2, "axnet_open('%s')\n", dev->name);
  509. if (!DEV_OK(link))
  510. return -ENODEV;
  511. link->open++;
  512. request_irq(dev->irq, ei_irq_wrapper, SA_SHIRQ, dev_info, dev);
  513. info->link_status = 0x00;
  514. init_timer(&info->watchdog);
  515. info->watchdog.function = &ei_watchdog;
  516. info->watchdog.data = (u_long)dev;
  517. info->watchdog.expires = jiffies + HZ;
  518. add_timer(&info->watchdog);
  519. return ax_open(dev);
  520. } /* axnet_open */
  521. /*====================================================================*/
  522. static int axnet_close(struct net_device *dev)
  523. {
  524. axnet_dev_t *info = PRIV(dev);
  525. dev_link_t *link = &info->link;
  526. DEBUG(2, "axnet_close('%s')\n", dev->name);
  527. ax_close(dev);
  528. free_irq(dev->irq, dev);
  529. link->open--;
  530. netif_stop_queue(dev);
  531. del_timer_sync(&info->watchdog);
  532. return 0;
  533. } /* axnet_close */
  534. /*======================================================================
  535. Hard reset the card. This used to pause for the same period that
  536. a 8390 reset command required, but that shouldn't be necessary.
  537. ======================================================================*/
  538. static void axnet_reset_8390(struct net_device *dev)
  539. {
  540. kio_addr_t nic_base = dev->base_addr;
  541. int i;
  542. ei_status.txing = ei_status.dmaing = 0;
  543. outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, nic_base + E8390_CMD);
  544. outb(inb(nic_base + AXNET_RESET), nic_base + AXNET_RESET);
  545. for (i = 0; i < 100; i++) {
  546. if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
  547. break;
  548. udelay(100);
  549. }
  550. outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
  551. if (i == 100)
  552. printk(KERN_ERR "%s: axnet_reset_8390() did not complete.\n",
  553. dev->name);
  554. } /* axnet_reset_8390 */
  555. /*====================================================================*/
  556. static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs)
  557. {
  558. struct net_device *dev = dev_id;
  559. PRIV(dev)->stale = 0;
  560. return ax_interrupt(irq, dev_id, regs);
  561. }
  562. static void ei_watchdog(u_long arg)
  563. {
  564. struct net_device *dev = (struct net_device *)(arg);
  565. axnet_dev_t *info = PRIV(dev);
  566. kio_addr_t nic_base = dev->base_addr;
  567. kio_addr_t mii_addr = nic_base + AXNET_MII_EEP;
  568. u_short link;
  569. if (!netif_device_present(dev)) goto reschedule;
  570. /* Check for pending interrupt with expired latency timer: with
  571. this, we can limp along even if the interrupt is blocked */
  572. if (info->stale++ && (inb_p(nic_base + EN0_ISR) & ENISR_ALL)) {
  573. if (!info->fast_poll)
  574. printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
  575. ei_irq_wrapper(dev->irq, dev, NULL);
  576. info->fast_poll = HZ;
  577. }
  578. if (info->fast_poll) {
  579. info->fast_poll--;
  580. info->watchdog.expires = jiffies + 1;
  581. add_timer(&info->watchdog);
  582. return;
  583. }
  584. if (info->phy_id < 0)
  585. goto reschedule;
  586. link = mdio_read(mii_addr, info->phy_id, 1);
  587. if (!link || (link == 0xffff)) {
  588. printk(KERN_INFO "%s: MII is missing!\n", dev->name);
  589. info->phy_id = -1;
  590. goto reschedule;
  591. }
  592. link &= 0x0004;
  593. if (link != info->link_status) {
  594. u_short p = mdio_read(mii_addr, info->phy_id, 5);
  595. printk(KERN_INFO "%s: %s link beat\n", dev->name,
  596. (link) ? "found" : "lost");
  597. if (link) {
  598. info->duplex_flag = (p & 0x0140) ? 0x80 : 0x00;
  599. if (p)
  600. printk(KERN_INFO "%s: autonegotiation complete: "
  601. "%sbaseT-%cD selected\n", dev->name,
  602. ((p & 0x0180) ? "100" : "10"),
  603. ((p & 0x0140) ? 'F' : 'H'));
  604. else
  605. printk(KERN_INFO "%s: link partner did not autonegotiate\n",
  606. dev->name);
  607. AX88190_init(dev, 1);
  608. }
  609. info->link_status = link;
  610. }
  611. reschedule:
  612. info->watchdog.expires = jiffies + HZ;
  613. add_timer(&info->watchdog);
  614. }
  615. static void netdev_get_drvinfo(struct net_device *dev,
  616. struct ethtool_drvinfo *info)
  617. {
  618. strcpy(info->driver, "axnet_cs");
  619. }
  620. static struct ethtool_ops netdev_ethtool_ops = {
  621. .get_drvinfo = netdev_get_drvinfo,
  622. };
  623. /*====================================================================*/
  624. static int axnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  625. {
  626. axnet_dev_t *info = PRIV(dev);
  627. u16 *data = (u16 *)&rq->ifr_ifru;
  628. kio_addr_t mii_addr = dev->base_addr + AXNET_MII_EEP;
  629. switch (cmd) {
  630. case SIOCGMIIPHY:
  631. data[0] = info->phy_id;
  632. case SIOCGMIIREG: /* Read MII PHY register. */
  633. data[3] = mdio_read(mii_addr, data[0], data[1] & 0x1f);
  634. return 0;
  635. case SIOCSMIIREG: /* Write MII PHY register. */
  636. if (!capable(CAP_NET_ADMIN))
  637. return -EPERM;
  638. mdio_write(mii_addr, data[0], data[1] & 0x1f, data[2]);
  639. return 0;
  640. }
  641. return -EOPNOTSUPP;
  642. }
  643. /*====================================================================*/
  644. static void get_8390_hdr(struct net_device *dev,
  645. struct e8390_pkt_hdr *hdr,
  646. int ring_page)
  647. {
  648. kio_addr_t nic_base = dev->base_addr;
  649. outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
  650. outb_p(ring_page, nic_base + EN0_RSARHI);
  651. outb_p(E8390_RREAD+E8390_START, nic_base + AXNET_CMD);
  652. insw(nic_base + AXNET_DATAPORT, hdr,
  653. sizeof(struct e8390_pkt_hdr)>>1);
  654. /* Fix for big endian systems */
  655. hdr->count = le16_to_cpu(hdr->count);
  656. }
  657. /*====================================================================*/
  658. static void block_input(struct net_device *dev, int count,
  659. struct sk_buff *skb, int ring_offset)
  660. {
  661. kio_addr_t nic_base = dev->base_addr;
  662. int xfer_count = count;
  663. char *buf = skb->data;
  664. #ifdef PCMCIA_DEBUG
  665. if ((ei_debug > 4) && (count != 4))
  666. printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
  667. #endif
  668. outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
  669. outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
  670. outb_p(E8390_RREAD+E8390_START, nic_base + AXNET_CMD);
  671. insw(nic_base + AXNET_DATAPORT,buf,count>>1);
  672. if (count & 0x01)
  673. buf[count-1] = inb(nic_base + AXNET_DATAPORT), xfer_count++;
  674. }
  675. /*====================================================================*/
  676. static void block_output(struct net_device *dev, int count,
  677. const u_char *buf, const int start_page)
  678. {
  679. kio_addr_t nic_base = dev->base_addr;
  680. #ifdef PCMCIA_DEBUG
  681. if (ei_debug > 4)
  682. printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
  683. #endif
  684. /* Round the count up for word writes. Do we need to do this?
  685. What effect will an odd byte count have on the 8390?
  686. I should check someday. */
  687. if (count & 0x01)
  688. count++;
  689. outb_p(0x00, nic_base + EN0_RSARLO);
  690. outb_p(start_page, nic_base + EN0_RSARHI);
  691. outb_p(E8390_RWRITE+E8390_START, nic_base + AXNET_CMD);
  692. outsw(nic_base + AXNET_DATAPORT, buf, count>>1);
  693. }
  694. static struct pcmcia_device_id axnet_ids[] = {
  695. PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x016c, 0x0081),
  696. PCMCIA_DEVICE_MANF_CARD(0x018a, 0x0301),
  697. PCMCIA_DEVICE_MANF_CARD(0x026f, 0x0301),
  698. PCMCIA_DEVICE_MANF_CARD(0x026f, 0x0303),
  699. PCMCIA_DEVICE_MANF_CARD(0x026f, 0x0309),
  700. PCMCIA_DEVICE_MANF_CARD(0x0274, 0x1106),
  701. PCMCIA_DEVICE_MANF_CARD(0x8a01, 0xc1ab),
  702. PCMCIA_DEVICE_PROD_ID124("Fast Ethernet", "16-bit PC Card", "AX88190", 0xb4be14e3, 0x9a12eb6a, 0xab9be5ef),
  703. PCMCIA_DEVICE_PROD_ID12("ASIX", "AX88190", 0x0959823b, 0xab9be5ef),
  704. PCMCIA_DEVICE_PROD_ID12("Billionton", "LNA-100B", 0x552ab682, 0xbc3b87e1),
  705. PCMCIA_DEVICE_PROD_ID12("CHEETAH ETHERCARD", "EN2228", 0x00fa7bc8, 0x00e990cc),
  706. PCMCIA_DEVICE_PROD_ID12("CNet", "CNF301", 0xbc477dde, 0x78c5f40b),
  707. PCMCIA_DEVICE_PROD_ID12("corega K.K.", "corega FEther PCC-TXD", 0x5261440f, 0x436768c5),
  708. PCMCIA_DEVICE_PROD_ID12("corega K.K.", "corega FEtherII PCC-TXD", 0x5261440f, 0x730df72e),
  709. PCMCIA_DEVICE_PROD_ID12("Dynalink", "L100C16", 0x55632fd5, 0x66bc2a90),
  710. PCMCIA_DEVICE_PROD_ID12("Linksys", "EtherFast 10/100 PC Card (PCMPC100 V3)", 0x0733cc81, 0x232019a8),
  711. PCMCIA_DEVICE_PROD_ID12("MELCO", "LPC3-TX", 0x481e0094, 0xf91af609),
  712. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "100BASE", 0x281f1c5d, 0x7c2add04),
  713. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "FastEtherCard", 0x281f1c5d, 0x7ef26116),
  714. PCMCIA_DEVICE_PROD_ID12("PCMCIA", "FEP501", 0x281f1c5d, 0x2e272058),
  715. PCMCIA_DEVICE_PROD_ID14("Network Everywhere", "AX88190", 0x820a67b6, 0xab9be5ef),
  716. /* this is not specific enough */
  717. /* PCMCIA_DEVICE_MANF_CARD(0x021b, 0x0202), */
  718. PCMCIA_DEVICE_NULL,
  719. };
  720. MODULE_DEVICE_TABLE(pcmcia, axnet_ids);
  721. static struct pcmcia_driver axnet_cs_driver = {
  722. .owner = THIS_MODULE,
  723. .drv = {
  724. .name = "axnet_cs",
  725. },
  726. .attach = axnet_attach,
  727. .detach = axnet_detach,
  728. .id_table = axnet_ids,
  729. };
  730. static int __init init_axnet_cs(void)
  731. {
  732. return pcmcia_register_driver(&axnet_cs_driver);
  733. }
  734. static void __exit exit_axnet_cs(void)
  735. {
  736. pcmcia_unregister_driver(&axnet_cs_driver);
  737. BUG_ON(dev_list != NULL);
  738. }
  739. module_init(init_axnet_cs);
  740. module_exit(exit_axnet_cs);
  741. /*====================================================================*/
  742. /* 8390.c: A general NS8390 ethernet driver core for linux. */
  743. /*
  744. Written 1992-94 by Donald Becker.
  745. Copyright 1993 United States Government as represented by the
  746. Director, National Security Agency.
  747. This software may be used and distributed according to the terms
  748. of the GNU General Public License, incorporated herein by reference.
  749. The author may be reached as becker@scyld.com, or C/O
  750. Scyld Computing Corporation
  751. 410 Severn Ave., Suite 210
  752. Annapolis MD 21403
  753. This is the chip-specific code for many 8390-based ethernet adaptors.
  754. This is not a complete driver, it must be combined with board-specific
  755. code such as ne.c, wd.c, 3c503.c, etc.
  756. Seeing how at least eight drivers use this code, (not counting the
  757. PCMCIA ones either) it is easy to break some card by what seems like
  758. a simple innocent change. Please contact me or Donald if you think
  759. you have found something that needs changing. -- PG
  760. Changelog:
  761. Paul Gortmaker : remove set_bit lock, other cleanups.
  762. Paul Gortmaker : add ei_get_8390_hdr() so we can pass skb's to
  763. ei_block_input() for eth_io_copy_and_sum().
  764. Paul Gortmaker : exchange static int ei_pingpong for a #define,
  765. also add better Tx error handling.
  766. Paul Gortmaker : rewrite Rx overrun handling as per NS specs.
  767. Alexey Kuznetsov : use the 8390's six bit hash multicast filter.
  768. Paul Gortmaker : tweak ANK's above multicast changes a bit.
  769. Paul Gortmaker : update packet statistics for v2.1.x
  770. Alan Cox : support arbitary stupid port mappings on the
  771. 68K Macintosh. Support >16bit I/O spaces
  772. Paul Gortmaker : add kmod support for auto-loading of the 8390
  773. module by all drivers that require it.
  774. Alan Cox : Spinlocking work, added 'BUG_83C690'
  775. Paul Gortmaker : Separate out Tx timeout code from Tx path.
  776. Sources:
  777. The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
  778. */
  779. static const char *version_8390 =
  780. "8390.c:v1.10cvs 9/23/94 Donald Becker (becker@scyld.com)\n";
  781. #include <linux/bitops.h>
  782. #include <asm/irq.h>
  783. #include <linux/fcntl.h>
  784. #include <linux/in.h>
  785. #include <linux/interrupt.h>
  786. #include <linux/etherdevice.h>
  787. #define BUG_83C690
  788. /* These are the operational function interfaces to board-specific
  789. routines.
  790. void reset_8390(struct net_device *dev)
  791. Resets the board associated with DEV, including a hardware reset of
  792. the 8390. This is only called when there is a transmit timeout, and
  793. it is always followed by 8390_init().
  794. void block_output(struct net_device *dev, int count, const unsigned char *buf,
  795. int start_page)
  796. Write the COUNT bytes of BUF to the packet buffer at START_PAGE. The
  797. "page" value uses the 8390's 256-byte pages.
  798. void get_8390_hdr(struct net_device *dev, struct e8390_hdr *hdr, int ring_page)
  799. Read the 4 byte, page aligned 8390 header. *If* there is a
  800. subsequent read, it will be of the rest of the packet.
  801. void block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
  802. Read COUNT bytes from the packet buffer into the skb data area. Start
  803. reading from RING_OFFSET, the address as the 8390 sees it. This will always
  804. follow the read of the 8390 header.
  805. */
  806. #define ei_reset_8390 (ei_local->reset_8390)
  807. #define ei_block_output (ei_local->block_output)
  808. #define ei_block_input (ei_local->block_input)
  809. #define ei_get_8390_hdr (ei_local->get_8390_hdr)
  810. /* use 0 for production, 1 for verification, >2 for debug */
  811. #ifndef ei_debug
  812. int ei_debug = 1;
  813. #endif
  814. /* Index to functions. */
  815. static void ei_tx_intr(struct net_device *dev);
  816. static void ei_tx_err(struct net_device *dev);
  817. static void ei_tx_timeout(struct net_device *dev);
  818. static void ei_receive(struct net_device *dev);
  819. static void ei_rx_overrun(struct net_device *dev);
  820. /* Routines generic to NS8390-based boards. */
  821. static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
  822. int start_page);
  823. static void set_multicast_list(struct net_device *dev);
  824. static void do_set_multicast_list(struct net_device *dev);
  825. /*
  826. * SMP and the 8390 setup.
  827. *
  828. * The 8390 isnt exactly designed to be multithreaded on RX/TX. There is
  829. * a page register that controls bank and packet buffer access. We guard
  830. * this with ei_local->page_lock. Nobody should assume or set the page other
  831. * than zero when the lock is not held. Lock holders must restore page 0
  832. * before unlocking. Even pure readers must take the lock to protect in
  833. * page 0.
  834. *
  835. * To make life difficult the chip can also be very slow. We therefore can't
  836. * just use spinlocks. For the longer lockups we disable the irq the device
  837. * sits on and hold the lock. We must hold the lock because there is a dual
  838. * processor case other than interrupts (get stats/set multicast list in
  839. * parallel with each other and transmit).
  840. *
  841. * Note: in theory we can just disable the irq on the card _but_ there is
  842. * a latency on SMP irq delivery. So we can easily go "disable irq" "sync irqs"
  843. * enter lock, take the queued irq. So we waddle instead of flying.
  844. *
  845. * Finally by special arrangement for the purpose of being generally
  846. * annoying the transmit function is called bh atomic. That places
  847. * restrictions on the user context callers as disable_irq won't save
  848. * them.
  849. */
  850. /**
  851. * ax_open - Open/initialize the board.
  852. * @dev: network device to initialize
  853. *
  854. * This routine goes all-out, setting everything
  855. * up anew at each open, even though many of these registers should only
  856. * need to be set once at boot.
  857. */
  858. static int ax_open(struct net_device *dev)
  859. {
  860. unsigned long flags;
  861. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  862. #ifdef HAVE_TX_TIMEOUT
  863. /* The card I/O part of the driver (e.g. 3c503) can hook a Tx timeout
  864. wrapper that does e.g. media check & then calls ei_tx_timeout. */
  865. if (dev->tx_timeout == NULL)
  866. dev->tx_timeout = ei_tx_timeout;
  867. if (dev->watchdog_timeo <= 0)
  868. dev->watchdog_timeo = TX_TIMEOUT;
  869. #endif
  870. /*
  871. * Grab the page lock so we own the register set, then call
  872. * the init function.
  873. */
  874. spin_lock_irqsave(&ei_local->page_lock, flags);
  875. AX88190_init(dev, 1);
  876. /* Set the flag before we drop the lock, That way the IRQ arrives
  877. after its set and we get no silly warnings */
  878. netif_start_queue(dev);
  879. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  880. ei_local->irqlock = 0;
  881. return 0;
  882. }
  883. #define dev_lock(dev) (((struct ei_device *)netdev_priv(dev))->page_lock)
  884. /**
  885. * ax_close - shut down network device
  886. * @dev: network device to close
  887. *
  888. * Opposite of ax_open(). Only used when "ifconfig <devname> down" is done.
  889. */
  890. int ax_close(struct net_device *dev)
  891. {
  892. unsigned long flags;
  893. /*
  894. * Hold the page lock during close
  895. */
  896. spin_lock_irqsave(&dev_lock(dev), flags);
  897. AX88190_init(dev, 0);
  898. spin_unlock_irqrestore(&dev_lock(dev), flags);
  899. netif_stop_queue(dev);
  900. return 0;
  901. }
  902. /**
  903. * ei_tx_timeout - handle transmit time out condition
  904. * @dev: network device which has apparently fallen asleep
  905. *
  906. * Called by kernel when device never acknowledges a transmit has
  907. * completed (or failed) - i.e. never posted a Tx related interrupt.
  908. */
  909. void ei_tx_timeout(struct net_device *dev)
  910. {
  911. long e8390_base = dev->base_addr;
  912. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  913. int txsr, isr, tickssofar = jiffies - dev->trans_start;
  914. unsigned long flags;
  915. ei_local->stat.tx_errors++;
  916. spin_lock_irqsave(&ei_local->page_lock, flags);
  917. txsr = inb(e8390_base+EN0_TSR);
  918. isr = inb(e8390_base+EN0_ISR);
  919. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  920. printk(KERN_DEBUG "%s: Tx timed out, %s TSR=%#2x, ISR=%#2x, t=%d.\n",
  921. dev->name, (txsr & ENTSR_ABT) ? "excess collisions." :
  922. (isr) ? "lost interrupt?" : "cable problem?", txsr, isr, tickssofar);
  923. if (!isr && !ei_local->stat.tx_packets)
  924. {
  925. /* The 8390 probably hasn't gotten on the cable yet. */
  926. ei_local->interface_num ^= 1; /* Try a different xcvr. */
  927. }
  928. /* Ugly but a reset can be slow, yet must be protected */
  929. disable_irq_nosync(dev->irq);
  930. spin_lock(&ei_local->page_lock);
  931. /* Try to restart the card. Perhaps the user has fixed something. */
  932. ei_reset_8390(dev);
  933. AX88190_init(dev, 1);
  934. spin_unlock(&ei_local->page_lock);
  935. enable_irq(dev->irq);
  936. netif_wake_queue(dev);
  937. }
  938. /**
  939. * ei_start_xmit - begin packet transmission
  940. * @skb: packet to be sent
  941. * @dev: network device to which packet is sent
  942. *
  943. * Sends a packet to an 8390 network device.
  944. */
  945. static int ei_start_xmit(struct sk_buff *skb, struct net_device *dev)
  946. {
  947. long e8390_base = dev->base_addr;
  948. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  949. int length, send_length, output_page;
  950. unsigned long flags;
  951. u8 packet[ETH_ZLEN];
  952. netif_stop_queue(dev);
  953. length = skb->len;
  954. /* Mask interrupts from the ethercard.
  955. SMP: We have to grab the lock here otherwise the IRQ handler
  956. on another CPU can flip window and race the IRQ mask set. We end
  957. up trashing the mcast filter not disabling irqs if we don't lock */
  958. spin_lock_irqsave(&ei_local->page_lock, flags);
  959. outb_p(0x00, e8390_base + EN0_IMR);
  960. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  961. /*
  962. * Slow phase with lock held.
  963. */
  964. disable_irq_nosync(dev->irq);
  965. spin_lock(&ei_local->page_lock);
  966. ei_local->irqlock = 1;
  967. send_length = ETH_ZLEN < length ? length : ETH_ZLEN;
  968. /*
  969. * We have two Tx slots available for use. Find the first free
  970. * slot, and then perform some sanity checks. With two Tx bufs,
  971. * you get very close to transmitting back-to-back packets. With
  972. * only one Tx buf, the transmitter sits idle while you reload the
  973. * card, leaving a substantial gap between each transmitted packet.
  974. */
  975. if (ei_local->tx1 == 0)
  976. {
  977. output_page = ei_local->tx_start_page;
  978. ei_local->tx1 = send_length;
  979. if (ei_debug && ei_local->tx2 > 0)
  980. printk(KERN_DEBUG "%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
  981. dev->name, ei_local->tx2, ei_local->lasttx, ei_local->txing);
  982. }
  983. else if (ei_local->tx2 == 0)
  984. {
  985. output_page = ei_local->tx_start_page + TX_PAGES/2;
  986. ei_local->tx2 = send_length;
  987. if (ei_debug && ei_local->tx1 > 0)
  988. printk(KERN_DEBUG "%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
  989. dev->name, ei_local->tx1, ei_local->lasttx, ei_local->txing);
  990. }
  991. else
  992. { /* We should never get here. */
  993. if (ei_debug)
  994. printk(KERN_DEBUG "%s: No Tx buffers free! tx1=%d tx2=%d last=%d\n",
  995. dev->name, ei_local->tx1, ei_local->tx2, ei_local->lasttx);
  996. ei_local->irqlock = 0;
  997. netif_stop_queue(dev);
  998. outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  999. spin_unlock(&ei_local->page_lock);
  1000. enable_irq(dev->irq);
  1001. ei_local->stat.tx_errors++;
  1002. return 1;
  1003. }
  1004. /*
  1005. * Okay, now upload the packet and trigger a send if the transmitter
  1006. * isn't already sending. If it is busy, the interrupt handler will
  1007. * trigger the send later, upon receiving a Tx done interrupt.
  1008. */
  1009. if (length == skb->len)
  1010. ei_block_output(dev, length, skb->data, output_page);
  1011. else {
  1012. memset(packet, 0, ETH_ZLEN);
  1013. memcpy(packet, skb->data, skb->len);
  1014. ei_block_output(dev, length, packet, output_page);
  1015. }
  1016. if (! ei_local->txing)
  1017. {
  1018. ei_local->txing = 1;
  1019. NS8390_trigger_send(dev, send_length, output_page);
  1020. dev->trans_start = jiffies;
  1021. if (output_page == ei_local->tx_start_page)
  1022. {
  1023. ei_local->tx1 = -1;
  1024. ei_local->lasttx = -1;
  1025. }
  1026. else
  1027. {
  1028. ei_local->tx2 = -1;
  1029. ei_local->lasttx = -2;
  1030. }
  1031. }
  1032. else ei_local->txqueue++;
  1033. if (ei_local->tx1 && ei_local->tx2)
  1034. netif_stop_queue(dev);
  1035. else
  1036. netif_start_queue(dev);
  1037. /* Turn 8390 interrupts back on. */
  1038. ei_local->irqlock = 0;
  1039. outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  1040. spin_unlock(&ei_local->page_lock);
  1041. enable_irq(dev->irq);
  1042. dev_kfree_skb (skb);
  1043. ei_local->stat.tx_bytes += send_length;
  1044. return 0;
  1045. }
  1046. /**
  1047. * ax_interrupt - handle the interrupts from an 8390
  1048. * @irq: interrupt number
  1049. * @dev_id: a pointer to the net_device
  1050. * @regs: unused
  1051. *
  1052. * Handle the ether interface interrupts. We pull packets from
  1053. * the 8390 via the card specific functions and fire them at the networking
  1054. * stack. We also handle transmit completions and wake the transmit path if
  1055. * necessary. We also update the counters and do other housekeeping as
  1056. * needed.
  1057. */
  1058. static irqreturn_t ax_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  1059. {
  1060. struct net_device *dev = dev_id;
  1061. long e8390_base;
  1062. int interrupts, nr_serviced = 0, i;
  1063. struct ei_device *ei_local;
  1064. int handled = 0;
  1065. if (dev == NULL)
  1066. {
  1067. printk ("net_interrupt(): irq %d for unknown device.\n", irq);
  1068. return IRQ_NONE;
  1069. }
  1070. e8390_base = dev->base_addr;
  1071. ei_local = (struct ei_device *) netdev_priv(dev);
  1072. /*
  1073. * Protect the irq test too.
  1074. */
  1075. spin_lock(&ei_local->page_lock);
  1076. if (ei_local->irqlock)
  1077. {
  1078. #if 1 /* This might just be an interrupt for a PCI device sharing this line */
  1079. /* The "irqlock" check is only for testing. */
  1080. printk(ei_local->irqlock
  1081. ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
  1082. : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
  1083. dev->name, inb_p(e8390_base + EN0_ISR),
  1084. inb_p(e8390_base + EN0_IMR));
  1085. #endif
  1086. spin_unlock(&ei_local->page_lock);
  1087. return IRQ_NONE;
  1088. }
  1089. if (ei_debug > 3)
  1090. printk(KERN_DEBUG "%s: interrupt(isr=%#2.2x).\n", dev->name,
  1091. inb_p(e8390_base + EN0_ISR));
  1092. outb_p(0x00, e8390_base + EN0_ISR);
  1093. ei_local->irqlock = 1;
  1094. /* !!Assumption!! -- we stay in page 0. Don't break this. */
  1095. while ((interrupts = inb_p(e8390_base + EN0_ISR)) != 0
  1096. && ++nr_serviced < MAX_SERVICE)
  1097. {
  1098. if (!netif_running(dev) || (interrupts == 0xff)) {
  1099. if (ei_debug > 1)
  1100. printk(KERN_WARNING "%s: interrupt from stopped card\n", dev->name);
  1101. outb_p(interrupts, e8390_base + EN0_ISR);
  1102. interrupts = 0;
  1103. break;
  1104. }
  1105. handled = 1;
  1106. /* AX88190 bug fix. */
  1107. outb_p(interrupts, e8390_base + EN0_ISR);
  1108. for (i = 0; i < 10; i++) {
  1109. if (!(inb(e8390_base + EN0_ISR) & interrupts))
  1110. break;
  1111. outb_p(0, e8390_base + EN0_ISR);
  1112. outb_p(interrupts, e8390_base + EN0_ISR);
  1113. }
  1114. if (interrupts & ENISR_OVER)
  1115. ei_rx_overrun(dev);
  1116. else if (interrupts & (ENISR_RX+ENISR_RX_ERR))
  1117. {
  1118. /* Got a good (?) packet. */
  1119. ei_receive(dev);
  1120. }
  1121. /* Push the next to-transmit packet through. */
  1122. if (interrupts & ENISR_TX)
  1123. ei_tx_intr(dev);
  1124. else if (interrupts & ENISR_TX_ERR)
  1125. ei_tx_err(dev);
  1126. if (interrupts & ENISR_COUNTERS)
  1127. {
  1128. ei_local->stat.rx_frame_errors += inb_p(e8390_base + EN0_COUNTER0);
  1129. ei_local->stat.rx_crc_errors += inb_p(e8390_base + EN0_COUNTER1);
  1130. ei_local->stat.rx_missed_errors+= inb_p(e8390_base + EN0_COUNTER2);
  1131. }
  1132. }
  1133. if (interrupts && ei_debug)
  1134. {
  1135. handled = 1;
  1136. if (nr_serviced >= MAX_SERVICE)
  1137. {
  1138. /* 0xFF is valid for a card removal */
  1139. if(interrupts!=0xFF)
  1140. printk(KERN_WARNING "%s: Too much work at interrupt, status %#2.2x\n",
  1141. dev->name, interrupts);
  1142. outb_p(ENISR_ALL, e8390_base + EN0_ISR); /* Ack. most intrs. */
  1143. } else {
  1144. printk(KERN_WARNING "%s: unknown interrupt %#2x\n", dev->name, interrupts);
  1145. outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
  1146. }
  1147. }
  1148. /* Turn 8390 interrupts back on. */
  1149. ei_local->irqlock = 0;
  1150. outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  1151. spin_unlock(&ei_local->page_lock);
  1152. return IRQ_RETVAL(handled);
  1153. }
  1154. /**
  1155. * ei_tx_err - handle transmitter error
  1156. * @dev: network device which threw the exception
  1157. *
  1158. * A transmitter error has happened. Most likely excess collisions (which
  1159. * is a fairly normal condition). If the error is one where the Tx will
  1160. * have been aborted, we try and send another one right away, instead of
  1161. * letting the failed packet sit and collect dust in the Tx buffer. This
  1162. * is a much better solution as it avoids kernel based Tx timeouts, and
  1163. * an unnecessary card reset.
  1164. *
  1165. * Called with lock held.
  1166. */
  1167. static void ei_tx_err(struct net_device *dev)
  1168. {
  1169. long e8390_base = dev->base_addr;
  1170. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1171. unsigned char txsr = inb_p(e8390_base+EN0_TSR);
  1172. unsigned char tx_was_aborted = txsr & (ENTSR_ABT+ENTSR_FU);
  1173. #ifdef VERBOSE_ERROR_DUMP
  1174. printk(KERN_DEBUG "%s: transmitter error (%#2x): ", dev->name, txsr);
  1175. if (txsr & ENTSR_ABT)
  1176. printk("excess-collisions ");
  1177. if (txsr & ENTSR_ND)
  1178. printk("non-deferral ");
  1179. if (txsr & ENTSR_CRS)
  1180. printk("lost-carrier ");
  1181. if (txsr & ENTSR_FU)
  1182. printk("FIFO-underrun ");
  1183. if (txsr & ENTSR_CDH)
  1184. printk("lost-heartbeat ");
  1185. printk("\n");
  1186. #endif
  1187. if (tx_was_aborted)
  1188. ei_tx_intr(dev);
  1189. else
  1190. {
  1191. ei_local->stat.tx_errors++;
  1192. if (txsr & ENTSR_CRS) ei_local->stat.tx_carrier_errors++;
  1193. if (txsr & ENTSR_CDH) ei_local->stat.tx_heartbeat_errors++;
  1194. if (txsr & ENTSR_OWC) ei_local->stat.tx_window_errors++;
  1195. }
  1196. }
  1197. /**
  1198. * ei_tx_intr - transmit interrupt handler
  1199. * @dev: network device for which tx intr is handled
  1200. *
  1201. * We have finished a transmit: check for errors and then trigger the next
  1202. * packet to be sent. Called with lock held.
  1203. */
  1204. static void ei_tx_intr(struct net_device *dev)
  1205. {
  1206. long e8390_base = dev->base_addr;
  1207. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1208. int status = inb(e8390_base + EN0_TSR);
  1209. /*
  1210. * There are two Tx buffers, see which one finished, and trigger
  1211. * the send of another one if it exists.
  1212. */
  1213. ei_local->txqueue--;
  1214. if (ei_local->tx1 < 0)
  1215. {
  1216. if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
  1217. printk(KERN_ERR "%s: bogus last_tx_buffer %d, tx1=%d.\n",
  1218. ei_local->name, ei_local->lasttx, ei_local->tx1);
  1219. ei_local->tx1 = 0;
  1220. if (ei_local->tx2 > 0)
  1221. {
  1222. ei_local->txing = 1;
  1223. NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
  1224. dev->trans_start = jiffies;
  1225. ei_local->tx2 = -1,
  1226. ei_local->lasttx = 2;
  1227. }
  1228. else ei_local->lasttx = 20, ei_local->txing = 0;
  1229. }
  1230. else if (ei_local->tx2 < 0)
  1231. {
  1232. if (ei_local->lasttx != 2 && ei_local->lasttx != -2)
  1233. printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
  1234. ei_local->name, ei_local->lasttx, ei_local->tx2);
  1235. ei_local->tx2 = 0;
  1236. if (ei_local->tx1 > 0)
  1237. {
  1238. ei_local->txing = 1;
  1239. NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
  1240. dev->trans_start = jiffies;
  1241. ei_local->tx1 = -1;
  1242. ei_local->lasttx = 1;
  1243. }
  1244. else
  1245. ei_local->lasttx = 10, ei_local->txing = 0;
  1246. }
  1247. // else printk(KERN_WARNING "%s: unexpected TX-done interrupt, lasttx=%d.\n",
  1248. // dev->name, ei_local->lasttx);
  1249. /* Minimize Tx latency: update the statistics after we restart TXing. */
  1250. if (status & ENTSR_COL)
  1251. ei_local->stat.collisions++;
  1252. if (status & ENTSR_PTX)
  1253. ei_local->stat.tx_packets++;
  1254. else
  1255. {
  1256. ei_local->stat.tx_errors++;
  1257. if (status & ENTSR_ABT)
  1258. {
  1259. ei_local->stat.tx_aborted_errors++;
  1260. ei_local->stat.collisions += 16;
  1261. }
  1262. if (status & ENTSR_CRS)
  1263. ei_local->stat.tx_carrier_errors++;
  1264. if (status & ENTSR_FU)
  1265. ei_local->stat.tx_fifo_errors++;
  1266. if (status & ENTSR_CDH)
  1267. ei_local->stat.tx_heartbeat_errors++;
  1268. if (status & ENTSR_OWC)
  1269. ei_local->stat.tx_window_errors++;
  1270. }
  1271. netif_wake_queue(dev);
  1272. }
  1273. /**
  1274. * ei_receive - receive some packets
  1275. * @dev: network device with which receive will be run
  1276. *
  1277. * We have a good packet(s), get it/them out of the buffers.
  1278. * Called with lock held.
  1279. */
  1280. static void ei_receive(struct net_device *dev)
  1281. {
  1282. long e8390_base = dev->base_addr;
  1283. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1284. unsigned char rxing_page, this_frame, next_frame;
  1285. unsigned short current_offset;
  1286. int rx_pkt_count = 0;
  1287. struct e8390_pkt_hdr rx_frame;
  1288. while (++rx_pkt_count < 10)
  1289. {
  1290. int pkt_len, pkt_stat;
  1291. /* Get the rx page (incoming packet pointer). */
  1292. rxing_page = inb_p(e8390_base + EN1_CURPAG -1);
  1293. /* Remove one frame from the ring. Boundary is always a page behind. */
  1294. this_frame = inb_p(e8390_base + EN0_BOUNDARY) + 1;
  1295. if (this_frame >= ei_local->stop_page)
  1296. this_frame = ei_local->rx_start_page;
  1297. /* Someday we'll omit the previous, iff we never get this message.
  1298. (There is at least one clone claimed to have a problem.)
  1299. Keep quiet if it looks like a card removal. One problem here
  1300. is that some clones crash in roughly the same way.
  1301. */
  1302. if (ei_debug > 0 && this_frame != ei_local->current_page && (this_frame!=0x0 || rxing_page!=0xFF))
  1303. printk(KERN_ERR "%s: mismatched read page pointers %2x vs %2x.\n",
  1304. dev->name, this_frame, ei_local->current_page);
  1305. if (this_frame == rxing_page) /* Read all the frames? */
  1306. break; /* Done for now */
  1307. current_offset = this_frame << 8;
  1308. ei_get_8390_hdr(dev, &rx_frame, this_frame);
  1309. pkt_len = rx_frame.count - sizeof(struct e8390_pkt_hdr);
  1310. pkt_stat = rx_frame.status;
  1311. next_frame = this_frame + 1 + ((pkt_len+4)>>8);
  1312. if (pkt_len < 60 || pkt_len > 1518)
  1313. {
  1314. if (ei_debug)
  1315. printk(KERN_DEBUG "%s: bogus packet size: %d, status=%#2x nxpg=%#2x.\n",
  1316. dev->name, rx_frame.count, rx_frame.status,
  1317. rx_frame.next);
  1318. ei_local->stat.rx_errors++;
  1319. ei_local->stat.rx_length_errors++;
  1320. }
  1321. else if ((pkt_stat & 0x0F) == ENRSR_RXOK)
  1322. {
  1323. struct sk_buff *skb;
  1324. skb = dev_alloc_skb(pkt_len+2);
  1325. if (skb == NULL)
  1326. {
  1327. if (ei_debug > 1)
  1328. printk(KERN_DEBUG "%s: Couldn't allocate a sk_buff of size %d.\n",
  1329. dev->name, pkt_len);
  1330. ei_local->stat.rx_dropped++;
  1331. break;
  1332. }
  1333. else
  1334. {
  1335. skb_reserve(skb,2); /* IP headers on 16 byte boundaries */
  1336. skb->dev = dev;
  1337. skb_put(skb, pkt_len); /* Make room */
  1338. ei_block_input(dev, pkt_len, skb, current_offset + sizeof(rx_frame));
  1339. skb->protocol=eth_type_trans(skb,dev);
  1340. netif_rx(skb);
  1341. dev->last_rx = jiffies;
  1342. ei_local->stat.rx_packets++;
  1343. ei_local->stat.rx_bytes += pkt_len;
  1344. if (pkt_stat & ENRSR_PHY)
  1345. ei_local->stat.multicast++;
  1346. }
  1347. }
  1348. else
  1349. {
  1350. if (ei_debug)
  1351. printk(KERN_DEBUG "%s: bogus packet: status=%#2x nxpg=%#2x size=%d\n",
  1352. dev->name, rx_frame.status, rx_frame.next,
  1353. rx_frame.count);
  1354. ei_local->stat.rx_errors++;
  1355. /* NB: The NIC counts CRC, frame and missed errors. */
  1356. if (pkt_stat & ENRSR_FO)
  1357. ei_local->stat.rx_fifo_errors++;
  1358. }
  1359. next_frame = rx_frame.next;
  1360. /* This _should_ never happen: it's here for avoiding bad clones. */
  1361. if (next_frame >= ei_local->stop_page) {
  1362. printk("%s: next frame inconsistency, %#2x\n", dev->name,
  1363. next_frame);
  1364. next_frame = ei_local->rx_start_page;
  1365. }
  1366. ei_local->current_page = next_frame;
  1367. outb_p(next_frame-1, e8390_base+EN0_BOUNDARY);
  1368. }
  1369. return;
  1370. }
  1371. /**
  1372. * ei_rx_overrun - handle receiver overrun
  1373. * @dev: network device which threw exception
  1374. *
  1375. * We have a receiver overrun: we have to kick the 8390 to get it started
  1376. * again. Problem is that you have to kick it exactly as NS prescribes in
  1377. * the updated datasheets, or "the NIC may act in an unpredictable manner."
  1378. * This includes causing "the NIC to defer indefinitely when it is stopped
  1379. * on a busy network." Ugh.
  1380. * Called with lock held. Don't call this with the interrupts off or your
  1381. * computer will hate you - it takes 10ms or so.
  1382. */
  1383. static void ei_rx_overrun(struct net_device *dev)
  1384. {
  1385. axnet_dev_t *info = (axnet_dev_t *)dev;
  1386. long e8390_base = dev->base_addr;
  1387. unsigned char was_txing, must_resend = 0;
  1388. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1389. /*
  1390. * Record whether a Tx was in progress and then issue the
  1391. * stop command.
  1392. */
  1393. was_txing = inb_p(e8390_base+E8390_CMD) & E8390_TRANS;
  1394. outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
  1395. if (ei_debug > 1)
  1396. printk(KERN_DEBUG "%s: Receiver overrun.\n", dev->name);
  1397. ei_local->stat.rx_over_errors++;
  1398. /*
  1399. * Wait a full Tx time (1.2ms) + some guard time, NS says 1.6ms total.
  1400. * Early datasheets said to poll the reset bit, but now they say that
  1401. * it "is not a reliable indicator and subsequently should be ignored."
  1402. * We wait at least 10ms.
  1403. */
  1404. mdelay(10);
  1405. /*
  1406. * Reset RBCR[01] back to zero as per magic incantation.
  1407. */
  1408. outb_p(0x00, e8390_base+EN0_RCNTLO);
  1409. outb_p(0x00, e8390_base+EN0_RCNTHI);
  1410. /*
  1411. * See if any Tx was interrupted or not. According to NS, this
  1412. * step is vital, and skipping it will cause no end of havoc.
  1413. */
  1414. if (was_txing)
  1415. {
  1416. unsigned char tx_completed = inb_p(e8390_base+EN0_ISR) & (ENISR_TX+ENISR_TX_ERR);
  1417. if (!tx_completed)
  1418. must_resend = 1;
  1419. }
  1420. /*
  1421. * Have to enter loopback mode and then restart the NIC before
  1422. * you are allowed to slurp packets up off the ring.
  1423. */
  1424. outb_p(E8390_TXOFF, e8390_base + EN0_TXCR);
  1425. outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, e8390_base + E8390_CMD);
  1426. /*
  1427. * Clear the Rx ring of all the debris, and ack the interrupt.
  1428. */
  1429. ei_receive(dev);
  1430. /*
  1431. * Leave loopback mode, and resend any packet that got stopped.
  1432. */
  1433. outb_p(E8390_TXCONFIG | info->duplex_flag, e8390_base + EN0_TXCR);
  1434. if (must_resend)
  1435. outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START + E8390_TRANS, e8390_base + E8390_CMD);
  1436. }
  1437. /*
  1438. * Collect the stats. This is called unlocked and from several contexts.
  1439. */
  1440. static struct net_device_stats *get_stats(struct net_device *dev)
  1441. {
  1442. long ioaddr = dev->base_addr;
  1443. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1444. unsigned long flags;
  1445. /* If the card is stopped, just return the present stats. */
  1446. if (!netif_running(dev))
  1447. return &ei_local->stat;
  1448. spin_lock_irqsave(&ei_local->page_lock,flags);
  1449. /* Read the counter registers, assuming we are in page 0. */
  1450. ei_local->stat.rx_frame_errors += inb_p(ioaddr + EN0_COUNTER0);
  1451. ei_local->stat.rx_crc_errors += inb_p(ioaddr + EN0_COUNTER1);
  1452. ei_local->stat.rx_missed_errors+= inb_p(ioaddr + EN0_COUNTER2);
  1453. spin_unlock_irqrestore(&ei_local->page_lock, flags);
  1454. return &ei_local->stat;
  1455. }
  1456. /**
  1457. * do_set_multicast_list - set/clear multicast filter
  1458. * @dev: net device for which multicast filter is adjusted
  1459. *
  1460. * Set or clear the multicast filter for this adaptor. May be called
  1461. * from a BH in 2.1.x. Must be called with lock held.
  1462. */
  1463. static void do_set_multicast_list(struct net_device *dev)
  1464. {
  1465. long e8390_base = dev->base_addr;
  1466. if(dev->flags&IFF_PROMISC)
  1467. outb_p(E8390_RXCONFIG | 0x58, e8390_base + EN0_RXCR);
  1468. else if(dev->flags&IFF_ALLMULTI || dev->mc_list)
  1469. outb_p(E8390_RXCONFIG | 0x48, e8390_base + EN0_RXCR);
  1470. else
  1471. outb_p(E8390_RXCONFIG | 0x40, e8390_base + EN0_RXCR);
  1472. }
  1473. /*
  1474. * Called without lock held. This is invoked from user context and may
  1475. * be parallel to just about everything else. Its also fairly quick and
  1476. * not called too often. Must protect against both bh and irq users
  1477. */
  1478. static void set_multicast_list(struct net_device *dev)
  1479. {
  1480. unsigned long flags;
  1481. spin_lock_irqsave(&dev_lock(dev), flags);
  1482. do_set_multicast_list(dev);
  1483. spin_unlock_irqrestore(&dev_lock(dev), flags);
  1484. }
  1485. /**
  1486. * axdev_setup - init rest of 8390 device struct
  1487. * @dev: network device structure to init
  1488. *
  1489. * Initialize the rest of the 8390 device structure. Do NOT __init
  1490. * this, as it is used by 8390 based modular drivers too.
  1491. */
  1492. static void axdev_setup(struct net_device *dev)
  1493. {
  1494. struct ei_device *ei_local;
  1495. if (ei_debug > 1)
  1496. printk(version_8390);
  1497. SET_MODULE_OWNER(dev);
  1498. ei_local = (struct ei_device *)netdev_priv(dev);
  1499. spin_lock_init(&ei_local->page_lock);
  1500. dev->hard_start_xmit = &ei_start_xmit;
  1501. dev->get_stats = get_stats;
  1502. dev->set_multicast_list = &set_multicast_list;
  1503. ether_setup(dev);
  1504. }
  1505. /* This page of functions should be 8390 generic */
  1506. /* Follow National Semi's recommendations for initializing the "NIC". */
  1507. /**
  1508. * AX88190_init - initialize 8390 hardware
  1509. * @dev: network device to initialize
  1510. * @startp: boolean. non-zero value to initiate chip processing
  1511. *
  1512. * Must be called with lock held.
  1513. */
  1514. static void AX88190_init(struct net_device *dev, int startp)
  1515. {
  1516. axnet_dev_t *info = PRIV(dev);
  1517. long e8390_base = dev->base_addr;
  1518. struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
  1519. int i;
  1520. int endcfg = ei_local->word16 ? (0x48 | ENDCFG_WTS) : 0x48;
  1521. if(sizeof(struct e8390_pkt_hdr)!=4)
  1522. panic("8390.c: header struct mispacked\n");
  1523. /* Follow National Semi's recommendations for initing the DP83902. */
  1524. outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD); /* 0x21 */
  1525. outb_p(endcfg, e8390_base + EN0_DCFG); /* 0x48 or 0x49 */
  1526. /* Clear the remote byte count registers. */
  1527. outb_p(0x00, e8390_base + EN0_RCNTLO);
  1528. outb_p(0x00, e8390_base + EN0_RCNTHI);
  1529. /* Set to monitor and loopback mode -- this is vital!. */
  1530. outb_p(E8390_RXOFF|0x40, e8390_base + EN0_RXCR); /* 0x60 */
  1531. outb_p(E8390_TXOFF, e8390_base + EN0_TXCR); /* 0x02 */
  1532. /* Set the transmit page and receive ring. */
  1533. outb_p(ei_local->tx_start_page, e8390_base + EN0_TPSR);
  1534. ei_local->tx1 = ei_local->tx2 = 0;
  1535. outb_p(ei_local->rx_start_page, e8390_base + EN0_STARTPG);
  1536. outb_p(ei_local->stop_page-1, e8390_base + EN0_BOUNDARY); /* 3c503 says 0x3f,NS0x26*/
  1537. ei_local->current_page = ei_local->rx_start_page; /* assert boundary+1 */
  1538. outb_p(ei_local->stop_page, e8390_base + EN0_STOPPG);
  1539. /* Clear the pending interrupts and mask. */
  1540. outb_p(0xFF, e8390_base + EN0_ISR);
  1541. outb_p(0x00, e8390_base + EN0_IMR);
  1542. /* Copy the station address into the DS8390 registers. */
  1543. outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP, e8390_base+E8390_CMD); /* 0x61 */
  1544. for(i = 0; i < 6; i++)
  1545. {
  1546. outb_p(dev->dev_addr[i], e8390_base + EN1_PHYS_SHIFT(i));
  1547. if(inb_p(e8390_base + EN1_PHYS_SHIFT(i))!=dev->dev_addr[i])
  1548. printk(KERN_ERR "Hw. address read/write mismap %d\n",i);
  1549. }
  1550. /*
  1551. * Initialize the multicast list to accept-all. If we enable multicast
  1552. * the higher levels can do the filtering.
  1553. */
  1554. for (i = 0; i < 8; i++)
  1555. outb_p(0xff, e8390_base + EN1_MULT + i);
  1556. outb_p(ei_local->rx_start_page, e8390_base + EN1_CURPAG);
  1557. outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
  1558. netif_start_queue(dev);
  1559. ei_local->tx1 = ei_local->tx2 = 0;
  1560. ei_local->txing = 0;
  1561. if (startp)
  1562. {
  1563. outb_p(0xff, e8390_base + EN0_ISR);
  1564. outb_p(ENISR_ALL, e8390_base + EN0_IMR);
  1565. outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base+E8390_CMD);
  1566. outb_p(E8390_TXCONFIG | info->duplex_flag,
  1567. e8390_base + EN0_TXCR); /* xmit on. */
  1568. /* 3c503 TechMan says rxconfig only after the NIC is started. */
  1569. outb_p(E8390_RXCONFIG | 0x40, e8390_base + EN0_RXCR); /* rx on, */
  1570. do_set_multicast_list(dev); /* (re)load the mcast table */
  1571. }
  1572. }
  1573. /* Trigger a transmit start, assuming the length is valid.
  1574. Always called with the page lock held */
  1575. static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
  1576. int start_page)
  1577. {
  1578. long e8390_base = dev->base_addr;
  1579. struct ei_device *ei_local __attribute((unused)) = (struct ei_device *) netdev_priv(dev);
  1580. if (inb_p(e8390_base) & E8390_TRANS)
  1581. {
  1582. printk(KERN_WARNING "%s: trigger_send() called with the transmitter busy.\n",
  1583. dev->name);
  1584. return;
  1585. }
  1586. outb_p(length & 0xff, e8390_base + EN0_TCNTLO);
  1587. outb_p(length >> 8, e8390_base + EN0_TCNTHI);
  1588. outb_p(start_page, e8390_base + EN0_TPSR);
  1589. outb_p(E8390_NODMA+E8390_TRANS+E8390_START, e8390_base+E8390_CMD);
  1590. }