atp.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /* atp.c: Attached (pocket) ethernet adapter driver for linux. */
  2. /*
  3. This is a driver for commonly OEM pocket (parallel port)
  4. ethernet adapters based on the Realtek RTL8002 and RTL8012 chips.
  5. Written 1993-2000 by Donald Becker.
  6. This software may be used and distributed according to the terms of
  7. the GNU General Public License (GPL), incorporated herein by reference.
  8. Drivers based on or derived from this code fall under the GPL and must
  9. retain the authorship, copyright and license notice. This file is not
  10. a complete program and may only be used when the entire operating
  11. system is licensed under the GPL.
  12. Copyright 1993 United States Government as represented by the Director,
  13. National Security Agency. Copyright 1994-2000 retained by the original
  14. author, Donald Becker. The timer-based reset code was supplied in 1995
  15. by Bill Carlson, wwc@super.org.
  16. The author may be reached as becker@scyld.com, or C/O
  17. Scyld Computing Corporation
  18. 410 Severn Ave., Suite 210
  19. Annapolis MD 21403
  20. Support information and updates available at
  21. http://www.scyld.com/network/atp.html
  22. Modular support/softnet added by Alan Cox.
  23. _bit abuse fixed up by Alan Cox
  24. */
  25. static const char version[] =
  26. "atp.c:v1.09=ac 2002/10/01 Donald Becker <becker@scyld.com>\n";
  27. /* The user-configurable values.
  28. These may be modified when a driver module is loaded.*/
  29. static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
  30. #define net_debug debug
  31. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  32. static int max_interrupt_work = 15;
  33. #define NUM_UNITS 2
  34. /* The standard set of ISA module parameters. */
  35. static int io[NUM_UNITS];
  36. static int irq[NUM_UNITS];
  37. static int xcvr[NUM_UNITS]; /* The data transfer mode. */
  38. /* Operational parameters that are set at compile time. */
  39. /* Time in jiffies before concluding the transmitter is hung. */
  40. #define TX_TIMEOUT (400*HZ/1000)
  41. /*
  42. This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
  43. ethernet adapter. This is a common low-cost OEM pocket ethernet
  44. adapter, sold under many names.
  45. Sources:
  46. This driver was written from the packet driver assembly code provided by
  47. Vincent Bono of AT-Lan-Tec. Ever try to figure out how a complicated
  48. device works just from the assembly code? It ain't pretty. The following
  49. description is written based on guesses and writing lots of special-purpose
  50. code to test my theorized operation.
  51. In 1997 Realtek made available the documentation for the second generation
  52. RTL8012 chip, which has lead to several driver improvements.
  53. http://www.realtek.com.tw/
  54. Theory of Operation
  55. The RTL8002 adapter seems to be built around a custom spin of the SEEQ
  56. controller core. It probably has a 16K or 64K internal packet buffer, of
  57. which the first 4K is devoted to transmit and the rest to receive.
  58. The controller maintains the queue of received packet and the packet buffer
  59. access pointer internally, with only 'reset to beginning' and 'skip to next
  60. packet' commands visible. The transmit packet queue holds two (or more?)
  61. packets: both 'retransmit this packet' (due to collision) and 'transmit next
  62. packet' commands must be started by hand.
  63. The station address is stored in a standard bit-serial EEPROM which must be
  64. read (ughh) by the device driver. (Provisions have been made for
  65. substituting a 74S288 PROM, but I haven't gotten reports of any models
  66. using it.) Unlike built-in devices, a pocket adapter can temporarily lose
  67. power without indication to the device driver. The major effect is that
  68. the station address, receive filter (promiscuous, etc.) and transceiver
  69. must be reset.
  70. The controller itself has 16 registers, some of which use only the lower
  71. bits. The registers are read and written 4 bits at a time. The four bit
  72. register address is presented on the data lines along with a few additional
  73. timing and control bits. The data is then read from status port or written
  74. to the data port.
  75. Correction: the controller has two banks of 16 registers. The second
  76. bank contains only the multicast filter table (now used) and the EEPROM
  77. access registers.
  78. Since the bulk data transfer of the actual packets through the slow
  79. parallel port dominates the driver's running time, four distinct data
  80. (non-register) transfer modes are provided by the adapter, two in each
  81. direction. In the first mode timing for the nibble transfers is
  82. provided through the data port. In the second mode the same timing is
  83. provided through the control port. In either case the data is read from
  84. the status port and written to the data port, just as it is accessing
  85. registers.
  86. In addition to the basic data transfer methods, several more are modes are
  87. created by adding some delay by doing multiple reads of the data to allow
  88. it to stabilize. This delay seems to be needed on most machines.
  89. The data transfer mode is stored in the 'dev->if_port' field. Its default
  90. value is '4'. It may be overridden at boot-time using the third parameter
  91. to the "ether=..." initialization.
  92. The header file <atp.h> provides inline functions that encapsulate the
  93. register and data access methods. These functions are hand-tuned to
  94. generate reasonable object code. This header file also documents my
  95. interpretations of the device registers.
  96. */
  97. #include <linux/kernel.h>
  98. #include <linux/module.h>
  99. #include <linux/types.h>
  100. #include <linux/fcntl.h>
  101. #include <linux/interrupt.h>
  102. #include <linux/ioport.h>
  103. #include <linux/in.h>
  104. #include <linux/string.h>
  105. #include <linux/errno.h>
  106. #include <linux/init.h>
  107. #include <linux/crc32.h>
  108. #include <linux/netdevice.h>
  109. #include <linux/etherdevice.h>
  110. #include <linux/skbuff.h>
  111. #include <linux/spinlock.h>
  112. #include <linux/delay.h>
  113. #include <linux/bitops.h>
  114. #include <asm/system.h>
  115. #include <asm/io.h>
  116. #include <asm/dma.h>
  117. #include "atp.h"
  118. MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  119. MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
  120. MODULE_LICENSE("GPL");
  121. module_param(max_interrupt_work, int, 0);
  122. module_param(debug, int, 0);
  123. module_param_array(io, int, NULL, 0);
  124. module_param_array(irq, int, NULL, 0);
  125. module_param_array(xcvr, int, NULL, 0);
  126. MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
  127. MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
  128. MODULE_PARM_DESC(io, "ATP I/O base address(es)");
  129. MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
  130. MODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)");
  131. /* The number of low I/O ports used by the ethercard. */
  132. #define ETHERCARD_TOTAL_SIZE 3
  133. /* Sequence to switch an 8012 from printer mux to ethernet mode. */
  134. static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
  135. struct net_local {
  136. spinlock_t lock;
  137. struct net_device *next_module;
  138. struct timer_list timer; /* Media selection timer. */
  139. long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */
  140. int saved_tx_size;
  141. unsigned int tx_unit_busy:1;
  142. unsigned char re_tx, /* Number of packet retransmissions. */
  143. addr_mode, /* Current Rx filter e.g. promiscuous, etc. */
  144. pac_cnt_in_tx_buf,
  145. chip_type;
  146. };
  147. /* This code, written by wwc@super.org, resets the adapter every
  148. TIMED_CHECKER ticks. This recovers from an unknown error which
  149. hangs the device. */
  150. #define TIMED_CHECKER (HZ/4)
  151. #ifdef TIMED_CHECKER
  152. #include <linux/timer.h>
  153. static void atp_timed_checker(unsigned long ignored);
  154. #endif
  155. /* Index to functions, as function prototypes. */
  156. static int atp_probe1(long ioaddr);
  157. static void get_node_ID(struct net_device *dev);
  158. static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
  159. static int net_open(struct net_device *dev);
  160. static void hardware_init(struct net_device *dev);
  161. static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
  162. static void trigger_send(long ioaddr, int length);
  163. static netdev_tx_t atp_send_packet(struct sk_buff *skb,
  164. struct net_device *dev);
  165. static irqreturn_t atp_interrupt(int irq, void *dev_id);
  166. static void net_rx(struct net_device *dev);
  167. static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
  168. static int net_close(struct net_device *dev);
  169. static void set_rx_mode(struct net_device *dev);
  170. static void tx_timeout(struct net_device *dev);
  171. /* A list of all installed ATP devices, for removing the driver module. */
  172. static struct net_device *root_atp_dev;
  173. /* Check for a network adapter of this type, and return '0' iff one exists.
  174. If dev->base_addr == 0, probe all likely locations.
  175. If dev->base_addr == 1, always return failure.
  176. If dev->base_addr == 2, allocate space for the device and return success
  177. (detachable devices only).
  178. FIXME: we should use the parport layer for this
  179. */
  180. static int __init atp_init(void)
  181. {
  182. int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
  183. int base_addr = io[0];
  184. if (base_addr > 0x1ff) /* Check a single specified location. */
  185. return atp_probe1(base_addr);
  186. else if (base_addr == 1) /* Don't probe at all. */
  187. return -ENXIO;
  188. for (port = ports; *port; port++) {
  189. long ioaddr = *port;
  190. outb(0x57, ioaddr + PAR_DATA);
  191. if (inb(ioaddr + PAR_DATA) != 0x57)
  192. continue;
  193. if (atp_probe1(ioaddr) == 0)
  194. return 0;
  195. }
  196. return -ENODEV;
  197. }
  198. static const struct net_device_ops atp_netdev_ops = {
  199. .ndo_open = net_open,
  200. .ndo_stop = net_close,
  201. .ndo_start_xmit = atp_send_packet,
  202. .ndo_set_multicast_list = set_rx_mode,
  203. .ndo_tx_timeout = tx_timeout,
  204. .ndo_change_mtu = eth_change_mtu,
  205. .ndo_set_mac_address = eth_mac_addr,
  206. .ndo_validate_addr = eth_validate_addr,
  207. };
  208. static int __init atp_probe1(long ioaddr)
  209. {
  210. struct net_device *dev = NULL;
  211. struct net_local *lp;
  212. int saved_ctrl_reg, status, i;
  213. int res;
  214. outb(0xff, ioaddr + PAR_DATA);
  215. /* Save the original value of the Control register, in case we guessed
  216. wrong. */
  217. saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
  218. if (net_debug > 3)
  219. printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
  220. /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
  221. outb(0x04, ioaddr + PAR_CONTROL);
  222. #ifndef final_version
  223. if (net_debug > 3) {
  224. /* Turn off the printer multiplexer on the 8012. */
  225. for (i = 0; i < 8; i++)
  226. outb(mux_8012[i], ioaddr + PAR_DATA);
  227. write_reg(ioaddr, MODSEL, 0x00);
  228. printk("atp: Registers are ");
  229. for (i = 0; i < 32; i++)
  230. printk(" %2.2x", read_nibble(ioaddr, i));
  231. printk(".\n");
  232. }
  233. #endif
  234. /* Turn off the printer multiplexer on the 8012. */
  235. for (i = 0; i < 8; i++)
  236. outb(mux_8012[i], ioaddr + PAR_DATA);
  237. write_reg_high(ioaddr, CMR1, CMR1h_RESET);
  238. /* udelay() here? */
  239. status = read_nibble(ioaddr, CMR1);
  240. if (net_debug > 3) {
  241. printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
  242. for (i = 0; i < 32; i++)
  243. printk(" %2.2x", read_nibble(ioaddr, i));
  244. printk("\n");
  245. }
  246. if ((status & 0x78) != 0x08) {
  247. /* The pocket adapter probe failed, restore the control register. */
  248. outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
  249. return -ENODEV;
  250. }
  251. status = read_nibble(ioaddr, CMR2_h);
  252. if ((status & 0x78) != 0x10) {
  253. outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
  254. return -ENODEV;
  255. }
  256. dev = alloc_etherdev(sizeof(struct net_local));
  257. if (!dev)
  258. return -ENOMEM;
  259. /* Find the IRQ used by triggering an interrupt. */
  260. write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
  261. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
  262. /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
  263. if (irq[0])
  264. dev->irq = irq[0];
  265. else if (ioaddr == 0x378)
  266. dev->irq = 7;
  267. else
  268. dev->irq = 5;
  269. write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
  270. write_reg(ioaddr, CMR2, CMR2_NULL);
  271. dev->base_addr = ioaddr;
  272. /* Read the station address PROM. */
  273. get_node_ID(dev);
  274. #ifndef MODULE
  275. if (net_debug)
  276. printk(KERN_INFO "%s", version);
  277. #endif
  278. printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
  279. "SAPROM %pM.\n",
  280. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  281. /* Reset the ethernet hardware and activate the printer pass-through. */
  282. write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
  283. lp = netdev_priv(dev);
  284. lp->chip_type = RTL8002;
  285. lp->addr_mode = CMR2h_Normal;
  286. spin_lock_init(&lp->lock);
  287. /* For the ATP adapter the "if_port" is really the data transfer mode. */
  288. if (xcvr[0])
  289. dev->if_port = xcvr[0];
  290. else
  291. dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
  292. if (dev->mem_end & 0xf)
  293. net_debug = dev->mem_end & 7;
  294. dev->netdev_ops = &atp_netdev_ops;
  295. dev->watchdog_timeo = TX_TIMEOUT;
  296. res = register_netdev(dev);
  297. if (res) {
  298. free_netdev(dev);
  299. return res;
  300. }
  301. lp->next_module = root_atp_dev;
  302. root_atp_dev = dev;
  303. return 0;
  304. }
  305. /* Read the station address PROM, usually a word-wide EEPROM. */
  306. static void __init get_node_ID(struct net_device *dev)
  307. {
  308. long ioaddr = dev->base_addr;
  309. int sa_offset = 0;
  310. int i;
  311. write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
  312. /* Some adapters have the station address at offset 15 instead of offset
  313. zero. Check for it, and fix it if needed. */
  314. if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
  315. sa_offset = 15;
  316. for (i = 0; i < 3; i++)
  317. ((__be16 *)dev->dev_addr)[i] =
  318. cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
  319. write_reg(ioaddr, CMR2, CMR2_NULL);
  320. }
  321. /*
  322. An EEPROM read command starts by shifting out 0x60+address, and then
  323. shifting in the serial data. See the NatSemi databook for details.
  324. * ________________
  325. * CS : __|
  326. * ___ ___
  327. * CLK: ______| |___| |
  328. * __ _______ _______
  329. * DI : __X_______X_______X
  330. * DO : _________X_______X
  331. */
  332. static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
  333. {
  334. unsigned eedata_out = 0;
  335. int num_bits = EE_CMD_SIZE;
  336. while (--num_bits >= 0) {
  337. char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
  338. write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
  339. write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
  340. eedata_out <<= 1;
  341. if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
  342. eedata_out++;
  343. }
  344. write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
  345. return eedata_out;
  346. }
  347. /* Open/initialize the board. This is called (in the current kernel)
  348. sometime after booting when the 'ifconfig' program is run.
  349. This routine sets everything up anew at each open, even
  350. registers that "should" only need to be set once at boot, so that
  351. there is non-reboot way to recover if something goes wrong.
  352. This is an attachable device: if there is no private entry then it wasn't
  353. probed for at boot-time, and we need to probe for it again.
  354. */
  355. static int net_open(struct net_device *dev)
  356. {
  357. struct net_local *lp = netdev_priv(dev);
  358. int ret;
  359. /* The interrupt line is turned off (tri-stated) when the device isn't in
  360. use. That's especially important for "attached" interfaces where the
  361. port or interrupt may be shared. */
  362. ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev);
  363. if (ret)
  364. return ret;
  365. hardware_init(dev);
  366. init_timer(&lp->timer);
  367. lp->timer.expires = jiffies + TIMED_CHECKER;
  368. lp->timer.data = (unsigned long)dev;
  369. lp->timer.function = atp_timed_checker; /* timer handler */
  370. add_timer(&lp->timer);
  371. netif_start_queue(dev);
  372. return 0;
  373. }
  374. /* This routine resets the hardware. We initialize everything, assuming that
  375. the hardware may have been temporarily detached. */
  376. static void hardware_init(struct net_device *dev)
  377. {
  378. struct net_local *lp = netdev_priv(dev);
  379. long ioaddr = dev->base_addr;
  380. int i;
  381. /* Turn off the printer multiplexer on the 8012. */
  382. for (i = 0; i < 8; i++)
  383. outb(mux_8012[i], ioaddr + PAR_DATA);
  384. write_reg_high(ioaddr, CMR1, CMR1h_RESET);
  385. for (i = 0; i < 6; i++)
  386. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  387. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  388. if (net_debug > 2) {
  389. printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
  390. (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
  391. }
  392. write_reg(ioaddr, CMR2, CMR2_IRQOUT);
  393. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
  394. /* Enable the interrupt line from the serial port. */
  395. outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  396. /* Unmask the interesting interrupts. */
  397. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  398. write_reg_high(ioaddr, IMR, ISRh_RxErr);
  399. lp->tx_unit_busy = 0;
  400. lp->pac_cnt_in_tx_buf = 0;
  401. lp->saved_tx_size = 0;
  402. }
  403. static void trigger_send(long ioaddr, int length)
  404. {
  405. write_reg_byte(ioaddr, TxCNT0, length & 0xff);
  406. write_reg(ioaddr, TxCNT1, length >> 8);
  407. write_reg(ioaddr, CMR1, CMR1_Xmit);
  408. }
  409. static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
  410. {
  411. if (length & 1)
  412. {
  413. length++;
  414. pad_len++;
  415. }
  416. outb(EOC+MAR, ioaddr + PAR_DATA);
  417. if ((data_mode & 1) == 0) {
  418. /* Write the packet out, starting with the write addr. */
  419. outb(WrAddr+MAR, ioaddr + PAR_DATA);
  420. do {
  421. write_byte_mode0(ioaddr, *packet++);
  422. } while (--length > pad_len) ;
  423. do {
  424. write_byte_mode0(ioaddr, 0);
  425. } while (--length > 0) ;
  426. } else {
  427. /* Write the packet out in slow mode. */
  428. unsigned char outbyte = *packet++;
  429. outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  430. outb(WrAddr+MAR, ioaddr + PAR_DATA);
  431. outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
  432. outb(outbyte & 0x0f, ioaddr + PAR_DATA);
  433. outbyte >>= 4;
  434. outb(outbyte & 0x0f, ioaddr + PAR_DATA);
  435. outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  436. while (--length > pad_len)
  437. write_byte_mode1(ioaddr, *packet++);
  438. while (--length > 0)
  439. write_byte_mode1(ioaddr, 0);
  440. }
  441. /* Terminate the Tx frame. End of write: ECB. */
  442. outb(0xff, ioaddr + PAR_DATA);
  443. outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  444. }
  445. static void tx_timeout(struct net_device *dev)
  446. {
  447. long ioaddr = dev->base_addr;
  448. printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
  449. inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
  450. : "IRQ conflict");
  451. dev->stats.tx_errors++;
  452. /* Try to restart the adapter. */
  453. hardware_init(dev);
  454. dev->trans_start = jiffies; /* prevent tx timeout */
  455. netif_wake_queue(dev);
  456. dev->stats.tx_errors++;
  457. }
  458. static netdev_tx_t atp_send_packet(struct sk_buff *skb,
  459. struct net_device *dev)
  460. {
  461. struct net_local *lp = netdev_priv(dev);
  462. long ioaddr = dev->base_addr;
  463. int length;
  464. unsigned long flags;
  465. length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  466. netif_stop_queue(dev);
  467. /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
  468. This sequence must not be interrupted by an incoming packet. */
  469. spin_lock_irqsave(&lp->lock, flags);
  470. write_reg(ioaddr, IMR, 0);
  471. write_reg_high(ioaddr, IMR, 0);
  472. spin_unlock_irqrestore(&lp->lock, flags);
  473. write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
  474. lp->pac_cnt_in_tx_buf++;
  475. if (lp->tx_unit_busy == 0) {
  476. trigger_send(ioaddr, length);
  477. lp->saved_tx_size = 0; /* Redundant */
  478. lp->re_tx = 0;
  479. lp->tx_unit_busy = 1;
  480. } else
  481. lp->saved_tx_size = length;
  482. /* Re-enable the LPT interrupts. */
  483. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  484. write_reg_high(ioaddr, IMR, ISRh_RxErr);
  485. dev_kfree_skb (skb);
  486. return NETDEV_TX_OK;
  487. }
  488. /* The typical workload of the driver:
  489. Handle the network interface interrupts. */
  490. static irqreturn_t atp_interrupt(int irq, void *dev_instance)
  491. {
  492. struct net_device *dev = dev_instance;
  493. struct net_local *lp;
  494. long ioaddr;
  495. static int num_tx_since_rx;
  496. int boguscount = max_interrupt_work;
  497. int handled = 0;
  498. ioaddr = dev->base_addr;
  499. lp = netdev_priv(dev);
  500. spin_lock(&lp->lock);
  501. /* Disable additional spurious interrupts. */
  502. outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
  503. /* The adapter's output is currently the IRQ line, switch it to data. */
  504. write_reg(ioaddr, CMR2, CMR2_NULL);
  505. write_reg(ioaddr, IMR, 0);
  506. if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
  507. while (--boguscount > 0) {
  508. int status = read_nibble(ioaddr, ISR);
  509. if (net_debug > 5) printk("loop status %02x..", status);
  510. if (status & (ISR_RxOK<<3)) {
  511. handled = 1;
  512. write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
  513. do {
  514. int read_status = read_nibble(ioaddr, CMR1);
  515. if (net_debug > 6)
  516. printk("handling Rx packet %02x..", read_status);
  517. /* We acknowledged the normal Rx interrupt, so if the interrupt
  518. is still outstanding we must have a Rx error. */
  519. if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
  520. dev->stats.rx_over_errors++;
  521. /* Set to no-accept mode long enough to remove a packet. */
  522. write_reg_high(ioaddr, CMR2, CMR2h_OFF);
  523. net_rx(dev);
  524. /* Clear the interrupt and return to normal Rx mode. */
  525. write_reg_high(ioaddr, ISR, ISRh_RxErr);
  526. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  527. } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
  528. net_rx(dev);
  529. num_tx_since_rx = 0;
  530. } else
  531. break;
  532. } while (--boguscount > 0);
  533. } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
  534. handled = 1;
  535. if (net_debug > 6) printk("handling Tx done..");
  536. /* Clear the Tx interrupt. We should check for too many failures
  537. and reinitialize the adapter. */
  538. write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
  539. if (status & (ISR_TxErr<<3)) {
  540. dev->stats.collisions++;
  541. if (++lp->re_tx > 15) {
  542. dev->stats.tx_aborted_errors++;
  543. hardware_init(dev);
  544. break;
  545. }
  546. /* Attempt to retransmit. */
  547. if (net_debug > 6) printk("attempting to ReTx");
  548. write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
  549. } else {
  550. /* Finish up the transmit. */
  551. dev->stats.tx_packets++;
  552. lp->pac_cnt_in_tx_buf--;
  553. if ( lp->saved_tx_size) {
  554. trigger_send(ioaddr, lp->saved_tx_size);
  555. lp->saved_tx_size = 0;
  556. lp->re_tx = 0;
  557. } else
  558. lp->tx_unit_busy = 0;
  559. netif_wake_queue(dev); /* Inform upper layers. */
  560. }
  561. num_tx_since_rx++;
  562. } else if (num_tx_since_rx > 8 &&
  563. time_after(jiffies, dev->last_rx + HZ)) {
  564. if (net_debug > 2)
  565. printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
  566. "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
  567. num_tx_since_rx, jiffies - dev->last_rx, status,
  568. (read_nibble(ioaddr, CMR1) >> 3) & 15);
  569. dev->stats.rx_missed_errors++;
  570. hardware_init(dev);
  571. num_tx_since_rx = 0;
  572. break;
  573. } else
  574. break;
  575. }
  576. /* This following code fixes a rare (and very difficult to track down)
  577. problem where the adapter forgets its ethernet address. */
  578. {
  579. int i;
  580. for (i = 0; i < 6; i++)
  581. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  582. #if 0 && defined(TIMED_CHECKER)
  583. mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
  584. #endif
  585. }
  586. /* Tell the adapter that it can go back to using the output line as IRQ. */
  587. write_reg(ioaddr, CMR2, CMR2_IRQOUT);
  588. /* Enable the physical interrupt line, which is sure to be low until.. */
  589. outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  590. /* .. we enable the interrupt sources. */
  591. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  592. write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
  593. spin_unlock(&lp->lock);
  594. if (net_debug > 5) printk("exiting interrupt.\n");
  595. return IRQ_RETVAL(handled);
  596. }
  597. #ifdef TIMED_CHECKER
  598. /* This following code fixes a rare (and very difficult to track down)
  599. problem where the adapter forgets its ethernet address. */
  600. static void atp_timed_checker(unsigned long data)
  601. {
  602. struct net_device *dev = (struct net_device *)data;
  603. long ioaddr = dev->base_addr;
  604. struct net_local *lp = netdev_priv(dev);
  605. int tickssofar = jiffies - lp->last_rx_time;
  606. int i;
  607. spin_lock(&lp->lock);
  608. if (tickssofar > 2*HZ) {
  609. #if 1
  610. for (i = 0; i < 6; i++)
  611. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  612. lp->last_rx_time = jiffies;
  613. #else
  614. for (i = 0; i < 6; i++)
  615. if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
  616. {
  617. struct net_local *lp = netdev_priv(atp_timed_dev);
  618. write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
  619. if (i == 2)
  620. dev->stats.tx_errors++;
  621. else if (i == 3)
  622. dev->stats.tx_dropped++;
  623. else if (i == 4)
  624. dev->stats.collisions++;
  625. else
  626. dev->stats.rx_errors++;
  627. }
  628. #endif
  629. }
  630. spin_unlock(&lp->lock);
  631. lp->timer.expires = jiffies + TIMED_CHECKER;
  632. add_timer(&lp->timer);
  633. }
  634. #endif
  635. /* We have a good packet(s), get it/them out of the buffers. */
  636. static void net_rx(struct net_device *dev)
  637. {
  638. struct net_local *lp = netdev_priv(dev);
  639. long ioaddr = dev->base_addr;
  640. struct rx_header rx_head;
  641. /* Process the received packet. */
  642. outb(EOC+MAR, ioaddr + PAR_DATA);
  643. read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
  644. if (net_debug > 5)
  645. printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
  646. rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
  647. if ((rx_head.rx_status & 0x77) != 0x01) {
  648. dev->stats.rx_errors++;
  649. if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
  650. else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
  651. if (net_debug > 3)
  652. printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
  653. dev->name, rx_head.rx_status);
  654. if (rx_head.rx_status & 0x0020) {
  655. dev->stats.rx_fifo_errors++;
  656. write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
  657. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
  658. } else if (rx_head.rx_status & 0x0050)
  659. hardware_init(dev);
  660. return;
  661. } else {
  662. /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
  663. int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
  664. struct sk_buff *skb;
  665. skb = dev_alloc_skb(pkt_len + 2);
  666. if (skb == NULL) {
  667. printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
  668. dev->name);
  669. dev->stats.rx_dropped++;
  670. goto done;
  671. }
  672. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  673. read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
  674. skb->protocol = eth_type_trans(skb, dev);
  675. netif_rx(skb);
  676. dev->last_rx = jiffies;
  677. dev->stats.rx_packets++;
  678. dev->stats.rx_bytes += pkt_len;
  679. }
  680. done:
  681. write_reg(ioaddr, CMR1, CMR1_NextPkt);
  682. lp->last_rx_time = jiffies;
  683. }
  684. static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
  685. {
  686. if (data_mode <= 3) { /* Mode 0 or 1 */
  687. outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
  688. outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
  689. ioaddr + PAR_DATA);
  690. if (data_mode <= 1) { /* Mode 0 or 1 */
  691. do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0);
  692. } else { /* Mode 2 or 3 */
  693. do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0);
  694. }
  695. } else if (data_mode <= 5) {
  696. do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0);
  697. } else {
  698. do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0);
  699. }
  700. outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
  701. outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
  702. }
  703. /* The inverse routine to net_open(). */
  704. static int
  705. net_close(struct net_device *dev)
  706. {
  707. struct net_local *lp = netdev_priv(dev);
  708. long ioaddr = dev->base_addr;
  709. netif_stop_queue(dev);
  710. del_timer_sync(&lp->timer);
  711. /* Flush the Tx and disable Rx here. */
  712. lp->addr_mode = CMR2h_OFF;
  713. write_reg_high(ioaddr, CMR2, CMR2h_OFF);
  714. /* Free the IRQ line. */
  715. outb(0x00, ioaddr + PAR_CONTROL);
  716. free_irq(dev->irq, dev);
  717. /* Reset the ethernet hardware and activate the printer pass-through. */
  718. write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
  719. return 0;
  720. }
  721. /*
  722. * Set or clear the multicast filter for this adapter.
  723. */
  724. static void set_rx_mode_8002(struct net_device *dev)
  725. {
  726. struct net_local *lp = netdev_priv(dev);
  727. long ioaddr = dev->base_addr;
  728. if (!netdev_mc_empty(dev) || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
  729. lp->addr_mode = CMR2h_PROMISC;
  730. else
  731. lp->addr_mode = CMR2h_Normal;
  732. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  733. }
  734. static void set_rx_mode_8012(struct net_device *dev)
  735. {
  736. struct net_local *lp = netdev_priv(dev);
  737. long ioaddr = dev->base_addr;
  738. unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
  739. int i;
  740. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  741. new_mode = CMR2h_PROMISC;
  742. } else if ((netdev_mc_count(dev) > 1000) ||
  743. (dev->flags & IFF_ALLMULTI)) {
  744. /* Too many to filter perfectly -- accept all multicasts. */
  745. memset(mc_filter, 0xff, sizeof(mc_filter));
  746. new_mode = CMR2h_Normal;
  747. } else {
  748. struct netdev_hw_addr *ha;
  749. memset(mc_filter, 0, sizeof(mc_filter));
  750. netdev_for_each_mc_addr(ha, dev) {
  751. int filterbit = ether_crc_le(ETH_ALEN, ha->addr) & 0x3f;
  752. mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
  753. }
  754. new_mode = CMR2h_Normal;
  755. }
  756. lp->addr_mode = new_mode;
  757. write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
  758. for (i = 0; i < 8; i++)
  759. write_reg_byte(ioaddr, i, mc_filter[i]);
  760. if (net_debug > 2 || 1) {
  761. lp->addr_mode = 1;
  762. printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
  763. dev->name, lp->addr_mode);
  764. for (i = 0; i < 8; i++)
  765. printk(" %2.2x", mc_filter[i]);
  766. printk(".\n");
  767. }
  768. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  769. write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
  770. }
  771. static void set_rx_mode(struct net_device *dev)
  772. {
  773. struct net_local *lp = netdev_priv(dev);
  774. if (lp->chip_type == RTL8002)
  775. return set_rx_mode_8002(dev);
  776. else
  777. return set_rx_mode_8012(dev);
  778. }
  779. static int __init atp_init_module(void) {
  780. if (debug) /* Emit version even if no cards detected. */
  781. printk(KERN_INFO "%s", version);
  782. return atp_init();
  783. }
  784. static void __exit atp_cleanup_module(void) {
  785. struct net_device *next_dev;
  786. while (root_atp_dev) {
  787. struct net_local *atp_local = netdev_priv(root_atp_dev);
  788. next_dev = atp_local->next_module;
  789. unregister_netdev(root_atp_dev);
  790. /* No need to release_region(), since we never snarf it. */
  791. free_netdev(root_atp_dev);
  792. root_atp_dev = next_dev;
  793. }
  794. }
  795. module_init(atp_init_module);
  796. module_exit(atp_cleanup_module);