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