atp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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/io.h>
  115. #include <asm/dma.h>
  116. #include "atp.h"
  117. MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
  118. MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
  119. MODULE_LICENSE("GPL");
  120. module_param(max_interrupt_work, int, 0);
  121. module_param(debug, int, 0);
  122. module_param_array(io, int, NULL, 0);
  123. module_param_array(irq, int, NULL, 0);
  124. module_param_array(xcvr, int, NULL, 0);
  125. MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
  126. MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
  127. MODULE_PARM_DESC(io, "ATP I/O base address(es)");
  128. MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
  129. MODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)");
  130. /* The number of low I/O ports used by the ethercard. */
  131. #define ETHERCARD_TOTAL_SIZE 3
  132. /* Sequence to switch an 8012 from printer mux to ethernet mode. */
  133. static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
  134. struct net_local {
  135. spinlock_t lock;
  136. struct net_device *next_module;
  137. struct timer_list timer; /* Media selection timer. */
  138. long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */
  139. int saved_tx_size;
  140. unsigned int tx_unit_busy:1;
  141. unsigned char re_tx, /* Number of packet retransmissions. */
  142. addr_mode, /* Current Rx filter e.g. promiscuous, etc. */
  143. pac_cnt_in_tx_buf;
  144. };
  145. /* This code, written by wwc@super.org, resets the adapter every
  146. TIMED_CHECKER ticks. This recovers from an unknown error which
  147. hangs the device. */
  148. #define TIMED_CHECKER (HZ/4)
  149. #ifdef TIMED_CHECKER
  150. #include <linux/timer.h>
  151. static void atp_timed_checker(unsigned long ignored);
  152. #endif
  153. /* Index to functions, as function prototypes. */
  154. static int atp_probe1(long ioaddr);
  155. static void get_node_ID(struct net_device *dev);
  156. static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
  157. static int net_open(struct net_device *dev);
  158. static void hardware_init(struct net_device *dev);
  159. static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
  160. static void trigger_send(long ioaddr, int length);
  161. static netdev_tx_t atp_send_packet(struct sk_buff *skb,
  162. struct net_device *dev);
  163. static irqreturn_t atp_interrupt(int irq, void *dev_id);
  164. static void net_rx(struct net_device *dev);
  165. static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
  166. static int net_close(struct net_device *dev);
  167. static void set_rx_mode(struct net_device *dev);
  168. static void tx_timeout(struct net_device *dev);
  169. /* A list of all installed ATP devices, for removing the driver module. */
  170. static struct net_device *root_atp_dev;
  171. /* Check for a network adapter of this type, and return '0' iff one exists.
  172. If dev->base_addr == 0, probe all likely locations.
  173. If dev->base_addr == 1, always return failure.
  174. If dev->base_addr == 2, allocate space for the device and return success
  175. (detachable devices only).
  176. FIXME: we should use the parport layer for this
  177. */
  178. static int __init atp_init(void)
  179. {
  180. int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
  181. int base_addr = io[0];
  182. if (base_addr > 0x1ff) /* Check a single specified location. */
  183. return atp_probe1(base_addr);
  184. else if (base_addr == 1) /* Don't probe at all. */
  185. return -ENXIO;
  186. for (port = ports; *port; port++) {
  187. long ioaddr = *port;
  188. outb(0x57, ioaddr + PAR_DATA);
  189. if (inb(ioaddr + PAR_DATA) != 0x57)
  190. continue;
  191. if (atp_probe1(ioaddr) == 0)
  192. return 0;
  193. }
  194. return -ENODEV;
  195. }
  196. static const struct net_device_ops atp_netdev_ops = {
  197. .ndo_open = net_open,
  198. .ndo_stop = net_close,
  199. .ndo_start_xmit = atp_send_packet,
  200. .ndo_set_rx_mode = set_rx_mode,
  201. .ndo_tx_timeout = tx_timeout,
  202. .ndo_change_mtu = eth_change_mtu,
  203. .ndo_set_mac_address = eth_mac_addr,
  204. .ndo_validate_addr = eth_validate_addr,
  205. };
  206. static int __init atp_probe1(long ioaddr)
  207. {
  208. struct net_device *dev = NULL;
  209. struct net_local *lp;
  210. int saved_ctrl_reg, status, i;
  211. int res;
  212. outb(0xff, ioaddr + PAR_DATA);
  213. /* Save the original value of the Control register, in case we guessed
  214. wrong. */
  215. saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
  216. if (net_debug > 3)
  217. printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
  218. /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
  219. outb(0x04, ioaddr + PAR_CONTROL);
  220. #ifndef final_version
  221. if (net_debug > 3) {
  222. /* Turn off the printer multiplexer on the 8012. */
  223. for (i = 0; i < 8; i++)
  224. outb(mux_8012[i], ioaddr + PAR_DATA);
  225. write_reg(ioaddr, MODSEL, 0x00);
  226. printk("atp: Registers are ");
  227. for (i = 0; i < 32; i++)
  228. printk(" %2.2x", read_nibble(ioaddr, i));
  229. printk(".\n");
  230. }
  231. #endif
  232. /* Turn off the printer multiplexer on the 8012. */
  233. for (i = 0; i < 8; i++)
  234. outb(mux_8012[i], ioaddr + PAR_DATA);
  235. write_reg_high(ioaddr, CMR1, CMR1h_RESET);
  236. /* udelay() here? */
  237. status = read_nibble(ioaddr, CMR1);
  238. if (net_debug > 3) {
  239. printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
  240. for (i = 0; i < 32; i++)
  241. printk(" %2.2x", read_nibble(ioaddr, i));
  242. printk("\n");
  243. }
  244. if ((status & 0x78) != 0x08) {
  245. /* The pocket adapter probe failed, restore the control register. */
  246. outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
  247. return -ENODEV;
  248. }
  249. status = read_nibble(ioaddr, CMR2_h);
  250. if ((status & 0x78) != 0x10) {
  251. outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
  252. return -ENODEV;
  253. }
  254. dev = alloc_etherdev(sizeof(struct net_local));
  255. if (!dev)
  256. return -ENOMEM;
  257. /* Find the IRQ used by triggering an interrupt. */
  258. write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
  259. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
  260. /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
  261. if (irq[0])
  262. dev->irq = irq[0];
  263. else if (ioaddr == 0x378)
  264. dev->irq = 7;
  265. else
  266. dev->irq = 5;
  267. write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
  268. write_reg(ioaddr, CMR2, CMR2_NULL);
  269. dev->base_addr = ioaddr;
  270. /* Read the station address PROM. */
  271. get_node_ID(dev);
  272. #ifndef MODULE
  273. if (net_debug)
  274. printk(KERN_INFO "%s", version);
  275. #endif
  276. printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
  277. "SAPROM %pM.\n",
  278. dev->name, dev->base_addr, dev->irq, dev->dev_addr);
  279. /* Reset the ethernet hardware and activate the printer pass-through. */
  280. write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
  281. lp = netdev_priv(dev);
  282. lp->addr_mode = CMR2h_Normal;
  283. spin_lock_init(&lp->lock);
  284. /* For the ATP adapter the "if_port" is really the data transfer mode. */
  285. if (xcvr[0])
  286. dev->if_port = xcvr[0];
  287. else
  288. dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
  289. if (dev->mem_end & 0xf)
  290. net_debug = dev->mem_end & 7;
  291. dev->netdev_ops = &atp_netdev_ops;
  292. dev->watchdog_timeo = TX_TIMEOUT;
  293. res = register_netdev(dev);
  294. if (res) {
  295. free_netdev(dev);
  296. return res;
  297. }
  298. lp->next_module = root_atp_dev;
  299. root_atp_dev = dev;
  300. return 0;
  301. }
  302. /* Read the station address PROM, usually a word-wide EEPROM. */
  303. static void __init get_node_ID(struct net_device *dev)
  304. {
  305. long ioaddr = dev->base_addr;
  306. int sa_offset = 0;
  307. int i;
  308. write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
  309. /* Some adapters have the station address at offset 15 instead of offset
  310. zero. Check for it, and fix it if needed. */
  311. if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
  312. sa_offset = 15;
  313. for (i = 0; i < 3; i++)
  314. ((__be16 *)dev->dev_addr)[i] =
  315. cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
  316. write_reg(ioaddr, CMR2, CMR2_NULL);
  317. }
  318. /*
  319. An EEPROM read command starts by shifting out 0x60+address, and then
  320. shifting in the serial data. See the NatSemi databook for details.
  321. * ________________
  322. * CS : __|
  323. * ___ ___
  324. * CLK: ______| |___| |
  325. * __ _______ _______
  326. * DI : __X_______X_______X
  327. * DO : _________X_______X
  328. */
  329. static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
  330. {
  331. unsigned eedata_out = 0;
  332. int num_bits = EE_CMD_SIZE;
  333. while (--num_bits >= 0) {
  334. char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
  335. write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
  336. write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
  337. eedata_out <<= 1;
  338. if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
  339. eedata_out++;
  340. }
  341. write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
  342. return eedata_out;
  343. }
  344. /* Open/initialize the board. This is called (in the current kernel)
  345. sometime after booting when the 'ifconfig' program is run.
  346. This routine sets everything up anew at each open, even
  347. registers that "should" only need to be set once at boot, so that
  348. there is non-reboot way to recover if something goes wrong.
  349. This is an attachable device: if there is no private entry then it wasn't
  350. probed for at boot-time, and we need to probe for it again.
  351. */
  352. static int net_open(struct net_device *dev)
  353. {
  354. struct net_local *lp = netdev_priv(dev);
  355. int ret;
  356. /* The interrupt line is turned off (tri-stated) when the device isn't in
  357. use. That's especially important for "attached" interfaces where the
  358. port or interrupt may be shared. */
  359. ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev);
  360. if (ret)
  361. return ret;
  362. hardware_init(dev);
  363. init_timer(&lp->timer);
  364. lp->timer.expires = jiffies + TIMED_CHECKER;
  365. lp->timer.data = (unsigned long)dev;
  366. lp->timer.function = atp_timed_checker; /* timer handler */
  367. add_timer(&lp->timer);
  368. netif_start_queue(dev);
  369. return 0;
  370. }
  371. /* This routine resets the hardware. We initialize everything, assuming that
  372. the hardware may have been temporarily detached. */
  373. static void hardware_init(struct net_device *dev)
  374. {
  375. struct net_local *lp = netdev_priv(dev);
  376. long ioaddr = dev->base_addr;
  377. int i;
  378. /* Turn off the printer multiplexer on the 8012. */
  379. for (i = 0; i < 8; i++)
  380. outb(mux_8012[i], ioaddr + PAR_DATA);
  381. write_reg_high(ioaddr, CMR1, CMR1h_RESET);
  382. for (i = 0; i < 6; i++)
  383. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  384. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  385. if (net_debug > 2) {
  386. printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
  387. (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
  388. }
  389. write_reg(ioaddr, CMR2, CMR2_IRQOUT);
  390. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
  391. /* Enable the interrupt line from the serial port. */
  392. outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  393. /* Unmask the interesting interrupts. */
  394. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  395. write_reg_high(ioaddr, IMR, ISRh_RxErr);
  396. lp->tx_unit_busy = 0;
  397. lp->pac_cnt_in_tx_buf = 0;
  398. lp->saved_tx_size = 0;
  399. }
  400. static void trigger_send(long ioaddr, int length)
  401. {
  402. write_reg_byte(ioaddr, TxCNT0, length & 0xff);
  403. write_reg(ioaddr, TxCNT1, length >> 8);
  404. write_reg(ioaddr, CMR1, CMR1_Xmit);
  405. }
  406. static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
  407. {
  408. if (length & 1)
  409. {
  410. length++;
  411. pad_len++;
  412. }
  413. outb(EOC+MAR, ioaddr + PAR_DATA);
  414. if ((data_mode & 1) == 0) {
  415. /* Write the packet out, starting with the write addr. */
  416. outb(WrAddr+MAR, ioaddr + PAR_DATA);
  417. do {
  418. write_byte_mode0(ioaddr, *packet++);
  419. } while (--length > pad_len) ;
  420. do {
  421. write_byte_mode0(ioaddr, 0);
  422. } while (--length > 0) ;
  423. } else {
  424. /* Write the packet out in slow mode. */
  425. unsigned char outbyte = *packet++;
  426. outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  427. outb(WrAddr+MAR, ioaddr + PAR_DATA);
  428. outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
  429. outb(outbyte & 0x0f, ioaddr + PAR_DATA);
  430. outbyte >>= 4;
  431. outb(outbyte & 0x0f, ioaddr + PAR_DATA);
  432. outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  433. while (--length > pad_len)
  434. write_byte_mode1(ioaddr, *packet++);
  435. while (--length > 0)
  436. write_byte_mode1(ioaddr, 0);
  437. }
  438. /* Terminate the Tx frame. End of write: ECB. */
  439. outb(0xff, ioaddr + PAR_DATA);
  440. outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  441. }
  442. static void tx_timeout(struct net_device *dev)
  443. {
  444. long ioaddr = dev->base_addr;
  445. printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
  446. inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
  447. : "IRQ conflict");
  448. dev->stats.tx_errors++;
  449. /* Try to restart the adapter. */
  450. hardware_init(dev);
  451. dev->trans_start = jiffies; /* prevent tx timeout */
  452. netif_wake_queue(dev);
  453. dev->stats.tx_errors++;
  454. }
  455. static netdev_tx_t atp_send_packet(struct sk_buff *skb,
  456. struct net_device *dev)
  457. {
  458. struct net_local *lp = netdev_priv(dev);
  459. long ioaddr = dev->base_addr;
  460. int length;
  461. unsigned long flags;
  462. length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  463. netif_stop_queue(dev);
  464. /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
  465. This sequence must not be interrupted by an incoming packet. */
  466. spin_lock_irqsave(&lp->lock, flags);
  467. write_reg(ioaddr, IMR, 0);
  468. write_reg_high(ioaddr, IMR, 0);
  469. spin_unlock_irqrestore(&lp->lock, flags);
  470. write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
  471. lp->pac_cnt_in_tx_buf++;
  472. if (lp->tx_unit_busy == 0) {
  473. trigger_send(ioaddr, length);
  474. lp->saved_tx_size = 0; /* Redundant */
  475. lp->re_tx = 0;
  476. lp->tx_unit_busy = 1;
  477. } else
  478. lp->saved_tx_size = length;
  479. /* Re-enable the LPT interrupts. */
  480. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  481. write_reg_high(ioaddr, IMR, ISRh_RxErr);
  482. dev_kfree_skb (skb);
  483. return NETDEV_TX_OK;
  484. }
  485. /* The typical workload of the driver:
  486. Handle the network interface interrupts. */
  487. static irqreturn_t atp_interrupt(int irq, void *dev_instance)
  488. {
  489. struct net_device *dev = dev_instance;
  490. struct net_local *lp;
  491. long ioaddr;
  492. static int num_tx_since_rx;
  493. int boguscount = max_interrupt_work;
  494. int handled = 0;
  495. ioaddr = dev->base_addr;
  496. lp = netdev_priv(dev);
  497. spin_lock(&lp->lock);
  498. /* Disable additional spurious interrupts. */
  499. outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
  500. /* The adapter's output is currently the IRQ line, switch it to data. */
  501. write_reg(ioaddr, CMR2, CMR2_NULL);
  502. write_reg(ioaddr, IMR, 0);
  503. if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
  504. while (--boguscount > 0) {
  505. int status = read_nibble(ioaddr, ISR);
  506. if (net_debug > 5) printk("loop status %02x..", status);
  507. if (status & (ISR_RxOK<<3)) {
  508. handled = 1;
  509. write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
  510. do {
  511. int read_status = read_nibble(ioaddr, CMR1);
  512. if (net_debug > 6)
  513. printk("handling Rx packet %02x..", read_status);
  514. /* We acknowledged the normal Rx interrupt, so if the interrupt
  515. is still outstanding we must have a Rx error. */
  516. if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
  517. dev->stats.rx_over_errors++;
  518. /* Set to no-accept mode long enough to remove a packet. */
  519. write_reg_high(ioaddr, CMR2, CMR2h_OFF);
  520. net_rx(dev);
  521. /* Clear the interrupt and return to normal Rx mode. */
  522. write_reg_high(ioaddr, ISR, ISRh_RxErr);
  523. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  524. } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
  525. net_rx(dev);
  526. num_tx_since_rx = 0;
  527. } else
  528. break;
  529. } while (--boguscount > 0);
  530. } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
  531. handled = 1;
  532. if (net_debug > 6) printk("handling Tx done..");
  533. /* Clear the Tx interrupt. We should check for too many failures
  534. and reinitialize the adapter. */
  535. write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
  536. if (status & (ISR_TxErr<<3)) {
  537. dev->stats.collisions++;
  538. if (++lp->re_tx > 15) {
  539. dev->stats.tx_aborted_errors++;
  540. hardware_init(dev);
  541. break;
  542. }
  543. /* Attempt to retransmit. */
  544. if (net_debug > 6) printk("attempting to ReTx");
  545. write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
  546. } else {
  547. /* Finish up the transmit. */
  548. dev->stats.tx_packets++;
  549. lp->pac_cnt_in_tx_buf--;
  550. if ( lp->saved_tx_size) {
  551. trigger_send(ioaddr, lp->saved_tx_size);
  552. lp->saved_tx_size = 0;
  553. lp->re_tx = 0;
  554. } else
  555. lp->tx_unit_busy = 0;
  556. netif_wake_queue(dev); /* Inform upper layers. */
  557. }
  558. num_tx_since_rx++;
  559. } else if (num_tx_since_rx > 8 &&
  560. time_after(jiffies, dev->last_rx + HZ)) {
  561. if (net_debug > 2)
  562. printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
  563. "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
  564. num_tx_since_rx, jiffies - dev->last_rx, status,
  565. (read_nibble(ioaddr, CMR1) >> 3) & 15);
  566. dev->stats.rx_missed_errors++;
  567. hardware_init(dev);
  568. num_tx_since_rx = 0;
  569. break;
  570. } else
  571. break;
  572. }
  573. /* This following code fixes a rare (and very difficult to track down)
  574. problem where the adapter forgets its ethernet address. */
  575. {
  576. int i;
  577. for (i = 0; i < 6; i++)
  578. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  579. #if 0 && defined(TIMED_CHECKER)
  580. mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
  581. #endif
  582. }
  583. /* Tell the adapter that it can go back to using the output line as IRQ. */
  584. write_reg(ioaddr, CMR2, CMR2_IRQOUT);
  585. /* Enable the physical interrupt line, which is sure to be low until.. */
  586. outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
  587. /* .. we enable the interrupt sources. */
  588. write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
  589. write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
  590. spin_unlock(&lp->lock);
  591. if (net_debug > 5) printk("exiting interrupt.\n");
  592. return IRQ_RETVAL(handled);
  593. }
  594. #ifdef TIMED_CHECKER
  595. /* This following code fixes a rare (and very difficult to track down)
  596. problem where the adapter forgets its ethernet address. */
  597. static void atp_timed_checker(unsigned long data)
  598. {
  599. struct net_device *dev = (struct net_device *)data;
  600. long ioaddr = dev->base_addr;
  601. struct net_local *lp = netdev_priv(dev);
  602. int tickssofar = jiffies - lp->last_rx_time;
  603. int i;
  604. spin_lock(&lp->lock);
  605. if (tickssofar > 2*HZ) {
  606. #if 1
  607. for (i = 0; i < 6; i++)
  608. write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
  609. lp->last_rx_time = jiffies;
  610. #else
  611. for (i = 0; i < 6; i++)
  612. if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
  613. {
  614. struct net_local *lp = netdev_priv(atp_timed_dev);
  615. write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
  616. if (i == 2)
  617. dev->stats.tx_errors++;
  618. else if (i == 3)
  619. dev->stats.tx_dropped++;
  620. else if (i == 4)
  621. dev->stats.collisions++;
  622. else
  623. dev->stats.rx_errors++;
  624. }
  625. #endif
  626. }
  627. spin_unlock(&lp->lock);
  628. lp->timer.expires = jiffies + TIMED_CHECKER;
  629. add_timer(&lp->timer);
  630. }
  631. #endif
  632. /* We have a good packet(s), get it/them out of the buffers. */
  633. static void net_rx(struct net_device *dev)
  634. {
  635. struct net_local *lp = netdev_priv(dev);
  636. long ioaddr = dev->base_addr;
  637. struct rx_header rx_head;
  638. /* Process the received packet. */
  639. outb(EOC+MAR, ioaddr + PAR_DATA);
  640. read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
  641. if (net_debug > 5)
  642. printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
  643. rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
  644. if ((rx_head.rx_status & 0x77) != 0x01) {
  645. dev->stats.rx_errors++;
  646. if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
  647. else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
  648. if (net_debug > 3)
  649. printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
  650. dev->name, rx_head.rx_status);
  651. if (rx_head.rx_status & 0x0020) {
  652. dev->stats.rx_fifo_errors++;
  653. write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
  654. write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
  655. } else if (rx_head.rx_status & 0x0050)
  656. hardware_init(dev);
  657. return;
  658. } else {
  659. /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
  660. int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
  661. struct sk_buff *skb;
  662. skb = netdev_alloc_skb(dev, pkt_len + 2);
  663. if (skb == NULL) {
  664. dev->stats.rx_dropped++;
  665. goto done;
  666. }
  667. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  668. read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
  669. skb->protocol = eth_type_trans(skb, dev);
  670. netif_rx(skb);
  671. dev->last_rx = jiffies;
  672. dev->stats.rx_packets++;
  673. dev->stats.rx_bytes += pkt_len;
  674. }
  675. done:
  676. write_reg(ioaddr, CMR1, CMR1_NextPkt);
  677. lp->last_rx_time = jiffies;
  678. }
  679. static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
  680. {
  681. if (data_mode <= 3) { /* Mode 0 or 1 */
  682. outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
  683. outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
  684. ioaddr + PAR_DATA);
  685. if (data_mode <= 1) { /* Mode 0 or 1 */
  686. do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0);
  687. } else { /* Mode 2 or 3 */
  688. do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0);
  689. }
  690. } else if (data_mode <= 5) {
  691. do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0);
  692. } else {
  693. do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0);
  694. }
  695. outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
  696. outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
  697. }
  698. /* The inverse routine to net_open(). */
  699. static int
  700. net_close(struct net_device *dev)
  701. {
  702. struct net_local *lp = netdev_priv(dev);
  703. long ioaddr = dev->base_addr;
  704. netif_stop_queue(dev);
  705. del_timer_sync(&lp->timer);
  706. /* Flush the Tx and disable Rx here. */
  707. lp->addr_mode = CMR2h_OFF;
  708. write_reg_high(ioaddr, CMR2, CMR2h_OFF);
  709. /* Free the IRQ line. */
  710. outb(0x00, ioaddr + PAR_CONTROL);
  711. free_irq(dev->irq, dev);
  712. /* Reset the ethernet hardware and activate the printer pass-through. */
  713. write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
  714. return 0;
  715. }
  716. /*
  717. * Set or clear the multicast filter for this adapter.
  718. */
  719. static void set_rx_mode(struct net_device *dev)
  720. {
  721. struct net_local *lp = netdev_priv(dev);
  722. long ioaddr = dev->base_addr;
  723. if (!netdev_mc_empty(dev) || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
  724. lp->addr_mode = CMR2h_PROMISC;
  725. else
  726. lp->addr_mode = CMR2h_Normal;
  727. write_reg_high(ioaddr, CMR2, lp->addr_mode);
  728. }
  729. static int __init atp_init_module(void) {
  730. if (debug) /* Emit version even if no cards detected. */
  731. printk(KERN_INFO "%s", version);
  732. return atp_init();
  733. }
  734. static void __exit atp_cleanup_module(void) {
  735. struct net_device *next_dev;
  736. while (root_atp_dev) {
  737. struct net_local *atp_local = netdev_priv(root_atp_dev);
  738. next_dev = atp_local->next_module;
  739. unregister_netdev(root_atp_dev);
  740. /* No need to release_region(), since we never snarf it. */
  741. free_netdev(root_atp_dev);
  742. root_atp_dev = next_dev;
  743. }
  744. }
  745. module_init(atp_init_module);
  746. module_exit(atp_cleanup_module);