staging: et131x: Remove module_param et131x_speed_set
[pandora-kernel.git] / drivers / staging / et131x / et131x_initpci.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1310 and ET131x series MACs
4  *
5  * Copyright © 2005 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  *------------------------------------------------------------------------------
10  *
11  * et131x_initpci.c - Routines and data used to register the driver with the
12  *                    PCI (and PCI Express) subsystem, as well as basic driver
13  *                    init and startup.
14  *
15  *------------------------------------------------------------------------------
16  *
17  * SOFTWARE LICENSE
18  *
19  * This software is provided subject to the following terms and conditions,
20  * which you should read carefully before using the software.  Using this
21  * software indicates your acceptance of these terms and conditions.  If you do
22  * not agree with these terms and conditions, do not use the software.
23  *
24  * Copyright © 2005 Agere Systems Inc.
25  * All rights reserved.
26  *
27  * Redistribution and use in source or binary forms, with or without
28  * modifications, are permitted provided that the following conditions are met:
29  *
30  * . Redistributions of source code must retain the above copyright notice, this
31  *    list of conditions and the following Disclaimer as comments in the code as
32  *    well as in the documentation and/or other materials provided with the
33  *    distribution.
34  *
35  * . Redistributions in binary form must reproduce the above copyright notice,
36  *    this list of conditions and the following Disclaimer in the documentation
37  *    and/or other materials provided with the distribution.
38  *
39  * . Neither the name of Agere Systems Inc. nor the names of the contributors
40  *    may be used to endorse or promote products derived from this software
41  *    without specific prior written permission.
42  *
43  * Disclaimer
44  *
45  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
48  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
49  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
50  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56  * DAMAGE.
57  *
58  */
59
60 #include "et131x_version.h"
61 #include "et131x_defs.h"
62
63 #include <linux/pci.h>
64 #include <linux/init.h>
65 #include <linux/module.h>
66 #include <linux/types.h>
67 #include <linux/kernel.h>
68
69 #include <linux/sched.h>
70 #include <linux/ptrace.h>
71 #include <linux/ctype.h>
72 #include <linux/string.h>
73 #include <linux/timer.h>
74 #include <linux/interrupt.h>
75 #include <linux/in.h>
76 #include <linux/delay.h>
77 #include <linux/io.h>
78 #include <linux/bitops.h>
79 #include <asm/system.h>
80
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
86 #include <linux/random.h>
87
88 #include "et1310_phy.h"
89
90 #include "et131x_adapter.h"
91
92 #include "et1310_address_map.h"
93 #include "et1310_tx.h"
94 #include "et1310_rx.h"
95 #include "et131x.h"
96
97 #define INTERNAL_MEM_SIZE       0x400   /* 1024 of internal memory */
98 #define INTERNAL_MEM_RX_OFFSET  0x1FF   /* 50%   Tx, 50%   Rx */
99
100 /**
101  * et131x_hwaddr_init - set up the MAC Address on the ET1310
102  * @adapter: pointer to our private adapter structure
103  */
104 void et131x_hwaddr_init(struct et131x_adapter *adapter)
105 {
106         /* If have our default mac from init and no mac address from
107          * EEPROM then we need to generate the last octet and set it on the
108          * device
109          */
110         if (adapter->rom_addr[0] == 0x00 &&
111             adapter->rom_addr[1] == 0x00 &&
112             adapter->rom_addr[2] == 0x00 &&
113             adapter->rom_addr[3] == 0x00 &&
114             adapter->rom_addr[4] == 0x00 &&
115             adapter->rom_addr[5] == 0x00) {
116                 /*
117                  * We need to randomly generate the last octet so we
118                  * decrease our chances of setting the mac address to
119                  * same as another one of our cards in the system
120                  */
121                 get_random_bytes(&adapter->addr[5], 1);
122                 /*
123                  * We have the default value in the register we are
124                  * working with so we need to copy the current
125                  * address into the permanent address
126                  */
127                 memcpy(adapter->rom_addr,
128                         adapter->addr, ETH_ALEN);
129         } else {
130                 /* We do not have an override address, so set the
131                  * current address to the permanent address and add
132                  * it to the device
133                  */
134                 memcpy(adapter->addr,
135                        adapter->rom_addr, ETH_ALEN);
136         }
137 }
138
139
140 /**
141  * et131x_pci_init       - initial PCI setup
142  * @adapter: pointer to our private adapter structure
143  * @pdev: our PCI device
144  *
145  * Perform the initial setup of PCI registers and if possible initialise
146  * the MAC address. At this point the I/O registers have yet to be mapped
147  */
148
149 static int et131x_pci_init(struct et131x_adapter *adapter,
150                                                 struct pci_dev *pdev)
151 {
152         int i;
153         u8 max_payload;
154         u8 read_size_reg;
155
156         if (et131x_init_eeprom(adapter) < 0)
157                 return -EIO;
158
159         /* Let's set up the PORT LOGIC Register.  First we need to know what
160          * the max_payload_size is
161          */
162         if (pci_read_config_byte(pdev, ET1310_PCI_MAX_PYLD, &max_payload)) {
163                 dev_err(&pdev->dev,
164                     "Could not read PCI config space for Max Payload Size\n");
165                 return -EIO;
166         }
167
168         /* Program the Ack/Nak latency and replay timers */
169         max_payload &= 0x07;    /* Only the lower 3 bits are valid */
170
171         if (max_payload < 2) {
172                 static const u16 acknak[2] = { 0x76, 0xD0 };
173                 static const u16 replay[2] = { 0x1E0, 0x2ED };
174
175                 if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
176                                                acknak[max_payload])) {
177                         dev_err(&pdev->dev,
178                           "Could not write PCI config space for ACK/NAK\n");
179                         return -EIO;
180                 }
181                 if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
182                                                replay[max_payload])) {
183                         dev_err(&pdev->dev,
184                           "Could not write PCI config space for Replay Timer\n");
185                         return -EIO;
186                 }
187         }
188
189         /* l0s and l1 latency timers.  We are using default values.
190          * Representing 001 for L0s and 010 for L1
191          */
192         if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
193                 dev_err(&pdev->dev,
194                   "Could not write PCI config space for Latency Timers\n");
195                 return -EIO;
196         }
197
198         /* Change the max read size to 2k */
199         if (pci_read_config_byte(pdev, 0x51, &read_size_reg)) {
200                 dev_err(&pdev->dev,
201                         "Could not read PCI config space for Max read size\n");
202                 return -EIO;
203         }
204
205         read_size_reg &= 0x8f;
206         read_size_reg |= 0x40;
207
208         if (pci_write_config_byte(pdev, 0x51, read_size_reg)) {
209                 dev_err(&pdev->dev,
210                       "Could not write PCI config space for Max read size\n");
211                 return -EIO;
212         }
213
214         /* Get MAC address from config space if an eeprom exists, otherwise
215          * the MAC address there will not be valid
216          */
217         if (!adapter->has_eeprom) {
218                 et131x_hwaddr_init(adapter);
219                 return 0;
220         }
221
222         for (i = 0; i < ETH_ALEN; i++) {
223                 if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
224                                         adapter->rom_addr + i)) {
225                         dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
226                         return -EIO;
227                 }
228         }
229         memcpy(adapter->addr, adapter->rom_addr, ETH_ALEN);
230         return 0;
231 }
232
233 /**
234  * et131x_error_timer_handler
235  * @data: timer-specific variable; here a pointer to our adapter structure
236  *
237  * The routine called when the error timer expires, to track the number of
238  * recurring errors.
239  */
240 void et131x_error_timer_handler(unsigned long data)
241 {
242         struct et131x_adapter *adapter = (struct et131x_adapter *) data;
243         u32 pm_csr;
244
245         pm_csr = readl(&adapter->regs->global.pm_csr);
246
247         if ((pm_csr & ET_PM_PHY_SW_COMA) == 0)
248                 et1310_update_macstat_host_counters(adapter);
249         else
250                 dev_err(&adapter->pdev->dev,
251                     "No interrupts, in PHY coma, pm_csr = 0x%x\n", pm_csr);
252
253         if (!(adapter->bmsr & MI_BMSR_LINK_STATUS) &&
254             adapter->registry_phy_coma &&
255             adapter->boot_coma < 11) {
256                 adapter->boot_coma++;
257         }
258
259         if (adapter->boot_coma == 10) {
260                 if (!(adapter->bmsr & MI_BMSR_LINK_STATUS)
261                     && adapter->registry_phy_coma) {
262                         if ((pm_csr & ET_PM_PHY_SW_COMA) == 0) {
263                                 /* NOTE - This was originally a 'sync with
264                                  *  interrupt'. How to do that under Linux?
265                                  */
266                                 et131x_enable_interrupts(adapter);
267                                 et1310_enable_phy_coma(adapter);
268                         }
269                 }
270         }
271
272         /* This is a periodic timer, so reschedule */
273         mod_timer(&adapter->error_timer, jiffies +
274                                           TX_ERROR_PERIOD * HZ / 1000);
275 }
276
277 /**
278  * et131x_configure_global_regs -       configure JAGCore global regs
279  * @adapter: pointer to our adapter structure
280  *
281  * Used to configure the global registers on the JAGCore
282  */
283 void et131x_configure_global_regs(struct et131x_adapter *adapter)
284 {
285         struct global_regs __iomem *regs = &adapter->regs->global;
286
287         writel(0, &regs->rxq_start_addr);
288         writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
289
290         if (adapter->registry_jumbo_packet < 2048) {
291                 /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
292                  * block of RAM that the driver can split between Tx
293                  * and Rx as it desires.  Our default is to split it
294                  * 50/50:
295                  */
296                 writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
297                 writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
298         } else if (adapter->registry_jumbo_packet < 8192) {
299                 /* For jumbo packets > 2k but < 8k, split 50-50. */
300                 writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
301                 writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
302         } else {
303                 /* 9216 is the only packet size greater than 8k that
304                  * is available. The Tx buffer has to be big enough
305                  * for one whole packet on the Tx side. We'll make
306                  * the Tx 9408, and give the rest to Rx
307                  */
308                 writel(0x01b3, &regs->rxq_end_addr);
309                 writel(0x01b4, &regs->txq_start_addr);
310         }
311
312         /* Initialize the loopback register. Disable all loopbacks. */
313         writel(0, &regs->loopback);
314
315         /* MSI Register */
316         writel(0, &regs->msi_config);
317
318         /* By default, disable the watchdog timer.  It will be enabled when
319          * a packet is queued.
320          */
321         writel(0, &regs->watchdog_timer);
322 }
323
324 /**
325  * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
326  * @adapter: pointer to our private adapter structure
327  *
328  * Returns 0 on success, errno on failure (as defined in errno.h)
329  */
330 int et131x_adapter_setup(struct et131x_adapter *adapter)
331 {
332         int status = 0;
333
334         /* Configure the JAGCore */
335         et131x_configure_global_regs(adapter);
336
337         et1310_config_mac_regs1(adapter);
338
339         /* Configure the MMC registers */
340         /* All we need to do is initialize the Memory Control Register */
341         writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
342
343         et1310_config_rxmac_regs(adapter);
344         et1310_config_txmac_regs(adapter);
345
346         et131x_config_rx_dma_regs(adapter);
347         et131x_config_tx_dma_regs(adapter);
348
349         et1310_config_macstat_regs(adapter);
350
351         /* Move the following code to Timer function?? */
352         status = et131x_xcvr_find(adapter);
353
354         if (status != 0)
355                 dev_warn(&adapter->pdev->dev, "Could not find the xcvr\n");
356
357         /* Prepare the TRUEPHY library. */
358         et1310_phy_init(adapter);
359
360         /* Reset the phy now so changes take place */
361         et1310_phy_reset(adapter);
362
363         /* Power down PHY */
364         et1310_phy_power_down(adapter, 1);
365
366         /*
367          * We need to turn off 1000 base half dulplex, the mac does not
368          * support it. For the 10/100 part, turn off all gig advertisement
369          */
370         if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
371                 et1310_phy_advertise_1000BaseT(adapter, TRUEPHY_ADV_DUPLEX_FULL);
372         else
373                 et1310_phy_advertise_1000BaseT(adapter, TRUEPHY_ADV_DUPLEX_NONE);
374
375         /* Power up PHY */
376         et1310_phy_power_down(adapter, 0);
377
378         et131x_setphy_normal(adapter);
379         return status;
380 }
381
382 /**
383  * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
384  * @adapter: pointer to our private adapter structure
385  */
386 void et131x_soft_reset(struct et131x_adapter *adapter)
387 {
388         /* Disable MAC Core */
389         writel(0xc00f0000, &adapter->regs->mac.cfg1);
390
391         /* Set everything to a reset value */
392         writel(0x7F, &adapter->regs->global.sw_reset);
393         writel(0x000f0000, &adapter->regs->mac.cfg1);
394         writel(0x00000000, &adapter->regs->mac.cfg1);
395 }
396
397 /**
398  * et131x_align_allocated_memory - Align allocated memory on a given boundary
399  * @adapter: pointer to our adapter structure
400  * @phys_addr: pointer to Physical address
401  * @offset: pointer to the offset variable
402  * @mask: correct mask
403  */
404 void et131x_align_allocated_memory(struct et131x_adapter *adapter,
405                                    uint64_t *phys_addr,
406                                    uint64_t *offset, uint64_t mask)
407 {
408         uint64_t new_addr;
409
410         *offset = 0;
411
412         new_addr = *phys_addr & ~mask;
413
414         if (new_addr != *phys_addr) {
415                 /* Move to next aligned block */
416                 new_addr += mask + 1;
417                 /* Return offset for adjusting virt addr */
418                 *offset = new_addr - *phys_addr;
419                 /* Return new physical address */
420                 *phys_addr = new_addr;
421         }
422 }
423
424 /**
425  * et131x_adapter_memory_alloc
426  * @adapter: pointer to our private adapter structure
427  *
428  * Returns 0 on success, errno on failure (as defined in errno.h).
429  *
430  * Allocate all the memory blocks for send, receive and others.
431  */
432 int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
433 {
434         int status;
435
436         /* Allocate memory for the Tx Ring */
437         status = et131x_tx_dma_memory_alloc(adapter);
438         if (status != 0) {
439                 dev_err(&adapter->pdev->dev,
440                           "et131x_tx_dma_memory_alloc FAILED\n");
441                 return status;
442         }
443         /* Receive buffer memory allocation */
444         status = et131x_rx_dma_memory_alloc(adapter);
445         if (status != 0) {
446                 dev_err(&adapter->pdev->dev,
447                           "et131x_rx_dma_memory_alloc FAILED\n");
448                 et131x_tx_dma_memory_free(adapter);
449                 return status;
450         }
451
452         /* Init receive data structures */
453         status = et131x_init_recv(adapter);
454         if (status != 0) {
455                 dev_err(&adapter->pdev->dev,
456                         "et131x_init_recv FAILED\n");
457                 et131x_tx_dma_memory_free(adapter);
458                 et131x_rx_dma_memory_free(adapter);
459         }
460         return status;
461 }
462
463 /**
464  * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
465  * @adapter: pointer to our private adapter structure
466  */
467 void et131x_adapter_memory_free(struct et131x_adapter *adapter)
468 {
469         /* Free DMA memory */
470         et131x_tx_dma_memory_free(adapter);
471         et131x_rx_dma_memory_free(adapter);
472 }
473
474 /**
475  * et131x_adapter_init
476  * @adapter: pointer to the private adapter struct
477  * @pdev: pointer to the PCI device
478  *
479  * Initialize the data structures for the et131x_adapter object and link
480  * them together with the platform provided device structures.
481  */
482 static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
483                 struct pci_dev *pdev)
484 {
485         static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
486         static const u8 duplex[] = { 0, 1, 2, 1, 2, 2 };
487         static const u16 speed[] = { 0, 10, 10, 100, 100, 1000 };
488
489         struct et131x_adapter *adapter;
490
491         /* Allocate private adapter struct and copy in relevant information */
492         adapter = netdev_priv(netdev);
493         adapter->pdev = pci_dev_get(pdev);
494         adapter->netdev = netdev;
495
496         /* Do the same for the netdev struct */
497         netdev->irq = pdev->irq;
498         netdev->base_addr = pci_resource_start(pdev, 0);
499
500         /* Initialize spinlocks here */
501         spin_lock_init(&adapter->lock);
502         spin_lock_init(&adapter->tcb_send_qlock);
503         spin_lock_init(&adapter->tcb_ready_qlock);
504         spin_lock_init(&adapter->send_hw_lock);
505         spin_lock_init(&adapter->rcv_lock);
506         spin_lock_init(&adapter->rcv_pend_lock);
507         spin_lock_init(&adapter->fbr_lock);
508         spin_lock_init(&adapter->phy_lock);
509
510         adapter->speed_duplex = 0; /* Auto Speed Auto Duplex */
511         adapter->registry_jumbo_packet = 1514;  /* 1514-9216 */
512
513         /* Set the MAC address to a default */
514         memcpy(adapter->addr, default_mac, ETH_ALEN);
515
516         adapter->ai_force_speed = speed[adapter->speed_duplex];
517         adapter->ai_force_duplex = duplex[adapter->speed_duplex];       /* Auto FDX */
518
519         return adapter;
520 }
521
522 /**
523  * et131x_pci_setup - Perform device initialization
524  * @pdev: a pointer to the device's pci_dev structure
525  * @ent: this device's entry in the pci_device_id table
526  *
527  * Returns 0 on success, errno on failure (as defined in errno.h)
528  *
529  * Registered in the pci_driver structure, this function is called when the
530  * PCI subsystem finds a new PCI device which matches the information
531  * contained in the pci_device_id table. This routine is the equivalent to
532  * a device insertion routine.
533  */
534 static int __devinit et131x_pci_setup(struct pci_dev *pdev,
535                                const struct pci_device_id *ent)
536 {
537         int result;
538         int pm_cap;
539         struct net_device *netdev;
540         struct et131x_adapter *adapter;
541
542         /* Enable the device via the PCI subsystem */
543         result = pci_enable_device(pdev);
544         if (result) {
545                 dev_err(&pdev->dev, "pci_enable_device() failed\n");
546                 goto err_out;
547         }
548
549         /* Perform some basic PCI checks */
550         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
551                 dev_err(&pdev->dev, "Can't find PCI device's base address\n");
552                 goto err_disable;
553         }
554
555         if (pci_request_regions(pdev, DRIVER_NAME)) {
556                 dev_err(&pdev->dev, "Can't get PCI resources\n");
557                 goto err_disable;
558         }
559
560         pci_set_master(pdev);
561
562         /* Query PCI for Power Mgmt Capabilities
563          *
564          * NOTE: Now reading PowerMgmt in another location; is this still
565          * needed?
566          */
567         pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
568         if (!pm_cap) {
569                 dev_err(&pdev->dev,
570                           "Cannot find Power Management capabilities\n");
571                 result = -EIO;
572                 goto err_release_res;
573         }
574
575         /* Check the DMA addressing support of this device */
576         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
577                 result = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
578                 if (result) {
579                         dev_err(&pdev->dev,
580                           "Unable to obtain 64 bit DMA for consistent allocations\n");
581                         goto err_release_res;
582                 }
583         } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
584                 result = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
585                 if (result) {
586                         dev_err(&pdev->dev,
587                           "Unable to obtain 32 bit DMA for consistent allocations\n");
588                         goto err_release_res;
589                 }
590         } else {
591                 dev_err(&pdev->dev, "No usable DMA addressing method\n");
592                 result = -EIO;
593                 goto err_release_res;
594         }
595
596         /* Allocate netdev and private adapter structs */
597         netdev = et131x_device_alloc();
598         if (!netdev) {
599                 dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
600                 result = -ENOMEM;
601                 goto err_release_res;
602         }
603
604         SET_NETDEV_DEV(netdev, &pdev->dev);
605
606         adapter = et131x_adapter_init(netdev, pdev);
607         /* Initialise the PCI setup for the device */
608         et131x_pci_init(adapter, pdev);
609
610         /* Map the bus-relative registers to system virtual memory */
611         adapter->regs = pci_ioremap_bar(pdev, 0);
612         if (!adapter->regs) {
613                 dev_err(&pdev->dev, "Cannot map device registers\n");
614                 result = -ENOMEM;
615                 goto err_free_dev;
616         }
617
618         /* If Phy COMA mode was enabled when we went down, disable it here. */
619         writel(ET_PMCSR_INIT,  &adapter->regs->global.pm_csr);
620
621         /* Issue a global reset to the et1310 */
622         et131x_soft_reset(adapter);
623
624         /* Disable all interrupts (paranoid) */
625         et131x_disable_interrupts(adapter);
626
627         /* Allocate DMA memory */
628         result = et131x_adapter_memory_alloc(adapter);
629         if (result) {
630                 dev_err(&pdev->dev, "Could not alloc adapater memory (DMA)\n");
631                 goto err_iounmap;
632         }
633
634         /* Init send data structures */
635         et131x_init_send(adapter);
636
637         /* Set up the task structure for the ISR's deferred handler */
638         INIT_WORK(&adapter->task, et131x_isr_handler);
639
640         /* Copy address into the net_device struct */
641         memcpy(netdev->dev_addr, adapter->addr, ETH_ALEN);
642
643         /* Setup et1310 as per the documentation */
644         et131x_adapter_setup(adapter);
645
646         /* Create a timer to count errors received by the NIC */
647         init_timer(&adapter->error_timer);
648
649         adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
650         adapter->error_timer.function = et131x_error_timer_handler;
651         adapter->error_timer.data = (unsigned long)adapter;
652
653         /* Initialize link state */
654         netif_carrier_off(adapter->netdev);
655
656         /* Init variable for counting how long we do not have link status */
657         adapter->boot_coma = 0;
658
659         /* We can enable interrupts now
660          *
661          *  NOTE - Because registration of interrupt handler is done in the
662          *         device's open(), defer enabling device interrupts to that
663          *         point
664          */
665
666         /* Register the net_device struct with the Linux network layer */
667         result = register_netdev(netdev);
668         if (result != 0) {
669                 dev_err(&pdev->dev, "register_netdev() failed\n");
670                 goto err_mem_free;
671         }
672
673         /* Register the net_device struct with the PCI subsystem. Save a copy
674          * of the PCI config space for this device now that the device has
675          * been initialized, just in case it needs to be quickly restored.
676          */
677         pci_set_drvdata(pdev, netdev);
678         pci_save_state(adapter->pdev);
679
680         return result;
681
682 err_mem_free:
683         et131x_adapter_memory_free(adapter);
684 err_iounmap:
685         iounmap(adapter->regs);
686 err_free_dev:
687         pci_dev_put(pdev);
688         free_netdev(netdev);
689 err_release_res:
690         pci_release_regions(pdev);
691 err_disable:
692         pci_disable_device(pdev);
693 err_out:
694         return result;
695 }
696
697 /**
698  * et131x_pci_remove
699  * @pdev: a pointer to the device's pci_dev structure
700  *
701  * Registered in the pci_driver structure, this function is called when the
702  * PCI subsystem detects that a PCI device which matches the information
703  * contained in the pci_device_id table has been removed.
704  */
705 static void __devexit et131x_pci_remove(struct pci_dev *pdev)
706 {
707         struct net_device *netdev;
708         struct et131x_adapter *adapter;
709
710         /* Retrieve the net_device pointer from the pci_dev struct, as well
711          * as the private adapter struct
712          */
713         netdev = pci_get_drvdata(pdev);
714         adapter = netdev_priv(netdev);
715
716         /* Perform device cleanup */
717         unregister_netdev(netdev);
718         et131x_adapter_memory_free(adapter);
719         iounmap(adapter->regs);
720         pci_dev_put(adapter->pdev);
721         free_netdev(netdev);
722         pci_release_regions(pdev);
723         pci_disable_device(pdev);
724 }
725
726 static struct pci_device_id et131x_pci_table[] __devinitdata = {
727         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_GIG, PCI_ANY_ID,
728          PCI_ANY_ID, 0, 0, 0UL},
729         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_FAST, PCI_ANY_ID,
730          PCI_ANY_ID, 0, 0, 0UL},
731         {0,}
732 };
733
734 MODULE_DEVICE_TABLE(pci, et131x_pci_table);
735
736 static struct pci_driver et131x_driver = {
737         .name           = DRIVER_NAME,
738         .id_table       = et131x_pci_table,
739         .probe          = et131x_pci_setup,
740         .remove         = __devexit_p(et131x_pci_remove),
741         .suspend        = NULL,         /* et131x_pci_suspend */
742         .resume         = NULL,         /* et131x_pci_resume */
743 };
744
745 /**
746  * et131x_init_module - The "main" entry point called on driver initialization
747  *
748  * Returns 0 on success, errno on failure (as defined in errno.h)
749  */
750 static int __init et131x_init_module(void)
751 {
752         return pci_register_driver(&et131x_driver);
753 }
754
755 /**
756  * et131x_cleanup_module - The entry point called on driver cleanup
757  */
758 static void __exit et131x_cleanup_module(void)
759 {
760         pci_unregister_driver(&et131x_driver);
761 }
762
763 module_init(et131x_init_module);
764 module_exit(et131x_cleanup_module);
765
766 /* Modinfo parameters (filled out using defines from et131x_version.h) */
767 MODULE_AUTHOR(DRIVER_AUTHOR);
768 MODULE_DESCRIPTION(DRIVER_INFO);
769 MODULE_LICENSE(DRIVER_LICENSE);