1/*
2 * eepro100.c -- This is a driver for Intel Fast Ethernet Controllers
3 * (ifec).
4 *
5 * Originally written for Etherboot by:
6 *
7 *   Copyright (C) AW Computer Systems.
8 *   written by R.E.Wolff -- R.E.Wolff@BitWizard.nl
9 *
10 *   AW Computer Systems is contributing to the free software community
11 *   by paying for this driver and then putting the result under GPL.
12 *
13 *   If you need a Linux device driver, please contact BitWizard for a
14 *   quote.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2, or (at
19 * your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 * General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 *
31 *              date       version  by      what
32 *  Written:    May 29 1997  V0.10  REW     Initial revision.
33 * changes:     May 31 1997  V0.90  REW     Works!
34 *              Jun 1  1997  V0.91  REW     Cleanup
35 *              Jun 2  1997  V0.92  REW     Add some code documentation
36 *              Jul 25 1997  V1.00  REW     Tested by AW to work in a PROM
37 *                                          Cleanup for publication
38 *              Dez 11 2004  V1.10  Kiszka  Add RX ring buffer support
39 *              Jun    2008  v2.0   mdeck   Updated to gPXE. Changed much.
40 *
41 * Cleanups and fixes by Thomas Miletich<thomas.miletich@gmail.com>
42 *
43 * This is the etherboot intel etherexpress Pro/100B driver.
44 *
45 * It was written from scratch, with Donald Beckers eepro100.c kernel
46 * driver as a guideline. Mostly the 82557 related definitions and the
47 * lower level routines have been cut-and-pasted into this source.
48 *
49 * The driver was finished before Intel got the NDA out of the closet.
50 *
51 * Datasheet is now published and available from
52 * ftp://download.intel.com/design/network/manuals/8255X_OpenSDM.pdf
53 *    - Michael Brown
54 * */
55
56FILE_LICENCE ( GPL2_OR_LATER );
57
58/*
59 * General Theory of Operation
60 *
61 * Initialization
62 *
63 * ifec_pci_probe() is called by gPXE during initialization. Typical NIC
64 * initialization is performed.  EEPROM data is read.
65 *
66 * Network Boot
67 *
68 * ifec_net_open() is called by gPXE before attempting to network boot from the
69 * card.  Here, the Command Unit & Receive Unit are initialized.  The tx & rx
70 * rings are setup.  The MAC address is programmed and the card is configured.
71 *
72 * Transmit
73 *
74 * ifec_net_transmit() enqueues a packet in the tx ring - active::tcbs[]  The tx
75 * ring is composed of TCBs linked to each other into a ring.  A tx request
76 * fills out the next available TCB with a pointer to the packet data.
77 * The last enqueued tx is always at active::tcb_head.  Thus, a tx request fills
78 * out the TCB following tcb_head.
79 * active::tcb_tail points to the TCB we're awaiting completion of.
80 * ifec_tx_process() checks tcb_tail, and once complete,
81 * blindly increments tcb_tail to the next ring TCB.
82 *
83 * Receive
84 *
85 * priv::rfds[] is an array of Receive Frame Descriptors. The RFDs are linked
86 * together to form a ring.
87 * ifec_net_poll() calls ifec_rx_process(), which checks the next RFD for
88 * data.  If we received a packet, we allocate a new io_buffer and copy the
89 * packet data into it. If alloc_iob() fails, we don't touch the RFD and try
90 * again on the next poll.
91 */
92
93/*
94 * Debugging levels:
95 *	- DBG() is for any errors, i.e. failed alloc_iob(), malloc_dma(),
96 *	  TX overflow, corrupted packets, ...
97 *	- DBG2() is for successful events, like packet received,
98 *	  packet transmitted, and other general notifications.
99 *	- DBGP() prints the name of each called function on entry
100 */
101
102#include <stdint.h>
103#include <byteswap.h>
104#include <errno.h>
105#include <stdio.h>
106#include <unistd.h>
107#include <gpxe/ethernet.h>
108#include <gpxe/if_ether.h>
109#include <gpxe/iobuf.h>
110#include <gpxe/malloc.h>
111#include <gpxe/pci.h>
112#include <gpxe/spi_bit.h>
113#include <gpxe/timer.h>
114#include <gpxe/nvs.h>
115#include <gpxe/threewire.h>
116#include <gpxe/netdevice.h>
117#include "eepro100.h"
118
119/****************************** Global data **********************************/
120
121/*
122 * This is the default configuration command data. The values were copied from
123 * the Linux kernel initialization for the eepro100.
124 */
125static struct ifec_cfg ifec_cfg = {
126	.status  = 0,
127	.command = CmdConfigure | CmdSuspend,
128	.link    = 0,        /* Filled in later */
129	.byte = { 22,        /* How many bytes in this array */
130	          ( TX_FIFO << 4 ) | RX_FIFO,  /* Rx & Tx FIFO limits */
131	          0, 0,                        /* Adaptive Interframe Spacing */
132	          RX_DMA_COUNT,                /* Rx DMA max byte count */
133	          TX_DMA_COUNT + 0x80,         /* Tx DMA max byte count */
134	          0x32,      /* Many bits. */
135	          0x03,      /* Discard short receive & Underrun retries */
136	          1,         /* 1=Use MII  0=Use AUI */
137	          0,
138	          0x2E,      /* NSAI, Preamble length, & Loopback*/
139	          0,         /* Linear priority */
140	          0x60,      /* L PRI MODE & Interframe spacing */
141	          0, 0xf2,
142	          0x48,      /* Promiscuous, Broadcast disable, CRS & CDT */
143	          0, 0x40,
144	          0xf2,      /* Stripping, Padding, Receive CRC Transfer */
145	          0x80,      /* 0x40=Force full-duplex, 0x80=Allowfull-duplex*/
146	          0x3f,      /* Multiple IA */
147	          0x0D }     /* Multicast all */
148};
149
150static struct net_device_operations ifec_operations = {
151	.open     = ifec_net_open,
152	.close    = ifec_net_close,
153	.transmit = ifec_net_transmit,
154	.poll     = ifec_net_poll,
155	.irq      = ifec_net_irq
156};
157
158/******************* gPXE PCI Device Driver API functions ********************/
159
160/*
161 * Initialize the PCI device.
162 *
163 * @v pci 		The device's associated pci_device structure.
164 * @v id  		The PCI device + vendor id.
165 * @ret rc		Returns zero if successfully initialized.
166 *
167 * This function is called very early on, while gPXE is initializing.
168 * This is a gPXE PCI Device Driver API function.
169 */
170static int ifec_pci_probe ( struct pci_device *pci,
171                            const struct pci_device_id *id __unused )
172{
173	struct net_device *netdev;
174	struct ifec_private *priv;
175	int rc;
176
177	DBGP ( "ifec_pci_probe: " );
178
179	if ( pci->ioaddr == 0 )
180		return -EINVAL;
181
182	netdev = alloc_etherdev ( sizeof(*priv) );
183	if ( !netdev )
184		return -ENOMEM;
185
186	netdev_init ( netdev, &ifec_operations );
187	priv = netdev->priv;
188
189	pci_set_drvdata ( pci, netdev );
190	netdev->dev = &pci->dev;
191
192	/* enable bus master, etc */
193	adjust_pci_device( pci );
194
195	DBGP ( "pci " );
196
197	memset ( priv, 0, sizeof(*priv) );
198	priv->ioaddr = pci->ioaddr;
199
200	ifec_reset ( netdev );
201	DBGP ( "reset " );
202
203	ifec_init_eeprom ( netdev );
204
205	/* read MAC address */
206	nvs_read ( &priv->eeprom.nvs, EEPROM_ADDR_MAC_0, netdev->hw_addr,
207		   ETH_ALEN );
208	/* read mdio_register */
209	nvs_read ( &priv->eeprom.nvs, EEPROM_ADDR_MDIO_REGISTER,
210		   &priv->mdio_register, 2 );
211
212	ifec_link_update ( netdev );	/* Update link state */
213
214	if ( ( rc = register_netdev ( netdev ) ) != 0 )
215		goto error;
216
217	DBGP ( "ints\n" );
218
219	return 0;
220
221error:
222	ifec_reset     ( netdev );
223	netdev_nullify ( netdev );
224	netdev_put     ( netdev );
225
226	return rc;
227}
228
229/*
230 * Remove a device from the PCI device list.
231 *
232 * @v pci		PCI device to remove.
233 *
234 * This is a PCI Device Driver API function.
235 */
236static void ifec_pci_remove ( struct pci_device *pci )
237{
238	struct net_device *netdev = pci_get_drvdata ( pci );
239
240	DBGP ( "ifec_pci_remove\n" );
241
242	unregister_netdev ( netdev );
243	ifec_reset        ( netdev );
244	netdev_nullify    ( netdev );
245	netdev_put        ( netdev );
246}
247
248/****************** gPXE Network Device Driver API functions *****************/
249
250/*
251 * Close a network device.
252 *
253 * @v netdev		Device to close.
254 *
255 * This is a gPXE Network Device Driver API function.
256 */
257static void ifec_net_close ( struct net_device *netdev )
258{
259	struct ifec_private *priv = netdev->priv;
260	unsigned long ioaddr = priv->ioaddr;
261	unsigned short intr_status;
262
263	DBGP ( "ifec_net_close\n" );
264
265	/* disable interrupts */
266	ifec_net_irq ( netdev, 0 );
267
268	/* Ack & clear ints */
269	intr_status = inw ( ioaddr + SCBStatus );
270	outw ( intr_status, ioaddr + SCBStatus );
271	inw ( ioaddr + SCBStatus );
272
273	ifec_reset ( netdev );
274
275	/* Free any resources */
276	ifec_free ( netdev );
277}
278
279/* Interrupts to be masked */
280#define INTERRUPT_MASK	( SCBMaskEarlyRx | SCBMaskFlowCtl )
281
282/*
283 * Enable or disable IRQ masking.
284 *
285 * @v netdev		Device to control.
286 * @v enable		Zero to mask off IRQ, non-zero to enable IRQ.
287 *
288 * This is a gPXE Network Driver API function.
289 */
290static void ifec_net_irq ( struct net_device *netdev, int enable )
291{
292	struct ifec_private *priv = netdev->priv;
293	unsigned long ioaddr = priv->ioaddr;
294
295	DBGP ( "ifec_net_irq\n" );
296
297	outw ( enable ? INTERRUPT_MASK : SCBMaskAll, ioaddr + SCBCmd );
298}
299
300/*
301 * Opens a network device.
302 *
303 * @v netdev		Device to be opened.
304 * @ret rc  		Non-zero if failed to open.
305 *
306 * This enables tx and rx on the device.
307 * This is a gPXE Network Device Driver API function.
308 */
309static int ifec_net_open ( struct net_device *netdev )
310{
311	struct ifec_private *priv = netdev->priv;
312	struct ifec_ias *ias = NULL;
313	struct ifec_cfg *cfg = NULL;
314	int i, options;
315	int rc = -ENOMEM;
316
317	DBGP ( "ifec_net_open: " );
318
319	/* Ensure interrupts are disabled. */
320	ifec_net_irq ( netdev, 0 );
321
322	/* Initialize Command Unit and Receive Unit base addresses. */
323	ifec_scb_cmd ( netdev, 0, RUAddrLoad );
324	ifec_scb_cmd ( netdev, virt_to_bus ( &priv->stats ), CUStatsAddr );
325	ifec_scb_cmd ( netdev, 0, CUCmdBase );
326
327	/* Initialize both rings */
328	if ( ( rc = ifec_rx_setup ( netdev ) ) != 0 )
329		goto error;
330	if ( ( rc = ifec_tx_setup ( netdev ) ) != 0 )
331		goto error;
332
333	/* Initialize MDIO */
334	options = 0x00; /* 0x40 = 10mbps half duplex, 0x00 = Autosense */
335	ifec_mdio_setup ( netdev, options );
336
337	/* Prepare MAC address w/ Individual Address Setup (ias) command.*/
338	ias = malloc_dma ( sizeof ( *ias ), CB_ALIGN );
339	if ( !ias ) {
340		rc = -ENOMEM;
341		goto error;
342	}
343	ias->command      = CmdIASetup;
344	ias->status       = 0;
345	memcpy ( ias->ia, netdev->ll_addr, ETH_ALEN );
346
347	/* Prepare operating parameters w/ a configure command. */
348	cfg = malloc_dma ( sizeof ( *cfg ), CB_ALIGN );
349	if ( !cfg ) {
350		rc = -ENOMEM;
351		goto error;
352	}
353	memcpy ( cfg, &ifec_cfg, sizeof ( *cfg ) );
354	cfg->link     = virt_to_bus ( priv->tcbs );
355	cfg->byte[19] = ( options & 0x10 ) ? 0xC0 : 0x80;
356	ias->link     = virt_to_bus ( cfg );
357
358	/* Issue the ias and configure commands. */
359	ifec_scb_cmd ( netdev, virt_to_bus ( ias ), CUStart );
360	ifec_scb_cmd_wait ( netdev );
361	priv->configured = 1;
362
363	/* Wait up to 10 ms for configuration to initiate */
364	for ( i = 10; i && !cfg->status; i-- )
365		mdelay ( 1 );
366	if ( ! cfg->status ) {
367		DBG ( "Failed to initiate!\n" );
368		goto error;
369	}
370	free_dma ( ias, sizeof ( *ias ) );
371	free_dma ( cfg, sizeof ( *cfg ) );
372	DBG2 ( "cfg " );
373
374	/* Enable rx by sending ring address to card */
375	if ( priv->rfds[0] != NULL ) {
376		ifec_scb_cmd ( netdev, virt_to_bus( priv->rfds[0] ), RUStart );
377		ifec_scb_cmd_wait ( netdev );
378	}
379	DBG2 ( "rx_start\n" );
380
381	return 0;
382
383error:
384	free_dma ( cfg, sizeof ( *cfg ) );
385	free_dma ( ias, sizeof ( *ias ) );
386	ifec_free ( netdev );
387	ifec_reset ( netdev );
388	return rc;
389}
390
391/*
392 * This function allows a driver to process events during operation.
393 *
394 * @v netdev		Device being polled.
395 *
396 * This is called periodically by gPXE to let the driver check the status of
397 * transmitted packets and to allow the driver to check for received packets.
398 * This is a gPXE Network Device Driver API function.
399 */
400static void ifec_net_poll ( struct net_device *netdev )
401{
402	struct ifec_private *priv = netdev->priv;
403	static int linkpoll = 0;
404	unsigned short intr_status;
405
406	DBGP ( "ifec_net_poll\n" );
407
408	/* acknowledge interrupts ASAP */
409	intr_status = inw ( priv->ioaddr + SCBStatus );
410	outw ( intr_status, priv->ioaddr + SCBStatus );
411	inw ( priv->ioaddr + SCBStatus );
412
413	DBG2 ( "poll - status: 0x%04X\n", intr_status );
414
415	if ( ++linkpoll > LINK_CHECK_PERIOD ) {
416		linkpoll = 0;
417		ifec_link_update ( netdev );	/* Update link state */
418	}
419
420	/* anything to do here? */
421	if ( ( intr_status & ( ~INTERRUPT_MASK ) ) == 0 )
422		return;
423
424	/* process received and transmitted packets */
425	ifec_tx_process ( netdev );
426	ifec_rx_process ( netdev );
427
428	ifec_check_ru_status ( netdev, intr_status );
429
430	return;
431}
432
433/*
434 * This transmits a packet.
435 *
436 * @v netdev		Device to transmit from.
437 * @v iobuf 		Data to transmit.
438 * @ret rc  		Non-zero if failed to transmit.
439 *
440 * This is a gPXE Network Driver API function.
441 */
442static int ifec_net_transmit ( struct net_device *netdev,
443                               struct io_buffer *iobuf )
444{
445	struct ifec_private *priv = netdev->priv;
446	struct ifec_tcb *tcb = priv->tcb_head->next;
447	unsigned long ioaddr = priv->ioaddr;
448
449	DBGP ( "ifec_net_transmit\n" );
450
451	/* Wait for TCB to become available. */
452	if ( tcb->status || tcb->iob ) {
453		DBG ( "TX overflow\n" );
454		return -ENOBUFS;
455	}
456
457	DBG2 ( "transmitting packet (%d bytes). status = %hX, cmd=%hX\n",
458		iob_len ( iobuf ), tcb->status, inw ( ioaddr + SCBCmd ) );
459
460	tcb->command   = CmdSuspend | CmdTx | CmdTxFlex;
461	tcb->count     = 0x01208000;
462	tcb->tbd_addr0 = virt_to_bus ( iobuf->data );
463	tcb->tbd_size0 = 0x3FFF & iob_len ( iobuf );
464	tcb->iob = iobuf;
465
466	ifec_tx_wake ( netdev );
467
468	/* Append to end of ring. */
469	priv->tcb_head = tcb;
470
471	return 0;
472}
473
474/*************************** Local support functions *************************/
475
476/* Define what each GPIO Pin does */
477static const uint16_t ifec_ee_bits[] = {
478	[SPI_BIT_SCLK]	= EE_SHIFT_CLK,
479	[SPI_BIT_MOSI]	= EE_DATA_WRITE,
480	[SPI_BIT_MISO]	= EE_DATA_READ,
481	[SPI_BIT_SS(0)]	= EE_ENB,
482};
483
484/*
485 * Read a single bit from the GPIO pins used for SPI.
486 * should be called by SPI bitbash functions only
487 *
488 * @v basher		Bitbash device
489 * @v bit_id		Line to be read
490 */
491static int ifec_spi_read_bit ( struct bit_basher *basher,
492			       unsigned int bit_id )
493{
494	struct ifec_private *priv =
495		container_of ( basher, struct ifec_private, spi.basher );
496	unsigned long ee_addr = priv->ioaddr + CSREeprom;
497	unsigned int ret = 0;
498	uint16_t mask;
499
500	DBGP ( "ifec_spi_read_bit\n" );
501
502	mask = ifec_ee_bits[bit_id];
503	ret = inw (ee_addr);
504
505	return ( ret & mask ) ? 1 : 0;
506}
507
508/*
509 * Write a single bit to the GPIO pins used for SPI.
510 * should be called by SPI bitbash functions only
511 *
512 * @v basher		Bitbash device
513 * @v bit_id		Line to write to
514 * @v data		Value to write
515 */
516static void ifec_spi_write_bit ( struct bit_basher *basher,
517				 unsigned int bit_id,
518				 unsigned long data )
519{
520	struct ifec_private *priv =
521		container_of ( basher, struct ifec_private, spi.basher );
522	unsigned long ee_addr = priv->ioaddr + CSREeprom;
523	short val;
524	uint16_t mask = ifec_ee_bits[bit_id];
525
526	DBGP ( "ifec_spi_write_bit\n" );
527
528	val = inw ( ee_addr );
529	val &= ~mask;
530	val |= data & mask;
531
532	outw ( val, ee_addr );
533}
534
535/* set function pointer to SPI read- and write-bit functions */
536static struct bit_basher_operations ifec_basher_ops = {
537	.read = ifec_spi_read_bit,
538	.write = ifec_spi_write_bit,
539};
540
541/*
542 * Initialize the eeprom stuff
543 *
544 * @v netdev		Network device
545 */
546static void ifec_init_eeprom ( struct net_device *netdev )
547{
548	struct ifec_private *priv = netdev->priv;
549
550	DBGP ( "ifec_init_eeprom\n" );
551
552	priv->spi.basher.op = &ifec_basher_ops;
553	priv->spi.bus.mode = SPI_MODE_THREEWIRE;
554	init_spi_bit_basher ( &priv->spi );
555
556	priv->eeprom.bus = &priv->spi.bus;
557
558	/* init as 93c46(93c14 compatible) first, to set the command len,
559	 * block size and word len. Needs to be set for address len detection.
560	 */
561	init_at93c46 ( &priv->eeprom, 16 );
562
563	/* detect address length, */
564	threewire_detect_address_len ( &priv->eeprom );
565
566	/* address len == 8 means 93c66 instead of 93c46 */
567	if ( priv->eeprom.address_len == 8 )
568		init_at93c66 ( &priv->eeprom, 16 );
569}
570
571/*
572 * Check if the network cable is plugged in.
573 *
574 * @v netdev  		Network device to check.
575 * @ret retval		greater 0 if linkup.
576 */
577static int ifec_link_check ( struct net_device *netdev )
578{
579	struct ifec_private *priv = netdev->priv;
580	unsigned short mdio_register = priv->mdio_register;
581
582	DBGP ( "ifec_link_check\n" );
583
584	/* Read the status register once to discard stale data */
585	ifec_mdio_read ( netdev, mdio_register & 0x1f, 1 );
586	/* Check to see if network cable is plugged in. */
587	if ( ! ( ifec_mdio_read ( netdev, mdio_register & 0x1f, 1 )
588		  & ( 1 << 2 ) ) ) {
589		return 0;
590	}
591	return 1;
592}
593
594/*
595 * Check network cable link, inform gPXE as appropriate.
596 *
597 * @v netdev  		Network device to check.
598 */
599static void ifec_link_update ( struct net_device *netdev )
600{
601	DBGP ( "ifec_link_update\n" );
602
603	/* Update link state */
604	if ( ifec_link_check ( netdev ) )
605		netdev_link_up ( netdev );
606	else
607		netdev_link_down ( netdev );
608}
609
610/*
611 * Support function: ifec_mdio_read
612 *
613 * This probably reads a register in the "physical media interface chip".
614 * -- REW
615 */
616static int ifec_mdio_read ( struct net_device *netdev, int phy_id,
617                                                       int location )
618{
619	struct ifec_private *priv = netdev->priv;
620	unsigned long ioaddr = priv->ioaddr;
621	int val;
622	int boguscnt = 64*4;     /* <64 usec. to complete, typ 27 ticks */
623
624	DBGP ( "ifec_mdio_read\n" );
625
626	outl ( 0x08000000 | ( location << 16 ) | ( phy_id << 21 ),
627	       ioaddr + CSRCtrlMDI );
628	do {
629		udelay ( 16 );
630
631		val = inl ( ioaddr + CSRCtrlMDI );
632
633		if ( --boguscnt < 0 ) {
634			DBG ( " ifec_mdio_read() time out with val = %X.\n",
635			         val );
636			break;
637		}
638	} while (! ( val & 0x10000000 ) );
639	return val & 0xffff;
640}
641
642/*
643 * Initializes MDIO.
644 *
645 * @v netdev 		Network device
646 * @v options		MDIO options
647 */
648static void ifec_mdio_setup ( struct net_device *netdev, int options )
649{
650	struct ifec_private *priv = netdev->priv;
651	unsigned short mdio_register = priv->mdio_register;
652
653	DBGP ( "ifec_mdio_setup\n" );
654
655	if (   ( (mdio_register>>8) & 0x3f ) == DP83840
656	    || ( (mdio_register>>8) & 0x3f ) == DP83840A ) {
657		int mdi_reg23 = ifec_mdio_read ( netdev, mdio_register
658						  & 0x1f, 23 ) | 0x0422;
659		if (CONGENB)
660			mdi_reg23 |= 0x0100;
661		DBG2 ( "DP83840 specific setup, setting register 23 to "
662		                                         "%hX.\n", mdi_reg23 );
663		ifec_mdio_write ( netdev, mdio_register & 0x1f, 23, mdi_reg23 );
664	}
665	DBG2 ( "dp83840 " );
666	if ( options != 0 ) {
667		ifec_mdio_write ( netdev, mdio_register & 0x1f, 0,
668		                           ( (options & 0x20) ? 0x2000 : 0 ) |
669		                           ( (options & 0x10) ? 0x0100 : 0 ) );
670		DBG2 ( "set mdio_register. " );
671	}
672}
673
674/*
675 * Support function: ifec_mdio_write
676 *
677 * This probably writes to the "physical media interface chip".
678 * -- REW
679 */
680static int ifec_mdio_write ( struct net_device *netdev,
681                             int phy_id, int location, int value )
682{
683	struct ifec_private *priv = netdev->priv;
684	unsigned long ioaddr = priv->ioaddr;
685	int val;
686	int boguscnt = 64*4;     /* <64 usec. to complete, typ 27 ticks */
687
688	DBGP ( "ifec_mdio_write\n" );
689
690	outl ( 0x04000000 | ( location << 16 ) | ( phy_id << 21 ) | value,
691	       ioaddr + CSRCtrlMDI );
692	do {
693		udelay ( 16 );
694
695		val = inl ( ioaddr + CSRCtrlMDI );
696		if ( --boguscnt < 0 ) {
697			DBG ( " ifec_mdio_write() time out with val = %X.\n",
698			      val );
699			break;
700		}
701	} while (! ( val & 0x10000000 ) );
702	return val & 0xffff;
703}
704
705/*
706 * Resets the hardware.
707 *
708 * @v netdev		Network device
709 */
710static void ifec_reset ( struct net_device *netdev )
711{
712	struct ifec_private *priv = netdev->priv;
713	unsigned long ioaddr = priv->ioaddr;
714
715	DBGP ( "ifec_reset\n" );
716
717	/* do partial reset first */
718	outl ( PortPartialReset, ioaddr + CSRPort );
719	inw ( ioaddr + SCBStatus );
720	udelay ( 20 );
721
722	/* full reset */
723	outl ( PortReset, ioaddr + CSRPort );
724	inw ( ioaddr + SCBStatus );
725	udelay ( 20 );
726
727	/* disable interrupts again */
728	ifec_net_irq ( netdev, 0 );
729}
730
731/*
732 * free()s the tx/rx rings.
733 *
734 * @v netdev		Network device
735 */
736static void ifec_free ( struct net_device *netdev )
737{
738	struct ifec_private *priv = netdev_priv ( netdev );
739	int i;
740
741	DBGP ( "ifec_free\n" );
742
743	/* free all allocated receive io_buffers */
744	for ( i = 0; i < RFD_COUNT; i++ ) {
745		free_iob ( priv->rx_iobs[i] );
746		priv->rx_iobs[i] = NULL;
747		priv->rfds[i] = NULL;
748	}
749
750	/* free TX ring buffer */
751	free_dma ( priv->tcbs, TX_RING_BYTES );
752
753	priv->tcbs = NULL;
754}
755
756/*
757 * Initializes an RFD.
758 *
759 * @v rfd    		RFD struct to initialize
760 * @v command		Command word
761 * @v link   		Link value
762 */
763static void ifec_rfd_init ( struct ifec_rfd *rfd, s16 command, u32 link )
764{
765	DBGP ( "ifec_rfd_init\n" );
766
767	rfd->status      = 0;
768	rfd->command     = command;
769	rfd->rx_buf_addr = 0xFFFFFFFF;
770	rfd->count       = 0;
771	rfd->size        = RFD_PACKET_LEN;
772	rfd->link        = link;
773}
774
775/*
776 * Send address of new RFD to card
777 *
778 * @v netdev		Network device
779 */
780static void ifec_reprime_ru ( struct net_device *netdev )
781{
782	struct ifec_private *priv = netdev->priv;
783	int cur_rx = priv->cur_rx;
784
785	DBGP ( "ifec_reprime_ru\n" );
786
787	if ( priv->rfds[cur_rx] != NULL ) {
788		ifec_scb_cmd ( netdev, virt_to_bus ( priv->rfds[cur_rx] ),
789			       RUStart );
790		ifec_scb_cmd_wait ( netdev );
791	}
792}
793
794/*
795 * Check if reprime of RU needed
796 *
797 * @v netdev		Network device
798 */
799static void ifec_check_ru_status ( struct net_device *netdev,
800				   unsigned short intr_status )
801{
802	struct ifec_private *priv = netdev->priv;
803
804	DBGP ( "ifec_check_ru_status\n" );
805
806	/*
807	* The chip may have suspended reception for various reasons.
808	* Check for that, and re-prime it should this be the case.
809	*/
810	switch ( ( intr_status >> 2 ) & 0xf ) {
811		case 0:  /* Idle */
812		case 4:  /* Ready */
813			break;
814		case 1:  /* Suspended */
815		case 2:  /* No resources (RFDs) */
816		case 9:  /* Suspended with no more RBDs */
817		case 10: /* No resources due to no RBDs */
818		case 12: /* Ready with no RBDs */
819			DBG ( "ifec_net_poll: RU reprimed.\n" );
820			ifec_reprime_ru ( netdev );
821			break;
822		default:
823			/* reserved values */
824			DBG ( "ifec_net_poll: RU state anomaly: %i\n",
825			      ( inw ( priv->ioaddr + SCBStatus ) >> 2 ) & 0xf );
826			break;
827	}
828}
829
830#define RFD_STATUS ( RFD_OK | RFDRxCol | RFDRxErr | RFDShort | \
831		     RFDDMAOverrun | RFDNoBufs | RFDCRCError )
832/*
833 * Looks for received packets in the rx ring, reports success or error to
834 * the core accordingly. Starts reallocation of rx ring.
835 *
836 * @v netdev		Network device
837 */
838static void ifec_rx_process ( struct net_device *netdev )
839{
840	struct ifec_private *priv   = netdev->priv;
841	int cur_rx = priv->cur_rx;
842	struct io_buffer *iob = priv->rx_iobs[cur_rx];
843	struct ifec_rfd *rfd = priv->rfds[cur_rx];
844	unsigned int rx_len;
845	s16 status;
846
847	DBGP ( "ifec_rx_process\n" );
848
849	/* Process any received packets */
850	while ( iob && rfd && ( status = rfd->status ) ) {
851		rx_len = rfd->count & RFDMaskCount;
852
853		DBG2 ( "Got a packet: Len = %d, cur_rx = %d.\n", rx_len,
854		       cur_rx );
855		DBGIO_HD ( (void*)rfd->packet, 0x30 );
856
857		if ( ( status & RFD_STATUS ) != RFD_OK ) {
858			DBG ( "Corrupted packet received. "
859			      "Status = %#08hx\n", status );
860			netdev_rx_err ( netdev, iob, -EINVAL );
861		} else {
862			/* Hand off the packet to the network subsystem */
863			iob_put ( iob, rx_len );
864			DBG2 ( "Received packet: %p, len: %d\n", iob, rx_len );
865			netdev_rx ( netdev, iob );
866		}
867
868		/* make sure we don't reuse this RFD */
869		priv->rx_iobs[cur_rx] = NULL;
870		priv->rfds[cur_rx] = NULL;
871
872		/* Next RFD */
873		priv->cur_rx = ( cur_rx + 1 ) % RFD_COUNT;
874		cur_rx = priv->cur_rx;
875		iob = priv->rx_iobs[cur_rx];
876		rfd = priv->rfds[cur_rx];
877	}
878
879	ifec_refill_rx_ring ( netdev );
880}
881
882/*
883 * Allocates io_buffer, set pointers in ifec_private structure accordingly,
884 * reserves space for RFD header in io_buffer.
885 *
886 * @v netdev		Network device
887 * @v cur		Descriptor number to work on
888 * @v cmd		Value to set cmd field in RFD to
889 * @v link		Pointer to ned RFD
890 * @ret rc		0 on success, negative on failure
891 */
892static int ifec_get_rx_desc ( struct net_device *netdev, int cur, int cmd,
893			      int link )
894{
895	struct ifec_private *priv = netdev->priv;
896	struct ifec_rfd *rfd  = priv->rfds[cur];
897
898	DBGP ( "ifec_get_rx_desc\n" );
899
900	priv->rx_iobs[cur] = alloc_iob ( sizeof ( *rfd ) );
901	if ( ! priv->rx_iobs[cur] ) {
902		DBG ( "alloc_iob failed. desc. nr: %d\n", cur );
903		priv->rfds[cur] = NULL;
904		return -ENOMEM;
905	}
906
907	/* Initialize new tail. */
908	priv->rfds[cur] = priv->rx_iobs[cur]->data;
909	ifec_rfd_init ( priv->rfds[cur], cmd, link );
910	iob_reserve ( priv->rx_iobs[cur], RFD_HEADER_LEN );
911
912	return 0;
913}
914
915/*
916 * Allocate new descriptor entries and initialize them if needed
917 *
918 * @v netdev		Network device
919 */
920static void ifec_refill_rx_ring ( struct net_device *netdev )
921{
922	struct ifec_private *priv = netdev->priv;
923	int i, cur_rx;
924	unsigned short intr_status;
925
926	DBGP ( "ifec_refill_rx_ring\n" );
927
928	for ( i = 0; i < RFD_COUNT; i++ ) {
929		cur_rx = ( priv->cur_rx + i ) % RFD_COUNT;
930		/* only refill if empty */
931		if ( priv->rfds[cur_rx] != NULL ||
932		     priv->rx_iobs[cur_rx] != NULL )
933			continue;
934
935		DBG2 ( "refilling RFD %d\n", cur_rx );
936
937		if ( ifec_get_rx_desc ( netdev, cur_rx,
938		     CmdSuspend | CmdEndOfList, 0 ) == 0 ) {
939			if ( i > 0 ) {
940				int prev_rx = ( ( ( cur_rx + RFD_COUNT ) - 1 )
941						% RFD_COUNT );
942				struct ifec_rfd *rfd = priv->rfds[prev_rx];
943
944				rfd->command = 0;
945				rfd->link = virt_to_bus ( priv->rfds[cur_rx] );
946			}
947		}
948	}
949
950	intr_status = inw ( priv->ioaddr + SCBStatus );
951	ifec_check_ru_status ( netdev, intr_status );
952}
953
954/*
955 * Initial allocation & initialization of the rx ring.
956 *
957 * @v netdev  		Device of rx ring.
958 * @ret rc    		Non-zero if error occured
959 */
960static int ifec_rx_setup ( struct net_device *netdev )
961{
962	struct ifec_private *priv = netdev->priv;
963	int i;
964
965	DBGP ( "ifec_rx_setup\n" );
966
967	priv->cur_rx = 0;
968
969	/* init values for ifec_refill_rx_ring() */
970	for ( i = 0; i < RFD_COUNT; i++ ) {
971		priv->rfds[i] = NULL;
972		priv->rx_iobs[i] = NULL;
973	}
974	ifec_refill_rx_ring ( netdev );
975
976	return 0;
977}
978
979/*
980 * Initiates a SCB command.
981 *
982 * @v netdev		Network device
983 * @v ptr   		General pointer value for command.
984 * @v cmd   		Command to issue.
985 * @ret rc  		Non-zero if command not issued.
986 */
987static int ifec_scb_cmd ( struct net_device *netdev, u32 ptr, u8 cmd )
988{
989	struct ifec_private *priv = netdev->priv;
990	unsigned long ioaddr = priv->ioaddr;
991	int rc;
992
993	DBGP ( "ifec_scb_cmd\n" );
994
995	rc = ifec_scb_cmd_wait ( netdev );	/* Wait until ready */
996	if ( !rc ) {
997		outl ( ptr, ioaddr + SCBPointer );
998		outb ( cmd, ioaddr + SCBCmd );		/* Issue command */
999	}
1000	return rc;
1001}
1002
1003/*
1004 * Wait for command unit to accept a command.
1005 *
1006 * @v cmd_ioaddr	I/O address of command register.
1007 * @ret rc      	Non-zero if command timed out.
1008 */
1009static int ifec_scb_cmd_wait ( struct net_device *netdev )
1010{
1011	struct ifec_private *priv = netdev->priv;
1012	unsigned long cmd_ioaddr = priv->ioaddr + SCBCmd;
1013	int rc, wait = CU_CMD_TIMEOUT;
1014
1015	DBGP ( "ifec_scb_cmd_wait\n" );
1016
1017	for ( ; wait && ( rc = inb ( cmd_ioaddr ) ); wait-- )
1018		udelay ( 1 );
1019
1020	if ( !wait )
1021		DBG ( "ifec_scb_cmd_wait timeout!\n" );
1022	return rc;
1023}
1024
1025/*
1026 * Check status of transmitted packets & perform tx completions.
1027 *
1028 * @v netdev    	Network device.
1029 */
1030static void ifec_tx_process ( struct net_device *netdev )
1031{
1032	struct ifec_private *priv = netdev->priv;
1033	struct ifec_tcb *tcb = priv->tcb_tail;
1034	s16 status;
1035
1036	DBGP ( "ifec_tx_process\n" );
1037
1038	/* Check status of transmitted packets */
1039	while ( ( status = tcb->status ) && tcb->iob ) {
1040		if ( status & TCB_U ) {
1041			/* report error to gPXE */
1042			DBG ( "ifec_tx_process : tx error!\n " );
1043			netdev_tx_complete_err ( netdev, tcb->iob, -EINVAL );
1044		} else {
1045			/* report successful transmit */
1046			netdev_tx_complete ( netdev, tcb->iob );
1047		}
1048		DBG2 ( "tx completion\n" );
1049
1050		tcb->iob = NULL;
1051		tcb->status = 0;
1052
1053		priv->tcb_tail = tcb->next;	/* Next TCB */
1054		tcb = tcb->next;
1055	}
1056}
1057
1058/*
1059 * Allocates & initialize tx resources.
1060 *
1061 * @v netdev    	Network device.
1062 * @ret rc      	Non-zero if error occurred.
1063 */
1064static int ifec_tx_setup ( struct net_device *netdev )
1065{
1066	struct ifec_private *priv = netdev->priv;
1067	struct ifec_tcb *tcb;
1068	int i;
1069
1070	DBGP ( "ifec_tx_setup\n" );
1071
1072	/* allocate tx ring */
1073	priv->tcbs = malloc_dma ( TX_RING_BYTES, CB_ALIGN );
1074	if ( !priv->tcbs ) {
1075		DBG ( "TX-ring allocation failed\n" );
1076		return -ENOMEM;
1077	}
1078
1079	tcb = priv->tcb_tail = priv->tcbs;
1080	priv->tx_curr = priv->tx_tail = 0;
1081	priv->tx_cnt = 0;
1082
1083	for ( i = 0; i < TCB_COUNT; i++, tcb++ ) {
1084		tcb->status    = 0;
1085		tcb->count     = 0x01208000;
1086		tcb->iob       = NULL;
1087		tcb->tbda_addr = virt_to_bus ( &tcb->tbd_addr0 );
1088		tcb->link      = virt_to_bus ( tcb + 1 );
1089		tcb->next      = tcb + 1;
1090	}
1091	/* We point tcb_head at the last TCB, so the first ifec_net_transmit()
1092	 * will use the first (head->next) TCB to transmit. */
1093	priv->tcb_head = --tcb;
1094	tcb->link = virt_to_bus ( priv->tcbs );
1095	tcb->next = priv->tcbs;
1096
1097	return 0;
1098}
1099
1100/*
1101 * Wake up the Command Unit and issue a Resume/Start.
1102 *
1103 * @v netdev		Network device containing Command Unit
1104 *
1105 * The time between clearing the S bit and issuing Resume must be as short as
1106 * possible to prevent a race condition. As noted in linux eepro100.c :
1107 *   Note: Watch out for the potential race condition here: imagine
1108 *	erasing the previous suspend
1109 *		the chip processes the previous command
1110 *		the chip processes the final command, and suspends
1111 *	doing the CU_RESUME
1112 *		the chip processes the next-yet-valid post-final-command.
1113 *   So blindly sending a CU_RESUME is only safe if we do it immediately after
1114 *   erasing the previous CmdSuspend, without the possibility of an intervening
1115 *   delay.
1116 */
1117void ifec_tx_wake ( struct net_device *netdev )
1118{
1119	struct ifec_private *priv = netdev->priv;
1120	unsigned long ioaddr = priv->ioaddr;
1121	struct ifec_tcb *tcb = priv->tcb_head->next;
1122
1123	DBGP ( "ifec_tx_wake\n" );
1124
1125	/* For the special case of the first transmit, we issue a START. The
1126	 * card won't RESUME after the configure command. */
1127	if ( priv->configured ) {
1128		priv->configured = 0;
1129		ifec_scb_cmd ( netdev, virt_to_bus ( tcb ), CUStart );
1130		ifec_scb_cmd_wait ( netdev );
1131		return;
1132	}
1133
1134	/* Resume if suspended. */
1135	switch ( ( inw ( ioaddr + SCBStatus ) >> 6 ) & 0x3 ) {
1136	case 0:  /* Idle - We should not reach this state. */
1137		DBG2 ( "ifec_tx_wake: tx idle!\n" );
1138		ifec_scb_cmd ( netdev, virt_to_bus ( tcb ), CUStart );
1139		ifec_scb_cmd_wait ( netdev );
1140		return;
1141	case 1:  /* Suspended */
1142		DBG2 ( "s" );
1143		break;
1144	default: /* Active */
1145		DBG2 ( "a" );
1146	}
1147	ifec_scb_cmd_wait ( netdev );
1148	outl ( 0, ioaddr + SCBPointer );
1149	priv->tcb_head->command &= ~CmdSuspend;
1150	/* Immediately issue Resume command */
1151	outb ( CUResume, ioaddr + SCBCmd );
1152	ifec_scb_cmd_wait ( netdev );
1153}
1154
1155/*********************************************************************/
1156
1157static struct pci_device_id ifec_nics[] = {
1158PCI_ROM(0x8086, 0x1029, "id1029",        "Intel EtherExpressPro100 ID1029", 0),
1159PCI_ROM(0x8086, 0x1030, "id1030",        "Intel EtherExpressPro100 ID1030", 0),
1160PCI_ROM(0x8086, 0x1031, "82801cam",      "Intel 82801CAM (ICH3) Chipset Ethernet Controller", 0),
1161PCI_ROM(0x8086, 0x1032, "eepro100-1032", "Intel PRO/100 VE Network Connection", 0),
1162PCI_ROM(0x8086, 0x1033, "eepro100-1033", "Intel PRO/100 VM Network Connection", 0),
1163PCI_ROM(0x8086, 0x1034, "eepro100-1034", "Intel PRO/100 VM Network Connection", 0),
1164PCI_ROM(0x8086, 0x1035, "eepro100-1035", "Intel 82801CAM (ICH3) Chipset Ethernet Controller", 0),
1165PCI_ROM(0x8086, 0x1036, "eepro100-1036", "Intel 82801CAM (ICH3) Chipset Ethernet Controller", 0),
1166PCI_ROM(0x8086, 0x1037, "eepro100-1037", "Intel 82801CAM (ICH3) Chipset Ethernet Controller", 0),
1167PCI_ROM(0x8086, 0x1038, "id1038",        "Intel PRO/100 VM Network Connection", 0),
1168PCI_ROM(0x8086, 0x1039, "82562et",       "Intel PRO100 VE 82562ET", 0),
1169PCI_ROM(0x8086, 0x103a, "id103a",        "Intel Corporation 82559 InBusiness 10/100", 0),
1170PCI_ROM(0x8086, 0x103b, "82562etb",      "Intel PRO100 VE 82562ETB", 0),
1171PCI_ROM(0x8086, 0x103c, "eepro100-103c", "Intel PRO/100 VM Network Connection", 0),
1172PCI_ROM(0x8086, 0x103d, "eepro100-103d", "Intel PRO/100 VE Network Connection", 0),
1173PCI_ROM(0x8086, 0x103e, "eepro100-103e", "Intel PRO/100 VM Network Connection", 0),
1174PCI_ROM(0x8086, 0x1051, "prove",         "Intel PRO/100 VE Network Connection", 0),
1175PCI_ROM(0x8086, 0x1059, "82551qm",       "Intel PRO/100 M Mobile Connection", 0),
1176PCI_ROM(0x8086, 0x1209, "82559er",       "Intel EtherExpressPro100 82559ER", 0),
1177PCI_ROM(0x8086, 0x1227, "82865",         "Intel 82865 EtherExpress PRO/100A", 0),
1178PCI_ROM(0x8086, 0x1228, "82556",         "Intel 82556 EtherExpress PRO/100 Smart", 0),
1179PCI_ROM(0x8086, 0x1229, "eepro100",      "Intel EtherExpressPro100", 0),
1180PCI_ROM(0x8086, 0x2449, "82562em",       "Intel EtherExpressPro100 82562EM", 0),
1181PCI_ROM(0x8086, 0x2459, "82562-1",       "Intel 82562 based Fast Ethernet Connection", 0),
1182PCI_ROM(0x8086, 0x245d, "82562-2",       "Intel 82562 based Fast Ethernet Connection", 0),
1183PCI_ROM(0x8086, 0x1050, "82562ez",       "Intel 82562EZ Network Connection", 0),
1184PCI_ROM(0x8086, 0x1051, "eepro100-1051", "Intel 82801EB/ER (ICH5/ICH5R) Chipset Ethernet Controller", 0),
1185PCI_ROM(0x8086, 0x1065, "82562-3",       "Intel 82562 based Fast Ethernet Connection", 0),
1186PCI_ROM(0x8086, 0x5200, "eepro100-5200", "Intel EtherExpress PRO/100 Intelligent Server", 0),
1187PCI_ROM(0x8086, 0x5201, "eepro100-5201", "Intel EtherExpress PRO/100 Intelligent Server", 0),
1188};
1189
1190/* Cards with device ids 0x1030 to 0x103F, 0x2449, 0x2459 or 0x245D might need
1191 * a workaround for hardware bug on 10 mbit half duplex (see linux driver eepro100.c)
1192 * 2003/03/17 gbaum */
1193
1194struct pci_driver ifec_driver __pci_driver = {
1195	.ids      = ifec_nics,
1196	.id_count = ( sizeof (ifec_nics) / sizeof (ifec_nics[0]) ),
1197	.probe    = ifec_pci_probe,
1198	.remove   = ifec_pci_remove
1199};
1200
1201/*
1202 * Local variables:
1203 *  c-basic-offset: 8
1204 *  c-indent-level: 8
1205 *  tab-width: 8
1206 * End:
1207 */
1208