Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / et131x / et131x_initpci.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1301 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 /* Defines for Parameter Default/Min/Max vaules */
101 #define PARM_SPEED_DUPLEX_MIN   0
102 #define PARM_SPEED_DUPLEX_MAX   5
103
104 /* Module parameter for manual speed setting
105  * Set Link speed and dublex manually (0-5)  [0]
106  *  1 : 10Mb   Half-Duplex
107  *  2 : 10Mb   Full-Duplex
108  *  3 : 100Mb  Half-Duplex
109  *  4 : 100Mb  Full-Duplex
110  *  5 : 1000Mb Full-Duplex
111  *  0 : Auto Speed Auto Duplex // default
112  */
113 static u32 et131x_speed_set;
114 module_param(et131x_speed_set, uint, 0);
115 MODULE_PARM_DESC(et131x_speed_set,
116                 "Set Link speed and dublex manually (0-5)  [0]\n \
117                  1 : 10Mb   Half-Duplex\n \
118                  2 : 10Mb   Full-Duplex\n \
119                  3 : 100Mb  Half-Duplex\n \
120                  4 : 100Mb  Full-Duplex\n \
121                  5 : 1000Mb Full-Duplex\n \
122                  0 : Auto Speed Auto Dublex");
123
124 /**
125  * et131x_hwaddr_init - set up the MAC Address on the ET1310
126  * @adapter: pointer to our private adapter structure
127  */
128 void et131x_hwaddr_init(struct et131x_adapter *adapter)
129 {
130         /* If have our default mac from init and no mac address from
131          * EEPROM then we need to generate the last octet and set it on the
132          * device
133          */
134         if (adapter->PermanentAddress[0] == 0x00 &&
135             adapter->PermanentAddress[1] == 0x00 &&
136             adapter->PermanentAddress[2] == 0x00 &&
137             adapter->PermanentAddress[3] == 0x00 &&
138             adapter->PermanentAddress[4] == 0x00 &&
139             adapter->PermanentAddress[5] == 0x00) {
140                 /*
141                  * We need to randomly generate the last octet so we
142                  * decrease our chances of setting the mac address to
143                  * same as another one of our cards in the system
144                  */
145                 get_random_bytes(&adapter->CurrentAddress[5], 1);
146                 /*
147                  * We have the default value in the register we are
148                  * working with so we need to copy the current
149                  * address into the permanent address
150                  */
151                 memcpy(adapter->PermanentAddress,
152                         adapter->CurrentAddress, ETH_ALEN);
153         } else {
154                 /* We do not have an override address, so set the
155                  * current address to the permanent address and add
156                  * it to the device
157                  */
158                 memcpy(adapter->CurrentAddress,
159                        adapter->PermanentAddress, ETH_ALEN);
160         }
161 }
162
163
164 /**
165  * et131x_pci_init       - initial PCI setup
166  * @adapter: pointer to our private adapter structure
167  * @pdev: our PCI device
168  *
169  * Perform the initial setup of PCI registers and if possible initialise
170  * the MAC address. At this point the I/O registers have yet to be mapped
171  */
172
173 static int et131x_pci_init(struct et131x_adapter *adapter,
174                                                 struct pci_dev *pdev)
175 {
176         int i;
177         u8 max_payload;
178         u8 read_size_reg;
179
180         if (et131x_init_eeprom(adapter) < 0)
181                 return -EIO;
182
183         /* Let's set up the PORT LOGIC Register.  First we need to know what
184          * the max_payload_size is
185          */
186         if (pci_read_config_byte(pdev, ET1310_PCI_MAX_PYLD, &max_payload)) {
187                 dev_err(&pdev->dev,
188                     "Could not read PCI config space for Max Payload Size\n");
189                 return -EIO;
190         }
191
192         /* Program the Ack/Nak latency and replay timers */
193         max_payload &= 0x07;    /* Only the lower 3 bits are valid */
194
195         if (max_payload < 2) {
196                 static const u16 AckNak[2] = { 0x76, 0xD0 };
197                 static const u16 Replay[2] = { 0x1E0, 0x2ED };
198
199                 if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
200                                                AckNak[max_payload])) {
201                         dev_err(&pdev->dev,
202                           "Could not write PCI config space for ACK/NAK\n");
203                         return -EIO;
204                 }
205                 if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
206                                                Replay[max_payload])) {
207                         dev_err(&pdev->dev,
208                           "Could not write PCI config space for Replay Timer\n");
209                         return -EIO;
210                 }
211         }
212
213         /* l0s and l1 latency timers.  We are using default values.
214          * Representing 001 for L0s and 010 for L1
215          */
216         if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
217                 dev_err(&pdev->dev,
218                   "Could not write PCI config space for Latency Timers\n");
219                 return -EIO;
220         }
221
222         /* Change the max read size to 2k */
223         if (pci_read_config_byte(pdev, 0x51, &read_size_reg)) {
224                 dev_err(&pdev->dev,
225                         "Could not read PCI config space for Max read size\n");
226                 return -EIO;
227         }
228
229         read_size_reg &= 0x8f;
230         read_size_reg |= 0x40;
231
232         if (pci_write_config_byte(pdev, 0x51, read_size_reg)) {
233                 dev_err(&pdev->dev,
234                       "Could not write PCI config space for Max read size\n");
235                 return -EIO;
236         }
237
238         /* Get MAC address from config space if an eeprom exists, otherwise
239          * the MAC address there will not be valid
240          */
241         if (!adapter->has_eeprom) {
242                 et131x_hwaddr_init(adapter);
243                 return 0;
244         }
245
246         for (i = 0; i < ETH_ALEN; i++) {
247                 if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
248                                         adapter->PermanentAddress + i)) {
249                         dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
250                         return -EIO;
251                 }
252         }
253         memcpy(adapter->CurrentAddress, adapter->PermanentAddress, ETH_ALEN);
254         return 0;
255 }
256
257 /**
258  * et131x_error_timer_handler
259  * @data: timer-specific variable; here a pointer to our adapter structure
260  *
261  * The routine called when the error timer expires, to track the number of
262  * recurring errors.
263  */
264 void et131x_error_timer_handler(unsigned long data)
265 {
266         struct et131x_adapter *etdev = (struct et131x_adapter *) data;
267         u32 pm_csr;
268
269         pm_csr = readl(&etdev->regs->global.pm_csr);
270
271         if ((pm_csr & ET_PM_PHY_SW_COMA) == 0)
272                 UpdateMacStatHostCounters(etdev);
273         else
274                 dev_err(&etdev->pdev->dev,
275                     "No interrupts, in PHY coma, pm_csr = 0x%x\n", pm_csr);
276
277         if (!etdev->Bmsr.bits.link_status &&
278             etdev->RegistryPhyComa &&
279             etdev->PoMgmt.TransPhyComaModeOnBoot < 11) {
280                 etdev->PoMgmt.TransPhyComaModeOnBoot++;
281         }
282
283         if (etdev->PoMgmt.TransPhyComaModeOnBoot == 10) {
284                 if (!etdev->Bmsr.bits.link_status
285                     && etdev->RegistryPhyComa) {
286                         if ((pm_csr & ET_PM_PHY_SW_COMA) == 0) {
287                                 /* NOTE - This was originally a 'sync with
288                                  *  interrupt'. How to do that under Linux?
289                                  */
290                                 et131x_enable_interrupts(etdev);
291                                 EnablePhyComa(etdev);
292                         }
293                 }
294         }
295
296         /* This is a periodic timer, so reschedule */
297         mod_timer(&etdev->ErrorTimer, jiffies +
298                                           TX_ERROR_PERIOD * HZ / 1000);
299 }
300
301 /**
302  * et131x_link_detection_handler
303  *
304  * Timer function for link up at driver load time
305  */
306 void et131x_link_detection_handler(unsigned long data)
307 {
308         struct et131x_adapter *etdev = (struct et131x_adapter *) data;
309         unsigned long flags;
310
311         if (etdev->MediaState == 0) {
312                 spin_lock_irqsave(&etdev->Lock, flags);
313
314                 etdev->MediaState = NETIF_STATUS_MEDIA_DISCONNECT;
315                 etdev->Flags &= ~fMP_ADAPTER_LINK_DETECTION;
316
317                 spin_unlock_irqrestore(&etdev->Lock, flags);
318
319                 netif_carrier_off(etdev->netdev);
320         }
321 }
322
323 /**
324  * et131x_configure_global_regs -       configure JAGCore global regs
325  * @etdev: pointer to our adapter structure
326  *
327  * Used to configure the global registers on the JAGCore
328  */
329 void ConfigGlobalRegs(struct et131x_adapter *etdev)
330 {
331         struct global_regs __iomem *regs = &etdev->regs->global;
332
333         writel(0, &regs->rxq_start_addr);
334         writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
335
336         if (etdev->RegistryJumboPacket < 2048) {
337                 /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
338                  * block of RAM that the driver can split between Tx
339                  * and Rx as it desires.  Our default is to split it
340                  * 50/50:
341                  */
342                 writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
343                 writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
344         } else if (etdev->RegistryJumboPacket < 8192) {
345                 /* For jumbo packets > 2k but < 8k, split 50-50. */
346                 writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
347                 writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
348         } else {
349                 /* 9216 is the only packet size greater than 8k that
350                  * is available. The Tx buffer has to be big enough
351                  * for one whole packet on the Tx side. We'll make
352                  * the Tx 9408, and give the rest to Rx
353                  */
354                 writel(0x01b3, &regs->rxq_end_addr);
355                 writel(0x01b4, &regs->txq_start_addr);
356         }
357
358         /* Initialize the loopback register. Disable all loopbacks. */
359         writel(0, &regs->loopback);
360
361         /* MSI Register */
362         writel(0, &regs->msi_config);
363
364         /* By default, disable the watchdog timer.  It will be enabled when
365          * a packet is queued.
366          */
367         writel(0, &regs->watchdog_timer);
368 }
369
370
371 /**
372  * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
373  * @adapter: pointer to our private adapter structure
374  *
375  * Returns 0 on success, errno on failure (as defined in errno.h)
376  */
377 int et131x_adapter_setup(struct et131x_adapter *etdev)
378 {
379         int status = 0;
380
381         /* Configure the JAGCore */
382         ConfigGlobalRegs(etdev);
383
384         ConfigMACRegs1(etdev);
385
386         /* Configure the MMC registers */
387         /* All we need to do is initialize the Memory Control Register */
388         writel(ET_MMC_ENABLE, &etdev->regs->mmc.mmc_ctrl);
389
390         ConfigRxMacRegs(etdev);
391         ConfigTxMacRegs(etdev);
392
393         ConfigRxDmaRegs(etdev);
394         ConfigTxDmaRegs(etdev);
395
396         ConfigMacStatRegs(etdev);
397
398         /* Move the following code to Timer function?? */
399         status = et131x_xcvr_find(etdev);
400
401         if (status != 0)
402                 dev_warn(&etdev->pdev->dev, "Could not find the xcvr\n");
403
404         /* Prepare the TRUEPHY library. */
405         ET1310_PhyInit(etdev);
406
407         /* Reset the phy now so changes take place */
408         ET1310_PhyReset(etdev);
409
410         /* Power down PHY */
411         ET1310_PhyPowerDown(etdev, 1);
412
413         /*
414          * We need to turn off 1000 base half dulplex, the mac does not
415          * support it. For the 10/100 part, turn off all gig advertisement
416          */
417         if (etdev->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
418                 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_FULL);
419         else
420                 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_NONE);
421
422         /* Power up PHY */
423         ET1310_PhyPowerDown(etdev, 0);
424
425         et131x_setphy_normal(etdev);
426 ;       return status;
427 }
428
429 /**
430  * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
431  * @adapter: pointer to our private adapter structure
432  */
433 void et131x_soft_reset(struct et131x_adapter *adapter)
434 {
435         /* Disable MAC Core */
436         writel(0xc00f0000, &adapter->regs->mac.cfg1);
437
438         /* Set everything to a reset value */
439         writel(0x7F, &adapter->regs->global.sw_reset);
440         writel(0x000f0000, &adapter->regs->mac.cfg1);
441         writel(0x00000000, &adapter->regs->mac.cfg1);
442 }
443
444 /**
445  * et131x_align_allocated_memory - Align allocated memory on a given boundary
446  * @adapter: pointer to our adapter structure
447  * @phys_addr: pointer to Physical address
448  * @offset: pointer to the offset variable
449  * @mask: correct mask
450  */
451 void et131x_align_allocated_memory(struct et131x_adapter *adapter,
452                                    uint64_t *phys_addr,
453                                    uint64_t *offset, uint64_t mask)
454 {
455         uint64_t new_addr;
456
457         *offset = 0;
458
459         new_addr = *phys_addr & ~mask;
460
461         if (new_addr != *phys_addr) {
462                 /* Move to next aligned block */
463                 new_addr += mask + 1;
464                 /* Return offset for adjusting virt addr */
465                 *offset = new_addr - *phys_addr;
466                 /* Return new physical address */
467                 *phys_addr = new_addr;
468         }
469 }
470
471 /**
472  * et131x_adapter_memory_alloc
473  * @adapter: pointer to our private adapter structure
474  *
475  * Returns 0 on success, errno on failure (as defined in errno.h).
476  *
477  * Allocate all the memory blocks for send, receive and others.
478  */
479 int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
480 {
481         int status;
482
483         /* Allocate memory for the Tx Ring */
484         status = et131x_tx_dma_memory_alloc(adapter);
485         if (status != 0) {
486                 dev_err(&adapter->pdev->dev,
487                           "et131x_tx_dma_memory_alloc FAILED\n");
488                 return status;
489         }
490         /* Receive buffer memory allocation */
491         status = et131x_rx_dma_memory_alloc(adapter);
492         if (status != 0) {
493                 dev_err(&adapter->pdev->dev,
494                           "et131x_rx_dma_memory_alloc FAILED\n");
495                 et131x_tx_dma_memory_free(adapter);
496                 return status;
497         }
498
499         /* Init receive data structures */
500         status = et131x_init_recv(adapter);
501         if (status != 0) {
502                 dev_err(&adapter->pdev->dev,
503                         "et131x_init_recv FAILED\n");
504                 et131x_tx_dma_memory_free(adapter);
505                 et131x_rx_dma_memory_free(adapter);
506         }
507         return status;
508 }
509
510 /**
511  * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
512  * @adapter: pointer to our private adapter structure
513  */
514 void et131x_adapter_memory_free(struct et131x_adapter *adapter)
515 {
516         /* Free DMA memory */
517         et131x_tx_dma_memory_free(adapter);
518         et131x_rx_dma_memory_free(adapter);
519 }
520
521
522
523 /**
524  * et131x_adapter_init
525  * @etdev: pointer to the private adapter struct
526  * @pdev: pointer to the PCI device
527  *
528  * Initialize the data structures for the et131x_adapter object and link
529  * them together with the platform provided device structures.
530  */
531
532
533 static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
534                 struct pci_dev *pdev)
535 {
536         static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
537         static const u8 duplex[] = { 0, 1, 2, 1, 2, 2 };
538         static const u16 speed[] = { 0, 10, 10, 100, 100, 1000 };
539
540         struct et131x_adapter *etdev;
541
542         /* Setup the fundamental net_device and private adapter structure elements  */
543         SET_NETDEV_DEV(netdev, &pdev->dev);
544
545         /* Allocate private adapter struct and copy in relevant information */
546         etdev = netdev_priv(netdev);
547         etdev->pdev = pci_dev_get(pdev);
548         etdev->netdev = netdev;
549
550         /* Do the same for the netdev struct */
551         netdev->irq = pdev->irq;
552         netdev->base_addr = pci_resource_start(pdev, 0);
553
554         /* Initialize spinlocks here */
555         spin_lock_init(&etdev->Lock);
556         spin_lock_init(&etdev->TCBSendQLock);
557         spin_lock_init(&etdev->TCBReadyQLock);
558         spin_lock_init(&etdev->SendHWLock);
559         spin_lock_init(&etdev->RcvLock);
560         spin_lock_init(&etdev->RcvPendLock);
561         spin_lock_init(&etdev->FbrLock);
562         spin_lock_init(&etdev->PHYLock);
563
564         /* Parse configuration parameters into the private adapter struct */
565         if (et131x_speed_set)
566                 dev_info(&etdev->pdev->dev,
567                         "Speed set manually to : %d\n", et131x_speed_set);
568
569         etdev->SpeedDuplex = et131x_speed_set;
570         etdev->RegistryJumboPacket = 1514;      /* 1514-9216 */
571
572         /* Set the MAC address to a default */
573         memcpy(etdev->CurrentAddress, default_mac, ETH_ALEN);
574
575         /* Decode SpeedDuplex
576          *
577          * Set up as if we are auto negotiating always and then change if we
578          * go into force mode
579          *
580          * If we are the 10/100 device, and gigabit is somehow requested then
581          * knock it down to 100 full.
582          */
583         if (etdev->pdev->device == ET131X_PCI_DEVICE_ID_FAST &&
584             etdev->SpeedDuplex == 5)
585                 etdev->SpeedDuplex = 4;
586
587         etdev->AiForceSpeed = speed[etdev->SpeedDuplex];
588         etdev->AiForceDpx = duplex[etdev->SpeedDuplex]; /* Auto FDX */
589
590         return etdev;
591 }
592
593 /**
594  * et131x_pci_setup - Perform device initialization
595  * @pdev: a pointer to the device's pci_dev structure
596  * @ent: this device's entry in the pci_device_id table
597  *
598  * Returns 0 on success, errno on failure (as defined in errno.h)
599  *
600  * Registered in the pci_driver structure, this function is called when the
601  * PCI subsystem finds a new PCI device which matches the information
602  * contained in the pci_device_id table. This routine is the equivalent to
603  * a device insertion routine.
604  */
605
606 static int __devinit et131x_pci_setup(struct pci_dev *pdev,
607                                const struct pci_device_id *ent)
608 {
609         int result = -EBUSY;
610         int pm_cap;
611         bool pci_using_dac;
612         struct net_device *netdev;
613         struct et131x_adapter *adapter;
614
615         /* Enable the device via the PCI subsystem */
616         if (pci_enable_device(pdev) != 0) {
617                 dev_err(&pdev->dev,
618                         "pci_enable_device() failed\n");
619                 return -EIO;
620         }
621
622         /* Perform some basic PCI checks */
623         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
624                 dev_err(&pdev->dev,
625                           "Can't find PCI device's base address\n");
626                 goto err_disable;
627         }
628
629         if (pci_request_regions(pdev, DRIVER_NAME)) {
630                 dev_err(&pdev->dev,
631                         "Can't get PCI resources\n");
632                 goto err_disable;
633         }
634
635         /* Enable PCI bus mastering */
636         pci_set_master(pdev);
637
638         /* Query PCI for Power Mgmt Capabilities
639          *
640          * NOTE: Now reading PowerMgmt in another location; is this still
641          * needed?
642          */
643         pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
644         if (pm_cap == 0) {
645                 dev_err(&pdev->dev,
646                           "Cannot find Power Management capabilities\n");
647                 result = -EIO;
648                 goto err_release_res;
649         }
650
651         /* Check the DMA addressing support of this device */
652         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
653                 pci_using_dac = true;
654
655                 result = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
656                 if (result != 0) {
657                         dev_err(&pdev->dev,
658                                   "Unable to obtain 64 bit DMA for consistent allocations\n");
659                         goto err_release_res;
660                 }
661         } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
662                 pci_using_dac = false;
663         } else {
664                 dev_err(&pdev->dev,
665                         "No usable DMA addressing method\n");
666                 result = -EIO;
667                 goto err_release_res;
668         }
669
670         /* Allocate netdev and private adapter structs */
671         netdev = et131x_device_alloc();
672         if (netdev == NULL) {
673                 dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
674                 result = -ENOMEM;
675                 goto err_release_res;
676         }
677         adapter = et131x_adapter_init(netdev, pdev);
678         /* Initialise the PCI setup for the device */
679         et131x_pci_init(adapter, pdev);
680
681         /* Map the bus-relative registers to system virtual memory */
682         adapter->regs = pci_ioremap_bar(pdev, 0);
683         if (adapter->regs == NULL) {
684                 dev_err(&pdev->dev, "Cannot map device registers\n");
685                 result = -ENOMEM;
686                 goto err_free_dev;
687         }
688
689         /* If Phy COMA mode was enabled when we went down, disable it here. */
690         writel(ET_PMCSR_INIT,  &adapter->regs->global.pm_csr);
691
692         /* Issue a global reset to the et1310 */
693         et131x_soft_reset(adapter);
694
695         /* Disable all interrupts (paranoid) */
696         et131x_disable_interrupts(adapter);
697
698         /* Allocate DMA memory */
699         result = et131x_adapter_memory_alloc(adapter);
700         if (result != 0) {
701                 dev_err(&pdev->dev, "Could not alloc adapater memory (DMA)\n");
702                 goto err_iounmap;
703         }
704
705         /* Init send data structures */
706         et131x_init_send(adapter);
707
708         /*
709          * Set up the task structure for the ISR's deferred handler
710          */
711         INIT_WORK(&adapter->task, et131x_isr_handler);
712
713         /* Copy address into the net_device struct */
714         memcpy(netdev->dev_addr, adapter->CurrentAddress, ETH_ALEN);
715
716         /* Setup et1310 as per the documentation */
717         et131x_adapter_setup(adapter);
718
719         /* Create a timer to count errors received by the NIC */
720         init_timer(&adapter->ErrorTimer);
721
722         adapter->ErrorTimer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
723         adapter->ErrorTimer.function = et131x_error_timer_handler;
724         adapter->ErrorTimer.data = (unsigned long)adapter;
725
726         /* Initialize link state */
727         et131x_link_detection_handler((unsigned long)adapter);
728
729         /* Initialize variable for counting how long we do not have
730                                                         link status */
731         adapter->PoMgmt.TransPhyComaModeOnBoot = 0;
732
733         /* We can enable interrupts now
734          *
735          *  NOTE - Because registration of interrupt handler is done in the
736          *         device's open(), defer enabling device interrupts to that
737          *         point
738          */
739
740         /* Register the net_device struct with the Linux network layer */
741         result = register_netdev(netdev);
742         if (result != 0) {
743                 dev_err(&pdev->dev, "register_netdev() failed\n");
744                 goto err_mem_free;
745         }
746
747         /* Register the net_device struct with the PCI subsystem. Save a copy
748          * of the PCI config space for this device now that the device has
749          * been initialized, just in case it needs to be quickly restored.
750          */
751         pci_set_drvdata(pdev, netdev);
752         pci_save_state(adapter->pdev);
753         return result;
754
755 err_mem_free:
756         et131x_adapter_memory_free(adapter);
757 err_iounmap:
758         iounmap(adapter->regs);
759 err_free_dev:
760         pci_dev_put(pdev);
761         free_netdev(netdev);
762 err_release_res:
763         pci_release_regions(pdev);
764 err_disable:
765         pci_disable_device(pdev);
766         return result;
767 }
768
769 /**
770  * et131x_pci_remove
771  * @pdev: a pointer to the device's pci_dev structure
772  *
773  * Registered in the pci_driver structure, this function is called when the
774  * PCI subsystem detects that a PCI device which matches the information
775  * contained in the pci_device_id table has been removed.
776  */
777
778 static void __devexit et131x_pci_remove(struct pci_dev *pdev)
779 {
780         struct net_device *netdev;
781         struct et131x_adapter *adapter;
782
783         /* Retrieve the net_device pointer from the pci_dev struct, as well
784          * as the private adapter struct
785          */
786         netdev = (struct net_device *) pci_get_drvdata(pdev);
787         adapter = netdev_priv(netdev);
788
789         /* Perform device cleanup */
790         unregister_netdev(netdev);
791         et131x_adapter_memory_free(adapter);
792         iounmap(adapter->regs);
793         pci_dev_put(adapter->pdev);
794         free_netdev(netdev);
795         pci_release_regions(pdev);
796         pci_disable_device(pdev);
797 }
798
799 static struct pci_device_id et131x_pci_table[] __devinitdata = {
800         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_GIG, PCI_ANY_ID,
801          PCI_ANY_ID, 0, 0, 0UL},
802         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_FAST, PCI_ANY_ID,
803          PCI_ANY_ID, 0, 0, 0UL},
804         {0,}
805 };
806
807 MODULE_DEVICE_TABLE(pci, et131x_pci_table);
808
809 static struct pci_driver et131x_driver = {
810       .name     = DRIVER_NAME,
811       .id_table = et131x_pci_table,
812       .probe    = et131x_pci_setup,
813       .remove   = __devexit_p(et131x_pci_remove),
814       .suspend  = NULL,         /* et131x_pci_suspend */
815       .resume   = NULL,         /* et131x_pci_resume */
816 };
817
818
819 /**
820  * et131x_init_module - The "main" entry point called on driver initialization
821  *
822  * Returns 0 on success, errno on failure (as defined in errno.h)
823  */
824 static int __init et131x_init_module(void)
825 {
826         if (et131x_speed_set < PARM_SPEED_DUPLEX_MIN ||
827             et131x_speed_set > PARM_SPEED_DUPLEX_MAX) {
828                 printk(KERN_WARNING "et131x: invalid speed setting ignored.\n");
829                 et131x_speed_set = 0;
830         }
831         return pci_register_driver(&et131x_driver);
832 }
833
834 /**
835  * et131x_cleanup_module - The entry point called on driver cleanup
836  */
837 static void __exit et131x_cleanup_module(void)
838 {
839         pci_unregister_driver(&et131x_driver);
840 }
841
842 module_init(et131x_init_module);
843 module_exit(et131x_cleanup_module);
844
845 /* Modinfo parameters (filled out using defines from et131x_version.h) */
846 MODULE_AUTHOR(DRIVER_AUTHOR);
847 MODULE_DESCRIPTION(DRIVER_INFO);
848 MODULE_LICENSE(DRIVER_LICENSE);